diff mbox series

[libpwm] cdev: Make backend compilable without <linux/pwm.h>

Message ID 20250516191855.1354125-2-u.kleine-koenig@baylibre.com
State Accepted
Headers show
Series [libpwm] cdev: Make backend compilable without <linux/pwm.h> | expand

Commit Message

Uwe Kleine-König May 16, 2025, 7:18 p.m. UTC
That uapi header is quite new and so hardly any system provides it yet.
So add a local copy and fall back to that to make character device
support included unconditional.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
---
 Makefile.am  |  5 +----
 cdev.c       |  7 ++++++-
 configure.ac | 21 ++++++---------------
 core.c       |  4 ----
 uapi-pwm.h   | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 66 insertions(+), 24 deletions(-)
 create mode 100644 uapi-pwm.h


base-commit: 00aff52a76199f13ea9e0ad628ffcac93fdce538

Comments

Uwe Kleine-König May 17, 2025, 3:10 p.m. UTC | #1
On Fri, May 16, 2025 at 09:18:56PM +0200, Uwe Kleine-König wrote:
> That uapi header is quite new and so hardly any system provides it yet.
> So add a local copy and fall back to that to make character device
> support included unconditional.
> 
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>

Also applied to
https://git.kernel.org/pub/scm/linux/kernel/git/ukleinek/libpwm.git main
.

Best regards
Uwe
diff mbox series

Patch

diff --git a/Makefile.am b/Makefile.am
index a2086de0f14b..08e2827e00fa 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -21,12 +21,9 @@  EXTRA_DIST = libpwm.sym
 
 libpwm_la_SOURCES = \
 	core.c \
+	cdev.c \
 	sysfs.c
 
-if HAVE_PWMCDEV
-libpwm_la_SOURCES += cdev.c
-endif
-
 bin_PROGRAMS = \
 	pwmround \
 	pwmset \
diff --git a/cdev.c b/cdev.c
index 924e6f74a0de..7000a8394592 100644
--- a/cdev.c
+++ b/cdev.c
@@ -5,7 +5,6 @@ 
 
 #include <errno.h>
 #include <fcntl.h>
-#include <linux/pwm.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/ioctl.h>
@@ -13,6 +12,12 @@ 
 
 #include <pwm.h>
 
