Skip to contentSkip to conversation

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

PATCH Superseded Baolin Wang 4 participants 12 messages Aug 19, 2015 – Aug 20, 2015

Subsystems (3)

v4 of 19 Superseded Latest is v19 →

lore
Share & tools
lore
#0 lore raw

v4 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.

Providing a standard framework for doing this in the 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 v3:
- Re-order the patch and split the patch to avoid breaking build.
- Other modifications.

Baolin Wang (5):
gadget: Introduce the notifier functions
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/Makefile       |    1 +
 drivers/usb/gadget/charger.c      |  538 +++++++++++++++++++++++++++++++++++++
 drivers/usb/gadget/udc/udc-core.c |   40 +++
 include/linux/mfd/wm831x/pdata.h  |    3 +
 include/linux/usb/gadget.h        |   20 ++
 include/linux/usb/usb_charger.h   |  138 ++++++++++
 8 files changed, 816 insertions(+)
 create mode 100644 drivers/usb/gadget/charger.c
 create mode 100644 include/linux/usb/usb_charger.h
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH v4 1/5 gadget: Introduce the notifier functions

The usb charger framework is based on usb gadget. The usb charger need to be notified the state changing of usb gadget to confirm the usb charger state.

Thus this patch adds a notifier mechanism for usb gadget to report a event to usb charger when the usb gadget state is changed.

---
 drivers/usb/gadget/udc/udc-core.c |   32 ++++++++++++++++++++++++++++++++
 include/linux/usb/gadget.h        |   18 ++++++++++++++++++
 2 files changed, 50 insertions(+)
drivers/usb/gadget/udc/udc-core.c +32 -0
index f660afb..4238fc3 100644
@@ -129,6 +129,32 @@ void usb_gadget_giveback_request(struct usb_ep *ep,
 }
 EXPORT_SYMBOL_GPL(usb_gadget_giveback_request);
 
