All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Taniya Das" <taniya.das@oss.qualcomm.com>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH 2/3] pwm: qcom: add Shikra PWM controller support
Date: Mon, 16 Nov 2026 07:29:14 +0000	[thread overview]
Message-ID: <sashiko-outbox-216428@kernel.org> (raw)
In-Reply-To: <20261116-pwm_qcom_shikra-v1-2-67ee4553adc5@oss.qualcomm.com>

Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [High] pwm: qcom: division by zero in qcom_pwm_config() when cycles >
  rate
- [High] pwm: qcom: broken duty-cycle math and -ERANGE check in
  qcom_pwm_config()
- [High] pwm: qcom: qcom_pwm_apply() runs qcom_pwm_config() when
  !state->enabled
- [Medium] pwm: qcom: unrounded current_period_ns check in
  qcom_pwm_config() fails
- [Medium] pwm: qcom: desynchronized software state in qcom_pwm_apply()
  and config
- [Medium] pwm: qcom: partial state mutation before validation in
  qcom_pwm_config()
- [Low] pwm: qcom: unnamed regmap_config collision in
  qcom_pwm_parse_dt()
--

commit 06e62578a3838a59f7adc59ac3054518eb4e4082
Author: Taniya Das <taniya.das@oss.qualcomm.com>

pwm: qcom: add Shikra PWM controller support

This commit adds a PWM controller driver for Qualcomm Shikra SoCs, modeling
up to ten independent PWM frames from DT child nodes with a shared
output-enable register and shared AHB and core clocks.

> diff --git a/drivers/pwm/pwm-qcom.c b/drivers/pwm/pwm-qcom.c
> new file mode 100644
> index 0000000000000..9b15cdcaa5c76
> --- /dev/null
> +++ b/drivers/pwm/pwm-qcom.c

[ ... ]

> +static int qcom_pwm_calc_frequency(struct qcom_pwm_chip *chip,
> +				   u64 period_ns, unsigned long rate, u32 hw_idx)
> +{
> +	struct regmap *frame_regmap = chip->frames[hw_idx].regmap;
> +	u64 cycles;
> +	int ret;
> +
> +	/*
> +	 * PWM client can set the period only once if the HW version does
> +	 * not support reset functionality.
> +	 */
> +	if (chip->frames[hw_idx].freq_set && !chip->priv_data->pwm_reset_support)
> +		return 0;
> +
> +	/*
> +	 * Number of core-clock cycles in one PWM period. Round down so the
> +	 * implemented period never exceeds the requested one, as the PWM API
> +	 * requires. PWM_CYC_CFG holds (cycles - 1) in its low 16 bits, so
> +	 * cycles is bounded to [2, 65536], i.e. an output frequency from
> +	 * rate / 2 down to rate / 65536.
> +	 */
> +	cycles = mul_u64_u64_div_u64(period_ns, rate, NSEC_PER_SEC);
> +	if (cycles < 2 || cycles > 65536) {
> +		dev_dbg(regmap_get_device(frame_regmap),
> +			"period %llu ns out of range for rate %lu Hz\n",
> +			period_ns, rate);
> +		return -ERANGE;
> +	}
> +
> +	ret = regmap_update_bits(frame_regmap, PWM_CYC_CFG,
> +				 PWM_FRAME_VAL_MASK, cycles - 1);
> +	if (ret)
> +		return ret;
> +
> +	chip->frames[hw_idx].current_freq = div64_ul(rate, cycles);
> +	chip->frames[hw_idx].freq_set = true;
> +	chip->frames[hw_idx].current_period_ns = period_ns;
> +
> +	return 0;
> +}

[ ... ]

