Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp1866874pxp; Thu, 17 Mar 2022 19:47:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy9FTkA9nTEbohs4w6PFKkeBdNPemiYfRtNoakspnyGFc7lrHcsrLMqkxXEVSqiVvmHRmpt X-Received: by 2002:a17:906:3014:b0:6da:f381:4dfe with SMTP id 20-20020a170906301400b006daf3814dfemr6892177ejz.670.1647571675215; Thu, 17 Mar 2022 19:47:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647571675; cv=none; d=google.com; s=arc-20160816; b=cP6J0bkG2/5/ef2aTaRTSfmJzS3e7YNzbq/12RSmEx19csgf8Z9wynP9jbjGrOJVZM 1Jt9668V+2HON1KsJvhAmp4Fs4l1dlvxFvoVk2LE3uJbT1E86vMmgircWc8mxrc6OLX7 UhdUOC48wynB/Xj2xVRCzD6xKrRFUbL6CvUQ6L1o+nwXuNwi0mGaOv1f5oLNfjVlcnzG YFDByEWKVX2d/Nx1Rti23KMFLzqOSeWww4QVxkr4CEDHCDw8h/v24gij5Hd/xMRFDE1A Ozc8K3BdjCANP9JJqUzrg6Ysdqj3woFa7Q2zCoJqNtMrc+glvLXE5kfD9bdfE5sOGWGB gkWw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=4ZkBQz8NiheDnJvOvv9PQ4skiw1S3vggtDMERPJ1pzM=; b=LFTwFWbmo2WcxpEzVQWC0a4rFf8dr0nbeT5hrTmYvI9InGlRvhw0J2+DKY5xQ3VFUs vnqPXZPAkHdLFQgs3EPodujSKE3W55y7gPAgfwQNNWXMwv0Mu5vCh2PmVdbJ+9Z98Oj2 XoZNQ0RqLK+3nGLbtxpk3TmSu8MgVNJ8cJx6jZ0Mo2omPVNe3JmN61nXwGkxKsRMWFuZ NFOzMaJQb0H65+zJmDKUIgaErEztZlOjo34BF1MALWrBdqZHuDaerHsVCUtYyWupQ9s+ zCsbNZRQu+bfelBQRF9WRyz9SWROuF7ASuSO339zT2V4Zx9uVpvUq/gA5bsB3muA/YYP 76NQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=puri.sm Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v7-20020a50d087000000b00418c2b5bf1asi3725186edd.508.2022.03.17.19.47.29; Thu, 17 Mar 2022 19:47:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=puri.sm Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230412AbiCRAM3 (ORCPT + 99 others); Thu, 17 Mar 2022 20:12:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229688AbiCRAM2 (ORCPT ); Thu, 17 Mar 2022 20:12:28 -0400 Received: from comms.puri.sm (comms.puri.sm [159.203.221.185]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03B21E29CE; Thu, 17 Mar 2022 17:11:07 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by comms.puri.sm (Postfix) with ESMTP id 5B5F6E0063; Thu, 17 Mar 2022 17:11:07 -0700 (PDT) Received: from comms.puri.sm ([127.0.0.1]) by localhost (comms.puri.sm [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id pzRlI9UiGHba; Thu, 17 Mar 2022 17:11:06 -0700 (PDT) From: Sebastian Krzyszkowiak To: Hans de Goede , Krzysztof Kozlowski , Marek Szyprowski , Sebastian Reichel , linux-pm@vger.kernel.org Cc: Sebastian Krzyszkowiak , Purism Kernel Team , Rob Herring , linux-kernel@vger.kernel.org, devicetree@vger.kernel.org Subject: [PATCH 1/4] power: supply: max17042_battery: Add unit conversion macros Date: Fri, 18 Mar 2022 01:10:45 +0100 Message-Id: <20220318001048.20922-2-sebastian.krzyszkowiak@puri.sm> In-Reply-To: <20220318001048.20922-1-sebastian.krzyszkowiak@puri.sm> References: <20220318001048.20922-1-sebastian.krzyszkowiak@puri.sm> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Instead of sprinkling the code with magic numbers, put the unit definitions used by the gauge into a set of macros. Macros are used instead of simple defines in order to not require floating point operations for divisions. Signed-off-by: Sebastian Krzyszkowiak --- drivers/power/supply/max17042_battery.c | 40 +++++++++++++++---------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/drivers/power/supply/max17042_battery.c b/drivers/power/supply/max17042_battery.c index ab031bbfbe78..c019d6c52363 100644 --- a/drivers/power/supply/max17042_battery.c +++ b/drivers/power/supply/max17042_battery.c @@ -51,6 +51,15 @@ #define MAX17042_VMAX_TOLERANCE 50 /* 50 mV */ +#define MAX17042_CURRENT_LSB 1562500ll /* µV */ +#define MAX17042_CURRENT_RSENSE(x) (x * MAX17042_CURRENT_LSB) /* µV */ +#define MAX17042_CAPACITY_LSB 5000000ll /* µVh */ +#define MAX17042_CAPACITY_RSENSE(x) (x * MAX17042_CAPACITY_LSB) /* µVh */ +#define MAX17042_TIME(x) (x * 5625 / 1000) /* s */ +#define MAX17042_VOLTAGE(x) (x * 625 / 8) /* µV */ +#define MAX17042_RESISTANCE(x) (x / 4096) /* Ω */ +#define MAX17042_TEMPERATURE(x) (x / 256) /* °C */ + struct max17042_chip { struct i2c_client *client; struct regmap *regmap; @@ -103,8 +112,7 @@ static int max17042_get_temperature(struct max17042_chip *chip, int *temp) *temp = sign_extend32(data, 15); /* The value is converted into deci-centigrade scale */ - /* Units of LSB = 1 / 256 degree Celsius */ - *temp = *temp * 10 / 256; + *temp = MAX17042_TEMPERATURE(*temp * 10); return 0; } @@ -161,7 +169,7 @@ static int max17042_get_status(struct max17042_chip *chip, int *status) return ret; avg_current = sign_extend32(data, 15); - avg_current *= 1562500 / chip->pdata->r_sns; + avg_current *= MAX17042_CURRENT_LSB / chip->pdata->r_sns; if (avg_current > 0) *status = POWER_SUPPLY_STATUS_CHARGING; @@ -181,7 +189,7 @@ static int max17042_get_battery_health(struct max17042_chip *chip, int *health) goto health_error; /* bits [0-3] unused */ - vavg = val * 625 / 8; + vavg = MAX17042_VOLTAGE(val); /* Convert to millivolts */ vavg /= 1000; @@ -190,7 +198,7 @@ static int max17042_get_battery_health(struct max17042_chip *chip, int *health) goto health_error; /* bits [0-3] unused */ - vbatt = val * 625 / 8; + vbatt = MAX17042_VOLTAGE(val); /* Convert to millivolts */ vbatt /= 1000; @@ -297,21 +305,21 @@ static int max17042_get_property(struct power_supply *psy, if (ret < 0) return ret; - val->intval = data * 625 / 8; + val->intval = MAX17042_VOLTAGE(data); break; case POWER_SUPPLY_PROP_VOLTAGE_AVG: ret = regmap_read(map, MAX17042_AvgVCELL, &data); if (ret < 0) return ret; - val->intval = data * 625 / 8; + val->intval = MAX17042_VOLTAGE(data); break; case POWER_SUPPLY_PROP_VOLTAGE_OCV: ret = regmap_read(map, MAX17042_OCVInternal, &data); if (ret < 0) return ret; - val->intval = data * 625 / 8; + val->intval = MAX17042_VOLTAGE(data); break; case POWER_SUPPLY_PROP_CAPACITY: if (chip->pdata->enable_current_sense) @@ -328,7 +336,7 @@ static int max17042_get_property(struct power_supply *psy, if (ret < 0) return ret; - data64 = data * 5000000ll; + data64 = MAX17042_CAPACITY_RSENSE(data); do_div(data64, chip->pdata->r_sns); val->intval = data64; break; @@ -337,7 +345,7 @@ static int max17042_get_property(struct power_supply *psy, if (ret < 0) return ret; - data64 = data * 5000000ll; + data64 = MAX17042_CAPACITY_RSENSE(data); do_div(data64, chip->pdata->r_sns); val->intval = data64; break; @@ -346,7 +354,7 @@ static int max17042_get_property(struct power_supply *psy, if (ret < 0) return ret; - data64 = data * 5000000ll; + data64 = MAX17042_CAPACITY_RSENSE(data); do_div(data64, chip->pdata->r_sns); val->intval = data64; break; @@ -355,7 +363,7 @@ static int max17042_get_property(struct power_supply *psy, if (ret < 0) return ret; - data64 = sign_extend64(data, 15) * 5000000ll; + data64 = MAX17042_CAPACITY_RSENSE(sign_extend64(data, 15)); val->intval = div_s64(data64, chip->pdata->r_sns); break; case POWER_SUPPLY_PROP_TEMP: @@ -397,7 +405,7 @@ static int max17042_get_property(struct power_supply *psy, if (ret < 0) return ret; - data64 = sign_extend64(data, 15) * 1562500ll; + data64 = MAX17042_CURRENT_RSENSE(sign_extend64(data, 15)); val->intval = div_s64(data64, chip->pdata->r_sns); } else { return -EINVAL; @@ -409,7 +417,7 @@ static int max17042_get_property(struct power_supply *psy, if (ret < 0) return ret; - data64 = sign_extend64(data, 15) * 1562500ll; + data64 = MAX17042_CURRENT_RSENSE(sign_extend64(data, 15)); val->intval = div_s64(data64, chip->pdata->r_sns); } else { return -EINVAL; @@ -420,7 +428,7 @@ static int max17042_get_property(struct power_supply *psy, if (ret < 0) return ret; - data64 = data * 1562500ll; + data64 = MAX17042_CURRENT_RSENSE(data); val->intval = div_s64(data64, chip->pdata->r_sns); break; case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: @@ -428,7 +436,7 @@ static int max17042_get_property(struct power_supply *psy, if (ret < 0) return ret; - val->intval = data * 5625 / 1000; + val->intval = MAX17042_TIME(data); break; default: return -EINVAL; -- 2.35.1