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