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