• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * TPD12S015 HDMI ESD protection & level shifter chip driver
3  *
4  * Copyright (C) 2013 Texas Instruments
5  * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License version 2 as published by
9  * the Free Software Foundation.
10  */
11 
12 #include <linux/completion.h>
13 #include <linux/delay.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/platform_device.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/mutex.h>
19 
20 #include "../dss/omapdss.h"
21 
22 struct panel_drv_data {
23 	struct omap_dss_device dssdev;
24 	struct omap_dss_device *in;
25 	void (*hpd_cb)(void *cb_data, enum drm_connector_status status);
26 	void *hpd_cb_data;
27 	bool hpd_enabled;
28 	struct mutex hpd_lock;
29 
30 	struct gpio_desc *ct_cp_hpd_gpio;
31 	struct gpio_desc *ls_oe_gpio;
32 	struct gpio_desc *hpd_gpio;
33 
34 	struct videomode vm;
35 };
36 
37 #define to_panel_data(x) container_of(x, struct panel_drv_data, dssdev)
38 
tpd_connect(struct omap_dss_device * dssdev,struct omap_dss_device * dst)39 static int tpd_connect(struct omap_dss_device *dssdev,
40 		struct omap_dss_device *dst)
41 {
42 	struct panel_drv_data *ddata = to_panel_data(dssdev);
43 	struct omap_dss_device *in = ddata->in;
44 	int r;
45 
46 	r = in->ops.hdmi->connect(in, dssdev);
47 	if (r)
48 		return r;
49 
50 	dst->src = dssdev;
51 	dssdev->dst = dst;
52 
53 	gpiod_set_value_cansleep(ddata->ct_cp_hpd_gpio, 1);
54 	/* DC-DC converter needs at max 300us to get to 90% of 5V */
55 	udelay(300);
56 
57 	return 0;
58 }
59 
tpd_disconnect(struct omap_dss_device * dssdev,struct omap_dss_device * dst)60 static void tpd_disconnect(struct omap_dss_device *dssdev,
61 		struct omap_dss_device *dst)
62 {
63 	struct panel_drv_data *ddata = to_panel_data(dssdev);
64 	struct omap_dss_device *in = ddata->in;
65 
66 	WARN_ON(dst != dssdev->dst);
67 
68 	if (dst != dssdev->dst)
69 		return;
70 
71 	gpiod_set_value_cansleep(ddata->ct_cp_hpd_gpio, 0);
72 
73 	dst->src = NULL;
74 	dssdev->dst = NULL;
75 
76 	in->ops.hdmi->disconnect(in, &ddata->dssdev);
77 }
78 
tpd_enable(struct omap_dss_device * dssdev)79 static int tpd_enable(struct omap_dss_device *dssdev)
80 {
81 	struct panel_drv_data *ddata = to_panel_data(dssdev);
82 	struct omap_dss_device *in = ddata->in;
83 	int r;
84 
85 	if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE)
86 		return 0;
87 
88 	in->ops.hdmi->set_timings(in, &ddata->vm);
89 
90 	r = in->ops.hdmi->enable(in);
91 	if (r)
92 		return r;
93 
94 	dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
95 
96 	return r;
97 }
98 
tpd_disable(struct omap_dss_device * dssdev)99 static void tpd_disable(struct omap_dss_device *dssdev)
100 {
101 	struct panel_drv_data *ddata = to_panel_data(dssdev);
102 	struct omap_dss_device *in = ddata->in;
103 
104 	if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE)
105 		return;
106 
107 	in->ops.hdmi->disable(in);
108 
109 	dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
110 }
111 
tpd_set_timings(struct omap_dss_device * dssdev,struct videomode * vm)112 static void tpd_set_timings(struct omap_dss_device *dssdev,
113 			    struct videomode *vm)
114 {
115 	struct panel_drv_data *ddata = to_panel_data(dssdev);
116 	struct omap_dss_device *in = ddata->in;
117 
118 	ddata->vm = *vm;
119 	dssdev->panel.vm = *vm;
120 
121 	in->ops.hdmi->set_timings(in, vm);
122 }
123 
tpd_get_timings(struct omap_dss_device * dssdev,struct videomode * vm)124 static void tpd_get_timings(struct omap_dss_device *dssdev,
125 			    struct videomode *vm)
126 {
127 	struct panel_drv_data *ddata = to_panel_data(dssdev);
128 
129 	*vm = ddata->vm;
130 }
131 
tpd_check_timings(struct omap_dss_device * dssdev,struct videomode * vm)132 static int tpd_check_timings(struct omap_dss_device *dssdev,
133 			     struct videomode *vm)
134 {
135 	struct panel_drv_data *ddata = to_panel_data(dssdev);
136 	struct omap_dss_device *in = ddata->in;
137 	int r;
138 
139 	r = in->ops.hdmi->check_timings(in, vm);
140 
141 	return r;
142 }
143 
tpd_read_edid(struct omap_dss_device * dssdev,u8 * edid,int len)144 static int tpd_read_edid(struct omap_dss_device *dssdev,
145 		u8 *edid, int len)
146 {
147 	struct panel_drv_data *ddata = to_panel_data(dssdev);
148 	struct omap_dss_device *in = ddata->in;
149 	int r;
150 
151 	if (!gpiod_get_value_cansleep(ddata->hpd_gpio))
152 		return -ENODEV;
153 
154 	gpiod_set_value_cansleep(ddata->ls_oe_gpio, 1);
155 
156 	r = in->ops.hdmi->read_edid(in, edid, len);
157 
158 	gpiod_set_value_cansleep(ddata->ls_oe_gpio, 0);
159 
160 	return r;
161 }
162 
tpd_detect(struct omap_dss_device * dssdev)163 static bool tpd_detect(struct omap_dss_device *dssdev)
164 {
165 	struct panel_drv_data *ddata = to_panel_data(dssdev);
166 
167 	return gpiod_get_value_cansleep(ddata->hpd_gpio);
168 }
169 
tpd_register_hpd_cb(struct omap_dss_device * dssdev,void (* cb)(void * cb_data,enum drm_connector_status status),void * cb_data)170 static int tpd_register_hpd_cb(struct omap_dss_device *dssdev,
171 			       void (*cb)(void *cb_data,
172 					  enum drm_connector_status status),
173 			       void *cb_data)
174 {
175 	struct panel_drv_data *ddata = to_panel_data(dssdev);
176 
177 	mutex_lock(&ddata->hpd_lock);
178 	ddata->hpd_cb = cb;
179 	ddata->hpd_cb_data = cb_data;
180 	mutex_unlock(&ddata->hpd_lock);
181 
182 	return 0;
183 }
184 
tpd_unregister_hpd_cb(struct omap_dss_device * dssdev)185 static void tpd_unregister_hpd_cb(struct omap_dss_device *dssdev)
186 {
187 	struct panel_drv_data *ddata = to_panel_data(dssdev);
188 
189 	mutex_lock(&ddata->hpd_lock);
190 	ddata->hpd_cb = NULL;
191 	ddata->hpd_cb_data = NULL;
192 	mutex_unlock(&ddata->hpd_lock);
193 }
194 
tpd_enable_hpd(struct omap_dss_device * dssdev)195 static void tpd_enable_hpd(struct omap_dss_device *dssdev)
196 {
197 	struct panel_drv_data *ddata = to_panel_data(dssdev);
198 
199 	mutex_lock(&ddata->hpd_lock);
200 	ddata->hpd_enabled = true;
201 	mutex_unlock(&ddata->hpd_lock);
202 }
203 
tpd_disable_hpd(struct omap_dss_device * dssdev)204 static void tpd_disable_hpd(struct omap_dss_device *dssdev)
205 {
206 	struct panel_drv_data *ddata = to_panel_data(dssdev);
207 
208 	mutex_lock(&ddata->hpd_lock);
209 	ddata->hpd_enabled = false;
210 	mutex_unlock(&ddata->hpd_lock);
211 }
212 
tpd_set_infoframe(struct omap_dss_device * dssdev,const struct hdmi_avi_infoframe * avi)213 static int tpd_set_infoframe(struct omap_dss_device *dssdev,
214 		const struct hdmi_avi_infoframe *avi)
215 {
216 	struct panel_drv_data *ddata = to_panel_data(dssdev);
217 	struct omap_dss_device *in = ddata->in;
218 
219 	return in->ops.hdmi->set_infoframe(in, avi);
220 }
221 
tpd_set_hdmi_mode(struct omap_dss_device * dssdev,bool hdmi_mode)222 static int tpd_set_hdmi_mode(struct omap_dss_device *dssdev,
223 		bool hdmi_mode)
224 {
225 	struct panel_drv_data *ddata = to_panel_data(dssdev);
226 	struct omap_dss_device *in = ddata->in;
227 
228 	return in->ops.hdmi->set_hdmi_mode(in, hdmi_mode);
229 }
230 
231 static const struct omapdss_hdmi_ops tpd_hdmi_ops = {
232 	.connect		= tpd_connect,
233 	.disconnect		= tpd_disconnect,
234 
235 	.enable			= tpd_enable,
236 	.disable		= tpd_disable,
237 
238 	.check_timings		= tpd_check_timings,
239 	.set_timings		= tpd_set_timings,
240 	.get_timings		= tpd_get_timings,
241 
242 	.read_edid		= tpd_read_edid,
243 	.detect			= tpd_detect,
244 	.register_hpd_cb	= tpd_register_hpd_cb,
245 	.unregister_hpd_cb	= tpd_unregister_hpd_cb,
246 	.enable_hpd		= tpd_enable_hpd,
247 	.disable_hpd		= tpd_disable_hpd,
248 	.set_infoframe		= tpd_set_infoframe,
249 	.set_hdmi_mode		= tpd_set_hdmi_mode,
250 };
251 
tpd_hpd_isr(int irq,void * data)252 static irqreturn_t tpd_hpd_isr(int irq, void *data)
253 {
254 	struct panel_drv_data *ddata = data;
255 
256 	mutex_lock(&ddata->hpd_lock);
257 	if (ddata->hpd_enabled && ddata->hpd_cb) {
258 		enum drm_connector_status status;
259 
260 		if (tpd_detect(&ddata->dssdev))
261 			status = connector_status_connected;
262 		else
263 			status = connector_status_disconnected;
264 
265 		ddata->hpd_cb(ddata->hpd_cb_data, status);
266 	}
267 	mutex_unlock(&ddata->hpd_lock);
268 
269 	return IRQ_HANDLED;
270 }
271 
tpd_probe_of(struct platform_device * pdev)272 static int tpd_probe_of(struct platform_device *pdev)
273 {
274 	struct panel_drv_data *ddata = platform_get_drvdata(pdev);
275 	struct device_node *node = pdev->dev.of_node;
276 	struct omap_dss_device *in;
277 
278 	in = omapdss_of_find_source_for_first_ep(node);
279 	if (IS_ERR(in)) {
280 		dev_err(&pdev->dev, "failed to find video source\n");
281 		return PTR_ERR(in);
282 	}
283 
284 	ddata->in = in;
285 
286 	return 0;
287 }
288 
tpd_probe(struct platform_device * pdev)289 static int tpd_probe(struct platform_device *pdev)
290 {
291 	struct omap_dss_device *in, *dssdev;
292 	struct panel_drv_data *ddata;
293 	int r;
294 	struct gpio_desc *gpio;
295 
296 	ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
297 	if (!ddata)
298 		return -ENOMEM;
299 
300 	platform_set_drvdata(pdev, ddata);
301 
302 	if (!pdev->dev.of_node)
303 		return -ENODEV;
304 
305 	r = tpd_probe_of(pdev);
306 	if (r)
307 		return r;
308 
309 	gpio = devm_gpiod_get_index_optional(&pdev->dev, NULL, 0,
310 		 GPIOD_OUT_LOW);
311 	if (IS_ERR(gpio)) {
312 		r = PTR_ERR(gpio);
313 		goto err_gpio;
314 	}
315 
316 	ddata->ct_cp_hpd_gpio = gpio;
317 
318 	gpio = devm_gpiod_get_index_optional(&pdev->dev, NULL, 1,
319 		 GPIOD_OUT_LOW);
320 	if (IS_ERR(gpio)) {
321 		r = PTR_ERR(gpio);
322 		goto err_gpio;
323 	}
324 
325 	ddata->ls_oe_gpio = gpio;
326 
327 	gpio = devm_gpiod_get_index(&pdev->dev, NULL, 2,
328 		GPIOD_IN);
329 	if (IS_ERR(gpio)) {
330 		r = PTR_ERR(gpio);
331 		goto err_gpio;
332 	}
333 
334 	ddata->hpd_gpio = gpio;
335 
336 	mutex_init(&ddata->hpd_lock);
337 
338 	r = devm_request_threaded_irq(&pdev->dev, gpiod_to_irq(ddata->hpd_gpio),
339 		NULL, tpd_hpd_isr,
340 		IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
341 		"tpd12s015 hpd", ddata);
342 	if (r)
343 		goto err_gpio;
344 
345 	dssdev = &ddata->dssdev;
346 	dssdev->ops.hdmi = &tpd_hdmi_ops;
347 	dssdev->dev = &pdev->dev;
348 	dssdev->type = OMAP_DISPLAY_TYPE_HDMI;
349 	dssdev->output_type = OMAP_DISPLAY_TYPE_HDMI;
350 	dssdev->owner = THIS_MODULE;
351 	dssdev->port_num = 1;
352 
353 	in = ddata->in;
354 
355 	r = omapdss_register_output(dssdev);
356 	if (r) {
357 		dev_err(&pdev->dev, "Failed to register output\n");
358 		goto err_reg;
359 	}
360 
361 	return 0;
362 err_reg:
363 err_gpio:
364 	omap_dss_put_device(ddata->in);
365 	return r;
366 }
367 
tpd_remove(struct platform_device * pdev)368 static int __exit tpd_remove(struct platform_device *pdev)
369 {
370 	struct panel_drv_data *ddata = platform_get_drvdata(pdev);
371 	struct omap_dss_device *dssdev = &ddata->dssdev;
372 	struct omap_dss_device *in = ddata->in;
373 
374 	omapdss_unregister_output(&ddata->dssdev);
375 
376 	WARN_ON(omapdss_device_is_enabled(dssdev));
377 	if (omapdss_device_is_enabled(dssdev))
378 		tpd_disable(dssdev);
379 
380 	WARN_ON(omapdss_device_is_connected(dssdev));
381 	if (omapdss_device_is_connected(dssdev))
382 		tpd_disconnect(dssdev, dssdev->dst);
383 
384 	omap_dss_put_device(in);
385 
386 	return 0;
387 }
388 
389 static const struct of_device_id tpd_of_match[] = {
390 	{ .compatible = "omapdss,ti,tpd12s015", },
391 	{},
392 };
393 
394 MODULE_DEVICE_TABLE(of, tpd_of_match);
395 
396 static struct platform_driver tpd_driver = {
397 	.probe	= tpd_probe,
398 	.remove	= __exit_p(tpd_remove),
399 	.driver	= {
400 		.name	= "tpd12s015",
401 		.of_match_table = tpd_of_match,
402 		.suppress_bind_attrs = true,
403 	},
404 };
405 
406 module_platform_driver(tpd_driver);
407 
408 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
409 MODULE_DESCRIPTION("TPD12S015 driver");
410 MODULE_LICENSE("GPL");
411