1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Analog TV Connector driver
4  *
5  * Copyright (C) 2013 Texas Instruments
6  * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
7  */
8 
9 #include <linux/slab.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/of.h>
13 
14 #include <video/omapfb_dss.h>
15 #include <video/omap-panel-data.h>
16 
17 struct panel_drv_data {
18 	struct omap_dss_device dssdev;
19 	struct omap_dss_device *in;
20 
21 	struct device *dev;
22 
23 	struct omap_video_timings timings;
24 
25 	bool invert_polarity;
26 };
27 
28 static const struct omap_video_timings tvc_pal_timings = {
29 	.x_res		= 720,
30 	.y_res		= 574,
31 	.pixelclock	= 13500000,
32 	.hsw		= 64,
33 	.hfp		= 12,
34 	.hbp		= 68,
35 	.vsw		= 5,
36 	.vfp		= 5,
37 	.vbp		= 41,
38 
39 	.interlace	= true,
40 };
41 
42 static const struct of_device_id tvc_of_match[];
43 
44 #define to_panel_data(x) container_of(x, struct panel_drv_data, dssdev)
45 
46 static int tvc_connect(struct omap_dss_device *dssdev)
47 {
48 	struct panel_drv_data *ddata = to_panel_data(dssdev);
49 	struct omap_dss_device *in = ddata->in;
50 	int r;
51 
52 	dev_dbg(ddata->dev, "connect\n");
53 
54 	if (omapdss_device_is_connected(dssdev))
55 		return 0;
56 
57 	r = in->ops.atv->connect(in, dssdev);
58 	if (r)
59 		return r;
60 
61 	return 0;
62 }
63 
64 static void tvc_disconnect(struct omap_dss_device *dssdev)
65 {
66 	struct panel_drv_data *ddata = to_panel_data(dssdev);
67 	struct omap_dss_device *in = ddata->in;
68 
69 	dev_dbg(ddata->dev, "disconnect\n");
70 
71 	if (!omapdss_device_is_connected(dssdev))
72 		return;
73 
74 	in->ops.atv->disconnect(in, dssdev);
75 }
76 
77 static int tvc_enable(struct omap_dss_device *dssdev)
78 {
79 	struct panel_drv_data *ddata = to_panel_data(dssdev);
80 	struct omap_dss_device *in = ddata->in;
81 	int r;
82 
83 	dev_dbg(ddata->dev, "enable\n");
84 
85 	if (!omapdss_device_is_connected(dssdev))
86 		return -ENODEV;
87 
88 	if (omapdss_device_is_enabled(dssdev))
89 		return 0;
90 
91 	in->ops.atv->set_timings(in, &ddata->timings);
92 
93 	if (!ddata->dev->of_node) {
94 		in->ops.atv->set_type(in, OMAP_DSS_VENC_TYPE_COMPOSITE);
95 
96 		in->ops.atv->invert_vid_out_polarity(in,
97 			ddata->invert_polarity);
98 	}
99 
100 	r = in->ops.atv->enable(in);
101 	if (r)
102 		return r;
103 
104 	dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
105 
106 	return r;
107 }
108 
109 static void tvc_disable(struct omap_dss_device *dssdev)
110 {
111 	struct panel_drv_data *ddata = to_panel_data(dssdev);
112 	struct omap_dss_device *in = ddata->in;
113 
114 	dev_dbg(ddata->dev, "disable\n");
115 
116 	if (!omapdss_device_is_enabled(dssdev))
117 		return;
118 
119 	in->ops.atv->disable(in);
120 
121 	dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
122 }
123 
124 static void tvc_set_timings(struct omap_dss_device *dssdev,
125 		struct omap_video_timings *timings)
126 {
127 	struct panel_drv_data *ddata = to_panel_data(dssdev);
128 	struct omap_dss_device *in = ddata->in;
129 
130 	ddata->timings = *timings;
131 	dssdev->panel.timings = *timings;
132 
133 	in->ops.atv->set_timings(in, timings);
134 }
135 
136 static void tvc_get_timings(struct omap_dss_device *dssdev,
137 		struct omap_video_timings *timings)
138 {
139 	struct panel_drv_data *ddata = to_panel_data(dssdev);
140 
141 	*timings = ddata->timings;
142 }
143 
144 static int tvc_check_timings(struct omap_dss_device *dssdev,
145 		struct omap_video_timings *timings)
146 {
147 	struct panel_drv_data *ddata = to_panel_data(dssdev);
148 	struct omap_dss_device *in = ddata->in;
149 
150 	return in->ops.atv->check_timings(in, timings);
151 }
152 
153 static u32 tvc_get_wss(struct omap_dss_device *dssdev)
154 {
155 	struct panel_drv_data *ddata = to_panel_data(dssdev);
156 	struct omap_dss_device *in = ddata->in;
157 
158 	return in->ops.atv->get_wss(in);
159 }
160 
161 static int tvc_set_wss(struct omap_dss_device *dssdev, u32 wss)
162 {
163 	struct panel_drv_data *ddata = to_panel_data(dssdev);
164 	struct omap_dss_device *in = ddata->in;
165 
166 	return in->ops.atv->set_wss(in, wss);
167 }
168 
169 static struct omap_dss_driver tvc_driver = {
170 	.connect		= tvc_connect,
171 	.disconnect		= tvc_disconnect,
172 
173 	.enable			= tvc_enable,
174 	.disable		= tvc_disable,
175 
176 	.set_timings		= tvc_set_timings,
177 	.get_timings		= tvc_get_timings,
178 	.check_timings		= tvc_check_timings,
179 
180 	.get_resolution		= omapdss_default_get_resolution,
181 
182 	.get_wss		= tvc_get_wss,
183 	.set_wss		= tvc_set_wss,
184 };
185 
186 static int tvc_probe_pdata(struct platform_device *pdev)
187 {
188 	struct panel_drv_data *ddata = platform_get_drvdata(pdev);
189 	struct connector_atv_platform_data *pdata;
190 	struct omap_dss_device *in, *dssdev;
191 
192 	pdata = dev_get_platdata(&pdev->dev);
193 
194 	in = omap_dss_find_output(pdata->source);
195 	if (in == NULL) {
196 		dev_err(&pdev->dev, "Failed to find video source\n");
197 		return -EPROBE_DEFER;
198 	}
199 
200 	ddata->in = in;
201 
202 	ddata->invert_polarity = pdata->invert_polarity;
203 
204 	dssdev = &ddata->dssdev;
205 	dssdev->name = pdata->name;
206 
207 	return 0;
208 }
209 
210 static int tvc_probe_of(struct platform_device *pdev)
211 {
212 	struct panel_drv_data *ddata = platform_get_drvdata(pdev);
213 	struct device_node *node = pdev->dev.of_node;
214 	struct omap_dss_device *in;
215 
216 	in = omapdss_of_find_source_for_first_ep(node);
217 	if (IS_ERR(in)) {
218 		dev_err(&pdev->dev, "failed to find video source\n");
219 		return PTR_ERR(in);
220 	}
221 
222 	ddata->in = in;
223 
224 	return 0;
225 }
226 
227 static int tvc_probe(struct platform_device *pdev)
228 {
229 	struct panel_drv_data *ddata;
230 	struct omap_dss_device *dssdev;
231 	int r;
232 
233 	ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
234 	if (!ddata)
235 		return -ENOMEM;
236 
237 	platform_set_drvdata(pdev, ddata);
238 	ddata->dev = &pdev->dev;
239 
240 	if (dev_get_platdata(&pdev->dev)) {
241 		r = tvc_probe_pdata(pdev);
242 		if (r)
243 			return r;
244 	} else if (pdev->dev.of_node) {
245 		r = tvc_probe_of(pdev);
246 		if (r)
247 			return r;
248 	} else {
249 		return -ENODEV;
250 	}
251 
252 	ddata->timings = tvc_pal_timings;
253 
254 	dssdev = &ddata->dssdev;
255 	dssdev->driver = &tvc_driver;
256 	dssdev->dev = &pdev->dev;
257 	dssdev->type = OMAP_DISPLAY_TYPE_VENC;
258 	dssdev->owner = THIS_MODULE;
259 	dssdev->panel.timings = tvc_pal_timings;
260 
261 	r = omapdss_register_display(dssdev);
262 	if (r) {
263 		dev_err(&pdev->dev, "Failed to register panel\n");
264 		goto err_reg;
265 	}
266 
267 	return 0;
268 err_reg:
269 	omap_dss_put_device(ddata->in);
270 	return r;
271 }
272 
273 static int __exit tvc_remove(struct platform_device *pdev)
274 {
275 	struct panel_drv_data *ddata = platform_get_drvdata(pdev);
276 	struct omap_dss_device *dssdev = &ddata->dssdev;
277 	struct omap_dss_device *in = ddata->in;
278 
279 	omapdss_unregister_display(&ddata->dssdev);
280 
281 	tvc_disable(dssdev);
282 	tvc_disconnect(dssdev);
283 
284 	omap_dss_put_device(in);
285 
286 	return 0;
287 }
288 
289 static const struct of_device_id tvc_of_match[] = {
290 	{ .compatible = "omapdss,svideo-connector", },
291 	{ .compatible = "omapdss,composite-video-connector", },
292 	{},
293 };
294 
295 MODULE_DEVICE_TABLE(of, tvc_of_match);
296 
297 static struct platform_driver tvc_connector_driver = {
298 	.probe	= tvc_probe,
299 	.remove	= __exit_p(tvc_remove),
300 	.driver	= {
301 		.name	= "connector-analog-tv",
302 		.of_match_table = tvc_of_match,
303 		.suppress_bind_attrs = true,
304 	},
305 };
306 
307 module_platform_driver(tvc_connector_driver);
308 
309 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
310 MODULE_DESCRIPTION("Analog TV Connector driver");
311 MODULE_LICENSE("GPL");
312