blob: 596a2522a6b1750b2c4c94c22f7c4177847f5bb4 [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 */
Sherman Yin18813842014-05-14 18:30:47 -070038 if (!ops->pin_config_set && !ops->pin_config_group_set
39 && !ops->pin_config_set_bulk
40 && !ops->pin_config_group_set_bulk) {
John Crispinad6e1102012-04-26 16:47:11 +020041 dev_err(pctldev->dev,
42 "pinconf has to be able to set a pins config\n");
Stephen Warren2b694252012-02-19 23:45:46 -070043 return -EINVAL;
John Crispinad6e1102012-04-26 16:47:11 +020044 }
Stephen Warren2b694252012-02-19 23:45:46 -070045 return 0;
46}
47
Stephen Warren1e2082b2012-03-02 13:05:48 -070048int pinconf_validate_map(struct pinctrl_map const *map, int i)
49{
50 if (!map->data.configs.group_or_pin) {
51 pr_err("failed to register map %s (%d): no group/pin given\n",
52 map->name, i);
53 return -EINVAL;
54 }
55
Dong Aishengc95df2d2012-05-14 19:06:36 +080056 if (!map->data.configs.num_configs ||
Stephen Warren1e2082b2012-03-02 13:05:48 -070057 !map->data.configs.configs) {
Dong Aishengc95df2d2012-05-14 19:06:36 +080058 pr_err("failed to register map %s (%d): no configs given\n",
Stephen Warren1e2082b2012-03-02 13:05:48 -070059 map->name, i);
60 return -EINVAL;
61 }
62
63 return 0;
64}
65
Linus Walleij394349f2011-11-24 18:27:15 +010066int pin_config_get_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
Linus Walleijae6b4d82011-10-19 18:14:33 +020067 unsigned long *config)
68{
69 const struct pinconf_ops *ops = pctldev->desc->confops;
70
71 if (!ops || !ops->pin_config_get) {
Stephen Warren51cd24e2011-12-09 16:59:05 -070072 dev_err(pctldev->dev, "cannot get pin configuration, missing "
Linus Walleijae6b4d82011-10-19 18:14:33 +020073 "pin_config_get() function in driver\n");
74 return -EINVAL;
75 }
76
77 return ops->pin_config_get(pctldev, pin, config);
78}
79
Stephen Warren43699de2011-12-15 16:57:17 -070080int pin_config_group_get(const char *dev_name, const char *pin_group,
Linus Walleijae6b4d82011-10-19 18:14:33 +020081 unsigned long *config)
82{
Stephen Warren43699de2011-12-15 16:57:17 -070083 struct pinctrl_dev *pctldev;
84 const struct pinconf_ops *ops;
Stephen Warren57b676f2012-03-02 13:05:44 -070085 int selector, ret;
86
Linus Walleij9dfac4f2012-02-01 18:02:47 +010087 pctldev = get_pinctrl_dev_from_devname(dev_name);
Stephen Warren57b676f2012-03-02 13:05:44 -070088 if (!pctldev) {
89 ret = -EINVAL;
Patrice Chotard42fed7b2013-04-11 11:01:27 +020090 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -070091 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +020092
93 mutex_lock(&pctldev->mutex);
94
Stephen Warren43699de2011-12-15 16:57:17 -070095 ops = pctldev->desc->confops;
96
Linus Walleijae6b4d82011-10-19 18:14:33 +020097 if (!ops || !ops->pin_config_group_get) {
Stephen Warren51cd24e2011-12-09 16:59:05 -070098 dev_err(pctldev->dev, "cannot get configuration for pin "
Linus Walleijae6b4d82011-10-19 18:14:33 +020099 "group, missing group config get function in "
100 "driver\n");
Stephen Warren57b676f2012-03-02 13:05:44 -0700101 ret = -EINVAL;
102 goto unlock;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200103 }
104
105 selector = pinctrl_get_group_selector(pctldev, pin_group);
Stephen Warren57b676f2012-03-02 13:05:44 -0700106 if (selector < 0) {
107 ret = selector;
108 goto unlock;
109 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200110
Stephen Warren57b676f2012-03-02 13:05:44 -0700111 ret = ops->pin_config_group_get(pctldev, selector, config);
112
113unlock:
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200114 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700115 return ret;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200116}
Linus Walleijae6b4d82011-10-19 18:14:33 +0200117
Stephen Warren1e2082b2012-03-02 13:05:48 -0700118int pinconf_map_to_setting(struct pinctrl_map const *map,
119 struct pinctrl_setting *setting)
120{
121 struct pinctrl_dev *pctldev = setting->pctldev;
Linus Walleij70b36372012-03-12 21:38:29 +0100122 int pin;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700123
124 switch (setting->type) {
125 case PIN_MAP_TYPE_CONFIGS_PIN:
Linus Walleij70b36372012-03-12 21:38:29 +0100126 pin = pin_get_from_name(pctldev,
127 map->data.configs.group_or_pin);
128 if (pin < 0) {
129 dev_err(pctldev->dev, "could not map pin config for \"%s\"",
130 map->data.configs.group_or_pin);
131 return pin;
132 }
133 setting->data.configs.group_or_pin = pin;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700134 break;
135 case PIN_MAP_TYPE_CONFIGS_GROUP:
Linus Walleij70b36372012-03-12 21:38:29 +0100136 pin = pinctrl_get_group_selector(pctldev,
137 map->data.configs.group_or_pin);
138 if (pin < 0) {
139 dev_err(pctldev->dev, "could not map group config for \"%s\"",
140 map->data.configs.group_or_pin);
141 return pin;
142 }
143 setting->data.configs.group_or_pin = pin;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700144 break;
145 default:
146 return -EINVAL;
147 }
148
149 setting->data.configs.num_configs = map->data.configs.num_configs;
150 setting->data.configs.configs = map->data.configs.configs;
151
152 return 0;
153}
154
155void pinconf_free_setting(struct pinctrl_setting const *setting)
156{
157}
158
159int pinconf_apply_setting(struct pinctrl_setting const *setting)
160{
161 struct pinctrl_dev *pctldev = setting->pctldev;
162 const struct pinconf_ops *ops = pctldev->desc->confops;
Mark Brownbaeae202014-04-04 17:16:57 +0100163 int ret, i;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700164
165 if (!ops) {
166 dev_err(pctldev->dev, "missing confops\n");
167 return -EINVAL;
168 }
169
170 switch (setting->type) {
171 case PIN_MAP_TYPE_CONFIGS_PIN:
Mark Brownbaeae202014-04-04 17:16:57 +0100172 if (!ops->pin_config_set && !ops->pin_config_set_bulk) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700173 dev_err(pctldev->dev, "missing pin_config_set op\n");
174 return -EINVAL;
175 }
Sherman Yin18813842014-05-14 18:30:47 -0700176 if (ops->pin_config_set_bulk) {
177 ret = ops->pin_config_set_bulk(pctldev,
Stephen Warren1e2082b2012-03-02 13:05:48 -0700178 setting->data.configs.group_or_pin,
Mark Brownbaeae202014-04-04 17:16:57 +0100179 setting->data.configs.configs,
180 setting->data.configs.num_configs);
Stephen Warren1e2082b2012-03-02 13:05:48 -0700181 if (ret < 0) {
182 dev_err(pctldev->dev,
Sherman Yin18813842014-05-14 18:30:47 -0700183 "pin_config_set_bulk op failed for pin %d\n",
Mark Brownbaeae202014-04-04 17:16:57 +0100184 setting->data.configs.group_or_pin);
Stephen Warren1e2082b2012-03-02 13:05:48 -0700185 return ret;
186 }
Mark Brownbaeae202014-04-04 17:16:57 +0100187 } else if (ops->pin_config_set) {
188 for (i = 0; i < setting->data.configs.num_configs; i++) {
189 ret = ops->pin_config_set(pctldev,
190 setting->data.configs.group_or_pin,
191 setting->data.configs.configs[i]);
192 if (ret < 0) {
193 dev_err(pctldev->dev,
194 "pin_config_set op failed for pin %d config %08lx\n",
195 setting->data.configs.group_or_pin,
196 setting->data.configs.configs[i]);
197 return ret;
198 }
199 }
Stephen Warren1e2082b2012-03-02 13:05:48 -0700200 }
201 break;
202 case PIN_MAP_TYPE_CONFIGS_GROUP:
Mark Brownbaeae202014-04-04 17:16:57 +0100203 if (!ops->pin_config_group_set &&
204 !ops->pin_config_group_set_bulk) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700205 dev_err(pctldev->dev,
206 "missing pin_config_group_set op\n");
207 return -EINVAL;
208 }
Mark Brownbaeae202014-04-04 17:16:57 +0100209 if (ops->pin_config_group_set_bulk) {
210 ret = ops->pin_config_group_set_bulk(pctldev,
Stephen Warren1e2082b2012-03-02 13:05:48 -0700211 setting->data.configs.group_or_pin,
Mark Brownbaeae202014-04-04 17:16:57 +0100212 setting->data.configs.configs,
213 setting->data.configs.num_configs);
Stephen Warren1e2082b2012-03-02 13:05:48 -0700214 if (ret < 0) {
215 dev_err(pctldev->dev,
Sherman Yin18813842014-05-14 18:30:47 -0700216 "pin_config_group_set_bulk op failed for group %d\n",
Mark Brownbaeae202014-04-04 17:16:57 +0100217 setting->data.configs.group_or_pin);
218 return ret;
219 }
220 } else if (ops->pin_config_group_set) {
221 for (i = 0; i < setting->data.configs.num_configs; i++) {
222 ret = ops->pin_config_group_set(pctldev,
Stephen Warren1e2082b2012-03-02 13:05:48 -0700223 setting->data.configs.group_or_pin,
224 setting->data.configs.configs[i]);
Mark Brownbaeae202014-04-04 17:16:57 +0100225 if (ret < 0) {
226 dev_err(pctldev->dev,
227 "pin_config_group_set op failed for group %d config %08lx\n",
228 setting->data.configs.group_or_pin,
229 setting->data.configs.configs[i]);
230 return ret;
231 }
Stephen Warren1e2082b2012-03-02 13:05:48 -0700232 }
233 }
234 break;
235 default:
236 return -EINVAL;
237 }
238
239 return 0;
240}
241
Linus Walleijae6b4d82011-10-19 18:14:33 +0200242#ifdef CONFIG_DEBUG_FS
243
Stephen Warren1e2082b2012-03-02 13:05:48 -0700244void pinconf_show_map(struct seq_file *s, struct pinctrl_map const *map)
245{
Stephen Warren6cb41582012-04-13 10:49:06 -0600246 struct pinctrl_dev *pctldev;
247 const struct pinconf_ops *confops;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700248 int i;
249
Stephen Warren6cb41582012-04-13 10:49:06 -0600250 pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
251 if (pctldev)
252 confops = pctldev->desc->confops;
253 else
254 confops = NULL;
255
Stephen Warren1e2082b2012-03-02 13:05:48 -0700256 switch (map->type) {
257 case PIN_MAP_TYPE_CONFIGS_PIN:
258 seq_printf(s, "pin ");
259 break;
260 case PIN_MAP_TYPE_CONFIGS_GROUP:
261 seq_printf(s, "group ");
262 break;
263 default:
264 break;
265 }
266
267 seq_printf(s, "%s\n", map->data.configs.group_or_pin);
268
Stephen Warren6cb41582012-04-13 10:49:06 -0600269 for (i = 0; i < map->data.configs.num_configs; i++) {
270 seq_printf(s, "config ");
271 if (confops && confops->pin_config_config_dbg_show)
272 confops->pin_config_config_dbg_show(pctldev, s,
273 map->data.configs.configs[i]);
274 else
275 seq_printf(s, "%08lx", map->data.configs.configs[i]);
276 seq_printf(s, "\n");
277 }
Stephen Warren1e2082b2012-03-02 13:05:48 -0700278}
279
280void pinconf_show_setting(struct seq_file *s,
281 struct pinctrl_setting const *setting)
282{
283 struct pinctrl_dev *pctldev = setting->pctldev;
284 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Stephen Warren6cb41582012-04-13 10:49:06 -0600285 const struct pinconf_ops *confops = pctldev->desc->confops;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700286 struct pin_desc *desc;
287 int i;
288
289 switch (setting->type) {
290 case PIN_MAP_TYPE_CONFIGS_PIN:
291 desc = pin_desc_get(setting->pctldev,
292 setting->data.configs.group_or_pin);
293 seq_printf(s, "pin %s (%d)",
294 desc->name ? desc->name : "unnamed",
295 setting->data.configs.group_or_pin);
296 break;
297 case PIN_MAP_TYPE_CONFIGS_GROUP:
298 seq_printf(s, "group %s (%d)",
299 pctlops->get_group_name(pctldev,
300 setting->data.configs.group_or_pin),
301 setting->data.configs.group_or_pin);
302 break;
303 default:
304 break;
305 }
306
307 /*
308 * FIXME: We should really get the pin controler to dump the config
309 * values, so they can be decoded to something meaningful.
310 */
Stephen Warren6cb41582012-04-13 10:49:06 -0600311 for (i = 0; i < setting->data.configs.num_configs; i++) {
312 seq_printf(s, " ");
313 if (confops && confops->pin_config_config_dbg_show)
314 confops->pin_config_config_dbg_show(pctldev, s,
315 setting->data.configs.configs[i]);
316 else
317 seq_printf(s, "%08lx",
318 setting->data.configs.configs[i]);
319 }
Stephen Warren1e2082b2012-03-02 13:05:48 -0700320
321 seq_printf(s, "\n");
322}
323
Linus Walleijae6b4d82011-10-19 18:14:33 +0200324static void pinconf_dump_pin(struct pinctrl_dev *pctldev,
325 struct seq_file *s, int pin)
326{
327 const struct pinconf_ops *ops = pctldev->desc->confops;
328
Linus Walleij394349f2011-11-24 18:27:15 +0100329 /* no-op when not using generic pin config */
330 pinconf_generic_dump_pin(pctldev, s, pin);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200331 if (ops && ops->pin_config_dbg_show)
332 ops->pin_config_dbg_show(pctldev, s, pin);
333}
334
335static int pinconf_pins_show(struct seq_file *s, void *what)
336{
337 struct pinctrl_dev *pctldev = s->private;
Dong Aishengad8bb722012-04-10 12:41:34 +0800338 const struct pinconf_ops *ops = pctldev->desc->confops;
Chanho Park706e8522012-01-03 16:47:50 +0900339 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200340
Dong Aishengad8bb722012-04-10 12:41:34 +0800341 if (!ops || !ops->pin_config_get)
342 return 0;
343
Linus Walleijae6b4d82011-10-19 18:14:33 +0200344 seq_puts(s, "Pin config settings per pin\n");
Dong Aisheng2aeefe02012-04-16 22:07:24 +0800345 seq_puts(s, "Format: pin (name): configs\n");
Linus Walleijae6b4d82011-10-19 18:14:33 +0200346
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200347 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700348
Chanho Park706e8522012-01-03 16:47:50 +0900349 /* The pin number can be retrived from the pin controller descriptor */
Stephen Warren546edd82012-01-06 13:38:31 -0700350 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200351 struct pin_desc *desc;
352
Chanho Park706e8522012-01-03 16:47:50 +0900353 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200354 desc = pin_desc_get(pctldev, pin);
Chanho Park706e8522012-01-03 16:47:50 +0900355 /* Skip if we cannot search the pin */
Linus Walleijae6b4d82011-10-19 18:14:33 +0200356 if (desc == NULL)
357 continue;
358
359 seq_printf(s, "pin %d (%s):", pin,
360 desc->name ? desc->name : "unnamed");
361
362 pinconf_dump_pin(pctldev, s, pin);
363
364 seq_printf(s, "\n");
365 }
366
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200367 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700368
Linus Walleijae6b4d82011-10-19 18:14:33 +0200369 return 0;
370}
371
372static void pinconf_dump_group(struct pinctrl_dev *pctldev,
373 struct seq_file *s, unsigned selector,
374 const char *gname)
375{
376 const struct pinconf_ops *ops = pctldev->desc->confops;
377
Linus Walleij394349f2011-11-24 18:27:15 +0100378 /* no-op when not using generic pin config */
379 pinconf_generic_dump_group(pctldev, s, gname);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200380 if (ops && ops->pin_config_group_dbg_show)
381 ops->pin_config_group_dbg_show(pctldev, s, selector);
382}
383
384static int pinconf_groups_show(struct seq_file *s, void *what)
385{
386 struct pinctrl_dev *pctldev = s->private;
387 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
388 const struct pinconf_ops *ops = pctldev->desc->confops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530389 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200390 unsigned selector = 0;
391
392 if (!ops || !ops->pin_config_group_get)
393 return 0;
394
395 seq_puts(s, "Pin config settings per pin group\n");
Dong Aisheng2aeefe02012-04-16 22:07:24 +0800396 seq_puts(s, "Format: group (name): configs\n");
Linus Walleijae6b4d82011-10-19 18:14:33 +0200397
Viresh Kumard1e90e92012-03-30 11:25:40 +0530398 while (selector < ngroups) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200399 const char *gname = pctlops->get_group_name(pctldev, selector);
400
401 seq_printf(s, "%u (%s):", selector, gname);
402 pinconf_dump_group(pctldev, s, selector, gname);
Stephen Warrenf7b90062012-02-19 23:45:58 -0700403 seq_printf(s, "\n");
404
Linus Walleijae6b4d82011-10-19 18:14:33 +0200405 selector++;
406 }
407
Linus Walleijae6b4d82011-10-19 18:14:33 +0200408 return 0;
409}
410
411static int pinconf_pins_open(struct inode *inode, struct file *file)
412{
413 return single_open(file, pinconf_pins_show, inode->i_private);
414}
415
416static int pinconf_groups_open(struct inode *inode, struct file *file)
417{
418 return single_open(file, pinconf_groups_show, inode->i_private);
419}
420
421static const struct file_operations pinconf_pins_ops = {
422 .open = pinconf_pins_open,
423 .read = seq_read,
424 .llseek = seq_lseek,
425 .release = single_release,
426};
427
428static const struct file_operations pinconf_groups_ops = {
429 .open = pinconf_groups_open,
430 .read = seq_read,
431 .llseek = seq_lseek,
432 .release = single_release,
433};
434
Laurent Meunierf07512e2013-04-18 10:48:07 +0200435#define MAX_NAME_LEN 15
436
437struct dbg_cfg {
438 enum pinctrl_map_type map_type;
439 char dev_name[MAX_NAME_LEN+1];
440 char state_name[MAX_NAME_LEN+1];
441 char pin_name[MAX_NAME_LEN+1];
442};
443
444/*
445 * Goal is to keep this structure as global in order to simply read the
446 * pinconf-config file after a write to check config is as expected
447 */
448static struct dbg_cfg pinconf_dbg_conf;
449
450/**
451 * pinconf_dbg_config_print() - display the pinctrl config from the pinctrl
452 * map, of the dev/pin/state that was last written to pinconf-config file.
453 * @s: string filled in with config description
454 * @d: not used
455 */
456static int pinconf_dbg_config_print(struct seq_file *s, void *d)
457{
458 struct pinctrl_maps *maps_node;
459 const struct pinctrl_map *map;
460 struct pinctrl_dev *pctldev = NULL;
461 const struct pinconf_ops *confops = NULL;
462 const struct pinctrl_map_configs *configs;
463 struct dbg_cfg *dbg = &pinconf_dbg_conf;
464 int i, j;
465 bool found = false;
466 unsigned long config;
467
Linus Walleija3862672013-05-27 15:53:32 +0200468 mutex_lock(&pinctrl_maps_mutex);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200469
470 /* Parse the pinctrl map and look for the elected pin/state */
471 for_each_maps(maps_node, i, map) {
472 if (map->type != dbg->map_type)
473 continue;
474 if (strcmp(map->dev_name, dbg->dev_name))
475 continue;
476 if (strcmp(map->name, dbg->state_name))
477 continue;
478
479 for (j = 0; j < map->data.configs.num_configs; j++) {
480 if (!strcmp(map->data.configs.group_or_pin,
481 dbg->pin_name)) {
482 /*
483 * We found the right pin / state, read the
484 * config and he pctldev for later use
485 */
486 configs = &map->data.configs;
487 pctldev = get_pinctrl_dev_from_devname
488 (map->ctrl_dev_name);
489 found = true;
490 break;
491 }
492 }
493 }
494
495 if (!found) {
496 seq_printf(s, "No config found for dev/state/pin, expected:\n");
497 seq_printf(s, "Searched dev:%s\n", dbg->dev_name);
498 seq_printf(s, "Searched state:%s\n", dbg->state_name);
499 seq_printf(s, "Searched pin:%s\n", dbg->pin_name);
500 seq_printf(s, "Use: modify config_pin <devname> "\
501 "<state> <pinname> <value>\n");
502 goto exit;
503 }
504
505 config = *(configs->configs);
506 seq_printf(s, "Dev %s has config of %s in state %s: 0x%08lX\n",
507 dbg->dev_name, dbg->pin_name,
508 dbg->state_name, config);
509
510 if (pctldev)
511 confops = pctldev->desc->confops;
512
513 if (confops && confops->pin_config_config_dbg_show)
514 confops->pin_config_config_dbg_show(pctldev, s, config);
515
516exit:
Linus Walleija3862672013-05-27 15:53:32 +0200517 mutex_unlock(&pinctrl_maps_mutex);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200518
519 return 0;
520}
521
522/**
523 * pinconf_dbg_config_write() - modify the pinctrl config in the pinctrl
524 * map, of a dev/pin/state entry based on user entries to pinconf-config
525 * @user_buf: contains the modification request with expected format:
526 * modify config_pin <devicename> <state> <pinname> <newvalue>
527 * modify is literal string, alternatives like add/delete not supported yet
528 * config_pin is literal, alternatives like config_mux not supported yet
529 * <devicename> <state> <pinname> are values that should match the pinctrl-maps
530 * <newvalue> reflects the new config and is driver dependant
531 */
532static int pinconf_dbg_config_write(struct file *file,
533 const char __user *user_buf, size_t count, loff_t *ppos)
534{
535 struct pinctrl_maps *maps_node;
536 const struct pinctrl_map *map;
537 struct pinctrl_dev *pctldev = NULL;
538 const struct pinconf_ops *confops = NULL;
539 struct dbg_cfg *dbg = &pinconf_dbg_conf;
540 const struct pinctrl_map_configs *configs;
541 char config[MAX_NAME_LEN+1];
542 bool found = false;
543 char buf[128];
544 char *b = &buf[0];
545 int buf_size;
546 char *token;
547 int i;
548
549 /* Get userspace string and assure termination */
550 buf_size = min(count, (sizeof(buf)-1));
551 if (copy_from_user(buf, user_buf, buf_size))
552 return -EFAULT;
553 buf[buf_size] = 0;
554
555 /*
556 * need to parse entry and extract parameters:
557 * modify configs_pin devicename state pinname newvalue
558 */
559
560 /* Get arg: 'modify' */
561 token = strsep(&b, " ");
562 if (!token)
563 return -EINVAL;
564 if (strcmp(token, "modify"))
565 return -EINVAL;
566
567 /* Get arg type: "config_pin" type supported so far */
568 token = strsep(&b, " ");
569 if (!token)
570 return -EINVAL;
571 if (strcmp(token, "config_pin"))
572 return -EINVAL;
573 dbg->map_type = PIN_MAP_TYPE_CONFIGS_PIN;
574
575 /* get arg 'device_name' */
576 token = strsep(&b, " ");
577 if (token == NULL)
578 return -EINVAL;
579 if (strlen(token) >= MAX_NAME_LEN)
580 return -EINVAL;
581 strncpy(dbg->dev_name, token, MAX_NAME_LEN);
582
583 /* get arg 'state_name' */
584 token = strsep(&b, " ");
585 if (token == NULL)
586 return -EINVAL;
587 if (strlen(token) >= MAX_NAME_LEN)
588 return -EINVAL;
589 strncpy(dbg->state_name, token, MAX_NAME_LEN);
590
591 /* get arg 'pin_name' */
592 token = strsep(&b, " ");
593 if (token == NULL)
594 return -EINVAL;
595 if (strlen(token) >= MAX_NAME_LEN)
596 return -EINVAL;
597 strncpy(dbg->pin_name, token, MAX_NAME_LEN);
598
599 /* get new_value of config' */
600 token = strsep(&b, " ");
601 if (token == NULL)
602 return -EINVAL;
603 if (strlen(token) >= MAX_NAME_LEN)
604 return -EINVAL;
605 strncpy(config, token, MAX_NAME_LEN);
606
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200607 mutex_lock(&pinctrl_maps_mutex);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200608
609 /* Parse the pinctrl map and look for the selected dev/state/pin */
610 for_each_maps(maps_node, i, map) {
611 if (strcmp(map->dev_name, dbg->dev_name))
612 continue;
613 if (map->type != dbg->map_type)
614 continue;
615 if (strcmp(map->name, dbg->state_name))
616 continue;
617
618 /* we found the right pin / state, so overwrite config */
619 if (!strcmp(map->data.configs.group_or_pin, dbg->pin_name)) {
620 found = true;
621 pctldev = get_pinctrl_dev_from_devname(
622 map->ctrl_dev_name);
623 configs = &map->data.configs;
624 break;
625 }
626 }
627
628 if (!found) {
Laurent Meunierf07512e2013-04-18 10:48:07 +0200629 count = -EINVAL;
Laurent Meuniercb6d3152013-04-24 13:05:50 +0200630 goto exit;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200631 }
632
633 if (pctldev)
634 confops = pctldev->desc->confops;
635
636 if (confops && confops->pin_config_dbg_parse_modify) {
637 for (i = 0; i < configs->num_configs; i++) {
638 confops->pin_config_dbg_parse_modify(pctldev,
639 config,
640 &configs->configs[i]);
641 }
642 }
643
644exit:
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200645 mutex_unlock(&pinctrl_maps_mutex);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200646
647 return count;
648}
649
650static int pinconf_dbg_config_open(struct inode *inode, struct file *file)
651{
652 return single_open(file, pinconf_dbg_config_print, inode->i_private);
653}
654
655static const struct file_operations pinconf_dbg_pinconfig_fops = {
656 .open = pinconf_dbg_config_open,
657 .write = pinconf_dbg_config_write,
658 .read = seq_read,
659 .llseek = seq_lseek,
660 .release = single_release,
661 .owner = THIS_MODULE,
662};
663
Linus Walleijae6b4d82011-10-19 18:14:33 +0200664void pinconf_init_device_debugfs(struct dentry *devroot,
665 struct pinctrl_dev *pctldev)
666{
667 debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO,
668 devroot, pctldev, &pinconf_pins_ops);
669 debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO,
670 devroot, pctldev, &pinconf_groups_ops);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200671 debugfs_create_file("pinconf-config", (S_IRUGO | S_IWUSR | S_IWGRP),
672 devroot, pctldev, &pinconf_dbg_pinconfig_fops);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200673}
674
675#endif