aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorLaxman Dewangan <ldewangan@nvidia.com>2013-08-06 09:12:33 -0400
committerLinus Walleij <linus.walleij@linaro.org>2013-08-14 15:00:41 -0400
commit1eb207a9ecaafb980704d8bc055a9a0269f62f8e (patch)
treebd4264b775dc59bd08279ee265eed0fd0d1e7732 /drivers
parent744f0a9adb6a3478c9753eef56b0614ee69f969e (diff)
pinctrl: add utility functions for add map/configs
Some of pincontrol driver needs the utility function to create map list. The utility function needed for adding mux, configs etc. In place of duplicating this in each driver, add the common utility function in common file and use from device specific driver. This will reduce the duplicating of code across drivers. Changes from V1: - Add this files in this patch and add common utility APIs to here. Changes from V2: - Nothing in code. - Added Reviewed by Stephen. Signed-off-by: Laxman Dewangan <ldewangan@nvidia.com> Reviewed-by: Stephen Warren <swarren@nvidia.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/pinctrl/Makefile2
-rw-r--r--drivers/pinctrl/pinctrl-utils.c135
-rw-r--r--drivers/pinctrl/pinctrl-utils.h43
3 files changed, 179 insertions, 1 deletions
diff --git a/drivers/pinctrl/Makefile b/drivers/pinctrl/Makefile
index d64563bf6fb4..bc03a6eb8255 100644
--- a/drivers/pinctrl/Makefile
+++ b/drivers/pinctrl/Makefile
@@ -2,7 +2,7 @@
2 2
3ccflags-$(CONFIG_DEBUG_PINCTRL) += -DDEBUG 3ccflags-$(CONFIG_DEBUG_PINCTRL) += -DDEBUG
4 4
5obj-$(CONFIG_PINCTRL) += core.o 5obj-$(CONFIG_PINCTRL) += core.o pinctrl-utils.o
6obj-$(CONFIG_PINMUX) += pinmux.o 6obj-$(CONFIG_PINMUX) += pinmux.o
7obj-$(CONFIG_PINCONF) += pinconf.o 7obj-$(CONFIG_PINCONF) += pinconf.o
8ifeq ($(CONFIG_OF),y) 8ifeq ($(CONFIG_OF),y)
diff --git a/drivers/pinctrl/pinctrl-utils.c b/drivers/pinctrl/pinctrl-utils.c
new file mode 100644
index 000000000000..b7ac646c43ba
--- /dev/null
+++ b/drivers/pinctrl/pinctrl-utils.c
@@ -0,0 +1,135 @@
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
30int 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}
53EXPORT_SYMBOL_GPL(pinctrl_utils_reserve_map);
54
55int 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}
70EXPORT_SYMBOL_GPL(pinctrl_utils_add_map_mux);
71
72int 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}
98EXPORT_SYMBOL_GPL(pinctrl_utils_add_map_configs);
99
100int 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}
122EXPORT_SYMBOL_GPL(pinctrl_utils_add_config);
123
124void 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}
135EXPORT_SYMBOL_GPL(pinctrl_utils_dt_free_map);
diff --git a/drivers/pinctrl/pinctrl-utils.h b/drivers/pinctrl/pinctrl-utils.h
new file mode 100644
index 000000000000..d0ffe1ce200f
--- /dev/null
+++ b/drivers/pinctrl/pinctrl-utils.h
@@ -0,0 +1,43 @@
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#ifndef __PINCTRL_UTILS_H__
23#define __PINCTRL_UTILS_H__
24
25int pinctrl_utils_reserve_map(struct pinctrl_dev *pctldev,
26 struct pinctrl_map **map, unsigned *reserved_maps,
27 unsigned *num_maps, unsigned reserve);
28int pinctrl_utils_add_map_mux(struct pinctrl_dev *pctldev,
29 struct pinctrl_map **map, unsigned *reserved_maps,
30 unsigned *num_maps, const char *group,
31 const char *function);
32int pinctrl_utils_add_map_configs(struct pinctrl_dev *pctldev,
33 struct pinctrl_map **map, unsigned *reserved_maps,
34 unsigned *num_maps, const char *group,
35 unsigned long *configs, unsigned num_configs,
36 enum pinctrl_map_type type);
37int pinctrl_utils_add_config(struct pinctrl_dev *pctldev,
38 unsigned long **configs, unsigned *num_configs,
39 unsigned long config);
40void pinctrl_utils_dt_free_map(struct pinctrl_dev *pctldev,
41 struct pinctrl_map *map, unsigned num_maps);
42
43#endif /* __PINCTRL_UTILS_H__ */