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