f_phonet: fix page offset of first received fragment
[linux/fpc-iii.git] / drivers / pinctrl / core.c
blobeadef9e191ea59d5f59b9453894a1d3d1824b4bf
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"
32 /* Global list of pin control devices */
33 static DEFINE_MUTEX(pinctrldev_list_mutex);
34 static LIST_HEAD(pinctrldev_list);
36 static void pinctrl_dev_release(struct device *dev)
38 struct pinctrl_dev *pctldev = dev_get_drvdata(dev);
39 kfree(pctldev);
42 const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
44 /* We're not allowed to register devices without name */
45 return pctldev->desc->name;
47 EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
49 void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
51 return pctldev->driver_data;
53 EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
55 /**
56 * get_pinctrl_dev_from_dev() - look up pin controller device
57 * @dev: a device pointer, this may be NULL but then devname needs to be
58 * defined instead
59 * @devname: the name of a device instance, as returned by dev_name(), this
60 * may be NULL but then dev needs to be defined instead
62 * Looks up a pin control device matching a certain device name or pure device
63 * pointer, the pure device pointer will take precedence.
65 struct pinctrl_dev *get_pinctrl_dev_from_dev(struct device *dev,
66 const char *devname)
68 struct pinctrl_dev *pctldev = NULL;
69 bool found = false;
71 mutex_lock(&pinctrldev_list_mutex);
72 list_for_each_entry(pctldev, &pinctrldev_list, node) {
73 if (dev && &pctldev->dev == dev) {
74 /* Matched on device pointer */
75 found = true;
76 break;
79 if (devname &&
80 !strcmp(dev_name(&pctldev->dev), devname)) {
81 /* Matched on device name */
82 found = true;
83 break;
86 mutex_unlock(&pinctrldev_list_mutex);
88 return found ? pctldev : NULL;
91 struct pin_desc *pin_desc_get(struct pinctrl_dev *pctldev, int pin)
93 struct pin_desc *pindesc;
94 unsigned long flags;
96 spin_lock_irqsave(&pctldev->pin_desc_tree_lock, flags);
97 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree, pin);
98 spin_unlock_irqrestore(&pctldev->pin_desc_tree_lock, flags);
100 return pindesc;
104 * pin_is_valid() - check if pin exists on controller
105 * @pctldev: the pin control device to check the pin on
106 * @pin: pin to check, use the local pin controller index number
108 * This tells us whether a certain pin exist on a certain pin controller or
109 * not. Pin lists may be sparse, so some pins may not exist.
111 bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
113 struct pin_desc *pindesc;
115 if (pin < 0)
116 return false;
118 pindesc = pin_desc_get(pctldev, pin);
119 if (pindesc == NULL)
120 return false;
122 return true;
124 EXPORT_SYMBOL_GPL(pin_is_valid);
126 /* Deletes a range of pin descriptors */
127 static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
128 const struct pinctrl_pin_desc *pins,
129 unsigned num_pins)
131 int i;
133 spin_lock(&pctldev->pin_desc_tree_lock);
134 for (i = 0; i < num_pins; i++) {
135 struct pin_desc *pindesc;
137 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
138 pins[i].number);
139 if (pindesc != NULL) {
140 radix_tree_delete(&pctldev->pin_desc_tree,
141 pins[i].number);
143 kfree(pindesc);
145 spin_unlock(&pctldev->pin_desc_tree_lock);
148 static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
149 unsigned number, const char *name)
151 struct pin_desc *pindesc;
153 pindesc = pin_desc_get(pctldev, number);
154 if (pindesc != NULL) {
155 pr_err("pin %d already registered on %s\n", number,
156 pctldev->desc->name);
157 return -EINVAL;
160 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
161 if (pindesc == NULL)
162 return -ENOMEM;
163 spin_lock_init(&pindesc->lock);
165 /* Set owner */
166 pindesc->pctldev = pctldev;
168 /* Copy basic pin info */
169 pindesc->name = name;
171 spin_lock(&pctldev->pin_desc_tree_lock);
172 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
173 spin_unlock(&pctldev->pin_desc_tree_lock);
174 pr_debug("registered pin %d (%s) on %s\n",
175 number, name ? name : "(unnamed)", pctldev->desc->name);
176 return 0;
179 static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
180 struct pinctrl_pin_desc const *pins,
181 unsigned num_descs)
183 unsigned i;
184 int ret = 0;
186 for (i = 0; i < num_descs; i++) {
187 ret = pinctrl_register_one_pin(pctldev,
188 pins[i].number, pins[i].name);
189 if (ret)
190 return ret;
193 return 0;
197 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
198 * @pctldev: pin controller device to check
199 * @gpio: gpio pin to check taken from the global GPIO pin space
201 * Tries to match a GPIO pin number to the ranges handled by a certain pin
202 * controller, return the range or NULL
204 static struct pinctrl_gpio_range *
205 pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
207 struct pinctrl_gpio_range *range = NULL;
209 /* Loop over the ranges */
210 mutex_lock(&pctldev->gpio_ranges_lock);
211 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
212 /* Check if we're in the valid range */
213 if (gpio >= range->base &&
214 gpio < range->base + range->npins) {
215 mutex_unlock(&pctldev->gpio_ranges_lock);
216 return range;
219 mutex_unlock(&pctldev->gpio_ranges_lock);
221 return NULL;
225 * pinctrl_get_device_gpio_range() - find device for GPIO range
226 * @gpio: the pin to locate the pin controller for
227 * @outdev: the pin control device if found
228 * @outrange: the GPIO range if found
230 * Find the pin controller handling a certain GPIO pin from the pinspace of
231 * the GPIO subsystem, return the device and the matching GPIO range. Returns
232 * negative if the GPIO range could not be found in any device.
234 int pinctrl_get_device_gpio_range(unsigned gpio,
235 struct pinctrl_dev **outdev,
236 struct pinctrl_gpio_range **outrange)
238 struct pinctrl_dev *pctldev = NULL;
240 /* Loop over the pin controllers */
241 mutex_lock(&pinctrldev_list_mutex);
242 list_for_each_entry(pctldev, &pinctrldev_list, node) {
243 struct pinctrl_gpio_range *range;
245 range = pinctrl_match_gpio_range(pctldev, gpio);
246 if (range != NULL) {
247 *outdev = pctldev;
248 *outrange = range;
249 mutex_unlock(&pinctrldev_list_mutex);
250 return 0;
253 mutex_unlock(&pinctrldev_list_mutex);
255 return -EINVAL;
259 * pinctrl_add_gpio_range() - register a GPIO range for a controller
260 * @pctldev: pin controller device to add the range to
261 * @range: the GPIO range to add
263 * This adds a range of GPIOs to be handled by a certain pin controller. Call
264 * this to register handled ranges after registering your pin controller.
266 void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
267 struct pinctrl_gpio_range *range)
269 mutex_lock(&pctldev->gpio_ranges_lock);
270 list_add(&range->node, &pctldev->gpio_ranges);
271 mutex_unlock(&pctldev->gpio_ranges_lock);
275 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
276 * @pctldev: pin controller device to remove the range from
277 * @range: the GPIO range to remove
279 void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
280 struct pinctrl_gpio_range *range)
282 mutex_lock(&pctldev->gpio_ranges_lock);
283 list_del(&range->node);
284 mutex_unlock(&pctldev->gpio_ranges_lock);
287 #ifdef CONFIG_DEBUG_FS
289 static int pinctrl_pins_show(struct seq_file *s, void *what)
291 struct pinctrl_dev *pctldev = s->private;
292 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
293 unsigned pin;
295 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
296 seq_printf(s, "max pin number: %d\n", pctldev->desc->maxpin);
298 /* The highest pin number need to be included in the loop, thus <= */
299 for (pin = 0; pin <= pctldev->desc->maxpin; pin++) {
300 struct pin_desc *desc;
302 desc = pin_desc_get(pctldev, pin);
303 /* Pin space may be sparse */
304 if (desc == NULL)
305 continue;
307 seq_printf(s, "pin %d (%s) ", pin,
308 desc->name ? desc->name : "unnamed");
310 /* Driver-specific info per pin */
311 if (ops->pin_dbg_show)
312 ops->pin_dbg_show(pctldev, s, pin);
314 seq_puts(s, "\n");
317 return 0;
320 static int pinctrl_groups_show(struct seq_file *s, void *what)
322 struct pinctrl_dev *pctldev = s->private;
323 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
324 unsigned selector = 0;
326 /* No grouping */
327 if (!ops)
328 return 0;
330 seq_puts(s, "registered pin groups:\n");
331 while (ops->list_groups(pctldev, selector) >= 0) {
332 const unsigned *pins;
333 unsigned num_pins;
334 const char *gname = ops->get_group_name(pctldev, selector);
335 int ret;
336 int i;
338 ret = ops->get_group_pins(pctldev, selector,
339 &pins, &num_pins);
340 if (ret)
341 seq_printf(s, "%s [ERROR GETTING PINS]\n",
342 gname);
343 else {
344 seq_printf(s, "group: %s, pins = [ ", gname);
345 for (i = 0; i < num_pins; i++)
346 seq_printf(s, "%d ", pins[i]);
347 seq_puts(s, "]\n");
349 selector++;
353 return 0;
356 static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
358 struct pinctrl_dev *pctldev = s->private;
359 struct pinctrl_gpio_range *range = NULL;
361 seq_puts(s, "GPIO ranges handled:\n");
363 /* Loop over the ranges */
364 mutex_lock(&pctldev->gpio_ranges_lock);
365 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
366 seq_printf(s, "%u: %s [%u - %u]\n", range->id, range->name,
367 range->base, (range->base + range->npins - 1));
369 mutex_unlock(&pctldev->gpio_ranges_lock);
371 return 0;
374 static int pinctrl_devices_show(struct seq_file *s, void *what)
376 struct pinctrl_dev *pctldev;
378 seq_puts(s, "name [pinmux]\n");
379 mutex_lock(&pinctrldev_list_mutex);
380 list_for_each_entry(pctldev, &pinctrldev_list, node) {
381 seq_printf(s, "%s ", pctldev->desc->name);
382 if (pctldev->desc->pmxops)
383 seq_puts(s, "yes");
384 else
385 seq_puts(s, "no");
386 seq_puts(s, "\n");
388 mutex_unlock(&pinctrldev_list_mutex);
390 return 0;
393 static int pinctrl_pins_open(struct inode *inode, struct file *file)
395 return single_open(file, pinctrl_pins_show, inode->i_private);
398 static int pinctrl_groups_open(struct inode *inode, struct file *file)
400 return single_open(file, pinctrl_groups_show, inode->i_private);
403 static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
405 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
408 static int pinctrl_devices_open(struct inode *inode, struct file *file)
410 return single_open(file, pinctrl_devices_show, NULL);
413 static const struct file_operations pinctrl_pins_ops = {
414 .open = pinctrl_pins_open,
415 .read = seq_read,
416 .llseek = seq_lseek,
417 .release = single_release,
420 static const struct file_operations pinctrl_groups_ops = {
421 .open = pinctrl_groups_open,
422 .read = seq_read,
423 .llseek = seq_lseek,
424 .release = single_release,
427 static const struct file_operations pinctrl_gpioranges_ops = {
428 .open = pinctrl_gpioranges_open,
429 .read = seq_read,
430 .llseek = seq_lseek,
431 .release = single_release,
434 static const struct file_operations pinctrl_devices_ops = {
435 .open = pinctrl_devices_open,
436 .read = seq_read,
437 .llseek = seq_lseek,
438 .release = single_release,
441 static struct dentry *debugfs_root;
443 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
445 static struct dentry *device_root;
447 device_root = debugfs_create_dir(dev_name(&pctldev->dev),
448 debugfs_root);
449 if (IS_ERR(device_root) || !device_root) {
450 pr_warn("failed to create debugfs directory for %s\n",
451 dev_name(&pctldev->dev));
452 return;
454 debugfs_create_file("pins", S_IFREG | S_IRUGO,
455 device_root, pctldev, &pinctrl_pins_ops);
456 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
457 device_root, pctldev, &pinctrl_groups_ops);
458 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
459 device_root, pctldev, &pinctrl_gpioranges_ops);
460 pinmux_init_device_debugfs(device_root, pctldev);
463 static void pinctrl_init_debugfs(void)
465 debugfs_root = debugfs_create_dir("pinctrl", NULL);
466 if (IS_ERR(debugfs_root) || !debugfs_root) {
467 pr_warn("failed to create debugfs directory\n");
468 debugfs_root = NULL;
469 return;
472 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
473 debugfs_root, NULL, &pinctrl_devices_ops);
474 pinmux_init_debugfs(debugfs_root);
477 #else /* CONFIG_DEBUG_FS */
479 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
483 static void pinctrl_init_debugfs(void)
487 #endif
490 * pinctrl_register() - register a pin controller device
491 * @pctldesc: descriptor for this pin controller
492 * @dev: parent device for this pin controller
493 * @driver_data: private pin controller data for this pin controller
495 struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
496 struct device *dev, void *driver_data)
498 static atomic_t pinmux_no = ATOMIC_INIT(0);
499 struct pinctrl_dev *pctldev;
500 int ret;
502 if (pctldesc == NULL)
503 return NULL;
504 if (pctldesc->name == NULL)
505 return NULL;
507 /* If we're implementing pinmuxing, check the ops for sanity */
508 if (pctldesc->pmxops) {
509 ret = pinmux_check_ops(pctldesc->pmxops);
510 if (ret) {
511 pr_err("%s pinmux ops lacks necessary functions\n",
512 pctldesc->name);
513 return NULL;
517 pctldev = kzalloc(sizeof(struct pinctrl_dev), GFP_KERNEL);
518 if (pctldev == NULL)
519 return NULL;
521 /* Initialize pin control device struct */
522 pctldev->owner = pctldesc->owner;
523 pctldev->desc = pctldesc;
524 pctldev->driver_data = driver_data;
525 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
526 spin_lock_init(&pctldev->pin_desc_tree_lock);
527 INIT_LIST_HEAD(&pctldev->gpio_ranges);
528 mutex_init(&pctldev->gpio_ranges_lock);
530 /* Register device */
531 pctldev->dev.parent = dev;
532 dev_set_name(&pctldev->dev, "pinctrl.%d",
533 atomic_inc_return(&pinmux_no) - 1);
534 pctldev->dev.release = pinctrl_dev_release;
535 ret = device_register(&pctldev->dev);
536 if (ret != 0) {
537 pr_err("error in device registration\n");
538 goto out_reg_dev_err;
540 dev_set_drvdata(&pctldev->dev, pctldev);
542 /* Register all the pins */
543 pr_debug("try to register %d pins on %s...\n",
544 pctldesc->npins, pctldesc->name);
545 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
546 if (ret) {
547 pr_err("error during pin registration\n");
548 pinctrl_free_pindescs(pctldev, pctldesc->pins,
549 pctldesc->npins);
550 goto out_reg_pins_err;
553 pinctrl_init_device_debugfs(pctldev);
554 mutex_lock(&pinctrldev_list_mutex);
555 list_add(&pctldev->node, &pinctrldev_list);
556 mutex_unlock(&pinctrldev_list_mutex);
557 pinmux_hog_maps(pctldev);
558 return pctldev;
560 out_reg_pins_err:
561 device_del(&pctldev->dev);
562 out_reg_dev_err:
563 put_device(&pctldev->dev);
564 return NULL;
566 EXPORT_SYMBOL_GPL(pinctrl_register);
569 * pinctrl_unregister() - unregister pinmux
570 * @pctldev: pin controller to unregister
572 * Called by pinmux drivers to unregister a pinmux.
574 void pinctrl_unregister(struct pinctrl_dev *pctldev)
576 if (pctldev == NULL)
577 return;
579 pinmux_unhog_maps(pctldev);
580 /* TODO: check that no pinmuxes are still active? */
581 mutex_lock(&pinctrldev_list_mutex);
582 list_del(&pctldev->node);
583 mutex_unlock(&pinctrldev_list_mutex);
584 /* Destroy descriptor tree */
585 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
586 pctldev->desc->npins);
587 device_unregister(&pctldev->dev);
589 EXPORT_SYMBOL_GPL(pinctrl_unregister);
591 static int __init pinctrl_init(void)
593 pr_info("initialized pinctrl subsystem\n");
594 pinctrl_init_debugfs();
595 return 0;
598 /* init early since many drivers really need to initialized pinmux early */
599 core_initcall(pinctrl_init);