Skip to contentSkip to conversation

usb: add support for Intel dual role port mux

PATCH Superseded Lu Baolu 2 participants 12 messages Mar 17, 2016

Subsystems (4)

v4 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Lu Baolu

author
#0 lore raw

v4 cover usb: add support for Intel dual role port mux

Intel SOC chips are featured with USB dual role. The host role is provided by Intel xHCI IP, and the gadget role is provided by IP from designware. Tablet platform designs always share a single port for both host and gadget controllers. There is a mux to switch the port to the right controller according to the cable type. OS needs to provide the callback to control the mux when a plug-in event raises. The method to control the mux is platform dependent. At least three types of implementation can be found across current devices. 1) GPIO pins; 2) a unit which can be controlled by memory mapped registers; 3) ACPI ASL code.

This patch series adds supports for Intel dual role port mux. It includes:
(1) A helper layer on top of extcon for individual mux driver. It listens to the USB-HOST extcon cable and call the switch call-back when the cable state changes.
(2) Drivers for GPIO controlled port mux which could be found on Baytrail devices. A mfd driver is used to split the GPIOs into USB gpio extcon device and a USB mux device. Driver for USB gpio extcon device is already in upstream Linux. This patch series includes a driver for GPIO USB mux.
(3) Drivers for USB port mux controlled through memory mapped registers and the logic to create the mux device. This type of dual role port mux could be found in Cherry Trail and Broxton devices.

Lu Baolu (7):
extcon: usb-gpio: add device binding for platform device
extcon: usb-gpio: add support for ACPI gpio interface
usb: mux: add common code for Intel dual role port mux
usb: mux: add driver for Intel gpio controlled port mux
usb: mux: add driver for Intel drcfg controlled port mux
usb: pci-quirks: add Intel USB drcfg mux device
mfd: intel_vuport: Add Intel virtual USB port MFD Driver

Change log:
v3->v4:
- Check all patches with "checkpatch.pl --strict", and fix all CHECKs;
- Change sysfs node from "intel_mux" to "port_mux";
- Refines below confusing functions:
intel_usb_mux_register() -> intel_usb_mux_bind_cable() intel_usb_mux_unregister() -> intel_usb_mux_unbind_cable();
- Remove unnecessary struct intel_mux_dev.

v2->v3:
- uvport mfd driver got reviewed by Lee Jones, the following changes were made accordingly.
- seperate uvport driver from the mux drivers in MAINTAINERS file
- refine the description in Kconfig
- refine the mfd_cell structure data

v1->v2:
- move mux driver from drivers/usb/misc to drivers/usb/mux;
- replace debugfs with sysfs for user level mux control;
- remove unnecessary register restore if mux registeration failed;
- Add "Acked-by: Chanwoo Choi <[email protected]>" to extcon changes;
- Make the file names and exported function names more specific;
- Remove the usb_mux_get_dev() interface;
- Move "struct intel_usb_mux" from .h to .c file;
- Fix various kbuild robot warnings.

 Documentation/ABI/testing/sysfs-bus-platform |  15 +++
 MAINTAINERS                                  |  15 +++
 drivers/extcon/extcon-usb-gpio.c             |  10 +-
 drivers/mfd/Kconfig                          |   8 ++
 drivers/mfd/Makefile                         |   1 +
 drivers/mfd/intel-vuport.c                   |  74 +++++++++++
 drivers/usb/Kconfig                          |   2 +
 drivers/usb/Makefile                         |   1 +
 drivers/usb/host/pci-quirks.c                |  47 ++++++-
 drivers/usb/host/xhci-ext-caps.h             |   2 +
 drivers/usb/mux/Kconfig                      |  28 +++++
 drivers/usb/mux/Makefile                     |   6 +
 drivers/usb/mux/intel-mux-drcfg.c            | 157 +++++++++++++++++++++++
 drivers/usb/mux/intel-mux-gpio.c             | 120 ++++++++++++++++++
 drivers/usb/mux/intel-mux.c                  | 180 +++++++++++++++++++++++++++
 include/linux/usb/intel-mux.h                |  43 +++++++
 16 files changed, 706 insertions(+), 3 deletions(-)
 create mode 100644 drivers/mfd/intel-vuport.c
 create mode 100644 drivers/usb/mux/Kconfig
 create mode 100644 drivers/usb/mux/Makefile
 create mode 100644 drivers/usb/mux/intel-mux-drcfg.c
 create mode 100644 drivers/usb/mux/intel-mux-gpio.c
 create mode 100644 drivers/usb/mux/intel-mux.c
 create mode 100644 include/linux/usb/intel-mux.h
Signature · 1 line
2.1.4
author
#1 lore raw

PATCH v4 1/7 extcon: usb-gpio: add device binding for platform device

This is needed to handle the GPIO connected USB ID pin found on Intel Baytrail devices.

---
 drivers/extcon/extcon-usb-gpio.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/extcon/extcon-usb-gpio.c +7 -0
index 2b2fecf..af9c8b0 100644
@@ -206,6 +206,12 @@ static const struct of_device_id usb_extcon_dt_match[] = {
 };
 MODULE_DEVICE_TABLE(of, usb_extcon_dt_match);
 
+static const struct platform_device_id usb_extcon_platform_ids[] = {
+	{ .name = "extcon-usb-gpio", },
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(platform, usb_extcon_platform_ids);
+
 static struct platform_driver usb_extcon_driver = {
 	.probe		= usb_extcon_probe,
 	.remove		= usb_extcon_remove,
@@ -214,6 +220,7 @@ static struct platform_driver usb_extcon_driver = {
 		.pm	= &usb_extcon_pm_ops,
 		.of_match_table = usb_extcon_dt_match,
 	},
+	.id_table = usb_extcon_platform_ids,
 };
 
 module_platform_driver(usb_extcon_driver);
Signature · 1 line
2.1.4
author
#2 lore raw

PATCH v4 2/7 extcon: usb-gpio: add support for ACPI gpio interface

In reply to Lu Baolu

GPIO resource could be retrieved through APCI as well.

---
 drivers/extcon/extcon-usb-gpio.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/extcon/extcon-usb-gpio.c +2 -1
index af9c8b0..472c431 100644
@@ -26,6 +26,7 @@
 #include <linux/platform_device.h>
 #include <linux/slab.h>
 #include <linux/workqueue.h>
+#include <linux/acpi.h>
 
 #define USB_GPIO_DEBOUNCE_MS	20	/* ms */
 
@@ -91,7 +92,7 @@ static int usb_extcon_probe(struct platform_device *pdev)
 	struct usb_extcon_info *info;
 	int ret;
 
-	if (!np)
+	if (!np && !ACPI_HANDLE(dev))
 		return -EINVAL;
 
 	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
Signature · 1 line
2.1.4
author
#3 lore raw

PATCH v4 3/7 usb: mux: add common code for Intel dual role port mux

In reply to Lu Baolu

Several Intel PCHs and SOCs have an internal mux that is used to share one USB port between device controller and host controller.

A usb port mux could be abstracted as the following elements:
1) mux state: HOST or PERIPHERAL;
2) an extcon cable which triggers the change of mux state between HOST and PERIPHERAL;
3) The required action to do the real port switch.

This patch adds the common code to handle usb port mux. With this common code, the individual mux driver, which always is platform dependent, could focus on the real operation of mux switch.

---
 Documentation/ABI/testing/sysfs-bus-platform |  15 +++
 MAINTAINERS                                  |   7 ++
 drivers/usb/Kconfig                          |   2 +
 drivers/usb/Makefile                         |   1 +
 drivers/usb/mux/Kconfig                      |  12 ++
 drivers/usb/mux/Makefile                     |   4 +
 drivers/usb/mux/intel-mux.c                  | 180 +++++++++++++++++++++++++++
 include/linux/usb/intel-mux.h                |  43 +++++++
 8 files changed, 264 insertions(+)
 create mode 100644 drivers/usb/mux/Kconfig
 create mode 100644 drivers/usb/mux/Makefile
 create mode 100644 drivers/usb/mux/intel-mux.c
 create mode 100644 include/linux/usb/intel-mux.h
Documentation/ABI/testing/sysfs-bus-platform +15 -0
index 5172a61..23bf76e 100644
@@ -18,3 +18,18 @@ Description:
 		devices to opt-out of driver binding using a driver_override
 		name such as "none".  Only a single driver may be specified in
 		the override, there is no support for parsing delimiters.
+
+What:		/sys/bus/platform/devices/.../port_mux
+Date:		Febuary 2016
+Contact:	Lu Baolu <[email protected]>
+Description:
+		In some platforms, a single USB port is shared between a USB host
+		controller and a device controller. A USB mux driver is needed to
+		handle the port mux. port_mux attribute shows and stores the mux
+		state.
+		For read:
+		'peripheral' - mux switched to PERIPHERAL controller;
+		'host'       - mux switched to HOST controller.
+		For write:
+		'peripheral' - mux will be switched to PERIPHERAL controller;
+		'host'       - mux will be switched to HOST controller.
MAINTAINERS +7 -0
index da3e4d8..0dbee11 100644
@@ -11399,6 +11399,13 @@ T:	git git://git.kernel.org/pub/scm/linux/kernel/git/balbi/usb.git
 S:	Maintained
 F:	drivers/usb/phy/
 
+USB PORT MUX DRIVER
+M:	Lu Baolu <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	include/linux/usb/intel-mux.h
+F:	drivers/usb/mux/intel-mux.c
+
 USB PRINTER DRIVER (usblp)
 M:	Pete Zaitcev <[email protected]>
 L:	[email protected]
drivers/usb/Kconfig +2 -0
index 8ed451d..dbd6620 100644
@@ -149,6 +149,8 @@ endif # USB
 
 source "drivers/usb/phy/Kconfig"
 
+source "drivers/usb/mux/Kconfig"
+
 source "drivers/usb/gadget/Kconfig"
 
 config USB_LED_TRIG
