[RFC PATCH 3/3] pwm: add pwm_config_full to pwm.h

From: Trevor Gamblin
Date: Thu Apr 04 2024 - 20:30:53 EST


Add a function that performs the old pwm_config operations while also
handling duty_offset. Change pwm_config to use pwm_config_full with the
duty_offset_ns argument set to 0.

Signed-off-by: Trevor Gamblin <tgamblin@xxxxxxxxxxxx>
---
include/linux/pwm.h | 35 ++++++++++++++++++++++++++++++-----
1 file changed, 30 insertions(+), 5 deletions(-)

diff --git a/include/linux/pwm.h b/include/linux/pwm.h
index e0e5960f91ba..eb018f11a48f 100644
--- a/include/linux/pwm.h
+++ b/include/linux/pwm.h
@@ -347,33 +347,51 @@ int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state);
int pwm_adjust_config(struct pwm_device *pwm);

/**
- * pwm_config() - change a PWM device configuration
+ * pwm_config_full() - change a PWM device configuration, including duty
+ * offset
* @pwm: PWM device
* @duty_ns: "on" time (in nanoseconds)
+ * @duty_offset_ns: offset (in nanoseconds) of "on" pulse
* @period_ns: duration (in nanoseconds) of one cycle
*
* Returns: 0 on success or a negative error code on failure.
*/
-static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
- int period_ns)
+static inline int pwm_config_full(struct pwm_device *pwm, int duty_ns,
+ int duty_offset_ns, int period_ns)
{
struct pwm_state state;

if (!pwm)
return -EINVAL;

- if (duty_ns < 0 || period_ns < 0)
+ if (duty_ns < 0 || period_ns < 0 || duty_offset_ns < 0)
return -EINVAL;

pwm_get_state(pwm, &state);
- if (state.duty_cycle == duty_ns && state.period == period_ns)
+ if (state.duty_cycle == duty_ns && state.period == period_ns &&
+ state.duty_offset == duty_offset_ns)
return 0;

state.duty_cycle = duty_ns;
+ state.duty_offset = duty_offset_ns;
state.period = period_ns;
return pwm_apply_might_sleep(pwm, &state);
}

+/**
+ * pwm_config() - change a PWM device configuration
+ * @pwm: PWM device
+ * @duty_ns: "on" time (in nanoseconds)
+ * @period_ns: duration (in nanoseconds) of one cycle
+ *
+ * Returns: 0 on success or a negative error code on failure.
+ */
+static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
+ int period_ns)
+{
+ return pwm_config_full(pwm, duty_ns, 0, period_ns);
+}
+
/**
* pwm_enable() - start a PWM output toggling
* @pwm: PWM device
@@ -480,6 +498,13 @@ static inline int pwm_adjust_config(struct pwm_device *pwm)
return -EOPNOTSUPP;
}

+static inline int pwm_config_full(struct pwm_device *pwm, int duty_ns,
+ int duty_offset_ns, int period_ns)
+{
+ might_sleep();
+ return -EINVAL;
+}
+
static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
int period_ns)
{
--
2.44.0