1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __LINUX_PWM_H 3 #define __LINUX_PWM_H 4 5 #include <linux/device.h> 6 #include <linux/err.h> 7 #include <linux/module.h> 8 #include <linux/mutex.h> 9 #include <linux/of.h> 10 11 MODULE_IMPORT_NS("PWM"); 12 13 struct pwm_chip; 14 15 /** 16 * enum pwm_polarity - polarity of a PWM signal 17 * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty- 18 * cycle, followed by a low signal for the remainder of the pulse 19 * period 20 * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty- 21 * cycle, followed by a high signal for the remainder of the pulse 22 * period 23 */ 24 enum pwm_polarity { 25 PWM_POLARITY_NORMAL, 26 PWM_POLARITY_INVERSED, 27 }; 28 29 /** 30 * struct pwm_args - board-dependent PWM arguments 31 * @period: reference period 32 * @polarity: reference polarity 33 * 34 * This structure describes board-dependent arguments attached to a PWM 35 * device. These arguments are usually retrieved from the PWM lookup table or 36 * device tree. 37 * 38 * Do not confuse this with the PWM state: PWM arguments represent the initial 39 * configuration that users want to use on this PWM device rather than the 40 * current PWM hardware state. 41 */ 42 struct pwm_args { 43 u64 period; 44 enum pwm_polarity polarity; 45 }; 46 47 enum { 48 PWMF_REQUESTED = 0, 49 PWMF_EXPORTED = 1, 50 }; 51 52 /** 53 * struct pwm_waveform - description of a PWM waveform 54 * @period_length_ns: PWM period 55 * @duty_length_ns: PWM duty cycle 56 * @duty_offset_ns: offset of the rising edge from the period's start 57 * 58 * This is a representation of a PWM waveform alternative to struct pwm_state 59 * below. It's more expressive than struct pwm_state as it contains a 60 * duty_offset_ns and so can represent offsets other than zero (with .polarity = 61 * PWM_POLARITY_NORMAL) and period - duty_cycle (.polarity = 62 * PWM_POLARITY_INVERSED). 63 * 64 * Note there is no explicit bool for enabled. A "disabled" PWM is represented 65 * by .period_length_ns = 0. Note further that the behaviour of a "disabled" PWM 66 * is undefined. Depending on the hardware's capabilities it might drive the 67 * active or inactive level, go high-z or even continue to toggle. 68 * 69 * The unit for all three members is nanoseconds. 70 */ 71 struct pwm_waveform { 72 u64 period_length_ns; 73 u64 duty_length_ns; 74 u64 duty_offset_ns; 75 }; 76 77 /* 78 * struct pwm_state - state of a PWM channel 79 * @period: PWM period (in nanoseconds) 80 * @duty_cycle: PWM duty cycle (in nanoseconds) 81 * @polarity: PWM polarity 82 * @enabled: PWM enabled status 83 * @usage_power: If set, the PWM driver is only required to maintain the power 84 * output but has more freedom regarding signal form. 85 * If supported, the signal can be optimized, for example to 86 * improve EMI by phase shifting individual channels. 87 */ 88 struct pwm_state { 89 u64 period; 90 u64 duty_cycle; 91 enum pwm_polarity polarity; 92 bool enabled; 93 bool usage_power; 94 }; 95 96 /** 97 * struct pwm_device - PWM channel object 98 * @label: name of the PWM device 99 * @flags: flags associated with the PWM device 100 * @hwpwm: per-chip relative index of the PWM device 101 * @chip: PWM chip providing this PWM device 102 * @args: PWM arguments 103 * @state: last applied state 104 * @last: last implemented state (for PWM_DEBUG) 105 */ 106 struct pwm_device { 107 const char *label; 108 unsigned long flags; 109 unsigned int hwpwm; 110 struct pwm_chip *chip; 111 112 struct pwm_args args; 113 struct pwm_state state; 114 struct pwm_state last; 115 }; 116 117 /** 118 * pwm_get_state() - retrieve the current PWM state 119 * @pwm: PWM device 120 * @state: state to fill with the current PWM state 121 * 122 * The returned PWM state represents the state that was applied by a previous call to 123 * pwm_apply_might_sleep(). Drivers may have to slightly tweak that state before programming it to 124 * hardware. If pwm_apply_might_sleep() was never called, this returns either the current hardware 125 * state (if supported) or the default settings. 126 */ 127 static inline void pwm_get_state(const struct pwm_device *pwm, 128 struct pwm_state *state) 129 { 130 *state = pwm->state; 131 } 132 133 static inline bool pwm_is_enabled(const struct pwm_device *pwm) 134 { 135 struct pwm_state state; 136 137 pwm_get_state(pwm, &state); 138 139 return state.enabled; 140 } 141 142 static inline u64 pwm_get_period(const struct pwm_device *pwm) 143 { 144 struct pwm_state state; 145 146 pwm_get_state(pwm, &state); 147 148 return state.period; 149 } 150 151 static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm) 152 { 153 struct pwm_state state; 154 155 pwm_get_state(pwm, &state); 156 157 return state.duty_cycle; 158 } 159 160 static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm) 161 { 162 struct pwm_state state; 163 164 pwm_get_state(pwm, &state); 165 166 return state.polarity; 167 } 168 169 static inline void pwm_get_args(const struct pwm_device *pwm, 170 struct pwm_args *args) 171 { 172 *args = pwm->args; 173 } 174 175 /** 176 * pwm_init_state() - prepare a new state to be applied with pwm_apply_might_sleep() 177 * @pwm: PWM device 178 * @state: state to fill with the prepared PWM state 179 * 180 * This functions prepares a state that can later be tweaked and applied 181 * to the PWM device with pwm_apply_might_sleep(). This is a convenient function 182 * that first retrieves the current PWM state and the replaces the period 183 * and polarity fields with the reference values defined in pwm->args. 184 * Once the function returns, you can adjust the ->enabled and ->duty_cycle 185 * fields according to your needs before calling pwm_apply_might_sleep(). 186 * 187 * ->duty_cycle is initially set to zero to avoid cases where the current 188 * ->duty_cycle value exceed the pwm_args->period one, which would trigger 189 * an error if the user calls pwm_apply_might_sleep() without adjusting ->duty_cycle 190 * first. 191 */ 192 static inline void pwm_init_state(const struct pwm_device *pwm, 193 struct pwm_state *state) 194 { 195 struct pwm_args args; 196 197 /* First get the current state. */ 198 pwm_get_state(pwm, state); 199 200 /* Then fill it with the reference config */ 201 pwm_get_args(pwm, &args); 202 203 state->period = args.period; 204 state->polarity = args.polarity; 205 state->duty_cycle = 0; 206 state->usage_power = false; 207 } 208 209 /** 210 * pwm_get_relative_duty_cycle() - Get a relative duty cycle value 211 * @state: PWM state to extract the duty cycle from 212 * @scale: target scale of the relative duty cycle 213 * 214 * This functions converts the absolute duty cycle stored in @state (expressed 215 * in nanosecond) into a value relative to the period. 216 * 217 * For example if you want to get the duty_cycle expressed in percent, call: 218 * 219 * pwm_get_state(pwm, &state); 220 * duty = pwm_get_relative_duty_cycle(&state, 100); 221 */ 222 static inline unsigned int 223 pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale) 224 { 225 if (!state->period) 226 return 0; 227 228 return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale, 229 state->period); 230 } 231 232 /** 233 * pwm_set_relative_duty_cycle() - Set a relative duty cycle value 234 * @state: PWM state to fill 235 * @duty_cycle: relative duty cycle value 236 * @scale: scale in which @duty_cycle is expressed 237 * 238 * This functions converts a relative into an absolute duty cycle (expressed 239 * in nanoseconds), and puts the result in state->duty_cycle. 240 * 241 * For example if you want to configure a 50% duty cycle, call: 242 * 243 * pwm_init_state(pwm, &state); 244 * pwm_set_relative_duty_cycle(&state, 50, 100); 245 * pwm_apply_might_sleep(pwm, &state); 246 * 247 * This functions returns -EINVAL if @duty_cycle and/or @scale are 248 * inconsistent (@scale == 0 or @duty_cycle > @scale). 249 */ 250 static inline int 251 pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle, 252 unsigned int scale) 253 { 254 if (!scale || duty_cycle > scale) 255 return -EINVAL; 256 257 state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle * 258 state->period, 259 scale); 260 261 return 0; 262 } 263 264 /** 265 * struct pwm_capture - PWM capture data 266 * @period: period of the PWM signal (in nanoseconds) 267 * @duty_cycle: duty cycle of the PWM signal (in nanoseconds) 268 */ 269 struct pwm_capture { 270 unsigned int period; 271 unsigned int duty_cycle; 272 }; 273 274 /** 275 * struct pwm_ops - PWM controller operations 276 * @request: optional hook for requesting a PWM 277 * @free: optional hook for freeing a PWM 278 * @capture: capture and report PWM signal 279 * @sizeof_wfhw: size (in bytes) of driver specific waveform presentation 280 * @round_waveform_tohw: convert a struct pwm_waveform to driver specific presentation 281 * @round_waveform_fromhw: convert a driver specific waveform presentation to struct pwm_waveform 282 * @read_waveform: read driver specific waveform presentation from hardware 283 * @write_waveform: write driver specific waveform presentation to hardware 284 * @apply: atomically apply a new PWM config 285 * @get_state: get the current PWM state. 286 */ 287 struct pwm_ops { 288 int (*request)(struct pwm_chip *chip, struct pwm_device *pwm); 289 void (*free)(struct pwm_chip *chip, struct pwm_device *pwm); 290 int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm, 291 struct pwm_capture *result, unsigned long timeout); 292 293 size_t sizeof_wfhw; 294 int (*round_waveform_tohw)(struct pwm_chip *chip, struct pwm_device *pwm, 295 const struct pwm_waveform *wf, void *wfhw); 296 int (*round_waveform_fromhw)(struct pwm_chip *chip, struct pwm_device *pwm, 297 const void *wfhw, struct pwm_waveform *wf); 298 int (*read_waveform)(struct pwm_chip *chip, struct pwm_device *pwm, 299 void *wfhw); 300 int (*write_waveform)(struct pwm_chip *chip, struct pwm_device *pwm, 301 const void *wfhw); 302 303 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm, 304 const struct pwm_state *state); 305 int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm, 306 struct pwm_state *state); 307 }; 308 309 /** 310 * struct pwm_chip - abstract a PWM controller 311 * @dev: device providing the PWMs 312 * @ops: callbacks for this PWM controller 313 * @owner: module providing this chip 314 * @id: unique number of this PWM chip 315 * @npwm: number of PWMs controlled by this chip 316 * @of_xlate: request a PWM device given a device tree PWM specifier 317 * @atomic: can the driver's ->apply() be called in atomic context 318 * @uses_pwmchip_alloc: signals if pwmchip_allow was used to allocate this chip 319 * @operational: signals if the chip can be used (or is already deregistered) 320 * @nonatomic_lock: mutex for nonatomic chips 321 * @atomic_lock: mutex for atomic chips 322 * @pwms: array of PWM devices allocated by the framework 323 */ 324 struct pwm_chip { 325 struct device dev; 326 const struct pwm_ops *ops; 327 struct module *owner; 328 unsigned int id; 329 unsigned int npwm; 330 331 struct pwm_device * (*of_xlate)(struct pwm_chip *chip, 332 const struct of_phandle_args *args); 333 bool atomic; 334 335 /* only used internally by the PWM framework */ 336 bool uses_pwmchip_alloc; 337 bool operational; 338 union { 339 /* 340 * depending on the chip being atomic or not either the mutex or 341 * the spinlock is used. It protects .operational and 342 * synchronizes the callbacks in .ops 343 */ 344 struct mutex nonatomic_lock; 345 spinlock_t atomic_lock; 346 }; 347 struct pwm_device pwms[] __counted_by(npwm); 348 }; 349 350 /** 351 * pwmchip_supports_waveform() - checks if the given chip supports waveform callbacks 352 * @chip: The pwm_chip to test 353 * 354 * Returns true iff the pwm chip support the waveform functions like 355 * pwm_set_waveform_might_sleep() and pwm_round_waveform_might_sleep() 356 */ 357 static inline bool pwmchip_supports_waveform(struct pwm_chip *chip) 358 { 359 /* 360 * only check for .write_waveform(). If that is available, 361 * .round_waveform_tohw() and .round_waveform_fromhw() asserted to be 362 * available, too, in pwmchip_add(). 363 */ 364 return chip->ops->write_waveform != NULL; 365 } 366 367 static inline struct device *pwmchip_parent(const struct pwm_chip *chip) 368 { 369 return chip->dev.parent; 370 } 371 372 static inline void *pwmchip_get_drvdata(struct pwm_chip *chip) 373 { 374 return dev_get_drvdata(&chip->dev); 375 } 376 377 static inline void pwmchip_set_drvdata(struct pwm_chip *chip, void *data) 378 { 379 dev_set_drvdata(&chip->dev, data); 380 } 381 382 #if IS_ENABLED(CONFIG_PWM) 383 384 /* PWM consumer APIs */ 385 int pwm_round_waveform_might_sleep(struct pwm_device *pwm, struct pwm_waveform *wf); 386 int pwm_get_waveform_might_sleep(struct pwm_device *pwm, struct pwm_waveform *wf); 387 int pwm_set_waveform_might_sleep(struct pwm_device *pwm, const struct pwm_waveform *wf, bool exact); 388 int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state); 389 int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state); 390 int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *state); 391 int pwm_adjust_config(struct pwm_device *pwm); 392 393 /** 394 * pwm_config() - change a PWM device configuration 395 * @pwm: PWM device 396 * @duty_ns: "on" time (in nanoseconds) 397 * @period_ns: duration (in nanoseconds) of one cycle 398 * 399 * Returns: 0 on success or a negative error code on failure. 400 */ 401 static inline int pwm_config(struct pwm_device *pwm, int duty_ns, 402 int period_ns) 403 { 404 struct pwm_state state; 405 406 if (!pwm) 407 return -EINVAL; 408 409 if (duty_ns < 0 || period_ns < 0) 410 return -EINVAL; 411 412 pwm_get_state(pwm, &state); 413 if (state.duty_cycle == duty_ns && state.period == period_ns) 414 return 0; 415 416 state.duty_cycle = duty_ns; 417 state.period = period_ns; 418 return pwm_apply_might_sleep(pwm, &state); 419 } 420 421 /** 422 * pwm_enable() - start a PWM output toggling 423 * @pwm: PWM device 424 * 425 * Returns: 0 on success or a negative error code on failure. 426 */ 427 static inline int pwm_enable(struct pwm_device *pwm) 428 { 429 struct pwm_state state; 430 431 if (!pwm) 432 return -EINVAL; 433 434 pwm_get_state(pwm, &state); 435 if (state.enabled) 436 return 0; 437 438 state.enabled = true; 439 return pwm_apply_might_sleep(pwm, &state); 440 } 441 442 /** 443 * pwm_disable() - stop a PWM output toggling 444 * @pwm: PWM device 445 */ 446 static inline void pwm_disable(struct pwm_device *pwm) 447 { 448 struct pwm_state state; 449 450 if (!pwm) 451 return; 452 453 pwm_get_state(pwm, &state); 454 if (!state.enabled) 455 return; 456 457 state.enabled = false; 458 pwm_apply_might_sleep(pwm, &state); 459 } 460 461 /** 462 * pwm_might_sleep() - is pwm_apply_atomic() supported? 463 * @pwm: PWM device 464 * 465 * Returns: false if pwm_apply_atomic() can be called from atomic context. 466 */ 467 static inline bool pwm_might_sleep(struct pwm_device *pwm) 468 { 469 return !pwm->chip->atomic; 470 } 471 472 /* PWM provider APIs */ 473 void pwmchip_put(struct pwm_chip *chip); 474 struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv); 475 struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv); 476 477 int __pwmchip_add(struct pwm_chip *chip, struct module *owner); 478 #define pwmchip_add(chip) __pwmchip_add(chip, THIS_MODULE) 479 void pwmchip_remove(struct pwm_chip *chip); 480 481 int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner); 482 #define devm_pwmchip_add(dev, chip) __devm_pwmchip_add(dev, chip, THIS_MODULE) 483 484 struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *chip, 485 const struct of_phandle_args *args); 486 struct pwm_device *of_pwm_single_xlate(struct pwm_chip *chip, 487 const struct of_phandle_args *args); 488 489 struct pwm_device *pwm_get(struct device *dev, const char *con_id); 490 void pwm_put(struct pwm_device *pwm); 491 492 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id); 493 struct pwm_device *devm_fwnode_pwm_get(struct device *dev, 494 struct fwnode_handle *fwnode, 495 const char *con_id); 496 #else 497 static inline bool pwm_might_sleep(struct pwm_device *pwm) 498 { 499 return true; 500 } 501 502 static inline int pwm_apply_might_sleep(struct pwm_device *pwm, 503 const struct pwm_state *state) 504 { 505 might_sleep(); 506 return -EOPNOTSUPP; 507 } 508 509 static inline int pwm_apply_atomic(struct pwm_device *pwm, 510 const struct pwm_state *state) 511 { 512 return -EOPNOTSUPP; 513 } 514 515 static inline int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *state) 516 { 517 return -EOPNOTSUPP; 518 } 519 520 static inline int pwm_adjust_config(struct pwm_device *pwm) 521 { 522 return -EOPNOTSUPP; 523 } 524 525 static inline int pwm_config(struct pwm_device *pwm, int duty_ns, 526 int period_ns) 527 { 528 might_sleep(); 529 return -EINVAL; 530 } 531 532 static inline int pwm_enable(struct pwm_device *pwm) 533 { 534 might_sleep(); 535 return -EINVAL; 536 } 537 538 static inline void pwm_disable(struct pwm_device *pwm) 539 { 540 might_sleep(); 541 } 542 543 static inline void pwmchip_put(struct pwm_chip *chip) 544 { 545 } 546 547 static inline struct pwm_chip *pwmchip_alloc(struct device *parent, 548 unsigned int npwm, 549 size_t sizeof_priv) 550 { 551 return ERR_PTR(-EINVAL); 552 } 553 554 static inline struct pwm_chip *devm_pwmchip_alloc(struct device *parent, 555 unsigned int npwm, 556 size_t sizeof_priv) 557 { 558 return pwmchip_alloc(parent, npwm, sizeof_priv); 559 } 560 561 static inline int pwmchip_add(struct pwm_chip *chip) 562 { 563 return -EINVAL; 564 } 565 566 static inline int pwmchip_remove(struct pwm_chip *chip) 567 { 568 return -EINVAL; 569 } 570 571 static inline int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip) 572 { 573 return -EINVAL; 574 } 575 576 static inline struct pwm_device *pwm_get(struct device *dev, 577 const char *consumer) 578 { 579 might_sleep(); 580 return ERR_PTR(-ENODEV); 581 } 582 583 static inline void pwm_put(struct pwm_device *pwm) 584 { 585 might_sleep(); 586 } 587 588 static inline struct pwm_device *devm_pwm_get(struct device *dev, 589 const char *consumer) 590 { 591 might_sleep(); 592 return ERR_PTR(-ENODEV); 593 } 594 595 static inline struct pwm_device * 596 devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode, 597 const char *con_id) 598 { 599 might_sleep(); 600 return ERR_PTR(-ENODEV); 601 } 602 #endif 603 604 static inline void pwm_apply_args(struct pwm_device *pwm) 605 { 606 struct pwm_state state = { }; 607 608 /* 609 * PWM users calling pwm_apply_args() expect to have a fresh config 610 * where the polarity and period are set according to pwm_args info. 611 * The problem is, polarity can only be changed when the PWM is 612 * disabled. 613 * 614 * PWM drivers supporting hardware readout may declare the PWM device 615 * as enabled, and prevent polarity setting, which changes from the 616 * existing behavior, where all PWM devices are declared as disabled 617 * at startup (even if they are actually enabled), thus authorizing 618 * polarity setting. 619 * 620 * To fulfill this requirement, we apply a new state which disables 621 * the PWM device and set the reference period and polarity config. 622 * 623 * Note that PWM users requiring a smooth handover between the 624 * bootloader and the kernel (like critical regulators controlled by 625 * PWM devices) will have to switch to the atomic API and avoid calling 626 * pwm_apply_args(). 627 */ 628 629 state.enabled = false; 630 state.polarity = pwm->args.polarity; 631 state.period = pwm->args.period; 632 state.usage_power = false; 633 634 pwm_apply_might_sleep(pwm, &state); 635 } 636 637 struct pwm_lookup { 638 struct list_head list; 639 const char *provider; 640 unsigned int index; 641 const char *dev_id; 642 const char *con_id; 643 unsigned int period; 644 enum pwm_polarity polarity; 645 const char *module; /* optional, may be NULL */ 646 }; 647 648 #define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \ 649 _period, _polarity, _module) \ 650 { \ 651 .provider = _provider, \ 652 .index = _index, \ 653 .dev_id = _dev_id, \ 654 .con_id = _con_id, \ 655 .period = _period, \ 656 .polarity = _polarity, \ 657 .module = _module, \ 658 } 659 660 #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \ 661 PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \ 662 _polarity, NULL) 663 664 #if IS_ENABLED(CONFIG_PWM) 665 void pwm_add_table(struct pwm_lookup *table, size_t num); 666 void pwm_remove_table(struct pwm_lookup *table, size_t num); 667 #else 668 static inline void pwm_add_table(struct pwm_lookup *table, size_t num) 669 { 670 } 671 672 static inline void pwm_remove_table(struct pwm_lookup *table, size_t num) 673 { 674 } 675 #endif 676 677 #endif /* __LINUX_PWM_H */ 678