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_graph.h>
17 #include <drm/drm_atomic_helper.h>
18 #include <drm/drm_crtc.h>
19 #include <drm/drm_crtc_helper.h>
22 struct drm_bridge bridge
;
23 struct drm_connector connector
;
25 struct i2c_adapter
*ddc
;
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_mode_connector_update_edid_property(connector
, edid
);
56 return drm_add_edid_modes(connector
, edid
);
60 * In case we cannot retrieve the EDIDs (broken or missing i2c
61 * bus), fallback on the XGA standards
63 ret
= drm_add_modes_noedid(connector
, 1920, 1200);
65 /* And prefer a mode pretty much anyone can handle */
66 drm_set_preferred_mode(connector
, 1024, 768);
71 static const struct drm_connector_helper_funcs dumb_vga_con_helper_funcs
= {
72 .get_modes
= dumb_vga_get_modes
,
75 static enum drm_connector_status
76 dumb_vga_connector_detect(struct drm_connector
*connector
, bool force
)
78 struct dumb_vga
*vga
= drm_connector_to_dumb_vga(connector
);
81 * Even if we have an I2C bus, we can't assume that the cable
82 * is disconnected if drm_probe_ddc fails. Some cables don't
83 * wire the DDC pins, or the I2C bus might not be working at
86 if (!IS_ERR(vga
->ddc
) && drm_probe_ddc(vga
->ddc
))
87 return connector_status_connected
;
89 return connector_status_unknown
;
92 static const struct drm_connector_funcs dumb_vga_con_funcs
= {
93 .dpms
= drm_atomic_helper_connector_dpms
,
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
,
102 static int dumb_vga_attach(struct drm_bridge
*bridge
)
104 struct dumb_vga
*vga
= drm_bridge_to_dumb_vga(bridge
);
107 if (!bridge
->encoder
) {
108 DRM_ERROR("Missing encoder\n");
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
);
117 DRM_ERROR("Failed to initialize connector\n");
121 drm_mode_connector_attach_encoder(&vga
->connector
,
127 static const struct drm_bridge_funcs dumb_vga_bridge_funcs
= {
128 .attach
= dumb_vga_attach
,
131 static struct i2c_adapter
*dumb_vga_retrieve_ddc(struct device
*dev
)
133 struct device_node
*end_node
, *phandle
, *remote
;
134 struct i2c_adapter
*ddc
;
136 end_node
= of_graph_get_endpoint_by_regs(dev
->of_node
, 1, -1);
138 dev_err(dev
, "Missing connector endpoint\n");
139 return ERR_PTR(-ENODEV
);
142 remote
= of_graph_get_remote_port_parent(end_node
);
143 of_node_put(end_node
);
145 dev_err(dev
, "Enable to parse remote node\n");
146 return ERR_PTR(-EINVAL
);
149 phandle
= of_parse_phandle(remote
, "ddc-i2c-bus", 0);
152 return ERR_PTR(-ENODEV
);
154 ddc
= of_get_i2c_adapter_by_node(phandle
);
155 of_node_put(phandle
);
157 return ERR_PTR(-EPROBE_DEFER
);
162 static int dumb_vga_probe(struct platform_device
*pdev
)
164 struct dumb_vga
*vga
;
167 vga
= devm_kzalloc(&pdev
->dev
, sizeof(*vga
), GFP_KERNEL
);
170 platform_set_drvdata(pdev
, vga
);
172 vga
->ddc
= dumb_vga_retrieve_ddc(&pdev
->dev
);
173 if (IS_ERR(vga
->ddc
)) {
174 if (PTR_ERR(vga
->ddc
) == -ENODEV
) {
176 "No i2c bus specified. Disabling EDID readout\n");
178 dev_err(&pdev
->dev
, "Couldn't retrieve i2c bus\n");
179 return PTR_ERR(vga
->ddc
);
183 vga
->bridge
.funcs
= &dumb_vga_bridge_funcs
;
184 vga
->bridge
.of_node
= pdev
->dev
.of_node
;
186 ret
= drm_bridge_add(&vga
->bridge
);
187 if (ret
&& !IS_ERR(vga
->ddc
))
188 i2c_put_adapter(vga
->ddc
);
193 static int dumb_vga_remove(struct platform_device
*pdev
)
195 struct dumb_vga
*vga
= platform_get_drvdata(pdev
);
197 drm_bridge_remove(&vga
->bridge
);
199 if (!IS_ERR(vga
->ddc
))
200 i2c_put_adapter(vga
->ddc
);
205 static const struct of_device_id dumb_vga_match
[] = {
206 { .compatible
= "dumb-vga-dac" },
209 MODULE_DEVICE_TABLE(of
, dumb_vga_match
);
211 static struct platform_driver dumb_vga_driver
= {
212 .probe
= dumb_vga_probe
,
213 .remove
= dumb_vga_remove
,
215 .name
= "dumb-vga-dac",
216 .of_match_table
= dumb_vga_match
,
219 module_platform_driver(dumb_vga_driver
);
221 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
222 MODULE_DESCRIPTION("Dumb VGA DAC bridge driver");
223 MODULE_LICENSE("GPL");