Skip to content

Commit 7170d3b

Browse files
seanyoungthierryreding
authored andcommitted
pwm: Make it possible to apply PWM changes in atomic context
Some PWM devices require sleeping, for example if the pwm device is connected over I2C. However, many PWM devices could be used from atomic context, e.g. memory mapped PWM. This is useful for, for example, the pwm-ir-tx driver which requires precise timing. Sleeping causes havoc with the generated IR signal. Since not all PWM devices can support atomic context, we also add a pwm_might_sleep() function to check if is not supported. Signed-off-by: Sean Young <[email protected]> Reviewed-by: Uwe Kleine-König <[email protected]> Signed-off-by: Thierry Reding <[email protected]>
1 parent 752193d commit 7170d3b

File tree

4 files changed

+86
-12
lines changed

4 files changed

+86
-12
lines changed

Documentation/driver-api/pwm.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ After being requested, a PWM has to be configured using::
4646
This API controls both the PWM period/duty_cycle config and the
4747
enable/disable state.
4848

49+
PWM devices can be used from atomic context, if the PWM does not sleep. You
50+
can check if this the case with::
51+
52+
bool pwm_might_sleep(struct pwm_device *pwm);
53+
54+
If false, the PWM can also be configured from atomic context with::
55+
56+
int pwm_apply_atomic(struct pwm_device *pwm, struct pwm_state *state);
57+
4958
As a consumer, don't rely on the output's state for a disabled PWM. If it's
5059
easily possible, drivers are supposed to emit the inactive state, but some
5160
drivers cannot. If you rely on getting the inactive state, use .duty_cycle=0,

MAINTAINERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17576,7 +17576,7 @@ F: drivers/video/backlight/pwm_bl.c
1757617576
F: include/dt-bindings/pwm/
1757717577
F: include/linux/pwm.h
1757817578
F: include/linux/pwm_backlight.h
17579-
K: pwm_(config|apply_might_sleep|ops)
17579+
K: pwm_(config|apply_might_sleep|apply_atomic|ops)
1758017580

1758117581
PXA GPIO DRIVER
1758217582
M: Robert Jarzmik <[email protected]>

drivers/pwm/core.c

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -433,24 +433,15 @@ static void pwm_apply_debug(struct pwm_device *pwm,
433433
}
434434

435435
/**
436-
* pwm_apply_might_sleep() - atomically apply a new state to a PWM device
436+
* __pwm_apply() - atomically apply a new state to a PWM device
437437
* @pwm: PWM device
438438
* @state: new state to apply
439439
*/
440-
int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state)
440+
static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state)
441441
{
442442
struct pwm_chip *chip;
443443
int err;
444444

445-
/*
446-
* Some lowlevel driver's implementations of .apply() make use of
447-
* mutexes, also with some drivers only returning when the new
448-
* configuration is active calling pwm_apply_might_sleep() from atomic context
449-
* is a bad idea. So make it explicit that calling this function might
450-
* sleep.
451-
*/
452-
might_sleep();
453-
454445
if (!pwm || !state || !state->period ||
455446
state->duty_cycle > state->period)
456447
return -EINVAL;
@@ -479,8 +470,57 @@ int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state)
479470

480471
return 0;
481472
}
473+
474+
/**
475+
* pwm_apply_might_sleep() - atomically apply a new state to a PWM device
476+
* Cannot be used in atomic context.
477+
* @pwm: PWM device
478+
* @state: new state to apply
479+
*/
480+
int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state)
481+
{
482+
int err;
483+
484+
/*
485+
* Some lowlevel driver's implementations of .apply() make use of
486+
* mutexes, also with some drivers only returning when the new
487+
* configuration is active calling pwm_apply_might_sleep() from atomic context
488+
* is a bad idea. So make it explicit that calling this function might
489+
* sleep.
490+
*/
491+
might_sleep();
492+
493+
if (IS_ENABLED(CONFIG_PWM_DEBUG) && pwm->chip->atomic) {
494+
/*
495+
* Catch any drivers that have been marked as atomic but
496+
* that will sleep anyway.
497+
*/
498+
non_block_start();
499+
err = __pwm_apply(pwm, state);
500+
non_block_end();
501+
} else {
502+
err = __pwm_apply(pwm, state);
503+
}
504+
505+
return err;
506+
}
482507
EXPORT_SYMBOL_GPL(pwm_apply_might_sleep);
483508

509+
/**
510+
* pwm_apply_atomic() - apply a new state to a PWM device from atomic context
511+
* Not all PWM devices support this function, check with pwm_might_sleep().
512+
* @pwm: PWM device
513+
* @state: new state to apply
514+
*/
515+
int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state)
516+
{
517+
WARN_ONCE(!pwm->chip->atomic,
518+
"sleeping PWM driver used in atomic context\n");
519+
520+
return __pwm_apply(pwm, state);
521+
}
522+
EXPORT_SYMBOL_GPL(pwm_apply_atomic);
523+
484524
/**
485525
* pwm_capture() - capture and report a PWM signal
486526
* @pwm: PWM device

include/linux/pwm.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ struct pwm_ops {
285285
* @npwm: number of PWMs controlled by this chip
286286
* @of_xlate: request a PWM device given a device tree PWM specifier
287287
* @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
288+
* @atomic: can the driver's ->apply() be called in atomic context
288289
* @pwms: array of PWM devices allocated by the framework
289290
*/
290291
struct pwm_chip {
@@ -297,6 +298,7 @@ struct pwm_chip {
297298
struct pwm_device * (*of_xlate)(struct pwm_chip *chip,
298299
const struct of_phandle_args *args);
299300
unsigned int of_pwm_n_cells;
301+
bool atomic;
300302

301303
/* only used internally by the PWM framework */
302304
struct pwm_device *pwms;
@@ -305,6 +307,7 @@ struct pwm_chip {
305307
#if IS_ENABLED(CONFIG_PWM)
306308
/* PWM user APIs */
307309
int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state);
310+
int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state);
308311
int pwm_adjust_config(struct pwm_device *pwm);
309312

310313
/**
@@ -375,6 +378,17 @@ static inline void pwm_disable(struct pwm_device *pwm)
375378
pwm_apply_might_sleep(pwm, &state);
376379
}
377380

381+
/**
382+
* pwm_might_sleep() - is pwm_apply_atomic() supported?
383+
* @pwm: PWM device
384+
*
385+
* Returns: false if pwm_apply_atomic() can be called from atomic context.
386+
*/
387+
static inline bool pwm_might_sleep(struct pwm_device *pwm)
388+
{
389+
return !pwm->chip->atomic;
390+
}
391+
378392
/* PWM provider APIs */
379393
int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
380394
unsigned long timeout);
@@ -403,13 +417,24 @@ struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
403417
struct fwnode_handle *fwnode,
404418
const char *con_id);
405419
#else
420+
static inline bool pwm_might_sleep(struct pwm_device *pwm)
421+
{
422+
return true;
423+
}
424+
406425
static inline int pwm_apply_might_sleep(struct pwm_device *pwm,
407426
const struct pwm_state *state)
408427
{
409428
might_sleep();
410429
return -EOPNOTSUPP;
411430
}
412431

432+
static inline int pwm_apply_atomic(struct pwm_device *pwm,
433+
const struct pwm_state *state)
434+
{
435+
return -EOPNOTSUPP;
436+
}
437+
413438
static inline int pwm_adjust_config(struct pwm_device *pwm)
414439
{
415440
return -EOPNOTSUPP;

0 commit comments

Comments
 (0)