From 2ff865418f446773e58aacf75a2af9a9327abedb Mon Sep 17 00:00:00 2001
From: Ondrej Jirman <megi@xff.cz>
Date: Fri, 8 Sep 2023 01:07:30 +0200
Subject: input: gpio-vibra: Allow to use vcc-supply alone to control the
 vibrator

Make enable-gpio optional to allow using this driver with boards that
have vibrator connected to a power supply without intermediate gpio
based enable circuitry.

Also avoid a case where neither regulator nor enable gpio is specified,
and bail out in probe in such a case.

Signed-off-by: Ondrej Jirman <megi@xff.cz>
---
 drivers/input/misc/gpio-vibra.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/drivers/input/misc/gpio-vibra.c b/drivers/input/misc/gpio-vibra.c
index ad44b4d18a2a..de24e28458b0 100644
--- a/drivers/input/misc/gpio-vibra.c
+++ b/drivers/input/misc/gpio-vibra.c
@@ -39,7 +39,7 @@ static int gpio_vibrator_start(struct gpio_vibrator *vibrator)
 	struct device *pdev = vibrator->input->dev.parent;
 	int err;
 
-	if (!vibrator->vcc_on) {
+	if (vibrator->vcc && !vibrator->vcc_on) {
 		err = regulator_enable(vibrator->vcc);
 		if (err) {
 			dev_err(pdev, "failed to enable regulator: %d\n", err);
@@ -57,7 +57,7 @@ static void gpio_vibrator_stop(struct gpio_vibrator *vibrator)
 {
 	gpiod_set_value_cansleep(vibrator->gpio, 0);
 
-	if (vibrator->vcc_on) {
+	if (vibrator->vcc && vibrator->vcc_on) {
 		regulator_disable(vibrator->vcc);
 		vibrator->vcc_on = false;
 	}
@@ -112,16 +112,27 @@ static int gpio_vibrator_probe(struct platform_device *pdev)
 	if (!vibrator->input)
 		return -ENOMEM;
 
-	vibrator->vcc = devm_regulator_get(&pdev->dev, "vcc");
-	if (IS_ERR(vibrator->vcc))
-		return dev_err_probe(&pdev->dev, PTR_ERR(vibrator->vcc),
+	vibrator->vcc = devm_regulator_get_optional(&pdev->dev, "vcc");
+	err = PTR_ERR_OR_ZERO(vibrator->vcc);
+	if (err == -ENODEV) {
+		vibrator->vcc = NULL;
+	} else if (err) {
+		return dev_err_probe(&pdev->dev, err,
 				     "Failed to request regulator\n");
+	}
 
-	vibrator->gpio = devm_gpiod_get(&pdev->dev, "enable", GPIOD_OUT_LOW);
-	if (IS_ERR(vibrator->gpio))
+	vibrator->gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
+						 GPIOD_OUT_LOW);
+	err = PTR_ERR_OR_ZERO(vibrator->gpio);
+	if (err)
 		return dev_err_probe(&pdev->dev, PTR_ERR(vibrator->gpio),
 				     "Failed to request main gpio\n");
 
+	if (!vibrator->vcc && !vibrator->gpio) {
+		dev_err(&pdev->dev, "Neither gpio nor regulator provided\n");
+		return -EINVAL;
+	}
+
 	INIT_WORK(&vibrator->play_work, gpio_vibrator_play_work);
 
 	vibrator->input->name = "gpio-vibrator";
-- 
2.35.3

