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