drivers/usb/Makefile +1 -0
index d5c57f1..6433f0c 100644
@@ -6,6 +6,7 @@
 
 obj-$(CONFIG_USB)		+= core/
 obj-$(CONFIG_USB_SUPPORT)	+= phy/
+obj-$(CONFIG_USB_SUPPORT)	+= mux/
 
 obj-$(CONFIG_USB_DWC3)		+= dwc3/
 obj-$(CONFIG_USB_DWC2)		+= dwc2/
drivers/usb/mux/Kconfig new file +12 -0
new file mode 100644
index 0000000..62e2cc3
@@ -0,0 +1,12 @@
+#
+# USB port mux driver configuration
+#
+menu "USB Port MUX drivers"
+config INTEL_USB_MUX
+	select EXTCON
+	def_bool n
+	help
+	  Common code for all Intel dual role port mux drivers. All Intel
+	  usb port mux drivers should select it.
+
+endmenu
drivers/usb/mux/Makefile new file +4 -0
new file mode 100644
index 0000000..84f0ae8
@@ -0,0 +1,4 @@
+#
+# Makefile for USB port mux drivers
+#
+obj-$(CONFIG_INTEL_USB_MUX)		+= intel-mux.o
drivers/usb/mux/intel-mux.c new file +180 -0

180 lines not on this page · raw message

include/linux/usb/intel-mux.h new file +43 -0

43 lines not on this page · raw message

Signature · 1 line
2.1.4
maintainer
#4 lore raw

Re: [PATCH v4 3/7] usb: mux: add common code for Intel dual role port mux

Hi Lu,

To handle extcon (external connector), I implemented the unique id
for each external connector on patch[1] instead of using the ambiguous string type.
[1] 2a9de9c0f08d6 (extcon: Use the unique id for external connector instead of string)

So I recommend that you should use the unique id (ex. EXTCON_USB, EXTCON_USB_HOST) with extcon_register_notifier(), extcon_get_cable_state_() and extcon_set_cable_state_().

extcon_register_interest() is deprecated-> extcon_register_notifier() extcon_get_cable_state() is deprecated -> extcon_get_cable_state_() extcon_set_cable_state() is deprecated -> extcon_set_cable_state_()

You can refer to usage for new function with unique id on patch[2]
[2] 5960387a2fb83 (usb: dwc3: omap: Replace deprecated API of extcon)

I'm sorry for late reply. I add the some comment on below.

Quoted 187 lines · Lu Baolu

Several Intel PCHs and SOCs have an internal mux that is used to share one USB port between device controller and host controller.

A usb port mux could be abstracted as the following elements:
1) mux state: HOST or PERIPHERAL;
2) an extcon cable which triggers the change of mux state between HOST and PERIPHERAL;
3) The required action to do the real port switch.

This patch adds the common code to handle usb port mux. With this common code, the individual mux driver, which always is platform dependent, could focus on the real operation of mux switch.

---
 Documentation/ABI/testing/sysfs-bus-platform |  15 +++
 MAINTAINERS                                  |   7 ++
 drivers/usb/Kconfig                          |   2 +
 drivers/usb/Makefile                         |   1 +
 drivers/usb/mux/Kconfig                      |  12 ++
 drivers/usb/mux/Makefile                     |   4 +
 drivers/usb/mux/intel-mux.c                  | 180 +++++++++++++++++++++++++++
 include/linux/usb/intel-mux.h                |  43 +++++++
 8 files changed, 264 insertions(+)
 create mode 100644 drivers/usb/mux/Kconfig
 create mode 100644 drivers/usb/mux/Makefile
 create mode 100644 drivers/usb/mux/intel-mux.c
 create mode 100644 include/linux/usb/intel-mux.h
Documentation/ABI/testing/sysfs-bus-platform +15 -0
index 5172a61..23bf76e 100644
@@ -18,3 +18,18 @@ Description:
 		devices to opt-out of driver binding using a driver_override
 		name such as "none".  Only a single driver may be specified in
 		the override, there is no support for parsing delimiters.
+
+What:		/sys/bus/platform/devices/.../port_mux
+Date:		Febuary 2016
+Contact:	Lu Baolu <[email protected]>
+Description:
+		In some platforms, a single USB port is shared between a USB host
+		controller and a device controller. A USB mux driver is needed to
+		handle the port mux. port_mux attribute shows and stores the mux
+		state.
+		For read:
+		'peripheral' - mux switched to PERIPHERAL controller;
+		'host'       - mux switched to HOST controller.
+		For write:
+		'peripheral' - mux will be switched to PERIPHERAL controller;
+		'host'       - mux will be switched to HOST controller.
MAINTAINERS +7 -0
index da3e4d8..0dbee11 100644
@@ -11399,6 +11399,13 @@ T:	git git://git.kernel.org/pub/scm/linux/kernel/git/balbi/usb.git
 S:	Maintained
 F:	drivers/usb/phy/
 
+USB PORT MUX DRIVER
+M:	Lu Baolu <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	include/linux/usb/intel-mux.h
+F:	drivers/usb/mux/intel-mux.c
+
 USB PRINTER DRIVER (usblp)
 M:	Pete Zaitcev <[email protected]>
 L:	[email protected]
drivers/usb/Kconfig +2 -0
index 8ed451d..dbd6620 100644
@@ -149,6 +149,8 @@ endif # USB
 
 source "drivers/usb/phy/Kconfig"
 
+source "drivers/usb/mux/Kconfig"
+
 source "drivers/usb/gadget/Kconfig"
 
 config USB_LED_TRIG
drivers/usb/Makefile +1 -0
index d5c57f1..6433f0c 100644
@@ -6,6 +6,7 @@
 
 obj-$(CONFIG_USB)		+= core/
 obj-$(CONFIG_USB_SUPPORT)	+= phy/
+obj-$(CONFIG_USB_SUPPORT)	+= mux/
 
 obj-$(CONFIG_USB_DWC3)		+= dwc3/
 obj-$(CONFIG_USB_DWC2)		+= dwc2/
drivers/usb/mux/Kconfig new file +12 -0
new file mode 100644
index 0000000..62e2cc3
@@ -0,0 +1,12 @@
+#
+# USB port mux driver configuration
+#
+menu "USB Port MUX drivers"
+config INTEL_USB_MUX
+	select EXTCON
+	def_bool n
+	help
+	  Common code for all Intel dual role port mux drivers. All Intel
+	  usb port mux drivers should select it.
+
+endmenu
drivers/usb/mux/Makefile new file +4 -0
new file mode 100644
index 0000000..84f0ae8
@@ -0,0 +1,4 @@
+#
+# Makefile for USB port mux drivers
+#
+obj-$(CONFIG_INTEL_USB_MUX)		+= intel-mux.o
drivers/usb/mux/intel-mux.c new file +56 -0
new file mode 100644
index 0000000..bb7b192
@@ -0,0 +1,180 @@
+/**
+ * intel_mux.c - USB Port Mux support
+ *
+ * Copyright (C) 2016 Intel Corporation
+ *
+ * Author: Lu Baolu <[email protected]>
+ *
+ * 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/notifier.h>
+#include <linux/extcon.h>
+#include <linux/err.h>
+
+struct intel_usb_mux {
+	struct device	*dev;
+	char		*cable_name;
+	int		(*cable_set_cb)(struct device *dev);
+	int		(*cable_unset_cb)(struct device *dev);
+
+	struct notifier_block		nb;
+	struct extcon_specific_cable_nb	obj;
+
+	/*
+	 * The state of the mux.
+	 * 0, 1 - mux switch state
+	 * -1   - uninitialized state
+	 */
+	int				mux_state;
+
+	 /* lock for mux_state */
+	struct mutex			mux_mutex;
+};
+
+static int usb_mux_change_state(struct intel_usb_mux *mux, int state)
+{
+	int ret;
+	struct device *dev = mux->dev;
+
+	dev_WARN_ONCE(dev,
+		      !mutex_is_locked(&mux->mux_mutex),
+		      "mutex is unlocked\n");
+
+	mux->mux_state = state;
+
+	if (mux->mux_state)
+		ret = mux->cable_set_cb(dev);
+	else
+		ret = mux->cable_unset_cb(dev);
+
+	return ret;
+}
+
+static int usb_mux_notifier(struct notifier_block *nb,

drivers/usb/mux/intel-mux.c line 66

Quoted 10 lines
drivers/usb/mux/intel-mux.c new file +10 -0
+			    unsigned long event, void *ptr)
+{
+	struct intel_usb_mux *mux;
+	int state;
+	int ret = NOTIFY_DONE;
+
+	mux = container_of(nb, struct intel_usb_mux, nb);
+
+	state = extcon_get_cable_state(mux->obj.edev,
+				       mux->cable_name);

Use the extcon_get_cable_stet_().

Quoted 62 lines
drivers/usb/mux/intel-mux.c new file +62 -0
+
+	if (mux->mux_state == -1 || mux->mux_state != state) {
+		mutex_lock(&mux->mux_mutex);
+		ret = usb_mux_change_state(mux, state);
+		mutex_unlock(&mux->mux_mutex);
+	}
+
+	return ret;
+}
+
+static ssize_t port_mux_show(struct device *dev,
+			     struct device_attribute *attr, char *buf)
+{
+	struct intel_usb_mux *mux = dev_get_drvdata(dev);
+
+	if (dev_WARN_ONCE(dev, !mux, "mux without data structure\n"))
+		return 0;
+
+	return sprintf(buf, "%s\n", mux->mux_state ? "host" : "peripheral");
+}
+
+static ssize_t port_mux_store(struct device *dev,
+			      struct device_attribute *attr,
+			      const char *buf, size_t count)
+{
+	struct intel_usb_mux *mux = dev_get_drvdata(dev);
+	int state;
+
+	if (dev_WARN_ONCE(dev, !mux, "mux without data structure\n"))
+		return -EINVAL;
+
+	if (sysfs_streq(buf, "peripheral"))
+		state = 0;
+	else if (sysfs_streq(buf, "host"))
+		state = 1;
+	else
+		return -EINVAL;
+
+	mutex_lock(&mux->mux_mutex);
+	usb_mux_change_state(mux, state);
+	mutex_unlock(&mux->mux_mutex);
+
+	return count;
+}
+static DEVICE_ATTR_RW(port_mux);
+
+int intel_usb_mux_bind_cable(struct device *dev,
+			     char *extcon_name,
+			     char *cable_name,
+			     int (*cable_set_cb)(struct device *dev),
+			     int (*cable_unset_cb)(struct device *dev))
+{
+	int ret;
+	struct intel_usb_mux *mux;
+
+	if (!cable_name)
+		return -ENODEV;
+
+	mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL);
+	if (!mux)
+		return -ENOMEM;
+

