Skip to contentSkip to conversation

Input: add optional amplifier regulator to pwm-beeper​ (previously "Input: add optional enable gpio to pwm-beeper​")

PATCH In review David Lechner 3 participants 11 messages Jan 11, 2017 – Jan 19, 2017

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover Input: add optional amplifier regulator to pwm-beeper​ (previously "Input: add optional enable gpio to pwm-beeper​")

This series adds an optional regulator to the pwm-beeper driver and device tree bindings. The regulator acts as an amplifier that powers the beeper.

v2 changes:
* Changed from using a gpio to a regulator
* Rebased on the pwm-beeper cleanup series "Input: pwm-beeper - remove calls to legacy pwm_request API"

David Lechner (3):
Input: pwm-beeper: suppress error message on probe defer
dt-bindings: Input: Add optional amp-supply property to pwm-beeper
Input: pwm-beeper: add optional amplifier regulator

 .../devicetree/bindings/input/pwm-beeper.txt       | 16 ++++++++++
 drivers/input/misc/pwm-beeper.c                    | 36 +++++++++++++++++++---
 2 files changed, 48 insertions(+), 4 deletions(-)
Signature · 6 lines
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#1 lore raw

PATCH v2 1/3 Input: pwm-beeper: suppress error message on probe defer

This suppress printing an error message when pwm_get returns -EPROBE_DEFER. Otherwise you get a bunch of noise in the kernel log.

---
 drivers/input/misc/pwm-beeper.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/input/misc/pwm-beeper.c +4 -3
index ce6eec4..30ac227 100644
@@ -104,9 +104,10 @@ static int pwm_beeper_probe(struct platform_device *pdev)
 		return -ENOMEM;
 
 	beeper->pwm = devm_pwm_get(dev, NULL);
-	if (IS_ERR(beeper->pwm)) {
-		error = PTR_ERR(beeper->pwm);
-		dev_err(dev, "Failed to request pwm device: %d\n", error);
+	error = PTR_ERR_OR_ZERO(beeper->pwm);
+	if (error) {
+		if (error != -EPROBE_DEFER)
+			dev_err(dev, "Failed to request pwm device\n");
 		return error;
 	}
 
Signature · 1 line
2.7.4
#3 lore raw

PATCH v2 2/3 dt-bindings: Input: Add optional amp-supply property to pwm-beeper

In reply to David Lechner

This adds an optional amp-supply property to pwm-beeper. This is a regulator that acts as an amplifier for the beeper.

---
 Documentation/devicetree/bindings/input/pwm-beeper.txt | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
Documentation/devicetree/bindings/input/pwm-beeper.txt +16 -0
index be332ae..529408b 100644
@@ -5,3 +5,19 @@ Registers a PWM device as beeper.
 Required properties:
 - compatible: should be "pwm-beeper"
 - pwms: phandle to the physical PWM device
+
+Optional properties:
+- amp-supply: phandle to a regulator that acts as an amplifier for the beeper
+
+Example:
+
+beeper_amp: amplifier {
+	compatible = "fixed-regulator";
+	gpios = <&gpio0 1 GPIO_ACTIVE_HIGH>;
+};
+
+beeper {
+	compatible = "pwm-beeper";
+	pwms = <&pwm0>;
+	amp-supply = <&beeper_amp>;
+};
Signature · 1 line
2.7.4
#5 lore raw

PATCH v2 3/3 Input: pwm-beeper: add optional amplifier regulator

In reply to David Lechner

This adds an optional regulator to the pwm-beeper device. This regulator acts as an amplifier. The amplifier is only enabled while beeping in order to reduce power consumption.

Tested on LEGO MINDSTORMS EV3, which has a speaker connected to PWM through an amplifier.

---
 drivers/input/misc/pwm-beeper.c | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
drivers/input/misc/pwm-beeper.c +28 -1
index 30ac227..708e88e 100644
@@ -14,6 +14,7 @@
  */
 
 #include <linux/input.h>
+#include <linux/regulator/consumer.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/of.h>
@@ -25,8 +26,10 @@
 struct pwm_beeper {
 	struct input_dev *input;
 	struct pwm_device *pwm;
+	struct regulator *reg;
 	struct work_struct work;
 	unsigned long period;
+	bool reg_enabled;
 };
 
 #define HZ_TO_NANOSECONDS(x) (1000000000UL/(x))
@@ -38,8 +41,20 @@ static void __pwm_beeper_set(struct pwm_beeper *beeper)
 	if (period) {
 		pwm_config(beeper->pwm, period / 2, period);
 		pwm_enable(beeper->pwm);
-	} else
+		if (beeper->reg) {
+			int error;
+
+			error = regulator_enable(beeper->reg);
+			if (!error)
+				beeper->reg_enabled = true;
+		}
+	} else {
+		if (beeper->reg_enabled) {
+			regulator_disable(beeper->reg);
+			beeper->reg_enabled = false;
+		}
 		pwm_disable(beeper->pwm);
+	}
 }
 
 static void pwm_beeper_work(struct work_struct *work)
