lcd_mipid.c 13 KB

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