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