blob: a02dba35fcf39e7119dbd568cf32d8beca53b199 [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
Stephen Warren2b694252012-02-19 23:45:46 -070031 /* We have to be able to config the pins in SOME way */
John Crispinad6e1102012-04-26 16:47:11 +020032 if (!ops->pin_config_set && !ops->pin_config_group_set) {
33 dev_err(pctldev->dev,
34 "pinconf has to be able to set a pins config\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 return 0;
38}
39
Stephen Warren1e2082b2012-03-02 13:05:48 -070040int pinconf_validate_map(struct pinctrl_map const *map, int i)
41{
42 if (!map->data.configs.group_or_pin) {
43 pr_err("failed to register map %s (%d): no group/pin given\n",
44 map->name, i);
45 return -EINVAL;
46 }
47
Dong Aishengc95df2d2012-05-14 19:06:36 +080048 if (!map->data.configs.num_configs ||
Stephen Warren1e2082b2012-03-02 13:05:48 -070049 !map->data.configs.configs) {
Dong Aishengc95df2d2012-05-14 19:06:36 +080050 pr_err("failed to register map %s (%d): no configs given\n",
Stephen Warren1e2082b2012-03-02 13:05:48 -070051 map->name, i);
52 return -EINVAL;
53 }
54
55 return 0;
56}
57
Linus Walleij394349f2011-11-24 18:27:15 +010058int pin_config_get_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
Linus Walleijae6b4d82011-10-19 18:14:33 +020059 unsigned long *config)
60{
61 const struct pinconf_ops *ops = pctldev->desc->confops;
62
63 if (!ops || !ops->pin_config_get) {
Masahiro Yamadaca67f102015-07-30 17:27:44 +090064 dev_dbg(pctldev->dev,
65 "cannot get pin configuration, .pin_config_get missing in driver\n");
Alexandre Bellonic4206192013-12-09 11:38:29 +010066 return -ENOTSUPP;
Linus Walleijae6b4d82011-10-19 18:14:33 +020067 }
68
69 return ops->pin_config_get(pctldev, pin, config);
70}
71
Stephen Warren43699de2011-12-15 16:57:17 -070072int pin_config_group_get(const char *dev_name, const char *pin_group,
Linus Walleijae6b4d82011-10-19 18:14:33 +020073 unsigned long *config)
74{
Stephen Warren43699de2011-12-15 16:57:17 -070075 struct pinctrl_dev *pctldev;
76 const struct pinconf_ops *ops;
Stephen Warren57b676f2012-03-02 13:05:44 -070077 int selector, ret;
78
Linus Walleij9dfac4f2012-02-01 18:02:47 +010079 pctldev = get_pinctrl_dev_from_devname(dev_name);
Stephen Warren57b676f2012-03-02 13:05:44 -070080 if (!pctldev) {
81 ret = -EINVAL;
Patrice Chotard42fed7b2013-04-11 11:01:27 +020082 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -070083 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +020084
85 mutex_lock(&pctldev->mutex);
86
Stephen Warren43699de2011-12-15 16:57:17 -070087 ops = pctldev->desc->confops;
88
Linus Walleijae6b4d82011-10-19 18:14:33 +020089 if (!ops || !ops->pin_config_group_get) {
Alexandre Bellonic4206192013-12-09 11:38:29 +010090 dev_dbg(pctldev->dev, "cannot get configuration for pin "
Linus Walleijae6b4d82011-10-19 18:14:33 +020091 "group, missing group config get function in "
92 "driver\n");
Alexandre Bellonic4206192013-12-09 11:38:29 +010093 ret = -ENOTSUPP;
Stephen Warren57b676f2012-03-02 13:05:44 -070094 goto unlock;
Linus Walleijae6b4d82011-10-19 18:14:33 +020095 }
96
97 selector = pinctrl_get_group_selector(pctldev, pin_group);
Stephen Warren57b676f2012-03-02 13:05:44 -070098 if (selector < 0) {
99 ret = selector;
100 goto unlock;
101 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200102
Stephen Warren57b676f2012-03-02 13:05:44 -0700103 ret = ops->pin_config_group_get(pctldev, selector, config);
104
105unlock:
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200106 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700107 return ret;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200108}
Linus Walleijae6b4d82011-10-19 18:14:33 +0200109
Stephen Warren1e2082b2012-03-02 13:05:48 -0700110int pinconf_map_to_setting(struct pinctrl_map const *map,
111 struct pinctrl_setting *setting)
112{
113 struct pinctrl_dev *pctldev = setting->pctldev;
Linus Walleij70b36372012-03-12 21:38:29 +0100114 int pin;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700115
116 switch (setting->type) {
117 case PIN_MAP_TYPE_CONFIGS_PIN:
Linus Walleij70b36372012-03-12 21:38:29 +0100118 pin = pin_get_from_name(pctldev,
119 map->data.configs.group_or_pin);
120 if (pin < 0) {
121 dev_err(pctldev->dev, "could not map pin config for \"%s\"",
122 map->data.configs.group_or_pin);
123 return pin;
124 }
125 setting->data.configs.group_or_pin = pin;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700126 break;
127 case PIN_MAP_TYPE_CONFIGS_GROUP:
Linus Walleij70b36372012-03-12 21:38:29 +0100128 pin = pinctrl_get_group_selector(pctldev,
129 map->data.configs.group_or_pin);
130 if (pin < 0) {
131 dev_err(pctldev->dev, "could not map group config for \"%s\"",
132 map->data.configs.group_or_pin);
133 return pin;
134 }
135 setting->data.configs.group_or_pin = pin;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700136 break;
137 default:
138 return -EINVAL;
139 }
140
141 setting->data.configs.num_configs = map->data.configs.num_configs;
142 setting->data.configs.configs = map->data.configs.configs;
143
144 return 0;
145}
146
147void pinconf_free_setting(struct pinctrl_setting const *setting)
148{
149}
150
151int pinconf_apply_setting(struct pinctrl_setting const *setting)
152{
153 struct pinctrl_dev *pctldev = setting->pctldev;
154 const struct pinconf_ops *ops = pctldev->desc->confops;
Sherman Yin03b054e2013-08-27 11:32:12 -0700155 int ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700156
157 if (!ops) {
158 dev_err(pctldev->dev, "missing confops\n");
159 return -EINVAL;
160 }
161
162 switch (setting->type) {
163 case PIN_MAP_TYPE_CONFIGS_PIN:
164 if (!ops->pin_config_set) {
165 dev_err(pctldev->dev, "missing pin_config_set op\n");
166 return -EINVAL;
167 }
Sherman Yin03b054e2013-08-27 11:32:12 -0700168 ret = ops->pin_config_set(pctldev,
169 setting->data.configs.group_or_pin,
170 setting->data.configs.configs,
171 setting->data.configs.num_configs);
172 if (ret < 0) {
173 dev_err(pctldev->dev,
174 "pin_config_set op failed for pin %d\n",
175 setting->data.configs.group_or_pin);
176 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700177 }
178 break;
179 case PIN_MAP_TYPE_CONFIGS_GROUP:
180 if (!ops->pin_config_group_set) {
181 dev_err(pctldev->dev,
182 "missing pin_config_group_set op\n");
183 return -EINVAL;
184 }
Sherman Yin03b054e2013-08-27 11:32:12 -0700185 ret = ops->pin_config_group_set(pctldev,
186 setting->data.configs.group_or_pin,
187 setting->data.configs.configs,
188 setting->data.configs.num_configs);
189 if (ret < 0) {
190 dev_err(pctldev->dev,
191 "pin_config_group_set op failed for group %d\n",
192 setting->data.configs.group_or_pin);
193 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700194 }
195 break;
196 default:
197 return -EINVAL;
198 }
199
200 return 0;
201}
202
Mika Westerberg15381bc2017-01-23 15:34:33 +0300203int pinconf_set_config(struct pinctrl_dev *pctldev, unsigned pin,
204 unsigned long *configs, size_t nconfigs)
205{
206 const struct pinconf_ops *ops;
207
208 ops = pctldev->desc->confops;
209 if (!ops)
210 return -ENOTSUPP;
211
212 return ops->pin_config_set(pctldev, pin, configs, nconfigs);
213}
214
Linus Walleijae6b4d82011-10-19 18:14:33 +0200215#ifdef CONFIG_DEBUG_FS
216
kbuild test robot6de52c12015-07-17 21:37:09 +0800217static void pinconf_show_config(struct seq_file *s, struct pinctrl_dev *pctldev,
Jon Hunterd96310a2015-07-14 11:17:59 +0100218 unsigned long *configs, unsigned num_configs)
Stephen Warren1e2082b2012-03-02 13:05:48 -0700219{
Stephen Warren6cb41582012-04-13 10:49:06 -0600220 const struct pinconf_ops *confops;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700221 int i;
222
Stephen Warren6cb41582012-04-13 10:49:06 -0600223 if (pctldev)
224 confops = pctldev->desc->confops;
225 else
226 confops = NULL;
227
Jon Hunterd96310a2015-07-14 11:17:59 +0100228 for (i = 0; i < num_configs; i++) {
229 seq_puts(s, "config ");
230 if (confops && confops->pin_config_config_dbg_show)
231 confops->pin_config_config_dbg_show(pctldev, s,
232 configs[i]);
233 else
234 seq_printf(s, "%08lx", configs[i]);
235 seq_puts(s, "\n");
236 }
237}
238
239void pinconf_show_map(struct seq_file *s, struct pinctrl_map const *map)
240{
241 struct pinctrl_dev *pctldev;
242
243 pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
244
Stephen Warren1e2082b2012-03-02 13:05:48 -0700245 switch (map->type) {
246 case PIN_MAP_TYPE_CONFIGS_PIN:
247 seq_printf(s, "pin ");
248 break;
249 case PIN_MAP_TYPE_CONFIGS_GROUP:
250 seq_printf(s, "group ");
251 break;
252 default:
253 break;
254 }
255
256 seq_printf(s, "%s\n", map->data.configs.group_or_pin);
257
Jon Hunterd96310a2015-07-14 11:17:59 +0100258 pinconf_show_config(s, pctldev, map->data.configs.configs,
259 map->data.configs.num_configs);
Stephen Warren1e2082b2012-03-02 13:05:48 -0700260}
261
262void pinconf_show_setting(struct seq_file *s,
263 struct pinctrl_setting const *setting)
264{
265 struct pinctrl_dev *pctldev = setting->pctldev;
266 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
267 struct pin_desc *desc;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700268
269 switch (setting->type) {
270 case PIN_MAP_TYPE_CONFIGS_PIN:
271 desc = pin_desc_get(setting->pctldev,
272 setting->data.configs.group_or_pin);
Masahiro Yamadacf9d9942016-05-24 14:26:26 +0900273 seq_printf(s, "pin %s (%d)", desc->name,
Stephen Warren1e2082b2012-03-02 13:05:48 -0700274 setting->data.configs.group_or_pin);
275 break;
276 case PIN_MAP_TYPE_CONFIGS_GROUP:
277 seq_printf(s, "group %s (%d)",
278 pctlops->get_group_name(pctldev,
279 setting->data.configs.group_or_pin),
280 setting->data.configs.group_or_pin);
281 break;
282 default:
283 break;
284 }
285
286 /*
Andy Shevchenko3ec440e2017-02-28 16:59:56 +0200287 * FIXME: We should really get the pin controller to dump the config
Stephen Warren1e2082b2012-03-02 13:05:48 -0700288 * values, so they can be decoded to something meaningful.
289 */
Jon Hunterd96310a2015-07-14 11:17:59 +0100290 pinconf_show_config(s, pctldev, setting->data.configs.configs,
291 setting->data.configs.num_configs);
Stephen Warren1e2082b2012-03-02 13:05:48 -0700292}
293
Linus Walleijae6b4d82011-10-19 18:14:33 +0200294static void pinconf_dump_pin(struct pinctrl_dev *pctldev,
295 struct seq_file *s, int pin)
296{
297 const struct pinconf_ops *ops = pctldev->desc->confops;
298
Linus Walleij394349f2011-11-24 18:27:15 +0100299 /* no-op when not using generic pin config */
Soren Brinkmanndd4d01f2015-01-09 07:43:46 -0800300 pinconf_generic_dump_pins(pctldev, s, NULL, pin);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200301 if (ops && ops->pin_config_dbg_show)
302 ops->pin_config_dbg_show(pctldev, s, pin);
303}
304
305static int pinconf_pins_show(struct seq_file *s, void *what)
306{
307 struct pinctrl_dev *pctldev = s->private;
Chanho Park706e8522012-01-03 16:47:50 +0900308 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200309
310 seq_puts(s, "Pin config settings per pin\n");
Dong Aisheng2aeefe02012-04-16 22:07:24 +0800311 seq_puts(s, "Format: pin (name): configs\n");
Linus Walleijae6b4d82011-10-19 18:14:33 +0200312
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200313 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700314
Chanho Park706e8522012-01-03 16:47:50 +0900315 /* The pin number can be retrived from the pin controller descriptor */
Stephen Warren546edd82012-01-06 13:38:31 -0700316 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200317 struct pin_desc *desc;
318
Chanho Park706e8522012-01-03 16:47:50 +0900319 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200320 desc = pin_desc_get(pctldev, pin);
Chanho Park706e8522012-01-03 16:47:50 +0900321 /* Skip if we cannot search the pin */
Linus Walleijae6b4d82011-10-19 18:14:33 +0200322 if (desc == NULL)
323 continue;
324
Masahiro Yamadaa672eb52016-05-25 15:37:27 +0900325 seq_printf(s, "pin %d (%s): ", pin, desc->name);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200326
327 pinconf_dump_pin(pctldev, s, pin);
328
329 seq_printf(s, "\n");
330 }
331
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200332 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700333
Linus Walleijae6b4d82011-10-19 18:14:33 +0200334 return 0;
335}
336
337static void pinconf_dump_group(struct pinctrl_dev *pctldev,
338 struct seq_file *s, unsigned selector,
339 const char *gname)
340{
341 const struct pinconf_ops *ops = pctldev->desc->confops;
342
Linus Walleij394349f2011-11-24 18:27:15 +0100343 /* no-op when not using generic pin config */
Soren Brinkmanndd4d01f2015-01-09 07:43:46 -0800344 pinconf_generic_dump_pins(pctldev, s, gname, 0);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200345 if (ops && ops->pin_config_group_dbg_show)
346 ops->pin_config_group_dbg_show(pctldev, s, selector);
347}
348
349static int pinconf_groups_show(struct seq_file *s, void *what)
350{
351 struct pinctrl_dev *pctldev = s->private;
352 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530353 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200354 unsigned selector = 0;
355
Linus Walleijae6b4d82011-10-19 18:14:33 +0200356 seq_puts(s, "Pin config settings per pin group\n");
Dong Aisheng2aeefe02012-04-16 22:07:24 +0800357 seq_puts(s, "Format: group (name): configs\n");
Linus Walleijae6b4d82011-10-19 18:14:33 +0200358
Viresh Kumard1e90e92012-03-30 11:25:40 +0530359 while (selector < ngroups) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200360 const char *gname = pctlops->get_group_name(pctldev, selector);
361
Masahiro Yamadaa672eb52016-05-25 15:37:27 +0900362 seq_printf(s, "%u (%s): ", selector, gname);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200363 pinconf_dump_group(pctldev, s, selector, gname);
Stephen Warrenf7b90062012-02-19 23:45:58 -0700364 seq_printf(s, "\n");
365
Linus Walleijae6b4d82011-10-19 18:14:33 +0200366 selector++;
367 }
368
Linus Walleijae6b4d82011-10-19 18:14:33 +0200369 return 0;
370}
371
372static int pinconf_pins_open(struct inode *inode, struct file *file)
373{
374 return single_open(file, pinconf_pins_show, inode->i_private);
375}
376
377static int pinconf_groups_open(struct inode *inode, struct file *file)
378{
379 return single_open(file, pinconf_groups_show, inode->i_private);
380}
381
382static const struct file_operations pinconf_pins_ops = {
383 .open = pinconf_pins_open,
384 .read = seq_read,
385 .llseek = seq_lseek,
386 .release = single_release,
387};
388
389static const struct file_operations pinconf_groups_ops = {
390 .open = pinconf_groups_open,
391 .read = seq_read,
392 .llseek = seq_lseek,
393 .release = single_release,
394};
395
Laurent Meunierf07512e2013-04-18 10:48:07 +0200396#define MAX_NAME_LEN 15
397
398struct dbg_cfg {
399 enum pinctrl_map_type map_type;
400 char dev_name[MAX_NAME_LEN+1];
401 char state_name[MAX_NAME_LEN+1];
402 char pin_name[MAX_NAME_LEN+1];
403};
404
405/*
406 * Goal is to keep this structure as global in order to simply read the
407 * pinconf-config file after a write to check config is as expected
408 */
409static struct dbg_cfg pinconf_dbg_conf;
410
411/**
412 * pinconf_dbg_config_print() - display the pinctrl config from the pinctrl
413 * map, of the dev/pin/state that was last written to pinconf-config file.
414 * @s: string filled in with config description
415 * @d: not used
416 */
417static int pinconf_dbg_config_print(struct seq_file *s, void *d)
418{
419 struct pinctrl_maps *maps_node;
420 const struct pinctrl_map *map;
Russell King - ARM Linux8a9dcc32013-07-28 13:13:00 +0100421 const struct pinctrl_map *found = NULL;
422 struct pinctrl_dev *pctldev;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200423 struct dbg_cfg *dbg = &pinconf_dbg_conf;
Laurent Meunierd99c8052015-10-30 15:15:51 +0100424 int i;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200425
Linus Walleija3862672013-05-27 15:53:32 +0200426 mutex_lock(&pinctrl_maps_mutex);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200427
428 /* Parse the pinctrl map and look for the elected pin/state */
429 for_each_maps(maps_node, i, map) {
430 if (map->type != dbg->map_type)
431 continue;
432 if (strcmp(map->dev_name, dbg->dev_name))
433 continue;
434 if (strcmp(map->name, dbg->state_name))
435 continue;
436
Laurent Meunierd99c8052015-10-30 15:15:51 +0100437 if (!strcmp(map->data.configs.group_or_pin, dbg->pin_name)) {
438 /* We found the right pin */
439 found = map;
440 break;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200441 }
442 }
443
444 if (!found) {
445 seq_printf(s, "No config found for dev/state/pin, expected:\n");
446 seq_printf(s, "Searched dev:%s\n", dbg->dev_name);
447 seq_printf(s, "Searched state:%s\n", dbg->state_name);
448 seq_printf(s, "Searched pin:%s\n", dbg->pin_name);
449 seq_printf(s, "Use: modify config_pin <devname> "\
450 "<state> <pinname> <value>\n");
451 goto exit;
452 }
453
Russell King - ARM Linux8a9dcc32013-07-28 13:13:00 +0100454 pctldev = get_pinctrl_dev_from_devname(found->ctrl_dev_name);
Jon Hunterd96310a2015-07-14 11:17:59 +0100455 seq_printf(s, "Dev %s has config of %s in state %s:\n",
456 dbg->dev_name, dbg->pin_name, dbg->state_name);
457 pinconf_show_config(s, pctldev, found->data.configs.configs,
458 found->data.configs.num_configs);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200459
460exit:
Linus Walleija3862672013-05-27 15:53:32 +0200461 mutex_unlock(&pinctrl_maps_mutex);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200462
463 return 0;
464}
465
466/**
467 * pinconf_dbg_config_write() - modify the pinctrl config in the pinctrl
468 * map, of a dev/pin/state entry based on user entries to pinconf-config
469 * @user_buf: contains the modification request with expected format:
Jon Hunter75629982015-07-14 11:17:58 +0100470 * modify <config> <devicename> <state> <name> <newvalue>
Laurent Meunierf07512e2013-04-18 10:48:07 +0200471 * modify is literal string, alternatives like add/delete not supported yet
Jon Hunter75629982015-07-14 11:17:58 +0100472 * <config> is the configuration to be changed. Supported configs are
473 * "config_pin" or "config_group", alternatives like config_mux are not
474 * supported yet.
475 * <devicename> <state> <name> are values that should match the pinctrl-maps
Andy Shevchenko3ec440e2017-02-28 16:59:56 +0200476 * <newvalue> reflects the new config and is driver dependent
Laurent Meunierf07512e2013-04-18 10:48:07 +0200477 */
Vincent Stehlé3b59e432013-09-16 22:50:32 +0200478static ssize_t pinconf_dbg_config_write(struct file *file,
Laurent Meunierf07512e2013-04-18 10:48:07 +0200479 const char __user *user_buf, size_t count, loff_t *ppos)
480{
481 struct pinctrl_maps *maps_node;
482 const struct pinctrl_map *map;
Russell King - ARM Linux8a9dcc32013-07-28 13:13:00 +0100483 const struct pinctrl_map *found = NULL;
484 struct pinctrl_dev *pctldev;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200485 const struct pinconf_ops *confops = NULL;
486 struct dbg_cfg *dbg = &pinconf_dbg_conf;
487 const struct pinctrl_map_configs *configs;
488 char config[MAX_NAME_LEN+1];
Laurent Meunierf07512e2013-04-18 10:48:07 +0200489 char buf[128];
490 char *b = &buf[0];
491 int buf_size;
492 char *token;
493 int i;
494
495 /* Get userspace string and assure termination */
Dan Carpenter81d36c42013-09-17 13:47:54 +0300496 buf_size = min(count, sizeof(buf) - 1);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200497 if (copy_from_user(buf, user_buf, buf_size))
498 return -EFAULT;
499 buf[buf_size] = 0;
500
501 /*
502 * need to parse entry and extract parameters:
503 * modify configs_pin devicename state pinname newvalue
504 */
505
506 /* Get arg: 'modify' */
507 token = strsep(&b, " ");
508 if (!token)
509 return -EINVAL;
510 if (strcmp(token, "modify"))
511 return -EINVAL;
512
Jon Hunter75629982015-07-14 11:17:58 +0100513 /*
514 * Get arg type: "config_pin" and "config_group"
515 * types are supported so far
516 */
Laurent Meunierf07512e2013-04-18 10:48:07 +0200517 token = strsep(&b, " ");
518 if (!token)
519 return -EINVAL;
Jon Hunter75629982015-07-14 11:17:58 +0100520 if (!strcmp(token, "config_pin"))
521 dbg->map_type = PIN_MAP_TYPE_CONFIGS_PIN;
522 else if (!strcmp(token, "config_group"))
523 dbg->map_type = PIN_MAP_TYPE_CONFIGS_GROUP;
524 else
Laurent Meunierf07512e2013-04-18 10:48:07 +0200525 return -EINVAL;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200526
527 /* get arg 'device_name' */
528 token = strsep(&b, " ");
529 if (token == NULL)
530 return -EINVAL;
531 if (strlen(token) >= MAX_NAME_LEN)
532 return -EINVAL;
533 strncpy(dbg->dev_name, token, MAX_NAME_LEN);
534
535 /* get arg 'state_name' */
536 token = strsep(&b, " ");
537 if (token == NULL)
538 return -EINVAL;
539 if (strlen(token) >= MAX_NAME_LEN)
540 return -EINVAL;
541 strncpy(dbg->state_name, token, MAX_NAME_LEN);
542
543 /* get arg 'pin_name' */
544 token = strsep(&b, " ");
545 if (token == NULL)
546 return -EINVAL;
547 if (strlen(token) >= MAX_NAME_LEN)
548 return -EINVAL;
549 strncpy(dbg->pin_name, token, MAX_NAME_LEN);
550
551 /* get new_value of config' */
552 token = strsep(&b, " ");
553 if (token == NULL)
554 return -EINVAL;
555 if (strlen(token) >= MAX_NAME_LEN)
556 return -EINVAL;
557 strncpy(config, token, MAX_NAME_LEN);
558
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200559 mutex_lock(&pinctrl_maps_mutex);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200560
561 /* Parse the pinctrl map and look for the selected dev/state/pin */
562 for_each_maps(maps_node, i, map) {
563 if (strcmp(map->dev_name, dbg->dev_name))
564 continue;
565 if (map->type != dbg->map_type)
566 continue;
567 if (strcmp(map->name, dbg->state_name))
568 continue;
569
570 /* we found the right pin / state, so overwrite config */
571 if (!strcmp(map->data.configs.group_or_pin, dbg->pin_name)) {
Russell King - ARM Linux8a9dcc32013-07-28 13:13:00 +0100572 found = map;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200573 break;
574 }
575 }
576
577 if (!found) {
Laurent Meunierf07512e2013-04-18 10:48:07 +0200578 count = -EINVAL;
Laurent Meuniercb6d3152013-04-24 13:05:50 +0200579 goto exit;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200580 }
581
Russell King - ARM Linux8a9dcc32013-07-28 13:13:00 +0100582 pctldev = get_pinctrl_dev_from_devname(found->ctrl_dev_name);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200583 if (pctldev)
584 confops = pctldev->desc->confops;
585
586 if (confops && confops->pin_config_dbg_parse_modify) {
Russell King - ARM Linux8a9dcc32013-07-28 13:13:00 +0100587 configs = &found->data.configs;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200588 for (i = 0; i < configs->num_configs; i++) {
589 confops->pin_config_dbg_parse_modify(pctldev,
590 config,
591 &configs->configs[i]);
592 }
593 }
594
595exit:
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200596 mutex_unlock(&pinctrl_maps_mutex);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200597
598 return count;
599}
600
601static int pinconf_dbg_config_open(struct inode *inode, struct file *file)
602{
603 return single_open(file, pinconf_dbg_config_print, inode->i_private);
604}
605
606static const struct file_operations pinconf_dbg_pinconfig_fops = {
607 .open = pinconf_dbg_config_open,
608 .write = pinconf_dbg_config_write,
609 .read = seq_read,
610 .llseek = seq_lseek,
611 .release = single_release,
612 .owner = THIS_MODULE,
613};
614
Linus Walleijae6b4d82011-10-19 18:14:33 +0200615void pinconf_init_device_debugfs(struct dentry *devroot,
616 struct pinctrl_dev *pctldev)
617{
618 debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO,
619 devroot, pctldev, &pinconf_pins_ops);
620 debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO,
621 devroot, pctldev, &pinconf_groups_ops);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200622 debugfs_create_file("pinconf-config", (S_IRUGO | S_IWUSR | S_IWGRP),
623 devroot, pctldev, &pinconf_dbg_pinconfig_fops);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200624}
625
626#endif