Devicetree
 help / color / mirror / Atom feed
* [PATCH v8 0/9] input: Add support for the Qualcomm PMI8998 and PM660 haptics
@ 2026-11-30  0:13 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
                   ` (8 more replies)
  0 siblings, 9 replies; 12+ messages in thread
From: David Heidelberg via B4 Relay @ 2026-11-30  0:13 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Lee Jones, Stephen Boyd, Bjorn Andersson, Konrad Dybcio,
	Abel Vesa, David Heidelberg
  Cc: David Collins, Subbaraman Narayanamurthy, Kamal Wadhwa,
	Fenglin Wu, Casey Connolly, linux-arm-msm, linux-input,
	devicetree, linux-kernel, mfd, phone-devel

The series adds a driver for the haptics module found in the Qualcomm
PMI8998 and PM660 PMICs, used together with SDM845 and SDM660 in phones
such as the OnePlus 6 and 6T, Xiaomi Poco F1, SHIFT6mq and Xiaomi Redmi
Note 7, and enables it on those boards.

The driver supports both linear resonant actuators (LRA) and eccentric
rotating mass (ERM) motors via the input force-feedback framework,
allowing userspace to upload and play FF_RUMBLE, FF_CONSTANT and
FF_PERIODIC (FF_SINE and FF_CUSTOM) effects and modulate drive strength
via FF_GAIN. Rumble, constant and sine effects use the board's
configured play mode (direct or buffer), with the drive voltage scaling
the amplitude of the waveform. A custom effect carries up to eight
samples, matching the module's 8-sample waveform buffer, and is played
in buffer mode.

For LRA actuators, the haptics module measures the resonance while it is
driven. The measured drive period is read back from the hardware and
used to track resonance drift caused by temperature and actuator aging.

The actuator type, play mode, waveform shape, brake pattern, waveform
and sample repeat counts, sample play rate and auto-resonance detection
options are configured from the device tree, matching the vendor driver.

This is not the hardware handled by the qcom-spmi-haptics driver for
PMIH0108 [1]. That one drives the HAP530_HV module of PMIH0108, which has
separate CFG and PTN register blocks, an 8 KB PCM FIFO and a 50 mV VMAX
step. PMI8998 and PM660 have a single register block at 0xc000, an
8-sample waveform buffer, a 116 mV VMAX step and a different
auto-resonance and fault interface; several register offsets even collide
with different meanings. Fenglin Wu confirmed that the new driver is not
meant to cover it [2], so the compatible strings qcom,pmi8998-haptics and
qcom,pm660-haptics are used on their own, without a qcom,spmi-haptics
fallback, and the qcom,spmi-pmic schema is taught to accept either
haptics binding.

This continues Casey Connolly's v7 series from 2022.

[1] https://lore.kernel.org/linux-input/20260828-qcom-spmi-haptics-v7-0-0367b3ac2e1e@oss.qualcomm.com/
[2] https://lore.kernel.org/linux-input/7dc6b68a-db8d-490a-89aa-596d70d2638c@oss.qualcomm.com/

---
Changes in v8:
- Complete rework, no longer memory-less, supports multiple modes, ERM support, etc.
- Link to v7: https://lore.kernel.org/all/20221015172915.1436236-1-caleb@connolly.tech/

---
David Heidelberg (9):
      dt-bindings: input: Add Qualcomm PMI8998 / PM660 haptics
      dt-bindings: mfd: qcom,spmi-pmic: Allow the PMI8998 haptics child node
      Input: qcom-pmi8998-haptics - Add Qualcomm PMI8998 haptics driver
      arm64: dts: qcom: pmi8998: Add haptics node
      arm64: dts: qcom: sdm845-oneplus: Enable haptics
      arm64: dts: qcom: sdm845-xiaomi-beryllium: Enable haptics
      arm64: dts: qcom: sdm845-shift-axolotl: Enable haptics
      arm64: dts: qcom: pm660: Add haptics node
      arm64: dts: qcom: sdm660-xiaomi-lavender: Enable haptics

 .../bindings/input/qcom,pmi8998-haptics.yaml       |  256 +++
 .../devicetree/bindings/mfd/qcom,spmi-pmic.yaml    |    4 +-
 MAINTAINERS                                        |    9 +
 arch/arm64/boot/dts/qcom/pm660.dtsi                |   11 +
 arch/arm64/boot/dts/qcom/pmi8998.dtsi              |   11 +
 .../arm64/boot/dts/qcom/sdm660-xiaomi-lavender.dts |   11 +
 .../arm64/boot/dts/qcom/sdm845-oneplus-common.dtsi |   16 +
 arch/arm64/boot/dts/qcom/sdm845-shift-axolotl.dts  |   13 +
 .../dts/qcom/sdm845-xiaomi-beryllium-common.dtsi   |   13 +
 drivers/input/misc/Kconfig                         |   13 +
 drivers/input/misc/Makefile                        |    1 +
 drivers/input/misc/qcom-pmi8998-haptics.c          | 2025 ++++++++++++++++++++
 include/dt-bindings/input/qcom,haptics.h           |   46 +
 13 files changed, 2428 insertions(+), 1 deletion(-)
---
base-commit: 909ef7a9a30fa139e69e555e3606680f5cca7726
change-id: 20260909-qcom-pmi8998-haptics-c46c81e00fe0

Best regards,
--  
David Heidelberg <david@ixit.cz>



^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v8 1/9] dt-bindings: input: Add Qualcomm PMI8998 / PM660 haptics
  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 ` 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
                   ` (7 subsequent siblings)
  8 siblings, 1 reply; 12+ messages in thread
From: David Heidelberg via B4 Relay @ 2026-11-30  0:13 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Lee Jones, Stephen Boyd, Bjorn Andersson, Konrad Dybcio,
	Abel Vesa, David Heidelberg
  Cc: David Collins, Subbaraman Narayanamurthy, Kamal Wadhwa,
	Fenglin Wu, Casey Connolly, linux-arm-msm, linux-input,
	devicetree, linux-kernel, mfd, phone-devel

From: David Heidelberg <david@ixit.cz>

Document the haptics module found in the Qualcomm PMI8998 PMIC. It drives
an LRA or ERM actuator from an 8-sample waveform buffer, a fixed voltage,
an audio stream or a PWM input, and reports short circuits and buffer
completion through two interrupts.

The module predates the HAP530 design covered by qcom,spmi-haptics.yaml
and has a different register layout, so it gets its own schema and no
qcom,spmi-haptics fallback compatible. The resonance period and maximum
drive voltage properties reuse the qcom,lra-period-us and
qcom,vmax-microvolt names from that binding.

Add the shared constants header include/dt-bindings/input/qcom,haptics.h
for the actuator type, auto-resonance detection mode, play mode, waveform
shape and high-impedance options, along with properties for ERM sample
duration, repeat counts, brake pattern and accepted auto-resonance drive
period variation. LRA-only properties are required for LRA actuators (the
default) and disallowed for ERM actuators.

Based on work of Casey Connolly.

Signed-off-by: David Heidelberg <david@ixit.cz>
---
 .../bindings/input/qcom,pmi8998-haptics.yaml       | 256 +++++++++++++++++++++
 MAINTAINERS                                        |   8 +
 include/dt-bindings/input/qcom,haptics.h           |  46 ++++
 3 files changed, 310 insertions(+)

diff --git a/Documentation/devicetree/bindings/input/qcom,pmi8998-haptics.yaml b/Documentation/devicetree/bindings/input/qcom,pmi8998-haptics.yaml
new file mode 100644
index 0000000000000..2a296b6096e02
--- /dev/null
+++ b/Documentation/devicetree/bindings/input/qcom,pmi8998-haptics.yaml
@@ -0,0 +1,256 @@
+# SPDX-License-Identifier: GPL-2.0-only
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/input/qcom,pmi8998-haptics.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Qualcomm PMI8998 / PM660 PMIC haptics
+
+maintainers:
+  - David Heidelberg <david@ixit.cz>
+
+description:
+  The haptics module inside the Qualcomm PMI8998 and PM660 PMICs drives a
+  linear resonant actuator (LRA) or an eccentric rotating mass (ERM) motor
+  from an internal 8-sample waveform buffer, a fixed voltage, an audio stream
+  or a PWM input.
+
+properties:
+  compatible:
+    enum:
+      - qcom,pm660-haptics
+      - qcom,pmi8998-haptics
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    items:
+      - description: Short circuit detected on the actuator output
+      - description: Waveform buffer played, next set of samples can be loaded
+
+  interrupt-names:
+    items:
+      - const: short
+      - const: play
+
+  qcom,actuator-type:
+    description:
+      Type of actuator connected to the haptics output.
+      See include/dt-bindings/input/qcom,haptics.h for the available types.
+    $ref: /schemas/types.yaml#/definitions/uint32
+    enum:
+      - 0  # QCOM_HAPTICS_ACTUATOR_LRA
+      - 1  # QCOM_HAPTICS_ACTUATOR_ERM
+    default: 0
+
+  qcom,lra-period-us:
+    description:
+      Resonance period of the LRA in microseconds (1000000 / resonant
+      frequency in Hz). Each waveform sample is played for this long.
+    minimum: 5
+    maximum: 20475
+    multipleOf: 5
+
+  qcom,play-rate-us:
+    description:
+      Play duration of each sample in the 8-sample waveform buffer
+      for an ERM actuator.
+    default: 5715
+    minimum: 5
+    maximum: 20475
+    multipleOf: 5
+
+  qcom,vmax-microvolt:
+    description:
+      Maximum drive voltage of the actuator.
+    minimum: 116000
+    maximum: 3596000
+    multipleOf: 116000
+
+  qcom,lra-auto-res-mode:
+    description:
+      Detection mode of the auto resonance of the LRA.
+      See include/dt-bindings/input/qcom,haptics.h for the available modes.
+    $ref: /schemas/types.yaml#/definitions/uint32
+    enum:
+      - 0  # QCOM_LRA_AUTO_RES_MODE_QWD
+      - 1  # QCOM_LRA_AUTO_RES_MODE_ZXD_EOP
+
+  qcom,lra-res-cal-period:
+    description:
+      Number of LRA cycles used to calibrate the resonance frequency during
+      the auto resonance detection.
+    $ref: /schemas/types.yaml#/definitions/uint32
+    enum: [4, 8, 16, 32, 64, 128, 256]
+
+  qcom,play-mode:
+    description:
+      Play mode of the haptics module. In the direct mode the actuator is
+      driven at the resonance frequency (LRA) or with a constant voltage
+      (ERM), in the buffer mode the waveform buffer of the module is played.
+      See include/dt-bindings/input/qcom,haptics.h for the available modes.
+    $ref: /schemas/types.yaml#/definitions/uint32
+    enum: [0, 1]
+
+  qcom,wave-shape:
+    description:
+      Shape of the waveform driven into the LRA actuator.
+      See include/dt-bindings/input/qcom,haptics.h for the available shapes.
+    $ref: /schemas/types.yaml#/definitions/uint32
+    enum:
+      - 0  # QCOM_HAPTICS_WAVE_SHAPE_SINE
+      - 1  # QCOM_HAPTICS_WAVE_SHAPE_SQUARE
+
+  qcom,lra-high-z:
+    description:
+      High impedance option applied to the LRA while the auto resonance
+      detection runs. See include/dt-bindings/input/qcom,haptics.h for the
+      available options.
+    $ref: /schemas/types.yaml#/definitions/uint32
+    enum: [0, 1, 2, 3]
+
+  qcom,wave-rep-cnt:
+    description:
+      Number of times the waveform is repeated in buffer mode.
+    $ref: /schemas/types.yaml#/definitions/uint32
+    default: 1
+    enum: [1, 2, 4, 8, 16, 32, 64, 128]
+
+  qcom,wave-samp-rep-cnt:
+    description:
+      Number of times the waveform samples are repeated in buffer mode.
+    $ref: /schemas/types.yaml#/definitions/uint32
+    default: 1
+    enum: [1, 2, 4, 8]
+
+  qcom,brake-pattern:
+    $ref: /schemas/types.yaml#/definitions/uint32-array
+    description:
+      Brake pattern applied at the end of the playback. It consists of four
+      values, one for each cycle of the brake. 0 disables the brake for the
+      cycle, 1 drives a quarter of the maximum voltage, 2 a half and 3 the
+      maximum voltage. The brake is disabled if the property is absent.
+    minItems: 4
+    maxItems: 4
+    items:
+      minimum: 0
+      maximum: 3
+
+  qcom,drive-period-min-variation-percent:
+    description:
+      Minimum variation of the drive period measured by the auto resonance
+      detection. The auto resonance is disabled when the measured drive
+      period is outside of the accepted range.
+    default: 25
+    minimum: 1
+    maximum: 99
+
+  qcom,drive-period-max-variation-percent:
+    description:
+      Maximum variation of the drive period measured by the auto resonance
+      detection. The auto resonance is disabled when the measured drive
+      period is outside of the accepted range.
+    default: 25
+    minimum: 1
+    maximum: 99
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - interrupt-names
+  - qcom,vmax-microvolt
+  - qcom,play-mode
+
+allOf:
+  - if:
+      properties:
+        qcom,actuator-type:
+          const: 1
+      required:
+        - qcom,actuator-type
+    then:
+      properties:
+        qcom,lra-period-us: false
+        qcom,lra-auto-res-mode: false
+        qcom,lra-res-cal-period: false
+        qcom,wave-shape: false
+        qcom,lra-high-z: false
+        qcom,drive-period-min-variation-percent: false
+        qcom,drive-period-max-variation-percent: false
+    else:
+      properties:
+        qcom,play-rate-us: false
+      required:
+        - qcom,lra-period-us
+        - qcom,lra-auto-res-mode
+        - qcom,lra-res-cal-period
+        - qcom,wave-shape
+        - qcom,lra-high-z
+
+  - if:
+      properties:
+        compatible:
+          contains:
+            const: qcom,pmi8998-haptics
+    then:
+      properties:
+        qcom,lra-res-cal-period:
+          maximum: 32
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/input/qcom,haptics.h>
+    #include <dt-bindings/interrupt-controller/irq.h>
+    #include <dt-bindings/spmi/spmi.h>
+
+    pmic@3 {
+      compatible = "qcom,pmi8998", "qcom,spmi-pmic";
+      reg = <0x3 SPMI_USID>;
+      #address-cells = <1>;
+      #size-cells = <0>;
+
+      haptics@c000 {
+        compatible = "qcom,pmi8998-haptics";
+        reg = <0xc000>;
+        interrupts = <0x3 0xc0 0x0 IRQ_TYPE_EDGE_BOTH>,
+                     <0x3 0xc0 0x1 IRQ_TYPE_EDGE_BOTH>;
+        interrupt-names = "short", "play";
+
+        qcom,lra-auto-res-mode = <QCOM_LRA_AUTO_RES_MODE_ZXD_EOP>;
+        qcom,lra-high-z = <QCOM_HAPTICS_LRA_HIGH_Z_OPT1>;
+        qcom,lra-period-us = <4255>;
+        qcom,lra-res-cal-period = <32>;
+        qcom,play-mode = <QCOM_HAPTICS_PLAY_MODE_BUFFER>;
+        qcom,vmax-microvolt = <2088000>;
+        qcom,wave-shape = <QCOM_HAPTICS_WAVE_SHAPE_SINE>;
+      };
+    };
+
+  - |
+    #include <dt-bindings/input/qcom,haptics.h>
+    #include <dt-bindings/interrupt-controller/irq.h>
+    #include <dt-bindings/spmi/spmi.h>
+
+    pmic@1 {
+      compatible = "qcom,pm660";
+      reg = <0x1 SPMI_USID>;
+      #address-cells = <1>;
+      #size-cells = <0>;
+
+      haptics@c000 {
+        compatible = "qcom,pm660-haptics";
+        reg = <0xc000>;
+        interrupts = <0x1 0xc0 0x0 IRQ_TYPE_EDGE_BOTH>,
+                     <0x1 0xc0 0x1 IRQ_TYPE_EDGE_BOTH>;
+        interrupt-names = "short", "play";
+
+        qcom,actuator-type = <QCOM_HAPTICS_ACTUATOR_ERM>;
+        qcom,play-mode = <QCOM_HAPTICS_PLAY_MODE_DIRECT>;
+        qcom,play-rate-us = <6665>;
+        qcom,vmax-microvolt = <3132000>;
+      };
+    };
diff --git a/MAINTAINERS b/MAINTAINERS
index c00d3612c9acf..301689be7665d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -23383,16 +23383,24 @@ F:	include/linux/firmware/qcom/qcom_pas.h
 
 QUALCOMM SMB CHARGER DRIVER
 M:	Casey Connolly <casey.connolly@linaro.org>
 L:	linux-arm-msm@vger.kernel.org
 S:	Maintained
 F:	Documentation/devicetree/bindings/power/supply/qcom,pmi8998-charger.yaml
 F:	drivers/power/supply/qcom_smbx.c
 
+QUALCOMM PMI8998 HAPTICS DRIVER
+M:	David Heidelberg <david@ixit.cz>
+L:	linux-arm-msm@vger.kernel.org
+L:	linux-input@vger.kernel.org
+S:	Maintained
+F:	Documentation/devicetree/bindings/input/qcom,pmi8998-haptics.yaml
+F:	include/dt-bindings/input/qcom,haptics.h
+
 QUALCOMM PPE DRIVER
 M:	Luo Jie <jie.luo@oss.qualcomm.com>
 L:	netdev@vger.kernel.org
 S:	Maintained
 F:	Documentation/devicetree/bindings/net/qcom,ipq9574-ppe.yaml
 F:	Documentation/networking/device_drivers/ethernet/qualcomm/ppe/ppe.rst
 F:	drivers/net/ethernet/qualcomm/ppe/
 
diff --git a/include/dt-bindings/input/qcom,haptics.h b/include/dt-bindings/input/qcom,haptics.h
new file mode 100644
index 0000000000000..7ac0d426a16d0
--- /dev/null
+++ b/include/dt-bindings/input/qcom,haptics.h
@@ -0,0 +1,46 @@
+/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
+
+#ifndef _DT_BINDINGS_INPUT_QCOM_HAPTICS_H
+#define _DT_BINDINGS_INPUT_QCOM_HAPTICS_H
+
+/*
+ * Actuator types driven by the haptics module
+ */
+#define QCOM_HAPTICS_ACTUATOR_LRA		0
+#define QCOM_HAPTICS_ACTUATOR_ERM		1
+
+/*
+ * Auto resonance detection modes of the LRA
+ *
+ * QCOM_LRA_AUTO_RES_MODE_QWD detects the quarter wave of the actuator,
+ * QCOM_LRA_AUTO_RES_MODE_ZXD_EOP detects the zero crossing at the end of
+ * a period. The mode depends on the actuator.
+ */
+#define QCOM_LRA_AUTO_RES_MODE_QWD		0
+#define QCOM_LRA_AUTO_RES_MODE_ZXD_EOP		1
+
+/*
+ * Play modes of the haptics module
+ *
+ * QCOM_HAPTICS_PLAY_MODE_DIRECT drives the actuator at the resonance
+ * frequency with the drive voltage, QCOM_HAPTICS_PLAY_MODE_BUFFER plays
+ * the waveform buffer of the module.
+ */
+#define QCOM_HAPTICS_PLAY_MODE_DIRECT	0
+#define QCOM_HAPTICS_PLAY_MODE_BUFFER	1
+
+/*
+ * Shape of the waveform driven into the actuator
+ */
+#define QCOM_HAPTICS_WAVE_SHAPE_SINE	0
+#define QCOM_HAPTICS_WAVE_SHAPE_SQUARE	1
+
+/*
+ * High impedance options of the auto resonance detection
+ */
+#define QCOM_HAPTICS_LRA_HIGH_Z_NONE	0
+#define QCOM_HAPTICS_LRA_HIGH_Z_OPT1	1
+#define QCOM_HAPTICS_LRA_HIGH_Z_OPT2	2
+#define QCOM_HAPTICS_LRA_HIGH_Z_OPT3	3
+
+#endif

-- 
2.55.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v8 2/9] dt-bindings: mfd: qcom,spmi-pmic: Allow the PMI8998 haptics child node
  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:13 ` 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
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 12+ messages in thread
From: David Heidelberg via B4 Relay @ 2026-11-30  0:13 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Lee Jones, Stephen Boyd, Bjorn Andersson, Konrad Dybcio,
	Abel Vesa, David Heidelberg
  Cc: David Collins, Subbaraman Narayanamurthy, Kamal Wadhwa,
	Fenglin Wu, Casey Connolly, linux-arm-msm, linux-input,
	devicetree, linux-kernel, mfd, phone-devel

