18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  GPIO vibrator driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) 2019 Luca Weiss <luca@z3ntu.xyz>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *  Based on PWM vibrator driver:
88c2ecf20Sopenharmony_ci *  Copyright (C) 2017 Collabora Ltd.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci *  Based on previous work from:
118c2ecf20Sopenharmony_ci *  Copyright (C) 2012 Dmitry Torokhov <dmitry.torokhov@gmail.com>
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci *  Based on PWM beeper driver:
148c2ecf20Sopenharmony_ci *  Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h>
188c2ecf20Sopenharmony_ci#include <linux/input.h>
198c2ecf20Sopenharmony_ci#include <linux/kernel.h>
208c2ecf20Sopenharmony_ci#include <linux/module.h>
218c2ecf20Sopenharmony_ci#include <linux/of_device.h>
228c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
238c2ecf20Sopenharmony_ci#include <linux/property.h>
248c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h>
258c2ecf20Sopenharmony_ci#include <linux/slab.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistruct gpio_vibrator {
288c2ecf20Sopenharmony_ci	struct input_dev *input;
298c2ecf20Sopenharmony_ci	struct gpio_desc *gpio;
308c2ecf20Sopenharmony_ci	struct regulator *vcc;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	struct work_struct play_work;
338c2ecf20Sopenharmony_ci	bool running;
348c2ecf20Sopenharmony_ci	bool vcc_on;
358c2ecf20Sopenharmony_ci};
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic int gpio_vibrator_start(struct gpio_vibrator *vibrator)
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	struct device *pdev = vibrator->input->dev.parent;
408c2ecf20Sopenharmony_ci	int err;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	if (!vibrator->vcc_on) {
438c2ecf20Sopenharmony_ci		err = regulator_enable(vibrator->vcc);
448c2ecf20Sopenharmony_ci		if (err) {
458c2ecf20Sopenharmony_ci			dev_err(pdev, "failed to enable regulator: %d\n", err);
468c2ecf20Sopenharmony_ci			return err;
478c2ecf20Sopenharmony_ci		}
488c2ecf20Sopenharmony_ci		vibrator->vcc_on = true;
498c2ecf20Sopenharmony_ci	}
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	gpiod_set_value_cansleep(vibrator->gpio, 1);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	return 0;
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic void gpio_vibrator_stop(struct gpio_vibrator *vibrator)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	gpiod_set_value_cansleep(vibrator->gpio, 0);
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	if (vibrator->vcc_on) {
618c2ecf20Sopenharmony_ci		regulator_disable(vibrator->vcc);
628c2ecf20Sopenharmony_ci		vibrator->vcc_on = false;
638c2ecf20Sopenharmony_ci	}
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistatic void gpio_vibrator_play_work(struct work_struct *work)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	struct gpio_vibrator *vibrator =
698c2ecf20Sopenharmony_ci		container_of(work, struct gpio_vibrator, play_work);
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	if (vibrator->running)
728c2ecf20Sopenharmony_ci		gpio_vibrator_start(vibrator);
738c2ecf20Sopenharmony_ci	else
748c2ecf20Sopenharmony_ci		gpio_vibrator_stop(vibrator);
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic int gpio_vibrator_play_effect(struct input_dev *dev, void *data,
788c2ecf20Sopenharmony_ci				     struct ff_effect *effect)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	struct gpio_vibrator *vibrator = input_get_drvdata(dev);
818c2ecf20Sopenharmony_ci	int level;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	level = effect->u.rumble.strong_magnitude;
848c2ecf20Sopenharmony_ci	if (!level)
858c2ecf20Sopenharmony_ci		level = effect->u.rumble.weak_magnitude;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	vibrator->running = level;
888c2ecf20Sopenharmony_ci	schedule_work(&vibrator->play_work);
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	return 0;
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic void gpio_vibrator_close(struct input_dev *input)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	struct gpio_vibrator *vibrator = input_get_drvdata(input);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	cancel_work_sync(&vibrator->play_work);
988c2ecf20Sopenharmony_ci	gpio_vibrator_stop(vibrator);
998c2ecf20Sopenharmony_ci	vibrator->running = false;
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic int gpio_vibrator_probe(struct platform_device *pdev)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	struct gpio_vibrator *vibrator;
1058c2ecf20Sopenharmony_ci	int err;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	vibrator = devm_kzalloc(&pdev->dev, sizeof(*vibrator), GFP_KERNEL);
1088c2ecf20Sopenharmony_ci	if (!vibrator)
1098c2ecf20Sopenharmony_ci		return -ENOMEM;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	vibrator->input = devm_input_allocate_device(&pdev->dev);
1128c2ecf20Sopenharmony_ci	if (!vibrator->input)
1138c2ecf20Sopenharmony_ci		return -ENOMEM;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	vibrator->vcc = devm_regulator_get(&pdev->dev, "vcc");
1168c2ecf20Sopenharmony_ci	err = PTR_ERR_OR_ZERO(vibrator->vcc);
1178c2ecf20Sopenharmony_ci	if (err) {
1188c2ecf20Sopenharmony_ci		if (err != -EPROBE_DEFER)
1198c2ecf20Sopenharmony_ci			dev_err(&pdev->dev, "Failed to request regulator: %d\n",
1208c2ecf20Sopenharmony_ci				err);
1218c2ecf20Sopenharmony_ci		return err;
1228c2ecf20Sopenharmony_ci	}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	vibrator->gpio = devm_gpiod_get(&pdev->dev, "enable", GPIOD_OUT_LOW);
1258c2ecf20Sopenharmony_ci	err = PTR_ERR_OR_ZERO(vibrator->gpio);
1268c2ecf20Sopenharmony_ci	if (err) {
1278c2ecf20Sopenharmony_ci		if (err != -EPROBE_DEFER)
1288c2ecf20Sopenharmony_ci			dev_err(&pdev->dev, "Failed to request main gpio: %d\n",
1298c2ecf20Sopenharmony_ci				err);
1308c2ecf20Sopenharmony_ci		return err;
1318c2ecf20Sopenharmony_ci	}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	INIT_WORK(&vibrator->play_work, gpio_vibrator_play_work);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	vibrator->input->name = "gpio-vibrator";
1368c2ecf20Sopenharmony_ci	vibrator->input->id.bustype = BUS_HOST;
1378c2ecf20Sopenharmony_ci	vibrator->input->close = gpio_vibrator_close;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	input_set_drvdata(vibrator->input, vibrator);
1408c2ecf20Sopenharmony_ci	input_set_capability(vibrator->input, EV_FF, FF_RUMBLE);
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	err = input_ff_create_memless(vibrator->input, NULL,
1438c2ecf20Sopenharmony_ci				      gpio_vibrator_play_effect);
1448c2ecf20Sopenharmony_ci	if (err) {
1458c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Couldn't create FF dev: %d\n", err);
1468c2ecf20Sopenharmony_ci		return err;
1478c2ecf20Sopenharmony_ci	}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	err = input_register_device(vibrator->input);
1508c2ecf20Sopenharmony_ci	if (err) {
1518c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Couldn't register input dev: %d\n", err);
1528c2ecf20Sopenharmony_ci		return err;
1538c2ecf20Sopenharmony_ci	}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, vibrator);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	return 0;
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic int __maybe_unused gpio_vibrator_suspend(struct device *dev)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	struct platform_device *pdev = to_platform_device(dev);
1638c2ecf20Sopenharmony_ci	struct gpio_vibrator *vibrator = platform_get_drvdata(pdev);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	cancel_work_sync(&vibrator->play_work);
1668c2ecf20Sopenharmony_ci	if (vibrator->running)
1678c2ecf20Sopenharmony_ci		gpio_vibrator_stop(vibrator);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	return 0;
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic int __maybe_unused gpio_vibrator_resume(struct device *dev)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	struct platform_device *pdev = to_platform_device(dev);
1758c2ecf20Sopenharmony_ci	struct gpio_vibrator *vibrator = platform_get_drvdata(pdev);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	if (vibrator->running)
1788c2ecf20Sopenharmony_ci		gpio_vibrator_start(vibrator);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	return 0;
1818c2ecf20Sopenharmony_ci}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(gpio_vibrator_pm_ops,
1848c2ecf20Sopenharmony_ci			 gpio_vibrator_suspend, gpio_vibrator_resume);
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci#ifdef CONFIG_OF
1878c2ecf20Sopenharmony_cistatic const struct of_device_id gpio_vibra_dt_match_table[] = {
1888c2ecf20Sopenharmony_ci	{ .compatible = "gpio-vibrator" },
1898c2ecf20Sopenharmony_ci	{}
1908c2ecf20Sopenharmony_ci};
1918c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, gpio_vibra_dt_match_table);
1928c2ecf20Sopenharmony_ci#endif
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_cistatic struct platform_driver gpio_vibrator_driver = {
1958c2ecf20Sopenharmony_ci	.probe	= gpio_vibrator_probe,
1968c2ecf20Sopenharmony_ci	.driver	= {
1978c2ecf20Sopenharmony_ci		.name	= "gpio-vibrator",
1988c2ecf20Sopenharmony_ci		.pm	= &gpio_vibrator_pm_ops,
1998c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(gpio_vibra_dt_match_table),
2008c2ecf20Sopenharmony_ci	},
2018c2ecf20Sopenharmony_ci};
2028c2ecf20Sopenharmony_cimodule_platform_driver(gpio_vibrator_driver);
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ciMODULE_AUTHOR("Luca Weiss <luca@z3ntu.xy>");
2058c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("GPIO vibrator driver");
2068c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
2078c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:gpio-vibrator");
208