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