powerpc/mm: Drop the unnecessary region check
[linux-2.6-block.git] / drivers / pinctrl / pinconf.c
CommitLineData
ae6b4d85
LW
1/*
2 * Core driver for the pin config portions of the pin control subsystem
3 *
4 * Copyright (C) 2011 ST-Ericsson SA
5 * Written on behalf of Linaro for ST-Ericsson
6 *
7 * Author: Linus Walleij <linus.walleij@linaro.org>
8 *
9 * License terms: GNU General Public License (GPL) version 2
10 */
11#define pr_fmt(fmt) "pinconfig core: " fmt
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/device.h>
17#include <linux/slab.h>
18#include <linux/debugfs.h>
19#include <linux/seq_file.h>
20#include <linux/pinctrl/machine.h>
21#include <linux/pinctrl/pinctrl.h>
22#include <linux/pinctrl/pinconf.h>
23#include "core.h"
24#include "pinconf.h"
25
2b694250
SW
26int pinconf_check_ops(struct pinctrl_dev *pctldev)
27{
28 const struct pinconf_ops *ops = pctldev->desc->confops;
29
2b694250 30 /* We have to be able to config the pins in SOME way */
ad6e1107
JC
31 if (!ops->pin_config_set && !ops->pin_config_group_set) {
32 dev_err(pctldev->dev,
33 "pinconf has to be able to set a pins config\n");
2b694250 34 return -EINVAL;
ad6e1107 35 }
2b694250
SW
36 return 0;
37}
38
3f713b7c 39int pinconf_validate_map(const struct pinctrl_map *map, int i)
1e2082b5
SW
40{
41 if (!map->data.configs.group_or_pin) {
42 pr_err("failed to register map %s (%d): no group/pin given\n",
43 map->name, i);
44 return -EINVAL;
45 }
46
c95df2db 47 if (!map->data.configs.num_configs ||
1e2082b5 48 !map->data.configs.configs) {
c95df2db 49 pr_err("failed to register map %s (%d): no configs given\n",
1e2082b5
SW
50 map->name, i);
51 return -EINVAL;
52 }
53
54 return 0;
55}
56
394349f7 57int pin_config_get_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
ae6b4d85
LW
58 unsigned long *config)
59{
60 const struct pinconf_ops *ops = pctldev->desc->confops;
61
62 if (!ops || !ops->pin_config_get) {
ca67f10f
MY
63 dev_dbg(pctldev->dev,
64 "cannot get pin configuration, .pin_config_get missing in driver\n");
c420619d 65 return -ENOTSUPP;
ae6b4d85
LW
66 }
67
68 return ops->pin_config_get(pctldev, pin, config);
69}
70
43699dea 71int pin_config_group_get(const char *dev_name, const char *pin_group,
ae6b4d85
LW
72 unsigned long *config)
73{
43699dea
SW
74 struct pinctrl_dev *pctldev;
75 const struct pinconf_ops *ops;
57b676f9
SW
76 int selector, ret;
77
9dfac4fd 78 pctldev = get_pinctrl_dev_from_devname(dev_name);
57b676f9
SW
79 if (!pctldev) {
80 ret = -EINVAL;
42fed7ba 81 return ret;
57b676f9 82 }
42fed7ba
PC
83
84 mutex_lock(&pctldev->mutex);
85
43699dea
SW
86 ops = pctldev->desc->confops;
87
ae6b4d85 88 if (!ops || !ops->pin_config_group_get) {
e4d03050
ME
89 dev_dbg(pctldev->dev,
90 "cannot get configuration for pin group, missing group config get function in driver\n");
c420619d 91 ret = -ENOTSUPP;
57b676f9 92 goto unlock;
ae6b4d85
LW
93 }
94
95 selector = pinctrl_get_group_selector(pctldev, pin_group);
57b676f9
SW
96 if (selector < 0) {
97 ret = selector;
98 goto unlock;
99 }
ae6b4d85 100
57b676f9
SW
101 ret = ops->pin_config_group_get(pctldev, selector, config);
102
103unlock:
42fed7ba 104 mutex_unlock(&pctldev->mutex);
57b676f9 105 return ret;
ae6b4d85 106}
ae6b4d85 107
3f713b7c 108int pinconf_map_to_setting(const struct pinctrl_map *map,
1e2082b5
SW
109 struct pinctrl_setting *setting)
110{
111 struct pinctrl_dev *pctldev = setting->pctldev;
70b36378 112 int pin;
1e2082b5
SW
113
114 switch (setting->type) {
115 case PIN_MAP_TYPE_CONFIGS_PIN:
70b36378
LW
116 pin = pin_get_from_name(pctldev,
117 map->data.configs.group_or_pin);
118 if (pin < 0) {
119 dev_err(pctldev->dev, "could not map pin config for \"%s\"",
120 map->data.configs.group_or_pin);
121 return pin;
122 }
123 setting->data.configs.group_or_pin = pin;
1e2082b5
SW
124 break;
125 case PIN_MAP_TYPE_CONFIGS_GROUP:
70b36378
LW
126 pin = pinctrl_get_group_selector(pctldev,
127 map->data.configs.group_or_pin);
128 if (pin < 0) {
129 dev_err(pctldev->dev, "could not map group config for \"%s\"",
130 map->data.configs.group_or_pin);
131 return pin;
132 }
133 setting->data.configs.group_or_pin = pin;
1e2082b5
SW
134 break;
135 default:
136 return -EINVAL;
137 }
138
139 setting->data.configs.num_configs = map->data.configs.num_configs;
140 setting->data.configs.configs = map->data.configs.configs;
141
142 return 0;
143}
144
3f713b7c 145void pinconf_free_setting(const struct pinctrl_setting *setting)
1e2082b5
SW
146{
147}
148
3f713b7c 149int pinconf_apply_setting(const struct pinctrl_setting *setting)
1e2082b5
SW
150{
151 struct pinctrl_dev *pctldev = setting->pctldev;
152 const struct pinconf_ops *ops = pctldev->desc->confops;
03b054e9 153 int ret;
1e2082b5
SW
154
155 if (!ops) {
156 dev_err(pctldev->dev, "missing confops\n");
157 return -EINVAL;
158 }
159
160 switch (setting->type) {
161 case PIN_MAP_TYPE_CONFIGS_PIN:
162 if (!ops->pin_config_set) {
163 dev_err(pctldev->dev, "missing pin_config_set op\n");
164 return -EINVAL;
165 }
03b054e9
SY
166 ret = ops->pin_config_set(pctldev,
167 setting->data.configs.group_or_pin,
168 setting->data.configs.configs,
169 setting->data.configs.num_configs);
170 if (ret < 0) {
171 dev_err(pctldev->dev,
172 "pin_config_set op failed for pin %d\n",
173 setting->data.configs.group_or_pin);
174 return ret;
1e2082b5
SW
175 }
176 break;
177 case PIN_MAP_TYPE_CONFIGS_GROUP:
178 if (!ops->pin_config_group_set) {
179 dev_err(pctldev->dev,
180 "missing pin_config_group_set op\n");
181 return -EINVAL;
182 }
03b054e9
SY
183 ret = ops->pin_config_group_set(pctldev,
184 setting->data.configs.group_or_pin,
185 setting->data.configs.configs,
186 setting->data.configs.num_configs);
187 if (ret < 0) {
188 dev_err(pctldev->dev,
189 "pin_config_group_set op failed for group %d\n",
190 setting->data.configs.group_or_pin);
191 return ret;
1e2082b5
SW
192 }
193 break;
194 default:
195 return -EINVAL;
196 }
197
198 return 0;
199}
200
15381bc7
MW
201int pinconf_set_config(struct pinctrl_dev *pctldev, unsigned pin,
202 unsigned long *configs, size_t nconfigs)
203{
204 const struct pinconf_ops *ops;
205
206 ops = pctldev->desc->confops;
17a51248 207 if (!ops || !ops->pin_config_set)
15381bc7
MW
208 return -ENOTSUPP;
209
210 return ops->pin_config_set(pctldev, pin, configs, nconfigs);
211}
212
ae6b4d85
LW
213#ifdef CONFIG_DEBUG_FS
214
6de52c15 215static void pinconf_show_config(struct seq_file *s, struct pinctrl_dev *pctldev,
d96310ae 216 unsigned long *configs, unsigned num_configs)
1e2082b5 217{
6cb41587 218 const struct pinconf_ops *confops;
1e2082b5
SW
219 int i;
220
6cb41587
SW
221 if (pctldev)
222 confops = pctldev->desc->confops;
223 else
224 confops = NULL;
225
d96310ae
JH
226 for (i = 0; i < num_configs; i++) {
227 seq_puts(s, "config ");
228 if (confops && confops->pin_config_config_dbg_show)
229 confops->pin_config_config_dbg_show(pctldev, s,
230 configs[i]);
231 else
232 seq_printf(s, "%08lx", configs[i]);
47352a63 233 seq_putc(s, '\n');
d96310ae
JH
234 }
235}
236
3f713b7c 237void pinconf_show_map(struct seq_file *s, const struct pinctrl_map *map)
d96310ae
JH
238{
239 struct pinctrl_dev *pctldev;
240
241 pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
242
1e2082b5
SW
243 switch (map->type) {
244 case PIN_MAP_TYPE_CONFIGS_PIN:
de2eae26 245 seq_puts(s, "pin ");
1e2082b5
SW
246 break;
247 case PIN_MAP_TYPE_CONFIGS_GROUP:
de2eae26 248 seq_puts(s, "group ");
1e2082b5
SW
249 break;
250 default:
251 break;
252 }
253
254 seq_printf(s, "%s\n", map->data.configs.group_or_pin);
255
d96310ae
JH
256 pinconf_show_config(s, pctldev, map->data.configs.configs,
257 map->data.configs.num_configs);
1e2082b5
SW
258}
259
260void pinconf_show_setting(struct seq_file *s,
3f713b7c 261 const struct pinctrl_setting *setting)
1e2082b5
SW
262{
263 struct pinctrl_dev *pctldev = setting->pctldev;
264 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
265 struct pin_desc *desc;
1e2082b5
SW
266
267 switch (setting->type) {
268 case PIN_MAP_TYPE_CONFIGS_PIN:
269 desc = pin_desc_get(setting->pctldev,
270 setting->data.configs.group_or_pin);
cf9d994d 271 seq_printf(s, "pin %s (%d)", desc->name,
1e2082b5
SW
272 setting->data.configs.group_or_pin);
273 break;
274 case PIN_MAP_TYPE_CONFIGS_GROUP:
275 seq_printf(s, "group %s (%d)",
276 pctlops->get_group_name(pctldev,
277 setting->data.configs.group_or_pin),
278 setting->data.configs.group_or_pin);
279 break;
280 default:
281 break;
282 }
283
284 /*
3ec440e3 285 * FIXME: We should really get the pin controller to dump the config
1e2082b5
SW
286 * values, so they can be decoded to something meaningful.
287 */
d96310ae
JH
288 pinconf_show_config(s, pctldev, setting->data.configs.configs,
289 setting->data.configs.num_configs);
1e2082b5
SW
290}
291
ae6b4d85
LW
292static void pinconf_dump_pin(struct pinctrl_dev *pctldev,
293 struct seq_file *s, int pin)
294{
295 const struct pinconf_ops *ops = pctldev->desc->confops;
296
394349f7 297 /* no-op when not using generic pin config */
dd4d01f7 298 pinconf_generic_dump_pins(pctldev, s, NULL, pin);
ae6b4d85
LW
299 if (ops && ops->pin_config_dbg_show)
300 ops->pin_config_dbg_show(pctldev, s, pin);
301}
302
303static int pinconf_pins_show(struct seq_file *s, void *what)
304{
305 struct pinctrl_dev *pctldev = s->private;
706e8520 306 unsigned i, pin;
ae6b4d85
LW
307
308 seq_puts(s, "Pin config settings per pin\n");
2aeefe02 309 seq_puts(s, "Format: pin (name): configs\n");
ae6b4d85 310
42fed7ba 311 mutex_lock(&pctldev->mutex);
57b676f9 312
706e8520 313 /* The pin number can be retrived from the pin controller descriptor */
546edd83 314 for (i = 0; i < pctldev->desc->npins; i++) {
ae6b4d85
LW
315 struct pin_desc *desc;
316
706e8520 317 pin = pctldev->desc->pins[i].number;
ae6b4d85 318 desc = pin_desc_get(pctldev, pin);
706e8520 319 /* Skip if we cannot search the pin */
76ce37f0 320 if (!desc)
ae6b4d85
LW
321 continue;
322
a672eb5e 323 seq_printf(s, "pin %d (%s): ", pin, desc->name);
ae6b4d85
LW
324
325 pinconf_dump_pin(pctldev, s, pin);
47352a63 326 seq_putc(s, '\n');
ae6b4d85
LW
327 }
328
42fed7ba 329 mutex_unlock(&pctldev->mutex);
57b676f9 330
ae6b4d85
LW
331 return 0;
332}
333
334static void pinconf_dump_group(struct pinctrl_dev *pctldev,
335 struct seq_file *s, unsigned selector,
336 const char *gname)
337{
338 const struct pinconf_ops *ops = pctldev->desc->confops;
339
394349f7 340 /* no-op when not using generic pin config */
dd4d01f7 341 pinconf_generic_dump_pins(pctldev, s, gname, 0);
ae6b4d85
LW
342 if (ops && ops->pin_config_group_dbg_show)
343 ops->pin_config_group_dbg_show(pctldev, s, selector);
344}
345
346static int pinconf_groups_show(struct seq_file *s, void *what)
347{
348 struct pinctrl_dev *pctldev = s->private;
349 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
d1e90e9e 350 unsigned ngroups = pctlops->get_groups_count(pctldev);
ae6b4d85
LW
351 unsigned selector = 0;
352
ae6b4d85 353 seq_puts(s, "Pin config settings per pin group\n");
2aeefe02 354 seq_puts(s, "Format: group (name): configs\n");
ae6b4d85 355
d1e90e9e 356 while (selector < ngroups) {
ae6b4d85
LW
357 const char *gname = pctlops->get_group_name(pctldev, selector);
358
a672eb5e 359 seq_printf(s, "%u (%s): ", selector, gname);
ae6b4d85 360 pinconf_dump_group(pctldev, s, selector, gname);
47352a63 361 seq_putc(s, '\n');
ae6b4d85
LW
362 selector++;
363 }
364
365 return 0;
366}
367
0819dc72
YL
368DEFINE_SHOW_ATTRIBUTE(pinconf_pins);
369DEFINE_SHOW_ATTRIBUTE(pinconf_groups);
ae6b4d85
LW
370
371void pinconf_init_device_debugfs(struct dentry *devroot,
372 struct pinctrl_dev *pctldev)
373{
374 debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO,
0819dc72 375 devroot, pctldev, &pinconf_pins_fops);
ae6b4d85 376 debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO,
0819dc72 377 devroot, pctldev, &pinconf_groups_fops);
ae6b4d85
LW
378}
379
380#endif