Merge tag 'iommu-updates-v3.16' of git://git.kernel.org/pub/scm/linux/kernel/git...
[cascardo/linux.git] / drivers / video / fbdev / omap2 / displays-new / connector-hdmi.c
1 /*
2  * HDMI Connector 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/slab.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/of.h>
16 #include <linux/of_gpio.h>
17
18 #include <drm/drm_edid.h>
19
20 #include <video/omapdss.h>
21 #include <video/omap-panel-data.h>
22
23 static const struct omap_video_timings hdmic_default_timings = {
24         .x_res          = 640,
25         .y_res          = 480,
26         .pixelclock     = 25175000,
27         .hsw            = 96,
28         .hfp            = 16,
29         .hbp            = 48,
30         .vsw            = 2,
31         .vfp            = 11,
32         .vbp            = 31,
33
34         .vsync_level    = OMAPDSS_SIG_ACTIVE_LOW,
35         .hsync_level    = OMAPDSS_SIG_ACTIVE_LOW,
36
37         .interlace      = false,
38 };
39
40 struct panel_drv_data {
41         struct omap_dss_device dssdev;
42         struct omap_dss_device *in;
43
44         struct device *dev;
45
46         struct omap_video_timings timings;
47
48         int hpd_gpio;
49 };
50
51 #define to_panel_data(x) container_of(x, struct panel_drv_data, dssdev)
52
53 static int hdmic_connect(struct omap_dss_device *dssdev)
54 {
55         struct panel_drv_data *ddata = to_panel_data(dssdev);
56         struct omap_dss_device *in = ddata->in;
57         int r;
58
59         dev_dbg(ddata->dev, "connect\n");
60
61         if (omapdss_device_is_connected(dssdev))
62                 return 0;
63
64         r = in->ops.hdmi->connect(in, dssdev);
65         if (r)
66                 return r;
67
68         return 0;
69 }
70
71 static void hdmic_disconnect(struct omap_dss_device *dssdev)
72 {
73         struct panel_drv_data *ddata = to_panel_data(dssdev);
74         struct omap_dss_device *in = ddata->in;
75
76         dev_dbg(ddata->dev, "disconnect\n");
77
78         if (!omapdss_device_is_connected(dssdev))
79                 return;
80
81         in->ops.hdmi->disconnect(in, dssdev);
82 }
83
84 static int hdmic_enable(struct omap_dss_device *dssdev)
85 {
86         struct panel_drv_data *ddata = to_panel_data(dssdev);
87         struct omap_dss_device *in = ddata->in;
88         int r;
89
90         dev_dbg(ddata->dev, "enable\n");
91
92         if (!omapdss_device_is_connected(dssdev))
93                 return -ENODEV;
94
95         if (omapdss_device_is_enabled(dssdev))
96                 return 0;
97
98         in->ops.hdmi->set_timings(in, &ddata->timings);
99
100         r = in->ops.hdmi->enable(in);
101         if (r)
102                 return r;
103
104         dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
105
106         return r;
107 }
108
109 static void hdmic_disable(struct omap_dss_device *dssdev)
110 {
111         struct panel_drv_data *ddata = to_panel_data(dssdev);
112         struct omap_dss_device *in = ddata->in;
113
114         dev_dbg(ddata->dev, "disable\n");
115
116         if (!omapdss_device_is_enabled(dssdev))
117                 return;
118
119         in->ops.hdmi->disable(in);
120
121         dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
122 }
123
124 static void hdmic_set_timings(struct omap_dss_device *dssdev,
125                 struct omap_video_timings *timings)
126 {
127         struct panel_drv_data *ddata = to_panel_data(dssdev);
128         struct omap_dss_device *in = ddata->in;
129
130         ddata->timings = *timings;
131         dssdev->panel.timings = *timings;
132
133         in->ops.hdmi->set_timings(in, timings);
134 }
135
136 static void hdmic_get_timings(struct omap_dss_device *dssdev,
137                 struct omap_video_timings *timings)
138 {
139         struct panel_drv_data *ddata = to_panel_data(dssdev);
140
141         *timings = ddata->timings;
142 }
143
144 static int hdmic_check_timings(struct omap_dss_device *dssdev,
145                 struct omap_video_timings *timings)
146 {
147         struct panel_drv_data *ddata = to_panel_data(dssdev);
148         struct omap_dss_device *in = ddata->in;
149
150         return in->ops.hdmi->check_timings(in, timings);
151 }
152
153 static int hdmic_read_edid(struct omap_dss_device *dssdev,
154                 u8 *edid, int len)
155 {
156         struct panel_drv_data *ddata = to_panel_data(dssdev);
157         struct omap_dss_device *in = ddata->in;
158
159         return in->ops.hdmi->read_edid(in, edid, len);
160 }
161
162 static bool hdmic_detect(struct omap_dss_device *dssdev)
163 {
164         struct panel_drv_data *ddata = to_panel_data(dssdev);
165         struct omap_dss_device *in = ddata->in;
166
167         if (gpio_is_valid(ddata->hpd_gpio))
168                 return gpio_get_value_cansleep(ddata->hpd_gpio);
169         else
170                 return in->ops.hdmi->detect(in);
171 }
172
173 static int hdmic_audio_enable(struct omap_dss_device *dssdev)
174 {
175         struct panel_drv_data *ddata = to_panel_data(dssdev);
176         struct omap_dss_device *in = ddata->in;
177         int r;
178
179         /* enable audio only if the display is active */
180         if (!omapdss_device_is_enabled(dssdev))
181                 return -EPERM;
182
183         r = in->ops.hdmi->audio_enable(in);
184         if (r)
185                 return r;
186
187         dssdev->audio_state = OMAP_DSS_AUDIO_ENABLED;
188
189         return 0;
190 }
191
192 static void hdmic_audio_disable(struct omap_dss_device *dssdev)
193 {
194         struct panel_drv_data *ddata = to_panel_data(dssdev);
195         struct omap_dss_device *in = ddata->in;
196
197         in->ops.hdmi->audio_disable(in);
198
199         dssdev->audio_state = OMAP_DSS_AUDIO_DISABLED;
200 }
201
202 static int hdmic_audio_start(struct omap_dss_device *dssdev)
203 {
204         struct panel_drv_data *ddata = to_panel_data(dssdev);
205         struct omap_dss_device *in = ddata->in;
206         int r;
207
208         /*
209          * No need to check the panel state. It was checked when trasitioning
210          * to AUDIO_ENABLED.
211          */
212         if (dssdev->audio_state != OMAP_DSS_AUDIO_ENABLED)
213                 return -EPERM;
214
215         r = in->ops.hdmi->audio_start(in);
216         if (r)
217                 return r;
218
219         dssdev->audio_state = OMAP_DSS_AUDIO_PLAYING;
220
221         return 0;
222 }
223
224 static void hdmic_audio_stop(struct omap_dss_device *dssdev)
225 {
226         struct panel_drv_data *ddata = to_panel_data(dssdev);
227         struct omap_dss_device *in = ddata->in;
228
229         in->ops.hdmi->audio_stop(in);
230
231         dssdev->audio_state = OMAP_DSS_AUDIO_ENABLED;
232 }
233
234 static bool hdmic_audio_supported(struct omap_dss_device *dssdev)
235 {
236         struct panel_drv_data *ddata = to_panel_data(dssdev);
237         struct omap_dss_device *in = ddata->in;
238
239         if (!omapdss_device_is_enabled(dssdev))
240                 return false;
241
242         return in->ops.hdmi->audio_supported(in);
243 }
244
245 static int hdmic_audio_config(struct omap_dss_device *dssdev,
246                 struct omap_dss_audio *audio)
247 {
248         struct panel_drv_data *ddata = to_panel_data(dssdev);
249         struct omap_dss_device *in = ddata->in;
250         int r;
251
252         /* config audio only if the display is active */
253         if (!omapdss_device_is_enabled(dssdev))
254                 return -EPERM;
255
256         r = in->ops.hdmi->audio_config(in, audio);
257         if (r)
258                 return r;
259
260         dssdev->audio_state = OMAP_DSS_AUDIO_CONFIGURED;
261
262         return 0;
263 }
264
265 static struct omap_dss_driver hdmic_driver = {
266         .connect                = hdmic_connect,
267         .disconnect             = hdmic_disconnect,
268
269         .enable                 = hdmic_enable,
270         .disable                = hdmic_disable,
271
272         .set_timings            = hdmic_set_timings,
273         .get_timings            = hdmic_get_timings,
274         .check_timings          = hdmic_check_timings,
275
276         .get_resolution         = omapdss_default_get_resolution,
277
278         .read_edid              = hdmic_read_edid,
279         .detect                 = hdmic_detect,
280
281         .audio_enable           = hdmic_audio_enable,
282         .audio_disable          = hdmic_audio_disable,
283         .audio_start            = hdmic_audio_start,
284         .audio_stop             = hdmic_audio_stop,
285         .audio_supported        = hdmic_audio_supported,
286         .audio_config           = hdmic_audio_config,
287 };
288
289 static int hdmic_probe_pdata(struct platform_device *pdev)
290 {
291         struct panel_drv_data *ddata = platform_get_drvdata(pdev);
292         struct connector_hdmi_platform_data *pdata;
293         struct omap_dss_device *in, *dssdev;
294
295         pdata = dev_get_platdata(&pdev->dev);
296
297         ddata->hpd_gpio = -ENODEV;
298
299         in = omap_dss_find_output(pdata->source);
300         if (in == NULL) {
301                 dev_err(&pdev->dev, "Failed to find video source\n");
302                 return -EPROBE_DEFER;
303         }
304
305         ddata->in = in;
306
307         dssdev = &ddata->dssdev;
308         dssdev->name = pdata->name;
309
310         return 0;
311 }
312
313 static int hdmic_probe_of(struct platform_device *pdev)
314 {
315         struct panel_drv_data *ddata = platform_get_drvdata(pdev);
316         struct device_node *node = pdev->dev.of_node;
317         struct omap_dss_device *in;
318         int gpio;
319
320         /* HPD GPIO */
321         gpio = of_get_named_gpio(node, "hpd-gpios", 0);
322         if (gpio_is_valid(gpio))
323                 ddata->hpd_gpio = gpio;
324         else
325                 ddata->hpd_gpio = -ENODEV;
326
327         in = omapdss_of_find_source_for_first_ep(node);
328         if (IS_ERR(in)) {
329                 dev_err(&pdev->dev, "failed to find video source\n");
330                 return PTR_ERR(in);
331         }
332
333         ddata->in = in;
334
335         return 0;
336 }
337
338 static int hdmic_probe(struct platform_device *pdev)
339 {
340         struct panel_drv_data *ddata;
341         struct omap_dss_device *dssdev;
342         int r;
343
344         ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
345         if (!ddata)
346                 return -ENOMEM;
347
348         platform_set_drvdata(pdev, ddata);
349         ddata->dev = &pdev->dev;
350
351         if (dev_get_platdata(&pdev->dev)) {
352                 r = hdmic_probe_pdata(pdev);
353                 if (r)
354                         return r;
355         } else if (pdev->dev.of_node) {
356                 r = hdmic_probe_of(pdev);
357                 if (r)
358                         return r;
359         } else {
360                 return -ENODEV;
361         }
362
363         if (gpio_is_valid(ddata->hpd_gpio)) {
364                 r = devm_gpio_request_one(&pdev->dev, ddata->hpd_gpio,
365                                 GPIOF_DIR_IN, "hdmi_hpd");
366                 if (r)
367                         goto err_reg;
368         }
369
370         ddata->timings = hdmic_default_timings;
371
372         dssdev = &ddata->dssdev;
373         dssdev->driver = &hdmic_driver;
374         dssdev->dev = &pdev->dev;
375         dssdev->type = OMAP_DISPLAY_TYPE_HDMI;
376         dssdev->owner = THIS_MODULE;
377         dssdev->panel.timings = hdmic_default_timings;
378
379         r = omapdss_register_display(dssdev);
380         if (r) {
381                 dev_err(&pdev->dev, "Failed to register panel\n");
382                 goto err_reg;
383         }
384
385         return 0;
386 err_reg:
387         omap_dss_put_device(ddata->in);
388         return r;
389 }
390
391 static int __exit hdmic_remove(struct platform_device *pdev)
392 {
393         struct panel_drv_data *ddata = platform_get_drvdata(pdev);
394         struct omap_dss_device *dssdev = &ddata->dssdev;
395         struct omap_dss_device *in = ddata->in;
396
397         omapdss_unregister_display(&ddata->dssdev);
398
399         hdmic_disable(dssdev);
400         hdmic_disconnect(dssdev);
401
402         omap_dss_put_device(in);
403
404         return 0;
405 }
406
407 static const struct of_device_id hdmic_of_match[] = {
408         { .compatible = "omapdss,hdmi-connector", },
409         {},
410 };
411
412 MODULE_DEVICE_TABLE(of, hdmic_of_match);
413
414 static struct platform_driver hdmi_connector_driver = {
415         .probe  = hdmic_probe,
416         .remove = __exit_p(hdmic_remove),
417         .driver = {
418                 .name   = "connector-hdmi",
419                 .owner  = THIS_MODULE,
420                 .of_match_table = hdmic_of_match,
421         },
422 };
423
424 module_platform_driver(hdmi_connector_driver);
425
426 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
427 MODULE_DESCRIPTION("HDMI Connector driver");
428 MODULE_LICENSE("GPL");