From patchwork Mon Feb 20 08:51:10 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: 94183 Delivered-To: patch@linaro.org Received: by 10.182.3.34 with SMTP id 2csp1241739obz; Mon, 20 Feb 2017 00:52:22 -0800 (PST) X-Received: by 10.99.102.134 with SMTP id a128mr25882845pgc.215.1487580742835; Mon, 20 Feb 2017 00:52:22 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 78si5743860pgh.202.2017.02.20.00.52.22; Mon, 20 Feb 2017 00:52:22 -0800 (PST) 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; 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 S1752485AbdBTIwL (ORCPT + 25 others); Mon, 20 Feb 2017 03:52:11 -0500 Received: from mail-pg0-f44.google.com ([74.125.83.44]:36571 "EHLO mail-pg0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752444AbdBTIwH (ORCPT ); Mon, 20 Feb 2017 03:52:07 -0500 Received: by mail-pg0-f44.google.com with SMTP id s67so5487991pgb.3 for ; Mon, 20 Feb 2017 00:52:07 -0800 (PST) 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=mgliuD5EwJS3YvKgMaOoykCsMSpE4SnK2/UH/Ezf1FQ=; b=LDc5BC31x0EPYMMtSlcakhNo5PB/9Z1Rgpme3yrb97a5ahrzdsDT/fHDOMlJ5YB8lO KXmYRpTwWSM8U/cEBOM+QBN1zFL8MlAbQdbQoUoPiY/paquU5LZj+eaUmXpaKD7ST1Vi iqgkgwEgdEuXvTmhHTDKYCLDKNTBxr6AfCsb4= 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=mgliuD5EwJS3YvKgMaOoykCsMSpE4SnK2/UH/Ezf1FQ=; b=fHA2uI7PxKJjksNEz4EzqVCdNsOtMLkBOAExn9tf/GhbVtNx/cDBwY5jNBvL2+FFBH rFPG59jOiLyYPxsOVp5SunacWNOGbgtgE8UCr1RczSXiUcuyzQFAXfq1qKUCKK3Im393 9mkmiwTM2xmdZeQoDKGQfinWxjUOO462euOlTiphPDKbhKE05dk5IMRvJSWQ1jQmZdj1 SG3VFR5gyh+XR6ov41XClUHPtnnsNwxZNtQfMkX+lT2ByC7Xvlmg+1Z+YTgzjvJgMdI+ YwM+GPSI8fRLWaBDvLQsM3BkdidRM5m00qzKe14HU2KM/F3RFwvq3qcQ+3Ag4F0Uk4xz bIKg== X-Gm-Message-State: AMke39mgOuZI0vEaoY8dkLEu50T6Y7wKbvISkSgR/ESzfuevzOZE3Zr6DTv14tQGoz7RY+h0 X-Received: by 10.98.216.202 with SMTP id e193mr5712862pfg.80.1487580726624; Mon, 20 Feb 2017 00:52:06 -0800 (PST) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id t184sm5102967pgb.11.2017.02.20.00.51.59 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 20 Feb 2017 00:52:06 -0800 (PST) From: Baolin Wang To: balbi@kernel.org, gregkh@linuxfoundation.org, sre@kernel.org, dbaryshkov@gmail.com, dwmw2@infradead.org Cc: robh@kernel.org, jun.li@nxp.com, m.szyprowski@samsung.com, ruslan.bilovol@gmail.com, peter.chen@freescale.com, stern@rowland.harvard.edu, grygorii.strashko@ti.com, yoshihiro.shimoda.uh@renesas.com, lee.jones@linaro.org, broonie@kernel.org, john.stultz@linaro.org, neilb@suse.com, ckeepax@opensource.wolfsonmicro.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 v19 4/4] power: wm831x_power: Support USB charger current limit management Date: Mon, 20 Feb 2017 16:51:10 +0800 Message-Id: X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: 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 Acked-by: Lee Jones Acked-by: Charles Keepax Acked-by: Peter Chen Acked-by: Sebastian Reichel --- drivers/power/supply/wm831x_power.c | 63 +++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) -- 1.7.9.5 diff --git a/drivers/power/supply/wm831x_power.c b/drivers/power/supply/wm831x_power.c index 7082301..b5f1652 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_charger *usb_charger; + struct notifier_block usb_notify; }; static int wm831x_power_check_online(struct wm831x *wm831x, int supply, @@ -125,6 +128,42 @@ 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); + + /* 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 +646,26 @@ static int wm831x_power_probe(struct platform_device *pdev) } } + power->usb_charger = usb_charger_get_instance(); + 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 +694,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,