1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2020 Marek Vasut <marex@denx.de>
4  *
5  * Based on rpi_touchscreen.c by Eric Anholt <eric@anholt.net>
6  */
7 
8 #include <linux/backlight.h>
9 #include <linux/err.h>
10 #include <linux/gpio.h>
11 #include <linux/gpio/driver.h>
12 #include <linux/i2c.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/regmap.h>
17 #include <linux/regulator/driver.h>
18 #include <linux/regulator/machine.h>
19 #include <linux/regulator/of_regulator.h>
20 #include <linux/slab.h>
21 
22 /* I2C registers of the Atmel microcontroller. */
23 #define REG_ID		0x80
24 #define REG_PORTA	0x81
25 #define REG_PORTB	0x82
26 #define REG_PORTC	0x83
27 #define REG_POWERON	0x85
28 #define REG_PWM		0x86
29 #define REG_ADDR_L	0x8c
30 #define REG_ADDR_H	0x8d
31 #define REG_WRITE_DATA_H	0x90
32 #define REG_WRITE_DATA_L	0x91
33 
34 #define PA_LCD_DITHB		BIT(0)
35 #define PA_LCD_MODE		BIT(1)
36 #define PA_LCD_LR		BIT(2)
37 #define PA_LCD_UD		BIT(3)
38 
39 #define PB_BRIDGE_PWRDNX_N	BIT(0)
40 #define PB_LCD_VCC_N		BIT(1)
41 #define PB_LCD_MAIN		BIT(7)
42 
43 #define PC_LED_EN		BIT(0)
44 #define PC_RST_TP_N		BIT(1)
45 #define PC_RST_LCD_N		BIT(2)
46 #define PC_RST_BRIDGE_N		BIT(3)
47 
48 enum gpio_signals {
49 	RST_BRIDGE_N,	/* TC358762 bridge reset */
50 	RST_TP_N,	/* Touch controller reset */
51 	NUM_GPIO
52 };
53 
54 struct gpio_signal_mappings {
55 	unsigned int reg;
56 	unsigned int mask;
57 };
58 
59 static const struct gpio_signal_mappings mappings[NUM_GPIO] = {
60 	[RST_BRIDGE_N] = { REG_PORTC, PC_RST_BRIDGE_N | PC_RST_LCD_N  },
61 	[RST_TP_N] = { REG_PORTC, PC_RST_TP_N },
62 };
63 
64 struct attiny_lcd {
65 	/* lock to serialise overall accesses to the Atmel */
66 	struct mutex	lock;
67 	struct regmap	*regmap;
68 	bool gpio_states[NUM_GPIO];
69 	u8 port_states[3];
70 
71 	struct gpio_chip gc;
72 };
73 
74 static const struct regmap_config attiny_regmap_config = {
75 	.reg_bits = 8,
76 	.val_bits = 8,
77 	.disable_locking = 1,
78 	.max_register = REG_WRITE_DATA_L,
79 	.cache_type = REGCACHE_RBTREE,
80 };
81 
82 static int attiny_set_port_state(struct attiny_lcd *state, int reg, u8 val)
83 {
84 	state->port_states[reg - REG_PORTA] = val;
85 	return regmap_write(state->regmap, reg, val);
86 };
87 
88 static u8 attiny_get_port_state(struct attiny_lcd *state, int reg)
89 {
90 	return state->port_states[reg - REG_PORTA];
91 };
92 
93 static int attiny_lcd_power_enable(struct regulator_dev *rdev)
94 {
95 	struct attiny_lcd *state = rdev_get_drvdata(rdev);
96 
97 	mutex_lock(&state->lock);
98 
99 	/* Ensure bridge, and tp stay in reset */
100 	attiny_set_port_state(state, REG_PORTC, 0);
101 	usleep_range(5000, 10000);
102 
103 	/* Default to the same orientation as the closed source
104 	 * firmware used for the panel.  Runtime rotation
105 	 * configuration will be supported using VC4's plane
106 	 * orientation bits.
107 	 */
108 	attiny_set_port_state(state, REG_PORTA, PA_LCD_LR);
109 	usleep_range(5000, 10000);
110 	/* Main regulator on, and power to the panel (LCD_VCC_N) */
111 	attiny_set_port_state(state, REG_PORTB, PB_LCD_MAIN);
112 	usleep_range(5000, 10000);
113 	/* Bring controllers out of reset */
114 	attiny_set_port_state(state, REG_PORTC, PC_LED_EN);
115 
116 	msleep(80);
117 
118 	mutex_unlock(&state->lock);
119 
120 	return 0;
121 }
122 
123 static int attiny_lcd_power_disable(struct regulator_dev *rdev)
124 {
125 	struct attiny_lcd *state = rdev_get_drvdata(rdev);
126 
127 	mutex_lock(&state->lock);
128 
129 	regmap_write(rdev->regmap, REG_PWM, 0);
130 	usleep_range(5000, 10000);
131 
132 	attiny_set_port_state(state, REG_PORTA, 0);
133 	usleep_range(5000, 10000);
134 	attiny_set_port_state(state, REG_PORTB, PB_LCD_VCC_N);
135 	usleep_range(5000, 10000);
136 	attiny_set_port_state(state, REG_PORTC, 0);
137 	msleep(30);
138 
139 	mutex_unlock(&state->lock);
140 
141 	return 0;
142 }
143 
144 static int attiny_lcd_power_is_enabled(struct regulator_dev *rdev)
145 {
146 	struct attiny_lcd *state = rdev_get_drvdata(rdev);
147 	unsigned int data;
148 	int ret, i;
149 
150 	mutex_lock(&state->lock);
151 
152 	for (i = 0; i < 10; i++) {
153 		ret = regmap_read(rdev->regmap, REG_PORTC, &data);
154 		if (!ret)
155 			break;
156 		usleep_range(10000, 12000);
157 	}
158 
159 	mutex_unlock(&state->lock);
160 
161 	if (ret < 0)
162 		return ret;
163 
164 	return data & PC_RST_BRIDGE_N;
165 }
166 
167 static const struct regulator_init_data attiny_regulator_default = {
168 	.constraints = {
169 		.valid_ops_mask = REGULATOR_CHANGE_STATUS,
170 	},
171 };
172 
173 static const struct regulator_ops attiny_regulator_ops = {
174 	.enable = attiny_lcd_power_enable,
175 	.disable = attiny_lcd_power_disable,
176 	.is_enabled = attiny_lcd_power_is_enabled,
177 };
178 
179 static const struct regulator_desc attiny_regulator = {
180 	.name	= "tc358762-power",
181 	.ops	= &attiny_regulator_ops,
182 	.type	= REGULATOR_VOLTAGE,
183 	.owner	= THIS_MODULE,
184 };
185 
186 static int attiny_update_status(struct backlight_device *bl)
187 {
188 	struct attiny_lcd *state = bl_get_data(bl);
189 	struct regmap *regmap = state->regmap;
190 	int brightness = backlight_get_brightness(bl);
191 	int ret, i;
192 
193 	mutex_lock(&state->lock);
194 
195 	for (i = 0; i < 10; i++) {
196 		ret = regmap_write(regmap, REG_PWM, brightness);
197 		if (!ret)
198 			break;
199 	}
200 
201 	mutex_unlock(&state->lock);
202 
203 	return ret;
204 }
205 
206 static const struct backlight_ops attiny_bl = {
207 	.update_status	= attiny_update_status,
208 };
209 
210 static int attiny_gpio_get_direction(struct gpio_chip *gc, unsigned int off)
211 {
212 	return GPIO_LINE_DIRECTION_OUT;
213 }
214 
215 static void attiny_gpio_set(struct gpio_chip *gc, unsigned int off, int val)
216 {
217 	struct attiny_lcd *state = gpiochip_get_data(gc);
218 	u8 last_val;
219 
220 	if (off >= NUM_GPIO)
221 		return;
222 
223 	mutex_lock(&state->lock);
224 
225 	last_val = attiny_get_port_state(state, mappings[off].reg);
226 	if (val)
227 		last_val |= mappings[off].mask;
228 	else
229 		last_val &= ~mappings[off].mask;
230 
231 	attiny_set_port_state(state, mappings[off].reg, last_val);
232 
233 	if (off == RST_BRIDGE_N && val) {
234 		usleep_range(5000, 8000);
235 		regmap_write(state->regmap, REG_ADDR_H, 0x04);
236 		usleep_range(5000, 8000);
237 		regmap_write(state->regmap, REG_ADDR_L, 0x7c);
238 		usleep_range(5000, 8000);
239 		regmap_write(state->regmap, REG_WRITE_DATA_H, 0x00);
240 		usleep_range(5000, 8000);
241 		regmap_write(state->regmap, REG_WRITE_DATA_L, 0x00);
242 
243 		msleep(100);
244 	}
245 
246 	mutex_unlock(&state->lock);
247 }
248 
249 static int attiny_i2c_read(struct i2c_client *client, u8 reg, unsigned int *buf)
250 {
251 	struct i2c_msg msgs[1];
252 	u8 addr_buf[1] = { reg };
253 	u8 data_buf[1] = { 0, };
254 	int ret;
255 
256 	/* Write register address */
257 	msgs[0].addr = client->addr;
258 	msgs[0].flags = 0;
259 	msgs[0].len = ARRAY_SIZE(addr_buf);
260 	msgs[0].buf = addr_buf;
261 
262 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
263 	if (ret != ARRAY_SIZE(msgs))
264 		return -EIO;
265 
266 	usleep_range(5000, 10000);
267 
268 	/* Read data from register */
269 	msgs[0].addr = client->addr;
270 	msgs[0].flags = I2C_M_RD;
271 	msgs[0].len = 1;
272 	msgs[0].buf = data_buf;
273 
274 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
275 	if (ret != ARRAY_SIZE(msgs))
276 		return -EIO;
277 
278 	*buf = data_buf[0];
279 	return 0;
280 }
281 
282 /*
283  * I2C driver interface functions
284  */
285 static int attiny_i2c_probe(struct i2c_client *i2c,
286 		const struct i2c_device_id *id)
287 {
288 	struct backlight_properties props = { };
289 	struct regulator_config config = { };
290 	struct backlight_device *bl;
291 	struct regulator_dev *rdev;
292 	struct attiny_lcd *state;
293 	struct regmap *regmap;
294 	unsigned int data;
295 	int ret;
296 
297 	state = devm_kzalloc(&i2c->dev, sizeof(*state), GFP_KERNEL);
298 	if (!state)
299 		return -ENOMEM;
300 
301 	mutex_init(&state->lock);
302 	i2c_set_clientdata(i2c, state);
303 
304 	regmap = devm_regmap_init_i2c(i2c, &attiny_regmap_config);
305 	if (IS_ERR(regmap)) {
306 		ret = PTR_ERR(regmap);
307 		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
308 			ret);
309 		goto error;
310 	}
311 
312 	ret = attiny_i2c_read(i2c, REG_ID, &data);
313 	if (ret < 0) {
314 		dev_err(&i2c->dev, "Failed to read REG_ID reg: %d\n", ret);
315 		goto error;
316 	}
317 
318 	switch (data) {
319 	case 0xde: /* ver 1 */
320 	case 0xc3: /* ver 2 */
321 		break;
322 	default:
323 		dev_err(&i2c->dev, "Unknown Atmel firmware revision: 0x%02x\n", data);
324 		ret = -ENODEV;
325 		goto error;
326 	}
327 
328 	regmap_write(regmap, REG_POWERON, 0);
329 	msleep(30);
330 	regmap_write(regmap, REG_PWM, 0);
331 
332 	config.dev = &i2c->dev;
333 	config.regmap = regmap;
334 	config.of_node = i2c->dev.of_node;
335 	config.init_data = &attiny_regulator_default;
336 	config.driver_data = state;
337 
338 	rdev = devm_regulator_register(&i2c->dev, &attiny_regulator, &config);
339 	if (IS_ERR(rdev)) {
340 		dev_err(&i2c->dev, "Failed to register ATTINY regulator\n");
341 		ret = PTR_ERR(rdev);
342 		goto error;
343 	}
344 
345 	props.type = BACKLIGHT_RAW;
346 	props.max_brightness = 0xff;
347 
348 	state->regmap = regmap;
349 
350 	bl = devm_backlight_device_register(&i2c->dev, dev_name(&i2c->dev),
351 					    &i2c->dev, state, &attiny_bl,
352 					    &props);
353 	if (IS_ERR(bl)) {
354 		ret = PTR_ERR(bl);
355 		goto error;
356 	}
357 
358 	bl->props.brightness = 0xff;
359 
360 	state->gc.parent = &i2c->dev;
361 	state->gc.label = i2c->name;
362 	state->gc.owner = THIS_MODULE;
363 	state->gc.base = -1;
364 	state->gc.ngpio = NUM_GPIO;
365 
366 	state->gc.set = attiny_gpio_set;
367 	state->gc.get_direction = attiny_gpio_get_direction;
368 	state->gc.can_sleep = true;
369 
370 	ret = devm_gpiochip_add_data(&i2c->dev, &state->gc, state);
371 	if (ret) {
372 		dev_err(&i2c->dev, "Failed to create gpiochip: %d\n", ret);
373 		goto error;
374 	}
375 
376 	return 0;
377 
378 error:
379 	mutex_destroy(&state->lock);
380 
381 	return ret;
382 }
383 
384 static int attiny_i2c_remove(struct i2c_client *client)
385 {
386 	struct attiny_lcd *state = i2c_get_clientdata(client);
387 
388 	mutex_destroy(&state->lock);
389 
390 	return 0;
391 }
392 
393 static const struct of_device_id attiny_dt_ids[] = {
394 	{ .compatible = "raspberrypi,7inch-touchscreen-panel-regulator" },
395 	{},
396 };
397 MODULE_DEVICE_TABLE(of, attiny_dt_ids);
398 
399 static struct i2c_driver attiny_regulator_driver = {
400 	.driver = {
401 		.name = "rpi_touchscreen_attiny",
402 		.of_match_table = of_match_ptr(attiny_dt_ids),
403 	},
404 	.probe = attiny_i2c_probe,
405 	.remove	= attiny_i2c_remove,
406 };
407 
408 module_i2c_driver(attiny_regulator_driver);
409 
410 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
411 MODULE_DESCRIPTION("Regulator device driver for Raspberry Pi 7-inch touchscreen");
412 MODULE_LICENSE("GPL v2");
413