2 * Utils functions to implement the pincontrol driver.
4 * Copyright (c) 2013, NVIDIA Corporation.
6 * Author: Laxman Dewangan <ldewangan@nvidia.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation version 2.
12 * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
13 * whether express or implied; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 #include <linux/device.h>
23 #include <linux/export.h>
24 #include <linux/kernel.h>
25 #include <linux/pinctrl/pinctrl.h>
27 #include <linux/slab.h>
29 #include "pinctrl-utils.h"
31 int pinctrl_utils_reserve_map(struct pinctrl_dev
*pctldev
,
32 struct pinctrl_map
**map
, unsigned *reserved_maps
,
33 unsigned *num_maps
, unsigned reserve
)
35 unsigned old_num
= *reserved_maps
;
36 unsigned new_num
= *num_maps
+ reserve
;
37 struct pinctrl_map
*new_map
;
39 if (old_num
>= new_num
)
42 new_map
= krealloc_array(*map
, new_num
, sizeof(*new_map
), GFP_KERNEL
);
44 dev_err(pctldev
->dev
, "krealloc(map) failed\n");
48 memset(new_map
+ old_num
, 0, (new_num
- old_num
) * sizeof(*new_map
));
51 *reserved_maps
= new_num
;
54 EXPORT_SYMBOL_GPL(pinctrl_utils_reserve_map
);
56 int pinctrl_utils_add_map_mux(struct pinctrl_dev
*pctldev
,
57 struct pinctrl_map
**map
, unsigned *reserved_maps
,
58 unsigned *num_maps
, const char *group
,
61 if (WARN_ON(*num_maps
== *reserved_maps
))
64 (*map
)[*num_maps
].type
= PIN_MAP_TYPE_MUX_GROUP
;
65 (*map
)[*num_maps
].data
.mux
.group
= group
;
66 (*map
)[*num_maps
].data
.mux
.function
= function
;
71 EXPORT_SYMBOL_GPL(pinctrl_utils_add_map_mux
);
73 int pinctrl_utils_add_map_configs(struct pinctrl_dev
*pctldev
,
74 struct pinctrl_map
**map
, unsigned *reserved_maps
,
75 unsigned *num_maps
, const char *group
,
76 unsigned long *configs
, unsigned num_configs
,
77 enum pinctrl_map_type type
)
79 unsigned long *dup_configs
;
81 if (WARN_ON(*num_maps
== *reserved_maps
))
84 dup_configs
= kmemdup(configs
, num_configs
* sizeof(*dup_configs
),
89 (*map
)[*num_maps
].type
= type
;
90 (*map
)[*num_maps
].data
.configs
.group_or_pin
= group
;
91 (*map
)[*num_maps
].data
.configs
.configs
= dup_configs
;
92 (*map
)[*num_maps
].data
.configs
.num_configs
= num_configs
;
97 EXPORT_SYMBOL_GPL(pinctrl_utils_add_map_configs
);
99 int pinctrl_utils_add_config(struct pinctrl_dev
*pctldev
,
100 unsigned long **configs
, unsigned *num_configs
,
101 unsigned long config
)
103 unsigned old_num
= *num_configs
;
104 unsigned new_num
= old_num
+ 1;
105 unsigned long *new_configs
;
107 new_configs
= krealloc(*configs
, sizeof(*new_configs
) * new_num
,
110 dev_err(pctldev
->dev
, "krealloc(configs) failed\n");
114 new_configs
[old_num
] = config
;
116 *configs
= new_configs
;
117 *num_configs
= new_num
;
121 EXPORT_SYMBOL_GPL(pinctrl_utils_add_config
);
123 void pinctrl_utils_free_map(struct pinctrl_dev
*pctldev
,
124 struct pinctrl_map
*map
, unsigned num_maps
)
128 for (i
= 0; i
< num_maps
; i
++) {
129 switch (map
[i
].type
) {
130 case PIN_MAP_TYPE_CONFIGS_GROUP
:
131 case PIN_MAP_TYPE_CONFIGS_PIN
:
132 kfree(map
[i
].data
.configs
.configs
);
140 EXPORT_SYMBOL_GPL(pinctrl_utils_free_map
);