xref: /linux/drivers/input/misc/da7280.c (revision cd3f609823a5896a6f4c229b3c2077475531e23d)
1*cd3f6098SRoy Im // SPDX-License-Identifier: GPL-2.0+
2*cd3f6098SRoy Im /*
3*cd3f6098SRoy Im  * DA7280 Haptic device driver
4*cd3f6098SRoy Im  *
5*cd3f6098SRoy Im  * Copyright (c) 2020 Dialog Semiconductor.
6*cd3f6098SRoy Im  * Author: Roy Im <Roy.Im.Opensource@diasemi.com>
7*cd3f6098SRoy Im  */
8*cd3f6098SRoy Im 
9*cd3f6098SRoy Im #include <linux/bitfield.h>
10*cd3f6098SRoy Im #include <linux/bitops.h>
11*cd3f6098SRoy Im #include <linux/err.h>
12*cd3f6098SRoy Im #include <linux/i2c.h>
13*cd3f6098SRoy Im #include <linux/input.h>
14*cd3f6098SRoy Im #include <linux/interrupt.h>
15*cd3f6098SRoy Im #include <linux/module.h>
16*cd3f6098SRoy Im #include <linux/pwm.h>
17*cd3f6098SRoy Im #include <linux/regmap.h>
18*cd3f6098SRoy Im #include <linux/workqueue.h>
19*cd3f6098SRoy Im #include <linux/uaccess.h>
20*cd3f6098SRoy Im 
21*cd3f6098SRoy Im /* Registers */
22*cd3f6098SRoy Im #define DA7280_IRQ_EVENT1			0x03
23*cd3f6098SRoy Im #define DA7280_IRQ_EVENT_WARNING_DIAG		0x04
24*cd3f6098SRoy Im #define DA7280_IRQ_EVENT_SEQ_DIAG		0x05
25*cd3f6098SRoy Im #define DA7280_IRQ_STATUS1			0x06
26*cd3f6098SRoy Im #define DA7280_IRQ_MASK1			0x07
27*cd3f6098SRoy Im #define DA7280_FRQ_LRA_PER_H			0x0A
28*cd3f6098SRoy Im #define DA7280_FRQ_LRA_PER_L			0x0B
29*cd3f6098SRoy Im #define DA7280_ACTUATOR1			0x0C
30*cd3f6098SRoy Im #define DA7280_ACTUATOR2			0x0D
31*cd3f6098SRoy Im #define DA7280_ACTUATOR3			0x0E
32*cd3f6098SRoy Im #define DA7280_CALIB_V2I_H			0x0F
33*cd3f6098SRoy Im #define DA7280_CALIB_V2I_L			0x10
34*cd3f6098SRoy Im #define DA7280_TOP_CFG1				0x13
35*cd3f6098SRoy Im #define DA7280_TOP_CFG2				0x14
36*cd3f6098SRoy Im #define DA7280_TOP_CFG4				0x16
37*cd3f6098SRoy Im #define DA7280_TOP_INT_CFG1			0x17
38*cd3f6098SRoy Im #define DA7280_TOP_CTL1				0x22
39*cd3f6098SRoy Im #define DA7280_TOP_CTL2				0x23
40*cd3f6098SRoy Im #define DA7280_SEQ_CTL2				0x28
41*cd3f6098SRoy Im #define DA7280_GPI_0_CTL			0x29
42*cd3f6098SRoy Im #define DA7280_GPI_1_CTL			0x2A
43*cd3f6098SRoy Im #define DA7280_GPI_2_CTL			0x2B
44*cd3f6098SRoy Im #define DA7280_MEM_CTL1				0x2C
45*cd3f6098SRoy Im #define DA7280_MEM_CTL2				0x2D
46*cd3f6098SRoy Im #define DA7280_TOP_CFG5				0x6E
47*cd3f6098SRoy Im #define DA7280_IRQ_MASK2			0x83
48*cd3f6098SRoy Im #define DA7280_SNP_MEM_99			0xE7
49*cd3f6098SRoy Im 
50*cd3f6098SRoy Im /* Register field */
51*cd3f6098SRoy Im 
52*cd3f6098SRoy Im /* DA7280_IRQ_EVENT1 (Address 0x03) */
53*cd3f6098SRoy Im #define DA7280_E_SEQ_CONTINUE_MASK		BIT(0)
54*cd3f6098SRoy Im #define DA7280_E_UVLO_MASK			BIT(1)
55*cd3f6098SRoy Im #define DA7280_E_SEQ_DONE_MASK			BIT(2)
56*cd3f6098SRoy Im #define DA7280_E_OVERTEMP_CRIT_MASK		BIT(3)
57*cd3f6098SRoy Im #define DA7280_E_SEQ_FAULT_MASK			BIT(4)
58*cd3f6098SRoy Im #define DA7280_E_WARNING_MASK			BIT(5)
59*cd3f6098SRoy Im #define DA7280_E_ACTUATOR_FAULT_MASK		BIT(6)
60*cd3f6098SRoy Im #define DA7280_E_OC_FAULT_MASK			BIT(7)
61*cd3f6098SRoy Im 
62*cd3f6098SRoy Im /* DA7280_IRQ_EVENT_WARNING_DIAG (Address 0x04) */
63*cd3f6098SRoy Im #define DA7280_E_OVERTEMP_WARN_MASK             BIT(3)
64*cd3f6098SRoy Im #define DA7280_E_MEM_TYPE_MASK                  BIT(4)
65*cd3f6098SRoy Im #define DA7280_E_LIM_DRIVE_ACC_MASK             BIT(6)
66*cd3f6098SRoy Im #define DA7280_E_LIM_DRIVE_MASK                 BIT(7)
67*cd3f6098SRoy Im 
68*cd3f6098SRoy Im /* DA7280_IRQ_EVENT_PAT_DIAG (Address 0x05) */
69*cd3f6098SRoy Im #define DA7280_E_PWM_FAULT_MASK			BIT(5)
70*cd3f6098SRoy Im #define DA7280_E_MEM_FAULT_MASK			BIT(6)
71*cd3f6098SRoy Im #define DA7280_E_SEQ_ID_FAULT_MASK		BIT(7)
72*cd3f6098SRoy Im 
73*cd3f6098SRoy Im /* DA7280_IRQ_STATUS1 (Address 0x06) */
74*cd3f6098SRoy Im #define DA7280_STA_SEQ_CONTINUE_MASK		BIT(0)
75*cd3f6098SRoy Im #define DA7280_STA_UVLO_VBAT_OK_MASK		BIT(1)
76*cd3f6098SRoy Im #define DA7280_STA_SEQ_DONE_MASK		BIT(2)
77*cd3f6098SRoy Im #define DA7280_STA_OVERTEMP_CRIT_MASK		BIT(3)
78*cd3f6098SRoy Im #define DA7280_STA_SEQ_FAULT_MASK		BIT(4)
79*cd3f6098SRoy Im #define DA7280_STA_WARNING_MASK			BIT(5)
80*cd3f6098SRoy Im #define DA7280_STA_ACTUATOR_MASK		BIT(6)
81*cd3f6098SRoy Im #define DA7280_STA_OC_MASK			BIT(7)
82*cd3f6098SRoy Im 
83*cd3f6098SRoy Im /* DA7280_IRQ_MASK1 (Address 0x07) */
84*cd3f6098SRoy Im #define DA7280_SEQ_CONTINUE_M_MASK		BIT(0)
85*cd3f6098SRoy Im #define DA7280_E_UVLO_M_MASK			BIT(1)
86*cd3f6098SRoy Im #define DA7280_SEQ_DONE_M_MASK			BIT(2)
87*cd3f6098SRoy Im #define DA7280_OVERTEMP_CRIT_M_MASK		BIT(3)
88*cd3f6098SRoy Im #define DA7280_SEQ_FAULT_M_MASK			BIT(4)
89*cd3f6098SRoy Im #define DA7280_WARNING_M_MASK			BIT(5)
90*cd3f6098SRoy Im #define DA7280_ACTUATOR_M_MASK			BIT(6)
91*cd3f6098SRoy Im #define DA7280_OC_M_MASK			BIT(7)
92*cd3f6098SRoy Im 
93*cd3f6098SRoy Im /* DA7280_ACTUATOR3 (Address 0x0e) */
94*cd3f6098SRoy Im #define DA7280_IMAX_MASK			GENMASK(4, 0)
95*cd3f6098SRoy Im 
96*cd3f6098SRoy Im /* DA7280_TOP_CFG1 (Address 0x13) */
97*cd3f6098SRoy Im #define DA7280_AMP_PID_EN_MASK			BIT(0)
98*cd3f6098SRoy Im #define DA7280_RAPID_STOP_EN_MASK		BIT(1)
99*cd3f6098SRoy Im #define DA7280_ACCELERATION_EN_MASK		BIT(2)
100*cd3f6098SRoy Im #define DA7280_FREQ_TRACK_EN_MASK		BIT(3)
101*cd3f6098SRoy Im #define DA7280_BEMF_SENSE_EN_MASK		BIT(4)
102*cd3f6098SRoy Im #define DA7280_ACTUATOR_TYPE_MASK		BIT(5)
103*cd3f6098SRoy Im 
104*cd3f6098SRoy Im /* DA7280_TOP_CFG2 (Address 0x14) */
105*cd3f6098SRoy Im #define DA7280_FULL_BRAKE_THR_MASK		GENMASK(3, 0)
106*cd3f6098SRoy Im #define DA7280_MEM_DATA_SIGNED_MASK		BIT(4)
107*cd3f6098SRoy Im 
108*cd3f6098SRoy Im /* DA7280_TOP_CFG4 (Address 0x16) */
109*cd3f6098SRoy Im #define DA7280_TST_CALIB_IMPEDANCE_DIS_MASK	BIT(6)
110*cd3f6098SRoy Im #define DA7280_V2I_FACTOR_FREEZE_MASK		BIT(7)
111*cd3f6098SRoy Im 
112*cd3f6098SRoy Im /* DA7280_TOP_INT_CFG1 (Address 0x17) */
113*cd3f6098SRoy Im #define DA7280_BEMF_FAULT_LIM_MASK		GENMASK(1, 0)
114*cd3f6098SRoy Im 
115*cd3f6098SRoy Im /* DA7280_TOP_CTL1 (Address 0x22) */
116*cd3f6098SRoy Im #define DA7280_OPERATION_MODE_MASK		GENMASK(2, 0)
117*cd3f6098SRoy Im #define DA7280_STANDBY_EN_MASK			BIT(3)
118*cd3f6098SRoy Im #define DA7280_SEQ_START_MASK			BIT(4)
119*cd3f6098SRoy Im 
120*cd3f6098SRoy Im /* DA7280_SEQ_CTL2 (Address 0x28) */
121*cd3f6098SRoy Im #define DA7280_PS_SEQ_ID_MASK			GENMASK(3, 0)
122*cd3f6098SRoy Im #define DA7280_PS_SEQ_LOOP_MASK			GENMASK(7, 4)
123*cd3f6098SRoy Im 
124*cd3f6098SRoy Im /* DA7280_GPIO_0_CTL (Address 0x29) */
125*cd3f6098SRoy Im #define DA7280_GPI0_POLARITY_MASK		GENMASK(1, 0)
126*cd3f6098SRoy Im #define DA7280_GPI0_MODE_MASK			BIT(2)
127*cd3f6098SRoy Im #define DA7280_GPI0_SEQUENCE_ID_MASK		GENMASK(6, 3)
128*cd3f6098SRoy Im 
129*cd3f6098SRoy Im /* DA7280_GPIO_1_CTL (Address 0x2a) */
130*cd3f6098SRoy Im #define DA7280_GPI1_POLARITY_MASK		GENMASK(1, 0)
131*cd3f6098SRoy Im #define DA7280_GPI1_MODE_MASK			BIT(2)
132*cd3f6098SRoy Im #define DA7280_GPI1_SEQUENCE_ID_MASK		GENMASK(6, 3)
133*cd3f6098SRoy Im 
134*cd3f6098SRoy Im /* DA7280_GPIO_2_CTL (Address 0x2b) */
135*cd3f6098SRoy Im #define DA7280_GPI2_POLARITY_MASK		GENMASK(1, 0)
136*cd3f6098SRoy Im #define DA7280_GPI2_MODE_MASK			BIT(2)
137*cd3f6098SRoy Im #define DA7280_GPI2_SEQUENCE_ID_MASK		GENMASK(6, 3)
138*cd3f6098SRoy Im 
139*cd3f6098SRoy Im /* DA7280_MEM_CTL2 (Address 0x2d) */
140*cd3f6098SRoy Im #define DA7280_WAV_MEM_LOCK_MASK		BIT(7)
141*cd3f6098SRoy Im 
142*cd3f6098SRoy Im /* DA7280_TOP_CFG5 (Address 0x6e) */
143*cd3f6098SRoy Im #define DA7280_V2I_FACTOR_OFFSET_EN_MASK	BIT(0)
144*cd3f6098SRoy Im 
145*cd3f6098SRoy Im /* DA7280_IRQ_MASK2 (Address 0x83) */
146*cd3f6098SRoy Im #define DA7280_ADC_SAT_M_MASK			BIT(7)
147*cd3f6098SRoy Im 
148*cd3f6098SRoy Im /* Controls */
149*cd3f6098SRoy Im 
150*cd3f6098SRoy Im #define DA7280_VOLTAGE_RATE_MAX			6000000
151*cd3f6098SRoy Im #define DA7280_VOLTAGE_RATE_STEP		23400
152*cd3f6098SRoy Im #define DA7280_NOMMAX_DFT			0x6B
153*cd3f6098SRoy Im #define DA7280_ABSMAX_DFT			0x78
154*cd3f6098SRoy Im 
155*cd3f6098SRoy Im #define DA7280_IMPD_MAX				1500000000
156*cd3f6098SRoy Im #define DA7280_IMPD_DEFAULT			22000000
157*cd3f6098SRoy Im 
158*cd3f6098SRoy Im #define DA7280_IMAX_DEFAULT			0x0E
159*cd3f6098SRoy Im #define DA7280_IMAX_STEP			7200
160*cd3f6098SRoy Im #define DA7280_IMAX_LIMIT			252000
161*cd3f6098SRoy Im 
162*cd3f6098SRoy Im #define DA7280_RESONT_FREQH_DFT			0x39
163*cd3f6098SRoy Im #define DA7280_RESONT_FREQL_DFT			0x32
164*cd3f6098SRoy Im #define DA7280_MIN_RESONAT_FREQ_HZ		50
165*cd3f6098SRoy Im #define DA7280_MAX_RESONAT_FREQ_HZ		300
166*cd3f6098SRoy Im 
167*cd3f6098SRoy Im #define DA7280_SEQ_ID_MAX			15
168*cd3f6098SRoy Im #define DA7280_SEQ_LOOP_MAX			15
169*cd3f6098SRoy Im #define DA7280_GPI_SEQ_ID_DFT			0
170*cd3f6098SRoy Im #define DA7280_GPI_SEQ_ID_MAX			2
171*cd3f6098SRoy Im 
172*cd3f6098SRoy Im #define DA7280_SNP_MEM_SIZE			100
173*cd3f6098SRoy Im #define DA7280_SNP_MEM_MAX			DA7280_SNP_MEM_99
174*cd3f6098SRoy Im 
175*cd3f6098SRoy Im #define DA7280_IRQ_NUM				3
176*cd3f6098SRoy Im 
177*cd3f6098SRoy Im #define DA7280_SKIP_INIT			0x100
178*cd3f6098SRoy Im 
179*cd3f6098SRoy Im #define DA7280_FF_EFFECT_COUNT_MAX		15
180*cd3f6098SRoy Im 
181*cd3f6098SRoy Im /* Maximum gain is 0x7fff for PWM mode */
182*cd3f6098SRoy Im #define DA7280_MAX_MAGNITUDE_SHIFT		15
183*cd3f6098SRoy Im 
184*cd3f6098SRoy Im enum da7280_haptic_dev_t {
185*cd3f6098SRoy Im 	DA7280_LRA	= 0,
186*cd3f6098SRoy Im 	DA7280_ERM_BAR	= 1,
187*cd3f6098SRoy Im 	DA7280_ERM_COIN	= 2,
188*cd3f6098SRoy Im 	DA7280_DEV_MAX,
189*cd3f6098SRoy Im };
190*cd3f6098SRoy Im 
191*cd3f6098SRoy Im enum da7280_op_mode {
192*cd3f6098SRoy Im 	DA7280_INACTIVE		= 0,
193*cd3f6098SRoy Im 	DA7280_DRO_MODE		= 1,
194*cd3f6098SRoy Im 	DA7280_PWM_MODE		= 2,
195*cd3f6098SRoy Im 	DA7280_RTWM_MODE	= 3,
196*cd3f6098SRoy Im 	DA7280_ETWM_MODE	= 4,
197*cd3f6098SRoy Im 	DA7280_OPMODE_MAX,
198*cd3f6098SRoy Im };
199*cd3f6098SRoy Im 
200*cd3f6098SRoy Im #define DA7280_FF_CONSTANT_DRO			1
201*cd3f6098SRoy Im #define DA7280_FF_PERIODIC_PWM			2
202*cd3f6098SRoy Im #define DA7280_FF_PERIODIC_RTWM			1
203*cd3f6098SRoy Im #define DA7280_FF_PERIODIC_ETWM			2
204*cd3f6098SRoy Im 
205*cd3f6098SRoy Im #define DA7280_FF_PERIODIC_MODE			DA7280_RTWM_MODE
206*cd3f6098SRoy Im #define DA7280_FF_CONSTANT_MODE			DA7280_DRO_MODE
207*cd3f6098SRoy Im 
208*cd3f6098SRoy Im enum da7280_custom_effect_param {
209*cd3f6098SRoy Im 	DA7280_CUSTOM_SEQ_ID_IDX	= 0,
210*cd3f6098SRoy Im 	DA7280_CUSTOM_SEQ_LOOP_IDX	= 1,
211*cd3f6098SRoy Im 	DA7280_CUSTOM_DATA_LEN		= 2,
212*cd3f6098SRoy Im };
213*cd3f6098SRoy Im 
214*cd3f6098SRoy Im enum da7280_custom_gpi_effect_param {
215*cd3f6098SRoy Im 	DA7280_CUSTOM_GPI_SEQ_ID_IDX	= 0,
216*cd3f6098SRoy Im 	DA7280_CUSTOM_GPI_NUM_IDX	= 2,
217*cd3f6098SRoy Im 	DA7280_CUSTOM_GP_DATA_LEN	= 3,
218*cd3f6098SRoy Im };
219*cd3f6098SRoy Im 
220*cd3f6098SRoy Im struct da7280_gpi_ctl {
221*cd3f6098SRoy Im 	u8 seq_id;
222*cd3f6098SRoy Im 	u8 mode;
223*cd3f6098SRoy Im 	u8 polarity;
224*cd3f6098SRoy Im };
225*cd3f6098SRoy Im 
226*cd3f6098SRoy Im struct da7280_haptic {
227*cd3f6098SRoy Im 	struct regmap *regmap;
228*cd3f6098SRoy Im 	struct input_dev *input_dev;
229*cd3f6098SRoy Im 	struct device *dev;
230*cd3f6098SRoy Im 	struct i2c_client *client;
231*cd3f6098SRoy Im 	struct pwm_device *pwm_dev;
232*cd3f6098SRoy Im 
233*cd3f6098SRoy Im 	bool legacy;
234*cd3f6098SRoy Im 	struct work_struct work;
235*cd3f6098SRoy Im 	int val;
236*cd3f6098SRoy Im 	u16 gain;
237*cd3f6098SRoy Im 	s16 level;
238*cd3f6098SRoy Im 
239*cd3f6098SRoy Im 	u8 dev_type;
240*cd3f6098SRoy Im 	u8 op_mode;
241*cd3f6098SRoy Im 	u8 const_op_mode;
242*cd3f6098SRoy Im 	u8 periodic_op_mode;
243*cd3f6098SRoy Im 	u16 nommax;
244*cd3f6098SRoy Im 	u16 absmax;
245*cd3f6098SRoy Im 	u32 imax;
246*cd3f6098SRoy Im 	u32 impd;
247*cd3f6098SRoy Im 	u32 resonant_freq_h;
248*cd3f6098SRoy Im 	u32 resonant_freq_l;
249*cd3f6098SRoy Im 	bool bemf_sense_en;
250*cd3f6098SRoy Im 	bool freq_track_en;
251*cd3f6098SRoy Im 	bool acc_en;
252*cd3f6098SRoy Im 	bool rapid_stop_en;
253*cd3f6098SRoy Im 	bool amp_pid_en;
254*cd3f6098SRoy Im 	u8 ps_seq_id;
255*cd3f6098SRoy Im 	u8 ps_seq_loop;
256*cd3f6098SRoy Im 	struct da7280_gpi_ctl gpi_ctl[3];
257*cd3f6098SRoy Im 	bool mem_update;
258*cd3f6098SRoy Im 	u8 snp_mem[DA7280_SNP_MEM_SIZE];
259*cd3f6098SRoy Im 	bool active;
260*cd3f6098SRoy Im 	bool suspended;
261*cd3f6098SRoy Im };
262*cd3f6098SRoy Im 
263*cd3f6098SRoy Im static bool da7280_volatile_register(struct device *dev, unsigned int reg)
264*cd3f6098SRoy Im {
265*cd3f6098SRoy Im 	switch (reg) {
266*cd3f6098SRoy Im 	case DA7280_IRQ_EVENT1:
267*cd3f6098SRoy Im 	case DA7280_IRQ_EVENT_WARNING_DIAG:
268*cd3f6098SRoy Im 	case DA7280_IRQ_EVENT_SEQ_DIAG:
269*cd3f6098SRoy Im 	case DA7280_IRQ_STATUS1:
270*cd3f6098SRoy Im 	case DA7280_TOP_CTL1:
271*cd3f6098SRoy Im 		return true;
272*cd3f6098SRoy Im 	default:
273*cd3f6098SRoy Im 		return false;
274*cd3f6098SRoy Im 	}
275*cd3f6098SRoy Im }
276*cd3f6098SRoy Im 
277*cd3f6098SRoy Im static const struct regmap_config da7280_haptic_regmap_config = {
278*cd3f6098SRoy Im 	.reg_bits = 8,
279*cd3f6098SRoy Im 	.val_bits = 8,
280*cd3f6098SRoy Im 	.max_register = DA7280_SNP_MEM_MAX,
281*cd3f6098SRoy Im 	.volatile_reg = da7280_volatile_register,
282*cd3f6098SRoy Im };
283*cd3f6098SRoy Im 
284*cd3f6098SRoy Im static int da7280_haptic_mem_update(struct da7280_haptic *haptics)
285*cd3f6098SRoy Im {
286*cd3f6098SRoy Im 	unsigned int val;
287*cd3f6098SRoy Im 	int error;
288*cd3f6098SRoy Im 
289*cd3f6098SRoy Im 	/* The patterns should be updated when haptic is not working */
290*cd3f6098SRoy Im 	error = regmap_read(haptics->regmap, DA7280_IRQ_STATUS1, &val);
291*cd3f6098SRoy Im 	if (error)
292*cd3f6098SRoy Im 		return error;
293*cd3f6098SRoy Im 	if (val & DA7280_STA_WARNING_MASK) {
294*cd3f6098SRoy Im 		dev_warn(haptics->dev,
295*cd3f6098SRoy Im 			 "Warning! Please check HAPTIC status.\n");
296*cd3f6098SRoy Im 		return -EBUSY;
297*cd3f6098SRoy Im 	}
298*cd3f6098SRoy Im 
299*cd3f6098SRoy Im 	/* Patterns are not updated if the lock bit is enabled */
300*cd3f6098SRoy Im 	val = 0;
301*cd3f6098SRoy Im 	error = regmap_read(haptics->regmap, DA7280_MEM_CTL2, &val);
302*cd3f6098SRoy Im 	if (error)
303*cd3f6098SRoy Im 		return error;
304*cd3f6098SRoy Im 	if (~val & DA7280_WAV_MEM_LOCK_MASK) {
305*cd3f6098SRoy Im 		dev_warn(haptics->dev, "Please unlock the bit first\n");
306*cd3f6098SRoy Im 		return -EACCES;
307*cd3f6098SRoy Im 	}
308*cd3f6098SRoy Im 
309*cd3f6098SRoy Im 	/* Set to Inactive mode to make sure safety */
310*cd3f6098SRoy Im 	error = regmap_update_bits(haptics->regmap,
311*cd3f6098SRoy Im 				   DA7280_TOP_CTL1,
312*cd3f6098SRoy Im 				   DA7280_OPERATION_MODE_MASK,
313*cd3f6098SRoy Im 				   0);
314*cd3f6098SRoy Im 	if (error)
315*cd3f6098SRoy Im 		return error;
316*cd3f6098SRoy Im 
317*cd3f6098SRoy Im 	error = regmap_read(haptics->regmap, DA7280_MEM_CTL1, &val);
318*cd3f6098SRoy Im 	if (error)
319*cd3f6098SRoy Im 		return error;
320*cd3f6098SRoy Im 
321*cd3f6098SRoy Im 	return regmap_bulk_write(haptics->regmap, val, haptics->snp_mem,
322*cd3f6098SRoy Im 				 DA7280_SNP_MEM_MAX - val + 1);
323*cd3f6098SRoy Im }
324*cd3f6098SRoy Im 
325*cd3f6098SRoy Im static int da7280_haptic_set_pwm(struct da7280_haptic *haptics, bool enabled)
326*cd3f6098SRoy Im {
327*cd3f6098SRoy Im 	struct pwm_state state;
328*cd3f6098SRoy Im 	u64 period_mag_multi;
329*cd3f6098SRoy Im 	int error;
330*cd3f6098SRoy Im 
331*cd3f6098SRoy Im 	if (!haptics->gain && enabled) {
332*cd3f6098SRoy Im 		dev_err(haptics->dev, "Unable to enable pwm with 0 gain\n");
333*cd3f6098SRoy Im 		return -EINVAL;
334*cd3f6098SRoy Im 	}
335*cd3f6098SRoy Im 
336*cd3f6098SRoy Im 	pwm_get_state(haptics->pwm_dev, &state);
337*cd3f6098SRoy Im 	state.enabled = enabled;
338*cd3f6098SRoy Im 	if (enabled) {
339*cd3f6098SRoy Im 		period_mag_multi = (u64)state.period * haptics->gain;
340*cd3f6098SRoy Im 		period_mag_multi >>= DA7280_MAX_MAGNITUDE_SHIFT;
341*cd3f6098SRoy Im 
342*cd3f6098SRoy Im 		/*
343*cd3f6098SRoy Im 		 * The interpretation of duty cycle depends on the acc_en,
344*cd3f6098SRoy Im 		 * it should be between 50% and 100% for acc_en = 0.
345*cd3f6098SRoy Im 		 * See datasheet 'PWM mode' section.
346*cd3f6098SRoy Im 		 */
347*cd3f6098SRoy Im 		if (!haptics->acc_en) {
348*cd3f6098SRoy Im 			period_mag_multi += state.period;
349*cd3f6098SRoy Im 			period_mag_multi /= 2;
350*cd3f6098SRoy Im 		}
351*cd3f6098SRoy Im 
352*cd3f6098SRoy Im 		state.duty_cycle = period_mag_multi;
353*cd3f6098SRoy Im 	}
354*cd3f6098SRoy Im 
355*cd3f6098SRoy Im 	error = pwm_apply_state(haptics->pwm_dev, &state);
356*cd3f6098SRoy Im 	if (error)
357*cd3f6098SRoy Im 		dev_err(haptics->dev, "Failed to apply pwm state: %d\n", error);
358*cd3f6098SRoy Im 
359*cd3f6098SRoy Im 	return error;
360*cd3f6098SRoy Im }
361*cd3f6098SRoy Im 
362*cd3f6098SRoy Im static void da7280_haptic_activate(struct da7280_haptic *haptics)
363*cd3f6098SRoy Im {
364*cd3f6098SRoy Im 	int error;
365*cd3f6098SRoy Im 
366*cd3f6098SRoy Im 	if (haptics->active)
367*cd3f6098SRoy Im 		return;
368*cd3f6098SRoy Im 
369*cd3f6098SRoy Im 	switch (haptics->op_mode) {
370*cd3f6098SRoy Im 	case DA7280_DRO_MODE:
371*cd3f6098SRoy Im 		/* the valid range check when acc_en is enabled */
372*cd3f6098SRoy Im 		if (haptics->acc_en && haptics->level > 0x7F)
373*cd3f6098SRoy Im 			haptics->level = 0x7F;
374*cd3f6098SRoy Im 		else if (haptics->level > 0xFF)
375*cd3f6098SRoy Im 			haptics->level = 0xFF;
376*cd3f6098SRoy Im 
377*cd3f6098SRoy Im 		/* Set level as a % of ACTUATOR_NOMMAX (nommax) */
378*cd3f6098SRoy Im 		error = regmap_write(haptics->regmap, DA7280_TOP_CTL2,
379*cd3f6098SRoy Im 				     haptics->level);
380*cd3f6098SRoy Im 		if (error) {
381*cd3f6098SRoy Im 			dev_err(haptics->dev,
382*cd3f6098SRoy Im 				"Failed to set level to %d: %d\n",
383*cd3f6098SRoy Im 				haptics->level, error);
384*cd3f6098SRoy Im 			return;
385*cd3f6098SRoy Im 		}
386*cd3f6098SRoy Im 		break;
387*cd3f6098SRoy Im 
388*cd3f6098SRoy Im 	case DA7280_PWM_MODE:
389*cd3f6098SRoy Im 		if (da7280_haptic_set_pwm(haptics, true))
390*cd3f6098SRoy Im 			return;
391*cd3f6098SRoy Im 		break;
392*cd3f6098SRoy Im 
393*cd3f6098SRoy Im 	case DA7280_RTWM_MODE:
394*cd3f6098SRoy Im 		/*
395*cd3f6098SRoy Im 		 * The pattern will be played by the PS_SEQ_ID and the
396*cd3f6098SRoy Im 		 * PS_SEQ_LOOP
397*cd3f6098SRoy Im 		 */
398*cd3f6098SRoy Im 		break;
399*cd3f6098SRoy Im 
400*cd3f6098SRoy Im 	case DA7280_ETWM_MODE:
401*cd3f6098SRoy Im 		/*
402*cd3f6098SRoy Im 		 * The pattern will be played by the GPI[N] state,
403*cd3f6098SRoy Im 		 * GPI(N)_SEQUENCE_ID and the PS_SEQ_LOOP. See the
404*cd3f6098SRoy Im 		 * datasheet for the details.
405*cd3f6098SRoy Im 		 */
406*cd3f6098SRoy Im 		break;
407*cd3f6098SRoy Im 
408*cd3f6098SRoy Im 	default:
409*cd3f6098SRoy Im 		dev_err(haptics->dev, "Invalid op mode %d\n", haptics->op_mode);
410*cd3f6098SRoy Im 		return;
411*cd3f6098SRoy Im 	}
412*cd3f6098SRoy Im 
413*cd3f6098SRoy Im 	error = regmap_update_bits(haptics->regmap,
414*cd3f6098SRoy Im 				   DA7280_TOP_CTL1,
415*cd3f6098SRoy Im 				   DA7280_OPERATION_MODE_MASK,
416*cd3f6098SRoy Im 				   haptics->op_mode);
417*cd3f6098SRoy Im 	if (error) {
418*cd3f6098SRoy Im 		dev_err(haptics->dev,
419*cd3f6098SRoy Im 			"Failed to set operation mode: %d", error);
420*cd3f6098SRoy Im 		return;
421*cd3f6098SRoy Im 	}
422*cd3f6098SRoy Im 
423*cd3f6098SRoy Im 	if (haptics->op_mode == DA7280_PWM_MODE ||
424*cd3f6098SRoy Im 	    haptics->op_mode == DA7280_RTWM_MODE) {
425*cd3f6098SRoy Im 		error = regmap_update_bits(haptics->regmap,
426*cd3f6098SRoy Im 					   DA7280_TOP_CTL1,
427*cd3f6098SRoy Im 					   DA7280_SEQ_START_MASK,
428*cd3f6098SRoy Im 					   DA7280_SEQ_START_MASK);
429*cd3f6098SRoy Im 		if (error) {
430*cd3f6098SRoy Im 			dev_err(haptics->dev,
431*cd3f6098SRoy Im 				"Failed to start sequence: %d\n", error);
432*cd3f6098SRoy Im 			return;
433*cd3f6098SRoy Im 		}
434*cd3f6098SRoy Im 	}
435*cd3f6098SRoy Im 
436*cd3f6098SRoy Im 	haptics->active = true;
437*cd3f6098SRoy Im }
438*cd3f6098SRoy Im 
439*cd3f6098SRoy Im static void da7280_haptic_deactivate(struct da7280_haptic *haptics)
440*cd3f6098SRoy Im {
441*cd3f6098SRoy Im 	int error;
442*cd3f6098SRoy Im 
443*cd3f6098SRoy Im 	if (!haptics->active)
444*cd3f6098SRoy Im 		return;
445*cd3f6098SRoy Im 
446*cd3f6098SRoy Im 	/* Set to Inactive mode */
447*cd3f6098SRoy Im 	error = regmap_update_bits(haptics->regmap,
448*cd3f6098SRoy Im 				   DA7280_TOP_CTL1,
449*cd3f6098SRoy Im 				   DA7280_OPERATION_MODE_MASK, 0);
450*cd3f6098SRoy Im 	if (error) {
451*cd3f6098SRoy Im 		dev_err(haptics->dev,
452*cd3f6098SRoy Im 			"Failed to clear operation mode: %d", error);
453*cd3f6098SRoy Im 		return;
454*cd3f6098SRoy Im 	}
455*cd3f6098SRoy Im 
456*cd3f6098SRoy Im 	switch (haptics->op_mode) {
457*cd3f6098SRoy Im 	case DA7280_DRO_MODE:
458*cd3f6098SRoy Im 		error = regmap_write(haptics->regmap,
459*cd3f6098SRoy Im 				     DA7280_TOP_CTL2, 0);
460*cd3f6098SRoy Im 		if (error) {
461*cd3f6098SRoy Im 			dev_err(haptics->dev,
462*cd3f6098SRoy Im 				"Failed to disable DRO mode: %d\n", error);
463*cd3f6098SRoy Im 			return;
464*cd3f6098SRoy Im 		}
465*cd3f6098SRoy Im 		break;
466*cd3f6098SRoy Im 
467*cd3f6098SRoy Im 	case DA7280_PWM_MODE:
468*cd3f6098SRoy Im 		if (da7280_haptic_set_pwm(haptics, false))
469*cd3f6098SRoy Im 			return;
470*cd3f6098SRoy Im 		break;
471*cd3f6098SRoy Im 
472*cd3f6098SRoy Im 	case DA7280_RTWM_MODE:
473*cd3f6098SRoy Im 	case DA7280_ETWM_MODE:
474*cd3f6098SRoy Im 		error = regmap_update_bits(haptics->regmap,
475*cd3f6098SRoy Im 					   DA7280_TOP_CTL1,
476*cd3f6098SRoy Im 					   DA7280_SEQ_START_MASK, 0);
477*cd3f6098SRoy Im 		if (error) {
478*cd3f6098SRoy Im 			dev_err(haptics->dev,
479*cd3f6098SRoy Im 				"Failed to disable RTWM/ETWM mode: %d\n",
480*cd3f6098SRoy Im 				error);
481*cd3f6098SRoy Im 			return;
482*cd3f6098SRoy Im 		}
483*cd3f6098SRoy Im 		break;
484*cd3f6098SRoy Im 
485*cd3f6098SRoy Im 	default:
486*cd3f6098SRoy Im 		dev_err(haptics->dev, "Invalid op mode %d\n", haptics->op_mode);
487*cd3f6098SRoy Im 		return;
488*cd3f6098SRoy Im 	}
489*cd3f6098SRoy Im 
490*cd3f6098SRoy Im 	haptics->active = false;
491*cd3f6098SRoy Im }
492*cd3f6098SRoy Im 
493*cd3f6098SRoy Im static void da7280_haptic_work(struct work_struct *work)
494*cd3f6098SRoy Im {
495*cd3f6098SRoy Im 	struct da7280_haptic *haptics =
496*cd3f6098SRoy Im 		container_of(work, struct da7280_haptic, work);
497*cd3f6098SRoy Im 	int val = haptics->val;
498*cd3f6098SRoy Im 
499*cd3f6098SRoy Im 	if (val)
500*cd3f6098SRoy Im 		da7280_haptic_activate(haptics);
501*cd3f6098SRoy Im 	else
502*cd3f6098SRoy Im 		da7280_haptic_deactivate(haptics);
503*cd3f6098SRoy Im }
504*cd3f6098SRoy Im 
505*cd3f6098SRoy Im static int da7280_haptics_upload_effect(struct input_dev *dev,
506*cd3f6098SRoy Im 					struct ff_effect *effect,
507*cd3f6098SRoy Im 					struct ff_effect *old)
508*cd3f6098SRoy Im {
509*cd3f6098SRoy Im 	struct da7280_haptic *haptics = input_get_drvdata(dev);
510*cd3f6098SRoy Im 	s16 data[DA7280_SNP_MEM_SIZE] = { 0 };
511*cd3f6098SRoy Im 	unsigned int val;
512*cd3f6098SRoy Im 	int tmp, i, num;
513*cd3f6098SRoy Im 	int error;
514*cd3f6098SRoy Im 
515*cd3f6098SRoy Im 	/* The effect should be uploaded when haptic is not working */
516*cd3f6098SRoy Im 	if (haptics->active)
517*cd3f6098SRoy Im 		return -EBUSY;
518*cd3f6098SRoy Im 
519*cd3f6098SRoy Im 	switch (effect->type) {
520*cd3f6098SRoy Im 	/* DRO/PWM modes support this type */
521*cd3f6098SRoy Im 	case FF_CONSTANT:
522*cd3f6098SRoy Im 		haptics->op_mode = haptics->const_op_mode;
523*cd3f6098SRoy Im 		if (haptics->op_mode == DA7280_DRO_MODE) {
524*cd3f6098SRoy Im 			tmp = effect->u.constant.level * 254;
525*cd3f6098SRoy Im 			haptics->level = tmp / 0x7FFF;
526*cd3f6098SRoy Im 			break;
527*cd3f6098SRoy Im 		}
528*cd3f6098SRoy Im 
529*cd3f6098SRoy Im 		haptics->gain =	effect->u.constant.level <= 0 ?
530*cd3f6098SRoy Im 					0 : effect->u.constant.level;
531*cd3f6098SRoy Im 		break;
532*cd3f6098SRoy Im 
533*cd3f6098SRoy Im 	/* RTWM/ETWM modes support this type */
534*cd3f6098SRoy Im 	case FF_PERIODIC:
535*cd3f6098SRoy Im 		if (effect->u.periodic.waveform != FF_CUSTOM) {
536*cd3f6098SRoy Im 			dev_err(haptics->dev,
537*cd3f6098SRoy Im 				"Device can only accept FF_CUSTOM waveform\n");
538*cd3f6098SRoy Im 			return -EINVAL;
539*cd3f6098SRoy Im 		}
540*cd3f6098SRoy Im 
541*cd3f6098SRoy Im 		/*
542*cd3f6098SRoy Im 		 * Load the data and check the length.
543*cd3f6098SRoy Im 		 * the data will be patterns in this case: 4 < X <= 100,
544*cd3f6098SRoy Im 		 * and will be saved into the waveform memory inside DA728x.
545*cd3f6098SRoy Im 		 * If X = 2, the data will be PS_SEQ_ID and PS_SEQ_LOOP.
546*cd3f6098SRoy Im 		 * If X = 3, the 1st data will be GPIX_SEQUENCE_ID .
547*cd3f6098SRoy Im 		 */
548*cd3f6098SRoy Im 		if (effect->u.periodic.custom_len == DA7280_CUSTOM_DATA_LEN)
549*cd3f6098SRoy Im 			goto set_seq_id_loop;
550*cd3f6098SRoy Im 
551*cd3f6098SRoy Im 		if (effect->u.periodic.custom_len == DA7280_CUSTOM_GP_DATA_LEN)
552*cd3f6098SRoy Im 			goto set_gpix_seq_id;
553*cd3f6098SRoy Im 
554*cd3f6098SRoy Im 		if (effect->u.periodic.custom_len < DA7280_CUSTOM_DATA_LEN ||
555*cd3f6098SRoy Im 		    effect->u.periodic.custom_len > DA7280_SNP_MEM_SIZE) {
556*cd3f6098SRoy Im 			dev_err(haptics->dev, "Invalid waveform data size\n");
557*cd3f6098SRoy Im 			return -EINVAL;
558*cd3f6098SRoy Im 		}
559*cd3f6098SRoy Im 
560*cd3f6098SRoy Im 		if (copy_from_user(data, effect->u.periodic.custom_data,
561*cd3f6098SRoy Im 				   sizeof(s16) *
562*cd3f6098SRoy Im 				   effect->u.periodic.custom_len))
563*cd3f6098SRoy Im 			return -EFAULT;
564*cd3f6098SRoy Im 
565*cd3f6098SRoy Im 		memset(haptics->snp_mem, 0, DA7280_SNP_MEM_SIZE);
566*cd3f6098SRoy Im 
567*cd3f6098SRoy Im 		for (i = 0; i < effect->u.periodic.custom_len; i++) {
568*cd3f6098SRoy Im 			if (data[i] < 0 || data[i] > 0xff) {
569*cd3f6098SRoy Im 				dev_err(haptics->dev,
570*cd3f6098SRoy Im 					"Invalid waveform data %d at offset %d\n",
571*cd3f6098SRoy Im 					data[i], i);
572*cd3f6098SRoy Im 				return -EINVAL;
573*cd3f6098SRoy Im 			}
574*cd3f6098SRoy Im 			haptics->snp_mem[i] = (u8)data[i];
575*cd3f6098SRoy Im 		}
576*cd3f6098SRoy Im 
577*cd3f6098SRoy Im 		error = da7280_haptic_mem_update(haptics);
578*cd3f6098SRoy Im 		if (error) {
579*cd3f6098SRoy Im 			dev_err(haptics->dev,
580*cd3f6098SRoy Im 				"Failed to upload waveform: %d\n", error);
581*cd3f6098SRoy Im 			return error;
582*cd3f6098SRoy Im 		}
583*cd3f6098SRoy Im 		break;
584*cd3f6098SRoy Im 
585*cd3f6098SRoy Im set_seq_id_loop:
586*cd3f6098SRoy Im 		if (copy_from_user(data, effect->u.periodic.custom_data,
587*cd3f6098SRoy Im 				   sizeof(s16) * DA7280_CUSTOM_DATA_LEN))
588*cd3f6098SRoy Im 			return -EFAULT;
589*cd3f6098SRoy Im 
590*cd3f6098SRoy Im 		if (data[DA7280_CUSTOM_SEQ_ID_IDX] < 0 ||
591*cd3f6098SRoy Im 		    data[DA7280_CUSTOM_SEQ_ID_IDX] > DA7280_SEQ_ID_MAX ||
592*cd3f6098SRoy Im 		    data[DA7280_CUSTOM_SEQ_LOOP_IDX] < 0 ||
593*cd3f6098SRoy Im 		    data[DA7280_CUSTOM_SEQ_LOOP_IDX] > DA7280_SEQ_LOOP_MAX) {
594*cd3f6098SRoy Im 			dev_err(haptics->dev,
595*cd3f6098SRoy Im 				"Invalid custom id (%d) or loop (%d)\n",
596*cd3f6098SRoy Im 				data[DA7280_CUSTOM_SEQ_ID_IDX],
597*cd3f6098SRoy Im 				data[DA7280_CUSTOM_SEQ_LOOP_IDX]);
598*cd3f6098SRoy Im 			return -EINVAL;
599*cd3f6098SRoy Im 		}
600*cd3f6098SRoy Im 
601*cd3f6098SRoy Im 		haptics->ps_seq_id = data[DA7280_CUSTOM_SEQ_ID_IDX] & 0x0f;
602*cd3f6098SRoy Im 		haptics->ps_seq_loop = data[DA7280_CUSTOM_SEQ_LOOP_IDX] & 0x0f;
603*cd3f6098SRoy Im 		haptics->op_mode = haptics->periodic_op_mode;
604*cd3f6098SRoy Im 
605*cd3f6098SRoy Im 		val = FIELD_PREP(DA7280_PS_SEQ_ID_MASK, haptics->ps_seq_id) |
606*cd3f6098SRoy Im 			FIELD_PREP(DA7280_PS_SEQ_LOOP_MASK,
607*cd3f6098SRoy Im 				   haptics->ps_seq_loop);
608*cd3f6098SRoy Im 		error = regmap_write(haptics->regmap, DA7280_SEQ_CTL2, val);
609*cd3f6098SRoy Im 		if (error) {
610*cd3f6098SRoy Im 			dev_err(haptics->dev,
611*cd3f6098SRoy Im 				"Failed to update PS sequence: %d\n", error);
612*cd3f6098SRoy Im 			return error;
613*cd3f6098SRoy Im 		}
614*cd3f6098SRoy Im 		break;
615*cd3f6098SRoy Im 
616*cd3f6098SRoy Im set_gpix_seq_id:
617*cd3f6098SRoy Im 		if (copy_from_user(data, effect->u.periodic.custom_data,
618*cd3f6098SRoy Im 				   sizeof(s16) * DA7280_CUSTOM_GP_DATA_LEN))
619*cd3f6098SRoy Im 			return -EFAULT;
620*cd3f6098SRoy Im 
621*cd3f6098SRoy Im 		if (data[DA7280_CUSTOM_GPI_SEQ_ID_IDX] < 0 ||
622*cd3f6098SRoy Im 		    data[DA7280_CUSTOM_GPI_SEQ_ID_IDX] > DA7280_SEQ_ID_MAX ||
623*cd3f6098SRoy Im 		    data[DA7280_CUSTOM_GPI_NUM_IDX] < 0 ||
624*cd3f6098SRoy Im 		    data[DA7280_CUSTOM_GPI_NUM_IDX] > DA7280_GPI_SEQ_ID_MAX) {
625*cd3f6098SRoy Im 			dev_err(haptics->dev,
626*cd3f6098SRoy Im 				"Invalid custom GPI id (%d) or num (%d)\n",
627*cd3f6098SRoy Im 				data[DA7280_CUSTOM_GPI_SEQ_ID_IDX],
628*cd3f6098SRoy Im 				data[DA7280_CUSTOM_GPI_NUM_IDX]);
629*cd3f6098SRoy Im 			return -EINVAL;
630*cd3f6098SRoy Im 		}
631*cd3f6098SRoy Im 
632*cd3f6098SRoy Im 		num = data[DA7280_CUSTOM_GPI_NUM_IDX] & 0x0f;
633*cd3f6098SRoy Im 		haptics->gpi_ctl[num].seq_id =
634*cd3f6098SRoy Im 			data[DA7280_CUSTOM_GPI_SEQ_ID_IDX] & 0x0f;
635*cd3f6098SRoy Im 		haptics->op_mode = haptics->periodic_op_mode;
636*cd3f6098SRoy Im 
637*cd3f6098SRoy Im 		val = FIELD_PREP(DA7280_GPI0_SEQUENCE_ID_MASK,
638*cd3f6098SRoy Im 				 haptics->gpi_ctl[num].seq_id);
639*cd3f6098SRoy Im 		error = regmap_update_bits(haptics->regmap,
640*cd3f6098SRoy Im 					   DA7280_GPI_0_CTL + num,
641*cd3f6098SRoy Im 					   DA7280_GPI0_SEQUENCE_ID_MASK,
642*cd3f6098SRoy Im 					   val);
643*cd3f6098SRoy Im 		if (error) {
644*cd3f6098SRoy Im 			dev_err(haptics->dev,
645*cd3f6098SRoy Im 				"Failed to update GPI sequemce: %d\n", error);
646*cd3f6098SRoy Im 			return error;
647*cd3f6098SRoy Im 		}
648*cd3f6098SRoy Im 		break;
649*cd3f6098SRoy Im 
650*cd3f6098SRoy Im 	default:
651*cd3f6098SRoy Im 		dev_err(haptics->dev, "Unsupported effect type: %d\n",
652*cd3f6098SRoy Im 			effect->type);
653*cd3f6098SRoy Im 		return -EINVAL;
654*cd3f6098SRoy Im 	}
655*cd3f6098SRoy Im 
656*cd3f6098SRoy Im 	return 0;
657*cd3f6098SRoy Im }
658*cd3f6098SRoy Im 
659*cd3f6098SRoy Im static int da7280_haptics_playback(struct input_dev *dev,
660*cd3f6098SRoy Im 				   int effect_id, int val)
661*cd3f6098SRoy Im {
662*cd3f6098SRoy Im 	struct da7280_haptic *haptics = input_get_drvdata(dev);
663*cd3f6098SRoy Im 
664*cd3f6098SRoy Im 	if (!haptics->op_mode) {
665*cd3f6098SRoy Im 		dev_warn(haptics->dev, "No effects have been uploaded\n");
666*cd3f6098SRoy Im 		return -EINVAL;
667*cd3f6098SRoy Im 	}
668*cd3f6098SRoy Im 
669*cd3f6098SRoy Im 	if (likely(!haptics->suspended)) {
670*cd3f6098SRoy Im 		haptics->val = val;
671*cd3f6098SRoy Im 		schedule_work(&haptics->work);
672*cd3f6098SRoy Im 	}
673*cd3f6098SRoy Im 
674*cd3f6098SRoy Im 	return 0;
675*cd3f6098SRoy Im }
676*cd3f6098SRoy Im 
677*cd3f6098SRoy Im static int da7280_haptic_start(struct da7280_haptic *haptics)
678*cd3f6098SRoy Im {
679*cd3f6098SRoy Im 	int error;
680*cd3f6098SRoy Im 
681*cd3f6098SRoy Im 	error = regmap_update_bits(haptics->regmap,
682*cd3f6098SRoy Im 				   DA7280_TOP_CTL1,
683*cd3f6098SRoy Im 				   DA7280_STANDBY_EN_MASK,
684*cd3f6098SRoy Im 				   DA7280_STANDBY_EN_MASK);
685*cd3f6098SRoy Im 	if (error) {
686*cd3f6098SRoy Im 		dev_err(haptics->dev, "Unable to enable device: %d\n", error);
687*cd3f6098SRoy Im 		return error;
688*cd3f6098SRoy Im 	}
689*cd3f6098SRoy Im 
690*cd3f6098SRoy Im 	return 0;
691*cd3f6098SRoy Im }
692*cd3f6098SRoy Im 
693*cd3f6098SRoy Im static void da7280_haptic_stop(struct da7280_haptic *haptics)
694*cd3f6098SRoy Im {
695*cd3f6098SRoy Im 	int error;
696*cd3f6098SRoy Im 
697*cd3f6098SRoy Im 	cancel_work_sync(&haptics->work);
698*cd3f6098SRoy Im 
699*cd3f6098SRoy Im 
700*cd3f6098SRoy Im 	da7280_haptic_deactivate(haptics);
701*cd3f6098SRoy Im 
702*cd3f6098SRoy Im 	error = regmap_update_bits(haptics->regmap, DA7280_TOP_CTL1,
703*cd3f6098SRoy Im 				   DA7280_STANDBY_EN_MASK, 0);
704*cd3f6098SRoy Im 	if (error)
705*cd3f6098SRoy Im 		dev_err(haptics->dev, "Failed to disable device: %d\n", error);
706*cd3f6098SRoy Im }
707*cd3f6098SRoy Im 
708*cd3f6098SRoy Im static int da7280_haptic_open(struct input_dev *dev)
709*cd3f6098SRoy Im {
710*cd3f6098SRoy Im 	struct da7280_haptic *haptics = input_get_drvdata(dev);
711*cd3f6098SRoy Im 
712*cd3f6098SRoy Im 	return da7280_haptic_start(haptics);
713*cd3f6098SRoy Im }
714*cd3f6098SRoy Im 
715*cd3f6098SRoy Im static void da7280_haptic_close(struct input_dev *dev)
716*cd3f6098SRoy Im {
717*cd3f6098SRoy Im 	struct da7280_haptic *haptics = input_get_drvdata(dev);
718*cd3f6098SRoy Im 
719*cd3f6098SRoy Im 	da7280_haptic_stop(haptics);
720*cd3f6098SRoy Im }
721*cd3f6098SRoy Im 
722*cd3f6098SRoy Im static u8 da7280_haptic_of_mode_str(struct device *dev,
723*cd3f6098SRoy Im 				    const char *str)
724*cd3f6098SRoy Im {
725*cd3f6098SRoy Im 	if (!strcmp(str, "LRA")) {
726*cd3f6098SRoy Im 		return DA7280_LRA;
727*cd3f6098SRoy Im 	} else if (!strcmp(str, "ERM-bar")) {
728*cd3f6098SRoy Im 		return DA7280_ERM_BAR;
729*cd3f6098SRoy Im 	} else if (!strcmp(str, "ERM-coin")) {
730*cd3f6098SRoy Im 		return DA7280_ERM_COIN;
731*cd3f6098SRoy Im 	} else {
732*cd3f6098SRoy Im 		dev_warn(dev, "Invalid string - set to LRA\n");
733*cd3f6098SRoy Im 		return DA7280_LRA;
734*cd3f6098SRoy Im 	}
735*cd3f6098SRoy Im }
736*cd3f6098SRoy Im 
737*cd3f6098SRoy Im static u8 da7280_haptic_of_gpi_mode_str(struct device *dev,
738*cd3f6098SRoy Im 					const char *str)
739*cd3f6098SRoy Im {
740*cd3f6098SRoy Im 	if (!strcmp(str, "Single-pattern")) {
741*cd3f6098SRoy Im 		return 0;
742*cd3f6098SRoy Im 	} else if (!strcmp(str, "Multi-pattern")) {
743*cd3f6098SRoy Im 		return 1;
744*cd3f6098SRoy Im 	} else {
745*cd3f6098SRoy Im 		dev_warn(dev, "Invalid string - set to Single-pattern\n");
746*cd3f6098SRoy Im 		return 0;
747*cd3f6098SRoy Im 	}
748*cd3f6098SRoy Im }
749*cd3f6098SRoy Im 
750*cd3f6098SRoy Im static u8 da7280_haptic_of_gpi_pol_str(struct device *dev,
751*cd3f6098SRoy Im 				       const char *str)
752*cd3f6098SRoy Im {
753*cd3f6098SRoy Im 	if (!strcmp(str, "Rising-edge")) {
754*cd3f6098SRoy Im 		return 0;
755*cd3f6098SRoy Im 	} else if (!strcmp(str, "Falling-edge")) {
756*cd3f6098SRoy Im 		return 1;
757*cd3f6098SRoy Im 	} else if (!strcmp(str, "Both-edge")) {
758*cd3f6098SRoy Im 		return 2;
759*cd3f6098SRoy Im 	} else {
760*cd3f6098SRoy Im 		dev_warn(dev, "Invalid string - set to Rising-edge\n");
761*cd3f6098SRoy Im 		return 0;
762*cd3f6098SRoy Im 	}
763*cd3f6098SRoy Im }
764*cd3f6098SRoy Im 
765*cd3f6098SRoy Im static u8 da7280_haptic_of_volt_rating_set(u32 val)
766*cd3f6098SRoy Im {
767*cd3f6098SRoy Im 	u32 voltage = val / DA7280_VOLTAGE_RATE_STEP + 1;
768*cd3f6098SRoy Im 
769*cd3f6098SRoy Im 	return min_t(u32, voltage, 0xff);
770*cd3f6098SRoy Im }
771*cd3f6098SRoy Im 
772*cd3f6098SRoy Im static void da7280_parse_properties(struct device *dev,
773*cd3f6098SRoy Im 				    struct da7280_haptic *haptics)
774*cd3f6098SRoy Im {
775*cd3f6098SRoy Im 	unsigned int i, mem[DA7280_SNP_MEM_SIZE];
776*cd3f6098SRoy Im 	char gpi_str1[] = "dlg,gpi0-seq-id";
777*cd3f6098SRoy Im 	char gpi_str2[] = "dlg,gpi0-mode";
778*cd3f6098SRoy Im 	char gpi_str3[] = "dlg,gpi0-polarity";
779*cd3f6098SRoy Im 	const char *str;
780*cd3f6098SRoy Im 	u32 val;
781*cd3f6098SRoy Im 	int error;
782*cd3f6098SRoy Im 
783*cd3f6098SRoy Im 	/*
784*cd3f6098SRoy Im 	 * If there is no property, then use the mode programmed into the chip.
785*cd3f6098SRoy Im 	 */
786*cd3f6098SRoy Im 	haptics->dev_type = DA7280_DEV_MAX;
787*cd3f6098SRoy Im 	error = device_property_read_string(dev, "dlg,actuator-type", &str);
788*cd3f6098SRoy Im 	if (!error)
789*cd3f6098SRoy Im 		haptics->dev_type = da7280_haptic_of_mode_str(dev, str);
790*cd3f6098SRoy Im 
791*cd3f6098SRoy Im 	haptics->const_op_mode = DA7280_DRO_MODE;
792*cd3f6098SRoy Im 	error = device_property_read_u32(dev, "dlg,const-op-mode", &val);
793*cd3f6098SRoy Im 	if (!error && val == DA7280_FF_PERIODIC_PWM)
794*cd3f6098SRoy Im 		haptics->const_op_mode = DA7280_PWM_MODE;
795*cd3f6098SRoy Im 
796*cd3f6098SRoy Im 	haptics->periodic_op_mode = DA7280_RTWM_MODE;
797*cd3f6098SRoy Im 	error = device_property_read_u32(dev, "dlg,periodic-op-mode", &val);
798*cd3f6098SRoy Im 	if (!error && val == DA7280_FF_PERIODIC_ETWM)
799*cd3f6098SRoy Im 		haptics->periodic_op_mode = DA7280_ETWM_MODE;
800*cd3f6098SRoy Im 
801*cd3f6098SRoy Im 	haptics->nommax = DA7280_SKIP_INIT;
802*cd3f6098SRoy Im 	error = device_property_read_u32(dev, "dlg,nom-microvolt", &val);
803*cd3f6098SRoy Im 	if (!error && val < DA7280_VOLTAGE_RATE_MAX)
804*cd3f6098SRoy Im 		haptics->nommax = da7280_haptic_of_volt_rating_set(val);
805*cd3f6098SRoy Im 
806*cd3f6098SRoy Im 	haptics->absmax = DA7280_SKIP_INIT;
807*cd3f6098SRoy Im 	error = device_property_read_u32(dev, "dlg,abs-max-microvolt", &val);
808*cd3f6098SRoy Im 	if (!error && val < DA7280_VOLTAGE_RATE_MAX)
809*cd3f6098SRoy Im 		haptics->absmax = da7280_haptic_of_volt_rating_set(val);
810*cd3f6098SRoy Im 
811*cd3f6098SRoy Im 	haptics->imax = DA7280_IMAX_DEFAULT;
812*cd3f6098SRoy Im 	error = device_property_read_u32(dev, "dlg,imax-microamp", &val);
813*cd3f6098SRoy Im 	if (!error && val < DA7280_IMAX_LIMIT)
814*cd3f6098SRoy Im 		haptics->imax = (val - 28600) / DA7280_IMAX_STEP + 1;
815*cd3f6098SRoy Im 
816*cd3f6098SRoy Im 	haptics->impd = DA7280_IMPD_DEFAULT;
817*cd3f6098SRoy Im 	error = device_property_read_u32(dev, "dlg,impd-micro-ohms", &val);
818*cd3f6098SRoy Im 	if (!error && val <= DA7280_IMPD_MAX)
819*cd3f6098SRoy Im 		haptics->impd = val;
820*cd3f6098SRoy Im 
821*cd3f6098SRoy Im 	haptics->resonant_freq_h = DA7280_SKIP_INIT;
822*cd3f6098SRoy Im 	haptics->resonant_freq_l = DA7280_SKIP_INIT;
823*cd3f6098SRoy Im 	error = device_property_read_u32(dev, "dlg,resonant-freq-hz", &val);
824*cd3f6098SRoy Im 	if (!error) {
825*cd3f6098SRoy Im 		if (val < DA7280_MAX_RESONAT_FREQ_HZ &&
826*cd3f6098SRoy Im 		    val > DA7280_MIN_RESONAT_FREQ_HZ) {
827*cd3f6098SRoy Im 			haptics->resonant_freq_h =
828*cd3f6098SRoy Im 				((1000000000 / (val * 1333)) >> 7) & 0xFF;
829*cd3f6098SRoy Im 			haptics->resonant_freq_l =
830*cd3f6098SRoy Im 				(1000000000 / (val * 1333)) & 0x7F;
831*cd3f6098SRoy Im 		} else {
832*cd3f6098SRoy Im 			haptics->resonant_freq_h = DA7280_RESONT_FREQH_DFT;
833*cd3f6098SRoy Im 			haptics->resonant_freq_l = DA7280_RESONT_FREQL_DFT;
834*cd3f6098SRoy Im 		}
835*cd3f6098SRoy Im 	}
836*cd3f6098SRoy Im 
837*cd3f6098SRoy Im 	/* If no property, set to zero as default is to do nothing. */
838*cd3f6098SRoy Im 	haptics->ps_seq_id = 0;
839*cd3f6098SRoy Im 	error = device_property_read_u32(dev, "dlg,ps-seq-id", &val);
840*cd3f6098SRoy Im 	if (!error && val <= DA7280_SEQ_ID_MAX)
841*cd3f6098SRoy Im 		haptics->ps_seq_id = val;
842*cd3f6098SRoy Im 
843*cd3f6098SRoy Im 	haptics->ps_seq_loop = 0;
844*cd3f6098SRoy Im 	error = device_property_read_u32(dev, "dlg,ps-seq-loop", &val);
845*cd3f6098SRoy Im 	if (!error && val <= DA7280_SEQ_LOOP_MAX)
846*cd3f6098SRoy Im 		haptics->ps_seq_loop = val;
847*cd3f6098SRoy Im 
848*cd3f6098SRoy Im 	/* GPI0~2 Control */
849*cd3f6098SRoy Im 	for (i = 0; i <= DA7280_GPI_SEQ_ID_MAX; i++) {
850*cd3f6098SRoy Im 		gpi_str1[7] = '0' + i;
851*cd3f6098SRoy Im 		haptics->gpi_ctl[i].seq_id = DA7280_GPI_SEQ_ID_DFT + i;
852*cd3f6098SRoy Im 		error = device_property_read_u32 (dev, gpi_str1, &val);
853*cd3f6098SRoy Im 		if (!error && val <= DA7280_SEQ_ID_MAX)
854*cd3f6098SRoy Im 			haptics->gpi_ctl[i].seq_id = val;
855*cd3f6098SRoy Im 
856*cd3f6098SRoy Im 		gpi_str2[7] = '0' + i;
857*cd3f6098SRoy Im 		haptics->gpi_ctl[i].mode = 0;
858*cd3f6098SRoy Im 		error = device_property_read_string(dev, gpi_str2, &str);
859*cd3f6098SRoy Im 		if (!error)
860*cd3f6098SRoy Im 			haptics->gpi_ctl[i].mode =
861*cd3f6098SRoy Im 				da7280_haptic_of_gpi_mode_str(dev, str);
862*cd3f6098SRoy Im 
863*cd3f6098SRoy Im 		gpi_str3[7] = '0' + i;
864*cd3f6098SRoy Im 		haptics->gpi_ctl[i].polarity = 0;
865*cd3f6098SRoy Im 		error = device_property_read_string(dev, gpi_str3, &str);
866*cd3f6098SRoy Im 			haptics->gpi_ctl[i].polarity =
867*cd3f6098SRoy Im 				da7280_haptic_of_gpi_pol_str(dev, str);
868*cd3f6098SRoy Im 	}
869*cd3f6098SRoy Im 
870*cd3f6098SRoy Im 	haptics->bemf_sense_en =
871*cd3f6098SRoy Im 		device_property_read_bool(dev, "dlg,bemf-sens-enable");
872*cd3f6098SRoy Im 	haptics->freq_track_en =
873*cd3f6098SRoy Im 		device_property_read_bool(dev, "dlg,freq-track-enable");
874*cd3f6098SRoy Im 	haptics->acc_en =
875*cd3f6098SRoy Im 		device_property_read_bool(dev, "dlg,acc-enable");
876*cd3f6098SRoy Im 	haptics->rapid_stop_en =
877*cd3f6098SRoy Im 		device_property_read_bool(dev, "dlg,rapid-stop-enable");
878*cd3f6098SRoy Im 	haptics->amp_pid_en =
879*cd3f6098SRoy Im 		device_property_read_bool(dev, "dlg,amp-pid-enable");
880*cd3f6098SRoy Im 
881*cd3f6098SRoy Im 	haptics->mem_update = false;
882*cd3f6098SRoy Im 	error = device_property_read_u32_array(dev, "dlg,mem-array",
883*cd3f6098SRoy Im 					       &mem[0], DA7280_SNP_MEM_SIZE);
884*cd3f6098SRoy Im 	if (!error) {
885*cd3f6098SRoy Im 		haptics->mem_update = true;
886*cd3f6098SRoy Im 		memset(haptics->snp_mem, 0, DA7280_SNP_MEM_SIZE);
887*cd3f6098SRoy Im 		for (i = 0; i < DA7280_SNP_MEM_SIZE; i++) {
888*cd3f6098SRoy Im 			if (mem[i] <= 0xff) {
889*cd3f6098SRoy Im 				haptics->snp_mem[i] = (u8)mem[i];
890*cd3f6098SRoy Im 			} else {
891*cd3f6098SRoy Im 				dev_err(haptics->dev,
892*cd3f6098SRoy Im 					"Invalid data in mem-array at %d: %x\n",
893*cd3f6098SRoy Im 					i, mem[i]);
894*cd3f6098SRoy Im 				haptics->mem_update = false;
895*cd3f6098SRoy Im 				break;
896*cd3f6098SRoy Im 			}
897*cd3f6098SRoy Im 		}
898*cd3f6098SRoy Im 	}
899*cd3f6098SRoy Im }
900*cd3f6098SRoy Im 
901*cd3f6098SRoy Im static irqreturn_t da7280_irq_handler(int irq, void *data)
902*cd3f6098SRoy Im {
903*cd3f6098SRoy Im 	struct da7280_haptic *haptics = data;
904*cd3f6098SRoy Im 	struct device *dev = haptics->dev;
905*cd3f6098SRoy Im 	u8 events[DA7280_IRQ_NUM];
906*cd3f6098SRoy Im 	int error;
907*cd3f6098SRoy Im 
908*cd3f6098SRoy Im 	/* Check what events have happened */
909*cd3f6098SRoy Im 	error = regmap_bulk_read(haptics->regmap, DA7280_IRQ_EVENT1,
910*cd3f6098SRoy Im 				 events, sizeof(events));
911*cd3f6098SRoy Im 	if (error) {
912*cd3f6098SRoy Im 		dev_err(dev, "failed to read interrupt data: %d\n", error);
913*cd3f6098SRoy Im 		goto out;
914*cd3f6098SRoy Im 	}
915*cd3f6098SRoy Im 
916*cd3f6098SRoy Im 	/* Clear events */
917*cd3f6098SRoy Im 	error = regmap_write(haptics->regmap, DA7280_IRQ_EVENT1, events[0]);
918*cd3f6098SRoy Im 	if (error) {
919*cd3f6098SRoy Im 		dev_err(dev, "failed to clear interrupts: %d\n", error);
920*cd3f6098SRoy Im 		goto out;
921*cd3f6098SRoy Im 	}
922*cd3f6098SRoy Im 
923*cd3f6098SRoy Im 	if (events[0] & DA7280_E_SEQ_FAULT_MASK) {
924*cd3f6098SRoy Im 		/*
925*cd3f6098SRoy Im 		 * Stop first if haptic is active, otherwise, the fault may
926*cd3f6098SRoy Im 		 * happen continually even though the bit is cleared.
927*cd3f6098SRoy Im 		 */
928*cd3f6098SRoy Im 		error = regmap_update_bits(haptics->regmap, DA7280_TOP_CTL1,
929*cd3f6098SRoy Im 					   DA7280_OPERATION_MODE_MASK, 0);
930*cd3f6098SRoy Im 		if (error)
931*cd3f6098SRoy Im 			dev_err(dev, "failed to clear op mode on fault: %d\n",
932*cd3f6098SRoy Im 				error);
933*cd3f6098SRoy Im 	}
934*cd3f6098SRoy Im 
935*cd3f6098SRoy Im 	if (events[0] & DA7280_E_SEQ_DONE_MASK)
936*cd3f6098SRoy Im 		haptics->active = false;
937*cd3f6098SRoy Im 
938*cd3f6098SRoy Im 	if (events[0] & DA7280_E_WARNING_MASK) {
939*cd3f6098SRoy Im 		if (events[1] & DA7280_E_LIM_DRIVE_MASK ||
940*cd3f6098SRoy Im 		    events[1] & DA7280_E_LIM_DRIVE_ACC_MASK)
941*cd3f6098SRoy Im 			dev_warn(dev, "Please reduce the driver level\n");
942*cd3f6098SRoy Im 		if (events[1] & DA7280_E_MEM_TYPE_MASK)
943*cd3f6098SRoy Im 			dev_warn(dev, "Please check the mem data format\n");
944*cd3f6098SRoy Im 		if (events[1] & DA7280_E_OVERTEMP_WARN_MASK)
945*cd3f6098SRoy Im 			dev_warn(dev, "Over-temperature warning\n");
946*cd3f6098SRoy Im 	}
947*cd3f6098SRoy Im 
948*cd3f6098SRoy Im 	if (events[0] & DA7280_E_SEQ_FAULT_MASK) {
949*cd3f6098SRoy Im 		if (events[2] & DA7280_E_SEQ_ID_FAULT_MASK)
950*cd3f6098SRoy Im 			dev_info(dev, "Please reload PS_SEQ_ID & mem data\n");
951*cd3f6098SRoy Im 		if (events[2] & DA7280_E_MEM_FAULT_MASK)
952*cd3f6098SRoy Im 			dev_info(dev, "Please reload the mem data\n");
953*cd3f6098SRoy Im 		if (events[2] & DA7280_E_PWM_FAULT_MASK)
954*cd3f6098SRoy Im 			dev_info(dev, "Please restart PWM interface\n");
955*cd3f6098SRoy Im 	}
956*cd3f6098SRoy Im 
957*cd3f6098SRoy Im out:
958*cd3f6098SRoy Im 	return IRQ_HANDLED;
959*cd3f6098SRoy Im }
960*cd3f6098SRoy Im 
961*cd3f6098SRoy Im static int da7280_init(struct da7280_haptic *haptics)
962*cd3f6098SRoy Im {
963*cd3f6098SRoy Im 	unsigned int val = 0;
964*cd3f6098SRoy Im 	u32 v2i_factor;
965*cd3f6098SRoy Im 	int error, i;
966*cd3f6098SRoy Im 	u8 mask = 0;
967*cd3f6098SRoy Im 
968*cd3f6098SRoy Im 	/*
969*cd3f6098SRoy Im 	 * If device type is DA7280_DEV_MAX then simply use currently
970*cd3f6098SRoy Im 	 * programmed mode.
971*cd3f6098SRoy Im 	 */
972*cd3f6098SRoy Im 	if (haptics->dev_type == DA7280_DEV_MAX) {
973*cd3f6098SRoy Im 		error = regmap_read(haptics->regmap, DA7280_TOP_CFG1, &val);
974*cd3f6098SRoy Im 		if (error)
975*cd3f6098SRoy Im 			goto out_err;
976*cd3f6098SRoy Im 
977*cd3f6098SRoy Im 		haptics->dev_type = val & DA7280_ACTUATOR_TYPE_MASK ?
978*cd3f6098SRoy Im 					DA7280_ERM_COIN : DA7280_LRA;
979*cd3f6098SRoy Im 	}
980*cd3f6098SRoy Im 
981*cd3f6098SRoy Im 	/* Apply user settings */
982*cd3f6098SRoy Im 	if (haptics->dev_type == DA7280_LRA &&
983*cd3f6098SRoy Im 	    haptics->resonant_freq_l != DA7280_SKIP_INIT) {
984*cd3f6098SRoy Im 		error = regmap_write(haptics->regmap, DA7280_FRQ_LRA_PER_H,
985*cd3f6098SRoy Im 				     haptics->resonant_freq_h);
986*cd3f6098SRoy Im 		if (error)
987*cd3f6098SRoy Im 			goto out_err;
988*cd3f6098SRoy Im 		error = regmap_write(haptics->regmap, DA7280_FRQ_LRA_PER_L,
989*cd3f6098SRoy Im 				     haptics->resonant_freq_l);
990*cd3f6098SRoy Im 		if (error)
991*cd3f6098SRoy Im 			goto out_err;
992*cd3f6098SRoy Im 	} else if (haptics->dev_type == DA7280_ERM_COIN) {
993*cd3f6098SRoy Im 		error = regmap_update_bits(haptics->regmap, DA7280_TOP_INT_CFG1,
994*cd3f6098SRoy Im 					   DA7280_BEMF_FAULT_LIM_MASK, 0);
995*cd3f6098SRoy Im 		if (error)
996*cd3f6098SRoy Im 			goto out_err;
997*cd3f6098SRoy Im 
998*cd3f6098SRoy Im 		mask = DA7280_TST_CALIB_IMPEDANCE_DIS_MASK |
999*cd3f6098SRoy Im 			DA7280_V2I_FACTOR_FREEZE_MASK;
1000*cd3f6098SRoy Im 		val = DA7280_TST_CALIB_IMPEDANCE_DIS_MASK |
1001*cd3f6098SRoy Im 			DA7280_V2I_FACTOR_FREEZE_MASK;
1002*cd3f6098SRoy Im 		error = regmap_update_bits(haptics->regmap, DA7280_TOP_CFG4,
1003*cd3f6098SRoy Im 					   mask, val);
1004*cd3f6098SRoy Im 		if (error)
1005*cd3f6098SRoy Im 			goto out_err;
1006*cd3f6098SRoy Im 
1007*cd3f6098SRoy Im 		haptics->acc_en = false;
1008*cd3f6098SRoy Im 		haptics->rapid_stop_en = false;
1009*cd3f6098SRoy Im 		haptics->amp_pid_en = false;
1010*cd3f6098SRoy Im 	}
1011*cd3f6098SRoy Im 
1012*cd3f6098SRoy Im 	mask = DA7280_ACTUATOR_TYPE_MASK |
1013*cd3f6098SRoy Im 			DA7280_BEMF_SENSE_EN_MASK |
1014*cd3f6098SRoy Im 			DA7280_FREQ_TRACK_EN_MASK |
1015*cd3f6098SRoy Im 			DA7280_ACCELERATION_EN_MASK |
1016*cd3f6098SRoy Im 			DA7280_RAPID_STOP_EN_MASK |
1017*cd3f6098SRoy Im 			DA7280_AMP_PID_EN_MASK;
1018*cd3f6098SRoy Im 	val = FIELD_PREP(DA7280_ACTUATOR_TYPE_MASK,
1019*cd3f6098SRoy Im 			 (haptics->dev_type ? 1 : 0)) |
1020*cd3f6098SRoy Im 		FIELD_PREP(DA7280_BEMF_SENSE_EN_MASK,
1021*cd3f6098SRoy Im 			   (haptics->bemf_sense_en ? 1 : 0)) |
1022*cd3f6098SRoy Im 		FIELD_PREP(DA7280_FREQ_TRACK_EN_MASK,
1023*cd3f6098SRoy Im 			   (haptics->freq_track_en ? 1 : 0)) |
1024*cd3f6098SRoy Im 		FIELD_PREP(DA7280_ACCELERATION_EN_MASK,
1025*cd3f6098SRoy Im 			   (haptics->acc_en ? 1 : 0)) |
1026*cd3f6098SRoy Im 		FIELD_PREP(DA7280_RAPID_STOP_EN_MASK,
1027*cd3f6098SRoy Im 			   (haptics->rapid_stop_en ? 1 : 0)) |
1028*cd3f6098SRoy Im 		FIELD_PREP(DA7280_AMP_PID_EN_MASK,
1029*cd3f6098SRoy Im 			   (haptics->amp_pid_en ? 1 : 0));
1030*cd3f6098SRoy Im 
1031*cd3f6098SRoy Im 	error = regmap_update_bits(haptics->regmap, DA7280_TOP_CFG1, mask, val);
1032*cd3f6098SRoy Im 	if (error)
1033*cd3f6098SRoy Im 		goto out_err;
1034*cd3f6098SRoy Im 
1035*cd3f6098SRoy Im 	error = regmap_update_bits(haptics->regmap, DA7280_TOP_CFG5,
1036*cd3f6098SRoy Im 				   DA7280_V2I_FACTOR_OFFSET_EN_MASK,
1037*cd3f6098SRoy Im 				   haptics->acc_en ?
1038*cd3f6098SRoy Im 					DA7280_V2I_FACTOR_OFFSET_EN_MASK : 0);
1039*cd3f6098SRoy Im 	if (error)
1040*cd3f6098SRoy Im 		goto out_err;
1041*cd3f6098SRoy Im 
1042*cd3f6098SRoy Im 	error = regmap_update_bits(haptics->regmap,
1043*cd3f6098SRoy Im 				   DA7280_TOP_CFG2,
1044*cd3f6098SRoy Im 				   DA7280_MEM_DATA_SIGNED_MASK,
1045*cd3f6098SRoy Im 				   haptics->acc_en ?
1046*cd3f6098SRoy Im 					0 : DA7280_MEM_DATA_SIGNED_MASK);
1047*cd3f6098SRoy Im 	if (error)
1048*cd3f6098SRoy Im 		goto out_err;
1049*cd3f6098SRoy Im 
1050*cd3f6098SRoy Im 	if (haptics->nommax != DA7280_SKIP_INIT) {
1051*cd3f6098SRoy Im 		error = regmap_write(haptics->regmap, DA7280_ACTUATOR1,
1052*cd3f6098SRoy Im 				     haptics->nommax);
1053*cd3f6098SRoy Im 		if (error)
1054*cd3f6098SRoy Im 			goto out_err;
1055*cd3f6098SRoy Im 	}
1056*cd3f6098SRoy Im 
1057*cd3f6098SRoy Im 	if (haptics->absmax != DA7280_SKIP_INIT) {
1058*cd3f6098SRoy Im 		error = regmap_write(haptics->regmap, DA7280_ACTUATOR2,
1059*cd3f6098SRoy Im 				     haptics->absmax);
1060*cd3f6098SRoy Im 		if (error)
1061*cd3f6098SRoy Im 			goto out_err;
1062*cd3f6098SRoy Im 	}
1063*cd3f6098SRoy Im 
1064*cd3f6098SRoy Im 	error = regmap_update_bits(haptics->regmap, DA7280_ACTUATOR3,
1065*cd3f6098SRoy Im 				   DA7280_IMAX_MASK, haptics->imax);
1066*cd3f6098SRoy Im 	if (error)
1067*cd3f6098SRoy Im 		goto out_err;
1068*cd3f6098SRoy Im 
1069*cd3f6098SRoy Im 	v2i_factor = haptics->impd * (haptics->imax + 4) / 1610400;
1070*cd3f6098SRoy Im 	error = regmap_write(haptics->regmap, DA7280_CALIB_V2I_L,
1071*cd3f6098SRoy Im 			     v2i_factor & 0xff);
1072*cd3f6098SRoy Im 	if (error)
1073*cd3f6098SRoy Im 		goto out_err;
1074*cd3f6098SRoy Im 	error = regmap_write(haptics->regmap, DA7280_CALIB_V2I_H,
1075*cd3f6098SRoy Im 			     v2i_factor >> 8);
1076*cd3f6098SRoy Im 	if (error)
1077*cd3f6098SRoy Im 		goto out_err;
1078*cd3f6098SRoy Im 
1079*cd3f6098SRoy Im 	error = regmap_update_bits(haptics->regmap,
1080*cd3f6098SRoy Im 				   DA7280_TOP_CTL1,
1081*cd3f6098SRoy Im 				   DA7280_STANDBY_EN_MASK,
1082*cd3f6098SRoy Im 				   DA7280_STANDBY_EN_MASK);
1083*cd3f6098SRoy Im 	if (error)
1084*cd3f6098SRoy Im 		goto out_err;
1085*cd3f6098SRoy Im 
1086*cd3f6098SRoy Im 	if (haptics->mem_update) {
1087*cd3f6098SRoy Im 		error = da7280_haptic_mem_update(haptics);
1088*cd3f6098SRoy Im 		if (error)
1089*cd3f6098SRoy Im 			goto out_err;
1090*cd3f6098SRoy Im 	}
1091*cd3f6098SRoy Im 
1092*cd3f6098SRoy Im 	/* Set  PS_SEQ_ID and PS_SEQ_LOOP */
1093*cd3f6098SRoy Im 	val = FIELD_PREP(DA7280_PS_SEQ_ID_MASK, haptics->ps_seq_id) |
1094*cd3f6098SRoy Im 		FIELD_PREP(DA7280_PS_SEQ_LOOP_MASK, haptics->ps_seq_loop);
1095*cd3f6098SRoy Im 	error = regmap_write(haptics->regmap, DA7280_SEQ_CTL2, val);
1096*cd3f6098SRoy Im 	if (error)
1097*cd3f6098SRoy Im 		goto out_err;
1098*cd3f6098SRoy Im 
1099*cd3f6098SRoy Im 	/* GPI(N) CTL */
1100*cd3f6098SRoy Im 	for (i = 0; i < 3; i++) {
1101*cd3f6098SRoy Im 		val = FIELD_PREP(DA7280_GPI0_SEQUENCE_ID_MASK,
1102*cd3f6098SRoy Im 				 haptics->gpi_ctl[i].seq_id) |
1103*cd3f6098SRoy Im 			FIELD_PREP(DA7280_GPI0_MODE_MASK,
1104*cd3f6098SRoy Im 				   haptics->gpi_ctl[i].mode) |
1105*cd3f6098SRoy Im 			FIELD_PREP(DA7280_GPI0_POLARITY_MASK,
1106*cd3f6098SRoy Im 				   haptics->gpi_ctl[i].polarity);
1107*cd3f6098SRoy Im 		error = regmap_write(haptics->regmap,
1108*cd3f6098SRoy Im 				     DA7280_GPI_0_CTL + i, val);
1109*cd3f6098SRoy Im 		if (error)
1110*cd3f6098SRoy Im 			goto out_err;
1111*cd3f6098SRoy Im 	}
1112*cd3f6098SRoy Im 
1113*cd3f6098SRoy Im 	/* Mask ADC_SAT_M bit as default */
1114*cd3f6098SRoy Im 	error = regmap_update_bits(haptics->regmap,
1115*cd3f6098SRoy Im 				   DA7280_IRQ_MASK2,
1116*cd3f6098SRoy Im 				   DA7280_ADC_SAT_M_MASK,
1117*cd3f6098SRoy Im 				   DA7280_ADC_SAT_M_MASK);
1118*cd3f6098SRoy Im 	if (error)
1119*cd3f6098SRoy Im 		goto out_err;
1120*cd3f6098SRoy Im 
1121*cd3f6098SRoy Im 	/* Clear Interrupts */
1122*cd3f6098SRoy Im 	error = regmap_write(haptics->regmap, DA7280_IRQ_EVENT1, 0xff);
1123*cd3f6098SRoy Im 	if (error)
1124*cd3f6098SRoy Im 		goto out_err;
1125*cd3f6098SRoy Im 
1126*cd3f6098SRoy Im 	error = regmap_update_bits(haptics->regmap,
1127*cd3f6098SRoy Im 				   DA7280_IRQ_MASK1,
1128*cd3f6098SRoy Im 				   DA7280_SEQ_FAULT_M_MASK |
1129*cd3f6098SRoy Im 					DA7280_SEQ_DONE_M_MASK,
1130*cd3f6098SRoy Im 				   0);
1131*cd3f6098SRoy Im 	if (error)
1132*cd3f6098SRoy Im 		goto out_err;
1133*cd3f6098SRoy Im 
1134*cd3f6098SRoy Im 	haptics->active = false;
1135*cd3f6098SRoy Im 	return 0;
1136*cd3f6098SRoy Im 
1137*cd3f6098SRoy Im out_err:
1138*cd3f6098SRoy Im 	dev_err(haptics->dev, "chip initialization error: %d\n", error);
1139*cd3f6098SRoy Im 	return error;
1140*cd3f6098SRoy Im }
1141*cd3f6098SRoy Im 
1142*cd3f6098SRoy Im static int da7280_probe(struct i2c_client *client,
1143*cd3f6098SRoy Im 			const struct i2c_device_id *id)
1144*cd3f6098SRoy Im {
1145*cd3f6098SRoy Im 	struct device *dev = &client->dev;
1146*cd3f6098SRoy Im 	struct da7280_haptic *haptics;
1147*cd3f6098SRoy Im 	struct input_dev *input_dev;
1148*cd3f6098SRoy Im 	struct pwm_state state;
1149*cd3f6098SRoy Im 	struct ff_device *ff;
1150*cd3f6098SRoy Im 	int error;
1151*cd3f6098SRoy Im 
1152*cd3f6098SRoy Im 	if (!client->irq) {
1153*cd3f6098SRoy Im 		dev_err(dev, "No IRQ configured\n");
1154*cd3f6098SRoy Im 		return -EINVAL;
1155*cd3f6098SRoy Im 	}
1156*cd3f6098SRoy Im 
1157*cd3f6098SRoy Im 	haptics = devm_kzalloc(dev, sizeof(*haptics), GFP_KERNEL);
1158*cd3f6098SRoy Im 	if (!haptics)
1159*cd3f6098SRoy Im 		return -ENOMEM;
1160*cd3f6098SRoy Im 
1161*cd3f6098SRoy Im 	haptics->dev = dev;
1162*cd3f6098SRoy Im 
1163*cd3f6098SRoy Im 	da7280_parse_properties(dev, haptics);
1164*cd3f6098SRoy Im 
1165*cd3f6098SRoy Im 	if (haptics->const_op_mode == DA7280_PWM_MODE) {
1166*cd3f6098SRoy Im 		haptics->pwm_dev = devm_pwm_get(dev, NULL);
1167*cd3f6098SRoy Im 		error = PTR_ERR_OR_ZERO(haptics->pwm_dev);
1168*cd3f6098SRoy Im 		if (error) {
1169*cd3f6098SRoy Im 			if (error != -EPROBE_DEFER)
1170*cd3f6098SRoy Im 				dev_err(dev, "Unable to request PWM: %d\n",
1171*cd3f6098SRoy Im 					error);
1172*cd3f6098SRoy Im 			return error;
1173*cd3f6098SRoy Im 		}
1174*cd3f6098SRoy Im 
1175*cd3f6098SRoy Im 		/* Sync up PWM state and ensure it is off. */
1176*cd3f6098SRoy Im 		pwm_init_state(haptics->pwm_dev, &state);
1177*cd3f6098SRoy Im 		state.enabled = false;
1178*cd3f6098SRoy Im 		error = pwm_apply_state(haptics->pwm_dev, &state);
1179*cd3f6098SRoy Im 		if (error) {
1180*cd3f6098SRoy Im 			dev_err(dev, "Failed to apply PWM state: %d\n", error);
1181*cd3f6098SRoy Im 			return error;
1182*cd3f6098SRoy Im 		}
1183*cd3f6098SRoy Im 
1184*cd3f6098SRoy Im 		/*
1185*cd3f6098SRoy Im 		 * Check PWM period, PWM freq = 1000000 / state.period.
1186*cd3f6098SRoy Im 		 * The valid PWM freq range: 10k ~ 250kHz.
1187*cd3f6098SRoy Im 		 */
1188*cd3f6098SRoy Im 		if (state.period > 100000 || state.period < 4000) {
1189*cd3f6098SRoy Im 			dev_err(dev, "Unsupported PWM period: %lld\n",
1190*cd3f6098SRoy Im 				state.period);
1191*cd3f6098SRoy Im 			return -EINVAL;
1192*cd3f6098SRoy Im 		}
1193*cd3f6098SRoy Im 	}
1194*cd3f6098SRoy Im 
1195*cd3f6098SRoy Im 	INIT_WORK(&haptics->work, da7280_haptic_work);
1196*cd3f6098SRoy Im 
1197*cd3f6098SRoy Im 	haptics->client = client;
1198*cd3f6098SRoy Im 	i2c_set_clientdata(client, haptics);
1199*cd3f6098SRoy Im 
1200*cd3f6098SRoy Im 	haptics->regmap = devm_regmap_init_i2c(client,
1201*cd3f6098SRoy Im 					       &da7280_haptic_regmap_config);
1202*cd3f6098SRoy Im 	error = PTR_ERR_OR_ZERO(haptics->regmap);
1203*cd3f6098SRoy Im 	if (error) {
1204*cd3f6098SRoy Im 		dev_err(dev, "Failed to allocate register map: %d\n", error);
1205*cd3f6098SRoy Im 		return error;
1206*cd3f6098SRoy Im 	}
1207*cd3f6098SRoy Im 
1208*cd3f6098SRoy Im 	error = da7280_init(haptics);
1209*cd3f6098SRoy Im 	if (error) {
1210*cd3f6098SRoy Im 		dev_err(dev, "Failed to initialize device: %d\n", error);
1211*cd3f6098SRoy Im 		return error;
1212*cd3f6098SRoy Im 	}
1213*cd3f6098SRoy Im 
1214*cd3f6098SRoy Im 	/* Initialize input device for haptic device */
1215*cd3f6098SRoy Im 	input_dev = devm_input_allocate_device(dev);
1216*cd3f6098SRoy Im 	if (!input_dev) {
1217*cd3f6098SRoy Im 		dev_err(dev, "Failed to allocate input device\n");
1218*cd3f6098SRoy Im 		return -ENOMEM;
1219*cd3f6098SRoy Im 	}
1220*cd3f6098SRoy Im 
1221*cd3f6098SRoy Im 	input_dev->name = "da7280-haptic";
1222*cd3f6098SRoy Im 	input_dev->dev.parent = client->dev.parent;
1223*cd3f6098SRoy Im 	input_dev->open = da7280_haptic_open;
1224*cd3f6098SRoy Im 	input_dev->close = da7280_haptic_close;
1225*cd3f6098SRoy Im 	input_set_drvdata(input_dev, haptics);
1226*cd3f6098SRoy Im 	haptics->input_dev = input_dev;
1227*cd3f6098SRoy Im 
1228*cd3f6098SRoy Im 	input_set_capability(haptics->input_dev, EV_FF, FF_PERIODIC);
1229*cd3f6098SRoy Im 	input_set_capability(haptics->input_dev, EV_FF, FF_CUSTOM);
1230*cd3f6098SRoy Im 	input_set_capability(haptics->input_dev, EV_FF, FF_CONSTANT);
1231*cd3f6098SRoy Im 	input_set_capability(haptics->input_dev, EV_FF, FF_GAIN);
1232*cd3f6098SRoy Im 
1233*cd3f6098SRoy Im 	error = input_ff_create(haptics->input_dev,
1234*cd3f6098SRoy Im 				DA7280_FF_EFFECT_COUNT_MAX);
1235*cd3f6098SRoy Im 	if (error) {
1236*cd3f6098SRoy Im 		dev_err(dev, "Failed to create FF input device: %d\n", error);
1237*cd3f6098SRoy Im 		return error;
1238*cd3f6098SRoy Im 	}
1239*cd3f6098SRoy Im 
1240*cd3f6098SRoy Im 	ff = input_dev->ff;
1241*cd3f6098SRoy Im 	ff->upload = da7280_haptics_upload_effect;
1242*cd3f6098SRoy Im 	ff->playback = da7280_haptics_playback;
1243*cd3f6098SRoy Im 
1244*cd3f6098SRoy Im 	error = input_register_device(input_dev);
1245*cd3f6098SRoy Im 	if (error) {
1246*cd3f6098SRoy Im 		dev_err(dev, "Failed to register input device: %d\n", error);
1247*cd3f6098SRoy Im 		return error;
1248*cd3f6098SRoy Im 	}
1249*cd3f6098SRoy Im 
1250*cd3f6098SRoy Im 	error = devm_request_threaded_irq(dev, client->irq,
1251*cd3f6098SRoy Im 					  NULL, da7280_irq_handler,
1252*cd3f6098SRoy Im 					  IRQF_ONESHOT,
1253*cd3f6098SRoy Im 					  "da7280-haptics", haptics);
1254*cd3f6098SRoy Im 	if (error) {
1255*cd3f6098SRoy Im 		dev_err(dev, "Failed to request IRQ %d: %d\n",
1256*cd3f6098SRoy Im 			client->irq, error);
1257*cd3f6098SRoy Im 		return error;
1258*cd3f6098SRoy Im 	}
1259*cd3f6098SRoy Im 
1260*cd3f6098SRoy Im 	return 0;
1261*cd3f6098SRoy Im }
1262*cd3f6098SRoy Im 
1263*cd3f6098SRoy Im static int __maybe_unused da7280_suspend(struct device *dev)
1264*cd3f6098SRoy Im {
1265*cd3f6098SRoy Im 	struct da7280_haptic *haptics = dev_get_drvdata(dev);
1266*cd3f6098SRoy Im 
1267*cd3f6098SRoy Im 	mutex_lock(&haptics->input_dev->mutex);
1268*cd3f6098SRoy Im 
1269*cd3f6098SRoy Im 	/*
1270*cd3f6098SRoy Im 	 * Make sure no new requests will be submitted while device is
1271*cd3f6098SRoy Im 	 * suspended.
1272*cd3f6098SRoy Im 	 */
1273*cd3f6098SRoy Im 	spin_lock_irq(&haptics->input_dev->event_lock);
1274*cd3f6098SRoy Im 	haptics->suspended = true;
1275*cd3f6098SRoy Im 	spin_unlock_irq(&haptics->input_dev->event_lock);
1276*cd3f6098SRoy Im 
1277*cd3f6098SRoy Im 	da7280_haptic_stop(haptics);
1278*cd3f6098SRoy Im 
1279*cd3f6098SRoy Im 	mutex_unlock(&haptics->input_dev->mutex);
1280*cd3f6098SRoy Im 
1281*cd3f6098SRoy Im 	return 0;
1282*cd3f6098SRoy Im }
1283*cd3f6098SRoy Im 
1284*cd3f6098SRoy Im static int __maybe_unused da7280_resume(struct device *dev)
1285*cd3f6098SRoy Im {
1286*cd3f6098SRoy Im 	struct da7280_haptic *haptics = dev_get_drvdata(dev);
1287*cd3f6098SRoy Im 	int retval;
1288*cd3f6098SRoy Im 
1289*cd3f6098SRoy Im 	mutex_lock(&haptics->input_dev->mutex);
1290*cd3f6098SRoy Im 
1291*cd3f6098SRoy Im 	retval = da7280_haptic_start(haptics);
1292*cd3f6098SRoy Im 	if (!retval) {
1293*cd3f6098SRoy Im 		spin_lock_irq(&haptics->input_dev->event_lock);
1294*cd3f6098SRoy Im 		haptics->suspended = false;
1295*cd3f6098SRoy Im 		spin_unlock_irq(&haptics->input_dev->event_lock);
1296*cd3f6098SRoy Im 	}
1297*cd3f6098SRoy Im 
1298*cd3f6098SRoy Im 	mutex_unlock(&haptics->input_dev->mutex);
1299*cd3f6098SRoy Im 	return retval;
1300*cd3f6098SRoy Im }
1301*cd3f6098SRoy Im 
1302*cd3f6098SRoy Im static const struct of_device_id da7280_of_match[] = {
1303*cd3f6098SRoy Im 	{ .compatible = "dlg,da7280", },
1304*cd3f6098SRoy Im 	{ }
1305*cd3f6098SRoy Im };
1306*cd3f6098SRoy Im MODULE_DEVICE_TABLE(of, da7280_of_match);
1307*cd3f6098SRoy Im 
1308*cd3f6098SRoy Im static const struct i2c_device_id da7280_i2c_id[] = {
1309*cd3f6098SRoy Im 	{ "da7280", },
1310*cd3f6098SRoy Im 	{ }
1311*cd3f6098SRoy Im };
1312*cd3f6098SRoy Im MODULE_DEVICE_TABLE(i2c, da7280_i2c_id);
1313*cd3f6098SRoy Im 
1314*cd3f6098SRoy Im static SIMPLE_DEV_PM_OPS(da7280_pm_ops, da7280_suspend, da7280_resume);
1315*cd3f6098SRoy Im 
1316*cd3f6098SRoy Im static struct i2c_driver da7280_driver = {
1317*cd3f6098SRoy Im 	.driver = {
1318*cd3f6098SRoy Im 		.name = "da7280",
1319*cd3f6098SRoy Im 		.of_match_table = of_match_ptr(da7280_of_match),
1320*cd3f6098SRoy Im 		.pm = &da7280_pm_ops,
1321*cd3f6098SRoy Im 	},
1322*cd3f6098SRoy Im 	.probe = da7280_probe,
1323*cd3f6098SRoy Im 	.id_table = da7280_i2c_id,
1324*cd3f6098SRoy Im };
1325*cd3f6098SRoy Im module_i2c_driver(da7280_driver);
1326*cd3f6098SRoy Im 
1327*cd3f6098SRoy Im MODULE_DESCRIPTION("DA7280 haptics driver");
1328*cd3f6098SRoy Im MODULE_AUTHOR("Roy Im <Roy.Im.Opensource@diasemi.com>");
1329*cd3f6098SRoy Im MODULE_LICENSE("GPL");
1330