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