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