panel-n8x0.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /* #define DEBUG */
  2. #include <linux/module.h>
  3. #include <linux/delay.h>
  4. #include <linux/slab.h>
  5. #include <linux/gpio.h>
  6. #include <linux/spi/spi.h>
  7. #include <linux/fb.h>
  8. #include <video/omapdss.h>
  9. #include <video/omap-panel-data.h>
  10. #define BLIZZARD_REV_CODE 0x00
  11. #define BLIZZARD_CONFIG 0x02
  12. #define BLIZZARD_PLL_DIV 0x04
  13. #define BLIZZARD_PLL_LOCK_RANGE 0x06
  14. #define BLIZZARD_PLL_CLOCK_SYNTH_0 0x08
  15. #define BLIZZARD_PLL_CLOCK_SYNTH_1 0x0a
  16. #define BLIZZARD_PLL_MODE 0x0c
  17. #define BLIZZARD_CLK_SRC 0x0e
  18. #define BLIZZARD_MEM_BANK0_ACTIVATE 0x10
  19. #define BLIZZARD_MEM_BANK0_STATUS 0x14
  20. #define BLIZZARD_PANEL_CONFIGURATION 0x28
  21. #define BLIZZARD_HDISP 0x2a
  22. #define BLIZZARD_HNDP 0x2c
  23. #define BLIZZARD_VDISP0 0x2e
  24. #define BLIZZARD_VDISP1 0x30
  25. #define BLIZZARD_VNDP 0x32
  26. #define BLIZZARD_HSW 0x34
  27. #define BLIZZARD_VSW 0x38
  28. #define BLIZZARD_DISPLAY_MODE 0x68
  29. #define BLIZZARD_INPUT_WIN_X_START_0 0x6c
  30. #define BLIZZARD_DATA_SOURCE_SELECT 0x8e
  31. #define BLIZZARD_DISP_MEM_DATA_PORT 0x90
  32. #define BLIZZARD_DISP_MEM_READ_ADDR0 0x92
  33. #define BLIZZARD_POWER_SAVE 0xE6
  34. #define BLIZZARD_NDISP_CTRL_STATUS 0xE8
  35. /* Data source select */
  36. /* For S1D13745 */
  37. #define BLIZZARD_SRC_WRITE_LCD_BACKGROUND 0x00
  38. #define BLIZZARD_SRC_WRITE_LCD_DESTRUCTIVE 0x01
  39. #define BLIZZARD_SRC_WRITE_OVERLAY_ENABLE 0x04
  40. #define BLIZZARD_SRC_DISABLE_OVERLAY 0x05
  41. /* For S1D13744 */
  42. #define BLIZZARD_SRC_WRITE_LCD 0x00
  43. #define BLIZZARD_SRC_BLT_LCD 0x06
  44. #define BLIZZARD_COLOR_RGB565 0x01
  45. #define BLIZZARD_COLOR_YUV420 0x09
  46. #define BLIZZARD_VERSION_S1D13745 0x01 /* Hailstorm */
  47. #define BLIZZARD_VERSION_S1D13744 0x02 /* Blizzard */
  48. #define MIPID_CMD_READ_DISP_ID 0x04
  49. #define MIPID_CMD_READ_RED 0x06
  50. #define MIPID_CMD_READ_GREEN 0x07
  51. #define MIPID_CMD_READ_BLUE 0x08
  52. #define MIPID_CMD_READ_DISP_STATUS 0x09
  53. #define MIPID_CMD_RDDSDR 0x0F
  54. #define MIPID_CMD_SLEEP_IN 0x10
  55. #define MIPID_CMD_SLEEP_OUT 0x11
  56. #define MIPID_CMD_DISP_OFF 0x28
  57. #define MIPID_CMD_DISP_ON 0x29
  58. static struct panel_drv_data {
  59. struct mutex lock;
  60. struct omap_dss_device *dssdev;
  61. struct spi_device *spidev;
  62. int blizzard_ver;
  63. } s_drv_data;
  64. static inline
  65. struct panel_n8x0_data *get_board_data(const struct omap_dss_device *dssdev)
  66. {
  67. return dssdev->data;
  68. }
  69. static inline
  70. struct panel_drv_data *get_drv_data(const struct omap_dss_device *dssdev)
  71. {
  72. return &s_drv_data;
  73. }
  74. static inline void blizzard_cmd(u8 cmd)
  75. {
  76. omap_rfbi_write_command(&cmd, 1);
  77. }
  78. static inline void blizzard_write(u8 cmd, const u8 *buf, int len)
  79. {
  80. omap_rfbi_write_command(&cmd, 1);
  81. omap_rfbi_write_data(buf, len);
  82. }
  83. static inline void blizzard_read(u8 cmd, u8 *buf, int len)
  84. {
  85. omap_rfbi_write_command(&cmd, 1);
  86. omap_rfbi_read_data(buf, len);
  87. }
  88. static u8 blizzard_read_reg(u8 cmd)
  89. {
  90. u8 data;
  91. blizzard_read(cmd, &data, 1);
  92. return data;
  93. }
  94. static void blizzard_ctrl_setup_update(struct omap_dss_device *dssdev,
  95. int x, int y, int w, int h)
  96. {
  97. struct panel_drv_data *ddata = get_drv_data(dssdev);
  98. u8 tmp[18];
  99. int x_end, y_end;
  100. x_end = x + w - 1;
  101. y_end = y + h - 1;
  102. tmp[0] = x;
  103. tmp[1] = x >> 8;
  104. tmp[2] = y;
  105. tmp[3] = y >> 8;
  106. tmp[4] = x_end;
  107. tmp[5] = x_end >> 8;
  108. tmp[6] = y_end;
  109. tmp[7] = y_end >> 8;
  110. /* scaling? */
  111. tmp[8] = x;
  112. tmp[9] = x >> 8;
  113. tmp[10] = y;
  114. tmp[11] = y >> 8;
  115. tmp[12] = x_end;
  116. tmp[13] = x_end >> 8;
  117. tmp[14] = y_end;
  118. tmp[15] = y_end >> 8;
  119. tmp[16] = BLIZZARD_COLOR_RGB565;
  120. if (ddata->blizzard_ver == BLIZZARD_VERSION_S1D13745)
  121. tmp[17] = BLIZZARD_SRC_WRITE_LCD_BACKGROUND;
  122. else
  123. tmp[17] = ddata->blizzard_ver == BLIZZARD_VERSION_S1D13744 ?
  124. BLIZZARD_SRC_WRITE_LCD :
  125. BLIZZARD_SRC_WRITE_LCD_DESTRUCTIVE;
  126. omapdss_rfbi_set_pixel_size(dssdev, 16);
  127. omapdss_rfbi_set_data_lines(dssdev, 8);
  128. omap_rfbi_configure(dssdev);
  129. blizzard_write(BLIZZARD_INPUT_WIN_X_START_0, tmp, 18);
  130. omapdss_rfbi_set_pixel_size(dssdev, 16);
  131. omapdss_rfbi_set_data_lines(dssdev, 16);
  132. omap_rfbi_configure(dssdev);
  133. }
  134. static void mipid_transfer(struct spi_device *spi, int cmd, const u8 *wbuf,
  135. int wlen, u8 *rbuf, int rlen)
  136. {
  137. struct spi_message m;
  138. struct spi_transfer *x, xfer[4];
  139. u16 w;
  140. int r;
  141. spi_message_init(&m);
  142. memset(xfer, 0, sizeof(xfer));
  143. x = &xfer[0];
  144. cmd &= 0xff;
  145. x->tx_buf = &cmd;
  146. x->bits_per_word = 9;
  147. x->len = 2;
  148. spi_message_add_tail(x, &m);
  149. if (wlen) {
  150. x++;
  151. x->tx_buf = wbuf;
  152. x->len = wlen;
  153. x->bits_per_word = 9;
  154. spi_message_add_tail(x, &m);
  155. }
  156. if (rlen) {
  157. x++;
  158. x->rx_buf = &w;
  159. x->len = 1;
  160. spi_message_add_tail(x, &m);
  161. if (rlen > 1) {
  162. /* Arrange for the extra clock before the first
  163. * data bit.
  164. */
  165. x->bits_per_word = 9;
  166. x->len = 2;
  167. x++;
  168. x->rx_buf = &rbuf[1];
  169. x->len = rlen - 1;
  170. spi_message_add_tail(x, &m);
  171. }
  172. }
  173. r = spi_sync(spi, &m);
  174. if (r < 0)
  175. dev_dbg(&spi->dev, "spi_sync %d\n", r);
  176. if (rlen)
  177. rbuf[0] = w & 0xff;
  178. }
  179. static inline void mipid_cmd(struct spi_device *spi, int cmd)
  180. {
  181. mipid_transfer(spi, cmd, NULL, 0, NULL, 0);
  182. }
  183. static inline void mipid_write(struct spi_device *spi,
  184. int reg, const u8 *buf, int len)
  185. {
  186. mipid_transfer(spi, reg, buf, len, NULL, 0);
  187. }
  188. static inline void mipid_read(struct spi_device *spi,
  189. int reg, u8 *buf, int len)
  190. {
  191. mipid_transfer(spi, reg, NULL, 0, buf, len);
  192. }
  193. static void set_data_lines(struct spi_device *spi, int data_lines)
  194. {
  195. u16 par;
  196. switch (data_lines) {
  197. case 16:
  198. par = 0x150;
  199. break;
  200. case 18:
  201. par = 0x160;
  202. break;
  203. case 24:
  204. par = 0x170;
  205. break;
  206. }
  207. mipid_write(spi, 0x3a, (u8 *)&par, 2);
  208. }
  209. static void send_init_string(struct spi_device *spi)
  210. {
  211. u16 initpar[] = { 0x0102, 0x0100, 0x0100 };
  212. mipid_write(spi, 0xc2, (u8 *)initpar, sizeof(initpar));
  213. }
  214. static void send_display_on(struct spi_device *spi)
  215. {
  216. mipid_cmd(spi, MIPID_CMD_DISP_ON);
  217. }
  218. static void send_display_off(struct spi_device *spi)
  219. {
  220. mipid_cmd(spi, MIPID_CMD_DISP_OFF);
  221. }
  222. static void send_sleep_out(struct spi_device *spi)
  223. {
  224. mipid_cmd(spi, MIPID_CMD_SLEEP_OUT);
  225. msleep(120);
  226. }
  227. static void send_sleep_in(struct spi_device *spi)
  228. {
  229. mipid_cmd(spi, MIPID_CMD_SLEEP_IN);
  230. msleep(50);
  231. }
  232. static int n8x0_panel_power_on(struct omap_dss_device *dssdev)
  233. {
  234. int r;
  235. struct panel_n8x0_data *bdata = get_board_data(dssdev);
  236. struct panel_drv_data *ddata = get_drv_data(dssdev);
  237. struct spi_device *spi = ddata->spidev;
  238. u8 rev, conf;
  239. u8 display_id[3];
  240. const char *panel_name;
  241. if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE)
  242. return 0;
  243. gpio_direction_output(bdata->ctrl_pwrdown, 1);
  244. omapdss_rfbi_set_size(dssdev, dssdev->panel.timings.x_res,
  245. dssdev->panel.timings.y_res);
  246. omapdss_rfbi_set_pixel_size(dssdev, dssdev->ctrl.pixel_size);
  247. omapdss_rfbi_set_data_lines(dssdev, dssdev->phy.rfbi.data_lines);
  248. omapdss_rfbi_set_interface_timings(dssdev, &dssdev->ctrl.rfbi_timings);
  249. r = omapdss_rfbi_display_enable(dssdev);
  250. if (r)
  251. goto err_rfbi_en;
  252. rev = blizzard_read_reg(BLIZZARD_REV_CODE);
  253. conf = blizzard_read_reg(BLIZZARD_CONFIG);
  254. switch (rev & 0xfc) {
  255. case 0x9c:
  256. ddata->blizzard_ver = BLIZZARD_VERSION_S1D13744;
  257. dev_info(&dssdev->dev, "s1d13744 LCD controller rev %d "
  258. "initialized (CNF pins %x)\n", rev & 0x03, conf & 0x07);
  259. break;
  260. case 0xa4:
  261. ddata->blizzard_ver = BLIZZARD_VERSION_S1D13745;
  262. dev_info(&dssdev->dev, "s1d13745 LCD controller rev %d "
  263. "initialized (CNF pins %x)\n", rev & 0x03, conf & 0x07);
  264. break;
  265. default:
  266. dev_err(&dssdev->dev, "invalid s1d1374x revision %02x\n", rev);
  267. r = -ENODEV;
  268. goto err_inv_chip;
  269. }
  270. /* panel */
  271. gpio_direction_output(bdata->panel_reset, 1);
  272. mipid_read(spi, MIPID_CMD_READ_DISP_ID, display_id, 3);
  273. dev_dbg(&spi->dev, "MIPI display ID: %02x%02x%02x\n",
  274. display_id[0], display_id[1], display_id[2]);
  275. switch (display_id[0]) {
  276. case 0x45:
  277. panel_name = "lph8923";
  278. break;
  279. case 0x83:
  280. panel_name = "ls041y3";
  281. break;
  282. default:
  283. dev_err(&dssdev->dev, "invalid display ID 0x%x\n",
  284. display_id[0]);
  285. r = -ENODEV;
  286. goto err_inv_panel;
  287. }
  288. dev_info(&dssdev->dev, "%s rev %02x LCD detected\n",
  289. panel_name, display_id[1]);
  290. send_sleep_out(spi);
  291. send_init_string(spi);
  292. set_data_lines(spi, 24);
  293. send_display_on(spi);
  294. return 0;
  295. err_inv_panel:
  296. /*
  297. * HACK: we should turn off the panel here, but there is some problem
  298. * with the initialization sequence, and we fail to init the panel if we
  299. * have turned it off
  300. */
  301. /* gpio_direction_output(bdata->panel_reset, 0); */
  302. err_inv_chip:
  303. omapdss_rfbi_display_disable(dssdev);
  304. err_rfbi_en:
  305. gpio_direction_output(bdata->ctrl_pwrdown, 0);
  306. return r;
  307. }
  308. static void n8x0_panel_power_off(struct omap_dss_device *dssdev)
  309. {
  310. struct panel_n8x0_data *bdata = get_board_data(dssdev);
  311. struct panel_drv_data *ddata = get_drv_data(dssdev);
  312. struct spi_device *spi = ddata->spidev;
  313. if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE)
  314. return;
  315. send_display_off(spi);
  316. send_sleep_in(spi);
  317. /*
  318. * HACK: we should turn off the panel here, but there is some problem
  319. * with the initialization sequence, and we fail to init the panel if we
  320. * have turned it off
  321. */
  322. /* gpio_direction_output(bdata->panel_reset, 0); */
  323. gpio_direction_output(bdata->ctrl_pwrdown, 0);
  324. omapdss_rfbi_display_disable(dssdev);
  325. }
  326. static const struct rfbi_timings n8x0_panel_timings = {
  327. .cs_on_time = 0,
  328. .we_on_time = 9000,
  329. .we_off_time = 18000,
  330. .we_cycle_time = 36000,
  331. .re_on_time = 9000,
  332. .re_off_time = 27000,
  333. .re_cycle_time = 36000,
  334. .access_time = 27000,
  335. .cs_off_time = 36000,
  336. .cs_pulse_width = 0,
  337. };
  338. static int n8x0_panel_probe(struct omap_dss_device *dssdev)
  339. {
  340. struct panel_n8x0_data *bdata = get_board_data(dssdev);
  341. struct panel_drv_data *ddata;
  342. int r;
  343. dev_dbg(&dssdev->dev, "probe\n");
  344. if (!bdata)
  345. return -EINVAL;
  346. s_drv_data.dssdev = dssdev;
  347. ddata = &s_drv_data;
  348. mutex_init(&ddata->lock);
  349. dssdev->panel.timings.x_res = 800;
  350. dssdev->panel.timings.y_res = 480;
  351. dssdev->ctrl.pixel_size = 16;
  352. dssdev->ctrl.rfbi_timings = n8x0_panel_timings;
  353. dssdev->caps = OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE;
  354. if (gpio_is_valid(bdata->panel_reset)) {
  355. r = devm_gpio_request_one(&dssdev->dev, bdata->panel_reset,
  356. GPIOF_OUT_INIT_LOW, "PANEL RESET");
  357. if (r)
  358. return r;
  359. }
  360. if (gpio_is_valid(bdata->ctrl_pwrdown)) {
  361. r = devm_gpio_request_one(&dssdev->dev, bdata->ctrl_pwrdown,
  362. GPIOF_OUT_INIT_LOW, "PANEL PWRDOWN");
  363. if (r)
  364. return r;
  365. }
  366. return 0;
  367. }
  368. static void n8x0_panel_remove(struct omap_dss_device *dssdev)
  369. {
  370. dev_dbg(&dssdev->dev, "remove\n");
  371. dev_set_drvdata(&dssdev->dev, NULL);
  372. }
  373. static int n8x0_panel_enable(struct omap_dss_device *dssdev)
  374. {
  375. struct panel_drv_data *ddata = get_drv_data(dssdev);
  376. int r;
  377. dev_dbg(&dssdev->dev, "enable\n");
  378. mutex_lock(&ddata->lock);
  379. rfbi_bus_lock();
  380. r = n8x0_panel_power_on(dssdev);
  381. rfbi_bus_unlock();
  382. if (r) {
  383. mutex_unlock(&ddata->lock);
  384. return r;
  385. }
  386. dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
  387. mutex_unlock(&ddata->lock);
  388. return 0;
  389. }
  390. static void n8x0_panel_disable(struct omap_dss_device *dssdev)
  391. {
  392. struct panel_drv_data *ddata = get_drv_data(dssdev);
  393. dev_dbg(&dssdev->dev, "disable\n");
  394. mutex_lock(&ddata->lock);
  395. rfbi_bus_lock();
  396. n8x0_panel_power_off(dssdev);
  397. rfbi_bus_unlock();
  398. dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
  399. mutex_unlock(&ddata->lock);
  400. }
  401. static void n8x0_panel_get_resolution(struct omap_dss_device *dssdev,
  402. u16 *xres, u16 *yres)
  403. {
  404. *xres = dssdev->panel.timings.x_res;
  405. *yres = dssdev->panel.timings.y_res;
  406. }
  407. static void update_done(void *data)
  408. {
  409. rfbi_bus_unlock();
  410. }
  411. static int n8x0_panel_update(struct omap_dss_device *dssdev,
  412. u16 x, u16 y, u16 w, u16 h)
  413. {
  414. struct panel_drv_data *ddata = get_drv_data(dssdev);
  415. u16 dw, dh;
  416. dev_dbg(&dssdev->dev, "update\n");
  417. dw = dssdev->panel.timings.x_res;
  418. dh = dssdev->panel.timings.y_res;
  419. if (x != 0 || y != 0 || w != dw || h != dh) {
  420. dev_err(&dssdev->dev, "invaid update region %d, %d, %d, %d\n",
  421. x, y, w, h);
  422. return -EINVAL;
  423. }
  424. mutex_lock(&ddata->lock);
  425. rfbi_bus_lock();
  426. blizzard_ctrl_setup_update(dssdev, x, y, w, h);
  427. omap_rfbi_update(dssdev, update_done, NULL);
  428. mutex_unlock(&ddata->lock);
  429. return 0;
  430. }
  431. static int n8x0_panel_sync(struct omap_dss_device *dssdev)
  432. {
  433. struct panel_drv_data *ddata = get_drv_data(dssdev);
  434. dev_dbg(&dssdev->dev, "sync\n");
  435. mutex_lock(&ddata->lock);
  436. rfbi_bus_lock();
  437. rfbi_bus_unlock();
  438. mutex_unlock(&ddata->lock);
  439. return 0;
  440. }
  441. static struct omap_dss_driver n8x0_panel_driver = {
  442. .probe = n8x0_panel_probe,
  443. .remove = n8x0_panel_remove,
  444. .enable = n8x0_panel_enable,
  445. .disable = n8x0_panel_disable,
  446. .update = n8x0_panel_update,
  447. .sync = n8x0_panel_sync,
  448. .get_resolution = n8x0_panel_get_resolution,
  449. .get_recommended_bpp = omapdss_default_get_recommended_bpp,
  450. .driver = {
  451. .name = "n8x0_panel",
  452. .owner = THIS_MODULE,
  453. },
  454. };
  455. /* PANEL */
  456. static int mipid_spi_probe(struct spi_device *spi)
  457. {
  458. int r;
  459. dev_dbg(&spi->dev, "mipid_spi_probe\n");
  460. spi->mode = SPI_MODE_0;
  461. s_drv_data.spidev = spi;
  462. r = omap_dss_register_driver(&n8x0_panel_driver);
  463. if (r)
  464. pr_err("n8x0_panel: dss driver registration failed\n");
  465. return r;
  466. }
  467. static int mipid_spi_remove(struct spi_device *spi)
  468. {
  469. dev_dbg(&spi->dev, "mipid_spi_remove\n");
  470. omap_dss_unregister_driver(&n8x0_panel_driver);
  471. return 0;
  472. }
  473. static struct spi_driver mipid_spi_driver = {
  474. .driver = {
  475. .name = "lcd_mipid",
  476. .owner = THIS_MODULE,
  477. },
  478. .probe = mipid_spi_probe,
  479. .remove = mipid_spi_remove,
  480. };
  481. module_spi_driver(mipid_spi_driver);
  482. MODULE_LICENSE("GPL");