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