0d7a8ff50f1665cd874d50266184fafc738a898d
[linux-3.10.git] / drivers / video / omap2 / displays / panel-picodlp.c
1 /*
2  * picodlp panel driver
3  * picodlp_i2c_driver: i2c_client driver
4  *
5  * Copyright (C) 2009-2011 Texas Instruments
6  * Author: Mythri P K <mythripk@ti.com>
7  * Mayuresh Janorkar <mayur@ti.com>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License version 2 as published by
11  * the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along with
19  * this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include <linux/module.h>
23 #include <linux/input.h>
24 #include <linux/platform_device.h>
25 #include <linux/interrupt.h>
26 #include <linux/firmware.h>
27 #include <linux/slab.h>
28 #include <linux/mutex.h>
29 #include <linux/i2c.h>
30 #include <linux/delay.h>
31 #include <linux/gpio.h>
32
33 #include <video/omapdss.h>
34 #include <video/omap-panel-picodlp.h>
35
36 #include "panel-picodlp.h"
37
38 struct picodlp_data {
39         struct mutex lock;
40         struct i2c_client *picodlp_i2c_client;
41 };
42
43 static struct i2c_board_info picodlp_i2c_board_info = {
44         I2C_BOARD_INFO("picodlp_i2c_driver", 0x1b),
45 };
46
47 struct picodlp_i2c_data {
48         struct mutex xfer_lock;
49 };
50
51 static struct i2c_device_id picodlp_i2c_id[] = {
52         { "picodlp_i2c_driver", 0 },
53 };
54
55 struct picodlp_i2c_command {
56         u8 reg;
57         u32 value;
58 };
59
60 static struct omap_video_timings pico_ls_timings = {
61         .x_res          = 864,
62         .y_res          = 480,
63         .hsw            = 7,
64         .hfp            = 11,
65         .hbp            = 7,
66
67         .pixel_clock    = 19200,
68
69         .vsw            = 2,
70         .vfp            = 3,
71         .vbp            = 14,
72
73         .vsync_level    = OMAPDSS_SIG_ACTIVE_LOW,
74         .hsync_level    = OMAPDSS_SIG_ACTIVE_LOW,
75         .data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE,
76         .de_level       = OMAPDSS_SIG_ACTIVE_HIGH,
77         .sync_pclk_edge = OMAPDSS_DRIVE_SIG_FALLING_EDGE,
78 };
79
80 static inline struct picodlp_panel_data
81                 *get_panel_data(const struct omap_dss_device *dssdev)
82 {
83         return (struct picodlp_panel_data *) dssdev->data;
84 }
85
86 static u32 picodlp_i2c_read(struct i2c_client *client, u8 reg)
87 {
88         u8 read_cmd[] = {READ_REG_SELECT, reg}, data[4];
89         struct picodlp_i2c_data *picodlp_i2c_data = i2c_get_clientdata(client);
90         struct i2c_msg msg[2];
91
92         mutex_lock(&picodlp_i2c_data->xfer_lock);
93
94         msg[0].addr = client->addr;
95         msg[0].flags = 0;
96         msg[0].len = 2;
97         msg[0].buf = read_cmd;
98
99         msg[1].addr = client->addr;
100         msg[1].flags = I2C_M_RD;
101         msg[1].len = 4;
102         msg[1].buf = data;
103
104         i2c_transfer(client->adapter, msg, 2);
105         mutex_unlock(&picodlp_i2c_data->xfer_lock);
106         return (data[3] | (data[2] << 8) | (data[1] << 16) | (data[0] << 24));
107 }
108
109 static int picodlp_i2c_write_block(struct i2c_client *client,
110                                         u8 *data, int len)
111 {
112         struct i2c_msg msg;
113         int i, r, msg_count = 1;
114
115         struct picodlp_i2c_data *picodlp_i2c_data = i2c_get_clientdata(client);
116
117         if (len < 1 || len > 32) {
118                 dev_err(&client->dev,
119                         "too long syn_write_block len %d\n", len);
120                 return -EIO;
121         }
122         mutex_lock(&picodlp_i2c_data->xfer_lock);
123
124         msg.addr = client->addr;
125         msg.flags = 0;
126         msg.len = len;
127         msg.buf = data;
128         r = i2c_transfer(client->adapter, &msg, msg_count);
129         mutex_unlock(&picodlp_i2c_data->xfer_lock);
130
131         /*
132          * i2c_transfer returns:
133          * number of messages sent in case of success
134          * a negative error number in case of failure
135          */
136         if (r != msg_count)
137                 goto err;
138
139         /* In case of success */
140         for (i = 0; i < len; i++)
141                 dev_dbg(&client->dev,
142                         "addr %x bw 0x%02x[%d]: 0x%02x\n",
143                         client->addr, data[0] + i, i, data[i]);
144
145         return 0;
146 err:
147         dev_err(&client->dev, "picodlp_i2c_write error\n");
148         return r;
149 }
150
151 static int picodlp_i2c_write(struct i2c_client *client, u8 reg, u32 value)
152 {
153         u8 data[5];
154         int i;
155
156         data[0] = reg;
157         for (i = 1; i < 5; i++)
158                 data[i] = (value >> (32 - (i) * 8)) & 0xFF;
159
160         return picodlp_i2c_write_block(client, data, 5);
161 }
162
163 static int picodlp_i2c_write_array(struct i2c_client *client,
164                         const struct picodlp_i2c_command commands[],
165                         int count)
166 {
167         int i, r = 0;
168         for (i = 0; i < count; i++) {
169                 r = picodlp_i2c_write(client, commands[i].reg,
170                                                 commands[i].value);
171                 if (r)
172                         return r;
173         }
174         return r;
175 }
176
177 static int picodlp_wait_for_dma_done(struct i2c_client *client)
178 {
179         u8 trial = 100;
180
181         do {
182                 msleep(1);
183                 if (!trial--)
184                         return -ETIMEDOUT;
185         } while (picodlp_i2c_read(client, MAIN_STATUS) & DMA_STATUS);
186
187         return 0;
188 }
189
190 /**
191  * picodlp_i2c_init:    i2c_initialization routine
192  * client:      i2c_client for communication
193  *
194  * return
195  *              0       : Success, no error
196  *      error code      : Failure
197  */
198 static int picodlp_i2c_init(struct i2c_client *client)
199 {
200         int r;
201         static const struct picodlp_i2c_command init_cmd_set1[] = {
202                 {SOFT_RESET, 1},
203                 {DMD_PARK_TRIGGER, 1},
204                 {MISC_REG, 5},
205                 {SEQ_CONTROL, 0},
206                 {SEQ_VECTOR, 0x100},
207                 {DMD_BLOCK_COUNT, 7},
208                 {DMD_VCC_CONTROL, 0x109},
209                 {DMD_PARK_PULSE_COUNT, 0xA},
210                 {DMD_PARK_PULSE_WIDTH, 0xB},
211                 {DMD_PARK_DELAY, 0x2ED},
212                 {DMD_SHADOW_ENABLE, 0},
213                 {FLASH_OPCODE, 0xB},
214                 {FLASH_DUMMY_BYTES, 1},
215                 {FLASH_ADDR_BYTES, 3},
216                 {PBC_CONTROL, 0},
217                 {FLASH_START_ADDR, CMT_LUT_0_START_ADDR},
218                 {FLASH_READ_BYTES, CMT_LUT_0_SIZE},
219                 {CMT_SPLASH_LUT_START_ADDR, 0},
220                 {CMT_SPLASH_LUT_DEST_SELECT, CMT_LUT_ALL},
221                 {PBC_CONTROL, 1},
222         };
223
224         static const struct picodlp_i2c_command init_cmd_set2[] = {
225                 {PBC_CONTROL, 0},
226                 {CMT_SPLASH_LUT_DEST_SELECT, 0},
227                 {PBC_CONTROL, 0},
228                 {FLASH_START_ADDR, SEQUENCE_0_START_ADDR},
229                 {FLASH_READ_BYTES, SEQUENCE_0_SIZE},
230                 {SEQ_RESET_LUT_START_ADDR, 0},
231                 {SEQ_RESET_LUT_DEST_SELECT, SEQ_SEQ_LUT},
232                 {PBC_CONTROL, 1},
233         };
234
235         static const struct picodlp_i2c_command init_cmd_set3[] = {
236                 {PBC_CONTROL, 0},
237                 {SEQ_RESET_LUT_DEST_SELECT, 0},
238                 {PBC_CONTROL, 0},
239                 {FLASH_START_ADDR, DRC_TABLE_0_START_ADDR},
240                 {FLASH_READ_BYTES, DRC_TABLE_0_SIZE},
241                 {SEQ_RESET_LUT_START_ADDR, 0},
242                 {SEQ_RESET_LUT_DEST_SELECT, SEQ_DRC_LUT_ALL},
243                 {PBC_CONTROL, 1},
244         };
245
246         static const struct picodlp_i2c_command init_cmd_set4[] = {
247                 {PBC_CONTROL, 0},
248                 {SEQ_RESET_LUT_DEST_SELECT, 0},
249                 {SDC_ENABLE, 1},
250                 {AGC_CTRL, 7},
251                 {CCA_C1A, 0x100},
252                 {CCA_C1B, 0x0},
253                 {CCA_C1C, 0x0},
254                 {CCA_C2A, 0x0},
255                 {CCA_C2B, 0x100},
256                 {CCA_C2C, 0x0},
257                 {CCA_C3A, 0x0},
258                 {CCA_C3B, 0x0},
259                 {CCA_C3C, 0x100},
260                 {CCA_C7A, 0x100},
261                 {CCA_C7B, 0x100},
262                 {CCA_C7C, 0x100},
263                 {CCA_ENABLE, 1},
264                 {CPU_IF_MODE, 1},
265                 {SHORT_FLIP, 1},
266                 {CURTAIN_CONTROL, 0},
267                 {DMD_PARK_TRIGGER, 0},
268                 {R_DRIVE_CURRENT, 0x298},
269                 {G_DRIVE_CURRENT, 0x298},
270                 {B_DRIVE_CURRENT, 0x298},
271                 {RGB_DRIVER_ENABLE, 7},
272                 {SEQ_CONTROL, 0},
273                 {ACTGEN_CONTROL, 0x10},
274                 {SEQUENCE_MODE, SEQ_LOCK},
275                 {DATA_FORMAT, RGB888},
276                 {INPUT_RESOLUTION, WVGA_864_LANDSCAPE},
277                 {INPUT_SOURCE, PARALLEL_RGB},
278                 {CPU_IF_SYNC_METHOD, 1},
279                 {SEQ_CONTROL, 1}
280         };
281
282         r = picodlp_i2c_write_array(client, init_cmd_set1,
283                                                 ARRAY_SIZE(init_cmd_set1));
284         if (r)
285                 return r;
286
287         r = picodlp_wait_for_dma_done(client);
288         if (r)
289                 return r;
290
291         r = picodlp_i2c_write_array(client, init_cmd_set2,
292                                         ARRAY_SIZE(init_cmd_set2));
293         if (r)
294                 return r;
295
296         r = picodlp_wait_for_dma_done(client);
297         if (r)
298                 return r;
299
300         r = picodlp_i2c_write_array(client, init_cmd_set3,
301                                         ARRAY_SIZE(init_cmd_set3));
302         if (r)
303                 return r;
304
305         r = picodlp_wait_for_dma_done(client);
306         if (r)
307                 return r;
308
309         r = picodlp_i2c_write_array(client, init_cmd_set4,
310                                         ARRAY_SIZE(init_cmd_set4));
311         if (r)
312                 return r;
313
314         return 0;
315 }
316
317 static int picodlp_i2c_probe(struct i2c_client *client,
318                 const struct i2c_device_id *id)
319 {
320         struct picodlp_i2c_data *picodlp_i2c_data;
321
322         picodlp_i2c_data = kzalloc(sizeof(struct picodlp_i2c_data), GFP_KERNEL);
323
324         if (!picodlp_i2c_data)
325                 return -ENOMEM;
326
327         mutex_init(&picodlp_i2c_data->xfer_lock);
328         i2c_set_clientdata(client, picodlp_i2c_data);
329
330         return 0;
331 }
332
333 static int picodlp_i2c_remove(struct i2c_client *client)
334 {
335         struct picodlp_i2c_data *picodlp_i2c_data =
336                                         i2c_get_clientdata(client);
337         kfree(picodlp_i2c_data);
338         return 0;
339 }
340
341 static struct i2c_driver picodlp_i2c_driver = {
342         .driver = {
343                 .name   = "picodlp_i2c_driver",
344         },
345         .probe          = picodlp_i2c_probe,
346         .remove         = picodlp_i2c_remove,
347         .id_table       = picodlp_i2c_id,
348 };
349
350 static int picodlp_panel_power_on(struct omap_dss_device *dssdev)
351 {
352         int r, trial = 100;
353         struct picodlp_data *picod = dev_get_drvdata(&dssdev->dev);
354         struct picodlp_panel_data *picodlp_pdata = get_panel_data(dssdev);
355
356         if (dssdev->platform_enable) {
357                 r = dssdev->platform_enable(dssdev);
358                 if (r)
359                         return r;
360         }
361
362         gpio_set_value(picodlp_pdata->pwrgood_gpio, 0);
363         msleep(1);
364         gpio_set_value(picodlp_pdata->pwrgood_gpio, 1);
365
366         while (!gpio_get_value(picodlp_pdata->emu_done_gpio)) {
367                 if (!trial--) {
368                         dev_err(&dssdev->dev, "emu_done signal not"
369                                                 " going high\n");
370                         return -ETIMEDOUT;
371                 }
372                 msleep(5);
373         }
374         /*
375          * As per dpp2600 programming guide,
376          * it is required to sleep for 1000ms after emu_done signal goes high
377          * then only i2c commands can be successfully sent to dpp2600
378          */
379         msleep(1000);
380
381         omapdss_dpi_set_timings(dssdev, &dssdev->panel.timings);
382
383         r = omapdss_dpi_display_enable(dssdev);
384         if (r) {
385                 dev_err(&dssdev->dev, "failed to enable DPI\n");
386                 goto err1;
387         }
388
389         r = picodlp_i2c_init(picod->picodlp_i2c_client);
390         if (r)
391                 goto err;
392
393         dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
394
395         return r;
396 err:
397         omapdss_dpi_display_disable(dssdev);
398 err1:
399         if (dssdev->platform_disable)
400                 dssdev->platform_disable(dssdev);
401
402         return r;
403 }
404
405 static void picodlp_panel_power_off(struct omap_dss_device *dssdev)
406 {
407         struct picodlp_panel_data *picodlp_pdata = get_panel_data(dssdev);
408
409         omapdss_dpi_display_disable(dssdev);
410
411         gpio_set_value(picodlp_pdata->emu_done_gpio, 0);
412         gpio_set_value(picodlp_pdata->pwrgood_gpio, 0);
413
414         if (dssdev->platform_disable)
415                 dssdev->platform_disable(dssdev);
416 }
417
418 static int picodlp_panel_probe(struct omap_dss_device *dssdev)
419 {
420         struct picodlp_data *picod;
421         struct picodlp_panel_data *picodlp_pdata = get_panel_data(dssdev);
422         struct i2c_adapter *adapter;
423         struct i2c_client *picodlp_i2c_client;
424         int r = 0, picodlp_adapter_id;
425
426         dssdev->panel.timings = pico_ls_timings;
427
428         picod =  kzalloc(sizeof(struct picodlp_data), GFP_KERNEL);
429         if (!picod)
430                 return -ENOMEM;
431
432         mutex_init(&picod->lock);
433
434         picodlp_adapter_id = picodlp_pdata->picodlp_adapter_id;
435
436         adapter = i2c_get_adapter(picodlp_adapter_id);
437         if (!adapter) {
438                 dev_err(&dssdev->dev, "can't get i2c adapter\n");
439                 r = -ENODEV;
440                 goto err;
441         }
442
443         picodlp_i2c_client = i2c_new_device(adapter, &picodlp_i2c_board_info);
444         if (!picodlp_i2c_client) {
445                 dev_err(&dssdev->dev, "can't add i2c device::"
446                                          " picodlp_i2c_client is NULL\n");
447                 r = -ENODEV;
448                 goto err;
449         }
450
451         picod->picodlp_i2c_client = picodlp_i2c_client;
452
453         dev_set_drvdata(&dssdev->dev, picod);
454         return r;
455 err:
456         kfree(picod);
457         return r;
458 }
459
460 static void picodlp_panel_remove(struct omap_dss_device *dssdev)
461 {
462         struct picodlp_data *picod = dev_get_drvdata(&dssdev->dev);
463
464         i2c_unregister_device(picod->picodlp_i2c_client);
465         dev_set_drvdata(&dssdev->dev, NULL);
466         dev_dbg(&dssdev->dev, "removing picodlp panel\n");
467
468         kfree(picod);
469 }
470
471 static int picodlp_panel_enable(struct omap_dss_device *dssdev)
472 {
473         struct picodlp_data *picod = dev_get_drvdata(&dssdev->dev);
474         int r;
475
476         dev_dbg(&dssdev->dev, "enabling picodlp panel\n");
477
478         mutex_lock(&picod->lock);
479         if (dssdev->state != OMAP_DSS_DISPLAY_DISABLED) {
480                 mutex_unlock(&picod->lock);
481                 return -EINVAL;
482         }
483
484         r = picodlp_panel_power_on(dssdev);
485         mutex_unlock(&picod->lock);
486
487         return r;
488 }
489
490 static void picodlp_panel_disable(struct omap_dss_device *dssdev)
491 {
492         struct picodlp_data *picod = dev_get_drvdata(&dssdev->dev);
493
494         mutex_lock(&picod->lock);
495         /* Turn off DLP Power */
496         if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE)
497                 picodlp_panel_power_off(dssdev);
498
499         dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
500         mutex_unlock(&picod->lock);
501
502         dev_dbg(&dssdev->dev, "disabling picodlp panel\n");
503 }
504
505 static int picodlp_panel_suspend(struct omap_dss_device *dssdev)
506 {
507         struct picodlp_data *picod = dev_get_drvdata(&dssdev->dev);
508
509         mutex_lock(&picod->lock);
510         /* Turn off DLP Power */
511         if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE) {
512                 mutex_unlock(&picod->lock);
513                 dev_err(&dssdev->dev, "unable to suspend picodlp panel,"
514                                         " panel is not ACTIVE\n");
515                 return -EINVAL;
516         }
517
518         picodlp_panel_power_off(dssdev);
519
520         dssdev->state = OMAP_DSS_DISPLAY_SUSPENDED;
521         mutex_unlock(&picod->lock);
522
523         dev_dbg(&dssdev->dev, "suspending picodlp panel\n");
524         return 0;
525 }
526
527 static int picodlp_panel_resume(struct omap_dss_device *dssdev)
528 {
529         struct picodlp_data *picod = dev_get_drvdata(&dssdev->dev);
530         int r;
531
532         mutex_lock(&picod->lock);
533         if (dssdev->state != OMAP_DSS_DISPLAY_SUSPENDED) {
534                 mutex_unlock(&picod->lock);
535                 dev_err(&dssdev->dev, "unable to resume picodlp panel,"
536                         " panel is not ACTIVE\n");
537                 return -EINVAL;
538         }
539
540         r = picodlp_panel_power_on(dssdev);
541         mutex_unlock(&picod->lock);
542         dev_dbg(&dssdev->dev, "resuming picodlp panel\n");
543         return r;
544 }
545
546 static void picodlp_get_resolution(struct omap_dss_device *dssdev,
547                                         u16 *xres, u16 *yres)
548 {
549         *xres = dssdev->panel.timings.x_res;
550         *yres = dssdev->panel.timings.y_res;
551 }
552
553 static struct omap_dss_driver picodlp_driver = {
554         .probe          = picodlp_panel_probe,
555         .remove         = picodlp_panel_remove,
556
557         .enable         = picodlp_panel_enable,
558         .disable        = picodlp_panel_disable,
559
560         .get_resolution = picodlp_get_resolution,
561
562         .suspend        = picodlp_panel_suspend,
563         .resume         = picodlp_panel_resume,
564
565         .driver         = {
566                 .name   = "picodlp_panel",
567                 .owner  = THIS_MODULE,
568         },
569 };
570
571 static int __init picodlp_init(void)
572 {
573         int r = 0;
574
575         r = i2c_add_driver(&picodlp_i2c_driver);
576         if (r) {
577                 printk(KERN_WARNING "picodlp_i2c_driver" \
578                         " registration failed\n");
579                 return r;
580         }
581
582         r = omap_dss_register_driver(&picodlp_driver);
583         if (r)
584                 i2c_del_driver(&picodlp_i2c_driver);
585
586         return r;
587 }
588
589 static void __exit picodlp_exit(void)
590 {
591         i2c_del_driver(&picodlp_i2c_driver);
592         omap_dss_unregister_driver(&picodlp_driver);
593 }
594
595 module_init(picodlp_init);
596 module_exit(picodlp_exit);
597
598 MODULE_AUTHOR("Mythri P K <mythripk@ti.com>");
599 MODULE_DESCRIPTION("picodlp driver");
600 MODULE_LICENSE("GPL");