drivers/usb/mux/intel-mux.c line 138

Quoted 10 lines
drivers/usb/mux/intel-mux.c new file +10 -0
+	mux->dev = dev;
+	mux->cable_name = kstrdup(cable_name, GFP_KERNEL);
+	mux->cable_set_cb = cable_set_cb;
+	mux->cable_unset_cb = cable_unset_cb;
+	mux->nb.notifier_call = usb_mux_notifier;
+	mutex_init(&mux->mux_mutex);
+	mux->mux_state = -1;
+	dev_set_drvdata(dev, mux);
+	ret = extcon_register_interest(&mux->obj, extcon_name,
+				       cable_name, &mux->nb);

Use the extcon_register_notifier()

drivers/usb/mux/intel-mux.c line 150

Quoted 12 lines
drivers/usb/mux/intel-mux.c new file +12 -0
+	if (ret) {
+		kfree(mux->cable_name);
+		dev_err(dev, "failed to register extcon notifier\n");
+		return -ENODEV;
+	}
+
+	usb_mux_notifier(&mux->nb, 0, NULL);
+
+	/* register the sysfs interface */
+	ret = device_create_file(dev, &dev_attr_port_mux);
+	if (ret) {
+		extcon_unregister_interest(&mux->obj);

Use the extcon_unregister_notifier()

Quoted 5 lines
drivers/usb/mux/intel-mux.c new file +5 -0
+		kfree(mux->cable_name);
+		dev_err(dev, "failed to create sysfs attribute\n");
+		return -ENODEV;
+	}
+

drivers/usb/mux/intel-mux.c line 165

Quoted 10 lines
drivers/usb/mux/intel-mux.c new file +10 -0
+	return 0;
+}
+EXPORT_SYMBOL_GPL(intel_usb_mux_bind_cable);
+
+int intel_usb_mux_unbind_cable(struct device *dev)
+{
+	struct intel_usb_mux *mux = dev_get_drvdata(dev);
+
+	device_remove_file(dev, &dev_attr_port_mux);
+	extcon_unregister_interest(&mux->obj);

Use the extcon_unregister_notifier()

Quoted 55 lines
drivers/usb/mux/intel-mux.c new file +15 -0
+	kfree(mux->cable_name);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(intel_usb_mux_unbind_cable);
+
+#ifdef CONFIG_PM_SLEEP
+void intel_usb_mux_complete(struct device *dev)
+{
+	struct intel_usb_mux *mux = dev_get_drvdata(dev);
+
+	usb_mux_notifier(&mux->nb, 0, NULL);
+}
+EXPORT_SYMBOL_GPL(intel_usb_mux_complete);
+#endif
include/linux/usb/intel-mux.h new file +33 -0
new file mode 100644
index 0000000..fd5612d
@@ -0,0 +1,43 @@
+/**
+ * intel_mux.h - USB Port Mux definitions
+ *
+ * Copyright (C) 2016 Intel Corporation
+ *
+ * Author: Lu Baolu <[email protected]>
+ *
+ * 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.
+ */
+
+#ifndef __LINUX_USB_INTEL_MUX_H
+#define __LINUX_USB_INTEL_MUX_H
+
+#if IS_ENABLED(CONFIG_INTEL_USB_MUX)
+int intel_usb_mux_bind_cable(struct device *dev, char *extcon_name,
+			     char *cable_name,
+			     int (*cable_set_cb)(struct device *dev),
+			     int (*cable_unset_cb)(struct device *dev));
+int intel_usb_mux_unbind_cable(struct device *dev);
+#ifdef CONFIG_PM_SLEEP
+void intel_usb_mux_complete(struct device *dev);
+#endif
+
+#else
+static inline int
+intel_usb_mux_bind_cable(struct device *dev,
+			 char *extcon_name,
+			 char *cable_name,
+			 int (*cable_set_cb)(struct device *dev),
+			 int (*cable_unset_cb)(struct device *dev))
+{

include/linux/usb/intel-mux.h line 43

Quoted 10 lines
include/linux/usb/intel-mux.h new file +10 -0
+	return -ENODEV;
+}
+
+static inline int intel_usb_mux_unbind_cable(struct device *dev)
+{
+	return 0;
+}
+#endif /* CONFIG_INTEL_USB_MUX */
+
+#endif /* __LINUX_USB_INTEL_MUX_H */

Best Regards,
Chanwoo Choi

author
#5 lore raw

Re: [PATCH v4 3/7] usb: mux: add common code for Intel dual role port mux

Hi Chanwoo,

Quoted 6 lines · Chanwoo Choi

Hi Lu,

To handle extcon (external connector), I implemented the unique id
for each external connector on patch[1] instead of using the ambiguous string type.
[1] 2a9de9c0f08d6 (extcon: Use the unique id for external connector instead of string)

Quoted 6 lines

So I recommend that you should use the unique id (ex. EXTCON_USB, EXTCON_USB_HOST) with extcon_register_notifier(), extcon_get_cable_state_() and extcon_set_cable_state_().

extcon_register_interest() is deprecated-> extcon_register_notifier() extcon_get_cable_state() is deprecated -> extcon_get_cable_state_() extcon_set_cable_state() is deprecated -> extcon_set_cable_state_()

Sure. I will use the new interfaces with a refreshed patch serial later.

Quoted 4 lines

You can refer to usage for new function with unique id on patch[2]
[2] 5960387a2fb83 (usb: dwc3: omap: Replace deprecated API of extcon)

Thanks. That's helpful.

By the way, is extcon_get_extcon_dev() still available?

Quoted 2 lines

I'm sorry for late reply. I add the some comment on below.

Never mind. Thank you for reminding me.

Best regards,
Baolu

Quoted 375 lines · Lu Baolu
Quoted 187 lines

Several Intel PCHs and SOCs have an internal mux that is used to share one USB port between device controller and host controller.

A usb port mux could be abstracted as the following elements:
1) mux state: HOST or PERIPHERAL;
2) an extcon cable which triggers the change of mux state between HOST and PERIPHERAL;
3) The required action to do the real port switch.

This patch adds the common code to handle usb port mux. With this common code, the individual mux driver, which always is platform dependent, could focus on the real operation of mux switch.

---
 Documentation/ABI/testing/sysfs-bus-platform |  15 +++
 MAINTAINERS                                  |   7 ++
 drivers/usb/Kconfig                          |   2 +
 drivers/usb/Makefile                         |   1 +
 drivers/usb/mux/Kconfig                      |  12 ++
 drivers/usb/mux/Makefile                     |   4 +
 drivers/usb/mux/intel-mux.c                  | 180 +++++++++++++++++++++++++++
 include/linux/usb/intel-mux.h                |  43 +++++++
 8 files changed, 264 insertions(+)
 create mode 100644 drivers/usb/mux/Kconfig
 create mode 100644 drivers/usb/mux/Makefile
 create mode 100644 drivers/usb/mux/intel-mux.c
 create mode 100644 include/linux/usb/intel-mux.h
Documentation/ABI/testing/sysfs-bus-platform +15 -0
index 5172a61..23bf76e 100644
@@ -18,3 +18,18 @@ Description:
 		devices to opt-out of driver binding using a driver_override
 		name such as "none".  Only a single driver may be specified in
 		the override, there is no support for parsing delimiters.
+
+What:		/sys/bus/platform/devices/.../port_mux
+Date:		Febuary 2016
+Contact:	Lu Baolu <[email protected]>
+Description:
+		In some platforms, a single USB port is shared between a USB host
+		controller and a device controller. A USB mux driver is needed to
+		handle the port mux. port_mux attribute shows and stores the mux
+		state.
+		For read:
+		'peripheral' - mux switched to PERIPHERAL controller;
+		'host'       - mux switched to HOST controller.
+		For write:
+		'peripheral' - mux will be switched to PERIPHERAL controller;
+		'host'       - mux will be switched to HOST controller.
MAINTAINERS +7 -0
index da3e4d8..0dbee11 100644
@@ -11399,6 +11399,13 @@ T:	git git://git.kernel.org/pub/scm/linux/kernel/git/balbi/usb.git
 S:	Maintained
 F:	drivers/usb/phy/
 
+USB PORT MUX DRIVER
+M:	Lu Baolu <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	include/linux/usb/intel-mux.h
+F:	drivers/usb/mux/intel-mux.c
+
 USB PRINTER DRIVER (usblp)
 M:	Pete Zaitcev <[email protected]>
 L:	[email protected]
drivers/usb/Kconfig +2 -0
index 8ed451d..dbd6620 100644
@@ -149,6 +149,8 @@ endif # USB
 
 source "drivers/usb/phy/Kconfig"
 
+source "drivers/usb/mux/Kconfig"
+
 source "drivers/usb/gadget/Kconfig"
 
 config USB_LED_TRIG
drivers/usb/Makefile +1 -0
index d5c57f1..6433f0c 100644
@@ -6,6 +6,7 @@
 
 obj-$(CONFIG_USB)		+= core/
 obj-$(CONFIG_USB_SUPPORT)	+= phy/
