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