lcd_mipid.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*
  2. * LCD driver for MIPI DBI-C / DCS compatible LCDs
  3. *
  4. * Copyright (C) 2006 Nokia Corporation
  5. * Author: Imre Deak <imre.deak@nokia.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. #include <linux/device.h>
  22. #include <linux/delay.h>
  23. #include <linux/slab.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/spi/spi.h>
  26. #include <plat/lcd_mipid.h>
  27. #include "omapfb.h"
  28. #define MIPID_MODULE_NAME "lcd_mipid"
  29. #define MIPID_CMD_READ_DISP_ID 0x04
  30. #define MIPID_CMD_READ_RED 0x06
  31. #define MIPID_CMD_READ_GREEN 0x07
  32. #define MIPID_CMD_READ_BLUE 0x08
  33. #define MIPID_CMD_READ_DISP_STATUS 0x09
  34. #define MIPID_CMD_RDDSDR 0x0F
  35. #define MIPID_CMD_SLEEP_IN 0x10
  36. #define MIPID_CMD_SLEEP_OUT 0x11
  37. #define MIPID_CMD_DISP_OFF 0x28
  38. #define MIPID_CMD_DISP_ON 0x29
  39. #define MIPID_ESD_CHECK_PERIOD msecs_to_jiffies(5000)
  40. #define to_mipid_device(p) container_of(p, struct mipid_device, \
  41. panel)
  42. struct mipid_device {
  43. int enabled;
  44. int revision;
  45. unsigned int saved_bklight_level;
  46. unsigned long hw_guard_end; /* next value of jiffies
  47. when we can issue the
  48. next sleep in/out command */
  49. unsigned long hw_guard_wait; /* max guard time in jiffies */
  50. struct omapfb_device *fbdev;
  51. struct spi_device *spi;
  52. struct mutex mutex;
  53. struct lcd_panel panel;
  54. struct workqueue_struct *esd_wq;
  55. struct delayed_work esd_work;
  56. void (*esd_check)(struct mipid_device *m);
  57. };
  58. static void mipid_transfer(struct mipid_device *md, int cmd, const u8 *wbuf,
  59. int wlen, u8 *rbuf, int rlen)
  60. {
  61. struct spi_message m;
  62. struct spi_transfer *x, xfer[4];
  63. u16 w;
  64. int r;
  65. BUG_ON(md->spi == NULL);
  66. spi_message_init(&m);
  67. memset(xfer, 0, sizeof(xfer));
  68. x = &xfer[0];
  69. cmd &= 0xff;
  70. x->tx_buf = &cmd;
  71. x->bits_per_word = 9;
  72. x->len = 2;
  73. spi_message_add_tail(x, &m);
  74. if (wlen) {
  75. x++;
  76. x->tx_buf = wbuf;
  77. x->len = wlen;
  78. x->bits_per_word = 9;
  79. spi_message_add_tail(x, &m);
  80. }
  81. if (rlen) {
  82. x++;
  83. x->rx_buf = &w;
  84. x->len = 1;
  85. spi_message_add_tail(x, &m);
  86. if (rlen > 1) {
  87. /* Arrange for the extra clock before the first
  88. * data bit.
  89. */
  90. x->bits_per_word = 9;
  91. x->len = 2;
  92. x++;
  93. x->rx_buf = &rbuf[1];
  94. x->len = rlen - 1;
  95. spi_message_add_tail(x, &m);
  96. }
  97. }
  98. r = spi_sync(md->spi, &m);
  99. if (r < 0)
  100. dev_dbg(&md->spi->dev, "spi_sync %d\n", r);
  101. if (rlen)
  102. rbuf[0] = w & 0xff;
  103. }
  104. static inline void mipid_cmd(struct mipid_device *md, int cmd)
  105. {
  106. mipid_transfer(md, cmd, NULL, 0, NULL, 0);
  107. }
  108. static inline void mipid_write(struct mipid_device *md,
  109. int reg, const u8 *buf, int len)
  110. {
  111. mipid_transfer(md, reg, buf, len, NULL, 0);
  112. }
  113. static inline void mipid_read(struct mipid_device *md,
  114. int reg, u8 *buf, int len)
  115. {
  116. mipid_transfer(md, reg, NULL, 0, buf, len);
  117. }
  118. static void set_data_lines(struct mipid_device *md, int data_lines)
  119. {
  120. u16 par;
  121. switch (data_lines) {
  122. case 16:
  123. par = 0x150;
  124. break;
  125. case 18:
  126. par = 0x160;
  127. break;
  128. case 24:
  129. par = 0x170;
  130. break;
  131. }
  132. mipid_write(md, 0x3a, (u8 *)&par, 2);
  133. }
  134. static void send_init_string(struct mipid_device *md)
  135. {
  136. u16 initpar[] = { 0x0102, 0x0100, 0x0100 };
  137. mipid_write(md, 0xc2, (u8 *)initpar, sizeof(initpar));
  138. set_data_lines(md, md->panel.data_lines);
  139. }
  140. static void hw_guard_start(struct mipid_device *md, int guard_msec)
  141. {
  142. md->hw_guard_wait = msecs_to_jiffies(guard_msec);
  143. md->hw_guard_end = jiffies + md->hw_guard_wait;
  144. }
  145. static void hw_guard_wait(struct mipid_device *md)
  146. {
  147. unsigned long wait = md->hw_guard_end - jiffies;
  148. if ((long)wait > 0 && wait <= md->hw_guard_wait) {
  149. set_current_state(TASK_UNINTERRUPTIBLE);
  150. schedule_timeout(wait);
  151. }
  152. }
  153. static void set_sleep_mode(struct mipid_device *md, int on)
  154. {
  155. int cmd, sleep_time = 50;
  156. if (on)
  157. cmd = MIPID_CMD_SLEEP_IN;
  158. else
  159. cmd = MIPID_CMD_SLEEP_OUT;
  160. hw_guard_wait(md);
  161. mipid_cmd(md, cmd);
  162. hw_guard_start(md, 120);
  163. /*
  164. * When we enable the panel, it seems we _have_ to sleep
  165. * 120 ms before sending the init string. When disabling the
  166. * panel we'll sleep for the duration of 2 frames, so that the
  167. * controller can still provide the PCLK,HS,VS signals.
  168. */
  169. if (!on)
  170. sleep_time = 120;
  171. msleep(sleep_time);
  172. }
  173. static void set_display_state(struct mipid_device *md, int enabled)
  174. {
  175. int cmd = enabled ? MIPID_CMD_DISP_ON : MIPID_CMD_DISP_OFF;
  176. mipid_cmd(md, cmd);
  177. }
  178. static int mipid_set_bklight_level(struct lcd_panel *panel, unsigned int level)
  179. {
  180. struct mipid_device *md = to_mipid_device(panel);
  181. struct mipid_platform_data *pd = md->spi->dev.platform_data;
  182. if (pd->get_bklight_max == NULL || pd->set_bklight_level == NULL)
  183. return -ENODEV;
  184. if (level > pd->get_bklight_max(pd))
  185. return -EINVAL;
  186. if (!md->enabled) {
  187. md->saved_bklight_level = level;
  188. return 0;
  189. }
  190. pd->set_bklight_level(pd, level);
  191. return 0;
  192. }
  193. static unsigned int mipid_get_bklight_level(struct lcd_panel *panel)
  194. {
  195. struct mipid_device *md = to_mipid_device(panel);
  196. struct mipid_platform_data *pd = md->spi->dev.platform_data;
  197. if (pd->get_bklight_level == NULL)
  198. return -ENODEV;
  199. return pd->get_bklight_level(pd);
  200. }
  201. static unsigned int mipid_get_bklight_max(struct lcd_panel *panel)
  202. {
  203. struct mipid_device *md = to_mipid_device(panel);
  204. struct mipid_platform_data *pd = md->spi->dev.platform_data;
  205. if (pd->get_bklight_max == NULL)
  206. return -ENODEV;
  207. return pd->get_bklight_max(pd);
  208. }
  209. static unsigned long mipid_get_caps(struct lcd_panel *panel)
  210. {
  211. return OMAPFB_CAPS_SET_BACKLIGHT;
  212. }
  213. static u16 read_first_pixel(struct mipid_device *md)
  214. {
  215. u16 pixel;
  216. u8 red, green, blue;
  217. mutex_lock(&md->mutex);
  218. mipid_read(md, MIPID_CMD_READ_RED, &red, 1);
  219. mipid_read(md, MIPID_CMD_READ_GREEN, &green, 1);
  220. mipid_read(md, MIPID_CMD_READ_BLUE, &blue, 1);
  221. mutex_unlock(&md->mutex);
  222. switch (md->panel.data_lines) {
  223. case 16:
  224. pixel = ((red >> 1) << 11) | (green << 5) | (blue >> 1);
  225. break;
  226. case 24:
  227. /* 24 bit -> 16 bit */
  228. pixel = ((red >> 3) << 11) | ((green >> 2) << 5) |
  229. (blue >> 3);
  230. break;
  231. default:
  232. pixel = 0;
  233. BUG();
  234. }
  235. return pixel;
  236. }
  237. static int mipid_run_test(struct lcd_panel *panel, int test_num)
  238. {
  239. struct mipid_device *md = to_mipid_device(panel);
  240. static const u16 test_values[4] = {
  241. 0x0000, 0xffff, 0xaaaa, 0x5555,
  242. };
  243. int i;
  244. if (test_num != MIPID_TEST_RGB_LINES)
  245. return MIPID_TEST_INVALID;
  246. for (i = 0; i < ARRAY_SIZE(test_values); i++) {
  247. int delay;
  248. unsigned long tmo;
  249. omapfb_write_first_pixel(md->fbdev, test_values[i]);
  250. tmo = jiffies + msecs_to_jiffies(100);
  251. delay = 25;
  252. while (1) {
  253. u16 pixel;
  254. msleep(delay);
  255. pixel = read_first_pixel(md);
  256. if (pixel == test_values[i])
  257. break;
  258. if (time_after(jiffies, tmo)) {
  259. dev_err(&md->spi->dev,
  260. "MIPI LCD RGB I/F test failed: "
  261. "expecting %04x, got %04x\n",
  262. test_values[i], pixel);
  263. return MIPID_TEST_FAILED;
  264. }
  265. delay = 10;
  266. }
  267. }
  268. return 0;
  269. }
  270. static void ls041y3_esd_recover(struct mipid_device *md)
  271. {
  272. dev_err(&md->spi->dev, "performing LCD ESD recovery\n");
  273. set_sleep_mode(md, 1);
  274. set_sleep_mode(md, 0);
  275. }
  276. static void ls041y3_esd_check_mode1(struct mipid_device *md)
  277. {
  278. u8 state1, state2;
  279. mipid_read(md, MIPID_CMD_RDDSDR, &state1, 1);
  280. set_sleep_mode(md, 0);
  281. mipid_read(md, MIPID_CMD_RDDSDR, &state2, 1);
  282. dev_dbg(&md->spi->dev, "ESD mode 1 state1 %02x state2 %02x\n",
  283. state1, state2);
  284. /* Each sleep out command will trigger a self diagnostic and flip
  285. * Bit6 if the test passes.
  286. */
  287. if (!((state1 ^ state2) & (1 << 6)))
  288. ls041y3_esd_recover(md);
  289. }
  290. static void ls041y3_esd_check_mode2(struct mipid_device *md)
  291. {
  292. int i;
  293. u8 rbuf[2];
  294. static const struct {
  295. int cmd;
  296. int wlen;
  297. u16 wbuf[3];
  298. } *rd, rd_ctrl[7] = {
  299. { 0xb0, 4, { 0x0101, 0x01fe, } },
  300. { 0xb1, 4, { 0x01de, 0x0121, } },
  301. { 0xc2, 4, { 0x0100, 0x0100, } },
  302. { 0xbd, 2, { 0x0100, } },
  303. { 0xc2, 4, { 0x01fc, 0x0103, } },
  304. { 0xb4, 0, },
  305. { 0x00, 0, },
  306. };
  307. rd = rd_ctrl;
  308. for (i = 0; i < 3; i++, rd++)
  309. mipid_write(md, rd->cmd, (u8 *)rd->wbuf, rd->wlen);
  310. udelay(10);
  311. mipid_read(md, rd->cmd, rbuf, 2);
  312. rd++;
  313. for (i = 0; i < 3; i++, rd++) {
  314. udelay(10);
  315. mipid_write(md, rd->cmd, (u8 *)rd->wbuf, rd->wlen);
  316. }
  317. dev_dbg(&md->spi->dev, "ESD mode 2 state %02x\n", rbuf[1]);
  318. if (rbuf[1] == 0x00)
  319. ls041y3_esd_recover(md);
  320. }
  321. static void ls041y3_esd_check(struct mipid_device *md)
  322. {
  323. ls041y3_esd_check_mode1(md);
  324. if (md->revision >= 0x88)
  325. ls041y3_esd_check_mode2(md);
  326. }
  327. static void mipid_esd_start_check(struct mipid_device *md)
  328. {
  329. if (md->esd_check != NULL)
  330. queue_delayed_work(md->esd_wq, &md->esd_work,
  331. MIPID_ESD_CHECK_PERIOD);
  332. }
  333. static void mipid_esd_stop_check(struct mipid_device *md)
  334. {
  335. if (md->esd_check != NULL)
  336. cancel_delayed_work_sync(&md->esd_work);
  337. }
  338. static void mipid_esd_work(struct work_struct *work)
  339. {
  340. struct mipid_device *md = container_of(work, struct mipid_device,
  341. esd_work.work);
  342. mutex_lock(&md->mutex);
  343. md->esd_check(md);
  344. mutex_unlock(&md->mutex);
  345. mipid_esd_start_check(md);
  346. }
  347. static int mipid_enable(struct lcd_panel *panel)
  348. {
  349. struct mipid_device *md = to_mipid_device(panel);
  350. mutex_lock(&md->mutex);
  351. if (md->enabled) {
  352. mutex_unlock(&md->mutex);
  353. return 0;
  354. }
  355. set_sleep_mode(md, 0);
  356. md->enabled = 1;
  357. send_init_string(md);
  358. set_display_state(md, 1);
  359. mipid_set_bklight_level(panel, md->saved_bklight_level);
  360. mipid_esd_start_check(md);
  361. mutex_unlock(&md->mutex);
  362. return 0;
  363. }
  364. static void mipid_disable(struct lcd_panel *panel)
  365. {
  366. struct mipid_device *md = to_mipid_device(panel);
  367. /*
  368. * A final ESD work might be called before returning,
  369. * so do this without holding the lock.
  370. */
  371. mipid_esd_stop_check(md);
  372. mutex_lock(&md->mutex);
  373. if (!md->enabled) {
  374. mutex_unlock(&md->mutex);
  375. return;
  376. }
  377. md->saved_bklight_level = mipid_get_bklight_level(panel);
  378. mipid_set_bklight_level(panel, 0);
  379. set_display_state(md, 0);
  380. set_sleep_mode(md, 1);
  381. md->enabled = 0;
  382. mutex_unlock(&md->mutex);
  383. }
  384. static int panel_enabled(struct mipid_device *md)
  385. {
  386. u32 disp_status;
  387. int enabled;
  388. mipid_read(md, MIPID_CMD_READ_DISP_STATUS, (u8 *)&disp_status, 4);
  389. disp_status = __be32_to_cpu(disp_status);
  390. enabled = (disp_status & (1 << 17)) && (disp_status & (1 << 10));
  391. dev_dbg(&md->spi->dev,
  392. "LCD panel %senabled by bootloader (status 0x%04x)\n",
  393. enabled ? "" : "not ", disp_status);
  394. return enabled;
  395. }
  396. static int mipid_init(struct lcd_panel *panel,
  397. struct omapfb_device *fbdev)
  398. {
  399. struct mipid_device *md = to_mipid_device(panel);
  400. md->fbdev = fbdev;
  401. md->esd_wq = create_singlethread_workqueue("mipid_esd");
  402. if (md->esd_wq == NULL) {
  403. dev_err(&md->spi->dev, "can't create ESD workqueue\n");
  404. return -ENOMEM;
  405. }
  406. INIT_DELAYED_WORK(&md->esd_work, mipid_esd_work);
  407. mutex_init(&md->mutex);
  408. md->enabled = panel_enabled(md);
  409. if (md->enabled)
  410. mipid_esd_start_check(md);
  411. else
  412. md->saved_bklight_level = mipid_get_bklight_level(panel);
  413. return 0;
  414. }
  415. static void mipid_cleanup(struct lcd_panel *panel)
  416. {
  417. struct mipid_device *md = to_mipid_device(panel);
  418. if (md->enabled)
  419. mipid_esd_stop_check(md);
  420. destroy_workqueue(md->esd_wq);
  421. }
  422. static struct lcd_panel mipid_panel = {
  423. .config = OMAP_LCDC_PANEL_TFT,
  424. .bpp = 16,
  425. .x_res = 800,
  426. .y_res = 480,
  427. .pixel_clock = 21940,
  428. .hsw = 50,
  429. .hfp = 20,
  430. .hbp = 15,
  431. .vsw = 2,
  432. .vfp = 1,
  433. .vbp = 3,
  434. .init = mipid_init,
  435. .cleanup = mipid_cleanup,
  436. .enable = mipid_enable,
  437. .disable = mipid_disable,
  438. .get_caps = mipid_get_caps,
  439. .set_bklight_level = mipid_set_bklight_level,
  440. .get_bklight_level = mipid_get_bklight_level,
  441. .get_bklight_max = mipid_get_bklight_max,
  442. .run_test = mipid_run_test,
  443. };
  444. static int mipid_detect(struct mipid_device *md)
  445. {
  446. struct mipid_platform_data *pdata;
  447. u8 display_id[3];
  448. pdata = md->spi->dev.platform_data;
  449. if (pdata == NULL) {
  450. dev_err(&md->spi->dev, "missing platform data\n");
  451. return -ENOENT;
  452. }
  453. mipid_read(md, MIPID_CMD_READ_DISP_ID, display_id, 3);
  454. dev_dbg(&md->spi->dev, "MIPI display ID: %02x%02x%02x\n",
  455. display_id[0], display_id[1], display_id[2]);
  456. switch (display_id[0]) {
  457. case 0x45:
  458. md->panel.name = "lph8923";
  459. break;
  460. case 0x83:
  461. md->panel.name = "ls041y3";
  462. md->esd_check = ls041y3_esd_check;
  463. break;
  464. default:
  465. md->panel.name = "unknown";
  466. dev_err(&md->spi->dev, "invalid display ID\n");
  467. return -ENODEV;
  468. }
  469. md->revision = display_id[1];
  470. md->panel.data_lines = pdata->data_lines;
  471. pr_info("omapfb: %s rev %02x LCD detected, %d data lines\n",
  472. md->panel.name, md->revision, md->panel.data_lines);
  473. return 0;
  474. }
  475. static int mipid_spi_probe(struct spi_device *spi)
  476. {
  477. struct mipid_device *md;
  478. int r;
  479. md = kzalloc(sizeof(*md), GFP_KERNEL);
  480. if (md == NULL) {
  481. dev_err(&spi->dev, "out of memory\n");
  482. return -ENOMEM;
  483. }
  484. spi->mode = SPI_MODE_0;
  485. md->spi = spi;
  486. dev_set_drvdata(&spi->dev, md);
  487. md->panel = mipid_panel;
  488. r = mipid_detect(md);
  489. if (r < 0)
  490. return r;
  491. omapfb_register_panel(&md->panel);
  492. return 0;
  493. }
  494. static int mipid_spi_remove(struct spi_device *spi)
  495. {
  496. struct mipid_device *md = dev_get_drvdata(&spi->dev);
  497. mipid_disable(&md->panel);
  498. kfree(md);
  499. return 0;
  500. }
  501. static struct spi_driver mipid_spi_driver = {
  502. .driver = {
  503. .name = MIPID_MODULE_NAME,
  504. .bus = &spi_bus_type,
  505. .owner = THIS_MODULE,
  506. },
  507. .probe = mipid_spi_probe,
  508. .remove = __devexit_p(mipid_spi_remove),
  509. };
  510. static int __init mipid_drv_init(void)
  511. {
  512. spi_register_driver(&mipid_spi_driver);
  513. return 0;
  514. }
  515. module_init(mipid_drv_init);
  516. static void __exit mipid_drv_cleanup(void)
  517. {
  518. spi_unregister_driver(&mipid_spi_driver);
  519. }
  520. module_exit(mipid_drv_cleanup);
  521. MODULE_DESCRIPTION("MIPI display driver");
  522. MODULE_LICENSE("GPL");