+obj-$(CONFIG_USB_SUPPORT)	+= mux/
 
 obj-$(CONFIG_USB_DWC3)		+= dwc3/
 obj-$(CONFIG_USB_DWC2)		+= dwc2/
drivers/usb/mux/Kconfig new file +12 -0
new file mode 100644
index 0000000..62e2cc3
@@ -0,0 +1,12 @@
+#
+# USB port mux driver configuration
+#
+menu "USB Port MUX drivers"
+config INTEL_USB_MUX
+	select EXTCON
+	def_bool n
+	help
+	  Common code for all Intel dual role port mux drivers. All Intel
+	  usb port mux drivers should select it.
+
+endmenu
drivers/usb/mux/Makefile new file +4 -0
new file mode 100644
index 0000000..84f0ae8
@@ -0,0 +1,4 @@
+#
+# Makefile for USB port mux drivers
+#
+obj-$(CONFIG_INTEL_USB_MUX)		+= intel-mux.o
drivers/usb/mux/intel-mux.c new file +56 -0
new file mode 100644
index 0000000..bb7b192
@@ -0,0 +1,180 @@
+/**
+ * intel_mux.c - USB Port Mux support
+ *
+ * Copyright (C) 2016 Intel Corporation
+ *
+ * Author: Lu Baolu <[email protected]>
+ *
+ * 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/notifier.h>
+#include <linux/extcon.h>
+#include <linux/err.h>
+
+struct intel_usb_mux {
+	struct device	*dev;
+	char		*cable_name;
+	int		(*cable_set_cb)(struct device *dev);
+	int		(*cable_unset_cb)(struct device *dev);
+
+	struct notifier_block		nb;
+	struct extcon_specific_cable_nb	obj;
+
+	/*
+	 * The state of the mux.
+	 * 0, 1 - mux switch state
+	 * -1   - uninitialized state
+	 */
+	int				mux_state;
+
+	 /* lock for mux_state */
+	struct mutex			mux_mutex;
+};
+
+static int usb_mux_change_state(struct intel_usb_mux *mux, int state)
+{
+	int ret;
+	struct device *dev = mux->dev;
+
+	dev_WARN_ONCE(dev,
+		      !mutex_is_locked(&mux->mux_mutex),
+		      "mutex is unlocked\n");
+
+	mux->mux_state = state;
+
+	if (mux->mux_state)
+		ret = mux->cable_set_cb(dev);
+	else
+		ret = mux->cable_unset_cb(dev);
+
+	return ret;
+}
+
+static int usb_mux_notifier(struct notifier_block *nb,

drivers/usb/mux/intel-mux.c line 66

Quoted 10 lines
drivers/usb/mux/intel-mux.c new file +10 -0
+			    unsigned long event, void *ptr)
+{
+	struct intel_usb_mux *mux;
+	int state;
+	int ret = NOTIFY_DONE;
+
+	mux = container_of(nb, struct intel_usb_mux, nb);
+
+	state = extcon_get_cable_state(mux->obj.edev,
+				       mux->cable_name);

Use the extcon_get_cable_stet_().

Quoted 62 lines
drivers/usb/mux/intel-mux.c new file +62 -0
+
+	if (mux->mux_state == -1 || mux->mux_state != state) {
+		mutex_lock(&mux->mux_mutex);
+		ret = usb_mux_change_state(mux, state);
+		mutex_unlock(&mux->mux_mutex);
+	}
+
+	return ret;
+}
+
+static ssize_t port_mux_show(struct device *dev,
+			     struct device_attribute *attr, char *buf)
+{
+	struct intel_usb_mux *mux = dev_get_drvdata(dev);
+
+	if (dev_WARN_ONCE(dev, !mux, "mux without data structure\n"))
+		return 0;
+
+	return sprintf(buf, "%s\n", mux->mux_state ? "host" : "peripheral");
+}
+
+static ssize_t port_mux_store(struct device *dev,
+			      struct device_attribute *attr,
+			      const char *buf, size_t count)
+{
+	struct intel_usb_mux *mux = dev_get_drvdata(dev);
+	int state;
+
+	if (dev_WARN_ONCE(dev, !mux, "mux without data structure\n"))
+		return -EINVAL;
+
+	if (sysfs_streq(buf, "peripheral"))
+		state = 0;
+	else if (sysfs_streq(buf, "host"))
+		state = 1;
+	else
+		return -EINVAL;
+
+	mutex_lock(&mux->mux_mutex);
+	usb_mux_change_state(mux, state);
+	mutex_unlock(&mux->mux_mutex);
+
+	return count;
+}
+static DEVICE_ATTR_RW(port_mux);
+
+int intel_usb_mux_bind_cable(struct device *dev,
+			     char *extcon_name,
+			     char *cable_name,
+			     int (*cable_set_cb)(struct device *dev),
+			     int (*cable_unset_cb)(struct device *dev))
+{
+	int ret;
+	struct intel_usb_mux *mux;
+
+	if (!cable_name)
+		return -ENODEV;
+
+	mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL);
+	if (!mux)
+		return -ENOMEM;
+

drivers/usb/mux/intel-mux.c line 138

Quoted 10 lines
drivers/usb/mux/intel-mux.c new file +10 -0
+	mux->dev = dev;
+	mux->cable_name = kstrdup(cable_name, GFP_KERNEL);
+	mux->cable_set_cb = cable_set_cb;
+	mux->cable_unset_cb = cable_unset_cb;
+	mux->nb.notifier_call = usb_mux_notifier;
+	mutex_init(&mux->mux_mutex);
+	mux->mux_state = -1;
+	dev_set_drvdata(dev, mux);
+	ret = extcon_register_interest(&mux->obj, extcon_name,
+				       cable_name, &mux->nb);

Use the extcon_register_notifier()

drivers/usb/mux/intel-mux.c line 150

Quoted 12 lines
drivers/usb/mux/intel-mux.c new file +12 -0
+	if (ret) {
+		kfree(mux->cable_name);
+		dev_err(dev, "failed to register extcon notifier\n");
+		return -ENODEV;
+	}
+
+	usb_mux_notifier(&mux->nb, 0, NULL);
+
+	/* register the sysfs interface */
+	ret = device_create_file(dev, &dev_attr_port_mux);
+	if (ret) {
+		extcon_unregister_interest(&mux->obj);

Use the extcon_unregister_notifier()

Quoted 5 lines
drivers/usb/mux/intel-mux.c new file +5 -0
+		kfree(mux->cable_name);
+		dev_err(dev, "failed to create sysfs attribute\n");
+		return -ENODEV;
+	}
+

drivers/usb/mux/intel-mux.c line 165

Quoted 10 lines
drivers/usb/mux/intel-mux.c new file +10 -0
+	return 0;
+}
+EXPORT_SYMBOL_GPL(intel_usb_mux_bind_cable);
+
+int intel_usb_mux_unbind_cable(struct device *dev)
+{
+	struct intel_usb_mux *mux = dev_get_drvdata(dev);
+
+	device_remove_file(dev, &dev_attr_port_mux);
+	extcon_unregister_interest(&mux->obj);

Use the extcon_unregister_notifier()

Quoted 55 lines
drivers/usb/mux/intel-mux.c new file +15 -0
+	kfree(mux->cable_name);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(intel_usb_mux_unbind_cable);
+
+#ifdef CONFIG_PM_SLEEP
+void intel_usb_mux_complete(struct device *dev)
+{
+	struct intel_usb_mux *mux = dev_get_drvdata(dev);
+
+	usb_mux_notifier(&mux->nb, 0, NULL);
+}
+EXPORT_SYMBOL_GPL(intel_usb_mux_complete);
+#endif
include/linux/usb/intel-mux.h new file +33 -0
new file mode 100644
index 0000000..fd5612d
@@ -0,0 +1,43 @@
+/**
+ * intel_mux.h - USB Port Mux definitions
+ *
+ * Copyright (C) 2016 Intel Corporation
+ *
+ * Author: Lu Baolu <[email protected]>
+ *
+ * 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.
+ */
+
+#ifndef __LINUX_USB_INTEL_MUX_H
+#define __LINUX_USB_INTEL_MUX_H
+
+#if IS_ENABLED(CONFIG_INTEL_USB_MUX)
+int intel_usb_mux_bind_cable(struct device *dev, char *extcon_name,
+			     char *cable_name,
+			     int (*cable_set_cb)(struct device *dev),
+			     int (*cable_unset_cb)(struct device *dev));
+int intel_usb_mux_unbind_cable(struct device *dev);
+#ifdef CONFIG_PM_SLEEP
+void intel_usb_mux_complete(struct device *dev);
+#endif
+
+#else
+static inline int
+intel_usb_mux_bind_cable(struct device *dev,
+			 char *extcon_name,
+			 char *cable_name,
+			 int (*cable_set_cb)(struct device *dev),
+			 int (*cable_unset_cb)(struct device *dev))
+{

include/linux/usb/intel-mux.h line 43

Quoted 10 lines
include/linux/usb/intel-mux.h new file +10 -0
+	return -ENODEV;
+}
+
+static inline int intel_usb_mux_unbind_cable(struct device *dev)
+{
+	return 0;
+}
+#endif /* CONFIG_INTEL_USB_MUX */
+
+#endif /* __LINUX_USB_INTEL_MUX_H */

Best Regards,
Chanwoo Choi

maintainer
#6 lore raw

Re: [PATCH v4 3/7] usb: mux: add common code for Intel dual role port mux

Hi Lu,

Quoted 20 lines · Lu Baolu

Hi Chanwoo,

Quoted 6 lines · Chanwoo Choi

Hi Lu,

To handle extcon (external connector), I implemented the unique id
for each external connector on patch[1] instead of using the ambiguous string type.
[1] 2a9de9c0f08d6 (extcon: Use the unique id for external connector instead of string)

Quoted 6 lines

So I recommend that you should use the unique id (ex. EXTCON_USB, EXTCON_USB_HOST) with extcon_register_notifier(), extcon_get_cable_state_() and extcon_set_cable_state_().

extcon_register_interest() is deprecated-> extcon_register_notifier() extcon_get_cable_state() is deprecated -> extcon_get_cable_state_() extcon_set_cable_state() is deprecated -> extcon_set_cable_state_()

Sure. I will use the new interfaces with a refreshed patch serial later.

Quoted 2 lines
Quoted 6 lines
Quoted 2 lines

You can refer to usage for new function with unique id on patch[2]
[2] 5960387a2fb83 (usb: dwc3: omap: Replace deprecated API of extcon)

Thanks. That's helpful.

By the way, is extcon_get_extcon_dev() still available?

Yes.
This function find the extcon device with string name.

If extcon client driver use the platform_data to get the name of extcon device, the extcon client driver should use the extcon_get_extcon_dev() funtcion. Because we can not pass the point(instance) of extcon device through platform_data.

But, if extcon client driver support the devicetree, we better to use the extcon_get_edev_by_phandle().

Best Regards,
Chanwoo Choi

Quoted 387 lines
Quoted 3 lines

I'm sorry for late reply. I add the some comment on below.

Never mind. Thank you for reminding me.

Best regards,
Baolu

Quoted 375 lines · Lu Baolu
Quoted 187 lines

Several Intel PCHs and SOCs have an internal mux that is used to share one USB port between device controller and host controller.

A usb port mux could be abstracted as the following elements:
1) mux state: HOST or PERIPHERAL;
2) an extcon cable which triggers the change of mux state between HOST and PERIPHERAL;
3) The required action to do the real port switch.

