Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp256567ybl; Tue, 10 Dec 2019 22:21:51 -0800 (PST) X-Google-Smtp-Source: APXvYqw/DvUiQPTG63+QO+tYIPEGGgan5l05lfFFTkaPFCDYg+Lp08PS7/wqh05gxoBKq+UlNuAj X-Received: by 2002:aca:5c03:: with SMTP id q3mr1415318oib.173.1576045311620; Tue, 10 Dec 2019 22:21:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576045311; cv=none; d=google.com; s=arc-20160816; b=GvMn+5oFw8XOex9Oyl+A3HJ+UxUyJTU3E31AcvdE2aXTSgJhJuMi+MfC2qDaK8oEu7 kL/3jjnsr5ylF33LPfUK8IklhmegBWbDuMZBjK6UDGkokwjd83dhBkY/XNu0iP42L1p4 qzfDhMU5u3mu78+I9NHYZaP9dbfcOFoUzqlFuD37KCk66mP7bPNjCHblrUm3217P/q7w YfBtnhCMqtgUS5sjuTrxC34CpwH6Q6QUEDpHzKdzuIaSroJIkx5AQ+8u3BRVsmiky6Vj Az30oox8SKRPAC98/503PeTJrQYLaX4QEyNejM36rPuC+8m0iFgrm/vBvi2AuhasXzPa jrpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=X6L+fh7OnQT1zXjxbAvo/8bLa08LP3Xh+NSarGpgTMs=; b=nuSQNIF4o6O5TEEaCIuHWwqxlf0xpMchEa4NPcg9fRZU1vdQlohirfsQYan0ht91jY T9iiROIAwEkPj53TIuh9hn/umhxnGpmthZvxKcU/m0UNJHxEU6SeS8RjMMbAcxhNBqmt Q6Yvr5vQ/JJYszxYm2YvaQrZN2/8mmR9rsxiNHVOka00ug6cGXuRzvEKqGiLUaiy7qF4 9PEw2oBK4sdbdwB7UZjKvWlvSKBMga4eH/ble3fgPm0HRbuxlPS96eFE118D2VRFeIZi GpEMGbnhalj8f5WQe917QZfgP8VTK6FpgBIiMzMOlLIA72K6rhKfkK1ZD93uEY0NLcLo jRiQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Z5oQQgTX; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l9si441170otn.301.2019.12.10.22.21.39; Tue, 10 Dec 2019 22:21:51 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Z5oQQgTX; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727894AbfLKGUB (ORCPT + 99 others); Wed, 11 Dec 2019 01:20:01 -0500 Received: from mail-pg1-f193.google.com ([209.85.215.193]:34854 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726357AbfLKGUB (ORCPT ); Wed, 11 Dec 2019 01:20:01 -0500 Received: by mail-pg1-f193.google.com with SMTP id l24so10241170pgk.2 for ; Tue, 10 Dec 2019 22:20:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=X6L+fh7OnQT1zXjxbAvo/8bLa08LP3Xh+NSarGpgTMs=; b=Z5oQQgTXY4M6w80jvS8rMzJy3iV0k0Ioluhy9WOof/EljqU5L25IBI5TrJiv2MPv2/ ZAdWLRg5wfZCQvim6rfZTBj9z25P26KtH1s1AzSnzzPlTT6u+aYp+TS6Y+1Y5TFWyLde IhqEz3MP4XMcs0mI2gspvBvXTVdwI7XXk53Us= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=X6L+fh7OnQT1zXjxbAvo/8bLa08LP3Xh+NSarGpgTMs=; b=PXF9BeFDRx5Mj1BSLwpKsrF6Cx0fOy1XWWy1AFgGVQl0ZYMA2O/oE4GellVAAqjanZ DWumnEQy96c4K8aC58uRFp9/2Zol9jV3ySaCRU9blV8DWX56kMPwhdA1FgJSLdOaa4dI IMVJJzch2lvaAnUDijSIoKc+3p1j8elJJTVpAWTDbVpas9fQD82jfqp9PDV2eib4ZHov WWj+MWIQpgW7W7eNHtPqCu6jxPvQMvzC5JPieK7ZHOaAJxXKWAGu297NSvRh5M12Ux7x 7sV+fWJ2OGOjAcckracgiGKQoi5oseVAxop6IY9qnJEp+V3tXyp9TU47JV4G111Cwyiy Ij1g== X-Gm-Message-State: APjAAAUPFbnSfoyIVhvzpllxGFum93Y7JzKsoNBA3c/BTSbXmBkW5oGW ZNDMrpQ2XGmgiZXSUAxpp4L2lw== X-Received: by 2002:a63:e0f:: with SMTP id d15mr2264222pgl.255.1576045200089; Tue, 10 Dec 2019 22:20:00 -0800 (PST) Received: from hsinyi-z840.tpe.corp.google.com ([2401:fa00:1:10:b852:bd51:9305:4261]) by smtp.gmail.com with ESMTPSA id h5sm1225579pfk.30.2019.12.10.22.19.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Dec 2019 22:19:59 -0800 (PST) From: Hsin-Yi Wang To: dri-devel@lists.freedesktop.org Cc: David Airlie , Daniel Vetter , Rob Herring , Mark Rutland , Nicolas Boichat , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Andrzej Hajda , Neil Armstrong , Laurent Pinchart , Jonas Karlman , Jernej Skrabec , p.zabel@pengutronix.de, Enric Balletbo i Serra , Matthias Brugger , Russell King Subject: [PATCH RESEND 4/4] drm: bridge: Generic GPIO mux driver Date: Wed, 11 Dec 2019 14:19:11 +0800 Message-Id: <20191211061911.238393-5-hsinyi@chromium.org> X-Mailer: git-send-email 2.24.0.525.g8f36a354ae-goog In-Reply-To: <20191211061911.238393-1-hsinyi@chromium.org> References: <20191211061911.238393-1-hsinyi@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Nicolas Boichat This driver supports single input, 2 output display mux (e.g. HDMI mux), that provide its status via a GPIO. Signed-off-by: Nicolas Boichat Signed-off-by: Hsin-Yi Wang --- drivers/gpu/drm/bridge/Kconfig | 10 + drivers/gpu/drm/bridge/Makefile | 1 + drivers/gpu/drm/bridge/generic-gpio-mux.c | 306 ++++++++++++++++++++++ 3 files changed, 317 insertions(+) create mode 100644 drivers/gpu/drm/bridge/generic-gpio-mux.c diff --git a/drivers/gpu/drm/bridge/Kconfig b/drivers/gpu/drm/bridge/Kconfig index 1f3fc6bec842..4734f6993858 100644 --- a/drivers/gpu/drm/bridge/Kconfig +++ b/drivers/gpu/drm/bridge/Kconfig @@ -54,6 +54,16 @@ config DRM_DUMB_VGA_DAC Support for non-programmable RGB to VGA DAC bridges, such as ADI ADV7123, TI THS8134 and THS8135 or passive resistor ladder DACs. +config DRM_GENERIC_GPIO_MUX + tristate "Generic GPIO-controlled mux" + depends on OF + select DRM_KMS_HELPER + ---help--- + This bridge driver models a GPIO-controlled display mux with one + input, 2 outputs (e.g. an HDMI mux). The hardware decides which output + is active, reports it as a GPIO, and the driver redirects calls to the + appropriate downstream bridge (if any). + config DRM_LVDS_ENCODER tristate "Transparent parallel to LVDS encoder support" depends on OF diff --git a/drivers/gpu/drm/bridge/Makefile b/drivers/gpu/drm/bridge/Makefile index 7a1e0ec032e6..1c0c92667ac4 100644 --- a/drivers/gpu/drm/bridge/Makefile +++ b/drivers/gpu/drm/bridge/Makefile @@ -3,6 +3,7 @@ obj-$(CONFIG_DRM_ANALOGIX_ANX7688) += analogix-anx7688.o obj-$(CONFIG_DRM_ANALOGIX_ANX78XX) += analogix-anx78xx.o obj-$(CONFIG_DRM_CDNS_DSI) += cdns-dsi.o obj-$(CONFIG_DRM_DUMB_VGA_DAC) += dumb-vga-dac.o +obj-$(CONFIG_DRM_GENERIC_GPIO_MUX) += generic-gpio-mux.o obj-$(CONFIG_DRM_LVDS_ENCODER) += lvds-encoder.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/generic-gpio-mux.c b/drivers/gpu/drm/bridge/generic-gpio-mux.c new file mode 100644 index 000000000000..ba08321dcc17 --- /dev/null +++ b/drivers/gpu/drm/bridge/generic-gpio-mux.c @@ -0,0 +1,306 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Generic gpio mux bridge driver + * + * Copyright 2016 Google LLC + */ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct gpio_display_mux { + struct device *dev; + + struct gpio_desc *gpiod_detect; + int detect_irq; + + struct drm_bridge bridge; + + struct drm_bridge *next[2]; +}; + +static inline struct gpio_display_mux *bridge_to_gpio_display_mux( + struct drm_bridge *bridge) +{ + return container_of(bridge, struct gpio_display_mux, bridge); +} + +static irqreturn_t gpio_display_mux_det_threaded_handler(int unused, void *data) +{ + struct gpio_display_mux *gpio_display_mux = data; + int active = gpiod_get_value(gpio_display_mux->gpiod_detect); + + dev_dbg(gpio_display_mux->dev, "Interrupt %d!\n", active); + + if (gpio_display_mux->bridge.dev) + drm_kms_helper_hotplug_event(gpio_display_mux->bridge.dev); + + return IRQ_HANDLED; +} + +static int gpio_display_mux_attach(struct drm_bridge *bridge) +{ + struct gpio_display_mux *gpio_display_mux = + bridge_to_gpio_display_mux(bridge); + struct drm_bridge *next; + int i; + + for (i = 0; i < ARRAY_SIZE(gpio_display_mux->next); i++) { + next = gpio_display_mux->next[i]; + if (next) + next->encoder = bridge->encoder; + } + + return 0; +} + +static bool gpio_display_mux_mode_fixup(struct drm_bridge *bridge, + const struct drm_display_mode *mode, + struct drm_display_mode *adjusted_mode) +{ + struct gpio_display_mux *gpio_display_mux = + bridge_to_gpio_display_mux(bridge); + int active; + struct drm_bridge *next; + + active = gpiod_get_value(gpio_display_mux->gpiod_detect); + next = gpio_display_mux->next[active]; + + if (next && next->funcs->mode_fixup) + return next->funcs->mode_fixup(next, mode, adjusted_mode); + else + return true; +} + +static void gpio_display_mux_mode_set(struct drm_bridge *bridge, + struct drm_display_mode *mode, + struct drm_display_mode *adjusted_mode) +{ + struct gpio_display_mux *gpio_display_mux = + bridge_to_gpio_display_mux(bridge); + int active; + struct drm_bridge *next; + + active = gpiod_get_value(gpio_display_mux->gpiod_detect); + next = gpio_display_mux->next[active]; + + if (next && next->funcs->mode_set) + next->funcs->mode_set(next, mode, adjusted_mode); +} + +/** + * Since this driver _reacts_ to mux changes, we need to make sure all + * downstream bridges are pre-enabled. + */ +static void gpio_display_mux_pre_enable(struct drm_bridge *bridge) +{ + struct gpio_display_mux *gpio_display_mux = + bridge_to_gpio_display_mux(bridge); + struct drm_bridge *next; + int i; + + for (i = 0; i < ARRAY_SIZE(gpio_display_mux->next); i++) { + next = gpio_display_mux->next[i]; + if (next && next->funcs->pre_enable) + next->funcs->pre_enable(next); + } +} + +static void gpio_display_mux_post_disable(struct drm_bridge *bridge) +{ + struct gpio_display_mux *gpio_display_mux = + bridge_to_gpio_display_mux(bridge); + struct drm_bridge *next; + int i; + + for (i = 0; i < ARRAY_SIZE(gpio_display_mux->next); i++) { + next = gpio_display_mux->next[i]; + if (next && next->funcs->post_disable) + next->funcs->post_disable(next); + } +} + +/** + * In an ideal mux driver, only the currently selected bridge should be enabled. + * For the sake of simplicity, we just just enable/disable all downstream + * bridges at the same time. + */ +static void gpio_display_mux_enable(struct drm_bridge *bridge) +{ + struct gpio_display_mux *gpio_display_mux = + bridge_to_gpio_display_mux(bridge); + struct drm_bridge *next; + int i; + + for (i = 0; i < ARRAY_SIZE(gpio_display_mux->next); i++) { + next = gpio_display_mux->next[i]; + if (next && next->funcs->enable) + next->funcs->enable(next); + } +} + +static void gpio_display_mux_disable(struct drm_bridge *bridge) +{ + struct gpio_display_mux *gpio_display_mux = + bridge_to_gpio_display_mux(bridge); + struct drm_bridge *next; + int i; + + for (i = 0; i < ARRAY_SIZE(gpio_display_mux->next); i++) { + next = gpio_display_mux->next[i]; + if (next && next->funcs->disable) + next->funcs->disable(next); + } +} + +static const struct drm_bridge_funcs gpio_display_mux_bridge_funcs = { + .attach = gpio_display_mux_attach, + .mode_fixup = gpio_display_mux_mode_fixup, + .disable = gpio_display_mux_disable, + .post_disable = gpio_display_mux_post_disable, + .mode_set = gpio_display_mux_mode_set, + .pre_enable = gpio_display_mux_pre_enable, + .enable = gpio_display_mux_enable, +}; + +static int gpio_display_mux_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct gpio_display_mux *gpio_display_mux; + struct device_node *port, *ep, *remote; + int ret; + u32 reg; + + gpio_display_mux = devm_kzalloc(dev, sizeof(*gpio_display_mux), + GFP_KERNEL); + if (!gpio_display_mux) + return -ENOMEM; + + platform_set_drvdata(pdev, gpio_display_mux); + gpio_display_mux->dev = &pdev->dev; + + gpio_display_mux->bridge.of_node = dev->of_node; + + gpio_display_mux->gpiod_detect = + devm_gpiod_get(dev, "detect", GPIOD_IN); + if (IS_ERR(gpio_display_mux->gpiod_detect)) + return PTR_ERR(gpio_display_mux->gpiod_detect); + + gpio_display_mux->detect_irq = + gpiod_to_irq(gpio_display_mux->gpiod_detect); + if (gpio_display_mux->detect_irq < 0) { + dev_err(dev, "Failed to get output irq %d\n", + gpio_display_mux->detect_irq); + return -ENODEV; + } + + port = of_graph_get_port_by_id(dev->of_node, 1); + if (!port) { + dev_err(dev, "Missing output port node\n"); + return -EINVAL; + } + + for_each_child_of_node(port, ep) { + if (!ep->name || (of_node_cmp(ep->name, "endpoint") != 0)) { + of_node_put(ep); + continue; + } + + if (of_property_read_u32(ep, "reg", ®) < 0 || + reg >= ARRAY_SIZE(gpio_display_mux->next)) { + dev_err(dev, + "Missing/invalid reg property for endpoint %s\n", + ep->full_name); + of_node_put(ep); + of_node_put(port); + return -EINVAL; + } + + remote = of_graph_get_remote_port_parent(ep); + if (!remote) { + dev_err(dev, + "Missing connector/bridge node for endpoint %s\n", + ep->full_name); + of_node_put(ep); + of_node_put(port); + return -EINVAL; + } + of_node_put(ep); + + if (of_device_is_compatible(remote, "hdmi-connector")) { + of_node_put(remote); + continue; + } + + gpio_display_mux->next[reg] = of_drm_find_bridge(remote); + if (!gpio_display_mux->next[reg]) { + dev_err(dev, "Waiting for external bridge %s\n", + remote->name); + of_node_put(remote); + of_node_put(port); + return -EPROBE_DEFER; + } + + of_node_put(remote); + } + of_node_put(port); + + gpio_display_mux->bridge.funcs = &gpio_display_mux_bridge_funcs; + drm_bridge_add(&gpio_display_mux->bridge); + + ret = devm_request_threaded_irq(dev, gpio_display_mux->detect_irq, + NULL, + gpio_display_mux_det_threaded_handler, + IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | + IRQF_ONESHOT, + "gpio-display-mux-det", gpio_display_mux); + if (ret) { + dev_err(dev, "Failed to request MUX_DET threaded irq\n"); + goto err_bridge_remove; + } + + return 0; + +err_bridge_remove: + drm_bridge_remove(&gpio_display_mux->bridge); + + return ret; +} + +static int gpio_display_mux_remove(struct platform_device *pdev) +{ + struct gpio_display_mux *gpio_display_mux = platform_get_drvdata(pdev); + + drm_bridge_remove(&gpio_display_mux->bridge); + + return 0; +} + +static const struct of_device_id gpio_display_mux_match[] = { + { .compatible = "gpio-display-mux", }, + {}, +}; + +struct platform_driver gpio_display_mux_driver = { + .probe = gpio_display_mux_probe, + .remove = gpio_display_mux_remove, + .driver = { + .name = "gpio-display-mux", + .of_match_table = gpio_display_mux_match, + }, +}; + +module_platform_driver(gpio_display_mux_driver); + +MODULE_DESCRIPTION("GPIO-controlled display mux"); +MODULE_AUTHOR("Nicolas Boichat "); +MODULE_LICENSE("GPL v2"); -- 2.24.0.525.g8f36a354ae-goog