@@ -82,6 +97,10 @@ static void pwm_beeper_stop(struct pwm_beeper *beeper)
 {
 	cancel_work_sync(&beeper->work);
 
+	if (beeper->reg_enabled) {
+		regulator_disable(beeper->reg);
+		beeper->reg_enabled = false;
+	}
 	if (beeper->period)
 		pwm_disable(beeper->pwm);
 }
@@ -111,6 +130,14 @@ static int pwm_beeper_probe(struct platform_device *pdev)
 		return error;
 	}
 
+	beeper->reg = devm_regulator_get_optional(&pdev->dev, "amp");
+	error = PTR_ERR_OR_ZERO(beeper->reg);
+	if (error) {
+		if (error != -EPROBE_DEFER)
+			dev_err(dev, "Failed to get amp regulator\n");
+		return error;
+	}
+
 	/*
 	 * FIXME: pwm_apply_args() should be removed when switching to
 	 * the atomic PWM API.
Signature · 1 line
2.7.4
maintainer
#2 lore raw

Re: [PATCH v2 1/3] Input: pwm-beeper: suppress error message on probe defer

In reply to David Lechner
Quoted 13 lines · David Lechner

This suppress printing an error message when pwm_get returns -EPROBE_DEFER. Otherwise you get a bunch of noise in the kernel log.

---
 drivers/input/misc/pwm-beeper.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/input/misc/pwm-beeper.c +0 -0
index ce6eec4..30ac227 100644

drivers/input/misc/pwm-beeper.c static int pwm_beeper_probe(struct platform_device *pdev) · line 108

Quoted 8 lines
drivers/input/misc/pwm-beeper.c +2 -3
@@ -104,9 +104,10 @@ static int pwm_beeper_probe(struct platform_device *pdev)
 		return -ENOMEM;
 
 	beeper->pwm = devm_pwm_get(dev, NULL);
-	if (IS_ERR(beeper->pwm)) {
-		error = PTR_ERR(beeper->pwm);
-		dev_err(dev, "Failed to request pwm device: %d\n", error);
+	error = PTR_ERR_OR_ZERO(beeper->pwm);
+	if (error) {

I do not find it in any way clearer than

	if (IS_ERR()) {
		...

I prefer PTR_ERR_OR_ZERO be used when you need to return value without acting on it.

I can adjust locally, no need to resubmit.

Quoted 8 lines
drivers/input/misc/pwm-beeper.c +2 -0
+		if (error != -EPROBE_DEFER)
+			dev_err(dev, "Failed to request pwm device\n");
 		return error;
 	}
 
Signature · 1 line
2.7.4
Signature · 1 line
Dmitry
maintainer
#6 lore raw

Re: [PATCH v2 3/3] Input: pwm-beeper: add optional amplifier regulator

In reply to David Lechner
Quoted 69 lines · David Lechner

This adds an optional regulator to the pwm-beeper device. This regulator acts as an amplifier. The amplifier is only enabled while beeping in order to reduce power consumption.

Tested on LEGO MINDSTORMS EV3, which has a speaker connected to PWM through an amplifier.

---
 drivers/input/misc/pwm-beeper.c | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
drivers/input/misc/pwm-beeper.c +20 -1
index 30ac227..708e88e 100644
@@ -14,6 +14,7 @@
  */
 
 #include <linux/input.h>
