]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/video/omap/lcd_mipid.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs...
[mirror_ubuntu-jammy-kernel.git] / drivers / video / omap / lcd_mipid.c
1 /*
2 * LCD driver for MIPI DBI-C / DCS compatible LCDs
3 *
4 * Copyright (C) 2006 Nokia Corporation
5 * Author: Imre Deak <imre.deak@nokia.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21 #include <linux/device.h>
22 #include <linux/delay.h>
23 #include <linux/workqueue.h>
24 #include <linux/spi/spi.h>
25
26 #include <mach/omapfb.h>
27 #include <mach/lcd_mipid.h>
28
29 #define MIPID_MODULE_NAME "lcd_mipid"
30
31 #define MIPID_CMD_READ_DISP_ID 0x04
32 #define MIPID_CMD_READ_RED 0x06
33 #define MIPID_CMD_READ_GREEN 0x07
34 #define MIPID_CMD_READ_BLUE 0x08
35 #define MIPID_CMD_READ_DISP_STATUS 0x09
36 #define MIPID_CMD_RDDSDR 0x0F
37 #define MIPID_CMD_SLEEP_IN 0x10
38 #define MIPID_CMD_SLEEP_OUT 0x11
39 #define MIPID_CMD_DISP_OFF 0x28
40 #define MIPID_CMD_DISP_ON 0x29
41
42 #define MIPID_ESD_CHECK_PERIOD msecs_to_jiffies(5000)
43
44 #define to_mipid_device(p) container_of(p, struct mipid_device, \
45 panel)
46 struct mipid_device {
47 int enabled;
48 int revision;
49 unsigned int saved_bklight_level;
50 unsigned long hw_guard_end; /* next value of jiffies
51 when we can issue the
52 next sleep in/out command */
53 unsigned long hw_guard_wait; /* max guard time in jiffies */
54
55 struct omapfb_device *fbdev;
56 struct spi_device *spi;
57 struct mutex mutex;
58 struct lcd_panel panel;
59
60 struct workqueue_struct *esd_wq;
61 struct delayed_work esd_work;
62 void (*esd_check)(struct mipid_device *m);
63 };
64
65 static void mipid_transfer(struct mipid_device *md, int cmd, const u8 *wbuf,
66 int wlen, u8 *rbuf, int rlen)
67 {
68 struct spi_message m;
69 struct spi_transfer *x, xfer[4];
70 u16 w;
71 int r;
72
73 BUG_ON(md->spi == NULL);
74
75 spi_message_init(&m);
76
77 memset(xfer, 0, sizeof(xfer));
78 x = &xfer[0];
79
80 cmd &= 0xff;
81 x->tx_buf = &cmd;
82 x->bits_per_word = 9;
83 x->len = 2;
84 spi_message_add_tail(x, &m);
85
86 if (wlen) {
87 x++;
88 x->tx_buf = wbuf;
89 x->len = wlen;
90 x->bits_per_word = 9;
91 spi_message_add_tail(x, &m);
92 }
93
94 if (rlen) {
95 x++;
96 x->rx_buf = &w;
97 x->len = 1;
98 spi_message_add_tail(x, &m);
99
100 if (rlen > 1) {
101 /* Arrange for the extra clock before the first
102 * data bit.
103 */
104 x->bits_per_word = 9;
105 x->len = 2;
106
107 x++;
108 x->rx_buf = &rbuf[1];
109 x->len = rlen - 1;
110 spi_message_add_tail(x, &m);
111 }
112 }
113
114 r = spi_sync(md->spi, &m);
115 if (r < 0)
116 dev_dbg(&md->spi->dev, "spi_sync %d\n", r);
117
118 if (rlen)
119 rbuf[0] = w & 0xff;
120 }
121
122 static inline void mipid_cmd(struct mipid_device *md, int cmd)
123 {
124 mipid_transfer(md, cmd, NULL, 0, NULL, 0);
125 }
126
127 static inline void mipid_write(struct mipid_device *md,
128 int reg, const u8 *buf, int len)
129 {
130 mipid_transfer(md, reg, buf, len, NULL, 0);
131 }
132
133 static inline void mipid_read(struct mipid_device *md,
134 int reg, u8 *buf, int len)
135 {
136 mipid_transfer(md, reg, NULL, 0, buf, len);
137 }
138
139 static void set_data_lines(struct mipid_device *md, int data_lines)
140 {
141 u16 par;
142
143 switch (data_lines) {
144 case 16:
145 par = 0x150;
146 break;
147 case 18:
148 par = 0x160;
149 break;
150 case 24:
151 par = 0x170;
152 break;
153 }
154 mipid_write(md, 0x3a, (u8 *)&par, 2);
155 }
156
157 static void send_init_string(struct mipid_device *md)
158 {
159 u16 initpar[] = { 0x0102, 0x0100, 0x0100 };
160
161 mipid_write(md, 0xc2, (u8 *)initpar, sizeof(initpar));
162 set_data_lines(md, md->panel.data_lines);
163 }
164
165 static void hw_guard_start(struct mipid_device *md, int guard_msec)
166 {
167 md->hw_guard_wait = msecs_to_jiffies(guard_msec);
168 md->hw_guard_end = jiffies + md->hw_guard_wait;
169 }
170
171 static void hw_guard_wait(struct mipid_device *md)
172 {
173 unsigned long wait = md->hw_guard_end - jiffies;
174
175 if ((long)wait > 0 && wait <= md->hw_guard_wait) {
176 set_current_state(TASK_UNINTERRUPTIBLE);
177 schedule_timeout(wait);
178 }
179 }
180
181 static void set_sleep_mode(struct mipid_device *md, int on)
182 {
183 int cmd, sleep_time = 50;
184
185 if (on)
186 cmd = MIPID_CMD_SLEEP_IN;
187 else
188 cmd = MIPID_CMD_SLEEP_OUT;
189 hw_guard_wait(md);
190 mipid_cmd(md, cmd);
191 hw_guard_start(md, 120);
192 /*
193 * When we enable the panel, it seems we _have_ to sleep
194 * 120 ms before sending the init string. When disabling the
195 * panel we'll sleep for the duration of 2 frames, so that the
196 * controller can still provide the PCLK,HS,VS signals.
197 */
198 if (!on)
199 sleep_time = 120;
200 msleep(sleep_time);
201 }
202
203 static void set_display_state(struct mipid_device *md, int enabled)
204 {
205 int cmd = enabled ? MIPID_CMD_DISP_ON : MIPID_CMD_DISP_OFF;
206
207 mipid_cmd(md, cmd);
208 }
209
210 static int mipid_set_bklight_level(struct lcd_panel *panel, unsigned int level)
211 {
212 struct mipid_device *md = to_mipid_device(panel);
213 struct mipid_platform_data *pd = md->spi->dev.platform_data;
214
215 if (pd->get_bklight_max == NULL || pd->set_bklight_level == NULL)
216 return -ENODEV;
217 if (level > pd->get_bklight_max(pd))
218 return -EINVAL;
219 if (!md->enabled) {
220 md->saved_bklight_level = level;
221 return 0;
222 }
223 pd->set_bklight_level(pd, level);
224
225 return 0;
226 }
227
228 static unsigned int mipid_get_bklight_level(struct lcd_panel *panel)
229 {
230 struct mipid_device *md = to_mipid_device(panel);
231 struct mipid_platform_data *pd = md->spi->dev.platform_data;
232
233 if (pd->get_bklight_level == NULL)
234 return -ENODEV;
235 return pd->get_bklight_level(pd);
236 }
237
238 static unsigned int mipid_get_bklight_max(struct lcd_panel *panel)
239 {
240 struct mipid_device *md = to_mipid_device(panel);
241 struct mipid_platform_data *pd = md->spi->dev.platform_data;
242
243 if (pd->get_bklight_max == NULL)
244 return -ENODEV;
245
246 return pd->get_bklight_max(pd);
247 }
248
249 static unsigned long mipid_get_caps(struct lcd_panel *panel)
250 {
251 return OMAPFB_CAPS_SET_BACKLIGHT;
252 }
253
254 static u16 read_first_pixel(struct mipid_device *md)
255 {
256 u16 pixel;
257 u8 red, green, blue;
258
259 mutex_lock(&md->mutex);
260 mipid_read(md, MIPID_CMD_READ_RED, &red, 1);
261 mipid_read(md, MIPID_CMD_READ_GREEN, &green, 1);
262 mipid_read(md, MIPID_CMD_READ_BLUE, &blue, 1);
263 mutex_unlock(&md->mutex);
264
265 switch (md->panel.data_lines) {
266 case 16:
267 pixel = ((red >> 1) << 11) | (green << 5) | (blue >> 1);
268 break;
269 case 24:
270 /* 24 bit -> 16 bit */
271 pixel = ((red >> 3) << 11) | ((green >> 2) << 5) |
272 (blue >> 3);
273 break;
274 default:
275 pixel = 0;
276 BUG();
277 }
278
279 return pixel;
280 }
281
282 static int mipid_run_test(struct lcd_panel *panel, int test_num)
283 {
284 struct mipid_device *md = to_mipid_device(panel);
285 static const u16 test_values[4] = {
286 0x0000, 0xffff, 0xaaaa, 0x5555,
287 };
288 int i;
289
290 if (test_num != MIPID_TEST_RGB_LINES)
291 return MIPID_TEST_INVALID;
292
293 for (i = 0; i < ARRAY_SIZE(test_values); i++) {
294 int delay;
295 unsigned long tmo;
296
297 omapfb_write_first_pixel(md->fbdev, test_values[i]);
298 tmo = jiffies + msecs_to_jiffies(100);
299 delay = 25;
300 while (1) {
301 u16 pixel;
302
303 msleep(delay);
304 pixel = read_first_pixel(md);
305 if (pixel == test_values[i])
306 break;
307 if (time_after(jiffies, tmo)) {
308 dev_err(&md->spi->dev,
309 "MIPI LCD RGB I/F test failed: "
310 "expecting %04x, got %04x\n",
311 test_values[i], pixel);
312 return MIPID_TEST_FAILED;
313 }
314 delay = 10;
315 }
316 }
317
318 return 0;
319 }
320
321 static void ls041y3_esd_recover(struct mipid_device *md)
322 {
323 dev_err(&md->spi->dev, "performing LCD ESD recovery\n");
324 set_sleep_mode(md, 1);
325 set_sleep_mode(md, 0);
326 }
327
328 static void ls041y3_esd_check_mode1(struct mipid_device *md)
329 {
330 u8 state1, state2;
331
332 mipid_read(md, MIPID_CMD_RDDSDR, &state1, 1);
333 set_sleep_mode(md, 0);
334 mipid_read(md, MIPID_CMD_RDDSDR, &state2, 1);
335 dev_dbg(&md->spi->dev, "ESD mode 1 state1 %02x state2 %02x\n",
336 state1, state2);
337 /* Each sleep out command will trigger a self diagnostic and flip
338 * Bit6 if the test passes.
339 */
340 if (!((state1 ^ state2) & (1 << 6)))
341 ls041y3_esd_recover(md);
342 }
343
344 static void ls041y3_esd_check_mode2(struct mipid_device *md)
345 {
346 int i;
347 u8 rbuf[2];
348 static const struct {
349 int cmd;
350 int wlen;
351 u16 wbuf[3];
352 } *rd, rd_ctrl[7] = {
353 { 0xb0, 4, { 0x0101, 0x01fe, } },
354 { 0xb1, 4, { 0x01de, 0x0121, } },
355 { 0xc2, 4, { 0x0100, 0x0100, } },
356 { 0xbd, 2, { 0x0100, } },
357 { 0xc2, 4, { 0x01fc, 0x0103, } },
358 { 0xb4, 0, },
359 { 0x00, 0, },
360 };
361
362 rd = rd_ctrl;
363 for (i = 0; i < 3; i++, rd++)
364 mipid_write(md, rd->cmd, (u8 *)rd->wbuf, rd->wlen);
365
366 udelay(10);
367 mipid_read(md, rd->cmd, rbuf, 2);
368 rd++;
369
370 for (i = 0; i < 3; i++, rd++) {
371 udelay(10);
372 mipid_write(md, rd->cmd, (u8 *)rd->wbuf, rd->wlen);
373 }
374
375 dev_dbg(&md->spi->dev, "ESD mode 2 state %02x\n", rbuf[1]);
376 if (rbuf[1] == 0x00)
377 ls041y3_esd_recover(md);
378 }
379
380 static void ls041y3_esd_check(struct mipid_device *md)
381 {
382 ls041y3_esd_check_mode1(md);
383 if (md->revision >= 0x88)
384 ls041y3_esd_check_mode2(md);
385 }
386
387 static void mipid_esd_start_check(struct mipid_device *md)
388 {
389 if (md->esd_check != NULL)
390 queue_delayed_work(md->esd_wq, &md->esd_work,
391 MIPID_ESD_CHECK_PERIOD);
392 }
393
394 static void mipid_esd_stop_check(struct mipid_device *md)
395 {
396 if (md->esd_check != NULL)
397 cancel_rearming_delayed_workqueue(md->esd_wq, &md->esd_work);
398 }
399
400 static void mipid_esd_work(struct work_struct *work)
401 {
402 struct mipid_device *md = container_of(work, struct mipid_device,
403 esd_work.work);
404
405 mutex_lock(&md->mutex);
406 md->esd_check(md);
407 mutex_unlock(&md->mutex);
408 mipid_esd_start_check(md);
409 }
410
411 static int mipid_enable(struct lcd_panel *panel)
412 {
413 struct mipid_device *md = to_mipid_device(panel);
414
415 mutex_lock(&md->mutex);
416
417 if (md->enabled) {
418 mutex_unlock(&md->mutex);
419 return 0;
420 }
421 set_sleep_mode(md, 0);
422 md->enabled = 1;
423 send_init_string(md);
424 set_display_state(md, 1);
425 mipid_set_bklight_level(panel, md->saved_bklight_level);
426 mipid_esd_start_check(md);
427
428 mutex_unlock(&md->mutex);
429 return 0;
430 }
431
432 static void mipid_disable(struct lcd_panel *panel)
433 {
434 struct mipid_device *md = to_mipid_device(panel);
435
436 /*
437 * A final ESD work might be called before returning,
438 * so do this without holding the lock.
439 */
440 mipid_esd_stop_check(md);
441 mutex_lock(&md->mutex);
442
443 if (!md->enabled) {
444 mutex_unlock(&md->mutex);
445 return;
446 }
447 md->saved_bklight_level = mipid_get_bklight_level(panel);
448 mipid_set_bklight_level(panel, 0);
449 set_display_state(md, 0);
450 set_sleep_mode(md, 1);
451 md->enabled = 0;
452
453 mutex_unlock(&md->mutex);
454 }
455
456 static int panel_enabled(struct mipid_device *md)
457 {
458 u32 disp_status;
459 int enabled;
460
461 mipid_read(md, MIPID_CMD_READ_DISP_STATUS, (u8 *)&disp_status, 4);
462 disp_status = __be32_to_cpu(disp_status);
463 enabled = (disp_status & (1 << 17)) && (disp_status & (1 << 10));
464 dev_dbg(&md->spi->dev,
465 "LCD panel %senabled by bootloader (status 0x%04x)\n",
466 enabled ? "" : "not ", disp_status);
467 return enabled;
468 }
469
470 static int mipid_init(struct lcd_panel *panel,
471 struct omapfb_device *fbdev)
472 {
473 struct mipid_device *md = to_mipid_device(panel);
474
475 md->fbdev = fbdev;
476 md->esd_wq = create_singlethread_workqueue("mipid_esd");
477 if (md->esd_wq == NULL) {
478 dev_err(&md->spi->dev, "can't create ESD workqueue\n");
479 return -ENOMEM;
480 }
481 INIT_DELAYED_WORK(&md->esd_work, mipid_esd_work);
482 mutex_init(&md->mutex);
483
484 md->enabled = panel_enabled(md);
485
486 if (md->enabled)
487 mipid_esd_start_check(md);
488 else
489 md->saved_bklight_level = mipid_get_bklight_level(panel);
490
491 return 0;
492 }
493
494 static void mipid_cleanup(struct lcd_panel *panel)
495 {
496 struct mipid_device *md = to_mipid_device(panel);
497
498 if (md->enabled)
499 mipid_esd_stop_check(md);
500 destroy_workqueue(md->esd_wq);
501 }
502
503 static struct lcd_panel mipid_panel = {
504 .config = OMAP_LCDC_PANEL_TFT,
505
506 .bpp = 16,
507 .x_res = 800,
508 .y_res = 480,
509 .pixel_clock = 21940,
510 .hsw = 50,
511 .hfp = 20,
512 .hbp = 15,
513 .vsw = 2,
514 .vfp = 1,
515 .vbp = 3,
516
517 .init = mipid_init,
518 .cleanup = mipid_cleanup,
519 .enable = mipid_enable,
520 .disable = mipid_disable,
521 .get_caps = mipid_get_caps,
522 .set_bklight_level = mipid_set_bklight_level,
523 .get_bklight_level = mipid_get_bklight_level,
524 .get_bklight_max = mipid_get_bklight_max,
525 .run_test = mipid_run_test,
526 };
527
528 static int mipid_detect(struct mipid_device *md)
529 {
530 struct mipid_platform_data *pdata;
531 u8 display_id[3];
532
533 pdata = md->spi->dev.platform_data;
534 if (pdata == NULL) {
535 dev_err(&md->spi->dev, "missing platform data\n");
536 return -ENOENT;
537 }
538
539 mipid_read(md, MIPID_CMD_READ_DISP_ID, display_id, 3);
540 dev_dbg(&md->spi->dev, "MIPI display ID: %02x%02x%02x\n",
541 display_id[0], display_id[1], display_id[2]);
542
543 switch (display_id[0]) {
544 case 0x45:
545 md->panel.name = "lph8923";
546 break;
547 case 0x83:
548 md->panel.name = "ls041y3";
549 md->esd_check = ls041y3_esd_check;
550 break;
551 default:
552 md->panel.name = "unknown";
553 dev_err(&md->spi->dev, "invalid display ID\n");
554 return -ENODEV;
555 }
556
557 md->revision = display_id[1];
558 md->panel.data_lines = pdata->data_lines;
559 pr_info("omapfb: %s rev %02x LCD detected, %d data lines\n",
560 md->panel.name, md->revision, md->panel.data_lines);
561
562 return 0;
563 }
564
565 static int mipid_spi_probe(struct spi_device *spi)
566 {
567 struct mipid_device *md;
568 int r;
569
570 md = kzalloc(sizeof(*md), GFP_KERNEL);
571 if (md == NULL) {
572 dev_err(&spi->dev, "out of memory\n");
573 return -ENOMEM;
574 }
575
576 spi->mode = SPI_MODE_0;
577 md->spi = spi;
578 dev_set_drvdata(&spi->dev, md);
579 md->panel = mipid_panel;
580
581 r = mipid_detect(md);
582 if (r < 0)
583 return r;
584
585 omapfb_register_panel(&md->panel);
586
587 return 0;
588 }
589
590 static int mipid_spi_remove(struct spi_device *spi)
591 {
592 struct mipid_device *md = dev_get_drvdata(&spi->dev);
593
594 mipid_disable(&md->panel);
595 kfree(md);
596
597 return 0;
598 }
599
600 static struct spi_driver mipid_spi_driver = {
601 .driver = {
602 .name = MIPID_MODULE_NAME,
603 .bus = &spi_bus_type,
604 .owner = THIS_MODULE,
605 },
606 .probe = mipid_spi_probe,
607 .remove = __devexit_p(mipid_spi_remove),
608 };
609
610 static int mipid_drv_init(void)
611 {
612 spi_register_driver(&mipid_spi_driver);
613
614 return 0;
615 }
616 module_init(mipid_drv_init);
617
618 static void mipid_drv_cleanup(void)
619 {
620 spi_unregister_driver(&mipid_spi_driver);
621 }
622 module_exit(mipid_drv_cleanup);
623
624 MODULE_DESCRIPTION("MIPI display driver");
625 MODULE_LICENSE("GPL");