sh_mipi_dsi.c 14 KB

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