rpi-panel-attiny-regulator.c 9.0 KB

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