Skip to contentSkip to conversation

Introduce usb charger framework to deal with the usb gadget power negotation

PATCH Superseded Baolin Wang 2 participants 16 messages Apr 7, 2016 – Apr 18, 2016

Subsystems (3)

v10 of 19 Superseded Latest is v19 →

lore
Share & tools
lore
#0 lore raw

v10 cover Introduce usb charger framework to deal with the usb gadget power negotation

Currently the Linux kernel does not provide any standard integration of this feature that integrates the USB subsystem with the system power regulation provided by PMICs meaning that either vendors must add this in their kernels or USB gadget devices based on Linux (such as mobile phones) may not behave as they should. Thus provide a standard framework for doing this in kernel.

Now introduce one user with wm831x_power to support and test the usb charger, which is pending testing. Moreover there may be other potential users will use it in future.

Changes since v9:
- Remove some redundant sysfs attributes.
- Change the SDP charger default current if gadget is SS.
- Remove the 'get_charger_type' callback in gadget->ops.

Baolin Wang (4):
gadget: Introduce the usb charger framework
gadget: Support for the usb charger framework
gadget: Integrate with the usb gadget supporting for usb charger
power: wm831x_power: Support USB charger current limit management

 drivers/power/wm831x_power.c      |   69 ++++
 drivers/usb/gadget/Kconfig        |    7 +
 drivers/usb/gadget/udc/Makefile   |    1 +
 drivers/usb/gadget/udc/charger.c  |  766 +++++++++++++++++++++++++++++++++++++
 drivers/usb/gadget/udc/udc-core.c |   11 +
 include/linux/mfd/wm831x/pdata.h  |    3 +
 include/linux/usb/charger.h       |  173 +++++++++
 include/linux/usb/gadget.h        |   13 +
 include/uapi/linux/usb/charger.h  |   31 ++
 9 files changed, 1074 insertions(+)
 create mode 100644 drivers/usb/gadget/udc/charger.c
 create mode 100644 include/linux/usb/charger.h
 create mode 100644 include/uapi/linux/usb/charger.h
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH v10 1/4 gadget: Introduce the usb charger framework

This patch introduces the usb charger driver based on usb gadget that makes an enhancement to a power driver. It works well in practice but that requires a system with suitable hardware.

The basic conception of the usb charger is that, when one usb charger is added or removed by reporting from the usb gadget state change or the extcon device state change, the usb charger will report to power user to set the current limitation.

The usb charger will register notifiees on the usb gadget or the extcon device to get notified the usb charger state. It also supplies the notification mechanism to userspace When the usb charger state is changed.

Power user will register a notifiee on the usb charger to get notified by status changes from the usb charger. It will report to power user to set the current limitation when detecting the usb charger is added or removed from extcon device state or usb gadget state.

This patch doesn't yet integrate with the gadget code, so some functions which rely on the 'gadget' are not completed, that will be implemented in the following patches.

---
 drivers/usb/gadget/Kconfig       |    7 +
 drivers/usb/gadget/udc/Makefile  |    1 +
 drivers/usb/gadget/udc/charger.c |  729 ++++++++++++++++++++++++++++++++++++++
 include/linux/usb/charger.h      |  173 +++++++++
 include/uapi/linux/usb/charger.h |   31 ++
 5 files changed, 941 insertions(+)
 create mode 100644 drivers/usb/gadget/udc/charger.c
 create mode 100644 include/linux/usb/charger.h
 create mode 100644 include/uapi/linux/usb/charger.h
drivers/usb/gadget/Kconfig +7 -0
index af5d922..82a5b3c 100644
@@ -133,6 +133,13 @@ config U_SERIAL_CONSOLE
 	help
 	   It supports the serial gadget can be used as a console.
 
+config USB_CHARGER
+	bool "USB charger support"
+	help
+	  The usb charger driver based on the usb gadget that makes an
+	  enhancement to a power driver which can set the current limitation
+	  when the usb charger is added or removed.
+
 source "drivers/usb/gadget/udc/Kconfig"
 
 #
drivers/usb/gadget/udc/Makefile +1 -0
index dfee534..0e9564c 100644
@@ -2,6 +2,7 @@
 # USB peripheral controller drivers
 #
 obj-$(CONFIG_USB_GADGET)	+= udc-core.o
+obj-$(CONFIG_USB_CHARGER)	+= charger.o
 obj-$(CONFIG_USB_DUMMY_HCD)	+= dummy_hcd.o
 obj-$(CONFIG_USB_NET2272)	+= net2272.o
 obj-$(CONFIG_USB_NET2280)	+= net2280.o
drivers/usb/gadget/udc/charger.c new file +729 -0

729 lines not on this page · raw message

include/linux/usb/charger.h new file +173 -0

173 lines not on this page · raw message

include/uapi/linux/usb/charger.h new file +31 -0
new file mode 100644
index 0000000..3c56ec4
@@ -0,0 +1,31 @@
+/*
+ * This file defines the USB charger type and state that are needed for
+ * USB device APIs.
+ */
+
+#ifndef _UAPI__LINUX_USB_CHARGER_H
+#define _UAPI__LINUX_USB_CHARGER_H
+
+/*
+ * USB charger type:
+ * SDP (Standard Downstream Port)
+ * DCP (Dedicated Charging Port)
+ * CDP (Charging Downstream Port)
+ * ACA (Accessory Charger Adapters)
+ */
+enum usb_charger_type {
+	UNKNOWN_TYPE,
+	SDP_TYPE,
+	DCP_TYPE,
+	CDP_TYPE,
+	ACA_TYPE,
+};
+
+/* USB charger state */
+enum usb_charger_state {
+	USB_CHARGER_DEFAULT,
+	USB_CHARGER_PRESENT,
+	USB_CHARGER_REMOVE,
+};
+
+#endif /* _UAPI__LINUX_USB_CHARGER_H */
Signature · 6 lines
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw

RE: [PATCH v10 1/4] gadget: Introduce the usb charger framework

Hi, Baolin

Quoted 50 lines

-----Original Message-----
From: [email protected] [mailto:linux-usb- [email protected]] On Behalf Of Baolin Wang
Sent: Thursday, April 07, 2016 7:49 PM
To: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]; linux- [email protected]
Subject: [PATCH v10 1/4] gadget: Introduce the usb charger framework

This patch introduces the usb charger driver based on usb gadget that makes an enhancement to a power driver. It works well in practice but that requires a system with suitable hardware.

The basic conception of the usb charger is that, when one usb charger is added or removed by reporting from the usb gadget state change or the extcon device state change, the usb charger will report to power user to set the current limitation.

