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