xref: /openbmc/linux/drivers/gpu/drm/bridge/ti-tfp410.c (revision 15e3ae36)
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_with_ddc() failed: %d\n",
154 			ret);
155 		return ret;
156 	}
157 
158 	drm_display_info_set_bus_formats(&dvi->connector.display_info,
159 					 &dvi->bus_format, 1);
160 
161 	drm_connector_attach_encoder(&dvi->connector, bridge->encoder);
162 
163 	return 0;
164 }
165 
166 static void tfp410_detach(struct drm_bridge *bridge)
167 {
168 	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
169 
170 	if (dvi->connector.dev && dvi->next_bridge->ops & DRM_BRIDGE_OP_HPD) {
171 		drm_bridge_hpd_disable(dvi->next_bridge);
172 		cancel_delayed_work_sync(&dvi->hpd_work);
173 	}
174 }
175 
176 static void tfp410_enable(struct drm_bridge *bridge)
177 {
178 	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
179 
180 	gpiod_set_value_cansleep(dvi->powerdown, 0);
181 }
182 
183 static void tfp410_disable(struct drm_bridge *bridge)
184 {
185 	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
186 
187 	gpiod_set_value_cansleep(dvi->powerdown, 1);
188 }
189 
190 static enum drm_mode_status tfp410_mode_valid(struct drm_bridge *bridge,
191 					      const struct drm_display_mode *mode)
192 {
193 	if (mode->clock < 25000)
194 		return MODE_CLOCK_LOW;
195 
196 	if (mode->clock > 165000)
197 		return MODE_CLOCK_HIGH;
198 
199 	return MODE_OK;
200 }
201 
202 static const struct drm_bridge_funcs tfp410_bridge_funcs = {
203 	.attach		= tfp410_attach,
204 	.detach		= tfp410_detach,
205 	.enable		= tfp410_enable,
206 	.disable	= tfp410_disable,
207 	.mode_valid	= tfp410_mode_valid,
208 };
209 
210 static const struct drm_bridge_timings tfp410_default_timings = {
211 	.input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
212 			 | DRM_BUS_FLAG_DE_HIGH,
213 	.setup_time_ps = 1200,
214 	.hold_time_ps = 1300,
215 };
216 
217 static int tfp410_parse_timings(struct tfp410 *dvi, bool i2c)
218 {
219 	struct drm_bridge_timings *timings = &dvi->timings;
220 	struct device_node *ep;
221 	u32 pclk_sample = 0;
222 	u32 bus_width = 24;
223 	s32 deskew = 0;
224 
225 	/* Start with defaults. */
226 	*timings = tfp410_default_timings;
227 
228 	if (i2c)
229 		/*
230 		 * In I2C mode timings are configured through the I2C interface.
231 		 * As the driver doesn't support I2C configuration yet, we just
232 		 * go with the defaults (BSEL=1, DSEL=1, DKEN=0, EDGE=1).
233 		 */
234 		return 0;
235 
236 	/*
237 	 * In non-I2C mode, timings are configured through the BSEL, DSEL, DKEN
238 	 * and EDGE pins. They are specified in DT through endpoint properties
239 	 * and vendor-specific properties.
240 	 */
241 	ep = of_graph_get_endpoint_by_regs(dvi->dev->of_node, 0, 0);
242 	if (!ep)
243 		return -EINVAL;
244 
245 	/* Get the sampling edge from the endpoint. */
246 	of_property_read_u32(ep, "pclk-sample", &pclk_sample);
247 	of_property_read_u32(ep, "bus-width", &bus_width);
248 	of_node_put(ep);
249 
250 	timings->input_bus_flags = DRM_BUS_FLAG_DE_HIGH;
251 
252 	switch (pclk_sample) {
253 	case 0:
254 		timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE
255 					 |  DRM_BUS_FLAG_SYNC_SAMPLE_NEGEDGE;
256 		break;
257 	case 1:
258 		timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
259 					 |  DRM_BUS_FLAG_SYNC_SAMPLE_POSEDGE;
260 		break;
261 	default:
262 		return -EINVAL;
263 	}
264 
265 	switch (bus_width) {
266 	case 12:
267 		dvi->bus_format = MEDIA_BUS_FMT_RGB888_2X12_LE;
268 		break;
269 	case 24:
270 		dvi->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
271 		break;
272 	default:
273 		return -EINVAL;
274 	}
275 
276 	/* Get the setup and hold time from vendor-specific properties. */
277 	of_property_read_u32(dvi->dev->of_node, "ti,deskew", (u32 *)&deskew);
278 	if (deskew < -4 || deskew > 3)
279 		return -EINVAL;
280 
281 	timings->setup_time_ps = min(0, 1200 - 350 * deskew);
282 	timings->hold_time_ps = min(0, 1300 + 350 * deskew);
283 
284 	return 0;
285 }
286 
287 static int tfp410_init(struct device *dev, bool i2c)
288 {
289 	struct device_node *node;
290 	struct tfp410 *dvi;
291 	int ret;
292 
293 	if (!dev->of_node) {
294 		dev_err(dev, "device-tree data is missing\n");
295 		return -ENXIO;
296 	}
297 
298 	dvi = devm_kzalloc(dev, sizeof(*dvi), GFP_KERNEL);
299 	if (!dvi)
300 		return -ENOMEM;
301 
302 	dvi->dev = dev;
303 	dev_set_drvdata(dev, dvi);
304 
305 	dvi->bridge.funcs = &tfp410_bridge_funcs;
306 	dvi->bridge.of_node = dev->of_node;
307 	dvi->bridge.timings = &dvi->timings;
308 	dvi->bridge.type = DRM_MODE_CONNECTOR_DVID;
309 
310 	ret = tfp410_parse_timings(dvi, i2c);
311 	if (ret)
312 		return ret;
313 
314 	/* Get the next bridge, connected to port@1. */
315 	node = of_graph_get_remote_node(dev->of_node, 1, -1);
316 	if (!node)
317 		return -ENODEV;
318 
319 	dvi->next_bridge = of_drm_find_bridge(node);
320 	of_node_put(node);
321 
322 	if (!dvi->next_bridge)
323 		return -EPROBE_DEFER;
324 
325 	/* Get the powerdown GPIO. */
326 	dvi->powerdown = devm_gpiod_get_optional(dev, "powerdown",
327 						 GPIOD_OUT_HIGH);
328 	if (IS_ERR(dvi->powerdown)) {
329 		dev_err(dev, "failed to parse powerdown gpio\n");
330 		return PTR_ERR(dvi->powerdown);
331 	}
332 
333 	/*  Register the DRM bridge. */
334 	drm_bridge_add(&dvi->bridge);
335 
336 	return 0;
337 }
338 
339 static int tfp410_fini(struct device *dev)
340 {
341 	struct tfp410 *dvi = dev_get_drvdata(dev);
342 
343 	drm_bridge_remove(&dvi->bridge);
344 
345 	return 0;
346 }
347 
348 static int tfp410_probe(struct platform_device *pdev)
349 {
350 	return tfp410_init(&pdev->dev, false);
351 }
352 
353 static int tfp410_remove(struct platform_device *pdev)
354 {
355 	return tfp410_fini(&pdev->dev);
356 }
357 
358 static const struct of_device_id tfp410_match[] = {
359 	{ .compatible = "ti,tfp410" },
360 	{},
361 };
362 MODULE_DEVICE_TABLE(of, tfp410_match);
363 
364 static struct platform_driver tfp410_platform_driver = {
365 	.probe	= tfp410_probe,
366 	.remove	= tfp410_remove,
367 	.driver	= {
368 		.name		= "tfp410-bridge",
369 		.of_match_table	= tfp410_match,
370 	},
371 };
372 
373 #if IS_ENABLED(CONFIG_I2C)
374 /* There is currently no i2c functionality. */
375 static int tfp410_i2c_probe(struct i2c_client *client,
376 			    const struct i2c_device_id *id)
377 {
378 	int reg;
379 
380 	if (!client->dev.of_node ||
381 	    of_property_read_u32(client->dev.of_node, "reg", &reg)) {
382 		dev_err(&client->dev,
383 			"Can't get i2c reg property from device-tree\n");
384 		return -ENXIO;
385 	}
386 
387 	return tfp410_init(&client->dev, true);
388 }
389 
390 static int tfp410_i2c_remove(struct i2c_client *client)
391 {
392 	return tfp410_fini(&client->dev);
393 }
394 
395 static const struct i2c_device_id tfp410_i2c_ids[] = {
396 	{ "tfp410", 0 },
397 	{ }
398 };
399 MODULE_DEVICE_TABLE(i2c, tfp410_i2c_ids);
400 
401 static struct i2c_driver tfp410_i2c_driver = {
402 	.driver = {
403 		.name	= "tfp410",
404 		.of_match_table = of_match_ptr(tfp410_match),
405 	},
406 	.id_table	= tfp410_i2c_ids,
407 	.probe		= tfp410_i2c_probe,
408 	.remove		= tfp410_i2c_remove,
409 };
410 #endif /* IS_ENABLED(CONFIG_I2C) */
411 
412 static struct {
413 	uint i2c:1;
414 	uint platform:1;
415 }  tfp410_registered_driver;
416 
417 static int __init tfp410_module_init(void)
418 {
419 	int ret;
420 
421 #if IS_ENABLED(CONFIG_I2C)
422 	ret = i2c_add_driver(&tfp410_i2c_driver);
423 	if (ret)
424 		pr_err("%s: registering i2c driver failed: %d",
425 		       __func__, ret);
426 	else
427 		tfp410_registered_driver.i2c = 1;
428 #endif
429 
430 	ret = platform_driver_register(&tfp410_platform_driver);
431 	if (ret)
432 		pr_err("%s: registering platform driver failed: %d",
433 		       __func__, ret);
434 	else
435 		tfp410_registered_driver.platform = 1;
436 
437 	if (tfp410_registered_driver.i2c ||
438 	    tfp410_registered_driver.platform)
439 		return 0;
440 
441 	return ret;
442 }
443 module_init(tfp410_module_init);
444 
445 static void __exit tfp410_module_exit(void)
446 {
447 #if IS_ENABLED(CONFIG_I2C)
448 	if (tfp410_registered_driver.i2c)
449 		i2c_del_driver(&tfp410_i2c_driver);
450 #endif
451 	if (tfp410_registered_driver.platform)
452 		platform_driver_unregister(&tfp410_platform_driver);
453 }
454 module_exit(tfp410_module_exit);
455 
456 MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
457 MODULE_DESCRIPTION("TI TFP410 DVI bridge driver");
458 MODULE_LICENSE("GPL");
459