Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Boris Brezillon and committed by
Thierry Reding
5ec803ed 15fa8a43

+314 -164
+130 -79
drivers/pwm/core.c
··· 226 226 } 227 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 243 * pwmchip_add_with_polarity() - register a new PWM chip 231 244 * @chip: the PWM chip to add ··· 257 244 unsigned int i; 258 245 int ret; 259 246 260 - if (!chip || !chip->dev || !chip->ops || !chip->ops->config || 261 - !chip->ops->enable || !chip->ops->disable || !chip->npwm) 247 + if (!chip || !chip->dev || !chip->ops || !chip->npwm) 248 + return -EINVAL; 249 + 250 + if (!pwm_ops_check(chip->ops)) 262 251 return -EINVAL; 263 252 264 253 mutex_lock(&pwm_lock); ··· 446 431 EXPORT_SYMBOL_GPL(pwm_free); 447 432 448 433 /** 449 - * pwm_config() - change a PWM device configuration 434 + * pwm_apply_state() - atomically apply a new state to a PWM device 450 435 * @pwm: PWM device 451 - * @duty_ns: "on" time (in nanoseconds) 452 - * @period_ns: duration (in nanoseconds) of one cycle 453 - * 454 - * Returns: 0 on success or a negative error code on failure. 436 + * @state: new state to apply. This can be adjusted by the PWM driver 437 + * if the requested config is not achievable, for example, 438 + * ->duty_cycle and ->period might be approximated. 455 439 */ 456 - int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns) 440 + int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state) 457 441 { 458 442 int err; 459 - 460 - if (!pwm || duty_ns < 0 || period_ns <= 0 || duty_ns > period_ns) 461 - return -EINVAL; 462 - 463 - err = pwm->chip->ops->config(pwm->chip, pwm, duty_ns, period_ns); 464 - if (err) 465 - return err; 466 - 467 - pwm->state.duty_cycle = duty_ns; 468 - pwm->state.period = period_ns; 469 - 470 - return 0; 471 - } 472 - EXPORT_SYMBOL_GPL(pwm_config); 473 - 474 - /** 475 - * pwm_set_polarity() - configure the polarity of a PWM signal 476 - * @pwm: PWM device 477 - * @polarity: new polarity of the PWM signal 478 - * 479 - * Note that the polarity cannot be configured while the PWM device is 480 - * enabled. 481 - * 482 - * Returns: 0 on success or a negative error code on failure. 483 - */ 484 - int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity) 485 - { 486 - int err; 487 - 488 - if (!pwm || !pwm->chip->ops) 489 - return -EINVAL; 490 - 491 - if (!pwm->chip->ops->set_polarity) 492 - return -ENOSYS; 493 - 494 - if (pwm_is_enabled(pwm)) 495 - return -EBUSY; 496 - 497 - err = pwm->chip->ops->set_polarity(pwm->chip, pwm, polarity); 498 - if (err) 499 - return err; 500 - 501 - pwm->state.polarity = polarity; 502 - 503 - return 0; 504 - } 505 - EXPORT_SYMBOL_GPL(pwm_set_polarity); 506 - 507 - /** 508 - * pwm_enable() - start a PWM output toggling 509 - * @pwm: PWM device 510 - * 511 - * Returns: 0 on success or a negative error code on failure. 512 - */ 513 - int pwm_enable(struct pwm_device *pwm) 514 - { 515 - int err = 0; 516 443 517 444 if (!pwm) 518 445 return -EINVAL; 519 446 520 - if (!pwm_is_enabled(pwm)) { 521 - err = pwm->chip->ops->enable(pwm->chip, pwm); 522 - if (!err) 523 - pwm->state.enabled = true; 447 + if (!memcmp(state, &pwm->state, sizeof(*state))) 448 + return 0; 449 + 450 + if (pwm->chip->ops->apply) { 451 + err = pwm->chip->ops->apply(pwm->chip, pwm, state); 452 + if (err) 453 + return err; 454 + 455 + pwm->state = *state; 456 + } else { 457 + /* 458 + * FIXME: restore the initial state in case of error. 459 + */ 460 + if (state->polarity != pwm->state.polarity) { 461 + if (!pwm->chip->ops->set_polarity) 462 + return -ENOTSUPP; 463 + 464 + /* 465 + * Changing the polarity of a running PWM is 466 + * only allowed when the PWM driver implements 467 + * ->apply(). 468 + */ 469 + if (pwm->state.enabled) { 470 + pwm->chip->ops->disable(pwm->chip, pwm); 471 + pwm->state.enabled = false; 472 + } 473 + 474 + err = pwm->chip->ops->set_polarity(pwm->chip, pwm, 475 + state->polarity); 476 + if (err) 477 + return err; 478 + 479 + pwm->state.polarity = state->polarity; 480 + } 481 + 482 + if (state->period != pwm->state.period || 483 + state->duty_cycle != pwm->state.duty_cycle) { 484 + err = pwm->chip->ops->config(pwm->chip, pwm, 485 + state->duty_cycle, 486 + state->period); 487 + if (err) 488 + return err; 489 + 490 + pwm->state.duty_cycle = state->duty_cycle; 491 + pwm->state.period = state->period; 492 + } 493 + 494 + if (state->enabled != pwm->state.enabled) { 495 + if (state->enabled) { 496 + err = pwm->chip->ops->enable(pwm->chip, pwm); 497 + if (err) 498 + return err; 499 + } else { 500 + pwm->chip->ops->disable(pwm->chip, pwm); 501 + } 502 + 503 + pwm->state.enabled = state->enabled; 504 + } 524 505 } 525 506 526 - return err; 507 + return 0; 527 508 } 528 - EXPORT_SYMBOL_GPL(pwm_enable); 509 + EXPORT_SYMBOL_GPL(pwm_apply_state); 529 510 530 511 /** 531 - * pwm_disable() - stop a PWM output toggling 512 + * pwm_adjust_config() - adjust the current PWM config to the PWM arguments 532 513 * @pwm: PWM device 514 + * 515 + * This function will adjust the PWM config to the PWM arguments provided 516 + * by the DT or PWM lookup table. This is particularly useful to adapt 517 + * the bootloader config to the Linux one. 533 518 */ 534 - void pwm_disable(struct pwm_device *pwm) 519 + int pwm_adjust_config(struct pwm_device *pwm) 535 520 { 536 - if (!pwm) 537 - return; 521 + struct pwm_state state; 522 + struct pwm_args pargs; 538 523 539 - if (pwm_is_enabled(pwm)) { 540 - pwm->chip->ops->disable(pwm->chip, pwm); 541 - pwm->state.enabled = false; 524 + pwm_get_args(pwm, &pargs); 525 + pwm_get_state(pwm, &state); 526 + 527 + /* 528 + * If the current period is zero it means that either the PWM driver 529 + * does not support initial state retrieval or the PWM has not yet 530 + * been configured. 531 + * 532 + * In either case, we setup the new period and polarity, and assign a 533 + * duty cycle of 0. 534 + */ 535 + if (!state.period) { 536 + state.duty_cycle = 0; 537 + state.period = pargs.period; 538 + state.polarity = pargs.polarity; 539 + 540 + return pwm_apply_state(pwm, &state); 542 541 } 542 + 543 + /* 544 + * Adjust the PWM duty cycle/period based on the period value provided 545 + * in PWM args. 546 + */ 547 + if (pargs.period != state.period) { 548 + u64 dutycycle = (u64)state.duty_cycle * pargs.period; 549 + 550 + do_div(dutycycle, state.period); 551 + state.duty_cycle = dutycycle; 552 + state.period = pargs.period; 553 + } 554 + 555 + /* 556 + * If the polarity changed, we should also change the duty cycle. 557 + */ 558 + if (pargs.polarity != state.polarity) { 559 + state.polarity = pargs.polarity; 560 + state.duty_cycle = state.period - state.duty_cycle; 561 + } 562 + 563 + return pwm_apply_state(pwm, &state); 543 564 } 544 - EXPORT_SYMBOL_GPL(pwm_disable); 565 + EXPORT_SYMBOL_GPL(pwm_adjust_config); 545 566 546 567 static struct pwm_chip *of_node_to_pwmchip(struct device_node *np) 547 568 {
+184 -85
include/linux/pwm.h
··· 5 5 #include <linux/mutex.h> 6 6 #include <linux/of.h> 7 7 8 - struct pwm_device; 9 8 struct seq_file; 10 - 11 - #if IS_ENABLED(CONFIG_PWM) 12 - /* 13 - * pwm_request - request a PWM device 14 - */ 15 - struct pwm_device *pwm_request(int pwm_id, const char *label); 16 - 17 - /* 18 - * pwm_free - free a PWM device 19 - */ 20 - void pwm_free(struct pwm_device *pwm); 21 - 22 - /* 23 - * pwm_config - change a PWM device configuration 24 - */ 25 - int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns); 26 - 27 - /* 28 - * pwm_enable - start a PWM output toggling 29 - */ 30 - int pwm_enable(struct pwm_device *pwm); 31 - 32 - /* 33 - * pwm_disable - stop a PWM output toggling 34 - */ 35 - void pwm_disable(struct pwm_device *pwm); 36 - #else 37 - static inline struct pwm_device *pwm_request(int pwm_id, const char *label) 38 - { 39 - return ERR_PTR(-ENODEV); 40 - } 41 - 42 - static inline void pwm_free(struct pwm_device *pwm) 43 - { 44 - } 45 - 46 - static inline int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns) 47 - { 48 - return -EINVAL; 49 - } 50 - 51 - static inline int pwm_enable(struct pwm_device *pwm) 52 - { 53 - return -EINVAL; 54 - } 55 - 56 - static inline void pwm_disable(struct pwm_device *pwm) 57 - { 58 - } 59 - #endif 60 - 61 9 struct pwm_chip; 62 10 63 11 /** ··· 132 184 return state.duty_cycle; 133 185 } 134 186 135 - /* 136 - * pwm_set_polarity - configure the polarity of a PWM signal 137 - */ 138 - int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity); 139 - 140 187 static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm) 141 188 { 142 189 struct pwm_state state; ··· 147 204 *args = pwm->args; 148 205 } 149 206 150 - static inline void pwm_apply_args(struct pwm_device *pwm) 151 - { 152 - /* 153 - * PWM users calling pwm_apply_args() expect to have a fresh config 154 - * where the polarity and period are set according to pwm_args info. 155 - * The problem is, polarity can only be changed when the PWM is 156 - * disabled. 157 - * 158 - * PWM drivers supporting hardware readout may declare the PWM device 159 - * as enabled, and prevent polarity setting, which changes from the 160 - * existing behavior, where all PWM devices are declared as disabled 161 - * at startup (even if they are actually enabled), thus authorizing 162 - * polarity setting. 163 - * 164 - * Instead of setting ->enabled to false, we call pwm_disable() 165 - * before pwm_set_polarity() to ensure that everything is configured 166 - * as expected, and the PWM is really disabled when the user request 167 - * it. 168 - * 169 - * Note that PWM users requiring a smooth handover between the 170 - * bootloader and the kernel (like critical regulators controlled by 171 - * PWM devices) will have to switch to the atomic API and avoid calling 172 - * pwm_apply_args(). 173 - */ 174 - pwm_disable(pwm); 175 - pwm_set_polarity(pwm, pwm->args.polarity); 176 - } 177 - 178 207 /** 179 208 * struct pwm_ops - PWM controller operations 180 209 * @request: optional hook for requesting a PWM ··· 155 240 * @set_polarity: configure the polarity of this PWM 156 241 * @enable: enable PWM output toggling 157 242 * @disable: disable PWM output toggling 243 + * @apply: atomically apply a new PWM config. The state argument 244 + * should be adjusted with the real hardware config (if the 245 + * approximate the period or duty_cycle value, state should 246 + * reflect it) 158 247 * @get_state: get the current PWM state. This function is only 159 248 * called once per PWM device when the PWM chip is 160 249 * registered. ··· 174 255 enum pwm_polarity polarity); 175 256 int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm); 176 257 void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm); 258 + int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm, 259 + struct pwm_state *state); 177 260 void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm, 178 261 struct pwm_state *state); 179 262 #ifdef CONFIG_DEBUG_FS ··· 213 292 }; 214 293 215 294 #if IS_ENABLED(CONFIG_PWM) 295 + /* PWM user APIs */ 296 + struct pwm_device *pwm_request(int pwm_id, const char *label); 297 + void pwm_free(struct pwm_device *pwm); 298 + int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state); 299 + int pwm_adjust_config(struct pwm_device *pwm); 300 + 301 + /** 302 + * pwm_config() - change a PWM device configuration 303 + * @pwm: PWM device 304 + * @duty_ns: "on" time (in nanoseconds) 305 + * @period_ns: duration (in nanoseconds) of one cycle 306 + * 307 + * Returns: 0 on success or a negative error code on failure. 308 + */ 309 + static inline int pwm_config(struct pwm_device *pwm, int duty_ns, 310 + int period_ns) 311 + { 312 + struct pwm_state state; 313 + 314 + if (!pwm) 315 + return -EINVAL; 316 + 317 + pwm_get_state(pwm, &state); 318 + if (state.duty_cycle == duty_ns && state.period == period_ns) 319 + return 0; 320 + 321 + state.duty_cycle = duty_ns; 322 + state.period = period_ns; 323 + return pwm_apply_state(pwm, &state); 324 + } 325 + 326 + /** 327 + * pwm_set_polarity() - configure the polarity of a PWM signal 328 + * @pwm: PWM device 329 + * @polarity: new polarity of the PWM signal 330 + * 331 + * Note that the polarity cannot be configured while the PWM device is 332 + * enabled. 333 + * 334 + * Returns: 0 on success or a negative error code on failure. 335 + */ 336 + static inline int pwm_set_polarity(struct pwm_device *pwm, 337 + enum pwm_polarity polarity) 338 + { 339 + struct pwm_state state; 340 + 341 + if (!pwm) 342 + return -EINVAL; 343 + 344 + pwm_get_state(pwm, &state); 345 + if (state.polarity == polarity) 346 + return 0; 347 + 348 + /* 349 + * Changing the polarity of a running PWM without adjusting the 350 + * dutycycle/period value is a bit risky (can introduce glitches). 351 + * Return -EBUSY in this case. 352 + * Note that this is allowed when using pwm_apply_state() because 353 + * the user specifies all the parameters. 354 + */ 355 + if (state.enabled) 356 + return -EBUSY; 357 + 358 + state.polarity = polarity; 359 + return pwm_apply_state(pwm, &state); 360 + } 361 + 362 + /** 363 + * pwm_enable() - start a PWM output toggling 364 + * @pwm: PWM device 365 + * 366 + * Returns: 0 on success or a negative error code on failure. 367 + */ 368 + static inline int pwm_enable(struct pwm_device *pwm) 369 + { 370 + struct pwm_state state; 371 + 372 + if (!pwm) 373 + return -EINVAL; 374 + 375 + pwm_get_state(pwm, &state); 376 + if (state.enabled) 377 + return 0; 378 + 379 + state.enabled = true; 380 + return pwm_apply_state(pwm, &state); 381 + } 382 + 383 + /** 384 + * pwm_disable() - stop a PWM output toggling 385 + * @pwm: PWM device 386 + */ 387 + static inline void pwm_disable(struct pwm_device *pwm) 388 + { 389 + struct pwm_state state; 390 + 391 + if (!pwm) 392 + return; 393 + 394 + pwm_get_state(pwm, &state); 395 + if (!state.enabled) 396 + return; 397 + 398 + state.enabled = false; 399 + pwm_apply_state(pwm, &state); 400 + } 401 + 402 + 403 + /* PWM provider APIs */ 216 404 int pwm_set_chip_data(struct pwm_device *pwm, void *data); 217 405 void *pwm_get_chip_data(struct pwm_device *pwm); 218 406 ··· 347 317 348 318 bool pwm_can_sleep(struct pwm_device *pwm); 349 319 #else 320 + static inline struct pwm_device *pwm_request(int pwm_id, const char *label) 321 + { 322 + return ERR_PTR(-ENODEV); 323 + } 324 + 325 + static inline void pwm_free(struct pwm_device *pwm) 326 + { 327 + } 328 + 329 + static inline int pwm_apply_state(struct pwm_device *pwm, 330 + const struct pwm_state *state) 331 + { 332 + return -ENOTSUPP; 333 + } 334 + 335 + static inline int pwm_adjust_config(struct pwm_device *pwm) 336 + { 337 + return -ENOTSUPP; 338 + } 339 + 340 + static inline int pwm_config(struct pwm_device *pwm, int duty_ns, 341 + int period_ns) 342 + { 343 + return -EINVAL; 344 + } 345 + 346 + static inline int pwm_set_polarity(struct pwm_device *pwm, 347 + enum pwm_polarity polarity) 348 + { 349 + return -ENOTSUPP; 350 + } 351 + 352 + static inline int pwm_enable(struct pwm_device *pwm) 353 + { 354 + return -EINVAL; 355 + } 356 + 357 + static inline void pwm_disable(struct pwm_device *pwm) 358 + { 359 + } 360 + 350 361 static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data) 351 362 { 352 363 return -EINVAL; ··· 458 387 return false; 459 388 } 460 389 #endif 390 + 391 + static inline void pwm_apply_args(struct pwm_device *pwm) 392 + { 393 + /* 394 + * PWM users calling pwm_apply_args() expect to have a fresh config 395 + * where the polarity and period are set according to pwm_args info. 396 + * The problem is, polarity can only be changed when the PWM is 397 + * disabled. 398 + * 399 + * PWM drivers supporting hardware readout may declare the PWM device 400 + * as enabled, and prevent polarity setting, which changes from the 401 + * existing behavior, where all PWM devices are declared as disabled 402 + * at startup (even if they are actually enabled), thus authorizing 403 + * polarity setting. 404 + * 405 + * Instead of setting ->enabled to false, we call pwm_disable() 406 + * before pwm_set_polarity() to ensure that everything is configured 407 + * as expected, and the PWM is really disabled when the user request 408 + * it. 409 + * 410 + * Note that PWM users requiring a smooth handover between the 411 + * bootloader and the kernel (like critical regulators controlled by 412 + * PWM devices) will have to switch to the atomic API and avoid calling 413 + * pwm_apply_args(). 414 + */ 415 + pwm_disable(pwm); 416 + pwm_set_polarity(pwm, pwm->args.polarity); 417 + } 461 418 462 419 struct pwm_lookup { 463 420 struct list_head list;