xref: /linux/drivers/pinctrl/core.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
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 			mutex_unlock(&pinctrl_maps_mutex);
1110 			pinctrl_free(p, false);
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 		gdev = NULL;
1674 		gpio_num = -1;
1675 		list_for_each_entry(range, &pctldev->gpio_ranges, node) {
1676 			if (range->pins != NULL) {
1677 				for (int i = 0; i < range->npins; ++i) {
1678 					if (range->pins[i] == pin) {
1679 						gpio_num = range->base + i;
1680 						break;
1681 					}
1682 				}
1683 			} else if ((pin >= range->pin_base) &&
1684 				   (pin < (range->pin_base + range->npins))) {
1685 				gpio_num =
1686 					range->base + (pin - range->pin_base);
1687 			}
1688 			if (gpio_num != -1)
1689 				break;
1690 		}
1691 		if (gpio_num >= 0)
1692 			/*
1693 			 * FIXME: gpio_num comes from the global GPIO numberspace.
1694 			 * we need to get rid of the range->base eventually and
1695 			 * get the descriptor directly from the gpio_chip.
1696 			 */
1697 			gdev = gpiod_to_gpio_device(gpio_to_desc(gpio_num));
1698 		if (gdev)
1699 			seq_printf(s, "%u:%s ",
1700 				   gpio_num - gpio_device_get_base(gdev),
1701 				   gpio_device_get_label(gdev));
1702 		else
1703 			seq_puts(s, "0:? ");
1704 #endif
1705 
1706 		/* Driver-specific info per pin */
1707 		if (ops->pin_dbg_show)
1708 			ops->pin_dbg_show(pctldev, s, pin);
1709 
1710 		seq_puts(s, "\n");
1711 	}
1712 
1713 	mutex_unlock(&pctldev->mutex);
1714 
1715 	return 0;
1716 }
1717 DEFINE_SHOW_ATTRIBUTE(pinctrl_pins);
1718 
pinctrl_groups_show(struct seq_file * s,void * what)1719 static int pinctrl_groups_show(struct seq_file *s, void *what)
1720 {
1721 	struct pinctrl_dev *pctldev = s->private;
1722 	const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1723 	unsigned int ngroups, selector = 0;
1724 
1725 	mutex_lock(&pctldev->mutex);
1726 
1727 	ngroups = ops->get_groups_count(pctldev);
1728 
1729 	seq_puts(s, "registered pin groups:\n");
1730 	while (selector < ngroups) {
1731 		const unsigned int *pins = NULL;
1732 		unsigned int num_pins = 0;
1733 		const char *gname = ops->get_group_name(pctldev, selector);
1734 		const char *pname;
1735 		int ret = 0;
1736 		int i;
1737 
1738 		if (ops->get_group_pins)
1739 			ret = ops->get_group_pins(pctldev, selector,
1740 						  &pins, &num_pins);
1741 		if (ret)
1742 			seq_printf(s, "%s [ERROR GETTING PINS]\n",
1743 				   gname);
1744 		else {
1745 			seq_printf(s, "group: %s\n", gname);
1746 			for (i = 0; i < num_pins; i++) {
1747 				pname = pin_get_name(pctldev, pins[i]);
1748 				if (WARN_ON(!pname)) {
1749 					mutex_unlock(&pctldev->mutex);
1750 					return -EINVAL;
1751 				}
1752 				seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1753 			}
1754 			seq_puts(s, "\n");
1755 		}
1756 		selector++;
1757 	}
1758 
1759 	mutex_unlock(&pctldev->mutex);
1760 
1761 	return 0;
1762 }
1763 DEFINE_SHOW_ATTRIBUTE(pinctrl_groups);
1764 
pinctrl_gpioranges_show(struct seq_file * s,void * what)1765 static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1766 {
1767 	struct pinctrl_dev *pctldev = s->private;
1768 	struct pinctrl_gpio_range *range;
1769 
1770 	seq_puts(s, "GPIO ranges handled:\n");
1771 
1772 	mutex_lock(&pctldev->mutex);
1773 
1774 	/* Loop over the ranges */
1775 	list_for_each_entry(range, &pctldev->gpio_ranges, node) {
1776 		if (range->pins) {
1777 			int a;
1778 			seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
1779 				range->id, range->name,
1780 				range->base, (range->base + range->npins - 1));
1781 			for (a = 0; a < range->npins - 1; a++)
1782 				seq_printf(s, "%u, ", range->pins[a]);
1783 			seq_printf(s, "%u}\n", range->pins[a]);
1784 		}
1785 		else
1786 			seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1787 				range->id, range->name,
1788 				range->base, (range->base + range->npins - 1),
1789 				range->pin_base,
1790 				(range->pin_base + range->npins - 1));
1791 	}
1792 
1793 	mutex_unlock(&pctldev->mutex);
1794 
1795 	return 0;
1796 }
1797 DEFINE_SHOW_ATTRIBUTE(pinctrl_gpioranges);
1798 
pinctrl_devices_show(struct seq_file * s,void * what)1799 static int pinctrl_devices_show(struct seq_file *s, void *what)
1800 {
1801 	struct pinctrl_dev *pctldev;
1802 
1803 	seq_puts(s, "name [pinmux] [pinconf]\n");
1804 
1805 	mutex_lock(&pinctrldev_list_mutex);
1806 
1807 	list_for_each_entry(pctldev, &pinctrldev_list, node) {
1808 		seq_printf(s, "%s ", pctldev->desc->name);
1809 		if (pctldev->desc->pmxops)
1810 			seq_puts(s, "yes ");
1811 		else
1812 			seq_puts(s, "no ");
1813 		if (pctldev->desc->confops)
1814 			seq_puts(s, "yes");
1815 		else
1816 			seq_puts(s, "no");
1817 		seq_puts(s, "\n");
1818 	}
1819 
1820 	mutex_unlock(&pinctrldev_list_mutex);
1821 
1822 	return 0;
1823 }
1824 DEFINE_SHOW_ATTRIBUTE(pinctrl_devices);
1825 
map_type(enum pinctrl_map_type type)1826 static inline const char *map_type(enum pinctrl_map_type type)
1827 {
1828 	static const char * const names[] = {
1829 		"INVALID",
1830 		"DUMMY_STATE",
1831 		"MUX_GROUP",
1832 		"CONFIGS_PIN",
1833 		"CONFIGS_GROUP",
1834 	};
1835 
1836 	if (type >= ARRAY_SIZE(names))
1837 		return "UNKNOWN";
1838 
1839 	return names[type];
1840 }
1841 
pinctrl_maps_show(struct seq_file * s,void * what)1842 static int pinctrl_maps_show(struct seq_file *s, void *what)
1843 {
1844 	struct pinctrl_maps *maps_node;
1845 	const struct pinctrl_map *map;
1846 
1847 	seq_puts(s, "Pinctrl maps:\n");
1848 
1849 	mutex_lock(&pinctrl_maps_mutex);
1850 	for_each_pin_map(maps_node, map) {
1851 		seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1852 			   map->dev_name, map->name, map_type(map->type),
1853 			   map->type);
1854 
1855 		if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1856 			seq_printf(s, "controlling device %s\n",
1857 				   map->ctrl_dev_name);
1858 
1859 		switch (map->type) {
1860 		case PIN_MAP_TYPE_MUX_GROUP:
1861 			pinmux_show_map(s, map);
1862 			break;
1863 		case PIN_MAP_TYPE_CONFIGS_PIN:
1864 		case PIN_MAP_TYPE_CONFIGS_GROUP:
1865 			pinconf_show_map(s, map);
1866 			break;
1867 		default:
1868 			break;
1869 		}
1870 
1871 		seq_putc(s, '\n');
1872 	}
1873 	mutex_unlock(&pinctrl_maps_mutex);
1874 
1875 	return 0;
1876 }
1877 DEFINE_SHOW_ATTRIBUTE(pinctrl_maps);
1878 
pinctrl_show(struct seq_file * s,void * what)1879 static int pinctrl_show(struct seq_file *s, void *what)
1880 {
1881 	struct pinctrl *p;
1882 	struct pinctrl_state *state;
1883 	struct pinctrl_setting *setting;
1884 
1885 	seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
1886 
1887 	mutex_lock(&pinctrl_list_mutex);
1888 
1889 	list_for_each_entry(p, &pinctrl_list, node) {
1890 		seq_printf(s, "device: %s current state: %s\n",
1891 			   dev_name(p->dev),
1892 			   p->state ? p->state->name : "none");
1893 
1894 		list_for_each_entry(state, &p->states, node) {
1895 			seq_printf(s, "  state: %s\n", state->name);
1896 
1897 			list_for_each_entry(setting, &state->settings, node) {
1898 				struct pinctrl_dev *pctldev = setting->pctldev;
1899 
1900 				seq_printf(s, "    type: %s controller %s ",
1901 					   map_type(setting->type),
1902 					   pinctrl_dev_get_name(pctldev));
1903 
1904 				switch (setting->type) {
1905 				case PIN_MAP_TYPE_MUX_GROUP:
1906 					pinmux_show_setting(s, setting);
1907 					break;
1908 				case PIN_MAP_TYPE_CONFIGS_PIN:
1909 				case PIN_MAP_TYPE_CONFIGS_GROUP:
1910 					pinconf_show_setting(s, setting);
1911 					break;
1912 				default:
1913 					break;
1914 				}
1915 			}
1916 		}
1917 	}
1918 
1919 	mutex_unlock(&pinctrl_list_mutex);
1920 
1921 	return 0;
1922 }
1923 DEFINE_SHOW_ATTRIBUTE(pinctrl);
1924 
1925 static struct dentry *debugfs_root;
1926 
pinctrl_init_device_debugfs(struct pinctrl_dev * pctldev)1927 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1928 {
1929 	struct dentry *device_root;
1930 	const char *debugfs_name;
1931 
1932 	if (pctldev->desc->name &&
1933 			strcmp(dev_name(pctldev->dev), pctldev->desc->name)) {
1934 		debugfs_name = devm_kasprintf(pctldev->dev, GFP_KERNEL,
1935 				"%s-%s", dev_name(pctldev->dev),
1936 				pctldev->desc->name);
1937 		if (!debugfs_name) {
1938 			pr_warn("failed to determine debugfs dir name for %s\n",
1939 				dev_name(pctldev->dev));
1940 			return;
1941 		}
1942 	} else {
1943 		debugfs_name = dev_name(pctldev->dev);
1944 	}
1945 
1946 	device_root = debugfs_create_dir(debugfs_name, debugfs_root);
1947 	pctldev->device_root = device_root;
1948 
1949 	if (IS_ERR(device_root) || !device_root) {
1950 		pr_warn("failed to create debugfs directory for %s\n",
1951 			dev_name(pctldev->dev));
1952 		return;
1953 	}
1954 	debugfs_create_file("pins", 0444,
1955 			    device_root, pctldev, &pinctrl_pins_fops);
1956 	debugfs_create_file("pingroups", 0444,
1957 			    device_root, pctldev, &pinctrl_groups_fops);
1958 	debugfs_create_file("gpio-ranges", 0444,
1959 			    device_root, pctldev, &pinctrl_gpioranges_fops);
1960 	if (pctldev->desc->pmxops)
1961 		pinmux_init_device_debugfs(device_root, pctldev);
1962 	if (pctldev->desc->confops)
1963 		pinconf_init_device_debugfs(device_root, pctldev);
1964 }
1965 
pinctrl_remove_device_debugfs(struct pinctrl_dev * pctldev)1966 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1967 {
1968 	debugfs_remove_recursive(pctldev->device_root);
1969 }
1970 
pinctrl_init_debugfs(void)1971 static void pinctrl_init_debugfs(void)
1972 {
1973 	debugfs_root = debugfs_create_dir("pinctrl", NULL);
1974 	if (IS_ERR(debugfs_root)) {
1975 		pr_warn("failed to create debugfs directory\n");
1976 		debugfs_root = NULL;
1977 		return;
1978 	}
1979 
1980 	debugfs_create_file("pinctrl-devices", 0444,
1981 			    debugfs_root, NULL, &pinctrl_devices_fops);
1982 	debugfs_create_file("pinctrl-maps", 0444,
1983 			    debugfs_root, NULL, &pinctrl_maps_fops);
1984 	debugfs_create_file("pinctrl-handles", 0444,
1985 			    debugfs_root, NULL, &pinctrl_fops);
1986 }
1987 
1988 #else /* CONFIG_DEBUG_FS */
1989 
pinctrl_init_device_debugfs(struct pinctrl_dev * pctldev)1990 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1991 {
1992 }
1993 
pinctrl_init_debugfs(void)1994 static void pinctrl_init_debugfs(void)
1995 {
1996 }
1997 
pinctrl_remove_device_debugfs(struct pinctrl_dev * pctldev)1998 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1999 {
2000 }
2001 
2002 #endif
2003 
pinctrl_check_ops(struct pinctrl_dev * pctldev)2004 static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
2005 {
2006 	const struct pinctrl_ops *ops = pctldev->desc->pctlops;
2007 
2008 	if (!ops ||
2009 	    !ops->get_groups_count ||
2010 	    !ops->get_group_name)
2011 		return -EINVAL;
2012 
2013 	return 0;
2014 }
2015 
2016 /**
2017  * pinctrl_init_controller() - init a pin controller device
2018  * @pctldesc: descriptor for this pin controller
2019  * @dev: parent device for this pin controller
2020  * @driver_data: private pin controller data for this pin controller
2021  */
2022 static struct pinctrl_dev *
pinctrl_init_controller(struct pinctrl_desc * pctldesc,struct device * dev,void * driver_data)2023 pinctrl_init_controller(struct pinctrl_desc *pctldesc, struct device *dev,
2024 			void *driver_data)
2025 {
2026 	struct pinctrl_dev *pctldev;
2027 	int ret;
2028 
2029 	if (!pctldesc)
2030 		return ERR_PTR(-EINVAL);
2031 	if (!pctldesc->name)
2032 		return ERR_PTR(-EINVAL);
2033 
2034 	pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
2035 	if (!pctldev)
2036 		return ERR_PTR(-ENOMEM);
2037 
2038 	/* Initialize pin control device struct */
2039 	pctldev->owner = pctldesc->owner;
2040 	pctldev->desc = pctldesc;
2041 	pctldev->driver_data = driver_data;
2042 	INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
2043 #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
2044 	INIT_RADIX_TREE(&pctldev->pin_group_tree, GFP_KERNEL);
2045 #endif
2046 #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
2047 	INIT_RADIX_TREE(&pctldev->pin_function_tree, GFP_KERNEL);
2048 #endif
2049 	INIT_LIST_HEAD(&pctldev->gpio_ranges);
2050 	INIT_LIST_HEAD(&pctldev->node);
2051 	pctldev->dev = dev;
2052 	mutex_init(&pctldev->mutex);
2053 
2054 	/* check core ops for sanity */
2055 	ret = pinctrl_check_ops(pctldev);
2056 	if (ret) {
2057 		dev_err(dev, "pinctrl ops lacks necessary functions\n");
2058 		goto out_err;
2059 	}
2060 
2061 	/* If we're implementing pinmuxing, check the ops for sanity */
2062 	if (pctldesc->pmxops) {
2063 		ret = pinmux_check_ops(pctldev);
2064 		if (ret)
2065 			goto out_err;
2066 	}
2067 
2068 	/* If we're implementing pinconfig, check the ops for sanity */
2069 	if (pctldesc->confops) {
2070 		ret = pinconf_check_ops(pctldev);
2071 		if (ret)
2072 			goto out_err;
2073 	}
2074 
2075 	/* Register all the pins */
2076 	dev_dbg(dev, "try to register %d pins ...\n",  pctldesc->npins);
2077 	ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
2078 	if (ret) {
2079 		dev_err(dev, "error during pin registration\n");
2080 		pinctrl_free_pindescs(pctldev, pctldesc->pins,
2081 				      pctldesc->npins);
2082 		goto out_err;
2083 	}
2084 
2085 	return pctldev;
2086 
2087 out_err:
2088 	mutex_destroy(&pctldev->mutex);
2089 	kfree(pctldev);
2090 	return ERR_PTR(ret);
2091 }
2092 
pinctrl_uninit_controller(struct pinctrl_dev * pctldev,struct pinctrl_desc * pctldesc)2093 static void pinctrl_uninit_controller(struct pinctrl_dev *pctldev, struct pinctrl_desc *pctldesc)
2094 {
2095 	pinctrl_free_pindescs(pctldev, pctldesc->pins,
2096 			      pctldesc->npins);
2097 	mutex_destroy(&pctldev->mutex);
2098 	kfree(pctldev);
2099 }
2100 
pinctrl_claim_hogs(struct pinctrl_dev * pctldev)2101 static int pinctrl_claim_hogs(struct pinctrl_dev *pctldev)
2102 {
2103 	pctldev->p = create_pinctrl(pctldev->dev, pctldev);
2104 	if (PTR_ERR(pctldev->p) == -ENODEV) {
2105 		dev_dbg(pctldev->dev, "no hogs found\n");
2106 
2107 		return 0;
2108 	}
2109 
2110 	if (IS_ERR(pctldev->p)) {
2111 		dev_err(pctldev->dev, "error claiming hogs: %li\n",
2112 			PTR_ERR(pctldev->p));
2113 
2114 		return PTR_ERR(pctldev->p);
2115 	}
2116 
2117 	pctldev->hog_default =
2118 		pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
2119 	if (IS_ERR(pctldev->hog_default)) {
2120 		dev_dbg(pctldev->dev,
2121 			"failed to lookup the default state\n");
2122 	} else {
2123 		if (pinctrl_select_state(pctldev->p,
2124 					 pctldev->hog_default))
2125 			dev_err(pctldev->dev,
2126 				"failed to select default state\n");
2127 	}
2128 
2129 	pctldev->hog_sleep =
2130 		pinctrl_lookup_state(pctldev->p,
2131 				     PINCTRL_STATE_SLEEP);
2132 	if (IS_ERR(pctldev->hog_sleep))
2133 		dev_dbg(pctldev->dev,
2134 			"failed to lookup the sleep state\n");
2135 
2136 	return 0;
2137 }
2138 
pinctrl_enable(struct pinctrl_dev * pctldev)2139 int pinctrl_enable(struct pinctrl_dev *pctldev)
2140 {
2141 	int error;
2142 
2143 	error = pinctrl_claim_hogs(pctldev);
2144 	if (error) {
2145 		dev_err(pctldev->dev, "could not claim hogs: %i\n", error);
2146 		return error;
2147 	}
2148 
2149 	mutex_lock(&pinctrldev_list_mutex);
2150 	list_add_tail(&pctldev->node, &pinctrldev_list);
2151 	mutex_unlock(&pinctrldev_list_mutex);
2152 
2153 	pinctrl_init_device_debugfs(pctldev);
2154 
2155 	return 0;
2156 }
2157 EXPORT_SYMBOL_GPL(pinctrl_enable);
2158 
2159 /**
2160  * pinctrl_register() - register a pin controller device
2161  * @pctldesc: descriptor for this pin controller
2162  * @dev: parent device for this pin controller
2163  * @driver_data: private pin controller data for this pin controller
2164  *
2165  * Note that pinctrl_register() is known to have problems as the pin
2166  * controller driver functions are called before the driver has a
2167  * struct pinctrl_dev handle. To avoid issues later on, please use the
2168  * new pinctrl_register_and_init() below instead.
2169  */
pinctrl_register(struct pinctrl_desc * pctldesc,struct device * dev,void * driver_data)2170 struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
2171 				    struct device *dev, void *driver_data)
2172 {
2173 	struct pinctrl_dev *pctldev;
2174 	int error;
2175 
2176 	pctldev = pinctrl_init_controller(pctldesc, dev, driver_data);
2177 	if (IS_ERR(pctldev))
2178 		return pctldev;
2179 
2180 	error = pinctrl_enable(pctldev);
2181 	if (error) {
2182 		pinctrl_uninit_controller(pctldev, pctldesc);
2183 		return ERR_PTR(error);
2184 	}
2185 
2186 	return pctldev;
2187 }
2188 EXPORT_SYMBOL_GPL(pinctrl_register);
2189 
2190 /**
2191  * pinctrl_register_and_init() - register and init pin controller device
2192  * @pctldesc: descriptor for this pin controller
2193  * @dev: parent device for this pin controller
2194  * @driver_data: private pin controller data for this pin controller
2195  * @pctldev: pin controller device
2196  *
2197  * Note that pinctrl_enable() still needs to be manually called after
2198  * this once the driver is ready.
2199  */
pinctrl_register_and_init(struct pinctrl_desc * pctldesc,struct device * dev,void * driver_data,struct pinctrl_dev ** pctldev)2200 int pinctrl_register_and_init(struct pinctrl_desc *pctldesc,
2201 			      struct device *dev, void *driver_data,
2202 			      struct pinctrl_dev **pctldev)
2203 {
2204 	struct pinctrl_dev *p;
2205 
2206 	p = pinctrl_init_controller(pctldesc, dev, driver_data);
2207 	if (IS_ERR(p))
2208 		return PTR_ERR(p);
2209 
2210 	/*
2211 	 * We have pinctrl_start() call functions in the pin controller
2212 	 * driver with create_pinctrl() for at least dt_node_to_map(). So
2213 	 * let's make sure pctldev is properly initialized for the
2214 	 * pin controller driver before we do anything.
2215 	 */
2216 	*pctldev = p;
2217 
2218 	return 0;
2219 }
2220 EXPORT_SYMBOL_GPL(pinctrl_register_and_init);
2221 
2222 /**
2223  * pinctrl_unregister() - unregister pinmux
2224  * @pctldev: pin controller to unregister
2225  *
2226  * Called by pinmux drivers to unregister a pinmux.
2227  */
pinctrl_unregister(struct pinctrl_dev * pctldev)2228 void pinctrl_unregister(struct pinctrl_dev *pctldev)
2229 {
2230 	struct pinctrl_gpio_range *range, *n;
2231 
2232 	if (!pctldev)
2233 		return;
2234 
2235 	mutex_lock(&pctldev->mutex);
2236 	pinctrl_remove_device_debugfs(pctldev);
2237 	mutex_unlock(&pctldev->mutex);
2238 
2239 	if (!IS_ERR_OR_NULL(pctldev->p))
2240 		pinctrl_put(pctldev->p);
2241 
2242 	mutex_lock(&pinctrldev_list_mutex);
2243 	mutex_lock(&pctldev->mutex);
2244 	/* TODO: check that no pinmuxes are still active? */
2245 	list_del(&pctldev->node);
2246 	pinmux_generic_free_functions(pctldev);
2247 	pinctrl_generic_free_groups(pctldev);
2248 	/* Destroy descriptor tree */
2249 	pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
2250 			      pctldev->desc->npins);
2251 	/* remove gpio ranges map */
2252 	list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
2253 		list_del(&range->node);
2254 
2255 	mutex_unlock(&pctldev->mutex);
2256 	mutex_destroy(&pctldev->mutex);
2257 	kfree(pctldev);
2258 	mutex_unlock(&pinctrldev_list_mutex);
2259 }
2260 EXPORT_SYMBOL_GPL(pinctrl_unregister);
2261 
devm_pinctrl_dev_release(struct device * dev,void * res)2262 static void devm_pinctrl_dev_release(struct device *dev, void *res)
2263 {
2264 	struct pinctrl_dev *pctldev = *(struct pinctrl_dev **)res;
2265 
2266 	pinctrl_unregister(pctldev);
2267 }
2268 
devm_pinctrl_dev_match(struct device * dev,void * res,void * data)2269 static int devm_pinctrl_dev_match(struct device *dev, void *res, void *data)
2270 {
2271 	struct pctldev **r = res;
2272 
2273 	if (WARN_ON(!r || !*r))
2274 		return 0;
2275 
2276 	return *r == data;
2277 }
2278 
2279 /**
2280  * devm_pinctrl_register() - Resource managed version of pinctrl_register().
2281  * @dev: parent device for this pin controller
2282  * @pctldesc: descriptor for this pin controller
2283  * @driver_data: private pin controller data for this pin controller
2284  *
2285  * Returns an error pointer if pincontrol register failed. Otherwise
2286  * it returns valid pinctrl handle.
2287  *
2288  * The pinctrl device will be automatically released when the device is unbound.
2289  */
devm_pinctrl_register(struct device * dev,struct pinctrl_desc * pctldesc,void * driver_data)2290 struct pinctrl_dev *devm_pinctrl_register(struct device *dev,
2291 					  struct pinctrl_desc *pctldesc,
2292 					  void *driver_data)
2293 {
2294 	struct pinctrl_dev **ptr, *pctldev;
2295 
2296 	ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
2297 	if (!ptr)
2298 		return ERR_PTR(-ENOMEM);
2299 
2300 	pctldev = pinctrl_register(pctldesc, dev, driver_data);
2301 	if (IS_ERR(pctldev)) {
2302 		devres_free(ptr);
2303 		return pctldev;
2304 	}
2305 
2306 	*ptr = pctldev;
2307 	devres_add(dev, ptr);
2308 
2309 	return pctldev;
2310 }
2311 EXPORT_SYMBOL_GPL(devm_pinctrl_register);
2312 
2313 /**
2314  * devm_pinctrl_register_and_init() - Resource managed pinctrl register and init
2315  * @dev: parent device for this pin controller
2316  * @pctldesc: descriptor for this pin controller
2317  * @driver_data: private pin controller data for this pin controller
2318  * @pctldev: pin controller device
2319  *
2320  * Returns zero on success or an error number on failure.
2321  *
2322  * The pinctrl device will be automatically released when the device is unbound.
2323  */
devm_pinctrl_register_and_init(struct device * dev,struct pinctrl_desc * pctldesc,void * driver_data,struct pinctrl_dev ** pctldev)2324 int devm_pinctrl_register_and_init(struct device *dev,
2325 				   struct pinctrl_desc *pctldesc,
2326 				   void *driver_data,
2327 				   struct pinctrl_dev **pctldev)
2328 {
2329 	struct pinctrl_dev **ptr;
2330 	int error;
2331 
2332 	ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
2333 	if (!ptr)
2334 		return -ENOMEM;
2335 
2336 	error = pinctrl_register_and_init(pctldesc, dev, driver_data, pctldev);
2337 	if (error) {
2338 		devres_free(ptr);
2339 		return error;
2340 	}
2341 
2342 	*ptr = *pctldev;
2343 	devres_add(dev, ptr);
2344 
2345 	return 0;
2346 }
2347 EXPORT_SYMBOL_GPL(devm_pinctrl_register_and_init);
2348 
2349 /**
2350  * devm_pinctrl_unregister() - Resource managed version of pinctrl_unregister().
2351  * @dev: device for which resource was allocated
2352  * @pctldev: the pinctrl device to unregister.
2353  */
devm_pinctrl_unregister(struct device * dev,struct pinctrl_dev * pctldev)2354 void devm_pinctrl_unregister(struct device *dev, struct pinctrl_dev *pctldev)
2355 {
2356 	WARN_ON(devres_release(dev, devm_pinctrl_dev_release,
2357 			       devm_pinctrl_dev_match, pctldev));
2358 }
2359 EXPORT_SYMBOL_GPL(devm_pinctrl_unregister);
2360 
pinctrl_init(void)2361 static int __init pinctrl_init(void)
2362 {
2363 	pr_info("initialized pinctrl subsystem\n");
2364 	pinctrl_init_debugfs();
2365 	return 0;
2366 }
2367 
2368 /* init early since many drivers really need to initialized pinmux early */
2369 core_initcall(pinctrl_init);
2370