+#ifdef HAVE_USABLE_LINUX_PWM_H
+#include <linux/pwm.h>
+#else
+#include "uapi-pwm.h"
+#endif
+
 #include "pwm-internal.h"
 
 struct pwm_cdev {
diff --git a/configure.ac b/configure.ac
index 63ccb8d819a1..3c6ed36d3447 100644
--- a/configure.ac
+++ b/configure.ac
@@ -28,28 +28,19 @@  AC_PROG_CC
 
 LT_INIT
 
-AC_ARG_ENABLE([pwmcdev],
-	      [AS_HELP_STRING([--enable-pwmcdev], [Build code to use /dev/pwmchipX @<:@default=check@:>@])],
-	      [],
-	      [: m4_divert_text([DEFAULTS], [enable_pwmcdev=check])])
-
-available_pwmcdev=yes
+have_usable_linux_pwm_h=yes
 
 AC_CHECK_HEADER([linux/pwm.h], [
-AC_CHECK_DECL([PWM_IOCTL_SETROUNDEDWF],, [available_pwmcdev=no], [#include <linux/pwm.h>])
-		], [available_pwmcdev=no])
+AC_CHECK_DECL([PWM_IOCTL_SETROUNDEDWF],, [have_usable_linux_pwm_h=no], [#include <linux/pwm.h>])
+		], [have_usable_linux_pwm_h=no])
 
-AS_IF([test "x$enable_pwmcdev" = "xcheck"], [enable_pwmcdev=$available_pwmcdev])
+AC_MSG_CHECKING([if system has a usable <linux/pwm.h>])
 
-AC_MSG_CHECKING([if character device support is to be enabled])
-
-AS_IF(test "x$enable_pwmcdev" = "xyes", [
-	AC_DEFINE([HAVE_PWMCDEV], [1], [Define to 1 if the character device backend should be built])
+AS_IF(test "x$have_usable_linux_pwm_h" = "xyes", [
+	AC_DEFINE([HAVE_USABLE_LINUX_PWM_H], [1], [Define to 1 if the character device backend should be built])
 	AC_MSG_RESULT([yes])
 ], [AC_MSG_RESULT([no])])
 
-AM_CONDITIONAL([HAVE_PWMCDEV], [test x$enable_pwmcdev = xyes])
-
 AC_CONFIG_FILES([
 	Makefile
 	libpwm.pc
diff --git a/core.c b/core.c
index cd1b2c591790..a1591d4efd4d 100644
--- a/core.c
+++ b/core.c
@@ -15,13 +15,9 @@  struct pwm_chip *pwm_chip_open_by_number(unsigned int num)
 {
 	struct pwm_chip *chip;
 
-#if HAVE_PWMCDEV
 	chip = pwm_chip_cdev_open_by_number(num);
 	if (chip == NULL && errno == ENOENT)
 		chip = pwm_chip_sysfs_open_by_number(num);
-#else
-	chip = pwm_chip_sysfs_open_by_number(num);
-#endif
 
 	return chip;
 }
diff --git a/uapi-pwm.h b/uapi-pwm.h
new file mode 100644
index 000000000000..182d59cc07ee
--- /dev/null
+++ b/uapi-pwm.h
@@ -0,0 +1,53 @@ 
+/* SPDX-License-Identifier: GPL-2.0-only WITH Linux-syscall-note */
+
+#ifndef _UAPI_PWM_H_
+#define _UAPI_PWM_H_
+
+#include <linux/ioctl.h>
+#include <linux/types.h>
+
+/**
+ * struct pwmchip_waveform - Describe a PWM waveform for a pwm_chip's PWM channel
+ * @hwpwm: per-chip relative index of the PWM device
+ * @__pad: padding, must be zero
+ * @period_length_ns: duration of the repeating period.
+ *    A value of 0 represents a disabled PWM.
+ * @duty_length_ns: duration of the active part in each period
+ * @duty_offset_ns: offset of the rising edge from a period's start
+ */
+struct pwmchip_waveform {
+	__u32 hwpwm;
+	__u32 __pad;
+	__u64 period_length_ns;
+	__u64 duty_length_ns;
+	__u64 duty_offset_ns;
+};
+
+/* Reserves the passed hwpwm for exclusive control. */
+#define PWM_IOCTL_REQUEST	_IO(0x75, 1)
+
+/* counter part to PWM_IOCTL_REQUEST */
+#define PWM_IOCTL_FREE		_IO(0x75, 2)
+
+/*
+ * Modifies the passed wf according to hardware constraints. All parameters are
+ * rounded down to the next possible value, unless there is no such value, then
+ * values are rounded up. Note that zero isn't considered for rounding down
+ * period_length_ns.
+ */
+#define PWM_IOCTL_ROUNDWF	_IOWR(0x75, 3, struct pwmchip_waveform)
+
+/* Get the currently implemented waveform */
+#define PWM_IOCTL_GETWF		_IOWR(0x75, 4, struct pwmchip_waveform)
+
+/* Like PWM_IOCTL_ROUNDWF + PWM_IOCTL_SETEXACTWF in one go. */
+#define PWM_IOCTL_SETROUNDEDWF	_IOW(0x75, 5, struct pwmchip_waveform)
+
+/*
+ * Program the PWM to emit exactly the passed waveform, subject only to rounding
+ * down each value less than 1 ns. Returns 0 on success, -EDOM if the waveform
+ * cannot be implemented exactly, or other negative error codes.
+ */
+#define PWM_IOCTL_SETEXACTWF	_IOW(0x75, 6, struct pwmchip_waveform)
+
+#endif /* _UAPI_PWM_H_ */