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