blob: d9d35fcbfc6b33885ab31124718be422702916d1 [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>
Stephen Rothwella5a697c2011-09-30 14:39:04 +100015#include <linux/export.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020016#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"
Linus Walleijae6b4d82011-10-19 18:14:33 +020031#include "pinconf.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020032
33/* Global list of pin control devices */
34static DEFINE_MUTEX(pinctrldev_list_mutex);
35static LIST_HEAD(pinctrldev_list);
36
Linus Walleij2744e8a2011-05-02 20:50:54 +020037const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
38{
39 /* We're not allowed to register devices without name */
40 return pctldev->desc->name;
41}
42EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
43
44void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
45{
46 return pctldev->driver_data;
47}
48EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
49
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
56 *
57 * Looks up a pin control device matching a certain device name or pure device
58 * pointer, the pure device pointer will take precedence.
59 */
60struct pinctrl_dev *get_pinctrl_dev_from_dev(struct device *dev,
61 const char *devname)
62{
63 struct pinctrl_dev *pctldev = NULL;
64 bool found = false;
65
66 mutex_lock(&pinctrldev_list_mutex);
67 list_for_each_entry(pctldev, &pinctrldev_list, node) {
Stephen Warren51cd24e2011-12-09 16:59:05 -070068 if (dev && pctldev->dev == dev) {
Linus Walleij2744e8a2011-05-02 20:50:54 +020069 /* Matched on device pointer */
70 found = true;
71 break;
72 }
73
74 if (devname &&
Stephen Warren51cd24e2011-12-09 16:59:05 -070075 !strcmp(dev_name(pctldev->dev), devname)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +020076 /* Matched on device name */
77 found = true;
78 break;
79 }
80 }
81 mutex_unlock(&pinctrldev_list_mutex);
82
83 return found ? pctldev : NULL;
84}
85
Marek Belisko33d58942011-10-31 21:27:52 +010086struct pin_desc *pin_desc_get(struct pinctrl_dev *pctldev, unsigned int pin)
Linus Walleij2744e8a2011-05-02 20:50:54 +020087{
88 struct pin_desc *pindesc;
89 unsigned long flags;
90
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);
94
95 return pindesc;
96}
97
98/**
Linus Walleijae6b4d82011-10-19 18:14:33 +020099 * 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
102 */
103int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
104{
Chanho Park706e8522012-01-03 16:47:50 +0900105 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200106
Chanho Park706e8522012-01-03 16:47:50 +0900107 /* The pin number can be retrived from the pin controller descriptor */
108 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200109 struct pin_desc *desc;
110
Chanho Park706e8522012-01-03 16:47:50 +0900111 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200112 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;
118 }
119
120 return -EINVAL;
121}
122
123/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200124 * 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
127 *
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.
130 */
131bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
132{
133 struct pin_desc *pindesc;
134
135 if (pin < 0)
136 return false;
137
138 pindesc = pin_desc_get(pctldev, pin);
139 if (pindesc == NULL)
140 return false;
141
142 return true;
143}
144EXPORT_SYMBOL_GPL(pin_is_valid);
145
146/* Deletes a range of pin descriptors */
147static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
148 const struct pinctrl_pin_desc *pins,
149 unsigned num_pins)
150{
151 int i;
152
153 spin_lock(&pctldev->pin_desc_tree_lock);
154 for (i = 0; i < num_pins; i++) {
155 struct pin_desc *pindesc;
156
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);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100162 if (pindesc->dynamic_name)
163 kfree(pindesc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200164 }
165 kfree(pindesc);
166 }
167 spin_unlock(&pctldev->pin_desc_tree_lock);
168}
169
170static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
171 unsigned number, const char *name)
172{
173 struct pin_desc *pindesc;
174
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;
180 }
181
182 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
183 if (pindesc == NULL)
184 return -ENOMEM;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200185
Linus Walleij2744e8a2011-05-02 20:50:54 +0200186 spin_lock_init(&pindesc->lock);
187
188 /* Set owner */
189 pindesc->pctldev = pctldev;
190
Stephen Warren9af1e442011-10-19 16:19:27 -0600191 /* Copy basic pin info */
Linus Walleijca53c5f2011-12-14 20:33:37 +0100192 if (pindesc->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;
199 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200200
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",
Linus Walleijca53c5f2011-12-14 20:33:37 +0100205 number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200206 return 0;
207}
208
209static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
210 struct pinctrl_pin_desc const *pins,
211 unsigned num_descs)
212{
213 unsigned i;
214 int ret = 0;
215
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;
221 }
222
223 return 0;
224}
225
226/**
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
230 *
231 * Tries to match a GPIO pin number to the ranges handled by a certain pin
232 * controller, return the range or NULL
233 */
234static struct pinctrl_gpio_range *
235pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
236{
237 struct pinctrl_gpio_range *range = NULL;
238
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;
247 }
248 }
249 mutex_unlock(&pctldev->gpio_ranges_lock);
250
251 return NULL;
252}
253
254/**
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
259 *
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.
263 */
264int pinctrl_get_device_gpio_range(unsigned gpio,
265 struct pinctrl_dev **outdev,
266 struct pinctrl_gpio_range **outrange)
267{
268 struct pinctrl_dev *pctldev = NULL;
269
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;
274
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;
281 }
282 }
283 mutex_unlock(&pinctrldev_list_mutex);
284
285 return -EINVAL;
286}
287
288/**
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
292 *
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.
295 */
296void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
297 struct pinctrl_gpio_range *range)
298{
299 mutex_lock(&pctldev->gpio_ranges_lock);
300 list_add(&range->node, &pctldev->gpio_ranges);
301 mutex_unlock(&pctldev->gpio_ranges_lock);
302}
303
304/**
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
308 */
309void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
310 struct pinctrl_gpio_range *range)
311{
312 mutex_lock(&pctldev->gpio_ranges_lock);
313 list_del(&range->node);
314 mutex_unlock(&pctldev->gpio_ranges_lock);
315}
316
Linus Walleij7afde8b2011-10-19 17:07:16 +0200317/**
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
321 */
322int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
323 const char *pin_group)
324{
325 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
326 unsigned group_selector = 0;
327
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)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700332 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200333 "found group selector %u for %s\n",
334 group_selector,
335 pin_group);
336 return group_selector;
337 }
338
339 group_selector++;
340 }
341
Stephen Warren51cd24e2011-12-09 16:59:05 -0700342 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200343 pin_group);
344
345 return -EINVAL;
346}
347
Linus Walleij2744e8a2011-05-02 20:50:54 +0200348#ifdef CONFIG_DEBUG_FS
349
350static int pinctrl_pins_show(struct seq_file *s, void *what)
351{
352 struct pinctrl_dev *pctldev = s->private;
353 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +0900354 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200355
356 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200357
Chanho Park706e8522012-01-03 16:47:50 +0900358 /* The pin number can be retrived from the pin controller descriptor */
359 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200360 struct pin_desc *desc;
361
Chanho Park706e8522012-01-03 16:47:50 +0900362 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200363 desc = pin_desc_get(pctldev, pin);
364 /* Pin space may be sparse */
365 if (desc == NULL)
366 continue;
367
368 seq_printf(s, "pin %d (%s) ", pin,
369 desc->name ? desc->name : "unnamed");
370
371 /* Driver-specific info per pin */
372 if (ops->pin_dbg_show)
373 ops->pin_dbg_show(pctldev, s, pin);
374
375 seq_puts(s, "\n");
376 }
377
378 return 0;
379}
380
381static int pinctrl_groups_show(struct seq_file *s, void *what)
382{
383 struct pinctrl_dev *pctldev = s->private;
384 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
385 unsigned selector = 0;
386
387 /* No grouping */
388 if (!ops)
389 return 0;
390
391 seq_puts(s, "registered pin groups:\n");
392 while (ops->list_groups(pctldev, selector) >= 0) {
Stephen Warrena5818a82011-10-19 16:19:25 -0600393 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200394 unsigned num_pins;
395 const char *gname = ops->get_group_name(pctldev, selector);
396 int ret;
397 int i;
398
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");
409 }
410 selector++;
411 }
412
413
414 return 0;
415}
416
417static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
418{
419 struct pinctrl_dev *pctldev = s->private;
420 struct pinctrl_gpio_range *range = NULL;
421
422 seq_puts(s, "GPIO ranges handled:\n");
423
424 /* Loop over the ranges */
425 mutex_lock(&pctldev->gpio_ranges_lock);
426 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Linus Walleij75d66422011-11-16 09:58:51 +0100427 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));
Linus Walleij2744e8a2011-05-02 20:50:54 +0200432 }
433 mutex_unlock(&pctldev->gpio_ranges_lock);
434
435 return 0;
436}
437
438static int pinctrl_devices_show(struct seq_file *s, void *what)
439{
440 struct pinctrl_dev *pctldev;
441
Linus Walleijae6b4d82011-10-19 18:14:33 +0200442 seq_puts(s, "name [pinmux] [pinconf]\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200443 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)
Linus Walleijae6b4d82011-10-19 18:14:33 +0200447 seq_puts(s, "yes ");
448 else
449 seq_puts(s, "no ");
450 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200451 seq_puts(s, "yes");
452 else
453 seq_puts(s, "no");
454 seq_puts(s, "\n");
455 }
456 mutex_unlock(&pinctrldev_list_mutex);
457
458 return 0;
459}
460
461static int pinctrl_pins_open(struct inode *inode, struct file *file)
462{
463 return single_open(file, pinctrl_pins_show, inode->i_private);
464}
465
466static int pinctrl_groups_open(struct inode *inode, struct file *file)
467{
468 return single_open(file, pinctrl_groups_show, inode->i_private);
469}
470
471static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
472{
473 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
474}
475
476static int pinctrl_devices_open(struct inode *inode, struct file *file)
477{
478 return single_open(file, pinctrl_devices_show, NULL);
479}
480
481static const struct file_operations pinctrl_pins_ops = {
482 .open = pinctrl_pins_open,
483 .read = seq_read,
484 .llseek = seq_lseek,
485 .release = single_release,
486};
487
488static const struct file_operations pinctrl_groups_ops = {
489 .open = pinctrl_groups_open,
490 .read = seq_read,
491 .llseek = seq_lseek,
492 .release = single_release,
493};
494
495static const struct file_operations pinctrl_gpioranges_ops = {
496 .open = pinctrl_gpioranges_open,
497 .read = seq_read,
498 .llseek = seq_lseek,
499 .release = single_release,
500};
501
502static const struct file_operations pinctrl_devices_ops = {
503 .open = pinctrl_devices_open,
504 .read = seq_read,
505 .llseek = seq_lseek,
506 .release = single_release,
507};
508
509static struct dentry *debugfs_root;
510
511static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
512{
Tony Lindgren02157162012-01-20 08:17:22 -0800513 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200514
Stephen Warren51cd24e2011-12-09 16:59:05 -0700515 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +0200516 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -0800517 pctldev->device_root = device_root;
518
Linus Walleij2744e8a2011-05-02 20:50:54 +0200519 if (IS_ERR(device_root) || !device_root) {
520 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -0700521 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +0200522 return;
523 }
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);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200531 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200532}
533
Tony Lindgren02157162012-01-20 08:17:22 -0800534static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
535{
536 debugfs_remove_recursive(pctldev->device_root);
537}
538
Linus Walleij2744e8a2011-05-02 20:50:54 +0200539static void pinctrl_init_debugfs(void)
540{
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;
546 }
547
548 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
549 debugfs_root, NULL, &pinctrl_devices_ops);
550 pinmux_init_debugfs(debugfs_root);
551}
552
553#else /* CONFIG_DEBUG_FS */
554
555static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
556{
557}
558
559static void pinctrl_init_debugfs(void)
560{
561}
562
Tony Lindgren02157162012-01-20 08:17:22 -0800563static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
564{
565}
566
Linus Walleij2744e8a2011-05-02 20:50:54 +0200567#endif
568
569/**
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
574 */
575struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
576 struct device *dev, void *driver_data)
577{
Linus Walleij2744e8a2011-05-02 20:50:54 +0200578 struct pinctrl_dev *pctldev;
579 int ret;
580
581 if (pctldesc == NULL)
582 return NULL;
583 if (pctldesc->name == NULL)
584 return NULL;
585
586 /* If we're implementing pinmuxing, check the ops for sanity */
587 if (pctldesc->pmxops) {
588 ret = pinmux_check_ops(pctldesc->pmxops);
589 if (ret) {
590 pr_err("%s pinmux ops lacks necessary functions\n",
591 pctldesc->name);
592 return NULL;
593 }
594 }
595
Linus Walleijae6b4d82011-10-19 18:14:33 +0200596 /* If we're implementing pinconfig, check the ops for sanity */
597 if (pctldesc->confops) {
598 ret = pinconf_check_ops(pctldesc->confops);
599 if (ret) {
600 pr_err("%s pin config ops lacks necessary functions\n",
601 pctldesc->name);
602 return NULL;
603 }
604 }
605
Linus Walleij2744e8a2011-05-02 20:50:54 +0200606 pctldev = kzalloc(sizeof(struct pinctrl_dev), GFP_KERNEL);
607 if (pctldev == NULL)
608 return NULL;
609
610 /* Initialize pin control device struct */
611 pctldev->owner = pctldesc->owner;
612 pctldev->desc = pctldesc;
613 pctldev->driver_data = driver_data;
614 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
615 spin_lock_init(&pctldev->pin_desc_tree_lock);
616 INIT_LIST_HEAD(&pctldev->gpio_ranges);
617 mutex_init(&pctldev->gpio_ranges_lock);
Stephen Warren51cd24e2011-12-09 16:59:05 -0700618 pctldev->dev = dev;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200619
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);
Stephen Warren51cd24e2011-12-09 16:59:05 -0700628 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200629 }
630
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;
637
Stephen Warren51cd24e2011-12-09 16:59:05 -0700638out_err:
639 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200640 return NULL;
641}
642EXPORT_SYMBOL_GPL(pinctrl_register);
643
644/**
645 * pinctrl_unregister() - unregister pinmux
646 * @pctldev: pin controller to unregister
647 *
648 * Called by pinmux drivers to unregister a pinmux.
649 */
650void pinctrl_unregister(struct pinctrl_dev *pctldev)
651{
652 if (pctldev == NULL)
653 return;
654
Tony Lindgren02157162012-01-20 08:17:22 -0800655 pinctrl_remove_device_debugfs(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200656 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);
Stephen Warren51cd24e2011-12-09 16:59:05 -0700664 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200665}
666EXPORT_SYMBOL_GPL(pinctrl_unregister);
667
668static int __init pinctrl_init(void)
669{
670 pr_info("initialized pinctrl subsystem\n");
671 pinctrl_init_debugfs();
672 return 0;
673}
674
675/* init early since many drivers really need to initialized pinmux early */
676core_initcall(pinctrl_init);