Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "David Heidelberg" <david@ixit.cz>
Cc: linux-input@vger.kernel.org, devicetree@vger.kernel.org,
	mfd@lists.linux.dev, robh@kernel.org, conor+dt@kernel.org,
	dmitry.torokhov@gmail.com, lee@kernel.org
Subject: Re: [PATCH v8 3/9] Input: qcom-pmi8998-haptics - Add Qualcomm PMI8998 haptics driver
Date: Mon, 30 Nov 2026 00:26:14 +0000	[thread overview]
Message-ID: <sashiko-outbox-240154@kernel.org> (raw)
In-Reply-To: <20261130-qcom-pmi8998-haptics-v8-3-a64ecd72d3bb@ixit.cz>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] Input: race between pmi8998_haptics_work() and
  pmi8998_haptics_erase()
- [Medium] Input: premature auto_res_work execution in
  pmi8998_haptics_auto_res_work()
- [Low] Input: direct error return in multi-failure qcom-pmi8998-haptics
  helpers
--

commit c3d9968584652dc5c9afb2487c36dd83fdc8de7f
Author: David Heidelberg <david@ixit.cz>

Input: qcom-pmi8998-haptics - Add Qualcomm PMI8998 haptics driver

This commit adds an input force-feedback driver for the haptics module in
the Qualcomm PMI8998 and PM660 PMICs, supporting LRA and ERM actuators in
direct and buffer playback modes.

> diff --git a/drivers/input/misc/qcom-pmi8998-haptics.c b/drivers/input/misc/qcom-pmi8998-haptics.c
> new file mode 100644
> index 0000000000000..480c56bc35f42
> --- /dev/null
> +++ b/drivers/input/misc/qcom-pmi8998-haptics.c
> @@ -0,0 +1,2025 @@

[ ... ]

> +static int pmi8998_haptics_write_play_mode(struct pmi8998_haptics *haptics,
> +					   u8 source)
> +{
> +	u8 val;
> +
> +	if (!is_haptics_idle(haptics))
> +		return -EBUSY;
> +
> +	val = FIELD_PREP(HAP_WF_SOURCE_MASK, source);
> +	return pmi8998_haptics_write_masked(haptics, HAP_SEL_REG,
> +					    HAP_WF_SOURCE_MASK, val);
> +}

[ ... ]

> +static void pmi8998_haptics_auto_res_work(struct work_struct *work)
> +{
> +	struct pmi8998_haptics *haptics = container_of(to_delayed_work(work),
> +						      struct pmi8998_haptics,
> +						      auto_res_work);
> +
> +	guard(mutex)(&haptics->play_lock);
> +
> +	if (!haptics->hw_enabled)
> +		return;
> +
> +	if (!haptics->auto_res_enabled) {
> +		if (pmi8998_haptics_set_auto_res(haptics, true) < 0)
> +			return;
> +		if (haptics->data->hw_auto_res)
> +			return;
> +	} else {
> +		pmi8998_haptics_update_rate(haptics);
> +		if (!haptics->auto_res_enabled)
> +			return;
> +	}
> +
> +	mod_delayed_work(system_percpu_wq, &haptics->auto_res_work,
> +			 msecs_to_jiffies(HAP_AUTO_RES_POLL_MS));
> +}

[ ... ]

