Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  *  GPIO vibrator driver
0004  *
0005  *  Copyright (C) 2019 Luca Weiss <luca@z3ntu.xyz>
0006  *
0007  *  Based on PWM vibrator driver:
0008  *  Copyright (C) 2017 Collabora Ltd.
0009  *
0010  *  Based on previous work from:
0011  *  Copyright (C) 2012 Dmitry Torokhov <dmitry.torokhov@gmail.com>
0012  *
0013  *  Based on PWM beeper driver:
0014  *  Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
0015  */
0016 
0017 #include <linux/gpio/consumer.h>
0018 #include <linux/input.h>
0019 #include <linux/kernel.h>
0020 #include <linux/module.h>
0021 #include <linux/of_device.h>
0022 #include <linux/platform_device.h>
0023 #include <linux/property.h>
0024 #include <linux/regulator/consumer.h>
0025 #include <linux/slab.h>
0026 
0027 struct gpio_vibrator {
0028     struct input_dev *input;
0029     struct gpio_desc *gpio;
0030     struct regulator *vcc;
0031 
0032     struct work_struct play_work;
0033     bool running;
0034     bool vcc_on;
0035 };
0036 
0037 static int gpio_vibrator_start(struct gpio_vibrator *vibrator)
0038 {
0039     struct device *pdev = vibrator->input->dev.parent;
0040     int err;
0041 
0042     if (!vibrator->vcc_on) {
0043         err = regulator_enable(vibrator->vcc);
0044         if (err) {
0045             dev_err(pdev, "failed to enable regulator: %d\n", err);
0046             return err;
0047         }
0048         vibrator->vcc_on = true;
0049     }
0050 
0051     gpiod_set_value_cansleep(vibrator->gpio, 1);
0052 
0053     return 0;
0054 }
0055 
0056 static void gpio_vibrator_stop(struct gpio_vibrator *vibrator)
0057 {
0058     gpiod_set_value_cansleep(vibrator->gpio, 0);
0059 
0060     if (vibrator->vcc_on) {
0061         regulator_disable(vibrator->vcc);
0062         vibrator->vcc_on = false;
0063     }
0064 }
0065 
0066 static void gpio_vibrator_play_work(struct work_struct *work)
0067 {
0068     struct gpio_vibrator *vibrator =
0069         container_of(work, struct gpio_vibrator, play_work);
0070 
0071     if (vibrator->running)
0072         gpio_vibrator_start(vibrator);
0073     else
0074         gpio_vibrator_stop(vibrator);
0075 }
0076 
0077 static int gpio_vibrator_play_effect(struct input_dev *dev, void *data,
0078                      struct ff_effect *effect)
0079 {
0080     struct gpio_vibrator *vibrator = input_get_drvdata(dev);
0081     int level;
0082 
0083     level = effect->u.rumble.strong_magnitude;
0084     if (!level)
0085         level = effect->u.rumble.weak_magnitude;
0086 
0087     vibrator->running = level;
0088     schedule_work(&vibrator->play_work);
0089 
0090     return 0;
0091 }
0092 
0093 static void gpio_vibrator_close(struct input_dev *input)
0094 {
0095     struct gpio_vibrator *vibrator = input_get_drvdata(input);
0096 
0097     cancel_work_sync(&vibrator->play_work);
0098     gpio_vibrator_stop(vibrator);
0099     vibrator->running = false;
0100 }
0101 
0102 static int gpio_vibrator_probe(struct platform_device *pdev)
0103 {
0104     struct gpio_vibrator *vibrator;
0105     int err;
0106 
0107     vibrator = devm_kzalloc(&pdev->dev, sizeof(*vibrator), GFP_KERNEL);
0108     if (!vibrator)
0109         return -ENOMEM;
0110 
0111     vibrator->input = devm_input_allocate_device(&pdev->dev);
0112     if (!vibrator->input)
0113         return -ENOMEM;
0114 
0115     vibrator->vcc = devm_regulator_get(&pdev->dev, "vcc");
0116     err = PTR_ERR_OR_ZERO(vibrator->vcc);
0117     if (err) {
0118         if (err != -EPROBE_DEFER)
0119             dev_err(&pdev->dev, "Failed to request regulator: %d\n",
0120                 err);
0121         return err;
0122     }
0123 
0124     vibrator->gpio = devm_gpiod_get(&pdev->dev, "enable", GPIOD_OUT_LOW);
0125     err = PTR_ERR_OR_ZERO(vibrator->gpio);
0126     if (err) {
0127         if (err != -EPROBE_DEFER)
0128             dev_err(&pdev->dev, "Failed to request main gpio: %d\n",
0129                 err);
0130         return err;
0131     }
0132 
0133     INIT_WORK(&vibrator->play_work, gpio_vibrator_play_work);
0134 
0135     vibrator->input->name = "gpio-vibrator";
0136     vibrator->input->id.bustype = BUS_HOST;
0137     vibrator->input->close = gpio_vibrator_close;
0138 
0139     input_set_drvdata(vibrator->input, vibrator);
0140     input_set_capability(vibrator->input, EV_FF, FF_RUMBLE);
0141 
0142     err = input_ff_create_memless(vibrator->input, NULL,
0143                       gpio_vibrator_play_effect);
0144     if (err) {
0145         dev_err(&pdev->dev, "Couldn't create FF dev: %d\n", err);
0146         return err;
0147     }
0148 
0149     err = input_register_device(vibrator->input);
0150     if (err) {
0151         dev_err(&pdev->dev, "Couldn't register input dev: %d\n", err);
0152         return err;
0153     }
0154 
0155     platform_set_drvdata(pdev, vibrator);
0156 
0157     return 0;
0158 }
0159 
0160 static int __maybe_unused gpio_vibrator_suspend(struct device *dev)
0161 {
0162     struct platform_device *pdev = to_platform_device(dev);
0163     struct gpio_vibrator *vibrator = platform_get_drvdata(pdev);
0164 
0165     cancel_work_sync(&vibrator->play_work);
0166     if (vibrator->running)
0167         gpio_vibrator_stop(vibrator);
0168 
0169     return 0;
0170 }
0171 
0172 static int __maybe_unused gpio_vibrator_resume(struct device *dev)
0173 {
0174     struct platform_device *pdev = to_platform_device(dev);
0175     struct gpio_vibrator *vibrator = platform_get_drvdata(pdev);
0176 
0177     if (vibrator->running)
0178         gpio_vibrator_start(vibrator);
0179 
0180     return 0;
0181 }
0182 
0183 static SIMPLE_DEV_PM_OPS(gpio_vibrator_pm_ops,
0184              gpio_vibrator_suspend, gpio_vibrator_resume);
0185 
0186 #ifdef CONFIG_OF
0187 static const struct of_device_id gpio_vibra_dt_match_table[] = {
0188     { .compatible = "gpio-vibrator" },
0189     {}
0190 };
0191 MODULE_DEVICE_TABLE(of, gpio_vibra_dt_match_table);
0192 #endif
0193 
0194 static struct platform_driver gpio_vibrator_driver = {
0195     .probe  = gpio_vibrator_probe,
0196     .driver = {
0197         .name   = "gpio-vibrator",
0198         .pm = &gpio_vibrator_pm_ops,
0199         .of_match_table = of_match_ptr(gpio_vibra_dt_match_table),
0200     },
0201 };
0202 module_platform_driver(gpio_vibrator_driver);
0203 
0204 MODULE_AUTHOR("Luca Weiss <luca@z3ntu.xy>");
0205 MODULE_DESCRIPTION("GPIO vibrator driver");
0206 MODULE_LICENSE("GPL");
0207 MODULE_ALIAS("platform:gpio-vibrator");