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