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