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