xref: /openbmc/linux/drivers/input/misc/drv260x.c (revision 844f5ed5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * DRV260X haptics driver family
4  *
5  * Author: Dan Murphy <dmurphy@ti.com>
6  *
7  * Copyright:   (C) 2014 Texas Instruments, Inc.
8  */
9 
10 #include <linux/i2c.h>
11 #include <linux/input.h>
12 #include <linux/module.h>
13 #include <linux/regmap.h>
14 #include <linux/slab.h>
15 #include <linux/delay.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/regulator/consumer.h>
18 
19 #include <dt-bindings/input/ti-drv260x.h>
20 
21 #define DRV260X_STATUS		0x0
22 #define DRV260X_MODE		0x1
23 #define DRV260X_RT_PB_IN	0x2
24 #define DRV260X_LIB_SEL		0x3
25 #define DRV260X_WV_SEQ_1	0x4
26 #define DRV260X_WV_SEQ_2	0x5
27 #define DRV260X_WV_SEQ_3	0x6
28 #define DRV260X_WV_SEQ_4	0x7
29 #define DRV260X_WV_SEQ_5	0x8
30 #define DRV260X_WV_SEQ_6	0x9
31 #define DRV260X_WV_SEQ_7	0xa
32 #define DRV260X_WV_SEQ_8	0xb
33 #define DRV260X_GO				0xc
34 #define DRV260X_OVERDRIVE_OFF	0xd
35 #define DRV260X_SUSTAIN_P_OFF	0xe
36 #define DRV260X_SUSTAIN_N_OFF	0xf
37 #define DRV260X_BRAKE_OFF		0x10
38 #define DRV260X_A_TO_V_CTRL		0x11
39 #define DRV260X_A_TO_V_MIN_INPUT	0x12
40 #define DRV260X_A_TO_V_MAX_INPUT	0x13
41 #define DRV260X_A_TO_V_MIN_OUT	0x14
42 #define DRV260X_A_TO_V_MAX_OUT	0x15
43 #define DRV260X_RATED_VOLT		0x16
44 #define DRV260X_OD_CLAMP_VOLT	0x17
45 #define DRV260X_CAL_COMP		0x18
46 #define DRV260X_CAL_BACK_EMF	0x19
47 #define DRV260X_FEEDBACK_CTRL	0x1a
48 #define DRV260X_CTRL1			0x1b
49 #define DRV260X_CTRL2			0x1c
50 #define DRV260X_CTRL3			0x1d
51 #define DRV260X_CTRL4			0x1e
52 #define DRV260X_CTRL5			0x1f
53 #define DRV260X_LRA_LOOP_PERIOD	0x20
54 #define DRV260X_VBAT_MON		0x21
55 #define DRV260X_LRA_RES_PERIOD	0x22
56 #define DRV260X_MAX_REG			0x23
57 
58 #define DRV260X_GO_BIT				0x01
59 
60 /* Library Selection */
61 #define DRV260X_LIB_SEL_MASK		0x07
62 #define DRV260X_LIB_SEL_RAM			0x0
63 #define DRV260X_LIB_SEL_OD			0x1
64 #define DRV260X_LIB_SEL_40_60		0x2
65 #define DRV260X_LIB_SEL_60_80		0x3
66 #define DRV260X_LIB_SEL_100_140		0x4
67 #define DRV260X_LIB_SEL_140_PLUS	0x5
68 
69 #define DRV260X_LIB_SEL_HIZ_MASK	0x10
70 #define DRV260X_LIB_SEL_HIZ_EN		0x01
71 #define DRV260X_LIB_SEL_HIZ_DIS		0
72 
73 /* Mode register */
74 #define DRV260X_STANDBY				(1 << 6)
75 #define DRV260X_STANDBY_MASK		0x40
76 #define DRV260X_INTERNAL_TRIGGER	0x00
77 #define DRV260X_EXT_TRIGGER_EDGE	0x01
78 #define DRV260X_EXT_TRIGGER_LEVEL	0x02
79 #define DRV260X_PWM_ANALOG_IN		0x03
80 #define DRV260X_AUDIOHAPTIC			0x04
81 #define DRV260X_RT_PLAYBACK			0x05
82 #define DRV260X_DIAGNOSTICS			0x06
83 #define DRV260X_AUTO_CAL			0x07
84 
85 /* Audio to Haptics Control */
86 #define DRV260X_AUDIO_HAPTICS_PEAK_10MS		(0 << 2)
87 #define DRV260X_AUDIO_HAPTICS_PEAK_20MS		(1 << 2)
88 #define DRV260X_AUDIO_HAPTICS_PEAK_30MS		(2 << 2)
89 #define DRV260X_AUDIO_HAPTICS_PEAK_40MS		(3 << 2)
90 
91 #define DRV260X_AUDIO_HAPTICS_FILTER_100HZ	0x00
92 #define DRV260X_AUDIO_HAPTICS_FILTER_125HZ	0x01
93 #define DRV260X_AUDIO_HAPTICS_FILTER_150HZ	0x02
94 #define DRV260X_AUDIO_HAPTICS_FILTER_200HZ	0x03
95 
96 /* Min/Max Input/Output Voltages */
97 #define DRV260X_AUDIO_HAPTICS_MIN_IN_VOLT	0x19
98 #define DRV260X_AUDIO_HAPTICS_MAX_IN_VOLT	0x64
99 #define DRV260X_AUDIO_HAPTICS_MIN_OUT_VOLT	0x19
100 #define DRV260X_AUDIO_HAPTICS_MAX_OUT_VOLT	0xFF
101 
102 /* Feedback register */
103 #define DRV260X_FB_REG_ERM_MODE			0x7f
104 #define DRV260X_FB_REG_LRA_MODE			(1 << 7)
105 
106 #define DRV260X_BRAKE_FACTOR_MASK	0x1f
107 #define DRV260X_BRAKE_FACTOR_2X		(1 << 0)
108 #define DRV260X_BRAKE_FACTOR_3X		(2 << 4)
109 #define DRV260X_BRAKE_FACTOR_4X		(3 << 4)
110 #define DRV260X_BRAKE_FACTOR_6X		(4 << 4)
111 #define DRV260X_BRAKE_FACTOR_8X		(5 << 4)
112 #define DRV260X_BRAKE_FACTOR_16		(6 << 4)
113 #define DRV260X_BRAKE_FACTOR_DIS	(7 << 4)
114 
115 #define DRV260X_LOOP_GAIN_LOW		0xf3
116 #define DRV260X_LOOP_GAIN_MED		(1 << 2)
117 #define DRV260X_LOOP_GAIN_HIGH		(2 << 2)
118 #define DRV260X_LOOP_GAIN_VERY_HIGH	(3 << 2)
119 
120 #define DRV260X_BEMF_GAIN_0			0xfc
121 #define DRV260X_BEMF_GAIN_1		(1 << 0)
122 #define DRV260X_BEMF_GAIN_2		(2 << 0)
123 #define DRV260X_BEMF_GAIN_3		(3 << 0)
124 
125 /* Control 1 register */
126 #define DRV260X_AC_CPLE_EN			(1 << 5)
127 #define DRV260X_STARTUP_BOOST		(1 << 7)
128 
129 /* Control 2 register */
130 
131 #define DRV260X_IDISS_TIME_45		0
132 #define DRV260X_IDISS_TIME_75		(1 << 0)
133 #define DRV260X_IDISS_TIME_150		(1 << 1)
134 #define DRV260X_IDISS_TIME_225		0x03
135 
136 #define DRV260X_BLANK_TIME_45	(0 << 2)
137 #define DRV260X_BLANK_TIME_75	(1 << 2)
138 #define DRV260X_BLANK_TIME_150	(2 << 2)
139 #define DRV260X_BLANK_TIME_225	(3 << 2)
140 
141 #define DRV260X_SAMP_TIME_150	(0 << 4)
142 #define DRV260X_SAMP_TIME_200	(1 << 4)
143 #define DRV260X_SAMP_TIME_250	(2 << 4)
144 #define DRV260X_SAMP_TIME_300	(3 << 4)
145 
146 #define DRV260X_BRAKE_STABILIZER	(1 << 6)
147 #define DRV260X_UNIDIR_IN			(0 << 7)
148 #define DRV260X_BIDIR_IN			(1 << 7)
149 
150 /* Control 3 Register */
151 #define DRV260X_LRA_OPEN_LOOP		(1 << 0)
152 #define DRV260X_ANANLOG_IN			(1 << 1)
153 #define DRV260X_LRA_DRV_MODE		(1 << 2)
154 #define DRV260X_RTP_UNSIGNED_DATA	(1 << 3)
155 #define DRV260X_SUPPLY_COMP_DIS		(1 << 4)
156 #define DRV260X_ERM_OPEN_LOOP		(1 << 5)
157 #define DRV260X_NG_THRESH_0			(0 << 6)
158 #define DRV260X_NG_THRESH_2			(1 << 6)
159 #define DRV260X_NG_THRESH_4			(2 << 6)
160 #define DRV260X_NG_THRESH_8			(3 << 6)
161 
162 /* Control 4 Register */
163 #define DRV260X_AUTOCAL_TIME_150MS		(0 << 4)
164 #define DRV260X_AUTOCAL_TIME_250MS		(1 << 4)
165 #define DRV260X_AUTOCAL_TIME_500MS		(2 << 4)
166 #define DRV260X_AUTOCAL_TIME_1000MS		(3 << 4)
167 
168 /**
169  * struct drv260x_data -
170  * @input_dev: Pointer to the input device
171  * @client: Pointer to the I2C client
172  * @regmap: Register map of the device
173  * @work: Work item used to off load the enable/disable of the vibration
174  * @enable_gpio: Pointer to the gpio used for enable/disabling
175  * @regulator: Pointer to the regulator for the IC
176  * @magnitude: Magnitude of the vibration event
177  * @mode: The operating mode of the IC (LRA_NO_CAL, ERM or LRA)
178  * @library: The vibration library to be used
179  * @rated_voltage: The rated_voltage of the actuator
180  * @overdrive_voltage: The over drive voltage of the actuator
181 **/
182 struct drv260x_data {
183 	struct input_dev *input_dev;
184 	struct i2c_client *client;
185 	struct regmap *regmap;
186 	struct work_struct work;
187 	struct gpio_desc *enable_gpio;
188 	struct regulator *regulator;
189 	u32 magnitude;
190 	u32 mode;
191 	u32 library;
192 	int rated_voltage;
193 	int overdrive_voltage;
194 };
195 
196 static const struct reg_default drv260x_reg_defs[] = {
197 	{ DRV260X_STATUS, 0xe0 },
198 	{ DRV260X_MODE, 0x40 },
199 	{ DRV260X_RT_PB_IN, 0x00 },
200 	{ DRV260X_LIB_SEL, 0x00 },
201 	{ DRV260X_WV_SEQ_1, 0x01 },
202 	{ DRV260X_WV_SEQ_2, 0x00 },
203 	{ DRV260X_WV_SEQ_3, 0x00 },
204 	{ DRV260X_WV_SEQ_4, 0x00 },
205 	{ DRV260X_WV_SEQ_5, 0x00 },
206 	{ DRV260X_WV_SEQ_6, 0x00 },
207 	{ DRV260X_WV_SEQ_7, 0x00 },
208 	{ DRV260X_WV_SEQ_8, 0x00 },
209 	{ DRV260X_GO, 0x00 },
210 	{ DRV260X_OVERDRIVE_OFF, 0x00 },
211 	{ DRV260X_SUSTAIN_P_OFF, 0x00 },
212 	{ DRV260X_SUSTAIN_N_OFF, 0x00 },
213 	{ DRV260X_BRAKE_OFF, 0x00 },
214 	{ DRV260X_A_TO_V_CTRL, 0x05 },
215 	{ DRV260X_A_TO_V_MIN_INPUT, 0x19 },
216 	{ DRV260X_A_TO_V_MAX_INPUT, 0xff },
217 	{ DRV260X_A_TO_V_MIN_OUT, 0x19 },
218 	{ DRV260X_A_TO_V_MAX_OUT, 0xff },
219 	{ DRV260X_RATED_VOLT, 0x3e },
220 	{ DRV260X_OD_CLAMP_VOLT, 0x8c },
221 	{ DRV260X_CAL_COMP, 0x0c },
222 	{ DRV260X_CAL_BACK_EMF, 0x6c },
223 	{ DRV260X_FEEDBACK_CTRL, 0x36 },
224 	{ DRV260X_CTRL1, 0x93 },
225 	{ DRV260X_CTRL2, 0xfa },
226 	{ DRV260X_CTRL3, 0xa0 },
227 	{ DRV260X_CTRL4, 0x20 },
228 	{ DRV260X_CTRL5, 0x80 },
229 	{ DRV260X_LRA_LOOP_PERIOD, 0x33 },
230 	{ DRV260X_VBAT_MON, 0x00 },
231 	{ DRV260X_LRA_RES_PERIOD, 0x00 },
232 };
233 
234 #define DRV260X_DEF_RATED_VOLT		0x90
235 #define DRV260X_DEF_OD_CLAMP_VOLT	0x90
236 
237 /*
238  * Rated and Overdriver Voltages:
239  * Calculated using the formula r = v * 255 / 5.6
240  * where r is what will be written to the register
241  * and v is the rated or overdriver voltage of the actuator
242  */
243 static int drv260x_calculate_voltage(unsigned int voltage)
244 {
245 	return (voltage * 255 / 5600);
246 }
247 
248 static void drv260x_worker(struct work_struct *work)
249 {
250 	struct drv260x_data *haptics = container_of(work, struct drv260x_data, work);
251 	int error;
252 
253 	gpiod_set_value(haptics->enable_gpio, 1);
254 	/* Data sheet says to wait 250us before trying to communicate */
255 	udelay(250);
256 
257 	error = regmap_write(haptics->regmap,
258 			     DRV260X_MODE, DRV260X_RT_PLAYBACK);
259 	if (error) {
260 		dev_err(&haptics->client->dev,
261 			"Failed to write set mode: %d\n", error);
262 	} else {
263 		error = regmap_write(haptics->regmap,
264 				     DRV260X_RT_PB_IN, haptics->magnitude);
265 		if (error)
266 			dev_err(&haptics->client->dev,
267 				"Failed to set magnitude: %d\n", error);
268 	}
269 }
270 
271 static int drv260x_haptics_play(struct input_dev *input, void *data,
272 				struct ff_effect *effect)
273 {
274 	struct drv260x_data *haptics = input_get_drvdata(input);
275 
276 	haptics->mode = DRV260X_LRA_NO_CAL_MODE;
277 
278 	if (effect->u.rumble.strong_magnitude > 0)
279 		haptics->magnitude = effect->u.rumble.strong_magnitude;
280 	else if (effect->u.rumble.weak_magnitude > 0)
281 		haptics->magnitude = effect->u.rumble.weak_magnitude;
282 	else
283 		haptics->magnitude = 0;
284 
285 	schedule_work(&haptics->work);
286 
287 	return 0;
288 }
289 
290 static void drv260x_close(struct input_dev *input)
291 {
292 	struct drv260x_data *haptics = input_get_drvdata(input);
293 	int error;
294 
295 	cancel_work_sync(&haptics->work);
296 
297 	error = regmap_write(haptics->regmap, DRV260X_MODE, DRV260X_STANDBY);
298 	if (error)
299 		dev_err(&haptics->client->dev,
300 			"Failed to enter standby mode: %d\n", error);
301 
302 	gpiod_set_value(haptics->enable_gpio, 0);
303 }
304 
305 static const struct reg_sequence drv260x_lra_cal_regs[] = {
306 	{ DRV260X_MODE, DRV260X_AUTO_CAL },
307 	{ DRV260X_CTRL3, DRV260X_NG_THRESH_2 },
308 	{ DRV260X_FEEDBACK_CTRL, DRV260X_FB_REG_LRA_MODE |
309 		DRV260X_BRAKE_FACTOR_4X | DRV260X_LOOP_GAIN_HIGH },
310 };
311 
312 static const struct reg_sequence drv260x_lra_init_regs[] = {
313 	{ DRV260X_MODE, DRV260X_RT_PLAYBACK },
314 	{ DRV260X_A_TO_V_CTRL, DRV260X_AUDIO_HAPTICS_PEAK_20MS |
315 		DRV260X_AUDIO_HAPTICS_FILTER_125HZ },
316 	{ DRV260X_A_TO_V_MIN_INPUT, DRV260X_AUDIO_HAPTICS_MIN_IN_VOLT },
317 	{ DRV260X_A_TO_V_MAX_INPUT, DRV260X_AUDIO_HAPTICS_MAX_IN_VOLT },
318 	{ DRV260X_A_TO_V_MIN_OUT, DRV260X_AUDIO_HAPTICS_MIN_OUT_VOLT },
319 	{ DRV260X_A_TO_V_MAX_OUT, DRV260X_AUDIO_HAPTICS_MAX_OUT_VOLT },
320 	{ DRV260X_FEEDBACK_CTRL, DRV260X_FB_REG_LRA_MODE |
321 		DRV260X_BRAKE_FACTOR_2X | DRV260X_LOOP_GAIN_MED |
322 		DRV260X_BEMF_GAIN_3 },
323 	{ DRV260X_CTRL1, DRV260X_STARTUP_BOOST },
324 	{ DRV260X_CTRL2, DRV260X_SAMP_TIME_250 },
325 	{ DRV260X_CTRL3, DRV260X_NG_THRESH_2 | DRV260X_ANANLOG_IN },
326 	{ DRV260X_CTRL4, DRV260X_AUTOCAL_TIME_500MS },
327 };
328 
329 static const struct reg_sequence drv260x_erm_cal_regs[] = {
330 	{ DRV260X_MODE, DRV260X_AUTO_CAL },
331 	{ DRV260X_A_TO_V_MIN_INPUT, DRV260X_AUDIO_HAPTICS_MIN_IN_VOLT },
332 	{ DRV260X_A_TO_V_MAX_INPUT, DRV260X_AUDIO_HAPTICS_MAX_IN_VOLT },
333 	{ DRV260X_A_TO_V_MIN_OUT, DRV260X_AUDIO_HAPTICS_MIN_OUT_VOLT },
334 	{ DRV260X_A_TO_V_MAX_OUT, DRV260X_AUDIO_HAPTICS_MAX_OUT_VOLT },
335 	{ DRV260X_FEEDBACK_CTRL, DRV260X_BRAKE_FACTOR_3X |
336 		DRV260X_LOOP_GAIN_MED | DRV260X_BEMF_GAIN_2 },
337 	{ DRV260X_CTRL1, DRV260X_STARTUP_BOOST },
338 	{ DRV260X_CTRL2, DRV260X_SAMP_TIME_250 | DRV260X_BLANK_TIME_75 |
339 		DRV260X_IDISS_TIME_75 },
340 	{ DRV260X_CTRL3, DRV260X_NG_THRESH_2 | DRV260X_ERM_OPEN_LOOP },
341 	{ DRV260X_CTRL4, DRV260X_AUTOCAL_TIME_500MS },
342 };
343 
344 static int drv260x_init(struct drv260x_data *haptics)
345 {
346 	int error;
347 	unsigned int cal_buf;
348 
349 	error = regmap_write(haptics->regmap,
350 			     DRV260X_RATED_VOLT, haptics->rated_voltage);
351 	if (error) {
352 		dev_err(&haptics->client->dev,
353 			"Failed to write DRV260X_RATED_VOLT register: %d\n",
354 			error);
355 		return error;
356 	}
357 
358 	error = regmap_write(haptics->regmap,
359 			     DRV260X_OD_CLAMP_VOLT, haptics->overdrive_voltage);
360 	if (error) {
361 		dev_err(&haptics->client->dev,
362 			"Failed to write DRV260X_OD_CLAMP_VOLT register: %d\n",
363 			error);
364 		return error;
365 	}
366 
367 	switch (haptics->mode) {
368 	case DRV260X_LRA_MODE:
369 		error = regmap_register_patch(haptics->regmap,
370 					      drv260x_lra_cal_regs,
371 					      ARRAY_SIZE(drv260x_lra_cal_regs));
372 		if (error) {
373 			dev_err(&haptics->client->dev,
374 				"Failed to write LRA calibration registers: %d\n",
375 				error);
376 			return error;
377 		}
378 
379 		break;
380 
381 	case DRV260X_ERM_MODE:
382 		error = regmap_register_patch(haptics->regmap,
383 					      drv260x_erm_cal_regs,
384 					      ARRAY_SIZE(drv260x_erm_cal_regs));
385 		if (error) {
386 			dev_err(&haptics->client->dev,
387 				"Failed to write ERM calibration registers: %d\n",
388 				error);
389 			return error;
390 		}
391 
392 		error = regmap_update_bits(haptics->regmap, DRV260X_LIB_SEL,
393 					   DRV260X_LIB_SEL_MASK,
394 					   haptics->library);
395 		if (error) {
396 			dev_err(&haptics->client->dev,
397 				"Failed to write DRV260X_LIB_SEL register: %d\n",
398 				error);
399 			return error;
400 		}
401 
402 		break;
403 
404 	default:
405 		error = regmap_register_patch(haptics->regmap,
406 					      drv260x_lra_init_regs,
407 					      ARRAY_SIZE(drv260x_lra_init_regs));
408 		if (error) {
409 			dev_err(&haptics->client->dev,
410 				"Failed to write LRA init registers: %d\n",
411 				error);
412 			return error;
413 		}
414 
415 		error = regmap_update_bits(haptics->regmap, DRV260X_LIB_SEL,
416 					   DRV260X_LIB_SEL_MASK,
417 					   haptics->library);
418 		if (error) {
419 			dev_err(&haptics->client->dev,
420 				"Failed to write DRV260X_LIB_SEL register: %d\n",
421 				error);
422 			return error;
423 		}
424 
425 		/* No need to set GO bit here */
426 		return 0;
427 	}
428 
429 	error = regmap_write(haptics->regmap, DRV260X_GO, DRV260X_GO_BIT);
430 	if (error) {
431 		dev_err(&haptics->client->dev,
432 			"Failed to write GO register: %d\n",
433 			error);
434 		return error;
435 	}
436 
437 	do {
438 		error = regmap_read(haptics->regmap, DRV260X_GO, &cal_buf);
439 		if (error) {
440 			dev_err(&haptics->client->dev,
441 				"Failed to read GO register: %d\n",
442 				error);
443 			return error;
444 		}
445 	} while (cal_buf == DRV260X_GO_BIT);
446 
447 	return 0;
448 }
449 
450 static const struct regmap_config drv260x_regmap_config = {
451 	.reg_bits = 8,
452 	.val_bits = 8,
453 
454 	.max_register = DRV260X_MAX_REG,
455 	.reg_defaults = drv260x_reg_defs,
456 	.num_reg_defaults = ARRAY_SIZE(drv260x_reg_defs),
457 	.cache_type = REGCACHE_NONE,
458 };
459 
460 static int drv260x_probe(struct i2c_client *client)
461 {
462 	struct device *dev = &client->dev;
463 	struct drv260x_data *haptics;
464 	u32 voltage;
465 	int error;
466 
467 	haptics = devm_kzalloc(dev, sizeof(*haptics), GFP_KERNEL);
468 	if (!haptics)
469 		return -ENOMEM;
470 
471 	error = device_property_read_u32(dev, "mode", &haptics->mode);
472 	if (error) {
473 		dev_err(dev, "Can't fetch 'mode' property: %d\n", error);
474 		return error;
475 	}
476 
477 	if (haptics->mode < DRV260X_LRA_MODE ||
478 	    haptics->mode > DRV260X_ERM_MODE) {
479 		dev_err(dev, "Vibrator mode is invalid: %i\n", haptics->mode);
480 		return -EINVAL;
481 	}
482 
483 	error = device_property_read_u32(dev, "library-sel", &haptics->library);
484 	if (error) {
485 		dev_err(dev, "Can't fetch 'library-sel' property: %d\n", error);
486 		return error;
487 	}
488 
489 	if (haptics->library < DRV260X_LIB_EMPTY ||
490 	    haptics->library > DRV260X_ERM_LIB_F) {
491 		dev_err(dev,
492 			"Library value is invalid: %i\n", haptics->library);
493 		return -EINVAL;
494 	}
495 
496 	if (haptics->mode == DRV260X_LRA_MODE &&
497 	    haptics->library != DRV260X_LIB_EMPTY &&
498 	    haptics->library != DRV260X_LIB_LRA) {
499 		dev_err(dev, "LRA Mode with ERM Library mismatch\n");
500 		return -EINVAL;
501 	}
502 
503 	if (haptics->mode == DRV260X_ERM_MODE &&
504 	    (haptics->library == DRV260X_LIB_EMPTY ||
505 	     haptics->library == DRV260X_LIB_LRA)) {
506 		dev_err(dev, "ERM Mode with LRA Library mismatch\n");
507 		return -EINVAL;
508 	}
509 
510 	error = device_property_read_u32(dev, "vib-rated-mv", &voltage);
511 	haptics->rated_voltage = error ? DRV260X_DEF_RATED_VOLT :
512 					 drv260x_calculate_voltage(voltage);
513 
514 	error = device_property_read_u32(dev, "vib-overdrive-mv", &voltage);
515 	haptics->overdrive_voltage = error ? DRV260X_DEF_OD_CLAMP_VOLT :
516 					     drv260x_calculate_voltage(voltage);
517 
518 	haptics->regulator = devm_regulator_get(dev, "vbat");
519 	if (IS_ERR(haptics->regulator)) {
520 		error = PTR_ERR(haptics->regulator);
521 		dev_err(dev, "unable to get regulator, error: %d\n", error);
522 		return error;
523 	}
524 
525 	haptics->enable_gpio = devm_gpiod_get_optional(dev, "enable",
526 						       GPIOD_OUT_HIGH);
527 	if (IS_ERR(haptics->enable_gpio))
528 		return PTR_ERR(haptics->enable_gpio);
529 
530 	haptics->input_dev = devm_input_allocate_device(dev);
531 	if (!haptics->input_dev) {
532 		dev_err(dev, "Failed to allocate input device\n");
533 		return -ENOMEM;
534 	}
535 
536 	haptics->input_dev->name = "drv260x:haptics";
537 	haptics->input_dev->close = drv260x_close;
538 	input_set_drvdata(haptics->input_dev, haptics);
539 	input_set_capability(haptics->input_dev, EV_FF, FF_RUMBLE);
540 
541 	error = input_ff_create_memless(haptics->input_dev, NULL,
542 					drv260x_haptics_play);
543 	if (error) {
544 		dev_err(dev, "input_ff_create() failed: %d\n", error);
545 		return error;
546 	}
547 
548 	INIT_WORK(&haptics->work, drv260x_worker);
549 
550 	haptics->client = client;
551 	i2c_set_clientdata(client, haptics);
552 
553 	haptics->regmap = devm_regmap_init_i2c(client, &drv260x_regmap_config);
554 	if (IS_ERR(haptics->regmap)) {
555 		error = PTR_ERR(haptics->regmap);
556 		dev_err(dev, "Failed to allocate register map: %d\n", error);
557 		return error;
558 	}
559 
560 	error = drv260x_init(haptics);
561 	if (error) {
562 		dev_err(dev, "Device init failed: %d\n", error);
563 		return error;
564 	}
565 
566 	error = input_register_device(haptics->input_dev);
567 	if (error) {
568 		dev_err(dev, "couldn't register input device: %d\n", error);
569 		return error;
570 	}
571 
572 	return 0;
573 }
574 
575 static int drv260x_suspend(struct device *dev)
576 {
577 	struct drv260x_data *haptics = dev_get_drvdata(dev);
578 	int ret = 0;
579 
580 	mutex_lock(&haptics->input_dev->mutex);
581 
582 	if (input_device_enabled(haptics->input_dev)) {
583 		ret = regmap_update_bits(haptics->regmap,
584 					 DRV260X_MODE,
585 					 DRV260X_STANDBY_MASK,
586 					 DRV260X_STANDBY);
587 		if (ret) {
588 			dev_err(dev, "Failed to set standby mode\n");
589 			goto out;
590 		}
591 
592 		gpiod_set_value(haptics->enable_gpio, 0);
593 
594 		ret = regulator_disable(haptics->regulator);
595 		if (ret) {
596 			dev_err(dev, "Failed to disable regulator\n");
597 			regmap_update_bits(haptics->regmap,
598 					   DRV260X_MODE,
599 					   DRV260X_STANDBY_MASK, 0);
600 		}
601 	}
602 out:
603 	mutex_unlock(&haptics->input_dev->mutex);
604 	return ret;
605 }
606 
607 static int drv260x_resume(struct device *dev)
608 {
609 	struct drv260x_data *haptics = dev_get_drvdata(dev);
610 	int ret = 0;
611 
612 	mutex_lock(&haptics->input_dev->mutex);
613 
614 	if (input_device_enabled(haptics->input_dev)) {
615 		ret = regulator_enable(haptics->regulator);
616 		if (ret) {
617 			dev_err(dev, "Failed to enable regulator\n");
618 			goto out;
619 		}
620 
621 		ret = regmap_update_bits(haptics->regmap,
622 					 DRV260X_MODE,
623 					 DRV260X_STANDBY_MASK, 0);
624 		if (ret) {
625 			dev_err(dev, "Failed to unset standby mode\n");
626 			regulator_disable(haptics->regulator);
627 			goto out;
628 		}
629 
630 		gpiod_set_value(haptics->enable_gpio, 1);
631 	}
632 
633 out:
634 	mutex_unlock(&haptics->input_dev->mutex);
635 	return ret;
636 }
637 
638 static DEFINE_SIMPLE_DEV_PM_OPS(drv260x_pm_ops, drv260x_suspend, drv260x_resume);
639 
640 static const struct i2c_device_id drv260x_id[] = {
641 	{ "drv2605l", 0 },
642 	{ }
643 };
644 MODULE_DEVICE_TABLE(i2c, drv260x_id);
645 
646 static const struct of_device_id drv260x_of_match[] = {
647 	{ .compatible = "ti,drv2604", },
648 	{ .compatible = "ti,drv2604l", },
649 	{ .compatible = "ti,drv2605", },
650 	{ .compatible = "ti,drv2605l", },
651 	{ }
652 };
653 MODULE_DEVICE_TABLE(of, drv260x_of_match);
654 
655 static struct i2c_driver drv260x_driver = {
656 	.probe_new	= drv260x_probe,
657 	.driver		= {
658 		.name	= "drv260x-haptics",
659 		.of_match_table = drv260x_of_match,
660 		.pm	= pm_sleep_ptr(&drv260x_pm_ops),
661 	},
662 	.id_table = drv260x_id,
663 };
664 module_i2c_driver(drv260x_driver);
665 
666 MODULE_DESCRIPTION("TI DRV260x haptics driver");
667 MODULE_LICENSE("GPL");
668 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
669