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