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