diff options
author | Boris Brezillon <boris.brezillon@free-electrons.com> | 2016-04-14 15:17:41 -0400 |
---|---|---|
committer | Thierry Reding <thierry.reding@gmail.com> | 2016-05-17 08:48:03 -0400 |
commit | 5ec803edcb703fe379836f13560b79dfac79b01d (patch) | |
tree | ee69e2156e03337bd928e151a5c5a39aa44baeb2 /drivers/pwm | |
parent | 15fa8a43c147213a9563903c87b29671035eb6e8 (diff) |
pwm: Add core infrastructure to allow atomic updates
Add an ->apply() method to the pwm_ops struct to allow PWM drivers to
implement atomic updates. This method is preferred over the ->enable(),
->disable() and ->config() methods if available.
Add the pwm_apply_state() function to the PWM user API.
Note that the pwm_apply_state() does not guarantee the atomicity of the
update operation, it all depends on the availability and implementation
of the ->apply() method.
pwm_enable/disable/set_polarity/config() are now implemented as wrappers
around the pwm_apply_state() function.
pwm_adjust_config() is allowing smooth handover between the bootloader
and the kernel. This function tries to adapt the current PWM state to
the PWM arguments coming from a PWM lookup table or a DT definition
without changing the duty_cycle/period proportion.
Signed-off-by: Boris Brezillon <boris.brezillon@free-electrons.com>
[thierry.reding@gmail.com: fix a couple of typos]
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
Diffstat (limited to 'drivers/pwm')
-rw-r--r-- | drivers/pwm/core.c | 187 |
1 files changed, 119 insertions, 68 deletions
diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c index a909c64ee863..729d457861fd 100644 --- a/drivers/pwm/core.c +++ b/drivers/pwm/core.c | |||
@@ -226,6 +226,19 @@ void *pwm_get_chip_data(struct pwm_device *pwm) | |||
226 | } | 226 | } |
227 | EXPORT_SYMBOL_GPL(pwm_get_chip_data); | 227 | EXPORT_SYMBOL_GPL(pwm_get_chip_data); |
228 | 228 | ||
229 | static bool pwm_ops_check(const struct pwm_ops *ops) | ||
230 | { | ||
231 | /* driver supports legacy, non-atomic operation */ | ||
232 | if (ops->config && ops->enable && ops->disable) | ||
233 | return true; | ||
234 | |||
235 | /* driver supports atomic operation */ | ||
236 | if (ops->apply) | ||
237 | return true; | ||
238 | |||
239 | return false; | ||
240 | } | ||
241 | |||
229 | /** | 242 | /** |
230 | * pwmchip_add_with_polarity() - register a new PWM chip | 243 | * pwmchip_add_with_polarity() - register a new PWM chip |
231 | * @chip: the PWM chip to add | 244 | * @chip: the PWM chip to add |
@@ -244,8 +257,10 @@ int pwmchip_add_with_polarity(struct pwm_chip *chip, | |||
244 | unsigned int i; | 257 | unsigned int i; |
245 | int ret; | 258 | int ret; |
246 | 259 | ||
247 | if (!chip || !chip->dev || !chip->ops || !chip->ops->config || | 260 | if (!chip || !chip->dev || !chip->ops || !chip->npwm) |
248 | !chip->ops->enable || !chip->ops->disable || !chip->npwm) | 261 | return -EINVAL; |
262 | |||
263 | if (!pwm_ops_check(chip->ops)) | ||
249 | return -EINVAL; | 264 | return -EINVAL; |
250 | 265 | ||
251 | mutex_lock(&pwm_lock); | 266 | mutex_lock(&pwm_lock); |
@@ -431,102 +446,138 @@ void pwm_free(struct pwm_device *pwm) | |||
431 | EXPORT_SYMBOL_GPL(pwm_free); | 446 | EXPORT_SYMBOL_GPL(pwm_free); |
432 | 447 | ||
433 | /** | 448 | /** |
434 | * pwm_config() - change a PWM device configuration | 449 | * pwm_apply_state() - atomically apply a new state to a PWM device |
435 | * @pwm: PWM device | 450 | * @pwm: PWM device |
436 | * @duty_ns: "on" time (in nanoseconds) | 451 | * @state: new state to apply. This can be adjusted by the PWM driver |
437 | * @period_ns: duration (in nanoseconds) of one cycle | 452 | * if the requested config is not achievable, for example, |
438 | * | 453 | * ->duty_cycle and ->period might be approximated. |
439 | * Returns: 0 on success or a negative error code on failure. | ||
440 | */ | 454 | */ |
441 | int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns) | 455 | int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state) |
442 | { | 456 | { |
443 | int err; | 457 | int err; |
444 | 458 | ||
445 | if (!pwm || duty_ns < 0 || period_ns <= 0 || duty_ns > period_ns) | 459 | if (!pwm) |
446 | return -EINVAL; | 460 | return -EINVAL; |
447 | 461 | ||
448 | err = pwm->chip->ops->config(pwm->chip, pwm, duty_ns, period_ns); | 462 | if (!memcmp(state, &pwm->state, sizeof(*state))) |
449 | if (err) | 463 | return 0; |
450 | return err; | ||
451 | |||
452 | pwm->state.duty_cycle = duty_ns; | ||
453 | pwm->state.period = period_ns; | ||
454 | |||
455 | return 0; | ||
456 | } | ||
457 | EXPORT_SYMBOL_GPL(pwm_config); | ||
458 | 464 | ||
459 | /** | 465 | if (pwm->chip->ops->apply) { |
460 | * pwm_set_polarity() - configure the polarity of a PWM signal | 466 | err = pwm->chip->ops->apply(pwm->chip, pwm, state); |
461 | * @pwm: PWM device | 467 | if (err) |
462 | * @polarity: new polarity of the PWM signal | 468 | return err; |
463 | * | ||
464 | * Note that the polarity cannot be configured while the PWM device is | ||
465 | * enabled. | ||
466 | * | ||
467 | * Returns: 0 on success or a negative error code on failure. | ||
468 | */ | ||
469 | int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity) | ||
470 | { | ||
471 | int err; | ||
472 | 469 | ||
473 | if (!pwm || !pwm->chip->ops) | 470 | pwm->state = *state; |
474 | return -EINVAL; | 471 | } else { |
472 | /* | ||
473 | * FIXME: restore the initial state in case of error. | ||
474 | */ | ||
475 | if (state->polarity != pwm->state.polarity) { | ||
476 | if (!pwm->chip->ops->set_polarity) | ||
477 | return -ENOTSUPP; | ||
478 | |||
479 | /* | ||
480 | * Changing the polarity of a running PWM is | ||
481 | * only allowed when the PWM driver implements | ||
482 | * ->apply(). | ||
483 | */ | ||
484 | if (pwm->state.enabled) { | ||
485 | pwm->chip->ops->disable(pwm->chip, pwm); | ||
486 | pwm->state.enabled = false; | ||
487 | } | ||
488 | |||
489 | err = pwm->chip->ops->set_polarity(pwm->chip, pwm, | ||
490 | state->polarity); | ||
491 | if (err) | ||
492 | return err; | ||
493 | |||
494 | pwm->state.polarity = state->polarity; | ||
495 | } | ||
475 | 496 | ||
476 | if (!pwm->chip->ops->set_polarity) | 497 | if (state->period != pwm->state.period || |
477 | return -ENOSYS; | 498 | state->duty_cycle != pwm->state.duty_cycle) { |
499 | err = pwm->chip->ops->config(pwm->chip, pwm, | ||
500 | state->duty_cycle, | ||
501 | state->period); | ||
502 | if (err) | ||
503 | return err; | ||
478 | 504 | ||
479 | if (pwm_is_enabled(pwm)) | 505 | pwm->state.duty_cycle = state->duty_cycle; |
480 | return -EBUSY; | 506 | pwm->state.period = state->period; |
507 | } | ||
481 | 508 | ||
482 | err = pwm->chip->ops->set_polarity(pwm->chip, pwm, polarity); | 509 | if (state->enabled != pwm->state.enabled) { |
483 | if (err) | 510 | if (state->enabled) { |
484 | return err; | 511 | err = pwm->chip->ops->enable(pwm->chip, pwm); |
512 | if (err) | ||
513 | return err; | ||
514 | } else { | ||
515 | pwm->chip->ops->disable(pwm->chip, pwm); | ||
516 | } | ||
485 | 517 | ||
486 | pwm->state.polarity = polarity; | 518 | pwm->state.enabled = state->enabled; |
519 | } | ||
520 | } | ||
487 | 521 | ||
488 | return 0; | 522 | return 0; |
489 | } | 523 | } |
490 | EXPORT_SYMBOL_GPL(pwm_set_polarity); | 524 | EXPORT_SYMBOL_GPL(pwm_apply_state); |
491 | 525 | ||
492 | /** | 526 | /** |
493 | * pwm_enable() - start a PWM output toggling | 527 | * pwm_adjust_config() - adjust the current PWM config to the PWM arguments |
494 | * @pwm: PWM device | 528 | * @pwm: PWM device |
495 | * | 529 | * |
496 | * Returns: 0 on success or a negative error code on failure. | 530 | * This function will adjust the PWM config to the PWM arguments provided |
531 | * by the DT or PWM lookup table. This is particularly useful to adapt | ||
532 | * the bootloader config to the Linux one. | ||
497 | */ | 533 | */ |
498 | int pwm_enable(struct pwm_device *pwm) | 534 | int pwm_adjust_config(struct pwm_device *pwm) |
499 | { | 535 | { |
500 | int err = 0; | 536 | struct pwm_state state; |
537 | struct pwm_args pargs; | ||
501 | 538 | ||
502 | if (!pwm) | 539 | pwm_get_args(pwm, &pargs); |
503 | return -EINVAL; | 540 | pwm_get_state(pwm, &state); |
541 | |||
542 | /* | ||
543 | * If the current period is zero it means that either the PWM driver | ||
544 | * does not support initial state retrieval or the PWM has not yet | ||
545 | * been configured. | ||
546 | * | ||
547 | * In either case, we setup the new period and polarity, and assign a | ||
548 | * duty cycle of 0. | ||
549 | */ | ||
550 | if (!state.period) { | ||
551 | state.duty_cycle = 0; | ||
552 | state.period = pargs.period; | ||
553 | state.polarity = pargs.polarity; | ||
504 | 554 | ||
505 | if (!pwm_is_enabled(pwm)) { | 555 | return pwm_apply_state(pwm, &state); |
506 | err = pwm->chip->ops->enable(pwm->chip, pwm); | ||
507 | if (!err) | ||
508 | pwm->state.enabled = true; | ||
509 | } | 556 | } |
510 | 557 | ||
511 | return err; | 558 | /* |
512 | } | 559 | * Adjust the PWM duty cycle/period based on the period value provided |
513 | EXPORT_SYMBOL_GPL(pwm_enable); | 560 | * in PWM args. |
561 | */ | ||
562 | if (pargs.period != state.period) { | ||
563 | u64 dutycycle = (u64)state.duty_cycle * pargs.period; | ||
514 | 564 | ||
515 | /** | 565 | do_div(dutycycle, state.period); |
516 | * pwm_disable() - stop a PWM output toggling | 566 | state.duty_cycle = dutycycle; |
517 | * @pwm: PWM device | 567 | state.period = pargs.period; |
518 | */ | 568 | } |
519 | void pwm_disable(struct pwm_device *pwm) | ||
520 | { | ||
521 | if (!pwm) | ||
522 | return; | ||
523 | 569 | ||
524 | if (pwm_is_enabled(pwm)) { | 570 | /* |
525 | pwm->chip->ops->disable(pwm->chip, pwm); | 571 | * If the polarity changed, we should also change the duty cycle. |
526 | pwm->state.enabled = false; | 572 | */ |
573 | if (pargs.polarity != state.polarity) { | ||
574 | state.polarity = pargs.polarity; | ||
575 | state.duty_cycle = state.period - state.duty_cycle; | ||
527 | } | 576 | } |
577 | |||
578 | return pwm_apply_state(pwm, &state); | ||
528 | } | 579 | } |
529 | EXPORT_SYMBOL_GPL(pwm_disable); | 580 | EXPORT_SYMBOL_GPL(pwm_adjust_config); |
530 | 581 | ||
531 | static struct pwm_chip *of_node_to_pwmchip(struct device_node *np) | 582 | static struct pwm_chip *of_node_to_pwmchip(struct device_node *np) |
532 | { | 583 | { |