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