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>
19 #include "../dss/omapdss.h"
21 static const struct videomode dvic_default_vm = {
25 .pixelclock = 23500000,
35 .flags = DISPLAY_FLAGS_HSYNC_HIGH | DISPLAY_FLAGS_VSYNC_HIGH |
36 DISPLAY_FLAGS_SYNC_NEGEDGE | DISPLAY_FLAGS_DE_HIGH |
37 DISPLAY_FLAGS_PIXDATA_POSEDGE,
40 struct panel_drv_data {
41 struct omap_dss_device dssdev;
42 struct omap_dss_device *in;
46 struct i2c_adapter *i2c_adapter;
49 #define to_panel_data(x) container_of(x, struct panel_drv_data, dssdev)
51 static int dvic_connect(struct omap_dss_device *dssdev)
53 struct panel_drv_data *ddata = to_panel_data(dssdev);
54 struct omap_dss_device *in = ddata->in;
57 if (omapdss_device_is_connected(dssdev))
60 r = in->ops.dvi->connect(in, dssdev);
67 static void dvic_disconnect(struct omap_dss_device *dssdev)
69 struct panel_drv_data *ddata = to_panel_data(dssdev);
70 struct omap_dss_device *in = ddata->in;
72 if (!omapdss_device_is_connected(dssdev))
75 in->ops.dvi->disconnect(in, dssdev);
78 static int dvic_enable(struct omap_dss_device *dssdev)
80 struct panel_drv_data *ddata = to_panel_data(dssdev);
81 struct omap_dss_device *in = ddata->in;
84 if (!omapdss_device_is_connected(dssdev))
87 if (omapdss_device_is_enabled(dssdev))
90 in->ops.dvi->set_timings(in, &ddata->vm);
92 r = in->ops.dvi->enable(in);
96 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
101 static void dvic_disable(struct omap_dss_device *dssdev)
103 struct panel_drv_data *ddata = to_panel_data(dssdev);
104 struct omap_dss_device *in = ddata->in;
106 if (!omapdss_device_is_enabled(dssdev))
109 in->ops.dvi->disable(in);
111 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
114 static void dvic_set_timings(struct omap_dss_device *dssdev,
115 struct videomode *vm)
117 struct panel_drv_data *ddata = to_panel_data(dssdev);
118 struct omap_dss_device *in = ddata->in;
121 dssdev->panel.vm = *vm;
123 in->ops.dvi->set_timings(in, vm);
126 static void dvic_get_timings(struct omap_dss_device *dssdev,
127 struct videomode *vm)
129 struct panel_drv_data *ddata = to_panel_data(dssdev);
134 static int dvic_check_timings(struct omap_dss_device *dssdev,
135 struct videomode *vm)
137 struct panel_drv_data *ddata = to_panel_data(dssdev);
138 struct omap_dss_device *in = ddata->in;
140 return in->ops.dvi->check_timings(in, vm);
143 static int dvic_ddc_read(struct i2c_adapter *adapter,
144 unsigned char *buf, u16 count, u8 offset)
148 for (retries = 3; retries > 0; retries--) {
149 struct i2c_msg msgs[] = {
163 r = i2c_transfer(adapter, msgs, 2);
171 return r < 0 ? r : -EIO;
174 static int dvic_read_edid(struct omap_dss_device *dssdev,
177 struct panel_drv_data *ddata = to_panel_data(dssdev);
178 int r, l, bytes_read;
180 if (!ddata->i2c_adapter)
183 l = min(EDID_LENGTH, len);
184 r = dvic_ddc_read(ddata->i2c_adapter, edid, l, 0);
190 /* if there are extensions, read second block */
191 if (len > EDID_LENGTH && edid[0x7e] > 0) {
192 l = min(EDID_LENGTH, len - EDID_LENGTH);
194 r = dvic_ddc_read(ddata->i2c_adapter, edid + EDID_LENGTH,
205 static bool dvic_detect(struct omap_dss_device *dssdev)
207 struct panel_drv_data *ddata = to_panel_data(dssdev);
211 if (!ddata->i2c_adapter)
214 r = dvic_ddc_read(ddata->i2c_adapter, &out, 1, 0);
219 static struct omap_dss_driver dvic_driver = {
220 .connect = dvic_connect,
221 .disconnect = dvic_disconnect,
223 .enable = dvic_enable,
224 .disable = dvic_disable,
226 .set_timings = dvic_set_timings,
227 .get_timings = dvic_get_timings,
228 .check_timings = dvic_check_timings,
230 .read_edid = dvic_read_edid,
231 .detect = dvic_detect,
234 static int dvic_probe_of(struct platform_device *pdev)
236 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
237 struct device_node *node = pdev->dev.of_node;
238 struct omap_dss_device *in;
239 struct device_node *adapter_node;
240 struct i2c_adapter *adapter;
242 in = omapdss_of_find_source_for_first_ep(node);
244 dev_err(&pdev->dev, "failed to find video source\n");
250 adapter_node = of_parse_phandle(node, "ddc-i2c-bus", 0);
252 adapter = of_get_i2c_adapter_by_node(adapter_node);
253 of_node_put(adapter_node);
254 if (adapter == NULL) {
255 dev_err(&pdev->dev, "failed to parse ddc-i2c-bus\n");
256 omap_dss_put_device(ddata->in);
257 return -EPROBE_DEFER;
260 ddata->i2c_adapter = adapter;
266 static int dvic_probe(struct platform_device *pdev)
268 struct panel_drv_data *ddata;
269 struct omap_dss_device *dssdev;
272 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
276 platform_set_drvdata(pdev, ddata);
278 if (!pdev->dev.of_node)
281 r = dvic_probe_of(pdev);
285 ddata->vm = dvic_default_vm;
287 dssdev = &ddata->dssdev;
288 dssdev->driver = &dvic_driver;
289 dssdev->dev = &pdev->dev;
290 dssdev->type = OMAP_DISPLAY_TYPE_DVI;
291 dssdev->owner = THIS_MODULE;
292 dssdev->panel.vm = dvic_default_vm;
294 r = omapdss_register_display(dssdev);
296 dev_err(&pdev->dev, "Failed to register panel\n");
303 omap_dss_put_device(ddata->in);
305 i2c_put_adapter(ddata->i2c_adapter);
310 static int __exit dvic_remove(struct platform_device *pdev)
312 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
313 struct omap_dss_device *dssdev = &ddata->dssdev;
314 struct omap_dss_device *in = ddata->in;
316 omapdss_unregister_display(&ddata->dssdev);
318 dvic_disable(dssdev);
319 dvic_disconnect(dssdev);
321 omap_dss_put_device(in);
323 i2c_put_adapter(ddata->i2c_adapter);
328 static const struct of_device_id dvic_of_match[] = {
329 { .compatible = "omapdss,dvi-connector", },
333 MODULE_DEVICE_TABLE(of, dvic_of_match);
335 static struct platform_driver dvi_connector_driver = {
337 .remove = __exit_p(dvic_remove),
339 .name = "connector-dvi",
340 .of_match_table = dvic_of_match,
341 .suppress_bind_attrs = true,
345 module_platform_driver(dvi_connector_driver);
347 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
348 MODULE_DESCRIPTION("Generic DVI Connector driver");
349 MODULE_LICENSE("GPL");