xref: /linux/drivers/gpu/drm/bridge/ti-tfp410.c (revision 99a97a8ba9881fc47901ff36b057e5cd0bf06af0)
1 /*
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 
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>
19 
20 #include <drm/drmP.h>
21 #include <drm/drm_atomic_helper.h>
22 #include <drm/drm_crtc.h>
23 #include <drm/drm_crtc_helper.h>
24 
25 #define HOTPLUG_DEBOUNCE_MS		1100
26 
27 struct tfp410 {
28 	struct drm_bridge	bridge;
29 	struct drm_connector	connector;
30 
31 	struct i2c_adapter	*ddc;
32 	struct gpio_desc	*hpd;
33 	struct delayed_work	hpd_work;
34 
35 	struct device *dev;
36 };
37 
38 static inline struct tfp410 *
39 drm_bridge_to_tfp410(struct drm_bridge *bridge)
40 {
41 	return container_of(bridge, struct tfp410, bridge);
42 }
43 
44 static inline struct tfp410 *
45 drm_connector_to_tfp410(struct drm_connector *connector)
46 {
47 	return container_of(connector, struct tfp410, connector);
48 }
49 
50 static int tfp410_get_modes(struct drm_connector *connector)
51 {
52 	struct tfp410 *dvi = drm_connector_to_tfp410(connector);
53 	struct edid *edid;
54 	int ret;
55 
56 	if (!dvi->ddc)
57 		goto fallback;
58 
59 	edid = drm_get_edid(connector, dvi->ddc);
60 	if (!edid) {
61 		DRM_INFO("EDID read failed. Fallback to standard modes\n");
62 		goto fallback;
63 	}
64 
65 	drm_mode_connector_update_edid_property(connector, edid);
66 
67 	return drm_add_edid_modes(connector, edid);
68 fallback:
69 	/* No EDID, fallback on the XGA standard modes */
70 	ret = drm_add_modes_noedid(connector, 1920, 1200);
71 
72 	/* And prefer a mode pretty much anything can handle */
73 	drm_set_preferred_mode(connector, 1024, 768);
74 
75 	return ret;
76 }
77 
78 static const struct drm_connector_helper_funcs tfp410_con_helper_funcs = {
79 	.get_modes	= tfp410_get_modes,
80 };
81 
82 static enum drm_connector_status
83 tfp410_connector_detect(struct drm_connector *connector, bool force)
84 {
85 	struct tfp410 *dvi = drm_connector_to_tfp410(connector);
86 
87 	if (dvi->hpd) {
88 		if (gpiod_get_value_cansleep(dvi->hpd))
89 			return connector_status_connected;
90 		else
91 			return connector_status_disconnected;
92 	}
93 
94 	if (dvi->ddc) {
95 		if (drm_probe_ddc(dvi->ddc))
96 			return connector_status_connected;
97 		else
98 			return connector_status_disconnected;
99 	}
100 
101 	return connector_status_unknown;
102 }
103 
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,
112 };
113 
114 static int tfp410_attach(struct drm_bridge *bridge)
115 {
116 	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
117 	int ret;
118 
119 	if (!bridge->encoder) {
120 		dev_err(dvi->dev, "Missing encoder\n");
121 		return -ENODEV;
122 	}
123 
124 	if (dvi->hpd)
125 		dvi->connector.polled = DRM_CONNECTOR_POLL_HPD;
126 
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);
131 	if (ret) {
132 		dev_err(dvi->dev, "drm_connector_init() failed: %d\n", ret);
133 		return ret;
134 	}
135 
136 	drm_mode_connector_attach_encoder(&dvi->connector,
137 					  bridge->encoder);
138 
139 	return 0;
140 }
141 
142 static const struct drm_bridge_funcs tfp410_bridge_funcs = {
143 	.attach		= tfp410_attach,
144 };
145 
146 static void tfp410_hpd_work_func(struct work_struct *work)
147 {
148 	struct tfp410 *dvi;
149 
150 	dvi = container_of(work, struct tfp410, hpd_work.work);
151 
152 	if (dvi->bridge.dev)
153 		drm_helper_hpd_irq_event(dvi->bridge.dev);
154 }
155 
156 static irqreturn_t tfp410_hpd_irq_thread(int irq, void *arg)
157 {
158 	struct tfp410 *dvi = arg;
159 
160 	mod_delayed_work(system_wq, &dvi->hpd_work,
161 			msecs_to_jiffies(HOTPLUG_DEBOUNCE_MS));
162 
163 	return IRQ_HANDLED;
164 }
165 
166 static int tfp410_get_connector_properties(struct tfp410 *dvi)
167 {
168 	struct device_node *ep = NULL, *connector_node = NULL;
169 	struct device_node *ddc_phandle = NULL;
170 	int ret = 0;
171 
172 	/* port@1 is the connector node */
173 	ep = of_graph_get_endpoint_by_regs(dvi->dev->of_node, 1, -1);
174 	if (!ep)
175 		goto fail;
176 
177 	connector_node = of_graph_get_remote_port_parent(ep);
178 	if (!connector_node)
179 		goto fail;
180 
181 	dvi->hpd = fwnode_get_named_gpiod(&connector_node->fwnode,
182 					"hpd-gpios", 0, GPIOD_IN, "hpd");
183 	if (IS_ERR(dvi->hpd)) {
184 		ret = PTR_ERR(dvi->hpd);
185 		dvi->hpd = NULL;
186 		if (ret == -ENOENT)
187 			ret = 0;
188 		else
189 			goto fail;
190 	}
191 
192 	ddc_phandle = of_parse_phandle(connector_node, "ddc-i2c-bus", 0);
193 	if (!ddc_phandle)
194 		goto fail;
195 
196 	dvi->ddc = of_get_i2c_adapter_by_node(ddc_phandle);
197 	if (dvi->ddc)
198 		dev_info(dvi->dev, "Connector's ddc i2c bus found\n");
199 	else
200 		ret = -EPROBE_DEFER;
201 
202 fail:
203 	of_node_put(ep);
204 	of_node_put(connector_node);
205 	of_node_put(ddc_phandle);
206 	return ret;
207 }
208 
209 static int tfp410_init(struct device *dev)
210 {
211 	struct tfp410 *dvi;
212 	int ret;
213 
214 	if (!dev->of_node) {
215 		dev_err(dev, "device-tree data is missing\n");
216 		return -ENXIO;
217 	}
218 
219 	dvi = devm_kzalloc(dev, sizeof(*dvi), GFP_KERNEL);
220 	if (!dvi)
221 		return -ENOMEM;
222 	dev_set_drvdata(dev, dvi);
223 
224 	dvi->bridge.funcs = &tfp410_bridge_funcs;
225 	dvi->bridge.of_node = dev->of_node;
226 	dvi->dev = dev;
227 
228 	ret = tfp410_get_connector_properties(dvi);
229 	if (ret)
230 		goto fail;
231 
232 	if (dvi->hpd) {
233 		INIT_DELAYED_WORK(&dvi->hpd_work, tfp410_hpd_work_func);
234 
235 		ret = devm_request_threaded_irq(dev, gpiod_to_irq(dvi->hpd),
236 			NULL, tfp410_hpd_irq_thread, IRQF_TRIGGER_RISING |
237 			IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
238 			"hdmi-hpd", dvi);
239 		if (ret) {
240 			DRM_ERROR("failed to register hpd interrupt\n");
241 			goto fail;
242 		}
243 	}
244 
245 	ret = drm_bridge_add(&dvi->bridge);
246 	if (ret) {
247 		dev_err(dev, "drm_bridge_add() failed: %d\n", ret);
248 		goto fail;
249 	}
250 
251 	return 0;
252 fail:
253 	i2c_put_adapter(dvi->ddc);
254 	if (dvi->hpd)
255 		gpiod_put(dvi->hpd);
256 	return ret;
257 }
258 
259 static int tfp410_fini(struct device *dev)
260 {
261 	struct tfp410 *dvi = dev_get_drvdata(dev);
262 
263 	cancel_delayed_work_sync(&dvi->hpd_work);
264 
265 	drm_bridge_remove(&dvi->bridge);
266 
267 	if (dvi->ddc)
268 		i2c_put_adapter(dvi->ddc);
269 	if (dvi->hpd)
270 		gpiod_put(dvi->hpd);
271 
272 	return 0;
273 }
274 
275 static int tfp410_probe(struct platform_device *pdev)
276 {
277 	return tfp410_init(&pdev->dev);
278 }
279 
280 static int tfp410_remove(struct platform_device *pdev)
281 {
282 	return tfp410_fini(&pdev->dev);
283 }
284 
285 static const struct of_device_id tfp410_match[] = {
286 	{ .compatible = "ti,tfp410" },
287 	{},
288 };
289 MODULE_DEVICE_TABLE(of, tfp410_match);
290 
291 static struct platform_driver tfp410_platform_driver = {
292 	.probe	= tfp410_probe,
293 	.remove	= tfp410_remove,
294 	.driver	= {
295 		.name		= "tfp410-bridge",
296 		.of_match_table	= tfp410_match,
297 	},
298 };
299 
300 #if IS_ENABLED(CONFIG_I2C)
301 /* There is currently no i2c functionality. */
302 static int tfp410_i2c_probe(struct i2c_client *client,
303 			    const struct i2c_device_id *id)
304 {
305 	int reg;
306 
307 	if (!client->dev.of_node ||
308 	    of_property_read_u32(client->dev.of_node, "reg", &reg)) {
309 		dev_err(&client->dev,
310 			"Can't get i2c reg property from device-tree\n");
311 		return -ENXIO;
312 	}
313 
314 	return tfp410_init(&client->dev);
315 }
316 
317 static int tfp410_i2c_remove(struct i2c_client *client)
318 {
319 	return tfp410_fini(&client->dev);
320 }
321 
322 static const struct i2c_device_id tfp410_i2c_ids[] = {
323 	{ "tfp410", 0 },
324 	{ }
325 };
326 MODULE_DEVICE_TABLE(i2c, tfp410_i2c_ids);
327 
328 static struct i2c_driver tfp410_i2c_driver = {
329 	.driver = {
330 		.name	= "tfp410",
331 		.of_match_table = of_match_ptr(tfp410_match),
332 	},
333 	.id_table	= tfp410_i2c_ids,
334 	.probe		= tfp410_i2c_probe,
335 	.remove		= tfp410_i2c_remove,
336 };
337 #endif /* IS_ENABLED(CONFIG_I2C) */
338 
339 static struct {
340 	uint i2c:1;
341 	uint platform:1;
342 }  tfp410_registered_driver;
343 
344 static int __init tfp410_module_init(void)
345 {
346 	int ret;
347 
348 #if IS_ENABLED(CONFIG_I2C)
349 	ret = i2c_add_driver(&tfp410_i2c_driver);
350 	if (ret)
351 		pr_err("%s: registering i2c driver failed: %d",
352 		       __func__, ret);
353 	else
354 		tfp410_registered_driver.i2c = 1;
355 #endif
356 
357 	ret = platform_driver_register(&tfp410_platform_driver);
358 	if (ret)
359 		pr_err("%s: registering platform driver failed: %d",
360 		       __func__, ret);
361 	else
362 		tfp410_registered_driver.platform = 1;
363 
364 	if (tfp410_registered_driver.i2c ||
365 	    tfp410_registered_driver.platform)
366 		return 0;
367 
368 	return ret;
369 }
370 module_init(tfp410_module_init);
371 
372 static void __exit tfp410_module_exit(void)
373 {
374 #if IS_ENABLED(CONFIG_I2C)
375 	if (tfp410_registered_driver.i2c)
376 		i2c_del_driver(&tfp410_i2c_driver);
377 #endif
378 	if (tfp410_registered_driver.platform)
379 		platform_driver_unregister(&tfp410_platform_driver);
380 }
381 module_exit(tfp410_module_exit);
382 
383 MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
384 MODULE_DESCRIPTION("TI TFP410 DVI bridge driver");
385 MODULE_LICENSE("GPL");
386