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