• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Core driver for the pin control subsystem
3  *
4  * Copyright (C) 2011-2012 ST-Ericsson SA
5  * Written on behalf of Linaro for ST-Ericsson
6  * Based on bits of regulator core, gpio core and clk core
7  *
8  * Author: Linus Walleij <linus.walleij@linaro.org>
9  *
10  * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
11  *
12  * License terms: GNU General Public License (GPL) version 2
13  */
14 #define pr_fmt(fmt) "pinctrl core: " fmt
15 
16 #include <linux/kernel.h>
17 #include <linux/kref.h>
18 #include <linux/export.h>
19 #include <linux/init.h>
20 #include <linux/device.h>
21 #include <linux/slab.h>
22 #include <linux/err.h>
23 #include <linux/list.h>
24 #include <linux/sysfs.h>
25 #include <linux/debugfs.h>
26 #include <linux/seq_file.h>
27 #include <linux/pinctrl/consumer.h>
28 #include <linux/pinctrl/pinctrl.h>
29 #include <linux/pinctrl/machine.h>
30 
31 #ifdef CONFIG_GPIOLIB
32 #include <asm-generic/gpio.h>
33 #endif
34 
35 #include "core.h"
36 #include "devicetree.h"
37 #include "pinmux.h"
38 #include "pinconf.h"
39 
40 
41 static bool pinctrl_dummy_state;
42 
43 /* Mutex taken to protect pinctrl_list */
44 static DEFINE_MUTEX(pinctrl_list_mutex);
45 
46 /* Mutex taken to protect pinctrl_maps */
47 DEFINE_MUTEX(pinctrl_maps_mutex);
48 
49 /* Mutex taken to protect pinctrldev_list */
50 static DEFINE_MUTEX(pinctrldev_list_mutex);
51 
52 /* Global list of pin control devices (struct pinctrl_dev) */
53 static LIST_HEAD(pinctrldev_list);
54 
55 /* List of pin controller handles (struct pinctrl) */
56 static LIST_HEAD(pinctrl_list);
57 
58 /* List of pinctrl maps (struct pinctrl_maps) */
59 LIST_HEAD(pinctrl_maps);
60 
61 
62 /**
63  * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
64  *
65  * Usually this function is called by platforms without pinctrl driver support
66  * but run with some shared drivers using pinctrl APIs.
67  * After calling this function, the pinctrl core will return successfully
68  * with creating a dummy state for the driver to keep going smoothly.
69  */
pinctrl_provide_dummies(void)70 void pinctrl_provide_dummies(void)
71 {
72 	pinctrl_dummy_state = true;
73 }
74 
pinctrl_dev_get_name(struct pinctrl_dev * pctldev)75 const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
76 {
77 	/* We're not allowed to register devices without name */
78 	return pctldev->desc->name;
79 }
80 EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
81 
pinctrl_dev_get_devname(struct pinctrl_dev * pctldev)82 const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
83 {
84 	return dev_name(pctldev->dev);
85 }
86 EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
87 
pinctrl_dev_get_drvdata(struct pinctrl_dev * pctldev)88 void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
89 {
90 	return pctldev->driver_data;
91 }
92 EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
93 
94 /**
95  * get_pinctrl_dev_from_devname() - look up pin controller device
96  * @devname: the name of a device instance, as returned by dev_name()
97  *
98  * Looks up a pin control device matching a certain device name or pure device
99  * pointer, the pure device pointer will take precedence.
100  */
get_pinctrl_dev_from_devname(const char * devname)101 struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
102 {
103 	struct pinctrl_dev *pctldev = NULL;
104 
105 	if (!devname)
106 		return NULL;
107 
108 	mutex_lock(&pinctrldev_list_mutex);
109 
110 	list_for_each_entry(pctldev, &pinctrldev_list, node) {
111 		if (!strcmp(dev_name(pctldev->dev), devname)) {
112 			/* Matched on device name */
113 			mutex_unlock(&pinctrldev_list_mutex);
114 			return pctldev;
115 		}
116 	}
117 
118 	mutex_unlock(&pinctrldev_list_mutex);
119 
120 	return NULL;
121 }
122 
get_pinctrl_dev_from_of_node(struct device_node * np)123 struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np)
124 {
125 	struct pinctrl_dev *pctldev;
126 
127 	mutex_lock(&pinctrldev_list_mutex);
128 
129 	list_for_each_entry(pctldev, &pinctrldev_list, node)
130 		if (pctldev->dev->of_node == np) {
131 			mutex_unlock(&pinctrldev_list_mutex);
132 			return pctldev;
133 		}
134 
135 	mutex_unlock(&pinctrldev_list_mutex);
136 
137 	return NULL;
138 }
139 
140 /**
141  * pin_get_from_name() - look up a pin number from a name
142  * @pctldev: the pin control device to lookup the pin on
143  * @name: the name of the pin to look up
144  */
pin_get_from_name(struct pinctrl_dev * pctldev,const char * name)145 int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
146 {
147 	unsigned i, pin;
148 
149 	/* The pin number can be retrived from the pin controller descriptor */
150 	for (i = 0; i < pctldev->desc->npins; i++) {
151 		struct pin_desc *desc;
152 
153 		pin = pctldev->desc->pins[i].number;
154 		desc = pin_desc_get(pctldev, pin);
155 		/* Pin space may be sparse */
156 		if (desc && !strcmp(name, desc->name))
157 			return pin;
158 	}
159 
160 	return -EINVAL;
161 }
162 
163 /**
164  * pin_get_name_from_id() - look up a pin name from a pin id
165  * @pctldev: the pin control device to lookup the pin on
166  * @name: the name of the pin to look up
167  */
pin_get_name(struct pinctrl_dev * pctldev,const unsigned pin)168 const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
169 {
170 	const struct pin_desc *desc;
171 
172 	desc = pin_desc_get(pctldev, pin);
173 	if (desc == NULL) {
174 		dev_err(pctldev->dev, "failed to get pin(%d) name\n",
175 			pin);
176 		return NULL;
177 	}
178 
179 	return desc->name;
180 }
181 
182 /**
183  * pin_is_valid() - check if pin exists on controller
184  * @pctldev: the pin control device to check the pin on
185  * @pin: pin to check, use the local pin controller index number
186  *
187  * This tells us whether a certain pin exist on a certain pin controller or
188  * not. Pin lists may be sparse, so some pins may not exist.
189  */
pin_is_valid(struct pinctrl_dev * pctldev,int pin)190 bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
191 {
192 	struct pin_desc *pindesc;
193 
194 	if (pin < 0)
195 		return false;
196 
197 	mutex_lock(&pctldev->mutex);
198 	pindesc = pin_desc_get(pctldev, pin);
199 	mutex_unlock(&pctldev->mutex);
200 
201 	return pindesc != NULL;
202 }
203 EXPORT_SYMBOL_GPL(pin_is_valid);
204 
205 /* Deletes a range of pin descriptors */
pinctrl_free_pindescs(struct pinctrl_dev * pctldev,const struct pinctrl_pin_desc * pins,unsigned num_pins)206 static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
207 				  const struct pinctrl_pin_desc *pins,
208 				  unsigned num_pins)
209 {
210 	int i;
211 
212 	for (i = 0; i < num_pins; i++) {
213 		struct pin_desc *pindesc;
214 
215 		pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
216 					    pins[i].number);
217 		if (pindesc != NULL) {
218 			radix_tree_delete(&pctldev->pin_desc_tree,
219 					  pins[i].number);
220 			if (pindesc->dynamic_name)
221 				kfree(pindesc->name);
222 		}
223 		kfree(pindesc);
224 	}
225 }
226 
pinctrl_register_one_pin(struct pinctrl_dev * pctldev,unsigned number,const char * name)227 static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
228 				    unsigned number, const char *name)
229 {
230 	struct pin_desc *pindesc;
231 
232 	pindesc = pin_desc_get(pctldev, number);
233 	if (pindesc != NULL) {
234 		dev_err(pctldev->dev, "pin %d already registered\n", number);
235 		return -EINVAL;
236 	}
237 
238 	pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
239 	if (pindesc == NULL) {
240 		dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
241 		return -ENOMEM;
242 	}
243 
244 	/* Set owner */
245 	pindesc->pctldev = pctldev;
246 
247 	/* Copy basic pin info */
248 	if (name) {
249 		pindesc->name = name;
250 	} else {
251 		pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
252 		if (pindesc->name == NULL) {
253 			kfree(pindesc);
254 			return -ENOMEM;
255 		}
256 		pindesc->dynamic_name = true;
257 	}
258 
259 	radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
260 	pr_debug("registered pin %d (%s) on %s\n",
261 		 number, pindesc->name, pctldev->desc->name);
262 	return 0;
263 }
264 
pinctrl_register_pins(struct pinctrl_dev * pctldev,struct pinctrl_pin_desc const * pins,unsigned num_descs)265 static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
266 				 struct pinctrl_pin_desc const *pins,
267 				 unsigned num_descs)
268 {
269 	unsigned i;
270 	int ret = 0;
271 
272 	for (i = 0; i < num_descs; i++) {
273 		ret = pinctrl_register_one_pin(pctldev,
274 					       pins[i].number, pins[i].name);
275 		if (ret)
276 			return ret;
277 	}
278 
279 	return 0;
280 }
281 
282 /**
283  * gpio_to_pin() - GPIO range GPIO number to pin number translation
284  * @range: GPIO range used for the translation
285  * @gpio: gpio pin to translate to a pin number
286  *
287  * Finds the pin number for a given GPIO using the specified GPIO range
288  * as a base for translation. The distinction between linear GPIO ranges
289  * and pin list based GPIO ranges is managed correctly by this function.
290  *
291  * This function assumes the gpio is part of the specified GPIO range, use
292  * only after making sure this is the case (e.g. by calling it on the
293  * result of successful pinctrl_get_device_gpio_range calls)!
294  */
gpio_to_pin(struct pinctrl_gpio_range * range,unsigned int gpio)295 static inline int gpio_to_pin(struct pinctrl_gpio_range *range,
296 				unsigned int gpio)
297 {
298 	unsigned int offset = gpio - range->base;
299 	if (range->pins)
300 		return range->pins[offset];
301 	else
302 		return range->pin_base + offset;
303 }
304 
305 /**
306  * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
307  * @pctldev: pin controller device to check
308  * @gpio: gpio pin to check taken from the global GPIO pin space
309  *
310  * Tries to match a GPIO pin number to the ranges handled by a certain pin
311  * controller, return the range or NULL
312  */
313 static struct pinctrl_gpio_range *
pinctrl_match_gpio_range(struct pinctrl_dev * pctldev,unsigned gpio)314 pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
315 {
316 	struct pinctrl_gpio_range *range = NULL;
317 
318 	mutex_lock(&pctldev->mutex);
319 	/* Loop over the ranges */
320 	list_for_each_entry(range, &pctldev->gpio_ranges, node) {
321 		/* Check if we're in the valid range */
322 		if (gpio >= range->base &&
323 		    gpio < range->base + range->npins) {
324 			mutex_unlock(&pctldev->mutex);
325 			return range;
326 		}
327 	}
328 	mutex_unlock(&pctldev->mutex);
329 	return NULL;
330 }
331 
332 /**
333  * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
334  * the same GPIO chip are in range
335  * @gpio: gpio pin to check taken from the global GPIO pin space
336  *
337  * This function is complement of pinctrl_match_gpio_range(). If the return
338  * value of pinctrl_match_gpio_range() is NULL, this function could be used
339  * to check whether pinctrl device is ready or not. Maybe some GPIO pins
340  * of the same GPIO chip don't have back-end pinctrl interface.
341  * If the return value is true, it means that pinctrl device is ready & the
342  * certain GPIO pin doesn't have back-end pinctrl device. If the return value
343  * is false, it means that pinctrl device may not be ready.
344  */
345 #ifdef CONFIG_GPIOLIB
pinctrl_ready_for_gpio_range(unsigned gpio)346 static bool pinctrl_ready_for_gpio_range(unsigned gpio)
347 {
348 	struct pinctrl_dev *pctldev;
349 	struct pinctrl_gpio_range *range = NULL;
350 	struct gpio_chip *chip = gpio_to_chip(gpio);
351 
352 	if (WARN(!chip, "no gpio_chip for gpio%i?", gpio))
353 		return false;
354 
355 	mutex_lock(&pinctrldev_list_mutex);
356 
357 	/* Loop over the pin controllers */
358 	list_for_each_entry(pctldev, &pinctrldev_list, node) {
359 		/* Loop over the ranges */
360 		mutex_lock(&pctldev->mutex);
361 		list_for_each_entry(range, &pctldev->gpio_ranges, node) {
362 			/* Check if any gpio range overlapped with gpio chip */
363 			if (range->base + range->npins - 1 < chip->base ||
364 			    range->base > chip->base + chip->ngpio - 1)
365 				continue;
366 			mutex_unlock(&pctldev->mutex);
367 			mutex_unlock(&pinctrldev_list_mutex);
368 			return true;
369 		}
370 		mutex_unlock(&pctldev->mutex);
371 	}
372 
373 	mutex_unlock(&pinctrldev_list_mutex);
374 
375 	return false;
376 }
377 #else
pinctrl_ready_for_gpio_range(unsigned gpio)378 static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
379 #endif
380 
381 /**
382  * pinctrl_get_device_gpio_range() - find device for GPIO range
383  * @gpio: the pin to locate the pin controller for
384  * @outdev: the pin control device if found
385  * @outrange: the GPIO range if found
386  *
387  * Find the pin controller handling a certain GPIO pin from the pinspace of
388  * the GPIO subsystem, return the device and the matching GPIO range. Returns
389  * -EPROBE_DEFER if the GPIO range could not be found in any device since it
390  * may still have not been registered.
391  */
pinctrl_get_device_gpio_range(unsigned gpio,struct pinctrl_dev ** outdev,struct pinctrl_gpio_range ** outrange)392 static int pinctrl_get_device_gpio_range(unsigned gpio,
393 					 struct pinctrl_dev **outdev,
394 					 struct pinctrl_gpio_range **outrange)
395 {
396 	struct pinctrl_dev *pctldev = NULL;
397 
398 	mutex_lock(&pinctrldev_list_mutex);
399 
400 	/* Loop over the pin controllers */
401 	list_for_each_entry(pctldev, &pinctrldev_list, node) {
402 		struct pinctrl_gpio_range *range;
403 
404 		range = pinctrl_match_gpio_range(pctldev, gpio);
405 		if (range != NULL) {
406 			*outdev = pctldev;
407 			*outrange = range;
408 			mutex_unlock(&pinctrldev_list_mutex);
409 			return 0;
410 		}
411 	}
412 
413 	mutex_unlock(&pinctrldev_list_mutex);
414 
415 	return -EPROBE_DEFER;
416 }
417 
418 /**
419  * pinctrl_add_gpio_range() - register a GPIO range for a controller
420  * @pctldev: pin controller device to add the range to
421  * @range: the GPIO range to add
422  *
423  * This adds a range of GPIOs to be handled by a certain pin controller. Call
424  * this to register handled ranges after registering your pin controller.
425  */
pinctrl_add_gpio_range(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range)426 void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
427 			    struct pinctrl_gpio_range *range)
428 {
429 	mutex_lock(&pctldev->mutex);
430 	list_add_tail(&range->node, &pctldev->gpio_ranges);
431 	mutex_unlock(&pctldev->mutex);
432 }
433 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
434 
pinctrl_add_gpio_ranges(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * ranges,unsigned nranges)435 void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
436 			     struct pinctrl_gpio_range *ranges,
437 			     unsigned nranges)
438 {
439 	int i;
440 
441 	for (i = 0; i < nranges; i++)
442 		pinctrl_add_gpio_range(pctldev, &ranges[i]);
443 }
444 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
445 
pinctrl_find_and_add_gpio_range(const char * devname,struct pinctrl_gpio_range * range)446 struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
447 		struct pinctrl_gpio_range *range)
448 {
449 	struct pinctrl_dev *pctldev;
450 
451 	pctldev = get_pinctrl_dev_from_devname(devname);
452 
453 	/*
454 	 * If we can't find this device, let's assume that is because
455 	 * it has not probed yet, so the driver trying to register this
456 	 * range need to defer probing.
457 	 */
458 	if (!pctldev) {
459 		return ERR_PTR(-EPROBE_DEFER);
460 	}
461 	pinctrl_add_gpio_range(pctldev, range);
462 
463 	return pctldev;
464 }
465 EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
466 
pinctrl_get_group_pins(struct pinctrl_dev * pctldev,const char * pin_group,const unsigned ** pins,unsigned * num_pins)467 int pinctrl_get_group_pins(struct pinctrl_dev *pctldev, const char *pin_group,
468 				const unsigned **pins, unsigned *num_pins)
469 {
470 	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
471 	int gs;
472 
473 	if (!pctlops->get_group_pins)
474 		return -EINVAL;
475 
476 	gs = pinctrl_get_group_selector(pctldev, pin_group);
477 	if (gs < 0)
478 		return gs;
479 
480 	return pctlops->get_group_pins(pctldev, gs, pins, num_pins);
481 }
482 EXPORT_SYMBOL_GPL(pinctrl_get_group_pins);
483 
484 /**
485  * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
486  * @pctldev: the pin controller device to look in
487  * @pin: a controller-local number to find the range for
488  */
489 struct pinctrl_gpio_range *
pinctrl_find_gpio_range_from_pin(struct pinctrl_dev * pctldev,unsigned int pin)490 pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
491 				 unsigned int pin)
492 {
493 	struct pinctrl_gpio_range *range;
494 
495 	mutex_lock(&pctldev->mutex);
496 	/* Loop over the ranges */
497 	list_for_each_entry(range, &pctldev->gpio_ranges, node) {
498 		/* Check if we're in the valid range */
499 		if (range->pins) {
500 			int a;
501 			for (a = 0; a < range->npins; a++) {
502 				if (range->pins[a] == pin)
503 					goto out;
504 			}
505 		} else if (pin >= range->pin_base &&
506 			   pin < range->pin_base + range->npins)
507 			goto out;
508 	}
509 	range = NULL;
510 out:
511 	mutex_unlock(&pctldev->mutex);
512 	return range;
513 }
514 EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
515 
516 /**
517  * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
518  * @pctldev: pin controller device to remove the range from
519  * @range: the GPIO range to remove
520  */
pinctrl_remove_gpio_range(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range)521 void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
522 			       struct pinctrl_gpio_range *range)
523 {
524 	mutex_lock(&pctldev->mutex);
525 	list_del(&range->node);
526 	mutex_unlock(&pctldev->mutex);
527 }
528 EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
529 
530 /**
531  * pinctrl_get_group_selector() - returns the group selector for a group
532  * @pctldev: the pin controller handling the group
533  * @pin_group: the pin group to look up
534  */
pinctrl_get_group_selector(struct pinctrl_dev * pctldev,const char * pin_group)535 int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
536 			       const char *pin_group)
537 {
538 	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
539 	unsigned ngroups = pctlops->get_groups_count(pctldev);
540 	unsigned group_selector = 0;
541 
542 	while (group_selector < ngroups) {
543 		const char *gname = pctlops->get_group_name(pctldev,
544 							    group_selector);
545 		if (!strcmp(gname, pin_group)) {
546 			dev_dbg(pctldev->dev,
547 				"found group selector %u for %s\n",
548 				group_selector,
549 				pin_group);
550 			return group_selector;
551 		}
552 
553 		group_selector++;
554 	}
555 
556 	dev_err(pctldev->dev, "does not have pin group %s\n",
557 		pin_group);
558 
559 	return -EINVAL;
560 }
561 
562 /**
563  * pinctrl_request_gpio() - request a single pin to be used as GPIO
564  * @gpio: the GPIO pin number from the GPIO subsystem number space
565  *
566  * This function should *ONLY* be used from gpiolib-based GPIO drivers,
567  * as part of their gpio_request() semantics, platforms and individual drivers
568  * shall *NOT* request GPIO pins to be muxed in.
569  */
pinctrl_request_gpio(unsigned gpio)570 int pinctrl_request_gpio(unsigned gpio)
571 {
572 	struct pinctrl_dev *pctldev;
573 	struct pinctrl_gpio_range *range;
574 	int ret;
575 	int pin;
576 
577 	ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
578 	if (ret) {
579 		if (pinctrl_ready_for_gpio_range(gpio))
580 			ret = 0;
581 		return ret;
582 	}
583 
584 	mutex_lock(&pctldev->mutex);
585 
586 	/* Convert to the pin controllers number space */
587 	pin = gpio_to_pin(range, gpio);
588 
589 	ret = pinmux_request_gpio(pctldev, range, pin, gpio);
590 
591 	mutex_unlock(&pctldev->mutex);
592 
593 	return ret;
594 }
595 EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
596 
597 /**
598  * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
599  * @gpio: the GPIO pin number from the GPIO subsystem number space
600  *
601  * This function should *ONLY* be used from gpiolib-based GPIO drivers,
602  * as part of their gpio_free() semantics, platforms and individual drivers
603  * shall *NOT* request GPIO pins to be muxed out.
604  */
pinctrl_free_gpio(unsigned gpio)605 void pinctrl_free_gpio(unsigned gpio)
606 {
607 	struct pinctrl_dev *pctldev;
608 	struct pinctrl_gpio_range *range;
609 	int ret;
610 	int pin;
611 
612 	ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
613 	if (ret) {
614 		return;
615 	}
616 	mutex_lock(&pctldev->mutex);
617 
618 	/* Convert to the pin controllers number space */
619 	pin = gpio_to_pin(range, gpio);
620 
621 	pinmux_free_gpio(pctldev, pin, range);
622 
623 	mutex_unlock(&pctldev->mutex);
624 }
625 EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
626 
pinctrl_gpio_direction(unsigned gpio,bool input)627 static int pinctrl_gpio_direction(unsigned gpio, bool input)
628 {
629 	struct pinctrl_dev *pctldev;
630 	struct pinctrl_gpio_range *range;
631 	int ret;
632 	int pin;
633 
634 	ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
635 	if (ret) {
636 		return ret;
637 	}
638 
639 	mutex_lock(&pctldev->mutex);
640 
641 	/* Convert to the pin controllers number space */
642 	pin = gpio_to_pin(range, gpio);
643 	ret = pinmux_gpio_direction(pctldev, range, pin, input);
644 
645 	mutex_unlock(&pctldev->mutex);
646 
647 	return ret;
648 }
649 
650 /**
651  * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
652  * @gpio: the GPIO pin number from the GPIO subsystem number space
653  *
654  * This function should *ONLY* be used from gpiolib-based GPIO drivers,
655  * as part of their gpio_direction_input() semantics, platforms and individual
656  * drivers shall *NOT* touch pin control GPIO calls.
657  */
pinctrl_gpio_direction_input(unsigned gpio)658 int pinctrl_gpio_direction_input(unsigned gpio)
659 {
660 	return pinctrl_gpio_direction(gpio, true);
661 }
662 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
663 
664 /**
665  * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
666  * @gpio: the GPIO pin number from the GPIO subsystem number space
667  *
668  * This function should *ONLY* be used from gpiolib-based GPIO drivers,
669  * as part of their gpio_direction_output() semantics, platforms and individual
670  * drivers shall *NOT* touch pin control GPIO calls.
671  */
pinctrl_gpio_direction_output(unsigned gpio)672 int pinctrl_gpio_direction_output(unsigned gpio)
673 {
674 	return pinctrl_gpio_direction(gpio, false);
675 }
676 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
677 
find_state(struct pinctrl * p,const char * name)678 static struct pinctrl_state *find_state(struct pinctrl *p,
679 					const char *name)
680 {
681 	struct pinctrl_state *state;
682 
683 	list_for_each_entry(state, &p->states, node)
684 		if (!strcmp(state->name, name))
685 			return state;
686 
687 	return NULL;
688 }
689 
create_state(struct pinctrl * p,const char * name)690 static struct pinctrl_state *create_state(struct pinctrl *p,
691 					  const char *name)
692 {
693 	struct pinctrl_state *state;
694 
695 	state = kzalloc(sizeof(*state), GFP_KERNEL);
696 	if (state == NULL) {
697 		dev_err(p->dev,
698 			"failed to alloc struct pinctrl_state\n");
699 		return ERR_PTR(-ENOMEM);
700 	}
701 
702 	state->name = name;
703 	INIT_LIST_HEAD(&state->settings);
704 
705 	list_add_tail(&state->node, &p->states);
706 
707 	return state;
708 }
709 
add_setting(struct pinctrl * p,struct pinctrl_map const * map)710 static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
711 {
712 	struct pinctrl_state *state;
713 	struct pinctrl_setting *setting;
714 	int ret;
715 
716 	state = find_state(p, map->name);
717 	if (!state)
718 		state = create_state(p, map->name);
719 	if (IS_ERR(state))
720 		return PTR_ERR(state);
721 
722 	if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
723 		return 0;
724 
725 	setting = kzalloc(sizeof(*setting), GFP_KERNEL);
726 	if (setting == NULL) {
727 		dev_err(p->dev,
728 			"failed to alloc struct pinctrl_setting\n");
729 		return -ENOMEM;
730 	}
731 
732 	setting->type = map->type;
733 
734 	setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
735 	if (setting->pctldev == NULL) {
736 		kfree(setting);
737 		/* Do not defer probing of hogs (circular loop) */
738 		if (!strcmp(map->ctrl_dev_name, map->dev_name))
739 			return -ENODEV;
740 		/*
741 		 * OK let us guess that the driver is not there yet, and
742 		 * let's defer obtaining this pinctrl handle to later...
743 		 */
744 		dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
745 			map->ctrl_dev_name);
746 		return -EPROBE_DEFER;
747 	}
748 
749 	setting->dev_name = map->dev_name;
750 
751 	switch (map->type) {
752 	case PIN_MAP_TYPE_MUX_GROUP:
753 		ret = pinmux_map_to_setting(map, setting);
754 		break;
755 	case PIN_MAP_TYPE_CONFIGS_PIN:
756 	case PIN_MAP_TYPE_CONFIGS_GROUP:
757 		ret = pinconf_map_to_setting(map, setting);
758 		break;
759 	default:
760 		ret = -EINVAL;
761 		break;
762 	}
763 	if (ret < 0) {
764 		kfree(setting);
765 		return ret;
766 	}
767 
768 	list_add_tail(&setting->node, &state->settings);
769 
770 	return 0;
771 }
772 
find_pinctrl(struct device * dev)773 static struct pinctrl *find_pinctrl(struct device *dev)
774 {
775 	struct pinctrl *p;
776 
777 	mutex_lock(&pinctrl_list_mutex);
778 	list_for_each_entry(p, &pinctrl_list, node)
779 		if (p->dev == dev) {
780 			mutex_unlock(&pinctrl_list_mutex);
781 			return p;
782 		}
783 
784 	mutex_unlock(&pinctrl_list_mutex);
785 	return NULL;
786 }
787 
788 static void pinctrl_free(struct pinctrl *p, bool inlist);
789 
create_pinctrl(struct device * dev)790 static struct pinctrl *create_pinctrl(struct device *dev)
791 {
792 	struct pinctrl *p;
793 	const char *devname;
794 	struct pinctrl_maps *maps_node;
795 	int i;
796 	struct pinctrl_map const *map;
797 	int ret;
798 
799 	/*
800 	 * create the state cookie holder struct pinctrl for each
801 	 * mapping, this is what consumers will get when requesting
802 	 * a pin control handle with pinctrl_get()
803 	 */
804 	p = kzalloc(sizeof(*p), GFP_KERNEL);
805 	if (p == NULL) {
806 		dev_err(dev, "failed to alloc struct pinctrl\n");
807 		return ERR_PTR(-ENOMEM);
808 	}
809 	p->dev = dev;
810 	INIT_LIST_HEAD(&p->states);
811 	INIT_LIST_HEAD(&p->dt_maps);
812 
813 	ret = pinctrl_dt_to_map(p);
814 	if (ret < 0) {
815 		kfree(p);
816 		return ERR_PTR(ret);
817 	}
818 
819 	devname = dev_name(dev);
820 
821 	mutex_lock(&pinctrl_maps_mutex);
822 	/* Iterate over the pin control maps to locate the right ones */
823 	for_each_maps(maps_node, i, map) {
824 		/* Map must be for this device */
825 		if (strcmp(map->dev_name, devname))
826 			continue;
827 
828 		ret = add_setting(p, map);
829 		/*
830 		 * At this point the adding of a setting may:
831 		 *
832 		 * - Defer, if the pinctrl device is not yet available
833 		 * - Fail, if the pinctrl device is not yet available,
834 		 *   AND the setting is a hog. We cannot defer that, since
835 		 *   the hog will kick in immediately after the device
836 		 *   is registered.
837 		 *
838 		 * If the error returned was not -EPROBE_DEFER then we
839 		 * accumulate the errors to see if we end up with
840 		 * an -EPROBE_DEFER later, as that is the worst case.
841 		 */
842 		if (ret == -EPROBE_DEFER) {
843 			pinctrl_free(p, false);
844 			mutex_unlock(&pinctrl_maps_mutex);
845 			return ERR_PTR(ret);
846 		}
847 	}
848 	mutex_unlock(&pinctrl_maps_mutex);
849 
850 	if (ret < 0) {
851 		/* If some other error than deferral occured, return here */
852 		pinctrl_free(p, false);
853 		return ERR_PTR(ret);
854 	}
855 
856 	kref_init(&p->users);
857 
858 	/* Add the pinctrl handle to the global list */
859 	mutex_lock(&pinctrl_list_mutex);
860 	list_add_tail(&p->node, &pinctrl_list);
861 	mutex_unlock(&pinctrl_list_mutex);
862 
863 	return p;
864 }
865 
866 /**
867  * pinctrl_get() - retrieves the pinctrl handle for a device
868  * @dev: the device to obtain the handle for
869  */
pinctrl_get(struct device * dev)870 struct pinctrl *pinctrl_get(struct device *dev)
871 {
872 	struct pinctrl *p;
873 
874 	if (WARN_ON(!dev))
875 		return ERR_PTR(-EINVAL);
876 
877 	/*
878 	 * See if somebody else (such as the device core) has already
879 	 * obtained a handle to the pinctrl for this device. In that case,
880 	 * return another pointer to it.
881 	 */
882 	p = find_pinctrl(dev);
883 	if (p != NULL) {
884 		dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
885 		kref_get(&p->users);
886 		return p;
887 	}
888 
889 	return create_pinctrl(dev);
890 }
891 EXPORT_SYMBOL_GPL(pinctrl_get);
892 
pinctrl_free_setting(bool disable_setting,struct pinctrl_setting * setting)893 static void pinctrl_free_setting(bool disable_setting,
894 				 struct pinctrl_setting *setting)
895 {
896 	switch (setting->type) {
897 	case PIN_MAP_TYPE_MUX_GROUP:
898 		if (disable_setting)
899 			pinmux_disable_setting(setting);
900 		pinmux_free_setting(setting);
901 		break;
902 	case PIN_MAP_TYPE_CONFIGS_PIN:
903 	case PIN_MAP_TYPE_CONFIGS_GROUP:
904 		pinconf_free_setting(setting);
905 		break;
906 	default:
907 		break;
908 	}
909 }
910 
pinctrl_free(struct pinctrl * p,bool inlist)911 static void pinctrl_free(struct pinctrl *p, bool inlist)
912 {
913 	struct pinctrl_state *state, *n1;
914 	struct pinctrl_setting *setting, *n2;
915 
916 	mutex_lock(&pinctrl_list_mutex);
917 	list_for_each_entry_safe(state, n1, &p->states, node) {
918 		list_for_each_entry_safe(setting, n2, &state->settings, node) {
919 			pinctrl_free_setting(state == p->state, setting);
920 			list_del(&setting->node);
921 			kfree(setting);
922 		}
923 		list_del(&state->node);
924 		kfree(state);
925 	}
926 
927 	pinctrl_dt_free_maps(p);
928 
929 	if (inlist)
930 		list_del(&p->node);
931 	kfree(p);
932 	mutex_unlock(&pinctrl_list_mutex);
933 }
934 
935 /**
936  * pinctrl_release() - release the pinctrl handle
937  * @kref: the kref in the pinctrl being released
938  */
pinctrl_release(struct kref * kref)939 static void pinctrl_release(struct kref *kref)
940 {
941 	struct pinctrl *p = container_of(kref, struct pinctrl, users);
942 
943 	pinctrl_free(p, true);
944 }
945 
946 /**
947  * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
948  * @p: the pinctrl handle to release
949  */
pinctrl_put(struct pinctrl * p)950 void pinctrl_put(struct pinctrl *p)
951 {
952 	kref_put(&p->users, pinctrl_release);
953 }
954 EXPORT_SYMBOL_GPL(pinctrl_put);
955 
956 /**
957  * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
958  * @p: the pinctrl handle to retrieve the state from
959  * @name: the state name to retrieve
960  */
pinctrl_lookup_state(struct pinctrl * p,const char * name)961 struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
962 						 const char *name)
963 {
964 	struct pinctrl_state *state;
965 
966 	state = find_state(p, name);
967 	if (!state) {
968 		if (pinctrl_dummy_state) {
969 			/* create dummy state */
970 			dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
971 				name);
972 			state = create_state(p, name);
973 		} else
974 			state = ERR_PTR(-ENODEV);
975 	}
976 
977 	return state;
978 }
979 EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
980 
981 /**
982  * pinctrl_commit_state() - select/activate/program a pinctrl state to HW
983  * @p: the pinctrl handle for the device that requests configuration
984  * @state: the state handle to select/activate/program
985  */
pinctrl_commit_state(struct pinctrl * p,struct pinctrl_state * state)986 static int pinctrl_commit_state(struct pinctrl *p, struct pinctrl_state *state)
987 {
988 	struct pinctrl_setting *setting, *setting2;
989 	struct pinctrl_state *old_state = p->state;
990 	int ret;
991 
992 	if (p->state) {
993 		/*
994 		 * For each pinmux setting in the old state, forget SW's record
995 		 * of mux owner for that pingroup. Any pingroups which are
996 		 * still owned by the new state will be re-acquired by the call
997 		 * to pinmux_enable_setting() in the loop below.
998 		 */
999 		list_for_each_entry(setting, &p->state->settings, node) {
1000 			if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
1001 				continue;
1002 			pinmux_disable_setting(setting);
1003 		}
1004 	}
1005 
1006 	p->state = NULL;
1007 
1008 	/* Apply all the settings for the new state */
1009 	list_for_each_entry(setting, &state->settings, node) {
1010 		switch (setting->type) {
1011 		case PIN_MAP_TYPE_MUX_GROUP:
1012 			ret = pinmux_enable_setting(setting);
1013 			break;
1014 		case PIN_MAP_TYPE_CONFIGS_PIN:
1015 		case PIN_MAP_TYPE_CONFIGS_GROUP:
1016 			ret = pinconf_apply_setting(setting);
1017 			break;
1018 		default:
1019 			ret = -EINVAL;
1020 			break;
1021 		}
1022 
1023 		if (ret < 0) {
1024 			goto unapply_new_state;
1025 		}
1026 	}
1027 
1028 	p->state = state;
1029 
1030 	return 0;
1031 
1032 unapply_new_state:
1033 	dev_err(p->dev, "Error applying setting, reverse things back\n");
1034 
1035 	list_for_each_entry(setting2, &state->settings, node) {
1036 		if (&setting2->node == &setting->node)
1037 			break;
1038 		/*
1039 		 * All we can do here is pinmux_disable_setting.
1040 		 * That means that some pins are muxed differently now
1041 		 * than they were before applying the setting (We can't
1042 		 * "unmux a pin"!), but it's not a big deal since the pins
1043 		 * are free to be muxed by another apply_setting.
1044 		 */
1045 		if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
1046 			pinmux_disable_setting(setting2);
1047 	}
1048 
1049 	/* There's no infinite recursive loop here because p->state is NULL */
1050 	if (old_state)
1051 		pinctrl_select_state(p, old_state);
1052 
1053 	return ret;
1054 }
1055 
1056 /**
1057  * pinctrl_select_state() - select/activate/program a pinctrl state to HW
1058  * @p: the pinctrl handle for the device that requests configuration
1059  * @state: the state handle to select/activate/program
1060  */
pinctrl_select_state(struct pinctrl * p,struct pinctrl_state * state)1061 int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
1062 {
1063 	if (p->state == state)
1064 		return 0;
1065 
1066 	return pinctrl_commit_state(p, state);
1067 }
1068 EXPORT_SYMBOL_GPL(pinctrl_select_state);
1069 
devm_pinctrl_release(struct device * dev,void * res)1070 static void devm_pinctrl_release(struct device *dev, void *res)
1071 {
1072 	pinctrl_put(*(struct pinctrl **)res);
1073 }
1074 
1075 /**
1076  * struct devm_pinctrl_get() - Resource managed pinctrl_get()
1077  * @dev: the device to obtain the handle for
1078  *
1079  * If there is a need to explicitly destroy the returned struct pinctrl,
1080  * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1081  */
devm_pinctrl_get(struct device * dev)1082 struct pinctrl *devm_pinctrl_get(struct device *dev)
1083 {
1084 	struct pinctrl **ptr, *p;
1085 
1086 	ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
1087 	if (!ptr)
1088 		return ERR_PTR(-ENOMEM);
1089 
1090 	p = pinctrl_get(dev);
1091 	if (!IS_ERR(p)) {
1092 		*ptr = p;
1093 		devres_add(dev, ptr);
1094 	} else {
1095 		devres_free(ptr);
1096 	}
1097 
1098 	return p;
1099 }
1100 EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1101 
devm_pinctrl_match(struct device * dev,void * res,void * data)1102 static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1103 {
1104 	struct pinctrl **p = res;
1105 
1106 	return *p == data;
1107 }
1108 
1109 /**
1110  * devm_pinctrl_put() - Resource managed pinctrl_put()
1111  * @p: the pinctrl handle to release
1112  *
1113  * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1114  * this function will not need to be called and the resource management
1115  * code will ensure that the resource is freed.
1116  */
devm_pinctrl_put(struct pinctrl * p)1117 void devm_pinctrl_put(struct pinctrl *p)
1118 {
1119 	WARN_ON(devres_release(p->dev, devm_pinctrl_release,
1120 			       devm_pinctrl_match, p));
1121 }
1122 EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1123 
pinctrl_register_map(struct pinctrl_map const * maps,unsigned num_maps,bool dup)1124 int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
1125 			 bool dup)
1126 {
1127 	int i, ret;
1128 	struct pinctrl_maps *maps_node;
1129 
1130 	pr_debug("add %u pinctrl maps\n", num_maps);
1131 
1132 	/* First sanity check the new mapping */
1133 	for (i = 0; i < num_maps; i++) {
1134 		if (!maps[i].dev_name) {
1135 			pr_err("failed to register map %s (%d): no device given\n",
1136 			       maps[i].name, i);
1137 			return -EINVAL;
1138 		}
1139 
1140 		if (!maps[i].name) {
1141 			pr_err("failed to register map %d: no map name given\n",
1142 			       i);
1143 			return -EINVAL;
1144 		}
1145 
1146 		if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1147 				!maps[i].ctrl_dev_name) {
1148 			pr_err("failed to register map %s (%d): no pin control device given\n",
1149 			       maps[i].name, i);
1150 			return -EINVAL;
1151 		}
1152 
1153 		switch (maps[i].type) {
1154 		case PIN_MAP_TYPE_DUMMY_STATE:
1155 			break;
1156 		case PIN_MAP_TYPE_MUX_GROUP:
1157 			ret = pinmux_validate_map(&maps[i], i);
1158 			if (ret < 0)
1159 				return ret;
1160 			break;
1161 		case PIN_MAP_TYPE_CONFIGS_PIN:
1162 		case PIN_MAP_TYPE_CONFIGS_GROUP:
1163 			ret = pinconf_validate_map(&maps[i], i);
1164 			if (ret < 0)
1165 				return ret;
1166 			break;
1167 		default:
1168 			pr_err("failed to register map %s (%d): invalid type given\n",
1169 			       maps[i].name, i);
1170 			return -EINVAL;
1171 		}
1172 	}
1173 
1174 	maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
1175 	if (!maps_node) {
1176 		pr_err("failed to alloc struct pinctrl_maps\n");
1177 		return -ENOMEM;
1178 	}
1179 
1180 	maps_node->num_maps = num_maps;
1181 	if (dup) {
1182 		maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
1183 					  GFP_KERNEL);
1184 		if (!maps_node->maps) {
1185 			pr_err("failed to duplicate mapping table\n");
1186 			kfree(maps_node);
1187 			return -ENOMEM;
1188 		}
1189 	} else {
1190 		maps_node->maps = maps;
1191 	}
1192 
1193 	mutex_lock(&pinctrl_maps_mutex);
1194 	list_add_tail(&maps_node->node, &pinctrl_maps);
1195 	mutex_unlock(&pinctrl_maps_mutex);
1196 
1197 	return 0;
1198 }
1199 
1200 /**
1201  * pinctrl_register_mappings() - register a set of pin controller mappings
1202  * @maps: the pincontrol mappings table to register. This should probably be
1203  *	marked with __initdata so it can be discarded after boot. This
1204  *	function will perform a shallow copy for the mapping entries.
1205  * @num_maps: the number of maps in the mapping table
1206  */
pinctrl_register_mappings(struct pinctrl_map const * maps,unsigned num_maps)1207 int pinctrl_register_mappings(struct pinctrl_map const *maps,
1208 			      unsigned num_maps)
1209 {
1210 	return pinctrl_register_map(maps, num_maps, true);
1211 }
1212 
pinctrl_unregister_map(struct pinctrl_map const * map)1213 void pinctrl_unregister_map(struct pinctrl_map const *map)
1214 {
1215 	struct pinctrl_maps *maps_node;
1216 
1217 	mutex_lock(&pinctrl_maps_mutex);
1218 	list_for_each_entry(maps_node, &pinctrl_maps, node) {
1219 		if (maps_node->maps == map) {
1220 			list_del(&maps_node->node);
1221 			kfree(maps_node);
1222 			mutex_unlock(&pinctrl_maps_mutex);
1223 			return;
1224 		}
1225 	}
1226 	mutex_unlock(&pinctrl_maps_mutex);
1227 }
1228 
1229 /**
1230  * pinctrl_force_sleep() - turn a given controller device into sleep state
1231  * @pctldev: pin controller device
1232  */
pinctrl_force_sleep(struct pinctrl_dev * pctldev)1233 int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1234 {
1235 	if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
1236 		return pinctrl_commit_state(pctldev->p, pctldev->hog_sleep);
1237 	return 0;
1238 }
1239 EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1240 
1241 /**
1242  * pinctrl_force_default() - turn a given controller device into default state
1243  * @pctldev: pin controller device
1244  */
pinctrl_force_default(struct pinctrl_dev * pctldev)1245 int pinctrl_force_default(struct pinctrl_dev *pctldev)
1246 {
1247 	if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
1248 		return pinctrl_commit_state(pctldev->p, pctldev->hog_default);
1249 	return 0;
1250 }
1251 EXPORT_SYMBOL_GPL(pinctrl_force_default);
1252 
1253 /**
1254  * pinctrl_init_done() - tell pinctrl probe is done
1255  *
1256  * We'll use this time to switch the pins from "init" to "default" unless the
1257  * driver selected some other state.
1258  *
1259  * @dev: device to that's done probing
1260  */
pinctrl_init_done(struct device * dev)1261 int pinctrl_init_done(struct device *dev)
1262 {
1263 	struct dev_pin_info *pins = dev->pins;
1264 	int ret;
1265 
1266 	if (!pins)
1267 		return 0;
1268 
1269 	if (IS_ERR(pins->init_state))
1270 		return 0; /* No such state */
1271 
1272 	if (pins->p->state != pins->init_state)
1273 		return 0; /* Not at init anyway */
1274 
1275 	if (IS_ERR(pins->default_state))
1276 		return 0; /* No default state */
1277 
1278 	ret = pinctrl_select_state(pins->p, pins->default_state);
1279 	if (ret)
1280 		dev_err(dev, "failed to activate default pinctrl state\n");
1281 
1282 	return ret;
1283 }
1284 
1285 #ifdef CONFIG_PM
1286 
1287 /**
1288  * pinctrl_pm_select_state() - select pinctrl state for PM
1289  * @dev: device to select default state for
1290  * @state: state to set
1291  */
pinctrl_pm_select_state(struct device * dev,struct pinctrl_state * state)1292 static int pinctrl_pm_select_state(struct device *dev,
1293 				   struct pinctrl_state *state)
1294 {
1295 	struct dev_pin_info *pins = dev->pins;
1296 	int ret;
1297 
1298 	if (IS_ERR(state))
1299 		return 0; /* No such state */
1300 	ret = pinctrl_select_state(pins->p, state);
1301 	if (ret)
1302 		dev_err(dev, "failed to activate pinctrl state %s\n",
1303 			state->name);
1304 	return ret;
1305 }
1306 
1307 /**
1308  * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1309  * @dev: device to select default state for
1310  */
pinctrl_pm_select_default_state(struct device * dev)1311 int pinctrl_pm_select_default_state(struct device *dev)
1312 {
1313 	if (!dev->pins)
1314 		return 0;
1315 
1316 	return pinctrl_pm_select_state(dev, dev->pins->default_state);
1317 }
1318 EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
1319 
1320 /**
1321  * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1322  * @dev: device to select sleep state for
1323  */
pinctrl_pm_select_sleep_state(struct device * dev)1324 int pinctrl_pm_select_sleep_state(struct device *dev)
1325 {
1326 	if (!dev->pins)
1327 		return 0;
1328 
1329 	return pinctrl_pm_select_state(dev, dev->pins->sleep_state);
1330 }
1331 EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
1332 
1333 /**
1334  * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1335  * @dev: device to select idle state for
1336  */
pinctrl_pm_select_idle_state(struct device * dev)1337 int pinctrl_pm_select_idle_state(struct device *dev)
1338 {
1339 	if (!dev->pins)
1340 		return 0;
1341 
1342 	return pinctrl_pm_select_state(dev, dev->pins->idle_state);
1343 }
1344 EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
1345 #endif
1346 
1347 #ifdef CONFIG_DEBUG_FS
1348 
pinctrl_pins_show(struct seq_file * s,void * what)1349 static int pinctrl_pins_show(struct seq_file *s, void *what)
1350 {
1351 	struct pinctrl_dev *pctldev = s->private;
1352 	const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1353 	unsigned i, pin;
1354 
1355 	seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
1356 
1357 	mutex_lock(&pctldev->mutex);
1358 
1359 	/* The pin number can be retrived from the pin controller descriptor */
1360 	for (i = 0; i < pctldev->desc->npins; i++) {
1361 		struct pin_desc *desc;
1362 
1363 		pin = pctldev->desc->pins[i].number;
1364 		desc = pin_desc_get(pctldev, pin);
1365 		/* Pin space may be sparse */
1366 		if (desc == NULL)
1367 			continue;
1368 
1369 		seq_printf(s, "pin %d (%s) ", pin,
1370 			   desc->name ? desc->name : "unnamed");
1371 
1372 		/* Driver-specific info per pin */
1373 		if (ops->pin_dbg_show)
1374 			ops->pin_dbg_show(pctldev, s, pin);
1375 
1376 		seq_puts(s, "\n");
1377 	}
1378 
1379 	mutex_unlock(&pctldev->mutex);
1380 
1381 	return 0;
1382 }
1383 
pinctrl_groups_show(struct seq_file * s,void * what)1384 static int pinctrl_groups_show(struct seq_file *s, void *what)
1385 {
1386 	struct pinctrl_dev *pctldev = s->private;
1387 	const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1388 	unsigned ngroups, selector = 0;
1389 
1390 	mutex_lock(&pctldev->mutex);
1391 
1392 	ngroups = ops->get_groups_count(pctldev);
1393 
1394 	seq_puts(s, "registered pin groups:\n");
1395 	while (selector < ngroups) {
1396 		const unsigned *pins = NULL;
1397 		unsigned num_pins = 0;
1398 		const char *gname = ops->get_group_name(pctldev, selector);
1399 		const char *pname;
1400 		int ret = 0;
1401 		int i;
1402 
1403 		if (ops->get_group_pins)
1404 			ret = ops->get_group_pins(pctldev, selector,
1405 						  &pins, &num_pins);
1406 		if (ret)
1407 			seq_printf(s, "%s [ERROR GETTING PINS]\n",
1408 				   gname);
1409 		else {
1410 			seq_printf(s, "group: %s\n", gname);
1411 			for (i = 0; i < num_pins; i++) {
1412 				pname = pin_get_name(pctldev, pins[i]);
1413 				if (WARN_ON(!pname)) {
1414 					mutex_unlock(&pctldev->mutex);
1415 					return -EINVAL;
1416 				}
1417 				seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1418 			}
1419 			seq_puts(s, "\n");
1420 		}
1421 		selector++;
1422 	}
1423 
1424 	mutex_unlock(&pctldev->mutex);
1425 
1426 	return 0;
1427 }
1428 
pinctrl_gpioranges_show(struct seq_file * s,void * what)1429 static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1430 {
1431 	struct pinctrl_dev *pctldev = s->private;
1432 	struct pinctrl_gpio_range *range = NULL;
1433 
1434 	seq_puts(s, "GPIO ranges handled:\n");
1435 
1436 	mutex_lock(&pctldev->mutex);
1437 
1438 	/* Loop over the ranges */
1439 	list_for_each_entry(range, &pctldev->gpio_ranges, node) {
1440 		if (range->pins) {
1441 			int a;
1442 			seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
1443 				range->id, range->name,
1444 				range->base, (range->base + range->npins - 1));
1445 			for (a = 0; a < range->npins - 1; a++)
1446 				seq_printf(s, "%u, ", range->pins[a]);
1447 			seq_printf(s, "%u}\n", range->pins[a]);
1448 		}
1449 		else
1450 			seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1451 				range->id, range->name,
1452 				range->base, (range->base + range->npins - 1),
1453 				range->pin_base,
1454 				(range->pin_base + range->npins - 1));
1455 	}
1456 
1457 	mutex_unlock(&pctldev->mutex);
1458 
1459 	return 0;
1460 }
1461 
pinctrl_devices_show(struct seq_file * s,void * what)1462 static int pinctrl_devices_show(struct seq_file *s, void *what)
1463 {
1464 	struct pinctrl_dev *pctldev;
1465 
1466 	seq_puts(s, "name [pinmux] [pinconf]\n");
1467 
1468 	mutex_lock(&pinctrldev_list_mutex);
1469 
1470 	list_for_each_entry(pctldev, &pinctrldev_list, node) {
1471 		seq_printf(s, "%s ", pctldev->desc->name);
1472 		if (pctldev->desc->pmxops)
1473 			seq_puts(s, "yes ");
1474 		else
1475 			seq_puts(s, "no ");
1476 		if (pctldev->desc->confops)
1477 			seq_puts(s, "yes");
1478 		else
1479 			seq_puts(s, "no");
1480 		seq_puts(s, "\n");
1481 	}
1482 
1483 	mutex_unlock(&pinctrldev_list_mutex);
1484 
1485 	return 0;
1486 }
1487 
map_type(enum pinctrl_map_type type)1488 static inline const char *map_type(enum pinctrl_map_type type)
1489 {
1490 	static const char * const names[] = {
1491 		"INVALID",
1492 		"DUMMY_STATE",
1493 		"MUX_GROUP",
1494 		"CONFIGS_PIN",
1495 		"CONFIGS_GROUP",
1496 	};
1497 
1498 	if (type >= ARRAY_SIZE(names))
1499 		return "UNKNOWN";
1500 
1501 	return names[type];
1502 }
1503 
pinctrl_maps_show(struct seq_file * s,void * what)1504 static int pinctrl_maps_show(struct seq_file *s, void *what)
1505 {
1506 	struct pinctrl_maps *maps_node;
1507 	int i;
1508 	struct pinctrl_map const *map;
1509 
1510 	seq_puts(s, "Pinctrl maps:\n");
1511 
1512 	mutex_lock(&pinctrl_maps_mutex);
1513 	for_each_maps(maps_node, i, map) {
1514 		seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1515 			   map->dev_name, map->name, map_type(map->type),
1516 			   map->type);
1517 
1518 		if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1519 			seq_printf(s, "controlling device %s\n",
1520 				   map->ctrl_dev_name);
1521 
1522 		switch (map->type) {
1523 		case PIN_MAP_TYPE_MUX_GROUP:
1524 			pinmux_show_map(s, map);
1525 			break;
1526 		case PIN_MAP_TYPE_CONFIGS_PIN:
1527 		case PIN_MAP_TYPE_CONFIGS_GROUP:
1528 			pinconf_show_map(s, map);
1529 			break;
1530 		default:
1531 			break;
1532 		}
1533 
1534 		seq_printf(s, "\n");
1535 	}
1536 	mutex_unlock(&pinctrl_maps_mutex);
1537 
1538 	return 0;
1539 }
1540 
pinctrl_show(struct seq_file * s,void * what)1541 static int pinctrl_show(struct seq_file *s, void *what)
1542 {
1543 	struct pinctrl *p;
1544 	struct pinctrl_state *state;
1545 	struct pinctrl_setting *setting;
1546 
1547 	seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
1548 
1549 	mutex_lock(&pinctrl_list_mutex);
1550 
1551 	list_for_each_entry(p, &pinctrl_list, node) {
1552 		seq_printf(s, "device: %s current state: %s\n",
1553 			   dev_name(p->dev),
1554 			   p->state ? p->state->name : "none");
1555 
1556 		list_for_each_entry(state, &p->states, node) {
1557 			seq_printf(s, "  state: %s\n", state->name);
1558 
1559 			list_for_each_entry(setting, &state->settings, node) {
1560 				struct pinctrl_dev *pctldev = setting->pctldev;
1561 
1562 				seq_printf(s, "    type: %s controller %s ",
1563 					   map_type(setting->type),
1564 					   pinctrl_dev_get_name(pctldev));
1565 
1566 				switch (setting->type) {
1567 				case PIN_MAP_TYPE_MUX_GROUP:
1568 					pinmux_show_setting(s, setting);
1569 					break;
1570 				case PIN_MAP_TYPE_CONFIGS_PIN:
1571 				case PIN_MAP_TYPE_CONFIGS_GROUP:
1572 					pinconf_show_setting(s, setting);
1573 					break;
1574 				default:
1575 					break;
1576 				}
1577 			}
1578 		}
1579 	}
1580 
1581 	mutex_unlock(&pinctrl_list_mutex);
1582 
1583 	return 0;
1584 }
1585 
pinctrl_pins_open(struct inode * inode,struct file * file)1586 static int pinctrl_pins_open(struct inode *inode, struct file *file)
1587 {
1588 	return single_open(file, pinctrl_pins_show, inode->i_private);
1589 }
1590 
pinctrl_groups_open(struct inode * inode,struct file * file)1591 static int pinctrl_groups_open(struct inode *inode, struct file *file)
1592 {
1593 	return single_open(file, pinctrl_groups_show, inode->i_private);
1594 }
1595 
pinctrl_gpioranges_open(struct inode * inode,struct file * file)1596 static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1597 {
1598 	return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1599 }
1600 
pinctrl_devices_open(struct inode * inode,struct file * file)1601 static int pinctrl_devices_open(struct inode *inode, struct file *file)
1602 {
1603 	return single_open(file, pinctrl_devices_show, NULL);
1604 }
1605 
pinctrl_maps_open(struct inode * inode,struct file * file)1606 static int pinctrl_maps_open(struct inode *inode, struct file *file)
1607 {
1608 	return single_open(file, pinctrl_maps_show, NULL);
1609 }
1610 
pinctrl_open(struct inode * inode,struct file * file)1611 static int pinctrl_open(struct inode *inode, struct file *file)
1612 {
1613 	return single_open(file, pinctrl_show, NULL);
1614 }
1615 
1616 static const struct file_operations pinctrl_pins_ops = {
1617 	.open		= pinctrl_pins_open,
1618 	.read		= seq_read,
1619 	.llseek		= seq_lseek,
1620 	.release	= single_release,
1621 };
1622 
1623 static const struct file_operations pinctrl_groups_ops = {
1624 	.open		= pinctrl_groups_open,
1625 	.read		= seq_read,
1626 	.llseek		= seq_lseek,
1627 	.release	= single_release,
1628 };
1629 
1630 static const struct file_operations pinctrl_gpioranges_ops = {
1631 	.open		= pinctrl_gpioranges_open,
1632 	.read		= seq_read,
1633 	.llseek		= seq_lseek,
1634 	.release	= single_release,
1635 };
1636 
1637 static const struct file_operations pinctrl_devices_ops = {
1638 	.open		= pinctrl_devices_open,
1639 	.read		= seq_read,
1640 	.llseek		= seq_lseek,
1641 	.release	= single_release,
1642 };
1643 
1644 static const struct file_operations pinctrl_maps_ops = {
1645 	.open		= pinctrl_maps_open,
1646 	.read		= seq_read,
1647 	.llseek		= seq_lseek,
1648 	.release	= single_release,
1649 };
1650 
1651 static const struct file_operations pinctrl_ops = {
1652 	.open		= pinctrl_open,
1653 	.read		= seq_read,
1654 	.llseek		= seq_lseek,
1655 	.release	= single_release,
1656 };
1657 
1658 static struct dentry *debugfs_root;
1659 
pinctrl_init_device_debugfs(struct pinctrl_dev * pctldev)1660 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1661 {
1662 	struct dentry *device_root;
1663 
1664 	device_root = debugfs_create_dir(dev_name(pctldev->dev),
1665 					 debugfs_root);
1666 	pctldev->device_root = device_root;
1667 
1668 	if (IS_ERR(device_root) || !device_root) {
1669 		pr_warn("failed to create debugfs directory for %s\n",
1670 			dev_name(pctldev->dev));
1671 		return;
1672 	}
1673 	debugfs_create_file("pins", S_IFREG | S_IRUGO,
1674 			    device_root, pctldev, &pinctrl_pins_ops);
1675 	debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1676 			    device_root, pctldev, &pinctrl_groups_ops);
1677 	debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1678 			    device_root, pctldev, &pinctrl_gpioranges_ops);
1679 	if (pctldev->desc->pmxops)
1680 		pinmux_init_device_debugfs(device_root, pctldev);
1681 	if (pctldev->desc->confops)
1682 		pinconf_init_device_debugfs(device_root, pctldev);
1683 }
1684 
pinctrl_remove_device_debugfs(struct pinctrl_dev * pctldev)1685 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1686 {
1687 	debugfs_remove_recursive(pctldev->device_root);
1688 }
1689 
pinctrl_init_debugfs(void)1690 static void pinctrl_init_debugfs(void)
1691 {
1692 	debugfs_root = debugfs_create_dir("pinctrl", NULL);
1693 	if (IS_ERR(debugfs_root) || !debugfs_root) {
1694 		pr_warn("failed to create debugfs directory\n");
1695 		debugfs_root = NULL;
1696 		return;
1697 	}
1698 
1699 	debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1700 			    debugfs_root, NULL, &pinctrl_devices_ops);
1701 	debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1702 			    debugfs_root, NULL, &pinctrl_maps_ops);
1703 	debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1704 			    debugfs_root, NULL, &pinctrl_ops);
1705 }
1706 
1707 #else /* CONFIG_DEBUG_FS */
1708 
pinctrl_init_device_debugfs(struct pinctrl_dev * pctldev)1709 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1710 {
1711 }
1712 
pinctrl_init_debugfs(void)1713 static void pinctrl_init_debugfs(void)
1714 {
1715 }
1716 
pinctrl_remove_device_debugfs(struct pinctrl_dev * pctldev)1717 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1718 {
1719 }
1720 
1721 #endif
1722 
pinctrl_check_ops(struct pinctrl_dev * pctldev)1723 static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1724 {
1725 	const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1726 
1727 	if (!ops ||
1728 	    !ops->get_groups_count ||
1729 	    !ops->get_group_name)
1730 		return -EINVAL;
1731 
1732 	if (ops->dt_node_to_map && !ops->dt_free_map)
1733 		return -EINVAL;
1734 
1735 	return 0;
1736 }
1737 
1738 /**
1739  * pinctrl_register() - register a pin controller device
1740  * @pctldesc: descriptor for this pin controller
1741  * @dev: parent device for this pin controller
1742  * @driver_data: private pin controller data for this pin controller
1743  */
pinctrl_register(struct pinctrl_desc * pctldesc,struct device * dev,void * driver_data)1744 struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1745 				    struct device *dev, void *driver_data)
1746 {
1747 	struct pinctrl_dev *pctldev;
1748 	int ret;
1749 
1750 	if (!pctldesc)
1751 		return ERR_PTR(-EINVAL);
1752 	if (!pctldesc->name)
1753 		return ERR_PTR(-EINVAL);
1754 
1755 	pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
1756 	if (pctldev == NULL) {
1757 		dev_err(dev, "failed to alloc struct pinctrl_dev\n");
1758 		return ERR_PTR(-ENOMEM);
1759 	}
1760 
1761 	/* Initialize pin control device struct */
1762 	pctldev->owner = pctldesc->owner;
1763 	pctldev->desc = pctldesc;
1764 	pctldev->driver_data = driver_data;
1765 	INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
1766 	INIT_LIST_HEAD(&pctldev->gpio_ranges);
1767 	pctldev->dev = dev;
1768 	mutex_init(&pctldev->mutex);
1769 
1770 	/* check core ops for sanity */
1771 	ret = pinctrl_check_ops(pctldev);
1772 	if (ret) {
1773 		dev_err(dev, "pinctrl ops lacks necessary functions\n");
1774 		goto out_err;
1775 	}
1776 
1777 	/* If we're implementing pinmuxing, check the ops for sanity */
1778 	if (pctldesc->pmxops) {
1779 		ret = pinmux_check_ops(pctldev);
1780 		if (ret)
1781 			goto out_err;
1782 	}
1783 
1784 	/* If we're implementing pinconfig, check the ops for sanity */
1785 	if (pctldesc->confops) {
1786 		ret = pinconf_check_ops(pctldev);
1787 		if (ret)
1788 			goto out_err;
1789 	}
1790 
1791 	/* Register all the pins */
1792 	dev_dbg(dev, "try to register %d pins ...\n",  pctldesc->npins);
1793 	ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1794 	if (ret) {
1795 		dev_err(dev, "error during pin registration\n");
1796 		pinctrl_free_pindescs(pctldev, pctldesc->pins,
1797 				      pctldesc->npins);
1798 		goto out_err;
1799 	}
1800 
1801 	mutex_lock(&pinctrldev_list_mutex);
1802 	list_add_tail(&pctldev->node, &pinctrldev_list);
1803 	mutex_unlock(&pinctrldev_list_mutex);
1804 
1805 	pctldev->p = pinctrl_get(pctldev->dev);
1806 
1807 	if (!IS_ERR(pctldev->p)) {
1808 		pctldev->hog_default =
1809 			pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
1810 		if (IS_ERR(pctldev->hog_default)) {
1811 			dev_dbg(dev, "failed to lookup the default state\n");
1812 		} else {
1813 			if (pinctrl_select_state(pctldev->p,
1814 						pctldev->hog_default))
1815 				dev_err(dev,
1816 					"failed to select default state\n");
1817 		}
1818 
1819 		pctldev->hog_sleep =
1820 			pinctrl_lookup_state(pctldev->p,
1821 						    PINCTRL_STATE_SLEEP);
1822 		if (IS_ERR(pctldev->hog_sleep))
1823 			dev_dbg(dev, "failed to lookup the sleep state\n");
1824 	}
1825 
1826 	pinctrl_init_device_debugfs(pctldev);
1827 
1828 	return pctldev;
1829 
1830 out_err:
1831 	mutex_destroy(&pctldev->mutex);
1832 	kfree(pctldev);
1833 	return ERR_PTR(ret);
1834 }
1835 EXPORT_SYMBOL_GPL(pinctrl_register);
1836 
1837 /**
1838  * pinctrl_unregister() - unregister pinmux
1839  * @pctldev: pin controller to unregister
1840  *
1841  * Called by pinmux drivers to unregister a pinmux.
1842  */
pinctrl_unregister(struct pinctrl_dev * pctldev)1843 void pinctrl_unregister(struct pinctrl_dev *pctldev)
1844 {
1845 	struct pinctrl_gpio_range *range, *n;
1846 	if (pctldev == NULL)
1847 		return;
1848 
1849 	mutex_lock(&pctldev->mutex);
1850 	pinctrl_remove_device_debugfs(pctldev);
1851 	mutex_unlock(&pctldev->mutex);
1852 
1853 	if (!IS_ERR(pctldev->p))
1854 		pinctrl_put(pctldev->p);
1855 
1856 	mutex_lock(&pinctrldev_list_mutex);
1857 	mutex_lock(&pctldev->mutex);
1858 	/* TODO: check that no pinmuxes are still active? */
1859 	list_del(&pctldev->node);
1860 	/* Destroy descriptor tree */
1861 	pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1862 			      pctldev->desc->npins);
1863 	/* remove gpio ranges map */
1864 	list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1865 		list_del(&range->node);
1866 
1867 	mutex_unlock(&pctldev->mutex);
1868 	mutex_destroy(&pctldev->mutex);
1869 	kfree(pctldev);
1870 	mutex_unlock(&pinctrldev_list_mutex);
1871 }
1872 EXPORT_SYMBOL_GPL(pinctrl_unregister);
1873 
pinctrl_init(void)1874 static int __init pinctrl_init(void)
1875 {
1876 	pr_info("initialized pinctrl subsystem\n");
1877 	pinctrl_init_debugfs();
1878 	return 0;
1879 }
1880 
1881 /* init early since many drivers really need to initialized pinmux early */
1882 core_initcall(pinctrl_init);
1883