xref: /linux/drivers/input/misc/gpio-vibra.c (revision 03c11eb3b16dc0058589751dfd91f254be2be613)
113bda2d0SLuca Weiss // SPDX-License-Identifier: GPL-2.0+
213bda2d0SLuca Weiss /*
313bda2d0SLuca Weiss  *  GPIO vibrator driver
413bda2d0SLuca Weiss  *
513bda2d0SLuca Weiss  *  Copyright (C) 2019 Luca Weiss <luca@z3ntu.xyz>
613bda2d0SLuca Weiss  *
713bda2d0SLuca Weiss  *  Based on PWM vibrator driver:
813bda2d0SLuca Weiss  *  Copyright (C) 2017 Collabora Ltd.
913bda2d0SLuca Weiss  *
1013bda2d0SLuca Weiss  *  Based on previous work from:
1113bda2d0SLuca Weiss  *  Copyright (C) 2012 Dmitry Torokhov <dmitry.torokhov@gmail.com>
1213bda2d0SLuca Weiss  *
1313bda2d0SLuca Weiss  *  Based on PWM beeper driver:
1413bda2d0SLuca Weiss  *  Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
1513bda2d0SLuca Weiss  */
1613bda2d0SLuca Weiss 
1713bda2d0SLuca Weiss #include <linux/gpio/consumer.h>
1813bda2d0SLuca Weiss #include <linux/input.h>
1913bda2d0SLuca Weiss #include <linux/kernel.h>
2013bda2d0SLuca Weiss #include <linux/module.h>
21*dbce1a7dSRob Herring #include <linux/of.h>
2213bda2d0SLuca Weiss #include <linux/platform_device.h>
2313bda2d0SLuca Weiss #include <linux/property.h>
2413bda2d0SLuca Weiss #include <linux/regulator/consumer.h>
2513bda2d0SLuca Weiss #include <linux/slab.h>
2613bda2d0SLuca Weiss 
2713bda2d0SLuca Weiss struct gpio_vibrator {
2813bda2d0SLuca Weiss 	struct input_dev *input;
2913bda2d0SLuca Weiss 	struct gpio_desc *gpio;
3013bda2d0SLuca Weiss 	struct regulator *vcc;
3113bda2d0SLuca Weiss 
3213bda2d0SLuca Weiss 	struct work_struct play_work;
3313bda2d0SLuca Weiss 	bool running;
3413bda2d0SLuca Weiss 	bool vcc_on;
3513bda2d0SLuca Weiss };
3613bda2d0SLuca Weiss 
gpio_vibrator_start(struct gpio_vibrator * vibrator)3713bda2d0SLuca Weiss static int gpio_vibrator_start(struct gpio_vibrator *vibrator)
3813bda2d0SLuca Weiss {
3913bda2d0SLuca Weiss 	struct device *pdev = vibrator->input->dev.parent;
4013bda2d0SLuca Weiss 	int err;
4113bda2d0SLuca Weiss 
4213bda2d0SLuca Weiss 	if (!vibrator->vcc_on) {
4313bda2d0SLuca Weiss 		err = regulator_enable(vibrator->vcc);
4413bda2d0SLuca Weiss 		if (err) {
4513bda2d0SLuca Weiss 			dev_err(pdev, "failed to enable regulator: %d\n", err);
4613bda2d0SLuca Weiss 			return err;
4713bda2d0SLuca Weiss 		}
4813bda2d0SLuca Weiss 		vibrator->vcc_on = true;
4913bda2d0SLuca Weiss 	}
5013bda2d0SLuca Weiss 
5113bda2d0SLuca Weiss 	gpiod_set_value_cansleep(vibrator->gpio, 1);
5213bda2d0SLuca Weiss 
5313bda2d0SLuca Weiss 	return 0;
5413bda2d0SLuca Weiss }
5513bda2d0SLuca Weiss 
gpio_vibrator_stop(struct gpio_vibrator * vibrator)5613bda2d0SLuca Weiss static void gpio_vibrator_stop(struct gpio_vibrator *vibrator)
5713bda2d0SLuca Weiss {
5813bda2d0SLuca Weiss 	gpiod_set_value_cansleep(vibrator->gpio, 0);
5913bda2d0SLuca Weiss 
6013bda2d0SLuca Weiss 	if (vibrator->vcc_on) {
6113bda2d0SLuca Weiss 		regulator_disable(vibrator->vcc);
6213bda2d0SLuca Weiss 		vibrator->vcc_on = false;
6313bda2d0SLuca Weiss 	}
6413bda2d0SLuca Weiss }
6513bda2d0SLuca Weiss 
gpio_vibrator_play_work(struct work_struct * work)6613bda2d0SLuca Weiss static void gpio_vibrator_play_work(struct work_struct *work)
6713bda2d0SLuca Weiss {
6813bda2d0SLuca Weiss 	struct gpio_vibrator *vibrator =
6913bda2d0SLuca Weiss 		container_of(work, struct gpio_vibrator, play_work);
7013bda2d0SLuca Weiss 
7113bda2d0SLuca Weiss 	if (vibrator->running)
7213bda2d0SLuca Weiss 		gpio_vibrator_start(vibrator);
7313bda2d0SLuca Weiss 	else
7413bda2d0SLuca Weiss 		gpio_vibrator_stop(vibrator);
7513bda2d0SLuca Weiss }
7613bda2d0SLuca Weiss 
gpio_vibrator_play_effect(struct input_dev * dev,void * data,struct ff_effect * effect)7713bda2d0SLuca Weiss static int gpio_vibrator_play_effect(struct input_dev *dev, void *data,
7813bda2d0SLuca Weiss 				     struct ff_effect *effect)
7913bda2d0SLuca Weiss {
8013bda2d0SLuca Weiss 	struct gpio_vibrator *vibrator = input_get_drvdata(dev);
8113bda2d0SLuca Weiss 	int level;
8213bda2d0SLuca Weiss 
8313bda2d0SLuca Weiss 	level = effect->u.rumble.strong_magnitude;
8413bda2d0SLuca Weiss 	if (!level)
8513bda2d0SLuca Weiss 		level = effect->u.rumble.weak_magnitude;
8613bda2d0SLuca Weiss 
8713bda2d0SLuca Weiss 	vibrator->running = level;
8813bda2d0SLuca Weiss 	schedule_work(&vibrator->play_work);
8913bda2d0SLuca Weiss 
9013bda2d0SLuca Weiss 	return 0;
9113bda2d0SLuca Weiss }
9213bda2d0SLuca Weiss 
gpio_vibrator_close(struct input_dev * input)9313bda2d0SLuca Weiss static void gpio_vibrator_close(struct input_dev *input)
9413bda2d0SLuca Weiss {
9513bda2d0SLuca Weiss 	struct gpio_vibrator *vibrator = input_get_drvdata(input);
9613bda2d0SLuca Weiss 
9713bda2d0SLuca Weiss 	cancel_work_sync(&vibrator->play_work);
9813bda2d0SLuca Weiss 	gpio_vibrator_stop(vibrator);
9913bda2d0SLuca Weiss 	vibrator->running = false;
10013bda2d0SLuca Weiss }
10113bda2d0SLuca Weiss 
gpio_vibrator_probe(struct platform_device * pdev)10213bda2d0SLuca Weiss static int gpio_vibrator_probe(struct platform_device *pdev)
10313bda2d0SLuca Weiss {
10413bda2d0SLuca Weiss 	struct gpio_vibrator *vibrator;
10513bda2d0SLuca Weiss 	int err;
10613bda2d0SLuca Weiss 
10713bda2d0SLuca Weiss 	vibrator = devm_kzalloc(&pdev->dev, sizeof(*vibrator), GFP_KERNEL);
10813bda2d0SLuca Weiss 	if (!vibrator)
10913bda2d0SLuca Weiss 		return -ENOMEM;
11013bda2d0SLuca Weiss 
11113bda2d0SLuca Weiss 	vibrator->input = devm_input_allocate_device(&pdev->dev);
11213bda2d0SLuca Weiss 	if (!vibrator->input)
11313bda2d0SLuca Weiss 		return -ENOMEM;
11413bda2d0SLuca Weiss 
11513bda2d0SLuca Weiss 	vibrator->vcc = devm_regulator_get(&pdev->dev, "vcc");
116c4834f4aSKrzysztof Kozlowski 	if (IS_ERR(vibrator->vcc))
117c4834f4aSKrzysztof Kozlowski 		return dev_err_probe(&pdev->dev, PTR_ERR(vibrator->vcc),
118c4834f4aSKrzysztof Kozlowski 				     "Failed to request regulator\n");
11913bda2d0SLuca Weiss 
12013bda2d0SLuca Weiss 	vibrator->gpio = devm_gpiod_get(&pdev->dev, "enable", GPIOD_OUT_LOW);
121c4834f4aSKrzysztof Kozlowski 	if (IS_ERR(vibrator->gpio))
122c4834f4aSKrzysztof Kozlowski 		return dev_err_probe(&pdev->dev, PTR_ERR(vibrator->gpio),
123c4834f4aSKrzysztof Kozlowski 				     "Failed to request main gpio\n");
12413bda2d0SLuca Weiss 
12513bda2d0SLuca Weiss 	INIT_WORK(&vibrator->play_work, gpio_vibrator_play_work);
12613bda2d0SLuca Weiss 
12713bda2d0SLuca Weiss 	vibrator->input->name = "gpio-vibrator";
12813bda2d0SLuca Weiss 	vibrator->input->id.bustype = BUS_HOST;
12913bda2d0SLuca Weiss 	vibrator->input->close = gpio_vibrator_close;
13013bda2d0SLuca Weiss 
13113bda2d0SLuca Weiss 	input_set_drvdata(vibrator->input, vibrator);
13213bda2d0SLuca Weiss 	input_set_capability(vibrator->input, EV_FF, FF_RUMBLE);
13313bda2d0SLuca Weiss 
13413bda2d0SLuca Weiss 	err = input_ff_create_memless(vibrator->input, NULL,
13513bda2d0SLuca Weiss 				      gpio_vibrator_play_effect);
13613bda2d0SLuca Weiss 	if (err) {
13713bda2d0SLuca Weiss 		dev_err(&pdev->dev, "Couldn't create FF dev: %d\n", err);
13813bda2d0SLuca Weiss 		return err;
13913bda2d0SLuca Weiss 	}
14013bda2d0SLuca Weiss 
14113bda2d0SLuca Weiss 	err = input_register_device(vibrator->input);
14213bda2d0SLuca Weiss 	if (err) {
14313bda2d0SLuca Weiss 		dev_err(&pdev->dev, "Couldn't register input dev: %d\n", err);
14413bda2d0SLuca Weiss 		return err;
14513bda2d0SLuca Weiss 	}
14613bda2d0SLuca Weiss 
14713bda2d0SLuca Weiss 	platform_set_drvdata(pdev, vibrator);
14813bda2d0SLuca Weiss 
14913bda2d0SLuca Weiss 	return 0;
15013bda2d0SLuca Weiss }
15113bda2d0SLuca Weiss 
gpio_vibrator_suspend(struct device * dev)15274305affSJonathan Cameron static int gpio_vibrator_suspend(struct device *dev)
15313bda2d0SLuca Weiss {
15413bda2d0SLuca Weiss 	struct platform_device *pdev = to_platform_device(dev);
15513bda2d0SLuca Weiss 	struct gpio_vibrator *vibrator = platform_get_drvdata(pdev);
15613bda2d0SLuca Weiss 
15713bda2d0SLuca Weiss 	cancel_work_sync(&vibrator->play_work);
15813bda2d0SLuca Weiss 	if (vibrator->running)
15913bda2d0SLuca Weiss 		gpio_vibrator_stop(vibrator);
16013bda2d0SLuca Weiss 
16113bda2d0SLuca Weiss 	return 0;
16213bda2d0SLuca Weiss }
16313bda2d0SLuca Weiss 
gpio_vibrator_resume(struct device * dev)16474305affSJonathan Cameron static int gpio_vibrator_resume(struct device *dev)
16513bda2d0SLuca Weiss {
16613bda2d0SLuca Weiss 	struct platform_device *pdev = to_platform_device(dev);
16713bda2d0SLuca Weiss 	struct gpio_vibrator *vibrator = platform_get_drvdata(pdev);
16813bda2d0SLuca Weiss 
16913bda2d0SLuca Weiss 	if (vibrator->running)
17013bda2d0SLuca Weiss 		gpio_vibrator_start(vibrator);
17113bda2d0SLuca Weiss 
17213bda2d0SLuca Weiss 	return 0;
17313bda2d0SLuca Weiss }
17413bda2d0SLuca Weiss 
17574305affSJonathan Cameron static DEFINE_SIMPLE_DEV_PM_OPS(gpio_vibrator_pm_ops,
17613bda2d0SLuca Weiss 				gpio_vibrator_suspend, gpio_vibrator_resume);
17713bda2d0SLuca Weiss 
17813bda2d0SLuca Weiss #ifdef CONFIG_OF
17913bda2d0SLuca Weiss static const struct of_device_id gpio_vibra_dt_match_table[] = {
18013bda2d0SLuca Weiss 	{ .compatible = "gpio-vibrator" },
18113bda2d0SLuca Weiss 	{}
18213bda2d0SLuca Weiss };
18313bda2d0SLuca Weiss MODULE_DEVICE_TABLE(of, gpio_vibra_dt_match_table);
18413bda2d0SLuca Weiss #endif
18513bda2d0SLuca Weiss 
18613bda2d0SLuca Weiss static struct platform_driver gpio_vibrator_driver = {
18713bda2d0SLuca Weiss 	.probe	= gpio_vibrator_probe,
18813bda2d0SLuca Weiss 	.driver	= {
18913bda2d0SLuca Weiss 		.name	= "gpio-vibrator",
19074305affSJonathan Cameron 		.pm	= pm_sleep_ptr(&gpio_vibrator_pm_ops),
19113bda2d0SLuca Weiss 		.of_match_table = of_match_ptr(gpio_vibra_dt_match_table),
19213bda2d0SLuca Weiss 	},
19313bda2d0SLuca Weiss };
19413bda2d0SLuca Weiss module_platform_driver(gpio_vibrator_driver);
19513bda2d0SLuca Weiss 
19613bda2d0SLuca Weiss MODULE_AUTHOR("Luca Weiss <luca@z3ntu.xy>");
19713bda2d0SLuca Weiss MODULE_DESCRIPTION("GPIO vibrator driver");
19813bda2d0SLuca Weiss MODULE_LICENSE("GPL");
19913bda2d0SLuca Weiss MODULE_ALIAS("platform:gpio-vibrator");
200