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