1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Generic pwmlib implementation 4 * 5 * Copyright (C) 2011 Sascha Hauer <s.hauer@pengutronix.de> 6 * Copyright (C) 2011-2012 Avionic Design GmbH 7 */ 8 9 #define DEFAULT_SYMBOL_NAMESPACE PWM 10 11 #include <linux/acpi.h> 12 #include <linux/module.h> 13 #include <linux/idr.h> 14 #include <linux/of.h> 15 #include <linux/pwm.h> 16 #include <linux/list.h> 17 #include <linux/mutex.h> 18 #include <linux/err.h> 19 #include <linux/slab.h> 20 #include <linux/device.h> 21 #include <linux/debugfs.h> 22 #include <linux/seq_file.h> 23 24 #include <dt-bindings/pwm/pwm.h> 25 26 #define CREATE_TRACE_POINTS 27 #include <trace/events/pwm.h> 28 29 /* protects access to pwm_chips */ 30 static DEFINE_MUTEX(pwm_lock); 31 32 static DEFINE_IDR(pwm_chips); 33 34 static void pwmchip_lock(struct pwm_chip *chip) 35 { 36 if (chip->atomic) 37 spin_lock(&chip->atomic_lock); 38 else 39 mutex_lock(&chip->nonatomic_lock); 40 } 41 42 static void pwmchip_unlock(struct pwm_chip *chip) 43 { 44 if (chip->atomic) 45 spin_unlock(&chip->atomic_lock); 46 else 47 mutex_unlock(&chip->nonatomic_lock); 48 } 49 50 DEFINE_GUARD(pwmchip, struct pwm_chip *, pwmchip_lock(_T), pwmchip_unlock(_T)) 51 52 static bool pwm_wf_valid(const struct pwm_waveform *wf) 53 { 54 /* 55 * For now restrict waveforms to period_length_ns <= S64_MAX to provide 56 * some space for future extensions. One possibility is to simplify 57 * representing waveforms with inverted polarity using negative values 58 * somehow. 59 */ 60 if (wf->period_length_ns > S64_MAX) 61 return false; 62 63 if (wf->duty_length_ns > wf->period_length_ns) 64 return false; 65 66 /* 67 * .duty_offset_ns is supposed to be smaller than .period_length_ns, apart 68 * from the corner case .duty_offset_ns == 0 && .period_length_ns == 0. 69 */ 70 if (wf->duty_offset_ns && wf->duty_offset_ns >= wf->period_length_ns) 71 return false; 72 73 return true; 74 } 75 76 static void pwm_wf2state(const struct pwm_waveform *wf, struct pwm_state *state) 77 { 78 if (wf->period_length_ns) { 79 if (wf->duty_length_ns + wf->duty_offset_ns < wf->period_length_ns) 80 *state = (struct pwm_state){ 81 .enabled = true, 82 .polarity = PWM_POLARITY_NORMAL, 83 .period = wf->period_length_ns, 84 .duty_cycle = wf->duty_length_ns, 85 }; 86 else 87 *state = (struct pwm_state){ 88 .enabled = true, 89 .polarity = PWM_POLARITY_INVERSED, 90 .period = wf->period_length_ns, 91 .duty_cycle = wf->period_length_ns - wf->duty_length_ns, 92 }; 93 } else { 94 *state = (struct pwm_state){ 95 .enabled = false, 96 }; 97 } 98 } 99 100 static void pwm_state2wf(const struct pwm_state *state, struct pwm_waveform *wf) 101 { 102 if (state->enabled) { 103 if (state->polarity == PWM_POLARITY_NORMAL) 104 *wf = (struct pwm_waveform){ 105 .period_length_ns = state->period, 106 .duty_length_ns = state->duty_cycle, 107 .duty_offset_ns = 0, 108 }; 109 else 110 *wf = (struct pwm_waveform){ 111 .period_length_ns = state->period, 112 .duty_length_ns = state->period - state->duty_cycle, 113 .duty_offset_ns = state->duty_cycle, 114 }; 115 } else { 116 *wf = (struct pwm_waveform){ 117 .period_length_ns = 0, 118 }; 119 } 120 } 121 122 static int pwmwfcmp(const struct pwm_waveform *a, const struct pwm_waveform *b) 123 { 124 if (a->period_length_ns > b->period_length_ns) 125 return 1; 126 127 if (a->period_length_ns < b->period_length_ns) 128 return -1; 129 130 if (a->duty_length_ns > b->duty_length_ns) 131 return 1; 132 133 if (a->duty_length_ns < b->duty_length_ns) 134 return -1; 135 136 if (a->duty_offset_ns > b->duty_offset_ns) 137 return 1; 138 139 if (a->duty_offset_ns < b->duty_offset_ns) 140 return -1; 141 142 return 0; 143 } 144 145 static bool pwm_check_rounding(const struct pwm_waveform *wf, 146 const struct pwm_waveform *wf_rounded) 147 { 148 if (!wf->period_length_ns) 149 return true; 150 151 if (wf->period_length_ns < wf_rounded->period_length_ns) 152 return false; 153 154 if (wf->duty_length_ns < wf_rounded->duty_length_ns) 155 return false; 156 157 if (wf->duty_offset_ns < wf_rounded->duty_offset_ns) 158 return false; 159 160 return true; 161 } 162 163 static int __pwm_round_waveform_tohw(struct pwm_chip *chip, struct pwm_device *pwm, 164 const struct pwm_waveform *wf, void *wfhw) 165 { 166 const struct pwm_ops *ops = chip->ops; 167 int ret; 168 169 ret = ops->round_waveform_tohw(chip, pwm, wf, wfhw); 170 trace_pwm_round_waveform_tohw(pwm, wf, wfhw, ret); 171 172 return ret; 173 } 174 175 static int __pwm_round_waveform_fromhw(struct pwm_chip *chip, struct pwm_device *pwm, 176 const void *wfhw, struct pwm_waveform *wf) 177 { 178 const struct pwm_ops *ops = chip->ops; 179 int ret; 180 181 ret = ops->round_waveform_fromhw(chip, pwm, wfhw, wf); 182 trace_pwm_round_waveform_fromhw(pwm, wfhw, wf, ret); 183 184 return ret; 185 } 186 187 static int __pwm_read_waveform(struct pwm_chip *chip, struct pwm_device *pwm, void *wfhw) 188 { 189 const struct pwm_ops *ops = chip->ops; 190 int ret; 191 192 ret = ops->read_waveform(chip, pwm, wfhw); 193 trace_pwm_read_waveform(pwm, wfhw, ret); 194 195 return ret; 196 } 197 198 static int __pwm_write_waveform(struct pwm_chip *chip, struct pwm_device *pwm, const void *wfhw) 199 { 200 const struct pwm_ops *ops = chip->ops; 201 int ret; 202 203 ret = ops->write_waveform(chip, pwm, wfhw); 204 trace_pwm_write_waveform(pwm, wfhw, ret); 205 206 return ret; 207 } 208 209 #define WFHWSIZE 20 210 211 /** 212 * pwm_round_waveform_might_sleep - Query hardware capabilities 213 * Cannot be used in atomic context. 214 * @pwm: PWM device 215 * @wf: waveform to round and output parameter 216 * 217 * Typically a given waveform cannot be implemented exactly by hardware, e.g. 218 * because hardware only supports coarse period resolution or no duty_offset. 219 * This function returns the actually implemented waveform if you pass wf to 220 * pwm_set_waveform_might_sleep now. 221 * 222 * Note however that the world doesn't stop turning when you call it, so when 223 * doing 224 * 225 * pwm_round_waveform_might_sleep(mypwm, &wf); 226 * pwm_set_waveform_might_sleep(mypwm, &wf, true); 227 * 228 * the latter might fail, e.g. because an input clock changed its rate between 229 * these two calls and the waveform determined by 230 * pwm_round_waveform_might_sleep() cannot be implemented any more. 231 * 232 * Returns 0 on success, 1 if there is no valid hardware configuration matching 233 * the input waveform under the PWM rounding rules or a negative errno. 234 */ 235 int pwm_round_waveform_might_sleep(struct pwm_device *pwm, struct pwm_waveform *wf) 236 { 237 struct pwm_chip *chip = pwm->chip; 238 const struct pwm_ops *ops = chip->ops; 239 struct pwm_waveform wf_req = *wf; 240 char wfhw[WFHWSIZE]; 241 int ret_tohw, ret_fromhw; 242 243 BUG_ON(WFHWSIZE < ops->sizeof_wfhw); 244 245 if (!pwm_wf_valid(wf)) 246 return -EINVAL; 247 248 guard(pwmchip)(chip); 249 250 if (!chip->operational) 251 return -ENODEV; 252 253 ret_tohw = __pwm_round_waveform_tohw(chip, pwm, wf, wfhw); 254 if (ret_tohw < 0) 255 return ret_tohw; 256 257 if (IS_ENABLED(CONFIG_PWM_DEBUG) && ret_tohw > 1) 258 dev_err(&chip->dev, "Unexpected return value from __pwm_round_waveform_tohw: requested %llu/%llu [+%llu], return value %d\n", 259 wf_req.duty_length_ns, wf_req.period_length_ns, wf_req.duty_offset_ns, ret_tohw); 260 261 ret_fromhw = __pwm_round_waveform_fromhw(chip, pwm, wfhw, wf); 262 if (ret_fromhw < 0) 263 return ret_fromhw; 264 265 if (IS_ENABLED(CONFIG_PWM_DEBUG) && ret_fromhw > 0) 266 dev_err(&chip->dev, "Unexpected return value from __pwm_round_waveform_fromhw: requested %llu/%llu [+%llu], return value %d\n", 267 wf_req.duty_length_ns, wf_req.period_length_ns, wf_req.duty_offset_ns, ret_tohw); 268 269 if (IS_ENABLED(CONFIG_PWM_DEBUG) && 270 ret_tohw == 0 && !pwm_check_rounding(&wf_req, wf)) 271 dev_err(&chip->dev, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu]\n", 272 wf_req.duty_length_ns, wf_req.period_length_ns, wf_req.duty_offset_ns, 273 wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns); 274 275 return ret_tohw; 276 } 277 EXPORT_SYMBOL_GPL(pwm_round_waveform_might_sleep); 278 279 /** 280 * pwm_get_waveform_might_sleep - Query hardware about current configuration 281 * Cannot be used in atomic context. 282 * @pwm: PWM device 283 * @wf: output parameter 284 * 285 * Stores the current configuration of the PWM in @wf. Note this is the 286 * equivalent of pwm_get_state_hw() (and not pwm_get_state()) for pwm_waveform. 287 */ 288 int pwm_get_waveform_might_sleep(struct pwm_device *pwm, struct pwm_waveform *wf) 289 { 290 struct pwm_chip *chip = pwm->chip; 291 const struct pwm_ops *ops = chip->ops; 292 char wfhw[WFHWSIZE]; 293 int err; 294 295 BUG_ON(WFHWSIZE < ops->sizeof_wfhw); 296 297 guard(pwmchip)(chip); 298 299 if (!chip->operational) 300 return -ENODEV; 301 302 err = __pwm_read_waveform(chip, pwm, &wfhw); 303 if (err) 304 return err; 305 306 return __pwm_round_waveform_fromhw(chip, pwm, &wfhw, wf); 307 } 308 EXPORT_SYMBOL_GPL(pwm_get_waveform_might_sleep); 309 310 /* Called with the pwmchip lock held */ 311 static int __pwm_set_waveform(struct pwm_device *pwm, 312 const struct pwm_waveform *wf, 313 bool exact) 314 { 315 struct pwm_chip *chip = pwm->chip; 316 const struct pwm_ops *ops = chip->ops; 317 char wfhw[WFHWSIZE]; 318 struct pwm_waveform wf_rounded; 319 int err; 320 321 BUG_ON(WFHWSIZE < ops->sizeof_wfhw); 322 323 if (!pwm_wf_valid(wf)) 324 return -EINVAL; 325 326 err = __pwm_round_waveform_tohw(chip, pwm, wf, &wfhw); 327 if (err) 328 return err; 329 330 if ((IS_ENABLED(CONFIG_PWM_DEBUG) || exact) && wf->period_length_ns) { 331 err = __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf_rounded); 332 if (err) 333 return err; 334 335 if (IS_ENABLED(CONFIG_PWM_DEBUG) && !pwm_check_rounding(wf, &wf_rounded)) 336 dev_err(&chip->dev, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu]\n", 337 wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns, 338 wf_rounded.duty_length_ns, wf_rounded.period_length_ns, wf_rounded.duty_offset_ns); 339 340 if (exact && pwmwfcmp(wf, &wf_rounded)) { 341 dev_dbg(&chip->dev, "Requested no rounding, but %llu/%llu [+%llu] -> %llu/%llu [+%llu]\n", 342 wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns, 343 wf_rounded.duty_length_ns, wf_rounded.period_length_ns, wf_rounded.duty_offset_ns); 344 345 return 1; 346 } 347 } 348 349 err = __pwm_write_waveform(chip, pwm, &wfhw); 350 if (err) 351 return err; 352 353 /* update .state */ 354 pwm_wf2state(wf, &pwm->state); 355 356 if (IS_ENABLED(CONFIG_PWM_DEBUG) && ops->read_waveform && wf->period_length_ns) { 357 struct pwm_waveform wf_set; 358 359 err = __pwm_read_waveform(chip, pwm, &wfhw); 360 if (err) 361 /* maybe ignore? */ 362 return err; 363 364 err = __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf_set); 365 if (err) 366 /* maybe ignore? */ 367 return err; 368 369 if (pwmwfcmp(&wf_set, &wf_rounded) != 0) 370 dev_err(&chip->dev, 371 "Unexpected setting: requested %llu/%llu [+%llu], expected %llu/%llu [+%llu], set %llu/%llu [+%llu]\n", 372 wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns, 373 wf_rounded.duty_length_ns, wf_rounded.period_length_ns, wf_rounded.duty_offset_ns, 374 wf_set.duty_length_ns, wf_set.period_length_ns, wf_set.duty_offset_ns); 375 } 376 return 0; 377 } 378 379 /** 380 * pwm_set_waveform_might_sleep - Apply a new waveform 381 * Cannot be used in atomic context. 382 * @pwm: PWM device 383 * @wf: The waveform to apply 384 * @exact: If true no rounding is allowed 385 * 386 * Typically a requested waveform cannot be implemented exactly, e.g. because 387 * you requested .period_length_ns = 100 ns, but the hardware can only set 388 * periods that are a multiple of 8.5 ns. With that hardware passing exact = 389 * true results in pwm_set_waveform_might_sleep() failing and returning 1. If 390 * exact = false you get a period of 93.5 ns (i.e. the biggest period not bigger 391 * than the requested value). 392 * Note that even with exact = true, some rounding by less than 1 is 393 * possible/needed. In the above example requesting .period_length_ns = 94 and 394 * exact = true, you get the hardware configured with period = 93.5 ns. 395 */ 396 int pwm_set_waveform_might_sleep(struct pwm_device *pwm, 397 const struct pwm_waveform *wf, bool exact) 398 { 399 struct pwm_chip *chip = pwm->chip; 400 int err; 401 402 might_sleep(); 403 404 guard(pwmchip)(chip); 405 406 if (!chip->operational) 407 return -ENODEV; 408 409 if (IS_ENABLED(CONFIG_PWM_DEBUG) && chip->atomic) { 410 /* 411 * Catch any drivers that have been marked as atomic but 412 * that will sleep anyway. 413 */ 414 non_block_start(); 415 err = __pwm_set_waveform(pwm, wf, exact); 416 non_block_end(); 417 } else { 418 err = __pwm_set_waveform(pwm, wf, exact); 419 } 420 421 return err; 422 } 423 EXPORT_SYMBOL_GPL(pwm_set_waveform_might_sleep); 424 425 static void pwm_apply_debug(struct pwm_device *pwm, 426 const struct pwm_state *state) 427 { 428 struct pwm_state *last = &pwm->last; 429 struct pwm_chip *chip = pwm->chip; 430 struct pwm_state s1 = { 0 }, s2 = { 0 }; 431 int err; 432 433 if (!IS_ENABLED(CONFIG_PWM_DEBUG)) 434 return; 435 436 /* No reasonable diagnosis possible without .get_state() */ 437 if (!chip->ops->get_state) 438 return; 439 440 /* 441 * *state was just applied. Read out the hardware state and do some 442 * checks. 443 */ 444 445 err = chip->ops->get_state(chip, pwm, &s1); 446 trace_pwm_get(pwm, &s1, err); 447 if (err) 448 /* If that failed there isn't much to debug */ 449 return; 450 451 /* 452 * The lowlevel driver either ignored .polarity (which is a bug) or as 453 * best effort inverted .polarity and fixed .duty_cycle respectively. 454 * Undo this inversion and fixup for further tests. 455 */ 456 if (s1.enabled && s1.polarity != state->polarity) { 457 s2.polarity = state->polarity; 458 s2.duty_cycle = s1.period - s1.duty_cycle; 459 s2.period = s1.period; 460 s2.enabled = s1.enabled; 461 } else { 462 s2 = s1; 463 } 464 465 if (s2.polarity != state->polarity && 466 state->duty_cycle < state->period) 467 dev_warn(pwmchip_parent(chip), ".apply ignored .polarity\n"); 468 469 if (state->enabled && 470 last->polarity == state->polarity && 471 last->period > s2.period && 472 last->period <= state->period) 473 dev_warn(pwmchip_parent(chip), 474 ".apply didn't pick the best available period (requested: %llu, applied: %llu, possible: %llu)\n", 475 state->period, s2.period, last->period); 476 477 if (state->enabled && state->period < s2.period) 478 dev_warn(pwmchip_parent(chip), 479 ".apply is supposed to round down period (requested: %llu, applied: %llu)\n", 480 state->period, s2.period); 481 482 if (state->enabled && 483 last->polarity == state->polarity && 484 last->period == s2.period && 485 last->duty_cycle > s2.duty_cycle && 486 last->duty_cycle <= state->duty_cycle) 487 dev_warn(pwmchip_parent(chip), 488 ".apply didn't pick the best available duty cycle (requested: %llu/%llu, applied: %llu/%llu, possible: %llu/%llu)\n", 489 state->duty_cycle, state->period, 490 s2.duty_cycle, s2.period, 491 last->duty_cycle, last->period); 492 493 if (state->enabled && state->duty_cycle < s2.duty_cycle) 494 dev_warn(pwmchip_parent(chip), 495 ".apply is supposed to round down duty_cycle (requested: %llu/%llu, applied: %llu/%llu)\n", 496 state->duty_cycle, state->period, 497 s2.duty_cycle, s2.period); 498 499 if (!state->enabled && s2.enabled && s2.duty_cycle > 0) 500 dev_warn(pwmchip_parent(chip), 501 "requested disabled, but yielded enabled with duty > 0\n"); 502 503 /* reapply the state that the driver reported being configured. */ 504 err = chip->ops->apply(chip, pwm, &s1); 505 trace_pwm_apply(pwm, &s1, err); 506 if (err) { 507 *last = s1; 508 dev_err(pwmchip_parent(chip), "failed to reapply current setting\n"); 509 return; 510 } 511 512 *last = (struct pwm_state){ 0 }; 513 err = chip->ops->get_state(chip, pwm, last); 514 trace_pwm_get(pwm, last, err); 515 if (err) 516 return; 517 518 /* reapplication of the current state should give an exact match */ 519 if (s1.enabled != last->enabled || 520 s1.polarity != last->polarity || 521 (s1.enabled && s1.period != last->period) || 522 (s1.enabled && s1.duty_cycle != last->duty_cycle)) { 523 dev_err(pwmchip_parent(chip), 524 ".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n", 525 s1.enabled, s1.polarity, s1.duty_cycle, s1.period, 526 last->enabled, last->polarity, last->duty_cycle, 527 last->period); 528 } 529 } 530 531 static bool pwm_state_valid(const struct pwm_state *state) 532 { 533 /* 534 * For a disabled state all other state description is irrelevant and 535 * and supposed to be ignored. So also ignore any strange values and 536 * consider the state ok. 537 */ 538 if (state->enabled) 539 return true; 540 541 if (!state->period) 542 return false; 543 544 if (state->duty_cycle > state->period) 545 return false; 546 547 return true; 548 } 549 550 /** 551 * __pwm_apply() - atomically apply a new state to a PWM device 552 * @pwm: PWM device 553 * @state: new state to apply 554 */ 555 static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state) 556 { 557 struct pwm_chip *chip; 558 const struct pwm_ops *ops; 559 int err; 560 561 if (!pwm || !state) 562 return -EINVAL; 563 564 if (!pwm_state_valid(state)) { 565 /* 566 * Allow to transition from one invalid state to another. 567 * This ensures that you can e.g. change the polarity while 568 * the period is zero. (This happens on stm32 when the hardware 569 * is in its poweron default state.) This greatly simplifies 570 * working with the sysfs API where you can only change one 571 * parameter at a time. 572 */ 573 if (!pwm_state_valid(&pwm->state)) { 574 pwm->state = *state; 575 return 0; 576 } 577 578 return -EINVAL; 579 } 580 581 chip = pwm->chip; 582 ops = chip->ops; 583 584 if (state->period == pwm->state.period && 585 state->duty_cycle == pwm->state.duty_cycle && 586 state->polarity == pwm->state.polarity && 587 state->enabled == pwm->state.enabled && 588 state->usage_power == pwm->state.usage_power) 589 return 0; 590 591 if (ops->write_waveform) { 592 struct pwm_waveform wf; 593 char wfhw[WFHWSIZE]; 594 595 BUG_ON(WFHWSIZE < ops->sizeof_wfhw); 596 597 pwm_state2wf(state, &wf); 598 599 /* 600 * The rounding is wrong here for states with inverted polarity. 601 * While .apply() rounds down duty_cycle (which represents the 602 * time from the start of the period to the inner edge), 603 * .round_waveform_tohw() rounds down the time the PWM is high. 604 * Can be fixed if the need arises, until reported otherwise 605 * let's assume that consumers don't care. 606 */ 607 608 err = __pwm_round_waveform_tohw(chip, pwm, &wf, &wfhw); 609 if (err) { 610 if (err > 0) 611 /* 612 * This signals an invalid request, typically 613 * the requested period (or duty_offset) is 614 * smaller than possible with the hardware. 615 */ 616 return -EINVAL; 617 618 return err; 619 } 620 621 if (IS_ENABLED(CONFIG_PWM_DEBUG)) { 622 struct pwm_waveform wf_rounded; 623 624 err = __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf_rounded); 625 if (err) 626 return err; 627 628 if (!pwm_check_rounding(&wf, &wf_rounded)) 629 dev_err(&chip->dev, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu]\n", 630 wf.duty_length_ns, wf.period_length_ns, wf.duty_offset_ns, 631 wf_rounded.duty_length_ns, wf_rounded.period_length_ns, wf_rounded.duty_offset_ns); 632 } 633 634 err = __pwm_write_waveform(chip, pwm, &wfhw); 635 if (err) 636 return err; 637 638 pwm->state = *state; 639 640 } else { 641 err = ops->apply(chip, pwm, state); 642 trace_pwm_apply(pwm, state, err); 643 if (err) 644 return err; 645 646 pwm->state = *state; 647 648 /* 649 * only do this after pwm->state was applied as some 650 * implementations of .get_state() depend on this 651 */ 652 pwm_apply_debug(pwm, state); 653 } 654 655 return 0; 656 } 657 658 /** 659 * pwm_apply_might_sleep() - atomically apply a new state to a PWM device 660 * Cannot be used in atomic context. 661 * @pwm: PWM device 662 * @state: new state to apply 663 */ 664 int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state) 665 { 666 int err; 667 struct pwm_chip *chip = pwm->chip; 668 669 /* 670 * Some lowlevel driver's implementations of .apply() make use of 671 * mutexes, also with some drivers only returning when the new 672 * configuration is active calling pwm_apply_might_sleep() from atomic context 673 * is a bad idea. So make it explicit that calling this function might 674 * sleep. 675 */ 676 might_sleep(); 677 678 guard(pwmchip)(chip); 679 680 if (!chip->operational) 681 return -ENODEV; 682 683 if (IS_ENABLED(CONFIG_PWM_DEBUG) && chip->atomic) { 684 /* 685 * Catch any drivers that have been marked as atomic but 686 * that will sleep anyway. 687 */ 688 non_block_start(); 689 err = __pwm_apply(pwm, state); 690 non_block_end(); 691 } else { 692 err = __pwm_apply(pwm, state); 693 } 694 695 return err; 696 } 697 EXPORT_SYMBOL_GPL(pwm_apply_might_sleep); 698 699 /** 700 * pwm_apply_atomic() - apply a new state to a PWM device from atomic context 701 * Not all PWM devices support this function, check with pwm_might_sleep(). 702 * @pwm: PWM device 703 * @state: new state to apply 704 */ 705 int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state) 706 { 707 struct pwm_chip *chip = pwm->chip; 708 709 WARN_ONCE(!chip->atomic, 710 "sleeping PWM driver used in atomic context\n"); 711 712 guard(pwmchip)(chip); 713 714 if (!chip->operational) 715 return -ENODEV; 716 717 return __pwm_apply(pwm, state); 718 } 719 EXPORT_SYMBOL_GPL(pwm_apply_atomic); 720 721 static int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *state) 722 { 723 struct pwm_chip *chip = pwm->chip; 724 const struct pwm_ops *ops = chip->ops; 725 int ret = -EOPNOTSUPP; 726 727 if (ops->read_waveform) { 728 char wfhw[WFHWSIZE]; 729 struct pwm_waveform wf; 730 731 BUG_ON(WFHWSIZE < ops->sizeof_wfhw); 732 733 scoped_guard(pwmchip, chip) { 734 735 ret = __pwm_read_waveform(chip, pwm, &wfhw); 736 if (ret) 737 return ret; 738 739 ret = __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf); 740 if (ret) 741 return ret; 742 } 743 744 pwm_wf2state(&wf, state); 745 746 } else if (ops->get_state) { 747 scoped_guard(pwmchip, chip) 748 ret = ops->get_state(chip, pwm, state); 749 750 trace_pwm_get(pwm, state, ret); 751 } 752 753 return ret; 754 } 755 756 /** 757 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments 758 * @pwm: PWM device 759 * 760 * This function will adjust the PWM config to the PWM arguments provided 761 * by the DT or PWM lookup table. This is particularly useful to adapt 762 * the bootloader config to the Linux one. 763 */ 764 int pwm_adjust_config(struct pwm_device *pwm) 765 { 766 struct pwm_state state; 767 struct pwm_args pargs; 768 769 pwm_get_args(pwm, &pargs); 770 pwm_get_state(pwm, &state); 771 772 /* 773 * If the current period is zero it means that either the PWM driver 774 * does not support initial state retrieval or the PWM has not yet 775 * been configured. 776 * 777 * In either case, we setup the new period and polarity, and assign a 778 * duty cycle of 0. 779 */ 780 if (!state.period) { 781 state.duty_cycle = 0; 782 state.period = pargs.period; 783 state.polarity = pargs.polarity; 784 785 return pwm_apply_might_sleep(pwm, &state); 786 } 787 788 /* 789 * Adjust the PWM duty cycle/period based on the period value provided 790 * in PWM args. 791 */ 792 if (pargs.period != state.period) { 793 u64 dutycycle = (u64)state.duty_cycle * pargs.period; 794 795 do_div(dutycycle, state.period); 796 state.duty_cycle = dutycycle; 797 state.period = pargs.period; 798 } 799 800 /* 801 * If the polarity changed, we should also change the duty cycle. 802 */ 803 if (pargs.polarity != state.polarity) { 804 state.polarity = pargs.polarity; 805 state.duty_cycle = state.period - state.duty_cycle; 806 } 807 808 return pwm_apply_might_sleep(pwm, &state); 809 } 810 EXPORT_SYMBOL_GPL(pwm_adjust_config); 811 812 /** 813 * pwm_capture() - capture and report a PWM signal 814 * @pwm: PWM device 815 * @result: structure to fill with capture result 816 * @timeout: time to wait, in milliseconds, before giving up on capture 817 * 818 * Returns: 0 on success or a negative error code on failure. 819 */ 820 static int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, 821 unsigned long timeout) 822 { 823 struct pwm_chip *chip = pwm->chip; 824 const struct pwm_ops *ops = chip->ops; 825 826 if (!ops->capture) 827 return -ENOSYS; 828 829 /* 830 * Holding the pwm_lock is probably not needed. If you use pwm_capture() 831 * and you're interested to speed it up, please convince yourself it's 832 * really not needed, test and then suggest a patch on the mailing list. 833 */ 834 guard(mutex)(&pwm_lock); 835 836 guard(pwmchip)(chip); 837 838 if (!chip->operational) 839 return -ENODEV; 840 841 return ops->capture(chip, pwm, result, timeout); 842 } 843 844 static struct pwm_chip *pwmchip_find_by_name(const char *name) 845 { 846 struct pwm_chip *chip; 847 unsigned long id, tmp; 848 849 if (!name) 850 return NULL; 851 852 guard(mutex)(&pwm_lock); 853 854 idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) { 855 const char *chip_name = dev_name(pwmchip_parent(chip)); 856 857 if (chip_name && strcmp(chip_name, name) == 0) 858 return chip; 859 } 860 861 return NULL; 862 } 863 864 static int pwm_device_request(struct pwm_device *pwm, const char *label) 865 { 866 int err; 867 struct pwm_chip *chip = pwm->chip; 868 const struct pwm_ops *ops = chip->ops; 869 870 if (test_bit(PWMF_REQUESTED, &pwm->flags)) 871 return -EBUSY; 872 873 /* 874 * This function is called while holding pwm_lock. As .operational only 875 * changes while holding this lock, checking it here without holding the 876 * chip lock is fine. 877 */ 878 if (!chip->operational) 879 return -ENODEV; 880 881 if (!try_module_get(chip->owner)) 882 return -ENODEV; 883 884 if (!get_device(&chip->dev)) { 885 err = -ENODEV; 886 goto err_get_device; 887 } 888 889 if (ops->request) { 890 err = ops->request(chip, pwm); 891 if (err) { 892 put_device(&chip->dev); 893 err_get_device: 894 module_put(chip->owner); 895 return err; 896 } 897 } 898 899 if (ops->read_waveform || ops->get_state) { 900 /* 901 * Zero-initialize state because most drivers are unaware of 902 * .usage_power. The other members of state are supposed to be 903 * set by lowlevel drivers. We still initialize the whole 904 * structure for simplicity even though this might paper over 905 * faulty implementations of .get_state(). 906 */ 907 struct pwm_state state = { 0, }; 908 909 err = pwm_get_state_hw(pwm, &state); 910 if (!err) 911 pwm->state = state; 912 913 if (IS_ENABLED(CONFIG_PWM_DEBUG)) 914 pwm->last = pwm->state; 915 } 916 917 set_bit(PWMF_REQUESTED, &pwm->flags); 918 pwm->label = label; 919 920 return 0; 921 } 922 923 /** 924 * pwm_request_from_chip() - request a PWM device relative to a PWM chip 925 * @chip: PWM chip 926 * @index: per-chip index of the PWM to request 927 * @label: a literal description string of this PWM 928 * 929 * Returns: A pointer to the PWM device at the given index of the given PWM 930 * chip. A negative error code is returned if the index is not valid for the 931 * specified PWM chip or if the PWM device cannot be requested. 932 */ 933 static struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, 934 unsigned int index, 935 const char *label) 936 { 937 struct pwm_device *pwm; 938 int err; 939 940 if (!chip || index >= chip->npwm) 941 return ERR_PTR(-EINVAL); 942 943 guard(mutex)(&pwm_lock); 944 945 pwm = &chip->pwms[index]; 946 947 err = pwm_device_request(pwm, label); 948 if (err < 0) 949 return ERR_PTR(err); 950 951 return pwm; 952 } 953 954 struct pwm_device * 955 of_pwm_xlate_with_flags(struct pwm_chip *chip, const struct of_phandle_args *args) 956 { 957 struct pwm_device *pwm; 958 959 /* period in the second cell and flags in the third cell are optional */ 960 if (args->args_count < 1) 961 return ERR_PTR(-EINVAL); 962 963 pwm = pwm_request_from_chip(chip, args->args[0], NULL); 964 if (IS_ERR(pwm)) 965 return pwm; 966 967 if (args->args_count > 1) 968 pwm->args.period = args->args[1]; 969 970 pwm->args.polarity = PWM_POLARITY_NORMAL; 971 if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED) 972 pwm->args.polarity = PWM_POLARITY_INVERSED; 973 974 return pwm; 975 } 976 EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags); 977 978 struct pwm_device * 979 of_pwm_single_xlate(struct pwm_chip *chip, const struct of_phandle_args *args) 980 { 981 struct pwm_device *pwm; 982 983 pwm = pwm_request_from_chip(chip, 0, NULL); 984 if (IS_ERR(pwm)) 985 return pwm; 986 987 if (args->args_count > 0) 988 pwm->args.period = args->args[0]; 989 990 pwm->args.polarity = PWM_POLARITY_NORMAL; 991 if (args->args_count > 1 && args->args[1] & PWM_POLARITY_INVERTED) 992 pwm->args.polarity = PWM_POLARITY_INVERSED; 993 994 return pwm; 995 } 996 EXPORT_SYMBOL_GPL(of_pwm_single_xlate); 997 998 struct pwm_export { 999 struct device pwm_dev; 1000 struct pwm_device *pwm; 1001 struct mutex lock; 1002 struct pwm_state suspend; 1003 }; 1004 1005 static inline struct pwm_chip *pwmchip_from_dev(struct device *pwmchip_dev) 1006 { 1007 return container_of(pwmchip_dev, struct pwm_chip, dev); 1008 } 1009 1010 static inline struct pwm_export *pwmexport_from_dev(struct device *pwm_dev) 1011 { 1012 return container_of(pwm_dev, struct pwm_export, pwm_dev); 1013 } 1014 1015 static inline struct pwm_device *pwm_from_dev(struct device *pwm_dev) 1016 { 1017 struct pwm_export *export = pwmexport_from_dev(pwm_dev); 1018 1019 return export->pwm; 1020 } 1021 1022 static ssize_t period_show(struct device *pwm_dev, 1023 struct device_attribute *attr, 1024 char *buf) 1025 { 1026 const struct pwm_device *pwm = pwm_from_dev(pwm_dev); 1027 struct pwm_state state; 1028 1029 pwm_get_state(pwm, &state); 1030 1031 return sysfs_emit(buf, "%llu\n", state.period); 1032 } 1033 1034 static ssize_t period_store(struct device *pwm_dev, 1035 struct device_attribute *attr, 1036 const char *buf, size_t size) 1037 { 1038 struct pwm_export *export = pwmexport_from_dev(pwm_dev); 1039 struct pwm_device *pwm = export->pwm; 1040 struct pwm_state state; 1041 u64 val; 1042 int ret; 1043 1044 ret = kstrtou64(buf, 0, &val); 1045 if (ret) 1046 return ret; 1047 1048 guard(mutex)(&export->lock); 1049 1050 pwm_get_state(pwm, &state); 1051 state.period = val; 1052 ret = pwm_apply_might_sleep(pwm, &state); 1053 1054 return ret ? : size; 1055 } 1056 1057 static ssize_t duty_cycle_show(struct device *pwm_dev, 1058 struct device_attribute *attr, 1059 char *buf) 1060 { 1061 const struct pwm_device *pwm = pwm_from_dev(pwm_dev); 1062 struct pwm_state state; 1063 1064 pwm_get_state(pwm, &state); 1065 1066 return sysfs_emit(buf, "%llu\n", state.duty_cycle); 1067 } 1068 1069 static ssize_t duty_cycle_store(struct device *pwm_dev, 1070 struct device_attribute *attr, 1071 const char *buf, size_t size) 1072 { 1073 struct pwm_export *export = pwmexport_from_dev(pwm_dev); 1074 struct pwm_device *pwm = export->pwm; 1075 struct pwm_state state; 1076 u64 val; 1077 int ret; 1078 1079 ret = kstrtou64(buf, 0, &val); 1080 if (ret) 1081 return ret; 1082 1083 guard(mutex)(&export->lock); 1084 1085 pwm_get_state(pwm, &state); 1086 state.duty_cycle = val; 1087 ret = pwm_apply_might_sleep(pwm, &state); 1088 1089 return ret ? : size; 1090 } 1091 1092 static ssize_t enable_show(struct device *pwm_dev, 1093 struct device_attribute *attr, 1094 char *buf) 1095 { 1096 const struct pwm_device *pwm = pwm_from_dev(pwm_dev); 1097 struct pwm_state state; 1098 1099 pwm_get_state(pwm, &state); 1100 1101 return sysfs_emit(buf, "%d\n", state.enabled); 1102 } 1103 1104 static ssize_t enable_store(struct device *pwm_dev, 1105 struct device_attribute *attr, 1106 const char *buf, size_t size) 1107 { 1108 struct pwm_export *export = pwmexport_from_dev(pwm_dev); 1109 struct pwm_device *pwm = export->pwm; 1110 struct pwm_state state; 1111 int val, ret; 1112 1113 ret = kstrtoint(buf, 0, &val); 1114 if (ret) 1115 return ret; 1116 1117 guard(mutex)(&export->lock); 1118 1119 pwm_get_state(pwm, &state); 1120 1121 switch (val) { 1122 case 0: 1123 state.enabled = false; 1124 break; 1125 case 1: 1126 state.enabled = true; 1127 break; 1128 default: 1129 return -EINVAL; 1130 } 1131 1132 ret = pwm_apply_might_sleep(pwm, &state); 1133 1134 return ret ? : size; 1135 } 1136 1137 static ssize_t polarity_show(struct device *pwm_dev, 1138 struct device_attribute *attr, 1139 char *buf) 1140 { 1141 const struct pwm_device *pwm = pwm_from_dev(pwm_dev); 1142 const char *polarity = "unknown"; 1143 struct pwm_state state; 1144 1145 pwm_get_state(pwm, &state); 1146 1147 switch (state.polarity) { 1148 case PWM_POLARITY_NORMAL: 1149 polarity = "normal"; 1150 break; 1151 1152 case PWM_POLARITY_INVERSED: 1153 polarity = "inversed"; 1154 break; 1155 } 1156 1157 return sysfs_emit(buf, "%s\n", polarity); 1158 } 1159 1160 static ssize_t polarity_store(struct device *pwm_dev, 1161 struct device_attribute *attr, 1162 const char *buf, size_t size) 1163 { 1164 struct pwm_export *export = pwmexport_from_dev(pwm_dev); 1165 struct pwm_device *pwm = export->pwm; 1166 enum pwm_polarity polarity; 1167 struct pwm_state state; 1168 int ret; 1169 1170 if (sysfs_streq(buf, "normal")) 1171 polarity = PWM_POLARITY_NORMAL; 1172 else if (sysfs_streq(buf, "inversed")) 1173 polarity = PWM_POLARITY_INVERSED; 1174 else 1175 return -EINVAL; 1176 1177 guard(mutex)(&export->lock); 1178 1179 pwm_get_state(pwm, &state); 1180 state.polarity = polarity; 1181 ret = pwm_apply_might_sleep(pwm, &state); 1182 1183 return ret ? : size; 1184 } 1185 1186 static ssize_t capture_show(struct device *pwm_dev, 1187 struct device_attribute *attr, 1188 char *buf) 1189 { 1190 struct pwm_device *pwm = pwm_from_dev(pwm_dev); 1191 struct pwm_capture result; 1192 int ret; 1193 1194 ret = pwm_capture(pwm, &result, jiffies_to_msecs(HZ)); 1195 if (ret) 1196 return ret; 1197 1198 return sysfs_emit(buf, "%u %u\n", result.period, result.duty_cycle); 1199 } 1200 1201 static DEVICE_ATTR_RW(period); 1202 static DEVICE_ATTR_RW(duty_cycle); 1203 static DEVICE_ATTR_RW(enable); 1204 static DEVICE_ATTR_RW(polarity); 1205 static DEVICE_ATTR_RO(capture); 1206 1207 static struct attribute *pwm_attrs[] = { 1208 &dev_attr_period.attr, 1209 &dev_attr_duty_cycle.attr, 1210 &dev_attr_enable.attr, 1211 &dev_attr_polarity.attr, 1212 &dev_attr_capture.attr, 1213 NULL 1214 }; 1215 ATTRIBUTE_GROUPS(pwm); 1216 1217 static void pwm_export_release(struct device *pwm_dev) 1218 { 1219 struct pwm_export *export = pwmexport_from_dev(pwm_dev); 1220 1221 kfree(export); 1222 } 1223 1224 static int pwm_export_child(struct device *pwmchip_dev, struct pwm_device *pwm) 1225 { 1226 struct pwm_export *export; 1227 char *pwm_prop[2]; 1228 int ret; 1229 1230 if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags)) 1231 return -EBUSY; 1232 1233 export = kzalloc(sizeof(*export), GFP_KERNEL); 1234 if (!export) { 1235 clear_bit(PWMF_EXPORTED, &pwm->flags); 1236 return -ENOMEM; 1237 } 1238 1239 export->pwm = pwm; 1240 mutex_init(&export->lock); 1241 1242 export->pwm_dev.release = pwm_export_release; 1243 export->pwm_dev.parent = pwmchip_dev; 1244 export->pwm_dev.devt = MKDEV(0, 0); 1245 export->pwm_dev.groups = pwm_groups; 1246 dev_set_name(&export->pwm_dev, "pwm%u", pwm->hwpwm); 1247 1248 ret = device_register(&export->pwm_dev); 1249 if (ret) { 1250 clear_bit(PWMF_EXPORTED, &pwm->flags); 1251 put_device(&export->pwm_dev); 1252 export = NULL; 1253 return ret; 1254 } 1255 pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm); 1256 pwm_prop[1] = NULL; 1257 kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop); 1258 kfree(pwm_prop[0]); 1259 1260 return 0; 1261 } 1262 1263 static int pwm_unexport_match(struct device *pwm_dev, void *data) 1264 { 1265 return pwm_from_dev(pwm_dev) == data; 1266 } 1267 1268 static int pwm_unexport_child(struct device *pwmchip_dev, struct pwm_device *pwm) 1269 { 1270 struct device *pwm_dev; 1271 char *pwm_prop[2]; 1272 1273 if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags)) 1274 return -ENODEV; 1275 1276 pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match); 1277 if (!pwm_dev) 1278 return -ENODEV; 1279 1280 pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm); 1281 pwm_prop[1] = NULL; 1282 kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop); 1283 kfree(pwm_prop[0]); 1284 1285 /* for device_find_child() */ 1286 put_device(pwm_dev); 1287 device_unregister(pwm_dev); 1288 pwm_put(pwm); 1289 1290 return 0; 1291 } 1292 1293 static ssize_t export_store(struct device *pwmchip_dev, 1294 struct device_attribute *attr, 1295 const char *buf, size_t len) 1296 { 1297 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev); 1298 struct pwm_device *pwm; 1299 unsigned int hwpwm; 1300 int ret; 1301 1302 ret = kstrtouint(buf, 0, &hwpwm); 1303 if (ret < 0) 1304 return ret; 1305 1306 if (hwpwm >= chip->npwm) 1307 return -ENODEV; 1308 1309 pwm = pwm_request_from_chip(chip, hwpwm, "sysfs"); 1310 if (IS_ERR(pwm)) 1311 return PTR_ERR(pwm); 1312 1313 ret = pwm_export_child(pwmchip_dev, pwm); 1314 if (ret < 0) 1315 pwm_put(pwm); 1316 1317 return ret ? : len; 1318 } 1319 static DEVICE_ATTR_WO(export); 1320 1321 static ssize_t unexport_store(struct device *pwmchip_dev, 1322 struct device_attribute *attr, 1323 const char *buf, size_t len) 1324 { 1325 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev); 1326 unsigned int hwpwm; 1327 int ret; 1328 1329 ret = kstrtouint(buf, 0, &hwpwm); 1330 if (ret < 0) 1331 return ret; 1332 1333 if (hwpwm >= chip->npwm) 1334 return -ENODEV; 1335 1336 ret = pwm_unexport_child(pwmchip_dev, &chip->pwms[hwpwm]); 1337 1338 return ret ? : len; 1339 } 1340 static DEVICE_ATTR_WO(unexport); 1341 1342 static ssize_t npwm_show(struct device *pwmchip_dev, struct device_attribute *attr, 1343 char *buf) 1344 { 1345 const struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev); 1346 1347 return sysfs_emit(buf, "%u\n", chip->npwm); 1348 } 1349 static DEVICE_ATTR_RO(npwm); 1350 1351 static struct attribute *pwm_chip_attrs[] = { 1352 &dev_attr_export.attr, 1353 &dev_attr_unexport.attr, 1354 &dev_attr_npwm.attr, 1355 NULL, 1356 }; 1357 ATTRIBUTE_GROUPS(pwm_chip); 1358 1359 /* takes export->lock on success */ 1360 static struct pwm_export *pwm_class_get_state(struct device *pwmchip_dev, 1361 struct pwm_device *pwm, 1362 struct pwm_state *state) 1363 { 1364 struct device *pwm_dev; 1365 struct pwm_export *export; 1366 1367 if (!test_bit(PWMF_EXPORTED, &pwm->flags)) 1368 return NULL; 1369 1370 pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match); 1371 if (!pwm_dev) 1372 return NULL; 1373 1374 export = pwmexport_from_dev(pwm_dev); 1375 put_device(pwm_dev); /* for device_find_child() */ 1376 1377 mutex_lock(&export->lock); 1378 pwm_get_state(pwm, state); 1379 1380 return export; 1381 } 1382 1383 static int pwm_class_apply_state(struct pwm_export *export, 1384 struct pwm_device *pwm, 1385 struct pwm_state *state) 1386 { 1387 int ret = pwm_apply_might_sleep(pwm, state); 1388 1389 /* release lock taken in pwm_class_get_state */ 1390 mutex_unlock(&export->lock); 1391 1392 return ret; 1393 } 1394 1395 static int pwm_class_resume_npwm(struct device *pwmchip_dev, unsigned int npwm) 1396 { 1397 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev); 1398 unsigned int i; 1399 int ret = 0; 1400 1401 for (i = 0; i < npwm; i++) { 1402 struct pwm_device *pwm = &chip->pwms[i]; 1403 struct pwm_state state; 1404 struct pwm_export *export; 1405 1406 export = pwm_class_get_state(pwmchip_dev, pwm, &state); 1407 if (!export) 1408 continue; 1409 1410 /* If pwmchip was not enabled before suspend, do nothing. */ 1411 if (!export->suspend.enabled) { 1412 /* release lock taken in pwm_class_get_state */ 1413 mutex_unlock(&export->lock); 1414 continue; 1415 } 1416 1417 state.enabled = export->suspend.enabled; 1418 ret = pwm_class_apply_state(export, pwm, &state); 1419 if (ret < 0) 1420 break; 1421 } 1422 1423 return ret; 1424 } 1425 1426 static int pwm_class_suspend(struct device *pwmchip_dev) 1427 { 1428 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev); 1429 unsigned int i; 1430 int ret = 0; 1431 1432 for (i = 0; i < chip->npwm; i++) { 1433 struct pwm_device *pwm = &chip->pwms[i]; 1434 struct pwm_state state; 1435 struct pwm_export *export; 1436 1437 export = pwm_class_get_state(pwmchip_dev, pwm, &state); 1438 if (!export) 1439 continue; 1440 1441 /* 1442 * If pwmchip was not enabled before suspend, save 1443 * state for resume time and do nothing else. 1444 */ 1445 export->suspend = state; 1446 if (!state.enabled) { 1447 /* release lock taken in pwm_class_get_state */ 1448 mutex_unlock(&export->lock); 1449 continue; 1450 } 1451 1452 state.enabled = false; 1453 ret = pwm_class_apply_state(export, pwm, &state); 1454 if (ret < 0) { 1455 /* 1456 * roll back the PWM devices that were disabled by 1457 * this suspend function. 1458 */ 1459 pwm_class_resume_npwm(pwmchip_dev, i); 1460 break; 1461 } 1462 } 1463 1464 return ret; 1465 } 1466 1467 static int pwm_class_resume(struct device *pwmchip_dev) 1468 { 1469 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev); 1470 1471 return pwm_class_resume_npwm(pwmchip_dev, chip->npwm); 1472 } 1473 1474 static DEFINE_SIMPLE_DEV_PM_OPS(pwm_class_pm_ops, pwm_class_suspend, pwm_class_resume); 1475 1476 static struct class pwm_class = { 1477 .name = "pwm", 1478 .dev_groups = pwm_chip_groups, 1479 .pm = pm_sleep_ptr(&pwm_class_pm_ops), 1480 }; 1481 1482 static void pwmchip_sysfs_unexport(struct pwm_chip *chip) 1483 { 1484 unsigned int i; 1485 1486 for (i = 0; i < chip->npwm; i++) { 1487 struct pwm_device *pwm = &chip->pwms[i]; 1488 1489 if (test_bit(PWMF_EXPORTED, &pwm->flags)) 1490 pwm_unexport_child(&chip->dev, pwm); 1491 } 1492 } 1493 1494 #define PWMCHIP_ALIGN ARCH_DMA_MINALIGN 1495 1496 static void *pwmchip_priv(struct pwm_chip *chip) 1497 { 1498 return (void *)chip + ALIGN(struct_size(chip, pwms, chip->npwm), PWMCHIP_ALIGN); 1499 } 1500 1501 /* This is the counterpart to pwmchip_alloc() */ 1502 void pwmchip_put(struct pwm_chip *chip) 1503 { 1504 put_device(&chip->dev); 1505 } 1506 EXPORT_SYMBOL_GPL(pwmchip_put); 1507 1508 static void pwmchip_release(struct device *pwmchip_dev) 1509 { 1510 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev); 1511 1512 kfree(chip); 1513 } 1514 1515 struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv) 1516 { 1517 struct pwm_chip *chip; 1518 struct device *pwmchip_dev; 1519 size_t alloc_size; 1520 unsigned int i; 1521 1522 alloc_size = size_add(ALIGN(struct_size(chip, pwms, npwm), PWMCHIP_ALIGN), 1523 sizeof_priv); 1524 1525 chip = kzalloc(alloc_size, GFP_KERNEL); 1526 if (!chip) 1527 return ERR_PTR(-ENOMEM); 1528 1529 chip->npwm = npwm; 1530 chip->uses_pwmchip_alloc = true; 1531 chip->operational = false; 1532 1533 pwmchip_dev = &chip->dev; 1534 device_initialize(pwmchip_dev); 1535 pwmchip_dev->class = &pwm_class; 1536 pwmchip_dev->parent = parent; 1537 pwmchip_dev->release = pwmchip_release; 1538 1539 pwmchip_set_drvdata(chip, pwmchip_priv(chip)); 1540 1541 for (i = 0; i < chip->npwm; i++) { 1542 struct pwm_device *pwm = &chip->pwms[i]; 1543 pwm->chip = chip; 1544 pwm->hwpwm = i; 1545 } 1546 1547 return chip; 1548 } 1549 EXPORT_SYMBOL_GPL(pwmchip_alloc); 1550 1551 static void devm_pwmchip_put(void *data) 1552 { 1553 struct pwm_chip *chip = data; 1554 1555 pwmchip_put(chip); 1556 } 1557 1558 struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv) 1559 { 1560 struct pwm_chip *chip; 1561 int ret; 1562 1563 chip = pwmchip_alloc(parent, npwm, sizeof_priv); 1564 if (IS_ERR(chip)) 1565 return chip; 1566 1567 ret = devm_add_action_or_reset(parent, devm_pwmchip_put, chip); 1568 if (ret) 1569 return ERR_PTR(ret); 1570 1571 return chip; 1572 } 1573 EXPORT_SYMBOL_GPL(devm_pwmchip_alloc); 1574 1575 static void of_pwmchip_add(struct pwm_chip *chip) 1576 { 1577 if (!pwmchip_parent(chip) || !pwmchip_parent(chip)->of_node) 1578 return; 1579 1580 if (!chip->of_xlate) 1581 chip->of_xlate = of_pwm_xlate_with_flags; 1582 1583 of_node_get(pwmchip_parent(chip)->of_node); 1584 } 1585 1586 static void of_pwmchip_remove(struct pwm_chip *chip) 1587 { 1588 if (pwmchip_parent(chip)) 1589 of_node_put(pwmchip_parent(chip)->of_node); 1590 } 1591 1592 static bool pwm_ops_check(const struct pwm_chip *chip) 1593 { 1594 const struct pwm_ops *ops = chip->ops; 1595 1596 if (ops->write_waveform) { 1597 if (!ops->round_waveform_tohw || 1598 !ops->round_waveform_fromhw || 1599 !ops->write_waveform) 1600 return false; 1601 1602 if (WFHWSIZE < ops->sizeof_wfhw) { 1603 dev_warn(pwmchip_parent(chip), "WFHWSIZE < %zu\n", ops->sizeof_wfhw); 1604 return false; 1605 } 1606 } else { 1607 if (!ops->apply) 1608 return false; 1609 1610 if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state) 1611 dev_warn(pwmchip_parent(chip), 1612 "Please implement the .get_state() callback\n"); 1613 } 1614 1615 return true; 1616 } 1617 1618 static struct device_link *pwm_device_link_add(struct device *dev, 1619 struct pwm_device *pwm) 1620 { 1621 struct device_link *dl; 1622 1623 if (!dev) { 1624 /* 1625 * No device for the PWM consumer has been provided. It may 1626 * impact the PM sequence ordering: the PWM supplier may get 1627 * suspended before the consumer. 1628 */ 1629 dev_warn(pwmchip_parent(pwm->chip), 1630 "No consumer device specified to create a link to\n"); 1631 return NULL; 1632 } 1633 1634 dl = device_link_add(dev, pwmchip_parent(pwm->chip), DL_FLAG_AUTOREMOVE_CONSUMER); 1635 if (!dl) { 1636 dev_err(dev, "failed to create device link to %s\n", 1637 dev_name(pwmchip_parent(pwm->chip))); 1638 return ERR_PTR(-EINVAL); 1639 } 1640 1641 return dl; 1642 } 1643 1644 static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode) 1645 { 1646 struct pwm_chip *chip; 1647 unsigned long id, tmp; 1648 1649 guard(mutex)(&pwm_lock); 1650 1651 idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) 1652 if (pwmchip_parent(chip) && device_match_fwnode(pwmchip_parent(chip), fwnode)) 1653 return chip; 1654 1655 return ERR_PTR(-EPROBE_DEFER); 1656 } 1657 1658 /** 1659 * of_pwm_get() - request a PWM via the PWM framework 1660 * @dev: device for PWM consumer 1661 * @np: device node to get the PWM from 1662 * @con_id: consumer name 1663 * 1664 * Returns the PWM device parsed from the phandle and index specified in the 1665 * "pwms" property of a device tree node or a negative error-code on failure. 1666 * Values parsed from the device tree are stored in the returned PWM device 1667 * object. 1668 * 1669 * If con_id is NULL, the first PWM device listed in the "pwms" property will 1670 * be requested. Otherwise the "pwm-names" property is used to do a reverse 1671 * lookup of the PWM index. This also means that the "pwm-names" property 1672 * becomes mandatory for devices that look up the PWM device via the con_id 1673 * parameter. 1674 * 1675 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded 1676 * error code on failure. 1677 */ 1678 static struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np, 1679 const char *con_id) 1680 { 1681 struct pwm_device *pwm = NULL; 1682 struct of_phandle_args args; 1683 struct device_link *dl; 1684 struct pwm_chip *chip; 1685 int index = 0; 1686 int err; 1687 1688 if (con_id) { 1689 index = of_property_match_string(np, "pwm-names", con_id); 1690 if (index < 0) 1691 return ERR_PTR(index); 1692 } 1693 1694 err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index, 1695 &args); 1696 if (err) { 1697 pr_err("%s(): can't parse \"pwms\" property\n", __func__); 1698 return ERR_PTR(err); 1699 } 1700 1701 chip = fwnode_to_pwmchip(of_fwnode_handle(args.np)); 1702 if (IS_ERR(chip)) { 1703 if (PTR_ERR(chip) != -EPROBE_DEFER) 1704 pr_err("%s(): PWM chip not found\n", __func__); 1705 1706 pwm = ERR_CAST(chip); 1707 goto put; 1708 } 1709 1710 pwm = chip->of_xlate(chip, &args); 1711 if (IS_ERR(pwm)) 1712 goto put; 1713 1714 dl = pwm_device_link_add(dev, pwm); 1715 if (IS_ERR(dl)) { 1716 /* of_xlate ended up calling pwm_request_from_chip() */ 1717 pwm_put(pwm); 1718 pwm = ERR_CAST(dl); 1719 goto put; 1720 } 1721 1722 /* 1723 * If a consumer name was not given, try to look it up from the 1724 * "pwm-names" property if it exists. Otherwise use the name of 1725 * the user device node. 1726 */ 1727 if (!con_id) { 1728 err = of_property_read_string_index(np, "pwm-names", index, 1729 &con_id); 1730 if (err < 0) 1731 con_id = np->name; 1732 } 1733 1734 pwm->label = con_id; 1735 1736 put: 1737 of_node_put(args.np); 1738 1739 return pwm; 1740 } 1741 1742 /** 1743 * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI 1744 * @fwnode: firmware node to get the "pwms" property from 1745 * 1746 * Returns the PWM device parsed from the fwnode and index specified in the 1747 * "pwms" property or a negative error-code on failure. 1748 * Values parsed from the device tree are stored in the returned PWM device 1749 * object. 1750 * 1751 * This is analogous to of_pwm_get() except con_id is not yet supported. 1752 * ACPI entries must look like 1753 * Package () {"pwms", Package () 1754 * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}} 1755 * 1756 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded 1757 * error code on failure. 1758 */ 1759 static struct pwm_device *acpi_pwm_get(const struct fwnode_handle *fwnode) 1760 { 1761 struct pwm_device *pwm; 1762 struct fwnode_reference_args args; 1763 struct pwm_chip *chip; 1764 int ret; 1765 1766 memset(&args, 0, sizeof(args)); 1767 1768 ret = __acpi_node_get_property_reference(fwnode, "pwms", 0, 3, &args); 1769 if (ret < 0) 1770 return ERR_PTR(ret); 1771 1772 if (args.nargs < 2) 1773 return ERR_PTR(-EPROTO); 1774 1775 chip = fwnode_to_pwmchip(args.fwnode); 1776 if (IS_ERR(chip)) 1777 return ERR_CAST(chip); 1778 1779 pwm = pwm_request_from_chip(chip, args.args[0], NULL); 1780 if (IS_ERR(pwm)) 1781 return pwm; 1782 1783 pwm->args.period = args.args[1]; 1784 pwm->args.polarity = PWM_POLARITY_NORMAL; 1785 1786 if (args.nargs > 2 && args.args[2] & PWM_POLARITY_INVERTED) 1787 pwm->args.polarity = PWM_POLARITY_INVERSED; 1788 1789 return pwm; 1790 } 1791 1792 static DEFINE_MUTEX(pwm_lookup_lock); 1793 static LIST_HEAD(pwm_lookup_list); 1794 1795 /** 1796 * pwm_get() - look up and request a PWM device 1797 * @dev: device for PWM consumer 1798 * @con_id: consumer name 1799 * 1800 * Lookup is first attempted using DT. If the device was not instantiated from 1801 * a device tree, a PWM chip and a relative index is looked up via a table 1802 * supplied by board setup code (see pwm_add_table()). 1803 * 1804 * Once a PWM chip has been found the specified PWM device will be requested 1805 * and is ready to be used. 1806 * 1807 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded 1808 * error code on failure. 1809 */ 1810 struct pwm_device *pwm_get(struct device *dev, const char *con_id) 1811 { 1812 const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL; 1813 const char *dev_id = dev ? dev_name(dev) : NULL; 1814 struct pwm_device *pwm; 1815 struct pwm_chip *chip; 1816 struct device_link *dl; 1817 unsigned int best = 0; 1818 struct pwm_lookup *p, *chosen = NULL; 1819 unsigned int match; 1820 int err; 1821 1822 /* look up via DT first */ 1823 if (is_of_node(fwnode)) 1824 return of_pwm_get(dev, to_of_node(fwnode), con_id); 1825 1826 /* then lookup via ACPI */ 1827 if (is_acpi_node(fwnode)) { 1828 pwm = acpi_pwm_get(fwnode); 1829 if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT) 1830 return pwm; 1831 } 1832 1833 /* 1834 * We look up the provider in the static table typically provided by 1835 * board setup code. We first try to lookup the consumer device by 1836 * name. If the consumer device was passed in as NULL or if no match 1837 * was found, we try to find the consumer by directly looking it up 1838 * by name. 1839 * 1840 * If a match is found, the provider PWM chip is looked up by name 1841 * and a PWM device is requested using the PWM device per-chip index. 1842 * 1843 * The lookup algorithm was shamelessly taken from the clock 1844 * framework: 1845 * 1846 * We do slightly fuzzy matching here: 1847 * An entry with a NULL ID is assumed to be a wildcard. 1848 * If an entry has a device ID, it must match 1849 * If an entry has a connection ID, it must match 1850 * Then we take the most specific entry - with the following order 1851 * of precedence: dev+con > dev only > con only. 1852 */ 1853 scoped_guard(mutex, &pwm_lookup_lock) 1854 list_for_each_entry(p, &pwm_lookup_list, list) { 1855 match = 0; 1856 1857 if (p->dev_id) { 1858 if (!dev_id || strcmp(p->dev_id, dev_id)) 1859 continue; 1860 1861 match += 2; 1862 } 1863 1864 if (p->con_id) { 1865 if (!con_id || strcmp(p->con_id, con_id)) 1866 continue; 1867 1868 match += 1; 1869 } 1870 1871 if (match > best) { 1872 chosen = p; 1873 1874 if (match != 3) 1875 best = match; 1876 else 1877 break; 1878 } 1879 } 1880 1881 if (!chosen) 1882 return ERR_PTR(-ENODEV); 1883 1884 chip = pwmchip_find_by_name(chosen->provider); 1885 1886 /* 1887 * If the lookup entry specifies a module, load the module and retry 1888 * the PWM chip lookup. This can be used to work around driver load 1889 * ordering issues if driver's can't be made to properly support the 1890 * deferred probe mechanism. 1891 */ 1892 if (!chip && chosen->module) { 1893 err = request_module(chosen->module); 1894 if (err == 0) 1895 chip = pwmchip_find_by_name(chosen->provider); 1896 } 1897 1898 if (!chip) 1899 return ERR_PTR(-EPROBE_DEFER); 1900 1901 pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id); 1902 if (IS_ERR(pwm)) 1903 return pwm; 1904 1905 dl = pwm_device_link_add(dev, pwm); 1906 if (IS_ERR(dl)) { 1907 pwm_put(pwm); 1908 return ERR_CAST(dl); 1909 } 1910 1911 pwm->args.period = chosen->period; 1912 pwm->args.polarity = chosen->polarity; 1913 1914 return pwm; 1915 } 1916 EXPORT_SYMBOL_GPL(pwm_get); 1917 1918 /** 1919 * pwm_put() - release a PWM device 1920 * @pwm: PWM device 1921 */ 1922 void pwm_put(struct pwm_device *pwm) 1923 { 1924 struct pwm_chip *chip; 1925 1926 if (!pwm) 1927 return; 1928 1929 chip = pwm->chip; 1930 1931 guard(mutex)(&pwm_lock); 1932 1933 /* 1934 * Trigger a warning if a consumer called pwm_put() twice. 1935 * If the chip isn't operational, PWMF_REQUESTED was already cleared in 1936 * pwmchip_remove(). So don't warn in this case. 1937 */ 1938 if (chip->operational && !test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) { 1939 pr_warn("PWM device already freed\n"); 1940 return; 1941 } 1942 1943 if (chip->operational && chip->ops->free) 1944 pwm->chip->ops->free(pwm->chip, pwm); 1945 1946 pwm->label = NULL; 1947 1948 put_device(&chip->dev); 1949 1950 module_put(chip->owner); 1951 } 1952 EXPORT_SYMBOL_GPL(pwm_put); 1953 1954 static void devm_pwm_release(void *pwm) 1955 { 1956 pwm_put(pwm); 1957 } 1958 1959 /** 1960 * devm_pwm_get() - resource managed pwm_get() 1961 * @dev: device for PWM consumer 1962 * @con_id: consumer name 1963 * 1964 * This function performs like pwm_get() but the acquired PWM device will 1965 * automatically be released on driver detach. 1966 * 1967 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded 1968 * error code on failure. 1969 */ 1970 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id) 1971 { 1972 struct pwm_device *pwm; 1973 int ret; 1974 1975 pwm = pwm_get(dev, con_id); 1976 if (IS_ERR(pwm)) 1977 return pwm; 1978 1979 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm); 1980 if (ret) 1981 return ERR_PTR(ret); 1982 1983 return pwm; 1984 } 1985 EXPORT_SYMBOL_GPL(devm_pwm_get); 1986 1987 /** 1988 * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node 1989 * @dev: device for PWM consumer 1990 * @fwnode: firmware node to get the PWM from 1991 * @con_id: consumer name 1992 * 1993 * Returns the PWM device parsed from the firmware node. See of_pwm_get() and 1994 * acpi_pwm_get() for a detailed description. 1995 * 1996 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded 1997 * error code on failure. 1998 */ 1999 struct pwm_device *devm_fwnode_pwm_get(struct device *dev, 2000 struct fwnode_handle *fwnode, 2001 const char *con_id) 2002 { 2003 struct pwm_device *pwm = ERR_PTR(-ENODEV); 2004 int ret; 2005 2006 if (is_of_node(fwnode)) 2007 pwm = of_pwm_get(dev, to_of_node(fwnode), con_id); 2008 else if (is_acpi_node(fwnode)) 2009 pwm = acpi_pwm_get(fwnode); 2010 if (IS_ERR(pwm)) 2011 return pwm; 2012 2013 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm); 2014 if (ret) 2015 return ERR_PTR(ret); 2016 2017 return pwm; 2018 } 2019 EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get); 2020 2021 /** 2022 * __pwmchip_add() - register a new PWM chip 2023 * @chip: the PWM chip to add 2024 * @owner: reference to the module providing the chip. 2025 * 2026 * Register a new PWM chip. @owner is supposed to be THIS_MODULE, use the 2027 * pwmchip_add wrapper to do this right. 2028 * 2029 * Returns: 0 on success or a negative error code on failure. 2030 */ 2031 int __pwmchip_add(struct pwm_chip *chip, struct module *owner) 2032 { 2033 int ret; 2034 2035 if (!chip || !pwmchip_parent(chip) || !chip->ops || !chip->npwm) 2036 return -EINVAL; 2037 2038 /* 2039 * a struct pwm_chip must be allocated using (devm_)pwmchip_alloc, 2040 * otherwise the embedded struct device might disappear too early 2041 * resulting in memory corruption. 2042 * Catch drivers that were not converted appropriately. 2043 */ 2044 if (!chip->uses_pwmchip_alloc) 2045 return -EINVAL; 2046 2047 if (!pwm_ops_check(chip)) 2048 return -EINVAL; 2049 2050 chip->owner = owner; 2051 2052 if (chip->atomic) 2053 spin_lock_init(&chip->atomic_lock); 2054 else 2055 mutex_init(&chip->nonatomic_lock); 2056 2057 guard(mutex)(&pwm_lock); 2058 2059 ret = idr_alloc(&pwm_chips, chip, 0, 0, GFP_KERNEL); 2060 if (ret < 0) 2061 return ret; 2062 2063 chip->id = ret; 2064 2065 dev_set_name(&chip->dev, "pwmchip%u", chip->id); 2066 2067 if (IS_ENABLED(CONFIG_OF)) 2068 of_pwmchip_add(chip); 2069 2070 scoped_guard(pwmchip, chip) 2071 chip->operational = true; 2072 2073 ret = device_add(&chip->dev); 2074 if (ret) 2075 goto err_device_add; 2076 2077 return 0; 2078 2079 err_device_add: 2080 scoped_guard(pwmchip, chip) 2081 chip->operational = false; 2082 2083 if (IS_ENABLED(CONFIG_OF)) 2084 of_pwmchip_remove(chip); 2085 2086 idr_remove(&pwm_chips, chip->id); 2087 2088 return ret; 2089 } 2090 EXPORT_SYMBOL_GPL(__pwmchip_add); 2091 2092 /** 2093 * pwmchip_remove() - remove a PWM chip 2094 * @chip: the PWM chip to remove 2095 * 2096 * Removes a PWM chip. 2097 */ 2098 void pwmchip_remove(struct pwm_chip *chip) 2099 { 2100 pwmchip_sysfs_unexport(chip); 2101 2102 scoped_guard(mutex, &pwm_lock) { 2103 unsigned int i; 2104 2105 scoped_guard(pwmchip, chip) 2106 chip->operational = false; 2107 2108 for (i = 0; i < chip->npwm; ++i) { 2109 struct pwm_device *pwm = &chip->pwms[i]; 2110 2111 if (test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) { 2112 dev_warn(&chip->dev, "Freeing requested PWM #%u\n", i); 2113 if (pwm->chip->ops->free) 2114 pwm->chip->ops->free(pwm->chip, pwm); 2115 } 2116 } 2117 2118 if (IS_ENABLED(CONFIG_OF)) 2119 of_pwmchip_remove(chip); 2120 2121 idr_remove(&pwm_chips, chip->id); 2122 } 2123 2124 device_del(&chip->dev); 2125 } 2126 EXPORT_SYMBOL_GPL(pwmchip_remove); 2127 2128 static void devm_pwmchip_remove(void *data) 2129 { 2130 struct pwm_chip *chip = data; 2131 2132 pwmchip_remove(chip); 2133 } 2134 2135 int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner) 2136 { 2137 int ret; 2138 2139 ret = __pwmchip_add(chip, owner); 2140 if (ret) 2141 return ret; 2142 2143 return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip); 2144 } 2145 EXPORT_SYMBOL_GPL(__devm_pwmchip_add); 2146 2147 /** 2148 * pwm_add_table() - register PWM device consumers 2149 * @table: array of consumers to register 2150 * @num: number of consumers in table 2151 */ 2152 void pwm_add_table(struct pwm_lookup *table, size_t num) 2153 { 2154 guard(mutex)(&pwm_lookup_lock); 2155 2156 while (num--) { 2157 list_add_tail(&table->list, &pwm_lookup_list); 2158 table++; 2159 } 2160 } 2161 2162 /** 2163 * pwm_remove_table() - unregister PWM device consumers 2164 * @table: array of consumers to unregister 2165 * @num: number of consumers in table 2166 */ 2167 void pwm_remove_table(struct pwm_lookup *table, size_t num) 2168 { 2169 guard(mutex)(&pwm_lookup_lock); 2170 2171 while (num--) { 2172 list_del(&table->list); 2173 table++; 2174 } 2175 } 2176 2177 static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s) 2178 { 2179 unsigned int i; 2180 2181 for (i = 0; i < chip->npwm; i++) { 2182 struct pwm_device *pwm = &chip->pwms[i]; 2183 struct pwm_state state; 2184 2185 pwm_get_state(pwm, &state); 2186 2187 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label); 2188 2189 if (test_bit(PWMF_REQUESTED, &pwm->flags)) 2190 seq_puts(s, " requested"); 2191 2192 if (state.enabled) 2193 seq_puts(s, " enabled"); 2194 2195 seq_printf(s, " period: %llu ns", state.period); 2196 seq_printf(s, " duty: %llu ns", state.duty_cycle); 2197 seq_printf(s, " polarity: %s", 2198 state.polarity ? "inverse" : "normal"); 2199 2200 if (state.usage_power) 2201 seq_puts(s, " usage_power"); 2202 2203 seq_puts(s, "\n"); 2204 } 2205 } 2206 2207 static void *pwm_seq_start(struct seq_file *s, loff_t *pos) 2208 { 2209 unsigned long id = *pos; 2210 void *ret; 2211 2212 mutex_lock(&pwm_lock); 2213 s->private = ""; 2214 2215 ret = idr_get_next_ul(&pwm_chips, &id); 2216 *pos = id; 2217 return ret; 2218 } 2219 2220 static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos) 2221 { 2222 unsigned long id = *pos + 1; 2223 void *ret; 2224 2225 s->private = "\n"; 2226 2227 ret = idr_get_next_ul(&pwm_chips, &id); 2228 *pos = id; 2229 return ret; 2230 } 2231 2232 static void pwm_seq_stop(struct seq_file *s, void *v) 2233 { 2234 mutex_unlock(&pwm_lock); 2235 } 2236 2237 static int pwm_seq_show(struct seq_file *s, void *v) 2238 { 2239 struct pwm_chip *chip = v; 2240 2241 seq_printf(s, "%s%d: %s/%s, %d PWM device%s\n", 2242 (char *)s->private, chip->id, 2243 pwmchip_parent(chip)->bus ? pwmchip_parent(chip)->bus->name : "no-bus", 2244 dev_name(pwmchip_parent(chip)), chip->npwm, 2245 (chip->npwm != 1) ? "s" : ""); 2246 2247 pwm_dbg_show(chip, s); 2248 2249 return 0; 2250 } 2251 2252 static const struct seq_operations pwm_debugfs_sops = { 2253 .start = pwm_seq_start, 2254 .next = pwm_seq_next, 2255 .stop = pwm_seq_stop, 2256 .show = pwm_seq_show, 2257 }; 2258 2259 DEFINE_SEQ_ATTRIBUTE(pwm_debugfs); 2260 2261 static int __init pwm_init(void) 2262 { 2263 int ret; 2264 2265 ret = class_register(&pwm_class); 2266 if (ret) { 2267 pr_err("Failed to initialize PWM class (%pe)\n", ERR_PTR(ret)); 2268 return ret; 2269 } 2270 2271 if (IS_ENABLED(CONFIG_DEBUG_FS)) 2272 debugfs_create_file("pwm", 0444, NULL, NULL, &pwm_debugfs_fops); 2273 2274 return 0; 2275 } 2276 subsys_initcall(pwm_init); 2277