mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Janne Grunau <j@jannau.net>
To: Nick Chan <towinchenmi@gmail.com>
Cc: michael.reeves077@gmail.com, Sebastian Reichel <sre@kernel.org>,
	Sven Peter <sven@kernel.org>, Neal Gompa <neal@gompa.dev>,
	Lee Jones <lee@kernel.org>,
	linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org,
	asahi@lists.linux.dev, linux-arm-kernel@lists.infradead.org,
	Hector Martin <marcan@marcan.st>
Subject: Re: [PATCH v3 1/2] power: supply: Add macsmc-power driver for Apple Silicon
Date: Sun, 18 Jan 2026 13:49:41 +0100	[thread overview]
Message-ID: <20260118124941.GB55832@robin.jannau.net> (raw)
In-Reply-To: <42645fb0-3ae1-4ded-91db-f1435fa79a72@gmail.com>

On Sun, Jan 18, 2026 at 08:19:38PM +0800, Nick Chan wrote:
> 
> 
> On 17/1/2026 20:26, Janne Grunau wrote:
> > On Thu, Jan 15, 2026 at 06:08:15PM +1100, Michael Reeves via B4 Relay wrote:
> >> From: Michael Reeves <michael.reeves077@gmail.com>
> > 
> > I think the driver is overall similar to the downstream AsahiLinux
> > driver so please keep Hector as author.
> > 
> >> This driver provides battery and AC status monitoring for Apple Silicon
> >> Macs via the SMC (System Management Controller). It supports
> >> reporting capacity, voltage, current, and charging status.
> >>
> >> Co-developed-by: Hector Martin <marcan@marcan.st>
> >> Signed-off-by: Hector Martin <marcan@marcan.st>
> > 
> > The downstream driver a quite a few more Co-developed-by:/Sobs. When I
> > squashed the commits I decided to err on the safe side and included
> > commit authors from incremental patches as Co-developed-by: Why did you
> > drop those?
> > 
> >> Reviewed-by: Neal Gompa <neal@gompa.dev>
> >> Signed-off-by: Michael Reeves <michael.reeves077@gmail.com>
> >> ---
> >>  MAINTAINERS                         |   1 +
> >>  drivers/power/supply/Kconfig        |  11 +
> >>  drivers/power/supply/Makefile       |   1 +
> >>  drivers/power/supply/macsmc-power.c | 834 ++++++++++++++++++++++++++++++++++++
> >>  4 files changed, 847 insertions(+)
> >>
> >> diff --git a/drivers/power/supply/macsmc-power.c b/drivers/power/supply/macsmc-power.c
> >> new file mode 100644
> >> index 000000000000..9b3faefe7a45
> >> --- /dev/null
> >> +++ b/drivers/power/supply/macsmc-power.c
> >> @@ -0,0 +1,834 @@

...

> >> +static int macsmc_power_probe(struct platform_device *pdev)
> >> +{
> >> +	struct device *dev = &pdev->dev;
> >> +	struct apple_smc *smc = dev_get_drvdata(pdev->dev.parent);
> >> +	struct power_supply_config psy_cfg = {};
> >> +	struct macsmc_power *power;
> >> +	bool has_battery = false;
> >> +	bool has_ac_adapter = false;
> >> +	int ret = -ENODEV;
> >> +	bool flag;
> >> +	u16 vu16;
> >> +	u32 val32;
> >> +	enum power_supply_property *props;
> >> +	size_t nprops;
> >> +
> >> +	if (!smc)
> >> +		return -ENODEV;
> >> +
> >> +	power = devm_kzalloc(dev, sizeof(*power), GFP_KERNEL);
> >> +	if (!power)
> >> +		return -ENOMEM;
> >> +
> >> +	power->dev = dev;
> >> +	power->smc = smc;
> >> +	dev_set_drvdata(dev, power);
> >> +
> >> +	INIT_WORK(&power->critical_work, macsmc_power_critical_work);
> >> +	ret = devm_work_autocancel(dev, &power->critical_work, macsmc_power_critical_work);
> >> +	if (ret)
> >> +		return ret;
> >> +
> >> +	/*
> >> +	 * Check for battery presence.
> >> +	 * B0AV is a fundamental key.
> >> +	 */
> >> +	if (apple_smc_read_u16(power->smc, SMC_KEY(B0AV), &vu16) == 0 &&
> >> +	    macsmc_battery_get_status(power) > POWER_SUPPLY_STATUS_UNKNOWN)
> >> +		has_battery = true;
> >> +
> >> +	/*
> >> +	 * Check for AC adapter presence.
> >> +	 * CHIS is a fundamental key.
> >> +	 */
> >> +	if (apple_smc_key_exists(smc, SMC_KEY(CHIS)))
> >> +		has_ac_adapter = true;
> >> +
> > 
> > I think a short circuit check for !(has_battery || has_ac_adapter) would
> > make sense here. The setup code for props is quite long. It should
> > return -ENODEV. ret is not -ENODEV. anymore since it was overwritten
> > with the return value of devm_work_autocancel()
> > 
> >> +	if (has_battery) {
> >> +		power->batt_desc = macsmc_battery_desc_template;
> >> +		props = devm_kcalloc(dev, MACSMC_MAX_BATT_PROPS,
> >> +				     sizeof(enum power_supply_property),
> >> +				     GFP_KERNEL);
> > 
> > I don't like the dynamic allocation for the props. I think we can
> > currently get way with static property arrays and so we should do that.
> > See my comments below
> 
> Dynamic allocation is needed to properly add support pre-M1 support,
> as the SMCs in them misses a few keys, yet in iOS 16 the charge limit
> (CHWA) keys are added. In a similar way, there really is no sane way
> to add any new properties for M1/M2/M3 with static allocation. So I
> would prefer if dynamic allocation is kept.

ack, I wasn't really thinking beyond the code in front of me. Let's go
with dynamic allocations. A check that nprops doesn't exceed
MACSMC_MAX_BATT_PROPS should be added then.

Janne

  reply	other threads:[~2026-01-18 12:49 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-15  7:08 [PATCH v3 0/2] Add Apple Silicon SMC power driver Michael Reeves via B4 Relay
2026-01-15  7:08 ` [PATCH v3 1/2] power: supply: Add macsmc-power driver for Apple Silicon Michael Reeves via B4 Relay
2026-01-15 21:00   ` Sven Peter
2026-01-18 12:53     ` Michael Reeves
2026-01-17 12:26   ` Janne Grunau
2026-01-18 12:19     ` Nick Chan
2026-01-18 12:49       ` Janne Grunau [this message]
2026-01-18 12:59         ` Michael Reeves
2026-01-15  7:08 ` [PATCH v3 2/2] mfd: macsmc: Wire up Apple SMC power driver Michael Reeves via B4 Relay
2026-01-17 12:32   ` Janne Grunau
2026-01-18 13:01     ` Michael Reeves

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=20260118124941.GB55832@robin.jannau.net \
    --to=j@jannau.net \
    --cc=asahi@lists.linux.dev \
    --cc=lee@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=marcan@marcan.st \
    --cc=michael.reeves077@gmail.com \
    --cc=neal@gompa.dev \
    --cc=sre@kernel.org \
    --cc=sven@kernel.org \
    --cc=towinchenmi@gmail.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

all inboxes | Powered by JetHome®