Revert "pinctrl: remove pinctrl_remove_gpio_range"
[firefly-linux-kernel-4.4.55.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/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/sysfs.h>
24 #include <linux/debugfs.h>
25 #include <linux/seq_file.h>
26 #include <linux/pinctrl/consumer.h>
27 #include <linux/pinctrl/pinctrl.h>
28 #include <linux/pinctrl/machine.h>
29 #include "core.h"
30 #include "devicetree.h"
31 #include "pinmux.h"
32 #include "pinconf.h"
33
34 /**
35  * struct pinctrl_maps - a list item containing part of the mapping table
36  * @node: mapping table list node
37  * @maps: array of mapping table entries
38  * @num_maps: the number of entries in @maps
39  */
40 struct pinctrl_maps {
41         struct list_head node;
42         struct pinctrl_map const *maps;
43         unsigned num_maps;
44 };
45
46 static bool pinctrl_dummy_state;
47
48 /* Mutex taken by all entry points */
49 DEFINE_MUTEX(pinctrl_mutex);
50
51 /* Global list of pin control devices (struct pinctrl_dev) */
52 LIST_HEAD(pinctrldev_list);
53
54 /* List of pin controller handles (struct pinctrl) */
55 static LIST_HEAD(pinctrl_list);
56
57 /* List of pinctrl maps (struct pinctrl_maps) */
58 static LIST_HEAD(pinctrl_maps);
59
60 #define for_each_maps(_maps_node_, _i_, _map_) \
61         list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
62                 for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
63                         _i_ < _maps_node_->num_maps; \
64                         _i_++, _map_ = &_maps_node_->maps[_i_])
65
66 /**
67  * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
68  *
69  * Usually this function is called by platforms without pinctrl driver support
70  * but run with some shared drivers using pinctrl APIs.
71  * After calling this function, the pinctrl core will return successfully
72  * with creating a dummy state for the driver to keep going smoothly.
73  */
74 void pinctrl_provide_dummies(void)
75 {
76         pinctrl_dummy_state = true;
77 }
78
79 const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
80 {
81         /* We're not allowed to register devices without name */
82         return pctldev->desc->name;
83 }
84 EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
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         bool found = false;
103
104         if (!devname)
105                 return NULL;
106
107         list_for_each_entry(pctldev, &pinctrldev_list, node) {
108                 if (!strcmp(dev_name(pctldev->dev), devname)) {
109                         /* Matched on device name */
110                         found = true;
111                         break;
112                 }
113         }
114
115         return found ? pctldev : NULL;
116 }
117
118 /**
119  * pin_get_from_name() - look up a pin number from a name
120  * @pctldev: the pin control device to lookup the pin on
121  * @name: the name of the pin to look up
122  */
123 int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
124 {
125         unsigned i, pin;
126
127         /* The pin number can be retrived from the pin controller descriptor */
128         for (i = 0; i < pctldev->desc->npins; i++) {
129                 struct pin_desc *desc;
130
131                 pin = pctldev->desc->pins[i].number;
132                 desc = pin_desc_get(pctldev, pin);
133                 /* Pin space may be sparse */
134                 if (desc == NULL)
135                         continue;
136                 if (desc->name && !strcmp(name, desc->name))
137                         return pin;
138         }
139
140         return -EINVAL;
141 }
142
143 /**
144  * pin_get_name_from_id() - look up a pin name from a pin id
145  * @pctldev: the pin control device to lookup the pin on
146  * @name: the name of the pin to look up
147  */
148 const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
149 {
150         const struct pin_desc *desc;
151
152         desc = pin_desc_get(pctldev, pin);
153         if (desc == NULL) {
154                 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
155                         pin);
156                 return NULL;
157         }
158
159         return desc->name;
160 }
161
162 /**
163  * pin_is_valid() - check if pin exists on controller
164  * @pctldev: the pin control device to check the pin on
165  * @pin: pin to check, use the local pin controller index number
166  *
167  * This tells us whether a certain pin exist on a certain pin controller or
168  * not. Pin lists may be sparse, so some pins may not exist.
169  */
170 bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
171 {
172         struct pin_desc *pindesc;
173
174         if (pin < 0)
175                 return false;
176
177         mutex_lock(&pinctrl_mutex);
178         pindesc = pin_desc_get(pctldev, pin);
179         mutex_unlock(&pinctrl_mutex);
180
181         return pindesc != NULL;
182 }
183 EXPORT_SYMBOL_GPL(pin_is_valid);
184
185 /* Deletes a range of pin descriptors */
186 static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
187                                   const struct pinctrl_pin_desc *pins,
188                                   unsigned num_pins)
189 {
190         int i;
191
192         for (i = 0; i < num_pins; i++) {
193                 struct pin_desc *pindesc;
194
195                 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
196                                             pins[i].number);
197                 if (pindesc != NULL) {
198                         radix_tree_delete(&pctldev->pin_desc_tree,
199                                           pins[i].number);
200                         if (pindesc->dynamic_name)
201                                 kfree(pindesc->name);
202                 }
203                 kfree(pindesc);
204         }
205 }
206
207 static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
208                                     unsigned number, const char *name)
209 {
210         struct pin_desc *pindesc;
211
212         pindesc = pin_desc_get(pctldev, number);
213         if (pindesc != NULL) {
214                 pr_err("pin %d already registered on %s\n", number,
215                        pctldev->desc->name);
216                 return -EINVAL;
217         }
218
219         pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
220         if (pindesc == NULL) {
221                 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
222                 return -ENOMEM;
223         }
224
225         /* Set owner */
226         pindesc->pctldev = pctldev;
227
228         /* Copy basic pin info */
229         if (name) {
230                 pindesc->name = name;
231         } else {
232                 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
233                 if (pindesc->name == NULL) {
234                         kfree(pindesc);
235                         return -ENOMEM;
236                 }
237                 pindesc->dynamic_name = true;
238         }
239
240         radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
241         pr_debug("registered pin %d (%s) on %s\n",
242                  number, pindesc->name, pctldev->desc->name);
243         return 0;
244 }
245
246 static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
247                                  struct pinctrl_pin_desc const *pins,
248                                  unsigned num_descs)
249 {
250         unsigned i;
251         int ret = 0;
252
253         for (i = 0; i < num_descs; i++) {
254                 ret = pinctrl_register_one_pin(pctldev,
255                                                pins[i].number, pins[i].name);
256                 if (ret)
257                         return ret;
258         }
259
260         return 0;
261 }
262
263 /**
264  * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
265  * @pctldev: pin controller device to check
266  * @gpio: gpio pin to check taken from the global GPIO pin space
267  *
268  * Tries to match a GPIO pin number to the ranges handled by a certain pin
269  * controller, return the range or NULL
270  */
271 static struct pinctrl_gpio_range *
272 pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
273 {
274         struct pinctrl_gpio_range *range = NULL;
275
276         /* Loop over the ranges */
277         list_for_each_entry(range, &pctldev->gpio_ranges, node) {
278                 /* Check if we're in the valid range */
279                 if (gpio >= range->base &&
280                     gpio < range->base + range->npins) {
281                         return range;
282                 }
283         }
284
285         return NULL;
286 }
287
288 /**
289  * pinctrl_get_device_gpio_range() - find device for GPIO range
290  * @gpio: the pin to locate the pin controller for
291  * @outdev: the pin control device if found
292  * @outrange: the GPIO range if found
293  *
294  * Find the pin controller handling a certain GPIO pin from the pinspace of
295  * the GPIO subsystem, return the device and the matching GPIO range. Returns
296  * -EPROBE_DEFER if the GPIO range could not be found in any device since it
297  * may still have not been registered.
298  */
299 static int pinctrl_get_device_gpio_range(unsigned gpio,
300                                          struct pinctrl_dev **outdev,
301                                          struct pinctrl_gpio_range **outrange)
302 {
303         struct pinctrl_dev *pctldev = NULL;
304
305         /* Loop over the pin controllers */
306         list_for_each_entry(pctldev, &pinctrldev_list, node) {
307                 struct pinctrl_gpio_range *range;
308
309                 range = pinctrl_match_gpio_range(pctldev, gpio);
310                 if (range != NULL) {
311                         *outdev = pctldev;
312                         *outrange = range;
313                         return 0;
314                 }
315         }
316
317         return -EPROBE_DEFER;
318 }
319
320 /**
321  * pinctrl_add_gpio_range() - register a GPIO range for a controller
322  * @pctldev: pin controller device to add the range to
323  * @range: the GPIO range to add
324  *
325  * This adds a range of GPIOs to be handled by a certain pin controller. Call
326  * this to register handled ranges after registering your pin controller.
327  */
328 void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
329                             struct pinctrl_gpio_range *range)
330 {
331         mutex_lock(&pinctrl_mutex);
332         list_add_tail(&range->node, &pctldev->gpio_ranges);
333         mutex_unlock(&pinctrl_mutex);
334 }
335 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
336
337 void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
338                              struct pinctrl_gpio_range *ranges,
339                              unsigned nranges)
340 {
341         int i;
342
343         for (i = 0; i < nranges; i++)
344                 pinctrl_add_gpio_range(pctldev, &ranges[i]);
345 }
346 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
347
348 /**
349  * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
350  * @pctldev: pin controller device to remove the range from
351  * @range: the GPIO range to remove
352  */
353 void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
354                                struct pinctrl_gpio_range *range)
355 {
356         mutex_lock(&pinctrl_mutex);
357         list_del(&range->node);
358         mutex_unlock(&pinctrl_mutex);
359 }
360 EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
361
362 /**
363  * pinctrl_get_group_selector() - returns the group selector for a group
364  * @pctldev: the pin controller handling the group
365  * @pin_group: the pin group to look up
366  */
367 int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
368                                const char *pin_group)
369 {
370         const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
371         unsigned ngroups = pctlops->get_groups_count(pctldev);
372         unsigned group_selector = 0;
373
374         while (group_selector < ngroups) {
375                 const char *gname = pctlops->get_group_name(pctldev,
376                                                             group_selector);
377                 if (!strcmp(gname, pin_group)) {
378                         dev_dbg(pctldev->dev,
379                                 "found group selector %u for %s\n",
380                                 group_selector,
381                                 pin_group);
382                         return group_selector;
383                 }
384
385                 group_selector++;
386         }
387
388         dev_err(pctldev->dev, "does not have pin group %s\n",
389                 pin_group);
390
391         return -EINVAL;
392 }
393
394 /**
395  * pinctrl_request_gpio() - request a single pin to be used in as GPIO
396  * @gpio: the GPIO pin number from the GPIO subsystem number space
397  *
398  * This function should *ONLY* be used from gpiolib-based GPIO drivers,
399  * as part of their gpio_request() semantics, platforms and individual drivers
400  * shall *NOT* request GPIO pins to be muxed in.
401  */
402 int pinctrl_request_gpio(unsigned gpio)
403 {
404         struct pinctrl_dev *pctldev;
405         struct pinctrl_gpio_range *range;
406         int ret;
407         int pin;
408
409         mutex_lock(&pinctrl_mutex);
410
411         ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
412         if (ret) {
413                 mutex_unlock(&pinctrl_mutex);
414                 return ret;
415         }
416
417         /* Convert to the pin controllers number space */
418         pin = gpio - range->base + range->pin_base;
419
420         ret = pinmux_request_gpio(pctldev, range, pin, gpio);
421
422         mutex_unlock(&pinctrl_mutex);
423         return ret;
424 }
425 EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
426
427 /**
428  * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
429  * @gpio: the GPIO pin number from the GPIO subsystem number space
430  *
431  * This function should *ONLY* be used from gpiolib-based GPIO drivers,
432  * as part of their gpio_free() semantics, platforms and individual drivers
433  * shall *NOT* request GPIO pins to be muxed out.
434  */
435 void pinctrl_free_gpio(unsigned gpio)
436 {
437         struct pinctrl_dev *pctldev;
438         struct pinctrl_gpio_range *range;
439         int ret;
440         int pin;
441
442         mutex_lock(&pinctrl_mutex);
443
444         ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
445         if (ret) {
446                 mutex_unlock(&pinctrl_mutex);
447                 return;
448         }
449
450         /* Convert to the pin controllers number space */
451         pin = gpio - range->base + range->pin_base;
452
453         pinmux_free_gpio(pctldev, pin, range);
454
455         mutex_unlock(&pinctrl_mutex);
456 }
457 EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
458
459 static int pinctrl_gpio_direction(unsigned gpio, bool input)
460 {
461         struct pinctrl_dev *pctldev;
462         struct pinctrl_gpio_range *range;
463         int ret;
464         int pin;
465
466         ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
467         if (ret)
468                 return ret;
469
470         /* Convert to the pin controllers number space */
471         pin = gpio - range->base + range->pin_base;
472
473         return pinmux_gpio_direction(pctldev, range, pin, input);
474 }
475
476 /**
477  * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
478  * @gpio: the GPIO pin number from the GPIO subsystem number space
479  *
480  * This function should *ONLY* be used from gpiolib-based GPIO drivers,
481  * as part of their gpio_direction_input() semantics, platforms and individual
482  * drivers shall *NOT* touch pin control GPIO calls.
483  */
484 int pinctrl_gpio_direction_input(unsigned gpio)
485 {
486         int ret;
487         mutex_lock(&pinctrl_mutex);
488         ret = pinctrl_gpio_direction(gpio, true);
489         mutex_unlock(&pinctrl_mutex);
490         return ret;
491 }
492 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
493
494 /**
495  * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
496  * @gpio: the GPIO pin number from the GPIO subsystem number space
497  *
498  * This function should *ONLY* be used from gpiolib-based GPIO drivers,
499  * as part of their gpio_direction_output() semantics, platforms and individual
500  * drivers shall *NOT* touch pin control GPIO calls.
501  */
502 int pinctrl_gpio_direction_output(unsigned gpio)
503 {
504         int ret;
505         mutex_lock(&pinctrl_mutex);
506         ret = pinctrl_gpio_direction(gpio, false);
507         mutex_unlock(&pinctrl_mutex);
508         return ret;
509 }
510 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
511
512 static struct pinctrl_state *find_state(struct pinctrl *p,
513                                         const char *name)
514 {
515         struct pinctrl_state *state;
516
517         list_for_each_entry(state, &p->states, node)
518                 if (!strcmp(state->name, name))
519                         return state;
520
521         return NULL;
522 }
523
524 static struct pinctrl_state *create_state(struct pinctrl *p,
525                                           const char *name)
526 {
527         struct pinctrl_state *state;
528
529         state = kzalloc(sizeof(*state), GFP_KERNEL);
530         if (state == NULL) {
531                 dev_err(p->dev,
532                         "failed to alloc struct pinctrl_state\n");
533                 return ERR_PTR(-ENOMEM);
534         }
535
536         state->name = name;
537         INIT_LIST_HEAD(&state->settings);
538
539         list_add_tail(&state->node, &p->states);
540
541         return state;
542 }
543
544 static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
545 {
546         struct pinctrl_state *state;
547         struct pinctrl_setting *setting;
548         int ret;
549
550         state = find_state(p, map->name);
551         if (!state)
552                 state = create_state(p, map->name);
553         if (IS_ERR(state))
554                 return PTR_ERR(state);
555
556         if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
557                 return 0;
558
559         setting = kzalloc(sizeof(*setting), GFP_KERNEL);
560         if (setting == NULL) {
561                 dev_err(p->dev,
562                         "failed to alloc struct pinctrl_setting\n");
563                 return -ENOMEM;
564         }
565
566         setting->type = map->type;
567
568         setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
569         if (setting->pctldev == NULL) {
570                 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
571                         map->ctrl_dev_name);
572                 kfree(setting);
573                 /*
574                  * OK let us guess that the driver is not there yet, and
575                  * let's defer obtaining this pinctrl handle to later...
576                  */
577                 return -EPROBE_DEFER;
578         }
579
580         setting->dev_name = map->dev_name;
581
582         switch (map->type) {
583         case PIN_MAP_TYPE_MUX_GROUP:
584                 ret = pinmux_map_to_setting(map, setting);
585                 break;
586         case PIN_MAP_TYPE_CONFIGS_PIN:
587         case PIN_MAP_TYPE_CONFIGS_GROUP:
588                 ret = pinconf_map_to_setting(map, setting);
589                 break;
590         default:
591                 ret = -EINVAL;
592                 break;
593         }
594         if (ret < 0) {
595                 kfree(setting);
596                 return ret;
597         }
598
599         list_add_tail(&setting->node, &state->settings);
600
601         return 0;
602 }
603
604 static struct pinctrl *find_pinctrl(struct device *dev)
605 {
606         struct pinctrl *p;
607
608         list_for_each_entry(p, &pinctrl_list, node)
609                 if (p->dev == dev)
610                         return p;
611
612         return NULL;
613 }
614
615 static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
616
617 static struct pinctrl *create_pinctrl(struct device *dev)
618 {
619         struct pinctrl *p;
620         const char *devname;
621         struct pinctrl_maps *maps_node;
622         int i;
623         struct pinctrl_map const *map;
624         int ret;
625
626         /*
627          * create the state cookie holder struct pinctrl for each
628          * mapping, this is what consumers will get when requesting
629          * a pin control handle with pinctrl_get()
630          */
631         p = kzalloc(sizeof(*p), GFP_KERNEL);
632         if (p == NULL) {
633                 dev_err(dev, "failed to alloc struct pinctrl\n");
634                 return ERR_PTR(-ENOMEM);
635         }
636         p->dev = dev;
637         INIT_LIST_HEAD(&p->states);
638         INIT_LIST_HEAD(&p->dt_maps);
639
640         ret = pinctrl_dt_to_map(p);
641         if (ret < 0) {
642                 kfree(p);
643                 return ERR_PTR(ret);
644         }
645
646         devname = dev_name(dev);
647
648         /* Iterate over the pin control maps to locate the right ones */
649         for_each_maps(maps_node, i, map) {
650                 /* Map must be for this device */
651                 if (strcmp(map->dev_name, devname))
652                         continue;
653
654                 ret = add_setting(p, map);
655                 if (ret < 0) {
656                         pinctrl_put_locked(p, false);
657                         return ERR_PTR(ret);
658                 }
659         }
660
661         /* Add the pinmux to the global list */
662         list_add_tail(&p->node, &pinctrl_list);
663
664         return p;
665 }
666
667 static struct pinctrl *pinctrl_get_locked(struct device *dev)
668 {
669         struct pinctrl *p;
670
671         if (WARN_ON(!dev))
672                 return ERR_PTR(-EINVAL);
673
674         p = find_pinctrl(dev);
675         if (p != NULL)
676                 return ERR_PTR(-EBUSY);
677
678         return create_pinctrl(dev);
679 }
680
681 /**
682  * pinctrl_get() - retrieves the pinctrl handle for a device
683  * @dev: the device to obtain the handle for
684  */
685 struct pinctrl *pinctrl_get(struct device *dev)
686 {
687         struct pinctrl *p;
688
689         mutex_lock(&pinctrl_mutex);
690         p = pinctrl_get_locked(dev);
691         mutex_unlock(&pinctrl_mutex);
692
693         return p;
694 }
695 EXPORT_SYMBOL_GPL(pinctrl_get);
696
697 static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
698 {
699         struct pinctrl_state *state, *n1;
700         struct pinctrl_setting *setting, *n2;
701
702         list_for_each_entry_safe(state, n1, &p->states, node) {
703                 list_for_each_entry_safe(setting, n2, &state->settings, node) {
704                         switch (setting->type) {
705                         case PIN_MAP_TYPE_MUX_GROUP:
706                                 if (state == p->state)
707                                         pinmux_disable_setting(setting);
708                                 pinmux_free_setting(setting);
709                                 break;
710                         case PIN_MAP_TYPE_CONFIGS_PIN:
711                         case PIN_MAP_TYPE_CONFIGS_GROUP:
712                                 pinconf_free_setting(setting);
713                                 break;
714                         default:
715                                 break;
716                         }
717                         list_del(&setting->node);
718                         kfree(setting);
719                 }
720                 list_del(&state->node);
721                 kfree(state);
722         }
723
724         pinctrl_dt_free_maps(p);
725
726         if (inlist)
727                 list_del(&p->node);
728         kfree(p);
729 }
730
731 /**
732  * pinctrl_put() - release a previously claimed pinctrl handle
733  * @p: the pinctrl handle to release
734  */
735 void pinctrl_put(struct pinctrl *p)
736 {
737         mutex_lock(&pinctrl_mutex);
738         pinctrl_put_locked(p, true);
739         mutex_unlock(&pinctrl_mutex);
740 }
741 EXPORT_SYMBOL_GPL(pinctrl_put);
742
743 static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
744                                                          const char *name)
745 {
746         struct pinctrl_state *state;
747
748         state = find_state(p, name);
749         if (!state) {
750                 if (pinctrl_dummy_state) {
751                         /* create dummy state */
752                         dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
753                                 name);
754                         state = create_state(p, name);
755                 } else
756                         state = ERR_PTR(-ENODEV);
757         }
758
759         return state;
760 }
761
762 /**
763  * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
764  * @p: the pinctrl handle to retrieve the state from
765  * @name: the state name to retrieve
766  */
767 struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
768 {
769         struct pinctrl_state *s;
770
771         mutex_lock(&pinctrl_mutex);
772         s = pinctrl_lookup_state_locked(p, name);
773         mutex_unlock(&pinctrl_mutex);
774
775         return s;
776 }
777 EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
778
779 static int pinctrl_select_state_locked(struct pinctrl *p,
780                                        struct pinctrl_state *state)
781 {
782         struct pinctrl_setting *setting, *setting2;
783         int ret;
784
785         if (p->state == state)
786                 return 0;
787
788         if (p->state) {
789                 /*
790                  * The set of groups with a mux configuration in the old state
791                  * may not be identical to the set of groups with a mux setting
792                  * in the new state. While this might be unusual, it's entirely
793                  * possible for the "user"-supplied mapping table to be written
794                  * that way. For each group that was configured in the old state
795                  * but not in the new state, this code puts that group into a
796                  * safe/disabled state.
797                  */
798                 list_for_each_entry(setting, &p->state->settings, node) {
799                         bool found = false;
800                         if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
801                                 continue;
802                         list_for_each_entry(setting2, &state->settings, node) {
803                                 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
804                                         continue;
805                                 if (setting2->data.mux.group ==
806                                                 setting->data.mux.group) {
807                                         found = true;
808                                         break;
809                                 }
810                         }
811                         if (!found)
812                                 pinmux_disable_setting(setting);
813                 }
814         }
815
816         p->state = state;
817
818         /* Apply all the settings for the new state */
819         list_for_each_entry(setting, &state->settings, node) {
820                 switch (setting->type) {
821                 case PIN_MAP_TYPE_MUX_GROUP:
822                         ret = pinmux_enable_setting(setting);
823                         break;
824                 case PIN_MAP_TYPE_CONFIGS_PIN:
825                 case PIN_MAP_TYPE_CONFIGS_GROUP:
826                         ret = pinconf_apply_setting(setting);
827                         break;
828                 default:
829                         ret = -EINVAL;
830                         break;
831                 }
832                 if (ret < 0) {
833                         /* FIXME: Difficult to return to prev state */
834                         return ret;
835                 }
836         }
837
838         return 0;
839 }
840
841 /**
842  * pinctrl_select() - select/activate/program a pinctrl state to HW
843  * @p: the pinctrl handle for the device that requests configuratio
844  * @state: the state handle to select/activate/program
845  */
846 int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
847 {
848         int ret;
849
850         mutex_lock(&pinctrl_mutex);
851         ret = pinctrl_select_state_locked(p, state);
852         mutex_unlock(&pinctrl_mutex);
853
854         return ret;
855 }
856 EXPORT_SYMBOL_GPL(pinctrl_select_state);
857
858 static void devm_pinctrl_release(struct device *dev, void *res)
859 {
860         pinctrl_put(*(struct pinctrl **)res);
861 }
862
863 /**
864  * struct devm_pinctrl_get() - Resource managed pinctrl_get()
865  * @dev: the device to obtain the handle for
866  *
867  * If there is a need to explicitly destroy the returned struct pinctrl,
868  * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
869  */
870 struct pinctrl *devm_pinctrl_get(struct device *dev)
871 {
872         struct pinctrl **ptr, *p;
873
874         ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
875         if (!ptr)
876                 return ERR_PTR(-ENOMEM);
877
878         p = pinctrl_get(dev);
879         if (!IS_ERR(p)) {
880                 *ptr = p;
881                 devres_add(dev, ptr);
882         } else {
883                 devres_free(ptr);
884         }
885
886         return p;
887 }
888 EXPORT_SYMBOL_GPL(devm_pinctrl_get);
889
890 static int devm_pinctrl_match(struct device *dev, void *res, void *data)
891 {
892         struct pinctrl **p = res;
893
894         return *p == data;
895 }
896
897 /**
898  * devm_pinctrl_put() - Resource managed pinctrl_put()
899  * @p: the pinctrl handle to release
900  *
901  * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
902  * this function will not need to be called and the resource management
903  * code will ensure that the resource is freed.
904  */
905 void devm_pinctrl_put(struct pinctrl *p)
906 {
907         WARN_ON(devres_destroy(p->dev, devm_pinctrl_release,
908                                devm_pinctrl_match, p));
909         pinctrl_put(p);
910 }
911 EXPORT_SYMBOL_GPL(devm_pinctrl_put);
912
913 int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
914                          bool dup, bool locked)
915 {
916         int i, ret;
917         struct pinctrl_maps *maps_node;
918
919         pr_debug("add %d pinmux maps\n", num_maps);
920
921         /* First sanity check the new mapping */
922         for (i = 0; i < num_maps; i++) {
923                 if (!maps[i].dev_name) {
924                         pr_err("failed to register map %s (%d): no device given\n",
925                                maps[i].name, i);
926                         return -EINVAL;
927                 }
928
929                 if (!maps[i].name) {
930                         pr_err("failed to register map %d: no map name given\n",
931                                i);
932                         return -EINVAL;
933                 }
934
935                 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
936                                 !maps[i].ctrl_dev_name) {
937                         pr_err("failed to register map %s (%d): no pin control device given\n",
938                                maps[i].name, i);
939                         return -EINVAL;
940                 }
941
942                 switch (maps[i].type) {
943                 case PIN_MAP_TYPE_DUMMY_STATE:
944                         break;
945                 case PIN_MAP_TYPE_MUX_GROUP:
946                         ret = pinmux_validate_map(&maps[i], i);
947                         if (ret < 0)
948                                 return ret;
949                         break;
950                 case PIN_MAP_TYPE_CONFIGS_PIN:
951                 case PIN_MAP_TYPE_CONFIGS_GROUP:
952                         ret = pinconf_validate_map(&maps[i], i);
953                         if (ret < 0)
954                                 return ret;
955                         break;
956                 default:
957                         pr_err("failed to register map %s (%d): invalid type given\n",
958                                maps[i].name, i);
959                         return -EINVAL;
960                 }
961         }
962
963         maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
964         if (!maps_node) {
965                 pr_err("failed to alloc struct pinctrl_maps\n");
966                 return -ENOMEM;
967         }
968
969         maps_node->num_maps = num_maps;
970         if (dup) {
971                 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
972                                           GFP_KERNEL);
973                 if (!maps_node->maps) {
974                         pr_err("failed to duplicate mapping table\n");
975                         kfree(maps_node);
976                         return -ENOMEM;
977                 }
978         } else {
979                 maps_node->maps = maps;
980         }
981
982         if (!locked)
983                 mutex_lock(&pinctrl_mutex);
984         list_add_tail(&maps_node->node, &pinctrl_maps);
985         if (!locked)
986                 mutex_unlock(&pinctrl_mutex);
987
988         return 0;
989 }
990
991 /**
992  * pinctrl_register_mappings() - register a set of pin controller mappings
993  * @maps: the pincontrol mappings table to register. This should probably be
994  *      marked with __initdata so it can be discarded after boot. This
995  *      function will perform a shallow copy for the mapping entries.
996  * @num_maps: the number of maps in the mapping table
997  */
998 int pinctrl_register_mappings(struct pinctrl_map const *maps,
999                               unsigned num_maps)
1000 {
1001         return pinctrl_register_map(maps, num_maps, true, false);
1002 }
1003
1004 void pinctrl_unregister_map(struct pinctrl_map const *map)
1005 {
1006         struct pinctrl_maps *maps_node;
1007
1008         list_for_each_entry(maps_node, &pinctrl_maps, node) {
1009                 if (maps_node->maps == map) {
1010                         list_del(&maps_node->node);
1011                         return;
1012                 }
1013         }
1014 }
1015
1016 #ifdef CONFIG_DEBUG_FS
1017
1018 static int pinctrl_pins_show(struct seq_file *s, void *what)
1019 {
1020         struct pinctrl_dev *pctldev = s->private;
1021         const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1022         unsigned i, pin;
1023
1024         seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
1025
1026         mutex_lock(&pinctrl_mutex);
1027
1028         /* The pin number can be retrived from the pin controller descriptor */
1029         for (i = 0; i < pctldev->desc->npins; i++) {
1030                 struct pin_desc *desc;
1031
1032                 pin = pctldev->desc->pins[i].number;
1033                 desc = pin_desc_get(pctldev, pin);
1034                 /* Pin space may be sparse */
1035                 if (desc == NULL)
1036                         continue;
1037
1038                 seq_printf(s, "pin %d (%s) ", pin,
1039                            desc->name ? desc->name : "unnamed");
1040
1041                 /* Driver-specific info per pin */
1042                 if (ops->pin_dbg_show)
1043                         ops->pin_dbg_show(pctldev, s, pin);
1044
1045                 seq_puts(s, "\n");
1046         }
1047
1048         mutex_unlock(&pinctrl_mutex);
1049
1050         return 0;
1051 }
1052
1053 static int pinctrl_groups_show(struct seq_file *s, void *what)
1054 {
1055         struct pinctrl_dev *pctldev = s->private;
1056         const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1057         unsigned ngroups, selector = 0;
1058
1059         ngroups = ops->get_groups_count(pctldev);
1060         mutex_lock(&pinctrl_mutex);
1061
1062         seq_puts(s, "registered pin groups:\n");
1063         while (selector < ngroups) {
1064                 const unsigned *pins;
1065                 unsigned num_pins;
1066                 const char *gname = ops->get_group_name(pctldev, selector);
1067                 const char *pname;
1068                 int ret;
1069                 int i;
1070
1071                 ret = ops->get_group_pins(pctldev, selector,
1072                                           &pins, &num_pins);
1073                 if (ret)
1074                         seq_printf(s, "%s [ERROR GETTING PINS]\n",
1075                                    gname);
1076                 else {
1077                         seq_printf(s, "group: %s\n", gname);
1078                         for (i = 0; i < num_pins; i++) {
1079                                 pname = pin_get_name(pctldev, pins[i]);
1080                                 if (WARN_ON(!pname)) {
1081                                         mutex_unlock(&pinctrl_mutex);
1082                                         return -EINVAL;
1083                                 }
1084                                 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1085                         }
1086                         seq_puts(s, "\n");
1087                 }
1088                 selector++;
1089         }
1090
1091         mutex_unlock(&pinctrl_mutex);
1092
1093         return 0;
1094 }
1095
1096 static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1097 {
1098         struct pinctrl_dev *pctldev = s->private;
1099         struct pinctrl_gpio_range *range = NULL;
1100
1101         seq_puts(s, "GPIO ranges handled:\n");
1102
1103         mutex_lock(&pinctrl_mutex);
1104
1105         /* Loop over the ranges */
1106         list_for_each_entry(range, &pctldev->gpio_ranges, node) {
1107                 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1108                            range->id, range->name,
1109                            range->base, (range->base + range->npins - 1),
1110                            range->pin_base,
1111                            (range->pin_base + range->npins - 1));
1112         }
1113
1114         mutex_unlock(&pinctrl_mutex);
1115
1116         return 0;
1117 }
1118
1119 static int pinctrl_devices_show(struct seq_file *s, void *what)
1120 {
1121         struct pinctrl_dev *pctldev;
1122
1123         seq_puts(s, "name [pinmux] [pinconf]\n");
1124
1125         mutex_lock(&pinctrl_mutex);
1126
1127         list_for_each_entry(pctldev, &pinctrldev_list, node) {
1128                 seq_printf(s, "%s ", pctldev->desc->name);
1129                 if (pctldev->desc->pmxops)
1130                         seq_puts(s, "yes ");
1131                 else
1132                         seq_puts(s, "no ");
1133                 if (pctldev->desc->confops)
1134                         seq_puts(s, "yes");
1135                 else
1136                         seq_puts(s, "no");
1137                 seq_puts(s, "\n");
1138         }
1139
1140         mutex_unlock(&pinctrl_mutex);
1141
1142         return 0;
1143 }
1144
1145 static inline const char *map_type(enum pinctrl_map_type type)
1146 {
1147         static const char * const names[] = {
1148                 "INVALID",
1149                 "DUMMY_STATE",
1150                 "MUX_GROUP",
1151                 "CONFIGS_PIN",
1152                 "CONFIGS_GROUP",
1153         };
1154
1155         if (type >= ARRAY_SIZE(names))
1156                 return "UNKNOWN";
1157
1158         return names[type];
1159 }
1160
1161 static int pinctrl_maps_show(struct seq_file *s, void *what)
1162 {
1163         struct pinctrl_maps *maps_node;
1164         int i;
1165         struct pinctrl_map const *map;
1166
1167         seq_puts(s, "Pinctrl maps:\n");
1168
1169         mutex_lock(&pinctrl_mutex);
1170
1171         for_each_maps(maps_node, i, map) {
1172                 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1173                            map->dev_name, map->name, map_type(map->type),
1174                            map->type);
1175
1176                 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1177                         seq_printf(s, "controlling device %s\n",
1178                                    map->ctrl_dev_name);
1179
1180                 switch (map->type) {
1181                 case PIN_MAP_TYPE_MUX_GROUP:
1182                         pinmux_show_map(s, map);
1183                         break;
1184                 case PIN_MAP_TYPE_CONFIGS_PIN:
1185                 case PIN_MAP_TYPE_CONFIGS_GROUP:
1186                         pinconf_show_map(s, map);
1187                         break;
1188                 default:
1189                         break;
1190                 }
1191
1192                 seq_printf(s, "\n");
1193         }
1194
1195         mutex_unlock(&pinctrl_mutex);
1196
1197         return 0;
1198 }
1199
1200 static int pinctrl_show(struct seq_file *s, void *what)
1201 {
1202         struct pinctrl *p;
1203         struct pinctrl_state *state;
1204         struct pinctrl_setting *setting;
1205
1206         seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
1207
1208         mutex_lock(&pinctrl_mutex);
1209
1210         list_for_each_entry(p, &pinctrl_list, node) {
1211                 seq_printf(s, "device: %s current state: %s\n",
1212                            dev_name(p->dev),
1213                            p->state ? p->state->name : "none");
1214
1215                 list_for_each_entry(state, &p->states, node) {
1216                         seq_printf(s, "  state: %s\n", state->name);
1217
1218                         list_for_each_entry(setting, &state->settings, node) {
1219                                 struct pinctrl_dev *pctldev = setting->pctldev;
1220
1221                                 seq_printf(s, "    type: %s controller %s ",
1222                                            map_type(setting->type),
1223                                            pinctrl_dev_get_name(pctldev));
1224
1225                                 switch (setting->type) {
1226                                 case PIN_MAP_TYPE_MUX_GROUP:
1227                                         pinmux_show_setting(s, setting);
1228                                         break;
1229                                 case PIN_MAP_TYPE_CONFIGS_PIN:
1230                                 case PIN_MAP_TYPE_CONFIGS_GROUP:
1231                                         pinconf_show_setting(s, setting);
1232                                         break;
1233                                 default:
1234                                         break;
1235                                 }
1236                         }
1237                 }
1238         }
1239
1240         mutex_unlock(&pinctrl_mutex);
1241
1242         return 0;
1243 }
1244
1245 static int pinctrl_pins_open(struct inode *inode, struct file *file)
1246 {
1247         return single_open(file, pinctrl_pins_show, inode->i_private);
1248 }
1249
1250 static int pinctrl_groups_open(struct inode *inode, struct file *file)
1251 {
1252         return single_open(file, pinctrl_groups_show, inode->i_private);
1253 }
1254
1255 static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1256 {
1257         return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1258 }
1259
1260 static int pinctrl_devices_open(struct inode *inode, struct file *file)
1261 {
1262         return single_open(file, pinctrl_devices_show, NULL);
1263 }
1264
1265 static int pinctrl_maps_open(struct inode *inode, struct file *file)
1266 {
1267         return single_open(file, pinctrl_maps_show, NULL);
1268 }
1269
1270 static int pinctrl_open(struct inode *inode, struct file *file)
1271 {
1272         return single_open(file, pinctrl_show, NULL);
1273 }
1274
1275 static const struct file_operations pinctrl_pins_ops = {
1276         .open           = pinctrl_pins_open,
1277         .read           = seq_read,
1278         .llseek         = seq_lseek,
1279         .release        = single_release,
1280 };
1281
1282 static const struct file_operations pinctrl_groups_ops = {
1283         .open           = pinctrl_groups_open,
1284         .read           = seq_read,
1285         .llseek         = seq_lseek,
1286         .release        = single_release,
1287 };
1288
1289 static const struct file_operations pinctrl_gpioranges_ops = {
1290         .open           = pinctrl_gpioranges_open,
1291         .read           = seq_read,
1292         .llseek         = seq_lseek,
1293         .release        = single_release,
1294 };
1295
1296 static const struct file_operations pinctrl_devices_ops = {
1297         .open           = pinctrl_devices_open,
1298         .read           = seq_read,
1299         .llseek         = seq_lseek,
1300         .release        = single_release,
1301 };
1302
1303 static const struct file_operations pinctrl_maps_ops = {
1304         .open           = pinctrl_maps_open,
1305         .read           = seq_read,
1306         .llseek         = seq_lseek,
1307         .release        = single_release,
1308 };
1309
1310 static const struct file_operations pinctrl_ops = {
1311         .open           = pinctrl_open,
1312         .read           = seq_read,
1313         .llseek         = seq_lseek,
1314         .release        = single_release,
1315 };
1316
1317 static struct dentry *debugfs_root;
1318
1319 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1320 {
1321         struct dentry *device_root;
1322
1323         device_root = debugfs_create_dir(dev_name(pctldev->dev),
1324                                          debugfs_root);
1325         pctldev->device_root = device_root;
1326
1327         if (IS_ERR(device_root) || !device_root) {
1328                 pr_warn("failed to create debugfs directory for %s\n",
1329                         dev_name(pctldev->dev));
1330                 return;
1331         }
1332         debugfs_create_file("pins", S_IFREG | S_IRUGO,
1333                             device_root, pctldev, &pinctrl_pins_ops);
1334         debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1335                             device_root, pctldev, &pinctrl_groups_ops);
1336         debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1337                             device_root, pctldev, &pinctrl_gpioranges_ops);
1338         pinmux_init_device_debugfs(device_root, pctldev);
1339         pinconf_init_device_debugfs(device_root, pctldev);
1340 }
1341
1342 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1343 {
1344         debugfs_remove_recursive(pctldev->device_root);
1345 }
1346
1347 static void pinctrl_init_debugfs(void)
1348 {
1349         debugfs_root = debugfs_create_dir("pinctrl", NULL);
1350         if (IS_ERR(debugfs_root) || !debugfs_root) {
1351                 pr_warn("failed to create debugfs directory\n");
1352                 debugfs_root = NULL;
1353                 return;
1354         }
1355
1356         debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1357                             debugfs_root, NULL, &pinctrl_devices_ops);
1358         debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1359                             debugfs_root, NULL, &pinctrl_maps_ops);
1360         debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1361                             debugfs_root, NULL, &pinctrl_ops);
1362 }
1363
1364 #else /* CONFIG_DEBUG_FS */
1365
1366 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1367 {
1368 }
1369
1370 static void pinctrl_init_debugfs(void)
1371 {
1372 }
1373
1374 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1375 {
1376 }
1377
1378 #endif
1379
1380 static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1381 {
1382         const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1383
1384         if (!ops ||
1385             !ops->get_groups_count ||
1386             !ops->get_group_name ||
1387             !ops->get_group_pins)
1388                 return -EINVAL;
1389
1390         if (ops->dt_node_to_map && !ops->dt_free_map)
1391                 return -EINVAL;
1392
1393         return 0;
1394 }
1395
1396 /**
1397  * pinctrl_register() - register a pin controller device
1398  * @pctldesc: descriptor for this pin controller
1399  * @dev: parent device for this pin controller
1400  * @driver_data: private pin controller data for this pin controller
1401  */
1402 struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1403                                     struct device *dev, void *driver_data)
1404 {
1405         struct pinctrl_dev *pctldev;
1406         int ret;
1407
1408         if (!pctldesc)
1409                 return NULL;
1410         if (!pctldesc->name)
1411                 return NULL;
1412
1413         pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
1414         if (pctldev == NULL) {
1415                 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
1416                 return NULL;
1417         }
1418
1419         /* Initialize pin control device struct */
1420         pctldev->owner = pctldesc->owner;
1421         pctldev->desc = pctldesc;
1422         pctldev->driver_data = driver_data;
1423         INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
1424         INIT_LIST_HEAD(&pctldev->gpio_ranges);
1425         pctldev->dev = dev;
1426
1427         /* check core ops for sanity */
1428         if (pinctrl_check_ops(pctldev)) {
1429                 dev_err(dev, "pinctrl ops lacks necessary functions\n");
1430                 goto out_err;
1431         }
1432
1433         /* If we're implementing pinmuxing, check the ops for sanity */
1434         if (pctldesc->pmxops) {
1435                 if (pinmux_check_ops(pctldev))
1436                         goto out_err;
1437         }
1438
1439         /* If we're implementing pinconfig, check the ops for sanity */
1440         if (pctldesc->confops) {
1441                 if (pinconf_check_ops(pctldev))
1442                         goto out_err;
1443         }
1444
1445         /* Register all the pins */
1446         dev_dbg(dev, "try to register %d pins ...\n",  pctldesc->npins);
1447         ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1448         if (ret) {
1449                 dev_err(dev, "error during pin registration\n");
1450                 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1451                                       pctldesc->npins);
1452                 goto out_err;
1453         }
1454
1455         mutex_lock(&pinctrl_mutex);
1456
1457         list_add_tail(&pctldev->node, &pinctrldev_list);
1458
1459         pctldev->p = pinctrl_get_locked(pctldev->dev);
1460         if (!IS_ERR(pctldev->p)) {
1461                 struct pinctrl_state *s =
1462                         pinctrl_lookup_state_locked(pctldev->p,
1463                                                     PINCTRL_STATE_DEFAULT);
1464                 if (IS_ERR(s)) {
1465                         dev_dbg(dev, "failed to lookup the default state\n");
1466                 } else {
1467                         if (pinctrl_select_state_locked(pctldev->p, s))
1468                                 dev_err(dev,
1469                                         "failed to select default state\n");
1470                 }
1471         }
1472
1473         mutex_unlock(&pinctrl_mutex);
1474
1475         pinctrl_init_device_debugfs(pctldev);
1476
1477         return pctldev;
1478
1479 out_err:
1480         kfree(pctldev);
1481         return NULL;
1482 }
1483 EXPORT_SYMBOL_GPL(pinctrl_register);
1484
1485 /**
1486  * pinctrl_unregister() - unregister pinmux
1487  * @pctldev: pin controller to unregister
1488  *
1489  * Called by pinmux drivers to unregister a pinmux.
1490  */
1491 void pinctrl_unregister(struct pinctrl_dev *pctldev)
1492 {
1493         struct pinctrl_gpio_range *range, *n;
1494         if (pctldev == NULL)
1495                 return;
1496
1497         pinctrl_remove_device_debugfs(pctldev);
1498
1499         mutex_lock(&pinctrl_mutex);
1500
1501         if (!IS_ERR(pctldev->p))
1502                 pinctrl_put_locked(pctldev->p, true);
1503
1504         /* TODO: check that no pinmuxes are still active? */
1505         list_del(&pctldev->node);
1506         /* Destroy descriptor tree */
1507         pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1508                               pctldev->desc->npins);
1509         /* remove gpio ranges map */
1510         list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1511                 list_del(&range->node);
1512
1513         kfree(pctldev);
1514
1515         mutex_unlock(&pinctrl_mutex);
1516 }
1517 EXPORT_SYMBOL_GPL(pinctrl_unregister);
1518
1519 static int __init pinctrl_init(void)
1520 {
1521         pr_info("initialized pinctrl subsystem\n");
1522         pinctrl_init_debugfs();
1523         return 0;
1524 }
1525
1526 /* init early since many drivers really need to initialized pinmux early */
1527 core_initcall(pinctrl_init);