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 pwm_apply_debug(struct pwm_device *pwm, 35 const struct pwm_state *state) 36 { 37 struct pwm_state *last = &pwm->last; 38 struct pwm_chip *chip = pwm->chip; 39 struct pwm_state s1 = { 0 }, s2 = { 0 }; 40 int err; 41 42 if (!IS_ENABLED(CONFIG_PWM_DEBUG)) 43 return; 44 45 /* No reasonable diagnosis possible without .get_state() */ 46 if (!chip->ops->get_state) 47 return; 48 49 /* 50 * *state was just applied. Read out the hardware state and do some 51 * checks. 52 */ 53 54 err = chip->ops->get_state(chip, pwm, &s1); 55 trace_pwm_get(pwm, &s1, err); 56 if (err) 57 /* If that failed there isn't much to debug */ 58 return; 59 60 /* 61 * The lowlevel driver either ignored .polarity (which is a bug) or as 62 * best effort inverted .polarity and fixed .duty_cycle respectively. 63 * Undo this inversion and fixup for further tests. 64 */ 65 if (s1.enabled && s1.polarity != state->polarity) { 66 s2.polarity = state->polarity; 67 s2.duty_cycle = s1.period - s1.duty_cycle; 68 s2.period = s1.period; 69 s2.enabled = s1.enabled; 70 } else { 71 s2 = s1; 72 } 73 74 if (s2.polarity != state->polarity && 75 state->duty_cycle < state->period) 76 dev_warn(pwmchip_parent(chip), ".apply ignored .polarity\n"); 77 78 if (state->enabled && 79 last->polarity == state->polarity && 80 last->period > s2.period && 81 last->period <= state->period) 82 dev_warn(pwmchip_parent(chip), 83 ".apply didn't pick the best available period (requested: %llu, applied: %llu, possible: %llu)\n", 84 state->period, s2.period, last->period); 85 86 if (state->enabled && state->period < s2.period) 87 dev_warn(pwmchip_parent(chip), 88 ".apply is supposed to round down period (requested: %llu, applied: %llu)\n", 89 state->period, s2.period); 90 91 if (state->enabled && 92 last->polarity == state->polarity && 93 last->period == s2.period && 94 last->duty_cycle > s2.duty_cycle && 95 last->duty_cycle <= state->duty_cycle) 96 dev_warn(pwmchip_parent(chip), 97 ".apply didn't pick the best available duty cycle (requested: %llu/%llu, applied: %llu/%llu, possible: %llu/%llu)\n", 98 state->duty_cycle, state->period, 99 s2.duty_cycle, s2.period, 100 last->duty_cycle, last->period); 101 102 if (state->enabled && state->duty_cycle < s2.duty_cycle) 103 dev_warn(pwmchip_parent(chip), 104 ".apply is supposed to round down duty_cycle (requested: %llu/%llu, applied: %llu/%llu)\n", 105 state->duty_cycle, state->period, 106 s2.duty_cycle, s2.period); 107 108 if (!state->enabled && s2.enabled && s2.duty_cycle > 0) 109 dev_warn(pwmchip_parent(chip), 110 "requested disabled, but yielded enabled with duty > 0\n"); 111 112 /* reapply the state that the driver reported being configured. */ 113 err = chip->ops->apply(chip, pwm, &s1); 114 trace_pwm_apply(pwm, &s1, err); 115 if (err) { 116 *last = s1; 117 dev_err(pwmchip_parent(chip), "failed to reapply current setting\n"); 118 return; 119 } 120 121 *last = (struct pwm_state){ 0 }; 122 err = chip->ops->get_state(chip, pwm, last); 123 trace_pwm_get(pwm, last, err); 124 if (err) 125 return; 126 127 /* reapplication of the current state should give an exact match */ 128 if (s1.enabled != last->enabled || 129 s1.polarity != last->polarity || 130 (s1.enabled && s1.period != last->period) || 131 (s1.enabled && s1.duty_cycle != last->duty_cycle)) { 132 dev_err(pwmchip_parent(chip), 133 ".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n", 134 s1.enabled, s1.polarity, s1.duty_cycle, s1.period, 135 last->enabled, last->polarity, last->duty_cycle, 136 last->period); 137 } 138 } 139 140 static bool pwm_state_valid(const struct pwm_state *state) 141 { 142 /* 143 * For a disabled state all other state description is irrelevant and 144 * and supposed to be ignored. So also ignore any strange values and 145 * consider the state ok. 146 */ 147 if (state->enabled) 148 return true; 149 150 if (!state->period) 151 return false; 152 153 if (state->duty_cycle > state->period) 154 return false; 155 156 return true; 157 } 158 159 /** 160 * __pwm_apply() - atomically apply a new state to a PWM device 161 * @pwm: PWM device 162 * @state: new state to apply 163 */ 164 static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state) 165 { 166 struct pwm_chip *chip; 167 int err; 168 169 if (!pwm || !state) 170 return -EINVAL; 171 172 if (!pwm_state_valid(state)) { 173 /* 174 * Allow to transition from one invalid state to another. 175 * This ensures that you can e.g. change the polarity while 176 * the period is zero. (This happens on stm32 when the hardware 177 * is in its poweron default state.) This greatly simplifies 178 * working with the sysfs API where you can only change one 179 * parameter at a time. 180 */ 181 if (!pwm_state_valid(&pwm->state)) { 182 pwm->state = *state; 183 return 0; 184 } 185 186 return -EINVAL; 187 } 188 189 chip = pwm->chip; 190 191 if (state->period == pwm->state.period && 192 state->duty_cycle == pwm->state.duty_cycle && 193 state->polarity == pwm->state.polarity && 194 state->enabled == pwm->state.enabled && 195 state->usage_power == pwm->state.usage_power) 196 return 0; 197 198 err = chip->ops->apply(chip, pwm, state); 199 trace_pwm_apply(pwm, state, err); 200 if (err) 201 return err; 202 203 pwm->state = *state; 204 205 /* 206 * only do this after pwm->state was applied as some 207 * implementations of .get_state depend on this 208 */ 209 pwm_apply_debug(pwm, state); 210 211 return 0; 212 } 213 214 /** 215 * pwm_apply_might_sleep() - atomically apply a new state to a PWM device 216 * Cannot be used in atomic context. 217 * @pwm: PWM device 218 * @state: new state to apply 219 */ 220 int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state) 221 { 222 int err; 223 224 /* 225 * Some lowlevel driver's implementations of .apply() make use of 226 * mutexes, also with some drivers only returning when the new 227 * configuration is active calling pwm_apply_might_sleep() from atomic context 228 * is a bad idea. So make it explicit that calling this function might 229 * sleep. 230 */ 231 might_sleep(); 232 233 if (IS_ENABLED(CONFIG_PWM_DEBUG) && pwm->chip->atomic) { 234 /* 235 * Catch any drivers that have been marked as atomic but 236 * that will sleep anyway. 237 */ 238 non_block_start(); 239 err = __pwm_apply(pwm, state); 240 non_block_end(); 241 } else { 242 err = __pwm_apply(pwm, state); 243 } 244 245 return err; 246 } 247 EXPORT_SYMBOL_GPL(pwm_apply_might_sleep); 248 249 /** 250 * pwm_apply_atomic() - apply a new state to a PWM device from atomic context 251 * Not all PWM devices support this function, check with pwm_might_sleep(). 252 * @pwm: PWM device 253 * @state: new state to apply 254 */ 255 int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state) 256 { 257 WARN_ONCE(!pwm->chip->atomic, 258 "sleeping PWM driver used in atomic context\n"); 259 260 return __pwm_apply(pwm, state); 261 } 262 EXPORT_SYMBOL_GPL(pwm_apply_atomic); 263 264 /** 265 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments 266 * @pwm: PWM device 267 * 268 * This function will adjust the PWM config to the PWM arguments provided 269 * by the DT or PWM lookup table. This is particularly useful to adapt 270 * the bootloader config to the Linux one. 271 */ 272 int pwm_adjust_config(struct pwm_device *pwm) 273 { 274 struct pwm_state state; 275 struct pwm_args pargs; 276 277 pwm_get_args(pwm, &pargs); 278 pwm_get_state(pwm, &state); 279 280 /* 281 * If the current period is zero it means that either the PWM driver 282 * does not support initial state retrieval or the PWM has not yet 283 * been configured. 284 * 285 * In either case, we setup the new period and polarity, and assign a 286 * duty cycle of 0. 287 */ 288 if (!state.period) { 289 state.duty_cycle = 0; 290 state.period = pargs.period; 291 state.polarity = pargs.polarity; 292 293 return pwm_apply_might_sleep(pwm, &state); 294 } 295 296 /* 297 * Adjust the PWM duty cycle/period based on the period value provided 298 * in PWM args. 299 */ 300 if (pargs.period != state.period) { 301 u64 dutycycle = (u64)state.duty_cycle * pargs.period; 302 303 do_div(dutycycle, state.period); 304 state.duty_cycle = dutycycle; 305 state.period = pargs.period; 306 } 307 308 /* 309 * If the polarity changed, we should also change the duty cycle. 310 */ 311 if (pargs.polarity != state.polarity) { 312 state.polarity = pargs.polarity; 313 state.duty_cycle = state.period - state.duty_cycle; 314 } 315 316 return pwm_apply_might_sleep(pwm, &state); 317 } 318 EXPORT_SYMBOL_GPL(pwm_adjust_config); 319 320 /** 321 * pwm_capture() - capture and report a PWM signal 322 * @pwm: PWM device 323 * @result: structure to fill with capture result 324 * @timeout: time to wait, in milliseconds, before giving up on capture 325 * 326 * Returns: 0 on success or a negative error code on failure. 327 */ 328 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, 329 unsigned long timeout) 330 { 331 if (!pwm || !pwm->chip->ops) 332 return -EINVAL; 333 334 if (!pwm->chip->ops->capture) 335 return -ENOSYS; 336 337 guard(mutex)(&pwm_lock); 338 339 return pwm->chip->ops->capture(pwm->chip, pwm, result, timeout); 340 } 341 EXPORT_SYMBOL_GPL(pwm_capture); 342 343 static struct pwm_chip *pwmchip_find_by_name(const char *name) 344 { 345 struct pwm_chip *chip; 346 unsigned long id, tmp; 347 348 if (!name) 349 return NULL; 350 351 guard(mutex)(&pwm_lock); 352 353 idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) { 354 const char *chip_name = dev_name(pwmchip_parent(chip)); 355 356 if (chip_name && strcmp(chip_name, name) == 0) 357 return chip; 358 } 359 360 return NULL; 361 } 362 363 static int pwm_device_request(struct pwm_device *pwm, const char *label) 364 { 365 int err; 366 struct pwm_chip *chip = pwm->chip; 367 const struct pwm_ops *ops = chip->ops; 368 369 if (test_bit(PWMF_REQUESTED, &pwm->flags)) 370 return -EBUSY; 371 372 if (!try_module_get(chip->owner)) 373 return -ENODEV; 374 375 if (!get_device(&chip->dev)) { 376 err = -ENODEV; 377 goto err_get_device; 378 } 379 380 if (ops->request) { 381 err = ops->request(chip, pwm); 382 if (err) { 383 put_device(&chip->dev); 384 err_get_device: 385 module_put(chip->owner); 386 return err; 387 } 388 } 389 390 if (ops->get_state) { 391 /* 392 * Zero-initialize state because most drivers are unaware of 393 * .usage_power. The other members of state are supposed to be 394 * set by lowlevel drivers. We still initialize the whole 395 * structure for simplicity even though this might paper over 396 * faulty implementations of .get_state(). 397 */ 398 struct pwm_state state = { 0, }; 399 400 err = ops->get_state(chip, pwm, &state); 401 trace_pwm_get(pwm, &state, err); 402 403 if (!err) 404 pwm->state = state; 405 406 if (IS_ENABLED(CONFIG_PWM_DEBUG)) 407 pwm->last = pwm->state; 408 } 409 410 set_bit(PWMF_REQUESTED, &pwm->flags); 411 pwm->label = label; 412 413 return 0; 414 } 415 416 /** 417 * pwm_request_from_chip() - request a PWM device relative to a PWM chip 418 * @chip: PWM chip 419 * @index: per-chip index of the PWM to request 420 * @label: a literal description string of this PWM 421 * 422 * Returns: A pointer to the PWM device at the given index of the given PWM 423 * chip. A negative error code is returned if the index is not valid for the 424 * specified PWM chip or if the PWM device cannot be requested. 425 */ 426 static struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, 427 unsigned int index, 428 const char *label) 429 { 430 struct pwm_device *pwm; 431 int err; 432 433 if (!chip || index >= chip->npwm) 434 return ERR_PTR(-EINVAL); 435 436 guard(mutex)(&pwm_lock); 437 438 pwm = &chip->pwms[index]; 439 440 err = pwm_device_request(pwm, label); 441 if (err < 0) 442 return ERR_PTR(err); 443 444 return pwm; 445 } 446 447 struct pwm_device * 448 of_pwm_xlate_with_flags(struct pwm_chip *chip, const struct of_phandle_args *args) 449 { 450 struct pwm_device *pwm; 451 452 /* period in the second cell and flags in the third cell are optional */ 453 if (args->args_count < 1) 454 return ERR_PTR(-EINVAL); 455 456 pwm = pwm_request_from_chip(chip, args->args[0], NULL); 457 if (IS_ERR(pwm)) 458 return pwm; 459 460 if (args->args_count > 1) 461 pwm->args.period = args->args[1]; 462 463 pwm->args.polarity = PWM_POLARITY_NORMAL; 464 if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED) 465 pwm->args.polarity = PWM_POLARITY_INVERSED; 466 467 return pwm; 468 } 469 EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags); 470 471 struct pwm_device * 472 of_pwm_single_xlate(struct pwm_chip *chip, const struct of_phandle_args *args) 473 { 474 struct pwm_device *pwm; 475 476 pwm = pwm_request_from_chip(chip, 0, NULL); 477 if (IS_ERR(pwm)) 478 return pwm; 479 480 if (args->args_count > 0) 481 pwm->args.period = args->args[0]; 482 483 pwm->args.polarity = PWM_POLARITY_NORMAL; 484 if (args->args_count > 1 && args->args[1] & PWM_POLARITY_INVERTED) 485 pwm->args.polarity = PWM_POLARITY_INVERSED; 486 487 return pwm; 488 } 489 EXPORT_SYMBOL_GPL(of_pwm_single_xlate); 490 491 struct pwm_export { 492 struct device pwm_dev; 493 struct pwm_device *pwm; 494 struct mutex lock; 495 struct pwm_state suspend; 496 }; 497 498 static inline struct pwm_chip *pwmchip_from_dev(struct device *pwmchip_dev) 499 { 500 return container_of(pwmchip_dev, struct pwm_chip, dev); 501 } 502 503 static inline struct pwm_export *pwmexport_from_dev(struct device *pwm_dev) 504 { 505 return container_of(pwm_dev, struct pwm_export, pwm_dev); 506 } 507 508 static inline struct pwm_device *pwm_from_dev(struct device *pwm_dev) 509 { 510 struct pwm_export *export = pwmexport_from_dev(pwm_dev); 511 512 return export->pwm; 513 } 514 515 static ssize_t period_show(struct device *pwm_dev, 516 struct device_attribute *attr, 517 char *buf) 518 { 519 const struct pwm_device *pwm = pwm_from_dev(pwm_dev); 520 struct pwm_state state; 521 522 pwm_get_state(pwm, &state); 523 524 return sysfs_emit(buf, "%llu\n", state.period); 525 } 526 527 static ssize_t period_store(struct device *pwm_dev, 528 struct device_attribute *attr, 529 const char *buf, size_t size) 530 { 531 struct pwm_export *export = pwmexport_from_dev(pwm_dev); 532 struct pwm_device *pwm = export->pwm; 533 struct pwm_state state; 534 u64 val; 535 int ret; 536 537 ret = kstrtou64(buf, 0, &val); 538 if (ret) 539 return ret; 540 541 guard(mutex)(&export->lock); 542 543 pwm_get_state(pwm, &state); 544 state.period = val; 545 ret = pwm_apply_might_sleep(pwm, &state); 546 547 return ret ? : size; 548 } 549 550 static ssize_t duty_cycle_show(struct device *pwm_dev, 551 struct device_attribute *attr, 552 char *buf) 553 { 554 const struct pwm_device *pwm = pwm_from_dev(pwm_dev); 555 struct pwm_state state; 556 557 pwm_get_state(pwm, &state); 558 559 return sysfs_emit(buf, "%llu\n", state.duty_cycle); 560 } 561 562 static ssize_t duty_cycle_store(struct device *pwm_dev, 563 struct device_attribute *attr, 564 const char *buf, size_t size) 565 { 566 struct pwm_export *export = pwmexport_from_dev(pwm_dev); 567 struct pwm_device *pwm = export->pwm; 568 struct pwm_state state; 569 u64 val; 570 int ret; 571 572 ret = kstrtou64(buf, 0, &val); 573 if (ret) 574 return ret; 575 576 guard(mutex)(&export->lock); 577 578 pwm_get_state(pwm, &state); 579 state.duty_cycle = val; 580 ret = pwm_apply_might_sleep(pwm, &state); 581 582 return ret ? : size; 583 } 584 585 static ssize_t enable_show(struct device *pwm_dev, 586 struct device_attribute *attr, 587 char *buf) 588 { 589 const struct pwm_device *pwm = pwm_from_dev(pwm_dev); 590 struct pwm_state state; 591 592 pwm_get_state(pwm, &state); 593 594 return sysfs_emit(buf, "%d\n", state.enabled); 595 } 596 597 static ssize_t enable_store(struct device *pwm_dev, 598 struct device_attribute *attr, 599 const char *buf, size_t size) 600 { 601 struct pwm_export *export = pwmexport_from_dev(pwm_dev); 602 struct pwm_device *pwm = export->pwm; 603 struct pwm_state state; 604 int val, ret; 605 606 ret = kstrtoint(buf, 0, &val); 607 if (ret) 608 return ret; 609 610 guard(mutex)(&export->lock); 611 612 pwm_get_state(pwm, &state); 613 614 switch (val) { 615 case 0: 616 state.enabled = false; 617 break; 618 case 1: 619 state.enabled = true; 620 break; 621 default: 622 return -EINVAL; 623 } 624 625 ret = pwm_apply_might_sleep(pwm, &state); 626 627 return ret ? : size; 628 } 629 630 static ssize_t polarity_show(struct device *pwm_dev, 631 struct device_attribute *attr, 632 char *buf) 633 { 634 const struct pwm_device *pwm = pwm_from_dev(pwm_dev); 635 const char *polarity = "unknown"; 636 struct pwm_state state; 637 638 pwm_get_state(pwm, &state); 639 640 switch (state.polarity) { 641 case PWM_POLARITY_NORMAL: 642 polarity = "normal"; 643 break; 644 645 case PWM_POLARITY_INVERSED: 646 polarity = "inversed"; 647 break; 648 } 649 650 return sysfs_emit(buf, "%s\n", polarity); 651 } 652 653 static ssize_t polarity_store(struct device *pwm_dev, 654 struct device_attribute *attr, 655 const char *buf, size_t size) 656 { 657 struct pwm_export *export = pwmexport_from_dev(pwm_dev); 658 struct pwm_device *pwm = export->pwm; 659 enum pwm_polarity polarity; 660 struct pwm_state state; 661 int ret; 662 663 if (sysfs_streq(buf, "normal")) 664 polarity = PWM_POLARITY_NORMAL; 665 else if (sysfs_streq(buf, "inversed")) 666 polarity = PWM_POLARITY_INVERSED; 667 else 668 return -EINVAL; 669 670 guard(mutex)(&export->lock); 671 672 pwm_get_state(pwm, &state); 673 state.polarity = polarity; 674 ret = pwm_apply_might_sleep(pwm, &state); 675 676 return ret ? : size; 677 } 678 679 static ssize_t capture_show(struct device *pwm_dev, 680 struct device_attribute *attr, 681 char *buf) 682 { 683 struct pwm_device *pwm = pwm_from_dev(pwm_dev); 684 struct pwm_capture result; 685 int ret; 686 687 ret = pwm_capture(pwm, &result, jiffies_to_msecs(HZ)); 688 if (ret) 689 return ret; 690 691 return sysfs_emit(buf, "%u %u\n", result.period, result.duty_cycle); 692 } 693 694 static DEVICE_ATTR_RW(period); 695 static DEVICE_ATTR_RW(duty_cycle); 696 static DEVICE_ATTR_RW(enable); 697 static DEVICE_ATTR_RW(polarity); 698 static DEVICE_ATTR_RO(capture); 699 700 static struct attribute *pwm_attrs[] = { 701 &dev_attr_period.attr, 702 &dev_attr_duty_cycle.attr, 703 &dev_attr_enable.attr, 704 &dev_attr_polarity.attr, 705 &dev_attr_capture.attr, 706 NULL 707 }; 708 ATTRIBUTE_GROUPS(pwm); 709 710 static void pwm_export_release(struct device *pwm_dev) 711 { 712 struct pwm_export *export = pwmexport_from_dev(pwm_dev); 713 714 kfree(export); 715 } 716 717 static int pwm_export_child(struct device *pwmchip_dev, struct pwm_device *pwm) 718 { 719 struct pwm_export *export; 720 char *pwm_prop[2]; 721 int ret; 722 723 if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags)) 724 return -EBUSY; 725 726 export = kzalloc(sizeof(*export), GFP_KERNEL); 727 if (!export) { 728 clear_bit(PWMF_EXPORTED, &pwm->flags); 729 return -ENOMEM; 730 } 731 732 export->pwm = pwm; 733 mutex_init(&export->lock); 734 735 export->pwm_dev.release = pwm_export_release; 736 export->pwm_dev.parent = pwmchip_dev; 737 export->pwm_dev.devt = MKDEV(0, 0); 738 export->pwm_dev.groups = pwm_groups; 739 dev_set_name(&export->pwm_dev, "pwm%u", pwm->hwpwm); 740 741 ret = device_register(&export->pwm_dev); 742 if (ret) { 743 clear_bit(PWMF_EXPORTED, &pwm->flags); 744 put_device(&export->pwm_dev); 745 export = NULL; 746 return ret; 747 } 748 pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm); 749 pwm_prop[1] = NULL; 750 kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop); 751 kfree(pwm_prop[0]); 752 753 return 0; 754 } 755 756 static int pwm_unexport_match(struct device *pwm_dev, void *data) 757 { 758 return pwm_from_dev(pwm_dev) == data; 759 } 760 761 static int pwm_unexport_child(struct device *pwmchip_dev, struct pwm_device *pwm) 762 { 763 struct device *pwm_dev; 764 char *pwm_prop[2]; 765 766 if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags)) 767 return -ENODEV; 768 769 pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match); 770 if (!pwm_dev) 771 return -ENODEV; 772 773 pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm); 774 pwm_prop[1] = NULL; 775 kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop); 776 kfree(pwm_prop[0]); 777 778 /* for device_find_child() */ 779 put_device(pwm_dev); 780 device_unregister(pwm_dev); 781 pwm_put(pwm); 782 783 return 0; 784 } 785 786 static ssize_t export_store(struct device *pwmchip_dev, 787 struct device_attribute *attr, 788 const char *buf, size_t len) 789 { 790 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev); 791 struct pwm_device *pwm; 792 unsigned int hwpwm; 793 int ret; 794 795 ret = kstrtouint(buf, 0, &hwpwm); 796 if (ret < 0) 797 return ret; 798 799 if (hwpwm >= chip->npwm) 800 return -ENODEV; 801 802 pwm = pwm_request_from_chip(chip, hwpwm, "sysfs"); 803 if (IS_ERR(pwm)) 804 return PTR_ERR(pwm); 805 806 ret = pwm_export_child(pwmchip_dev, pwm); 807 if (ret < 0) 808 pwm_put(pwm); 809 810 return ret ? : len; 811 } 812 static DEVICE_ATTR_WO(export); 813 814 static ssize_t unexport_store(struct device *pwmchip_dev, 815 struct device_attribute *attr, 816 const char *buf, size_t len) 817 { 818 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev); 819 unsigned int hwpwm; 820 int ret; 821 822 ret = kstrtouint(buf, 0, &hwpwm); 823 if (ret < 0) 824 return ret; 825 826 if (hwpwm >= chip->npwm) 827 return -ENODEV; 828 829 ret = pwm_unexport_child(pwmchip_dev, &chip->pwms[hwpwm]); 830 831 return ret ? : len; 832 } 833 static DEVICE_ATTR_WO(unexport); 834 835 static ssize_t npwm_show(struct device *pwmchip_dev, struct device_attribute *attr, 836 char *buf) 837 { 838 const struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev); 839 840 return sysfs_emit(buf, "%u\n", chip->npwm); 841 } 842 static DEVICE_ATTR_RO(npwm); 843 844 static struct attribute *pwm_chip_attrs[] = { 845 &dev_attr_export.attr, 846 &dev_attr_unexport.attr, 847 &dev_attr_npwm.attr, 848 NULL, 849 }; 850 ATTRIBUTE_GROUPS(pwm_chip); 851 852 /* takes export->lock on success */ 853 static struct pwm_export *pwm_class_get_state(struct device *pwmchip_dev, 854 struct pwm_device *pwm, 855 struct pwm_state *state) 856 { 857 struct device *pwm_dev; 858 struct pwm_export *export; 859 860 if (!test_bit(PWMF_EXPORTED, &pwm->flags)) 861 return NULL; 862 863 pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match); 864 if (!pwm_dev) 865 return NULL; 866 867 export = pwmexport_from_dev(pwm_dev); 868 put_device(pwm_dev); /* for device_find_child() */ 869 870 mutex_lock(&export->lock); 871 pwm_get_state(pwm, state); 872 873 return export; 874 } 875 876 static int pwm_class_apply_state(struct pwm_export *export, 877 struct pwm_device *pwm, 878 struct pwm_state *state) 879 { 880 int ret = pwm_apply_might_sleep(pwm, state); 881 882 /* release lock taken in pwm_class_get_state */ 883 mutex_unlock(&export->lock); 884 885 return ret; 886 } 887 888 static int pwm_class_resume_npwm(struct device *pwmchip_dev, unsigned int npwm) 889 { 890 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev); 891 unsigned int i; 892 int ret = 0; 893 894 for (i = 0; i < npwm; i++) { 895 struct pwm_device *pwm = &chip->pwms[i]; 896 struct pwm_state state; 897 struct pwm_export *export; 898 899 export = pwm_class_get_state(pwmchip_dev, pwm, &state); 900 if (!export) 901 continue; 902 903 /* If pwmchip was not enabled before suspend, do nothing. */ 904 if (!export->suspend.enabled) { 905 /* release lock taken in pwm_class_get_state */ 906 mutex_unlock(&export->lock); 907 continue; 908 } 909 910 state.enabled = export->suspend.enabled; 911 ret = pwm_class_apply_state(export, pwm, &state); 912 if (ret < 0) 913 break; 914 } 915 916 return ret; 917 } 918 919 static int pwm_class_suspend(struct device *pwmchip_dev) 920 { 921 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev); 922 unsigned int i; 923 int ret = 0; 924 925 for (i = 0; i < chip->npwm; i++) { 926 struct pwm_device *pwm = &chip->pwms[i]; 927 struct pwm_state state; 928 struct pwm_export *export; 929 930 export = pwm_class_get_state(pwmchip_dev, pwm, &state); 931 if (!export) 932 continue; 933 934 /* 935 * If pwmchip was not enabled before suspend, save 936 * state for resume time and do nothing else. 937 */ 938 export->suspend = state; 939 if (!state.enabled) { 940 /* release lock taken in pwm_class_get_state */ 941 mutex_unlock(&export->lock); 942 continue; 943 } 944 945 state.enabled = false; 946 ret = pwm_class_apply_state(export, pwm, &state); 947 if (ret < 0) { 948 /* 949 * roll back the PWM devices that were disabled by 950 * this suspend function. 951 */ 952 pwm_class_resume_npwm(pwmchip_dev, i); 953 break; 954 } 955 } 956 957 return ret; 958 } 959 960 static int pwm_class_resume(struct device *pwmchip_dev) 961 { 962 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev); 963 964 return pwm_class_resume_npwm(pwmchip_dev, chip->npwm); 965 } 966 967 static DEFINE_SIMPLE_DEV_PM_OPS(pwm_class_pm_ops, pwm_class_suspend, pwm_class_resume); 968 969 static struct class pwm_class = { 970 .name = "pwm", 971 .dev_groups = pwm_chip_groups, 972 .pm = pm_sleep_ptr(&pwm_class_pm_ops), 973 }; 974 975 static void pwmchip_sysfs_unexport(struct pwm_chip *chip) 976 { 977 unsigned int i; 978 979 for (i = 0; i < chip->npwm; i++) { 980 struct pwm_device *pwm = &chip->pwms[i]; 981 982 if (test_bit(PWMF_EXPORTED, &pwm->flags)) 983 pwm_unexport_child(&chip->dev, pwm); 984 } 985 } 986 987 #define PWMCHIP_ALIGN ARCH_DMA_MINALIGN 988 989 static void *pwmchip_priv(struct pwm_chip *chip) 990 { 991 return (void *)chip + ALIGN(struct_size(chip, pwms, chip->npwm), PWMCHIP_ALIGN); 992 } 993 994 /* This is the counterpart to pwmchip_alloc() */ 995 void pwmchip_put(struct pwm_chip *chip) 996 { 997 put_device(&chip->dev); 998 } 999 EXPORT_SYMBOL_GPL(pwmchip_put); 1000 1001 static void pwmchip_release(struct device *pwmchip_dev) 1002 { 1003 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev); 1004 1005 kfree(chip); 1006 } 1007 1008 struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv) 1009 { 1010 struct pwm_chip *chip; 1011 struct device *pwmchip_dev; 1012 size_t alloc_size; 1013 unsigned int i; 1014 1015 alloc_size = size_add(ALIGN(struct_size(chip, pwms, npwm), PWMCHIP_ALIGN), 1016 sizeof_priv); 1017 1018 chip = kzalloc(alloc_size, GFP_KERNEL); 1019 if (!chip) 1020 return ERR_PTR(-ENOMEM); 1021 1022 chip->npwm = npwm; 1023 chip->uses_pwmchip_alloc = true; 1024 1025 pwmchip_dev = &chip->dev; 1026 device_initialize(pwmchip_dev); 1027 pwmchip_dev->class = &pwm_class; 1028 pwmchip_dev->parent = parent; 1029 pwmchip_dev->release = pwmchip_release; 1030 1031 pwmchip_set_drvdata(chip, pwmchip_priv(chip)); 1032 1033 for (i = 0; i < chip->npwm; i++) { 1034 struct pwm_device *pwm = &chip->pwms[i]; 1035 pwm->chip = chip; 1036 pwm->hwpwm = i; 1037 } 1038 1039 return chip; 1040 } 1041 EXPORT_SYMBOL_GPL(pwmchip_alloc); 1042 1043 static void devm_pwmchip_put(void *data) 1044 { 1045 struct pwm_chip *chip = data; 1046 1047 pwmchip_put(chip); 1048 } 1049 1050 struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv) 1051 { 1052 struct pwm_chip *chip; 1053 int ret; 1054 1055 chip = pwmchip_alloc(parent, npwm, sizeof_priv); 1056 if (IS_ERR(chip)) 1057 return chip; 1058 1059 ret = devm_add_action_or_reset(parent, devm_pwmchip_put, chip); 1060 if (ret) 1061 return ERR_PTR(ret); 1062 1063 return chip; 1064 } 1065 EXPORT_SYMBOL_GPL(devm_pwmchip_alloc); 1066 1067 static void of_pwmchip_add(struct pwm_chip *chip) 1068 { 1069 if (!pwmchip_parent(chip) || !pwmchip_parent(chip)->of_node) 1070 return; 1071 1072 if (!chip->of_xlate) 1073 chip->of_xlate = of_pwm_xlate_with_flags; 1074 1075 of_node_get(pwmchip_parent(chip)->of_node); 1076 } 1077 1078 static void of_pwmchip_remove(struct pwm_chip *chip) 1079 { 1080 if (pwmchip_parent(chip)) 1081 of_node_put(pwmchip_parent(chip)->of_node); 1082 } 1083 1084 static bool pwm_ops_check(const struct pwm_chip *chip) 1085 { 1086 const struct pwm_ops *ops = chip->ops; 1087 1088 if (!ops->apply) 1089 return false; 1090 1091 if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state) 1092 dev_warn(pwmchip_parent(chip), 1093 "Please implement the .get_state() callback\n"); 1094 1095 return true; 1096 } 1097 1098 /** 1099 * __pwmchip_add() - register a new PWM chip 1100 * @chip: the PWM chip to add 1101 * @owner: reference to the module providing the chip. 1102 * 1103 * Register a new PWM chip. @owner is supposed to be THIS_MODULE, use the 1104 * pwmchip_add wrapper to do this right. 1105 * 1106 * Returns: 0 on success or a negative error code on failure. 1107 */ 1108 int __pwmchip_add(struct pwm_chip *chip, struct module *owner) 1109 { 1110 int ret; 1111 1112 if (!chip || !pwmchip_parent(chip) || !chip->ops || !chip->npwm) 1113 return -EINVAL; 1114 1115 /* 1116 * a struct pwm_chip must be allocated using (devm_)pwmchip_alloc, 1117 * otherwise the embedded struct device might disappear too early 1118 * resulting in memory corruption. 1119 * Catch drivers that were not converted appropriately. 1120 */ 1121 if (!chip->uses_pwmchip_alloc) 1122 return -EINVAL; 1123 1124 if (!pwm_ops_check(chip)) 1125 return -EINVAL; 1126 1127 chip->owner = owner; 1128 1129 guard(mutex)(&pwm_lock); 1130 1131 ret = idr_alloc(&pwm_chips, chip, 0, 0, GFP_KERNEL); 1132 if (ret < 0) 1133 return ret; 1134 1135 chip->id = ret; 1136 1137 dev_set_name(&chip->dev, "pwmchip%u", chip->id); 1138 1139 if (IS_ENABLED(CONFIG_OF)) 1140 of_pwmchip_add(chip); 1141 1142 ret = device_add(&chip->dev); 1143 if (ret) 1144 goto err_device_add; 1145 1146 return 0; 1147 1148 err_device_add: 1149 if (IS_ENABLED(CONFIG_OF)) 1150 of_pwmchip_remove(chip); 1151 1152 idr_remove(&pwm_chips, chip->id); 1153 1154 return ret; 1155 } 1156 EXPORT_SYMBOL_GPL(__pwmchip_add); 1157 1158 /** 1159 * pwmchip_remove() - remove a PWM chip 1160 * @chip: the PWM chip to remove 1161 * 1162 * Removes a PWM chip. 1163 */ 1164 void pwmchip_remove(struct pwm_chip *chip) 1165 { 1166 pwmchip_sysfs_unexport(chip); 1167 1168 if (IS_ENABLED(CONFIG_OF)) 1169 of_pwmchip_remove(chip); 1170 1171 scoped_guard(mutex, &pwm_lock) 1172 idr_remove(&pwm_chips, chip->id); 1173 1174 device_del(&chip->dev); 1175 } 1176 EXPORT_SYMBOL_GPL(pwmchip_remove); 1177 1178 static void devm_pwmchip_remove(void *data) 1179 { 1180 struct pwm_chip *chip = data; 1181 1182 pwmchip_remove(chip); 1183 } 1184 1185 int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner) 1186 { 1187 int ret; 1188 1189 ret = __pwmchip_add(chip, owner); 1190 if (ret) 1191 return ret; 1192 1193 return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip); 1194 } 1195 EXPORT_SYMBOL_GPL(__devm_pwmchip_add); 1196 1197 static struct device_link *pwm_device_link_add(struct device *dev, 1198 struct pwm_device *pwm) 1199 { 1200 struct device_link *dl; 1201 1202 if (!dev) { 1203 /* 1204 * No device for the PWM consumer has been provided. It may 1205 * impact the PM sequence ordering: the PWM supplier may get 1206 * suspended before the consumer. 1207 */ 1208 dev_warn(pwmchip_parent(pwm->chip), 1209 "No consumer device specified to create a link to\n"); 1210 return NULL; 1211 } 1212 1213 dl = device_link_add(dev, pwmchip_parent(pwm->chip), DL_FLAG_AUTOREMOVE_CONSUMER); 1214 if (!dl) { 1215 dev_err(dev, "failed to create device link to %s\n", 1216 dev_name(pwmchip_parent(pwm->chip))); 1217 return ERR_PTR(-EINVAL); 1218 } 1219 1220 return dl; 1221 } 1222 1223 static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode) 1224 { 1225 struct pwm_chip *chip; 1226 unsigned long id, tmp; 1227 1228 guard(mutex)(&pwm_lock); 1229 1230 idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) 1231 if (pwmchip_parent(chip) && device_match_fwnode(pwmchip_parent(chip), fwnode)) 1232 return chip; 1233 1234 return ERR_PTR(-EPROBE_DEFER); 1235 } 1236 1237 /** 1238 * of_pwm_get() - request a PWM via the PWM framework 1239 * @dev: device for PWM consumer 1240 * @np: device node to get the PWM from 1241 * @con_id: consumer name 1242 * 1243 * Returns the PWM device parsed from the phandle and index specified in the 1244 * "pwms" property of a device tree node or a negative error-code on failure. 1245 * Values parsed from the device tree are stored in the returned PWM device 1246 * object. 1247 * 1248 * If con_id is NULL, the first PWM device listed in the "pwms" property will 1249 * be requested. Otherwise the "pwm-names" property is used to do a reverse 1250 * lookup of the PWM index. This also means that the "pwm-names" property 1251 * becomes mandatory for devices that look up the PWM device via the con_id 1252 * parameter. 1253 * 1254 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded 1255 * error code on failure. 1256 */ 1257 static struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np, 1258 const char *con_id) 1259 { 1260 struct pwm_device *pwm = NULL; 1261 struct of_phandle_args args; 1262 struct device_link *dl; 1263 struct pwm_chip *chip; 1264 int index = 0; 1265 int err; 1266 1267 if (con_id) { 1268 index = of_property_match_string(np, "pwm-names", con_id); 1269 if (index < 0) 1270 return ERR_PTR(index); 1271 } 1272 1273 err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index, 1274 &args); 1275 if (err) { 1276 pr_err("%s(): can't parse \"pwms\" property\n", __func__); 1277 return ERR_PTR(err); 1278 } 1279 1280 chip = fwnode_to_pwmchip(of_fwnode_handle(args.np)); 1281 if (IS_ERR(chip)) { 1282 if (PTR_ERR(chip) != -EPROBE_DEFER) 1283 pr_err("%s(): PWM chip not found\n", __func__); 1284 1285 pwm = ERR_CAST(chip); 1286 goto put; 1287 } 1288 1289 pwm = chip->of_xlate(chip, &args); 1290 if (IS_ERR(pwm)) 1291 goto put; 1292 1293 dl = pwm_device_link_add(dev, pwm); 1294 if (IS_ERR(dl)) { 1295 /* of_xlate ended up calling pwm_request_from_chip() */ 1296 pwm_put(pwm); 1297 pwm = ERR_CAST(dl); 1298 goto put; 1299 } 1300 1301 /* 1302 * If a consumer name was not given, try to look it up from the 1303 * "pwm-names" property if it exists. Otherwise use the name of 1304 * the user device node. 1305 */ 1306 if (!con_id) { 1307 err = of_property_read_string_index(np, "pwm-names", index, 1308 &con_id); 1309 if (err < 0) 1310 con_id = np->name; 1311 } 1312 1313 pwm->label = con_id; 1314 1315 put: 1316 of_node_put(args.np); 1317 1318 return pwm; 1319 } 1320 1321 /** 1322 * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI 1323 * @fwnode: firmware node to get the "pwms" property from 1324 * 1325 * Returns the PWM device parsed from the fwnode and index specified in the 1326 * "pwms" property or a negative error-code on failure. 1327 * Values parsed from the device tree are stored in the returned PWM device 1328 * object. 1329 * 1330 * This is analogous to of_pwm_get() except con_id is not yet supported. 1331 * ACPI entries must look like 1332 * Package () {"pwms", Package () 1333 * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}} 1334 * 1335 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded 1336 * error code on failure. 1337 */ 1338 static struct pwm_device *acpi_pwm_get(const struct fwnode_handle *fwnode) 1339 { 1340 struct pwm_device *pwm; 1341 struct fwnode_reference_args args; 1342 struct pwm_chip *chip; 1343 int ret; 1344 1345 memset(&args, 0, sizeof(args)); 1346 1347 ret = __acpi_node_get_property_reference(fwnode, "pwms", 0, 3, &args); 1348 if (ret < 0) 1349 return ERR_PTR(ret); 1350 1351 if (args.nargs < 2) 1352 return ERR_PTR(-EPROTO); 1353 1354 chip = fwnode_to_pwmchip(args.fwnode); 1355 if (IS_ERR(chip)) 1356 return ERR_CAST(chip); 1357 1358 pwm = pwm_request_from_chip(chip, args.args[0], NULL); 1359 if (IS_ERR(pwm)) 1360 return pwm; 1361 1362 pwm->args.period = args.args[1]; 1363 pwm->args.polarity = PWM_POLARITY_NORMAL; 1364 1365 if (args.nargs > 2 && args.args[2] & PWM_POLARITY_INVERTED) 1366 pwm->args.polarity = PWM_POLARITY_INVERSED; 1367 1368 return pwm; 1369 } 1370 1371 static DEFINE_MUTEX(pwm_lookup_lock); 1372 static LIST_HEAD(pwm_lookup_list); 1373 1374 /** 1375 * pwm_add_table() - register PWM device consumers 1376 * @table: array of consumers to register 1377 * @num: number of consumers in table 1378 */ 1379 void pwm_add_table(struct pwm_lookup *table, size_t num) 1380 { 1381 guard(mutex)(&pwm_lookup_lock); 1382 1383 while (num--) { 1384 list_add_tail(&table->list, &pwm_lookup_list); 1385 table++; 1386 } 1387 } 1388 1389 /** 1390 * pwm_remove_table() - unregister PWM device consumers 1391 * @table: array of consumers to unregister 1392 * @num: number of consumers in table 1393 */ 1394 void pwm_remove_table(struct pwm_lookup *table, size_t num) 1395 { 1396 guard(mutex)(&pwm_lookup_lock); 1397 1398 while (num--) { 1399 list_del(&table->list); 1400 table++; 1401 } 1402 } 1403 1404 /** 1405 * pwm_get() - look up and request a PWM device 1406 * @dev: device for PWM consumer 1407 * @con_id: consumer name 1408 * 1409 * Lookup is first attempted using DT. If the device was not instantiated from 1410 * a device tree, a PWM chip and a relative index is looked up via a table 1411 * supplied by board setup code (see pwm_add_table()). 1412 * 1413 * Once a PWM chip has been found the specified PWM device will be requested 1414 * and is ready to be used. 1415 * 1416 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded 1417 * error code on failure. 1418 */ 1419 struct pwm_device *pwm_get(struct device *dev, const char *con_id) 1420 { 1421 const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL; 1422 const char *dev_id = dev ? dev_name(dev) : NULL; 1423 struct pwm_device *pwm; 1424 struct pwm_chip *chip; 1425 struct device_link *dl; 1426 unsigned int best = 0; 1427 struct pwm_lookup *p, *chosen = NULL; 1428 unsigned int match; 1429 int err; 1430 1431 /* look up via DT first */ 1432 if (is_of_node(fwnode)) 1433 return of_pwm_get(dev, to_of_node(fwnode), con_id); 1434 1435 /* then lookup via ACPI */ 1436 if (is_acpi_node(fwnode)) { 1437 pwm = acpi_pwm_get(fwnode); 1438 if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT) 1439 return pwm; 1440 } 1441 1442 /* 1443 * We look up the provider in the static table typically provided by 1444 * board setup code. We first try to lookup the consumer device by 1445 * name. If the consumer device was passed in as NULL or if no match 1446 * was found, we try to find the consumer by directly looking it up 1447 * by name. 1448 * 1449 * If a match is found, the provider PWM chip is looked up by name 1450 * and a PWM device is requested using the PWM device per-chip index. 1451 * 1452 * The lookup algorithm was shamelessly taken from the clock 1453 * framework: 1454 * 1455 * We do slightly fuzzy matching here: 1456 * An entry with a NULL ID is assumed to be a wildcard. 1457 * If an entry has a device ID, it must match 1458 * If an entry has a connection ID, it must match 1459 * Then we take the most specific entry - with the following order 1460 * of precedence: dev+con > dev only > con only. 1461 */ 1462 scoped_guard(mutex, &pwm_lookup_lock) 1463 list_for_each_entry(p, &pwm_lookup_list, list) { 1464 match = 0; 1465 1466 if (p->dev_id) { 1467 if (!dev_id || strcmp(p->dev_id, dev_id)) 1468 continue; 1469 1470 match += 2; 1471 } 1472 1473 if (p->con_id) { 1474 if (!con_id || strcmp(p->con_id, con_id)) 1475 continue; 1476 1477 match += 1; 1478 } 1479 1480 if (match > best) { 1481 chosen = p; 1482 1483 if (match != 3) 1484 best = match; 1485 else 1486 break; 1487 } 1488 } 1489 1490 if (!chosen) 1491 return ERR_PTR(-ENODEV); 1492 1493 chip = pwmchip_find_by_name(chosen->provider); 1494 1495 /* 1496 * If the lookup entry specifies a module, load the module and retry 1497 * the PWM chip lookup. This can be used to work around driver load 1498 * ordering issues if driver's can't be made to properly support the 1499 * deferred probe mechanism. 1500 */ 1501 if (!chip && chosen->module) { 1502 err = request_module(chosen->module); 1503 if (err == 0) 1504 chip = pwmchip_find_by_name(chosen->provider); 1505 } 1506 1507 if (!chip) 1508 return ERR_PTR(-EPROBE_DEFER); 1509 1510 pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id); 1511 if (IS_ERR(pwm)) 1512 return pwm; 1513 1514 dl = pwm_device_link_add(dev, pwm); 1515 if (IS_ERR(dl)) { 1516 pwm_put(pwm); 1517 return ERR_CAST(dl); 1518 } 1519 1520 pwm->args.period = chosen->period; 1521 pwm->args.polarity = chosen->polarity; 1522 1523 return pwm; 1524 } 1525 EXPORT_SYMBOL_GPL(pwm_get); 1526 1527 /** 1528 * pwm_put() - release a PWM device 1529 * @pwm: PWM device 1530 */ 1531 void pwm_put(struct pwm_device *pwm) 1532 { 1533 struct pwm_chip *chip; 1534 1535 if (!pwm) 1536 return; 1537 1538 chip = pwm->chip; 1539 1540 guard(mutex)(&pwm_lock); 1541 1542 if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) { 1543 pr_warn("PWM device already freed\n"); 1544 return; 1545 } 1546 1547 if (chip->ops->free) 1548 pwm->chip->ops->free(pwm->chip, pwm); 1549 1550 pwm->label = NULL; 1551 1552 put_device(&chip->dev); 1553 1554 module_put(chip->owner); 1555 } 1556 EXPORT_SYMBOL_GPL(pwm_put); 1557 1558 static void devm_pwm_release(void *pwm) 1559 { 1560 pwm_put(pwm); 1561 } 1562 1563 /** 1564 * devm_pwm_get() - resource managed pwm_get() 1565 * @dev: device for PWM consumer 1566 * @con_id: consumer name 1567 * 1568 * This function performs like pwm_get() but the acquired PWM device will 1569 * automatically be released on driver detach. 1570 * 1571 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded 1572 * error code on failure. 1573 */ 1574 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id) 1575 { 1576 struct pwm_device *pwm; 1577 int ret; 1578 1579 pwm = pwm_get(dev, con_id); 1580 if (IS_ERR(pwm)) 1581 return pwm; 1582 1583 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm); 1584 if (ret) 1585 return ERR_PTR(ret); 1586 1587 return pwm; 1588 } 1589 EXPORT_SYMBOL_GPL(devm_pwm_get); 1590 1591 /** 1592 * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node 1593 * @dev: device for PWM consumer 1594 * @fwnode: firmware node to get the PWM from 1595 * @con_id: consumer name 1596 * 1597 * Returns the PWM device parsed from the firmware node. See of_pwm_get() and 1598 * acpi_pwm_get() for a detailed description. 1599 * 1600 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded 1601 * error code on failure. 1602 */ 1603 struct pwm_device *devm_fwnode_pwm_get(struct device *dev, 1604 struct fwnode_handle *fwnode, 1605 const char *con_id) 1606 { 1607 struct pwm_device *pwm = ERR_PTR(-ENODEV); 1608 int ret; 1609 1610 if (is_of_node(fwnode)) 1611 pwm = of_pwm_get(dev, to_of_node(fwnode), con_id); 1612 else if (is_acpi_node(fwnode)) 1613 pwm = acpi_pwm_get(fwnode); 1614 if (IS_ERR(pwm)) 1615 return pwm; 1616 1617 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm); 1618 if (ret) 1619 return ERR_PTR(ret); 1620 1621 return pwm; 1622 } 1623 EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get); 1624 1625 static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s) 1626 { 1627 unsigned int i; 1628 1629 for (i = 0; i < chip->npwm; i++) { 1630 struct pwm_device *pwm = &chip->pwms[i]; 1631 struct pwm_state state; 1632 1633 pwm_get_state(pwm, &state); 1634 1635 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label); 1636 1637 if (test_bit(PWMF_REQUESTED, &pwm->flags)) 1638 seq_puts(s, " requested"); 1639 1640 if (state.enabled) 1641 seq_puts(s, " enabled"); 1642 1643 seq_printf(s, " period: %llu ns", state.period); 1644 seq_printf(s, " duty: %llu ns", state.duty_cycle); 1645 seq_printf(s, " polarity: %s", 1646 state.polarity ? "inverse" : "normal"); 1647 1648 if (state.usage_power) 1649 seq_puts(s, " usage_power"); 1650 1651 seq_puts(s, "\n"); 1652 } 1653 } 1654 1655 static void *pwm_seq_start(struct seq_file *s, loff_t *pos) 1656 { 1657 unsigned long id = *pos; 1658 void *ret; 1659 1660 mutex_lock(&pwm_lock); 1661 s->private = ""; 1662 1663 ret = idr_get_next_ul(&pwm_chips, &id); 1664 *pos = id; 1665 return ret; 1666 } 1667 1668 static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos) 1669 { 1670 unsigned long id = *pos + 1; 1671 void *ret; 1672 1673 s->private = "\n"; 1674 1675 ret = idr_get_next_ul(&pwm_chips, &id); 1676 *pos = id; 1677 return ret; 1678 } 1679 1680 static void pwm_seq_stop(struct seq_file *s, void *v) 1681 { 1682 mutex_unlock(&pwm_lock); 1683 } 1684 1685 static int pwm_seq_show(struct seq_file *s, void *v) 1686 { 1687 struct pwm_chip *chip = v; 1688 1689 seq_printf(s, "%s%d: %s/%s, %d PWM device%s\n", 1690 (char *)s->private, chip->id, 1691 pwmchip_parent(chip)->bus ? pwmchip_parent(chip)->bus->name : "no-bus", 1692 dev_name(pwmchip_parent(chip)), chip->npwm, 1693 (chip->npwm != 1) ? "s" : ""); 1694 1695 pwm_dbg_show(chip, s); 1696 1697 return 0; 1698 } 1699 1700 static const struct seq_operations pwm_debugfs_sops = { 1701 .start = pwm_seq_start, 1702 .next = pwm_seq_next, 1703 .stop = pwm_seq_stop, 1704 .show = pwm_seq_show, 1705 }; 1706 1707 DEFINE_SEQ_ATTRIBUTE(pwm_debugfs); 1708 1709 static int __init pwm_init(void) 1710 { 1711 int ret; 1712 1713 ret = class_register(&pwm_class); 1714 if (ret) { 1715 pr_err("Failed to initialize PWM class (%pe)\n", ERR_PTR(ret)); 1716 return ret; 1717 } 1718 1719 if (IS_ENABLED(CONFIG_DEBUG_FS)) 1720 debugfs_create_file("pwm", 0444, NULL, NULL, &pwm_debugfs_fops); 1721 1722 return 0; 1723 } 1724 subsys_initcall(pwm_init); 1725