The usb charger will register notifiees on the usb gadget or the extcon device to get notified the usb charger state. It also supplies the notification mechanism to userspace When the usb charger state is changed.

Power user will register a notifiee on the usb charger to get notified by status changes from the usb charger. It will report to power user to set the current limitation when detecting the usb charger is added or removed from extcon device state or usb gadget state.

This patch doesn't yet integrate with the gadget code, so some functions which rely on the 'gadget' are not completed, that will be implemented in the following patches.

---
 drivers/usb/gadget/Kconfig       |    7 +
 drivers/usb/gadget/udc/Makefile  |    1 +
 drivers/usb/gadget/udc/charger.c |  729
++++++++++++++++++++++++++++++++++++++
 include/linux/usb/charger.h      |  173 +++++++++
 include/uapi/linux/usb/charger.h |   31 ++
 5 files changed, 941 insertions(+)
 create mode 100644 drivers/usb/gadget/udc/charger.c  create mode 100644

include/linux/usb/charger.h create mode 100644
include/uapi/linux/usb/charger.h

...

Quoted 18 lines
drivers/usb/gadget/udc/charger.c new file +18 -0
+
+/*
+ * usb_charger_detect_type() - detect the charger type manually.
+ * @uchger - usb charger device
+ *
+ * Note: You should ensure you need to detect the charger type manually
+on your
+ * platform.
+ * You should call it at the right gadget state to avoid affecting
+gadget
+ * enumeration.
+ */
+int usb_charger_detect_type(struct usb_charger *uchger) {
+	enum usb_charger_type type;
+
+	if (WARN(!uchger->charger_detect,
+		 "charger detection method should not be NULL"))
+		return -EINVAL;

drivers/usb/gadget/udc/charger.c line 217

Quoted 10 lines
drivers/usb/gadget/udc/charger.c new file +10 -0
+
+	type = uchger->charger_detect(uchger);
+
+	mutex_lock(&uchger->lock);
+	uchger->type = type;
+	mutex_unlock(&uchger->lock);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(usb_charger_detect_type);

I still recommend have a central place to call usb_charger_detect_type() to cover real charger detection instead of leaving this question open to diff users. This can be done after vbus-on is detected and before do gadget connect. I don't think this will make your framework complicated.

Hi Felipe, what do you think of this?

Li Jun

Quoted 50 lines
drivers/usb/gadget/udc/charger.c new file +50 -0
+
+/*
+ * usb_charger_get_type_by_others() - Get the usb charger type by the
+callback
+ * which is implemented by users.
+ * @uchger - the usb charger device.
+ *
+ * Note: This function is just used for getting the charger type, not
+for
+ * detecting charger type which might affect the DP/DM line when gadget
+is on
+ * enumeration state.
+ */
+static enum usb_charger_type
+usb_charger_get_type_by_others(struct usb_charger *uchger) {
+	if (uchger->type != UNKNOWN_TYPE)
+		return uchger->type;
+
+	if (uchger->psy) {
+		union power_supply_propval val;
+
+		power_supply_get_property(uchger->psy,
+					  POWER_SUPPLY_PROP_CHARGE_TYPE,
+					  &val);
+		switch (val.intval) {
+		case POWER_SUPPLY_TYPE_USB:
+			uchger->type = SDP_TYPE;
+			break;
+		case POWER_SUPPLY_TYPE_USB_DCP:
+			uchger->type = DCP_TYPE;
+			break;
+		case POWER_SUPPLY_TYPE_USB_CDP:
+			uchger->type = CDP_TYPE;
+			break;
+		case POWER_SUPPLY_TYPE_USB_ACA:
+			uchger->type = ACA_TYPE;
+			break;
+		default:
+			uchger->type = UNKNOWN_TYPE;
+			break;
+		}
+	} else if (uchger->get_charger_type) {
+		uchger->type = uchger->get_charger_type(uchger);
+	} else {
+		uchger->type = UNKNOWN_TYPE;
+	}
+
+	return uchger->type;
+}
+
#3 lore raw

Re: [PATCH v10 1/4] gadget: Introduce the usb charger framework

Hi Jun,

Quoted 26 lines · Jun Li

Hi, Baolin

Quoted 24 lines
drivers/usb/gadget/udc/charger.c new file +24 -0
+/*
+ * usb_charger_detect_type() - detect the charger type manually.
+ * @uchger - usb charger device
+ *
+ * Note: You should ensure you need to detect the charger type manually
+on your
+ * platform.
+ * You should call it at the right gadget state to avoid affecting
+gadget
+ * enumeration.
+ */
+int usb_charger_detect_type(struct usb_charger *uchger) {
+     enum usb_charger_type type;
+
+     if (WARN(!uchger->charger_detect,
+              "charger detection method should not be NULL"))
+             return -EINVAL;
+
+     type = uchger->charger_detect(uchger);
+
+     mutex_lock(&uchger->lock);
+     uchger->type = type;
+     mutex_unlock(&uchger->lock);
+
Quoted 8 lines

drivers/usb/gadget/udc/charger.c line 217

Quoted 3 lines
drivers/usb/gadget/udc/charger.c new file +3 -0
+     return 0;
+}
+EXPORT_SYMBOL_GPL(usb_charger_detect_type);

I still recommend have a central place to call usb_charger_detect_type() to cover real charger detection instead of leaving this question open to diff users. This can be done after vbus-on is detected and before do gadget connect. I don't think this will make your framework complicated.

This API is used for detecting the charger type manually (software charger detection), so if user's udc driver is needed to do this, then they can issue it at their right place to make it more flexible. But let us see other people's suggestion. Thanks.

Quoted 55 lines

Hi Felipe, what do you think of this?

Li Jun

Quoted 50 lines
drivers/usb/gadget/udc/charger.c new file +50 -0
+
+/*
+ * usb_charger_get_type_by_others() - Get the usb charger type by the
+callback
+ * which is implemented by users.
+ * @uchger - the usb charger device.
+ *
+ * Note: This function is just used for getting the charger type, not
+for
+ * detecting charger type which might affect the DP/DM line when gadget
+is on
+ * enumeration state.
+ */
+static enum usb_charger_type
+usb_charger_get_type_by_others(struct usb_charger *uchger) {
+     if (uchger->type != UNKNOWN_TYPE)
+             return uchger->type;
+
+     if (uchger->psy) {
+             union power_supply_propval val;
+
+             power_supply_get_property(uchger->psy,
+                                       POWER_SUPPLY_PROP_CHARGE_TYPE,
+                                       &val);
+             switch (val.intval) {
+             case POWER_SUPPLY_TYPE_USB:
+                     uchger->type = SDP_TYPE;
+                     break;
+             case POWER_SUPPLY_TYPE_USB_DCP:
+                     uchger->type = DCP_TYPE;
+                     break;
+             case POWER_SUPPLY_TYPE_USB_CDP:
+                     uchger->type = CDP_TYPE;
+                     break;
+             case POWER_SUPPLY_TYPE_USB_ACA:
+                     uchger->type = ACA_TYPE;
+                     break;
+             default:
+                     uchger->type = UNKNOWN_TYPE;
+                     break;
+             }
+     } else if (uchger->get_charger_type) {
+             uchger->type = uchger->get_charger_type(uchger);
+     } else {
+             uchger->type = UNKNOWN_TYPE;
+     }
+
+     return uchger->type;
+}
+
Signature · 2 lines
Baolin.wang
Best Regards
#4 lore raw

RE: [PATCH v10 1/4] gadget: Introduce the usb charger framework

Hi

Quoted 50 lines

-----Original Message-----
From: Baolin Wang [mailto:[email protected]]
Sent: Friday, April 08, 2016 5:56 PM
To: Jun Li <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; device- [email protected]; [email protected]
Subject: Re: [PATCH v10 1/4] gadget: Introduce the usb charger framework

Hi Jun,

Quoted 26 lines · Jun Li

Hi, Baolin

Quoted 24 lines
drivers/usb/gadget/udc/charger.c new file +24 -0
+/*
+ * usb_charger_detect_type() - detect the charger type manually.
+ * @uchger - usb charger device
+ *
+ * Note: You should ensure you need to detect the charger type
+manually on your
+ * platform.
+ * You should call it at the right gadget state to avoid affecting
+gadget
+ * enumeration.
+ */
+int usb_charger_detect_type(struct usb_charger *uchger) {
+     enum usb_charger_type type;
+
+     if (WARN(!uchger->charger_detect,
+              "charger detection method should not be NULL"))
+             return -EINVAL;
+
+     type = uchger->charger_detect(uchger);
+
+     mutex_lock(&uchger->lock);
+     uchger->type = type;
+     mutex_unlock(&uchger->lock);
+
Quoted 8 lines

drivers/usb/gadget/udc/charger.c line 217

Quoted 3 lines
drivers/usb/gadget/udc/charger.c new file +3 -0
+     return 0;
+}
+EXPORT_SYMBOL_GPL(usb_charger_detect_type);

I still recommend have a central place to call usb_charger_detect_type() to cover real charger detection instead of leaving this question open to diff users. This can be done after vbus-on is detected and before do gadget connect. I don't think this

Quoted 6 lines

will make your framework complicated.

This API is used for detecting the charger type manually (software charger detection), so if user's udc driver is needed to do this, then they can issue it at their right place to make it more flexible. But let us see other people's suggestion. Thanks.

Ok, actually this API can be used for what ever charger detection
type, user can implement any method(manual detection, directly read
result from some HW...what ever) in uchger->charger_detect(), target is
to have the charger type and set uchger->type, then you no need to add the comments/description to limit it usage. Also I do see there is possible central place to do this.

Quoted 62 lines
Quoted 48 lines

Hi Felipe, what do you think of this?

Li Jun

Quoted 42 lines
drivers/usb/gadget/udc/charger.c new file +42 -0
+
+/*
+ * usb_charger_get_type_by_others() - Get the usb charger type by
+the callback
+ * which is implemented by users.
+ * @uchger - the usb charger device.
+ *
+ * Note: This function is just used for getting the charger type,
+not for
+ * detecting charger type which might affect the DP/DM line when
+gadget is on
+ * enumeration state.
+ */
+static enum usb_charger_type
+usb_charger_get_type_by_others(struct usb_charger *uchger) {
+     if (uchger->type != UNKNOWN_TYPE)
+             return uchger->type;
+
+     if (uchger->psy) {
+             union power_supply_propval val;
+
+             power_supply_get_property(uchger->psy,
+                                       POWER_SUPPLY_PROP_CHARGE_TYPE,
+                                       &val);
+             switch (val.intval) {
+             case POWER_SUPPLY_TYPE_USB:
+                     uchger->type = SDP_TYPE;
+                     break;
+             case POWER_SUPPLY_TYPE_USB_DCP:
+                     uchger->type = DCP_TYPE;
+                     break;
+             case POWER_SUPPLY_TYPE_USB_CDP:
+                     uchger->type = CDP_TYPE;
+                     break;
+             case POWER_SUPPLY_TYPE_USB_ACA:
+                     uchger->type = ACA_TYPE;
+                     break;
+             default:
+                     uchger->type = UNKNOWN_TYPE;
+                     break;
+             }
+     } else if (uchger->get_charger_type) {
Quoted 8 lines

drivers/usb/gadget/udc/charger.c line 265

Quoted 8 lines
drivers/usb/gadget/udc/charger.c new file +8 -0
+             uchger->type = uchger->get_charger_type(uchger);
+     } else {
+             uchger->type = UNKNOWN_TYPE;
+     }
+
+     return uchger->type;
+}
+

--
Baolin.wang
Best Regards

#5 lore raw

Re: [PATCH v10 1/4] gadget: Introduce the usb charger framework

Hi Jun,

Quoted 34 lines
Quoted 34 lines
Quoted 24 lines
Quoted 24 lines
drivers/usb/gadget/udc/charger.c new file +24 -0
+/*
+ * usb_charger_detect_type() - detect the charger type manually.
+ * @uchger - usb charger device
+ *
+ * Note: You should ensure you need to detect the charger type
+manually on your
+ * platform.
+ * You should call it at the right gadget state to avoid affecting
+gadget
+ * enumeration.
+ */
+int usb_charger_detect_type(struct usb_charger *uchger) {
+     enum usb_charger_type type;
+
+     if (WARN(!uchger->charger_detect,
+              "charger detection method should not be NULL"))
+             return -EINVAL;
+
+     type = uchger->charger_detect(uchger);
+
+     mutex_lock(&uchger->lock);
+     uchger->type = type;
+     mutex_unlock(&uchger->lock);
+
Quoted 8 lines

drivers/usb/gadget/udc/charger.c line 217

Quoted 3 lines
drivers/usb/gadget/udc/charger.c new file +3 -0
+     return 0;
+}
+EXPORT_SYMBOL_GPL(usb_charger_detect_type);

I still recommend have a central place to call usb_charger_detect_type() to cover real charger detection instead of leaving this question open to diff users. This can be done after vbus-on is detected and before do gadget connect. I don't think this

will make your framework complicated.

Quoted 8 lines
Quoted 4 lines

This API is used for detecting the charger type manually (software charger detection), so if user's udc driver is needed to do this, then they can issue it at their right place to make it more flexible. But let us see other people's suggestion. Thanks.

Ok, actually this API can be used for what ever charger detection type, user can implement any method(manual detection, directly read result from some HW...what ever) in uchger->charger_detect(), target is

But reading result from some HW dose not means *detect*, actually is *get*. We can not mix them together with the different semantics.

Quoted 3 lines

to have the charger type and set uchger->type, then you no need to add the comments/description to limit it usage. Also I do see there is possible central place to do this.

Signature · 2 lines
Baolin.wang
Best Regards
#6 lore raw

RE: [PATCH v10 1/4] gadget: Introduce the usb charger framework

Quoted 57 lines

-----Original Message-----
From: Baolin Wang [mailto:[email protected]]
Sent: Friday, April 08, 2016 7:00 PM
To: Jun Li <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; device- [email protected]; [email protected]
Subject: Re: [PATCH v10 1/4] gadget: Introduce the usb charger framework

Hi Jun,

Quoted 40 lines
Quoted 39 lines
Quoted 25 lines
Quoted 25 lines
drivers/usb/gadget/udc/charger.c new file +25 -0
+/*
+ * usb_charger_detect_type() - detect the charger type manually.
+ * @uchger - usb charger device
+ *
+ * Note: You should ensure you need to detect the charger type
+manually on your
+ * platform.
+ * You should call it at the right gadget state to avoid
+affecting gadget
+ * enumeration.
+ */
+int usb_charger_detect_type(struct usb_charger *uchger) {
+     enum usb_charger_type type;
+
+     if (WARN(!uchger->charger_detect,
+              "charger detection method should not be NULL"))
+             return -EINVAL;
+
+     type = uchger->charger_detect(uchger);
+
+     mutex_lock(&uchger->lock);
+     uchger->type = type;
+     mutex_unlock(&uchger->lock);
+
+     return 0;
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+}
+EXPORT_SYMBOL_GPL(usb_charger_detect_type);

I still recommend have a central place to call usb_charger_detect_type() to cover real charger detection instead of leaving this question open to diff users. This can be done after vbus-on is detected and before do gadget connect. I don't think this

will make your framework complicated.

This API is used for detecting the charger type manually (software charger detection), so if user's udc driver is needed to do this, then they can issue it at their right place to make it more flexible. But let us see other people's suggestion. Thanks.

Quoted 7 lines
Quoted 4 lines

Ok, actually this API can be used for what ever charger detection type, user can implement any method(manual detection, directly read result from some HW...what ever) in uchger->charger_detect(), target is

But reading result from some HW dose not means *detect*, actually is *get*. We can not mix them together with the different semantics.

It doesn’t matter here, you already define the _get_ API for just read the charger type from the saved value(uchger->type), so we can think the API to make uchger->type from UNKNOW to ONE KNOWN type is *detect*, because we don't need 2 separate API one for *get* type from HW and another one for *detect* from HW, only one API involve HW access is enough.

- Detect: (write to and) read from HW to get the charger type, save to uchger->type;
- Get: read the charger type from uchger->type.

Quoted 9 lines
Quoted 3 lines

to have the charger type and set uchger->type, then you no need to add the comments/description to limit it usage. Also I do see there is

possible central place to do this.

Quoted 1 line

--
Baolin.wang
Best Regards

#7 lore raw

Re: [PATCH v10 1/4] gadget: Introduce the usb charger framework

Quoted 62 lines · Jun Li
Quoted 62 lines

-----Original Message-----
From: Baolin Wang [mailto:[email protected]]
Sent: Friday, April 08, 2016 7:00 PM
To: Jun Li <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; device- [email protected]; [email protected]
Subject: Re: [PATCH v10 1/4] gadget: Introduce the usb charger framework

Hi Jun,

Quoted 44 lines
Quoted 33 lines
Quoted 25 lines
Quoted 25 lines
drivers/usb/gadget/udc/charger.c new file +25 -0
+/*
+ * usb_charger_detect_type() - detect the charger type manually.
+ * @uchger - usb charger device
+ *
+ * Note: You should ensure you need to detect the charger type
+manually on your
+ * platform.
+ * You should call it at the right gadget state to avoid
+affecting gadget
+ * enumeration.
+ */
+int usb_charger_detect_type(struct usb_charger *uchger) {
+     enum usb_charger_type type;
+
+     if (WARN(!uchger->charger_detect,
+              "charger detection method should not be NULL"))
+             return -EINVAL;
+
+     type = uchger->charger_detect(uchger);
+
+     mutex_lock(&uchger->lock);
+     uchger->type = type;
+     mutex_unlock(&uchger->lock);
+
+     return 0;
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+}
+EXPORT_SYMBOL_GPL(usb_charger_detect_type);

I still recommend have a central place to call usb_charger_detect_type() to cover real charger detection instead of leaving this question open to diff users. This can be done after vbus-on is detected and before do gadget connect. I don't think this

Quoted 6 lines

will make your framework complicated.

This API is used for detecting the charger type manually (software charger detection), so if user's udc driver is needed to do this, then they can issue it at their right place to make it more flexible. But let us see other people's suggestion. Thanks.

Ok, actually this API can be used for what ever charger detection type, user can implement any method(manual detection, directly read result from some HW...what ever) in uchger->charger_detect(), target is

Quoted 8 lines
Quoted 2 lines

But reading result from some HW dose not means *detect*, actually is *get*. We can not mix them together with the different semantics.

It doesn’t matter here, you already define the _get_ API for just read the charger type from the saved value(uchger->type), so we can think the API to make uchger->type from UNKNOW to ONE KNOWN type is *detect*, because we don't need 2 separate API one for *get* type from HW and another one for *detect* from HW, only one API involve HW access is enough.

But another issue is some users may need to get the charger type from power supply by "power_supply_get_property()" function, we need to integrate with the power supply things in the usb charger framework, not user to implement that.

Quoted 14 lines

- Detect: (write to and) read from HW to get the charger type, save to uchger->type;
- Get: read the charger type from uchger->type.

Quoted 9 lines
Quoted 3 lines

to have the charger type and set uchger->type, then you no need to add the comments/description to limit it usage. Also I do see there is

possible central place to do this.

Quoted 1 line

--
Baolin.wang
Best Regards

Signature · 2 lines
Baolin.wang
Best Regards
#8 lore raw

RE: [PATCH v10 1/4] gadget: Introduce the usb charger framework

Hi

Quoted 87 lines

-----Original Message-----
From: Baolin Wang [mailto:[email protected]]
Sent: Friday, April 08, 2016 7:51 PM
To: Jun Li <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; device- [email protected]; [email protected]
Subject: Re: [PATCH v10 1/4] gadget: Introduce the usb charger framework

Quoted 59 lines · Jun Li
Quoted 59 lines

-----Original Message-----
From: Baolin Wang [mailto:[email protected]]
Sent: Friday, April 08, 2016 7:00 PM
To: Jun Li <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; device- [email protected]; [email protected]
Subject: Re: [PATCH v10 1/4] gadget: Introduce the usb charger framework

Hi Jun,

Quoted 40 lines
Quoted 39 lines
Quoted 25 lines
Quoted 25 lines
(quoted diff) +25 -0
+/*
+ * usb_charger_detect_type() - detect the charger type manually.
+ * @uchger - usb charger device
+ *
+ * Note: You should ensure you need to detect the charger type
+manually on your
+ * platform.
+ * You should call it at the right gadget state to avoid
+affecting gadget
+ * enumeration.
+ */
+int usb_charger_detect_type(struct usb_charger *uchger) {
+     enum usb_charger_type type;
+
+     if (WARN(!uchger->charger_detect,
+              "charger detection method should not be NULL"))
+             return -EINVAL;
+
+     type = uchger->charger_detect(uchger);
+
+     mutex_lock(&uchger->lock);
+     uchger->type = type;
+     mutex_unlock(&uchger->lock);
+
+     return 0;
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+}
+EXPORT_SYMBOL_GPL(usb_charger_detect_type);

I still recommend have a central place to call usb_charger_detect_type() to cover real charger detection instead of leaving this question open to diff users. This can be done after vbus-on is detected and before do gadget connect. I don't think this

will make your framework complicated.

This API is used for detecting the charger type manually (software charger detection), so if user's udc driver is needed to do this, then they can issue it at their right place to make it more flexible. But let us see other people's suggestion. Thanks.

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

Ok, actually this API can be used for what ever charger detection type, user can implement any method(manual detection, directly read result from some HW...what ever) in uchger->charger_detect(), target is

But reading result from some HW dose not means *detect*, actually is

*get*.

Quoted 7 lines
Quoted 1 line

We can not mix them together with the different semantics.

It doesn’t matter here, you already define the _get_ API for just read the charger type from the saved value(uchger->type), so we can think the API to make uchger->type from UNKNOW to ONE KNOWN type is *detect*, because we don't need 2 separate API one for *get* type from HW and another one for *detect* from HW, only one API involve HW access

Quoted 6 lines

is enough.

But another issue is some users may need to get the charger type from power supply by "power_supply_get_property()" function, we need to integrate with the power supply things in the usb charger framework, not user to implement that.

Why this user(your case) can't put the charger type get by "power_supply_get_property()" in its uchger->charger_detect(), any special reason prevent you doing it? I am not sure if this case is very common, even if it is, you also can put it in usb_charger_detect_type()

usb_charger_detect_type()

{
	If can get from power_supply_get_property()
		...
	else if uchger->charger_detect
		uchger->charger_detect();
	...
}

I don't know if most design of usb charger detection now days is as easy as your PMIC for software(HW does the whole detection process and prevent the vbus generation until detection has completed), anyway if your framework can guarantee the detection process finished before gadget connect, then each user don't have to worry about the HW detection process details and seek a proper place to do it.

Li Jun

Quoted 22 lines
Quoted 16 lines

- Detect: (write to and) read from HW to get the charger type, save to uchger->type;
- Get: read the charger type from uchger->type.

Quoted 10 lines
Quoted 4 lines

to have the charger type and set uchger->type, then you no need to add the comments/description to limit it usage. Also I do see there is

possible central place to do this.

Quoted 1 line

--
Baolin.wang
Best Regards

--
Baolin.wang
Best Regards

#9 lore raw

Re: [PATCH v10 1/4] gadget: Introduce the usb charger framework

Hi Jun,

Sorry for late reply.

Quoted 67 lines
Quoted 54 lines
Quoted 40 lines
Quoted 40 lines
Quoted 40 lines
Quoted 39 lines
Quoted 25 lines
Quoted 25 lines
(quoted diff) +25 -0
+/*
+ * usb_charger_detect_type() - detect the charger type manually.
+ * @uchger - usb charger device
+ *
+ * Note: You should ensure you need to detect the charger type
+manually on your
+ * platform.
+ * You should call it at the right gadget state to avoid
+affecting gadget
+ * enumeration.
+ */
+int usb_charger_detect_type(struct usb_charger *uchger) {
+     enum usb_charger_type type;
+
+     if (WARN(!uchger->charger_detect,
+              "charger detection method should not be NULL"))
+             return -EINVAL;
+
+     type = uchger->charger_detect(uchger);
+
+     mutex_lock(&uchger->lock);
+     uchger->type = type;
+     mutex_unlock(&uchger->lock);
+
+     return 0;
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+}
+EXPORT_SYMBOL_GPL(usb_charger_detect_type);

I still recommend have a central place to call usb_charger_detect_type() to cover real charger detection instead of leaving this question open to diff users. This can be done after vbus-on is detected and before do gadget connect. I don't think this

will make your framework complicated.

This API is used for detecting the charger type manually (software charger detection), so if user's udc driver is needed to do this, then they can issue it at their right place to make it more flexible. But let us see other people's suggestion. Thanks.

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

Ok, actually this API can be used for what ever charger detection type, user can implement any method(manual detection, directly read result from some HW...what ever) in uchger->charger_detect(), target is

But reading result from some HW dose not means *detect*, actually is

*get*.

Quoted 7 lines
Quoted 1 line

We can not mix them together with the different semantics.

It doesn’t matter here, you already define the _get_ API for just read the charger type from the saved value(uchger->type), so we can think the API to make uchger->type from UNKNOW to ONE KNOWN type is *detect*, because we don't need 2 separate API one for *get* type from HW and another one for *detect* from HW, only one API involve HW access

Quoted 6 lines

is enough.

But another issue is some users may need to get the charger type from power supply by "power_supply_get_property()" function, we need to integrate with the power supply things in the usb charger framework, not user to implement that.

Why this user(your case) can't put the charger type get by "power_supply_get_property()" in its uchger->charger_detect(), any special reason prevent you doing it? I am not sure if this case is very common, even if it is, you also can put it in usb_charger_detect_type()

Quoted 8 lines

usb_charger_detect_type()

{
        If can get from power_supply_get_property()
                ...
        else if uchger->charger_detect
                uchger->charger_detect();
        ...
}

If user want to implement above method, they need to get the 'power_supply' structure to do this action, which maybe can not get in some contexts. So we need to integrate with the power supply things to avoid this situation. IIRC, Felipe suggested me to flesh out the charger getting method.

Hi Felipe, what do you think of Jun's suggestion? Thanks.

Quoted 31 lines

I don't know if most design of usb charger detection now days is as easy as your PMIC for software(HW does the whole detection process and prevent the vbus generation until detection has completed), anyway if your framework can guarantee the detection process finished before gadget connect, then each user don't have to worry about the HW detection process details and seek a proper place to do it.

Li Jun

Quoted 22 lines
Quoted 16 lines

- Detect: (write to and) read from HW to get the charger type, save to uchger->type;
- Get: read the charger type from uchger->type.

Quoted 10 lines
Quoted 4 lines

to have the charger type and set uchger->type, then you no need to add the comments/description to limit it usage. Also I do see there is

possible central place to do this.

Quoted 1 line

--
Baolin.wang
Best Regards

--
Baolin.wang
Best Regards

Signature · 2 lines
Baolin.wang
Best Regards
#10 lore raw

RE: [PATCH v10 1/4] gadget: Introduce the usb charger framework

Hi

Quoted 97 lines

-----Original Message-----
From: Baolin Wang [mailto:[email protected]]
Sent: Monday, April 11, 2016 7:15 PM
To: Jun Li <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; device- [email protected]; [email protected]
Subject: Re: [PATCH v10 1/4] gadget: Introduce the usb charger framework

Hi Jun,

Sorry for late reply.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+/*
+ * usb_charger_detect_type() - detect the charger type

manually.

Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
Quoted 15 lines

+ * @uchger - usb charger device
+ *
+ * Note: You should ensure you need to detect the charger

(quoted diff) +12 -0
+type manually on your
+ * platform.
+ * You should call it at the right gadget state to avoid
+affecting gadget
+ * enumeration.
+ */
+int usb_charger_detect_type(struct usb_charger *uchger) {
+     enum usb_charger_type type;
+
+     if (WARN(!uchger->charger_detect,
+              "charger detection method should not be NULL"))
+             return -EINVAL;

Review

Quoted 10 lines
(quoted diff) +10 -0
+
+     type = uchger->charger_detect(uchger);
+
+     mutex_lock(&uchger->lock);
+     uchger->type = type;
+     mutex_unlock(&uchger->lock);
+
+     return 0;
+}
+EXPORT_SYMBOL_GPL(usb_charger_detect_type);

I still recommend have a central place to call usb_charger_detect_type() to cover real charger detection instead of leaving this question open to diff users. This can be done after vbus-on is detected and before do gadget connect. I don't think this

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

will make your framework complicated.

This API is used for detecting the charger type manually (software charger detection), so if user's udc driver is needed to do this, then they can issue it at their right place to make

it more flexible.

Quoted 31 lines
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
Quoted 6 lines
Quoted 1 line

But let us see other people's suggestion. Thanks.

Ok, actually this API can be used for what ever charger detection type, user can implement any method(manual detection, directly read result from some HW...what ever) in uchger->charger_detect(), target is

But reading result from some HW dose not means *detect*, actually is

*get*.

Quoted 8 lines
Quoted 1 line

We can not mix them together with the different semantics.

It doesn’t matter here, you already define the _get_ API for just read the charger type from the saved value(uchger->type), so we can think the API to make uchger->type from UNKNOW to ONE KNOWN type is *detect*, because we don't need 2 separate API one for *get* type from HW and another one for *detect* from HW, only one API involve HW access

Quoted 6 lines

is enough.

But another issue is some users may need to get the charger type from power supply by "power_supply_get_property()" function, we need to integrate with the power supply things in the usb charger framework, not user to implement that.

Why this user(your case) can't put the charger type get by "power_supply_get_property()" in its uchger->charger_detect(), any special reason prevent you doing it? I am not sure if this case is very common, even if it is, you also can put it in usb_charger_detect_type()

Quoted 8 lines

usb_charger_detect_type()

{
        If can get from power_supply_get_property()
                ...
        else if uchger->charger_detect
                uchger->charger_detect();
        ...
}
Quoted 6 lines

If user want to implement above method, they need to get the 'power_supply' structure to do this action, which maybe can not get in some contexts. So we need to integrate with the power supply things to avoid this situation. IIRC, Felipe suggested me to flesh out the charger getting method.

Okay, then I agree with you to let user do that with more flexibility, I tried to enable usb charger detection on one NXP i.mx platform based on this framework, works fine, thanks!

Li Jun

Quoted 39 lines

Hi Felipe, what do you think of Jun's suggestion? Thanks.

Quoted 31 lines

I don't know if most design of usb charger detection now days is as easy as your PMIC for software(HW does the whole detection process and prevent the vbus generation until detection has completed), anyway if your framework can guarantee the detection process finished before gadget connect, then each user don't have to worry about the HW detection process details and seek a proper place to do it.

Li Jun

Quoted 22 lines
Quoted 16 lines

- Detect: (write to and) read from HW to get the charger type, save to uchger->type;
- Get: read the charger type from uchger->type.

Quoted 10 lines
Quoted 4 lines

to have the charger type and set uchger->type, then you no need to add the comments/description to limit it usage. Also I do see there is

possible central place to do this.

Quoted 1 line

--
Baolin.wang
Best Regards

--
Baolin.wang
Best Regards

--
Baolin.wang
Best Regards

#11 lore raw

Re: [PATCH v10 1/4] gadget: Introduce the usb charger framework

Hi Jun,

Quoted 28 lines · Jun Li
Quoted 21 lines
Quoted 12 lines
Quoted 5 lines

But another issue is some users may need to get the charger type from power supply by "power_supply_get_property()" function, we need to integrate with the power supply things in the usb charger framework, not user to implement that.

Why this user(your case) can't put the charger type get by "power_supply_get_property()" in its uchger->charger_detect(), any special reason prevent you doing it? I am not sure if this case is very common, even if it is, you also can put it in usb_charger_detect_type()

Quoted 8 lines

usb_charger_detect_type()

{
        If can get from power_supply_get_property()
                ...
        else if uchger->charger_detect
                uchger->charger_detect();
        ...
}
Quoted 6 lines

If user want to implement above method, they need to get the 'power_supply' structure to do this action, which maybe can not get in some contexts. So we need to integrate with the power supply things to avoid this situation. IIRC, Felipe suggested me to flesh out the charger getting method.

Quoted 3 lines

Okay, then I agree with you to let user do that with more flexibility, I tried to enable usb charger detection on one NXP i.mx platform based on this framework, works fine, thanks!

That's great. Thanks.

Signature · 2 lines
Baolin.wang
Best Regards
#12 lore raw

PATCH v10 2/4 gadget: Support for the usb charger framework

In reply to Baolin Wang

For supporting the usb charger, it adds the usb_charger_init() and usb_charger_exit() functions for usb charger initialization and exit.

It will report to the usb charger when the gadget state is changed, then the usb charger can do the power things.

---
 drivers/usb/gadget/udc/udc-core.c |   11 +++++++++++
 include/linux/usb/gadget.h        |   13 +++++++++++++
 2 files changed, 24 insertions(+)
drivers/usb/gadget/udc/udc-core.c +11 -0
index b86a6f0..8d98c6b 100644
@@ -28,6 +28,7 @@
 #include <linux/usb/ch9.h>
 #include <linux/usb/gadget.h>
 #include <linux/usb.h>
+#include <linux/usb/charger.h>
 
 /**
  * struct usb_udc - describes one usb device controller
@@ -230,6 +231,9 @@ static void usb_gadget_state_work(struct work_struct *work)
 	struct usb_gadget *gadget = work_to_gadget(work);
 	struct usb_udc *udc = gadget->udc;
 
+	/* when the gadget state is changed, then report to USB charger */
+	usb_charger_plug_by_gadget(gadget, gadget->state);
+
 	if (udc)
 		sysfs_notify(&udc->dev.kobj, NULL, "state");
 }
