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

pinctrl: implement devm_pinctrl_get()/put()

These functions allow the driver core to automatically clean up any
allocations made by drivers, thus leading to simplified drivers.

Signed-off-by: Stephen Warren <swarren@nvidia.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>

authored by

Stephen Warren and committed by
Linus Walleij
6d4ca1fb 2aeefe02

+133 -19
+4
Documentation/driver-model/devres.txt
··· 276 276 devm_regulator_get() 277 277 devm_regulator_put() 278 278 devm_regulator_bulk_get() 279 + 280 + PINCTRL 281 + devm_pinctrl_get() 282 + devm_pinctrl_put()
+29 -19
Documentation/pinctrl.txt
··· 945 945 The result of grabbing this mapping from the device with something like 946 946 this (see next paragraph): 947 947 948 - p = pinctrl_get(dev); 948 + p = devm_pinctrl_get(dev); 949 949 s = pinctrl_lookup_state(p, "8bit"); 950 950 ret = pinctrl_select_state(p, s); 951 951 952 952 or more simply: 953 953 954 - p = pinctrl_get_select(dev, "8bit"); 954 + p = devm_pinctrl_get_select(dev, "8bit"); 955 955 956 956 Will be that you activate all the three bottom records in the mapping at 957 957 once. Since they share the same name, pin controller device, function and ··· 985 985 /* Allocate a state holder named "foo" etc */ 986 986 struct foo_state *foo = ...; 987 987 988 - foo->p = pinctrl_get(&device); 988 + foo->p = devm_pinctrl_get(&device); 989 989 if (IS_ERR(foo->p)) { 990 990 /* FIXME: clean up "foo" here */ 991 991 return PTR_ERR(foo->p); ··· 993 993 994 994 foo->s = pinctrl_lookup_state(foo->p, PINCTRL_STATE_DEFAULT); 995 995 if (IS_ERR(foo->s)) { 996 - pinctrl_put(foo->p); 997 996 /* FIXME: clean up "foo" here */ 998 997 return PTR_ERR(s); 999 998 } 1000 999 1001 1000 ret = pinctrl_select_state(foo->s); 1002 1001 if (ret < 0) { 1003 - pinctrl_put(foo->p); 1004 1002 /* FIXME: clean up "foo" here */ 1005 1003 return ret; 1006 1004 } 1007 - } 1008 - 1009 - foo_remove() 1010 - { 1011 - pinctrl_put(state->p); 1012 1005 } 1013 1006 1014 1007 This get/lookup/select/put sequence can just as well be handled by bus drivers ··· 1015 1022 kernel memory to hold the pinmux state. All mapping table parsing or similar 1016 1023 slow operations take place within this API. 1017 1024 1025 + - devm_pinctrl_get() is a variant of pinctrl_get() that causes pinctrl_put() 1026 + to be called automatically on the retrieved pointer when the associated 1027 + device is removed. It is recommended to use this function over plain 1028 + pinctrl_get(). 1029 + 1018 1030 - pinctrl_lookup_state() is called in process context to obtain a handle to a 1019 1031 specific state for a the client device. This operation may be slow too. 1020 1032 ··· 1032 1034 1033 1035 - pinctrl_put() frees all information associated with a pinctrl handle. 1034 1036 1037 + - devm_pinctrl_put() is a variant of pinctrl_put() that may be used to 1038 + explicitly destroy a pinctrl object returned by devm_pinctrl_get(). 1039 + However, use of this function will be rare, due to the automatic cleanup 1040 + that will occur even without calling it. 1041 + 1042 + pinctrl_get() must be paired with a plain pinctrl_put(). 1043 + pinctrl_get() may not be paired with devm_pinctrl_put(). 1044 + devm_pinctrl_get() can optionally be paired with devm_pinctrl_put(). 1045 + devm_pinctrl_get() may not be paired with plain pinctrl_put(). 1046 + 1035 1047 Usually the pin control core handled the get/put pair and call out to the 1036 1048 device drivers bookkeeping operations, like checking available functions and 1037 1049 the associated pins, whereas the enable/disable pass on to the pin controller 1038 1050 driver which takes care of activating and/or deactivating the mux setting by 1039 1051 quickly poking some registers. 1040 1052 1041 - The pins are allocated for your device when you issue the pinctrl_get() call, 1042 - after this you should be able to see this in the debugfs listing of all pins. 1053 + The pins are allocated for your device when you issue the devm_pinctrl_get() 1054 + call, after this you should be able to see this in the debugfs listing of all 1055 + pins. 1043 1056 1044 1057 NOTE: the pinctrl system will return -EPROBE_DEFER if it cannot find the 1045 1058 requested pinctrl handles, for example if the pinctrl driver has not yet ··· 1101 1092 1102 1093 #include <linux/pinctrl/consumer.h> 1103 1094 1104 - foo_switch() 1105 - { 1106 - struct pinctrl *p; 1107 - struct pinctrl_state *s1, *s2; 1095 + struct pinctrl *p; 1096 + struct pinctrl_state *s1, *s2; 1108 1097 1098 + foo_probe() 1099 + { 1109 1100 /* Setup */ 1110 - p = pinctrl_get(&device); 1101 + p = devm_pinctrl_get(&device); 1111 1102 if (IS_ERR(p)) 1112 1103 ... 1113 1104 ··· 1118 1109 s2 = pinctrl_lookup_state(foo->p, "pos-B"); 1119 1110 if (IS_ERR(s2)) 1120 1111 ... 1112 + } 1121 1113 1114 + foo_switch() 1115 + { 1122 1116 /* Enable on position A */ 1123 1117 ret = pinctrl_select_state(s1); 1124 1118 if (ret < 0) ··· 1135 1123 ... 1136 1124 1137 1125 ... 1138 - 1139 - pinctrl_put(p); 1140 1126 } 1141 1127 1142 1128 The above has to be done from process context.
+56
drivers/pinctrl/core.c
··· 23 23 #include <linux/sysfs.h> 24 24 #include <linux/debugfs.h> 25 25 #include <linux/seq_file.h> 26 + #include <linux/pinctrl/consumer.h> 26 27 #include <linux/pinctrl/pinctrl.h> 27 28 #include <linux/pinctrl/machine.h> 28 29 #include "core.h" ··· 801 800 return ret; 802 801 } 803 802 EXPORT_SYMBOL_GPL(pinctrl_select_state); 803 + 804 + static void devm_pinctrl_release(struct device *dev, void *res) 805 + { 806 + pinctrl_put(*(struct pinctrl **)res); 807 + } 808 + 809 + /** 810 + * struct devm_pinctrl_get() - Resource managed pinctrl_get() 811 + * @dev: the device to obtain the handle for 812 + * 813 + * If there is a need to explicitly destroy the returned struct pinctrl, 814 + * devm_pinctrl_put() should be used, rather than plain pinctrl_put(). 815 + */ 816 + struct pinctrl *devm_pinctrl_get(struct device *dev) 817 + { 818 + struct pinctrl **ptr, *p; 819 + 820 + ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL); 821 + if (!ptr) 822 + return ERR_PTR(-ENOMEM); 823 + 824 + p = pinctrl_get(dev); 825 + if (!IS_ERR(p)) { 826 + *ptr = p; 827 + devres_add(dev, ptr); 828 + } else { 829 + devres_free(ptr); 830 + } 831 + 832 + return p; 833 + } 834 + EXPORT_SYMBOL_GPL(devm_pinctrl_get); 835 + 836 + static int devm_pinctrl_match(struct device *dev, void *res, void *data) 837 + { 838 + struct pinctrl **p = res; 839 + 840 + return *p == data; 841 + } 842 + 843 + /** 844 + * devm_pinctrl_put() - Resource managed pinctrl_put() 845 + * @p: the pinctrl handle to release 846 + * 847 + * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally 848 + * this function will not need to be called and the resource management 849 + * code will ensure that the resource is freed. 850 + */ 851 + void devm_pinctrl_put(struct pinctrl *p) 852 + { 853 + WARN_ON(devres_destroy(p->dev, devm_pinctrl_release, 854 + devm_pinctrl_match, p)); 855 + pinctrl_put(p); 856 + } 857 + EXPORT_SYMBOL_GPL(devm_pinctrl_put); 804 858 805 859 int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps, 806 860 bool dup, bool locked)
+44
include/linux/pinctrl/consumer.h
··· 36 36 const char *name); 37 37 extern int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *s); 38 38 39 + extern struct pinctrl * __must_check devm_pinctrl_get(struct device *dev); 40 + extern void devm_pinctrl_put(struct pinctrl *p); 41 + 39 42 #else /* !CONFIG_PINCTRL */ 40 43 41 44 static inline int pinctrl_request_gpio(unsigned gpio) ··· 82 79 return 0; 83 80 } 84 81 82 + static inline struct pinctrl * __must_check devm_pinctrl_get(struct device *dev) 83 + { 84 + return NULL; 85 + } 86 + 87 + static inline void devm_pinctrl_put(struct pinctrl *p) 88 + { 89 + } 90 + 85 91 #endif /* CONFIG_PINCTRL */ 86 92 87 93 static inline struct pinctrl * __must_check pinctrl_get_select( ··· 123 111 struct device *dev) 124 112 { 125 113 return pinctrl_get_select(dev, PINCTRL_STATE_DEFAULT); 114 + } 115 + 116 + static inline struct pinctrl * __must_check devm_pinctrl_get_select( 117 + struct device *dev, const char *name) 118 + { 119 + struct pinctrl *p; 120 + struct pinctrl_state *s; 121 + int ret; 122 + 123 + p = devm_pinctrl_get(dev); 124 + if (IS_ERR(p)) 125 + return p; 126 + 127 + s = pinctrl_lookup_state(p, name); 128 + if (IS_ERR(s)) { 129 + devm_pinctrl_put(p); 130 + return ERR_PTR(PTR_ERR(s)); 131 + } 132 + 133 + ret = pinctrl_select_state(p, s); 134 + if (ret < 0) { 135 + devm_pinctrl_put(p); 136 + return ERR_PTR(ret); 137 + } 138 + 139 + return p; 140 + } 141 + 142 + static inline struct pinctrl * __must_check devm_pinctrl_get_select_default( 143 + struct device *dev) 144 + { 145 + return devm_pinctrl_get_select(dev, PINCTRL_STATE_DEFAULT); 126 146 } 127 147 128 148 #ifdef CONFIG_PINCONF