Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

pwm: Reorder symbols in core.c

This moves the functions called by pwm consumers above the functions
called by pwm providers. When character device support is added later
this is hooked into the chip registration functions. As the needed
callbacks are a kind of consumer and make use of the consumer functions,
having this order is more natural and prevents having to add
declarations for static functions.

Also move the global variables for pwm tables to the respective
functions to have them properly grouped.

Link: https://lore.kernel.org/r/eed83de07bdfb69b5ceba0b9aed757ee612dea8f.1706182805.git.u.kleine-koenig@pengutronix.de
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>

+310 -309
+310 -309
drivers/pwm/core.c
··· 24 24 #define CREATE_TRACE_POINTS 25 25 #include <trace/events/pwm.h> 26 26 27 - static DEFINE_MUTEX(pwm_lookup_lock); 28 - static LIST_HEAD(pwm_lookup_list); 29 - 30 27 /* protects access to pwm_chips */ 31 28 static DEFINE_MUTEX(pwm_lock); 32 29 33 30 static DEFINE_IDR(pwm_chips); 34 - 35 - static struct pwm_chip *pwmchip_find_by_name(const char *name) 36 - { 37 - struct pwm_chip *chip; 38 - unsigned long id, tmp; 39 - 40 - if (!name) 41 - return NULL; 42 - 43 - mutex_lock(&pwm_lock); 44 - 45 - idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) { 46 - const char *chip_name = dev_name(chip->dev); 47 - 48 - if (chip_name && strcmp(chip_name, name) == 0) { 49 - mutex_unlock(&pwm_lock); 50 - return chip; 51 - } 52 - } 53 - 54 - mutex_unlock(&pwm_lock); 55 - 56 - return NULL; 57 - } 58 - 59 - static int pwm_device_request(struct pwm_device *pwm, const char *label) 60 - { 61 - int err; 62 - struct pwm_chip *chip = pwm->chip; 63 - const struct pwm_ops *ops = chip->ops; 64 - 65 - if (test_bit(PWMF_REQUESTED, &pwm->flags)) 66 - return -EBUSY; 67 - 68 - if (!try_module_get(chip->owner)) 69 - return -ENODEV; 70 - 71 - if (ops->request) { 72 - err = ops->request(chip, pwm); 73 - if (err) { 74 - module_put(chip->owner); 75 - return err; 76 - } 77 - } 78 - 79 - if (ops->get_state) { 80 - /* 81 - * Zero-initialize state because most drivers are unaware of 82 - * .usage_power. The other members of state are supposed to be 83 - * set by lowlevel drivers. We still initialize the whole 84 - * structure for simplicity even though this might paper over 85 - * faulty implementations of .get_state(). 86 - */ 87 - struct pwm_state state = { 0, }; 88 - 89 - err = ops->get_state(chip, pwm, &state); 90 - trace_pwm_get(pwm, &state, err); 91 - 92 - if (!err) 93 - pwm->state = state; 94 - 95 - if (IS_ENABLED(CONFIG_PWM_DEBUG)) 96 - pwm->last = pwm->state; 97 - } 98 - 99 - set_bit(PWMF_REQUESTED, &pwm->flags); 100 - pwm->label = label; 101 - 102 - return 0; 103 - } 104 - 105 - struct pwm_device * 106 - of_pwm_xlate_with_flags(struct pwm_chip *chip, const struct of_phandle_args *args) 107 - { 108 - struct pwm_device *pwm; 109 - 110 - /* period in the second cell and flags in the third cell are optional */ 111 - if (args->args_count < 1) 112 - return ERR_PTR(-EINVAL); 113 - 114 - pwm = pwm_request_from_chip(chip, args->args[0], NULL); 115 - if (IS_ERR(pwm)) 116 - return pwm; 117 - 118 - if (args->args_count > 1) 119 - pwm->args.period = args->args[1]; 120 - 121 - pwm->args.polarity = PWM_POLARITY_NORMAL; 122 - if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED) 123 - pwm->args.polarity = PWM_POLARITY_INVERSED; 124 - 125 - return pwm; 126 - } 127 - EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags); 128 - 129 - struct pwm_device * 130 - of_pwm_single_xlate(struct pwm_chip *chip, const struct of_phandle_args *args) 131 - { 132 - struct pwm_device *pwm; 133 - 134 - pwm = pwm_request_from_chip(chip, 0, NULL); 135 - if (IS_ERR(pwm)) 136 - return pwm; 137 - 138 - if (args->args_count > 1) 139 - pwm->args.period = args->args[0]; 140 - 141 - pwm->args.polarity = PWM_POLARITY_NORMAL; 142 - if (args->args_count > 1 && args->args[1] & PWM_POLARITY_INVERTED) 143 - pwm->args.polarity = PWM_POLARITY_INVERSED; 144 - 145 - return pwm; 146 - } 147 - EXPORT_SYMBOL_GPL(of_pwm_single_xlate); 148 - 149 - static void of_pwmchip_add(struct pwm_chip *chip) 150 - { 151 - if (!chip->dev || !chip->dev->of_node) 152 - return; 153 - 154 - if (!chip->of_xlate) 155 - chip->of_xlate = of_pwm_xlate_with_flags; 156 - 157 - of_node_get(chip->dev->of_node); 158 - } 159 - 160 - static void of_pwmchip_remove(struct pwm_chip *chip) 161 - { 162 - if (chip->dev) 163 - of_node_put(chip->dev->of_node); 164 - } 165 - 166 - static bool pwm_ops_check(const struct pwm_chip *chip) 167 - { 168 - const struct pwm_ops *ops = chip->ops; 169 - 170 - if (!ops->apply) 171 - return false; 172 - 173 - if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state) 174 - dev_warn(chip->dev, 175 - "Please implement the .get_state() callback\n"); 176 - 177 - return true; 178 - } 179 - 180 - /** 181 - * __pwmchip_add() - register a new PWM chip 182 - * @chip: the PWM chip to add 183 - * @owner: reference to the module providing the chip. 184 - * 185 - * Register a new PWM chip. @owner is supposed to be THIS_MODULE, use the 186 - * pwmchip_add wrapper to do this right. 187 - * 188 - * Returns: 0 on success or a negative error code on failure. 189 - */ 190 - int __pwmchip_add(struct pwm_chip *chip, struct module *owner) 191 - { 192 - unsigned int i; 193 - int ret; 194 - 195 - if (!chip || !chip->dev || !chip->ops || !chip->npwm) 196 - return -EINVAL; 197 - 198 - if (!pwm_ops_check(chip)) 199 - return -EINVAL; 200 - 201 - chip->owner = owner; 202 - 203 - chip->pwms = kcalloc(chip->npwm, sizeof(*chip->pwms), GFP_KERNEL); 204 - if (!chip->pwms) 205 - return -ENOMEM; 206 - 207 - mutex_lock(&pwm_lock); 208 - 209 - ret = idr_alloc(&pwm_chips, chip, 0, 0, GFP_KERNEL); 210 - if (ret < 0) { 211 - mutex_unlock(&pwm_lock); 212 - kfree(chip->pwms); 213 - return ret; 214 - } 215 - 216 - chip->id = ret; 217 - 218 - for (i = 0; i < chip->npwm; i++) { 219 - struct pwm_device *pwm = &chip->pwms[i]; 220 - 221 - pwm->chip = chip; 222 - pwm->hwpwm = i; 223 - } 224 - 225 - mutex_unlock(&pwm_lock); 226 - 227 - if (IS_ENABLED(CONFIG_OF)) 228 - of_pwmchip_add(chip); 229 - 230 - pwmchip_sysfs_export(chip); 231 - 232 - return 0; 233 - } 234 - EXPORT_SYMBOL_GPL(__pwmchip_add); 235 - 236 - /** 237 - * pwmchip_remove() - remove a PWM chip 238 - * @chip: the PWM chip to remove 239 - * 240 - * Removes a PWM chip. 241 - */ 242 - void pwmchip_remove(struct pwm_chip *chip) 243 - { 244 - pwmchip_sysfs_unexport(chip); 245 - 246 - if (IS_ENABLED(CONFIG_OF)) 247 - of_pwmchip_remove(chip); 248 - 249 - mutex_lock(&pwm_lock); 250 - 251 - idr_remove(&pwm_chips, chip->id); 252 - 253 - mutex_unlock(&pwm_lock); 254 - 255 - kfree(chip->pwms); 256 - } 257 - EXPORT_SYMBOL_GPL(pwmchip_remove); 258 - 259 - static void devm_pwmchip_remove(void *data) 260 - { 261 - struct pwm_chip *chip = data; 262 - 263 - pwmchip_remove(chip); 264 - } 265 - 266 - int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner) 267 - { 268 - int ret; 269 - 270 - ret = __pwmchip_add(chip, owner); 271 - if (ret) 272 - return ret; 273 - 274 - return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip); 275 - } 276 - EXPORT_SYMBOL_GPL(__devm_pwmchip_add); 277 - 278 - /** 279 - * pwm_request_from_chip() - request a PWM device relative to a PWM chip 280 - * @chip: PWM chip 281 - * @index: per-chip index of the PWM to request 282 - * @label: a literal description string of this PWM 283 - * 284 - * Returns: A pointer to the PWM device at the given index of the given PWM 285 - * chip. A negative error code is returned if the index is not valid for the 286 - * specified PWM chip or if the PWM device cannot be requested. 287 - */ 288 - struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, 289 - unsigned int index, 290 - const char *label) 291 - { 292 - struct pwm_device *pwm; 293 - int err; 294 - 295 - if (!chip || index >= chip->npwm) 296 - return ERR_PTR(-EINVAL); 297 - 298 - mutex_lock(&pwm_lock); 299 - pwm = &chip->pwms[index]; 300 - 301 - err = pwm_device_request(pwm, label); 302 - if (err < 0) 303 - pwm = ERR_PTR(err); 304 - 305 - mutex_unlock(&pwm_lock); 306 - return pwm; 307 - } 308 - EXPORT_SYMBOL_GPL(pwm_request_from_chip); 309 31 310 32 static void pwm_apply_debug(struct pwm_device *pwm, 311 33 const struct pwm_state *state) ··· 225 503 EXPORT_SYMBOL_GPL(pwm_apply_atomic); 226 504 227 505 /** 228 - * pwm_capture() - capture and report a PWM signal 229 - * @pwm: PWM device 230 - * @result: structure to fill with capture result 231 - * @timeout: time to wait, in milliseconds, before giving up on capture 232 - * 233 - * Returns: 0 on success or a negative error code on failure. 234 - */ 235 - int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, 236 - unsigned long timeout) 237 - { 238 - int err; 239 - 240 - if (!pwm || !pwm->chip->ops) 241 - return -EINVAL; 242 - 243 - if (!pwm->chip->ops->capture) 244 - return -ENOSYS; 245 - 246 - mutex_lock(&pwm_lock); 247 - err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout); 248 - mutex_unlock(&pwm_lock); 249 - 250 - return err; 251 - } 252 - EXPORT_SYMBOL_GPL(pwm_capture); 253 - 254 - /** 255 506 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments 256 507 * @pwm: PWM device 257 508 * ··· 280 585 } 281 586 EXPORT_SYMBOL_GPL(pwm_adjust_config); 282 587 283 - static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode) 588 + /** 589 + * pwm_capture() - capture and report a PWM signal 590 + * @pwm: PWM device 591 + * @result: structure to fill with capture result 592 + * @timeout: time to wait, in milliseconds, before giving up on capture 593 + * 594 + * Returns: 0 on success or a negative error code on failure. 595 + */ 596 + int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, 597 + unsigned long timeout) 598 + { 599 + int err; 600 + 601 + if (!pwm || !pwm->chip->ops) 602 + return -EINVAL; 603 + 604 + if (!pwm->chip->ops->capture) 605 + return -ENOSYS; 606 + 607 + mutex_lock(&pwm_lock); 608 + err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout); 609 + mutex_unlock(&pwm_lock); 610 + 611 + return err; 612 + } 613 + EXPORT_SYMBOL_GPL(pwm_capture); 614 + 615 + static struct pwm_chip *pwmchip_find_by_name(const char *name) 284 616 { 285 617 struct pwm_chip *chip; 286 618 unsigned long id, tmp; 287 619 620 + if (!name) 621 + return NULL; 622 + 288 623 mutex_lock(&pwm_lock); 289 624 290 - idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) 291 - if (chip->dev && device_match_fwnode(chip->dev, fwnode)) { 625 + idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) { 626 + const char *chip_name = dev_name(chip->dev); 627 + 628 + if (chip_name && strcmp(chip_name, name) == 0) { 292 629 mutex_unlock(&pwm_lock); 293 630 return chip; 294 631 } 632 + } 295 633 296 634 mutex_unlock(&pwm_lock); 297 635 298 - return ERR_PTR(-EPROBE_DEFER); 636 + return NULL; 299 637 } 638 + 639 + static int pwm_device_request(struct pwm_device *pwm, const char *label) 640 + { 641 + int err; 642 + struct pwm_chip *chip = pwm->chip; 643 + const struct pwm_ops *ops = chip->ops; 644 + 645 + if (test_bit(PWMF_REQUESTED, &pwm->flags)) 646 + return -EBUSY; 647 + 648 + if (!try_module_get(chip->owner)) 649 + return -ENODEV; 650 + 651 + if (ops->request) { 652 + err = ops->request(chip, pwm); 653 + if (err) { 654 + module_put(chip->owner); 655 + return err; 656 + } 657 + } 658 + 659 + if (ops->get_state) { 660 + /* 661 + * Zero-initialize state because most drivers are unaware of 662 + * .usage_power. The other members of state are supposed to be 663 + * set by lowlevel drivers. We still initialize the whole 664 + * structure for simplicity even though this might paper over 665 + * faulty implementations of .get_state(). 666 + */ 667 + struct pwm_state state = { 0, }; 668 + 669 + err = ops->get_state(chip, pwm, &state); 670 + trace_pwm_get(pwm, &state, err); 671 + 672 + if (!err) 673 + pwm->state = state; 674 + 675 + if (IS_ENABLED(CONFIG_PWM_DEBUG)) 676 + pwm->last = pwm->state; 677 + } 678 + 679 + set_bit(PWMF_REQUESTED, &pwm->flags); 680 + pwm->label = label; 681 + 682 + return 0; 683 + } 684 + 685 + /** 686 + * pwm_request_from_chip() - request a PWM device relative to a PWM chip 687 + * @chip: PWM chip 688 + * @index: per-chip index of the PWM to request 689 + * @label: a literal description string of this PWM 690 + * 691 + * Returns: A pointer to the PWM device at the given index of the given PWM 692 + * chip. A negative error code is returned if the index is not valid for the 693 + * specified PWM chip or if the PWM device cannot be requested. 694 + */ 695 + struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, 696 + unsigned int index, 697 + const char *label) 698 + { 699 + struct pwm_device *pwm; 700 + int err; 701 + 702 + if (!chip || index >= chip->npwm) 703 + return ERR_PTR(-EINVAL); 704 + 705 + mutex_lock(&pwm_lock); 706 + pwm = &chip->pwms[index]; 707 + 708 + err = pwm_device_request(pwm, label); 709 + if (err < 0) 710 + pwm = ERR_PTR(err); 711 + 712 + mutex_unlock(&pwm_lock); 713 + return pwm; 714 + } 715 + EXPORT_SYMBOL_GPL(pwm_request_from_chip); 716 + 717 + 718 + struct pwm_device * 719 + of_pwm_xlate_with_flags(struct pwm_chip *chip, const struct of_phandle_args *args) 720 + { 721 + struct pwm_device *pwm; 722 + 723 + /* period in the second cell and flags in the third cell are optional */ 724 + if (args->args_count < 1) 725 + return ERR_PTR(-EINVAL); 726 + 727 + pwm = pwm_request_from_chip(chip, args->args[0], NULL); 728 + if (IS_ERR(pwm)) 729 + return pwm; 730 + 731 + if (args->args_count > 1) 732 + pwm->args.period = args->args[1]; 733 + 734 + pwm->args.polarity = PWM_POLARITY_NORMAL; 735 + if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED) 736 + pwm->args.polarity = PWM_POLARITY_INVERSED; 737 + 738 + return pwm; 739 + } 740 + EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags); 741 + 742 + struct pwm_device * 743 + of_pwm_single_xlate(struct pwm_chip *chip, const struct of_phandle_args *args) 744 + { 745 + struct pwm_device *pwm; 746 + 747 + pwm = pwm_request_from_chip(chip, 0, NULL); 748 + if (IS_ERR(pwm)) 749 + return pwm; 750 + 751 + if (args->args_count > 1) 752 + pwm->args.period = args->args[0]; 753 + 754 + pwm->args.polarity = PWM_POLARITY_NORMAL; 755 + if (args->args_count > 1 && args->args[1] & PWM_POLARITY_INVERTED) 756 + pwm->args.polarity = PWM_POLARITY_INVERSED; 757 + 758 + return pwm; 759 + } 760 + EXPORT_SYMBOL_GPL(of_pwm_single_xlate); 761 + 762 + static void of_pwmchip_add(struct pwm_chip *chip) 763 + { 764 + if (!chip->dev || !chip->dev->of_node) 765 + return; 766 + 767 + if (!chip->of_xlate) 768 + chip->of_xlate = of_pwm_xlate_with_flags; 769 + 770 + of_node_get(chip->dev->of_node); 771 + } 772 + 773 + static void of_pwmchip_remove(struct pwm_chip *chip) 774 + { 775 + if (chip->dev) 776 + of_node_put(chip->dev->of_node); 777 + } 778 + 779 + static bool pwm_ops_check(const struct pwm_chip *chip) 780 + { 781 + const struct pwm_ops *ops = chip->ops; 782 + 783 + if (!ops->apply) 784 + return false; 785 + 786 + if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state) 787 + dev_warn(chip->dev, 788 + "Please implement the .get_state() callback\n"); 789 + 790 + return true; 791 + } 792 + 793 + /** 794 + * __pwmchip_add() - register a new PWM chip 795 + * @chip: the PWM chip to add 796 + * @owner: reference to the module providing the chip. 797 + * 798 + * Register a new PWM chip. @owner is supposed to be THIS_MODULE, use the 799 + * pwmchip_add wrapper to do this right. 800 + * 801 + * Returns: 0 on success or a negative error code on failure. 802 + */ 803 + int __pwmchip_add(struct pwm_chip *chip, struct module *owner) 804 + { 805 + unsigned int i; 806 + int ret; 807 + 808 + if (!chip || !chip->dev || !chip->ops || !chip->npwm) 809 + return -EINVAL; 810 + 811 + if (!pwm_ops_check(chip)) 812 + return -EINVAL; 813 + 814 + chip->owner = owner; 815 + 816 + chip->pwms = kcalloc(chip->npwm, sizeof(*chip->pwms), GFP_KERNEL); 817 + if (!chip->pwms) 818 + return -ENOMEM; 819 + 820 + mutex_lock(&pwm_lock); 821 + 822 + ret = idr_alloc(&pwm_chips, chip, 0, 0, GFP_KERNEL); 823 + if (ret < 0) { 824 + mutex_unlock(&pwm_lock); 825 + kfree(chip->pwms); 826 + return ret; 827 + } 828 + 829 + chip->id = ret; 830 + 831 + for (i = 0; i < chip->npwm; i++) { 832 + struct pwm_device *pwm = &chip->pwms[i]; 833 + 834 + pwm->chip = chip; 835 + pwm->hwpwm = i; 836 + } 837 + 838 + mutex_unlock(&pwm_lock); 839 + 840 + if (IS_ENABLED(CONFIG_OF)) 841 + of_pwmchip_add(chip); 842 + 843 + pwmchip_sysfs_export(chip); 844 + 845 + return 0; 846 + } 847 + EXPORT_SYMBOL_GPL(__pwmchip_add); 848 + 849 + /** 850 + * pwmchip_remove() - remove a PWM chip 851 + * @chip: the PWM chip to remove 852 + * 853 + * Removes a PWM chip. 854 + */ 855 + void pwmchip_remove(struct pwm_chip *chip) 856 + { 857 + pwmchip_sysfs_unexport(chip); 858 + 859 + if (IS_ENABLED(CONFIG_OF)) 860 + of_pwmchip_remove(chip); 861 + 862 + mutex_lock(&pwm_lock); 863 + 864 + idr_remove(&pwm_chips, chip->id); 865 + 866 + mutex_unlock(&pwm_lock); 867 + 868 + kfree(chip->pwms); 869 + } 870 + EXPORT_SYMBOL_GPL(pwmchip_remove); 871 + 872 + static void devm_pwmchip_remove(void *data) 873 + { 874 + struct pwm_chip *chip = data; 875 + 876 + pwmchip_remove(chip); 877 + } 878 + 879 + int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner) 880 + { 881 + int ret; 882 + 883 + ret = __pwmchip_add(chip, owner); 884 + if (ret) 885 + return ret; 886 + 887 + return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip); 888 + } 889 + EXPORT_SYMBOL_GPL(__devm_pwmchip_add); 300 890 301 891 static struct device_link *pwm_device_link_add(struct device *dev, 302 892 struct pwm_device *pwm) ··· 607 627 } 608 628 609 629 return dl; 630 + } 631 + 632 + static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode) 633 + { 634 + struct pwm_chip *chip; 635 + unsigned long id, tmp; 636 + 637 + mutex_lock(&pwm_lock); 638 + 639 + idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) 640 + if (chip->dev && device_match_fwnode(chip->dev, fwnode)) { 641 + mutex_unlock(&pwm_lock); 642 + return chip; 643 + } 644 + 645 + mutex_unlock(&pwm_lock); 646 + 647 + return ERR_PTR(-EPROBE_DEFER); 610 648 } 611 649 612 650 /** ··· 760 762 761 763 return pwm; 762 764 } 765 + 766 + static DEFINE_MUTEX(pwm_lookup_lock); 767 + static LIST_HEAD(pwm_lookup_list); 763 768 764 769 /** 765 770 * pwm_add_table() - register PWM device consumers