@@ -423,8 +427,14 @@ int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
 
 	mutex_unlock(&udc_lock);
 
+	ret = usb_charger_init(gadget);
+	if (ret)
+		goto err5;
+
 	return 0;
 
+err5:
+	device_del(&udc->dev);
 err4:
 	list_del(&udc->list);
 	mutex_unlock(&udc_lock);
@@ -503,6 +513,7 @@ void usb_del_gadget_udc(struct usb_gadget *gadget)
 	kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
 	flush_work(&gadget->work);
 	device_unregister(&udc->dev);
+	usb_charger_exit(gadget);
 	device_unregister(&gadget->dev);
 }
 EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
include/linux/usb/gadget.h +13 -0
index d82d006..8b36a4e 100644
@@ -24,6 +24,7 @@
 #include <linux/types.h>
 #include <linux/workqueue.h>
 #include <linux/usb/ch9.h>
+#include <linux/usb/charger.h>
 
 struct usb_ep;
 
@@ -635,6 +636,8 @@ struct usb_gadget {
 	unsigned			out_epnum;
 	unsigned			in_epnum;
 	struct usb_otg_caps		*otg_caps;
+	/* negotiate the power with the usb charger */
+	struct usb_charger		*charger;
 
 	unsigned			sg_supported:1;
 	unsigned			is_otg:1;
@@ -839,10 +842,20 @@ static inline int usb_gadget_vbus_connect(struct usb_gadget *gadget)
  * reporting how much power the device may consume.  For example, this
  * could affect how quickly batteries are recharged.
  *
+ * It will also notify the USB charger how much power the device may
+ * consume if there is a USB charger linking with the gadget.
+ *
  * Returns zero on success, else negative errno.
  */
 static inline int usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
 {
+	enum usb_charger_type type;
+
+	if (gadget->charger) {
+		type = usb_charger_get_type(gadget->charger);
+		usb_charger_set_cur_limit_by_type(gadget->charger, type, mA);
+	}
+
 	if (!gadget->ops->vbus_draw)
 		return -EOPNOTSUPP;
 	return gadget->ops->vbus_draw(gadget, mA);
Signature · 1 line
1.7.9.5
#13 lore raw

PATCH v10 3/4 gadget: Integrate with the usb gadget supporting for usb charger

In reply to Baolin Wang

When the usb gadget supporting for usb charger is ready, the usb charger can implement the usb_charger_plug_by_gadget() function and usb_charger_exit() function by getting 'struct usb_charger' from 'struct gadget'.

---
 drivers/usb/gadget/udc/charger.c |   39 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)
drivers/usb/gadget/udc/charger.c +38 -1
index e24cb0a..ee711cd 100644
@@ -527,6 +527,30 @@ usb_charger_plug_by_extcon(struct notifier_block *nb,
 int usb_charger_plug_by_gadget(struct usb_gadget *gadget,
 			       unsigned long state)
 {
+	struct usb_charger *uchger = gadget->charger;
+	enum usb_charger_state uchger_state;
+
+	if (WARN(!uchger, "charger can not be NULL"))
+		return -EINVAL;
+
+	/*
+	 * Report event to power to setting the current limitation
+	 * for this usb charger when one usb charger state is changed
+	 * with detecting by usb gadget state.
+	 */
+	if (uchger->old_gadget_state != state) {
+		uchger->old_gadget_state = state;
+
+		if (state >= USB_STATE_ATTACHED)
+			uchger_state = USB_CHARGER_PRESENT;
+		else if (state == USB_STATE_NOTATTACHED)
+			uchger_state = USB_CHARGER_REMOVE;
+		else
+			uchger_state = USB_CHARGER_DEFAULT;
+
+		usb_charger_notify_others(uchger, uchger_state);
+	}
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(usb_charger_plug_by_gadget);
@@ -683,6 +707,7 @@ int usb_charger_init(struct usb_gadget *ugadget)
 
 	/* register a notifier on a usb gadget device */
 	uchger->gadget = ugadget;
+	ugadget->charger = uchger;
 	uchger->old_gadget_state = ugadget->state;
 
 	/* register a new usb charger */
@@ -703,7 +728,19 @@ fail:
 
 int usb_charger_exit(struct usb_gadget *ugadget)
 {
-	return 0;
+	struct usb_charger *uchger = ugadget->charger;
+
+	if (WARN(!uchger, "charger can not be NULL"))
+		return -EINVAL;
+
+	if (uchger->extcon_dev)
+		extcon_unregister_notifier(uchger->extcon_dev,
+					   EXTCON_USB,
+					   &uchger->extcon_nb.nb);
+
+	ida_simple_remove(&usb_charger_ida, uchger->id);
+
+	return usb_charger_unregister(uchger);
 }
 
 static int __init usb_charger_class_init(void)
Signature · 1 line
1.7.9.5
#14 lore raw

PATCH v10 4/4 power: wm831x_power: Support USB charger current limit management

In reply to Baolin Wang

Integrate with the newly added USB charger interface to limit the current we draw from the USB input based on the input device configuration identified by the USB stack, allowing us to charge more quickly from high current inputs without drawing more current than specified from others.

---
 drivers/power/wm831x_power.c     |   69 ++++++++++++++++++++++++++++++++++++++
 include/linux/mfd/wm831x/pdata.h |    3 ++
 2 files changed, 72 insertions(+)
drivers/power/wm831x_power.c +69 -0
index 7082301..cef1812 100644
@@ -13,6 +13,7 @@
 #include <linux/platform_device.h>
 #include <linux/power_supply.h>
 #include <linux/slab.h>
+#include <linux/usb/charger.h>
 
 #include <linux/mfd/wm831x/core.h>
 #include <linux/mfd/wm831x/auxadc.h>
@@ -31,6 +32,8 @@ struct wm831x_power {
 	char usb_name[20];
 	char battery_name[20];
 	bool have_battery;
+	struct usb_charger *usb_charger;
+	struct notifier_block usb_notify;
 };
 
 static int wm831x_power_check_online(struct wm831x *wm831x, int supply,
@@ -125,6 +128,43 @@ static enum power_supply_property wm831x_usb_props[] = {
 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
 };
 
+/* In milliamps */
+static const unsigned int wm831x_usb_limits[] = {
+	0,
+	2,
+	100,
+	500,
+	900,
+	1500,
+	1800,
+	550,
+};
+
+static int wm831x_usb_limit_change(struct notifier_block *nb,
+				   unsigned long limit, void *data)
+{
+	struct wm831x_power *wm831x_power = container_of(nb,
+							 struct wm831x_power,
+							 usb_notify);
+	unsigned int i, best;
+
+	/* Find the highest supported limit */
+	best = 0;
+	for (i = 0; i < ARRAY_SIZE(wm831x_usb_limits); i++) {
+		if (limit >= wm831x_usb_limits[i] &&
+		    wm831x_usb_limits[best] < wm831x_usb_limits[i])
+			best = i;
+	}
+
+	dev_dbg(wm831x_power->wm831x->dev,
+		"Limiting USB current to %umA", wm831x_usb_limits[best]);
+
+	wm831x_set_bits(wm831x_power->wm831x, WM831X_POWER_STATE,
+		        WM831X_USB_ILIM_MASK, best);
+
+	return 0;
+}
+
 /*********************************************************************
  *		Battery properties
  *********************************************************************/
@@ -607,8 +647,31 @@ static int wm831x_power_probe(struct platform_device *pdev)
 		}
 	}
 
+	if (wm831x_pdata && wm831x_pdata->usb_gadget) {
+		power->usb_charger =
+			usb_charger_find_by_name(wm831x_pdata->usb_gadget);
+		if (IS_ERR(power->usb_charger)) {
+			ret = PTR_ERR(power->usb_charger);
+			dev_err(&pdev->dev,
+				"Failed to find USB gadget: %d\n", ret);
+			goto err_bat_irq;
+		}
+
+		power->usb_notify.notifier_call = wm831x_usb_limit_change;
+
+		ret = usb_charger_register_notify(power->usb_charger,
+						  &power->usb_notify);
+		if (ret != 0) {
+			dev_err(&pdev->dev,
+				"Failed to register notifier: %d\n", ret);
+			goto err_usb_charger;
+		}
+	}
+
 	return ret;
 
+err_usb_charger:
+	/* put_device on charger */
 err_bat_irq:
 	--i;
 	for (; i >= 0; i--) {
@@ -637,6 +700,12 @@ static int wm831x_power_remove(struct platform_device *pdev)
 	struct wm831x *wm831x = wm831x_power->wm831x;
 	int irq, i;
 
+	if (wm831x_power->usb_charger) {
+		usb_charger_unregister_notify(wm831x_power->usb_charger,
+					      &wm831x_power->usb_notify);
+		/* Free charger */
+	}
+
 	for (i = 0; i < ARRAY_SIZE(wm831x_bat_irqs); i++) {
 		irq = wm831x_irq(wm831x, 
 				 platform_get_irq_byname(pdev,
include/linux/mfd/wm831x/pdata.h +3 -0
index dcc9631..5af8399 100644
@@ -126,6 +126,9 @@ struct wm831x_pdata {
 	/** The driver should initiate a power off sequence during shutdown */
 	bool soft_shutdown;
 
+	/** dev_name of USB charger gadget to integrate with */
+	const char *usb_gadget;
+
 	int irq_base;
 	int gpio_base;
 	int gpio_defaults[WM831X_GPIO_NUM];
Signature · 6 lines
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#15 lore raw
In reply to Baolin Wang

Hi Felipe,

What do you think of this version patchset? Could you review this patchset when you feel free? I really hope to move the usb charger forwards with suggestions. Thanks.

Quoted 38 lines · Baolin Wang

Currently the Linux kernel does not provide any standard integration of this feature that integrates the USB subsystem with the system power regulation provided by PMICs meaning that either vendors must add this in their kernels or USB gadget devices based on Linux (such as mobile phones) may not behave as they should. Thus provide a standard framework for doing this in kernel.

Now introduce one user with wm831x_power to support and test the usb charger, which is pending testing. Moreover there may be other potential users will use it in future.

Changes since v9:
- Remove some redundant sysfs attributes.
- Change the SDP charger default current if gadget is SS.
- Remove the 'get_charger_type' callback in gadget->ops.

Baolin Wang (4):
gadget: Introduce the usb charger framework
gadget: Support for the usb charger framework
gadget: Integrate with the usb gadget supporting for usb charger
power: wm831x_power: Support USB charger current limit management

 drivers/power/wm831x_power.c      |   69 ++++
 drivers/usb/gadget/Kconfig        |    7 +
 drivers/usb/gadget/udc/Makefile   |    1 +
 drivers/usb/gadget/udc/charger.c  |  766 +++++++++++++++++++++++++++++++++++++
 drivers/usb/gadget/udc/udc-core.c |   11 +
 include/linux/mfd/wm831x/pdata.h  |    3 +
 include/linux/usb/charger.h       |  173 +++++++++
 include/linux/usb/gadget.h        |   13 +
 include/uapi/linux/usb/charger.h  |   31 ++
 9 files changed, 1074 insertions(+)
 create mode 100644 drivers/usb/gadget/udc/charger.c
 create mode 100644 include/linux/usb/charger.h
 create mode 100644 include/uapi/linux/usb/charger.h

--
1.7.9.5

Signature · 2 lines
Baolin.wang
Best Regards