linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Lu Baolu <baolu.lu@linux.intel.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	felipe.balbi@linux.intel.com,
	Mathias Nyman <mathias.nyman@intel.com>,
	Lee Jones <lee.jones@linaro.org>,
	Heikki Krogerus <heikki.krogerus@linux.intel.com>,
	Liam Girdwood <lgirdwood@gmail.com>,
	Mark Brown <broonie@kernel.org>
Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
	Lu Baolu <baolu.lu@linux.intel.com>,
	David Cohen <david.a.cohen@linux.intel.com>
Subject: [PATCH v9 3/7] usb: mux: add driver for Intel gpio controlled port mux
Date: Mon, 30 May 2016 13:51:40 +0800	[thread overview]
Message-ID: <1464587504-8139-4-git-send-email-baolu.lu@linux.intel.com> (raw)
In-Reply-To: <1464587504-8139-1-git-send-email-baolu.lu@linux.intel.com>

In some Intel platforms, a single usb port is shared between USB host
and device controller. The shared port is under control of GPIO pins.

This patch adds the support for USB GPIO controlled port mux.

[baolu: removed .owner per platform_no_drv_owner.cocci]
[baolu: extcon usage reviewed by Chanwoo Choi]
Signed-off-by: David Cohen <david.a.cohen@linux.intel.com>
Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Reviewed-by: Felipe Balbi <balbi@kernel.org>
Reviewed-by: Chanwoo Choi <cw00.choi@samsung.com>
---
 drivers/usb/mux/Kconfig              |  13 +++
 drivers/usb/mux/Makefile             |   1 +
 drivers/usb/mux/portmux-intel-gpio.c | 183 +++++++++++++++++++++++++++++++++++
 3 files changed, 197 insertions(+)
 create mode 100644 drivers/usb/mux/portmux-intel-gpio.c

diff --git a/drivers/usb/mux/Kconfig b/drivers/usb/mux/Kconfig
index ba778f2..41b0f72 100644
--- a/drivers/usb/mux/Kconfig
+++ b/drivers/usb/mux/Kconfig
@@ -6,3 +6,16 @@ menuconfig USB_PORTMUX
 	bool "USB dual role port MUX support"
 	help
 	  Generic USB dual role port mux support.
+
+if USB_PORTMUX
+
+config INTEL_MUX_GPIO
+tristate "Intel dual role port mux controlled by GPIOs"
+	depends on GPIOLIB
+	depends on REGULATOR
+	depends on X86 && ACPI
+	help
+	  Say Y here to enable support for Intel dual role port mux
+	  controlled by GPIOs.
+
+endif
diff --git a/drivers/usb/mux/Makefile b/drivers/usb/mux/Makefile
index f85df92..4eb5582 100644
--- a/drivers/usb/mux/Makefile
+++ b/drivers/usb/mux/Makefile
@@ -2,3 +2,4 @@
 # Makefile for USB port mux drivers
 #
 obj-$(CONFIG_USB_PORTMUX)		+= portmux-core.o