+int usb_gadget_register_notify(struct usb_gadget *gadget,
+			       struct notifier_block *nb)
+{
+	int ret;
+
+	mutex_lock(&gadget->lock);
+	ret = raw_notifier_chain_register(&gadget->nh, nb);
+	mutex_unlock(&gadget->lock);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(usb_gadget_register_notify);
+
+int usb_gadget_unregister_notify(struct usb_gadget *gadget,
+				 struct notifier_block *nb)
+{
+	int ret;
+
+	mutex_lock(&gadget->lock);
+	ret = raw_notifier_chain_unregister(&gadget->nh, nb);
+	mutex_unlock(&gadget->lock);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(usb_gadget_unregister_notify);
+
 /* ------------------------------------------------------------------------- */
 
 /**
@@ -226,6 +252,10 @@ static void usb_gadget_state_work(struct work_struct *work)
 	struct usb_gadget *gadget = work_to_gadget(work);
 	struct usb_udc *udc = gadget->udc;
 
+	mutex_lock(&gadget->lock);
+	raw_notifier_call_chain(&gadget->nh, gadget->state, gadget);
+	mutex_unlock(&gadget->lock);
+
 	if (udc)
 		sysfs_notify(&udc->dev.kobj, NULL, "state");
 }
@@ -364,6 +394,8 @@ int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
 
 	dev_set_name(&gadget->dev, "gadget");
 	INIT_WORK(&gadget->work, usb_gadget_state_work);
+	RAW_INIT_NOTIFIER_HEAD(&gadget->nh);
+	mutex_init(&gadget->lock);
 	gadget->dev.parent = parent;
 
 #ifdef	CONFIG_HAS_DMA
include/linux/usb/gadget.h +18 -0
index c14a69b..755e8bc 100644
@@ -609,6 +609,8 @@ struct usb_gadget {
 	unsigned			out_epnum;
 	unsigned			in_epnum;
 	struct usb_otg_caps		*otg_caps;
+	struct raw_notifier_head	nh;
+	struct mutex			lock;
 
 	unsigned			sg_supported:1;
 	unsigned			is_otg:1;
@@ -1183,6 +1185,22 @@ extern void usb_gadget_unmap_request(struct usb_gadget *gadget,
 
 /*-------------------------------------------------------------------------*/
 
+/**
+ * Register a notifiee to get notified by any attach status changes from
+ * the usb gadget
+ */
+int usb_gadget_register_notify(struct usb_gadget *gadget,
+			       struct notifier_block *nb);
+
+/*-------------------------------------------------------------------------*/
+
+
+/* Unregister a notifiee from the usb gadget */
+int usb_gadget_unregister_notify(struct usb_gadget *gadget,
+				 struct notifier_block *nb);
+
+/*-------------------------------------------------------------------------*/
+
 /* utility to set gadget state properly */
 
 extern void usb_gadget_set_state(struct usb_gadget *gadget,
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

PATCH v4 2/5 gadget: Introduce the usb charger framework

In reply to Baolin Wang

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.

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.

---
 drivers/usb/gadget/Kconfig      |    7 +
 drivers/usb/gadget/Makefile     |    1 +
 drivers/usb/gadget/charger.c    |  529 +++++++++++++++++++++++++++++++++++++++
 include/linux/usb/usb_charger.h |  138 ++++++++++
 4 files changed, 675 insertions(+)
 create mode 100644 drivers/usb/gadget/charger.c
 create mode 100644 include/linux/usb/usb_charger.h
drivers/usb/gadget/Kconfig +7 -0
index bcf83c0..3d2b959 100644
@@ -127,6 +127,13 @@ config USB_GADGET_STORAGE_NUM_BUFFERS
 	   a module parameter as well.
 	   If unsure, say 2.
 
+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/Makefile +1 -0
index 598a67d..1e421c1 100644
@@ -10,3 +10,4 @@ libcomposite-y			:= usbstring.o config.o epautoconf.o
 libcomposite-y			+= composite.o functions.o configfs.o u_f.o
 
 obj-$(CONFIG_USB_GADGET)	+= udc/ function/ legacy/
+obj-$(CONFIG_USB_CHARGER)	+= charger.o
drivers/usb/gadget/charger.c new file +529 -0

529 lines not on this page · raw message

include/linux/usb/usb_charger.h new file +138 -0

138 lines not on this page · raw message

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
#3 lore raw

PATCH v4 3/5 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.

Introduce a callback 'get_charger_type' which will implemented by user for usb gadget operations to get the usb charger type.

---
 drivers/usb/gadget/udc/udc-core.c |    8 ++++++++
 include/linux/usb/gadget.h        |    2 ++
 2 files changed, 10 insertions(+)
drivers/usb/gadget/udc/udc-core.c +8 -0
index 4238fc3..370376e 100644
@@ -28,6 +28,7 @@
 #include <linux/usb/ch9.h>
 #include <linux/usb/gadget.h>
 #include <linux/usb.h>
+#include <linux/usb/usb_charger.h>
 
 /**
  * struct usb_udc - describes one usb device controller
@@ -437,8 +438,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);
@@ -513,6 +520,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 +2 -0
index 755e8bc..44d82f5 100644
@@ -537,6 +537,7 @@ struct usb_gadget_ops {
 	struct usb_ep *(*match_ep)(struct usb_gadget *,
 			struct usb_endpoint_descriptor *,
 			struct usb_ss_ep_comp_descriptor *);
+	enum usb_charger_type	(*get_charger_type)(struct usb_gadget *);
 };
 
 /**
@@ -611,6 +612,7 @@ struct usb_gadget {
 	struct usb_otg_caps		*otg_caps;
 	struct raw_notifier_head	nh;
 	struct mutex			lock;
+	struct usb_charger		*uchger;
 
 	unsigned			sg_supported:1;
 	unsigned			is_otg:1;
Signature · 1 line
1.7.9.5
#4 lore raw

Re: [PATCH v4 3/5] gadget: Support for the usb charger framework

Hello.

Quoted 7 lines · Baolin Wang

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

Introduce a callback 'get_charger_type' which will implemented by user for usb gadget operations to get the usb charger type.

[...]

include/linux/usb/gadget.h struct usb_gadget_ops { · line 540

Quoted 9 lines
include/linux/usb/gadget.h +1 -0
index 755e8bc..44d82f5 100644
@@ -537,6 +537,7 @@ struct usb_gadget_ops {
  	struct usb_ep *(*match_ep)(struct usb_gadget *,
  			struct usb_endpoint_descriptor *,
  			struct usb_ss_ep_comp_descriptor *);
+	enum usb_charger_type	(*get_charger_type)(struct usb_gadget *);

^^^ please use space, not tab here

include/linux/usb/gadget.h struct usb_gadget { · line 615

Quoted 8 lines
include/linux/usb/gadget.h +1 -0
  };
 
  /**
@@ -611,6 +612,7 @@ struct usb_gadget {
  	struct usb_otg_caps		*otg_caps;
  	struct raw_notifier_head	nh;
  	struct mutex			lock;
+	struct usb_charger		*uchger;

Why not simply call the field 'charger'? :-)

[...]

WBR, Sergei

--
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

#5 lore raw

Re: [PATCH v4 3/5] gadget: Support for the usb charger framework

Quoted 14 lines · Sergei Shtylyov

Hello.

Quoted 7 lines · Baolin Wang

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

Introduce a callback 'get_charger_type' which will implemented by user for usb gadget operations to get the usb charger type.

[...]

Quoted 11 lines

include/linux/usb/gadget.h struct usb_gadget_ops { · line 540

Quoted 9 lines
include/linux/usb/gadget.h +1 -0
index 755e8bc..44d82f5 100644
@@ -537,6 +537,7 @@ struct usb_gadget_ops {
        struct usb_ep *(*match_ep)(struct usb_gadget *,
                        struct usb_endpoint_descriptor *,
                        struct usb_ss_ep_comp_descriptor *);
+       enum usb_charger_type   (*get_charger_type)(struct usb_gadget *);

^^^ please use space, not tab here

OK.

Quoted 12 lines

include/linux/usb/gadget.h struct usb_gadget { · line 615

Quoted 9 lines
include/linux/usb/gadget.h +1 -0
  };
 
  /**
@@ -611,6 +612,7 @@ struct usb_gadget {
        struct usb_otg_caps             *otg_caps;
        struct raw_notifier_head        nh;
        struct mutex                    lock;
+       struct usb_charger              *uchger;

Why not simply call the field 'charger'? :-)

I think 'uchger' is the abbreviation of 'usb charger' which is maybe a little verbous, I'll change it. Thanks for your comments.

Quoted 5 lines

[...]

WBR, Sergei

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

PATCH v4 4/5 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 should get the type by the 'get_charger_type' callback which is implemented by the usb gadget operations, and get the usb charger pointer from struct 'usb_gadget'.

---
 drivers/usb/gadget/charger.c |   13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/usb/gadget/charger.c +11 -2
index 35b46c1..93fd36f 100644
@@ -181,6 +181,13 @@ int usb_charger_unregister_notify(struct usb_charger *uchger,
 enum usb_charger_type
 usb_charger_detect_type(struct usb_charger *uchger)
 {
+	if (uchger->gadget && uchger->gadget->ops
+	    && uchger->gadget->ops->get_charger_type)
+		uchger->type =
+			uchger->gadget->ops->get_charger_type(uchger->gadget);
+	else
+		uchger->type = UNKNOWN_TYPE;
+
 	return uchger->type;
 }
 
@@ -313,7 +320,8 @@ static int
 usb_charger_plug_by_gadget(struct notifier_block *nb,
 			   unsigned long state, void *data)
 {
-	struct usb_charger *uchger = NULL;
+	struct usb_gadget *gadget = (struct usb_gadget *)data;
+	struct usb_charger *uchger = gadget->uchger;
 	enum usb_charger_state uchger_state;
 
 	if (!uchger)
@@ -480,6 +488,7 @@ int usb_charger_init(struct usb_gadget *ugadget)
 
 	/* register a notifier on a usb gadget device */
 	uchger->gadget = ugadget;
+	ugadget->uchger = uchger;
 	uchger->old_gadget_state = ugadget->state;
 	uchger->gadget_nb.notifier_call = usb_charger_plug_by_gadget;
 	usb_gadget_register_notify(ugadget, &uchger->gadget_nb);
@@ -503,7 +512,7 @@ fail:
 
 int usb_charger_exit(struct usb_gadget *ugadget)
 {
-	struct usb_charger *uchger = NULL;
+	struct usb_charger *uchger = ugadget->uchger;
 
 	if (!uchger)
 		return -EINVAL;
Signature · 1 line
1.7.9.5
#7 lore raw

PATCH v4 5/5 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 db11ae6..72c661f 100644
@@ -13,6 +13,7 @@
 #include <linux/platform_device.h>
 #include <linux/power_supply.h>
 #include <linux/slab.h>
+#include <linux/usb/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 miliamps */
+static 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);
+	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 %dmA", wm831x_usb_limits[best]);
+
+	wm831x_set_bits(wm831x_power->wm831x, WM831X_POWER_STATE,
+		        WM831X_USB_ILIM_MASK, best);
+
+	return 0;
+}
+
 /*********************************************************************
  *		Battery properties
  *********************************************************************/
@@ -606,8 +646,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:
+	usb_charger_put(power->usb_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);
+		usb_charger_put(wm831x_power->usb_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
#8 lore raw

Re: [PATCH v4 5/5] power: wm831x_power: Support USB charger current limit management

Quoted 10 lines · 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 ++

I'm not going to keep reviewing this. You should carry forward any Acks received onto subsequent submissions yourself.

Quoted 126 lines
 2 files changed, 72 insertions(+)
drivers/power/wm831x_power.c +69 -0
index db11ae6..72c661f 100644
@@ -13,6 +13,7 @@
 #include <linux/platform_device.h>
 #include <linux/power_supply.h>
 #include <linux/slab.h>
+#include <linux/usb/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 miliamps */
+static 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);
+	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 %dmA", wm831x_usb_limits[best]);
+
+	wm831x_set_bits(wm831x_power->wm831x, WM831X_POWER_STATE,
+		        WM831X_USB_ILIM_MASK, best);
+
+	return 0;
+}
+
 /*********************************************************************
  *		Battery properties
  *********************************************************************/
@@ -606,8 +646,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:
+	usb_charger_put(power->usb_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);
+		usb_charger_put(wm831x_power->usb_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 · 4 lines
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
#9 lore raw

Re: [PATCH v4 5/5] power: wm831x_power: Support USB charger current limit management

Quoted 7 lines · Lee Jones
Quoted 5 lines · 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.

Quoted 9 lines
Quoted 5 lines
---
 drivers/power/wm831x_power.c     |   69 ++++++++++++++++++++++++++++++++++++++
 include/linux/mfd/wm831x/pdata.h |    3 ++

I'm not going to keep reviewing this. You should carry forward any Acks received onto subsequent submissions yourself.

OK, thanks.

Quoted 132 lines
Quoted 117 lines
 2 files changed, 72 insertions(+)
drivers/power/wm831x_power.c +69 -0
index db11ae6..72c661f 100644
@@ -13,6 +13,7 @@
 #include <linux/platform_device.h>
 #include <linux/power_supply.h>
 #include <linux/slab.h>
+#include <linux/usb/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 miliamps */
+static 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);
+     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 %dmA", wm831x_usb_limits[best]);
+
+     wm831x_set_bits(wm831x_power->wm831x, WM831X_POWER_STATE,
+                     WM831X_USB_ILIM_MASK, best);
+
+     return 0;
+}
+
 /*********************************************************************
  *           Battery properties
  *********************************************************************/
