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