+obj-$(CONFIG_INTEL_MUX_GPIO)		+= portmux-intel-gpio.o
diff --git a/drivers/usb/mux/portmux-intel-gpio.c b/drivers/usb/mux/portmux-intel-gpio.c
new file mode 100644
index 0000000..cada490
--- /dev/null
+++ b/drivers/usb/mux/portmux-intel-gpio.c
@@ -0,0 +1,183 @@
+/*
+ * USB Dual Role Port Mux driver controlled by gpios
+ *
+ * Copyright (c) 2016, Intel Corporation.
+ * Author: David Cohen <david.a.cohen@linux.intel.com>
+ * Author: Lu Baolu <baolu.lu@linux.intel.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/acpi.h>
+#include <linux/module.h>
+#include <linux/extcon.h>
+#include <linux/gpio/consumer.h>
+#include <linux/platform_device.h>
+#include <linux/usb/portmux.h>
+#include <linux/regulator/consumer.h>
+
+struct vuport {
+	struct extcon_dev *edev;
+	struct notifier_block nb;
+	struct portmux_desc desc;
+	struct portmux_dev *pdev;
+	struct regulator *regulator;
+	struct gpio_desc *gpio_usb_mux;
+};
+
+/*
+ * id == 0, HOST connected, USB port should be set to peripheral
+ * id == 1, HOST disconnected, USB port should be set to host
+ *
+ * Peripheral: set USB mux to peripheral and disable VBUS
+ * Host: set USB mux to host and enable VBUS
+ */
+static inline int vuport_set_port(struct device *dev, int id)
+{
+	struct vuport *vup;
+
+	dev_dbg(dev, "USB PORT ID: %s\n", id ? "HOST" : "PERIPHERAL");
+
+	vup = dev_get_drvdata(dev);
+
+	gpiod_set_value_cansleep(vup->gpio_usb_mux, !id);
+
+	if (!id ^ regulator_is_enabled(vup->regulator))
+		return id ? regulator_disable(vup->regulator) :
+				regulator_enable(vup->regulator);
+
+	return 0;
+}
+
+static int vuport_cable_set(struct device *dev)
+{
+	return vuport_set_port(dev, 1);
+}
+
+static int vuport_cable_unset(struct device *dev)
+{
+	return vuport_set_port(dev, 0);
+}
+
+static const struct portmux_ops vuport_ops = {
+	.set_host_cb = vuport_cable_set,
+	.set_device_cb = vuport_cable_unset,
+};
+
+static int vuport_notifier(struct notifier_block *nb,
+			   unsigned long event, void *ptr)
+{
+	struct vuport *vup;
+	int state;
+
+	vup = container_of(nb, struct vuport, nb);
+	state = extcon_get_cable_state_(vup->edev, EXTCON_USB_HOST);
+	if (state < 0)
+		return state;
+
+	return portmux_switch(vup->pdev, state ? PORTMUX_HOST : PORTMUX_DEVICE);
+}
+
+static int vuport_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct vuport *vup;
+	int ret;
+
+	vup = devm_kzalloc(dev, sizeof(*vup), GFP_KERNEL);
+	if (!vup)
+		return -ENOMEM;
+
+	vup->regulator = devm_regulator_get_exclusive(dev,
+						      "regulator-usb-gpio");
+	if (IS_ERR_OR_NULL(vup->regulator))
+		return -EPROBE_DEFER;
+
+	vup->edev = extcon_get_extcon_dev("extcon-usb-gpio");
+	if (IS_ERR_OR_NULL(vup->edev))
+		return -EPROBE_DEFER;
+
+	vup->gpio_usb_mux = devm_gpiod_get_optional(dev,
+						    "usb_mux", GPIOD_ASIS);
+	if (IS_ERR(vup->gpio_usb_mux))
+		return PTR_ERR(vup->gpio_usb_mux);
+
+	vup->desc.dev = dev;
+	vup->desc.name = "intel-mux-gpio";
+	vup->desc.ops = &vuport_ops;
+	dev_set_drvdata(dev, vup);
+	vup->pdev = portmux_register(&vup->desc);
+	if (IS_ERR(vup->pdev))
+		return PTR_ERR(vup->pdev);
+
+	vup->nb.notifier_call = vuport_notifier;
+	ret = extcon_register_notifier(vup->edev, EXTCON_USB_HOST,
+				       &vup->nb);
+	if (ret < 0) {
+		portmux_unregister(vup->pdev);
+		return ret;
+	}
+
+	vuport_notifier(&vup->nb, 0, NULL);
+
+	return 0;
+}
+
+static int vuport_remove(struct platform_device *pdev)
+{
+	struct vuport *vup;
+
+	vup = platform_get_drvdata(pdev);
+	extcon_unregister_notifier(vup->edev, EXTCON_USB_HOST, &vup->nb);
+	portmux_unregister(vup->pdev);
+
+	return 0;
+}
+
+#ifdef CONFIG_PM_SLEEP
+/*
+ * In case a micro A cable was plugged in while device was sleeping,
+ * we missed the interrupt. We need to poll usb id gpio when waking the
+ * driver to detect the missed event.
+ * We use 'complete' callback to give time to all extcon listeners to
+ * resume before we send new events.
+ */
+static void vuport_complete(struct device *dev)
+{
+	struct vuport *vup;
+
+	vup = dev_get_drvdata(dev);
+	vuport_notifier(&vup->nb, 0, NULL);
+}
+
+static const struct dev_pm_ops vuport_pm_ops = {
+	.complete = vuport_complete,
+};
+#endif
+
+static const struct platform_device_id vuport_platform_ids[] = {
+	{ .name = "intel-mux-gpio", },
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(platform, vuport_platform_ids);
+
+static struct platform_driver vuport_driver = {
+	.driver = {
+		.name = "intel-mux-gpio",
+#ifdef CONFIG_PM_SLEEP
+		.pm = &vuport_pm_ops,
+#endif
+	},
+	.probe = vuport_probe,
+	.remove = vuport_remove,
+	.id_table = vuport_platform_ids,
+};
+
+module_platform_driver(vuport_driver);
+
+MODULE_AUTHOR("David Cohen <david.a.cohen@linux.intel.com>");
+MODULE_AUTHOR("Lu Baolu <baolu.lu@linux.intel.com>");
+MODULE_DESCRIPTION("Intel USB gpio mux driver");
+MODULE_LICENSE("GPL v2");
-- 
2.1.4

  parent reply	other threads:[~2016-05-30  5:52 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-30  5:51 [PATCH v9 0/7] usb: add support for Intel dual role port mux Lu Baolu
2016-05-30  5:51 ` [PATCH v9 1/7] regulator: fixed: add support for ACPI interface Lu Baolu
2016-05-30  5:51 ` [PATCH v9 2/7] usb: mux: add generic code for dual role port mux Lu Baolu
2016-05-30  5:51 ` Lu Baolu [this message]
2016-05-30  5:51 ` [PATCH v9 4/7] usb: mux: add driver for Intel drcfg controlled " Lu Baolu
2016-05-30  5:51 ` [PATCH v9 5/7] mfd: intel_vuport: Add Intel virtual USB port MFD Driver Lu Baolu
2016-05-30  5:51 ` [PATCH v9 6/7] usb: pci-quirks: add Intel USB drcfg mux device Lu Baolu
2016-05-30  5:51 ` [PATCH v9 7/7] MAINTAINERS: add maintainer entry for Intel USB dual role mux drivers Lu Baolu
2016-05-30  8:16   ` Heikki Krogerus
2016-05-30 12:53     ` Lu Baolu
2016-06-01  2:44       ` Lu Baolu

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=1464587504-8139-4-git-send-email-baolu.lu@linux.intel.com \
    --to=baolu.lu@linux.intel.com \
    --cc=broonie@kernel.org \
    --cc=david.a.cohen@linux.intel.com \
    --cc=felipe.balbi@linux.intel.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=heikki.krogerus@linux.intel.com \
    --cc=lee.jones@linaro.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=mathias.nyman@intel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).