Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Device tree integration for the pin control subsystem
0004  *
0005  * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
0006  */
0007 
0008 #include <linux/device.h>
0009 #include <linux/of.h>
0010 #include <linux/pinctrl/pinctrl.h>
0011 #include <linux/slab.h>
0012 
0013 #include "core.h"
0014 #include "devicetree.h"
0015 
0016 /**
0017  * struct pinctrl_dt_map - mapping table chunk parsed from device tree
0018  * @node: list node for struct pinctrl's @dt_maps field
0019  * @pctldev: the pin controller that allocated this struct, and will free it
0020  * @map: the mapping table entries
0021  * @num_maps: number of mapping table entries
0022  */
0023 struct pinctrl_dt_map {
0024     struct list_head node;
0025     struct pinctrl_dev *pctldev;
0026     struct pinctrl_map *map;
0027     unsigned num_maps;
0028 };
0029 
0030 static void dt_free_map(struct pinctrl_dev *pctldev,
0031              struct pinctrl_map *map, unsigned num_maps)
0032 {
0033     int i;
0034 
0035     for (i = 0; i < num_maps; ++i) {
0036         kfree_const(map[i].dev_name);
0037         map[i].dev_name = NULL;
0038     }
0039 
0040     if (pctldev) {
0041         const struct pinctrl_ops *ops = pctldev->desc->pctlops;
0042         if (ops->dt_free_map)
0043             ops->dt_free_map(pctldev, map, num_maps);
0044     } else {
0045         /* There is no pctldev for PIN_MAP_TYPE_DUMMY_STATE */
0046         kfree(map);
0047     }
0048 }
0049 
0050 void pinctrl_dt_free_maps(struct pinctrl *p)
0051 {
0052     struct pinctrl_dt_map *dt_map, *n1;
0053 
0054     list_for_each_entry_safe(dt_map, n1, &p->dt_maps, node) {
0055         pinctrl_unregister_mappings(dt_map->map);
0056         list_del(&dt_map->node);
0057         dt_free_map(dt_map->pctldev, dt_map->map,
0058                 dt_map->num_maps);
0059         kfree(dt_map);
0060     }
0061 
0062     of_node_put(p->dev->of_node);
0063 }
0064 
0065 static int dt_remember_or_free_map(struct pinctrl *p, const char *statename,
0066                    struct pinctrl_dev *pctldev,
0067                    struct pinctrl_map *map, unsigned num_maps)
0068 {
0069     int i;
0070     struct pinctrl_dt_map *dt_map;
0071 
0072     /* Initialize common mapping table entry fields */
0073     for (i = 0; i < num_maps; i++) {
0074         const char *devname;
0075 
0076         devname = kstrdup_const(dev_name(p->dev), GFP_KERNEL);
0077         if (!devname)
0078             goto err_free_map;
0079 
0080         map[i].dev_name = devname;
0081         map[i].name = statename;
0082         if (pctldev)
0083             map[i].ctrl_dev_name = dev_name(pctldev->dev);
0084     }
0085 
0086     /* Remember the converted mapping table entries */
0087     dt_map = kzalloc(sizeof(*dt_map), GFP_KERNEL);
0088     if (!dt_map)
0089         goto err_free_map;
0090 
0091     dt_map->pctldev = pctldev;
0092     dt_map->map = map;
0093     dt_map->num_maps = num_maps;
0094     list_add_tail(&dt_map->node, &p->dt_maps);
0095 
0096     return pinctrl_register_mappings(map, num_maps);
0097 
0098 err_free_map:
0099     dt_free_map(pctldev, map, num_maps);
0100     return -ENOMEM;
0101 }
0102 
0103 struct pinctrl_dev *of_pinctrl_get(struct device_node *np)
0104 {
0105     return get_pinctrl_dev_from_of_node(np);
0106 }
0107 EXPORT_SYMBOL_GPL(of_pinctrl_get);
0108 
0109 static int dt_to_map_one_config(struct pinctrl *p,
0110                 struct pinctrl_dev *hog_pctldev,
0111                 const char *statename,
0112                 struct device_node *np_config)
0113 {
0114     struct pinctrl_dev *pctldev = NULL;
0115     struct device_node *np_pctldev;
0116     const struct pinctrl_ops *ops;
0117     int ret;
0118     struct pinctrl_map *map;
0119     unsigned num_maps;
0120     bool allow_default = false;
0121 
0122     /* Find the pin controller containing np_config */
0123     np_pctldev = of_node_get(np_config);
0124     for (;;) {
0125         if (!allow_default)
0126             allow_default = of_property_read_bool(np_pctldev,
0127                                   "pinctrl-use-default");
0128 
0129         np_pctldev = of_get_next_parent(np_pctldev);
0130         if (!np_pctldev || of_node_is_root(np_pctldev)) {
0131             of_node_put(np_pctldev);
0132             ret = -ENODEV;
0133             /* keep deferring if modules are enabled */
0134             if (IS_ENABLED(CONFIG_MODULES) && !allow_default && ret < 0)
0135                 ret = -EPROBE_DEFER;
0136             return ret;
0137         }
0138         /* If we're creating a hog we can use the passed pctldev */
0139         if (hog_pctldev && (np_pctldev == p->dev->of_node)) {
0140             pctldev = hog_pctldev;
0141             break;
0142         }
0143         pctldev = get_pinctrl_dev_from_of_node(np_pctldev);
0144         if (pctldev)
0145             break;
0146         /* Do not defer probing of hogs (circular loop) */
0147         if (np_pctldev == p->dev->of_node) {
0148             of_node_put(np_pctldev);
0149             return -ENODEV;
0150         }
0151     }
0152     of_node_put(np_pctldev);
0153 
0154     /*
0155      * Call pinctrl driver to parse device tree node, and
0156      * generate mapping table entries
0157      */
0158     ops = pctldev->desc->pctlops;
0159     if (!ops->dt_node_to_map) {
0160         dev_err(p->dev, "pctldev %s doesn't support DT\n",
0161             dev_name(pctldev->dev));
0162         return -ENODEV;
0163     }
0164     ret = ops->dt_node_to_map(pctldev, np_config, &map, &num_maps);
0165     if (ret < 0)
0166         return ret;
0167     else if (num_maps == 0) {
0168         /*
0169          * If we have no valid maps (maybe caused by empty pinctrl node
0170          * or typing error) ther is no need remember this, so just
0171          * return.
0172          */
0173         dev_info(p->dev,
0174              "there is not valid maps for state %s\n", statename);
0175         return 0;
0176     }
0177 
0178     /* Stash the mapping table chunk away for later use */
0179     return dt_remember_or_free_map(p, statename, pctldev, map, num_maps);
0180 }
0181 
0182 static int dt_remember_dummy_state(struct pinctrl *p, const char *statename)
0183 {
0184     struct pinctrl_map *map;
0185 
0186     map = kzalloc(sizeof(*map), GFP_KERNEL);
0187     if (!map)
0188         return -ENOMEM;
0189 
0190     /* There is no pctldev for PIN_MAP_TYPE_DUMMY_STATE */
0191     map->type = PIN_MAP_TYPE_DUMMY_STATE;
0192 
0193     return dt_remember_or_free_map(p, statename, NULL, map, 1);
0194 }
0195 
0196 int pinctrl_dt_to_map(struct pinctrl *p, struct pinctrl_dev *pctldev)
0197 {
0198     struct device_node *np = p->dev->of_node;
0199     int state, ret;
0200     char *propname;
0201     struct property *prop;
0202     const char *statename;
0203     const __be32 *list;
0204     int size, config;
0205     phandle phandle;
0206     struct device_node *np_config;
0207 
0208     /* CONFIG_OF enabled, p->dev not instantiated from DT */
0209     if (!np) {
0210         if (of_have_populated_dt())
0211             dev_dbg(p->dev,
0212                 "no of_node; not parsing pinctrl DT\n");
0213         return 0;
0214     }
0215 
0216     /* We may store pointers to property names within the node */
0217     of_node_get(np);
0218 
0219     /* For each defined state ID */
0220     for (state = 0; ; state++) {
0221         /* Retrieve the pinctrl-* property */
0222         propname = kasprintf(GFP_KERNEL, "pinctrl-%d", state);
0223         prop = of_find_property(np, propname, &size);
0224         kfree(propname);
0225         if (!prop) {
0226             if (state == 0) {
0227                 of_node_put(np);
0228                 return -ENODEV;
0229             }
0230             break;
0231         }
0232         list = prop->value;
0233         size /= sizeof(*list);
0234 
0235         /* Determine whether pinctrl-names property names the state */
0236         ret = of_property_read_string_index(np, "pinctrl-names",
0237                             state, &statename);
0238         /*
0239          * If not, statename is just the integer state ID. But rather
0240          * than dynamically allocate it and have to free it later,
0241          * just point part way into the property name for the string.
0242          */
0243         if (ret < 0)
0244             statename = prop->name + strlen("pinctrl-");
0245 
0246         /* For every referenced pin configuration node in it */
0247         for (config = 0; config < size; config++) {
0248             phandle = be32_to_cpup(list++);
0249 
0250             /* Look up the pin configuration node */
0251             np_config = of_find_node_by_phandle(phandle);
0252             if (!np_config) {
0253                 dev_err(p->dev,
0254                     "prop %s index %i invalid phandle\n",
0255                     prop->name, config);
0256                 ret = -EINVAL;
0257                 goto err;
0258             }
0259 
0260             /* Parse the node */
0261             ret = dt_to_map_one_config(p, pctldev, statename,
0262                            np_config);
0263             of_node_put(np_config);
0264             if (ret < 0)
0265                 goto err;
0266         }
0267 
0268         /* No entries in DT? Generate a dummy state table entry */
0269         if (!size) {
0270             ret = dt_remember_dummy_state(p, statename);
0271             if (ret < 0)
0272                 goto err;
0273         }
0274     }
0275 
0276     return 0;
0277 
0278 err:
0279     pinctrl_dt_free_maps(p);
0280     return ret;
0281 }
0282 
0283 /*
0284  * For pinctrl binding, typically #pinctrl-cells is for the pin controller
0285  * device, so either parent or grandparent. See pinctrl-bindings.txt.
0286  */
0287 static int pinctrl_find_cells_size(const struct device_node *np)
0288 {
0289     const char *cells_name = "#pinctrl-cells";
0290     int cells_size, error;
0291 
0292     error = of_property_read_u32(np->parent, cells_name, &cells_size);
0293     if (error) {
0294         error = of_property_read_u32(np->parent->parent,
0295                          cells_name, &cells_size);
0296         if (error)
0297             return -ENOENT;
0298     }
0299 
0300     return cells_size;
0301 }
0302 
0303 /**
0304  * pinctrl_get_list_and_count - Gets the list and it's cell size and number
0305  * @np: pointer to device node with the property
0306  * @list_name: property that contains the list
0307  * @list: pointer for the list found
0308  * @cells_size: pointer for the cell size found
0309  * @nr_elements: pointer for the number of elements found
0310  *
0311  * Typically np is a single pinctrl entry containing the list.
0312  */
0313 static int pinctrl_get_list_and_count(const struct device_node *np,
0314                       const char *list_name,
0315                       const __be32 **list,
0316                       int *cells_size,
0317                       int *nr_elements)
0318 {
0319     int size;
0320 
0321     *cells_size = 0;
0322     *nr_elements = 0;
0323 
0324     *list = of_get_property(np, list_name, &size);
0325     if (!*list)
0326         return -ENOENT;
0327 
0328     *cells_size = pinctrl_find_cells_size(np);
0329     if (*cells_size < 0)
0330         return -ENOENT;
0331 
0332     /* First element is always the index within the pinctrl device */
0333     *nr_elements = (size / sizeof(**list)) / (*cells_size + 1);
0334 
0335     return 0;
0336 }
0337 
0338 /**
0339  * pinctrl_count_index_with_args - Count number of elements in a pinctrl entry
0340  * @np: pointer to device node with the property
0341  * @list_name: property that contains the list
0342  *
0343  * Counts the number of elements in a pinctrl array consisting of an index
0344  * within the controller and a number of u32 entries specified for each
0345  * entry. Note that device_node is always for the parent pin controller device.
0346  */
0347 int pinctrl_count_index_with_args(const struct device_node *np,
0348                   const char *list_name)
0349 {
0350     const __be32 *list;
0351     int size, nr_cells, error;
0352 
0353     error = pinctrl_get_list_and_count(np, list_name, &list,
0354                        &nr_cells, &size);
0355     if (error)
0356         return error;
0357 
0358     return size;
0359 }
0360 EXPORT_SYMBOL_GPL(pinctrl_count_index_with_args);
0361 
0362 /**
0363  * pinctrl_copy_args - Populates of_phandle_args based on index
0364  * @np: pointer to device node with the property
0365  * @list: pointer to a list with the elements
0366  * @index: entry within the list of elements
0367  * @nr_cells: number of cells in the list
0368  * @nr_elem: number of elements for each entry in the list
0369  * @out_args: returned values
0370  *
0371  * Populates the of_phandle_args based on the index in the list.
0372  */
0373 static int pinctrl_copy_args(const struct device_node *np,
0374                  const __be32 *list,
0375                  int index, int nr_cells, int nr_elem,
0376                  struct of_phandle_args *out_args)
0377 {
0378     int i;
0379 
0380     memset(out_args, 0, sizeof(*out_args));
0381     out_args->np = (struct device_node *)np;
0382     out_args->args_count = nr_cells + 1;
0383 
0384     if (index >= nr_elem)
0385         return -EINVAL;
0386 
0387     list += index * (nr_cells + 1);
0388 
0389     for (i = 0; i < nr_cells + 1; i++)
0390         out_args->args[i] = be32_to_cpup(list++);
0391 
0392     return 0;
0393 }
0394 
0395 /**
0396  * pinctrl_parse_index_with_args - Find a node pointed by index in a list
0397  * @np: pointer to device node with the property
0398  * @list_name: property that contains the list
0399  * @index: index within the list
0400  * @out_args: entries in the list pointed by index
0401  *
0402  * Finds the selected element in a pinctrl array consisting of an index
0403  * within the controller and a number of u32 entries specified for each
0404  * entry. Note that device_node is always for the parent pin controller device.
0405  */
0406 int pinctrl_parse_index_with_args(const struct device_node *np,
0407                   const char *list_name, int index,
0408                   struct of_phandle_args *out_args)
0409 {
0410     const __be32 *list;
0411     int nr_elem, nr_cells, error;
0412 
0413     error = pinctrl_get_list_and_count(np, list_name, &list,
0414                        &nr_cells, &nr_elem);
0415     if (error || !nr_cells)
0416         return error;
0417 
0418     error = pinctrl_copy_args(np, list, index, nr_cells, nr_elem,
0419                   out_args);
0420     if (error)
0421         return error;
0422 
0423     return 0;
0424 }
0425 EXPORT_SYMBOL_GPL(pinctrl_parse_index_with_args);