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