From patchwork Fri Mar 18 00:10:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sebastian Krzyszkowiak X-Patchwork-Id: 553146 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7C214C433FE for ; Fri, 18 Mar 2022 00:11:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230351AbiCRAM2 (ORCPT ); Thu, 17 Mar 2022 20:12:28 -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 Precedence: bulk List-ID: X-Mailing-List: linux-pm@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; From patchwork Fri Mar 18 00:10:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Krzyszkowiak X-Patchwork-Id: 553144 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F0DB3C433FE for ; Fri, 18 Mar 2022 00:11:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230442AbiCRANA (ORCPT ); Thu, 17 Mar 2022 20:13:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46684 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230448AbiCRAM7 (ORCPT ); Thu, 17 Mar 2022 20:12:59 -0400 Received: from comms.puri.sm (comms.puri.sm [159.203.221.185]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE5B4141479; Thu, 17 Mar 2022 17:11:41 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by comms.puri.sm (Postfix) with ESMTP id 7A512E007A; Thu, 17 Mar 2022 17:11:11 -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 HS29Kj_9xdt9; Thu, 17 Mar 2022 17:11:10 -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 2/4] power: supply: max17042_battery: use ModelCfg refresh on max17055 Date: Fri, 18 Mar 2022 01:10:46 +0100 Message-Id: <20220318001048.20922-3-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 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Unlike other models, max17055 doesn't require cell characterization data and operates on smaller amount of input variables (DesignCap, VEmpty, IChgTerm and ModelCfg). Input data can already be filled in by max17042_override_por_values, however model refresh bit has to be set after adjusting input variables in order to make them apply. Signed-off-by: Sebastian Krzyszkowiak --- drivers/power/supply/max17042_battery.c | 73 +++++++++++++++---------- include/linux/power/max17042_battery.h | 3 + 2 files changed, 48 insertions(+), 28 deletions(-) diff --git a/drivers/power/supply/max17042_battery.c b/drivers/power/supply/max17042_battery.c index c019d6c52363..c39250349a1d 100644 --- a/drivers/power/supply/max17042_battery.c +++ b/drivers/power/supply/max17042_battery.c @@ -806,6 +806,13 @@ static inline void max17042_override_por_values(struct max17042_chip *chip) (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17055)) { max17042_override_por(map, MAX17047_V_empty, config->vempty); } + + if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17055) { + max17042_override_por(map, MAX17055_ModelCfg, config->model_cfg); + // VChg is 1 by default, so allow it to be set to 0 + regmap_update_bits(map, MAX17055_ModelCfg, + MAX17055_MODELCFG_VCHG_BIT, config->model_cfg); + } } static int max17042_init_chip(struct max17042_chip *chip) @@ -814,44 +821,54 @@ static int max17042_init_chip(struct max17042_chip *chip) int ret; max17042_override_por_values(chip); + + if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17055) { + regmap_write_bits(map, MAX17055_ModelCfg, + MAX17055_MODELCFG_REFRESH_BIT, MAX17055_MODELCFG_REFRESH_BIT); + } + /* After Power up, the MAX17042 requires 500mS in order * to perform signal debouncing and initial SOC reporting */ msleep(500); - /* Initialize configuration */ - max17042_write_config_regs(chip); - - /* write cell characterization data */ - ret = max17042_init_model(chip); - if (ret) { - dev_err(&chip->client->dev, "%s init failed\n", - __func__); - return -EIO; - } + if ((chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) || + (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17047) || + (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17050)) { + /* Initialize configuration */ + max17042_write_config_regs(chip); + + /* write cell characterization data */ + ret = max17042_init_model(chip); + if (ret) { + dev_err(&chip->client->dev, "%s init failed\n", + __func__); + return -EIO; + } - ret = max17042_verify_model_lock(chip); - if (ret) { - dev_err(&chip->client->dev, "%s lock verify failed\n", - __func__); - return -EIO; - } - /* write custom parameters */ - max17042_write_custom_regs(chip); + ret = max17042_verify_model_lock(chip); + if (ret) { + dev_err(&chip->client->dev, "%s lock verify failed\n", + __func__); + return -EIO; + } + /* write custom parameters */ + max17042_write_custom_regs(chip); - /* update capacity params */ - max17042_update_capacity_regs(chip); + /* update capacity params */ + max17042_update_capacity_regs(chip); - /* delay must be atleast 350mS to allow VFSOC - * to be calculated from the new configuration - */ - msleep(350); + /* delay must be at least 350mS to allow VFSOC + * to be calculated from the new configuration + */ + msleep(350); - /* reset vfsoc0 reg */ - max17042_reset_vfsoc0_reg(chip); + /* reset vfsoc0 reg */ + max17042_reset_vfsoc0_reg(chip); - /* load new capacity params */ - max17042_load_new_capacity_params(chip); + /* load new capacity params */ + max17042_load_new_capacity_params(chip); + } /* Init complete, Clear the POR bit */ regmap_update_bits(map, MAX17042_STATUS, STATUS_POR_BIT, 0x0); diff --git a/include/linux/power/max17042_battery.h b/include/linux/power/max17042_battery.h index c417abd2ab70..6943921cab5e 100644 --- a/include/linux/power/max17042_battery.h +++ b/include/linux/power/max17042_battery.h @@ -23,6 +23,8 @@ #define MAX17042_CHARACTERIZATION_DATA_SIZE 48 +#define MAX17055_MODELCFG_REFRESH_BIT BIT(15) + enum max17042_register { MAX17042_STATUS = 0x00, MAX17042_VALRT_Th = 0x01, @@ -208,6 +210,7 @@ struct max17042_config_data { u16 full_soc_thresh; /* 0x13 */ u16 design_cap; /* 0x18 */ u16 ichgt_term; /* 0x1E */ + u16 model_cfg; /* 0xDB */ /* MG3 config */ u16 at_rate; /* 0x04 */ From patchwork Fri Mar 18 00:10:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Krzyszkowiak X-Patchwork-Id: 552974 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F3A4CC433EF for ; Fri, 18 Mar 2022 00:11:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230418AbiCRAMe (ORCPT ); Thu, 17 Mar 2022 20:12:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230420AbiCRAMd (ORCPT ); Thu, 17 Mar 2022 20:12:33 -0400 Received: from comms.puri.sm (comms.puri.sm [159.203.221.185]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42DCB139CED; Thu, 17 Mar 2022 17:11:16 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by comms.puri.sm (Postfix) with ESMTP id 2FB22E0063; Thu, 17 Mar 2022 17:11:16 -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 wTGkuPE_hqo8; Thu, 17 Mar 2022 17:11:15 -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 3/4] dt-bindings: power: supply: max17042: Add monitored-battery phandle Date: Fri, 18 Mar 2022 01:10:47 +0100 Message-Id: <20220318001048.20922-4-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 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org In order to let the driver know about the characteristics of the monitored battery, allow a standard "monitored-battery" property to be specified. Cc: devicetree@vger.kernel.org Signed-off-by: Sebastian Krzyszkowiak Reviewed-by: Krzysztof Kozlowski --- .../devicetree/bindings/power/supply/maxim,max17042.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Documentation/devicetree/bindings/power/supply/maxim,max17042.yaml b/Documentation/devicetree/bindings/power/supply/maxim,max17042.yaml index 971b53c58cc6..88c9f466f2c5 100644 --- a/Documentation/devicetree/bindings/power/supply/maxim,max17042.yaml +++ b/Documentation/devicetree/bindings/power/supply/maxim,max17042.yaml @@ -59,6 +59,10 @@ properties: Voltage threshold to report battery as over voltage (in mV). Default is not to report over-voltage events. + monitored-battery: + $ref: /schemas/types.yaml#/definitions/phandle + description: phandle to the battery node being monitored + required: - compatible - reg From patchwork Fri Mar 18 00:10:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Krzyszkowiak X-Patchwork-Id: 553145 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 75A23C433EF for ; Fri, 18 Mar 2022 00:11:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230428AbiCRAMi (ORCPT ); Thu, 17 Mar 2022 20:12:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230420AbiCRAMi (ORCPT ); Thu, 17 Mar 2022 20:12:38 -0400 Received: from comms.puri.sm (comms.puri.sm [159.203.221.185]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A183139CED; Thu, 17 Mar 2022 17:11:20 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by comms.puri.sm (Postfix) with ESMTP id 757F8E0063; Thu, 17 Mar 2022 17:11:20 -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 iedwKpeVX3pq; Thu, 17 Mar 2022 17:11:19 -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 4/4] power: supply: max17042_battery: read battery properties from device tree Date: Fri, 18 Mar 2022 01:10:48 +0100 Message-Id: <20220318001048.20922-5-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 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org So far configuring the gauge was only possible using platform data, with no way to provide the configuration on device tree-based platforms. Change that by looking up the configuration values from monitored-battery property. This is especially useful on models implementing ModelGauge m5 EZ algorithm, such as MAX17055, as all the required configuration can be derived from a "simple-battery" DT node there. In order to be able to access power supply framework in get_of_pdata, move devm_power_supply_register earlier in max17042_probe. Signed-off-by: Sebastian Krzyszkowiak --- drivers/power/supply/max17042_battery.c | 50 +++++++++++++++++++------ include/linux/power/max17042_battery.h | 1 + 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/drivers/power/supply/max17042_battery.c b/drivers/power/supply/max17042_battery.c index c39250349a1d..4c33565802d5 100644 --- a/drivers/power/supply/max17042_battery.c +++ b/drivers/power/supply/max17042_battery.c @@ -937,7 +937,9 @@ max17042_get_of_pdata(struct max17042_chip *chip) struct device *dev = &chip->client->dev; struct device_node *np = dev->of_node; u32 prop; + u64 data64; struct max17042_platform_data *pdata; + struct power_supply_battery_info *info; pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); if (!pdata) @@ -961,6 +963,32 @@ max17042_get_of_pdata(struct max17042_chip *chip) if (of_property_read_s32(np, "maxim,over-volt", &pdata->vmax)) pdata->vmax = INT_MAX; + if (pdata->enable_current_sense && + power_supply_get_battery_info(chip->battery, &info) == 0) { + pdata->config_data = devm_kzalloc(dev, sizeof(*pdata->config_data), GFP_KERNEL); + if (!pdata->config_data) + return NULL; + + if (info->charge_full_design_uah != -EINVAL) { + data64 = (u64)info->charge_full_design_uah * pdata->r_sns; + do_div(data64, MAX17042_CAPACITY_LSB); + pdata->config_data->design_cap = (u16)data64; + pdata->enable_por_init = true; + } + if (info->charge_term_current_ua != -EINVAL) { + data64 = (u64)info->charge_term_current_ua * pdata->r_sns; + do_div(data64, MAX17042_CURRENT_LSB); + pdata->config_data->ichgt_term = (u16)data64; + pdata->enable_por_init = true; + } + if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17055) { + if (info->voltage_max_design_uv > 4250000) { + pdata->config_data->model_cfg = MAX17055_MODELCFG_VCHG_BIT; + pdata->enable_por_init = true; + } + } + } + return pdata; } #endif @@ -1092,16 +1120,23 @@ static int max17042_probe(struct i2c_client *client, return -EINVAL; } + i2c_set_clientdata(client, chip); + psy_cfg.drv_data = chip; + psy_cfg.of_node = dev->of_node; + + chip->battery = devm_power_supply_register(&client->dev, max17042_desc, + &psy_cfg); + if (IS_ERR(chip->battery)) { + dev_err(&client->dev, "failed: power supply register\n"); + return PTR_ERR(chip->battery); + } + chip->pdata = max17042_get_pdata(chip); if (!chip->pdata) { dev_err(&client->dev, "no platform data provided\n"); return -EINVAL; } - i2c_set_clientdata(client, chip); - psy_cfg.drv_data = chip; - psy_cfg.of_node = dev->of_node; - /* When current is not measured, * CURRENT_NOW and CURRENT_AVG properties should be invisible. */ if (!chip->pdata->enable_current_sense) @@ -1122,13 +1157,6 @@ static int max17042_probe(struct i2c_client *client, regmap_write(chip->regmap, MAX17042_LearnCFG, 0x0007); } - chip->battery = devm_power_supply_register(&client->dev, max17042_desc, - &psy_cfg); - if (IS_ERR(chip->battery)) { - dev_err(&client->dev, "failed: power supply register\n"); - return PTR_ERR(chip->battery); - } - if (client->irq) { unsigned int flags = IRQF_ONESHOT; diff --git a/include/linux/power/max17042_battery.h b/include/linux/power/max17042_battery.h index 6943921cab5e..367620800e7e 100644 --- a/include/linux/power/max17042_battery.h +++ b/include/linux/power/max17042_battery.h @@ -23,6 +23,7 @@ #define MAX17042_CHARACTERIZATION_DATA_SIZE 48 +#define MAX17055_MODELCFG_VCHG_BIT BIT(10) #define MAX17055_MODELCFG_REFRESH_BIT BIT(15) enum max17042_register {