From patchwork Fri Feb 25 00:13:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 546106 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 36838C433F5 for ; Fri, 25 Feb 2022 00:15:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231713AbiBYAP6 (ORCPT ); Thu, 24 Feb 2022 19:15:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56878 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229531AbiBYAP5 (ORCPT ); Thu, 24 Feb 2022 19:15:57 -0500 Received: from mail-lf1-x132.google.com (mail-lf1-x132.google.com [IPv6:2a00:1450:4864:20::132]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1BFB52692F8 for ; Thu, 24 Feb 2022 16:15:26 -0800 (PST) Received: by mail-lf1-x132.google.com with SMTP id j7so6666437lfu.6 for ; Thu, 24 Feb 2022 16:15:26 -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 :mime-version:content-transfer-encoding; bh=80lALybd4B4C324WnBemdZ34mj5V1US+2MSBKMwmNBM=; b=s0uZp9S3P6txw14m80+NHvCNWED/E5zYqzMP/FitwS8zcEWFtAHcchULu+Ya7bgVzq NlzyKaPC8Zsu7Zzm6xkLvNqQofEueh3sRf/2xM4g3p44wBttnrqky5kH7J9waJABRiFn 534+ld4qJc+fxEJ0lrijVt0TfwLlJwj9z5DLsULs3klCwE5nmkCjZhPPay8UBrpMQs65 bdKiwnk56pqcQ2Cs11TFF0Oq4zRD0zyAK2O6NPZvu0U2C5SCVZXke1bb2gSxXG2St+FF UKUj9utHvBx6Y4oevjXTZdmz6Sejde7yRvTCjcsEiUsJUStXK8iAt5RxyX/WablDVjOT +y1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=80lALybd4B4C324WnBemdZ34mj5V1US+2MSBKMwmNBM=; b=njFAa1furroCDVHrQlJrMRaWOVSDsXhUhtJsmFWU3UENYSzvIX7veKcEowlVwL+fQg +/ZgY5PlTZvILhB9o6RYNEOOalcDyIQUwEaWkI9JmqlCEaETlBH3R3Qt+zKM2YUSdKsU ms5xk2a07IKNRtmyslw9UkNS/9j1kvjzsVo+qg/we2ojwbJuwB3Zg4ZXUlS8D8aTNvm2 Mdyjx7cxs8Nx6A5DDWZsa6/vrDTipxZnO5HqAWuGEYtOhvnllbkq9zTM8I8CR2a+Ho2S fXVFYWwJKY1Nclv4DVvp3amcyCIKOX7xklAaWfKJ5YJsGO5nL83TKFNgPvIFobUH91Xu enlg== X-Gm-Message-State: AOAM531/We7SIiARo94jVL/ERkVCm0ANGRgdeif1zMrQRG8C1xBIvCFy oq9cmHbMiofNQ/r2BZ6bTDrDLPp3p6puqOUj X-Google-Smtp-Source: ABdhPJyaFRdRb7Zdtc3NkPhQtJD1rFMW7j4u+hGf3iXWnTOh2fstixJxOCQ7kuLj+/mbbQC/I/qS8w== X-Received: by 2002:a05:6512:a92:b0:444:65e:732a with SMTP id m18-20020a0565120a9200b00444065e732amr265469lfu.305.1645748123986; Thu, 24 Feb 2022 16:15:23 -0800 (PST) Received: from localhost.localdomain (c-fdcc225c.014-348-6c756e10.bbcust.telenor.se. [92.34.204.253]) by smtp.gmail.com with ESMTPSA id 16-20020ac25f10000000b00443890bd84asm55859lfq.114.2022.02.24.16.15.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Feb 2022 16:15:23 -0800 (PST) From: Linus Walleij To: Sebastian Reichel , Marcus Cooper Cc: linux-pm@vger.kernel.org, Matti Vaittinen , Linus Walleij Subject: [PATCH 1/6 v3] power: supply: ab8500: Standardize maintenance charging Date: Fri, 25 Feb 2022 01:13:09 +0100 Message-Id: <20220225001314.1881549-2-linus.walleij@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220225001314.1881549-1-linus.walleij@linaro.org> References: <20220225001314.1881549-1-linus.walleij@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Maintenance charging is the phase of keeping up the charge after the battery has charged fully using CC/CV charging. This can be done in many successive phases and is usually done with a slightly lower constant voltage than CV, and a slightly lower allowed current. Add an array of maintenance charging points each with a current, voltage and safety timer, and add helper functions to use these. Migrate the AB8500 code over. This is used in several Samsung products using the AB8500 and these batteries and their complete parameters will be added later as full examples, but the default battery in the AB8500 code serves as a reasonable example so far. Reviewed-by: Matti Vaittinen Signed-off-by: Linus Walleij --- ChangeLog v2->v3: - Collect Matti's review tag ChangeLog v1->v2: - Rename default maintenance charge table ab8500_maint_charg_table - Mark ab8500_maint_charg_table static --- drivers/power/supply/ab8500-bm.h | 14 ------ drivers/power/supply/ab8500_bmdata.c | 27 +++++++--- drivers/power/supply/ab8500_chargalg.c | 41 +++++++++++---- drivers/power/supply/power_supply_core.c | 11 ++++ include/linux/power_supply.h | 64 ++++++++++++++++++++++++ 5 files changed, 126 insertions(+), 31 deletions(-) diff --git a/drivers/power/supply/ab8500-bm.h b/drivers/power/supply/ab8500-bm.h index 6efd5174dbce..4d74d21cf1eb 100644 --- a/drivers/power/supply/ab8500-bm.h +++ b/drivers/power/supply/ab8500-bm.h @@ -331,24 +331,12 @@ struct ab8500_maxim_parameters { * struct ab8500_battery_type - different batteries supported * @resis_high: battery upper resistance limit * @resis_low: battery lower resistance limit - * @maint_a_cur_lvl: charger current in maintenance A state in mA - * @maint_a_vol_lvl: charger voltage in maintenance A state in mV - * @maint_a_chg_timer_h: charge time in maintenance A state - * @maint_b_cur_lvl: charger current in maintenance B state in mA - * @maint_b_vol_lvl: charger voltage in maintenance B state in mV - * @maint_b_chg_timer_h: charge time in maintenance B state * @low_high_cur_lvl: charger current in temp low/high state in mA * @low_high_vol_lvl: charger voltage in temp low/high state in mV' */ struct ab8500_battery_type { int resis_high; int resis_low; - int maint_a_cur_lvl; - int maint_a_vol_lvl; - int maint_a_chg_timer_h; - int maint_b_cur_lvl; - int maint_b_vol_lvl; - int maint_b_chg_timer_h; int low_high_cur_lvl; int low_high_vol_lvl; }; @@ -393,7 +381,6 @@ struct ab8500_bm_charger_parameters { * @usb_safety_tmr_h safety timer for usb charger * @bkup_bat_v voltage which we charge the backup battery with * @bkup_bat_i current which we charge the backup battery with - * @no_maintenance indicates that maintenance charging is disabled * @capacity_scaling indicates whether capacity scaling is to be used * @chg_unknown_bat flag to enable charging of unknown batteries * @enable_overshoot flag to enable VBAT overshoot control @@ -417,7 +404,6 @@ struct ab8500_bm_data { int usb_safety_tmr_h; int bkup_bat_v; int bkup_bat_i; - bool no_maintenance; bool capacity_scaling; bool chg_unknown_bat; bool enable_overshoot; diff --git a/drivers/power/supply/ab8500_bmdata.c b/drivers/power/supply/ab8500_bmdata.c index d8fc72be0f0e..66a454942c7c 100644 --- a/drivers/power/supply/ab8500_bmdata.c +++ b/drivers/power/supply/ab8500_bmdata.c @@ -58,16 +58,25 @@ static struct power_supply_resistance_temp_table temp_to_batres_tbl_thermistor[] { .temp = -20, .resistance = 198 /* 595 mOhm */ }, }; +static struct power_supply_maintenance_charge_table ab8500_maint_charg_table[] = { + { + /* Maintenance charging phase A, 60 hours */ + .charge_current_max_ua = 400000, + .charge_voltage_max_uv = 4050000, + .charge_safety_timer_minutes = 60*60, + }, + { + /* Maintenance charging phase B, 200 hours */ + .charge_current_max_ua = 400000, + .charge_voltage_max_uv = 4000000, + .charge_safety_timer_minutes = 200*60, + } +}; + /* Default battery type for reference designs is the unknown type */ static struct ab8500_battery_type bat_type_thermistor_unknown = { .resis_high = 0, .resis_low = 0, - .maint_a_cur_lvl = 400, - .maint_a_vol_lvl = 4050, - .maint_a_chg_timer_h = 60, - .maint_b_cur_lvl = 400, - .maint_b_vol_lvl = 4000, - .maint_b_chg_timer_h = 200, .low_high_cur_lvl = 300, .low_high_vol_lvl = 4000, }; @@ -124,7 +133,6 @@ struct ab8500_bm_data ab8500_bm_data = { .usb_safety_tmr_h = 4, .bkup_bat_v = BUP_VCH_SEL_2P6V, .bkup_bat_i = BUP_ICH_SEL_150UA, - .no_maintenance = false, .capacity_scaling = false, .chg_unknown_bat = false, .enable_overshoot = false, @@ -179,6 +187,11 @@ int ab8500_bm_of_probe(struct power_supply *psy, /* Charging stops when we drop below this current */ bi->charge_term_current_ua = 200000; + if (!bi->maintenance_charge || !bi->maintenance_charge_size) { + bi->maintenance_charge = ab8500_maint_charg_table; + bi->maintenance_charge_size = ARRAY_SIZE(ab8500_maint_charg_table); + } + /* * Internal resistance and factory resistance are tightly coupled * so both MUST be defined or we fall back to defaults. diff --git a/drivers/power/supply/ab8500_chargalg.c b/drivers/power/supply/ab8500_chargalg.c index b5a3096e78a1..6054996b6260 100644 --- a/drivers/power/supply/ab8500_chargalg.c +++ b/drivers/power/supply/ab8500_chargalg.c @@ -430,7 +430,7 @@ static void ab8500_chargalg_stop_safety_timer(struct ab8500_chargalg *di) /** * ab8500_chargalg_start_maintenance_timer() - Start charging maintenance timer * @di: pointer to the ab8500_chargalg structure - * @duration: duration of ther maintenance timer in hours + * @duration: duration of ther maintenance timer in minutes * * The maintenance timer is used to maintain the charge in the battery once * the battery is considered full. These timers are chosen to match the @@ -439,9 +439,10 @@ static void ab8500_chargalg_stop_safety_timer(struct ab8500_chargalg *di) static void ab8500_chargalg_start_maintenance_timer(struct ab8500_chargalg *di, int duration) { + /* Set a timer in minutes with a 30 second range */ hrtimer_set_expires_range(&di->maintenance_timer, - ktime_set(duration * ONE_HOUR_IN_SECONDS, 0), - ktime_set(FIVE_MINUTES_IN_SECONDS, 0)); + ktime_set(duration * 60, 0), + ktime_set(30, 0)); di->events.maintenance_timer_expired = false; hrtimer_start_expires(&di->maintenance_timer, HRTIMER_MODE_REL); } @@ -1223,6 +1224,7 @@ static void ab8500_chargalg_external_power_changed(struct power_supply *psy) static void ab8500_chargalg_algorithm(struct ab8500_chargalg *di) { struct power_supply_battery_info *bi = di->bm->bi; + struct power_supply_maintenance_charge_table *mt; int charger_status; int ret; @@ -1433,7 +1435,12 @@ static void ab8500_chargalg_algorithm(struct ab8500_chargalg *di) handle_maxim_chg_curr(di); if (di->charge_status == POWER_SUPPLY_STATUS_FULL && di->maintenance_chg) { - if (di->bm->no_maintenance) + /* + * The battery is fully charged, check if we support + * maintenance charging else go back to waiting for + * the recharge voltage limit. + */ + if (!power_supply_supports_maintenance_charging(bi)) ab8500_chargalg_state_to(di, STATE_WAIT_FOR_RECHARGE_INIT); else @@ -1454,12 +1461,19 @@ static void ab8500_chargalg_algorithm(struct ab8500_chargalg *di) break; case STATE_MAINTENANCE_A_INIT: + mt = power_supply_get_maintenance_charging_setting(bi, 0); + if (!mt) { + /* No maintenance A state, go back to normal */ + ab8500_chargalg_state_to(di, STATE_NORMAL_INIT); + power_supply_changed(di->chargalg_psy); + break; + } ab8500_chargalg_stop_safety_timer(di); ab8500_chargalg_start_maintenance_timer(di, - di->bm->bat_type->maint_a_chg_timer_h); + mt->charge_safety_timer_minutes); ab8500_chargalg_start_charging(di, - di->bm->bat_type->maint_a_vol_lvl, - di->bm->bat_type->maint_a_cur_lvl); + mt->charge_voltage_max_uv, + mt->charge_current_max_ua); ab8500_chargalg_state_to(di, STATE_MAINTENANCE_A); power_supply_changed(di->chargalg_psy); fallthrough; @@ -1472,11 +1486,18 @@ static void ab8500_chargalg_algorithm(struct ab8500_chargalg *di) break; case STATE_MAINTENANCE_B_INIT: + mt = power_supply_get_maintenance_charging_setting(bi, 1); + if (!mt) { + /* No maintenance B state, go back to normal */ + ab8500_chargalg_state_to(di, STATE_NORMAL_INIT); + power_supply_changed(di->chargalg_psy); + break; + } ab8500_chargalg_start_maintenance_timer(di, - di->bm->bat_type->maint_b_chg_timer_h); + mt->charge_safety_timer_minutes); ab8500_chargalg_start_charging(di, - di->bm->bat_type->maint_b_vol_lvl, - di->bm->bat_type->maint_b_cur_lvl); + mt->charge_voltage_max_uv, + mt->charge_current_max_ua); ab8500_chargalg_state_to(di, STATE_MAINTENANCE_B); power_supply_changed(di->chargalg_psy); fallthrough; diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c index ec838c9bcc0a..6568939e4518 100644 --- a/drivers/power/supply/power_supply_core.c +++ b/drivers/power/supply/power_supply_core.c @@ -590,6 +590,7 @@ int power_supply_get_battery_info(struct power_supply *psy, info->precharge_voltage_max_uv = -EINVAL; info->charge_restart_voltage_uv = -EINVAL; info->overvoltage_limit_uv = -EINVAL; + info->maintenance_charge = NULL; info->temp_ambient_alert_min = INT_MIN; info->temp_ambient_alert_max = INT_MAX; info->temp_alert_min = INT_MIN; @@ -821,6 +822,16 @@ int power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *t } EXPORT_SYMBOL_GPL(power_supply_temp2resist_simple); +struct power_supply_maintenance_charge_table * +power_supply_get_maintenance_charging_setting(struct power_supply_battery_info *info, + int index) +{ + if (index >= info->maintenance_charge_size) + return NULL; + return &info->maintenance_charge[index]; +} +EXPORT_SYMBOL_GPL(power_supply_get_maintenance_charging_setting); + /** * power_supply_ocv2cap_simple() - find the battery capacity * @table: Pointer to battery OCV lookup table diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h index e218041cc000..b998fc4c87ae 100644 --- a/include/linux/power_supply.h +++ b/include/linux/power_supply.h @@ -348,6 +348,52 @@ struct power_supply_resistance_temp_table { int resistance; /* internal resistance percent */ }; +/** + * struct power_supply_maintenance_charge_table - setting for maintenace charging + * @charge_current_max_ua: maintenance charging current that is used to keep + * the charge of the battery full as current is consumed after full charging. + * The corresponding charge_voltage_max_uv is used as a safeguard: when we + * reach this voltage the maintenance charging current is turned off. It is + * turned back on if we fall below this voltage. + * @charge_voltage_max_uv: maintenance charging voltage that is usually a bit + * lower than the constant_charge_voltage_max_uv. We can apply this settings + * charge_current_max_ua until we get back up to this voltage. + * @safety_timer_minutes: maintenance charging safety timer, with an expiry + * time in minutes. We will only use maintenance charging in this setting + * for a certain amount of time, then we will first move to the next + * maintenance charge current and voltage pair in respective array and wait + * for the next safety timer timeout, or, if we reached the last maintencance + * charging setting, disable charging until we reach + * charge_restart_voltage_uv and restart ordinary CC/CV charging from there. + * These timers should be chosen to align with the typical discharge curve + * for the battery. + * + * When the main CC/CV charging is complete the battery can optionally be + * maintenance charged at the voltages from this table: a table of settings is + * traversed using a slightly lower current and voltage than what is used for + * CC/CV charging. The maintenance charging will for safety reasons not go on + * indefinately: we lower the current and voltage with successive maintenance + * settings, then disable charging completely after we reach the last one, + * and after that we do not restart charging until we reach + * charge_restart_voltage_uv (see struct power_supply_battery_info) and restart + * ordinary CC/CV charging from there. + * + * As an example, a Samsung EB425161LA Lithium-Ion battery is CC/CV charged + * at 900mA to 4340mV, then maintenance charged at 600mA and 4150mV for + * 60 hours, then maintenance charged at 600mA and 4100mV for 200 hours. + * After this the charge cycle is restarted waiting for + * charge_restart_voltage_uv. + * + * For most mobile electronics this type of maintenance charging is enough for + * the user to disconnect the device and make use of it before both maintenance + * charging cycles are complete. + */ +struct power_supply_maintenance_charge_table { + int charge_current_max_ua; + int charge_voltage_max_uv; + int charge_safety_timer_minutes; +}; + #define POWER_SUPPLY_OCV_TEMP_MAX 20 /** @@ -393,6 +439,10 @@ struct power_supply_resistance_temp_table { * @constant_charge_voltage_max_uv: voltage in microvolts signifying the end of * the CC (constant current) charging phase and the beginning of the CV * (constant voltage) charging phase. + * @maintenance_charge: an array of maintenance charging settings to be used + * after the main CC/CV charging phase is complete. + * @maintenance_charge_size: the number of maintenance charging settings in + * maintenance_charge. * @factory_internal_resistance_uohm: the internal resistance of the battery * at fabrication time, expressed in microohms. This resistance will vary * depending on the lifetime and charge of the battery, so this is just a @@ -542,6 +592,8 @@ struct power_supply_battery_info { int overvoltage_limit_uv; int constant_charge_current_max_ua; int constant_charge_voltage_max_uv; + struct power_supply_maintenance_charge_table *maintenance_charge; + int maintenance_charge_size; int factory_internal_resistance_uohm; int ocv_temp[POWER_SUPPLY_OCV_TEMP_MAX]; int temp_ambient_alert_min; @@ -595,12 +647,24 @@ extern int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info, extern int power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *table, int table_len, int temp); +extern struct power_supply_maintenance_charge_table * +power_supply_get_maintenance_charging_setting(struct power_supply_battery_info *info, int index); extern void power_supply_changed(struct power_supply *psy); extern int power_supply_am_i_supplied(struct power_supply *psy); extern int power_supply_set_input_current_limit_from_supplier( struct power_supply *psy); extern int power_supply_set_battery_charged(struct power_supply *psy); +static inline bool +power_supply_supports_maintenance_charging(struct power_supply_battery_info *info) +{ + struct power_supply_maintenance_charge_table *mt; + + mt = power_supply_get_maintenance_charging_setting(info, 0); + + return (mt != NULL); +} + #ifdef CONFIG_POWER_SUPPLY extern int power_supply_is_system_supplied(void); #else From patchwork Fri Feb 25 00:13:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 546373 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 98491C433EF for ; Fri, 25 Feb 2022 00:15:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229531AbiBYAP7 (ORCPT ); Thu, 24 Feb 2022 19:15:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231247AbiBYAP6 (ORCPT ); Thu, 24 Feb 2022 19:15:58 -0500 Received: from mail-lf1-x129.google.com (mail-lf1-x129.google.com [IPv6:2a00:1450:4864:20::129]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A7E22692FC for ; Thu, 24 Feb 2022 16:15:27 -0800 (PST) Received: by mail-lf1-x129.google.com with SMTP id b9so6656903lfv.7 for ; Thu, 24 Feb 2022 16:15:27 -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 :mime-version:content-transfer-encoding; bh=jhZqumWiQOsDPbjWa5PcQ3R6sfjJQUAoD7px6LyGo7E=; b=I5fo2iBhhNkRtYGtltOXJ1VXosu3TxzNwKMBD37Bmk1Fn0XS8hOVnCwJ0rq66hXqm7 04/l+1yu2YNyKHPEwIYcV2iY9Qf3sZyTTbrLryWUQJMGo5KmC9W6WwJ9/REuPWnCxLLb nVWxGBNRkhw7dsm9bhv6MKHM28voBc/31pfWvbQ1IAcQ8aq/9lCz4SoEExKQL8CLohD3 gSyRV9iZXGFft6yD2REW3dG7YmPflqIwTzzz7DJtgS0RumcUGrhDglpvUhgwNB/4FIAW QSNUO4Lp7sT/hzcNwrCpZasWmfBV4d4Ead8XWBjcjf4Ud6VkhfHsmSrdq6sw9Bxq3Bfa GZHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jhZqumWiQOsDPbjWa5PcQ3R6sfjJQUAoD7px6LyGo7E=; b=bBEWUFrarW+r/BLqrpSKvhStp+pSCnQgdVKQU/JOKG7vJvpeKnmZd/OvGbKoqqFx1o Hpgh6JBvWjLPTlDxsv0ITMiuUEeXHdC7cEo5zSkrRvysSQzfkn+KDuhKLrurTFjAdoqq zsVwuTkHY5SIEVsDMFUYZ7Ekjtj2ukEUYjBH0fg1WFrvyn4K51KF1w/MPlKcwS+vGj73 i61TnLJLrVyb/D5CQGIFHzTqeqVUrTinteNDbq91TsPefrhHnmiN6g6/cd8EIDPui7UN A6hm4vxJ0v7BEV/PSOgyd5CuMnmBxI8GVQWpsJ5RaYFukNlTuW+tuWc2KHxzVpbE8bUU C6Sg== X-Gm-Message-State: AOAM533kDa0hUyHElstlEWNL6H8mLKar5ZFzvxuBpf/bIP0m13jPZRFk hjXjzEHlSdFhkPhv4zKd5zsWEz3FXv4sgQKo X-Google-Smtp-Source: ABdhPJzZOsPJk1ufHStFNSXeF3IqsrXuqPIhrmACVH5eT0OEnqSWgGr4NLD/sN5+uv3fr5acTps/5g== X-Received: by 2002:ac2:5fe1:0:b0:443:4e28:7ccb with SMTP id s1-20020ac25fe1000000b004434e287ccbmr3289133lfg.30.1645748125498; Thu, 24 Feb 2022 16:15:25 -0800 (PST) Received: from localhost.localdomain (c-fdcc225c.014-348-6c756e10.bbcust.telenor.se. [92.34.204.253]) by smtp.gmail.com with ESMTPSA id 16-20020ac25f10000000b00443890bd84asm55859lfq.114.2022.02.24.16.15.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Feb 2022 16:15:25 -0800 (PST) From: Linus Walleij To: Sebastian Reichel , Marcus Cooper Cc: linux-pm@vger.kernel.org, Matti Vaittinen , Linus Walleij Subject: [PATCH 2/6 v3] power: supply: ab8500: Standardize alert mode charging Date: Fri, 25 Feb 2022 01:13:10 +0100 Message-Id: <20220225001314.1881549-3-linus.walleij@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220225001314.1881549-1-linus.walleij@linaro.org> References: <20220225001314.1881549-1-linus.walleij@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org The AB8500 code is using a special current and voltage setting when the battery is in "alert mode", i.e. when it is starting to go outside normal operating conditions so it is too cold or too hot. This makes sense as a way for the charging algorithm to deal with hostile environments. Add the needed members to the struct power_supply_battery_info, and switch the AB8500 charging code over to using this. Reviewed-by: Matti Vaittineen Signed-off-by: Linus Walleij --- ChangeLog v2->v3: - Collect Matti's Review tag ChangeLog v1->v2: - Split the combined alert temperature state current and voltage into two: one for low temperature and one for high temperature. - Augment the AB8500 charging algorithm to keep track of if the battery got too cold or too warm - Utilize the different voltages in the AB8500 driver. --- drivers/power/supply/ab8500-bm.h | 4 -- drivers/power/supply/ab8500_bmdata.c | 15 +++++++- drivers/power/supply/ab8500_chargalg.c | 48 ++++++++++++++++-------- drivers/power/supply/power_supply_core.c | 4 ++ include/linux/power_supply.h | 17 +++++++++ 5 files changed, 66 insertions(+), 22 deletions(-) diff --git a/drivers/power/supply/ab8500-bm.h b/drivers/power/supply/ab8500-bm.h index 4d74d21cf1eb..91ef9d4a5222 100644 --- a/drivers/power/supply/ab8500-bm.h +++ b/drivers/power/supply/ab8500-bm.h @@ -331,14 +331,10 @@ struct ab8500_maxim_parameters { * struct ab8500_battery_type - different batteries supported * @resis_high: battery upper resistance limit * @resis_low: battery lower resistance limit - * @low_high_cur_lvl: charger current in temp low/high state in mA - * @low_high_vol_lvl: charger voltage in temp low/high state in mV' */ struct ab8500_battery_type { int resis_high; int resis_low; - int low_high_cur_lvl; - int low_high_vol_lvl; }; /** diff --git a/drivers/power/supply/ab8500_bmdata.c b/drivers/power/supply/ab8500_bmdata.c index 66a454942c7c..bf0b74773eee 100644 --- a/drivers/power/supply/ab8500_bmdata.c +++ b/drivers/power/supply/ab8500_bmdata.c @@ -77,8 +77,6 @@ static struct power_supply_maintenance_charge_table ab8500_maint_charg_table[] = static struct ab8500_battery_type bat_type_thermistor_unknown = { .resis_high = 0, .resis_low = 0, - .low_high_cur_lvl = 300, - .low_high_vol_lvl = 4000, }; static const struct ab8500_bm_capacity_levels cap_levels = { @@ -192,6 +190,19 @@ int ab8500_bm_of_probe(struct power_supply *psy, bi->maintenance_charge_size = ARRAY_SIZE(ab8500_maint_charg_table); } + if (bi->alert_low_temp_charge_current_ua < 0 || + bi->alert_low_temp_charge_voltage_uv < 0) + { + bi->alert_low_temp_charge_current_ua = 300000; + bi->alert_low_temp_charge_voltage_uv = 4000000; + } + if (bi->alert_high_temp_charge_current_ua < 0 || + bi->alert_high_temp_charge_voltage_uv < 0) + { + bi->alert_high_temp_charge_current_ua = 300000; + bi->alert_high_temp_charge_voltage_uv = 4000000; + } + /* * Internal resistance and factory resistance are tightly coupled * so both MUST be defined or we fall back to defaults. diff --git a/drivers/power/supply/ab8500_chargalg.c b/drivers/power/supply/ab8500_chargalg.c index 6054996b6260..c9c7f7028af6 100644 --- a/drivers/power/supply/ab8500_chargalg.c +++ b/drivers/power/supply/ab8500_chargalg.c @@ -149,7 +149,8 @@ struct ab8500_chargalg_events { bool batt_ovv; bool batt_rem; bool btemp_underover; - bool btemp_lowhigh; + bool btemp_low; + bool btemp_high; bool main_thermal_prot; bool usb_thermal_prot; bool main_ovv; @@ -684,26 +685,31 @@ static void ab8500_chargalg_check_temp(struct ab8500_chargalg *di) di->batt_data.temp < (bi->temp_alert_max - di->t_hyst_norm)) { /* Temp OK! */ di->events.btemp_underover = false; - di->events.btemp_lowhigh = false; + di->events.btemp_low = false; + di->events.btemp_high = false; di->t_hyst_norm = 0; di->t_hyst_lowhigh = 0; } else { - if (((di->batt_data.temp >= bi->temp_alert_max) && - (di->batt_data.temp < - (bi->temp_max - di->t_hyst_lowhigh))) || - ((di->batt_data.temp > - (bi->temp_min + di->t_hyst_lowhigh)) && - (di->batt_data.temp <= bi->temp_alert_min))) { - /* TEMP minor!!!!! */ + if ((di->batt_data.temp >= bi->temp_alert_max) && + (di->batt_data.temp < (bi->temp_max - di->t_hyst_lowhigh))) { + /* Alert zone for high temperature */ di->events.btemp_underover = false; - di->events.btemp_lowhigh = true; + di->events.btemp_high = true; + di->t_hyst_norm = di->bm->temp_hysteresis; + di->t_hyst_lowhigh = 0; + } else if ((di->batt_data.temp > (bi->temp_min + di->t_hyst_lowhigh)) && + (di->batt_data.temp <= bi->temp_alert_min)) { + /* Alert zone for low temperature */ + di->events.btemp_underover = false; + di->events.btemp_low = true; di->t_hyst_norm = di->bm->temp_hysteresis; di->t_hyst_lowhigh = 0; } else if (di->batt_data.temp <= bi->temp_min || di->batt_data.temp >= bi->temp_max) { /* TEMP major!!!!! */ di->events.btemp_underover = true; - di->events.btemp_lowhigh = false; + di->events.btemp_low = false; + di->events.btemp_high = false; di->t_hyst_norm = 0; di->t_hyst_lowhigh = di->bm->temp_hysteresis; } else { @@ -1313,7 +1319,7 @@ static void ab8500_chargalg_algorithm(struct ab8500_chargalg *di) ab8500_chargalg_state_to(di, STATE_WD_EXPIRED_INIT); } /* Battery temp high/low */ - else if (di->events.btemp_lowhigh) { + else if (di->events.btemp_low || di->events.btemp_high) { if (di->charge_state != STATE_TEMP_LOWHIGH) ab8500_chargalg_state_to(di, STATE_TEMP_LOWHIGH_INIT); } @@ -1510,9 +1516,19 @@ static void ab8500_chargalg_algorithm(struct ab8500_chargalg *di) break; case STATE_TEMP_LOWHIGH_INIT: - ab8500_chargalg_start_charging(di, - di->bm->bat_type->low_high_vol_lvl, - di->bm->bat_type->low_high_cur_lvl); + if (di->events.btemp_low) { + ab8500_chargalg_start_charging(di, + bi->alert_low_temp_charge_voltage_uv, + bi->alert_low_temp_charge_current_ua); + } else if (di->events.btemp_high) { + ab8500_chargalg_start_charging(di, + bi->alert_high_temp_charge_voltage_uv, + bi->alert_high_temp_charge_current_ua); + } else { + dev_err(di->dev, "neither low or high temp event occured\n"); + ab8500_chargalg_state_to(di, STATE_NORMAL_INIT); + break; + } ab8500_chargalg_stop_maintenance_timer(di); di->charge_status = POWER_SUPPLY_STATUS_CHARGING; ab8500_chargalg_state_to(di, STATE_TEMP_LOWHIGH); @@ -1520,7 +1536,7 @@ static void ab8500_chargalg_algorithm(struct ab8500_chargalg *di) fallthrough; case STATE_TEMP_LOWHIGH: - if (!di->events.btemp_lowhigh) + if (!di->events.btemp_low && !di->events.btemp_high) ab8500_chargalg_state_to(di, STATE_NORMAL_INIT); break; diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c index 6568939e4518..5d7d15860529 100644 --- a/drivers/power/supply/power_supply_core.c +++ b/drivers/power/supply/power_supply_core.c @@ -591,6 +591,10 @@ int power_supply_get_battery_info(struct power_supply *psy, info->charge_restart_voltage_uv = -EINVAL; info->overvoltage_limit_uv = -EINVAL; info->maintenance_charge = NULL; + info->alert_low_temp_charge_current_ua = -EINVAL; + info->alert_low_temp_charge_voltage_uv = -EINVAL; + info->alert_high_temp_charge_current_ua = -EINVAL; + info->alert_high_temp_charge_voltage_uv = -EINVAL; info->temp_ambient_alert_min = INT_MIN; info->temp_ambient_alert_max = INT_MAX; info->temp_alert_min = INT_MIN; diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h index b998fc4c87ae..42a47d7aa3fd 100644 --- a/include/linux/power_supply.h +++ b/include/linux/power_supply.h @@ -443,6 +443,19 @@ struct power_supply_maintenance_charge_table { * after the main CC/CV charging phase is complete. * @maintenance_charge_size: the number of maintenance charging settings in * maintenance_charge. + * @alert_low_temp_charge_current_ua: The charging current to use if the battery + * enters low alert temperature, i.e. if the internal temperature is between + * temp_alert_min and temp_min. No matter the charging phase, this + * and alert_high_temp_charge_voltage_uv will be applied. + * @alert_low_temp_charge_voltage_uv: Same as alert_low_temp_charge_current_ua, + * but for the charging voltage. + * @alert_high_temp_charge_current_ua: The charging current to use if the + * battery enters high alert temperature, i.e. if the internal temperature is + * between temp_alert_max and temp_max. No matter the charging phase, this + * and alert_high_temp_charge_voltage_uv will be applied, usually lowering + * the charging current as an evasive manouver. + * @alert_high_temp_charge_voltage_uv: Same as + * alert_high_temp_charge_current_ua, but for the charging voltage. * @factory_internal_resistance_uohm: the internal resistance of the battery * at fabrication time, expressed in microohms. This resistance will vary * depending on the lifetime and charge of the battery, so this is just a @@ -594,6 +607,10 @@ struct power_supply_battery_info { int constant_charge_voltage_max_uv; struct power_supply_maintenance_charge_table *maintenance_charge; int maintenance_charge_size; + int alert_low_temp_charge_current_ua; + int alert_low_temp_charge_voltage_uv; + int alert_high_temp_charge_current_ua; + int alert_high_temp_charge_voltage_uv; int factory_internal_resistance_uohm; int ocv_temp[POWER_SUPPLY_OCV_TEMP_MAX]; int temp_ambient_alert_min; From patchwork Fri Feb 25 00:13:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 546105 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 5CB95C433FE for ; Fri, 25 Feb 2022 00:15:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231247AbiBYAQA (ORCPT ); Thu, 24 Feb 2022 19:16:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233276AbiBYAQA (ORCPT ); Thu, 24 Feb 2022 19:16:00 -0500 Received: from mail-lf1-x12e.google.com (mail-lf1-x12e.google.com [IPv6:2a00:1450:4864:20::12e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5F462692FC for ; Thu, 24 Feb 2022 16:15:28 -0800 (PST) Received: by mail-lf1-x12e.google.com with SMTP id f37so6622121lfv.8 for ; Thu, 24 Feb 2022 16:15:28 -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 :mime-version:content-transfer-encoding; bh=iV/EJYlCNMZZ5eyaaS7hYTgLYIqKGViA2YLNeTINVQw=; b=C0P48msRw2pf3ZWr9bdWDpy7JjKO90I/ML39PTzmHYdncL80ySlsUFWSv5fuQ54aDZ sc7PN1E1WkluSH85AyyzOAK7dtlzFO/Y4uoXxCY8V+b/7PPsvuxqjzOZhlz4WSdCyE2y q7UyPn14Jtgxl96qBle44aT1hO+/ZXf+UoO8SdEXHRoAeE4ErX91TEE0TpKHtgB8Jik4 hnbXApeTEsrXCcKR6CvJFL9iOv37jqpVpzIUN4KdINL5TA2fYHMsFw5ibGBUDbEz879l w+kyA6xh0DcGZAqaBEr66QbDx0Zjae55PkLSwMcD4QmOTqYwPKgoXJz/uDc7sB5n8lcV xw2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=iV/EJYlCNMZZ5eyaaS7hYTgLYIqKGViA2YLNeTINVQw=; b=eJrhq3o8pw3yRFW6JfPWhmHVyCO/UcrLzxJWu7Be82LH5cHe0LiImGb3sq/b811KJ0 yJjVGs8cjzhvdwceVic8/vq/D14POzYQcDYILIRnmV2m540fTNxY9HOj52e59jT++xM8 1+x0TaOtWLHqawLjkySMTOhbp/QLEvUqzkbWKM51JL8/8I1uRsS9VSYFop8mPiSAXOiH pGNjF8yhb7iX66vUkTet0GgL8SOwZmkmrvBdGqt4rEB9ylUSMG2xPjbr0KvjXnzq8E4S XSNrrx7b1n7lkbNcGM4Cyjb0NOUD4RBg871g5A2+s2KZUgHYCYH28X4ZsKVRcmUAO014 LDbg== X-Gm-Message-State: AOAM532sxCGwF6dOB3AxuL3QGm3M3QQHrmqFiVkkxRS3SBQAyMavG/tx SIVqbbI3SG+ya8foTZ1/sb+DPg== X-Google-Smtp-Source: ABdhPJwA+HD4Jopu9/oN56SpX3vRa/AsZwQhhtdc1IAiLfr4590zwhDQF5zEn6oFUqk57HQaWM1JOg== X-Received: by 2002:ac2:4da1:0:b0:438:74be:5a88 with SMTP id h1-20020ac24da1000000b0043874be5a88mr3077681lfe.210.1645748127111; Thu, 24 Feb 2022 16:15:27 -0800 (PST) Received: from localhost.localdomain (c-fdcc225c.014-348-6c756e10.bbcust.telenor.se. [92.34.204.253]) by smtp.gmail.com with ESMTPSA id 16-20020ac25f10000000b00443890bd84asm55859lfq.114.2022.02.24.16.15.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Feb 2022 16:15:26 -0800 (PST) From: Linus Walleij To: Sebastian Reichel , Marcus Cooper Cc: linux-pm@vger.kernel.org, Matti Vaittinen , Linus Walleij Subject: [PATCH 3/6 v3] power: supply: ab8500: Standardize BTI resistance Date: Fri, 25 Feb 2022 01:13:11 +0100 Message-Id: <20220225001314.1881549-4-linus.walleij@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220225001314.1881549-1-linus.walleij@linaro.org> References: <20220225001314.1881549-1-linus.walleij@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org The Battery Type Indicator (BTI) resistor is a resistor mounted between a special terminal on the battery and ground. By sending a fixed current (such as 7mA) through this resistor and measuring the voltage over it, the resistance can be determined, and this verifies the battery type. Typical side view of the battery: o o o GND BTI +3.8V Typical example of the electrical layout: +3.8 V BTI | | | + | _______ [ ] 7kOhm ___ | | | | | GND GND By verifying this resistance before attempting to charge the battery we add an additional level of security. In some systems this is used for plug-and-play of batteries with different capacity. In other cases, this is merely used to verify that the right type of battery is connected, if several batteries have the same physical shape and can be plugged into the same slot. Sometimes this is just a surplus security mechanism. Nokia and Samsung among many other vendors are known to use these BTI resistors. Add the BTI properties to struct power_supply_battery_info and switch the AB8500 charger code over to using it. Signed-off-by: Linus Walleij --- ChangeLog v2->v3: - No changes ChangeLog v1->v2: - No changes --- drivers/power/supply/ab8500-bm.h | 12 ----------- drivers/power/supply/ab8500_bmdata.c | 14 ++++++------- drivers/power/supply/ab8500_btemp.c | 14 ++++++------- drivers/power/supply/ab8500_fg.c | 4 ---- drivers/power/supply/power_supply_core.c | 26 +++++++++++++++++++++++- include/linux/power_supply.h | 13 ++++++++++++ 6 files changed, 51 insertions(+), 32 deletions(-) diff --git a/drivers/power/supply/ab8500-bm.h b/drivers/power/supply/ab8500-bm.h index 91ef9d4a5222..180a016b3662 100644 --- a/drivers/power/supply/ab8500-bm.h +++ b/drivers/power/supply/ab8500-bm.h @@ -327,16 +327,6 @@ struct ab8500_maxim_parameters { int charger_curr_step_ua; }; -/** - * struct ab8500_battery_type - different batteries supported - * @resis_high: battery upper resistance limit - * @resis_low: battery lower resistance limit - */ -struct ab8500_battery_type { - int resis_high; - int resis_low; -}; - /** * struct ab8500_bm_capacity_levels - ab8500 capacity level data * @critical: critical capacity level in percent @@ -387,7 +377,6 @@ struct ab8500_bm_charger_parameters { * @temp_hysteresis temperature hysteresis * @maxi maximization parameters * @cap_levels capacity in percent for the different capacity levels - * @bat_type table of supported battery types * @chg_params charger parameters * @fg_params fuel gauge parameters */ @@ -410,7 +399,6 @@ struct ab8500_bm_data { int temp_hysteresis; const struct ab8500_maxim_parameters *maxi; const struct ab8500_bm_capacity_levels *cap_levels; - struct ab8500_battery_type *bat_type; const struct ab8500_bm_charger_parameters *chg_params; const struct ab8500_fg_parameters *fg_params; }; diff --git a/drivers/power/supply/ab8500_bmdata.c b/drivers/power/supply/ab8500_bmdata.c index bf0b74773eee..3e6ea22372b2 100644 --- a/drivers/power/supply/ab8500_bmdata.c +++ b/drivers/power/supply/ab8500_bmdata.c @@ -73,12 +73,6 @@ static struct power_supply_maintenance_charge_table ab8500_maint_charg_table[] = } }; -/* Default battery type for reference designs is the unknown type */ -static struct ab8500_battery_type bat_type_thermistor_unknown = { - .resis_high = 0, - .resis_low = 0, -}; - static const struct ab8500_bm_capacity_levels cap_levels = { .critical = 2, .low = 10, @@ -136,7 +130,6 @@ struct ab8500_bm_data ab8500_bm_data = { .enable_overshoot = false, .fg_res = 100, .cap_levels = &cap_levels, - .bat_type = &bat_type_thermistor_unknown, .interval_charging = 5, .interval_not_charging = 120, .maxi = &ab8500_maxi_params, @@ -214,6 +207,13 @@ int ab8500_bm_of_probe(struct power_supply *psy, bi->resist_table_size = ARRAY_SIZE(temp_to_batres_tbl_thermistor); } + /* The default battery is emulated by a resistor at 7K */ + if (bi->bti_resistance_ohm < 0 || + bi->bti_resistance_tolerance < 0) { + bi->bti_resistance_ohm = 7000; + bi->bti_resistance_tolerance = 20; + } + if (!bi->ocv_table[0]) { /* Default capacity table at say 25 degrees Celsius */ bi->ocv_temp[0] = 25; diff --git a/drivers/power/supply/ab8500_btemp.c b/drivers/power/supply/ab8500_btemp.c index 2a6fc151210c..b7e842dff567 100644 --- a/drivers/power/supply/ab8500_btemp.c +++ b/drivers/power/supply/ab8500_btemp.c @@ -237,8 +237,8 @@ static int ab8500_btemp_get_batctrl_res(struct ab8500_btemp *di) */ static int ab8500_btemp_id(struct ab8500_btemp *di) { + struct power_supply_battery_info *bi = di->bm->bi; int res; - u8 i; di->curr_source = BTEMP_BATCTRL_CURR_SRC_7UA; @@ -248,13 +248,11 @@ static int ab8500_btemp_id(struct ab8500_btemp *di) return -ENXIO; } - if ((res <= di->bm->bat_type->resis_high) && - (res >= di->bm->bat_type->resis_low)) { - dev_info(di->dev, "Battery detected on BATTEMP" - " low %d < res %d < high: %d" - " index: %d\n", - di->bm->bat_type->resis_low, res, - di->bm->bat_type->resis_high, i); + if (power_supply_battery_bti_in_range(bi, res)) { + dev_info(di->dev, "Battery detected on BATCTRL (pin C3)" + " resistance %d Ohm = %d Ohm +/- %d%%\n", + res, bi->bti_resistance_ohm, + bi->bti_resistance_tolerance); } else { dev_warn(di->dev, "Battery identified as unknown" ", resistance %d Ohm\n", res); diff --git a/drivers/power/supply/ab8500_fg.c b/drivers/power/supply/ab8500_fg.c index 6436861db016..39c7e6b0be52 100644 --- a/drivers/power/supply/ab8500_fg.c +++ b/drivers/power/supply/ab8500_fg.c @@ -2241,10 +2241,6 @@ static int ab8500_fg_get_ext_psy_data(struct device *dev, void *data) if (!di->flags.batt_id_received && (bi && (bi->technology != POWER_SUPPLY_TECHNOLOGY_UNKNOWN))) { - const struct ab8500_battery_type *b; - - b = di->bm->bat_type; - di->flags.batt_id_received = true; di->bat_cap.max_mah_design = diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c index 5d7d15860529..cbe957088c56 100644 --- a/drivers/power/supply/power_supply_core.c +++ b/drivers/power/supply/power_supply_core.c @@ -602,7 +602,9 @@ int power_supply_get_battery_info(struct power_supply *psy, info->temp_min = INT_MIN; info->temp_max = INT_MAX; info->factory_internal_resistance_uohm = -EINVAL; - info->resist_table = NULL; + info->resist_table = NULL; + info->bti_resistance_ohm = -EINVAL; + info->bti_resistance_tolerance = -EINVAL; for (index = 0; index < POWER_SUPPLY_OCV_TEMP_MAX; index++) { info->ocv_table[index] = NULL; @@ -915,6 +917,28 @@ int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info, } EXPORT_SYMBOL_GPL(power_supply_batinfo_ocv2cap); +bool power_supply_battery_bti_in_range(struct power_supply_battery_info *info, + int resistance) +{ + int low, high; + + /* Nothing like this can be checked */ + if (info->bti_resistance_ohm <= 0) + return false; + + /* This will be extremely strict and unlikely to work */ + if (info->bti_resistance_tolerance <= 0) + return (info->bti_resistance_ohm == resistance); + + low = info->bti_resistance_ohm - + (info->bti_resistance_ohm * info->bti_resistance_tolerance) / 100; + high = info->bti_resistance_ohm + + (info->bti_resistance_ohm * info->bti_resistance_tolerance) / 100; + + return ((resistance >= low) && (resistance <= high)); +} +EXPORT_SYMBOL_GPL(power_supply_battery_bti_in_range); + int power_supply_get_property(struct power_supply *psy, enum power_supply_property psp, union power_supply_propval *val) diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h index 42a47d7aa3fd..ed206ac64122 100644 --- a/include/linux/power_supply.h +++ b/include/linux/power_supply.h @@ -497,6 +497,14 @@ struct power_supply_maintenance_charge_table { * by temperature: highest temperature with lowest resistance first, lowest * temperature with highest resistance last. * @resist_table_size: the number of items in the resist_table. + * @bti_resistance_ohm: The Battery Type Indicator (BIT) nominal resistance + * in ohms for this battery, if an identification resistor is mounted + * between a third battery terminal and ground. This scheme is used by a lot + * of mobile device batteries. + * @bti_resistance_tolerance: The tolerance in percent of the BTI resistance, + * for example 10 for +/- 10%, if the bti_resistance is set to 7000 and the + * tolerance is 10% we will detect a proper battery if the BTI resistance + * is between 6300 and 7700 Ohm. * * This is the recommended struct to manage static battery parameters, * populated by power_supply_get_battery_info(). Most platform drivers should @@ -623,6 +631,8 @@ struct power_supply_battery_info { int ocv_table_size[POWER_SUPPLY_OCV_TEMP_MAX]; struct power_supply_resistance_temp_table *resist_table; int resist_table_size; + int bti_resistance_ohm; + int bti_resistance_tolerance; }; extern struct atomic_notifier_head power_supply_notifier; @@ -666,6 +676,8 @@ power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *table int table_len, int temp); extern struct power_supply_maintenance_charge_table * power_supply_get_maintenance_charging_setting(struct power_supply_battery_info *info, int index); +extern bool power_supply_battery_bti_in_range(struct power_supply_battery_info *info, + int resistance); extern void power_supply_changed(struct power_supply *psy); extern int power_supply_am_i_supplied(struct power_supply *psy); extern int power_supply_set_input_current_limit_from_supplier( @@ -682,6 +694,7 @@ power_supply_supports_maintenance_charging(struct power_supply_battery_info *inf return (mt != NULL); } + #ifdef CONFIG_POWER_SUPPLY extern int power_supply_is_system_supplied(void); #else From patchwork Fri Feb 25 00:13:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 546372 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 130BFC4332F for ; Fri, 25 Feb 2022 00:15:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233276AbiBYAQC (ORCPT ); Thu, 24 Feb 2022 19:16:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232174AbiBYAQB (ORCPT ); Thu, 24 Feb 2022 19:16:01 -0500 Received: from mail-lf1-x12e.google.com (mail-lf1-x12e.google.com [IPv6:2a00:1450:4864:20::12e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A1112692FD for ; Thu, 24 Feb 2022 16:15:30 -0800 (PST) Received: by mail-lf1-x12e.google.com with SMTP id w27so6677197lfa.5 for ; Thu, 24 Feb 2022 16:15:30 -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 :mime-version:content-transfer-encoding; bh=tlggcYrqwEd83iy37IBfsdLOac89UGRNeeb9nNDv2l4=; b=ARXvRevJPGJ3F2NQsOoJ16uyDIfRa/KcbknTbeVFJfj1HHVEei1E/w6EET5AYnTC+Y 3LeK1DiN+CbkPG2cPRsrL9IPHrqG5uLdHOxA3r81/DNjtn7imVyLLQsA+gqxSwQUtx7O 47VQzvxE85Y5JAxLsKXaCpI7dy+gnGTuNOBGiQJcQbd2JWcjgFcfhMrseZXf5qAa3HEO WvJrSFjZRc9BxSIpLWO7VVQ/pkJjVg6VrbaDyU4thmGHkBV+tWXXVCw3rLkrcDdKjW0K 0CAAdink/HNpkCWlPSovIv0oYXZKpdceEGEeZPCe+GYt7mzv5yODAOZB9tCP7LYU52Ev iXeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=tlggcYrqwEd83iy37IBfsdLOac89UGRNeeb9nNDv2l4=; b=KGIWSjPR+0qgb2wybRsPHwgyzbsJPebmslxXT+TB1+W+TR1AJyTj/DdKtGUCJRus6Y JczxcwbiqkLN9uMP14oPND7DMNQ+aFWM0WVe72PWlc2EZUSMeWrPQaUnwSL3j+qhLWyh cLNOSZ4RQ1mCbwVmuTMYjZpAV5+ldDZMmayihy1GD4AF3E6tna/JWo9L+yhcykwcNGRP LvsxiNDBxdDh9j5y4k6sSZ07DOfuRVokOdmJfKjCB1AuJluqVZq7H/4orpzuVCTWM9mm d3qWM+gTan7QzwP14Is9CrcY+3GJX8fO5RQ2M8BWNcWZdod28o6QH8/2pZouuk3VllVh oSDA== X-Gm-Message-State: AOAM531j11XbNNGLndq0FEX+li+BmlwoCeQzALaowjrEDrHYm7FwCn/L jKq8U+SIiEp2ds/SR514cH0s5A== X-Google-Smtp-Source: ABdhPJy3XScPgdusHpeyWv4jQjic38Ejtj8p4r0lEQm9peV3Wc45F7gJP9aVdm9bZpo6zHDHZm+x0Q== X-Received: by 2002:ac2:5fd0:0:b0:42e:6a36:c02c with SMTP id q16-20020ac25fd0000000b0042e6a36c02cmr3155756lfg.108.1645748128555; Thu, 24 Feb 2022 16:15:28 -0800 (PST) Received: from localhost.localdomain (c-fdcc225c.014-348-6c756e10.bbcust.telenor.se. [92.34.204.253]) by smtp.gmail.com with ESMTPSA id 16-20020ac25f10000000b00443890bd84asm55859lfq.114.2022.02.24.16.15.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Feb 2022 16:15:28 -0800 (PST) From: Linus Walleij To: Sebastian Reichel , Marcus Cooper Cc: linux-pm@vger.kernel.org, Matti Vaittinen , Linus Walleij Subject: [PATCH 4/6 v3] power: supply: Support VBAT-to-Ri lookup tables Date: Fri, 25 Feb 2022 01:13:12 +0100 Message-Id: <20220225001314.1881549-5-linus.walleij@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220225001314.1881549-1-linus.walleij@linaro.org> References: <20220225001314.1881549-1-linus.walleij@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org In Samsung devices, the method used to compensate for temperature, age, load etc is by way of VBAT to Ri tables, which correlates the battery voltage under load (VBAT) to an internal resistance (Ri). Using this Ri and a measurement of the current out of the battery (IBAT) the open circuit voltage (OCV) can be calculated as: OCV = VBAT - (Ri * IBAT) The details are described in comments to struct power_supply_battery_info in the commit. Since not all batteries supply this VBAT-to-Ri data, the fallback method to use the temperature-to-Ri lookup table can also be used as a fallback. Add two helper functions to check if we have the tables needed for using power_supply_vbat2ri() or power_supply_temp2resist_simple() respectively, so capacity estimation code can choose which one to employ. Signed-off-by: Linus Walleij --- ChangeLog v2->v3: - No changes ChangeLog v1->v2: - No changes --- drivers/power/supply/power_supply_core.c | 67 +++++++++++++- include/linux/power_supply.h | 113 ++++++++++++++++++++++- 2 files changed, 177 insertions(+), 3 deletions(-) diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c index cbe957088c56..1c0b1be22067 100644 --- a/drivers/power/supply/power_supply_core.c +++ b/drivers/power/supply/power_supply_core.c @@ -791,7 +791,7 @@ EXPORT_SYMBOL_GPL(power_supply_put_battery_info); /** * power_supply_temp2resist_simple() - find the battery internal resistance - * percent + * percent from temperature * @table: Pointer to battery resistance temperature table * @table_len: The table length * @temp: Current temperature @@ -828,6 +828,71 @@ int power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *t } EXPORT_SYMBOL_GPL(power_supply_temp2resist_simple); +/** + * power_supply_vbat2ri() - find the battery internal resistance + * from the battery voltage + * @info: The battery information container + * @table: Pointer to battery resistance temperature table + * @vbat_uv: The battery voltage in microvolt + * @charging: If we are charging (true) or not (false) + * + * This helper function is used to look up battery internal resistance + * according to current battery voltage. Depending on whether the battery + * is currently charging or not, different resistance will be returned. + * + * Returns the internal resistance in microohm or negative error code. + */ +int power_supply_vbat2ri(struct power_supply_battery_info *info, + int vbat_uv, bool charging) +{ + struct power_supply_vbat_ri_table *vbat2ri; + int table_len; + int i, high, low; + + /* + * If we are charging, and the battery supplies a separate table + * for this state, we use that in order to compensate for the + * charging voltage. Otherwise we use the main table. + */ + if (charging && info->vbat2ri_charging) { + vbat2ri = info->vbat2ri_charging; + table_len = info->vbat2ri_charging_size; + } else { + vbat2ri = info->vbat2ri_discharging; + table_len = info->vbat2ri_discharging_size; + } + + /* + * If no tables are specified, or if we are above the highest voltage in + * the voltage table, just return the factory specified internal resistance. + */ + if (!vbat2ri || (table_len <= 0) || (vbat_uv > vbat2ri[0].vbat_uv)) { + if (charging && (info->factory_internal_resistance_charging_uohm > 0)) + return info->factory_internal_resistance_charging_uohm; + else + return info->factory_internal_resistance_uohm; + } + + /* Break loop at table_len - 1 because that is the highest index */ + for (i = 0; i < table_len - 1; i++) + if (vbat_uv > vbat2ri[i].vbat_uv) + break; + + /* The library function will deal with high == low */ + if ((i == 0) || (i == (table_len - 1))) + high = i; + else + high = i - 1; + low = i; + + return fixp_linear_interpolate(vbat2ri[low].vbat_uv, + vbat2ri[low].ri_uohm, + vbat2ri[high].vbat_uv, + vbat2ri[high].ri_uohm, + vbat_uv); +} +EXPORT_SYMBOL_GPL(power_supply_vbat2ri); + struct power_supply_maintenance_charge_table * power_supply_get_maintenance_charging_setting(struct power_supply_battery_info *info, int index) diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h index ed206ac64122..ffbf69c00003 100644 --- a/include/linux/power_supply.h +++ b/include/linux/power_supply.h @@ -348,6 +348,11 @@ struct power_supply_resistance_temp_table { int resistance; /* internal resistance percent */ }; +struct power_supply_vbat_ri_table { + int vbat_uv; /* Battery voltage in microvolt */ + int ri_uohm; /* Internal resistance in microohm */ +}; + /** * struct power_supply_maintenance_charge_table - setting for maintenace charging * @charge_current_max_ua: maintenance charging current that is used to keep @@ -459,7 +464,14 @@ struct power_supply_maintenance_charge_table { * @factory_internal_resistance_uohm: the internal resistance of the battery * at fabrication time, expressed in microohms. This resistance will vary * depending on the lifetime and charge of the battery, so this is just a - * nominal ballpark figure. + * nominal ballpark figure. This internal resistance is given for the state + * when the battery is discharging. + * @factory_internal_resistance_charging_uohm: the internal resistance of the + * battery at fabrication time while charging, expressed in microohms. + * The charging process will affect the internal resistance of the battery + * so this value provides a better resistance under these circumstances. + * This resistance will vary depending on the lifetime and charge of the + * battery, so this is just a nominal ballpark figure. * @ocv_temp: array indicating the open circuit voltage (OCV) capacity * temperature indices. This is an array of temperatures in degrees Celsius * indicating which capacity table to use for a certain temperature, since @@ -497,6 +509,21 @@ struct power_supply_maintenance_charge_table { * by temperature: highest temperature with lowest resistance first, lowest * temperature with highest resistance last. * @resist_table_size: the number of items in the resist_table. + * @vbat2ri_discharging: this is a table that correlates Battery voltage (VBAT) + * to internal resistance (Ri). The resistance is given in microohm for the + * corresponding voltage in microvolts. The internal resistance is used to + * determine the open circuit voltage so that we can determine the capacity + * of the battery. These voltages to resistance tables apply when the battery + * is discharging. The table must be ordered descending by voltage: highest + * voltage first. + * @vbat2ri_discharging_size: the number of items in the vbat2ri_discharging + * table. + * @vbat2ri_charging: same function as vbat2ri_discharging but for the state + * when the battery is charging. Being under charge changes the battery's + * internal resistance characteristics so a separate table is needed.* + * The table must be ordered descending by voltage: highest voltage first. + * @vbat2ri_charging_size: the number of items in the vbat2ri_charging + * table. * @bti_resistance_ohm: The Battery Type Indicator (BIT) nominal resistance * in ohms for this battery, if an identification resistor is mounted * between a third battery terminal and ground. This scheme is used by a lot @@ -511,7 +538,9 @@ struct power_supply_maintenance_charge_table { * use these for consistency. * * Its field names must correspond to elements in enum power_supply_property. - * The default field value is -EINVAL. + * The default field value is -EINVAL or NULL for pointers. + * + * CC/CV CHARGING: * * The charging parameters here assume a CC/CV charging scheme. This method * is most common with Lithium Ion batteries (other methods are possible) and @@ -596,6 +625,66 @@ struct power_supply_maintenance_charge_table { * Overcharging Lithium Ion cells can be DANGEROUS and lead to fire or * explosions. * + * DETERMINING BATTERY CAPACITY: + * + * Several members of the struct deal with trying to determine the remaining + * capacity in the battery, usually as a percentage of charge. In practice + * many chargers uses a so-called fuel gauge or coloumb counter that measure + * how much charge goes into the battery and how much goes out (+/- leak + * consumption). This does not help if we do not know how much capacity the + * battery has to begin with, such as when it is first used or was taken out + * and charged in a separate charger. Therefore many capacity algorithms use + * the open circuit voltage with a look-up table to determine the rough + * capacity of the battery. The open circuit voltage can be conceptualized + * with an ideal voltage source (V) in series with an internal resistance (Ri) + * like this: + * + * +-------> IBAT >----------------+ + * | ^ | + * [ ] Ri | | + * | | VBAT | + * o <---------- | | + * +| ^ | [ ] Rload + * .---. | | | + * | V | | OCV | | + * '---' | | | + * | | | | + * GND +-------------------------------+ + * + * If we disconnect the load (here simplified as a fixed resistance Rload) + * and measure VBAT with a infinite impedance voltage meter we will get + * VBAT = OCV and this assumption is sometimes made even under load, assuming + * Rload is insignificant. However this will be of dubious quality because the + * load is rarely that small and Ri is strongly nonlinear depending on + * temperature and how much capacity is left in the battery due to the + * chemistry involved. + * + * In many practical applications we cannot just disconnect the battery from + * the load, so instead we often try to measure the instantaneous IBAT (the + * current out from the battery), estimate the Ri and thus calculate the + * voltage drop over Ri and compensate like this: + * + * OCV = VBAT - (IBAT * Ri) + * + * The tables vbat2ri_discharging and vbat2ri_charging are used to determine + * (by interpolation) the Ri from the VBAT under load. These curves are highly + * nonlinear and may need many datapoints but can be found in datasheets for + * some batteries. This gives the compensated open circuit voltage (OCV) for + * the battery even under load. Using this method will also compensate for + * temperature changes in the environment: this will also make the internal + * resistance change, and it will affect the VBAT under load, so correlating + * VBAT to Ri takes both remaining capacity and temperature into consideration. + * + * Alternatively a manufacturer can specify how the capacity of the battery + * is dependent on the battery temperature which is the main factor affecting + * Ri. As we know all checmical reactions are faster when it is warm and slower + * when it is cold. You can put in 1500mAh and only get 800mAh out before the + * voltage drops too low for example. This effect is also highly nonlinear and + * the purpose of the table resist_table: this will take a temperature and + * tell us how big percentage of Ri the specified temperature correlates to. + * Usually we have 100% of the factory_internal_resistance_uohm at 25 degrees + * Celsius. + * * The power supply class itself doesn't use this struct as of now. */ @@ -620,6 +709,7 @@ struct power_supply_battery_info { int alert_high_temp_charge_current_ua; int alert_high_temp_charge_voltage_uv; int factory_internal_resistance_uohm; + int factory_internal_resistance_charging_uohm; int ocv_temp[POWER_SUPPLY_OCV_TEMP_MAX]; int temp_ambient_alert_min; int temp_ambient_alert_max; @@ -631,6 +721,10 @@ struct power_supply_battery_info { int ocv_table_size[POWER_SUPPLY_OCV_TEMP_MAX]; struct power_supply_resistance_temp_table *resist_table; int resist_table_size; + struct power_supply_vbat_ri_table *vbat2ri_discharging; + int vbat2ri_discharging_size; + struct power_supply_vbat_ri_table *vbat2ri_charging; + int vbat2ri_charging_size; int bti_resistance_ohm; int bti_resistance_tolerance; }; @@ -674,6 +768,8 @@ extern int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info, extern int power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *table, int table_len, int temp); +extern int power_supply_vbat2ri(struct power_supply_battery_info *info, + int vbat_uv, bool charging); extern struct power_supply_maintenance_charge_table * power_supply_get_maintenance_charging_setting(struct power_supply_battery_info *info, int index); extern bool power_supply_battery_bti_in_range(struct power_supply_battery_info *info, @@ -694,6 +790,19 @@ power_supply_supports_maintenance_charging(struct power_supply_battery_info *inf return (mt != NULL); } +static inline bool +power_supply_supports_vbat2ri(struct power_supply_battery_info *info) +{ + return ((info->vbat2ri_discharging != NULL) && + info->vbat2ri_discharging_size > 0); +} + +static inline bool +power_supply_supports_temp2ri(struct power_supply_battery_info *info) +{ + return ((info->resist_table != NULL) && + info->resist_table_size > 0); +} #ifdef CONFIG_POWER_SUPPLY extern int power_supply_is_system_supplied(void); From patchwork Fri Feb 25 00:13:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 546104 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 567B0C433F5 for ; Fri, 25 Feb 2022 00:15:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233831AbiBYAQE (ORCPT ); Thu, 24 Feb 2022 19:16:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232174AbiBYAQD (ORCPT ); Thu, 24 Feb 2022 19:16:03 -0500 Received: from mail-lj1-x22a.google.com (mail-lj1-x22a.google.com [IPv6:2a00:1450:4864:20::22a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 084E32692F8 for ; Thu, 24 Feb 2022 16:15:32 -0800 (PST) Received: by mail-lj1-x22a.google.com with SMTP id bn33so5232023ljb.6 for ; Thu, 24 Feb 2022 16:15:31 -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 :mime-version:content-transfer-encoding; bh=van0GZu2Hw5Wh2Og/fehUOeDRqMGuyBvE9y7yh8MAC8=; b=D59vPuomOzz8z3OfX6mZwWQTEBC9h5OKo4qWj2Mp0jjeHi+anUTA4Ctnp3WbmexS/v GRqlYL0co1UEfd+Jx61LRB2ZRpAQvVv8ZD1NjZHcj9LrGm7A8S//DobEG7E6hRLH6mQM yhjel/C6Ee3sTybgDvVArZXK6ec3c94rJG2sOpI4EDK2Pap/GkuYae/Kv5Pg0Bh336+N xRanC6vRN6i0OVFl+hOH6ECNo6OgdvTDP4MLk9RGz56LAsZn61RFKIDZXyUhwKhtnW9q 9d5oDwt5MHXzgAHbJ8GXidLQMmeIzMV4FLsm1XCjJg81MyK4wTOMCdmNkBXcBYSPrK3h 7T0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=van0GZu2Hw5Wh2Og/fehUOeDRqMGuyBvE9y7yh8MAC8=; b=0fzt6vSDqXCnHuW0dekn9NibyIrrB0oT5sWlCXAyu/S4aQbLao9jWlcDCGrul8fXRz x9IWSVvXLTJdYrGnXXkQZFgNhH2rVtReZRU0gc+hIT8xOm97lO0upe3r8lbcEIsKOEkx cm/7lGIMTtikNmZY01mB4vo/qrYRfiUoOEdyd4Qqin+dLn19vResH5vvpMWeinRtE/5e wDOtV0Vo27lL4HpOnillGOKUyqaMFD0jMcxW/kuonQH8EYxwc1PGqCRymH+45akdJxsh au7s1GQEjR2tqD8Kg+X8la+8kN9eS+hSY6N7dmHF+Bkj4BXxMx3olAx5x1T7aj8AKf9q yzXg== X-Gm-Message-State: AOAM533bRPfPUkudXBQmnrSYb4uK42G5YXsk9nLe+G7lPZljqpskm/t7 a1RHElj35KN7N7gyX/qsoKyzCw== X-Google-Smtp-Source: ABdhPJwJz7Jqn+X0gxRuUe7WMTk3Tg8zfC+7lNzFMw1zjQw/HVgf0F8KbMy914o6+uU9rp6rZhT8vw== X-Received: by 2002:a2e:8890:0:b0:244:d635:b4af with SMTP id k16-20020a2e8890000000b00244d635b4afmr3396821lji.33.1645748130359; Thu, 24 Feb 2022 16:15:30 -0800 (PST) Received: from localhost.localdomain (c-fdcc225c.014-348-6c756e10.bbcust.telenor.se. [92.34.204.253]) by smtp.gmail.com with ESMTPSA id 16-20020ac25f10000000b00443890bd84asm55859lfq.114.2022.02.24.16.15.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Feb 2022 16:15:29 -0800 (PST) From: Linus Walleij To: Sebastian Reichel , Marcus Cooper Cc: linux-pm@vger.kernel.org, Matti Vaittinen , Linus Walleij Subject: [PATCH 5/6 v3] power: supply: ab8500_fg: Use VBAT-to-Ri if possible Date: Fri, 25 Feb 2022 01:13:13 +0100 Message-Id: <20220225001314.1881549-6-linus.walleij@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220225001314.1881549-1-linus.walleij@linaro.org> References: <20220225001314.1881549-1-linus.walleij@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Augment the AB8500 fuel gauge to use the VBAT-to-Ri method of estimating the internal resistance if possible. Else fall back to using the temperature-to-Ri or just the default Ri. Signed-off-by: Linus Walleij --- ChangeLog v2->v3: - No changes ChangeLog v1->v2: - No changes --- drivers/power/supply/ab8500_fg.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/drivers/power/supply/ab8500_fg.c b/drivers/power/supply/ab8500_fg.c index 39c7e6b0be52..e735ba74d1c3 100644 --- a/drivers/power/supply/ab8500_fg.c +++ b/drivers/power/supply/ab8500_fg.c @@ -877,27 +877,38 @@ static int ab8500_fg_uncomp_volt_to_capacity(struct ab8500_fg *di) /** * ab8500_fg_battery_resistance() - Returns the battery inner resistance * @di: pointer to the ab8500_fg structure + * @vbat_uncomp_uv: Uncompensated VBAT voltage * * Returns battery inner resistance added with the fuel gauge resistor value * to get the total resistance in the whole link from gnd to bat+ node * in milliohm. */ -static int ab8500_fg_battery_resistance(struct ab8500_fg *di) +static int ab8500_fg_battery_resistance(struct ab8500_fg *di, int vbat_uncomp_uv) { struct power_supply_battery_info *bi = di->bm->bi; int resistance_percent = 0; int resistance; - resistance_percent = power_supply_temp2resist_simple(bi->resist_table, - bi->resist_table_size, - di->bat_temp / 10); /* - * We get a percentage of factory resistance here so first get - * the factory resistance in milliohms then calculate how much - * resistance we have at this temperature. + * Determine the resistance at this voltage. First try VBAT-to-Ri else + * just infer it from the surrounding temperature, if nothing works just + * use the internal resistance. */ - resistance = (bi->factory_internal_resistance_uohm / 1000); - resistance = resistance * resistance_percent / 100; + if (power_supply_supports_vbat2ri(bi)) { + resistance = power_supply_vbat2ri(bi, vbat_uncomp_uv, di->flags.charging); + /* Convert to milliohm */ + resistance = resistance / 1000; + } else if (power_supply_supports_temp2ri(bi)) { + resistance_percent = power_supply_temp2resist_simple(bi->resist_table, + bi->resist_table_size, + di->bat_temp / 10); + /* Convert to milliohm */ + resistance = bi->factory_internal_resistance_uohm / 1000; + resistance = resistance * resistance_percent / 100; + } else { + /* Last fallback */ + resistance = bi->factory_internal_resistance_uohm / 1000; + } dev_dbg(di->dev, "%s Temp: %d battery internal resistance: %d" " fg resistance %d, total: %d (mOhm)\n", @@ -955,7 +966,7 @@ static int ab8500_load_comp_fg_bat_voltage(struct ab8500_fg *di, bool always) vbat_uv = vbat_uv / i; /* Next we apply voltage compensation from internal resistance */ - rcomp = ab8500_fg_battery_resistance(di); + rcomp = ab8500_fg_battery_resistance(di, vbat_uv); vbat_uv = vbat_uv - (di->inst_curr_ua * rcomp) / 1000; /* Always keep this state at latest measurement */ From patchwork Fri Feb 25 00:13:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 546371 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 CB80FC433F5 for ; Fri, 25 Feb 2022 00:15:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233876AbiBYAQH (ORCPT ); Thu, 24 Feb 2022 19:16:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232174AbiBYAQG (ORCPT ); Thu, 24 Feb 2022 19:16:06 -0500 Received: from mail-lf1-x133.google.com (mail-lf1-x133.google.com [IPv6:2a00:1450:4864:20::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5B672692FC for ; Thu, 24 Feb 2022 16:15:33 -0800 (PST) Received: by mail-lf1-x133.google.com with SMTP id d23so6599011lfv.13 for ; Thu, 24 Feb 2022 16:15:33 -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 :mime-version:content-transfer-encoding; bh=qIEo7TbWS1JdsYy4dCYSGqkprPuYvTwG9n3JXUjzANQ=; b=sXgglo/IMPG6WH8TiGW+9OPblxn42NftpaHRXTuECfIRc+lQKYIboLdVHKZIKWaJHN crigrGZJnb0Nfr2KDesCnGDvnTyctTUmehxsjTKAAyYwfXUZatlOMNmQMbTRM+TJSN2f qJMWehC7nHVJIPxvB+hKUVJHObMPLQclUwhlLecYyClsnY7gJm/Dc5udgezytyA/xsQJ Di9rPBdHdmgAwoHS+eDZFaJaLv9a9CnL6XGVSV2IVwPGhUBPILqw4ZT5PFKh+rUYD6HB uzpNlDdVfu49fnHiKF34wIBmamKzfas3LxWxUbgtr1WQRotgtcrBa3Ban/2iVHUm6JyT Q9dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=qIEo7TbWS1JdsYy4dCYSGqkprPuYvTwG9n3JXUjzANQ=; b=c7qadQ96JFREJavZqrJwqMzpFCgZ669EPkwGczuhaR0dj+/be8amSOZ37ENjKxohLB /AIdj6X61WbHym+ezFIWuU3KGeAD64jXyE6JBO2XX/FKA2A5f2hcRQekH3VERK3xgzkf AGJbztMzjgbz1ozbepZq5iuFbu5TC99k8f+3f6vWwxPE7ReXIQjzqdN2ANu+AHUD6TxJ FoYMVpPsT4K9x4bc3k+HC0R5mUq7chBMBbvEYAMN6ZQHqfeg31yBhVUy2Ww8A7rSkg3/ Z+0r9MwcYi3bwzczI1T6tuhovTgE0OXGA6V2WjvwRKy7KAf6HT6PMXoA2j6NtJlV7SAu qDxg== X-Gm-Message-State: AOAM5305MF2bjyu1Y+bJLeH2QXvEVzsrBfRENtkdduq6Tevttu+TzLQb Myc4VE7yZQ0jadGeybcPWDqkBA== X-Google-Smtp-Source: ABdhPJwmZpbAv6hLfZ1DfYvrGeBAmHJ14KWA9d4zXcOyr/NYmDEUDEsO4opz4v6S00/KwqyoUxOzBQ== X-Received: by 2002:ac2:539b:0:b0:443:db38:ac87 with SMTP id g27-20020ac2539b000000b00443db38ac87mr3272929lfh.247.1645748131903; Thu, 24 Feb 2022 16:15:31 -0800 (PST) Received: from localhost.localdomain (c-fdcc225c.014-348-6c756e10.bbcust.telenor.se. [92.34.204.253]) by smtp.gmail.com with ESMTPSA id 16-20020ac25f10000000b00443890bd84asm55859lfq.114.2022.02.24.16.15.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Feb 2022 16:15:31 -0800 (PST) From: Linus Walleij To: Sebastian Reichel , Marcus Cooper Cc: linux-pm@vger.kernel.org, Matti Vaittinen , Linus Walleij Subject: [PATCH 6/6 v3] power: supply: Static data for Samsung batteries Date: Fri, 25 Feb 2022 01:13:14 +0100 Message-Id: <20220225001314.1881549-7-linus.walleij@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220225001314.1881549-1-linus.walleij@linaro.org> References: <20220225001314.1881549-1-linus.walleij@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org If we detect a Samsung SDI battery, we return a static struct power_supply_battery_info and avoid looking further. Signed-off-by: Linus Walleij --- ChangeLog v2->v3: - Fix a whole bunch of tables after realizing that completely different battery data was used if CONFIG_SAMSUNG_BATTERY was set for Golden, Kyle, Codina and Skomer. ChangeLog v1->v2: - Bump tolerance to 40% on all BTI resistances - Change config option to bool, as we are calling OUT from the supply core this cannot be compiled into a module - Augment data to support low/high temperature charge current and voltages. Sadly the vendor tree just provide one and the same current and voltage for all of these. --- drivers/power/supply/Kconfig | 6 + drivers/power/supply/Makefile | 1 + drivers/power/supply/power_supply_core.c | 28 +- drivers/power/supply/samsung-sdi-battery.c | 919 +++++++++++++++++++++ drivers/power/supply/samsung-sdi-battery.h | 13 + 5 files changed, 957 insertions(+), 10 deletions(-) create mode 100644 drivers/power/supply/samsung-sdi-battery.c create mode 100644 drivers/power/supply/samsung-sdi-battery.h diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig index 6815e5a4c0bd..e9df4151f2e0 100644 --- a/drivers/power/supply/Kconfig +++ b/drivers/power/supply/Kconfig @@ -181,6 +181,12 @@ config BATTERY_OLPC help Say Y to enable support for the battery on the OLPC laptop. +config BATTERY_SAMSUNG_SDI + bool "Samsung SDI batteries" + help + Say Y to enable support for Samsung SDI battery data. + These batteries are used in Samsung mobile phones. + config BATTERY_TOSA tristate "Sharp SL-6000 (tosa) battery" depends on MACH_TOSA && MFD_TC6393XB && TOUCHSCREEN_WM97XX diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile index 2c1b264b2046..ebcd2f5fe26d 100644 --- a/drivers/power/supply/Makefile +++ b/drivers/power/supply/Makefile @@ -34,6 +34,7 @@ obj-$(CONFIG_BATTERY_GOLDFISH) += goldfish_battery.o obj-$(CONFIG_BATTERY_LEGO_EV3) += lego_ev3_battery.o obj-$(CONFIG_BATTERY_PMU) += pmu_battery.o obj-$(CONFIG_BATTERY_OLPC) += olpc_battery.o +obj-$(CONFIG_BATTERY_SAMSUNG_SDI) += samsung-sdi-battery.o obj-$(CONFIG_BATTERY_TOSA) += tosa_battery.o obj-$(CONFIG_BATTERY_COLLIE) += collie_battery.o obj-$(CONFIG_BATTERY_INGENIC) += ingenic-battery.o diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c index 1c0b1be22067..43c66214bedf 100644 --- a/drivers/power/supply/power_supply_core.c +++ b/drivers/power/supply/power_supply_core.c @@ -23,6 +23,7 @@ #include #include #include "power_supply.h" +#include "samsung-sdi-battery.h" /* exported for the APM Power driver, APM emulation */ struct class *power_supply_class; @@ -573,6 +574,23 @@ int power_supply_get_battery_info(struct power_supply *psy, int err, len, index; const __be32 *list; + if (!psy->of_node) { + dev_warn(&psy->dev, "%s currently only supports devicetree\n", + __func__); + return -ENXIO; + } + + battery_np = of_parse_phandle(psy->of_node, "monitored-battery", 0); + if (!battery_np) + return -ENODEV; + + /* Try static batteries first */ + err = samsung_sdi_battery_get_info(&psy->dev, battery_np, &info); + if (!err) { + *info_out = info; + return err; + } + info = devm_kmalloc(&psy->dev, sizeof(*info), GFP_KERNEL); if (!info) return -ENOMEM; @@ -612,16 +630,6 @@ int power_supply_get_battery_info(struct power_supply *psy, info->ocv_table_size[index] = -EINVAL; } - if (!psy->of_node) { - dev_warn(&psy->dev, "%s currently only supports devicetree\n", - __func__); - return -ENXIO; - } - - battery_np = of_parse_phandle(psy->of_node, "monitored-battery", 0); - if (!battery_np) - return -ENODEV; - err = of_property_read_string(battery_np, "compatible", &value); if (err) goto out_put_node; diff --git a/drivers/power/supply/samsung-sdi-battery.c b/drivers/power/supply/samsung-sdi-battery.c new file mode 100644 index 000000000000..8e718f0fc2b5 --- /dev/null +++ b/drivers/power/supply/samsung-sdi-battery.c @@ -0,0 +1,919 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Battery data and characteristics for Samsung SDI (Samsung Digital Interface) + * batteries. The data is retrieved automatically into drivers using + * the power_supply_get_battery_info() call. + * + * The BTI (battery type indicator) resistance in the code drops was very + * unreliable. The resistance listed here was obtained by simply measuring + * the BTI resistance with a multimeter on the battery. + */ +#include +#include +#include +#include "samsung-sdi-battery.h" + +struct samsung_sdi_battery { + char *compatible; + char *name; + struct power_supply_battery_info info; +}; + +/* + * Voltage to internal resistance tables. The internal resistance varies + * depending on the VBAT voltage, so look this up from a table. Different + * tables apply depending on whether we are charging or not. + */ + +struct power_supply_vbat_ri_table samsung_vbat2res_discharging_eb_l1m7flu[] = { + { .vbat_uv = 4240000, .ri_uohm = 160000 }, + { .vbat_uv = 4210000, .ri_uohm = 179000 }, + { .vbat_uv = 4180000, .ri_uohm = 183000 }, + { .vbat_uv = 4160000, .ri_uohm = 184000 }, + { .vbat_uv = 4140000, .ri_uohm = 191000 }, + { .vbat_uv = 4120000, .ri_uohm = 204000 }, + { .vbat_uv = 4076000, .ri_uohm = 220000 }, + { .vbat_uv = 4030000, .ri_uohm = 227000 }, + { .vbat_uv = 3986000, .ri_uohm = 215000 }, + { .vbat_uv = 3916000, .ri_uohm = 221000 }, + { .vbat_uv = 3842000, .ri_uohm = 259000 }, + { .vbat_uv = 3773000, .ri_uohm = 287000 }, + { .vbat_uv = 3742000, .ri_uohm = 283000 }, + { .vbat_uv = 3709000, .ri_uohm = 277000 }, + { .vbat_uv = 3685000, .ri_uohm = 297000 }, + { .vbat_uv = 3646000, .ri_uohm = 310000 }, + { .vbat_uv = 3616000, .ri_uohm = 331000 }, + { .vbat_uv = 3602000, .ri_uohm = 370000 }, + { .vbat_uv = 3578000, .ri_uohm = 350000 }, + { .vbat_uv = 3553000, .ri_uohm = 321000 }, + { .vbat_uv = 3503000, .ri_uohm = 322000 }, + { .vbat_uv = 3400000, .ri_uohm = 269000 }, + { .vbat_uv = 3360000, .ri_uohm = 328000 }, + { .vbat_uv = 3330000, .ri_uohm = 305000 }, + { .vbat_uv = 3300000, .ri_uohm = 339000 }, +}; + +struct power_supply_vbat_ri_table samsung_vbat2res_charging_eb_l1m7flu[] = { + { .vbat_uv = 4302000, .ri_uohm = 230000 }, + { .vbat_uv = 4276000, .ri_uohm = 345000 }, + { .vbat_uv = 4227000, .ri_uohm = 345000 }, + { .vbat_uv = 4171000, .ri_uohm = 346000 }, + { .vbat_uv = 4134000, .ri_uohm = 311000 }, + { .vbat_uv = 4084000, .ri_uohm = 299000 }, + { .vbat_uv = 4052000, .ri_uohm = 316000 }, + { .vbat_uv = 4012000, .ri_uohm = 309000 }, + { .vbat_uv = 3961000, .ri_uohm = 303000 }, + { .vbat_uv = 3939000, .ri_uohm = 280000 }, + { .vbat_uv = 3904000, .ri_uohm = 261000 }, + { .vbat_uv = 3850000, .ri_uohm = 212000 }, + { .vbat_uv = 3800000, .ri_uohm = 232000 }, + { .vbat_uv = 3750000, .ri_uohm = 177000 }, + { .vbat_uv = 3712000, .ri_uohm = 164000 }, + { .vbat_uv = 3674000, .ri_uohm = 161000 }, + { .vbat_uv = 3590000, .ri_uohm = 164000 }, +}; + +struct power_supply_vbat_ri_table samsung_vbat2res_discharging_eb425161la[] = { + { .vbat_uv = 4240000, .ri_uohm = 160000 }, + { .vbat_uv = 4210000, .ri_uohm = 179000 }, + { .vbat_uv = 4180000, .ri_uohm = 183000 }, + { .vbat_uv = 4160000, .ri_uohm = 184000 }, + { .vbat_uv = 4140000, .ri_uohm = 191000 }, + { .vbat_uv = 4120000, .ri_uohm = 204000 }, + { .vbat_uv = 4080000, .ri_uohm = 200000 }, + { .vbat_uv = 4027000, .ri_uohm = 202000 }, + { .vbat_uv = 3916000, .ri_uohm = 221000 }, + { .vbat_uv = 3842000, .ri_uohm = 259000 }, + { .vbat_uv = 3800000, .ri_uohm = 262000 }, + { .vbat_uv = 3742000, .ri_uohm = 263000 }, + { .vbat_uv = 3709000, .ri_uohm = 277000 }, + { .vbat_uv = 3685000, .ri_uohm = 312000 }, + { .vbat_uv = 3668000, .ri_uohm = 258000 }, + { .vbat_uv = 3660000, .ri_uohm = 247000 }, + { .vbat_uv = 3636000, .ri_uohm = 293000 }, + { .vbat_uv = 3616000, .ri_uohm = 331000 }, + { .vbat_uv = 3600000, .ri_uohm = 349000 }, + { .vbat_uv = 3593000, .ri_uohm = 345000 }, + { .vbat_uv = 3585000, .ri_uohm = 344000 }, + { .vbat_uv = 3572000, .ri_uohm = 336000 }, + { .vbat_uv = 3553000, .ri_uohm = 321000 }, + { .vbat_uv = 3517000, .ri_uohm = 336000 }, + { .vbat_uv = 3503000, .ri_uohm = 322000 }, + { .vbat_uv = 3400000, .ri_uohm = 269000 }, + { .vbat_uv = 3360000, .ri_uohm = 328000 }, + { .vbat_uv = 3330000, .ri_uohm = 305000 }, + { .vbat_uv = 3300000, .ri_uohm = 339000 }, +}; + +struct power_supply_vbat_ri_table samsung_vbat2res_charging_eb425161la[] = { + { .vbat_uv = 4345000, .ri_uohm = 230000 }, + { .vbat_uv = 4329000, .ri_uohm = 238000 }, + { .vbat_uv = 4314000, .ri_uohm = 225000 }, + { .vbat_uv = 4311000, .ri_uohm = 239000 }, + { .vbat_uv = 4294000, .ri_uohm = 235000 }, + { .vbat_uv = 4264000, .ri_uohm = 229000 }, + { .vbat_uv = 4262000, .ri_uohm = 228000 }, + { .vbat_uv = 4252000, .ri_uohm = 236000 }, + { .vbat_uv = 4244000, .ri_uohm = 234000 }, + { .vbat_uv = 4235000, .ri_uohm = 234000 }, + { .vbat_uv = 4227000, .ri_uohm = 238000 }, + { .vbat_uv = 4219000, .ri_uohm = 242000 }, + { .vbat_uv = 4212000, .ri_uohm = 239000 }, + { .vbat_uv = 4206000, .ri_uohm = 231000 }, + { .vbat_uv = 4201000, .ri_uohm = 231000 }, + { .vbat_uv = 4192000, .ri_uohm = 224000 }, + { .vbat_uv = 4184000, .ri_uohm = 238000 }, + { .vbat_uv = 4173000, .ri_uohm = 245000 }, + { .vbat_uv = 4161000, .ri_uohm = 244000 }, + { .vbat_uv = 4146000, .ri_uohm = 244000 }, + { .vbat_uv = 4127000, .ri_uohm = 228000 }, + { .vbat_uv = 4119000, .ri_uohm = 218000 }, + { .vbat_uv = 4112000, .ri_uohm = 215000 }, + { .vbat_uv = 4108000, .ri_uohm = 209000 }, + { .vbat_uv = 4102000, .ri_uohm = 214000 }, + { .vbat_uv = 4096000, .ri_uohm = 215000 }, + { .vbat_uv = 4090000, .ri_uohm = 215000 }, + { .vbat_uv = 4083000, .ri_uohm = 219000 }, + { .vbat_uv = 4078000, .ri_uohm = 208000 }, + { .vbat_uv = 4071000, .ri_uohm = 205000 }, + { .vbat_uv = 4066000, .ri_uohm = 208000 }, + { .vbat_uv = 4061000, .ri_uohm = 210000 }, + { .vbat_uv = 4055000, .ri_uohm = 212000 }, + { .vbat_uv = 4049000, .ri_uohm = 215000 }, + { .vbat_uv = 4042000, .ri_uohm = 212000 }, + { .vbat_uv = 4032000, .ri_uohm = 217000 }, + { .vbat_uv = 4027000, .ri_uohm = 220000 }, + { .vbat_uv = 4020000, .ri_uohm = 210000 }, + { .vbat_uv = 4013000, .ri_uohm = 214000 }, + { .vbat_uv = 4007000, .ri_uohm = 219000 }, + { .vbat_uv = 4003000, .ri_uohm = 229000 }, + { .vbat_uv = 3996000, .ri_uohm = 246000 }, + { .vbat_uv = 3990000, .ri_uohm = 245000 }, + { .vbat_uv = 3984000, .ri_uohm = 242000 }, + { .vbat_uv = 3977000, .ri_uohm = 236000 }, + { .vbat_uv = 3971000, .ri_uohm = 231000 }, + { .vbat_uv = 3966000, .ri_uohm = 229000 }, + { .vbat_uv = 3952000, .ri_uohm = 226000 }, + { .vbat_uv = 3946000, .ri_uohm = 222000 }, + { .vbat_uv = 3941000, .ri_uohm = 222000 }, + { .vbat_uv = 3936000, .ri_uohm = 217000 }, + { .vbat_uv = 3932000, .ri_uohm = 217000 }, + { .vbat_uv = 3928000, .ri_uohm = 212000 }, + { .vbat_uv = 3926000, .ri_uohm = 214000 }, + { .vbat_uv = 3922000, .ri_uohm = 209000 }, + { .vbat_uv = 3917000, .ri_uohm = 215000 }, + { .vbat_uv = 3914000, .ri_uohm = 212000 }, + { .vbat_uv = 3912000, .ri_uohm = 220000 }, + { .vbat_uv = 3910000, .ri_uohm = 226000 }, + { .vbat_uv = 3903000, .ri_uohm = 226000 }, + { .vbat_uv = 3891000, .ri_uohm = 222000 }, + { .vbat_uv = 3871000, .ri_uohm = 221000 }, + { .vbat_uv = 3857000, .ri_uohm = 219000 }, + { .vbat_uv = 3850000, .ri_uohm = 216000 }, + { .vbat_uv = 3843000, .ri_uohm = 212000 }, + { .vbat_uv = 3835000, .ri_uohm = 206000 }, + { .vbat_uv = 3825000, .ri_uohm = 217000 }, + { .vbat_uv = 3824000, .ri_uohm = 220000 }, + { .vbat_uv = 3820000, .ri_uohm = 237000 }, + { .vbat_uv = 3800000, .ri_uohm = 232000 }, + { .vbat_uv = 3750000, .ri_uohm = 177000 }, + { .vbat_uv = 3712000, .ri_uohm = 164000 }, + { .vbat_uv = 3674000, .ri_uohm = 161000 }, + { .vbat_uv = 3590000, .ri_uohm = 164000 }, +}; + +struct power_supply_vbat_ri_table samsung_vbat2res_discharging_eb425161lu[] = { + { .vbat_uv = 4240000, .ri_uohm = 160000 }, + { .vbat_uv = 4210000, .ri_uohm = 179000 }, + { .vbat_uv = 4180000, .ri_uohm = 183000 }, + { .vbat_uv = 4160000, .ri_uohm = 184000 }, + { .vbat_uv = 4140000, .ri_uohm = 191000 }, + { .vbat_uv = 4120000, .ri_uohm = 204000 }, + { .vbat_uv = 4080000, .ri_uohm = 200000 }, + { .vbat_uv = 4027000, .ri_uohm = 202000 }, + { .vbat_uv = 3916000, .ri_uohm = 221000 }, + { .vbat_uv = 3842000, .ri_uohm = 259000 }, + { .vbat_uv = 3800000, .ri_uohm = 262000 }, + { .vbat_uv = 3742000, .ri_uohm = 263000 }, + { .vbat_uv = 3708000, .ri_uohm = 277000 }, + { .vbat_uv = 3684000, .ri_uohm = 272000 }, + { .vbat_uv = 3664000, .ri_uohm = 278000 }, + { .vbat_uv = 3655000, .ri_uohm = 285000 }, + { .vbat_uv = 3638000, .ri_uohm = 261000 }, + { .vbat_uv = 3624000, .ri_uohm = 259000 }, + { .vbat_uv = 3616000, .ri_uohm = 266000 }, + { .vbat_uv = 3597000, .ri_uohm = 278000 }, + { .vbat_uv = 3581000, .ri_uohm = 281000 }, + { .vbat_uv = 3560000, .ri_uohm = 287000 }, + { .vbat_uv = 3527000, .ri_uohm = 289000 }, + { .vbat_uv = 3512000, .ri_uohm = 286000 }, + { .vbat_uv = 3494000, .ri_uohm = 282000 }, + { .vbat_uv = 3400000, .ri_uohm = 269000 }, + { .vbat_uv = 3360000, .ri_uohm = 328000 }, + { .vbat_uv = 3330000, .ri_uohm = 305000 }, + { .vbat_uv = 3300000, .ri_uohm = 339000 }, +}; + +struct power_supply_vbat_ri_table samsung_vbat2res_charging_eb425161lu[] = { + { .vbat_uv = 4346000, .ri_uohm = 293000 }, + { .vbat_uv = 4336000, .ri_uohm = 290000 }, + { .vbat_uv = 4315000, .ri_uohm = 274000 }, + { .vbat_uv = 4310000, .ri_uohm = 264000 }, + { .vbat_uv = 4275000, .ri_uohm = 275000 }, + { .vbat_uv = 4267000, .ri_uohm = 274000 }, + { .vbat_uv = 4227000, .ri_uohm = 262000 }, + { .vbat_uv = 4186000, .ri_uohm = 282000 }, + { .vbat_uv = 4136000, .ri_uohm = 246000 }, + { .vbat_uv = 4110000, .ri_uohm = 242000 }, + { .vbat_uv = 4077000, .ri_uohm = 249000 }, + { .vbat_uv = 4049000, .ri_uohm = 238000 }, + { .vbat_uv = 4017000, .ri_uohm = 268000 }, + { .vbat_uv = 3986000, .ri_uohm = 261000 }, + { .vbat_uv = 3962000, .ri_uohm = 252000 }, + { .vbat_uv = 3940000, .ri_uohm = 235000 }, + { .vbat_uv = 3930000, .ri_uohm = 237000 }, + { .vbat_uv = 3924000, .ri_uohm = 255000 }, + { .vbat_uv = 3910000, .ri_uohm = 244000 }, + { .vbat_uv = 3889000, .ri_uohm = 231000 }, + { .vbat_uv = 3875000, .ri_uohm = 249000 }, + { .vbat_uv = 3850000, .ri_uohm = 212000 }, + { .vbat_uv = 3800000, .ri_uohm = 232000 }, + { .vbat_uv = 3750000, .ri_uohm = 177000 }, + { .vbat_uv = 3712000, .ri_uohm = 164000 }, + { .vbat_uv = 3674000, .ri_uohm = 161000 }, + { .vbat_uv = 3590000, .ri_uohm = 164000 }, +}; + +struct power_supply_vbat_ri_table samsung_vbat2res_discharging_eb485159lu[] = { + { .vbat_uv = 4240000, .ri_uohm = 160000 }, + { .vbat_uv = 4210000, .ri_uohm = 179000 }, + { .vbat_uv = 4180000, .ri_uohm = 183000 }, + { .vbat_uv = 4160000, .ri_uohm = 184000 }, + { .vbat_uv = 4140000, .ri_uohm = 191000 }, + { .vbat_uv = 4120000, .ri_uohm = 204000 }, + { .vbat_uv = 4080000, .ri_uohm = 200000 }, + { .vbat_uv = 4027000, .ri_uohm = 202000 }, + { .vbat_uv = 3916000, .ri_uohm = 221000 }, + { .vbat_uv = 3842000, .ri_uohm = 259000 }, + { .vbat_uv = 3800000, .ri_uohm = 262000 }, + { .vbat_uv = 3715000, .ri_uohm = 340000 }, + { .vbat_uv = 3700000, .ri_uohm = 300000 }, + { .vbat_uv = 3682000, .ri_uohm = 233000 }, + { .vbat_uv = 3655000, .ri_uohm = 246000 }, + { .vbat_uv = 3639000, .ri_uohm = 260000 }, + { .vbat_uv = 3621000, .ri_uohm = 254000 }, + { .vbat_uv = 3583000, .ri_uohm = 266000 }, + { .vbat_uv = 3536000, .ri_uohm = 274000 }, + { .vbat_uv = 3502000, .ri_uohm = 300000 }, + { .vbat_uv = 3465000, .ri_uohm = 245000 }, + { .vbat_uv = 3438000, .ri_uohm = 225000 }, + { .vbat_uv = 3330000, .ri_uohm = 305000 }, + { .vbat_uv = 3300000, .ri_uohm = 339000 }, +}; + +struct power_supply_vbat_ri_table samsung_vbat2res_charging_eb485159lu[] = { + { .vbat_uv = 4302000, .ri_uohm = 200000 }, + { .vbat_uv = 4258000, .ri_uohm = 206000 }, + { .vbat_uv = 4200000, .ri_uohm = 231000 }, + { .vbat_uv = 4150000, .ri_uohm = 198000 }, + { .vbat_uv = 4134000, .ri_uohm = 268000 }, + { .vbat_uv = 4058000, .ri_uohm = 172000 }, + { .vbat_uv = 4003000, .ri_uohm = 227000 }, + { .vbat_uv = 3972000, .ri_uohm = 241000 }, + { .vbat_uv = 3953000, .ri_uohm = 244000 }, + { .vbat_uv = 3950000, .ri_uohm = 213000 }, + { .vbat_uv = 3900000, .ri_uohm = 225000 }, + { .vbat_uv = 3850000, .ri_uohm = 212000 }, + { .vbat_uv = 3800000, .ri_uohm = 232000 }, + { .vbat_uv = 3750000, .ri_uohm = 177000 }, + { .vbat_uv = 3712000, .ri_uohm = 164000 }, + { .vbat_uv = 3674000, .ri_uohm = 161000 }, + { .vbat_uv = 3590000, .ri_uohm = 164000 }, +}; + +struct power_supply_vbat_ri_table samsung_vbat2res_discharging_eb535151vu[] = { + { .vbat_uv = 4071000, .ri_uohm = 158000 }, + { .vbat_uv = 4019000, .ri_uohm = 187000 }, + { .vbat_uv = 3951000, .ri_uohm = 191000 }, + { .vbat_uv = 3901000, .ri_uohm = 193000 }, + { .vbat_uv = 3850000, .ri_uohm = 273000 }, + { .vbat_uv = 3800000, .ri_uohm = 305000 }, + { .vbat_uv = 3750000, .ri_uohm = 205000 }, + { .vbat_uv = 3700000, .ri_uohm = 290000 }, + { .vbat_uv = 3650000, .ri_uohm = 262000 }, + { .vbat_uv = 3618000, .ri_uohm = 290000 }, + { .vbat_uv = 3505000, .ri_uohm = 235000 }, + { .vbat_uv = 3484000, .ri_uohm = 253000 }, + { .vbat_uv = 3413000, .ri_uohm = 243000 }, + { .vbat_uv = 3393000, .ri_uohm = 285000 }, + { .vbat_uv = 3361000, .ri_uohm = 281000 }, + { .vbat_uv = 3302000, .ri_uohm = 286000 }, + { .vbat_uv = 3280000, .ri_uohm = 250000 }, +}; + +struct power_supply_vbat_ri_table samsung_vbat2res_charging_eb535151vu[] = { + { .vbat_uv = 4190000, .ri_uohm = 214000 }, + { .vbat_uv = 4159000, .ri_uohm = 252000 }, + { .vbat_uv = 4121000, .ri_uohm = 245000 }, + { .vbat_uv = 4069000, .ri_uohm = 228000 }, + { .vbat_uv = 4046000, .ri_uohm = 229000 }, + { .vbat_uv = 4026000, .ri_uohm = 233000 }, + { .vbat_uv = 4007000, .ri_uohm = 240000 }, + { .vbat_uv = 3982000, .ri_uohm = 291000 }, + { .vbat_uv = 3945000, .ri_uohm = 276000 }, + { .vbat_uv = 3924000, .ri_uohm = 266000 }, + { .vbat_uv = 3910000, .ri_uohm = 258000 }, + { .vbat_uv = 3900000, .ri_uohm = 271000 }, + { .vbat_uv = 3844000, .ri_uohm = 279000 }, + { .vbat_uv = 3772000, .ri_uohm = 217000 }, + { .vbat_uv = 3673000, .ri_uohm = 208000 }, + { .vbat_uv = 3571000, .ri_uohm = 208000 }, + { .vbat_uv = 3510000, .ri_uohm = 228000 }, +}; + +struct power_supply_vbat_ri_table samsung_vbat2res_discharging_eb585157lu[] = { + { .vbat_uv = 4194000, .ri_uohm = 121000 }, + { .vbat_uv = 4169000, .ri_uohm = 188000 }, + { .vbat_uv = 4136000, .ri_uohm = 173000 }, + { .vbat_uv = 4108000, .ri_uohm = 158000 }, + { .vbat_uv = 4064000, .ri_uohm = 143000 }, + { .vbat_uv = 3956000, .ri_uohm = 160000 }, + { .vbat_uv = 3847000, .ri_uohm = 262000 }, + { .vbat_uv = 3806000, .ri_uohm = 280000 }, + { .vbat_uv = 3801000, .ri_uohm = 266000 }, + { .vbat_uv = 3794000, .ri_uohm = 259000 }, + { .vbat_uv = 3785000, .ri_uohm = 234000 }, + { .vbat_uv = 3779000, .ri_uohm = 227000 }, + { .vbat_uv = 3772000, .ri_uohm = 222000 }, + { .vbat_uv = 3765000, .ri_uohm = 221000 }, + { .vbat_uv = 3759000, .ri_uohm = 216000 }, + { .vbat_uv = 3754000, .ri_uohm = 206000 }, + { .vbat_uv = 3747000, .ri_uohm = 212000 }, + { .vbat_uv = 3743000, .ri_uohm = 208000 }, + { .vbat_uv = 3737000, .ri_uohm = 212000 }, + { .vbat_uv = 3733000, .ri_uohm = 200000 }, + { .vbat_uv = 3728000, .ri_uohm = 203000 }, + { .vbat_uv = 3722000, .ri_uohm = 207000 }, + { .vbat_uv = 3719000, .ri_uohm = 208000 }, + { .vbat_uv = 3715000, .ri_uohm = 209000 }, + { .vbat_uv = 3712000, .ri_uohm = 211000 }, + { .vbat_uv = 3709000, .ri_uohm = 210000 }, + { .vbat_uv = 3704000, .ri_uohm = 216000 }, + { .vbat_uv = 3701000, .ri_uohm = 218000 }, + { .vbat_uv = 3698000, .ri_uohm = 222000 }, + { .vbat_uv = 3694000, .ri_uohm = 218000 }, + { .vbat_uv = 3692000, .ri_uohm = 215000 }, + { .vbat_uv = 3688000, .ri_uohm = 224000 }, + { .vbat_uv = 3686000, .ri_uohm = 224000 }, + { .vbat_uv = 3683000, .ri_uohm = 228000 }, + { .vbat_uv = 3681000, .ri_uohm = 228000 }, + { .vbat_uv = 3679000, .ri_uohm = 229000 }, + { .vbat_uv = 3676000, .ri_uohm = 232000 }, + { .vbat_uv = 3675000, .ri_uohm = 229000 }, + { .vbat_uv = 3673000, .ri_uohm = 229000 }, + { .vbat_uv = 3672000, .ri_uohm = 223000 }, + { .vbat_uv = 3669000, .ri_uohm = 224000 }, + { .vbat_uv = 3666000, .ri_uohm = 224000 }, + { .vbat_uv = 3663000, .ri_uohm = 221000 }, + { .vbat_uv = 3660000, .ri_uohm = 218000 }, + { .vbat_uv = 3657000, .ri_uohm = 215000 }, + { .vbat_uv = 3654000, .ri_uohm = 212000 }, + { .vbat_uv = 3649000, .ri_uohm = 215000 }, + { .vbat_uv = 3644000, .ri_uohm = 215000 }, + { .vbat_uv = 3636000, .ri_uohm = 215000 }, + { .vbat_uv = 3631000, .ri_uohm = 206000 }, + { .vbat_uv = 3623000, .ri_uohm = 205000 }, + { .vbat_uv = 3616000, .ri_uohm = 193000 }, + { .vbat_uv = 3605000, .ri_uohm = 193000 }, + { .vbat_uv = 3600000, .ri_uohm = 198000 }, + { .vbat_uv = 3597000, .ri_uohm = 198000 }, + { .vbat_uv = 3592000, .ri_uohm = 203000 }, + { .vbat_uv = 3591000, .ri_uohm = 188000 }, + { .vbat_uv = 3587000, .ri_uohm = 188000 }, + { .vbat_uv = 3583000, .ri_uohm = 177000 }, + { .vbat_uv = 3577000, .ri_uohm = 170000 }, + { .vbat_uv = 3568000, .ri_uohm = 135000 }, + { .vbat_uv = 3552000, .ri_uohm = 54000 }, + { .vbat_uv = 3526000, .ri_uohm = 130000 }, + { .vbat_uv = 3501000, .ri_uohm = 48000 }, + { .vbat_uv = 3442000, .ri_uohm = 183000 }, + { .vbat_uv = 3326000, .ri_uohm = 372000 }, + { .vbat_uv = 3161000, .ri_uohm = 452000 }, +}; + +struct power_supply_vbat_ri_table samsung_vbat2res_charging_eb585157lu[] = { + { .vbat_uv = 4360000, .ri_uohm = 128000 }, + { .vbat_uv = 4325000, .ri_uohm = 130000 }, + { .vbat_uv = 4316000, .ri_uohm = 148000 }, + { .vbat_uv = 4308000, .ri_uohm = 162000 }, + { .vbat_uv = 4301000, .ri_uohm = 162000 }, + { .vbat_uv = 4250000, .ri_uohm = 162000 }, + { .vbat_uv = 4230000, .ri_uohm = 164000 }, + { .vbat_uv = 4030000, .ri_uohm = 164000 }, + { .vbat_uv = 4000000, .ri_uohm = 193000 }, + { .vbat_uv = 3950000, .ri_uohm = 204000 }, + { .vbat_uv = 3850000, .ri_uohm = 210000 }, + { .vbat_uv = 3800000, .ri_uohm = 230000 }, + { .vbat_uv = 3790000, .ri_uohm = 240000 }, + { .vbat_uv = 3780000, .ri_uohm = 311000 }, + { .vbat_uv = 3760000, .ri_uohm = 420000 }, + { .vbat_uv = 3700000, .ri_uohm = 504000 }, + { .vbat_uv = 3600000, .ri_uohm = 565000 }, +}; + +/* + * Temperature to internal resistance scaling tables. + * + * "resistance" is the percentage of the resistance determined from the voltage + * so this represents the capacity ratio at different temperatures. + * + * FIXME: the proper table is missing: Samsung does not provide the necessary + * temperature compensation tables so we just state 100% for every temperature. + * If you have the datasheets, please provide these tables. + */ +static struct power_supply_resistance_temp_table samsung_temp2res[] = { + { .temp = 50, .resistance = 100 }, + { .temp = 40, .resistance = 100 }, + { .temp = 30, .resistance = 100 }, + { .temp = 20, .resistance = 100 }, + { .temp = 10, .resistance = 100 }, + { .temp = 00, .resistance = 100 }, + { .temp = -10, .resistance = 100 }, + { .temp = -20, .resistance = 100 }, +}; + +/* + * Capacity tables for different Open Circuit Voltages (OCV). + * These must be sorted by falling OCV value. + */ + +static struct power_supply_battery_ocv_table samsung_ocv_cap_eb485159lu[] = { + { .ocv = 4330000, .capacity = 100}, + { .ocv = 4320000, .capacity = 99}, + { .ocv = 4283000, .capacity = 95}, + { .ocv = 4246000, .capacity = 92}, + { .ocv = 4211000, .capacity = 89}, + { .ocv = 4167000, .capacity = 85}, + { .ocv = 4146000, .capacity = 83}, + { .ocv = 4124000, .capacity = 81}, + { .ocv = 4062000, .capacity = 75}, + { .ocv = 4013000, .capacity = 70}, + { .ocv = 3977000, .capacity = 66}, + { .ocv = 3931000, .capacity = 60}, + { .ocv = 3914000, .capacity = 58}, + { .ocv = 3901000, .capacity = 57}, + { .ocv = 3884000, .capacity = 56}, + { .ocv = 3870000, .capacity = 55}, + { .ocv = 3862000, .capacity = 54}, + { .ocv = 3854000, .capacity = 53}, + { .ocv = 3838000, .capacity = 50}, + { .ocv = 3823000, .capacity = 47}, + { .ocv = 3813000, .capacity = 45}, + { .ocv = 3807000, .capacity = 43}, + { .ocv = 3800000, .capacity = 41}, + { .ocv = 3795000, .capacity = 40}, + { .ocv = 3786000, .capacity = 37}, + { .ocv = 3783000, .capacity = 35}, + { .ocv = 3773000, .capacity = 30}, + { .ocv = 3758000, .capacity = 25}, + { .ocv = 3745000, .capacity = 22}, + { .ocv = 3738000, .capacity = 20}, + { .ocv = 3733000, .capacity = 19}, + { .ocv = 3716000, .capacity = 17}, + { .ocv = 3709000, .capacity = 16}, + { .ocv = 3698000, .capacity = 15}, + { .ocv = 3687000, .capacity = 14}, + { .ocv = 3684000, .capacity = 13}, + { .ocv = 3684000, .capacity = 12}, + { .ocv = 3678000, .capacity = 10}, + { .ocv = 3671000, .capacity = 9}, + { .ocv = 3665000, .capacity = 8}, + { .ocv = 3651000, .capacity = 7}, + { .ocv = 3634000, .capacity = 6}, + { .ocv = 3601000, .capacity = 5}, + { .ocv = 3564000, .capacity = 4}, + { .ocv = 3516000, .capacity = 3}, + { .ocv = 3456000, .capacity = 2}, + { .ocv = 3381000, .capacity = 1}, + { .ocv = 3300000, .capacity = 0}, +}; + +/* Same capacity table is used by eb-l1m7flu, eb425161la, eb425161lu */ +static struct power_supply_battery_ocv_table samsung_ocv_cap_1500mah[] = { + { .ocv = 4328000, .capacity = 100}, + { .ocv = 4299000, .capacity = 99}, + { .ocv = 4281000, .capacity = 98}, + { .ocv = 4241000, .capacity = 95}, + { .ocv = 4183000, .capacity = 90}, + { .ocv = 4150000, .capacity = 87}, + { .ocv = 4116000, .capacity = 84}, + { .ocv = 4077000, .capacity = 80}, + { .ocv = 4068000, .capacity = 79}, + { .ocv = 4058000, .capacity = 77}, + { .ocv = 4026000, .capacity = 75}, + { .ocv = 3987000, .capacity = 72}, + { .ocv = 3974000, .capacity = 69}, + { .ocv = 3953000, .capacity = 66}, + { .ocv = 3933000, .capacity = 63}, + { .ocv = 3911000, .capacity = 60}, + { .ocv = 3900000, .capacity = 58}, + { .ocv = 3873000, .capacity = 55}, + { .ocv = 3842000, .capacity = 52}, + { .ocv = 3829000, .capacity = 50}, + { .ocv = 3810000, .capacity = 45}, + { .ocv = 3793000, .capacity = 40}, + { .ocv = 3783000, .capacity = 35}, + { .ocv = 3776000, .capacity = 30}, + { .ocv = 3762000, .capacity = 25}, + { .ocv = 3746000, .capacity = 20}, + { .ocv = 3739000, .capacity = 18}, + { .ocv = 3715000, .capacity = 15}, + { .ocv = 3700000, .capacity = 12}, + { .ocv = 3690000, .capacity = 10}, + { .ocv = 3680000, .capacity = 9}, + { .ocv = 3670000, .capacity = 7}, + { .ocv = 3656000, .capacity = 5}, + { .ocv = 3634000, .capacity = 4}, + { .ocv = 3614000, .capacity = 3}, + { .ocv = 3551000, .capacity = 2}, + { .ocv = 3458000, .capacity = 1}, + { .ocv = 3300000, .capacity = 0}, +}; + +static struct power_supply_battery_ocv_table samsung_ocv_cap_eb535151vu[] = { + { .ocv = 4178000, .capacity = 100}, + { .ocv = 4148000, .capacity = 99}, + { .ocv = 4105000, .capacity = 95}, + { .ocv = 4078000, .capacity = 92}, + { .ocv = 4057000, .capacity = 89}, + { .ocv = 4013000, .capacity = 85}, + { .ocv = 3988000, .capacity = 82}, + { .ocv = 3962000, .capacity = 77}, + { .ocv = 3920000, .capacity = 70}, + { .ocv = 3891000, .capacity = 65}, + { .ocv = 3874000, .capacity = 62}, + { .ocv = 3839000, .capacity = 59}, + { .ocv = 3816000, .capacity = 55}, + { .ocv = 3798000, .capacity = 50}, + { .ocv = 3778000, .capacity = 40}, + { .ocv = 3764000, .capacity = 30}, + { .ocv = 3743000, .capacity = 25}, + { .ocv = 3711000, .capacity = 20}, + { .ocv = 3691000, .capacity = 18}, + { .ocv = 3685000, .capacity = 15}, + { .ocv = 3680000, .capacity = 12}, + { .ocv = 3662000, .capacity = 10}, + { .ocv = 3638000, .capacity = 9}, + { .ocv = 3593000, .capacity = 7}, + { .ocv = 3566000, .capacity = 6}, + { .ocv = 3497000, .capacity = 4}, + { .ocv = 3405000, .capacity = 2}, + { .ocv = 3352000, .capacity = 1}, + { .ocv = 3300000, .capacity = 0}, +}; + +static struct power_supply_battery_ocv_table samsung_ocv_cap_eb585157lu[] = { + { .ocv = 4320000, .capacity = 100}, + { .ocv = 4296000, .capacity = 99}, + { .ocv = 4283000, .capacity = 98}, + { .ocv = 4245000, .capacity = 95}, + { .ocv = 4185000, .capacity = 90}, + { .ocv = 4152000, .capacity = 87}, + { .ocv = 4119000, .capacity = 84}, + { .ocv = 4077000, .capacity = 80}, + { .ocv = 4057000, .capacity = 78}, + { .ocv = 4048000, .capacity = 77}, + { .ocv = 4020000, .capacity = 74}, + { .ocv = 4003000, .capacity = 72}, + { .ocv = 3978000, .capacity = 69}, + { .ocv = 3955000, .capacity = 66}, + { .ocv = 3934000, .capacity = 63}, + { .ocv = 3912000, .capacity = 60}, + { .ocv = 3894000, .capacity = 58}, + { .ocv = 3860000, .capacity = 55}, + { .ocv = 3837000, .capacity = 52}, + { .ocv = 3827000, .capacity = 50}, + { .ocv = 3806000, .capacity = 45}, + { .ocv = 3791000, .capacity = 40}, + { .ocv = 3779000, .capacity = 35}, + { .ocv = 3770000, .capacity = 30}, + { .ocv = 3758000, .capacity = 25}, + { .ocv = 3739000, .capacity = 20}, + { .ocv = 3730000, .capacity = 18}, + { .ocv = 3706000, .capacity = 15}, + { .ocv = 3684000, .capacity = 13}, + { .ocv = 3675000, .capacity = 10}, + { .ocv = 3673000, .capacity = 9}, + { .ocv = 3665000, .capacity = 7}, + { .ocv = 3649000, .capacity = 5}, + { .ocv = 3628000, .capacity = 4}, + { .ocv = 3585000, .capacity = 3}, + { .ocv = 3525000, .capacity = 2}, + { .ocv = 3441000, .capacity = 1}, + { .ocv = 3300000, .capacity = 0}, +}; + +struct power_supply_maintenance_charge_table samsung_maint_charge_table[] = { + { + /* Maintenance charging phase A, 60 hours */ + .charge_current_max_ua = 600000, + .charge_voltage_max_uv = 4150000, + .charge_safety_timer_minutes = 60*60, + }, + { + /* Maintenance charging phase B, 200 hours */ + .charge_current_max_ua = 600000, + .charge_voltage_max_uv = 4100000, + .charge_safety_timer_minutes = 200*60, + } +}; + +struct samsung_sdi_battery samsung_sdi_batteries[] = { + { + /* + * Used in Samsung GT-I8190 "Golden" + * Data from vendor boardfile board-golden-[bm|battery].c + */ + .compatible = "samsung,eb-l1m7flu", + .name = "EB-L1M7FLU", + .info = { + .charge_full_design_uah = 1500000, + .technology = POWER_SUPPLY_TECHNOLOGY_LION, + .factory_internal_resistance_uohm = 100000, + .factory_internal_resistance_charging_uohm = 200000, + /* If you have data on this fix the min_design_uv */ + .voltage_min_design_uv = 3320000, + .voltage_max_design_uv = 4340000, + .overvoltage_limit_uv = 4500000, + .constant_charge_current_max_ua = 900000, + .constant_charge_voltage_max_uv = 4320000, + .charge_term_current_ua = 200000, + .charge_restart_voltage_uv = 4300000, + .maintenance_charge = samsung_maint_charge_table, + .maintenance_charge_size = ARRAY_SIZE(samsung_maint_charge_table), + .alert_low_temp_charge_current_ua = 300000, + .alert_low_temp_charge_voltage_uv = 4000000, + .alert_high_temp_charge_current_ua = 300000, + .alert_high_temp_charge_voltage_uv = 4000000, + .temp_min = -50, + .temp_alert_min = 0, + .temp_alert_max = 40, + .temp_max = 60, + .resist_table = samsung_temp2res, + .resist_table_size = ARRAY_SIZE(samsung_temp2res), + /* If you have tables for more temperatures, add them */ + .ocv_temp[0] = 25, + .ocv_table[0] = samsung_ocv_cap_1500mah, + .ocv_table_size[0] = ARRAY_SIZE(samsung_ocv_cap_1500mah), + .vbat2ri_discharging = samsung_vbat2res_discharging_eb_l1m7flu, + .vbat2ri_discharging_size = ARRAY_SIZE(samsung_vbat2res_discharging_eb_l1m7flu), + .vbat2ri_charging = samsung_vbat2res_charging_eb_l1m7flu, + .vbat2ri_charging_size = ARRAY_SIZE(samsung_vbat2res_charging_eb_l1m7flu), + .bti_resistance_ohm = 2400, + .bti_resistance_tolerance = 40, + }, + }, + { + /* + * Used in Samsung SGH-T599 "Codina TMO" and SGH-I407 "Kyle" + * Data from vendor boardfile board-kyle-[bm|battery].c + */ + .compatible = "samsung,eb425161la", + .name = "EB425161LA", + .info = { + .charge_full_design_uah = 1500000, + .technology = POWER_SUPPLY_TECHNOLOGY_LION, + .factory_internal_resistance_uohm = 136000, + .factory_internal_resistance_charging_uohm = 200000, + /* If you have data on this fix the min_design_uv */ + .voltage_min_design_uv = 3320000, + .voltage_max_design_uv = 4340000, + .overvoltage_limit_uv = 4500000, + .constant_charge_current_max_ua = 900000, + .constant_charge_voltage_max_uv = 4320000, + .charge_term_current_ua = 200000, + .charge_restart_voltage_uv = 4270000, + .maintenance_charge = samsung_maint_charge_table, + .maintenance_charge_size = ARRAY_SIZE(samsung_maint_charge_table), + .alert_low_temp_charge_current_ua = 300000, + .alert_low_temp_charge_voltage_uv = 4000000, + .alert_high_temp_charge_current_ua = 300000, + .alert_high_temp_charge_voltage_uv = 4000000, + .temp_min = -30, + .temp_alert_min = 0, + .temp_alert_max = 40, + .temp_max = 47, + .resist_table = samsung_temp2res, + .resist_table_size = ARRAY_SIZE(samsung_temp2res), + /* If you have tables for more temperatures, add them */ + .ocv_temp[0] = 25, + .ocv_table[0] = samsung_ocv_cap_1500mah, + .ocv_table_size[0] = ARRAY_SIZE(samsung_ocv_cap_1500mah), + .vbat2ri_discharging = samsung_vbat2res_discharging_eb425161la, + .vbat2ri_discharging_size = ARRAY_SIZE(samsung_vbat2res_discharging_eb425161la), + .vbat2ri_charging = samsung_vbat2res_charging_eb425161la, + .vbat2ri_charging_size = ARRAY_SIZE(samsung_vbat2res_charging_eb425161la), + .bti_resistance_ohm = 2400, + .bti_resistance_tolerance = 40, + }, + }, + { + /* + * Used in Samsung GT-I8160 "Codina" + * Data from vendor boardfile board-codina-[bm|battery].c + */ + .compatible = "samsung,eb425161lu", + .name = "EB425161LU", + .info = { + .charge_full_design_uah = 1500000, + .technology = POWER_SUPPLY_TECHNOLOGY_LION, + .factory_internal_resistance_uohm = 100000, + .factory_internal_resistance_charging_uohm = 200000, + /* If you have data on this fix the min_design_uv */ + .voltage_min_design_uv = 3320000, + .voltage_max_design_uv = 4350000, + .overvoltage_limit_uv = 4500000, + .constant_charge_current_max_ua = 900000, + .constant_charge_voltage_max_uv = 4340000, + .charge_term_current_ua = 200000, + .charge_restart_voltage_uv = 4280000, + .maintenance_charge = samsung_maint_charge_table, + .maintenance_charge_size = ARRAY_SIZE(samsung_maint_charge_table), + .alert_low_temp_charge_current_ua = 300000, + .alert_low_temp_charge_voltage_uv = 4000000, + .alert_high_temp_charge_current_ua = 300000, + .alert_high_temp_charge_voltage_uv = 4000000, + .temp_min = -50, + .temp_alert_min = 0, + .temp_alert_max = 43, + .temp_max = 49, + .resist_table = samsung_temp2res, + .resist_table_size = ARRAY_SIZE(samsung_temp2res), + /* If you have tables for more temperatures, add them */ + .ocv_temp[0] = 25, + .ocv_table[0] = samsung_ocv_cap_1500mah, + .ocv_table_size[0] = ARRAY_SIZE(samsung_ocv_cap_1500mah), + .vbat2ri_discharging = samsung_vbat2res_discharging_eb425161lu, + .vbat2ri_discharging_size = ARRAY_SIZE(samsung_vbat2res_discharging_eb425161lu), + .vbat2ri_charging = samsung_vbat2res_charging_eb425161lu, + .vbat2ri_charging_size = ARRAY_SIZE(samsung_vbat2res_charging_eb425161lu), + .bti_resistance_ohm = 2400, + .bti_resistance_tolerance = 40, + }, + }, + { + /* + * Used in Samsung GT-S7710 "Skomer" + * Data from vendor boardfile board-skomer-[bm|battery].c + */ + .compatible = "samsung,eb485159lu", + .name = "EB485159LU", + .info = { + .charge_full_design_uah = 1700000, + .technology = POWER_SUPPLY_TECHNOLOGY_LION, + .factory_internal_resistance_uohm = 100000, + .factory_internal_resistance_charging_uohm = 200000, + .voltage_min_design_uv = 3320000, + .voltage_max_design_uv = 4350000, + .overvoltage_limit_uv = 4500000, + .constant_charge_current_max_ua = 900000, + .constant_charge_voltage_max_uv = 4340000, + .charge_term_current_ua = 200000, + .charge_restart_voltage_uv = 4300000, + .maintenance_charge = samsung_maint_charge_table, + .maintenance_charge_size = ARRAY_SIZE(samsung_maint_charge_table), + .alert_low_temp_charge_current_ua = 300000, + .alert_low_temp_charge_voltage_uv = 4000000, + .alert_high_temp_charge_current_ua = 300000, + .alert_high_temp_charge_voltage_uv = 4000000, + .temp_min = -50, + .temp_alert_min = 0, + .temp_alert_max = 40, + .temp_max = 60, + .resist_table = samsung_temp2res, + .resist_table_size = ARRAY_SIZE(samsung_temp2res), + /* If you have tables for more temperatures, add them */ + .ocv_temp[0] = 25, + .ocv_table[0] = samsung_ocv_cap_eb485159lu, + .ocv_table_size[0] = ARRAY_SIZE(samsung_ocv_cap_eb485159lu), + /* CHECKME: vendor uses the 1500 mAh table, check against datasheet */ + .vbat2ri_discharging = samsung_vbat2res_discharging_eb485159lu, + .vbat2ri_discharging_size = ARRAY_SIZE(samsung_vbat2res_discharging_eb485159lu), + .vbat2ri_charging = samsung_vbat2res_charging_eb485159lu, + .vbat2ri_charging_size = ARRAY_SIZE(samsung_vbat2res_charging_eb485159lu), + .bti_resistance_ohm = 2400, + .bti_resistance_tolerance = 40, + }, + }, + { + /* + * Used in Samsung GT-I9070 "Janice" + * Data from vendor boardfile board-janice-bm.c + */ + .compatible = "samsung,eb535151vu", + .name = "EB535151VU", + .info = { + .charge_full_design_uah = 1500000, + .technology = POWER_SUPPLY_TECHNOLOGY_LION, + .factory_internal_resistance_uohm = 100000, + .factory_internal_resistance_charging_uohm = 200000, + /* If you have data on this fix the min_design_uv */ + .voltage_min_design_uv = 3300000, + .voltage_max_design_uv = 4180000, + .overvoltage_limit_uv = 4500000, + .constant_charge_current_max_ua = 900000, + .constant_charge_voltage_max_uv = 4200000, + .charge_term_current_ua = 200000, + .maintenance_charge = samsung_maint_charge_table, + .maintenance_charge_size = ARRAY_SIZE(samsung_maint_charge_table), + .alert_low_temp_charge_current_ua = 300000, + .alert_low_temp_charge_voltage_uv = 4000000, + .alert_high_temp_charge_current_ua = 300000, + .alert_high_temp_charge_voltage_uv = 4000000, + .temp_min = -5, + .temp_alert_min = 0, + .temp_alert_max = 40, + .temp_max = 60, + .resist_table = samsung_temp2res, + .resist_table_size = ARRAY_SIZE(samsung_temp2res), + /* If you have tables for more temperatures, add them */ + .ocv_temp[0] = 25, + .ocv_table[0] = samsung_ocv_cap_eb585157lu, + .ocv_table_size[0] = ARRAY_SIZE(samsung_ocv_cap_eb535151vu), + .vbat2ri_discharging = samsung_vbat2res_discharging_eb535151vu, + .vbat2ri_discharging_size = ARRAY_SIZE(samsung_vbat2res_discharging_eb535151vu), + .vbat2ri_charging = samsung_vbat2res_charging_eb535151vu, + .vbat2ri_charging_size = ARRAY_SIZE(samsung_vbat2res_charging_eb535151vu), + .bti_resistance_ohm = 1500, + .bti_resistance_tolerance = 40, + }, + }, + { + /* + * Used in Samsung GT-I8530 "Gavini" + * Data from vendor boardfile board-gavini-bm.c + */ + .compatible = "samsung,eb585157lu", + .name = "EB585157LU", + .info = { + .charge_full_design_uah = 2000000, + .technology = POWER_SUPPLY_TECHNOLOGY_LION, + .factory_internal_resistance_uohm = 105000, + .factory_internal_resistance_charging_uohm = 160000, + /* If you have data on this fix the min_design_uv */ + .voltage_min_design_uv = 3300000, + .voltage_max_design_uv = 4320000, + .overvoltage_limit_uv = 4500000, + .constant_charge_current_max_ua = 1500000, + .constant_charge_voltage_max_uv = 4350000, + .charge_term_current_ua = 120000, + .maintenance_charge = samsung_maint_charge_table, + .maintenance_charge_size = ARRAY_SIZE(samsung_maint_charge_table), + .alert_low_temp_charge_current_ua = 300000, + .alert_low_temp_charge_voltage_uv = 4000000, + .alert_high_temp_charge_current_ua = 300000, + .alert_high_temp_charge_voltage_uv = 4000000, + .temp_min = -5, + .temp_alert_min = 0, + .temp_alert_max = 40, + .temp_max = 60, + .resist_table = samsung_temp2res, + .resist_table_size = ARRAY_SIZE(samsung_temp2res), + /* If you have tables for more temperatures, add them */ + .ocv_temp[0] = 25, + .ocv_table[0] = samsung_ocv_cap_eb585157lu, + .ocv_table_size[0] = ARRAY_SIZE(samsung_ocv_cap_eb585157lu), + .vbat2ri_discharging = samsung_vbat2res_discharging_eb585157lu, + .vbat2ri_discharging_size = ARRAY_SIZE(samsung_vbat2res_discharging_eb585157lu), + .vbat2ri_charging = samsung_vbat2res_charging_eb585157lu, + .vbat2ri_charging_size = ARRAY_SIZE(samsung_vbat2res_charging_eb585157lu), + .bti_resistance_ohm = 2400, + .bti_resistance_tolerance = 40, + }, + }, +}; + +int samsung_sdi_battery_get_info(struct device *dev, + struct device_node *np, + struct power_supply_battery_info **info) +{ + struct samsung_sdi_battery *batt; + int i; + + for (i = 0; i < ARRAY_SIZE(samsung_sdi_batteries); i++) { + batt = &samsung_sdi_batteries[i]; + if (of_device_is_compatible(np, batt->compatible)) + break; + } + + if (i == ARRAY_SIZE(samsung_sdi_batteries)) + return -ENODEV; + + *info = &batt->info; + dev_info(dev, "Samsung SDI %s battery %d mAh\n", + batt->name, batt->info.charge_full_design_uah / 1000); + + return 0; +} +EXPORT_SYMBOL_GPL(samsung_sdi_battery_get_info); diff --git a/drivers/power/supply/samsung-sdi-battery.h b/drivers/power/supply/samsung-sdi-battery.h new file mode 100644 index 000000000000..08783847dfcb --- /dev/null +++ b/drivers/power/supply/samsung-sdi-battery.h @@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#if IS_ENABLED(CONFIG_BATTERY_SAMSUNG_SDI) +extern int samsung_sdi_battery_get_info(struct device *dev, + struct device_node *np, + struct power_supply_battery_info **info); +#else +static inline int samsung_sdi_battery_get_info(struct device *dev, + struct device_node *np, + struct power_supply_battery_info **info) +{ + return -ENODEV; +} +#endif