Skip to contentSkip to conversation

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

PATCH Superseded Baolin Wang 2 participants 7 messages Jun 30, 2016

Subsystems (3)

v14 of 19 Superseded Latest is v19 →

lore
Share & tools
lore
#0 lore raw

v14 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 v13:
- Remove the charger checking in usb_gadget_vbus_draw() function.
- Rename some functions in charger.c file.
- Rebase on git://git.kernel.org/pub/scm/linux/kernel/git/balbi/usb.git tags/usb-for-v4.8

Changes since v12:
- Remove the class and device things.
- Link usb charger to udc-core.ko.
- Create one "charger" subdirectory which holds all charger-related attributes.

Changes since v11:
- Reviewed and tested by Li Jun.

Changes since v10:
- Introduce usb_charger_get_state() function to check charger state.
- Remove the mutex lock in usb_charger_set_cur_limit_by_type() function in case will be issued in atomic context.

Baolin Wang (4):
usb: gadget: Introduce the usb charger framework
usb: gadget: Support for the usb charger framework
usb: 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       |    8 +
 drivers/usb/gadget/udc/Makefile  |    1 +
 drivers/usb/gadget/udc/charger.c |  743 ++++++++++++++++++++++++++++++++++++++
 drivers/usb/gadget/udc/core.c    |   17 +
 include/linux/mfd/wm831x/pdata.h |    3 +
 include/linux/usb/charger.h      |  164 +++++++++
 include/linux/usb/gadget.h       |    3 +
 include/uapi/linux/usb/charger.h |   31 ++
 9 files changed, 1039 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 v14 1/4 usb: 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       |    8 +
 drivers/usb/gadget/udc/Makefile  |    1 +
 drivers/usb/gadget/udc/charger.c |  679 ++++++++++++++++++++++++++++++++++++++
 include/linux/usb/charger.h      |  164 +++++++++
 include/uapi/linux/usb/charger.h |   31 ++
 5 files changed, 883 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 +8 -0
index 3c3f31c..acea3f7 100644
@@ -134,6 +134,14 @@ config U_SERIAL_CONSOLE
 	help
 	   It supports the serial gadget can be used as a console.
 
+config USB_CHARGER
+	bool "USB charger support"
+	select EXTCON
+	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 98e74ed..ede2351 100644
@@ -2,6 +2,7 @@
 CFLAGS_trace.o			:= -I$(src)
 
 udc-core-y			:= core.o trace.o
+udc-core-$(CONFIG_USB_CHARGER)	+= charger.o
 
 #
 # USB peripheral controller drivers
drivers/usb/gadget/udc/charger.c new file +679 -0

679 lines not on this page · raw message

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

164 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 · 1 line
1.7.9.5
#2 lore raw

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

Hi,