> +static int pm660_haptics_init_chip(struct pmi8998_haptics *haptics)
> +{
> +	u8 val = HAP_EN_CTL3_DEFAULT;
> +	int err;
> +
> +	err = pmi8998_haptics_write(haptics, HAP_EN_CTL3_REG, &val, 1);
> +	if (err < 0)
> +		return err;
> +
> +	return pmi8998_haptics_write_masked(haptics, HAP_ZX_CFG_REG,
> +					    HAP_ZX_DET_DEB_MASK,
> +					    HAP_ZX_DET_DEB_80US);
> +}
> +
> +static int pm660_haptics_config_auto_res(struct pmi8998_haptics *haptics)
> +{
> +	u8 val, mask;
> +	int err;
> +
> +	if (haptics->auto_res_mode == QCOM_LRA_AUTO_RES_MODE_QWD) {
> +		val = FIELD_PREP(PM660_AUTO_RES_MODE_MASK, PM660_AUTO_RES_QWD) |
> +		      FIELD_PREP(PM660_CAL_DURATION_MASK, haptics->lra_high_z);
> +	} else {
> +		u8 cal_period = ilog2(haptics->res_cal_period /
> +				      HAP_RES_CAL_PERIOD_MIN) + 1;
> +
> +		val = FIELD_PREP(PM660_AUTO_RES_MODE_MASK, PM660_AUTO_RES_ZXD) |
> +		      FIELD_PREP(PM660_CAL_DURATION_MASK, haptics->lra_high_z) |
> +		      PM660_CAL_EOP_EN_BIT |
> +		      FIELD_PREP(PM660_LRA_RES_CAL_MASK, cal_period);
> +	}
> +
> +	mask = PM660_AUTO_RES_MODE_MASK | PM660_CAL_DURATION_MASK |
> +	       PM660_CAL_EOP_EN_BIT | PM660_LRA_RES_CAL_MASK;
> +
> +	err = pmi8998_haptics_write_masked(haptics, HAP_LRA_AUTO_RES_REG,
> +					   mask, val);
> +	if (err < 0)
> +		return err;
> +
> +	val = HAP_SEL_AUTO_RES_PERIOD_BIT |
> +	      FIELD_PREP(HAP_AUTO_RES_CNT_ERR_DELTA_MASK,
> +			 HAP_AUTO_RES_CNT_ERR_DELTA_DEF) |
> +	      HAP_AUTO_RES_ERR_RECOVERY_BIT |
> +	      FIELD_PREP(HAP_AUTO_RES_EN_DLY_MASK, HAP_AUTO_RES_EN_DLY_DEF);
> +
> +	return pmi8998_haptics_write(haptics, HAP_AUTO_RES_CTRL_REG, &val, 1);
> +}

[Severity: Low]
This isn't a bug, but input subsystem style prefers that functions with
multiple failure points (pmi8998_haptics_write_play_mode(),
pm660_haptics_init_chip(), and pm660_haptics_config_auto_res()) check the
final call's error and explicitly return 0 on the success path rather than
ending with "return action(...);".

[ ... ]

