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