@@ -606,8 +646,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:
+     usb_charger_put(power->usb_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);
+             usb_charger_put(wm831x_power->usb_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 +0 -0
index dcc9631..5af8399 100644

include/linux/mfd/wm831x/pdata.h struct wm831x_pdata { · line 134

Quoted 9 lines
include/linux/mfd/wm831x/pdata.h +3 -0
@@ -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];

--
Lee Jones
Linaro STMicroelectronics Landing Team Lead

Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
Signature · 6 lines
Baolin.wang
Best Regards
--
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
#10 lore raw

Re: [PATCH v4 5/5] power: wm831x_power: Support USB charger current limit management

In reply to Baolin Wang
Quoted 7 lines · 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.

When people (like Charles and Lee have) have reviewed a change you should add any tags they gave when you resend the change so they don't have to duplicate their work and other people know that the review has happened.

#11 lore raw

Re: [PATCH v4 5/5] power: wm831x_power: Support USB charger current limit management

Quoted 6 lines · Mark Brown
Quoted 5 lines · 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.

Quoted 7 lines
Quoted 2 lines

When people (like Charles and Lee have) have reviewed a change you should add any tags they gave when you resend the change so they don't have to duplicate their work and other people know that the review has happened.

Make sense. Thanks.

Signature · 6 lines
Baolin.wang
Best Regards
--
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