sh_mipi_dsi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. * Renesas SH-mobile MIPI DSI support
  3. *
  4. * Copyright (C) 2010 Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  5. *
  6. * This is free software; you can redistribute it and/or modify
  7. * it under the terms of version 2 of the GNU General Public License as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/delay.h>
  12. #include <linux/init.h>
  13. #include <linux/io.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/slab.h>
  16. #include <linux/string.h>
  17. #include <linux/types.h>
  18. #include <video/mipi_display.h>
  19. #include <video/sh_mipi_dsi.h>
  20. #include <video/sh_mobile_lcdc.h>
  21. #define CMTSRTCTR 0x80d0
  22. #define CMTSRTREQ 0x8070
  23. #define DSIINTE 0x0060
  24. /* E.g., sh7372 has 2 MIPI-DSIs - one for each LCDC */
  25. #define MAX_SH_MIPI_DSI 2
  26. struct sh_mipi {
  27. void __iomem *base;
  28. struct clk *dsit_clk;
  29. struct clk *dsip_clk;
  30. };
  31. static struct sh_mipi *mipi_dsi[MAX_SH_MIPI_DSI];
  32. /* Protect the above array */
  33. static DEFINE_MUTEX(array_lock);
  34. static struct sh_mipi *sh_mipi_by_handle(int handle)
  35. {
  36. if (handle >= ARRAY_SIZE(mipi_dsi) || handle < 0)
  37. return NULL;
  38. return mipi_dsi[handle];
  39. }
  40. static int sh_mipi_send_short(struct sh_mipi *mipi, u8 dsi_cmd,
  41. u8 cmd, u8 param)
  42. {
  43. u32 data = (dsi_cmd << 24) | (cmd << 16) | (param << 8);
  44. int cnt = 100;
  45. /* transmit a short packet to LCD panel */
  46. iowrite32(1 | data, mipi->base + 0x80d0); /* CMTSRTCTR */
  47. iowrite32(1, mipi->base + 0x8070); /* CMTSRTREQ */
  48. while ((ioread32(mipi->base + 0x8070) & 1) && --cnt)
  49. udelay(1);
  50. return cnt ? 0 : -ETIMEDOUT;
  51. }
  52. #define LCD_CHAN2MIPI(c) ((c) < LCDC_CHAN_MAINLCD || (c) > LCDC_CHAN_SUBLCD ? \
  53. -EINVAL : (c) - 1)
  54. static int sh_mipi_dcs(int handle, u8 cmd)
  55. {
  56. struct sh_mipi *mipi = sh_mipi_by_handle(LCD_CHAN2MIPI(handle));
  57. if (!mipi)
  58. return -ENODEV;
  59. return sh_mipi_send_short(mipi, MIPI_DSI_DCS_SHORT_WRITE, cmd, 0);
  60. }
  61. static int sh_mipi_dcs_param(int handle, u8 cmd, u8 param)
  62. {
  63. struct sh_mipi *mipi = sh_mipi_by_handle(LCD_CHAN2MIPI(handle));
  64. if (!mipi)
  65. return -ENODEV;
  66. return sh_mipi_send_short(mipi, MIPI_DSI_DCS_SHORT_WRITE_PARAM, cmd,
  67. param);
  68. }
  69. static void sh_mipi_dsi_enable(struct sh_mipi *mipi, bool enable)
  70. {
  71. /*
  72. * enable LCDC data tx, transition to LPS after completion of each HS
  73. * packet
  74. */
  75. iowrite32(0x00000002 | enable, mipi->base + 0x8000); /* DTCTR */
  76. }
  77. static void sh_mipi_shutdown(struct platform_device *pdev)
  78. {
  79. struct sh_mipi *mipi = platform_get_drvdata(pdev);
  80. sh_mipi_dsi_enable(mipi, false);
  81. }
  82. static void mipi_display_on(void *arg, struct fb_info *info)
  83. {
  84. struct sh_mipi *mipi = arg;
  85. sh_mipi_dsi_enable(mipi, true);
  86. }
  87. static void mipi_display_off(void *arg)
  88. {
  89. struct sh_mipi *mipi = arg;
  90. sh_mipi_dsi_enable(mipi, false);
  91. }
  92. static int __init sh_mipi_setup(struct sh_mipi *mipi,
  93. struct sh_mipi_dsi_info *pdata)
  94. {
  95. void __iomem *base = mipi->base;
  96. struct sh_mobile_lcdc_chan_cfg *ch = pdata->lcd_chan;
  97. u32 pctype, datatype, pixfmt;
  98. u32 linelength;
  99. bool yuv;
  100. /*
  101. * Select data format. MIPI DSI is not hot-pluggable, so, we just use
  102. * the default videomode. If this ever becomes a problem, We'll have to
  103. * move this to mipi_display_on() above and use info->var.xres
  104. */
  105. switch (pdata->data_format) {
  106. case MIPI_RGB888:
  107. pctype = 0;
  108. datatype = MIPI_DSI_PACKED_PIXEL_STREAM_24;
  109. pixfmt = MIPI_DCS_PIXEL_FMT_24BIT;
  110. linelength = ch->lcd_cfg[0].xres * 3;
  111. yuv = false;
  112. break;
  113. case MIPI_RGB565:
  114. pctype = 1;
  115. datatype = MIPI_DSI_PACKED_PIXEL_STREAM_16;
  116. pixfmt = MIPI_DCS_PIXEL_FMT_16BIT;
  117. linelength = ch->lcd_cfg[0].xres * 2;
  118. yuv = false;
  119. break;
  120. case MIPI_RGB666_LP:
  121. pctype = 2;
  122. datatype = MIPI_DSI_PIXEL_STREAM_3BYTE_18;
  123. pixfmt = MIPI_DCS_PIXEL_FMT_24BIT;
  124. linelength = ch->lcd_cfg[0].xres * 3;
  125. yuv = false;
  126. break;
  127. case MIPI_RGB666:
  128. pctype = 3;
  129. datatype = MIPI_DSI_PACKED_PIXEL_STREAM_18;
  130. pixfmt = MIPI_DCS_PIXEL_FMT_18BIT;
  131. linelength = (ch->lcd_cfg[0].xres * 18 + 7) / 8;
  132. yuv = false;
  133. break;
  134. case MIPI_BGR888:
  135. pctype = 8;
  136. datatype = MIPI_DSI_PACKED_PIXEL_STREAM_24;
  137. pixfmt = MIPI_DCS_PIXEL_FMT_24BIT;
  138. linelength = ch->lcd_cfg[0].xres * 3;
  139. yuv = false;
  140. break;
  141. case MIPI_BGR565:
  142. pctype = 9;
  143. datatype = MIPI_DSI_PACKED_PIXEL_STREAM_16;
  144. pixfmt = MIPI_DCS_PIXEL_FMT_16BIT;
  145. linelength = ch->lcd_cfg[0].xres * 2;
  146. yuv = false;
  147. break;
  148. case MIPI_BGR666_LP:
  149. pctype = 0xa;
  150. datatype = MIPI_DSI_PIXEL_STREAM_3BYTE_18;
  151. pixfmt = MIPI_DCS_PIXEL_FMT_24BIT;
  152. linelength = ch->lcd_cfg[0].xres * 3;
  153. yuv = false;
  154. break;
  155. case MIPI_BGR666:
  156. pctype = 0xb;
  157. datatype = MIPI_DSI_PACKED_PIXEL_STREAM_18;
  158. pixfmt = MIPI_DCS_PIXEL_FMT_18BIT;
  159. linelength = (ch->lcd_cfg[0].xres * 18 + 7) / 8;
  160. yuv = false;
  161. break;
  162. case MIPI_YUYV:
  163. pctype = 4;
  164. datatype = MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16;
  165. pixfmt = MIPI_DCS_PIXEL_FMT_16BIT;
  166. linelength = ch->lcd_cfg[0].xres * 2;
  167. yuv = true;
  168. break;
  169. case MIPI_UYVY:
  170. pctype = 5;
  171. datatype = MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16;
  172. pixfmt = MIPI_DCS_PIXEL_FMT_16BIT;
  173. linelength = ch->lcd_cfg[0].xres * 2;
  174. yuv = true;
  175. break;
  176. case MIPI_YUV420_L:
  177. pctype = 6;
  178. datatype = MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12;
  179. pixfmt = MIPI_DCS_PIXEL_FMT_12BIT;
  180. linelength = (ch->lcd_cfg[0].xres * 12 + 7) / 8;
  181. yuv = true;
  182. break;
  183. case MIPI_YUV420:
  184. pctype = 7;
  185. datatype = MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12;
  186. pixfmt = MIPI_DCS_PIXEL_FMT_12BIT;
  187. /* Length of U/V line */
  188. linelength = (ch->lcd_cfg[0].xres + 1) / 2;
  189. yuv = true;
  190. break;
  191. default:
  192. return -EINVAL;
  193. }
  194. if ((yuv && ch->interface_type != YUV422) ||
  195. (!yuv && ch->interface_type != RGB24))
  196. return -EINVAL;
  197. /* reset DSI link */
  198. iowrite32(0x00000001, base); /* SYSCTRL */
  199. /* Hold reset for 100 cycles of the slowest of bus, HS byte and LP clock */
  200. udelay(50);
  201. iowrite32(0x00000000, base); /* SYSCTRL */
  202. /* setup DSI link */
  203. /*
  204. * Default = ULPS enable |
  205. * Contention detection enabled |
  206. * EoT packet transmission enable |
  207. * CRC check enable |
  208. * ECC check enable
  209. * additionally enable first two lanes
  210. */
  211. iowrite32(0x00003703, base + 0x04); /* SYSCONF */
  212. /*
  213. * T_wakeup = 0x7000
  214. * T_hs-trail = 3
  215. * T_hs-prepare = 3
  216. * T_clk-trail = 3
  217. * T_clk-prepare = 2
  218. */
  219. iowrite32(0x70003332, base + 0x08); /* TIMSET */
  220. /* no responses requested */
  221. iowrite32(0x00000000, base + 0x18); /* RESREQSET0 */
  222. /* request response to packets of type 0x28 */
  223. iowrite32(0x00000100, base + 0x1c); /* RESREQSET1 */
  224. /* High-speed transmission timeout, default 0xffffffff */
  225. iowrite32(0x0fffffff, base + 0x20); /* HSTTOVSET */
  226. /* LP reception timeout, default 0xffffffff */
  227. iowrite32(0x0fffffff, base + 0x24); /* LPRTOVSET */
  228. /* Turn-around timeout, default 0xffffffff */
  229. iowrite32(0x0fffffff, base + 0x28); /* TATOVSET */
  230. /* Peripheral reset timeout, default 0xffffffff */
  231. iowrite32(0x0fffffff, base + 0x2c); /* PRTOVSET */
  232. /* Enable timeout counters */
  233. iowrite32(0x00000f00, base + 0x30); /* DSICTRL */
  234. /* Interrupts not used, disable all */
  235. iowrite32(0, base + DSIINTE);
  236. /* DSI-Tx bias on */
  237. iowrite32(0x00000001, base + 0x70); /* PHYCTRL */
  238. udelay(200);
  239. /* Deassert resets, power on, set multiplier */
  240. iowrite32(0x03070b01, base + 0x70); /* PHYCTRL */
  241. /* setup l-bridge */
  242. /*
  243. * Enable transmission of all packets,
  244. * transmit LPS after each HS packet completion
  245. */
  246. iowrite32(0x00000006, base + 0x8000); /* DTCTR */
  247. /* VSYNC width = 2 (<< 17) */
  248. iowrite32(0x00040000 | (pctype << 12) | datatype, base + 0x8020); /* VMCTR1 */
  249. /*
  250. * Non-burst mode with sync pulses: VSE and HSE are output,
  251. * HSA period allowed, no commands in LP
  252. */
  253. iowrite32(0x00e00000, base + 0x8024); /* VMCTR2 */
  254. /*
  255. * 0x660 = 1632 bytes per line (RGB24, 544 pixels: see
  256. * sh_mobile_lcdc_info.ch[0].lcd_cfg[0].xres), HSALEN = 1 - default
  257. * (unused, since VMCTR2[HSABM] = 0)
  258. */
  259. iowrite32(1 | (linelength << 16), base + 0x8028); /* VMLEN1 */
  260. msleep(5);
  261. /* setup LCD panel */
  262. /* cf. drivers/video/omap/lcd_mipid.c */
  263. sh_mipi_dcs(ch->chan, MIPI_DCS_EXIT_SLEEP_MODE);
  264. msleep(120);
  265. /*
  266. * [7] - Page Address Mode
  267. * [6] - Column Address Mode
  268. * [5] - Page / Column Address Mode
  269. * [4] - Display Device Line Refresh Order
  270. * [3] - RGB/BGR Order
  271. * [2] - Display Data Latch Data Order
  272. * [1] - Flip Horizontal
  273. * [0] - Flip Vertical
  274. */
  275. sh_mipi_dcs_param(ch->chan, MIPI_DCS_SET_ADDRESS_MODE, 0x00);
  276. /* cf. set_data_lines() */
  277. sh_mipi_dcs_param(ch->chan, MIPI_DCS_SET_PIXEL_FORMAT,
  278. pixfmt << 4);
  279. sh_mipi_dcs(ch->chan, MIPI_DCS_SET_DISPLAY_ON);
  280. return 0;
  281. }
  282. static int __init sh_mipi_probe(struct platform_device *pdev)
  283. {
  284. struct sh_mipi *mipi;
  285. struct sh_mipi_dsi_info *pdata = pdev->dev.platform_data;
  286. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  287. unsigned long rate, f_current;
  288. int idx = pdev->id, ret;
  289. char dsip_clk[] = "dsi.p_clk";
  290. if (!res || idx >= ARRAY_SIZE(mipi_dsi) || !pdata)
  291. return -ENODEV;
  292. mutex_lock(&array_lock);
  293. if (idx < 0)
  294. for (idx = 0; idx < ARRAY_SIZE(mipi_dsi) && mipi_dsi[idx]; idx++)
  295. ;
  296. if (idx == ARRAY_SIZE(mipi_dsi)) {
  297. ret = -EBUSY;
  298. goto efindslot;
  299. }
  300. mipi = kzalloc(sizeof(*mipi), GFP_KERNEL);
  301. if (!mipi) {
  302. ret = -ENOMEM;
  303. goto ealloc;
  304. }
  305. if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
  306. dev_err(&pdev->dev, "MIPI register region already claimed\n");
  307. ret = -EBUSY;
  308. goto ereqreg;
  309. }
  310. mipi->base = ioremap(res->start, resource_size(res));
  311. if (!mipi->base) {
  312. ret = -ENOMEM;
  313. goto emap;
  314. }
  315. mipi->dsit_clk = clk_get(&pdev->dev, "dsit_clk");
  316. if (IS_ERR(mipi->dsit_clk)) {
  317. ret = PTR_ERR(mipi->dsit_clk);
  318. goto eclktget;
  319. }
  320. f_current = clk_get_rate(mipi->dsit_clk);
  321. /* 80MHz required by the datasheet */
  322. rate = clk_round_rate(mipi->dsit_clk, 80000000);
  323. if (rate > 0 && rate != f_current)
  324. ret = clk_set_rate(mipi->dsit_clk, rate);
  325. else
  326. ret = rate;
  327. if (ret < 0)
  328. goto esettrate;
  329. dev_dbg(&pdev->dev, "DSI-T clk %lu -> %lu\n", f_current, rate);
  330. sprintf(dsip_clk, "dsi%1.1dp_clk", idx);
  331. mipi->dsip_clk = clk_get(&pdev->dev, dsip_clk);
  332. if (IS_ERR(mipi->dsip_clk)) {
  333. ret = PTR_ERR(mipi->dsip_clk);
  334. goto eclkpget;
  335. }
  336. f_current = clk_get_rate(mipi->dsip_clk);
  337. /* Between 10 and 50MHz */
  338. rate = clk_round_rate(mipi->dsip_clk, 24000000);
  339. if (rate > 0 && rate != f_current)
  340. ret = clk_set_rate(mipi->dsip_clk, rate);
  341. else
  342. ret = rate;
  343. if (ret < 0)
  344. goto esetprate;
  345. dev_dbg(&pdev->dev, "DSI-P clk %lu -> %lu\n", f_current, rate);
  346. msleep(10);
  347. ret = clk_enable(mipi->dsit_clk);
  348. if (ret < 0)
  349. goto eclkton;
  350. ret = clk_enable(mipi->dsip_clk);
  351. if (ret < 0)
  352. goto eclkpon;
  353. mipi_dsi[idx] = mipi;
  354. ret = sh_mipi_setup(mipi, pdata);
  355. if (ret < 0)
  356. goto emipisetup;
  357. mutex_unlock(&array_lock);
  358. platform_set_drvdata(pdev, mipi);
  359. /* Set up LCDC callbacks */
  360. pdata->lcd_chan->board_cfg.board_data = mipi;
  361. pdata->lcd_chan->board_cfg.display_on = mipi_display_on;
  362. pdata->lcd_chan->board_cfg.display_off = mipi_display_off;
  363. return 0;
  364. emipisetup:
  365. mipi_dsi[idx] = NULL;
  366. clk_disable(mipi->dsip_clk);
  367. eclkpon:
  368. clk_disable(mipi->dsit_clk);
  369. eclkton:
  370. esetprate:
  371. clk_put(mipi->dsip_clk);
  372. eclkpget:
  373. esettrate:
  374. clk_put(mipi->dsit_clk);
  375. eclktget:
  376. iounmap(mipi->base);
  377. emap:
  378. release_mem_region(res->start, resource_size(res));
  379. ereqreg:
  380. kfree(mipi);
  381. ealloc:
  382. efindslot:
  383. mutex_unlock(&array_lock);
  384. return ret;
  385. }
  386. static int __exit sh_mipi_remove(struct platform_device *pdev)
  387. {
  388. struct sh_mipi_dsi_info *pdata = pdev->dev.platform_data;
  389. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  390. struct sh_mipi *mipi = platform_get_drvdata(pdev);
  391. int i, ret;
  392. mutex_lock(&array_lock);
  393. for (i = 0; i < ARRAY_SIZE(mipi_dsi) && mipi_dsi[i] != mipi; i++)
  394. ;
  395. if (i == ARRAY_SIZE(mipi_dsi)) {
  396. ret = -EINVAL;
  397. } else {
  398. ret = 0;
  399. mipi_dsi[i] = NULL;
  400. }
  401. mutex_unlock(&array_lock);
  402. if (ret < 0)
  403. return ret;
  404. pdata->lcd_chan->board_cfg.display_on = NULL;
  405. pdata->lcd_chan->board_cfg.display_off = NULL;
  406. pdata->lcd_chan->board_cfg.board_data = NULL;
  407. clk_disable(mipi->dsip_clk);
  408. clk_disable(mipi->dsit_clk);
  409. clk_put(mipi->dsit_clk);
  410. clk_put(mipi->dsip_clk);
  411. iounmap(mipi->base);
  412. if (res)
  413. release_mem_region(res->start, resource_size(res));
  414. platform_set_drvdata(pdev, NULL);
  415. kfree(mipi);
  416. return 0;
  417. }
  418. static struct platform_driver sh_mipi_driver = {
  419. .remove = __exit_p(sh_mipi_remove),
  420. .shutdown = sh_mipi_shutdown,
  421. .driver = {
  422. .name = "sh-mipi-dsi",
  423. },
  424. };
  425. static int __init sh_mipi_init(void)
  426. {
  427. return platform_driver_probe(&sh_mipi_driver, sh_mipi_probe);
  428. }
  429. module_init(sh_mipi_init);
  430. static void __exit sh_mipi_exit(void)
  431. {
  432. platform_driver_unregister(&sh_mipi_driver);
  433. }
  434. module_exit(sh_mipi_exit);
  435. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
  436. MODULE_DESCRIPTION("SuperH / ARM-shmobile MIPI DSI driver");
  437. MODULE_LICENSE("GPL v2");