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