GNU Linux-libre 4.14.262-gnu1
[releases.git] / drivers / gpu / drm / bridge / dumb-vga-dac.c
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
22 struct dumb_vga {
23         struct drm_bridge       bridge;
24         struct drm_connector    connector;
25
26         struct i2c_adapter      *ddc;
27         struct regulator        *vdd;
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         ret = drm_add_edid_modes(connector, edid);
59         kfree(edid);
60         return ret;
61
62 fallback:
63         /*
64          * In case we cannot retrieve the EDIDs (broken or missing i2c
65          * bus), fallback on the XGA standards
66          */
67         ret = drm_add_modes_noedid(connector, 1920, 1200);
68
69         /* And prefer a mode pretty much anyone can handle */
70         drm_set_preferred_mode(connector, 1024, 768);
71
72         return ret;
73 }
74
75 static const struct drm_connector_helper_funcs dumb_vga_con_helper_funcs = {
76         .get_modes      = dumb_vga_get_modes,
77 };
78
79 static enum drm_connector_status
80 dumb_vga_connector_detect(struct drm_connector *connector, bool force)
81 {
82         struct dumb_vga *vga = drm_connector_to_dumb_vga(connector);
83
84         /*
85          * Even if we have an I2C bus, we can't assume that the cable
86          * is disconnected if drm_probe_ddc fails. Some cables don't
87          * wire the DDC pins, or the I2C bus might not be working at
88          * all.
89          */
90         if (!IS_ERR(vga->ddc) && drm_probe_ddc(vga->ddc))
91                 return connector_status_connected;
92
93         return connector_status_unknown;
94 }
95
96 static const struct drm_connector_funcs dumb_vga_con_funcs = {
97         .detect                 = dumb_vga_connector_detect,
98         .fill_modes             = drm_helper_probe_single_connector_modes,
99         .destroy                = drm_connector_cleanup,
100         .reset                  = drm_atomic_helper_connector_reset,
101         .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
102         .atomic_destroy_state   = drm_atomic_helper_connector_destroy_state,
103 };
104
105 static int dumb_vga_attach(struct drm_bridge *bridge)
106 {
107         struct dumb_vga *vga = drm_bridge_to_dumb_vga(bridge);
108         int ret;
109
110         if (!bridge->encoder) {
111                 DRM_ERROR("Missing encoder\n");
112                 return -ENODEV;
113         }
114
115         drm_connector_helper_add(&vga->connector,
116                                  &dumb_vga_con_helper_funcs);
117         ret = drm_connector_init(bridge->dev, &vga->connector,
118                                  &dumb_vga_con_funcs, DRM_MODE_CONNECTOR_VGA);
119         if (ret) {
120                 DRM_ERROR("Failed to initialize connector\n");
121                 return ret;
122         }
123
124         drm_mode_connector_attach_encoder(&vga->connector,
125                                           bridge->encoder);
126
127         return 0;
128 }
129
130 static void dumb_vga_enable(struct drm_bridge *bridge)
131 {
132         struct dumb_vga *vga = drm_bridge_to_dumb_vga(bridge);
133         int ret = 0;
134
135         if (vga->vdd)
136                 ret = regulator_enable(vga->vdd);
137
138         if (ret)
139                 DRM_ERROR("Failed to enable vdd regulator: %d\n", ret);
140 }
141
142 static void dumb_vga_disable(struct drm_bridge *bridge)
143 {
144         struct dumb_vga *vga = drm_bridge_to_dumb_vga(bridge);
145
146         if (vga->vdd)
147                 regulator_disable(vga->vdd);
148 }
149
150 static const struct drm_bridge_funcs dumb_vga_bridge_funcs = {
151         .attach         = dumb_vga_attach,
152         .enable         = dumb_vga_enable,
153         .disable        = dumb_vga_disable,
154 };
155
156 static struct i2c_adapter *dumb_vga_retrieve_ddc(struct device *dev)
157 {
158         struct device_node *phandle, *remote;
159         struct i2c_adapter *ddc;
160
161         remote = of_graph_get_remote_node(dev->of_node, 1, -1);
162         if (!remote)
163                 return ERR_PTR(-EINVAL);
164
165         phandle = of_parse_phandle(remote, "ddc-i2c-bus", 0);
166         of_node_put(remote);
167         if (!phandle)
168                 return ERR_PTR(-ENODEV);
169
170         ddc = of_get_i2c_adapter_by_node(phandle);
171         of_node_put(phandle);
172         if (!ddc)
173                 return ERR_PTR(-EPROBE_DEFER);
174
175         return ddc;
176 }
177
178 static int dumb_vga_probe(struct platform_device *pdev)
179 {
180         struct dumb_vga *vga;
181
182         vga = devm_kzalloc(&pdev->dev, sizeof(*vga), GFP_KERNEL);
183         if (!vga)
184                 return -ENOMEM;
185         platform_set_drvdata(pdev, vga);
186
187         vga->vdd = devm_regulator_get_optional(&pdev->dev, "vdd");
188         if (IS_ERR(vga->vdd)) {
189                 int ret = PTR_ERR(vga->vdd);
190                 if (ret == -EPROBE_DEFER)
191                         return -EPROBE_DEFER;
192                 vga->vdd = NULL;
193                 dev_dbg(&pdev->dev, "No vdd regulator found: %d\n", ret);
194         }
195
196         vga->ddc = dumb_vga_retrieve_ddc(&pdev->dev);
197         if (IS_ERR(vga->ddc)) {
198                 if (PTR_ERR(vga->ddc) == -ENODEV) {
199                         dev_dbg(&pdev->dev,
200                                 "No i2c bus specified. Disabling EDID readout\n");
201                 } else {
202                         dev_err(&pdev->dev, "Couldn't retrieve i2c bus\n");
203                         return PTR_ERR(vga->ddc);
204                 }
205         }
206
207         vga->bridge.funcs = &dumb_vga_bridge_funcs;
208         vga->bridge.of_node = pdev->dev.of_node;
209
210         drm_bridge_add(&vga->bridge);
211
212         return 0;
213 }
214
215 static int dumb_vga_remove(struct platform_device *pdev)
216 {
217         struct dumb_vga *vga = platform_get_drvdata(pdev);
218
219         drm_bridge_remove(&vga->bridge);
220
221         if (!IS_ERR(vga->ddc))
222                 i2c_put_adapter(vga->ddc);
223
224         return 0;
225 }
226
227 static const struct of_device_id dumb_vga_match[] = {
228         { .compatible = "dumb-vga-dac" },
229         { .compatible = "adi,adv7123" },
230         { .compatible = "ti,ths8135" },
231         {},
232 };
233 MODULE_DEVICE_TABLE(of, dumb_vga_match);
234
235 static struct platform_driver dumb_vga_driver = {
236         .probe  = dumb_vga_probe,
237         .remove = dumb_vga_remove,
238         .driver         = {
239                 .name           = "dumb-vga-dac",
240                 .of_match_table = dumb_vga_match,
241         },
242 };
243 module_platform_driver(dumb_vga_driver);
244
245 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
246 MODULE_DESCRIPTION("Dumb VGA DAC bridge driver");
247 MODULE_LICENSE("GPL");