xref: /linux/drivers/video/fbdev/omap2/omapfb/displays/encoder-tpd12s015.c (revision d2c9a99135da931377240942d44f3dea104cedb8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * TPD12S015 HDMI ESD protection & level shifter chip driver
4  *
5  * Copyright (C) 2013 Texas Instruments
6  * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
7  */
8 
9 #include <linux/completion.h>
10 #include <linux/delay.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/platform_device.h>
14 #include <linux/gpio/consumer.h>
15 
16 #include <video/omapfb_dss.h>
17 
18 struct panel_drv_data {
19 	struct omap_dss_device dssdev;
20 	struct omap_dss_device *in;
21 
22 	struct gpio_desc *ct_cp_hpd_gpio;
23 	struct gpio_desc *ls_oe_gpio;
24 	struct gpio_desc *hpd_gpio;
25 
26 	struct omap_video_timings timings;
27 };
28 
29 #define to_panel_data(x) container_of(x, struct panel_drv_data, dssdev)
30 
tpd_connect(struct omap_dss_device * dssdev,struct omap_dss_device * dst)31 static int tpd_connect(struct omap_dss_device *dssdev,
32 		struct omap_dss_device *dst)
33 {
34 	struct panel_drv_data *ddata = to_panel_data(dssdev);
35 	struct omap_dss_device *in = ddata->in;
36 	int r;
37 
38 	r = in->ops.hdmi->connect(in, dssdev);
39 	if (r)
40 		return r;
41 
42 	dst->src = dssdev;
43 	dssdev->dst = dst;
44 
45 	if (ddata->ct_cp_hpd_gpio) {
46 		gpiod_set_value_cansleep(ddata->ct_cp_hpd_gpio, 1);
47 		/* DC-DC converter needs at max 300us to get to 90% of 5V */
48 		udelay(300);
49 	}
50 
51 	return 0;
52 }
53 
tpd_disconnect(struct omap_dss_device * dssdev,struct omap_dss_device * dst)54 static void tpd_disconnect(struct omap_dss_device *dssdev,
55 		struct omap_dss_device *dst)
56 {
57 	struct panel_drv_data *ddata = to_panel_data(dssdev);
58 	struct omap_dss_device *in = ddata->in;
59 
60 	WARN_ON(dst != dssdev->dst);
61 
62 	if (dst != dssdev->dst)
63 		return;
64 
65 	gpiod_set_value_cansleep(ddata->ct_cp_hpd_gpio, 0);
66 
67 	dst->src = NULL;
68 	dssdev->dst = NULL;
69 
70 	in->ops.hdmi->disconnect(in, &ddata->dssdev);
71 }
72 
tpd_enable(struct omap_dss_device * dssdev)73 static int tpd_enable(struct omap_dss_device *dssdev)
74 {
75 	struct panel_drv_data *ddata = to_panel_data(dssdev);
76 	struct omap_dss_device *in = ddata->in;
77 	int r;
78 
79 	if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE)
80 		return 0;
81 
82 	in->ops.hdmi->set_timings(in, &ddata->timings);
83 
84 	r = in->ops.hdmi->enable(in);
85 	if (r)
86 		return r;
87 
88 	dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
89 
90 	return r;
91 }
92 
tpd_disable(struct omap_dss_device * dssdev)93 static void tpd_disable(struct omap_dss_device *dssdev)
94 {
95 	struct panel_drv_data *ddata = to_panel_data(dssdev);
96 	struct omap_dss_device *in = ddata->in;
97 
98 	if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE)
99 		return;
100 
101 	in->ops.hdmi->disable(in);
102 
103 	dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
104 }
105 
tpd_set_timings(struct omap_dss_device * dssdev,struct omap_video_timings * timings)106 static void tpd_set_timings(struct omap_dss_device *dssdev,
107 		struct omap_video_timings *timings)
108 {
109 	struct panel_drv_data *ddata = to_panel_data(dssdev);
110 	struct omap_dss_device *in = ddata->in;
111 
112 	ddata->timings = *timings;
113 	dssdev->panel.timings = *timings;
114 
115 	in->ops.hdmi->set_timings(in, timings);
116 }
117 
tpd_get_timings(struct omap_dss_device * dssdev,struct omap_video_timings * timings)118 static void tpd_get_timings(struct omap_dss_device *dssdev,
119 		struct omap_video_timings *timings)
120 {
121 	struct panel_drv_data *ddata = to_panel_data(dssdev);
122 
123 	*timings = ddata->timings;
124 }
125 
tpd_check_timings(struct omap_dss_device * dssdev,struct omap_video_timings * timings)126 static int tpd_check_timings(struct omap_dss_device *dssdev,
127 		struct omap_video_timings *timings)
128 {
129 	struct panel_drv_data *ddata = to_panel_data(dssdev);
130 	struct omap_dss_device *in = ddata->in;
131 	int r;
132 
133 	r = in->ops.hdmi->check_timings(in, timings);
134 
135 	return r;
136 }
137 
tpd_read_edid(struct omap_dss_device * dssdev,u8 * edid,int len)138 static int tpd_read_edid(struct omap_dss_device *dssdev,
139 		u8 *edid, int len)
140 {
141 	struct panel_drv_data *ddata = to_panel_data(dssdev);
142 	struct omap_dss_device *in = ddata->in;
143 	int r;
144 
145 	if (!gpiod_get_value_cansleep(ddata->hpd_gpio))
146 		return -ENODEV;
147 
148 	gpiod_set_value_cansleep(ddata->ls_oe_gpio, 1);
149 
150 	r = in->ops.hdmi->read_edid(in, edid, len);
151 
152 	gpiod_set_value_cansleep(ddata->ls_oe_gpio, 0);
153 
154 	return r;
155 }
156 
tpd_detect(struct omap_dss_device * dssdev)157 static bool tpd_detect(struct omap_dss_device *dssdev)
158 {
159 	struct panel_drv_data *ddata = to_panel_data(dssdev);
160 
161 	return gpiod_get_value_cansleep(ddata->hpd_gpio);
162 }
163 
tpd_set_infoframe(struct omap_dss_device * dssdev,const struct hdmi_avi_infoframe * avi)164 static int tpd_set_infoframe(struct omap_dss_device *dssdev,
165 		const struct hdmi_avi_infoframe *avi)
166 {
167 	struct panel_drv_data *ddata = to_panel_data(dssdev);
168 	struct omap_dss_device *in = ddata->in;
169 
170 	return in->ops.hdmi->set_infoframe(in, avi);
171 }
172 
tpd_set_hdmi_mode(struct omap_dss_device * dssdev,bool hdmi_mode)173 static int tpd_set_hdmi_mode(struct omap_dss_device *dssdev,
174 		bool hdmi_mode)
175 {
176 	struct panel_drv_data *ddata = to_panel_data(dssdev);
177 	struct omap_dss_device *in = ddata->in;
178 
179 	return in->ops.hdmi->set_hdmi_mode(in, hdmi_mode);
180 }
181 
182 static const struct omapdss_hdmi_ops tpd_hdmi_ops = {
183 	.connect		= tpd_connect,
184 	.disconnect		= tpd_disconnect,
185 
186 	.enable			= tpd_enable,
187 	.disable		= tpd_disable,
188 
189 	.check_timings		= tpd_check_timings,
190 	.set_timings		= tpd_set_timings,
191 	.get_timings		= tpd_get_timings,
192 
193 	.read_edid		= tpd_read_edid,
194 	.detect			= tpd_detect,
195 	.set_infoframe		= tpd_set_infoframe,
196 	.set_hdmi_mode		= tpd_set_hdmi_mode,
197 };
198 
tpd_probe_of(struct platform_device * pdev)199 static int tpd_probe_of(struct platform_device *pdev)
200 {
201 	struct panel_drv_data *ddata = platform_get_drvdata(pdev);
202 	struct device_node *node = pdev->dev.of_node;
203 	struct omap_dss_device *in;
204 
205 	in = omapdss_of_find_source_for_first_ep(node);
206 	if (IS_ERR(in)) {
207 		dev_err(&pdev->dev, "failed to find video source\n");
208 		return PTR_ERR(in);
209 	}
210 
211 	ddata->in = in;
212 
213 	return 0;
214 }
215 
tpd_probe(struct platform_device * pdev)216 static int tpd_probe(struct platform_device *pdev)
217 {
218 	struct omap_dss_device *dssdev;
219 	struct panel_drv_data *ddata;
220 	int r;
221 	struct gpio_desc *gpio;
222 
223 	ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
224 	if (!ddata)
225 		return -ENOMEM;
226 
227 	platform_set_drvdata(pdev, ddata);
228 
229 	if (pdev->dev.of_node) {
230 		r = tpd_probe_of(pdev);
231 		if (r)
232 			return r;
233 	} else {
234 		return -ENODEV;
235 	}
236 
237 	gpio = devm_gpiod_get_index_optional(&pdev->dev, NULL, 0,
238 		GPIOD_OUT_LOW);
239 	if (IS_ERR(gpio)) {
240 		r = PTR_ERR(gpio);
241 		goto err_gpio;
242 	}
243 
244 	ddata->ct_cp_hpd_gpio = gpio;
245 
246 	gpio = devm_gpiod_get_index_optional(&pdev->dev, NULL, 1,
247 		GPIOD_OUT_LOW);
248 	if (IS_ERR(gpio)) {
249 		r = PTR_ERR(gpio);
250 		goto err_gpio;
251 	}
252 
253 	ddata->ls_oe_gpio = gpio;
254 
255 	gpio = devm_gpiod_get_index(&pdev->dev, NULL, 2,
256 		GPIOD_IN);
257 	if (IS_ERR(gpio)) {
258 		r = PTR_ERR(gpio);
259 		goto err_gpio;
260 	}
261 
262 	ddata->hpd_gpio = gpio;
263 
264 	dssdev = &ddata->dssdev;
265 	dssdev->ops.hdmi = &tpd_hdmi_ops;
266 	dssdev->dev = &pdev->dev;
267 	dssdev->type = OMAP_DISPLAY_TYPE_HDMI;
268 	dssdev->output_type = OMAP_DISPLAY_TYPE_HDMI;
269 	dssdev->owner = THIS_MODULE;
270 	dssdev->port_num = 1;
271 
272 	r = omapdss_register_output(dssdev);
273 	if (r) {
274 		dev_err(&pdev->dev, "Failed to register output\n");
275 		goto err_reg;
276 	}
277 
278 	return 0;
279 err_reg:
280 err_gpio:
281 	omap_dss_put_device(ddata->in);
282 	return r;
283 }
284 
tpd_remove(struct platform_device * pdev)285 static void tpd_remove(struct platform_device *pdev)
286 {
287 	struct panel_drv_data *ddata = platform_get_drvdata(pdev);
288 	struct omap_dss_device *dssdev = &ddata->dssdev;
289 	struct omap_dss_device *in = ddata->in;
290 
291 	omapdss_unregister_output(&ddata->dssdev);
292 
293 	WARN_ON(omapdss_device_is_enabled(dssdev));
294 	if (omapdss_device_is_enabled(dssdev))
295 		tpd_disable(dssdev);
296 
297 	WARN_ON(omapdss_device_is_connected(dssdev));
298 	if (omapdss_device_is_connected(dssdev))
299 		tpd_disconnect(dssdev, dssdev->dst);
300 
301 	omap_dss_put_device(in);
302 }
303 
304 static const struct of_device_id tpd_of_match[] = {
305 	{ .compatible = "omapdss,ti,tpd12s015", },
306 	{},
307 };
308 
309 MODULE_DEVICE_TABLE(of, tpd_of_match);
310 
311 static struct platform_driver tpd_driver = {
312 	.probe	= tpd_probe,
313 	.remove	= tpd_remove,
314 	.driver	= {
315 		.name	= "tpd12s015",
316 		.of_match_table = tpd_of_match,
317 	},
318 };
319 
320 module_platform_driver(tpd_driver);
321 
322 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
323 MODULE_DESCRIPTION("TPD12S015 driver");
324 MODULE_LICENSE("GPL");
325