Merge tag 'qcom-soc-for-3.16-2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[pandora-kernel.git] / drivers / video / fbdev / omap2 / displays-new / encoder-tpd12s015.c
1 /*
2  * TPD12S015 HDMI ESD protection & level shifter chip driver
3  *
4  * Copyright (C) 2013 Texas Instruments
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
12 #include <linux/completion.h>
13 #include <linux/delay.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/gpio.h>
17 #include <linux/platform_device.h>
18 #include <linux/of_gpio.h>
19
20 #include <video/omapdss.h>
21 #include <video/omap-panel-data.h>
22
23 struct panel_drv_data {
24         struct omap_dss_device dssdev;
25         struct omap_dss_device *in;
26
27         int ct_cp_hpd_gpio;
28         int ls_oe_gpio;
29         int hpd_gpio;
30
31         struct omap_video_timings timings;
32
33         struct completion hpd_completion;
34 };
35
36 #define to_panel_data(x) container_of(x, struct panel_drv_data, dssdev)
37
38 static irqreturn_t tpd_hpd_irq_handler(int irq, void *data)
39 {
40         struct panel_drv_data *ddata = data;
41         bool hpd;
42
43         hpd = gpio_get_value_cansleep(ddata->hpd_gpio);
44
45         dev_dbg(ddata->dssdev.dev, "hpd %d\n", hpd);
46
47         if (gpio_is_valid(ddata->ls_oe_gpio)) {
48                 if (hpd)
49                         gpio_set_value_cansleep(ddata->ls_oe_gpio, 1);
50                 else
51                         gpio_set_value_cansleep(ddata->ls_oe_gpio, 0);
52         }
53
54         complete_all(&ddata->hpd_completion);
55
56         return IRQ_HANDLED;
57 }
58
59 static int tpd_connect(struct omap_dss_device *dssdev,
60                 struct omap_dss_device *dst)
61 {
62         struct panel_drv_data *ddata = to_panel_data(dssdev);
63         struct omap_dss_device *in = ddata->in;
64         int r;
65
66         r = in->ops.hdmi->connect(in, dssdev);
67         if (r)
68                 return r;
69
70         dst->src = dssdev;
71         dssdev->dst = dst;
72
73         reinit_completion(&ddata->hpd_completion);
74
75         gpio_set_value_cansleep(ddata->ct_cp_hpd_gpio, 1);
76         /* DC-DC converter needs at max 300us to get to 90% of 5V */
77         udelay(300);
78
79         /*
80          * If there's a cable connected, wait for the hpd irq to trigger,
81          * which turns on the level shifters.
82          */
83         if (gpio_get_value_cansleep(ddata->hpd_gpio)) {
84                 unsigned long to;
85                 to = wait_for_completion_timeout(&ddata->hpd_completion,
86                                 msecs_to_jiffies(250));
87                 WARN_ON_ONCE(to == 0);
88         }
89
90         return 0;
91 }
92
93 static void tpd_disconnect(struct omap_dss_device *dssdev,
94                 struct omap_dss_device *dst)
95 {
96         struct panel_drv_data *ddata = to_panel_data(dssdev);
97         struct omap_dss_device *in = ddata->in;
98
99         WARN_ON(dst != dssdev->dst);
100
101         if (dst != dssdev->dst)
102                 return;
103
104         gpio_set_value_cansleep(ddata->ct_cp_hpd_gpio, 0);
105
106         dst->src = NULL;
107         dssdev->dst = NULL;
108
109         in->ops.hdmi->disconnect(in, &ddata->dssdev);
110 }
111
112 static int tpd_enable(struct omap_dss_device *dssdev)
113 {
114         struct panel_drv_data *ddata = to_panel_data(dssdev);
115         struct omap_dss_device *in = ddata->in;
116         int r;
117
118         if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE)
119                 return 0;
120
121         in->ops.hdmi->set_timings(in, &ddata->timings);
122
123         r = in->ops.hdmi->enable(in);
124         if (r)
125                 return r;
126
127         dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
128
129         return r;
130 }
131
132 static void tpd_disable(struct omap_dss_device *dssdev)
133 {
134         struct panel_drv_data *ddata = to_panel_data(dssdev);
135         struct omap_dss_device *in = ddata->in;
136
137         if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE)
138                 return;
139
140         in->ops.hdmi->disable(in);
141
142         dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
143 }
144
145 static void tpd_set_timings(struct omap_dss_device *dssdev,
146                 struct omap_video_timings *timings)
147 {
148         struct panel_drv_data *ddata = to_panel_data(dssdev);
149         struct omap_dss_device *in = ddata->in;
150
151         ddata->timings = *timings;
152         dssdev->panel.timings = *timings;
153
154         in->ops.hdmi->set_timings(in, timings);
155 }
156
157 static void tpd_get_timings(struct omap_dss_device *dssdev,
158                 struct omap_video_timings *timings)
159 {
160         struct panel_drv_data *ddata = to_panel_data(dssdev);
161
162         *timings = ddata->timings;
163 }
164
165 static int tpd_check_timings(struct omap_dss_device *dssdev,
166                 struct omap_video_timings *timings)
167 {
168         struct panel_drv_data *ddata = to_panel_data(dssdev);
169         struct omap_dss_device *in = ddata->in;
170         int r;
171
172         r = in->ops.hdmi->check_timings(in, timings);
173
174         return r;
175 }
176
177 static int tpd_read_edid(struct omap_dss_device *dssdev,
178                 u8 *edid, int len)
179 {
180         struct panel_drv_data *ddata = to_panel_data(dssdev);
181         struct omap_dss_device *in = ddata->in;
182
183         if (!gpio_get_value_cansleep(ddata->hpd_gpio))
184                 return -ENODEV;
185
186         return in->ops.hdmi->read_edid(in, edid, len);
187 }
188
189 static bool tpd_detect(struct omap_dss_device *dssdev)
190 {
191         struct panel_drv_data *ddata = to_panel_data(dssdev);
192
193         return gpio_get_value_cansleep(ddata->hpd_gpio);
194 }
195
196 static int tpd_audio_enable(struct omap_dss_device *dssdev)
197 {
198         struct panel_drv_data *ddata = to_panel_data(dssdev);
199         struct omap_dss_device *in = ddata->in;
200
201         return in->ops.hdmi->audio_enable(in);
202 }
203
204 static void tpd_audio_disable(struct omap_dss_device *dssdev)
205 {
206         struct panel_drv_data *ddata = to_panel_data(dssdev);
207         struct omap_dss_device *in = ddata->in;
208
209         in->ops.hdmi->audio_disable(in);
210 }
211
212 static int tpd_audio_start(struct omap_dss_device *dssdev)
213 {
214         struct panel_drv_data *ddata = to_panel_data(dssdev);
215         struct omap_dss_device *in = ddata->in;
216
217         return in->ops.hdmi->audio_start(in);
218 }
219
220 static void tpd_audio_stop(struct omap_dss_device *dssdev)
221 {
222         struct panel_drv_data *ddata = to_panel_data(dssdev);
223         struct omap_dss_device *in = ddata->in;
224
225         in->ops.hdmi->audio_stop(in);
226 }
227
228 static bool tpd_audio_supported(struct omap_dss_device *dssdev)
229 {
230         struct panel_drv_data *ddata = to_panel_data(dssdev);
231         struct omap_dss_device *in = ddata->in;
232
233         return in->ops.hdmi->audio_supported(in);
234 }
235
236 static int tpd_audio_config(struct omap_dss_device *dssdev,
237                 struct omap_dss_audio *audio)
238 {
239         struct panel_drv_data *ddata = to_panel_data(dssdev);
240         struct omap_dss_device *in = ddata->in;
241
242         return in->ops.hdmi->audio_config(in, audio);
243 }
244
245 static const struct omapdss_hdmi_ops tpd_hdmi_ops = {
246         .connect                = tpd_connect,
247         .disconnect             = tpd_disconnect,
248
249         .enable                 = tpd_enable,
250         .disable                = tpd_disable,
251
252         .check_timings          = tpd_check_timings,
253         .set_timings            = tpd_set_timings,
254         .get_timings            = tpd_get_timings,
255
256         .read_edid              = tpd_read_edid,
257         .detect                 = tpd_detect,
258
259         .audio_enable           = tpd_audio_enable,
260         .audio_disable          = tpd_audio_disable,
261         .audio_start            = tpd_audio_start,
262         .audio_stop             = tpd_audio_stop,
263         .audio_supported        = tpd_audio_supported,
264         .audio_config           = tpd_audio_config,
265 };
266
267 static int tpd_probe_pdata(struct platform_device *pdev)
268 {
269         struct panel_drv_data *ddata = platform_get_drvdata(pdev);
270         struct encoder_tpd12s015_platform_data *pdata;
271         struct omap_dss_device *dssdev, *in;
272
273         pdata = dev_get_platdata(&pdev->dev);
274
275         ddata->ct_cp_hpd_gpio = pdata->ct_cp_hpd_gpio;
276         ddata->ls_oe_gpio = pdata->ls_oe_gpio;
277         ddata->hpd_gpio = pdata->hpd_gpio;
278
279         in = omap_dss_find_output(pdata->source);
280         if (in == NULL) {
281                 dev_err(&pdev->dev, "Failed to find video source\n");
282                 return -ENODEV;
283         }
284
285         ddata->in = in;
286
287         dssdev = &ddata->dssdev;
288         dssdev->name = pdata->name;
289
290         return 0;
291 }
292
293 static int tpd_probe_of(struct platform_device *pdev)
294 {
295         struct panel_drv_data *ddata = platform_get_drvdata(pdev);
296         struct device_node *node = pdev->dev.of_node;
297         struct omap_dss_device *in;
298         int gpio;
299
300         /* CT CP HPD GPIO */
301         gpio = of_get_gpio(node, 0);
302         if (!gpio_is_valid(gpio)) {
303                 dev_err(&pdev->dev, "failed to parse CT CP HPD gpio\n");
304                 return gpio;
305         }
306         ddata->ct_cp_hpd_gpio = gpio;
307
308         /* LS OE GPIO */
309         gpio = of_get_gpio(node, 1);
310         if (gpio_is_valid(gpio) || gpio == -ENOENT) {
311                 ddata->ls_oe_gpio = gpio;
312         } else {
313                 dev_err(&pdev->dev, "failed to parse LS OE gpio\n");
314                 return gpio;
315         }
316
317         /* HPD GPIO */
318         gpio = of_get_gpio(node, 2);
319         if (!gpio_is_valid(gpio)) {
320                 dev_err(&pdev->dev, "failed to parse HPD gpio\n");
321                 return gpio;
322         }
323         ddata->hpd_gpio = gpio;
324
325         in = omapdss_of_find_source_for_first_ep(node);
326         if (IS_ERR(in)) {
327                 dev_err(&pdev->dev, "failed to find video source\n");
328                 return PTR_ERR(in);
329         }
330
331         ddata->in = in;
332
333         return 0;
334 }
335
336 static int tpd_probe(struct platform_device *pdev)
337 {
338         struct omap_dss_device *in, *dssdev;
339         struct panel_drv_data *ddata;
340         int r;
341
342         ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
343         if (!ddata)
344                 return -ENOMEM;
345
346         platform_set_drvdata(pdev, ddata);
347
348         init_completion(&ddata->hpd_completion);
349
350         if (dev_get_platdata(&pdev->dev)) {
351                 r = tpd_probe_pdata(pdev);
352                 if (r)
353                         return r;
354         } else if (pdev->dev.of_node) {
355                 r = tpd_probe_of(pdev);
356                 if (r)
357                         return r;
358         } else {
359                 return -ENODEV;
360         }
361
362         r = devm_gpio_request_one(&pdev->dev, ddata->ct_cp_hpd_gpio,
363                         GPIOF_OUT_INIT_LOW, "hdmi_ct_cp_hpd");
364         if (r)
365                 goto err_gpio;
366
367         if (gpio_is_valid(ddata->ls_oe_gpio)) {
368                 r = devm_gpio_request_one(&pdev->dev, ddata->ls_oe_gpio,
369                                 GPIOF_OUT_INIT_LOW, "hdmi_ls_oe");
370                 if (r)
371                         goto err_gpio;
372         }
373
374         r = devm_gpio_request_one(&pdev->dev, ddata->hpd_gpio,
375                         GPIOF_DIR_IN, "hdmi_hpd");
376         if (r)
377                 goto err_gpio;
378
379         r = devm_request_threaded_irq(&pdev->dev, gpio_to_irq(ddata->hpd_gpio),
380                                  NULL, tpd_hpd_irq_handler,
381                                  IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
382                                  IRQF_ONESHOT, "hpd", ddata);
383         if (r)
384                 goto err_irq;
385
386         dssdev = &ddata->dssdev;
387         dssdev->ops.hdmi = &tpd_hdmi_ops;
388         dssdev->dev = &pdev->dev;
389         dssdev->type = OMAP_DISPLAY_TYPE_HDMI;
390         dssdev->output_type = OMAP_DISPLAY_TYPE_HDMI;
391         dssdev->owner = THIS_MODULE;
392
393         in = ddata->in;
394
395         r = omapdss_register_output(dssdev);
396         if (r) {
397                 dev_err(&pdev->dev, "Failed to register output\n");
398                 goto err_reg;
399         }
400
401         return 0;
402 err_reg:
403 err_irq:
404 err_gpio:
405         omap_dss_put_device(ddata->in);
406         return r;
407 }
408
409 static int __exit tpd_remove(struct platform_device *pdev)
410 {
411         struct panel_drv_data *ddata = platform_get_drvdata(pdev);
412         struct omap_dss_device *dssdev = &ddata->dssdev;
413         struct omap_dss_device *in = ddata->in;
414
415         omapdss_unregister_output(&ddata->dssdev);
416
417         WARN_ON(omapdss_device_is_enabled(dssdev));
418         if (omapdss_device_is_enabled(dssdev))
419                 tpd_disable(dssdev);
420
421         WARN_ON(omapdss_device_is_connected(dssdev));
422         if (omapdss_device_is_connected(dssdev))
423                 tpd_disconnect(dssdev, dssdev->dst);
424
425         omap_dss_put_device(in);
426
427         return 0;
428 }
429
430 static const struct of_device_id tpd_of_match[] = {
431         { .compatible = "omapdss,ti,tpd12s015", },
432         {},
433 };
434
435 MODULE_DEVICE_TABLE(of, tpd_of_match);
436
437 static struct platform_driver tpd_driver = {
438         .probe  = tpd_probe,
439         .remove = __exit_p(tpd_remove),
440         .driver = {
441                 .name   = "tpd12s015",
442                 .owner  = THIS_MODULE,
443                 .of_match_table = tpd_of_match,
444         },
445 };
446
447 module_platform_driver(tpd_driver);
448
449 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
450 MODULE_DESCRIPTION("TPD12S015 driver");
451 MODULE_LICENSE("GPL");