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