blob: 4f10476cc1f275fa831f0bb3a6d5e98c74a80be3 [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/**
Linus Walleij9dfac4f2012-02-01 18:02:47 +010051 * get_pinctrl_dev_from_devname() - look up pin controller device
52 * @devname: the name of a device instance, as returned by dev_name()
Linus Walleij2744e8a2011-05-02 20:50:54 +020053 *
54 * Looks up a pin control device matching a certain device name or pure device
55 * pointer, the pure device pointer will take precedence.
56 */
Linus Walleij9dfac4f2012-02-01 18:02:47 +010057struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
Linus Walleij2744e8a2011-05-02 20:50:54 +020058{
59 struct pinctrl_dev *pctldev = NULL;
60 bool found = false;
61
Linus Walleij9dfac4f2012-02-01 18:02:47 +010062 if (!devname)
63 return NULL;
64
Linus Walleij2744e8a2011-05-02 20:50:54 +020065 mutex_lock(&pinctrldev_list_mutex);
66 list_for_each_entry(pctldev, &pinctrldev_list, node) {
Linus Walleij9dfac4f2012-02-01 18:02:47 +010067 if (!strcmp(dev_name(pctldev->dev), devname)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +020068 /* Matched on device name */
69 found = true;
70 break;
71 }
72 }
73 mutex_unlock(&pinctrldev_list_mutex);
74
75 return found ? pctldev : NULL;
76}
77
Marek Belisko33d58942011-10-31 21:27:52 +010078struct pin_desc *pin_desc_get(struct pinctrl_dev *pctldev, unsigned int pin)
Linus Walleij2744e8a2011-05-02 20:50:54 +020079{
80 struct pin_desc *pindesc;
81 unsigned long flags;
82
83 spin_lock_irqsave(&pctldev->pin_desc_tree_lock, flags);
84 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree, pin);
85 spin_unlock_irqrestore(&pctldev->pin_desc_tree_lock, flags);
86
87 return pindesc;
88}
89
90/**
Linus Walleijae6b4d82011-10-19 18:14:33 +020091 * pin_get_from_name() - look up a pin number from a name
92 * @pctldev: the pin control device to lookup the pin on
93 * @name: the name of the pin to look up
94 */
95int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
96{
Chanho Park706e8522012-01-03 16:47:50 +090097 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +020098
Chanho Park706e8522012-01-03 16:47:50 +090099 /* The pin number can be retrived from the pin controller descriptor */
100 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200101 struct pin_desc *desc;
102
Chanho Park706e8522012-01-03 16:47:50 +0900103 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200104 desc = pin_desc_get(pctldev, pin);
105 /* Pin space may be sparse */
106 if (desc == NULL)
107 continue;
108 if (desc->name && !strcmp(name, desc->name))
109 return pin;
110 }
111
112 return -EINVAL;
113}
114
115/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200116 * pin_is_valid() - check if pin exists on controller
117 * @pctldev: the pin control device to check the pin on
118 * @pin: pin to check, use the local pin controller index number
119 *
120 * This tells us whether a certain pin exist on a certain pin controller or
121 * not. Pin lists may be sparse, so some pins may not exist.
122 */
123bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
124{
125 struct pin_desc *pindesc;
126
127 if (pin < 0)
128 return false;
129
130 pindesc = pin_desc_get(pctldev, pin);
131 if (pindesc == NULL)
132 return false;
133
134 return true;
135}
136EXPORT_SYMBOL_GPL(pin_is_valid);
137
138/* Deletes a range of pin descriptors */
139static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
140 const struct pinctrl_pin_desc *pins,
141 unsigned num_pins)
142{
143 int i;
144
145 spin_lock(&pctldev->pin_desc_tree_lock);
146 for (i = 0; i < num_pins; i++) {
147 struct pin_desc *pindesc;
148
149 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
150 pins[i].number);
151 if (pindesc != NULL) {
152 radix_tree_delete(&pctldev->pin_desc_tree,
153 pins[i].number);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100154 if (pindesc->dynamic_name)
155 kfree(pindesc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200156 }
157 kfree(pindesc);
158 }
159 spin_unlock(&pctldev->pin_desc_tree_lock);
160}
161
162static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
163 unsigned number, const char *name)
164{
165 struct pin_desc *pindesc;
166
167 pindesc = pin_desc_get(pctldev, number);
168 if (pindesc != NULL) {
169 pr_err("pin %d already registered on %s\n", number,
170 pctldev->desc->name);
171 return -EINVAL;
172 }
173
174 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
175 if (pindesc == NULL)
176 return -ENOMEM;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200177
Linus Walleij2744e8a2011-05-02 20:50:54 +0200178 spin_lock_init(&pindesc->lock);
179
180 /* Set owner */
181 pindesc->pctldev = pctldev;
182
Stephen Warren9af1e442011-10-19 16:19:27 -0600183 /* Copy basic pin info */
Linus Walleij8dc6ae42012-02-01 18:11:40 +0100184 if (name) {
Linus Walleijca53c5f2011-12-14 20:33:37 +0100185 pindesc->name = name;
186 } else {
187 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
188 if (pindesc->name == NULL)
189 return -ENOMEM;
190 pindesc->dynamic_name = true;
191 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200192
193 spin_lock(&pctldev->pin_desc_tree_lock);
194 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
195 spin_unlock(&pctldev->pin_desc_tree_lock);
196 pr_debug("registered pin %d (%s) on %s\n",
Linus Walleijca53c5f2011-12-14 20:33:37 +0100197 number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200198 return 0;
199}
200
201static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
202 struct pinctrl_pin_desc const *pins,
203 unsigned num_descs)
204{
205 unsigned i;
206 int ret = 0;
207
208 for (i = 0; i < num_descs; i++) {
209 ret = pinctrl_register_one_pin(pctldev,
210 pins[i].number, pins[i].name);
211 if (ret)
212 return ret;
213 }
214
215 return 0;
216}
217
218/**
219 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
220 * @pctldev: pin controller device to check
221 * @gpio: gpio pin to check taken from the global GPIO pin space
222 *
223 * Tries to match a GPIO pin number to the ranges handled by a certain pin
224 * controller, return the range or NULL
225 */
226static struct pinctrl_gpio_range *
227pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
228{
229 struct pinctrl_gpio_range *range = NULL;
230
231 /* Loop over the ranges */
232 mutex_lock(&pctldev->gpio_ranges_lock);
233 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
234 /* Check if we're in the valid range */
235 if (gpio >= range->base &&
236 gpio < range->base + range->npins) {
237 mutex_unlock(&pctldev->gpio_ranges_lock);
238 return range;
239 }
240 }
241 mutex_unlock(&pctldev->gpio_ranges_lock);
242
243 return NULL;
244}
245
246/**
247 * pinctrl_get_device_gpio_range() - find device for GPIO range
248 * @gpio: the pin to locate the pin controller for
249 * @outdev: the pin control device if found
250 * @outrange: the GPIO range if found
251 *
252 * Find the pin controller handling a certain GPIO pin from the pinspace of
253 * the GPIO subsystem, return the device and the matching GPIO range. Returns
254 * negative if the GPIO range could not be found in any device.
255 */
256int pinctrl_get_device_gpio_range(unsigned gpio,
257 struct pinctrl_dev **outdev,
258 struct pinctrl_gpio_range **outrange)
259{
260 struct pinctrl_dev *pctldev = NULL;
261
262 /* Loop over the pin controllers */
263 mutex_lock(&pinctrldev_list_mutex);
264 list_for_each_entry(pctldev, &pinctrldev_list, node) {
265 struct pinctrl_gpio_range *range;
266
267 range = pinctrl_match_gpio_range(pctldev, gpio);
268 if (range != NULL) {
269 *outdev = pctldev;
270 *outrange = range;
271 mutex_unlock(&pinctrldev_list_mutex);
272 return 0;
273 }
274 }
275 mutex_unlock(&pinctrldev_list_mutex);
276
277 return -EINVAL;
278}
279
280/**
281 * pinctrl_add_gpio_range() - register a GPIO range for a controller
282 * @pctldev: pin controller device to add the range to
283 * @range: the GPIO range to add
284 *
285 * This adds a range of GPIOs to be handled by a certain pin controller. Call
286 * this to register handled ranges after registering your pin controller.
287 */
288void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
289 struct pinctrl_gpio_range *range)
290{
291 mutex_lock(&pctldev->gpio_ranges_lock);
292 list_add(&range->node, &pctldev->gpio_ranges);
293 mutex_unlock(&pctldev->gpio_ranges_lock);
294}
295
296/**
297 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
298 * @pctldev: pin controller device to remove the range from
299 * @range: the GPIO range to remove
300 */
301void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
302 struct pinctrl_gpio_range *range)
303{
304 mutex_lock(&pctldev->gpio_ranges_lock);
305 list_del(&range->node);
306 mutex_unlock(&pctldev->gpio_ranges_lock);
307}
308
Linus Walleij7afde8b2011-10-19 17:07:16 +0200309/**
310 * pinctrl_get_group_selector() - returns the group selector for a group
311 * @pctldev: the pin controller handling the group
312 * @pin_group: the pin group to look up
313 */
314int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
315 const char *pin_group)
316{
317 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
318 unsigned group_selector = 0;
319
320 while (pctlops->list_groups(pctldev, group_selector) >= 0) {
321 const char *gname = pctlops->get_group_name(pctldev,
322 group_selector);
323 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700324 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200325 "found group selector %u for %s\n",
326 group_selector,
327 pin_group);
328 return group_selector;
329 }
330
331 group_selector++;
332 }
333
Stephen Warren51cd24e2011-12-09 16:59:05 -0700334 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200335 pin_group);
336
337 return -EINVAL;
338}
339
Linus Walleij2744e8a2011-05-02 20:50:54 +0200340#ifdef CONFIG_DEBUG_FS
341
342static int pinctrl_pins_show(struct seq_file *s, void *what)
343{
344 struct pinctrl_dev *pctldev = s->private;
345 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +0900346 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200347
348 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200349
Chanho Park706e8522012-01-03 16:47:50 +0900350 /* The pin number can be retrived from the pin controller descriptor */
351 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200352 struct pin_desc *desc;
353
Chanho Park706e8522012-01-03 16:47:50 +0900354 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200355 desc = pin_desc_get(pctldev, pin);
356 /* Pin space may be sparse */
357 if (desc == NULL)
358 continue;
359
360 seq_printf(s, "pin %d (%s) ", pin,
361 desc->name ? desc->name : "unnamed");
362
363 /* Driver-specific info per pin */
364 if (ops->pin_dbg_show)
365 ops->pin_dbg_show(pctldev, s, pin);
366
367 seq_puts(s, "\n");
368 }
369
370 return 0;
371}
372
373static int pinctrl_groups_show(struct seq_file *s, void *what)
374{
375 struct pinctrl_dev *pctldev = s->private;
376 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
377 unsigned selector = 0;
378
379 /* No grouping */
380 if (!ops)
381 return 0;
382
383 seq_puts(s, "registered pin groups:\n");
384 while (ops->list_groups(pctldev, selector) >= 0) {
Stephen Warrena5818a82011-10-19 16:19:25 -0600385 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200386 unsigned num_pins;
387 const char *gname = ops->get_group_name(pctldev, selector);
388 int ret;
389 int i;
390
391 ret = ops->get_group_pins(pctldev, selector,
392 &pins, &num_pins);
393 if (ret)
394 seq_printf(s, "%s [ERROR GETTING PINS]\n",
395 gname);
396 else {
397 seq_printf(s, "group: %s, pins = [ ", gname);
398 for (i = 0; i < num_pins; i++)
399 seq_printf(s, "%d ", pins[i]);
400 seq_puts(s, "]\n");
401 }
402 selector++;
403 }
404
405
406 return 0;
407}
408
409static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
410{
411 struct pinctrl_dev *pctldev = s->private;
412 struct pinctrl_gpio_range *range = NULL;
413
414 seq_puts(s, "GPIO ranges handled:\n");
415
416 /* Loop over the ranges */
417 mutex_lock(&pctldev->gpio_ranges_lock);
418 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Linus Walleij75d66422011-11-16 09:58:51 +0100419 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
420 range->id, range->name,
421 range->base, (range->base + range->npins - 1),
422 range->pin_base,
423 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +0200424 }
425 mutex_unlock(&pctldev->gpio_ranges_lock);
426
427 return 0;
428}
429
430static int pinctrl_devices_show(struct seq_file *s, void *what)
431{
432 struct pinctrl_dev *pctldev;
433
Linus Walleijae6b4d82011-10-19 18:14:33 +0200434 seq_puts(s, "name [pinmux] [pinconf]\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200435 mutex_lock(&pinctrldev_list_mutex);
436 list_for_each_entry(pctldev, &pinctrldev_list, node) {
437 seq_printf(s, "%s ", pctldev->desc->name);
438 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +0200439 seq_puts(s, "yes ");
440 else
441 seq_puts(s, "no ");
442 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200443 seq_puts(s, "yes");
444 else
445 seq_puts(s, "no");
446 seq_puts(s, "\n");
447 }
448 mutex_unlock(&pinctrldev_list_mutex);
449
450 return 0;
451}
452
453static int pinctrl_pins_open(struct inode *inode, struct file *file)
454{
455 return single_open(file, pinctrl_pins_show, inode->i_private);
456}
457
458static int pinctrl_groups_open(struct inode *inode, struct file *file)
459{
460 return single_open(file, pinctrl_groups_show, inode->i_private);
461}
462
463static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
464{
465 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
466}
467
468static int pinctrl_devices_open(struct inode *inode, struct file *file)
469{
470 return single_open(file, pinctrl_devices_show, NULL);
471}
472
473static const struct file_operations pinctrl_pins_ops = {
474 .open = pinctrl_pins_open,
475 .read = seq_read,
476 .llseek = seq_lseek,
477 .release = single_release,
478};
479
480static const struct file_operations pinctrl_groups_ops = {
481 .open = pinctrl_groups_open,
482 .read = seq_read,
483 .llseek = seq_lseek,
484 .release = single_release,
485};
486
487static const struct file_operations pinctrl_gpioranges_ops = {
488 .open = pinctrl_gpioranges_open,
489 .read = seq_read,
490 .llseek = seq_lseek,
491 .release = single_release,
492};
493
494static const struct file_operations pinctrl_devices_ops = {
495 .open = pinctrl_devices_open,
496 .read = seq_read,
497 .llseek = seq_lseek,
498 .release = single_release,
499};
500
501static struct dentry *debugfs_root;
502
503static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
504{
Tony Lindgren02157162012-01-20 08:17:22 -0800505 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200506
Stephen Warren51cd24e2011-12-09 16:59:05 -0700507 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +0200508 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -0800509 pctldev->device_root = device_root;
510
Linus Walleij2744e8a2011-05-02 20:50:54 +0200511 if (IS_ERR(device_root) || !device_root) {
512 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -0700513 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +0200514 return;
515 }
516 debugfs_create_file("pins", S_IFREG | S_IRUGO,
517 device_root, pctldev, &pinctrl_pins_ops);
518 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
519 device_root, pctldev, &pinctrl_groups_ops);
520 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
521 device_root, pctldev, &pinctrl_gpioranges_ops);
522 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200523 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200524}
525
Tony Lindgren02157162012-01-20 08:17:22 -0800526static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
527{
528 debugfs_remove_recursive(pctldev->device_root);
529}
530
Linus Walleij2744e8a2011-05-02 20:50:54 +0200531static void pinctrl_init_debugfs(void)
532{
533 debugfs_root = debugfs_create_dir("pinctrl", NULL);
534 if (IS_ERR(debugfs_root) || !debugfs_root) {
535 pr_warn("failed to create debugfs directory\n");
536 debugfs_root = NULL;
537 return;
538 }
539
540 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
541 debugfs_root, NULL, &pinctrl_devices_ops);
542 pinmux_init_debugfs(debugfs_root);
543}
544
545#else /* CONFIG_DEBUG_FS */
546
547static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
548{
549}
550
551static void pinctrl_init_debugfs(void)
552{
553}
554
Tony Lindgren02157162012-01-20 08:17:22 -0800555static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
556{
557}
558
Linus Walleij2744e8a2011-05-02 20:50:54 +0200559#endif
560
561/**
562 * pinctrl_register() - register a pin controller device
563 * @pctldesc: descriptor for this pin controller
564 * @dev: parent device for this pin controller
565 * @driver_data: private pin controller data for this pin controller
566 */
567struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
568 struct device *dev, void *driver_data)
569{
Linus Walleij2744e8a2011-05-02 20:50:54 +0200570 struct pinctrl_dev *pctldev;
571 int ret;
572
573 if (pctldesc == NULL)
574 return NULL;
575 if (pctldesc->name == NULL)
576 return NULL;
577
Linus Walleij2744e8a2011-05-02 20:50:54 +0200578 pctldev = kzalloc(sizeof(struct pinctrl_dev), GFP_KERNEL);
579 if (pctldev == NULL)
580 return NULL;
581
582 /* Initialize pin control device struct */
583 pctldev->owner = pctldesc->owner;
584 pctldev->desc = pctldesc;
585 pctldev->driver_data = driver_data;
586 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
587 spin_lock_init(&pctldev->pin_desc_tree_lock);
588 INIT_LIST_HEAD(&pctldev->gpio_ranges);
589 mutex_init(&pctldev->gpio_ranges_lock);
Stephen Warren51cd24e2011-12-09 16:59:05 -0700590 pctldev->dev = dev;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200591
Tony Lindgrenb9130b72012-01-24 16:28:08 -0800592 /* If we're implementing pinmuxing, check the ops for sanity */
593 if (pctldesc->pmxops) {
594 ret = pinmux_check_ops(pctldev);
595 if (ret) {
596 pr_err("%s pinmux ops lacks necessary functions\n",
597 pctldesc->name);
598 goto out_err;
599 }
600 }
601
602 /* If we're implementing pinconfig, check the ops for sanity */
603 if (pctldesc->confops) {
604 ret = pinconf_check_ops(pctldev);
605 if (ret) {
606 pr_err("%s pin config ops lacks necessary functions\n",
607 pctldesc->name);
608 goto out_err;
609 }
610 }
611
Linus Walleij2744e8a2011-05-02 20:50:54 +0200612 /* Register all the pins */
613 pr_debug("try to register %d pins on %s...\n",
614 pctldesc->npins, pctldesc->name);
615 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
616 if (ret) {
617 pr_err("error during pin registration\n");
618 pinctrl_free_pindescs(pctldev, pctldesc->pins,
619 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -0700620 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200621 }
622
623 pinctrl_init_device_debugfs(pctldev);
624 mutex_lock(&pinctrldev_list_mutex);
625 list_add(&pctldev->node, &pinctrldev_list);
626 mutex_unlock(&pinctrldev_list_mutex);
627 pinmux_hog_maps(pctldev);
628 return pctldev;
629
Stephen Warren51cd24e2011-12-09 16:59:05 -0700630out_err:
631 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200632 return NULL;
633}
634EXPORT_SYMBOL_GPL(pinctrl_register);
635
636/**
637 * pinctrl_unregister() - unregister pinmux
638 * @pctldev: pin controller to unregister
639 *
640 * Called by pinmux drivers to unregister a pinmux.
641 */
642void pinctrl_unregister(struct pinctrl_dev *pctldev)
643{
644 if (pctldev == NULL)
645 return;
646
Tony Lindgren02157162012-01-20 08:17:22 -0800647 pinctrl_remove_device_debugfs(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200648 pinmux_unhog_maps(pctldev);
649 /* TODO: check that no pinmuxes are still active? */
650 mutex_lock(&pinctrldev_list_mutex);
651 list_del(&pctldev->node);
652 mutex_unlock(&pinctrldev_list_mutex);
653 /* Destroy descriptor tree */
654 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
655 pctldev->desc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -0700656 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200657}
658EXPORT_SYMBOL_GPL(pinctrl_unregister);
659
660static int __init pinctrl_init(void)
661{
662 pr_info("initialized pinctrl subsystem\n");
663 pinctrl_init_debugfs();
664 return 0;
665}
666
667/* init early since many drivers really need to initialized pinmux early */
668core_initcall(pinctrl_init);