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/module.h> 10 #include <linux/pwm.h> 11 #include <linux/radix-tree.h> 12 #include <linux/list.h> 13 #include <linux/mutex.h> 14 #include <linux/err.h> 15 #include <linux/slab.h> 16 #include <linux/device.h> 17 #include <linux/debugfs.h> 18 #include <linux/seq_file.h> 19 20 #include <dt-bindings/pwm/pwm.h> 21 22 #define MAX_PWMS 1024 23 24 static DEFINE_MUTEX(pwm_lookup_lock); 25 static LIST_HEAD(pwm_lookup_list); 26 static DEFINE_MUTEX(pwm_lock); 27 static LIST_HEAD(pwm_chips); 28 static DECLARE_BITMAP(allocated_pwms, MAX_PWMS); 29 static RADIX_TREE(pwm_tree, GFP_KERNEL); 30 31 static struct pwm_device *pwm_to_device(unsigned int pwm) 32 { 33 return radix_tree_lookup(&pwm_tree, pwm); 34 } 35 36 static int alloc_pwms(int pwm, unsigned int count) 37 { 38 unsigned int from = 0; 39 unsigned int start; 40 41 if (pwm >= MAX_PWMS) 42 return -EINVAL; 43 44 if (pwm >= 0) 45 from = pwm; 46 47 start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, from, 48 count, 0); 49 50 if (pwm >= 0 && start != pwm) 51 return -EEXIST; 52 53 if (start + count > MAX_PWMS) 54 return -ENOSPC; 55 56 return start; 57 } 58 59 static void free_pwms(struct pwm_chip *chip) 60 { 61 unsigned int i; 62 63 for (i = 0; i < chip->npwm; i++) { 64 struct pwm_device *pwm = &chip->pwms[i]; 65 66 radix_tree_delete(&pwm_tree, pwm->pwm); 67 } 68 69 bitmap_clear(allocated_pwms, chip->base, chip->npwm); 70 71 kfree(chip->pwms); 72 chip->pwms = NULL; 73 } 74 75 static struct pwm_chip *pwmchip_find_by_name(const char *name) 76 { 77 struct pwm_chip *chip; 78 79 if (!name) 80 return NULL; 81 82 mutex_lock(&pwm_lock); 83 84 list_for_each_entry(chip, &pwm_chips, list) { 85 const char *chip_name = dev_name(chip->dev); 86 87 if (chip_name && strcmp(chip_name, name) == 0) { 88 mutex_unlock(&pwm_lock); 89 return chip; 90 } 91 } 92 93 mutex_unlock(&pwm_lock); 94 95 return NULL; 96 } 97 98 static int pwm_device_request(struct pwm_device *pwm, const char *label) 99 { 100 int err; 101 102 if (test_bit(PWMF_REQUESTED, &pwm->flags)) 103 return -EBUSY; 104 105 if (!try_module_get(pwm->chip->ops->owner)) 106 return -ENODEV; 107 108 if (pwm->chip->ops->request) { 109 err = pwm->chip->ops->request(pwm->chip, pwm); 110 if (err) { 111 module_put(pwm->chip->ops->owner); 112 return err; 113 } 114 } 115 116 set_bit(PWMF_REQUESTED, &pwm->flags); 117 pwm->label = label; 118 119 return 0; 120 } 121 122 struct pwm_device * 123 of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args) 124 { 125 struct pwm_device *pwm; 126 127 /* check, whether the driver supports a third cell for flags */ 128 if (pc->of_pwm_n_cells < 3) 129 return ERR_PTR(-EINVAL); 130 131 /* flags in the third cell are optional */ 132 if (args->args_count < 2) 133 return ERR_PTR(-EINVAL); 134 135 if (args->args[0] >= pc->npwm) 136 return ERR_PTR(-EINVAL); 137 138 pwm = pwm_request_from_chip(pc, args->args[0], NULL); 139 if (IS_ERR(pwm)) 140 return pwm; 141 142 pwm->args.period = args->args[1]; 143 pwm->args.polarity = PWM_POLARITY_NORMAL; 144 145 if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED) 146 pwm->args.polarity = PWM_POLARITY_INVERSED; 147 148 return pwm; 149 } 150 EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags); 151 152 static struct pwm_device * 153 of_pwm_simple_xlate(struct pwm_chip *pc, const struct of_phandle_args *args) 154 { 155 struct pwm_device *pwm; 156 157 /* sanity check driver support */ 158 if (pc->of_pwm_n_cells < 2) 159 return ERR_PTR(-EINVAL); 160 161 /* all cells are required */ 162 if (args->args_count != pc->of_pwm_n_cells) 163 return ERR_PTR(-EINVAL); 164 165 if (args->args[0] >= pc->npwm) 166 return ERR_PTR(-EINVAL); 167 168 pwm = pwm_request_from_chip(pc, args->args[0], NULL); 169 if (IS_ERR(pwm)) 170 return pwm; 171 172 pwm->args.period = args->args[1]; 173 174 return pwm; 175 } 176 177 static void of_pwmchip_add(struct pwm_chip *chip) 178 { 179 if (!chip->dev || !chip->dev->of_node) 180 return; 181 182 if (!chip->of_xlate) { 183 chip->of_xlate = of_pwm_simple_xlate; 184 chip->of_pwm_n_cells = 2; 185 } 186 187 of_node_get(chip->dev->of_node); 188 } 189 190 static void of_pwmchip_remove(struct pwm_chip *chip) 191 { 192 if (chip->dev) 193 of_node_put(chip->dev->of_node); 194 } 195 196 /** 197 * pwm_set_chip_data() - set private chip data for a PWM 198 * @pwm: PWM device 199 * @data: pointer to chip-specific data 200 * 201 * Returns: 0 on success or a negative error code on failure. 202 */ 203 int pwm_set_chip_data(struct pwm_device *pwm, void *data) 204 { 205 if (!pwm) 206 return -EINVAL; 207 208 pwm->chip_data = data; 209 210 return 0; 211 } 212 EXPORT_SYMBOL_GPL(pwm_set_chip_data); 213 214 /** 215 * pwm_get_chip_data() - get private chip data for a PWM 216 * @pwm: PWM device 217 * 218 * Returns: A pointer to the chip-private data for the PWM device. 219 */ 220 void *pwm_get_chip_data(struct pwm_device *pwm) 221 { 222 return pwm ? pwm->chip_data : NULL; 223 } 224 EXPORT_SYMBOL_GPL(pwm_get_chip_data); 225 226 static bool pwm_ops_check(const struct pwm_ops *ops) 227 { 228 /* driver supports legacy, non-atomic operation */ 229 if (ops->config && ops->enable && ops->disable) 230 return true; 231 232 /* driver supports atomic operation */ 233 if (ops->apply) 234 return true; 235 236 return false; 237 } 238 239 /** 240 * pwmchip_add_with_polarity() - register a new PWM chip 241 * @chip: the PWM chip to add 242 * @polarity: initial polarity of PWM channels 243 * 244 * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base 245 * will be used. The initial polarity for all channels is specified by the 246 * @polarity parameter. 247 * 248 * Returns: 0 on success or a negative error code on failure. 249 */ 250 int pwmchip_add_with_polarity(struct pwm_chip *chip, 251 enum pwm_polarity polarity) 252 { 253 struct pwm_device *pwm; 254 unsigned int i; 255 int ret; 256 257 if (!chip || !chip->dev || !chip->ops || !chip->npwm) 258 return -EINVAL; 259 260 if (!pwm_ops_check(chip->ops)) 261 return -EINVAL; 262 263 mutex_lock(&pwm_lock); 264 265 ret = alloc_pwms(chip->base, chip->npwm); 266 if (ret < 0) 267 goto out; 268 269 chip->pwms = kcalloc(chip->npwm, sizeof(*pwm), GFP_KERNEL); 270 if (!chip->pwms) { 271 ret = -ENOMEM; 272 goto out; 273 } 274 275 chip->base = ret; 276 277 for (i = 0; i < chip->npwm; i++) { 278 pwm = &chip->pwms[i]; 279 280 pwm->chip = chip; 281 pwm->pwm = chip->base + i; 282 pwm->hwpwm = i; 283 pwm->state.polarity = polarity; 284 285 if (chip->ops->get_state) 286 chip->ops->get_state(chip, pwm, &pwm->state); 287 288 radix_tree_insert(&pwm_tree, pwm->pwm, pwm); 289 } 290 291 bitmap_set(allocated_pwms, chip->base, chip->npwm); 292 293 INIT_LIST_HEAD(&chip->list); 294 list_add(&chip->list, &pwm_chips); 295 296 ret = 0; 297 298 if (IS_ENABLED(CONFIG_OF)) 299 of_pwmchip_add(chip); 300 301 out: 302 mutex_unlock(&pwm_lock); 303 304 if (!ret) 305 pwmchip_sysfs_export(chip); 306 307 return ret; 308 } 309 EXPORT_SYMBOL_GPL(pwmchip_add_with_polarity); 310 311 /** 312 * pwmchip_add() - register a new PWM chip 313 * @chip: the PWM chip to add 314 * 315 * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base 316 * will be used. The initial polarity for all channels is normal. 317 * 318 * Returns: 0 on success or a negative error code on failure. 319 */ 320 int pwmchip_add(struct pwm_chip *chip) 321 { 322 return pwmchip_add_with_polarity(chip, PWM_POLARITY_NORMAL); 323 } 324 EXPORT_SYMBOL_GPL(pwmchip_add); 325 326 /** 327 * pwmchip_remove() - remove a PWM chip 328 * @chip: the PWM chip to remove 329 * 330 * Removes a PWM chip. This function may return busy if the PWM chip provides 331 * a PWM device that is still requested. 332 * 333 * Returns: 0 on success or a negative error code on failure. 334 */ 335 int pwmchip_remove(struct pwm_chip *chip) 336 { 337 unsigned int i; 338 int ret = 0; 339 340 pwmchip_sysfs_unexport(chip); 341 342 mutex_lock(&pwm_lock); 343 344 for (i = 0; i < chip->npwm; i++) { 345 struct pwm_device *pwm = &chip->pwms[i]; 346 347 if (test_bit(PWMF_REQUESTED, &pwm->flags)) { 348 ret = -EBUSY; 349 goto out; 350 } 351 } 352 353 list_del_init(&chip->list); 354 355 if (IS_ENABLED(CONFIG_OF)) 356 of_pwmchip_remove(chip); 357 358 free_pwms(chip); 359 360 out: 361 mutex_unlock(&pwm_lock); 362 return ret; 363 } 364 EXPORT_SYMBOL_GPL(pwmchip_remove); 365 366 /** 367 * pwm_request() - request a PWM device 368 * @pwm: global PWM device index 369 * @label: PWM device label 370 * 371 * This function is deprecated, use pwm_get() instead. 372 * 373 * Returns: A pointer to a PWM device or an ERR_PTR()-encoded error code on 374 * failure. 375 */ 376 struct pwm_device *pwm_request(int pwm, const char *label) 377 { 378 struct pwm_device *dev; 379 int err; 380 381 if (pwm < 0 || pwm >= MAX_PWMS) 382 return ERR_PTR(-EINVAL); 383 384 mutex_lock(&pwm_lock); 385 386 dev = pwm_to_device(pwm); 387 if (!dev) { 388 dev = ERR_PTR(-EPROBE_DEFER); 389 goto out; 390 } 391 392 err = pwm_device_request(dev, label); 393 if (err < 0) 394 dev = ERR_PTR(err); 395 396 out: 397 mutex_unlock(&pwm_lock); 398 399 return dev; 400 } 401 EXPORT_SYMBOL_GPL(pwm_request); 402 403 /** 404 * pwm_request_from_chip() - request a PWM device relative to a PWM chip 405 * @chip: PWM chip 406 * @index: per-chip index of the PWM to request 407 * @label: a literal description string of this PWM 408 * 409 * Returns: A pointer to the PWM device at the given index of the given PWM 410 * chip. A negative error code is returned if the index is not valid for the 411 * specified PWM chip or if the PWM device cannot be requested. 412 */ 413 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, 414 unsigned int index, 415 const char *label) 416 { 417 struct pwm_device *pwm; 418 int err; 419 420 if (!chip || index >= chip->npwm) 421 return ERR_PTR(-EINVAL); 422 423 mutex_lock(&pwm_lock); 424 pwm = &chip->pwms[index]; 425 426 err = pwm_device_request(pwm, label); 427 if (err < 0) 428 pwm = ERR_PTR(err); 429 430 mutex_unlock(&pwm_lock); 431 return pwm; 432 } 433 EXPORT_SYMBOL_GPL(pwm_request_from_chip); 434 435 /** 436 * pwm_free() - free a PWM device 437 * @pwm: PWM device 438 * 439 * This function is deprecated, use pwm_put() instead. 440 */ 441 void pwm_free(struct pwm_device *pwm) 442 { 443 pwm_put(pwm); 444 } 445 EXPORT_SYMBOL_GPL(pwm_free); 446 447 /** 448 * pwm_apply_state() - atomically apply a new state to a PWM device 449 * @pwm: PWM device 450 * @state: new state to apply. This can be adjusted by the PWM driver 451 * if the requested config is not achievable, for example, 452 * ->duty_cycle and ->period might be approximated. 453 */ 454 int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state) 455 { 456 int err; 457 458 if (!pwm || !state || !state->period || 459 state->duty_cycle > state->period) 460 return -EINVAL; 461 462 if (state->period == pwm->state.period && 463 state->duty_cycle == pwm->state.duty_cycle && 464 state->polarity == pwm->state.polarity && 465 state->enabled == pwm->state.enabled) 466 return 0; 467 468 if (pwm->chip->ops->apply) { 469 err = pwm->chip->ops->apply(pwm->chip, pwm, state); 470 if (err) 471 return err; 472 473 pwm->state = *state; 474 } else { 475 /* 476 * FIXME: restore the initial state in case of error. 477 */ 478 if (state->polarity != pwm->state.polarity) { 479 if (!pwm->chip->ops->set_polarity) 480 return -ENOTSUPP; 481 482 /* 483 * Changing the polarity of a running PWM is 484 * only allowed when the PWM driver implements 485 * ->apply(). 486 */ 487 if (pwm->state.enabled) { 488 pwm->chip->ops->disable(pwm->chip, pwm); 489 pwm->state.enabled = false; 490 } 491 492 err = pwm->chip->ops->set_polarity(pwm->chip, pwm, 493 state->polarity); 494 if (err) 495 return err; 496 497 pwm->state.polarity = state->polarity; 498 } 499 500 if (state->period != pwm->state.period || 501 state->duty_cycle != pwm->state.duty_cycle) { 502 err = pwm->chip->ops->config(pwm->chip, pwm, 503 state->duty_cycle, 504 state->period); 505 if (err) 506 return err; 507 508 pwm->state.duty_cycle = state->duty_cycle; 509 pwm->state.period = state->period; 510 } 511 512 if (state->enabled != pwm->state.enabled) { 513 if (state->enabled) { 514 err = pwm->chip->ops->enable(pwm->chip, pwm); 515 if (err) 516 return err; 517 } else { 518 pwm->chip->ops->disable(pwm->chip, pwm); 519 } 520 521 pwm->state.enabled = state->enabled; 522 } 523 } 524 525 return 0; 526 } 527 EXPORT_SYMBOL_GPL(pwm_apply_state); 528 529 /** 530 * pwm_capture() - capture and report a PWM signal 531 * @pwm: PWM device 532 * @result: structure to fill with capture result 533 * @timeout: time to wait, in milliseconds, before giving up on capture 534 * 535 * Returns: 0 on success or a negative error code on failure. 536 */ 537 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, 538 unsigned long timeout) 539 { 540 int err; 541 542 if (!pwm || !pwm->chip->ops) 543 return -EINVAL; 544 545 if (!pwm->chip->ops->capture) 546 return -ENOSYS; 547 548 mutex_lock(&pwm_lock); 549 err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout); 550 mutex_unlock(&pwm_lock); 551 552 return err; 553 } 554 EXPORT_SYMBOL_GPL(pwm_capture); 555 556 /** 557 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments 558 * @pwm: PWM device 559 * 560 * This function will adjust the PWM config to the PWM arguments provided 561 * by the DT or PWM lookup table. This is particularly useful to adapt 562 * the bootloader config to the Linux one. 563 */ 564 int pwm_adjust_config(struct pwm_device *pwm) 565 { 566 struct pwm_state state; 567 struct pwm_args pargs; 568 569 pwm_get_args(pwm, &pargs); 570 pwm_get_state(pwm, &state); 571 572 /* 573 * If the current period is zero it means that either the PWM driver 574 * does not support initial state retrieval or the PWM has not yet 575 * been configured. 576 * 577 * In either case, we setup the new period and polarity, and assign a 578 * duty cycle of 0. 579 */ 580 if (!state.period) { 581 state.duty_cycle = 0; 582 state.period = pargs.period; 583 state.polarity = pargs.polarity; 584 585 return pwm_apply_state(pwm, &state); 586 } 587 588 /* 589 * Adjust the PWM duty cycle/period based on the period value provided 590 * in PWM args. 591 */ 592 if (pargs.period != state.period) { 593 u64 dutycycle = (u64)state.duty_cycle * pargs.period; 594 595 do_div(dutycycle, state.period); 596 state.duty_cycle = dutycycle; 597 state.period = pargs.period; 598 } 599 600 /* 601 * If the polarity changed, we should also change the duty cycle. 602 */ 603 if (pargs.polarity != state.polarity) { 604 state.polarity = pargs.polarity; 605 state.duty_cycle = state.period - state.duty_cycle; 606 } 607 608 return pwm_apply_state(pwm, &state); 609 } 610 EXPORT_SYMBOL_GPL(pwm_adjust_config); 611 612 static struct pwm_chip *of_node_to_pwmchip(struct device_node *np) 613 { 614 struct pwm_chip *chip; 615 616 mutex_lock(&pwm_lock); 617 618 list_for_each_entry(chip, &pwm_chips, list) 619 if (chip->dev && chip->dev->of_node == np) { 620 mutex_unlock(&pwm_lock); 621 return chip; 622 } 623 624 mutex_unlock(&pwm_lock); 625 626 return ERR_PTR(-EPROBE_DEFER); 627 } 628 629 /** 630 * of_pwm_get() - request a PWM via the PWM framework 631 * @np: device node to get the PWM from 632 * @con_id: consumer name 633 * 634 * Returns the PWM device parsed from the phandle and index specified in the 635 * "pwms" property of a device tree node or a negative error-code on failure. 636 * Values parsed from the device tree are stored in the returned PWM device 637 * object. 638 * 639 * If con_id is NULL, the first PWM device listed in the "pwms" property will 640 * be requested. Otherwise the "pwm-names" property is used to do a reverse 641 * lookup of the PWM index. This also means that the "pwm-names" property 642 * becomes mandatory for devices that look up the PWM device via the con_id 643 * parameter. 644 * 645 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded 646 * error code on failure. 647 */ 648 struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id) 649 { 650 struct pwm_device *pwm = NULL; 651 struct of_phandle_args args; 652 struct pwm_chip *pc; 653 int index = 0; 654 int err; 655 656 if (con_id) { 657 index = of_property_match_string(np, "pwm-names", con_id); 658 if (index < 0) 659 return ERR_PTR(index); 660 } 661 662 err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index, 663 &args); 664 if (err) { 665 pr_err("%s(): can't parse \"pwms\" property\n", __func__); 666 return ERR_PTR(err); 667 } 668 669 pc = of_node_to_pwmchip(args.np); 670 if (IS_ERR(pc)) { 671 if (PTR_ERR(pc) != -EPROBE_DEFER) 672 pr_err("%s(): PWM chip not found\n", __func__); 673 674 pwm = ERR_CAST(pc); 675 goto put; 676 } 677 678 pwm = pc->of_xlate(pc, &args); 679 if (IS_ERR(pwm)) 680 goto put; 681 682 /* 683 * If a consumer name was not given, try to look it up from the 684 * "pwm-names" property if it exists. Otherwise use the name of 685 * the user device node. 686 */ 687 if (!con_id) { 688 err = of_property_read_string_index(np, "pwm-names", index, 689 &con_id); 690 if (err < 0) 691 con_id = np->name; 692 } 693 694 pwm->label = con_id; 695 696 put: 697 of_node_put(args.np); 698 699 return pwm; 700 } 701 EXPORT_SYMBOL_GPL(of_pwm_get); 702 703 /** 704 * pwm_add_table() - register PWM device consumers 705 * @table: array of consumers to register 706 * @num: number of consumers in table 707 */ 708 void pwm_add_table(struct pwm_lookup *table, size_t num) 709 { 710 mutex_lock(&pwm_lookup_lock); 711 712 while (num--) { 713 list_add_tail(&table->list, &pwm_lookup_list); 714 table++; 715 } 716 717 mutex_unlock(&pwm_lookup_lock); 718 } 719 720 /** 721 * pwm_remove_table() - unregister PWM device consumers 722 * @table: array of consumers to unregister 723 * @num: number of consumers in table 724 */ 725 void pwm_remove_table(struct pwm_lookup *table, size_t num) 726 { 727 mutex_lock(&pwm_lookup_lock); 728 729 while (num--) { 730 list_del(&table->list); 731 table++; 732 } 733 734 mutex_unlock(&pwm_lookup_lock); 735 } 736 737 /** 738 * pwm_get() - look up and request a PWM device 739 * @dev: device for PWM consumer 740 * @con_id: consumer name 741 * 742 * Lookup is first attempted using DT. If the device was not instantiated from 743 * a device tree, a PWM chip and a relative index is looked up via a table 744 * supplied by board setup code (see pwm_add_table()). 745 * 746 * Once a PWM chip has been found the specified PWM device will be requested 747 * and is ready to be used. 748 * 749 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded 750 * error code on failure. 751 */ 752 struct pwm_device *pwm_get(struct device *dev, const char *con_id) 753 { 754 const char *dev_id = dev ? dev_name(dev) : NULL; 755 struct pwm_device *pwm; 756 struct pwm_chip *chip; 757 unsigned int best = 0; 758 struct pwm_lookup *p, *chosen = NULL; 759 unsigned int match; 760 int err; 761 762 /* look up via DT first */ 763 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) 764 return of_pwm_get(dev->of_node, con_id); 765 766 /* 767 * We look up the provider in the static table typically provided by 768 * board setup code. We first try to lookup the consumer device by 769 * name. If the consumer device was passed in as NULL or if no match 770 * was found, we try to find the consumer by directly looking it up 771 * by name. 772 * 773 * If a match is found, the provider PWM chip is looked up by name 774 * and a PWM device is requested using the PWM device per-chip index. 775 * 776 * The lookup algorithm was shamelessly taken from the clock 777 * framework: 778 * 779 * We do slightly fuzzy matching here: 780 * An entry with a NULL ID is assumed to be a wildcard. 781 * If an entry has a device ID, it must match 782 * If an entry has a connection ID, it must match 783 * Then we take the most specific entry - with the following order 784 * of precedence: dev+con > dev only > con only. 785 */ 786 mutex_lock(&pwm_lookup_lock); 787 788 list_for_each_entry(p, &pwm_lookup_list, list) { 789 match = 0; 790 791 if (p->dev_id) { 792 if (!dev_id || strcmp(p->dev_id, dev_id)) 793 continue; 794 795 match += 2; 796 } 797 798 if (p->con_id) { 799 if (!con_id || strcmp(p->con_id, con_id)) 800 continue; 801 802 match += 1; 803 } 804 805 if (match > best) { 806 chosen = p; 807 808 if (match != 3) 809 best = match; 810 else 811 break; 812 } 813 } 814 815 mutex_unlock(&pwm_lookup_lock); 816 817 if (!chosen) 818 return ERR_PTR(-ENODEV); 819 820 chip = pwmchip_find_by_name(chosen->provider); 821 822 /* 823 * If the lookup entry specifies a module, load the module and retry 824 * the PWM chip lookup. This can be used to work around driver load 825 * ordering issues if driver's can't be made to properly support the 826 * deferred probe mechanism. 827 */ 828 if (!chip && chosen->module) { 829 err = request_module(chosen->module); 830 if (err == 0) 831 chip = pwmchip_find_by_name(chosen->provider); 832 } 833 834 if (!chip) 835 return ERR_PTR(-EPROBE_DEFER); 836 837 pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id); 838 if (IS_ERR(pwm)) 839 return pwm; 840 841 pwm->args.period = chosen->period; 842 pwm->args.polarity = chosen->polarity; 843 844 return pwm; 845 } 846 EXPORT_SYMBOL_GPL(pwm_get); 847 848 /** 849 * pwm_put() - release a PWM device 850 * @pwm: PWM device 851 */ 852 void pwm_put(struct pwm_device *pwm) 853 { 854 if (!pwm) 855 return; 856 857 mutex_lock(&pwm_lock); 858 859 if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) { 860 pr_warn("PWM device already freed\n"); 861 goto out; 862 } 863 864 if (pwm->chip->ops->free) 865 pwm->chip->ops->free(pwm->chip, pwm); 866 867 pwm_set_chip_data(pwm, NULL); 868 pwm->label = NULL; 869 870 module_put(pwm->chip->ops->owner); 871 out: 872 mutex_unlock(&pwm_lock); 873 } 874 EXPORT_SYMBOL_GPL(pwm_put); 875 876 static void devm_pwm_release(struct device *dev, void *res) 877 { 878 pwm_put(*(struct pwm_device **)res); 879 } 880 881 /** 882 * devm_pwm_get() - resource managed pwm_get() 883 * @dev: device for PWM consumer 884 * @con_id: consumer name 885 * 886 * This function performs like pwm_get() but the acquired PWM device will 887 * automatically be released on driver detach. 888 * 889 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded 890 * error code on failure. 891 */ 892 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id) 893 { 894 struct pwm_device **ptr, *pwm; 895 896 ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL); 897 if (!ptr) 898 return ERR_PTR(-ENOMEM); 899 900 pwm = pwm_get(dev, con_id); 901 if (!IS_ERR(pwm)) { 902 *ptr = pwm; 903 devres_add(dev, ptr); 904 } else { 905 devres_free(ptr); 906 } 907 908 return pwm; 909 } 910 EXPORT_SYMBOL_GPL(devm_pwm_get); 911 912 /** 913 * devm_of_pwm_get() - resource managed of_pwm_get() 914 * @dev: device for PWM consumer 915 * @np: device node to get the PWM from 916 * @con_id: consumer name 917 * 918 * This function performs like of_pwm_get() but the acquired PWM device will 919 * automatically be released on driver detach. 920 * 921 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded 922 * error code on failure. 923 */ 924 struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np, 925 const char *con_id) 926 { 927 struct pwm_device **ptr, *pwm; 928 929 ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL); 930 if (!ptr) 931 return ERR_PTR(-ENOMEM); 932 933 pwm = of_pwm_get(np, con_id); 934 if (!IS_ERR(pwm)) { 935 *ptr = pwm; 936 devres_add(dev, ptr); 937 } else { 938 devres_free(ptr); 939 } 940 941 return pwm; 942 } 943 EXPORT_SYMBOL_GPL(devm_of_pwm_get); 944 945 static int devm_pwm_match(struct device *dev, void *res, void *data) 946 { 947 struct pwm_device **p = res; 948 949 if (WARN_ON(!p || !*p)) 950 return 0; 951 952 return *p == data; 953 } 954 955 /** 956 * devm_pwm_put() - resource managed pwm_put() 957 * @dev: device for PWM consumer 958 * @pwm: PWM device 959 * 960 * Release a PWM previously allocated using devm_pwm_get(). Calling this 961 * function is usually not needed because devm-allocated resources are 962 * automatically released on driver detach. 963 */ 964 void devm_pwm_put(struct device *dev, struct pwm_device *pwm) 965 { 966 WARN_ON(devres_release(dev, devm_pwm_release, devm_pwm_match, pwm)); 967 } 968 EXPORT_SYMBOL_GPL(devm_pwm_put); 969 970 #ifdef CONFIG_DEBUG_FS 971 static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s) 972 { 973 unsigned int i; 974 975 for (i = 0; i < chip->npwm; i++) { 976 struct pwm_device *pwm = &chip->pwms[i]; 977 struct pwm_state state; 978 979 pwm_get_state(pwm, &state); 980 981 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label); 982 983 if (test_bit(PWMF_REQUESTED, &pwm->flags)) 984 seq_puts(s, " requested"); 985 986 if (state.enabled) 987 seq_puts(s, " enabled"); 988 989 seq_printf(s, " period: %u ns", state.period); 990 seq_printf(s, " duty: %u ns", state.duty_cycle); 991 seq_printf(s, " polarity: %s", 992 state.polarity ? "inverse" : "normal"); 993 994 seq_puts(s, "\n"); 995 } 996 } 997 998 static void *pwm_seq_start(struct seq_file *s, loff_t *pos) 999 { 1000 mutex_lock(&pwm_lock); 1001 s->private = ""; 1002 1003 return seq_list_start(&pwm_chips, *pos); 1004 } 1005 1006 static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos) 1007 { 1008 s->private = "\n"; 1009 1010 return seq_list_next(v, &pwm_chips, pos); 1011 } 1012 1013 static void pwm_seq_stop(struct seq_file *s, void *v) 1014 { 1015 mutex_unlock(&pwm_lock); 1016 } 1017 1018 static int pwm_seq_show(struct seq_file *s, void *v) 1019 { 1020 struct pwm_chip *chip = list_entry(v, struct pwm_chip, list); 1021 1022 seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private, 1023 chip->dev->bus ? chip->dev->bus->name : "no-bus", 1024 dev_name(chip->dev), chip->npwm, 1025 (chip->npwm != 1) ? "s" : ""); 1026 1027 pwm_dbg_show(chip, s); 1028 1029 return 0; 1030 } 1031 1032 static const struct seq_operations pwm_seq_ops = { 1033 .start = pwm_seq_start, 1034 .next = pwm_seq_next, 1035 .stop = pwm_seq_stop, 1036 .show = pwm_seq_show, 1037 }; 1038 1039 static int pwm_seq_open(struct inode *inode, struct file *file) 1040 { 1041 return seq_open(file, &pwm_seq_ops); 1042 } 1043 1044 static const struct file_operations pwm_debugfs_ops = { 1045 .owner = THIS_MODULE, 1046 .open = pwm_seq_open, 1047 .read = seq_read, 1048 .llseek = seq_lseek, 1049 .release = seq_release, 1050 }; 1051 1052 static int __init pwm_debugfs_init(void) 1053 { 1054 debugfs_create_file("pwm", S_IFREG | S_IRUGO, NULL, NULL, 1055 &pwm_debugfs_ops); 1056 1057 return 0; 1058 } 1059 subsys_initcall(pwm_debugfs_init); 1060 #endif /* CONFIG_DEBUG_FS */ 1061