| 1 | |
| 2 | |
| 3 | * Device tree integration for the pin control subsystem
|
| 4 | *
|
| 5 | * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
|
| 6 |
|
| 7 |
|
| 8 | #include <linux/device.h>
|
| 9 | #include <linux/of.h>
|
| 10 | #include <linux/pinctrl/pinctrl.h>
|
| 11 | #include <linux/slab.h>
|
| 12 |
|
| 13 | #include "core.h"
|
| 14 | #include "devicetree.h"
|
| 15 |
|
| 16 | |
| 17 | * struct pinctrl_dt_map - mapping table chunk parsed from device tree
|
| 18 | * @node: list node for struct pinctrl's @dt_maps field
|
| 19 | * @pctldev: the pin controller that allocated this struct, and will free it
|
| 20 | * @map: the mapping table entries
|
| 21 | * @num_maps: number of mapping table entries
|
| 22 |
|
| 23 | struct pinctrl_dt_map {
|
| 24 | struct list_head node;
|
| 25 | struct pinctrl_dev *pctldev;
|
| 26 | struct pinctrl_map *map;
|
| 27 | unsigned int num_maps;
|
| 28 | };
|
| 29 |
|
| 30 | static void dt_free_map(struct pinctrl_dev *pctldev,
|
| 31 | struct pinctrl_map *map, unsigned int num_maps)
|
| 32 | {
|
| 33 | int i;
|
| 34 |
|
| 35 | for (i = 0; i < num_maps; ++i) {
|
| 36 | kfree_const(map[i].dev_name);
|
| 37 | map[i].dev_name = NULL;
|
| 38 | }
|
| 39 |
|
| 40 | if (pctldev) {
|
| 41 | const struct pinctrl_ops *ops = pctldev->desc->pctlops;
|
| 42 | if (ops->dt_free_map)
|
| 43 | ops->dt_free_map(pctldev, map, num_maps);
|
| 44 | } else {
|
| 45 |
|
| 46 | kfree(map);
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | void pinctrl_dt_free_maps(struct pinctrl *p)
|
| 51 | {
|
| 52 | struct pinctrl_dt_map *dt_map, *n1;
|
| 53 |
|
| 54 | list_for_each_entry_safe(dt_map, n1, &p->dt_maps, node) {
|
| 55 | pinctrl_unregister_mappings(dt_map->map);
|
| 56 | list_del(&dt_map->node);
|
| 57 | dt_free_map(dt_map->pctldev, dt_map->map,
|
| 58 | dt_map->num_maps);
|
| 59 | kfree(dt_map);
|
| 60 | }
|
| 61 |
|
| 62 | of_node_put(p->dev->of_node);
|
| 63 | }
|
| 64 |
|
| 65 | static int dt_remember_or_free_map(struct pinctrl *p, const char *statename,
|
| 66 | struct pinctrl_dev *pctldev,
|
| 67 | struct pinctrl_map *map, unsigned int num_maps)
|
| 68 | {
|
| 69 | int i;
|
| 70 | struct pinctrl_dt_map *dt_map;
|
| 71 |
|
| 72 |
|
| 73 | for (i = 0; i < num_maps; i++)
|
| 74 | map[i].dev_name = NULL;
|
| 75 |
|
| 76 |
|
| 77 | for (i = 0; i < num_maps; i++) {
|
| 78 | const char *devname;
|
| 79 |
|
| 80 | devname = kstrdup_const(dev_name(p->dev), GFP_KERNEL);
|
| 81 | if (!devname)
|
| 82 | goto err_free_map;
|
| 83 |
|
| 84 | map[i].dev_name = devname;
|
| 85 | map[i].name = statename;
|
| 86 | if (pctldev)
|
| 87 | map[i].ctrl_dev_name = dev_name(pctldev->dev);
|
| 88 | }
|
| 89 |
|
| 90 |
|
| 91 | dt_map = kzalloc_obj(*dt_map);
|
| 92 | if (!dt_map)
|
| 93 | goto err_free_map;
|
| 94 |
|
| 95 | dt_map->pctldev = pctldev;
|
| 96 | dt_map->map = map;
|
| 97 | dt_map->num_maps = num_maps;
|
| 98 | list_add_tail(&dt_map->node, &p->dt_maps);
|
| 99 |
|
| 100 | return pinctrl_register_mappings(map, num_maps);
|
| 101 |
|
| 102 | err_free_map:
|
| 103 | dt_free_map(pctldev, map, num_maps);
|
| 104 | return -ENOMEM;
|
| 105 | }
|
| 106 |
|
| 107 | struct pinctrl_dev *of_pinctrl_get(struct device_node *np)
|
| 108 | {
|
| 109 | return get_pinctrl_dev_from_of_node(np);
|
| 110 | }
|
| 111 | EXPORT_SYMBOL_GPL(of_pinctrl_get);
|
| 112 |
|
| 113 | static int dt_to_map_one_config(struct pinctrl *p,
|
| 114 | struct pinctrl_dev *hog_pctldev,
|
| 115 | const char *statename,
|
| 116 | struct device_node *np_config)
|
| 117 | {
|
| 118 | struct pinctrl_dev *pctldev = NULL;
|
| 119 | struct device_node *np_pctldev;
|
| 120 | const struct pinctrl_ops *ops;
|
| 121 | int ret;
|
| 122 | struct pinctrl_map *map;
|
| 123 | unsigned int num_maps;
|
| 124 | bool allow_default = false;
|
| 125 |
|
| 126 |
|
| 127 | np_pctldev = of_node_get(np_config);
|
| 128 | for (;;) {
|
| 129 | if (!allow_default)
|
| 130 | allow_default = of_property_read_bool(np_pctldev,
|
| 131 | "pinctrl-use-default");
|
| 132 |
|
| 133 | np_pctldev = of_get_next_parent(np_pctldev);
|
| 134 | if (!np_pctldev || of_node_is_root(np_pctldev)) {
|
| 135 | of_node_put(np_pctldev);
|
| 136 | ret = -ENODEV;
|
| 137 |
|
| 138 | if (IS_ENABLED(CONFIG_MODULES) && !allow_default && ret < 0)
|
| 139 | ret = -EPROBE_DEFER;
|
| 140 | return ret;
|
| 141 | }
|
| 142 |
|
| 143 | if (hog_pctldev && (np_pctldev == p->dev->of_node)) {
|
| 144 | pctldev = hog_pctldev;
|
| 145 | break;
|
| 146 | }
|
| 147 | pctldev = get_pinctrl_dev_from_of_node(np_pctldev);
|
| 148 | if (pctldev)
|
| 149 | break;
|
| 150 | |
| 151 | * Do not defer probing of hogs (circular loop)
|
| 152 | *
|
| 153 | * Return 1 to let the caller catch the case.
|
| 154 |
|
| 155 | if (np_pctldev == p->dev->of_node) {
|
| 156 | of_node_put(np_pctldev);
|
| 157 | return 1;
|
| 158 | }
|
| 159 | }
|
| 160 | of_node_put(np_pctldev);
|
| 161 |
|
| 162 | |
| 163 | * Call pinctrl driver to parse device tree node, and
|
| 164 | * generate mapping table entries
|
| 165 |
|
| 166 | ops = pctldev->desc->pctlops;
|
| 167 | if (!ops->dt_node_to_map) {
|
| 168 | dev_err(p->dev, "pctldev %s doesn't support DT\n",
|
| 169 | dev_name(pctldev->dev));
|
| 170 | return -ENODEV;
|
| 171 | }
|
| 172 | ret = ops->dt_node_to_map(pctldev, np_config, &map, &num_maps);
|
| 173 | if (ret < 0)
|
| 174 | return ret;
|
| 175 | else if (num_maps == 0) {
|
| 176 | |
| 177 | * If we have no valid maps (maybe caused by empty pinctrl node
|
| 178 | * or typing error) ther is no need remember this, so just
|
| 179 | * return.
|
| 180 |
|
| 181 | dev_info(p->dev,
|
| 182 | "there are no valid maps for state %s\n", statename);
|
| 183 | return 0;
|
| 184 | }
|
| 185 |
|
| 186 |
|
| 187 | return dt_remember_or_free_map(p, statename, pctldev, map, num_maps);
|
| 188 | }
|
| 189 |
|
| 190 | static int dt_remember_dummy_state(struct pinctrl *p, const char *statename)
|
| 191 | {
|
| 192 | struct pinctrl_map *map;
|
| 193 |
|
| 194 | map = kzalloc_obj(*map);
|
| 195 | if (!map)
|
| 196 | return -ENOMEM;
|
| 197 |
|
| 198 |
|
| 199 | map->type = PIN_MAP_TYPE_DUMMY_STATE;
|
| 200 |
|
| 201 | return dt_remember_or_free_map(p, statename, NULL, map, 1);
|
| 202 | }
|
| 203 |
|
| 204 | int pinctrl_dt_to_map(struct pinctrl *p, struct pinctrl_dev *pctldev)
|
| 205 | {
|
| 206 | struct device_node *np = p->dev->of_node;
|
| 207 | int state, ret;
|
| 208 | char *propname;
|
| 209 | struct property *prop;
|
| 210 | const char *statename;
|
| 211 | const __be32 *list;
|
| 212 | int size, config;
|
| 213 | phandle phandle;
|
| 214 | struct device_node *np_config;
|
| 215 |
|
| 216 |
|
| 217 | if (!np) {
|
| 218 | if (of_have_populated_dt())
|
| 219 | dev_dbg(p->dev,
|
| 220 | "no of_node; not parsing pinctrl DT\n");
|
| 221 | return 0;
|
| 222 | }
|
| 223 |
|
| 224 |
|
| 225 | of_node_get(np);
|
| 226 |
|
| 227 |
|
| 228 | for (state = 0; ; state++) {
|
| 229 |
|
| 230 | propname = kasprintf(GFP_KERNEL, "pinctrl-%d", state);
|
| 231 | if (!propname) {
|
| 232 | ret = -ENOMEM;
|
| 233 | goto err;
|
| 234 | }
|
| 235 | prop = of_find_property(np, propname, &size);
|
| 236 | kfree(propname);
|
| 237 | if (!prop) {
|
| 238 | if (state == 0) {
|
| 239 | ret = -ENODEV;
|
| 240 | goto err;
|
| 241 | }
|
| 242 | break;
|
| 243 | }
|
| 244 | list = prop->value;
|
| 245 | size /= sizeof(*list);
|
| 246 |
|
| 247 |
|
| 248 | ret = of_property_read_string_index(np, "pinctrl-names",
|
| 249 | state, &statename);
|
| 250 | |
| 251 | * If not, statename is just the integer state ID. But rather
|
| 252 | * than dynamically allocate it and have to free it later,
|
| 253 | * just point part way into the property name for the string.
|
| 254 |
|
| 255 | if (ret < 0)
|
| 256 | statename = prop->name + strlen("pinctrl-");
|
| 257 |
|
| 258 |
|
| 259 | for (config = 0; config < size; config++) {
|
| 260 | phandle = be32_to_cpup(list++);
|
| 261 |
|
| 262 |
|
| 263 | np_config = of_find_node_by_phandle(phandle);
|
| 264 | if (!np_config) {
|
| 265 | dev_err(p->dev,
|
| 266 | "prop %s index %i invalid phandle\n",
|
| 267 | prop->name, config);
|
| 268 | ret = -EINVAL;
|
| 269 | goto err;
|
| 270 | }
|
| 271 |
|
| 272 |
|
| 273 | ret = dt_to_map_one_config(p, pctldev, statename,
|
| 274 | np_config);
|
| 275 | of_node_put(np_config);
|
| 276 | if (ret == 1)
|
| 277 | continue;
|
| 278 | if (ret < 0)
|
| 279 | goto err;
|
| 280 | }
|
| 281 |
|
| 282 |
|
| 283 | if (!size) {
|
| 284 | ret = dt_remember_dummy_state(p, statename);
|
| 285 | if (ret < 0)
|
| 286 | goto err;
|
| 287 | }
|
| 288 | }
|
| 289 |
|
| 290 | return 0;
|
| 291 |
|
| 292 | err:
|
| 293 | pinctrl_dt_free_maps(p);
|
| 294 | return ret;
|
| 295 | }
|
| 296 |
|
| 297 | |
| 298 | * For pinctrl binding, typically #pinctrl-cells is for the pin controller
|
| 299 | * device, so either parent or grandparent. See pinctrl-bindings.txt.
|
| 300 |
|
| 301 | static int pinctrl_find_cells_size(const struct device_node *np)
|
| 302 | {
|
| 303 | const char *cells_name = "#pinctrl-cells";
|
| 304 | int cells_size, error;
|
| 305 |
|
| 306 | error = of_property_read_u32(np->parent, cells_name, &cells_size);
|
| 307 | if (error) {
|
| 308 | error = of_property_read_u32(np->parent->parent,
|
| 309 | cells_name, &cells_size);
|
| 310 | if (error)
|
| 311 | return -ENOENT;
|
| 312 | }
|
| 313 |
|
| 314 | return cells_size;
|
| 315 | }
|
| 316 |
|
| 317 | |
| 318 | * pinctrl_get_list_and_count - Gets the list and it's cell size and number
|
| 319 | * @np: pointer to device node with the property
|
| 320 | * @list_name: property that contains the list
|
| 321 | * @list: pointer for the list found
|
| 322 | * @cells_size: pointer for the cell size found
|
| 323 | * @nr_elements: pointer for the number of elements found
|
| 324 | *
|
| 325 | * Typically np is a single pinctrl entry containing the list.
|
| 326 |
|
| 327 | static int pinctrl_get_list_and_count(const struct device_node *np,
|
| 328 | const char *list_name,
|
| 329 | const __be32 **list,
|
| 330 | int *cells_size,
|
| 331 | int *nr_elements)
|
| 332 | {
|
| 333 | int size;
|
| 334 |
|
| 335 | *cells_size = 0;
|
| 336 | *nr_elements = 0;
|
| 337 |
|
| 338 | *list = of_get_property(np, list_name, &size);
|
| 339 | if (!*list)
|
| 340 | return -ENOENT;
|
| 341 |
|
| 342 | *cells_size = pinctrl_find_cells_size(np);
|
| 343 | if (*cells_size < 0)
|
| 344 | return -ENOENT;
|
| 345 |
|
| 346 |
|
| 347 | *nr_elements = (size / sizeof(**list)) / (*cells_size + 1);
|
| 348 |
|
| 349 | return 0;
|
| 350 | }
|
| 351 |
|
| 352 | |
| 353 | * pinctrl_count_index_with_args - Count number of elements in a pinctrl entry
|
| 354 | * @np: pointer to device node with the property
|
| 355 | * @list_name: property that contains the list
|
| 356 | *
|
| 357 | * Counts the number of elements in a pinctrl array consisting of an index
|
| 358 | * within the controller and a number of u32 entries specified for each
|
| 359 | * entry. Note that device_node is always for the parent pin controller device.
|
| 360 |
|
| 361 | int pinctrl_count_index_with_args(const struct device_node *np,
|
| 362 | const char *list_name)
|
| 363 | {
|
| 364 | const __be32 *list;
|
| 365 | int size, nr_cells, error;
|
| 366 |
|
| 367 | error = pinctrl_get_list_and_count(np, list_name, &list,
|
| 368 | &nr_cells, &size);
|
| 369 | if (error)
|
| 370 | return error;
|
| 371 |
|
| 372 | return size;
|
| 373 | }
|
| 374 | EXPORT_SYMBOL_GPL(pinctrl_count_index_with_args);
|
| 375 |
|
| 376 | |
| 377 | * pinctrl_copy_args - Populates of_phandle_args based on index
|
| 378 | * @np: pointer to device node with the property
|
| 379 | * @list: pointer to a list with the elements
|
| 380 | * @index: entry within the list of elements
|
| 381 | * @nr_cells: number of cells in the list
|
| 382 | * @nr_elem: number of elements for each entry in the list
|
| 383 | * @out_args: returned values
|
| 384 | *
|
| 385 | * Populates the of_phandle_args based on the index in the list.
|
| 386 |
|
| 387 | static int pinctrl_copy_args(const struct device_node *np,
|
| 388 | const __be32 *list,
|
| 389 | int index, int nr_cells, int nr_elem,
|
| 390 | struct of_phandle_args *out_args)
|
| 391 | {
|
| 392 | int i;
|
| 393 |
|
| 394 | memset(out_args, 0, sizeof(*out_args));
|
| 395 | out_args->np = (struct device_node *)np;
|
| 396 | out_args->args_count = nr_cells + 1;
|
| 397 |
|
| 398 | if (index >= nr_elem)
|
| 399 | return -EINVAL;
|
| 400 |
|
| 401 | list += index * (nr_cells + 1);
|
| 402 |
|
| 403 | for (i = 0; i < nr_cells + 1; i++)
|
| 404 | out_args->args[i] = be32_to_cpup(list++);
|
| 405 |
|
| 406 | return 0;
|
| 407 | }
|
| 408 |
|
| 409 | |
| 410 | * pinctrl_parse_index_with_args - Find a node pointed by index in a list
|
| 411 | * @np: pointer to device node with the property
|
| 412 | * @list_name: property that contains the list
|
| 413 | * @index: index within the list
|
| 414 | * @out_args: entries in the list pointed by index
|
| 415 | *
|
| 416 | * Finds the selected element in a pinctrl array consisting of an index
|
| 417 | * within the controller and a number of u32 entries specified for each
|
| 418 | * entry. Note that device_node is always for the parent pin controller device.
|
| 419 |
|
| 420 | int pinctrl_parse_index_with_args(const struct device_node *np,
|
| 421 | const char *list_name, int index,
|
| 422 | struct of_phandle_args *out_args)
|
| 423 | {
|
| 424 | const __be32 *list;
|
| 425 | int nr_elem, nr_cells, error;
|
| 426 |
|
| 427 | error = pinctrl_get_list_and_count(np, list_name, &list,
|
| 428 | &nr_cells, &nr_elem);
|
| 429 | if (error || !nr_cells)
|
| 430 | return error;
|
| 431 |
|
| 432 | error = pinctrl_copy_args(np, list, index, nr_cells, nr_elem,
|
| 433 | out_args);
|
| 434 | if (error)
|
| 435 | return error;
|
| 436 |
|
| 437 | return 0;
|
| 438 | }
|
| 439 | EXPORT_SYMBOL_GPL(pinctrl_parse_index_with_args);
|