Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Regulator haptic driver
0004  *
0005  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
0006  * Author: Jaewon Kim <jaewon02.kim@samsung.com>
0007  * Author: Hyunhee Kim <hyunhee.kim@samsung.com>
0008  */
0009 
0010 #include <linux/input.h>
0011 #include <linux/module.h>
0012 #include <linux/of.h>
0013 #include <linux/platform_data/regulator-haptic.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/regulator/consumer.h>
0016 #include <linux/slab.h>
0017 
0018 #define MAX_MAGNITUDE_SHIFT 16
0019 
0020 struct regulator_haptic {
0021     struct device *dev;
0022     struct input_dev *input_dev;
0023     struct regulator *regulator;
0024 
0025     struct work_struct work;
0026     struct mutex mutex;
0027 
0028     bool active;
0029     bool suspended;
0030 
0031     unsigned int max_volt;
0032     unsigned int min_volt;
0033     unsigned int magnitude;
0034 };
0035 
0036 static int regulator_haptic_toggle(struct regulator_haptic *haptic, bool on)
0037 {
0038     int error;
0039 
0040     if (haptic->active != on) {
0041 
0042         error = on ? regulator_enable(haptic->regulator) :
0043                  regulator_disable(haptic->regulator);
0044         if (error) {
0045             dev_err(haptic->dev,
0046                 "failed to switch regulator %s: %d\n",
0047                 on ? "on" : "off", error);
0048             return error;
0049         }
0050 
0051         haptic->active = on;
0052     }
0053 
0054     return 0;
0055 }
0056 
0057 static int regulator_haptic_set_voltage(struct regulator_haptic *haptic,
0058                      unsigned int magnitude)
0059 {
0060     u64 volt_mag_multi;
0061     unsigned int intensity;
0062     int error;
0063 
0064     volt_mag_multi = (u64)(haptic->max_volt - haptic->min_volt) * magnitude;
0065     intensity = (unsigned int)(volt_mag_multi >> MAX_MAGNITUDE_SHIFT);
0066 
0067     error = regulator_set_voltage(haptic->regulator,
0068                       intensity + haptic->min_volt,
0069                       haptic->max_volt);
0070     if (error) {
0071         dev_err(haptic->dev, "cannot set regulator voltage to %d: %d\n",
0072             intensity + haptic->min_volt, error);
0073         return error;
0074     }
0075 
0076     regulator_haptic_toggle(haptic, !!magnitude);
0077 
0078     return 0;
0079 }
0080 
0081 static void regulator_haptic_work(struct work_struct *work)
0082 {
0083     struct regulator_haptic *haptic = container_of(work,
0084                     struct regulator_haptic, work);
0085 
0086     mutex_lock(&haptic->mutex);
0087 
0088     if (!haptic->suspended)
0089         regulator_haptic_set_voltage(haptic, haptic->magnitude);
0090 
0091     mutex_unlock(&haptic->mutex);
0092 }
0093 
0094 static int regulator_haptic_play_effect(struct input_dev *input, void *data,
0095                     struct ff_effect *effect)
0096 {
0097     struct regulator_haptic *haptic = input_get_drvdata(input);
0098 
0099     haptic->magnitude = effect->u.rumble.strong_magnitude;
0100     if (!haptic->magnitude)
0101         haptic->magnitude = effect->u.rumble.weak_magnitude;
0102 
0103     schedule_work(&haptic->work);
0104 
0105     return 0;
0106 }
0107 
0108 static void regulator_haptic_close(struct input_dev *input)
0109 {
0110     struct regulator_haptic *haptic = input_get_drvdata(input);
0111 
0112     cancel_work_sync(&haptic->work);
0113     regulator_haptic_set_voltage(haptic, 0);
0114 }
0115 
0116 static int __maybe_unused
0117 regulator_haptic_parse_dt(struct device *dev, struct regulator_haptic *haptic)
0118 {
0119     struct device_node *node;
0120     int error;
0121 
0122     node = dev->of_node;
0123     if(!node) {
0124         dev_err(dev, "Missing device tree data\n");
0125         return -EINVAL;
0126     }
0127 
0128     error = of_property_read_u32(node, "max-microvolt", &haptic->max_volt);
0129     if (error) {
0130         dev_err(dev, "cannot parse max-microvolt\n");
0131         return error;
0132     }
0133 
0134     error = of_property_read_u32(node, "min-microvolt", &haptic->min_volt);
0135     if (error) {
0136         dev_err(dev, "cannot parse min-microvolt\n");
0137         return error;
0138     }
0139 
0140     return 0;
0141 }
0142 
0143 static int regulator_haptic_probe(struct platform_device *pdev)
0144 {
0145     const struct regulator_haptic_data *pdata = dev_get_platdata(&pdev->dev);
0146     struct regulator_haptic *haptic;
0147     struct input_dev *input_dev;
0148     int error;
0149 
0150     haptic = devm_kzalloc(&pdev->dev, sizeof(*haptic), GFP_KERNEL);
0151     if (!haptic)
0152         return -ENOMEM;
0153 
0154     platform_set_drvdata(pdev, haptic);
0155     haptic->dev = &pdev->dev;
0156     mutex_init(&haptic->mutex);
0157     INIT_WORK(&haptic->work, regulator_haptic_work);
0158 
0159     if (pdata) {
0160         haptic->max_volt = pdata->max_volt;
0161         haptic->min_volt = pdata->min_volt;
0162     } else if (IS_ENABLED(CONFIG_OF)) {
0163         error = regulator_haptic_parse_dt(&pdev->dev, haptic);
0164         if (error)
0165             return error;
0166     } else {
0167         dev_err(&pdev->dev, "Missing platform data\n");
0168         return -EINVAL;
0169     }
0170 
0171     haptic->regulator = devm_regulator_get_exclusive(&pdev->dev, "haptic");
0172     if (IS_ERR(haptic->regulator)) {
0173         dev_err(&pdev->dev, "failed to get regulator\n");
0174         return PTR_ERR(haptic->regulator);
0175     }
0176 
0177     input_dev = devm_input_allocate_device(&pdev->dev);
0178     if (!input_dev)
0179         return  -ENOMEM;
0180 
0181     haptic->input_dev = input_dev;
0182     haptic->input_dev->name = "regulator-haptic";
0183     haptic->input_dev->dev.parent = &pdev->dev;
0184     haptic->input_dev->close = regulator_haptic_close;
0185     input_set_drvdata(haptic->input_dev, haptic);
0186     input_set_capability(haptic->input_dev, EV_FF, FF_RUMBLE);
0187 
0188     error = input_ff_create_memless(input_dev, NULL,
0189                     regulator_haptic_play_effect);
0190     if (error) {
0191         dev_err(&pdev->dev, "failed to create force-feedback\n");
0192         return error;
0193     }
0194 
0195     error = input_register_device(haptic->input_dev);
0196     if (error) {
0197         dev_err(&pdev->dev, "failed to register input device\n");
0198         return error;
0199     }
0200 
0201     return 0;
0202 }
0203 
0204 static int __maybe_unused regulator_haptic_suspend(struct device *dev)
0205 {
0206     struct platform_device *pdev = to_platform_device(dev);
0207     struct regulator_haptic *haptic = platform_get_drvdata(pdev);
0208     int error;
0209 
0210     error = mutex_lock_interruptible(&haptic->mutex);
0211     if (error)
0212         return error;
0213 
0214     regulator_haptic_set_voltage(haptic, 0);
0215 
0216     haptic->suspended = true;
0217 
0218     mutex_unlock(&haptic->mutex);
0219 
0220     return 0;
0221 }
0222 
0223 static int __maybe_unused regulator_haptic_resume(struct device *dev)
0224 {
0225     struct platform_device *pdev = to_platform_device(dev);
0226     struct regulator_haptic *haptic = platform_get_drvdata(pdev);
0227     unsigned int magnitude;
0228 
0229     mutex_lock(&haptic->mutex);
0230 
0231     haptic->suspended = false;
0232 
0233     magnitude = READ_ONCE(haptic->magnitude);
0234     if (magnitude)
0235         regulator_haptic_set_voltage(haptic, magnitude);
0236 
0237     mutex_unlock(&haptic->mutex);
0238 
0239     return 0;
0240 }
0241 
0242 static SIMPLE_DEV_PM_OPS(regulator_haptic_pm_ops,
0243         regulator_haptic_suspend, regulator_haptic_resume);
0244 
0245 static const struct of_device_id regulator_haptic_dt_match[] = {
0246     { .compatible = "regulator-haptic" },
0247     { /* sentinel */ },
0248 };
0249 MODULE_DEVICE_TABLE(of, regulator_haptic_dt_match);
0250 
0251 static struct platform_driver regulator_haptic_driver = {
0252     .probe      = regulator_haptic_probe,
0253     .driver     = {
0254         .name       = "regulator-haptic",
0255         .of_match_table = regulator_haptic_dt_match,
0256         .pm     = &regulator_haptic_pm_ops,
0257     },
0258 };
0259 module_platform_driver(regulator_haptic_driver);
0260 
0261 MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
0262 MODULE_AUTHOR("Hyunhee Kim <hyunhee.kim@samsung.com>");
0263 MODULE_DESCRIPTION("Regulator haptic driver");
0264 MODULE_LICENSE("GPL");