+#include <linux/regulator/consumer.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/of.h>
@@ -25,8 +26,10 @@
 struct pwm_beeper {
 	struct input_dev *input;
 	struct pwm_device *pwm;
+	struct regulator *reg;
 	struct work_struct work;
 	unsigned long period;
+	bool reg_enabled;
 };
 
 #define HZ_TO_NANOSECONDS(x) (1000000000UL/(x))
@@ -38,8 +41,20 @@ static void __pwm_beeper_set(struct pwm_beeper *beeper)
 	if (period) {
 		pwm_config(beeper->pwm, period / 2, period);
 		pwm_enable(beeper->pwm);
-	} else
+		if (beeper->reg) {
+			int error;
+
+			error = regulator_enable(beeper->reg);
+			if (!error)
+				beeper->reg_enabled = true;
+		}
+	} else {
+		if (beeper->reg_enabled) {
+			regulator_disable(beeper->reg);
+			beeper->reg_enabled = false;
+		}
 		pwm_disable(beeper->pwm);
+	}
 }
 
 static void pwm_beeper_work(struct work_struct *work)
@@ -82,6 +97,10 @@ static void pwm_beeper_stop(struct pwm_beeper *beeper)
 {
 	cancel_work_sync(&beeper->work);
 
+	if (beeper->reg_enabled) {
+		regulator_disable(beeper->reg);
+		beeper->reg_enabled = false;
+	}
 	if (beeper->period)
 		pwm_disable(beeper->pwm);
 }

drivers/input/misc/pwm-beeper.c static int pwm_beeper_probe(struct platform_device *pdev) · line 133

Quoted 4 lines
drivers/input/misc/pwm-beeper.c +1 -0
@@ -111,6 +130,14 @@ static int pwm_beeper_probe(struct platform_device *pdev)
 		return error;
 	}
 
+	beeper->reg = devm_regulator_get_optional(&pdev->dev, "amp");

If you do not use optional regulator then you will not have to check if you have it or not everywhere: regulator core will give you a dummy that you can toggle to your heart's content.

Quoted 13 lines
drivers/input/misc/pwm-beeper.c +7 -0
+	error = PTR_ERR_OR_ZERO(beeper->reg);
+	if (error) {
+		if (error != -EPROBE_DEFER)
+			dev_err(dev, "Failed to get amp regulator\n");
+		return error;
+	}
+
 	/*
 	 * FIXME: pwm_apply_args() should be removed when switching to
 	 * the atomic PWM API.
Signature · 1 line
2.7.4

Thanks.

Signature · 5 lines
Dmitry
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#7 lore raw

Re: [PATCH v2 3/3] Input: pwm-beeper: add optional amplifier regulator

Quoted 73 lines · Dmitry Torokhov
Quoted 72 lines · David Lechner

This adds an optional regulator to the pwm-beeper device. This regulator acts as an amplifier. The amplifier is only enabled while beeping in order to reduce power consumption.

Tested on LEGO MINDSTORMS EV3, which has a speaker connected to PWM through an amplifier.

---
 drivers/input/misc/pwm-beeper.c | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
drivers/input/misc/pwm-beeper.c +20 -1
index 30ac227..708e88e 100644
@@ -14,6 +14,7 @@
  */
 
 #include <linux/input.h>
