Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * DRV260X haptics driver family
0004  *
0005  * Author: Dan Murphy <dmurphy@ti.com>
0006  *
0007  * Copyright:   (C) 2014 Texas Instruments, Inc.
0008  */
0009 
0010 #include <linux/i2c.h>
0011 #include <linux/input.h>
0012 #include <linux/module.h>
0013 #include <linux/regmap.h>
0014 #include <linux/slab.h>
0015 #include <linux/delay.h>
0016 #include <linux/gpio/consumer.h>
0017 #include <linux/regulator/consumer.h>
0018 
0019 #include <dt-bindings/input/ti-drv260x.h>
0020 
0021 #define DRV260X_STATUS      0x0
0022 #define DRV260X_MODE        0x1
0023 #define DRV260X_RT_PB_IN    0x2
0024 #define DRV260X_LIB_SEL     0x3
0025 #define DRV260X_WV_SEQ_1    0x4
0026 #define DRV260X_WV_SEQ_2    0x5
0027 #define DRV260X_WV_SEQ_3    0x6
0028 #define DRV260X_WV_SEQ_4    0x7
0029 #define DRV260X_WV_SEQ_5    0x8
0030 #define DRV260X_WV_SEQ_6    0x9
0031 #define DRV260X_WV_SEQ_7    0xa
0032 #define DRV260X_WV_SEQ_8    0xb
0033 #define DRV260X_GO              0xc
0034 #define DRV260X_OVERDRIVE_OFF   0xd
0035 #define DRV260X_SUSTAIN_P_OFF   0xe
0036 #define DRV260X_SUSTAIN_N_OFF   0xf
0037 #define DRV260X_BRAKE_OFF       0x10
0038 #define DRV260X_A_TO_V_CTRL     0x11
0039 #define DRV260X_A_TO_V_MIN_INPUT    0x12
0040 #define DRV260X_A_TO_V_MAX_INPUT    0x13
0041 #define DRV260X_A_TO_V_MIN_OUT  0x14
0042 #define DRV260X_A_TO_V_MAX_OUT  0x15
0043 #define DRV260X_RATED_VOLT      0x16
0044 #define DRV260X_OD_CLAMP_VOLT   0x17
0045 #define DRV260X_CAL_COMP        0x18
0046 #define DRV260X_CAL_BACK_EMF    0x19
0047 #define DRV260X_FEEDBACK_CTRL   0x1a
0048 #define DRV260X_CTRL1           0x1b
0049 #define DRV260X_CTRL2           0x1c
0050 #define DRV260X_CTRL3           0x1d
0051 #define DRV260X_CTRL4           0x1e
0052 #define DRV260X_CTRL5           0x1f
0053 #define DRV260X_LRA_LOOP_PERIOD 0x20
0054 #define DRV260X_VBAT_MON        0x21
0055 #define DRV260X_LRA_RES_PERIOD  0x22
0056 #define DRV260X_MAX_REG         0x23
0057 
0058 #define DRV260X_GO_BIT              0x01
0059 
0060 /* Library Selection */
0061 #define DRV260X_LIB_SEL_MASK        0x07
0062 #define DRV260X_LIB_SEL_RAM         0x0
0063 #define DRV260X_LIB_SEL_OD          0x1
0064 #define DRV260X_LIB_SEL_40_60       0x2
0065 #define DRV260X_LIB_SEL_60_80       0x3
0066 #define DRV260X_LIB_SEL_100_140     0x4
0067 #define DRV260X_LIB_SEL_140_PLUS    0x5
0068 
0069 #define DRV260X_LIB_SEL_HIZ_MASK    0x10
0070 #define DRV260X_LIB_SEL_HIZ_EN      0x01
0071 #define DRV260X_LIB_SEL_HIZ_DIS     0
0072 
0073 /* Mode register */
0074 #define DRV260X_STANDBY             (1 << 6)
0075 #define DRV260X_STANDBY_MASK        0x40
0076 #define DRV260X_INTERNAL_TRIGGER    0x00
0077 #define DRV260X_EXT_TRIGGER_EDGE    0x01
0078 #define DRV260X_EXT_TRIGGER_LEVEL   0x02
0079 #define DRV260X_PWM_ANALOG_IN       0x03
0080 #define DRV260X_AUDIOHAPTIC         0x04
0081 #define DRV260X_RT_PLAYBACK         0x05
0082 #define DRV260X_DIAGNOSTICS         0x06
0083 #define DRV260X_AUTO_CAL            0x07
0084 
0085 /* Audio to Haptics Control */
0086 #define DRV260X_AUDIO_HAPTICS_PEAK_10MS     (0 << 2)
0087 #define DRV260X_AUDIO_HAPTICS_PEAK_20MS     (1 << 2)
0088 #define DRV260X_AUDIO_HAPTICS_PEAK_30MS     (2 << 2)
0089 #define DRV260X_AUDIO_HAPTICS_PEAK_40MS     (3 << 2)
0090 
0091 #define DRV260X_AUDIO_HAPTICS_FILTER_100HZ  0x00
0092 #define DRV260X_AUDIO_HAPTICS_FILTER_125HZ  0x01
0093 #define DRV260X_AUDIO_HAPTICS_FILTER_150HZ  0x02
0094 #define DRV260X_AUDIO_HAPTICS_FILTER_200HZ  0x03
0095 
0096 /* Min/Max Input/Output Voltages */
0097 #define DRV260X_AUDIO_HAPTICS_MIN_IN_VOLT   0x19
0098 #define DRV260X_AUDIO_HAPTICS_MAX_IN_VOLT   0x64
0099 #define DRV260X_AUDIO_HAPTICS_MIN_OUT_VOLT  0x19
0100 #define DRV260X_AUDIO_HAPTICS_MAX_OUT_VOLT  0xFF
0101 
0102 /* Feedback register */
0103 #define DRV260X_FB_REG_ERM_MODE         0x7f
0104 #define DRV260X_FB_REG_LRA_MODE         (1 << 7)
0105 
0106 #define DRV260X_BRAKE_FACTOR_MASK   0x1f
0107 #define DRV260X_BRAKE_FACTOR_2X     (1 << 0)
0108 #define DRV260X_BRAKE_FACTOR_3X     (2 << 4)
0109 #define DRV260X_BRAKE_FACTOR_4X     (3 << 4)
0110 #define DRV260X_BRAKE_FACTOR_6X     (4 << 4)
0111 #define DRV260X_BRAKE_FACTOR_8X     (5 << 4)
0112 #define DRV260X_BRAKE_FACTOR_16     (6 << 4)
0113 #define DRV260X_BRAKE_FACTOR_DIS    (7 << 4)
0114 
0115 #define DRV260X_LOOP_GAIN_LOW       0xf3
0116 #define DRV260X_LOOP_GAIN_MED       (1 << 2)
0117 #define DRV260X_LOOP_GAIN_HIGH      (2 << 2)
0118 #define DRV260X_LOOP_GAIN_VERY_HIGH (3 << 2)
0119 
0120 #define DRV260X_BEMF_GAIN_0         0xfc
0121 #define DRV260X_BEMF_GAIN_1     (1 << 0)
0122 #define DRV260X_BEMF_GAIN_2     (2 << 0)
0123 #define DRV260X_BEMF_GAIN_3     (3 << 0)
0124 
0125 /* Control 1 register */
0126 #define DRV260X_AC_CPLE_EN          (1 << 5)
0127 #define DRV260X_STARTUP_BOOST       (1 << 7)
0128 
0129 /* Control 2 register */
0130 
0131 #define DRV260X_IDISS_TIME_45       0
0132 #define DRV260X_IDISS_TIME_75       (1 << 0)
0133 #define DRV260X_IDISS_TIME_150      (1 << 1)
0134 #define DRV260X_IDISS_TIME_225      0x03
0135 
0136 #define DRV260X_BLANK_TIME_45   (0 << 2)
0137 #define DRV260X_BLANK_TIME_75   (1 << 2)
0138 #define DRV260X_BLANK_TIME_150  (2 << 2)
0139 #define DRV260X_BLANK_TIME_225  (3 << 2)
0140 
0141 #define DRV260X_SAMP_TIME_150   (0 << 4)
0142 #define DRV260X_SAMP_TIME_200   (1 << 4)
0143 #define DRV260X_SAMP_TIME_250   (2 << 4)
0144 #define DRV260X_SAMP_TIME_300   (3 << 4)
0145 
0146 #define DRV260X_BRAKE_STABILIZER    (1 << 6)
0147 #define DRV260X_UNIDIR_IN           (0 << 7)
0148 #define DRV260X_BIDIR_IN            (1 << 7)
0149 
0150 /* Control 3 Register */
0151 #define DRV260X_LRA_OPEN_LOOP       (1 << 0)
0152 #define DRV260X_ANANLOG_IN          (1 << 1)
0153 #define DRV260X_LRA_DRV_MODE        (1 << 2)
0154 #define DRV260X_RTP_UNSIGNED_DATA   (1 << 3)
0155 #define DRV260X_SUPPLY_COMP_DIS     (1 << 4)
0156 #define DRV260X_ERM_OPEN_LOOP       (1 << 5)
0157 #define DRV260X_NG_THRESH_0         (0 << 6)
0158 #define DRV260X_NG_THRESH_2         (1 << 6)
0159 #define DRV260X_NG_THRESH_4         (2 << 6)
0160 #define DRV260X_NG_THRESH_8         (3 << 6)
0161 
0162 /* Control 4 Register */
0163 #define DRV260X_AUTOCAL_TIME_150MS      (0 << 4)
0164 #define DRV260X_AUTOCAL_TIME_250MS      (1 << 4)
0165 #define DRV260X_AUTOCAL_TIME_500MS      (2 << 4)
0166 #define DRV260X_AUTOCAL_TIME_1000MS     (3 << 4)
0167 
0168 /**
0169  * struct drv260x_data -
0170  * @input_dev: Pointer to the input device
0171  * @client: Pointer to the I2C client
0172  * @regmap: Register map of the device
0173  * @work: Work item used to off load the enable/disable of the vibration
0174  * @enable_gpio: Pointer to the gpio used for enable/disabling
0175  * @regulator: Pointer to the regulator for the IC
0176  * @magnitude: Magnitude of the vibration event
0177  * @mode: The operating mode of the IC (LRA_NO_CAL, ERM or LRA)
0178  * @library: The vibration library to be used
0179  * @rated_voltage: The rated_voltage of the actuator
0180  * @overdrive_voltage: The over drive voltage of the actuator
0181 **/
0182 struct drv260x_data {
0183     struct input_dev *input_dev;
0184     struct i2c_client *client;
0185     struct regmap *regmap;
0186     struct work_struct work;
0187     struct gpio_desc *enable_gpio;
0188     struct regulator *regulator;
0189     u32 magnitude;
0190     u32 mode;
0191     u32 library;
0192     int rated_voltage;
0193     int overdrive_voltage;
0194 };
0195 
0196 static const struct reg_default drv260x_reg_defs[] = {
0197     { DRV260X_STATUS, 0xe0 },
0198     { DRV260X_MODE, 0x40 },
0199     { DRV260X_RT_PB_IN, 0x00 },
0200     { DRV260X_LIB_SEL, 0x00 },
0201     { DRV260X_WV_SEQ_1, 0x01 },
0202     { DRV260X_WV_SEQ_2, 0x00 },
0203     { DRV260X_WV_SEQ_3, 0x00 },
0204     { DRV260X_WV_SEQ_4, 0x00 },
0205     { DRV260X_WV_SEQ_5, 0x00 },
0206     { DRV260X_WV_SEQ_6, 0x00 },
0207     { DRV260X_WV_SEQ_7, 0x00 },
0208     { DRV260X_WV_SEQ_8, 0x00 },
0209     { DRV260X_GO, 0x00 },
0210     { DRV260X_OVERDRIVE_OFF, 0x00 },
0211     { DRV260X_SUSTAIN_P_OFF, 0x00 },
0212     { DRV260X_SUSTAIN_N_OFF, 0x00 },
0213     { DRV260X_BRAKE_OFF, 0x00 },
0214     { DRV260X_A_TO_V_CTRL, 0x05 },
0215     { DRV260X_A_TO_V_MIN_INPUT, 0x19 },
0216     { DRV260X_A_TO_V_MAX_INPUT, 0xff },
0217     { DRV260X_A_TO_V_MIN_OUT, 0x19 },
0218     { DRV260X_A_TO_V_MAX_OUT, 0xff },
0219     { DRV260X_RATED_VOLT, 0x3e },
0220     { DRV260X_OD_CLAMP_VOLT, 0x8c },
0221     { DRV260X_CAL_COMP, 0x0c },
0222     { DRV260X_CAL_BACK_EMF, 0x6c },
0223     { DRV260X_FEEDBACK_CTRL, 0x36 },
0224     { DRV260X_CTRL1, 0x93 },
0225     { DRV260X_CTRL2, 0xfa },
0226     { DRV260X_CTRL3, 0xa0 },
0227     { DRV260X_CTRL4, 0x20 },
0228     { DRV260X_CTRL5, 0x80 },
0229     { DRV260X_LRA_LOOP_PERIOD, 0x33 },
0230     { DRV260X_VBAT_MON, 0x00 },
0231     { DRV260X_LRA_RES_PERIOD, 0x00 },
0232 };
0233 
0234 #define DRV260X_DEF_RATED_VOLT      0x90
0235 #define DRV260X_DEF_OD_CLAMP_VOLT   0x90
0236 
0237 /*
0238  * Rated and Overdriver Voltages:
0239  * Calculated using the formula r = v * 255 / 5.6
0240  * where r is what will be written to the register
0241  * and v is the rated or overdriver voltage of the actuator
0242  */
0243 static int drv260x_calculate_voltage(unsigned int voltage)
0244 {
0245     return (voltage * 255 / 5600);
0246 }
0247 
0248 static void drv260x_worker(struct work_struct *work)
0249 {
0250     struct drv260x_data *haptics = container_of(work, struct drv260x_data, work);
0251     int error;
0252 
0253     gpiod_set_value(haptics->enable_gpio, 1);
0254     /* Data sheet says to wait 250us before trying to communicate */
0255     udelay(250);
0256 
0257     error = regmap_write(haptics->regmap,
0258                  DRV260X_MODE, DRV260X_RT_PLAYBACK);
0259     if (error) {
0260         dev_err(&haptics->client->dev,
0261             "Failed to write set mode: %d\n", error);
0262     } else {
0263         error = regmap_write(haptics->regmap,
0264                      DRV260X_RT_PB_IN, haptics->magnitude);
0265         if (error)
0266             dev_err(&haptics->client->dev,
0267                 "Failed to set magnitude: %d\n", error);
0268     }
0269 }
0270 
0271 static int drv260x_haptics_play(struct input_dev *input, void *data,
0272                 struct ff_effect *effect)
0273 {
0274     struct drv260x_data *haptics = input_get_drvdata(input);
0275 
0276     haptics->mode = DRV260X_LRA_NO_CAL_MODE;
0277 
0278     if (effect->u.rumble.strong_magnitude > 0)
0279         haptics->magnitude = effect->u.rumble.strong_magnitude;
0280     else if (effect->u.rumble.weak_magnitude > 0)
0281         haptics->magnitude = effect->u.rumble.weak_magnitude;
0282     else
0283         haptics->magnitude = 0;
0284 
0285     schedule_work(&haptics->work);
0286 
0287     return 0;
0288 }
0289 
0290 static void drv260x_close(struct input_dev *input)
0291 {
0292     struct drv260x_data *haptics = input_get_drvdata(input);
0293     int error;
0294 
0295     cancel_work_sync(&haptics->work);
0296 
0297     error = regmap_write(haptics->regmap, DRV260X_MODE, DRV260X_STANDBY);
0298     if (error)
0299         dev_err(&haptics->client->dev,
0300             "Failed to enter standby mode: %d\n", error);
0301 
0302     gpiod_set_value(haptics->enable_gpio, 0);
0303 }
0304 
0305 static const struct reg_sequence drv260x_lra_cal_regs[] = {
0306     { DRV260X_MODE, DRV260X_AUTO_CAL },
0307     { DRV260X_CTRL3, DRV260X_NG_THRESH_2 },
0308     { DRV260X_FEEDBACK_CTRL, DRV260X_FB_REG_LRA_MODE |
0309         DRV260X_BRAKE_FACTOR_4X | DRV260X_LOOP_GAIN_HIGH },
0310 };
0311 
0312 static const struct reg_sequence drv260x_lra_init_regs[] = {
0313     { DRV260X_MODE, DRV260X_RT_PLAYBACK },
0314     { DRV260X_A_TO_V_CTRL, DRV260X_AUDIO_HAPTICS_PEAK_20MS |
0315         DRV260X_AUDIO_HAPTICS_FILTER_125HZ },
0316     { DRV260X_A_TO_V_MIN_INPUT, DRV260X_AUDIO_HAPTICS_MIN_IN_VOLT },
0317     { DRV260X_A_TO_V_MAX_INPUT, DRV260X_AUDIO_HAPTICS_MAX_IN_VOLT },
0318     { DRV260X_A_TO_V_MIN_OUT, DRV260X_AUDIO_HAPTICS_MIN_OUT_VOLT },
0319     { DRV260X_A_TO_V_MAX_OUT, DRV260X_AUDIO_HAPTICS_MAX_OUT_VOLT },
0320     { DRV260X_FEEDBACK_CTRL, DRV260X_FB_REG_LRA_MODE |
0321         DRV260X_BRAKE_FACTOR_2X | DRV260X_LOOP_GAIN_MED |
0322         DRV260X_BEMF_GAIN_3 },
0323     { DRV260X_CTRL1, DRV260X_STARTUP_BOOST },
0324     { DRV260X_CTRL2, DRV260X_SAMP_TIME_250 },
0325     { DRV260X_CTRL3, DRV260X_NG_THRESH_2 | DRV260X_ANANLOG_IN },
0326     { DRV260X_CTRL4, DRV260X_AUTOCAL_TIME_500MS },
0327 };
0328 
0329 static const struct reg_sequence drv260x_erm_cal_regs[] = {
0330     { DRV260X_MODE, DRV260X_AUTO_CAL },
0331     { DRV260X_A_TO_V_MIN_INPUT, DRV260X_AUDIO_HAPTICS_MIN_IN_VOLT },
0332     { DRV260X_A_TO_V_MAX_INPUT, DRV260X_AUDIO_HAPTICS_MAX_IN_VOLT },
0333     { DRV260X_A_TO_V_MIN_OUT, DRV260X_AUDIO_HAPTICS_MIN_OUT_VOLT },
0334     { DRV260X_A_TO_V_MAX_OUT, DRV260X_AUDIO_HAPTICS_MAX_OUT_VOLT },
0335     { DRV260X_FEEDBACK_CTRL, DRV260X_BRAKE_FACTOR_3X |
0336         DRV260X_LOOP_GAIN_MED | DRV260X_BEMF_GAIN_2 },
0337     { DRV260X_CTRL1, DRV260X_STARTUP_BOOST },
0338     { DRV260X_CTRL2, DRV260X_SAMP_TIME_250 | DRV260X_BLANK_TIME_75 |
0339         DRV260X_IDISS_TIME_75 },
0340     { DRV260X_CTRL3, DRV260X_NG_THRESH_2 | DRV260X_ERM_OPEN_LOOP },
0341     { DRV260X_CTRL4, DRV260X_AUTOCAL_TIME_500MS },
0342 };
0343 
0344 static int drv260x_init(struct drv260x_data *haptics)
0345 {
0346     int error;
0347     unsigned int cal_buf;
0348 
0349     error = regmap_write(haptics->regmap,
0350                  DRV260X_RATED_VOLT, haptics->rated_voltage);
0351     if (error) {
0352         dev_err(&haptics->client->dev,
0353             "Failed to write DRV260X_RATED_VOLT register: %d\n",
0354             error);
0355         return error;
0356     }
0357 
0358     error = regmap_write(haptics->regmap,
0359                  DRV260X_OD_CLAMP_VOLT, haptics->overdrive_voltage);
0360     if (error) {
0361         dev_err(&haptics->client->dev,
0362             "Failed to write DRV260X_OD_CLAMP_VOLT register: %d\n",
0363             error);
0364         return error;
0365     }
0366 
0367     switch (haptics->mode) {
0368     case DRV260X_LRA_MODE:
0369         error = regmap_register_patch(haptics->regmap,
0370                           drv260x_lra_cal_regs,
0371                           ARRAY_SIZE(drv260x_lra_cal_regs));
0372         if (error) {
0373             dev_err(&haptics->client->dev,
0374                 "Failed to write LRA calibration registers: %d\n",
0375                 error);
0376             return error;
0377         }
0378 
0379         break;
0380 
0381     case DRV260X_ERM_MODE:
0382         error = regmap_register_patch(haptics->regmap,
0383                           drv260x_erm_cal_regs,
0384                           ARRAY_SIZE(drv260x_erm_cal_regs));
0385         if (error) {
0386             dev_err(&haptics->client->dev,
0387                 "Failed to write ERM calibration registers: %d\n",
0388                 error);
0389             return error;
0390         }
0391 
0392         error = regmap_update_bits(haptics->regmap, DRV260X_LIB_SEL,
0393                        DRV260X_LIB_SEL_MASK,
0394                        haptics->library);
0395         if (error) {
0396             dev_err(&haptics->client->dev,
0397                 "Failed to write DRV260X_LIB_SEL register: %d\n",
0398                 error);
0399             return error;
0400         }
0401 
0402         break;
0403 
0404     default:
0405         error = regmap_register_patch(haptics->regmap,
0406                           drv260x_lra_init_regs,
0407                           ARRAY_SIZE(drv260x_lra_init_regs));
0408         if (error) {
0409             dev_err(&haptics->client->dev,
0410                 "Failed to write LRA init registers: %d\n",
0411                 error);
0412             return error;
0413         }
0414 
0415         error = regmap_update_bits(haptics->regmap, DRV260X_LIB_SEL,
0416                        DRV260X_LIB_SEL_MASK,
0417                        haptics->library);
0418         if (error) {
0419             dev_err(&haptics->client->dev,
0420                 "Failed to write DRV260X_LIB_SEL register: %d\n",
0421                 error);
0422             return error;
0423         }
0424 
0425         /* No need to set GO bit here */
0426         return 0;
0427     }
0428 
0429     error = regmap_write(haptics->regmap, DRV260X_GO, DRV260X_GO_BIT);
0430     if (error) {
0431         dev_err(&haptics->client->dev,
0432             "Failed to write GO register: %d\n",
0433             error);
0434         return error;
0435     }
0436 
0437     do {
0438         error = regmap_read(haptics->regmap, DRV260X_GO, &cal_buf);
0439         if (error) {
0440             dev_err(&haptics->client->dev,
0441                 "Failed to read GO register: %d\n",
0442                 error);
0443             return error;
0444         }
0445     } while (cal_buf == DRV260X_GO_BIT);
0446 
0447     return 0;
0448 }
0449 
0450 static const struct regmap_config drv260x_regmap_config = {
0451     .reg_bits = 8,
0452     .val_bits = 8,
0453 
0454     .max_register = DRV260X_MAX_REG,
0455     .reg_defaults = drv260x_reg_defs,
0456     .num_reg_defaults = ARRAY_SIZE(drv260x_reg_defs),
0457     .cache_type = REGCACHE_NONE,
0458 };
0459 
0460 static int drv260x_probe(struct i2c_client *client,
0461              const struct i2c_device_id *id)
0462 {
0463     struct device *dev = &client->dev;
0464     struct drv260x_data *haptics;
0465     u32 voltage;
0466     int error;
0467 
0468     haptics = devm_kzalloc(dev, sizeof(*haptics), GFP_KERNEL);
0469     if (!haptics)
0470         return -ENOMEM;
0471 
0472     error = device_property_read_u32(dev, "mode", &haptics->mode);
0473     if (error) {
0474         dev_err(dev, "Can't fetch 'mode' property: %d\n", error);
0475         return error;
0476     }
0477 
0478     if (haptics->mode < DRV260X_LRA_MODE ||
0479         haptics->mode > DRV260X_ERM_MODE) {
0480         dev_err(dev, "Vibrator mode is invalid: %i\n", haptics->mode);
0481         return -EINVAL;
0482     }
0483 
0484     error = device_property_read_u32(dev, "library-sel", &haptics->library);
0485     if (error) {
0486         dev_err(dev, "Can't fetch 'library-sel' property: %d\n", error);
0487         return error;
0488     }
0489 
0490     if (haptics->library < DRV260X_LIB_EMPTY ||
0491         haptics->library > DRV260X_ERM_LIB_F) {
0492         dev_err(dev,
0493             "Library value is invalid: %i\n", haptics->library);
0494         return -EINVAL;
0495     }
0496 
0497     if (haptics->mode == DRV260X_LRA_MODE &&
0498         haptics->library != DRV260X_LIB_EMPTY &&
0499         haptics->library != DRV260X_LIB_LRA) {
0500         dev_err(dev, "LRA Mode with ERM Library mismatch\n");
0501         return -EINVAL;
0502     }
0503 
0504     if (haptics->mode == DRV260X_ERM_MODE &&
0505         (haptics->library == DRV260X_LIB_EMPTY ||
0506          haptics->library == DRV260X_LIB_LRA)) {
0507         dev_err(dev, "ERM Mode with LRA Library mismatch\n");
0508         return -EINVAL;
0509     }
0510 
0511     error = device_property_read_u32(dev, "vib-rated-mv", &voltage);
0512     haptics->rated_voltage = error ? DRV260X_DEF_RATED_VOLT :
0513                      drv260x_calculate_voltage(voltage);
0514 
0515     error = device_property_read_u32(dev, "vib-overdrive-mv", &voltage);
0516     haptics->overdrive_voltage = error ? DRV260X_DEF_OD_CLAMP_VOLT :
0517                          drv260x_calculate_voltage(voltage);
0518 
0519     haptics->regulator = devm_regulator_get(dev, "vbat");
0520     if (IS_ERR(haptics->regulator)) {
0521         error = PTR_ERR(haptics->regulator);
0522         dev_err(dev, "unable to get regulator, error: %d\n", error);
0523         return error;
0524     }
0525 
0526     haptics->enable_gpio = devm_gpiod_get_optional(dev, "enable",
0527                                GPIOD_OUT_HIGH);
0528     if (IS_ERR(haptics->enable_gpio))
0529         return PTR_ERR(haptics->enable_gpio);
0530 
0531     haptics->input_dev = devm_input_allocate_device(dev);
0532     if (!haptics->input_dev) {
0533         dev_err(dev, "Failed to allocate input device\n");
0534         return -ENOMEM;
0535     }
0536 
0537     haptics->input_dev->name = "drv260x:haptics";
0538     haptics->input_dev->close = drv260x_close;
0539     input_set_drvdata(haptics->input_dev, haptics);
0540     input_set_capability(haptics->input_dev, EV_FF, FF_RUMBLE);
0541 
0542     error = input_ff_create_memless(haptics->input_dev, NULL,
0543                     drv260x_haptics_play);
0544     if (error) {
0545         dev_err(dev, "input_ff_create() failed: %d\n", error);
0546         return error;
0547     }
0548 
0549     INIT_WORK(&haptics->work, drv260x_worker);
0550 
0551     haptics->client = client;
0552     i2c_set_clientdata(client, haptics);
0553 
0554     haptics->regmap = devm_regmap_init_i2c(client, &drv260x_regmap_config);
0555     if (IS_ERR(haptics->regmap)) {
0556         error = PTR_ERR(haptics->regmap);
0557         dev_err(dev, "Failed to allocate register map: %d\n", error);
0558         return error;
0559     }
0560 
0561     error = drv260x_init(haptics);
0562     if (error) {
0563         dev_err(dev, "Device init failed: %d\n", error);
0564         return error;
0565     }
0566 
0567     error = input_register_device(haptics->input_dev);
0568     if (error) {
0569         dev_err(dev, "couldn't register input device: %d\n", error);
0570         return error;
0571     }
0572 
0573     return 0;
0574 }
0575 
0576 static int __maybe_unused drv260x_suspend(struct device *dev)
0577 {
0578     struct drv260x_data *haptics = dev_get_drvdata(dev);
0579     int ret = 0;
0580 
0581     mutex_lock(&haptics->input_dev->mutex);
0582 
0583     if (input_device_enabled(haptics->input_dev)) {
0584         ret = regmap_update_bits(haptics->regmap,
0585                      DRV260X_MODE,
0586                      DRV260X_STANDBY_MASK,
0587                      DRV260X_STANDBY);
0588         if (ret) {
0589             dev_err(dev, "Failed to set standby mode\n");
0590             goto out;
0591         }
0592 
0593         gpiod_set_value(haptics->enable_gpio, 0);
0594 
0595         ret = regulator_disable(haptics->regulator);
0596         if (ret) {
0597             dev_err(dev, "Failed to disable regulator\n");
0598             regmap_update_bits(haptics->regmap,
0599                        DRV260X_MODE,
0600                        DRV260X_STANDBY_MASK, 0);
0601         }
0602     }
0603 out:
0604     mutex_unlock(&haptics->input_dev->mutex);
0605     return ret;
0606 }
0607 
0608 static int __maybe_unused drv260x_resume(struct device *dev)
0609 {
0610     struct drv260x_data *haptics = dev_get_drvdata(dev);
0611     int ret = 0;
0612 
0613     mutex_lock(&haptics->input_dev->mutex);
0614 
0615     if (input_device_enabled(haptics->input_dev)) {
0616         ret = regulator_enable(haptics->regulator);
0617         if (ret) {
0618             dev_err(dev, "Failed to enable regulator\n");
0619             goto out;
0620         }
0621 
0622         ret = regmap_update_bits(haptics->regmap,
0623                      DRV260X_MODE,
0624                      DRV260X_STANDBY_MASK, 0);
0625         if (ret) {
0626             dev_err(dev, "Failed to unset standby mode\n");
0627             regulator_disable(haptics->regulator);
0628             goto out;
0629         }
0630 
0631         gpiod_set_value(haptics->enable_gpio, 1);
0632     }
0633 
0634 out:
0635     mutex_unlock(&haptics->input_dev->mutex);
0636     return ret;
0637 }
0638 
0639 static SIMPLE_DEV_PM_OPS(drv260x_pm_ops, drv260x_suspend, drv260x_resume);
0640 
0641 static const struct i2c_device_id drv260x_id[] = {
0642     { "drv2605l", 0 },
0643     { }
0644 };
0645 MODULE_DEVICE_TABLE(i2c, drv260x_id);
0646 
0647 static const struct of_device_id drv260x_of_match[] = {
0648     { .compatible = "ti,drv2604", },
0649     { .compatible = "ti,drv2604l", },
0650     { .compatible = "ti,drv2605", },
0651     { .compatible = "ti,drv2605l", },
0652     { }
0653 };
0654 MODULE_DEVICE_TABLE(of, drv260x_of_match);
0655 
0656 static struct i2c_driver drv260x_driver = {
0657     .probe      = drv260x_probe,
0658     .driver     = {
0659         .name   = "drv260x-haptics",
0660         .of_match_table = drv260x_of_match,
0661         .pm = &drv260x_pm_ops,
0662     },
0663     .id_table = drv260x_id,
0664 };
0665 module_i2c_driver(drv260x_driver);
0666 
0667 MODULE_DESCRIPTION("TI DRV260x haptics driver");
0668 MODULE_LICENSE("GPL");
0669 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");