This patch adds the common code to handle usb port mux. With this common code, the individual mux driver, which always is platform dependent, could focus on the real operation of mux switch.

---
 Documentation/ABI/testing/sysfs-bus-platform |  15 +++
 MAINTAINERS                                  |   7 ++
 drivers/usb/Kconfig                          |   2 +
 drivers/usb/Makefile                         |   1 +
 drivers/usb/mux/Kconfig                      |  12 ++
 drivers/usb/mux/Makefile                     |   4 +
 drivers/usb/mux/intel-mux.c                  | 180 +++++++++++++++++++++++++++
 include/linux/usb/intel-mux.h                |  43 +++++++
 8 files changed, 264 insertions(+)
 create mode 100644 drivers/usb/mux/Kconfig
 create mode 100644 drivers/usb/mux/Makefile
 create mode 100644 drivers/usb/mux/intel-mux.c
 create mode 100644 include/linux/usb/intel-mux.h
Documentation/ABI/testing/sysfs-bus-platform +15 -0
index 5172a61..23bf76e 100644
@@ -18,3 +18,18 @@ Description:
 		devices to opt-out of driver binding using a driver_override
 		name such as "none".  Only a single driver may be specified in
 		the override, there is no support for parsing delimiters.
+
+What:		/sys/bus/platform/devices/.../port_mux
+Date:		Febuary 2016
+Contact:	Lu Baolu <[email protected]>
+Description:
+		In some platforms, a single USB port is shared between a USB host
+		controller and a device controller. A USB mux driver is needed to
+		handle the port mux. port_mux attribute shows and stores the mux
+		state.
+		For read:
+		'peripheral' - mux switched to PERIPHERAL controller;
+		'host'       - mux switched to HOST controller.
+		For write:
+		'peripheral' - mux will be switched to PERIPHERAL controller;
+		'host'       - mux will be switched to HOST controller.
MAINTAINERS +7 -0
index da3e4d8..0dbee11 100644
@@ -11399,6 +11399,13 @@ T:	git git://git.kernel.org/pub/scm/linux/kernel/git/balbi/usb.git
 S:	Maintained
 F:	drivers/usb/phy/
 
+USB PORT MUX DRIVER
+M:	Lu Baolu <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	include/linux/usb/intel-mux.h
+F:	drivers/usb/mux/intel-mux.c
+
 USB PRINTER DRIVER (usblp)
 M:	Pete Zaitcev <[email protected]>
 L:	[email protected]
drivers/usb/Kconfig +2 -0
index 8ed451d..dbd6620 100644
@@ -149,6 +149,8 @@ endif # USB
 
 source "drivers/usb/phy/Kconfig"
 
+source "drivers/usb/mux/Kconfig"
+
 source "drivers/usb/gadget/Kconfig"
 
 config USB_LED_TRIG
drivers/usb/Makefile +1 -0
index d5c57f1..6433f0c 100644
@@ -6,6 +6,7 @@
 
 obj-$(CONFIG_USB)		+= core/
 obj-$(CONFIG_USB_SUPPORT)	+= phy/
+obj-$(CONFIG_USB_SUPPORT)	+= mux/
 
 obj-$(CONFIG_USB_DWC3)		+= dwc3/
 obj-$(CONFIG_USB_DWC2)		+= dwc2/
drivers/usb/mux/Kconfig new file +12 -0
new file mode 100644
index 0000000..62e2cc3
@@ -0,0 +1,12 @@
+#
+# USB port mux driver configuration
+#
+menu "USB Port MUX drivers"
+config INTEL_USB_MUX
+	select EXTCON
+	def_bool n
+	help
+	  Common code for all Intel dual role port mux drivers. All Intel
+	  usb port mux drivers should select it.
+
+endmenu
drivers/usb/mux/Makefile new file +4 -0
new file mode 100644
index 0000000..84f0ae8
@@ -0,0 +1,4 @@
+#
+# Makefile for USB port mux drivers
+#
+obj-$(CONFIG_INTEL_USB_MUX)		+= intel-mux.o
drivers/usb/mux/intel-mux.c new file +56 -0
new file mode 100644
index 0000000..bb7b192
@@ -0,0 +1,180 @@
+/**
+ * intel_mux.c - USB Port Mux support
+ *
+ * Copyright (C) 2016 Intel Corporation
+ *
+ * Author: Lu Baolu <[email protected]>
+ *
+ * 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/notifier.h>
+#include <linux/extcon.h>
+#include <linux/err.h>
+
+struct intel_usb_mux {
+	struct device	*dev;
+	char		*cable_name;
+	int		(*cable_set_cb)(struct device *dev);
+	int		(*cable_unset_cb)(struct device *dev);
+
+	struct notifier_block		nb;
+	struct extcon_specific_cable_nb	obj;
+
+	/*
+	 * The state of the mux.
+	 * 0, 1 - mux switch state
+	 * -1   - uninitialized state
+	 */
+	int				mux_state;
+
+	 /* lock for mux_state */
+	struct mutex			mux_mutex;
+};
+
+static int usb_mux_change_state(struct intel_usb_mux *mux, int state)
+{
+	int ret;
+	struct device *dev = mux->dev;
+
+	dev_WARN_ONCE(dev,
+		      !mutex_is_locked(&mux->mux_mutex),
+		      "mutex is unlocked\n");
+
+	mux->mux_state = state;
+
+	if (mux->mux_state)
+		ret = mux->cable_set_cb(dev);
+	else
+		ret = mux->cable_unset_cb(dev);
+
+	return ret;
+}
+
+static int usb_mux_notifier(struct notifier_block *nb,

drivers/usb/mux/intel-mux.c line 66

Quoted 10 lines
drivers/usb/mux/intel-mux.c new file +10 -0
+			    unsigned long event, void *ptr)
+{
+	struct intel_usb_mux *mux;
+	int state;
+	int ret = NOTIFY_DONE;
+
+	mux = container_of(nb, struct intel_usb_mux, nb);
+
+	state = extcon_get_cable_state(mux->obj.edev,
+				       mux->cable_name);

Use the extcon_get_cable_stet_().

Quoted 62 lines
drivers/usb/mux/intel-mux.c new file +62 -0
+
+	if (mux->mux_state == -1 || mux->mux_state != state) {
+		mutex_lock(&mux->mux_mutex);
+		ret = usb_mux_change_state(mux, state);
+		mutex_unlock(&mux->mux_mutex);
+	}
+
+	return ret;
+}
+
+static ssize_t port_mux_show(struct device *dev,
+			     struct device_attribute *attr, char *buf)
+{
+	struct intel_usb_mux *mux = dev_get_drvdata(dev);
+
+	if (dev_WARN_ONCE(dev, !mux, "mux without data structure\n"))
+		return 0;
+
+	return sprintf(buf, "%s\n", mux->mux_state ? "host" : "peripheral");
+}
+
+static ssize_t port_mux_store(struct device *dev,
+			      struct device_attribute *attr,
+			      const char *buf, size_t count)
+{
+	struct intel_usb_mux *mux = dev_get_drvdata(dev);
+	int state;
+
+	if (dev_WARN_ONCE(dev, !mux, "mux without data structure\n"))
+		return -EINVAL;
+
+	if (sysfs_streq(buf, "peripheral"))
+		state = 0;
+	else if (sysfs_streq(buf, "host"))
+		state = 1;
+	else
+		return -EINVAL;
+
+	mutex_lock(&mux->mux_mutex);
+	usb_mux_change_state(mux, state);
+	mutex_unlock(&mux->mux_mutex);
+
+	return count;
+}
+static DEVICE_ATTR_RW(port_mux);
+
+int intel_usb_mux_bind_cable(struct device *dev,
+			     char *extcon_name,
+			     char *cable_name,
+			     int (*cable_set_cb)(struct device *dev),
+			     int (*cable_unset_cb)(struct device *dev))
+{
+	int ret;
+	struct intel_usb_mux *mux;
+
+	if (!cable_name)
+		return -ENODEV;
+
+	mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL);
+	if (!mux)
+		return -ENOMEM;
+

drivers/usb/mux/intel-mux.c line 138

