1 // SPDX-License-Identifier: GPL-2.0-only
3 * Utils functions to implement the pincontrol driver.
5 * Copyright (c) 2013, NVIDIA Corporation.
7 * Author: Laxman Dewangan <ldewangan@nvidia.com>
9 #include <linux/device.h>
10 #include <linux/export.h>
11 #include <linux/kernel.h>
12 #include <linux/pinctrl/pinctrl.h>
14 #include <linux/slab.h>
16 #include "pinctrl-utils.h"
18 int pinctrl_utils_reserve_map(struct pinctrl_dev *pctldev,
19 struct pinctrl_map **map, unsigned *reserved_maps,
20 unsigned *num_maps, unsigned reserve)
22 unsigned old_num = *reserved_maps;
23 unsigned new_num = *num_maps + reserve;
24 struct pinctrl_map *new_map;
26 if (old_num >= new_num)
29 new_map = krealloc_array(*map, new_num, sizeof(*new_map), GFP_KERNEL);
31 dev_err(pctldev->dev, "krealloc(map) failed\n");
35 memset(new_map + old_num, 0, (new_num - old_num) * sizeof(*new_map));
38 *reserved_maps = new_num;
41 EXPORT_SYMBOL_GPL(pinctrl_utils_reserve_map);
43 int pinctrl_utils_add_map_mux(struct pinctrl_dev *pctldev,
44 struct pinctrl_map **map, unsigned *reserved_maps,
45 unsigned *num_maps, const char *group,
48 if (WARN_ON(*num_maps == *reserved_maps))
51 (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
52 (*map)[*num_maps].data.mux.group = group;
53 (*map)[*num_maps].data.mux.function = function;
58 EXPORT_SYMBOL_GPL(pinctrl_utils_add_map_mux);
60 int pinctrl_utils_add_map_configs(struct pinctrl_dev *pctldev,
61 struct pinctrl_map **map, unsigned *reserved_maps,
62 unsigned *num_maps, const char *group,
63 unsigned long *configs, unsigned num_configs,
64 enum pinctrl_map_type type)
66 unsigned long *dup_configs;
68 if (WARN_ON(*num_maps == *reserved_maps))
71 dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs),
76 (*map)[*num_maps].type = type;
77 (*map)[*num_maps].data.configs.group_or_pin = group;
78 (*map)[*num_maps].data.configs.configs = dup_configs;
79 (*map)[*num_maps].data.configs.num_configs = num_configs;
84 EXPORT_SYMBOL_GPL(pinctrl_utils_add_map_configs);
86 int pinctrl_utils_add_config(struct pinctrl_dev *pctldev,
87 unsigned long **configs, unsigned *num_configs,
90 unsigned old_num = *num_configs;
91 unsigned new_num = old_num + 1;
92 unsigned long *new_configs;
94 new_configs = krealloc(*configs, sizeof(*new_configs) * new_num,
97 dev_err(pctldev->dev, "krealloc(configs) failed\n");
101 new_configs[old_num] = config;
103 *configs = new_configs;
104 *num_configs = new_num;
108 EXPORT_SYMBOL_GPL(pinctrl_utils_add_config);
110 void pinctrl_utils_free_map(struct pinctrl_dev *pctldev,
111 struct pinctrl_map *map, unsigned num_maps)
115 for (i = 0; i < num_maps; i++) {
116 switch (map[i].type) {
117 case PIN_MAP_TYPE_CONFIGS_GROUP:
118 case PIN_MAP_TYPE_CONFIGS_PIN:
119 kfree(map[i].data.configs.configs);
127 EXPORT_SYMBOL_GPL(pinctrl_utils_free_map);