1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020 Marek Vasut <marex@denx.de>
5 * Based on rpi_touchscreen.c by Eric Anholt <eric@anholt.net>
8 #include <linux/backlight.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>
22 /* I2C registers of the Atmel microcontroller. */
24 #define REG_PORTA 0x81
25 #define REG_PORTB 0x82
26 #define REG_PORTC 0x83
27 #define REG_POWERON 0x85
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
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)
39 #define PB_BRIDGE_PWRDNX_N BIT(0)
40 #define PB_LCD_VCC_N BIT(1)
41 #define PB_LCD_MAIN BIT(7)
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)
49 RST_BRIDGE_N, /* TC358762 bridge reset */
50 RST_TP_N, /* Touch controller reset */
54 struct gpio_signal_mappings {
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 },
65 /* lock to serialise overall accesses to the Atmel */
67 struct regmap *regmap;
68 bool gpio_states[NUM_GPIO];
74 static const struct regmap_config attiny_regmap_config = {
78 .max_register = REG_WRITE_DATA_L,
79 .cache_type = REGCACHE_NONE,
82 static int attiny_set_port_state(struct attiny_lcd *state, int reg, u8 val)
84 state->port_states[reg - REG_PORTA] = val;
85 return regmap_write(state->regmap, reg, val);
88 static u8 attiny_get_port_state(struct attiny_lcd *state, int reg)
90 return state->port_states[reg - REG_PORTA];
93 static int attiny_lcd_power_enable(struct regulator_dev *rdev)
95 struct attiny_lcd *state = rdev_get_drvdata(rdev);
97 mutex_lock(&state->lock);
99 /* Ensure bridge, and tp stay in reset */
100 attiny_set_port_state(state, REG_PORTC, 0);
101 usleep_range(5000, 10000);
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
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);
118 mutex_unlock(&state->lock);
123 static int attiny_lcd_power_disable(struct regulator_dev *rdev)
125 struct attiny_lcd *state = rdev_get_drvdata(rdev);
127 mutex_lock(&state->lock);
129 regmap_write(rdev->regmap, REG_PWM, 0);
130 usleep_range(5000, 10000);
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);
139 mutex_unlock(&state->lock);
144 static int attiny_lcd_power_is_enabled(struct regulator_dev *rdev)
146 struct attiny_lcd *state = rdev_get_drvdata(rdev);
148 return state->port_states[REG_PORTC - REG_PORTA] & PC_RST_BRIDGE_N;
151 static const struct regulator_init_data attiny_regulator_default = {
153 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
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,
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,
170 static int attiny_update_status(struct backlight_device *bl)
172 struct attiny_lcd *state = bl_get_data(bl);
173 struct regmap *regmap = state->regmap;
174 int brightness = bl->props.brightness;
177 mutex_lock(&state->lock);
179 if (bl->props.power != FB_BLANK_UNBLANK ||
180 bl->props.fb_blank != FB_BLANK_UNBLANK)
183 for (i = 0; i < 10; i++) {
184 ret = regmap_write(regmap, REG_PWM, brightness);
189 mutex_unlock(&state->lock);
194 static const struct backlight_ops attiny_bl = {
195 .update_status = attiny_update_status,
198 static int attiny_gpio_get_direction(struct gpio_chip *gc, unsigned int off)
200 return GPIO_LINE_DIRECTION_OUT;
203 static void attiny_gpio_set(struct gpio_chip *gc, unsigned int off, int val)
205 struct attiny_lcd *state = gpiochip_get_data(gc);
211 mutex_lock(&state->lock);
213 last_val = attiny_get_port_state(state, mappings[off].reg);
215 last_val |= mappings[off].mask;
217 last_val &= ~mappings[off].mask;
219 attiny_set_port_state(state, mappings[off].reg, last_val);
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);
234 mutex_unlock(&state->lock);
237 static int attiny_i2c_read(struct i2c_client *client, u8 reg, unsigned int *buf)
239 struct i2c_msg msgs[1];
240 u8 addr_buf[1] = { reg };
241 u8 data_buf[1] = { 0, };
244 /* Write register address */
245 msgs[0].addr = client->addr;
247 msgs[0].len = ARRAY_SIZE(addr_buf);
248 msgs[0].buf = addr_buf;
250 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
251 if (ret != ARRAY_SIZE(msgs))
254 usleep_range(5000, 10000);
256 /* Read data from register */
257 msgs[0].addr = client->addr;
258 msgs[0].flags = I2C_M_RD;
260 msgs[0].buf = data_buf;
262 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
263 if (ret != ARRAY_SIZE(msgs))
271 * I2C driver interface functions
273 static int attiny_i2c_probe(struct i2c_client *i2c,
274 const struct i2c_device_id *id)
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;
285 state = devm_kzalloc(&i2c->dev, sizeof(*state), GFP_KERNEL);
289 mutex_init(&state->lock);
290 i2c_set_clientdata(i2c, state);
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",
300 ret = attiny_i2c_read(i2c, REG_ID, &data);
302 dev_err(&i2c->dev, "Failed to read REG_ID reg: %d\n", ret);
307 case 0xde: /* ver 1 */
308 case 0xc3: /* ver 2 */
311 dev_err(&i2c->dev, "Unknown Atmel firmware revision: 0x%02x\n", data);
316 regmap_write(regmap, REG_POWERON, 0);
318 regmap_write(regmap, REG_PWM, 0);
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;
326 rdev = devm_regulator_register(&i2c->dev, &attiny_regulator, &config);
328 dev_err(&i2c->dev, "Failed to register ATTINY regulator\n");
333 props.type = BACKLIGHT_RAW;
334 props.max_brightness = 0xff;
336 state->regmap = regmap;
338 bl = devm_backlight_device_register(&i2c->dev, dev_name(&i2c->dev),
339 &i2c->dev, state, &attiny_bl,
346 bl->props.brightness = 0xff;
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;
353 state->gc.ngpio = NUM_GPIO;
355 state->gc.set = attiny_gpio_set;
356 state->gc.get_direction = attiny_gpio_get_direction;
357 state->gc.can_sleep = true;
359 ret = devm_gpiochip_add_data(&i2c->dev, &state->gc, state);
361 dev_err(&i2c->dev, "Failed to create gpiochip: %d\n", ret);
368 mutex_destroy(&state->lock);
373 static int attiny_i2c_remove(struct i2c_client *client)
375 struct attiny_lcd *state = i2c_get_clientdata(client);
377 mutex_destroy(&state->lock);
382 static const struct of_device_id attiny_dt_ids[] = {
383 { .compatible = "raspberrypi,7inch-touchscreen-panel-regulator" },
386 MODULE_DEVICE_TABLE(of, attiny_dt_ids);
388 static struct i2c_driver attiny_regulator_driver = {
390 .name = "rpi_touchscreen_attiny",
391 .of_match_table = of_match_ptr(attiny_dt_ids),
393 .probe = attiny_i2c_probe,
394 .remove = attiny_i2c_remove,
397 module_i2c_driver(attiny_regulator_driver);
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");