From: David Heidelberg <david@ixit.cz>

The haptics child node currently validates only against the
qcom,spmi-haptics schema. Let it also match qcom,pmi8998-haptics.yaml,
which describes the older haptics module in PMI8998 with a different
register layout.

Signed-off-by: David Heidelberg <david@ixit.cz>
---
 Documentation/devicetree/bindings/mfd/qcom,spmi-pmic.yaml | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/Documentation/devicetree/bindings/mfd/qcom,spmi-pmic.yaml b/Documentation/devicetree/bindings/mfd/qcom,spmi-pmic.yaml
index 7a889d8883835..3ed0e69e43ad2 100644
--- a/Documentation/devicetree/bindings/mfd/qcom,spmi-pmic.yaml
+++ b/Documentation/devicetree/bindings/mfd/qcom,spmi-pmic.yaml
@@ -166,17 +166,19 @@ patternProperties:
       - $ref: /schemas/power/supply/qcom,pmi8998-charger.yaml#
 
   "gpio@[0-9a-f]+$":
     type: object
     $ref: /schemas/pinctrl/qcom,pmic-gpio.yaml#
 
   "^haptics@[0-9a-f]+$":
     type: object
-    $ref: /schemas/input/qcom,spmi-haptics.yaml#
+    oneOf:
+      - $ref: /schemas/input/qcom,pmi8998-haptics.yaml#
+      - $ref: /schemas/input/qcom,spmi-haptics.yaml#
 
   "^led-controller@[0-9a-f]+$":
     type: object
     $ref: /schemas/leds/qcom,spmi-flash-led.yaml#
 
   "^nvram@[0-9a-f]+$":
     type: object
     $ref: /schemas/nvmem/qcom,spmi-sdam.yaml#

-- 
2.55.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v8 3/9] Input: qcom-pmi8998-haptics - Add Qualcomm PMI8998 haptics driver
  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: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 ` David Heidelberg via B4 Relay
  2026-11-30  0:26   ` sashiko-bot
  2026-11-30  0:13 ` [PATCH v8 4/9] arm64: dts: qcom: pmi8998: Add haptics node David Heidelberg via B4 Relay
                   ` (5 subsequent siblings)
  8 siblings, 1 reply; 12+ messages in thread
From: David Heidelberg via B4 Relay @ 2026-11-30  0:13 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Lee Jones, Stephen Boyd, Bjorn Andersson, Konrad Dybcio,
	Abel Vesa, David Heidelberg
  Cc: David Collins, Subbaraman Narayanamurthy, Kamal Wadhwa,
	Fenglin Wu, Casey Connolly, linux-arm-msm, linux-input,
	devicetree, linux-kernel, mfd, phone-devel

From: David Heidelberg <david@ixit.cz>

Add a driver for the haptics module in the Qualcomm PMI8998 PMIC, based
on the downstream qpnp-haptics driver and using the input force-feedback
framework.

Both linear resonant actuators (LRA) and eccentric rotating mass (ERM)
motors are supported in direct and 8-sample buffer play modes.
FF_RUMBLE, FF_CONSTANT, FF_PERIODIC (FF_SINE and FF_CUSTOM) effects and
FF_GAIN master gain are supported, with requested strength scaled
linearly between 116 mV and the board's qcom,vmax-microvolt and timed
effects stopped automatically after replay.length ms. For LRA actuators,
the module's auto-resonance measurement is polled while driving to track
resonance drift within the configured variation bounds; ERM actuators
disable auto-resonance and use the configured sample play rate. Active
braking and short-circuit fault handling (which shuts down the module if
shorts persist) are supported. Audio and PWM waveform sources, overdrive
and multi-set waveforms are not implemented.

PMI8941, PMI8994 and PMI8996 carry a similar block; they can be added
once tested.

Based on work of Casey Connolly.

Signed-off-by: David Heidelberg <david@ixit.cz>
---
 MAINTAINERS                               |    1 +
 drivers/input/misc/Kconfig                |   13 +
 drivers/input/misc/Makefile               |    1 +
 drivers/input/misc/qcom-pmi8998-haptics.c | 2025 +++++++++++++++++++++++++++++
 4 files changed, 2040 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 301689be7665d..4f85225ed419c 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -23389,16 +23389,17 @@ F:	Documentation/devicetree/bindings/power/supply/qcom,pmi8998-charger.yaml
 F:	drivers/power/supply/qcom_smbx.c
 
 QUALCOMM PMI8998 HAPTICS DRIVER
 M:	David Heidelberg <david@ixit.cz>
 L:	linux-arm-msm@vger.kernel.org
 L:	linux-input@vger.kernel.org
 S:	Maintained
 F:	Documentation/devicetree/bindings/input/qcom,pmi8998-haptics.yaml
+F:	drivers/input/misc/qcom-pmi8998-haptics.c
 F:	include/dt-bindings/input/qcom,haptics.h
 
 QUALCOMM PPE DRIVER
 M:	Luo Jie <jie.luo@oss.qualcomm.com>
 L:	netdev@vger.kernel.org
 S:	Maintained
 F:	Documentation/devicetree/bindings/net/qcom,ipq9574-ppe.yaml
 F:	Documentation/networking/device_drivers/ethernet/qualcomm/ppe/ppe.rst
diff --git a/drivers/input/misc/Kconfig b/drivers/input/misc/Kconfig
index 169768e3d5bb5..88ace267bccfb 100644
--- a/drivers/input/misc/Kconfig
+++ b/drivers/input/misc/Kconfig
@@ -231,16 +231,29 @@ config INPUT_PMIC8XXX_PWRKEY
 	help
 	  Say Y here if you want support for the PMIC8XXX power key.
 
 	  If unsure, say N.
 
 	  To compile this driver as a module, choose M here: the
 	  module will be called pmic8xxx-pwrkey.
 
+config INPUT_QCOM_PMI8998_HAPTICS
+	tristate "Qualcomm PMI8998 / PM660 haptics support"
+	depends on MFD_SPMI_PMIC || COMPILE_TEST
+	select REGMAP
+	help
+	  Say Y here to enable support for the haptics module found in the
+	  Qualcomm PMI8998 and PM660 PMICs. It drives a linear resonant
+	  actuator (LRA) or an eccentric rotating mass (ERM) motor through the
+	  Linux input force-feedback framework.
+
+	  To compile this driver as a module, choose M here: the module will
+	  be called qcom-pmi8998-haptics.
+
 config INPUT_SPARCSPKR
 	tristate "SPARC Speaker support"
 	depends on PCI && SPARC64
 	help
 	  Say Y here if you want the standard Speaker on Sparc PCI systems
 	  to be used for bells and whistles.
 
 	  If unsure, say Y.
diff --git a/drivers/input/misc/Makefile b/drivers/input/misc/Makefile
index 1339619f07696..d9713c386a0a8 100644
--- a/drivers/input/misc/Makefile
+++ b/drivers/input/misc/Makefile
@@ -66,16 +66,17 @@ obj-$(CONFIG_INPUT_PCF8574)		+= pcf8574_keypad.o
 obj-$(CONFIG_INPUT_PCSPKR)		+= pcspkr.o
 obj-$(CONFIG_INPUT_PF1550_ONKEY)	+= pf1550-onkey.o
 obj-$(CONFIG_INPUT_PM8941_PWRKEY)	+= pm8941-pwrkey.o
 obj-$(CONFIG_INPUT_PM8XXX_VIBRATOR)	+= pm8xxx-vibrator.o
 obj-$(CONFIG_INPUT_PMIC8XXX_PWRKEY)	+= pmic8xxx-pwrkey.o
 obj-$(CONFIG_INPUT_POWERMATE)		+= powermate.o
 obj-$(CONFIG_INPUT_PWM_BEEPER)		+= pwm-beeper.o
 obj-$(CONFIG_INPUT_PWM_VIBRA)		+= pwm-vibra.o
