rpi-panel-attiny-regulator.c 9.3 KB

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