blob: b8fcc38c0d116a97cf2b6f4142b026f8b8f94afb [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
Stephen Warren43699de2011-12-15 16:57:17 -070078int pin_config_group_get(const char *dev_name, const char *pin_group,
Linus Walleijae6b4d82011-10-19 18:14:33 +020079 unsigned long *config)
80{
Stephen Warren43699de2011-12-15 16:57:17 -070081 struct pinctrl_dev *pctldev;
82 const struct pinconf_ops *ops;
Stephen Warren57b676f2012-03-02 13:05:44 -070083 int selector, ret;
84
Linus Walleij9dfac4f2012-02-01 18:02:47 +010085 pctldev = get_pinctrl_dev_from_devname(dev_name);
Stephen Warren57b676f2012-03-02 13:05:44 -070086 if (!pctldev) {
87 ret = -EINVAL;
Patrice Chotard42fed7b2013-04-11 11:01:27 +020088 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -070089 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +020090
91 mutex_lock(&pctldev->mutex);
92
Stephen Warren43699de2011-12-15 16:57:17 -070093 ops = pctldev->desc->confops;
94
Linus Walleijae6b4d82011-10-19 18:14:33 +020095 if (!ops || !ops->pin_config_group_get) {
Stephen Warren51cd24e2011-12-09 16:59:05 -070096 dev_err(pctldev->dev, "cannot get configuration for pin "
Linus Walleijae6b4d82011-10-19 18:14:33 +020097 "group, missing group config get function in "
98 "driver\n");
Stephen Warren57b676f2012-03-02 13:05:44 -070099 ret = -EINVAL;
100 goto unlock;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200101 }
102
103 selector = pinctrl_get_group_selector(pctldev, pin_group);
Stephen Warren57b676f2012-03-02 13:05:44 -0700104 if (selector < 0) {
105 ret = selector;
106 goto unlock;
107 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200108
Stephen Warren57b676f2012-03-02 13:05:44 -0700109 ret = ops->pin_config_group_get(pctldev, selector, config);
110
111unlock:
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200112 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700113 return ret;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200114}
Linus Walleijae6b4d82011-10-19 18:14:33 +0200115
Stephen Warren1e2082b2012-03-02 13:05:48 -0700116int pinconf_map_to_setting(struct pinctrl_map const *map,
117 struct pinctrl_setting *setting)
118{
119 struct pinctrl_dev *pctldev = setting->pctldev;
Linus Walleij70b36372012-03-12 21:38:29 +0100120 int pin;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700121
122 switch (setting->type) {
123 case PIN_MAP_TYPE_CONFIGS_PIN:
Linus Walleij70b36372012-03-12 21:38:29 +0100124 pin = pin_get_from_name(pctldev,
125 map->data.configs.group_or_pin);
126 if (pin < 0) {
127 dev_err(pctldev->dev, "could not map pin config for \"%s\"",
128 map->data.configs.group_or_pin);
129 return pin;
130 }
131 setting->data.configs.group_or_pin = pin;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700132 break;
133 case PIN_MAP_TYPE_CONFIGS_GROUP:
Linus Walleij70b36372012-03-12 21:38:29 +0100134 pin = pinctrl_get_group_selector(pctldev,
135 map->data.configs.group_or_pin);
136 if (pin < 0) {
137 dev_err(pctldev->dev, "could not map group config for \"%s\"",
138 map->data.configs.group_or_pin);
139 return pin;
140 }
141 setting->data.configs.group_or_pin = pin;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700142 break;
143 default:
144 return -EINVAL;
145 }
146
147 setting->data.configs.num_configs = map->data.configs.num_configs;
148 setting->data.configs.configs = map->data.configs.configs;
149
150 return 0;
151}
152
153void pinconf_free_setting(struct pinctrl_setting const *setting)
154{
155}
156
157int pinconf_apply_setting(struct pinctrl_setting const *setting)
158{
159 struct pinctrl_dev *pctldev = setting->pctldev;
160 const struct pinconf_ops *ops = pctldev->desc->confops;
Sherman Yin03b054e2013-08-27 11:32:12 -0700161 int ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700162
163 if (!ops) {
164 dev_err(pctldev->dev, "missing confops\n");
165 return -EINVAL;
166 }
167
168 switch (setting->type) {
169 case PIN_MAP_TYPE_CONFIGS_PIN:
170 if (!ops->pin_config_set) {
171 dev_err(pctldev->dev, "missing pin_config_set op\n");
172 return -EINVAL;
173 }
Sherman Yin03b054e2013-08-27 11:32:12 -0700174 ret = ops->pin_config_set(pctldev,
175 setting->data.configs.group_or_pin,
176 setting->data.configs.configs,
177 setting->data.configs.num_configs);
178 if (ret < 0) {
179 dev_err(pctldev->dev,
180 "pin_config_set op failed for pin %d\n",
181 setting->data.configs.group_or_pin);
182 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700183 }
184 break;
185 case PIN_MAP_TYPE_CONFIGS_GROUP:
186 if (!ops->pin_config_group_set) {
187 dev_err(pctldev->dev,
188 "missing pin_config_group_set op\n");
189 return -EINVAL;
190 }
Sherman Yin03b054e2013-08-27 11:32:12 -0700191 ret = ops->pin_config_group_set(pctldev,
192 setting->data.configs.group_or_pin,
193 setting->data.configs.configs,
194 setting->data.configs.num_configs);
195 if (ret < 0) {
196 dev_err(pctldev->dev,
197 "pin_config_group_set op failed for group %d\n",
198 setting->data.configs.group_or_pin);
199 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700200 }
201 break;
202 default:
203 return -EINVAL;
204 }
205
206 return 0;
207}
208
Linus Walleijae6b4d82011-10-19 18:14:33 +0200209#ifdef CONFIG_DEBUG_FS
210
Stephen Warren1e2082b2012-03-02 13:05:48 -0700211void pinconf_show_map(struct seq_file *s, struct pinctrl_map const *map)
212{
Stephen Warren6cb41582012-04-13 10:49:06 -0600213 struct pinctrl_dev *pctldev;
214 const struct pinconf_ops *confops;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700215 int i;
216
Stephen Warren6cb41582012-04-13 10:49:06 -0600217 pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
218 if (pctldev)
219 confops = pctldev->desc->confops;
220 else
221 confops = NULL;
222
Stephen Warren1e2082b2012-03-02 13:05:48 -0700223 switch (map->type) {
224 case PIN_MAP_TYPE_CONFIGS_PIN:
225 seq_printf(s, "pin ");
226 break;
227 case PIN_MAP_TYPE_CONFIGS_GROUP:
228 seq_printf(s, "group ");
229 break;
230 default:
231 break;
232 }
233
234 seq_printf(s, "%s\n", map->data.configs.group_or_pin);
235
Stephen Warren6cb41582012-04-13 10:49:06 -0600236 for (i = 0; i < map->data.configs.num_configs; i++) {
237 seq_printf(s, "config ");
238 if (confops && confops->pin_config_config_dbg_show)
239 confops->pin_config_config_dbg_show(pctldev, s,
240 map->data.configs.configs[i]);
241 else
242 seq_printf(s, "%08lx", map->data.configs.configs[i]);
243 seq_printf(s, "\n");
244 }
Stephen Warren1e2082b2012-03-02 13:05:48 -0700245}
246
247void pinconf_show_setting(struct seq_file *s,
248 struct pinctrl_setting const *setting)
249{
250 struct pinctrl_dev *pctldev = setting->pctldev;
251 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Stephen Warren6cb41582012-04-13 10:49:06 -0600252 const struct pinconf_ops *confops = pctldev->desc->confops;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700253 struct pin_desc *desc;
254 int i;
255
256 switch (setting->type) {
257 case PIN_MAP_TYPE_CONFIGS_PIN:
258 desc = pin_desc_get(setting->pctldev,
259 setting->data.configs.group_or_pin);
260 seq_printf(s, "pin %s (%d)",
261 desc->name ? desc->name : "unnamed",
262 setting->data.configs.group_or_pin);
263 break;
264 case PIN_MAP_TYPE_CONFIGS_GROUP:
265 seq_printf(s, "group %s (%d)",
266 pctlops->get_group_name(pctldev,
267 setting->data.configs.group_or_pin),
268 setting->data.configs.group_or_pin);
269 break;
270 default:
271 break;
272 }
273
274 /*
275 * FIXME: We should really get the pin controler to dump the config
276 * values, so they can be decoded to something meaningful.
277 */
Stephen Warren6cb41582012-04-13 10:49:06 -0600278 for (i = 0; i < setting->data.configs.num_configs; i++) {
279 seq_printf(s, " ");
280 if (confops && confops->pin_config_config_dbg_show)
281 confops->pin_config_config_dbg_show(pctldev, s,
282 setting->data.configs.configs[i]);
283 else
284 seq_printf(s, "%08lx",
285 setting->data.configs.configs[i]);
286 }
Stephen Warren1e2082b2012-03-02 13:05:48 -0700287
288 seq_printf(s, "\n");
289}
290
Linus Walleijae6b4d82011-10-19 18:14:33 +0200291static void pinconf_dump_pin(struct pinctrl_dev *pctldev,
292 struct seq_file *s, int pin)
293{
294 const struct pinconf_ops *ops = pctldev->desc->confops;
295
Linus Walleij394349f2011-11-24 18:27:15 +0100296 /* no-op when not using generic pin config */
297 pinconf_generic_dump_pin(pctldev, s, pin);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200298 if (ops && ops->pin_config_dbg_show)
299 ops->pin_config_dbg_show(pctldev, s, pin);
300}
301
302static int pinconf_pins_show(struct seq_file *s, void *what)
303{
304 struct pinctrl_dev *pctldev = s->private;
Dong Aishengad8bb722012-04-10 12:41:34 +0800305 const struct pinconf_ops *ops = pctldev->desc->confops;
Chanho Park706e8522012-01-03 16:47:50 +0900306 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200307
Dong Aishengad8bb722012-04-10 12:41:34 +0800308 if (!ops || !ops->pin_config_get)
309 return 0;
310
Linus Walleijae6b4d82011-10-19 18:14:33 +0200311 seq_puts(s, "Pin config settings per pin\n");
Dong Aisheng2aeefe02012-04-16 22:07:24 +0800312 seq_puts(s, "Format: pin (name): configs\n");
Linus Walleijae6b4d82011-10-19 18:14:33 +0200313
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200314 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700315
Chanho Park706e8522012-01-03 16:47:50 +0900316 /* The pin number can be retrived from the pin controller descriptor */
Stephen Warren546edd82012-01-06 13:38:31 -0700317 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200318 struct pin_desc *desc;
319
Chanho Park706e8522012-01-03 16:47:50 +0900320 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200321 desc = pin_desc_get(pctldev, pin);
Chanho Park706e8522012-01-03 16:47:50 +0900322 /* Skip if we cannot search the pin */
Linus Walleijae6b4d82011-10-19 18:14:33 +0200323 if (desc == NULL)
324 continue;
325
326 seq_printf(s, "pin %d (%s):", pin,
327 desc->name ? desc->name : "unnamed");
328
329 pinconf_dump_pin(pctldev, s, pin);
330
331 seq_printf(s, "\n");
332 }
333
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200334 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700335
Linus Walleijae6b4d82011-10-19 18:14:33 +0200336 return 0;
337}
338
339static void pinconf_dump_group(struct pinctrl_dev *pctldev,
340 struct seq_file *s, unsigned selector,
341 const char *gname)
342{
343 const struct pinconf_ops *ops = pctldev->desc->confops;
344
Linus Walleij394349f2011-11-24 18:27:15 +0100345 /* no-op when not using generic pin config */
346 pinconf_generic_dump_group(pctldev, s, gname);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200347 if (ops && ops->pin_config_group_dbg_show)
348 ops->pin_config_group_dbg_show(pctldev, s, selector);
349}
350
351static int pinconf_groups_show(struct seq_file *s, void *what)
352{
353 struct pinctrl_dev *pctldev = s->private;
354 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
355 const struct pinconf_ops *ops = pctldev->desc->confops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530356 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200357 unsigned selector = 0;
358
359 if (!ops || !ops->pin_config_group_get)
360 return 0;
361
362 seq_puts(s, "Pin config settings per pin group\n");
Dong Aisheng2aeefe02012-04-16 22:07:24 +0800363 seq_puts(s, "Format: group (name): configs\n");
Linus Walleijae6b4d82011-10-19 18:14:33 +0200364
Viresh Kumard1e90e92012-03-30 11:25:40 +0530365 while (selector < ngroups) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200366 const char *gname = pctlops->get_group_name(pctldev, selector);
367
368 seq_printf(s, "%u (%s):", selector, gname);
369 pinconf_dump_group(pctldev, s, selector, gname);
Stephen Warrenf7b90062012-02-19 23:45:58 -0700370 seq_printf(s, "\n");
371
Linus Walleijae6b4d82011-10-19 18:14:33 +0200372 selector++;
373 }
374
Linus Walleijae6b4d82011-10-19 18:14:33 +0200375 return 0;
376}
377
378static int pinconf_pins_open(struct inode *inode, struct file *file)
379{
380 return single_open(file, pinconf_pins_show, inode->i_private);
381}
382
383static int pinconf_groups_open(struct inode *inode, struct file *file)
384{
385 return single_open(file, pinconf_groups_show, inode->i_private);
386}
387
388static const struct file_operations pinconf_pins_ops = {
389 .open = pinconf_pins_open,
390 .read = seq_read,
391 .llseek = seq_lseek,
392 .release = single_release,
393};
394
395static const struct file_operations pinconf_groups_ops = {
396 .open = pinconf_groups_open,
397 .read = seq_read,
398 .llseek = seq_lseek,
399 .release = single_release,
400};
401
Laurent Meunierf07512e2013-04-18 10:48:07 +0200402#define MAX_NAME_LEN 15
403
404struct dbg_cfg {
405 enum pinctrl_map_type map_type;
406 char dev_name[MAX_NAME_LEN+1];
407 char state_name[MAX_NAME_LEN+1];
408 char pin_name[MAX_NAME_LEN+1];
409};
410
411/*
412 * Goal is to keep this structure as global in order to simply read the
413 * pinconf-config file after a write to check config is as expected
414 */
415static struct dbg_cfg pinconf_dbg_conf;
416
417/**
418 * pinconf_dbg_config_print() - display the pinctrl config from the pinctrl
419 * map, of the dev/pin/state that was last written to pinconf-config file.
420 * @s: string filled in with config description
421 * @d: not used
422 */
423static int pinconf_dbg_config_print(struct seq_file *s, void *d)
424{
425 struct pinctrl_maps *maps_node;
426 const struct pinctrl_map *map;
Russell King - ARM Linux8a9dcc32013-07-28 13:13:00 +0100427 const struct pinctrl_map *found = NULL;
428 struct pinctrl_dev *pctldev;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200429 const struct pinconf_ops *confops = NULL;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200430 struct dbg_cfg *dbg = &pinconf_dbg_conf;
431 int i, j;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200432 unsigned long config;
433
Linus Walleija3862672013-05-27 15:53:32 +0200434 mutex_lock(&pinctrl_maps_mutex);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200435
436 /* Parse the pinctrl map and look for the elected pin/state */
437 for_each_maps(maps_node, i, map) {
438 if (map->type != dbg->map_type)
439 continue;
440 if (strcmp(map->dev_name, dbg->dev_name))
441 continue;
442 if (strcmp(map->name, dbg->state_name))
443 continue;
444
445 for (j = 0; j < map->data.configs.num_configs; j++) {
446 if (!strcmp(map->data.configs.group_or_pin,
447 dbg->pin_name)) {
Russell King - ARM Linux8a9dcc32013-07-28 13:13:00 +0100448 /* We found the right pin / state */
449 found = map;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200450 break;
451 }
452 }
453 }
454
455 if (!found) {
456 seq_printf(s, "No config found for dev/state/pin, expected:\n");
457 seq_printf(s, "Searched dev:%s\n", dbg->dev_name);
458 seq_printf(s, "Searched state:%s\n", dbg->state_name);
459 seq_printf(s, "Searched pin:%s\n", dbg->pin_name);
460 seq_printf(s, "Use: modify config_pin <devname> "\
461 "<state> <pinname> <value>\n");
462 goto exit;
463 }
464
Russell King - ARM Linux8a9dcc32013-07-28 13:13:00 +0100465 pctldev = get_pinctrl_dev_from_devname(found->ctrl_dev_name);
466 config = *found->data.configs.configs;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200467 seq_printf(s, "Dev %s has config of %s in state %s: 0x%08lX\n",
468 dbg->dev_name, dbg->pin_name,
469 dbg->state_name, config);
470
471 if (pctldev)
472 confops = pctldev->desc->confops;
473
474 if (confops && confops->pin_config_config_dbg_show)
475 confops->pin_config_config_dbg_show(pctldev, s, config);
476
477exit:
Linus Walleija3862672013-05-27 15:53:32 +0200478 mutex_unlock(&pinctrl_maps_mutex);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200479
480 return 0;
481}
482
483/**
484 * pinconf_dbg_config_write() - modify the pinctrl config in the pinctrl
485 * map, of a dev/pin/state entry based on user entries to pinconf-config
486 * @user_buf: contains the modification request with expected format:
487 * modify config_pin <devicename> <state> <pinname> <newvalue>
488 * modify is literal string, alternatives like add/delete not supported yet
489 * config_pin is literal, alternatives like config_mux not supported yet
490 * <devicename> <state> <pinname> are values that should match the pinctrl-maps
491 * <newvalue> reflects the new config and is driver dependant
492 */
Vincent Stehlé3b59e432013-09-16 22:50:32 +0200493static ssize_t pinconf_dbg_config_write(struct file *file,
Laurent Meunierf07512e2013-04-18 10:48:07 +0200494 const char __user *user_buf, size_t count, loff_t *ppos)
495{
496 struct pinctrl_maps *maps_node;
497 const struct pinctrl_map *map;
Russell King - ARM Linux8a9dcc32013-07-28 13:13:00 +0100498 const struct pinctrl_map *found = NULL;
499 struct pinctrl_dev *pctldev;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200500 const struct pinconf_ops *confops = NULL;
501 struct dbg_cfg *dbg = &pinconf_dbg_conf;
502 const struct pinctrl_map_configs *configs;
503 char config[MAX_NAME_LEN+1];
Laurent Meunierf07512e2013-04-18 10:48:07 +0200504 char buf[128];
505 char *b = &buf[0];
506 int buf_size;
507 char *token;
508 int i;
509
510 /* Get userspace string and assure termination */
Dan Carpenter81d36c42013-09-17 13:47:54 +0300511 buf_size = min(count, sizeof(buf) - 1);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200512 if (copy_from_user(buf, user_buf, buf_size))
513 return -EFAULT;
514 buf[buf_size] = 0;
515
516 /*
517 * need to parse entry and extract parameters:
518 * modify configs_pin devicename state pinname newvalue
519 */
520
521 /* Get arg: 'modify' */
522 token = strsep(&b, " ");
523 if (!token)
524 return -EINVAL;
525 if (strcmp(token, "modify"))
526 return -EINVAL;
527
528 /* Get arg type: "config_pin" type supported so far */
529 token = strsep(&b, " ");
530 if (!token)
531 return -EINVAL;
532 if (strcmp(token, "config_pin"))
533 return -EINVAL;
534 dbg->map_type = PIN_MAP_TYPE_CONFIGS_PIN;
535
536 /* get arg 'device_name' */
537 token = strsep(&b, " ");
538 if (token == NULL)
539 return -EINVAL;
540 if (strlen(token) >= MAX_NAME_LEN)
541 return -EINVAL;
542 strncpy(dbg->dev_name, token, MAX_NAME_LEN);
543
544 /* get arg 'state_name' */
545 token = strsep(&b, " ");
546 if (token == NULL)
547 return -EINVAL;
548 if (strlen(token) >= MAX_NAME_LEN)
549 return -EINVAL;
550 strncpy(dbg->state_name, token, MAX_NAME_LEN);
551
552 /* get arg 'pin_name' */
553 token = strsep(&b, " ");
554 if (token == NULL)
555 return -EINVAL;
556 if (strlen(token) >= MAX_NAME_LEN)
557 return -EINVAL;
558 strncpy(dbg->pin_name, token, MAX_NAME_LEN);
559
560 /* get new_value of config' */
561 token = strsep(&b, " ");
562 if (token == NULL)
563 return -EINVAL;
564 if (strlen(token) >= MAX_NAME_LEN)
565 return -EINVAL;
566 strncpy(config, token, MAX_NAME_LEN);
567
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200568 mutex_lock(&pinctrl_maps_mutex);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200569
570 /* Parse the pinctrl map and look for the selected dev/state/pin */
571 for_each_maps(maps_node, i, map) {
572 if (strcmp(map->dev_name, dbg->dev_name))
573 continue;
574 if (map->type != dbg->map_type)
575 continue;
576 if (strcmp(map->name, dbg->state_name))
577 continue;
578
579 /* we found the right pin / state, so overwrite config */
580 if (!strcmp(map->data.configs.group_or_pin, dbg->pin_name)) {
Russell King - ARM Linux8a9dcc32013-07-28 13:13:00 +0100581 found = map;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200582 break;
583 }
584 }
585
586 if (!found) {
Laurent Meunierf07512e2013-04-18 10:48:07 +0200587 count = -EINVAL;
Laurent Meuniercb6d3152013-04-24 13:05:50 +0200588 goto exit;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200589 }
590
Russell King - ARM Linux8a9dcc32013-07-28 13:13:00 +0100591 pctldev = get_pinctrl_dev_from_devname(found->ctrl_dev_name);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200592 if (pctldev)
593 confops = pctldev->desc->confops;
594
595 if (confops && confops->pin_config_dbg_parse_modify) {
Russell King - ARM Linux8a9dcc32013-07-28 13:13:00 +0100596 configs = &found->data.configs;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200597 for (i = 0; i < configs->num_configs; i++) {
598 confops->pin_config_dbg_parse_modify(pctldev,
599 config,
600 &configs->configs[i]);
601 }
602 }
603
604exit:
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200605 mutex_unlock(&pinctrl_maps_mutex);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200606
607 return count;
608}
609
610static int pinconf_dbg_config_open(struct inode *inode, struct file *file)
611{
612 return single_open(file, pinconf_dbg_config_print, inode->i_private);
613}
614
615static const struct file_operations pinconf_dbg_pinconfig_fops = {
616 .open = pinconf_dbg_config_open,
617 .write = pinconf_dbg_config_write,
618 .read = seq_read,
619 .llseek = seq_lseek,
620 .release = single_release,
621 .owner = THIS_MODULE,
622};
623
Linus Walleijae6b4d82011-10-19 18:14:33 +0200624void pinconf_init_device_debugfs(struct dentry *devroot,
625 struct pinctrl_dev *pctldev)
626{
627 debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO,
628 devroot, pctldev, &pinconf_pins_ops);
629 debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO,
630 devroot, pctldev, &pinconf_groups_ops);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200631 debugfs_create_file("pinconf-config", (S_IRUGO | S_IWUSR | S_IWGRP),
632 devroot, pctldev, &pinconf_dbg_pinconfig_fops);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200633}
634
635#endif