Quoted 10 lines
drivers/usb/mux/intel-mux.c new file +10 -0
+	mux->dev = dev;
+	mux->cable_name = kstrdup(cable_name, GFP_KERNEL);
+	mux->cable_set_cb = cable_set_cb;
+	mux->cable_unset_cb = cable_unset_cb;
+	mux->nb.notifier_call = usb_mux_notifier;
+	mutex_init(&mux->mux_mutex);
+	mux->mux_state = -1;
+	dev_set_drvdata(dev, mux);
+	ret = extcon_register_interest(&mux->obj, extcon_name,
+				       cable_name, &mux->nb);

Use the extcon_register_notifier()

drivers/usb/mux/intel-mux.c line 150

Quoted 12 lines
drivers/usb/mux/intel-mux.c new file +12 -0
+	if (ret) {
+		kfree(mux->cable_name);
+		dev_err(dev, "failed to register extcon notifier\n");
+		return -ENODEV;
+	}
+
+	usb_mux_notifier(&mux->nb, 0, NULL);
+
+	/* register the sysfs interface */
+	ret = device_create_file(dev, &dev_attr_port_mux);
+	if (ret) {
+		extcon_unregister_interest(&mux->obj);

Use the extcon_unregister_notifier()

Quoted 5 lines
drivers/usb/mux/intel-mux.c new file +5 -0
+		kfree(mux->cable_name);
+		dev_err(dev, "failed to create sysfs attribute\n");
+		return -ENODEV;
+	}
+

drivers/usb/mux/intel-mux.c line 165

Quoted 10 lines
drivers/usb/mux/intel-mux.c new file +10 -0
+	return 0;
+}
+EXPORT_SYMBOL_GPL(intel_usb_mux_bind_cable);
+
+int intel_usb_mux_unbind_cable(struct device *dev)
+{
+	struct intel_usb_mux *mux = dev_get_drvdata(dev);
+
+	device_remove_file(dev, &dev_attr_port_mux);
+	extcon_unregister_interest(&mux->obj);

Use the extcon_unregister_notifier()

Quoted 55 lines
drivers/usb/mux/intel-mux.c new file +15 -0
+	kfree(mux->cable_name);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(intel_usb_mux_unbind_cable);
+
+#ifdef CONFIG_PM_SLEEP
+void intel_usb_mux_complete(struct device *dev)
+{
+	struct intel_usb_mux *mux = dev_get_drvdata(dev);
+
+	usb_mux_notifier(&mux->nb, 0, NULL);
+}
+EXPORT_SYMBOL_GPL(intel_usb_mux_complete);
+#endif
include/linux/usb/intel-mux.h new file +33 -0
new file mode 100644
index 0000000..fd5612d
@@ -0,0 +1,43 @@
+/**
+ * intel_mux.h - USB Port Mux definitions
+ *
+ * Copyright (C) 2016 Intel Corporation
+ *
+ * Author: Lu Baolu <[email protected]>
+ *
+ * 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.
+ */
+
+#ifndef __LINUX_USB_INTEL_MUX_H
+#define __LINUX_USB_INTEL_MUX_H
+
+#if IS_ENABLED(CONFIG_INTEL_USB_MUX)
+int intel_usb_mux_bind_cable(struct device *dev, char *extcon_name,
+			     char *cable_name,
+			     int (*cable_set_cb)(struct device *dev),
+			     int (*cable_unset_cb)(struct device *dev));
+int intel_usb_mux_unbind_cable(struct device *dev);
+#ifdef CONFIG_PM_SLEEP
+void intel_usb_mux_complete(struct device *dev);
+#endif
+
+#else
+static inline int
+intel_usb_mux_bind_cable(struct device *dev,
+			 char *extcon_name,
+			 char *cable_name,
+			 int (*cable_set_cb)(struct device *dev),
+			 int (*cable_unset_cb)(struct device *dev))
+{

include/linux/usb/intel-mux.h line 43

Quoted 10 lines
include/linux/usb/intel-mux.h new file +10 -0
+	return -ENODEV;
+}
+
+static inline int intel_usb_mux_unbind_cable(struct device *dev)
+{
+	return 0;
+}
+#endif /* CONFIG_INTEL_USB_MUX */
+
+#endif /* __LINUX_USB_INTEL_MUX_H */

Best Regards,
Chanwoo Choi

author
#7 lore raw

Re: [PATCH v4 3/7] usb: mux: add common code for Intel dual role port mux

Quoted 29 lines · Chanwoo Choi

Hi Lu,

Quoted 15 lines · Lu Baolu

Hi Chanwoo,

Quoted 6 lines · Chanwoo Choi

Hi Lu,

To handle extcon (external connector), I implemented the unique id
for each external connector on patch[1] instead of using the ambiguous string type.
[1] 2a9de9c0f08d6 (extcon: Use the unique id for external connector instead of string)

Quoted 6 lines

So I recommend that you should use the unique id (ex. EXTCON_USB, EXTCON_USB_HOST) with extcon_register_notifier(), extcon_get_cable_state_() and extcon_set_cable_state_().

extcon_register_interest() is deprecated-> extcon_register_notifier() extcon_get_cable_state() is deprecated -> extcon_get_cable_state_() extcon_set_cable_state() is deprecated -> extcon_set_cable_state_()

Quoted 8 lines

Sure. I will use the new interfaces with a refreshed patch serial later.

Quoted 3 lines

You can refer to usage for new function with unique id on patch[2]
[2] 5960387a2fb83 (usb: dwc3: omap: Replace deprecated API of extcon)

Thanks. That's helpful.

By the way, is extcon_get_extcon_dev() still available?

Yes.
This function find the extcon device with string name.

Quoted 6 lines

If extcon client driver use the platform_data to get the name of extcon device, the extcon client driver should use the extcon_get_extcon_dev() funtcion. Because we can not pass the point(instance) of extcon device through platform_data.

But, if extcon client driver support the devicetree, we better to use the extcon_get_edev_by_phandle().

That's clear. Thank you!

Best regards,
Baolu

Quoted 387 lines

Best Regards,
Chanwoo Choi

Quoted 382 lines
Quoted 1 line

I'm sorry for late reply. I add the some comment on below.

Never mind. Thank you for reminding me.

Best regards,
Baolu

Quoted 374 lines · Lu Baolu
Quoted 187 lines

Several Intel PCHs and SOCs have an internal mux that is used to share one USB port between device controller and host controller.

A usb port mux could be abstracted as the following elements:
1) mux state: HOST or PERIPHERAL;
2) an extcon cable which triggers the change of mux state between HOST and PERIPHERAL;
3) The required action to do the real port switch.

This patch adds the common code to handle usb port mux. With this common code, the individual mux driver, which always is platform dependent, could focus on the real operation of mux switch.

---
 Documentation/ABI/testing/sysfs-bus-platform |  15 +++
 MAINTAINERS                                  |   7 ++
 drivers/usb/Kconfig                          |   2 +
 drivers/usb/Makefile                         |   1 +
 drivers/usb/mux/Kconfig                      |  12 ++
 drivers/usb/mux/Makefile                     |   4 +
 drivers/usb/mux/intel-mux.c                  | 180 +++++++++++++++++++++++++++
 include/linux/usb/intel-mux.h                |  43 +++++++
 8 files changed, 264 insertions(+)
 create mode 100644 drivers/usb/mux/Kconfig
 create mode 100644 drivers/usb/mux/Makefile
 create mode 100644 drivers/usb/mux/intel-mux.c
 create mode 100644 include/linux/usb/intel-mux.h
Documentation/ABI/testing/sysfs-bus-platform +15 -0
index 5172a61..23bf76e 100644
@@ -18,3 +18,18 @@ Description:
 		devices to opt-out of driver binding using a driver_override
 		name such as "none".  Only a single driver may be specified in
 		the override, there is no support for parsing delimiters.
+
+What:		/sys/bus/platform/devices/.../port_mux
+Date:		Febuary 2016
+Contact:	Lu Baolu <[email protected]>
+Description:
+		In some platforms, a single USB port is shared between a USB host
+		controller and a device controller. A USB mux driver is needed to
+		handle the port mux. port_mux attribute shows and stores the mux
+		state.
+		For read:
+		'peripheral' - mux switched to PERIPHERAL controller;
+		'host'       - mux switched to HOST controller.
+		For write:
+		'peripheral' - mux will be switched to PERIPHERAL controller;
+		'host'       - mux will be switched to HOST controller.
MAINTAINERS +7 -0
index da3e4d8..0dbee11 100644
@@ -11399,6 +11399,13 @@ T:	git git://git.kernel.org/pub/scm/linux/kernel/git/balbi/usb.git
 S:	Maintained
 F:	drivers/usb/phy/
 
+USB PORT MUX DRIVER
+M:	Lu Baolu <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	include/linux/usb/intel-mux.h
+F:	drivers/usb/mux/intel-mux.c
+
 USB PRINTER DRIVER (usblp)
 M:	Pete Zaitcev <[email protected]>
 L:	[email protected]
drivers/usb/Kconfig +2 -0
index 8ed451d..dbd6620 100644
@@ -149,6 +149,8 @@ endif # USB
 
 source "drivers/usb/phy/Kconfig"
 
+source "drivers/usb/mux/Kconfig"
+
 source "drivers/usb/gadget/Kconfig"
 
 config USB_LED_TRIG
drivers/usb/Makefile +1 -0
index d5c57f1..6433f0c 100644
@@ -6,6 +6,7 @@
 
 obj-$(CONFIG_USB)		+= core/
 obj-$(CONFIG_USB_SUPPORT)	+= phy/
+obj-$(CONFIG_USB_SUPPORT)	+= mux/
 
 obj-$(CONFIG_USB_DWC3)		+= dwc3/
 obj-$(CONFIG_USB_DWC2)		+= dwc2/
