]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/pinctrl/pinconf.c
pinctrl: tegra114: Remove MODULE_ALIAS
[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
31 /* We must be able to read out pin status */
ad6e1107
JC
32 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");
2b694250 35 return -EINVAL;
ad6e1107 36 }
2b694250 37 /* We have to be able to config the pins in SOME way */
ad6e1107
JC
38 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");
2b694250 41 return -EINVAL;
ad6e1107 42 }
2b694250
SW
43 return 0;
44}
45
1e2082b5
SW
46int 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
c95df2db 54 if (!map->data.configs.num_configs ||
1e2082b5 55 !map->data.configs.configs) {
c95df2db 56 pr_err("failed to register map %s (%d): no configs given\n",
1e2082b5
SW
57 map->name, i);
58 return -EINVAL;
59 }
60
61 return 0;
62}
63
394349f7 64int pin_config_get_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
ae6b4d85
LW
65 unsigned long *config)
66{
67 const struct pinconf_ops *ops = pctldev->desc->confops;
68
69 if (!ops || !ops->pin_config_get) {
51cd24ee 70 dev_err(pctldev->dev, "cannot get pin configuration, missing "
ae6b4d85
LW
71 "pin_config_get() function in driver\n");
72 return -EINVAL;
73 }
74
75 return ops->pin_config_get(pctldev, pin, config);
76}
77
43699dea 78int pin_config_group_get(const char *dev_name, const char *pin_group,
ae6b4d85
LW
79 unsigned long *config)
80{
43699dea
SW
81 struct pinctrl_dev *pctldev;
82 const struct pinconf_ops *ops;
57b676f9
SW
83 int selector, ret;
84
9dfac4fd 85 pctldev = get_pinctrl_dev_from_devname(dev_name);
57b676f9
SW
86 if (!pctldev) {
87 ret = -EINVAL;
42fed7ba 88 return ret;
57b676f9 89 }
42fed7ba
PC
90
91 mutex_lock(&pctldev->mutex);
92
43699dea
SW
93 ops = pctldev->desc->confops;
94
ae6b4d85 95 if (!ops || !ops->pin_config_group_get) {
51cd24ee 96 dev_err(pctldev->dev, "cannot get configuration for pin "
ae6b4d85
LW
97 "group, missing group config get function in "
98 "driver\n");
57b676f9
SW
99 ret = -EINVAL;
100 goto unlock;
ae6b4d85
LW
101 }
102
103 selector = pinctrl_get_group_selector(pctldev, pin_group);
57b676f9
SW
104 if (selector < 0) {
105 ret = selector;
106 goto unlock;
107 }
ae6b4d85 108
57b676f9
SW
109 ret = ops->pin_config_group_get(pctldev, selector, config);
110
111unlock:
42fed7ba 112 mutex_unlock(&pctldev->mutex);
57b676f9 113 return ret;
ae6b4d85 114}
ae6b4d85 115
1e2082b5
SW
116int pinconf_map_to_setting(struct pinctrl_map const *map,
117 struct pinctrl_setting *setting)
118{
119 struct pinctrl_dev *pctldev = setting->pctldev;
70b36378 120 int pin;
1e2082b5
SW
121
122 switch (setting->type) {
123 case PIN_MAP_TYPE_CONFIGS_PIN:
70b36378
LW
124 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;
1e2082b5
SW
132 break;
133 case PIN_MAP_TYPE_CONFIGS_GROUP:
70b36378
LW
134 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;
1e2082b5
SW
142 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;
03b054e9 161 int ret;
1e2082b5
SW
162
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 }
03b054e9
SY
174 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;
1e2082b5
SW
183 }
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 }
03b054e9
SY
191 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;
1e2082b5
SW
200 }
201 break;
202 default:
203 return -EINVAL;
204 }
205
206 return 0;
207}
208
ae6b4d85
LW
209#ifdef CONFIG_DEBUG_FS
210
1e2082b5
SW
211void pinconf_show_map(struct seq_file *s, struct pinctrl_map const *map)
212{
6cb41587
SW
213 struct pinctrl_dev *pctldev;
214 const struct pinconf_ops *confops;
1e2082b5
SW
215 int i;
216
6cb41587
SW
217 pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
218 if (pctldev)
219 confops = pctldev->desc->confops;
220 else
221 confops = NULL;
222
1e2082b5
SW
223 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
6cb41587
SW
236 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 }
1e2082b5
SW
245}
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;
6cb41587 252 const struct pinconf_ops *confops = pctldev->desc->confops;
1e2082b5
SW
253 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 */
6cb41587
SW
278 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 }
1e2082b5
SW
287
288 seq_printf(s, "\n");
289}
290
ae6b4d85
LW
291static 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
394349f7
LW
296 /* no-op when not using generic pin config */
297 pinconf_generic_dump_pin(pctldev, s, pin);
ae6b4d85
LW
298 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;
ad8bb720 305 const struct pinconf_ops *ops = pctldev->desc->confops;
706e8520 306 unsigned i, pin;
ae6b4d85 307
ad8bb720
DA
308 if (!ops || !ops->pin_config_get)
309 return 0;
310
ae6b4d85 311 seq_puts(s, "Pin config settings per pin\n");
2aeefe02 312 seq_puts(s, "Format: pin (name): configs\n");
ae6b4d85 313
42fed7ba 314 mutex_lock(&pctldev->mutex);
57b676f9 315
706e8520 316 /* The pin number can be retrived from the pin controller descriptor */
546edd83 317 for (i = 0; i < pctldev->desc->npins; i++) {
ae6b4d85
LW
318 struct pin_desc *desc;
319
706e8520 320 pin = pctldev->desc->pins[i].number;
ae6b4d85 321 desc = pin_desc_get(pctldev, pin);
706e8520 322 /* Skip if we cannot search the pin */
ae6b4d85
LW
323 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
42fed7ba 334 mutex_unlock(&pctldev->mutex);
57b676f9 335
ae6b4d85
LW
336 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
394349f7
LW
345 /* no-op when not using generic pin config */
346 pinconf_generic_dump_group(pctldev, s, gname);
ae6b4d85
LW
347 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;
d1e90e9e 356 unsigned ngroups = pctlops->get_groups_count(pctldev);
ae6b4d85
LW
357 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");
2aeefe02 363 seq_puts(s, "Format: group (name): configs\n");
ae6b4d85 364
d1e90e9e 365 while (selector < ngroups) {
ae6b4d85
LW
366 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);
f7b9006f
SW
370 seq_printf(s, "\n");
371
ae6b4d85
LW
372 selector++;
373 }
374
375 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
f07512e6
LM
402#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;
8a9dcc3f
RKAL
427 const struct pinctrl_map *found = NULL;
428 struct pinctrl_dev *pctldev;
f07512e6 429 const struct pinconf_ops *confops = NULL;
f07512e6
LM
430 struct dbg_cfg *dbg = &pinconf_dbg_conf;
431 int i, j;
f07512e6
LM
432 unsigned long config;
433
a386267a 434 mutex_lock(&pinctrl_maps_mutex);
f07512e6
LM
435
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)) {
8a9dcc3f
RKAL
448 /* We found the right pin / state */
449 found = map;
f07512e6
LM
450 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
8a9dcc3f
RKAL
465 pctldev = get_pinctrl_dev_from_devname(found->ctrl_dev_name);
466 config = *found->data.configs.configs;
f07512e6
LM
467 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:
a386267a 478 mutex_unlock(&pinctrl_maps_mutex);
f07512e6
LM
479
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 */
493static int pinconf_dbg_config_write(struct file *file,
494 const char __user *user_buf, size_t count, loff_t *ppos)
495{
496 struct pinctrl_maps *maps_node;
497 const struct pinctrl_map *map;
8a9dcc3f
RKAL
498 const struct pinctrl_map *found = NULL;
499 struct pinctrl_dev *pctldev;
f07512e6
LM
500 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];
f07512e6
LM
504 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 */
9b680354 511 buf_size = min(count, (size_t)(sizeof(buf)-1));
f07512e6
LM
512 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
42fed7ba 568 mutex_lock(&pinctrl_maps_mutex);
f07512e6
LM
569
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)) {
8a9dcc3f 581 found = map;
f07512e6
LM
582 break;
583 }
584 }
585
586 if (!found) {
f07512e6 587 count = -EINVAL;
cb6d315d 588 goto exit;
f07512e6
LM
589 }
590
8a9dcc3f 591 pctldev = get_pinctrl_dev_from_devname(found->ctrl_dev_name);
f07512e6
LM
592 if (pctldev)
593 confops = pctldev->desc->confops;
594
595 if (confops && confops->pin_config_dbg_parse_modify) {
8a9dcc3f 596 configs = &found->data.configs;
f07512e6
LM
597 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:
42fed7ba 605 mutex_unlock(&pinctrl_maps_mutex);
f07512e6
LM
606
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
ae6b4d85
LW
624void 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);
f07512e6
LM
631 debugfs_create_file("pinconf-config", (S_IRUGO | S_IWUSR | S_IWGRP),
632 devroot, pctldev, &pinconf_dbg_pinconfig_fops);
ae6b4d85
LW
633}
634
635#endif