Skip to content

Commit e866834

Browse files
ukleinekUwe Kleine-König
authored andcommitted
pwm: Let pwm_set_waveform_might_sleep() fail for exact but impossible requests
Up to now pwm_set_waveform_might_sleep() returned 1 for exact requests that couldn't be served exactly. In contrast to pwm_round_waveform_might_sleep() and pwm_set_waveform_might_sleep() with exact = false this is an error condition. So simplify handling for callers of pwm_set_waveform_might_sleep() by returning -EDOM instead of 1 in this case. Signed-off-by: Uwe Kleine-König <[email protected]> Link: https://lore.kernel.org/r/20538a46719584dafd8a1395c886780a97dcdf79.1746010245.git.u.kleine-koenig@baylibre.com Signed-off-by: Uwe Kleine-König <[email protected]>
1 parent 2006016 commit e866834

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

drivers/pwm/core.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,15 +404,16 @@ static int __pwm_set_waveform(struct pwm_device *pwm,
404404
* Typically a requested waveform cannot be implemented exactly, e.g. because
405405
* you requested .period_length_ns = 100 ns, but the hardware can only set
406406
* periods that are a multiple of 8.5 ns. With that hardware passing @exact =
407-
* true results in pwm_set_waveform_might_sleep() failing and returning 1. If
408-
* @exact = false you get a period of 93.5 ns (i.e. the biggest period not bigger
409-
* than the requested value).
407+
* true results in pwm_set_waveform_might_sleep() failing and returning -EDOM.
408+
* If @exact = false you get a period of 93.5 ns (i.e. the biggest period not
409+
* bigger than the requested value).
410410
* Note that even with @exact = true, some rounding by less than 1 ns is
411411
* possible/needed. In the above example requesting .period_length_ns = 94 and
412412
* @exact = true, you get the hardware configured with period = 93.5 ns.
413413
*
414-
* Returns: 0 on success, 1 if was rounded up (if !@exact) or no perfect match was
415-
* possible (if @exact), or a negative errno
414+
* Returns: 0 on success, 1 if was rounded up (if !@exact), -EDOM if setting
415+
* failed due to the exact waveform not being possible (if @exact), or a
416+
* different negative errno on failure.
416417
* Context: May sleep.
417418
*/
418419
int pwm_set_waveform_might_sleep(struct pwm_device *pwm,
@@ -440,6 +441,16 @@ int pwm_set_waveform_might_sleep(struct pwm_device *pwm,
440441
err = __pwm_set_waveform(pwm, wf, exact);
441442
}
442443

444+
/*
445+
* map err == 1 to -EDOM for exact requests. Also make sure that -EDOM is
446+
* only returned in exactly that case. Note that __pwm_set_waveform()
447+
* should never return -EDOM which justifies the unlikely().
448+
*/
449+
if (unlikely(err == -EDOM))
450+
err = -EINVAL;
451+
else if (exact && err == 1)
452+
err = -EDOM;
453+
443454
return err;
444455
}
445456
EXPORT_SYMBOL_GPL(pwm_set_waveform_might_sleep);

0 commit comments

Comments
 (0)