All of lore.kernel.org
 help / color / mirror / Atom feed
From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
To: dri-devel@lists.freedesktop.org
Cc: Tomi Valkeinen <tomi.valkeinen@ti.com>,
	Sam Ravnborg <sam@ravnborg.org>,
	Sebastian Reichel <sebastian.reichel@collabora.com>,
	Boris Brezillon <bbrezillon@kernel.org>
Subject: [PATCH v5 12/52] drm/bridge: Add bridge driver for display connectors
Date: Fri, 24 Jan 2020 05:54:05 +0200	[thread overview]
Message-ID: <20200124035445.1830-13-laurent.pinchart@ideasonboard.com> (raw)
In-Reply-To: <20200124035445.1830-1-laurent.pinchart@ideasonboard.com>

Display connectors are modelled in DT as a device node, but have so far
been handled manually in several bridge drivers. This resulted in
duplicate code in several bridge drivers, with slightly different (and
thus confusing) logics.

In order to fix this, implement a bridge driver for display connectors.
The driver centralises logic for the DVI, HDMI, VGAn composite and
S-video connectors and exposes corresponding bridge operations.

This driver in itself doesn't solve the issue completely, changes in
bridge and display controller drivers are needed to make use of the new
connector driver.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Maxime Ripard <mripard@kernel.org>
Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com>
Acked-by: Sam Ravnborg <sam@ravnborg.org>
---
Changes since v3:

- Turn probe confirmation message into a dev_dbg()

Changes since v2:

- Fall back to polling if the GPIO IRQ chip doesn't support
  edge-triggered interrupts

Changes since v1:

- Use drm_get_connector_type_name() instead of open-coding
  display_connector_type_name()
- Remove empty .hpd_enable() and .hpd_disable() operations
- Set bridge.ddc
---
 drivers/gpu/drm/bridge/Kconfig             |  11 +
 drivers/gpu/drm/bridge/Makefile            |   1 +
 drivers/gpu/drm/bridge/display-connector.c | 292 +++++++++++++++++++++
 3 files changed, 304 insertions(+)
 create mode 100644 drivers/gpu/drm/bridge/display-connector.c

diff --git a/drivers/gpu/drm/bridge/Kconfig b/drivers/gpu/drm/bridge/Kconfig
index 09b2ca59576e..f69e7f52013e 100644
--- a/drivers/gpu/drm/bridge/Kconfig
+++ b/drivers/gpu/drm/bridge/Kconfig
@@ -27,6 +27,17 @@ config DRM_CDNS_DSI
 	  Support Cadence DPI to DSI bridge. This is an internal
 	  bridge and is meant to be directly embedded in a SoC.
 
+config DRM_DISPLAY_CONNECTOR
+	tristate "Display connector support"
+	depends on OF
+	help
+	  Driver for display connectors with support for DDC and hot-plug
+	  detection. Most display controller handle display connectors
+	  internally and don't need this driver, but the DRM subsystem is
+	  moving towards separating connector handling from display controllers
+	  on ARM-based platforms. Saying Y here when this driver is not needed
+	  will not cause any issue.
+
 config DRM_LVDS_CODEC
 	tristate "Transparent LVDS encoders and decoders support"
 	depends on OF
diff --git a/drivers/gpu/drm/bridge/Makefile b/drivers/gpu/drm/bridge/Makefile
index 85d4d8336219..f77b34854b78 100644
--- a/drivers/gpu/drm/bridge/Makefile
+++ b/drivers/gpu/drm/bridge/Makefile
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_DRM_CDNS_DSI) += cdns-dsi.o
+obj-$(CONFIG_DRM_DISPLAY_CONNECTOR) += display-connector.o
 obj-$(CONFIG_DRM_LVDS_CODEC) += lvds-codec.o
 obj-$(CONFIG_DRM_MEGACHIPS_STDPXXXX_GE_B850V3_FW) += megachips-stdpxxxx-ge-b850v3-fw.o
 obj-$(CONFIG_DRM_NXP_PTN3460) += nxp-ptn3460.o
