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