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