Input: xpad - add support for Xbox1 PDP Camo series gamepad
[linux/fpc-iii.git] / drivers / gpu / drm / omapdrm / displays / connector-dvi.c
blob684b7aeda411a859299930d174e953450df58775
1 /*
2 * Generic DVI Connector driver
4 * Copyright (C) 2013 Texas Instruments
5 * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
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.
12 #include <linux/i2c.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
17 #include <drm/drm_edid.h>
18 #include <video/omap-panel-data.h>
20 #include "../dss/omapdss.h"
22 static const struct omap_video_timings dvic_default_timings = {
23 .x_res = 640,
24 .y_res = 480,
26 .pixelclock = 23500000,
28 .hfp = 48,
29 .hsw = 32,
30 .hbp = 80,
32 .vfp = 3,
33 .vsw = 4,
34 .vbp = 7,
36 .vsync_level = OMAPDSS_SIG_ACTIVE_HIGH,
37 .hsync_level = OMAPDSS_SIG_ACTIVE_HIGH,
38 .data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE,
39 .de_level = OMAPDSS_SIG_ACTIVE_HIGH,
40 .sync_pclk_edge = OMAPDSS_DRIVE_SIG_FALLING_EDGE,
43 struct panel_drv_data {
44 struct omap_dss_device dssdev;
45 struct omap_dss_device *in;
47 struct omap_video_timings timings;
49 struct i2c_adapter *i2c_adapter;
52 #define to_panel_data(x) container_of(x, struct panel_drv_data, dssdev)
54 static int dvic_connect(struct omap_dss_device *dssdev)
56 struct panel_drv_data *ddata = to_panel_data(dssdev);
57 struct omap_dss_device *in = ddata->in;
58 int r;
60 if (omapdss_device_is_connected(dssdev))
61 return 0;
63 r = in->ops.dvi->connect(in, dssdev);
64 if (r)
65 return r;
67 return 0;
70 static void dvic_disconnect(struct omap_dss_device *dssdev)
72 struct panel_drv_data *ddata = to_panel_data(dssdev);
73 struct omap_dss_device *in = ddata->in;
75 if (!omapdss_device_is_connected(dssdev))
76 return;
78 in->ops.dvi->disconnect(in, dssdev);
81 static int dvic_enable(struct omap_dss_device *dssdev)
83 struct panel_drv_data *ddata = to_panel_data(dssdev);
84 struct omap_dss_device *in = ddata->in;
85 int r;
87 if (!omapdss_device_is_connected(dssdev))
88 return -ENODEV;
90 if (omapdss_device_is_enabled(dssdev))
91 return 0;
93 in->ops.dvi->set_timings(in, &ddata->timings);
95 r = in->ops.dvi->enable(in);
96 if (r)
97 return r;
99 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
101 return 0;
104 static void dvic_disable(struct omap_dss_device *dssdev)
106 struct panel_drv_data *ddata = to_panel_data(dssdev);
107 struct omap_dss_device *in = ddata->in;
109 if (!omapdss_device_is_enabled(dssdev))
110 return;
112 in->ops.dvi->disable(in);
114 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
117 static void dvic_set_timings(struct omap_dss_device *dssdev,
118 struct omap_video_timings *timings)
120 struct panel_drv_data *ddata = to_panel_data(dssdev);
121 struct omap_dss_device *in = ddata->in;
123 ddata->timings = *timings;
124 dssdev->panel.timings = *timings;
126 in->ops.dvi->set_timings(in, timings);
129 static void dvic_get_timings(struct omap_dss_device *dssdev,
130 struct omap_video_timings *timings)
132 struct panel_drv_data *ddata = to_panel_data(dssdev);
134 *timings = ddata->timings;
137 static int dvic_check_timings(struct omap_dss_device *dssdev,
138 struct omap_video_timings *timings)
140 struct panel_drv_data *ddata = to_panel_data(dssdev);
141 struct omap_dss_device *in = ddata->in;
143 return in->ops.dvi->check_timings(in, timings);
146 static int dvic_ddc_read(struct i2c_adapter *adapter,
147 unsigned char *buf, u16 count, u8 offset)
149 int r, retries;
151 for (retries = 3; retries > 0; retries--) {
152 struct i2c_msg msgs[] = {
154 .addr = DDC_ADDR,
155 .flags = 0,
156 .len = 1,
157 .buf = &offset,
158 }, {
159 .addr = DDC_ADDR,
160 .flags = I2C_M_RD,
161 .len = count,
162 .buf = buf,
166 r = i2c_transfer(adapter, msgs, 2);
167 if (r == 2)
168 return 0;
170 if (r != -EAGAIN)
171 break;
174 return r < 0 ? r : -EIO;
177 static int dvic_read_edid(struct omap_dss_device *dssdev,
178 u8 *edid, int len)
180 struct panel_drv_data *ddata = to_panel_data(dssdev);
181 int r, l, bytes_read;
183 if (!ddata->i2c_adapter)
184 return -ENODEV;
186 l = min(EDID_LENGTH, len);
187 r = dvic_ddc_read(ddata->i2c_adapter, edid, l, 0);
188 if (r)
189 return r;
191 bytes_read = l;
193 /* if there are extensions, read second block */
194 if (len > EDID_LENGTH && edid[0x7e] > 0) {
195 l = min(EDID_LENGTH, len - EDID_LENGTH);
197 r = dvic_ddc_read(ddata->i2c_adapter, edid + EDID_LENGTH,
198 l, EDID_LENGTH);
199 if (r)
200 return r;
202 bytes_read += l;
205 return bytes_read;
208 static bool dvic_detect(struct omap_dss_device *dssdev)
210 struct panel_drv_data *ddata = to_panel_data(dssdev);
211 unsigned char out;
212 int r;
214 if (!ddata->i2c_adapter)
215 return true;
217 r = dvic_ddc_read(ddata->i2c_adapter, &out, 1, 0);
219 return r == 0;
222 static struct omap_dss_driver dvic_driver = {
223 .connect = dvic_connect,
224 .disconnect = dvic_disconnect,
226 .enable = dvic_enable,
227 .disable = dvic_disable,
229 .set_timings = dvic_set_timings,
230 .get_timings = dvic_get_timings,
231 .check_timings = dvic_check_timings,
233 .get_resolution = omapdss_default_get_resolution,
235 .read_edid = dvic_read_edid,
236 .detect = dvic_detect,
239 static int dvic_probe_of(struct platform_device *pdev)
241 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
242 struct device_node *node = pdev->dev.of_node;
243 struct omap_dss_device *in;
244 struct device_node *adapter_node;
245 struct i2c_adapter *adapter;
247 in = omapdss_of_find_source_for_first_ep(node);
248 if (IS_ERR(in)) {
249 dev_err(&pdev->dev, "failed to find video source\n");
250 return PTR_ERR(in);
253 ddata->in = in;
255 adapter_node = of_parse_phandle(node, "ddc-i2c-bus", 0);
256 if (adapter_node) {
257 adapter = of_get_i2c_adapter_by_node(adapter_node);
258 of_node_put(adapter_node);
259 if (adapter == NULL) {
260 dev_err(&pdev->dev, "failed to parse ddc-i2c-bus\n");
261 omap_dss_put_device(ddata->in);
262 return -EPROBE_DEFER;
265 ddata->i2c_adapter = adapter;
268 return 0;
271 static int dvic_probe(struct platform_device *pdev)
273 struct panel_drv_data *ddata;
274 struct omap_dss_device *dssdev;
275 int r;
277 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
278 if (!ddata)
279 return -ENOMEM;
281 platform_set_drvdata(pdev, ddata);
283 if (!pdev->dev.of_node)
284 return -ENODEV;
286 r = dvic_probe_of(pdev);
287 if (r)
288 return r;
290 ddata->timings = dvic_default_timings;
292 dssdev = &ddata->dssdev;
293 dssdev->driver = &dvic_driver;
294 dssdev->dev = &pdev->dev;
295 dssdev->type = OMAP_DISPLAY_TYPE_DVI;
296 dssdev->owner = THIS_MODULE;
297 dssdev->panel.timings = dvic_default_timings;
299 r = omapdss_register_display(dssdev);
300 if (r) {
301 dev_err(&pdev->dev, "Failed to register panel\n");
302 goto err_reg;
305 return 0;
307 err_reg:
308 omap_dss_put_device(ddata->in);
310 i2c_put_adapter(ddata->i2c_adapter);
312 return r;
315 static int __exit dvic_remove(struct platform_device *pdev)
317 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
318 struct omap_dss_device *dssdev = &ddata->dssdev;
319 struct omap_dss_device *in = ddata->in;
321 omapdss_unregister_display(&ddata->dssdev);
323 dvic_disable(dssdev);
324 dvic_disconnect(dssdev);
326 omap_dss_put_device(in);
328 i2c_put_adapter(ddata->i2c_adapter);
330 return 0;
333 static const struct of_device_id dvic_of_match[] = {
334 { .compatible = "omapdss,dvi-connector", },
338 MODULE_DEVICE_TABLE(of, dvic_of_match);
340 static struct platform_driver dvi_connector_driver = {
341 .probe = dvic_probe,
342 .remove = __exit_p(dvic_remove),
343 .driver = {
344 .name = "connector-dvi",
345 .of_match_table = dvic_of_match,
346 .suppress_bind_attrs = true,
350 module_platform_driver(dvi_connector_driver);
352 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
353 MODULE_DESCRIPTION("Generic DVI Connector driver");
354 MODULE_LICENSE("GPL");