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_edid.h> 19 #include <drm/drm_print.h> 20 #include <drm/drm_probe_helper.h> 21 22 struct simple_bridge_info { 23 const struct drm_bridge_timings *timings; 24 unsigned int connector_type; 25 }; 26 27 struct simple_bridge { 28 struct drm_bridge bridge; 29 struct drm_connector connector; 30 31 const struct simple_bridge_info *info; 32 33 struct drm_bridge *next_bridge; 34 struct regulator *vdd; 35 struct gpio_desc *enable; 36 }; 37 38 static inline struct simple_bridge * 39 drm_bridge_to_simple_bridge(struct drm_bridge *bridge) 40 { 41 return container_of(bridge, struct simple_bridge, bridge); 42 } 43 44 static inline struct simple_bridge * 45 drm_connector_to_simple_bridge(struct drm_connector *connector) 46 { 47 return container_of(connector, struct simple_bridge, connector); 48 } 49 50 static int simple_bridge_get_modes(struct drm_connector *connector) 51 { 52 struct simple_bridge *sbridge = drm_connector_to_simple_bridge(connector); 53 struct edid *edid; 54 int ret; 55 56 if (sbridge->next_bridge->ops & DRM_BRIDGE_OP_EDID) { 57 edid = drm_bridge_get_edid(sbridge->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 * In case we cannot retrieve the EDIDs (missing or broken DDC 67 * bus from the next bridge), fallback on the XGA standards and 68 * prefer a mode pretty much anyone can handle. 69 */ 70 ret = drm_add_modes_noedid(connector, 1920, 1200); 71 drm_set_preferred_mode(connector, 1024, 768); 72 return ret; 73 } 74 75 drm_connector_update_edid_property(connector, edid); 76 ret = drm_add_edid_modes(connector, edid); 77 kfree(edid); 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 return drm_bridge_detect(sbridge->next_bridge); 92 } 93 94 static const struct drm_connector_funcs simple_bridge_con_funcs = { 95 .detect = simple_bridge_connector_detect, 96 .fill_modes = drm_helper_probe_single_connector_modes, 97 .destroy = drm_connector_cleanup, 98 .reset = drm_atomic_helper_connector_reset, 99 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 100 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 101 }; 102 103 static int simple_bridge_attach(struct drm_bridge *bridge, 104 enum drm_bridge_attach_flags flags) 105 { 106 struct simple_bridge *sbridge = drm_bridge_to_simple_bridge(bridge); 107 int ret; 108 109 ret = drm_bridge_attach(bridge->encoder, sbridge->next_bridge, bridge, 110 DRM_BRIDGE_ATTACH_NO_CONNECTOR); 111 if (ret < 0) 112 return ret; 113 114 if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR) 115 return 0; 116 117 if (!bridge->encoder) { 118 DRM_ERROR("Missing encoder\n"); 119 return -ENODEV; 120 } 121 122 drm_connector_helper_add(&sbridge->connector, 123 &simple_bridge_con_helper_funcs); 124 ret = drm_connector_init_with_ddc(bridge->dev, &sbridge->connector, 125 &simple_bridge_con_funcs, 126 sbridge->info->connector_type, 127 sbridge->next_bridge->ddc); 128 if (ret) { 129 DRM_ERROR("Failed to initialize connector\n"); 130 return ret; 131 } 132 133 drm_connector_attach_encoder(&sbridge->connector, bridge->encoder); 134 135 return 0; 136 } 137 138 static void simple_bridge_enable(struct drm_bridge *bridge) 139 { 140 struct simple_bridge *sbridge = drm_bridge_to_simple_bridge(bridge); 141 int ret; 142 143 if (sbridge->vdd) { 144 ret = regulator_enable(sbridge->vdd); 145 if (ret) 146 DRM_ERROR("Failed to enable vdd regulator: %d\n", ret); 147 } 148 149 gpiod_set_value_cansleep(sbridge->enable, 1); 150 } 151 152 static void simple_bridge_disable(struct drm_bridge *bridge) 153 { 154 struct simple_bridge *sbridge = drm_bridge_to_simple_bridge(bridge); 155 156 gpiod_set_value_cansleep(sbridge->enable, 0); 157 158 if (sbridge->vdd) 159 regulator_disable(sbridge->vdd); 160 } 161 162 static const struct drm_bridge_funcs simple_bridge_bridge_funcs = { 163 .attach = simple_bridge_attach, 164 .enable = simple_bridge_enable, 165 .disable = simple_bridge_disable, 166 }; 167 168 static int simple_bridge_probe(struct platform_device *pdev) 169 { 170 struct simple_bridge *sbridge; 171 struct device_node *remote; 172 173 sbridge = devm_kzalloc(&pdev->dev, sizeof(*sbridge), GFP_KERNEL); 174 if (!sbridge) 175 return -ENOMEM; 176 platform_set_drvdata(pdev, sbridge); 177 178 sbridge->info = of_device_get_match_data(&pdev->dev); 179 180 /* Get the next bridge in the pipeline. */ 181 remote = of_graph_get_remote_node(pdev->dev.of_node, 1, -1); 182 if (!remote) 183 return -EINVAL; 184 185 sbridge->next_bridge = of_drm_find_bridge(remote); 186 of_node_put(remote); 187 188 if (!sbridge->next_bridge) { 189 dev_dbg(&pdev->dev, "Next bridge not found, deferring probe\n"); 190 return -EPROBE_DEFER; 191 } 192 193 /* Get the regulator and GPIO resources. */ 194 sbridge->vdd = devm_regulator_get_optional(&pdev->dev, "vdd"); 195 if (IS_ERR(sbridge->vdd)) { 196 int ret = PTR_ERR(sbridge->vdd); 197 if (ret == -EPROBE_DEFER) 198 return -EPROBE_DEFER; 199 sbridge->vdd = NULL; 200 dev_dbg(&pdev->dev, "No vdd regulator found: %d\n", ret); 201 } 202 203 sbridge->enable = devm_gpiod_get_optional(&pdev->dev, "enable", 204 GPIOD_OUT_LOW); 205 if (IS_ERR(sbridge->enable)) { 206 if (PTR_ERR(sbridge->enable) != -EPROBE_DEFER) 207 dev_err(&pdev->dev, "Unable to retrieve enable GPIO\n"); 208 return PTR_ERR(sbridge->enable); 209 } 210 211 /* Register the bridge. */ 212 sbridge->bridge.funcs = &simple_bridge_bridge_funcs; 213 sbridge->bridge.of_node = pdev->dev.of_node; 214 sbridge->bridge.timings = sbridge->info->timings; 215 216 drm_bridge_add(&sbridge->bridge); 217 218 return 0; 219 } 220 221 static int simple_bridge_remove(struct platform_device *pdev) 222 { 223 struct simple_bridge *sbridge = platform_get_drvdata(pdev); 224 225 drm_bridge_remove(&sbridge->bridge); 226 227 return 0; 228 } 229 230 /* 231 * We assume the ADV7123 DAC is the "default" for historical reasons 232 * Information taken from the ADV7123 datasheet, revision D. 233 * NOTE: the ADV7123EP seems to have other timings and need a new timings 234 * set if used. 235 */ 236 static const struct drm_bridge_timings default_bridge_timings = { 237 /* Timing specifications, datasheet page 7 */ 238 .input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE, 239 .setup_time_ps = 500, 240 .hold_time_ps = 1500, 241 }; 242 243 /* 244 * Information taken from the THS8134, THS8134A, THS8134B datasheet named 245 * "SLVS205D", dated May 1990, revised March 2000. 246 */ 247 static const struct drm_bridge_timings ti_ths8134_bridge_timings = { 248 /* From timing diagram, datasheet page 9 */ 249 .input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE, 250 /* From datasheet, page 12 */ 251 .setup_time_ps = 3000, 252 /* I guess this means latched input */ 253 .hold_time_ps = 0, 254 }; 255 256 /* 257 * Information taken from the THS8135 datasheet named "SLAS343B", dated 258 * May 2001, revised April 2013. 259 */ 260 static const struct drm_bridge_timings ti_ths8135_bridge_timings = { 261 /* From timing diagram, datasheet page 14 */ 262 .input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE, 263 /* From datasheet, page 16 */ 264 .setup_time_ps = 2000, 265 .hold_time_ps = 500, 266 }; 267 268 static const struct of_device_id simple_bridge_match[] = { 269 { 270 .compatible = "dumb-vga-dac", 271 .data = &(const struct simple_bridge_info) { 272 .connector_type = DRM_MODE_CONNECTOR_VGA, 273 }, 274 }, { 275 .compatible = "adi,adv7123", 276 .data = &(const struct simple_bridge_info) { 277 .timings = &default_bridge_timings, 278 .connector_type = DRM_MODE_CONNECTOR_VGA, 279 }, 280 }, { 281 .compatible = "ti,opa362", 282 .data = &(const struct simple_bridge_info) { 283 .connector_type = DRM_MODE_CONNECTOR_Composite, 284 }, 285 }, { 286 .compatible = "ti,ths8135", 287 .data = &(const struct simple_bridge_info) { 288 .timings = &ti_ths8135_bridge_timings, 289 .connector_type = DRM_MODE_CONNECTOR_VGA, 290 }, 291 }, { 292 .compatible = "ti,ths8134", 293 .data = &(const struct simple_bridge_info) { 294 .timings = &ti_ths8134_bridge_timings, 295 .connector_type = DRM_MODE_CONNECTOR_VGA, 296 }, 297 }, 298 {}, 299 }; 300 MODULE_DEVICE_TABLE(of, simple_bridge_match); 301 302 static struct platform_driver simple_bridge_driver = { 303 .probe = simple_bridge_probe, 304 .remove = simple_bridge_remove, 305 .driver = { 306 .name = "simple-bridge", 307 .of_match_table = simple_bridge_match, 308 }, 309 }; 310 module_platform_driver(simple_bridge_driver); 311 312 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>"); 313 MODULE_DESCRIPTION("Simple DRM bridge driver"); 314 MODULE_LICENSE("GPL"); 315