spi-topcliff-pch: add recovery processing in case wait-event timeout
[zen-stable.git] / drivers / pinctrl / core.c
blob894cd5e103da6225bd660d66b696b44eab6d4a12
1 /*
2 * Core driver for the pin control subsystem
4 * Copyright (C) 2011 ST-Ericsson SA
5 * Written on behalf of Linaro for ST-Ericsson
6 * Based on bits of regulator core, gpio core and clk core
8 * Author: Linus Walleij <linus.walleij@linaro.org>
10 * License terms: GNU General Public License (GPL) version 2
12 #define pr_fmt(fmt) "pinctrl core: " fmt
14 #include <linux/kernel.h>
15 #include <linux/export.h>
16 #include <linux/init.h>
17 #include <linux/device.h>
18 #include <linux/slab.h>
19 #include <linux/radix-tree.h>
20 #include <linux/err.h>
21 #include <linux/list.h>
22 #include <linux/mutex.h>
23 #include <linux/spinlock.h>
24 #include <linux/sysfs.h>
25 #include <linux/debugfs.h>
26 #include <linux/seq_file.h>
27 #include <linux/pinctrl/pinctrl.h>
28 #include <linux/pinctrl/machine.h>
29 #include "core.h"
30 #include "pinmux.h"
31 #include "pinconf.h"
33 /* Global list of pin control devices */
34 static DEFINE_MUTEX(pinctrldev_list_mutex);
35 static LIST_HEAD(pinctrldev_list);
37 const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
39 /* We're not allowed to register devices without name */
40 return pctldev->desc->name;
42 EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
44 void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
46 return pctldev->driver_data;
48 EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
50 /**
51 * get_pinctrl_dev_from_dev() - look up pin controller device
52 * @dev: a device pointer, this may be NULL but then devname needs to be
53 * defined instead
54 * @devname: the name of a device instance, as returned by dev_name(), this
55 * may be NULL but then dev needs to be defined instead
57 * Looks up a pin control device matching a certain device name or pure device
58 * pointer, the pure device pointer will take precedence.
60 struct pinctrl_dev *get_pinctrl_dev_from_dev(struct device *dev,
61 const char *devname)
63 struct pinctrl_dev *pctldev = NULL;
64 bool found = false;
66 mutex_lock(&pinctrldev_list_mutex);
67 list_for_each_entry(pctldev, &pinctrldev_list, node) {
68 if (dev && pctldev->dev == dev) {
69 /* Matched on device pointer */
70 found = true;
71 break;
74 if (devname &&
75 !strcmp(dev_name(pctldev->dev), devname)) {
76 /* Matched on device name */
77 found = true;
78 break;
81 mutex_unlock(&pinctrldev_list_mutex);
83 return found ? pctldev : NULL;
86 struct pin_desc *pin_desc_get(struct pinctrl_dev *pctldev, unsigned int pin)
88 struct pin_desc *pindesc;
89 unsigned long flags;
91 spin_lock_irqsave(&pctldev->pin_desc_tree_lock, flags);
92 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree, pin);
93 spin_unlock_irqrestore(&pctldev->pin_desc_tree_lock, flags);
95 return pindesc;
98 /**
99 * pin_get_from_name() - look up a pin number from a name
100 * @pctldev: the pin control device to lookup the pin on
101 * @name: the name of the pin to look up
103 int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
105 unsigned i, pin;
107 /* The pin number can be retrived from the pin controller descriptor */
108 for (i = 0; i < pctldev->desc->npins; i++) {
109 struct pin_desc *desc;
111 pin = pctldev->desc->pins[i].number;
112 desc = pin_desc_get(pctldev, pin);
113 /* Pin space may be sparse */
114 if (desc == NULL)
115 continue;
116 if (desc->name && !strcmp(name, desc->name))
117 return pin;
120 return -EINVAL;
124 * pin_is_valid() - check if pin exists on controller
125 * @pctldev: the pin control device to check the pin on
126 * @pin: pin to check, use the local pin controller index number
128 * This tells us whether a certain pin exist on a certain pin controller or
129 * not. Pin lists may be sparse, so some pins may not exist.
131 bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
133 struct pin_desc *pindesc;
135 if (pin < 0)
136 return false;
138 pindesc = pin_desc_get(pctldev, pin);
139 if (pindesc == NULL)
140 return false;
142 return true;
144 EXPORT_SYMBOL_GPL(pin_is_valid);
146 /* Deletes a range of pin descriptors */
147 static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
148 const struct pinctrl_pin_desc *pins,
149 unsigned num_pins)
151 int i;
153 spin_lock(&pctldev->pin_desc_tree_lock);
154 for (i = 0; i < num_pins; i++) {
155 struct pin_desc *pindesc;
157 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
158 pins[i].number);
159 if (pindesc != NULL) {
160 radix_tree_delete(&pctldev->pin_desc_tree,
161 pins[i].number);
162 if (pindesc->dynamic_name)
163 kfree(pindesc->name);
165 kfree(pindesc);
167 spin_unlock(&pctldev->pin_desc_tree_lock);
170 static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
171 unsigned number, const char *name)
173 struct pin_desc *pindesc;
175 pindesc = pin_desc_get(pctldev, number);
176 if (pindesc != NULL) {
177 pr_err("pin %d already registered on %s\n", number,
178 pctldev->desc->name);
179 return -EINVAL;
182 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
183 if (pindesc == NULL)
184 return -ENOMEM;
186 spin_lock_init(&pindesc->lock);
188 /* Set owner */
189 pindesc->pctldev = pctldev;
191 /* Copy basic pin info */
192 if (name) {
193 pindesc->name = name;
194 } else {
195 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
196 if (pindesc->name == NULL)
197 return -ENOMEM;
198 pindesc->dynamic_name = true;
201 spin_lock(&pctldev->pin_desc_tree_lock);
202 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
203 spin_unlock(&pctldev->pin_desc_tree_lock);
204 pr_debug("registered pin %d (%s) on %s\n",
205 number, pindesc->name, pctldev->desc->name);
206 return 0;
209 static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
210 struct pinctrl_pin_desc const *pins,
211 unsigned num_descs)
213 unsigned i;
214 int ret = 0;
216 for (i = 0; i < num_descs; i++) {
217 ret = pinctrl_register_one_pin(pctldev,
218 pins[i].number, pins[i].name);
219 if (ret)
220 return ret;
223 return 0;
227 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
228 * @pctldev: pin controller device to check
229 * @gpio: gpio pin to check taken from the global GPIO pin space
231 * Tries to match a GPIO pin number to the ranges handled by a certain pin
232 * controller, return the range or NULL
234 static struct pinctrl_gpio_range *
235 pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
237 struct pinctrl_gpio_range *range = NULL;
239 /* Loop over the ranges */
240 mutex_lock(&pctldev->gpio_ranges_lock);
241 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
242 /* Check if we're in the valid range */
243 if (gpio >= range->base &&
244 gpio < range->base + range->npins) {
245 mutex_unlock(&pctldev->gpio_ranges_lock);
246 return range;
249 mutex_unlock(&pctldev->gpio_ranges_lock);
251 return NULL;
255 * pinctrl_get_device_gpio_range() - find device for GPIO range
256 * @gpio: the pin to locate the pin controller for
257 * @outdev: the pin control device if found
258 * @outrange: the GPIO range if found
260 * Find the pin controller handling a certain GPIO pin from the pinspace of
261 * the GPIO subsystem, return the device and the matching GPIO range. Returns
262 * negative if the GPIO range could not be found in any device.
264 int pinctrl_get_device_gpio_range(unsigned gpio,
265 struct pinctrl_dev **outdev,
266 struct pinctrl_gpio_range **outrange)
268 struct pinctrl_dev *pctldev = NULL;
270 /* Loop over the pin controllers */
271 mutex_lock(&pinctrldev_list_mutex);
272 list_for_each_entry(pctldev, &pinctrldev_list, node) {
273 struct pinctrl_gpio_range *range;
275 range = pinctrl_match_gpio_range(pctldev, gpio);
276 if (range != NULL) {
277 *outdev = pctldev;
278 *outrange = range;
279 mutex_unlock(&pinctrldev_list_mutex);
280 return 0;
283 mutex_unlock(&pinctrldev_list_mutex);
285 return -EINVAL;
289 * pinctrl_add_gpio_range() - register a GPIO range for a controller
290 * @pctldev: pin controller device to add the range to
291 * @range: the GPIO range to add
293 * This adds a range of GPIOs to be handled by a certain pin controller. Call
294 * this to register handled ranges after registering your pin controller.
296 void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
297 struct pinctrl_gpio_range *range)
299 mutex_lock(&pctldev->gpio_ranges_lock);
300 list_add(&range->node, &pctldev->gpio_ranges);
301 mutex_unlock(&pctldev->gpio_ranges_lock);
305 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
306 * @pctldev: pin controller device to remove the range from
307 * @range: the GPIO range to remove
309 void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
310 struct pinctrl_gpio_range *range)
312 mutex_lock(&pctldev->gpio_ranges_lock);
313 list_del(&range->node);
314 mutex_unlock(&pctldev->gpio_ranges_lock);
318 * pinctrl_get_group_selector() - returns the group selector for a group
319 * @pctldev: the pin controller handling the group
320 * @pin_group: the pin group to look up
322 int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
323 const char *pin_group)
325 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
326 unsigned group_selector = 0;
328 while (pctlops->list_groups(pctldev, group_selector) >= 0) {
329 const char *gname = pctlops->get_group_name(pctldev,
330 group_selector);
331 if (!strcmp(gname, pin_group)) {
332 dev_dbg(pctldev->dev,
333 "found group selector %u for %s\n",
334 group_selector,
335 pin_group);
336 return group_selector;
339 group_selector++;
342 dev_err(pctldev->dev, "does not have pin group %s\n",
343 pin_group);
345 return -EINVAL;
348 #ifdef CONFIG_DEBUG_FS
350 static int pinctrl_pins_show(struct seq_file *s, void *what)
352 struct pinctrl_dev *pctldev = s->private;
353 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
354 unsigned i, pin;
356 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
358 /* The pin number can be retrived from the pin controller descriptor */
359 for (i = 0; i < pctldev->desc->npins; i++) {
360 struct pin_desc *desc;
362 pin = pctldev->desc->pins[i].number;
363 desc = pin_desc_get(pctldev, pin);
364 /* Pin space may be sparse */
365 if (desc == NULL)
366 continue;
368 seq_printf(s, "pin %d (%s) ", pin,
369 desc->name ? desc->name : "unnamed");
371 /* Driver-specific info per pin */
372 if (ops->pin_dbg_show)
373 ops->pin_dbg_show(pctldev, s, pin);
375 seq_puts(s, "\n");
378 return 0;
381 static int pinctrl_groups_show(struct seq_file *s, void *what)
383 struct pinctrl_dev *pctldev = s->private;
384 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
385 unsigned selector = 0;
387 /* No grouping */
388 if (!ops)
389 return 0;
391 seq_puts(s, "registered pin groups:\n");
392 while (ops->list_groups(pctldev, selector) >= 0) {
393 const unsigned *pins;
394 unsigned num_pins;
395 const char *gname = ops->get_group_name(pctldev, selector);
396 int ret;
397 int i;
399 ret = ops->get_group_pins(pctldev, selector,
400 &pins, &num_pins);
401 if (ret)
402 seq_printf(s, "%s [ERROR GETTING PINS]\n",
403 gname);
404 else {
405 seq_printf(s, "group: %s, pins = [ ", gname);
406 for (i = 0; i < num_pins; i++)
407 seq_printf(s, "%d ", pins[i]);
408 seq_puts(s, "]\n");
410 selector++;
414 return 0;
417 static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
419 struct pinctrl_dev *pctldev = s->private;
420 struct pinctrl_gpio_range *range = NULL;
422 seq_puts(s, "GPIO ranges handled:\n");
424 /* Loop over the ranges */
425 mutex_lock(&pctldev->gpio_ranges_lock);
426 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
427 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
428 range->id, range->name,
429 range->base, (range->base + range->npins - 1),
430 range->pin_base,
431 (range->pin_base + range->npins - 1));
433 mutex_unlock(&pctldev->gpio_ranges_lock);
435 return 0;
438 static int pinctrl_devices_show(struct seq_file *s, void *what)
440 struct pinctrl_dev *pctldev;
442 seq_puts(s, "name [pinmux] [pinconf]\n");
443 mutex_lock(&pinctrldev_list_mutex);
444 list_for_each_entry(pctldev, &pinctrldev_list, node) {
445 seq_printf(s, "%s ", pctldev->desc->name);
446 if (pctldev->desc->pmxops)
447 seq_puts(s, "yes ");
448 else
449 seq_puts(s, "no ");
450 if (pctldev->desc->confops)
451 seq_puts(s, "yes");
452 else
453 seq_puts(s, "no");
454 seq_puts(s, "\n");
456 mutex_unlock(&pinctrldev_list_mutex);
458 return 0;
461 static int pinctrl_pins_open(struct inode *inode, struct file *file)
463 return single_open(file, pinctrl_pins_show, inode->i_private);
466 static int pinctrl_groups_open(struct inode *inode, struct file *file)
468 return single_open(file, pinctrl_groups_show, inode->i_private);
471 static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
473 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
476 static int pinctrl_devices_open(struct inode *inode, struct file *file)
478 return single_open(file, pinctrl_devices_show, NULL);
481 static const struct file_operations pinctrl_pins_ops = {
482 .open = pinctrl_pins_open,
483 .read = seq_read,
484 .llseek = seq_lseek,
485 .release = single_release,
488 static const struct file_operations pinctrl_groups_ops = {
489 .open = pinctrl_groups_open,
490 .read = seq_read,
491 .llseek = seq_lseek,
492 .release = single_release,
495 static const struct file_operations pinctrl_gpioranges_ops = {
496 .open = pinctrl_gpioranges_open,
497 .read = seq_read,
498 .llseek = seq_lseek,
499 .release = single_release,
502 static const struct file_operations pinctrl_devices_ops = {
503 .open = pinctrl_devices_open,
504 .read = seq_read,
505 .llseek = seq_lseek,
506 .release = single_release,
509 static struct dentry *debugfs_root;
511 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
513 struct dentry *device_root;
515 device_root = debugfs_create_dir(dev_name(pctldev->dev),
516 debugfs_root);
517 pctldev->device_root = device_root;
519 if (IS_ERR(device_root) || !device_root) {
520 pr_warn("failed to create debugfs directory for %s\n",
521 dev_name(pctldev->dev));
522 return;
524 debugfs_create_file("pins", S_IFREG | S_IRUGO,
525 device_root, pctldev, &pinctrl_pins_ops);
526 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
527 device_root, pctldev, &pinctrl_groups_ops);
528 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
529 device_root, pctldev, &pinctrl_gpioranges_ops);
530 pinmux_init_device_debugfs(device_root, pctldev);
531 pinconf_init_device_debugfs(device_root, pctldev);
534 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
536 debugfs_remove_recursive(pctldev->device_root);
539 static void pinctrl_init_debugfs(void)
541 debugfs_root = debugfs_create_dir("pinctrl", NULL);
542 if (IS_ERR(debugfs_root) || !debugfs_root) {
543 pr_warn("failed to create debugfs directory\n");
544 debugfs_root = NULL;
545 return;
548 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
549 debugfs_root, NULL, &pinctrl_devices_ops);
550 pinmux_init_debugfs(debugfs_root);
553 #else /* CONFIG_DEBUG_FS */
555 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
559 static void pinctrl_init_debugfs(void)
563 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
567 #endif
570 * pinctrl_register() - register a pin controller device
571 * @pctldesc: descriptor for this pin controller
572 * @dev: parent device for this pin controller
573 * @driver_data: private pin controller data for this pin controller
575 struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
576 struct device *dev, void *driver_data)
578 struct pinctrl_dev *pctldev;
579 int ret;
581 if (pctldesc == NULL)
582 return NULL;
583 if (pctldesc->name == NULL)
584 return NULL;
586 pctldev = kzalloc(sizeof(struct pinctrl_dev), GFP_KERNEL);
587 if (pctldev == NULL)
588 return NULL;
590 /* Initialize pin control device struct */
591 pctldev->owner = pctldesc->owner;
592 pctldev->desc = pctldesc;
593 pctldev->driver_data = driver_data;
594 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
595 spin_lock_init(&pctldev->pin_desc_tree_lock);
596 INIT_LIST_HEAD(&pctldev->gpio_ranges);
597 mutex_init(&pctldev->gpio_ranges_lock);
598 pctldev->dev = dev;
600 /* If we're implementing pinmuxing, check the ops for sanity */
601 if (pctldesc->pmxops) {
602 ret = pinmux_check_ops(pctldev);
603 if (ret) {
604 pr_err("%s pinmux ops lacks necessary functions\n",
605 pctldesc->name);
606 goto out_err;
610 /* If we're implementing pinconfig, check the ops for sanity */
611 if (pctldesc->confops) {
612 ret = pinconf_check_ops(pctldev);
613 if (ret) {
614 pr_err("%s pin config ops lacks necessary functions\n",
615 pctldesc->name);
616 goto out_err;
620 /* Register all the pins */
621 pr_debug("try to register %d pins on %s...\n",
622 pctldesc->npins, pctldesc->name);
623 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
624 if (ret) {
625 pr_err("error during pin registration\n");
626 pinctrl_free_pindescs(pctldev, pctldesc->pins,
627 pctldesc->npins);
628 goto out_err;
631 pinctrl_init_device_debugfs(pctldev);
632 mutex_lock(&pinctrldev_list_mutex);
633 list_add(&pctldev->node, &pinctrldev_list);
634 mutex_unlock(&pinctrldev_list_mutex);
635 pinmux_hog_maps(pctldev);
636 return pctldev;
638 out_err:
639 kfree(pctldev);
640 return NULL;
642 EXPORT_SYMBOL_GPL(pinctrl_register);
645 * pinctrl_unregister() - unregister pinmux
646 * @pctldev: pin controller to unregister
648 * Called by pinmux drivers to unregister a pinmux.
650 void pinctrl_unregister(struct pinctrl_dev *pctldev)
652 if (pctldev == NULL)
653 return;
655 pinctrl_remove_device_debugfs(pctldev);
656 pinmux_unhog_maps(pctldev);
657 /* TODO: check that no pinmuxes are still active? */
658 mutex_lock(&pinctrldev_list_mutex);
659 list_del(&pctldev->node);
660 mutex_unlock(&pinctrldev_list_mutex);
661 /* Destroy descriptor tree */
662 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
663 pctldev->desc->npins);
664 kfree(pctldev);
666 EXPORT_SYMBOL_GPL(pinctrl_unregister);
668 static int __init pinctrl_init(void)
670 pr_info("initialized pinctrl subsystem\n");
671 pinctrl_init_debugfs();
672 return 0;
675 /* init early since many drivers really need to initialized pinmux early */
676 core_initcall(pinctrl_init);