Quoted 7 lines · Baolin Wang
drivers/usb/gadget/udc/charger.c new file +7 -0
+static ssize_t charger_state_show(struct device *dev,
+				  struct device_attribute *attr,
+				  char *buf)
+{
+	struct usb_charger *uchger = dev_to_uchger(dev);
+	int cnt;
+

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

Quoted 10 lines
drivers/usb/gadget/udc/charger.c new file +10 -0
+	switch (uchger->state) {
+	case USB_CHARGER_PRESENT:
+		cnt = sprintf(buf, "%s\n", "PRESENT");
+		break;
+	case USB_CHARGER_REMOVE:
+		cnt = sprintf(buf, "%s\n", "REMOVE");
+		break;
+	default:
+		cnt = sprintf(buf, "%s\n", "UNKNOWN");
+		break;

are these the only states we need? Don't we need at least "CHARGING" and "ERROR" or something like that? Maybe those are exposed elsewhere, dunno.

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

Quoted 6 lines
drivers/usb/gadget/udc/charger.c new file +6 -0
+static int __usb_charger_set_cur_limit_by_type(struct usb_charger *uchger,
+					       enum usb_charger_type type,
+					       unsigned int cur_limit)
+{
+	if (WARN(!uchger, "charger can not be NULL"))
+		return -EINVAL;

IIRC, I mentioned that this should assume charger to be a valid pointer. Look at this, for a second:

You check that you have a valid pointer here...

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

Quoted 8 lines
drivers/usb/gadget/udc/charger.c new file +8 -0
+int usb_charger_set_cur_limit_by_type(struct usb_charger *uchger,
+				      enum usb_charger_type type,
+				      unsigned int cur_limit)
+{
+	int ret;
+
+	if (WARN(!uchger, "charger can not be NULL"))
+		return -EINVAL;

... and here, before calling the other function. This is the only place which should check for valid uchger.

Quoted 8 lines
drivers/usb/gadget/udc/charger.c new file +8 -0
+	mutex_lock(&uchger->lock);
+	ret = __usb_charger_set_cur_limit_by_type(uchger, type, cur_limit);
+	mutex_unlock(&uchger->lock);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(usb_charger_set_cur_limit_by_type);
+

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

Quoted 10 lines
drivers/usb/gadget/udc/charger.c new file +10 -0
+/*
+ * usb_charger_set_cur_limit() - Set the current limitation.
+ * @uchger - the usb charger instance.
+ * @cur_limit_set - the current limitation.
+ */
+int usb_charger_set_cur_limit(struct usb_charger *uchger,
+			      struct usb_charger_cur_limit *cur_limit_set)
+{
+	if (WARN(!uchger || !cur_limit_set, "charger or setting can't be NULL"))
+		return -EINVAL;

I can see a pattern here. Not *all* errors need a splat. Sometimes a simple pr_err() or dev_err() (when you have a valid dev pointer) are enough.

Quoted 17 lines
include/linux/usb/charger.h new file +11 -0
new file mode 100644
index 0000000..d2e745e
@@ -0,0 +1,164 @@
+#ifndef __LINUX_USB_CHARGER_H__
+#define __LINUX_USB_CHARGER_H__
+
+#include <uapi/linux/usb/ch9.h>
+#include <uapi/linux/usb/charger.h>
+
+#define CHARGER_NAME_MAX 30
+
+/* Current limitation by charger type */
+struct usb_charger_cur_limit {
+	unsigned int sdp_cur_limit;

include/linux/usb/charger.h line 21

Quoted 10 lines
include/linux/usb/charger.h new file +10 -0
+	unsigned int dcp_cur_limit;
+	unsigned int cdp_cur_limit;
+	unsigned int aca_cur_limit;
+};
+
+struct usb_charger_nb {
+	struct notifier_block	nb;
+	struct usb_charger	*uchger;
+};
+

please add KernelDoc here. And mention the fields which aren't supposed to be accessed directly but should rely on the accessor functions. At least type and state prefer to be accessed by their respective getter/setter methods.

Quoted 34 lines
include/linux/usb/charger.h new file +34 -0
+struct usb_charger {
+	char			name[CHARGER_NAME_MAX];
+	struct list_head	list;
+	struct raw_notifier_head	uchger_nh;
+	/* protect the notifier head and charger */
+	struct mutex		lock;
+	int			id;
+	enum usb_charger_type	type;
+	enum usb_charger_state	state;
+
+	/* for supporting extcon usb gpio */
+	struct extcon_dev	*extcon_dev;
+	struct usb_charger_nb	extcon_nb;
+
+	/* for supporting usb gadget */
+	struct usb_gadget	*gadget;
+	enum usb_device_state	old_gadget_state;
+
+	/* for supporting power supply */
+	struct power_supply	*psy;
+
+	/* user can get charger type by implementing this callback */
+	enum usb_charger_type	(*get_charger_type)(struct usb_charger *);
+	/*
+	 * charger detection method can be implemented if you need to
+	 * manually detect the charger type.
+	 */
+	enum usb_charger_type	(*charger_detect)(struct usb_charger *);
+
+	/* current limitation */
+	struct usb_charger_cur_limit	cur_limit;
+	/* to check if it is needed to change the SDP charger default current */
+	unsigned int		sdp_default_cur_change;
+};
Signature · 1 line
balbi
#3 lore raw

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

Hi Felipe,

Quoted 17 lines · Felipe Balbi

Hi,

Quoted 13 lines · Baolin Wang
drivers/usb/gadget/udc/charger.c new file +13 -0
+static ssize_t charger_state_show(struct device *dev,
+                               struct device_attribute *attr,
+                               char *buf)
+{
+     struct usb_charger *uchger = dev_to_uchger(dev);
+     int cnt;
+
+     switch (uchger->state) {
+     case USB_CHARGER_PRESENT:
+             cnt = sprintf(buf, "%s\n", "PRESENT");
+             break;
+     case USB_CHARGER_REMOVE:
+             cnt = sprintf(buf, "%s\n", "REMOVE");
Quoted 8 lines

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

Quoted 4 lines
drivers/usb/gadget/udc/charger.c new file +4 -0
+             break;
+     default:
+             cnt = sprintf(buf, "%s\n", "UNKNOWN");
+             break;

are these the only states we need? Don't we need at least "CHARGING" and "ERROR" or something like that? Maybe those are exposed elsewhere, dunno.

Present state means we are charging. For charging error, I think it can be exposed in power driver, which is more proper. Until now I only see PRESENT and REMOVE state are useful.

Quoted 11 lines

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

Quoted 7 lines
drivers/usb/gadget/udc/charger.c new file +6 -0
+static int __usb_charger_set_cur_limit_by_type(struct usb_charger *uchger,
+                                            enum usb_charger_type type,
+                                            unsigned int cur_limit)
+{
+     if (WARN(!uchger, "charger can not be NULL"))
+             return -EINVAL;

IIRC, I mentioned that this should assume charger to be a valid pointer. Look at this, for a second:

Quoted 1 line

You check that you have a valid pointer here...

Okay. I will remove this.

Quoted 12 lines

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

Quoted 9 lines
drivers/usb/gadget/udc/charger.c new file +8 -0
+int usb_charger_set_cur_limit_by_type(struct usb_charger *uchger,
+                                   enum usb_charger_type type,
+                                   unsigned int cur_limit)
+{
+     int ret;
+
+     if (WARN(!uchger, "charger can not be NULL"))
+             return -EINVAL;

... and here, before calling the other function. This is the only place which should check for valid uchger.

Okay.

Quoted 15 lines
Quoted 15 lines
drivers/usb/gadget/udc/charger.c new file +14 -0
+     mutex_lock(&uchger->lock);
+     ret = __usb_charger_set_cur_limit_by_type(uchger, type, cur_limit);
+     mutex_unlock(&uchger->lock);
+
+     return ret;
+}
+EXPORT_SYMBOL_GPL(usb_charger_set_cur_limit_by_type);
+
+/*
+ * usb_charger_set_cur_limit() - Set the current limitation.
+ * @uchger - the usb charger instance.
+ * @cur_limit_set - the current limitation.
+ */
+int usb_charger_set_cur_limit(struct usb_charger *uchger,
Quoted 8 lines

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

Quoted 4 lines
drivers/usb/gadget/udc/charger.c new file +4 -0
+                           struct usb_charger_cur_limit *cur_limit_set)
+{
+     if (WARN(!uchger || !cur_limit_set, "charger or setting can't be NULL"))
+             return -EINVAL;

I can see a pattern here. Not *all* errors need a splat. Sometimes a simple pr_err() or dev_err() (when you have a valid dev pointer) are enough.

Make sense.

Quoted 25 lines
Quoted 25 lines
include/linux/usb/charger.h new file +18 -0
new file mode 100644
index 0000000..d2e745e
@@ -0,0 +1,164 @@
+#ifndef __LINUX_USB_CHARGER_H__
+#define __LINUX_USB_CHARGER_H__
+
+#include <uapi/linux/usb/ch9.h>
+#include <uapi/linux/usb/charger.h>
+
+#define CHARGER_NAME_MAX 30
+
+/* Current limitation by charger type */
+struct usb_charger_cur_limit {
+     unsigned int sdp_cur_limit;
+     unsigned int dcp_cur_limit;
+     unsigned int cdp_cur_limit;
+     unsigned int aca_cur_limit;
+};
+
+struct usb_charger_nb {
+     struct notifier_block   nb;
Quoted 8 lines

include/linux/usb/charger.h line 21

Quoted 3 lines
include/linux/usb/charger.h new file +3 -0
+     struct usb_charger      *uchger;
+};
+

please add KernelDoc here. And mention the fields which aren't supposed to be accessed directly but should rely on the accessor functions. At least type and state prefer to be accessed by their respective getter/setter methods.

Will add kernel doc for struct usb_charger. Thanks for your comments.

Quoted 38 lines
Quoted 25 lines
include/linux/usb/charger.h new file +24 -0
+struct usb_charger {
+     char                    name[CHARGER_NAME_MAX];
+     struct list_head        list;
+     struct raw_notifier_head        uchger_nh;
+     /* protect the notifier head and charger */
+     struct mutex            lock;
+     int                     id;
+     enum usb_charger_type   type;
+     enum usb_charger_state  state;
+
+     /* for supporting extcon usb gpio */
+     struct extcon_dev       *extcon_dev;
+     struct usb_charger_nb   extcon_nb;
+
+     /* for supporting usb gadget */
+     struct usb_gadget       *gadget;
+     enum usb_device_state   old_gadget_state;
+
+     /* for supporting power supply */
+     struct power_supply     *psy;
+
+     /* user can get charger type by implementing this callback */
+     enum usb_charger_type   (*get_charger_type)(struct usb_charger *);
+     /*

include/linux/usb/charger.h line 55

Quoted 10 lines
include/linux/usb/charger.h new file +10 -0
+      * charger detection method can be implemented if you need to
+      * manually detect the charger type.
+      */
+     enum usb_charger_type   (*charger_detect)(struct usb_charger *);
+
+     /* current limitation */
+     struct usb_charger_cur_limit    cur_limit;
+     /* to check if it is needed to change the SDP charger default current */
+     unsigned int            sdp_default_cur_change;
+};

--
balbi

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

PATCH v14 2/4 usb: 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/core.c |   17 +++++++++++++++++
 include/linux/usb/gadget.h    |    3 +++
 2 files changed, 20 insertions(+)
drivers/usb/gadget/udc/core.c +17 -0
index ff8685e..bdf1874 100644
@@ -25,6 +25,7 @@
 #include <linux/dma-mapping.h>
 #include <linux/workqueue.h>
 
+#include <linux/usb/charger.h>
 #include <linux/usb/ch9.h>
 #include <linux/usb/gadget.h>
 #include <linux/usb.h>
@@ -578,12 +579,17 @@ EXPORT_SYMBOL_GPL(usb_gadget_vbus_connect);
  * 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.
  */
 int usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
 {
 	int ret = 0;
 
+	usb_charger_set_cur_limit_by_gadget(gadget, mA);
+
 	if (!gadget->ops->vbus_draw) {
 		ret = -EOPNOTSUPP;
 		goto out;
@@ -965,6 +971,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");
 }
@@ -1134,6 +1143,10 @@ int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
 	if (ret)
 		goto err4;
 
+	ret = usb_charger_init(gadget);
+	if (ret)
+		goto err5;
+
 	usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED);
 	udc->vbus = true;
 
@@ -1154,6 +1167,9 @@ int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
 
 	return 0;
 
+err5:
+	device_del(&udc->dev);
+
 err4:
 	list_del(&udc->list);
 	mutex_unlock(&udc_lock);
@@ -1262,6 +1278,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 +3 -0
index 612dbdf..c864b51 100644
@@ -24,6 +24,7 @@
 #include <linux/types.h>
 #include <linux/workqueue.h>
 #include <linux/usb/ch9.h>
+#include <linux/usb/charger.h>
 
 #define UDC_TRACE_STR_MAX	512
 
@@ -328,6 +329,7 @@ struct usb_gadget_ops {
  * @in_epnum: last used in ep number
  * @mA: last set mA value
  * @otg_caps: OTG capabilities of this gadget.
+ * @charger: Negotiate the power with the usb charger.
  * @sg_supported: true if we can handle scatter-gather
  * @is_otg: True if the USB device port uses a Mini-AB jack, so that the
  *	gadget driver must provide a USB OTG descriptor.
@@ -385,6 +387,7 @@ struct usb_gadget {
 	unsigned			in_epnum;
 	unsigned			mA;
 	struct usb_otg_caps		*otg_caps;
+	struct usb_charger		*charger;
 
 	unsigned			sg_supported:1;
 	unsigned			is_otg:1;
Signature · 1 line
1.7.9.5
#5 lore raw

PATCH v14 3/4 usb: 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, usb_charger_exit() function and dev_to_uchger() function by getting 'struct usb_charger' from 'struct gadget'.

---
 drivers/usb/gadget/udc/charger.c |   70 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 67 insertions(+), 3 deletions(-)
drivers/usb/gadget/udc/charger.c +67 -3
index 7abecf3..45c0bf1b 100644
@@ -35,7 +35,9 @@ static unsigned int __usb_charger_get_cur_limit(struct usb_charger *uchger);
 
 static struct usb_charger *dev_to_uchger(struct device *dev)
 {
-	return NULL;
+	struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev);
+
+	return gadget->charger;
 }
 
 /*
@@ -308,7 +310,13 @@ static int __usb_charger_set_cur_limit_by_type(struct usb_charger *uchger,
 int usb_charger_set_cur_limit_by_gadget(struct usb_gadget *gadget,
 					unsigned int cur_limit)
 {
-	return 0;
+	struct usb_charger *uchger = gadget->charger;
+
+	if (!uchger)
+		return 0;
+
+	return __usb_charger_set_cur_limit_by_type(uchger, uchger->type,
+						   cur_limit);
 }
 EXPORT_SYMBOL_GPL(usb_charger_set_cur_limit_by_gadget);
 
@@ -562,11 +570,55 @@ 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);
 
 /*
+ * usb_charger_unregister() - Unregister a usb charger.
+ * @uchger - the usb charger to be unregistered.
+ */
+static int usb_charger_unregister(struct usb_charger *uchger)
+{
+	if (WARN(!uchger, "charger can not be NULL"))
+		return -EINVAL;
+
+	ida_simple_remove(&usb_charger_ida, uchger->id);
+	sysfs_remove_groups(&uchger->gadget->dev.kobj, usb_charger_groups);
+
+	mutex_lock(&charger_lock);
+	list_del(&uchger->list);
+	mutex_unlock(&charger_lock);
+
+	kfree(uchger);
+	return 0;
+}
+
+/*
  * usb_charger_register() - Register a new usb charger.
  * @uchger - the new usb charger instance.
  */
@@ -649,6 +701,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 = USB_STATE_NOTATTACHED;
 
 	/* register a new usb charger */
@@ -671,7 +724,18 @@ 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;
+
+	ugadget->charger = NULL;
+	if (uchger->extcon_dev)
+		extcon_unregister_notifier(uchger->extcon_dev,
+					   EXTCON_USB,
+					   &uchger->extcon_nb.nb);
+
+	return usb_charger_unregister(uchger);
 }
 
 MODULE_AUTHOR("Baolin Wang <[email protected]>");
Signature · 1 line
1.7.9.5
#6 lore raw

PATCH v14 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 · 1 line
1.7.9.5