blob: ceb63275c7fe3ca1eff3c74c008a3fda6b07ce7d [file] [log] [blame]
Linus Walleij2744e8a2011-05-02 20:50:54 +02001/*
2 * Core driver for the pin control subsystem
3 *
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
7 *
8 * Author: Linus Walleij <linus.walleij@linaro.org>
9 *
10 * License terms: GNU General Public License (GPL) version 2
11 */
12#define pr_fmt(fmt) "pinctrl core: " fmt
13
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"
30
31/* Global list of pin control devices */
32static DEFINE_MUTEX(pinctrldev_list_mutex);
33static LIST_HEAD(pinctrldev_list);
34
35static void pinctrl_dev_release(struct device *dev)
36{
37 struct pinctrl_dev *pctldev = dev_get_drvdata(dev);
38 kfree(pctldev);
39}
40
41const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
42{
43 /* We're not allowed to register devices without name */
44 return pctldev->desc->name;
45}
46EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
47
48void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
49{
50 return pctldev->driver_data;
51}
52EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
53
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
60 *
61 * Looks up a pin control device matching a certain device name or pure device
62 * pointer, the pure device pointer will take precedence.
63 */
64struct pinctrl_dev *get_pinctrl_dev_from_dev(struct device *dev,
65 const char *devname)
66{
67 struct pinctrl_dev *pctldev = NULL;
68 bool found = false;
69
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;
76 }
77
78 if (devname &&
79 !strcmp(dev_name(&pctldev->dev), devname)) {
80 /* Matched on device name */
81 found = true;
82 break;
83 }
84 }
85 mutex_unlock(&pinctrldev_list_mutex);
86
87 return found ? pctldev : NULL;
88}
89
90struct pin_desc *pin_desc_get(struct pinctrl_dev *pctldev, int pin)
91{
92 struct pin_desc *pindesc;
93 unsigned long flags;
94
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);
98
99 return pindesc;
100}
101
102/**
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
106 *
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.
109 */
110bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
111{
112 struct pin_desc *pindesc;
113
114 if (pin < 0)
115 return false;
116
117 pindesc = pin_desc_get(pctldev, pin);
118 if (pindesc == NULL)
119 return false;
120
121 return true;
122}
123EXPORT_SYMBOL_GPL(pin_is_valid);
124
125/* Deletes a range of pin descriptors */
126static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
127 const struct pinctrl_pin_desc *pins,
128 unsigned num_pins)
129{
130 int i;
131
132 spin_lock(&pctldev->pin_desc_tree_lock);
133 for (i = 0; i < num_pins; i++) {
134 struct pin_desc *pindesc;
135
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);
141 }
142 kfree(pindesc);
143 }
144 spin_unlock(&pctldev->pin_desc_tree_lock);
145}
146
147static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
148 unsigned number, const char *name)
149{
150 struct pin_desc *pindesc;
151
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;
157 }
158
159 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
160 if (pindesc == NULL)
161 return -ENOMEM;
162 spin_lock_init(&pindesc->lock);
163
164 /* Set owner */
165 pindesc->pctldev = pctldev;
166
167 /* Copy optional basic pin info */
168 if (name)
169 strlcpy(pindesc->name, name, sizeof(pindesc->name));
170
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;
177}
178
179static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
180 struct pinctrl_pin_desc const *pins,
181 unsigned num_descs)
182{
183 unsigned i;
184 int ret = 0;
185
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;
191 }
192
193 return 0;
194}
195
196/**
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
200 *
201 * Tries to match a GPIO pin number to the ranges handled by a certain pin
202 * controller, return the range or NULL
203 */
204static struct pinctrl_gpio_range *
205pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
206{
207 struct pinctrl_gpio_range *range = NULL;
208
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;
217 }
218 }
219 mutex_unlock(&pctldev->gpio_ranges_lock);
220
221 return NULL;
222}
223
224/**
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
229 *
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.
233 */
234int pinctrl_get_device_gpio_range(unsigned gpio,
235 struct pinctrl_dev **outdev,
236 struct pinctrl_gpio_range **outrange)
237{
238 struct pinctrl_dev *pctldev = NULL;
239
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;
244
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;
251 }
252 }
253 mutex_unlock(&pinctrldev_list_mutex);
254
255 return -EINVAL;
256}
257
258/**
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
262 *
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.
265 */
266void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
267 struct pinctrl_gpio_range *range)
268{
269 mutex_lock(&pctldev->gpio_ranges_lock);
270 list_add(&range->node, &pctldev->gpio_ranges);
271 mutex_unlock(&pctldev->gpio_ranges_lock);
272}
273
274/**
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
278 */
279void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
280 struct pinctrl_gpio_range *range)
281{
282 mutex_lock(&pctldev->gpio_ranges_lock);
283 list_del(&range->node);
284 mutex_unlock(&pctldev->gpio_ranges_lock);
285}
286
287#ifdef CONFIG_DEBUG_FS
288
289static int pinctrl_pins_show(struct seq_file *s, void *what)
290{
291 struct pinctrl_dev *pctldev = s->private;
292 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
293 unsigned pin;
294
295 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
296 seq_printf(s, "max pin number: %d\n", pctldev->desc->maxpin);
297
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;
301
302 desc = pin_desc_get(pctldev, pin);
303 /* Pin space may be sparse */
304 if (desc == NULL)
305 continue;
306
307 seq_printf(s, "pin %d (%s) ", pin,
308 desc->name ? desc->name : "unnamed");
309
310 /* Driver-specific info per pin */
311 if (ops->pin_dbg_show)
312 ops->pin_dbg_show(pctldev, s, pin);
313
314 seq_puts(s, "\n");
315 }
316
317 return 0;
318}
319
320static int pinctrl_groups_show(struct seq_file *s, void *what)
321{
322 struct pinctrl_dev *pctldev = s->private;
323 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
324 unsigned selector = 0;
325
326 /* No grouping */
327 if (!ops)
328 return 0;
329
330 seq_puts(s, "registered pin groups:\n");
331 while (ops->list_groups(pctldev, selector) >= 0) {
Stephen Warrena5818a82011-10-19 16:19:25 -0600332 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200333 unsigned num_pins;
334 const char *gname = ops->get_group_name(pctldev, selector);
335 int ret;
336 int i;
337
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");
348 }
349 selector++;
350 }
351
352
353 return 0;
354}
355
356static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
357{
358 struct pinctrl_dev *pctldev = s->private;
359 struct pinctrl_gpio_range *range = NULL;
360
361 seq_puts(s, "GPIO ranges handled:\n");
362
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));
368 }
369 mutex_unlock(&pctldev->gpio_ranges_lock);
370
371 return 0;
372}
373
374static int pinctrl_devices_show(struct seq_file *s, void *what)
375{
376 struct pinctrl_dev *pctldev;
377
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");
387 }
388 mutex_unlock(&pinctrldev_list_mutex);
389
390 return 0;
391}
392
393static int pinctrl_pins_open(struct inode *inode, struct file *file)
394{
395 return single_open(file, pinctrl_pins_show, inode->i_private);
396}
397
398static int pinctrl_groups_open(struct inode *inode, struct file *file)
399{
400 return single_open(file, pinctrl_groups_show, inode->i_private);
401}
402
403static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
404{
405 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
406}
407
408static int pinctrl_devices_open(struct inode *inode, struct file *file)
409{
410 return single_open(file, pinctrl_devices_show, NULL);
411}
412
413static const struct file_operations pinctrl_pins_ops = {
414 .open = pinctrl_pins_open,
415 .read = seq_read,
416 .llseek = seq_lseek,
417 .release = single_release,
418};
419
420static const struct file_operations pinctrl_groups_ops = {
421 .open = pinctrl_groups_open,
422 .read = seq_read,
423 .llseek = seq_lseek,
424 .release = single_release,
425};
426
427static const struct file_operations pinctrl_gpioranges_ops = {
428 .open = pinctrl_gpioranges_open,
429 .read = seq_read,
430 .llseek = seq_lseek,
431 .release = single_release,
432};
433
434static const struct file_operations pinctrl_devices_ops = {
435 .open = pinctrl_devices_open,
436 .read = seq_read,
437 .llseek = seq_lseek,
438 .release = single_release,
439};
440
441static struct dentry *debugfs_root;
442
443static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
444{
445 static struct dentry *device_root;
446
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;
453 }
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);
461}
462
463static void pinctrl_init_debugfs(void)
464{
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;
470 }
471
472 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
473 debugfs_root, NULL, &pinctrl_devices_ops);
474 pinmux_init_debugfs(debugfs_root);
475}
476
477#else /* CONFIG_DEBUG_FS */
478
479static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
480{
481}
482
483static void pinctrl_init_debugfs(void)
484{
485}
486
487#endif
488
489/**
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
494 */
495struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
496 struct device *dev, void *driver_data)
497{
498 static atomic_t pinmux_no = ATOMIC_INIT(0);
499 struct pinctrl_dev *pctldev;
500 int ret;
501
502 if (pctldesc == NULL)
503 return NULL;
504 if (pctldesc->name == NULL)
505 return NULL;
506
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;
514 }
515 }
516
517 pctldev = kzalloc(sizeof(struct pinctrl_dev), GFP_KERNEL);
518 if (pctldev == NULL)
519 return NULL;
520
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);
529
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;
539 }
540 dev_set_drvdata(&pctldev->dev, pctldev);
541
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;
551 }
552
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;
559
560out_reg_pins_err:
561 device_del(&pctldev->dev);
562out_reg_dev_err:
563 put_device(&pctldev->dev);
564 return NULL;
565}
566EXPORT_SYMBOL_GPL(pinctrl_register);
567
568/**
569 * pinctrl_unregister() - unregister pinmux
570 * @pctldev: pin controller to unregister
571 *
572 * Called by pinmux drivers to unregister a pinmux.
573 */
574void pinctrl_unregister(struct pinctrl_dev *pctldev)
575{
576 if (pctldev == NULL)
577 return;
578
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);
588}
589EXPORT_SYMBOL_GPL(pinctrl_unregister);
590
591static int __init pinctrl_init(void)
592{
593 pr_info("initialized pinctrl subsystem\n");
594 pinctrl_init_debugfs();
595 return 0;
596}
597
598/* init early since many drivers really need to initialized pinmux early */
599core_initcall(pinctrl_init);