Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * MAXIM MAX77693/MAX77843 Haptic device driver
0004  *
0005  * Copyright (C) 2014,2015 Samsung Electronics
0006  * Jaewon Kim <jaewon02.kim@samsung.com>
0007  * Krzysztof Kozlowski <krzk@kernel.org>
0008  *
0009  * This program is not provided / owned by Maxim Integrated Products.
0010  */
0011 
0012 #include <linux/err.h>
0013 #include <linux/init.h>
0014 #include <linux/i2c.h>
0015 #include <linux/regmap.h>
0016 #include <linux/input.h>
0017 #include <linux/module.h>
0018 #include <linux/platform_device.h>
0019 #include <linux/pwm.h>
0020 #include <linux/slab.h>
0021 #include <linux/workqueue.h>
0022 #include <linux/regulator/consumer.h>
0023 #include <linux/mfd/max77693.h>
0024 #include <linux/mfd/max77693-common.h>
0025 #include <linux/mfd/max77693-private.h>
0026 #include <linux/mfd/max77843-private.h>
0027 
0028 #define MAX_MAGNITUDE_SHIFT 16
0029 
0030 enum max77693_haptic_motor_type {
0031     MAX77693_HAPTIC_ERM = 0,
0032     MAX77693_HAPTIC_LRA,
0033 };
0034 
0035 enum max77693_haptic_pulse_mode {
0036     MAX77693_HAPTIC_EXTERNAL_MODE = 0,
0037     MAX77693_HAPTIC_INTERNAL_MODE,
0038 };
0039 
0040 enum max77693_haptic_pwm_divisor {
0041     MAX77693_HAPTIC_PWM_DIVISOR_32 = 0,
0042     MAX77693_HAPTIC_PWM_DIVISOR_64,
0043     MAX77693_HAPTIC_PWM_DIVISOR_128,
0044     MAX77693_HAPTIC_PWM_DIVISOR_256,
0045 };
0046 
0047 struct max77693_haptic {
0048     enum max77693_types dev_type;
0049 
0050     struct regmap *regmap_pmic;
0051     struct regmap *regmap_haptic;
0052     struct device *dev;
0053     struct input_dev *input_dev;
0054     struct pwm_device *pwm_dev;
0055     struct regulator *motor_reg;
0056 
0057     bool enabled;
0058     bool suspend_state;
0059     unsigned int magnitude;
0060     unsigned int pwm_duty;
0061     enum max77693_haptic_motor_type type;
0062     enum max77693_haptic_pulse_mode mode;
0063 
0064     struct work_struct work;
0065 };
0066 
0067 static int max77693_haptic_set_duty_cycle(struct max77693_haptic *haptic)
0068 {
0069     struct pwm_args pargs;
0070     int delta;
0071     int error;
0072 
0073     pwm_get_args(haptic->pwm_dev, &pargs);
0074     delta = (pargs.period + haptic->pwm_duty) / 2;
0075     error = pwm_config(haptic->pwm_dev, delta, pargs.period);
0076     if (error) {
0077         dev_err(haptic->dev, "failed to configure pwm: %d\n", error);
0078         return error;
0079     }
0080 
0081     return 0;
0082 }
0083 
0084 static int max77843_haptic_bias(struct max77693_haptic *haptic, bool on)
0085 {
0086     int error;
0087 
0088     if (haptic->dev_type != TYPE_MAX77843)
0089         return 0;
0090 
0091     error = regmap_update_bits(haptic->regmap_haptic,
0092                    MAX77843_SYS_REG_MAINCTRL1,
0093                    MAX77843_MAINCTRL1_BIASEN_MASK,
0094                    on << MAINCTRL1_BIASEN_SHIFT);
0095     if (error) {
0096         dev_err(haptic->dev, "failed to %s bias: %d\n",
0097             on ? "enable" : "disable", error);
0098         return error;
0099     }
0100 
0101     return 0;
0102 }
0103 
0104 static int max77693_haptic_configure(struct max77693_haptic *haptic,
0105                      bool enable)
0106 {
0107     unsigned int value, config_reg;
0108     int error;
0109 
0110     switch (haptic->dev_type) {
0111     case TYPE_MAX77693:
0112         value = ((haptic->type << MAX77693_CONFIG2_MODE) |
0113             (enable << MAX77693_CONFIG2_MEN) |
0114             (haptic->mode << MAX77693_CONFIG2_HTYP) |
0115             MAX77693_HAPTIC_PWM_DIVISOR_128);
0116         config_reg = MAX77693_HAPTIC_REG_CONFIG2;
0117         break;
0118     case TYPE_MAX77843:
0119         value = (haptic->type << MCONFIG_MODE_SHIFT) |
0120             (enable << MCONFIG_MEN_SHIFT) |
0121             MAX77693_HAPTIC_PWM_DIVISOR_128;
0122         config_reg = MAX77843_HAP_REG_MCONFIG;
0123         break;
0124     default:
0125         return -EINVAL;
0126     }
0127 
0128     error = regmap_write(haptic->regmap_haptic,
0129                  config_reg, value);
0130     if (error) {
0131         dev_err(haptic->dev,
0132             "failed to update haptic config: %d\n", error);
0133         return error;
0134     }
0135 
0136     return 0;
0137 }
0138 
0139 static int max77693_haptic_lowsys(struct max77693_haptic *haptic, bool enable)
0140 {
0141     int error;
0142 
0143     if (haptic->dev_type != TYPE_MAX77693)
0144         return 0;
0145 
0146     error = regmap_update_bits(haptic->regmap_pmic,
0147                    MAX77693_PMIC_REG_LSCNFG,
0148                    MAX77693_PMIC_LOW_SYS_MASK,
0149                    enable << MAX77693_PMIC_LOW_SYS_SHIFT);
0150     if (error) {
0151         dev_err(haptic->dev, "cannot update pmic regmap: %d\n", error);
0152         return error;
0153     }
0154 
0155     return 0;
0156 }
0157 
0158 static void max77693_haptic_enable(struct max77693_haptic *haptic)
0159 {
0160     int error;
0161 
0162     if (haptic->enabled)
0163         return;
0164 
0165     error = pwm_enable(haptic->pwm_dev);
0166     if (error) {
0167         dev_err(haptic->dev,
0168             "failed to enable haptic pwm device: %d\n", error);
0169         return;
0170     }
0171 
0172     error = max77693_haptic_lowsys(haptic, true);
0173     if (error)
0174         goto err_enable_lowsys;
0175 
0176     error = max77693_haptic_configure(haptic, true);
0177     if (error)
0178         goto err_enable_config;
0179 
0180     haptic->enabled = true;
0181 
0182     return;
0183 
0184 err_enable_config:
0185     max77693_haptic_lowsys(haptic, false);
0186 err_enable_lowsys:
0187     pwm_disable(haptic->pwm_dev);
0188 }
0189 
0190 static void max77693_haptic_disable(struct max77693_haptic *haptic)
0191 {
0192     int error;
0193 
0194     if (!haptic->enabled)
0195         return;
0196 
0197     error = max77693_haptic_configure(haptic, false);
0198     if (error)
0199         return;
0200 
0201     error = max77693_haptic_lowsys(haptic, false);
0202     if (error)
0203         goto err_disable_lowsys;
0204 
0205     pwm_disable(haptic->pwm_dev);
0206     haptic->enabled = false;
0207 
0208     return;
0209 
0210 err_disable_lowsys:
0211     max77693_haptic_configure(haptic, true);
0212 }
0213 
0214 static void max77693_haptic_play_work(struct work_struct *work)
0215 {
0216     struct max77693_haptic *haptic =
0217             container_of(work, struct max77693_haptic, work);
0218     int error;
0219 
0220     error = max77693_haptic_set_duty_cycle(haptic);
0221     if (error) {
0222         dev_err(haptic->dev, "failed to set duty cycle: %d\n", error);
0223         return;
0224     }
0225 
0226     if (haptic->magnitude)
0227         max77693_haptic_enable(haptic);
0228     else
0229         max77693_haptic_disable(haptic);
0230 }
0231 
0232 static int max77693_haptic_play_effect(struct input_dev *dev, void *data,
0233                        struct ff_effect *effect)
0234 {
0235     struct max77693_haptic *haptic = input_get_drvdata(dev);
0236     struct pwm_args pargs;
0237     u64 period_mag_multi;
0238 
0239     haptic->magnitude = effect->u.rumble.strong_magnitude;
0240     if (!haptic->magnitude)
0241         haptic->magnitude = effect->u.rumble.weak_magnitude;
0242 
0243     /*
0244      * The magnitude comes from force-feedback interface.
0245      * The formula to convert magnitude to pwm_duty as follows:
0246      * - pwm_duty = (magnitude * pwm_period) / MAX_MAGNITUDE(0xFFFF)
0247      */
0248     pwm_get_args(haptic->pwm_dev, &pargs);
0249     period_mag_multi = (u64)pargs.period * haptic->magnitude;
0250     haptic->pwm_duty = (unsigned int)(period_mag_multi >>
0251                         MAX_MAGNITUDE_SHIFT);
0252 
0253     schedule_work(&haptic->work);
0254 
0255     return 0;
0256 }
0257 
0258 static int max77693_haptic_open(struct input_dev *dev)
0259 {
0260     struct max77693_haptic *haptic = input_get_drvdata(dev);
0261     int error;
0262 
0263     error = max77843_haptic_bias(haptic, true);
0264     if (error)
0265         return error;
0266 
0267     error = regulator_enable(haptic->motor_reg);
0268     if (error) {
0269         dev_err(haptic->dev,
0270             "failed to enable regulator: %d\n", error);
0271         return error;
0272     }
0273 
0274     return 0;
0275 }
0276 
0277 static void max77693_haptic_close(struct input_dev *dev)
0278 {
0279     struct max77693_haptic *haptic = input_get_drvdata(dev);
0280     int error;
0281 
0282     cancel_work_sync(&haptic->work);
0283     max77693_haptic_disable(haptic);
0284 
0285     error = regulator_disable(haptic->motor_reg);
0286     if (error)
0287         dev_err(haptic->dev,
0288             "failed to disable regulator: %d\n", error);
0289 
0290     max77843_haptic_bias(haptic, false);
0291 }
0292 
0293 static int max77693_haptic_probe(struct platform_device *pdev)
0294 {
0295     struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
0296     struct max77693_haptic *haptic;
0297     int error;
0298 
0299     haptic = devm_kzalloc(&pdev->dev, sizeof(*haptic), GFP_KERNEL);
0300     if (!haptic)
0301         return -ENOMEM;
0302 
0303     haptic->regmap_pmic = max77693->regmap;
0304     haptic->dev = &pdev->dev;
0305     haptic->type = MAX77693_HAPTIC_LRA;
0306     haptic->mode = MAX77693_HAPTIC_EXTERNAL_MODE;
0307     haptic->suspend_state = false;
0308 
0309     /* Variant-specific init */
0310     haptic->dev_type = platform_get_device_id(pdev)->driver_data;
0311     switch (haptic->dev_type) {
0312     case TYPE_MAX77693:
0313         haptic->regmap_haptic = max77693->regmap_haptic;
0314         break;
0315     case TYPE_MAX77843:
0316         haptic->regmap_haptic = max77693->regmap;
0317         break;
0318     default:
0319         dev_err(&pdev->dev, "unsupported device type: %u\n",
0320             haptic->dev_type);
0321         return -EINVAL;
0322     }
0323 
0324     INIT_WORK(&haptic->work, max77693_haptic_play_work);
0325 
0326     /* Get pwm and regulatot for haptic device */
0327     haptic->pwm_dev = devm_pwm_get(&pdev->dev, NULL);
0328     if (IS_ERR(haptic->pwm_dev)) {
0329         dev_err(&pdev->dev, "failed to get pwm device\n");
0330         return PTR_ERR(haptic->pwm_dev);
0331     }
0332 
0333     /*
0334      * FIXME: pwm_apply_args() should be removed when switching to the
0335      * atomic PWM API.
0336      */
0337     pwm_apply_args(haptic->pwm_dev);
0338 
0339     haptic->motor_reg = devm_regulator_get(&pdev->dev, "haptic");
0340     if (IS_ERR(haptic->motor_reg)) {
0341         dev_err(&pdev->dev, "failed to get regulator\n");
0342         return PTR_ERR(haptic->motor_reg);
0343     }
0344 
0345     /* Initialize input device for haptic device */
0346     haptic->input_dev = devm_input_allocate_device(&pdev->dev);
0347     if (!haptic->input_dev) {
0348         dev_err(&pdev->dev, "failed to allocate input device\n");
0349         return -ENOMEM;
0350     }
0351 
0352     haptic->input_dev->name = "max77693-haptic";
0353     haptic->input_dev->id.version = 1;
0354     haptic->input_dev->dev.parent = &pdev->dev;
0355     haptic->input_dev->open = max77693_haptic_open;
0356     haptic->input_dev->close = max77693_haptic_close;
0357     input_set_drvdata(haptic->input_dev, haptic);
0358     input_set_capability(haptic->input_dev, EV_FF, FF_RUMBLE);
0359 
0360     error = input_ff_create_memless(haptic->input_dev, NULL,
0361                 max77693_haptic_play_effect);
0362     if (error) {
0363         dev_err(&pdev->dev, "failed to create force-feedback\n");
0364         return error;
0365     }
0366 
0367     error = input_register_device(haptic->input_dev);
0368     if (error) {
0369         dev_err(&pdev->dev, "failed to register input device\n");
0370         return error;
0371     }
0372 
0373     platform_set_drvdata(pdev, haptic);
0374 
0375     return 0;
0376 }
0377 
0378 static int __maybe_unused max77693_haptic_suspend(struct device *dev)
0379 {
0380     struct platform_device *pdev = to_platform_device(dev);
0381     struct max77693_haptic *haptic = platform_get_drvdata(pdev);
0382 
0383     if (haptic->enabled) {
0384         max77693_haptic_disable(haptic);
0385         haptic->suspend_state = true;
0386     }
0387 
0388     return 0;
0389 }
0390 
0391 static int __maybe_unused max77693_haptic_resume(struct device *dev)
0392 {
0393     struct platform_device *pdev = to_platform_device(dev);
0394     struct max77693_haptic *haptic = platform_get_drvdata(pdev);
0395 
0396     if (haptic->suspend_state) {
0397         max77693_haptic_enable(haptic);
0398         haptic->suspend_state = false;
0399     }
0400 
0401     return 0;
0402 }
0403 
0404 static SIMPLE_DEV_PM_OPS(max77693_haptic_pm_ops,
0405              max77693_haptic_suspend, max77693_haptic_resume);
0406 
0407 static const struct platform_device_id max77693_haptic_id[] = {
0408     { "max77693-haptic", TYPE_MAX77693 },
0409     { "max77843-haptic", TYPE_MAX77843 },
0410     {},
0411 };
0412 MODULE_DEVICE_TABLE(platform, max77693_haptic_id);
0413 
0414 static struct platform_driver max77693_haptic_driver = {
0415     .driver     = {
0416         .name   = "max77693-haptic",
0417         .pm = &max77693_haptic_pm_ops,
0418     },
0419     .probe      = max77693_haptic_probe,
0420     .id_table   = max77693_haptic_id,
0421 };
0422 module_platform_driver(max77693_haptic_driver);
0423 
0424 MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
0425 MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
0426 MODULE_DESCRIPTION("MAXIM 77693/77843 Haptic driver");
0427 MODULE_LICENSE("GPL");