2 * File: drivers/video/omap/lcd_mipid.c
4 * LCD driver for MIPI DBI-C / DCS compatible LCDs
6 * Copyright (C) 2006 Nokia Corporation
7 * Author: Imre Deak <imre.deak@nokia.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include <linux/device.h>
24 #include <linux/delay.h>
25 #include <linux/spi/spi.h>
27 #include <asm/arch/omapfb.h>
28 #include <asm/arch/lcd_mipid.h>
30 #include "../../cbus/tahvo.h"
32 #define MIPID_MODULE_NAME "lcd_mipid"
34 #define MIPID_CMD_READ_DISP_ID 0x04
35 #define MIPID_CMD_READ_RED 0x06
36 #define MIPID_CMD_READ_GREEN 0x07
37 #define MIPID_CMD_READ_BLUE 0x08
38 #define MIPID_CMD_READ_DISP_STATUS 0x09
39 #define MIPID_CMD_RDDSDR 0x0F
40 #define MIPID_CMD_SLEEP_IN 0x10
41 #define MIPID_CMD_SLEEP_OUT 0x11
42 #define MIPID_CMD_DISP_OFF 0x28
43 #define MIPID_CMD_DISP_ON 0x29
45 #define MIPID_VER_LPH8923 3
46 #define MIPID_VER_LS041Y3 4
48 #define MIPID_ESD_CHECK_PERIOD msecs_to_jiffies(5000)
50 #define to_mipid_device(p) container_of(p, struct mipid_device, \
57 unsigned int saved_bklight_level;
58 unsigned long hw_guard_end; /* next value of jiffies
60 next sleep in/out command */
61 unsigned long hw_guard_wait; /* max guard time in jiffies */
63 struct omapfb_device *fbdev;
64 struct spi_device *spi;
66 struct lcd_panel panel;
68 struct work_struct esd_work;
69 void (*esd_check)(struct mipid_device *m);
72 static void mipid_transfer(struct mipid_device *md, int cmd, const u8 *wbuf,
73 int wlen, u8 *rbuf, int rlen)
76 struct spi_transfer *x, xfer[4];
80 BUG_ON(md->spi == NULL);
84 memset(xfer, 0, sizeof(xfer));
91 spi_message_add_tail(x, &m);
98 spi_message_add_tail(x, &m);
105 spi_message_add_tail(x, &m);
108 /* Arrange for the extra clock before the first
111 x->bits_per_word = 9;
115 x->rx_buf = &rbuf[1];
117 spi_message_add_tail(x, &m);
121 r = spi_sync(md->spi, &m);
123 dev_dbg(md->spi->dev, "spi_sync %d\n", r);
129 static inline void mipid_cmd(struct mipid_device *md, int cmd)
131 mipid_transfer(md, cmd, NULL, 0, NULL, 0);
134 static inline void mipid_write(struct mipid_device *md,
135 int reg, const u8 *buf, int len)
137 mipid_transfer(md, reg, buf, len, NULL, 0);
140 static inline void mipid_read(struct mipid_device *md,
141 int reg, u8 *buf, int len)
143 mipid_transfer(md, reg, NULL, 0, buf, len);
146 static void set_data_lines(struct mipid_device *md, int data_lines)
150 switch (data_lines) {
161 mipid_write(md, 0x3a, (u8 *)&par, 2);
164 static void send_init_string(struct mipid_device *md)
166 u16 initpar[] = { 0x0102, 0x0100, 0x0100 };
168 mipid_write(md, 0xc2, (u8 *)initpar, sizeof(initpar));
169 set_data_lines(md, md->panel.data_lines);
172 static void hw_guard_start(struct mipid_device *md, int guard_msec)
174 md->hw_guard_wait = msecs_to_jiffies(guard_msec);
175 md->hw_guard_end = jiffies + md->hw_guard_wait;
178 static void hw_guard_wait(struct mipid_device *md)
180 unsigned long wait = md->hw_guard_end - jiffies;
182 if ((long)wait > 0 && wait <= md->hw_guard_wait) {
183 set_current_state(TASK_UNINTERRUPTIBLE);
184 schedule_timeout(wait);
188 static void set_sleep_mode(struct mipid_device *md, int on)
190 int cmd, sleep_time = 5;
193 cmd = MIPID_CMD_SLEEP_IN;
195 cmd = MIPID_CMD_SLEEP_OUT;
198 hw_guard_start(md, 120);
199 /* When we enable the panel, it seems we _have_ to sleep
200 * 120 ms before sending the init string */
206 static void set_display_state(struct mipid_device *md, int enabled)
208 int cmd = enabled ? MIPID_CMD_DISP_ON : MIPID_CMD_DISP_OFF;
213 static int mipid_set_bklight_level(struct lcd_panel *panel, unsigned int level)
215 struct mipid_device *md = to_mipid_device(panel);
217 if (level > tahvo_get_max_backlight_level())
220 md->saved_bklight_level = level;
223 tahvo_set_backlight_level(level);
228 static unsigned int mipid_get_bklight_level(struct lcd_panel *panel)
230 return tahvo_get_backlight_level();
233 static unsigned int mipid_get_bklight_max(struct lcd_panel *panel)
235 return tahvo_get_max_backlight_level();
239 static unsigned long mipid_get_caps(struct lcd_panel *panel)
241 return OMAPFB_CAPS_SET_BACKLIGHT;
244 static u16 read_first_pixel(struct mipid_device *md)
249 mutex_lock(&md->mutex);
250 mipid_read(md, MIPID_CMD_READ_RED, &red, 1);
251 mipid_read(md, MIPID_CMD_READ_GREEN, &green, 1);
252 mipid_read(md, MIPID_CMD_READ_BLUE, &blue, 1);
253 mutex_unlock(&md->mutex);
255 switch (md->panel.data_lines) {
257 pixel = ((red >> 1) << 11) | (green << 5) | (blue >> 1);
260 /* 24 bit -> 16 bit */
261 pixel = ((red >> 3) << 11) | ((green >> 2) << 5) |
271 static int mipid_run_test(struct lcd_panel *panel, int test_num)
273 struct mipid_device *md = to_mipid_device(panel);
274 static const u16 test_values[4] = {
275 0x0000, 0xffff, 0xaaaa, 0x5555,
279 if (test_num != MIPID_TEST_RGB_LINES)
280 return MIPID_TEST_INVALID;
282 for (i = 0; i < ARRAY_SIZE(test_values); i++) {
286 omapfb_write_first_pixel(md->fbdev, test_values[i]);
287 tmo = jiffies + msecs_to_jiffies(100);
293 pixel = read_first_pixel(md);
294 if (pixel == test_values[i])
296 if (time_after(jiffies, tmo)) {
297 dev_err(&md->spi->dev,
298 "MIPI LCD RGB I/F test failed: "
299 "expecting %04x, got %04x\n",
300 test_values[i], pixel);
301 return MIPID_TEST_FAILED;
310 static void ls041y3_esd_recover(struct mipid_device *md)
312 dev_err(&md->spi->dev, "performing LCD ESD recovery\n");
313 set_sleep_mode(md, 1);
314 set_sleep_mode(md, 0);
317 static void ls041y3_esd_check_mode1(struct mipid_device *md)
321 mipid_read(md, MIPID_CMD_RDDSDR, &state1, 1);
322 set_sleep_mode(md, 0);
323 mipid_read(md, MIPID_CMD_RDDSDR, &state2, 1);
324 dev_dbg(&md->spi->dev, "ESD mode 1 state1 %02x state2 %02x\n",
326 /* Each sleep out command will trigger a self diagnostic and flip
327 * Bit6 if the test passes.
329 if (!((state1 ^ state2) & (1 << 6)))
330 ls041y3_esd_recover(md);
333 static void ls041y3_esd_check_mode2(struct mipid_device *md)
337 static const struct {
341 } *rd, rd_ctrl[7] = {
342 { 0xb0, 4, { 0x0101, 0x01fe, } },
343 { 0xb1, 4, { 0x01de, 0x0121, } },
344 { 0xc2, 4, { 0x0100, 0x0100, } },
345 { 0xbd, 2, { 0x0100, } },
346 { 0xc2, 4, { 0x01fc, 0x0103, } },
352 for (i = 0; i < 3; i++, rd++)
353 mipid_write(md, rd->cmd, (u8 *)rd->wbuf, rd->wlen);
356 mipid_read(md, rd->cmd, rbuf, 2);
359 for (i = 0; i < 3; i++, rd++) {
361 mipid_write(md, rd->cmd, (u8 *)rd->wbuf, rd->wlen);
364 dev_dbg(&md->spi->dev, "ESD mode 2 state %02x\n", rbuf[1]);
366 ls041y3_esd_recover(md);
369 static void ls041y3_esd_check(struct mipid_device *md)
371 ls041y3_esd_check_mode1(md);
372 if (md->revision >= 0x88)
373 ls041y3_esd_check_mode2(md);
376 static void mipid_esd_start_check(struct mipid_device *md)
378 if (md->esd_check != NULL)
379 schedule_delayed_work(&md->esd_work, MIPID_ESD_CHECK_PERIOD);
382 static void mipid_esd_stop_check(struct mipid_device *md)
384 cancel_rearming_delayed_work(&md->esd_work);
387 static void mipid_esd_work(void *data)
389 struct mipid_device *md = data;
391 mutex_lock(&md->mutex);
393 mutex_unlock(&md->mutex);
394 mipid_esd_start_check(md);
397 static int mipid_enable(struct lcd_panel *panel)
399 struct mipid_device *md = to_mipid_device(panel);
401 mutex_lock(&md->mutex);
404 mutex_unlock(&md->mutex);
407 set_sleep_mode(md, 0);
409 send_init_string(md);
410 set_display_state(md, 1);
411 mipid_set_bklight_level(panel, md->saved_bklight_level);
412 mipid_esd_start_check(md);
414 mutex_unlock(&md->mutex);
418 static void mipid_disable(struct lcd_panel *panel)
420 struct mipid_device *md = to_mipid_device(panel);
422 /* A final ESD work might be called before returning,
423 * so do this without holding the lock. */
424 mipid_esd_stop_check(md);
425 mutex_lock(&md->mutex);
428 mutex_unlock(&md->mutex);
431 md->saved_bklight_level = mipid_get_bklight_level(panel);
432 mipid_set_bklight_level(panel, 0);
433 set_display_state(md, 0);
434 set_sleep_mode(md, 1);
437 mutex_unlock(&md->mutex);
440 static int panel_enabled(struct mipid_device *md)
445 mipid_read(md, MIPID_CMD_READ_DISP_STATUS, (u8 *)&disp_status, 4);
446 disp_status = __be32_to_cpu(disp_status);
447 enabled = (disp_status & (1 << 17)) && (disp_status & (1 << 10));
448 dev_dbg(&md->spi->dev,
449 "LCD panel %s enabled by bootloader (status 0x%04x)\n",
450 enabled ? "" : "not ", disp_status);
454 static int mipid_init(struct lcd_panel *panel,
455 struct omapfb_device *fbdev)
457 struct mipid_device *md = to_mipid_device(panel);
460 INIT_WORK(&md->esd_work, mipid_esd_work, md);
461 mutex_init(&md->mutex);
463 md->enabled = panel_enabled(md);
466 mipid_esd_start_check(md);
468 md->saved_bklight_level = mipid_get_bklight_level(panel);
473 static void mipid_cleanup(struct lcd_panel *panel)
475 struct mipid_device *md = to_mipid_device(panel);
477 mipid_esd_stop_check(md);
480 static struct lcd_panel mipid_panel = {
481 .config = OMAP_LCDC_PANEL_TFT,
486 .pixel_clock = 21940,
495 .cleanup = mipid_cleanup,
496 .enable = mipid_enable,
497 .disable = mipid_disable,
498 .get_caps = mipid_get_caps,
499 .set_bklight_level= mipid_set_bklight_level,
500 .get_bklight_level= mipid_get_bklight_level,
501 .get_bklight_max= mipid_get_bklight_max,
502 .run_test = mipid_run_test,
505 static int mipid_detect(struct mipid_device *md)
507 struct mipid_platform_data *pdata;
509 pdata = md->spi->dev.platform_data;
511 dev_err(&md->spi->dev, "missing platform data\n");
515 mipid_read(md, MIPID_CMD_READ_DISP_ID, md->display_id, 3);
516 dev_dbg(&md->spi->dev, "MIPI display ID: %02x%02x%02x\n",
517 md->display_id[0], md->display_id[1], md->display_id[2]);
519 switch (md->display_id[0]) {
521 md->model = MIPID_VER_LPH8923;
522 md->panel.name = "lph8923";
525 md->model = MIPID_VER_LS041Y3;
526 md->panel.name = "ls041y3";
527 md->esd_check = ls041y3_esd_check;
530 md->panel.name = "unknown";
531 dev_err(&md->spi->dev, "invalid display ID\n");
535 md->revision = md->display_id[1];
536 md->panel.data_lines = pdata->data_lines;
537 pr_info("omapfb: %s rev %02x LCD detected\n",
538 md->panel.name, md->revision);
543 static int mipid_spi_probe(struct spi_device *spi)
545 struct mipid_device *md;
548 md = kzalloc(sizeof(*md), SLAB_KERNEL);
550 dev_err(&md->spi->dev, "out of memory\n");
554 spi->mode = SPI_MODE_1;
556 dev_set_drvdata(&spi->dev, md);
557 md->panel = mipid_panel;
559 r = mipid_detect(md);
563 omapfb_register_panel(&md->panel);
568 static int mipid_spi_remove(struct spi_device *spi)
570 struct mipid_device *md = dev_get_drvdata(&spi->dev);
572 mipid_disable(&md->panel);
578 static struct spi_driver mipid_spi_driver = {
580 .name = MIPID_MODULE_NAME,
581 .bus = &spi_bus_type,
582 .owner = THIS_MODULE,
584 .probe = mipid_spi_probe,
585 .remove = __devexit_p(mipid_spi_remove),
588 static int mipid_drv_init(void)
590 spi_register_driver(&mipid_spi_driver);
594 module_init(mipid_drv_init);
596 static void mipid_drv_cleanup(void)
598 spi_unregister_driver(&mipid_spi_driver);
600 module_exit(mipid_drv_cleanup);
602 MODULE_DESCRIPTION("MIPI display driver");
603 MODULE_LICENSE("GPL");