blob: 8b0e71bd3ca7734f3fdd9e525641cb5609f618f1 [file] [log] [blame]
Jyri Sarhadc55ac32016-10-31 17:21:31 +02001/*
2 * Copyright (C) 2016 Texas Instruments
3 * Author: Jyri Sarha <jsarha@ti.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 *
9 */
10
Christopher Spinrathf56c9202017-03-06 22:40:43 +010011#include <linux/delay.h>
12#include <linux/fwnode.h>
13#include <linux/gpio/consumer.h>
14#include <linux/irq.h>
Jyri Sarhadc55ac32016-10-31 17:21:31 +020015#include <linux/module.h>
16#include <linux/of_graph.h>
17#include <linux/platform_device.h>
18#include <linux/i2c.h>
19
20#include <drm/drmP.h>
21#include <drm/drm_atomic_helper.h>
22#include <drm/drm_crtc.h>
Daniel Vetterfcd70cd2019-01-17 22:03:34 +010023#include <drm/drm_probe_helper.h>
Jyri Sarhadc55ac32016-10-31 17:21:31 +020024
Christopher Spinrathf56c9202017-03-06 22:40:43 +010025#define HOTPLUG_DEBOUNCE_MS 1100
26
Jyri Sarhadc55ac32016-10-31 17:21:31 +020027struct tfp410 {
28 struct drm_bridge bridge;
29 struct drm_connector connector;
Laurent Pinchart60b903c32018-09-25 16:59:28 +030030 unsigned int connector_type;
Jyri Sarhadc55ac32016-10-31 17:21:31 +020031
Peter Ujfalusi0eb27662019-04-01 15:41:43 +030032 u32 bus_format;
Jyri Sarhadc55ac32016-10-31 17:21:31 +020033 struct i2c_adapter *ddc;
Christopher Spinrathf56c9202017-03-06 22:40:43 +010034 struct gpio_desc *hpd;
Peter Ujfalusi3d31e212019-04-01 15:33:42 +030035 int hpd_irq;
Christopher Spinrathf56c9202017-03-06 22:40:43 +010036 struct delayed_work hpd_work;
Laurent Pinchart38c02db2018-10-01 18:07:48 +030037 struct gpio_desc *powerdown;
Jyri Sarhadc55ac32016-10-31 17:21:31 +020038
Laurent Pinchart897dae52018-09-27 11:29:48 +030039 struct drm_bridge_timings timings;
40
Jyri Sarhadc55ac32016-10-31 17:21:31 +020041 struct device *dev;
42};
43
44static inline struct tfp410 *
45drm_bridge_to_tfp410(struct drm_bridge *bridge)
46{
47 return container_of(bridge, struct tfp410, bridge);
48}
49
50static inline struct tfp410 *
51drm_connector_to_tfp410(struct drm_connector *connector)
52{
53 return container_of(connector, struct tfp410, connector);
54}
55
56static int tfp410_get_modes(struct drm_connector *connector)
57{
58 struct tfp410 *dvi = drm_connector_to_tfp410(connector);
59 struct edid *edid;
60 int ret;
61
62 if (!dvi->ddc)
63 goto fallback;
64
65 edid = drm_get_edid(connector, dvi->ddc);
66 if (!edid) {
67 DRM_INFO("EDID read failed. Fallback to standard modes\n");
68 goto fallback;
69 }
70
Daniel Vetterc555f022018-07-09 10:40:06 +020071 drm_connector_update_edid_property(connector, edid);
Jyri Sarhadc55ac32016-10-31 17:21:31 +020072
73 return drm_add_edid_modes(connector, edid);
74fallback:
75 /* No EDID, fallback on the XGA standard modes */
76 ret = drm_add_modes_noedid(connector, 1920, 1200);
77
78 /* And prefer a mode pretty much anything can handle */
79 drm_set_preferred_mode(connector, 1024, 768);
80
81 return ret;
82}
83
84static const struct drm_connector_helper_funcs tfp410_con_helper_funcs = {
85 .get_modes = tfp410_get_modes,
86};
87
88static enum drm_connector_status
89tfp410_connector_detect(struct drm_connector *connector, bool force)
90{
91 struct tfp410 *dvi = drm_connector_to_tfp410(connector);
92
Christopher Spinrathf56c9202017-03-06 22:40:43 +010093 if (dvi->hpd) {
94 if (gpiod_get_value_cansleep(dvi->hpd))
95 return connector_status_connected;
96 else
97 return connector_status_disconnected;
98 }
99
Jyri Sarhadc55ac32016-10-31 17:21:31 +0200100 if (dvi->ddc) {
101 if (drm_probe_ddc(dvi->ddc))
102 return connector_status_connected;
103 else
104 return connector_status_disconnected;
105 }
106
107 return connector_status_unknown;
108}
109
110static const struct drm_connector_funcs tfp410_con_funcs = {
Jyri Sarhadc55ac32016-10-31 17:21:31 +0200111 .detect = tfp410_connector_detect,
112 .fill_modes = drm_helper_probe_single_connector_modes,
113 .destroy = drm_connector_cleanup,
114 .reset = drm_atomic_helper_connector_reset,
115 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
116 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
117};
118
119static int tfp410_attach(struct drm_bridge *bridge)
120{
121 struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
122 int ret;
123
124 if (!bridge->encoder) {
125 dev_err(dvi->dev, "Missing encoder\n");
126 return -ENODEV;
127 }
128
Peter Ujfalusi3d31e212019-04-01 15:33:42 +0300129 if (dvi->hpd_irq >= 0)
Christopher Spinrathf56c9202017-03-06 22:40:43 +0100130 dvi->connector.polled = DRM_CONNECTOR_POLL_HPD;
Peter Ujfalusi3d31e212019-04-01 15:33:42 +0300131 else
132 dvi->connector.polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
Christopher Spinrathf56c9202017-03-06 22:40:43 +0100133
Jyri Sarhadc55ac32016-10-31 17:21:31 +0200134 drm_connector_helper_add(&dvi->connector,
135 &tfp410_con_helper_funcs);
136 ret = drm_connector_init(bridge->dev, &dvi->connector,
Laurent Pinchart60b903c32018-09-25 16:59:28 +0300137 &tfp410_con_funcs, dvi->connector_type);
Jyri Sarhadc55ac32016-10-31 17:21:31 +0200138 if (ret) {
139 dev_err(dvi->dev, "drm_connector_init() failed: %d\n", ret);
140 return ret;
141 }
142
Peter Ujfalusi0eb27662019-04-01 15:41:43 +0300143 drm_display_info_set_bus_formats(&dvi->connector.display_info,
144 &dvi->bus_format, 1);
145
Daniel Vettercde4c442018-07-09 10:40:07 +0200146 drm_connector_attach_encoder(&dvi->connector,
Jyri Sarhadc55ac32016-10-31 17:21:31 +0200147 bridge->encoder);
148
149 return 0;
150}
151
Laurent Pinchart38c02db2018-10-01 18:07:48 +0300152static void tfp410_enable(struct drm_bridge *bridge)
153{
154 struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
155
156 gpiod_set_value_cansleep(dvi->powerdown, 0);
157}
158
159static void tfp410_disable(struct drm_bridge *bridge)
160{
161 struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
162
163 gpiod_set_value_cansleep(dvi->powerdown, 1);
164}
165
Jyri Sarhadc55ac32016-10-31 17:21:31 +0200166static const struct drm_bridge_funcs tfp410_bridge_funcs = {
167 .attach = tfp410_attach,
Laurent Pinchart38c02db2018-10-01 18:07:48 +0300168 .enable = tfp410_enable,
169 .disable = tfp410_disable,
Jyri Sarhadc55ac32016-10-31 17:21:31 +0200170};
171
Christopher Spinrathf56c9202017-03-06 22:40:43 +0100172static void tfp410_hpd_work_func(struct work_struct *work)
173{
174 struct tfp410 *dvi;
175
176 dvi = container_of(work, struct tfp410, hpd_work.work);
177
178 if (dvi->bridge.dev)
179 drm_helper_hpd_irq_event(dvi->bridge.dev);
180}
181
182static irqreturn_t tfp410_hpd_irq_thread(int irq, void *arg)
183{
184 struct tfp410 *dvi = arg;
185
186 mod_delayed_work(system_wq, &dvi->hpd_work,
187 msecs_to_jiffies(HOTPLUG_DEBOUNCE_MS));
188
189 return IRQ_HANDLED;
190}
191
Laurent Pinchart897dae52018-09-27 11:29:48 +0300192static const struct drm_bridge_timings tfp410_default_timings = {
193 .input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
194 | DRM_BUS_FLAG_DE_HIGH,
195 .setup_time_ps = 1200,
196 .hold_time_ps = 1300,
197};
198
199static int tfp410_parse_timings(struct tfp410 *dvi, bool i2c)
200{
201 struct drm_bridge_timings *timings = &dvi->timings;
202 struct device_node *ep;
203 u32 pclk_sample = 0;
Peter Ujfalusi0eb27662019-04-01 15:41:43 +0300204 u32 bus_width = 24;
Laurent Pinchart897dae52018-09-27 11:29:48 +0300205 s32 deskew = 0;
206
207 /* Start with defaults. */
208 *timings = tfp410_default_timings;
209
210 if (i2c)
211 /*
212 * In I2C mode timings are configured through the I2C interface.
213 * As the driver doesn't support I2C configuration yet, we just
214 * go with the defaults (BSEL=1, DSEL=1, DKEN=0, EDGE=1).
215 */
216 return 0;
217
218 /*
219 * In non-I2C mode, timings are configured through the BSEL, DSEL, DKEN
220 * and EDGE pins. They are specified in DT through endpoint properties
221 * and vendor-specific properties.
222 */
223 ep = of_graph_get_endpoint_by_regs(dvi->dev->of_node, 0, 0);
224 if (!ep)
225 return -EINVAL;
226
227 /* Get the sampling edge from the endpoint. */
228 of_property_read_u32(ep, "pclk-sample", &pclk_sample);
Peter Ujfalusi0eb27662019-04-01 15:41:43 +0300229 of_property_read_u32(ep, "bus-width", &bus_width);
Laurent Pinchart897dae52018-09-27 11:29:48 +0300230 of_node_put(ep);
231
232 timings->input_bus_flags = DRM_BUS_FLAG_DE_HIGH;
233
234 switch (pclk_sample) {
235 case 0:
236 timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE
237 | DRM_BUS_FLAG_SYNC_SAMPLE_NEGEDGE;
238 break;
239 case 1:
240 timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
241 | DRM_BUS_FLAG_SYNC_SAMPLE_POSEDGE;
242 break;
243 default:
244 return -EINVAL;
245 }
246
Peter Ujfalusi0eb27662019-04-01 15:41:43 +0300247 switch (bus_width) {
248 case 12:
249 dvi->bus_format = MEDIA_BUS_FMT_RGB888_2X12_LE;
250 break;
251 case 24:
252 dvi->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
253 break;
254 default:
255 return -EINVAL;
256 }
257
Laurent Pinchart897dae52018-09-27 11:29:48 +0300258 /* Get the setup and hold time from vendor-specific properties. */
259 of_property_read_u32(dvi->dev->of_node, "ti,deskew", (u32 *)&deskew);
260 if (deskew < -4 || deskew > 3)
261 return -EINVAL;
262
263 timings->setup_time_ps = min(0, 1200 - 350 * deskew);
264 timings->hold_time_ps = min(0, 1300 + 350 * deskew);
265
266 return 0;
267}
268
Christopher Spinrathf56c9202017-03-06 22:40:43 +0100269static int tfp410_get_connector_properties(struct tfp410 *dvi)
Jyri Sarhadc55ac32016-10-31 17:21:31 +0200270{
Rob Herring86418f92017-03-22 08:26:06 -0500271 struct device_node *connector_node, *ddc_phandle;
Jyri Sarhadc55ac32016-10-31 17:21:31 +0200272 int ret = 0;
273
274 /* port@1 is the connector node */
Rob Herring86418f92017-03-22 08:26:06 -0500275 connector_node = of_graph_get_remote_node(dvi->dev->of_node, 1, -1);
Jyri Sarhadc55ac32016-10-31 17:21:31 +0200276 if (!connector_node)
Rob Herring86418f92017-03-22 08:26:06 -0500277 return -ENODEV;
Jyri Sarhadc55ac32016-10-31 17:21:31 +0200278
Laurent Pinchart60b903c32018-09-25 16:59:28 +0300279 if (of_device_is_compatible(connector_node, "hdmi-connector"))
280 dvi->connector_type = DRM_MODE_CONNECTOR_HDMIA;
281 else
282 dvi->connector_type = DRM_MODE_CONNECTOR_DVID;
283
Christopher Spinrathf56c9202017-03-06 22:40:43 +0100284 dvi->hpd = fwnode_get_named_gpiod(&connector_node->fwnode,
285 "hpd-gpios", 0, GPIOD_IN, "hpd");
286 if (IS_ERR(dvi->hpd)) {
287 ret = PTR_ERR(dvi->hpd);
288 dvi->hpd = NULL;
289 if (ret == -ENOENT)
290 ret = 0;
291 else
292 goto fail;
293 }
294
Jyri Sarhadc55ac32016-10-31 17:21:31 +0200295 ddc_phandle = of_parse_phandle(connector_node, "ddc-i2c-bus", 0);
296 if (!ddc_phandle)
297 goto fail;
298
299 dvi->ddc = of_get_i2c_adapter_by_node(ddc_phandle);
300 if (dvi->ddc)
301 dev_info(dvi->dev, "Connector's ddc i2c bus found\n");
302 else
303 ret = -EPROBE_DEFER;
304
Jyri Sarhadc55ac32016-10-31 17:21:31 +0200305 of_node_put(ddc_phandle);
Rob Herring86418f92017-03-22 08:26:06 -0500306
307fail:
308 of_node_put(connector_node);
Jyri Sarhadc55ac32016-10-31 17:21:31 +0200309 return ret;
310}
311
Laurent Pinchart897dae52018-09-27 11:29:48 +0300312static int tfp410_init(struct device *dev, bool i2c)
Jyri Sarhadc55ac32016-10-31 17:21:31 +0200313{
314 struct tfp410 *dvi;
315 int ret;
316
317 if (!dev->of_node) {
318 dev_err(dev, "device-tree data is missing\n");
319 return -ENXIO;
320 }
321
322 dvi = devm_kzalloc(dev, sizeof(*dvi), GFP_KERNEL);
323 if (!dvi)
324 return -ENOMEM;
325 dev_set_drvdata(dev, dvi);
326
327 dvi->bridge.funcs = &tfp410_bridge_funcs;
328 dvi->bridge.of_node = dev->of_node;
Laurent Pinchart897dae52018-09-27 11:29:48 +0300329 dvi->bridge.timings = &dvi->timings;
Jyri Sarhadc55ac32016-10-31 17:21:31 +0200330 dvi->dev = dev;
331
Laurent Pinchart897dae52018-09-27 11:29:48 +0300332 ret = tfp410_parse_timings(dvi, i2c);
333 if (ret)
334 goto fail;
335
Christopher Spinrathf56c9202017-03-06 22:40:43 +0100336 ret = tfp410_get_connector_properties(dvi);
Jyri Sarhadc55ac32016-10-31 17:21:31 +0200337 if (ret)
338 goto fail;
339
Laurent Pinchart38c02db2018-10-01 18:07:48 +0300340 dvi->powerdown = devm_gpiod_get_optional(dev, "powerdown",
341 GPIOD_OUT_HIGH);
342 if (IS_ERR(dvi->powerdown)) {
343 dev_err(dev, "failed to parse powerdown gpio\n");
344 return PTR_ERR(dvi->powerdown);
345 }
346
Peter Ujfalusi3d31e212019-04-01 15:33:42 +0300347 if (dvi->hpd)
348 dvi->hpd_irq = gpiod_to_irq(dvi->hpd);
349 else
350 dvi->hpd_irq = -ENXIO;
351
352 if (dvi->hpd_irq >= 0) {
Christopher Spinrathf56c9202017-03-06 22:40:43 +0100353 INIT_DELAYED_WORK(&dvi->hpd_work, tfp410_hpd_work_func);
354
Peter Ujfalusi3d31e212019-04-01 15:33:42 +0300355 ret = devm_request_threaded_irq(dev, dvi->hpd_irq,
Christopher Spinrathf56c9202017-03-06 22:40:43 +0100356 NULL, tfp410_hpd_irq_thread, IRQF_TRIGGER_RISING |
357 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
358 "hdmi-hpd", dvi);
359 if (ret) {
360 DRM_ERROR("failed to register hpd interrupt\n");
361 goto fail;
362 }
363 }
364
Inki Daef74c5272017-07-03 17:42:27 +0900365 drm_bridge_add(&dvi->bridge);
Jyri Sarhadc55ac32016-10-31 17:21:31 +0200366
367 return 0;
368fail:
369 i2c_put_adapter(dvi->ddc);
Christopher Spinrathf56c9202017-03-06 22:40:43 +0100370 if (dvi->hpd)
371 gpiod_put(dvi->hpd);
Jyri Sarhadc55ac32016-10-31 17:21:31 +0200372 return ret;
373}
374
375static int tfp410_fini(struct device *dev)
376{
377 struct tfp410 *dvi = dev_get_drvdata(dev);
378
Christopher Spinrathf56c9202017-03-06 22:40:43 +0100379 cancel_delayed_work_sync(&dvi->hpd_work);
380
Jyri Sarhadc55ac32016-10-31 17:21:31 +0200381 drm_bridge_remove(&dvi->bridge);
382
383 if (dvi->ddc)
384 i2c_put_adapter(dvi->ddc);
Christopher Spinrathf56c9202017-03-06 22:40:43 +0100385 if (dvi->hpd)
386 gpiod_put(dvi->hpd);
Jyri Sarhadc55ac32016-10-31 17:21:31 +0200387
388 return 0;
389}
390
391static int tfp410_probe(struct platform_device *pdev)
392{
Laurent Pinchart897dae52018-09-27 11:29:48 +0300393 return tfp410_init(&pdev->dev, false);
Jyri Sarhadc55ac32016-10-31 17:21:31 +0200394}
395
396static int tfp410_remove(struct platform_device *pdev)
397{
398 return tfp410_fini(&pdev->dev);
399}
400
401static const struct of_device_id tfp410_match[] = {
402 { .compatible = "ti,tfp410" },
403 {},
404};
405MODULE_DEVICE_TABLE(of, tfp410_match);
406
Wei Yongjun5b47d082017-02-09 15:25:49 +0000407static struct platform_driver tfp410_platform_driver = {
Jyri Sarhadc55ac32016-10-31 17:21:31 +0200408 .probe = tfp410_probe,
409 .remove = tfp410_remove,
410 .driver = {
411 .name = "tfp410-bridge",
412 .of_match_table = tfp410_match,
413 },
414};
415
416#if IS_ENABLED(CONFIG_I2C)
417/* There is currently no i2c functionality. */
418static int tfp410_i2c_probe(struct i2c_client *client,
419 const struct i2c_device_id *id)
420{
421 int reg;
422
423 if (!client->dev.of_node ||
424 of_property_read_u32(client->dev.of_node, "reg", &reg)) {
425 dev_err(&client->dev,
426 "Can't get i2c reg property from device-tree\n");
427 return -ENXIO;
428 }
429
Laurent Pinchart897dae52018-09-27 11:29:48 +0300430 return tfp410_init(&client->dev, true);
Jyri Sarhadc55ac32016-10-31 17:21:31 +0200431}
432
433static int tfp410_i2c_remove(struct i2c_client *client)
434{
435 return tfp410_fini(&client->dev);
436}
437
438static const struct i2c_device_id tfp410_i2c_ids[] = {
439 { "tfp410", 0 },
440 { }
441};
442MODULE_DEVICE_TABLE(i2c, tfp410_i2c_ids);
443
444static struct i2c_driver tfp410_i2c_driver = {
445 .driver = {
446 .name = "tfp410",
447 .of_match_table = of_match_ptr(tfp410_match),
448 },
449 .id_table = tfp410_i2c_ids,
450 .probe = tfp410_i2c_probe,
451 .remove = tfp410_i2c_remove,
452};
453#endif /* IS_ENABLED(CONFIG_I2C) */
454
455static struct {
456 uint i2c:1;
457 uint platform:1;
458} tfp410_registered_driver;
459
460static int __init tfp410_module_init(void)
461{
462 int ret;
463
464#if IS_ENABLED(CONFIG_I2C)
465 ret = i2c_add_driver(&tfp410_i2c_driver);
466 if (ret)
467 pr_err("%s: registering i2c driver failed: %d",
468 __func__, ret);
469 else
470 tfp410_registered_driver.i2c = 1;
471#endif
472
473 ret = platform_driver_register(&tfp410_platform_driver);
474 if (ret)
475 pr_err("%s: registering platform driver failed: %d",
476 __func__, ret);
477 else
478 tfp410_registered_driver.platform = 1;
479
480 if (tfp410_registered_driver.i2c ||
481 tfp410_registered_driver.platform)
482 return 0;
483
484 return ret;
485}
486module_init(tfp410_module_init);
487
488static void __exit tfp410_module_exit(void)
489{
490#if IS_ENABLED(CONFIG_I2C)
491 if (tfp410_registered_driver.i2c)
492 i2c_del_driver(&tfp410_i2c_driver);
493#endif
494 if (tfp410_registered_driver.platform)
495 platform_driver_unregister(&tfp410_platform_driver);
496}
497module_exit(tfp410_module_exit);
498
499MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
500MODULE_DESCRIPTION("TI TFP410 DVI bridge driver");
501MODULE_LICENSE("GPL");