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