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