+obj-$(CONFIG_INPUT_QCOM_PMI8998_HAPTICS)	+= qcom-pmi8998-haptics.o
 obj-$(CONFIG_INPUT_QNAP_MCU)		+= qnap-mcu-input.o
 obj-$(CONFIG_INPUT_RAVE_SP_PWRBUTTON)	+= rave-sp-pwrbutton.o
 obj-$(CONFIG_INPUT_RB532_BUTTON)	+= rb532_button.o
 obj-$(CONFIG_INPUT_REGULATOR_HAPTIC)	+= regulator-haptic.o
 obj-$(CONFIG_INPUT_RETU_PWRBUTTON)	+= retu-pwrbutton.o
 obj-$(CONFIG_INPUT_RT5120_PWRKEY)	+= rt5120-pwrkey.o
 obj-$(CONFIG_INPUT_AXP20X_PEK)		+= axp20x-pek.o
 obj-$(CONFIG_INPUT_GPIO_ROTARY_ENCODER)	+= rotary_encoder.o
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 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2021, Casey Connolly
+ * Copyright David Heidelberg.
+ * Qualcomm SPMI haptics driver for the PMI8998 PMIC.
+ */
+
+#include <dt-bindings/input/qcom,haptics.h>
+#include <linux/atomic.h>
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/errno.h>
+#include <linux/input.h>
+#include <linux/interrupt.h>
+#include <linux/jiffies.h>
+#include <linux/kernel.h>
+#include <linux/ktime.h>
+#include <linux/limits.h>
+#include <linux/list.h>
+#include <linux/log2.h>
+#include <linux/math.h>
+#include <linux/minmax.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/platform_device.h>
+#include <linux/pm.h>
+#include <linux/property.h>
+#include <linux/regmap.h>
+#include <linux/spinlock.h>
+#include <linux/string.h>
+#include <linux/types.h>
+#include <linux/uaccess.h>
+#include <linux/workqueue.h>
+
+#define HAP_EN_CTL_REG			0x46
+#define  HAP_EN_BIT			BIT(7)
+
+#define HAP_EN_CTL2_REG			0x48
+#define  BRAKE_EN_BIT			BIT(0)
+
+#define HAP_EN_CTL3_REG			0x4a
+#define  HAP_HBRIDGE_EN_BIT		BIT(7)
+#define  HAP_PWM_SIGNAL_EN_BIT		BIT(6)
+#define  HAP_ILIM_EN_BIT		BIT(5)
+#define  HAP_ILIM_CC_EN_BIT		BIT(4)
+#define  HAP_AUTO_RES_RBIAS_EN_BIT	BIT(3)
+#define  HAP_DAC_EN_BIT			BIT(2)
+#define  HAP_PWM_CTL_EN_BIT		BIT(0)
+#define  HAP_EN_CTL3_DEFAULT		(HAP_HBRIDGE_EN_BIT | \
+					 HAP_PWM_SIGNAL_EN_BIT | \
+					 HAP_ILIM_EN_BIT | \
+					 HAP_ILIM_CC_EN_BIT | \
+					 HAP_AUTO_RES_RBIAS_EN_BIT | \
+					 HAP_DAC_EN_BIT | \
+					 HAP_PWM_CTL_EN_BIT)
+
+#define HAP_AUTO_RES_CTRL_REG		0x4b
+#define  HAP_SEL_AUTO_RES_PERIOD_BIT	BIT(6)
+#define  HAP_AUTO_RES_CNT_ERR_DELTA_MASK GENMASK(5, 4)
+#define  HAP_AUTO_RES_CNT_ERR_DELTA_DEF	2
+#define  HAP_AUTO_RES_ERR_RECOVERY_BIT	BIT(3)
+#define  HAP_AUTO_RES_EN_DLY_MASK	GENMASK(2, 0)
+#define  HAP_AUTO_RES_EN_DLY_DEF	4
+
+#define HAP_CFG1_REG			0x4c
+#define  HAP_ACT_TYPE_MASK		BIT(0)
+#define  HAP_TYPE_LRA			0
+#define  HAP_TYPE_ERM			1
+
+#define HAP_CFG2_REG			0x4d
+#define  HAP_LRA_RES_TYPE_MASK		BIT(0)
+#define  HAP_WAVE_SINE			0
+#define  HAP_WAVE_SQUARE		1
+
+#define HAP_SEL_REG			0x4e
+#define  HAP_WF_SOURCE_MASK		GENMASK(5, 4)
+#define  HAP_PLAY_DIRECT		0
+#define  HAP_PLAY_BUFFER		1
+
+#define HAP_LRA_AUTO_RES_REG		0x4f
+#define  LRA_AUTO_RES_MODE_MASK		GENMASK(6, 4)
+#define  HAP_AUTO_RES_QWD		2
+#define  HAP_AUTO_RES_ZXD_EOP		4
+#define  LRA_HIGH_Z_MASK		GENMASK(3, 2)
+#define  LRA_HIGH_Z_NONE		0
+#define  LRA_HIGH_Z_OPT1		1
+#define  LRA_HIGH_Z_OPT2		2
+#define  LRA_HIGH_Z_OPT3		3
+#define  LRA_RES_CAL_MASK		GENMASK(1, 0)
+#define  HAP_RES_CAL_PERIOD_MIN		4
+#define  HAP_RES_CAL_PERIOD_MAX		32
+/* PM660 auto resonance configuration layout */
+#define  PM660_AUTO_RES_MODE_MASK	BIT(7)
+#define  PM660_AUTO_RES_ZXD		0
+#define  PM660_AUTO_RES_QWD		1
+#define  PM660_CAL_DURATION_MASK	GENMASK(6, 5)
+#define  PM660_CAL_EOP_EN_BIT		BIT(3)
+#define  PM660_LRA_RES_CAL_MASK		GENMASK(2, 0)
+#define  PM660_RES_CAL_PERIOD_MAX	256
+
+/*
+ * Time needed for the back-EMF of the actuator to build up. With the
+ * quarter-wave detection mode the high impedance is applied for every
+ * LRA cycle, so there is not enough back-EMF when the playback starts.
+ */
+#define HAPTICS_BACK_EMF_DELAY_MS	20
+
+/* Interval at which the drive period is read back from the hardware */
+#define HAP_AUTO_RES_POLL_MS		20
+
+#define HAP_BUSY_POLL_US		1000
+#define HAP_BUSY_TIMEOUT_US		100000
+
+/* Accepted variation of the drive period in percent */
+#define HAP_RATE_VAR_PCT_DEFAULT	25
+
+#define HAP_VMAX_CFG_REG		0x51
+#define  HAP_VMAX_OVD_BIT		BIT(6)
+#define  HAP_VMAX_MASK			GENMASK(5, 1)
+
+#define HAP_ILIM_CFG_REG		0x52
+#define	 HAP_ILIM_SEL_MASK		GENMASK(1, 0)
+#define  HAP_ILIM_SEL_400_MA		0
+#define  HAP_ILIM_SEL_800_MA		1
+#define  HAP_ILIM_SEL_1000_MA		2
+
+#define HAP_SC_DEB_REG			0x53
+#define  HAP_SC_DEB_MASK		GENMASK(2, 0)
+/* Debounce of 0, 8, 16 or 32 cycles is encoded as 0 to 3 */
+#define  HAP_SC_DEB_8_CYCLES		1
+#define  HAP_SC_DEB_32_CYCLES		3
+
+#define HAP_RATE_CFG1_REG		0x54
+#define  HAP_RATE_CFG1_MASK		GENMASK(7, 0)
+#define  HAP_RATE_CFG2_SHIFT		8 /* As CFG2 is the MSB */
+
+#define HAP_RATE_CFG2_REG		0x55
+#define  HAP_RATE_CFG2_MASK		GENMASK(3, 0)
+
+#define HAP_SC_CLR_REG			0x59
+#define  SC_CLR_BIT			BIT(0)
+
+#define HAP_ZX_CFG_REG			0x5a
+#define  HAP_ZX_DET_DEB_MASK		GENMASK(2, 0)
+#define  HAP_ZX_DET_DEB_80US		3
+
+#define HAP_BRAKE_REG			0x5c
+
+#define HAP_WF_REPEAT_REG		0x5e
+#define  WF_REPEAT_MASK			GENMASK(6, 4)
+#define  WF_S_REPEAT_MASK		GENMASK(1, 0)
+
+#define HAP_WF_S1_REG			0x60
+#define  HAP_WF_SAMP_SIGN_BIT		BIT(7)
+#define  HAP_WF_SAMP_AMP_MAX		0x1f
+#define  HAP_WF_SAMP_MAX		GENMASK(5, 1)
+
+#define HAP_PLAY_REG			0x70
+#define  HAP_PLAY_BIT			BIT(7)
+#define  HAP_PAUSE_BIT			BIT(0)
+
+#define HAP_STATUS_1_REG		0x0a
+#define  HAP_BUSY_BIT			BIT(1)
+#define  SC_FLAG_BIT			BIT(3)
+#define  HAP_AUTO_RES_ERROR_BIT		BIT(4)
+
+/* The drive period measured by the auto resonance detection */
+#define HAP_LRA_AUTO_RES_LO_REG	0x0b
+#define HAP_LRA_AUTO_RES_HI_REG	0x0c
+
+#define HAP_SEC_ACCESS_REG		0xd0
+#define  HAP_SEC_ACCESS_UNLOCK		0xa5
+
+#define HAP_TEST2_REG			0xe3
+#define  AUTO_RES_EN_BIT		BIT(7)
+
+#define HAP_VMAX_MIN_MV			116
+#define HAP_VMAX_MAX_MV			3596
+
+#define HAP_PLAY_RATE_MAX_US		20475
+#define HAP_PLAY_RATE_DEFAULT_US	5715
+
+#define HAP_BRAKE_PAT_LEN		4
+#define HAP_BRAKE_PAT_MASK		0x3
+#define HAP_WAVE_SAMP_LEN		8
+#define HAPTICS_MAX_EFFECTS		8
+#define HAP_WAVE_REP_CNT_MIN		1
+#define HAP_WAVE_REP_CNT_MAX		128
+#define HAP_WAVE_SAMP_REP_CNT_MIN	1
+#define HAP_WAVE_SAMP_REP_CNT_MAX	8
+#define HAP_RATE_CFG_STEP_US		5
+
+#define SC_MAX_COUNT			5
+#define SC_COUNT_RST_DELAY_US		1000000
+
+enum hap_play_control {
+	HAP_STOP,
+	HAP_PAUSE,
+	HAP_PLAY,
+};
+
+enum pmi8998_haptics_mode {
+	PMI8998_HAPTICS_MODE_NONE,
+	PMI8998_HAPTICS_MODE_DIRECT,
+	PMI8998_HAPTICS_MODE_BUFFER,
+};
+
+/**
+ * struct pmi8998_haptics_effect - a haptics effect
+ * @mode: The play mode of the effect.
+ * @magnitude: The requested strength of the effect, 0 to U16_MAX.
+ * @length_ms: Duration of the effect in milliseconds, 0 for infinite.
+ * @wave_samp: The waveform of the effect in the format of the waveform
+ *	sample register of the module.
+ * @wave_valid: Whether the effect carries a waveform.
+ * @play_node: Entry in the pending playback request queue.
+ * @play_req: Pending playback request state (true to play, false to stop).
+ */
+struct pmi8998_haptics_effect {
+	enum pmi8998_haptics_mode mode;
+	u16 magnitude;
+	u16 length_ms;
+	u8 wave_samp[HAP_WAVE_SAMP_LEN];
+	bool wave_valid;
+	struct list_head play_node;
+	bool play_req;
+};
+
+struct pmi8998_haptics;
+
+/**
+ * struct pmi8998_haptics_data - per-PMIC hardware description
+ * @auto_res_en_reg: Register holding AUTO_RES_EN_BIT.
+ * @res_cal_period_max: Maximum supported auto-resonance calibration period.
+ * @hw_auto_res: Hardware handles auto-resonance period tracking and recovery.
+ * @init_chip: Optional PMIC-specific hardware initialization callback.
+ * @config_auto_res: Callback to configure LRA auto-resonance registers.
+ */
+struct pmi8998_haptics_data {
+	u16 auto_res_en_reg;
+	u16 res_cal_period_max;
+	bool hw_auto_res;
+	int (*init_chip)(struct pmi8998_haptics *haptics);
+	int (*config_auto_res)(struct pmi8998_haptics *haptics);
+};
+
+/**
+ * struct pmi8998_haptics - driver data.
+ *
+ * @dev: The haptics device.
+ * @data: Per-PMIC hardware description.
+ * @regmap: Register map for the hardware block.
+ * @work: Work struct to process queued play/stop requests.
+ * @gain_work: Work struct to update drive voltage on gain/strength change.
+ * @auto_res_work: Work that enables and polls auto resonance detection.
+ * @stop_work: Delayed work that stops playback after replay.length expires.
+ * @base: Base address of the regmap.
+ * @sc_irq: Short circuit irq.
+ * @last_sc_time: Time since the short circuit IRQ last fired.
+ * @sc_count: Number of times the short circuit IRQ has fired in this interval.
+ * @perm_disable: Latched true after persistent short-circuit faults.
+ * @magnitude: Requested strength of the effect, 0 to U16_MAX.
+ * @vmax: Max voltage to use when playing.
+ * @vmax_limit: Maximum drive voltage from qcom,vmax-microvolt in millivolts.
+ * @act_type: Actuator type (LRA or ERM).
+ * @play_rate_us: Resonance period for LRA or sample duration for ERM in microseconds.
+ * @auto_res_mode: Auto resonance detection mode.
+ * @res_cal_period: Auto resonance calibration period in cycles.
+ * @mode: The default play mode of the haptics module.
+ * @wave_shape: Shape of the waveform driven into the actuator.
+ * @lra_high_z: High impedance option of the auto resonance detection.
+ * @wave_rep_cnt: Number of times the waveform is repeated.
+ * @wave_samp_rep_cnt: Number of times the waveform samples are repeated.
+ * @brake_en: Whether the brake is enabled.
+ * @brake_pat: The brake pattern.
+ * @wave_samp: The array of wave samples to write for buffer mode.
+ * @rate_cfg: The programmed drive period code.
+ * @rate_min_limit: The lowest accepted drive period code.
+ * @rate_max_limit: The highest accepted drive period code.
+ * @rate_var_pct_min: Minimum accepted drive period variation in percent.
+ * @rate_var_pct_max: Maximum accepted drive period variation in percent.
+ * @auto_res_enabled: Whether auto resonance detection is currently enabled.
+ * @auto_res_ready: Jiffies timestamp after which auto-resonance readback is valid.
+ * @hw_enabled: Whether the hardware module is currently enabled.
+ * @active_effect_id: The effect which is currently played, or -1 if idle.
+ * @active_mode: The play mode of the effect which is played.
+ * @stop_armed: Whether @stop_work is armed for the current effect.
+ * @stop_timeline: Jiffies deadline at which @stop_work stops playback.
+ * @gain: The gain of the haptics, 0 to U16_MAX.
+ * @effects: The effects of the haptics.
+ * @req_lock: Spinlock protecting @play_queue and per-effect play requests.
+ * @play_queue: Pending start/stop requests from atomic playback callbacks.
+ * @play_lock: Mutex serialising hardware state and effect table updates.
+ */
+struct pmi8998_haptics {
+	struct device *dev;
+	const struct pmi8998_haptics_data *data;
+	struct regmap *regmap;
+	struct work_struct work;
+	struct work_struct gain_work;
+	struct delayed_work auto_res_work;
+	struct delayed_work stop_work;
+	u32 base;
+
+	int sc_irq;
+	ktime_t last_sc_time;
+	u8 sc_count;
+	bool perm_disable;
+
+	u16 magnitude;
+	u32 vmax;
+	u32 vmax_limit;
+	u8 act_type;
+	u32 play_rate_us;
+	u8 auto_res_mode;
+	u16 res_cal_period;
+	enum pmi8998_haptics_mode mode;
+	u8 wave_shape;
+	u8 lra_high_z;
+	u8 wave_rep_cnt;
+	u8 wave_samp_rep_cnt;
+	bool brake_en;
+	u8 brake_pat[HAP_BRAKE_PAT_LEN];
+
+	u8 wave_samp[HAP_WAVE_SAMP_LEN];
+
+	u32 rate_cfg;
+	u32 rate_min_limit;
+	u32 rate_max_limit;
+	u8 rate_var_pct_min;
+	u8 rate_var_pct_max;
+	bool auto_res_enabled;
+	unsigned long auto_res_ready;
+	bool hw_enabled;
+	int active_effect_id;
+	enum pmi8998_haptics_mode active_mode;
+	bool stop_armed;
+	unsigned long stop_timeline;
+	atomic_t gain;
+
+	struct pmi8998_haptics_effect effects[HAPTICS_MAX_EFFECTS];
+
+	/* protects play_queue and effect play_node/play_req */
+	spinlock_t req_lock;
+	struct list_head play_queue;
+
+	/* serialises enable/disable against the hardware */
+	struct mutex play_lock;
+};
+
+static inline bool is_secure_addr(u16 addr)
+{
+	return (addr & 0xff) > 0xd0;
+}
+
+static int pmi8998_haptics_read(struct pmi8998_haptics *haptics,
+				u16 offset, u8 *val, int len)
+{
+	u16 addr = haptics->base + offset;
+	int err;
+
+	err = regmap_bulk_read(haptics->regmap, addr, val, len);
+	if (err < 0) {
+		dev_err(haptics->dev, "Error reading address: 0x%x, err %d\n",
+			addr, err);
+		return err;
+	}
+
+	return 0;
+}
+
+static int pmi8998_haptics_write(struct pmi8998_haptics *haptics,
+				 u16 offset, u8 *val, int len)
+{
+	u16 addr = haptics->base + offset;
+	int err = 0;
+
+	if (is_secure_addr(addr)) {
+		for (int i = 0; i < len; i++) {
+			dev_dbg(haptics->dev, "%s: unlocking for addr: 0x%x, val: 0x%x\n",
+				__func__, addr, val[i]);
+			err = regmap_write(haptics->regmap,
+					   haptics->base + HAP_SEC_ACCESS_REG,
+					   HAP_SEC_ACCESS_UNLOCK);
+			if (err < 0) {
+				dev_err(haptics->dev,
+					"Error writing unlock code, err %d\n",
+					err);
+				return err;
+			}
+
+			err = regmap_write(haptics->regmap, addr + i, val[i]);
+			if (err < 0) {
+				dev_err(haptics->dev,
+					"Error writing address 0x%x, err %d\n",
+					addr + i, err);
+				return err;
+			}
+		}
+	} else {
+		if (len > 1)
+			err = regmap_bulk_write(haptics->regmap, addr, val, len);
+		else
+			err = regmap_write(haptics->regmap, addr, *val);
+
+		if (err < 0) {
+			dev_err(haptics->dev,
+				"%s: Error writing address: 0x%x, err %d\n",
+				__func__, addr, err);
+			return err;
+		}
+	}
+
+	return 0;
+}
+
+static int pmi8998_haptics_write_masked(struct pmi8998_haptics *haptics,
+					u16 offset, u8 mask, u8 val)
+{
+	u16 addr = haptics->base + offset;
+	int err;
+
+	if (is_secure_addr(addr)) {
+		err = regmap_write(haptics->regmap,
+				   haptics->base + HAP_SEC_ACCESS_REG,
+				   HAP_SEC_ACCESS_UNLOCK);
+		if (err < 0) {
+			dev_err(haptics->dev, "Error writing unlock code - err %d\n",
+				err);
+			return err;
+		}
+	}
+
+	err = regmap_update_bits(haptics->regmap, addr, mask, val);
+	if (err < 0) {
+		dev_err(haptics->dev, "Error writing address: 0x%x - err %d\n",
+			addr, err);
+		return err;
+	}
+
+	return 0;
+}
+
+static bool is_haptics_idle(struct pmi8998_haptics *haptics)
+{
+	int err;
+	u8 val;
+
+	err = pmi8998_haptics_read(haptics, HAP_STATUS_1_REG, &val, 1);
+	if (err < 0 || (val & HAP_BUSY_BIT))
+		return false;
+
+	return true;
+}
+
+static int pmi8998_haptics_wait_idle(struct pmi8998_haptics *haptics)
+{
+	unsigned int status;
+
+	return regmap_read_poll_timeout(haptics->regmap,
+					haptics->base + HAP_STATUS_1_REG,
+					status, !(status & HAP_BUSY_BIT),
+					HAP_BUSY_POLL_US, HAP_BUSY_TIMEOUT_US);
+}
+
+static int pmi8998_haptics_module_enable(struct pmi8998_haptics *haptics, bool enable)
+{
+	u8 val;
+
+	dev_dbg(haptics->dev, "Setting module enable: %d\n", enable);
+
+	val = enable ? HAP_EN_BIT : 0;
+	return pmi8998_haptics_write(haptics, HAP_EN_CTL_REG, &val, 1);
+}
+
+static int pmi8998_haptics_write_vmax(struct pmi8998_haptics *haptics)
+{
+	u8 val = 0;
+	u32 vmax_mv = haptics->vmax;
+
+	vmax_mv = clamp_t(u32, vmax_mv, HAP_VMAX_MIN_MV, haptics->vmax_limit);
+
+	dev_dbg(haptics->dev, "Setting vmax to: %d\n", vmax_mv);
+
+	val = FIELD_PREP(HAP_VMAX_MASK, vmax_mv / HAP_VMAX_MIN_MV);
+
+	return pmi8998_haptics_write_masked(haptics, HAP_VMAX_CFG_REG,
+					    HAP_VMAX_MASK | HAP_VMAX_OVD_BIT,
+					    val);
+}
+
+static int pmi8998_haptics_write_current_limit(struct pmi8998_haptics *haptics)
+{
+	/* default from the upstream driver */
+	return pmi8998_haptics_write_masked(haptics, HAP_ILIM_CFG_REG,
+					    HAP_ILIM_SEL_MASK, HAP_ILIM_SEL_1000_MA);
+}
+
+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 int pmi8998_haptics_write_play_rate(struct pmi8998_haptics *haptics, u16 play_rate)
+{
+	u8 val[2];
+
+	dev_dbg(haptics->dev, "Setting play_rate to: %d\n", play_rate);
+
+	val[0] = FIELD_PREP(HAP_RATE_CFG1_MASK, play_rate);
+	val[1] = FIELD_PREP(HAP_RATE_CFG2_MASK, play_rate >> HAP_RATE_CFG2_SHIFT);
+	return pmi8998_haptics_write(haptics, HAP_RATE_CFG1_REG, val, 2);
+}
+
+/*
+ * pmi8998_haptics_set_auto_res() - Auto resonance
+ * allows the haptics to automatically adjust the
+ * speed of the oscillation in order to maintain
+ * the resonant frequency.
+ */
+static int pmi8998_haptics_set_auto_res(struct pmi8998_haptics *haptics, bool enable)
+{
+	u8 val = enable ? AUTO_RES_EN_BIT : 0;
+	int err;
+
+	err = pmi8998_haptics_write_masked(haptics,
+					   haptics->data->auto_res_en_reg,
+					   AUTO_RES_EN_BIT, val);
+	if (err < 0)
+		return err;
+
+	if (enable)
+		haptics->auto_res_ready =
+			jiffies + msecs_to_jiffies(HAP_AUTO_RES_POLL_MS);
+	haptics->auto_res_enabled = enable;
+	return 0;
+}
+
+/**
+ * pmi8998_haptics_update_rate - update the drive period from the auto resonance
+ * @haptics: pointer to haptics struct
+ *
+ * The auto resonance of the module measures the resonance of the actuator
+ * while it is driven. The measured drive period is read back and used to
+ * follow the resonance of the actuator, which drifts with the temperature
+ * and the age of the actuator.
+ *
+ * The auto resonance is disabled when the measurement fails or when the
+ * measured drive period is out of the accepted range, driving the actuator
+ * off resonance is worse than driving it at the nominal frequency.
+ */
+static void pmi8998_haptics_update_rate(struct pmi8998_haptics *haptics)
+{
+	u8 buf[2], val;
+	u32 rate;
+	int err;
+
+	if (!haptics->auto_res_enabled)
+		return;
+
+	err = pmi8998_haptics_read(haptics, HAP_LRA_AUTO_RES_LO_REG, buf, 2);
+	if (err < 0)
+		return;
+
+	/*
+	 * The upper nibble of the high byte holds bits [11:8] of the
+	 * measured drive period.
+	 */
+	rate = ((u32)(buf[1] & 0xf0) << 4) | buf[0];
+
+	err = pmi8998_haptics_read(haptics, HAP_STATUS_1_REG, &val, 1);
+	if (err < 0)
+		return;
+
+	if (val & HAP_AUTO_RES_ERROR_BIT) {
+		dev_dbg(haptics->dev, "Auto resonance error, disabling it\n");
+		pmi8998_haptics_set_auto_res(haptics, false);
+		return;
+	}
+
+	if (rate <= haptics->rate_min_limit ||
+	    rate >= haptics->rate_max_limit) {
+		dev_dbg(haptics->dev,
+			"Measured drive period 0x%x out of range [0x%x, 0x%x], disabling auto resonance\n",
+			rate, haptics->rate_min_limit, haptics->rate_max_limit);
+		pmi8998_haptics_set_auto_res(haptics, false);
+		return;
+	}
+
+	if (rate == haptics->rate_cfg)
+		return;
+
+	dev_dbg(haptics->dev, "Updating drive period from 0x%x to 0x%x\n",
+		haptics->rate_cfg, rate);
+
+	err = pmi8998_haptics_write_play_rate(haptics, rate);
+	if (err < 0) {
+		dev_err(haptics->dev, "Error updating the drive period, err=%d\n",
+			err);
+		return;
+	}
+
+	haptics->rate_cfg = rate;
+}
+
+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));
+}
+
+/* Configure active braking */
+static int pmi8998_haptics_write_brake(struct pmi8998_haptics *haptics)
+{
+	int err;
+	u8 val = 0;
+
+	err = pmi8998_haptics_write_masked(haptics, HAP_EN_CTL2_REG,
+					   BRAKE_EN_BIT,
+					   haptics->brake_en ? BRAKE_EN_BIT : 0);
+	if (err < 0)
+		return err;
+
+	if (!haptics->brake_en)
+		return 0;
+
+	for (int i = HAP_BRAKE_PAT_LEN - 1; i >= 0; i--)
+		val |= (haptics->brake_pat[i] & HAP_BRAKE_PAT_MASK) << (i * 2);
+
+	err = pmi8998_haptics_write(haptics, HAP_BRAKE_REG, &val, 1);
+	if (err < 0)
+		return err;
+
+	return 0;
+}
+
+static int pmi8998_haptics_write_buffer_config(struct pmi8998_haptics *haptics)
+{
+	return pmi8998_haptics_write(haptics, HAP_WF_S1_REG,
+				     haptics->wave_samp, HAP_WAVE_SAMP_LEN);
+}
+
+/*
+ * pmi8998_haptics_write_wave_repeat() - write wave repeat values.
+ */
+static int pmi8998_haptics_write_wave_repeat(struct pmi8998_haptics *haptics)
+{
+	u8 val, mask;
+
+	/* The number of times to repeat each wave */
+	mask = WF_REPEAT_MASK | WF_S_REPEAT_MASK;
+	val = FIELD_PREP(WF_REPEAT_MASK, ilog2(haptics->wave_rep_cnt)) |
+	      FIELD_PREP(WF_S_REPEAT_MASK, ilog2(haptics->wave_samp_rep_cnt));
+
+	return pmi8998_haptics_write_masked(haptics, HAP_WF_REPEAT_REG,
+					    mask, val);
+}
+
+static int pmi8998_haptics_write_play_control(struct pmi8998_haptics *haptics,
+					      enum hap_play_control ctrl)
+{
+	u8 val;
+
+	switch (ctrl) {
+	case HAP_STOP:
+		val = 0;
+		break;
+	case HAP_PAUSE:
+		val = HAP_PAUSE_BIT;
+		break;
+	case HAP_PLAY:
+		val = HAP_PLAY_BIT;
+		break;
+	default:
+		return 0;
+	}
+
+	dev_dbg(haptics->dev, "haptics play ctrl: %d\n", ctrl);
+	return pmi8998_haptics_write(haptics, HAP_PLAY_REG, &val, 1);
+}
+
+static int pmi8998_haptics_config_auto_res(struct pmi8998_haptics *haptics)
+{
+	u8 mode, val, mask;
+
+	mode = haptics->auto_res_mode == QCOM_LRA_AUTO_RES_MODE_QWD ?
+	       HAP_AUTO_RES_QWD : HAP_AUTO_RES_ZXD_EOP;
+
+	val = FIELD_PREP(LRA_RES_CAL_MASK,
+			 ilog2(haptics->res_cal_period / HAP_RES_CAL_PERIOD_MIN)) |
+	      FIELD_PREP(LRA_AUTO_RES_MODE_MASK, mode) |
+	      FIELD_PREP(LRA_HIGH_Z_MASK, haptics->lra_high_z);
+
+	mask = LRA_AUTO_RES_MODE_MASK | LRA_HIGH_Z_MASK | LRA_RES_CAL_MASK;
+
+	return pmi8998_haptics_write_masked(haptics, HAP_LRA_AUTO_RES_REG,
+					    mask, val);
+}
+
+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);
+}
+
+/**
+ * pmi8998_haptics_init() - Initialise haptics hardware for use
+ * @haptics: haptics device
+ * Returns: 0 on success, < 0 on error
+ */
+static int pmi8998_haptics_init(struct pmi8998_haptics *haptics)
+{
+	int err;
+	u8 val;
+	u16 play_rate;
+
+	err = pmi8998_haptics_write_masked(haptics, HAP_CFG1_REG,
+					   HAP_ACT_TYPE_MASK, haptics->act_type);
+	if (err < 0)
+		return err;
+
+	if (haptics->data->init_chip) {
+		err = haptics->data->init_chip(haptics);
+		if (err < 0)
+			return err;
+	}
+
+	if (haptics->act_type == HAP_TYPE_LRA) {
+		/*
+		 * Configure auto resonance. The detection mode and calibration
+		 * period depend on the actuator and are given by the device tree.
+		 */
+		err = haptics->data->config_auto_res(haptics);
+		if (err < 0)
+			return err;
+	} else {
+		/* Disable auto resonance for ERM */
+		val = 0;
+		err = pmi8998_haptics_write(haptics, HAP_LRA_AUTO_RES_REG,
+					    &val, 1);
+		if (err < 0)
+			return err;
+
+		err = pmi8998_haptics_set_auto_res(haptics, false);
+		if (err < 0)
+			return err;
+	}
+
+	err = pmi8998_haptics_write_vmax(haptics);
+	if (err < 0)
+		return err;
+
+	/* Configure the ILIM register */
+	err = pmi8998_haptics_write_current_limit(haptics);
+	if (err < 0)
+		return err;
+
+	/*
+	 * Debounce short-circuit detection for 8 cycles, the value the
+	 * vendor driver uses on every SDM845 board. A longer debounce
+	 * delays the detection of a shorted actuator.
+	 */
+	err = pmi8998_haptics_write_masked(haptics, HAP_SC_DEB_REG,
+					   HAP_SC_DEB_MASK, HAP_SC_DEB_8_CYCLES);
+	if (err < 0)
+		return err;
+
+	if (haptics->act_type == HAP_TYPE_LRA) {
+		/* Configure the waveform shape */
+		err = pmi8998_haptics_write_masked(haptics, HAP_CFG2_REG,
+						   HAP_LRA_RES_TYPE_MASK,
+						   haptics->wave_shape);
+		if (err < 0)
+			return err;
+	}
+
+	play_rate = haptics->play_rate_us / HAP_RATE_CFG_STEP_US;
+
+	/*
+	 * Configure RATE_CFG1 and RATE_CFG2 registers.
+	 * Note: For ERM these registers act as play rate and
+	 * for LRA these represent resonance period
+	 */
+	err = pmi8998_haptics_write_play_rate(haptics, play_rate);
+	if (err < 0)
+		return err;
+
+	haptics->rate_cfg = play_rate;
+	if (haptics->act_type == HAP_TYPE_LRA) {
+		haptics->rate_min_limit =
+			(play_rate * (100 - haptics->rate_var_pct_min)) / 100;
+		haptics->rate_max_limit =
+			(play_rate * (100 + haptics->rate_var_pct_max)) / 100;
+	}
+
+	err = pmi8998_haptics_write_brake(haptics);
+	if (err < 0)
+		return err;
+
+	return 0;
+}
+
+/**
+ * pmi8998_haptics_start_direct - start the direct playback
+ * @haptics: pointer to haptics struct
+ * Returns: 0 on success, < 0 on failure
+ *
+ * In the direct mode the actuator is driven at its resonance frequency
+ * with the drive voltage, the waveform buffer of the module is not used.
+ */
+static int pmi8998_haptics_start_direct(struct pmi8998_haptics *haptics)
+{
+	return pmi8998_haptics_write_play_mode(haptics, HAP_PLAY_DIRECT);
+}
+
+/**
+ * pmi8998_haptics_start_buffer - start the buffer playback
+ * @haptics: pointer to haptics struct
+ * Returns: 0 on success, < 0 on failure
+ *
+ * In the buffer mode the waveform buffer of the module is played back,
+ * the amplitude of the waveform is given by the drive voltage.
+ */
+static int pmi8998_haptics_start_buffer(struct pmi8998_haptics *haptics)
+{
+	int err;
+
+	err = pmi8998_haptics_write_play_mode(haptics, HAP_PLAY_BUFFER);
+	if (err < 0)
+		return err;
+
+	err = pmi8998_haptics_write_wave_repeat(haptics);
+	if (err < 0)
+		return err;
+
+	if (haptics->active_effect_id >= 0) {
+		struct pmi8998_haptics_effect *eff =
+			&haptics->effects[haptics->active_effect_id];
+
+		if (eff->wave_valid)
+			memcpy(haptics->wave_samp, eff->wave_samp,
+			       HAP_WAVE_SAMP_LEN);
+		else
+			memset(haptics->wave_samp, HAP_WF_SAMP_MAX,
+			       HAP_WAVE_SAMP_LEN);
+	}
+
+	err = pmi8998_haptics_write_buffer_config(haptics);
+	if (err < 0)
+		return err;
+
+	return 0;
+}
+
+/**
+ * pmi8998_haptics_start - start the playback in the configured mode
+ * @haptics: pointer to haptics struct
+ * Returns: 0 on success, < 0 on failure
+ */
+static int pmi8998_haptics_start(struct pmi8998_haptics *haptics)
+{
+	switch (haptics->active_mode) {
+	case PMI8998_HAPTICS_MODE_DIRECT:
+		return pmi8998_haptics_start_direct(haptics);
+	case PMI8998_HAPTICS_MODE_BUFFER:
+		return pmi8998_haptics_start_buffer(haptics);
+	default:
+		return -EINVAL;
+	}
+}
+
+static void pmi8998_haptics_flush_queue(struct pmi8998_haptics *haptics)
+{
+	struct pmi8998_haptics_effect *eff, *tmp;
+
+	guard(spinlock_irqsave)(&haptics->req_lock);
+
+	list_for_each_entry_safe(eff, tmp, &haptics->play_queue, play_node)
+		list_del_init(&eff->play_node);
+}
+
+static u32 pmi8998_haptics_calc_vmax(struct pmi8998_haptics *haptics)
+{
+	u32 vmax;
+
+	vmax = HAP_VMAX_MIN_MV +
+	       mult_frac(haptics->vmax_limit - HAP_VMAX_MIN_MV,
+			 haptics->magnitude, U16_MAX);
+	vmax = HAP_VMAX_MIN_MV +
+	       mult_frac(vmax - HAP_VMAX_MIN_MV,
+			 atomic_read(&haptics->gain), U16_MAX);
+
+	return vmax;
+}
+
+/**
+ * pmi8998_haptics_hw_stop_locked - stop vibrating and power the module down
+ * @haptics: pointer to haptics struct
+ *
+ * Stops the hardware without clearing the active logical effect or its stop
+ * timer, so a muted effect can be resumed if strength or gain becomes non-zero.
+ *
+ * Returns: 0 on success, < 0 on failure
+ */
+static int pmi8998_haptics_hw_stop_locked(struct pmi8998_haptics *haptics)
+{
+	int error = 0;
+	int err;
+
+	lockdep_assert_held(&haptics->play_lock);
+
+	cancel_delayed_work(&haptics->auto_res_work);
+
+	if (!haptics->hw_enabled) {
+		haptics->auto_res_enabled = false;
+		return 0;
+	}
+
+	/* Read the drive period one last time before stopping */
+	if (!haptics->data->hw_auto_res && haptics->auto_res_enabled &&
+	    time_after_eq(jiffies, haptics->auto_res_ready))
+		pmi8998_haptics_update_rate(haptics);
+
+	err = pmi8998_haptics_write_play_control(haptics, HAP_STOP);
+	if (err < 0) {
+		dev_err(haptics->dev, "Error disabling play, err=%d\n", err);
+		error = err;
+	} else if (!haptics->perm_disable) {
+		/*
+		 * Wait for active braking and the current waveform cycle to
+		 * finish before cutting power to the H-bridge.
+		 */
+		if (pmi8998_haptics_wait_idle(haptics) < 0)
+			dev_dbg(haptics->dev,
+				"Timed out waiting for haptics idle, forcing disable\n");
+	}
+
+	/*
+	 * Disable the module even when stopping the playback failed. The
+	 * module drives the actuator, so leaving it enabled keeps the
+	 * actuator running.
+	 */
+	err = pmi8998_haptics_module_enable(haptics, false);
+	if (err < 0) {
+		dev_err(haptics->dev, "Error disabling module, err=%d\n", err);
+		if (!error)
+			error = err;
+	} else {
+		haptics->hw_enabled = false;
+	}
+
+	haptics->auto_res_enabled = false;
+
+	return error;
+}
+
+/**
+ * pmi8998_haptics_disable_locked - stop the active effect and power down
+ * @haptics: pointer to haptics struct
+ * Returns: 0 on success, < 0 on failure
+ */
+static int pmi8998_haptics_disable_locked(struct pmi8998_haptics *haptics)
+{
+	lockdep_assert_held(&haptics->play_lock);
+
+	cancel_delayed_work(&haptics->stop_work);
+	haptics->stop_armed = false;
+	haptics->active_effect_id = -1;
+
+	return pmi8998_haptics_hw_stop_locked(haptics);
+}
+
+/*
+ * Raised by the short-circuit detector. SC_FLAG_BIT in the status register
+ * tells whether a short was actually seen: if not, just reset the counter.
+ * Otherwise clear the flag so playback continues and permanently shut the
+ * module down when shorts keep coming within a second of each other.
+ */
+static irqreturn_t pmi8998_haptics_sc_irq_handler(int irq, void *data)
+{
+	struct pmi8998_haptics *haptics = data;
+	int err;
+	u8 val;
+	s64 sc_delta_time_us;
+	ktime_t temp;
+
+	guard(mutex)(&haptics->play_lock);
+
+	if (haptics->perm_disable)
+		return IRQ_HANDLED;
+
+	err = pmi8998_haptics_read(haptics, HAP_STATUS_1_REG, &val, 1);
+	if (err < 0)
+		return IRQ_HANDLED;
+
+	if (!(val & SC_FLAG_BIT)) {
+		haptics->sc_count = 0;
+		return IRQ_HANDLED;
+	}
+
+	temp = ktime_get();
+	sc_delta_time_us = ktime_us_delta(temp, haptics->last_sc_time);
+	haptics->last_sc_time = temp;
+
+	if (sc_delta_time_us > SC_COUNT_RST_DELAY_US)
+		haptics->sc_count = 0;
+	else
+		haptics->sc_count++;
+
+	/* Clear the interrupt flag */
+	val = SC_CLR_BIT;
+	err = pmi8998_haptics_write(haptics, HAP_SC_CLR_REG, &val, 1);
+	if (err < 0)
+		return IRQ_HANDLED;
+
+	if (haptics->sc_count > SC_MAX_COUNT) {
+		dev_crit(haptics->dev, "Short circuit persists, disabling haptics\n");
+		haptics->perm_disable = true;
+		haptics->auto_res_enabled = false;
+		haptics->hw_enabled = true;
+		err = pmi8998_haptics_disable_locked(haptics);
+		if (err < 0)
+			dev_err(haptics->dev, "Error disabling module, err=%d\n", err);
+	}
+
+	return IRQ_HANDLED;
+}
+
+/**
+ * pmi8998_haptics_hw_start_locked - configure and start the hardware block
+ * @haptics: pointer to haptics struct
+ * Returns: 0 on success, < 0 on failure
+ */
+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);
+
+	/*
+	 * Stop a running playback, the waveform source can only be changed
+	 * while the module is idle.
+	 */
+	if (!is_haptics_idle(haptics)) {
+		haptics->hw_enabled = true;
+		err = pmi8998_haptics_write_play_control(haptics, HAP_STOP);
+		if (err < 0) {
+			dev_err(haptics->dev, "Error stopping playback, err=%d\n",
+				err);
+			goto out_disable;
+		}
+
+		err = pmi8998_haptics_wait_idle(haptics);
+		if (err < 0) {
+			dev_err(haptics->dev, "Timed out waiting for haptics idle, err=%d\n",
+				err);
+			goto out_disable;
+		}
+	}
+
+	haptics->vmax = pmi8998_haptics_calc_vmax(haptics);
+	err = pmi8998_haptics_write_vmax(haptics);
+	if (err < 0) {
+		dev_err(haptics->dev, "Error setting vmax, err=%d\n", err);
+		goto out_disable;
+	}
+
+	if (haptics->act_type == HAP_TYPE_LRA) {
+		err = pmi8998_haptics_set_auto_res(haptics, false);
+		if (err < 0) {
+			dev_err(haptics->dev, "Error disabling auto_res, err=%d\n", err);
+			goto out_disable;
+		}
+	}
+
+	err = pmi8998_haptics_module_enable(haptics, true);
+	if (err < 0) {
+		dev_err(haptics->dev, "Error enabling module, err=%d\n", err);
+		goto out_disable;
+	}
+	haptics->hw_enabled = true;
+
+	err = pmi8998_haptics_start(haptics);
+	if (err < 0) {
+		dev_err(haptics->dev, "Error starting playback, err=%d\n", err);
+		goto out_disable;
+	}
+
+	err = pmi8998_haptics_write_play_control(haptics, HAP_PLAY);
+	if (err < 0) {
+		dev_err(haptics->dev, "Error enabling play, err=%d\n", err);
+		goto out_disable;
+	}
+
+	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));
+		}
+	}
+
+	return 0;
+
+out_disable:
+	haptics->auto_res_enabled = false;
+	pmi8998_haptics_hw_stop_locked(haptics);
+	return err;
+}
+
+/**
+ * pmi8998_haptics_enable_locked - start vibrating for an uploaded effect
+ * @haptics: pointer to haptics struct
+ * @effect_id: index of the effect to play
+ * Returns: 0 on success, < 0 on failure
+ */
+static int pmi8998_haptics_enable_locked(struct pmi8998_haptics *haptics,
+					 int effect_id)
+{
+	struct pmi8998_haptics_effect *eff = &haptics->effects[effect_id];
+	int err;
+
+	lockdep_assert_held(&haptics->play_lock);
+
+	if (haptics->perm_disable) {
+		dev_err(haptics->dev, "Can't play while in short circuit\n");
+		return -EBUSY;
+	}
+
+	if (eff->mode == PMI8998_HAPTICS_MODE_NONE)
+		return -EINVAL;
+
+	cancel_delayed_work(&haptics->stop_work);
+	haptics->stop_armed = false;
+
+	haptics->magnitude = eff->magnitude;
+	haptics->active_mode = eff->mode;
+	haptics->active_effect_id = effect_id;
+
+	if (!eff->magnitude || !atomic_read(&haptics->gain)) {
+		err = pmi8998_haptics_hw_stop_locked(haptics);
+		if (err < 0) {
+			haptics->active_effect_id = -1;
+			return err;
+		}
+	} else {
+		err = pmi8998_haptics_hw_start_locked(haptics);
+		if (err < 0) {
+			haptics->active_effect_id = -1;
+			return err;
+		}
+	}
+
+	if (eff->length_ms) {
+		unsigned long timeout = msecs_to_jiffies(eff->length_ms);
+
+		haptics->stop_timeline = jiffies + timeout;
+		haptics->stop_armed = true;
+		mod_delayed_work(system_percpu_wq, &haptics->stop_work, timeout);
+	}
+
+	return 0;
+}
+
+static int pmi8998_haptics_apply_gain_locked(struct pmi8998_haptics *haptics)
+{
+	u32 vmax;
+	int err;
+
+	lockdep_assert_held(&haptics->play_lock);
+
+	if (haptics->active_effect_id < 0)
+		return 0;
+
+	if (!haptics->magnitude || !atomic_read(&haptics->gain))
+		return pmi8998_haptics_hw_stop_locked(haptics);
+
+	if (!haptics->hw_enabled) {
+		if (haptics->perm_disable)
+			return -EBUSY;
+
+		err = pmi8998_haptics_hw_start_locked(haptics);
+		if (err < 0)
+			return err;
+
+		return 0;
+	}
+
+	vmax = pmi8998_haptics_calc_vmax(haptics);
+	if (vmax == haptics->vmax)
+		return 0;
+
+	haptics->vmax = vmax;
+	err = pmi8998_haptics_write_vmax(haptics);
+	if (err < 0)
+		return err;
+
+	return 0;
+}
+
+static void pmi8998_haptics_stop_work(struct work_struct *work)
+{
+	struct pmi8998_haptics *haptics = container_of(to_delayed_work(work),
+						       struct pmi8998_haptics,
+						       stop_work);
+
+	guard(mutex)(&haptics->play_lock);
+
+	if (haptics->active_effect_id >= 0 && haptics->stop_armed &&
+	    time_after_eq(jiffies, haptics->stop_timeline))
+		pmi8998_haptics_disable_locked(haptics);
+}
+
+/*
+ * Threaded function to process queued playback requests.
+ */
+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 void pmi8998_haptics_stop_all(struct pmi8998_haptics *haptics)
+{
+	pmi8998_haptics_flush_queue(haptics);
+	cancel_work_sync(&haptics->gain_work);
+	cancel_work_sync(&haptics->work);
+	cancel_delayed_work_sync(&haptics->stop_work);
+	cancel_delayed_work_sync(&haptics->auto_res_work);
+
+	guard(mutex)(&haptics->play_lock);
+	pmi8998_haptics_disable_locked(haptics);
+}
+
+/**
+ * pmi8998_haptics_close - callback for input device close
+ * @dev: input device pointer
+ *
+ * Turns off the vibrator.
+ */
+static void pmi8998_haptics_close(struct input_dev *dev)
+{
+	struct pmi8998_haptics *haptics = input_get_drvdata(dev);
+
+	pmi8998_haptics_stop_all(haptics);
+}
+
+/**
+ * pmi8998_haptics_upload_effect - upload an effect
+ * @dev: input device pointer
+ * @effect: effect to upload
+ * @old: the old effect
+ *
+ * The effect is stored by the driver, the waveform of a custom effect is
+ * converted into the format of the waveform sample register of the module.
+ *
+ * Return: 0 on success, negative errno on failure.
+ */
+static int pmi8998_haptics_upload_effect(struct input_dev *dev,
+					 struct ff_effect *effect,
+					 struct ff_effect *old)
+{
+	struct pmi8998_haptics *haptics = input_get_drvdata(dev);
+	struct pmi8998_haptics_effect *eff;
+	s16 custom_samples[HAP_WAVE_SAMP_LEN];
+	u8 wave_samp[HAP_WAVE_SAMP_LEN] = {};
+	bool wave_changed = false;
+	u16 old_length_ms;
+	int id = effect->id;
+	int err, i;
+
+	if (id < 0 || id >= HAPTICS_MAX_EFFECTS)
+		return -EINVAL;
+
+	if (effect->type == FF_PERIODIC &&
+	    effect->u.periodic.waveform == FF_CUSTOM) {
+		/* The waveform buffer of the module holds eight samples */
+		if (!effect->u.periodic.custom_len ||
+		    effect->u.periodic.custom_len > HAP_WAVE_SAMP_LEN)
+			return -EINVAL;
+
+		if (copy_from_user(custom_samples,
+				   effect->u.periodic.custom_data,
+				   effect->u.periodic.custom_len *
+				   sizeof(*custom_samples)))
+			return -EFAULT;
+
+		/*
+		 * The samples are signed 8 bit PCM, the module expects the
+		 * sign in bit 7 and the amplitude in bits 5:1.
+		 */
+		for (i = 0; i < effect->u.periodic.custom_len; i++) {
+			int sample = custom_samples[i];
+			u8 amplitude;
+
+			if (sample < S8_MIN || sample > S8_MAX)
+				return -EINVAL;
+
+			amplitude = DIV_ROUND_CLOSEST(min(abs(sample), S8_MAX) *
+						      HAP_WF_SAMP_AMP_MAX,
+						      S8_MAX);
+			wave_samp[i] = FIELD_PREP(HAP_WF_SAMP_MAX, amplitude);
+			if (sample < 0)
+				wave_samp[i] |= HAP_WF_SAMP_SIGN_BIT;
+		}
+	}
+
+	guard(mutex)(&haptics->play_lock);
+
+	eff = &haptics->effects[id];
+	old_length_ms = eff->length_ms;
+
+	switch (effect->type) {
+	case FF_RUMBLE:
+		eff->mode = haptics->mode;
+		eff->magnitude = max_t(u32, effect->u.rumble.strong_magnitude,
+				       effect->u.rumble.weak_magnitude / 2);
+		eff->wave_valid = false;
+		break;
+
+	case FF_CONSTANT:
+		eff->mode = haptics->mode;
+		eff->magnitude = mult_frac(max_t(int, effect->u.constant.level, 0),
+					   U16_MAX, 0x7FFF);
+		eff->wave_valid = false;
+		break;
+
+	case FF_PERIODIC:
+		if (effect->u.periodic.waveform == FF_SINE) {
+			eff->mode = haptics->mode;
+			eff->magnitude = mult_frac(max_t(int,
+							 effect->u.periodic.magnitude,
+							 0),
+						   U16_MAX, 0x7FFF);
+			eff->wave_valid = false;
+			break;
+		}
+
+		if (effect->u.periodic.waveform != FF_CUSTOM)
+			return -EINVAL;
+
+		wave_changed = !eff->wave_valid ||
+			       memcmp(eff->wave_samp, wave_samp,
+				      HAP_WAVE_SAMP_LEN);
+		memcpy(eff->wave_samp, wave_samp, HAP_WAVE_SAMP_LEN);
+		eff->mode = PMI8998_HAPTICS_MODE_BUFFER;
+		eff->magnitude = mult_frac(max_t(int,
+						 effect->u.periodic.magnitude,
+						 0),
+					   U16_MAX, 0x7FFF);
+		eff->wave_valid = true;
+		break;
+
+	default:
+		return -EINVAL;
+	}
+
+	eff->length_ms = effect->replay.length;
+
+	/* Apply updated parameters to a running effect */
+	if (haptics->active_effect_id == id) {
+		if (wave_changed) {
+			err = pmi8998_haptics_enable_locked(haptics, id);
+			if (err < 0)
+				return err;
+		} else {
+			haptics->magnitude = eff->magnitude;
+
+			if (eff->length_ms != old_length_ms) {
+				if (eff->length_ms) {
+					unsigned long timeout =
+						msecs_to_jiffies(eff->length_ms);
+
+					haptics->stop_timeline = jiffies + timeout;
+					haptics->stop_armed = true;
+					mod_delayed_work(system_percpu_wq,
+							 &haptics->stop_work,
+							 timeout);
+				} else {
+					cancel_delayed_work(&haptics->stop_work);
+					haptics->stop_armed = false;
+				}
+			}
+
+			err = pmi8998_haptics_apply_gain_locked(haptics);
+			if (err < 0)
+				return err;
+		}
+	}
+
+	return 0;
+}
+
+/**
+ * pmi8998_haptics_playback - play or stop an effect
+ * @dev: input device pointer
+ * @effect_id: the effect to play or stop
+ * @val: non-zero to play the effect, 0 to stop it
+ *
+ * Called with @dev->event_lock held (atomic context).
+ *
+ * Return: 0 on success, negative errno on failure.
+ */
+static int pmi8998_haptics_playback(struct input_dev *dev, int effect_id,
+				    int val)
+{
+	struct pmi8998_haptics *haptics = input_get_drvdata(dev);
+	struct pmi8998_haptics_effect *eff;
+
+	if (effect_id < 0 || effect_id >= HAPTICS_MAX_EFFECTS)
+		return -EINVAL;
+
+	scoped_guard(spinlock_irqsave, &haptics->req_lock) {
+		eff = &haptics->effects[effect_id];
+		eff->play_req = val > 0;
+		if (list_empty(&eff->play_node))
+			list_add_tail(&eff->play_node, &haptics->play_queue);
+		else
+			list_move_tail(&eff->play_node, &haptics->play_queue);
+	}
+
+	schedule_work(&haptics->work);
+
+	return 0;
+}
+
+/**
+ * pmi8998_haptics_erase - erase an effect
+ * @dev: input device pointer
+ * @effect_id: the effect to erase
+ *
+ * Return: 0 on success, negative errno on failure.
+ */
+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;
+	eff->magnitude = 0;
+	eff->length_ms = 0;
+	eff->wave_valid = false;
+
+	return 0;
+}
+
+/**
+ * pmi8998_haptics_set_gain - set the gain of the haptics
+ * @dev: input device pointer
+ * @gain: the gain, 0 to U16_MAX
+ */
+static void pmi8998_haptics_set_gain(struct input_dev *dev, u16 gain)
+{
+	struct pmi8998_haptics *haptics = input_get_drvdata(dev);
+
+	atomic_set(&haptics->gain, gain);
+	schedule_work(&haptics->gain_work);
+}
+
+/**
+ * pmi8998_haptics_gain_work - update the drive voltage
+ * @work: the work of the haptics
+ */
+static void pmi8998_haptics_gain_work(struct work_struct *work)
+{
+	struct pmi8998_haptics *haptics = container_of(work,
+							struct pmi8998_haptics,
+							gain_work);
+
+	guard(mutex)(&haptics->play_lock);
+
+	pmi8998_haptics_apply_gain_locked(haptics);
+}
+
+static void pmi8998_haptics_teardown(void *data)
+{
+	struct pmi8998_haptics *haptics = data;
+
+	pmi8998_haptics_stop_all(haptics);
+}
+
+static int pmi8998_haptics_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct pmi8998_haptics *haptics;
+	struct input_dev *input_dev;
+	struct ff_device *ff;
+	u32 act_type, vmax_uv, cal_period, auto_res_mode, play_mode;
+	u32 wave_shape, lra_high_z, wave_rep_cnt, wave_samp_rep_cnt;
+	u32 brake_pat[HAP_BRAKE_PAT_LEN], var_pct;
+	int err;
+
+	haptics = devm_kzalloc(dev, sizeof(*haptics), GFP_KERNEL);
+	if (!haptics)
+		return -ENOMEM;
+
+	haptics->dev = dev;
+	haptics->data = device_get_match_data(dev);
+	if (!haptics->data)
+		return -EINVAL;
+
+	haptics->regmap = dev_get_regmap(dev->parent, NULL);
+	if (!haptics->regmap)
+		return dev_err_probe(dev, -ENODEV, "Failed to get parent regmap\n");
+
+	err = device_property_read_u32(dev, "reg", &haptics->base);
+	if (err)
+		return dev_err_probe(dev, err, "Failed to read reg\n");
+
+	act_type = QCOM_HAPTICS_ACTUATOR_LRA;
+	err = device_property_read_u32(dev, "qcom,actuator-type", &act_type);
+	if (err && err != -EINVAL)
+		return dev_err_probe(dev, err, "Failed to read qcom,actuator-type\n");
+
+	switch (act_type) {
+	case QCOM_HAPTICS_ACTUATOR_LRA:
+		haptics->act_type = HAP_TYPE_LRA;
+		break;
+	case QCOM_HAPTICS_ACTUATOR_ERM:
+		haptics->act_type = HAP_TYPE_ERM;
+		break;
+	default:
+		return dev_err_probe(dev, -EINVAL, "Invalid qcom,actuator-type\n");
+	}
+
+	if (haptics->act_type == HAP_TYPE_LRA) {
+		err = device_property_read_u32(dev, "qcom,lra-period-us",
+					       &haptics->play_rate_us);
+		if (err)
+			return dev_err_probe(dev, err,
+					     "Failed to read qcom,lra-period-us\n");
+
+		if (haptics->play_rate_us < HAP_RATE_CFG_STEP_US ||
+		    haptics->play_rate_us > HAP_PLAY_RATE_MAX_US ||
+		    haptics->play_rate_us % HAP_RATE_CFG_STEP_US)
+			return dev_err_probe(dev, -EINVAL,
+					     "qcom,lra-period-us out of range\n");
+
+		err = device_property_read_u32(dev, "qcom,lra-auto-res-mode",
+					       &auto_res_mode);
+		if (err)
+			return dev_err_probe(dev, err,
+					     "Failed to read qcom,lra-auto-res-mode\n");
+
+		switch (auto_res_mode) {
+		case QCOM_LRA_AUTO_RES_MODE_QWD:
+		case QCOM_LRA_AUTO_RES_MODE_ZXD_EOP:
+			haptics->auto_res_mode = auto_res_mode;
+			break;
+		default:
+			return dev_err_probe(dev, -EINVAL,
+					     "Invalid qcom,lra-auto-res-mode\n");
+		}
+
+		err = device_property_read_u32(dev, "qcom,lra-res-cal-period",
+					       &cal_period);
+		if (err)
+			return dev_err_probe(dev, err,
+					     "Failed to read qcom,lra-res-cal-period\n");
+
+		if (cal_period < HAP_RES_CAL_PERIOD_MIN ||
+		    cal_period > haptics->data->res_cal_period_max ||
+		    !is_power_of_2(cal_period))
+			return dev_err_probe(dev, -EINVAL,
+					     "qcom,lra-res-cal-period out of range\n");
+
+		haptics->res_cal_period = cal_period;
+
+		err = device_property_read_u32(dev, "qcom,wave-shape", &wave_shape);
+		if (err)
+			return dev_err_probe(dev, err,
+					     "Failed to read qcom,wave-shape\n");
+
+		switch (wave_shape) {
+		case QCOM_HAPTICS_WAVE_SHAPE_SINE:
+			haptics->wave_shape = HAP_WAVE_SINE;
+			break;
+		case QCOM_HAPTICS_WAVE_SHAPE_SQUARE:
+			haptics->wave_shape = HAP_WAVE_SQUARE;
+			break;
+		default:
+			return dev_err_probe(dev, -EINVAL,
+					     "Invalid qcom,wave-shape\n");
+		}
+
+		err = device_property_read_u32(dev, "qcom,lra-high-z", &lra_high_z);
+		if (err)
+			return dev_err_probe(dev, err,
+					     "Failed to read qcom,lra-high-z\n");
+
+		switch (lra_high_z) {
+		case QCOM_HAPTICS_LRA_HIGH_Z_NONE:
+			haptics->lra_high_z = LRA_HIGH_Z_NONE;
+			break;
+		case QCOM_HAPTICS_LRA_HIGH_Z_OPT1:
+			haptics->lra_high_z = LRA_HIGH_Z_OPT1;
+			break;
+		case QCOM_HAPTICS_LRA_HIGH_Z_OPT2:
+			haptics->lra_high_z = LRA_HIGH_Z_OPT2;
+			break;
+		case QCOM_HAPTICS_LRA_HIGH_Z_OPT3:
+			haptics->lra_high_z = LRA_HIGH_Z_OPT3;
+			break;
+		default:
+			return dev_err_probe(dev, -EINVAL,
+					     "Invalid qcom,lra-high-z\n");
+		}
+
+		haptics->rate_var_pct_min = HAP_RATE_VAR_PCT_DEFAULT;
+		haptics->rate_var_pct_max = HAP_RATE_VAR_PCT_DEFAULT;
+
+		err = device_property_read_u32(dev,
+					       "qcom,drive-period-min-variation-percent",
+					       &var_pct);
+		if (!err) {
+			if (var_pct == 0 || var_pct >= 100)
+				return dev_err_probe(dev, -EINVAL,
+						     "Invalid qcom,drive-period-min-variation-percent\n");
+
+			haptics->rate_var_pct_min = var_pct;
+		} else if (err != -EINVAL) {
+			return dev_err_probe(dev, err,
+					     "Failed to read qcom,drive-period-min-variation-percent\n");
+		}
+
+		err = device_property_read_u32(dev,
+					       "qcom,drive-period-max-variation-percent",
+					       &var_pct);
+		if (!err) {
+			if (var_pct == 0 || var_pct >= 100)
+				return dev_err_probe(dev, -EINVAL,
+						     "Invalid qcom,drive-period-max-variation-percent\n");
+
+			haptics->rate_var_pct_max = var_pct;
+		} else if (err != -EINVAL) {
+			return dev_err_probe(dev, err,
+					     "Failed to read qcom,drive-period-max-variation-percent\n");
+		}
+	} else {
+		haptics->play_rate_us = HAP_PLAY_RATE_DEFAULT_US;
+		err = device_property_read_u32(dev, "qcom,play-rate-us",
+					       &haptics->play_rate_us);
+		if (err && err != -EINVAL)
+			return dev_err_probe(dev, err,
+					     "Failed to read qcom,play-rate-us\n");
+
+		if (haptics->play_rate_us < HAP_RATE_CFG_STEP_US ||
+		    haptics->play_rate_us > HAP_PLAY_RATE_MAX_US ||
+		    haptics->play_rate_us % HAP_RATE_CFG_STEP_US)
+			return dev_err_probe(dev, -EINVAL,
+					     "qcom,play-rate-us out of range\n");
+	}
+
+	err = device_property_read_u32(dev, "qcom,play-mode", &play_mode);
+	if (err)
+		return dev_err_probe(dev, err, "Failed to read qcom,play-mode\n");
+	switch (play_mode) {
+	case QCOM_HAPTICS_PLAY_MODE_DIRECT:
+		haptics->mode = PMI8998_HAPTICS_MODE_DIRECT;
+		break;
+	case QCOM_HAPTICS_PLAY_MODE_BUFFER:
+		haptics->mode = PMI8998_HAPTICS_MODE_BUFFER;
+		break;
+	default:
+		return dev_err_probe(dev, -EINVAL, "Invalid qcom,play-mode\n");
+	}
+
+	err = device_property_read_u32(dev, "qcom,vmax-microvolt", &vmax_uv);
+	if (err)
+		return dev_err_probe(dev, err, "Failed to read qcom,vmax-microvolt\n");
+
+	haptics->vmax_limit = vmax_uv / 1000;
+	if (vmax_uv % (HAP_VMAX_MIN_MV * 1000) ||
+	    haptics->vmax_limit < HAP_VMAX_MIN_MV ||
+	    haptics->vmax_limit > HAP_VMAX_MAX_MV)
+		return dev_err_probe(dev, -EINVAL, "qcom,vmax-microvolt out of range\n");
+
+	haptics->wave_rep_cnt = HAP_WAVE_REP_CNT_MIN;
+	err = device_property_read_u32(dev, "qcom,wave-rep-cnt", &wave_rep_cnt);
+	if (!err) {
+		if (wave_rep_cnt < HAP_WAVE_REP_CNT_MIN ||
+		    wave_rep_cnt > HAP_WAVE_REP_CNT_MAX ||
+		    !is_power_of_2(wave_rep_cnt))
+			return dev_err_probe(dev, -EINVAL,
+					     "qcom,wave-rep-cnt out of range\n");
+
+		haptics->wave_rep_cnt = wave_rep_cnt;
+	} else if (err != -EINVAL) {
+		return dev_err_probe(dev, err, "Failed to read qcom,wave-rep-cnt\n");
+	}
+
+	haptics->wave_samp_rep_cnt = HAP_WAVE_SAMP_REP_CNT_MIN;
+	err = device_property_read_u32(dev, "qcom,wave-samp-rep-cnt",
+				       &wave_samp_rep_cnt);
+	if (!err) {
+		if (wave_samp_rep_cnt < HAP_WAVE_SAMP_REP_CNT_MIN ||
+		    wave_samp_rep_cnt > HAP_WAVE_SAMP_REP_CNT_MAX ||
+		    !is_power_of_2(wave_samp_rep_cnt))
+			return dev_err_probe(dev, -EINVAL,
+					     "qcom,wave-samp-rep-cnt out of range\n");
+
+		haptics->wave_samp_rep_cnt = wave_samp_rep_cnt;
+	} else if (err != -EINVAL) {
+		return dev_err_probe(dev, err,
+				     "Failed to read qcom,wave-samp-rep-cnt\n");
+	}
+
+	/*
+	 * The brake is enabled by the presence of the brake pattern, the
+	 * vendor driver enables it with a separate property.
+	 */
+	err = device_property_read_u32_array(dev, "qcom,brake-pattern",
+					     brake_pat, HAP_BRAKE_PAT_LEN);
+	if (!err) {
+		for (int i = 0; i < HAP_BRAKE_PAT_LEN; i++) {
+			if (brake_pat[i] > HAP_BRAKE_PAT_MASK)
+				return dev_err_probe(dev, -EINVAL,
+						     "Invalid qcom,brake-pattern\n");
+
+			haptics->brake_pat[i] = brake_pat[i];
+		}
+
+		haptics->brake_en = true;
+	} else if (err != -EINVAL) {
+		return dev_err_probe(dev, err, "Failed to read qcom,brake-pattern\n");
+	}
+
+	haptics->sc_irq = platform_get_irq_byname(pdev, "short");
+	if (haptics->sc_irq < 0)
+		return dev_err_probe(dev, haptics->sc_irq,
+				     "Failed to get short-circuit IRQ\n");
+
+	haptics->vmax = haptics->vmax_limit;
+	for (int i = 0; i < HAP_WAVE_SAMP_LEN; i++)
+		haptics->wave_samp[i] = HAP_WF_SAMP_MAX;
+
+	atomic_set(&haptics->gain, U16_MAX);
+	haptics->active_effect_id = -1;
+
+	err = devm_mutex_init(dev, &haptics->play_lock);
+	if (err)
+		return err;
+
+	spin_lock_init(&haptics->req_lock);
+	INIT_LIST_HEAD(&haptics->play_queue);
+	for (int i = 0; i < HAPTICS_MAX_EFFECTS; i++)
+		INIT_LIST_HEAD(&haptics->effects[i].play_node);
+
+	INIT_WORK(&haptics->work, pmi8998_haptics_work);
+	INIT_WORK(&haptics->gain_work, pmi8998_haptics_gain_work);
+	INIT_DELAYED_WORK(&haptics->auto_res_work,
+			  pmi8998_haptics_auto_res_work);
+	INIT_DELAYED_WORK(&haptics->stop_work, pmi8998_haptics_stop_work);
+	platform_set_drvdata(pdev, haptics);
+
+	err = pmi8998_haptics_init(haptics);
+	if (err < 0)
+		return dev_err_probe(dev, err, "Failed to initialise haptics\n");
+
+	err = devm_add_action_or_reset(dev, pmi8998_haptics_teardown, haptics);
+	if (err)
+		return err;
+
+	err = devm_request_threaded_irq(dev, haptics->sc_irq, NULL,
+					pmi8998_haptics_sc_irq_handler,
+					IRQF_ONESHOT, "haptics_sc_irq", haptics);
+	if (err)
+		return dev_err_probe(dev, err, "Failed to request short-circuit IRQ\n");
+
+	input_dev = devm_input_allocate_device(dev);
+	if (!input_dev)
+		return -ENOMEM;
+
+	input_dev->name = "pmi8998_haptics";
+	input_dev->id.bustype = BUS_HOST;
+	input_dev->close = pmi8998_haptics_close;
+	input_set_drvdata(input_dev, haptics);
+	input_set_capability(input_dev, EV_FF, FF_RUMBLE);
+	input_set_capability(input_dev, EV_FF, FF_CONSTANT);
+	input_set_capability(input_dev, EV_FF, FF_PERIODIC);
+	input_set_capability(input_dev, EV_FF, FF_SINE);
+	input_set_capability(input_dev, EV_FF, FF_CUSTOM);
+	input_set_capability(input_dev, EV_FF, FF_GAIN);
+
+	err = input_ff_create(input_dev, HAPTICS_MAX_EFFECTS);
+	if (err)
+		return dev_err_probe(dev, err, "Failed to create FF device\n");
+
+	ff = input_dev->ff;
+	ff->upload	= pmi8998_haptics_upload_effect;
+	ff->playback	= pmi8998_haptics_playback;
+	ff->erase	= pmi8998_haptics_erase;
+	ff->set_gain	= pmi8998_haptics_set_gain;
+
+	err = input_register_device(input_dev);
+	if (err)
+		return dev_err_probe(dev, err, "Failed to register input device\n");
+
+	return 0;
+}
+
+static int pmi8998_haptics_suspend(struct device *dev)
+{
+	struct pmi8998_haptics *haptics = dev_get_drvdata(dev);
+	int err;
+
+	disable_work_sync(&haptics->gain_work);
+	disable_work_sync(&haptics->work);
+	disable_delayed_work_sync(&haptics->stop_work);
+	disable_delayed_work_sync(&haptics->auto_res_work);
+
+	pmi8998_haptics_flush_queue(haptics);
+
+	scoped_guard(mutex, &haptics->play_lock)
+		err = pmi8998_haptics_disable_locked(haptics);
+	if (err)
+		dev_err(dev, "Failed to stop haptics for suspend: %d\n", err);
+
+	return 0;
+}
+
+static int pmi8998_haptics_resume(struct device *dev)
+{
+	struct pmi8998_haptics *haptics = dev_get_drvdata(dev);
+
+	enable_delayed_work(&haptics->auto_res_work);
+	enable_delayed_work(&haptics->stop_work);
+	enable_work(&haptics->work);
+	enable_work(&haptics->gain_work);
+
+	return 0;
+}
+
+static DEFINE_SIMPLE_DEV_PM_OPS(pmi8998_haptics_pm_ops,
+				pmi8998_haptics_suspend,
+				pmi8998_haptics_resume);
+
+static void pmi8998_haptics_shutdown(struct platform_device *pdev)
+{
+	struct pmi8998_haptics *haptics = platform_get_drvdata(pdev);
+
+	pmi8998_haptics_stop_all(haptics);
+}
+
+static const struct pmi8998_haptics_data pmi8998_haptics_data = {
+	.auto_res_en_reg = HAP_TEST2_REG,
+	.res_cal_period_max = HAP_RES_CAL_PERIOD_MAX,
+	.config_auto_res = pmi8998_haptics_config_auto_res,
+};
+
+static const struct pmi8998_haptics_data pm660_haptics_data = {
+	.auto_res_en_reg = HAP_AUTO_RES_CTRL_REG,
+	.res_cal_period_max = PM660_RES_CAL_PERIOD_MAX,
+	.hw_auto_res = true,
+	.init_chip = pm660_haptics_init_chip,
+	.config_auto_res = pm660_haptics_config_auto_res,
+};
+
+static const struct of_device_id pmi8998_haptics_match_table[] = {
+	{ .compatible = "qcom,pmi8998-haptics", .data = &pmi8998_haptics_data },
+	{ .compatible = "qcom,pm660-haptics", .data = &pm660_haptics_data },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, pmi8998_haptics_match_table);
+
+static struct platform_driver pmi8998_haptics_driver = {
+	.probe		= pmi8998_haptics_probe,
+	.shutdown	= pmi8998_haptics_shutdown,
+	.driver		= {
+		.name	= "qcom-pmi8998-haptics",
+		.pm	= pm_sleep_ptr(&pmi8998_haptics_pm_ops),
+		.of_match_table = pmi8998_haptics_match_table,
+	},
+};
+module_platform_driver(pmi8998_haptics_driver);
+
+MODULE_DESCRIPTION("Qualcomm PMI8998 haptics driver");
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Casey Connolly <casey@connolly.tech>");
+MODULE_AUTHOR("David Heidelberg <david@ixit.cz>");

