Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

power: wm831x_power: Support USB charger current limit management

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.

Signed-off-by: Mark Brown <broonie@kernel.org>
Signed-off-by: Baolin Wang <baolin.wang@linaro.org>
Acked-by: Lee Jones <lee.jones@linaro.org>
Acked-by: Charles Keepax <ckeepax@opensource.wolfsonmicro.com>
Acked-by: Sebastian Reichel <sebastian.reichel@collabora.co.uk>
Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>

authored by

Baolin Wang and committed by
Felipe Balbi
626b6cd5 a9081a00

+73
+1
Documentation/devicetree/bindings/mfd/wm831x.txt
··· 31 31 ../interrupt-controller/interrupts.txt 32 32 33 33 Optional sub-nodes: 34 + - phys : Contains a phandle to the USB PHY. 34 35 - regulators : Contains sub-nodes for each of the regulators supplied by 35 36 the device. The regulators are bound using their names listed below: 36 37
+72
drivers/power/supply/wm831x_power.c
··· 13 13 #include <linux/platform_device.h> 14 14 #include <linux/power_supply.h> 15 15 #include <linux/slab.h> 16 + #include <linux/usb/phy.h> 16 17 17 18 #include <linux/mfd/wm831x/core.h> 18 19 #include <linux/mfd/wm831x/auxadc.h> ··· 32 31 char usb_name[20]; 33 32 char battery_name[20]; 34 33 bool have_battery; 34 + struct usb_phy *usb_phy; 35 + struct notifier_block usb_notify; 35 36 }; 36 37 37 38 static int wm831x_power_check_online(struct wm831x *wm831x, int supply, ··· 127 124 POWER_SUPPLY_PROP_ONLINE, 128 125 POWER_SUPPLY_PROP_VOLTAGE_NOW, 129 126 }; 127 + 128 + /* In milliamps */ 129 + static const unsigned int wm831x_usb_limits[] = { 130 + 0, 131 + 2, 132 + 100, 133 + 500, 134 + 900, 135 + 1500, 136 + 1800, 137 + 550, 138 + }; 139 + 140 + static int wm831x_usb_limit_change(struct notifier_block *nb, 141 + unsigned long limit, void *data) 142 + { 143 + struct wm831x_power *wm831x_power = container_of(nb, 144 + struct wm831x_power, 145 + usb_notify); 146 + unsigned int i, best; 147 + 148 + /* Find the highest supported limit */ 149 + best = 0; 150 + for (i = 0; i < ARRAY_SIZE(wm831x_usb_limits); i++) { 151 + if (limit >= wm831x_usb_limits[i] && 152 + wm831x_usb_limits[best] < wm831x_usb_limits[i]) 153 + best = i; 154 + } 155 + 156 + dev_dbg(wm831x_power->wm831x->dev, 157 + "Limiting USB current to %umA", wm831x_usb_limits[best]); 158 + 159 + wm831x_set_bits(wm831x_power->wm831x, WM831X_POWER_STATE, 160 + WM831X_USB_ILIM_MASK, best); 161 + 162 + return 0; 163 + } 130 164 131 165 /********************************************************************* 132 166 * Battery properties ··· 647 607 } 648 608 } 649 609 610 + power->usb_phy = devm_usb_get_phy_by_phandle(&pdev->dev, "phys", 0); 611 + ret = PTR_ERR_OR_ZERO(power->usb_phy); 612 + 613 + switch (ret) { 614 + case 0: 615 + power->usb_notify.notifier_call = wm831x_usb_limit_change; 616 + ret = usb_register_notifier(power->usb_phy, &power->usb_notify); 617 + if (ret) { 618 + dev_err(&pdev->dev, "Failed to register notifier: %d\n", 619 + ret); 620 + goto err_bat_irq; 621 + } 622 + break; 623 + case -EINVAL: 624 + case -ENODEV: 625 + /* ignore missing usb-phy, it's optional */ 626 + power->usb_phy = NULL; 627 + ret = 0; 628 + break; 629 + default: 630 + dev_err(&pdev->dev, "Failed to find USB phy: %d\n", ret); 631 + /* fall-through */ 632 + case -EPROBE_DEFER: 633 + goto err_bat_irq; 634 + break; 635 + } 636 + 650 637 return ret; 651 638 652 639 err_bat_irq: ··· 703 636 struct wm831x_power *wm831x_power = platform_get_drvdata(pdev); 704 637 struct wm831x *wm831x = wm831x_power->wm831x; 705 638 int irq, i; 639 + 640 + if (wm831x_power->usb_phy) { 641 + usb_unregister_notifier(wm831x_power->usb_phy, 642 + &wm831x_power->usb_notify); 643 + } 706 644 707 645 for (i = 0; i < ARRAY_SIZE(wm831x_bat_irqs); i++) { 708 646 irq = wm831x_irq(wm831x,