Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-btrfs-devel.git] / drivers / pinctrl / core.c
blob423522d87313b583620ddbad5d85272793e0e794
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/init.h>
16 #include <linux/device.h>
17 #include <linux/slab.h>
18 #include <linux/radix-tree.h>
19 #include <linux/err.h>
20 #include <linux/list.h>
21 #include <linux/mutex.h>
22 #include <linux/spinlock.h>
23 #include <linux/sysfs.h>
24 #include <linux/debugfs.h>
25 #include <linux/seq_file.h>
26 #include <linux/pinctrl/pinctrl.h>
27 #include <linux/pinctrl/machine.h>
28 #include "core.h"
29 #include "pinmux.h"
31 /* Global list of pin control devices */
32 static DEFINE_MUTEX(pinctrldev_list_mutex);
33 static LIST_HEAD(pinctrldev_list);
35 static void pinctrl_dev_release(struct device *dev)
37 struct pinctrl_dev *pctldev = dev_get_drvdata(dev);
38 kfree(pctldev);
41 const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
43 /* We're not allowed to register devices without name */
44 return pctldev->desc->name;
46 EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
48 void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
50 return pctldev->driver_data;
52 EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
54 /**
55 * get_pinctrl_dev_from_dev() - look up pin controller device
56 * @dev: a device pointer, this may be NULL but then devname needs to be
57 * defined instead
58 * @devname: the name of a device instance, as returned by dev_name(), this
59 * may be NULL but then dev needs to be defined instead
61 * Looks up a pin control device matching a certain device name or pure device
62 * pointer, the pure device pointer will take precedence.
64 struct pinctrl_dev *get_pinctrl_dev_from_dev(struct device *dev,
65 const char *devname)
67 struct pinctrl_dev *pctldev = NULL;
68 bool found = false;
70 mutex_lock(&pinctrldev_list_mutex);
71 list_for_each_entry(pctldev, &pinctrldev_list, node) {
72 if (dev && &pctldev->dev == dev) {
73 /* Matched on device pointer */
74 found = true;
75 break;
78 if (devname &&
79 !strcmp(dev_name(&pctldev->dev), devname)) {
80 /* Matched on device name */
81 found = true;
82 break;
85 mutex_unlock(&pinctrldev_list_mutex);
87 return found ? pctldev : NULL;
90 struct pin_desc *pin_desc_get(struct pinctrl_dev *pctldev, int pin)
92 struct pin_desc *pindesc;
93 unsigned long flags;
95 spin_lock_irqsave(&pctldev->pin_desc_tree_lock, flags);
96 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree, pin);
97 spin_unlock_irqrestore(&pctldev->pin_desc_tree_lock, flags);
99 return pindesc;
103 * pin_is_valid() - check if pin exists on controller
104 * @pctldev: the pin control device to check the pin on
105 * @pin: pin to check, use the local pin controller index number
107 * This tells us whether a certain pin exist on a certain pin controller or
108 * not. Pin lists may be sparse, so some pins may not exist.
110 bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
112 struct pin_desc *pindesc;
114 if (pin < 0)
115 return false;
117 pindesc = pin_desc_get(pctldev, pin);
118 if (pindesc == NULL)
119 return false;
121 return true;
123 EXPORT_SYMBOL_GPL(pin_is_valid);
125 /* Deletes a range of pin descriptors */
126 static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
127 const struct pinctrl_pin_desc *pins,
128 unsigned num_pins)
130 int i;
132 spin_lock(&pctldev->pin_desc_tree_lock);
133 for (i = 0; i < num_pins; i++) {
134 struct pin_desc *pindesc;
136 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
137 pins[i].number);
138 if (pindesc != NULL) {
139 radix_tree_delete(&pctldev->pin_desc_tree,
140 pins[i].number);
142 kfree(pindesc);
144 spin_unlock(&pctldev->pin_desc_tree_lock);
147 static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
148 unsigned number, const char *name)
150 struct pin_desc *pindesc;
152 pindesc = pin_desc_get(pctldev, number);
153 if (pindesc != NULL) {
154 pr_err("pin %d already registered on %s\n", number,
155 pctldev->desc->name);
156 return -EINVAL;
159 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
160 if (pindesc == NULL)
161 return -ENOMEM;
162 spin_lock_init(&pindesc->lock);
164 /* Set owner */
165 pindesc->pctldev = pctldev;
167 /* Copy basic pin info */
168 pindesc->name = name;
170 spin_lock(&pctldev->pin_desc_tree_lock);
171 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
172 spin_unlock(&pctldev->pin_desc_tree_lock);
173 pr_debug("registered pin %d (%s) on %s\n",
174 number, name ? name : "(unnamed)", pctldev->desc->name);
175 return 0;
178 static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
179 struct pinctrl_pin_desc const *pins,
180 unsigned num_descs)
182 unsigned i;
183 int ret = 0;
185 for (i = 0; i < num_descs; i++) {
186 ret = pinctrl_register_one_pin(pctldev,
187 pins[i].number, pins[i].name);
188 if (ret)
189 return ret;
192 return 0;
196 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
197 * @pctldev: pin controller device to check
198 * @gpio: gpio pin to check taken from the global GPIO pin space
200 * Tries to match a GPIO pin number to the ranges handled by a certain pin
201 * controller, return the range or NULL
203 static struct pinctrl_gpio_range *
204 pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
206 struct pinctrl_gpio_range *range = NULL;
208 /* Loop over the ranges */
209 mutex_lock(&pctldev->gpio_ranges_lock);
210 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
211 /* Check if we're in the valid range */
212 if (gpio >= range->base &&
213 gpio < range->base + range->npins) {
214 mutex_unlock(&pctldev->gpio_ranges_lock);
215 return range;
218 mutex_unlock(&pctldev->gpio_ranges_lock);
220 return NULL;
224 * pinctrl_get_device_gpio_range() - find device for GPIO range
225 * @gpio: the pin to locate the pin controller for
226 * @outdev: the pin control device if found
227 * @outrange: the GPIO range if found
229 * Find the pin controller handling a certain GPIO pin from the pinspace of
230 * the GPIO subsystem, return the device and the matching GPIO range. Returns
231 * negative if the GPIO range could not be found in any device.
233 int pinctrl_get_device_gpio_range(unsigned gpio,
234 struct pinctrl_dev **outdev,
235 struct pinctrl_gpio_range **outrange)
237 struct pinctrl_dev *pctldev = NULL;
239 /* Loop over the pin controllers */
240 mutex_lock(&pinctrldev_list_mutex);
241 list_for_each_entry(pctldev, &pinctrldev_list, node) {
242 struct pinctrl_gpio_range *range;
244 range = pinctrl_match_gpio_range(pctldev, gpio);
245 if (range != NULL) {
246 *outdev = pctldev;
247 *outrange = range;
248 mutex_unlock(&pinctrldev_list_mutex);
249 return 0;
252 mutex_unlock(&pinctrldev_list_mutex);
254 return -EINVAL;
258 * pinctrl_add_gpio_range() - register a GPIO range for a controller
259 * @pctldev: pin controller device to add the range to
260 * @range: the GPIO range to add
262 * This adds a range of GPIOs to be handled by a certain pin controller. Call
263 * this to register handled ranges after registering your pin controller.
265 void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
266 struct pinctrl_gpio_range *range)
268 mutex_lock(&pctldev->gpio_ranges_lock);
269 list_add(&range->node, &pctldev->gpio_ranges);
270 mutex_unlock(&pctldev->gpio_ranges_lock);
274 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
275 * @pctldev: pin controller device to remove the range from
276 * @range: the GPIO range to remove
278 void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
279 struct pinctrl_gpio_range *range)
281 mutex_lock(&pctldev->gpio_ranges_lock);
282 list_del(&range->node);
283 mutex_unlock(&pctldev->gpio_ranges_lock);
286 #ifdef CONFIG_DEBUG_FS
288 static int pinctrl_pins_show(struct seq_file *s, void *what)
290 struct pinctrl_dev *pctldev = s->private;
291 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
292 unsigned pin;
294 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
295 seq_printf(s, "max pin number: %d\n", pctldev->desc->maxpin);
297 /* The highest pin number need to be included in the loop, thus <= */
298 for (pin = 0; pin <= pctldev->desc->maxpin; pin++) {
299 struct pin_desc *desc;
301 desc = pin_desc_get(pctldev, pin);
302 /* Pin space may be sparse */
303 if (desc == NULL)
304 continue;
306 seq_printf(s, "pin %d (%s) ", pin,
307 desc->name ? desc->name : "unnamed");
309 /* Driver-specific info per pin */
310 if (ops->pin_dbg_show)
311 ops->pin_dbg_show(pctldev, s, pin);
313 seq_puts(s, "\n");
316 return 0;
319 static int pinctrl_groups_show(struct seq_file *s, void *what)
321 struct pinctrl_dev *pctldev = s->private;
322 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
323 unsigned selector = 0;
325 /* No grouping */
326 if (!ops)
327 return 0;
329 seq_puts(s, "registered pin groups:\n");
330 while (ops->list_groups(pctldev, selector) >= 0) {
331 const unsigned *pins;
332 unsigned num_pins;
333 const char *gname = ops->get_group_name(pctldev, selector);
334 int ret;
335 int i;
337 ret = ops->get_group_pins(pctldev, selector,
338 &pins, &num_pins);
339 if (ret)
340 seq_printf(s, "%s [ERROR GETTING PINS]\n",
341 gname);
342 else {
343 seq_printf(s, "group: %s, pins = [ ", gname);
344 for (i = 0; i < num_pins; i++)
345 seq_printf(s, "%d ", pins[i]);
346 seq_puts(s, "]\n");
348 selector++;
352 return 0;
355 static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
357 struct pinctrl_dev *pctldev = s->private;
358 struct pinctrl_gpio_range *range = NULL;
360 seq_puts(s, "GPIO ranges handled:\n");
362 /* Loop over the ranges */
363 mutex_lock(&pctldev->gpio_ranges_lock);
364 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
365 seq_printf(s, "%u: %s [%u - %u]\n", range->id, range->name,
366 range->base, (range->base + range->npins - 1));
368 mutex_unlock(&pctldev->gpio_ranges_lock);
370 return 0;
373 static int pinctrl_devices_show(struct seq_file *s, void *what)
375 struct pinctrl_dev *pctldev;
377 seq_puts(s, "name [pinmux]\n");
378 mutex_lock(&pinctrldev_list_mutex);
379 list_for_each_entry(pctldev, &pinctrldev_list, node) {
380 seq_printf(s, "%s ", pctldev->desc->name);
381 if (pctldev->desc->pmxops)
382 seq_puts(s, "yes");
383 else
384 seq_puts(s, "no");
385 seq_puts(s, "\n");
387 mutex_unlock(&pinctrldev_list_mutex);
389 return 0;
392 static int pinctrl_pins_open(struct inode *inode, struct file *file)
394 return single_open(file, pinctrl_pins_show, inode->i_private);
397 static int pinctrl_groups_open(struct inode *inode, struct file *file)
399 return single_open(file, pinctrl_groups_show, inode->i_private);
402 static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
404 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
407 static int pinctrl_devices_open(struct inode *inode, struct file *file)
409 return single_open(file, pinctrl_devices_show, NULL);
412 static const struct file_operations pinctrl_pins_ops = {
413 .open = pinctrl_pins_open,
414 .read = seq_read,
415 .llseek = seq_lseek,
416 .release = single_release,
419 static const struct file_operations pinctrl_groups_ops = {
420 .open = pinctrl_groups_open,
421 .read = seq_read,
422 .llseek = seq_lseek,
423 .release = single_release,
426 static const struct file_operations pinctrl_gpioranges_ops = {
427 .open = pinctrl_gpioranges_open,
428 .read = seq_read,
429 .llseek = seq_lseek,
430 .release = single_release,
433 static const struct file_operations pinctrl_devices_ops = {
434 .open = pinctrl_devices_open,
435 .read = seq_read,
436 .llseek = seq_lseek,
437 .release = single_release,
440 static struct dentry *debugfs_root;
442 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
444 static struct dentry *device_root;
446 device_root = debugfs_create_dir(dev_name(&pctldev->dev),
447 debugfs_root);
448 if (IS_ERR(device_root) || !device_root) {
449 pr_warn("failed to create debugfs directory for %s\n",
450 dev_name(&pctldev->dev));
451 return;
453 debugfs_create_file("pins", S_IFREG | S_IRUGO,
454 device_root, pctldev, &pinctrl_pins_ops);
455 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
456 device_root, pctldev, &pinctrl_groups_ops);
457 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
458 device_root, pctldev, &pinctrl_gpioranges_ops);
459 pinmux_init_device_debugfs(device_root, pctldev);
462 static void pinctrl_init_debugfs(void)
464 debugfs_root = debugfs_create_dir("pinctrl", NULL);
465 if (IS_ERR(debugfs_root) || !debugfs_root) {
466 pr_warn("failed to create debugfs directory\n");
467 debugfs_root = NULL;
468 return;
471 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
472 debugfs_root, NULL, &pinctrl_devices_ops);
473 pinmux_init_debugfs(debugfs_root);
476 #else /* CONFIG_DEBUG_FS */
478 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
482 static void pinctrl_init_debugfs(void)
486 #endif
489 * pinctrl_register() - register a pin controller device
490 * @pctldesc: descriptor for this pin controller
491 * @dev: parent device for this pin controller
492 * @driver_data: private pin controller data for this pin controller
494 struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
495 struct device *dev, void *driver_data)
497 static atomic_t pinmux_no = ATOMIC_INIT(0);
498 struct pinctrl_dev *pctldev;
499 int ret;
501 if (pctldesc == NULL)
502 return NULL;
503 if (pctldesc->name == NULL)
504 return NULL;
506 /* If we're implementing pinmuxing, check the ops for sanity */
507 if (pctldesc->pmxops) {
508 ret = pinmux_check_ops(pctldesc->pmxops);
509 if (ret) {
510 pr_err("%s pinmux ops lacks necessary functions\n",
511 pctldesc->name);
512 return NULL;
516 pctldev = kzalloc(sizeof(struct pinctrl_dev), GFP_KERNEL);
517 if (pctldev == NULL)
518 return NULL;
520 /* Initialize pin control device struct */
521 pctldev->owner = pctldesc->owner;
522 pctldev->desc = pctldesc;
523 pctldev->driver_data = driver_data;
524 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
525 spin_lock_init(&pctldev->pin_desc_tree_lock);
526 INIT_LIST_HEAD(&pctldev->gpio_ranges);
527 mutex_init(&pctldev->gpio_ranges_lock);
529 /* Register device */
530 pctldev->dev.parent = dev;
531 dev_set_name(&pctldev->dev, "pinctrl.%d",
532 atomic_inc_return(&pinmux_no) - 1);
533 pctldev->dev.release = pinctrl_dev_release;
534 ret = device_register(&pctldev->dev);
535 if (ret != 0) {
536 pr_err("error in device registration\n");
537 goto out_reg_dev_err;
539 dev_set_drvdata(&pctldev->dev, pctldev);
541 /* Register all the pins */
542 pr_debug("try to register %d pins on %s...\n",
543 pctldesc->npins, pctldesc->name);
544 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
545 if (ret) {
546 pr_err("error during pin registration\n");
547 pinctrl_free_pindescs(pctldev, pctldesc->pins,
548 pctldesc->npins);
549 goto out_reg_pins_err;
552 pinctrl_init_device_debugfs(pctldev);
553 mutex_lock(&pinctrldev_list_mutex);
554 list_add(&pctldev->node, &pinctrldev_list);
555 mutex_unlock(&pinctrldev_list_mutex);
556 pinmux_hog_maps(pctldev);
557 return pctldev;
559 out_reg_pins_err:
560 device_del(&pctldev->dev);
561 out_reg_dev_err:
562 put_device(&pctldev->dev);
563 return NULL;
565 EXPORT_SYMBOL_GPL(pinctrl_register);
568 * pinctrl_unregister() - unregister pinmux
569 * @pctldev: pin controller to unregister
571 * Called by pinmux drivers to unregister a pinmux.
573 void pinctrl_unregister(struct pinctrl_dev *pctldev)
575 if (pctldev == NULL)
576 return;
578 pinmux_unhog_maps(pctldev);
579 /* TODO: check that no pinmuxes are still active? */
580 mutex_lock(&pinctrldev_list_mutex);
581 list_del(&pctldev->node);
582 mutex_unlock(&pinctrldev_list_mutex);
583 /* Destroy descriptor tree */
584 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
585 pctldev->desc->npins);
586 device_unregister(&pctldev->dev);
588 EXPORT_SYMBOL_GPL(pinctrl_unregister);
590 static int __init pinctrl_init(void)
592 pr_info("initialized pinctrl subsystem\n");
593 pinctrl_init_debugfs();
594 return 0;
597 /* init early since many drivers really need to initialized pinmux early */
598 core_initcall(pinctrl_init);