From patchwork Fri Feb 25 23:27:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 546100 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 36C75C433F5 for ; Fri, 25 Feb 2022 23:30:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239398AbiBYXbE (ORCPT ); Fri, 25 Feb 2022 18:31:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37704 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239367AbiBYXbD (ORCPT ); Fri, 25 Feb 2022 18:31: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 0B5D219F46D for ; Fri, 25 Feb 2022 15:30:29 -0800 (PST) Received: by mail-lj1-x22a.google.com with SMTP id s25so9416253lji.5 for ; Fri, 25 Feb 2022 15:30: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=XYau+YFuStXJjEyFXlgwl9Tl3iYN8y3LmvaJeN9U81c=; b=oN+UEV/ruOQtxoiASnE52e1tY2/LAOTN62idCluZpcnjgo0DiDhGHsIq9SQJEoTcrz c96rhH/wUBlUMvI2bfBTidIxs8mQeVQURcQypzqkXgJpKDYHVByZ5OCU5IyGHhCQLHFn drwXmE7H49oZJlguCgLQkS6BenCXdjRlvxMDUf/719j9SUjArKHM4pzOCBE+Zb2tJYaw MPoMteC4Ir+/9gxeacBsHW2XTfpOGJAt2kXFjR87pAsw5M1nlAsse0Dqy99e1a2tsGzt Unci4ZdxYr0UNYpntAIhm76nd9TXIF2QWvxP0K8wNdV88owk0cbtbQcfJLPt3c2soBHD 1IjA== 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=XYau+YFuStXJjEyFXlgwl9Tl3iYN8y3LmvaJeN9U81c=; b=KDSGsrF3T+rGd1Gtixkp5kiBPTlEYXu0siPaEccAEyhmEFLtXJ0DbwUvhZhYfmgebi jPIMhwpGmJQM6FUwH/flTD2yO5T/JVuTvkZPOIbUT+eJ6cd6nU+SUUJuFaOSwpIH1yGd UYNO+5ll/uNjMNdQqASLtZiXddYTmdpqHsXCPvZ2gu1lIvBvy3gCrmPoR82uN9PzRWjr /QZl6Z/XXzyML3lg2T+rfhOeURVvkw4OzbXLOHvLRKi94Sre08YBtRRnVhzToymyYZjb ijkQguitocTBMQ9aNBrPG254Tz7qdecU7XojBpWBzrh+AJP6zhxDX34ItcjsTGNiuioC /ROg== X-Gm-Message-State: AOAM531viowVzRfsJ7r3rzTxehMMvkVTxDpQn1HWnSq9biGeHDVCGEYl 6E7Qvw5eEPjEVnK/w76fNf2H/Rub2gFxWzT+ X-Google-Smtp-Source: ABdhPJy+hL+GGHHfbA48/TwUMKJR08rML7DZAfUayZsPt5JQWFNNZxfQSKx06MkhpUxtWEjsN5KQrA== X-Received: by 2002:a2e:908f:0:b0:246:4cf7:69c9 with SMTP id l15-20020a2e908f000000b002464cf769c9mr7012797ljg.149.1645831827347; Fri, 25 Feb 2022 15:30: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 u9-20020ac251c9000000b00443dc755dfdsm313524lfm.215.2022.02.25.15.30.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Feb 2022 15:30:26 -0800 (PST) From: Linus Walleij To: Sebastian Reichel , Marcus Cooper Cc: linux-pm@vger.kernel.org, Matti Vaittinen , Linus Walleij Subject: [PATCH 1/6 v4] power: supply: ab8500: Standardize maintenance charging Date: Sat, 26 Feb 2022 00:27:55 +0100 Message-Id: <20220225232800.2021909-2-linus.walleij@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220225232800.2021909-1-linus.walleij@linaro.org> References: <20220225232800.2021909-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 v3->v4: - Rebase on Sebastians linux-power-supply tree. 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 8dbd1197cc62..accbbd36bfe7 100644 --- a/drivers/power/supply/power_supply_core.c +++ b/drivers/power/supply/power_supply_core.c @@ -595,6 +595,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; @@ -844,6 +845,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 c135196aa9d1..8ced6550caa7 100644 --- a/include/linux/power_supply.h +++ b/include/linux/power_supply.h @@ -349,6 +349,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 /** @@ -394,6 +440,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 @@ -543,6 +593,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; @@ -596,6 +648,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 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); int power_supply_get_property_from_supplier(struct power_supply *psy, @@ -603,6 +657,16 @@ int power_supply_get_property_from_supplier(struct power_supply *psy, union power_supply_propval *val); 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 23:27:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 546365 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 DC55DC433EF for ; Fri, 25 Feb 2022 23:30:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239417AbiBYXbF (ORCPT ); Fri, 25 Feb 2022 18:31:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234931AbiBYXbE (ORCPT ); Fri, 25 Feb 2022 18:31:04 -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 7C6DE19E73E for ; Fri, 25 Feb 2022 15:30:30 -0800 (PST) Received: by mail-lf1-x133.google.com with SMTP id bu29so11989431lfb.0 for ; Fri, 25 Feb 2022 15:30: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=r2/2n3NnArDcR9rJNW+Z7XjpriuMlaTagPEbZnwTGBQ=; b=dt+7z0y+k4iQdSJUK7dI1MsqE3RT3W2Gw61Pg1l28LI7m3r1QOzyzewY5PHXU0NHGW bN/zwjrGqtpS4Qne89VhykdKh3gq0F5YZ+P1dQ5jyJqdDFQavI4CUuHf4MXq/m0b9PkU Hq0sBIsfOTv/+6dgfF4kBI5cOWAoHYoRRLSFQMIX79wyeW1A8ukF3zhOSWdH9+Em9v8i ucpCS2qFse+nf7BzM/5TI33MsehAGHmbLREILu4m3PfX/a/NvOLxNULw7thdwxlmLCXv /PUCc6eofgZm1zjPS4etzDTj8VnnTLKDQJEVT6MQGp/nh2YRNCI3CODM++sD/Ur48w01 Y/lg== 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=r2/2n3NnArDcR9rJNW+Z7XjpriuMlaTagPEbZnwTGBQ=; b=G0d8kBIU5ijtyrtbGMBezOHLA0162FOJWMrePVD/Rar8RTVabolKvSzYHlhobkXW7Y kgDDM6XvP7By2rpSRNzGu2oIthxvL9LCsHxW4+/wM18xxUge8luBZrsHCxr4Ob4kiFaI C0K6dIgWjoHl6wFxERobwJftKHqqdYvnG8lr5lEIBiwMN2YDfhsrsxlSbH0TOvRlVvvc vdaIGeN+NR4rLtl786bbwyQO+fGZrGZgVzDBDW1LC8ZEWtZmhLI4L9g6I07q0qMP+mC9 JrT0JVwhTQBOulCEVjEGXMEHr/rmvyUIMM/mGxIQa2WQjgTDPyfmF33ZL2UDnmCtsDHJ UutQ== X-Gm-Message-State: AOAM531xAbhCZmQBgU16wH90q1INCMNrFPND3TU9yDa49jsDP/S6DpI9 sOU9q5LRvKYmupN40JLWlG3YJuuqulgfjLC3 X-Google-Smtp-Source: ABdhPJyyuaBNx0Qx8NRLa/Rv3alIrO/GKLuCFbJXmsnj/NDM4wXTmBMW6lvgOYyhA0vgiM1Fkt/jzg== X-Received: by 2002:a05:6512:3408:b0:443:c898:520b with SMTP id i8-20020a056512340800b00443c898520bmr6009413lfr.465.1645831828834; Fri, 25 Feb 2022 15:30: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 u9-20020ac251c9000000b00443dc755dfdsm313524lfm.215.2022.02.25.15.30.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Feb 2022 15:30:28 -0800 (PST) From: Linus Walleij To: Sebastian Reichel , Marcus Cooper Cc: linux-pm@vger.kernel.org, Matti Vaittinen , Linus Walleij Subject: [PATCH 2/6 v4] power: supply: ab8500: Standardize alert mode charging Date: Sat, 26 Feb 2022 00:27:56 +0100 Message-Id: <20220225232800.2021909-3-linus.walleij@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220225232800.2021909-1-linus.walleij@linaro.org> References: <20220225232800.2021909-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 v3->v4: - Rebase on Sebastians linux-power-supply tree. 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 accbbd36bfe7..e3d6d3ff492a 100644 --- a/drivers/power/supply/power_supply_core.c +++ b/drivers/power/supply/power_supply_core.c @@ -596,6 +596,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 8ced6550caa7..f8601598d3d3 100644 --- a/include/linux/power_supply.h +++ b/include/linux/power_supply.h @@ -444,6 +444,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 @@ -595,6 +608,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 23:27:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 546099 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 BB0A3C433FE for ; Fri, 25 Feb 2022 23:30:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239458AbiBYXbK (ORCPT ); Fri, 25 Feb 2022 18:31:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234931AbiBYXbG (ORCPT ); Fri, 25 Feb 2022 18:31:06 -0500 Received: from mail-lj1-x230.google.com (mail-lj1-x230.google.com [IPv6:2a00:1450:4864:20::230]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2B2519F473 for ; Fri, 25 Feb 2022 15:30:31 -0800 (PST) Received: by mail-lj1-x230.google.com with SMTP id o6so9423549ljp.3 for ; Fri, 25 Feb 2022 15:30: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=gORRKkd56nVlZ1cfz7qEp6vIkxxD3KbRh4C514IVtu0=; b=NQ9VpbGJ6QQrJGgru63SQc7fE8AfyroO/UwUDQ53ptdzkQguMguGYQybJVXvkDbqP2 KLiK0+lLErkjgula/IL8Y4KXM/Fs0WlJKHQxmpqWVi0846wNGR7eMkCrTriCVVkMGsuu Qf95TCBJHh/OcEA01FELA2dddeDJppsymE1pOXSHdVoDUOEHZF+gEcj+EYBqkrsWVjZA GH8oaiO18TQL9VbXFZczq00poGwSyvD61dgW4Z0/em9bYDmIeW8TG+GsAgDgFEa/0CxP h4GCCwC6tsc7tS+Xf5GaNecfhdRrB/iV0FOeshF4ivAQWOeddckO6bUB05BM2gr9s+31 iwfg== 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=gORRKkd56nVlZ1cfz7qEp6vIkxxD3KbRh4C514IVtu0=; b=Yw8YGwrP176ryheTf2GCQiOFxjSEQyAZ/+KEMcfhp8l1TvWw9thCefFbZrTNeakEGx TtmMjPhD22xPnPab2vXQVlFchObnh3ctdcXixEdpDruoJp+ymUfkLrRHrmkeKsTu9OHl 9YbiZ0LivV2mlT6D8pofiNsBsePU1FwaBgkuOOqyWf4zqZWf0gZuJwOYhT0x24GxvtuF iKzPGKw80ZdVKa05FXXyyW9fIqOumAVQGK7NlA1LIK7mXkNFiecxrn3bIfObwf/LkV2D LiofxpIU5lOMpWmxO4zubdq0mPpZaAPpcugM1tez2eH+e2C202OTOkT2vv10A1Yi3Iuy yxKQ== X-Gm-Message-State: AOAM530nQswXzvS9Qxusc8/OJxQeDEv7nMuDW2d0e/1buqnrPz7XeTM9 PxYB4IwnDCaVHWdPXDpMP907PA== X-Google-Smtp-Source: ABdhPJyAn6xl7sDqnfTIhDB1L7u4SIu3ARo579+94q/hSD29FTVK5C+uTIhIELV7KYXU1T7tu7Uqbg== X-Received: by 2002:a2e:bc17:0:b0:246:32b7:464 with SMTP id b23-20020a2ebc17000000b0024632b70464mr6665359ljf.506.1645831830222; Fri, 25 Feb 2022 15:30: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 u9-20020ac251c9000000b00443dc755dfdsm313524lfm.215.2022.02.25.15.30.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Feb 2022 15:30:29 -0800 (PST) From: Linus Walleij To: Sebastian Reichel , Marcus Cooper Cc: linux-pm@vger.kernel.org, Matti Vaittinen , Linus Walleij Subject: [PATCH 3/6 v4] power: supply: ab8500: Standardize BTI resistance Date: Sat, 26 Feb 2022 00:27:57 +0100 Message-Id: <20220225232800.2021909-4-linus.walleij@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220225232800.2021909-1-linus.walleij@linaro.org> References: <20220225232800.2021909-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 v3->v4: - Rebased on Sebastians linux-power-supply tree. 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 0227e800c58d..f2ff3103e0d0 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 e3d6d3ff492a..3d5047d3fe99 100644 --- a/drivers/power/supply/power_supply_core.c +++ b/drivers/power/supply/power_supply_core.c @@ -607,7 +607,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; @@ -938,6 +940,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 f8601598d3d3..7fdc03cf2285 100644 --- a/include/linux/power_supply.h +++ b/include/linux/power_supply.h @@ -498,6 +498,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 @@ -624,6 +632,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; @@ -667,6 +677,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); int power_supply_get_property_from_supplier(struct power_supply *psy, @@ -684,6 +696,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 23:27:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 546098 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 35DB0C4332F for ; Fri, 25 Feb 2022 23:30:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234931AbiBYXbL (ORCPT ); Fri, 25 Feb 2022 18:31:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37972 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239336AbiBYXbH (ORCPT ); Fri, 25 Feb 2022 18:31:07 -0500 Received: from mail-lj1-x236.google.com (mail-lj1-x236.google.com [IPv6:2a00:1450:4864:20::236]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6909019F46C for ; Fri, 25 Feb 2022 15:30:33 -0800 (PST) Received: by mail-lj1-x236.google.com with SMTP id u11so9427676lju.4 for ; Fri, 25 Feb 2022 15:30: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=uRYL5qnIiMzP7BZ/K3ui2HkKddDsnn1d6EQhsmYHaiA=; b=WqadAUSSFuXFp8kCenPmSduX0AywHe5n6eyxXO0QgACaWP/Fg82H2QMcHVBfoTGAxY MoZxUZq9X5z2mbVeatZpTutI+snle4qfQgA2YqxJ2LLXchR/DypgU6A1TIblz68FS83k X7erPESZR8wO0J6cfIGnOvpvVEOpFBE0PdMl3ZTWxc+6vtxsT0IxAxygmYzDN4rJm+6a /SpT9oWKYXbNxrV83no1uGKUl0HNa9hpFW2J2BWSLZ0IehWvvxY9uzUqAp+Iic2rjf6n ZP28L8BxmHqBbJOnYYMumx4MhbfUnVH9iAdpIXszGU1N9HHdqXajiduNqi2vsLVwBK8r 8zcg== 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=uRYL5qnIiMzP7BZ/K3ui2HkKddDsnn1d6EQhsmYHaiA=; b=OauuI5PUrWOmWBAIT0RSjFpuQ8oB6lOPHTIiqLKzWvB+LUGlWtHjDWTaeQdd0uYNks Xwd1v9sStSfUv/3IJWeG9bT4O6eE/0wGc8y7arrhLpSnsz3z+ZM/t60uRNiutnBl1hpq DJzz4Y9G2BeodSizQBMVuwWIkjPwmOXRXoD19mhSylAaixiK9SIX94wMIixJ6rCSVsFo hQ8+CsaP0T8wuzMuQp8e3RGgUxFQjsN8INTAQ/oIJTlr3RROv2JKzJ/7FOr0rbXTGYEi XUJPwbcglaS2ee9g7cpD/HPSoiyouGthme0DeNgHkpiJd1VJAytMpa/SNM7jNjLldcSY EZSQ== X-Gm-Message-State: AOAM531rZdrGhmqjyhbGvIdAxFwgupgoaxylhAIhNt1EcyKF6jGs5UMW PAFgygpVU73QAORW78qgXW6Ud45D2NU8Z4ha X-Google-Smtp-Source: ABdhPJwXXi/HxRzu/2Dsd4ctVn88xqPfnM8Pd3ZWTKz4uIaAcZIrh3VgRa9Xj67C2nungJdAJTU0Gw== X-Received: by 2002:a2e:a36c:0:b0:246:4680:e55a with SMTP id i12-20020a2ea36c000000b002464680e55amr6811653ljn.451.1645831831688; Fri, 25 Feb 2022 15:30: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 u9-20020ac251c9000000b00443dc755dfdsm313524lfm.215.2022.02.25.15.30.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Feb 2022 15:30:31 -0800 (PST) From: Linus Walleij To: Sebastian Reichel , Marcus Cooper Cc: linux-pm@vger.kernel.org, Matti Vaittinen , Linus Walleij Subject: [PATCH 4/6 v4] power: supply: Support VBAT-to-Ri lookup tables Date: Sat, 26 Feb 2022 00:27:58 +0100 Message-Id: <20220225232800.2021909-5-linus.walleij@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220225232800.2021909-1-linus.walleij@linaro.org> References: <20220225232800.2021909-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 v3->v4: - Rebase on Sebastians linux-power-supply tree. 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 3d5047d3fe99..fb0b3870566e 100644 --- a/drivers/power/supply/power_supply_core.c +++ b/drivers/power/supply/power_supply_core.c @@ -814,7 +814,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 @@ -851,6 +851,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 7fdc03cf2285..cb380c1d9459 100644 --- a/include/linux/power_supply.h +++ b/include/linux/power_supply.h @@ -349,6 +349,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 @@ -460,7 +465,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 @@ -498,6 +510,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 @@ -512,7 +539,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 @@ -597,6 +626,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. */ @@ -621,6 +710,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; @@ -632,6 +722,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; }; @@ -675,6 +769,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, @@ -696,6 +792,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 23:27:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 546364 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 5FE00C433EF for ; Fri, 25 Feb 2022 23:30:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239433AbiBYXbL (ORCPT ); Fri, 25 Feb 2022 18:31:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38032 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239434AbiBYXbI (ORCPT ); Fri, 25 Feb 2022 18:31:08 -0500 Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [IPv6:2a00:1450:4864:20::12d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9D21119F46D for ; Fri, 25 Feb 2022 15:30:34 -0800 (PST) Received: by mail-lf1-x12d.google.com with SMTP id j7so11860059lfu.6 for ; Fri, 25 Feb 2022 15:30:34 -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=UA8R2LFEhf2Y+8DFX3dyw367vtbvKF6B0Y9QLNF8IMs=; b=KBvQ6HDwadQMXp+yhzXUXi//XdpRRI1m9+HOilyrBJj5yxaFn+Cd+Jrh297bu31lj+ 4ZiFdK7Uu+bDVRw2sl0zVrXXtGf3HsUOfcDvxUZZikJbtU0hQnIW5j6gViCK565aTmyq XJ0mPB3r8HMMSX/C5xpK/Kg4ZvUUboSkcldKdxxvHqbalyv7yqosYYl798WCOEnHJ+Cn kXMJuHhjm+l+ClDg7wWHtdPLgIvPqDtJc02hr4frfPXg68gc36SD3oDGnh4HT7e+4xD2 Bwistc+kktt+M/fK9S51PYq1RhqGfBoS7XpH43QuNVVms4ZjryilRRd+hNMKshJGbOZa 5MvQ== 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=UA8R2LFEhf2Y+8DFX3dyw367vtbvKF6B0Y9QLNF8IMs=; b=Lbz7bEdeWp8KhHbf4GJpBB30jT3gSoiYiIGV+1DuduWkql1dnSpLDjJtUsEyymX00L 0NJvmZzHhNk9OT7KtBemow978gRWZUx1NJ+uzfxTFRP03ExhRZyaXDJw+PKm5PdTJa+o h1RwDC63xyCphXnyzYftQ1/vW9TPWNgoQKU2nBQZaf7ZxhxJ/3jcO34dATNMF4CQVJwJ 0rrcE2AIvIcoEUWDUI6uxrfxmf1k8PVO4522yG2UwQRPpk0mUhTsqecXxFifoTmt9OyY 4bFlI9EFUb7FjiorsO+owMH35BMwAH9USt7LidQO+XueFMBbbHB79OWkQ1F37gof35vl vfRQ== X-Gm-Message-State: AOAM531U2GFCTb6xkkmSh2lnTxohXtrh4oNF2hiBdRnlIM1Hjp4cZYEE trt9tRY3VyGL+a8xC9b0eQ/scD+75Jrxg696 X-Google-Smtp-Source: ABdhPJwJE5U2lfce1zI97xI+qQoQYynqXUzZCDaXXqMTUbEoArrkOgrARMjvwnLnIEftLc76lOSgsw== X-Received: by 2002:a05:6512:48c:b0:443:3d4f:e559 with SMTP id v12-20020a056512048c00b004433d4fe559mr6185030lfq.309.1645831832989; Fri, 25 Feb 2022 15:30:32 -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 u9-20020ac251c9000000b00443dc755dfdsm313524lfm.215.2022.02.25.15.30.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Feb 2022 15:30:32 -0800 (PST) From: Linus Walleij To: Sebastian Reichel , Marcus Cooper Cc: linux-pm@vger.kernel.org, Matti Vaittinen , Linus Walleij Subject: [PATCH 5/6 v4] power: supply: ab8500_fg: Use VBAT-to-Ri if possible Date: Sat, 26 Feb 2022 00:27:59 +0100 Message-Id: <20220225232800.2021909-6-linus.walleij@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220225232800.2021909-1-linus.walleij@linaro.org> References: <20220225232800.2021909-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 v3->v4: - Rebase on Sebastians linux-power-supply tree. 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 f2ff3103e0d0..1abe10c7ff2a 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 23:28:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 546363 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 D92A3C43217 for ; Fri, 25 Feb 2022 23:30:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239336AbiBYXbN (ORCPT ); Fri, 25 Feb 2022 18:31:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239420AbiBYXbM (ORCPT ); Fri, 25 Feb 2022 18:31:12 -0500 Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [IPv6:2a00:1450:4864:20::12d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE59C1AF8F2 for ; Fri, 25 Feb 2022 15:30:36 -0800 (PST) Received: by mail-lf1-x12d.google.com with SMTP id u20so11887663lff.2 for ; Fri, 25 Feb 2022 15:30:36 -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=1SfT6HClw/5bUKU9Tjz2QjFq64nn2/hoRIPK2rS1IlY=; b=yvW3F/lsJbq+QEQx/+lXb3RF/e1TpOpWY0mewKxWaO8E498mTzDxFU1s7yB8O/vA3B Pa+UN1rUHcNbo87fxT+k7J/pMmlmgJc8Baq47k6VA7P0ZqIw21ZQCWCI6Zd4UOae71XW 0r0msNJQo6oA0xaV9ixFgxrSSsfBgIClTl82iQpPoZu2YFTA6yqgsBrEI1AWMoUyfPWJ On44BPdx26eG7ACZ0/yNqUD0FclnbYlKTnboW9H7V5uRcey0cdr/1XQ+n1BYfqkIzXjj EiyunlSRiMgqpRsoi+cRR0UHwekZtVd8qRzYGGQtouYKBK9kIn/sQT6e0tSb2WJsQI9r MQog== 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=1SfT6HClw/5bUKU9Tjz2QjFq64nn2/hoRIPK2rS1IlY=; b=qP/LphXyTtwPaoc9xDcf9aH24TnvCToWDuE4HCVU2+mQzYmDulOZicTfyN3ThKmLrn 1LOjBGR2/IRXnlZKgxGmm7FzR+CRnMV+M0yI3FBQGpVOq3Or2hxmJe+JT6bcdKhnmmB+ T2WiecPgNS+acMWSf93Wr4JiXp2rxVoCDPZWqX93B1xIW5cGwjtC3hNCYkOoZ35aDacA RyaYTcnf0AaZjN8EUXJg44OURdzD++6RJ+gjWZAn1pTqfn0iQZogo5iRel9AkHVfefNy 6mcN05XhneVcPtpXo4RRSQW+k1DP5HdZeHtZpoPzJ8vkBWKIjOT7VqMNiXAAudXCUh3l MyRg== X-Gm-Message-State: AOAM5300Cj6457VNWdZn8szddZOmnISSEje/RBZO+1lGh2lZKy29VzQ8 8FbpcdFJmbjCWE8latj9PB2VzA== X-Google-Smtp-Source: ABdhPJwraY7m94T/I+vGlBiLrmj0kqlQXATksmjkCaU5Y4aIou2scnjcZuz1JeLfRtnka9qdwn3oOg== X-Received: by 2002:a05:6512:1093:b0:443:3d8d:84af with SMTP id j19-20020a056512109300b004433d8d84afmr6409521lfg.334.1645831834957; Fri, 25 Feb 2022 15:30:34 -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 u9-20020ac251c9000000b00443dc755dfdsm313524lfm.215.2022.02.25.15.30.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Feb 2022 15:30:34 -0800 (PST) From: Linus Walleij To: Sebastian Reichel , Marcus Cooper Cc: linux-pm@vger.kernel.org, Matti Vaittinen , Linus Walleij Subject: [PATCH 6/6 v4] power: supply: Static data for Samsung batteries Date: Sat, 26 Feb 2022 00:28:00 +0100 Message-Id: <20220225232800.2021909-7-linus.walleij@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220225232800.2021909-1-linus.walleij@linaro.org> References: <20220225232800.2021909-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 v3->v4: - Rebase on Sebastians linux-power-supply tree. - One more oneline bug in the Janice charging parameter block. - Add static keyword to tables 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 | 8 + drivers/power/supply/samsung-sdi-battery.c | 919 +++++++++++++++++++++ drivers/power/supply/samsung-sdi-battery.h | 13 + 5 files changed, 947 insertions(+) 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 8f9033679f49..fa128b9f6e63 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 edf983676799..b87eb8d87bac 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 fb0b3870566e..44816d41b4ec 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; @@ -578,6 +579,13 @@ int power_supply_get_battery_info(struct power_supply *psy, const __be32 *list; u32 min_max[2]; + /* 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; diff --git a/drivers/power/supply/samsung-sdi-battery.c b/drivers/power/supply/samsung-sdi-battery.c new file mode 100644 index 000000000000..64ec34a41c5b --- /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. + */ + +static 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 }, +}; + +static 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 }, +}; + +static 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 }, +}; + +static 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 }, +}; + +static 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 }, +}; + +static 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 }, +}; + +static 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 }, +}; + +static 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 }, +}; + +static 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 }, +}; + +static 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 }, +}; + +static 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 }, +}; + +static 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}, +}; + +static 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, + } +}; + +static 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_eb535151vu, + .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