mbox series

[V4,0/2] pwm: Add GPIO PWM driver

Message ID 20240204220851.4783-1-wahrenst@gmx.net
Headers show
Series pwm: Add GPIO PWM driver | expand

Message

Stefan Wahren Feb. 4, 2024, 10:08 p.m. UTC
Add a software PWM which toggles a GPIO from a high-resolution timer.

Recent discussions in the Raspberry Pi community revealt that a lot
of users still use MMIO userspace tools for GPIO access. One argument
for this approach is the lack of a GPIO PWM kernel driver. So this
series tries to fill this gap.

This continues the work of Vincent Whitchurch [1], which is easier
to read and more consequent by rejecting sleeping GPIOs than Nicola's
approach [2].

The work has been tested on a Raspberry Pi 3 B+ and a cheap logic
analyzer.

V4:
 - address DT bindings comments from Conor Dooley
 - drop unnecessary MODULE_ALIAS() as suggested by Krzysztof Kozlowski
 - add range checks to apply which consider the hrtimer resolution
   (idea comes from Sean Young)
 - mark driver as atomic as suggested by Sean Young

V3:
 - rebase on top of v6.8-pwm-next
 - cherry-pick improvements from Nicola's series
 - try to address Uwe's, Linus' and Andy's comments
 - try to avoid GPIO glitches during probe
 - fix pwm_gpio_remove()
 - some code clean up's and comments

V2:
 - Rename gpio to gpios in binding
 - Calculate next expiry from expected current expiry rather than "now"
 - Only change configuration after current period ends
 - Implement get_state()
 - Add error message for probe failures
 - Stop PWM before unregister

[1] - https://lore.kernel.org/all/20200915135445.al75xmjxudj2rgcp@axis.com/T/
[2] - https://lore.kernel.org/all/20201205214353.xapax46tt5snzd2v@einstein.dilieto.eu/

Nicola Di Lieto (1):
  dt-bindings: pwm: Add pwm-gpio

Vincent Whitchurch (1):
  pwm: Add GPIO PWM driver

 .../devicetree/bindings/pwm/pwm-gpio.yaml     |  42 ++++
 drivers/pwm/Kconfig                           |  11 +
 drivers/pwm/Makefile                          |   1 +
 drivers/pwm/pwm-gpio.c                        | 228 ++++++++++++++++++
 4 files changed, 282 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pwm/pwm-gpio.yaml
 create mode 100644 drivers/pwm/pwm-gpio.c

--
2.34.1

Comments

Linus Walleij Feb. 4, 2024, 10:47 p.m. UTC | #1
On Sun, Feb 4, 2024 at 11:09 PM Stefan Wahren <wahrenst@gmx.net> wrote:

> From: Nicola Di Lieto <nicola.dilieto@gmail.com>
>
> Add bindings for PWM modulated by GPIO.
>
> Signed-off-by: Nicola Di Lieto <nicola.dilieto@gmail.com>
> Co-developed-by: Stefan Wahren <wahrenst@gmx.net>
> Signed-off-by: Stefan Wahren <wahrenst@gmx.net>

Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

Yours,
Linus Walleij
Dhruva Gole Feb. 5, 2024, 5:55 a.m. UTC | #2
Hi,

On Feb 04, 2024 at 23:08:49 +0100, Stefan Wahren wrote:
> Add a software PWM which toggles a GPIO from a high-resolution timer.
> 
> Recent discussions in the Raspberry Pi community revealt that a lot
> of users still use MMIO userspace tools for GPIO access. One argument
> for this approach is the lack of a GPIO PWM kernel driver. So this
> series tries to fill this gap.
> 
> This continues the work of Vincent Whitchurch [1], which is easier
> to read and more consequent by rejecting sleeping GPIOs than Nicola's
> approach [2].
> 
> The work has been tested on a Raspberry Pi 3 B+ and a cheap logic
> analyzer.

I recently came across this series and I have to say that it will sure be
a nice to have feature to be able to use any GPIO as a PWM.

However, just a minor suggestion is that we should make sure it's well
documented how to actually use this. It would be much appreciated if you
could include some basic documentation of a few sysfs commands or any
userspace library that you used to test what you've mentioned above.

Maybe add another patch for this page?
https://docs.kernel.org/driver-api/pwm.html#using-pwms-with-the-sysfs-interface

This will ensure people know about this feature and will actually be
able to use it.
Stefan Wahren Feb. 5, 2024, 7:08 a.m. UTC | #3
Hi Dhruva,

Am 05.02.24 um 06:55 schrieb Dhruva Gole:
> Hi,
>
> On Feb 04, 2024 at 23:08:49 +0100, Stefan Wahren wrote:
>> Add a software PWM which toggles a GPIO from a high-resolution timer.
>>
>> Recent discussions in the Raspberry Pi community revealt that a lot
>> of users still use MMIO userspace tools for GPIO access. One argument
>> for this approach is the lack of a GPIO PWM kernel driver. So this
>> series tries to fill this gap.
>>
>> This continues the work of Vincent Whitchurch [1], which is easier
>> to read and more consequent by rejecting sleeping GPIOs than Nicola's
>> approach [2].
>>
>> The work has been tested on a Raspberry Pi 3 B+ and a cheap logic
>> analyzer.
> I recently came across this series and I have to say that it will sure be
> a nice to have feature to be able to use any GPIO as a PWM.
>
> However, just a minor suggestion is that we should make sure it's well
> documented how to actually use this. It would be much appreciated if you
> could include some basic documentation of a few sysfs commands or any
> userspace library that you used to test what you've mentioned above.
i used the PWM sysfs for testing and this PWM driver doesn't change
anything on this well known interface.

Sorry, i don't understand what needs to be documented additionally?
>
> Maybe add another patch for this page?
> https://docs.kernel.org/driver-api/pwm.html#using-pwms-with-the-sysfs-interface
>
> This will ensure people know about this feature and will actually be
> able to use it.
>
Uwe Kleine-König Feb. 5, 2024, 9:15 a.m. UTC | #4
Hello,

On Sun, Feb 04, 2024 at 11:08:50PM +0100, Stefan Wahren wrote:
> +  "#pwm-cells":
> +    const: 3
> +
> +  gpios:
> +    description:
> +      GPIO to be modulated
> +    maxItems: 1

Given that we have 3 PWM cells (so there is an u32 that specifies the
pwm_chip's line number) it would be obvious to allow several GPIOs. But
I guess we can extend this easily if and when the need arises.

Otherwise I'm happy with this patch.

Best regards
Uwe