1 // SPDX-License-Identifier: GPL-2.0-only 2 3 #define pr_fmt(fmt) "generic pinconfig core: " fmt 4 5 #include <linux/array_size.h> 6 #include <linux/device.h> 7 #include <linux/module.h> 8 #include <linux/of.h> 9 #include <linux/slab.h> 10 11 #include <linux/pinctrl/pinconf-generic.h> 12 #include <linux/pinctrl/pinconf.h> 13 #include <linux/pinctrl/pinctrl.h> 14 15 #include "core.h" 16 #include "pinconf.h" 17 #include "pinctrl-utils.h" 18 #include "pinmux.h" 19 20 int pinctrl_generic_to_map(struct pinctrl_dev *pctldev, struct device_node *parent, 21 struct device_node *np, struct pinctrl_map **maps, 22 unsigned int *num_maps, unsigned int *num_reserved_maps, 23 const char **group_names, unsigned int ngroups, 24 void *data, unsigned int *pins, unsigned int npins) 25 { 26 struct device *dev = pctldev->dev; 27 unsigned int num_configs; 28 const char *group_name; 29 unsigned long *configs; 30 int ret, reserve = 1; 31 32 group_name = devm_kasprintf(dev, GFP_KERNEL, "%pOFn.%pOFn", parent, np); 33 if (!group_name) 34 return -ENOMEM; 35 36 group_names[ngroups] = group_name; 37 38 ret = pinctrl_utils_reserve_map(pctldev, maps, num_reserved_maps, num_maps, reserve); 39 if (ret) 40 return ret; 41 42 ret = pinctrl_utils_add_map_mux(pctldev, maps, num_reserved_maps, num_maps, group_name, 43 parent->name); 44 if (ret < 0) 45 return ret; 46 47 ret = pinctrl_generic_add_group(pctldev, group_name, pins, npins, data); 48 if (ret < 0) 49 return dev_err_probe(dev, ret, "failed to add group %s: %d\n", 50 group_name, ret); 51 52 ret = pinconf_generic_parse_dt_config(np, pctldev, &configs, &num_configs); 53 if (ret) 54 return dev_err_probe(dev, ret, "failed to parse pin config of group %s\n", 55 group_name); 56 57 if (num_configs == 0) 58 return 0; 59 60 ret = pinctrl_utils_reserve_map(pctldev, maps, num_reserved_maps, num_maps, reserve); 61 if (ret) 62 return ret; 63 64 ret = pinctrl_utils_add_map_configs(pctldev, maps, num_reserved_maps, num_maps, group_name, 65 configs, 66 num_configs, PIN_MAP_TYPE_CONFIGS_GROUP); 67 kfree(configs); 68 if (ret) 69 return ret; 70 71 return 0; 72 }; 73 EXPORT_SYMBOL_GPL(pinctrl_generic_to_map); 74 75 static int pinctrl_generic_pins_function_dt_subnode_to_map(struct pinctrl_dev *pctldev, 76 struct device_node *parent, 77 struct device_node *np, 78 struct pinctrl_map **maps, 79 unsigned int *num_maps, 80 unsigned int *num_reserved_maps, 81 const char **group_names, 82 unsigned int ngroups) 83 { 84 struct device *dev = pctldev->dev; 85 unsigned int pin, *pins; 86 const char **functions; 87 int npins, ret; 88 89 npins = of_property_count_u32_elems(np, "pins"); 90 91 if (npins < 1) { 92 dev_err(dev, "invalid pinctrl group %pOFn.%pOFn %d\n", 93 parent, np, npins); 94 return npins; 95 } 96 97 pins = devm_kcalloc(dev, npins, sizeof(*pins), GFP_KERNEL); 98 if (!pins) 99 return -ENOMEM; 100 101 functions = devm_kcalloc(dev, npins, sizeof(*functions), GFP_KERNEL); 102 if (!functions) 103 return -ENOMEM; 104 105 for (int i = 0; i < npins; i++) { 106 ret = of_property_read_u32_index(np, "pins", i, &pin); 107 if (ret) 108 return ret; 109 110 pins[i] = pin; 111 112 ret = of_property_read_string(np, "function", &functions[i]); 113 if (ret) 114 return ret; 115 } 116 117 return pinctrl_generic_to_map(pctldev, parent, np, maps, num_maps, 118 num_reserved_maps, group_names, ngroups, 119 functions, pins, npins); 120 } 121 122 static int pinctrl_generic_pinmux_dt_subnode_to_map(struct pinctrl_dev *pctldev, 123 struct device_node *parent, 124 struct device_node *np, 125 struct pinctrl_map **maps, 126 unsigned int *num_maps, 127 unsigned int *num_reserved_maps, 128 const char **group_names, 129 unsigned int ngroups) 130 { 131 struct device *dev = pctldev->dev; 132 unsigned int *pins, *muxes; 133 int npins, ret; 134 135 npins = of_property_count_u32_elems(np, "pinmux"); 136 137 if (npins < 1) { 138 dev_err(dev, "invalid pinctrl group %pOFn.%pOFn %d\n", 139 parent, np, npins); 140 return npins; 141 } 142 143 pins = devm_kcalloc(dev, npins, sizeof(*pins), GFP_KERNEL); 144 if (!pins) 145 return -ENOMEM; 146 147 muxes = devm_kcalloc(dev, npins, sizeof(*muxes), GFP_KERNEL); 148 if (!muxes) 149 return -ENOMEM; 150 151 for (int i = 0; i < npins; i++) { 152 unsigned int pinmux; 153 154 ret = of_property_read_u32_index(np, "pinmux", i, &pinmux); 155 if (ret) 156 return ret; 157 158 pins[i] = pinmux >> 16; 159 muxes[i] = pinmux & GENMASK(15, 0); 160 } 161 162 return pinctrl_generic_to_map(pctldev, parent, np, maps, num_maps, 163 num_reserved_maps, group_names, ngroups, 164 muxes, pins, npins); 165 } 166 167 static int pinctrl_generic_dt_node_to_map(struct pinctrl_dev *pctldev, 168 struct device_node *np, 169 struct pinctrl_map **maps, 170 unsigned int *num_maps, 171 int (dt_subnode_to_map)( 172 struct pinctrl_dev *, 173 struct device_node *, 174 struct device_node *, 175 struct pinctrl_map **, 176 unsigned int *, 177 unsigned int *, 178 const char **, 179 unsigned int)) 180 { 181 struct device *dev = pctldev->dev; 182 struct device_node *child_np; 183 const char **group_names; 184 unsigned int num_reserved_maps = 0; 185 int ngroups = 0; 186 int ret; 187 188 *maps = NULL; 189 *num_maps = 0; 190 191 /* 192 * Check if this is actually the pins node, or a parent containing 193 * multiple pins nodes. 194 */ 195 if (!of_property_present(np, "pins")) 196 goto parent; 197 198 group_names = devm_kcalloc(dev, 1, sizeof(*group_names), GFP_KERNEL); 199 if (!group_names) 200 return -ENOMEM; 201 202 ret = dt_subnode_to_map(pctldev, np, np, maps, num_maps, 203 &num_reserved_maps, group_names, ngroups); 204 if (ret) { 205 pinctrl_utils_free_map(pctldev, *maps, *num_maps); 206 return dev_err_probe(dev, ret, "error figuring out mappings for %s\n", np->name); 207 } 208 209 ret = pinmux_generic_add_function(pctldev, np->name, group_names, 1, NULL); 210 if (ret < 0) { 211 pinctrl_utils_free_map(pctldev, *maps, *num_maps); 212 return dev_err_probe(dev, ret, "error adding function %s\n", np->name); 213 } 214 215 return 0; 216 217 parent: 218 for_each_available_child_of_node(np, child_np) 219 ngroups += 1; 220 221 group_names = devm_kcalloc(dev, ngroups, sizeof(*group_names), GFP_KERNEL); 222 if (!group_names) 223 return -ENOMEM; 224 225 ngroups = 0; 226 for_each_available_child_of_node_scoped(np, child_np) { 227 ret = dt_subnode_to_map(pctldev, np, child_np, maps, num_maps, 228 &num_reserved_maps, group_names, ngroups); 229 if (ret) { 230 pinctrl_utils_free_map(pctldev, *maps, *num_maps); 231 return dev_err_probe(dev, ret, "error figuring out mappings for %s\n", 232 np->name); 233 } 234 235 ngroups++; 236 } 237 238 ret = pinmux_generic_add_function(pctldev, np->name, group_names, ngroups, NULL); 239 if (ret < 0) { 240 pinctrl_utils_free_map(pctldev, *maps, *num_maps); 241 return dev_err_probe(dev, ret, "error adding function %s\n", np->name); 242 } 243 244 return 0; 245 } 246 247 /* 248 * For platforms that do not define groups or functions in the driver, but 249 * instead use the devicetree to describe them. This function will, unlike 250 * pinconf_generic_dt_node_to_map() etc which rely on driver defined groups 251 * and functions, create them in addition to parsing pinconf properties and 252 * adding mappings. 253 */ 254 int pinctrl_generic_pins_function_dt_node_to_map(struct pinctrl_dev *pctldev, 255 struct device_node *np, 256 struct pinctrl_map **maps, 257 unsigned int *num_maps) 258 { 259 return pinctrl_generic_dt_node_to_map(pctldev, np, maps, num_maps, 260 &pinctrl_generic_pins_function_dt_subnode_to_map); 261 } 262 EXPORT_SYMBOL_GPL(pinctrl_generic_pins_function_dt_node_to_map); 263 264 /* 265 * For platforms that do not define groups or functions in the driver, but 266 * instead use the devicetree to describe them. This function will, unlike 267 * pinconf_generic_dt_node_to_map() etc which rely on driver defined groups 268 * and functions, create them in addition to parsing pinconf properties and 269 * adding mappings. 270 * 271 * It assumes that the upper 16 bits of the pinmux items contain the pin 272 * and the lower 16 the mux setting. 273 */ 274 int pinctrl_generic_pinmux_dt_node_to_map(struct pinctrl_dev *pctldev, 275 struct device_node *np, 276 struct pinctrl_map **maps, 277 unsigned int *num_maps) 278 { 279 return pinctrl_generic_dt_node_to_map(pctldev, np, maps, num_maps, 280 &pinctrl_generic_pinmux_dt_subnode_to_map); 281 }; 282 EXPORT_SYMBOL_GPL(pinctrl_generic_pinmux_dt_node_to_map); 283