-- 
2.55.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v8 4/9] arm64: dts: qcom: pmi8998: Add haptics node
  2026-11-30  0:13 [PATCH v8 0/9] input: Add support for the Qualcomm PMI8998 and PM660 haptics David Heidelberg via B4 Relay
                   ` (2 preceding siblings ...)
  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:13 ` 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
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 12+ messages in thread
From: David Heidelberg via B4 Relay @ 2026-11-30  0:13 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Lee Jones, Stephen Boyd, Bjorn Andersson, Konrad Dybcio,
	Abel Vesa, David Heidelberg
  Cc: David Collins, Subbaraman Narayanamurthy, Kamal Wadhwa,
	Fenglin Wu, Casey Connolly, linux-arm-msm, linux-input,
	devicetree, linux-kernel, mfd, phone-devel

From: David Heidelberg <david@ixit.cz>

Add the haptics module of the PMI8998 PMIC. It stays disabled until a
board configures its actuator.

Co-developed-by: Casey Connolly <casey@connolly.tech>
Signed-off-by: Casey Connolly <casey@connolly.tech>
Signed-off-by: David Heidelberg <david@ixit.cz>
---
 arch/arm64/boot/dts/qcom/pmi8998.dtsi | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/arch/arm64/boot/dts/qcom/pmi8998.dtsi b/arch/arm64/boot/dts/qcom/pmi8998.dtsi