drivers/usb/mux/Kconfig new file +12 -0
new file mode 100644
index 0000000..62e2cc3
@@ -0,0 +1,12 @@
+#
+# USB port mux driver configuration
+#
+menu "USB Port MUX drivers"
+config INTEL_USB_MUX
+	select EXTCON
+	def_bool n
+	help
+	  Common code for all Intel dual role port mux drivers. All Intel
+	  usb port mux drivers should select it.
+
+endmenu
drivers/usb/mux/Makefile new file +4 -0
new file mode 100644
index 0000000..84f0ae8
@@ -0,0 +1,4 @@
+#
+# Makefile for USB port mux drivers
+#
+obj-$(CONFIG_INTEL_USB_MUX)		+= intel-mux.o
drivers/usb/mux/intel-mux.c new file +56 -0
new file mode 100644
index 0000000..bb7b192
@@ -0,0 +1,180 @@
+/**
+ * intel_mux.c - USB Port Mux support
+ *
+ * Copyright (C) 2016 Intel Corporation
+ *
+ * Author: Lu Baolu <[email protected]>
+ *
+ * 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/notifier.h>
+#include <linux/extcon.h>
+#include <linux/err.h>
+
+struct intel_usb_mux {
+	struct device	*dev;
+	char		*cable_name;
+	int		(*cable_set_cb)(struct device *dev);
+	int		(*cable_unset_cb)(struct device *dev);
+
+	struct notifier_block		nb;
+	struct extcon_specific_cable_nb	obj;
+
+	/*
+	 * The state of the mux.
+	 * 0, 1 - mux switch state
+	 * -1   - uninitialized state
+	 */
+	int				mux_state;
+
+	 /* lock for mux_state */
+	struct mutex			mux_mutex;
+};
+
+static int usb_mux_change_state(struct intel_usb_mux *mux, int state)
+{
+	int ret;
+	struct device *dev = mux->dev;
+
+	dev_WARN_ONCE(dev,
+		      !mutex_is_locked(&mux->mux_mutex),
+		      "mutex is unlocked\n");
+
+	mux->mux_state = state;
+
+	if (mux->mux_state)
+		ret = mux->cable_set_cb(dev);
+	else
+		ret = mux->cable_unset_cb(dev);
+
+	return ret;
+}
+
+static int usb_mux_notifier(struct notifier_block *nb,

drivers/usb/mux/intel-mux.c line 66

Quoted 10 lines
drivers/usb/mux/intel-mux.c new file +10 -0
+			    unsigned long event, void *ptr)
+{
+	struct intel_usb_mux *mux;
+	int state;
+	int ret = NOTIFY_DONE;
+
+	mux = container_of(nb, struct intel_usb_mux, nb);
+
+	state = extcon_get_cable_state(mux->obj.edev,
+				       mux->cable_name);

Use the extcon_get_cable_stet_().

Quoted 62 lines
drivers/usb/mux/intel-mux.c new file +62 -0
+
+	if (mux->mux_state == -1 || mux->mux_state != state) {
+		mutex_lock(&mux->mux_mutex);
+		ret = usb_mux_change_state(mux, state);
+		mutex_unlock(&mux->mux_mutex);
+	}
+
+	return ret;
+}
+
+static ssize_t port_mux_show(struct device *dev,
+			     struct device_attribute *attr, char *buf)
+{
+	struct intel_usb_mux *mux = dev_get_drvdata(dev);
+
+	if (dev_WARN_ONCE(dev, !mux, "mux without data structure\n"))
+		return 0;
+
+	return sprintf(buf, "%s\n", mux->mux_state ? "host" : "peripheral");
+}
+
+static ssize_t port_mux_store(struct device *dev,
+			      struct device_attribute *attr,
+			      const char *buf, size_t count)
+{
+	struct intel_usb_mux *mux = dev_get_drvdata(dev);
+	int state;
+
+	if (dev_WARN_ONCE(dev, !mux, "mux without data structure\n"))
+		return -EINVAL;
+
+	if (sysfs_streq(buf, "peripheral"))
+		state = 0;
+	else if (sysfs_streq(buf, "host"))
+		state = 1;
+	else
+		return -EINVAL;
+
+	mutex_lock(&mux->mux_mutex);
+	usb_mux_change_state(mux, state);
+	mutex_unlock(&mux->mux_mutex);
+
+	return count;
+}
+static DEVICE_ATTR_RW(port_mux);
+
+int intel_usb_mux_bind_cable(struct device *dev,
+			     char *extcon_name,
+			     char *cable_name,
+			     int (*cable_set_cb)(struct device *dev),
+			     int (*cable_unset_cb)(struct device *dev))
+{
+	int ret;
+	struct intel_usb_mux *mux;
+
+	if (!cable_name)
+		return -ENODEV;
+
+	mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL);
+	if (!mux)
+		return -ENOMEM;
+

drivers/usb/mux/intel-mux.c line 138

Quoted 10 lines
drivers/usb/mux/intel-mux.c new file +10 -0
+	mux->dev = dev;
+	mux->cable_name = kstrdup(cable_name, GFP_KERNEL);
+	mux->cable_set_cb = cable_set_cb;
+	mux->cable_unset_cb = cable_unset_cb;
+	mux->nb.notifier_call = usb_mux_notifier;
+	mutex_init(&mux->mux_mutex);
+	mux->mux_state = -1;
+	dev_set_drvdata(dev, mux);
+	ret = extcon_register_interest(&mux->obj, extcon_name,
+				       cable_name, &mux->nb);

Use the extcon_register_notifier()

drivers/usb/mux/intel-mux.c line 150

Quoted 12 lines
drivers/usb/mux/intel-mux.c new file +12 -0
+	if (ret) {
+		kfree(mux->cable_name);
+		dev_err(dev, "failed to register extcon notifier\n");
+		return -ENODEV;
+	}
+
+	usb_mux_notifier(&mux->nb, 0, NULL);
+
+	/* register the sysfs interface */
+	ret = device_create_file(dev, &dev_attr_port_mux);
+	if (ret) {
+		extcon_unregister_interest(&mux->obj);

Use the extcon_unregister_notifier()

Quoted 5 lines
drivers/usb/mux/intel-mux.c new file +5 -0
+		kfree(mux->cable_name);
+		dev_err(dev, "failed to create sysfs attribute\n");
+		return -ENODEV;
+	}
+

drivers/usb/mux/intel-mux.c line 165

Quoted 10 lines
drivers/usb/mux/intel-mux.c new file +10 -0
+	return 0;
+}
+EXPORT_SYMBOL_GPL(intel_usb_mux_bind_cable);
+
+int intel_usb_mux_unbind_cable(struct device *dev)
+{
+	struct intel_usb_mux *mux = dev_get_drvdata(dev);
+
+	device_remove_file(dev, &dev_attr_port_mux);
+	extcon_unregister_interest(&mux->obj);

Use the extcon_unregister_notifier()

Quoted 55 lines
drivers/usb/mux/intel-mux.c new file +15 -0
+	kfree(mux->cable_name);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(intel_usb_mux_unbind_cable);
+
+#ifdef CONFIG_PM_SLEEP
+void intel_usb_mux_complete(struct device *dev)
+{
+	struct intel_usb_mux *mux = dev_get_drvdata(dev);
+
+	usb_mux_notifier(&mux->nb, 0, NULL);
+}
+EXPORT_SYMBOL_GPL(intel_usb_mux_complete);
+#endif
include/linux/usb/intel-mux.h new file +33 -0
new file mode 100644
index 0000000..fd5612d
@@ -0,0 +1,43 @@
+/**
+ * intel_mux.h - USB Port Mux definitions
+ *
+ * Copyright (C) 2016 Intel Corporation
+ *
+ * Author: Lu Baolu <[email protected]>
+ *
+ * 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.
+ */
+
+#ifndef __LINUX_USB_INTEL_MUX_H
+#define __LINUX_USB_INTEL_MUX_H
+
+#if IS_ENABLED(CONFIG_INTEL_USB_MUX)
+int intel_usb_mux_bind_cable(struct device *dev, char *extcon_name,
+			     char *cable_name,
+			     int (*cable_set_cb)(struct device *dev),
+			     int (*cable_unset_cb)(struct device *dev));
+int intel_usb_mux_unbind_cable(struct device *dev);
+#ifdef CONFIG_PM_SLEEP
+void intel_usb_mux_complete(struct device *dev);
+#endif
+
+#else
+static inline int
+intel_usb_mux_bind_cable(struct device *dev,
+			 char *extcon_name,
+			 char *cable_name,
+			 int (*cable_set_cb)(struct device *dev),
+			 int (*cable_unset_cb)(struct device *dev))
+{

include/linux/usb/intel-mux.h line 43

Quoted 10 lines
include/linux/usb/intel-mux.h new file +10 -0
+	return -ENODEV;
+}
+
+static inline int intel_usb_mux_unbind_cable(struct device *dev)
+{
+	return 0;
+}
+#endif /* CONFIG_INTEL_USB_MUX */
+
+#endif /* __LINUX_USB_INTEL_MUX_H */

Best Regards,
Chanwoo Choi

author
#8 lore raw

PATCH v4 4/7 usb: mux: add driver for Intel gpio controlled port mux

In reply to Lu Baolu

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.

---
 MAINTAINERS                      |   1 +
 drivers/usb/mux/Kconfig          |   9 +++
 drivers/usb/mux/Makefile         |   1 +
 drivers/usb/mux/intel-mux-gpio.c | 120 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 131 insertions(+)
 create mode 100644 drivers/usb/mux/intel-mux-gpio.c
MAINTAINERS +1 -0
index 0dbee11..99bc198 100644
@@ -11405,6 +11405,7 @@ L:	[email protected]
 S:	Supported
 F:	include/linux/usb/intel-mux.h
 F:	drivers/usb/mux/intel-mux.c
+F:	drivers/usb/mux/intel-mux-gpio.c
 
 USB PRINTER DRIVER (usblp)
 M:	Pete Zaitcev <[email protected]>
drivers/usb/mux/Kconfig +9 -0
index 62e2cc3..2b197c1 100644
@@ -9,4 +9,13 @@ config INTEL_USB_MUX
 	  Common code for all Intel dual role port mux drivers. All Intel
 	  usb port mux drivers should select it.
 
+config INTEL_MUX_GPIO
+	tristate "Intel dual role port mux controlled by GPIOs"
+	depends on GPIOLIB
+	depends on X86 && ACPI
+	select INTEL_USB_MUX
+	help
+	  Say Y here to enable support for Intel dual role port mux
+	  controlled by GPIOs.
+
 endmenu
drivers/usb/mux/Makefile +1 -0
index 84f0ae8..66f765c 100644
@@ -2,3 +2,4 @@
 # Makefile for USB port mux drivers
 #
 obj-$(CONFIG_INTEL_USB_MUX)		+= intel-mux.o
+obj-$(CONFIG_INTEL_MUX_GPIO)		+= intel-mux-gpio.o
drivers/usb/mux/intel-mux-gpio.c new file +120 -0

120 lines not on this page · raw message

Signature · 1 line
2.1.4
author
#9 lore raw

PATCH v4 5/7 usb: mux: add driver for Intel drcfg controlled port mux

In reply to Lu Baolu

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.

---
 MAINTAINERS                       |   1 +
 drivers/usb/mux/Kconfig           |   7 ++
 drivers/usb/mux/Makefile          |   1 +
 drivers/usb/mux/intel-mux-drcfg.c | 157 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 166 insertions(+)
 create mode 100644 drivers/usb/mux/intel-mux-drcfg.c
MAINTAINERS +1 -0
index 99bc198..1d0f090 100644
@@ -11406,6 +11406,7 @@ S:	Supported
 F:	include/linux/usb/intel-mux.h
 F:	drivers/usb/mux/intel-mux.c
 F:	drivers/usb/mux/intel-mux-gpio.c
+F:	drivers/usb/mux/intel-mux-drcfg.c
 
 USB PRINTER DRIVER (usblp)
 M:	Pete Zaitcev <[email protected]>
drivers/usb/mux/Kconfig +7 -0
index 2b197c1..975c5a0 100644
@@ -18,4 +18,11 @@ config INTEL_MUX_GPIO
 	  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
+	select INTEL_USB_MUX
+	help
+	  Say Y here to enable support for Intel dual role port mux
+	  controlled by the Dual Role Configuration Register.
 endmenu
drivers/usb/mux/Makefile +1 -0
index 66f765c..3179909 100644
@@ -3,3 +3,4 @@
 #
 obj-$(CONFIG_INTEL_USB_MUX)		+= intel-mux.o
 obj-$(CONFIG_INTEL_MUX_GPIO)		+= intel-mux-gpio.o
+obj-$(CONFIG_INTEL_MUX_DRCFG)		+= intel-mux-drcfg.o
drivers/usb/mux/intel-mux-drcfg.c new file +157 -0

157 lines not on this page · raw message

Signature · 1 line
2.1.4
author
#10 lore raw

PATCH v4 6/7 usb: pci-quirks: add Intel USB drcfg mux device

In reply to Lu Baolu

In some Intel platforms, a single usb port is shared between USB host and device controllers. The shared port is under control of a switch which is defined in the Intel vendor defined extended capability for xHCI.

This patch adds the support to detect and create the platform device for the port mux switch.

---
 drivers/usb/host/pci-quirks.c    | 47 ++++++++++++++++++++++++++++++++++++++--
 drivers/usb/host/xhci-ext-caps.h |  2 ++
 2 files changed, 47 insertions(+), 2 deletions(-)
drivers/usb/host/pci-quirks.c +45 -2
index 26cb8c8..d2770c8 100644
@@ -16,10 +16,11 @@
 #include <linux/export.h>
 #include <linux/acpi.h>
 #include <linux/dmi.h>
+#include <linux/platform_device.h>
+
 #include "pci-quirks.h"
 #include "xhci-ext-caps.h"
 
-
 #define UHCI_USBLEGSUP		0xc0		/* legacy support */
 #define UHCI_USBCMD		0		/* command register */
 #define UHCI_USBINTR		4		/* interrupt register */
@@ -78,6 +79,9 @@
 #define USB_INTEL_USB3_PSSEN   0xD8
 #define USB_INTEL_USB3PRM      0xDC
 
+#define DEVICE_ID_INTEL_CHERRYVIEW_XHCI		0x22b5
+#define DEVICE_ID_INTEL_BROXTON_M_XHCI		0x0aa8
+
 /*
  * amd_chipset_gen values represent AMD different chipset generations
  */
@@ -956,6 +960,41 @@ void usb_disable_xhci_ports(struct pci_dev *xhci_pdev)
 }
 EXPORT_SYMBOL_GPL(usb_disable_xhci_ports);
 
+static void create_intel_usb_mux_device(struct pci_dev *xhci_pdev,
+					void __iomem *base)
+{
+	struct platform_device *plat_dev;
+	struct property_set pset;
+	int ret;
+
+	struct property_entry pentry[] = {
+		PROPERTY_ENTRY_U64("reg-start",
+				   pci_resource_start(xhci_pdev, 0) + 0x80d8),
+		PROPERTY_ENTRY_U64("reg-size", 8),
+		{ },
+	};
+
+	if (!xhci_find_next_ext_cap(base, 0, XHCI_EXT_CAPS_INTEL_USB_MUX))
+		return;
+
+	plat_dev = platform_device_alloc("intel-mux-drcfg",
+					 PLATFORM_DEVID_AUTO);
+	if (!plat_dev)
+		return;
+
+	plat_dev->dev.parent = &xhci_pdev->dev;
+	pset.properties = pentry;
+	platform_device_add_properties(plat_dev, &pset);
+
+	ret = platform_device_add(plat_dev);
+	if (ret) {
+		dev_warn(&xhci_pdev->dev,
+			 "failed to create mux device with error %d",
+				ret);
+		platform_device_put(plat_dev);
+	}
+}
+
 /**
  * PCI Quirks for xHCI.
  *
@@ -1022,8 +1061,12 @@ static void quirk_usb_handoff_xhci(struct pci_dev *pdev)
 	writel(val, base + ext_cap_offset + XHCI_LEGACY_CONTROL_OFFSET);
 
 hc_init:
-	if (pdev->vendor == PCI_VENDOR_ID_INTEL)
+	if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
 		usb_enable_intel_xhci_ports(pdev);
+		if (pdev->device == DEVICE_ID_INTEL_CHERRYVIEW_XHCI ||
+		    pdev->device == DEVICE_ID_INTEL_BROXTON_M_XHCI)
+			create_intel_usb_mux_device(pdev, base);
+	}
 
 	op_reg_base = base + XHCI_HC_LENGTH(readl(base));
 
drivers/usb/host/xhci-ext-caps.h +2 -0
index e0244fb..e368ccb 100644
@@ -51,6 +51,8 @@
 #define XHCI_EXT_CAPS_ROUTE	5
 /* IDs 6-9 reserved */
 #define XHCI_EXT_CAPS_DEBUG	10
+/* Vendor defined 192-255 */
+#define XHCI_EXT_CAPS_INTEL_USB_MUX	192
 /* USB Legacy Support Capability - section 7.1.1 */
 #define XHCI_HC_BIOS_OWNED	(1 << 16)
 #define XHCI_HC_OS_OWNED	(1 << 24)
Signature · 1 line
2.1.4
author
#11 lore raw

PATCH v4 7/7 mfd: intel_vuport: Add Intel virtual USB port MFD Driver

In reply to Lu Baolu

Some Intel platforms have an USB port mux controlled by GPIOs. There's a single ACPI platform device that provides both USB ID extcon device and a USB port mux device. This MFD driver will split the 2 devices for their respective drivers.

---
 MAINTAINERS                |  6 ++++
 drivers/mfd/Kconfig        |  8 +++++
 drivers/mfd/Makefile       |  1 +
 drivers/mfd/intel-vuport.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 89 insertions(+)
 create mode 100644 drivers/mfd/intel-vuport.c
MAINTAINERS +6 -0
index 1d0f090..0a932b3 100644
@@ -5599,6 +5599,12 @@ L:	[email protected]
 S:	Supported
 F:	drivers/cpufreq/intel_pstate.c
 
+INTEL VIRTUAL USB PORT DRIVER
+M:	Lu Baolu <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	drivers/mfd/intel-vuport.c
+
 INTEL FRAMEBUFFER DRIVER (excluding 810 and 815)
 M:	Maik Broemme <[email protected]>
 L:	[email protected]
drivers/mfd/Kconfig +8 -0
index 9ca66de..48933d4 100644
@@ -1534,5 +1534,13 @@ config MFD_VEXPRESS_SYSREG
 	  System Registers are the platform configuration block
 	  on the ARM Ltd. Versatile Express board.
 
+config MFD_INTEL_VUPORT
+	tristate "Intel virtual USB port controller"
+	select MFD_CORE
+	depends on X86 && ACPI
+	help
+	  Say Y here to enable support for Intel's dual role port mux
+	  controlled by GPIOs.
+
 endmenu
 endif
drivers/mfd/Makefile +1 -0
index 0f230a6..0ccd107 100644
@@ -198,3 +198,4 @@ intel-soc-pmic-objs		:= intel_soc_pmic_core.o intel_soc_pmic_crc.o
 intel-soc-pmic-$(CONFIG_INTEL_PMC_IPC)	+= intel_soc_pmic_bxtwc.o
 obj-$(CONFIG_INTEL_SOC_PMIC)	+= intel-soc-pmic.o
 obj-$(CONFIG_MFD_MT6397)	+= mt6397-core.o
+obj-$(CONFIG_MFD_INTEL_VUPORT)	+= intel-vuport.o
drivers/mfd/intel-vuport.c new file +74 -0

74 lines not on this page · raw message

Signature · 1 line
2.1.4