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