index cd3f0790fd420..5489602843119 100644
--- a/arch/arm64/boot/dts/qcom/pmi8998.dtsi
+++ b/arch/arm64/boot/dts/qcom/pmi8998.dtsi
@@ -73,16 +73,27 @@ pmi8998_lpg: pwm {
 
 			#address-cells = <1>;
 			#size-cells = <0>;
 			#pwm-cells = <2>;
 
 			status = "disabled";
 		};
 
+		pmi8998_haptics: haptics@c000 {
+			compatible = "qcom,pmi8998-haptics";
+			reg = <0xc000>;
+
+			interrupts = <0x3 0xc0 0x0 IRQ_TYPE_EDGE_BOTH>,
+				     <0x3 0xc0 0x1 IRQ_TYPE_EDGE_BOTH>;
+			interrupt-names = "short", "play";
+
+			status = "disabled";
+		};
+
 		pmi8998_flash: led-controller@d300 {
 			compatible = "qcom,pmi8998-flash-led", "qcom,spmi-flash-led";
 			reg = <0xd300>;
 			status = "disabled";
 		};
 
 		pmi8998_wled: leds@d800 {
 			compatible = "qcom,pmi8998-wled";

-- 
2.55.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v8 5/9] arm64: dts: qcom: sdm845-oneplus: Enable haptics
  2026-11-30  0:13 [PATCH v8 0/9] input: Add support for the Qualcomm PMI8998 and PM660 haptics David Heidelberg via B4 Relay
                   ` (3 preceding siblings ...)
  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 ` 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
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 12+ messages in thread
From: David Heidelberg via B4 Relay @ 2026-11-30  0:13 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Lee Jones, Stephen Boyd, Bjorn Andersson, Konrad Dybcio,
	Abel Vesa, David Heidelberg
  Cc: David Collins, Subbaraman Narayanamurthy, Kamal Wadhwa,
	Fenglin Wu, Casey Connolly, linux-arm-msm, linux-input,
	devicetree, linux-kernel, mfd, phone-devel

