Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * DRV2667 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/platform_device.h>
0014 #include <linux/regmap.h>
0015 #include <linux/slab.h>
0016 #include <linux/delay.h>
0017 #include <linux/regulator/consumer.h>
0018 
0019 /* Contol registers */
0020 #define DRV2667_STATUS  0x00
0021 #define DRV2667_CTRL_1  0x01
0022 #define DRV2667_CTRL_2  0x02
0023 /* Waveform sequencer */
0024 #define DRV2667_WV_SEQ_0    0x03
0025 #define DRV2667_WV_SEQ_1    0x04
0026 #define DRV2667_WV_SEQ_2    0x05
0027 #define DRV2667_WV_SEQ_3    0x06
0028 #define DRV2667_WV_SEQ_4    0x07
0029 #define DRV2667_WV_SEQ_5    0x08
0030 #define DRV2667_WV_SEQ_6    0x09
0031 #define DRV2667_WV_SEQ_7    0x0A
0032 #define DRV2667_FIFO        0x0B
0033 #define DRV2667_PAGE        0xFF
0034 #define DRV2667_MAX_REG     DRV2667_PAGE
0035 
0036 #define DRV2667_PAGE_0      0x00
0037 #define DRV2667_PAGE_1      0x01
0038 #define DRV2667_PAGE_2      0x02
0039 #define DRV2667_PAGE_3      0x03
0040 #define DRV2667_PAGE_4      0x04
0041 #define DRV2667_PAGE_5      0x05
0042 #define DRV2667_PAGE_6      0x06
0043 #define DRV2667_PAGE_7      0x07
0044 #define DRV2667_PAGE_8      0x08
0045 
0046 /* RAM fields */
0047 #define DRV2667_RAM_HDR_SZ  0x0
0048 /* RAM Header addresses */
0049 #define DRV2667_RAM_START_HI    0x01
0050 #define DRV2667_RAM_START_LO    0x02
0051 #define DRV2667_RAM_STOP_HI     0x03
0052 #define DRV2667_RAM_STOP_LO     0x04
0053 #define DRV2667_RAM_REPEAT_CT   0x05
0054 /* RAM data addresses */
0055 #define DRV2667_RAM_AMP     0x06
0056 #define DRV2667_RAM_FREQ    0x07
0057 #define DRV2667_RAM_DURATION    0x08
0058 #define DRV2667_RAM_ENVELOPE    0x09
0059 
0060 /* Control 1 Register */
0061 #define DRV2667_25_VPP_GAIN     0x00
0062 #define DRV2667_50_VPP_GAIN     0x01
0063 #define DRV2667_75_VPP_GAIN     0x02
0064 #define DRV2667_100_VPP_GAIN    0x03
0065 #define DRV2667_DIGITAL_IN      0xfc
0066 #define DRV2667_ANALOG_IN       (1 << 2)
0067 
0068 /* Control 2 Register */
0069 #define DRV2667_GO          (1 << 0)
0070 #define DRV2667_STANDBY     (1 << 6)
0071 #define DRV2667_DEV_RST     (1 << 7)
0072 
0073 /* RAM Envelope settings */
0074 #define DRV2667_NO_ENV          0x00
0075 #define DRV2667_32_MS_ENV       0x01
0076 #define DRV2667_64_MS_ENV       0x02
0077 #define DRV2667_96_MS_ENV       0x03
0078 #define DRV2667_128_MS_ENV      0x04
0079 #define DRV2667_160_MS_ENV      0x05
0080 #define DRV2667_192_MS_ENV      0x06
0081 #define DRV2667_224_MS_ENV      0x07
0082 #define DRV2667_256_MS_ENV      0x08
0083 #define DRV2667_512_MS_ENV      0x09
0084 #define DRV2667_768_MS_ENV      0x0a
0085 #define DRV2667_1024_MS_ENV     0x0b
0086 #define DRV2667_1280_MS_ENV     0x0c
0087 #define DRV2667_1536_MS_ENV     0x0d
0088 #define DRV2667_1792_MS_ENV     0x0e
0089 #define DRV2667_2048_MS_ENV     0x0f
0090 
0091 /**
0092  * struct drv2667_data -
0093  * @input_dev: Pointer to the input device
0094  * @client: Pointer to the I2C client
0095  * @regmap: Register map of the device
0096  * @work: Work item used to off load the enable/disable of the vibration
0097  * @regulator: Pointer to the regulator for the IC
0098  * @page: Page number
0099  * @magnitude: Magnitude of the vibration event
0100  * @frequency: Frequency of the vibration event
0101 **/
0102 struct drv2667_data {
0103     struct input_dev *input_dev;
0104     struct i2c_client *client;
0105     struct regmap *regmap;
0106     struct work_struct work;
0107     struct regulator *regulator;
0108     u32 page;
0109     u32 magnitude;
0110     u32 frequency;
0111 };
0112 
0113 static const struct reg_default drv2667_reg_defs[] = {
0114     { DRV2667_STATUS, 0x02 },
0115     { DRV2667_CTRL_1, 0x28 },
0116     { DRV2667_CTRL_2, 0x40 },
0117     { DRV2667_WV_SEQ_0, 0x00 },
0118     { DRV2667_WV_SEQ_1, 0x00 },
0119     { DRV2667_WV_SEQ_2, 0x00 },
0120     { DRV2667_WV_SEQ_3, 0x00 },
0121     { DRV2667_WV_SEQ_4, 0x00 },
0122     { DRV2667_WV_SEQ_5, 0x00 },
0123     { DRV2667_WV_SEQ_6, 0x00 },
0124     { DRV2667_WV_SEQ_7, 0x00 },
0125     { DRV2667_FIFO, 0x00 },
0126     { DRV2667_PAGE, 0x00 },
0127 };
0128 
0129 static int drv2667_set_waveform_freq(struct drv2667_data *haptics)
0130 {
0131     unsigned int read_buf;
0132     int freq;
0133     int error;
0134 
0135     /* Per the data sheet:
0136      * Sinusoid Frequency (Hz) = 7.8125 x Frequency
0137      */
0138     freq = (haptics->frequency * 1000) / 78125;
0139     if (freq <= 0) {
0140         dev_err(&haptics->client->dev,
0141             "ERROR: Frequency calculated to %i\n", freq);
0142         return -EINVAL;
0143     }
0144 
0145     error = regmap_read(haptics->regmap, DRV2667_PAGE, &read_buf);
0146     if (error) {
0147         dev_err(&haptics->client->dev,
0148             "Failed to read the page number: %d\n", error);
0149         return -EIO;
0150     }
0151 
0152     if (read_buf == DRV2667_PAGE_0 ||
0153         haptics->page != read_buf) {
0154         error = regmap_write(haptics->regmap,
0155                 DRV2667_PAGE, haptics->page);
0156         if (error) {
0157             dev_err(&haptics->client->dev,
0158                 "Failed to set the page: %d\n", error);
0159             return -EIO;
0160         }
0161     }
0162 
0163     error = regmap_write(haptics->regmap, DRV2667_RAM_FREQ, freq);
0164     if (error)
0165         dev_err(&haptics->client->dev,
0166                 "Failed to set the frequency: %d\n", error);
0167 
0168     /* Reset back to original page */
0169     if (read_buf == DRV2667_PAGE_0 ||
0170         haptics->page != read_buf) {
0171         error = regmap_write(haptics->regmap, DRV2667_PAGE, read_buf);
0172         if (error) {
0173             dev_err(&haptics->client->dev,
0174                 "Failed to set the page: %d\n", error);
0175             return -EIO;
0176         }
0177     }
0178 
0179     return error;
0180 }
0181 
0182 static void drv2667_worker(struct work_struct *work)
0183 {
0184     struct drv2667_data *haptics = container_of(work, struct drv2667_data, work);
0185     int error;
0186 
0187     if (haptics->magnitude) {
0188         error = regmap_write(haptics->regmap,
0189                 DRV2667_PAGE, haptics->page);
0190         if (error) {
0191             dev_err(&haptics->client->dev,
0192                 "Failed to set the page: %d\n", error);
0193             return;
0194         }
0195 
0196         error = regmap_write(haptics->regmap, DRV2667_RAM_AMP,
0197                 haptics->magnitude);
0198         if (error) {
0199             dev_err(&haptics->client->dev,
0200                 "Failed to set the amplitude: %d\n", error);
0201             return;
0202         }
0203 
0204         error = regmap_write(haptics->regmap,
0205                 DRV2667_PAGE, DRV2667_PAGE_0);
0206         if (error) {
0207             dev_err(&haptics->client->dev,
0208                 "Failed to set the page: %d\n", error);
0209             return;
0210         }
0211 
0212         error = regmap_write(haptics->regmap,
0213                 DRV2667_CTRL_2, DRV2667_GO);
0214         if (error) {
0215             dev_err(&haptics->client->dev,
0216                 "Failed to set the GO bit: %d\n", error);
0217         }
0218     } else {
0219         error = regmap_update_bits(haptics->regmap, DRV2667_CTRL_2,
0220                 DRV2667_GO, 0);
0221         if (error) {
0222             dev_err(&haptics->client->dev,
0223                 "Failed to unset the GO bit: %d\n", error);
0224         }
0225     }
0226 }
0227 
0228 static int drv2667_haptics_play(struct input_dev *input, void *data,
0229                 struct ff_effect *effect)
0230 {
0231     struct drv2667_data *haptics = input_get_drvdata(input);
0232 
0233     if (effect->u.rumble.strong_magnitude > 0)
0234         haptics->magnitude = effect->u.rumble.strong_magnitude;
0235     else if (effect->u.rumble.weak_magnitude > 0)
0236         haptics->magnitude = effect->u.rumble.weak_magnitude;
0237     else
0238         haptics->magnitude = 0;
0239 
0240     schedule_work(&haptics->work);
0241 
0242     return 0;
0243 }
0244 
0245 static void drv2667_close(struct input_dev *input)
0246 {
0247     struct drv2667_data *haptics = input_get_drvdata(input);
0248     int error;
0249 
0250     cancel_work_sync(&haptics->work);
0251 
0252     error = regmap_update_bits(haptics->regmap, DRV2667_CTRL_2,
0253                    DRV2667_STANDBY, DRV2667_STANDBY);
0254     if (error)
0255         dev_err(&haptics->client->dev,
0256             "Failed to enter standby mode: %d\n", error);
0257 }
0258 
0259 static const struct reg_sequence drv2667_init_regs[] = {
0260     { DRV2667_CTRL_2, 0 },
0261     { DRV2667_CTRL_1, DRV2667_25_VPP_GAIN },
0262     { DRV2667_WV_SEQ_0, 1 },
0263     { DRV2667_WV_SEQ_1, 0 }
0264 };
0265 
0266 static const struct reg_sequence drv2667_page1_init[] = {
0267     { DRV2667_RAM_HDR_SZ, 0x05 },
0268     { DRV2667_RAM_START_HI, 0x80 },
0269     { DRV2667_RAM_START_LO, 0x06 },
0270     { DRV2667_RAM_STOP_HI, 0x00 },
0271     { DRV2667_RAM_STOP_LO, 0x09 },
0272     { DRV2667_RAM_REPEAT_CT, 0 },
0273     { DRV2667_RAM_DURATION, 0x05 },
0274     { DRV2667_RAM_ENVELOPE, DRV2667_NO_ENV },
0275     { DRV2667_RAM_AMP, 0x60 },
0276 };
0277 
0278 static int drv2667_init(struct drv2667_data *haptics)
0279 {
0280     int error;
0281 
0282     /* Set default haptic frequency to 195Hz on Page 1*/
0283     haptics->frequency = 195;
0284     haptics->page = DRV2667_PAGE_1;
0285 
0286     error = regmap_register_patch(haptics->regmap,
0287                       drv2667_init_regs,
0288                       ARRAY_SIZE(drv2667_init_regs));
0289     if (error) {
0290         dev_err(&haptics->client->dev,
0291             "Failed to write init registers: %d\n",
0292             error);
0293         return error;
0294     }
0295 
0296     error = regmap_write(haptics->regmap, DRV2667_PAGE, haptics->page);
0297     if (error) {
0298         dev_err(&haptics->client->dev, "Failed to set page: %d\n",
0299             error);
0300         goto error_out;
0301     }
0302 
0303     error = drv2667_set_waveform_freq(haptics);
0304     if (error)
0305         goto error_page;
0306 
0307     error = regmap_register_patch(haptics->regmap,
0308                       drv2667_page1_init,
0309                       ARRAY_SIZE(drv2667_page1_init));
0310     if (error) {
0311         dev_err(&haptics->client->dev,
0312             "Failed to write page registers: %d\n",
0313             error);
0314         return error;
0315     }
0316 
0317     error = regmap_write(haptics->regmap, DRV2667_PAGE, DRV2667_PAGE_0);
0318     return error;
0319 
0320 error_page:
0321     regmap_write(haptics->regmap, DRV2667_PAGE, DRV2667_PAGE_0);
0322 error_out:
0323     return error;
0324 }
0325 
0326 static const struct regmap_config drv2667_regmap_config = {
0327     .reg_bits = 8,
0328     .val_bits = 8,
0329 
0330     .max_register = DRV2667_MAX_REG,
0331     .reg_defaults = drv2667_reg_defs,
0332     .num_reg_defaults = ARRAY_SIZE(drv2667_reg_defs),
0333     .cache_type = REGCACHE_NONE,
0334 };
0335 
0336 static int drv2667_probe(struct i2c_client *client,
0337              const struct i2c_device_id *id)
0338 {
0339     struct drv2667_data *haptics;
0340     int error;
0341 
0342     haptics = devm_kzalloc(&client->dev, sizeof(*haptics), GFP_KERNEL);
0343     if (!haptics)
0344         return -ENOMEM;
0345 
0346     haptics->regulator = devm_regulator_get(&client->dev, "vbat");
0347     if (IS_ERR(haptics->regulator)) {
0348         error = PTR_ERR(haptics->regulator);
0349         dev_err(&client->dev,
0350             "unable to get regulator, error: %d\n", error);
0351         return error;
0352     }
0353 
0354     haptics->input_dev = devm_input_allocate_device(&client->dev);
0355     if (!haptics->input_dev) {
0356         dev_err(&client->dev, "Failed to allocate input device\n");
0357         return -ENOMEM;
0358     }
0359 
0360     haptics->input_dev->name = "drv2667:haptics";
0361     haptics->input_dev->dev.parent = client->dev.parent;
0362     haptics->input_dev->close = drv2667_close;
0363     input_set_drvdata(haptics->input_dev, haptics);
0364     input_set_capability(haptics->input_dev, EV_FF, FF_RUMBLE);
0365 
0366     error = input_ff_create_memless(haptics->input_dev, NULL,
0367                     drv2667_haptics_play);
0368     if (error) {
0369         dev_err(&client->dev, "input_ff_create() failed: %d\n",
0370             error);
0371         return error;
0372     }
0373 
0374     INIT_WORK(&haptics->work, drv2667_worker);
0375 
0376     haptics->client = client;
0377     i2c_set_clientdata(client, haptics);
0378 
0379     haptics->regmap = devm_regmap_init_i2c(client, &drv2667_regmap_config);
0380     if (IS_ERR(haptics->regmap)) {
0381         error = PTR_ERR(haptics->regmap);
0382         dev_err(&client->dev, "Failed to allocate register map: %d\n",
0383             error);
0384         return error;
0385     }
0386 
0387     error = drv2667_init(haptics);
0388     if (error) {
0389         dev_err(&client->dev, "Device init failed: %d\n", error);
0390         return error;
0391     }
0392 
0393     error = input_register_device(haptics->input_dev);
0394     if (error) {
0395         dev_err(&client->dev, "couldn't register input device: %d\n",
0396             error);
0397         return error;
0398     }
0399 
0400     return 0;
0401 }
0402 
0403 static int __maybe_unused drv2667_suspend(struct device *dev)
0404 {
0405     struct drv2667_data *haptics = dev_get_drvdata(dev);
0406     int ret = 0;
0407 
0408     mutex_lock(&haptics->input_dev->mutex);
0409 
0410     if (input_device_enabled(haptics->input_dev)) {
0411         ret = regmap_update_bits(haptics->regmap, DRV2667_CTRL_2,
0412                      DRV2667_STANDBY, DRV2667_STANDBY);
0413         if (ret) {
0414             dev_err(dev, "Failed to set standby mode\n");
0415             regulator_disable(haptics->regulator);
0416             goto out;
0417         }
0418 
0419         ret = regulator_disable(haptics->regulator);
0420         if (ret) {
0421             dev_err(dev, "Failed to disable regulator\n");
0422             regmap_update_bits(haptics->regmap,
0423                        DRV2667_CTRL_2,
0424                        DRV2667_STANDBY, 0);
0425         }
0426     }
0427 out:
0428     mutex_unlock(&haptics->input_dev->mutex);
0429     return ret;
0430 }
0431 
0432 static int __maybe_unused drv2667_resume(struct device *dev)
0433 {
0434     struct drv2667_data *haptics = dev_get_drvdata(dev);
0435     int ret = 0;
0436 
0437     mutex_lock(&haptics->input_dev->mutex);
0438 
0439     if (input_device_enabled(haptics->input_dev)) {
0440         ret = regulator_enable(haptics->regulator);
0441         if (ret) {
0442             dev_err(dev, "Failed to enable regulator\n");
0443             goto out;
0444         }
0445 
0446         ret = regmap_update_bits(haptics->regmap, DRV2667_CTRL_2,
0447                      DRV2667_STANDBY, 0);
0448         if (ret) {
0449             dev_err(dev, "Failed to unset standby mode\n");
0450             regulator_disable(haptics->regulator);
0451             goto out;
0452         }
0453 
0454     }
0455 
0456 out:
0457     mutex_unlock(&haptics->input_dev->mutex);
0458     return ret;
0459 }
0460 
0461 static SIMPLE_DEV_PM_OPS(drv2667_pm_ops, drv2667_suspend, drv2667_resume);
0462 
0463 static const struct i2c_device_id drv2667_id[] = {
0464     { "drv2667", 0 },
0465     { }
0466 };
0467 MODULE_DEVICE_TABLE(i2c, drv2667_id);
0468 
0469 #ifdef CONFIG_OF
0470 static const struct of_device_id drv2667_of_match[] = {
0471     { .compatible = "ti,drv2667", },
0472     { }
0473 };
0474 MODULE_DEVICE_TABLE(of, drv2667_of_match);
0475 #endif
0476 
0477 static struct i2c_driver drv2667_driver = {
0478     .probe      = drv2667_probe,
0479     .driver     = {
0480         .name   = "drv2667-haptics",
0481         .of_match_table = of_match_ptr(drv2667_of_match),
0482         .pm = &drv2667_pm_ops,
0483     },
0484     .id_table = drv2667_id,
0485 };
0486 module_i2c_driver(drv2667_driver);
0487 
0488 MODULE_DESCRIPTION("TI DRV2667 haptics driver");
0489 MODULE_LICENSE("GPL");
0490 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");