Merge remote-tracking branch 'stable/linux-5.15.y' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / drivers / regulator / rpi-panel-attiny-regulator.c
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_NONE,
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
148         return state->port_states[REG_PORTC - REG_PORTA] & PC_RST_BRIDGE_N;
149 }
150
151 static const struct regulator_init_data attiny_regulator_default = {
152         .constraints = {
153                 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
154         },
155 };
156
157 static const struct regulator_ops attiny_regulator_ops = {
158         .enable = attiny_lcd_power_enable,
159         .disable = attiny_lcd_power_disable,
160         .is_enabled = attiny_lcd_power_is_enabled,
161 };
162
163 static const struct regulator_desc attiny_regulator = {
164         .name   = "tc358762-power",
165         .ops    = &attiny_regulator_ops,
166         .type   = REGULATOR_VOLTAGE,
167         .owner  = THIS_MODULE,
168 };
169
170 static int attiny_update_status(struct backlight_device *bl)
171 {
172         struct attiny_lcd *state = bl_get_data(bl);
173         struct regmap *regmap = state->regmap;
174         int brightness = bl->props.brightness;
175         int ret, i;
176
177         mutex_lock(&state->lock);
178
179         if (bl->props.power != FB_BLANK_UNBLANK ||
180             bl->props.fb_blank != FB_BLANK_UNBLANK)
181                 brightness = 0;
182
183         for (i = 0; i < 10; i++) {
184                 ret = regmap_write(regmap, REG_PWM, brightness);
185                 if (!ret)
186                         break;
187         }
188
189         mutex_unlock(&state->lock);
190
191         return ret;
192 }
193
194 static const struct backlight_ops attiny_bl = {
195         .update_status  = attiny_update_status,
196 };
197
198 static int attiny_gpio_get_direction(struct gpio_chip *gc, unsigned int off)
199 {
200         return GPIO_LINE_DIRECTION_OUT;
201 }
202
203 static void attiny_gpio_set(struct gpio_chip *gc, unsigned int off, int val)
204 {
205         struct attiny_lcd *state = gpiochip_get_data(gc);
206         u8 last_val;
207
208         if (off >= NUM_GPIO)
209                 return;
210
211         mutex_lock(&state->lock);
212
213         last_val = attiny_get_port_state(state, mappings[off].reg);
214         if (val)
215                 last_val |= mappings[off].mask;
216         else
217                 last_val &= ~mappings[off].mask;
218
219         attiny_set_port_state(state, mappings[off].reg, last_val);
220
221         if (off == RST_BRIDGE_N && val) {
222                 usleep_range(5000, 8000);
223                 regmap_write(state->regmap, REG_ADDR_H, 0x04);
224                 usleep_range(5000, 8000);
225                 regmap_write(state->regmap, REG_ADDR_L, 0x7c);
226                 usleep_range(5000, 8000);
227                 regmap_write(state->regmap, REG_WRITE_DATA_H, 0x00);
228                 usleep_range(5000, 8000);
229                 regmap_write(state->regmap, REG_WRITE_DATA_L, 0x00);
230
231                 msleep(100);
232         }
233
234         mutex_unlock(&state->lock);
235 }
236
237 static int attiny_i2c_read(struct i2c_client *client, u8 reg, unsigned int *buf)
238 {
239         struct i2c_msg msgs[1];
240         u8 addr_buf[1] = { reg };
241         u8 data_buf[1] = { 0, };
242         int ret;
243
244         /* Write register address */
245         msgs[0].addr = client->addr;
246         msgs[0].flags = 0;
247         msgs[0].len = ARRAY_SIZE(addr_buf);
248         msgs[0].buf = addr_buf;
249
250         ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
251         if (ret != ARRAY_SIZE(msgs))
252                 return -EIO;
253
254         usleep_range(5000, 10000);
255
256         /* Read data from register */
257         msgs[0].addr = client->addr;
258         msgs[0].flags = I2C_M_RD;
259         msgs[0].len = 1;
260         msgs[0].buf = data_buf;
261
262         ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
263         if (ret != ARRAY_SIZE(msgs))
264                 return -EIO;
265
266         *buf = data_buf[0];
267         return 0;
268 }
269
270 /*
271  * I2C driver interface functions
272  */
273 static int attiny_i2c_probe(struct i2c_client *i2c,
274                 const struct i2c_device_id *id)
275 {
276         struct backlight_properties props = { };
277         struct regulator_config config = { };
278         struct backlight_device *bl;
279         struct regulator_dev *rdev;
280         struct attiny_lcd *state;
281         struct regmap *regmap;
282         unsigned int data;
283         int ret;
284
285         state = devm_kzalloc(&i2c->dev, sizeof(*state), GFP_KERNEL);
286         if (!state)
287                 return -ENOMEM;
288
289         mutex_init(&state->lock);
290         i2c_set_clientdata(i2c, state);
291
292         regmap = devm_regmap_init_i2c(i2c, &attiny_regmap_config);
293         if (IS_ERR(regmap)) {
294                 ret = PTR_ERR(regmap);
295                 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
296                         ret);
297                 goto error;
298         }
299
300         ret = attiny_i2c_read(i2c, REG_ID, &data);
301         if (ret < 0) {
302                 dev_err(&i2c->dev, "Failed to read REG_ID reg: %d\n", ret);
303                 goto error;
304         }
305
306         switch (data) {
307         case 0xde: /* ver 1 */
308         case 0xc3: /* ver 2 */
309                 break;
310         default:
311                 dev_err(&i2c->dev, "Unknown Atmel firmware revision: 0x%02x\n", data);
312                 ret = -ENODEV;
313                 goto error;
314         }
315
316         regmap_write(regmap, REG_POWERON, 0);
317         msleep(30);
318         regmap_write(regmap, REG_PWM, 0);
319
320         config.dev = &i2c->dev;
321         config.regmap = regmap;
322         config.of_node = i2c->dev.of_node;
323         config.init_data = &attiny_regulator_default;
324         config.driver_data = state;
325
326         rdev = devm_regulator_register(&i2c->dev, &attiny_regulator, &config);
327         if (IS_ERR(rdev)) {
328                 dev_err(&i2c->dev, "Failed to register ATTINY regulator\n");
329                 ret = PTR_ERR(rdev);
330                 goto error;
331         }
332
333         props.type = BACKLIGHT_RAW;
334         props.max_brightness = 0xff;
335
336         state->regmap = regmap;
337
338         bl = devm_backlight_device_register(&i2c->dev, dev_name(&i2c->dev),
339                                             &i2c->dev, state, &attiny_bl,
340                                             &props);
341         if (IS_ERR(bl)) {
342                 ret = PTR_ERR(bl);
343                 goto error;
344         }
345
346         bl->props.brightness = 0xff;
347
348         state->gc.parent = &i2c->dev;
349         state->gc.label = i2c->name;
350         state->gc.owner = THIS_MODULE;
351         state->gc.of_node = i2c->dev.of_node;
352         state->gc.base = -1;
353         state->gc.ngpio = NUM_GPIO;
354
355         state->gc.set = attiny_gpio_set;
356         state->gc.get_direction = attiny_gpio_get_direction;
357         state->gc.can_sleep = true;
358
359         ret = devm_gpiochip_add_data(&i2c->dev, &state->gc, state);
360         if (ret) {
361                 dev_err(&i2c->dev, "Failed to create gpiochip: %d\n", ret);
362                 goto error;
363         }
364
365         return 0;
366
367 error:
368         mutex_destroy(&state->lock);
369
370         return ret;
371 }
372
373 static int attiny_i2c_remove(struct i2c_client *client)
374 {
375         struct attiny_lcd *state = i2c_get_clientdata(client);
376
377         mutex_destroy(&state->lock);
378
379         return 0;
380 }
381
382 static const struct of_device_id attiny_dt_ids[] = {
383         { .compatible = "raspberrypi,7inch-touchscreen-panel-regulator" },
384         {},
385 };
386 MODULE_DEVICE_TABLE(of, attiny_dt_ids);
387
388 static struct i2c_driver attiny_regulator_driver = {
389         .driver = {
390                 .name = "rpi_touchscreen_attiny",
391                 .of_match_table = of_match_ptr(attiny_dt_ids),
392         },
393         .probe = attiny_i2c_probe,
394         .remove = attiny_i2c_remove,
395 };
396
397 module_i2c_driver(attiny_regulator_driver);
398
399 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
400 MODULE_DESCRIPTION("Regulator device driver for Raspberry Pi 7-inch touchscreen");
401 MODULE_LICENSE("GPL v2");