From: David Heidelberg <david@ixit.cz>

The OnePlus 6 and 6T use an LRA resonating at 235 Hz, a 4255 us period,
driven with up to 2088 mV as in the vendor device tree.

Signed-off-by: David Heidelberg <david@ixit.cz>
---
 arch/arm64/boot/dts/qcom/sdm845-oneplus-common.dtsi | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/arch/arm64/boot/dts/qcom/sdm845-oneplus-common.dtsi b/arch/arm64/boot/dts/qcom/sdm845-oneplus-common.dtsi
index 1df09da3c4d71..1aa1b754de3a4 100644
--- a/arch/arm64/boot/dts/qcom/sdm845-oneplus-common.dtsi
+++ b/arch/arm64/boot/dts/qcom/sdm845-oneplus-common.dtsi
@@ -4,16 +4,17 @@
  *
  * Copyright (c) 2020, The Linux Foundation. All rights reserved.
  */
 
 /dts-v1/;
 
 #include <dt-bindings/arm/qcom,ids.h>
 #include <dt-bindings/input/linux-event-codes.h>
+#include <dt-bindings/input/qcom,haptics.h>
 #include <dt-bindings/leds/common.h>
 #include <dt-bindings/regulator/qcom,rpmh-regulator.h>
 #include <dt-bindings/sound/qcom,q6afe.h>
 #include <dt-bindings/sound/qcom,q6asm.h>
 
 #include "sdm845.dtsi"
 #include "sdm845-wcd9340.dtsi"
 #include "pm8998.dtsi"
