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