xref: /openbmc/linux/drivers/gpu/drm/bridge/ti-tfp410.c (revision 51c7b447)
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_probe_helper.h>
24 
25 #define HOTPLUG_DEBOUNCE_MS		1100
26 
27 struct tfp410 {
28 	struct drm_bridge	bridge;
29 	struct drm_connector	connector;
30 	unsigned int		connector_type;
31 
32 	struct i2c_adapter	*ddc;
33 	struct gpio_desc	*hpd;
34 	int			hpd_irq;
35 	struct delayed_work	hpd_work;
36 	struct gpio_desc	*powerdown;
37 
38 	struct drm_bridge_timings timings;
39 
40 	struct device *dev;
41 };
42 
43 static inline struct tfp410 *
44 drm_bridge_to_tfp410(struct drm_bridge *bridge)
45 {
46 	return container_of(bridge, struct tfp410, bridge);
47 }
48 
49 static inline struct tfp410 *
50 drm_connector_to_tfp410(struct drm_connector *connector)
51 {
52 	return container_of(connector, struct tfp410, connector);
53 }
54 
55 static int tfp410_get_modes(struct drm_connector *connector)
56 {
57 	struct tfp410 *dvi = drm_connector_to_tfp410(connector);
58 	struct edid *edid;
59 	int ret;
60 
61 	if (!dvi->ddc)
62 		goto fallback;
63 
64 	edid = drm_get_edid(connector, dvi->ddc);
65 	if (!edid) {
66 		DRM_INFO("EDID read failed. Fallback to standard modes\n");
67 		goto fallback;
68 	}
69 
70 	drm_connector_update_edid_property(connector, edid);
71 
72 	return drm_add_edid_modes(connector, edid);
73 fallback:
74 	/* No EDID, fallback on the XGA standard modes */
75 	ret = drm_add_modes_noedid(connector, 1920, 1200);
76 
77 	/* And prefer a mode pretty much anything can handle */
78 	drm_set_preferred_mode(connector, 1024, 768);
79 
80 	return ret;
81 }
82 
83 static const struct drm_connector_helper_funcs tfp410_con_helper_funcs = {
84 	.get_modes	= tfp410_get_modes,
85 };
86 
87 static enum drm_connector_status
88 tfp410_connector_detect(struct drm_connector *connector, bool force)
89 {
90 	struct tfp410 *dvi = drm_connector_to_tfp410(connector);
91 
92 	if (dvi->hpd) {
93 		if (gpiod_get_value_cansleep(dvi->hpd))
94 			return connector_status_connected;
95 		else
96 			return connector_status_disconnected;
97 	}
98 
99 	if (dvi->ddc) {
100 		if (drm_probe_ddc(dvi->ddc))
101 			return connector_status_connected;
102 		else
103 			return connector_status_disconnected;
104 	}
105 
106 	return connector_status_unknown;
107 }
108 
109 static const struct drm_connector_funcs tfp410_con_funcs = {
110 	.detect			= tfp410_connector_detect,
111 	.fill_modes		= drm_helper_probe_single_connector_modes,
112 	.destroy		= drm_connector_cleanup,
113 	.reset			= drm_atomic_helper_connector_reset,
114 	.atomic_duplicate_state	= drm_atomic_helper_connector_duplicate_state,
115 	.atomic_destroy_state	= drm_atomic_helper_connector_destroy_state,
116 };
117 
118 static int tfp410_attach(struct drm_bridge *bridge)
119 {
120 	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
121 	int ret;
122 
123 	if (!bridge->encoder) {
124 		dev_err(dvi->dev, "Missing encoder\n");
125 		return -ENODEV;
126 	}
127 
128 	if (dvi->hpd_irq >= 0)
129 		dvi->connector.polled = DRM_CONNECTOR_POLL_HPD;
130 	else
131 		dvi->connector.polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
132 
133 	drm_connector_helper_add(&dvi->connector,
134 				 &tfp410_con_helper_funcs);
135 	ret = drm_connector_init(bridge->dev, &dvi->connector,
136 				 &tfp410_con_funcs, dvi->connector_type);
137 	if (ret) {
138 		dev_err(dvi->dev, "drm_connector_init() failed: %d\n", ret);
139 		return ret;
140 	}
141 
142 	drm_connector_attach_encoder(&dvi->connector,
143 					  bridge->encoder);
144 
145 	return 0;
146 }
147 
148 static void tfp410_enable(struct drm_bridge *bridge)
149 {
150 	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
151 
152 	gpiod_set_value_cansleep(dvi->powerdown, 0);
153 }
154 
155 static void tfp410_disable(struct drm_bridge *bridge)
156 {
157 	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
158 
159 	gpiod_set_value_cansleep(dvi->powerdown, 1);
160 }
161 
162 static const struct drm_bridge_funcs tfp410_bridge_funcs = {
163 	.attach		= tfp410_attach,
164 	.enable		= tfp410_enable,
165 	.disable	= tfp410_disable,
166 };
167 
168 static void tfp410_hpd_work_func(struct work_struct *work)
169 {
170 	struct tfp410 *dvi;
171 
172 	dvi = container_of(work, struct tfp410, hpd_work.work);
173 
174 	if (dvi->bridge.dev)
175 		drm_helper_hpd_irq_event(dvi->bridge.dev);
176 }
177 
178 static irqreturn_t tfp410_hpd_irq_thread(int irq, void *arg)
179 {
180 	struct tfp410 *dvi = arg;
181 
182 	mod_delayed_work(system_wq, &dvi->hpd_work,
183 			msecs_to_jiffies(HOTPLUG_DEBOUNCE_MS));
184 
185 	return IRQ_HANDLED;
186 }
187 
188 static const struct drm_bridge_timings tfp410_default_timings = {
189 	.input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
190 			 | DRM_BUS_FLAG_DE_HIGH,
191 	.setup_time_ps = 1200,
192 	.hold_time_ps = 1300,
193 };
194 
195 static int tfp410_parse_timings(struct tfp410 *dvi, bool i2c)
196 {
197 	struct drm_bridge_timings *timings = &dvi->timings;
198 	struct device_node *ep;
199 	u32 pclk_sample = 0;
200 	s32 deskew = 0;
201 
202 	/* Start with defaults. */
203 	*timings = tfp410_default_timings;
204 
205 	if (i2c)
206 		/*
207 		 * In I2C mode timings are configured through the I2C interface.
208 		 * As the driver doesn't support I2C configuration yet, we just
209 		 * go with the defaults (BSEL=1, DSEL=1, DKEN=0, EDGE=1).
210 		 */
211 		return 0;
212 
213 	/*
214 	 * In non-I2C mode, timings are configured through the BSEL, DSEL, DKEN
215 	 * and EDGE pins. They are specified in DT through endpoint properties
216 	 * and vendor-specific properties.
217 	 */
218 	ep = of_graph_get_endpoint_by_regs(dvi->dev->of_node, 0, 0);
219 	if (!ep)
220 		return -EINVAL;
221 
222 	/* Get the sampling edge from the endpoint. */
223 	of_property_read_u32(ep, "pclk-sample", &pclk_sample);
224 	of_node_put(ep);
225 
226 	timings->input_bus_flags = DRM_BUS_FLAG_DE_HIGH;
227 
228 	switch (pclk_sample) {
229 	case 0:
230 		timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE
231 					 |  DRM_BUS_FLAG_SYNC_SAMPLE_NEGEDGE;
232 		break;
233 	case 1:
234 		timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
235 					 |  DRM_BUS_FLAG_SYNC_SAMPLE_POSEDGE;
236 		break;
237 	default:
238 		return -EINVAL;
239 	}
240 
241 	/* Get the setup and hold time from vendor-specific properties. */
242 	of_property_read_u32(dvi->dev->of_node, "ti,deskew", (u32 *)&deskew);
243 	if (deskew < -4 || deskew > 3)
244 		return -EINVAL;
245 
246 	timings->setup_time_ps = min(0, 1200 - 350 * deskew);
247 	timings->hold_time_ps = min(0, 1300 + 350 * deskew);
248 
249 	return 0;
250 }
251 
252 static int tfp410_get_connector_properties(struct tfp410 *dvi)
253 {
254 	struct device_node *connector_node, *ddc_phandle;
255 	int ret = 0;
256 
257 	/* port@1 is the connector node */
258 	connector_node = of_graph_get_remote_node(dvi->dev->of_node, 1, -1);
259 	if (!connector_node)
260 		return -ENODEV;
261 
262 	if (of_device_is_compatible(connector_node, "hdmi-connector"))
263 		dvi->connector_type = DRM_MODE_CONNECTOR_HDMIA;
264 	else
265 		dvi->connector_type = DRM_MODE_CONNECTOR_DVID;
266 
267 	dvi->hpd = fwnode_get_named_gpiod(&connector_node->fwnode,
268 					"hpd-gpios", 0, GPIOD_IN, "hpd");
269 	if (IS_ERR(dvi->hpd)) {
270 		ret = PTR_ERR(dvi->hpd);
271 		dvi->hpd = NULL;
272 		if (ret == -ENOENT)
273 			ret = 0;
274 		else
275 			goto fail;
276 	}
277 
278 	ddc_phandle = of_parse_phandle(connector_node, "ddc-i2c-bus", 0);
279 	if (!ddc_phandle)
280 		goto fail;
281 
282 	dvi->ddc = of_get_i2c_adapter_by_node(ddc_phandle);
283 	if (dvi->ddc)
284 		dev_info(dvi->dev, "Connector's ddc i2c bus found\n");
285 	else
286 		ret = -EPROBE_DEFER;
287 
288 	of_node_put(ddc_phandle);
289 
290 fail:
291 	of_node_put(connector_node);
292 	return ret;
293 }
294 
295 static int tfp410_init(struct device *dev, bool i2c)
296 {
297 	struct tfp410 *dvi;
298 	int ret;
299 
300 	if (!dev->of_node) {
301 		dev_err(dev, "device-tree data is missing\n");
302 		return -ENXIO;
303 	}
304 
305 	dvi = devm_kzalloc(dev, sizeof(*dvi), GFP_KERNEL);
306 	if (!dvi)
307 		return -ENOMEM;
308 	dev_set_drvdata(dev, dvi);
309 
310 	dvi->bridge.funcs = &tfp410_bridge_funcs;
311 	dvi->bridge.of_node = dev->of_node;
312 	dvi->bridge.timings = &dvi->timings;
313 	dvi->dev = dev;
314 
315 	ret = tfp410_parse_timings(dvi, i2c);
316 	if (ret)
317 		goto fail;
318 
319 	ret = tfp410_get_connector_properties(dvi);
320 	if (ret)
321 		goto fail;
322 
323 	dvi->powerdown = devm_gpiod_get_optional(dev, "powerdown",
324 						 GPIOD_OUT_HIGH);
325 	if (IS_ERR(dvi->powerdown)) {
326 		dev_err(dev, "failed to parse powerdown gpio\n");
327 		return PTR_ERR(dvi->powerdown);
328 	}
329 
330 	if (dvi->hpd)
331 		dvi->hpd_irq = gpiod_to_irq(dvi->hpd);
332 	else
333 		dvi->hpd_irq = -ENXIO;
334 
335 	if (dvi->hpd_irq >= 0) {
336 		INIT_DELAYED_WORK(&dvi->hpd_work, tfp410_hpd_work_func);
337 
338 		ret = devm_request_threaded_irq(dev, dvi->hpd_irq,
339 			NULL, tfp410_hpd_irq_thread, IRQF_TRIGGER_RISING |
340 			IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
341 			"hdmi-hpd", dvi);
342 		if (ret) {
343 			DRM_ERROR("failed to register hpd interrupt\n");
344 			goto fail;
345 		}
346 	}
347 
348 	drm_bridge_add(&dvi->bridge);
349 
350 	return 0;
351 fail:
352 	i2c_put_adapter(dvi->ddc);
353 	if (dvi->hpd)
354 		gpiod_put(dvi->hpd);
355 	return ret;
356 }
357 
358 static int tfp410_fini(struct device *dev)
359 {
360 	struct tfp410 *dvi = dev_get_drvdata(dev);
361 
362 	cancel_delayed_work_sync(&dvi->hpd_work);
363 
364 	drm_bridge_remove(&dvi->bridge);
365 
366 	if (dvi->ddc)
367 		i2c_put_adapter(dvi->ddc);
368 	if (dvi->hpd)
369 		gpiod_put(dvi->hpd);
370 
371 	return 0;
372 }
373 
374 static int tfp410_probe(struct platform_device *pdev)
375 {
376 	return tfp410_init(&pdev->dev, false);
377 }
378 
379 static int tfp410_remove(struct platform_device *pdev)
380 {
381 	return tfp410_fini(&pdev->dev);
382 }
383 
384 static const struct of_device_id tfp410_match[] = {
385 	{ .compatible = "ti,tfp410" },
386 	{},
387 };
388 MODULE_DEVICE_TABLE(of, tfp410_match);
389 
390 static struct platform_driver tfp410_platform_driver = {
391 	.probe	= tfp410_probe,
392 	.remove	= tfp410_remove,
393 	.driver	= {
394 		.name		= "tfp410-bridge",
395 		.of_match_table	= tfp410_match,
396 	},
397 };
398 
399 #if IS_ENABLED(CONFIG_I2C)
400 /* There is currently no i2c functionality. */
401 static int tfp410_i2c_probe(struct i2c_client *client,
402 			    const struct i2c_device_id *id)
403 {
404 	int reg;
405 
406 	if (!client->dev.of_node ||
407 	    of_property_read_u32(client->dev.of_node, "reg", &reg)) {
408 		dev_err(&client->dev,
409 			"Can't get i2c reg property from device-tree\n");
410 		return -ENXIO;
411 	}
412 
413 	return tfp410_init(&client->dev, true);
414 }
415 
416 static int tfp410_i2c_remove(struct i2c_client *client)
417 {
418 	return tfp410_fini(&client->dev);
419 }
420 
421 static const struct i2c_device_id tfp410_i2c_ids[] = {
422 	{ "tfp410", 0 },
423 	{ }
424 };
425 MODULE_DEVICE_TABLE(i2c, tfp410_i2c_ids);
426 
427 static struct i2c_driver tfp410_i2c_driver = {
428 	.driver = {
429 		.name	= "tfp410",
430 		.of_match_table = of_match_ptr(tfp410_match),
431 	},
432 	.id_table	= tfp410_i2c_ids,
433 	.probe		= tfp410_i2c_probe,
434 	.remove		= tfp410_i2c_remove,
435 };
436 #endif /* IS_ENABLED(CONFIG_I2C) */
437 
438 static struct {
439 	uint i2c:1;
440 	uint platform:1;
441 }  tfp410_registered_driver;
442 
443 static int __init tfp410_module_init(void)
444 {
445 	int ret;
446 
447 #if IS_ENABLED(CONFIG_I2C)
448 	ret = i2c_add_driver(&tfp410_i2c_driver);
449 	if (ret)
450 		pr_err("%s: registering i2c driver failed: %d",
451 		       __func__, ret);
452 	else
453 		tfp410_registered_driver.i2c = 1;
454 #endif
455 
456 	ret = platform_driver_register(&tfp410_platform_driver);
457 	if (ret)
458 		pr_err("%s: registering platform driver failed: %d",
459 		       __func__, ret);
460 	else
461 		tfp410_registered_driver.platform = 1;
462 
463 	if (tfp410_registered_driver.i2c ||
464 	    tfp410_registered_driver.platform)
465 		return 0;
466 
467 	return ret;
468 }
469 module_init(tfp410_module_init);
470 
471 static void __exit tfp410_module_exit(void)
472 {
473 #if IS_ENABLED(CONFIG_I2C)
474 	if (tfp410_registered_driver.i2c)
475 		i2c_del_driver(&tfp410_i2c_driver);
476 #endif
477 	if (tfp410_registered_driver.platform)
478 		platform_driver_unregister(&tfp410_platform_driver);
479 }
480 module_exit(tfp410_module_exit);
481 
482 MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
483 MODULE_DESCRIPTION("TI TFP410 DVI bridge driver");
484 MODULE_LICENSE("GPL");
485