Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp496942pxb; Thu, 14 Jan 2021 10:52:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJwtMANBWJBW9MJRY1/hcICDlSCQmZ7fSltDqwKE9ROdMh88MxnxhelJK3kQjefoV82rD+Iw X-Received: by 2002:a17:906:828a:: with SMTP id h10mr2483824ejx.274.1610650355077; Thu, 14 Jan 2021 10:52:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610650355; cv=none; d=google.com; s=arc-20160816; b=Dy1q2KTM/nHpUFRBbkZZLsOOLLYG+JjuRdyqyOvv/J4Gxo3h/4TMcAFeR0ly7xpm5Z Nsb4xqPH6McR8y2m9aFh15FHMbdyx3vY4DLKRx/sTECfyTfW3KSBUmew1DCjo2cjWDj1 gPx9/L49/2ZKMVYub6yELuDVm+l9P7j1aHKiDuoeWbf3KsIpVHGqqvLBlUvFuUSY/ocX smPVmwue+DvXOVtX9se2eAfGlNJ8CzTs6gDQHaov6UKdbHTwKe1in9yDKPcrg2FjDL9B 2k0H8mjiqucgh1UgtzYSN9MxdRI0koJm3CAU8OF4yZO/21Lhf2R2iKtyhNWGZCyHyyjq 92yg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=QIJOvr1AR7Odpji6rCxbUskgzf23Co6PZR8+G6fLZ0s=; b=nDdmwu6UoH2MyGfVZpFcUm03Kcb/qZ/BsQRJyV0GBUo3s7w5AGztvSnlUwIbgSm5Qy xzh8iqxbX35Q9cW7qgWWXX5ocUJ0mwm82BlKFHyzkvFop4zA99Hz97OevS8aGNUGa4vQ fzz2z0n1UmCrwAkosIQRxXqqrG64p8Sa+0GyLg2CfRJsvWlHO6rSe3mc+e927NVKq+i+ Z7gw54GadK7sxM452OffbxeHohFKi3Ht7RFKfGQYrbj9bP+/MLiznsnVw+5nxGv+gfjx lNd33EuVtF4CbvyXdJHwYSQXCdbzNxX5FAWPJV+zYfL7NRlR4e/BBXXPkF1l3ItFTXpC E5fA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=collabora.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a20si2002918edn.43.2021.01.14.10.52.11; Thu, 14 Jan 2021 10:52:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727637AbhANSug (ORCPT + 99 others); Thu, 14 Jan 2021 13:50:36 -0500 Received: from bhuna.collabora.co.uk ([46.235.227.227]:52108 "EHLO bhuna.collabora.co.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726162AbhANSug (ORCPT ); Thu, 14 Jan 2021 13:50:36 -0500 Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: sre) with ESMTPSA id 0F7451F45A80 Received: by earth.universe (Postfix, from userid 1000) id 0F4FB3C0C94; Thu, 14 Jan 2021 19:49:51 +0100 (CET) Date: Thu, 14 Jan 2021 19:49:51 +0100 From: Sebastian Reichel To: Dmitry Osipenko Cc: Lee Jones , Rob Herring , Thierry Reding , Jonathan Hunter , Dan Murphy , devicetree@vger.kernel.org, linux-tegra@vger.kernel.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v8 3/3] power: supply: Add battery gauge driver for Acer Iconia Tab A500 Message-ID: <20210114184951.4itiy5kfqjcxkoms@earth.universe> References: <20201228160547.30562-1-digetx@gmail.com> <20201228160547.30562-4-digetx@gmail.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="fanhwzk2b46wa3oi" Content-Disposition: inline In-Reply-To: <20201228160547.30562-4-digetx@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --fanhwzk2b46wa3oi Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hi, On Mon, Dec 28, 2020 at 07:05:47PM +0300, Dmitry Osipenko wrote: > This patch adds battery gauge driver for Acer Iconia Tab A500 device. > The battery gauge function is provided via the Embedded Controller, > which is found on the Acer A500. >=20 > Reviewed-by: Sebastian Reichel > Signed-off-by: Dmitry Osipenko > --- Thanks, queued. -- Sebastian > drivers/power/supply/Kconfig | 6 + > drivers/power/supply/Makefile | 1 + > drivers/power/supply/acer_a500_battery.c | 297 +++++++++++++++++++++++ > 3 files changed, 304 insertions(+) > create mode 100644 drivers/power/supply/acer_a500_battery.c >=20 > diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig > index eec646c568b7..bc493173ddbc 100644 > --- a/drivers/power/supply/Kconfig > +++ b/drivers/power/supply/Kconfig > @@ -774,4 +774,10 @@ config RN5T618_POWER > This driver can also be built as a module. If so, the module will be > called rn5t618_power. > =20 > +config BATTERY_ACER_A500 > + tristate "Acer Iconia Tab A500 battery driver" > + depends on MFD_ACER_A500_EC > + help > + Say Y to include support for Acer Iconia Tab A500 battery fuel gauge. > + > endif # POWER_SUPPLY > diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile > index dd4b86318cd9..0607a3d64c0f 100644 > --- a/drivers/power/supply/Makefile > +++ b/drivers/power/supply/Makefile > @@ -98,3 +98,4 @@ obj-$(CONFIG_CHARGER_BD70528) +=3D bd70528-charger.o > obj-$(CONFIG_CHARGER_BD99954) +=3D bd99954-charger.o > obj-$(CONFIG_CHARGER_WILCO) +=3D wilco-charger.o > obj-$(CONFIG_RN5T618_POWER) +=3D rn5t618_power.o > +obj-$(CONFIG_BATTERY_ACER_A500) +=3D acer_a500_battery.o > diff --git a/drivers/power/supply/acer_a500_battery.c b/drivers/power/sup= ply/acer_a500_battery.c > new file mode 100644 > index 000000000000..32a0bfcac08f > --- /dev/null > +++ b/drivers/power/supply/acer_a500_battery.c > @@ -0,0 +1,297 @@ > +// SPDX-License-Identifier: GPL-2.0+ > +/* > + * Battery driver for Acer Iconia Tab A500. > + * > + * Copyright 2020 GRATE-driver project. > + * > + * Based on downstream driver from Acer Inc. > + * Based on NVIDIA Gas Gauge driver for SBS Compliant Batteries. > + * > + * Copyright (c) 2010, NVIDIA Corporation. > + */ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +enum { > + REG_CAPACITY, > + REG_VOLTAGE, > + REG_CURRENT, > + REG_DESIGN_CAPACITY, > + REG_TEMPERATURE, > +}; > + > +#define EC_DATA(_reg, _psp) { \ > + .psp =3D POWER_SUPPLY_PROP_ ## _psp, \ > + .reg =3D _reg, \ > +} > + > +static const struct battery_register { > + enum power_supply_property psp; > + unsigned int reg; > +} ec_data[] =3D { > + [REG_CAPACITY] =3D EC_DATA(0x00, CAPACITY), > + [REG_VOLTAGE] =3D EC_DATA(0x01, VOLTAGE_NOW), > + [REG_CURRENT] =3D EC_DATA(0x03, CURRENT_NOW), > + [REG_DESIGN_CAPACITY] =3D EC_DATA(0x08, CHARGE_FULL_DESIGN), > + [REG_TEMPERATURE] =3D EC_DATA(0x0a, TEMP), > +}; > + > +static const enum power_supply_property a500_battery_properties[] =3D { > + POWER_SUPPLY_PROP_CAPACITY, > + POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, > + POWER_SUPPLY_PROP_CURRENT_NOW, > + POWER_SUPPLY_PROP_PRESENT, > + POWER_SUPPLY_PROP_STATUS, > + POWER_SUPPLY_PROP_TECHNOLOGY, > + POWER_SUPPLY_PROP_TEMP, > + POWER_SUPPLY_PROP_VOLTAGE_NOW, > +}; > + > +struct a500_battery { > + struct delayed_work poll_work; > + struct power_supply *psy; > + struct regmap *regmap; > + unsigned int capacity; > +}; > + > +static bool a500_battery_update_capacity(struct a500_battery *bat) > +{ > + unsigned int capacity; > + int err; > + > + err =3D regmap_read(bat->regmap, ec_data[REG_CAPACITY].reg, &capacity); > + if (err) > + return false; > + > + /* capacity can be >100% even if max value is 100% */ > + capacity =3D min(capacity, 100u); > + > + if (bat->capacity !=3D capacity) { > + bat->capacity =3D capacity; > + return true; > + } > + > + return false; > +} > + > +static int a500_battery_get_status(struct a500_battery *bat) > +{ > + if (bat->capacity < 100) { > + if (power_supply_am_i_supplied(bat->psy)) > + return POWER_SUPPLY_STATUS_CHARGING; > + else > + return POWER_SUPPLY_STATUS_DISCHARGING; > + } > + > + return POWER_SUPPLY_STATUS_FULL; > +} > + > +static void a500_battery_unit_adjustment(struct device *dev, > + enum power_supply_property psp, > + union power_supply_propval *val) > +{ > + const unsigned int base_unit_conversion =3D 1000; > + const unsigned int temp_kelvin_to_celsius =3D 2731; > + > + switch (psp) { > + case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: > + case POWER_SUPPLY_PROP_CURRENT_NOW: > + case POWER_SUPPLY_PROP_VOLTAGE_NOW: > + val->intval *=3D base_unit_conversion; > + break; > + > + case POWER_SUPPLY_PROP_TEMP: > + val->intval -=3D temp_kelvin_to_celsius; > + break; > + > + case POWER_SUPPLY_PROP_PRESENT: > + val->intval =3D !!val->intval; > + break; > + > + default: > + dev_dbg(dev, > + "%s: no need for unit conversion %d\n", __func__, psp); > + } > +} > + > +static int a500_battery_get_ec_data_index(struct device *dev, > + enum power_supply_property psp) > +{ > + unsigned int i; > + > + /* > + * DESIGN_CAPACITY register always returns a non-zero value if > + * battery is connected and zero if disconnected, hence we'll use > + * it for judging the battery presence. > + */ > + if (psp =3D=3D POWER_SUPPLY_PROP_PRESENT) > + psp =3D POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN; > + > + for (i =3D 0; i < ARRAY_SIZE(ec_data); i++) > + if (psp =3D=3D ec_data[i].psp) > + return i; > + > + dev_dbg(dev, "%s: invalid property %u\n", __func__, psp); > + > + return -EINVAL; > +} > + > +static int a500_battery_get_property(struct power_supply *psy, > + enum power_supply_property psp, > + union power_supply_propval *val) > +{ > + struct a500_battery *bat =3D power_supply_get_drvdata(psy); > + struct device *dev =3D psy->dev.parent; > + int ret =3D 0; > + > + switch (psp) { > + case POWER_SUPPLY_PROP_STATUS: > + val->intval =3D a500_battery_get_status(bat); > + break; > + > + case POWER_SUPPLY_PROP_TECHNOLOGY: > + val->intval =3D POWER_SUPPLY_TECHNOLOGY_LION; > + break; > + > + case POWER_SUPPLY_PROP_CAPACITY: > + a500_battery_update_capacity(bat); > + val->intval =3D bat->capacity; > + break; > + > + case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: > + case POWER_SUPPLY_PROP_CURRENT_NOW: > + case POWER_SUPPLY_PROP_VOLTAGE_NOW: > + case POWER_SUPPLY_PROP_PRESENT: > + case POWER_SUPPLY_PROP_TEMP: > + ret =3D a500_battery_get_ec_data_index(dev, psp); > + if (ret < 0) > + break; > + > + ret =3D regmap_read(bat->regmap, ec_data[ret].reg, &val->intval); > + break; > + > + default: > + dev_err(dev, "%s: invalid property %u\n", __func__, psp); > + return -EINVAL; > + } > + > + if (!ret) { > + /* convert units to match requirements of power supply class */ > + a500_battery_unit_adjustment(dev, psp, val); > + } > + > + dev_dbg(dev, "%s: property =3D %d, value =3D %x\n", > + __func__, psp, val->intval); > + > + /* return NODATA for properties if battery not presents */ > + if (ret) > + return -ENODATA; > + > + return 0; > +} > + > +static void a500_battery_poll_work(struct work_struct *work) > +{ > + struct a500_battery *bat; > + bool capacity_changed; > + > + bat =3D container_of(work, struct a500_battery, poll_work.work); > + capacity_changed =3D a500_battery_update_capacity(bat); > + > + if (capacity_changed) > + power_supply_changed(bat->psy); > + > + /* continuously send uevent notification */ > + schedule_delayed_work(&bat->poll_work, 30 * HZ); > +} > + > +static const struct power_supply_desc a500_battery_desc =3D { > + .name =3D "ec-battery", > + .type =3D POWER_SUPPLY_TYPE_BATTERY, > + .properties =3D a500_battery_properties, > + .get_property =3D a500_battery_get_property, > + .num_properties =3D ARRAY_SIZE(a500_battery_properties), > + .external_power_changed =3D power_supply_changed, > +}; > + > +static int a500_battery_probe(struct platform_device *pdev) > +{ > + struct power_supply_config psy_cfg =3D {}; > + struct a500_battery *bat; > + > + bat =3D devm_kzalloc(&pdev->dev, sizeof(*bat), GFP_KERNEL); > + if (!bat) > + return -ENOMEM; > + > + platform_set_drvdata(pdev, bat); > + > + psy_cfg.of_node =3D pdev->dev.parent->of_node; > + psy_cfg.drv_data =3D bat; > + > + bat->regmap =3D dev_get_regmap(pdev->dev.parent, "KB930"); > + if (!bat->regmap) > + return -EINVAL; > + > + bat->psy =3D devm_power_supply_register_no_ws(&pdev->dev, > + &a500_battery_desc, > + &psy_cfg); > + if (IS_ERR(bat->psy)) > + return dev_err_probe(&pdev->dev, PTR_ERR(bat->psy), > + "failed to register battery\n"); > + > + INIT_DELAYED_WORK(&bat->poll_work, a500_battery_poll_work); > + schedule_delayed_work(&bat->poll_work, HZ); > + > + return 0; > +} > + > +static int a500_battery_remove(struct platform_device *pdev) > +{ > + struct a500_battery *bat =3D dev_get_drvdata(&pdev->dev); > + > + cancel_delayed_work_sync(&bat->poll_work); > + > + return 0; > +} > + > +static int __maybe_unused a500_battery_suspend(struct device *dev) > +{ > + struct a500_battery *bat =3D dev_get_drvdata(dev); > + > + cancel_delayed_work_sync(&bat->poll_work); > + > + return 0; > +} > + > +static int __maybe_unused a500_battery_resume(struct device *dev) > +{ > + struct a500_battery *bat =3D dev_get_drvdata(dev); > + > + schedule_delayed_work(&bat->poll_work, HZ); > + > + return 0; > +} > + > +static SIMPLE_DEV_PM_OPS(a500_battery_pm_ops, > + a500_battery_suspend, a500_battery_resume); > + > +static struct platform_driver a500_battery_driver =3D { > + .driver =3D { > + .name =3D "acer-a500-iconia-battery", > + .pm =3D &a500_battery_pm_ops, > + }, > + .probe =3D a500_battery_probe, > + .remove =3D a500_battery_remove, > +}; > +module_platform_driver(a500_battery_driver); > + > +MODULE_DESCRIPTION("Battery gauge driver for Acer Iconia Tab A500"); > +MODULE_AUTHOR("Dmitry Osipenko "); > +MODULE_ALIAS("platform:acer-a500-iconia-battery"); > +MODULE_LICENSE("GPL"); > --=20 > 2.29.2 >=20 --fanhwzk2b46wa3oi Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEE72YNB0Y/i3JqeVQT2O7X88g7+poFAmAAkk4ACgkQ2O7X88g7 +ppdjw/6AkYv9leP/9NMuxsp3uUHlSnkhvhL0R1obHZVCcrzUvOKVSTOUNRKjfYB N/IHrt0W8Ehkqeeeh+c/hIiNuId6mGtdqnmYJUdSy/GKahHNvBWdwhizGY8NjwYq yrZDAmPiqZ/KpS4oYdeQE72/4JD28gX3Ws9e8U7MknIPuCRRGDrqi+ZxEM2qBkG2 cWOSs/THhTYEbYNmdPAqo1kq3Ii/6INCQkLXh+xxUSk3euTDWWG+ash4+OIutGvZ czbQ8Im2MYMRepOpN6zmjnb3jXZBveqiMi1VpKIycWMQ41sw61fEBA6O7C+77R2D CEVN16u6sS3aSBkwTenRsUXr58B+yL1Oth1e09iwLSJ3m78+E+UK6oE5mS6ztu5Y XNrEh1Ika/wL+eK9xBjNSVospj1vGxu5qYP7y57778t/VzkrawRxGIY3LNoonCK/ MuAywfXs5kfJYOYDBf5NrUWmqw4gE/xQSeqWpYM+ku3Ou61JeW/X3t2HBdMpf0ax pEZ397FPs49LyuW/D6HmZ+tPUq6BEtz6a7CpgdnUWA9LgVpTGGbqjefRZ+Yh66DY yufrJUZiM/KuyljRFJuf4SSF1i033yFWC9HBKuqk1aTE7U96rDh7JVdIVF95Taw3 8iZssJX31FVNcS3pKBn3ni6a4pk/yo2T1rYBBjpj9hafrzGgU2k= =JNT8 -----END PGP SIGNATURE----- --fanhwzk2b46wa3oi--