lcd_mipid.c 14 KB

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