1 // SPDX-License-Identifier: GPL-2.0+
3 * DA7280 Haptic device driver
5 * Copyright (c) 2020 Dialog Semiconductor.
6 * Author: Roy Im <Roy.Im.Opensource@diasemi.com>
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>
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
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)
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)
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)
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)
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)
93 /* DA7280_ACTUATOR3 (Address 0x0e) */
94 #define DA7280_IMAX_MASK GENMASK(4, 0)
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)
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)
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)
112 /* DA7280_TOP_INT_CFG1 (Address 0x17) */
113 #define DA7280_BEMF_FAULT_LIM_MASK GENMASK(1, 0)
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)
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)
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)
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)
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)
139 /* DA7280_MEM_CTL2 (Address 0x2d) */
140 #define DA7280_WAV_MEM_LOCK_MASK BIT(7)
142 /* DA7280_TOP_CFG5 (Address 0x6e) */
143 #define DA7280_V2I_FACTOR_OFFSET_EN_MASK BIT(0)
145 /* DA7280_IRQ_MASK2 (Address 0x83) */
146 #define DA7280_ADC_SAT_M_MASK BIT(7)
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
155 #define DA7280_IMPD_MAX 1500000000
156 #define DA7280_IMPD_DEFAULT 22000000
158 #define DA7280_IMAX_DEFAULT 0x0E
159 #define DA7280_IMAX_STEP 7200
160 #define DA7280_IMAX_LIMIT 252000
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
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
172 #define DA7280_SNP_MEM_SIZE 100
173 #define DA7280_SNP_MEM_MAX DA7280_SNP_MEM_99
175 #define DA7280_IRQ_NUM 3
177 #define DA7280_SKIP_INIT 0x100
179 #define DA7280_FF_EFFECT_COUNT_MAX 15
181 /* Maximum gain is 0x7fff for PWM mode */
182 #define DA7280_MAX_MAGNITUDE_SHIFT 15
184 enum da7280_haptic_dev_t
{
191 enum da7280_op_mode
{
195 DA7280_RTWM_MODE
= 3,
196 DA7280_ETWM_MODE
= 4,
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
205 #define DA7280_FF_PERIODIC_MODE DA7280_RTWM_MODE
206 #define DA7280_FF_CONSTANT_MODE DA7280_DRO_MODE
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,
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,
220 struct da7280_gpi_ctl
{
226 struct da7280_haptic
{
227 struct regmap
*regmap
;
228 struct input_dev
*input_dev
;
230 struct i2c_client
*client
;
231 struct pwm_device
*pwm_dev
;
233 struct work_struct work
;
255 struct da7280_gpi_ctl gpi_ctl
[3];
257 u8 snp_mem
[DA7280_SNP_MEM_SIZE
];
262 static bool da7280_volatile_register(struct device
*dev
, unsigned int 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
:
276 static const struct regmap_config da7280_haptic_regmap_config
= {
279 .max_register
= DA7280_SNP_MEM_MAX
,
280 .volatile_reg
= da7280_volatile_register
,
283 static int da7280_haptic_mem_update(struct da7280_haptic
*haptics
)
288 /* The patterns should be updated when haptic is not working */
289 error
= regmap_read(haptics
->regmap
, DA7280_IRQ_STATUS1
, &val
);
292 if (val
& DA7280_STA_WARNING_MASK
) {
293 dev_warn(haptics
->dev
,
294 "Warning! Please check HAPTIC status.\n");
298 /* Patterns are not updated if the lock bit is enabled */
300 error
= regmap_read(haptics
->regmap
, DA7280_MEM_CTL2
, &val
);
303 if (~val
& DA7280_WAV_MEM_LOCK_MASK
) {
304 dev_warn(haptics
->dev
, "Please unlock the bit first\n");
308 /* Set to Inactive mode to make sure safety */
309 error
= regmap_update_bits(haptics
->regmap
,
311 DA7280_OPERATION_MODE_MASK
,
316 error
= regmap_read(haptics
->regmap
, DA7280_MEM_CTL1
, &val
);
320 return regmap_bulk_write(haptics
->regmap
, val
, haptics
->snp_mem
,
321 DA7280_SNP_MEM_MAX
- val
+ 1);
324 static int da7280_haptic_set_pwm(struct da7280_haptic
*haptics
, bool enabled
)
326 struct pwm_state state
;
327 u64 period_mag_multi
;
330 if (!haptics
->gain
&& enabled
) {
331 dev_err(haptics
->dev
, "Unable to enable pwm with 0 gain\n");
335 pwm_get_state(haptics
->pwm_dev
, &state
);
336 state
.enabled
= enabled
;
338 period_mag_multi
= (u64
)state
.period
* haptics
->gain
;
339 period_mag_multi
>>= DA7280_MAX_MAGNITUDE_SHIFT
;
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.
346 if (!haptics
->acc_en
) {
347 period_mag_multi
+= state
.period
;
348 period_mag_multi
/= 2;
351 state
.duty_cycle
= period_mag_multi
;
354 error
= pwm_apply_might_sleep(haptics
->pwm_dev
, &state
);
356 dev_err(haptics
->dev
, "Failed to apply pwm state: %d\n", error
);
361 static void da7280_haptic_activate(struct da7280_haptic
*haptics
)
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;
376 /* Set level as a % of ACTUATOR_NOMMAX (nommax) */
377 error
= regmap_write(haptics
->regmap
, DA7280_TOP_CTL2
,
380 dev_err(haptics
->dev
,
381 "Failed to set level to %d: %d\n",
382 haptics
->level
, error
);
387 case DA7280_PWM_MODE
:
388 if (da7280_haptic_set_pwm(haptics
, true))
392 case DA7280_RTWM_MODE
:
394 * The pattern will be played by the PS_SEQ_ID and the
399 case DA7280_ETWM_MODE
:
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.
408 dev_err(haptics
->dev
, "Invalid op mode %d\n", haptics
->op_mode
);
412 error
= regmap_update_bits(haptics
->regmap
,
414 DA7280_OPERATION_MODE_MASK
,
417 dev_err(haptics
->dev
,
418 "Failed to set operation mode: %d", error
);
422 if (haptics
->op_mode
== DA7280_PWM_MODE
||
423 haptics
->op_mode
== DA7280_RTWM_MODE
) {
424 error
= regmap_update_bits(haptics
->regmap
,
426 DA7280_SEQ_START_MASK
,
427 DA7280_SEQ_START_MASK
);
429 dev_err(haptics
->dev
,
430 "Failed to start sequence: %d\n", error
);
435 haptics
->active
= true;
438 static void da7280_haptic_deactivate(struct da7280_haptic
*haptics
)
442 if (!haptics
->active
)
445 /* Set to Inactive mode */
446 error
= regmap_update_bits(haptics
->regmap
,
448 DA7280_OPERATION_MODE_MASK
, 0);
450 dev_err(haptics
->dev
,
451 "Failed to clear operation mode: %d", error
);
455 switch (haptics
->op_mode
) {
456 case DA7280_DRO_MODE
:
457 error
= regmap_write(haptics
->regmap
,
460 dev_err(haptics
->dev
,
461 "Failed to disable DRO mode: %d\n", error
);
466 case DA7280_PWM_MODE
:
467 if (da7280_haptic_set_pwm(haptics
, false))
471 case DA7280_RTWM_MODE
:
472 case DA7280_ETWM_MODE
:
473 error
= regmap_update_bits(haptics
->regmap
,
475 DA7280_SEQ_START_MASK
, 0);
477 dev_err(haptics
->dev
,
478 "Failed to disable RTWM/ETWM mode: %d\n",
485 dev_err(haptics
->dev
, "Invalid op mode %d\n", haptics
->op_mode
);
489 haptics
->active
= false;
492 static void da7280_haptic_work(struct work_struct
*work
)
494 struct da7280_haptic
*haptics
=
495 container_of(work
, struct da7280_haptic
, work
);
496 int val
= haptics
->val
;
499 da7280_haptic_activate(haptics
);
501 da7280_haptic_deactivate(haptics
);
504 static int da7280_haptics_upload_effect(struct input_dev
*dev
,
505 struct ff_effect
*effect
,
506 struct ff_effect
*old
)
508 struct da7280_haptic
*haptics
= input_get_drvdata(dev
);
509 s16 data
[DA7280_SNP_MEM_SIZE
] = { 0 };
514 /* The effect should be uploaded when haptic is not working */
518 switch (effect
->type
) {
519 /* DRO/PWM modes support this type */
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;
528 haptics
->gain
= effect
->u
.constant
.level
<= 0 ?
529 0 : effect
->u
.constant
.level
;
532 /* RTWM/ETWM modes support this type */
534 if (effect
->u
.periodic
.waveform
!= FF_CUSTOM
) {
535 dev_err(haptics
->dev
,
536 "Device can only accept FF_CUSTOM waveform\n");
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 .
547 if (effect
->u
.periodic
.custom_len
== DA7280_CUSTOM_DATA_LEN
)
548 goto set_seq_id_loop
;
550 if (effect
->u
.periodic
.custom_len
== DA7280_CUSTOM_GP_DATA_LEN
)
551 goto set_gpix_seq_id
;
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");
559 if (copy_from_user(data
, effect
->u
.periodic
.custom_data
,
561 effect
->u
.periodic
.custom_len
))
564 memset(haptics
->snp_mem
, 0, DA7280_SNP_MEM_SIZE
);
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",
573 haptics
->snp_mem
[i
] = (u8
)data
[i
];
576 error
= da7280_haptic_mem_update(haptics
);
578 dev_err(haptics
->dev
,
579 "Failed to upload waveform: %d\n", error
);
585 if (copy_from_user(data
, effect
->u
.periodic
.custom_data
,
586 sizeof(s16
) * DA7280_CUSTOM_DATA_LEN
))
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
]);
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
;
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
);
609 dev_err(haptics
->dev
,
610 "Failed to update PS sequence: %d\n", error
);
616 if (copy_from_user(data
, effect
->u
.periodic
.custom_data
,
617 sizeof(s16
) * DA7280_CUSTOM_GP_DATA_LEN
))
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
]);
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
;
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
,
643 dev_err(haptics
->dev
,
644 "Failed to update GPI sequence: %d\n", error
);
650 dev_err(haptics
->dev
, "Unsupported effect type: %d\n",
658 static int da7280_haptics_playback(struct input_dev
*dev
,
659 int effect_id
, int val
)
661 struct da7280_haptic
*haptics
= input_get_drvdata(dev
);
663 if (!haptics
->op_mode
) {
664 dev_warn(haptics
->dev
, "No effects have been uploaded\n");
668 if (likely(!haptics
->suspended
)) {
670 schedule_work(&haptics
->work
);
676 static int da7280_haptic_start(struct da7280_haptic
*haptics
)
680 error
= regmap_update_bits(haptics
->regmap
,
682 DA7280_STANDBY_EN_MASK
,
683 DA7280_STANDBY_EN_MASK
);
685 dev_err(haptics
->dev
, "Unable to enable device: %d\n", error
);
692 static void da7280_haptic_stop(struct da7280_haptic
*haptics
)
696 cancel_work_sync(&haptics
->work
);
699 da7280_haptic_deactivate(haptics
);
701 error
= regmap_update_bits(haptics
->regmap
, DA7280_TOP_CTL1
,
702 DA7280_STANDBY_EN_MASK
, 0);
704 dev_err(haptics
->dev
, "Failed to disable device: %d\n", error
);
707 static int da7280_haptic_open(struct input_dev
*dev
)
709 struct da7280_haptic
*haptics
= input_get_drvdata(dev
);
711 return da7280_haptic_start(haptics
);
714 static void da7280_haptic_close(struct input_dev
*dev
)
716 struct da7280_haptic
*haptics
= input_get_drvdata(dev
);
718 da7280_haptic_stop(haptics
);
721 static u8
da7280_haptic_of_mode_str(struct device
*dev
,
724 if (!strcmp(str
, "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
;
731 dev_warn(dev
, "Invalid string - set to LRA\n");
736 static u8
da7280_haptic_of_gpi_mode_str(struct device
*dev
,
739 if (!strcmp(str
, "Single-pattern")) {
741 } else if (!strcmp(str
, "Multi-pattern")) {
744 dev_warn(dev
, "Invalid string - set to Single-pattern\n");
749 static u8
da7280_haptic_of_gpi_pol_str(struct device
*dev
,
752 if (!strcmp(str
, "Rising-edge")) {
754 } else if (!strcmp(str
, "Falling-edge")) {
756 } else if (!strcmp(str
, "Both-edge")) {
759 dev_warn(dev
, "Invalid string - set to Rising-edge\n");
764 static u8
da7280_haptic_of_volt_rating_set(u32 val
)
766 u32 voltage
= val
/ DA7280_VOLTAGE_RATE_STEP
+ 1;
768 return min_t(u32
, voltage
, 0xff);
771 static void da7280_parse_properties(struct device
*dev
,
772 struct da7280_haptic
*haptics
)
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";
783 * If there is no property, then use the mode programmed into the chip.
785 haptics
->dev_type
= DA7280_DEV_MAX
;
786 error
= device_property_read_string(dev
, "dlg,actuator-type", &str
);
788 haptics
->dev_type
= da7280_haptic_of_mode_str(dev
, str
);
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
;
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
;
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
);
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
);
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;
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
)
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
);
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;
831 haptics
->resonant_freq_h
= DA7280_RESONT_FREQH_DFT
;
832 haptics
->resonant_freq_l
= DA7280_RESONT_FREQL_DFT
;
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
;
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
;
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
;
855 gpi_str2
[7] = '0' + i
;
856 haptics
->gpi_ctl
[i
].mode
= 0;
857 error
= device_property_read_string(dev
, gpi_str2
, &str
);
859 haptics
->gpi_ctl
[i
].mode
=
860 da7280_haptic_of_gpi_mode_str(dev
, str
);
862 gpi_str3
[7] = '0' + i
;
863 haptics
->gpi_ctl
[i
].polarity
= 0;
864 error
= device_property_read_string(dev
, gpi_str3
, &str
);
866 haptics
->gpi_ctl
[i
].polarity
=
867 da7280_haptic_of_gpi_pol_str(dev
, str
);
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");
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");
881 haptics
->mem_update
= false;
882 error
= device_property_read_u32_array(dev
, "dlg,mem-array",
883 &mem
[0], DA7280_SNP_MEM_SIZE
);
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
];
891 dev_err(haptics
->dev
,
892 "Invalid data in mem-array at %d: %x\n",
894 haptics
->mem_update
= false;
901 static irqreturn_t
da7280_irq_handler(int irq
, void *data
)
903 struct da7280_haptic
*haptics
= data
;
904 struct device
*dev
= haptics
->dev
;
905 u8 events
[DA7280_IRQ_NUM
];
908 /* Check what events have happened */
909 error
= regmap_bulk_read(haptics
->regmap
, DA7280_IRQ_EVENT1
,
910 events
, sizeof(events
));
912 dev_err(dev
, "failed to read interrupt data: %d\n", error
);
917 error
= regmap_write(haptics
->regmap
, DA7280_IRQ_EVENT1
, events
[0]);
919 dev_err(dev
, "failed to clear interrupts: %d\n", error
);
923 if (events
[0] & DA7280_E_SEQ_FAULT_MASK
) {
925 * Stop first if haptic is active, otherwise, the fault may
926 * happen continually even though the bit is cleared.
928 error
= regmap_update_bits(haptics
->regmap
, DA7280_TOP_CTL1
,
929 DA7280_OPERATION_MODE_MASK
, 0);
931 dev_err(dev
, "failed to clear op mode on fault: %d\n",
935 if (events
[0] & DA7280_E_SEQ_DONE_MASK
)
936 haptics
->active
= false;
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");
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");
961 static int da7280_init(struct da7280_haptic
*haptics
)
963 unsigned int val
= 0;
969 * If device type is DA7280_DEV_MAX then simply use currently
972 if (haptics
->dev_type
== DA7280_DEV_MAX
) {
973 error
= regmap_read(haptics
->regmap
, DA7280_TOP_CFG1
, &val
);
977 haptics
->dev_type
= val
& DA7280_ACTUATOR_TYPE_MASK
?
978 DA7280_ERM_COIN
: DA7280_LRA
;
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
);
988 error
= regmap_write(haptics
->regmap
, DA7280_FRQ_LRA_PER_L
,
989 haptics
->resonant_freq_l
);
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);
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
,
1007 haptics
->acc_en
= false;
1008 haptics
->rapid_stop_en
= false;
1009 haptics
->amp_pid_en
= false;
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));
1031 error
= regmap_update_bits(haptics
->regmap
, DA7280_TOP_CFG1
, mask
, val
);
1035 error
= regmap_update_bits(haptics
->regmap
, DA7280_TOP_CFG5
,
1036 DA7280_V2I_FACTOR_OFFSET_EN_MASK
,
1038 DA7280_V2I_FACTOR_OFFSET_EN_MASK
: 0);
1042 error
= regmap_update_bits(haptics
->regmap
,
1044 DA7280_MEM_DATA_SIGNED_MASK
,
1046 0 : DA7280_MEM_DATA_SIGNED_MASK
);
1050 if (haptics
->nommax
!= DA7280_SKIP_INIT
) {
1051 error
= regmap_write(haptics
->regmap
, DA7280_ACTUATOR1
,
1057 if (haptics
->absmax
!= DA7280_SKIP_INIT
) {
1058 error
= regmap_write(haptics
->regmap
, DA7280_ACTUATOR2
,
1064 error
= regmap_update_bits(haptics
->regmap
, DA7280_ACTUATOR3
,
1065 DA7280_IMAX_MASK
, haptics
->imax
);
1069 v2i_factor
= haptics
->impd
* (haptics
->imax
+ 4) / 1610400;
1070 error
= regmap_write(haptics
->regmap
, DA7280_CALIB_V2I_L
,
1074 error
= regmap_write(haptics
->regmap
, DA7280_CALIB_V2I_H
,
1079 error
= regmap_update_bits(haptics
->regmap
,
1081 DA7280_STANDBY_EN_MASK
,
1082 DA7280_STANDBY_EN_MASK
);
1086 if (haptics
->mem_update
) {
1087 error
= da7280_haptic_mem_update(haptics
);
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
);
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
);
1113 /* Mask ADC_SAT_M bit as default */
1114 error
= regmap_update_bits(haptics
->regmap
,
1116 DA7280_ADC_SAT_M_MASK
,
1117 DA7280_ADC_SAT_M_MASK
);
1121 /* Clear Interrupts */
1122 error
= regmap_write(haptics
->regmap
, DA7280_IRQ_EVENT1
, 0xff);
1126 error
= regmap_update_bits(haptics
->regmap
,
1128 DA7280_SEQ_FAULT_M_MASK
|
1129 DA7280_SEQ_DONE_M_MASK
,
1134 haptics
->active
= false;
1138 dev_err(haptics
->dev
, "chip initialization error: %d\n", error
);
1142 static int da7280_probe(struct i2c_client
*client
)
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
;
1152 dev_err(dev
, "No IRQ configured\n");
1156 haptics
= devm_kzalloc(dev
, sizeof(*haptics
), GFP_KERNEL
);
1162 da7280_parse_properties(dev
, haptics
);
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
);
1168 if (error
!= -EPROBE_DEFER
)
1169 dev_err(dev
, "Unable to request PWM: %d\n",
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
);
1179 dev_err(dev
, "Failed to apply PWM state: %d\n", error
);
1184 * Check PWM period, PWM freq = 1000000 / state.period.
1185 * The valid PWM freq range: 10k ~ 250kHz.
1187 if (state
.period
> 100000 || state
.period
< 4000) {
1188 dev_err(dev
, "Unsupported PWM period: %lld\n",
1194 INIT_WORK(&haptics
->work
, da7280_haptic_work
);
1196 haptics
->client
= client
;
1197 i2c_set_clientdata(client
, haptics
);
1199 haptics
->regmap
= devm_regmap_init_i2c(client
,
1200 &da7280_haptic_regmap_config
);
1201 error
= PTR_ERR_OR_ZERO(haptics
->regmap
);
1203 dev_err(dev
, "Failed to allocate register map: %d\n", error
);
1207 error
= da7280_init(haptics
);
1209 dev_err(dev
, "Failed to initialize device: %d\n", error
);
1213 /* Initialize input device for haptic device */
1214 input_dev
= devm_input_allocate_device(dev
);
1216 dev_err(dev
, "Failed to allocate input device\n");
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
;
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
);
1232 error
= input_ff_create(haptics
->input_dev
,
1233 DA7280_FF_EFFECT_COUNT_MAX
);
1235 dev_err(dev
, "Failed to create FF input device: %d\n", error
);
1240 ff
->upload
= da7280_haptics_upload_effect
;
1241 ff
->playback
= da7280_haptics_playback
;
1243 error
= input_register_device(input_dev
);
1245 dev_err(dev
, "Failed to register input device: %d\n", error
);
1249 error
= devm_request_threaded_irq(dev
, client
->irq
,
1250 NULL
, da7280_irq_handler
,
1252 "da7280-haptics", haptics
);
1254 dev_err(dev
, "Failed to request IRQ %d: %d\n",
1255 client
->irq
, error
);
1262 static int da7280_suspend(struct device
*dev
)
1264 struct da7280_haptic
*haptics
= dev_get_drvdata(dev
);
1266 guard(mutex
)(&haptics
->input_dev
->mutex
);
1269 * Make sure no new requests will be submitted while device is
1272 scoped_guard(spinlock_irq
, &haptics
->input_dev
->event_lock
) {
1273 haptics
->suspended
= true;
1276 da7280_haptic_stop(haptics
);
1281 static int da7280_resume(struct device
*dev
)
1283 struct da7280_haptic
*haptics
= dev_get_drvdata(dev
);
1286 guard(mutex
)(&haptics
->input_dev
->mutex
);
1288 error
= da7280_haptic_start(haptics
);
1292 scoped_guard(spinlock_irq
, &haptics
->input_dev
->event_lock
) {
1293 haptics
->suspended
= false;
1300 static const struct of_device_id da7280_of_match
[] = {
1301 { .compatible
= "dlg,da7280", },
1304 MODULE_DEVICE_TABLE(of
, da7280_of_match
);
1307 static const struct i2c_device_id da7280_i2c_id
[] = {
1311 MODULE_DEVICE_TABLE(i2c
, da7280_i2c_id
);
1313 static DEFINE_SIMPLE_DEV_PM_OPS(da7280_pm_ops
, da7280_suspend
, da7280_resume
);
1315 static struct i2c_driver da7280_driver
= {
1318 .of_match_table
= of_match_ptr(da7280_of_match
),
1319 .pm
= pm_sleep_ptr(&da7280_pm_ops
),
1321 .probe
= da7280_probe
,
1322 .id_table
= da7280_i2c_id
,
1324 module_i2c_driver(da7280_driver
);
1326 MODULE_DESCRIPTION("DA7280 haptics driver");
1327 MODULE_AUTHOR("Roy Im <Roy.Im.Opensource@diasemi.com>");
1328 MODULE_LICENSE("GPL");