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