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