panel-raspberrypi-touchscreen.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. u8 msg[] = {
  216. reg,
  217. reg >> 8,
  218. val,
  219. val >> 8,
  220. val >> 16,
  221. val >> 24,
  222. };
  223. mipi_dsi_generic_write(ts->dsi, msg, sizeof(msg));
  224. return 0;
  225. }
  226. static int rpi_touchscreen_disable(struct drm_panel *panel)
  227. {
  228. struct rpi_touchscreen *ts = panel_to_ts(panel);
  229. rpi_touchscreen_i2c_write(ts, REG_PWM, 0);
  230. rpi_touchscreen_i2c_write(ts, REG_POWERON, 0);
  231. udelay(1);
  232. return 0;
  233. }
  234. static int rpi_touchscreen_noop(struct drm_panel *panel)
  235. {
  236. return 0;
  237. }
  238. static int rpi_touchscreen_enable(struct drm_panel *panel)
  239. {
  240. struct rpi_touchscreen *ts = panel_to_ts(panel);
  241. int i;
  242. rpi_touchscreen_i2c_write(ts, REG_POWERON, 1);
  243. /* Wait for nPWRDWN to go low to indicate poweron is done. */
  244. for (i = 0; i < 100; i++) {
  245. if (rpi_touchscreen_i2c_read(ts, REG_PORTB) & 1)
  246. break;
  247. }
  248. rpi_touchscreen_write(ts, DSI_LANEENABLE,
  249. DSI_LANEENABLE_CLOCK |
  250. DSI_LANEENABLE_D0);
  251. rpi_touchscreen_write(ts, PPI_D0S_CLRSIPOCOUNT, 0x05);
  252. rpi_touchscreen_write(ts, PPI_D1S_CLRSIPOCOUNT, 0x05);
  253. rpi_touchscreen_write(ts, PPI_D0S_ATMR, 0x00);
  254. rpi_touchscreen_write(ts, PPI_D1S_ATMR, 0x00);
  255. rpi_touchscreen_write(ts, PPI_LPTXTIMECNT, 0x03);
  256. rpi_touchscreen_write(ts, SPICMR, 0x00);
  257. rpi_touchscreen_write(ts, LCDCTRL, 0x00100150);
  258. rpi_touchscreen_write(ts, SYSCTRL, 0x040f);
  259. msleep(100);
  260. rpi_touchscreen_write(ts, PPI_STARTPPI, 0x01);
  261. rpi_touchscreen_write(ts, DSI_STARTDSI, 0x01);
  262. msleep(100);
  263. /* Turn on the backlight. */
  264. rpi_touchscreen_i2c_write(ts, REG_PWM, 255);
  265. /* Default to the same orientation as the closed source
  266. * firmware used for the panel. Runtime rotation
  267. * configuration will be supported using VC4's plane
  268. * orientation bits.
  269. */
  270. rpi_touchscreen_i2c_write(ts, REG_PORTA, BIT(2));
  271. return 0;
  272. }
  273. static int rpi_touchscreen_get_modes(struct drm_panel *panel)
  274. {
  275. struct drm_connector *connector = panel->connector;
  276. struct drm_device *drm = panel->drm;
  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(drm, m);
  283. if (!mode) {
  284. dev_err(drm->dev, "failed to add mode %ux%u@%u\n",
  285. m->hdisplay, m->vdisplay, m->vrefresh);
  286. continue;
  287. }
  288. mode->type |= DRM_MODE_TYPE_DRIVER;
  289. if (i == 0)
  290. mode->type |= DRM_MODE_TYPE_PREFERRED;
  291. drm_mode_set_name(mode);
  292. drm_mode_probed_add(connector, mode);
  293. num++;
  294. }
  295. connector->display_info.bpc = 8;
  296. connector->display_info.width_mm = 154;
  297. connector->display_info.height_mm = 86;
  298. drm_display_info_set_bus_formats(&connector->display_info,
  299. &bus_format, 1);
  300. return num;
  301. }
  302. static const struct drm_panel_funcs rpi_touchscreen_funcs = {
  303. .disable = rpi_touchscreen_disable,
  304. .unprepare = rpi_touchscreen_noop,
  305. .prepare = rpi_touchscreen_noop,
  306. .enable = rpi_touchscreen_enable,
  307. .get_modes = rpi_touchscreen_get_modes,
  308. };
  309. static int rpi_touchscreen_probe(struct i2c_client *i2c,
  310. const struct i2c_device_id *id)
  311. {
  312. struct device *dev = &i2c->dev;
  313. struct rpi_touchscreen *ts;
  314. struct device_node *endpoint, *dsi_host_node;
  315. struct mipi_dsi_host *host;
  316. int ret, ver;
  317. struct mipi_dsi_device_info info = {
  318. .type = RPI_DSI_DRIVER_NAME,
  319. .channel = 0,
  320. .node = NULL,
  321. };
  322. ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
  323. if (!ts)
  324. return -ENOMEM;
  325. i2c_set_clientdata(i2c, ts);
  326. ts->i2c = i2c;
  327. ver = rpi_touchscreen_i2c_read(ts, REG_ID);
  328. if (ver < 0) {
  329. dev_err(dev, "Atmel I2C read failed: %d\n", ver);
  330. return -ENODEV;
  331. }
  332. switch (ver) {
  333. case 0xde: /* ver 1 */
  334. case 0xc3: /* ver 2 */
  335. break;
  336. default:
  337. dev_err(dev, "Unknown Atmel firmware revision: 0x%02x\n", ver);
  338. return -ENODEV;
  339. }
  340. /* Turn off at boot, so we can cleanly sequence powering on. */
  341. rpi_touchscreen_i2c_write(ts, REG_POWERON, 0);
  342. /* Look up the DSI host. It needs to probe before we do. */
  343. endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
  344. dsi_host_node = of_graph_get_remote_port_parent(endpoint);
  345. host = of_find_mipi_dsi_host_by_node(dsi_host_node);
  346. of_node_put(dsi_host_node);
  347. if (!host) {
  348. of_node_put(endpoint);
  349. return -EPROBE_DEFER;
  350. }
  351. info.node = of_graph_get_remote_port(endpoint);
  352. of_node_put(endpoint);
  353. ts->dsi = mipi_dsi_device_register_full(host, &info);
  354. if (IS_ERR(ts->dsi)) {
  355. dev_err(dev, "DSI device registration failed: %ld\n",
  356. PTR_ERR(ts->dsi));
  357. return PTR_ERR(ts->dsi);
  358. }
  359. ts->base.dev = dev;
  360. ts->base.funcs = &rpi_touchscreen_funcs;
  361. /* This appears last, as it's what will unblock the DSI host
  362. * driver's component bind function.
  363. */
  364. ret = drm_panel_add(&ts->base);
  365. if (ret)
  366. return ret;
  367. return 0;
  368. }
  369. static int rpi_touchscreen_remove(struct i2c_client *i2c)
  370. {
  371. struct rpi_touchscreen *ts = i2c_get_clientdata(i2c);
  372. mipi_dsi_detach(ts->dsi);
  373. drm_panel_remove(&ts->base);
  374. mipi_dsi_device_unregister(ts->dsi);
  375. kfree(ts->dsi);
  376. return 0;
  377. }
  378. static int rpi_touchscreen_dsi_probe(struct mipi_dsi_device *dsi)
  379. {
  380. int ret;
  381. dsi->mode_flags = (MIPI_DSI_MODE_VIDEO |
  382. MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
  383. MIPI_DSI_MODE_LPM);
  384. dsi->format = MIPI_DSI_FMT_RGB888;
  385. dsi->lanes = 1;
  386. ret = mipi_dsi_attach(dsi);
  387. if (ret)
  388. dev_err(&dsi->dev, "failed to attach dsi to host: %d\n", ret);
  389. return ret;
  390. }
  391. static struct mipi_dsi_driver rpi_touchscreen_dsi_driver = {
  392. .driver.name = RPI_DSI_DRIVER_NAME,
  393. .probe = rpi_touchscreen_dsi_probe,
  394. };
  395. static const struct of_device_id rpi_touchscreen_of_ids[] = {
  396. { .compatible = "raspberrypi,7inch-touchscreen-panel" },
  397. { } /* sentinel */
  398. };
  399. MODULE_DEVICE_TABLE(of, rpi_touchscreen_of_ids);
  400. static struct i2c_driver rpi_touchscreen_driver = {
  401. .driver = {
  402. .name = "rpi_touchscreen",
  403. .of_match_table = rpi_touchscreen_of_ids,
  404. },
  405. .probe = rpi_touchscreen_probe,
  406. .remove = rpi_touchscreen_remove,
  407. };
  408. static int __init rpi_touchscreen_init(void)
  409. {
  410. mipi_dsi_driver_register(&rpi_touchscreen_dsi_driver);
  411. return i2c_add_driver(&rpi_touchscreen_driver);
  412. }
  413. module_init(rpi_touchscreen_init);
  414. static void __exit rpi_touchscreen_exit(void)
  415. {
  416. i2c_del_driver(&rpi_touchscreen_driver);
  417. mipi_dsi_driver_unregister(&rpi_touchscreen_dsi_driver);
  418. }
  419. module_exit(rpi_touchscreen_exit);
  420. MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
  421. MODULE_DESCRIPTION("Raspberry Pi 7-inch touchscreen driver");
  422. MODULE_LICENSE("GPL v2");