Merge tag 'renesas-fixes4-for-v4.13' of https://git.kernel.org/pub/scm/linux/kernel...
[sfrench/cifs-2.6.git] / drivers / gpu / drm / omapdrm / displays / 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/gpio/consumer.h>
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/of.h>
17 #include <linux/of_gpio.h>
18
19 #include <drm/drm_edid.h>
20
21 #include "../dss/omapdss.h"
22
23 static const struct videomode hdmic_default_vm = {
24         .hactive        = 640,
25         .vactive        = 480,
26         .pixelclock     = 25175000,
27         .hsync_len      = 96,
28         .hfront_porch   = 16,
29         .hback_porch    = 48,
30         .vsync_len      = 2,
31         .vfront_porch   = 11,
32         .vback_porch    = 31,
33
34         .flags          = DISPLAY_FLAGS_HSYNC_LOW | DISPLAY_FLAGS_VSYNC_LOW,
35 };
36
37 struct panel_drv_data {
38         struct omap_dss_device dssdev;
39         struct omap_dss_device *in;
40
41         struct device *dev;
42
43         struct videomode vm;
44
45         int hpd_gpio;
46 };
47
48 #define to_panel_data(x) container_of(x, struct panel_drv_data, dssdev)
49
50 static int hdmic_connect(struct omap_dss_device *dssdev)
51 {
52         struct panel_drv_data *ddata = to_panel_data(dssdev);
53         struct omap_dss_device *in = ddata->in;
54         int r;
55
56         dev_dbg(ddata->dev, "connect\n");
57
58         if (omapdss_device_is_connected(dssdev))
59                 return 0;
60
61         r = in->ops.hdmi->connect(in, dssdev);
62         if (r)
63                 return r;
64
65         return 0;
66 }
67
68 static void hdmic_disconnect(struct omap_dss_device *dssdev)
69 {
70         struct panel_drv_data *ddata = to_panel_data(dssdev);
71         struct omap_dss_device *in = ddata->in;
72
73         dev_dbg(ddata->dev, "disconnect\n");
74
75         if (!omapdss_device_is_connected(dssdev))
76                 return;
77
78         in->ops.hdmi->disconnect(in, dssdev);
79 }
80
81 static int hdmic_enable(struct omap_dss_device *dssdev)
82 {
83         struct panel_drv_data *ddata = to_panel_data(dssdev);
84         struct omap_dss_device *in = ddata->in;
85         int r;
86
87         dev_dbg(ddata->dev, "enable\n");
88
89         if (!omapdss_device_is_connected(dssdev))
90                 return -ENODEV;
91
92         if (omapdss_device_is_enabled(dssdev))
93                 return 0;
94
95         in->ops.hdmi->set_timings(in, &ddata->vm);
96
97         r = in->ops.hdmi->enable(in);
98         if (r)
99                 return r;
100
101         dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
102
103         return r;
104 }
105
106 static void hdmic_disable(struct omap_dss_device *dssdev)
107 {
108         struct panel_drv_data *ddata = to_panel_data(dssdev);
109         struct omap_dss_device *in = ddata->in;
110
111         dev_dbg(ddata->dev, "disable\n");
112
113         if (!omapdss_device_is_enabled(dssdev))
114                 return;
115
116         in->ops.hdmi->disable(in);
117
118         dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
119 }
120
121 static void hdmic_set_timings(struct omap_dss_device *dssdev,
122                               struct videomode *vm)
123 {
124         struct panel_drv_data *ddata = to_panel_data(dssdev);
125         struct omap_dss_device *in = ddata->in;
126
127         ddata->vm = *vm;
128         dssdev->panel.vm = *vm;
129
130         in->ops.hdmi->set_timings(in, vm);
131 }
132
133 static void hdmic_get_timings(struct omap_dss_device *dssdev,
134                               struct videomode *vm)
135 {
136         struct panel_drv_data *ddata = to_panel_data(dssdev);
137
138         *vm = ddata->vm;
139 }
140
141 static int hdmic_check_timings(struct omap_dss_device *dssdev,
142                                struct videomode *vm)
143 {
144         struct panel_drv_data *ddata = to_panel_data(dssdev);
145         struct omap_dss_device *in = ddata->in;
146
147         return in->ops.hdmi->check_timings(in, vm);
148 }
149
150 static int hdmic_read_edid(struct omap_dss_device *dssdev,
151                 u8 *edid, int len)
152 {
153         struct panel_drv_data *ddata = to_panel_data(dssdev);
154         struct omap_dss_device *in = ddata->in;
155
156         return in->ops.hdmi->read_edid(in, edid, len);
157 }
158
159 static bool hdmic_detect(struct omap_dss_device *dssdev)
160 {
161         struct panel_drv_data *ddata = to_panel_data(dssdev);
162         struct omap_dss_device *in = ddata->in;
163
164         if (gpio_is_valid(ddata->hpd_gpio))
165                 return gpio_get_value_cansleep(ddata->hpd_gpio);
166         else
167                 return in->ops.hdmi->detect(in);
168 }
169
170 static int hdmic_set_hdmi_mode(struct omap_dss_device *dssdev, bool hdmi_mode)
171 {
172         struct panel_drv_data *ddata = to_panel_data(dssdev);
173         struct omap_dss_device *in = ddata->in;
174
175         return in->ops.hdmi->set_hdmi_mode(in, hdmi_mode);
176 }
177
178 static int hdmic_set_infoframe(struct omap_dss_device *dssdev,
179                 const struct hdmi_avi_infoframe *avi)
180 {
181         struct panel_drv_data *ddata = to_panel_data(dssdev);
182         struct omap_dss_device *in = ddata->in;
183
184         return in->ops.hdmi->set_infoframe(in, avi);
185 }
186
187 static struct omap_dss_driver hdmic_driver = {
188         .connect                = hdmic_connect,
189         .disconnect             = hdmic_disconnect,
190
191         .enable                 = hdmic_enable,
192         .disable                = hdmic_disable,
193
194         .set_timings            = hdmic_set_timings,
195         .get_timings            = hdmic_get_timings,
196         .check_timings          = hdmic_check_timings,
197
198         .read_edid              = hdmic_read_edid,
199         .detect                 = hdmic_detect,
200         .set_hdmi_mode          = hdmic_set_hdmi_mode,
201         .set_hdmi_infoframe     = hdmic_set_infoframe,
202 };
203
204 static int hdmic_probe_of(struct platform_device *pdev)
205 {
206         struct panel_drv_data *ddata = platform_get_drvdata(pdev);
207         struct device_node *node = pdev->dev.of_node;
208         struct omap_dss_device *in;
209         int gpio;
210
211         /* HPD GPIO */
212         gpio = of_get_named_gpio(node, "hpd-gpios", 0);
213         if (gpio_is_valid(gpio))
214                 ddata->hpd_gpio = gpio;
215         else
216                 ddata->hpd_gpio = -ENODEV;
217
218         in = omapdss_of_find_source_for_first_ep(node);
219         if (IS_ERR(in)) {
220                 dev_err(&pdev->dev, "failed to find video source\n");
221                 return PTR_ERR(in);
222         }
223
224         ddata->in = in;
225
226         return 0;
227 }
228
229 static int hdmic_probe(struct platform_device *pdev)
230 {
231         struct panel_drv_data *ddata;
232         struct omap_dss_device *dssdev;
233         int r;
234
235         ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
236         if (!ddata)
237                 return -ENOMEM;
238
239         platform_set_drvdata(pdev, ddata);
240         ddata->dev = &pdev->dev;
241
242         if (!pdev->dev.of_node)
243                 return -ENODEV;
244
245         r = hdmic_probe_of(pdev);
246         if (r)
247                 return r;
248
249         if (gpio_is_valid(ddata->hpd_gpio)) {
250                 r = devm_gpio_request_one(&pdev->dev, ddata->hpd_gpio,
251                                 GPIOF_DIR_IN, "hdmi_hpd");
252                 if (r)
253                         goto err_reg;
254         }
255
256         ddata->vm = hdmic_default_vm;
257
258         dssdev = &ddata->dssdev;
259         dssdev->driver = &hdmic_driver;
260         dssdev->dev = &pdev->dev;
261         dssdev->type = OMAP_DISPLAY_TYPE_HDMI;
262         dssdev->owner = THIS_MODULE;
263         dssdev->panel.vm = hdmic_default_vm;
264
265         r = omapdss_register_display(dssdev);
266         if (r) {
267                 dev_err(&pdev->dev, "Failed to register panel\n");
268                 goto err_reg;
269         }
270
271         return 0;
272 err_reg:
273         omap_dss_put_device(ddata->in);
274         return r;
275 }
276
277 static int __exit hdmic_remove(struct platform_device *pdev)
278 {
279         struct panel_drv_data *ddata = platform_get_drvdata(pdev);
280         struct omap_dss_device *dssdev = &ddata->dssdev;
281         struct omap_dss_device *in = ddata->in;
282
283         omapdss_unregister_display(&ddata->dssdev);
284
285         hdmic_disable(dssdev);
286         hdmic_disconnect(dssdev);
287
288         omap_dss_put_device(in);
289
290         return 0;
291 }
292
293 static const struct of_device_id hdmic_of_match[] = {
294         { .compatible = "omapdss,hdmi-connector", },
295         {},
296 };
297
298 MODULE_DEVICE_TABLE(of, hdmic_of_match);
299
300 static struct platform_driver hdmi_connector_driver = {
301         .probe  = hdmic_probe,
302         .remove = __exit_p(hdmic_remove),
303         .driver = {
304                 .name   = "connector-hdmi",
305                 .of_match_table = hdmic_of_match,
306                 .suppress_bind_attrs = true,
307         },
308 };
309
310 module_platform_driver(hdmi_connector_driver);
311
312 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
313 MODULE_DESCRIPTION("HDMI Connector driver");
314 MODULE_LICENSE("GPL");