panel-raspberrypi-touchscreen.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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/i2c.h>
  46. #include <linux/module.h>
  47. #include <linux/of.h>
  48. #include <linux/of_device.h>
  49. #include <linux/of_graph.h>
  50. #include <linux/pm.h>
  51. #include <drm/drm_crtc.h>
  52. #include <drm/drm_device.h>
  53. #include <drm/drm_mipi_dsi.h>
  54. #include <drm/drm_panel.h>
  55. #define RPI_DSI_DRIVER_NAME "rpi-ts-dsi"
  56. /* I2C registers of the Atmel microcontroller. */
  57. enum REG_ADDR {
  58. REG_ID = 0x80,
  59. REG_PORTA, /* BIT(2) for horizontal flip, BIT(3) for vertical flip */
  60. REG_PORTB,
  61. REG_PORTC,
  62. REG_PORTD,
  63. REG_POWERON,
  64. REG_PWM,
  65. REG_DDRA,
  66. REG_DDRB,
  67. REG_DDRC,
  68. REG_DDRD,
  69. REG_TEST,
  70. REG_WR_ADDRL,
  71. REG_WR_ADDRH,
  72. REG_READH,
  73. REG_READL,
  74. REG_WRITEH,
  75. REG_WRITEL,
  76. REG_ID2,
  77. };
  78. /* DSI D-PHY Layer Registers */
  79. #define D0W_DPHYCONTTX 0x0004
  80. #define CLW_DPHYCONTRX 0x0020
  81. #define D0W_DPHYCONTRX 0x0024
  82. #define D1W_DPHYCONTRX 0x0028
  83. #define COM_DPHYCONTRX 0x0038
  84. #define CLW_CNTRL 0x0040
  85. #define D0W_CNTRL 0x0044
  86. #define D1W_CNTRL 0x0048
  87. #define DFTMODE_CNTRL 0x0054
  88. /* DSI PPI Layer Registers */
  89. #define PPI_STARTPPI 0x0104
  90. #define PPI_BUSYPPI 0x0108
  91. #define PPI_LINEINITCNT 0x0110
  92. #define PPI_LPTXTIMECNT 0x0114
  93. #define PPI_CLS_ATMR 0x0140
  94. #define PPI_D0S_ATMR 0x0144
  95. #define PPI_D1S_ATMR 0x0148
  96. #define PPI_D0S_CLRSIPOCOUNT 0x0164
  97. #define PPI_D1S_CLRSIPOCOUNT 0x0168
  98. #define CLS_PRE 0x0180
  99. #define D0S_PRE 0x0184
  100. #define D1S_PRE 0x0188
  101. #define CLS_PREP 0x01A0
  102. #define D0S_PREP 0x01A4
  103. #define D1S_PREP 0x01A8
  104. #define CLS_ZERO 0x01C0
  105. #define D0S_ZERO 0x01C4
  106. #define D1S_ZERO 0x01C8
  107. #define PPI_CLRFLG 0x01E0
  108. #define PPI_CLRSIPO 0x01E4
  109. #define HSTIMEOUT 0x01F0
  110. #define HSTIMEOUTENABLE 0x01F4
  111. /* DSI Protocol Layer Registers */
  112. #define DSI_STARTDSI 0x0204
  113. #define DSI_BUSYDSI 0x0208
  114. #define DSI_LANEENABLE 0x0210
  115. # define DSI_LANEENABLE_CLOCK BIT(0)
  116. # define DSI_LANEENABLE_D0 BIT(1)
  117. # define DSI_LANEENABLE_D1 BIT(2)
  118. #define DSI_LANESTATUS0 0x0214
  119. #define DSI_LANESTATUS1 0x0218
  120. #define DSI_INTSTATUS 0x0220
  121. #define DSI_INTMASK 0x0224
  122. #define DSI_INTCLR 0x0228
  123. #define DSI_LPTXTO 0x0230
  124. #define DSI_MODE 0x0260
  125. #define DSI_PAYLOAD0 0x0268
  126. #define DSI_PAYLOAD1 0x026C
  127. #define DSI_SHORTPKTDAT 0x0270
  128. #define DSI_SHORTPKTREQ 0x0274
  129. #define DSI_BTASTA 0x0278
  130. #define DSI_BTACLR 0x027C
  131. /* DSI General Registers */
  132. #define DSIERRCNT 0x0300
  133. #define DSISIGMOD 0x0304
  134. /* DSI Application Layer Registers */
  135. #define APLCTRL 0x0400
  136. #define APLSTAT 0x0404
  137. #define APLERR 0x0408
  138. #define PWRMOD 0x040C
  139. #define RDPKTLN 0x0410
  140. #define PXLFMT 0x0414
  141. #define MEMWRCMD 0x0418
  142. /* LCDC/DPI Host Registers */
  143. #define LCDCTRL 0x0420
  144. #define HSR 0x0424
  145. #define HDISPR 0x0428
  146. #define VSR 0x042C
  147. #define VDISPR 0x0430
  148. #define VFUEN 0x0434
  149. /* DBI-B Host Registers */
  150. #define DBIBCTRL 0x0440
  151. /* SPI Master Registers */
  152. #define SPICMR 0x0450
  153. #define SPITCR 0x0454
  154. /* System Controller Registers */
  155. #define SYSSTAT 0x0460
  156. #define SYSCTRL 0x0464
  157. #define SYSPLL1 0x0468
  158. #define SYSPLL2 0x046C
  159. #define SYSPLL3 0x0470
  160. #define SYSPMCTRL 0x047C
  161. /* GPIO Registers */
  162. #define GPIOC 0x0480
  163. #define GPIOO 0x0484
  164. #define GPIOI 0x0488
  165. /* I2C Registers */
  166. #define I2CCLKCTRL 0x0490
  167. /* Chip/Rev Registers */
  168. #define IDREG 0x04A0
  169. /* Debug Registers */
  170. #define WCMDQUEUE 0x0500
  171. #define RCMDQUEUE 0x0504
  172. struct rpi_touchscreen {
  173. struct drm_panel base;
  174. struct mipi_dsi_device *dsi;
  175. struct i2c_client *i2c;
  176. };
  177. static const struct drm_display_mode rpi_touchscreen_modes[] = {
  178. {
  179. /* Modeline comes from the Raspberry Pi firmware, with HFP=1
  180. * plugged in and clock re-computed from that.
  181. */
  182. .clock = 25979400 / 1000,
  183. .hdisplay = 800,
  184. .hsync_start = 800 + 1,
  185. .hsync_end = 800 + 1 + 2,
  186. .htotal = 800 + 1 + 2 + 46,
  187. .vdisplay = 480,
  188. .vsync_start = 480 + 7,
  189. .vsync_end = 480 + 7 + 2,
  190. .vtotal = 480 + 7 + 2 + 21,
  191. },
  192. };
  193. static struct rpi_touchscreen *panel_to_ts(struct drm_panel *panel)
  194. {
  195. return container_of(panel, struct rpi_touchscreen, base);
  196. }
  197. static int rpi_touchscreen_i2c_read(struct rpi_touchscreen *ts, u8 reg)
  198. {
  199. return i2c_smbus_read_byte_data(ts->i2c, reg);
  200. }
  201. static void rpi_touchscreen_i2c_write(struct rpi_touchscreen *ts,
  202. u8 reg, u8 val)
  203. {
  204. int ret;
  205. ret = i2c_smbus_write_byte_data(ts->i2c, reg, val);
  206. if (ret)
  207. dev_err(&ts->i2c->dev, "I2C write failed: %d\n", ret);
  208. }
  209. static int rpi_touchscreen_write(struct rpi_touchscreen *ts, u16 reg, u32 val)
  210. {
  211. u8 msg[] = {
  212. reg,
  213. reg >> 8,
  214. val,
  215. val >> 8,
  216. val >> 16,
  217. val >> 24,
  218. };
  219. mipi_dsi_generic_write(ts->dsi, msg, sizeof(msg));
  220. return 0;
  221. }
  222. static int rpi_touchscreen_disable(struct drm_panel *panel)
  223. {
  224. struct rpi_touchscreen *ts = panel_to_ts(panel);
  225. rpi_touchscreen_i2c_write(ts, REG_PWM, 0);
  226. rpi_touchscreen_i2c_write(ts, REG_POWERON, 0);
  227. udelay(1);
  228. return 0;
  229. }
  230. static int rpi_touchscreen_noop(struct drm_panel *panel)
  231. {
  232. return 0;
  233. }
  234. static int rpi_touchscreen_prepare(struct drm_panel *panel)
  235. {
  236. struct rpi_touchscreen *ts = panel_to_ts(panel);
  237. int i;
  238. rpi_touchscreen_i2c_write(ts, REG_POWERON, 1);
  239. /* Wait for nPWRDWN to go low to indicate poweron is done. */
  240. for (i = 0; i < 100; i++) {
  241. if (rpi_touchscreen_i2c_read(ts, REG_PORTB) & 1)
  242. break;
  243. }
  244. rpi_touchscreen_write(ts, DSI_LANEENABLE,
  245. DSI_LANEENABLE_CLOCK |
  246. DSI_LANEENABLE_D0);
  247. rpi_touchscreen_write(ts, PPI_D0S_CLRSIPOCOUNT, 0x05);
  248. rpi_touchscreen_write(ts, PPI_D1S_CLRSIPOCOUNT, 0x05);
  249. rpi_touchscreen_write(ts, PPI_D0S_ATMR, 0x00);
  250. rpi_touchscreen_write(ts, PPI_D1S_ATMR, 0x00);
  251. rpi_touchscreen_write(ts, PPI_LPTXTIMECNT, 0x03);
  252. rpi_touchscreen_write(ts, SPICMR, 0x00);
  253. rpi_touchscreen_write(ts, LCDCTRL, 0x00100150);
  254. rpi_touchscreen_write(ts, SYSCTRL, 0x040f);
  255. msleep(100);
  256. rpi_touchscreen_write(ts, PPI_STARTPPI, 0x01);
  257. rpi_touchscreen_write(ts, DSI_STARTDSI, 0x01);
  258. msleep(100);
  259. return 0;
  260. }
  261. static int rpi_touchscreen_enable(struct drm_panel *panel)
  262. {
  263. struct rpi_touchscreen *ts = panel_to_ts(panel);
  264. /* Turn on the backlight. */
  265. rpi_touchscreen_i2c_write(ts, REG_PWM, 255);
  266. /* Default to the same orientation as the closed source
  267. * firmware used for the panel. Runtime rotation
  268. * configuration will be supported using VC4's plane
  269. * orientation bits.
  270. */
  271. rpi_touchscreen_i2c_write(ts, REG_PORTA, BIT(2));
  272. return 0;
  273. }
  274. static int rpi_touchscreen_get_modes(struct drm_panel *panel,
  275. struct drm_connector *connector)
  276. {
  277. unsigned int i, num = 0;
  278. static const u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
  279. for (i = 0; i < ARRAY_SIZE(rpi_touchscreen_modes); i++) {
  280. const struct drm_display_mode *m = &rpi_touchscreen_modes[i];
  281. struct drm_display_mode *mode;
  282. mode = drm_mode_duplicate(connector->dev, m);
  283. if (!mode) {
  284. dev_err(panel->dev, "failed to add mode %ux%u@%u\n",
  285. m->hdisplay, m->vdisplay,
  286. drm_mode_vrefresh(m));
  287. continue;
  288. }
  289. mode->type |= DRM_MODE_TYPE_DRIVER;
  290. if (i == 0)
  291. mode->type |= DRM_MODE_TYPE_PREFERRED;
  292. drm_mode_set_name(mode);
  293. drm_mode_probed_add(connector, mode);
  294. num++;
  295. }
  296. connector->display_info.bpc = 8;
  297. connector->display_info.width_mm = 154;
  298. connector->display_info.height_mm = 86;
  299. drm_display_info_set_bus_formats(&connector->display_info,
  300. &bus_format, 1);
  301. return num;
  302. }
  303. static const struct drm_panel_funcs rpi_touchscreen_funcs = {
  304. .disable = rpi_touchscreen_disable,
  305. .unprepare = rpi_touchscreen_noop,
  306. .prepare = rpi_touchscreen_prepare,
  307. .enable = rpi_touchscreen_enable,
  308. .get_modes = rpi_touchscreen_get_modes,
  309. };
  310. static int rpi_touchscreen_probe(struct i2c_client *i2c,
  311. const struct i2c_device_id *id)
  312. {
  313. struct device *dev = &i2c->dev;
  314. struct rpi_touchscreen *ts;
  315. struct device_node *endpoint, *dsi_host_node;
  316. struct mipi_dsi_host *host;
  317. int ver;
  318. struct mipi_dsi_device_info info = {
  319. .type = RPI_DSI_DRIVER_NAME,
  320. .channel = 0,
  321. .node = NULL,
  322. };
  323. ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
  324. if (!ts)
  325. return -ENOMEM;
  326. i2c_set_clientdata(i2c, ts);
  327. ts->i2c = i2c;
  328. ver = rpi_touchscreen_i2c_read(ts, REG_ID);
  329. if (ver < 0) {
  330. dev_err(dev, "Atmel I2C read failed: %d\n", ver);
  331. return -ENODEV;
  332. }
  333. switch (ver) {
  334. case 0xde: /* ver 1 */
  335. case 0xc3: /* ver 2 */
  336. break;
  337. default:
  338. dev_err(dev, "Unknown Atmel firmware revision: 0x%02x\n", ver);
  339. return -ENODEV;
  340. }
  341. /* Turn off at boot, so we can cleanly sequence powering on. */
  342. rpi_touchscreen_i2c_write(ts, REG_POWERON, 0);
  343. /* Look up the DSI host. It needs to probe before we do. */
  344. endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
  345. if (!endpoint)
  346. return -ENODEV;
  347. dsi_host_node = of_graph_get_remote_port_parent(endpoint);
  348. if (!dsi_host_node)
  349. goto error;
  350. host = of_find_mipi_dsi_host_by_node(dsi_host_node);
  351. of_node_put(dsi_host_node);
  352. if (!host) {
  353. of_node_put(endpoint);
  354. return -EPROBE_DEFER;
  355. }
  356. info.node = of_graph_get_remote_port(endpoint);
  357. if (!info.node)
  358. goto error;
  359. of_node_put(endpoint);
  360. ts->dsi = mipi_dsi_device_register_full(host, &info);
  361. if (IS_ERR(ts->dsi)) {
  362. dev_err(dev, "DSI device registration failed: %ld\n",
  363. PTR_ERR(ts->dsi));
  364. return PTR_ERR(ts->dsi);
  365. }
  366. drm_panel_init(&ts->base, dev, &rpi_touchscreen_funcs,
  367. DRM_MODE_CONNECTOR_DSI);
  368. /* This appears last, as it's what will unblock the DSI host
  369. * driver's component bind function.
  370. */
  371. drm_panel_add(&ts->base);
  372. return 0;
  373. error:
  374. of_node_put(endpoint);
  375. return -ENODEV;
  376. }
  377. static int rpi_touchscreen_remove(struct i2c_client *i2c)
  378. {
  379. struct rpi_touchscreen *ts = i2c_get_clientdata(i2c);
  380. mipi_dsi_detach(ts->dsi);
  381. drm_panel_remove(&ts->base);
  382. mipi_dsi_device_unregister(ts->dsi);
  383. return 0;
  384. }
  385. static int rpi_touchscreen_dsi_probe(struct mipi_dsi_device *dsi)
  386. {
  387. int ret;
  388. dsi->mode_flags = (MIPI_DSI_MODE_VIDEO |
  389. MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
  390. MIPI_DSI_MODE_LPM);
  391. dsi->format = MIPI_DSI_FMT_RGB888;
  392. dsi->lanes = 1;
  393. ret = mipi_dsi_attach(dsi);
  394. if (ret)
  395. dev_err(&dsi->dev, "failed to attach dsi to host: %d\n", ret);
  396. return ret;
  397. }
  398. static struct mipi_dsi_driver rpi_touchscreen_dsi_driver = {
  399. .driver.name = RPI_DSI_DRIVER_NAME,
  400. .probe = rpi_touchscreen_dsi_probe,
  401. };
  402. static const struct of_device_id rpi_touchscreen_of_ids[] = {
  403. { .compatible = "raspberrypi,7inch-touchscreen-panel" },
  404. { } /* sentinel */
  405. };
  406. MODULE_DEVICE_TABLE(of, rpi_touchscreen_of_ids);
  407. static struct i2c_driver rpi_touchscreen_driver = {
  408. .driver = {
  409. .name = "rpi_touchscreen",
  410. .of_match_table = rpi_touchscreen_of_ids,
  411. },
  412. .probe = rpi_touchscreen_probe,
  413. .remove = rpi_touchscreen_remove,
  414. };
  415. static int __init rpi_touchscreen_init(void)
  416. {
  417. mipi_dsi_driver_register(&rpi_touchscreen_dsi_driver);
  418. return i2c_add_driver(&rpi_touchscreen_driver);
  419. }
  420. module_init(rpi_touchscreen_init);
  421. static void __exit rpi_touchscreen_exit(void)
  422. {
  423. i2c_del_driver(&rpi_touchscreen_driver);
  424. mipi_dsi_driver_unregister(&rpi_touchscreen_dsi_driver);
  425. }
  426. module_exit(rpi_touchscreen_exit);
  427. MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
  428. MODULE_DESCRIPTION("Raspberry Pi 7-inch touchscreen driver");
  429. MODULE_LICENSE("GPL v2");