From patchwork Mon Jul 3 06:07:15 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "\(Exiting\) Baolin Wang" X-Patchwork-Id: 106877 Delivered-To: patch@linaro.org Received: by 10.140.101.44 with SMTP id t41csp5397851qge; Sun, 2 Jul 2017 23:08:18 -0700 (PDT) X-Received: by 10.98.64.7 with SMTP id n7mr8090503pfa.76.1499062098699; Sun, 02 Jul 2017 23:08:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1499062098; cv=none; d=google.com; s=arc-20160816; b=pNsvvUzWkML2/CsMmtsp0M+SHC9uNXQqOSN7DIEV1cfh316qbBIw8B0almyUTS4usB S7O9eUbRodNLt8hqKAW7xRuUYs1z7ABsCnHvGzkei5Ti7uwaQx3QeDQqU0tpVBzIFwvE Q80Crb1WNwtqWOykvEul4lZQN8uG1NEdZa4HLNcYZbMSoClXBXlCl7G/63Kgqs2p9JBJ hbHEn8/ZmprfjFW6X7Fk9HCJE/E4F1d5iv0gBLGJSisPLQWKzF7f4dRya/CIQNz73XO+ T7WI6os/0m7k5gl48S1pZ7OswVP+TPUnjVfw4LkW0x3ZcFJzBjNNR1qjaru3CNAypLKg aAvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=bM7WrtNbs4vogs+DPbfB6XUbFxqjdN8sBfbv/yRf3W8=; b=tJGilAhgW67S57zbRmx7jqLtpd4xbBsTFtyW3NKzQnq9Z6dMzresHcWkHCPm+zj+T/ /Ehu7yzoc6JgbjsmdxeR/2iHjt7zGkTUHzRHsn573KacSjwF0uAXUyTn76Yh0FlTqYga Qg3hBTnQ9wW1+L3LKExlrVao7svgp7aZCHUEfh0A0GN2gqEhtE/6j2aeGlzD9AxUUhzP Tszu4hJFqTu9dHD6tm5CiSt0+o0qhrF41HiFs13E6vZPFDIrwDI0w4pyMz0rIfWdoVka zYQowQ9q5+TdSDlwjSLr2FCUzOxgavvyC/9zjLj+HFdgtKDdfwi71A+GD2yeQVEKD53C oYgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.b=MC/3bQFs; spf=pass (google.com: best guess record for domain of linux-usb-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-usb-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r7si12403050ple.505.2017.07.02.23.08.18; Sun, 02 Jul 2017 23:08:18 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-usb-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.b=MC/3bQFs; spf=pass (google.com: best guess record for domain of linux-usb-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-usb-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752296AbdGCGIQ (ORCPT + 4 others); Mon, 3 Jul 2017 02:08:16 -0400 Received: from mail-pg0-f53.google.com ([74.125.83.53]:32861 "EHLO mail-pg0-f53.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752073AbdGCGIO (ORCPT ); Mon, 3 Jul 2017 02:08:14 -0400 Received: by mail-pg0-f53.google.com with SMTP id k14so14189751pgr.0 for ; Sun, 02 Jul 2017 23:08:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=g2MtqLNe5Zrv7Rq/SA/5+RCNBw5JP4jhnMkGdIR/TKI=; b=MC/3bQFstOP9W21eo9nZg15W2r/ezMTFDu2sOg55LceurnIgX9+76P7yxWsaIlzRKT 2FP4pHoF3C4JSYLYcLs8S712YppYXmp8V5wYr4bYE7xzrtE85jen9qsmf4J0JcL+oxZI 5DzFOMRZn6xqJ0xK35XVs6xWg5bqM2w85Tws4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=g2MtqLNe5Zrv7Rq/SA/5+RCNBw5JP4jhnMkGdIR/TKI=; b=r8rNzZFtCWHFW7N8u9iMI9/VPnL/dlTASMZHOilAv2RVSxOiZ/8o7JjbrM7hs1Dqmo v+MQUR2C0svNN1s8Mvg0sq536AUJM3zOUcIefEtZ8PRBCE7Y7CmOWXHTTCR6lt3rc4o0 GpMuFNsPxs0TswaFj4vwVCyyFHmfjqQf7VACToMVQT/dbix1uYL0ugta9OXkvGLDCbgi Q5Xd1UFfkopcDy9hRuJQH1Q62l3rlu4FFnvHtiNi9T1OQ8u2NI7IU+EUOEOo+GCSjirC Ja0xWINWXvHhVCCx7yXooKRtgCCWycRwlnCKjDO9vfugexKMVhIyOOpMsA+G7i0LGc+L e/og== X-Gm-Message-State: AIVw112me5swbervcJnGswOx2+5LsCFPBwE62SnetwfJy+2kFmuYsJqk /mCpF0L/uXAPakhy X-Received: by 10.84.232.207 with SMTP id x15mr9107809plm.173.1499062088806; Sun, 02 Jul 2017 23:08:08 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id x64sm33177577pfk.20.2017.07.02.23.08.04 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 02 Jul 2017 23:08:08 -0700 (PDT) From: Baolin Wang To: balbi@kernel.org, gregkh@linuxfoundation.org, sre@kernel.org Cc: jun.li@nxp.com, peter.chen@freescale.com, broonie@kernel.org, john.stultz@linaro.org, neilb@suse.com, patches@opensource.wolfsonmicro.com, baolin.wang@linaro.org, linux-pm@vger.kernel.org, linux-usb@vger.kernel.org, device-mainlining@lists.linuxfoundation.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 3/3] power: wm831x_power: Support USB charger current limit management Date: Mon, 3 Jul 2017 14:07:15 +0800 Message-Id: X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: In-Reply-To: References: Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org 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 Signed-off-by: Baolin Wang --- drivers/power/supply/wm831x_power.c | 61 +++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) -- 1.7.9.5 -- To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/power/supply/wm831x_power.c b/drivers/power/supply/wm831x_power.c index 7082301..3e3480708 100644 --- a/drivers/power/supply/wm831x_power.c +++ b/drivers/power/supply/wm831x_power.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -31,6 +32,8 @@ struct wm831x_power { char usb_name[20]; char battery_name[20]; bool have_battery; + struct usb_phy *usb_phy; + struct notifier_block usb_notify; }; static int wm831x_power_check_online(struct wm831x *wm831x, int supply, @@ -125,6 +128,43 @@ static int wm831x_usb_get_prop(struct power_supply *psy, 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,6 +647,22 @@ static int wm831x_power_probe(struct platform_device *pdev) } } + power->usb_phy = devm_usb_get_phy_by_phandle(&pdev->dev, + "usb-phy", 0); + if (IS_ERR(power->usb_phy)) { + ret = PTR_ERR(power->usb_phy); + dev_err(&pdev->dev, "Failed to find USB phy: %d\n", ret); + goto err_bat_irq; + } + + power->usb_notify.notifier_call = wm831x_usb_limit_change; + ret = usb_register_notifier(power->usb_phy, + &power->usb_notify); + if (ret) { + dev_err(&pdev->dev, "Failed to register notifier: %d\n", ret); + goto err_bat_irq; + } + return ret; err_bat_irq: @@ -637,6 +693,11 @@ static int wm831x_power_remove(struct platform_device *pdev) struct wm831x *wm831x = wm831x_power->wm831x; int irq, i; + if (wm831x_power->usb_phy) { + usb_unregister_notifier(wm831x_power->usb_phy, + &wm831x_power->usb_notify); + } + for (i = 0; i < ARRAY_SIZE(wm831x_bat_irqs); i++) { irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev,