]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/pinctrl/core.c
Merge remote-tracking branch 'pinctrl/for-next'
[karo-tx-linux.git] / drivers / pinctrl / core.c
1 /*
2  * Core driver for the pin control subsystem
3  *
4  * Copyright (C) 2011-2012 ST-Ericsson SA
5  * Written on behalf of Linaro for ST-Ericsson
6  * Based on bits of regulator core, gpio core and clk core
7  *
8  * Author: Linus Walleij <linus.walleij@linaro.org>
9  *
10  * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
11  *
12  * License terms: GNU General Public License (GPL) version 2
13  */
14 #define pr_fmt(fmt) "pinctrl core: " fmt
15
16 #include <linux/kernel.h>
17 #include <linux/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_get_group_selector() - returns the group selector for a group
350  * @pctldev: the pin controller handling the group
351  * @pin_group: the pin group to look up
352  */
353 int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
354                                const char *pin_group)
355 {
356         const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
357         unsigned ngroups = pctlops->get_groups_count(pctldev);
358         unsigned group_selector = 0;
359
360         while (group_selector < ngroups) {
361                 const char *gname = pctlops->get_group_name(pctldev,
362                                                             group_selector);
363                 if (!strcmp(gname, pin_group)) {
364                         dev_dbg(pctldev->dev,
365                                 "found group selector %u for %s\n",
366                                 group_selector,
367                                 pin_group);
368                         return group_selector;
369                 }
370
371                 group_selector++;
372         }
373
374         dev_err(pctldev->dev, "does not have pin group %s\n",
375                 pin_group);
376
377         return -EINVAL;
378 }
379
380 /**
381  * pinctrl_request_gpio() - request a single pin to be used in as GPIO
382  * @gpio: the GPIO pin number from the GPIO subsystem number space
383  *
384  * This function should *ONLY* be used from gpiolib-based GPIO drivers,
385  * as part of their gpio_request() semantics, platforms and individual drivers
386  * shall *NOT* request GPIO pins to be muxed in.
387  */
388 int pinctrl_request_gpio(unsigned gpio)
389 {
390         struct pinctrl_dev *pctldev;
391         struct pinctrl_gpio_range *range;
392         int ret;
393         int pin;
394
395         mutex_lock(&pinctrl_mutex);
396
397         ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
398         if (ret) {
399                 mutex_unlock(&pinctrl_mutex);
400                 return ret;
401         }
402
403         /* Convert to the pin controllers number space */
404         pin = gpio - range->base + range->pin_base;
405
406         ret = pinmux_request_gpio(pctldev, range, pin, gpio);
407
408         mutex_unlock(&pinctrl_mutex);
409         return ret;
410 }
411 EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
412
413 /**
414  * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
415  * @gpio: the GPIO pin number from the GPIO subsystem number space
416  *
417  * This function should *ONLY* be used from gpiolib-based GPIO drivers,
418  * as part of their gpio_free() semantics, platforms and individual drivers
419  * shall *NOT* request GPIO pins to be muxed out.
420  */
421 void pinctrl_free_gpio(unsigned gpio)
422 {
423         struct pinctrl_dev *pctldev;
424         struct pinctrl_gpio_range *range;
425         int ret;
426         int pin;
427
428         mutex_lock(&pinctrl_mutex);
429
430         ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
431         if (ret) {
432                 mutex_unlock(&pinctrl_mutex);
433                 return;
434         }
435
436         /* Convert to the pin controllers number space */
437         pin = gpio - range->base + range->pin_base;
438
439         pinmux_free_gpio(pctldev, pin, range);
440
441         mutex_unlock(&pinctrl_mutex);
442 }
443 EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
444
445 static int pinctrl_gpio_direction(unsigned gpio, bool input)
446 {
447         struct pinctrl_dev *pctldev;
448         struct pinctrl_gpio_range *range;
449         int ret;
450         int pin;
451
452         ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
453         if (ret)
454                 return ret;
455
456         /* Convert to the pin controllers number space */
457         pin = gpio - range->base + range->pin_base;
458
459         return pinmux_gpio_direction(pctldev, range, pin, input);
460 }
461
462 /**
463  * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
464  * @gpio: the GPIO pin number from the GPIO subsystem number space
465  *
466  * This function should *ONLY* be used from gpiolib-based GPIO drivers,
467  * as part of their gpio_direction_input() semantics, platforms and individual
468  * drivers shall *NOT* touch pin control GPIO calls.
469  */
470 int pinctrl_gpio_direction_input(unsigned gpio)
471 {
472         int ret;
473         mutex_lock(&pinctrl_mutex);
474         ret = pinctrl_gpio_direction(gpio, true);
475         mutex_unlock(&pinctrl_mutex);
476         return ret;
477 }
478 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
479
480 /**
481  * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
482  * @gpio: the GPIO pin number from the GPIO subsystem number space
483  *
484  * This function should *ONLY* be used from gpiolib-based GPIO drivers,
485  * as part of their gpio_direction_output() semantics, platforms and individual
486  * drivers shall *NOT* touch pin control GPIO calls.
487  */
488 int pinctrl_gpio_direction_output(unsigned gpio)
489 {
490         int ret;
491         mutex_lock(&pinctrl_mutex);
492         ret = pinctrl_gpio_direction(gpio, false);
493         mutex_unlock(&pinctrl_mutex);
494         return ret;
495 }
496 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
497
498 static struct pinctrl_state *find_state(struct pinctrl *p,
499                                         const char *name)
500 {
501         struct pinctrl_state *state;
502
503         list_for_each_entry(state, &p->states, node)
504                 if (!strcmp(state->name, name))
505                         return state;
506
507         return NULL;
508 }
509
510 static struct pinctrl_state *create_state(struct pinctrl *p,
511                                           const char *name)
512 {
513         struct pinctrl_state *state;
514
515         state = kzalloc(sizeof(*state), GFP_KERNEL);
516         if (state == NULL) {
517                 dev_err(p->dev,
518                         "failed to alloc struct pinctrl_state\n");
519                 return ERR_PTR(-ENOMEM);
520         }
521
522         state->name = name;
523         INIT_LIST_HEAD(&state->settings);
524
525         list_add_tail(&state->node, &p->states);
526
527         return state;
528 }
529
530 static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
531 {
532         struct pinctrl_state *state;
533         struct pinctrl_setting *setting;
534         int ret;
535
536         state = find_state(p, map->name);
537         if (!state)
538                 state = create_state(p, map->name);
539         if (IS_ERR(state))
540                 return PTR_ERR(state);
541
542         if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
543                 return 0;
544
545         setting = kzalloc(sizeof(*setting), GFP_KERNEL);
546         if (setting == NULL) {
547                 dev_err(p->dev,
548                         "failed to alloc struct pinctrl_setting\n");
549                 return -ENOMEM;
550         }
551
552         setting->type = map->type;
553
554         setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
555         if (setting->pctldev == NULL) {
556                 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
557                         map->ctrl_dev_name);
558                 kfree(setting);
559                 /*
560                  * OK let us guess that the driver is not there yet, and
561                  * let's defer obtaining this pinctrl handle to later...
562                  */
563                 return -EPROBE_DEFER;
564         }
565
566         setting->dev_name = map->dev_name;
567
568         switch (map->type) {
569         case PIN_MAP_TYPE_MUX_GROUP:
570                 ret = pinmux_map_to_setting(map, setting);
571                 break;
572         case PIN_MAP_TYPE_CONFIGS_PIN:
573         case PIN_MAP_TYPE_CONFIGS_GROUP:
574                 ret = pinconf_map_to_setting(map, setting);
575                 break;
576         default:
577                 ret = -EINVAL;
578                 break;
579         }
580         if (ret < 0) {
581                 kfree(setting);
582                 return ret;
583         }
584
585         list_add_tail(&setting->node, &state->settings);
586
587         return 0;
588 }
589
590 static struct pinctrl *find_pinctrl(struct device *dev)
591 {
592         struct pinctrl *p;
593
594         list_for_each_entry(p, &pinctrl_list, node)
595                 if (p->dev == dev)
596                         return p;
597
598         return NULL;
599 }
600
601 static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
602
603 static struct pinctrl *create_pinctrl(struct device *dev)
604 {
605         struct pinctrl *p;
606         const char *devname;
607         struct pinctrl_maps *maps_node;
608         int i;
609         struct pinctrl_map const *map;
610         int ret;
611
612         /*
613          * create the state cookie holder struct pinctrl for each
614          * mapping, this is what consumers will get when requesting
615          * a pin control handle with pinctrl_get()
616          */
617         p = kzalloc(sizeof(*p), GFP_KERNEL);
618         if (p == NULL) {
619                 dev_err(dev, "failed to alloc struct pinctrl\n");
620                 return ERR_PTR(-ENOMEM);
621         }
622         p->dev = dev;
623         INIT_LIST_HEAD(&p->states);
624         INIT_LIST_HEAD(&p->dt_maps);
625
626         ret = pinctrl_dt_to_map(p);
627         if (ret < 0) {
628                 kfree(p);
629                 return ERR_PTR(ret);
630         }
631
632         devname = dev_name(dev);
633
634         /* Iterate over the pin control maps to locate the right ones */
635         for_each_maps(maps_node, i, map) {
636                 /* Map must be for this device */
637                 if (strcmp(map->dev_name, devname))
638                         continue;
639
640                 ret = add_setting(p, map);
641                 if (ret < 0) {
642                         pinctrl_put_locked(p, false);
643                         return ERR_PTR(ret);
644                 }
645         }
646
647         /* Add the pinmux to the global list */
648         list_add_tail(&p->node, &pinctrl_list);
649
650         return p;
651 }
652
653 static struct pinctrl *pinctrl_get_locked(struct device *dev)
654 {
655         struct pinctrl *p;
656
657         if (WARN_ON(!dev))
658                 return ERR_PTR(-EINVAL);
659
660         p = find_pinctrl(dev);
661         if (p != NULL)
662                 return ERR_PTR(-EBUSY);
663
664         return create_pinctrl(dev);
665 }
666
667 /**
668  * pinctrl_get() - retrieves the pinctrl handle for a device
669  * @dev: the device to obtain the handle for
670  */
671 struct pinctrl *pinctrl_get(struct device *dev)
672 {
673         struct pinctrl *p;
674
675         mutex_lock(&pinctrl_mutex);
676         p = pinctrl_get_locked(dev);
677         mutex_unlock(&pinctrl_mutex);
678
679         return p;
680 }
681 EXPORT_SYMBOL_GPL(pinctrl_get);
682
683 static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
684 {
685         struct pinctrl_state *state, *n1;
686         struct pinctrl_setting *setting, *n2;
687
688         list_for_each_entry_safe(state, n1, &p->states, node) {
689                 list_for_each_entry_safe(setting, n2, &state->settings, node) {
690                         switch (setting->type) {
691                         case PIN_MAP_TYPE_MUX_GROUP:
692                                 if (state == p->state)
693                                         pinmux_disable_setting(setting);
694                                 break;
695                         case PIN_MAP_TYPE_CONFIGS_PIN:
696                         case PIN_MAP_TYPE_CONFIGS_GROUP:
697                                 pinconf_free_setting(setting);
698                                 break;
699                         default:
700                                 break;
701                         }
702                         list_del(&setting->node);
703                         kfree(setting);
704                 }
705                 list_del(&state->node);
706                 kfree(state);
707         }
708
709         pinctrl_dt_free_maps(p);
710
711         if (inlist)
712                 list_del(&p->node);
713         kfree(p);
714 }
715
716 /**
717  * pinctrl_put() - release a previously claimed pinctrl handle
718  * @p: the pinctrl handle to release
719  */
720 void pinctrl_put(struct pinctrl *p)
721 {
722         mutex_lock(&pinctrl_mutex);
723         pinctrl_put_locked(p, true);
724         mutex_unlock(&pinctrl_mutex);
725 }
726 EXPORT_SYMBOL_GPL(pinctrl_put);
727
728 static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
729                                                          const char *name)
730 {
731         struct pinctrl_state *state;
732
733         state = find_state(p, name);
734         if (!state) {
735                 if (pinctrl_dummy_state) {
736                         /* create dummy state */
737                         dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
738                                 name);
739                         state = create_state(p, name);
740                 } else
741                         state = ERR_PTR(-ENODEV);
742         }
743
744         return state;
745 }
746
747 /**
748  * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
749  * @p: the pinctrl handle to retrieve the state from
750  * @name: the state name to retrieve
751  */
752 struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
753 {
754         struct pinctrl_state *s;
755
756         mutex_lock(&pinctrl_mutex);
757         s = pinctrl_lookup_state_locked(p, name);
758         mutex_unlock(&pinctrl_mutex);
759
760         return s;
761 }
762 EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
763
764 static int pinctrl_select_state_locked(struct pinctrl *p,
765                                        struct pinctrl_state *state)
766 {
767         struct pinctrl_setting *setting, *setting2;
768         int ret;
769
770         if (p->state == state)
771                 return 0;
772
773         if (p->state) {
774                 /*
775                  * The set of groups with a mux configuration in the old state
776                  * may not be identical to the set of groups with a mux setting
777                  * in the new state. While this might be unusual, it's entirely
778                  * possible for the "user"-supplied mapping table to be written
779                  * that way. For each group that was configured in the old state
780                  * but not in the new state, this code puts that group into a
781                  * safe/disabled state.
782                  */
783                 list_for_each_entry(setting, &p->state->settings, node) {
784                         bool found = false;
785                         if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
786                                 continue;
787                         list_for_each_entry(setting2, &state->settings, node) {
788                                 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
789                                         continue;
790                                 if (setting2->data.mux.group ==
791                                                 setting->data.mux.group) {
792                                         found = true;
793                                         break;
794                                 }
795                         }
796                         if (!found)
797                                 pinmux_disable_setting(setting);
798                 }
799         }
800
801         p->state = state;
802
803         /* Apply all the settings for the new state */
804         list_for_each_entry(setting, &state->settings, node) {
805                 switch (setting->type) {
806                 case PIN_MAP_TYPE_MUX_GROUP:
807                         ret = pinmux_enable_setting(setting);
808                         break;
809                 case PIN_MAP_TYPE_CONFIGS_PIN:
810                 case PIN_MAP_TYPE_CONFIGS_GROUP:
811                         ret = pinconf_apply_setting(setting);
812                         break;
813                 default:
814                         ret = -EINVAL;
815                         break;
816                 }
817                 if (ret < 0) {
818                         /* FIXME: Difficult to return to prev state */
819                         return ret;
820                 }
821         }
822
823         return 0;
824 }
825
826 /**
827  * pinctrl_select() - select/activate/program a pinctrl state to HW
828  * @p: the pinctrl handle for the device that requests configuratio
829  * @state: the state handle to select/activate/program
830  */
831 int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
832 {
833         int ret;
834
835         mutex_lock(&pinctrl_mutex);
836         ret = pinctrl_select_state_locked(p, state);
837         mutex_unlock(&pinctrl_mutex);
838
839         return ret;
840 }
841 EXPORT_SYMBOL_GPL(pinctrl_select_state);
842
843 static void devm_pinctrl_release(struct device *dev, void *res)
844 {
845         pinctrl_put(*(struct pinctrl **)res);
846 }
847
848 /**
849  * struct devm_pinctrl_get() - Resource managed pinctrl_get()
850  * @dev: the device to obtain the handle for
851  *
852  * If there is a need to explicitly destroy the returned struct pinctrl,
853  * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
854  */
855 struct pinctrl *devm_pinctrl_get(struct device *dev)
856 {
857         struct pinctrl **ptr, *p;
858
859         ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
860         if (!ptr)
861                 return ERR_PTR(-ENOMEM);
862
863         p = pinctrl_get(dev);
864         if (!IS_ERR(p)) {
865                 *ptr = p;
866                 devres_add(dev, ptr);
867         } else {
868                 devres_free(ptr);
869         }
870
871         return p;
872 }
873 EXPORT_SYMBOL_GPL(devm_pinctrl_get);
874
875 static int devm_pinctrl_match(struct device *dev, void *res, void *data)
876 {
877         struct pinctrl **p = res;
878
879         return *p == data;
880 }
881
882 /**
883  * devm_pinctrl_put() - Resource managed pinctrl_put()
884  * @p: the pinctrl handle to release
885  *
886  * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
887  * this function will not need to be called and the resource management
888  * code will ensure that the resource is freed.
889  */
890 void devm_pinctrl_put(struct pinctrl *p)
891 {
892         WARN_ON(devres_destroy(p->dev, devm_pinctrl_release,
893                                devm_pinctrl_match, p));
894         pinctrl_put(p);
895 }
896 EXPORT_SYMBOL_GPL(devm_pinctrl_put);
897
898 int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
899                          bool dup, bool locked)
900 {
901         int i, ret;
902         struct pinctrl_maps *maps_node;
903
904         pr_debug("add %d pinmux maps\n", num_maps);
905
906         /* First sanity check the new mapping */
907         for (i = 0; i < num_maps; i++) {
908                 if (!maps[i].dev_name) {
909                         pr_err("failed to register map %s (%d): no device given\n",
910                                maps[i].name, i);
911                         return -EINVAL;
912                 }
913
914                 if (!maps[i].name) {
915                         pr_err("failed to register map %d: no map name given\n",
916                                i);
917                         return -EINVAL;
918                 }
919
920                 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
921                                 !maps[i].ctrl_dev_name) {
922                         pr_err("failed to register map %s (%d): no pin control device given\n",
923                                maps[i].name, i);
924                         return -EINVAL;
925                 }
926
927                 switch (maps[i].type) {
928                 case PIN_MAP_TYPE_DUMMY_STATE:
929                         break;
930                 case PIN_MAP_TYPE_MUX_GROUP:
931                         ret = pinmux_validate_map(&maps[i], i);
932                         if (ret < 0)
933                                 return ret;
934                         break;
935                 case PIN_MAP_TYPE_CONFIGS_PIN:
936                 case PIN_MAP_TYPE_CONFIGS_GROUP:
937                         ret = pinconf_validate_map(&maps[i], i);
938                         if (ret < 0)
939                                 return ret;
940                         break;
941                 default:
942                         pr_err("failed to register map %s (%d): invalid type given\n",
943                                maps[i].name, i);
944                         return -EINVAL;
945                 }
946         }
947
948         maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
949         if (!maps_node) {
950                 pr_err("failed to alloc struct pinctrl_maps\n");
951                 return -ENOMEM;
952         }
953
954         maps_node->num_maps = num_maps;
955         if (dup) {
956                 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
957                                           GFP_KERNEL);
958                 if (!maps_node->maps) {
959                         pr_err("failed to duplicate mapping table\n");
960                         kfree(maps_node);
961                         return -ENOMEM;
962                 }
963         } else {
964                 maps_node->maps = maps;
965         }
966
967         if (!locked)
968                 mutex_lock(&pinctrl_mutex);
969         list_add_tail(&maps_node->node, &pinctrl_maps);
970         if (!locked)
971                 mutex_unlock(&pinctrl_mutex);
972
973         return 0;
974 }
975
976 /**
977  * pinctrl_register_mappings() - register a set of pin controller mappings
978  * @maps: the pincontrol mappings table to register. This should probably be
979  *      marked with __initdata so it can be discarded after boot. This
980  *      function will perform a shallow copy for the mapping entries.
981  * @num_maps: the number of maps in the mapping table
982  */
983 int pinctrl_register_mappings(struct pinctrl_map const *maps,
984                               unsigned num_maps)
985 {
986         return pinctrl_register_map(maps, num_maps, true, false);
987 }
988
989 void pinctrl_unregister_map(struct pinctrl_map const *map)
990 {
991         struct pinctrl_maps *maps_node;
992
993         list_for_each_entry(maps_node, &pinctrl_maps, node) {
994                 if (maps_node->maps == map) {
995                         list_del(&maps_node->node);
996                         return;
997                 }
998         }
999 }
1000
1001 #ifdef CONFIG_DEBUG_FS
1002
1003 static int pinctrl_pins_show(struct seq_file *s, void *what)
1004 {
1005         struct pinctrl_dev *pctldev = s->private;
1006         const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1007         unsigned i, pin;
1008
1009         seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
1010
1011         mutex_lock(&pinctrl_mutex);
1012
1013         /* The pin number can be retrived from the pin controller descriptor */
1014         for (i = 0; i < pctldev->desc->npins; i++) {
1015                 struct pin_desc *desc;
1016
1017                 pin = pctldev->desc->pins[i].number;
1018                 desc = pin_desc_get(pctldev, pin);
1019                 /* Pin space may be sparse */
1020                 if (desc == NULL)
1021                         continue;
1022
1023                 seq_printf(s, "pin %d (%s) ", pin,
1024                            desc->name ? desc->name : "unnamed");
1025
1026                 /* Driver-specific info per pin */
1027                 if (ops->pin_dbg_show)
1028                         ops->pin_dbg_show(pctldev, s, pin);
1029
1030                 seq_puts(s, "\n");
1031         }
1032
1033         mutex_unlock(&pinctrl_mutex);
1034
1035         return 0;
1036 }
1037
1038 static int pinctrl_groups_show(struct seq_file *s, void *what)
1039 {
1040         struct pinctrl_dev *pctldev = s->private;
1041         const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1042         unsigned ngroups, selector = 0;
1043
1044         ngroups = ops->get_groups_count(pctldev);
1045         mutex_lock(&pinctrl_mutex);
1046
1047         seq_puts(s, "registered pin groups:\n");
1048         while (selector < ngroups) {
1049                 const unsigned *pins;
1050                 unsigned num_pins;
1051                 const char *gname = ops->get_group_name(pctldev, selector);
1052                 const char *pname;
1053                 int ret;
1054                 int i;
1055
1056                 ret = ops->get_group_pins(pctldev, selector,
1057                                           &pins, &num_pins);
1058                 if (ret)
1059                         seq_printf(s, "%s [ERROR GETTING PINS]\n",
1060                                    gname);
1061                 else {
1062                         seq_printf(s, "group: %s\n", gname);
1063                         for (i = 0; i < num_pins; i++) {
1064                                 pname = pin_get_name(pctldev, pins[i]);
1065                                 if (WARN_ON(!pname))
1066                                         return -EINVAL;
1067                                 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1068                         }
1069                         seq_puts(s, "\n");
1070                 }
1071                 selector++;
1072         }
1073
1074         mutex_unlock(&pinctrl_mutex);
1075
1076         return 0;
1077 }
1078
1079 static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1080 {
1081         struct pinctrl_dev *pctldev = s->private;
1082         struct pinctrl_gpio_range *range = NULL;
1083
1084         seq_puts(s, "GPIO ranges handled:\n");
1085
1086         mutex_lock(&pinctrl_mutex);
1087
1088         /* Loop over the ranges */
1089         list_for_each_entry(range, &pctldev->gpio_ranges, node) {
1090                 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1091                            range->id, range->name,
1092                            range->base, (range->base + range->npins - 1),
1093                            range->pin_base,
1094                            (range->pin_base + range->npins - 1));
1095         }
1096
1097         mutex_unlock(&pinctrl_mutex);
1098
1099         return 0;
1100 }
1101
1102 static int pinctrl_devices_show(struct seq_file *s, void *what)
1103 {
1104         struct pinctrl_dev *pctldev;
1105
1106         seq_puts(s, "name [pinmux] [pinconf]\n");
1107
1108         mutex_lock(&pinctrl_mutex);
1109
1110         list_for_each_entry(pctldev, &pinctrldev_list, node) {
1111                 seq_printf(s, "%s ", pctldev->desc->name);
1112                 if (pctldev->desc->pmxops)
1113                         seq_puts(s, "yes ");
1114                 else
1115                         seq_puts(s, "no ");
1116                 if (pctldev->desc->confops)
1117                         seq_puts(s, "yes");
1118                 else
1119                         seq_puts(s, "no");
1120                 seq_puts(s, "\n");
1121         }
1122
1123         mutex_unlock(&pinctrl_mutex);
1124
1125         return 0;
1126 }
1127
1128 static inline const char *map_type(enum pinctrl_map_type type)
1129 {
1130         static const char * const names[] = {
1131                 "INVALID",
1132                 "DUMMY_STATE",
1133                 "MUX_GROUP",
1134                 "CONFIGS_PIN",
1135                 "CONFIGS_GROUP",
1136         };
1137
1138         if (type >= ARRAY_SIZE(names))
1139                 return "UNKNOWN";
1140
1141         return names[type];
1142 }
1143
1144 static int pinctrl_maps_show(struct seq_file *s, void *what)
1145 {
1146         struct pinctrl_maps *maps_node;
1147         int i;
1148         struct pinctrl_map const *map;
1149
1150         seq_puts(s, "Pinctrl maps:\n");
1151
1152         mutex_lock(&pinctrl_mutex);
1153
1154         for_each_maps(maps_node, i, map) {
1155                 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1156                            map->dev_name, map->name, map_type(map->type),
1157                            map->type);
1158
1159                 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1160                         seq_printf(s, "controlling device %s\n",
1161                                    map->ctrl_dev_name);
1162
1163                 switch (map->type) {
1164                 case PIN_MAP_TYPE_MUX_GROUP:
1165                         pinmux_show_map(s, map);
1166                         break;
1167                 case PIN_MAP_TYPE_CONFIGS_PIN:
1168                 case PIN_MAP_TYPE_CONFIGS_GROUP:
1169                         pinconf_show_map(s, map);
1170                         break;
1171                 default:
1172                         break;
1173                 }
1174
1175                 seq_printf(s, "\n");
1176         }
1177
1178         mutex_unlock(&pinctrl_mutex);
1179
1180         return 0;
1181 }
1182
1183 static int pinctrl_show(struct seq_file *s, void *what)
1184 {
1185         struct pinctrl *p;
1186         struct pinctrl_state *state;
1187         struct pinctrl_setting *setting;
1188
1189         seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
1190
1191         mutex_lock(&pinctrl_mutex);
1192
1193         list_for_each_entry(p, &pinctrl_list, node) {
1194                 seq_printf(s, "device: %s current state: %s\n",
1195                            dev_name(p->dev),
1196                            p->state ? p->state->name : "none");
1197
1198                 list_for_each_entry(state, &p->states, node) {
1199                         seq_printf(s, "  state: %s\n", state->name);
1200
1201                         list_for_each_entry(setting, &state->settings, node) {
1202                                 struct pinctrl_dev *pctldev = setting->pctldev;
1203
1204                                 seq_printf(s, "    type: %s controller %s ",
1205                                            map_type(setting->type),
1206                                            pinctrl_dev_get_name(pctldev));
1207
1208                                 switch (setting->type) {
1209                                 case PIN_MAP_TYPE_MUX_GROUP:
1210                                         pinmux_show_setting(s, setting);
1211                                         break;
1212                                 case PIN_MAP_TYPE_CONFIGS_PIN:
1213                                 case PIN_MAP_TYPE_CONFIGS_GROUP:
1214                                         pinconf_show_setting(s, setting);
1215                                         break;
1216                                 default:
1217                                         break;
1218                                 }
1219                         }
1220                 }
1221         }
1222
1223         mutex_unlock(&pinctrl_mutex);
1224
1225         return 0;
1226 }
1227
1228 static int pinctrl_pins_open(struct inode *inode, struct file *file)
1229 {
1230         return single_open(file, pinctrl_pins_show, inode->i_private);
1231 }
1232
1233 static int pinctrl_groups_open(struct inode *inode, struct file *file)
1234 {
1235         return single_open(file, pinctrl_groups_show, inode->i_private);
1236 }
1237
1238 static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1239 {
1240         return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1241 }
1242
1243 static int pinctrl_devices_open(struct inode *inode, struct file *file)
1244 {
1245         return single_open(file, pinctrl_devices_show, NULL);
1246 }
1247
1248 static int pinctrl_maps_open(struct inode *inode, struct file *file)
1249 {
1250         return single_open(file, pinctrl_maps_show, NULL);
1251 }
1252
1253 static int pinctrl_open(struct inode *inode, struct file *file)
1254 {
1255         return single_open(file, pinctrl_show, NULL);
1256 }
1257
1258 static const struct file_operations pinctrl_pins_ops = {
1259         .open           = pinctrl_pins_open,
1260         .read           = seq_read,
1261         .llseek         = seq_lseek,
1262         .release        = single_release,
1263 };
1264
1265 static const struct file_operations pinctrl_groups_ops = {
1266         .open           = pinctrl_groups_open,
1267         .read           = seq_read,
1268         .llseek         = seq_lseek,
1269         .release        = single_release,
1270 };
1271
1272 static const struct file_operations pinctrl_gpioranges_ops = {
1273         .open           = pinctrl_gpioranges_open,
1274         .read           = seq_read,
1275         .llseek         = seq_lseek,
1276         .release        = single_release,
1277 };
1278
1279 static const struct file_operations pinctrl_devices_ops = {
1280         .open           = pinctrl_devices_open,
1281         .read           = seq_read,
1282         .llseek         = seq_lseek,
1283         .release        = single_release,
1284 };
1285
1286 static const struct file_operations pinctrl_maps_ops = {
1287         .open           = pinctrl_maps_open,
1288         .read           = seq_read,
1289         .llseek         = seq_lseek,
1290         .release        = single_release,
1291 };
1292
1293 static const struct file_operations pinctrl_ops = {
1294         .open           = pinctrl_open,
1295         .read           = seq_read,
1296         .llseek         = seq_lseek,
1297         .release        = single_release,
1298 };
1299
1300 static struct dentry *debugfs_root;
1301
1302 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1303 {
1304         struct dentry *device_root;
1305
1306         device_root = debugfs_create_dir(dev_name(pctldev->dev),
1307                                          debugfs_root);
1308         pctldev->device_root = device_root;
1309
1310         if (IS_ERR(device_root) || !device_root) {
1311                 pr_warn("failed to create debugfs directory for %s\n",
1312                         dev_name(pctldev->dev));
1313                 return;
1314         }
1315         debugfs_create_file("pins", S_IFREG | S_IRUGO,
1316                             device_root, pctldev, &pinctrl_pins_ops);
1317         debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1318                             device_root, pctldev, &pinctrl_groups_ops);
1319         debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1320                             device_root, pctldev, &pinctrl_gpioranges_ops);
1321         pinmux_init_device_debugfs(device_root, pctldev);
1322         pinconf_init_device_debugfs(device_root, pctldev);
1323 }
1324
1325 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1326 {
1327         debugfs_remove_recursive(pctldev->device_root);
1328 }
1329
1330 static void pinctrl_init_debugfs(void)
1331 {
1332         debugfs_root = debugfs_create_dir("pinctrl", NULL);
1333         if (IS_ERR(debugfs_root) || !debugfs_root) {
1334                 pr_warn("failed to create debugfs directory\n");
1335                 debugfs_root = NULL;
1336                 return;
1337         }
1338
1339         debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1340                             debugfs_root, NULL, &pinctrl_devices_ops);
1341         debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1342                             debugfs_root, NULL, &pinctrl_maps_ops);
1343         debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1344                             debugfs_root, NULL, &pinctrl_ops);
1345 }
1346
1347 #else /* CONFIG_DEBUG_FS */
1348
1349 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1350 {
1351 }
1352
1353 static void pinctrl_init_debugfs(void)
1354 {
1355 }
1356
1357 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1358 {
1359 }
1360
1361 #endif
1362
1363 static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1364 {
1365         const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1366
1367         if (!ops ||
1368             !ops->get_groups_count ||
1369             !ops->get_group_name ||
1370             !ops->get_group_pins)
1371                 return -EINVAL;
1372
1373         if (ops->dt_node_to_map && !ops->dt_free_map)
1374                 return -EINVAL;
1375
1376         return 0;
1377 }
1378
1379 /**
1380  * pinctrl_register() - register a pin controller device
1381  * @pctldesc: descriptor for this pin controller
1382  * @dev: parent device for this pin controller
1383  * @driver_data: private pin controller data for this pin controller
1384  */
1385 struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1386                                     struct device *dev, void *driver_data)
1387 {
1388         struct pinctrl_dev *pctldev;
1389         int ret;
1390
1391         if (!pctldesc)
1392                 return NULL;
1393         if (!pctldesc->name)
1394                 return NULL;
1395
1396         pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
1397         if (pctldev == NULL) {
1398                 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
1399                 return NULL;
1400         }
1401
1402         /* Initialize pin control device struct */
1403         pctldev->owner = pctldesc->owner;
1404         pctldev->desc = pctldesc;
1405         pctldev->driver_data = driver_data;
1406         INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
1407         INIT_LIST_HEAD(&pctldev->gpio_ranges);
1408         pctldev->dev = dev;
1409
1410         /* check core ops for sanity */
1411         if (pinctrl_check_ops(pctldev)) {
1412                 dev_err(dev, "pinctrl ops lacks necessary functions\n");
1413                 goto out_err;
1414         }
1415
1416         /* If we're implementing pinmuxing, check the ops for sanity */
1417         if (pctldesc->pmxops) {
1418                 if (pinmux_check_ops(pctldev))
1419                         goto out_err;
1420         }
1421
1422         /* If we're implementing pinconfig, check the ops for sanity */
1423         if (pctldesc->confops) {
1424                 if (pinconf_check_ops(pctldev))
1425                         goto out_err;
1426         }
1427
1428         /* Register all the pins */
1429         dev_dbg(dev, "try to register %d pins ...\n",  pctldesc->npins);
1430         ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1431         if (ret) {
1432                 dev_err(dev, "error during pin registration\n");
1433                 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1434                                       pctldesc->npins);
1435                 goto out_err;
1436         }
1437
1438         mutex_lock(&pinctrl_mutex);
1439
1440         list_add_tail(&pctldev->node, &pinctrldev_list);
1441
1442         pctldev->p = pinctrl_get_locked(pctldev->dev);
1443         if (!IS_ERR(pctldev->p)) {
1444                 struct pinctrl_state *s =
1445                         pinctrl_lookup_state_locked(pctldev->p,
1446                                                     PINCTRL_STATE_DEFAULT);
1447                 if (IS_ERR(s)) {
1448                         dev_dbg(dev, "failed to lookup the default state\n");
1449                 } else {
1450                         if (pinctrl_select_state_locked(pctldev->p, s))
1451                                 dev_err(dev,
1452                                         "failed to select default state\n");
1453                 }
1454         }
1455
1456         mutex_unlock(&pinctrl_mutex);
1457
1458         pinctrl_init_device_debugfs(pctldev);
1459
1460         return pctldev;
1461
1462 out_err:
1463         kfree(pctldev);
1464         return NULL;
1465 }
1466 EXPORT_SYMBOL_GPL(pinctrl_register);
1467
1468 /**
1469  * pinctrl_unregister() - unregister pinmux
1470  * @pctldev: pin controller to unregister
1471  *
1472  * Called by pinmux drivers to unregister a pinmux.
1473  */
1474 void pinctrl_unregister(struct pinctrl_dev *pctldev)
1475 {
1476         struct pinctrl_gpio_range *range, *n;
1477         if (pctldev == NULL)
1478                 return;
1479
1480         pinctrl_remove_device_debugfs(pctldev);
1481
1482         mutex_lock(&pinctrl_mutex);
1483
1484         if (!IS_ERR(pctldev->p))
1485                 pinctrl_put_locked(pctldev->p, true);
1486
1487         /* TODO: check that no pinmuxes are still active? */
1488         list_del(&pctldev->node);
1489         /* Destroy descriptor tree */
1490         pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1491                               pctldev->desc->npins);
1492         /* remove gpio ranges map */
1493         list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1494                 list_del(&range->node);
1495
1496         kfree(pctldev);
1497
1498         mutex_unlock(&pinctrl_mutex);
1499 }
1500 EXPORT_SYMBOL_GPL(pinctrl_unregister);
1501
1502 static int __init pinctrl_init(void)
1503 {
1504         pr_info("initialized pinctrl subsystem\n");
1505         pinctrl_init_debugfs();
1506         return 0;
1507 }
1508
1509 /* init early since many drivers really need to initialized pinmux early */
1510 core_initcall(pinctrl_init);