1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2016 Texas Instruments
4 * Author: Jyri Sarha <jsarha@ti.com>
7 #include <linux/gpio/consumer.h>
9 #include <linux/media-bus-format.h>
10 #include <linux/module.h>
11 #include <linux/of_graph.h>
12 #include <linux/platform_device.h>
13 #include <linux/workqueue.h>
15 #include <drm/drm_atomic_helper.h>
16 #include <drm/drm_bridge.h>
17 #include <drm/drm_crtc.h>
18 #include <drm/drm_edid.h>
19 #include <drm/drm_print.h>
20 #include <drm/drm_probe_helper.h>
22 #define HOTPLUG_DEBOUNCE_MS 1100
25 struct drm_bridge bridge;
26 struct drm_connector connector;
29 struct delayed_work hpd_work;
30 struct gpio_desc *powerdown;
32 struct drm_bridge_timings timings;
33 struct drm_bridge *next_bridge;
38 static inline struct tfp410 *
39 drm_bridge_to_tfp410(struct drm_bridge *bridge)
41 return container_of(bridge, struct tfp410, bridge);
44 static inline struct tfp410 *
45 drm_connector_to_tfp410(struct drm_connector *connector)
47 return container_of(connector, struct tfp410, connector);
50 static int tfp410_get_modes(struct drm_connector *connector)
52 struct tfp410 *dvi = drm_connector_to_tfp410(connector);
56 if (dvi->next_bridge->ops & DRM_BRIDGE_OP_EDID) {
57 edid = drm_bridge_get_edid(dvi->next_bridge, connector);
59 DRM_INFO("EDID read failed. Fallback to standard modes\n");
66 * No EDID, fallback on the XGA standard modes and prefer a mode
67 * pretty much anything can handle.
69 ret = drm_add_modes_noedid(connector, 1920, 1200);
70 drm_set_preferred_mode(connector, 1024, 768);
74 drm_connector_update_edid_property(connector, edid);
76 ret = drm_add_edid_modes(connector, edid);
83 static const struct drm_connector_helper_funcs tfp410_con_helper_funcs = {
84 .get_modes = tfp410_get_modes,
87 static enum drm_connector_status
88 tfp410_connector_detect(struct drm_connector *connector, bool force)
90 struct tfp410 *dvi = drm_connector_to_tfp410(connector);
92 return drm_bridge_detect(dvi->next_bridge);
95 static const struct drm_connector_funcs tfp410_con_funcs = {
96 .detect = tfp410_connector_detect,
97 .fill_modes = drm_helper_probe_single_connector_modes,
98 .destroy = drm_connector_cleanup,
99 .reset = drm_atomic_helper_connector_reset,
100 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
101 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
104 static void tfp410_hpd_work_func(struct work_struct *work)
108 dvi = container_of(work, struct tfp410, hpd_work.work);
111 drm_helper_hpd_irq_event(dvi->bridge.dev);
114 static void tfp410_hpd_callback(void *arg, enum drm_connector_status status)
116 struct tfp410 *dvi = arg;
118 mod_delayed_work(system_wq, &dvi->hpd_work,
119 msecs_to_jiffies(HOTPLUG_DEBOUNCE_MS));
122 static int tfp410_attach(struct drm_bridge *bridge,
123 enum drm_bridge_attach_flags flags)
125 struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
128 ret = drm_bridge_attach(bridge->encoder, dvi->next_bridge, bridge,
129 DRM_BRIDGE_ATTACH_NO_CONNECTOR);
133 if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
136 if (!bridge->encoder) {
137 dev_err(dvi->dev, "Missing encoder\n");
141 if (dvi->next_bridge->ops & DRM_BRIDGE_OP_DETECT)
142 dvi->connector.polled = DRM_CONNECTOR_POLL_HPD;
144 dvi->connector.polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
146 if (dvi->next_bridge->ops & DRM_BRIDGE_OP_HPD) {
147 INIT_DELAYED_WORK(&dvi->hpd_work, tfp410_hpd_work_func);
148 drm_bridge_hpd_enable(dvi->next_bridge, tfp410_hpd_callback,
152 drm_connector_helper_add(&dvi->connector,
153 &tfp410_con_helper_funcs);
154 ret = drm_connector_init_with_ddc(bridge->dev, &dvi->connector,
156 dvi->next_bridge->type,
157 dvi->next_bridge->ddc);
159 dev_err(dvi->dev, "drm_connector_init_with_ddc() failed: %d\n",
164 drm_display_info_set_bus_formats(&dvi->connector.display_info,
165 &dvi->bus_format, 1);
167 drm_connector_attach_encoder(&dvi->connector, bridge->encoder);
172 static void tfp410_detach(struct drm_bridge *bridge)
174 struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
176 if (dvi->connector.dev && dvi->next_bridge->ops & DRM_BRIDGE_OP_HPD) {
177 drm_bridge_hpd_disable(dvi->next_bridge);
178 cancel_delayed_work_sync(&dvi->hpd_work);
182 static void tfp410_enable(struct drm_bridge *bridge)
184 struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
186 gpiod_set_value_cansleep(dvi->powerdown, 0);
189 static void tfp410_disable(struct drm_bridge *bridge)
191 struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
193 gpiod_set_value_cansleep(dvi->powerdown, 1);
196 static enum drm_mode_status tfp410_mode_valid(struct drm_bridge *bridge,
197 const struct drm_display_info *info,
198 const struct drm_display_mode *mode)
200 if (mode->clock < 25000)
201 return MODE_CLOCK_LOW;
203 if (mode->clock > 165000)
204 return MODE_CLOCK_HIGH;
209 static u32 *tfp410_get_input_bus_fmts(struct drm_bridge *bridge,
210 struct drm_bridge_state *bridge_state,
211 struct drm_crtc_state *crtc_state,
212 struct drm_connector_state *conn_state,
214 unsigned int *num_input_fmts)
216 struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
221 input_fmts = kzalloc(sizeof(*input_fmts), GFP_KERNEL);
226 input_fmts[0] = dvi->bus_format;
231 static int tfp410_atomic_check(struct drm_bridge *bridge,
232 struct drm_bridge_state *bridge_state,
233 struct drm_crtc_state *crtc_state,
234 struct drm_connector_state *conn_state)
236 struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
239 * There might be flags negotiation supported in future.
240 * Set the bus flags in atomic_check statically for now.
242 bridge_state->input_bus_cfg.flags = dvi->timings.input_bus_flags;
247 static const struct drm_bridge_funcs tfp410_bridge_funcs = {
248 .attach = tfp410_attach,
249 .detach = tfp410_detach,
250 .enable = tfp410_enable,
251 .disable = tfp410_disable,
252 .mode_valid = tfp410_mode_valid,
253 .atomic_reset = drm_atomic_helper_bridge_reset,
254 .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
255 .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
256 .atomic_get_input_bus_fmts = tfp410_get_input_bus_fmts,
257 .atomic_check = tfp410_atomic_check,
260 static const struct drm_bridge_timings tfp410_default_timings = {
261 .input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
262 | DRM_BUS_FLAG_DE_HIGH,
263 .setup_time_ps = 1200,
264 .hold_time_ps = 1300,
267 static int tfp410_parse_timings(struct tfp410 *dvi, bool i2c)
269 struct drm_bridge_timings *timings = &dvi->timings;
270 struct device_node *ep;
275 /* Start with defaults. */
276 *timings = tfp410_default_timings;
280 * In I2C mode timings are configured through the I2C interface.
281 * As the driver doesn't support I2C configuration yet, we just
282 * go with the defaults (BSEL=1, DSEL=1, DKEN=0, EDGE=1).
287 * In non-I2C mode, timings are configured through the BSEL, DSEL, DKEN
288 * and EDGE pins. They are specified in DT through endpoint properties
289 * and vendor-specific properties.
291 ep = of_graph_get_endpoint_by_regs(dvi->dev->of_node, 0, 0);
295 /* Get the sampling edge from the endpoint. */
296 of_property_read_u32(ep, "pclk-sample", &pclk_sample);
297 of_property_read_u32(ep, "bus-width", &bus_width);
300 timings->input_bus_flags = DRM_BUS_FLAG_DE_HIGH;
302 switch (pclk_sample) {
304 timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE
305 | DRM_BUS_FLAG_SYNC_SAMPLE_NEGEDGE;
308 timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
309 | DRM_BUS_FLAG_SYNC_SAMPLE_POSEDGE;
317 dvi->bus_format = MEDIA_BUS_FMT_RGB888_2X12_LE;
320 dvi->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
326 /* Get the setup and hold time from vendor-specific properties. */
327 of_property_read_u32(dvi->dev->of_node, "ti,deskew", &deskew);
331 timings->setup_time_ps = 1200 - 350 * ((s32)deskew - 4);
332 timings->hold_time_ps = max(0, 1300 + 350 * ((s32)deskew - 4));
337 static int tfp410_init(struct device *dev, bool i2c)
339 struct device_node *node;
344 dev_err(dev, "device-tree data is missing\n");
348 dvi = devm_kzalloc(dev, sizeof(*dvi), GFP_KERNEL);
353 dev_set_drvdata(dev, dvi);
355 dvi->bridge.funcs = &tfp410_bridge_funcs;
356 dvi->bridge.of_node = dev->of_node;
357 dvi->bridge.timings = &dvi->timings;
358 dvi->bridge.type = DRM_MODE_CONNECTOR_DVID;
360 ret = tfp410_parse_timings(dvi, i2c);
364 /* Get the next bridge, connected to port@1. */
365 node = of_graph_get_remote_node(dev->of_node, 1, -1);
369 dvi->next_bridge = of_drm_find_bridge(node);
372 if (!dvi->next_bridge)
373 return -EPROBE_DEFER;
375 /* Get the powerdown GPIO. */
376 dvi->powerdown = devm_gpiod_get_optional(dev, "powerdown",
378 if (IS_ERR(dvi->powerdown)) {
379 dev_err(dev, "failed to parse powerdown gpio\n");
380 return PTR_ERR(dvi->powerdown);
383 /* Register the DRM bridge. */
384 drm_bridge_add(&dvi->bridge);
389 static void tfp410_fini(struct device *dev)
391 struct tfp410 *dvi = dev_get_drvdata(dev);
393 drm_bridge_remove(&dvi->bridge);
396 static int tfp410_probe(struct platform_device *pdev)
398 return tfp410_init(&pdev->dev, false);
401 static void tfp410_remove(struct platform_device *pdev)
403 tfp410_fini(&pdev->dev);
406 static const struct of_device_id tfp410_match[] = {
407 { .compatible = "ti,tfp410" },
410 MODULE_DEVICE_TABLE(of, tfp410_match);
412 static struct platform_driver tfp410_platform_driver = {
413 .probe = tfp410_probe,
414 .remove_new = tfp410_remove,
416 .name = "tfp410-bridge",
417 .of_match_table = tfp410_match,
421 #if IS_ENABLED(CONFIG_I2C)
422 /* There is currently no i2c functionality. */
423 static int tfp410_i2c_probe(struct i2c_client *client)
427 if (!client->dev.of_node ||
428 of_property_read_u32(client->dev.of_node, "reg", ®)) {
429 dev_err(&client->dev,
430 "Can't get i2c reg property from device-tree\n");
434 return tfp410_init(&client->dev, true);
437 static void tfp410_i2c_remove(struct i2c_client *client)
439 tfp410_fini(&client->dev);
442 static const struct i2c_device_id tfp410_i2c_ids[] = {
446 MODULE_DEVICE_TABLE(i2c, tfp410_i2c_ids);
448 static struct i2c_driver tfp410_i2c_driver = {
451 .of_match_table = tfp410_match,
453 .id_table = tfp410_i2c_ids,
454 .probe = tfp410_i2c_probe,
455 .remove = tfp410_i2c_remove,
457 #endif /* IS_ENABLED(CONFIG_I2C) */
462 } tfp410_registered_driver;
464 static int __init tfp410_module_init(void)
468 #if IS_ENABLED(CONFIG_I2C)
469 ret = i2c_add_driver(&tfp410_i2c_driver);
471 pr_err("%s: registering i2c driver failed: %d",
474 tfp410_registered_driver.i2c = 1;
477 ret = platform_driver_register(&tfp410_platform_driver);
479 pr_err("%s: registering platform driver failed: %d",
482 tfp410_registered_driver.platform = 1;
484 if (tfp410_registered_driver.i2c ||
485 tfp410_registered_driver.platform)
490 module_init(tfp410_module_init);
492 static void __exit tfp410_module_exit(void)
494 #if IS_ENABLED(CONFIG_I2C)
495 if (tfp410_registered_driver.i2c)
496 i2c_del_driver(&tfp410_i2c_driver);
498 if (tfp410_registered_driver.platform)
499 platform_driver_unregister(&tfp410_platform_driver);
501 module_exit(tfp410_module_exit);
503 MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
504 MODULE_DESCRIPTION("TI TFP410 DVI bridge driver");
505 MODULE_LICENSE("GPL");