]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/pinctrl/pinconf.c
Merge tag 'for-linus-4.3-rc0-tag' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-bionic-kernel.git] / drivers / pinctrl / pinconf.c
CommitLineData
ae6b4d85
LW
1/*
2 * Core driver for the pin config portions of the pin control subsystem
3 *
4 * Copyright (C) 2011 ST-Ericsson SA
5 * Written on behalf of Linaro for ST-Ericsson
6 *
7 * Author: Linus Walleij <linus.walleij@linaro.org>
8 *
9 * License terms: GNU General Public License (GPL) version 2
10 */
11#define pr_fmt(fmt) "pinconfig core: " fmt
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/device.h>
17#include <linux/slab.h>
18#include <linux/debugfs.h>
19#include <linux/seq_file.h>
f07512e6 20#include <linux/uaccess.h>
ae6b4d85
LW
21#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
2b694250
SW
27int pinconf_check_ops(struct pinctrl_dev *pctldev)
28{
29 const struct pinconf_ops *ops = pctldev->desc->confops;
30
2b694250 31 /* We have to be able to config the pins in SOME way */
ad6e1107
JC
32 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");
2b694250 35 return -EINVAL;
ad6e1107 36 }
2b694250
SW
37 return 0;
38}
39
1e2082b5
SW
40int 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
c95df2db 48 if (!map->data.configs.num_configs ||
1e2082b5 49 !map->data.configs.configs) {
c95df2db 50 pr_err("failed to register map %s (%d): no configs given\n",
1e2082b5
SW
51 map->name, i);
52 return -EINVAL;
53 }
54
55 return 0;
56}
57
394349f7 58int pin_config_get_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
ae6b4d85
LW
59 unsigned long *config)
60{
61 const struct pinconf_ops *ops = pctldev->desc->confops;
62
63 if (!ops || !ops->pin_config_get) {
ca67f10f
MY
64 dev_dbg(pctldev->dev,
65 "cannot get pin configuration, .pin_config_get missing in driver\n");
c420619d 66 return -ENOTSUPP;
ae6b4d85
LW
67 }
68
69 return ops->pin_config_get(pctldev, pin, config);
70}
71
43699dea 72int pin_config_group_get(const char *dev_name, const char *pin_group,
ae6b4d85
LW
73 unsigned long *config)
74{
43699dea
SW
75 struct pinctrl_dev *pctldev;
76 const struct pinconf_ops *ops;
57b676f9
SW
77 int selector, ret;
78
9dfac4fd 79 pctldev = get_pinctrl_dev_from_devname(dev_name);
57b676f9
SW
80 if (!pctldev) {
81 ret = -EINVAL;
42fed7ba 82 return ret;
57b676f9 83 }
42fed7ba
PC
84
85 mutex_lock(&pctldev->mutex);
86
43699dea
SW
87 ops = pctldev->desc->confops;
88
ae6b4d85 89 if (!ops || !ops->pin_config_group_get) {
c420619d 90 dev_dbg(pctldev->dev, "cannot get configuration for pin "
ae6b4d85
LW
91 "group, missing group config get function in "
92 "driver\n");
c420619d 93 ret = -ENOTSUPP;
57b676f9 94 goto unlock;
ae6b4d85
LW
95 }
96
97 selector = pinctrl_get_group_selector(pctldev, pin_group);
57b676f9
SW
98 if (selector < 0) {
99 ret = selector;
100 goto unlock;
101 }
ae6b4d85 102
57b676f9
SW
103 ret = ops->pin_config_group_get(pctldev, selector, config);
104
105unlock:
42fed7ba 106 mutex_unlock(&pctldev->mutex);
57b676f9 107 return ret;
ae6b4d85 108}
ae6b4d85 109
1e2082b5
SW
110int pinconf_map_to_setting(struct pinctrl_map const *map,
111 struct pinctrl_setting *setting)
112{
113 struct pinctrl_dev *pctldev = setting->pctldev;
70b36378 114 int pin;
1e2082b5
SW
115
116 switch (setting->type) {
117 case PIN_MAP_TYPE_CONFIGS_PIN:
70b36378
LW
118 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;
1e2082b5
SW
126 break;
127 case PIN_MAP_TYPE_CONFIGS_GROUP:
70b36378
LW
128 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;
1e2082b5
SW
136 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;
03b054e9 155 int ret;
1e2082b5
SW
156
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 }
03b054e9
SY
168 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;
1e2082b5
SW
177 }
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 }
03b054e9
SY
185 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;
1e2082b5
SW
194 }
195 break;
196 default:
197 return -EINVAL;
198 }
199
200 return 0;
201}
202
ae6b4d85
LW
203#ifdef CONFIG_DEBUG_FS
204
6de52c15 205static void pinconf_show_config(struct seq_file *s, struct pinctrl_dev *pctldev,
d96310ae 206 unsigned long *configs, unsigned num_configs)
1e2082b5 207{
6cb41587 208 const struct pinconf_ops *confops;
1e2082b5
SW
209 int i;
210
6cb41587
SW
211 if (pctldev)
212 confops = pctldev->desc->confops;
213 else
214 confops = NULL;
215
d96310ae
JH
216 for (i = 0; i < num_configs; i++) {
217 seq_puts(s, "config ");
218 if (confops && confops->pin_config_config_dbg_show)
219 confops->pin_config_config_dbg_show(pctldev, s,
220 configs[i]);
221 else
222 seq_printf(s, "%08lx", configs[i]);
223 seq_puts(s, "\n");
224 }
225}
226
227void pinconf_show_map(struct seq_file *s, struct pinctrl_map const *map)
228{
229 struct pinctrl_dev *pctldev;
230
231 pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
232
1e2082b5
SW
233 switch (map->type) {
234 case PIN_MAP_TYPE_CONFIGS_PIN:
235 seq_printf(s, "pin ");
236 break;
237 case PIN_MAP_TYPE_CONFIGS_GROUP:
238 seq_printf(s, "group ");
239 break;
240 default:
241 break;
242 }
243
244 seq_printf(s, "%s\n", map->data.configs.group_or_pin);
245
d96310ae
JH
246 pinconf_show_config(s, pctldev, map->data.configs.configs,
247 map->data.configs.num_configs);
1e2082b5
SW
248}
249
250void pinconf_show_setting(struct seq_file *s,
251 struct pinctrl_setting const *setting)
252{
253 struct pinctrl_dev *pctldev = setting->pctldev;
254 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
255 struct pin_desc *desc;
1e2082b5
SW
256
257 switch (setting->type) {
258 case PIN_MAP_TYPE_CONFIGS_PIN:
259 desc = pin_desc_get(setting->pctldev,
260 setting->data.configs.group_or_pin);
261 seq_printf(s, "pin %s (%d)",
262 desc->name ? desc->name : "unnamed",
263 setting->data.configs.group_or_pin);
264 break;
265 case PIN_MAP_TYPE_CONFIGS_GROUP:
266 seq_printf(s, "group %s (%d)",
267 pctlops->get_group_name(pctldev,
268 setting->data.configs.group_or_pin),
269 setting->data.configs.group_or_pin);
270 break;
271 default:
272 break;
273 }
274
275 /*
276 * FIXME: We should really get the pin controler to dump the config
277 * values, so they can be decoded to something meaningful.
278 */
d96310ae
JH
279 pinconf_show_config(s, pctldev, setting->data.configs.configs,
280 setting->data.configs.num_configs);
1e2082b5
SW
281}
282
ae6b4d85
LW
283static void pinconf_dump_pin(struct pinctrl_dev *pctldev,
284 struct seq_file *s, int pin)
285{
286 const struct pinconf_ops *ops = pctldev->desc->confops;
287
394349f7 288 /* no-op when not using generic pin config */
dd4d01f7 289 pinconf_generic_dump_pins(pctldev, s, NULL, pin);
ae6b4d85
LW
290 if (ops && ops->pin_config_dbg_show)
291 ops->pin_config_dbg_show(pctldev, s, pin);
292}
293
294static int pinconf_pins_show(struct seq_file *s, void *what)
295{
296 struct pinctrl_dev *pctldev = s->private;
706e8520 297 unsigned i, pin;
ae6b4d85
LW
298
299 seq_puts(s, "Pin config settings per pin\n");
2aeefe02 300 seq_puts(s, "Format: pin (name): configs\n");
ae6b4d85 301
42fed7ba 302 mutex_lock(&pctldev->mutex);
57b676f9 303
706e8520 304 /* The pin number can be retrived from the pin controller descriptor */
546edd83 305 for (i = 0; i < pctldev->desc->npins; i++) {
ae6b4d85
LW
306 struct pin_desc *desc;
307
706e8520 308 pin = pctldev->desc->pins[i].number;
ae6b4d85 309 desc = pin_desc_get(pctldev, pin);
706e8520 310 /* Skip if we cannot search the pin */
ae6b4d85
LW
311 if (desc == NULL)
312 continue;
313
314 seq_printf(s, "pin %d (%s):", pin,
315 desc->name ? desc->name : "unnamed");
316
317 pinconf_dump_pin(pctldev, s, pin);
318
319 seq_printf(s, "\n");
320 }
321
42fed7ba 322 mutex_unlock(&pctldev->mutex);
57b676f9 323
ae6b4d85
LW
324 return 0;
325}
326
327static void pinconf_dump_group(struct pinctrl_dev *pctldev,
328 struct seq_file *s, unsigned selector,
329 const char *gname)
330{
331 const struct pinconf_ops *ops = pctldev->desc->confops;
332
394349f7 333 /* no-op when not using generic pin config */
dd4d01f7 334 pinconf_generic_dump_pins(pctldev, s, gname, 0);
ae6b4d85
LW
335 if (ops && ops->pin_config_group_dbg_show)
336 ops->pin_config_group_dbg_show(pctldev, s, selector);
337}
338
339static int pinconf_groups_show(struct seq_file *s, void *what)
340{
341 struct pinctrl_dev *pctldev = s->private;
342 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
d1e90e9e 343 unsigned ngroups = pctlops->get_groups_count(pctldev);
ae6b4d85
LW
344 unsigned selector = 0;
345
ae6b4d85 346 seq_puts(s, "Pin config settings per pin group\n");
2aeefe02 347 seq_puts(s, "Format: group (name): configs\n");
ae6b4d85 348
d1e90e9e 349 while (selector < ngroups) {
ae6b4d85
LW
350 const char *gname = pctlops->get_group_name(pctldev, selector);
351
352 seq_printf(s, "%u (%s):", selector, gname);
353 pinconf_dump_group(pctldev, s, selector, gname);
f7b9006f
SW
354 seq_printf(s, "\n");
355
ae6b4d85
LW
356 selector++;
357 }
358
359 return 0;
360}
361
362static int pinconf_pins_open(struct inode *inode, struct file *file)
363{
364 return single_open(file, pinconf_pins_show, inode->i_private);
365}
366
367static int pinconf_groups_open(struct inode *inode, struct file *file)
368{
369 return single_open(file, pinconf_groups_show, inode->i_private);
370}
371
372static const struct file_operations pinconf_pins_ops = {
373 .open = pinconf_pins_open,
374 .read = seq_read,
375 .llseek = seq_lseek,
376 .release = single_release,
377};
378
379static const struct file_operations pinconf_groups_ops = {
380 .open = pinconf_groups_open,
381 .read = seq_read,
382 .llseek = seq_lseek,
383 .release = single_release,
384};
385
f07512e6
LM
386#define MAX_NAME_LEN 15
387
388struct dbg_cfg {
389 enum pinctrl_map_type map_type;
390 char dev_name[MAX_NAME_LEN+1];
391 char state_name[MAX_NAME_LEN+1];
392 char pin_name[MAX_NAME_LEN+1];
393};
394
395/*
396 * Goal is to keep this structure as global in order to simply read the
397 * pinconf-config file after a write to check config is as expected
398 */
399static struct dbg_cfg pinconf_dbg_conf;
400
401/**
402 * pinconf_dbg_config_print() - display the pinctrl config from the pinctrl
403 * map, of the dev/pin/state that was last written to pinconf-config file.
404 * @s: string filled in with config description
405 * @d: not used
406 */
407static int pinconf_dbg_config_print(struct seq_file *s, void *d)
408{
409 struct pinctrl_maps *maps_node;
410 const struct pinctrl_map *map;
8a9dcc3f
RKAL
411 const struct pinctrl_map *found = NULL;
412 struct pinctrl_dev *pctldev;
f07512e6
LM
413 struct dbg_cfg *dbg = &pinconf_dbg_conf;
414 int i, j;
f07512e6 415
a386267a 416 mutex_lock(&pinctrl_maps_mutex);
f07512e6
LM
417
418 /* Parse the pinctrl map and look for the elected pin/state */
419 for_each_maps(maps_node, i, map) {
420 if (map->type != dbg->map_type)
421 continue;
422 if (strcmp(map->dev_name, dbg->dev_name))
423 continue;
424 if (strcmp(map->name, dbg->state_name))
425 continue;
426
427 for (j = 0; j < map->data.configs.num_configs; j++) {
428 if (!strcmp(map->data.configs.group_or_pin,
429 dbg->pin_name)) {
8a9dcc3f
RKAL
430 /* We found the right pin / state */
431 found = map;
f07512e6
LM
432 break;
433 }
434 }
435 }
436
437 if (!found) {
438 seq_printf(s, "No config found for dev/state/pin, expected:\n");
439 seq_printf(s, "Searched dev:%s\n", dbg->dev_name);
440 seq_printf(s, "Searched state:%s\n", dbg->state_name);
441 seq_printf(s, "Searched pin:%s\n", dbg->pin_name);
442 seq_printf(s, "Use: modify config_pin <devname> "\
443 "<state> <pinname> <value>\n");
444 goto exit;
445 }
446
8a9dcc3f 447 pctldev = get_pinctrl_dev_from_devname(found->ctrl_dev_name);
d96310ae
JH
448 seq_printf(s, "Dev %s has config of %s in state %s:\n",
449 dbg->dev_name, dbg->pin_name, dbg->state_name);
450 pinconf_show_config(s, pctldev, found->data.configs.configs,
451 found->data.configs.num_configs);
f07512e6
LM
452
453exit:
a386267a 454 mutex_unlock(&pinctrl_maps_mutex);
f07512e6
LM
455
456 return 0;
457}
458
459/**
460 * pinconf_dbg_config_write() - modify the pinctrl config in the pinctrl
461 * map, of a dev/pin/state entry based on user entries to pinconf-config
462 * @user_buf: contains the modification request with expected format:
75629981 463 * modify <config> <devicename> <state> <name> <newvalue>
f07512e6 464 * modify is literal string, alternatives like add/delete not supported yet
75629981
JH
465 * <config> is the configuration to be changed. Supported configs are
466 * "config_pin" or "config_group", alternatives like config_mux are not
467 * supported yet.
468 * <devicename> <state> <name> are values that should match the pinctrl-maps
f07512e6
LM
469 * <newvalue> reflects the new config and is driver dependant
470 */
3b59e432 471static ssize_t pinconf_dbg_config_write(struct file *file,
f07512e6
LM
472 const char __user *user_buf, size_t count, loff_t *ppos)
473{
474 struct pinctrl_maps *maps_node;
475 const struct pinctrl_map *map;
8a9dcc3f
RKAL
476 const struct pinctrl_map *found = NULL;
477 struct pinctrl_dev *pctldev;
f07512e6
LM
478 const struct pinconf_ops *confops = NULL;
479 struct dbg_cfg *dbg = &pinconf_dbg_conf;
480 const struct pinctrl_map_configs *configs;
481 char config[MAX_NAME_LEN+1];
f07512e6
LM
482 char buf[128];
483 char *b = &buf[0];
484 int buf_size;
485 char *token;
486 int i;
487
488 /* Get userspace string and assure termination */
81d36c4f 489 buf_size = min(count, sizeof(buf) - 1);
f07512e6
LM
490 if (copy_from_user(buf, user_buf, buf_size))
491 return -EFAULT;
492 buf[buf_size] = 0;
493
494 /*
495 * need to parse entry and extract parameters:
496 * modify configs_pin devicename state pinname newvalue
497 */
498
499 /* Get arg: 'modify' */
500 token = strsep(&b, " ");
501 if (!token)
502 return -EINVAL;
503 if (strcmp(token, "modify"))
504 return -EINVAL;
505
75629981
JH
506 /*
507 * Get arg type: "config_pin" and "config_group"
508 * types are supported so far
509 */
f07512e6
LM
510 token = strsep(&b, " ");
511 if (!token)
512 return -EINVAL;
75629981
JH
513 if (!strcmp(token, "config_pin"))
514 dbg->map_type = PIN_MAP_TYPE_CONFIGS_PIN;
515 else if (!strcmp(token, "config_group"))
516 dbg->map_type = PIN_MAP_TYPE_CONFIGS_GROUP;
517 else
f07512e6 518 return -EINVAL;
f07512e6
LM
519
520 /* get arg 'device_name' */
521 token = strsep(&b, " ");
522 if (token == NULL)
523 return -EINVAL;
524 if (strlen(token) >= MAX_NAME_LEN)
525 return -EINVAL;
526 strncpy(dbg->dev_name, token, MAX_NAME_LEN);
527
528 /* get arg 'state_name' */
529 token = strsep(&b, " ");
530 if (token == NULL)
531 return -EINVAL;
532 if (strlen(token) >= MAX_NAME_LEN)
533 return -EINVAL;
534 strncpy(dbg->state_name, token, MAX_NAME_LEN);
535
536 /* get arg 'pin_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->pin_name, token, MAX_NAME_LEN);
543
544 /* get new_value of config' */
545 token = strsep(&b, " ");
546 if (token == NULL)
547 return -EINVAL;
548 if (strlen(token) >= MAX_NAME_LEN)
549 return -EINVAL;
550 strncpy(config, token, MAX_NAME_LEN);
551
42fed7ba 552 mutex_lock(&pinctrl_maps_mutex);
f07512e6
LM
553
554 /* Parse the pinctrl map and look for the selected dev/state/pin */
555 for_each_maps(maps_node, i, map) {
556 if (strcmp(map->dev_name, dbg->dev_name))
557 continue;
558 if (map->type != dbg->map_type)
559 continue;
560 if (strcmp(map->name, dbg->state_name))
561 continue;
562
563 /* we found the right pin / state, so overwrite config */
564 if (!strcmp(map->data.configs.group_or_pin, dbg->pin_name)) {
8a9dcc3f 565 found = map;
f07512e6
LM
566 break;
567 }
568 }
569
570 if (!found) {
f07512e6 571 count = -EINVAL;
cb6d315d 572 goto exit;
f07512e6
LM
573 }
574
8a9dcc3f 575 pctldev = get_pinctrl_dev_from_devname(found->ctrl_dev_name);
f07512e6
LM
576 if (pctldev)
577 confops = pctldev->desc->confops;
578
579 if (confops && confops->pin_config_dbg_parse_modify) {
8a9dcc3f 580 configs = &found->data.configs;
f07512e6
LM
581 for (i = 0; i < configs->num_configs; i++) {
582 confops->pin_config_dbg_parse_modify(pctldev,
583 config,
584 &configs->configs[i]);
585 }
586 }
587
588exit:
42fed7ba 589 mutex_unlock(&pinctrl_maps_mutex);
f07512e6
LM
590
591 return count;
592}
593
594static int pinconf_dbg_config_open(struct inode *inode, struct file *file)
595{
596 return single_open(file, pinconf_dbg_config_print, inode->i_private);
597}
598
599static const struct file_operations pinconf_dbg_pinconfig_fops = {
600 .open = pinconf_dbg_config_open,
601 .write = pinconf_dbg_config_write,
602 .read = seq_read,
603 .llseek = seq_lseek,
604 .release = single_release,
605 .owner = THIS_MODULE,
606};
607
ae6b4d85
LW
608void pinconf_init_device_debugfs(struct dentry *devroot,
609 struct pinctrl_dev *pctldev)
610{
611 debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO,
612 devroot, pctldev, &pinconf_pins_ops);
613 debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO,
614 devroot, pctldev, &pinconf_groups_ops);
f07512e6
LM
615 debugfs_create_file("pinconf-config", (S_IRUGO | S_IWUSR | S_IWGRP),
616 devroot, pctldev, &pinconf_dbg_pinconfig_fops);
ae6b4d85
LW
617}
618
619#endif