2 * HDMI Connector driver
4 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/
5 * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
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.
12 #include <linux/gpio/consumer.h>
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
17 #include <linux/of_gpio.h>
18 #include <linux/mutex.h>
20 #include <drm/drm_edid.h>
22 #include "../dss/omapdss.h"
24 static const struct videomode hdmic_default_vm = {
27 .pixelclock = 25175000,
35 .flags = DISPLAY_FLAGS_HSYNC_LOW | DISPLAY_FLAGS_VSYNC_LOW,
38 struct panel_drv_data {
39 struct omap_dss_device dssdev;
40 struct omap_dss_device *in;
41 void (*hpd_cb)(void *cb_data, enum drm_connector_status status);
44 struct mutex hpd_lock;
53 #define to_panel_data(x) container_of(x, struct panel_drv_data, dssdev)
55 static int hdmic_connect(struct omap_dss_device *dssdev)
57 struct panel_drv_data *ddata = to_panel_data(dssdev);
58 struct omap_dss_device *in = ddata->in;
61 dev_dbg(ddata->dev, "connect\n");
63 if (omapdss_device_is_connected(dssdev))
66 r = in->ops.hdmi->connect(in, dssdev);
73 static void hdmic_disconnect(struct omap_dss_device *dssdev)
75 struct panel_drv_data *ddata = to_panel_data(dssdev);
76 struct omap_dss_device *in = ddata->in;
78 dev_dbg(ddata->dev, "disconnect\n");
80 if (!omapdss_device_is_connected(dssdev))
83 in->ops.hdmi->disconnect(in, dssdev);
86 static int hdmic_enable(struct omap_dss_device *dssdev)
88 struct panel_drv_data *ddata = to_panel_data(dssdev);
89 struct omap_dss_device *in = ddata->in;
92 dev_dbg(ddata->dev, "enable\n");
94 if (!omapdss_device_is_connected(dssdev))
97 if (omapdss_device_is_enabled(dssdev))
100 in->ops.hdmi->set_timings(in, &ddata->vm);
102 r = in->ops.hdmi->enable(in);
106 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
111 static void hdmic_disable(struct omap_dss_device *dssdev)
113 struct panel_drv_data *ddata = to_panel_data(dssdev);
114 struct omap_dss_device *in = ddata->in;
116 dev_dbg(ddata->dev, "disable\n");
118 if (!omapdss_device_is_enabled(dssdev))
121 in->ops.hdmi->disable(in);
123 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
126 static void hdmic_set_timings(struct omap_dss_device *dssdev,
127 struct videomode *vm)
129 struct panel_drv_data *ddata = to_panel_data(dssdev);
130 struct omap_dss_device *in = ddata->in;
133 dssdev->panel.vm = *vm;
135 in->ops.hdmi->set_timings(in, vm);
138 static void hdmic_get_timings(struct omap_dss_device *dssdev,
139 struct videomode *vm)
141 struct panel_drv_data *ddata = to_panel_data(dssdev);
146 static int hdmic_check_timings(struct omap_dss_device *dssdev,
147 struct videomode *vm)
149 struct panel_drv_data *ddata = to_panel_data(dssdev);
150 struct omap_dss_device *in = ddata->in;
152 return in->ops.hdmi->check_timings(in, vm);
155 static int hdmic_read_edid(struct omap_dss_device *dssdev,
158 struct panel_drv_data *ddata = to_panel_data(dssdev);
159 struct omap_dss_device *in = ddata->in;
161 return in->ops.hdmi->read_edid(in, edid, len);
164 static bool hdmic_detect(struct omap_dss_device *dssdev)
166 struct panel_drv_data *ddata = to_panel_data(dssdev);
167 struct omap_dss_device *in = ddata->in;
170 if (gpio_is_valid(ddata->hpd_gpio))
171 connected = gpio_get_value_cansleep(ddata->hpd_gpio);
173 connected = in->ops.hdmi->detect(in);
174 if (!connected && in->ops.hdmi->lost_hotplug)
175 in->ops.hdmi->lost_hotplug(in);
179 static int hdmic_register_hpd_cb(struct omap_dss_device *dssdev,
180 void (*cb)(void *cb_data,
181 enum drm_connector_status status),
184 struct panel_drv_data *ddata = to_panel_data(dssdev);
185 struct omap_dss_device *in = ddata->in;
187 if (gpio_is_valid(ddata->hpd_gpio)) {
188 mutex_lock(&ddata->hpd_lock);
190 ddata->hpd_cb_data = cb_data;
191 mutex_unlock(&ddata->hpd_lock);
193 } else if (in->ops.hdmi->register_hpd_cb) {
194 return in->ops.hdmi->register_hpd_cb(in, cb, cb_data);
200 static void hdmic_unregister_hpd_cb(struct omap_dss_device *dssdev)
202 struct panel_drv_data *ddata = to_panel_data(dssdev);
203 struct omap_dss_device *in = ddata->in;
205 if (gpio_is_valid(ddata->hpd_gpio)) {
206 mutex_lock(&ddata->hpd_lock);
207 ddata->hpd_cb = NULL;
208 ddata->hpd_cb_data = NULL;
209 mutex_unlock(&ddata->hpd_lock);
210 } else if (in->ops.hdmi->unregister_hpd_cb) {
211 in->ops.hdmi->unregister_hpd_cb(in);
215 static void hdmic_enable_hpd(struct omap_dss_device *dssdev)
217 struct panel_drv_data *ddata = to_panel_data(dssdev);
218 struct omap_dss_device *in = ddata->in;
220 if (gpio_is_valid(ddata->hpd_gpio)) {
221 mutex_lock(&ddata->hpd_lock);
222 ddata->hpd_enabled = true;
223 mutex_unlock(&ddata->hpd_lock);
224 } else if (in->ops.hdmi->enable_hpd) {
225 in->ops.hdmi->enable_hpd(in);
229 static void hdmic_disable_hpd(struct omap_dss_device *dssdev)
231 struct panel_drv_data *ddata = to_panel_data(dssdev);
232 struct omap_dss_device *in = ddata->in;
234 if (gpio_is_valid(ddata->hpd_gpio)) {
235 mutex_lock(&ddata->hpd_lock);
236 ddata->hpd_enabled = false;
237 mutex_unlock(&ddata->hpd_lock);
238 } else if (in->ops.hdmi->disable_hpd) {
239 in->ops.hdmi->disable_hpd(in);
243 static int hdmic_set_hdmi_mode(struct omap_dss_device *dssdev, bool hdmi_mode)
245 struct panel_drv_data *ddata = to_panel_data(dssdev);
246 struct omap_dss_device *in = ddata->in;
248 return in->ops.hdmi->set_hdmi_mode(in, hdmi_mode);
251 static int hdmic_set_infoframe(struct omap_dss_device *dssdev,
252 const struct hdmi_avi_infoframe *avi)
254 struct panel_drv_data *ddata = to_panel_data(dssdev);
255 struct omap_dss_device *in = ddata->in;
257 return in->ops.hdmi->set_infoframe(in, avi);
260 static struct omap_dss_driver hdmic_driver = {
261 .connect = hdmic_connect,
262 .disconnect = hdmic_disconnect,
264 .enable = hdmic_enable,
265 .disable = hdmic_disable,
267 .set_timings = hdmic_set_timings,
268 .get_timings = hdmic_get_timings,
269 .check_timings = hdmic_check_timings,
271 .read_edid = hdmic_read_edid,
272 .detect = hdmic_detect,
273 .register_hpd_cb = hdmic_register_hpd_cb,
274 .unregister_hpd_cb = hdmic_unregister_hpd_cb,
275 .enable_hpd = hdmic_enable_hpd,
276 .disable_hpd = hdmic_disable_hpd,
277 .set_hdmi_mode = hdmic_set_hdmi_mode,
278 .set_hdmi_infoframe = hdmic_set_infoframe,
281 static irqreturn_t hdmic_hpd_isr(int irq, void *data)
283 struct panel_drv_data *ddata = data;
285 mutex_lock(&ddata->hpd_lock);
286 if (ddata->hpd_enabled && ddata->hpd_cb) {
287 enum drm_connector_status status;
289 if (hdmic_detect(&ddata->dssdev))
290 status = connector_status_connected;
292 status = connector_status_disconnected;
294 ddata->hpd_cb(ddata->hpd_cb_data, status);
296 mutex_unlock(&ddata->hpd_lock);
301 static int hdmic_probe_of(struct platform_device *pdev)
303 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
304 struct device_node *node = pdev->dev.of_node;
305 struct omap_dss_device *in;
309 gpio = of_get_named_gpio(node, "hpd-gpios", 0);
310 if (gpio_is_valid(gpio))
311 ddata->hpd_gpio = gpio;
313 ddata->hpd_gpio = -ENODEV;
315 in = omapdss_of_find_source_for_first_ep(node);
317 dev_err(&pdev->dev, "failed to find video source\n");
326 static int hdmic_probe(struct platform_device *pdev)
328 struct panel_drv_data *ddata;
329 struct omap_dss_device *dssdev;
332 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
336 platform_set_drvdata(pdev, ddata);
337 ddata->dev = &pdev->dev;
339 if (!pdev->dev.of_node)
342 r = hdmic_probe_of(pdev);
346 mutex_init(&ddata->hpd_lock);
348 if (gpio_is_valid(ddata->hpd_gpio)) {
349 r = devm_gpio_request_one(&pdev->dev, ddata->hpd_gpio,
350 GPIOF_DIR_IN, "hdmi_hpd");
354 r = devm_request_threaded_irq(&pdev->dev,
355 gpio_to_irq(ddata->hpd_gpio),
357 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
364 ddata->vm = hdmic_default_vm;
366 dssdev = &ddata->dssdev;
367 dssdev->driver = &hdmic_driver;
368 dssdev->dev = &pdev->dev;
369 dssdev->type = OMAP_DISPLAY_TYPE_HDMI;
370 dssdev->owner = THIS_MODULE;
371 dssdev->panel.vm = hdmic_default_vm;
373 r = omapdss_register_display(dssdev);
375 dev_err(&pdev->dev, "Failed to register panel\n");
381 omap_dss_put_device(ddata->in);
385 static int __exit hdmic_remove(struct platform_device *pdev)
387 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
388 struct omap_dss_device *dssdev = &ddata->dssdev;
389 struct omap_dss_device *in = ddata->in;
391 omapdss_unregister_display(&ddata->dssdev);
393 hdmic_disable(dssdev);
394 hdmic_disconnect(dssdev);
396 omap_dss_put_device(in);
401 static const struct of_device_id hdmic_of_match[] = {
402 { .compatible = "omapdss,hdmi-connector", },
406 MODULE_DEVICE_TABLE(of, hdmic_of_match);
408 static struct platform_driver hdmi_connector_driver = {
409 .probe = hdmic_probe,
410 .remove = __exit_p(hdmic_remove),
412 .name = "connector-hdmi",
413 .of_match_table = hdmic_of_match,
414 .suppress_bind_attrs = true,
418 module_platform_driver(hdmi_connector_driver);
420 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
421 MODULE_DESCRIPTION("HDMI Connector driver");
422 MODULE_LICENSE("GPL");