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>, Wu Hao <hao.wu@intel.com>
Subject: [PATCH v9 4/7] usb: mux: add driver for Intel drcfg controlled port mux
Date: Mon, 30 May 2016 13:51:41 +0800	[thread overview]
Message-ID: <1464587504-8139-5-git-send-email-baolu.lu@linux.intel.com> (raw)
In-Reply-To: <1464587504-8139-1-git-send-email-baolu.lu@linux.intel.com>

Several Intel PCHs and SOCs have an internal mux that is used to
share one USB port between device controller and host controller.
The mux is handled through the Dual Role Configuration Register.

Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
Signed-off-by: Wu Hao <hao.wu@intel.com>
Reviewed-by: Felipe Balbi <balbi@kernel.org>
---
 drivers/usb/mux/Kconfig               |   7 ++
 drivers/usb/mux/Makefile              |   1 +
 drivers/usb/mux/portmux-intel-drcfg.c | 162 ++++++++++++++++++++++++++++++++++
 3 files changed, 170 insertions(+)
 create mode 100644 drivers/usb/mux/portmux-intel-drcfg.c

diff --git a/drivers/usb/mux/Kconfig b/drivers/usb/mux/Kconfig
index 41b0f72..98b54d2 100644
--- a/drivers/usb/mux/Kconfig
+++ b/drivers/usb/mux/Kconfig
@@ -18,4 +18,11 @@ tristate "Intel dual role port mux controlled by GPIOs"
 	  Say Y here to enable support for Intel dual role port mux
 	  controlled by GPIOs.
 
+config INTEL_MUX_DRCFG
+	tristate "Intel dual role port mux controlled by register"
+	depends on X86
+	help
+	  Say Y here to enable support for Intel dual role port mux
+	  controlled by the Dual Role Configuration Register.
+
 endif
diff --git a/drivers/usb/mux/Makefile b/drivers/usb/mux/Makefile
index 4eb5582..0f102b5 100644
--- a/drivers/usb/mux/Makefile
+++ b/drivers/usb/mux/Makefile
@@ -3,3 +3,4 @@
 #
 obj-$(CONFIG_USB_PORTMUX)		+= portmux-core.o
 obj-$(CONFIG_INTEL_MUX_GPIO)		+= portmux-intel-gpio.o