@@ -660,16 +661,31 @@ led-1 {
 		color = <LED_COLOR_ID_YELLOW>;
 		led-sources = <2>;
 		led-max-microamp = <500000>;
 		flash-max-microamp = <1500000>;
 		flash-max-timeout-us = <1280000>;
 	};
 };
 
+&pmi8998_haptics {
+	qcom,lra-period-us = <4255>;
+	qcom,vmax-microvolt = <2088000>;
+	qcom,lra-auto-res-mode = <QCOM_LRA_AUTO_RES_MODE_ZXD_EOP>;
+	qcom,lra-res-cal-period = <32>;
+	qcom,play-mode = <QCOM_HAPTICS_PLAY_MODE_BUFFER>;
+	qcom,wave-shape = <QCOM_HAPTICS_WAVE_SHAPE_SINE>;
+	qcom,lra-high-z = <QCOM_HAPTICS_LRA_HIGH_Z_OPT1>;
+	qcom,brake-pattern = <0x3 0x3 0x3 0x3>;
+	qcom,drive-period-min-variation-percent = <5>;
+	qcom,drive-period-max-variation-percent = <5>;
+
+	status = "okay";
+};
+
 &q6afedai {
 	dai@22 {
 		reg = <QUATERNARY_MI2S_RX>;
 		qcom,sd-lines = <1>;
 	};
 
 	dai@23 {
 		reg = <QUATERNARY_MI2S_TX>;

-- 
2.55.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v8 6/9] arm64: dts: qcom: sdm845-xiaomi-beryllium: Enable haptics
  2026-11-30  0:13 [PATCH v8 0/9] input: Add support for the Qualcomm PMI8998 and PM660 haptics David Heidelberg via B4 Relay
                   ` (4 preceding siblings ...)
  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 ` 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
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 12+ messages in thread
From: David Heidelberg via B4 Relay @ 2026-11-30  0:13 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Lee Jones, Stephen Boyd, Bjorn Andersson, Konrad Dybcio,
	Abel Vesa, David Heidelberg
  Cc: David Collins, Subbaraman Narayanamurthy, Kamal Wadhwa,
	Fenglin Wu, Casey Connolly, linux-arm-msm, linux-input,
	devicetree, linux-kernel, mfd, phone-devel

From: David Heidelberg <david@ixit.cz>

The Poco F1 uses an LRA resonating at 205 Hz, a 4875 us period rounded
to the 5 us step of the RATE_CFG registers, driven with up to 2784 mV,
the vendor's 2812 mV rounded down to the 116 mV step of VMAX.

Signed-off-by: David Heidelberg <david@ixit.cz>
---
 .../arm64/boot/dts/qcom/sdm845-xiaomi-beryllium-common.dtsi | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/arch/arm64/boot/dts/qcom/sdm845-xiaomi-beryllium-common.dtsi b/arch/arm64/boot/dts/qcom/sdm845-xiaomi-beryllium-common.dtsi
index d57851b0b9419..0b9940027caf7 100644
--- a/arch/arm64/boot/dts/qcom/sdm845-xiaomi-beryllium-common.dtsi
+++ b/arch/arm64/boot/dts/qcom/sdm845-xiaomi-beryllium-common.dtsi
@@ -1,13 +1,14 @@
 // SPDX-License-Identifier: GPL-2.0
 
 /dts-v1/;
 
 #include <dt-bindings/arm/qcom,ids.h>
+#include <dt-bindings/input/qcom,haptics.h>
 #include <dt-bindings/leds/common.h>
 #include <dt-bindings/pinctrl/qcom,pmic-gpio.h>
 #include <dt-bindings/regulator/qcom,rpmh-regulator.h>
 #include <dt-bindings/sound/qcom,q6afe.h>
 #include <dt-bindings/sound/qcom,q6asm.h>
 #include "sdm845.dtsi"
 #include "sdm845-wcd9340.dtsi"
 #include "pm8998.dtsi"
@@ -413,16 +414,28 @@ led-1 {
 		color = <LED_COLOR_ID_YELLOW>;
 		led-sources = <2>;
 		led-max-microamp = <500000>;
 		flash-max-microamp = <1500000>;
 		flash-max-timeout-us = <1280000>;
 	};
 };
 
