]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/omapdrm/displays/connector-dvi.c
fa3a69bf8a04dff0cbce53c1beee80b40d5478bd
[linux.git] / drivers / gpu / drm / omapdrm / displays / connector-dvi.c
1 /*
2  * Generic DVI Connector driver
3  *
4  * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/
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/gpio/consumer.h>
13 #include <linux/i2c.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
17
18 #include <drm/drm_edid.h>
19
20 #include "../dss/omapdss.h"
21
22 struct panel_drv_data {
23         struct omap_dss_device dssdev;
24
25         struct i2c_adapter *i2c_adapter;
26
27         struct gpio_desc *hpd_gpio;
28
29         void (*hpd_cb)(void *cb_data, enum drm_connector_status status);
30         void *hpd_cb_data;
31         bool hpd_enabled;
32         /* mutex for hpd fields above */
33         struct mutex hpd_lock;
34 };
35
36 #define to_panel_data(x) container_of(x, struct panel_drv_data, dssdev)
37
38 static int dvic_connect(struct omap_dss_device *src,
39                         struct omap_dss_device *dst)
40 {
41         return 0;
42 }
43
44 static void dvic_disconnect(struct omap_dss_device *src,
45                             struct omap_dss_device *dst)
46 {
47 }
48
49 static int dvic_ddc_read(struct i2c_adapter *adapter,
50                 unsigned char *buf, u16 count, u8 offset)
51 {
52         int r, retries;
53
54         for (retries = 3; retries > 0; retries--) {
55                 struct i2c_msg msgs[] = {
56                         {
57                                 .addr   = DDC_ADDR,
58                                 .flags  = 0,
59                                 .len    = 1,
60                                 .buf    = &offset,
61                         }, {
62                                 .addr   = DDC_ADDR,
63                                 .flags  = I2C_M_RD,
64                                 .len    = count,
65                                 .buf    = buf,
66                         }
67                 };
68
69                 r = i2c_transfer(adapter, msgs, 2);
70                 if (r == 2)
71                         return 0;
72
73                 if (r != -EAGAIN)
74                         break;
75         }
76
77         return r < 0 ? r : -EIO;
78 }
79
80 static int dvic_read_edid(struct omap_dss_device *dssdev,
81                 u8 *edid, int len)
82 {
83         struct panel_drv_data *ddata = to_panel_data(dssdev);
84         int r, l, bytes_read;
85
86         l = min(EDID_LENGTH, len);
87         r = dvic_ddc_read(ddata->i2c_adapter, edid, l, 0);
88         if (r)
89                 return r;
90
91         bytes_read = l;
92
93         /* if there are extensions, read second block */
94         if (len > EDID_LENGTH && edid[0x7e] > 0) {
95                 l = min(EDID_LENGTH, len - EDID_LENGTH);
96
97                 r = dvic_ddc_read(ddata->i2c_adapter, edid + EDID_LENGTH,
98                                 l, EDID_LENGTH);
99                 if (r)
100                         return r;
101
102                 bytes_read += l;
103         }
104
105         return bytes_read;
106 }
107
108 static bool dvic_detect(struct omap_dss_device *dssdev)
109 {
110         struct panel_drv_data *ddata = to_panel_data(dssdev);
111         unsigned char out;
112         int r;
113
114         if (ddata->hpd_gpio)
115                 return gpiod_get_value_cansleep(ddata->hpd_gpio);
116
117         if (!ddata->i2c_adapter)
118                 return true;
119
120         r = dvic_ddc_read(ddata->i2c_adapter, &out, 1, 0);
121
122         return r == 0;
123 }
124
125 static void dvic_register_hpd_cb(struct omap_dss_device *dssdev,
126                                  void (*cb)(void *cb_data,
127                                             enum drm_connector_status status),
128                                  void *cb_data)
129 {
130         struct panel_drv_data *ddata = to_panel_data(dssdev);
131
132         mutex_lock(&ddata->hpd_lock);
133         ddata->hpd_cb = cb;
134         ddata->hpd_cb_data = cb_data;
135         mutex_unlock(&ddata->hpd_lock);
136 }
137
138 static void dvic_unregister_hpd_cb(struct omap_dss_device *dssdev)
139 {
140         struct panel_drv_data *ddata = to_panel_data(dssdev);
141
142         mutex_lock(&ddata->hpd_lock);
143         ddata->hpd_cb = NULL;
144         ddata->hpd_cb_data = NULL;
145         mutex_unlock(&ddata->hpd_lock);
146 }
147
148 static const struct omap_dss_device_ops dvic_ops = {
149         .connect        = dvic_connect,
150         .disconnect     = dvic_disconnect,
151
152         .read_edid      = dvic_read_edid,
153         .detect         = dvic_detect,
154
155         .register_hpd_cb        = dvic_register_hpd_cb,
156         .unregister_hpd_cb      = dvic_unregister_hpd_cb,
157 };
158
159 static irqreturn_t dvic_hpd_isr(int irq, void *data)
160 {
161         struct panel_drv_data *ddata = data;
162
163         mutex_lock(&ddata->hpd_lock);
164         if (ddata->hpd_enabled && ddata->hpd_cb) {
165                 enum drm_connector_status status;
166
167                 if (dvic_detect(&ddata->dssdev))
168                         status = connector_status_connected;
169                 else
170                         status = connector_status_disconnected;
171
172                 ddata->hpd_cb(ddata->hpd_cb_data, status);
173         }
174         mutex_unlock(&ddata->hpd_lock);
175
176         return IRQ_HANDLED;
177 }
178
179 static int dvic_probe_of(struct platform_device *pdev)
180 {
181         struct panel_drv_data *ddata = platform_get_drvdata(pdev);
182         struct device_node *node = pdev->dev.of_node;
183         struct device_node *adapter_node;
184         struct i2c_adapter *adapter;
185         struct gpio_desc *gpio;
186         int r;
187
188         gpio = devm_gpiod_get_optional(&pdev->dev, "hpd", GPIOD_IN);
189         if (IS_ERR(gpio)) {
190                 dev_err(&pdev->dev, "failed to parse HPD gpio\n");
191                 return PTR_ERR(gpio);
192         }
193
194         ddata->hpd_gpio = gpio;
195
196         mutex_init(&ddata->hpd_lock);
197
198         if (ddata->hpd_gpio) {
199                 r = devm_request_threaded_irq(&pdev->dev,
200                         gpiod_to_irq(ddata->hpd_gpio), NULL, dvic_hpd_isr,
201                         IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
202                         "DVI HPD", ddata);
203                 if (r)
204                         return r;
205         }
206
207         adapter_node = of_parse_phandle(node, "ddc-i2c-bus", 0);
208         if (adapter_node) {
209                 adapter = of_get_i2c_adapter_by_node(adapter_node);
210                 of_node_put(adapter_node);
211                 if (adapter == NULL) {
212                         dev_err(&pdev->dev, "failed to parse ddc-i2c-bus\n");
213                         return -EPROBE_DEFER;
214                 }
215
216                 ddata->i2c_adapter = adapter;
217         }
218
219         return 0;
220 }
221
222 static int dvic_probe(struct platform_device *pdev)
223 {
224         struct panel_drv_data *ddata;
225         struct omap_dss_device *dssdev;
226         int r;
227
228         ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
229         if (!ddata)
230                 return -ENOMEM;
231
232         platform_set_drvdata(pdev, ddata);
233
234         r = dvic_probe_of(pdev);
235         if (r)
236                 return r;
237
238         dssdev = &ddata->dssdev;
239         dssdev->ops = &dvic_ops;
240         dssdev->dev = &pdev->dev;
241         dssdev->type = OMAP_DISPLAY_TYPE_DVI;
242         dssdev->display = true;
243         dssdev->owner = THIS_MODULE;
244         dssdev->of_ports = BIT(0);
245
246         if (ddata->hpd_gpio)
247                 dssdev->ops_flags |= OMAP_DSS_DEVICE_OP_DETECT
248                                   |  OMAP_DSS_DEVICE_OP_HPD;
249         if (ddata->i2c_adapter)
250                 dssdev->ops_flags |= OMAP_DSS_DEVICE_OP_DETECT
251                                   |  OMAP_DSS_DEVICE_OP_EDID;
252
253         omapdss_display_init(dssdev);
254         omapdss_device_register(dssdev);
255
256         return 0;
257 }
258
259 static int __exit dvic_remove(struct platform_device *pdev)
260 {
261         struct panel_drv_data *ddata = platform_get_drvdata(pdev);
262
263         omapdss_device_unregister(&ddata->dssdev);
264
265         i2c_put_adapter(ddata->i2c_adapter);
266
267         mutex_destroy(&ddata->hpd_lock);
268
269         return 0;
270 }
271
272 static const struct of_device_id dvic_of_match[] = {
273         { .compatible = "omapdss,dvi-connector", },
274         {},
275 };
276
277 MODULE_DEVICE_TABLE(of, dvic_of_match);
278
279 static struct platform_driver dvi_connector_driver = {
280         .probe  = dvic_probe,
281         .remove = __exit_p(dvic_remove),
282         .driver = {
283                 .name   = "connector-dvi",
284                 .of_match_table = dvic_of_match,
285                 .suppress_bind_attrs = true,
286         },
287 };
288
289 module_platform_driver(dvi_connector_driver);
290
291 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
292 MODULE_DESCRIPTION("Generic DVI Connector driver");
293 MODULE_LICENSE("GPL");