xref: /linux/drivers/gpu/drm/bridge/display-connector.c (revision edbafe65eef2b58625db1e113fbbfb1fe10c0291)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2019 Laurent Pinchart <laurent.pinchart@ideasonboard.com>
4  */
5 
6 #include <linux/gpio/consumer.h>
7 #include <linux/i2c.h>
8 #include <linux/interrupt.h>
9 #include <linux/media-bus-format.h>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/of.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_edid.h>
19 
20 struct display_connector {
21 	struct drm_bridge	bridge;
22 
23 	struct gpio_desc	*hpd_gpio;
24 	int			hpd_irq;
25 
26 	struct regulator	*supply;
27 	struct gpio_desc	*ddc_en;
28 };
29 
30 static inline struct display_connector *
31 to_display_connector(struct drm_bridge *bridge)
32 {
33 	return container_of(bridge, struct display_connector, bridge);
34 }
35 
36 static int display_connector_attach(struct drm_bridge *bridge,
37 				    struct drm_encoder *encoder,
38 				    enum drm_bridge_attach_flags flags)
39 {
40 	return flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR ? 0 : -EINVAL;
41 }
42 
43 static void display_connector_destroy(struct drm_bridge *bridge)
44 {
45 	struct display_connector *conn = to_display_connector(bridge);
46 
47 	i2c_put_adapter(conn->bridge.ddc);
48 }
49 
50 static enum drm_connector_status display_connector_detect(struct drm_bridge *bridge)
51 {
52 	struct display_connector *conn = to_display_connector(bridge);
53 
54 	if (conn->hpd_gpio) {
55 		if (gpiod_get_value_cansleep(conn->hpd_gpio))
56 			return connector_status_connected;
57 		else
58 			return connector_status_disconnected;
59 	}
60 
61 	if (conn->bridge.ddc && drm_probe_ddc(conn->bridge.ddc))
62 		return connector_status_connected;
63 
64 	switch (conn->bridge.type) {
65 	case DRM_MODE_CONNECTOR_DVIA:
66 	case DRM_MODE_CONNECTOR_DVID:
67 	case DRM_MODE_CONNECTOR_DVII:
68 	case DRM_MODE_CONNECTOR_HDMIA:
69 	case DRM_MODE_CONNECTOR_HDMIB:
70 		/*
71 		 * For DVI and HDMI connectors a DDC probe failure indicates
72 		 * that no cable is connected.
73 		 */
74 		return connector_status_disconnected;
75 
76 	case DRM_MODE_CONNECTOR_Composite:
77 	case DRM_MODE_CONNECTOR_SVIDEO:
78 	case DRM_MODE_CONNECTOR_VGA:
79 	default:
80 		/*
81 		 * Composite and S-Video connectors have no other detection
82 		 * mean than the HPD GPIO. For VGA connectors, even if we have
83 		 * an I2C bus, we can't assume that the cable is disconnected
84 		 * if drm_probe_ddc fails, as some cables don't wire the DDC
85 		 * pins.
86 		 */
87 		return connector_status_unknown;
88 	}
89 }
90 
91 static enum drm_connector_status
92 display_connector_bridge_detect(struct drm_bridge *bridge, struct drm_connector *connector)
93 {
94 	return display_connector_detect(bridge);
95 }
96 
97 static const struct drm_edid *display_connector_edid_read(struct drm_bridge *bridge,
98 							  struct drm_connector *connector)
99 {
100 	struct display_connector *conn = to_display_connector(bridge);
101 
102 	return drm_edid_read_ddc(connector, conn->bridge.ddc);
103 }
104 
105 /*
106  * Since this bridge is tied to the connector, it acts like a passthrough,
107  * so concerning the output bus formats, either pass the bus formats from the
108  * previous bridge or return fallback data like done in the bridge function:
109  * drm_atomic_bridge_chain_select_bus_fmts().
110  * This supports negotiation if the bridge chain has all bits in place.
111  */
112 static u32 *display_connector_get_output_bus_fmts(struct drm_bridge *bridge,
113 					struct drm_bridge_state *bridge_state,
114 					struct drm_crtc_state *crtc_state,
115 					struct drm_connector_state *conn_state,
116 					unsigned int *num_output_fmts)
117 {
118 	struct drm_bridge *prev_bridge __free(drm_bridge_put) = drm_bridge_get_prev_bridge(bridge);
119 	struct drm_bridge_state *prev_bridge_state;
120 
121 	if (!prev_bridge || !prev_bridge->funcs->atomic_get_output_bus_fmts) {
122 		struct drm_connector *conn = conn_state->connector;
123 		u32 *out_bus_fmts;
124 
125 		*num_output_fmts = 1;
126 		out_bus_fmts = kmalloc_obj(*out_bus_fmts);
127 		if (!out_bus_fmts)
128 			return NULL;
129 
130 		if (conn->display_info.num_bus_formats &&
131 		    conn->display_info.bus_formats)
132 			out_bus_fmts[0] = conn->display_info.bus_formats[0];
133 		else
134 			out_bus_fmts[0] = MEDIA_BUS_FMT_FIXED;
135 
136 		return out_bus_fmts;
137 	}
138 
139 	prev_bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,
140 							    prev_bridge);
141 
142 	return prev_bridge->funcs->atomic_get_output_bus_fmts(prev_bridge, prev_bridge_state,
143 							      crtc_state, conn_state,
144 							      num_output_fmts);
145 }
146 
147 /*
148  * Since this bridge is tied to the connector, it acts like a passthrough,
149  * so concerning the input bus formats, either pass the bus formats from the
150  * previous bridge or MEDIA_BUS_FMT_FIXED (like select_bus_fmt_recursive())
151  * when atomic_get_input_bus_fmts is not supported.
152  * This supports negotiation if the bridge chain has all bits in place.
153  */
154 static u32 *display_connector_get_input_bus_fmts(struct drm_bridge *bridge,
155 					struct drm_bridge_state *bridge_state,
156 					struct drm_crtc_state *crtc_state,
157 					struct drm_connector_state *conn_state,
158 					u32 output_fmt,
159 					unsigned int *num_input_fmts)
160 {
161 	struct drm_bridge *prev_bridge __free(drm_bridge_put) = drm_bridge_get_prev_bridge(bridge);
162 	struct drm_bridge_state *prev_bridge_state;
163 
164 	if (!prev_bridge || !prev_bridge->funcs->atomic_get_input_bus_fmts) {
165 		u32 *in_bus_fmts;
166 
167 		*num_input_fmts = 1;
168 		in_bus_fmts = kmalloc_obj(*in_bus_fmts);
169 		if (!in_bus_fmts)
170 			return NULL;
171 
172 		in_bus_fmts[0] = MEDIA_BUS_FMT_FIXED;
173 
174 		return in_bus_fmts;
175 	}
176 
177 	prev_bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,
178 							    prev_bridge);
179 
180 	return prev_bridge->funcs->atomic_get_input_bus_fmts(prev_bridge, prev_bridge_state,
181 							     crtc_state, conn_state, output_fmt,
182 							     num_input_fmts);
183 }
184 
185 static const struct drm_bridge_funcs display_connector_bridge_funcs = {
186 	.attach = display_connector_attach,
187 	.destroy = display_connector_destroy,
188 	.detect = display_connector_bridge_detect,
189 	.edid_read = display_connector_edid_read,
190 	.atomic_get_output_bus_fmts = display_connector_get_output_bus_fmts,
191 	.atomic_get_input_bus_fmts = display_connector_get_input_bus_fmts,
192 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
193 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
194 	.atomic_reset = drm_atomic_helper_bridge_reset,
195 };
196 
197 static irqreturn_t display_connector_hpd_irq(int irq, void *arg)
198 {
199 	struct display_connector *conn = arg;
200 	struct drm_bridge *bridge = &conn->bridge;
201 
202 	drm_bridge_hpd_notify(bridge, display_connector_detect(bridge));
203 
204 	return IRQ_HANDLED;
205 }
206 
207 static int display_connector_get_supply(struct platform_device *pdev,
208 					struct display_connector *conn,
209 					const char *name)
210 {
211 	conn->supply = devm_regulator_get_optional(&pdev->dev, name);
212 
213 	if (conn->supply == ERR_PTR(-ENODEV))
214 		conn->supply = NULL;
215 
216 	return PTR_ERR_OR_ZERO(conn->supply);
217 }
218 
219 static int display_connector_probe(struct platform_device *pdev)
220 {
221 	struct display_connector *conn;
222 	unsigned int type;
223 	const char *label = NULL;
224 	int ret;
225 
226 	conn = devm_drm_bridge_alloc(&pdev->dev, struct display_connector, bridge,
227 				     &display_connector_bridge_funcs);
228 	if (IS_ERR(conn))
229 		return PTR_ERR(conn);
230 
231 	platform_set_drvdata(pdev, conn);
232 
233 	type = (uintptr_t)of_device_get_match_data(&pdev->dev);
234 
235 	/* Get the exact connector type. */
236 	switch (type) {
237 	case DRM_MODE_CONNECTOR_DVII: {
238 		bool analog, digital;
239 
240 		analog = of_property_read_bool(pdev->dev.of_node, "analog");
241 		digital = of_property_read_bool(pdev->dev.of_node, "digital");
242 		if (analog && !digital) {
243 			conn->bridge.type = DRM_MODE_CONNECTOR_DVIA;
244 		} else if (!analog && digital) {
245 			conn->bridge.type = DRM_MODE_CONNECTOR_DVID;
246 		} else if (analog && digital) {
247 			conn->bridge.type = DRM_MODE_CONNECTOR_DVII;
248 		} else {
249 			dev_err(&pdev->dev, "DVI connector with no type\n");
250 			return -EINVAL;
251 		}
252 		break;
253 	}
254 
255 	case DRM_MODE_CONNECTOR_HDMIA: {
256 		const char *hdmi_type;
257 
258 		ret = of_property_read_string(pdev->dev.of_node, "type",
259 					      &hdmi_type);
260 		if (ret < 0) {
261 			dev_err(&pdev->dev, "HDMI connector with no type\n");
262 			return -EINVAL;
263 		}
264 
265 		if (!strcmp(hdmi_type, "a") || !strcmp(hdmi_type, "c") ||
266 		    !strcmp(hdmi_type, "d") || !strcmp(hdmi_type, "e")) {
267 			conn->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
268 		} else if (!strcmp(hdmi_type, "b")) {
269 			conn->bridge.type = DRM_MODE_CONNECTOR_HDMIB;
270 		} else {
271 			dev_err(&pdev->dev,
272 				"Unsupported HDMI connector type '%s'\n",
273 				hdmi_type);
274 			return -EINVAL;
275 		}
276 
277 		break;
278 	}
279 
280 	default:
281 		conn->bridge.type = type;
282 		break;
283 	}
284 
285 	/* All the supported connector types support interlaced modes. */
286 	conn->bridge.interlace_allowed = true;
287 
288 	if (type == DRM_MODE_CONNECTOR_HDMIA ||
289 	    type == DRM_MODE_CONNECTOR_DisplayPort)
290 		conn->bridge.ycbcr_420_allowed = true;
291 
292 	/* Get the optional connector label. */
293 	of_property_read_string(pdev->dev.of_node, "label", &label);
294 
295 	/*
296 	 * Get the HPD GPIO for DVI, HDMI and DP connectors. If the GPIO can provide
297 	 * edge interrupts, register an interrupt handler.
298 	 */
299 	if (type == DRM_MODE_CONNECTOR_DVII ||
300 	    type == DRM_MODE_CONNECTOR_HDMIA ||
301 	    type == DRM_MODE_CONNECTOR_DisplayPort) {
302 		conn->hpd_gpio = devm_gpiod_get_optional(&pdev->dev, "hpd",
303 							 GPIOD_IN);
304 		if (IS_ERR(conn->hpd_gpio))
305 			return dev_err_probe(&pdev->dev, PTR_ERR(conn->hpd_gpio),
306 					     "Unable to retrieve HPD GPIO\n");
307 
308 		conn->hpd_irq = gpiod_to_irq(conn->hpd_gpio);
309 	} else {
310 		conn->hpd_irq = -EINVAL;
311 	}
312 
313 	if (conn->hpd_irq >= 0) {
314 		ret = devm_request_threaded_irq(&pdev->dev, conn->hpd_irq,
315 						NULL, display_connector_hpd_irq,
316 						IRQF_TRIGGER_RISING |
317 						IRQF_TRIGGER_FALLING |
318 						IRQF_ONESHOT,
319 						"HPD", conn);
320 		if (ret) {
321 			dev_info(&pdev->dev,
322 				 "Failed to request HPD edge interrupt, falling back to polling\n");
323 			conn->hpd_irq = -EINVAL;
324 		}
325 	}
326 
327 	/* Retrieve the DDC I2C adapter for DVI, HDMI and VGA connectors. */
328 	if (type == DRM_MODE_CONNECTOR_DVII ||
329 	    type == DRM_MODE_CONNECTOR_HDMIA ||
330 	    type == DRM_MODE_CONNECTOR_VGA) {
331 		struct device_node *phandle;
332 
333 		phandle = of_parse_phandle(pdev->dev.of_node, "ddc-i2c-bus", 0);
334 		if (phandle) {
335 			conn->bridge.ddc = of_get_i2c_adapter_by_node(phandle);
336 			of_node_put(phandle);
337 			if (!conn->bridge.ddc)
338 				return -EPROBE_DEFER;
339 		} else {
340 			dev_dbg(&pdev->dev,
341 				"No I2C bus specified, disabling EDID readout\n");
342 		}
343 	}
344 
345 	/* Get the DP PWR for DP connector. */
346 	if (type == DRM_MODE_CONNECTOR_DisplayPort) {
347 		int ret;
348 
349 		ret = display_connector_get_supply(pdev, conn, "dp-pwr");
350 		if (ret < 0)
351 			return dev_err_probe(&pdev->dev, ret, "failed to get DP PWR regulator\n");
352 	}
353 
354 	/* enable DDC */
355 	if (type == DRM_MODE_CONNECTOR_HDMIA) {
356 		int ret;
357 
358 		conn->ddc_en = devm_gpiod_get_optional(&pdev->dev, "ddc-en",
359 						       GPIOD_OUT_HIGH);
360 
361 		if (IS_ERR(conn->ddc_en)) {
362 			dev_err(&pdev->dev, "Couldn't get ddc-en gpio\n");
363 			return PTR_ERR(conn->ddc_en);
364 		}
365 
366 		ret = display_connector_get_supply(pdev, conn, "hdmi-pwr");
367 		if (ret < 0)
368 			return dev_err_probe(&pdev->dev, ret, "failed to get HDMI +5V Power regulator\n");
369 	}
370 
371 	if (conn->supply) {
372 		ret = regulator_enable(conn->supply);
373 		if (ret) {
374 			dev_err(&pdev->dev, "failed to enable PWR regulator: %d\n", ret);
375 			return ret;
376 		}
377 	}
378 
379 	conn->bridge.of_node = pdev->dev.of_node;
380 
381 	if (conn->bridge.ddc)
382 		conn->bridge.ops |= DRM_BRIDGE_OP_EDID
383 				 |  DRM_BRIDGE_OP_DETECT;
384 	/* Detecting the monitor requires reading DPCD */
385 	if (conn->hpd_gpio && type != DRM_MODE_CONNECTOR_DisplayPort)
386 		conn->bridge.ops |= DRM_BRIDGE_OP_DETECT;
387 	if (conn->hpd_irq >= 0)
388 		conn->bridge.ops |= DRM_BRIDGE_OP_HPD;
389 
390 	dev_dbg(&pdev->dev,
391 		"Found %s display connector '%s' %s DDC bus and %s HPD GPIO (ops 0x%x)\n",
392 		drm_get_connector_type_name(conn->bridge.type),
393 		label ? label : "<unlabelled>",
394 		conn->bridge.ddc ? "with" : "without",
395 		conn->hpd_gpio ? "with" : "without",
396 		conn->bridge.ops);
397 
398 	drm_bridge_add(&conn->bridge);
399 
400 	return 0;
401 }
402 
403 static void display_connector_remove(struct platform_device *pdev)
404 {
405 	struct display_connector *conn = platform_get_drvdata(pdev);
406 
407 	if (conn->ddc_en)
408 		gpiod_set_value(conn->ddc_en, 0);
409 
410 	if (conn->supply)
411 		regulator_disable(conn->supply);
412 
413 	drm_bridge_remove(&conn->bridge);
414 }
415 
416 static const struct of_device_id display_connector_match[] = {
417 	{
418 		.compatible = "composite-video-connector",
419 		.data = (void *)DRM_MODE_CONNECTOR_Composite,
420 	}, {
421 		.compatible = "dvi-connector",
422 		.data = (void *)DRM_MODE_CONNECTOR_DVII,
423 	}, {
424 		.compatible = "hdmi-connector",
425 		.data = (void *)DRM_MODE_CONNECTOR_HDMIA,
426 	}, {
427 		.compatible = "svideo-connector",
428 		.data = (void *)DRM_MODE_CONNECTOR_SVIDEO,
429 	}, {
430 		.compatible = "vga-connector",
431 		.data = (void *)DRM_MODE_CONNECTOR_VGA,
432 	}, {
433 		.compatible = "dp-connector",
434 		.data = (void *)DRM_MODE_CONNECTOR_DisplayPort,
435 	},
436 	{},
437 };
438 MODULE_DEVICE_TABLE(of, display_connector_match);
439 
440 static struct platform_driver display_connector_driver = {
441 	.probe	= display_connector_probe,
442 	.remove = display_connector_remove,
443 	.driver		= {
444 		.name		= "display-connector",
445 		.of_match_table	= display_connector_match,
446 	},
447 };
448 module_platform_driver(display_connector_driver);
449 
450 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
451 MODULE_DESCRIPTION("Display connector driver");
452 MODULE_LICENSE("GPL");
453