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