> +static int pmi8998_haptics_hw_start_locked(struct pmi8998_haptics *haptics)
> +{
> +	int err;
> +
> +	lockdep_assert_held(&haptics->play_lock);
> +
> +	cancel_delayed_work(&haptics->auto_res_work);

[ ... ]

> +	if (haptics->act_type == HAP_TYPE_LRA) {
> +		if (haptics->auto_res_mode == QCOM_LRA_AUTO_RES_MODE_QWD) {
> +			/*
> +			 * With the quarter-wave detection mode the high impedance is
> +			 * applied for every LRA cycle, so there is not enough back-EMF
> +			 * at the start of the playback. Defer enabling auto resonance
> +			 * until the actuator has vibrated for a few cycles.
> +			 */
> +			mod_delayed_work(system_percpu_wq, &haptics->auto_res_work,
> +					 msecs_to_jiffies(HAPTICS_BACK_EMF_DELAY_MS));
> +		} else {
> +			err = pmi8998_haptics_set_auto_res(haptics, true);
> +			if (err < 0) {
> +				dev_err(haptics->dev, "Error enabling auto_res, err=%d\n",
> +					err);
> +				goto out_disable;
> +			}
> +
> +			/*
> +			 * Follow the resonance of the actuator while it is driven, the
> +			 * measured drive period is read back from the hardware.
> +			 */
> +			if (!haptics->data->hw_auto_res)
> +				mod_delayed_work(system_percpu_wq,
> +						 &haptics->auto_res_work,
> +						 msecs_to_jiffies(HAP_AUTO_RES_POLL_MS));
> +		}
> +	}

[Severity: Medium]
Can an already-dispatched pmi8998_haptics_auto_res_work() run immediately
after pmi8998_haptics_hw_start_locked() releases haptics->play_lock,
skipping the back-EMF or calibration delay?

CPU0                                    CPU1 (kworker)
pmi8998_haptics_work()
  lock(&haptics->play_lock)
                                        pmi8998_haptics_auto_res_work()
                                          lock(&haptics->play_lock) [blocks]
  pmi8998_haptics_hw_start_locked()
    cancel_delayed_work(&auto_res_work)
    pmi8998_haptics_set_auto_res(false)
    ...
    mod_delayed_work(..., 20 ms)
  unlock(&haptics->play_lock)
                                          [acquires play_lock at t = 0 ms]
                                          if (!haptics->auto_res_enabled)
                                            pmi8998_haptics_set_auto_res(true)
                                              <-- enables before back-EMF delay

cancel_delayed_work() cannot cancel a worker already blocked on
haptics->play_lock, and unlike pmi8998_haptics_stop_work(),
pmi8998_haptics_auto_res_work() does not check a readiness timestamp after
acquiring the lock.

In QWD mode, the waiting worker enables auto-resonance at 0 ms without
waiting HAPTICS_BACK_EMF_DELAY_MS; on PM660 (where hw_auto_res is true), it
also leaves the 20 ms timer armed, which later sees auto_res_enabled == true
and starts software polling via pmi8998_haptics_update_rate().  In ZXD_EOP
mode, the waiting worker calls pmi8998_haptics_update_rate() at 0 ms before
haptics->auto_res_ready has elapsed, reading an uncalibrated period that
can disable auto-resonance.

Checking time_after_eq(jiffies, haptics->auto_res_ready) in
pmi8998_haptics_auto_res_work() (and setting auto_res_ready when arming the
QWD back-EMF delay) would prevent the stale worker from acting early.

[ ... ]

> +static void pmi8998_haptics_work(struct work_struct *work)
> +{
> +	struct pmi8998_haptics *haptics = container_of(work,
> +						       struct pmi8998_haptics,
> +						       work);
> +
> +	for (;;) {
> +		struct pmi8998_haptics_effect *eff = NULL;
> +		int effect_id;
> +		bool play;
> +		int err = 0;
> +
> +		scoped_guard(spinlock_irqsave, &haptics->req_lock) {
> +			if (!list_empty(&haptics->play_queue)) {
> +				eff = list_first_entry(&haptics->play_queue,
> +						       struct pmi8998_haptics_effect,
> +						       play_node);
> +				list_del_init(&eff->play_node);
> +				play = eff->play_req;
> +			}
> +		}
> +
> +		if (!eff)
> +			return;
> +
> +		effect_id = eff - haptics->effects;
> +		dev_dbg(haptics->dev, "%s: effect: %d play: %d\n",
> +			__func__, effect_id, play);
> +
> +		scoped_guard(mutex, &haptics->play_lock) {
> +			if (play)
> +				err = pmi8998_haptics_enable_locked(haptics,
> +								    effect_id);
> +			else if (haptics->active_effect_id == effect_id)
> +				err = pmi8998_haptics_disable_locked(haptics);
> +		}
> +
> +		if (err < 0)
> +			dev_err(haptics->dev, "Error setting haptics, err=%d\n",
> +				err);
> +	}
> +}

[ ... ]

> +static int pmi8998_haptics_erase(struct input_dev *dev, int effect_id)
> +{
> +	struct pmi8998_haptics *haptics = input_get_drvdata(dev);
> +	struct pmi8998_haptics_effect *eff;
> +
> +	if (effect_id < 0 || effect_id >= HAPTICS_MAX_EFFECTS)
> +		return -EINVAL;
> +
> +	guard(mutex)(&haptics->play_lock);
> +
> +	eff = &haptics->effects[effect_id];
> +
> +	scoped_guard(spinlock_irqsave, &haptics->req_lock)
> +		list_del_init(&eff->play_node);
> +
> +	if (haptics->active_effect_id == effect_id)
> +		pmi8998_haptics_disable_locked(haptics);
> +
> +	eff->mode = PMI8998_HAPTICS_MODE_NONE;

[Severity: Medium]
Can pmi8998_haptics_work() race with pmi8998_haptics_erase() (or a stop
request) by popping eff->play_node and snapshotting play = eff->play_req
before acquiring haptics->play_lock?

CPU0 (kworker)                          CPU1
pmi8998_haptics_work()
  lock(&haptics->req_lock)
  list_del_init(&eff->play_node)
  play = eff->play_req; /* true */
  unlock(&haptics->req_lock)
                                        erase_effect()
                                          pmi8998_haptics_playback(..., 0)
                                            eff->play_req = false
                                            list_add_tail(&eff->play_node, ...)
                                          pmi8998_haptics_erase()
                                            lock(&haptics->play_lock)
                                            list_del_init(&eff->play_node)
                                            eff->mode = PMI8998_HAPTICS_MODE_NONE
                                            unlock(&haptics->play_lock)
  lock(&haptics->play_lock)
  if (play)
    pmi8998_haptics_enable_locked()     <-- runs after erase

When pmi8998_haptics_work() finally acquires haptics->play_lock with its
stale play == true snapshot, pmi8998_haptics_enable_locked() either fails
with -EINVAL and logs a spurious dev_err(), or, if a new effect has already
been uploaded into the freed slot, starts vibrating the new effect after its
stop request was deleted by pmi8998_haptics_erase().  Popping eff->play_node
before acquiring haptics->play_lock also prevents start/stop requests from
coalescing in play_queue while play_lock is held across SPMI writes and
pmi8998_haptics_wait_idle().

Since pmi8998_haptics_erase() already nests req_lock inside play_lock,
acquiring play_lock before taking req_lock to dequeue from play_queue in
pmi8998_haptics_work() would avoid this race and keep request coalescing
intact.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130-qcom-pmi8998-haptics-v8-0-a64ecd72d3bb@ixit.cz?part=3

  reply	other threads:[~2026-11-30  0:26 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  0:13 [PATCH v8 0/9] input: Add support for the Qualcomm PMI8998 and PM660 haptics David Heidelberg via B4 Relay
2026-11-30  0:13 ` [PATCH v8 1/9] dt-bindings: input: Add Qualcomm PMI8998 / " David Heidelberg via B4 Relay
2026-11-30  0:22   ` sashiko-bot
2026-11-30  0:13 ` [PATCH v8 2/9] dt-bindings: mfd: qcom,spmi-pmic: Allow the PMI8998 haptics child node David Heidelberg via B4 Relay
2026-11-30  0:13 ` [PATCH v8 3/9] Input: qcom-pmi8998-haptics - Add Qualcomm PMI8998 haptics driver David Heidelberg via B4 Relay
2026-11-30  0:26   ` sashiko-bot [this message]
2026-11-30  0:13 ` [PATCH v8 4/9] arm64: dts: qcom: pmi8998: Add haptics node David Heidelberg via B4 Relay
2026-11-30  0:13 ` [PATCH v8 5/9] arm64: dts: qcom: sdm845-oneplus: Enable haptics David Heidelberg via B4 Relay
2026-11-30  0:13 ` [PATCH v8 6/9] arm64: dts: qcom: sdm845-xiaomi-beryllium: " David Heidelberg via B4 Relay
2026-11-30  0:13 ` [PATCH v8 7/9] arm64: dts: qcom: sdm845-shift-axolotl: " David Heidelberg via B4 Relay
2026-11-30  0:13 ` [PATCH v8 8/9] arm64: dts: qcom: pm660: Add haptics node David Heidelberg via B4 Relay
2026-11-30  0:13 ` [PATCH v8 9/9] arm64: dts: qcom: sdm660-xiaomi-lavender: Enable haptics David Heidelberg via B4 Relay

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-240154@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=david@ixit.cz \
    --cc=devicetree@vger.kernel.org \
    --cc=dmitry.torokhov@gmail.com \
    --cc=lee@kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=mfd@lists.linux.dev \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox