]> Pileus Git - ~andy/linux/blob - drivers/video/omap2/displays/panel-picodlp.c
Merge tag 'fbdev-updates-for-3.6' of git://github.com/schandinat/linux-2.6
[~andy/linux] / 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         r = omapdss_dpi_display_enable(dssdev);
381         if (r) {
382                 dev_err(&dssdev->dev, "failed to enable DPI\n");
383                 goto err1;
384         }
385
386         r = picodlp_i2c_init(picod->picodlp_i2c_client);
387         if (r)
388                 goto err;
389
390         dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
391
392         return r;
393 err:
394         omapdss_dpi_display_disable(dssdev);
395 err1:
396         if (dssdev->platform_disable)
397                 dssdev->platform_disable(dssdev);
398
399         return r;
400 }
401
402 static void picodlp_panel_power_off(struct omap_dss_device *dssdev)
403 {
404         struct picodlp_panel_data *picodlp_pdata = get_panel_data(dssdev);
405
406         omapdss_dpi_display_disable(dssdev);
407
408         gpio_set_value(picodlp_pdata->emu_done_gpio, 0);
409         gpio_set_value(picodlp_pdata->pwrgood_gpio, 0);
410
411         if (dssdev->platform_disable)
412                 dssdev->platform_disable(dssdev);
413 }
414
415 static int picodlp_panel_probe(struct omap_dss_device *dssdev)
416 {
417         struct picodlp_data *picod;
418         struct picodlp_panel_data *picodlp_pdata = get_panel_data(dssdev);
419         struct i2c_adapter *adapter;
420         struct i2c_client *picodlp_i2c_client;
421         int r = 0, picodlp_adapter_id;
422
423         dssdev->panel.timings = pico_ls_timings;
424
425         picod =  kzalloc(sizeof(struct picodlp_data), GFP_KERNEL);
426         if (!picod)
427                 return -ENOMEM;
428
429         mutex_init(&picod->lock);
430
431         picodlp_adapter_id = picodlp_pdata->picodlp_adapter_id;
432
433         adapter = i2c_get_adapter(picodlp_adapter_id);
434         if (!adapter) {
435                 dev_err(&dssdev->dev, "can't get i2c adapter\n");
436                 r = -ENODEV;
437                 goto err;
438         }
439
440         picodlp_i2c_client = i2c_new_device(adapter, &picodlp_i2c_board_info);
441         if (!picodlp_i2c_client) {
442                 dev_err(&dssdev->dev, "can't add i2c device::"
443                                          " picodlp_i2c_client is NULL\n");
444                 r = -ENODEV;
445                 goto err;
446         }
447
448         picod->picodlp_i2c_client = picodlp_i2c_client;
449
450         dev_set_drvdata(&dssdev->dev, picod);
451         return r;
452 err:
453         kfree(picod);
454         return r;
455 }
456
457 static void picodlp_panel_remove(struct omap_dss_device *dssdev)
458 {
459         struct picodlp_data *picod = dev_get_drvdata(&dssdev->dev);
460
461         i2c_unregister_device(picod->picodlp_i2c_client);
462         dev_set_drvdata(&dssdev->dev, NULL);
463         dev_dbg(&dssdev->dev, "removing picodlp panel\n");
464
465         kfree(picod);
466 }
467
468 static int picodlp_panel_enable(struct omap_dss_device *dssdev)
469 {
470         struct picodlp_data *picod = dev_get_drvdata(&dssdev->dev);
471         int r;
472
473         dev_dbg(&dssdev->dev, "enabling picodlp panel\n");
474
475         mutex_lock(&picod->lock);
476         if (dssdev->state != OMAP_DSS_DISPLAY_DISABLED) {
477                 mutex_unlock(&picod->lock);
478                 return -EINVAL;
479         }
480
481         r = picodlp_panel_power_on(dssdev);
482         mutex_unlock(&picod->lock);
483
484         return r;
485 }
486
487 static void picodlp_panel_disable(struct omap_dss_device *dssdev)
488 {
489         struct picodlp_data *picod = dev_get_drvdata(&dssdev->dev);
490
491         mutex_lock(&picod->lock);
492         /* Turn off DLP Power */
493         if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE)
494                 picodlp_panel_power_off(dssdev);
495
496         dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
497         mutex_unlock(&picod->lock);
498
499         dev_dbg(&dssdev->dev, "disabling picodlp panel\n");
500 }
501
502 static int picodlp_panel_suspend(struct omap_dss_device *dssdev)
503 {
504         struct picodlp_data *picod = dev_get_drvdata(&dssdev->dev);
505
506         mutex_lock(&picod->lock);
507         /* Turn off DLP Power */
508         if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE) {
509                 mutex_unlock(&picod->lock);
510                 dev_err(&dssdev->dev, "unable to suspend picodlp panel,"
511                                         " panel is not ACTIVE\n");
512                 return -EINVAL;
513         }
514
515         picodlp_panel_power_off(dssdev);
516
517         dssdev->state = OMAP_DSS_DISPLAY_SUSPENDED;
518         mutex_unlock(&picod->lock);
519
520         dev_dbg(&dssdev->dev, "suspending picodlp panel\n");
521         return 0;
522 }
523
524 static int picodlp_panel_resume(struct omap_dss_device *dssdev)
525 {
526         struct picodlp_data *picod = dev_get_drvdata(&dssdev->dev);
527         int r;
528
529         mutex_lock(&picod->lock);
530         if (dssdev->state != OMAP_DSS_DISPLAY_SUSPENDED) {
531                 mutex_unlock(&picod->lock);
532                 dev_err(&dssdev->dev, "unable to resume picodlp panel,"
533                         " panel is not ACTIVE\n");
534                 return -EINVAL;
535         }
536
537         r = picodlp_panel_power_on(dssdev);
538         mutex_unlock(&picod->lock);
539         dev_dbg(&dssdev->dev, "resuming picodlp panel\n");
540         return r;
541 }
542
543 static void picodlp_get_resolution(struct omap_dss_device *dssdev,
544                                         u16 *xres, u16 *yres)
545 {
546         *xres = dssdev->panel.timings.x_res;
547         *yres = dssdev->panel.timings.y_res;
548 }
549
550 static struct omap_dss_driver picodlp_driver = {
551         .probe          = picodlp_panel_probe,
552         .remove         = picodlp_panel_remove,
553
554         .enable         = picodlp_panel_enable,
555         .disable        = picodlp_panel_disable,
556
557         .get_resolution = picodlp_get_resolution,
558
559         .suspend        = picodlp_panel_suspend,
560         .resume         = picodlp_panel_resume,
561
562         .driver         = {
563                 .name   = "picodlp_panel",
564                 .owner  = THIS_MODULE,
565         },
566 };
567
568 static int __init picodlp_init(void)
569 {
570         int r = 0;
571
572         r = i2c_add_driver(&picodlp_i2c_driver);
573         if (r) {
574                 printk(KERN_WARNING "picodlp_i2c_driver" \
575                         " registration failed\n");
576                 return r;
577         }
578
579         r = omap_dss_register_driver(&picodlp_driver);
580         if (r)
581                 i2c_del_driver(&picodlp_i2c_driver);
582
583         return r;
584 }
585
586 static void __exit picodlp_exit(void)
587 {
588         i2c_del_driver(&picodlp_i2c_driver);
589         omap_dss_unregister_driver(&picodlp_driver);
590 }
591
592 module_init(picodlp_init);
593 module_exit(picodlp_exit);
594
595 MODULE_AUTHOR("Mythri P K <mythripk@ti.com>");
596 MODULE_DESCRIPTION("picodlp driver");
597 MODULE_LICENSE("GPL");