Skip to content

Commit 6ae851b

Browse files
committed
pwm: rp1: Silently correct illegal values
Remove the need for the user to know the limitations of this PWM implementation by adjusting configuration requests to be the closest acceptable value. Add a get_state method so that the actual values can be queried. Signed-off-by: Phil Elwell <phil@raspberrypi.com>
1 parent 4be1159 commit 6ae851b

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

drivers/pwm/pwm-pio-rp1.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,12 @@ static int pwm_pio_rp1_apply(struct pwm_chip *chip, struct pwm_device *pwm,
101101
uint32_t new_duty_cycle;
102102
uint32_t new_period;
103103

104-
if (state->duty_cycle && state->duty_cycle < pwm_pio_resolution)
105-
return -EINVAL;
106-
107-
if (state->duty_cycle != state->period &&
108-
(state->period - state->duty_cycle < pwm_pio_resolution))
109-
return -EINVAL;
110-
111-
new_period = state->period / pwm_pio_resolution;
112-
new_duty_cycle = state->duty_cycle / pwm_pio_resolution;
104+
new_period = DIV_ROUND_CLOSEST(state->period, pwm_pio_resolution);
105+
if (new_period < 2)
106+
new_period = 2;
107+
new_duty_cycle = DIV_ROUND_CLOSEST(state->duty_cycle, pwm_pio_resolution);
108+
if (new_duty_cycle > new_period - 1)
109+
new_duty_cycle = new_period - 1;
113110

114111
mutex_lock(&ppwm->mutex);
115112

@@ -145,8 +142,22 @@ static int pwm_pio_rp1_apply(struct pwm_chip *chip, struct pwm_device *pwm,
145142
return 0;
146143
}
147144

145+
static int pwm_pio_rp1_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
146+
struct pwm_state *state)
147+
{
148+
struct pwm_pio_rp1 *ppwm = pwmchip_get_drvdata(chip);
149+
150+
state->enabled = ppwm->enabled;
151+
state->polarity = ppwm->polarity;
152+
state->period = ppwm->period * pwm_pio_resolution;
153+
state->duty_cycle = ppwm->duty_cycle * pwm_pio_resolution;
154+
155+
return 0;
156+
}
157+
148158
static const struct pwm_ops pwm_pio_rp1_ops = {
149159
.apply = pwm_pio_rp1_apply,
160+
.get_state = pwm_pio_rp1_get_state,
150161
};
151162

152163
static int pwm_pio_rp1_probe(struct platform_device *pdev)

0 commit comments

Comments
 (0)