diff --git a/drivers/gpu/drm/bridge/display-connector.c b/drivers/gpu/drm/bridge/display-connector.c
new file mode 100644
index 000000000000..cce0eaec38df
--- /dev/null
+++ b/drivers/gpu/drm/bridge/display-connector.c
@@ -0,0 +1,292 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2019 Laurent Pinchart <laurent.pinchart@ideasonboard.com>
+ */
+
+#include <linux/gpio/consumer.h>
+#include <linux/i2c.h>
+#include <linux/interrupt.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+
+#include <drm/drm_bridge.h>
+#include <drm/drm_edid.h>
+
+struct display_connector {
+	struct drm_bridge	bridge;
+
+	struct gpio_desc	*hpd_gpio;
+	int			hpd_irq;
+};
+
+static inline struct display_connector *
+to_display_connector(struct drm_bridge *bridge)
+{
+	return container_of(bridge, struct display_connector, bridge);
+}
+
+static int display_connector_attach(struct drm_bridge *bridge,
+				    enum drm_bridge_attach_flags flags)
+{
+	return flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR ? 0 : -EINVAL;
+}
+
+static enum drm_connector_status
+display_connector_detect(struct drm_bridge *bridge)
+{
+	struct display_connector *conn = to_display_connector(bridge);
+
+	if (conn->hpd_gpio) {
+		if (gpiod_get_value_cansleep(conn->hpd_gpio))
+			return connector_status_connected;
+		else
+			return connector_status_disconnected;
+	}
+
+	if (conn->bridge.ddc && drm_probe_ddc(conn->bridge.ddc))
+		return connector_status_connected;
+
+	switch (conn->bridge.type) {
+	case DRM_MODE_CONNECTOR_DVIA:
+	case DRM_MODE_CONNECTOR_DVID:
+	case DRM_MODE_CONNECTOR_DVII:
+	case DRM_MODE_CONNECTOR_HDMIA:
+	case DRM_MODE_CONNECTOR_HDMIB:
+		/*
+		 * For DVI and HDMI connectors a DDC probe failure indicates
+		 * that no cable is connected.
+		 */
+		return connector_status_disconnected;
+
+	case DRM_MODE_CONNECTOR_Composite:
+	case DRM_MODE_CONNECTOR_SVIDEO:
+	case DRM_MODE_CONNECTOR_VGA:
+	default:
+		/*
+		 * Composite and S-Video connectors have no other detection
+		 * mean than the HPD GPIO. For VGA connectors, even if we have
+		 * an I2C bus, we can't assume that the cable is disconnected
+		 * if drm_probe_ddc fails, as some cables don't wire the DDC
+		 * pins.
+		 */
+		return connector_status_unknown;
+	}
+}
+
+static struct edid *display_connector_get_edid(struct drm_bridge *bridge,
+					       struct drm_connector *connector)
+{
+	struct display_connector *conn = to_display_connector(bridge);
+
+	return drm_get_edid(connector, conn->bridge.ddc);
+}
+
+static const struct drm_bridge_funcs display_connector_bridge_funcs = {
+	.attach = display_connector_attach,
+	.detect = display_connector_detect,
+	.get_edid = display_connector_get_edid,
+};
+
+static irqreturn_t display_connector_hpd_irq(int irq, void *arg)
+{
+	struct display_connector *conn = arg;
+	struct drm_bridge *bridge = &conn->bridge;
+
+	drm_bridge_hpd_notify(bridge, display_connector_detect(bridge));
+
+	return IRQ_HANDLED;
+}
+
+static int display_connector_probe(struct platform_device *pdev)
+{
+	struct display_connector *conn;
+	unsigned int type;
+	const char *label;
+	int ret;
+
+	conn = devm_kzalloc(&pdev->dev, sizeof(*conn), GFP_KERNEL);
+	if (!conn)
+		return -ENOMEM;
+
+	platform_set_drvdata(pdev, conn);
+
+	type = (uintptr_t)of_device_get_match_data(&pdev->dev);
+
+	/* Get the exact connector type. */
+	switch (type) {
+	case DRM_MODE_CONNECTOR_DVII: {
+		bool analog, digital;
+
+		analog = of_property_read_bool(pdev->dev.of_node, "analog");
+		digital = of_property_read_bool(pdev->dev.of_node, "digital");
+		if (analog && !digital) {
+			conn->bridge.type = DRM_MODE_CONNECTOR_DVIA;
+		} else if (!analog && digital) {
+			conn->bridge.type = DRM_MODE_CONNECTOR_DVID;
+		} else if (analog && digital) {
+			conn->bridge.type = DRM_MODE_CONNECTOR_DVII;
+		} else {
+			dev_err(&pdev->dev, "DVI connector with no type\n");
+			return -EINVAL;
+		}
+		break;
+	}
+
+	case DRM_MODE_CONNECTOR_HDMIA: {
+		const char *hdmi_type;
+
+		ret = of_property_read_string(pdev->dev.of_node, "type",
+					      &hdmi_type);
+		if (ret < 0) {
+			dev_err(&pdev->dev, "HDMI connector with no type\n");
+			return -EINVAL;
+		}
+
+		if (!strcmp(hdmi_type, "a") || !strcmp(hdmi_type, "c") ||
+		    !strcmp(hdmi_type, "d") || !strcmp(hdmi_type, "e")) {
+			conn->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
+		} else if (!strcmp(hdmi_type, "b")) {
+			conn->bridge.type = DRM_MODE_CONNECTOR_HDMIB;
+		} else {
+			dev_err(&pdev->dev,
+				"Unsupported HDMI connector type '%s'\n",
+				hdmi_type);
+			return -EINVAL;
+		}
+
+		break;
+	}
+
+	default:
+		conn->bridge.type = type;
+		break;
+	}
+
+	/* Get the optional connector label. */
+	of_property_read_string(pdev->dev.of_node, "label", &label);
+
+	/*
+	 * Get the HPD GPIO for DVI and HDMI connectors. If the GPIO can provide
+	 * edge interrupts, register an interrupt handler.
+	 */
+	if (type == DRM_MODE_CONNECTOR_DVII ||
+	    type == DRM_MODE_CONNECTOR_HDMIA) {
+		conn->hpd_gpio = devm_gpiod_get_optional(&pdev->dev, "hpd",
+							 GPIOD_IN);
+		if (IS_ERR(conn->hpd_gpio)) {
+			if (PTR_ERR(conn->hpd_gpio) != -EPROBE_DEFER)
+				dev_err(&pdev->dev,
+					"Unable to retrieve HPD GPIO\n");
+			return PTR_ERR(conn->hpd_gpio);
+		}
+
+		conn->hpd_irq = gpiod_to_irq(conn->hpd_gpio);
+	} else {
+		conn->hpd_irq = -EINVAL;
+	}
+
+	if (conn->hpd_irq >= 0) {
+		ret = devm_request_threaded_irq(&pdev->dev, conn->hpd_irq,
+						NULL, display_connector_hpd_irq,
+						IRQF_TRIGGER_RISING |
+						IRQF_TRIGGER_FALLING |
+						IRQF_ONESHOT,
+						"HPD", conn);
+		if (ret) {
+			dev_info(&pdev->dev,
+				 "Failed to request HPD edge interrupt, falling back to polling\n");
+			conn->hpd_irq = -EINVAL;
+		}
+	}
+
+	/* Retrieve the DDC I2C adapter for DVI, HDMI and VGA connectors. */
+	if (type == DRM_MODE_CONNECTOR_DVII ||
+	    type == DRM_MODE_CONNECTOR_HDMIA ||
+	    type == DRM_MODE_CONNECTOR_VGA) {
+		struct device_node *phandle;
+
+		phandle = of_parse_phandle(pdev->dev.of_node, "ddc-i2c-bus", 0);
+		if (phandle) {
+			conn->bridge.ddc = of_get_i2c_adapter_by_node(phandle);
+			of_node_put(phandle);
+			if (!conn->bridge.ddc)
+				return -EPROBE_DEFER;
+		} else {
+			dev_dbg(&pdev->dev,
+				"No I2C bus specified, disabling EDID readout\n");
+		}
+	}
+
+	conn->bridge.funcs = &display_connector_bridge_funcs;
+	conn->bridge.of_node = pdev->dev.of_node;
+
+	if (conn->bridge.ddc)
+		conn->bridge.ops |= DRM_BRIDGE_OP_EDID
+				 |  DRM_BRIDGE_OP_DETECT;
+	if (conn->hpd_gpio)
+		conn->bridge.ops |= DRM_BRIDGE_OP_DETECT;
+	if (conn->hpd_irq >= 0)
+		conn->bridge.ops |= DRM_BRIDGE_OP_HPD;
+
+	dev_dbg(&pdev->dev,
+		"Found %s display connector '%s' %s DDC bus and %s HPD GPIO (ops 0x%x)\n",
+		drm_get_connector_type_name(conn->bridge.type),
+		label ? label : "<unlabelled>",
+		conn->bridge.ddc ? "with" : "without",
+		conn->hpd_gpio ? "with" : "without",
+		conn->bridge.ops);
+
+	drm_bridge_add(&conn->bridge);
+
+	return 0;
+}
+
+static int display_connector_remove(struct platform_device *pdev)
+{
+	struct display_connector *conn = platform_get_drvdata(pdev);
+
+	drm_bridge_remove(&conn->bridge);
+
+	if (!IS_ERR(conn->bridge.ddc))
+		i2c_put_adapter(conn->bridge.ddc);
+
+	return 0;
+}
+
+static const struct of_device_id display_connector_match[] = {
+	{
+		.compatible = "composite-video-connector",
+		.data = (void *)DRM_MODE_CONNECTOR_Composite,
+	}, {
+		.compatible = "dvi-connector",
+		.data = (void *)DRM_MODE_CONNECTOR_DVII,
+	}, {
+		.compatible = "hdmi-connector",
+		.data = (void *)DRM_MODE_CONNECTOR_HDMIA,
+	}, {
+		.compatible = "svideo-connector",
+		.data = (void *)DRM_MODE_CONNECTOR_SVIDEO,
+	}, {
+		.compatible = "vga-connector",
+		.data = (void *)DRM_MODE_CONNECTOR_VGA,
+	},
+	{},
+};
+MODULE_DEVICE_TABLE(of, display_connector_match);
+
+static struct platform_driver display_connector_driver = {
+	.probe	= display_connector_probe,
+	.remove	= display_connector_remove,
+	.driver		= {
+		.name		= "display-connector",
+		.of_match_table	= display_connector_match,
+	},
+};
+module_platform_driver(display_connector_driver);
+
+MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
+MODULE_DESCRIPTION("Display connector driver");
+MODULE_LICENSE("GPL");
-- 
Regards,

