From: Ivaylo Dimitrov <ivo.g.dimitrov.75@gmail.com>
To: sre@kernel.org
Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org,
tony@atomide.com, philipp@uvos.xyz
Subject: Re: [PATCH 3/3] power: supply: cpcap_battery: Read battery parameters from nvram
Date: Mon, 5 Dec 2022 22:28:02 +0200 [thread overview]
Message-ID: <6d5e2890-a86b-1b9b-bb8f-474838d9bd86@gmail.com> (raw)
In-Reply-To: <1667647544-12945-4-git-send-email-ivo.g.dimitrov.75@gmail.com>
ping
On 5.11.22 г. 13:25 ч., Ivaylo Dimitrov wrote:
> Formulas were taken from android blob
>
> Signed-off-by: Ivaylo Dimitrov <ivo.g.dimitrov.75@gmail.com>
> ---
> drivers/power/supply/cpcap-battery.c | 88 ++++++++++++++++++++----------------
> 1 file changed, 48 insertions(+), 40 deletions(-)
>
> diff --git a/drivers/power/supply/cpcap-battery.c b/drivers/power/supply/cpcap-battery.c
> index ca6ee2b..92aa66c 100644
> --- a/drivers/power/supply/cpcap-battery.c
> +++ b/drivers/power/supply/cpcap-battery.c
> @@ -74,9 +74,6 @@
>
> #define CPCAP_BATTERY_CC_SAMPLE_PERIOD_MS 250
>
> -#define CPCAP_BATTERY_EB41_HW4X_ID 0x9E
> -#define CPCAP_BATTERY_BW8X_ID 0x98
> -
> enum {
> CPCAP_BATTERY_IIO_BATTDET,
> CPCAP_BATTERY_IIO_VOLTAGE,
> @@ -388,22 +385,9 @@ static int cpcap_battery_cc_to_ua(struct cpcap_battery_ddata *ddata,
> * kernel on droid 4, full is 4351000 and software initiates shutdown
> * at 3078000. The device will die around 2743000.
> */
> -static const struct cpcap_battery_config cpcap_battery_eb41_data = {
> - .cd_factor = 0x3cc,
> - .info.technology = POWER_SUPPLY_TECHNOLOGY_LION,
> - .info.voltage_max_design = 4351000,
> - .info.voltage_min_design = 3100000,
> - .info.charge_full_design = 1740000,
> - .bat.constant_charge_voltage_max_uv = 4200000,
> -};
> -
> -/* Values for the extended Droid Bionic battery bw8x. */
> -static const struct cpcap_battery_config cpcap_battery_bw8x_data = {
> +static struct cpcap_battery_config cpcap_battery_mot_data = {
> .cd_factor = 0x3cc,
> .info.technology = POWER_SUPPLY_TECHNOLOGY_LION,
> - .info.voltage_max_design = 4200000,
> - .info.voltage_min_design = 3200000,
> - .info.charge_full_design = 2760000,
> .bat.constant_charge_voltage_max_uv = 4200000,
> };
>
> @@ -431,39 +415,63 @@ static int cpcap_battery_match_nvmem(struct device *dev, const void *data)
> static void cpcap_battery_detect_battery_type(struct cpcap_battery_ddata *ddata)
> {
> struct nvmem_device *nvmem;
> - u8 battery_id = 0;
> + char buf[24];
> + u8 capacity;
> + u8 mul_idx;
> + u8 charge_voltage;
> + u32 v;
> + static const u32 multipliers[] = {20, 10, 10, 10, 10, 40, 10, 20, 40};
>
> ddata->check_nvmem = false;
>
> nvmem = nvmem_device_find(NULL, &cpcap_battery_match_nvmem);
> if (IS_ERR_OR_NULL(nvmem)) {
> - ddata->check_nvmem = true;
> dev_info_once(ddata->dev, "Can not find battery nvmem device. Assuming generic lipo battery\n");
> - } else {
> - char buf[24];
> -
> - if (nvmem_device_read(nvmem, 96, 4, buf) < 0 ||
> - strncmp(buf, "COPR", 4) != 0 ||
> - nvmem_device_read(nvmem, 104, 24, buf) < 0 ||
> - strncmp(buf, "MOTOROLA E.P CHARGE ONLY", 24) != 0 ||
> - nvmem_device_read(nvmem, 2, 1, &battery_id) < 0) {
> - battery_id = 0;
> - ddata->check_nvmem = true;
> - dev_warn(ddata->dev, "Can not read battery nvmem device. Assuming generic lipo battery\n");
> - }
> + goto unknown;
> + }
>
> + if (nvmem_device_read(nvmem, 96, 4, buf) < 0 ||
> + strncmp(buf, "COPR", 4) != 0 ||
> + nvmem_device_read(nvmem, 104, 24, buf) < 0 ||
> + strncmp(buf, "MOTOROLA E.P CHARGE ONLY", 24) != 0) {
> + dev_warn(ddata->dev, "Unknown battery nvmem device. Assuming generic lipo battery\n");
> + goto unknown;
> }
>
> - switch (battery_id) {
> - case CPCAP_BATTERY_EB41_HW4X_ID:
> - ddata->config = cpcap_battery_eb41_data;
> - break;
> - case CPCAP_BATTERY_BW8X_ID:
> - ddata->config = cpcap_battery_bw8x_data;
> - break;
> - default:
> - ddata->config = cpcap_battery_unkown_data;
> + if (nvmem_device_read(nvmem, 49, 1, &mul_idx) < 0 ||
> + nvmem_device_read(nvmem, 34, 1, &capacity) < 0 ||
> + nvmem_device_read(nvmem, 65, 1, &charge_voltage) < 0) {
> + dev_warn(ddata->dev, "Can not read battery nvmem device. Assuming generic lipo battery\n");
> + goto unknown;
> }
> +
> + /* design capacity */
> + mul_idx -= 2;
> +
> + if (mul_idx < ARRAY_SIZE(multipliers))
> + v = multipliers[mul_idx];
> + else
> + v = 10;
> +
> + cpcap_battery_mot_data.info.charge_full_design = 1000 * v * capacity;
> +
> + /* design max voltage */
> + v = 1000 * ((16702 * charge_voltage) / 1000 + 1260);
> + cpcap_battery_mot_data.info.voltage_max_design = v;
> +
> + /* design min voltage */
> + if (v > 4200000)
> + cpcap_battery_mot_data.info.voltage_min_design = 3100000;
> + else
> + cpcap_battery_mot_data.info.voltage_min_design = 3200000;
> +
> + ddata->config = cpcap_battery_mot_data;
> +
> + return;
> +
> +unknown:
> + ddata->check_nvmem = true;
> + ddata->config = cpcap_battery_unkown_data;
> }
>
> /**
>
next prev parent reply other threads:[~2022-12-05 20:28 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-05 11:25 [PATCH v2 0/3] power: supply: cpcap-battery improvements Ivaylo Dimitrov
2022-11-05 11:25 ` [PATCH 1/3] power: cpcap-battery: Do not issue low signal too frequently Ivaylo Dimitrov
2022-11-10 15:55 ` Sebastian Reichel
2022-11-10 18:13 ` Ivaylo Dimitrov
2022-11-11 6:15 ` Dan Carpenter
2022-11-05 11:25 ` [PATCH 2/3] power: supply: cpcap-battery: Fix battery identification Ivaylo Dimitrov
2022-11-10 16:05 ` Sebastian Reichel
2022-11-10 16:50 ` Ivaylo Dimitrov
2022-11-15 13:49 ` Tony Lindgren
2022-11-15 15:41 ` Ivaylo Dimitrov
2022-11-17 4:53 ` Tony Lindgren
2022-11-17 8:15 ` Carl Klemm
2022-11-22 7:15 ` Tony Lindgren
2022-11-05 11:25 ` [PATCH 3/3] power: supply: cpcap_battery: Read battery parameters from nvram Ivaylo Dimitrov
2022-12-05 20:28 ` Ivaylo Dimitrov [this message]
-- strict thread matches above, loose matches on Subject: below --
2022-11-01 19:53 [PATCH 0/3] power: supply: cpcap-battery improvements Ivaylo Dimitrov
2022-11-01 19:53 ` [PATCH 3/3] power: supply: cpcap_battery: Read battery parameters from nvram Ivaylo Dimitrov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6d5e2890-a86b-1b9b-bb8f-474838d9bd86@gmail.com \
--to=ivo.g.dimitrov.75@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=philipp@uvos.xyz \
--cc=sre@kernel.org \
--cc=tony@atomide.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).