+#include <linux/regulator/consumer.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/of.h>
@@ -25,8 +26,10 @@
 struct pwm_beeper {
 	struct input_dev *input;
 	struct pwm_device *pwm;
+	struct regulator *reg;
 	struct work_struct work;
 	unsigned long period;
+	bool reg_enabled;
 };
 
 #define HZ_TO_NANOSECONDS(x) (1000000000UL/(x))
@@ -38,8 +41,20 @@ static void __pwm_beeper_set(struct pwm_beeper *beeper)
 	if (period) {
 		pwm_config(beeper->pwm, period / 2, period);
 		pwm_enable(beeper->pwm);
-	} else
+		if (beeper->reg) {
+			int error;
+
+			error = regulator_enable(beeper->reg);
+			if (!error)
+				beeper->reg_enabled = true;
+		}
+	} else {
+		if (beeper->reg_enabled) {
+			regulator_disable(beeper->reg);
+			beeper->reg_enabled = false;
+		}
 		pwm_disable(beeper->pwm);
+	}
 }
 
 static void pwm_beeper_work(struct work_struct *work)
@@ -82,6 +97,10 @@ static void pwm_beeper_stop(struct pwm_beeper *beeper)
 {
 	cancel_work_sync(&beeper->work);
 
+	if (beeper->reg_enabled) {
+		regulator_disable(beeper->reg);
+		beeper->reg_enabled = false;
+	}
 	if (beeper->period)
 		pwm_disable(beeper->pwm);
 }
@@ -111,6 +130,14 @@ static int pwm_beeper_probe(struct platform_device *pdev)
 		return error;
 	}
 
Quoted 5 lines

drivers/input/misc/pwm-beeper.c static int pwm_beeper_probe(struct platform_device *pdev) · line 133

Quoted 1 line
drivers/input/misc/pwm-beeper.c +1 -0
+	beeper->reg = devm_regulator_get_optional(&pdev->dev, "amp");

If you do not use optional regulator then you will not have to check if you have it or not everywhere: regulator core will give you a dummy that you can toggle to your heart's content.

Some months ago, I learned that if you are not using device tree and you do not call regulator_has_full_constraints(), then you do not get a dummy regulator. And here, we are only checking if the regulator exists in one place. We will still need the checks for beeper->reg_enabled to keep calls to regulator_enable() and regulator_disable() balanced.

On the other hand, it is recommended that you always call regulator_has_full_constraints(), so I don't mind changing it if that is what you think we should do. But, I don't really see much of an advantage to changing it compared to the current implementation.

Quoted 17 lines
Quoted 12 lines
drivers/input/misc/pwm-beeper.c +7 -1
+	error = PTR_ERR_OR_ZERO(beeper->reg);
+	if (error) {
+		if (error != -EPROBE_DEFER)
+			dev_err(dev, "Failed to get amp regulator\n");
+		return error;
+	}
+
 	/*
 	 * FIXME: pwm_apply_args() should be removed when switching to
 	 * the atomic PWM API.
--
Quoted 2 lines

2.7.4

Thanks.

maintainer
#8 lore raw

Re: [PATCH v2 3/3] Input: pwm-beeper: add optional amplifier regulator

Quoted 80 lines · David Lechner
Quoted 73 lines · Dmitry Torokhov
Quoted 72 lines · David Lechner

This adds an optional regulator to the pwm-beeper device. This regulator acts as an amplifier. The amplifier is only enabled while beeping in order to reduce power consumption.

Tested on LEGO MINDSTORMS EV3, which has a speaker connected to PWM through an amplifier.

---
drivers/input/misc/pwm-beeper.c | 29 ++++++++++++++++++++++++++++-

1 file changed, 28 insertions(+), 1 deletion(-)
drivers/input/misc/pwm-beeper.c +20 -1
index 30ac227..708e88e 100644
@@ -14,6 +14,7 @@
 */
 
 #include <linux/input.h>
+#include <linux/regulator/consumer.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/of.h>
@@ -25,8 +26,10 @@
 struct pwm_beeper {
 	struct input_dev *input;
 	struct pwm_device *pwm;
+	struct regulator *reg;
 	struct work_struct work;
 	unsigned long period;
+	bool reg_enabled;
 };
 
 #define HZ_TO_NANOSECONDS(x) (1000000000UL/(x))
