1
2
3
4
5
6
7
8
9#include <linux/module.h>
10#include <linux/of_device.h>
11#include <linux/of_graph.h>
12#include <linux/regulator/consumer.h>
13
14#include <drm/drm_atomic_helper.h>
15#include <drm/drm_crtc.h>
16#include <drm/drm_print.h>
17#include <drm/drm_probe_helper.h>
18
19struct dumb_vga {
20 struct drm_bridge bridge;
21 struct drm_connector connector;
22
23 struct i2c_adapter *ddc;
24 struct regulator *vdd;
25};
26
27static inline struct dumb_vga *
28drm_bridge_to_dumb_vga(struct drm_bridge *bridge)
29{
30 return container_of(bridge, struct dumb_vga, bridge);
31}
32
33static inline struct dumb_vga *
34drm_connector_to_dumb_vga(struct drm_connector *connector)
35{
36 return container_of(connector, struct dumb_vga, connector);
37}
38
39static int dumb_vga_get_modes(struct drm_connector *connector)
40{
41 struct dumb_vga *vga = drm_connector_to_dumb_vga(connector);
42 struct edid *edid;
43 int ret;
44
45 if (IS_ERR(vga->ddc))
46 goto fallback;
47
48 edid = drm_get_edid(connector, vga->ddc);
49 if (!edid) {
50 DRM_INFO("EDID readout failed, falling back to standard modes\n");
51 goto fallback;
52 }
53
54 drm_connector_update_edid_property(connector, edid);
55 ret = drm_add_edid_modes(connector, edid);
56 kfree(edid);
57 return ret;
58
59fallback:
60
61
62
63
64 ret = drm_add_modes_noedid(connector, 1920, 1200);
65
66
67 drm_set_preferred_mode(connector, 1024, 768);
68
69 return ret;
70}
71
72static const struct drm_connector_helper_funcs dumb_vga_con_helper_funcs = {
73 .get_modes = dumb_vga_get_modes,
74};
75
76static enum drm_connector_status
77dumb_vga_connector_detect(struct drm_connector *connector, bool force)
78{
79 struct dumb_vga *vga = drm_connector_to_dumb_vga(connector);
80
81
82
83
84
85
86
87 if (!IS_ERR(vga->ddc) && drm_probe_ddc(vga->ddc))
88 return connector_status_connected;
89
90 return connector_status_unknown;
91}
92
93static const struct drm_connector_funcs dumb_vga_con_funcs = {
94 .detect = dumb_vga_connector_detect,
95 .fill_modes = drm_helper_probe_single_connector_modes,
96 .destroy = drm_connector_cleanup,
97 .reset = drm_atomic_helper_connector_reset,
98 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
99 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
100};
101
102static int dumb_vga_attach(struct drm_bridge *bridge)
103{
104 struct dumb_vga *vga = drm_bridge_to_dumb_vga(bridge);
105 int ret;
106
107 if (!bridge->encoder) {
108 DRM_ERROR("Missing encoder\n");
109 return -ENODEV;
110 }
111
112 drm_connector_helper_add(&vga->connector,
113 &dumb_vga_con_helper_funcs);
114 ret = drm_connector_init(bridge->dev, &vga->connector,
115 &dumb_vga_con_funcs, DRM_MODE_CONNECTOR_VGA);
116 if (ret) {
117 DRM_ERROR("Failed to initialize connector\n");
118 return ret;
119 }
120
121 drm_connector_attach_encoder(&vga->connector,
122 bridge->encoder);
123
124 return 0;
125}
126
127static void dumb_vga_enable(struct drm_bridge *bridge)
128{
129 struct dumb_vga *vga = drm_bridge_to_dumb_vga(bridge);
130 int ret = 0;
131
132 if (vga->vdd)
133 ret = regulator_enable(vga->vdd);
134
135 if (ret)
136 DRM_ERROR("Failed to enable vdd regulator: %d\n", ret);
137}
138
139static void dumb_vga_disable(struct drm_bridge *bridge)
140{
141 struct dumb_vga *vga = drm_bridge_to_dumb_vga(bridge);
142
143 if (vga->vdd)
144 regulator_disable(vga->vdd);
145}
146
147static const struct drm_bridge_funcs dumb_vga_bridge_funcs = {
148 .attach = dumb_vga_attach,
149 .enable = dumb_vga_enable,
150 .disable = dumb_vga_disable,
151};
152
153static struct i2c_adapter *dumb_vga_retrieve_ddc(struct device *dev)
154{
155 struct device_node *phandle, *remote;
156 struct i2c_adapter *ddc;
157
158 remote = of_graph_get_remote_node(dev->of_node, 1, -1);
159 if (!remote)
160 return ERR_PTR(-EINVAL);
161
162 phandle = of_parse_phandle(remote, "ddc-i2c-bus", 0);
163 of_node_put(remote);
164 if (!phandle)
165 return ERR_PTR(-ENODEV);
166
167 ddc = of_get_i2c_adapter_by_node(phandle);
168 of_node_put(phandle);
169 if (!ddc)
170 return ERR_PTR(-EPROBE_DEFER);
171
172 return ddc;
173}
174
175static int dumb_vga_probe(struct platform_device *pdev)
176{
177 struct dumb_vga *vga;
178
179 vga = devm_kzalloc(&pdev->dev, sizeof(*vga), GFP_KERNEL);
180 if (!vga)
181 return -ENOMEM;
182 platform_set_drvdata(pdev, vga);
183
184 vga->vdd = devm_regulator_get_optional(&pdev->dev, "vdd");
185 if (IS_ERR(vga->vdd)) {
186 int ret = PTR_ERR(vga->vdd);
187 if (ret == -EPROBE_DEFER)
188 return -EPROBE_DEFER;
189 vga->vdd = NULL;
190 dev_dbg(&pdev->dev, "No vdd regulator found: %d\n", ret);
191 }
192
193 vga->ddc = dumb_vga_retrieve_ddc(&pdev->dev);
194 if (IS_ERR(vga->ddc)) {
195 if (PTR_ERR(vga->ddc) == -ENODEV) {
196 dev_dbg(&pdev->dev,
197 "No i2c bus specified. Disabling EDID readout\n");
198 } else {
199 dev_err(&pdev->dev, "Couldn't retrieve i2c bus\n");
200 return PTR_ERR(vga->ddc);
201 }
202 }
203
204 vga->bridge.funcs = &dumb_vga_bridge_funcs;
205 vga->bridge.of_node = pdev->dev.of_node;
206 vga->bridge.timings = of_device_get_match_data(&pdev->dev);
207
208 drm_bridge_add(&vga->bridge);
209
210 return 0;
211}
212
213static 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
226
227
228
229
230
231static const struct drm_bridge_timings default_dac_timings = {
232
233 .input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE,
234 .setup_time_ps = 500,
235 .hold_time_ps = 1500,
236};
237
238
239
240
241
242static const struct drm_bridge_timings ti_ths8134_dac_timings = {
243
244 .input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE,
245
246 .setup_time_ps = 3000,
247
248 .hold_time_ps = 0,
249};
250
251
252
253
254
255static const struct drm_bridge_timings ti_ths8135_dac_timings = {
256
257 .input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE,
258
259 .setup_time_ps = 2000,
260 .hold_time_ps = 500,
261};
262
263static const struct of_device_id dumb_vga_match[] = {
264 {
265 .compatible = "dumb-vga-dac",
266 .data = NULL,
267 },
268 {
269 .compatible = "adi,adv7123",
270 .data = &default_dac_timings,
271 },
272 {
273 .compatible = "ti,ths8135",
274 .data = &ti_ths8135_dac_timings,
275 },
276 {
277 .compatible = "ti,ths8134",
278 .data = &ti_ths8134_dac_timings,
279 },
280 {},
281};
282MODULE_DEVICE_TABLE(of, dumb_vga_match);
283
284static struct platform_driver dumb_vga_driver = {
285 .probe = dumb_vga_probe,
286 .remove = dumb_vga_remove,
287 .driver = {
288 .name = "dumb-vga-dac",
289 .of_match_table = dumb_vga_match,
290 },
291};
292module_platform_driver(dumb_vga_driver);
293
294MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
295MODULE_DESCRIPTION("Dumb VGA DAC bridge driver");
296MODULE_LICENSE("GPL");
297