1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2016 Texas Instruments 4 * Author: Jyri Sarha <jsarha@ti.com> 5 */ 6 7 #include <linux/gpio/consumer.h> 8 #include <linux/i2c.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> 14 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> 21 22 #define HOTPLUG_DEBOUNCE_MS 1100 23 24 struct tfp410 { 25 struct drm_bridge bridge; 26 struct drm_connector connector; 27 28 u32 bus_format; 29 struct delayed_work hpd_work; 30 struct gpio_desc *powerdown; 31 32 struct drm_bridge_timings timings; 33 struct drm_bridge *next_bridge; 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->next_bridge->ops & DRM_BRIDGE_OP_EDID) { 57 edid = drm_bridge_get_edid(dvi->next_bridge, connector); 58 if (!edid) 59 DRM_INFO("EDID read failed. Fallback to standard modes\n"); 60 } else { 61 edid = NULL; 62 } 63 64 if (!edid) { 65 /* 66 * No EDID, fallback on the XGA standard modes and prefer a mode 67 * pretty much anything can handle. 68 */ 69 ret = drm_add_modes_noedid(connector, 1920, 1200); 70 drm_set_preferred_mode(connector, 1024, 768); 71 return ret; 72 } 73 74 drm_connector_update_edid_property(connector, edid); 75 76 ret = drm_add_edid_modes(connector, edid); 77 78 kfree(edid); 79 80 return ret; 81 } 82 83 static const struct drm_connector_helper_funcs tfp410_con_helper_funcs = { 84 .get_modes = tfp410_get_modes, 85 }; 86 87 static enum drm_connector_status 88 tfp410_connector_detect(struct drm_connector *connector, bool force) 89 { 90 struct tfp410 *dvi = drm_connector_to_tfp410(connector); 91 92 return drm_bridge_detect(dvi->next_bridge); 93 } 94 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, 102 }; 103 104 static void tfp410_hpd_work_func(struct work_struct *work) 105 { 106 struct tfp410 *dvi; 107 108 dvi = container_of(work, struct tfp410, hpd_work.work); 109 110 if (dvi->bridge.dev) 111 drm_helper_hpd_irq_event(dvi->bridge.dev); 112 } 113 114 static void tfp410_hpd_callback(void *arg, enum drm_connector_status status) 115 { 116 struct tfp410 *dvi = arg; 117 118 mod_delayed_work(system_wq, &dvi->hpd_work, 119 msecs_to_jiffies(HOTPLUG_DEBOUNCE_MS)); 120 } 121 122 static int tfp410_attach(struct drm_bridge *bridge, 123 enum drm_bridge_attach_flags flags) 124 { 125 struct tfp410 *dvi = drm_bridge_to_tfp410(bridge); 126 int ret; 127 128 ret = drm_bridge_attach(bridge->encoder, dvi->next_bridge, bridge, 129 DRM_BRIDGE_ATTACH_NO_CONNECTOR); 130 if (ret < 0) 131 return ret; 132 133 if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR) 134 return 0; 135 136 if (!bridge->encoder) { 137 dev_err(dvi->dev, "Missing encoder\n"); 138 return -ENODEV; 139 } 140 141 if (dvi->next_bridge->ops & DRM_BRIDGE_OP_DETECT) 142 dvi->connector.polled = DRM_CONNECTOR_POLL_HPD; 143 else 144 dvi->connector.polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT; 145 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, 149 dvi); 150 } 151 152 drm_connector_helper_add(&dvi->connector, 153 &tfp410_con_helper_funcs); 154 ret = drm_connector_init_with_ddc(bridge->dev, &dvi->connector, 155 &tfp410_con_funcs, 156 dvi->next_bridge->type, 157 dvi->next_bridge->ddc); 158 if (ret) { 159 dev_err(dvi->dev, "drm_connector_init_with_ddc() failed: %d\n", 160 ret); 161 return ret; 162 } 163 164 drm_display_info_set_bus_formats(&dvi->connector.display_info, 165 &dvi->bus_format, 1); 166 167 drm_connector_attach_encoder(&dvi->connector, bridge->encoder); 168 169 return 0; 170 } 171 172 static void tfp410_detach(struct drm_bridge *bridge) 173 { 174 struct tfp410 *dvi = drm_bridge_to_tfp410(bridge); 175 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); 179 } 180 } 181 182 static void tfp410_enable(struct drm_bridge *bridge) 183 { 184 struct tfp410 *dvi = drm_bridge_to_tfp410(bridge); 185 186 gpiod_set_value_cansleep(dvi->powerdown, 0); 187 } 188 189 static void tfp410_disable(struct drm_bridge *bridge) 190 { 191 struct tfp410 *dvi = drm_bridge_to_tfp410(bridge); 192 193 gpiod_set_value_cansleep(dvi->powerdown, 1); 194 } 195 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) 199 { 200 if (mode->clock < 25000) 201 return MODE_CLOCK_LOW; 202 203 if (mode->clock > 165000) 204 return MODE_CLOCK_HIGH; 205 206 return MODE_OK; 207 } 208 209 static const struct drm_bridge_funcs tfp410_bridge_funcs = { 210 .attach = tfp410_attach, 211 .detach = tfp410_detach, 212 .enable = tfp410_enable, 213 .disable = tfp410_disable, 214 .mode_valid = tfp410_mode_valid, 215 }; 216 217 static const struct drm_bridge_timings tfp410_default_timings = { 218 .input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE 219 | DRM_BUS_FLAG_DE_HIGH, 220 .setup_time_ps = 1200, 221 .hold_time_ps = 1300, 222 }; 223 224 static int tfp410_parse_timings(struct tfp410 *dvi, bool i2c) 225 { 226 struct drm_bridge_timings *timings = &dvi->timings; 227 struct device_node *ep; 228 u32 pclk_sample = 0; 229 u32 bus_width = 24; 230 u32 deskew = 0; 231 232 /* Start with defaults. */ 233 *timings = tfp410_default_timings; 234 235 if (i2c) 236 /* 237 * In I2C mode timings are configured through the I2C interface. 238 * As the driver doesn't support I2C configuration yet, we just 239 * go with the defaults (BSEL=1, DSEL=1, DKEN=0, EDGE=1). 240 */ 241 return 0; 242 243 /* 244 * In non-I2C mode, timings are configured through the BSEL, DSEL, DKEN 245 * and EDGE pins. They are specified in DT through endpoint properties 246 * and vendor-specific properties. 247 */ 248 ep = of_graph_get_endpoint_by_regs(dvi->dev->of_node, 0, 0); 249 if (!ep) 250 return -EINVAL; 251 252 /* Get the sampling edge from the endpoint. */ 253 of_property_read_u32(ep, "pclk-sample", &pclk_sample); 254 of_property_read_u32(ep, "bus-width", &bus_width); 255 of_node_put(ep); 256 257 timings->input_bus_flags = DRM_BUS_FLAG_DE_HIGH; 258 259 switch (pclk_sample) { 260 case 0: 261 timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE 262 | DRM_BUS_FLAG_SYNC_SAMPLE_NEGEDGE; 263 break; 264 case 1: 265 timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE 266 | DRM_BUS_FLAG_SYNC_SAMPLE_POSEDGE; 267 break; 268 default: 269 return -EINVAL; 270 } 271 272 switch (bus_width) { 273 case 12: 274 dvi->bus_format = MEDIA_BUS_FMT_RGB888_2X12_LE; 275 break; 276 case 24: 277 dvi->bus_format = MEDIA_BUS_FMT_RGB888_1X24; 278 break; 279 default: 280 return -EINVAL; 281 } 282 283 /* Get the setup and hold time from vendor-specific properties. */ 284 of_property_read_u32(dvi->dev->of_node, "ti,deskew", &deskew); 285 if (deskew > 7) 286 return -EINVAL; 287 288 timings->setup_time_ps = 1200 - 350 * ((s32)deskew - 4); 289 timings->hold_time_ps = max(0, 1300 + 350 * ((s32)deskew - 4)); 290 291 return 0; 292 } 293 294 static int tfp410_init(struct device *dev, bool i2c) 295 { 296 struct device_node *node; 297 struct tfp410 *dvi; 298 int ret; 299 300 if (!dev->of_node) { 301 dev_err(dev, "device-tree data is missing\n"); 302 return -ENXIO; 303 } 304 305 dvi = devm_kzalloc(dev, sizeof(*dvi), GFP_KERNEL); 306 if (!dvi) 307 return -ENOMEM; 308 309 dvi->dev = dev; 310 dev_set_drvdata(dev, dvi); 311 312 dvi->bridge.funcs = &tfp410_bridge_funcs; 313 dvi->bridge.of_node = dev->of_node; 314 dvi->bridge.timings = &dvi->timings; 315 dvi->bridge.type = DRM_MODE_CONNECTOR_DVID; 316 317 ret = tfp410_parse_timings(dvi, i2c); 318 if (ret) 319 return ret; 320 321 /* Get the next bridge, connected to port@1. */ 322 node = of_graph_get_remote_node(dev->of_node, 1, -1); 323 if (!node) 324 return -ENODEV; 325 326 dvi->next_bridge = of_drm_find_bridge(node); 327 of_node_put(node); 328 329 if (!dvi->next_bridge) 330 return -EPROBE_DEFER; 331 332 /* Get the powerdown GPIO. */ 333 dvi->powerdown = devm_gpiod_get_optional(dev, "powerdown", 334 GPIOD_OUT_HIGH); 335 if (IS_ERR(dvi->powerdown)) { 336 dev_err(dev, "failed to parse powerdown gpio\n"); 337 return PTR_ERR(dvi->powerdown); 338 } 339 340 /* Register the DRM bridge. */ 341 drm_bridge_add(&dvi->bridge); 342 343 return 0; 344 } 345 346 static void tfp410_fini(struct device *dev) 347 { 348 struct tfp410 *dvi = dev_get_drvdata(dev); 349 350 drm_bridge_remove(&dvi->bridge); 351 } 352 353 static int tfp410_probe(struct platform_device *pdev) 354 { 355 return tfp410_init(&pdev->dev, false); 356 } 357 358 static int tfp410_remove(struct platform_device *pdev) 359 { 360 tfp410_fini(&pdev->dev); 361 362 return 0; 363 } 364 365 static const struct of_device_id tfp410_match[] = { 366 { .compatible = "ti,tfp410" }, 367 {}, 368 }; 369 MODULE_DEVICE_TABLE(of, tfp410_match); 370 371 static struct platform_driver tfp410_platform_driver = { 372 .probe = tfp410_probe, 373 .remove = tfp410_remove, 374 .driver = { 375 .name = "tfp410-bridge", 376 .of_match_table = tfp410_match, 377 }, 378 }; 379 380 #if IS_ENABLED(CONFIG_I2C) 381 /* There is currently no i2c functionality. */ 382 static int tfp410_i2c_probe(struct i2c_client *client, 383 const struct i2c_device_id *id) 384 { 385 int reg; 386 387 if (!client->dev.of_node || 388 of_property_read_u32(client->dev.of_node, "reg", ®)) { 389 dev_err(&client->dev, 390 "Can't get i2c reg property from device-tree\n"); 391 return -ENXIO; 392 } 393 394 return tfp410_init(&client->dev, true); 395 } 396 397 static void tfp410_i2c_remove(struct i2c_client *client) 398 { 399 tfp410_fini(&client->dev); 400 } 401 402 static const struct i2c_device_id tfp410_i2c_ids[] = { 403 { "tfp410", 0 }, 404 { } 405 }; 406 MODULE_DEVICE_TABLE(i2c, tfp410_i2c_ids); 407 408 static struct i2c_driver tfp410_i2c_driver = { 409 .driver = { 410 .name = "tfp410", 411 .of_match_table = of_match_ptr(tfp410_match), 412 }, 413 .id_table = tfp410_i2c_ids, 414 .probe = tfp410_i2c_probe, 415 .remove = tfp410_i2c_remove, 416 }; 417 #endif /* IS_ENABLED(CONFIG_I2C) */ 418 419 static struct { 420 uint i2c:1; 421 uint platform:1; 422 } tfp410_registered_driver; 423 424 static int __init tfp410_module_init(void) 425 { 426 int ret; 427 428 #if IS_ENABLED(CONFIG_I2C) 429 ret = i2c_add_driver(&tfp410_i2c_driver); 430 if (ret) 431 pr_err("%s: registering i2c driver failed: %d", 432 __func__, ret); 433 else 434 tfp410_registered_driver.i2c = 1; 435 #endif 436 437 ret = platform_driver_register(&tfp410_platform_driver); 438 if (ret) 439 pr_err("%s: registering platform driver failed: %d", 440 __func__, ret); 441 else 442 tfp410_registered_driver.platform = 1; 443 444 if (tfp410_registered_driver.i2c || 445 tfp410_registered_driver.platform) 446 return 0; 447 448 return ret; 449 } 450 module_init(tfp410_module_init); 451 452 static void __exit tfp410_module_exit(void) 453 { 454 #if IS_ENABLED(CONFIG_I2C) 455 if (tfp410_registered_driver.i2c) 456 i2c_del_driver(&tfp410_i2c_driver); 457 #endif 458 if (tfp410_registered_driver.platform) 459 platform_driver_unregister(&tfp410_platform_driver); 460 } 461 module_exit(tfp410_module_exit); 462 463 MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>"); 464 MODULE_DESCRIPTION("TI TFP410 DVI bridge driver"); 465 MODULE_LICENSE("GPL"); 466