Skip to contentSkip to conversation

pwm: pwm-gpio: New driver

PATCH In review Nicola Di Lieto 8 participants 25 messages Dec 5, 2020 – Jan 28, 2024

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

PATCH v2 1/2 pwm: pwm-gpio: New driver

This new driver allows pulse width modulating any GPIOs using a high resolution timer. It is fully generic and can be useful in a variety of situations. As an example I used it to provide a pwm to the pwm-beeper driver so that my embedded system can produce tones through a piezo buzzer connected to a GPIO which unfortunately is not hardware PWM capable.

---

MAINTAINERS            |   7 ++
drivers/pwm/Kconfig    |  10 +++
drivers/pwm/Makefile   |   1 +
drivers/pwm/pwm-gpio.c | 172 +++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 190 insertions(+)

create mode 100644 drivers/pwm/pwm-gpio.c

MAINTAINERS +0 -0
index ebe4829cdd4d..fe9b5b00ba94 100644
@@ -14215,6 +14215,13 @@ F:	Documentation/devicetree/bindings/hwmon/pwm-fan.txt

F: Documentation/hwmon/pwm-fan.rst
F: drivers/hwmon/pwm-fan.c

+PWM GPIO DRIVER
+M: Nicola Di Lieto <[email protected]>
+L: [email protected]
+S: Maintained
+F: Documentation/devicetree/bindings/pwm/pwm-gpio.yaml
+F: drivers/pwm/pwm-gpio.c
+
PWM IR Transmitter
M: Sean Young <[email protected]>
L: [email protected]

drivers/pwm/Kconfig +0 -0
index 63be5362fd3a..5432084c6276 100644
@@ -181,6 +181,16 @@ config PWM_FSL_FTM

To compile this driver as a module, choose M here: the module will be called pwm-fsl-ftm.

+config PWM_GPIO
+ tristate "PWM GPIO support"
+ depends on GPIOLIB
+ depends on HIGH_RES_TIMERS
+ help
+ Generic PWM for software modulation of GPIOs
+
+ To compile this driver as a module, choose M here: the module
+ will be called pwm-gpio.
+
config PWM_HIBVT

	tristate "HiSilicon BVT PWM support"
	depends on ARCH_HISI || COMPILE_TEST
drivers/pwm/Makefile +0 -0
index cbdcd55d69ee..eea0216215a7 100644
@@ -15,6 +15,7 @@ obj-$(CONFIG_PWM_CRC)		+= pwm-crc.o

obj-$(CONFIG_PWM_CROS_EC) += pwm-cros-ec.o
obj-$(CONFIG_PWM_EP93XX) += pwm-ep93xx.o
obj-$(CONFIG_PWM_FSL_FTM) += pwm-fsl-ftm.o
+obj-$(CONFIG_PWM_GPIO) += pwm-gpio.o
obj-$(CONFIG_PWM_HIBVT) += pwm-hibvt.o
obj-$(CONFIG_PWM_IMG) += pwm-img.o
obj-$(CONFIG_PWM_IMX1) += pwm-imx1.o

drivers/pwm/pwm-gpio.c new file +172 -0

172 lines not on this page · raw message

Signature · 1 line
2.11.0
#1 lore raw

Hello Nicola,

Quoted 21 lines · Nicola Di Lieto

This new driver allows pulse width modulating any GPIOs using a high resolution timer. It is fully generic and can be useful in a variety of situations. As an example I used it to provide a pwm to the pwm-beeper driver so that my embedded system can produce tones through a piezo buzzer connected to a GPIO which unfortunately is not hardware PWM capable.

---

MAINTAINERS            |   7 ++
drivers/pwm/Kconfig    |  10 +++
drivers/pwm/Makefile   |   1 +
drivers/pwm/pwm-gpio.c | 172 +++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 190 insertions(+)

create mode 100644 drivers/pwm/pwm-gpio.c

MAINTAINERS +0 -0
index ebe4829cdd4d..fe9b5b00ba94 100644
@@ -14215,6 +14215,13 @@ F:	Documentation/devicetree/bindings/hwmon/pwm-fan.txt
Quoted 3 lines

F: Documentation/hwmon/pwm-fan.rst
F: drivers/hwmon/pwm-fan.c

The patch is whitespace damaged. Also it would be good if the patch set was sent in a single mail thread. I suggest you look into git-send-email for the next round.

Quoted 102 lines
MAINTAINERS +7 -0
+PWM GPIO DRIVER
+M:	Nicola Di Lieto <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/pwm/pwm-gpio.yaml
+F:	drivers/pwm/pwm-gpio.c
+

PWM IR Transmitter
M: Sean Young <[email protected]>
L: [email protected]

drivers/pwm/Kconfig +10 -0
index 63be5362fd3a..5432084c6276 100644
@@ -181,6 +181,16 @@ config PWM_FSL_FTM
 	  To compile this driver as a module, choose M here: the module
 	  will be called pwm-fsl-ftm.
 
+config PWM_GPIO
+	tristate "PWM GPIO support"
+	depends on GPIOLIB
+	depends on HIGH_RES_TIMERS
+	help
+	  Generic PWM for software modulation of GPIOs
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called pwm-gpio.
+

config PWM_HIBVT

	tristate "HiSilicon BVT PWM support"
	depends on ARCH_HISI || COMPILE_TEST
drivers/pwm/Makefile +0 -0
index cbdcd55d69ee..eea0216215a7 100644
@@ -15,6 +15,7 @@ obj-$(CONFIG_PWM_CRC)		+= pwm-crc.o

obj-$(CONFIG_PWM_CROS_EC) += pwm-cros-ec.o
obj-$(CONFIG_PWM_EP93XX) += pwm-ep93xx.o
obj-$(CONFIG_PWM_FSL_FTM) += pwm-fsl-ftm.o
+obj-$(CONFIG_PWM_GPIO) += pwm-gpio.o
obj-$(CONFIG_PWM_HIBVT) += pwm-hibvt.o
obj-$(CONFIG_PWM_IMG) += pwm-img.o
obj-$(CONFIG_PWM_IMX1) += pwm-imx1.o

drivers/pwm/pwm-gpio.c new file +53 -0
new file mode 100644
index 000000000000..6f425f2d02fe
@@ -0,0 +1,172 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Generic software PWM for modulating GPIOs
+ *
+ * Copyright 2020 Nicola Di Lieto
+ *
+ * Author: Nicola Di Lieto <[email protected]>
+ */
+
+#include <linux/atomic.h>
+#include <linux/err.h>
+#include <linux/gpio/consumer.h>
+#include <linux/hrtimer.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/pwm.h>
+#include <linux/workqueue.h>
+
+struct pwm_gpio {
+	struct pwm_chip chip;
+	struct gpio_desc *desc;
+	struct work_struct work;
+	struct hrtimer timer;
+	atomic_t enabled;
+	u64 ton_ns;
+	u64 toff_ns;
+	enum pwm_polarity polarity;
+	bool output;
+};
+
+static void pwm_gpio_work(struct work_struct *work)
+{
+	struct pwm_gpio *pwm_gpio = container_of(work, struct pwm_gpio, work);
+
+	gpiod_set_value_cansleep(pwm_gpio->desc,
+		(pwm_gpio->polarity == PWM_POLARITY_INVERSED) ^ pwm_gpio->output);
+}
+
+enum hrtimer_restart pwm_gpio_do_timer(struct hrtimer *handle)
+{
+	struct pwm_gpio *pwm_gpio = container_of(handle, struct pwm_gpio, timer);
+	u64 ns;
+
+	if (!atomic_read(&pwm_gpio->enabled))
+		return HRTIMER_NORESTART;
+
+	if (pwm_gpio->output) {
+		ns = pwm_gpio->toff_ns;
+		pwm_gpio->output = false;
+	} else {
+		ns = pwm_gpio->ton_ns;

drivers/pwm/pwm-gpio.c line 63

Quoted 10 lines
drivers/pwm/pwm-gpio.c new file +10 -0
+		pwm_gpio->output = true;
+	}
+
+	schedule_work(&pwm_gpio->work);
+	hrtimer_forward(handle, hrtimer_get_expires(handle), ns_to_ktime(ns));
+
+	return HRTIMER_RESTART;
+}
+
+static inline struct pwm_gpio *to_pwm_gpio(struct pwm_chip *_chip)

