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