dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / pinctrl / pinconf.c
blob4dd7722f993552a4c8cd698cdc2f8d224dc337c4
1 /*
2 * Core driver for the pin config portions of the pin control subsystem
4 * Copyright (C) 2011 ST-Ericsson SA
5 * Written on behalf of Linaro for ST-Ericsson
7 * Author: Linus Walleij <linus.walleij@linaro.org>
9 * License terms: GNU General Public License (GPL) version 2
11 #define pr_fmt(fmt) "pinconfig core: " fmt
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>
20 #include <linux/uaccess.h>
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"
27 int pinconf_check_ops(struct pinctrl_dev *pctldev)
29 const struct pinconf_ops *ops = pctldev->desc->confops;
31 /* We have to be able to config the pins in SOME way */
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");
35 return -EINVAL;
37 return 0;
40 int pinconf_validate_map(struct pinctrl_map const *map, int i)
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;
48 if (!map->data.configs.num_configs ||
49 !map->data.configs.configs) {
50 pr_err("failed to register map %s (%d): no configs given\n",
51 map->name, i);
52 return -EINVAL;
55 return 0;
58 int pin_config_get_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
59 unsigned long *config)
61 const struct pinconf_ops *ops = pctldev->desc->confops;
63 if (!ops || !ops->pin_config_get) {
64 dev_dbg(pctldev->dev,
65 "cannot get pin configuration, .pin_config_get missing in driver\n");
66 return -ENOTSUPP;
69 return ops->pin_config_get(pctldev, pin, config);
72 int pin_config_group_get(const char *dev_name, const char *pin_group,
73 unsigned long *config)
75 struct pinctrl_dev *pctldev;
76 const struct pinconf_ops *ops;
77 int selector, ret;
79 pctldev = get_pinctrl_dev_from_devname(dev_name);
80 if (!pctldev) {
81 ret = -EINVAL;
82 return ret;
85 mutex_lock(&pctldev->mutex);
87 ops = pctldev->desc->confops;
89 if (!ops || !ops->pin_config_group_get) {
90 dev_dbg(pctldev->dev, "cannot get configuration for pin "
91 "group, missing group config get function in "
92 "driver\n");
93 ret = -ENOTSUPP;
94 goto unlock;
97 selector = pinctrl_get_group_selector(pctldev, pin_group);
98 if (selector < 0) {
99 ret = selector;
100 goto unlock;
103 ret = ops->pin_config_group_get(pctldev, selector, config);
105 unlock:
106 mutex_unlock(&pctldev->mutex);
107 return ret;
110 int pinconf_map_to_setting(struct pinctrl_map const *map,
111 struct pinctrl_setting *setting)
113 struct pinctrl_dev *pctldev = setting->pctldev;
114 int pin;
116 switch (setting->type) {
117 case PIN_MAP_TYPE_CONFIGS_PIN:
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;
125 setting->data.configs.group_or_pin = pin;
126 break;
127 case PIN_MAP_TYPE_CONFIGS_GROUP:
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;
135 setting->data.configs.group_or_pin = pin;
136 break;
137 default:
138 return -EINVAL;
141 setting->data.configs.num_configs = map->data.configs.num_configs;
142 setting->data.configs.configs = map->data.configs.configs;
144 return 0;
147 void pinconf_free_setting(struct pinctrl_setting const *setting)
151 int pinconf_apply_setting(struct pinctrl_setting const *setting)
153 struct pinctrl_dev *pctldev = setting->pctldev;
154 const struct pinconf_ops *ops = pctldev->desc->confops;
155 int ret;
157 if (!ops) {
158 dev_err(pctldev->dev, "missing confops\n");
159 return -EINVAL;
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;
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;
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;
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;
195 break;
196 default:
197 return -EINVAL;
200 return 0;
203 #ifdef CONFIG_DEBUG_FS
205 static void pinconf_show_config(struct seq_file *s, struct pinctrl_dev *pctldev,
206 unsigned long *configs, unsigned num_configs)
208 const struct pinconf_ops *confops;
209 int i;
211 if (pctldev)
212 confops = pctldev->desc->confops;
213 else
214 confops = NULL;
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");
227 void pinconf_show_map(struct seq_file *s, struct pinctrl_map const *map)
229 struct pinctrl_dev *pctldev;
231 pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
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;
244 seq_printf(s, "%s\n", map->data.configs.group_or_pin);
246 pinconf_show_config(s, pctldev, map->data.configs.configs,
247 map->data.configs.num_configs);
250 void pinconf_show_setting(struct seq_file *s,
251 struct pinctrl_setting const *setting)
253 struct pinctrl_dev *pctldev = setting->pctldev;
254 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
255 struct pin_desc *desc;
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;
276 * FIXME: We should really get the pin controler to dump the config
277 * values, so they can be decoded to something meaningful.
279 pinconf_show_config(s, pctldev, setting->data.configs.configs,
280 setting->data.configs.num_configs);
283 static void pinconf_dump_pin(struct pinctrl_dev *pctldev,
284 struct seq_file *s, int pin)
286 const struct pinconf_ops *ops = pctldev->desc->confops;
288 /* no-op when not using generic pin config */
289 pinconf_generic_dump_pins(pctldev, s, NULL, pin);
290 if (ops && ops->pin_config_dbg_show)
291 ops->pin_config_dbg_show(pctldev, s, pin);
294 static int pinconf_pins_show(struct seq_file *s, void *what)
296 struct pinctrl_dev *pctldev = s->private;
297 unsigned i, pin;
299 seq_puts(s, "Pin config settings per pin\n");
300 seq_puts(s, "Format: pin (name): configs\n");
302 mutex_lock(&pctldev->mutex);
304 /* The pin number can be retrived from the pin controller descriptor */
305 for (i = 0; i < pctldev->desc->npins; i++) {
306 struct pin_desc *desc;
308 pin = pctldev->desc->pins[i].number;
309 desc = pin_desc_get(pctldev, pin);
310 /* Skip if we cannot search the pin */
311 if (desc == NULL)
312 continue;
314 seq_printf(s, "pin %d (%s):", pin,
315 desc->name ? desc->name : "unnamed");
317 pinconf_dump_pin(pctldev, s, pin);
319 seq_printf(s, "\n");
322 mutex_unlock(&pctldev->mutex);
324 return 0;
327 static void pinconf_dump_group(struct pinctrl_dev *pctldev,
328 struct seq_file *s, unsigned selector,
329 const char *gname)
331 const struct pinconf_ops *ops = pctldev->desc->confops;
333 /* no-op when not using generic pin config */
334 pinconf_generic_dump_pins(pctldev, s, gname, 0);
335 if (ops && ops->pin_config_group_dbg_show)
336 ops->pin_config_group_dbg_show(pctldev, s, selector);
339 static int pinconf_groups_show(struct seq_file *s, void *what)
341 struct pinctrl_dev *pctldev = s->private;
342 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
343 unsigned ngroups = pctlops->get_groups_count(pctldev);
344 unsigned selector = 0;
346 seq_puts(s, "Pin config settings per pin group\n");
347 seq_puts(s, "Format: group (name): configs\n");
349 while (selector < ngroups) {
350 const char *gname = pctlops->get_group_name(pctldev, selector);
352 seq_printf(s, "%u (%s):", selector, gname);
353 pinconf_dump_group(pctldev, s, selector, gname);
354 seq_printf(s, "\n");
356 selector++;
359 return 0;
362 static int pinconf_pins_open(struct inode *inode, struct file *file)
364 return single_open(file, pinconf_pins_show, inode->i_private);
367 static int pinconf_groups_open(struct inode *inode, struct file *file)
369 return single_open(file, pinconf_groups_show, inode->i_private);
372 static const struct file_operations pinconf_pins_ops = {
373 .open = pinconf_pins_open,
374 .read = seq_read,
375 .llseek = seq_lseek,
376 .release = single_release,
379 static const struct file_operations pinconf_groups_ops = {
380 .open = pinconf_groups_open,
381 .read = seq_read,
382 .llseek = seq_lseek,
383 .release = single_release,
386 #define MAX_NAME_LEN 15
388 struct 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];
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
399 static struct dbg_cfg pinconf_dbg_conf;
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
407 static int pinconf_dbg_config_print(struct seq_file *s, void *d)
409 struct pinctrl_maps *maps_node;
410 const struct pinctrl_map *map;
411 const struct pinctrl_map *found = NULL;
412 struct pinctrl_dev *pctldev;
413 struct dbg_cfg *dbg = &pinconf_dbg_conf;
414 int i;
416 mutex_lock(&pinctrl_maps_mutex);
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;
427 if (!strcmp(map->data.configs.group_or_pin, dbg->pin_name)) {
428 /* We found the right pin */
429 found = map;
430 break;
434 if (!found) {
435 seq_printf(s, "No config found for dev/state/pin, expected:\n");
436 seq_printf(s, "Searched dev:%s\n", dbg->dev_name);
437 seq_printf(s, "Searched state:%s\n", dbg->state_name);
438 seq_printf(s, "Searched pin:%s\n", dbg->pin_name);
439 seq_printf(s, "Use: modify config_pin <devname> "\
440 "<state> <pinname> <value>\n");
441 goto exit;
444 pctldev = get_pinctrl_dev_from_devname(found->ctrl_dev_name);
445 seq_printf(s, "Dev %s has config of %s in state %s:\n",
446 dbg->dev_name, dbg->pin_name, dbg->state_name);
447 pinconf_show_config(s, pctldev, found->data.configs.configs,
448 found->data.configs.num_configs);
450 exit:
451 mutex_unlock(&pinctrl_maps_mutex);
453 return 0;
457 * pinconf_dbg_config_write() - modify the pinctrl config in the pinctrl
458 * map, of a dev/pin/state entry based on user entries to pinconf-config
459 * @user_buf: contains the modification request with expected format:
460 * modify <config> <devicename> <state> <name> <newvalue>
461 * modify is literal string, alternatives like add/delete not supported yet
462 * <config> is the configuration to be changed. Supported configs are
463 * "config_pin" or "config_group", alternatives like config_mux are not
464 * supported yet.
465 * <devicename> <state> <name> are values that should match the pinctrl-maps
466 * <newvalue> reflects the new config and is driver dependant
468 static ssize_t pinconf_dbg_config_write(struct file *file,
469 const char __user *user_buf, size_t count, loff_t *ppos)
471 struct pinctrl_maps *maps_node;
472 const struct pinctrl_map *map;
473 const struct pinctrl_map *found = NULL;
474 struct pinctrl_dev *pctldev;
475 const struct pinconf_ops *confops = NULL;
476 struct dbg_cfg *dbg = &pinconf_dbg_conf;
477 const struct pinctrl_map_configs *configs;
478 char config[MAX_NAME_LEN+1];
479 char buf[128];
480 char *b = &buf[0];
481 int buf_size;
482 char *token;
483 int i;
485 /* Get userspace string and assure termination */
486 buf_size = min(count, sizeof(buf) - 1);
487 if (copy_from_user(buf, user_buf, buf_size))
488 return -EFAULT;
489 buf[buf_size] = 0;
492 * need to parse entry and extract parameters:
493 * modify configs_pin devicename state pinname newvalue
496 /* Get arg: 'modify' */
497 token = strsep(&b, " ");
498 if (!token)
499 return -EINVAL;
500 if (strcmp(token, "modify"))
501 return -EINVAL;
504 * Get arg type: "config_pin" and "config_group"
505 * types are supported so far
507 token = strsep(&b, " ");
508 if (!token)
509 return -EINVAL;
510 if (!strcmp(token, "config_pin"))
511 dbg->map_type = PIN_MAP_TYPE_CONFIGS_PIN;
512 else if (!strcmp(token, "config_group"))
513 dbg->map_type = PIN_MAP_TYPE_CONFIGS_GROUP;
514 else
515 return -EINVAL;
517 /* get arg 'device_name' */
518 token = strsep(&b, " ");
519 if (token == NULL)
520 return -EINVAL;
521 if (strlen(token) >= MAX_NAME_LEN)
522 return -EINVAL;
523 strncpy(dbg->dev_name, token, MAX_NAME_LEN);
525 /* get arg 'state_name' */
526 token = strsep(&b, " ");
527 if (token == NULL)
528 return -EINVAL;
529 if (strlen(token) >= MAX_NAME_LEN)
530 return -EINVAL;
531 strncpy(dbg->state_name, token, MAX_NAME_LEN);
533 /* get arg 'pin_name' */
534 token = strsep(&b, " ");
535 if (token == NULL)
536 return -EINVAL;
537 if (strlen(token) >= MAX_NAME_LEN)
538 return -EINVAL;
539 strncpy(dbg->pin_name, token, MAX_NAME_LEN);
541 /* get new_value of config' */
542 token = strsep(&b, " ");
543 if (token == NULL)
544 return -EINVAL;
545 if (strlen(token) >= MAX_NAME_LEN)
546 return -EINVAL;
547 strncpy(config, token, MAX_NAME_LEN);
549 mutex_lock(&pinctrl_maps_mutex);
551 /* Parse the pinctrl map and look for the selected dev/state/pin */
552 for_each_maps(maps_node, i, map) {
553 if (strcmp(map->dev_name, dbg->dev_name))
554 continue;
555 if (map->type != dbg->map_type)
556 continue;
557 if (strcmp(map->name, dbg->state_name))
558 continue;
560 /* we found the right pin / state, so overwrite config */
561 if (!strcmp(map->data.configs.group_or_pin, dbg->pin_name)) {
562 found = map;
563 break;
567 if (!found) {
568 count = -EINVAL;
569 goto exit;
572 pctldev = get_pinctrl_dev_from_devname(found->ctrl_dev_name);
573 if (pctldev)
574 confops = pctldev->desc->confops;
576 if (confops && confops->pin_config_dbg_parse_modify) {
577 configs = &found->data.configs;
578 for (i = 0; i < configs->num_configs; i++) {
579 confops->pin_config_dbg_parse_modify(pctldev,
580 config,
581 &configs->configs[i]);
585 exit:
586 mutex_unlock(&pinctrl_maps_mutex);
588 return count;
591 static int pinconf_dbg_config_open(struct inode *inode, struct file *file)
593 return single_open(file, pinconf_dbg_config_print, inode->i_private);
596 static const struct file_operations pinconf_dbg_pinconfig_fops = {
597 .open = pinconf_dbg_config_open,
598 .write = pinconf_dbg_config_write,
599 .read = seq_read,
600 .llseek = seq_lseek,
601 .release = single_release,
602 .owner = THIS_MODULE,
605 void pinconf_init_device_debugfs(struct dentry *devroot,
606 struct pinctrl_dev *pctldev)
608 debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO,
609 devroot, pctldev, &pinconf_pins_ops);
610 debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO,
611 devroot, pctldev, &pinconf_groups_ops);
612 debugfs_create_file("pinconf-config", (S_IRUGO | S_IWUSR | S_IWGRP),
613 devroot, pctldev, &pinconf_dbg_pinconfig_fops);
616 #endif