OMAPDSS: TFP410: rename dvi -> tfp410
[linux-2.6-block.git] / drivers / video / omap2 / displays / panel-dvi.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-dvi.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
44 struct panel_drv_data {
45         struct omap_dss_device *dssdev;
46
47         struct mutex lock;
48
49         int pd_gpio;
50 };
51
52 static inline struct tfp410_platform_data
53 *get_pdata(const struct omap_dss_device *dssdev)
54 {
55         return dssdev->data;
56 }
57
58 static int tfp410_power_on(struct omap_dss_device *dssdev)
59 {
60         struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
61         int r;
62
63         if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE)
64                 return 0;
65
66         r = omapdss_dpi_display_enable(dssdev);
67         if (r)
68                 goto err0;
69
70         if (gpio_is_valid(ddata->pd_gpio))
71                 gpio_set_value(ddata->pd_gpio, 1);
72
73         return 0;
74 err0:
75         return r;
76 }
77
78 static void tfp410_power_off(struct omap_dss_device *dssdev)
79 {
80         struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
81
82         if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE)
83                 return;
84
85         if (gpio_is_valid(ddata->pd_gpio))
86                 gpio_set_value(ddata->pd_gpio, 0);
87
88         omapdss_dpi_display_disable(dssdev);
89 }
90
91 static int tfp410_probe(struct omap_dss_device *dssdev)
92 {
93         struct tfp410_platform_data *pdata = get_pdata(dssdev);
94         struct panel_drv_data *ddata;
95         int r;
96
97         ddata = kzalloc(sizeof(*ddata), GFP_KERNEL);
98         if (!ddata)
99                 return -ENOMEM;
100
101         dssdev->panel.timings = tfp410_default_timings;
102         dssdev->panel.config = OMAP_DSS_LCD_TFT;
103
104         ddata->dssdev = dssdev;
105         mutex_init(&ddata->lock);
106
107         if (pdata)
108                 ddata->pd_gpio = pdata->power_down_gpio;
109         else
110                 ddata->pd_gpio = -1;
111
112         if (gpio_is_valid(ddata->pd_gpio)) {
113                 r = gpio_request_one(ddata->pd_gpio, GPIOF_OUT_INIT_LOW,
114                                 "tfp410 pd");
115                 if (r) {
116                         dev_err(&dssdev->dev, "Failed to request PD GPIO %d\n",
117                                         ddata->pd_gpio);
118                         ddata->pd_gpio = -1;
119                 }
120         }
121
122         dev_set_drvdata(&dssdev->dev, ddata);
123
124         return 0;
125 }
126
127 static void __exit tfp410_remove(struct omap_dss_device *dssdev)
128 {
129         struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
130
131         mutex_lock(&ddata->lock);
132
133         if (gpio_is_valid(ddata->pd_gpio))
134                 gpio_free(ddata->pd_gpio);
135
136         dev_set_drvdata(&dssdev->dev, NULL);
137
138         mutex_unlock(&ddata->lock);
139
140         kfree(ddata);
141 }
142
143 static int tfp410_enable(struct omap_dss_device *dssdev)
144 {
145         struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
146         int r;
147
148         mutex_lock(&ddata->lock);
149
150         r = tfp410_power_on(dssdev);
151         if (r == 0)
152                 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
153
154         mutex_unlock(&ddata->lock);
155
156         return r;
157 }
158
159 static void tfp410_disable(struct omap_dss_device *dssdev)
160 {
161         struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
162
163         mutex_lock(&ddata->lock);
164
165         tfp410_power_off(dssdev);
166
167         dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
168
169         mutex_unlock(&ddata->lock);
170 }
171
172 static int tfp410_suspend(struct omap_dss_device *dssdev)
173 {
174         struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
175
176         mutex_lock(&ddata->lock);
177
178         tfp410_power_off(dssdev);
179
180         dssdev->state = OMAP_DSS_DISPLAY_SUSPENDED;
181
182         mutex_unlock(&ddata->lock);
183
184         return 0;
185 }
186
187 static int tfp410_resume(struct omap_dss_device *dssdev)
188 {
189         struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
190         int r;
191
192         mutex_lock(&ddata->lock);
193
194         r = tfp410_power_on(dssdev);
195         if (r == 0)
196                 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
197
198         mutex_unlock(&ddata->lock);
199
200         return r;
201 }
202
203 static void tfp410_set_timings(struct omap_dss_device *dssdev,
204                 struct omap_video_timings *timings)
205 {
206         struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
207
208         mutex_lock(&ddata->lock);
209         dpi_set_timings(dssdev, timings);
210         mutex_unlock(&ddata->lock);
211 }
212
213 static void tfp410_get_timings(struct omap_dss_device *dssdev,
214                 struct omap_video_timings *timings)
215 {
216         struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
217
218         mutex_lock(&ddata->lock);
219         *timings = dssdev->panel.timings;
220         mutex_unlock(&ddata->lock);
221 }
222
223 static int tfp410_check_timings(struct omap_dss_device *dssdev,
224                 struct omap_video_timings *timings)
225 {
226         struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
227         int r;
228
229         mutex_lock(&ddata->lock);
230         r = dpi_check_timings(dssdev, timings);
231         mutex_unlock(&ddata->lock);
232
233         return r;
234 }
235
236
237 static int tfp410_ddc_read(struct i2c_adapter *adapter,
238                 unsigned char *buf, u16 count, u8 offset)
239 {
240         int r, retries;
241
242         for (retries = 3; retries > 0; retries--) {
243                 struct i2c_msg msgs[] = {
244                         {
245                                 .addr   = DDC_ADDR,
246                                 .flags  = 0,
247                                 .len    = 1,
248                                 .buf    = &offset,
249                         }, {
250                                 .addr   = DDC_ADDR,
251                                 .flags  = I2C_M_RD,
252                                 .len    = count,
253                                 .buf    = buf,
254                         }
255                 };
256
257                 r = i2c_transfer(adapter, msgs, 2);
258                 if (r == 2)
259                         return 0;
260
261                 if (r != -EAGAIN)
262                         break;
263         }
264
265         return r < 0 ? r : -EIO;
266 }
267
268 static int tfp410_read_edid(struct omap_dss_device *dssdev,
269                 u8 *edid, int len)
270 {
271         struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
272         struct tfp410_platform_data *pdata = get_pdata(dssdev);
273         struct i2c_adapter *adapter;
274         int r, l, bytes_read;
275
276         mutex_lock(&ddata->lock);
277
278         if (pdata->i2c_bus_num == 0) {
279                 r = -ENODEV;
280                 goto err;
281         }
282
283         adapter = i2c_get_adapter(pdata->i2c_bus_num);
284         if (!adapter) {
285                 dev_err(&dssdev->dev, "Failed to get I2C adapter, bus %d\n",
286                                 pdata->i2c_bus_num);
287                 r = -EINVAL;
288                 goto err;
289         }
290
291         l = min(EDID_LENGTH, len);
292         r = tfp410_ddc_read(adapter, edid, l, 0);
293         if (r)
294                 goto err;
295
296         bytes_read = l;
297
298         /* if there are extensions, read second block */
299         if (len > EDID_LENGTH && edid[0x7e] > 0) {
300                 l = min(EDID_LENGTH, len - EDID_LENGTH);
301
302                 r = tfp410_ddc_read(adapter, edid + EDID_LENGTH,
303                                 l, EDID_LENGTH);
304                 if (r)
305                         goto err;
306
307                 bytes_read += l;
308         }
309
310         mutex_unlock(&ddata->lock);
311
312         return bytes_read;
313
314 err:
315         mutex_unlock(&ddata->lock);
316         return r;
317 }
318
319 static bool tfp410_detect(struct omap_dss_device *dssdev)
320 {
321         struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
322         struct tfp410_platform_data *pdata = get_pdata(dssdev);
323         struct i2c_adapter *adapter;
324         unsigned char out;
325         int r;
326
327         mutex_lock(&ddata->lock);
328
329         if (pdata->i2c_bus_num == 0)
330                 goto out;
331
332         adapter = i2c_get_adapter(pdata->i2c_bus_num);
333         if (!adapter)
334                 goto out;
335
336         r = tfp410_ddc_read(adapter, &out, 1, 0);
337
338         mutex_unlock(&ddata->lock);
339
340         return r == 0;
341
342 out:
343         mutex_unlock(&ddata->lock);
344         return true;
345 }
346
347 static struct omap_dss_driver tfp410_driver = {
348         .probe          = tfp410_probe,
349         .remove         = __exit_p(tfp410_remove),
350
351         .enable         = tfp410_enable,
352         .disable        = tfp410_disable,
353         .suspend        = tfp410_suspend,
354         .resume         = tfp410_resume,
355
356         .set_timings    = tfp410_set_timings,
357         .get_timings    = tfp410_get_timings,
358         .check_timings  = tfp410_check_timings,
359
360         .read_edid      = tfp410_read_edid,
361         .detect         = tfp410_detect,
362
363         .driver         = {
364                 .name   = "tfp410",
365                 .owner  = THIS_MODULE,
366         },
367 };
368
369 static int __init tfp410_init(void)
370 {
371         return omap_dss_register_driver(&tfp410_driver);
372 }
373
374 static void __exit tfp410_exit(void)
375 {
376         omap_dss_unregister_driver(&tfp410_driver);
377 }
378
379 module_init(tfp410_init);
380 module_exit(tfp410_exit);
381 MODULE_LICENSE("GPL");