Laurent Pinchart

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

  parent reply	other threads:[~2020-01-24  3:55 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-24  3:53 [PATCH v5 00/52] drm/omap: Replace custom display drivers with drm_bridge and drm_panel Laurent Pinchart
2020-01-24  3:53 ` [PATCH v5 01/52] video: hdmi: Change return type of hdmi_avi_infoframe_init() to void Laurent Pinchart
2020-01-24  3:53 ` [PATCH v5 02/52] drm/connector: Add helper to get a connector type name Laurent Pinchart
2020-01-24  3:53 ` [PATCH v5 03/52] drm/edid: Add flag to drm_display_info to identify HDMI sinks Laurent Pinchart
2020-01-24  3:53 ` [PATCH v5 04/52] drm/bridge: Add connector-related bridge operations and data Laurent Pinchart
2020-01-24  3:53 ` [PATCH v5 05/52] drm/bridge: Add interlace_allowed flag to drm_bridge Laurent Pinchart
2020-01-24  3:53 ` [PATCH v5 06/52] drm/bridge: Extend bridge API to disable connector creation Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 07/52] drm/bridge: dumb-vga-dac: Rename internal symbols to simple-bridge Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 08/52] drm/bridge: dumb-vga-dac: Rename driver " Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 09/52] drm/bridge: simple-bridge: Add support for non-VGA bridges Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 10/52] drm/bridge: simple-bridge: Add support for enable GPIO Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 11/52] drm/bridge: simple-bridge: Add support for the TI OPA362 Laurent Pinchart
2020-01-24  3:54 ` Laurent Pinchart [this message]
2020-01-24  3:54 ` [PATCH v5 13/52] drm/bridge: Add driver for the TI TPD12S015 HDMI level shifter Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 14/52] drm/bridge: panel: Implement bridge connector operations Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 15/52] drm/bridge: tfp410: Replace manual connector handling with bridge Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 16/52] drm/bridge: tfp410: Allow operation without drm_connector Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 17/52] drm: Add helper to create a connector for a chain of bridges Laurent Pinchart
2020-01-28 11:19   ` Tomi Valkeinen
2020-02-16 15:39     ` Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 18/52] drm/omap: Fix possible object reference leak Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 19/52] drm/omap: dss: Cleanup DSS ports on initialisation failure Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 20/52] drm/omap: Simplify HDMI mode and infoframe configuration Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 21/52] drm/omap: Factor out display type to connector type conversion Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 22/52] drm/omap: Use the drm_panel_bridge API Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 23/52] drm/omap: dss: Fix output next device lookup in DT Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 24/52] drm/omap: Add infrastructure to support drm_bridge local to DSS outputs Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 25/52] drm/omap: dss: Make omap_dss_device_ops optional Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 26/52] drm/omap: hdmi: Allocate EDID in the .read_edid() operation Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 27/52] drm/omap: hdmi4: Rework EDID read to isolate data read Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 28/52] drm/omap: hdmi5: " Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 29/52] drm/omap: hdmi4: Register a drm_bridge for EDID read Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 30/52] drm/omap: hdmi5: " Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 31/52] drm/omap: hdmi4: Move mode set, enable and disable operations to bridge Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 32/52] drm/omap: hdmi5: " Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 33/52] drm/omap: hdmi4: Implement drm_bridge .hpd_notify() operation Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 34/52] drm/omap: dss: Remove .set_hdmi_mode() and .set_infoframe() operations Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 35/52] drm/omap: venc: Register a drm_bridge Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 36/52] drm/omap: Create connector for bridges Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 37/52] drm/omap: Switch the HDMI and VENC outputs to drm_bridge Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 38/52] drm/omap: Remove HPD, detect and EDID omapdss operations Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 39/52] drm/omap: hdmi: Remove omap_dss_device operations Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 40/52] drm/omap: venc: " Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 41/52] drm/omap: hdmi4: Simplify EDID read Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 42/52] drm/omap: hdmi5: " Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 43/52] drm/omap: dpi: Sort includes alphabetically Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 44/52] drm/omap: dpi: Reorder functions in sections Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 45/52] drm/omap: dpi: Simplify clock setting API Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 46/52] drm/omap: dpi: Register a drm_bridge Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 47/52] drm/omap: sdi: Sort includes alphabetically Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 48/52] drm/omap: sdi: Register a drm_bridge Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 49/52] drm/omap: Hardcode omap_connector type to DSI Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 50/52] drm/omap: dss: Remove unused omap_dss_device operations Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 51/52] drm/omap: dss: Inline the omapdss_display_get() function Laurent Pinchart
2020-01-24  3:54 ` [PATCH v5 52/52] drm/omap: dss: Remove unused omapdss_of_find_connected_device() function Laurent Pinchart

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200124035445.1830-13-laurent.pinchart@ideasonboard.com \
    --to=laurent.pinchart@ideasonboard.com \
    --cc=bbrezillon@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=sam@ravnborg.org \
    --cc=sebastian.reichel@collabora.com \
    --cc=tomi.valkeinen@ti.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.