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