Maxime Ripard | 56fe8b6 | 2016-09-30 16:37:06 +0200 | [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_graph.h> |
Chen-Yu Tsai | af948a2 | 2016-11-16 23:42:31 +0800 | [diff] [blame] | 15 | #include <linux/regulator/consumer.h> |
Maxime Ripard | 56fe8b6 | 2016-09-30 16:37:06 +0200 | [diff] [blame] | 16 | |
| 17 | #include <drm/drmP.h> |
| 18 | #include <drm/drm_atomic_helper.h> |
| 19 | #include <drm/drm_crtc.h> |
| 20 | #include <drm/drm_crtc_helper.h> |
| 21 | |
| 22 | struct dumb_vga { |
| 23 | struct drm_bridge bridge; |
| 24 | struct drm_connector connector; |
| 25 | |
| 26 | struct i2c_adapter *ddc; |
Chen-Yu Tsai | af948a2 | 2016-11-16 23:42:31 +0800 | [diff] [blame] | 27 | struct regulator *vdd; |
Maxime Ripard | 56fe8b6 | 2016-09-30 16:37:06 +0200 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | static inline struct dumb_vga * |
| 31 | drm_bridge_to_dumb_vga(struct drm_bridge *bridge) |
| 32 | { |
| 33 | return container_of(bridge, struct dumb_vga, bridge); |
| 34 | } |
| 35 | |
| 36 | static inline struct dumb_vga * |
| 37 | drm_connector_to_dumb_vga(struct drm_connector *connector) |
| 38 | { |
| 39 | return container_of(connector, struct dumb_vga, connector); |
| 40 | } |
| 41 | |
| 42 | static int dumb_vga_get_modes(struct drm_connector *connector) |
| 43 | { |
| 44 | struct dumb_vga *vga = drm_connector_to_dumb_vga(connector); |
| 45 | struct edid *edid; |
| 46 | int ret; |
| 47 | |
| 48 | if (IS_ERR(vga->ddc)) |
| 49 | goto fallback; |
| 50 | |
| 51 | edid = drm_get_edid(connector, vga->ddc); |
| 52 | if (!edid) { |
| 53 | DRM_INFO("EDID readout failed, falling back to standard modes\n"); |
| 54 | goto fallback; |
| 55 | } |
| 56 | |
| 57 | drm_mode_connector_update_edid_property(connector, edid); |
| 58 | return drm_add_edid_modes(connector, edid); |
| 59 | |
| 60 | fallback: |
| 61 | /* |
| 62 | * In case we cannot retrieve the EDIDs (broken or missing i2c |
| 63 | * bus), fallback on the XGA standards |
| 64 | */ |
| 65 | ret = drm_add_modes_noedid(connector, 1920, 1200); |
| 66 | |
| 67 | /* And prefer a mode pretty much anyone can handle */ |
| 68 | drm_set_preferred_mode(connector, 1024, 768); |
| 69 | |
| 70 | return ret; |
| 71 | } |
| 72 | |
| 73 | static const struct drm_connector_helper_funcs dumb_vga_con_helper_funcs = { |
| 74 | .get_modes = dumb_vga_get_modes, |
| 75 | }; |
| 76 | |
| 77 | static enum drm_connector_status |
| 78 | dumb_vga_connector_detect(struct drm_connector *connector, bool force) |
| 79 | { |
| 80 | struct dumb_vga *vga = drm_connector_to_dumb_vga(connector); |
| 81 | |
| 82 | /* |
| 83 | * Even if we have an I2C bus, we can't assume that the cable |
| 84 | * is disconnected if drm_probe_ddc fails. Some cables don't |
| 85 | * wire the DDC pins, or the I2C bus might not be working at |
| 86 | * all. |
| 87 | */ |
| 88 | if (!IS_ERR(vga->ddc) && drm_probe_ddc(vga->ddc)) |
| 89 | return connector_status_connected; |
| 90 | |
| 91 | return connector_status_unknown; |
| 92 | } |
| 93 | |
| 94 | static const struct drm_connector_funcs dumb_vga_con_funcs = { |
Maxime Ripard | 56fe8b6 | 2016-09-30 16:37:06 +0200 | [diff] [blame] | 95 | .detect = dumb_vga_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 dumb_vga_attach(struct drm_bridge *bridge) |
| 104 | { |
| 105 | struct dumb_vga *vga = drm_bridge_to_dumb_vga(bridge); |
| 106 | int ret; |
| 107 | |
| 108 | if (!bridge->encoder) { |
| 109 | DRM_ERROR("Missing encoder\n"); |
| 110 | return -ENODEV; |
| 111 | } |
| 112 | |
| 113 | drm_connector_helper_add(&vga->connector, |
| 114 | &dumb_vga_con_helper_funcs); |
| 115 | ret = drm_connector_init(bridge->dev, &vga->connector, |
| 116 | &dumb_vga_con_funcs, DRM_MODE_CONNECTOR_VGA); |
| 117 | if (ret) { |
| 118 | DRM_ERROR("Failed to initialize connector\n"); |
| 119 | return ret; |
| 120 | } |
| 121 | |
| 122 | drm_mode_connector_attach_encoder(&vga->connector, |
| 123 | bridge->encoder); |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
Chen-Yu Tsai | af948a2 | 2016-11-16 23:42:31 +0800 | [diff] [blame] | 128 | static void dumb_vga_enable(struct drm_bridge *bridge) |
| 129 | { |
| 130 | struct dumb_vga *vga = drm_bridge_to_dumb_vga(bridge); |
| 131 | int ret = 0; |
| 132 | |
| 133 | if (vga->vdd) |
| 134 | ret = regulator_enable(vga->vdd); |
| 135 | |
| 136 | if (ret) |
| 137 | DRM_ERROR("Failed to enable vdd regulator: %d\n", ret); |
| 138 | } |
| 139 | |
| 140 | static void dumb_vga_disable(struct drm_bridge *bridge) |
| 141 | { |
| 142 | struct dumb_vga *vga = drm_bridge_to_dumb_vga(bridge); |
| 143 | |
| 144 | if (vga->vdd) |
| 145 | regulator_disable(vga->vdd); |
| 146 | } |
| 147 | |
Maxime Ripard | 56fe8b6 | 2016-09-30 16:37:06 +0200 | [diff] [blame] | 148 | static const struct drm_bridge_funcs dumb_vga_bridge_funcs = { |
| 149 | .attach = dumb_vga_attach, |
Chen-Yu Tsai | af948a2 | 2016-11-16 23:42:31 +0800 | [diff] [blame] | 150 | .enable = dumb_vga_enable, |
| 151 | .disable = dumb_vga_disable, |
Maxime Ripard | 56fe8b6 | 2016-09-30 16:37:06 +0200 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | static struct i2c_adapter *dumb_vga_retrieve_ddc(struct device *dev) |
| 155 | { |
Rob Herring | 86418f9 | 2017-03-22 08:26:06 -0500 | [diff] [blame] | 156 | struct device_node *phandle, *remote; |
Maxime Ripard | 56fe8b6 | 2016-09-30 16:37:06 +0200 | [diff] [blame] | 157 | struct i2c_adapter *ddc; |
| 158 | |
Rob Herring | 86418f9 | 2017-03-22 08:26:06 -0500 | [diff] [blame] | 159 | remote = of_graph_get_remote_node(dev->of_node, 1, -1); |
| 160 | if (!remote) |
Maxime Ripard | 56fe8b6 | 2016-09-30 16:37:06 +0200 | [diff] [blame] | 161 | return ERR_PTR(-EINVAL); |
Maxime Ripard | 56fe8b6 | 2016-09-30 16:37:06 +0200 | [diff] [blame] | 162 | |
| 163 | phandle = of_parse_phandle(remote, "ddc-i2c-bus", 0); |
| 164 | of_node_put(remote); |
| 165 | if (!phandle) |
| 166 | return ERR_PTR(-ENODEV); |
| 167 | |
| 168 | ddc = of_get_i2c_adapter_by_node(phandle); |
| 169 | of_node_put(phandle); |
| 170 | if (!ddc) |
| 171 | return ERR_PTR(-EPROBE_DEFER); |
| 172 | |
| 173 | return ddc; |
| 174 | } |
| 175 | |
| 176 | static int dumb_vga_probe(struct platform_device *pdev) |
| 177 | { |
| 178 | struct dumb_vga *vga; |
Maxime Ripard | 56fe8b6 | 2016-09-30 16:37:06 +0200 | [diff] [blame] | 179 | |
| 180 | vga = devm_kzalloc(&pdev->dev, sizeof(*vga), GFP_KERNEL); |
| 181 | if (!vga) |
| 182 | return -ENOMEM; |
| 183 | platform_set_drvdata(pdev, vga); |
| 184 | |
Chen-Yu Tsai | af948a2 | 2016-11-16 23:42:31 +0800 | [diff] [blame] | 185 | vga->vdd = devm_regulator_get_optional(&pdev->dev, "vdd"); |
| 186 | if (IS_ERR(vga->vdd)) { |
Inki Dae | 47e3427 | 2017-07-03 17:42:20 +0900 | [diff] [blame] | 187 | int ret = PTR_ERR(vga->vdd); |
Chen-Yu Tsai | af948a2 | 2016-11-16 23:42:31 +0800 | [diff] [blame] | 188 | if (ret == -EPROBE_DEFER) |
| 189 | return -EPROBE_DEFER; |
| 190 | vga->vdd = NULL; |
| 191 | dev_dbg(&pdev->dev, "No vdd regulator found: %d\n", ret); |
| 192 | } |
| 193 | |
Maxime Ripard | 56fe8b6 | 2016-09-30 16:37:06 +0200 | [diff] [blame] | 194 | vga->ddc = dumb_vga_retrieve_ddc(&pdev->dev); |
| 195 | if (IS_ERR(vga->ddc)) { |
| 196 | if (PTR_ERR(vga->ddc) == -ENODEV) { |
| 197 | dev_dbg(&pdev->dev, |
| 198 | "No i2c bus specified. Disabling EDID readout\n"); |
| 199 | } else { |
| 200 | dev_err(&pdev->dev, "Couldn't retrieve i2c bus\n"); |
| 201 | return PTR_ERR(vga->ddc); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | vga->bridge.funcs = &dumb_vga_bridge_funcs; |
| 206 | vga->bridge.of_node = pdev->dev.of_node; |
| 207 | |
Inki Dae | 47e3427 | 2017-07-03 17:42:20 +0900 | [diff] [blame] | 208 | drm_bridge_add(&vga->bridge); |
Maxime Ripard | 56fe8b6 | 2016-09-30 16:37:06 +0200 | [diff] [blame] | 209 | |
Inki Dae | 47e3427 | 2017-07-03 17:42:20 +0900 | [diff] [blame] | 210 | return 0; |
Maxime Ripard | 56fe8b6 | 2016-09-30 16:37:06 +0200 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static int dumb_vga_remove(struct platform_device *pdev) |
| 214 | { |
| 215 | struct dumb_vga *vga = platform_get_drvdata(pdev); |
| 216 | |
| 217 | drm_bridge_remove(&vga->bridge); |
| 218 | |
| 219 | if (!IS_ERR(vga->ddc)) |
| 220 | i2c_put_adapter(vga->ddc); |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static const struct of_device_id dumb_vga_match[] = { |
| 226 | { .compatible = "dumb-vga-dac" }, |
Laurent Pinchart | d29ffab | 2017-03-02 12:47:27 +0200 | [diff] [blame] | 227 | { .compatible = "adi,adv7123" }, |
Bartosz Golaszewski | a1f761a | 2016-12-13 11:09:17 +0100 | [diff] [blame] | 228 | { .compatible = "ti,ths8135" }, |
Maxime Ripard | 56fe8b6 | 2016-09-30 16:37:06 +0200 | [diff] [blame] | 229 | {}, |
| 230 | }; |
| 231 | MODULE_DEVICE_TABLE(of, dumb_vga_match); |
| 232 | |
| 233 | static struct platform_driver dumb_vga_driver = { |
| 234 | .probe = dumb_vga_probe, |
| 235 | .remove = dumb_vga_remove, |
| 236 | .driver = { |
| 237 | .name = "dumb-vga-dac", |
| 238 | .of_match_table = dumb_vga_match, |
| 239 | }, |
| 240 | }; |
| 241 | module_platform_driver(dumb_vga_driver); |
| 242 | |
| 243 | MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>"); |
| 244 | MODULE_DESCRIPTION("Dumb VGA DAC bridge driver"); |
| 245 | MODULE_LICENSE("GPL"); |