From patchwork Fri Dec 9 11:04:11 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Schulz X-Patchwork-Id: 9467919 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 61D02607D8 for ; Fri, 9 Dec 2016 11:07:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 547EC284EC for ; Fri, 9 Dec 2016 11:07:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 48F85285B6; Fri, 9 Dec 2016 11:07:47 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id E5ED6284EC for ; Fri, 9 Dec 2016 11:07:46 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.85_2 #1 (Red Hat Linux)) id 1cFJ09-0004jp-B7; Fri, 09 Dec 2016 11:06:05 +0000 Received: from mail.free-electrons.com ([62.4.15.54]) by bombadil.infradead.org with esmtp (Exim 4.85_2 #1 (Red Hat Linux)) id 1cFIyu-0002ts-FS for linux-arm-kernel@lists.infradead.org; Fri, 09 Dec 2016 11:04:51 +0000 Received: by mail.free-electrons.com (Postfix, from userid 110) id 503A720C11; Fri, 9 Dec 2016 12:04:28 +0100 (CET) Received: from qschulz.home (LStLambert-657-1-97-87.w90-63.abo.wanadoo.fr [90.63.216.87]) by mail.free-electrons.com (Postfix) with ESMTPSA id 02BA2208D9; Fri, 9 Dec 2016 12:04:27 +0100 (CET) From: Quentin Schulz To: sre@kernel.org, robh+dt@kernel.org, mark.rutland@arm.com, wens@csie.org, linux@armlinux.org.uk, maxime.ripard@free-electrons.com, lee.jones@linaro.org Subject: [PATCH v2 03/11] power: supply: axp20x_usb_power: set min voltage and max current from sysfs Date: Fri, 9 Dec 2016 12:04:11 +0100 Message-Id: <20161209110419.28981-4-quentin.schulz@free-electrons.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20161209110419.28981-1-quentin.schulz@free-electrons.com> References: <20161209110419.28981-1-quentin.schulz@free-electrons.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20161209_030449_038468_A5426EE4 X-CRM114-Status: GOOD ( 12.71 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: thomas.petazzoni@free-electrons.com, devicetree@vger.kernel.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Quentin Schulz , linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP AXP20X and AXP22X PMICs allow setting the min voltage and max current of VBUS power supply. This adds entries in sysfs to allow to do so. Signed-off-by: Quentin Schulz Acked-by: Chen-Yu Tsai --- v2: - moving defines from header mfd/axp20x.h to the driver, - adding two functions to reduce indentation in axp20x_usb_power_set_property, - adding new define for VBUS register VHOLD bits offset, - switching return values in case of invalid value from 0 to -EINVAL, drivers/power/supply/axp20x_usb_power.c | 81 +++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/drivers/power/supply/axp20x_usb_power.c b/drivers/power/supply/axp20x_usb_power.c index 8985646..9e3f4ae 100644 --- a/drivers/power/supply/axp20x_usb_power.c +++ b/drivers/power/supply/axp20x_usb_power.c @@ -31,6 +31,8 @@ #define AXP20X_USB_STATUS_VBUS_VALID BIT(2) #define AXP20X_VBUS_VHOLD_uV(b) (4000000 + (((b) >> 3) & 7) * 100000) +#define AXP20X_VBUS_VHOLD_MASK GENMASK(5, 3) +#define AXP20X_VBUS_VHOLD_OFFSET 3 #define AXP20X_VBUS_CLIMIT_MASK 3 #define AXP20X_VBUC_CLIMIT_900mA 0 #define AXP20X_VBUC_CLIMIT_500mA 1 @@ -155,6 +157,81 @@ static int axp20x_usb_power_get_property(struct power_supply *psy, return 0; } +static int axp20x_usb_power_set_voltage_min(struct axp20x_usb_power *power, + int intval) +{ + int val; + + switch (intval) { + case 4000000: + case 4100000: + case 4200000: + case 4300000: + case 4400000: + case 4500000: + case 4600000: + case 4700000: + val = (intval - 4000000) / 100000; + return regmap_update_bits(power->regmap, + AXP20X_VBUS_IPSOUT_MGMT, + AXP20X_VBUS_VHOLD_MASK, + val << AXP20X_VBUS_VHOLD_OFFSET); + default: + return -EINVAL; + } + + return -EINVAL; +} + +static int axp20x_usb_power_set_current_max(struct axp20x_usb_power *power, + int intval) +{ + int val; + + switch (intval) { + case 100000: + if (power->axp20x_id == AXP221_ID) + return -EINVAL; + case 500000: + case 900000: + val = (900000 - intval) / 400000; + return regmap_update_bits(power->regmap, + AXP20X_VBUS_IPSOUT_MGMT, + AXP20X_VBUS_CLIMIT_MASK, val); + default: + return -EINVAL; + } + + return -EINVAL; +} + +static int axp20x_usb_power_set_property(struct power_supply *psy, + enum power_supply_property psp, + const union power_supply_propval *val) +{ + struct axp20x_usb_power *power = power_supply_get_drvdata(psy); + + switch (psp) { + case POWER_SUPPLY_PROP_VOLTAGE_MIN: + return axp20x_usb_power_set_voltage_min(power, val->intval); + + case POWER_SUPPLY_PROP_CURRENT_MAX: + return axp20x_usb_power_set_current_max(power, val->intval); + + default: + return -EINVAL; + } + + return -EINVAL; +} + +static int axp20x_usb_power_prop_writeable(struct power_supply *psy, + enum power_supply_property psp) +{ + return psp == POWER_SUPPLY_PROP_VOLTAGE_MIN || + psp == POWER_SUPPLY_PROP_CURRENT_MAX; +} + static enum power_supply_property axp20x_usb_power_properties[] = { POWER_SUPPLY_PROP_HEALTH, POWER_SUPPLY_PROP_PRESENT, @@ -178,7 +255,9 @@ static const struct power_supply_desc axp20x_usb_power_desc = { .type = POWER_SUPPLY_TYPE_USB, .properties = axp20x_usb_power_properties, .num_properties = ARRAY_SIZE(axp20x_usb_power_properties), + .property_is_writeable = axp20x_usb_power_prop_writeable, .get_property = axp20x_usb_power_get_property, + .set_property = axp20x_usb_power_set_property, }; static const struct power_supply_desc axp22x_usb_power_desc = { @@ -186,7 +265,9 @@ static const struct power_supply_desc axp22x_usb_power_desc = { .type = POWER_SUPPLY_TYPE_USB, .properties = axp22x_usb_power_properties, .num_properties = ARRAY_SIZE(axp22x_usb_power_properties), + .property_is_writeable = axp20x_usb_power_prop_writeable, .get_property = axp20x_usb_power_get_property, + .set_property = axp20x_usb_power_set_property, }; static int axp20x_usb_power_probe(struct platform_device *pdev)