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