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