From patchwork Wed Jun 24 15:56:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iskren Chernev X-Patchwork-Id: 198525 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7D8D1C433E1 for ; Wed, 24 Jun 2020 15:56:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 58E4E20B80 for ; Wed, 24 Jun 2020 15:56:57 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="u1n8/Pfd" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404644AbgFXP4u (ORCPT ); Wed, 24 Jun 2020 11:56:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35954 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404351AbgFXP4t (ORCPT ); Wed, 24 Jun 2020 11:56:49 -0400 Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EF59DC061573; Wed, 24 Jun 2020 08:56:48 -0700 (PDT) Received: by mail-wr1-x443.google.com with SMTP id g18so2818487wrm.2; Wed, 24 Jun 2020 08:56:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=e0eRXDu98M9NKV0R7IkrYaYI1Jss3RwntJq8TlK0J3M=; b=u1n8/Pfd1zsmvwHOIwFAdpXFBCEJEz99VHTMmmpy89KwouVtNivtqrweIBxKBuQCsO TfeoGz26qldEMwNYh9hDI1XnyXYilLL6U0ptbxEfVl9up42fCTL1UGjBlp99ykNFpx7w WR8bihnqS1GVLAB0jN3eHsiHq0EffU9ptWUQI5l5gBeCoET5l4BikddXAZ9/oMoLmIQm hmr/3JRtg4mi7fhX57Cm5YK93aLY7YvmP9bS2zOzte4yrmGIJN1LhAqnX0wUzaCiVnta 0GMEcWoaepVf6VL+tZQbgaIel6HA/8POR4oO3k/v2Xm9dimysPK0h/Qp+MQKwInI/PXG PttQ== 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:mime-version:content-transfer-encoding; bh=e0eRXDu98M9NKV0R7IkrYaYI1Jss3RwntJq8TlK0J3M=; b=Mp6P+LGOrfUol2gr8goA8wq/m9AwjkMsNIm1r03vp+FCQy7ESdQEsWdh810hJzgWpG WlIVcdIX3G3ut6yk8ZJi6THGygRt2R/AAIr5xy/T+SxeFI9HJbUFTavi8vSY9wcYHF6m Uo9nLjKftLzxoULiqtbkoLYlAv8CyP6Qm84LgN4kw9Lm5TVa1ycs3x8cMr5A25M3CgG6 kYbmCV36muiZybHwDy2mm+souw3MvRWh6kF7SuTkR8WRy92O2lTGsGnK4pJ/LT6w0w2Z p8CgZBH4I2MWbPuvUUzv62/6fcDAhKpyVEOYcWHyQhjKQF1PyndnaQ356Ret0qMQTP2c u/Ug== X-Gm-Message-State: AOAM531mxqjqlkj17RhVJpKEOY7aggP6DBOM9S7TkvwW9I8mAxv8Mv2O MfCxs1+UAeMEmokayLttjH8tr3W9lZQ= X-Google-Smtp-Source: ABdhPJz9fyj06ufYAP3BqfcnXXtQcxvhh9AoXY0nrSORTgtvgbARLxsAqSZqc6LznjMyUA57s6o0sw== X-Received: by 2002:adf:edc6:: with SMTP id v6mr20373157wro.413.1593014207500; Wed, 24 Jun 2020 08:56:47 -0700 (PDT) Received: from localhost ([213.191.183.145]) by smtp.gmail.com with ESMTPSA id m10sm9398257wru.4.2020.06.24.08.56.45 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 24 Jun 2020 08:56:46 -0700 (PDT) From: Iskren Chernev To: Sebastian Reichel , Rob Herring Cc: linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, ~postmarketos/upstreaming@lists.sr.ht, Jonathan Bakker , Vladimir Barinov , Iskren Chernev Subject: [PATCH v3 1/6] power: supply: max17040: Use regmap i2c Date: Wed, 24 Jun 2020 18:56:28 +0300 Message-Id: <20200624155633.3557401-2-iskren.chernev@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200624155633.3557401-1-iskren.chernev@gmail.com> References: <20200624155633.3557401-1-iskren.chernev@gmail.com> MIME-Version: 1.0 Sender: devicetree-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org Rewrite i2c operations from i2c client read/write to regmap i2c. As a result, most private functions now accept the private driver data instead of an i2c client pointer. Signed-off-by: Iskren Chernev --- drivers/power/supply/Kconfig | 1 + drivers/power/supply/max17040_battery.c | 250 +++++++++++------------- 2 files changed, 110 insertions(+), 141 deletions(-) diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig index 44d3c8512fb8d..12ca79952768f 100644 --- a/drivers/power/supply/Kconfig +++ b/drivers/power/supply/Kconfig @@ -367,6 +367,7 @@ config AXP288_FUEL_GAUGE config BATTERY_MAX17040 tristate "Maxim MAX17040 Fuel Gauge" depends on I2C + select REGMAP_I2C help MAX17040 is fuel-gauge systems for lithium-ion (Li+) batteries in handheld and portable equipment. The MAX17040 is configured diff --git a/drivers/power/supply/max17040_battery.c b/drivers/power/supply/max17040_battery.c index 48aa44665e2f1..678241fcc2548 100644 --- a/drivers/power/supply/max17040_battery.c +++ b/drivers/power/supply/max17040_battery.c @@ -16,32 +16,30 @@ #include #include #include +#include #include #define MAX17040_VCELL 0x02 #define MAX17040_SOC 0x04 #define MAX17040_MODE 0x06 #define MAX17040_VER 0x08 -#define MAX17040_RCOMP 0x0C +#define MAX17040_CONFIG 0x0C #define MAX17040_CMD 0xFE #define MAX17040_DELAY 1000 #define MAX17040_BATTERY_FULL 95 -#define MAX17040_ATHD_MASK 0xFFC0 +#define MAX17040_ATHD_MASK 0x3f #define MAX17040_ATHD_DEFAULT_POWER_UP 4 struct max17040_chip { struct i2c_client *client; + struct regmap *regmap; struct delayed_work work; struct power_supply *battery; struct max17040_platform_data *pdata; - /* State Of Connect */ - int online; - /* battery voltage */ - int vcell; /* battery capacity */ int soc; /* State Of Charge */ @@ -50,135 +48,68 @@ struct max17040_chip { u32 low_soc_alert; }; -static int max17040_get_property(struct power_supply *psy, - enum power_supply_property psp, - union power_supply_propval *val) -{ - struct max17040_chip *chip = power_supply_get_drvdata(psy); - - switch (psp) { - case POWER_SUPPLY_PROP_STATUS: - val->intval = chip->status; - break; - case POWER_SUPPLY_PROP_ONLINE: - val->intval = chip->online; - break; - case POWER_SUPPLY_PROP_VOLTAGE_NOW: - val->intval = chip->vcell; - break; - case POWER_SUPPLY_PROP_CAPACITY: - val->intval = chip->soc; - break; - default: - return -EINVAL; - } - return 0; -} - -static int max17040_write_reg(struct i2c_client *client, int reg, u16 value) -{ - int ret; - - ret = i2c_smbus_write_word_swapped(client, reg, value); - - if (ret < 0) - dev_err(&client->dev, "%s: err %d\n", __func__, ret); - - return ret; -} - -static int max17040_read_reg(struct i2c_client *client, int reg) -{ - int ret; - - ret = i2c_smbus_read_word_swapped(client, reg); - - if (ret < 0) - dev_err(&client->dev, "%s: err %d\n", __func__, ret); - - return ret; -} - -static void max17040_reset(struct i2c_client *client) +static int max17040_reset(struct max17040_chip *chip) { - max17040_write_reg(client, MAX17040_CMD, 0x0054); + return regmap_write(chip->regmap, MAX17040_CMD, 0x0054); } -static int max17040_set_low_soc_alert(struct i2c_client *client, u32 level) +static int max17040_set_low_soc_alert(struct max17040_chip *chip, u32 level) { - int ret; - u16 data; - level = 32 - level; - data = max17040_read_reg(client, MAX17040_RCOMP); - /* clear the alrt bit and set LSb 5 bits */ - data &= MAX17040_ATHD_MASK; - data |= level; - ret = max17040_write_reg(client, MAX17040_RCOMP, data); - - return ret; + return regmap_update_bits(chip->regmap, MAX17040_CONFIG, + MAX17040_ATHD_MASK, level); } -static void max17040_get_vcell(struct i2c_client *client) +static int max17040_get_vcell(struct max17040_chip *chip) { - struct max17040_chip *chip = i2c_get_clientdata(client); - u16 vcell; + u32 vcell; - vcell = max17040_read_reg(client, MAX17040_VCELL); + regmap_read(chip->regmap, MAX17040_VCELL, &vcell); - chip->vcell = (vcell >> 4) * 1250; + return (vcell >> 4) * 1250; } -static void max17040_get_soc(struct i2c_client *client) +static int max17040_get_soc(struct max17040_chip *chip) { - struct max17040_chip *chip = i2c_get_clientdata(client); - u16 soc; + u32 soc; - soc = max17040_read_reg(client, MAX17040_SOC); + regmap_read(chip->regmap, MAX17040_SOC, &soc); - chip->soc = (soc >> 8); + return soc >> 8; } -static void max17040_get_version(struct i2c_client *client) +static int max17040_get_version(struct max17040_chip *chip) { - u16 version; + int ret; + u32 version; - version = max17040_read_reg(client, MAX17040_VER); + ret = regmap_read(chip->regmap, MAX17040_VER, &version); - dev_info(&client->dev, "MAX17040 Fuel-Gauge Ver 0x%x\n", version); + return ret ? ret : version; } -static void max17040_get_online(struct i2c_client *client) +static int max17040_get_online(struct max17040_chip *chip) { - struct max17040_chip *chip = i2c_get_clientdata(client); - - if (chip->pdata && chip->pdata->battery_online) - chip->online = chip->pdata->battery_online(); - else - chip->online = 1; + return chip->pdata && chip->pdata->battery_online ? + chip->pdata->battery_online() : 1; } -static void max17040_get_status(struct i2c_client *client) +static int max17040_get_status(struct max17040_chip *chip) { - struct max17040_chip *chip = i2c_get_clientdata(client); - if (!chip->pdata || !chip->pdata->charger_online - || !chip->pdata->charger_enable) { - chip->status = POWER_SUPPLY_STATUS_UNKNOWN; - return; - } + || !chip->pdata->charger_enable) + return POWER_SUPPLY_STATUS_UNKNOWN; + + if (max17040_get_soc(chip) > MAX17040_BATTERY_FULL) + return POWER_SUPPLY_STATUS_FULL; - if (chip->pdata->charger_online()) { + if (chip->pdata->charger_online()) if (chip->pdata->charger_enable()) - chip->status = POWER_SUPPLY_STATUS_CHARGING; + return POWER_SUPPLY_STATUS_CHARGING; else - chip->status = POWER_SUPPLY_STATUS_NOT_CHARGING; - } else { - chip->status = POWER_SUPPLY_STATUS_DISCHARGING; - } - - if (chip->soc > MAX17040_BATTERY_FULL) - chip->status = POWER_SUPPLY_STATUS_FULL; + return POWER_SUPPLY_STATUS_NOT_CHARGING; + else + return POWER_SUPPLY_STATUS_DISCHARGING; } static int max17040_get_of_data(struct max17040_chip *chip) @@ -190,18 +121,31 @@ static int max17040_get_of_data(struct max17040_chip *chip) "maxim,alert-low-soc-level", &chip->low_soc_alert); - if (chip->low_soc_alert <= 0 || chip->low_soc_alert >= 33) + if (chip->low_soc_alert <= 0 || chip->low_soc_alert >= 33) { + dev_err(dev, "maxim,alert-low-soc-level out of bounds\n"); return -EINVAL; + } return 0; } -static void max17040_check_changes(struct i2c_client *client) +static void max17040_check_changes(struct max17040_chip *chip) +{ + chip->soc = max17040_get_soc(chip); + chip->status = max17040_get_status(chip); +} + +static void max17040_queue_work(struct max17040_chip *chip) +{ + queue_delayed_work(system_power_efficient_wq, &chip->work, + MAX17040_DELAY); +} + +static void max17040_stop_work(void *data) { - max17040_get_vcell(client); - max17040_get_soc(client); - max17040_get_online(client); - max17040_get_status(client); + struct max17040_chip *chip = data; + + cancel_delayed_work_sync(&chip->work); } static void max17040_work(struct work_struct *work) @@ -214,30 +158,29 @@ static void max17040_work(struct work_struct *work) /* store SOC and status to check changes */ last_soc = chip->soc; last_status = chip->status; - max17040_check_changes(chip->client); + max17040_check_changes(chip); /* check changes and send uevent */ if (last_soc != chip->soc || last_status != chip->status) power_supply_changed(chip->battery); - queue_delayed_work(system_power_efficient_wq, &chip->work, - MAX17040_DELAY); + max17040_queue_work(chip); } static irqreturn_t max17040_thread_handler(int id, void *dev) { struct max17040_chip *chip = dev; - struct i2c_client *client = chip->client; - dev_warn(&client->dev, "IRQ: Alert battery low level"); + dev_warn(&chip->client->dev, "IRQ: Alert battery low level"); + /* read registers */ - max17040_check_changes(chip->client); + max17040_check_changes(chip); /* send uevent */ power_supply_changed(chip->battery); /* reset alert bit */ - max17040_set_low_soc_alert(client, chip->low_soc_alert); + max17040_set_low_soc_alert(chip, chip->low_soc_alert); return IRQ_HANDLED; } @@ -256,6 +199,38 @@ static int max17040_enable_alert_irq(struct max17040_chip *chip) return ret; } +static int max17040_get_property(struct power_supply *psy, + enum power_supply_property psp, + union power_supply_propval *val) +{ + struct max17040_chip *chip = power_supply_get_drvdata(psy); + + switch (psp) { + case POWER_SUPPLY_PROP_STATUS: + val->intval = max17040_get_status(chip); + break; + case POWER_SUPPLY_PROP_ONLINE: + val->intval = max17040_get_online(chip); + break; + case POWER_SUPPLY_PROP_VOLTAGE_NOW: + val->intval = max17040_get_vcell(chip); + break; + case POWER_SUPPLY_PROP_CAPACITY: + val->intval = max17040_get_soc(chip); + break; + default: + return -EINVAL; + } + return 0; +} + +static const struct regmap_config max17040_regmap = { + .reg_bits = 8, + .reg_stride = 2, + .val_bits = 16, + .val_format_endian = REGMAP_ENDIAN_BIG, +}; + static enum power_supply_property max17040_battery_props[] = { POWER_SUPPLY_PROP_STATUS, POWER_SUPPLY_PROP_ONLINE, @@ -287,31 +262,33 @@ static int max17040_probe(struct i2c_client *client, return -ENOMEM; chip->client = client; + chip->regmap = devm_regmap_init_i2c(client, &max17040_regmap); chip->pdata = client->dev.platform_data; ret = max17040_get_of_data(chip); - if (ret) { - dev_err(&client->dev, - "failed: low SOC alert OF data out of bounds\n"); + if (ret) return ret; - } i2c_set_clientdata(client, chip); psy_cfg.drv_data = chip; - chip->battery = power_supply_register(&client->dev, + chip->battery = devm_power_supply_register(&client->dev, &max17040_battery_desc, &psy_cfg); if (IS_ERR(chip->battery)) { dev_err(&client->dev, "failed: power supply register\n"); return PTR_ERR(chip->battery); } - max17040_reset(client); - max17040_get_version(client); + ret = max17040_get_version(chip); + if (ret < 0) + return ret; + dev_dbg(&chip->client->dev, "MAX17040 Fuel-Gauge Ver 0x%x\n", ret); + + max17040_reset(chip); /* check interrupt */ if (client->irq && of_device_is_compatible(client->dev.of_node, "maxim,max77836-battery")) { - ret = max17040_set_low_soc_alert(client, chip->low_soc_alert); + ret = max17040_set_low_soc_alert(chip, chip->low_soc_alert); if (ret) { dev_err(&client->dev, "Failed to set low SOC alert: err %d\n", ret); @@ -327,18 +304,11 @@ static int max17040_probe(struct i2c_client *client, } INIT_DEFERRABLE_WORK(&chip->work, max17040_work); - queue_delayed_work(system_power_efficient_wq, &chip->work, - MAX17040_DELAY); - - return 0; -} - -static int max17040_remove(struct i2c_client *client) -{ - struct max17040_chip *chip = i2c_get_clientdata(client); + ret = devm_add_action(&client->dev, max17040_stop_work, chip); + if (ret) + return ret; + max17040_queue_work(chip); - power_supply_unregister(chip->battery); - cancel_delayed_work(&chip->work); return 0; } @@ -362,12 +332,11 @@ static int max17040_resume(struct device *dev) struct i2c_client *client = to_i2c_client(dev); struct max17040_chip *chip = i2c_get_clientdata(client); - queue_delayed_work(system_power_efficient_wq, &chip->work, - MAX17040_DELAY); - if (client->irq && device_may_wakeup(dev)) disable_irq_wake(client->irq); + max17040_queue_work(chip); + return 0; } @@ -401,7 +370,6 @@ static struct i2c_driver max17040_i2c_driver = { .pm = MAX17040_PM_OPS, }, .probe = max17040_probe, - .remove = max17040_remove, .id_table = max17040_id, }; module_i2c_driver(max17040_i2c_driver); From patchwork Wed Jun 24 15:56:29 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iskren Chernev X-Patchwork-Id: 198523 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4E764C433DF for ; Wed, 24 Jun 2020 15:57:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2DA3120738 for ; Wed, 24 Jun 2020 15:57:35 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="vcsjg+WO" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404669AbgFXP4z (ORCPT ); Wed, 24 Jun 2020 11:56:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404657AbgFXP4w (ORCPT ); Wed, 24 Jun 2020 11:56:52 -0400 Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B15F1C061573; Wed, 24 Jun 2020 08:56:51 -0700 (PDT) Received: by mail-wr1-x443.google.com with SMTP id h15so2796128wrq.8; Wed, 24 Jun 2020 08:56:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=SsocOkhsEBGPt4ylLwY4+agknd5SdHwSbE5z/lYrnUg=; b=vcsjg+WOdUVNUUl3OoEfJ4yxqra7MFSyrqsQSozD6FzBjgLAEOBa3tnynCqcCuAgeB k0fDK564QFFUmRe3JRopIl33gBrXT8IzO4FMF9R5Dja/gOC6H0pD0tUnh9yeuIH1Wc5Q iUoNDVkVCgc7kA82GVR5M01YGkq39+cXy4k9AZYwDzJNPLX6RNeaVkCBR22Rps1RTBxT 1utl693aWjNHtjfjiLd/OUYJit8PCL2Ea9+RY3JsNHQYtuffqy+Xnty3l2alonoYT9yY GHUxCZFrCjrRICPullT8Vj9oT1vpJ8ZbUIoetRmabfMM1NutXO1FTXmUMlM2C9LN2Hnd noMQ== 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:mime-version:content-transfer-encoding; bh=SsocOkhsEBGPt4ylLwY4+agknd5SdHwSbE5z/lYrnUg=; b=n0FasC+6xucT/c0s2Gc/yVilp9TK02AJbCzHM9v9SdvMCnIKtDsbDAAh+BYKl9py+6 qdB5bbpPrU7IzeqA+skbk5pMDPDUXGt6Cu5hr5Pq4/3X4x5okEZdFDy3vPHAd2WUz/xu aTtc3P0+orXwFJfTMsQjvf4hhev/1WJUaThhNm9wSP1Ee6CrbpnFG1NQMZYtK5L+GFb4 /X7vuvkNS5mpuufa7Ob1zdtjSp3lu1+dsHQB4/2c3oqc4O6Kx5/ooTDCrhxkaFCyW8XV 83EkBpOw8VS1CqTmaob5Zu7RxRhDZF8CG2wLtkIwQJwlmwxP7IIYcSDCxc1MLE8qGdZe he1w== X-Gm-Message-State: AOAM532PldRy47X1YnyHz7ctnG47BVp6EOGs8Hg7EBMpJwTQcGLkyw7y 7OMqFpQF3E3veU05asbyBrw= X-Google-Smtp-Source: ABdhPJxv8Mb6sKrct1+QA79hTd2qvGoOEFPmWrTsskj8dTeDfsa31O6Yjmj85VeNIi0zhmcw5FYHzQ== X-Received: by 2002:a5d:664e:: with SMTP id f14mr31903766wrw.6.1593014210501; Wed, 24 Jun 2020 08:56:50 -0700 (PDT) Received: from localhost ([213.191.183.145]) by smtp.gmail.com with ESMTPSA id a22sm8289031wmb.4.2020.06.24.08.56.48 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 24 Jun 2020 08:56:49 -0700 (PDT) From: Iskren Chernev To: Sebastian Reichel , Rob Herring Cc: linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, ~postmarketos/upstreaming@lists.sr.ht, Jonathan Bakker , Vladimir Barinov , Iskren Chernev Subject: [PATCH v3 2/6] dt-bindings: power: supply: Extend max17040 compatibility Date: Wed, 24 Jun 2020 18:56:29 +0300 Message-Id: <20200624155633.3557401-3-iskren.chernev@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200624155633.3557401-1-iskren.chernev@gmail.com> References: <20200624155633.3557401-1-iskren.chernev@gmail.com> MIME-Version: 1.0 Sender: devicetree-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org Maxim max17040 is a fuel gauge from a larger family utilising the Model Gauge technology. Document all different compatible strings that the max17040 driver recognizes. Some devices in the wild report double the capacity. The maxim,double-soc (from State-Of-Charge) property fixes that. Signed-off-by: Iskren Chernev Reviewed-by: Rob Herring --- .../bindings/power/supply/max17040_battery.txt | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Documentation/devicetree/bindings/power/supply/max17040_battery.txt b/Documentation/devicetree/bindings/power/supply/max17040_battery.txt index 4e0186b8380fa..554bce82a08e6 100644 --- a/Documentation/devicetree/bindings/power/supply/max17040_battery.txt +++ b/Documentation/devicetree/bindings/power/supply/max17040_battery.txt @@ -2,7 +2,9 @@ max17040_battery ~~~~~~~~~~~~~~~~ Required properties : - - compatible : "maxim,max17040" or "maxim,max77836-battery" + - compatible : "maxim,max17040", "maxim,max17041", "maxim,max17043", + "maxim,max17044", "maxim,max17048", "maxim,max17049", + "maxim,max17058", "maxim,max17059" or "maxim,max77836-battery" - reg: i2c slave address Optional properties : @@ -11,6 +13,10 @@ Optional properties : generated. Can be configured from 1 up to 32 (%). If skipped the power up default value of 4 (%) will be used. +- maxim,double-soc : Certain devices return double the capacity. + Specify this boolean property to divide the + reported value in 2 and thus normalize it. + SOC == State of Charge == Capacity. - interrupts : Interrupt line see Documentation/devicetree/ bindings/interrupt-controller/interrupts.txt - wakeup-source : This device has wakeup capabilities. Use this @@ -31,3 +37,10 @@ Example: interrupts = <2 IRQ_TYPE_EDGE_FALLING>; wakeup-source; }; + + battery-fuel-gauge@36 { + compatible = "maxim,max17048"; + reg = <0x36>; + maxim,alert-low-soc-level = <10>; + maxim,double-soc; + }; From patchwork Wed Jun 24 15:56:33 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iskren Chernev X-Patchwork-Id: 198524 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 280CCC433E0 for ; Wed, 24 Jun 2020 15:57:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 011F9212CC for ; Wed, 24 Jun 2020 15:57:31 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="rUBalvyy" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404635AbgFXP5M (ORCPT ); Wed, 24 Jun 2020 11:57:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36000 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404718AbgFXP5C (ORCPT ); Wed, 24 Jun 2020 11:57:02 -0400 Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E4E60C061573; Wed, 24 Jun 2020 08:57:01 -0700 (PDT) Received: by mail-wr1-x443.google.com with SMTP id r12so2758327wrj.13; Wed, 24 Jun 2020 08:57:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=0fWqoRxWBCqdBHBS2H2/Nb85DBqENZwJYZ563FQxmmA=; b=rUBalvyyjuzbg5xoceVShBhFF0BzgWQFg0AOoc2qHtRNRvowsiWu2pqejfNGvdB8Jx PqlgYIvst/jtIVEav/2Prz3BcJM0RS+lWSQL/7VO3UzP/EDXVnxcC/Psp89gK1j2+kRg F500TJuLDZ5cPhwEtUA2yOW26s3nYXMcvaNrVCFN9pps60OzDa2NOdIowPfMyx2SFTpj v9dN9zXh94YjcZOgYwrRtTJMojpU85Sq6EOnjVZs9l/Vr5UpAQ0TE9O+U9l9F1Krk9gO vZnzpn1YJOG9lqEvkXLWMn19OdHhlR7vLHIa/CgCAuv/9JecmxVztty5ZJHQSNMFnkuH TL3A== 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:mime-version:content-transfer-encoding; bh=0fWqoRxWBCqdBHBS2H2/Nb85DBqENZwJYZ563FQxmmA=; b=oofsVO7uUu+Xy5FvPIapjPoiDJYO183T6a8ieLi86j0++ivTuuLCGjVHlP6ou1nSRA NFgCt+15cEfoG6zrjQCQ44ckMCmQSMsvQS1BLQttC2vF++aXFPqgONHTEcDElz6NocnD 1JoWMH/NiByIxBrIdcOtuiWskc5qIqUypHo/xXs/73BZAY+qj5K4m4HFy7wjZlE8r52h ptLyD1EP7Et96avb7xvQC4w9RrA7k7yWHlKX2M9cHuY/+v66DF5mgS8BD6Q5SDAjINgT XA/taG+IKzT99afwO1jJym2PhKG0biV8fX3QbhSWqbBbUKxjM5po8jt+WknPFIZIJ+IN esVg== X-Gm-Message-State: AOAM533w4QcbgcCSquNWY2UgZfv1VzwN/VqWPazsVkqtisq/wc5FZRaY iJKNyibG7+0uBi7HFsRTR54= X-Google-Smtp-Source: ABdhPJxD6+qpz0n4qf0xCNR0wrqQdIU/JLb07XUrL0PuZCeycXAeQEX77+AbjTC4v/5AjrgguvFV3Q== X-Received: by 2002:a5d:4008:: with SMTP id n8mr31143871wrp.82.1593014220595; Wed, 24 Jun 2020 08:57:00 -0700 (PDT) Received: from localhost ([213.191.183.145]) by smtp.gmail.com with ESMTPSA id u186sm8951064wmu.10.2020.06.24.08.56.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 24 Jun 2020 08:57:00 -0700 (PDT) From: Iskren Chernev To: Sebastian Reichel , Rob Herring Cc: linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, ~postmarketos/upstreaming@lists.sr.ht, Jonathan Bakker , Vladimir Barinov , Iskren Chernev Subject: [PATCH v3 6/6] power: supply: max17040: Support soc alert Date: Wed, 24 Jun 2020 18:56:33 +0300 Message-Id: <20200624155633.3557401-7-iskren.chernev@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200624155633.3557401-1-iskren.chernev@gmail.com> References: <20200624155633.3557401-1-iskren.chernev@gmail.com> MIME-Version: 1.0 Sender: devicetree-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org max17048 and max17049 support SOC alerts (interrupts when battery capacity changes by +/- 1%). At the moment the driver polls for changes every second. Using the alerts removes the need for polling. Signed-off-by: Iskren Chernev --- drivers/power/supply/max17040_battery.c | 82 ++++++++++++++++++++++--- 1 file changed, 73 insertions(+), 9 deletions(-) diff --git a/drivers/power/supply/max17040_battery.c b/drivers/power/supply/max17040_battery.c index 54393f411211e..4425411775f26 100644 --- a/drivers/power/supply/max17040_battery.c +++ b/drivers/power/supply/max17040_battery.c @@ -25,6 +25,7 @@ #define MAX17040_MODE 0x06 #define MAX17040_VER 0x08 #define MAX17040_CONFIG 0x0C +#define MAX17040_STATUS 0x1A #define MAX17040_CMD 0xFE @@ -33,7 +34,10 @@ #define MAX17040_RCOMP_DEFAULT 0x9700 #define MAX17040_ATHD_MASK 0x3f +#define MAX17040_ALSC_MASK 0x40 #define MAX17040_ATHD_DEFAULT_POWER_UP 4 +#define MAX17040_STATUS_HD_MASK 0x1000 +#define MAX17040_STATUS_SC_MASK 0x2000 #define MAX17040_CFG_RCOMP_MASK 0xff00 enum chip_id { @@ -55,6 +59,7 @@ struct chip_data { u16 vcell_div; u8 has_low_soc_alert; u8 rcomp_bytes; + u8 has_soc_alert; }; static struct chip_data max17040_family[] = { @@ -65,6 +70,7 @@ static struct chip_data max17040_family[] = { .vcell_div = 1, .has_low_soc_alert = 0, .rcomp_bytes = 2, + .has_soc_alert = 0, }, [ID_MAX17041] = { .reset_val = 0x0054, @@ -73,6 +79,7 @@ static struct chip_data max17040_family[] = { .vcell_div = 1, .has_low_soc_alert = 0, .rcomp_bytes = 2, + .has_soc_alert = 0, }, [ID_MAX17043] = { .reset_val = 0x0054, @@ -81,6 +88,7 @@ static struct chip_data max17040_family[] = { .vcell_div = 1, .has_low_soc_alert = 1, .rcomp_bytes = 1, + .has_soc_alert = 0, }, [ID_MAX17044] = { .reset_val = 0x0054, @@ -89,6 +97,7 @@ static struct chip_data max17040_family[] = { .vcell_div = 1, .has_low_soc_alert = 1, .rcomp_bytes = 1, + .has_soc_alert = 0, }, [ID_MAX17048] = { .reset_val = 0x5400, @@ -97,6 +106,7 @@ static struct chip_data max17040_family[] = { .vcell_div = 8, .has_low_soc_alert = 1, .rcomp_bytes = 1, + .has_soc_alert = 1, }, [ID_MAX17049] = { .reset_val = 0x5400, @@ -105,6 +115,7 @@ static struct chip_data max17040_family[] = { .vcell_div = 4, .has_low_soc_alert = 1, .rcomp_bytes = 1, + .has_soc_alert = 1, }, [ID_MAX17058] = { .reset_val = 0x5400, @@ -113,6 +124,7 @@ static struct chip_data max17040_family[] = { .vcell_div = 8, .has_low_soc_alert = 1, .rcomp_bytes = 1, + .has_soc_alert = 0, }, [ID_MAX17059] = { .reset_val = 0x5400, @@ -121,6 +133,7 @@ static struct chip_data max17040_family[] = { .vcell_div = 4, .has_low_soc_alert = 1, .rcomp_bytes = 1, + .has_soc_alert = 0, }, }; @@ -156,6 +169,12 @@ static int max17040_set_low_soc_alert(struct max17040_chip *chip, u32 level) MAX17040_ATHD_MASK, level); } +static int max17040_set_soc_alert(struct max17040_chip *chip, bool enable) +{ + return regmap_update_bits(chip->regmap, MAX17040_CONFIG, + MAX17040_ALSC_MASK, enable ? MAX17040_ALSC_MASK : 0); +} + static int max17040_set_rcomp(struct max17040_chip *chip, u16 rcomp) { u16 mask = chip->data.rcomp_bytes == 2 ? @@ -298,11 +317,33 @@ static void max17040_work(struct work_struct *work) max17040_queue_work(chip); } +/* Returns true if alert cause was SOC change, not low SOC */ +static bool max17040_handle_soc_alert(struct max17040_chip *chip) +{ + bool ret = true; + u32 data; + + regmap_read(chip->regmap, MAX17040_STATUS, &data); + + if (data & MAX17040_STATUS_HD_MASK) { + // this alert was caused by low soc + ret = false; + } + if (data & MAX17040_STATUS_SC_MASK) { + // soc change bit -- deassert to mark as handled + regmap_write(chip->regmap, MAX17040_STATUS, + data & ~MAX17040_STATUS_SC_MASK); + } + + return ret; +} + static irqreturn_t max17040_thread_handler(int id, void *dev) { struct max17040_chip *chip = dev; - dev_warn(&chip->client->dev, "IRQ: Alert battery low level"); + if (!(chip->data.has_soc_alert && max17040_handle_soc_alert(chip))) + dev_warn(&chip->client->dev, "IRQ: Alert battery low level\n"); /* read registers */ max17040_check_changes(chip); @@ -384,6 +425,7 @@ static int max17040_probe(struct i2c_client *client, struct power_supply_config psy_cfg = {}; struct max17040_chip *chip; enum chip_id chip_id; + bool enable_irq = false; int ret; if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) @@ -434,6 +476,27 @@ static int max17040_probe(struct i2c_client *client, return ret; } + enable_irq = true; + } + + if (client->irq && chip->data.has_soc_alert) { + ret = max17040_set_soc_alert(chip, 1); + if (ret) { + dev_err(&client->dev, + "Failed to set SOC alert: err %d\n", ret); + return ret; + } + enable_irq = true; + } else { + /* soc alerts negate the need for polling */ + INIT_DEFERRABLE_WORK(&chip->work, max17040_work); + ret = devm_add_action(&client->dev, max17040_stop_work, chip); + if (ret) + return ret; + max17040_queue_work(chip); + } + + if (enable_irq) { ret = max17040_enable_alert_irq(chip); if (ret) { client->irq = 0; @@ -442,12 +505,6 @@ static int max17040_probe(struct i2c_client *client, } } - INIT_DEFERRABLE_WORK(&chip->work, max17040_work); - ret = devm_add_action(&client->dev, max17040_stop_work, chip); - if (ret) - return ret; - max17040_queue_work(chip); - return 0; } @@ -458,7 +515,11 @@ static int max17040_suspend(struct device *dev) struct i2c_client *client = to_i2c_client(dev); struct max17040_chip *chip = i2c_get_clientdata(client); - cancel_delayed_work(&chip->work); + if (client->irq && chip->data.has_soc_alert) + // disable soc alert to prevent wakeup + max17040_set_soc_alert(chip, 0); + else + cancel_delayed_work(&chip->work); if (client->irq && device_may_wakeup(dev)) enable_irq_wake(client->irq); @@ -474,7 +535,10 @@ static int max17040_resume(struct device *dev) if (client->irq && device_may_wakeup(dev)) disable_irq_wake(client->irq); - max17040_queue_work(chip); + if (client->irq && chip->data.has_soc_alert) + max17040_set_soc_alert(chip, 1); + else + max17040_queue_work(chip); return 0; }