pwm: Add relative duty cycle manipulation helpers
authorBoris Brezillon <boris.brezillon@free-electrons.com>
Tue, 14 Jun 2016 09:13:10 +0000 (11:13 +0200)
committerThierry Reding <thierry.reding@gmail.com>
Fri, 8 Jul 2016 15:52:19 +0000 (17:52 +0200)
The PWM framework expects PWM users to configure the duty cycle in nano-
seconds, but many users want to express the duty cycle relatively to the
period value (i.e. duty_cycle = 33% of the period).

Add the pwm_{get,set}_relative_duty_cycle() helpers to ease this kind of
conversion.

Signed-off-by: Boris Brezillon <boris.brezillon@free-electrons.com>
Tested-by: Heiko Stuebner <heiko@sntech.de>
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
include/linux/pwm.h

index a100f6e80738b2d2fdd7e0511dcd9811d9f955e2..fd1092729ed6d8f2bb98db8110bd20734dc18034 100644 (file)
@@ -180,6 +180,61 @@ static inline void pwm_init_state(const struct pwm_device *pwm,
        state->duty_cycle = 0;
 }
 
+/**
+ * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
+ * @state: PWM state to extract the duty cycle from
+ * @scale: target scale of the relative duty cycle
+ *
+ * This functions converts the absolute duty cycle stored in @state (expressed
+ * in nanosecond) into a value relative to the period.
+ *
+ * For example if you want to get the duty_cycle expressed in percent, call:
+ *
+ * pwm_get_state(pwm, &state);
+ * duty = pwm_get_relative_duty_cycle(&state, 100);
+ */
+static inline unsigned int
+pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale)
+{
+       if (!state->period)
+               return 0;
+
+       return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale,
+                                    state->period);
+}
+
+/**
+ * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
+ * @state: PWM state to fill
+ * @duty_cycle: relative duty cycle value
+ * @scale: scale in which @duty_cycle is expressed
+ *
+ * This functions converts a relative into an absolute duty cycle (expressed
+ * in nanoseconds), and puts the result in state->duty_cycle.
+ *
+ * For example if you want to configure a 50% duty cycle, call:
+ *
+ * pwm_init_state(pwm, &state);
+ * pwm_set_relative_duty_cycle(&state, 50, 100);
+ * pwm_apply_state(pwm, &state);
+ *
+ * This functions returns -EINVAL if @duty_cycle and/or @scale are
+ * inconsistent (@scale == 0 or @duty_cycle > @scale).
+ */
+static inline int
+pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
+                           unsigned int scale)
+{
+       if (!scale || duty_cycle > scale)
+               return -EINVAL;
+
+       state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle *
+                                                 state->period,
+                                                 scale);
+
+       return 0;
+}
+
 /**
  * struct pwm_ops - PWM controller operations
  * @request: optional hook for requesting a PWM
This page took 0.033398 seconds and 5 git commands to generate.