1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * LCD driver for MIPI DBI-C / DCS compatible LCDs
5 * Copyright (C) 2006 Nokia Corporation
6 * Author: Imre Deak <imre.deak@nokia.com>
8 #include <linux/device.h>
9 #include <linux/delay.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/slab.h>
12 #include <linux/workqueue.h>
13 #include <linux/spi/spi.h>
14 #include <linux/module.h>
16 #include <linux/platform_data/lcd-mipid.h>
20 #define MIPID_MODULE_NAME "lcd_mipid"
22 #define MIPID_CMD_READ_DISP_ID 0x04
23 #define MIPID_CMD_READ_RED 0x06
24 #define MIPID_CMD_READ_GREEN 0x07
25 #define MIPID_CMD_READ_BLUE 0x08
26 #define MIPID_CMD_READ_DISP_STATUS 0x09
27 #define MIPID_CMD_RDDSDR 0x0F
28 #define MIPID_CMD_SLEEP_IN 0x10
29 #define MIPID_CMD_SLEEP_OUT 0x11
30 #define MIPID_CMD_DISP_OFF 0x28
31 #define MIPID_CMD_DISP_ON 0x29
33 #define MIPID_ESD_CHECK_PERIOD msecs_to_jiffies(5000)
35 #define to_mipid_device(p) container_of(p, struct mipid_device, \
40 unsigned int saved_bklight_level
;
41 unsigned long hw_guard_end
; /* next value of jiffies
43 next sleep in/out command */
44 unsigned long hw_guard_wait
; /* max guard time in jiffies */
45 struct gpio_desc
*reset
;
47 struct omapfb_device
*fbdev
;
48 struct spi_device
*spi
;
50 struct lcd_panel panel
;
52 struct delayed_work esd_work
;
53 void (*esd_check
)(struct mipid_device
*m
);
56 static void mipid_transfer(struct mipid_device
*md
, int cmd
, const u8
*wbuf
,
57 int wlen
, u8
*rbuf
, int rlen
)
60 struct spi_transfer
*x
, xfer
[4];
64 BUG_ON(md
->spi
== NULL
);
68 memset(xfer
, 0, sizeof(xfer
));
75 spi_message_add_tail(x
, &m
);
82 spi_message_add_tail(x
, &m
);
89 spi_message_add_tail(x
, &m
);
92 /* Arrange for the extra clock before the first
101 spi_message_add_tail(x
, &m
);
105 r
= spi_sync(md
->spi
, &m
);
107 dev_dbg(&md
->spi
->dev
, "spi_sync %d\n", r
);
113 static inline void mipid_cmd(struct mipid_device
*md
, int cmd
)
115 mipid_transfer(md
, cmd
, NULL
, 0, NULL
, 0);
118 static inline void mipid_write(struct mipid_device
*md
,
119 int reg
, const u8
*buf
, int len
)
121 mipid_transfer(md
, reg
, buf
, len
, NULL
, 0);
124 static inline void mipid_read(struct mipid_device
*md
,
125 int reg
, u8
*buf
, int len
)
127 mipid_transfer(md
, reg
, NULL
, 0, buf
, len
);
130 static void set_data_lines(struct mipid_device
*md
, int data_lines
)
134 switch (data_lines
) {
145 mipid_write(md
, 0x3a, (u8
*)&par
, 2);
148 static void send_init_string(struct mipid_device
*md
)
150 u16 initpar
[] = { 0x0102, 0x0100, 0x0100 };
152 mipid_write(md
, 0xc2, (u8
*)initpar
, sizeof(initpar
));
153 set_data_lines(md
, md
->panel
.data_lines
);
156 static void hw_guard_start(struct mipid_device
*md
, int guard_msec
)
158 md
->hw_guard_wait
= msecs_to_jiffies(guard_msec
);
159 md
->hw_guard_end
= jiffies
+ md
->hw_guard_wait
;
162 static void hw_guard_wait(struct mipid_device
*md
)
164 unsigned long wait
= md
->hw_guard_end
- jiffies
;
166 if ((long)wait
> 0 && time_before_eq(wait
, md
->hw_guard_wait
)) {
167 set_current_state(TASK_UNINTERRUPTIBLE
);
168 schedule_timeout(wait
);
172 static void set_sleep_mode(struct mipid_device
*md
, int on
)
174 int cmd
, sleep_time
= 50;
177 cmd
= MIPID_CMD_SLEEP_IN
;
179 cmd
= MIPID_CMD_SLEEP_OUT
;
182 hw_guard_start(md
, 120);
184 * When we enable the panel, it seems we _have_ to sleep
185 * 120 ms before sending the init string. When disabling the
186 * panel we'll sleep for the duration of 2 frames, so that the
187 * controller can still provide the PCLK,HS,VS signals.
194 static void set_display_state(struct mipid_device
*md
, int enabled
)
196 int cmd
= enabled
? MIPID_CMD_DISP_ON
: MIPID_CMD_DISP_OFF
;
201 static int mipid_set_bklight_level(struct lcd_panel
*panel
, unsigned int level
)
203 struct mipid_device
*md
= to_mipid_device(panel
);
204 struct mipid_platform_data
*pd
= md
->spi
->dev
.platform_data
;
206 if (pd
->get_bklight_max
== NULL
|| pd
->set_bklight_level
== NULL
)
208 if (level
> pd
->get_bklight_max(pd
))
211 md
->saved_bklight_level
= level
;
214 pd
->set_bklight_level(pd
, level
);
219 static unsigned int mipid_get_bklight_level(struct lcd_panel
*panel
)
221 struct mipid_device
*md
= to_mipid_device(panel
);
222 struct mipid_platform_data
*pd
= md
->spi
->dev
.platform_data
;
224 if (pd
->get_bklight_level
== NULL
)
226 return pd
->get_bklight_level(pd
);
229 static unsigned int mipid_get_bklight_max(struct lcd_panel
*panel
)
231 struct mipid_device
*md
= to_mipid_device(panel
);
232 struct mipid_platform_data
*pd
= md
->spi
->dev
.platform_data
;
234 if (pd
->get_bklight_max
== NULL
)
237 return pd
->get_bklight_max(pd
);
240 static unsigned long mipid_get_caps(struct lcd_panel
*panel
)
242 return OMAPFB_CAPS_SET_BACKLIGHT
;
245 static u16
read_first_pixel(struct mipid_device
*md
)
250 mutex_lock(&md
->mutex
);
251 mipid_read(md
, MIPID_CMD_READ_RED
, &red
, 1);
252 mipid_read(md
, MIPID_CMD_READ_GREEN
, &green
, 1);
253 mipid_read(md
, MIPID_CMD_READ_BLUE
, &blue
, 1);
254 mutex_unlock(&md
->mutex
);
256 switch (md
->panel
.data_lines
) {
258 pixel
= ((red
>> 1) << 11) | (green
<< 5) | (blue
>> 1);
261 /* 24 bit -> 16 bit */
262 pixel
= ((red
>> 3) << 11) | ((green
>> 2) << 5) |
273 static int mipid_run_test(struct lcd_panel
*panel
, int test_num
)
275 struct mipid_device
*md
= to_mipid_device(panel
);
276 static const u16 test_values
[4] = {
277 0x0000, 0xffff, 0xaaaa, 0x5555,
281 if (test_num
!= MIPID_TEST_RGB_LINES
)
282 return MIPID_TEST_INVALID
;
284 for (i
= 0; i
< ARRAY_SIZE(test_values
); i
++) {
288 omapfb_write_first_pixel(md
->fbdev
, test_values
[i
]);
289 tmo
= jiffies
+ msecs_to_jiffies(100);
295 pixel
= read_first_pixel(md
);
296 if (pixel
== test_values
[i
])
298 if (time_after(jiffies
, tmo
)) {
299 dev_err(&md
->spi
->dev
,
300 "MIPI LCD RGB I/F test failed: "
301 "expecting %04x, got %04x\n",
302 test_values
[i
], pixel
);
303 return MIPID_TEST_FAILED
;
312 static void ls041y3_esd_recover(struct mipid_device
*md
)
314 dev_err(&md
->spi
->dev
, "performing LCD ESD recovery\n");
315 set_sleep_mode(md
, 1);
316 set_sleep_mode(md
, 0);
319 static void ls041y3_esd_check_mode1(struct mipid_device
*md
)
323 mipid_read(md
, MIPID_CMD_RDDSDR
, &state1
, 1);
324 set_sleep_mode(md
, 0);
325 mipid_read(md
, MIPID_CMD_RDDSDR
, &state2
, 1);
326 dev_dbg(&md
->spi
->dev
, "ESD mode 1 state1 %02x state2 %02x\n",
328 /* Each sleep out command will trigger a self diagnostic and flip
329 * Bit6 if the test passes.
331 if (!((state1
^ state2
) & (1 << 6)))
332 ls041y3_esd_recover(md
);
335 static void ls041y3_esd_check_mode2(struct mipid_device
*md
)
339 static const struct {
343 } *rd
, rd_ctrl
[7] = {
344 { 0xb0, 4, { 0x0101, 0x01fe, } },
345 { 0xb1, 4, { 0x01de, 0x0121, } },
346 { 0xc2, 4, { 0x0100, 0x0100, } },
347 { 0xbd, 2, { 0x0100, } },
348 { 0xc2, 4, { 0x01fc, 0x0103, } },
354 for (i
= 0; i
< 3; i
++, rd
++)
355 mipid_write(md
, rd
->cmd
, (u8
*)rd
->wbuf
, rd
->wlen
);
358 mipid_read(md
, rd
->cmd
, rbuf
, 2);
361 for (i
= 0; i
< 3; i
++, rd
++) {
363 mipid_write(md
, rd
->cmd
, (u8
*)rd
->wbuf
, rd
->wlen
);
366 dev_dbg(&md
->spi
->dev
, "ESD mode 2 state %02x\n", rbuf
[1]);
368 ls041y3_esd_recover(md
);
371 static void ls041y3_esd_check(struct mipid_device
*md
)
373 ls041y3_esd_check_mode1(md
);
374 if (md
->revision
>= 0x88)
375 ls041y3_esd_check_mode2(md
);
378 static void mipid_esd_start_check(struct mipid_device
*md
)
380 if (md
->esd_check
!= NULL
)
381 schedule_delayed_work(&md
->esd_work
,
382 MIPID_ESD_CHECK_PERIOD
);
385 static void mipid_esd_stop_check(struct mipid_device
*md
)
387 if (md
->esd_check
!= NULL
)
388 cancel_delayed_work_sync(&md
->esd_work
);
391 static void mipid_esd_work(struct work_struct
*work
)
393 struct mipid_device
*md
= container_of(work
, struct mipid_device
,
396 mutex_lock(&md
->mutex
);
398 mutex_unlock(&md
->mutex
);
399 mipid_esd_start_check(md
);
402 static int mipid_enable(struct lcd_panel
*panel
)
404 struct mipid_device
*md
= to_mipid_device(panel
);
406 mutex_lock(&md
->mutex
);
409 mutex_unlock(&md
->mutex
);
412 set_sleep_mode(md
, 0);
414 send_init_string(md
);
415 set_display_state(md
, 1);
416 mipid_set_bklight_level(panel
, md
->saved_bklight_level
);
417 mipid_esd_start_check(md
);
419 mutex_unlock(&md
->mutex
);
423 static void mipid_disable(struct lcd_panel
*panel
)
425 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.
431 mipid_esd_stop_check(md
);
432 mutex_lock(&md
->mutex
);
435 mutex_unlock(&md
->mutex
);
438 md
->saved_bklight_level
= mipid_get_bklight_level(panel
);
439 mipid_set_bklight_level(panel
, 0);
440 set_display_state(md
, 0);
441 set_sleep_mode(md
, 1);
444 mutex_unlock(&md
->mutex
);
447 static int panel_enabled(struct mipid_device
*md
)
452 mipid_read(md
, MIPID_CMD_READ_DISP_STATUS
, (u8
*)&disp_status
, 4);
453 disp_status
= __be32_to_cpu(disp_status
);
454 enabled
= (disp_status
& (1 << 17)) && (disp_status
& (1 << 10));
455 dev_dbg(&md
->spi
->dev
,
456 "LCD panel %senabled by bootloader (status 0x%04x)\n",
457 enabled
? "" : "not ", disp_status
);
461 static int mipid_init(struct lcd_panel
*panel
,
462 struct omapfb_device
*fbdev
)
464 struct mipid_device
*md
= to_mipid_device(panel
);
467 INIT_DELAYED_WORK(&md
->esd_work
, mipid_esd_work
);
468 mutex_init(&md
->mutex
);
470 md
->enabled
= panel_enabled(md
);
473 mipid_esd_start_check(md
);
475 md
->saved_bklight_level
= mipid_get_bklight_level(panel
);
480 static void mipid_cleanup(struct lcd_panel
*panel
)
482 struct mipid_device
*md
= to_mipid_device(panel
);
485 mipid_esd_stop_check(md
);
488 static const struct lcd_panel mipid_panel
= {
489 .config
= OMAP_LCDC_PANEL_TFT
,
494 .pixel_clock
= 21940,
503 .cleanup
= mipid_cleanup
,
504 .enable
= mipid_enable
,
505 .disable
= mipid_disable
,
506 .get_caps
= mipid_get_caps
,
507 .set_bklight_level
= mipid_set_bklight_level
,
508 .get_bklight_level
= mipid_get_bklight_level
,
509 .get_bklight_max
= mipid_get_bklight_max
,
510 .run_test
= mipid_run_test
,
513 static int mipid_detect(struct mipid_device
*md
)
515 struct mipid_platform_data
*pdata
;
518 pdata
= md
->spi
->dev
.platform_data
;
520 dev_err(&md
->spi
->dev
, "missing platform data\n");
524 mipid_read(md
, MIPID_CMD_READ_DISP_ID
, display_id
, 3);
525 dev_dbg(&md
->spi
->dev
, "MIPI display ID: %02x%02x%02x\n",
526 display_id
[0], display_id
[1], display_id
[2]);
528 switch (display_id
[0]) {
530 md
->panel
.name
= "lph8923";
533 md
->panel
.name
= "ls041y3";
534 md
->esd_check
= ls041y3_esd_check
;
537 md
->panel
.name
= "unknown";
538 dev_err(&md
->spi
->dev
, "invalid display ID\n");
542 md
->revision
= display_id
[1];
543 md
->panel
.data_lines
= pdata
->data_lines
;
544 pr_info("omapfb: %s rev %02x LCD detected, %d data lines\n",
545 md
->panel
.name
, md
->revision
, md
->panel
.data_lines
);
550 static int mipid_spi_probe(struct spi_device
*spi
)
552 struct mipid_device
*md
;
555 md
= kzalloc(sizeof(*md
), GFP_KERNEL
);
557 dev_err(&spi
->dev
, "out of memory\n");
561 /* This will de-assert RESET if active */
562 md
->reset
= gpiod_get(&spi
->dev
, "reset", GPIOD_OUT_LOW
);
563 if (IS_ERR(md
->reset
))
564 return dev_err_probe(&spi
->dev
, PTR_ERR(md
->reset
),
565 "no reset GPIO line\n");
567 spi
->mode
= SPI_MODE_0
;
569 dev_set_drvdata(&spi
->dev
, md
);
570 md
->panel
= mipid_panel
;
572 r
= mipid_detect(md
);
576 omapfb_register_panel(&md
->panel
);
585 static void mipid_spi_remove(struct spi_device
*spi
)
587 struct mipid_device
*md
= dev_get_drvdata(&spi
->dev
);
590 gpiod_set_value(md
->reset
, 1);
591 mipid_disable(&md
->panel
);
595 static struct spi_driver mipid_spi_driver
= {
597 .name
= MIPID_MODULE_NAME
,
599 .probe
= mipid_spi_probe
,
600 .remove
= mipid_spi_remove
,
603 module_spi_driver(mipid_spi_driver
);
605 MODULE_DESCRIPTION("MIPI display driver");
606 MODULE_LICENSE("GPL");