pinctrl: support gpio request deferred probing
[linux-2.6/btrfs-unstable.git] / drivers / pinctrl / core.c
blobc3b331b74fa0a5702300745b0d6423a19d88d30a
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/export.h>
18 #include <linux/init.h>
19 #include <linux/device.h>
20 #include <linux/slab.h>
21 #include <linux/err.h>
22 #include <linux/list.h>
23 #include <linux/sysfs.h>
24 #include <linux/debugfs.h>
25 #include <linux/seq_file.h>
26 #include <linux/pinctrl/consumer.h>
27 #include <linux/pinctrl/pinctrl.h>
28 #include <linux/pinctrl/machine.h>
29 #include "core.h"
30 #include "devicetree.h"
31 #include "pinmux.h"
32 #include "pinconf.h"
34 /**
35 * struct pinctrl_maps - a list item containing part of the mapping table
36 * @node: mapping table list node
37 * @maps: array of mapping table entries
38 * @num_maps: the number of entries in @maps
40 struct pinctrl_maps {
41 struct list_head node;
42 struct pinctrl_map const *maps;
43 unsigned num_maps;
46 static bool pinctrl_dummy_state;
48 /* Mutex taken by all entry points */
49 DEFINE_MUTEX(pinctrl_mutex);
51 /* Global list of pin control devices (struct pinctrl_dev) */
52 LIST_HEAD(pinctrldev_list);
54 /* List of pin controller handles (struct pinctrl) */
55 static LIST_HEAD(pinctrl_list);
57 /* List of pinctrl maps (struct pinctrl_maps) */
58 static LIST_HEAD(pinctrl_maps);
60 #define for_each_maps(_maps_node_, _i_, _map_) \
61 list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
62 for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
63 _i_ < _maps_node_->num_maps; \
64 i++, _map_ = &_maps_node_->maps[_i_])
66 /**
67 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
69 * Usually this function is called by platforms without pinctrl driver support
70 * but run with some shared drivers using pinctrl APIs.
71 * After calling this function, the pinctrl core will return successfully
72 * with creating a dummy state for the driver to keep going smoothly.
74 void pinctrl_provide_dummies(void)
76 pinctrl_dummy_state = true;
79 const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
81 /* We're not allowed to register devices without name */
82 return pctldev->desc->name;
84 EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
86 void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
88 return pctldev->driver_data;
90 EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
92 /**
93 * get_pinctrl_dev_from_devname() - look up pin controller device
94 * @devname: the name of a device instance, as returned by dev_name()
96 * Looks up a pin control device matching a certain device name or pure device
97 * pointer, the pure device pointer will take precedence.
99 struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
101 struct pinctrl_dev *pctldev = NULL;
102 bool found = false;
104 if (!devname)
105 return NULL;
107 list_for_each_entry(pctldev, &pinctrldev_list, node) {
108 if (!strcmp(dev_name(pctldev->dev), devname)) {
109 /* Matched on device name */
110 found = true;
111 break;
115 return found ? pctldev : NULL;
119 * pin_get_from_name() - look up a pin number from a name
120 * @pctldev: the pin control device to lookup the pin on
121 * @name: the name of the pin to look up
123 int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
125 unsigned i, pin;
127 /* The pin number can be retrived from the pin controller descriptor */
128 for (i = 0; i < pctldev->desc->npins; i++) {
129 struct pin_desc *desc;
131 pin = pctldev->desc->pins[i].number;
132 desc = pin_desc_get(pctldev, pin);
133 /* Pin space may be sparse */
134 if (desc == NULL)
135 continue;
136 if (desc->name && !strcmp(name, desc->name))
137 return pin;
140 return -EINVAL;
144 * pin_get_name_from_id() - look up a pin name from a pin id
145 * @pctldev: the pin control device to lookup the pin on
146 * @name: the name of the pin to look up
148 const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
150 const struct pin_desc *desc;
152 desc = pin_desc_get(pctldev, pin);
153 if (desc == NULL) {
154 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
155 pin);
156 return NULL;
159 return desc->name;
163 * pin_is_valid() - check if pin exists on controller
164 * @pctldev: the pin control device to check the pin on
165 * @pin: pin to check, use the local pin controller index number
167 * This tells us whether a certain pin exist on a certain pin controller or
168 * not. Pin lists may be sparse, so some pins may not exist.
170 bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
172 struct pin_desc *pindesc;
174 if (pin < 0)
175 return false;
177 mutex_lock(&pinctrl_mutex);
178 pindesc = pin_desc_get(pctldev, pin);
179 mutex_unlock(&pinctrl_mutex);
181 return pindesc != NULL;
183 EXPORT_SYMBOL_GPL(pin_is_valid);
185 /* Deletes a range of pin descriptors */
186 static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
187 const struct pinctrl_pin_desc *pins,
188 unsigned num_pins)
190 int i;
192 for (i = 0; i < num_pins; i++) {
193 struct pin_desc *pindesc;
195 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
196 pins[i].number);
197 if (pindesc != NULL) {
198 radix_tree_delete(&pctldev->pin_desc_tree,
199 pins[i].number);
200 if (pindesc->dynamic_name)
201 kfree(pindesc->name);
203 kfree(pindesc);
207 static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
208 unsigned number, const char *name)
210 struct pin_desc *pindesc;
212 pindesc = pin_desc_get(pctldev, number);
213 if (pindesc != NULL) {
214 pr_err("pin %d already registered on %s\n", number,
215 pctldev->desc->name);
216 return -EINVAL;
219 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
220 if (pindesc == NULL) {
221 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
222 return -ENOMEM;
225 /* Set owner */
226 pindesc->pctldev = pctldev;
228 /* Copy basic pin info */
229 if (name) {
230 pindesc->name = name;
231 } else {
232 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
233 if (pindesc->name == NULL)
234 return -ENOMEM;
235 pindesc->dynamic_name = true;
238 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
239 pr_debug("registered pin %d (%s) on %s\n",
240 number, pindesc->name, pctldev->desc->name);
241 return 0;
244 static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
245 struct pinctrl_pin_desc const *pins,
246 unsigned num_descs)
248 unsigned i;
249 int ret = 0;
251 for (i = 0; i < num_descs; i++) {
252 ret = pinctrl_register_one_pin(pctldev,
253 pins[i].number, pins[i].name);
254 if (ret)
255 return ret;
258 return 0;
262 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
263 * @pctldev: pin controller device to check
264 * @gpio: gpio pin to check taken from the global GPIO pin space
266 * Tries to match a GPIO pin number to the ranges handled by a certain pin
267 * controller, return the range or NULL
269 static struct pinctrl_gpio_range *
270 pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
272 struct pinctrl_gpio_range *range = NULL;
274 /* Loop over the ranges */
275 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
276 /* Check if we're in the valid range */
277 if (gpio >= range->base &&
278 gpio < range->base + range->npins) {
279 return range;
283 return NULL;
287 * pinctrl_get_device_gpio_range() - find device for GPIO range
288 * @gpio: the pin to locate the pin controller for
289 * @outdev: the pin control device if found
290 * @outrange: the GPIO range if found
292 * Find the pin controller handling a certain GPIO pin from the pinspace of
293 * the GPIO subsystem, return the device and the matching GPIO range. Returns
294 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
295 * may still have not been registered.
297 static int pinctrl_get_device_gpio_range(unsigned gpio,
298 struct pinctrl_dev **outdev,
299 struct pinctrl_gpio_range **outrange)
301 struct pinctrl_dev *pctldev = NULL;
303 /* Loop over the pin controllers */
304 list_for_each_entry(pctldev, &pinctrldev_list, node) {
305 struct pinctrl_gpio_range *range;
307 range = pinctrl_match_gpio_range(pctldev, gpio);
308 if (range != NULL) {
309 *outdev = pctldev;
310 *outrange = range;
311 return 0;
315 return -EPROBE_DEFER;
319 * pinctrl_add_gpio_range() - register a GPIO range for a controller
320 * @pctldev: pin controller device to add the range to
321 * @range: the GPIO range to add
323 * This adds a range of GPIOs to be handled by a certain pin controller. Call
324 * this to register handled ranges after registering your pin controller.
326 void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
327 struct pinctrl_gpio_range *range)
329 mutex_lock(&pinctrl_mutex);
330 list_add_tail(&range->node, &pctldev->gpio_ranges);
331 mutex_unlock(&pinctrl_mutex);
333 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
336 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
337 * @pctldev: pin controller device to remove the range from
338 * @range: the GPIO range to remove
340 void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
341 struct pinctrl_gpio_range *range)
343 mutex_lock(&pinctrl_mutex);
344 list_del(&range->node);
345 mutex_unlock(&pinctrl_mutex);
347 EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
350 * pinctrl_get_group_selector() - returns the group selector for a group
351 * @pctldev: the pin controller handling the group
352 * @pin_group: the pin group to look up
354 int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
355 const char *pin_group)
357 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
358 unsigned ngroups = pctlops->get_groups_count(pctldev);
359 unsigned group_selector = 0;
361 while (group_selector < ngroups) {
362 const char *gname = pctlops->get_group_name(pctldev,
363 group_selector);
364 if (!strcmp(gname, pin_group)) {
365 dev_dbg(pctldev->dev,
366 "found group selector %u for %s\n",
367 group_selector,
368 pin_group);
369 return group_selector;
372 group_selector++;
375 dev_err(pctldev->dev, "does not have pin group %s\n",
376 pin_group);
378 return -EINVAL;
382 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
383 * @gpio: the GPIO pin number from the GPIO subsystem number space
385 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
386 * as part of their gpio_request() semantics, platforms and individual drivers
387 * shall *NOT* request GPIO pins to be muxed in.
389 int pinctrl_request_gpio(unsigned gpio)
391 struct pinctrl_dev *pctldev;
392 struct pinctrl_gpio_range *range;
393 int ret;
394 int pin;
396 mutex_lock(&pinctrl_mutex);
398 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
399 if (ret) {
400 mutex_unlock(&pinctrl_mutex);
401 return ret;
404 /* Convert to the pin controllers number space */
405 pin = gpio - range->base + range->pin_base;
407 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
409 mutex_unlock(&pinctrl_mutex);
410 return ret;
412 EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
415 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
416 * @gpio: the GPIO pin number from the GPIO subsystem number space
418 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
419 * as part of their gpio_free() semantics, platforms and individual drivers
420 * shall *NOT* request GPIO pins to be muxed out.
422 void pinctrl_free_gpio(unsigned gpio)
424 struct pinctrl_dev *pctldev;
425 struct pinctrl_gpio_range *range;
426 int ret;
427 int pin;
429 mutex_lock(&pinctrl_mutex);
431 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
432 if (ret) {
433 mutex_unlock(&pinctrl_mutex);
434 return;
437 /* Convert to the pin controllers number space */
438 pin = gpio - range->base + range->pin_base;
440 pinmux_free_gpio(pctldev, pin, range);
442 mutex_unlock(&pinctrl_mutex);
444 EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
446 static int pinctrl_gpio_direction(unsigned gpio, bool input)
448 struct pinctrl_dev *pctldev;
449 struct pinctrl_gpio_range *range;
450 int ret;
451 int pin;
453 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
454 if (ret)
455 return ret;
457 /* Convert to the pin controllers number space */
458 pin = gpio - range->base + range->pin_base;
460 return pinmux_gpio_direction(pctldev, range, pin, input);
464 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
465 * @gpio: the GPIO pin number from the GPIO subsystem number space
467 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
468 * as part of their gpio_direction_input() semantics, platforms and individual
469 * drivers shall *NOT* touch pin control GPIO calls.
471 int pinctrl_gpio_direction_input(unsigned gpio)
473 int ret;
474 mutex_lock(&pinctrl_mutex);
475 ret = pinctrl_gpio_direction(gpio, true);
476 mutex_unlock(&pinctrl_mutex);
477 return ret;
479 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
482 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
483 * @gpio: the GPIO pin number from the GPIO subsystem number space
485 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
486 * as part of their gpio_direction_output() semantics, platforms and individual
487 * drivers shall *NOT* touch pin control GPIO calls.
489 int pinctrl_gpio_direction_output(unsigned gpio)
491 int ret;
492 mutex_lock(&pinctrl_mutex);
493 ret = pinctrl_gpio_direction(gpio, false);
494 mutex_unlock(&pinctrl_mutex);
495 return ret;
497 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
499 static struct pinctrl_state *find_state(struct pinctrl *p,
500 const char *name)
502 struct pinctrl_state *state;
504 list_for_each_entry(state, &p->states, node)
505 if (!strcmp(state->name, name))
506 return state;
508 return NULL;
511 static struct pinctrl_state *create_state(struct pinctrl *p,
512 const char *name)
514 struct pinctrl_state *state;
516 state = kzalloc(sizeof(*state), GFP_KERNEL);
517 if (state == NULL) {
518 dev_err(p->dev,
519 "failed to alloc struct pinctrl_state\n");
520 return ERR_PTR(-ENOMEM);
523 state->name = name;
524 INIT_LIST_HEAD(&state->settings);
526 list_add_tail(&state->node, &p->states);
528 return state;
531 static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
533 struct pinctrl_state *state;
534 struct pinctrl_setting *setting;
535 int ret;
537 state = find_state(p, map->name);
538 if (!state)
539 state = create_state(p, map->name);
540 if (IS_ERR(state))
541 return PTR_ERR(state);
543 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
544 return 0;
546 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
547 if (setting == NULL) {
548 dev_err(p->dev,
549 "failed to alloc struct pinctrl_setting\n");
550 return -ENOMEM;
553 setting->type = map->type;
555 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
556 if (setting->pctldev == NULL) {
557 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
558 map->ctrl_dev_name);
559 kfree(setting);
561 * OK let us guess that the driver is not there yet, and
562 * let's defer obtaining this pinctrl handle to later...
564 return -EPROBE_DEFER;
567 switch (map->type) {
568 case PIN_MAP_TYPE_MUX_GROUP:
569 ret = pinmux_map_to_setting(map, setting);
570 break;
571 case PIN_MAP_TYPE_CONFIGS_PIN:
572 case PIN_MAP_TYPE_CONFIGS_GROUP:
573 ret = pinconf_map_to_setting(map, setting);
574 break;
575 default:
576 ret = -EINVAL;
577 break;
579 if (ret < 0) {
580 kfree(setting);
581 return ret;
584 list_add_tail(&setting->node, &state->settings);
586 return 0;
589 static struct pinctrl *find_pinctrl(struct device *dev)
591 struct pinctrl *p;
593 list_for_each_entry(p, &pinctrl_list, node)
594 if (p->dev == dev)
595 return p;
597 return NULL;
600 static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
602 static struct pinctrl *create_pinctrl(struct device *dev)
604 struct pinctrl *p;
605 const char *devname;
606 struct pinctrl_maps *maps_node;
607 int i;
608 struct pinctrl_map const *map;
609 int ret;
612 * create the state cookie holder struct pinctrl for each
613 * mapping, this is what consumers will get when requesting
614 * a pin control handle with pinctrl_get()
616 p = kzalloc(sizeof(*p), GFP_KERNEL);
617 if (p == NULL) {
618 dev_err(dev, "failed to alloc struct pinctrl\n");
619 return ERR_PTR(-ENOMEM);
621 p->dev = dev;
622 INIT_LIST_HEAD(&p->states);
623 INIT_LIST_HEAD(&p->dt_maps);
625 ret = pinctrl_dt_to_map(p);
626 if (ret < 0) {
627 kfree(p);
628 return ERR_PTR(ret);
631 devname = dev_name(dev);
633 /* Iterate over the pin control maps to locate the right ones */
634 for_each_maps(maps_node, i, map) {
635 /* Map must be for this device */
636 if (strcmp(map->dev_name, devname))
637 continue;
639 ret = add_setting(p, map);
640 if (ret < 0) {
641 pinctrl_put_locked(p, false);
642 return ERR_PTR(ret);
646 /* Add the pinmux to the global list */
647 list_add_tail(&p->node, &pinctrl_list);
649 return p;
652 static struct pinctrl *pinctrl_get_locked(struct device *dev)
654 struct pinctrl *p;
656 if (WARN_ON(!dev))
657 return ERR_PTR(-EINVAL);
659 p = find_pinctrl(dev);
660 if (p != NULL)
661 return ERR_PTR(-EBUSY);
663 p = create_pinctrl(dev);
664 if (IS_ERR(p))
665 return p;
667 return p;
671 * pinctrl_get() - retrieves the pinctrl handle for a device
672 * @dev: the device to obtain the handle for
674 struct pinctrl *pinctrl_get(struct device *dev)
676 struct pinctrl *p;
678 mutex_lock(&pinctrl_mutex);
679 p = pinctrl_get_locked(dev);
680 mutex_unlock(&pinctrl_mutex);
682 return p;
684 EXPORT_SYMBOL_GPL(pinctrl_get);
686 static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
688 struct pinctrl_state *state, *n1;
689 struct pinctrl_setting *setting, *n2;
691 list_for_each_entry_safe(state, n1, &p->states, node) {
692 list_for_each_entry_safe(setting, n2, &state->settings, node) {
693 switch (setting->type) {
694 case PIN_MAP_TYPE_MUX_GROUP:
695 if (state == p->state)
696 pinmux_disable_setting(setting);
697 pinmux_free_setting(setting);
698 break;
699 case PIN_MAP_TYPE_CONFIGS_PIN:
700 case PIN_MAP_TYPE_CONFIGS_GROUP:
701 pinconf_free_setting(setting);
702 break;
703 default:
704 break;
706 list_del(&setting->node);
707 kfree(setting);
709 list_del(&state->node);
710 kfree(state);
713 pinctrl_dt_free_maps(p);
715 if (inlist)
716 list_del(&p->node);
717 kfree(p);
721 * pinctrl_put() - release a previously claimed pinctrl handle
722 * @p: the pinctrl handle to release
724 void pinctrl_put(struct pinctrl *p)
726 mutex_lock(&pinctrl_mutex);
727 pinctrl_put_locked(p, true);
728 mutex_unlock(&pinctrl_mutex);
730 EXPORT_SYMBOL_GPL(pinctrl_put);
732 static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
733 const char *name)
735 struct pinctrl_state *state;
737 state = find_state(p, name);
738 if (!state) {
739 if (pinctrl_dummy_state) {
740 /* create dummy state */
741 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
742 name);
743 state = create_state(p, name);
744 if (IS_ERR(state))
745 return state;
746 } else {
747 return ERR_PTR(-ENODEV);
751 return state;
755 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
756 * @p: the pinctrl handle to retrieve the state from
757 * @name: the state name to retrieve
759 struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
761 struct pinctrl_state *s;
763 mutex_lock(&pinctrl_mutex);
764 s = pinctrl_lookup_state_locked(p, name);
765 mutex_unlock(&pinctrl_mutex);
767 return s;
769 EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
771 static int pinctrl_select_state_locked(struct pinctrl *p,
772 struct pinctrl_state *state)
774 struct pinctrl_setting *setting, *setting2;
775 int ret;
777 if (p->state == state)
778 return 0;
780 if (p->state) {
782 * The set of groups with a mux configuration in the old state
783 * may not be identical to the set of groups with a mux setting
784 * in the new state. While this might be unusual, it's entirely
785 * possible for the "user"-supplied mapping table to be written
786 * that way. For each group that was configured in the old state
787 * but not in the new state, this code puts that group into a
788 * safe/disabled state.
790 list_for_each_entry(setting, &p->state->settings, node) {
791 bool found = false;
792 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
793 continue;
794 list_for_each_entry(setting2, &state->settings, node) {
795 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
796 continue;
797 if (setting2->data.mux.group ==
798 setting->data.mux.group) {
799 found = true;
800 break;
803 if (!found)
804 pinmux_disable_setting(setting);
808 p->state = state;
810 /* Apply all the settings for the new state */
811 list_for_each_entry(setting, &state->settings, node) {
812 switch (setting->type) {
813 case PIN_MAP_TYPE_MUX_GROUP:
814 ret = pinmux_enable_setting(setting);
815 break;
816 case PIN_MAP_TYPE_CONFIGS_PIN:
817 case PIN_MAP_TYPE_CONFIGS_GROUP:
818 ret = pinconf_apply_setting(setting);
819 break;
820 default:
821 ret = -EINVAL;
822 break;
824 if (ret < 0) {
825 /* FIXME: Difficult to return to prev state */
826 return ret;
830 return 0;
834 * pinctrl_select() - select/activate/program a pinctrl state to HW
835 * @p: the pinctrl handle for the device that requests configuratio
836 * @state: the state handle to select/activate/program
838 int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
840 int ret;
842 mutex_lock(&pinctrl_mutex);
843 ret = pinctrl_select_state_locked(p, state);
844 mutex_unlock(&pinctrl_mutex);
846 return ret;
848 EXPORT_SYMBOL_GPL(pinctrl_select_state);
850 static void devm_pinctrl_release(struct device *dev, void *res)
852 pinctrl_put(*(struct pinctrl **)res);
856 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
857 * @dev: the device to obtain the handle for
859 * If there is a need to explicitly destroy the returned struct pinctrl,
860 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
862 struct pinctrl *devm_pinctrl_get(struct device *dev)
864 struct pinctrl **ptr, *p;
866 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
867 if (!ptr)
868 return ERR_PTR(-ENOMEM);
870 p = pinctrl_get(dev);
871 if (!IS_ERR(p)) {
872 *ptr = p;
873 devres_add(dev, ptr);
874 } else {
875 devres_free(ptr);
878 return p;
880 EXPORT_SYMBOL_GPL(devm_pinctrl_get);
882 static int devm_pinctrl_match(struct device *dev, void *res, void *data)
884 struct pinctrl **p = res;
886 return *p == data;
890 * devm_pinctrl_put() - Resource managed pinctrl_put()
891 * @p: the pinctrl handle to release
893 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
894 * this function will not need to be called and the resource management
895 * code will ensure that the resource is freed.
897 void devm_pinctrl_put(struct pinctrl *p)
899 WARN_ON(devres_destroy(p->dev, devm_pinctrl_release,
900 devm_pinctrl_match, p));
901 pinctrl_put(p);
903 EXPORT_SYMBOL_GPL(devm_pinctrl_put);
905 int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
906 bool dup, bool locked)
908 int i, ret;
909 struct pinctrl_maps *maps_node;
911 pr_debug("add %d pinmux maps\n", num_maps);
913 /* First sanity check the new mapping */
914 for (i = 0; i < num_maps; i++) {
915 if (!maps[i].dev_name) {
916 pr_err("failed to register map %s (%d): no device given\n",
917 maps[i].name, i);
918 return -EINVAL;
921 if (!maps[i].name) {
922 pr_err("failed to register map %d: no map name given\n",
924 return -EINVAL;
927 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
928 !maps[i].ctrl_dev_name) {
929 pr_err("failed to register map %s (%d): no pin control device given\n",
930 maps[i].name, i);
931 return -EINVAL;
934 switch (maps[i].type) {
935 case PIN_MAP_TYPE_DUMMY_STATE:
936 break;
937 case PIN_MAP_TYPE_MUX_GROUP:
938 ret = pinmux_validate_map(&maps[i], i);
939 if (ret < 0)
940 return ret;
941 break;
942 case PIN_MAP_TYPE_CONFIGS_PIN:
943 case PIN_MAP_TYPE_CONFIGS_GROUP:
944 ret = pinconf_validate_map(&maps[i], i);
945 if (ret < 0)
946 return ret;
947 break;
948 default:
949 pr_err("failed to register map %s (%d): invalid type given\n",
950 maps[i].name, i);
951 return -EINVAL;
955 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
956 if (!maps_node) {
957 pr_err("failed to alloc struct pinctrl_maps\n");
958 return -ENOMEM;
961 maps_node->num_maps = num_maps;
962 if (dup) {
963 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
964 GFP_KERNEL);
965 if (!maps_node->maps) {
966 pr_err("failed to duplicate mapping table\n");
967 kfree(maps_node);
968 return -ENOMEM;
970 } else {
971 maps_node->maps = maps;
974 if (!locked)
975 mutex_lock(&pinctrl_mutex);
976 list_add_tail(&maps_node->node, &pinctrl_maps);
977 if (!locked)
978 mutex_unlock(&pinctrl_mutex);
980 return 0;
984 * pinctrl_register_mappings() - register a set of pin controller mappings
985 * @maps: the pincontrol mappings table to register. This should probably be
986 * marked with __initdata so it can be discarded after boot. This
987 * function will perform a shallow copy for the mapping entries.
988 * @num_maps: the number of maps in the mapping table
990 int pinctrl_register_mappings(struct pinctrl_map const *maps,
991 unsigned num_maps)
993 return pinctrl_register_map(maps, num_maps, true, false);
996 void pinctrl_unregister_map(struct pinctrl_map const *map)
998 struct pinctrl_maps *maps_node;
1000 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1001 if (maps_node->maps == map) {
1002 list_del(&maps_node->node);
1003 return;
1008 #ifdef CONFIG_DEBUG_FS
1010 static int pinctrl_pins_show(struct seq_file *s, void *what)
1012 struct pinctrl_dev *pctldev = s->private;
1013 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1014 unsigned i, pin;
1016 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
1018 mutex_lock(&pinctrl_mutex);
1020 /* The pin number can be retrived from the pin controller descriptor */
1021 for (i = 0; i < pctldev->desc->npins; i++) {
1022 struct pin_desc *desc;
1024 pin = pctldev->desc->pins[i].number;
1025 desc = pin_desc_get(pctldev, pin);
1026 /* Pin space may be sparse */
1027 if (desc == NULL)
1028 continue;
1030 seq_printf(s, "pin %d (%s) ", pin,
1031 desc->name ? desc->name : "unnamed");
1033 /* Driver-specific info per pin */
1034 if (ops->pin_dbg_show)
1035 ops->pin_dbg_show(pctldev, s, pin);
1037 seq_puts(s, "\n");
1040 mutex_unlock(&pinctrl_mutex);
1042 return 0;
1045 static int pinctrl_groups_show(struct seq_file *s, void *what)
1047 struct pinctrl_dev *pctldev = s->private;
1048 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1049 unsigned ngroups, selector = 0;
1051 ngroups = ops->get_groups_count(pctldev);
1052 mutex_lock(&pinctrl_mutex);
1054 seq_puts(s, "registered pin groups:\n");
1055 while (selector < ngroups) {
1056 const unsigned *pins;
1057 unsigned num_pins;
1058 const char *gname = ops->get_group_name(pctldev, selector);
1059 const char *pname;
1060 int ret;
1061 int i;
1063 ret = ops->get_group_pins(pctldev, selector,
1064 &pins, &num_pins);
1065 if (ret)
1066 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1067 gname);
1068 else {
1069 seq_printf(s, "group: %s\n", gname);
1070 for (i = 0; i < num_pins; i++) {
1071 pname = pin_get_name(pctldev, pins[i]);
1072 if (WARN_ON(!pname))
1073 return -EINVAL;
1074 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1076 seq_puts(s, "\n");
1078 selector++;
1081 mutex_unlock(&pinctrl_mutex);
1083 return 0;
1086 static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1088 struct pinctrl_dev *pctldev = s->private;
1089 struct pinctrl_gpio_range *range = NULL;
1091 seq_puts(s, "GPIO ranges handled:\n");
1093 mutex_lock(&pinctrl_mutex);
1095 /* Loop over the ranges */
1096 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
1097 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1098 range->id, range->name,
1099 range->base, (range->base + range->npins - 1),
1100 range->pin_base,
1101 (range->pin_base + range->npins - 1));
1104 mutex_unlock(&pinctrl_mutex);
1106 return 0;
1109 static int pinctrl_devices_show(struct seq_file *s, void *what)
1111 struct pinctrl_dev *pctldev;
1113 seq_puts(s, "name [pinmux] [pinconf]\n");
1115 mutex_lock(&pinctrl_mutex);
1117 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1118 seq_printf(s, "%s ", pctldev->desc->name);
1119 if (pctldev->desc->pmxops)
1120 seq_puts(s, "yes ");
1121 else
1122 seq_puts(s, "no ");
1123 if (pctldev->desc->confops)
1124 seq_puts(s, "yes");
1125 else
1126 seq_puts(s, "no");
1127 seq_puts(s, "\n");
1130 mutex_unlock(&pinctrl_mutex);
1132 return 0;
1135 static inline const char *map_type(enum pinctrl_map_type type)
1137 static const char * const names[] = {
1138 "INVALID",
1139 "DUMMY_STATE",
1140 "MUX_GROUP",
1141 "CONFIGS_PIN",
1142 "CONFIGS_GROUP",
1145 if (type >= ARRAY_SIZE(names))
1146 return "UNKNOWN";
1148 return names[type];
1151 static int pinctrl_maps_show(struct seq_file *s, void *what)
1153 struct pinctrl_maps *maps_node;
1154 int i;
1155 struct pinctrl_map const *map;
1157 seq_puts(s, "Pinctrl maps:\n");
1159 mutex_lock(&pinctrl_mutex);
1161 for_each_maps(maps_node, i, map) {
1162 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1163 map->dev_name, map->name, map_type(map->type),
1164 map->type);
1166 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1167 seq_printf(s, "controlling device %s\n",
1168 map->ctrl_dev_name);
1170 switch (map->type) {
1171 case PIN_MAP_TYPE_MUX_GROUP:
1172 pinmux_show_map(s, map);
1173 break;
1174 case PIN_MAP_TYPE_CONFIGS_PIN:
1175 case PIN_MAP_TYPE_CONFIGS_GROUP:
1176 pinconf_show_map(s, map);
1177 break;
1178 default:
1179 break;
1182 seq_printf(s, "\n");
1185 mutex_unlock(&pinctrl_mutex);
1187 return 0;
1190 static int pinctrl_show(struct seq_file *s, void *what)
1192 struct pinctrl *p;
1193 struct pinctrl_state *state;
1194 struct pinctrl_setting *setting;
1196 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
1198 mutex_lock(&pinctrl_mutex);
1200 list_for_each_entry(p, &pinctrl_list, node) {
1201 seq_printf(s, "device: %s current state: %s\n",
1202 dev_name(p->dev),
1203 p->state ? p->state->name : "none");
1205 list_for_each_entry(state, &p->states, node) {
1206 seq_printf(s, " state: %s\n", state->name);
1208 list_for_each_entry(setting, &state->settings, node) {
1209 struct pinctrl_dev *pctldev = setting->pctldev;
1211 seq_printf(s, " type: %s controller %s ",
1212 map_type(setting->type),
1213 pinctrl_dev_get_name(pctldev));
1215 switch (setting->type) {
1216 case PIN_MAP_TYPE_MUX_GROUP:
1217 pinmux_show_setting(s, setting);
1218 break;
1219 case PIN_MAP_TYPE_CONFIGS_PIN:
1220 case PIN_MAP_TYPE_CONFIGS_GROUP:
1221 pinconf_show_setting(s, setting);
1222 break;
1223 default:
1224 break;
1230 mutex_unlock(&pinctrl_mutex);
1232 return 0;
1235 static int pinctrl_pins_open(struct inode *inode, struct file *file)
1237 return single_open(file, pinctrl_pins_show, inode->i_private);
1240 static int pinctrl_groups_open(struct inode *inode, struct file *file)
1242 return single_open(file, pinctrl_groups_show, inode->i_private);
1245 static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1247 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1250 static int pinctrl_devices_open(struct inode *inode, struct file *file)
1252 return single_open(file, pinctrl_devices_show, NULL);
1255 static int pinctrl_maps_open(struct inode *inode, struct file *file)
1257 return single_open(file, pinctrl_maps_show, NULL);
1260 static int pinctrl_open(struct inode *inode, struct file *file)
1262 return single_open(file, pinctrl_show, NULL);
1265 static const struct file_operations pinctrl_pins_ops = {
1266 .open = pinctrl_pins_open,
1267 .read = seq_read,
1268 .llseek = seq_lseek,
1269 .release = single_release,
1272 static const struct file_operations pinctrl_groups_ops = {
1273 .open = pinctrl_groups_open,
1274 .read = seq_read,
1275 .llseek = seq_lseek,
1276 .release = single_release,
1279 static const struct file_operations pinctrl_gpioranges_ops = {
1280 .open = pinctrl_gpioranges_open,
1281 .read = seq_read,
1282 .llseek = seq_lseek,
1283 .release = single_release,
1286 static const struct file_operations pinctrl_devices_ops = {
1287 .open = pinctrl_devices_open,
1288 .read = seq_read,
1289 .llseek = seq_lseek,
1290 .release = single_release,
1293 static const struct file_operations pinctrl_maps_ops = {
1294 .open = pinctrl_maps_open,
1295 .read = seq_read,
1296 .llseek = seq_lseek,
1297 .release = single_release,
1300 static const struct file_operations pinctrl_ops = {
1301 .open = pinctrl_open,
1302 .read = seq_read,
1303 .llseek = seq_lseek,
1304 .release = single_release,
1307 static struct dentry *debugfs_root;
1309 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1311 struct dentry *device_root;
1313 device_root = debugfs_create_dir(dev_name(pctldev->dev),
1314 debugfs_root);
1315 pctldev->device_root = device_root;
1317 if (IS_ERR(device_root) || !device_root) {
1318 pr_warn("failed to create debugfs directory for %s\n",
1319 dev_name(pctldev->dev));
1320 return;
1322 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1323 device_root, pctldev, &pinctrl_pins_ops);
1324 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1325 device_root, pctldev, &pinctrl_groups_ops);
1326 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1327 device_root, pctldev, &pinctrl_gpioranges_ops);
1328 pinmux_init_device_debugfs(device_root, pctldev);
1329 pinconf_init_device_debugfs(device_root, pctldev);
1332 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1334 debugfs_remove_recursive(pctldev->device_root);
1337 static void pinctrl_init_debugfs(void)
1339 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1340 if (IS_ERR(debugfs_root) || !debugfs_root) {
1341 pr_warn("failed to create debugfs directory\n");
1342 debugfs_root = NULL;
1343 return;
1346 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1347 debugfs_root, NULL, &pinctrl_devices_ops);
1348 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1349 debugfs_root, NULL, &pinctrl_maps_ops);
1350 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1351 debugfs_root, NULL, &pinctrl_ops);
1354 #else /* CONFIG_DEBUG_FS */
1356 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1360 static void pinctrl_init_debugfs(void)
1364 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1368 #endif
1370 static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1372 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1374 if (!ops ||
1375 !ops->get_groups_count ||
1376 !ops->get_group_name ||
1377 !ops->get_group_pins)
1378 return -EINVAL;
1380 if (ops->dt_node_to_map && !ops->dt_free_map)
1381 return -EINVAL;
1383 return 0;
1387 * pinctrl_register() - register a pin controller device
1388 * @pctldesc: descriptor for this pin controller
1389 * @dev: parent device for this pin controller
1390 * @driver_data: private pin controller data for this pin controller
1392 struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1393 struct device *dev, void *driver_data)
1395 struct pinctrl_dev *pctldev;
1396 int ret;
1398 if (pctldesc == NULL)
1399 return NULL;
1400 if (pctldesc->name == NULL)
1401 return NULL;
1403 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
1404 if (pctldev == NULL) {
1405 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
1406 return NULL;
1409 /* Initialize pin control device struct */
1410 pctldev->owner = pctldesc->owner;
1411 pctldev->desc = pctldesc;
1412 pctldev->driver_data = driver_data;
1413 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
1414 INIT_LIST_HEAD(&pctldev->gpio_ranges);
1415 pctldev->dev = dev;
1417 /* check core ops for sanity */
1418 ret = pinctrl_check_ops(pctldev);
1419 if (ret) {
1420 dev_err(dev, "pinctrl ops lacks necessary functions\n");
1421 goto out_err;
1424 /* If we're implementing pinmuxing, check the ops for sanity */
1425 if (pctldesc->pmxops) {
1426 ret = pinmux_check_ops(pctldev);
1427 if (ret)
1428 goto out_err;
1431 /* If we're implementing pinconfig, check the ops for sanity */
1432 if (pctldesc->confops) {
1433 ret = pinconf_check_ops(pctldev);
1434 if (ret)
1435 goto out_err;
1438 /* Register all the pins */
1439 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
1440 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1441 if (ret) {
1442 dev_err(dev, "error during pin registration\n");
1443 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1444 pctldesc->npins);
1445 goto out_err;
1448 mutex_lock(&pinctrl_mutex);
1450 list_add_tail(&pctldev->node, &pinctrldev_list);
1452 pctldev->p = pinctrl_get_locked(pctldev->dev);
1453 if (!IS_ERR(pctldev->p)) {
1454 struct pinctrl_state *s =
1455 pinctrl_lookup_state_locked(pctldev->p,
1456 PINCTRL_STATE_DEFAULT);
1457 if (IS_ERR(s)) {
1458 dev_dbg(dev, "failed to lookup the default state\n");
1459 } else {
1460 ret = pinctrl_select_state_locked(pctldev->p, s);
1461 if (ret) {
1462 dev_err(dev,
1463 "failed to select default state\n");
1468 mutex_unlock(&pinctrl_mutex);
1470 pinctrl_init_device_debugfs(pctldev);
1472 return pctldev;
1474 out_err:
1475 kfree(pctldev);
1476 return NULL;
1478 EXPORT_SYMBOL_GPL(pinctrl_register);
1481 * pinctrl_unregister() - unregister pinmux
1482 * @pctldev: pin controller to unregister
1484 * Called by pinmux drivers to unregister a pinmux.
1486 void pinctrl_unregister(struct pinctrl_dev *pctldev)
1488 if (pctldev == NULL)
1489 return;
1491 pinctrl_remove_device_debugfs(pctldev);
1493 mutex_lock(&pinctrl_mutex);
1495 if (!IS_ERR(pctldev->p))
1496 pinctrl_put_locked(pctldev->p, true);
1498 /* TODO: check that no pinmuxes are still active? */
1499 list_del(&pctldev->node);
1500 /* Destroy descriptor tree */
1501 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1502 pctldev->desc->npins);
1503 kfree(pctldev);
1505 mutex_unlock(&pinctrl_mutex);
1507 EXPORT_SYMBOL_GPL(pinctrl_unregister);
1509 static int __init pinctrl_init(void)
1511 pr_info("initialized pinctrl subsystem\n");
1512 pinctrl_init_debugfs();
1513 return 0;
1516 /* init early since many drivers really need to initialized pinmux early */
1517 core_initcall(pinctrl_init);