From: sashiko-bot@kernel.org
To: "Patrick Fischer" <admin@tsv-stahl-riesa.de>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH 3/3] power: supply: smb347: Set the USB input current limit from APSD
Date: Sat, 21 Nov 2026 09:51:22 +0000 [thread overview]
Message-ID: <sashiko-outbox-226870@kernel.org> (raw)
In-Reply-To: <20261121-upstream-smb347-apsd-v1-3-ca46b92bfd44@tsv-stahl-riesa.de>
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] power: supply: smb347: zero probe delay in
smb347_apsd_schedule() when offline
- [Medium] power: supply: smb347: smb347_set_current_limits() sets USB
limit before APSD
- [Medium] power: supply: smb347: unsynchronized CMD_A_ALLOW_WRITE in
smb347_apsd_work()
- [Medium] power: supply: smb347: missing apsd_work cancel on
remove/shutdown and IRQ free
--
commit 1d463a50b5c9dfcf14295d13dceba5f8a5903b43
Author: Patrick Fischer <admin@tsv-stahl-riesa.de>
power: supply: smb347: Set the USB input current limit from APSD
This commit adds Automatic Power Source Detection (APSD) support for the
SMB347 charger to configure the USB input current limit according to the
detected port type and expose POWER_SUPPLY_PROP_USB_TYPE.
> diff --git a/drivers/power/supply/smb347-charger.c b/drivers/power/supply/smb347-charger.c
> index 03d58b378a25f..330c05cfddc33 100644
> --- a/drivers/power/supply/smb347-charger.c
> +++ b/drivers/power/supply/smb347-charger.c
[ ... ]
> @@ -497,6 +535,59 @@ static int smb347_set_current_limits(struct smb347_charger *smb)
> return 0;
> }
>
> +/*
> + * Sets the USB input current limit. Like the vendor driver, turn AICL off
> + * while the limit changes, so that the charger detects the input current
> + * again with the new limit once AICL is turned back on.
> + *
> + * Must be called with the configuration registers writable.
> + */
> +static int smb347_set_usb_current_limit(struct smb347_charger *smb,
> + unsigned int limit)
> +{
> + unsigned int id = smb->id;
> + unsigned int val;
> + bool aicl;
> + int hw, ret, ret2;
> +
> + hw = current_to_hw(icl_tbl[id], ARRAY_SIZE(icl_tbl[id]), limit);
> + if (hw < 0)
> + return hw;
> +
> + ret = regmap_read(smb->regmap, CFG_CURRENT_LIMIT, &val);
> + if (ret < 0)
> + return ret;
> +
> + if ((val & CFG_CURRENT_LIMIT_USB_MASK) == hw)
> + return 0;
[Severity: Medium]
Does this skip toggling CFG_VARIOUS_FUNCTION_AICL at boot when
"summit,usb-current-limit-microamp" is set in the device tree?
During probe, smb347_hw_init() -> smb347_set_current_limits() writes
smb->usb_hc_current_limit directly into CFG_CURRENT_LIMIT without
cycling AICL:
drivers/power/supply/smb347-charger.c:smb347_set_current_limits() {
...
if (smb->usb_hc_current_limit) {
ret = current_to_hw(icl_tbl[id], ARRAY_SIZE(icl_tbl[id]),
smb->usb_hc_current_limit);
if (ret < 0)
return ret;
ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
CFG_CURRENT_LIMIT_USB_MASK, ret);
...
}
If the board boots with a DCP charger already connected,
smb347_apsd_work() later calls smb347_set_usb_current_limit() with the
same limit, sees that CFG_CURRENT_LIMIT_USB_MASK already equals hw, and
returns 0 without turning AICL off and back on. If the board instead
boots connected to an SDP host port, smb347_hw_init() enables charging
with the full smb->usb_hc_current_limit (e.g. 1800 mA) for the 500 ms
before smb347_apsd_work() runs.
> +
> + ret = regmap_read(smb->regmap, CFG_VARIOUS_FUNCTION, &val);
> + if (ret < 0)
> + return ret;
> +
> + aicl = val & CFG_VARIOUS_FUNCTION_AICL;
> + if (aicl) {
> + ret = regmap_clear_bits(smb->regmap, CFG_VARIOUS_FUNCTION,
> + CFG_VARIOUS_FUNCTION_AICL);
> + if (ret < 0)
> + return ret;
> + }
> +
> + ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
> + CFG_CURRENT_LIMIT_USB_MASK, hw);
> +
> + if (aicl) {
> + /* The vendor driver waits 20 ms before turning AICL back on. */
> + msleep(20);
> + ret2 = regmap_set_bits(smb->regmap, CFG_VARIOUS_FUNCTION,
> + CFG_VARIOUS_FUNCTION_AICL);
> + if (!ret)
> + ret = ret2;
> + }
> +
> + return ret;
> +}
[ ... ]
> +static void smb347_apsd_work(struct work_struct *work)
> +{
> + struct smb347_charger *smb = container_of(to_delayed_work(work),
> + struct smb347_charger,
> + apsd_work);
> + int usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN;
> + unsigned int limit, val;
> + int ret;
[ ... ]
> + limit = smb347_usb_type_current_limit(smb, usb_type);
> +
> + ret = smb347_set_writable(smb, true, true);
> + if (ret < 0)
> + goto out;
> +
> + ret = smb347_set_usb_current_limit(smb, limit);
> + smb347_set_writable(smb, false, true);
[Severity: Medium]
Can a concurrent smb347_set_writable() caller clear CMD_A_ALLOW_WRITE
while smb347_set_usb_current_limit() is sleeping in msleep(20)?
smb347_set_writable() disables the client IRQ, which nests across threads
and does not serialize process-context callers such as the USB VBUS
regulator callbacks or smb347_irq_disable():
kworker CPU1
smb347_apsd_work()
smb347_set_writable(smb, true, true)
/* CMD_A_ALLOW_WRITE = 1 */
smb347_set_usb_current_limit()
regmap_clear_bits(..., AICL)
regmap_update_bits(..., hw)
msleep(20)
smb347_usb_vbus_set_current_limit()
smb347_set_writable(smb, true, true)
...
smb347_set_writable(smb, false, true)
/* CMD_A_ALLOW_WRITE = 0 */
regmap_set_bits(..., AICL)
<-- rejected because CMD_A_ALLOW_WRITE is 0
When the second write to CFG_VARIOUS_FUNCTION is rejected, AICL remains
disabled.
> +out:
> + if (ret < 0)
> + dev_err(smb->dev, "failed to set USB input current limit: %d\n",
> + ret);
> +
> + WRITE_ONCE(smb->usb_type, usb_type);
> + power_supply_changed(smb->usb);
> +}
> +
> +static void smb347_apsd_schedule(struct smb347_charger *smb)
> +{
> + unsigned long delay = 0;
> +
> + if (!smb->use_apsd)
> + return;
> +
> + if (smb->usb_online)
> + delay = msecs_to_jiffies(SMB347_APSD_DELAY_MS);
> +
> + mod_delayed_work(system_dfl_wq, &smb->apsd_work, delay);
> +}
> +
> +static int smb347_apsd_init(struct smb347_charger *smb)
> +{
> + int ret;
> +
> + smb->use_apsd = apsd_tbl[smb->id] && smb->use_usb &&
> + !smb->irq_unsupported;
> + if (!smb->use_apsd)
> + return 0;
> +
> + ret = smb347_set_writable(smb, true, true);
> + if (ret < 0)
> + return ret;
> +
> + /* The vendor driver turns APSD on at probe as well. */
> + ret = regmap_set_bits(smb->regmap, CFG_CHARGE_CONTROL,
> + CFG_CHARGE_CONTROL_APSD);
> + smb347_set_writable(smb, false, true);
> + if (ret < 0)
> + return ret;
> +
> + /*
> + * Directly after probe the charger NAKs the write to
> + * CFG_VARIOUS_FUNCTION. Wait as after a new connection.
> + */
> + smb347_apsd_schedule(smb);
[Severity: Medium]
When the driver probes with no USB cable connected (smb->usb_online is
false), does smb347_apsd_schedule() queue smb->apsd_work with a 0 delay
instead of waiting SMB347_APSD_DELAY_MS?
smb347_apsd_work() then runs immediately at probe and calls
smb347_set_usb_current_limit() to lower the default limit to 500 mA,
hitting the post-probe NAK on CFG_VARIOUS_FUNCTION described in the
comment and leaving the higher NVM or DT current limit in place for the
next connection.
> +
> + return 0;
> +}
[ ... ]
> @@ -1582,10 +1818,22 @@ static int smb347_probe(struct i2c_client *client)
> if (ret < 0)
> return ret;
>
> + /* Before the IRQ, so that it is cancelled after the IRQ is freed. */
> + ret = devm_delayed_work_autocancel(dev, &smb->apsd_work,
> + smb347_apsd_work);
> + if (ret)
> + return ret;
> +
> ret = smb347_irq_init(smb, client);
> if (ret)
> return ret;
>
> + ret = smb347_apsd_init(smb);
> + if (ret < 0) {
> + smb347_irq_disable(smb);
> + return dev_err_probe(dev, ret, "failed to enable APSD\n");
> + }
> +
> usb_rdev_cfg.dev = dev;
> usb_rdev_cfg.driver_data = smb;
> usb_rdev_cfg.regmap = smb->regmap;
[Severity: Medium]
Can smb347_apsd_work() continue running during and after
smb347_remove(), smb347_shutdown(), or a devm_regulator_register() probe
failure?
smb347_remove() and smb347_shutdown() do not cancel smb->apsd_work, and
device_shutdown() does not run devres release callbacks, so an in-flight
work item (which polls STAT_D for up to 2 seconds) can keep issuing I2C
transfers after shutdown.
In addition, because devm_delayed_work_autocancel() is registered before
smb347_irq_init(), devres teardown frees client->irq before cancelling
smb->apsd_work, while smb347_apsd_work() -> smb347_set_writable() still
calls disable_irq(client->irq) and enable_irq(client->irq) on the freed
IRQ.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261121-upstream-smb347-apsd-v1-0-ca46b92bfd44@tsv-stahl-riesa.de?part=3
prev parent reply other threads:[~2026-11-21 9:51 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-21 9:30 [PATCH 0/3] power: supply: smb347: Set the USB input current limit from APSD Patrick Fischer via B4 Relay
2026-11-21 9:30 ` Patrick Fischer
2026-11-21 9:30 ` [PATCH 1/3] dt-bindings: power: supply: summit,smb347: Clarify the USB current limit Patrick Fischer via B4 Relay
2026-11-21 9:30 ` Patrick Fischer
2026-11-21 9:30 ` [PATCH 2/3] power: supply: smb347: Make the APSD and AICL registers readable Patrick Fischer via B4 Relay
2026-11-21 9:30 ` Patrick Fischer
2026-11-21 9:30 ` [PATCH 3/3] power: supply: smb347: Set the USB input current limit from APSD Patrick Fischer via B4 Relay
2026-11-21 9:30 ` Patrick Fischer
2026-11-21 9:51 ` sashiko-bot [this message]
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=sashiko-outbox-226870@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=admin@tsv-stahl-riesa.de \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=robh@kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.