pinctrl: core: Fix regression caused by delayed work for hogs
[GitHub/LineageOS/android_kernel_motorola_exynos9610.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,
cd8f61f1 228 const struct pinctrl_pin_desc *pin)
2744e8af
LW
229{
230 struct pin_desc *pindesc;
231
cd8f61f1 232 pindesc = pin_desc_get(pctldev, pin->number);
2744e8af 233 if (pindesc != NULL) {
cd8f61f1
MY
234 dev_err(pctldev->dev, "pin %d already registered\n",
235 pin->number);
2744e8af
LW
236 return -EINVAL;
237 }
238
239 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
2104d12d 240 if (!pindesc)
2744e8af 241 return -ENOMEM;
ae6b4d85 242
2744e8af
LW
243 /* Set owner */
244 pindesc->pctldev = pctldev;
245
9af1e44f 246 /* Copy basic pin info */
cd8f61f1
MY
247 if (pin->name) {
248 pindesc->name = pin->name;
ca53c5f1 249 } else {
cd8f61f1 250 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", pin->number);
eb26cc9c
SK
251 if (pindesc->name == NULL) {
252 kfree(pindesc);
ca53c5f1 253 return -ENOMEM;
eb26cc9c 254 }
ca53c5f1
LW
255 pindesc->dynamic_name = true;
256 }
2744e8af 257
cd8f61f1
MY
258 pindesc->drv_data = pin->drv_data;
259
260 radix_tree_insert(&pctldev->pin_desc_tree, pin->number, pindesc);
2744e8af 261 pr_debug("registered pin %d (%s) on %s\n",
cd8f61f1 262 pin->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++) {
cd8f61f1 274 ret = pinctrl_register_one_pin(pctldev, &pins[i]);
2744e8af
LW
275 if (ret)
276 return ret;
277 }
278
279 return 0;
280}
281
c8587eee
CR
282/**
283 * gpio_to_pin() - GPIO range GPIO number to pin number translation
284 * @range: GPIO range used for the translation
285 * @gpio: gpio pin to translate to a pin number
286 *
287 * Finds the pin number for a given GPIO using the specified GPIO range
288 * as a base for translation. The distinction between linear GPIO ranges
289 * and pin list based GPIO ranges is managed correctly by this function.
290 *
291 * This function assumes the gpio is part of the specified GPIO range, use
292 * only after making sure this is the case (e.g. by calling it on the
293 * result of successful pinctrl_get_device_gpio_range calls)!
294 */
295static inline int gpio_to_pin(struct pinctrl_gpio_range *range,
296 unsigned int gpio)
297{
298 unsigned int offset = gpio - range->base;
299 if (range->pins)
300 return range->pins[offset];
301 else
302 return range->pin_base + offset;
303}
304
2744e8af
LW
305/**
306 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
307 * @pctldev: pin controller device to check
308 * @gpio: gpio pin to check taken from the global GPIO pin space
309 *
310 * Tries to match a GPIO pin number to the ranges handled by a certain pin
311 * controller, return the range or NULL
312 */
313static struct pinctrl_gpio_range *
314pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
315{
316 struct pinctrl_gpio_range *range = NULL;
317
42fed7ba 318 mutex_lock(&pctldev->mutex);
2744e8af 319 /* Loop over the ranges */
2744e8af
LW
320 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
321 /* Check if we're in the valid range */
322 if (gpio >= range->base &&
323 gpio < range->base + range->npins) {
42fed7ba 324 mutex_unlock(&pctldev->mutex);
2744e8af
LW
325 return range;
326 }
327 }
42fed7ba 328 mutex_unlock(&pctldev->mutex);
2744e8af
LW
329 return NULL;
330}
331
51e13c24
HZ
332/**
333 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
334 * the same GPIO chip are in range
335 * @gpio: gpio pin to check taken from the global GPIO pin space
336 *
337 * This function is complement of pinctrl_match_gpio_range(). If the return
338 * value of pinctrl_match_gpio_range() is NULL, this function could be used
339 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
340 * of the same GPIO chip don't have back-end pinctrl interface.
341 * If the return value is true, it means that pinctrl device is ready & the
342 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
343 * is false, it means that pinctrl device may not be ready.
344 */
2afe8229 345#ifdef CONFIG_GPIOLIB
51e13c24
HZ
346static bool pinctrl_ready_for_gpio_range(unsigned gpio)
347{
348 struct pinctrl_dev *pctldev;
349 struct pinctrl_gpio_range *range = NULL;
350 struct gpio_chip *chip = gpio_to_chip(gpio);
351
942cde72
TL
352 if (WARN(!chip, "no gpio_chip for gpio%i?", gpio))
353 return false;
354
44d5f7bb
LW
355 mutex_lock(&pinctrldev_list_mutex);
356
51e13c24
HZ
357 /* Loop over the pin controllers */
358 list_for_each_entry(pctldev, &pinctrldev_list, node) {
359 /* Loop over the ranges */
5ffbe2e6 360 mutex_lock(&pctldev->mutex);
51e13c24
HZ
361 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
362 /* Check if any gpio range overlapped with gpio chip */
363 if (range->base + range->npins - 1 < chip->base ||
364 range->base > chip->base + chip->ngpio - 1)
365 continue;
5ffbe2e6 366 mutex_unlock(&pctldev->mutex);
44d5f7bb 367 mutex_unlock(&pinctrldev_list_mutex);
51e13c24
HZ
368 return true;
369 }
5ffbe2e6 370 mutex_unlock(&pctldev->mutex);
51e13c24 371 }
44d5f7bb
LW
372
373 mutex_unlock(&pinctrldev_list_mutex);
374
51e13c24
HZ
375 return false;
376}
2afe8229
HZ
377#else
378static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
379#endif
51e13c24 380
2744e8af
LW
381/**
382 * pinctrl_get_device_gpio_range() - find device for GPIO range
383 * @gpio: the pin to locate the pin controller for
384 * @outdev: the pin control device if found
385 * @outrange: the GPIO range if found
386 *
387 * Find the pin controller handling a certain GPIO pin from the pinspace of
388 * the GPIO subsystem, return the device and the matching GPIO range. Returns
4650b7cb
DA
389 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
390 * may still have not been registered.
2744e8af 391 */
4ecce45d
SW
392static int pinctrl_get_device_gpio_range(unsigned gpio,
393 struct pinctrl_dev **outdev,
394 struct pinctrl_gpio_range **outrange)
2744e8af
LW
395{
396 struct pinctrl_dev *pctldev = NULL;
397
f0059021
AL
398 mutex_lock(&pinctrldev_list_mutex);
399
2744e8af 400 /* Loop over the pin controllers */
2744e8af
LW
401 list_for_each_entry(pctldev, &pinctrldev_list, node) {
402 struct pinctrl_gpio_range *range;
403
404 range = pinctrl_match_gpio_range(pctldev, gpio);
405 if (range != NULL) {
406 *outdev = pctldev;
407 *outrange = range;
f0059021 408 mutex_unlock(&pinctrldev_list_mutex);
2744e8af
LW
409 return 0;
410 }
411 }
2744e8af 412
f0059021
AL
413 mutex_unlock(&pinctrldev_list_mutex);
414
4650b7cb 415 return -EPROBE_DEFER;
2744e8af
LW
416}
417
418/**
419 * pinctrl_add_gpio_range() - register a GPIO range for a controller
420 * @pctldev: pin controller device to add the range to
421 * @range: the GPIO range to add
422 *
423 * This adds a range of GPIOs to be handled by a certain pin controller. Call
424 * this to register handled ranges after registering your pin controller.
425 */
426void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
427 struct pinctrl_gpio_range *range)
428{
42fed7ba 429 mutex_lock(&pctldev->mutex);
8b9c139f 430 list_add_tail(&range->node, &pctldev->gpio_ranges);
42fed7ba 431 mutex_unlock(&pctldev->mutex);
2744e8af 432}
4ecce45d 433EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
2744e8af 434
3e5e00b6
DA
435void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
436 struct pinctrl_gpio_range *ranges,
437 unsigned nranges)
438{
439 int i;
440
441 for (i = 0; i < nranges; i++)
442 pinctrl_add_gpio_range(pctldev, &ranges[i]);
443}
444EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
445
192c369c 446struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
f23f1516
SH
447 struct pinctrl_gpio_range *range)
448{
42fed7ba
PC
449 struct pinctrl_dev *pctldev;
450
42fed7ba 451 pctldev = get_pinctrl_dev_from_devname(devname);
f23f1516 452
dfa97515
LW
453 /*
454 * If we can't find this device, let's assume that is because
455 * it has not probed yet, so the driver trying to register this
456 * range need to defer probing.
457 */
42fed7ba 458 if (!pctldev) {
dfa97515 459 return ERR_PTR(-EPROBE_DEFER);
42fed7ba 460 }
f23f1516 461 pinctrl_add_gpio_range(pctldev, range);
42fed7ba 462
f23f1516
SH
463 return pctldev;
464}
192c369c 465EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
f23f1516 466
586a87e6
CR
467int pinctrl_get_group_pins(struct pinctrl_dev *pctldev, const char *pin_group,
468 const unsigned **pins, unsigned *num_pins)
469{
470 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
471 int gs;
472
e5b3b2d9
AT
473 if (!pctlops->get_group_pins)
474 return -EINVAL;
475
586a87e6
CR
476 gs = pinctrl_get_group_selector(pctldev, pin_group);
477 if (gs < 0)
478 return gs;
479
480 return pctlops->get_group_pins(pctldev, gs, pins, num_pins);
481}
482EXPORT_SYMBOL_GPL(pinctrl_get_group_pins);
483
9afbefb2 484struct pinctrl_gpio_range *
b18537cd
JE
485pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev *pctldev,
486 unsigned int pin)
9afbefb2 487{
c8f50e86 488 struct pinctrl_gpio_range *range;
9afbefb2
LW
489
490 /* Loop over the ranges */
491 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
492 /* Check if we're in the valid range */
c8587eee
CR
493 if (range->pins) {
494 int a;
495 for (a = 0; a < range->npins; a++) {
496 if (range->pins[a] == pin)
b18537cd 497 return range;
c8587eee
CR
498 }
499 } else if (pin >= range->pin_base &&
c8f50e86 500 pin < range->pin_base + range->npins)
b18537cd 501 return range;
9afbefb2 502 }
b18537cd
JE
503
504 return NULL;
505}
506EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin_nolock);
507
508/**
509 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
510 * @pctldev: the pin controller device to look in
511 * @pin: a controller-local number to find the range for
512 */
513struct pinctrl_gpio_range *
514pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
515 unsigned int pin)
516{
517 struct pinctrl_gpio_range *range;
518
519 mutex_lock(&pctldev->mutex);
520 range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin);
42fed7ba 521 mutex_unlock(&pctldev->mutex);
b18537cd 522
c8f50e86 523 return range;
9afbefb2
LW
524}
525EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
526
7e10ee68
VK
527/**
528 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
529 * @pctldev: pin controller device to remove the range from
530 * @range: the GPIO range to remove
531 */
532void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
533 struct pinctrl_gpio_range *range)
534{
42fed7ba 535 mutex_lock(&pctldev->mutex);
7e10ee68 536 list_del(&range->node);
42fed7ba 537 mutex_unlock(&pctldev->mutex);
7e10ee68
VK
538}
539EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
540
c033a718 541#ifdef CONFIG_GENERIC_PINCTRL_GROUPS
c7059c5a
TL
542
543/**
544 * pinctrl_generic_get_group_count() - returns the number of pin groups
545 * @pctldev: pin controller device
546 */
547int pinctrl_generic_get_group_count(struct pinctrl_dev *pctldev)
548{
549 return pctldev->num_groups;
550}
551EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_count);
552
553/**
554 * pinctrl_generic_get_group_name() - returns the name of a pin group
555 * @pctldev: pin controller device
556 * @selector: group number
557 */
558const char *pinctrl_generic_get_group_name(struct pinctrl_dev *pctldev,
559 unsigned int selector)
560{
561 struct group_desc *group;
562
563 group = radix_tree_lookup(&pctldev->pin_group_tree,
564 selector);
565 if (!group)
566 return NULL;
567
568 return group->name;
569}
570EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_name);
571
572/**
573 * pinctrl_generic_get_group_pins() - gets the pin group pins
574 * @pctldev: pin controller device
575 * @selector: group number
576 * @pins: pins in the group
577 * @num_pins: number of pins in the group
578 */
579int pinctrl_generic_get_group_pins(struct pinctrl_dev *pctldev,
580 unsigned int selector,
581 const unsigned int **pins,
582 unsigned int *num_pins)
583{
584 struct group_desc *group;
585
586 group = radix_tree_lookup(&pctldev->pin_group_tree,
587 selector);
588 if (!group) {
589 dev_err(pctldev->dev, "%s could not find pingroup%i\n",
590 __func__, selector);
591 return -EINVAL;
592 }
593
594 *pins = group->pins;
595 *num_pins = group->num_pins;
596
597 return 0;
598}
599EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_pins);
600
601/**
602 * pinctrl_generic_get_group() - returns a pin group based on the number
603 * @pctldev: pin controller device
604 * @gselector: group number
605 */
606struct group_desc *pinctrl_generic_get_group(struct pinctrl_dev *pctldev,
607 unsigned int selector)
608{
609 struct group_desc *group;
610
611 group = radix_tree_lookup(&pctldev->pin_group_tree,
612 selector);
613 if (!group)
614 return NULL;
615
616 return group;
617}
618EXPORT_SYMBOL_GPL(pinctrl_generic_get_group);
619
620/**
621 * pinctrl_generic_add_group() - adds a new pin group
622 * @pctldev: pin controller device
623 * @name: name of the pin group
624 * @pins: pins in the pin group
625 * @num_pins: number of pins in the pin group
626 * @data: pin controller driver specific data
627 *
628 * Note that the caller must take care of locking.
629 */
630int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name,
631 int *pins, int num_pins, void *data)
632{
633 struct group_desc *group;
634
635 group = devm_kzalloc(pctldev->dev, sizeof(*group), GFP_KERNEL);
636 if (!group)
637 return -ENOMEM;
638
639 group->name = name;
640 group->pins = pins;
641 group->num_pins = num_pins;
642 group->data = data;
643
644 radix_tree_insert(&pctldev->pin_group_tree, pctldev->num_groups,
645 group);
646
647 pctldev->num_groups++;
648
649 return 0;
650}
651EXPORT_SYMBOL_GPL(pinctrl_generic_add_group);
652
653/**
654 * pinctrl_generic_remove_group() - removes a numbered pin group
655 * @pctldev: pin controller device
656 * @selector: group number
657 *
658 * Note that the caller must take care of locking.
659 */
660int pinctrl_generic_remove_group(struct pinctrl_dev *pctldev,
661 unsigned int selector)
662{
663 struct group_desc *group;
664
665 group = radix_tree_lookup(&pctldev->pin_group_tree,
666 selector);
667 if (!group)
668 return -ENOENT;
669
670 radix_tree_delete(&pctldev->pin_group_tree, selector);
671 devm_kfree(pctldev->dev, group);
672
673 pctldev->num_groups--;
674
675 return 0;
676}
677EXPORT_SYMBOL_GPL(pinctrl_generic_remove_group);
678
679/**
680 * pinctrl_generic_free_groups() - removes all pin groups
681 * @pctldev: pin controller device
682 *
683 * Note that the caller must take care of locking.
684 */
685static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
686{
687 struct radix_tree_iter iter;
688 struct group_desc *group;
689 unsigned long *indices;
690 void **slot;
691 int i = 0;
692
693 indices = devm_kzalloc(pctldev->dev, sizeof(*indices) *
694 pctldev->num_groups, GFP_KERNEL);
695 if (!indices)
696 return;
697
698 radix_tree_for_each_slot(slot, &pctldev->pin_group_tree, &iter, 0)
699 indices[i++] = iter.index;
700
701 for (i = 0; i < pctldev->num_groups; i++) {
702 group = radix_tree_lookup(&pctldev->pin_group_tree,
703 indices[i]);
704 radix_tree_delete(&pctldev->pin_group_tree, indices[i]);
705 devm_kfree(pctldev->dev, group);
706 }
707
708 pctldev->num_groups = 0;
709}
710
711#else
712static inline void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
713{
714}
c033a718 715#endif /* CONFIG_GENERIC_PINCTRL_GROUPS */
c7059c5a 716
7afde8ba
LW
717/**
718 * pinctrl_get_group_selector() - returns the group selector for a group
719 * @pctldev: the pin controller handling the group
720 * @pin_group: the pin group to look up
721 */
722int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
723 const char *pin_group)
724{
725 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
d1e90e9e 726 unsigned ngroups = pctlops->get_groups_count(pctldev);
7afde8ba
LW
727 unsigned group_selector = 0;
728
d1e90e9e 729 while (group_selector < ngroups) {
7afde8ba
LW
730 const char *gname = pctlops->get_group_name(pctldev,
731 group_selector);
732 if (!strcmp(gname, pin_group)) {
51cd24ee 733 dev_dbg(pctldev->dev,
7afde8ba
LW
734 "found group selector %u for %s\n",
735 group_selector,
736 pin_group);
737 return group_selector;
738 }
739
740 group_selector++;
741 }
742
51cd24ee 743 dev_err(pctldev->dev, "does not have pin group %s\n",
7afde8ba
LW
744 pin_group);
745
746 return -EINVAL;
747}
748
befe5bdf 749/**
b217e438 750 * pinctrl_request_gpio() - request a single pin to be used as GPIO
befe5bdf
LW
751 * @gpio: the GPIO pin number from the GPIO subsystem number space
752 *
753 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
754 * as part of their gpio_request() semantics, platforms and individual drivers
755 * shall *NOT* request GPIO pins to be muxed in.
756 */
757int pinctrl_request_gpio(unsigned gpio)
758{
759 struct pinctrl_dev *pctldev;
760 struct pinctrl_gpio_range *range;
761 int ret;
762 int pin;
763
764 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
57b676f9 765 if (ret) {
51e13c24
HZ
766 if (pinctrl_ready_for_gpio_range(gpio))
767 ret = 0;
4650b7cb 768 return ret;
57b676f9 769 }
befe5bdf 770
9b77ace4
AL
771 mutex_lock(&pctldev->mutex);
772
befe5bdf 773 /* Convert to the pin controllers number space */
c8587eee 774 pin = gpio_to_pin(range, gpio);
befe5bdf 775
57b676f9
SW
776 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
777
9b77ace4
AL
778 mutex_unlock(&pctldev->mutex);
779
57b676f9 780 return ret;
befe5bdf
LW
781}
782EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
783
784/**
785 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
786 * @gpio: the GPIO pin number from the GPIO subsystem number space
787 *
788 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
789 * as part of their gpio_free() semantics, platforms and individual drivers
790 * shall *NOT* request GPIO pins to be muxed out.
791 */
792void pinctrl_free_gpio(unsigned gpio)
793{
794 struct pinctrl_dev *pctldev;
795 struct pinctrl_gpio_range *range;
796 int ret;
797 int pin;
798
799 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
57b676f9 800 if (ret) {
befe5bdf 801 return;
57b676f9 802 }
42fed7ba 803 mutex_lock(&pctldev->mutex);
befe5bdf
LW
804
805 /* Convert to the pin controllers number space */
c8587eee 806 pin = gpio_to_pin(range, gpio);
befe5bdf 807
57b676f9
SW
808 pinmux_free_gpio(pctldev, pin, range);
809
42fed7ba 810 mutex_unlock(&pctldev->mutex);
befe5bdf
LW
811}
812EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
813
814static int pinctrl_gpio_direction(unsigned gpio, bool input)
815{
816 struct pinctrl_dev *pctldev;
817 struct pinctrl_gpio_range *range;
818 int ret;
819 int pin;
820
821 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
42fed7ba 822 if (ret) {
befe5bdf 823 return ret;
42fed7ba
PC
824 }
825
826 mutex_lock(&pctldev->mutex);
befe5bdf
LW
827
828 /* Convert to the pin controllers number space */
c8587eee 829 pin = gpio_to_pin(range, gpio);
42fed7ba
PC
830 ret = pinmux_gpio_direction(pctldev, range, pin, input);
831
832 mutex_unlock(&pctldev->mutex);
befe5bdf 833
42fed7ba 834 return ret;
befe5bdf
LW
835}
836
837/**
838 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
839 * @gpio: the GPIO pin number from the GPIO subsystem number space
840 *
841 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
842 * as part of their gpio_direction_input() semantics, platforms and individual
843 * drivers shall *NOT* touch pin control GPIO calls.
844 */
845int pinctrl_gpio_direction_input(unsigned gpio)
846{
42fed7ba 847 return pinctrl_gpio_direction(gpio, true);
befe5bdf
LW
848}
849EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
850
851/**
852 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
853 * @gpio: the GPIO pin number from the GPIO subsystem number space
854 *
855 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
856 * as part of their gpio_direction_output() semantics, platforms and individual
857 * drivers shall *NOT* touch pin control GPIO calls.
858 */
859int pinctrl_gpio_direction_output(unsigned gpio)
860{
42fed7ba 861 return pinctrl_gpio_direction(gpio, false);
befe5bdf
LW
862}
863EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
864
6e5e959d
SW
865static struct pinctrl_state *find_state(struct pinctrl *p,
866 const char *name)
befe5bdf 867{
6e5e959d
SW
868 struct pinctrl_state *state;
869
870 list_for_each_entry(state, &p->states, node)
871 if (!strcmp(state->name, name))
872 return state;
873
874 return NULL;
875}
876
877static struct pinctrl_state *create_state(struct pinctrl *p,
878 const char *name)
879{
880 struct pinctrl_state *state;
881
882 state = kzalloc(sizeof(*state), GFP_KERNEL);
2104d12d 883 if (!state)
6e5e959d 884 return ERR_PTR(-ENOMEM);
6e5e959d
SW
885
886 state->name = name;
887 INIT_LIST_HEAD(&state->settings);
888
889 list_add_tail(&state->node, &p->states);
890
891 return state;
892}
893
99e4f675
TL
894static int add_setting(struct pinctrl *p, struct pinctrl_dev *pctldev,
895 struct pinctrl_map const *map)
6e5e959d
SW
896{
897 struct pinctrl_state *state;
7ecdb16f 898 struct pinctrl_setting *setting;
6e5e959d 899 int ret;
befe5bdf 900
6e5e959d
SW
901 state = find_state(p, map->name);
902 if (!state)
903 state = create_state(p, map->name);
904 if (IS_ERR(state))
905 return PTR_ERR(state);
befe5bdf 906
1e2082b5
SW
907 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
908 return 0;
909
6e5e959d 910 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
2104d12d 911 if (!setting)
6e5e959d 912 return -ENOMEM;
befe5bdf 913
1e2082b5
SW
914 setting->type = map->type;
915
99e4f675
TL
916 if (pctldev)
917 setting->pctldev = pctldev;
918 else
919 setting->pctldev =
920 get_pinctrl_dev_from_devname(map->ctrl_dev_name);
6e5e959d 921 if (setting->pctldev == NULL) {
6e5e959d 922 kfree(setting);
89216494
LW
923 /* Do not defer probing of hogs (circular loop) */
924 if (!strcmp(map->ctrl_dev_name, map->dev_name))
925 return -ENODEV;
c05127c4
LW
926 /*
927 * OK let us guess that the driver is not there yet, and
928 * let's defer obtaining this pinctrl handle to later...
929 */
89216494
LW
930 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
931 map->ctrl_dev_name);
c05127c4 932 return -EPROBE_DEFER;
6e5e959d
SW
933 }
934
1a78958d
LW
935 setting->dev_name = map->dev_name;
936
1e2082b5
SW
937 switch (map->type) {
938 case PIN_MAP_TYPE_MUX_GROUP:
939 ret = pinmux_map_to_setting(map, setting);
940 break;
941 case PIN_MAP_TYPE_CONFIGS_PIN:
942 case PIN_MAP_TYPE_CONFIGS_GROUP:
943 ret = pinconf_map_to_setting(map, setting);
944 break;
945 default:
946 ret = -EINVAL;
947 break;
948 }
6e5e959d
SW
949 if (ret < 0) {
950 kfree(setting);
951 return ret;
952 }
953
954 list_add_tail(&setting->node, &state->settings);
955
956 return 0;
957}
958
959static struct pinctrl *find_pinctrl(struct device *dev)
960{
961 struct pinctrl *p;
962
42fed7ba 963 mutex_lock(&pinctrl_list_mutex);
1e2082b5 964 list_for_each_entry(p, &pinctrl_list, node)
42fed7ba
PC
965 if (p->dev == dev) {
966 mutex_unlock(&pinctrl_list_mutex);
6e5e959d 967 return p;
42fed7ba 968 }
6e5e959d 969
42fed7ba 970 mutex_unlock(&pinctrl_list_mutex);
6e5e959d
SW
971 return NULL;
972}
973
42fed7ba 974static void pinctrl_free(struct pinctrl *p, bool inlist);
6e5e959d 975
99e4f675
TL
976static struct pinctrl *create_pinctrl(struct device *dev,
977 struct pinctrl_dev *pctldev)
6e5e959d
SW
978{
979 struct pinctrl *p;
980 const char *devname;
981 struct pinctrl_maps *maps_node;
982 int i;
983 struct pinctrl_map const *map;
984 int ret;
befe5bdf
LW
985
986 /*
987 * create the state cookie holder struct pinctrl for each
988 * mapping, this is what consumers will get when requesting
989 * a pin control handle with pinctrl_get()
990 */
02f5b989 991 p = kzalloc(sizeof(*p), GFP_KERNEL);
2104d12d 992 if (!p)
befe5bdf 993 return ERR_PTR(-ENOMEM);
7ecdb16f 994 p->dev = dev;
6e5e959d 995 INIT_LIST_HEAD(&p->states);
57291ce2
SW
996 INIT_LIST_HEAD(&p->dt_maps);
997
99e4f675 998 ret = pinctrl_dt_to_map(p, pctldev);
57291ce2
SW
999 if (ret < 0) {
1000 kfree(p);
1001 return ERR_PTR(ret);
1002 }
6e5e959d
SW
1003
1004 devname = dev_name(dev);
befe5bdf 1005
42fed7ba 1006 mutex_lock(&pinctrl_maps_mutex);
befe5bdf 1007 /* Iterate over the pin control maps to locate the right ones */
b2b3e66e 1008 for_each_maps(maps_node, i, map) {
7ecdb16f
SW
1009 /* Map must be for this device */
1010 if (strcmp(map->dev_name, devname))
1011 continue;
1012
99e4f675 1013 ret = add_setting(p, pctldev, map);
89216494
LW
1014 /*
1015 * At this point the adding of a setting may:
1016 *
1017 * - Defer, if the pinctrl device is not yet available
1018 * - Fail, if the pinctrl device is not yet available,
1019 * AND the setting is a hog. We cannot defer that, since
1020 * the hog will kick in immediately after the device
1021 * is registered.
1022 *
1023 * If the error returned was not -EPROBE_DEFER then we
1024 * accumulate the errors to see if we end up with
1025 * an -EPROBE_DEFER later, as that is the worst case.
1026 */
1027 if (ret == -EPROBE_DEFER) {
42fed7ba
PC
1028 pinctrl_free(p, false);
1029 mutex_unlock(&pinctrl_maps_mutex);
6e5e959d 1030 return ERR_PTR(ret);
7ecdb16f 1031 }
befe5bdf 1032 }
42fed7ba
PC
1033 mutex_unlock(&pinctrl_maps_mutex);
1034
89216494
LW
1035 if (ret < 0) {
1036 /* If some other error than deferral occured, return here */
42fed7ba 1037 pinctrl_free(p, false);
89216494
LW
1038 return ERR_PTR(ret);
1039 }
befe5bdf 1040
ab78029e
LW
1041 kref_init(&p->users);
1042
b0666ba4 1043 /* Add the pinctrl handle to the global list */
7b320cb1 1044 mutex_lock(&pinctrl_list_mutex);
8b9c139f 1045 list_add_tail(&p->node, &pinctrl_list);
7b320cb1 1046 mutex_unlock(&pinctrl_list_mutex);
befe5bdf
LW
1047
1048 return p;
6e5e959d 1049}
7ecdb16f 1050
42fed7ba
PC
1051/**
1052 * pinctrl_get() - retrieves the pinctrl handle for a device
1053 * @dev: the device to obtain the handle for
1054 */
1055struct pinctrl *pinctrl_get(struct device *dev)
6e5e959d
SW
1056{
1057 struct pinctrl *p;
7ecdb16f 1058
6e5e959d
SW
1059 if (WARN_ON(!dev))
1060 return ERR_PTR(-EINVAL);
1061
ab78029e
LW
1062 /*
1063 * See if somebody else (such as the device core) has already
1064 * obtained a handle to the pinctrl for this device. In that case,
1065 * return another pointer to it.
1066 */
6e5e959d 1067 p = find_pinctrl(dev);
ab78029e
LW
1068 if (p != NULL) {
1069 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
1070 kref_get(&p->users);
1071 return p;
1072 }
7ecdb16f 1073
99e4f675 1074 return create_pinctrl(dev, NULL);
befe5bdf
LW
1075}
1076EXPORT_SYMBOL_GPL(pinctrl_get);
1077
d3cee830
RG
1078static void pinctrl_free_setting(bool disable_setting,
1079 struct pinctrl_setting *setting)
1080{
1081 switch (setting->type) {
1082 case PIN_MAP_TYPE_MUX_GROUP:
1083 if (disable_setting)
1084 pinmux_disable_setting(setting);
1085 pinmux_free_setting(setting);
1086 break;
1087 case PIN_MAP_TYPE_CONFIGS_PIN:
1088 case PIN_MAP_TYPE_CONFIGS_GROUP:
1089 pinconf_free_setting(setting);
1090 break;
1091 default:
1092 break;
1093 }
1094}
1095
42fed7ba 1096static void pinctrl_free(struct pinctrl *p, bool inlist)
befe5bdf 1097{
6e5e959d
SW
1098 struct pinctrl_state *state, *n1;
1099 struct pinctrl_setting *setting, *n2;
1100
42fed7ba 1101 mutex_lock(&pinctrl_list_mutex);
6e5e959d
SW
1102 list_for_each_entry_safe(state, n1, &p->states, node) {
1103 list_for_each_entry_safe(setting, n2, &state->settings, node) {
d3cee830 1104 pinctrl_free_setting(state == p->state, setting);
6e5e959d
SW
1105 list_del(&setting->node);
1106 kfree(setting);
1107 }
1108 list_del(&state->node);
1109 kfree(state);
7ecdb16f 1110 }
befe5bdf 1111
57291ce2
SW
1112 pinctrl_dt_free_maps(p);
1113
6e5e959d
SW
1114 if (inlist)
1115 list_del(&p->node);
befe5bdf 1116 kfree(p);
42fed7ba 1117 mutex_unlock(&pinctrl_list_mutex);
befe5bdf 1118}
befe5bdf
LW
1119
1120/**
ab78029e
LW
1121 * pinctrl_release() - release the pinctrl handle
1122 * @kref: the kref in the pinctrl being released
1123 */
2917e833 1124static void pinctrl_release(struct kref *kref)
ab78029e
LW
1125{
1126 struct pinctrl *p = container_of(kref, struct pinctrl, users);
1127
42fed7ba 1128 pinctrl_free(p, true);
ab78029e
LW
1129}
1130
1131/**
1132 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
6e5e959d 1133 * @p: the pinctrl handle to release
befe5bdf 1134 */
57b676f9
SW
1135void pinctrl_put(struct pinctrl *p)
1136{
ab78029e 1137 kref_put(&p->users, pinctrl_release);
57b676f9
SW
1138}
1139EXPORT_SYMBOL_GPL(pinctrl_put);
1140
42fed7ba
PC
1141/**
1142 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
1143 * @p: the pinctrl handle to retrieve the state from
1144 * @name: the state name to retrieve
1145 */
1146struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
1147 const char *name)
befe5bdf 1148{
6e5e959d 1149 struct pinctrl_state *state;
befe5bdf 1150
6e5e959d 1151 state = find_state(p, name);
5b3aa5f7
DA
1152 if (!state) {
1153 if (pinctrl_dummy_state) {
1154 /* create dummy state */
1155 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
1156 name);
1157 state = create_state(p, name);
d599bfb3
RG
1158 } else
1159 state = ERR_PTR(-ENODEV);
5b3aa5f7 1160 }
57b676f9 1161
6e5e959d 1162 return state;
befe5bdf 1163}
42fed7ba 1164EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
befe5bdf
LW
1165
1166/**
42fed7ba
PC
1167 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
1168 * @p: the pinctrl handle for the device that requests configuration
1169 * @state: the state handle to select/activate/program
befe5bdf 1170 */
42fed7ba 1171int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
befe5bdf 1172{
6e5e959d 1173 struct pinctrl_setting *setting, *setting2;
50cf7c8a 1174 struct pinctrl_state *old_state = p->state;
6e5e959d 1175 int ret;
7ecdb16f 1176
6e5e959d
SW
1177 if (p->state == state)
1178 return 0;
befe5bdf 1179
6e5e959d
SW
1180 if (p->state) {
1181 /*
2243a87d
FW
1182 * For each pinmux setting in the old state, forget SW's record
1183 * of mux owner for that pingroup. Any pingroups which are
1184 * still owned by the new state will be re-acquired by the call
1185 * to pinmux_enable_setting() in the loop below.
6e5e959d
SW
1186 */
1187 list_for_each_entry(setting, &p->state->settings, node) {
1e2082b5
SW
1188 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
1189 continue;
2243a87d 1190 pinmux_disable_setting(setting);
6e5e959d
SW
1191 }
1192 }
1193
3102a76c 1194 p->state = NULL;
6e5e959d
SW
1195
1196 /* Apply all the settings for the new state */
1197 list_for_each_entry(setting, &state->settings, node) {
1e2082b5
SW
1198 switch (setting->type) {
1199 case PIN_MAP_TYPE_MUX_GROUP:
1200 ret = pinmux_enable_setting(setting);
1201 break;
1202 case PIN_MAP_TYPE_CONFIGS_PIN:
1203 case PIN_MAP_TYPE_CONFIGS_GROUP:
1204 ret = pinconf_apply_setting(setting);
1205 break;
1206 default:
1207 ret = -EINVAL;
1208 break;
1209 }
3102a76c 1210
42fed7ba 1211 if (ret < 0) {
3102a76c 1212 goto unapply_new_state;
42fed7ba 1213 }
befe5bdf 1214 }
6e5e959d 1215
3102a76c
RG
1216 p->state = state;
1217
6e5e959d 1218 return 0;
3102a76c
RG
1219
1220unapply_new_state:
da58751c 1221 dev_err(p->dev, "Error applying setting, reverse things back\n");
3102a76c 1222
3102a76c
RG
1223 list_for_each_entry(setting2, &state->settings, node) {
1224 if (&setting2->node == &setting->node)
1225 break;
af606177
RG
1226 /*
1227 * All we can do here is pinmux_disable_setting.
1228 * That means that some pins are muxed differently now
1229 * than they were before applying the setting (We can't
1230 * "unmux a pin"!), but it's not a big deal since the pins
1231 * are free to be muxed by another apply_setting.
1232 */
1233 if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
1234 pinmux_disable_setting(setting2);
3102a76c 1235 }
8009d5ff 1236
385d9424
RG
1237 /* There's no infinite recursive loop here because p->state is NULL */
1238 if (old_state)
42fed7ba 1239 pinctrl_select_state(p, old_state);
6e5e959d
SW
1240
1241 return ret;
befe5bdf 1242}
6e5e959d 1243EXPORT_SYMBOL_GPL(pinctrl_select_state);
befe5bdf 1244
6d4ca1fb
SW
1245static void devm_pinctrl_release(struct device *dev, void *res)
1246{
1247 pinctrl_put(*(struct pinctrl **)res);
1248}
1249
1250/**
1251 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
1252 * @dev: the device to obtain the handle for
1253 *
1254 * If there is a need to explicitly destroy the returned struct pinctrl,
1255 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1256 */
1257struct pinctrl *devm_pinctrl_get(struct device *dev)
1258{
1259 struct pinctrl **ptr, *p;
1260
1261 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
1262 if (!ptr)
1263 return ERR_PTR(-ENOMEM);
1264
1265 p = pinctrl_get(dev);
1266 if (!IS_ERR(p)) {
1267 *ptr = p;
1268 devres_add(dev, ptr);
1269 } else {
1270 devres_free(ptr);
1271 }
1272
1273 return p;
1274}
1275EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1276
1277static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1278{
1279 struct pinctrl **p = res;
1280
1281 return *p == data;
1282}
1283
1284/**
1285 * devm_pinctrl_put() - Resource managed pinctrl_put()
1286 * @p: the pinctrl handle to release
1287 *
1288 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1289 * this function will not need to be called and the resource management
1290 * code will ensure that the resource is freed.
1291 */
1292void devm_pinctrl_put(struct pinctrl *p)
1293{
a72149e8 1294 WARN_ON(devres_release(p->dev, devm_pinctrl_release,
6d4ca1fb 1295 devm_pinctrl_match, p));
6d4ca1fb
SW
1296}
1297EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1298
57291ce2 1299int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
c5272a28 1300 bool dup)
befe5bdf 1301{
1e2082b5 1302 int i, ret;
b2b3e66e 1303 struct pinctrl_maps *maps_node;
befe5bdf 1304
7e9236ff 1305 pr_debug("add %u pinctrl maps\n", num_maps);
befe5bdf
LW
1306
1307 /* First sanity check the new mapping */
1308 for (i = 0; i < num_maps; i++) {
1e2082b5
SW
1309 if (!maps[i].dev_name) {
1310 pr_err("failed to register map %s (%d): no device given\n",
1311 maps[i].name, i);
1312 return -EINVAL;
1313 }
1314
befe5bdf
LW
1315 if (!maps[i].name) {
1316 pr_err("failed to register map %d: no map name given\n",
95dcd4ae 1317 i);
befe5bdf
LW
1318 return -EINVAL;
1319 }
1320
1e2082b5
SW
1321 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1322 !maps[i].ctrl_dev_name) {
befe5bdf
LW
1323 pr_err("failed to register map %s (%d): no pin control device given\n",
1324 maps[i].name, i);
1325 return -EINVAL;
1326 }
1327
1e2082b5
SW
1328 switch (maps[i].type) {
1329 case PIN_MAP_TYPE_DUMMY_STATE:
1330 break;
1331 case PIN_MAP_TYPE_MUX_GROUP:
1332 ret = pinmux_validate_map(&maps[i], i);
1333 if (ret < 0)
fde04f41 1334 return ret;
1e2082b5
SW
1335 break;
1336 case PIN_MAP_TYPE_CONFIGS_PIN:
1337 case PIN_MAP_TYPE_CONFIGS_GROUP:
1338 ret = pinconf_validate_map(&maps[i], i);
1339 if (ret < 0)
fde04f41 1340 return ret;
1e2082b5
SW
1341 break;
1342 default:
1343 pr_err("failed to register map %s (%d): invalid type given\n",
95dcd4ae 1344 maps[i].name, i);
1681f5ae
SW
1345 return -EINVAL;
1346 }
befe5bdf
LW
1347 }
1348
b2b3e66e 1349 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
2104d12d 1350 if (!maps_node)
b2b3e66e 1351 return -ENOMEM;
befe5bdf 1352
b2b3e66e 1353 maps_node->num_maps = num_maps;
57291ce2
SW
1354 if (dup) {
1355 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
1356 GFP_KERNEL);
1357 if (!maps_node->maps) {
1358 pr_err("failed to duplicate mapping table\n");
1359 kfree(maps_node);
1360 return -ENOMEM;
1361 }
1362 } else {
1363 maps_node->maps = maps;
befe5bdf
LW
1364 }
1365
c5272a28 1366 mutex_lock(&pinctrl_maps_mutex);
b2b3e66e 1367 list_add_tail(&maps_node->node, &pinctrl_maps);
c5272a28 1368 mutex_unlock(&pinctrl_maps_mutex);
b2b3e66e 1369
befe5bdf
LW
1370 return 0;
1371}
1372
57291ce2
SW
1373/**
1374 * pinctrl_register_mappings() - register a set of pin controller mappings
1375 * @maps: the pincontrol mappings table to register. This should probably be
1376 * marked with __initdata so it can be discarded after boot. This
1377 * function will perform a shallow copy for the mapping entries.
1378 * @num_maps: the number of maps in the mapping table
1379 */
1380int pinctrl_register_mappings(struct pinctrl_map const *maps,
1381 unsigned num_maps)
1382{
c5272a28 1383 return pinctrl_register_map(maps, num_maps, true);
57291ce2
SW
1384}
1385
1386void pinctrl_unregister_map(struct pinctrl_map const *map)
1387{
1388 struct pinctrl_maps *maps_node;
1389
42fed7ba 1390 mutex_lock(&pinctrl_maps_mutex);
57291ce2
SW
1391 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1392 if (maps_node->maps == map) {
1393 list_del(&maps_node->node);
db6c2c69 1394 kfree(maps_node);
42fed7ba 1395 mutex_unlock(&pinctrl_maps_mutex);
57291ce2
SW
1396 return;
1397 }
1398 }
42fed7ba 1399 mutex_unlock(&pinctrl_maps_mutex);
57291ce2
SW
1400}
1401
840a47ba
JD
1402/**
1403 * pinctrl_force_sleep() - turn a given controller device into sleep state
1404 * @pctldev: pin controller device
1405 */
1406int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1407{
1408 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
1409 return pinctrl_select_state(pctldev->p, pctldev->hog_sleep);
1410 return 0;
1411}
1412EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1413
1414/**
1415 * pinctrl_force_default() - turn a given controller device into default state
1416 * @pctldev: pin controller device
1417 */
1418int pinctrl_force_default(struct pinctrl_dev *pctldev)
1419{
1420 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
1421 return pinctrl_select_state(pctldev->p, pctldev->hog_default);
1422 return 0;
1423}
1424EXPORT_SYMBOL_GPL(pinctrl_force_default);
1425
ef0eebc0
DA
1426/**
1427 * pinctrl_init_done() - tell pinctrl probe is done
1428 *
1429 * We'll use this time to switch the pins from "init" to "default" unless the
1430 * driver selected some other state.
1431 *
1432 * @dev: device to that's done probing
1433 */
1434int pinctrl_init_done(struct device *dev)
1435{
1436 struct dev_pin_info *pins = dev->pins;
1437 int ret;
1438
1439 if (!pins)
1440 return 0;
1441
1442 if (IS_ERR(pins->init_state))
1443 return 0; /* No such state */
1444
1445 if (pins->p->state != pins->init_state)
1446 return 0; /* Not at init anyway */
1447
1448 if (IS_ERR(pins->default_state))
1449 return 0; /* No default state */
1450
1451 ret = pinctrl_select_state(pins->p, pins->default_state);
1452 if (ret)
1453 dev_err(dev, "failed to activate default pinctrl state\n");
1454
1455 return ret;
1456}
1457
14005ee2
LW
1458#ifdef CONFIG_PM
1459
1460/**
f3333497 1461 * pinctrl_pm_select_state() - select pinctrl state for PM
14005ee2 1462 * @dev: device to select default state for
f3333497 1463 * @state: state to set
14005ee2 1464 */
f3333497
TL
1465static int pinctrl_pm_select_state(struct device *dev,
1466 struct pinctrl_state *state)
14005ee2
LW
1467{
1468 struct dev_pin_info *pins = dev->pins;
1469 int ret;
1470
f3333497
TL
1471 if (IS_ERR(state))
1472 return 0; /* No such state */
1473 ret = pinctrl_select_state(pins->p, state);
14005ee2 1474 if (ret)
f3333497
TL
1475 dev_err(dev, "failed to activate pinctrl state %s\n",
1476 state->name);
14005ee2
LW
1477 return ret;
1478}
f3333497
TL
1479
1480/**
1481 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1482 * @dev: device to select default state for
1483 */
1484int pinctrl_pm_select_default_state(struct device *dev)
1485{
1486 if (!dev->pins)
1487 return 0;
1488
1489 return pinctrl_pm_select_state(dev, dev->pins->default_state);
1490}
f472dead 1491EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
14005ee2
LW
1492
1493/**
1494 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1495 * @dev: device to select sleep state for
1496 */
1497int pinctrl_pm_select_sleep_state(struct device *dev)
1498{
f3333497 1499 if (!dev->pins)
14005ee2 1500 return 0;
f3333497
TL
1501
1502 return pinctrl_pm_select_state(dev, dev->pins->sleep_state);
14005ee2 1503}
f472dead 1504EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
14005ee2
LW
1505
1506/**
1507 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1508 * @dev: device to select idle state for
1509 */
1510int pinctrl_pm_select_idle_state(struct device *dev)
1511{
f3333497 1512 if (!dev->pins)
14005ee2 1513 return 0;
f3333497
TL
1514
1515 return pinctrl_pm_select_state(dev, dev->pins->idle_state);
14005ee2 1516}
f472dead 1517EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
14005ee2
LW
1518#endif
1519
2744e8af
LW
1520#ifdef CONFIG_DEBUG_FS
1521
1522static int pinctrl_pins_show(struct seq_file *s, void *what)
1523{
1524 struct pinctrl_dev *pctldev = s->private;
1525 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
706e8520 1526 unsigned i, pin;
2744e8af
LW
1527
1528 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
2744e8af 1529
42fed7ba 1530 mutex_lock(&pctldev->mutex);
57b676f9 1531
706e8520
CP
1532 /* The pin number can be retrived from the pin controller descriptor */
1533 for (i = 0; i < pctldev->desc->npins; i++) {
2744e8af
LW
1534 struct pin_desc *desc;
1535
706e8520 1536 pin = pctldev->desc->pins[i].number;
2744e8af
LW
1537 desc = pin_desc_get(pctldev, pin);
1538 /* Pin space may be sparse */
1539 if (desc == NULL)
1540 continue;
1541
cf9d994d 1542 seq_printf(s, "pin %d (%s) ", pin, desc->name);
2744e8af
LW
1543
1544 /* Driver-specific info per pin */
1545 if (ops->pin_dbg_show)
1546 ops->pin_dbg_show(pctldev, s, pin);
1547
1548 seq_puts(s, "\n");
1549 }
1550
42fed7ba 1551 mutex_unlock(&pctldev->mutex);
57b676f9 1552
2744e8af
LW
1553 return 0;
1554}
1555
1556static int pinctrl_groups_show(struct seq_file *s, void *what)
1557{
1558 struct pinctrl_dev *pctldev = s->private;
1559 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
d1e90e9e 1560 unsigned ngroups, selector = 0;
2744e8af 1561
42fed7ba
PC
1562 mutex_lock(&pctldev->mutex);
1563
d1e90e9e 1564 ngroups = ops->get_groups_count(pctldev);
57b676f9 1565
2744e8af 1566 seq_puts(s, "registered pin groups:\n");
d1e90e9e 1567 while (selector < ngroups) {
e5b3b2d9
AT
1568 const unsigned *pins = NULL;
1569 unsigned num_pins = 0;
2744e8af 1570 const char *gname = ops->get_group_name(pctldev, selector);
dcb5dbc3 1571 const char *pname;
e5b3b2d9 1572 int ret = 0;
2744e8af
LW
1573 int i;
1574
e5b3b2d9
AT
1575 if (ops->get_group_pins)
1576 ret = ops->get_group_pins(pctldev, selector,
1577 &pins, &num_pins);
2744e8af
LW
1578 if (ret)
1579 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1580 gname);
1581 else {
dcb5dbc3
DA
1582 seq_printf(s, "group: %s\n", gname);
1583 for (i = 0; i < num_pins; i++) {
1584 pname = pin_get_name(pctldev, pins[i]);
b4dd784b 1585 if (WARN_ON(!pname)) {
42fed7ba 1586 mutex_unlock(&pctldev->mutex);
dcb5dbc3 1587 return -EINVAL;
b4dd784b 1588 }
dcb5dbc3
DA
1589 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1590 }
1591 seq_puts(s, "\n");
2744e8af
LW
1592 }
1593 selector++;
1594 }
1595
42fed7ba 1596 mutex_unlock(&pctldev->mutex);
2744e8af
LW
1597
1598 return 0;
1599}
1600
1601static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1602{
1603 struct pinctrl_dev *pctldev = s->private;
1604 struct pinctrl_gpio_range *range = NULL;
1605
1606 seq_puts(s, "GPIO ranges handled:\n");
1607
42fed7ba 1608 mutex_lock(&pctldev->mutex);
57b676f9 1609
2744e8af 1610 /* Loop over the ranges */
2744e8af 1611 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
c8587eee
CR
1612 if (range->pins) {
1613 int a;
1614 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
1615 range->id, range->name,
1616 range->base, (range->base + range->npins - 1));
1617 for (a = 0; a < range->npins - 1; a++)
1618 seq_printf(s, "%u, ", range->pins[a]);
1619 seq_printf(s, "%u}\n", range->pins[a]);
1620 }
1621 else
1622 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1623 range->id, range->name,
1624 range->base, (range->base + range->npins - 1),
1625 range->pin_base,
1626 (range->pin_base + range->npins - 1));
2744e8af 1627 }
57b676f9 1628
42fed7ba 1629 mutex_unlock(&pctldev->mutex);
2744e8af
LW
1630
1631 return 0;
1632}
1633
1634static int pinctrl_devices_show(struct seq_file *s, void *what)
1635{
1636 struct pinctrl_dev *pctldev;
1637
ae6b4d85 1638 seq_puts(s, "name [pinmux] [pinconf]\n");
57b676f9 1639
42fed7ba 1640 mutex_lock(&pinctrldev_list_mutex);
57b676f9 1641
2744e8af
LW
1642 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1643 seq_printf(s, "%s ", pctldev->desc->name);
1644 if (pctldev->desc->pmxops)
ae6b4d85
LW
1645 seq_puts(s, "yes ");
1646 else
1647 seq_puts(s, "no ");
1648 if (pctldev->desc->confops)
2744e8af
LW
1649 seq_puts(s, "yes");
1650 else
1651 seq_puts(s, "no");
1652 seq_puts(s, "\n");
1653 }
57b676f9 1654
42fed7ba 1655 mutex_unlock(&pinctrldev_list_mutex);
2744e8af
LW
1656
1657 return 0;
1658}
1659
1e2082b5
SW
1660static inline const char *map_type(enum pinctrl_map_type type)
1661{
1662 static const char * const names[] = {
1663 "INVALID",
1664 "DUMMY_STATE",
1665 "MUX_GROUP",
1666 "CONFIGS_PIN",
1667 "CONFIGS_GROUP",
1668 };
1669
1670 if (type >= ARRAY_SIZE(names))
1671 return "UNKNOWN";
1672
1673 return names[type];
1674}
1675
3eedb437
SW
1676static int pinctrl_maps_show(struct seq_file *s, void *what)
1677{
1678 struct pinctrl_maps *maps_node;
1679 int i;
1680 struct pinctrl_map const *map;
1681
1682 seq_puts(s, "Pinctrl maps:\n");
1683
42fed7ba 1684 mutex_lock(&pinctrl_maps_mutex);
3eedb437 1685 for_each_maps(maps_node, i, map) {
1e2082b5
SW
1686 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1687 map->dev_name, map->name, map_type(map->type),
1688 map->type);
1689
1690 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1691 seq_printf(s, "controlling device %s\n",
1692 map->ctrl_dev_name);
1693
1694 switch (map->type) {
1695 case PIN_MAP_TYPE_MUX_GROUP:
1696 pinmux_show_map(s, map);
1697 break;
1698 case PIN_MAP_TYPE_CONFIGS_PIN:
1699 case PIN_MAP_TYPE_CONFIGS_GROUP:
1700 pinconf_show_map(s, map);
1701 break;
1702 default:
1703 break;
1704 }
1705
1706 seq_printf(s, "\n");
3eedb437 1707 }
42fed7ba 1708 mutex_unlock(&pinctrl_maps_mutex);
3eedb437
SW
1709
1710 return 0;
1711}
1712
befe5bdf
LW
1713static int pinctrl_show(struct seq_file *s, void *what)
1714{
1715 struct pinctrl *p;
6e5e959d 1716 struct pinctrl_state *state;
7ecdb16f 1717 struct pinctrl_setting *setting;
befe5bdf
LW
1718
1719 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
57b676f9 1720
42fed7ba 1721 mutex_lock(&pinctrl_list_mutex);
57b676f9 1722
befe5bdf 1723 list_for_each_entry(p, &pinctrl_list, node) {
6e5e959d
SW
1724 seq_printf(s, "device: %s current state: %s\n",
1725 dev_name(p->dev),
1726 p->state ? p->state->name : "none");
1727
1728 list_for_each_entry(state, &p->states, node) {
1729 seq_printf(s, " state: %s\n", state->name);
befe5bdf 1730
6e5e959d 1731 list_for_each_entry(setting, &state->settings, node) {
1e2082b5
SW
1732 struct pinctrl_dev *pctldev = setting->pctldev;
1733
1734 seq_printf(s, " type: %s controller %s ",
1735 map_type(setting->type),
1736 pinctrl_dev_get_name(pctldev));
1737
1738 switch (setting->type) {
1739 case PIN_MAP_TYPE_MUX_GROUP:
1740 pinmux_show_setting(s, setting);
1741 break;
1742 case PIN_MAP_TYPE_CONFIGS_PIN:
1743 case PIN_MAP_TYPE_CONFIGS_GROUP:
1744 pinconf_show_setting(s, setting);
1745 break;
1746 default:
1747 break;
1748 }
6e5e959d 1749 }
befe5bdf 1750 }
befe5bdf
LW
1751 }
1752
42fed7ba 1753 mutex_unlock(&pinctrl_list_mutex);
57b676f9 1754
befe5bdf
LW
1755 return 0;
1756}
1757
2744e8af
LW
1758static int pinctrl_pins_open(struct inode *inode, struct file *file)
1759{
1760 return single_open(file, pinctrl_pins_show, inode->i_private);
1761}
1762
1763static int pinctrl_groups_open(struct inode *inode, struct file *file)
1764{
1765 return single_open(file, pinctrl_groups_show, inode->i_private);
1766}
1767
1768static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1769{
1770 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1771}
1772
1773static int pinctrl_devices_open(struct inode *inode, struct file *file)
1774{
1775 return single_open(file, pinctrl_devices_show, NULL);
1776}
1777
3eedb437
SW
1778static int pinctrl_maps_open(struct inode *inode, struct file *file)
1779{
1780 return single_open(file, pinctrl_maps_show, NULL);
1781}
1782
befe5bdf
LW
1783static int pinctrl_open(struct inode *inode, struct file *file)
1784{
1785 return single_open(file, pinctrl_show, NULL);
1786}
1787
2744e8af
LW
1788static const struct file_operations pinctrl_pins_ops = {
1789 .open = pinctrl_pins_open,
1790 .read = seq_read,
1791 .llseek = seq_lseek,
1792 .release = single_release,
1793};
1794
1795static const struct file_operations pinctrl_groups_ops = {
1796 .open = pinctrl_groups_open,
1797 .read = seq_read,
1798 .llseek = seq_lseek,
1799 .release = single_release,
1800};
1801
1802static const struct file_operations pinctrl_gpioranges_ops = {
1803 .open = pinctrl_gpioranges_open,
1804 .read = seq_read,
1805 .llseek = seq_lseek,
1806 .release = single_release,
1807};
1808
3eedb437
SW
1809static const struct file_operations pinctrl_devices_ops = {
1810 .open = pinctrl_devices_open,
befe5bdf
LW
1811 .read = seq_read,
1812 .llseek = seq_lseek,
1813 .release = single_release,
1814};
1815
3eedb437
SW
1816static const struct file_operations pinctrl_maps_ops = {
1817 .open = pinctrl_maps_open,
2744e8af
LW
1818 .read = seq_read,
1819 .llseek = seq_lseek,
1820 .release = single_release,
1821};
1822
befe5bdf
LW
1823static const struct file_operations pinctrl_ops = {
1824 .open = pinctrl_open,
1825 .read = seq_read,
1826 .llseek = seq_lseek,
1827 .release = single_release,
1828};
1829
2744e8af
LW
1830static struct dentry *debugfs_root;
1831
1832static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1833{
02157160 1834 struct dentry *device_root;
2744e8af 1835
51cd24ee 1836 device_root = debugfs_create_dir(dev_name(pctldev->dev),
2744e8af 1837 debugfs_root);
02157160
TL
1838 pctldev->device_root = device_root;
1839
2744e8af
LW
1840 if (IS_ERR(device_root) || !device_root) {
1841 pr_warn("failed to create debugfs directory for %s\n",
51cd24ee 1842 dev_name(pctldev->dev));
2744e8af
LW
1843 return;
1844 }
1845 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1846 device_root, pctldev, &pinctrl_pins_ops);
1847 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1848 device_root, pctldev, &pinctrl_groups_ops);
1849 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1850 device_root, pctldev, &pinctrl_gpioranges_ops);
e7f2a444
FV
1851 if (pctldev->desc->pmxops)
1852 pinmux_init_device_debugfs(device_root, pctldev);
1853 if (pctldev->desc->confops)
1854 pinconf_init_device_debugfs(device_root, pctldev);
2744e8af
LW
1855}
1856
02157160
TL
1857static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1858{
1859 debugfs_remove_recursive(pctldev->device_root);
1860}
1861
2744e8af
LW
1862static void pinctrl_init_debugfs(void)
1863{
1864 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1865 if (IS_ERR(debugfs_root) || !debugfs_root) {
1866 pr_warn("failed to create debugfs directory\n");
1867 debugfs_root = NULL;
1868 return;
1869 }
1870
1871 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1872 debugfs_root, NULL, &pinctrl_devices_ops);
3eedb437
SW
1873 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1874 debugfs_root, NULL, &pinctrl_maps_ops);
befe5bdf
LW
1875 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1876 debugfs_root, NULL, &pinctrl_ops);
2744e8af
LW
1877}
1878
1879#else /* CONFIG_DEBUG_FS */
1880
1881static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1882{
1883}
1884
1885static void pinctrl_init_debugfs(void)
1886{
1887}
1888
02157160
TL
1889static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1890{
1891}
1892
2744e8af
LW
1893#endif
1894
d26bc49f
SW
1895static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1896{
1897 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1898
1899 if (!ops ||
d1e90e9e 1900 !ops->get_groups_count ||
e5b3b2d9 1901 !ops->get_group_name)
d26bc49f
SW
1902 return -EINVAL;
1903
1904 return 0;
1905}
1906
99e4f675 1907/**
950b0d91 1908 * pinctrl_init_controller() - init a pin controller device
2744e8af
LW
1909 * @pctldesc: descriptor for this pin controller
1910 * @dev: parent device for this pin controller
1911 * @driver_data: private pin controller data for this pin controller
1912 */
950b0d91
TL
1913struct pinctrl_dev *pinctrl_init_controller(struct pinctrl_desc *pctldesc,
1914 struct device *dev,
1915 void *driver_data)
2744e8af 1916{
2744e8af
LW
1917 struct pinctrl_dev *pctldev;
1918 int ret;
1919
da9aecb0 1920 if (!pctldesc)
323de9ef 1921 return ERR_PTR(-EINVAL);
da9aecb0 1922 if (!pctldesc->name)
323de9ef 1923 return ERR_PTR(-EINVAL);
2744e8af 1924
02f5b989 1925 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
2104d12d 1926 if (!pctldev)
323de9ef 1927 return ERR_PTR(-ENOMEM);
b9130b77
TL
1928
1929 /* Initialize pin control device struct */
1930 pctldev->owner = pctldesc->owner;
1931 pctldev->desc = pctldesc;
1932 pctldev->driver_data = driver_data;
1933 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
c033a718 1934#ifdef CONFIG_GENERIC_PINCTRL_GROUPS
c7059c5a 1935 INIT_RADIX_TREE(&pctldev->pin_group_tree, GFP_KERNEL);
a76edc89
TL
1936#endif
1937#ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
1938 INIT_RADIX_TREE(&pctldev->pin_function_tree, GFP_KERNEL);
c033a718 1939#endif
b9130b77 1940 INIT_LIST_HEAD(&pctldev->gpio_ranges);
b9130b77 1941 pctldev->dev = dev;
42fed7ba 1942 mutex_init(&pctldev->mutex);
b9130b77 1943
d26bc49f 1944 /* check core ops for sanity */
323de9ef
MY
1945 ret = pinctrl_check_ops(pctldev);
1946 if (ret) {
ad6e1107 1947 dev_err(dev, "pinctrl ops lacks necessary functions\n");
d26bc49f
SW
1948 goto out_err;
1949 }
1950
2744e8af
LW
1951 /* If we're implementing pinmuxing, check the ops for sanity */
1952 if (pctldesc->pmxops) {
323de9ef
MY
1953 ret = pinmux_check_ops(pctldev);
1954 if (ret)
b9130b77 1955 goto out_err;
2744e8af
LW
1956 }
1957
ae6b4d85
LW
1958 /* If we're implementing pinconfig, check the ops for sanity */
1959 if (pctldesc->confops) {
323de9ef
MY
1960 ret = pinconf_check_ops(pctldev);
1961 if (ret)
b9130b77 1962 goto out_err;
ae6b4d85
LW
1963 }
1964
2744e8af 1965 /* Register all the pins */
ad6e1107 1966 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
2744e8af
LW
1967 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1968 if (ret) {
ad6e1107 1969 dev_err(dev, "error during pin registration\n");
2744e8af
LW
1970 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1971 pctldesc->npins);
51cd24ee 1972 goto out_err;
2744e8af
LW
1973 }
1974
2744e8af
LW
1975 return pctldev;
1976
51cd24ee 1977out_err:
42fed7ba 1978 mutex_destroy(&pctldev->mutex);
51cd24ee 1979 kfree(pctldev);
323de9ef 1980 return ERR_PTR(ret);
2744e8af 1981}
950b0d91
TL
1982
1983static int pinctrl_create_and_start(struct pinctrl_dev *pctldev)
1984{
1985 pctldev->p = create_pinctrl(pctldev->dev, pctldev);
1986 if (!IS_ERR(pctldev->p)) {
1987 kref_get(&pctldev->p->users);
1988 pctldev->hog_default =
1989 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
1990 if (IS_ERR(pctldev->hog_default)) {
1991 dev_dbg(pctldev->dev,
1992 "failed to lookup the default state\n");
1993 } else {
1994 if (pinctrl_select_state(pctldev->p,
1995 pctldev->hog_default))
1996 dev_err(pctldev->dev,
1997 "failed to select default state\n");
1998 }
1999
2000 pctldev->hog_sleep =
2001 pinctrl_lookup_state(pctldev->p,
2002 PINCTRL_STATE_SLEEP);
2003 if (IS_ERR(pctldev->hog_sleep))
2004 dev_dbg(pctldev->dev,
2005 "failed to lookup the sleep state\n");
2006 }
2007
2008 mutex_lock(&pinctrldev_list_mutex);
2009 list_add_tail(&pctldev->node, &pinctrldev_list);
2010 mutex_unlock(&pinctrldev_list_mutex);
2011
2012 pinctrl_init_device_debugfs(pctldev);
2013
2014 return 0;
2015}
2016
2017/**
2018 * pinctrl_register() - register a pin controller device
2019 * @pctldesc: descriptor for this pin controller
2020 * @dev: parent device for this pin controller
2021 * @driver_data: private pin controller data for this pin controller
2022 *
2023 * Note that pinctrl_register() is known to have problems as the pin
2024 * controller driver functions are called before the driver has a
2025 * struct pinctrl_dev handle. To avoid issues later on, please use the
2026 * new pinctrl_register_and_init() below instead.
2027 */
2028struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
2029 struct device *dev, void *driver_data)
2030{
2031 struct pinctrl_dev *pctldev;
2032 int error;
2033
2034 pctldev = pinctrl_init_controller(pctldesc, dev, driver_data);
2035 if (IS_ERR(pctldev))
2036 return pctldev;
2037
2038 error = pinctrl_create_and_start(pctldev);
2039 if (error) {
2040 mutex_destroy(&pctldev->mutex);
2041 kfree(pctldev);
2042
2043 return ERR_PTR(error);
2044 }
2045
2046 return pctldev;
2047
2048}
2744e8af
LW
2049EXPORT_SYMBOL_GPL(pinctrl_register);
2050
950b0d91
TL
2051int pinctrl_register_and_init(struct pinctrl_desc *pctldesc,
2052 struct device *dev, void *driver_data,
2053 struct pinctrl_dev **pctldev)
2054{
2055 struct pinctrl_dev *p;
2056 int error;
2057
2058 p = pinctrl_init_controller(pctldesc, dev, driver_data);
2059 if (IS_ERR(p))
2060 return PTR_ERR(p);
2061
2062 /*
2063 * We have pinctrl_start() call functions in the pin controller
2064 * driver with create_pinctrl() for at least dt_node_to_map(). So
2065 * let's make sure pctldev is properly initialized for the
2066 * pin controller driver before we do anything.
2067 */
2068 *pctldev = p;
2069
2070 error = pinctrl_create_and_start(p);
2071 if (error) {
2072 mutex_destroy(&p->mutex);
2073 kfree(p);
2074 *pctldev = NULL;
2075
2076 return error;
2077 }
2078
2079 return 0;
2080}
2081EXPORT_SYMBOL_GPL(pinctrl_register_and_init);
2082
2744e8af
LW
2083/**
2084 * pinctrl_unregister() - unregister pinmux
2085 * @pctldev: pin controller to unregister
2086 *
2087 * Called by pinmux drivers to unregister a pinmux.
2088 */
2089void pinctrl_unregister(struct pinctrl_dev *pctldev)
2090{
5d589b09 2091 struct pinctrl_gpio_range *range, *n;
3429fb3c
JH
2092 struct pinctrl_dev *p, *p1;
2093
2744e8af
LW
2094 if (pctldev == NULL)
2095 return;
2096
42fed7ba 2097 mutex_lock(&pctldev->mutex);
42fed7ba 2098 pinctrl_remove_device_debugfs(pctldev);
db93facf 2099 mutex_unlock(&pctldev->mutex);
57b676f9 2100
3429fb3c 2101 if (!IS_ERR_OR_NULL(pctldev->p))
42fed7ba 2102 pinctrl_put(pctldev->p);
57b676f9 2103
db93facf
JL
2104 mutex_lock(&pinctrldev_list_mutex);
2105 mutex_lock(&pctldev->mutex);
2744e8af 2106 /* TODO: check that no pinmuxes are still active? */
3429fb3c
JH
2107 list_for_each_entry_safe(p, p1, &pinctrldev_list, node)
2108 if (p == pctldev)
2109 list_del(&p->node);
a76edc89 2110 pinmux_generic_free_functions(pctldev);
c7059c5a 2111 pinctrl_generic_free_groups(pctldev);
2744e8af
LW
2112 /* Destroy descriptor tree */
2113 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
2114 pctldev->desc->npins);
5d589b09
DA
2115 /* remove gpio ranges map */
2116 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
2117 list_del(&range->node);
2118
42fed7ba
PC
2119 mutex_unlock(&pctldev->mutex);
2120 mutex_destroy(&pctldev->mutex);
51cd24ee 2121 kfree(pctldev);
42fed7ba 2122 mutex_unlock(&pinctrldev_list_mutex);
2744e8af
LW
2123}
2124EXPORT_SYMBOL_GPL(pinctrl_unregister);
2125
80e0f8d9
LD
2126static void devm_pinctrl_dev_release(struct device *dev, void *res)
2127{
2128 struct pinctrl_dev *pctldev = *(struct pinctrl_dev **)res;
2129
2130 pinctrl_unregister(pctldev);
2131}
2132
2133static int devm_pinctrl_dev_match(struct device *dev, void *res, void *data)
2134{
2135 struct pctldev **r = res;
2136
3024f920 2137 if (WARN_ON(!r || !*r))
80e0f8d9
LD
2138 return 0;
2139
2140 return *r == data;
2141}
2142
2143/**
2144 * devm_pinctrl_register() - Resource managed version of pinctrl_register().
2145 * @dev: parent device for this pin controller
2146 * @pctldesc: descriptor for this pin controller
2147 * @driver_data: private pin controller data for this pin controller
2148 *
2149 * Returns an error pointer if pincontrol register failed. Otherwise
2150 * it returns valid pinctrl handle.
2151 *
2152 * The pinctrl device will be automatically released when the device is unbound.
2153 */
2154struct pinctrl_dev *devm_pinctrl_register(struct device *dev,
2155 struct pinctrl_desc *pctldesc,
2156 void *driver_data)
2157{
2158 struct pinctrl_dev **ptr, *pctldev;
2159
2160 ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
2161 if (!ptr)
2162 return ERR_PTR(-ENOMEM);
2163
2164 pctldev = pinctrl_register(pctldesc, dev, driver_data);
2165 if (IS_ERR(pctldev)) {
2166 devres_free(ptr);
2167 return pctldev;
2168 }
2169
2170 *ptr = pctldev;
2171 devres_add(dev, ptr);
2172
2173 return pctldev;
2174}
2175EXPORT_SYMBOL_GPL(devm_pinctrl_register);
2176
950b0d91
TL
2177/**
2178 * devm_pinctrl_register_and_init() - Resource managed pinctrl register and init
2179 * @dev: parent device for this pin controller
2180 * @pctldesc: descriptor for this pin controller
2181 * @driver_data: private pin controller data for this pin controller
2182 *
2183 * Returns an error pointer if pincontrol register failed. Otherwise
2184 * it returns valid pinctrl handle.
2185 *
2186 * The pinctrl device will be automatically released when the device is unbound.
2187 */
2188int devm_pinctrl_register_and_init(struct device *dev,
2189 struct pinctrl_desc *pctldesc,
2190 void *driver_data,
2191 struct pinctrl_dev **pctldev)
2192{
2193 struct pinctrl_dev **ptr;
2194 int error;
2195
2196 ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
2197 if (!ptr)
2198 return -ENOMEM;
2199
2200 error = pinctrl_register_and_init(pctldesc, dev, driver_data, pctldev);
2201 if (error) {
2202 devres_free(ptr);
2203 return error;
2204 }
2205
2206 *ptr = *pctldev;
2207 devres_add(dev, ptr);
2208
2209 return 0;
2210}
2211EXPORT_SYMBOL_GPL(devm_pinctrl_register_and_init);
2212
80e0f8d9
LD
2213/**
2214 * devm_pinctrl_unregister() - Resource managed version of pinctrl_unregister().
2215 * @dev: device for which which resource was allocated
2216 * @pctldev: the pinctrl device to unregister.
2217 */
2218void devm_pinctrl_unregister(struct device *dev, struct pinctrl_dev *pctldev)
2219{
2220 WARN_ON(devres_release(dev, devm_pinctrl_dev_release,
2221 devm_pinctrl_dev_match, pctldev));
2222}
2223EXPORT_SYMBOL_GPL(devm_pinctrl_unregister);
2224
2744e8af
LW
2225static int __init pinctrl_init(void)
2226{
2227 pr_info("initialized pinctrl subsystem\n");
2228 pinctrl_init_debugfs();
2229 return 0;
2230}
2231
2232/* init early since many drivers really need to initialized pinmux early */
2233core_initcall(pinctrl_init);