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_crtc_helper.h>
25 #define HOTPLUG_DEBOUNCE_MS 1100
28 struct drm_bridge bridge;
29 struct drm_connector connector;
31 struct i2c_adapter *ddc;
32 struct gpio_desc *hpd;
33 struct delayed_work hpd_work;
38 static inline struct tfp410 *
39 drm_bridge_to_tfp410(struct drm_bridge *bridge)
41 return container_of(bridge, struct tfp410, bridge);
44 static inline struct tfp410 *
45 drm_connector_to_tfp410(struct drm_connector *connector)
47 return container_of(connector, struct tfp410, connector);
50 static int tfp410_get_modes(struct drm_connector *connector)
52 struct tfp410 *dvi = drm_connector_to_tfp410(connector);
59 edid = drm_get_edid(connector, dvi->ddc);
61 DRM_INFO("EDID read failed. Fallback to standard modes\n");
65 drm_mode_connector_update_edid_property(connector, edid);
67 return drm_add_edid_modes(connector, edid);
69 /* No EDID, fallback on the XGA standard modes */
70 ret = drm_add_modes_noedid(connector, 1920, 1200);
72 /* And prefer a mode pretty much anything can handle */
73 drm_set_preferred_mode(connector, 1024, 768);
78 static const struct drm_connector_helper_funcs tfp410_con_helper_funcs = {
79 .get_modes = tfp410_get_modes,
82 static enum drm_connector_status
83 tfp410_connector_detect(struct drm_connector *connector, bool force)
85 struct tfp410 *dvi = drm_connector_to_tfp410(connector);
88 if (gpiod_get_value_cansleep(dvi->hpd))
89 return connector_status_connected;
91 return connector_status_disconnected;
95 if (drm_probe_ddc(dvi->ddc))
96 return connector_status_connected;
98 return connector_status_disconnected;
101 return connector_status_unknown;
104 static const struct drm_connector_funcs tfp410_con_funcs = {
105 .dpms = drm_atomic_helper_connector_dpms,
106 .detect = tfp410_connector_detect,
107 .fill_modes = drm_helper_probe_single_connector_modes,
108 .destroy = drm_connector_cleanup,
109 .reset = drm_atomic_helper_connector_reset,
110 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
111 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
114 static int tfp410_attach(struct drm_bridge *bridge)
116 struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
119 if (!bridge->encoder) {
120 dev_err(dvi->dev, "Missing encoder\n");
125 dvi->connector.polled = DRM_CONNECTOR_POLL_HPD;
127 drm_connector_helper_add(&dvi->connector,
128 &tfp410_con_helper_funcs);
129 ret = drm_connector_init(bridge->dev, &dvi->connector,
130 &tfp410_con_funcs, DRM_MODE_CONNECTOR_HDMIA);
132 dev_err(dvi->dev, "drm_connector_init() failed: %d\n", ret);
136 drm_mode_connector_attach_encoder(&dvi->connector,
142 static const struct drm_bridge_funcs tfp410_bridge_funcs = {
143 .attach = tfp410_attach,
146 static void tfp410_hpd_work_func(struct work_struct *work)
150 dvi = container_of(work, struct tfp410, hpd_work.work);
153 drm_helper_hpd_irq_event(dvi->bridge.dev);
156 static irqreturn_t tfp410_hpd_irq_thread(int irq, void *arg)
158 struct tfp410 *dvi = arg;
160 mod_delayed_work(system_wq, &dvi->hpd_work,
161 msecs_to_jiffies(HOTPLUG_DEBOUNCE_MS));
166 static int tfp410_get_connector_properties(struct tfp410 *dvi)
168 struct device_node *connector_node, *ddc_phandle;
171 /* port@1 is the connector node */
172 connector_node = of_graph_get_remote_node(dvi->dev->of_node, 1, -1);
176 dvi->hpd = fwnode_get_named_gpiod(&connector_node->fwnode,
177 "hpd-gpios", 0, GPIOD_IN, "hpd");
178 if (IS_ERR(dvi->hpd)) {
179 ret = PTR_ERR(dvi->hpd);
187 ddc_phandle = of_parse_phandle(connector_node, "ddc-i2c-bus", 0);
191 dvi->ddc = of_get_i2c_adapter_by_node(ddc_phandle);
193 dev_info(dvi->dev, "Connector's ddc i2c bus found\n");
197 of_node_put(ddc_phandle);
200 of_node_put(connector_node);
204 static int tfp410_init(struct device *dev)
210 dev_err(dev, "device-tree data is missing\n");
214 dvi = devm_kzalloc(dev, sizeof(*dvi), GFP_KERNEL);
217 dev_set_drvdata(dev, dvi);
219 dvi->bridge.funcs = &tfp410_bridge_funcs;
220 dvi->bridge.of_node = dev->of_node;
223 ret = tfp410_get_connector_properties(dvi);
228 INIT_DELAYED_WORK(&dvi->hpd_work, tfp410_hpd_work_func);
230 ret = devm_request_threaded_irq(dev, gpiod_to_irq(dvi->hpd),
231 NULL, tfp410_hpd_irq_thread, IRQF_TRIGGER_RISING |
232 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
235 DRM_ERROR("failed to register hpd interrupt\n");
240 ret = drm_bridge_add(&dvi->bridge);
242 dev_err(dev, "drm_bridge_add() failed: %d\n", ret);
248 i2c_put_adapter(dvi->ddc);
254 static int tfp410_fini(struct device *dev)
256 struct tfp410 *dvi = dev_get_drvdata(dev);
258 cancel_delayed_work_sync(&dvi->hpd_work);
260 drm_bridge_remove(&dvi->bridge);
263 i2c_put_adapter(dvi->ddc);
270 static int tfp410_probe(struct platform_device *pdev)
272 return tfp410_init(&pdev->dev);
275 static int tfp410_remove(struct platform_device *pdev)
277 return tfp410_fini(&pdev->dev);
280 static const struct of_device_id tfp410_match[] = {
281 { .compatible = "ti,tfp410" },
284 MODULE_DEVICE_TABLE(of, tfp410_match);
286 static struct platform_driver tfp410_platform_driver = {
287 .probe = tfp410_probe,
288 .remove = tfp410_remove,
290 .name = "tfp410-bridge",
291 .of_match_table = tfp410_match,
295 #if IS_ENABLED(CONFIG_I2C)
296 /* There is currently no i2c functionality. */
297 static int tfp410_i2c_probe(struct i2c_client *client,
298 const struct i2c_device_id *id)
302 if (!client->dev.of_node ||
303 of_property_read_u32(client->dev.of_node, "reg", ®)) {
304 dev_err(&client->dev,
305 "Can't get i2c reg property from device-tree\n");
309 return tfp410_init(&client->dev);
312 static int tfp410_i2c_remove(struct i2c_client *client)
314 return tfp410_fini(&client->dev);
317 static const struct i2c_device_id tfp410_i2c_ids[] = {
321 MODULE_DEVICE_TABLE(i2c, tfp410_i2c_ids);
323 static struct i2c_driver tfp410_i2c_driver = {
326 .of_match_table = of_match_ptr(tfp410_match),
328 .id_table = tfp410_i2c_ids,
329 .probe = tfp410_i2c_probe,
330 .remove = tfp410_i2c_remove,
332 #endif /* IS_ENABLED(CONFIG_I2C) */
337 } tfp410_registered_driver;
339 static int __init tfp410_module_init(void)
343 #if IS_ENABLED(CONFIG_I2C)
344 ret = i2c_add_driver(&tfp410_i2c_driver);
346 pr_err("%s: registering i2c driver failed: %d",
349 tfp410_registered_driver.i2c = 1;
352 ret = platform_driver_register(&tfp410_platform_driver);
354 pr_err("%s: registering platform driver failed: %d",
357 tfp410_registered_driver.platform = 1;
359 if (tfp410_registered_driver.i2c ||
360 tfp410_registered_driver.platform)
365 module_init(tfp410_module_init);
367 static void __exit tfp410_module_exit(void)
369 #if IS_ENABLED(CONFIG_I2C)
370 if (tfp410_registered_driver.i2c)
371 i2c_del_driver(&tfp410_i2c_driver);
373 if (tfp410_registered_driver.platform)
374 platform_driver_unregister(&tfp410_platform_driver);
376 module_exit(tfp410_module_exit);
378 MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
379 MODULE_DESCRIPTION("TI TFP410 DVI bridge driver");
380 MODULE_LICENSE("GPL");