Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * twl6040-vibra.c - TWL6040 Vibrator driver
0004  *
0005  * Author:      Jorge Eduardo Candelaria <jorge.candelaria@ti.com>
0006  * Author:      Misael Lopez Cruz <misael.lopez@ti.com>
0007  *
0008  * Copyright:   (C) 2011 Texas Instruments, Inc.
0009  *
0010  * Based on twl4030-vibra.c by Henrik Saari <henrik.saari@nokia.com>
0011  *              Felipe Balbi <felipe.balbi@nokia.com>
0012  *              Jari Vanhala <ext-javi.vanhala@nokia.com>
0013  */
0014 #include <linux/module.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/of.h>
0017 #include <linux/workqueue.h>
0018 #include <linux/input.h>
0019 #include <linux/mfd/twl6040.h>
0020 #include <linux/slab.h>
0021 #include <linux/delay.h>
0022 #include <linux/regulator/consumer.h>
0023 
0024 #define EFFECT_DIR_180_DEG  0x8000
0025 
0026 /* Recommended modulation index 85% */
0027 #define TWL6040_VIBRA_MOD   85
0028 
0029 #define TWL6040_NUM_SUPPLIES 2
0030 
0031 struct vibra_info {
0032     struct device *dev;
0033     struct input_dev *input_dev;
0034     struct work_struct play_work;
0035 
0036     int irq;
0037 
0038     bool enabled;
0039     int weak_speed;
0040     int strong_speed;
0041     int direction;
0042 
0043     unsigned int vibldrv_res;
0044     unsigned int vibrdrv_res;
0045     unsigned int viblmotor_res;
0046     unsigned int vibrmotor_res;
0047 
0048     struct regulator_bulk_data supplies[TWL6040_NUM_SUPPLIES];
0049 
0050     struct twl6040 *twl6040;
0051 };
0052 
0053 static irqreturn_t twl6040_vib_irq_handler(int irq, void *data)
0054 {
0055     struct vibra_info *info = data;
0056     struct twl6040 *twl6040 = info->twl6040;
0057     u8 status;
0058 
0059     status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
0060     if (status & TWL6040_VIBLOCDET) {
0061         dev_warn(info->dev, "Left Vibrator overcurrent detected\n");
0062         twl6040_clear_bits(twl6040, TWL6040_REG_VIBCTLL,
0063                    TWL6040_VIBENA);
0064     }
0065     if (status & TWL6040_VIBROCDET) {
0066         dev_warn(info->dev, "Right Vibrator overcurrent detected\n");
0067         twl6040_clear_bits(twl6040, TWL6040_REG_VIBCTLR,
0068                    TWL6040_VIBENA);
0069     }
0070 
0071     return IRQ_HANDLED;
0072 }
0073 
0074 static void twl6040_vibra_enable(struct vibra_info *info)
0075 {
0076     struct twl6040 *twl6040 = info->twl6040;
0077     int ret;
0078 
0079     ret = regulator_bulk_enable(ARRAY_SIZE(info->supplies), info->supplies);
0080     if (ret) {
0081         dev_err(info->dev, "failed to enable regulators %d\n", ret);
0082         return;
0083     }
0084 
0085     twl6040_power(info->twl6040, 1);
0086     if (twl6040_get_revid(twl6040) <= TWL6040_REV_ES1_1) {
0087         /*
0088          * ERRATA: Disable overcurrent protection for at least
0089          * 3ms when enabling vibrator drivers to avoid false
0090          * overcurrent detection
0091          */
0092         twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL,
0093                   TWL6040_VIBENA | TWL6040_VIBCTRL);
0094         twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR,
0095                   TWL6040_VIBENA | TWL6040_VIBCTRL);
0096         usleep_range(3000, 3500);
0097     }
0098 
0099     twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL,
0100               TWL6040_VIBENA);
0101     twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR,
0102               TWL6040_VIBENA);
0103 
0104     info->enabled = true;
0105 }
0106 
0107 static void twl6040_vibra_disable(struct vibra_info *info)
0108 {
0109     struct twl6040 *twl6040 = info->twl6040;
0110 
0111     twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL, 0x00);
0112     twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR, 0x00);
0113     twl6040_power(info->twl6040, 0);
0114 
0115     regulator_bulk_disable(ARRAY_SIZE(info->supplies), info->supplies);
0116 
0117     info->enabled = false;
0118 }
0119 
0120 static u8 twl6040_vibra_code(int vddvib, int vibdrv_res, int motor_res,
0121                  int speed, int direction)
0122 {
0123     int vpk, max_code;
0124     u8 vibdat;
0125 
0126     /* output swing */
0127     vpk = (vddvib * motor_res * TWL6040_VIBRA_MOD) /
0128         (100 * (vibdrv_res + motor_res));
0129 
0130     /* 50mV per VIBDAT code step */
0131     max_code = vpk / 50;
0132     if (max_code > TWL6040_VIBDAT_MAX)
0133         max_code = TWL6040_VIBDAT_MAX;
0134 
0135     /* scale speed to max allowed code */
0136     vibdat = (u8)((speed * max_code) / USHRT_MAX);
0137 
0138     /* 2's complement for direction > 180 degrees */
0139     vibdat *= direction;
0140 
0141     return vibdat;
0142 }
0143 
0144 static void twl6040_vibra_set_effect(struct vibra_info *info)
0145 {
0146     struct twl6040 *twl6040 = info->twl6040;
0147     u8 vibdatl, vibdatr;
0148     int volt;
0149 
0150     /* weak motor */
0151     volt = regulator_get_voltage(info->supplies[0].consumer) / 1000;
0152     vibdatl = twl6040_vibra_code(volt, info->vibldrv_res,
0153                      info->viblmotor_res,
0154                      info->weak_speed, info->direction);
0155 
0156     /* strong motor */
0157     volt = regulator_get_voltage(info->supplies[1].consumer) / 1000;
0158     vibdatr = twl6040_vibra_code(volt, info->vibrdrv_res,
0159                      info->vibrmotor_res,
0160                      info->strong_speed, info->direction);
0161 
0162     twl6040_reg_write(twl6040, TWL6040_REG_VIBDATL, vibdatl);
0163     twl6040_reg_write(twl6040, TWL6040_REG_VIBDATR, vibdatr);
0164 }
0165 
0166 static void vibra_play_work(struct work_struct *work)
0167 {
0168     struct vibra_info *info = container_of(work,
0169                 struct vibra_info, play_work);
0170     int ret;
0171 
0172     /* Do not allow effect, while the routing is set to use audio */
0173     ret = twl6040_get_vibralr_status(info->twl6040);
0174     if (ret & TWL6040_VIBSEL) {
0175         dev_info(info->dev, "Vibra is configured for audio\n");
0176         return;
0177     }
0178 
0179     if (info->weak_speed || info->strong_speed) {
0180         if (!info->enabled)
0181             twl6040_vibra_enable(info);
0182 
0183         twl6040_vibra_set_effect(info);
0184     } else if (info->enabled)
0185         twl6040_vibra_disable(info);
0186 
0187 }
0188 
0189 static int vibra_play(struct input_dev *input, void *data,
0190               struct ff_effect *effect)
0191 {
0192     struct vibra_info *info = input_get_drvdata(input);
0193 
0194     info->weak_speed = effect->u.rumble.weak_magnitude;
0195     info->strong_speed = effect->u.rumble.strong_magnitude;
0196     info->direction = effect->direction < EFFECT_DIR_180_DEG ? 1 : -1;
0197 
0198     schedule_work(&info->play_work);
0199 
0200     return 0;
0201 }
0202 
0203 static void twl6040_vibra_close(struct input_dev *input)
0204 {
0205     struct vibra_info *info = input_get_drvdata(input);
0206 
0207     cancel_work_sync(&info->play_work);
0208 
0209     if (info->enabled)
0210         twl6040_vibra_disable(info);
0211 }
0212 
0213 static int __maybe_unused twl6040_vibra_suspend(struct device *dev)
0214 {
0215     struct platform_device *pdev = to_platform_device(dev);
0216     struct vibra_info *info = platform_get_drvdata(pdev);
0217 
0218     cancel_work_sync(&info->play_work);
0219 
0220     if (info->enabled)
0221         twl6040_vibra_disable(info);
0222 
0223     return 0;
0224 }
0225 
0226 static SIMPLE_DEV_PM_OPS(twl6040_vibra_pm_ops, twl6040_vibra_suspend, NULL);
0227 
0228 static int twl6040_vibra_probe(struct platform_device *pdev)
0229 {
0230     struct device *twl6040_core_dev = pdev->dev.parent;
0231     struct device_node *twl6040_core_node;
0232     struct vibra_info *info;
0233     int vddvibl_uV = 0;
0234     int vddvibr_uV = 0;
0235     int error;
0236 
0237     twl6040_core_node = of_get_child_by_name(twl6040_core_dev->of_node,
0238                          "vibra");
0239     if (!twl6040_core_node) {
0240         dev_err(&pdev->dev, "parent of node is missing?\n");
0241         return -EINVAL;
0242     }
0243 
0244     info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
0245     if (!info) {
0246         of_node_put(twl6040_core_node);
0247         dev_err(&pdev->dev, "couldn't allocate memory\n");
0248         return -ENOMEM;
0249     }
0250 
0251     info->dev = &pdev->dev;
0252 
0253     info->twl6040 = dev_get_drvdata(pdev->dev.parent);
0254 
0255     of_property_read_u32(twl6040_core_node, "ti,vibldrv-res",
0256                  &info->vibldrv_res);
0257     of_property_read_u32(twl6040_core_node, "ti,vibrdrv-res",
0258                  &info->vibrdrv_res);
0259     of_property_read_u32(twl6040_core_node, "ti,viblmotor-res",
0260                  &info->viblmotor_res);
0261     of_property_read_u32(twl6040_core_node, "ti,vibrmotor-res",
0262                  &info->vibrmotor_res);
0263     of_property_read_u32(twl6040_core_node, "ti,vddvibl-uV", &vddvibl_uV);
0264     of_property_read_u32(twl6040_core_node, "ti,vddvibr-uV", &vddvibr_uV);
0265 
0266     of_node_put(twl6040_core_node);
0267 
0268     if ((!info->vibldrv_res && !info->viblmotor_res) ||
0269         (!info->vibrdrv_res && !info->vibrmotor_res)) {
0270         dev_err(info->dev, "invalid vibra driver/motor resistance\n");
0271         return -EINVAL;
0272     }
0273 
0274     info->irq = platform_get_irq(pdev, 0);
0275     if (info->irq < 0)
0276         return -EINVAL;
0277 
0278     error = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
0279                       twl6040_vib_irq_handler,
0280                       IRQF_ONESHOT,
0281                       "twl6040_irq_vib", info);
0282     if (error) {
0283         dev_err(info->dev, "VIB IRQ request failed: %d\n", error);
0284         return error;
0285     }
0286 
0287     info->supplies[0].supply = "vddvibl";
0288     info->supplies[1].supply = "vddvibr";
0289     /*
0290      * When booted with Device tree the regulators are attached to the
0291      * parent device (twl6040 MFD core)
0292      */
0293     error = devm_regulator_bulk_get(twl6040_core_dev,
0294                     ARRAY_SIZE(info->supplies),
0295                     info->supplies);
0296     if (error) {
0297         dev_err(info->dev, "couldn't get regulators %d\n", error);
0298         return error;
0299     }
0300 
0301     if (vddvibl_uV) {
0302         error = regulator_set_voltage(info->supplies[0].consumer,
0303                           vddvibl_uV, vddvibl_uV);
0304         if (error) {
0305             dev_err(info->dev, "failed to set VDDVIBL volt %d\n",
0306                 error);
0307             return error;
0308         }
0309     }
0310 
0311     if (vddvibr_uV) {
0312         error = regulator_set_voltage(info->supplies[1].consumer,
0313                           vddvibr_uV, vddvibr_uV);
0314         if (error) {
0315             dev_err(info->dev, "failed to set VDDVIBR volt %d\n",
0316                 error);
0317             return error;
0318         }
0319     }
0320 
0321     INIT_WORK(&info->play_work, vibra_play_work);
0322 
0323     info->input_dev = devm_input_allocate_device(&pdev->dev);
0324     if (!info->input_dev) {
0325         dev_err(info->dev, "couldn't allocate input device\n");
0326         return -ENOMEM;
0327     }
0328 
0329     input_set_drvdata(info->input_dev, info);
0330 
0331     info->input_dev->name = "twl6040:vibrator";
0332     info->input_dev->id.version = 1;
0333     info->input_dev->close = twl6040_vibra_close;
0334     __set_bit(FF_RUMBLE, info->input_dev->ffbit);
0335 
0336     error = input_ff_create_memless(info->input_dev, NULL, vibra_play);
0337     if (error) {
0338         dev_err(info->dev, "couldn't register vibrator to FF\n");
0339         return error;
0340     }
0341 
0342     error = input_register_device(info->input_dev);
0343     if (error) {
0344         dev_err(info->dev, "couldn't register input device\n");
0345         return error;
0346     }
0347 
0348     platform_set_drvdata(pdev, info);
0349 
0350     return 0;
0351 }
0352 
0353 static struct platform_driver twl6040_vibra_driver = {
0354     .probe      = twl6040_vibra_probe,
0355     .driver     = {
0356         .name   = "twl6040-vibra",
0357         .pm = &twl6040_vibra_pm_ops,
0358     },
0359 };
0360 module_platform_driver(twl6040_vibra_driver);
0361 
0362 MODULE_ALIAS("platform:twl6040-vibra");
0363 MODULE_DESCRIPTION("TWL6040 Vibra driver");
0364 MODULE_LICENSE("GPL");
0365 MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
0366 MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");