xref: /linux/drivers/pinctrl/core.c (revision fdfc374af5dc345fbb9686921fa60176c1c41da0)
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 *p;
1026 
1027 	mutex_lock(&pinctrl_list_mutex);
1028 	list_for_each_entry(p, &pinctrl_list, node)
1029 		if (p->dev == dev) {
1030 			mutex_unlock(&pinctrl_list_mutex);
1031 			return p;
1032 		}
1033 
1034 	mutex_unlock(&pinctrl_list_mutex);
1035 	return NULL;
1036 }
1037 
1038 static void pinctrl_free(struct pinctrl *p, bool inlist);
1039 
1040 static struct pinctrl *create_pinctrl(struct device *dev,
1041 				      struct pinctrl_dev *pctldev)
1042 {
1043 	struct pinctrl *p;
1044 	const char *devname;
1045 	struct pinctrl_maps *maps_node;
1046 	const struct pinctrl_map *map;
1047 	int ret;
1048 
1049 	/*
1050 	 * create the state cookie holder struct pinctrl for each
1051 	 * mapping, this is what consumers will get when requesting
1052 	 * a pin control handle with pinctrl_get()
1053 	 */
1054 	p = kzalloc(sizeof(*p), GFP_KERNEL);
1055 	if (!p)
1056 		return ERR_PTR(-ENOMEM);
1057 	p->dev = dev;
1058 	INIT_LIST_HEAD(&p->states);
1059 	INIT_LIST_HEAD(&p->dt_maps);
1060 
1061 	ret = pinctrl_dt_to_map(p, pctldev);
1062 	if (ret < 0) {
1063 		kfree(p);
1064 		return ERR_PTR(ret);
1065 	}
1066 
1067 	devname = dev_name(dev);
1068 
1069 	mutex_lock(&pinctrl_maps_mutex);
1070 	/* Iterate over the pin control maps to locate the right ones */
1071 	for_each_pin_map(maps_node, map) {
1072 		/* Map must be for this device */
1073 		if (strcmp(map->dev_name, devname))
1074 			continue;
1075 		/*
1076 		 * If pctldev is not null, we are claiming hog for it,
1077 		 * that means, setting that is served by pctldev by itself.
1078 		 *
1079 		 * Thus we must skip map that is for this device but is served
1080 		 * by other device.
1081 		 */
1082 		if (pctldev &&
1083 		    strcmp(dev_name(pctldev->dev), map->ctrl_dev_name))
1084 			continue;
1085 
1086 		ret = add_setting(p, pctldev, map);
1087 		/*
1088 		 * At this point the adding of a setting may:
1089 		 *
1090 		 * - Defer, if the pinctrl device is not yet available
1091 		 * - Fail, if the pinctrl device is not yet available,
1092 		 *   AND the setting is a hog. We cannot defer that, since
1093 		 *   the hog will kick in immediately after the device
1094 		 *   is registered.
1095 		 *
1096 		 * If the error returned was not -EPROBE_DEFER then we
1097 		 * accumulate the errors to see if we end up with
1098 		 * an -EPROBE_DEFER later, as that is the worst case.
1099 		 */
1100 		if (ret == -EPROBE_DEFER) {
1101 			pinctrl_free(p, false);
1102 			mutex_unlock(&pinctrl_maps_mutex);
1103 			return ERR_PTR(ret);
1104 		}
1105 	}
1106 	mutex_unlock(&pinctrl_maps_mutex);
1107 
1108 	if (ret < 0) {
1109 		/* If some other error than deferral occurred, return here */
1110 		pinctrl_free(p, false);
1111 		return ERR_PTR(ret);
1112 	}
1113 
1114 	kref_init(&p->users);
1115 
1116 	/* Add the pinctrl handle to the global list */
1117 	mutex_lock(&pinctrl_list_mutex);
1118 	list_add_tail(&p->node, &pinctrl_list);
1119 	mutex_unlock(&pinctrl_list_mutex);
1120 
1121 	return p;
1122 }
1123 
1124 /**
1125  * pinctrl_get() - retrieves the pinctrl handle for a device
1126  * @dev: the device to obtain the handle for
1127  */
1128 struct pinctrl *pinctrl_get(struct device *dev)
1129 {
1130 	struct pinctrl *p;
1131 
1132 	if (WARN_ON(!dev))
1133 		return ERR_PTR(-EINVAL);
1134 
1135 	/*
1136 	 * See if somebody else (such as the device core) has already
1137 	 * obtained a handle to the pinctrl for this device. In that case,
1138 	 * return another pointer to it.
1139 	 */
1140 	p = find_pinctrl(dev);
1141 	if (p) {
1142 		dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
1143 		kref_get(&p->users);
1144 		return p;
1145 	}
1146 
1147 	return create_pinctrl(dev, NULL);
1148 }
1149 EXPORT_SYMBOL_GPL(pinctrl_get);
1150 
1151 static void pinctrl_free_setting(bool disable_setting,
1152 				 struct pinctrl_setting *setting)
1153 {
1154 	switch (setting->type) {
1155 	case PIN_MAP_TYPE_MUX_GROUP:
1156 		if (disable_setting)
1157 			pinmux_disable_setting(setting);
1158 		pinmux_free_setting(setting);
1159 		break;
1160 	case PIN_MAP_TYPE_CONFIGS_PIN:
1161 	case PIN_MAP_TYPE_CONFIGS_GROUP:
1162 		pinconf_free_setting(setting);
1163 		break;
1164 	default:
1165 		break;
1166 	}
1167 }
1168 
1169 static void pinctrl_free(struct pinctrl *p, bool inlist)
1170 {
1171 	struct pinctrl_state *state, *n1;
1172 	struct pinctrl_setting *setting, *n2;
1173 
1174 	mutex_lock(&pinctrl_list_mutex);
1175 	list_for_each_entry_safe(state, n1, &p->states, node) {
1176 		list_for_each_entry_safe(setting, n2, &state->settings, node) {
1177 			pinctrl_free_setting(state == p->state, setting);
1178 			list_del(&setting->node);
1179 			kfree(setting);
1180 		}
1181 		list_del(&state->node);
1182 		kfree(state);
1183 	}
1184 
1185 	pinctrl_dt_free_maps(p);
1186 
1187 	if (inlist)
1188 		list_del(&p->node);
1189 	kfree(p);
1190 	mutex_unlock(&pinctrl_list_mutex);
1191 }
1192 
1193 /**
1194  * pinctrl_release() - release the pinctrl handle
1195  * @kref: the kref in the pinctrl being released
1196  */
1197 static void pinctrl_release(struct kref *kref)
1198 {
1199 	struct pinctrl *p = container_of(kref, struct pinctrl, users);
1200 
1201 	pinctrl_free(p, true);
1202 }
1203 
1204 /**
1205  * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
1206  * @p: the pinctrl handle to release
1207  */
1208 void pinctrl_put(struct pinctrl *p)
1209 {
1210 	kref_put(&p->users, pinctrl_release);
1211 }
1212 EXPORT_SYMBOL_GPL(pinctrl_put);
1213 
1214 /**
1215  * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
1216  * @p: the pinctrl handle to retrieve the state from
1217  * @name: the state name to retrieve
1218  */
1219 struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
1220 						 const char *name)
1221 {
1222 	struct pinctrl_state *state;
1223 
1224 	state = find_state(p, name);
1225 	if (!state) {
1226 		if (pinctrl_dummy_state) {
1227 			/* create dummy state */
1228 			dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
1229 				name);
1230 			state = create_state(p, name);
1231 		} else
1232 			state = ERR_PTR(-ENODEV);
1233 	}
1234 
1235 	return state;
1236 }
1237 EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
1238 
1239 static void pinctrl_link_add(struct pinctrl_dev *pctldev,
1240 			     struct device *consumer)
1241 {
1242 	if (pctldev->desc->link_consumers)
1243 		device_link_add(consumer, pctldev->dev,
1244 				DL_FLAG_PM_RUNTIME |
1245 				DL_FLAG_AUTOREMOVE_CONSUMER);
1246 }
1247 
1248 /**
1249  * pinctrl_commit_state() - select/activate/program a pinctrl state to HW
1250  * @p: the pinctrl handle for the device that requests configuration
1251  * @state: the state handle to select/activate/program
1252  */
1253 static int pinctrl_commit_state(struct pinctrl *p, struct pinctrl_state *state)
1254 {
1255 	struct pinctrl_setting *setting, *setting2;
1256 	struct pinctrl_state *old_state = p->state;
1257 	int ret;
1258 
1259 	if (p->state) {
1260 		/*
1261 		 * For each pinmux setting in the old state, forget SW's record
1262 		 * of mux owner for that pingroup. Any pingroups which are
1263 		 * still owned by the new state will be re-acquired by the call
1264 		 * to pinmux_enable_setting() in the loop below.
1265 		 */
1266 		list_for_each_entry(setting, &p->state->settings, node) {
1267 			if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
1268 				continue;
1269 			pinmux_disable_setting(setting);
1270 		}
1271 	}
1272 
1273 	p->state = NULL;
1274 
1275 	/* Apply all the settings for the new state - pinmux first */
1276 	list_for_each_entry(setting, &state->settings, node) {
1277 		switch (setting->type) {
1278 		case PIN_MAP_TYPE_MUX_GROUP:
1279 			ret = pinmux_enable_setting(setting);
1280 			break;
1281 		case PIN_MAP_TYPE_CONFIGS_PIN:
1282 		case PIN_MAP_TYPE_CONFIGS_GROUP:
1283 			ret = 0;
1284 			break;
1285 		default:
1286 			ret = -EINVAL;
1287 			break;
1288 		}
1289 
1290 		if (ret < 0)
1291 			goto unapply_new_state;
1292 
1293 		/* Do not link hogs (circular dependency) */
1294 		if (p != setting->pctldev->p)
1295 			pinctrl_link_add(setting->pctldev, p->dev);
1296 	}
1297 
1298 	/* Apply all the settings for the new state - pinconf after */
1299 	list_for_each_entry(setting, &state->settings, node) {
1300 		switch (setting->type) {
1301 		case PIN_MAP_TYPE_MUX_GROUP:
1302 			ret = 0;
1303 			break;
1304 		case PIN_MAP_TYPE_CONFIGS_PIN:
1305 		case PIN_MAP_TYPE_CONFIGS_GROUP:
1306 			ret = pinconf_apply_setting(setting);
1307 			break;
1308 		default:
1309 			ret = -EINVAL;
1310 			break;
1311 		}
1312 
1313 		if (ret < 0) {
1314 			goto unapply_new_state;
1315 		}
1316 
1317 		/* Do not link hogs (circular dependency) */
1318 		if (p != setting->pctldev->p)
1319 			pinctrl_link_add(setting->pctldev, p->dev);
1320 	}
1321 
1322 	p->state = state;
1323 
1324 	return 0;
1325 
1326 unapply_new_state:
1327 	dev_err(p->dev, "Error applying setting, reverse things back\n");
1328 
1329 	list_for_each_entry(setting2, &state->settings, node) {
1330 		if (&setting2->node == &setting->node)
1331 			break;
1332 		/*
1333 		 * All we can do here is pinmux_disable_setting.
1334 		 * That means that some pins are muxed differently now
1335 		 * than they were before applying the setting (We can't
1336 		 * "unmux a pin"!), but it's not a big deal since the pins
1337 		 * are free to be muxed by another apply_setting.
1338 		 */
1339 		if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
1340 			pinmux_disable_setting(setting2);
1341 	}
1342 
1343 	/* There's no infinite recursive loop here because p->state is NULL */
1344 	if (old_state)
1345 		pinctrl_select_state(p, old_state);
1346 
1347 	return ret;
1348 }
1349 
1350 /**
1351  * pinctrl_select_state() - select/activate/program a pinctrl state to HW
1352  * @p: the pinctrl handle for the device that requests configuration
1353  * @state: the state handle to select/activate/program
1354  */
1355 int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
1356 {
1357 	if (p->state == state)
1358 		return 0;
1359 
1360 	return pinctrl_commit_state(p, state);
1361 }
1362 EXPORT_SYMBOL_GPL(pinctrl_select_state);
1363 
1364 static void devm_pinctrl_release(struct device *dev, void *res)
1365 {
1366 	pinctrl_put(*(struct pinctrl **)res);
1367 }
1368 
1369 /**
1370  * devm_pinctrl_get() - Resource managed pinctrl_get()
1371  * @dev: the device to obtain the handle for
1372  *
1373  * If there is a need to explicitly destroy the returned struct pinctrl,
1374  * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1375  */
1376 struct pinctrl *devm_pinctrl_get(struct device *dev)
1377 {
1378 	struct pinctrl **ptr, *p;
1379 
1380 	ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
1381 	if (!ptr)
1382 		return ERR_PTR(-ENOMEM);
1383 
1384 	p = pinctrl_get(dev);
1385 	if (!IS_ERR(p)) {
1386 		*ptr = p;
1387 		devres_add(dev, ptr);
1388 	} else {
1389 		devres_free(ptr);
1390 	}
1391 
1392 	return p;
1393 }
1394 EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1395 
1396 static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1397 {
1398 	struct pinctrl **p = res;
1399 
1400 	return *p == data;
1401 }
1402 
1403 /**
1404  * devm_pinctrl_put() - Resource managed pinctrl_put()
1405  * @p: the pinctrl handle to release
1406  *
1407  * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1408  * this function will not need to be called and the resource management
1409  * code will ensure that the resource is freed.
1410  */
1411 void devm_pinctrl_put(struct pinctrl *p)
1412 {
1413 	WARN_ON(devres_release(p->dev, devm_pinctrl_release,
1414 			       devm_pinctrl_match, p));
1415 }
1416 EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1417 
1418 /**
1419  * pinctrl_register_mappings() - register a set of pin controller mappings
1420  * @maps: the pincontrol mappings table to register. Note the pinctrl-core
1421  *	keeps a reference to the passed in maps, so they should _not_ be
1422  *	marked with __initdata.
1423  * @num_maps: the number of maps in the mapping table
1424  */
1425 int pinctrl_register_mappings(const struct pinctrl_map *maps,
1426 			      unsigned num_maps)
1427 {
1428 	int i, ret;
1429 	struct pinctrl_maps *maps_node;
1430 
1431 	pr_debug("add %u pinctrl maps\n", num_maps);
1432 
1433 	/* First sanity check the new mapping */
1434 	for (i = 0; i < num_maps; i++) {
1435 		if (!maps[i].dev_name) {
1436 			pr_err("failed to register map %s (%d): no device given\n",
1437 			       maps[i].name, i);
1438 			return -EINVAL;
1439 		}
1440 
1441 		if (!maps[i].name) {
1442 			pr_err("failed to register map %d: no map name given\n",
1443 			       i);
1444 			return -EINVAL;
1445 		}
1446 
1447 		if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1448 				!maps[i].ctrl_dev_name) {
1449 			pr_err("failed to register map %s (%d): no pin control device given\n",
1450 			       maps[i].name, i);
1451 			return -EINVAL;
1452 		}
1453 
1454 		switch (maps[i].type) {
1455 		case PIN_MAP_TYPE_DUMMY_STATE:
1456 			break;
1457 		case PIN_MAP_TYPE_MUX_GROUP:
1458 			ret = pinmux_validate_map(&maps[i], i);
1459 			if (ret < 0)
1460 				return ret;
1461 			break;
1462 		case PIN_MAP_TYPE_CONFIGS_PIN:
1463 		case PIN_MAP_TYPE_CONFIGS_GROUP:
1464 			ret = pinconf_validate_map(&maps[i], i);
1465 			if (ret < 0)
1466 				return ret;
1467 			break;
1468 		default:
1469 			pr_err("failed to register map %s (%d): invalid type given\n",
1470 			       maps[i].name, i);
1471 			return -EINVAL;
1472 		}
1473 	}
1474 
1475 	maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
1476 	if (!maps_node)
1477 		return -ENOMEM;
1478 
1479 	maps_node->maps = maps;
1480 	maps_node->num_maps = num_maps;
1481 
1482 	mutex_lock(&pinctrl_maps_mutex);
1483 	list_add_tail(&maps_node->node, &pinctrl_maps);
1484 	mutex_unlock(&pinctrl_maps_mutex);
1485 
1486 	return 0;
1487 }
1488 EXPORT_SYMBOL_GPL(pinctrl_register_mappings);
1489 
1490 /**
1491  * pinctrl_unregister_mappings() - unregister a set of pin controller mappings
1492  * @map: the pincontrol mappings table passed to pinctrl_register_mappings()
1493  *	when registering the mappings.
1494  */
1495 void pinctrl_unregister_mappings(const struct pinctrl_map *map)
1496 {
1497 	struct pinctrl_maps *maps_node;
1498 
1499 	mutex_lock(&pinctrl_maps_mutex);
1500 	list_for_each_entry(maps_node, &pinctrl_maps, node) {
1501 		if (maps_node->maps == map) {
1502 			list_del(&maps_node->node);
1503 			kfree(maps_node);
1504 			mutex_unlock(&pinctrl_maps_mutex);
1505 			return;
1506 		}
1507 	}
1508 	mutex_unlock(&pinctrl_maps_mutex);
1509 }
1510 EXPORT_SYMBOL_GPL(pinctrl_unregister_mappings);
1511 
1512 /**
1513  * pinctrl_force_sleep() - turn a given controller device into sleep state
1514  * @pctldev: pin controller device
1515  */
1516 int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1517 {
1518 	if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
1519 		return pinctrl_commit_state(pctldev->p, pctldev->hog_sleep);
1520 	return 0;
1521 }
1522 EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1523 
1524 /**
1525  * pinctrl_force_default() - turn a given controller device into default state
1526  * @pctldev: pin controller device
1527  */
1528 int pinctrl_force_default(struct pinctrl_dev *pctldev)
1529 {
1530 	if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
1531 		return pinctrl_commit_state(pctldev->p, pctldev->hog_default);
1532 	return 0;
1533 }
1534 EXPORT_SYMBOL_GPL(pinctrl_force_default);
1535 
1536 /**
1537  * pinctrl_init_done() - tell pinctrl probe is done
1538  *
1539  * We'll use this time to switch the pins from "init" to "default" unless the
1540  * driver selected some other state.
1541  *
1542  * @dev: device to that's done probing
1543  */
1544 int pinctrl_init_done(struct device *dev)
1545 {
1546 	struct dev_pin_info *pins = dev->pins;
1547 	int ret;
1548 
1549 	if (!pins)
1550 		return 0;
1551 
1552 	if (IS_ERR(pins->init_state))
1553 		return 0; /* No such state */
1554 
1555 	if (pins->p->state != pins->init_state)
1556 		return 0; /* Not at init anyway */
1557 
1558 	if (IS_ERR(pins->default_state))
1559 		return 0; /* No default state */
1560 
1561 	ret = pinctrl_select_state(pins->p, pins->default_state);
1562 	if (ret)
1563 		dev_err(dev, "failed to activate default pinctrl state\n");
1564 
1565 	return ret;
1566 }
1567 
1568 static int pinctrl_select_bound_state(struct device *dev,
1569 				      struct pinctrl_state *state)
1570 {
1571 	struct dev_pin_info *pins = dev->pins;
1572 	int ret;
1573 
1574 	if (IS_ERR(state))
1575 		return 0; /* No such state */
1576 	ret = pinctrl_select_state(pins->p, state);
1577 	if (ret)
1578 		dev_err(dev, "failed to activate pinctrl state %s\n",
1579 			state->name);
1580 	return ret;
1581 }
1582 
1583 /**
1584  * pinctrl_select_default_state() - select default pinctrl state
1585  * @dev: device to select default state for
1586  */
1587 int pinctrl_select_default_state(struct device *dev)
1588 {
1589 	if (!dev->pins)
1590 		return 0;
1591 
1592 	return pinctrl_select_bound_state(dev, dev->pins->default_state);
1593 }
1594 EXPORT_SYMBOL_GPL(pinctrl_select_default_state);
1595 
1596 #ifdef CONFIG_PM
1597 
1598 /**
1599  * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1600  * @dev: device to select default state for
1601  */
1602 int pinctrl_pm_select_default_state(struct device *dev)
1603 {
1604 	return pinctrl_select_default_state(dev);
1605 }
1606 EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
1607 
1608 /**
1609  * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1610  * @dev: device to select sleep state for
1611  */
1612 int pinctrl_pm_select_sleep_state(struct device *dev)
1613 {
1614 	if (!dev->pins)
1615 		return 0;
1616 
1617 	return pinctrl_select_bound_state(dev, dev->pins->sleep_state);
1618 }
1619 EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
1620 
1621 /**
1622  * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1623  * @dev: device to select idle state for
1624  */
1625 int pinctrl_pm_select_idle_state(struct device *dev)
1626 {
1627 	if (!dev->pins)
1628 		return 0;
1629 
1630 	return pinctrl_select_bound_state(dev, dev->pins->idle_state);
1631 }
1632 EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
1633 #endif
1634 
1635 #ifdef CONFIG_DEBUG_FS
1636 
1637 static int pinctrl_pins_show(struct seq_file *s, void *what)
1638 {
1639 	struct pinctrl_dev *pctldev = s->private;
1640 	const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1641 	unsigned i, pin;
1642 #ifdef CONFIG_GPIOLIB
1643 	struct pinctrl_gpio_range *range;
1644 	struct gpio_chip *chip;
1645 	int gpio_num;
1646 #endif
1647 
1648 	seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
1649 
1650 	mutex_lock(&pctldev->mutex);
1651 
1652 	/* The pin number can be retrived from the pin controller descriptor */
1653 	for (i = 0; i < pctldev->desc->npins; i++) {
1654 		struct pin_desc *desc;
1655 
1656 		pin = pctldev->desc->pins[i].number;
1657 		desc = pin_desc_get(pctldev, pin);
1658 		/* Pin space may be sparse */
1659 		if (!desc)
1660 			continue;
1661 
1662 		seq_printf(s, "pin %d (%s) ", pin, desc->name);
1663 
1664 #ifdef CONFIG_GPIOLIB
1665 		gpio_num = -1;
1666 		list_for_each_entry(range, &pctldev->gpio_ranges, node) {
1667 			if ((pin >= range->pin_base) &&
1668 			    (pin < (range->pin_base + range->npins))) {
1669 				gpio_num = range->base + (pin - range->pin_base);
1670 				break;
1671 			}
1672 		}
1673 		if (gpio_num >= 0)
1674 			/*
1675 			 * FIXME: gpio_num comes from the global GPIO numberspace.
1676 			 * we need to get rid of the range->base eventually and
1677 			 * get the descriptor directly from the gpio_chip.
1678 			 */
1679 			chip = gpiod_to_chip(gpio_to_desc(gpio_num));
1680 		else
1681 			chip = NULL;
1682 		if (chip)
1683 			seq_printf(s, "%u:%s ", gpio_num - chip->gpiodev->base, chip->label);
1684 		else
1685 			seq_puts(s, "0:? ");
1686 #endif
1687 
1688 		/* Driver-specific info per pin */
1689 		if (ops->pin_dbg_show)
1690 			ops->pin_dbg_show(pctldev, s, pin);
1691 
1692 		seq_puts(s, "\n");
1693 	}
1694 
1695 	mutex_unlock(&pctldev->mutex);
1696 
1697 	return 0;
1698 }
1699 DEFINE_SHOW_ATTRIBUTE(pinctrl_pins);
1700 
1701 static int pinctrl_groups_show(struct seq_file *s, void *what)
1702 {
1703 	struct pinctrl_dev *pctldev = s->private;
1704 	const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1705 	unsigned ngroups, selector = 0;
1706 
1707 	mutex_lock(&pctldev->mutex);
1708 
1709 	ngroups = ops->get_groups_count(pctldev);
1710 
1711 	seq_puts(s, "registered pin groups:\n");
1712 	while (selector < ngroups) {
1713 		const unsigned *pins = NULL;
1714 		unsigned num_pins = 0;
1715 		const char *gname = ops->get_group_name(pctldev, selector);
1716 		const char *pname;
1717 		int ret = 0;
1718 		int i;
1719 
1720 		if (ops->get_group_pins)
1721 			ret = ops->get_group_pins(pctldev, selector,
1722 						  &pins, &num_pins);
1723 		if (ret)
1724 			seq_printf(s, "%s [ERROR GETTING PINS]\n",
1725 				   gname);
1726 		else {
1727 			seq_printf(s, "group: %s\n", gname);
1728 			for (i = 0; i < num_pins; i++) {
1729 				pname = pin_get_name(pctldev, pins[i]);
1730 				if (WARN_ON(!pname)) {
1731 					mutex_unlock(&pctldev->mutex);
1732 					return -EINVAL;
1733 				}
1734 				seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1735 			}
1736 			seq_puts(s, "\n");
1737 		}
1738 		selector++;
1739 	}
1740 
1741 	mutex_unlock(&pctldev->mutex);
1742 
1743 	return 0;
1744 }
1745 DEFINE_SHOW_ATTRIBUTE(pinctrl_groups);
1746 
1747 static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1748 {
1749 	struct pinctrl_dev *pctldev = s->private;
1750 	struct pinctrl_gpio_range *range;
1751 
1752 	seq_puts(s, "GPIO ranges handled:\n");
1753 
1754 	mutex_lock(&pctldev->mutex);
1755 
1756 	/* Loop over the ranges */
1757 	list_for_each_entry(range, &pctldev->gpio_ranges, node) {
1758 		if (range->pins) {
1759 			int a;
1760 			seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
1761 				range->id, range->name,
1762 				range->base, (range->base + range->npins - 1));
1763 			for (a = 0; a < range->npins - 1; a++)
1764 				seq_printf(s, "%u, ", range->pins[a]);
1765 			seq_printf(s, "%u}\n", range->pins[a]);
1766 		}
1767 		else
1768 			seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1769 				range->id, range->name,
1770 				range->base, (range->base + range->npins - 1),
1771 				range->pin_base,
1772 				(range->pin_base + range->npins - 1));
1773 	}
1774 
1775 	mutex_unlock(&pctldev->mutex);
1776 
1777 	return 0;
1778 }
1779 DEFINE_SHOW_ATTRIBUTE(pinctrl_gpioranges);
1780 
1781 static int pinctrl_devices_show(struct seq_file *s, void *what)
1782 {
1783 	struct pinctrl_dev *pctldev;
1784 
1785 	seq_puts(s, "name [pinmux] [pinconf]\n");
1786 
1787 	mutex_lock(&pinctrldev_list_mutex);
1788 
1789 	list_for_each_entry(pctldev, &pinctrldev_list, node) {
1790 		seq_printf(s, "%s ", pctldev->desc->name);
1791 		if (pctldev->desc->pmxops)
1792 			seq_puts(s, "yes ");
1793 		else
1794 			seq_puts(s, "no ");
1795 		if (pctldev->desc->confops)
1796 			seq_puts(s, "yes");
1797 		else
1798 			seq_puts(s, "no");
1799 		seq_puts(s, "\n");
1800 	}
1801 
1802 	mutex_unlock(&pinctrldev_list_mutex);
1803 
1804 	return 0;
1805 }
1806 DEFINE_SHOW_ATTRIBUTE(pinctrl_devices);
1807 
1808 static inline const char *map_type(enum pinctrl_map_type type)
1809 {
1810 	static const char * const names[] = {
1811 		"INVALID",
1812 		"DUMMY_STATE",
1813 		"MUX_GROUP",
1814 		"CONFIGS_PIN",
1815 		"CONFIGS_GROUP",
1816 	};
1817 
1818 	if (type >= ARRAY_SIZE(names))
1819 		return "UNKNOWN";
1820 
1821 	return names[type];
1822 }
1823 
1824 static int pinctrl_maps_show(struct seq_file *s, void *what)
1825 {
1826 	struct pinctrl_maps *maps_node;
1827 	const struct pinctrl_map *map;
1828 
1829 	seq_puts(s, "Pinctrl maps:\n");
1830 
1831 	mutex_lock(&pinctrl_maps_mutex);
1832 	for_each_pin_map(maps_node, map) {
1833 		seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1834 			   map->dev_name, map->name, map_type(map->type),
1835 			   map->type);
1836 
1837 		if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1838 			seq_printf(s, "controlling device %s\n",
1839 				   map->ctrl_dev_name);
1840 
1841 		switch (map->type) {
1842 		case PIN_MAP_TYPE_MUX_GROUP:
1843 			pinmux_show_map(s, map);
1844 			break;
1845 		case PIN_MAP_TYPE_CONFIGS_PIN:
1846 		case PIN_MAP_TYPE_CONFIGS_GROUP:
1847 			pinconf_show_map(s, map);
1848 			break;
1849 		default:
1850 			break;
1851 		}
1852 
1853 		seq_putc(s, '\n');
1854 	}
1855 	mutex_unlock(&pinctrl_maps_mutex);
1856 
1857 	return 0;
1858 }
1859 DEFINE_SHOW_ATTRIBUTE(pinctrl_maps);
1860 
1861 static int pinctrl_show(struct seq_file *s, void *what)
1862 {
1863 	struct pinctrl *p;
1864 	struct pinctrl_state *state;
1865 	struct pinctrl_setting *setting;
1866 
1867 	seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
1868 
1869 	mutex_lock(&pinctrl_list_mutex);
1870 
1871 	list_for_each_entry(p, &pinctrl_list, node) {
1872 		seq_printf(s, "device: %s current state: %s\n",
1873 			   dev_name(p->dev),
1874 			   p->state ? p->state->name : "none");
1875 
1876 		list_for_each_entry(state, &p->states, node) {
1877 			seq_printf(s, "  state: %s\n", state->name);
1878 
1879 			list_for_each_entry(setting, &state->settings, node) {
1880 				struct pinctrl_dev *pctldev = setting->pctldev;
1881 
1882 				seq_printf(s, "    type: %s controller %s ",
1883 					   map_type(setting->type),
1884 					   pinctrl_dev_get_name(pctldev));
1885 
1886 				switch (setting->type) {
1887 				case PIN_MAP_TYPE_MUX_GROUP:
1888 					pinmux_show_setting(s, setting);
1889 					break;
1890 				case PIN_MAP_TYPE_CONFIGS_PIN:
1891 				case PIN_MAP_TYPE_CONFIGS_GROUP:
1892 					pinconf_show_setting(s, setting);
1893 					break;
1894 				default:
1895 					break;
1896 				}
1897 			}
1898 		}
1899 	}
1900 
1901 	mutex_unlock(&pinctrl_list_mutex);
1902 
1903 	return 0;
1904 }
1905 DEFINE_SHOW_ATTRIBUTE(pinctrl);
1906 
1907 static struct dentry *debugfs_root;
1908 
1909 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1910 {
1911 	struct dentry *device_root;
1912 	const char *debugfs_name;
1913 
1914 	if (pctldev->desc->name &&
1915 			strcmp(dev_name(pctldev->dev), pctldev->desc->name)) {
1916 		debugfs_name = devm_kasprintf(pctldev->dev, GFP_KERNEL,
1917 				"%s-%s", dev_name(pctldev->dev),
1918 				pctldev->desc->name);
1919 		if (!debugfs_name) {
1920 			pr_warn("failed to determine debugfs dir name for %s\n",
1921 				dev_name(pctldev->dev));
1922 			return;
1923 		}
1924 	} else {
1925 		debugfs_name = dev_name(pctldev->dev);
1926 	}
1927 
1928 	device_root = debugfs_create_dir(debugfs_name, debugfs_root);
1929 	pctldev->device_root = device_root;
1930 
1931 	if (IS_ERR(device_root) || !device_root) {
1932 		pr_warn("failed to create debugfs directory for %s\n",
1933 			dev_name(pctldev->dev));
1934 		return;
1935 	}
1936 	debugfs_create_file("pins", 0444,
1937 			    device_root, pctldev, &pinctrl_pins_fops);
1938 	debugfs_create_file("pingroups", 0444,
1939 			    device_root, pctldev, &pinctrl_groups_fops);
1940 	debugfs_create_file("gpio-ranges", 0444,
1941 			    device_root, pctldev, &pinctrl_gpioranges_fops);
1942 	if (pctldev->desc->pmxops)
1943 		pinmux_init_device_debugfs(device_root, pctldev);
1944 	if (pctldev->desc->confops)
1945 		pinconf_init_device_debugfs(device_root, pctldev);
1946 }
1947 
1948 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1949 {
1950 	debugfs_remove_recursive(pctldev->device_root);
1951 }
1952 
1953 static void pinctrl_init_debugfs(void)
1954 {
1955 	debugfs_root = debugfs_create_dir("pinctrl", NULL);
1956 	if (IS_ERR(debugfs_root) || !debugfs_root) {
1957 		pr_warn("failed to create debugfs directory\n");
1958 		debugfs_root = NULL;
1959 		return;
1960 	}
1961 
1962 	debugfs_create_file("pinctrl-devices", 0444,
1963 			    debugfs_root, NULL, &pinctrl_devices_fops);
1964 	debugfs_create_file("pinctrl-maps", 0444,
1965 			    debugfs_root, NULL, &pinctrl_maps_fops);
1966 	debugfs_create_file("pinctrl-handles", 0444,
1967 			    debugfs_root, NULL, &pinctrl_fops);
1968 }
1969 
1970 #else /* CONFIG_DEBUG_FS */
1971 
1972 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1973 {
1974 }
1975 
1976 static void pinctrl_init_debugfs(void)
1977 {
1978 }
1979 
1980 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1981 {
1982 }
1983 
1984 #endif
1985 
1986 static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1987 {
1988 	const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1989 
1990 	if (!ops ||
1991 	    !ops->get_groups_count ||
1992 	    !ops->get_group_name)
1993 		return -EINVAL;
1994 
1995 	return 0;
1996 }
1997 
1998 /**
1999  * pinctrl_init_controller() - init a pin controller device
2000  * @pctldesc: descriptor for this pin controller
2001  * @dev: parent device for this pin controller
2002  * @driver_data: private pin controller data for this pin controller
2003  */
2004 static struct pinctrl_dev *
2005 pinctrl_init_controller(struct pinctrl_desc *pctldesc, struct device *dev,
2006 			void *driver_data)
2007 {
2008 	struct pinctrl_dev *pctldev;
2009 	int ret;
2010 
2011 	if (!pctldesc)
2012 		return ERR_PTR(-EINVAL);
2013 	if (!pctldesc->name)
2014 		return ERR_PTR(-EINVAL);
2015 
2016 	pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
2017 	if (!pctldev)
2018 		return ERR_PTR(-ENOMEM);
2019 
2020 	/* Initialize pin control device struct */
2021 	pctldev->owner = pctldesc->owner;
2022 	pctldev->desc = pctldesc;
2023 	pctldev->driver_data = driver_data;
2024 	INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
2025 #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
2026 	INIT_RADIX_TREE(&pctldev->pin_group_tree, GFP_KERNEL);
2027 #endif
2028 #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
2029 	INIT_RADIX_TREE(&pctldev->pin_function_tree, GFP_KERNEL);
2030 #endif
2031 	INIT_LIST_HEAD(&pctldev->gpio_ranges);
2032 	INIT_LIST_HEAD(&pctldev->node);
2033 	pctldev->dev = dev;
2034 	mutex_init(&pctldev->mutex);
2035 
2036 	/* check core ops for sanity */
2037 	ret = pinctrl_check_ops(pctldev);
2038 	if (ret) {
2039 		dev_err(dev, "pinctrl ops lacks necessary functions\n");
2040 		goto out_err;
2041 	}
2042 
2043 	/* If we're implementing pinmuxing, check the ops for sanity */
2044 	if (pctldesc->pmxops) {
2045 		ret = pinmux_check_ops(pctldev);
2046 		if (ret)
2047 			goto out_err;
2048 	}
2049 
2050 	/* If we're implementing pinconfig, check the ops for sanity */
2051 	if (pctldesc->confops) {
2052 		ret = pinconf_check_ops(pctldev);
2053 		if (ret)
2054 			goto out_err;
2055 	}
2056 
2057 	/* Register all the pins */
2058 	dev_dbg(dev, "try to register %d pins ...\n",  pctldesc->npins);
2059 	ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
2060 	if (ret) {
2061 		dev_err(dev, "error during pin registration\n");
2062 		pinctrl_free_pindescs(pctldev, pctldesc->pins,
2063 				      pctldesc->npins);
2064 		goto out_err;
2065 	}
2066 
2067 	return pctldev;
2068 
2069 out_err:
2070 	mutex_destroy(&pctldev->mutex);
2071 	kfree(pctldev);
2072 	return ERR_PTR(ret);
2073 }
2074 
2075 static int pinctrl_claim_hogs(struct pinctrl_dev *pctldev)
2076 {
2077 	pctldev->p = create_pinctrl(pctldev->dev, pctldev);
2078 	if (PTR_ERR(pctldev->p) == -ENODEV) {
2079 		dev_dbg(pctldev->dev, "no hogs found\n");
2080 
2081 		return 0;
2082 	}
2083 
2084 	if (IS_ERR(pctldev->p)) {
2085 		dev_err(pctldev->dev, "error claiming hogs: %li\n",
2086 			PTR_ERR(pctldev->p));
2087 
2088 		return PTR_ERR(pctldev->p);
2089 	}
2090 
2091 	pctldev->hog_default =
2092 		pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
2093 	if (IS_ERR(pctldev->hog_default)) {
2094 		dev_dbg(pctldev->dev,
2095 			"failed to lookup the default state\n");
2096 	} else {
2097 		if (pinctrl_select_state(pctldev->p,
2098 					 pctldev->hog_default))
2099 			dev_err(pctldev->dev,
2100 				"failed to select default state\n");
2101 	}
2102 
2103 	pctldev->hog_sleep =
2104 		pinctrl_lookup_state(pctldev->p,
2105 				     PINCTRL_STATE_SLEEP);
2106 	if (IS_ERR(pctldev->hog_sleep))
2107 		dev_dbg(pctldev->dev,
2108 			"failed to lookup the sleep state\n");
2109 
2110 	return 0;
2111 }
2112 
2113 int pinctrl_enable(struct pinctrl_dev *pctldev)
2114 {
2115 	int error;
2116 
2117 	error = pinctrl_claim_hogs(pctldev);
2118 	if (error) {
2119 		dev_err(pctldev->dev, "could not claim hogs: %i\n",
2120 			error);
2121 		pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
2122 				      pctldev->desc->npins);
2123 		mutex_destroy(&pctldev->mutex);
2124 		kfree(pctldev);
2125 
2126 		return error;
2127 	}
2128 
2129 	mutex_lock(&pinctrldev_list_mutex);
2130 	list_add_tail(&pctldev->node, &pinctrldev_list);
2131 	mutex_unlock(&pinctrldev_list_mutex);
2132 
2133 	pinctrl_init_device_debugfs(pctldev);
2134 
2135 	return 0;
2136 }
2137 EXPORT_SYMBOL_GPL(pinctrl_enable);
2138 
2139 /**
2140  * pinctrl_register() - register a pin controller device
2141  * @pctldesc: descriptor for this pin controller
2142  * @dev: parent device for this pin controller
2143  * @driver_data: private pin controller data for this pin controller
2144  *
2145  * Note that pinctrl_register() is known to have problems as the pin
2146  * controller driver functions are called before the driver has a
2147  * struct pinctrl_dev handle. To avoid issues later on, please use the
2148  * new pinctrl_register_and_init() below instead.
2149  */
2150 struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
2151 				    struct device *dev, void *driver_data)
2152 {
2153 	struct pinctrl_dev *pctldev;
2154 	int error;
2155 
2156 	pctldev = pinctrl_init_controller(pctldesc, dev, driver_data);
2157 	if (IS_ERR(pctldev))
2158 		return pctldev;
2159 
2160 	error = pinctrl_enable(pctldev);
2161 	if (error)
2162 		return ERR_PTR(error);
2163 
2164 	return pctldev;
2165 }
2166 EXPORT_SYMBOL_GPL(pinctrl_register);
2167 
2168 /**
2169  * pinctrl_register_and_init() - register and init pin controller device
2170  * @pctldesc: descriptor for this pin controller
2171  * @dev: parent device for this pin controller
2172  * @driver_data: private pin controller data for this pin controller
2173  * @pctldev: pin controller device
2174  *
2175  * Note that pinctrl_enable() still needs to be manually called after
2176  * this once the driver is ready.
2177  */
2178 int pinctrl_register_and_init(struct pinctrl_desc *pctldesc,
2179 			      struct device *dev, void *driver_data,
2180 			      struct pinctrl_dev **pctldev)
2181 {
2182 	struct pinctrl_dev *p;
2183 
2184 	p = pinctrl_init_controller(pctldesc, dev, driver_data);
2185 	if (IS_ERR(p))
2186 		return PTR_ERR(p);
2187 
2188 	/*
2189 	 * We have pinctrl_start() call functions in the pin controller
2190 	 * driver with create_pinctrl() for at least dt_node_to_map(). So
2191 	 * let's make sure pctldev is properly initialized for the
2192 	 * pin controller driver before we do anything.
2193 	 */
2194 	*pctldev = p;
2195 
2196 	return 0;
2197 }
2198 EXPORT_SYMBOL_GPL(pinctrl_register_and_init);
2199 
2200 /**
2201  * pinctrl_unregister() - unregister pinmux
2202  * @pctldev: pin controller to unregister
2203  *
2204  * Called by pinmux drivers to unregister a pinmux.
2205  */
2206 void pinctrl_unregister(struct pinctrl_dev *pctldev)
2207 {
2208 	struct pinctrl_gpio_range *range, *n;
2209 
2210 	if (!pctldev)
2211 		return;
2212 
2213 	mutex_lock(&pctldev->mutex);
2214 	pinctrl_remove_device_debugfs(pctldev);
2215 	mutex_unlock(&pctldev->mutex);
2216 
2217 	if (!IS_ERR_OR_NULL(pctldev->p))
2218 		pinctrl_put(pctldev->p);
2219 
2220 	mutex_lock(&pinctrldev_list_mutex);
2221 	mutex_lock(&pctldev->mutex);
2222 	/* TODO: check that no pinmuxes are still active? */
2223 	list_del(&pctldev->node);
2224 	pinmux_generic_free_functions(pctldev);
2225 	pinctrl_generic_free_groups(pctldev);
2226 	/* Destroy descriptor tree */
2227 	pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
2228 			      pctldev->desc->npins);
2229 	/* remove gpio ranges map */
2230 	list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
2231 		list_del(&range->node);
2232 
2233 	mutex_unlock(&pctldev->mutex);
2234 	mutex_destroy(&pctldev->mutex);
2235 	kfree(pctldev);
2236 	mutex_unlock(&pinctrldev_list_mutex);
2237 }
2238 EXPORT_SYMBOL_GPL(pinctrl_unregister);
2239 
2240 static void devm_pinctrl_dev_release(struct device *dev, void *res)
2241 {
2242 	struct pinctrl_dev *pctldev = *(struct pinctrl_dev **)res;
2243 
2244 	pinctrl_unregister(pctldev);
2245 }
2246 
2247 static int devm_pinctrl_dev_match(struct device *dev, void *res, void *data)
2248 {
2249 	struct pctldev **r = res;
2250 
2251 	if (WARN_ON(!r || !*r))
2252 		return 0;
2253 
2254 	return *r == data;
2255 }
2256 
2257 /**
2258  * devm_pinctrl_register() - Resource managed version of pinctrl_register().
2259  * @dev: parent device for this pin controller
2260  * @pctldesc: descriptor for this pin controller
2261  * @driver_data: private pin controller data for this pin controller
2262  *
2263  * Returns an error pointer if pincontrol register failed. Otherwise
2264  * it returns valid pinctrl handle.
2265  *
2266  * The pinctrl device will be automatically released when the device is unbound.
2267  */
2268 struct pinctrl_dev *devm_pinctrl_register(struct device *dev,
2269 					  struct pinctrl_desc *pctldesc,
2270 					  void *driver_data)
2271 {
2272 	struct pinctrl_dev **ptr, *pctldev;
2273 
2274 	ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
2275 	if (!ptr)
2276 		return ERR_PTR(-ENOMEM);
2277 
2278 	pctldev = pinctrl_register(pctldesc, dev, driver_data);
2279 	if (IS_ERR(pctldev)) {
2280 		devres_free(ptr);
2281 		return pctldev;
2282 	}
2283 
2284 	*ptr = pctldev;
2285 	devres_add(dev, ptr);
2286 
2287 	return pctldev;
2288 }
2289 EXPORT_SYMBOL_GPL(devm_pinctrl_register);
2290 
2291 /**
2292  * devm_pinctrl_register_and_init() - Resource managed pinctrl register and init
2293  * @dev: parent device for this pin controller
2294  * @pctldesc: descriptor for this pin controller
2295  * @driver_data: private pin controller data for this pin controller
2296  * @pctldev: pin controller device
2297  *
2298  * Returns zero on success or an error number on failure.
2299  *
2300  * The pinctrl device will be automatically released when the device is unbound.
2301  */
2302 int devm_pinctrl_register_and_init(struct device *dev,
2303 				   struct pinctrl_desc *pctldesc,
2304 				   void *driver_data,
2305 				   struct pinctrl_dev **pctldev)
2306 {
2307 	struct pinctrl_dev **ptr;
2308 	int error;
2309 
2310 	ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
2311 	if (!ptr)
2312 		return -ENOMEM;
2313 
2314 	error = pinctrl_register_and_init(pctldesc, dev, driver_data, pctldev);
2315 	if (error) {
2316 		devres_free(ptr);
2317 		return error;
2318 	}
2319 
2320 	*ptr = *pctldev;
2321 	devres_add(dev, ptr);
2322 
2323 	return 0;
2324 }
2325 EXPORT_SYMBOL_GPL(devm_pinctrl_register_and_init);
2326 
2327 /**
2328  * devm_pinctrl_unregister() - Resource managed version of pinctrl_unregister().
2329  * @dev: device for which resource was allocated
2330  * @pctldev: the pinctrl device to unregister.
2331  */
2332 void devm_pinctrl_unregister(struct device *dev, struct pinctrl_dev *pctldev)
2333 {
2334 	WARN_ON(devres_release(dev, devm_pinctrl_dev_release,
2335 			       devm_pinctrl_dev_match, pctldev));
2336 }
2337 EXPORT_SYMBOL_GPL(devm_pinctrl_unregister);
2338 
2339 static int __init pinctrl_init(void)
2340 {
2341 	pr_info("initialized pinctrl subsystem\n");
2342 	pinctrl_init_debugfs();
2343 	return 0;
2344 }
2345 
2346 /* init early since many drivers really need to initialized pinmux early */
2347 core_initcall(pinctrl_init);
2348