Loading...
Note: File does not exist in v3.1.
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>
15#include <linux/regulator/consumer.h>
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
22struct dumb_vga {
23 struct drm_bridge bridge;
24 struct drm_connector connector;
25
26 struct i2c_adapter *ddc;
27 struct regulator *vdd;
28};
29
30static inline struct dumb_vga *
31drm_bridge_to_dumb_vga(struct drm_bridge *bridge)
32{
33 return container_of(bridge, struct dumb_vga, bridge);
34}
35
36static inline struct dumb_vga *
37drm_connector_to_dumb_vga(struct drm_connector *connector)
38{
39 return container_of(connector, struct dumb_vga, connector);
40}
41
42static 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
60fallback:
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
73static const struct drm_connector_helper_funcs dumb_vga_con_helper_funcs = {
74 .get_modes = dumb_vga_get_modes,
75};
76
77static enum drm_connector_status
78dumb_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
94static const struct drm_connector_funcs dumb_vga_con_funcs = {
95 .dpms = drm_atomic_helper_connector_dpms,
96 .detect = dumb_vga_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
104static int dumb_vga_attach(struct drm_bridge *bridge)
105{
106 struct dumb_vga *vga = drm_bridge_to_dumb_vga(bridge);
107 int ret;
108
109 if (!bridge->encoder) {
110 DRM_ERROR("Missing encoder\n");
111 return -ENODEV;
112 }
113
114 drm_connector_helper_add(&vga->connector,
115 &dumb_vga_con_helper_funcs);
116 ret = drm_connector_init(bridge->dev, &vga->connector,
117 &dumb_vga_con_funcs, DRM_MODE_CONNECTOR_VGA);
118 if (ret) {
119 DRM_ERROR("Failed to initialize connector\n");
120 return ret;
121 }
122
123 drm_mode_connector_attach_encoder(&vga->connector,
124 bridge->encoder);
125
126 return 0;
127}
128
129static void dumb_vga_enable(struct drm_bridge *bridge)
130{
131 struct dumb_vga *vga = drm_bridge_to_dumb_vga(bridge);
132 int ret = 0;
133
134 if (vga->vdd)
135 ret = regulator_enable(vga->vdd);
136
137 if (ret)
138 DRM_ERROR("Failed to enable vdd regulator: %d\n", ret);
139}
140
141static void dumb_vga_disable(struct drm_bridge *bridge)
142{
143 struct dumb_vga *vga = drm_bridge_to_dumb_vga(bridge);
144
145 if (vga->vdd)
146 regulator_disable(vga->vdd);
147}
148
149static const struct drm_bridge_funcs dumb_vga_bridge_funcs = {
150 .attach = dumb_vga_attach,
151 .enable = dumb_vga_enable,
152 .disable = dumb_vga_disable,
153};
154
155static struct i2c_adapter *dumb_vga_retrieve_ddc(struct device *dev)
156{
157 struct device_node *end_node, *phandle, *remote;
158 struct i2c_adapter *ddc;
159
160 end_node = of_graph_get_endpoint_by_regs(dev->of_node, 1, -1);
161 if (!end_node) {
162 dev_err(dev, "Missing connector endpoint\n");
163 return ERR_PTR(-ENODEV);
164 }
165
166 remote = of_graph_get_remote_port_parent(end_node);
167 of_node_put(end_node);
168 if (!remote) {
169 dev_err(dev, "Enable to parse remote node\n");
170 return ERR_PTR(-EINVAL);
171 }
172
173 phandle = of_parse_phandle(remote, "ddc-i2c-bus", 0);
174 of_node_put(remote);
175 if (!phandle)
176 return ERR_PTR(-ENODEV);
177
178 ddc = of_get_i2c_adapter_by_node(phandle);
179 of_node_put(phandle);
180 if (!ddc)
181 return ERR_PTR(-EPROBE_DEFER);
182
183 return ddc;
184}
185
186static int dumb_vga_probe(struct platform_device *pdev)
187{
188 struct dumb_vga *vga;
189 int ret;
190
191 vga = devm_kzalloc(&pdev->dev, sizeof(*vga), GFP_KERNEL);
192 if (!vga)
193 return -ENOMEM;
194 platform_set_drvdata(pdev, vga);
195
196 vga->vdd = devm_regulator_get_optional(&pdev->dev, "vdd");
197 if (IS_ERR(vga->vdd)) {
198 ret = PTR_ERR(vga->vdd);
199 if (ret == -EPROBE_DEFER)
200 return -EPROBE_DEFER;
201 vga->vdd = NULL;
202 dev_dbg(&pdev->dev, "No vdd regulator found: %d\n", ret);
203 }
204
205 vga->ddc = dumb_vga_retrieve_ddc(&pdev->dev);
206 if (IS_ERR(vga->ddc)) {
207 if (PTR_ERR(vga->ddc) == -ENODEV) {
208 dev_dbg(&pdev->dev,
209 "No i2c bus specified. Disabling EDID readout\n");
210 } else {
211 dev_err(&pdev->dev, "Couldn't retrieve i2c bus\n");
212 return PTR_ERR(vga->ddc);
213 }
214 }
215
216 vga->bridge.funcs = &dumb_vga_bridge_funcs;
217 vga->bridge.of_node = pdev->dev.of_node;
218
219 ret = drm_bridge_add(&vga->bridge);
220 if (ret && !IS_ERR(vga->ddc))
221 i2c_put_adapter(vga->ddc);
222
223 return ret;
224}
225
226static int dumb_vga_remove(struct platform_device *pdev)
227{
228 struct dumb_vga *vga = platform_get_drvdata(pdev);
229
230 drm_bridge_remove(&vga->bridge);
231
232 if (!IS_ERR(vga->ddc))
233 i2c_put_adapter(vga->ddc);
234
235 return 0;
236}
237
238static const struct of_device_id dumb_vga_match[] = {
239 { .compatible = "dumb-vga-dac" },
240 {},
241};
242MODULE_DEVICE_TABLE(of, dumb_vga_match);
243
244static struct platform_driver dumb_vga_driver = {
245 .probe = dumb_vga_probe,
246 .remove = dumb_vga_remove,
247 .driver = {
248 .name = "dumb-vga-dac",
249 .of_match_table = dumb_vga_match,
250 },
251};
252module_platform_driver(dumb_vga_driver);
253
254MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
255MODULE_DESCRIPTION("Dumb VGA DAC bridge driver");
256MODULE_LICENSE("GPL");