+obj-$(CONFIG_INTEL_MUX_DRCFG)		+= portmux-intel-drcfg.o
diff --git a/drivers/usb/mux/portmux-intel-drcfg.c b/drivers/usb/mux/portmux-intel-drcfg.c
new file mode 100644
index 0000000..67b11d2
--- /dev/null
+++ b/drivers/usb/mux/portmux-intel-drcfg.c
@@ -0,0 +1,162 @@
+/**
+ * intel-mux-drcfg.c - Driver for Intel USB mux via register
+ *
+ * Copyright (C) 2016 Intel Corporation
+ * Author: Heikki Krogerus <heikki.krogerus@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/slab.h>
+#include <linux/module.h>
+#include <linux/property.h>
+#include <linux/platform_device.h>
+#include <linux/usb.h>
+#include <linux/usb/portmux.h>
+
+#define INTEL_MUX_CFG0		0x00
+#define CFG0_SW_IDPIN		BIT(20)
+#define CFG0_SW_IDPIN_EN	BIT(21)
+#define CFG0_SW_VBUS_VALID	BIT(24)
+
+struct intel_mux_drcfg {
+	struct portmux_desc desc;
+	struct device *dev;
+	void __iomem *regs;
+	struct portmux_dev *pdev;
+};
+
+static inline int intel_mux_drcfg_switch(struct device *dev, bool host)
+{
+	u32 data;
+	struct intel_mux_drcfg *mux;
+
+	mux = dev_get_drvdata(dev);
+
+	/* Check and set mux to SW controlled mode */
+	data = readl(mux->regs + INTEL_MUX_CFG0);
+	if (!(data & CFG0_SW_IDPIN_EN)) {
+		data |= CFG0_SW_IDPIN_EN;
+		writel(data, mux->regs + INTEL_MUX_CFG0);
+	}
+
+	/*
+	 * Configure CFG0 to switch the mux and VBUS_VALID bit is
+	 * required for device mode.
+	 */
+	data = readl(mux->regs + INTEL_MUX_CFG0);
+	if (host)
+		data &= ~(CFG0_SW_IDPIN | CFG0_SW_VBUS_VALID);
+	else
+		data |= (CFG0_SW_IDPIN | CFG0_SW_VBUS_VALID);
+	writel(data, mux->regs + INTEL_MUX_CFG0);
+
+	return 0;
+}
+
+static int intel_mux_drcfg_set_host(struct device *dev)
+{
+	dev_dbg(dev, "drcfg mux switch to HOST\n");
+
+	return intel_mux_drcfg_switch(dev, true);
+}
+
+static int intel_mux_drcfg_set_device(struct device *dev)
+{
+	dev_dbg(dev, "drcfg mux switch to DEVICE\n");
+
+	return intel_mux_drcfg_switch(dev, false);
+}
+
+static const struct portmux_ops drcfg_ops = {
+	.set_host_cb = intel_mux_drcfg_set_host,
+	.set_device_cb = intel_mux_drcfg_set_device,
+};
+
+static int intel_mux_drcfg_probe(struct platform_device *pdev)
+{
+	struct intel_mux_drcfg *mux;
+	struct device *dev = &pdev->dev;
+	u64 start, size;
+	int ret;
+
+	mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL);
+	if (!mux)
+		return -ENOMEM;
+
+	ret = device_property_read_u64(dev, "reg-start", &start);
+	ret |= device_property_read_u64(dev, "reg-size", &size);
+	if (ret)
+		return -ENODEV;
+
+	mux->regs = devm_ioremap_nocache(dev, start, size);
+	if (!mux->regs)
+		return -ENOMEM;
+
+	mux->desc.dev = dev;
+	mux->desc.name = "intel-mux-drcfg";
+	mux->desc.ops = &drcfg_ops;
+	dev_set_drvdata(dev, mux);
+	mux->pdev = portmux_register(&mux->desc);
+
+	return PTR_ERR_OR_ZERO(mux->pdev);
+}
+
+static int intel_mux_drcfg_remove(struct platform_device *pdev)
+{
+	struct intel_mux_drcfg *mux;
+
+	mux = platform_get_drvdata(pdev);
+	portmux_unregister(mux->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 state 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 intel_mux_drcfg_complete(struct device *dev)
+{
+	struct intel_mux_drcfg *mux;
+
+	mux = dev_get_drvdata(dev);
+	portmux_complete(mux->pdev);
+}
+
+static const struct dev_pm_ops intel_mux_drcfg_pm_ops = {
+	.complete = intel_mux_drcfg_complete,
+};
+#endif
+
+static const struct platform_device_id intel_mux_drcfg_platform_ids[] = {
+	{ .name = "intel-mux-drcfg", },
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(platform, intel_mux_drcfg_platform_ids);
+
+static struct platform_driver intel_mux_drcfg_driver = {
+	.probe		= intel_mux_drcfg_probe,
+	.remove		= intel_mux_drcfg_remove,
+	.driver		= {
+		.name	= "intel-mux-drcfg",
+#ifdef CONFIG_PM_SLEEP
+		.pm	= &intel_mux_drcfg_pm_ops,
+#endif
+	},
+	.id_table = intel_mux_drcfg_platform_ids,
+};
+
+module_platform_driver(intel_mux_drcfg_driver);
+
+MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
+MODULE_AUTHOR("Lu Baolu <baolu.lu@linux.intel.com>");
+MODULE_DESCRIPTION("Intel USB drcfg mux driver");
+MODULE_LICENSE("GPL v2");
-- 
2.1.4

  parent reply	other threads:[~2016-05-30  5:53 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 ` [PATCH v9 3/7] usb: mux: add driver for Intel gpio controlled " Lu Baolu
2016-05-30  5:51 ` Lu Baolu [this message]
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-5-git-send-email-baolu.lu@linux.intel.com \
    --to=baolu.lu@linux.intel.com \
    --cc=broonie@kernel.org \
    --cc=felipe.balbi@linux.intel.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=hao.wu@intel.com \
    --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).