1 // SPDX-License-Identifier: GPL-2.0
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_RBTREE,
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);
150 mutex_lock(&state->lock);
152 for (i = 0; i < 10; i++) {
153 ret = regmap_read(rdev->regmap, REG_PORTC, &data);
156 usleep_range(10000, 12000);
159 mutex_unlock(&state->lock);
164 return data & PC_RST_BRIDGE_N;
167 static const struct regulator_init_data attiny_regulator_default = {
169 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
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,
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,
186 static int attiny_update_status(struct backlight_device *bl)
188 struct attiny_lcd *state = bl_get_data(bl);
189 struct regmap *regmap = state->regmap;
190 int brightness = backlight_get_brightness(bl);
193 mutex_lock(&state->lock);
195 for (i = 0; i < 10; i++) {
196 ret = regmap_write(regmap, REG_PWM, brightness);
201 mutex_unlock(&state->lock);
206 static const struct backlight_ops attiny_bl = {
207 .update_status = attiny_update_status,
210 static int attiny_gpio_get_direction(struct gpio_chip *gc, unsigned int off)
212 return GPIO_LINE_DIRECTION_OUT;
215 static void attiny_gpio_set(struct gpio_chip *gc, unsigned int off, int val)
217 struct attiny_lcd *state = gpiochip_get_data(gc);
223 mutex_lock(&state->lock);
225 last_val = attiny_get_port_state(state, mappings[off].reg);
227 last_val |= mappings[off].mask;
229 last_val &= ~mappings[off].mask;
231 attiny_set_port_state(state, mappings[off].reg, last_val);
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);
246 mutex_unlock(&state->lock);
249 static int attiny_i2c_read(struct i2c_client *client, u8 reg, unsigned int *buf)
251 struct i2c_msg msgs[1];
252 u8 addr_buf[1] = { reg };
253 u8 data_buf[1] = { 0, };
256 /* Write register address */
257 msgs[0].addr = client->addr;
259 msgs[0].len = ARRAY_SIZE(addr_buf);
260 msgs[0].buf = addr_buf;
262 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
263 if (ret != ARRAY_SIZE(msgs))
266 usleep_range(5000, 10000);
268 /* Read data from register */
269 msgs[0].addr = client->addr;
270 msgs[0].flags = I2C_M_RD;
272 msgs[0].buf = data_buf;
274 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
275 if (ret != ARRAY_SIZE(msgs))
283 * I2C driver interface functions
285 static int attiny_i2c_probe(struct i2c_client *i2c,
286 const struct i2c_device_id *id)
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;
297 state = devm_kzalloc(&i2c->dev, sizeof(*state), GFP_KERNEL);
301 mutex_init(&state->lock);
302 i2c_set_clientdata(i2c, state);
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",
312 ret = attiny_i2c_read(i2c, REG_ID, &data);
314 dev_err(&i2c->dev, "Failed to read REG_ID reg: %d\n", ret);
319 case 0xde: /* ver 1 */
320 case 0xc3: /* ver 2 */
323 dev_err(&i2c->dev, "Unknown Atmel firmware revision: 0x%02x\n", data);
328 regmap_write(regmap, REG_POWERON, 0);
330 regmap_write(regmap, REG_PWM, 0);
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;
338 rdev = devm_regulator_register(&i2c->dev, &attiny_regulator, &config);
340 dev_err(&i2c->dev, "Failed to register ATTINY regulator\n");
345 props.type = BACKLIGHT_RAW;
346 props.max_brightness = 0xff;
348 state->regmap = regmap;
350 bl = devm_backlight_device_register(&i2c->dev, dev_name(&i2c->dev),
351 &i2c->dev, state, &attiny_bl,
358 bl->props.brightness = 0xff;
360 state->gc.parent = &i2c->dev;
361 state->gc.label = i2c->name;
362 state->gc.owner = THIS_MODULE;
364 state->gc.ngpio = NUM_GPIO;
366 state->gc.set = attiny_gpio_set;
367 state->gc.get_direction = attiny_gpio_get_direction;
368 state->gc.can_sleep = true;
370 ret = devm_gpiochip_add_data(&i2c->dev, &state->gc, state);
372 dev_err(&i2c->dev, "Failed to create gpiochip: %d\n", ret);
379 mutex_destroy(&state->lock);
384 static int attiny_i2c_remove(struct i2c_client *client)
386 struct attiny_lcd *state = i2c_get_clientdata(client);
388 mutex_destroy(&state->lock);
393 static const struct of_device_id attiny_dt_ids[] = {
394 { .compatible = "raspberrypi,7inch-touchscreen-panel-regulator" },
397 MODULE_DEVICE_TABLE(of, attiny_dt_ids);
399 static struct i2c_driver attiny_regulator_driver = {
401 .name = "rpi_touchscreen_attiny",
402 .of_match_table = of_match_ptr(attiny_dt_ids),
404 .probe = attiny_i2c_probe,
405 .remove = attiny_i2c_remove,
408 module_i2c_driver(attiny_regulator_driver);
411 MODULE_DESCRIPTION("Regulator device driver for Raspberry Pi 7-inch touchscreen");
412 MODULE_LICENSE("GPL v2");