at v6.16 679 lines 19 kB view raw
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 11MODULE_IMPORT_NS("PWM"); 12 13struct 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 */ 24enum 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 */ 42struct pwm_args { 43 u64 period; 44 enum pwm_polarity polarity; 45}; 46 47enum { 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 */ 71struct 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 */ 88struct 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 */ 106struct 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 */ 127static inline void pwm_get_state(const struct pwm_device *pwm, 128 struct pwm_state *state) 129{ 130 *state = pwm->state; 131} 132 133static 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 142static 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 151static 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 160static 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 169static 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 */ 192static 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 * Returns: rounded relative duty cycle multiplied by @scale 223 */ 224static inline unsigned int 225pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale) 226{ 227 if (!state->period) 228 return 0; 229 230 return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale, 231 state->period); 232} 233 234/** 235 * pwm_set_relative_duty_cycle() - Set a relative duty cycle value 236 * @state: PWM state to fill 237 * @duty_cycle: relative duty cycle value 238 * @scale: scale in which @duty_cycle is expressed 239 * 240 * This functions converts a relative into an absolute duty cycle (expressed 241 * in nanoseconds), and puts the result in state->duty_cycle. 242 * 243 * For example if you want to configure a 50% duty cycle, call: 244 * 245 * pwm_init_state(pwm, &state); 246 * pwm_set_relative_duty_cycle(&state, 50, 100); 247 * pwm_apply_might_sleep(pwm, &state); 248 * 249 * Returns: 0 on success or ``-EINVAL`` if @duty_cycle and/or @scale are 250 * inconsistent (@scale == 0 or @duty_cycle > @scale) 251 */ 252static inline int 253pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle, 254 unsigned int scale) 255{ 256 if (!scale || duty_cycle > scale) 257 return -EINVAL; 258 259 state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle * 260 state->period, 261 scale); 262 263 return 0; 264} 265 266/** 267 * struct pwm_capture - PWM capture data 268 * @period: period of the PWM signal (in nanoseconds) 269 * @duty_cycle: duty cycle of the PWM signal (in nanoseconds) 270 */ 271struct pwm_capture { 272 unsigned int period; 273 unsigned int duty_cycle; 274}; 275 276/** 277 * struct pwm_ops - PWM controller operations 278 * @request: optional hook for requesting a PWM 279 * @free: optional hook for freeing a PWM 280 * @capture: capture and report PWM signal 281 * @sizeof_wfhw: size (in bytes) of driver specific waveform presentation 282 * @round_waveform_tohw: convert a struct pwm_waveform to driver specific presentation 283 * @round_waveform_fromhw: convert a driver specific waveform presentation to struct pwm_waveform 284 * @read_waveform: read driver specific waveform presentation from hardware 285 * @write_waveform: write driver specific waveform presentation to hardware 286 * @apply: atomically apply a new PWM config 287 * @get_state: get the current PWM state. 288 */ 289struct pwm_ops { 290 int (*request)(struct pwm_chip *chip, struct pwm_device *pwm); 291 void (*free)(struct pwm_chip *chip, struct pwm_device *pwm); 292 int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm, 293 struct pwm_capture *result, unsigned long timeout); 294 295 size_t sizeof_wfhw; 296 int (*round_waveform_tohw)(struct pwm_chip *chip, struct pwm_device *pwm, 297 const struct pwm_waveform *wf, void *wfhw); 298 int (*round_waveform_fromhw)(struct pwm_chip *chip, struct pwm_device *pwm, 299 const void *wfhw, struct pwm_waveform *wf); 300 int (*read_waveform)(struct pwm_chip *chip, struct pwm_device *pwm, 301 void *wfhw); 302 int (*write_waveform)(struct pwm_chip *chip, struct pwm_device *pwm, 303 const void *wfhw); 304 305 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm, 306 const struct pwm_state *state); 307 int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm, 308 struct pwm_state *state); 309}; 310 311/** 312 * struct pwm_chip - abstract a PWM controller 313 * @dev: device providing the PWMs 314 * @ops: callbacks for this PWM controller 315 * @owner: module providing this chip 316 * @id: unique number of this PWM chip 317 * @npwm: number of PWMs controlled by this chip 318 * @of_xlate: request a PWM device given a device tree PWM specifier 319 * @atomic: can the driver's ->apply() be called in atomic context 320 * @uses_pwmchip_alloc: signals if pwmchip_allow was used to allocate this chip 321 * @operational: signals if the chip can be used (or is already deregistered) 322 * @nonatomic_lock: mutex for nonatomic chips 323 * @atomic_lock: mutex for atomic chips 324 * @pwms: array of PWM devices allocated by the framework 325 */ 326struct pwm_chip { 327 struct device dev; 328 const struct pwm_ops *ops; 329 struct module *owner; 330 unsigned int id; 331 unsigned int npwm; 332 333 struct pwm_device * (*of_xlate)(struct pwm_chip *chip, 334 const struct of_phandle_args *args); 335 bool atomic; 336 337 /* only used internally by the PWM framework */ 338 bool uses_pwmchip_alloc; 339 bool operational; 340 union { 341 /* 342 * depending on the chip being atomic or not either the mutex or 343 * the spinlock is used. It protects .operational and 344 * synchronizes the callbacks in .ops 345 */ 346 struct mutex nonatomic_lock; 347 spinlock_t atomic_lock; 348 }; 349 struct pwm_device pwms[] __counted_by(npwm); 350}; 351 352/** 353 * pwmchip_supports_waveform() - checks if the given chip supports waveform callbacks 354 * @chip: The pwm_chip to test 355 * 356 * Returns: true iff the pwm chip support the waveform functions like 357 * pwm_set_waveform_might_sleep() and pwm_round_waveform_might_sleep() 358 */ 359static inline bool pwmchip_supports_waveform(struct pwm_chip *chip) 360{ 361 /* 362 * only check for .write_waveform(). If that is available, 363 * .round_waveform_tohw() and .round_waveform_fromhw() asserted to be 364 * available, too, in pwmchip_add(). 365 */ 366 return chip->ops->write_waveform != NULL; 367} 368 369static inline struct device *pwmchip_parent(const struct pwm_chip *chip) 370{ 371 return chip->dev.parent; 372} 373 374static inline void *pwmchip_get_drvdata(const struct pwm_chip *chip) 375{ 376 return dev_get_drvdata(&chip->dev); 377} 378 379static inline void pwmchip_set_drvdata(struct pwm_chip *chip, void *data) 380{ 381 dev_set_drvdata(&chip->dev, data); 382} 383 384#if IS_REACHABLE(CONFIG_PWM) 385 386/* PWM consumer APIs */ 387int pwm_round_waveform_might_sleep(struct pwm_device *pwm, struct pwm_waveform *wf); 388int pwm_get_waveform_might_sleep(struct pwm_device *pwm, struct pwm_waveform *wf); 389int pwm_set_waveform_might_sleep(struct pwm_device *pwm, const struct pwm_waveform *wf, bool exact); 390int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state); 391int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state); 392int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *state); 393int pwm_adjust_config(struct pwm_device *pwm); 394 395/** 396 * pwm_config() - change a PWM device configuration 397 * @pwm: PWM device 398 * @duty_ns: "on" time (in nanoseconds) 399 * @period_ns: duration (in nanoseconds) of one cycle 400 * 401 * Returns: 0 on success or a negative error code on failure. 402 */ 403static inline int pwm_config(struct pwm_device *pwm, int duty_ns, 404 int period_ns) 405{ 406 struct pwm_state state; 407 408 if (!pwm) 409 return -EINVAL; 410 411 if (duty_ns < 0 || period_ns < 0) 412 return -EINVAL; 413 414 pwm_get_state(pwm, &state); 415 if (state.duty_cycle == duty_ns && state.period == period_ns) 416 return 0; 417 418 state.duty_cycle = duty_ns; 419 state.period = period_ns; 420 return pwm_apply_might_sleep(pwm, &state); 421} 422 423/** 424 * pwm_enable() - start a PWM output toggling 425 * @pwm: PWM device 426 * 427 * Returns: 0 on success or a negative error code on failure. 428 */ 429static inline int pwm_enable(struct pwm_device *pwm) 430{ 431 struct pwm_state state; 432 433 if (!pwm) 434 return -EINVAL; 435 436 pwm_get_state(pwm, &state); 437 if (state.enabled) 438 return 0; 439 440 state.enabled = true; 441 return pwm_apply_might_sleep(pwm, &state); 442} 443 444/** 445 * pwm_disable() - stop a PWM output toggling 446 * @pwm: PWM device 447 */ 448static inline void pwm_disable(struct pwm_device *pwm) 449{ 450 struct pwm_state state; 451 452 if (!pwm) 453 return; 454 455 pwm_get_state(pwm, &state); 456 if (!state.enabled) 457 return; 458 459 state.enabled = false; 460 pwm_apply_might_sleep(pwm, &state); 461} 462 463/** 464 * pwm_might_sleep() - is pwm_apply_atomic() supported? 465 * @pwm: PWM device 466 * 467 * Returns: false if pwm_apply_atomic() can be called from atomic context. 468 */ 469static inline bool pwm_might_sleep(struct pwm_device *pwm) 470{ 471 return !pwm->chip->atomic; 472} 473 474/* PWM provider APIs */ 475void pwmchip_put(struct pwm_chip *chip); 476struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv); 477struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv); 478 479int __pwmchip_add(struct pwm_chip *chip, struct module *owner); 480#define pwmchip_add(chip) __pwmchip_add(chip, THIS_MODULE) 481void pwmchip_remove(struct pwm_chip *chip); 482 483int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner); 484#define devm_pwmchip_add(dev, chip) __devm_pwmchip_add(dev, chip, THIS_MODULE) 485 486struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *chip, 487 const struct of_phandle_args *args); 488struct pwm_device *of_pwm_single_xlate(struct pwm_chip *chip, 489 const struct of_phandle_args *args); 490 491struct pwm_device *pwm_get(struct device *dev, const char *con_id); 492void pwm_put(struct pwm_device *pwm); 493 494struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id); 495struct pwm_device *devm_fwnode_pwm_get(struct device *dev, 496 struct fwnode_handle *fwnode, 497 const char *con_id); 498#else 499static inline bool pwm_might_sleep(struct pwm_device *pwm) 500{ 501 return true; 502} 503 504static inline int pwm_apply_might_sleep(struct pwm_device *pwm, 505 const struct pwm_state *state) 506{ 507 might_sleep(); 508 return -EOPNOTSUPP; 509} 510 511static inline int pwm_apply_atomic(struct pwm_device *pwm, 512 const struct pwm_state *state) 513{ 514 return -EOPNOTSUPP; 515} 516 517static inline int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *state) 518{ 519 return -EOPNOTSUPP; 520} 521 522static inline int pwm_adjust_config(struct pwm_device *pwm) 523{ 524 return -EOPNOTSUPP; 525} 526 527static inline int pwm_config(struct pwm_device *pwm, int duty_ns, 528 int period_ns) 529{ 530 might_sleep(); 531 return -EINVAL; 532} 533 534static inline int pwm_enable(struct pwm_device *pwm) 535{ 536 might_sleep(); 537 return -EINVAL; 538} 539 540static inline void pwm_disable(struct pwm_device *pwm) 541{ 542 might_sleep(); 543} 544 545static inline void pwmchip_put(struct pwm_chip *chip) 546{ 547} 548 549static inline struct pwm_chip *pwmchip_alloc(struct device *parent, 550 unsigned int npwm, 551 size_t sizeof_priv) 552{ 553 return ERR_PTR(-EINVAL); 554} 555 556static inline struct pwm_chip *devm_pwmchip_alloc(struct device *parent, 557 unsigned int npwm, 558 size_t sizeof_priv) 559{ 560 return pwmchip_alloc(parent, npwm, sizeof_priv); 561} 562 563static inline int pwmchip_add(struct pwm_chip *chip) 564{ 565 return -EINVAL; 566} 567 568static inline int pwmchip_remove(struct pwm_chip *chip) 569{ 570 return -EINVAL; 571} 572 573static inline int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip) 574{ 575 return -EINVAL; 576} 577 578static inline struct pwm_device *pwm_get(struct device *dev, 579 const char *consumer) 580{ 581 might_sleep(); 582 return ERR_PTR(-ENODEV); 583} 584 585static inline void pwm_put(struct pwm_device *pwm) 586{ 587 might_sleep(); 588} 589 590static inline struct pwm_device *devm_pwm_get(struct device *dev, 591 const char *consumer) 592{ 593 might_sleep(); 594 return ERR_PTR(-ENODEV); 595} 596 597static inline struct pwm_device * 598devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode, 599 const char *con_id) 600{ 601 might_sleep(); 602 return ERR_PTR(-ENODEV); 603} 604#endif 605 606static inline void pwm_apply_args(struct pwm_device *pwm) 607{ 608 struct pwm_state state = { }; 609 610 /* 611 * PWM users calling pwm_apply_args() expect to have a fresh config 612 * where the polarity and period are set according to pwm_args info. 613 * The problem is, polarity can only be changed when the PWM is 614 * disabled. 615 * 616 * PWM drivers supporting hardware readout may declare the PWM device 617 * as enabled, and prevent polarity setting, which changes from the 618 * existing behavior, where all PWM devices are declared as disabled 619 * at startup (even if they are actually enabled), thus authorizing 620 * polarity setting. 621 * 622 * To fulfill this requirement, we apply a new state which disables 623 * the PWM device and set the reference period and polarity config. 624 * 625 * Note that PWM users requiring a smooth handover between the 626 * bootloader and the kernel (like critical regulators controlled by 627 * PWM devices) will have to switch to the atomic API and avoid calling 628 * pwm_apply_args(). 629 */ 630 631 state.enabled = false; 632 state.polarity = pwm->args.polarity; 633 state.period = pwm->args.period; 634 state.usage_power = false; 635 636 pwm_apply_might_sleep(pwm, &state); 637} 638 639struct pwm_lookup { 640 struct list_head list; 641 const char *provider; 642 unsigned int index; 643 const char *dev_id; 644 const char *con_id; 645 unsigned int period; 646 enum pwm_polarity polarity; 647 const char *module; /* optional, may be NULL */ 648}; 649 650#define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \ 651 _period, _polarity, _module) \ 652 { \ 653 .provider = _provider, \ 654 .index = _index, \ 655 .dev_id = _dev_id, \ 656 .con_id = _con_id, \ 657 .period = _period, \ 658 .polarity = _polarity, \ 659 .module = _module, \ 660 } 661 662#define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \ 663 PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \ 664 _polarity, NULL) 665 666#if IS_REACHABLE(CONFIG_PWM) 667void pwm_add_table(struct pwm_lookup *table, size_t num); 668void pwm_remove_table(struct pwm_lookup *table, size_t num); 669#else 670static inline void pwm_add_table(struct pwm_lookup *table, size_t num) 671{ 672} 673 674static inline void pwm_remove_table(struct pwm_lookup *table, size_t num) 675{ 676} 677#endif 678 679#endif /* __LINUX_PWM_H */