• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_crtc_helper.h>
24 
25 #define HOTPLUG_DEBOUNCE_MS		1100
26 
27 struct tfp410 {
28 	struct drm_bridge	bridge;
29 	struct drm_connector	connector;
30 
31 	struct i2c_adapter	*ddc;
32 	struct gpio_desc	*hpd;
33 	struct delayed_work	hpd_work;
34 
35 	struct device *dev;
36 };
37 
38 static inline struct tfp410 *
drm_bridge_to_tfp410(struct drm_bridge * bridge)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 *
drm_connector_to_tfp410(struct drm_connector * connector)45 drm_connector_to_tfp410(struct drm_connector *connector)
46 {
47 	return container_of(connector, struct tfp410, connector);
48 }
49 
tfp410_get_modes(struct drm_connector * connector)50 static int tfp410_get_modes(struct drm_connector *connector)
51 {
52 	struct tfp410 *dvi = drm_connector_to_tfp410(connector);
53 	struct edid *edid;
54 	int ret;
55 
56 	if (!dvi->ddc)
57 		goto fallback;
58 
59 	edid = drm_get_edid(connector, dvi->ddc);
60 	if (!edid) {
61 		DRM_INFO("EDID read failed. Fallback to standard modes\n");
62 		goto fallback;
63 	}
64 
65 	drm_connector_update_edid_property(connector, edid);
66 
67 	ret = drm_add_edid_modes(connector, edid);
68 
69 	kfree(edid);
70 
71 	return ret;
72 
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
tfp410_connector_detect(struct drm_connector * connector,bool force)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 
tfp410_attach(struct drm_bridge * bridge)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)
129 		dvi->connector.polled = DRM_CONNECTOR_POLL_HPD;
130 
131 	drm_connector_helper_add(&dvi->connector,
132 				 &tfp410_con_helper_funcs);
133 	ret = drm_connector_init(bridge->dev, &dvi->connector,
134 				 &tfp410_con_funcs, DRM_MODE_CONNECTOR_HDMIA);
135 	if (ret) {
136 		dev_err(dvi->dev, "drm_connector_init() failed: %d\n", ret);
137 		return ret;
138 	}
139 
140 	drm_connector_attach_encoder(&dvi->connector,
141 					  bridge->encoder);
142 
143 	return 0;
144 }
145 
146 static const struct drm_bridge_funcs tfp410_bridge_funcs = {
147 	.attach		= tfp410_attach,
148 };
149 
tfp410_hpd_work_func(struct work_struct * work)150 static void tfp410_hpd_work_func(struct work_struct *work)
151 {
152 	struct tfp410 *dvi;
153 
154 	dvi = container_of(work, struct tfp410, hpd_work.work);
155 
156 	if (dvi->bridge.dev)
157 		drm_helper_hpd_irq_event(dvi->bridge.dev);
158 }
159 
tfp410_hpd_irq_thread(int irq,void * arg)160 static irqreturn_t tfp410_hpd_irq_thread(int irq, void *arg)
161 {
162 	struct tfp410 *dvi = arg;
163 
164 	mod_delayed_work(system_wq, &dvi->hpd_work,
165 			msecs_to_jiffies(HOTPLUG_DEBOUNCE_MS));
166 
167 	return IRQ_HANDLED;
168 }
169 
tfp410_get_connector_properties(struct tfp410 * dvi)170 static int tfp410_get_connector_properties(struct tfp410 *dvi)
171 {
172 	struct device_node *connector_node, *ddc_phandle;
173 	int ret = 0;
174 
175 	/* port@1 is the connector node */
176 	connector_node = of_graph_get_remote_node(dvi->dev->of_node, 1, -1);
177 	if (!connector_node)
178 		return -ENODEV;
179 
180 	dvi->hpd = fwnode_get_named_gpiod(&connector_node->fwnode,
181 					"hpd-gpios", 0, GPIOD_IN, "hpd");
182 	if (IS_ERR(dvi->hpd)) {
183 		ret = PTR_ERR(dvi->hpd);
184 		dvi->hpd = NULL;
185 		if (ret == -ENOENT)
186 			ret = 0;
187 		else
188 			goto fail;
189 	}
190 
191 	ddc_phandle = of_parse_phandle(connector_node, "ddc-i2c-bus", 0);
192 	if (!ddc_phandle)
193 		goto fail;
194 
195 	dvi->ddc = of_get_i2c_adapter_by_node(ddc_phandle);
196 	if (dvi->ddc)
197 		dev_info(dvi->dev, "Connector's ddc i2c bus found\n");
198 	else
199 		ret = -EPROBE_DEFER;
200 
201 	of_node_put(ddc_phandle);
202 
203 fail:
204 	of_node_put(connector_node);
205 	return ret;
206 }
207 
tfp410_init(struct device * dev)208 static int tfp410_init(struct device *dev)
209 {
210 	struct tfp410 *dvi;
211 	int ret;
212 
213 	if (!dev->of_node) {
214 		dev_err(dev, "device-tree data is missing\n");
215 		return -ENXIO;
216 	}
217 
218 	dvi = devm_kzalloc(dev, sizeof(*dvi), GFP_KERNEL);
219 	if (!dvi)
220 		return -ENOMEM;
221 	dev_set_drvdata(dev, dvi);
222 
223 	dvi->bridge.funcs = &tfp410_bridge_funcs;
224 	dvi->bridge.of_node = dev->of_node;
225 	dvi->dev = dev;
226 
227 	ret = tfp410_get_connector_properties(dvi);
228 	if (ret)
229 		goto fail;
230 
231 	if (dvi->hpd) {
232 		INIT_DELAYED_WORK(&dvi->hpd_work, tfp410_hpd_work_func);
233 
234 		ret = devm_request_threaded_irq(dev, gpiod_to_irq(dvi->hpd),
235 			NULL, tfp410_hpd_irq_thread, IRQF_TRIGGER_RISING |
236 			IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
237 			"hdmi-hpd", dvi);
238 		if (ret) {
239 			DRM_ERROR("failed to register hpd interrupt\n");
240 			goto fail;
241 		}
242 	}
243 
244 	drm_bridge_add(&dvi->bridge);
245 
246 	return 0;
247 fail:
248 	i2c_put_adapter(dvi->ddc);
249 	if (dvi->hpd)
250 		gpiod_put(dvi->hpd);
251 	return ret;
252 }
253 
tfp410_fini(struct device * dev)254 static int tfp410_fini(struct device *dev)
255 {
256 	struct tfp410 *dvi = dev_get_drvdata(dev);
257 
258 	cancel_delayed_work_sync(&dvi->hpd_work);
259 
260 	drm_bridge_remove(&dvi->bridge);
261 
262 	if (dvi->ddc)
263 		i2c_put_adapter(dvi->ddc);
264 	if (dvi->hpd)
265 		gpiod_put(dvi->hpd);
266 
267 	return 0;
268 }
269 
tfp410_probe(struct platform_device * pdev)270 static int tfp410_probe(struct platform_device *pdev)
271 {
272 	return tfp410_init(&pdev->dev);
273 }
274 
tfp410_remove(struct platform_device * pdev)275 static int tfp410_remove(struct platform_device *pdev)
276 {
277 	return tfp410_fini(&pdev->dev);
278 }
279 
280 static const struct of_device_id tfp410_match[] = {
281 	{ .compatible = "ti,tfp410" },
282 	{},
283 };
284 MODULE_DEVICE_TABLE(of, tfp410_match);
285 
286 static struct platform_driver tfp410_platform_driver = {
287 	.probe	= tfp410_probe,
288 	.remove	= tfp410_remove,
289 	.driver	= {
290 		.name		= "tfp410-bridge",
291 		.of_match_table	= tfp410_match,
292 	},
293 };
294 
295 #if IS_ENABLED(CONFIG_I2C)
296 /* There is currently no i2c functionality. */
tfp410_i2c_probe(struct i2c_client * client,const struct i2c_device_id * id)297 static int tfp410_i2c_probe(struct i2c_client *client,
298 			    const struct i2c_device_id *id)
299 {
300 	int reg;
301 
302 	if (!client->dev.of_node ||
303 	    of_property_read_u32(client->dev.of_node, "reg", &reg)) {
304 		dev_err(&client->dev,
305 			"Can't get i2c reg property from device-tree\n");
306 		return -ENXIO;
307 	}
308 
309 	return tfp410_init(&client->dev);
310 }
311 
tfp410_i2c_remove(struct i2c_client * client)312 static int tfp410_i2c_remove(struct i2c_client *client)
313 {
314 	return tfp410_fini(&client->dev);
315 }
316 
317 static const struct i2c_device_id tfp410_i2c_ids[] = {
318 	{ "tfp410", 0 },
319 	{ }
320 };
321 MODULE_DEVICE_TABLE(i2c, tfp410_i2c_ids);
322 
323 static struct i2c_driver tfp410_i2c_driver = {
324 	.driver = {
325 		.name	= "tfp410",
326 		.of_match_table = of_match_ptr(tfp410_match),
327 	},
328 	.id_table	= tfp410_i2c_ids,
329 	.probe		= tfp410_i2c_probe,
330 	.remove		= tfp410_i2c_remove,
331 };
332 #endif /* IS_ENABLED(CONFIG_I2C) */
333 
334 static struct {
335 	uint i2c:1;
336 	uint platform:1;
337 }  tfp410_registered_driver;
338 
tfp410_module_init(void)339 static int __init tfp410_module_init(void)
340 {
341 	int ret;
342 
343 #if IS_ENABLED(CONFIG_I2C)
344 	ret = i2c_add_driver(&tfp410_i2c_driver);
345 	if (ret)
346 		pr_err("%s: registering i2c driver failed: %d",
347 		       __func__, ret);
348 	else
349 		tfp410_registered_driver.i2c = 1;
350 #endif
351 
352 	ret = platform_driver_register(&tfp410_platform_driver);
353 	if (ret)
354 		pr_err("%s: registering platform driver failed: %d",
355 		       __func__, ret);
356 	else
357 		tfp410_registered_driver.platform = 1;
358 
359 	if (tfp410_registered_driver.i2c ||
360 	    tfp410_registered_driver.platform)
361 		return 0;
362 
363 	return ret;
364 }
365 module_init(tfp410_module_init);
366 
tfp410_module_exit(void)367 static void __exit tfp410_module_exit(void)
368 {
369 #if IS_ENABLED(CONFIG_I2C)
370 	if (tfp410_registered_driver.i2c)
371 		i2c_del_driver(&tfp410_i2c_driver);
372 #endif
373 	if (tfp410_registered_driver.platform)
374 		platform_driver_unregister(&tfp410_platform_driver);
375 }
376 module_exit(tfp410_module_exit);
377 
378 MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
379 MODULE_DESCRIPTION("TI TFP410 DVI bridge driver");
380 MODULE_LICENSE("GPL");
381