blob: 32f96808202a60b6675166ed537912847a86c62b [file] [log] [blame]
Linus Walleijae6b4d82011-10-19 18:14:33 +02001/*
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>
Laurent Meunierf07512e2013-04-18 10:48:07 +020020#include <linux/uaccess.h>
Linus Walleijae6b4d82011-10-19 18:14:33 +020021#include <linux/pinctrl/machine.h>
22#include <linux/pinctrl/pinctrl.h>
23#include <linux/pinctrl/pinconf.h>
24#include "core.h"
25#include "pinconf.h"
26
Stephen Warren2b694252012-02-19 23:45:46 -070027int pinconf_check_ops(struct pinctrl_dev *pctldev)
28{
29 const struct pinconf_ops *ops = pctldev->desc->confops;
30
31 /* We must be able to read out pin status */
John Crispinad6e1102012-04-26 16:47:11 +020032 if (!ops->pin_config_get && !ops->pin_config_group_get) {
33 dev_err(pctldev->dev,
34 "pinconf must be able to read out pin status\n");
Stephen Warren2b694252012-02-19 23:45:46 -070035 return -EINVAL;
John Crispinad6e1102012-04-26 16:47:11 +020036 }
Stephen Warren2b694252012-02-19 23:45:46 -070037 /* We have to be able to config the pins in SOME way */
John Crispinad6e1102012-04-26 16:47:11 +020038 if (!ops->pin_config_set && !ops->pin_config_group_set) {
39 dev_err(pctldev->dev,
40 "pinconf has to be able to set a pins config\n");
Stephen Warren2b694252012-02-19 23:45:46 -070041 return -EINVAL;
John Crispinad6e1102012-04-26 16:47:11 +020042 }
Stephen Warren2b694252012-02-19 23:45:46 -070043 return 0;
44}
45
Stephen Warren1e2082b2012-03-02 13:05:48 -070046int pinconf_validate_map(struct pinctrl_map const *map, int i)
47{
48 if (!map->data.configs.group_or_pin) {
49 pr_err("failed to register map %s (%d): no group/pin given\n",
50 map->name, i);
51 return -EINVAL;
52 }
53
Dong Aishengc95df2d2012-05-14 19:06:36 +080054 if (!map->data.configs.num_configs ||
Stephen Warren1e2082b2012-03-02 13:05:48 -070055 !map->data.configs.configs) {
Dong Aishengc95df2d2012-05-14 19:06:36 +080056 pr_err("failed to register map %s (%d): no configs given\n",
Stephen Warren1e2082b2012-03-02 13:05:48 -070057 map->name, i);
58 return -EINVAL;
59 }
60
61 return 0;
62}
63
Linus Walleij394349f2011-11-24 18:27:15 +010064int pin_config_get_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
Linus Walleijae6b4d82011-10-19 18:14:33 +020065 unsigned long *config)
66{
67 const struct pinconf_ops *ops = pctldev->desc->confops;
68
69 if (!ops || !ops->pin_config_get) {
Stephen Warren51cd24e2011-12-09 16:59:05 -070070 dev_err(pctldev->dev, "cannot get pin configuration, missing "
Linus Walleijae6b4d82011-10-19 18:14:33 +020071 "pin_config_get() function in driver\n");
72 return -EINVAL;
73 }
74
75 return ops->pin_config_get(pctldev, pin, config);
76}
77
78/**
79 * pin_config_get() - get the configuration of a single pin parameter
Stephen Warren43699de2011-12-15 16:57:17 -070080 * @dev_name: name of the pin controller device for this pin
Linus Walleijae6b4d82011-10-19 18:14:33 +020081 * @name: name of the pin to get the config for
82 * @config: the config pointed to by this argument will be filled in with the
83 * current pin state, it can be used directly by drivers as a numeral, or
84 * it can be dereferenced to any struct.
85 */
Stephen Warren43699de2011-12-15 16:57:17 -070086int pin_config_get(const char *dev_name, const char *name,
Linus Walleijae6b4d82011-10-19 18:14:33 +020087 unsigned long *config)
88{
Stephen Warren43699de2011-12-15 16:57:17 -070089 struct pinctrl_dev *pctldev;
Linus Walleijae6b4d82011-10-19 18:14:33 +020090 int pin;
91
Stephen Warren57b676f2012-03-02 13:05:44 -070092 mutex_lock(&pinctrl_mutex);
93
Linus Walleij9dfac4f2012-02-01 18:02:47 +010094 pctldev = get_pinctrl_dev_from_devname(dev_name);
Stephen Warren57b676f2012-03-02 13:05:44 -070095 if (!pctldev) {
96 pin = -EINVAL;
97 goto unlock;
98 }
Stephen Warren43699de2011-12-15 16:57:17 -070099
Linus Walleijae6b4d82011-10-19 18:14:33 +0200100 pin = pin_get_from_name(pctldev, name);
101 if (pin < 0)
Stephen Warren57b676f2012-03-02 13:05:44 -0700102 goto unlock;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200103
Stephen Warren57b676f2012-03-02 13:05:44 -0700104 pin = pin_config_get_for_pin(pctldev, pin, config);
105
106unlock:
107 mutex_unlock(&pinctrl_mutex);
108 return pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200109}
110EXPORT_SYMBOL(pin_config_get);
111
Stephen Warren2b694252012-02-19 23:45:46 -0700112static int pin_config_set_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
Linus Walleijae6b4d82011-10-19 18:14:33 +0200113 unsigned long config)
114{
115 const struct pinconf_ops *ops = pctldev->desc->confops;
116 int ret;
117
118 if (!ops || !ops->pin_config_set) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700119 dev_err(pctldev->dev, "cannot configure pin, missing "
Linus Walleijae6b4d82011-10-19 18:14:33 +0200120 "config function in driver\n");
121 return -EINVAL;
122 }
123
124 ret = ops->pin_config_set(pctldev, pin, config);
125 if (ret) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700126 dev_err(pctldev->dev,
Linus Walleijae6b4d82011-10-19 18:14:33 +0200127 "unable to set pin configuration on pin %d\n", pin);
128 return ret;
129 }
130
131 return 0;
132}
133
134/**
135 * pin_config_set() - set the configuration of a single pin parameter
Stephen Warren43699de2011-12-15 16:57:17 -0700136 * @dev_name: name of pin controller device for this pin
Linus Walleijae6b4d82011-10-19 18:14:33 +0200137 * @name: name of the pin to set the config for
138 * @config: the config in this argument will contain the desired pin state, it
139 * can be used directly by drivers as a numeral, or it can be dereferenced
140 * to any struct.
141 */
Stephen Warren43699de2011-12-15 16:57:17 -0700142int pin_config_set(const char *dev_name, const char *name,
Linus Walleijae6b4d82011-10-19 18:14:33 +0200143 unsigned long config)
144{
Stephen Warren43699de2011-12-15 16:57:17 -0700145 struct pinctrl_dev *pctldev;
Stephen Warren57b676f2012-03-02 13:05:44 -0700146 int pin, ret;
147
148 mutex_lock(&pinctrl_mutex);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200149
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100150 pctldev = get_pinctrl_dev_from_devname(dev_name);
Stephen Warren57b676f2012-03-02 13:05:44 -0700151 if (!pctldev) {
152 ret = -EINVAL;
153 goto unlock;
154 }
Stephen Warren43699de2011-12-15 16:57:17 -0700155
Linus Walleijae6b4d82011-10-19 18:14:33 +0200156 pin = pin_get_from_name(pctldev, name);
Stephen Warren57b676f2012-03-02 13:05:44 -0700157 if (pin < 0) {
158 ret = pin;
159 goto unlock;
160 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200161
Stephen Warren57b676f2012-03-02 13:05:44 -0700162 ret = pin_config_set_for_pin(pctldev, pin, config);
163
164unlock:
165 mutex_unlock(&pinctrl_mutex);
166 return ret;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200167}
168EXPORT_SYMBOL(pin_config_set);
169
Stephen Warren43699de2011-12-15 16:57:17 -0700170int pin_config_group_get(const char *dev_name, const char *pin_group,
Linus Walleijae6b4d82011-10-19 18:14:33 +0200171 unsigned long *config)
172{
Stephen Warren43699de2011-12-15 16:57:17 -0700173 struct pinctrl_dev *pctldev;
174 const struct pinconf_ops *ops;
Stephen Warren57b676f2012-03-02 13:05:44 -0700175 int selector, ret;
176
177 mutex_lock(&pinctrl_mutex);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200178
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100179 pctldev = get_pinctrl_dev_from_devname(dev_name);
Stephen Warren57b676f2012-03-02 13:05:44 -0700180 if (!pctldev) {
181 ret = -EINVAL;
182 goto unlock;
183 }
Stephen Warren43699de2011-12-15 16:57:17 -0700184 ops = pctldev->desc->confops;
185
Linus Walleijae6b4d82011-10-19 18:14:33 +0200186 if (!ops || !ops->pin_config_group_get) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700187 dev_err(pctldev->dev, "cannot get configuration for pin "
Linus Walleijae6b4d82011-10-19 18:14:33 +0200188 "group, missing group config get function in "
189 "driver\n");
Stephen Warren57b676f2012-03-02 13:05:44 -0700190 ret = -EINVAL;
191 goto unlock;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200192 }
193
194 selector = pinctrl_get_group_selector(pctldev, pin_group);
Stephen Warren57b676f2012-03-02 13:05:44 -0700195 if (selector < 0) {
196 ret = selector;
197 goto unlock;
198 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200199
Stephen Warren57b676f2012-03-02 13:05:44 -0700200 ret = ops->pin_config_group_get(pctldev, selector, config);
201
202unlock:
203 mutex_unlock(&pinctrl_mutex);
204 return ret;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200205}
206EXPORT_SYMBOL(pin_config_group_get);
207
Stephen Warren43699de2011-12-15 16:57:17 -0700208int pin_config_group_set(const char *dev_name, const char *pin_group,
Linus Walleijae6b4d82011-10-19 18:14:33 +0200209 unsigned long config)
210{
Stephen Warren43699de2011-12-15 16:57:17 -0700211 struct pinctrl_dev *pctldev;
212 const struct pinconf_ops *ops;
213 const struct pinctrl_ops *pctlops;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200214 int selector;
215 const unsigned *pins;
216 unsigned num_pins;
217 int ret;
218 int i;
219
Stephen Warren57b676f2012-03-02 13:05:44 -0700220 mutex_lock(&pinctrl_mutex);
221
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100222 pctldev = get_pinctrl_dev_from_devname(dev_name);
Stephen Warren57b676f2012-03-02 13:05:44 -0700223 if (!pctldev) {
224 ret = -EINVAL;
225 goto unlock;
226 }
Stephen Warren43699de2011-12-15 16:57:17 -0700227 ops = pctldev->desc->confops;
228 pctlops = pctldev->desc->pctlops;
229
Linus Walleijae6b4d82011-10-19 18:14:33 +0200230 if (!ops || (!ops->pin_config_group_set && !ops->pin_config_set)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700231 dev_err(pctldev->dev, "cannot configure pin group, missing "
Linus Walleijae6b4d82011-10-19 18:14:33 +0200232 "config function in driver\n");
Stephen Warren57b676f2012-03-02 13:05:44 -0700233 ret = -EINVAL;
234 goto unlock;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200235 }
236
237 selector = pinctrl_get_group_selector(pctldev, pin_group);
Stephen Warren57b676f2012-03-02 13:05:44 -0700238 if (selector < 0) {
239 ret = selector;
240 goto unlock;
241 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200242
243 ret = pctlops->get_group_pins(pctldev, selector, &pins, &num_pins);
244 if (ret) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700245 dev_err(pctldev->dev, "cannot configure pin group, error "
Linus Walleijae6b4d82011-10-19 18:14:33 +0200246 "getting pins\n");
Stephen Warren57b676f2012-03-02 13:05:44 -0700247 goto unlock;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200248 }
249
250 /*
251 * If the pin controller supports handling entire groups we use that
252 * capability.
253 */
254 if (ops->pin_config_group_set) {
255 ret = ops->pin_config_group_set(pctldev, selector, config);
256 /*
257 * If the pin controller prefer that a certain group be handled
258 * pin-by-pin as well, it returns -EAGAIN.
259 */
260 if (ret != -EAGAIN)
Stephen Warren57b676f2012-03-02 13:05:44 -0700261 goto unlock;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200262 }
263
264 /*
265 * If the controller cannot handle entire groups, we configure each pin
266 * individually.
267 */
Stephen Warren57b676f2012-03-02 13:05:44 -0700268 if (!ops->pin_config_set) {
269 ret = 0;
270 goto unlock;
271 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200272
273 for (i = 0; i < num_pins; i++) {
274 ret = ops->pin_config_set(pctldev, pins[i], config);
275 if (ret < 0)
Stephen Warren57b676f2012-03-02 13:05:44 -0700276 goto unlock;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200277 }
278
Stephen Warren57b676f2012-03-02 13:05:44 -0700279 ret = 0;
280
281unlock:
282 mutex_unlock(&pinctrl_mutex);
283
284 return ret;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200285}
286EXPORT_SYMBOL(pin_config_group_set);
287
Stephen Warren1e2082b2012-03-02 13:05:48 -0700288int pinconf_map_to_setting(struct pinctrl_map const *map,
289 struct pinctrl_setting *setting)
290{
291 struct pinctrl_dev *pctldev = setting->pctldev;
Linus Walleij70b36372012-03-12 21:38:29 +0100292 int pin;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700293
294 switch (setting->type) {
295 case PIN_MAP_TYPE_CONFIGS_PIN:
Linus Walleij70b36372012-03-12 21:38:29 +0100296 pin = pin_get_from_name(pctldev,
297 map->data.configs.group_or_pin);
298 if (pin < 0) {
299 dev_err(pctldev->dev, "could not map pin config for \"%s\"",
300 map->data.configs.group_or_pin);
301 return pin;
302 }
303 setting->data.configs.group_or_pin = pin;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700304 break;
305 case PIN_MAP_TYPE_CONFIGS_GROUP:
Linus Walleij70b36372012-03-12 21:38:29 +0100306 pin = pinctrl_get_group_selector(pctldev,
307 map->data.configs.group_or_pin);
308 if (pin < 0) {
309 dev_err(pctldev->dev, "could not map group config for \"%s\"",
310 map->data.configs.group_or_pin);
311 return pin;
312 }
313 setting->data.configs.group_or_pin = pin;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700314 break;
315 default:
316 return -EINVAL;
317 }
318
319 setting->data.configs.num_configs = map->data.configs.num_configs;
320 setting->data.configs.configs = map->data.configs.configs;
321
322 return 0;
323}
324
325void pinconf_free_setting(struct pinctrl_setting const *setting)
326{
327}
328
329int pinconf_apply_setting(struct pinctrl_setting const *setting)
330{
331 struct pinctrl_dev *pctldev = setting->pctldev;
332 const struct pinconf_ops *ops = pctldev->desc->confops;
333 int i, ret;
334
335 if (!ops) {
336 dev_err(pctldev->dev, "missing confops\n");
337 return -EINVAL;
338 }
339
340 switch (setting->type) {
341 case PIN_MAP_TYPE_CONFIGS_PIN:
342 if (!ops->pin_config_set) {
343 dev_err(pctldev->dev, "missing pin_config_set op\n");
344 return -EINVAL;
345 }
346 for (i = 0; i < setting->data.configs.num_configs; i++) {
347 ret = ops->pin_config_set(pctldev,
348 setting->data.configs.group_or_pin,
349 setting->data.configs.configs[i]);
350 if (ret < 0) {
351 dev_err(pctldev->dev,
352 "pin_config_set op failed for pin %d config %08lx\n",
353 setting->data.configs.group_or_pin,
354 setting->data.configs.configs[i]);
355 return ret;
356 }
357 }
358 break;
359 case PIN_MAP_TYPE_CONFIGS_GROUP:
360 if (!ops->pin_config_group_set) {
361 dev_err(pctldev->dev,
362 "missing pin_config_group_set op\n");
363 return -EINVAL;
364 }
365 for (i = 0; i < setting->data.configs.num_configs; i++) {
366 ret = ops->pin_config_group_set(pctldev,
367 setting->data.configs.group_or_pin,
368 setting->data.configs.configs[i]);
369 if (ret < 0) {
370 dev_err(pctldev->dev,
371 "pin_config_group_set op failed for group %d config %08lx\n",
372 setting->data.configs.group_or_pin,
373 setting->data.configs.configs[i]);
374 return ret;
375 }
376 }
377 break;
378 default:
379 return -EINVAL;
380 }
381
382 return 0;
383}
384
Linus Walleijae6b4d82011-10-19 18:14:33 +0200385#ifdef CONFIG_DEBUG_FS
386
Stephen Warren1e2082b2012-03-02 13:05:48 -0700387void pinconf_show_map(struct seq_file *s, struct pinctrl_map const *map)
388{
Stephen Warren6cb41582012-04-13 10:49:06 -0600389 struct pinctrl_dev *pctldev;
390 const struct pinconf_ops *confops;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700391 int i;
392
Stephen Warren6cb41582012-04-13 10:49:06 -0600393 pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
394 if (pctldev)
395 confops = pctldev->desc->confops;
396 else
397 confops = NULL;
398
Stephen Warren1e2082b2012-03-02 13:05:48 -0700399 switch (map->type) {
400 case PIN_MAP_TYPE_CONFIGS_PIN:
401 seq_printf(s, "pin ");
402 break;
403 case PIN_MAP_TYPE_CONFIGS_GROUP:
404 seq_printf(s, "group ");
405 break;
406 default:
407 break;
408 }
409
410 seq_printf(s, "%s\n", map->data.configs.group_or_pin);
411
Stephen Warren6cb41582012-04-13 10:49:06 -0600412 for (i = 0; i < map->data.configs.num_configs; i++) {
413 seq_printf(s, "config ");
414 if (confops && confops->pin_config_config_dbg_show)
415 confops->pin_config_config_dbg_show(pctldev, s,
416 map->data.configs.configs[i]);
417 else
418 seq_printf(s, "%08lx", map->data.configs.configs[i]);
419 seq_printf(s, "\n");
420 }
Stephen Warren1e2082b2012-03-02 13:05:48 -0700421}
422
423void pinconf_show_setting(struct seq_file *s,
424 struct pinctrl_setting const *setting)
425{
426 struct pinctrl_dev *pctldev = setting->pctldev;
427 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Stephen Warren6cb41582012-04-13 10:49:06 -0600428 const struct pinconf_ops *confops = pctldev->desc->confops;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700429 struct pin_desc *desc;
430 int i;
431
432 switch (setting->type) {
433 case PIN_MAP_TYPE_CONFIGS_PIN:
434 desc = pin_desc_get(setting->pctldev,
435 setting->data.configs.group_or_pin);
436 seq_printf(s, "pin %s (%d)",
437 desc->name ? desc->name : "unnamed",
438 setting->data.configs.group_or_pin);
439 break;
440 case PIN_MAP_TYPE_CONFIGS_GROUP:
441 seq_printf(s, "group %s (%d)",
442 pctlops->get_group_name(pctldev,
443 setting->data.configs.group_or_pin),
444 setting->data.configs.group_or_pin);
445 break;
446 default:
447 break;
448 }
449
450 /*
451 * FIXME: We should really get the pin controler to dump the config
452 * values, so they can be decoded to something meaningful.
453 */
Stephen Warren6cb41582012-04-13 10:49:06 -0600454 for (i = 0; i < setting->data.configs.num_configs; i++) {
455 seq_printf(s, " ");
456 if (confops && confops->pin_config_config_dbg_show)
457 confops->pin_config_config_dbg_show(pctldev, s,
458 setting->data.configs.configs[i]);
459 else
460 seq_printf(s, "%08lx",
461 setting->data.configs.configs[i]);
462 }
Stephen Warren1e2082b2012-03-02 13:05:48 -0700463
464 seq_printf(s, "\n");
465}
466
Linus Walleijae6b4d82011-10-19 18:14:33 +0200467static void pinconf_dump_pin(struct pinctrl_dev *pctldev,
468 struct seq_file *s, int pin)
469{
470 const struct pinconf_ops *ops = pctldev->desc->confops;
471
Linus Walleij394349f2011-11-24 18:27:15 +0100472 /* no-op when not using generic pin config */
473 pinconf_generic_dump_pin(pctldev, s, pin);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200474 if (ops && ops->pin_config_dbg_show)
475 ops->pin_config_dbg_show(pctldev, s, pin);
476}
477
478static int pinconf_pins_show(struct seq_file *s, void *what)
479{
480 struct pinctrl_dev *pctldev = s->private;
Dong Aishengad8bb722012-04-10 12:41:34 +0800481 const struct pinconf_ops *ops = pctldev->desc->confops;
Chanho Park706e8522012-01-03 16:47:50 +0900482 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200483
Dong Aishengad8bb722012-04-10 12:41:34 +0800484 if (!ops || !ops->pin_config_get)
485 return 0;
486
Linus Walleijae6b4d82011-10-19 18:14:33 +0200487 seq_puts(s, "Pin config settings per pin\n");
Dong Aisheng2aeefe02012-04-16 22:07:24 +0800488 seq_puts(s, "Format: pin (name): configs\n");
Linus Walleijae6b4d82011-10-19 18:14:33 +0200489
Stephen Warren57b676f2012-03-02 13:05:44 -0700490 mutex_lock(&pinctrl_mutex);
491
Chanho Park706e8522012-01-03 16:47:50 +0900492 /* The pin number can be retrived from the pin controller descriptor */
Stephen Warren546edd82012-01-06 13:38:31 -0700493 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200494 struct pin_desc *desc;
495
Chanho Park706e8522012-01-03 16:47:50 +0900496 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200497 desc = pin_desc_get(pctldev, pin);
Chanho Park706e8522012-01-03 16:47:50 +0900498 /* Skip if we cannot search the pin */
Linus Walleijae6b4d82011-10-19 18:14:33 +0200499 if (desc == NULL)
500 continue;
501
502 seq_printf(s, "pin %d (%s):", pin,
503 desc->name ? desc->name : "unnamed");
504
505 pinconf_dump_pin(pctldev, s, pin);
506
507 seq_printf(s, "\n");
508 }
509
Stephen Warren57b676f2012-03-02 13:05:44 -0700510 mutex_unlock(&pinctrl_mutex);
511
Linus Walleijae6b4d82011-10-19 18:14:33 +0200512 return 0;
513}
514
515static void pinconf_dump_group(struct pinctrl_dev *pctldev,
516 struct seq_file *s, unsigned selector,
517 const char *gname)
518{
519 const struct pinconf_ops *ops = pctldev->desc->confops;
520
Linus Walleij394349f2011-11-24 18:27:15 +0100521 /* no-op when not using generic pin config */
522 pinconf_generic_dump_group(pctldev, s, gname);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200523 if (ops && ops->pin_config_group_dbg_show)
524 ops->pin_config_group_dbg_show(pctldev, s, selector);
525}
526
527static int pinconf_groups_show(struct seq_file *s, void *what)
528{
529 struct pinctrl_dev *pctldev = s->private;
530 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
531 const struct pinconf_ops *ops = pctldev->desc->confops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530532 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200533 unsigned selector = 0;
534
535 if (!ops || !ops->pin_config_group_get)
536 return 0;
537
538 seq_puts(s, "Pin config settings per pin group\n");
Dong Aisheng2aeefe02012-04-16 22:07:24 +0800539 seq_puts(s, "Format: group (name): configs\n");
Linus Walleijae6b4d82011-10-19 18:14:33 +0200540
Viresh Kumard1e90e92012-03-30 11:25:40 +0530541 while (selector < ngroups) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200542 const char *gname = pctlops->get_group_name(pctldev, selector);
543
544 seq_printf(s, "%u (%s):", selector, gname);
545 pinconf_dump_group(pctldev, s, selector, gname);
Stephen Warrenf7b90062012-02-19 23:45:58 -0700546 seq_printf(s, "\n");
547
Linus Walleijae6b4d82011-10-19 18:14:33 +0200548 selector++;
549 }
550
Linus Walleijae6b4d82011-10-19 18:14:33 +0200551 return 0;
552}
553
554static int pinconf_pins_open(struct inode *inode, struct file *file)
555{
556 return single_open(file, pinconf_pins_show, inode->i_private);
557}
558
559static int pinconf_groups_open(struct inode *inode, struct file *file)
560{
561 return single_open(file, pinconf_groups_show, inode->i_private);
562}
563
564static const struct file_operations pinconf_pins_ops = {
565 .open = pinconf_pins_open,
566 .read = seq_read,
567 .llseek = seq_lseek,
568 .release = single_release,
569};
570
571static const struct file_operations pinconf_groups_ops = {
572 .open = pinconf_groups_open,
573 .read = seq_read,
574 .llseek = seq_lseek,
575 .release = single_release,
576};
577
Laurent Meunierf07512e2013-04-18 10:48:07 +0200578#define MAX_NAME_LEN 15
579
580struct dbg_cfg {
581 enum pinctrl_map_type map_type;
582 char dev_name[MAX_NAME_LEN+1];
583 char state_name[MAX_NAME_LEN+1];
584 char pin_name[MAX_NAME_LEN+1];
585};
586
587/*
588 * Goal is to keep this structure as global in order to simply read the
589 * pinconf-config file after a write to check config is as expected
590 */
591static struct dbg_cfg pinconf_dbg_conf;
592
593/**
594 * pinconf_dbg_config_print() - display the pinctrl config from the pinctrl
595 * map, of the dev/pin/state that was last written to pinconf-config file.
596 * @s: string filled in with config description
597 * @d: not used
598 */
599static int pinconf_dbg_config_print(struct seq_file *s, void *d)
600{
601 struct pinctrl_maps *maps_node;
602 const struct pinctrl_map *map;
603 struct pinctrl_dev *pctldev = NULL;
604 const struct pinconf_ops *confops = NULL;
605 const struct pinctrl_map_configs *configs;
606 struct dbg_cfg *dbg = &pinconf_dbg_conf;
607 int i, j;
608 bool found = false;
609 unsigned long config;
610
611 mutex_lock(&pinctrl_mutex);
612
613 /* Parse the pinctrl map and look for the elected pin/state */
614 for_each_maps(maps_node, i, map) {
615 if (map->type != dbg->map_type)
616 continue;
617 if (strcmp(map->dev_name, dbg->dev_name))
618 continue;
619 if (strcmp(map->name, dbg->state_name))
620 continue;
621
622 for (j = 0; j < map->data.configs.num_configs; j++) {
623 if (!strcmp(map->data.configs.group_or_pin,
624 dbg->pin_name)) {
625 /*
626 * We found the right pin / state, read the
627 * config and he pctldev for later use
628 */
629 configs = &map->data.configs;
630 pctldev = get_pinctrl_dev_from_devname
631 (map->ctrl_dev_name);
632 found = true;
633 break;
634 }
635 }
636 }
637
638 if (!found) {
639 seq_printf(s, "No config found for dev/state/pin, expected:\n");
640 seq_printf(s, "Searched dev:%s\n", dbg->dev_name);
641 seq_printf(s, "Searched state:%s\n", dbg->state_name);
642 seq_printf(s, "Searched pin:%s\n", dbg->pin_name);
643 seq_printf(s, "Use: modify config_pin <devname> "\
644 "<state> <pinname> <value>\n");
645 goto exit;
646 }
647
648 config = *(configs->configs);
649 seq_printf(s, "Dev %s has config of %s in state %s: 0x%08lX\n",
650 dbg->dev_name, dbg->pin_name,
651 dbg->state_name, config);
652
653 if (pctldev)
654 confops = pctldev->desc->confops;
655
656 if (confops && confops->pin_config_config_dbg_show)
657 confops->pin_config_config_dbg_show(pctldev, s, config);
658
659exit:
660 mutex_unlock(&pinctrl_mutex);
661
662 return 0;
663}
664
665/**
666 * pinconf_dbg_config_write() - modify the pinctrl config in the pinctrl
667 * map, of a dev/pin/state entry based on user entries to pinconf-config
668 * @user_buf: contains the modification request with expected format:
669 * modify config_pin <devicename> <state> <pinname> <newvalue>
670 * modify is literal string, alternatives like add/delete not supported yet
671 * config_pin is literal, alternatives like config_mux not supported yet
672 * <devicename> <state> <pinname> are values that should match the pinctrl-maps
673 * <newvalue> reflects the new config and is driver dependant
674 */
675static int pinconf_dbg_config_write(struct file *file,
676 const char __user *user_buf, size_t count, loff_t *ppos)
677{
678 struct pinctrl_maps *maps_node;
679 const struct pinctrl_map *map;
680 struct pinctrl_dev *pctldev = NULL;
681 const struct pinconf_ops *confops = NULL;
682 struct dbg_cfg *dbg = &pinconf_dbg_conf;
683 const struct pinctrl_map_configs *configs;
684 char config[MAX_NAME_LEN+1];
685 bool found = false;
686 char buf[128];
687 char *b = &buf[0];
688 int buf_size;
689 char *token;
690 int i;
691
692 /* Get userspace string and assure termination */
693 buf_size = min(count, (sizeof(buf)-1));
694 if (copy_from_user(buf, user_buf, buf_size))
695 return -EFAULT;
696 buf[buf_size] = 0;
697
698 /*
699 * need to parse entry and extract parameters:
700 * modify configs_pin devicename state pinname newvalue
701 */
702
703 /* Get arg: 'modify' */
704 token = strsep(&b, " ");
705 if (!token)
706 return -EINVAL;
707 if (strcmp(token, "modify"))
708 return -EINVAL;
709
710 /* Get arg type: "config_pin" type supported so far */
711 token = strsep(&b, " ");
712 if (!token)
713 return -EINVAL;
714 if (strcmp(token, "config_pin"))
715 return -EINVAL;
716 dbg->map_type = PIN_MAP_TYPE_CONFIGS_PIN;
717
718 /* get arg 'device_name' */
719 token = strsep(&b, " ");
720 if (token == NULL)
721 return -EINVAL;
722 if (strlen(token) >= MAX_NAME_LEN)
723 return -EINVAL;
724 strncpy(dbg->dev_name, token, MAX_NAME_LEN);
725
726 /* get arg 'state_name' */
727 token = strsep(&b, " ");
728 if (token == NULL)
729 return -EINVAL;
730 if (strlen(token) >= MAX_NAME_LEN)
731 return -EINVAL;
732 strncpy(dbg->state_name, token, MAX_NAME_LEN);
733
734 /* get arg 'pin_name' */
735 token = strsep(&b, " ");
736 if (token == NULL)
737 return -EINVAL;
738 if (strlen(token) >= MAX_NAME_LEN)
739 return -EINVAL;
740 strncpy(dbg->pin_name, token, MAX_NAME_LEN);
741
742 /* get new_value of config' */
743 token = strsep(&b, " ");
744 if (token == NULL)
745 return -EINVAL;
746 if (strlen(token) >= MAX_NAME_LEN)
747 return -EINVAL;
748 strncpy(config, token, MAX_NAME_LEN);
749
750 mutex_lock(&pinctrl_mutex);
751
752 /* Parse the pinctrl map and look for the selected dev/state/pin */
753 for_each_maps(maps_node, i, map) {
754 if (strcmp(map->dev_name, dbg->dev_name))
755 continue;
756 if (map->type != dbg->map_type)
757 continue;
758 if (strcmp(map->name, dbg->state_name))
759 continue;
760
761 /* we found the right pin / state, so overwrite config */
762 if (!strcmp(map->data.configs.group_or_pin, dbg->pin_name)) {
763 found = true;
764 pctldev = get_pinctrl_dev_from_devname(
765 map->ctrl_dev_name);
766 configs = &map->data.configs;
767 break;
768 }
769 }
770
771 if (!found) {
Laurent Meunierf07512e2013-04-18 10:48:07 +0200772 count = -EINVAL;
Laurent Meuniercb6d3152013-04-24 13:05:50 +0200773 goto exit;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200774 }
775
776 if (pctldev)
777 confops = pctldev->desc->confops;
778
779 if (confops && confops->pin_config_dbg_parse_modify) {
780 for (i = 0; i < configs->num_configs; i++) {
781 confops->pin_config_dbg_parse_modify(pctldev,
782 config,
783 &configs->configs[i]);
784 }
785 }
786
787exit:
788 mutex_unlock(&pinctrl_mutex);
789
790 return count;
791}
792
793static int pinconf_dbg_config_open(struct inode *inode, struct file *file)
794{
795 return single_open(file, pinconf_dbg_config_print, inode->i_private);
796}
797
798static const struct file_operations pinconf_dbg_pinconfig_fops = {
799 .open = pinconf_dbg_config_open,
800 .write = pinconf_dbg_config_write,
801 .read = seq_read,
802 .llseek = seq_lseek,
803 .release = single_release,
804 .owner = THIS_MODULE,
805};
806
Linus Walleijae6b4d82011-10-19 18:14:33 +0200807void pinconf_init_device_debugfs(struct dentry *devroot,
808 struct pinctrl_dev *pctldev)
809{
810 debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO,
811 devroot, pctldev, &pinconf_pins_ops);
812 debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO,
813 devroot, pctldev, &pinconf_groups_ops);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200814 debugfs_create_file("pinconf-config", (S_IRUGO | S_IWUSR | S_IWGRP),
815 devroot, pctldev, &pinconf_dbg_pinconfig_fops);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200816}
817
818#endif