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