+&pmi8998_haptics {
+	qcom,lra-period-us = <4875>;
+	qcom,vmax-microvolt = <2784000>;
+	qcom,lra-auto-res-mode = <QCOM_LRA_AUTO_RES_MODE_QWD>;
+	qcom,lra-res-cal-period = <4>;
+	qcom,play-mode = <QCOM_HAPTICS_PLAY_MODE_BUFFER>;
+	qcom,wave-shape = <QCOM_HAPTICS_WAVE_SHAPE_SINE>;
+	qcom,lra-high-z = <QCOM_HAPTICS_LRA_HIGH_Z_OPT1>;
+
+	status = "okay";
+};
+
 &pm8998_resin {
 	linux,code = <KEY_VOLUMEDOWN>;
 	status = "okay";
 };
 
 /* QUAT I2S Uses 1 I2S SD Line for audio on TAS2559/60 amplifiers */
 &q6afedai {
 	dai@22 {

-- 
2.55.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v8 7/9] arm64: dts: qcom: sdm845-shift-axolotl: Enable haptics
  2026-11-30  0:13 [PATCH v8 0/9] input: Add support for the Qualcomm PMI8998 and PM660 haptics David Heidelberg via B4 Relay
                   ` (5 preceding siblings ...)
  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 ` 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
  8 siblings, 0 replies; 12+ messages in thread
From: David Heidelberg via B4 Relay @ 2026-11-30  0:13 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Lee Jones, Stephen Boyd, Bjorn Andersson, Konrad Dybcio,
	Abel Vesa, David Heidelberg
  Cc: David Collins, Subbaraman Narayanamurthy, Kamal Wadhwa,
	Fenglin Wu, Casey Connolly, linux-arm-msm, linux-input,
	devicetree, linux-kernel, mfd, phone-devel

From: David Heidelberg <david@ixit.cz>

The SHIFT6mq inherits the haptics configuration of the QRD, it uses an
LRA resonating at 230 Hz, a 4345 us period (4347 us rounded down to the
5 us step of the RATE_CFG registers), driven with up to 1392 mV (the
vendor's 1400 mV rounded down to the 116 mV step of VMAX).

The vendor driver uses the quarter wave detection with a calibration
period of 4 cycles and the direct mode. It drives the LRA with a square
wave, the QRD does not override the default of the vendor driver.

Signed-off-by: David Heidelberg <david@ixit.cz>
---
 arch/arm64/boot/dts/qcom/sdm845-shift-axolotl.dts | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/arch/arm64/boot/dts/qcom/sdm845-shift-axolotl.dts b/arch/arm64/boot/dts/qcom/sdm845-shift-axolotl.dts
index 1b0eaeafbd517..c15b01a5767d0 100644
--- a/arch/arm64/boot/dts/qcom/sdm845-shift-axolotl.dts
+++ b/arch/arm64/boot/dts/qcom/sdm845-shift-axolotl.dts
@@ -4,16 +4,17 @@
  * Copyright (c) 2022, Casey Connolly <casey.connolly@linaro.org>
  * Copyright (c) 2022, Dylan Van Assche <me@dylanvanassche.be>
  */
 
 /dts-v1/;
 
 #include <dt-bindings/arm/qcom,ids.h>
 #include <dt-bindings/gpio/gpio.h>
+#include <dt-bindings/input/qcom,haptics.h>
 #include <dt-bindings/leds/common.h>
 #include <dt-bindings/regulator/qcom,rpmh-regulator.h>
 #include "sdm845.dtsi"
 #include "pm8998.dtsi"
 #include "pmi8998.dtsi"
 
 / {
 	model = "SHIFT SHIFT6mq";
@@ -600,16 +601,28 @@ pinconf {
 };
 
 &pmi8998_charger {
 	monitored-battery = <&battery>;
 
 	status = "okay";
 };
 
+&pmi8998_haptics {
+	qcom,lra-period-us = <4345>;
+	qcom,vmax-microvolt = <1392000>;
+	qcom,lra-auto-res-mode = <QCOM_LRA_AUTO_RES_MODE_QWD>;
+	qcom,lra-res-cal-period = <4>;
+	qcom,play-mode = <QCOM_HAPTICS_PLAY_MODE_DIRECT>;
+	qcom,wave-shape = <QCOM_HAPTICS_WAVE_SHAPE_SQUARE>;
+	qcom,lra-high-z = <QCOM_HAPTICS_LRA_HIGH_Z_OPT1>;
+
+	status = "okay";
+};
+
 &pm8998_resin {
 	linux,code = <KEY_VOLUMEDOWN>;
 	status = "okay";
 };
 
 &pmi8998_lpg {
 	status = "okay";
 

-- 
2.55.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v8 8/9] arm64: dts: qcom: pm660: Add haptics node
  2026-11-30  0:13 [PATCH v8 0/9] input: Add support for the Qualcomm PMI8998 and PM660 haptics David Heidelberg via B4 Relay
                   ` (6 preceding siblings ...)
  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 ` 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
  8 siblings, 0 replies; 12+ messages in thread
From: David Heidelberg via B4 Relay @ 2026-11-30  0:13 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Lee Jones, Stephen Boyd, Bjorn Andersson, Konrad Dybcio,
	Abel Vesa, David Heidelberg
  Cc: David Collins, Subbaraman Narayanamurthy, Kamal Wadhwa,
	Fenglin Wu, Casey Connolly, linux-arm-msm, linux-input,
	devicetree, linux-kernel, mfd, phone-devel

From: David Heidelberg <david@ixit.cz>

Add the haptics module of the PM660 PMIC at 0xc000 on USID 1. It stays
disabled until a board configures its actuator.

Signed-off-by: David Heidelberg <david@ixit.cz>
---
 arch/arm64/boot/dts/qcom/pm660.dtsi | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/arch/arm64/boot/dts/qcom/pm660.dtsi b/arch/arm64/boot/dts/qcom/pm660.dtsi
index 156b2ddff0dcb..b1fcf39232267 100644
--- a/arch/arm64/boot/dts/qcom/pm660.dtsi
+++ b/arch/arm64/boot/dts/qcom/pm660.dtsi
@@ -220,10 +220,21 @@ pmic@1 {
 		compatible = "qcom,pm660", "qcom,spmi-pmic";
 		reg = <0x1 SPMI_USID>;
 		#address-cells = <1>;
 		#size-cells = <0>;
 
 		pm660_spmi_regulators: regulators {
 			compatible = "qcom,pm660-regulators";
 		};
+
+		pm660_haptics: haptics@c000 {
+			compatible = "qcom,pm660-haptics";
+			reg = <0xc000>;
+
+			interrupts = <0x1 0xc0 0x0 IRQ_TYPE_EDGE_BOTH>,
+				     <0x1 0xc0 0x1 IRQ_TYPE_EDGE_BOTH>;
+			interrupt-names = "short", "play";
+
+			status = "disabled";
+		};
 	};
 };

-- 
2.55.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v8 9/9] arm64: dts: qcom: sdm660-xiaomi-lavender: Enable haptics
  2026-11-30  0:13 [PATCH v8 0/9] input: Add support for the Qualcomm PMI8998 and PM660 haptics David Heidelberg via B4 Relay
                   ` (7 preceding siblings ...)
  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 ` David Heidelberg via B4 Relay
  8 siblings, 0 replies; 12+ messages in thread
From: David Heidelberg via B4 Relay @ 2026-11-30  0:13 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Lee Jones, Stephen Boyd, Bjorn Andersson, Konrad Dybcio,
	Abel Vesa, David Heidelberg
  Cc: David Collins, Subbaraman Narayanamurthy, Kamal Wadhwa,
	Fenglin Wu, Casey Connolly, linux-arm-msm, linux-input,
	devicetree, linux-kernel, mfd, phone-devel

From: David Heidelberg <david@ixit.cz>

The Xiaomi Redmi Note 7 (lavender) uses an ERM vibrator driven by the
PM660 haptics module with up to 3132 mV (the vendor's 3200 mV rounded
down to the 116 mV step of VMAX) in direct mode and a 6665 us sample play
rate (the vendor's 6667 us rounded down to the 5 us step of RATE_CFG).

The downstream lavender-base.dtsi also sets qcom,lra-resonance-sig-shape,
which the vendor qti-haptics driver ignores for ERM actuators.

Signed-off-by: David Heidelberg <david@ixit.cz>
---
 arch/arm64/boot/dts/qcom/sdm660-xiaomi-lavender.dts | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/arch/arm64/boot/dts/qcom/sdm660-xiaomi-lavender.dts b/arch/arm64/boot/dts/qcom/sdm660-xiaomi-lavender.dts
index a9926ad6c6f9f..81d1289c98642 100644
--- a/arch/arm64/boot/dts/qcom/sdm660-xiaomi-lavender.dts
+++ b/arch/arm64/boot/dts/qcom/sdm660-xiaomi-lavender.dts
@@ -6,16 +6,17 @@
 
 /dts-v1/;
 
 #include "sdm660.dtsi"
 #include "pm660.dtsi"
 #include "pm660l.dtsi"
 #include <dt-bindings/input/input.h>
 #include <dt-bindings/input/gpio-keys.h>
+#include <dt-bindings/input/qcom,haptics.h>
 
 / {
 	model = "Xiaomi Redmi Note 7";
 	compatible = "xiaomi,lavender", "qcom,sdm660";
 	chassis-type = "handset";
 
 	aliases {
 		serial0 = &blsp1_uart2;
@@ -88,16 +89,26 @@ extcon_usb: extcon-usb {
 		id-gpios = <&tlmm 58 GPIO_ACTIVE_HIGH>;
 	};
 };
 
 &blsp1_uart2 {
 	status = "okay";
 };
 
+&pm660_haptics {
+	qcom,actuator-type = <QCOM_HAPTICS_ACTUATOR_ERM>;
+	qcom,vmax-microvolt = <3132000>;
+	qcom,play-mode = <QCOM_HAPTICS_PLAY_MODE_DIRECT>;
+	qcom,play-rate-us = <6665>;
+	qcom,brake-pattern = <0x3 0x3 0x0 0x0>;
+
+	status = "okay";
+};
+
 &pon_pwrkey {
 	status = "okay";
 };
 
 &pon_resin {
 	status = "okay";
 
 	linux,code = <KEY_VOLUMEDOWN>;

-- 
2.55.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH v8 1/9] dt-bindings: input: Add Qualcomm PMI8998 / PM660 haptics
  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
  0 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-11-30  0:22 UTC (permalink / raw)
  To: David Heidelberg
  Cc: linux-input, devicetree, mfd, robh, conor+dt, dmitry.torokhov,
	lee

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] dt-bindings: input: qcom,pmi8998-haptics: examples fail
  dt_binding_check against qcom,spmi-pmic.yaml
- [Low] dt-bindings: input: qcom,pmi8998-haptics: license is
  GPL-2.0-only instead of dual license
--

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

dt-bindings: input: Add Qualcomm PMI8998 / PM660 haptics

This commit adds the devicetree binding schema and constants header for the
Qualcomm PMI8998 and PM660 PMIC haptics module.

> diff --git a/Documentation/devicetree/bindings/input/qcom,pmi8998-haptics.yaml b/Documentation/devicetree/bindings/input/qcom,pmi8998-haptics.yaml
> new file mode 100644
> index 0000000000000..2a296b6096e02
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/input/qcom,pmi8998-haptics.yaml
> @@ -0,0 +1,256 @@
> +# SPDX-License-Identifier: GPL-2.0-only

[Severity: Low]
Should this new binding schema be dual-licensed as (GPL-2.0-only OR
BSD-2-Clause), matching include/dt-bindings/input/qcom,haptics.h?

[ ... ]

> +examples:
> +  - |
> +    #include <dt-bindings/input/qcom,haptics.h>
> +    #include <dt-bindings/interrupt-controller/irq.h>
> +    #include <dt-bindings/spmi/spmi.h>
> +
> +    pmic@3 {
> +      compatible = "qcom,pmi8998", "qcom,spmi-pmic";
> +      reg = <0x3 SPMI_USID>;
> +      #address-cells = <1>;
> +      #size-cells = <0>;
> +
> +      haptics@c000 {
> +        compatible = "qcom,pmi8998-haptics";
> +        reg = <0xc000>;
> +        interrupts = <0x3 0xc0 0x0 IRQ_TYPE_EDGE_BOTH>,
> +                     <0x3 0xc0 0x1 IRQ_TYPE_EDGE_BOTH>;
> +        interrupt-names = "short", "play";
> +
> +        qcom,lra-auto-res-mode = <QCOM_LRA_AUTO_RES_MODE_ZXD_EOP>;
> +        qcom,lra-high-z = <QCOM_HAPTICS_LRA_HIGH_Z_OPT1>;
> +        qcom,lra-period-us = <4255>;
> +        qcom,lra-res-cal-period = <32>;
> +        qcom,play-mode = <QCOM_HAPTICS_PLAY_MODE_BUFFER>;
> +        qcom,vmax-microvolt = <2088000>;
> +        qcom,wave-shape = <QCOM_HAPTICS_WAVE_SHAPE_SINE>;
> +      };
> +    };
> +
> +  - |
> +    #include <dt-bindings/input/qcom,haptics.h>
> +    #include <dt-bindings/interrupt-controller/irq.h>
> +    #include <dt-bindings/spmi/spmi.h>
> +
> +    pmic@1 {
> +      compatible = "qcom,pm660";
> +      reg = <0x1 SPMI_USID>;
> +      #address-cells = <1>;
> +      #size-cells = <0>;
> +
> +      haptics@c000 {
> +        compatible = "qcom,pm660-haptics";

[Severity: Medium]
Do these examples pass make dt_binding_check?

In the second example, pmic@1 matches
Documentation/devicetree/bindings/mfd/qcom,spmi-pmic.yaml via "qcom,pm660",
which requires the "qcom,spmi-pmic" fallback compatible.

Also, at this point in the series, qcom,spmi-pmic.yaml only allows
/schemas/input/qcom,spmi-haptics.yaml# for "^haptics@[0-9a-f]+$" child
nodes until commit "dt-bindings: mfd: qcom,spmi-pmic: Allow the PMI8998
haptics child node", so both examples fail dt_binding_check on this commit
unless the parent MFD schema update is ordered first or untyped pmic
wrapper nodes are used.

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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v8 3/9] Input: qcom-pmi8998-haptics - Add Qualcomm PMI8998 haptics driver
  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
  0 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-11-30  0:26 UTC (permalink / raw)
  To: David Heidelberg
  Cc: linux-input, devicetree, mfd, robh, conor+dt, dmitry.torokhov,
	lee

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

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2026-11-30  0:26 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox