]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/pinctrl/core.c
Merge branch 'mullins' of git://people.freedesktop.org/~deathsimple/linux into drm...
[mirror_ubuntu-zesty-kernel.git] / drivers / pinctrl / core.c
CommitLineData
2744e8af
LW
1/*
2 * Core driver for the pin control subsystem
3 *
befe5bdf 4 * Copyright (C) 2011-2012 ST-Ericsson SA
2744e8af
LW
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 *
b2b3e66e
SW
10 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
11 *
2744e8af
LW
12 * License terms: GNU General Public License (GPL) version 2
13 */
14#define pr_fmt(fmt) "pinctrl core: " fmt
15
16#include <linux/kernel.h>
ab78029e 17#include <linux/kref.h>
a5a697cd 18#include <linux/export.h>
2744e8af
LW
19#include <linux/init.h>
20#include <linux/device.h>
21#include <linux/slab.h>
2744e8af
LW
22#include <linux/err.h>
23#include <linux/list.h>
2744e8af
LW
24#include <linux/sysfs.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
6d4ca1fb 27#include <linux/pinctrl/consumer.h>
2744e8af
LW
28#include <linux/pinctrl/pinctrl.h>
29#include <linux/pinctrl/machine.h>
2afe8229
HZ
30
31#ifdef CONFIG_GPIOLIB
51e13c24 32#include <asm-generic/gpio.h>
2afe8229
HZ
33#endif
34
2744e8af 35#include "core.h"
57291ce2 36#include "devicetree.h"
2744e8af 37#include "pinmux.h"
ae6b4d85 38#include "pinconf.h"
2744e8af 39
b2b3e66e 40
5b3aa5f7
DA
41static bool pinctrl_dummy_state;
42
42fed7ba 43/* Mutex taken to protect pinctrl_list */
843aec96 44static DEFINE_MUTEX(pinctrl_list_mutex);
42fed7ba
PC
45
46/* Mutex taken to protect pinctrl_maps */
47DEFINE_MUTEX(pinctrl_maps_mutex);
48
49/* Mutex taken to protect pinctrldev_list */
843aec96 50static DEFINE_MUTEX(pinctrldev_list_mutex);
57b676f9
SW
51
52/* Global list of pin control devices (struct pinctrl_dev) */
42fed7ba 53static LIST_HEAD(pinctrldev_list);
2744e8af 54
57b676f9 55/* List of pin controller handles (struct pinctrl) */
befe5bdf
LW
56static LIST_HEAD(pinctrl_list);
57
57b676f9 58/* List of pinctrl maps (struct pinctrl_maps) */
6f9e41f4 59LIST_HEAD(pinctrl_maps);
b2b3e66e 60
befe5bdf 61
5b3aa5f7
DA
62/**
63 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
64 *
65 * Usually this function is called by platforms without pinctrl driver support
66 * but run with some shared drivers using pinctrl APIs.
67 * After calling this function, the pinctrl core will return successfully
68 * with creating a dummy state for the driver to keep going smoothly.
69 */
70void pinctrl_provide_dummies(void)
71{
72 pinctrl_dummy_state = true;
73}
74
2744e8af
LW
75const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
76{
77 /* We're not allowed to register devices without name */
78 return pctldev->desc->name;
79}
80EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
81
d6e99abb
HZ
82const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
83{
84 return dev_name(pctldev->dev);
85}
86EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
87
2744e8af
LW
88void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
89{
90 return pctldev->driver_data;
91}
92EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
93
94/**
9dfac4fd
LW
95 * get_pinctrl_dev_from_devname() - look up pin controller device
96 * @devname: the name of a device instance, as returned by dev_name()
2744e8af
LW
97 *
98 * Looks up a pin control device matching a certain device name or pure device
99 * pointer, the pure device pointer will take precedence.
100 */
9dfac4fd 101struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
2744e8af
LW
102{
103 struct pinctrl_dev *pctldev = NULL;
2744e8af 104
9dfac4fd
LW
105 if (!devname)
106 return NULL;
107
44d5f7bb
LW
108 mutex_lock(&pinctrldev_list_mutex);
109
2744e8af 110 list_for_each_entry(pctldev, &pinctrldev_list, node) {
9dfac4fd 111 if (!strcmp(dev_name(pctldev->dev), devname)) {
2744e8af 112 /* Matched on device name */
44d5f7bb
LW
113 mutex_unlock(&pinctrldev_list_mutex);
114 return pctldev;
2744e8af
LW
115 }
116 }
2744e8af 117
44d5f7bb
LW
118 mutex_unlock(&pinctrldev_list_mutex);
119
120 return NULL;
2744e8af
LW
121}
122
42fed7ba
PC
123struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np)
124{
125 struct pinctrl_dev *pctldev;
126
127 mutex_lock(&pinctrldev_list_mutex);
128
129 list_for_each_entry(pctldev, &pinctrldev_list, node)
130 if (pctldev->dev->of_node == np) {
131 mutex_unlock(&pinctrldev_list_mutex);
132 return pctldev;
133 }
134
d463f82d 135 mutex_unlock(&pinctrldev_list_mutex);
42fed7ba
PC
136
137 return NULL;
138}
139
ae6b4d85
LW
140/**
141 * pin_get_from_name() - look up a pin number from a name
142 * @pctldev: the pin control device to lookup the pin on
143 * @name: the name of the pin to look up
144 */
145int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
146{
706e8520 147 unsigned i, pin;
ae6b4d85 148
706e8520
CP
149 /* The pin number can be retrived from the pin controller descriptor */
150 for (i = 0; i < pctldev->desc->npins; i++) {
ae6b4d85
LW
151 struct pin_desc *desc;
152
706e8520 153 pin = pctldev->desc->pins[i].number;
ae6b4d85
LW
154 desc = pin_desc_get(pctldev, pin);
155 /* Pin space may be sparse */
6c325f87 156 if (desc && !strcmp(name, desc->name))
ae6b4d85
LW
157 return pin;
158 }
159
160 return -EINVAL;
161}
162
dcb5dbc3
DA
163/**
164 * pin_get_name_from_id() - look up a pin name from a pin id
165 * @pctldev: the pin control device to lookup the pin on
166 * @name: the name of the pin to look up
167 */
168const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
169{
170 const struct pin_desc *desc;
171
172 desc = pin_desc_get(pctldev, pin);
173 if (desc == NULL) {
174 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
175 pin);
176 return NULL;
177 }
178
179 return desc->name;
180}
181
2744e8af
LW
182/**
183 * pin_is_valid() - check if pin exists on controller
184 * @pctldev: the pin control device to check the pin on
185 * @pin: pin to check, use the local pin controller index number
186 *
187 * This tells us whether a certain pin exist on a certain pin controller or
188 * not. Pin lists may be sparse, so some pins may not exist.
189 */
190bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
191{
192 struct pin_desc *pindesc;
193
194 if (pin < 0)
195 return false;
196
42fed7ba 197 mutex_lock(&pctldev->mutex);
2744e8af 198 pindesc = pin_desc_get(pctldev, pin);
42fed7ba 199 mutex_unlock(&pctldev->mutex);
2744e8af 200
57b676f9 201 return pindesc != NULL;
2744e8af
LW
202}
203EXPORT_SYMBOL_GPL(pin_is_valid);
204
205/* Deletes a range of pin descriptors */
206static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
207 const struct pinctrl_pin_desc *pins,
208 unsigned num_pins)
209{
210 int i;
211
2744e8af
LW
212 for (i = 0; i < num_pins; i++) {
213 struct pin_desc *pindesc;
214
215 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
216 pins[i].number);
217 if (pindesc != NULL) {
218 radix_tree_delete(&pctldev->pin_desc_tree,
219 pins[i].number);
ca53c5f1
LW
220 if (pindesc->dynamic_name)
221 kfree(pindesc->name);
2744e8af
LW
222 }
223 kfree(pindesc);
224 }
2744e8af
LW
225}
226
227static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
228 unsigned number, const char *name)
229{
230 struct pin_desc *pindesc;
231
232 pindesc = pin_desc_get(pctldev, number);
233 if (pindesc != NULL) {
234 pr_err("pin %d already registered on %s\n", number,
235 pctldev->desc->name);
236 return -EINVAL;
237 }
238
239 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
95dcd4ae
SW
240 if (pindesc == NULL) {
241 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
2744e8af 242 return -ENOMEM;
95dcd4ae 243 }
ae6b4d85 244
2744e8af
LW
245 /* Set owner */
246 pindesc->pctldev = pctldev;
247
9af1e44f 248 /* Copy basic pin info */
8dc6ae4d 249 if (name) {
ca53c5f1
LW
250 pindesc->name = name;
251 } else {
252 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
eb26cc9c
SK
253 if (pindesc->name == NULL) {
254 kfree(pindesc);
ca53c5f1 255 return -ENOMEM;
eb26cc9c 256 }
ca53c5f1
LW
257 pindesc->dynamic_name = true;
258 }
2744e8af 259
2744e8af 260 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
2744e8af 261 pr_debug("registered pin %d (%s) on %s\n",
ca53c5f1 262 number, pindesc->name, pctldev->desc->name);
2744e8af
LW
263 return 0;
264}
265
266static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
267 struct pinctrl_pin_desc const *pins,
268 unsigned num_descs)
269{
270 unsigned i;
271 int ret = 0;
272
273 for (i = 0; i < num_descs; i++) {
274 ret = pinctrl_register_one_pin(pctldev,
275 pins[i].number, pins[i].name);
276 if (ret)
277 return ret;
278 }
279
280 return 0;
281}
282
c8587eee
CR
283/**
284 * gpio_to_pin() - GPIO range GPIO number to pin number translation
285 * @range: GPIO range used for the translation
286 * @gpio: gpio pin to translate to a pin number
287 *
288 * Finds the pin number for a given GPIO using the specified GPIO range
289 * as a base for translation. The distinction between linear GPIO ranges
290 * and pin list based GPIO ranges is managed correctly by this function.
291 *
292 * This function assumes the gpio is part of the specified GPIO range, use
293 * only after making sure this is the case (e.g. by calling it on the
294 * result of successful pinctrl_get_device_gpio_range calls)!
295 */
296static inline int gpio_to_pin(struct pinctrl_gpio_range *range,
297 unsigned int gpio)
298{
299 unsigned int offset = gpio - range->base;
300 if (range->pins)
301 return range->pins[offset];
302 else
303 return range->pin_base + offset;
304}
305
2744e8af
LW
306/**
307 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
308 * @pctldev: pin controller device to check
309 * @gpio: gpio pin to check taken from the global GPIO pin space
310 *
311 * Tries to match a GPIO pin number to the ranges handled by a certain pin
312 * controller, return the range or NULL
313 */
314static struct pinctrl_gpio_range *
315pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
316{
317 struct pinctrl_gpio_range *range = NULL;
318
42fed7ba 319 mutex_lock(&pctldev->mutex);
2744e8af 320 /* Loop over the ranges */
2744e8af
LW
321 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
322 /* Check if we're in the valid range */
323 if (gpio >= range->base &&
324 gpio < range->base + range->npins) {
42fed7ba 325 mutex_unlock(&pctldev->mutex);
2744e8af
LW
326 return range;
327 }
328 }
42fed7ba 329 mutex_unlock(&pctldev->mutex);
2744e8af
LW
330 return NULL;
331}
332
51e13c24
HZ
333/**
334 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
335 * the same GPIO chip are in range
336 * @gpio: gpio pin to check taken from the global GPIO pin space
337 *
338 * This function is complement of pinctrl_match_gpio_range(). If the return
339 * value of pinctrl_match_gpio_range() is NULL, this function could be used
340 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
341 * of the same GPIO chip don't have back-end pinctrl interface.
342 * If the return value is true, it means that pinctrl device is ready & the
343 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
344 * is false, it means that pinctrl device may not be ready.
345 */
2afe8229 346#ifdef CONFIG_GPIOLIB
51e13c24
HZ
347static bool pinctrl_ready_for_gpio_range(unsigned gpio)
348{
349 struct pinctrl_dev *pctldev;
350 struct pinctrl_gpio_range *range = NULL;
351 struct gpio_chip *chip = gpio_to_chip(gpio);
352
44d5f7bb
LW
353 mutex_lock(&pinctrldev_list_mutex);
354
51e13c24
HZ
355 /* Loop over the pin controllers */
356 list_for_each_entry(pctldev, &pinctrldev_list, node) {
357 /* Loop over the ranges */
5ffbe2e6 358 mutex_lock(&pctldev->mutex);
51e13c24
HZ
359 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
360 /* Check if any gpio range overlapped with gpio chip */
361 if (range->base + range->npins - 1 < chip->base ||
362 range->base > chip->base + chip->ngpio - 1)
363 continue;
5ffbe2e6 364 mutex_unlock(&pctldev->mutex);
44d5f7bb 365 mutex_unlock(&pinctrldev_list_mutex);
51e13c24
HZ
366 return true;
367 }
5ffbe2e6 368 mutex_unlock(&pctldev->mutex);
51e13c24 369 }
44d5f7bb
LW
370
371 mutex_unlock(&pinctrldev_list_mutex);
372
51e13c24
HZ
373 return false;
374}
2afe8229
HZ
375#else
376static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
377#endif
51e13c24 378
2744e8af
LW
379/**
380 * pinctrl_get_device_gpio_range() - find device for GPIO range
381 * @gpio: the pin to locate the pin controller for
382 * @outdev: the pin control device if found
383 * @outrange: the GPIO range if found
384 *
385 * Find the pin controller handling a certain GPIO pin from the pinspace of
386 * the GPIO subsystem, return the device and the matching GPIO range. Returns
4650b7cb
DA
387 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
388 * may still have not been registered.
2744e8af 389 */
4ecce45d
SW
390static int pinctrl_get_device_gpio_range(unsigned gpio,
391 struct pinctrl_dev **outdev,
392 struct pinctrl_gpio_range **outrange)
2744e8af
LW
393{
394 struct pinctrl_dev *pctldev = NULL;
395
f0059021
AL
396 mutex_lock(&pinctrldev_list_mutex);
397
2744e8af 398 /* Loop over the pin controllers */
2744e8af
LW
399 list_for_each_entry(pctldev, &pinctrldev_list, node) {
400 struct pinctrl_gpio_range *range;
401
402 range = pinctrl_match_gpio_range(pctldev, gpio);
403 if (range != NULL) {
404 *outdev = pctldev;
405 *outrange = range;
f0059021 406 mutex_unlock(&pinctrldev_list_mutex);
2744e8af
LW
407 return 0;
408 }
409 }
2744e8af 410
f0059021
AL
411 mutex_unlock(&pinctrldev_list_mutex);
412
4650b7cb 413 return -EPROBE_DEFER;
2744e8af
LW
414}
415
416/**
417 * pinctrl_add_gpio_range() - register a GPIO range for a controller
418 * @pctldev: pin controller device to add the range to
419 * @range: the GPIO range to add
420 *
421 * This adds a range of GPIOs to be handled by a certain pin controller. Call
422 * this to register handled ranges after registering your pin controller.
423 */
424void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
425 struct pinctrl_gpio_range *range)
426{
42fed7ba 427 mutex_lock(&pctldev->mutex);
8b9c139f 428 list_add_tail(&range->node, &pctldev->gpio_ranges);
42fed7ba 429 mutex_unlock(&pctldev->mutex);
2744e8af 430}
4ecce45d 431EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
2744e8af 432
3e5e00b6
DA
433void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
434 struct pinctrl_gpio_range *ranges,
435 unsigned nranges)
436{
437 int i;
438
439 for (i = 0; i < nranges; i++)
440 pinctrl_add_gpio_range(pctldev, &ranges[i]);
441}
442EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
443
192c369c 444struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
f23f1516
SH
445 struct pinctrl_gpio_range *range)
446{
42fed7ba
PC
447 struct pinctrl_dev *pctldev;
448
42fed7ba 449 pctldev = get_pinctrl_dev_from_devname(devname);
f23f1516 450
dfa97515
LW
451 /*
452 * If we can't find this device, let's assume that is because
453 * it has not probed yet, so the driver trying to register this
454 * range need to defer probing.
455 */
42fed7ba 456 if (!pctldev) {
dfa97515 457 return ERR_PTR(-EPROBE_DEFER);
42fed7ba 458 }
f23f1516 459 pinctrl_add_gpio_range(pctldev, range);
42fed7ba 460
f23f1516
SH
461 return pctldev;
462}
192c369c 463EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
f23f1516 464
586a87e6
CR
465int pinctrl_get_group_pins(struct pinctrl_dev *pctldev, const char *pin_group,
466 const unsigned **pins, unsigned *num_pins)
467{
468 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
469 int gs;
470
471 gs = pinctrl_get_group_selector(pctldev, pin_group);
472 if (gs < 0)
473 return gs;
474
475 return pctlops->get_group_pins(pctldev, gs, pins, num_pins);
476}
477EXPORT_SYMBOL_GPL(pinctrl_get_group_pins);
478
9afbefb2
LW
479/**
480 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
481 * @pctldev: the pin controller device to look in
482 * @pin: a controller-local number to find the range for
483 */
484struct pinctrl_gpio_range *
485pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
486 unsigned int pin)
487{
c8f50e86 488 struct pinctrl_gpio_range *range;
9afbefb2 489
42fed7ba 490 mutex_lock(&pctldev->mutex);
9afbefb2
LW
491 /* Loop over the ranges */
492 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
493 /* Check if we're in the valid range */
c8587eee
CR
494 if (range->pins) {
495 int a;
496 for (a = 0; a < range->npins; a++) {
497 if (range->pins[a] == pin)
c8f50e86 498 goto out;
c8587eee
CR
499 }
500 } else if (pin >= range->pin_base &&
c8f50e86
WY
501 pin < range->pin_base + range->npins)
502 goto out;
9afbefb2 503 }
c8f50e86
WY
504 range = NULL;
505out:
42fed7ba 506 mutex_unlock(&pctldev->mutex);
c8f50e86 507 return range;
9afbefb2
LW
508}
509EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
510
7e10ee68
VK
511/**
512 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
513 * @pctldev: pin controller device to remove the range from
514 * @range: the GPIO range to remove
515 */
516void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
517 struct pinctrl_gpio_range *range)
518{
42fed7ba 519 mutex_lock(&pctldev->mutex);
7e10ee68 520 list_del(&range->node);
42fed7ba 521 mutex_unlock(&pctldev->mutex);
7e10ee68
VK
522}
523EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
524
7afde8ba
LW
525/**
526 * pinctrl_get_group_selector() - returns the group selector for a group
527 * @pctldev: the pin controller handling the group
528 * @pin_group: the pin group to look up
529 */
530int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
531 const char *pin_group)
532{
533 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
d1e90e9e 534 unsigned ngroups = pctlops->get_groups_count(pctldev);
7afde8ba
LW
535 unsigned group_selector = 0;
536
d1e90e9e 537 while (group_selector < ngroups) {
7afde8ba
LW
538 const char *gname = pctlops->get_group_name(pctldev,
539 group_selector);
540 if (!strcmp(gname, pin_group)) {
51cd24ee 541 dev_dbg(pctldev->dev,
7afde8ba
LW
542 "found group selector %u for %s\n",
543 group_selector,
544 pin_group);
545 return group_selector;
546 }
547
548 group_selector++;
549 }
550
51cd24ee 551 dev_err(pctldev->dev, "does not have pin group %s\n",
7afde8ba
LW
552 pin_group);
553
554 return -EINVAL;
555}
556
befe5bdf
LW
557/**
558 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
559 * @gpio: the GPIO pin number from the GPIO subsystem number space
560 *
561 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
562 * as part of their gpio_request() semantics, platforms and individual drivers
563 * shall *NOT* request GPIO pins to be muxed in.
564 */
565int pinctrl_request_gpio(unsigned gpio)
566{
567 struct pinctrl_dev *pctldev;
568 struct pinctrl_gpio_range *range;
569 int ret;
570 int pin;
571
572 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
57b676f9 573 if (ret) {
51e13c24
HZ
574 if (pinctrl_ready_for_gpio_range(gpio))
575 ret = 0;
4650b7cb 576 return ret;
57b676f9 577 }
befe5bdf 578
9b77ace4
AL
579 mutex_lock(&pctldev->mutex);
580
befe5bdf 581 /* Convert to the pin controllers number space */
c8587eee 582 pin = gpio_to_pin(range, gpio);
befe5bdf 583
57b676f9
SW
584 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
585
9b77ace4
AL
586 mutex_unlock(&pctldev->mutex);
587
57b676f9 588 return ret;
befe5bdf
LW
589}
590EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
591
592/**
593 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
594 * @gpio: the GPIO pin number from the GPIO subsystem number space
595 *
596 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
597 * as part of their gpio_free() semantics, platforms and individual drivers
598 * shall *NOT* request GPIO pins to be muxed out.
599 */
600void pinctrl_free_gpio(unsigned gpio)
601{
602 struct pinctrl_dev *pctldev;
603 struct pinctrl_gpio_range *range;
604 int ret;
605 int pin;
606
607 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
57b676f9 608 if (ret) {
befe5bdf 609 return;
57b676f9 610 }
42fed7ba 611 mutex_lock(&pctldev->mutex);
befe5bdf
LW
612
613 /* Convert to the pin controllers number space */
c8587eee 614 pin = gpio_to_pin(range, gpio);
befe5bdf 615
57b676f9
SW
616 pinmux_free_gpio(pctldev, pin, range);
617
42fed7ba 618 mutex_unlock(&pctldev->mutex);
befe5bdf
LW
619}
620EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
621
622static int pinctrl_gpio_direction(unsigned gpio, bool input)
623{
624 struct pinctrl_dev *pctldev;
625 struct pinctrl_gpio_range *range;
626 int ret;
627 int pin;
628
629 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
42fed7ba 630 if (ret) {
befe5bdf 631 return ret;
42fed7ba
PC
632 }
633
634 mutex_lock(&pctldev->mutex);
befe5bdf
LW
635
636 /* Convert to the pin controllers number space */
c8587eee 637 pin = gpio_to_pin(range, gpio);
42fed7ba
PC
638 ret = pinmux_gpio_direction(pctldev, range, pin, input);
639
640 mutex_unlock(&pctldev->mutex);
befe5bdf 641
42fed7ba 642 return ret;
befe5bdf
LW
643}
644
645/**
646 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
647 * @gpio: the GPIO pin number from the GPIO subsystem number space
648 *
649 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
650 * as part of their gpio_direction_input() semantics, platforms and individual
651 * drivers shall *NOT* touch pin control GPIO calls.
652 */
653int pinctrl_gpio_direction_input(unsigned gpio)
654{
42fed7ba 655 return pinctrl_gpio_direction(gpio, true);
befe5bdf
LW
656}
657EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
658
659/**
660 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
661 * @gpio: the GPIO pin number from the GPIO subsystem number space
662 *
663 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
664 * as part of their gpio_direction_output() semantics, platforms and individual
665 * drivers shall *NOT* touch pin control GPIO calls.
666 */
667int pinctrl_gpio_direction_output(unsigned gpio)
668{
42fed7ba 669 return pinctrl_gpio_direction(gpio, false);
befe5bdf
LW
670}
671EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
672
6e5e959d
SW
673static struct pinctrl_state *find_state(struct pinctrl *p,
674 const char *name)
befe5bdf 675{
6e5e959d
SW
676 struct pinctrl_state *state;
677
678 list_for_each_entry(state, &p->states, node)
679 if (!strcmp(state->name, name))
680 return state;
681
682 return NULL;
683}
684
685static struct pinctrl_state *create_state(struct pinctrl *p,
686 const char *name)
687{
688 struct pinctrl_state *state;
689
690 state = kzalloc(sizeof(*state), GFP_KERNEL);
691 if (state == NULL) {
692 dev_err(p->dev,
693 "failed to alloc struct pinctrl_state\n");
694 return ERR_PTR(-ENOMEM);
695 }
696
697 state->name = name;
698 INIT_LIST_HEAD(&state->settings);
699
700 list_add_tail(&state->node, &p->states);
701
702 return state;
703}
704
705static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
706{
707 struct pinctrl_state *state;
7ecdb16f 708 struct pinctrl_setting *setting;
6e5e959d 709 int ret;
befe5bdf 710
6e5e959d
SW
711 state = find_state(p, map->name);
712 if (!state)
713 state = create_state(p, map->name);
714 if (IS_ERR(state))
715 return PTR_ERR(state);
befe5bdf 716
1e2082b5
SW
717 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
718 return 0;
719
6e5e959d
SW
720 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
721 if (setting == NULL) {
722 dev_err(p->dev,
723 "failed to alloc struct pinctrl_setting\n");
724 return -ENOMEM;
725 }
befe5bdf 726
1e2082b5
SW
727 setting->type = map->type;
728
6e5e959d
SW
729 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
730 if (setting->pctldev == NULL) {
6e5e959d 731 kfree(setting);
89216494
LW
732 /* Do not defer probing of hogs (circular loop) */
733 if (!strcmp(map->ctrl_dev_name, map->dev_name))
734 return -ENODEV;
c05127c4
LW
735 /*
736 * OK let us guess that the driver is not there yet, and
737 * let's defer obtaining this pinctrl handle to later...
738 */
89216494
LW
739 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
740 map->ctrl_dev_name);
c05127c4 741 return -EPROBE_DEFER;
6e5e959d
SW
742 }
743
1a78958d
LW
744 setting->dev_name = map->dev_name;
745
1e2082b5
SW
746 switch (map->type) {
747 case PIN_MAP_TYPE_MUX_GROUP:
748 ret = pinmux_map_to_setting(map, setting);
749 break;
750 case PIN_MAP_TYPE_CONFIGS_PIN:
751 case PIN_MAP_TYPE_CONFIGS_GROUP:
752 ret = pinconf_map_to_setting(map, setting);
753 break;
754 default:
755 ret = -EINVAL;
756 break;
757 }
6e5e959d
SW
758 if (ret < 0) {
759 kfree(setting);
760 return ret;
761 }
762
763 list_add_tail(&setting->node, &state->settings);
764
765 return 0;
766}
767
768static struct pinctrl *find_pinctrl(struct device *dev)
769{
770 struct pinctrl *p;
771
42fed7ba 772 mutex_lock(&pinctrl_list_mutex);
1e2082b5 773 list_for_each_entry(p, &pinctrl_list, node)
42fed7ba
PC
774 if (p->dev == dev) {
775 mutex_unlock(&pinctrl_list_mutex);
6e5e959d 776 return p;
42fed7ba 777 }
6e5e959d 778
42fed7ba 779 mutex_unlock(&pinctrl_list_mutex);
6e5e959d
SW
780 return NULL;
781}
782
42fed7ba 783static void pinctrl_free(struct pinctrl *p, bool inlist);
6e5e959d
SW
784
785static struct pinctrl *create_pinctrl(struct device *dev)
786{
787 struct pinctrl *p;
788 const char *devname;
789 struct pinctrl_maps *maps_node;
790 int i;
791 struct pinctrl_map const *map;
792 int ret;
befe5bdf
LW
793
794 /*
795 * create the state cookie holder struct pinctrl for each
796 * mapping, this is what consumers will get when requesting
797 * a pin control handle with pinctrl_get()
798 */
02f5b989 799 p = kzalloc(sizeof(*p), GFP_KERNEL);
95dcd4ae
SW
800 if (p == NULL) {
801 dev_err(dev, "failed to alloc struct pinctrl\n");
befe5bdf 802 return ERR_PTR(-ENOMEM);
95dcd4ae 803 }
7ecdb16f 804 p->dev = dev;
6e5e959d 805 INIT_LIST_HEAD(&p->states);
57291ce2
SW
806 INIT_LIST_HEAD(&p->dt_maps);
807
808 ret = pinctrl_dt_to_map(p);
809 if (ret < 0) {
810 kfree(p);
811 return ERR_PTR(ret);
812 }
6e5e959d
SW
813
814 devname = dev_name(dev);
befe5bdf 815
42fed7ba 816 mutex_lock(&pinctrl_maps_mutex);
befe5bdf 817 /* Iterate over the pin control maps to locate the right ones */
b2b3e66e 818 for_each_maps(maps_node, i, map) {
7ecdb16f
SW
819 /* Map must be for this device */
820 if (strcmp(map->dev_name, devname))
821 continue;
822
6e5e959d 823 ret = add_setting(p, map);
89216494
LW
824 /*
825 * At this point the adding of a setting may:
826 *
827 * - Defer, if the pinctrl device is not yet available
828 * - Fail, if the pinctrl device is not yet available,
829 * AND the setting is a hog. We cannot defer that, since
830 * the hog will kick in immediately after the device
831 * is registered.
832 *
833 * If the error returned was not -EPROBE_DEFER then we
834 * accumulate the errors to see if we end up with
835 * an -EPROBE_DEFER later, as that is the worst case.
836 */
837 if (ret == -EPROBE_DEFER) {
42fed7ba
PC
838 pinctrl_free(p, false);
839 mutex_unlock(&pinctrl_maps_mutex);
6e5e959d 840 return ERR_PTR(ret);
7ecdb16f 841 }
befe5bdf 842 }
42fed7ba
PC
843 mutex_unlock(&pinctrl_maps_mutex);
844
89216494
LW
845 if (ret < 0) {
846 /* If some other error than deferral occured, return here */
42fed7ba 847 pinctrl_free(p, false);
89216494
LW
848 return ERR_PTR(ret);
849 }
befe5bdf 850
ab78029e
LW
851 kref_init(&p->users);
852
b0666ba4 853 /* Add the pinctrl handle to the global list */
7b320cb1 854 mutex_lock(&pinctrl_list_mutex);
8b9c139f 855 list_add_tail(&p->node, &pinctrl_list);
7b320cb1 856 mutex_unlock(&pinctrl_list_mutex);
befe5bdf
LW
857
858 return p;
6e5e959d 859}
7ecdb16f 860
42fed7ba
PC
861/**
862 * pinctrl_get() - retrieves the pinctrl handle for a device
863 * @dev: the device to obtain the handle for
864 */
865struct pinctrl *pinctrl_get(struct device *dev)
6e5e959d
SW
866{
867 struct pinctrl *p;
7ecdb16f 868
6e5e959d
SW
869 if (WARN_ON(!dev))
870 return ERR_PTR(-EINVAL);
871
ab78029e
LW
872 /*
873 * See if somebody else (such as the device core) has already
874 * obtained a handle to the pinctrl for this device. In that case,
875 * return another pointer to it.
876 */
6e5e959d 877 p = find_pinctrl(dev);
ab78029e
LW
878 if (p != NULL) {
879 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
880 kref_get(&p->users);
881 return p;
882 }
7ecdb16f 883
d599bfb3 884 return create_pinctrl(dev);
befe5bdf
LW
885}
886EXPORT_SYMBOL_GPL(pinctrl_get);
887
d3cee830
RG
888static void pinctrl_free_setting(bool disable_setting,
889 struct pinctrl_setting *setting)
890{
891 switch (setting->type) {
892 case PIN_MAP_TYPE_MUX_GROUP:
893 if (disable_setting)
894 pinmux_disable_setting(setting);
895 pinmux_free_setting(setting);
896 break;
897 case PIN_MAP_TYPE_CONFIGS_PIN:
898 case PIN_MAP_TYPE_CONFIGS_GROUP:
899 pinconf_free_setting(setting);
900 break;
901 default:
902 break;
903 }
904}
905
42fed7ba 906static void pinctrl_free(struct pinctrl *p, bool inlist)
befe5bdf 907{
6e5e959d
SW
908 struct pinctrl_state *state, *n1;
909 struct pinctrl_setting *setting, *n2;
910
42fed7ba 911 mutex_lock(&pinctrl_list_mutex);
6e5e959d
SW
912 list_for_each_entry_safe(state, n1, &p->states, node) {
913 list_for_each_entry_safe(setting, n2, &state->settings, node) {
d3cee830 914 pinctrl_free_setting(state == p->state, setting);
6e5e959d
SW
915 list_del(&setting->node);
916 kfree(setting);
917 }
918 list_del(&state->node);
919 kfree(state);
7ecdb16f 920 }
befe5bdf 921
57291ce2
SW
922 pinctrl_dt_free_maps(p);
923
6e5e959d
SW
924 if (inlist)
925 list_del(&p->node);
befe5bdf 926 kfree(p);
42fed7ba 927 mutex_unlock(&pinctrl_list_mutex);
befe5bdf 928}
befe5bdf
LW
929
930/**
ab78029e
LW
931 * pinctrl_release() - release the pinctrl handle
932 * @kref: the kref in the pinctrl being released
933 */
2917e833 934static void pinctrl_release(struct kref *kref)
ab78029e
LW
935{
936 struct pinctrl *p = container_of(kref, struct pinctrl, users);
937
42fed7ba 938 pinctrl_free(p, true);
ab78029e
LW
939}
940
941/**
942 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
6e5e959d 943 * @p: the pinctrl handle to release
befe5bdf 944 */
57b676f9
SW
945void pinctrl_put(struct pinctrl *p)
946{
ab78029e 947 kref_put(&p->users, pinctrl_release);
57b676f9
SW
948}
949EXPORT_SYMBOL_GPL(pinctrl_put);
950
42fed7ba
PC
951/**
952 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
953 * @p: the pinctrl handle to retrieve the state from
954 * @name: the state name to retrieve
955 */
956struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
957 const char *name)
befe5bdf 958{
6e5e959d 959 struct pinctrl_state *state;
befe5bdf 960
6e5e959d 961 state = find_state(p, name);
5b3aa5f7
DA
962 if (!state) {
963 if (pinctrl_dummy_state) {
964 /* create dummy state */
965 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
966 name);
967 state = create_state(p, name);
d599bfb3
RG
968 } else
969 state = ERR_PTR(-ENODEV);
5b3aa5f7 970 }
57b676f9 971
6e5e959d 972 return state;
befe5bdf 973}
42fed7ba 974EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
befe5bdf
LW
975
976/**
42fed7ba
PC
977 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
978 * @p: the pinctrl handle for the device that requests configuration
979 * @state: the state handle to select/activate/program
befe5bdf 980 */
42fed7ba 981int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
befe5bdf 982{
6e5e959d 983 struct pinctrl_setting *setting, *setting2;
50cf7c8a 984 struct pinctrl_state *old_state = p->state;
6e5e959d 985 int ret;
7ecdb16f 986
6e5e959d
SW
987 if (p->state == state)
988 return 0;
befe5bdf 989
6e5e959d
SW
990 if (p->state) {
991 /*
992 * The set of groups with a mux configuration in the old state
993 * may not be identical to the set of groups with a mux setting
994 * in the new state. While this might be unusual, it's entirely
995 * possible for the "user"-supplied mapping table to be written
996 * that way. For each group that was configured in the old state
997 * but not in the new state, this code puts that group into a
998 * safe/disabled state.
999 */
1000 list_for_each_entry(setting, &p->state->settings, node) {
1001 bool found = false;
1e2082b5
SW
1002 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
1003 continue;
6e5e959d 1004 list_for_each_entry(setting2, &state->settings, node) {
1e2082b5
SW
1005 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
1006 continue;
1007 if (setting2->data.mux.group ==
1008 setting->data.mux.group) {
6e5e959d
SW
1009 found = true;
1010 break;
1011 }
1012 }
1013 if (!found)
1014 pinmux_disable_setting(setting);
1015 }
1016 }
1017
3102a76c 1018 p->state = NULL;
6e5e959d
SW
1019
1020 /* Apply all the settings for the new state */
1021 list_for_each_entry(setting, &state->settings, node) {
1e2082b5
SW
1022 switch (setting->type) {
1023 case PIN_MAP_TYPE_MUX_GROUP:
1024 ret = pinmux_enable_setting(setting);
1025 break;
1026 case PIN_MAP_TYPE_CONFIGS_PIN:
1027 case PIN_MAP_TYPE_CONFIGS_GROUP:
1028 ret = pinconf_apply_setting(setting);
1029 break;
1030 default:
1031 ret = -EINVAL;
1032 break;
1033 }
3102a76c 1034
42fed7ba 1035 if (ret < 0) {
3102a76c 1036 goto unapply_new_state;
42fed7ba 1037 }
befe5bdf 1038 }
6e5e959d 1039
3102a76c
RG
1040 p->state = state;
1041
6e5e959d 1042 return 0;
3102a76c
RG
1043
1044unapply_new_state:
da58751c 1045 dev_err(p->dev, "Error applying setting, reverse things back\n");
3102a76c 1046
3102a76c
RG
1047 list_for_each_entry(setting2, &state->settings, node) {
1048 if (&setting2->node == &setting->node)
1049 break;
af606177
RG
1050 /*
1051 * All we can do here is pinmux_disable_setting.
1052 * That means that some pins are muxed differently now
1053 * than they were before applying the setting (We can't
1054 * "unmux a pin"!), but it's not a big deal since the pins
1055 * are free to be muxed by another apply_setting.
1056 */
1057 if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
1058 pinmux_disable_setting(setting2);
3102a76c 1059 }
8009d5ff 1060
385d9424
RG
1061 /* There's no infinite recursive loop here because p->state is NULL */
1062 if (old_state)
42fed7ba 1063 pinctrl_select_state(p, old_state);
6e5e959d
SW
1064
1065 return ret;
befe5bdf 1066}
6e5e959d 1067EXPORT_SYMBOL_GPL(pinctrl_select_state);
befe5bdf 1068
6d4ca1fb
SW
1069static void devm_pinctrl_release(struct device *dev, void *res)
1070{
1071 pinctrl_put(*(struct pinctrl **)res);
1072}
1073
1074/**
1075 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
1076 * @dev: the device to obtain the handle for
1077 *
1078 * If there is a need to explicitly destroy the returned struct pinctrl,
1079 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1080 */
1081struct pinctrl *devm_pinctrl_get(struct device *dev)
1082{
1083 struct pinctrl **ptr, *p;
1084
1085 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
1086 if (!ptr)
1087 return ERR_PTR(-ENOMEM);
1088
1089 p = pinctrl_get(dev);
1090 if (!IS_ERR(p)) {
1091 *ptr = p;
1092 devres_add(dev, ptr);
1093 } else {
1094 devres_free(ptr);
1095 }
1096
1097 return p;
1098}
1099EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1100
1101static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1102{
1103 struct pinctrl **p = res;
1104
1105 return *p == data;
1106}
1107
1108/**
1109 * devm_pinctrl_put() - Resource managed pinctrl_put()
1110 * @p: the pinctrl handle to release
1111 *
1112 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1113 * this function will not need to be called and the resource management
1114 * code will ensure that the resource is freed.
1115 */
1116void devm_pinctrl_put(struct pinctrl *p)
1117{
a72149e8 1118 WARN_ON(devres_release(p->dev, devm_pinctrl_release,
6d4ca1fb 1119 devm_pinctrl_match, p));
6d4ca1fb
SW
1120}
1121EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1122
57291ce2
SW
1123int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
1124 bool dup, bool locked)
befe5bdf 1125{
1e2082b5 1126 int i, ret;
b2b3e66e 1127 struct pinctrl_maps *maps_node;
befe5bdf
LW
1128
1129 pr_debug("add %d pinmux maps\n", num_maps);
1130
1131 /* First sanity check the new mapping */
1132 for (i = 0; i < num_maps; i++) {
1e2082b5
SW
1133 if (!maps[i].dev_name) {
1134 pr_err("failed to register map %s (%d): no device given\n",
1135 maps[i].name, i);
1136 return -EINVAL;
1137 }
1138
befe5bdf
LW
1139 if (!maps[i].name) {
1140 pr_err("failed to register map %d: no map name given\n",
95dcd4ae 1141 i);
befe5bdf
LW
1142 return -EINVAL;
1143 }
1144
1e2082b5
SW
1145 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1146 !maps[i].ctrl_dev_name) {
befe5bdf
LW
1147 pr_err("failed to register map %s (%d): no pin control device given\n",
1148 maps[i].name, i);
1149 return -EINVAL;
1150 }
1151
1e2082b5
SW
1152 switch (maps[i].type) {
1153 case PIN_MAP_TYPE_DUMMY_STATE:
1154 break;
1155 case PIN_MAP_TYPE_MUX_GROUP:
1156 ret = pinmux_validate_map(&maps[i], i);
1157 if (ret < 0)
fde04f41 1158 return ret;
1e2082b5
SW
1159 break;
1160 case PIN_MAP_TYPE_CONFIGS_PIN:
1161 case PIN_MAP_TYPE_CONFIGS_GROUP:
1162 ret = pinconf_validate_map(&maps[i], i);
1163 if (ret < 0)
fde04f41 1164 return ret;
1e2082b5
SW
1165 break;
1166 default:
1167 pr_err("failed to register map %s (%d): invalid type given\n",
95dcd4ae 1168 maps[i].name, i);
1681f5ae
SW
1169 return -EINVAL;
1170 }
befe5bdf
LW
1171 }
1172
b2b3e66e
SW
1173 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
1174 if (!maps_node) {
1175 pr_err("failed to alloc struct pinctrl_maps\n");
1176 return -ENOMEM;
1177 }
befe5bdf 1178
b2b3e66e 1179 maps_node->num_maps = num_maps;
57291ce2
SW
1180 if (dup) {
1181 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
1182 GFP_KERNEL);
1183 if (!maps_node->maps) {
1184 pr_err("failed to duplicate mapping table\n");
1185 kfree(maps_node);
1186 return -ENOMEM;
1187 }
1188 } else {
1189 maps_node->maps = maps;
befe5bdf
LW
1190 }
1191
57291ce2 1192 if (!locked)
42fed7ba 1193 mutex_lock(&pinctrl_maps_mutex);
b2b3e66e 1194 list_add_tail(&maps_node->node, &pinctrl_maps);
57291ce2 1195 if (!locked)
42fed7ba 1196 mutex_unlock(&pinctrl_maps_mutex);
b2b3e66e 1197
befe5bdf
LW
1198 return 0;
1199}
1200
57291ce2
SW
1201/**
1202 * pinctrl_register_mappings() - register a set of pin controller mappings
1203 * @maps: the pincontrol mappings table to register. This should probably be
1204 * marked with __initdata so it can be discarded after boot. This
1205 * function will perform a shallow copy for the mapping entries.
1206 * @num_maps: the number of maps in the mapping table
1207 */
1208int pinctrl_register_mappings(struct pinctrl_map const *maps,
1209 unsigned num_maps)
1210{
1211 return pinctrl_register_map(maps, num_maps, true, false);
1212}
1213
1214void pinctrl_unregister_map(struct pinctrl_map const *map)
1215{
1216 struct pinctrl_maps *maps_node;
1217
42fed7ba 1218 mutex_lock(&pinctrl_maps_mutex);
57291ce2
SW
1219 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1220 if (maps_node->maps == map) {
1221 list_del(&maps_node->node);
db6c2c69 1222 kfree(maps_node);
42fed7ba 1223 mutex_unlock(&pinctrl_maps_mutex);
57291ce2
SW
1224 return;
1225 }
1226 }
42fed7ba 1227 mutex_unlock(&pinctrl_maps_mutex);
57291ce2
SW
1228}
1229
840a47ba
JD
1230/**
1231 * pinctrl_force_sleep() - turn a given controller device into sleep state
1232 * @pctldev: pin controller device
1233 */
1234int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1235{
1236 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
1237 return pinctrl_select_state(pctldev->p, pctldev->hog_sleep);
1238 return 0;
1239}
1240EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1241
1242/**
1243 * pinctrl_force_default() - turn a given controller device into default state
1244 * @pctldev: pin controller device
1245 */
1246int pinctrl_force_default(struct pinctrl_dev *pctldev)
1247{
1248 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
1249 return pinctrl_select_state(pctldev->p, pctldev->hog_default);
1250 return 0;
1251}
1252EXPORT_SYMBOL_GPL(pinctrl_force_default);
1253
14005ee2
LW
1254#ifdef CONFIG_PM
1255
1256/**
f3333497 1257 * pinctrl_pm_select_state() - select pinctrl state for PM
14005ee2 1258 * @dev: device to select default state for
f3333497 1259 * @state: state to set
14005ee2 1260 */
f3333497
TL
1261static int pinctrl_pm_select_state(struct device *dev,
1262 struct pinctrl_state *state)
14005ee2
LW
1263{
1264 struct dev_pin_info *pins = dev->pins;
1265 int ret;
1266
f3333497
TL
1267 if (IS_ERR(state))
1268 return 0; /* No such state */
1269 ret = pinctrl_select_state(pins->p, state);
14005ee2 1270 if (ret)
f3333497
TL
1271 dev_err(dev, "failed to activate pinctrl state %s\n",
1272 state->name);
14005ee2
LW
1273 return ret;
1274}
f3333497
TL
1275
1276/**
1277 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1278 * @dev: device to select default state for
1279 */
1280int pinctrl_pm_select_default_state(struct device *dev)
1281{
1282 if (!dev->pins)
1283 return 0;
1284
1285 return pinctrl_pm_select_state(dev, dev->pins->default_state);
1286}
f472dead 1287EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
14005ee2
LW
1288
1289/**
1290 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1291 * @dev: device to select sleep state for
1292 */
1293int pinctrl_pm_select_sleep_state(struct device *dev)
1294{
f3333497 1295 if (!dev->pins)
14005ee2 1296 return 0;
f3333497
TL
1297
1298 return pinctrl_pm_select_state(dev, dev->pins->sleep_state);
14005ee2 1299}
f472dead 1300EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
14005ee2
LW
1301
1302/**
1303 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1304 * @dev: device to select idle state for
1305 */
1306int pinctrl_pm_select_idle_state(struct device *dev)
1307{
f3333497 1308 if (!dev->pins)
14005ee2 1309 return 0;
f3333497
TL
1310
1311 return pinctrl_pm_select_state(dev, dev->pins->idle_state);
14005ee2 1312}
f472dead 1313EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
14005ee2
LW
1314#endif
1315
2744e8af
LW
1316#ifdef CONFIG_DEBUG_FS
1317
1318static int pinctrl_pins_show(struct seq_file *s, void *what)
1319{
1320 struct pinctrl_dev *pctldev = s->private;
1321 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
706e8520 1322 unsigned i, pin;
2744e8af
LW
1323
1324 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
2744e8af 1325
42fed7ba 1326 mutex_lock(&pctldev->mutex);
57b676f9 1327
706e8520
CP
1328 /* The pin number can be retrived from the pin controller descriptor */
1329 for (i = 0; i < pctldev->desc->npins; i++) {
2744e8af
LW
1330 struct pin_desc *desc;
1331
706e8520 1332 pin = pctldev->desc->pins[i].number;
2744e8af
LW
1333 desc = pin_desc_get(pctldev, pin);
1334 /* Pin space may be sparse */
1335 if (desc == NULL)
1336 continue;
1337
1338 seq_printf(s, "pin %d (%s) ", pin,
1339 desc->name ? desc->name : "unnamed");
1340
1341 /* Driver-specific info per pin */
1342 if (ops->pin_dbg_show)
1343 ops->pin_dbg_show(pctldev, s, pin);
1344
1345 seq_puts(s, "\n");
1346 }
1347
42fed7ba 1348 mutex_unlock(&pctldev->mutex);
57b676f9 1349
2744e8af
LW
1350 return 0;
1351}
1352
1353static int pinctrl_groups_show(struct seq_file *s, void *what)
1354{
1355 struct pinctrl_dev *pctldev = s->private;
1356 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
d1e90e9e 1357 unsigned ngroups, selector = 0;
2744e8af 1358
42fed7ba
PC
1359 mutex_lock(&pctldev->mutex);
1360
d1e90e9e 1361 ngroups = ops->get_groups_count(pctldev);
57b676f9 1362
2744e8af 1363 seq_puts(s, "registered pin groups:\n");
d1e90e9e 1364 while (selector < ngroups) {
a5818a8b 1365 const unsigned *pins;
2744e8af
LW
1366 unsigned num_pins;
1367 const char *gname = ops->get_group_name(pctldev, selector);
dcb5dbc3 1368 const char *pname;
2744e8af
LW
1369 int ret;
1370 int i;
1371
1372 ret = ops->get_group_pins(pctldev, selector,
1373 &pins, &num_pins);
1374 if (ret)
1375 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1376 gname);
1377 else {
dcb5dbc3
DA
1378 seq_printf(s, "group: %s\n", gname);
1379 for (i = 0; i < num_pins; i++) {
1380 pname = pin_get_name(pctldev, pins[i]);
b4dd784b 1381 if (WARN_ON(!pname)) {
42fed7ba 1382 mutex_unlock(&pctldev->mutex);
dcb5dbc3 1383 return -EINVAL;
b4dd784b 1384 }
dcb5dbc3
DA
1385 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1386 }
1387 seq_puts(s, "\n");
2744e8af
LW
1388 }
1389 selector++;
1390 }
1391
42fed7ba 1392 mutex_unlock(&pctldev->mutex);
2744e8af
LW
1393
1394 return 0;
1395}
1396
1397static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1398{
1399 struct pinctrl_dev *pctldev = s->private;
1400 struct pinctrl_gpio_range *range = NULL;
1401
1402 seq_puts(s, "GPIO ranges handled:\n");
1403
42fed7ba 1404 mutex_lock(&pctldev->mutex);
57b676f9 1405
2744e8af 1406 /* Loop over the ranges */
2744e8af 1407 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
c8587eee
CR
1408 if (range->pins) {
1409 int a;
1410 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
1411 range->id, range->name,
1412 range->base, (range->base + range->npins - 1));
1413 for (a = 0; a < range->npins - 1; a++)
1414 seq_printf(s, "%u, ", range->pins[a]);
1415 seq_printf(s, "%u}\n", range->pins[a]);
1416 }
1417 else
1418 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1419 range->id, range->name,
1420 range->base, (range->base + range->npins - 1),
1421 range->pin_base,
1422 (range->pin_base + range->npins - 1));
2744e8af 1423 }
57b676f9 1424
42fed7ba 1425 mutex_unlock(&pctldev->mutex);
2744e8af
LW
1426
1427 return 0;
1428}
1429
1430static int pinctrl_devices_show(struct seq_file *s, void *what)
1431{
1432 struct pinctrl_dev *pctldev;
1433
ae6b4d85 1434 seq_puts(s, "name [pinmux] [pinconf]\n");
57b676f9 1435
42fed7ba 1436 mutex_lock(&pinctrldev_list_mutex);
57b676f9 1437
2744e8af
LW
1438 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1439 seq_printf(s, "%s ", pctldev->desc->name);
1440 if (pctldev->desc->pmxops)
ae6b4d85
LW
1441 seq_puts(s, "yes ");
1442 else
1443 seq_puts(s, "no ");
1444 if (pctldev->desc->confops)
2744e8af
LW
1445 seq_puts(s, "yes");
1446 else
1447 seq_puts(s, "no");
1448 seq_puts(s, "\n");
1449 }
57b676f9 1450
42fed7ba 1451 mutex_unlock(&pinctrldev_list_mutex);
2744e8af
LW
1452
1453 return 0;
1454}
1455
1e2082b5
SW
1456static inline const char *map_type(enum pinctrl_map_type type)
1457{
1458 static const char * const names[] = {
1459 "INVALID",
1460 "DUMMY_STATE",
1461 "MUX_GROUP",
1462 "CONFIGS_PIN",
1463 "CONFIGS_GROUP",
1464 };
1465
1466 if (type >= ARRAY_SIZE(names))
1467 return "UNKNOWN";
1468
1469 return names[type];
1470}
1471
3eedb437
SW
1472static int pinctrl_maps_show(struct seq_file *s, void *what)
1473{
1474 struct pinctrl_maps *maps_node;
1475 int i;
1476 struct pinctrl_map const *map;
1477
1478 seq_puts(s, "Pinctrl maps:\n");
1479
42fed7ba 1480 mutex_lock(&pinctrl_maps_mutex);
3eedb437 1481 for_each_maps(maps_node, i, map) {
1e2082b5
SW
1482 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1483 map->dev_name, map->name, map_type(map->type),
1484 map->type);
1485
1486 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1487 seq_printf(s, "controlling device %s\n",
1488 map->ctrl_dev_name);
1489
1490 switch (map->type) {
1491 case PIN_MAP_TYPE_MUX_GROUP:
1492 pinmux_show_map(s, map);
1493 break;
1494 case PIN_MAP_TYPE_CONFIGS_PIN:
1495 case PIN_MAP_TYPE_CONFIGS_GROUP:
1496 pinconf_show_map(s, map);
1497 break;
1498 default:
1499 break;
1500 }
1501
1502 seq_printf(s, "\n");
3eedb437 1503 }
42fed7ba 1504 mutex_unlock(&pinctrl_maps_mutex);
3eedb437
SW
1505
1506 return 0;
1507}
1508
befe5bdf
LW
1509static int pinctrl_show(struct seq_file *s, void *what)
1510{
1511 struct pinctrl *p;
6e5e959d 1512 struct pinctrl_state *state;
7ecdb16f 1513 struct pinctrl_setting *setting;
befe5bdf
LW
1514
1515 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
57b676f9 1516
42fed7ba 1517 mutex_lock(&pinctrl_list_mutex);
57b676f9 1518
befe5bdf 1519 list_for_each_entry(p, &pinctrl_list, node) {
6e5e959d
SW
1520 seq_printf(s, "device: %s current state: %s\n",
1521 dev_name(p->dev),
1522 p->state ? p->state->name : "none");
1523
1524 list_for_each_entry(state, &p->states, node) {
1525 seq_printf(s, " state: %s\n", state->name);
befe5bdf 1526
6e5e959d 1527 list_for_each_entry(setting, &state->settings, node) {
1e2082b5
SW
1528 struct pinctrl_dev *pctldev = setting->pctldev;
1529
1530 seq_printf(s, " type: %s controller %s ",
1531 map_type(setting->type),
1532 pinctrl_dev_get_name(pctldev));
1533
1534 switch (setting->type) {
1535 case PIN_MAP_TYPE_MUX_GROUP:
1536 pinmux_show_setting(s, setting);
1537 break;
1538 case PIN_MAP_TYPE_CONFIGS_PIN:
1539 case PIN_MAP_TYPE_CONFIGS_GROUP:
1540 pinconf_show_setting(s, setting);
1541 break;
1542 default:
1543 break;
1544 }
6e5e959d 1545 }
befe5bdf 1546 }
befe5bdf
LW
1547 }
1548
42fed7ba 1549 mutex_unlock(&pinctrl_list_mutex);
57b676f9 1550
befe5bdf
LW
1551 return 0;
1552}
1553
2744e8af
LW
1554static int pinctrl_pins_open(struct inode *inode, struct file *file)
1555{
1556 return single_open(file, pinctrl_pins_show, inode->i_private);
1557}
1558
1559static int pinctrl_groups_open(struct inode *inode, struct file *file)
1560{
1561 return single_open(file, pinctrl_groups_show, inode->i_private);
1562}
1563
1564static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1565{
1566 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1567}
1568
1569static int pinctrl_devices_open(struct inode *inode, struct file *file)
1570{
1571 return single_open(file, pinctrl_devices_show, NULL);
1572}
1573
3eedb437
SW
1574static int pinctrl_maps_open(struct inode *inode, struct file *file)
1575{
1576 return single_open(file, pinctrl_maps_show, NULL);
1577}
1578
befe5bdf
LW
1579static int pinctrl_open(struct inode *inode, struct file *file)
1580{
1581 return single_open(file, pinctrl_show, NULL);
1582}
1583
2744e8af
LW
1584static const struct file_operations pinctrl_pins_ops = {
1585 .open = pinctrl_pins_open,
1586 .read = seq_read,
1587 .llseek = seq_lseek,
1588 .release = single_release,
1589};
1590
1591static const struct file_operations pinctrl_groups_ops = {
1592 .open = pinctrl_groups_open,
1593 .read = seq_read,
1594 .llseek = seq_lseek,
1595 .release = single_release,
1596};
1597
1598static const struct file_operations pinctrl_gpioranges_ops = {
1599 .open = pinctrl_gpioranges_open,
1600 .read = seq_read,
1601 .llseek = seq_lseek,
1602 .release = single_release,
1603};
1604
3eedb437
SW
1605static const struct file_operations pinctrl_devices_ops = {
1606 .open = pinctrl_devices_open,
befe5bdf
LW
1607 .read = seq_read,
1608 .llseek = seq_lseek,
1609 .release = single_release,
1610};
1611
3eedb437
SW
1612static const struct file_operations pinctrl_maps_ops = {
1613 .open = pinctrl_maps_open,
2744e8af
LW
1614 .read = seq_read,
1615 .llseek = seq_lseek,
1616 .release = single_release,
1617};
1618
befe5bdf
LW
1619static const struct file_operations pinctrl_ops = {
1620 .open = pinctrl_open,
1621 .read = seq_read,
1622 .llseek = seq_lseek,
1623 .release = single_release,
1624};
1625
2744e8af
LW
1626static struct dentry *debugfs_root;
1627
1628static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1629{
02157160 1630 struct dentry *device_root;
2744e8af 1631
51cd24ee 1632 device_root = debugfs_create_dir(dev_name(pctldev->dev),
2744e8af 1633 debugfs_root);
02157160
TL
1634 pctldev->device_root = device_root;
1635
2744e8af
LW
1636 if (IS_ERR(device_root) || !device_root) {
1637 pr_warn("failed to create debugfs directory for %s\n",
51cd24ee 1638 dev_name(pctldev->dev));
2744e8af
LW
1639 return;
1640 }
1641 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1642 device_root, pctldev, &pinctrl_pins_ops);
1643 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1644 device_root, pctldev, &pinctrl_groups_ops);
1645 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1646 device_root, pctldev, &pinctrl_gpioranges_ops);
e7f2a444
FV
1647 if (pctldev->desc->pmxops)
1648 pinmux_init_device_debugfs(device_root, pctldev);
1649 if (pctldev->desc->confops)
1650 pinconf_init_device_debugfs(device_root, pctldev);
2744e8af
LW
1651}
1652
02157160
TL
1653static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1654{
1655 debugfs_remove_recursive(pctldev->device_root);
1656}
1657
2744e8af
LW
1658static void pinctrl_init_debugfs(void)
1659{
1660 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1661 if (IS_ERR(debugfs_root) || !debugfs_root) {
1662 pr_warn("failed to create debugfs directory\n");
1663 debugfs_root = NULL;
1664 return;
1665 }
1666
1667 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1668 debugfs_root, NULL, &pinctrl_devices_ops);
3eedb437
SW
1669 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1670 debugfs_root, NULL, &pinctrl_maps_ops);
befe5bdf
LW
1671 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1672 debugfs_root, NULL, &pinctrl_ops);
2744e8af
LW
1673}
1674
1675#else /* CONFIG_DEBUG_FS */
1676
1677static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1678{
1679}
1680
1681static void pinctrl_init_debugfs(void)
1682{
1683}
1684
02157160
TL
1685static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1686{
1687}
1688
2744e8af
LW
1689#endif
1690
d26bc49f
SW
1691static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1692{
1693 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1694
1695 if (!ops ||
d1e90e9e 1696 !ops->get_groups_count ||
d26bc49f
SW
1697 !ops->get_group_name ||
1698 !ops->get_group_pins)
1699 return -EINVAL;
1700
57291ce2
SW
1701 if (ops->dt_node_to_map && !ops->dt_free_map)
1702 return -EINVAL;
1703
d26bc49f
SW
1704 return 0;
1705}
1706
2744e8af
LW
1707/**
1708 * pinctrl_register() - register a pin controller device
1709 * @pctldesc: descriptor for this pin controller
1710 * @dev: parent device for this pin controller
1711 * @driver_data: private pin controller data for this pin controller
1712 */
1713struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1714 struct device *dev, void *driver_data)
1715{
2744e8af
LW
1716 struct pinctrl_dev *pctldev;
1717 int ret;
1718
da9aecb0 1719 if (!pctldesc)
2744e8af 1720 return NULL;
da9aecb0 1721 if (!pctldesc->name)
2744e8af
LW
1722 return NULL;
1723
02f5b989 1724 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
95dcd4ae
SW
1725 if (pctldev == NULL) {
1726 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
b9130b77 1727 return NULL;
95dcd4ae 1728 }
b9130b77
TL
1729
1730 /* Initialize pin control device struct */
1731 pctldev->owner = pctldesc->owner;
1732 pctldev->desc = pctldesc;
1733 pctldev->driver_data = driver_data;
1734 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
b9130b77 1735 INIT_LIST_HEAD(&pctldev->gpio_ranges);
b9130b77 1736 pctldev->dev = dev;
42fed7ba 1737 mutex_init(&pctldev->mutex);
b9130b77 1738
d26bc49f 1739 /* check core ops for sanity */
da9aecb0 1740 if (pinctrl_check_ops(pctldev)) {
ad6e1107 1741 dev_err(dev, "pinctrl ops lacks necessary functions\n");
d26bc49f
SW
1742 goto out_err;
1743 }
1744
2744e8af
LW
1745 /* If we're implementing pinmuxing, check the ops for sanity */
1746 if (pctldesc->pmxops) {
da9aecb0 1747 if (pinmux_check_ops(pctldev))
b9130b77 1748 goto out_err;
2744e8af
LW
1749 }
1750
ae6b4d85
LW
1751 /* If we're implementing pinconfig, check the ops for sanity */
1752 if (pctldesc->confops) {
da9aecb0 1753 if (pinconf_check_ops(pctldev))
b9130b77 1754 goto out_err;
ae6b4d85
LW
1755 }
1756
2744e8af 1757 /* Register all the pins */
ad6e1107 1758 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
2744e8af
LW
1759 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1760 if (ret) {
ad6e1107 1761 dev_err(dev, "error during pin registration\n");
2744e8af
LW
1762 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1763 pctldesc->npins);
51cd24ee 1764 goto out_err;
2744e8af
LW
1765 }
1766
42fed7ba 1767 mutex_lock(&pinctrldev_list_mutex);
8b9c139f 1768 list_add_tail(&pctldev->node, &pinctrldev_list);
42fed7ba
PC
1769 mutex_unlock(&pinctrldev_list_mutex);
1770
1771 pctldev->p = pinctrl_get(pctldev->dev);
57b676f9 1772
6e5e959d 1773 if (!IS_ERR(pctldev->p)) {
840a47ba 1774 pctldev->hog_default =
42fed7ba 1775 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
840a47ba 1776 if (IS_ERR(pctldev->hog_default)) {
ad6e1107
JC
1777 dev_dbg(dev, "failed to lookup the default state\n");
1778 } else {
42fed7ba 1779 if (pinctrl_select_state(pctldev->p,
840a47ba 1780 pctldev->hog_default))
ad6e1107
JC
1781 dev_err(dev,
1782 "failed to select default state\n");
ad6e1107 1783 }
840a47ba
JD
1784
1785 pctldev->hog_sleep =
42fed7ba 1786 pinctrl_lookup_state(pctldev->p,
840a47ba
JD
1787 PINCTRL_STATE_SLEEP);
1788 if (IS_ERR(pctldev->hog_sleep))
1789 dev_dbg(dev, "failed to lookup the sleep state\n");
6e5e959d 1790 }
57b676f9 1791
2304b473
SW
1792 pinctrl_init_device_debugfs(pctldev);
1793
2744e8af
LW
1794 return pctldev;
1795
51cd24ee 1796out_err:
42fed7ba 1797 mutex_destroy(&pctldev->mutex);
51cd24ee 1798 kfree(pctldev);
2744e8af
LW
1799 return NULL;
1800}
1801EXPORT_SYMBOL_GPL(pinctrl_register);
1802
1803/**
1804 * pinctrl_unregister() - unregister pinmux
1805 * @pctldev: pin controller to unregister
1806 *
1807 * Called by pinmux drivers to unregister a pinmux.
1808 */
1809void pinctrl_unregister(struct pinctrl_dev *pctldev)
1810{
5d589b09 1811 struct pinctrl_gpio_range *range, *n;
2744e8af
LW
1812 if (pctldev == NULL)
1813 return;
1814
42fed7ba
PC
1815 mutex_lock(&pinctrldev_list_mutex);
1816 mutex_lock(&pctldev->mutex);
57b676f9 1817
42fed7ba 1818 pinctrl_remove_device_debugfs(pctldev);
57b676f9 1819
6e5e959d 1820 if (!IS_ERR(pctldev->p))
42fed7ba 1821 pinctrl_put(pctldev->p);
57b676f9 1822
2744e8af 1823 /* TODO: check that no pinmuxes are still active? */
2744e8af 1824 list_del(&pctldev->node);
2744e8af
LW
1825 /* Destroy descriptor tree */
1826 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1827 pctldev->desc->npins);
5d589b09
DA
1828 /* remove gpio ranges map */
1829 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1830 list_del(&range->node);
1831
42fed7ba
PC
1832 mutex_unlock(&pctldev->mutex);
1833 mutex_destroy(&pctldev->mutex);
51cd24ee 1834 kfree(pctldev);
42fed7ba 1835 mutex_unlock(&pinctrldev_list_mutex);
2744e8af
LW
1836}
1837EXPORT_SYMBOL_GPL(pinctrl_unregister);
1838
1839static int __init pinctrl_init(void)
1840{
1841 pr_info("initialized pinctrl subsystem\n");
1842 pinctrl_init_debugfs();
1843 return 0;
1844}
1845
1846/* init early since many drivers really need to initialized pinmux early */
1847core_initcall(pinctrl_init);