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