@@ -38,8 +41,20 @@ static void __pwm_beeper_set(struct pwm_beeper *beeper)
 	if (period) {
 		pwm_config(beeper->pwm, period / 2, period);
 		pwm_enable(beeper->pwm);
-	} else
+		if (beeper->reg) {
+			int error;
+
+			error = regulator_enable(beeper->reg);
+			if (!error)
+				beeper->reg_enabled = true;
+		}
+	} else {
+		if (beeper->reg_enabled) {
+			regulator_disable(beeper->reg);
+			beeper->reg_enabled = false;
+		}
 		pwm_disable(beeper->pwm);
+	}
 }
 
 static void pwm_beeper_work(struct work_struct *work)
@@ -82,6 +97,10 @@ static void pwm_beeper_stop(struct pwm_beeper *beeper)
 {
 	cancel_work_sync(&beeper->work);
 
+	if (beeper->reg_enabled) {
+		regulator_disable(beeper->reg);
+		beeper->reg_enabled = false;
+	}
 	if (beeper->period)
 		pwm_disable(beeper->pwm);
 }
@@ -111,6 +130,14 @@ static int pwm_beeper_probe(struct platform_device *pdev)
 		return error;
 	}
 
Quoted 5 lines

drivers/input/misc/pwm-beeper.c static int pwm_beeper_probe(struct platform_device *pdev) · line 133

Quoted 1 line
drivers/input/misc/pwm-beeper.c +1 -0
+	beeper->reg = devm_regulator_get_optional(&pdev->dev, "amp");

If you do not use optional regulator then you will not have to check if you have it or not everywhere: regulator core will give you a dummy that you can toggle to your heart's content.

Quoted 6 lines

Some months ago, I learned that if you are not using device tree and you do not call regulator_has_full_constraints(), then you do not get a dummy regulator. And here, we are only checking if the regulator exists in one place. We will still need the checks for beeper->reg_enabled to keep calls to regulator_enable() and regulator_disable() balanced.

Why? You do not have checks for calls to pwm_enable() and pwm_disable(), (or rather beeper->period is used as such flag) why regulator would be any different?

Quoted 5 lines

On the other hand, it is recommended that you always call regulator_has_full_constraints(), so I don't mind changing it if that is what you think we should do. But, I don't really see much of an advantage to changing it compared to the current implementation.

It greatly simplifies control flow in the driver (since I believe you can get rid of the flags you introduced).

As far as arch not having full constraints - I am not sure if this makes sense anymore. I am not quite sure what the original intent here was, we should probably ask Mark Brown. But a lot of drivers do expect the dummy substitution to imply work.

Thanks.

Signature · 1 line
Dmitry
#9 lore raw

Re: [PATCH v2 3/3] Input: pwm-beeper: add optional amplifier regulator

Quoted 88 lines · Dmitry Torokhov
Quoted 80 lines · David Lechner
Quoted 73 lines · Dmitry Torokhov
Quoted 72 lines · David Lechner

This adds an optional regulator to the pwm-beeper device. This regulator acts as an amplifier. The amplifier is only enabled while beeping in order to reduce power consumption.

Tested on LEGO MINDSTORMS EV3, which has a speaker connected to PWM through an amplifier.

---
drivers/input/misc/pwm-beeper.c | 29 ++++++++++++++++++++++++++++-

1 file changed, 28 insertions(+), 1 deletion(-)
drivers/input/misc/pwm-beeper.c +20 -1
index 30ac227..708e88e 100644
@@ -14,6 +14,7 @@
 */
 
 #include <linux/input.h>
+#include <linux/regulator/consumer.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/of.h>
@@ -25,8 +26,10 @@
 struct pwm_beeper {
 	struct input_dev *input;
 	struct pwm_device *pwm;
+	struct regulator *reg;
 	struct work_struct work;
 	unsigned long period;
+	bool reg_enabled;
 };
 
 #define HZ_TO_NANOSECONDS(x) (1000000000UL/(x))
