xref: /linux/drivers/gpu/drm/bridge/simple-bridge.c (revision 705c09bb3cdffb141986598ad4ff9c9b0a66c3bd)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2015-2016 Free Electrons
4  * Copyright (C) 2015-2016 NextThing Co
5  *
6  * Maxime Ripard <maxime.ripard@free-electrons.com>
7  */
8 
9 #include <linux/gpio/consumer.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/of_graph.h>
13 #include <linux/platform_device.h>
14 #include <linux/regulator/consumer.h>
15 
16 #include <drm/drm_atomic_helper.h>
17 #include <drm/drm_bridge.h>
18 #include <drm/drm_crtc.h>
19 #include <drm/drm_edid.h>
20 #include <drm/drm_print.h>
21 #include <drm/drm_probe_helper.h>
22 
23 struct simple_bridge_info {
24 	const struct drm_bridge_timings *timings;
25 	unsigned int connector_type;
26 };
27 
28 struct simple_bridge {
29 	struct drm_bridge	bridge;
30 	struct drm_connector	connector;
31 
32 	const struct simple_bridge_info *info;
33 
34 	struct drm_bridge	*next_bridge;
35 	struct regulator	*vdd;
36 	struct gpio_desc	*enable;
37 };
38 
39 static inline struct simple_bridge *
40 drm_bridge_to_simple_bridge(struct drm_bridge *bridge)
41 {
42 	return container_of(bridge, struct simple_bridge, bridge);
43 }
44 
45 static inline struct simple_bridge *
46 drm_connector_to_simple_bridge(struct drm_connector *connector)
47 {
48 	return container_of(connector, struct simple_bridge, connector);
49 }
50 
51 static int simple_bridge_get_modes(struct drm_connector *connector)
52 {
53 	struct simple_bridge *sbridge = drm_connector_to_simple_bridge(connector);
54 	const struct drm_edid *drm_edid;
55 	int ret;
56 
57 	if (sbridge->next_bridge->ops & DRM_BRIDGE_OP_EDID) {
58 		drm_edid = drm_bridge_edid_read(sbridge->next_bridge, connector);
59 		if (!drm_edid)
60 			DRM_INFO("EDID read failed. Fallback to standard modes\n");
61 	} else {
62 		drm_edid = NULL;
63 	}
64 
65 	drm_edid_connector_update(connector, drm_edid);
66 
67 	if (!drm_edid) {
68 		/*
69 		 * In case we cannot retrieve the EDIDs (missing or broken DDC
70 		 * bus from the next bridge), fallback on the XGA standards and
71 		 * prefer a mode pretty much anyone can handle.
72 		 */
73 		ret = drm_add_modes_noedid(connector, 1920, 1200);
74 		drm_set_preferred_mode(connector, 1024, 768);
75 		return ret;
76 	}
77 
78 	ret = drm_edid_connector_add_modes(connector);
79 	drm_edid_free(drm_edid);
80 
81 	return ret;
82 }
83 
84 static const struct drm_connector_helper_funcs simple_bridge_con_helper_funcs = {
85 	.get_modes	= simple_bridge_get_modes,
86 };
87 
88 static enum drm_connector_status
89 simple_bridge_connector_detect(struct drm_connector *connector, bool force)
90 {
91 	struct simple_bridge *sbridge = drm_connector_to_simple_bridge(connector);
92 
93 	return drm_bridge_detect(sbridge->next_bridge);
94 }
95 
96 static const struct drm_connector_funcs simple_bridge_con_funcs = {
97 	.detect			= simple_bridge_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 simple_bridge_attach(struct drm_bridge *bridge,
106 				enum drm_bridge_attach_flags flags)
107 {
108 	struct simple_bridge *sbridge = drm_bridge_to_simple_bridge(bridge);
109 	int ret;
110 
111 	ret = drm_bridge_attach(bridge->encoder, sbridge->next_bridge, bridge,
112 				DRM_BRIDGE_ATTACH_NO_CONNECTOR);
113 	if (ret < 0)
114 		return ret;
115 
116 	if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
117 		return 0;
118 
119 	if (!bridge->encoder) {
120 		DRM_ERROR("Missing encoder\n");
121 		return -ENODEV;
122 	}
123 
124 	drm_connector_helper_add(&sbridge->connector,
125 				 &simple_bridge_con_helper_funcs);
126 	ret = drm_connector_init_with_ddc(bridge->dev, &sbridge->connector,
127 					  &simple_bridge_con_funcs,
128 					  sbridge->info->connector_type,
129 					  sbridge->next_bridge->ddc);
130 	if (ret) {
131 		DRM_ERROR("Failed to initialize connector\n");
132 		return ret;
133 	}
134 
135 	drm_connector_attach_encoder(&sbridge->connector, bridge->encoder);
136 
137 	return 0;
138 }
139 
140 static void simple_bridge_enable(struct drm_bridge *bridge)
141 {
142 	struct simple_bridge *sbridge = drm_bridge_to_simple_bridge(bridge);
143 	int ret;
144 
145 	if (sbridge->vdd) {
146 		ret = regulator_enable(sbridge->vdd);
147 		if (ret)
148 			DRM_ERROR("Failed to enable vdd regulator: %d\n", ret);
149 	}
150 
151 	gpiod_set_value_cansleep(sbridge->enable, 1);
152 }
153 
154 static void simple_bridge_disable(struct drm_bridge *bridge)
155 {
156 	struct simple_bridge *sbridge = drm_bridge_to_simple_bridge(bridge);
157 
158 	gpiod_set_value_cansleep(sbridge->enable, 0);
159 
160 	if (sbridge->vdd)
161 		regulator_disable(sbridge->vdd);
162 }
163 
164 static const struct drm_bridge_funcs simple_bridge_bridge_funcs = {
165 	.attach		= simple_bridge_attach,
166 	.enable		= simple_bridge_enable,
167 	.disable	= simple_bridge_disable,
168 };
169 
170 static int simple_bridge_probe(struct platform_device *pdev)
171 {
172 	struct simple_bridge *sbridge;
173 	struct device_node *remote;
174 
175 	sbridge = devm_kzalloc(&pdev->dev, sizeof(*sbridge), GFP_KERNEL);
176 	if (!sbridge)
177 		return -ENOMEM;
178 	platform_set_drvdata(pdev, sbridge);
179 
180 	sbridge->info = of_device_get_match_data(&pdev->dev);
181 
182 	/* Get the next bridge in the pipeline. */
183 	remote = of_graph_get_remote_node(pdev->dev.of_node, 1, -1);
184 	if (!remote)
185 		return -EINVAL;
186 
187 	sbridge->next_bridge = of_drm_find_bridge(remote);
188 	of_node_put(remote);
189 
190 	if (!sbridge->next_bridge) {
191 		dev_dbg(&pdev->dev, "Next bridge not found, deferring probe\n");
192 		return -EPROBE_DEFER;
193 	}
194 
195 	/* Get the regulator and GPIO resources. */
196 	sbridge->vdd = devm_regulator_get_optional(&pdev->dev, "vdd");
197 	if (IS_ERR(sbridge->vdd)) {
198 		int ret = PTR_ERR(sbridge->vdd);
199 		if (ret == -EPROBE_DEFER)
200 			return -EPROBE_DEFER;
201 		sbridge->vdd = NULL;
202 		dev_dbg(&pdev->dev, "No vdd regulator found: %d\n", ret);
203 	}
204 
205 	sbridge->enable = devm_gpiod_get_optional(&pdev->dev, "enable",
206 						  GPIOD_OUT_LOW);
207 	if (IS_ERR(sbridge->enable))
208 		return dev_err_probe(&pdev->dev, PTR_ERR(sbridge->enable),
209 				     "Unable to retrieve enable GPIO\n");
210 
211 	/* Register the bridge. */
212 	sbridge->bridge.funcs = &simple_bridge_bridge_funcs;
213 	sbridge->bridge.of_node = pdev->dev.of_node;
214 	sbridge->bridge.timings = sbridge->info->timings;
215 
216 	drm_bridge_add(&sbridge->bridge);
217 
218 	return 0;
219 }
220 
221 static void simple_bridge_remove(struct platform_device *pdev)
222 {
223 	struct simple_bridge *sbridge = platform_get_drvdata(pdev);
224 
225 	drm_bridge_remove(&sbridge->bridge);
226 }
227 
228 /*
229  * We assume the ADV7123 DAC is the "default" for historical reasons
230  * Information taken from the ADV7123 datasheet, revision D.
231  * NOTE: the ADV7123EP seems to have other timings and need a new timings
232  * set if used.
233  */
234 static const struct drm_bridge_timings default_bridge_timings = {
235 	/* Timing specifications, datasheet page 7 */
236 	.input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE,
237 	.setup_time_ps = 500,
238 	.hold_time_ps = 1500,
239 };
240 
241 /*
242  * Information taken from the THS8134, THS8134A, THS8134B datasheet named
243  * "SLVS205D", dated May 1990, revised March 2000.
244  */
245 static const struct drm_bridge_timings ti_ths8134_bridge_timings = {
246 	/* From timing diagram, datasheet page 9 */
247 	.input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE,
248 	/* From datasheet, page 12 */
249 	.setup_time_ps = 3000,
250 	/* I guess this means latched input */
251 	.hold_time_ps = 0,
252 };
253 
254 /*
255  * Information taken from the THS8135 datasheet named "SLAS343B", dated
256  * May 2001, revised April 2013.
257  */
258 static const struct drm_bridge_timings ti_ths8135_bridge_timings = {
259 	/* From timing diagram, datasheet page 14 */
260 	.input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE,
261 	/* From datasheet, page 16 */
262 	.setup_time_ps = 2000,
263 	.hold_time_ps = 500,
264 };
265 
266 static const struct of_device_id simple_bridge_match[] = {
267 	{
268 		.compatible = "dumb-vga-dac",
269 		.data = &(const struct simple_bridge_info) {
270 			.connector_type = DRM_MODE_CONNECTOR_VGA,
271 		},
272 	}, {
273 		.compatible = "adi,adv7123",
274 		.data = &(const struct simple_bridge_info) {
275 			.timings = &default_bridge_timings,
276 			.connector_type = DRM_MODE_CONNECTOR_VGA,
277 		},
278 	}, {
279 		.compatible = "ti,opa362",
280 		.data = &(const struct simple_bridge_info) {
281 			.connector_type = DRM_MODE_CONNECTOR_Composite,
282 		},
283 	}, {
284 		.compatible = "ti,ths8135",
285 		.data = &(const struct simple_bridge_info) {
286 			.timings = &ti_ths8135_bridge_timings,
287 			.connector_type = DRM_MODE_CONNECTOR_VGA,
288 		},
289 	}, {
290 		.compatible = "ti,ths8134",
291 		.data = &(const struct simple_bridge_info) {
292 			.timings = &ti_ths8134_bridge_timings,
293 			.connector_type = DRM_MODE_CONNECTOR_VGA,
294 		},
295 	},
296 	{},
297 };
298 MODULE_DEVICE_TABLE(of, simple_bridge_match);
299 
300 static struct platform_driver simple_bridge_driver = {
301 	.probe	= simple_bridge_probe,
302 	.remove_new = simple_bridge_remove,
303 	.driver		= {
304 		.name		= "simple-bridge",
305 		.of_match_table	= simple_bridge_match,
306 	},
307 };
308 module_platform_driver(simple_bridge_driver);
309 
310 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
311 MODULE_DESCRIPTION("Simple DRM bridge driver");
312 MODULE_LICENSE("GPL");
313