blob: d9d25df6fc1b5311cb1a97c6517fb3587f8bf708 [file] [log] [blame]
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +03001/*
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
Arnd Bergmannd0196c82016-05-11 18:05:39 +020012#include <linux/gpio/consumer.h>
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +030013#include <linux/slab.h>
14#include <linux/module.h>
15#include <linux/platform_device.h>
Tomi Valkeinen0b8879f2013-07-30 10:37:34 +030016#include <linux/of.h>
Tomi Valkeinen2c75e3c2014-04-17 10:18:58 +030017#include <linux/of_gpio.h>
Peter Ujfalusic9741b42017-06-02 15:26:36 +030018#include <linux/mutex.h>
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +030019
20#include <drm/drm_edid.h>
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +030021
Peter Ujfalusi32043da2016-05-27 14:40:49 +030022#include "../dss/omapdss.h"
23
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +030024static const struct videomode hdmic_default_vm = {
Peter Ujfalusi81899062016-09-22 14:06:46 +030025 .hactive = 640,
Peter Ujfalusifb7f3c42016-09-22 14:06:47 +030026 .vactive = 480,
Tomi Valkeinend8d789412013-04-10 14:12:14 +030027 .pixelclock = 25175000,
Peter Ujfalusi4dc22502016-09-22 14:06:48 +030028 .hsync_len = 96,
Peter Ujfalusi0a30e152016-09-22 14:06:49 +030029 .hfront_porch = 16,
Peter Ujfalusia85f4a82016-09-22 14:06:50 +030030 .hback_porch = 48,
Peter Ujfalusid5bcf0a2016-09-22 14:06:51 +030031 .vsync_len = 2,
Peter Ujfalusi0996c682016-09-22 14:06:52 +030032 .vfront_porch = 11,
Peter Ujfalusi458540c2016-09-22 14:06:53 +030033 .vback_porch = 31,
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +030034
Peter Ujfalusi6b44cd22016-09-22 14:06:57 +030035 .flags = DISPLAY_FLAGS_HSYNC_LOW | DISPLAY_FLAGS_VSYNC_LOW,
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +030036};
37
38struct panel_drv_data {
39 struct omap_dss_device dssdev;
40 struct omap_dss_device *in;
Peter Ujfalusic9741b42017-06-02 15:26:36 +030041 void (*hpd_cb)(void *cb_data, enum drm_connector_status status);
42 void *hpd_cb_data;
43 bool hpd_enabled;
44 struct mutex hpd_lock;
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +030045
46 struct device *dev;
47
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +030048 struct videomode vm;
Tomi Valkeinen2c75e3c2014-04-17 10:18:58 +030049
50 int hpd_gpio;
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +030051};
52
53#define to_panel_data(x) container_of(x, struct panel_drv_data, dssdev)
54
55static int hdmic_connect(struct omap_dss_device *dssdev)
56{
57 struct panel_drv_data *ddata = to_panel_data(dssdev);
58 struct omap_dss_device *in = ddata->in;
59 int r;
60
61 dev_dbg(ddata->dev, "connect\n");
62
63 if (omapdss_device_is_connected(dssdev))
64 return 0;
65
66 r = in->ops.hdmi->connect(in, dssdev);
67 if (r)
68 return r;
69
70 return 0;
71}
72
73static void hdmic_disconnect(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
78 dev_dbg(ddata->dev, "disconnect\n");
79
80 if (!omapdss_device_is_connected(dssdev))
81 return;
82
83 in->ops.hdmi->disconnect(in, dssdev);
84}
85
86static int hdmic_enable(struct omap_dss_device *dssdev)
87{
88 struct panel_drv_data *ddata = to_panel_data(dssdev);
89 struct omap_dss_device *in = ddata->in;
90 int r;
91
92 dev_dbg(ddata->dev, "enable\n");
93
94 if (!omapdss_device_is_connected(dssdev))
95 return -ENODEV;
96
97 if (omapdss_device_is_enabled(dssdev))
98 return 0;
99
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300100 in->ops.hdmi->set_timings(in, &ddata->vm);
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +0300101
102 r = in->ops.hdmi->enable(in);
103 if (r)
104 return r;
105
106 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
107
108 return r;
109}
110
111static void hdmic_disable(struct omap_dss_device *dssdev)
112{
113 struct panel_drv_data *ddata = to_panel_data(dssdev);
114 struct omap_dss_device *in = ddata->in;
115
116 dev_dbg(ddata->dev, "disable\n");
117
118 if (!omapdss_device_is_enabled(dssdev))
119 return;
120
121 in->ops.hdmi->disable(in);
122
123 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
124}
125
126static void hdmic_set_timings(struct omap_dss_device *dssdev,
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300127 struct videomode *vm)
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +0300128{
129 struct panel_drv_data *ddata = to_panel_data(dssdev);
130 struct omap_dss_device *in = ddata->in;
131
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300132 ddata->vm = *vm;
133 dssdev->panel.vm = *vm;
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +0300134
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300135 in->ops.hdmi->set_timings(in, vm);
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +0300136}
137
138static void hdmic_get_timings(struct omap_dss_device *dssdev,
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300139 struct videomode *vm)
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +0300140{
141 struct panel_drv_data *ddata = to_panel_data(dssdev);
142
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300143 *vm = ddata->vm;
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +0300144}
145
146static int hdmic_check_timings(struct omap_dss_device *dssdev,
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300147 struct videomode *vm)
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +0300148{
149 struct panel_drv_data *ddata = to_panel_data(dssdev);
150 struct omap_dss_device *in = ddata->in;
151
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300152 return in->ops.hdmi->check_timings(in, vm);
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +0300153}
154
155static int hdmic_read_edid(struct omap_dss_device *dssdev,
156 u8 *edid, int len)
157{
158 struct panel_drv_data *ddata = to_panel_data(dssdev);
159 struct omap_dss_device *in = ddata->in;
160
161 return in->ops.hdmi->read_edid(in, edid, len);
162}
163
164static bool hdmic_detect(struct omap_dss_device *dssdev)
165{
166 struct panel_drv_data *ddata = to_panel_data(dssdev);
167 struct omap_dss_device *in = ddata->in;
168
Tomi Valkeinen2c75e3c2014-04-17 10:18:58 +0300169 if (gpio_is_valid(ddata->hpd_gpio))
170 return gpio_get_value_cansleep(ddata->hpd_gpio);
171 else
172 return in->ops.hdmi->detect(in);
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +0300173}
174
Peter Ujfalusic9741b42017-06-02 15:26:36 +0300175static int hdmic_register_hpd_cb(struct omap_dss_device *dssdev,
176 void (*cb)(void *cb_data,
177 enum drm_connector_status status),
178 void *cb_data)
179{
180 struct panel_drv_data *ddata = to_panel_data(dssdev);
181 struct omap_dss_device *in = ddata->in;
182
183 if (gpio_is_valid(ddata->hpd_gpio)) {
184 mutex_lock(&ddata->hpd_lock);
185 ddata->hpd_cb = cb;
186 ddata->hpd_cb_data = cb_data;
187 mutex_unlock(&ddata->hpd_lock);
188 return 0;
189 } else if (in->ops.hdmi->register_hpd_cb) {
190 return in->ops.hdmi->register_hpd_cb(in, cb, cb_data);
191 }
192
193 return -ENOTSUPP;
194}
195
196static void hdmic_unregister_hpd_cb(struct omap_dss_device *dssdev)
197{
198 struct panel_drv_data *ddata = to_panel_data(dssdev);
199 struct omap_dss_device *in = ddata->in;
200
201 if (gpio_is_valid(ddata->hpd_gpio)) {
202 mutex_lock(&ddata->hpd_lock);
203 ddata->hpd_cb = NULL;
204 ddata->hpd_cb_data = NULL;
205 mutex_unlock(&ddata->hpd_lock);
206 } else if (in->ops.hdmi->unregister_hpd_cb) {
207 in->ops.hdmi->unregister_hpd_cb(in);
208 }
209}
210
211static void hdmic_enable_hpd(struct omap_dss_device *dssdev)
212{
213 struct panel_drv_data *ddata = to_panel_data(dssdev);
214 struct omap_dss_device *in = ddata->in;
215
216 if (gpio_is_valid(ddata->hpd_gpio)) {
217 mutex_lock(&ddata->hpd_lock);
218 ddata->hpd_enabled = true;
219 mutex_unlock(&ddata->hpd_lock);
220 } else if (in->ops.hdmi->enable_hpd) {
221 in->ops.hdmi->enable_hpd(in);
222 }
223}
224
225static void hdmic_disable_hpd(struct omap_dss_device *dssdev)
226{
227 struct panel_drv_data *ddata = to_panel_data(dssdev);
228 struct omap_dss_device *in = ddata->in;
229
230 if (gpio_is_valid(ddata->hpd_gpio)) {
231 mutex_lock(&ddata->hpd_lock);
232 ddata->hpd_enabled = false;
233 mutex_unlock(&ddata->hpd_lock);
234 } else if (in->ops.hdmi->disable_hpd) {
235 in->ops.hdmi->disable_hpd(in);
236 }
237}
238
Tomi Valkeinen9fb17372014-06-18 12:58:02 +0300239static int hdmic_set_hdmi_mode(struct omap_dss_device *dssdev, bool hdmi_mode)
240{
241 struct panel_drv_data *ddata = to_panel_data(dssdev);
242 struct omap_dss_device *in = ddata->in;
243
244 return in->ops.hdmi->set_hdmi_mode(in, hdmi_mode);
245}
246
247static int hdmic_set_infoframe(struct omap_dss_device *dssdev,
248 const struct hdmi_avi_infoframe *avi)
249{
250 struct panel_drv_data *ddata = to_panel_data(dssdev);
251 struct omap_dss_device *in = ddata->in;
252
253 return in->ops.hdmi->set_infoframe(in, avi);
254}
255
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +0300256static struct omap_dss_driver hdmic_driver = {
257 .connect = hdmic_connect,
258 .disconnect = hdmic_disconnect,
259
260 .enable = hdmic_enable,
261 .disable = hdmic_disable,
262
263 .set_timings = hdmic_set_timings,
264 .get_timings = hdmic_get_timings,
265 .check_timings = hdmic_check_timings,
266
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +0300267 .read_edid = hdmic_read_edid,
268 .detect = hdmic_detect,
Peter Ujfalusic9741b42017-06-02 15:26:36 +0300269 .register_hpd_cb = hdmic_register_hpd_cb,
270 .unregister_hpd_cb = hdmic_unregister_hpd_cb,
271 .enable_hpd = hdmic_enable_hpd,
272 .disable_hpd = hdmic_disable_hpd,
Tomi Valkeinen9fb17372014-06-18 12:58:02 +0300273 .set_hdmi_mode = hdmic_set_hdmi_mode,
274 .set_hdmi_infoframe = hdmic_set_infoframe,
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +0300275};
276
Peter Ujfalusic9741b42017-06-02 15:26:36 +0300277static irqreturn_t hdmic_hpd_isr(int irq, void *data)
278{
279 struct panel_drv_data *ddata = data;
280
281 mutex_lock(&ddata->hpd_lock);
282 if (ddata->hpd_enabled && ddata->hpd_cb) {
283 enum drm_connector_status status;
284
285 if (hdmic_detect(&ddata->dssdev))
286 status = connector_status_connected;
287 else
288 status = connector_status_disconnected;
289
290 ddata->hpd_cb(ddata->hpd_cb_data, status);
291 }
292 mutex_unlock(&ddata->hpd_lock);
293
294 return IRQ_HANDLED;
295}
296
Tomi Valkeinen0b8879f2013-07-30 10:37:34 +0300297static int hdmic_probe_of(struct platform_device *pdev)
298{
299 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
300 struct device_node *node = pdev->dev.of_node;
301 struct omap_dss_device *in;
Tomi Valkeinen2c75e3c2014-04-17 10:18:58 +0300302 int gpio;
303
304 /* HPD GPIO */
305 gpio = of_get_named_gpio(node, "hpd-gpios", 0);
306 if (gpio_is_valid(gpio))
307 ddata->hpd_gpio = gpio;
308 else
309 ddata->hpd_gpio = -ENODEV;
Tomi Valkeinen0b8879f2013-07-30 10:37:34 +0300310
311 in = omapdss_of_find_source_for_first_ep(node);
312 if (IS_ERR(in)) {
313 dev_err(&pdev->dev, "failed to find video source\n");
314 return PTR_ERR(in);
315 }
316
317 ddata->in = in;
318
319 return 0;
320}
321
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +0300322static int hdmic_probe(struct platform_device *pdev)
323{
324 struct panel_drv_data *ddata;
325 struct omap_dss_device *dssdev;
326 int r;
327
328 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
329 if (!ddata)
330 return -ENOMEM;
331
332 platform_set_drvdata(pdev, ddata);
333 ddata->dev = &pdev->dev;
334
Tomi Valkeinen7f7642b2016-02-18 17:20:54 +0200335 if (!pdev->dev.of_node)
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +0300336 return -ENODEV;
Tomi Valkeinen7f7642b2016-02-18 17:20:54 +0200337
338 r = hdmic_probe_of(pdev);
339 if (r)
340 return r;
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +0300341
Peter Ujfalusic9741b42017-06-02 15:26:36 +0300342 mutex_init(&ddata->hpd_lock);
343
Tomi Valkeinen2c75e3c2014-04-17 10:18:58 +0300344 if (gpio_is_valid(ddata->hpd_gpio)) {
345 r = devm_gpio_request_one(&pdev->dev, ddata->hpd_gpio,
346 GPIOF_DIR_IN, "hdmi_hpd");
347 if (r)
348 goto err_reg;
Peter Ujfalusic9741b42017-06-02 15:26:36 +0300349
350 r = devm_request_threaded_irq(&pdev->dev,
351 gpio_to_irq(ddata->hpd_gpio),
352 NULL, hdmic_hpd_isr,
353 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
354 IRQF_ONESHOT,
355 "hdmic hpd", ddata);
356 if (r)
357 goto err_reg;
Tomi Valkeinen2c75e3c2014-04-17 10:18:58 +0300358 }
359
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300360 ddata->vm = hdmic_default_vm;
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +0300361
362 dssdev = &ddata->dssdev;
363 dssdev->driver = &hdmic_driver;
364 dssdev->dev = &pdev->dev;
365 dssdev->type = OMAP_DISPLAY_TYPE_HDMI;
366 dssdev->owner = THIS_MODULE;
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300367 dssdev->panel.vm = hdmic_default_vm;
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +0300368
369 r = omapdss_register_display(dssdev);
370 if (r) {
371 dev_err(&pdev->dev, "Failed to register panel\n");
372 goto err_reg;
373 }
374
375 return 0;
376err_reg:
377 omap_dss_put_device(ddata->in);
378 return r;
379}
380
381static int __exit hdmic_remove(struct platform_device *pdev)
382{
383 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
384 struct omap_dss_device *dssdev = &ddata->dssdev;
385 struct omap_dss_device *in = ddata->in;
386
387 omapdss_unregister_display(&ddata->dssdev);
388
389 hdmic_disable(dssdev);
390 hdmic_disconnect(dssdev);
391
392 omap_dss_put_device(in);
393
394 return 0;
395}
396
Tomi Valkeinen0b8879f2013-07-30 10:37:34 +0300397static const struct of_device_id hdmic_of_match[] = {
398 { .compatible = "omapdss,hdmi-connector", },
399 {},
400};
401
402MODULE_DEVICE_TABLE(of, hdmic_of_match);
403
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +0300404static struct platform_driver hdmi_connector_driver = {
405 .probe = hdmic_probe,
406 .remove = __exit_p(hdmic_remove),
407 .driver = {
408 .name = "connector-hdmi",
Tomi Valkeinen0b8879f2013-07-30 10:37:34 +0300409 .of_match_table = hdmic_of_match,
Tomi Valkeinen422ccbd2014-10-16 09:54:25 +0300410 .suppress_bind_attrs = true,
Tomi Valkeinen3cb07ee2013-05-24 14:21:08 +0300411 },
412};
413
414module_platform_driver(hdmi_connector_driver);
415
416MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
417MODULE_DESCRIPTION("HDMI Connector driver");
418MODULE_LICENSE("GPL");