blob: 423522d87313b583620ddbad5d85272793e0e794 [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
Stephen Warren9af1e442011-10-19 16:19:27 -0600167 /* Copy basic pin info */
168 pindesc->name = name;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200169
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;
176}
177
178static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
179 struct pinctrl_pin_desc const *pins,
180 unsigned num_descs)
181{
182 unsigned i;
183 int ret = 0;
184
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;
190 }
191
192 return 0;
193}
194
195/**
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
199 *
200 * Tries to match a GPIO pin number to the ranges handled by a certain pin
201 * controller, return the range or NULL
202 */
203static struct pinctrl_gpio_range *
204pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
205{
206 struct pinctrl_gpio_range *range = NULL;
207
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;
216 }
217 }
218 mutex_unlock(&pctldev->gpio_ranges_lock);
219
220 return NULL;
221}
222
223/**
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
228 *
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.
232 */
233int pinctrl_get_device_gpio_range(unsigned gpio,
234 struct pinctrl_dev **outdev,
235 struct pinctrl_gpio_range **outrange)
236{
237 struct pinctrl_dev *pctldev = NULL;
238
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;
243
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;
250 }
251 }
252 mutex_unlock(&pinctrldev_list_mutex);
253
254 return -EINVAL;
255}
256
257/**
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
261 *
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.
264 */
265void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
266 struct pinctrl_gpio_range *range)
267{
268 mutex_lock(&pctldev->gpio_ranges_lock);
269 list_add(&range->node, &pctldev->gpio_ranges);
270 mutex_unlock(&pctldev->gpio_ranges_lock);
271}
272
273/**
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
277 */
278void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
279 struct pinctrl_gpio_range *range)
280{
281 mutex_lock(&pctldev->gpio_ranges_lock);
282 list_del(&range->node);
283 mutex_unlock(&pctldev->gpio_ranges_lock);
284}
285
286#ifdef CONFIG_DEBUG_FS
287
288static int pinctrl_pins_show(struct seq_file *s, void *what)
289{
290 struct pinctrl_dev *pctldev = s->private;
291 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
292 unsigned pin;
293
294 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
295 seq_printf(s, "max pin number: %d\n", pctldev->desc->maxpin);
296
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;
300
301 desc = pin_desc_get(pctldev, pin);
302 /* Pin space may be sparse */
303 if (desc == NULL)
304 continue;
305
306 seq_printf(s, "pin %d (%s) ", pin,
307 desc->name ? desc->name : "unnamed");
308
309 /* Driver-specific info per pin */
310 if (ops->pin_dbg_show)
311 ops->pin_dbg_show(pctldev, s, pin);
312
313 seq_puts(s, "\n");
314 }
315
316 return 0;
317}
318
319static int pinctrl_groups_show(struct seq_file *s, void *what)
320{
321 struct pinctrl_dev *pctldev = s->private;
322 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
323 unsigned selector = 0;
324
325 /* No grouping */
326 if (!ops)
327 return 0;
328
329 seq_puts(s, "registered pin groups:\n");
330 while (ops->list_groups(pctldev, selector) >= 0) {
Stephen Warrena5818a82011-10-19 16:19:25 -0600331 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200332 unsigned num_pins;
333 const char *gname = ops->get_group_name(pctldev, selector);
334 int ret;
335 int i;
336
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");
347 }
348 selector++;
349 }
350
351
352 return 0;
353}
354
355static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
356{
357 struct pinctrl_dev *pctldev = s->private;
358 struct pinctrl_gpio_range *range = NULL;
359
360 seq_puts(s, "GPIO ranges handled:\n");
361
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));
367 }
368 mutex_unlock(&pctldev->gpio_ranges_lock);
369
370 return 0;
371}
372
373static int pinctrl_devices_show(struct seq_file *s, void *what)
374{
375 struct pinctrl_dev *pctldev;
376
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");
386 }
387 mutex_unlock(&pinctrldev_list_mutex);
388
389 return 0;
390}
391
392static int pinctrl_pins_open(struct inode *inode, struct file *file)
393{
394 return single_open(file, pinctrl_pins_show, inode->i_private);
395}
396
397static int pinctrl_groups_open(struct inode *inode, struct file *file)
398{
399 return single_open(file, pinctrl_groups_show, inode->i_private);
400}
401
402static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
403{
404 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
405}
406
407static int pinctrl_devices_open(struct inode *inode, struct file *file)
408{
409 return single_open(file, pinctrl_devices_show, NULL);
410}
411
412static const struct file_operations pinctrl_pins_ops = {
413 .open = pinctrl_pins_open,
414 .read = seq_read,
415 .llseek = seq_lseek,
416 .release = single_release,
417};
418
419static const struct file_operations pinctrl_groups_ops = {
420 .open = pinctrl_groups_open,
421 .read = seq_read,
422 .llseek = seq_lseek,
423 .release = single_release,
424};
425
426static const struct file_operations pinctrl_gpioranges_ops = {
427 .open = pinctrl_gpioranges_open,
428 .read = seq_read,
429 .llseek = seq_lseek,
430 .release = single_release,
431};
432
433static const struct file_operations pinctrl_devices_ops = {
434 .open = pinctrl_devices_open,
435 .read = seq_read,
436 .llseek = seq_lseek,
437 .release = single_release,
438};
439
440static struct dentry *debugfs_root;
441
442static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
443{
444 static struct dentry *device_root;
445
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;
452 }
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);
460}
461
462static void pinctrl_init_debugfs(void)
463{
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;
469 }
470
471 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
472 debugfs_root, NULL, &pinctrl_devices_ops);
473 pinmux_init_debugfs(debugfs_root);
474}
475
476#else /* CONFIG_DEBUG_FS */
477
478static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
479{
480}
481
482static void pinctrl_init_debugfs(void)
483{
484}
485
486#endif
487
488/**
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
493 */
494struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
495 struct device *dev, void *driver_data)
496{
497 static atomic_t pinmux_no = ATOMIC_INIT(0);
498 struct pinctrl_dev *pctldev;
499 int ret;
500
501 if (pctldesc == NULL)
502 return NULL;
503 if (pctldesc->name == NULL)
504 return NULL;
505
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;
513 }
514 }
515
516 pctldev = kzalloc(sizeof(struct pinctrl_dev), GFP_KERNEL);
517 if (pctldev == NULL)
518 return NULL;
519
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);
528
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;
538 }
539 dev_set_drvdata(&pctldev->dev, pctldev);
540
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;
550 }
551
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;
558
559out_reg_pins_err:
560 device_del(&pctldev->dev);
561out_reg_dev_err:
562 put_device(&pctldev->dev);
563 return NULL;
564}
565EXPORT_SYMBOL_GPL(pinctrl_register);
566
567/**
568 * pinctrl_unregister() - unregister pinmux
569 * @pctldev: pin controller to unregister
570 *
571 * Called by pinmux drivers to unregister a pinmux.
572 */
573void pinctrl_unregister(struct pinctrl_dev *pctldev)
574{
575 if (pctldev == NULL)
576 return;
577
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);
587}
588EXPORT_SYMBOL_GPL(pinctrl_unregister);
589
590static int __init pinctrl_init(void)
591{
592 pr_info("initialized pinctrl subsystem\n");
593 pinctrl_init_debugfs();
594 return 0;
595}
596
597/* init early since many drivers really need to initialized pinmux early */
598core_initcall(pinctrl_init);