@@ -38,8 +41,20 @@ static void __pwm_beeper_set(struct pwm_beeper *beeper)
 	if (period) {
 		pwm_config(beeper->pwm, period / 2, period);
 		pwm_enable(beeper->pwm);
-	} else
+		if (beeper->reg) {
+			int error;
+
+			error = regulator_enable(beeper->reg);
+			if (!error)
+				beeper->reg_enabled = true;
+		}
+	} else {
+		if (beeper->reg_enabled) {
+			regulator_disable(beeper->reg);
+			beeper->reg_enabled = false;
+		}
 		pwm_disable(beeper->pwm);
+	}
 }
 
 static void pwm_beeper_work(struct work_struct *work)
@@ -82,6 +97,10 @@ static void pwm_beeper_stop(struct pwm_beeper *beeper)
 {
 	cancel_work_sync(&beeper->work);
 
+	if (beeper->reg_enabled) {
+		regulator_disable(beeper->reg);
+		beeper->reg_enabled = false;
+	}
 	if (beeper->period)
 		pwm_disable(beeper->pwm);
 }
@@ -111,6 +130,14 @@ static int pwm_beeper_probe(struct platform_device *pdev)
 		return error;
 	}
 
Quoted 5 lines

drivers/input/misc/pwm-beeper.c static int pwm_beeper_probe(struct platform_device *pdev) · line 133

Quoted 1 line
drivers/input/misc/pwm-beeper.c +1 -0
+	beeper->reg = devm_regulator_get_optional(&pdev->dev, "amp");

If you do not use optional regulator then you will not have to check if you have it or not everywhere: regulator core will give you a dummy that you can toggle to your heart's content.

Quoted 6 lines

Some months ago, I learned that if you are not using device tree and you do not call regulator_has_full_constraints(), then you do not get a dummy regulator. And here, we are only checking if the regulator exists in one place. We will still need the checks for beeper->reg_enabled to keep calls to regulator_enable() and regulator_disable() balanced.

Quoted 3 lines

Why? You do not have checks for calls to pwm_enable() and pwm_disable(), (or rather beeper->period is used as such flag) why regulator would be any different?

regulator_enable() has a __must_check attribute on it, so we get compiler warnings if we do not check the return value. Also, if enabling the regulator fails and returns an error, then calling regulator_disable() later would cause an imbalance.

pwm_enable() and pwm_disable() work differently because they don't count how many times they have been called. regulator_enable() and regulator_disable(), on the other hand, work like reference counting.

Quoted 7 lines
Quoted 6 lines

On the other hand, it is recommended that you always call regulator_has_full_constraints(), so I don't mind changing it if that is what you think we should do. But, I don't really see much of an advantage to changing it compared to the current implementation.

Quoted 7 lines

It greatly simplifies control flow in the driver (since I believe you can get rid of the flags you introduced).

As far as arch not having full constraints - I am not sure if this makes sense anymore. I am not quite sure what the original intent here was, we should probably ask Mark Brown. But a lot of drivers do expect the dummy substitution to imply work.

I am OK with using the dummy regulator, but I don't see how I can get rid of the beeper->reg_enabled flag.

maintainer Acked-by on v2 2/3
#4 lore raw

Re: [PATCH v2 2/3] dt-bindings: Input: Add optional amp-supply property to pwm-beeper

In reply to David Lechner
Quoted 7 lines · David Lechner

This adds an optional amp-supply property to pwm-beeper. This is a regulator that acts as an amplifier for the beeper.

---
 Documentation/devicetree/bindings/input/pwm-beeper.txt | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

maintainer
#10 lore raw

Re: [PATCH v2 3/3] Input: pwm-beeper: add optional amplifier regulator

