2 * Copyright (C) 2016 Texas Instruments
3 * Author: Jyri Sarha <jsarha@ti.com>
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.
11 #include <linux/delay.h>
12 #include <linux/fwnode.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/irq.h>
15 #include <linux/module.h>
16 #include <linux/of_graph.h>
17 #include <linux/platform_device.h>
18 #include <linux/i2c.h>
21 #include <drm/drm_atomic_helper.h>
22 #include <drm/drm_crtc.h>
23 #include <drm/drm_probe_helper.h>
25 #define HOTPLUG_DEBOUNCE_MS 1100
28 struct drm_bridge bridge;
29 struct drm_connector connector;
30 unsigned int connector_type;
33 struct i2c_adapter *ddc;
34 struct gpio_desc *hpd;
36 struct delayed_work hpd_work;
37 struct gpio_desc *powerdown;
39 struct drm_bridge_timings timings;
44 static inline struct tfp410 *
45 drm_bridge_to_tfp410(struct drm_bridge *bridge)
47 return container_of(bridge, struct tfp410, bridge);
50 static inline struct tfp410 *
51 drm_connector_to_tfp410(struct drm_connector *connector)
53 return container_of(connector, struct tfp410, connector);
56 static int tfp410_get_modes(struct drm_connector *connector)
58 struct tfp410 *dvi = drm_connector_to_tfp410(connector);
65 edid = drm_get_edid(connector, dvi->ddc);
67 DRM_INFO("EDID read failed. Fallback to standard modes\n");
71 drm_connector_update_edid_property(connector, edid);
73 return drm_add_edid_modes(connector, edid);
75 /* No EDID, fallback on the XGA standard modes */
76 ret = drm_add_modes_noedid(connector, 1920, 1200);
78 /* And prefer a mode pretty much anything can handle */
79 drm_set_preferred_mode(connector, 1024, 768);
84 static const struct drm_connector_helper_funcs tfp410_con_helper_funcs = {
85 .get_modes = tfp410_get_modes,
88 static enum drm_connector_status
89 tfp410_connector_detect(struct drm_connector *connector, bool force)
91 struct tfp410 *dvi = drm_connector_to_tfp410(connector);
94 if (gpiod_get_value_cansleep(dvi->hpd))
95 return connector_status_connected;
97 return connector_status_disconnected;
101 if (drm_probe_ddc(dvi->ddc))
102 return connector_status_connected;
104 return connector_status_disconnected;
107 return connector_status_unknown;
110 static const struct drm_connector_funcs tfp410_con_funcs = {
111 .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,
119 static int tfp410_attach(struct drm_bridge *bridge)
121 struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
124 if (!bridge->encoder) {
125 dev_err(dvi->dev, "Missing encoder\n");
129 if (dvi->hpd_irq >= 0)
130 dvi->connector.polled = DRM_CONNECTOR_POLL_HPD;
132 dvi->connector.polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
134 drm_connector_helper_add(&dvi->connector,
135 &tfp410_con_helper_funcs);
136 ret = drm_connector_init(bridge->dev, &dvi->connector,
137 &tfp410_con_funcs, dvi->connector_type);
139 dev_err(dvi->dev, "drm_connector_init() failed: %d\n", ret);
143 drm_display_info_set_bus_formats(&dvi->connector.display_info,
144 &dvi->bus_format, 1);
146 drm_connector_attach_encoder(&dvi->connector,
152 static void tfp410_enable(struct drm_bridge *bridge)
154 struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
156 gpiod_set_value_cansleep(dvi->powerdown, 0);
159 static void tfp410_disable(struct drm_bridge *bridge)
161 struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
163 gpiod_set_value_cansleep(dvi->powerdown, 1);
166 static const struct drm_bridge_funcs tfp410_bridge_funcs = {
167 .attach = tfp410_attach,
168 .enable = tfp410_enable,
169 .disable = tfp410_disable,
172 static void tfp410_hpd_work_func(struct work_struct *work)
176 dvi = container_of(work, struct tfp410, hpd_work.work);
179 drm_helper_hpd_irq_event(dvi->bridge.dev);
182 static irqreturn_t tfp410_hpd_irq_thread(int irq, void *arg)
184 struct tfp410 *dvi = arg;
186 mod_delayed_work(system_wq, &dvi->hpd_work,
187 msecs_to_jiffies(HOTPLUG_DEBOUNCE_MS));
192 static 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,
199 static int tfp410_parse_timings(struct tfp410 *dvi, bool i2c)
201 struct drm_bridge_timings *timings = &dvi->timings;
202 struct device_node *ep;
207 /* Start with defaults. */
208 *timings = tfp410_default_timings;
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).
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.
223 ep = of_graph_get_endpoint_by_regs(dvi->dev->of_node, 0, 0);
227 /* Get the sampling edge from the endpoint. */
228 of_property_read_u32(ep, "pclk-sample", &pclk_sample);
229 of_property_read_u32(ep, "bus-width", &bus_width);
232 timings->input_bus_flags = DRM_BUS_FLAG_DE_HIGH;
234 switch (pclk_sample) {
236 timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE
237 | DRM_BUS_FLAG_SYNC_SAMPLE_NEGEDGE;
240 timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
241 | DRM_BUS_FLAG_SYNC_SAMPLE_POSEDGE;
249 dvi->bus_format = MEDIA_BUS_FMT_RGB888_2X12_LE;
252 dvi->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
258 /* 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)
263 timings->setup_time_ps = min(0, 1200 - 350 * deskew);
264 timings->hold_time_ps = min(0, 1300 + 350 * deskew);
269 static int tfp410_get_connector_properties(struct tfp410 *dvi)
271 struct device_node *connector_node, *ddc_phandle;
274 /* port@1 is the connector node */
275 connector_node = of_graph_get_remote_node(dvi->dev->of_node, 1, -1);
279 if (of_device_is_compatible(connector_node, "hdmi-connector"))
280 dvi->connector_type = DRM_MODE_CONNECTOR_HDMIA;
282 dvi->connector_type = DRM_MODE_CONNECTOR_DVID;
284 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);
295 ddc_phandle = of_parse_phandle(connector_node, "ddc-i2c-bus", 0);
299 dvi->ddc = of_get_i2c_adapter_by_node(ddc_phandle);
301 dev_info(dvi->dev, "Connector's ddc i2c bus found\n");
305 of_node_put(ddc_phandle);
308 of_node_put(connector_node);
312 static int tfp410_init(struct device *dev, bool i2c)
318 dev_err(dev, "device-tree data is missing\n");
322 dvi = devm_kzalloc(dev, sizeof(*dvi), GFP_KERNEL);
325 dev_set_drvdata(dev, dvi);
327 dvi->bridge.funcs = &tfp410_bridge_funcs;
328 dvi->bridge.of_node = dev->of_node;
329 dvi->bridge.timings = &dvi->timings;
332 ret = tfp410_parse_timings(dvi, i2c);
336 ret = tfp410_get_connector_properties(dvi);
340 dvi->powerdown = devm_gpiod_get_optional(dev, "powerdown",
342 if (IS_ERR(dvi->powerdown)) {
343 dev_err(dev, "failed to parse powerdown gpio\n");
344 return PTR_ERR(dvi->powerdown);
348 dvi->hpd_irq = gpiod_to_irq(dvi->hpd);
350 dvi->hpd_irq = -ENXIO;
352 if (dvi->hpd_irq >= 0) {
353 INIT_DELAYED_WORK(&dvi->hpd_work, tfp410_hpd_work_func);
355 ret = devm_request_threaded_irq(dev, dvi->hpd_irq,
356 NULL, tfp410_hpd_irq_thread, IRQF_TRIGGER_RISING |
357 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
360 DRM_ERROR("failed to register hpd interrupt\n");
365 drm_bridge_add(&dvi->bridge);
369 i2c_put_adapter(dvi->ddc);
375 static int tfp410_fini(struct device *dev)
377 struct tfp410 *dvi = dev_get_drvdata(dev);
379 cancel_delayed_work_sync(&dvi->hpd_work);
381 drm_bridge_remove(&dvi->bridge);
384 i2c_put_adapter(dvi->ddc);
391 static int tfp410_probe(struct platform_device *pdev)
393 return tfp410_init(&pdev->dev, false);
396 static int tfp410_remove(struct platform_device *pdev)
398 return tfp410_fini(&pdev->dev);
401 static const struct of_device_id tfp410_match[] = {
402 { .compatible = "ti,tfp410" },
405 MODULE_DEVICE_TABLE(of, tfp410_match);
407 static struct platform_driver tfp410_platform_driver = {
408 .probe = tfp410_probe,
409 .remove = tfp410_remove,
411 .name = "tfp410-bridge",
412 .of_match_table = tfp410_match,
416 #if IS_ENABLED(CONFIG_I2C)
417 /* There is currently no i2c functionality. */
418 static int tfp410_i2c_probe(struct i2c_client *client,
419 const struct i2c_device_id *id)
423 if (!client->dev.of_node ||
424 of_property_read_u32(client->dev.of_node, "reg", ®)) {
425 dev_err(&client->dev,
426 "Can't get i2c reg property from device-tree\n");
430 return tfp410_init(&client->dev, true);
433 static int tfp410_i2c_remove(struct i2c_client *client)
435 return tfp410_fini(&client->dev);
438 static const struct i2c_device_id tfp410_i2c_ids[] = {
442 MODULE_DEVICE_TABLE(i2c, tfp410_i2c_ids);
444 static struct i2c_driver tfp410_i2c_driver = {
447 .of_match_table = of_match_ptr(tfp410_match),
449 .id_table = tfp410_i2c_ids,
450 .probe = tfp410_i2c_probe,
451 .remove = tfp410_i2c_remove,
453 #endif /* IS_ENABLED(CONFIG_I2C) */
458 } tfp410_registered_driver;
460 static int __init tfp410_module_init(void)
464 #if IS_ENABLED(CONFIG_I2C)
465 ret = i2c_add_driver(&tfp410_i2c_driver);
467 pr_err("%s: registering i2c driver failed: %d",
470 tfp410_registered_driver.i2c = 1;
473 ret = platform_driver_register(&tfp410_platform_driver);
475 pr_err("%s: registering platform driver failed: %d",
478 tfp410_registered_driver.platform = 1;
480 if (tfp410_registered_driver.i2c ||
481 tfp410_registered_driver.platform)
486 module_init(tfp410_module_init);
488 static void __exit tfp410_module_exit(void)
490 #if IS_ENABLED(CONFIG_I2C)
491 if (tfp410_registered_driver.i2c)
492 i2c_del_driver(&tfp410_i2c_driver);
494 if (tfp410_registered_driver.platform)
495 platform_driver_unregister(&tfp410_platform_driver);
497 module_exit(tfp410_module_exit);
499 MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
500 MODULE_DESCRIPTION("TI TFP410 DVI bridge driver");
501 MODULE_LICENSE("GPL");