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