In reply to David Lechner
Quoted 98 lines · David Lechner
Quoted 88 lines · Dmitry Torokhov
Quoted 80 lines · David Lechner
Quoted 73 lines · Dmitry Torokhov
Quoted 72 lines · David Lechner

This adds an optional regulator to the pwm-beeper device. This regulator acts as an amplifier. The amplifier is only enabled while beeping in order to reduce power consumption.

Tested on LEGO MINDSTORMS EV3, which has a speaker connected to PWM through an amplifier.

---
drivers/input/misc/pwm-beeper.c | 29 ++++++++++++++++++++++++++++-

1 file changed, 28 insertions(+), 1 deletion(-)
drivers/input/misc/pwm-beeper.c +20 -1
index 30ac227..708e88e 100644
@@ -14,6 +14,7 @@
 */
 
 #include <linux/input.h>
+#include <linux/regulator/consumer.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/of.h>
@@ -25,8 +26,10 @@
 struct pwm_beeper {
 	struct input_dev *input;
 	struct pwm_device *pwm;
+	struct regulator *reg;
 	struct work_struct work;
 	unsigned long period;
+	bool reg_enabled;
 };
 
 #define HZ_TO_NANOSECONDS(x) (1000000000UL/(x))
@@ -38,8 +41,20 @@ static void __pwm_beeper_set(struct pwm_beeper *beeper)
 	if (period) {
 		pwm_config(beeper->pwm, period / 2, period);
 		pwm_enable(beeper->pwm);
-	} else
+		if (beeper->reg) {
+			int error;
+
+			error = regulator_enable(beeper->reg);
+			if (!error)
+				beeper->reg_enabled = true;
+		}
+	} else {
+		if (beeper->reg_enabled) {
+			regulator_disable(beeper->reg);
+			beeper->reg_enabled = false;
+		}
 		pwm_disable(beeper->pwm);
+	}
 }
 
 static void pwm_beeper_work(struct work_struct *work)
@@ -82,6 +97,10 @@ static void pwm_beeper_stop(struct pwm_beeper *beeper)
 {
 	cancel_work_sync(&beeper->work);
 
+	if (beeper->reg_enabled) {
+		regulator_disable(beeper->reg);
+		beeper->reg_enabled = false;
+	}
 	if (beeper->period)
 		pwm_disable(beeper->pwm);
 }
@@ -111,6 +130,14 @@ static int pwm_beeper_probe(struct platform_device *pdev)
 		return error;
 	}
 
Quoted 5 lines

drivers/input/misc/pwm-beeper.c static int pwm_beeper_probe(struct platform_device *pdev) · line 133

Quoted 1 line
drivers/input/misc/pwm-beeper.c +1 -0
+	beeper->reg = devm_regulator_get_optional(&pdev->dev, "amp");

If you do not use optional regulator then you will not have to check if you have it or not everywhere: regulator core will give you a dummy that you can toggle to your heart's content.

Quoted 6 lines

Some months ago, I learned that if you are not using device tree and you do not call regulator_has_full_constraints(), then you do not get a dummy regulator. And here, we are only checking if the regulator exists in one place. We will still need the checks for beeper->reg_enabled to keep calls to regulator_enable() and regulator_disable() balanced.

Quoted 3 lines

Why? You do not have checks for calls to pwm_enable() and pwm_disable(), (or rather beeper->period is used as such flag) why regulator would be any different?

regulator_enable() has a __must_check attribute on it, so we get compiler warnings if we do not check the return value. Also, if enabling the regulator fails and returns an error, then calling regulator_disable() later would cause an imbalance.

Quoted 4 lines

pwm_enable() and pwm_disable() work differently because they don't count how many times they have been called. regulator_enable() and regulator_disable(), on the other hand, work like reference counting.

Ah, you are right, but it is more than that. It is possible to receive multiple SND_BELL/SND_TONE events with non-0 value. You need to check if regulator is already enabled before trying to enable it second time, or your counting will be off.

Thanks.

Signature · 1 line
Dmitry