1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015-2016 Free Electrons
4 * Copyright (C) 2015-2016 NextThing Co
6 * Maxime Ripard <maxime.ripard@free-electrons.com>
9 #include <linux/module.h>
10 #include <linux/of_device.h>
11 #include <linux/of_graph.h>
12 #include <linux/regulator/consumer.h>
14 #include <drm/drm_atomic_helper.h>
15 #include <drm/drm_bridge.h>
16 #include <drm/drm_crtc.h>
17 #include <drm/drm_print.h>
18 #include <drm/drm_probe_helper.h>
21 struct drm_bridge bridge
;
22 struct drm_connector connector
;
24 struct i2c_adapter
*ddc
;
25 struct regulator
*vdd
;
28 static inline struct dumb_vga
*
29 drm_bridge_to_dumb_vga(struct drm_bridge
*bridge
)
31 return container_of(bridge
, struct dumb_vga
, bridge
);
34 static inline struct dumb_vga
*
35 drm_connector_to_dumb_vga(struct drm_connector
*connector
)
37 return container_of(connector
, struct dumb_vga
, connector
);
40 static int dumb_vga_get_modes(struct drm_connector
*connector
)
42 struct dumb_vga
*vga
= drm_connector_to_dumb_vga(connector
);
49 edid
= drm_get_edid(connector
, vga
->ddc
);
51 DRM_INFO("EDID readout failed, falling back to standard modes\n");
55 drm_connector_update_edid_property(connector
, edid
);
56 ret
= drm_add_edid_modes(connector
, edid
);
62 * In case we cannot retrieve the EDIDs (broken or missing i2c
63 * bus), fallback on the XGA standards
65 ret
= drm_add_modes_noedid(connector
, 1920, 1200);
67 /* And prefer a mode pretty much anyone can handle */
68 drm_set_preferred_mode(connector
, 1024, 768);
73 static const struct drm_connector_helper_funcs dumb_vga_con_helper_funcs
= {
74 .get_modes
= dumb_vga_get_modes
,
77 static enum drm_connector_status
78 dumb_vga_connector_detect(struct drm_connector
*connector
, bool force
)
80 struct dumb_vga
*vga
= drm_connector_to_dumb_vga(connector
);
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
88 if (vga
->ddc
&& drm_probe_ddc(vga
->ddc
))
89 return connector_status_connected
;
91 return connector_status_unknown
;
94 static const struct drm_connector_funcs dumb_vga_con_funcs
= {
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
,
103 static int dumb_vga_attach(struct drm_bridge
*bridge
)
105 struct dumb_vga
*vga
= drm_bridge_to_dumb_vga(bridge
);
108 if (!bridge
->encoder
) {
109 DRM_ERROR("Missing encoder\n");
113 drm_connector_helper_add(&vga
->connector
,
114 &dumb_vga_con_helper_funcs
);
115 ret
= drm_connector_init_with_ddc(bridge
->dev
, &vga
->connector
,
117 DRM_MODE_CONNECTOR_VGA
,
120 DRM_ERROR("Failed to initialize connector\n");
124 drm_connector_attach_encoder(&vga
->connector
,
130 static void dumb_vga_enable(struct drm_bridge
*bridge
)
132 struct dumb_vga
*vga
= drm_bridge_to_dumb_vga(bridge
);
136 ret
= regulator_enable(vga
->vdd
);
139 DRM_ERROR("Failed to enable vdd regulator: %d\n", ret
);
142 static void dumb_vga_disable(struct drm_bridge
*bridge
)
144 struct dumb_vga
*vga
= drm_bridge_to_dumb_vga(bridge
);
147 regulator_disable(vga
->vdd
);
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
,
156 static struct i2c_adapter
*dumb_vga_retrieve_ddc(struct device
*dev
)
158 struct device_node
*phandle
, *remote
;
159 struct i2c_adapter
*ddc
;
161 remote
= of_graph_get_remote_node(dev
->of_node
, 1, -1);
163 return ERR_PTR(-EINVAL
);
165 phandle
= of_parse_phandle(remote
, "ddc-i2c-bus", 0);
168 return ERR_PTR(-ENODEV
);
170 ddc
= of_get_i2c_adapter_by_node(phandle
);
171 of_node_put(phandle
);
173 return ERR_PTR(-EPROBE_DEFER
);
178 static int dumb_vga_probe(struct platform_device
*pdev
)
180 struct dumb_vga
*vga
;
182 vga
= devm_kzalloc(&pdev
->dev
, sizeof(*vga
), GFP_KERNEL
);
185 platform_set_drvdata(pdev
, vga
);
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
;
193 dev_dbg(&pdev
->dev
, "No vdd regulator found: %d\n", ret
);
196 vga
->ddc
= dumb_vga_retrieve_ddc(&pdev
->dev
);
197 if (IS_ERR(vga
->ddc
)) {
198 if (PTR_ERR(vga
->ddc
) == -ENODEV
) {
200 "No i2c bus specified. Disabling EDID readout\n");
203 dev_err(&pdev
->dev
, "Couldn't retrieve i2c bus\n");
204 return PTR_ERR(vga
->ddc
);
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
);
212 drm_bridge_add(&vga
->bridge
);
217 static int dumb_vga_remove(struct platform_device
*pdev
)
219 struct dumb_vga
*vga
= platform_get_drvdata(pdev
);
221 drm_bridge_remove(&vga
->bridge
);
224 i2c_put_adapter(vga
->ddc
);
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
235 static const struct drm_bridge_timings default_dac_timings
= {
236 /* Timing specifications, datasheet page 7 */
237 .input_bus_flags
= DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
,
238 .setup_time_ps
= 500,
239 .hold_time_ps
= 1500,
243 * Information taken from the THS8134, THS8134A, THS8134B datasheet named
244 * "SLVS205D", dated May 1990, revised March 2000.
246 static const struct drm_bridge_timings ti_ths8134_dac_timings
= {
247 /* From timing diagram, datasheet page 9 */
248 .input_bus_flags
= DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
,
249 /* From datasheet, page 12 */
250 .setup_time_ps
= 3000,
251 /* I guess this means latched input */
256 * Information taken from the THS8135 datasheet named "SLAS343B", dated
257 * May 2001, revised April 2013.
259 static const struct drm_bridge_timings ti_ths8135_dac_timings
= {
260 /* From timing diagram, datasheet page 14 */
261 .input_bus_flags
= DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
,
262 /* From datasheet, page 16 */
263 .setup_time_ps
= 2000,
267 static const struct of_device_id dumb_vga_match
[] = {
269 .compatible
= "dumb-vga-dac",
273 .compatible
= "adi,adv7123",
274 .data
= &default_dac_timings
,
277 .compatible
= "ti,ths8135",
278 .data
= &ti_ths8135_dac_timings
,
281 .compatible
= "ti,ths8134",
282 .data
= &ti_ths8134_dac_timings
,
286 MODULE_DEVICE_TABLE(of
, dumb_vga_match
);
288 static struct platform_driver dumb_vga_driver
= {
289 .probe
= dumb_vga_probe
,
290 .remove
= dumb_vga_remove
,
292 .name
= "dumb-vga-dac",
293 .of_match_table
= dumb_vga_match
,
296 module_platform_driver(dumb_vga_driver
);
298 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
299 MODULE_DESCRIPTION("Dumb VGA DAC bridge driver");
300 MODULE_LICENSE("GPL");