blob: 05fa24a518c87783b16e3faed17edb7167569760 [file] [log] [blame]
Tomi Valkeinen348077b2013-05-24 14:20:45 +03001/*
2 * Generic DVI 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/i2c.h>
13#include <linux/module.h>
14#include <linux/platform_device.h>
15#include <linux/slab.h>
16
17#include <drm/drm_edid.h>
Tomi Valkeinen348077b2013-05-24 14:20:45 +030018
Peter Ujfalusi32043da2016-05-27 14:40:49 +030019#include "../dss/omapdss.h"
20
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +030021static const struct videomode dvic_default_vm = {
Peter Ujfalusi81899062016-09-22 14:06:46 +030022 .hactive = 640,
Peter Ujfalusifb7f3c42016-09-22 14:06:47 +030023 .vactive = 480,
Tomi Valkeinen348077b2013-05-24 14:20:45 +030024
Tomi Valkeinend8d789412013-04-10 14:12:14 +030025 .pixelclock = 23500000,
Tomi Valkeinen348077b2013-05-24 14:20:45 +030026
Peter Ujfalusi0a30e152016-09-22 14:06:49 +030027 .hfront_porch = 48,
Peter Ujfalusi4dc22502016-09-22 14:06:48 +030028 .hsync_len = 32,
Peter Ujfalusia85f4a82016-09-22 14:06:50 +030029 .hback_porch = 80,
Tomi Valkeinen348077b2013-05-24 14:20:45 +030030
Peter Ujfalusi0996c682016-09-22 14:06:52 +030031 .vfront_porch = 3,
Peter Ujfalusid5bcf0a2016-09-22 14:06:51 +030032 .vsync_len = 4,
Peter Ujfalusi458540c2016-09-22 14:06:53 +030033 .vback_porch = 7,
Tomi Valkeinen348077b2013-05-24 14:20:45 +030034
Peter Ujfalusi3fa3ab42016-09-22 14:06:58 +030035 .flags = DISPLAY_FLAGS_HSYNC_HIGH | DISPLAY_FLAGS_VSYNC_HIGH |
Peter Ujfalusid34afb72016-09-22 14:07:01 +030036 DISPLAY_FLAGS_SYNC_NEGEDGE | DISPLAY_FLAGS_DE_HIGH |
37 DISPLAY_FLAGS_PIXDATA_POSEDGE,
Tomi Valkeinen348077b2013-05-24 14:20:45 +030038};
39
40struct panel_drv_data {
41 struct omap_dss_device dssdev;
42 struct omap_dss_device *in;
43
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +030044 struct videomode vm;
Tomi Valkeinen348077b2013-05-24 14:20:45 +030045
46 struct i2c_adapter *i2c_adapter;
47};
48
49#define to_panel_data(x) container_of(x, struct panel_drv_data, dssdev)
50
51static int dvic_connect(struct omap_dss_device *dssdev)
52{
53 struct panel_drv_data *ddata = to_panel_data(dssdev);
54 struct omap_dss_device *in = ddata->in;
55 int r;
56
57 if (omapdss_device_is_connected(dssdev))
58 return 0;
59
60 r = in->ops.dvi->connect(in, dssdev);
61 if (r)
62 return r;
63
64 return 0;
65}
66
67static void dvic_disconnect(struct omap_dss_device *dssdev)
68{
69 struct panel_drv_data *ddata = to_panel_data(dssdev);
70 struct omap_dss_device *in = ddata->in;
71
72 if (!omapdss_device_is_connected(dssdev))
73 return;
74
75 in->ops.dvi->disconnect(in, dssdev);
76}
77
78static int dvic_enable(struct omap_dss_device *dssdev)
79{
80 struct panel_drv_data *ddata = to_panel_data(dssdev);
81 struct omap_dss_device *in = ddata->in;
82 int r;
83
84 if (!omapdss_device_is_connected(dssdev))
85 return -ENODEV;
86
87 if (omapdss_device_is_enabled(dssdev))
88 return 0;
89
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +030090 in->ops.dvi->set_timings(in, &ddata->vm);
Tomi Valkeinen348077b2013-05-24 14:20:45 +030091
92 r = in->ops.dvi->enable(in);
93 if (r)
94 return r;
95
96 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
97
98 return 0;
99}
100
101static void dvic_disable(struct omap_dss_device *dssdev)
102{
103 struct panel_drv_data *ddata = to_panel_data(dssdev);
104 struct omap_dss_device *in = ddata->in;
105
106 if (!omapdss_device_is_enabled(dssdev))
107 return;
108
109 in->ops.dvi->disable(in);
110
111 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
112}
113
114static void dvic_set_timings(struct omap_dss_device *dssdev,
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300115 struct videomode *vm)
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300116{
117 struct panel_drv_data *ddata = to_panel_data(dssdev);
118 struct omap_dss_device *in = ddata->in;
119
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300120 ddata->vm = *vm;
121 dssdev->panel.vm = *vm;
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300122
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300123 in->ops.dvi->set_timings(in, vm);
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300124}
125
126static void dvic_get_timings(struct omap_dss_device *dssdev,
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300127 struct videomode *vm)
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300128{
129 struct panel_drv_data *ddata = to_panel_data(dssdev);
130
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300131 *vm = ddata->vm;
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300132}
133
134static int dvic_check_timings(struct omap_dss_device *dssdev,
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300135 struct videomode *vm)
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300136{
137 struct panel_drv_data *ddata = to_panel_data(dssdev);
138 struct omap_dss_device *in = ddata->in;
139
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300140 return in->ops.dvi->check_timings(in, vm);
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300141}
142
143static int dvic_ddc_read(struct i2c_adapter *adapter,
144 unsigned char *buf, u16 count, u8 offset)
145{
146 int r, retries;
147
148 for (retries = 3; retries > 0; retries--) {
149 struct i2c_msg msgs[] = {
150 {
151 .addr = DDC_ADDR,
152 .flags = 0,
153 .len = 1,
154 .buf = &offset,
155 }, {
156 .addr = DDC_ADDR,
157 .flags = I2C_M_RD,
158 .len = count,
159 .buf = buf,
160 }
161 };
162
163 r = i2c_transfer(adapter, msgs, 2);
164 if (r == 2)
165 return 0;
166
167 if (r != -EAGAIN)
168 break;
169 }
170
171 return r < 0 ? r : -EIO;
172}
173
174static int dvic_read_edid(struct omap_dss_device *dssdev,
175 u8 *edid, int len)
176{
177 struct panel_drv_data *ddata = to_panel_data(dssdev);
178 int r, l, bytes_read;
179
180 if (!ddata->i2c_adapter)
181 return -ENODEV;
182
183 l = min(EDID_LENGTH, len);
184 r = dvic_ddc_read(ddata->i2c_adapter, edid, l, 0);
185 if (r)
186 return r;
187
188 bytes_read = l;
189
190 /* if there are extensions, read second block */
191 if (len > EDID_LENGTH && edid[0x7e] > 0) {
192 l = min(EDID_LENGTH, len - EDID_LENGTH);
193
194 r = dvic_ddc_read(ddata->i2c_adapter, edid + EDID_LENGTH,
195 l, EDID_LENGTH);
196 if (r)
197 return r;
198
199 bytes_read += l;
200 }
201
202 return bytes_read;
203}
204
205static bool dvic_detect(struct omap_dss_device *dssdev)
206{
207 struct panel_drv_data *ddata = to_panel_data(dssdev);
208 unsigned char out;
209 int r;
210
211 if (!ddata->i2c_adapter)
212 return true;
213
214 r = dvic_ddc_read(ddata->i2c_adapter, &out, 1, 0);
215
216 return r == 0;
217}
218
219static struct omap_dss_driver dvic_driver = {
220 .connect = dvic_connect,
221 .disconnect = dvic_disconnect,
222
223 .enable = dvic_enable,
224 .disable = dvic_disable,
225
226 .set_timings = dvic_set_timings,
227 .get_timings = dvic_get_timings,
228 .check_timings = dvic_check_timings,
229
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300230 .read_edid = dvic_read_edid,
231 .detect = dvic_detect,
232};
233
Tomi Valkeinen2cbe2302013-07-30 10:36:29 +0300234static int dvic_probe_of(struct platform_device *pdev)
235{
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;
241
242 in = omapdss_of_find_source_for_first_ep(node);
243 if (IS_ERR(in)) {
244 dev_err(&pdev->dev, "failed to find video source\n");
245 return PTR_ERR(in);
246 }
247
248 ddata->in = in;
249
250 adapter_node = of_parse_phandle(node, "ddc-i2c-bus", 0);
251 if (adapter_node) {
Vladimir Zapolskiyad38cc52015-09-15 16:12:33 +0300252 adapter = of_get_i2c_adapter_by_node(adapter_node);
Peter Chen0697a052016-07-15 11:17:02 +0800253 of_node_put(adapter_node);
Tomi Valkeinen2cbe2302013-07-30 10:36:29 +0300254 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;
258 }
259
260 ddata->i2c_adapter = adapter;
261 }
262
263 return 0;
264}
265
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300266static int dvic_probe(struct platform_device *pdev)
267{
268 struct panel_drv_data *ddata;
269 struct omap_dss_device *dssdev;
270 int r;
271
272 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
273 if (!ddata)
274 return -ENOMEM;
275
276 platform_set_drvdata(pdev, ddata);
277
Tomi Valkeinen55f6fca2016-03-15 14:55:53 +0200278 if (!pdev->dev.of_node)
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300279 return -ENODEV;
Tomi Valkeinena0d56d72016-02-18 17:18:41 +0200280
281 r = dvic_probe_of(pdev);
282 if (r)
283 return r;
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300284
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300285 ddata->vm = dvic_default_vm;
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300286
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;
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300292 dssdev->panel.vm = dvic_default_vm;
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300293
294 r = omapdss_register_display(dssdev);
295 if (r) {
296 dev_err(&pdev->dev, "Failed to register panel\n");
297 goto err_reg;
298 }
299
300 return 0;
301
302err_reg:
303 omap_dss_put_device(ddata->in);
Tomi Valkeinencc9fd772013-10-18 10:46:33 +0300304
Markus Elfringbc500d62014-11-23 14:07:22 +0100305 i2c_put_adapter(ddata->i2c_adapter);
Tomi Valkeinencc9fd772013-10-18 10:46:33 +0300306
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300307 return r;
308}
309
310static int __exit dvic_remove(struct platform_device *pdev)
311{
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;
315
316 omapdss_unregister_display(&ddata->dssdev);
317
318 dvic_disable(dssdev);
319 dvic_disconnect(dssdev);
320
321 omap_dss_put_device(in);
322
Markus Elfringbc500d62014-11-23 14:07:22 +0100323 i2c_put_adapter(ddata->i2c_adapter);
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300324
325 return 0;
326}
327
Tomi Valkeinen2cbe2302013-07-30 10:36:29 +0300328static const struct of_device_id dvic_of_match[] = {
329 { .compatible = "omapdss,dvi-connector", },
330 {},
331};
332
333MODULE_DEVICE_TABLE(of, dvic_of_match);
334
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300335static struct platform_driver dvi_connector_driver = {
336 .probe = dvic_probe,
337 .remove = __exit_p(dvic_remove),
338 .driver = {
339 .name = "connector-dvi",
Tomi Valkeinen2cbe2302013-07-30 10:36:29 +0300340 .of_match_table = dvic_of_match,
Tomi Valkeinen422ccbd2014-10-16 09:54:25 +0300341 .suppress_bind_attrs = true,
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300342 },
343};
344
345module_platform_driver(dvi_connector_driver);
346
347MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
348MODULE_DESCRIPTION("Generic DVI Connector driver");
349MODULE_LICENSE("GPL");