Merge git://1984.lsi.us.es/nf-next
[cascardo/linux.git] / drivers / video / omap2 / displays / panel-tfp410.c
1 /*
2  * TFP410 DPI-to-DVI chip
3  *
4  * Copyright (C) 2011 Texas Instruments Inc
5  * Author: Tomi Valkeinen <tomi.valkeinen@ti.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 version 2 as published by
9  * the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <video/omapdss.h>
23 #include <linux/i2c.h>
24 #include <linux/gpio.h>
25 #include <drm/drm_edid.h>
26
27 #include <video/omap-panel-tfp410.h>
28
29 static const struct omap_video_timings tfp410_default_timings = {
30         .x_res          = 640,
31         .y_res          = 480,
32
33         .pixel_clock    = 23500,
34
35         .hfp            = 48,
36         .hsw            = 32,
37         .hbp            = 80,
38
39         .vfp            = 3,
40         .vsw            = 4,
41         .vbp            = 7,
42
43         .vsync_level    = OMAPDSS_SIG_ACTIVE_HIGH,
44         .hsync_level    = OMAPDSS_SIG_ACTIVE_HIGH,
45         .data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE,
46         .de_level       = OMAPDSS_SIG_ACTIVE_HIGH,
47         .sync_pclk_edge = OMAPDSS_DRIVE_SIG_OPPOSITE_EDGES,
48 };
49
50 struct panel_drv_data {
51         struct omap_dss_device *dssdev;
52
53         struct mutex lock;
54
55         int pd_gpio;
56
57         struct i2c_adapter *i2c_adapter;
58 };
59
60 static int tfp410_power_on(struct omap_dss_device *dssdev)
61 {
62         struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
63         int r;
64
65         if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE)
66                 return 0;
67
68         r = omapdss_dpi_display_enable(dssdev);
69         if (r)
70                 goto err0;
71
72         if (gpio_is_valid(ddata->pd_gpio))
73                 gpio_set_value_cansleep(ddata->pd_gpio, 1);
74
75         return 0;
76 err0:
77         return r;
78 }
79
80 static void tfp410_power_off(struct omap_dss_device *dssdev)
81 {
82         struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
83
84         if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE)
85                 return;
86
87         if (gpio_is_valid(ddata->pd_gpio))
88                 gpio_set_value_cansleep(ddata->pd_gpio, 0);
89
90         omapdss_dpi_display_disable(dssdev);
91 }
92
93 static int tfp410_probe(struct omap_dss_device *dssdev)
94 {
95         struct panel_drv_data *ddata;
96         int r;
97         int i2c_bus_num;
98
99         ddata = devm_kzalloc(&dssdev->dev, sizeof(*ddata), GFP_KERNEL);
100         if (!ddata)
101                 return -ENOMEM;
102
103         dssdev->panel.timings = tfp410_default_timings;
104
105         ddata->dssdev = dssdev;
106         mutex_init(&ddata->lock);
107
108         if (dssdev->data) {
109                 struct tfp410_platform_data *pdata = dssdev->data;
110
111                 ddata->pd_gpio = pdata->power_down_gpio;
112                 i2c_bus_num = pdata->i2c_bus_num;
113         } else {
114                 ddata->pd_gpio = -1;
115                 i2c_bus_num = -1;
116         }
117
118         if (gpio_is_valid(ddata->pd_gpio)) {
119                 r = gpio_request_one(ddata->pd_gpio, GPIOF_OUT_INIT_LOW,
120                                 "tfp410 pd");
121                 if (r) {
122                         dev_err(&dssdev->dev, "Failed to request PD GPIO %d\n",
123                                         ddata->pd_gpio);
124                         return r;
125                 }
126         }
127
128         if (i2c_bus_num != -1) {
129                 struct i2c_adapter *adapter;
130
131                 adapter = i2c_get_adapter(i2c_bus_num);
132                 if (!adapter) {
133                         dev_err(&dssdev->dev, "Failed to get I2C adapter, bus %d\n",
134                                         i2c_bus_num);
135                         r = -EINVAL;
136                         goto err_i2c;
137                 }
138
139                 ddata->i2c_adapter = adapter;
140         }
141
142         dev_set_drvdata(&dssdev->dev, ddata);
143
144         return 0;
145 err_i2c:
146         if (gpio_is_valid(ddata->pd_gpio))
147                 gpio_free(ddata->pd_gpio);
148         return r;
149 }
150
151 static void __exit tfp410_remove(struct omap_dss_device *dssdev)
152 {
153         struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
154
155         mutex_lock(&ddata->lock);
156
157         if (ddata->i2c_adapter)
158                 i2c_put_adapter(ddata->i2c_adapter);
159
160         if (gpio_is_valid(ddata->pd_gpio))
161                 gpio_free(ddata->pd_gpio);
162
163         dev_set_drvdata(&dssdev->dev, NULL);
164
165         mutex_unlock(&ddata->lock);
166 }
167
168 static int tfp410_enable(struct omap_dss_device *dssdev)
169 {
170         struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
171         int r;
172
173         mutex_lock(&ddata->lock);
174
175         r = tfp410_power_on(dssdev);
176         if (r == 0)
177                 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
178
179         mutex_unlock(&ddata->lock);
180
181         return r;
182 }
183
184 static void tfp410_disable(struct omap_dss_device *dssdev)
185 {
186         struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
187
188         mutex_lock(&ddata->lock);
189
190         tfp410_power_off(dssdev);
191
192         dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
193
194         mutex_unlock(&ddata->lock);
195 }
196
197 static int tfp410_suspend(struct omap_dss_device *dssdev)
198 {
199         struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
200
201         mutex_lock(&ddata->lock);
202
203         tfp410_power_off(dssdev);
204
205         dssdev->state = OMAP_DSS_DISPLAY_SUSPENDED;
206
207         mutex_unlock(&ddata->lock);
208
209         return 0;
210 }
211
212 static int tfp410_resume(struct omap_dss_device *dssdev)
213 {
214         struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
215         int r;
216
217         mutex_lock(&ddata->lock);
218
219         r = tfp410_power_on(dssdev);
220         if (r == 0)
221                 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
222
223         mutex_unlock(&ddata->lock);
224
225         return r;
226 }
227
228 static void tfp410_set_timings(struct omap_dss_device *dssdev,
229                 struct omap_video_timings *timings)
230 {
231         struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
232
233         mutex_lock(&ddata->lock);
234         dpi_set_timings(dssdev, timings);
235         mutex_unlock(&ddata->lock);
236 }
237
238 static void tfp410_get_timings(struct omap_dss_device *dssdev,
239                 struct omap_video_timings *timings)
240 {
241         struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
242
243         mutex_lock(&ddata->lock);
244         *timings = dssdev->panel.timings;
245         mutex_unlock(&ddata->lock);
246 }
247
248 static int tfp410_check_timings(struct omap_dss_device *dssdev,
249                 struct omap_video_timings *timings)
250 {
251         struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
252         int r;
253
254         mutex_lock(&ddata->lock);
255         r = dpi_check_timings(dssdev, timings);
256         mutex_unlock(&ddata->lock);
257
258         return r;
259 }
260
261
262 static int tfp410_ddc_read(struct i2c_adapter *adapter,
263                 unsigned char *buf, u16 count, u8 offset)
264 {
265         int r, retries;
266
267         for (retries = 3; retries > 0; retries--) {
268                 struct i2c_msg msgs[] = {
269                         {
270                                 .addr   = DDC_ADDR,
271                                 .flags  = 0,
272                                 .len    = 1,
273                                 .buf    = &offset,
274                         }, {
275                                 .addr   = DDC_ADDR,
276                                 .flags  = I2C_M_RD,
277                                 .len    = count,
278                                 .buf    = buf,
279                         }
280                 };
281
282                 r = i2c_transfer(adapter, msgs, 2);
283                 if (r == 2)
284                         return 0;
285
286                 if (r != -EAGAIN)
287                         break;
288         }
289
290         return r < 0 ? r : -EIO;
291 }
292
293 static int tfp410_read_edid(struct omap_dss_device *dssdev,
294                 u8 *edid, int len)
295 {
296         struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
297         int r, l, bytes_read;
298
299         mutex_lock(&ddata->lock);
300
301         if (!ddata->i2c_adapter) {
302                 r = -ENODEV;
303                 goto err;
304         }
305
306         l = min(EDID_LENGTH, len);
307         r = tfp410_ddc_read(ddata->i2c_adapter, edid, l, 0);
308         if (r)
309                 goto err;
310
311         bytes_read = l;
312
313         /* if there are extensions, read second block */
314         if (len > EDID_LENGTH && edid[0x7e] > 0) {
315                 l = min(EDID_LENGTH, len - EDID_LENGTH);
316
317                 r = tfp410_ddc_read(ddata->i2c_adapter, edid + EDID_LENGTH,
318                                 l, EDID_LENGTH);
319                 if (r)
320                         goto err;
321
322                 bytes_read += l;
323         }
324
325         mutex_unlock(&ddata->lock);
326
327         return bytes_read;
328
329 err:
330         mutex_unlock(&ddata->lock);
331         return r;
332 }
333
334 static bool tfp410_detect(struct omap_dss_device *dssdev)
335 {
336         struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
337         unsigned char out;
338         int r;
339
340         mutex_lock(&ddata->lock);
341
342         if (!ddata->i2c_adapter)
343                 goto out;
344
345         r = tfp410_ddc_read(ddata->i2c_adapter, &out, 1, 0);
346
347         mutex_unlock(&ddata->lock);
348
349         return r == 0;
350
351 out:
352         mutex_unlock(&ddata->lock);
353         return true;
354 }
355
356 static struct omap_dss_driver tfp410_driver = {
357         .probe          = tfp410_probe,
358         .remove         = __exit_p(tfp410_remove),
359
360         .enable         = tfp410_enable,
361         .disable        = tfp410_disable,
362         .suspend        = tfp410_suspend,
363         .resume         = tfp410_resume,
364
365         .set_timings    = tfp410_set_timings,
366         .get_timings    = tfp410_get_timings,
367         .check_timings  = tfp410_check_timings,
368
369         .read_edid      = tfp410_read_edid,
370         .detect         = tfp410_detect,
371
372         .driver         = {
373                 .name   = "tfp410",
374                 .owner  = THIS_MODULE,
375         },
376 };
377
378 static int __init tfp410_init(void)
379 {
380         return omap_dss_register_driver(&tfp410_driver);
381 }
382
383 static void __exit tfp410_exit(void)
384 {
385         omap_dss_unregister_driver(&tfp410_driver);
386 }
387
388 module_init(tfp410_init);
389 module_exit(tfp410_exit);
390 MODULE_LICENSE("GPL");