| 1 | |
| 2 | |
| 3 | * Copyright (C) 2015-2016 Free Electrons
|
| 4 | * Copyright (C) 2015-2016 NextThing Co
|
| 5 | *
|
| 6 | * Maxime Ripard <[email protected]>
|
| 7 |
|
| 8 |
|
| 9 | #include <linux/gpio/consumer.h>
|
| 10 | #include <linux/module.h>
|
| 11 | #include <linux/of.h>
|
| 12 | #include <linux/of_graph.h>
|
| 13 | #include <linux/platform_device.h>
|
| 14 | #include <linux/regulator/consumer.h>
|
| 15 |
|
| 16 | #include <drm/drm_atomic_helper.h>
|
| 17 | #include <drm/drm_bridge.h>
|
| 18 | #include <drm/drm_crtc.h>
|
| 19 | #include <drm/drm_edid.h>
|
| 20 | #include <drm/drm_print.h>
|
| 21 | #include <drm/drm_probe_helper.h>
|
| 22 |
|
| 23 | struct simple_bridge_info {
|
| 24 | const struct drm_bridge_timings *timings;
|
| 25 | unsigned int connector_type;
|
| 26 | };
|
| 27 |
|
| 28 | struct simple_bridge {
|
| 29 | struct drm_bridge bridge;
|
| 30 | struct drm_connector connector;
|
| 31 |
|
| 32 | const struct simple_bridge_info *info;
|
| 33 |
|
| 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 | const struct drm_edid *drm_edid;
|
| 54 | int ret;
|
| 55 |
|
| 56 | if (sbridge->bridge.next_bridge->ops & DRM_BRIDGE_OP_EDID) {
|
| 57 | drm_edid = drm_bridge_edid_read(sbridge->bridge.next_bridge, connector);
|
| 58 | if (!drm_edid)
|
| 59 | DRM_INFO("EDID read failed. Fallback to standard modes\n");
|
| 60 | } else {
|
| 61 | drm_edid = NULL;
|
| 62 | }
|
| 63 |
|
| 64 | drm_edid_connector_update(connector, drm_edid);
|
| 65 |
|
| 66 | if (!drm_edid) {
|
| 67 | |
| 68 | * In case we cannot retrieve the EDIDs (missing or broken DDC
|
| 69 | * bus from the next bridge), fallback on the XGA standards and
|
| 70 | * prefer a mode pretty much anyone can handle.
|
| 71 |
|
| 72 | ret = drm_add_modes_noedid(connector, 1920, 1200);
|
| 73 | drm_set_preferred_mode(connector, 1024, 768);
|
| 74 | return ret;
|
| 75 | }
|
| 76 |
|
| 77 | ret = drm_edid_connector_add_modes(connector);
|
| 78 | drm_edid_free(drm_edid);
|
| 79 |
|
| 80 | return ret;
|
| 81 | }
|
| 82 |
|
| 83 | static const struct drm_connector_helper_funcs simple_bridge_con_helper_funcs = {
|
| 84 | .get_modes = simple_bridge_get_modes,
|
| 85 | };
|
| 86 |
|
| 87 | static enum drm_connector_status
|
| 88 | simple_bridge_connector_detect(struct drm_connector *connector, bool force)
|
| 89 | {
|
| 90 | struct simple_bridge *sbridge = drm_connector_to_simple_bridge(connector);
|
| 91 |
|
| 92 | return drm_bridge_detect(sbridge->bridge.next_bridge, connector);
|
| 93 | }
|
| 94 |
|
| 95 | static const struct drm_connector_funcs simple_bridge_con_funcs = {
|
| 96 | .detect = simple_bridge_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 int simple_bridge_attach(struct drm_bridge *bridge,
|
| 105 | struct drm_encoder *encoder,
|
| 106 | enum drm_bridge_attach_flags flags)
|
| 107 | {
|
| 108 | struct simple_bridge *sbridge = drm_bridge_to_simple_bridge(bridge);
|
| 109 | int ret;
|
| 110 |
|
| 111 | ret = drm_bridge_attach(encoder, sbridge->bridge.next_bridge, bridge,
|
| 112 | DRM_BRIDGE_ATTACH_NO_CONNECTOR);
|
| 113 | if (ret < 0)
|
| 114 | return ret;
|
| 115 |
|
| 116 | if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
|
| 117 | return 0;
|
| 118 |
|
| 119 | drm_connector_helper_add(&sbridge->connector,
|
| 120 | &simple_bridge_con_helper_funcs);
|
| 121 | ret = drm_connector_init_with_ddc(bridge->dev, &sbridge->connector,
|
| 122 | &simple_bridge_con_funcs,
|
| 123 | sbridge->info->connector_type,
|
| 124 | sbridge->bridge.next_bridge->ddc);
|
| 125 | if (ret) {
|
| 126 | DRM_ERROR("Failed to initialize connector\n");
|
| 127 | return ret;
|
| 128 | }
|
| 129 |
|
| 130 | drm_connector_attach_encoder(&sbridge->connector, encoder);
|
| 131 |
|
| 132 | return 0;
|
| 133 | }
|
| 134 |
|
| 135 | static void simple_bridge_enable(struct drm_bridge *bridge,
|
| 136 | struct drm_atomic_commit *commit)
|
| 137 | {
|
| 138 | struct simple_bridge *sbridge = drm_bridge_to_simple_bridge(bridge);
|
| 139 | int ret;
|
| 140 |
|
| 141 | if (sbridge->vdd) {
|
| 142 | ret = regulator_enable(sbridge->vdd);
|
| 143 | if (ret)
|
| 144 | DRM_ERROR("Failed to enable vdd regulator: %d\n", ret);
|
| 145 | }
|
| 146 |
|
| 147 | gpiod_set_value_cansleep(sbridge->enable, 1);
|
| 148 | }
|
| 149 |
|
| 150 | static void simple_bridge_disable(struct drm_bridge *bridge,
|
| 151 | struct drm_atomic_commit *commit)
|
| 152 | {
|
| 153 | struct simple_bridge *sbridge = drm_bridge_to_simple_bridge(bridge);
|
| 154 |
|
| 155 | gpiod_set_value_cansleep(sbridge->enable, 0);
|
| 156 |
|
| 157 | if (sbridge->vdd)
|
| 158 | regulator_disable(sbridge->vdd);
|
| 159 | }
|
| 160 |
|
| 161 | static const struct drm_bridge_funcs simple_bridge_bridge_funcs = {
|
| 162 | .atomic_create_state = drm_atomic_helper_bridge_create_state,
|
| 163 | .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
|
| 164 | .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
|
| 165 | .attach = simple_bridge_attach,
|
| 166 | .atomic_enable = simple_bridge_enable,
|
| 167 | .atomic_disable = simple_bridge_disable,
|
| 168 | };
|
| 169 |
|
| 170 | static int simple_bridge_probe(struct platform_device *pdev)
|
| 171 | {
|
| 172 | struct simple_bridge *sbridge;
|
| 173 | struct device_node *remote;
|
| 174 |
|
| 175 | sbridge = devm_drm_bridge_alloc(&pdev->dev, struct simple_bridge,
|
| 176 | bridge, &simple_bridge_bridge_funcs);
|
| 177 | if (IS_ERR(sbridge))
|
| 178 | return PTR_ERR(sbridge);
|
| 179 |
|
| 180 | sbridge->info = of_device_get_match_data(&pdev->dev);
|
| 181 |
|
| 182 |
|
| 183 | remote = of_graph_get_remote_node(pdev->dev.of_node, 1, -1);
|
| 184 | if (!remote)
|
| 185 | return -EINVAL;
|
| 186 |
|
| 187 | sbridge->bridge.next_bridge = of_drm_find_and_get_bridge(remote);
|
| 188 | of_node_put(remote);
|
| 189 |
|
| 190 | if (!sbridge->bridge.next_bridge) {
|
| 191 | dev_dbg(&pdev->dev, "Next bridge not found, deferring probe\n");
|
| 192 | return -EPROBE_DEFER;
|
| 193 | }
|
| 194 |
|
| 195 |
|
| 196 | sbridge->vdd = devm_regulator_get_optional(&pdev->dev, "vdd");
|
| 197 | if (IS_ERR(sbridge->vdd)) {
|
| 198 | int ret = PTR_ERR(sbridge->vdd);
|
| 199 | if (ret == -EPROBE_DEFER)
|
| 200 | return -EPROBE_DEFER;
|
| 201 | sbridge->vdd = NULL;
|
| 202 | dev_dbg(&pdev->dev, "No vdd regulator found: %d\n", ret);
|
| 203 | }
|
| 204 |
|
| 205 | sbridge->enable = devm_gpiod_get_optional(&pdev->dev, "enable",
|
| 206 | GPIOD_OUT_LOW);
|
| 207 | if (IS_ERR(sbridge->enable))
|
| 208 | return dev_err_probe(&pdev->dev, PTR_ERR(sbridge->enable),
|
| 209 | "Unable to retrieve enable GPIO\n");
|
| 210 |
|
| 211 |
|
| 212 | sbridge->bridge.of_node = pdev->dev.of_node;
|
| 213 | sbridge->bridge.timings = sbridge->info->timings;
|
| 214 |
|
| 215 | return devm_drm_bridge_add(&pdev->dev, &sbridge->bridge);
|
| 216 | }
|
| 217 |
|
| 218 | |
| 219 | * We assume the ADV7123 DAC is the "default" for historical reasons
|
| 220 | * Information taken from the ADV7123 datasheet, revision D.
|
| 221 | * NOTE: the ADV7123EP seems to have other timings and need a new timings
|
| 222 | * set if used.
|
| 223 |
|
| 224 | static const struct drm_bridge_timings default_bridge_timings = {
|
| 225 |
|
| 226 | .input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE,
|
| 227 | .setup_time_ps = 500,
|
| 228 | .hold_time_ps = 1500,
|
| 229 | };
|
| 230 |
|
| 231 | |
| 232 | * Information taken from the THS8134, THS8134A, THS8134B datasheet named
|
| 233 | * "SLVS205D", dated May 1990, revised March 2000.
|
| 234 |
|
| 235 | static const struct drm_bridge_timings ti_ths8134_bridge_timings = {
|
| 236 |
|
| 237 | .input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE,
|
| 238 |
|
| 239 | .setup_time_ps = 3000,
|
| 240 |
|
| 241 | .hold_time_ps = 0,
|
| 242 | };
|
| 243 |
|
| 244 | |
| 245 | * Information taken from the THS8135 datasheet named "SLAS343B", dated
|
| 246 | * May 2001, revised April 2013.
|
| 247 |
|
| 248 | static const struct drm_bridge_timings ti_ths8135_bridge_timings = {
|
| 249 |
|
| 250 | .input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE,
|
| 251 |
|
| 252 | .setup_time_ps = 2000,
|
| 253 | .hold_time_ps = 500,
|
| 254 | };
|
| 255 |
|
| 256 | static const struct of_device_id simple_bridge_match[] = {
|
| 257 | {
|
| 258 | .compatible = "dumb-vga-dac",
|
| 259 | .data = &(const struct simple_bridge_info) {
|
| 260 | .connector_type = DRM_MODE_CONNECTOR_VGA,
|
| 261 | },
|
| 262 | }, {
|
| 263 | .compatible = "adi,adv7123",
|
| 264 | .data = &(const struct simple_bridge_info) {
|
| 265 | .timings = &default_bridge_timings,
|
| 266 | .connector_type = DRM_MODE_CONNECTOR_VGA,
|
| 267 | },
|
| 268 | }, {
|
| 269 | .compatible = "algoltek,ag6311",
|
| 270 | .data = &(const struct simple_bridge_info) {
|
| 271 | .connector_type = DRM_MODE_CONNECTOR_HDMIA,
|
| 272 | },
|
| 273 | }, {
|
| 274 | .compatible = "asl-tek,cs5263",
|
| 275 | .data = &(const struct simple_bridge_info) {
|
| 276 | .connector_type = DRM_MODE_CONNECTOR_HDMIA,
|
| 277 | },
|
| 278 | }, {
|
| 279 | .compatible = "mstar,tsumu88adt3-lf-1",
|
| 280 | .data = &(const struct simple_bridge_info) {
|
| 281 | .connector_type = DRM_MODE_CONNECTOR_HDMIA,
|
| 282 | },
|
| 283 | }, {
|
| 284 | .compatible = "parade,ps185hdm",
|
| 285 | .data = &(const struct simple_bridge_info) {
|
| 286 | .connector_type = DRM_MODE_CONNECTOR_HDMIA,
|
| 287 | },
|
| 288 | }, {
|
| 289 | .compatible = "radxa,ra620",
|
| 290 | .data = &(const struct simple_bridge_info) {
|
| 291 | .connector_type = DRM_MODE_CONNECTOR_HDMIA,
|
| 292 | },
|
| 293 | }, {
|
| 294 | .compatible = "realtek,rtd2171",
|
| 295 | .data = &(const struct simple_bridge_info) {
|
| 296 | .connector_type = DRM_MODE_CONNECTOR_HDMIA,
|
| 297 | },
|
| 298 | }, {
|
| 299 | .compatible = "ti,opa362",
|
| 300 | .data = &(const struct simple_bridge_info) {
|
| 301 | .connector_type = DRM_MODE_CONNECTOR_Composite,
|
| 302 | },
|
| 303 | }, {
|
| 304 | .compatible = "ti,ths8135",
|
| 305 | .data = &(const struct simple_bridge_info) {
|
| 306 | .timings = &ti_ths8135_bridge_timings,
|
| 307 | .connector_type = DRM_MODE_CONNECTOR_VGA,
|
| 308 | },
|
| 309 | }, {
|
| 310 | .compatible = "ti,ths8134",
|
| 311 | .data = &(const struct simple_bridge_info) {
|
| 312 | .timings = &ti_ths8134_bridge_timings,
|
| 313 | .connector_type = DRM_MODE_CONNECTOR_VGA,
|
| 314 | },
|
| 315 | },
|
| 316 | {},
|
| 317 | };
|
| 318 | MODULE_DEVICE_TABLE(of, simple_bridge_match);
|
| 319 |
|
| 320 | static struct platform_driver simple_bridge_driver = {
|
| 321 | .probe = simple_bridge_probe,
|
| 322 | .driver = {
|
| 323 | .name = "simple-bridge",
|
| 324 | .of_match_table = simple_bridge_match,
|
| 325 | },
|
| 326 | };
|
| 327 | module_platform_driver(simple_bridge_driver);
|
| 328 |
|
| 329 | MODULE_AUTHOR("Maxime Ripard <[email protected]>");
|
| 330 | MODULE_DESCRIPTION("Simple DRM bridge driver");
|
| 331 | MODULE_LICENSE("GPL");
|