blob: 50bcc511193e27ee8a04be04cf80f5757a8b4058 [file] [log] [blame]
Linus Walleij2744e8a2011-05-02 20:50:54 +02001/*
2 * Core driver for the pin control subsystem
3 *
Linus Walleijbefe5bd2012-02-09 19:47:48 +01004 * Copyright (C) 2011-2012 ST-Ericsson SA
Linus Walleij2744e8a2011-05-02 20:50:54 +02005 * 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 *
Stephen Warrenb2b3e662012-02-19 23:45:43 -070010 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
11 *
Linus Walleij2744e8a2011-05-02 20:50:54 +020012 * License terms: GNU General Public License (GPL) version 2
13 */
14#define pr_fmt(fmt) "pinctrl core: " fmt
15
16#include <linux/kernel.h>
Stephen Rothwella5a697c2011-09-30 14:39:04 +100017#include <linux/export.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020018#include <linux/init.h>
19#include <linux/device.h>
20#include <linux/slab.h>
21#include <linux/radix-tree.h>
22#include <linux/err.h>
23#include <linux/list.h>
24#include <linux/mutex.h>
25#include <linux/spinlock.h>
26#include <linux/sysfs.h>
27#include <linux/debugfs.h>
28#include <linux/seq_file.h>
29#include <linux/pinctrl/pinctrl.h>
30#include <linux/pinctrl/machine.h>
31#include "core.h"
32#include "pinmux.h"
Linus Walleijae6b4d82011-10-19 18:14:33 +020033#include "pinconf.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020034
Linus Walleijbefe5bd2012-02-09 19:47:48 +010035/**
Stephen Warrenb2b3e662012-02-19 23:45:43 -070036 * struct pinctrl_maps - a list item containing part of the mapping table
37 * @node: mapping table list node
38 * @maps: array of mapping table entries
39 * @num_maps: the number of entries in @maps
40 */
41struct pinctrl_maps {
42 struct list_head node;
43 struct pinctrl_map const *maps;
44 unsigned num_maps;
45};
46
47/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +010048 * struct pinctrl_hog - a list item to stash control hogs
49 * @node: pin control hog list node
50 * @map: map entry responsible for this hogging
51 * @pmx: the pin control hogged by this item
52 */
53struct pinctrl_hog {
54 struct list_head node;
55 struct pinctrl_map const *map;
56 struct pinctrl *p;
57};
58
Linus Walleij2744e8a2011-05-02 20:50:54 +020059/* Global list of pin control devices */
60static DEFINE_MUTEX(pinctrldev_list_mutex);
61static LIST_HEAD(pinctrldev_list);
62
Linus Walleijbefe5bd2012-02-09 19:47:48 +010063/* List of pin controller handles */
64static DEFINE_MUTEX(pinctrl_list_mutex);
65static LIST_HEAD(pinctrl_list);
66
67/* Global pinctrl maps */
Stephen Warrenb2b3e662012-02-19 23:45:43 -070068static DEFINE_MUTEX(pinctrl_maps_mutex);
69static LIST_HEAD(pinctrl_maps);
70
71#define for_each_maps(_maps_node_, _i_, _map_) \
72 list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
73 for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
74 _i_ < _maps_node_->num_maps; \
75 i++, _map_ = &_maps_node_->maps[_i_])
Linus Walleijbefe5bd2012-02-09 19:47:48 +010076
Linus Walleij2744e8a2011-05-02 20:50:54 +020077const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
78{
79 /* We're not allowed to register devices without name */
80 return pctldev->desc->name;
81}
82EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
83
84void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
85{
86 return pctldev->driver_data;
87}
88EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
89
90/**
Linus Walleij9dfac4f2012-02-01 18:02:47 +010091 * get_pinctrl_dev_from_devname() - look up pin controller device
92 * @devname: the name of a device instance, as returned by dev_name()
Linus Walleij2744e8a2011-05-02 20:50:54 +020093 *
94 * Looks up a pin control device matching a certain device name or pure device
95 * pointer, the pure device pointer will take precedence.
96 */
Linus Walleij9dfac4f2012-02-01 18:02:47 +010097struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
Linus Walleij2744e8a2011-05-02 20:50:54 +020098{
99 struct pinctrl_dev *pctldev = NULL;
100 bool found = false;
101
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100102 if (!devname)
103 return NULL;
104
Linus Walleij2744e8a2011-05-02 20:50:54 +0200105 mutex_lock(&pinctrldev_list_mutex);
106 list_for_each_entry(pctldev, &pinctrldev_list, node) {
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100107 if (!strcmp(dev_name(pctldev->dev), devname)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200108 /* Matched on device name */
109 found = true;
110 break;
111 }
112 }
113 mutex_unlock(&pinctrldev_list_mutex);
114
115 return found ? pctldev : NULL;
116}
117
Marek Belisko33d58942011-10-31 21:27:52 +0100118struct pin_desc *pin_desc_get(struct pinctrl_dev *pctldev, unsigned int pin)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200119{
120 struct pin_desc *pindesc;
121 unsigned long flags;
122
123 spin_lock_irqsave(&pctldev->pin_desc_tree_lock, flags);
124 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree, pin);
125 spin_unlock_irqrestore(&pctldev->pin_desc_tree_lock, flags);
126
127 return pindesc;
128}
129
130/**
Linus Walleijae6b4d82011-10-19 18:14:33 +0200131 * pin_get_from_name() - look up a pin number from a name
132 * @pctldev: the pin control device to lookup the pin on
133 * @name: the name of the pin to look up
134 */
135int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
136{
Chanho Park706e8522012-01-03 16:47:50 +0900137 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200138
Chanho Park706e8522012-01-03 16:47:50 +0900139 /* The pin number can be retrived from the pin controller descriptor */
140 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200141 struct pin_desc *desc;
142
Chanho Park706e8522012-01-03 16:47:50 +0900143 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200144 desc = pin_desc_get(pctldev, pin);
145 /* Pin space may be sparse */
146 if (desc == NULL)
147 continue;
148 if (desc->name && !strcmp(name, desc->name))
149 return pin;
150 }
151
152 return -EINVAL;
153}
154
155/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200156 * pin_is_valid() - check if pin exists on controller
157 * @pctldev: the pin control device to check the pin on
158 * @pin: pin to check, use the local pin controller index number
159 *
160 * This tells us whether a certain pin exist on a certain pin controller or
161 * not. Pin lists may be sparse, so some pins may not exist.
162 */
163bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
164{
165 struct pin_desc *pindesc;
166
167 if (pin < 0)
168 return false;
169
170 pindesc = pin_desc_get(pctldev, pin);
171 if (pindesc == NULL)
172 return false;
173
174 return true;
175}
176EXPORT_SYMBOL_GPL(pin_is_valid);
177
178/* Deletes a range of pin descriptors */
179static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
180 const struct pinctrl_pin_desc *pins,
181 unsigned num_pins)
182{
183 int i;
184
185 spin_lock(&pctldev->pin_desc_tree_lock);
186 for (i = 0; i < num_pins; i++) {
187 struct pin_desc *pindesc;
188
189 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
190 pins[i].number);
191 if (pindesc != NULL) {
192 radix_tree_delete(&pctldev->pin_desc_tree,
193 pins[i].number);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100194 if (pindesc->dynamic_name)
195 kfree(pindesc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200196 }
197 kfree(pindesc);
198 }
199 spin_unlock(&pctldev->pin_desc_tree_lock);
200}
201
202static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
203 unsigned number, const char *name)
204{
205 struct pin_desc *pindesc;
206
207 pindesc = pin_desc_get(pctldev, number);
208 if (pindesc != NULL) {
209 pr_err("pin %d already registered on %s\n", number,
210 pctldev->desc->name);
211 return -EINVAL;
212 }
213
214 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
215 if (pindesc == NULL)
216 return -ENOMEM;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200217
Linus Walleij2744e8a2011-05-02 20:50:54 +0200218 spin_lock_init(&pindesc->lock);
219
220 /* Set owner */
221 pindesc->pctldev = pctldev;
222
Stephen Warren9af1e442011-10-19 16:19:27 -0600223 /* Copy basic pin info */
Linus Walleij8dc6ae42012-02-01 18:11:40 +0100224 if (name) {
Linus Walleijca53c5f2011-12-14 20:33:37 +0100225 pindesc->name = name;
226 } else {
227 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
228 if (pindesc->name == NULL)
229 return -ENOMEM;
230 pindesc->dynamic_name = true;
231 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200232
233 spin_lock(&pctldev->pin_desc_tree_lock);
234 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
235 spin_unlock(&pctldev->pin_desc_tree_lock);
236 pr_debug("registered pin %d (%s) on %s\n",
Linus Walleijca53c5f2011-12-14 20:33:37 +0100237 number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200238 return 0;
239}
240
241static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
242 struct pinctrl_pin_desc const *pins,
243 unsigned num_descs)
244{
245 unsigned i;
246 int ret = 0;
247
248 for (i = 0; i < num_descs; i++) {
249 ret = pinctrl_register_one_pin(pctldev,
250 pins[i].number, pins[i].name);
251 if (ret)
252 return ret;
253 }
254
255 return 0;
256}
257
258/**
259 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
260 * @pctldev: pin controller device to check
261 * @gpio: gpio pin to check taken from the global GPIO pin space
262 *
263 * Tries to match a GPIO pin number to the ranges handled by a certain pin
264 * controller, return the range or NULL
265 */
266static struct pinctrl_gpio_range *
267pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
268{
269 struct pinctrl_gpio_range *range = NULL;
270
271 /* Loop over the ranges */
272 mutex_lock(&pctldev->gpio_ranges_lock);
273 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
274 /* Check if we're in the valid range */
275 if (gpio >= range->base &&
276 gpio < range->base + range->npins) {
277 mutex_unlock(&pctldev->gpio_ranges_lock);
278 return range;
279 }
280 }
281 mutex_unlock(&pctldev->gpio_ranges_lock);
282
283 return NULL;
284}
285
286/**
287 * pinctrl_get_device_gpio_range() - find device for GPIO range
288 * @gpio: the pin to locate the pin controller for
289 * @outdev: the pin control device if found
290 * @outrange: the GPIO range if found
291 *
292 * Find the pin controller handling a certain GPIO pin from the pinspace of
293 * the GPIO subsystem, return the device and the matching GPIO range. Returns
294 * negative if the GPIO range could not be found in any device.
295 */
296int pinctrl_get_device_gpio_range(unsigned gpio,
297 struct pinctrl_dev **outdev,
298 struct pinctrl_gpio_range **outrange)
299{
300 struct pinctrl_dev *pctldev = NULL;
301
302 /* Loop over the pin controllers */
303 mutex_lock(&pinctrldev_list_mutex);
304 list_for_each_entry(pctldev, &pinctrldev_list, node) {
305 struct pinctrl_gpio_range *range;
306
307 range = pinctrl_match_gpio_range(pctldev, gpio);
308 if (range != NULL) {
309 *outdev = pctldev;
310 *outrange = range;
311 mutex_unlock(&pinctrldev_list_mutex);
312 return 0;
313 }
314 }
315 mutex_unlock(&pinctrldev_list_mutex);
316
317 return -EINVAL;
318}
319
320/**
321 * pinctrl_add_gpio_range() - register a GPIO range for a controller
322 * @pctldev: pin controller device to add the range to
323 * @range: the GPIO range to add
324 *
325 * This adds a range of GPIOs to be handled by a certain pin controller. Call
326 * this to register handled ranges after registering your pin controller.
327 */
328void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
329 struct pinctrl_gpio_range *range)
330{
331 mutex_lock(&pctldev->gpio_ranges_lock);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700332 list_add_tail(&range->node, &pctldev->gpio_ranges);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200333 mutex_unlock(&pctldev->gpio_ranges_lock);
334}
335
336/**
337 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
338 * @pctldev: pin controller device to remove the range from
339 * @range: the GPIO range to remove
340 */
341void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
342 struct pinctrl_gpio_range *range)
343{
344 mutex_lock(&pctldev->gpio_ranges_lock);
345 list_del(&range->node);
346 mutex_unlock(&pctldev->gpio_ranges_lock);
347}
348
Linus Walleij7afde8b2011-10-19 17:07:16 +0200349/**
350 * pinctrl_get_group_selector() - returns the group selector for a group
351 * @pctldev: the pin controller handling the group
352 * @pin_group: the pin group to look up
353 */
354int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
355 const char *pin_group)
356{
357 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
358 unsigned group_selector = 0;
359
360 while (pctlops->list_groups(pctldev, group_selector) >= 0) {
361 const char *gname = pctlops->get_group_name(pctldev,
362 group_selector);
363 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700364 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200365 "found group selector %u for %s\n",
366 group_selector,
367 pin_group);
368 return group_selector;
369 }
370
371 group_selector++;
372 }
373
Stephen Warren51cd24e2011-12-09 16:59:05 -0700374 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200375 pin_group);
376
377 return -EINVAL;
378}
379
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100380/**
381 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
382 * @gpio: the GPIO pin number from the GPIO subsystem number space
383 *
384 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
385 * as part of their gpio_request() semantics, platforms and individual drivers
386 * shall *NOT* request GPIO pins to be muxed in.
387 */
388int pinctrl_request_gpio(unsigned gpio)
389{
390 struct pinctrl_dev *pctldev;
391 struct pinctrl_gpio_range *range;
392 int ret;
393 int pin;
394
395 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
396 if (ret)
397 return -EINVAL;
398
399 /* Convert to the pin controllers number space */
400 pin = gpio - range->base + range->pin_base;
401
402 return pinmux_request_gpio(pctldev, range, pin, gpio);
403}
404EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
405
406/**
407 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
408 * @gpio: the GPIO pin number from the GPIO subsystem number space
409 *
410 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
411 * as part of their gpio_free() semantics, platforms and individual drivers
412 * shall *NOT* request GPIO pins to be muxed out.
413 */
414void pinctrl_free_gpio(unsigned gpio)
415{
416 struct pinctrl_dev *pctldev;
417 struct pinctrl_gpio_range *range;
418 int ret;
419 int pin;
420
421 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
422 if (ret)
423 return;
424
425 /* Convert to the pin controllers number space */
426 pin = gpio - range->base + range->pin_base;
427
428 return pinmux_free_gpio(pctldev, pin, range);
429}
430EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
431
432static int pinctrl_gpio_direction(unsigned gpio, bool input)
433{
434 struct pinctrl_dev *pctldev;
435 struct pinctrl_gpio_range *range;
436 int ret;
437 int pin;
438
439 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
440 if (ret)
441 return ret;
442
443 /* Convert to the pin controllers number space */
444 pin = gpio - range->base + range->pin_base;
445
446 return pinmux_gpio_direction(pctldev, range, pin, input);
447}
448
449/**
450 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
451 * @gpio: the GPIO pin number from the GPIO subsystem number space
452 *
453 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
454 * as part of their gpio_direction_input() semantics, platforms and individual
455 * drivers shall *NOT* touch pin control GPIO calls.
456 */
457int pinctrl_gpio_direction_input(unsigned gpio)
458{
459 return pinctrl_gpio_direction(gpio, true);
460}
461EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
462
463/**
464 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
465 * @gpio: the GPIO pin number from the GPIO subsystem number space
466 *
467 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
468 * as part of their gpio_direction_output() semantics, platforms and individual
469 * drivers shall *NOT* touch pin control GPIO calls.
470 */
471int pinctrl_gpio_direction_output(unsigned gpio)
472{
473 return pinctrl_gpio_direction(gpio, false);
474}
475EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
476
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700477static struct pinctrl *pinctrl_get_locked(struct device *dev, const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100478{
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100479 struct pinctrl_dev *pctldev = NULL;
480 const char *devname = NULL;
481 struct pinctrl *p;
482 bool found_map;
483 unsigned num_maps = 0;
484 int ret = -ENODEV;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700485 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100486 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700487 struct pinctrl_map const *map;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100488
489 /* We must have dev or ID or both */
490 if (!dev && !name)
491 return ERR_PTR(-EINVAL);
492
493 if (dev)
494 devname = dev_name(dev);
495
496 pr_debug("get pin control handle %s for device %s\n", name,
497 devname ? devname : "(none)");
498
499 /*
500 * create the state cookie holder struct pinctrl for each
501 * mapping, this is what consumers will get when requesting
502 * a pin control handle with pinctrl_get()
503 */
504 p = kzalloc(sizeof(struct pinctrl), GFP_KERNEL);
505 if (p == NULL)
506 return ERR_PTR(-ENOMEM);
507 mutex_init(&p->mutex);
508 pinmux_init_pinctrl_handle(p);
509
510 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700511 for_each_maps(maps_node, i, map) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100512 found_map = false;
513
514 /*
515 * First, try to find the pctldev given in the map
516 */
517 pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
518 if (!pctldev) {
519 pr_warning("could not find a pinctrl device for pinmux function %s, fishy, they shall all have one\n",
520 map->function);
521 pr_warning("given pinctrl device name: %s",
522 map->ctrl_dev_name);
523
524 /* Continue to check the other mappings anyway... */
525 continue;
526 }
527
528 pr_debug("in map, found pctldev %s to handle function %s",
529 dev_name(pctldev->dev), map->function);
530
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100531 /*
532 * If we're looking for a specific named map, this must match,
533 * else we loop and look for the next.
534 */
535 if (name != NULL) {
536 if (map->name == NULL)
537 continue;
538 if (strcmp(map->name, name))
539 continue;
540 }
541
542 /*
543 * This is for the case where no device name is given, we
544 * already know that the function name matches from above
545 * code.
546 */
547 if (!map->dev_name && (name != NULL))
548 found_map = true;
549
550 /* If the mapping has a device set up it must match */
551 if (map->dev_name &&
552 (!devname || !strcmp(map->dev_name, devname)))
553 /* MATCH! */
554 found_map = true;
555
556 /* If this map is applicable, then apply it */
557 if (found_map) {
558 ret = pinmux_apply_muxmap(pctldev, p, dev,
559 devname, map);
560 if (ret) {
561 kfree(p);
562 return ERR_PTR(ret);
563 }
564 num_maps++;
565 }
566 }
567
568 /* We should have atleast one map, right */
569 if (!num_maps) {
570 pr_err("could not find any mux maps for device %s, ID %s\n",
571 devname ? devname : "(anonymous)",
572 name ? name : "(undefined)");
573 kfree(p);
574 return ERR_PTR(-EINVAL);
575 }
576
577 pr_debug("found %u mux maps for device %s, UD %s\n",
578 num_maps,
579 devname ? devname : "(anonymous)",
580 name ? name : "(undefined)");
581
582 /* Add the pinmux to the global list */
583 mutex_lock(&pinctrl_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700584 list_add_tail(&p->node, &pinctrl_list);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100585 mutex_unlock(&pinctrl_list_mutex);
586
587 return p;
588}
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700589
590/**
591 * pinctrl_get() - retrieves the pin controller handle for a certain device
592 * @dev: the device to get the pin controller handle for
593 * @name: an optional specific control mapping name or NULL, the name is only
594 * needed if you want to have more than one mapping per device, or if you
595 * need an anonymous pin control (not tied to any specific device)
596 */
597struct pinctrl *pinctrl_get(struct device *dev, const char *name)
598{
599 struct pinctrl *p;
600
601 mutex_lock(&pinctrl_maps_mutex);
602 p = pinctrl_get_locked(dev, name);
603 mutex_unlock(&pinctrl_maps_mutex);
604
605 return p;
606}
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100607EXPORT_SYMBOL_GPL(pinctrl_get);
608
609/**
610 * pinctrl_put() - release a previously claimed pin control handle
611 * @p: a pin control handle previously claimed by pinctrl_get()
612 */
613void pinctrl_put(struct pinctrl *p)
614{
615 if (p == NULL)
616 return;
617
618 mutex_lock(&p->mutex);
619 if (p->usecount)
620 pr_warn("releasing pin control handle with active users!\n");
621 /* Free the groups and all acquired pins */
622 pinmux_put(p);
623 mutex_unlock(&p->mutex);
624
625 /* Remove from list */
626 mutex_lock(&pinctrl_list_mutex);
627 list_del(&p->node);
628 mutex_unlock(&pinctrl_list_mutex);
629
630 kfree(p);
631}
632EXPORT_SYMBOL_GPL(pinctrl_put);
633
634/**
635 * pinctrl_enable() - enable a certain pin controller setting
636 * @p: the pin control handle to enable, previously claimed by pinctrl_get()
637 */
638int pinctrl_enable(struct pinctrl *p)
639{
640 int ret = 0;
641
642 if (p == NULL)
643 return -EINVAL;
644 mutex_lock(&p->mutex);
645 if (p->usecount++ == 0) {
646 ret = pinmux_enable(p);
647 if (ret)
648 p->usecount--;
649 }
650 mutex_unlock(&p->mutex);
651 return ret;
652}
653EXPORT_SYMBOL_GPL(pinctrl_enable);
654
655/**
656 * pinctrl_disable() - disable a certain pin control setting
657 * @p: the pin control handle to disable, previously claimed by pinctrl_get()
658 */
659void pinctrl_disable(struct pinctrl *p)
660{
661 if (p == NULL)
662 return;
663
664 mutex_lock(&p->mutex);
665 if (--p->usecount == 0) {
666 pinmux_disable(p);
667 }
668 mutex_unlock(&p->mutex);
669}
670EXPORT_SYMBOL_GPL(pinctrl_disable);
671
672/**
673 * pinctrl_register_mappings() - register a set of pin controller mappings
Stephen Warren13398a42012-02-19 23:45:41 -0700674 * @maps: the pincontrol mappings table to register. This should probably be
675 * marked with __initdata so it can be discarded after boot. This
676 * function will perform a shallow copy for the mapping entries.
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100677 * @num_maps: the number of maps in the mapping table
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100678 */
Stephen Warren13398a42012-02-19 23:45:41 -0700679int pinctrl_register_mappings(struct pinctrl_map const *maps,
680 unsigned num_maps)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100681{
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100682 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700683 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100684
685 pr_debug("add %d pinmux maps\n", num_maps);
686
687 /* First sanity check the new mapping */
688 for (i = 0; i < num_maps; i++) {
689 if (!maps[i].name) {
690 pr_err("failed to register map %d: no map name given\n",
691 i);
692 return -EINVAL;
693 }
694
695 if (!maps[i].ctrl_dev_name) {
696 pr_err("failed to register map %s (%d): no pin control device given\n",
697 maps[i].name, i);
698 return -EINVAL;
699 }
700
701 if (!maps[i].function) {
702 pr_err("failed to register map %s (%d): no function ID given\n",
703 maps[i].name, i);
704 return -EINVAL;
705 }
706
707 if (!maps[i].dev_name)
708 pr_debug("add system map %s function %s with no device\n",
709 maps[i].name,
710 maps[i].function);
711 else
712 pr_debug("register map %s, function %s\n",
713 maps[i].name,
714 maps[i].function);
715 }
716
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700717 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
718 if (!maps_node) {
719 pr_err("failed to alloc struct pinctrl_maps\n");
720 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100721 }
722
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700723 maps_node->num_maps = num_maps;
724 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps, GFP_KERNEL);
725 if (!maps_node->maps) {
726 kfree(maps_node);
727 return -ENOMEM;
728 }
729
730 mutex_lock(&pinctrl_maps_mutex);
731 list_add_tail(&maps_node->node, &pinctrl_maps);
732 mutex_unlock(&pinctrl_maps_mutex);
733
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100734 return 0;
735}
736
737/* Hog a single map entry and add to the hoglist */
738static int pinctrl_hog_map(struct pinctrl_dev *pctldev,
739 struct pinctrl_map const *map)
740{
741 struct pinctrl_hog *hog;
742 struct pinctrl *p;
743 int ret;
744
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100745 hog = kzalloc(sizeof(struct pinctrl_hog), GFP_KERNEL);
746 if (!hog)
747 return -ENOMEM;
748
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700749 p = pinctrl_get_locked(pctldev->dev, map->name);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100750 if (IS_ERR(p)) {
751 kfree(hog);
752 dev_err(pctldev->dev,
753 "could not get the %s pin control mapping for hogging\n",
754 map->name);
755 return PTR_ERR(p);
756 }
757
758 ret = pinctrl_enable(p);
759 if (ret) {
760 pinctrl_put(p);
761 kfree(hog);
762 dev_err(pctldev->dev,
763 "could not enable the %s pin control mapping for hogging\n",
764 map->name);
765 return ret;
766 }
767
768 hog->map = map;
769 hog->p = p;
770
771 dev_info(pctldev->dev, "hogged map %s, function %s\n", map->name,
772 map->function);
773 mutex_lock(&pctldev->pinctrl_hogs_lock);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700774 list_add_tail(&hog->node, &pctldev->pinctrl_hogs);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100775 mutex_unlock(&pctldev->pinctrl_hogs_lock);
776
777 return 0;
778}
779
780/**
781 * pinctrl_hog_maps() - hog specific map entries on controller device
782 * @pctldev: the pin control device to hog entries on
783 *
784 * When the pin controllers are registered, there may be some specific pinmux
785 * map entries that need to be hogged, i.e. get+enabled until the system shuts
786 * down.
787 */
788int pinctrl_hog_maps(struct pinctrl_dev *pctldev)
789{
790 struct device *dev = pctldev->dev;
791 const char *devname = dev_name(dev);
792 int ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700793 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100794 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700795 struct pinctrl_map const *map;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100796
797 INIT_LIST_HEAD(&pctldev->pinctrl_hogs);
798 mutex_init(&pctldev->pinctrl_hogs_lock);
799
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700800 mutex_lock(&pinctrl_maps_mutex);
801 for_each_maps(maps_node, i, map) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100802 if (map->ctrl_dev_name &&
Linus Walleij77a59882012-02-10 01:34:12 +0100803 !strcmp(map->ctrl_dev_name, devname) &&
804 !strcmp(map->dev_name, devname)) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100805 /* OK time to hog! */
806 ret = pinctrl_hog_map(pctldev, map);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700807 if (ret) {
808 mutex_unlock(&pinctrl_maps_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100809 return ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700810 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100811 }
812 }
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700813 mutex_unlock(&pinctrl_maps_mutex);
814
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100815 return 0;
816}
817
818/**
819 * pinctrl_unhog_maps() - unhog specific map entries on controller device
820 * @pctldev: the pin control device to unhog entries on
821 */
822void pinctrl_unhog_maps(struct pinctrl_dev *pctldev)
823{
824 struct list_head *node, *tmp;
825
826 mutex_lock(&pctldev->pinctrl_hogs_lock);
827 list_for_each_safe(node, tmp, &pctldev->pinctrl_hogs) {
828 struct pinctrl_hog *hog =
829 list_entry(node, struct pinctrl_hog, node);
830 pinctrl_disable(hog->p);
831 pinctrl_put(hog->p);
832 list_del(node);
833 kfree(hog);
834 }
835 mutex_unlock(&pctldev->pinctrl_hogs_lock);
836}
837
Linus Walleij2744e8a2011-05-02 20:50:54 +0200838#ifdef CONFIG_DEBUG_FS
839
840static int pinctrl_pins_show(struct seq_file *s, void *what)
841{
842 struct pinctrl_dev *pctldev = s->private;
843 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +0900844 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200845
846 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200847
Chanho Park706e8522012-01-03 16:47:50 +0900848 /* The pin number can be retrived from the pin controller descriptor */
849 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200850 struct pin_desc *desc;
851
Chanho Park706e8522012-01-03 16:47:50 +0900852 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200853 desc = pin_desc_get(pctldev, pin);
854 /* Pin space may be sparse */
855 if (desc == NULL)
856 continue;
857
858 seq_printf(s, "pin %d (%s) ", pin,
859 desc->name ? desc->name : "unnamed");
860
861 /* Driver-specific info per pin */
862 if (ops->pin_dbg_show)
863 ops->pin_dbg_show(pctldev, s, pin);
864
865 seq_puts(s, "\n");
866 }
867
868 return 0;
869}
870
871static int pinctrl_groups_show(struct seq_file *s, void *what)
872{
873 struct pinctrl_dev *pctldev = s->private;
874 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
875 unsigned selector = 0;
876
877 /* No grouping */
878 if (!ops)
879 return 0;
880
881 seq_puts(s, "registered pin groups:\n");
882 while (ops->list_groups(pctldev, selector) >= 0) {
Stephen Warrena5818a82011-10-19 16:19:25 -0600883 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200884 unsigned num_pins;
885 const char *gname = ops->get_group_name(pctldev, selector);
886 int ret;
887 int i;
888
889 ret = ops->get_group_pins(pctldev, selector,
890 &pins, &num_pins);
891 if (ret)
892 seq_printf(s, "%s [ERROR GETTING PINS]\n",
893 gname);
894 else {
895 seq_printf(s, "group: %s, pins = [ ", gname);
896 for (i = 0; i < num_pins; i++)
897 seq_printf(s, "%d ", pins[i]);
898 seq_puts(s, "]\n");
899 }
900 selector++;
901 }
902
903
904 return 0;
905}
906
907static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
908{
909 struct pinctrl_dev *pctldev = s->private;
910 struct pinctrl_gpio_range *range = NULL;
911
912 seq_puts(s, "GPIO ranges handled:\n");
913
914 /* Loop over the ranges */
915 mutex_lock(&pctldev->gpio_ranges_lock);
916 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Linus Walleij75d66422011-11-16 09:58:51 +0100917 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
918 range->id, range->name,
919 range->base, (range->base + range->npins - 1),
920 range->pin_base,
921 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +0200922 }
923 mutex_unlock(&pctldev->gpio_ranges_lock);
924
925 return 0;
926}
927
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100928static int pinctrl_maps_show(struct seq_file *s, void *what)
929{
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700930 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100931 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700932 struct pinctrl_map const *map;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100933
934 seq_puts(s, "Pinctrl maps:\n");
935
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700936 mutex_lock(&pinctrl_maps_mutex);
937 for_each_maps(maps_node, i, map) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100938 seq_printf(s, "%s:\n", map->name);
939 if (map->dev_name)
940 seq_printf(s, " device: %s\n",
941 map->dev_name);
942 else
943 seq_printf(s, " SYSTEM MUX\n");
944 seq_printf(s, " controlling device %s\n",
945 map->ctrl_dev_name);
946 seq_printf(s, " function: %s\n", map->function);
947 seq_printf(s, " group: %s\n", map->group ? map->group :
948 "(default)");
949 }
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700950 mutex_unlock(&pinctrl_maps_mutex);
951
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100952 return 0;
953}
954
955static int pinmux_hogs_show(struct seq_file *s, void *what)
956{
957 struct pinctrl_dev *pctldev = s->private;
958 struct pinctrl_hog *hog;
959
960 seq_puts(s, "Pin control map hogs held by device\n");
961
962 list_for_each_entry(hog, &pctldev->pinctrl_hogs, node)
963 seq_printf(s, "%s\n", hog->map->name);
964
965 return 0;
966}
967
Linus Walleij2744e8a2011-05-02 20:50:54 +0200968static int pinctrl_devices_show(struct seq_file *s, void *what)
969{
970 struct pinctrl_dev *pctldev;
971
Linus Walleijae6b4d82011-10-19 18:14:33 +0200972 seq_puts(s, "name [pinmux] [pinconf]\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200973 mutex_lock(&pinctrldev_list_mutex);
974 list_for_each_entry(pctldev, &pinctrldev_list, node) {
975 seq_printf(s, "%s ", pctldev->desc->name);
976 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +0200977 seq_puts(s, "yes ");
978 else
979 seq_puts(s, "no ");
980 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200981 seq_puts(s, "yes");
982 else
983 seq_puts(s, "no");
984 seq_puts(s, "\n");
985 }
986 mutex_unlock(&pinctrldev_list_mutex);
987
988 return 0;
989}
990
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100991static int pinctrl_show(struct seq_file *s, void *what)
992{
993 struct pinctrl *p;
994
995 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
996 list_for_each_entry(p, &pinctrl_list, node) {
997 struct pinctrl_dev *pctldev = p->pctldev;
998
999 if (!pctldev) {
1000 seq_puts(s, "NO PIN CONTROLLER DEVICE\n");
1001 continue;
1002 }
1003
1004 seq_printf(s, "device: %s",
1005 pinctrl_dev_get_name(p->pctldev));
1006
1007 pinmux_dbg_show(s, p);
1008
1009 seq_printf(s, " users: %u map-> %s\n",
1010 p->usecount,
1011 p->dev ? dev_name(p->dev) : "(system)");
1012 }
1013
1014 return 0;
1015}
1016
Linus Walleij2744e8a2011-05-02 20:50:54 +02001017static int pinctrl_pins_open(struct inode *inode, struct file *file)
1018{
1019 return single_open(file, pinctrl_pins_show, inode->i_private);
1020}
1021
1022static int pinctrl_groups_open(struct inode *inode, struct file *file)
1023{
1024 return single_open(file, pinctrl_groups_show, inode->i_private);
1025}
1026
1027static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1028{
1029 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1030}
1031
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001032static int pinctrl_maps_open(struct inode *inode, struct file *file)
1033{
1034 return single_open(file, pinctrl_maps_show, inode->i_private);
1035}
1036
1037static int pinmux_hogs_open(struct inode *inode, struct file *file)
1038{
1039 return single_open(file, pinmux_hogs_show, inode->i_private);
1040}
1041
Linus Walleij2744e8a2011-05-02 20:50:54 +02001042static int pinctrl_devices_open(struct inode *inode, struct file *file)
1043{
1044 return single_open(file, pinctrl_devices_show, NULL);
1045}
1046
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001047static int pinctrl_open(struct inode *inode, struct file *file)
1048{
1049 return single_open(file, pinctrl_show, NULL);
1050}
1051
Linus Walleij2744e8a2011-05-02 20:50:54 +02001052static const struct file_operations pinctrl_pins_ops = {
1053 .open = pinctrl_pins_open,
1054 .read = seq_read,
1055 .llseek = seq_lseek,
1056 .release = single_release,
1057};
1058
1059static const struct file_operations pinctrl_groups_ops = {
1060 .open = pinctrl_groups_open,
1061 .read = seq_read,
1062 .llseek = seq_lseek,
1063 .release = single_release,
1064};
1065
1066static const struct file_operations pinctrl_gpioranges_ops = {
1067 .open = pinctrl_gpioranges_open,
1068 .read = seq_read,
1069 .llseek = seq_lseek,
1070 .release = single_release,
1071};
1072
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001073static const struct file_operations pinctrl_maps_ops = {
1074 .open = pinctrl_maps_open,
1075 .read = seq_read,
1076 .llseek = seq_lseek,
1077 .release = single_release,
1078};
1079
1080static const struct file_operations pinmux_hogs_ops = {
1081 .open = pinmux_hogs_open,
1082 .read = seq_read,
1083 .llseek = seq_lseek,
1084 .release = single_release,
1085};
1086
Linus Walleij2744e8a2011-05-02 20:50:54 +02001087static const struct file_operations pinctrl_devices_ops = {
1088 .open = pinctrl_devices_open,
1089 .read = seq_read,
1090 .llseek = seq_lseek,
1091 .release = single_release,
1092};
1093
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001094static const struct file_operations pinctrl_ops = {
1095 .open = pinctrl_open,
1096 .read = seq_read,
1097 .llseek = seq_lseek,
1098 .release = single_release,
1099};
1100
Linus Walleij2744e8a2011-05-02 20:50:54 +02001101static struct dentry *debugfs_root;
1102
1103static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1104{
Tony Lindgren02157162012-01-20 08:17:22 -08001105 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001106
Stephen Warren51cd24e2011-12-09 16:59:05 -07001107 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001108 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001109 pctldev->device_root = device_root;
1110
Linus Walleij2744e8a2011-05-02 20:50:54 +02001111 if (IS_ERR(device_root) || !device_root) {
1112 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001113 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001114 return;
1115 }
1116 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1117 device_root, pctldev, &pinctrl_pins_ops);
1118 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1119 device_root, pctldev, &pinctrl_groups_ops);
1120 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1121 device_root, pctldev, &pinctrl_gpioranges_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001122 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1123 device_root, pctldev, &pinctrl_maps_ops);
1124 debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO,
1125 device_root, pctldev, &pinmux_hogs_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001126 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +02001127 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001128}
1129
Tony Lindgren02157162012-01-20 08:17:22 -08001130static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1131{
1132 debugfs_remove_recursive(pctldev->device_root);
1133}
1134
Linus Walleij2744e8a2011-05-02 20:50:54 +02001135static void pinctrl_init_debugfs(void)
1136{
1137 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1138 if (IS_ERR(debugfs_root) || !debugfs_root) {
1139 pr_warn("failed to create debugfs directory\n");
1140 debugfs_root = NULL;
1141 return;
1142 }
1143
1144 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1145 debugfs_root, NULL, &pinctrl_devices_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001146 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1147 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001148}
1149
1150#else /* CONFIG_DEBUG_FS */
1151
1152static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1153{
1154}
1155
1156static void pinctrl_init_debugfs(void)
1157{
1158}
1159
Tony Lindgren02157162012-01-20 08:17:22 -08001160static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1161{
1162}
1163
Linus Walleij2744e8a2011-05-02 20:50:54 +02001164#endif
1165
1166/**
1167 * pinctrl_register() - register a pin controller device
1168 * @pctldesc: descriptor for this pin controller
1169 * @dev: parent device for this pin controller
1170 * @driver_data: private pin controller data for this pin controller
1171 */
1172struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1173 struct device *dev, void *driver_data)
1174{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001175 struct pinctrl_dev *pctldev;
1176 int ret;
1177
1178 if (pctldesc == NULL)
1179 return NULL;
1180 if (pctldesc->name == NULL)
1181 return NULL;
1182
Linus Walleij2744e8a2011-05-02 20:50:54 +02001183 pctldev = kzalloc(sizeof(struct pinctrl_dev), GFP_KERNEL);
1184 if (pctldev == NULL)
1185 return NULL;
1186
1187 /* Initialize pin control device struct */
1188 pctldev->owner = pctldesc->owner;
1189 pctldev->desc = pctldesc;
1190 pctldev->driver_data = driver_data;
1191 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
1192 spin_lock_init(&pctldev->pin_desc_tree_lock);
1193 INIT_LIST_HEAD(&pctldev->gpio_ranges);
1194 mutex_init(&pctldev->gpio_ranges_lock);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001195 pctldev->dev = dev;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001196
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001197 /* If we're implementing pinmuxing, check the ops for sanity */
1198 if (pctldesc->pmxops) {
1199 ret = pinmux_check_ops(pctldev);
1200 if (ret) {
1201 pr_err("%s pinmux ops lacks necessary functions\n",
1202 pctldesc->name);
1203 goto out_err;
1204 }
1205 }
1206
1207 /* If we're implementing pinconfig, check the ops for sanity */
1208 if (pctldesc->confops) {
1209 ret = pinconf_check_ops(pctldev);
1210 if (ret) {
1211 pr_err("%s pin config ops lacks necessary functions\n",
1212 pctldesc->name);
1213 goto out_err;
1214 }
1215 }
1216
Linus Walleij2744e8a2011-05-02 20:50:54 +02001217 /* Register all the pins */
1218 pr_debug("try to register %d pins on %s...\n",
1219 pctldesc->npins, pctldesc->name);
1220 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1221 if (ret) {
1222 pr_err("error during pin registration\n");
1223 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1224 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001225 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001226 }
1227
1228 pinctrl_init_device_debugfs(pctldev);
1229 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -07001230 list_add_tail(&pctldev->node, &pinctrldev_list);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001231 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleije93bcee2012-02-09 07:23:28 +01001232 pinctrl_hog_maps(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001233 return pctldev;
1234
Stephen Warren51cd24e2011-12-09 16:59:05 -07001235out_err:
1236 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001237 return NULL;
1238}
1239EXPORT_SYMBOL_GPL(pinctrl_register);
1240
1241/**
1242 * pinctrl_unregister() - unregister pinmux
1243 * @pctldev: pin controller to unregister
1244 *
1245 * Called by pinmux drivers to unregister a pinmux.
1246 */
1247void pinctrl_unregister(struct pinctrl_dev *pctldev)
1248{
1249 if (pctldev == NULL)
1250 return;
1251
Tony Lindgren02157162012-01-20 08:17:22 -08001252 pinctrl_remove_device_debugfs(pctldev);
Linus Walleije93bcee2012-02-09 07:23:28 +01001253 pinctrl_unhog_maps(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001254 /* TODO: check that no pinmuxes are still active? */
1255 mutex_lock(&pinctrldev_list_mutex);
1256 list_del(&pctldev->node);
1257 mutex_unlock(&pinctrldev_list_mutex);
1258 /* Destroy descriptor tree */
1259 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1260 pctldev->desc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001261 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001262}
1263EXPORT_SYMBOL_GPL(pinctrl_unregister);
1264
1265static int __init pinctrl_init(void)
1266{
1267 pr_info("initialized pinctrl subsystem\n");
1268 pinctrl_init_debugfs();
1269 return 0;
1270}
1271
1272/* init early since many drivers really need to initialized pinmux early */
1273core_initcall(pinctrl_init);