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