If you rename this to "pwm_gpio_from_chip" it shares the prefix that all other functions and global variables in your driver use.

Quoted 8 lines
drivers/pwm/pwm-gpio.c new file +8 -0
+{
+	return container_of(_chip, struct pwm_gpio, chip);
+}
+
+static void pwm_gpio_free(struct pwm_chip *chip, struct pwm_device *pwm)
+{
+	struct pwm_gpio *pwm_gpio = to_pwm_gpio(chip);
+

drivers/pwm/pwm-gpio.c line 81

Quoted 10 lines
drivers/pwm/pwm-gpio.c new file +10 -0
+	cancel_work_sync(&pwm_gpio->work);
+	gpiod_set_value_cansleep(pwm_gpio->desc,
+		pwm_gpio->polarity == PWM_POLARITY_INVERSED);
+}
+
+static int pwm_gpio_apply(struct pwm_chip *chip, struct pwm_device *pwm,
+			  const struct pwm_state *state)
+{
+	struct pwm_gpio *pwm_gpio = to_pwm_gpio(chip);
+	u64 period = clamp(state->period, U64_C(50000), U64_C(1000000000));

.apply is expected to implement the biggest period that is not bigger than the requested period. So clamping to a value > 50000 is wrong.

Also the (arbitrary?) choice to not implement periods < 50 µs needs a comment. (I assume it is something like: the pwm-gpio driver suffers from latency in the timer and gpio frameworks, smaller periods are expected to be impossible to be implemented.)

drivers/pwm/pwm-gpio.c line 82

Quoted 1 line
drivers/pwm/pwm-gpio.c new file +1 -0
+	u64 duty_cycle = clamp(state->duty_cycle, U64_C(0), period);

If you consider that a period < 50 µs is problematic, I wonder if you have similar concerns for a duty_cycle of 25 µs.

drivers/pwm/pwm-gpio.c line 85

Quoted 2 lines
drivers/pwm/pwm-gpio.c new file +2 -0
+	pwm_gpio->ton_ns = duty_cycle;
+	pwm_gpio->toff_ns = period - duty_cycle;

If the timer triggers here it has an inconsistent view because ton and toff are already updated, but polarity isn't.

drivers/pwm/pwm-gpio.c line 95

Quoted 10 lines
drivers/pwm/pwm-gpio.c new file +10 -0
+	pwm_gpio->polarity = state->polarity;
+
+	if (state->enabled && !atomic_read(&pwm_gpio->enabled)) {
+		atomic_set(&pwm_gpio->enabled, 1);
+		hrtimer_start(&pwm_gpio->timer, 0, HRTIMER_MODE_REL);
+	} else if (!state->enabled && atomic_read(&pwm_gpio->enabled)) {
+		atomic_set(&pwm_gpio->enabled, 0);
+		pwm_gpio->output = false;
+		schedule_work(&pwm_gpio->work);
+	}

It's not obvious to me that this driver completes the currently running period before the new setting takes effect. If you think it is, please make the mechanism more obvious in a comment, if it isn't, please fix.

Quoted 30 lines
drivers/pwm/pwm-gpio.c new file +30 -0
+	return 0;
+}
+
+static void pwm_gpio_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
+			       struct pwm_state *state)
+{
+	struct pwm_gpio *pwm_gpio = to_pwm_gpio(chip);
+
+	state->duty_cycle = pwm_gpio->ton_ns;
+	state->period = pwm_gpio->ton_ns + pwm_gpio->toff_ns;
+	state->polarity = pwm_gpio->polarity;
+	state->enabled = atomic_read(&pwm_gpio->enabled);
+}
+
+static const struct pwm_ops pwm_gpio_ops = {
+	.free = pwm_gpio_free,
+	.apply = pwm_gpio_apply,
+	.get_state = pwm_gpio_get_state,
+	.owner = THIS_MODULE,
+};
+
+static int pwm_gpio_probe(struct platform_device *pdev)
+{
+	struct pwm_gpio *pwm_gpio;
+
+	pwm_gpio = devm_kzalloc(&pdev->dev, sizeof(*pwm_gpio), GFP_KERNEL);
+	if (!pwm_gpio)
+		return -ENOMEM;
+
+	pwm_gpio->desc = devm_gpiod_get(&pdev->dev, NULL, GPIOD_OUT_LOW);

drivers/pwm/pwm-gpio.c line 135

Quoted 10 lines
drivers/pwm/pwm-gpio.c new file +10 -0
+	if (IS_ERR(pwm_gpio->desc))
+		return PTR_ERR(pwm_gpio->desc);
+
+	INIT_WORK(&pwm_gpio->work, pwm_gpio_work);
+
+	hrtimer_init(&pwm_gpio->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+	pwm_gpio->timer.function = pwm_gpio_do_timer;
+	pwm_gpio->chip.dev = &pdev->dev;
+	pwm_gpio->chip.ops = &pwm_gpio_ops;
+	pwm_gpio->chip.npwm = 1;
	pwm_gpio->base = -1;

drivers/pwm/pwm-gpio.c line 148

Quoted 12 lines
drivers/pwm/pwm-gpio.c new file +12 -0
+	platform_set_drvdata(pdev, pwm_gpio);
+
+	return pwmchip_add(&pwm_gpio->chip);
+}
+
+static int pwm_gpio_remove(struct platform_device *pdev)
+{
+	struct pwm_gpio *pwm_gpio = platform_get_drvdata(pdev);
+
+	hrtimer_cancel(&pwm_gpio->timer);
+
+	return pwmchip_remove(&pwm_gpio->chip);

Please swap the order of hrtimer_cancel and pwmchip_remove to have it in reverse order to the allocation in .probe and to make the PWM unfunctional only after the pwmchip is unregistered.

Quoted 14 lines
drivers/pwm/pwm-gpio.c new file +14 -0
+}
+
+#ifdef CONFIG_OF
+static const struct of_device_id pwm_gpio_of_match[] = {
+	{ .compatible = "pwm-gpio", },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, pwm_gpio_of_match);
+#endif
+
+static struct platform_driver pwm_gpio_driver = {
+	.probe = pwm_gpio_probe,
+	.remove = pwm_gpio_remove,
+	.driver = {

drivers/pwm/pwm-gpio.c line 172

Quoted 10 lines
drivers/pwm/pwm-gpio.c new file +10 -0
+		.name = "pwm-gpio",
+		.of_match_table = of_match_ptr(pwm_gpio_of_match),
+	},
+};
+module_platform_driver(pwm_gpio_driver);
+
+MODULE_DESCRIPTION("PWM GPIO driver");
+MODULE_ALIAS("platform:pwm-gpio");
+MODULE_AUTHOR("Nicola Di Lieto");
+MODULE_LICENSE("GPL");

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
#2 lore raw

v2 cover pwm: pwm-gpio: New driver

New version, implementing Uwe's suggestions.

Changes in v2:
- Synchronize pwm variables to ensure consistency
- Remove clamping of pwm period and duty cycle
- Rename "to_pwm_gpio" as "pwm_gpio_from_chip"
- Initialize pwm_gpio->chip.base to -1
- Swap order of hrtimer_cancel and pwmchip_remove
- Increase #pwm-cells to 3

Nicola Di Lieto (2):
pwm: pwm-gpio: New driver
pwm: pwm-gpio: Add DT bindings

 .../devicetree/bindings/pwm/pwm-gpio.yaml          |  42 +++++
 MAINTAINERS                                        |   7 +
 drivers/pwm/Kconfig                                |  10 ++
 drivers/pwm/Makefile                               |   1 +
 drivers/pwm/pwm-gpio.c                             | 188 +++++++++++++++++++++
 5 files changed, 248 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pwm/pwm-gpio.yaml
 create mode 100644 drivers/pwm/pwm-gpio.c
Signature · 1 line
2.11.0
#3 lore raw

PATCH v2 1/2 pwm: pwm-gpio: New driver

This new driver allows pulse width modulating any GPIOs using a high resolution timer. It is fully generic and can be useful in a variety of situations. As an example I used it to provide a pwm to the pwm-beeper driver so that my embedded system can produce tones through a piezo buzzer connected to a GPIO which unfortunately is not hardware PWM capable.

---
 MAINTAINERS            |   7 ++
 drivers/pwm/Kconfig    |  10 +++
 drivers/pwm/Makefile   |   1 +
 drivers/pwm/pwm-gpio.c | 188 +++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 206 insertions(+)
 create mode 100644 drivers/pwm/pwm-gpio.c
MAINTAINERS +7 -0
index 52086876ce40..486a8857b47b 100644
@@ -14222,6 +14222,13 @@ F:	Documentation/devicetree/bindings/hwmon/pwm-fan.txt
 F:	Documentation/hwmon/pwm-fan.rst
 F:	drivers/hwmon/pwm-fan.c
 
+PWM GPIO DRIVER
+M:	Nicola Di Lieto <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/pwm/pwm-gpio.yaml
+F:	drivers/pwm/pwm-gpio.c
+
 PWM IR Transmitter
 M:	Sean Young <[email protected]>
 L:	[email protected]
drivers/pwm/Kconfig +10 -0
index 63be5362fd3a..5432084c6276 100644
@@ -181,6 +181,16 @@ config PWM_FSL_FTM
 	  To compile this driver as a module, choose M here: the module
 	  will be called pwm-fsl-ftm.
 
+config PWM_GPIO
+	tristate "PWM GPIO support"
+	depends on GPIOLIB
+	depends on HIGH_RES_TIMERS
+	help
+	  Generic PWM for software modulation of GPIOs
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called pwm-gpio.
+
 config PWM_HIBVT
 	tristate "HiSilicon BVT PWM support"
 	depends on ARCH_HISI || COMPILE_TEST
drivers/pwm/Makefile +1 -0
index cbdcd55d69ee..eea0216215a7 100644
@@ -15,6 +15,7 @@ obj-$(CONFIG_PWM_CRC)		+= pwm-crc.o
 obj-$(CONFIG_PWM_CROS_EC)	+= pwm-cros-ec.o
 obj-$(CONFIG_PWM_EP93XX)	+= pwm-ep93xx.o
 obj-$(CONFIG_PWM_FSL_FTM)	+= pwm-fsl-ftm.o
+obj-$(CONFIG_PWM_GPIO)		+= pwm-gpio.o
 obj-$(CONFIG_PWM_HIBVT)		+= pwm-hibvt.o
 obj-$(CONFIG_PWM_IMG)		+= pwm-img.o
 obj-$(CONFIG_PWM_IMX1)		+= pwm-imx1.o
drivers/pwm/pwm-gpio.c new file +188 -0

188 lines not on this page · raw message

Signature · 1 line
2.11.0
#4 lore raw

Hello,

[added the gpio people to Cc:]

Quoted 21 lines · Nicola Di Lieto

This new driver allows pulse width modulating any GPIOs using a high resolution timer. It is fully generic and can be useful in a variety of situations. As an example I used it to provide a pwm to the pwm-beeper driver so that my embedded system can produce tones through a piezo buzzer connected to a GPIO which unfortunately is not hardware PWM capable.

---
 MAINTAINERS            |   7 ++
 drivers/pwm/Kconfig    |  10 +++
 drivers/pwm/Makefile   |   1 +
 drivers/pwm/pwm-gpio.c | 188 +++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 206 insertions(+)
 create mode 100644 drivers/pwm/pwm-gpio.c
MAINTAINERS +0 -0
index 52086876ce40..486a8857b47b 100644

MAINTAINERS F: Documentation/devicetree/bindings/hwmon/pwm-fan.txt · line 14230

Quoted 9 lines
MAINTAINERS +6 -0
@@ -14222,6 +14222,13 @@ F:	Documentation/devicetree/bindings/hwmon/pwm-fan.txt
 F:	Documentation/hwmon/pwm-fan.rst
 F:	drivers/hwmon/pwm-fan.c
 
+PWM GPIO DRIVER
+M:	Nicola Di Lieto <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/pwm/pwm-gpio.yaml
+F:	drivers/pwm/pwm-gpio.c

Maybe only add the line for the binding doc in the second patch?

Quoted 69 lines
MAINTAINERS +1 -0
+
 PWM IR Transmitter
 M:	Sean Young <[email protected]>
 L:	[email protected]
drivers/pwm/Kconfig +10 -0
index 63be5362fd3a..5432084c6276 100644
@@ -181,6 +181,16 @@ config PWM_FSL_FTM
 	  To compile this driver as a module, choose M here: the module
 	  will be called pwm-fsl-ftm.
 
+config PWM_GPIO
+	tristate "PWM GPIO support"
+	depends on GPIOLIB
+	depends on HIGH_RES_TIMERS
+	help
+	  Generic PWM for software modulation of GPIOs
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called pwm-gpio.
+
 config PWM_HIBVT
 	tristate "HiSilicon BVT PWM support"
 	depends on ARCH_HISI || COMPILE_TEST
drivers/pwm/Makefile +1 -0
index cbdcd55d69ee..eea0216215a7 100644
@@ -15,6 +15,7 @@ obj-$(CONFIG_PWM_CRC)		+= pwm-crc.o
 obj-$(CONFIG_PWM_CROS_EC)	+= pwm-cros-ec.o
 obj-$(CONFIG_PWM_EP93XX)	+= pwm-ep93xx.o
 obj-$(CONFIG_PWM_FSL_FTM)	+= pwm-fsl-ftm.o
+obj-$(CONFIG_PWM_GPIO)		+= pwm-gpio.o
 obj-$(CONFIG_PWM_HIBVT)		+= pwm-hibvt.o
 obj-$(CONFIG_PWM_IMG)		+= pwm-img.o
 obj-$(CONFIG_PWM_IMX1)		+= pwm-imx1.o
drivers/pwm/pwm-gpio.c new file +26 -0
new file mode 100644
index 000000000000..06b4ddee389d
@@ -0,0 +1,188 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Generic software PWM for modulating GPIOs
+ *
+ * Copyright 2020 Nicola Di Lieto
+ *
+ * Author: Nicola Di Lieto <[email protected]>
+ */
+
+#include <linux/atomic.h>
+#include <linux/err.h>
+#include <linux/gpio/consumer.h>
+#include <linux/hrtimer.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/pwm.h>
+#include <linux/spinlock.h>
+#include <linux/workqueue.h>
+
+struct pwm_gpio {
+	struct pwm_chip chip;
+	struct gpio_desc *desc;
+	struct work_struct work;
+	struct hrtimer timer;

drivers/pwm/pwm-gpio.c line 36

Quoted 10 lines
drivers/pwm/pwm-gpio.c new file +10 -0
+	atomic_t enabled;
+	spinlock_t lock;
+	struct {
+		u64 ton_ns;
+		u64 toff_ns;
+		bool invert;
+	} cur, new;
+	bool state;
+	bool output;
+};

I would have called this struct pwm_gpio_ddata. Given that pwm_gpio is the common prefix of all variables and functions in this driver, pwm_gpio alone is a bit short.

drivers/pwm/pwm-gpio.c line 42

Quoted 5 lines
drivers/pwm/pwm-gpio.c new file +5 -0
+static void pwm_gpio_work(struct work_struct *work)
+{
+	struct pwm_gpio *pwm_gpio = container_of(work, struct pwm_gpio, work);
+
+	gpiod_set_value_cansleep(pwm_gpio->desc, pwm_gpio->output);

Usually you want to check the return value of gpiod_set_value. @Linus +
Bartosz: What do you think, does it need checking for an error here?

Quoted 15 lines
drivers/pwm/pwm-gpio.c new file +15 -0
+}
+
+enum hrtimer_restart pwm_gpio_do_timer(struct hrtimer *handle)
+{
+	struct pwm_gpio *pwm_gpio = container_of(handle, struct pwm_gpio, timer);
+	u64 ns;
+
+	if (!atomic_read(&pwm_gpio->enabled))
+		return HRTIMER_NORESTART;
+
+	if (pwm_gpio->state) {
+		ns = pwm_gpio->cur.toff_ns;
+		pwm_gpio->state = false;
+	} else {
+		if (spin_trylock(&pwm_gpio->lock)) {

drivers/pwm/pwm-gpio.c line 67

Quoted 10 lines
drivers/pwm/pwm-gpio.c new file +10 -0
+			pwm_gpio->cur = pwm_gpio->new;
+			spin_unlock(&pwm_gpio->lock);
+		}
+		ns = pwm_gpio->cur.ton_ns;
+		pwm_gpio->state = true;
+	}
+	pwm_gpio->output = pwm_gpio->state ^ pwm_gpio->cur.invert;
+
+	schedule_work(&pwm_gpio->work);
+	hrtimer_forward(handle, hrtimer_get_expires(handle), ns_to_ktime(ns));

This is hard to understand. I think we need more comments explaining (e.g.) the semantic of the members of struct pwm_gpio.

Does it make sense to use the workqueue only if the GPIO is a sleeping one and for fast GPIOs call gpiod_set_value directly?

Quoted 42 lines
drivers/pwm/pwm-gpio.c new file +42 -0
+
+	return HRTIMER_RESTART;
+}
+
+static inline struct pwm_gpio *pwm_gpio_from_chip(struct pwm_chip *_chip)
+{
+	return container_of(_chip, struct pwm_gpio, chip);
+}
+
+static void pwm_gpio_free(struct pwm_chip *chip, struct pwm_device *pwm)
+{
+	struct pwm_gpio *pwm_gpio = pwm_gpio_from_chip(chip);
+
+	cancel_work_sync(&pwm_gpio->work);
+	gpiod_set_value_cansleep(pwm_gpio->desc, pwm_gpio->cur.invert);
+}
+
+static int pwm_gpio_apply(struct pwm_chip *chip, struct pwm_device *pwm,
+			  const struct pwm_state *state)
+{
+	struct pwm_gpio *pwm_gpio = pwm_gpio_from_chip(chip);
+
+	spin_lock(&pwm_gpio->lock);
+	pwm_gpio->new.ton_ns = state->duty_cycle;
+	pwm_gpio->new.toff_ns = state->period - state->duty_cycle;
+	pwm_gpio->new.invert = (state->polarity == PWM_POLARITY_INVERSED);
+	spin_unlock(&pwm_gpio->lock);
+
+	if (state->enabled && !atomic_cmpxchg(&pwm_gpio->enabled, 0, 1)) {
+		hrtimer_start(&pwm_gpio->timer, 0, HRTIMER_MODE_REL);
+	} else if (!state->enabled && atomic_cmpxchg(&pwm_gpio->enabled, 1, 0)) {
+		pwm_gpio->state = false;
+		pwm_gpio->output = (state->polarity == PWM_POLARITY_INVERSED);
+		schedule_work(&pwm_gpio->work);
+	}
+	return 0;
+}
+
+static void pwm_gpio_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
+			       struct pwm_state *state)
+{
+	struct pwm_gpio *pwm_gpio = pwm_gpio_from_chip(chip);

drivers/pwm/pwm-gpio.c line 119

Quoted 10 lines
drivers/pwm/pwm-gpio.c new file +10 -0
+
+	state->duty_cycle = pwm_gpio->new.ton_ns;
+	state->period = pwm_gpio->new.ton_ns + pwm_gpio->new.toff_ns;
+	state->polarity = pwm_gpio->new.invert ? PWM_POLARITY_INVERSED
+					       : PWM_POLARITY_NORMAL;
+	state->enabled = atomic_read(&pwm_gpio->enabled);
+}
+
+static const struct pwm_ops pwm_gpio_ops = {
+	.free = pwm_gpio_free,

A free without request seems wrong. The callback stops the PWM, this is wrong, the PWM should continue to toggle.

Quoted 21 lines
drivers/pwm/pwm-gpio.c new file +21 -0
+	.apply = pwm_gpio_apply,
+	.get_state = pwm_gpio_get_state,
+	.owner = THIS_MODULE,
+};
+
+static int pwm_gpio_probe(struct platform_device *pdev)
+{
+	struct pwm_gpio *pwm_gpio;
+
+	pwm_gpio = devm_kzalloc(&pdev->dev, sizeof(*pwm_gpio), GFP_KERNEL);
+	if (!pwm_gpio)
+		return -ENOMEM;
+
+	pwm_gpio->desc = devm_gpiod_get(&pdev->dev, NULL, GPIOD_OUT_LOW);
+	if (IS_ERR(pwm_gpio->desc))
+		return PTR_ERR(pwm_gpio->desc);
+
+	INIT_WORK(&pwm_gpio->work, pwm_gpio_work);
+
+	hrtimer_init(&pwm_gpio->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+	pwm_gpio->timer.function = pwm_gpio_do_timer;

drivers/pwm/pwm-gpio.c line 150

Quoted 10 lines
drivers/pwm/pwm-gpio.c new file +10 -0
+	pwm_gpio->chip.dev = &pdev->dev;
+	pwm_gpio->chip.ops = &pwm_gpio_ops;
+	pwm_gpio->chip.npwm = 1;
+	pwm_gpio->chip.base = -1;
+
+	platform_set_drvdata(pdev, pwm_gpio);
+
+	spin_lock_init(&pwm_gpio->lock);
+
+	return pwmchip_add(&pwm_gpio->chip);

Error message please if pwmchip_add fails

drivers/pwm/pwm-gpio.c line 160

Quoted 10 lines
drivers/pwm/pwm-gpio.c new file +10 -0
+}
+
+static int pwm_gpio_remove(struct platform_device *pdev)
+{
+	int ret;
+	struct pwm_gpio *pwm_gpio = platform_get_drvdata(pdev);
+
+	ret = pwmchip_remove(&pwm_gpio->chip);
+	if (ret)
+		return ret;

This exit path is bad. The return value of the remove callback is ignored and after pwm_gpio_remove() returns the gpio and *pwm_gpio are freed.

drivers/pwm/pwm-gpio.c line 165

Quoted 5 lines
drivers/pwm/pwm-gpio.c new file +5 -0
+
+	hrtimer_cancel(&pwm_gpio->timer);
+
+	return 0;
+}

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
#5 lore raw

Hello,

thanks for reviewing this.

Quoted 5 lines · Uwe Kleine-König

Maybe only add the line for the binding doc in the second patch?

I would have called this struct pwm_gpio_ddata. Given that pwm_gpio is the common prefix of all variables and functions in this driver, pwm_gpio alone is a bit short.

I will change these as suggested.

Quoted 2 lines

Usually you want to check the return value of gpiod_set_value. @Linus +
Bartosz: What do you think, does it need checking for an error here?

I will wait until they reply.

Quoted 14 lines
Quoted 14 lines
drivers/pwm/pwm-gpio.c new file +14 -0
+
+	if (!atomic_read(&pwm_gpio->enabled))
+		return HRTIMER_NORESTART;
+
+	if (pwm_gpio->state) {
+		ns = pwm_gpio->cur.toff_ns;
+		pwm_gpio->state = false;
+	} else {
+		if (spin_trylock(&pwm_gpio->lock)) {
+			pwm_gpio->cur = pwm_gpio->new;
+			spin_unlock(&pwm_gpio->lock);
+		}
+		ns = pwm_gpio->cur.ton_ns;
+		pwm_gpio->state = true;
Quoted 8 lines

drivers/pwm/pwm-gpio.c line 67

Quoted 5 lines
drivers/pwm/pwm-gpio.c new file +5 -0
+	}
+	pwm_gpio->output = pwm_gpio->state ^ pwm_gpio->cur.invert;
+
+	schedule_work(&pwm_gpio->work);
+	hrtimer_forward(handle, hrtimer_get_expires(handle), ns_to_ktime(ns));

This is hard to understand. I think we need more comments explaining (e.g.) the semantic of the members of struct pwm_gpio.

Would it be OK if I added the following comment in the code?

pwm_gpio_apply writes the new settings to pwm_gpio->new, synchronized by the spinlock. At the beginning of every PWM cycle pwm_gpio->new is copied into pwm_gpio->cur, but only if the spinlock can be locked immediately (meaning the settings aren't being applied concurrently to the beginning of the period). By not waiting for the spinlock, no extra jitter in the PWM period is introduced.

Quoted 2 lines

Does it make sense to use the workqueue only if the GPIO is a sleeping one and for fast GPIOs call gpiod_set_value directly?

I thought about this but didn't implement it as it seemed overkill to me. The workqueue is needed anyway to cope with sleeping GPIOs, and it can deal with fast GPIOs with insignificant degradation compared to a direct implementation.

Quoted 6 lines

drivers/pwm/pwm-gpio.c line 119

Quoted 2 lines
drivers/pwm/pwm-gpio.c new file +2 -0
+static const struct pwm_ops pwm_gpio_ops = {
+	.free = pwm_gpio_free,

A free without request seems wrong. The callback stops the PWM, this is wrong, the PWM should continue to toggle.

Would it be OK to remove the pwm_gpio_free callback altogether and move the cancel_work_sync() call to pwm_gpio_remove?

Quoted 1 line

Error message please if pwmchip_add fails

I will add this.

Quoted 15 lines

drivers/pwm/pwm-gpio.c line 160

Quoted 10 lines
drivers/pwm/pwm-gpio.c new file +10 -0
+}
+
+static int pwm_gpio_remove(struct platform_device *pdev)
+{
+	int ret;
+	struct pwm_gpio *pwm_gpio = platform_get_drvdata(pdev);
+
+	ret = pwmchip_remove(&pwm_gpio->chip);
+	if (ret)
+		return ret;

This exit path is bad. The return value of the remove callback is ignored and after pwm_gpio_remove() returns the gpio and *pwm_gpio are freed.

Quoted 5 lines

drivers/pwm/pwm-gpio.c line 165

Quoted 5 lines
drivers/pwm/pwm-gpio.c new file +5 -0
+
+	hrtimer_cancel(&pwm_gpio->timer);
+
+	return 0;
+}

Would it be ok to cancel the timer first and then "return pwmchip_remove(...)"?

Best regards,
Nicola

#6 lore raw

Hello Nicola,

Quoted 23 lines · Nicola Di Lieto
Quoted 13 lines · Uwe Kleine-König
Quoted 13 lines
drivers/pwm/pwm-gpio.c new file +13 -0
+	if (!atomic_read(&pwm_gpio->enabled))
+		return HRTIMER_NORESTART;
+
+	if (pwm_gpio->state) {
+		ns = pwm_gpio->cur.toff_ns;
+		pwm_gpio->state = false;
+	} else {
+		if (spin_trylock(&pwm_gpio->lock)) {
+			pwm_gpio->cur = pwm_gpio->new;
+			spin_unlock(&pwm_gpio->lock);
+		}
+		ns = pwm_gpio->cur.ton_ns;
+		pwm_gpio->state = true;
Quoted 8 lines

drivers/pwm/pwm-gpio.c line 67

Quoted 5 lines
drivers/pwm/pwm-gpio.c new file +5 -0
+	}
+	pwm_gpio->output = pwm_gpio->state ^ pwm_gpio->cur.invert;
+
+	schedule_work(&pwm_gpio->work);
+	hrtimer_forward(handle, hrtimer_get_expires(handle), ns_to_ktime(ns));

This is hard to understand. I think we need more comments explaining (e.g.) the semantic of the members of struct pwm_gpio.

Quoted 8 lines

Would it be OK if I added the following comment in the code?

pwm_gpio_apply writes the new settings to pwm_gpio->new, synchronized by the spinlock. At the beginning of every PWM cycle pwm_gpio->new is copied into pwm_gpio->cur, but only if the spinlock can be locked immediately (meaning the settings aren't being applied concurrently to the beginning of the period). By not waiting for the spinlock, no extra jitter in the PWM period is introduced.

So far I understood also only comment. What wasn't obvious immediately is the state member.

Quoted 7 lines
Quoted 2 lines

Does it make sense to use the workqueue only if the GPIO is a sleeping one and for fast GPIOs call gpiod_set_value directly?

I thought about this but didn't implement it as it seemed overkill to me. The workqueue is needed anyway to cope with sleeping GPIOs, and it can deal with fast GPIOs with insignificant degradation compared to a direct implementation.

OK. If later the need arises this can be added then.

Quoted 9 lines
Quoted 6 lines

drivers/pwm/pwm-gpio.c line 119

Quoted 2 lines
drivers/pwm/pwm-gpio.c new file +2 -0
+static const struct pwm_ops pwm_gpio_ops = {
+	.free = pwm_gpio_free,

A free without request seems wrong. The callback stops the PWM, this is wrong, the PWM should continue to toggle.

Would it be OK to remove the pwm_gpio_free callback altogether and move the cancel_work_sync() call to pwm_gpio_remove?

Yes, that sounds right.

Quoted 2 lines

Would it be ok to cancel the timer first and then "return pwmchip_remove(...)"?

No. The PWM must stay functional until pwmchip_remove() returns.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
#7 lore raw

Hello Uwe,

Quoted 4 lines · Uwe Kleine-König
Quoted 1 line
Quoted 1 line

drivers/pwm/pwm-gpio.c line 64

Quoted 1 line
drivers/pwm/pwm-gpio.c new file +1 -0
+	pwm_gpio->output = pwm_gpio->state ^ pwm_gpio->cur.invert;

So far I understood also only comment. What wasn't obvious immediately is the state member.

Would it become clear enough by adding: "state is the logical PWM output; the actual PIN output level is inverted by XORing with cur.invert when the latter is true" ?

Quoted 5 lines
Quoted 2 lines

Would it be ok to cancel the timer first and then "return pwmchip_remove(...)"?

No. The PWM must stay functional until pwmchip_remove() returns.

Could you please clarify what I should do when pwmchip_remove returns non-zero? In my original implementation

- if pwmchip_remove returns a non-zero error code, I return it to the caller and do not cancel the timer.
- if pwmchip_remove returns zero, I cancel the timer and return zero to the caller

So under no circumstance I return a different code from what pwmchip_remove does...

Best regards,
Nicola

#8 lore raw

Hello Nicola,

Quoted 9 lines · Nicola Di Lieto
Quoted 4 lines · Uwe Kleine-König
Quoted 1 line
Quoted 1 line

drivers/pwm/pwm-gpio.c line 64

Quoted 1 line
drivers/pwm/pwm-gpio.c new file +1 -0
+	pwm_gpio->output = pwm_gpio->state ^ pwm_gpio->cur.invert;

So far I understood also only comment. What wasn't obvious immediately is the state member.

Would it become clear enough by adding: "state is the logical PWM output; the actual PIN output level is inverted by XORing with cur.invert when the latter is true" ?

This was at least good enough for me to understand it now.

So iff state is true, the PWM is in the active phase of the current period. Maybe "currently_active" is a better name for this variable?

Then the code could (with some comments added and a few more variables renamed) could look as follows:

	if (ddata->currently_active) {
		/* Enter the inactive part of the current period. */
		ddata->currently_active = false;
		next_transistion = ddata->cur.toff_ns;
	} else {
		/*
		 * Start a new period. First check if there is a new
		 * configuration setting pending in ddata->new.
		 */
		ddata->currently_active = true;

		if (spin_trylock(&ddata->lock)) {
			ddata->cur = ddata->new;
			spin_unlock(&ddata->lock);
		}
		next_transition = ddata->cur.ton_ns;
	}
	...

which IMHO is easier to understand.

I think there are still two problems with this approach:

- The locking is hard to follow, .enabled is accessed using atomic accessors, .new is protected by the spinlock and the other members are not accessed concurrently, right?
If pwm_apply(..., {.enabled = false}) and pwm_apply(.., {.enabled = true}) are called in quick sequence (e.g. faster than the first call triggers the work queue) there is trouble ahead, isn't there?

- If .duty_cycle is equal to 0 (or .period) the output should be constant. I think this isn't what will happen.

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

Would it be ok to cancel the timer first and then "return pwmchip_remove(...)"?

No. The PWM must stay functional until pwmchip_remove() returns.

Quoted 6 lines

Could you please clarify what I should do when pwmchip_remove returns non-zero? In my original implementation
- if pwmchip_remove returns a non-zero error code, I return it to the caller and do not cancel the timer.
- if pwmchip_remove returns zero, I cancel the timer and return zero to the caller

IMHO it's a bug that pwmchip_remove() can return an error code. I think the best you can do currently is:

	ret = pwmchip_remove(...)
	WARN_ON(ret);

	hrtimer_cancel(..);

	return 0;

because whatever you do is wrong. To sort this out needs some thought and work in the framework and so is unrelated to this patch.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
#9 lore raw
In reply to Uwe Kleine-König

Hi Nicola, Uwe,

thanks for looping me in on this very interesting driver!

This can be really helpful, I already see that it would be possible to replace the hopeless idiomatic driver for the NSLU2 ixp4xx beeper speaker with this driver.

Quoted 8 lines · Uwe Kleine-König

drivers/pwm/pwm-gpio.c line 42

Quoted 5 lines
drivers/pwm/pwm-gpio.c new file +5 -0
+static void pwm_gpio_work(struct work_struct *work)
+{
+     struct pwm_gpio *pwm_gpio = container_of(work, struct pwm_gpio, work);
+
+     gpiod_set_value_cansleep(pwm_gpio->desc, pwm_gpio->output);

Usually you want to check the return value of gpiod_set_value. @Linus +
Bartosz: What do you think, does it need checking for an error here?

We have traditionally been quite relaxed on that. But since it is the cansleep variant, meaning this GPIO could be on a GPIO expander on I2C or SPI, it would be more important.

However: in this specific case for PWM I wonder if we should stick with gpio_set_value() without _cansleep, and then we can definitely skip the error check.

That means GPIO PWM can happen on on-chip GPIOs that are just a register write. Certainly no need to check the return value of these. Also we know it will satisfy timing.

If we allow GPIO PWM on slow buses and expanders that can sleep I do not think the PWM will be very good or reliable.

For example on an I2C expander, with the bus speed of max 400 kHz, what kind of PWM can we create on that? Certainly now above 200 kHz (Nyquist frequency) and probably less. And we have to way to actually check the frequency of the underlying bus, so I am a bit skeptic here.

Would gpio_set_value() work for now or do we need to support expanders and _cansleep?

Yours,
Linus Walleij

#10 lore raw

Il giorno ven 22 gen 2021 alle ore 11:20 Linus Walleij <[email protected]> ha scritto:

Quoted 43 lines

Hi Nicola, Uwe,

thanks for looping me in on this very interesting driver!

This can be really helpful, I already see that it would be possible to replace the hopeless idiomatic driver for the NSLU2 ixp4xx beeper speaker with this driver.

Quoted 8 lines · Uwe Kleine-König

drivers/pwm/pwm-gpio.c line 42

Quoted 5 lines
drivers/pwm/pwm-gpio.c new file +5 -0
+static void pwm_gpio_work(struct work_struct *work)
+{
+     struct pwm_gpio *pwm_gpio = container_of(work, struct pwm_gpio, work);
+
+     gpiod_set_value_cansleep(pwm_gpio->desc, pwm_gpio->output);

Usually you want to check the return value of gpiod_set_value. @Linus +
Bartosz: What do you think, does it need checking for an error here?

We have traditionally been quite relaxed on that. But since it is the cansleep variant, meaning this GPIO could be on a GPIO expander on I2C or SPI, it would be more important.

However: in this specific case for PWM I wonder if we should stick with gpio_set_value() without _cansleep, and then we can definitely skip the error check.

That means GPIO PWM can happen on on-chip GPIOs that are just a register write. Certainly no need to check the return value of these. Also we know it will satisfy timing.

If we allow GPIO PWM on slow buses and expanders that can sleep I do not think the PWM will be very good or reliable.

For example on an I2C expander, with the bus speed of max 400 kHz, what kind of PWM can we create on that? Certainly now above 200 kHz (Nyquist frequency) and probably less. And we have to way to actually check the frequency of the underlying bus, so I am a bit skeptic here.

Quoted 2 lines

Would gpio_set_value() work for now or do we need to support expanders and _cansleep?

More than a year passed from the last message, could we reopen the discussion? I'd like to have this upstream!

Thanks!

Quoted 3 lines

Yours,
Linus Walleij

Signature · 1 line
Profile: http://it.linkedin.com/in/compagnucciangelo
#11 lore raw

Fri, Feb 10, 2023 at 10:54:49PM +0100, Angelo Compagnucci kirjoitti:

Quoted 2 lines

Il giorno ven 22 gen 2021 alle ore 11:20 Linus Walleij <[email protected]> ha scritto:

...

Quoted 2 lines

More than a year passed from the last message, could we reopen the discussion? I'd like to have this upstream!

Seems not much interest neither from community nor from author. Maybe later people will look into this?

P.S> FWIW, I have reviewed code more than a week ago.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#12 lore raw

Hello Andy

Quoted 4 lines · On Wed, Feb 22, 2023 at 02:51:00PM +0200, [email protected] wrote:

Seems not much interest neither from community nor from author. Maybe later people will look into this?

It's not lack of interest, but rather lack of time. I should be able to have a look at this sometime the week after next.

Nicola

#13 lore raw
Quoted 8 lines · Nicola Di Lieto
Quoted 4 lines · On Wed, Feb 22, 2023 at 02:51:00PM +0200, [email protected] wrote:

Seems not much interest neither from community nor from author. Maybe later people will look into this?

It's not lack of interest, but rather lack of time. I should be able to have a look at this sometime the week after next.

Thanks Nicola, I am also interested in seeing this driver upstream.

Yours,
Linus Walleij

#14 lore raw
In reply to Nicola Di Lieto
Quoted 9 lines · Nicola Di Lieto

Hello Andy

Quoted 4 lines · On Wed, Feb 22, 2023 at 02:51:00PM +0200, [email protected] wrote:

Seems not much interest neither from community nor from author. Maybe later people will look into this?

It's not lack of interest, but rather lack of time. I should be able to have a look at this sometime the week after next.

I'd love to know if you're still likely to look into this.

As part of the shift away from memory-mapped GPIO and sysfs on Raspberry Pi, I have some legacy devices which need PWM on arbitrary pins and no canonical solution. As such- I am interested!

Quoted 2 lines

Nicola

#15 lore raw
Quoted 2 lines · Philip Howard
Quoted 1 line · Nicola Di Lieto

On Wed, Feb 22, 2023 at 02:51:00PM +0200, [email protected] wrote:

...

Quoted 1 line

I'd love to know if you're still likely to look into this.

If you are asking me as well (since To refers to me), Cc for the new version and I might look at it. Currently I am OoO till the end of month and after that I probably will have more tasks to do, so no guarantee for this cycle, but just in case Cc and we will see.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#16 lore raw
Quoted 5 lines · Andy Shevchenko
Quoted 2 lines · Philip Howard
Quoted 1 line · Nicola Di Lieto

On Wed, Feb 22, 2023 at 02:51:00PM +0200, [email protected] wrote:

Quoted 8 lines

...

Quoted 1 line

I'd love to know if you're still likely to look into this.

If you are asking me as well (since To refers to me), Cc for the new version and I might look at it. Currently I am OoO till the end of month and after that I probably will have more tasks to do, so no guarantee for this cycle, but just in case Cc and we will see.

Thanks for your reply!

I'm not sure I know how to "Cc for the new version," it took me about an hour to generate a (hopefully) suitably formatted email response to this patch. I've only just subbed to linux-pwm.

But yes- my reply was cast out into the ether in the hopes that logging my interest might get things moving again, or at least let me know if I should try tackling it myself.

I'm moving over to libgpiod on Raspberry Pi, losing software PWM ( from the library I was using) in the process. I don't want to use or contrive another not-invented-here solution.

Quoted 4 lines

--
With Best Regards,
Andy Shevchenko

--
Philip Howard

#17 lore raw
Quoted 4 lines · Phil Howard
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Philip Howard
Quoted 1 line · Nicola Di Lieto

On Wed, Feb 22, 2023 at 02:51:00PM +0200, [email protected] wrote:

...

Quoted 7 lines
Quoted 6 lines
Quoted 1 line

I'd love to know if you're still likely to look into this.

If you are asking me as well (since To refers to me), Cc for the new version and I might look at it. Currently I am OoO till the end of month and after that I probably will have more tasks to do, so no guarantee for this cycle, but just in case Cc and we will see.

Quoted 5 lines

Thanks for your reply!

I'm not sure I know how to "Cc for the new version," it took me about an hour to generate a (hopefully) suitably formatted email response to this patch. I've only just subbed to linux-pwm.

I use the script [1] that uses a heuristics for the Cc/To fields, additionally you can add --cc "Andy Shevchenko ..." at the end.

Quoted 7 lines

But yes- my reply was cast out into the ether in the hopes that logging my interest might get things moving again, or at least let me know if I should try tackling it myself.

I'm moving over to libgpiod on Raspberry Pi, losing software PWM ( from the library I was using) in the process. I don't want to use or contrive another not-invented-here solution.

Totally supporting this motivation!

[1]: https://github.com/andy-shev/home-bin-tools/blob/master/ge2maintainer.sh

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#18 lore raw
In reply to Philip Howard

Hi,

i was working on this a little bit during the holiday. Personally i prefer Vincent's approach [1], which is easier to read and more consequent by rejecting sleeping GPIOs. So i prepared a WIP branch [2], which was tested on a Raspberry Pi 3 B Plus + a cheap Logic analyzer. So maybe you want to give it a try. I will try to send a proper series soon.

Changes:
- rebased Vincent's last patch series on top of Linux 6.7
- cherry picked some improvements from Nicola's series
- tried to address Uwe's, Linus' and Andy's comments
- tried to avoid glitches during probe

Best regards

[1] -
https://lore.kernel.org/all/[email protected]/T/
[2] - https://github.com/lategoodbye/rpi-zero/commits/v6.7-pwm-gpio/

#19 lore raw
Quoted 11 lines · Stefan Wahren

i was working on this a little bit during the holiday. Personally i prefer Vincent's approach [1], which is easier to read and more consequent by rejecting sleeping GPIOs. So i prepared a WIP branch [2], which was tested on a Raspberry Pi 3 B Plus + a cheap Logic analyzer. So maybe you want to give it a try. I will try to send a proper series soon.

Changes:
- rebased Vincent's last patch series on top of Linux 6.7
- cherry picked some improvements from Nicola's series
- tried to address Uwe's, Linus' and Andy's comments
- tried to avoid glitches during probe

This looks good, I guess you will just squash and send it Co-developed-by?

Thanks for looking into this!

Yours,
Linus Walleij

#20 lore raw
In reply to Nicola Di Lieto

Fri, Dec 11, 2020 at 06:04:31PM +0100, Nicola Di Lieto kirjoitti:

Quoted 6 lines

This new driver allows pulse width modulating any GPIOs using a high resolution timer. It is fully generic and can be useful in a variety of situations. As an example I used it to provide a pwm to the pwm-beeper driver so that my embedded system can produce tones through a piezo buzzer connected to a GPIO which unfortunately is not hardware PWM capable.

...

drivers/pwm/pwm-gpio.c line 16

Quoted 8 lines
drivers/pwm/pwm-gpio.c new file +7 -0
+#include <linux/atomic.h>
+#include <linux/err.h>
+#include <linux/gpio/consumer.h>
+#include <linux/hrtimer.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
 
+#include <linux/of.h>

No need (instead use mod_devicetable.h). See below.

Quoted 4 lines
drivers/pwm/pwm-gpio.c new file +4 -0
+#include <linux/platform_device.h>
+#include <linux/pwm.h>
+#include <linux/spinlock.h>
+#include <linux/workqueue.h>

...

drivers/pwm/pwm-gpio.c line 33

Quoted 13 lines
drivers/pwm/pwm-gpio.c new file +12 -0
+struct pwm_gpio {
+	struct pwm_chip chip;
+	struct gpio_desc *desc;
+	struct work_struct work;
+	struct hrtimer timer;
+	atomic_t enabled;
+	spinlock_t lock;
 
+	struct {
+		u64 ton_ns;
+		u64 toff_ns;
+		bool invert;
+	} cur, new;

Can we instead have two struct pwm_state members?

Quoted 3 lines
drivers/pwm/pwm-gpio.c new file +3 -0
+	bool state;
+	bool output;
+};

...

Quoted 4 lines
drivers/pwm/pwm-gpio.c new file +4 -0
+enum hrtimer_restart pwm_gpio_do_timer(struct hrtimer *handle)
+{
+	struct pwm_gpio *pwm_gpio = container_of(handle, struct pwm_gpio, timer);
+	u64 ns;

drivers/pwm/pwm-gpio.c line 57

Quoted 10 lines
drivers/pwm/pwm-gpio.c new file +9 -0
+
+	if (!atomic_read(&pwm_gpio->enabled))
+		return HRTIMER_NORESTART;
+
+	if (pwm_gpio->state) {
+		ns = pwm_gpio->cur.toff_ns;
+		pwm_gpio->state = false;
+	} else {
 
+		if (spin_trylock(&pwm_gpio->lock)) {

What does this mean? So, if we don't get a lock, it doesn't imply we won't get it locked below...

drivers/pwm/pwm-gpio.c line 60

Quoted 3 lines
drivers/pwm/pwm-gpio.c new file +3 -0
+			pwm_gpio->cur = pwm_gpio->new;
+			spin_unlock(&pwm_gpio->lock);
+		}

...i.e. here...

drivers/pwm/pwm-gpio.c line 61

Quoted 1 line
drivers/pwm/pwm-gpio.c new file +1 -0
+		ns = pwm_gpio->cur.ton_ns;

...or here.

drivers/pwm/pwm-gpio.c line 62

Quoted 1 line
drivers/pwm/pwm-gpio.c new file +1 -0
+		pwm_gpio->state = true;

So the trylock needs a good comment explaining why it's not a problem.

Quoted 2 lines
(quoted diff) +2 -0
+	}
+	pwm_gpio->output = pwm_gpio->state ^ pwm_gpio->cur.invert;

...

Quoted 1 line

+#ifdef CONFIG_OF

Drop this ifdeffery.

drivers/pwm/pwm-gpio.c line 169

Quoted 2 lines
drivers/pwm/pwm-gpio.c new file +2 -0
+static const struct of_device_id pwm_gpio_of_match[] = {
+	{ .compatible = "pwm-gpio", },

Inner comma is not needed.

Quoted 4 lines
drivers/pwm/pwm-gpio.c new file +4 -0
+	{ }
+};
+MODULE_DEVICE_TABLE(of, pwm_gpio_of_match);
+#endif

...

drivers/pwm/pwm-gpio.c line 180

Quoted 1 line
drivers/pwm/pwm-gpio.c new file +1 -0
+		.of_match_table = of_match_ptr(pwm_gpio_of_match),

No of_match_ptr(), please.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#21 lore raw

PATCH v2 2/2 pwm: pwm-gpio: Add DT bindings

In reply to Nicola Di Lieto

Added Documentation/devicetree/bindings/pwm/pwm-gpio.yaml

---
 .../devicetree/bindings/pwm/pwm-gpio.yaml          | 42 ++++++++++++++++++++++
 1 file changed, 42 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pwm/pwm-gpio.yaml
Documentation/devicetree/bindings/pwm/pwm-gpio.yaml new file +42 -0
new file mode 100644
index 000000000000..e681b2b1c229
@@ -0,0 +1,42 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/pwm/pwm-gpio.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Generic software PWM for modulating GPIOs
+
+maintainers:
+  - Nicola Di Lieto <[email protected]>
+
+properties:
+  "#pwm-cells":
+    description: |
+      It should be 3. See pwm.yaml in this directory for a
+      description of the cells format.
+    const: 3
+
+  compatible:
+    const: pwm-gpio
+
+  gpios:
+    description:
+      GPIO to be modulated
+    maxItems: 1
+
+required:
+  - "#pwm-cells"
+  - compatible
+  - gpios
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+
+    pwm0 {
+        #pwm-cells = <3>;
+        compatible = "pwm-gpio";
+        gpios = <&gpio 1 GPIO_ACTIVE_HIGH>;
+    };
Signature · 1 line
2.11.0
#22 lore raw

Re: [PATCH v2 2/2] pwm: pwm-gpio: Add DT bindings

Hello,

Quoted 1 line · Nicola Di Lieto

Added Documentation/devicetree/bindings/pwm/pwm-gpio.yaml

this looks fine to me, but this patch should be sent to the dt mailing list ([email protected]) to catch the attention of the people who have to Ack it.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
#23 lore raw

Re: [PATCH v2 2/2] pwm: pwm-gpio: Add DT bindings

Quoted 12 lines · Uwe Kleine-König

Hello,

Quoted 1 line · Nicola Di Lieto

Added Documentation/devicetree/bindings/pwm/pwm-gpio.yaml

this looks fine to me, but this patch should be sent to the dt mailing list ([email protected]) to catch the attention of the people who have to Ack it.

Best regards
Uwe

As suggested by Uwe, I am copying the patch here. The original post is at https://lore.kernel.org/linux-pwm/[email protected]/

--

Added Documentation/devicetree/bindings/pwm/pwm-gpio.yaml

---
.../devicetree/bindings/pwm/pwm-gpio.yaml | 42 ++++++++++++++++++++++

1 file changed, 42 insertions(+)

create mode 100644 Documentation/devicetree/bindings/pwm/pwm-gpio.yaml

Documentation/devicetree/bindings/pwm/pwm-gpio.yaml new file +42 -0
new file mode 100644
index 000000000000..e681b2b1c229
@@ -0,0 +1,42 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/pwm/pwm-gpio.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Generic software PWM for modulating GPIOs
+
+maintainers:
+  - Nicola Di Lieto <[email protected]>
+
+properties:
+  "#pwm-cells":
+    description: |
+      It should be 3. See pwm.yaml in this directory for a
+      description of the cells format.
+    const: 3
+
+  compatible:
+    const: pwm-gpio
+
+  gpios:
+    description:
+      GPIO to be modulated
+    maxItems: 1
+
+required:
+  - "#pwm-cells"
+  - compatible
+  - gpios
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+
+    pwm0 {
+        #pwm-cells = <3>;
+        compatible = "pwm-gpio";
+        gpios = <&gpio 1 GPIO_ACTIVE_HIGH>;
+    };
Signature · 1 line
2.11.0
maintainer
#24 lore raw

Re: [PATCH v2 2/2] pwm: pwm-gpio: Add DT bindings

Quoted 11 lines · Nicola Di Lieto
Quoted 9 lines · Uwe Kleine-König

Hello,

Quoted 1 line · Nicola Di Lieto

Added Documentation/devicetree/bindings/pwm/pwm-gpio.yaml

this looks fine to me, but this patch should be sent to the dt mailing list ([email protected]) to catch the attention of the people who have to Ack it.

Quoted 6 lines
Quoted 3 lines

Best regards
Uwe

As suggested by Uwe, I am copying the patch here. The original post is at https://lore.kernel.org/linux-pwm/[email protected]/

You need to resend the patch to the list. Then patchwork picks it up and automated tests run.

Of course, I'd rather just have the other one I already reviewed applied.

Rob