panel-raspberrypi-touchscreen.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * Copyright © 2016-2017 Broadcom
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * Portions of this file (derived from panel-simple.c) are:
  9. *
  10. * Copyright (C) 2013, NVIDIA Corporation. All rights reserved.
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a
  13. * copy of this software and associated documentation files (the "Software"),
  14. * to deal in the Software without restriction, including without limitation
  15. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  16. * and/or sell copies of the Software, and to permit persons to whom the
  17. * Software is furnished to do so, subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice (including the
  20. * next paragraph) shall be included in all copies or substantial portions
  21. * of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  26. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  28. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  29. * DEALINGS IN THE SOFTWARE.
  30. */
  31. /**
  32. * Raspberry Pi 7" touchscreen panel driver.
  33. *
  34. * The 7" touchscreen consists of a DPI LCD panel, a Toshiba
  35. * TC358762XBG DSI-DPI bridge, and an I2C-connected Atmel ATTINY88-MUR
  36. * controlling power management, the LCD PWM, and initial register
  37. * setup of the Tohsiba.
  38. *
  39. * This driver controls the TC358762 and ATTINY88, presenting a DSI
  40. * device with a drm_panel.
  41. */
  42. #include <linux/delay.h>
  43. #include <linux/err.h>
  44. #include <linux/fb.h>
  45. #include <linux/gpio.h>
  46. #include <linux/gpio/consumer.h>
  47. #include <linux/i2c.h>
  48. #include <linux/module.h>
  49. #include <linux/of.h>
  50. #include <linux/of_device.h>
  51. #include <linux/of_graph.h>
  52. #include <linux/pm.h>
  53. #include <drm/drm_panel.h>
  54. #include <drm/drmP.h>
  55. #include <drm/drm_crtc.h>
  56. #include <drm/drm_mipi_dsi.h>
  57. #include <drm/drm_panel.h>
  58. #define RPI_DSI_DRIVER_NAME "rpi-ts-dsi"
  59. /* I2C registers of the Atmel microcontroller. */
  60. enum REG_ADDR {
  61. REG_ID = 0x80,
  62. REG_PORTA, /* BIT(2) for horizontal flip, BIT(3) for vertical flip */
  63. REG_PORTB,
  64. REG_PORTC,
  65. REG_PORTD,
  66. REG_POWERON,
  67. REG_PWM,
  68. REG_DDRA,
  69. REG_DDRB,
  70. REG_DDRC,
  71. REG_DDRD,
  72. REG_TEST,
  73. REG_WR_ADDRL,
  74. REG_WR_ADDRH,
  75. REG_READH,
  76. REG_READL,
  77. REG_WRITEH,
  78. REG_WRITEL,
  79. REG_ID2,
  80. };
  81. /* DSI D-PHY Layer Registers */
  82. #define D0W_DPHYCONTTX 0x0004
  83. #define CLW_DPHYCONTRX 0x0020
  84. #define D0W_DPHYCONTRX 0x0024
  85. #define D1W_DPHYCONTRX 0x0028
  86. #define COM_DPHYCONTRX 0x0038
  87. #define CLW_CNTRL 0x0040
  88. #define D0W_CNTRL 0x0044
  89. #define D1W_CNTRL 0x0048
  90. #define DFTMODE_CNTRL 0x0054
  91. /* DSI PPI Layer Registers */
  92. #define PPI_STARTPPI 0x0104
  93. #define PPI_BUSYPPI 0x0108
  94. #define PPI_LINEINITCNT 0x0110
  95. #define PPI_LPTXTIMECNT 0x0114
  96. #define PPI_CLS_ATMR 0x0140
  97. #define PPI_D0S_ATMR 0x0144
  98. #define PPI_D1S_ATMR 0x0148
  99. #define PPI_D0S_CLRSIPOCOUNT 0x0164
  100. #define PPI_D1S_CLRSIPOCOUNT 0x0168
  101. #define CLS_PRE 0x0180
  102. #define D0S_PRE 0x0184
  103. #define D1S_PRE 0x0188
  104. #define CLS_PREP 0x01A0
  105. #define D0S_PREP 0x01A4
  106. #define D1S_PREP 0x01A8
  107. #define CLS_ZERO 0x01C0
  108. #define D0S_ZERO 0x01C4
  109. #define D1S_ZERO 0x01C8
  110. #define PPI_CLRFLG 0x01E0
  111. #define PPI_CLRSIPO 0x01E4
  112. #define HSTIMEOUT 0x01F0
  113. #define HSTIMEOUTENABLE 0x01F4
  114. /* DSI Protocol Layer Registers */
  115. #define DSI_STARTDSI 0x0204
  116. #define DSI_BUSYDSI 0x0208
  117. #define DSI_LANEENABLE 0x0210
  118. # define DSI_LANEENABLE_CLOCK BIT(0)
  119. # define DSI_LANEENABLE_D0 BIT(1)
  120. # define DSI_LANEENABLE_D1 BIT(2)
  121. #define DSI_LANESTATUS0 0x0214
  122. #define DSI_LANESTATUS1 0x0218
  123. #define DSI_INTSTATUS 0x0220
  124. #define DSI_INTMASK 0x0224
  125. #define DSI_INTCLR 0x0228
  126. #define DSI_LPTXTO 0x0230
  127. #define DSI_MODE 0x0260
  128. #define DSI_PAYLOAD0 0x0268
  129. #define DSI_PAYLOAD1 0x026C
  130. #define DSI_SHORTPKTDAT 0x0270
  131. #define DSI_SHORTPKTREQ 0x0274
  132. #define DSI_BTASTA 0x0278
  133. #define DSI_BTACLR 0x027C
  134. /* DSI General Registers */
  135. #define DSIERRCNT 0x0300
  136. #define DSISIGMOD 0x0304
  137. /* DSI Application Layer Registers */
  138. #define APLCTRL 0x0400
  139. #define APLSTAT 0x0404
  140. #define APLERR 0x0408
  141. #define PWRMOD 0x040C
  142. #define RDPKTLN 0x0410
  143. #define PXLFMT 0x0414
  144. #define MEMWRCMD 0x0418
  145. /* LCDC/DPI Host Registers */
  146. #define LCDCTRL 0x0420
  147. #define HSR 0x0424
  148. #define HDISPR 0x0428
  149. #define VSR 0x042C
  150. #define VDISPR 0x0430
  151. #define VFUEN 0x0434
  152. /* DBI-B Host Registers */
  153. #define DBIBCTRL 0x0440
  154. /* SPI Master Registers */
  155. #define SPICMR 0x0450
  156. #define SPITCR 0x0454
  157. /* System Controller Registers */
  158. #define SYSSTAT 0x0460
  159. #define SYSCTRL 0x0464
  160. #define SYSPLL1 0x0468
  161. #define SYSPLL2 0x046C
  162. #define SYSPLL3 0x0470
  163. #define SYSPMCTRL 0x047C
  164. /* GPIO Registers */
  165. #define GPIOC 0x0480
  166. #define GPIOO 0x0484
  167. #define GPIOI 0x0488
  168. /* I2C Registers */
  169. #define I2CCLKCTRL 0x0490
  170. /* Chip/Rev Registers */
  171. #define IDREG 0x04A0
  172. /* Debug Registers */
  173. #define WCMDQUEUE 0x0500
  174. #define RCMDQUEUE 0x0504
  175. struct rpi_touchscreen {
  176. struct drm_panel base;
  177. struct mipi_dsi_device *dsi;
  178. struct i2c_client *i2c;
  179. };
  180. static const struct drm_display_mode rpi_touchscreen_modes[] = {
  181. {
  182. /* Modeline comes from the Raspberry Pi firmware, with HFP=1
  183. * plugged in and clock re-computed from that.
  184. */
  185. .clock = 25979400 / 1000,
  186. .hdisplay = 800,
  187. .hsync_start = 800 + 1,
  188. .hsync_end = 800 + 1 + 2,
  189. .htotal = 800 + 1 + 2 + 46,
  190. .vdisplay = 480,
  191. .vsync_start = 480 + 7,
  192. .vsync_end = 480 + 7 + 2,
  193. .vtotal = 480 + 7 + 2 + 21,
  194. .vrefresh = 60,
  195. },
  196. };
  197. static struct rpi_touchscreen *panel_to_ts(struct drm_panel *panel)
  198. {
  199. return container_of(panel, struct rpi_touchscreen, base);
  200. }
  201. static int rpi_touchscreen_i2c_read(struct rpi_touchscreen *ts, u8 reg)
  202. {
  203. return i2c_smbus_read_byte_data(ts->i2c, reg);
  204. }
  205. static void rpi_touchscreen_i2c_write(struct rpi_touchscreen *ts,
  206. u8 reg, u8 val)
  207. {
  208. int ret;
  209. ret = i2c_smbus_write_byte_data(ts->i2c, reg, val);
  210. if (ret)
  211. dev_err(&ts->dsi->dev, "I2C write failed: %d\n", ret);
  212. }
  213. static int rpi_touchscreen_write(struct rpi_touchscreen *ts, u16 reg, u32 val)
  214. {
  215. #if 0
  216. /* The firmware uses LP DSI transactions like this to bring up
  217. * the hardware, which should be faster than using I2C to then
  218. * pass to the Toshiba. However, I was unable to get it to
  219. * work.
  220. */
  221. u8 msg[] = {
  222. reg,
  223. reg >> 8,
  224. val,
  225. val >> 8,
  226. val >> 16,
  227. val >> 24,
  228. };
  229. mipi_dsi_dcs_write_buffer(ts->dsi, msg, sizeof(msg));
  230. #else
  231. rpi_touchscreen_i2c_write(ts, REG_WR_ADDRH, reg >> 8);
  232. rpi_touchscreen_i2c_write(ts, REG_WR_ADDRL, reg);
  233. rpi_touchscreen_i2c_write(ts, REG_WRITEH, val >> 8);
  234. rpi_touchscreen_i2c_write(ts, REG_WRITEL, val);
  235. #endif
  236. return 0;
  237. }
  238. static int rpi_touchscreen_disable(struct drm_panel *panel)
  239. {
  240. struct rpi_touchscreen *ts = panel_to_ts(panel);
  241. rpi_touchscreen_i2c_write(ts, REG_PWM, 0);
  242. rpi_touchscreen_i2c_write(ts, REG_POWERON, 0);
  243. udelay(1);
  244. return 0;
  245. }
  246. static int rpi_touchscreen_noop(struct drm_panel *panel)
  247. {
  248. return 0;
  249. }
  250. static int rpi_touchscreen_enable(struct drm_panel *panel)
  251. {
  252. struct rpi_touchscreen *ts = panel_to_ts(panel);
  253. int i;
  254. rpi_touchscreen_i2c_write(ts, REG_POWERON, 1);
  255. /* Wait for nPWRDWN to go low to indicate poweron is done. */
  256. for (i = 0; i < 100; i++) {
  257. if (rpi_touchscreen_i2c_read(ts, REG_PORTB) & 1)
  258. break;
  259. }
  260. rpi_touchscreen_write(ts, DSI_LANEENABLE,
  261. DSI_LANEENABLE_CLOCK |
  262. DSI_LANEENABLE_D0);
  263. rpi_touchscreen_write(ts, PPI_D0S_CLRSIPOCOUNT, 0x05);
  264. rpi_touchscreen_write(ts, PPI_D1S_CLRSIPOCOUNT, 0x05);
  265. rpi_touchscreen_write(ts, PPI_D0S_ATMR, 0x00);
  266. rpi_touchscreen_write(ts, PPI_D1S_ATMR, 0x00);
  267. rpi_touchscreen_write(ts, PPI_LPTXTIMECNT, 0x03);
  268. rpi_touchscreen_write(ts, SPICMR, 0x00);
  269. rpi_touchscreen_write(ts, LCDCTRL, 0x00100150);
  270. rpi_touchscreen_write(ts, SYSCTRL, 0x040f);
  271. msleep(100);
  272. rpi_touchscreen_write(ts, PPI_STARTPPI, 0x01);
  273. rpi_touchscreen_write(ts, DSI_STARTDSI, 0x01);
  274. msleep(100);
  275. /* Turn on the backlight. */
  276. rpi_touchscreen_i2c_write(ts, REG_PWM, 255);
  277. /* Default to the same orientation as the closed source
  278. * firmware used for the panel. Runtime rotation
  279. * configuration will be supported using VC4's plane
  280. * orientation bits.
  281. */
  282. rpi_touchscreen_i2c_write(ts, REG_PORTA, BIT(2));
  283. return 0;
  284. }
  285. static int rpi_touchscreen_get_modes(struct drm_panel *panel)
  286. {
  287. struct drm_connector *connector = panel->connector;
  288. struct drm_device *drm = panel->drm;
  289. unsigned int i, num = 0;
  290. static const u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
  291. for (i = 0; i < ARRAY_SIZE(rpi_touchscreen_modes); i++) {
  292. const struct drm_display_mode *m = &rpi_touchscreen_modes[i];
  293. struct drm_display_mode *mode;
  294. mode = drm_mode_duplicate(drm, m);
  295. if (!mode) {
  296. dev_err(drm->dev, "failed to add mode %ux%u@%u\n",
  297. m->hdisplay, m->vdisplay, m->vrefresh);
  298. continue;
  299. }
  300. mode->type |= DRM_MODE_TYPE_DRIVER;
  301. if (i == 0)
  302. mode->type |= DRM_MODE_TYPE_PREFERRED;
  303. drm_mode_set_name(mode);
  304. drm_mode_probed_add(connector, mode);
  305. num++;
  306. }
  307. connector->display_info.bpc = 8;
  308. connector->display_info.width_mm = 154;
  309. connector->display_info.height_mm = 86;
  310. drm_display_info_set_bus_formats(&connector->display_info,
  311. &bus_format, 1);
  312. return num;
  313. }
  314. static const struct drm_panel_funcs rpi_touchscreen_funcs = {
  315. .disable = rpi_touchscreen_disable,
  316. .unprepare = rpi_touchscreen_noop,
  317. .prepare = rpi_touchscreen_noop,
  318. .enable = rpi_touchscreen_enable,
  319. .get_modes = rpi_touchscreen_get_modes,
  320. };
  321. static int rpi_touchscreen_probe(struct i2c_client *i2c,
  322. const struct i2c_device_id *id)
  323. {
  324. struct device *dev = &i2c->dev;
  325. struct rpi_touchscreen *ts;
  326. struct device_node *endpoint, *dsi_host_node;
  327. struct mipi_dsi_host *host;
  328. int ret, ver;
  329. struct mipi_dsi_device_info info = {
  330. .type = RPI_DSI_DRIVER_NAME,
  331. .channel = 0,
  332. .node = NULL,
  333. };
  334. ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
  335. if (!ts)
  336. return -ENOMEM;
  337. i2c_set_clientdata(i2c, ts);
  338. ts->i2c = i2c;
  339. ver = rpi_touchscreen_i2c_read(ts, REG_ID);
  340. if (ver < 0) {
  341. dev_err(dev, "Atmel I2C read failed: %d\n", ver);
  342. return -ENODEV;
  343. }
  344. switch (ver) {
  345. case 0xde: /* ver 1 */
  346. case 0xc3: /* ver 2 */
  347. break;
  348. default:
  349. dev_err(dev, "Unknown Atmel firmware revision: 0x%02x\n", ver);
  350. return -ENODEV;
  351. }
  352. /* Turn off at boot, so we can cleanly sequence powering on. */
  353. rpi_touchscreen_i2c_write(ts, REG_POWERON, 0);
  354. /* Look up the DSI host. It needs to probe before we do. */
  355. endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
  356. dsi_host_node = of_graph_get_remote_port_parent(endpoint);
  357. host = of_find_mipi_dsi_host_by_node(dsi_host_node);
  358. of_node_put(dsi_host_node);
  359. if (!host) {
  360. of_node_put(endpoint);
  361. return -EPROBE_DEFER;
  362. }
  363. info.node = of_graph_get_remote_port(endpoint);
  364. of_node_put(endpoint);
  365. ts->dsi = mipi_dsi_device_register_full(host, &info);
  366. if (IS_ERR(ts->dsi)) {
  367. dev_err(dev, "DSI device registration failed: %ld\n",
  368. PTR_ERR(ts->dsi));
  369. return PTR_ERR(ts->dsi);
  370. }
  371. ts->base.dev = dev;
  372. ts->base.funcs = &rpi_touchscreen_funcs;
  373. /* This appears last, as it's what will unblock the DSI host
  374. * driver's component bind function.
  375. */
  376. ret = drm_panel_add(&ts->base);
  377. if (ret)
  378. return ret;
  379. return 0;
  380. }
  381. static int rpi_touchscreen_remove(struct i2c_client *i2c)
  382. {
  383. struct rpi_touchscreen *ts = i2c_get_clientdata(i2c);
  384. mipi_dsi_detach(ts->dsi);
  385. drm_panel_remove(&ts->base);
  386. mipi_dsi_device_unregister(ts->dsi);
  387. kfree(ts->dsi);
  388. return 0;
  389. }
  390. static int rpi_touchscreen_dsi_probe(struct mipi_dsi_device *dsi)
  391. {
  392. int ret;
  393. dsi->mode_flags = (MIPI_DSI_MODE_VIDEO |
  394. MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
  395. MIPI_DSI_MODE_LPM);
  396. dsi->format = MIPI_DSI_FMT_RGB888;
  397. dsi->lanes = 1;
  398. ret = mipi_dsi_attach(dsi);
  399. if (ret)
  400. dev_err(&dsi->dev, "failed to attach dsi to host: %d\n", ret);
  401. return ret;
  402. }
  403. static struct mipi_dsi_driver rpi_touchscreen_dsi_driver = {
  404. .driver.name = RPI_DSI_DRIVER_NAME,
  405. .probe = rpi_touchscreen_dsi_probe,
  406. };
  407. static const struct of_device_id rpi_touchscreen_of_ids[] = {
  408. { .compatible = "raspberrypi,7inch-touchscreen-panel" },
  409. { } /* sentinel */
  410. };
  411. MODULE_DEVICE_TABLE(of, rpi_touchscreen_of_ids);
  412. static struct i2c_driver rpi_touchscreen_driver = {
  413. .driver = {
  414. .name = "rpi_touchscreen",
  415. .of_match_table = rpi_touchscreen_of_ids,
  416. },
  417. .probe = rpi_touchscreen_probe,
  418. .remove = rpi_touchscreen_remove,
  419. };
  420. static int __init rpi_touchscreen_init(void)
  421. {
  422. mipi_dsi_driver_register(&rpi_touchscreen_dsi_driver);
  423. return i2c_add_driver(&rpi_touchscreen_driver);
  424. }
  425. module_init(rpi_touchscreen_init);
  426. static void __exit rpi_touchscreen_exit(void)
  427. {
  428. i2c_del_driver(&rpi_touchscreen_driver);
  429. mipi_dsi_driver_unregister(&rpi_touchscreen_dsi_driver);
  430. }
  431. module_exit(rpi_touchscreen_exit);
  432. MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
  433. MODULE_DESCRIPTION("Raspberry Pi 7-inch touchscreen driver");
  434. MODULE_LICENSE("GPL v2");