From patchwork Tue Jun 27 07:49:35 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: 106404 Delivered-To: patch@linaro.org Received: by 10.140.101.48 with SMTP id t45csp923102qge; Tue, 27 Jun 2017 00:50:30 -0700 (PDT) X-Received: by 10.98.1.72 with SMTP id 69mr3983602pfb.124.1498549830851; Tue, 27 Jun 2017 00:50:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1498549830; cv=none; d=google.com; s=arc-20160816; b=buipYzQc7GybUhpDYrR9lyCx36tTD37u5vZ4dPW8dlF6kgap4LPL8OBlFeX8RBCLuR Sup0syyKUzHJdOlxWn10huLS1MXxqzNQGVtCshVvLwHvG6XXA8zjKRrZrznivQ7AXxSD Ygz6Lh6DIivoQ8bUQaZlRZUGmUCbuAs5e/hNk6DjXMpYeo8u1ni2iSV5iag2VjhjL/i8 QRUtJAtjFSf0vqucYY6tnwOI5asRAs/3E7kR6nk3ceXVsIxda+SkwB+ZHt0M2JqP3Kkg X+PVQqrbBOlY8rNacs1OHmSAtlzfxKGOi9xRGmseRD/Y4OBQvg4H468mbBdchrMdfEgd f+4A== 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=g2MtqLNe5Zrv7Rq/SA/5+RCNBw5JP4jhnMkGdIR/TKI=; b=etSw98BJHWKGBryfGm8X8Mub+ee8n2uy3CZyPV3PnJIHsXi3A9nCCx1ywzaiaSakc7 q/Yk2xIClP59ixsEupoY8Gd/6vxnVATjBKlgmOlyCmIe5PJuxlEcyCqoD3A/DhX5qzg9 ASUtcj55TH7aD28n4vjkbz74IeRGr68fZVbKGwbeAGpUFY5EIVDMWe2jJWqhguSoL+uW 5ZnArlsJZdj86jaEypc7tkcDDVTHryR2650tV0RbSuZZBYWdevoo3DtdeMA12z4VOVGe N28hqnIzAqB4zS4EgCDpR0gE9CGwvb7WOwOy++uE/FtPn70OCcj/nocDdSitgqwHMFt7 HnRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.b=ZyN86H0R; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (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 98si1601344plt.318.2017.06.27.00.50.30; Tue, 27 Jun 2017 00:50:30 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.b=ZyN86H0R; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752451AbdF0HuN (ORCPT + 25 others); Tue, 27 Jun 2017 03:50:13 -0400 Received: from mail-pf0-f178.google.com ([209.85.192.178]:32816 "EHLO mail-pf0-f178.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752360AbdF0HuI (ORCPT ); Tue, 27 Jun 2017 03:50:08 -0400 Received: by mail-pf0-f178.google.com with SMTP id e7so12618144pfk.0 for ; Tue, 27 Jun 2017 00:50:07 -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=ZyN86H0RmzwIm7poMM2eSZJcyshd1D9/1EohqeasoUBvPCluRynkTnD5CIITC7zNL/ xNbWpZQCdQYNnd4NtefvBIjW01LjvmMLmPsEoxlFW5+eMVeNdLWMeEr5C0doOzg4sqWg w/w7TKG33cBuxsQ730vZkfvN9Ar1XYWeywUOM= 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=mDArPfwljz5P510XXh1NoEDrfH4vGdRJ3O8VSqRVueMCg85LchE207/fTvo0Dzv5/u E4SJIz1dVdGyA5ykK0ysJoe5TQlXByp6itL4WbDExgnQIxtbZA3EKLv0FsoOyabWunpl rtLRYuR3KrUg95gzi8nEEphlom33Em6KlbAPI/G3e5zD4s0+pW23wKCT5PVpiNlapWFF hEI/g8Gk+zCsnbH3Pnp2Q2e8UwpQnuBQ2dDOOWsrmArnQtg3NU0jUZZ1XxBng5DdcGJy yj9RCdmxHDokugJAjKwktzARsiwUtr/LOGmxoLByKmf6bCqGAK6+SO8DKACLm254DgwO /AJw== X-Gm-Message-State: AKS2vOxboSrfsF3gHJYKkSyhKnmy/pzDcYqB4IoGQpiJKLYcSpRufPCu eAyrC2NxI1y+7Qlf X-Received: by 10.98.152.208 with SMTP id d77mr1198052pfk.210.1498549807323; Tue, 27 Jun 2017 00:50:07 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id b85sm4634961pfe.94.2017.06.27.00.50.03 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 27 Jun 2017 00:50:06 -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 3/3] power: wm831x_power: Support USB charger current limit management Date: Tue, 27 Jun 2017 15:49:35 +0800 Message-Id: <86f990c6212d1d90a36e937736c01a7b131f6fcb.1498548022.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <54d4a3ae584900a74e04b5cd27157dced44fdfef.1498548022.git.baolin.wang@linaro.org> References: <54d4a3ae584900a74e04b5cd27157dced44fdfef.1498548022.git.baolin.wang@linaro.org> In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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 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,