2 * Copyright (C) 2015-2016 Free Electrons
3 * Copyright (C) 2015-2016 NextThing Co
5 * Maxime Ripard <maxime.ripard@free-electrons.com>
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.
13 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/of_graph.h>
16 #include <linux/regulator/consumer.h>
19 #include <drm/drm_atomic_helper.h>
20 #include <drm/drm_crtc.h>
21 #include <drm/drm_crtc_helper.h>
24 struct drm_bridge bridge
;
25 struct drm_connector connector
;
27 struct i2c_adapter
*ddc
;
28 struct regulator
*vdd
;
31 static inline struct dumb_vga
*
32 drm_bridge_to_dumb_vga(struct drm_bridge
*bridge
)
34 return container_of(bridge
, struct dumb_vga
, bridge
);
37 static inline struct dumb_vga
*
38 drm_connector_to_dumb_vga(struct drm_connector
*connector
)
40 return container_of(connector
, struct dumb_vga
, connector
);
43 static int dumb_vga_get_modes(struct drm_connector
*connector
)
45 struct dumb_vga
*vga
= drm_connector_to_dumb_vga(connector
);
52 edid
= drm_get_edid(connector
, vga
->ddc
);
54 DRM_INFO("EDID readout failed, falling back to standard modes\n");
58 drm_connector_update_edid_property(connector
, edid
);
59 ret
= drm_add_edid_modes(connector
, edid
);
65 * In case we cannot retrieve the EDIDs (broken or missing i2c
66 * bus), fallback on the XGA standards
68 ret
= drm_add_modes_noedid(connector
, 1920, 1200);
70 /* And prefer a mode pretty much anyone can handle */
71 drm_set_preferred_mode(connector
, 1024, 768);
76 static const struct drm_connector_helper_funcs dumb_vga_con_helper_funcs
= {
77 .get_modes
= dumb_vga_get_modes
,
80 static enum drm_connector_status
81 dumb_vga_connector_detect(struct drm_connector
*connector
, bool force
)
83 struct dumb_vga
*vga
= drm_connector_to_dumb_vga(connector
);
86 * Even if we have an I2C bus, we can't assume that the cable
87 * is disconnected if drm_probe_ddc fails. Some cables don't
88 * wire the DDC pins, or the I2C bus might not be working at
91 if (!IS_ERR(vga
->ddc
) && drm_probe_ddc(vga
->ddc
))
92 return connector_status_connected
;
94 return connector_status_unknown
;
97 static const struct drm_connector_funcs dumb_vga_con_funcs
= {
98 .detect
= dumb_vga_connector_detect
,
99 .fill_modes
= drm_helper_probe_single_connector_modes
,
100 .destroy
= drm_connector_cleanup
,
101 .reset
= drm_atomic_helper_connector_reset
,
102 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
103 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
106 static int dumb_vga_attach(struct drm_bridge
*bridge
)
108 struct dumb_vga
*vga
= drm_bridge_to_dumb_vga(bridge
);
111 if (!bridge
->encoder
) {
112 DRM_ERROR("Missing encoder\n");
116 drm_connector_helper_add(&vga
->connector
,
117 &dumb_vga_con_helper_funcs
);
118 ret
= drm_connector_init(bridge
->dev
, &vga
->connector
,
119 &dumb_vga_con_funcs
, DRM_MODE_CONNECTOR_VGA
);
121 DRM_ERROR("Failed to initialize connector\n");
125 drm_connector_attach_encoder(&vga
->connector
,
131 static void dumb_vga_enable(struct drm_bridge
*bridge
)
133 struct dumb_vga
*vga
= drm_bridge_to_dumb_vga(bridge
);
137 ret
= regulator_enable(vga
->vdd
);
140 DRM_ERROR("Failed to enable vdd regulator: %d\n", ret
);
143 static void dumb_vga_disable(struct drm_bridge
*bridge
)
145 struct dumb_vga
*vga
= drm_bridge_to_dumb_vga(bridge
);
148 regulator_disable(vga
->vdd
);
151 static const struct drm_bridge_funcs dumb_vga_bridge_funcs
= {
152 .attach
= dumb_vga_attach
,
153 .enable
= dumb_vga_enable
,
154 .disable
= dumb_vga_disable
,
157 static struct i2c_adapter
*dumb_vga_retrieve_ddc(struct device
*dev
)
159 struct device_node
*phandle
, *remote
;
160 struct i2c_adapter
*ddc
;
162 remote
= of_graph_get_remote_node(dev
->of_node
, 1, -1);
164 return ERR_PTR(-EINVAL
);
166 phandle
= of_parse_phandle(remote
, "ddc-i2c-bus", 0);
169 return ERR_PTR(-ENODEV
);
171 ddc
= of_get_i2c_adapter_by_node(phandle
);
172 of_node_put(phandle
);
174 return ERR_PTR(-EPROBE_DEFER
);
179 static int dumb_vga_probe(struct platform_device
*pdev
)
181 struct dumb_vga
*vga
;
183 vga
= devm_kzalloc(&pdev
->dev
, sizeof(*vga
), GFP_KERNEL
);
186 platform_set_drvdata(pdev
, vga
);
188 vga
->vdd
= devm_regulator_get_optional(&pdev
->dev
, "vdd");
189 if (IS_ERR(vga
->vdd
)) {
190 int ret
= PTR_ERR(vga
->vdd
);
191 if (ret
== -EPROBE_DEFER
)
192 return -EPROBE_DEFER
;
194 dev_dbg(&pdev
->dev
, "No vdd regulator found: %d\n", ret
);
197 vga
->ddc
= dumb_vga_retrieve_ddc(&pdev
->dev
);
198 if (IS_ERR(vga
->ddc
)) {
199 if (PTR_ERR(vga
->ddc
) == -ENODEV
) {
201 "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
);
223 if (!IS_ERR(vga
->ddc
))
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 .sampling_edge
= DRM_BUS_FLAG_PIXDATA_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 .sampling_edge
= DRM_BUS_FLAG_PIXDATA_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 .sampling_edge
= DRM_BUS_FLAG_PIXDATA_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");