Merge tag 'nds32-for-linus-4.18' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.git] / drivers / gpu / drm / bridge / ti-tfp410.c
1 /*
2  * Copyright (C) 2016 Texas Instruments
3  * Author: Jyri Sarha <jsarha@ti.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  *
9  */
10
11 #include <linux/delay.h>
12 #include <linux/fwnode.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/irq.h>
15 #include <linux/module.h>
16 #include <linux/of_graph.h>
17 #include <linux/platform_device.h>
18 #include <linux/i2c.h>
19
20 #include <drm/drmP.h>
21 #include <drm/drm_atomic_helper.h>
22 #include <drm/drm_crtc.h>
23 #include <drm/drm_crtc_helper.h>
24
25 #define HOTPLUG_DEBOUNCE_MS             1100
26
27 struct tfp410 {
28         struct drm_bridge       bridge;
29         struct drm_connector    connector;
30
31         struct i2c_adapter      *ddc;
32         struct gpio_desc        *hpd;
33         struct delayed_work     hpd_work;
34
35         struct device *dev;
36 };
37
38 static inline struct tfp410 *
39 drm_bridge_to_tfp410(struct drm_bridge *bridge)
40 {
41         return container_of(bridge, struct tfp410, bridge);
42 }
43
44 static inline struct tfp410 *
45 drm_connector_to_tfp410(struct drm_connector *connector)
46 {
47         return container_of(connector, struct tfp410, connector);
48 }
49
50 static int tfp410_get_modes(struct drm_connector *connector)
51 {
52         struct tfp410 *dvi = drm_connector_to_tfp410(connector);
53         struct edid *edid;
54         int ret;
55
56         if (!dvi->ddc)
57                 goto fallback;
58
59         edid = drm_get_edid(connector, dvi->ddc);
60         if (!edid) {
61                 DRM_INFO("EDID read failed. Fallback to standard modes\n");
62                 goto fallback;
63         }
64
65         drm_mode_connector_update_edid_property(connector, edid);
66
67         return drm_add_edid_modes(connector, edid);
68 fallback:
69         /* No EDID, fallback on the XGA standard modes */
70         ret = drm_add_modes_noedid(connector, 1920, 1200);
71
72         /* And prefer a mode pretty much anything can handle */
73         drm_set_preferred_mode(connector, 1024, 768);
74
75         return ret;
76 }
77
78 static const struct drm_connector_helper_funcs tfp410_con_helper_funcs = {
79         .get_modes      = tfp410_get_modes,
80 };
81
82 static enum drm_connector_status
83 tfp410_connector_detect(struct drm_connector *connector, bool force)
84 {
85         struct tfp410 *dvi = drm_connector_to_tfp410(connector);
86
87         if (dvi->hpd) {
88                 if (gpiod_get_value_cansleep(dvi->hpd))
89                         return connector_status_connected;
90                 else
91                         return connector_status_disconnected;
92         }
93
94         if (dvi->ddc) {
95                 if (drm_probe_ddc(dvi->ddc))
96                         return connector_status_connected;
97                 else
98                         return connector_status_disconnected;
99         }
100
101         return connector_status_unknown;
102 }
103
104 static const struct drm_connector_funcs tfp410_con_funcs = {
105         .detect                 = tfp410_connector_detect,
106         .fill_modes             = drm_helper_probe_single_connector_modes,
107         .destroy                = drm_connector_cleanup,
108         .reset                  = drm_atomic_helper_connector_reset,
109         .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
110         .atomic_destroy_state   = drm_atomic_helper_connector_destroy_state,
111 };
112
113 static int tfp410_attach(struct drm_bridge *bridge)
114 {
115         struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
116         int ret;
117
118         if (!bridge->encoder) {
119                 dev_err(dvi->dev, "Missing encoder\n");
120                 return -ENODEV;
121         }
122
123         if (dvi->hpd)
124                 dvi->connector.polled = DRM_CONNECTOR_POLL_HPD;
125
126         drm_connector_helper_add(&dvi->connector,
127                                  &tfp410_con_helper_funcs);
128         ret = drm_connector_init(bridge->dev, &dvi->connector,
129                                  &tfp410_con_funcs, DRM_MODE_CONNECTOR_HDMIA);
130         if (ret) {
131                 dev_err(dvi->dev, "drm_connector_init() failed: %d\n", ret);
132                 return ret;
133         }
134
135         drm_mode_connector_attach_encoder(&dvi->connector,
136                                           bridge->encoder);
137
138         return 0;
139 }
140
141 static const struct drm_bridge_funcs tfp410_bridge_funcs = {
142         .attach         = tfp410_attach,
143 };
144
145 static void tfp410_hpd_work_func(struct work_struct *work)
146 {
147         struct tfp410 *dvi;
148
149         dvi = container_of(work, struct tfp410, hpd_work.work);
150
151         if (dvi->bridge.dev)
152                 drm_helper_hpd_irq_event(dvi->bridge.dev);
153 }
154
155 static irqreturn_t tfp410_hpd_irq_thread(int irq, void *arg)
156 {
157         struct tfp410 *dvi = arg;
158
159         mod_delayed_work(system_wq, &dvi->hpd_work,
160                         msecs_to_jiffies(HOTPLUG_DEBOUNCE_MS));
161
162         return IRQ_HANDLED;
163 }
164
165 static int tfp410_get_connector_properties(struct tfp410 *dvi)
166 {
167         struct device_node *connector_node, *ddc_phandle;
168         int ret = 0;
169
170         /* port@1 is the connector node */
171         connector_node = of_graph_get_remote_node(dvi->dev->of_node, 1, -1);
172         if (!connector_node)
173                 return -ENODEV;
174
175         dvi->hpd = fwnode_get_named_gpiod(&connector_node->fwnode,
176                                         "hpd-gpios", 0, GPIOD_IN, "hpd");
177         if (IS_ERR(dvi->hpd)) {
178                 ret = PTR_ERR(dvi->hpd);
179                 dvi->hpd = NULL;
180                 if (ret == -ENOENT)
181                         ret = 0;
182                 else
183                         goto fail;
184         }
185
186         ddc_phandle = of_parse_phandle(connector_node, "ddc-i2c-bus", 0);
187         if (!ddc_phandle)
188                 goto fail;
189
190         dvi->ddc = of_get_i2c_adapter_by_node(ddc_phandle);
191         if (dvi->ddc)
192                 dev_info(dvi->dev, "Connector's ddc i2c bus found\n");
193         else
194                 ret = -EPROBE_DEFER;
195
196         of_node_put(ddc_phandle);
197
198 fail:
199         of_node_put(connector_node);
200         return ret;
201 }
202
203 static int tfp410_init(struct device *dev)
204 {
205         struct tfp410 *dvi;
206         int ret;
207
208         if (!dev->of_node) {
209                 dev_err(dev, "device-tree data is missing\n");
210                 return -ENXIO;
211         }
212
213         dvi = devm_kzalloc(dev, sizeof(*dvi), GFP_KERNEL);
214         if (!dvi)
215                 return -ENOMEM;
216         dev_set_drvdata(dev, dvi);
217
218         dvi->bridge.funcs = &tfp410_bridge_funcs;
219         dvi->bridge.of_node = dev->of_node;
220         dvi->dev = dev;
221
222         ret = tfp410_get_connector_properties(dvi);
223         if (ret)
224                 goto fail;
225
226         if (dvi->hpd) {
227                 INIT_DELAYED_WORK(&dvi->hpd_work, tfp410_hpd_work_func);
228
229                 ret = devm_request_threaded_irq(dev, gpiod_to_irq(dvi->hpd),
230                         NULL, tfp410_hpd_irq_thread, IRQF_TRIGGER_RISING |
231                         IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
232                         "hdmi-hpd", dvi);
233                 if (ret) {
234                         DRM_ERROR("failed to register hpd interrupt\n");
235                         goto fail;
236                 }
237         }
238
239         drm_bridge_add(&dvi->bridge);
240
241         return 0;
242 fail:
243         i2c_put_adapter(dvi->ddc);
244         if (dvi->hpd)
245                 gpiod_put(dvi->hpd);
246         return ret;
247 }
248
249 static int tfp410_fini(struct device *dev)
250 {
251         struct tfp410 *dvi = dev_get_drvdata(dev);
252
253         cancel_delayed_work_sync(&dvi->hpd_work);
254
255         drm_bridge_remove(&dvi->bridge);
256
257         if (dvi->ddc)
258                 i2c_put_adapter(dvi->ddc);
259         if (dvi->hpd)
260                 gpiod_put(dvi->hpd);
261
262         return 0;
263 }
264
265 static int tfp410_probe(struct platform_device *pdev)
266 {
267         return tfp410_init(&pdev->dev);
268 }
269
270 static int tfp410_remove(struct platform_device *pdev)
271 {
272         return tfp410_fini(&pdev->dev);
273 }
274
275 static const struct of_device_id tfp410_match[] = {
276         { .compatible = "ti,tfp410" },
277         {},
278 };
279 MODULE_DEVICE_TABLE(of, tfp410_match);
280
281 static struct platform_driver tfp410_platform_driver = {
282         .probe  = tfp410_probe,
283         .remove = tfp410_remove,
284         .driver = {
285                 .name           = "tfp410-bridge",
286                 .of_match_table = tfp410_match,
287         },
288 };
289
290 #if IS_ENABLED(CONFIG_I2C)
291 /* There is currently no i2c functionality. */
292 static int tfp410_i2c_probe(struct i2c_client *client,
293                             const struct i2c_device_id *id)
294 {
295         int reg;
296
297         if (!client->dev.of_node ||
298             of_property_read_u32(client->dev.of_node, "reg", &reg)) {
299                 dev_err(&client->dev,
300                         "Can't get i2c reg property from device-tree\n");
301                 return -ENXIO;
302         }
303
304         return tfp410_init(&client->dev);
305 }
306
307 static int tfp410_i2c_remove(struct i2c_client *client)
308 {
309         return tfp410_fini(&client->dev);
310 }
311
312 static const struct i2c_device_id tfp410_i2c_ids[] = {
313         { "tfp410", 0 },
314         { }
315 };
316 MODULE_DEVICE_TABLE(i2c, tfp410_i2c_ids);
317
318 static struct i2c_driver tfp410_i2c_driver = {
319         .driver = {
320                 .name   = "tfp410",
321                 .of_match_table = of_match_ptr(tfp410_match),
322         },
323         .id_table       = tfp410_i2c_ids,
324         .probe          = tfp410_i2c_probe,
325         .remove         = tfp410_i2c_remove,
326 };
327 #endif /* IS_ENABLED(CONFIG_I2C) */
328
329 static struct {
330         uint i2c:1;
331         uint platform:1;
332 }  tfp410_registered_driver;
333
334 static int __init tfp410_module_init(void)
335 {
336         int ret;
337
338 #if IS_ENABLED(CONFIG_I2C)
339         ret = i2c_add_driver(&tfp410_i2c_driver);
340         if (ret)
341                 pr_err("%s: registering i2c driver failed: %d",
342                        __func__, ret);
343         else
344                 tfp410_registered_driver.i2c = 1;
345 #endif
346
347         ret = platform_driver_register(&tfp410_platform_driver);
348         if (ret)
349                 pr_err("%s: registering platform driver failed: %d",
350                        __func__, ret);
351         else
352                 tfp410_registered_driver.platform = 1;
353
354         if (tfp410_registered_driver.i2c ||
355             tfp410_registered_driver.platform)
356                 return 0;
357
358         return ret;
359 }
360 module_init(tfp410_module_init);
361
362 static void __exit tfp410_module_exit(void)
363 {
364 #if IS_ENABLED(CONFIG_I2C)
365         if (tfp410_registered_driver.i2c)
366                 i2c_del_driver(&tfp410_i2c_driver);
367 #endif
368         if (tfp410_registered_driver.platform)
369                 platform_driver_unregister(&tfp410_platform_driver);
370 }
371 module_exit(tfp410_module_exit);
372
373 MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
374 MODULE_DESCRIPTION("TI TFP410 DVI bridge driver");
375 MODULE_LICENSE("GPL");