rpi-panel-attiny-regulator.c 9.3 KB

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