public inbox for devicetree@vger.kernel.org
 help / color / mirror / Atom feed
From: Sebastian Reichel <sebastian.reichel@collabora.com>
To: Josua Mayer <josua.mayer@jm0.eu>
Cc: "Jonathan Neuschäfer" <j.neuschaefer@gmx.net>,
	"Lee Jones" <lee@kernel.org>, "Rob Herring" <robh@kernel.org>,
	"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
	"Conor Dooley" <conor+dt@kernel.org>,
	"Shawn Guo" <shawnguo@kernel.org>,
	"Sascha Hauer" <s.hauer@pengutronix.de>,
	"Pengutronix Kernel Team" <kernel@pengutronix.de>,
	"Fabio Estevam" <festevam@gmail.com>,
	linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org,
	devicetree@vger.kernel.org, imx@lists.linux.dev,
	linux-arm-kernel@lists.infradead.org
Subject: Re: [PATCH 3/4] power: supply: add battery driver for netronix ec
Date: Wed, 14 Jan 2026 01:55:47 +0100	[thread overview]
Message-ID: <aWblrimQTy5-khZF@venus> (raw)
In-Reply-To: <20260102-kobo-aura-battery-v1-3-501f2a8fa575@jm0.eu>

[-- Attachment #1: Type: text/plain, Size: 5853 bytes --]

Hi,

On Fri, Jan 02, 2026 at 07:00:32PM +0100, Josua Mayer wrote:
> Implement a simple battery driver for monitoring voltage with the
> netronix embedded controller found in certain ebook readers.
> 
> Signed-off-by: Josua Mayer <josua.mayer@jm0.eu>
> ---
>  drivers/power/supply/Kconfig         |   9 ++++
>  drivers/power/supply/Makefile        |   1 +
>  drivers/power/supply/ntxec-battery.c | 101 +++++++++++++++++++++++++++++++++++

That's indeed simple :)

>  3 files changed, 111 insertions(+)
> 
> diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig
> index 92f9f7aae92f..0f944c10e50b 100644
> --- a/drivers/power/supply/Kconfig
> +++ b/drivers/power/supply/Kconfig
> @@ -1132,4 +1132,13 @@ config FUEL_GAUGE_MM8013
>  	  the state of charge, temperature, cycle count, actual and design
>  	  capacity, etc.
>  
> +config BATTERY_NTXEC
> +	tristate "Battery driver for Netronix embedded controller"
> +	depends on MFD_NTXEC

You can add "|| COMPILE_TEST"

> +	help
> +	  Say yes here to enable netronix ec battery monitoring driver.
> +	  It enables the monitoring battery voltage on certain e-book readers
> +	  using an embedded controller by ODM Netronix. Battery design
> +	  characteristics are read from device-tree if available.
> +
>  endif # POWER_SUPPLY
> diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile
> index 4b79d5abc49a..db6fc815f9da 100644
> --- a/drivers/power/supply/Makefile
> +++ b/drivers/power/supply/Makefile
> @@ -128,3 +128,4 @@ obj-$(CONFIG_CHARGER_SURFACE)	+= surface_charger.o
>  obj-$(CONFIG_BATTERY_UG3105)	+= ug3105_battery.o
>  obj-$(CONFIG_CHARGER_QCOM_SMB2)	+= qcom_smbx.o
>  obj-$(CONFIG_FUEL_GAUGE_MM8013)	+= mm8013.o
> +obj-$(CONFIG_BATTERY_NTXEC)	+= ntxec-battery.o
> diff --git a/drivers/power/supply/ntxec-battery.c b/drivers/power/supply/ntxec-battery.c
> new file mode 100644
> index 000000000000..f49f0966d18d
> --- /dev/null
> +++ b/drivers/power/supply/ntxec-battery.c
> @@ -0,0 +1,101 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * The Netronix embedded controller is a microcontroller found in some
> + * e-book readers designed by the original design manufacturer Netronix, Inc.
> + * It contains RTC, battery monitoring, system power management, and PWM
> + * functionality.
> + *
> + * This driver implements battery monitoring.
> + *
> + * Copyright 2021 Josua Mayer <josua.mayer@jm0.eu>
> + */
> +
> +#include <linux/mfd/ntxec.h>

^ you probably don't need this with my comments down below :)

> +#include <linux/module.h>
> +#include <linux/platform_device.h>
> +#include <linux/power_supply.h>
> +#include <linux/property.h>
> +#include <linux/regmap.h>
> +
> +static const enum power_supply_property ntxec_battery_properties[] = {
> +	POWER_SUPPLY_PROP_VOLTAGE_NOW,
> +};
> +
> +struct ntxec_battery {
> +	struct ntxec *ec;
> +};

Store the regmap instead of the ec. You don't need anything else.

> +#define NTXEC_REG_READ_BATTERY	0x41
> +
> +static int ntxec_battery_get_property(struct power_supply *psy,
> +				     enum power_supply_property psp,
> +				     union power_supply_propval *val)
> +{
> +	struct ntxec_battery *priv = power_supply_get_drvdata(psy);
> +	int ret;
> +	unsigned int value;
> +
> +	switch (psp) {
> +		case POWER_SUPPLY_PROP_VOLTAGE_NOW:
> +			ret = regmap_read(priv->ec->regmap, NTXEC_REG_READ_BATTERY, &value);
> +			if (ret < 0)
> +				return ret;
> +
> +			/* ec value to microvolt conversion:
> +			 * vendor kernel source suggests linear behaviour from 3V to 4.2V
> +			 * with readings 767 to 1023; each increment represents 4687,5uV.

4687.5uV ?

> +			 * adjust 3V boundary slightly to report exactly 4.2V when full.
> +			 */
> +			val->intval = 2999872 + (value - 767) * 4688;
> +			break;
> +		default:
> +			dev_err(&psy->dev, "%s: invalid property %u\n", __func__, psp);
> +			return -EINVAL;
> +	}
> +
> +	return 0;
> +}
> +
> +static const struct power_supply_desc ntxec_battery_desc = {
> +	.name = "ec-battery",
> +	.type = POWER_SUPPLY_TYPE_BATTERY,
> +	.properties = ntxec_battery_properties,
> +	.get_property = ntxec_battery_get_property,
> +	.num_properties = ARRAY_SIZE(ntxec_battery_properties),
> +};
> +
> +static int ntxec_battery_probe(struct platform_device *pdev)
> +{
> +	struct device *dev = &pdev->dev;
> +	struct ntxec *ec = dev_get_drvdata(dev->parent);

Based on the regmap comment above, I think you can just do the
following and completley avoid 'struct ntxec' (please test):

struct regmap *regmap = dev_get_regmap(dev->parent, NULL);

> +	struct power_supply_config psy_cfg = {};
> +	struct ntxec_battery *priv;
> +	struct power_supply *psy;
> +
> +	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
> +	if (!priv)
> +		return -ENOMEM;
> +
> +	priv->ec = ec;
> +	psy_cfg.drv_data = priv;
> +	psy_cfg.fwnode = dev_fwnode(dev->parent);
> +	psy_cfg.no_wakeup_source = true;
> +	psy = devm_power_supply_register(dev, &ntxec_battery_desc, &psy_cfg);
> +	if (IS_ERR(psy))
> +		return PTR_ERR(psy);
> +
> +	return 0;
> +}
> +
> +static struct platform_driver ntxec_battery_driver = {
> +	.driver = {
> +		.name = "ntxec-battery",
> +	},
> +	.probe = ntxec_battery_probe,
> +};
> +module_platform_driver(ntxec_battery_driver);
> +
> +MODULE_AUTHOR("Josua Mayer <josua.mayer@jm0.eu>");
> +MODULE_DESCRIPTION("Battery driver for Netronix EC");
> +MODULE_LICENSE("GPL");
> +MODULE_ALIAS("platform:ntxec-battery");

I know there are a bunch of wrong examples in my subsystem, but the
proper way to do this is to drop this alias and instead add a
platform_device_id table together with MODULE_DEVICE_TABLE(platform, ...).
See for example cros_peripheral_charger.c

Greetings,

-- Sebastian

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2026-01-14  0:56 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-02 18:00 [PATCH 0/4] power: supply: add battery driver for netronix ec Josua Mayer
2026-01-02 18:00 ` [PATCH 1/4] dt-bindings: mfd: netronix,ntxec: add reference to power-supply Josua Mayer
2026-01-05  8:35   ` Krzysztof Kozlowski
2026-01-02 18:00 ` [PATCH 2/4] ARM: dts: imx: imx50-kobo-aura: add description for battery Josua Mayer
2026-01-02 18:00 ` [PATCH 3/4] power: supply: add battery driver for netronix ec Josua Mayer
2026-01-14  0:55   ` Sebastian Reichel [this message]
2026-01-02 18:00 ` [PATCH 4/4] mfd: ntxec: register battery subdevice Josua Mayer
2026-01-02 18:47 ` [PATCH 0/4] power: supply: add battery driver for netronix ec Ing. Josua Mayer

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=aWblrimQTy5-khZF@venus \
    --to=sebastian.reichel@collabora.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=festevam@gmail.com \
    --cc=imx@lists.linux.dev \
    --cc=j.neuschaefer@gmx.net \
    --cc=josua.mayer@jm0.eu \
    --cc=kernel@pengutronix.de \
    --cc=krzk+dt@kernel.org \
    --cc=lee@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=s.hauer@pengutronix.de \
    --cc=shawnguo@kernel.org \
    /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