Linux Audio

Check our new training course

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