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