* [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 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