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