1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2015-2016 Free Electrons 4 * Copyright (C) 2015-2016 NextThing Co 5 * 6 * Maxime Ripard <maxime.ripard@free-electrons.com> 7 */ 8 9 #include <linux/gpio/consumer.h> 10 #include <linux/module.h> 11 #include <linux/of_device.h> 12 #include <linux/of_graph.h> 13 #include <linux/regulator/consumer.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_print.h> 19 #include <drm/drm_probe_helper.h> 20 21 struct simple_bridge_info { 22 const struct drm_bridge_timings *timings; 23 unsigned int connector_type; 24 }; 25 26 struct simple_bridge { 27 struct drm_bridge bridge; 28 struct drm_connector connector; 29 30 const struct simple_bridge_info *info; 31 32 struct i2c_adapter *ddc; 33 struct regulator *vdd; 34 struct gpio_desc *enable; 35 }; 36 37 static inline struct simple_bridge * 38 drm_bridge_to_simple_bridge(struct drm_bridge *bridge) 39 { 40 return container_of(bridge, struct simple_bridge, bridge); 41 } 42 43 static inline struct simple_bridge * 44 drm_connector_to_simple_bridge(struct drm_connector *connector) 45 { 46 return container_of(connector, struct simple_bridge, connector); 47 } 48 49 static int simple_bridge_get_modes(struct drm_connector *connector) 50 { 51 struct simple_bridge *sbridge = drm_connector_to_simple_bridge(connector); 52 struct edid *edid; 53 int ret; 54 55 if (!sbridge->ddc) 56 goto fallback; 57 58 edid = drm_get_edid(connector, sbridge->ddc); 59 if (!edid) { 60 DRM_INFO("EDID readout failed, falling back to standard modes\n"); 61 goto fallback; 62 } 63 64 drm_connector_update_edid_property(connector, edid); 65 ret = drm_add_edid_modes(connector, edid); 66 kfree(edid); 67 return ret; 68 69 fallback: 70 /* 71 * In case we cannot retrieve the EDIDs (broken or missing i2c 72 * bus), fallback on the XGA standards 73 */ 74 ret = drm_add_modes_noedid(connector, 1920, 1200); 75 76 /* And prefer a mode pretty much anyone can handle */ 77 drm_set_preferred_mode(connector, 1024, 768); 78 79 return ret; 80 } 81 82 static const struct drm_connector_helper_funcs simple_bridge_con_helper_funcs = { 83 .get_modes = simple_bridge_get_modes, 84 }; 85 86 static enum drm_connector_status 87 simple_bridge_connector_detect(struct drm_connector *connector, bool force) 88 { 89 struct simple_bridge *sbridge = drm_connector_to_simple_bridge(connector); 90 91 /* 92 * Even if we have an I2C bus, we can't assume that the cable 93 * is disconnected if drm_probe_ddc fails. Some cables don't 94 * wire the DDC pins, or the I2C bus might not be working at 95 * all. 96 */ 97 if (sbridge->ddc && drm_probe_ddc(sbridge->ddc)) 98 return connector_status_connected; 99 100 return connector_status_unknown; 101 } 102 103 static const struct drm_connector_funcs simple_bridge_con_funcs = { 104 .detect = simple_bridge_connector_detect, 105 .fill_modes = drm_helper_probe_single_connector_modes, 106 .destroy = drm_connector_cleanup, 107 .reset = drm_atomic_helper_connector_reset, 108 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 109 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 110 }; 111 112 static int simple_bridge_attach(struct drm_bridge *bridge, 113 enum drm_bridge_attach_flags flags) 114 { 115 struct simple_bridge *sbridge = drm_bridge_to_simple_bridge(bridge); 116 int ret; 117 118 if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR) { 119 DRM_ERROR("Fix bridge driver to make connector optional!"); 120 return -EINVAL; 121 } 122 123 if (!bridge->encoder) { 124 DRM_ERROR("Missing encoder\n"); 125 return -ENODEV; 126 } 127 128 drm_connector_helper_add(&sbridge->connector, 129 &simple_bridge_con_helper_funcs); 130 ret = drm_connector_init_with_ddc(bridge->dev, &sbridge->connector, 131 &simple_bridge_con_funcs, 132 sbridge->info->connector_type, 133 sbridge->ddc); 134 if (ret) { 135 DRM_ERROR("Failed to initialize connector\n"); 136 return ret; 137 } 138 139 drm_connector_attach_encoder(&sbridge->connector, 140 bridge->encoder); 141 142 return 0; 143 } 144 145 static void simple_bridge_enable(struct drm_bridge *bridge) 146 { 147 struct simple_bridge *sbridge = drm_bridge_to_simple_bridge(bridge); 148 int ret; 149 150 if (sbridge->vdd) { 151 ret = regulator_enable(sbridge->vdd); 152 if (ret) 153 DRM_ERROR("Failed to enable vdd regulator: %d\n", ret); 154 } 155 156 gpiod_set_value_cansleep(sbridge->enable, 1); 157 } 158 159 static void simple_bridge_disable(struct drm_bridge *bridge) 160 { 161 struct simple_bridge *sbridge = drm_bridge_to_simple_bridge(bridge); 162 163 gpiod_set_value_cansleep(sbridge->enable, 0); 164 165 if (sbridge->vdd) 166 regulator_disable(sbridge->vdd); 167 } 168 169 static const struct drm_bridge_funcs simple_bridge_bridge_funcs = { 170 .attach = simple_bridge_attach, 171 .enable = simple_bridge_enable, 172 .disable = simple_bridge_disable, 173 }; 174 175 static struct i2c_adapter *simple_bridge_retrieve_ddc(struct device *dev) 176 { 177 struct device_node *phandle, *remote; 178 struct i2c_adapter *ddc; 179 180 remote = of_graph_get_remote_node(dev->of_node, 1, -1); 181 if (!remote) 182 return ERR_PTR(-EINVAL); 183 184 phandle = of_parse_phandle(remote, "ddc-i2c-bus", 0); 185 of_node_put(remote); 186 if (!phandle) 187 return ERR_PTR(-ENODEV); 188 189 ddc = of_get_i2c_adapter_by_node(phandle); 190 of_node_put(phandle); 191 if (!ddc) 192 return ERR_PTR(-EPROBE_DEFER); 193 194 return ddc; 195 } 196 197 static int simple_bridge_probe(struct platform_device *pdev) 198 { 199 struct simple_bridge *sbridge; 200 201 sbridge = devm_kzalloc(&pdev->dev, sizeof(*sbridge), GFP_KERNEL); 202 if (!sbridge) 203 return -ENOMEM; 204 platform_set_drvdata(pdev, sbridge); 205 206 sbridge->info = of_device_get_match_data(&pdev->dev); 207 208 sbridge->vdd = devm_regulator_get_optional(&pdev->dev, "vdd"); 209 if (IS_ERR(sbridge->vdd)) { 210 int ret = PTR_ERR(sbridge->vdd); 211 if (ret == -EPROBE_DEFER) 212 return -EPROBE_DEFER; 213 sbridge->vdd = NULL; 214 dev_dbg(&pdev->dev, "No vdd regulator found: %d\n", ret); 215 } 216 217 sbridge->enable = devm_gpiod_get_optional(&pdev->dev, "enable", 218 GPIOD_OUT_LOW); 219 if (IS_ERR(sbridge->enable)) { 220 if (PTR_ERR(sbridge->enable) != -EPROBE_DEFER) 221 dev_err(&pdev->dev, "Unable to retrieve enable GPIO\n"); 222 return PTR_ERR(sbridge->enable); 223 } 224 225 sbridge->ddc = simple_bridge_retrieve_ddc(&pdev->dev); 226 if (IS_ERR(sbridge->ddc)) { 227 if (PTR_ERR(sbridge->ddc) == -ENODEV) { 228 dev_dbg(&pdev->dev, 229 "No i2c bus specified. Disabling EDID readout\n"); 230 sbridge->ddc = NULL; 231 } else { 232 dev_err(&pdev->dev, "Couldn't retrieve i2c bus\n"); 233 return PTR_ERR(sbridge->ddc); 234 } 235 } 236 237 sbridge->bridge.funcs = &simple_bridge_bridge_funcs; 238 sbridge->bridge.of_node = pdev->dev.of_node; 239 sbridge->bridge.timings = sbridge->info->timings; 240 241 drm_bridge_add(&sbridge->bridge); 242 243 return 0; 244 } 245 246 static int simple_bridge_remove(struct platform_device *pdev) 247 { 248 struct simple_bridge *sbridge = platform_get_drvdata(pdev); 249 250 drm_bridge_remove(&sbridge->bridge); 251 252 if (sbridge->ddc) 253 i2c_put_adapter(sbridge->ddc); 254 255 return 0; 256 } 257 258 /* 259 * We assume the ADV7123 DAC is the "default" for historical reasons 260 * Information taken from the ADV7123 datasheet, revision D. 261 * NOTE: the ADV7123EP seems to have other timings and need a new timings 262 * set if used. 263 */ 264 static const struct drm_bridge_timings default_bridge_timings = { 265 /* Timing specifications, datasheet page 7 */ 266 .input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE, 267 .setup_time_ps = 500, 268 .hold_time_ps = 1500, 269 }; 270 271 /* 272 * Information taken from the THS8134, THS8134A, THS8134B datasheet named 273 * "SLVS205D", dated May 1990, revised March 2000. 274 */ 275 static const struct drm_bridge_timings ti_ths8134_bridge_timings = { 276 /* From timing diagram, datasheet page 9 */ 277 .input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE, 278 /* From datasheet, page 12 */ 279 .setup_time_ps = 3000, 280 /* I guess this means latched input */ 281 .hold_time_ps = 0, 282 }; 283 284 /* 285 * Information taken from the THS8135 datasheet named "SLAS343B", dated 286 * May 2001, revised April 2013. 287 */ 288 static const struct drm_bridge_timings ti_ths8135_bridge_timings = { 289 /* From timing diagram, datasheet page 14 */ 290 .input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE, 291 /* From datasheet, page 16 */ 292 .setup_time_ps = 2000, 293 .hold_time_ps = 500, 294 }; 295 296 static const struct of_device_id simple_bridge_match[] = { 297 { 298 .compatible = "dumb-vga-dac", 299 .data = &(const struct simple_bridge_info) { 300 .connector_type = DRM_MODE_CONNECTOR_VGA, 301 }, 302 }, { 303 .compatible = "adi,adv7123", 304 .data = &(const struct simple_bridge_info) { 305 .timings = &default_bridge_timings, 306 .connector_type = DRM_MODE_CONNECTOR_VGA, 307 }, 308 }, { 309 .compatible = "ti,opa362", 310 .data = &(const struct simple_bridge_info) { 311 .connector_type = DRM_MODE_CONNECTOR_Composite, 312 }, 313 }, { 314 .compatible = "ti,ths8135", 315 .data = &(const struct simple_bridge_info) { 316 .timings = &ti_ths8135_bridge_timings, 317 .connector_type = DRM_MODE_CONNECTOR_VGA, 318 }, 319 }, { 320 .compatible = "ti,ths8134", 321 .data = &(const struct simple_bridge_info) { 322 .timings = &ti_ths8134_bridge_timings, 323 .connector_type = DRM_MODE_CONNECTOR_VGA, 324 }, 325 }, 326 {}, 327 }; 328 MODULE_DEVICE_TABLE(of, simple_bridge_match); 329 330 static struct platform_driver simple_bridge_driver = { 331 .probe = simple_bridge_probe, 332 .remove = simple_bridge_remove, 333 .driver = { 334 .name = "simple-bridge", 335 .of_match_table = simple_bridge_match, 336 }, 337 }; 338 module_platform_driver(simple_bridge_driver); 339 340 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>"); 341 MODULE_DESCRIPTION("Simple DRM bridge driver"); 342 MODULE_LICENSE("GPL"); 343