xref: /linux/drivers/gpu/drm/bridge/ti-tfp410.c (revision 9c399719cfb98fd92c7b76dcd57098e5e3ca5cda)
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/gpio/consumer.h>
8 #include <linux/i2c.h>
9 #include <linux/media-bus-format.h>
10 #include <linux/module.h>
11 #include <linux/of_graph.h>
12 #include <linux/platform_device.h>
13 #include <linux/workqueue.h>
14 
15 #include <drm/drm_atomic_helper.h>
16 #include <drm/drm_bridge.h>
17 #include <drm/drm_crtc.h>
18 #include <drm/drm_edid.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 
28 	u32			bus_format;
29 	struct delayed_work	hpd_work;
30 	struct gpio_desc	*powerdown;
31 
32 	struct drm_bridge_timings timings;
33 	struct drm_bridge	*next_bridge;
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 	const struct drm_edid *drm_edid;
54 	int ret;
55 
56 	if (dvi->next_bridge->ops & DRM_BRIDGE_OP_EDID) {
57 		drm_edid = drm_bridge_edid_read(dvi->next_bridge, connector);
58 		if (!drm_edid)
59 			DRM_INFO("EDID read failed. Fallback to standard modes\n");
60 	} else {
61 		drm_edid = NULL;
62 	}
63 
64 	drm_edid_connector_update(connector, drm_edid);
65 
66 	if (!drm_edid) {
67 		/*
68 		 * No EDID, fallback on the XGA standard modes and prefer a mode
69 		 * pretty much anything can handle.
70 		 */
71 		ret = drm_add_modes_noedid(connector, 1920, 1200);
72 		drm_set_preferred_mode(connector, 1024, 768);
73 		return ret;
74 	}
75 
76 	ret = drm_edid_connector_add_modes(connector);
77 
78 	drm_edid_free(drm_edid);
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 	return drm_bridge_detect(dvi->next_bridge);
93 }
94 
95 static const struct drm_connector_funcs tfp410_con_funcs = {
96 	.detect			= tfp410_connector_detect,
97 	.fill_modes		= drm_helper_probe_single_connector_modes,
98 	.destroy		= drm_connector_cleanup,
99 	.reset			= drm_atomic_helper_connector_reset,
100 	.atomic_duplicate_state	= drm_atomic_helper_connector_duplicate_state,
101 	.atomic_destroy_state	= drm_atomic_helper_connector_destroy_state,
102 };
103 
104 static void tfp410_hpd_work_func(struct work_struct *work)
105 {
106 	struct tfp410 *dvi;
107 
108 	dvi = container_of(work, struct tfp410, hpd_work.work);
109 
110 	if (dvi->bridge.dev)
111 		drm_helper_hpd_irq_event(dvi->bridge.dev);
112 }
113 
114 static void tfp410_hpd_callback(void *arg, enum drm_connector_status status)
115 {
116 	struct tfp410 *dvi = arg;
117 
118 	mod_delayed_work(system_wq, &dvi->hpd_work,
119 			 msecs_to_jiffies(HOTPLUG_DEBOUNCE_MS));
120 }
121 
122 static int tfp410_attach(struct drm_bridge *bridge,
123 			 struct drm_encoder *encoder,
124 			 enum drm_bridge_attach_flags flags)
125 {
126 	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
127 	int ret;
128 
129 	ret = drm_bridge_attach(encoder, dvi->next_bridge, bridge,
130 				DRM_BRIDGE_ATTACH_NO_CONNECTOR);
131 	if (ret < 0)
132 		return ret;
133 
134 	if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
135 		return 0;
136 
137 	if (dvi->next_bridge->ops & DRM_BRIDGE_OP_DETECT)
138 		dvi->connector.polled = DRM_CONNECTOR_POLL_HPD;
139 	else
140 		dvi->connector.polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
141 
142 	if (dvi->next_bridge->ops & DRM_BRIDGE_OP_HPD) {
143 		INIT_DELAYED_WORK(&dvi->hpd_work, tfp410_hpd_work_func);
144 		drm_bridge_hpd_enable(dvi->next_bridge, tfp410_hpd_callback,
145 				      dvi);
146 	}
147 
148 	drm_connector_helper_add(&dvi->connector,
149 				 &tfp410_con_helper_funcs);
150 	ret = drm_connector_init_with_ddc(bridge->dev, &dvi->connector,
151 					  &tfp410_con_funcs,
152 					  dvi->next_bridge->type,
153 					  dvi->next_bridge->ddc);
154 	if (ret) {
155 		dev_err(dvi->dev, "drm_connector_init_with_ddc() failed: %d\n",
156 			ret);
157 		return ret;
158 	}
159 
160 	drm_display_info_set_bus_formats(&dvi->connector.display_info,
161 					 &dvi->bus_format, 1);
162 
163 	drm_connector_attach_encoder(&dvi->connector, encoder);
164 
165 	return 0;
166 }
167 
168 static void tfp410_detach(struct drm_bridge *bridge)
169 {
170 	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
171 
172 	if (dvi->connector.dev && dvi->next_bridge->ops & DRM_BRIDGE_OP_HPD) {
173 		drm_bridge_hpd_disable(dvi->next_bridge);
174 		cancel_delayed_work_sync(&dvi->hpd_work);
175 	}
176 }
177 
178 static void tfp410_enable(struct drm_bridge *bridge)
179 {
180 	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
181 
182 	gpiod_set_value_cansleep(dvi->powerdown, 0);
183 }
184 
185 static void tfp410_disable(struct drm_bridge *bridge)
186 {
187 	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
188 
189 	gpiod_set_value_cansleep(dvi->powerdown, 1);
190 }
191 
192 static enum drm_mode_status tfp410_mode_valid(struct drm_bridge *bridge,
193 					      const struct drm_display_info *info,
194 					      const struct drm_display_mode *mode)
195 {
196 	if (mode->clock < 25000)
197 		return MODE_CLOCK_LOW;
198 
199 	if (mode->clock > 165000)
200 		return MODE_CLOCK_HIGH;
201 
202 	return MODE_OK;
203 }
204 
205 static u32 *tfp410_get_input_bus_fmts(struct drm_bridge *bridge,
206 				      struct drm_bridge_state *bridge_state,
207 				      struct drm_crtc_state *crtc_state,
208 				      struct drm_connector_state *conn_state,
209 				      u32 output_fmt,
210 				      unsigned int *num_input_fmts)
211 {
212 	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
213 	u32 *input_fmts;
214 
215 	*num_input_fmts = 0;
216 
217 	input_fmts = kzalloc(sizeof(*input_fmts), GFP_KERNEL);
218 	if (!input_fmts)
219 		return NULL;
220 
221 	*num_input_fmts = 1;
222 	input_fmts[0] = dvi->bus_format;
223 
224 	return input_fmts;
225 }
226 
227 static int tfp410_atomic_check(struct drm_bridge *bridge,
228 			       struct drm_bridge_state *bridge_state,
229 			       struct drm_crtc_state *crtc_state,
230 			       struct drm_connector_state *conn_state)
231 {
232 	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
233 
234 	/*
235 	 * There might be flags negotiation supported in future.
236 	 * Set the bus flags in atomic_check statically for now.
237 	 */
238 	bridge_state->input_bus_cfg.flags = dvi->timings.input_bus_flags;
239 
240 	return 0;
241 }
242 
243 static const struct drm_bridge_funcs tfp410_bridge_funcs = {
244 	.attach		= tfp410_attach,
245 	.detach		= tfp410_detach,
246 	.enable		= tfp410_enable,
247 	.disable	= tfp410_disable,
248 	.mode_valid	= tfp410_mode_valid,
249 	.atomic_reset = drm_atomic_helper_bridge_reset,
250 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
251 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
252 	.atomic_get_input_bus_fmts = tfp410_get_input_bus_fmts,
253 	.atomic_check = tfp410_atomic_check,
254 };
255 
256 static const struct drm_bridge_timings tfp410_default_timings = {
257 	.input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
258 			 | DRM_BUS_FLAG_DE_HIGH,
259 	.setup_time_ps = 1200,
260 	.hold_time_ps = 1300,
261 };
262 
263 static int tfp410_parse_timings(struct tfp410 *dvi, bool i2c)
264 {
265 	struct drm_bridge_timings *timings = &dvi->timings;
266 	struct device_node *ep;
267 	u32 pclk_sample = 0;
268 	u32 bus_width = 24;
269 	u32 deskew = 0;
270 
271 	/* Start with defaults. */
272 	*timings = tfp410_default_timings;
273 
274 	if (i2c)
275 		/*
276 		 * In I2C mode timings are configured through the I2C interface.
277 		 * As the driver doesn't support I2C configuration yet, we just
278 		 * go with the defaults (BSEL=1, DSEL=1, DKEN=0, EDGE=1).
279 		 */
280 		return 0;
281 
282 	/*
283 	 * In non-I2C mode, timings are configured through the BSEL, DSEL, DKEN
284 	 * and EDGE pins. They are specified in DT through endpoint properties
285 	 * and vendor-specific properties.
286 	 */
287 	ep = of_graph_get_endpoint_by_regs(dvi->dev->of_node, 0, 0);
288 	if (!ep)
289 		return -EINVAL;
290 
291 	/* Get the sampling edge from the endpoint. */
292 	of_property_read_u32(ep, "pclk-sample", &pclk_sample);
293 	of_property_read_u32(ep, "bus-width", &bus_width);
294 	of_node_put(ep);
295 
296 	timings->input_bus_flags = DRM_BUS_FLAG_DE_HIGH;
297 
298 	switch (pclk_sample) {
299 	case 0:
300 		timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE
301 					 |  DRM_BUS_FLAG_SYNC_SAMPLE_NEGEDGE;
302 		break;
303 	case 1:
304 		timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
305 					 |  DRM_BUS_FLAG_SYNC_SAMPLE_POSEDGE;
306 		break;
307 	default:
308 		return -EINVAL;
309 	}
310 
311 	switch (bus_width) {
312 	case 12:
313 		dvi->bus_format = MEDIA_BUS_FMT_RGB888_2X12_LE;
314 		break;
315 	case 24:
316 		dvi->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
317 		break;
318 	default:
319 		return -EINVAL;
320 	}
321 
322 	/* Get the setup and hold time from vendor-specific properties. */
323 	of_property_read_u32(dvi->dev->of_node, "ti,deskew", &deskew);
324 	if (deskew > 7)
325 		return -EINVAL;
326 
327 	timings->setup_time_ps = 1200 - 350 * ((s32)deskew - 4);
328 	timings->hold_time_ps = max(0, 1300 + 350 * ((s32)deskew - 4));
329 
330 	return 0;
331 }
332 
333 static int tfp410_init(struct device *dev, bool i2c)
334 {
335 	struct device_node *node;
336 	struct tfp410 *dvi;
337 	int ret;
338 
339 	if (!dev->of_node) {
340 		dev_err(dev, "device-tree data is missing\n");
341 		return -ENXIO;
342 	}
343 
344 	dvi = devm_drm_bridge_alloc(dev, struct tfp410, bridge,
345 				    &tfp410_bridge_funcs);
346 	if (IS_ERR(dvi))
347 		return PTR_ERR(dvi);
348 
349 	dvi->dev = dev;
350 	dev_set_drvdata(dev, dvi);
351 
352 	dvi->bridge.of_node = dev->of_node;
353 	dvi->bridge.timings = &dvi->timings;
354 	dvi->bridge.type = DRM_MODE_CONNECTOR_DVID;
355 
356 	ret = tfp410_parse_timings(dvi, i2c);
357 	if (ret)
358 		return ret;
359 
360 	/* Get the next bridge, connected to port@1. */
361 	node = of_graph_get_remote_node(dev->of_node, 1, -1);
362 	if (!node)
363 		return -ENODEV;
364 
365 	dvi->next_bridge = of_drm_find_bridge(node);
366 	of_node_put(node);
367 
368 	if (!dvi->next_bridge)
369 		return -EPROBE_DEFER;
370 
371 	/* Get the powerdown GPIO. */
372 	dvi->powerdown = devm_gpiod_get_optional(dev, "powerdown",
373 						 GPIOD_OUT_HIGH);
374 	if (IS_ERR(dvi->powerdown)) {
375 		dev_err(dev, "failed to parse powerdown gpio\n");
376 		return PTR_ERR(dvi->powerdown);
377 	}
378 
379 	/*  Register the DRM bridge. */
380 	drm_bridge_add(&dvi->bridge);
381 
382 	return 0;
383 }
384 
385 static void tfp410_fini(struct device *dev)
386 {
387 	struct tfp410 *dvi = dev_get_drvdata(dev);
388 
389 	drm_bridge_remove(&dvi->bridge);
390 }
391 
392 static int tfp410_probe(struct platform_device *pdev)
393 {
394 	return tfp410_init(&pdev->dev, false);
395 }
396 
397 static void tfp410_remove(struct platform_device *pdev)
398 {
399 	tfp410_fini(&pdev->dev);
400 }
401 
402 static const struct of_device_id tfp410_match[] = {
403 	{ .compatible = "ti,tfp410" },
404 	{},
405 };
406 MODULE_DEVICE_TABLE(of, tfp410_match);
407 
408 static struct platform_driver tfp410_platform_driver = {
409 	.probe	= tfp410_probe,
410 	.remove = tfp410_remove,
411 	.driver	= {
412 		.name		= "tfp410-bridge",
413 		.of_match_table	= tfp410_match,
414 	},
415 };
416 
417 #if IS_ENABLED(CONFIG_I2C)
418 /* There is currently no i2c functionality. */
419 static int tfp410_i2c_probe(struct i2c_client *client)
420 {
421 	int reg;
422 
423 	if (!client->dev.of_node ||
424 	    of_property_read_u32(client->dev.of_node, "reg", &reg)) {
425 		dev_err(&client->dev,
426 			"Can't get i2c reg property from device-tree\n");
427 		return -ENXIO;
428 	}
429 
430 	return tfp410_init(&client->dev, true);
431 }
432 
433 static void tfp410_i2c_remove(struct i2c_client *client)
434 {
435 	tfp410_fini(&client->dev);
436 }
437 
438 static const struct i2c_device_id tfp410_i2c_ids[] = {
439 	{ "tfp410" },
440 	{ }
441 };
442 MODULE_DEVICE_TABLE(i2c, tfp410_i2c_ids);
443 
444 static struct i2c_driver tfp410_i2c_driver = {
445 	.driver = {
446 		.name	= "tfp410",
447 		.of_match_table = tfp410_match,
448 	},
449 	.id_table	= tfp410_i2c_ids,
450 	.probe		= tfp410_i2c_probe,
451 	.remove		= tfp410_i2c_remove,
452 };
453 #endif /* IS_ENABLED(CONFIG_I2C) */
454 
455 static struct {
456 	uint i2c:1;
457 	uint platform:1;
458 }  tfp410_registered_driver;
459 
460 static int __init tfp410_module_init(void)
461 {
462 	int ret;
463 
464 #if IS_ENABLED(CONFIG_I2C)
465 	ret = i2c_add_driver(&tfp410_i2c_driver);
466 	if (ret)
467 		pr_err("%s: registering i2c driver failed: %d",
468 		       __func__, ret);
469 	else
470 		tfp410_registered_driver.i2c = 1;
471 #endif
472 
473 	ret = platform_driver_register(&tfp410_platform_driver);
474 	if (ret)
475 		pr_err("%s: registering platform driver failed: %d",
476 		       __func__, ret);
477 	else
478 		tfp410_registered_driver.platform = 1;
479 
480 	if (tfp410_registered_driver.i2c ||
481 	    tfp410_registered_driver.platform)
482 		return 0;
483 
484 	return ret;
485 }
486 module_init(tfp410_module_init);
487 
488 static void __exit tfp410_module_exit(void)
489 {
490 #if IS_ENABLED(CONFIG_I2C)
491 	if (tfp410_registered_driver.i2c)
492 		i2c_del_driver(&tfp410_i2c_driver);
493 #endif
494 	if (tfp410_registered_driver.platform)
495 		platform_driver_unregister(&tfp410_platform_driver);
496 }
497 module_exit(tfp410_module_exit);
498 
499 MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
500 MODULE_DESCRIPTION("TI TFP410 DVI bridge driver");
501 MODULE_LICENSE("GPL");
502