projects/linux·blob master · refs

2438 lines · 61.4 KiB

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