lcd_mipid.c 14 KB

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