Merge tag 'drm-misc-next-2019-04-18' of git://anongit.freedesktop.org/drm/drm-misc...
[sfrench/cifs-2.6.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_probe_helper.h>
24
25 #define HOTPLUG_DEBOUNCE_MS             1100
26
27 struct tfp410 {
28         struct drm_bridge       bridge;
29         struct drm_connector    connector;
30         unsigned int            connector_type;
31
32         u32                     bus_format;
33         struct i2c_adapter      *ddc;
34         struct gpio_desc        *hpd;
35         int                     hpd_irq;
36         struct delayed_work     hpd_work;
37         struct gpio_desc        *powerdown;
38
39         struct drm_bridge_timings timings;
40
41         struct device *dev;
42 };
43
44 static inline struct tfp410 *
45 drm_bridge_to_tfp410(struct drm_bridge *bridge)
46 {
47         return container_of(bridge, struct tfp410, bridge);
48 }
49
50 static inline struct tfp410 *
51 drm_connector_to_tfp410(struct drm_connector *connector)
52 {
53         return container_of(connector, struct tfp410, connector);
54 }
55
56 static int tfp410_get_modes(struct drm_connector *connector)
57 {
58         struct tfp410 *dvi = drm_connector_to_tfp410(connector);
59         struct edid *edid;
60         int ret;
61
62         if (!dvi->ddc)
63                 goto fallback;
64
65         edid = drm_get_edid(connector, dvi->ddc);
66         if (!edid) {
67                 DRM_INFO("EDID read failed. Fallback to standard modes\n");
68                 goto fallback;
69         }
70
71         drm_connector_update_edid_property(connector, edid);
72
73         return drm_add_edid_modes(connector, edid);
74 fallback:
75         /* No EDID, fallback on the XGA standard modes */
76         ret = drm_add_modes_noedid(connector, 1920, 1200);
77
78         /* And prefer a mode pretty much anything can handle */
79         drm_set_preferred_mode(connector, 1024, 768);
80
81         return ret;
82 }
83
84 static const struct drm_connector_helper_funcs tfp410_con_helper_funcs = {
85         .get_modes      = tfp410_get_modes,
86 };
87
88 static enum drm_connector_status
89 tfp410_connector_detect(struct drm_connector *connector, bool force)
90 {
91         struct tfp410 *dvi = drm_connector_to_tfp410(connector);
92
93         if (dvi->hpd) {
94                 if (gpiod_get_value_cansleep(dvi->hpd))
95                         return connector_status_connected;
96                 else
97                         return connector_status_disconnected;
98         }
99
100         if (dvi->ddc) {
101                 if (drm_probe_ddc(dvi->ddc))
102                         return connector_status_connected;
103                 else
104                         return connector_status_disconnected;
105         }
106
107         return connector_status_unknown;
108 }
109
110 static const struct drm_connector_funcs tfp410_con_funcs = {
111         .detect                 = tfp410_connector_detect,
112         .fill_modes             = drm_helper_probe_single_connector_modes,
113         .destroy                = drm_connector_cleanup,
114         .reset                  = drm_atomic_helper_connector_reset,
115         .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
116         .atomic_destroy_state   = drm_atomic_helper_connector_destroy_state,
117 };
118
119 static int tfp410_attach(struct drm_bridge *bridge)
120 {
121         struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
122         int ret;
123
124         if (!bridge->encoder) {
125                 dev_err(dvi->dev, "Missing encoder\n");
126                 return -ENODEV;
127         }
128
129         if (dvi->hpd_irq >= 0)
130                 dvi->connector.polled = DRM_CONNECTOR_POLL_HPD;
131         else
132                 dvi->connector.polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
133
134         drm_connector_helper_add(&dvi->connector,
135                                  &tfp410_con_helper_funcs);
136         ret = drm_connector_init(bridge->dev, &dvi->connector,
137                                  &tfp410_con_funcs, dvi->connector_type);
138         if (ret) {
139                 dev_err(dvi->dev, "drm_connector_init() failed: %d\n", ret);
140                 return ret;
141         }
142
143         drm_display_info_set_bus_formats(&dvi->connector.display_info,
144                                          &dvi->bus_format, 1);
145
146         drm_connector_attach_encoder(&dvi->connector,
147                                           bridge->encoder);
148
149         return 0;
150 }
151
152 static void tfp410_enable(struct drm_bridge *bridge)
153 {
154         struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
155
156         gpiod_set_value_cansleep(dvi->powerdown, 0);
157 }
158
159 static void tfp410_disable(struct drm_bridge *bridge)
160 {
161         struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
162
163         gpiod_set_value_cansleep(dvi->powerdown, 1);
164 }
165
166 static const struct drm_bridge_funcs tfp410_bridge_funcs = {
167         .attach         = tfp410_attach,
168         .enable         = tfp410_enable,
169         .disable        = tfp410_disable,
170 };
171
172 static void tfp410_hpd_work_func(struct work_struct *work)
173 {
174         struct tfp410 *dvi;
175
176         dvi = container_of(work, struct tfp410, hpd_work.work);
177
178         if (dvi->bridge.dev)
179                 drm_helper_hpd_irq_event(dvi->bridge.dev);
180 }
181
182 static irqreturn_t tfp410_hpd_irq_thread(int irq, void *arg)
183 {
184         struct tfp410 *dvi = arg;
185
186         mod_delayed_work(system_wq, &dvi->hpd_work,
187                         msecs_to_jiffies(HOTPLUG_DEBOUNCE_MS));
188
189         return IRQ_HANDLED;
190 }
191
192 static const struct drm_bridge_timings tfp410_default_timings = {
193         .input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
194                          | DRM_BUS_FLAG_DE_HIGH,
195         .setup_time_ps = 1200,
196         .hold_time_ps = 1300,
197 };
198
199 static int tfp410_parse_timings(struct tfp410 *dvi, bool i2c)
200 {
201         struct drm_bridge_timings *timings = &dvi->timings;
202         struct device_node *ep;
203         u32 pclk_sample = 0;
204         u32 bus_width = 24;
205         s32 deskew = 0;
206
207         /* Start with defaults. */
208         *timings = tfp410_default_timings;
209
210         if (i2c)
211                 /*
212                  * In I2C mode timings are configured through the I2C interface.
213                  * As the driver doesn't support I2C configuration yet, we just
214                  * go with the defaults (BSEL=1, DSEL=1, DKEN=0, EDGE=1).
215                  */
216                 return 0;
217
218         /*
219          * In non-I2C mode, timings are configured through the BSEL, DSEL, DKEN
220          * and EDGE pins. They are specified in DT through endpoint properties
221          * and vendor-specific properties.
222          */
223         ep = of_graph_get_endpoint_by_regs(dvi->dev->of_node, 0, 0);
224         if (!ep)
225                 return -EINVAL;
226
227         /* Get the sampling edge from the endpoint. */
228         of_property_read_u32(ep, "pclk-sample", &pclk_sample);
229         of_property_read_u32(ep, "bus-width", &bus_width);
230         of_node_put(ep);
231
232         timings->input_bus_flags = DRM_BUS_FLAG_DE_HIGH;
233
234         switch (pclk_sample) {
235         case 0:
236                 timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE
237                                          |  DRM_BUS_FLAG_SYNC_SAMPLE_NEGEDGE;
238                 break;
239         case 1:
240                 timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
241                                          |  DRM_BUS_FLAG_SYNC_SAMPLE_POSEDGE;
242                 break;
243         default:
244                 return -EINVAL;
245         }
246
247         switch (bus_width) {
248         case 12:
249                 dvi->bus_format = MEDIA_BUS_FMT_RGB888_2X12_LE;
250                 break;
251         case 24:
252                 dvi->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
253                 break;
254         default:
255                 return -EINVAL;
256         }
257
258         /* Get the setup and hold time from vendor-specific properties. */
259         of_property_read_u32(dvi->dev->of_node, "ti,deskew", (u32 *)&deskew);
260         if (deskew < -4 || deskew > 3)
261                 return -EINVAL;
262
263         timings->setup_time_ps = min(0, 1200 - 350 * deskew);
264         timings->hold_time_ps = min(0, 1300 + 350 * deskew);
265
266         return 0;
267 }
268
269 static int tfp410_get_connector_properties(struct tfp410 *dvi)
270 {
271         struct device_node *connector_node, *ddc_phandle;
272         int ret = 0;
273
274         /* port@1 is the connector node */
275         connector_node = of_graph_get_remote_node(dvi->dev->of_node, 1, -1);
276         if (!connector_node)
277                 return -ENODEV;
278
279         if (of_device_is_compatible(connector_node, "hdmi-connector"))
280                 dvi->connector_type = DRM_MODE_CONNECTOR_HDMIA;
281         else
282                 dvi->connector_type = DRM_MODE_CONNECTOR_DVID;
283
284         dvi->hpd = fwnode_get_named_gpiod(&connector_node->fwnode,
285                                         "hpd-gpios", 0, GPIOD_IN, "hpd");
286         if (IS_ERR(dvi->hpd)) {
287                 ret = PTR_ERR(dvi->hpd);
288                 dvi->hpd = NULL;
289                 if (ret == -ENOENT)
290                         ret = 0;
291                 else
292                         goto fail;
293         }
294
295         ddc_phandle = of_parse_phandle(connector_node, "ddc-i2c-bus", 0);
296         if (!ddc_phandle)
297                 goto fail;
298
299         dvi->ddc = of_get_i2c_adapter_by_node(ddc_phandle);
300         if (dvi->ddc)
301                 dev_info(dvi->dev, "Connector's ddc i2c bus found\n");
302         else
303                 ret = -EPROBE_DEFER;
304
305         of_node_put(ddc_phandle);
306
307 fail:
308         of_node_put(connector_node);
309         return ret;
310 }
311
312 static int tfp410_init(struct device *dev, bool i2c)
313 {
314         struct tfp410 *dvi;
315         int ret;
316
317         if (!dev->of_node) {
318                 dev_err(dev, "device-tree data is missing\n");
319                 return -ENXIO;
320         }
321
322         dvi = devm_kzalloc(dev, sizeof(*dvi), GFP_KERNEL);
323         if (!dvi)
324                 return -ENOMEM;
325         dev_set_drvdata(dev, dvi);
326
327         dvi->bridge.funcs = &tfp410_bridge_funcs;
328         dvi->bridge.of_node = dev->of_node;
329         dvi->bridge.timings = &dvi->timings;
330         dvi->dev = dev;
331
332         ret = tfp410_parse_timings(dvi, i2c);
333         if (ret)
334                 goto fail;
335
336         ret = tfp410_get_connector_properties(dvi);
337         if (ret)
338                 goto fail;
339
340         dvi->powerdown = devm_gpiod_get_optional(dev, "powerdown",
341                                                  GPIOD_OUT_HIGH);
342         if (IS_ERR(dvi->powerdown)) {
343                 dev_err(dev, "failed to parse powerdown gpio\n");
344                 return PTR_ERR(dvi->powerdown);
345         }
346
347         if (dvi->hpd)
348                 dvi->hpd_irq = gpiod_to_irq(dvi->hpd);
349         else
350                 dvi->hpd_irq = -ENXIO;
351
352         if (dvi->hpd_irq >= 0) {
353                 INIT_DELAYED_WORK(&dvi->hpd_work, tfp410_hpd_work_func);
354
355                 ret = devm_request_threaded_irq(dev, dvi->hpd_irq,
356                         NULL, tfp410_hpd_irq_thread, IRQF_TRIGGER_RISING |
357                         IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
358                         "hdmi-hpd", dvi);
359                 if (ret) {
360                         DRM_ERROR("failed to register hpd interrupt\n");
361                         goto fail;
362                 }
363         }
364
365         drm_bridge_add(&dvi->bridge);
366
367         return 0;
368 fail:
369         i2c_put_adapter(dvi->ddc);
370         if (dvi->hpd)
371                 gpiod_put(dvi->hpd);
372         return ret;
373 }
374
375 static int tfp410_fini(struct device *dev)
376 {
377         struct tfp410 *dvi = dev_get_drvdata(dev);
378
379         cancel_delayed_work_sync(&dvi->hpd_work);
380
381         drm_bridge_remove(&dvi->bridge);
382
383         if (dvi->ddc)
384                 i2c_put_adapter(dvi->ddc);
385         if (dvi->hpd)
386                 gpiod_put(dvi->hpd);
387
388         return 0;
389 }
390
391 static int tfp410_probe(struct platform_device *pdev)
392 {
393         return tfp410_init(&pdev->dev, false);
394 }
395
396 static int tfp410_remove(struct platform_device *pdev)
397 {
398         return tfp410_fini(&pdev->dev);
399 }
400
401 static const struct of_device_id tfp410_match[] = {
402         { .compatible = "ti,tfp410" },
403         {},
404 };
405 MODULE_DEVICE_TABLE(of, tfp410_match);
406
407 static struct platform_driver tfp410_platform_driver = {
408         .probe  = tfp410_probe,
409         .remove = tfp410_remove,
410         .driver = {
411                 .name           = "tfp410-bridge",
412                 .of_match_table = tfp410_match,
413         },
414 };
415
416 #if IS_ENABLED(CONFIG_I2C)
417 /* There is currently no i2c functionality. */
418 static int tfp410_i2c_probe(struct i2c_client *client,
419                             const struct i2c_device_id *id)
420 {
421         int reg;
422
423         if (!client->dev.of_node ||
424             of_property_read_u32(client->dev.of_node, "reg", &reg)) {
425                 dev_err(&client->dev,
426                         "Can't get i2c reg property from device-tree\n");
427                 return -ENXIO;
428         }
429
430         return tfp410_init(&client->dev, true);
431 }
432
433 static int tfp410_i2c_remove(struct i2c_client *client)
434 {
435         return tfp410_fini(&client->dev);
436 }
437
438 static const struct i2c_device_id tfp410_i2c_ids[] = {
439         { "tfp410", 0 },
440         { }
441 };
442 MODULE_DEVICE_TABLE(i2c, tfp410_i2c_ids);
443
444 static struct i2c_driver tfp410_i2c_driver = {
445         .driver = {
446                 .name   = "tfp410",
447                 .of_match_table = of_match_ptr(tfp410_match),
448         },
449         .id_table       = tfp410_i2c_ids,
450         .probe          = tfp410_i2c_probe,
451         .remove         = tfp410_i2c_remove,
452 };
453 #endif /* IS_ENABLED(CONFIG_I2C) */
454
455 static struct {
456         uint i2c:1;
457         uint platform:1;
458 }  tfp410_registered_driver;
459
460 static int __init tfp410_module_init(void)
461 {
462         int ret;
463
464 #if IS_ENABLED(CONFIG_I2C)
465         ret = i2c_add_driver(&tfp410_i2c_driver);
466         if (ret)
467                 pr_err("%s: registering i2c driver failed: %d",
468                        __func__, ret);
469         else
470                 tfp410_registered_driver.i2c = 1;
471 #endif
472
473         ret = platform_driver_register(&tfp410_platform_driver);
474         if (ret)
475                 pr_err("%s: registering platform driver failed: %d",
476                        __func__, ret);
477         else
478                 tfp410_registered_driver.platform = 1;
479
480         if (tfp410_registered_driver.i2c ||
481             tfp410_registered_driver.platform)
482                 return 0;
483
484         return ret;
485 }
486 module_init(tfp410_module_init);
487
488 static void __exit tfp410_module_exit(void)
489 {
490 #if IS_ENABLED(CONFIG_I2C)
491         if (tfp410_registered_driver.i2c)
492                 i2c_del_driver(&tfp410_i2c_driver);
493 #endif
494         if (tfp410_registered_driver.platform)
495                 platform_driver_unregister(&tfp410_platform_driver);
496 }
497 module_exit(tfp410_module_exit);
498
499 MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
500 MODULE_DESCRIPTION("TI TFP410 DVI bridge driver");
501 MODULE_LICENSE("GPL");