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/workqueue.h>
26 #include <linux/spi/spi.h>
28 #include <asm/arch/omapfb.h>
29 #include <asm/arch/lcd_mipid.h>
31 #include "../../cbus/tahvo.h"
33 #define MIPID_MODULE_NAME "lcd_mipid"
35 #define MIPID_CMD_READ_DISP_ID 0x04
36 #define MIPID_CMD_READ_RED 0x06
37 #define MIPID_CMD_READ_GREEN 0x07
38 #define MIPID_CMD_READ_BLUE 0x08
39 #define MIPID_CMD_READ_DISP_STATUS 0x09
40 #define MIPID_CMD_RDDSDR 0x0F
41 #define MIPID_CMD_SLEEP_IN 0x10
42 #define MIPID_CMD_SLEEP_OUT 0x11
43 #define MIPID_CMD_DISP_OFF 0x28
44 #define MIPID_CMD_DISP_ON 0x29
46 #define MIPID_VER_LPH8923 3
47 #define MIPID_VER_LS041Y3 4
49 #define MIPID_ESD_CHECK_PERIOD msecs_to_jiffies(5000)
51 #define to_mipid_device(p) container_of(p, struct mipid_device, \
58 unsigned int saved_bklight_level;
59 unsigned long hw_guard_end; /* next value of jiffies
61 next sleep in/out command */
62 unsigned long hw_guard_wait; /* max guard time in jiffies */
64 struct omapfb_device *fbdev;
65 struct spi_device *spi;
67 struct lcd_panel panel;
69 struct workqueue_struct *esd_wq;
70 struct delayed_work esd_work;
71 void (*esd_check)(struct mipid_device *m);
74 static void mipid_transfer(struct mipid_device *md, int cmd, const u8 *wbuf,
75 int wlen, u8 *rbuf, int rlen)
78 struct spi_transfer *x, xfer[4];
82 BUG_ON(md->spi == NULL);
86 memset(xfer, 0, sizeof(xfer));
93 spi_message_add_tail(x, &m);
100 spi_message_add_tail(x, &m);
107 spi_message_add_tail(x, &m);
110 /* Arrange for the extra clock before the first
113 x->bits_per_word = 9;
117 x->rx_buf = &rbuf[1];
119 spi_message_add_tail(x, &m);
123 r = spi_sync(md->spi, &m);
125 dev_dbg(&md->spi->dev, "spi_sync %d\n", r);
131 static inline void mipid_cmd(struct mipid_device *md, int cmd)
133 mipid_transfer(md, cmd, NULL, 0, NULL, 0);
136 static inline void mipid_write(struct mipid_device *md,
137 int reg, const u8 *buf, int len)
139 mipid_transfer(md, reg, buf, len, NULL, 0);
142 static inline void mipid_read(struct mipid_device *md,
143 int reg, u8 *buf, int len)
145 mipid_transfer(md, reg, NULL, 0, buf, len);
148 static void set_data_lines(struct mipid_device *md, int data_lines)
152 switch (data_lines) {
163 mipid_write(md, 0x3a, (u8 *)&par, 2);
166 static void send_init_string(struct mipid_device *md)
168 u16 initpar[] = { 0x0102, 0x0100, 0x0100 };
170 mipid_write(md, 0xc2, (u8 *)initpar, sizeof(initpar));
171 set_data_lines(md, md->panel.data_lines);
174 static void hw_guard_start(struct mipid_device *md, int guard_msec)
176 md->hw_guard_wait = msecs_to_jiffies(guard_msec);
177 md->hw_guard_end = jiffies + md->hw_guard_wait;
180 static void hw_guard_wait(struct mipid_device *md)
182 unsigned long wait = md->hw_guard_end - jiffies;
184 if ((long)wait > 0 && wait <= md->hw_guard_wait) {
185 set_current_state(TASK_UNINTERRUPTIBLE);
186 schedule_timeout(wait);
190 static void set_sleep_mode(struct mipid_device *md, int on)
192 int cmd, sleep_time = 50;
195 cmd = MIPID_CMD_SLEEP_IN;
197 cmd = MIPID_CMD_SLEEP_OUT;
200 hw_guard_start(md, 120);
201 /* When we enable the panel, it seems we _have_ to sleep
202 * 120 ms before sending the init string. When disabling the
203 * panel we'll sleep for the duration of 2 frames, so that the
204 * controller can still provide the PCLK,HS,VS signals. */
210 static void set_display_state(struct mipid_device *md, int enabled)
212 int cmd = enabled ? MIPID_CMD_DISP_ON : MIPID_CMD_DISP_OFF;
217 static int mipid_set_bklight_level(struct lcd_panel *panel, unsigned int level)
219 struct mipid_device *md = to_mipid_device(panel);
221 if (level > tahvo_get_max_backlight_level())
224 md->saved_bklight_level = level;
227 tahvo_set_backlight_level(level);
232 static unsigned int mipid_get_bklight_level(struct lcd_panel *panel)
234 return tahvo_get_backlight_level();
237 static unsigned int mipid_get_bklight_max(struct lcd_panel *panel)
239 return tahvo_get_max_backlight_level();
243 static unsigned long mipid_get_caps(struct lcd_panel *panel)
245 return OMAPFB_CAPS_SET_BACKLIGHT;
248 static u16 read_first_pixel(struct mipid_device *md)
253 mutex_lock(&md->mutex);
254 mipid_read(md, MIPID_CMD_READ_RED, &red, 1);
255 mipid_read(md, MIPID_CMD_READ_GREEN, &green, 1);
256 mipid_read(md, MIPID_CMD_READ_BLUE, &blue, 1);
257 mutex_unlock(&md->mutex);
259 switch (md->panel.data_lines) {
261 pixel = ((red >> 1) << 11) | (green << 5) | (blue >> 1);
264 /* 24 bit -> 16 bit */
265 pixel = ((red >> 3) << 11) | ((green >> 2) << 5) |
275 static int mipid_run_test(struct lcd_panel *panel, int test_num)
277 struct mipid_device *md = to_mipid_device(panel);
278 static const u16 test_values[4] = {
279 0x0000, 0xffff, 0xaaaa, 0x5555,
283 if (test_num != MIPID_TEST_RGB_LINES)
284 return MIPID_TEST_INVALID;
286 for (i = 0; i < ARRAY_SIZE(test_values); i++) {
290 omapfb_write_first_pixel(md->fbdev, test_values[i]);
291 tmo = jiffies + msecs_to_jiffies(100);
297 pixel = read_first_pixel(md);
298 if (pixel == test_values[i])
300 if (time_after(jiffies, tmo)) {
301 dev_err(&md->spi->dev,
302 "MIPI LCD RGB I/F test failed: "
303 "expecting %04x, got %04x\n",
304 test_values[i], pixel);
305 return MIPID_TEST_FAILED;
314 static void ls041y3_esd_recover(struct mipid_device *md)
316 dev_err(&md->spi->dev, "performing LCD ESD recovery\n");
317 set_sleep_mode(md, 1);
318 set_sleep_mode(md, 0);
321 static void ls041y3_esd_check_mode1(struct mipid_device *md)
325 mipid_read(md, MIPID_CMD_RDDSDR, &state1, 1);
326 set_sleep_mode(md, 0);
327 mipid_read(md, MIPID_CMD_RDDSDR, &state2, 1);
328 dev_dbg(&md->spi->dev, "ESD mode 1 state1 %02x state2 %02x\n",
330 /* Each sleep out command will trigger a self diagnostic and flip
331 * Bit6 if the test passes.
333 if (!((state1 ^ state2) & (1 << 6)))
334 ls041y3_esd_recover(md);
337 static void ls041y3_esd_check_mode2(struct mipid_device *md)
341 static const struct {
345 } *rd, rd_ctrl[7] = {
346 { 0xb0, 4, { 0x0101, 0x01fe, } },
347 { 0xb1, 4, { 0x01de, 0x0121, } },
348 { 0xc2, 4, { 0x0100, 0x0100, } },
349 { 0xbd, 2, { 0x0100, } },
350 { 0xc2, 4, { 0x01fc, 0x0103, } },
356 for (i = 0; i < 3; i++, rd++)
357 mipid_write(md, rd->cmd, (u8 *)rd->wbuf, rd->wlen);
360 mipid_read(md, rd->cmd, rbuf, 2);
363 for (i = 0; i < 3; i++, rd++) {
365 mipid_write(md, rd->cmd, (u8 *)rd->wbuf, rd->wlen);
368 dev_dbg(&md->spi->dev, "ESD mode 2 state %02x\n", rbuf[1]);
370 ls041y3_esd_recover(md);
373 static void ls041y3_esd_check(struct mipid_device *md)
375 ls041y3_esd_check_mode1(md);
376 if (md->revision >= 0x88)
377 ls041y3_esd_check_mode2(md);
380 static void mipid_esd_start_check(struct mipid_device *md)
382 if (md->esd_check != NULL)
383 queue_delayed_work(md->esd_wq, &md->esd_work,
384 MIPID_ESD_CHECK_PERIOD);
387 static void mipid_esd_stop_check(struct mipid_device *md)
389 if (md->esd_check != NULL)
390 cancel_rearming_delayed_workqueue(md->esd_wq, &md->esd_work);
393 static void mipid_esd_work(struct work_struct *work)
395 struct mipid_device *md = container_of(work, struct mipid_device, esd_work.work);
397 mutex_lock(&md->mutex);
399 mutex_unlock(&md->mutex);
400 mipid_esd_start_check(md);
403 static int mipid_enable(struct lcd_panel *panel)
405 struct mipid_device *md = to_mipid_device(panel);
407 mutex_lock(&md->mutex);
410 mutex_unlock(&md->mutex);
413 set_sleep_mode(md, 0);
415 send_init_string(md);
416 set_display_state(md, 1);
417 mipid_set_bklight_level(panel, md->saved_bklight_level);
418 mipid_esd_start_check(md);
420 mutex_unlock(&md->mutex);
424 static void mipid_disable(struct lcd_panel *panel)
426 struct mipid_device *md = to_mipid_device(panel);
428 /* A final ESD work might be called before returning,
429 * so do this without holding the lock. */
430 mipid_esd_stop_check(md);
431 mutex_lock(&md->mutex);
434 mutex_unlock(&md->mutex);
437 md->saved_bklight_level = mipid_get_bklight_level(panel);
438 mipid_set_bklight_level(panel, 0);
439 set_display_state(md, 0);
440 set_sleep_mode(md, 1);
443 mutex_unlock(&md->mutex);
446 static int panel_enabled(struct mipid_device *md)
451 mipid_read(md, MIPID_CMD_READ_DISP_STATUS, (u8 *)&disp_status, 4);
452 disp_status = __be32_to_cpu(disp_status);
453 enabled = (disp_status & (1 << 17)) && (disp_status & (1 << 10));
454 dev_dbg(&md->spi->dev,
455 "LCD panel %s enabled by bootloader (status 0x%04x)\n",
456 enabled ? "" : "not ", disp_status);
460 static int mipid_init(struct lcd_panel *panel,
461 struct omapfb_device *fbdev)
463 struct mipid_device *md = to_mipid_device(panel);
466 md->esd_wq = create_singlethread_workqueue("mipid_esd");
467 if (md->esd_wq == NULL) {
468 dev_err(&md->spi->dev, "can't create ESD workqueue\n");
471 INIT_DELAYED_WORK(&md->esd_work, mipid_esd_work);
472 mutex_init(&md->mutex);
474 md->enabled = panel_enabled(md);
477 mipid_esd_start_check(md);
479 md->saved_bklight_level = mipid_get_bklight_level(panel);
484 static void mipid_cleanup(struct lcd_panel *panel)
486 struct mipid_device *md = to_mipid_device(panel);
489 mipid_esd_stop_check(md);
490 destroy_workqueue(md->esd_wq);
493 static struct lcd_panel mipid_panel = {
494 .config = OMAP_LCDC_PANEL_TFT,
499 .pixel_clock = 21940,
508 .cleanup = mipid_cleanup,
509 .enable = mipid_enable,
510 .disable = mipid_disable,
511 .get_caps = mipid_get_caps,
512 .set_bklight_level= mipid_set_bklight_level,
513 .get_bklight_level= mipid_get_bklight_level,
514 .get_bklight_max= mipid_get_bklight_max,
515 .run_test = mipid_run_test,
518 static int mipid_detect(struct mipid_device *md)
520 struct mipid_platform_data *pdata;
522 pdata = md->spi->dev.platform_data;
524 dev_err(&md->spi->dev, "missing platform data\n");
528 mipid_read(md, MIPID_CMD_READ_DISP_ID, md->display_id, 3);
529 dev_dbg(&md->spi->dev, "MIPI display ID: %02x%02x%02x\n",
530 md->display_id[0], md->display_id[1], md->display_id[2]);
532 switch (md->display_id[0]) {
534 md->model = MIPID_VER_LPH8923;
535 md->panel.name = "lph8923";
538 md->model = MIPID_VER_LS041Y3;
539 md->panel.name = "ls041y3";
540 md->esd_check = ls041y3_esd_check;
543 md->panel.name = "unknown";
544 dev_err(&md->spi->dev, "invalid display ID\n");
548 md->revision = md->display_id[1];
549 md->panel.data_lines = pdata->data_lines;
550 pr_info("omapfb: %s rev %02x LCD detected\n",
551 md->panel.name, md->revision);
556 static int mipid_spi_probe(struct spi_device *spi)
558 struct mipid_device *md;
561 md = kzalloc(sizeof(*md), GFP_KERNEL);
563 dev_err(&md->spi->dev, "out of memory\n");
567 spi->mode = SPI_MODE_1;
569 dev_set_drvdata(&spi->dev, md);
570 md->panel = mipid_panel;
572 r = mipid_detect(md);
576 omapfb_register_panel(&md->panel);
581 static int mipid_spi_remove(struct spi_device *spi)
583 struct mipid_device *md = dev_get_drvdata(&spi->dev);
585 mipid_disable(&md->panel);
591 static struct spi_driver mipid_spi_driver = {
593 .name = MIPID_MODULE_NAME,
594 .bus = &spi_bus_type,
595 .owner = THIS_MODULE,
597 .probe = mipid_spi_probe,
598 .remove = __devexit_p(mipid_spi_remove),
601 static int mipid_drv_init(void)
603 spi_register_driver(&mipid_spi_driver);
607 module_init(mipid_drv_init);
609 static void mipid_drv_cleanup(void)
611 spi_unregister_driver(&mipid_spi_driver);
613 module_exit(mipid_drv_cleanup);
615 MODULE_DESCRIPTION("MIPI display driver");
616 MODULE_LICENSE("GPL");