> +static int qcom_pwm_config(struct qcom_pwm_chip *chip,
> +			   u32 hw_idx, u64 duty_ns, u64 period_ns, int polarity)
> +{

[ ... ]

> +	if (chip->frames[hw_idx].freq_set &&
> +	    chip->frames[hw_idx].current_period_ns != period_ns) {
> +		if (chip->priv_data->pwm_reset_support) {
> +			ret = regmap_update_bits(frame_regmap, PWM_RESET,
> +						 PWM_FRAME_RESET_BIT,
> +						 PWM_FRAME_RESET_BIT);
> +			if (ret)
> +				goto out;
> +		} else {
> +			ret = -EINVAL;
> +			goto out;
> +		}
> +	}

[Severity: Medium]
Does comparing against the unrounded current_period_ns cause re-applying the
state returned by qcom_pwm_get_state() to fail with -EINVAL on
qcom,shikra-pwm?

qcom_pwm_calc_frequency() rounds cycles down and stores the caller's
unrounded period_ns in current_period_ns, whereas qcom_pwm_get_state()
returns the hardware period rounded up from cycles.  Because shikra_pwm_data
leaves pwm_reset_support false, re-applying the state read back from
qcom_pwm_get_state() (for example in pwm_apply_debug() or a consumer
read-modify-write update) sees period_ns != current_period_ns and returns
-EINVAL even though both periods map to the same cycle count.

> +
> +	ret = qcom_pwm_calc_frequency(chip, current_period, rate, hw_idx);
> +	if (ret)
> +		goto out;
> +
> +	if (chip->frames[hw_idx].polarity != polarity) {
> +		ret = regmap_update_bits(frame_regmap, PWM_CTL0,
> +					 PWM_FRAME_POLARITY_BIT, polarity);
> +		if (ret)
> +			goto out;
> +
> +		chip->frames[hw_idx].polarity = polarity;
> +	}
> +
> +	ctl1 = DIV_ROUND_CLOSEST(rate, chip->frames[hw_idx].current_freq);

[Severity: High]
Can this divide by zero when cycles > rate?

When the core clock is divided below 65536 Hz (down to 37.5 kHz on Shikra)
and a valid period longer than one second is requested (rate < cycles <=
65536), div64_ul(rate, cycles) in qcom_pwm_calc_frequency() truncates
current_freq to 0, and DIV_ROUND_CLOSEST(rate, current_freq) divides by zero.
Even when cycles <= rate, round-tripping through an integer current_freq
loses cycle-count precision compared to using cycles directly.

> +	duty_percent = mul_u64_add_u64_div_u64(duty_ns, 100, current_period / 2, current_period);
> +	ctl1 = mul_u64_add_u64_div_u64(ctl1, duty_percent, 50, 100);
> +
> +	ret = regmap_read(frame_regmap, PWM_CYC_CFG, &cyc_cfg);
> +	if (ret)
> +		goto out;
> +
> +	if ((ctl1 > cyc_cfg || ctl1 == 0) && duty_ns != 0) {
> +		dev_err(regmap_get_device(frame_regmap),
> +			"duty cycle out of valid range\n");
> +		ret = -ERANGE;
> +		goto out;
> +	}

[Severity: High]
Does this calculation reject valid 100% and < 0.5% duty cycles with -ERANGE
and quantize PWM_CTL1 to integer percentages?

PWM_CYC_CFG stores cycles - 1, so when duty_ns == period_ns (or >= 99.5% of
period_ns), duty_percent rounds to 100 and ctl1 becomes cycles (cyc_cfg + 1),
tripping ctl1 > cyc_cfg.  When 0 < duty_ns < 0.5% of period_ns, duty_percent
rounds to 0 and ctl1 == 0 with duty_ns != 0 also returns -ERANGE instead of
programming the cycle count or rounding down to 0.  Quantizing through a
rounded 0..100 percentage also discards the 16-bit counter resolution of
PWM_CTL1 and rounds duty_cycle up, contrary to the PWM framework contract and
the comment in qcom_pwm_get_state().

[Severity: Medium]
If the ctl1 range check above fails, does qcom_pwm_config() leave hardware
registers and per-frame state partially updated?

qcom_pwm_calc_frequency() has already written PWM_CYC_CFG and set
freq_set = true and current_period_ns, and the polarity block has already
updated PWM_CTL0 and frame->polarity, while __pwm_apply() leaves pwm->state
unchanged on error.  On qcom,shikra-pwm (!pwm_reset_support), an initial
apply that fails the ctl1 check leaves freq_set true for the failed period,
causing subsequent apply calls with any other period to fail with -EINVAL.

[ ... ]

> +static int qcom_pwm_apply(struct pwm_chip *pwm_chip, struct pwm_device *pwm,
> +			  const struct pwm_state *state)
> +{
> +	struct qcom_pwm_chip *chip = to_qcom_pwm_chip(pwm_chip);
> +	struct pwm_state curr_state;
> +	u32 hw_idx = pwm->hwpwm;
> +	int ret;
> +
> +	pwm_get_state(pwm, &curr_state);
> +
> +	if (state->period &&
> +	    (state->period != curr_state.period ||
> +	     state->duty_cycle != curr_state.duty_cycle ||
> +	     state->polarity != curr_state.polarity)) {
> +		ret = qcom_pwm_config(chip, hw_idx, state->duty_cycle,
> +				      state->period, state->polarity);
> +		if (ret) {
> +			dev_err(pwmchip_parent(pwm_chip),
> +				"failed to update PWM configuration\n");
> +			return ret;
> +		}
> +	}

[Severity: High]
Should qcom_pwm_apply() skip qcom_pwm_config() when !state->enabled?

When disabling an active PWM, running qcom_pwm_config() first can fail (for
example with -EINVAL if state->period changed on Shikra, or -ERANGE) and
return before qcom_pwm_disable() is reached, leaving the output active and
its ahb and core clocks enabled.  When a channel is exported via sysfs while
disabled, qcom_pwm_get_state() initializes pwm->state.period from the reset
default PWM_CYC_CFG == 0 (1 clock cycle), so writing duty_cycle or polarity
before period calls qcom_pwm_config() with a 1-cycle period and fails with
-ERANGE, while writing period while disabled sets freq_set = true and locks
the period before the channel is ever enabled.

> +
> +	if (state->enabled != curr_state.enabled) {
> +		if (state->enabled)
> +			return qcom_pwm_enable(chip, pwm);
> +
> +		ret = qcom_pwm_disable(chip, pwm);
> +		if (ret)
> +			return ret;
> +	}
> +
> +	return 0;
> +}

[Severity: Medium]
Can comparing against curr_state (pwm->state) and frame->polarity skip
necessary hardware updates or disable clocks twice?

When a channel is first requested, pwm_device_request() populates pwm->state
from qcom_pwm_get_state() while frame->freq_set remains false; if a consumer
then enables the channel with those same period, duty_cycle, and polarity
values, qcom_pwm_apply() sees no change from curr_state, skips
qcom_pwm_config(), and enables an unconfigured frame.  Similarly,
frame->polarity is initialized to 0 rather than read from PWM_CTL0, so
qcom_pwm_config() will not clear PWM_FRAME_POLARITY_BIT if it was set prior
to probe and PWM_POLARITY_NORMAL is requested.

[ ... ]

> +static const struct regmap_config pwm_regmap_config = {
> +	.reg_bits   = 32,
> +	.reg_stride = 4,
> +	.val_bits   = 32,
> +	.fast_io    = true,
> +};
> +
> +static int qcom_pwm_parse_dt(struct platform_device *pdev,
> +			     struct pwm_chip *pwm_chip,
> +			     struct qcom_pwm_chip *chip)
> +{
> +	struct device *dev = pwmchip_parent(pwm_chip);
> +	struct device_node *np = pdev->dev.of_node;
> +	struct device_node *frame_node;
> +	void __iomem *base;
> +	unsigned long frame_ids = 0;
> +	int ret;
> +
> +	base = devm_platform_ioremap_resource(pdev, 0);
> +	if (IS_ERR(base))
> +		return PTR_ERR(base);
> +
> +	chip->regmap = devm_regmap_init_mmio(dev, base, &pwm_regmap_config);

[ ... ]

> +		chip->frames[n].regmap =
> +			devm_regmap_init_mmio(dev, base, &pwm_regmap_config);

[Severity: Low]
Does registering chip->regmap and every chip->frames[n].regmap on the same
device with an unnamed pwm_regmap_config collide in regmap debugfs?

Because pwm_regmap_config.name is NULL, regmap_debugfs_init() uses
dev_name(dev) for all of them under /sys/kernel/debug/regmap/, causing every
per-frame regmap to fail debugfs directory creation with -EEXIST during
probe.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261116-pwm_qcom_shikra-v1-0-67ee4553adc5@oss.qualcomm.com?part=2

  reply	other threads:[~2026-11-16  7:29 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16  6:59 [PATCH 0/3] pwm: qcom: add Shikra PWM controller Taniya Das
2026-11-16  6:59 ` [PATCH 1/3] dt-bindings: " Taniya Das
2026-11-16  7:22   ` sashiko-bot
2026-11-16 10:05   ` Krzysztof Kozlowski
2026-11-16  6:59 ` [PATCH 2/3] pwm: qcom: add Shikra PWM controller support Taniya Das
2026-11-16  7:29   ` sashiko-bot [this message]
2026-11-17 14:44   ` Uwe Kleine-König
2026-11-16  6:59 ` [PATCH 3/3] arm64: dts: qcom: add Shikra PWM controller Taniya Das
2026-11-16  7:32   ` sashiko-bot

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-216428@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=taniya.das@oss.qualcomm.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 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.