Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2015-2016 Free Electrons |
| 3 | * Copyright (C) 2015-2016 NextThing Co |
| 4 | * |
| 5 | * Maxime Ripard <maxime.ripard@free-electrons.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of |
| 10 | * the License, or (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/of_device.h> |
| 15 | #include <linux/of_graph.h> |
| 16 | #include <linux/regulator/consumer.h> |
| 17 | |
| 18 | #include <drm/drmP.h> |
| 19 | #include <drm/drm_atomic_helper.h> |
| 20 | #include <drm/drm_crtc.h> |
| 21 | #include <drm/drm_crtc_helper.h> |
| 22 | |
| 23 | struct dumb_vga { |
| 24 | struct drm_bridge bridge; |
| 25 | struct drm_connector connector; |
| 26 | |
| 27 | struct i2c_adapter *ddc; |
| 28 | struct regulator *vdd; |
| 29 | }; |
| 30 | |
| 31 | static inline struct dumb_vga * |
| 32 | drm_bridge_to_dumb_vga(struct drm_bridge *bridge) |
| 33 | { |
| 34 | return container_of(bridge, struct dumb_vga, bridge); |
| 35 | } |
| 36 | |
| 37 | static inline struct dumb_vga * |
| 38 | drm_connector_to_dumb_vga(struct drm_connector *connector) |
| 39 | { |
| 40 | return container_of(connector, struct dumb_vga, connector); |
| 41 | } |
| 42 | |
| 43 | static int dumb_vga_get_modes(struct drm_connector *connector) |
| 44 | { |
| 45 | struct dumb_vga *vga = drm_connector_to_dumb_vga(connector); |
| 46 | struct edid *edid; |
| 47 | int ret; |
| 48 | |
| 49 | if (IS_ERR(vga->ddc)) |
| 50 | goto fallback; |
| 51 | |
| 52 | edid = drm_get_edid(connector, vga->ddc); |
| 53 | if (!edid) { |
| 54 | DRM_INFO("EDID readout failed, falling back to standard modes\n"); |
| 55 | goto fallback; |
| 56 | } |
| 57 | |
| 58 | drm_connector_update_edid_property(connector, edid); |
| 59 | ret = drm_add_edid_modes(connector, edid); |
| 60 | kfree(edid); |
| 61 | return ret; |
| 62 | |
| 63 | fallback: |
| 64 | /* |
| 65 | * In case we cannot retrieve the EDIDs (broken or missing i2c |
| 66 | * bus), fallback on the XGA standards |
| 67 | */ |
| 68 | ret = drm_add_modes_noedid(connector, 1920, 1200); |
| 69 | |
| 70 | /* And prefer a mode pretty much anyone can handle */ |
| 71 | drm_set_preferred_mode(connector, 1024, 768); |
| 72 | |
| 73 | return ret; |
| 74 | } |
| 75 | |
| 76 | static const struct drm_connector_helper_funcs dumb_vga_con_helper_funcs = { |
| 77 | .get_modes = dumb_vga_get_modes, |
| 78 | }; |
| 79 | |
| 80 | static enum drm_connector_status |
| 81 | dumb_vga_connector_detect(struct drm_connector *connector, bool force) |
| 82 | { |
| 83 | struct dumb_vga *vga = drm_connector_to_dumb_vga(connector); |
| 84 | |
| 85 | /* |
| 86 | * Even if we have an I2C bus, we can't assume that the cable |
| 87 | * is disconnected if drm_probe_ddc fails. Some cables don't |
| 88 | * wire the DDC pins, or the I2C bus might not be working at |
| 89 | * all. |
| 90 | */ |
| 91 | if (!IS_ERR(vga->ddc) && drm_probe_ddc(vga->ddc)) |
| 92 | return connector_status_connected; |
| 93 | |
| 94 | return connector_status_unknown; |
| 95 | } |
| 96 | |
| 97 | static const struct drm_connector_funcs dumb_vga_con_funcs = { |
| 98 | .detect = dumb_vga_connector_detect, |
| 99 | .fill_modes = drm_helper_probe_single_connector_modes, |
| 100 | .destroy = drm_connector_cleanup, |
| 101 | .reset = drm_atomic_helper_connector_reset, |
| 102 | .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, |
| 103 | .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, |
| 104 | }; |
| 105 | |
| 106 | static int dumb_vga_attach(struct drm_bridge *bridge) |
| 107 | { |
| 108 | struct dumb_vga *vga = drm_bridge_to_dumb_vga(bridge); |
| 109 | int ret; |
| 110 | |
| 111 | if (!bridge->encoder) { |
| 112 | DRM_ERROR("Missing encoder\n"); |
| 113 | return -ENODEV; |
| 114 | } |
| 115 | |
| 116 | drm_connector_helper_add(&vga->connector, |
| 117 | &dumb_vga_con_helper_funcs); |
| 118 | ret = drm_connector_init(bridge->dev, &vga->connector, |
| 119 | &dumb_vga_con_funcs, DRM_MODE_CONNECTOR_VGA); |
| 120 | if (ret) { |
| 121 | DRM_ERROR("Failed to initialize connector\n"); |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | drm_connector_attach_encoder(&vga->connector, |
| 126 | bridge->encoder); |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static void dumb_vga_enable(struct drm_bridge *bridge) |
| 132 | { |
| 133 | struct dumb_vga *vga = drm_bridge_to_dumb_vga(bridge); |
| 134 | int ret = 0; |
| 135 | |
| 136 | if (vga->vdd) |
| 137 | ret = regulator_enable(vga->vdd); |
| 138 | |
| 139 | if (ret) |
| 140 | DRM_ERROR("Failed to enable vdd regulator: %d\n", ret); |
| 141 | } |
| 142 | |
| 143 | static void dumb_vga_disable(struct drm_bridge *bridge) |
| 144 | { |
| 145 | struct dumb_vga *vga = drm_bridge_to_dumb_vga(bridge); |
| 146 | |
| 147 | if (vga->vdd) |
| 148 | regulator_disable(vga->vdd); |
| 149 | } |
| 150 | |
| 151 | static const struct drm_bridge_funcs dumb_vga_bridge_funcs = { |
| 152 | .attach = dumb_vga_attach, |
| 153 | .enable = dumb_vga_enable, |
| 154 | .disable = dumb_vga_disable, |
| 155 | }; |
| 156 | |
| 157 | static struct i2c_adapter *dumb_vga_retrieve_ddc(struct device *dev) |
| 158 | { |
| 159 | struct device_node *phandle, *remote; |
| 160 | struct i2c_adapter *ddc; |
| 161 | |
| 162 | remote = of_graph_get_remote_node(dev->of_node, 1, -1); |
| 163 | if (!remote) |
| 164 | return ERR_PTR(-EINVAL); |
| 165 | |
| 166 | phandle = of_parse_phandle(remote, "ddc-i2c-bus", 0); |
| 167 | of_node_put(remote); |
| 168 | if (!phandle) |
| 169 | return ERR_PTR(-ENODEV); |
| 170 | |
| 171 | ddc = of_get_i2c_adapter_by_node(phandle); |
| 172 | of_node_put(phandle); |
| 173 | if (!ddc) |
| 174 | return ERR_PTR(-EPROBE_DEFER); |
| 175 | |
| 176 | return ddc; |
| 177 | } |
| 178 | |
| 179 | static int dumb_vga_probe(struct platform_device *pdev) |
| 180 | { |
| 181 | struct dumb_vga *vga; |
| 182 | |
| 183 | vga = devm_kzalloc(&pdev->dev, sizeof(*vga), GFP_KERNEL); |
| 184 | if (!vga) |
| 185 | return -ENOMEM; |
| 186 | platform_set_drvdata(pdev, vga); |
| 187 | |
| 188 | vga->vdd = devm_regulator_get_optional(&pdev->dev, "vdd"); |
| 189 | if (IS_ERR(vga->vdd)) { |
| 190 | int ret = PTR_ERR(vga->vdd); |
| 191 | if (ret == -EPROBE_DEFER) |
| 192 | return -EPROBE_DEFER; |
| 193 | vga->vdd = NULL; |
| 194 | dev_dbg(&pdev->dev, "No vdd regulator found: %d\n", ret); |
| 195 | } |
| 196 | |
| 197 | vga->ddc = dumb_vga_retrieve_ddc(&pdev->dev); |
| 198 | if (IS_ERR(vga->ddc)) { |
| 199 | if (PTR_ERR(vga->ddc) == -ENODEV) { |
| 200 | dev_dbg(&pdev->dev, |
| 201 | "No i2c bus specified. Disabling EDID readout\n"); |
| 202 | } else { |
| 203 | dev_err(&pdev->dev, "Couldn't retrieve i2c bus\n"); |
| 204 | return PTR_ERR(vga->ddc); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | vga->bridge.funcs = &dumb_vga_bridge_funcs; |
| 209 | vga->bridge.of_node = pdev->dev.of_node; |
| 210 | vga->bridge.timings = of_device_get_match_data(&pdev->dev); |
| 211 | |
| 212 | drm_bridge_add(&vga->bridge); |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static int dumb_vga_remove(struct platform_device *pdev) |
| 218 | { |
| 219 | struct dumb_vga *vga = platform_get_drvdata(pdev); |
| 220 | |
| 221 | drm_bridge_remove(&vga->bridge); |
| 222 | |
| 223 | if (!IS_ERR(vga->ddc)) |
| 224 | i2c_put_adapter(vga->ddc); |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * We assume the ADV7123 DAC is the "default" for historical reasons |
| 231 | * Information taken from the ADV7123 datasheet, revision D. |
| 232 | * NOTE: the ADV7123EP seems to have other timings and need a new timings |
| 233 | * set if used. |
| 234 | */ |
| 235 | static const struct drm_bridge_timings default_dac_timings = { |
| 236 | /* Timing specifications, datasheet page 7 */ |
| 237 | .sampling_edge = DRM_BUS_FLAG_PIXDATA_POSEDGE, |
| 238 | .setup_time_ps = 500, |
| 239 | .hold_time_ps = 1500, |
| 240 | }; |
| 241 | |
| 242 | /* |
| 243 | * Information taken from the THS8134, THS8134A, THS8134B datasheet named |
| 244 | * "SLVS205D", dated May 1990, revised March 2000. |
| 245 | */ |
| 246 | static const struct drm_bridge_timings ti_ths8134_dac_timings = { |
| 247 | /* From timing diagram, datasheet page 9 */ |
| 248 | .sampling_edge = DRM_BUS_FLAG_PIXDATA_POSEDGE, |
| 249 | /* From datasheet, page 12 */ |
| 250 | .setup_time_ps = 3000, |
| 251 | /* I guess this means latched input */ |
| 252 | .hold_time_ps = 0, |
| 253 | }; |
| 254 | |
| 255 | /* |
| 256 | * Information taken from the THS8135 datasheet named "SLAS343B", dated |
| 257 | * May 2001, revised April 2013. |
| 258 | */ |
| 259 | static const struct drm_bridge_timings ti_ths8135_dac_timings = { |
| 260 | /* From timing diagram, datasheet page 14 */ |
| 261 | .sampling_edge = DRM_BUS_FLAG_PIXDATA_POSEDGE, |
| 262 | /* From datasheet, page 16 */ |
| 263 | .setup_time_ps = 2000, |
| 264 | .hold_time_ps = 500, |
| 265 | }; |
| 266 | |
| 267 | static const struct of_device_id dumb_vga_match[] = { |
| 268 | { |
| 269 | .compatible = "dumb-vga-dac", |
| 270 | .data = NULL, |
| 271 | }, |
| 272 | { |
| 273 | .compatible = "adi,adv7123", |
| 274 | .data = &default_dac_timings, |
| 275 | }, |
| 276 | { |
| 277 | .compatible = "ti,ths8135", |
| 278 | .data = &ti_ths8135_dac_timings, |
| 279 | }, |
| 280 | { |
| 281 | .compatible = "ti,ths8134", |
| 282 | .data = &ti_ths8134_dac_timings, |
| 283 | }, |
| 284 | {}, |
| 285 | }; |
| 286 | MODULE_DEVICE_TABLE(of, dumb_vga_match); |
| 287 | |
| 288 | static struct platform_driver dumb_vga_driver = { |
| 289 | .probe = dumb_vga_probe, |
| 290 | .remove = dumb_vga_remove, |
| 291 | .driver = { |
| 292 | .name = "dumb-vga-dac", |
| 293 | .of_match_table = dumb_vga_match, |
| 294 | }, |
| 295 | }; |
| 296 | module_platform_driver(dumb_vga_driver); |
| 297 | |
| 298 | MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>"); |
| 299 | MODULE_DESCRIPTION("Dumb VGA DAC bridge driver"); |
| 300 | MODULE_LICENSE("GPL"); |