Adding support for MOXA ART SoC. Testing port of linux-2.6.32.60-moxart.
[linux-3.6.7-moxart.git] / drivers / pinctrl / core.c
blob63560c3a322c56457a8c0e6900f52d730f29f2a0
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);
335 void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
336 struct pinctrl_gpio_range *ranges,
337 unsigned nranges)
339 int i;
341 for (i = 0; i < nranges; i++)
342 pinctrl_add_gpio_range(pctldev, &ranges[i]);
344 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
347 * pinctrl_get_group_selector() - returns the group selector for a group
348 * @pctldev: the pin controller handling the group
349 * @pin_group: the pin group to look up
351 int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
352 const char *pin_group)
354 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
355 unsigned ngroups = pctlops->get_groups_count(pctldev);
356 unsigned group_selector = 0;
358 while (group_selector < ngroups) {
359 const char *gname = pctlops->get_group_name(pctldev,
360 group_selector);
361 if (!strcmp(gname, pin_group)) {
362 dev_dbg(pctldev->dev,
363 "found group selector %u for %s\n",
364 group_selector,
365 pin_group);
366 return group_selector;
369 group_selector++;
372 dev_err(pctldev->dev, "does not have pin group %s\n",
373 pin_group);
375 return -EINVAL;
379 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
380 * @gpio: the GPIO pin number from the GPIO subsystem number space
382 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
383 * as part of their gpio_request() semantics, platforms and individual drivers
384 * shall *NOT* request GPIO pins to be muxed in.
386 int pinctrl_request_gpio(unsigned gpio)
388 struct pinctrl_dev *pctldev;
389 struct pinctrl_gpio_range *range;
390 int ret;
391 int pin;
393 mutex_lock(&pinctrl_mutex);
395 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
396 if (ret) {
397 mutex_unlock(&pinctrl_mutex);
398 return ret;
401 /* Convert to the pin controllers number space */
402 pin = gpio - range->base + range->pin_base;
404 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
406 mutex_unlock(&pinctrl_mutex);
407 return ret;
409 EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
412 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
413 * @gpio: the GPIO pin number from the GPIO subsystem number space
415 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
416 * as part of their gpio_free() semantics, platforms and individual drivers
417 * shall *NOT* request GPIO pins to be muxed out.
419 void pinctrl_free_gpio(unsigned gpio)
421 struct pinctrl_dev *pctldev;
422 struct pinctrl_gpio_range *range;
423 int ret;
424 int pin;
426 mutex_lock(&pinctrl_mutex);
428 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
429 if (ret) {
430 mutex_unlock(&pinctrl_mutex);
431 return;
434 /* Convert to the pin controllers number space */
435 pin = gpio - range->base + range->pin_base;
437 pinmux_free_gpio(pctldev, pin, range);
439 mutex_unlock(&pinctrl_mutex);
441 EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
443 static int pinctrl_gpio_direction(unsigned gpio, bool input)
445 struct pinctrl_dev *pctldev;
446 struct pinctrl_gpio_range *range;
447 int ret;
448 int pin;
450 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
451 if (ret)
452 return ret;
454 /* Convert to the pin controllers number space */
455 pin = gpio - range->base + range->pin_base;
457 return pinmux_gpio_direction(pctldev, range, pin, input);
461 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
462 * @gpio: the GPIO pin number from the GPIO subsystem number space
464 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
465 * as part of their gpio_direction_input() semantics, platforms and individual
466 * drivers shall *NOT* touch pin control GPIO calls.
468 int pinctrl_gpio_direction_input(unsigned gpio)
470 int ret;
471 mutex_lock(&pinctrl_mutex);
472 ret = pinctrl_gpio_direction(gpio, true);
473 mutex_unlock(&pinctrl_mutex);
474 return ret;
476 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
479 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
480 * @gpio: the GPIO pin number from the GPIO subsystem number space
482 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
483 * as part of their gpio_direction_output() semantics, platforms and individual
484 * drivers shall *NOT* touch pin control GPIO calls.
486 int pinctrl_gpio_direction_output(unsigned gpio)
488 int ret;
489 mutex_lock(&pinctrl_mutex);
490 ret = pinctrl_gpio_direction(gpio, false);
491 mutex_unlock(&pinctrl_mutex);
492 return ret;
494 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
496 static struct pinctrl_state *find_state(struct pinctrl *p,
497 const char *name)
499 struct pinctrl_state *state;
501 list_for_each_entry(state, &p->states, node)
502 if (!strcmp(state->name, name))
503 return state;
505 return NULL;
508 static struct pinctrl_state *create_state(struct pinctrl *p,
509 const char *name)
511 struct pinctrl_state *state;
513 state = kzalloc(sizeof(*state), GFP_KERNEL);
514 if (state == NULL) {
515 dev_err(p->dev,
516 "failed to alloc struct pinctrl_state\n");
517 return ERR_PTR(-ENOMEM);
520 state->name = name;
521 INIT_LIST_HEAD(&state->settings);
523 list_add_tail(&state->node, &p->states);
525 return state;
528 static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
530 struct pinctrl_state *state;
531 struct pinctrl_setting *setting;
532 int ret;
534 state = find_state(p, map->name);
535 if (!state)
536 state = create_state(p, map->name);
537 if (IS_ERR(state))
538 return PTR_ERR(state);
540 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
541 return 0;
543 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
544 if (setting == NULL) {
545 dev_err(p->dev,
546 "failed to alloc struct pinctrl_setting\n");
547 return -ENOMEM;
550 setting->type = map->type;
552 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
553 if (setting->pctldev == NULL) {
554 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
555 map->ctrl_dev_name);
556 kfree(setting);
558 * OK let us guess that the driver is not there yet, and
559 * let's defer obtaining this pinctrl handle to later...
561 return -EPROBE_DEFER;
564 switch (map->type) {
565 case PIN_MAP_TYPE_MUX_GROUP:
566 ret = pinmux_map_to_setting(map, setting);
567 break;
568 case PIN_MAP_TYPE_CONFIGS_PIN:
569 case PIN_MAP_TYPE_CONFIGS_GROUP:
570 ret = pinconf_map_to_setting(map, setting);
571 break;
572 default:
573 ret = -EINVAL;
574 break;
576 if (ret < 0) {
577 kfree(setting);
578 return ret;
581 list_add_tail(&setting->node, &state->settings);
583 return 0;
586 static struct pinctrl *find_pinctrl(struct device *dev)
588 struct pinctrl *p;
590 list_for_each_entry(p, &pinctrl_list, node)
591 if (p->dev == dev)
592 return p;
594 return NULL;
597 static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
599 static struct pinctrl *create_pinctrl(struct device *dev)
601 struct pinctrl *p;
602 const char *devname;
603 struct pinctrl_maps *maps_node;
604 int i;
605 struct pinctrl_map const *map;
606 int ret;
609 * create the state cookie holder struct pinctrl for each
610 * mapping, this is what consumers will get when requesting
611 * a pin control handle with pinctrl_get()
613 p = kzalloc(sizeof(*p), GFP_KERNEL);
614 if (p == NULL) {
615 dev_err(dev, "failed to alloc struct pinctrl\n");
616 return ERR_PTR(-ENOMEM);
618 p->dev = dev;
619 INIT_LIST_HEAD(&p->states);
620 INIT_LIST_HEAD(&p->dt_maps);
622 ret = pinctrl_dt_to_map(p);
623 if (ret < 0) {
624 kfree(p);
625 return ERR_PTR(ret);
628 devname = dev_name(dev);
630 /* Iterate over the pin control maps to locate the right ones */
631 for_each_maps(maps_node, i, map) {
632 /* Map must be for this device */
633 if (strcmp(map->dev_name, devname))
634 continue;
636 ret = add_setting(p, map);
637 if (ret < 0) {
638 pinctrl_put_locked(p, false);
639 return ERR_PTR(ret);
643 /* Add the pinmux to the global list */
644 list_add_tail(&p->node, &pinctrl_list);
646 return p;
649 static struct pinctrl *pinctrl_get_locked(struct device *dev)
651 struct pinctrl *p;
653 if (WARN_ON(!dev))
654 return ERR_PTR(-EINVAL);
656 p = find_pinctrl(dev);
657 if (p != NULL)
658 return ERR_PTR(-EBUSY);
660 return create_pinctrl(dev);
664 * pinctrl_get() - retrieves the pinctrl handle for a device
665 * @dev: the device to obtain the handle for
667 struct pinctrl *pinctrl_get(struct device *dev)
669 struct pinctrl *p;
671 mutex_lock(&pinctrl_mutex);
672 p = pinctrl_get_locked(dev);
673 mutex_unlock(&pinctrl_mutex);
675 return p;
677 EXPORT_SYMBOL_GPL(pinctrl_get);
679 static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
681 struct pinctrl_state *state, *n1;
682 struct pinctrl_setting *setting, *n2;
684 list_for_each_entry_safe(state, n1, &p->states, node) {
685 list_for_each_entry_safe(setting, n2, &state->settings, node) {
686 switch (setting->type) {
687 case PIN_MAP_TYPE_MUX_GROUP:
688 if (state == p->state)
689 pinmux_disable_setting(setting);
690 pinmux_free_setting(setting);
691 break;
692 case PIN_MAP_TYPE_CONFIGS_PIN:
693 case PIN_MAP_TYPE_CONFIGS_GROUP:
694 pinconf_free_setting(setting);
695 break;
696 default:
697 break;
699 list_del(&setting->node);
700 kfree(setting);
702 list_del(&state->node);
703 kfree(state);
706 pinctrl_dt_free_maps(p);
708 if (inlist)
709 list_del(&p->node);
710 kfree(p);
714 * pinctrl_put() - release a previously claimed pinctrl handle
715 * @p: the pinctrl handle to release
717 void pinctrl_put(struct pinctrl *p)
719 mutex_lock(&pinctrl_mutex);
720 pinctrl_put_locked(p, true);
721 mutex_unlock(&pinctrl_mutex);
723 EXPORT_SYMBOL_GPL(pinctrl_put);
725 static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
726 const char *name)
728 struct pinctrl_state *state;
730 state = find_state(p, name);
731 if (!state) {
732 if (pinctrl_dummy_state) {
733 /* create dummy state */
734 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
735 name);
736 state = create_state(p, name);
737 } else
738 state = ERR_PTR(-ENODEV);
741 return state;
745 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
746 * @p: the pinctrl handle to retrieve the state from
747 * @name: the state name to retrieve
749 struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
751 struct pinctrl_state *s;
753 mutex_lock(&pinctrl_mutex);
754 s = pinctrl_lookup_state_locked(p, name);
755 mutex_unlock(&pinctrl_mutex);
757 return s;
759 EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
761 static int pinctrl_select_state_locked(struct pinctrl *p,
762 struct pinctrl_state *state)
764 struct pinctrl_setting *setting, *setting2;
765 int ret;
767 if (p->state == state)
768 return 0;
770 if (p->state) {
772 * The set of groups with a mux configuration in the old state
773 * may not be identical to the set of groups with a mux setting
774 * in the new state. While this might be unusual, it's entirely
775 * possible for the "user"-supplied mapping table to be written
776 * that way. For each group that was configured in the old state
777 * but not in the new state, this code puts that group into a
778 * safe/disabled state.
780 list_for_each_entry(setting, &p->state->settings, node) {
781 bool found = false;
782 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
783 continue;
784 list_for_each_entry(setting2, &state->settings, node) {
785 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
786 continue;
787 if (setting2->data.mux.group ==
788 setting->data.mux.group) {
789 found = true;
790 break;
793 if (!found)
794 pinmux_disable_setting(setting);
798 p->state = state;
800 /* Apply all the settings for the new state */
801 list_for_each_entry(setting, &state->settings, node) {
802 switch (setting->type) {
803 case PIN_MAP_TYPE_MUX_GROUP:
804 ret = pinmux_enable_setting(setting);
805 break;
806 case PIN_MAP_TYPE_CONFIGS_PIN:
807 case PIN_MAP_TYPE_CONFIGS_GROUP:
808 ret = pinconf_apply_setting(setting);
809 break;
810 default:
811 ret = -EINVAL;
812 break;
814 if (ret < 0) {
815 /* FIXME: Difficult to return to prev state */
816 return ret;
820 return 0;
824 * pinctrl_select() - select/activate/program a pinctrl state to HW
825 * @p: the pinctrl handle for the device that requests configuratio
826 * @state: the state handle to select/activate/program
828 int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
830 int ret;
832 mutex_lock(&pinctrl_mutex);
833 ret = pinctrl_select_state_locked(p, state);
834 mutex_unlock(&pinctrl_mutex);
836 return ret;
838 EXPORT_SYMBOL_GPL(pinctrl_select_state);
840 static void devm_pinctrl_release(struct device *dev, void *res)
842 pinctrl_put(*(struct pinctrl **)res);
846 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
847 * @dev: the device to obtain the handle for
849 * If there is a need to explicitly destroy the returned struct pinctrl,
850 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
852 struct pinctrl *devm_pinctrl_get(struct device *dev)
854 struct pinctrl **ptr, *p;
856 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
857 if (!ptr)
858 return ERR_PTR(-ENOMEM);
860 p = pinctrl_get(dev);
861 if (!IS_ERR(p)) {
862 *ptr = p;
863 devres_add(dev, ptr);
864 } else {
865 devres_free(ptr);
868 return p;
870 EXPORT_SYMBOL_GPL(devm_pinctrl_get);
872 static int devm_pinctrl_match(struct device *dev, void *res, void *data)
874 struct pinctrl **p = res;
876 return *p == data;
880 * devm_pinctrl_put() - Resource managed pinctrl_put()
881 * @p: the pinctrl handle to release
883 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
884 * this function will not need to be called and the resource management
885 * code will ensure that the resource is freed.
887 void devm_pinctrl_put(struct pinctrl *p)
889 WARN_ON(devres_destroy(p->dev, devm_pinctrl_release,
890 devm_pinctrl_match, p));
891 pinctrl_put(p);
893 EXPORT_SYMBOL_GPL(devm_pinctrl_put);
895 int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
896 bool dup, bool locked)
898 int i, ret;
899 struct pinctrl_maps *maps_node;
901 pr_debug("add %d pinmux maps\n", num_maps);
903 /* First sanity check the new mapping */
904 for (i = 0; i < num_maps; i++) {
905 if (!maps[i].dev_name) {
906 pr_err("failed to register map %s (%d): no device given\n",
907 maps[i].name, i);
908 return -EINVAL;
911 if (!maps[i].name) {
912 pr_err("failed to register map %d: no map name given\n",
914 return -EINVAL;
917 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
918 !maps[i].ctrl_dev_name) {
919 pr_err("failed to register map %s (%d): no pin control device given\n",
920 maps[i].name, i);
921 return -EINVAL;
924 switch (maps[i].type) {
925 case PIN_MAP_TYPE_DUMMY_STATE:
926 break;
927 case PIN_MAP_TYPE_MUX_GROUP:
928 ret = pinmux_validate_map(&maps[i], i);
929 if (ret < 0)
930 return ret;
931 break;
932 case PIN_MAP_TYPE_CONFIGS_PIN:
933 case PIN_MAP_TYPE_CONFIGS_GROUP:
934 ret = pinconf_validate_map(&maps[i], i);
935 if (ret < 0)
936 return ret;
937 break;
938 default:
939 pr_err("failed to register map %s (%d): invalid type given\n",
940 maps[i].name, i);
941 return -EINVAL;
945 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
946 if (!maps_node) {
947 pr_err("failed to alloc struct pinctrl_maps\n");
948 return -ENOMEM;
951 maps_node->num_maps = num_maps;
952 if (dup) {
953 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
954 GFP_KERNEL);
955 if (!maps_node->maps) {
956 pr_err("failed to duplicate mapping table\n");
957 kfree(maps_node);
958 return -ENOMEM;
960 } else {
961 maps_node->maps = maps;
964 if (!locked)
965 mutex_lock(&pinctrl_mutex);
966 list_add_tail(&maps_node->node, &pinctrl_maps);
967 if (!locked)
968 mutex_unlock(&pinctrl_mutex);
970 return 0;
974 * pinctrl_register_mappings() - register a set of pin controller mappings
975 * @maps: the pincontrol mappings table to register. This should probably be
976 * marked with __initdata so it can be discarded after boot. This
977 * function will perform a shallow copy for the mapping entries.
978 * @num_maps: the number of maps in the mapping table
980 int pinctrl_register_mappings(struct pinctrl_map const *maps,
981 unsigned num_maps)
983 return pinctrl_register_map(maps, num_maps, true, false);
986 void pinctrl_unregister_map(struct pinctrl_map const *map)
988 struct pinctrl_maps *maps_node;
990 list_for_each_entry(maps_node, &pinctrl_maps, node) {
991 if (maps_node->maps == map) {
992 list_del(&maps_node->node);
993 return;
998 #ifdef CONFIG_DEBUG_FS
1000 static int pinctrl_pins_show(struct seq_file *s, void *what)
1002 struct pinctrl_dev *pctldev = s->private;
1003 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1004 unsigned i, pin;
1006 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
1008 mutex_lock(&pinctrl_mutex);
1010 /* The pin number can be retrived from the pin controller descriptor */
1011 for (i = 0; i < pctldev->desc->npins; i++) {
1012 struct pin_desc *desc;
1014 pin = pctldev->desc->pins[i].number;
1015 desc = pin_desc_get(pctldev, pin);
1016 /* Pin space may be sparse */
1017 if (desc == NULL)
1018 continue;
1020 seq_printf(s, "pin %d (%s) ", pin,
1021 desc->name ? desc->name : "unnamed");
1023 /* Driver-specific info per pin */
1024 if (ops->pin_dbg_show)
1025 ops->pin_dbg_show(pctldev, s, pin);
1027 seq_puts(s, "\n");
1030 mutex_unlock(&pinctrl_mutex);
1032 return 0;
1035 static int pinctrl_groups_show(struct seq_file *s, void *what)
1037 struct pinctrl_dev *pctldev = s->private;
1038 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1039 unsigned ngroups, selector = 0;
1041 ngroups = ops->get_groups_count(pctldev);
1042 mutex_lock(&pinctrl_mutex);
1044 seq_puts(s, "registered pin groups:\n");
1045 while (selector < ngroups) {
1046 const unsigned *pins;
1047 unsigned num_pins;
1048 const char *gname = ops->get_group_name(pctldev, selector);
1049 const char *pname;
1050 int ret;
1051 int i;
1053 ret = ops->get_group_pins(pctldev, selector,
1054 &pins, &num_pins);
1055 if (ret)
1056 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1057 gname);
1058 else {
1059 seq_printf(s, "group: %s\n", gname);
1060 for (i = 0; i < num_pins; i++) {
1061 pname = pin_get_name(pctldev, pins[i]);
1062 if (WARN_ON(!pname)) {
1063 mutex_unlock(&pinctrl_mutex);
1064 return -EINVAL;
1066 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1068 seq_puts(s, "\n");
1070 selector++;
1073 mutex_unlock(&pinctrl_mutex);
1075 return 0;
1078 static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1080 struct pinctrl_dev *pctldev = s->private;
1081 struct pinctrl_gpio_range *range = NULL;
1083 seq_puts(s, "GPIO ranges handled:\n");
1085 mutex_lock(&pinctrl_mutex);
1087 /* Loop over the ranges */
1088 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
1089 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1090 range->id, range->name,
1091 range->base, (range->base + range->npins - 1),
1092 range->pin_base,
1093 (range->pin_base + range->npins - 1));
1096 mutex_unlock(&pinctrl_mutex);
1098 return 0;
1101 static int pinctrl_devices_show(struct seq_file *s, void *what)
1103 struct pinctrl_dev *pctldev;
1105 seq_puts(s, "name [pinmux] [pinconf]\n");
1107 mutex_lock(&pinctrl_mutex);
1109 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1110 seq_printf(s, "%s ", pctldev->desc->name);
1111 if (pctldev->desc->pmxops)
1112 seq_puts(s, "yes ");
1113 else
1114 seq_puts(s, "no ");
1115 if (pctldev->desc->confops)
1116 seq_puts(s, "yes");
1117 else
1118 seq_puts(s, "no");
1119 seq_puts(s, "\n");
1122 mutex_unlock(&pinctrl_mutex);
1124 return 0;
1127 static inline const char *map_type(enum pinctrl_map_type type)
1129 static const char * const names[] = {
1130 "INVALID",
1131 "DUMMY_STATE",
1132 "MUX_GROUP",
1133 "CONFIGS_PIN",
1134 "CONFIGS_GROUP",
1137 if (type >= ARRAY_SIZE(names))
1138 return "UNKNOWN";
1140 return names[type];
1143 static int pinctrl_maps_show(struct seq_file *s, void *what)
1145 struct pinctrl_maps *maps_node;
1146 int i;
1147 struct pinctrl_map const *map;
1149 seq_puts(s, "Pinctrl maps:\n");
1151 mutex_lock(&pinctrl_mutex);
1153 for_each_maps(maps_node, i, map) {
1154 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1155 map->dev_name, map->name, map_type(map->type),
1156 map->type);
1158 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1159 seq_printf(s, "controlling device %s\n",
1160 map->ctrl_dev_name);
1162 switch (map->type) {
1163 case PIN_MAP_TYPE_MUX_GROUP:
1164 pinmux_show_map(s, map);
1165 break;
1166 case PIN_MAP_TYPE_CONFIGS_PIN:
1167 case PIN_MAP_TYPE_CONFIGS_GROUP:
1168 pinconf_show_map(s, map);
1169 break;
1170 default:
1171 break;
1174 seq_printf(s, "\n");
1177 mutex_unlock(&pinctrl_mutex);
1179 return 0;
1182 static int pinctrl_show(struct seq_file *s, void *what)
1184 struct pinctrl *p;
1185 struct pinctrl_state *state;
1186 struct pinctrl_setting *setting;
1188 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
1190 mutex_lock(&pinctrl_mutex);
1192 list_for_each_entry(p, &pinctrl_list, node) {
1193 seq_printf(s, "device: %s current state: %s\n",
1194 dev_name(p->dev),
1195 p->state ? p->state->name : "none");
1197 list_for_each_entry(state, &p->states, node) {
1198 seq_printf(s, " state: %s\n", state->name);
1200 list_for_each_entry(setting, &state->settings, node) {
1201 struct pinctrl_dev *pctldev = setting->pctldev;
1203 seq_printf(s, " type: %s controller %s ",
1204 map_type(setting->type),
1205 pinctrl_dev_get_name(pctldev));
1207 switch (setting->type) {
1208 case PIN_MAP_TYPE_MUX_GROUP:
1209 pinmux_show_setting(s, setting);
1210 break;
1211 case PIN_MAP_TYPE_CONFIGS_PIN:
1212 case PIN_MAP_TYPE_CONFIGS_GROUP:
1213 pinconf_show_setting(s, setting);
1214 break;
1215 default:
1216 break;
1222 mutex_unlock(&pinctrl_mutex);
1224 return 0;
1227 static int pinctrl_pins_open(struct inode *inode, struct file *file)
1229 return single_open(file, pinctrl_pins_show, inode->i_private);
1232 static int pinctrl_groups_open(struct inode *inode, struct file *file)
1234 return single_open(file, pinctrl_groups_show, inode->i_private);
1237 static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1239 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1242 static int pinctrl_devices_open(struct inode *inode, struct file *file)
1244 return single_open(file, pinctrl_devices_show, NULL);
1247 static int pinctrl_maps_open(struct inode *inode, struct file *file)
1249 return single_open(file, pinctrl_maps_show, NULL);
1252 static int pinctrl_open(struct inode *inode, struct file *file)
1254 return single_open(file, pinctrl_show, NULL);
1257 static const struct file_operations pinctrl_pins_ops = {
1258 .open = pinctrl_pins_open,
1259 .read = seq_read,
1260 .llseek = seq_lseek,
1261 .release = single_release,
1264 static const struct file_operations pinctrl_groups_ops = {
1265 .open = pinctrl_groups_open,
1266 .read = seq_read,
1267 .llseek = seq_lseek,
1268 .release = single_release,
1271 static const struct file_operations pinctrl_gpioranges_ops = {
1272 .open = pinctrl_gpioranges_open,
1273 .read = seq_read,
1274 .llseek = seq_lseek,
1275 .release = single_release,
1278 static const struct file_operations pinctrl_devices_ops = {
1279 .open = pinctrl_devices_open,
1280 .read = seq_read,
1281 .llseek = seq_lseek,
1282 .release = single_release,
1285 static const struct file_operations pinctrl_maps_ops = {
1286 .open = pinctrl_maps_open,
1287 .read = seq_read,
1288 .llseek = seq_lseek,
1289 .release = single_release,
1292 static const struct file_operations pinctrl_ops = {
1293 .open = pinctrl_open,
1294 .read = seq_read,
1295 .llseek = seq_lseek,
1296 .release = single_release,
1299 static struct dentry *debugfs_root;
1301 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1303 struct dentry *device_root;
1305 device_root = debugfs_create_dir(dev_name(pctldev->dev),
1306 debugfs_root);
1307 pctldev->device_root = device_root;
1309 if (IS_ERR(device_root) || !device_root) {
1310 pr_warn("failed to create debugfs directory for %s\n",
1311 dev_name(pctldev->dev));
1312 return;
1314 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1315 device_root, pctldev, &pinctrl_pins_ops);
1316 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1317 device_root, pctldev, &pinctrl_groups_ops);
1318 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1319 device_root, pctldev, &pinctrl_gpioranges_ops);
1320 pinmux_init_device_debugfs(device_root, pctldev);
1321 pinconf_init_device_debugfs(device_root, pctldev);
1324 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1326 debugfs_remove_recursive(pctldev->device_root);
1329 static void pinctrl_init_debugfs(void)
1331 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1332 if (IS_ERR(debugfs_root) || !debugfs_root) {
1333 pr_warn("failed to create debugfs directory\n");
1334 debugfs_root = NULL;
1335 return;
1338 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1339 debugfs_root, NULL, &pinctrl_devices_ops);
1340 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1341 debugfs_root, NULL, &pinctrl_maps_ops);
1342 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1343 debugfs_root, NULL, &pinctrl_ops);
1346 #else /* CONFIG_DEBUG_FS */
1348 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1352 static void pinctrl_init_debugfs(void)
1356 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1360 #endif
1362 static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1364 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1366 if (!ops ||
1367 !ops->get_groups_count ||
1368 !ops->get_group_name ||
1369 !ops->get_group_pins)
1370 return -EINVAL;
1372 if (ops->dt_node_to_map && !ops->dt_free_map)
1373 return -EINVAL;
1375 return 0;
1379 * pinctrl_register() - register a pin controller device
1380 * @pctldesc: descriptor for this pin controller
1381 * @dev: parent device for this pin controller
1382 * @driver_data: private pin controller data for this pin controller
1384 struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1385 struct device *dev, void *driver_data)
1387 struct pinctrl_dev *pctldev;
1388 int ret;
1390 if (!pctldesc)
1391 return NULL;
1392 if (!pctldesc->name)
1393 return NULL;
1395 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
1396 if (pctldev == NULL) {
1397 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
1398 return NULL;
1401 /* Initialize pin control device struct */
1402 pctldev->owner = pctldesc->owner;
1403 pctldev->desc = pctldesc;
1404 pctldev->driver_data = driver_data;
1405 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
1406 INIT_LIST_HEAD(&pctldev->gpio_ranges);
1407 pctldev->dev = dev;
1409 /* check core ops for sanity */
1410 if (pinctrl_check_ops(pctldev)) {
1411 dev_err(dev, "pinctrl ops lacks necessary functions\n");
1412 goto out_err;
1415 /* If we're implementing pinmuxing, check the ops for sanity */
1416 if (pctldesc->pmxops) {
1417 if (pinmux_check_ops(pctldev))
1418 goto out_err;
1421 /* If we're implementing pinconfig, check the ops for sanity */
1422 if (pctldesc->confops) {
1423 if (pinconf_check_ops(pctldev))
1424 goto out_err;
1427 /* Register all the pins */
1428 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
1429 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1430 if (ret) {
1431 dev_err(dev, "error during pin registration\n");
1432 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1433 pctldesc->npins);
1434 goto out_err;
1437 mutex_lock(&pinctrl_mutex);
1439 list_add_tail(&pctldev->node, &pinctrldev_list);
1441 pctldev->p = pinctrl_get_locked(pctldev->dev);
1442 if (!IS_ERR(pctldev->p)) {
1443 struct pinctrl_state *s =
1444 pinctrl_lookup_state_locked(pctldev->p,
1445 PINCTRL_STATE_DEFAULT);
1446 if (IS_ERR(s)) {
1447 dev_dbg(dev, "failed to lookup the default state\n");
1448 } else {
1449 if (pinctrl_select_state_locked(pctldev->p, s))
1450 dev_err(dev,
1451 "failed to select default state\n");
1455 mutex_unlock(&pinctrl_mutex);
1457 pinctrl_init_device_debugfs(pctldev);
1459 return pctldev;
1461 out_err:
1462 kfree(pctldev);
1463 return NULL;
1465 EXPORT_SYMBOL_GPL(pinctrl_register);
1468 * pinctrl_unregister() - unregister pinmux
1469 * @pctldev: pin controller to unregister
1471 * Called by pinmux drivers to unregister a pinmux.
1473 void pinctrl_unregister(struct pinctrl_dev *pctldev)
1475 struct pinctrl_gpio_range *range, *n;
1476 if (pctldev == NULL)
1477 return;
1479 pinctrl_remove_device_debugfs(pctldev);
1481 mutex_lock(&pinctrl_mutex);
1483 if (!IS_ERR(pctldev->p))
1484 pinctrl_put_locked(pctldev->p, true);
1486 /* TODO: check that no pinmuxes are still active? */
1487 list_del(&pctldev->node);
1488 /* Destroy descriptor tree */
1489 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1490 pctldev->desc->npins);
1491 /* remove gpio ranges map */
1492 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1493 list_del(&range->node);
1495 kfree(pctldev);
1497 mutex_unlock(&pinctrl_mutex);
1499 EXPORT_SYMBOL_GPL(pinctrl_unregister);
1501 static int __init pinctrl_init(void)
1503 pr_info("initialized pinctrl subsystem\n");
1504 pinctrl_init_debugfs();
1505 return 0;
1508 /* init early since many drivers really need to initialized pinmux early */
1509 core_initcall(pinctrl_init);