Commit | Line | Data |
---|---|---|
1eb207a9 LD |
1 | /* |
2 | * Utils functions to implement the pincontrol driver. | |
3 | * | |
4 | * Copyright (c) 2013, NVIDIA Corporation. | |
5 | * | |
6 | * Author: Laxman Dewangan <ldewangan@nvidia.com> | |
7 | * | |
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. | |
11 | * | |
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. | |
16 | * | |
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 | |
20 | * 02111-1307, USA | |
21 | */ | |
22 | #include <linux/device.h> | |
23 | #include <linux/kernel.h> | |
24 | #include <linux/pinctrl/pinctrl.h> | |
25 | #include <linux/of.h> | |
26 | #include <linux/slab.h> | |
27 | #include "core.h" | |
28 | #include "pinctrl-utils.h" | |
29 | ||
30 | int pinctrl_utils_reserve_map(struct pinctrl_dev *pctldev, | |
31 | struct pinctrl_map **map, unsigned *reserved_maps, | |
32 | unsigned *num_maps, unsigned reserve) | |
33 | { | |
34 | unsigned old_num = *reserved_maps; | |
35 | unsigned new_num = *num_maps + reserve; | |
36 | struct pinctrl_map *new_map; | |
37 | ||
38 | if (old_num >= new_num) | |
39 | return 0; | |
40 | ||
41 | new_map = krealloc(*map, sizeof(*new_map) * new_num, GFP_KERNEL); | |
42 | if (!new_map) { | |
43 | dev_err(pctldev->dev, "krealloc(map) failed\n"); | |
44 | return -ENOMEM; | |
45 | } | |
46 | ||
47 | memset(new_map + old_num, 0, (new_num - old_num) * sizeof(*new_map)); | |
48 | ||
49 | *map = new_map; | |
50 | *reserved_maps = new_num; | |
51 | return 0; | |
52 | } | |
53 | EXPORT_SYMBOL_GPL(pinctrl_utils_reserve_map); | |
54 | ||
55 | int pinctrl_utils_add_map_mux(struct pinctrl_dev *pctldev, | |
56 | struct pinctrl_map **map, unsigned *reserved_maps, | |
57 | unsigned *num_maps, const char *group, | |
58 | const char *function) | |
59 | { | |
60 | if (WARN_ON(*num_maps == *reserved_maps)) | |
61 | return -ENOSPC; | |
62 | ||
63 | (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP; | |
64 | (*map)[*num_maps].data.mux.group = group; | |
65 | (*map)[*num_maps].data.mux.function = function; | |
66 | (*num_maps)++; | |
67 | ||
68 | return 0; | |
69 | } | |
70 | EXPORT_SYMBOL_GPL(pinctrl_utils_add_map_mux); | |
71 | ||
72 | int pinctrl_utils_add_map_configs(struct pinctrl_dev *pctldev, | |
73 | struct pinctrl_map **map, unsigned *reserved_maps, | |
74 | unsigned *num_maps, const char *group, | |
75 | unsigned long *configs, unsigned num_configs, | |
76 | enum pinctrl_map_type type) | |
77 | { | |
78 | unsigned long *dup_configs; | |
79 | ||
80 | if (WARN_ON(*num_maps == *reserved_maps)) | |
81 | return -ENOSPC; | |
82 | ||
83 | dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs), | |
84 | GFP_KERNEL); | |
85 | if (!dup_configs) { | |
86 | dev_err(pctldev->dev, "kmemdup(configs) failed\n"); | |
87 | return -ENOMEM; | |
88 | } | |
89 | ||
90 | (*map)[*num_maps].type = type; | |
91 | (*map)[*num_maps].data.configs.group_or_pin = group; | |
92 | (*map)[*num_maps].data.configs.configs = dup_configs; | |
93 | (*map)[*num_maps].data.configs.num_configs = num_configs; | |
94 | (*num_maps)++; | |
95 | ||
96 | return 0; | |
97 | } | |
98 | EXPORT_SYMBOL_GPL(pinctrl_utils_add_map_configs); | |
99 | ||
100 | int pinctrl_utils_add_config(struct pinctrl_dev *pctldev, | |
101 | unsigned long **configs, unsigned *num_configs, | |
102 | unsigned long config) | |
103 | { | |
104 | unsigned old_num = *num_configs; | |
105 | unsigned new_num = old_num + 1; | |
106 | unsigned long *new_configs; | |
107 | ||
108 | new_configs = krealloc(*configs, sizeof(*new_configs) * new_num, | |
109 | GFP_KERNEL); | |
110 | if (!new_configs) { | |
111 | dev_err(pctldev->dev, "krealloc(configs) failed\n"); | |
112 | return -ENOMEM; | |
113 | } | |
114 | ||
115 | new_configs[old_num] = config; | |
116 | ||
117 | *configs = new_configs; | |
118 | *num_configs = new_num; | |
119 | ||
120 | return 0; | |
121 | } | |
122 | EXPORT_SYMBOL_GPL(pinctrl_utils_add_config); | |
123 | ||
124 | void pinctrl_utils_dt_free_map(struct pinctrl_dev *pctldev, | |
125 | struct pinctrl_map *map, unsigned num_maps) | |
126 | { | |
127 | int i; | |
128 | ||
129 | for (i = 0; i < num_maps; i++) | |
130 | if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP) | |
131 | kfree(map[i].data.configs.configs); | |
132 | ||
133 | kfree(map); | |
134 | } | |
135 | EXPORT_SYMBOL_GPL(pinctrl_utils_dt_free_map); |