blob: 6d8cbd9e21101df8630872b18457775183ec1a33 [file] [log] [blame]
Tomi Valkeinen348077b2013-05-24 14:20:45 +03001/*
2 * Generic DVI Connector driver
3 *
Andrew F. Davisbb5cdf82017-12-05 14:29:31 -06004 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/
Tomi Valkeinen348077b2013-05-24 14:20:45 +03005 * 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
Tomi Valkeinen588fd852017-12-15 16:08:23 +020012#include <linux/gpio/consumer.h>
Tomi Valkeinen348077b2013-05-24 14:20:45 +030013#include <linux/i2c.h>
14#include <linux/module.h>
15#include <linux/platform_device.h>
16#include <linux/slab.h>
17
18#include <drm/drm_edid.h>
Tomi Valkeinen348077b2013-05-24 14:20:45 +030019
Peter Ujfalusi32043da2016-05-27 14:40:49 +030020#include "../dss/omapdss.h"
21
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +030022static const struct videomode dvic_default_vm = {
Peter Ujfalusi81899062016-09-22 14:06:46 +030023 .hactive = 640,
Peter Ujfalusifb7f3c42016-09-22 14:06:47 +030024 .vactive = 480,
Tomi Valkeinen348077b2013-05-24 14:20:45 +030025
Tomi Valkeinend8d789412013-04-10 14:12:14 +030026 .pixelclock = 23500000,
Tomi Valkeinen348077b2013-05-24 14:20:45 +030027
Peter Ujfalusi0a30e152016-09-22 14:06:49 +030028 .hfront_porch = 48,
Peter Ujfalusi4dc22502016-09-22 14:06:48 +030029 .hsync_len = 32,
Peter Ujfalusia85f4a82016-09-22 14:06:50 +030030 .hback_porch = 80,
Tomi Valkeinen348077b2013-05-24 14:20:45 +030031
Peter Ujfalusi0996c682016-09-22 14:06:52 +030032 .vfront_porch = 3,
Peter Ujfalusid5bcf0a2016-09-22 14:06:51 +030033 .vsync_len = 4,
Peter Ujfalusi458540c2016-09-22 14:06:53 +030034 .vback_porch = 7,
Tomi Valkeinen348077b2013-05-24 14:20:45 +030035
Peter Ujfalusi3fa3ab42016-09-22 14:06:58 +030036 .flags = DISPLAY_FLAGS_HSYNC_HIGH | DISPLAY_FLAGS_VSYNC_HIGH |
Peter Ujfalusid34afb72016-09-22 14:07:01 +030037 DISPLAY_FLAGS_SYNC_NEGEDGE | DISPLAY_FLAGS_DE_HIGH |
38 DISPLAY_FLAGS_PIXDATA_POSEDGE,
Tomi Valkeinen348077b2013-05-24 14:20:45 +030039};
40
41struct panel_drv_data {
42 struct omap_dss_device dssdev;
43 struct omap_dss_device *in;
44
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +030045 struct videomode vm;
Tomi Valkeinen348077b2013-05-24 14:20:45 +030046
47 struct i2c_adapter *i2c_adapter;
Tomi Valkeinen588fd852017-12-15 16:08:23 +020048
49 struct gpio_desc *hpd_gpio;
50
51 void (*hpd_cb)(void *cb_data, enum drm_connector_status status);
52 void *hpd_cb_data;
53 bool hpd_enabled;
54 /* mutex for hpd fields above */
55 struct mutex hpd_lock;
Tomi Valkeinen348077b2013-05-24 14:20:45 +030056};
57
58#define to_panel_data(x) container_of(x, struct panel_drv_data, dssdev)
59
60static int dvic_connect(struct omap_dss_device *dssdev)
61{
62 struct panel_drv_data *ddata = to_panel_data(dssdev);
Laurent Pinchartb47e6dc2018-02-11 15:07:39 +020063 struct omap_dss_device *in;
Tomi Valkeinen348077b2013-05-24 14:20:45 +030064 int r;
65
66 if (omapdss_device_is_connected(dssdev))
67 return 0;
68
Laurent Pinchartb47e6dc2018-02-11 15:07:39 +020069 in = omapdss_of_find_source_for_first_ep(dssdev->dev->of_node);
70 if (IS_ERR(in)) {
71 dev_err(dssdev->dev, "failed to find video source\n");
72 return PTR_ERR(in);
73 }
Tomi Valkeinen348077b2013-05-24 14:20:45 +030074
Laurent Pinchartb47e6dc2018-02-11 15:07:39 +020075 r = in->ops.dvi->connect(in, dssdev);
76 if (r) {
77 omap_dss_put_device(in);
78 return r;
79 }
80
81 ddata->in = in;
Tomi Valkeinen348077b2013-05-24 14:20:45 +030082 return 0;
83}
84
85static void dvic_disconnect(struct omap_dss_device *dssdev)
86{
87 struct panel_drv_data *ddata = to_panel_data(dssdev);
88 struct omap_dss_device *in = ddata->in;
89
90 if (!omapdss_device_is_connected(dssdev))
91 return;
92
93 in->ops.dvi->disconnect(in, dssdev);
Laurent Pinchartb47e6dc2018-02-11 15:07:39 +020094
95 omap_dss_put_device(in);
96 ddata->in = NULL;
Tomi Valkeinen348077b2013-05-24 14:20:45 +030097}
98
99static int dvic_enable(struct omap_dss_device *dssdev)
100{
101 struct panel_drv_data *ddata = to_panel_data(dssdev);
102 struct omap_dss_device *in = ddata->in;
103 int r;
104
105 if (!omapdss_device_is_connected(dssdev))
106 return -ENODEV;
107
108 if (omapdss_device_is_enabled(dssdev))
109 return 0;
110
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300111 in->ops.dvi->set_timings(in, &ddata->vm);
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300112
113 r = in->ops.dvi->enable(in);
114 if (r)
115 return r;
116
117 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
118
119 return 0;
120}
121
122static void dvic_disable(struct omap_dss_device *dssdev)
123{
124 struct panel_drv_data *ddata = to_panel_data(dssdev);
125 struct omap_dss_device *in = ddata->in;
126
127 if (!omapdss_device_is_enabled(dssdev))
128 return;
129
130 in->ops.dvi->disable(in);
131
132 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
133}
134
135static void dvic_set_timings(struct omap_dss_device *dssdev,
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300136 struct videomode *vm)
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300137{
138 struct panel_drv_data *ddata = to_panel_data(dssdev);
139 struct omap_dss_device *in = ddata->in;
140
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300141 ddata->vm = *vm;
142 dssdev->panel.vm = *vm;
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300143
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300144 in->ops.dvi->set_timings(in, vm);
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300145}
146
147static void dvic_get_timings(struct omap_dss_device *dssdev,
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300148 struct videomode *vm)
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300149{
150 struct panel_drv_data *ddata = to_panel_data(dssdev);
151
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300152 *vm = ddata->vm;
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300153}
154
155static int dvic_check_timings(struct omap_dss_device *dssdev,
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300156 struct videomode *vm)
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300157{
158 struct panel_drv_data *ddata = to_panel_data(dssdev);
159 struct omap_dss_device *in = ddata->in;
160
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300161 return in->ops.dvi->check_timings(in, vm);
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300162}
163
164static int dvic_ddc_read(struct i2c_adapter *adapter,
165 unsigned char *buf, u16 count, u8 offset)
166{
167 int r, retries;
168
169 for (retries = 3; retries > 0; retries--) {
170 struct i2c_msg msgs[] = {
171 {
172 .addr = DDC_ADDR,
173 .flags = 0,
174 .len = 1,
175 .buf = &offset,
176 }, {
177 .addr = DDC_ADDR,
178 .flags = I2C_M_RD,
179 .len = count,
180 .buf = buf,
181 }
182 };
183
184 r = i2c_transfer(adapter, msgs, 2);
185 if (r == 2)
186 return 0;
187
188 if (r != -EAGAIN)
189 break;
190 }
191
192 return r < 0 ? r : -EIO;
193}
194
195static int dvic_read_edid(struct omap_dss_device *dssdev,
196 u8 *edid, int len)
197{
198 struct panel_drv_data *ddata = to_panel_data(dssdev);
199 int r, l, bytes_read;
200
Tomi Valkeinen588fd852017-12-15 16:08:23 +0200201 if (ddata->hpd_gpio && !gpiod_get_value_cansleep(ddata->hpd_gpio))
202 return -ENODEV;
203
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300204 if (!ddata->i2c_adapter)
205 return -ENODEV;
206
207 l = min(EDID_LENGTH, len);
208 r = dvic_ddc_read(ddata->i2c_adapter, edid, l, 0);
209 if (r)
210 return r;
211
212 bytes_read = l;
213
214 /* if there are extensions, read second block */
215 if (len > EDID_LENGTH && edid[0x7e] > 0) {
216 l = min(EDID_LENGTH, len - EDID_LENGTH);
217
218 r = dvic_ddc_read(ddata->i2c_adapter, edid + EDID_LENGTH,
219 l, EDID_LENGTH);
220 if (r)
221 return r;
222
223 bytes_read += l;
224 }
225
226 return bytes_read;
227}
228
229static bool dvic_detect(struct omap_dss_device *dssdev)
230{
231 struct panel_drv_data *ddata = to_panel_data(dssdev);
232 unsigned char out;
233 int r;
234
Tomi Valkeinen588fd852017-12-15 16:08:23 +0200235 if (ddata->hpd_gpio)
236 return gpiod_get_value_cansleep(ddata->hpd_gpio);
237
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300238 if (!ddata->i2c_adapter)
239 return true;
240
241 r = dvic_ddc_read(ddata->i2c_adapter, &out, 1, 0);
242
243 return r == 0;
244}
245
Tomi Valkeinen588fd852017-12-15 16:08:23 +0200246static int dvic_register_hpd_cb(struct omap_dss_device *dssdev,
247 void (*cb)(void *cb_data,
248 enum drm_connector_status status),
249 void *cb_data)
250{
251 struct panel_drv_data *ddata = to_panel_data(dssdev);
252
253 if (!ddata->hpd_gpio)
254 return -ENOTSUPP;
255
256 mutex_lock(&ddata->hpd_lock);
257 ddata->hpd_cb = cb;
258 ddata->hpd_cb_data = cb_data;
259 mutex_unlock(&ddata->hpd_lock);
260 return 0;
261}
262
263static void dvic_unregister_hpd_cb(struct omap_dss_device *dssdev)
264{
265 struct panel_drv_data *ddata = to_panel_data(dssdev);
266
267 if (!ddata->hpd_gpio)
268 return;
269
270 mutex_lock(&ddata->hpd_lock);
271 ddata->hpd_cb = NULL;
272 ddata->hpd_cb_data = NULL;
273 mutex_unlock(&ddata->hpd_lock);
274}
275
276static void dvic_enable_hpd(struct omap_dss_device *dssdev)
277{
278 struct panel_drv_data *ddata = to_panel_data(dssdev);
279
280 if (!ddata->hpd_gpio)
281 return;
282
283 mutex_lock(&ddata->hpd_lock);
284 ddata->hpd_enabled = true;
285 mutex_unlock(&ddata->hpd_lock);
286}
287
288static void dvic_disable_hpd(struct omap_dss_device *dssdev)
289{
290 struct panel_drv_data *ddata = to_panel_data(dssdev);
291
292 if (!ddata->hpd_gpio)
293 return;
294
295 mutex_lock(&ddata->hpd_lock);
296 ddata->hpd_enabled = false;
297 mutex_unlock(&ddata->hpd_lock);
298}
299
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300300static struct omap_dss_driver dvic_driver = {
301 .connect = dvic_connect,
302 .disconnect = dvic_disconnect,
303
304 .enable = dvic_enable,
305 .disable = dvic_disable,
306
307 .set_timings = dvic_set_timings,
308 .get_timings = dvic_get_timings,
309 .check_timings = dvic_check_timings,
310
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300311 .read_edid = dvic_read_edid,
312 .detect = dvic_detect,
Tomi Valkeinen588fd852017-12-15 16:08:23 +0200313
314 .register_hpd_cb = dvic_register_hpd_cb,
315 .unregister_hpd_cb = dvic_unregister_hpd_cb,
316 .enable_hpd = dvic_enable_hpd,
317 .disable_hpd = dvic_disable_hpd,
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300318};
319
Tomi Valkeinen588fd852017-12-15 16:08:23 +0200320static irqreturn_t dvic_hpd_isr(int irq, void *data)
321{
322 struct panel_drv_data *ddata = data;
323
324 mutex_lock(&ddata->hpd_lock);
325 if (ddata->hpd_enabled && ddata->hpd_cb) {
326 enum drm_connector_status status;
327
328 if (dvic_detect(&ddata->dssdev))
329 status = connector_status_connected;
330 else
331 status = connector_status_disconnected;
332
333 ddata->hpd_cb(ddata->hpd_cb_data, status);
334 }
335 mutex_unlock(&ddata->hpd_lock);
336
337 return IRQ_HANDLED;
338}
339
Tomi Valkeinen2cbe2302013-07-30 10:36:29 +0300340static int dvic_probe_of(struct platform_device *pdev)
341{
342 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
343 struct device_node *node = pdev->dev.of_node;
Tomi Valkeinen2cbe2302013-07-30 10:36:29 +0300344 struct device_node *adapter_node;
345 struct i2c_adapter *adapter;
Tomi Valkeinen588fd852017-12-15 16:08:23 +0200346 struct gpio_desc *gpio;
347 int r;
348
349 gpio = devm_gpiod_get_optional(&pdev->dev, "hpd", GPIOD_IN);
350 if (IS_ERR(gpio)) {
351 dev_err(&pdev->dev, "failed to parse HPD gpio\n");
352 return PTR_ERR(gpio);
353 }
354
355 ddata->hpd_gpio = gpio;
356
357 mutex_init(&ddata->hpd_lock);
358
359 if (ddata->hpd_gpio) {
360 r = devm_request_threaded_irq(&pdev->dev,
361 gpiod_to_irq(ddata->hpd_gpio), NULL, dvic_hpd_isr,
362 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
363 "DVI HPD", ddata);
364 if (r)
365 return r;
366 }
Tomi Valkeinen2cbe2302013-07-30 10:36:29 +0300367
Tomi Valkeinen2cbe2302013-07-30 10:36:29 +0300368 adapter_node = of_parse_phandle(node, "ddc-i2c-bus", 0);
369 if (adapter_node) {
Vladimir Zapolskiyad38cc52015-09-15 16:12:33 +0300370 adapter = of_get_i2c_adapter_by_node(adapter_node);
Peter Chen0697a052016-07-15 11:17:02 +0800371 of_node_put(adapter_node);
Tomi Valkeinen2cbe2302013-07-30 10:36:29 +0300372 if (adapter == NULL) {
373 dev_err(&pdev->dev, "failed to parse ddc-i2c-bus\n");
Tomi Valkeinen2cbe2302013-07-30 10:36:29 +0300374 return -EPROBE_DEFER;
375 }
376
377 ddata->i2c_adapter = adapter;
378 }
379
380 return 0;
381}
382
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300383static int dvic_probe(struct platform_device *pdev)
384{
385 struct panel_drv_data *ddata;
386 struct omap_dss_device *dssdev;
387 int r;
388
389 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
390 if (!ddata)
391 return -ENOMEM;
392
393 platform_set_drvdata(pdev, ddata);
394
Tomi Valkeinena0d56d72016-02-18 17:18:41 +0200395 r = dvic_probe_of(pdev);
396 if (r)
397 return r;
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300398
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300399 ddata->vm = dvic_default_vm;
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300400
401 dssdev = &ddata->dssdev;
402 dssdev->driver = &dvic_driver;
403 dssdev->dev = &pdev->dev;
404 dssdev->type = OMAP_DISPLAY_TYPE_DVI;
405 dssdev->owner = THIS_MODULE;
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300406 dssdev->panel.vm = dvic_default_vm;
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300407
408 r = omapdss_register_display(dssdev);
409 if (r) {
410 dev_err(&pdev->dev, "Failed to register panel\n");
411 goto err_reg;
412 }
413
414 return 0;
415
416err_reg:
Markus Elfringbc500d62014-11-23 14:07:22 +0100417 i2c_put_adapter(ddata->i2c_adapter);
Tomi Valkeinen588fd852017-12-15 16:08:23 +0200418 mutex_destroy(&ddata->hpd_lock);
Tomi Valkeinencc9fd772013-10-18 10:46:33 +0300419
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300420 return r;
421}
422
423static int __exit dvic_remove(struct platform_device *pdev)
424{
425 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
426 struct omap_dss_device *dssdev = &ddata->dssdev;
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300427
428 omapdss_unregister_display(&ddata->dssdev);
429
430 dvic_disable(dssdev);
431 dvic_disconnect(dssdev);
432
Markus Elfringbc500d62014-11-23 14:07:22 +0100433 i2c_put_adapter(ddata->i2c_adapter);
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300434
Tomi Valkeinen588fd852017-12-15 16:08:23 +0200435 mutex_destroy(&ddata->hpd_lock);
436
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300437 return 0;
438}
439
Tomi Valkeinen2cbe2302013-07-30 10:36:29 +0300440static const struct of_device_id dvic_of_match[] = {
441 { .compatible = "omapdss,dvi-connector", },
442 {},
443};
444
445MODULE_DEVICE_TABLE(of, dvic_of_match);
446
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300447static struct platform_driver dvi_connector_driver = {
448 .probe = dvic_probe,
449 .remove = __exit_p(dvic_remove),
450 .driver = {
451 .name = "connector-dvi",
Tomi Valkeinen2cbe2302013-07-30 10:36:29 +0300452 .of_match_table = dvic_of_match,
Tomi Valkeinen422ccbd2014-10-16 09:54:25 +0300453 .suppress_bind_attrs = true,
Tomi Valkeinen348077b2013-05-24 14:20:45 +0300454 },
455};
456
457module_platform_driver(dvi_connector_driver);
458
459MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
460MODULE_DESCRIPTION("Generic DVI Connector driver");
461MODULE_LICENSE("GPL");