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