hpsa: fix bad -ENOMEM return value in hpsa_big_passthru_ioctl
[linux/fpc-iii.git] / drivers / pinctrl / core.c
bloba1ffae4c3770430aee815dc9dbdbdd2d572a663b
1 /*
2 * Core driver for the pin control subsystem
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
8 * Author: Linus Walleij <linus.walleij@linaro.org>
10 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
12 * License terms: GNU General Public License (GPL) version 2
14 #define pr_fmt(fmt) "pinctrl core: " fmt
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>
31 #ifdef CONFIG_GPIOLIB
32 #include <asm-generic/gpio.h>
33 #endif
35 #include "core.h"
36 #include "devicetree.h"
37 #include "pinmux.h"
38 #include "pinconf.h"
41 static bool pinctrl_dummy_state;
43 /* Mutex taken to protect pinctrl_list */
44 static DEFINE_MUTEX(pinctrl_list_mutex);
46 /* Mutex taken to protect pinctrl_maps */
47 DEFINE_MUTEX(pinctrl_maps_mutex);
49 /* Mutex taken to protect pinctrldev_list */
50 static DEFINE_MUTEX(pinctrldev_list_mutex);
52 /* Global list of pin control devices (struct pinctrl_dev) */
53 static LIST_HEAD(pinctrldev_list);
55 /* List of pin controller handles (struct pinctrl) */
56 static LIST_HEAD(pinctrl_list);
58 /* List of pinctrl maps (struct pinctrl_maps) */
59 LIST_HEAD(pinctrl_maps);
62 /**
63 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
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.
70 void pinctrl_provide_dummies(void)
72 pinctrl_dummy_state = true;
75 const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
77 /* We're not allowed to register devices without name */
78 return pctldev->desc->name;
80 EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
82 const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
84 return dev_name(pctldev->dev);
86 EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
88 void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
90 return pctldev->driver_data;
92 EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
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()
98 * Looks up a pin control device matching a certain device name or pure device
99 * pointer, the pure device pointer will take precedence.
101 struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
103 struct pinctrl_dev *pctldev = NULL;
105 if (!devname)
106 return NULL;
108 mutex_lock(&pinctrldev_list_mutex);
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;
118 mutex_unlock(&pinctrldev_list_mutex);
120 return NULL;
123 struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np)
125 struct pinctrl_dev *pctldev;
127 mutex_lock(&pinctrldev_list_mutex);
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;
135 mutex_unlock(&pinctrldev_list_mutex);
137 return NULL;
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
145 int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
147 unsigned i, pin;
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;
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;
160 return -EINVAL;
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
168 const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
170 const struct pin_desc *desc;
172 desc = pin_desc_get(pctldev, pin);
173 if (desc == NULL) {
174 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
175 pin);
176 return NULL;
179 return desc->name;
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
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.
190 bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
192 struct pin_desc *pindesc;
194 if (pin < 0)
195 return false;
197 mutex_lock(&pctldev->mutex);
198 pindesc = pin_desc_get(pctldev, pin);
199 mutex_unlock(&pctldev->mutex);
201 return pindesc != NULL;
203 EXPORT_SYMBOL_GPL(pin_is_valid);
205 /* Deletes a range of pin descriptors */
206 static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
207 const struct pinctrl_pin_desc *pins,
208 unsigned num_pins)
210 int i;
212 for (i = 0; i < num_pins; i++) {
213 struct pin_desc *pindesc;
215 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
216 pins[i].number);
217 if (pindesc != NULL) {
218 radix_tree_delete(&pctldev->pin_desc_tree,
219 pins[i].number);
220 if (pindesc->dynamic_name)
221 kfree(pindesc->name);
223 kfree(pindesc);
227 static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
228 unsigned number, const char *name)
230 struct pin_desc *pindesc;
232 pindesc = pin_desc_get(pctldev, number);
233 if (pindesc != NULL) {
234 pr_err("pin %d already registered on %s\n", number,
235 pctldev->desc->name);
236 return -EINVAL;
239 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
240 if (pindesc == NULL) {
241 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
242 return -ENOMEM;
245 /* Set owner */
246 pindesc->pctldev = pctldev;
248 /* Copy basic pin info */
249 if (name) {
250 pindesc->name = name;
251 } else {
252 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
253 if (pindesc->name == NULL) {
254 kfree(pindesc);
255 return -ENOMEM;
257 pindesc->dynamic_name = true;
260 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
261 pr_debug("registered pin %d (%s) on %s\n",
262 number, pindesc->name, pctldev->desc->name);
263 return 0;
266 static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
267 struct pinctrl_pin_desc const *pins,
268 unsigned num_descs)
270 unsigned i;
271 int ret = 0;
273 for (i = 0; i < num_descs; i++) {
274 ret = pinctrl_register_one_pin(pctldev,
275 pins[i].number, pins[i].name);
276 if (ret)
277 return ret;
280 return 0;
284 * gpio_to_pin() - GPIO range GPIO number to pin number translation
285 * @range: GPIO range used for the translation
286 * @gpio: gpio pin to translate to a pin number
288 * Finds the pin number for a given GPIO using the specified GPIO range
289 * as a base for translation. The distinction between linear GPIO ranges
290 * and pin list based GPIO ranges is managed correctly by this function.
292 * This function assumes the gpio is part of the specified GPIO range, use
293 * only after making sure this is the case (e.g. by calling it on the
294 * result of successful pinctrl_get_device_gpio_range calls)!
296 static inline int gpio_to_pin(struct pinctrl_gpio_range *range,
297 unsigned int gpio)
299 unsigned int offset = gpio - range->base;
300 if (range->pins)
301 return range->pins[offset];
302 else
303 return range->pin_base + offset;
307 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
308 * @pctldev: pin controller device to check
309 * @gpio: gpio pin to check taken from the global GPIO pin space
311 * Tries to match a GPIO pin number to the ranges handled by a certain pin
312 * controller, return the range or NULL
314 static struct pinctrl_gpio_range *
315 pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
317 struct pinctrl_gpio_range *range = NULL;
319 mutex_lock(&pctldev->mutex);
320 /* Loop over the ranges */
321 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
322 /* Check if we're in the valid range */
323 if (gpio >= range->base &&
324 gpio < range->base + range->npins) {
325 mutex_unlock(&pctldev->mutex);
326 return range;
329 mutex_unlock(&pctldev->mutex);
330 return NULL;
334 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
335 * the same GPIO chip are in range
336 * @gpio: gpio pin to check taken from the global GPIO pin space
338 * This function is complement of pinctrl_match_gpio_range(). If the return
339 * value of pinctrl_match_gpio_range() is NULL, this function could be used
340 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
341 * of the same GPIO chip don't have back-end pinctrl interface.
342 * If the return value is true, it means that pinctrl device is ready & the
343 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
344 * is false, it means that pinctrl device may not be ready.
346 #ifdef CONFIG_GPIOLIB
347 static bool pinctrl_ready_for_gpio_range(unsigned gpio)
349 struct pinctrl_dev *pctldev;
350 struct pinctrl_gpio_range *range = NULL;
351 struct gpio_chip *chip = gpio_to_chip(gpio);
353 mutex_lock(&pinctrldev_list_mutex);
355 /* Loop over the pin controllers */
356 list_for_each_entry(pctldev, &pinctrldev_list, node) {
357 /* Loop over the ranges */
358 mutex_lock(&pctldev->mutex);
359 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
360 /* Check if any gpio range overlapped with gpio chip */
361 if (range->base + range->npins - 1 < chip->base ||
362 range->base > chip->base + chip->ngpio - 1)
363 continue;
364 mutex_unlock(&pctldev->mutex);
365 mutex_unlock(&pinctrldev_list_mutex);
366 return true;
368 mutex_unlock(&pctldev->mutex);
371 mutex_unlock(&pinctrldev_list_mutex);
373 return false;
375 #else
376 static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
377 #endif
380 * pinctrl_get_device_gpio_range() - find device for GPIO range
381 * @gpio: the pin to locate the pin controller for
382 * @outdev: the pin control device if found
383 * @outrange: the GPIO range if found
385 * Find the pin controller handling a certain GPIO pin from the pinspace of
386 * the GPIO subsystem, return the device and the matching GPIO range. Returns
387 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
388 * may still have not been registered.
390 static int pinctrl_get_device_gpio_range(unsigned gpio,
391 struct pinctrl_dev **outdev,
392 struct pinctrl_gpio_range **outrange)
394 struct pinctrl_dev *pctldev = NULL;
396 mutex_lock(&pinctrldev_list_mutex);
398 /* Loop over the pin controllers */
399 list_for_each_entry(pctldev, &pinctrldev_list, node) {
400 struct pinctrl_gpio_range *range;
402 range = pinctrl_match_gpio_range(pctldev, gpio);
403 if (range != NULL) {
404 *outdev = pctldev;
405 *outrange = range;
406 mutex_unlock(&pinctrldev_list_mutex);
407 return 0;
411 mutex_unlock(&pinctrldev_list_mutex);
413 return -EPROBE_DEFER;
417 * pinctrl_add_gpio_range() - register a GPIO range for a controller
418 * @pctldev: pin controller device to add the range to
419 * @range: the GPIO range to add
421 * This adds a range of GPIOs to be handled by a certain pin controller. Call
422 * this to register handled ranges after registering your pin controller.
424 void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
425 struct pinctrl_gpio_range *range)
427 mutex_lock(&pctldev->mutex);
428 list_add_tail(&range->node, &pctldev->gpio_ranges);
429 mutex_unlock(&pctldev->mutex);
431 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
433 void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
434 struct pinctrl_gpio_range *ranges,
435 unsigned nranges)
437 int i;
439 for (i = 0; i < nranges; i++)
440 pinctrl_add_gpio_range(pctldev, &ranges[i]);
442 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
444 struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
445 struct pinctrl_gpio_range *range)
447 struct pinctrl_dev *pctldev;
449 pctldev = get_pinctrl_dev_from_devname(devname);
452 * If we can't find this device, let's assume that is because
453 * it has not probed yet, so the driver trying to register this
454 * range need to defer probing.
456 if (!pctldev) {
457 return ERR_PTR(-EPROBE_DEFER);
459 pinctrl_add_gpio_range(pctldev, range);
461 return pctldev;
463 EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
466 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
467 * @pctldev: the pin controller device to look in
468 * @pin: a controller-local number to find the range for
470 struct pinctrl_gpio_range *
471 pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
472 unsigned int pin)
474 struct pinctrl_gpio_range *range;
476 mutex_lock(&pctldev->mutex);
477 /* Loop over the ranges */
478 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
479 /* Check if we're in the valid range */
480 if (range->pins) {
481 int a;
482 for (a = 0; a < range->npins; a++) {
483 if (range->pins[a] == pin)
484 goto out;
486 } else if (pin >= range->pin_base &&
487 pin < range->pin_base + range->npins)
488 goto out;
490 range = NULL;
491 out:
492 mutex_unlock(&pctldev->mutex);
493 return range;
495 EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
498 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
499 * @pctldev: pin controller device to remove the range from
500 * @range: the GPIO range to remove
502 void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
503 struct pinctrl_gpio_range *range)
505 mutex_lock(&pctldev->mutex);
506 list_del(&range->node);
507 mutex_unlock(&pctldev->mutex);
509 EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
512 * pinctrl_get_group_selector() - returns the group selector for a group
513 * @pctldev: the pin controller handling the group
514 * @pin_group: the pin group to look up
516 int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
517 const char *pin_group)
519 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
520 unsigned ngroups = pctlops->get_groups_count(pctldev);
521 unsigned group_selector = 0;
523 while (group_selector < ngroups) {
524 const char *gname = pctlops->get_group_name(pctldev,
525 group_selector);
526 if (!strcmp(gname, pin_group)) {
527 dev_dbg(pctldev->dev,
528 "found group selector %u for %s\n",
529 group_selector,
530 pin_group);
531 return group_selector;
534 group_selector++;
537 dev_err(pctldev->dev, "does not have pin group %s\n",
538 pin_group);
540 return -EINVAL;
544 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
545 * @gpio: the GPIO pin number from the GPIO subsystem number space
547 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
548 * as part of their gpio_request() semantics, platforms and individual drivers
549 * shall *NOT* request GPIO pins to be muxed in.
551 int pinctrl_request_gpio(unsigned gpio)
553 struct pinctrl_dev *pctldev;
554 struct pinctrl_gpio_range *range;
555 int ret;
556 int pin;
558 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
559 if (ret) {
560 if (pinctrl_ready_for_gpio_range(gpio))
561 ret = 0;
562 return ret;
565 mutex_lock(&pctldev->mutex);
567 /* Convert to the pin controllers number space */
568 pin = gpio_to_pin(range, gpio);
570 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
572 mutex_unlock(&pctldev->mutex);
574 return ret;
576 EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
579 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
580 * @gpio: the GPIO pin number from the GPIO subsystem number space
582 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
583 * as part of their gpio_free() semantics, platforms and individual drivers
584 * shall *NOT* request GPIO pins to be muxed out.
586 void pinctrl_free_gpio(unsigned gpio)
588 struct pinctrl_dev *pctldev;
589 struct pinctrl_gpio_range *range;
590 int ret;
591 int pin;
593 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
594 if (ret) {
595 return;
597 mutex_lock(&pctldev->mutex);
599 /* Convert to the pin controllers number space */
600 pin = gpio_to_pin(range, gpio);
602 pinmux_free_gpio(pctldev, pin, range);
604 mutex_unlock(&pctldev->mutex);
606 EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
608 static int pinctrl_gpio_direction(unsigned gpio, bool input)
610 struct pinctrl_dev *pctldev;
611 struct pinctrl_gpio_range *range;
612 int ret;
613 int pin;
615 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
616 if (ret) {
617 return ret;
620 mutex_lock(&pctldev->mutex);
622 /* Convert to the pin controllers number space */
623 pin = gpio_to_pin(range, gpio);
624 ret = pinmux_gpio_direction(pctldev, range, pin, input);
626 mutex_unlock(&pctldev->mutex);
628 return ret;
632 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
633 * @gpio: the GPIO pin number from the GPIO subsystem number space
635 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
636 * as part of their gpio_direction_input() semantics, platforms and individual
637 * drivers shall *NOT* touch pin control GPIO calls.
639 int pinctrl_gpio_direction_input(unsigned gpio)
641 return pinctrl_gpio_direction(gpio, true);
643 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
646 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
647 * @gpio: the GPIO pin number from the GPIO subsystem number space
649 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
650 * as part of their gpio_direction_output() semantics, platforms and individual
651 * drivers shall *NOT* touch pin control GPIO calls.
653 int pinctrl_gpio_direction_output(unsigned gpio)
655 return pinctrl_gpio_direction(gpio, false);
657 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
659 static struct pinctrl_state *find_state(struct pinctrl *p,
660 const char *name)
662 struct pinctrl_state *state;
664 list_for_each_entry(state, &p->states, node)
665 if (!strcmp(state->name, name))
666 return state;
668 return NULL;
671 static struct pinctrl_state *create_state(struct pinctrl *p,
672 const char *name)
674 struct pinctrl_state *state;
676 state = kzalloc(sizeof(*state), GFP_KERNEL);
677 if (state == NULL) {
678 dev_err(p->dev,
679 "failed to alloc struct pinctrl_state\n");
680 return ERR_PTR(-ENOMEM);
683 state->name = name;
684 INIT_LIST_HEAD(&state->settings);
686 list_add_tail(&state->node, &p->states);
688 return state;
691 static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
693 struct pinctrl_state *state;
694 struct pinctrl_setting *setting;
695 int ret;
697 state = find_state(p, map->name);
698 if (!state)
699 state = create_state(p, map->name);
700 if (IS_ERR(state))
701 return PTR_ERR(state);
703 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
704 return 0;
706 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
707 if (setting == NULL) {
708 dev_err(p->dev,
709 "failed to alloc struct pinctrl_setting\n");
710 return -ENOMEM;
713 setting->type = map->type;
715 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
716 if (setting->pctldev == NULL) {
717 kfree(setting);
718 /* Do not defer probing of hogs (circular loop) */
719 if (!strcmp(map->ctrl_dev_name, map->dev_name))
720 return -ENODEV;
722 * OK let us guess that the driver is not there yet, and
723 * let's defer obtaining this pinctrl handle to later...
725 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
726 map->ctrl_dev_name);
727 return -EPROBE_DEFER;
730 setting->dev_name = map->dev_name;
732 switch (map->type) {
733 case PIN_MAP_TYPE_MUX_GROUP:
734 ret = pinmux_map_to_setting(map, setting);
735 break;
736 case PIN_MAP_TYPE_CONFIGS_PIN:
737 case PIN_MAP_TYPE_CONFIGS_GROUP:
738 ret = pinconf_map_to_setting(map, setting);
739 break;
740 default:
741 ret = -EINVAL;
742 break;
744 if (ret < 0) {
745 kfree(setting);
746 return ret;
749 list_add_tail(&setting->node, &state->settings);
751 return 0;
754 static struct pinctrl *find_pinctrl(struct device *dev)
756 struct pinctrl *p;
758 mutex_lock(&pinctrl_list_mutex);
759 list_for_each_entry(p, &pinctrl_list, node)
760 if (p->dev == dev) {
761 mutex_unlock(&pinctrl_list_mutex);
762 return p;
765 mutex_unlock(&pinctrl_list_mutex);
766 return NULL;
769 static void pinctrl_free(struct pinctrl *p, bool inlist);
771 static struct pinctrl *create_pinctrl(struct device *dev)
773 struct pinctrl *p;
774 const char *devname;
775 struct pinctrl_maps *maps_node;
776 int i;
777 struct pinctrl_map const *map;
778 int ret;
781 * create the state cookie holder struct pinctrl for each
782 * mapping, this is what consumers will get when requesting
783 * a pin control handle with pinctrl_get()
785 p = kzalloc(sizeof(*p), GFP_KERNEL);
786 if (p == NULL) {
787 dev_err(dev, "failed to alloc struct pinctrl\n");
788 return ERR_PTR(-ENOMEM);
790 p->dev = dev;
791 INIT_LIST_HEAD(&p->states);
792 INIT_LIST_HEAD(&p->dt_maps);
794 ret = pinctrl_dt_to_map(p);
795 if (ret < 0) {
796 kfree(p);
797 return ERR_PTR(ret);
800 devname = dev_name(dev);
802 mutex_lock(&pinctrl_maps_mutex);
803 /* Iterate over the pin control maps to locate the right ones */
804 for_each_maps(maps_node, i, map) {
805 /* Map must be for this device */
806 if (strcmp(map->dev_name, devname))
807 continue;
809 ret = add_setting(p, map);
811 * At this point the adding of a setting may:
813 * - Defer, if the pinctrl device is not yet available
814 * - Fail, if the pinctrl device is not yet available,
815 * AND the setting is a hog. We cannot defer that, since
816 * the hog will kick in immediately after the device
817 * is registered.
819 * If the error returned was not -EPROBE_DEFER then we
820 * accumulate the errors to see if we end up with
821 * an -EPROBE_DEFER later, as that is the worst case.
823 if (ret == -EPROBE_DEFER) {
824 pinctrl_free(p, false);
825 mutex_unlock(&pinctrl_maps_mutex);
826 return ERR_PTR(ret);
829 mutex_unlock(&pinctrl_maps_mutex);
831 if (ret < 0) {
832 /* If some other error than deferral occured, return here */
833 pinctrl_free(p, false);
834 return ERR_PTR(ret);
837 kref_init(&p->users);
839 /* Add the pinctrl handle to the global list */
840 mutex_lock(&pinctrl_list_mutex);
841 list_add_tail(&p->node, &pinctrl_list);
842 mutex_unlock(&pinctrl_list_mutex);
844 return p;
848 * pinctrl_get() - retrieves the pinctrl handle for a device
849 * @dev: the device to obtain the handle for
851 struct pinctrl *pinctrl_get(struct device *dev)
853 struct pinctrl *p;
855 if (WARN_ON(!dev))
856 return ERR_PTR(-EINVAL);
859 * See if somebody else (such as the device core) has already
860 * obtained a handle to the pinctrl for this device. In that case,
861 * return another pointer to it.
863 p = find_pinctrl(dev);
864 if (p != NULL) {
865 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
866 kref_get(&p->users);
867 return p;
870 return create_pinctrl(dev);
872 EXPORT_SYMBOL_GPL(pinctrl_get);
874 static void pinctrl_free_setting(bool disable_setting,
875 struct pinctrl_setting *setting)
877 switch (setting->type) {
878 case PIN_MAP_TYPE_MUX_GROUP:
879 if (disable_setting)
880 pinmux_disable_setting(setting);
881 pinmux_free_setting(setting);
882 break;
883 case PIN_MAP_TYPE_CONFIGS_PIN:
884 case PIN_MAP_TYPE_CONFIGS_GROUP:
885 pinconf_free_setting(setting);
886 break;
887 default:
888 break;
892 static void pinctrl_free(struct pinctrl *p, bool inlist)
894 struct pinctrl_state *state, *n1;
895 struct pinctrl_setting *setting, *n2;
897 mutex_lock(&pinctrl_list_mutex);
898 list_for_each_entry_safe(state, n1, &p->states, node) {
899 list_for_each_entry_safe(setting, n2, &state->settings, node) {
900 pinctrl_free_setting(state == p->state, setting);
901 list_del(&setting->node);
902 kfree(setting);
904 list_del(&state->node);
905 kfree(state);
908 pinctrl_dt_free_maps(p);
910 if (inlist)
911 list_del(&p->node);
912 kfree(p);
913 mutex_unlock(&pinctrl_list_mutex);
917 * pinctrl_release() - release the pinctrl handle
918 * @kref: the kref in the pinctrl being released
920 static void pinctrl_release(struct kref *kref)
922 struct pinctrl *p = container_of(kref, struct pinctrl, users);
924 pinctrl_free(p, true);
928 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
929 * @p: the pinctrl handle to release
931 void pinctrl_put(struct pinctrl *p)
933 kref_put(&p->users, pinctrl_release);
935 EXPORT_SYMBOL_GPL(pinctrl_put);
938 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
939 * @p: the pinctrl handle to retrieve the state from
940 * @name: the state name to retrieve
942 struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
943 const char *name)
945 struct pinctrl_state *state;
947 state = find_state(p, name);
948 if (!state) {
949 if (pinctrl_dummy_state) {
950 /* create dummy state */
951 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
952 name);
953 state = create_state(p, name);
954 } else
955 state = ERR_PTR(-ENODEV);
958 return state;
960 EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
963 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
964 * @p: the pinctrl handle for the device that requests configuration
965 * @state: the state handle to select/activate/program
967 int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
969 struct pinctrl_setting *setting, *setting2;
970 struct pinctrl_state *old_state = p->state;
971 int ret;
973 if (p->state == state)
974 return 0;
976 if (p->state) {
978 * The set of groups with a mux configuration in the old state
979 * may not be identical to the set of groups with a mux setting
980 * in the new state. While this might be unusual, it's entirely
981 * possible for the "user"-supplied mapping table to be written
982 * that way. For each group that was configured in the old state
983 * but not in the new state, this code puts that group into a
984 * safe/disabled state.
986 list_for_each_entry(setting, &p->state->settings, node) {
987 bool found = false;
988 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
989 continue;
990 list_for_each_entry(setting2, &state->settings, node) {
991 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
992 continue;
993 if (setting2->data.mux.group ==
994 setting->data.mux.group) {
995 found = true;
996 break;
999 if (!found)
1000 pinmux_disable_setting(setting);
1004 p->state = NULL;
1006 /* Apply all the settings for the new state */
1007 list_for_each_entry(setting, &state->settings, node) {
1008 switch (setting->type) {
1009 case PIN_MAP_TYPE_MUX_GROUP:
1010 ret = pinmux_enable_setting(setting);
1011 break;
1012 case PIN_MAP_TYPE_CONFIGS_PIN:
1013 case PIN_MAP_TYPE_CONFIGS_GROUP:
1014 ret = pinconf_apply_setting(setting);
1015 break;
1016 default:
1017 ret = -EINVAL;
1018 break;
1021 if (ret < 0) {
1022 goto unapply_new_state;
1026 p->state = state;
1028 return 0;
1030 unapply_new_state:
1031 dev_err(p->dev, "Error applying setting, reverse things back\n");
1033 list_for_each_entry(setting2, &state->settings, node) {
1034 if (&setting2->node == &setting->node)
1035 break;
1037 * All we can do here is pinmux_disable_setting.
1038 * That means that some pins are muxed differently now
1039 * than they were before applying the setting (We can't
1040 * "unmux a pin"!), but it's not a big deal since the pins
1041 * are free to be muxed by another apply_setting.
1043 if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
1044 pinmux_disable_setting(setting2);
1047 /* There's no infinite recursive loop here because p->state is NULL */
1048 if (old_state)
1049 pinctrl_select_state(p, old_state);
1051 return ret;
1053 EXPORT_SYMBOL_GPL(pinctrl_select_state);
1055 static void devm_pinctrl_release(struct device *dev, void *res)
1057 pinctrl_put(*(struct pinctrl **)res);
1061 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
1062 * @dev: the device to obtain the handle for
1064 * If there is a need to explicitly destroy the returned struct pinctrl,
1065 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1067 struct pinctrl *devm_pinctrl_get(struct device *dev)
1069 struct pinctrl **ptr, *p;
1071 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
1072 if (!ptr)
1073 return ERR_PTR(-ENOMEM);
1075 p = pinctrl_get(dev);
1076 if (!IS_ERR(p)) {
1077 *ptr = p;
1078 devres_add(dev, ptr);
1079 } else {
1080 devres_free(ptr);
1083 return p;
1085 EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1087 static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1089 struct pinctrl **p = res;
1091 return *p == data;
1095 * devm_pinctrl_put() - Resource managed pinctrl_put()
1096 * @p: the pinctrl handle to release
1098 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1099 * this function will not need to be called and the resource management
1100 * code will ensure that the resource is freed.
1102 void devm_pinctrl_put(struct pinctrl *p)
1104 WARN_ON(devres_release(p->dev, devm_pinctrl_release,
1105 devm_pinctrl_match, p));
1107 EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1109 int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
1110 bool dup, bool locked)
1112 int i, ret;
1113 struct pinctrl_maps *maps_node;
1115 pr_debug("add %d pinmux maps\n", num_maps);
1117 /* First sanity check the new mapping */
1118 for (i = 0; i < num_maps; i++) {
1119 if (!maps[i].dev_name) {
1120 pr_err("failed to register map %s (%d): no device given\n",
1121 maps[i].name, i);
1122 return -EINVAL;
1125 if (!maps[i].name) {
1126 pr_err("failed to register map %d: no map name given\n",
1128 return -EINVAL;
1131 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1132 !maps[i].ctrl_dev_name) {
1133 pr_err("failed to register map %s (%d): no pin control device given\n",
1134 maps[i].name, i);
1135 return -EINVAL;
1138 switch (maps[i].type) {
1139 case PIN_MAP_TYPE_DUMMY_STATE:
1140 break;
1141 case PIN_MAP_TYPE_MUX_GROUP:
1142 ret = pinmux_validate_map(&maps[i], i);
1143 if (ret < 0)
1144 return ret;
1145 break;
1146 case PIN_MAP_TYPE_CONFIGS_PIN:
1147 case PIN_MAP_TYPE_CONFIGS_GROUP:
1148 ret = pinconf_validate_map(&maps[i], i);
1149 if (ret < 0)
1150 return ret;
1151 break;
1152 default:
1153 pr_err("failed to register map %s (%d): invalid type given\n",
1154 maps[i].name, i);
1155 return -EINVAL;
1159 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
1160 if (!maps_node) {
1161 pr_err("failed to alloc struct pinctrl_maps\n");
1162 return -ENOMEM;
1165 maps_node->num_maps = num_maps;
1166 if (dup) {
1167 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
1168 GFP_KERNEL);
1169 if (!maps_node->maps) {
1170 pr_err("failed to duplicate mapping table\n");
1171 kfree(maps_node);
1172 return -ENOMEM;
1174 } else {
1175 maps_node->maps = maps;
1178 if (!locked)
1179 mutex_lock(&pinctrl_maps_mutex);
1180 list_add_tail(&maps_node->node, &pinctrl_maps);
1181 if (!locked)
1182 mutex_unlock(&pinctrl_maps_mutex);
1184 return 0;
1188 * pinctrl_register_mappings() - register a set of pin controller mappings
1189 * @maps: the pincontrol mappings table to register. This should probably be
1190 * marked with __initdata so it can be discarded after boot. This
1191 * function will perform a shallow copy for the mapping entries.
1192 * @num_maps: the number of maps in the mapping table
1194 int pinctrl_register_mappings(struct pinctrl_map const *maps,
1195 unsigned num_maps)
1197 return pinctrl_register_map(maps, num_maps, true, false);
1200 void pinctrl_unregister_map(struct pinctrl_map const *map)
1202 struct pinctrl_maps *maps_node;
1204 mutex_lock(&pinctrl_maps_mutex);
1205 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1206 if (maps_node->maps == map) {
1207 list_del(&maps_node->node);
1208 kfree(maps_node);
1209 mutex_unlock(&pinctrl_maps_mutex);
1210 return;
1213 mutex_unlock(&pinctrl_maps_mutex);
1217 * pinctrl_force_sleep() - turn a given controller device into sleep state
1218 * @pctldev: pin controller device
1220 int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1222 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
1223 return pinctrl_select_state(pctldev->p, pctldev->hog_sleep);
1224 return 0;
1226 EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1229 * pinctrl_force_default() - turn a given controller device into default state
1230 * @pctldev: pin controller device
1232 int pinctrl_force_default(struct pinctrl_dev *pctldev)
1234 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
1235 return pinctrl_select_state(pctldev->p, pctldev->hog_default);
1236 return 0;
1238 EXPORT_SYMBOL_GPL(pinctrl_force_default);
1240 #ifdef CONFIG_PM
1243 * pinctrl_pm_select_state() - select pinctrl state for PM
1244 * @dev: device to select default state for
1245 * @state: state to set
1247 static int pinctrl_pm_select_state(struct device *dev,
1248 struct pinctrl_state *state)
1250 struct dev_pin_info *pins = dev->pins;
1251 int ret;
1253 if (IS_ERR(state))
1254 return 0; /* No such state */
1255 ret = pinctrl_select_state(pins->p, state);
1256 if (ret)
1257 dev_err(dev, "failed to activate pinctrl state %s\n",
1258 state->name);
1259 return ret;
1263 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1264 * @dev: device to select default state for
1266 int pinctrl_pm_select_default_state(struct device *dev)
1268 if (!dev->pins)
1269 return 0;
1271 return pinctrl_pm_select_state(dev, dev->pins->default_state);
1273 EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
1276 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1277 * @dev: device to select sleep state for
1279 int pinctrl_pm_select_sleep_state(struct device *dev)
1281 if (!dev->pins)
1282 return 0;
1284 return pinctrl_pm_select_state(dev, dev->pins->sleep_state);
1286 EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
1289 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1290 * @dev: device to select idle state for
1292 int pinctrl_pm_select_idle_state(struct device *dev)
1294 if (!dev->pins)
1295 return 0;
1297 return pinctrl_pm_select_state(dev, dev->pins->idle_state);
1299 EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
1300 #endif
1302 #ifdef CONFIG_DEBUG_FS
1304 static int pinctrl_pins_show(struct seq_file *s, void *what)
1306 struct pinctrl_dev *pctldev = s->private;
1307 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1308 unsigned i, pin;
1310 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
1312 mutex_lock(&pctldev->mutex);
1314 /* The pin number can be retrived from the pin controller descriptor */
1315 for (i = 0; i < pctldev->desc->npins; i++) {
1316 struct pin_desc *desc;
1318 pin = pctldev->desc->pins[i].number;
1319 desc = pin_desc_get(pctldev, pin);
1320 /* Pin space may be sparse */
1321 if (desc == NULL)
1322 continue;
1324 seq_printf(s, "pin %d (%s) ", pin,
1325 desc->name ? desc->name : "unnamed");
1327 /* Driver-specific info per pin */
1328 if (ops->pin_dbg_show)
1329 ops->pin_dbg_show(pctldev, s, pin);
1331 seq_puts(s, "\n");
1334 mutex_unlock(&pctldev->mutex);
1336 return 0;
1339 static int pinctrl_groups_show(struct seq_file *s, void *what)
1341 struct pinctrl_dev *pctldev = s->private;
1342 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1343 unsigned ngroups, selector = 0;
1345 mutex_lock(&pctldev->mutex);
1347 ngroups = ops->get_groups_count(pctldev);
1349 seq_puts(s, "registered pin groups:\n");
1350 while (selector < ngroups) {
1351 const unsigned *pins;
1352 unsigned num_pins;
1353 const char *gname = ops->get_group_name(pctldev, selector);
1354 const char *pname;
1355 int ret;
1356 int i;
1358 ret = ops->get_group_pins(pctldev, selector,
1359 &pins, &num_pins);
1360 if (ret)
1361 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1362 gname);
1363 else {
1364 seq_printf(s, "group: %s\n", gname);
1365 for (i = 0; i < num_pins; i++) {
1366 pname = pin_get_name(pctldev, pins[i]);
1367 if (WARN_ON(!pname)) {
1368 mutex_unlock(&pctldev->mutex);
1369 return -EINVAL;
1371 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1373 seq_puts(s, "\n");
1375 selector++;
1378 mutex_unlock(&pctldev->mutex);
1380 return 0;
1383 static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1385 struct pinctrl_dev *pctldev = s->private;
1386 struct pinctrl_gpio_range *range = NULL;
1388 seq_puts(s, "GPIO ranges handled:\n");
1390 mutex_lock(&pctldev->mutex);
1392 /* Loop over the ranges */
1393 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
1394 if (range->pins) {
1395 int a;
1396 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
1397 range->id, range->name,
1398 range->base, (range->base + range->npins - 1));
1399 for (a = 0; a < range->npins - 1; a++)
1400 seq_printf(s, "%u, ", range->pins[a]);
1401 seq_printf(s, "%u}\n", range->pins[a]);
1403 else
1404 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1405 range->id, range->name,
1406 range->base, (range->base + range->npins - 1),
1407 range->pin_base,
1408 (range->pin_base + range->npins - 1));
1411 mutex_unlock(&pctldev->mutex);
1413 return 0;
1416 static int pinctrl_devices_show(struct seq_file *s, void *what)
1418 struct pinctrl_dev *pctldev;
1420 seq_puts(s, "name [pinmux] [pinconf]\n");
1422 mutex_lock(&pinctrldev_list_mutex);
1424 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1425 seq_printf(s, "%s ", pctldev->desc->name);
1426 if (pctldev->desc->pmxops)
1427 seq_puts(s, "yes ");
1428 else
1429 seq_puts(s, "no ");
1430 if (pctldev->desc->confops)
1431 seq_puts(s, "yes");
1432 else
1433 seq_puts(s, "no");
1434 seq_puts(s, "\n");
1437 mutex_unlock(&pinctrldev_list_mutex);
1439 return 0;
1442 static inline const char *map_type(enum pinctrl_map_type type)
1444 static const char * const names[] = {
1445 "INVALID",
1446 "DUMMY_STATE",
1447 "MUX_GROUP",
1448 "CONFIGS_PIN",
1449 "CONFIGS_GROUP",
1452 if (type >= ARRAY_SIZE(names))
1453 return "UNKNOWN";
1455 return names[type];
1458 static int pinctrl_maps_show(struct seq_file *s, void *what)
1460 struct pinctrl_maps *maps_node;
1461 int i;
1462 struct pinctrl_map const *map;
1464 seq_puts(s, "Pinctrl maps:\n");
1466 mutex_lock(&pinctrl_maps_mutex);
1467 for_each_maps(maps_node, i, map) {
1468 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1469 map->dev_name, map->name, map_type(map->type),
1470 map->type);
1472 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1473 seq_printf(s, "controlling device %s\n",
1474 map->ctrl_dev_name);
1476 switch (map->type) {
1477 case PIN_MAP_TYPE_MUX_GROUP:
1478 pinmux_show_map(s, map);
1479 break;
1480 case PIN_MAP_TYPE_CONFIGS_PIN:
1481 case PIN_MAP_TYPE_CONFIGS_GROUP:
1482 pinconf_show_map(s, map);
1483 break;
1484 default:
1485 break;
1488 seq_printf(s, "\n");
1490 mutex_unlock(&pinctrl_maps_mutex);
1492 return 0;
1495 static int pinctrl_show(struct seq_file *s, void *what)
1497 struct pinctrl *p;
1498 struct pinctrl_state *state;
1499 struct pinctrl_setting *setting;
1501 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
1503 mutex_lock(&pinctrl_list_mutex);
1505 list_for_each_entry(p, &pinctrl_list, node) {
1506 seq_printf(s, "device: %s current state: %s\n",
1507 dev_name(p->dev),
1508 p->state ? p->state->name : "none");
1510 list_for_each_entry(state, &p->states, node) {
1511 seq_printf(s, " state: %s\n", state->name);
1513 list_for_each_entry(setting, &state->settings, node) {
1514 struct pinctrl_dev *pctldev = setting->pctldev;
1516 seq_printf(s, " type: %s controller %s ",
1517 map_type(setting->type),
1518 pinctrl_dev_get_name(pctldev));
1520 switch (setting->type) {
1521 case PIN_MAP_TYPE_MUX_GROUP:
1522 pinmux_show_setting(s, setting);
1523 break;
1524 case PIN_MAP_TYPE_CONFIGS_PIN:
1525 case PIN_MAP_TYPE_CONFIGS_GROUP:
1526 pinconf_show_setting(s, setting);
1527 break;
1528 default:
1529 break;
1535 mutex_unlock(&pinctrl_list_mutex);
1537 return 0;
1540 static int pinctrl_pins_open(struct inode *inode, struct file *file)
1542 return single_open(file, pinctrl_pins_show, inode->i_private);
1545 static int pinctrl_groups_open(struct inode *inode, struct file *file)
1547 return single_open(file, pinctrl_groups_show, inode->i_private);
1550 static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1552 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1555 static int pinctrl_devices_open(struct inode *inode, struct file *file)
1557 return single_open(file, pinctrl_devices_show, NULL);
1560 static int pinctrl_maps_open(struct inode *inode, struct file *file)
1562 return single_open(file, pinctrl_maps_show, NULL);
1565 static int pinctrl_open(struct inode *inode, struct file *file)
1567 return single_open(file, pinctrl_show, NULL);
1570 static const struct file_operations pinctrl_pins_ops = {
1571 .open = pinctrl_pins_open,
1572 .read = seq_read,
1573 .llseek = seq_lseek,
1574 .release = single_release,
1577 static const struct file_operations pinctrl_groups_ops = {
1578 .open = pinctrl_groups_open,
1579 .read = seq_read,
1580 .llseek = seq_lseek,
1581 .release = single_release,
1584 static const struct file_operations pinctrl_gpioranges_ops = {
1585 .open = pinctrl_gpioranges_open,
1586 .read = seq_read,
1587 .llseek = seq_lseek,
1588 .release = single_release,
1591 static const struct file_operations pinctrl_devices_ops = {
1592 .open = pinctrl_devices_open,
1593 .read = seq_read,
1594 .llseek = seq_lseek,
1595 .release = single_release,
1598 static const struct file_operations pinctrl_maps_ops = {
1599 .open = pinctrl_maps_open,
1600 .read = seq_read,
1601 .llseek = seq_lseek,
1602 .release = single_release,
1605 static const struct file_operations pinctrl_ops = {
1606 .open = pinctrl_open,
1607 .read = seq_read,
1608 .llseek = seq_lseek,
1609 .release = single_release,
1612 static struct dentry *debugfs_root;
1614 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1616 struct dentry *device_root;
1618 device_root = debugfs_create_dir(dev_name(pctldev->dev),
1619 debugfs_root);
1620 pctldev->device_root = device_root;
1622 if (IS_ERR(device_root) || !device_root) {
1623 pr_warn("failed to create debugfs directory for %s\n",
1624 dev_name(pctldev->dev));
1625 return;
1627 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1628 device_root, pctldev, &pinctrl_pins_ops);
1629 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1630 device_root, pctldev, &pinctrl_groups_ops);
1631 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1632 device_root, pctldev, &pinctrl_gpioranges_ops);
1633 pinmux_init_device_debugfs(device_root, pctldev);
1634 pinconf_init_device_debugfs(device_root, pctldev);
1637 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1639 debugfs_remove_recursive(pctldev->device_root);
1642 static void pinctrl_init_debugfs(void)
1644 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1645 if (IS_ERR(debugfs_root) || !debugfs_root) {
1646 pr_warn("failed to create debugfs directory\n");
1647 debugfs_root = NULL;
1648 return;
1651 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1652 debugfs_root, NULL, &pinctrl_devices_ops);
1653 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1654 debugfs_root, NULL, &pinctrl_maps_ops);
1655 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1656 debugfs_root, NULL, &pinctrl_ops);
1659 #else /* CONFIG_DEBUG_FS */
1661 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1665 static void pinctrl_init_debugfs(void)
1669 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1673 #endif
1675 static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1677 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1679 if (!ops ||
1680 !ops->get_groups_count ||
1681 !ops->get_group_name ||
1682 !ops->get_group_pins)
1683 return -EINVAL;
1685 if (ops->dt_node_to_map && !ops->dt_free_map)
1686 return -EINVAL;
1688 return 0;
1692 * pinctrl_register() - register a pin controller device
1693 * @pctldesc: descriptor for this pin controller
1694 * @dev: parent device for this pin controller
1695 * @driver_data: private pin controller data for this pin controller
1697 struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1698 struct device *dev, void *driver_data)
1700 struct pinctrl_dev *pctldev;
1701 int ret;
1703 if (!pctldesc)
1704 return NULL;
1705 if (!pctldesc->name)
1706 return NULL;
1708 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
1709 if (pctldev == NULL) {
1710 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
1711 return NULL;
1714 /* Initialize pin control device struct */
1715 pctldev->owner = pctldesc->owner;
1716 pctldev->desc = pctldesc;
1717 pctldev->driver_data = driver_data;
1718 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
1719 INIT_LIST_HEAD(&pctldev->gpio_ranges);
1720 pctldev->dev = dev;
1721 mutex_init(&pctldev->mutex);
1723 /* check core ops for sanity */
1724 if (pinctrl_check_ops(pctldev)) {
1725 dev_err(dev, "pinctrl ops lacks necessary functions\n");
1726 goto out_err;
1729 /* If we're implementing pinmuxing, check the ops for sanity */
1730 if (pctldesc->pmxops) {
1731 if (pinmux_check_ops(pctldev))
1732 goto out_err;
1735 /* If we're implementing pinconfig, check the ops for sanity */
1736 if (pctldesc->confops) {
1737 if (pinconf_check_ops(pctldev))
1738 goto out_err;
1741 /* Register all the pins */
1742 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
1743 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1744 if (ret) {
1745 dev_err(dev, "error during pin registration\n");
1746 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1747 pctldesc->npins);
1748 goto out_err;
1751 mutex_lock(&pinctrldev_list_mutex);
1752 list_add_tail(&pctldev->node, &pinctrldev_list);
1753 mutex_unlock(&pinctrldev_list_mutex);
1755 pctldev->p = pinctrl_get(pctldev->dev);
1757 if (!IS_ERR(pctldev->p)) {
1758 pctldev->hog_default =
1759 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
1760 if (IS_ERR(pctldev->hog_default)) {
1761 dev_dbg(dev, "failed to lookup the default state\n");
1762 } else {
1763 if (pinctrl_select_state(pctldev->p,
1764 pctldev->hog_default))
1765 dev_err(dev,
1766 "failed to select default state\n");
1769 pctldev->hog_sleep =
1770 pinctrl_lookup_state(pctldev->p,
1771 PINCTRL_STATE_SLEEP);
1772 if (IS_ERR(pctldev->hog_sleep))
1773 dev_dbg(dev, "failed to lookup the sleep state\n");
1776 pinctrl_init_device_debugfs(pctldev);
1778 return pctldev;
1780 out_err:
1781 mutex_destroy(&pctldev->mutex);
1782 kfree(pctldev);
1783 return NULL;
1785 EXPORT_SYMBOL_GPL(pinctrl_register);
1788 * pinctrl_unregister() - unregister pinmux
1789 * @pctldev: pin controller to unregister
1791 * Called by pinmux drivers to unregister a pinmux.
1793 void pinctrl_unregister(struct pinctrl_dev *pctldev)
1795 struct pinctrl_gpio_range *range, *n;
1796 if (pctldev == NULL)
1797 return;
1799 mutex_lock(&pinctrldev_list_mutex);
1800 mutex_lock(&pctldev->mutex);
1802 pinctrl_remove_device_debugfs(pctldev);
1804 if (!IS_ERR(pctldev->p))
1805 pinctrl_put(pctldev->p);
1807 /* TODO: check that no pinmuxes are still active? */
1808 list_del(&pctldev->node);
1809 /* Destroy descriptor tree */
1810 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1811 pctldev->desc->npins);
1812 /* remove gpio ranges map */
1813 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1814 list_del(&range->node);
1816 mutex_unlock(&pctldev->mutex);
1817 mutex_destroy(&pctldev->mutex);
1818 kfree(pctldev);
1819 mutex_unlock(&pinctrldev_list_mutex);
1821 EXPORT_SYMBOL_GPL(pinctrl_unregister);
1823 static int __init pinctrl_init(void)
1825 pr_info("initialized pinctrl subsystem\n");
1826 pinctrl_init_debugfs();
1827 return 0;
1830 /* init early since many drivers really need to initialized pinmux early */
1831 core_initcall(pinctrl_init);