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/slab.h>
11 #include <linux/workqueue.h>
12 #include <linux/spi/spi.h>
13 #include <linux/module.h>
15 #include <linux/platform_data/lcd-mipid.h>
19 #define MIPID_MODULE_NAME "lcd_mipid"
21 #define MIPID_CMD_READ_DISP_ID 0x04
22 #define MIPID_CMD_READ_RED 0x06
23 #define MIPID_CMD_READ_GREEN 0x07
24 #define MIPID_CMD_READ_BLUE 0x08
25 #define MIPID_CMD_READ_DISP_STATUS 0x09
26 #define MIPID_CMD_RDDSDR 0x0F
27 #define MIPID_CMD_SLEEP_IN 0x10
28 #define MIPID_CMD_SLEEP_OUT 0x11
29 #define MIPID_CMD_DISP_OFF 0x28
30 #define MIPID_CMD_DISP_ON 0x29
32 #define MIPID_ESD_CHECK_PERIOD msecs_to_jiffies(5000)
34 #define to_mipid_device(p) container_of(p, struct mipid_device, \
39 unsigned int saved_bklight_level
;
40 unsigned long hw_guard_end
; /* next value of jiffies
42 next sleep in/out command */
43 unsigned long hw_guard_wait
; /* max guard time in jiffies */
45 struct omapfb_device
*fbdev
;
46 struct spi_device
*spi
;
48 struct lcd_panel panel
;
50 struct delayed_work esd_work
;
51 void (*esd_check
)(struct mipid_device
*m
);
54 static void mipid_transfer(struct mipid_device
*md
, int cmd
, const u8
*wbuf
,
55 int wlen
, u8
*rbuf
, int rlen
)
58 struct spi_transfer
*x
, xfer
[4];
62 BUG_ON(md
->spi
== NULL
);
66 memset(xfer
, 0, sizeof(xfer
));
73 spi_message_add_tail(x
, &m
);
80 spi_message_add_tail(x
, &m
);
87 spi_message_add_tail(x
, &m
);
90 /* Arrange for the extra clock before the first
99 spi_message_add_tail(x
, &m
);
103 r
= spi_sync(md
->spi
, &m
);
105 dev_dbg(&md
->spi
->dev
, "spi_sync %d\n", r
);
111 static inline void mipid_cmd(struct mipid_device
*md
, int cmd
)
113 mipid_transfer(md
, cmd
, NULL
, 0, NULL
, 0);
116 static inline void mipid_write(struct mipid_device
*md
,
117 int reg
, const u8
*buf
, int len
)
119 mipid_transfer(md
, reg
, buf
, len
, NULL
, 0);
122 static inline void mipid_read(struct mipid_device
*md
,
123 int reg
, u8
*buf
, int len
)
125 mipid_transfer(md
, reg
, NULL
, 0, buf
, len
);
128 static void set_data_lines(struct mipid_device
*md
, int data_lines
)
132 switch (data_lines
) {
143 mipid_write(md
, 0x3a, (u8
*)&par
, 2);
146 static void send_init_string(struct mipid_device
*md
)
148 u16 initpar
[] = { 0x0102, 0x0100, 0x0100 };
150 mipid_write(md
, 0xc2, (u8
*)initpar
, sizeof(initpar
));
151 set_data_lines(md
, md
->panel
.data_lines
);
154 static void hw_guard_start(struct mipid_device
*md
, int guard_msec
)
156 md
->hw_guard_wait
= msecs_to_jiffies(guard_msec
);
157 md
->hw_guard_end
= jiffies
+ md
->hw_guard_wait
;
160 static void hw_guard_wait(struct mipid_device
*md
)
162 unsigned long wait
= md
->hw_guard_end
- jiffies
;
164 if ((long)wait
> 0 && time_before_eq(wait
, md
->hw_guard_wait
)) {
165 set_current_state(TASK_UNINTERRUPTIBLE
);
166 schedule_timeout(wait
);
170 static void set_sleep_mode(struct mipid_device
*md
, int on
)
172 int cmd
, sleep_time
= 50;
175 cmd
= MIPID_CMD_SLEEP_IN
;
177 cmd
= MIPID_CMD_SLEEP_OUT
;
180 hw_guard_start(md
, 120);
182 * When we enable the panel, it seems we _have_ to sleep
183 * 120 ms before sending the init string. When disabling the
184 * panel we'll sleep for the duration of 2 frames, so that the
185 * controller can still provide the PCLK,HS,VS signals.
192 static void set_display_state(struct mipid_device
*md
, int enabled
)
194 int cmd
= enabled
? MIPID_CMD_DISP_ON
: MIPID_CMD_DISP_OFF
;
199 static int mipid_set_bklight_level(struct lcd_panel
*panel
, unsigned int level
)
201 struct mipid_device
*md
= to_mipid_device(panel
);
202 struct mipid_platform_data
*pd
= md
->spi
->dev
.platform_data
;
204 if (pd
->get_bklight_max
== NULL
|| pd
->set_bklight_level
== NULL
)
206 if (level
> pd
->get_bklight_max(pd
))
209 md
->saved_bklight_level
= level
;
212 pd
->set_bklight_level(pd
, level
);
217 static unsigned int mipid_get_bklight_level(struct lcd_panel
*panel
)
219 struct mipid_device
*md
= to_mipid_device(panel
);
220 struct mipid_platform_data
*pd
= md
->spi
->dev
.platform_data
;
222 if (pd
->get_bklight_level
== NULL
)
224 return pd
->get_bklight_level(pd
);
227 static unsigned int mipid_get_bklight_max(struct lcd_panel
*panel
)
229 struct mipid_device
*md
= to_mipid_device(panel
);
230 struct mipid_platform_data
*pd
= md
->spi
->dev
.platform_data
;
232 if (pd
->get_bklight_max
== NULL
)
235 return pd
->get_bklight_max(pd
);
238 static unsigned long mipid_get_caps(struct lcd_panel
*panel
)
240 return OMAPFB_CAPS_SET_BACKLIGHT
;
243 static u16
read_first_pixel(struct mipid_device
*md
)
248 mutex_lock(&md
->mutex
);
249 mipid_read(md
, MIPID_CMD_READ_RED
, &red
, 1);
250 mipid_read(md
, MIPID_CMD_READ_GREEN
, &green
, 1);
251 mipid_read(md
, MIPID_CMD_READ_BLUE
, &blue
, 1);
252 mutex_unlock(&md
->mutex
);
254 switch (md
->panel
.data_lines
) {
256 pixel
= ((red
>> 1) << 11) | (green
<< 5) | (blue
>> 1);
259 /* 24 bit -> 16 bit */
260 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
,
380 MIPID_ESD_CHECK_PERIOD
);
383 static void mipid_esd_stop_check(struct mipid_device
*md
)
385 if (md
->esd_check
!= NULL
)
386 cancel_delayed_work_sync(&md
->esd_work
);
389 static void mipid_esd_work(struct work_struct
*work
)
391 struct mipid_device
*md
= container_of(work
, struct mipid_device
,
394 mutex_lock(&md
->mutex
);
396 mutex_unlock(&md
->mutex
);
397 mipid_esd_start_check(md
);
400 static int mipid_enable(struct lcd_panel
*panel
)
402 struct mipid_device
*md
= to_mipid_device(panel
);
404 mutex_lock(&md
->mutex
);
407 mutex_unlock(&md
->mutex
);
410 set_sleep_mode(md
, 0);
412 send_init_string(md
);
413 set_display_state(md
, 1);
414 mipid_set_bklight_level(panel
, md
->saved_bklight_level
);
415 mipid_esd_start_check(md
);
417 mutex_unlock(&md
->mutex
);
421 static void mipid_disable(struct lcd_panel
*panel
)
423 struct mipid_device
*md
= to_mipid_device(panel
);
426 * A final ESD work might be called before returning,
427 * so do this without holding the lock.
429 mipid_esd_stop_check(md
);
430 mutex_lock(&md
->mutex
);
433 mutex_unlock(&md
->mutex
);
436 md
->saved_bklight_level
= mipid_get_bklight_level(panel
);
437 mipid_set_bklight_level(panel
, 0);
438 set_display_state(md
, 0);
439 set_sleep_mode(md
, 1);
442 mutex_unlock(&md
->mutex
);
445 static int panel_enabled(struct mipid_device
*md
)
450 mipid_read(md
, MIPID_CMD_READ_DISP_STATUS
, (u8
*)&disp_status
, 4);
451 disp_status
= __be32_to_cpu(disp_status
);
452 enabled
= (disp_status
& (1 << 17)) && (disp_status
& (1 << 10));
453 dev_dbg(&md
->spi
->dev
,
454 "LCD panel %senabled by bootloader (status 0x%04x)\n",
455 enabled
? "" : "not ", disp_status
);
459 static int mipid_init(struct lcd_panel
*panel
,
460 struct omapfb_device
*fbdev
)
462 struct mipid_device
*md
= to_mipid_device(panel
);
465 INIT_DELAYED_WORK(&md
->esd_work
, mipid_esd_work
);
466 mutex_init(&md
->mutex
);
468 md
->enabled
= panel_enabled(md
);
471 mipid_esd_start_check(md
);
473 md
->saved_bklight_level
= mipid_get_bklight_level(panel
);
478 static void mipid_cleanup(struct lcd_panel
*panel
)
480 struct mipid_device
*md
= to_mipid_device(panel
);
483 mipid_esd_stop_check(md
);
486 static const struct lcd_panel mipid_panel
= {
487 .config
= OMAP_LCDC_PANEL_TFT
,
492 .pixel_clock
= 21940,
501 .cleanup
= mipid_cleanup
,
502 .enable
= mipid_enable
,
503 .disable
= mipid_disable
,
504 .get_caps
= mipid_get_caps
,
505 .set_bklight_level
= mipid_set_bklight_level
,
506 .get_bklight_level
= mipid_get_bklight_level
,
507 .get_bklight_max
= mipid_get_bklight_max
,
508 .run_test
= mipid_run_test
,
511 static int mipid_detect(struct mipid_device
*md
)
513 struct mipid_platform_data
*pdata
;
516 pdata
= md
->spi
->dev
.platform_data
;
518 dev_err(&md
->spi
->dev
, "missing platform data\n");
522 mipid_read(md
, MIPID_CMD_READ_DISP_ID
, display_id
, 3);
523 dev_dbg(&md
->spi
->dev
, "MIPI display ID: %02x%02x%02x\n",
524 display_id
[0], display_id
[1], display_id
[2]);
526 switch (display_id
[0]) {
528 md
->panel
.name
= "lph8923";
531 md
->panel
.name
= "ls041y3";
532 md
->esd_check
= ls041y3_esd_check
;
535 md
->panel
.name
= "unknown";
536 dev_err(&md
->spi
->dev
, "invalid display ID\n");
540 md
->revision
= display_id
[1];
541 md
->panel
.data_lines
= pdata
->data_lines
;
542 pr_info("omapfb: %s rev %02x LCD detected, %d data lines\n",
543 md
->panel
.name
, md
->revision
, md
->panel
.data_lines
);
548 static int mipid_spi_probe(struct spi_device
*spi
)
550 struct mipid_device
*md
;
553 md
= kzalloc(sizeof(*md
), GFP_KERNEL
);
555 dev_err(&spi
->dev
, "out of memory\n");
559 spi
->mode
= SPI_MODE_0
;
561 dev_set_drvdata(&spi
->dev
, md
);
562 md
->panel
= mipid_panel
;
564 r
= mipid_detect(md
);
568 omapfb_register_panel(&md
->panel
);
573 static int mipid_spi_remove(struct spi_device
*spi
)
575 struct mipid_device
*md
= dev_get_drvdata(&spi
->dev
);
577 mipid_disable(&md
->panel
);
583 static struct spi_driver mipid_spi_driver
= {
585 .name
= MIPID_MODULE_NAME
,
587 .probe
= mipid_spi_probe
,
588 .remove
= mipid_spi_remove
,
591 module_spi_driver(mipid_spi_driver
);
593 MODULE_DESCRIPTION("MIPI display driver");
594 MODULE_LICENSE("GPL");