blob: dc5c126e398a0ac84fc13fa5f564d832ade0831e [file] [log] [blame]
Linus Walleij2744e8a2011-05-02 20:50:54 +02001/*
2 * Core driver for the pin control subsystem
3 *
Linus Walleijbefe5bd2012-02-09 19:47:48 +01004 * Copyright (C) 2011-2012 ST-Ericsson SA
Linus Walleij2744e8a2011-05-02 20:50:54 +02005 * Written on behalf of Linaro for ST-Ericsson
6 * Based on bits of regulator core, gpio core and clk core
7 *
8 * Author: Linus Walleij <linus.walleij@linaro.org>
9 *
Stephen Warrenb2b3e662012-02-19 23:45:43 -070010 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
11 *
Linus Walleij2744e8a2011-05-02 20:50:54 +020012 * License terms: GNU General Public License (GPL) version 2
13 */
14#define pr_fmt(fmt) "pinctrl core: " fmt
15
16#include <linux/kernel.h>
Stephen Rothwella5a697c2011-09-30 14:39:04 +100017#include <linux/export.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020018#include <linux/init.h>
19#include <linux/device.h>
20#include <linux/slab.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020021#include <linux/err.h>
22#include <linux/list.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020023#include <linux/sysfs.h>
24#include <linux/debugfs.h>
25#include <linux/seq_file.h>
Stephen Warren6d4ca1f2012-04-16 10:51:00 -060026#include <linux/pinctrl/consumer.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020027#include <linux/pinctrl/pinctrl.h>
28#include <linux/pinctrl/machine.h>
29#include "core.h"
Stephen Warren57291ce2012-03-23 10:29:46 -060030#include "devicetree.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020031#include "pinmux.h"
Linus Walleijae6b4d82011-10-19 18:14:33 +020032#include "pinconf.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020033
Linus Walleijbefe5bd2012-02-09 19:47:48 +010034/**
Stephen Warrenb2b3e662012-02-19 23:45:43 -070035 * 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
39 */
40struct pinctrl_maps {
41 struct list_head node;
42 struct pinctrl_map const *maps;
43 unsigned num_maps;
44};
45
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080046static bool pinctrl_dummy_state;
47
Stephen Warren57b676f2012-03-02 13:05:44 -070048/* Mutex taken by all entry points */
49DEFINE_MUTEX(pinctrl_mutex);
50
51/* Global list of pin control devices (struct pinctrl_dev) */
Stephen Warren57291ce2012-03-23 10:29:46 -060052LIST_HEAD(pinctrldev_list);
Linus Walleij2744e8a2011-05-02 20:50:54 +020053
Stephen Warren57b676f2012-03-02 13:05:44 -070054/* List of pin controller handles (struct pinctrl) */
Linus Walleijbefe5bd2012-02-09 19:47:48 +010055static LIST_HEAD(pinctrl_list);
56
Stephen Warren57b676f2012-03-02 13:05:44 -070057/* List of pinctrl maps (struct pinctrl_maps) */
Stephen Warrenb2b3e662012-02-19 23:45:43 -070058static LIST_HEAD(pinctrl_maps);
59
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; \
Guennadi Liakhovetskibc664682012-05-23 00:20:17 +020064 _i_++, _map_ = &_maps_node_->maps[_i_])
Linus Walleijbefe5bd2012-02-09 19:47:48 +010065
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080066/**
67 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
68 *
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.
73 */
74void pinctrl_provide_dummies(void)
75{
76 pinctrl_dummy_state = true;
77}
78
Linus Walleij2744e8a2011-05-02 20:50:54 +020079const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
80{
81 /* We're not allowed to register devices without name */
82 return pctldev->desc->name;
83}
84EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
85
86void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
87{
88 return pctldev->driver_data;
89}
90EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
91
92/**
Linus Walleij9dfac4f2012-02-01 18:02:47 +010093 * get_pinctrl_dev_from_devname() - look up pin controller device
94 * @devname: the name of a device instance, as returned by dev_name()
Linus Walleij2744e8a2011-05-02 20:50:54 +020095 *
96 * Looks up a pin control device matching a certain device name or pure device
97 * pointer, the pure device pointer will take precedence.
98 */
Linus Walleij9dfac4f2012-02-01 18:02:47 +010099struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200100{
101 struct pinctrl_dev *pctldev = NULL;
102 bool found = false;
103
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100104 if (!devname)
105 return NULL;
106
Linus Walleij2744e8a2011-05-02 20:50:54 +0200107 list_for_each_entry(pctldev, &pinctrldev_list, node) {
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100108 if (!strcmp(dev_name(pctldev->dev), devname)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200109 /* Matched on device name */
110 found = true;
111 break;
112 }
113 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200114
115 return found ? pctldev : NULL;
116}
117
Linus Walleij2744e8a2011-05-02 20:50:54 +0200118/**
Linus Walleijae6b4d82011-10-19 18:14:33 +0200119 * 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
122 */
123int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
124{
Chanho Park706e8522012-01-03 16:47:50 +0900125 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200126
Chanho Park706e8522012-01-03 16:47:50 +0900127 /* The pin number can be retrived from the pin controller descriptor */
128 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200129 struct pin_desc *desc;
130
Chanho Park706e8522012-01-03 16:47:50 +0900131 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200132 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;
138 }
139
140 return -EINVAL;
141}
142
143/**
Dong Aishengdcb5dbc2012-04-17 15:00:46 +0800144 * 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
147 */
148const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
149{
150 const struct pin_desc *desc;
151
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;
157 }
158
159 return desc->name;
160}
161
162/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200163 * 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
166 *
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.
169 */
170bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
171{
172 struct pin_desc *pindesc;
173
174 if (pin < 0)
175 return false;
176
Stephen Warren57b676f2012-03-02 13:05:44 -0700177 mutex_lock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200178 pindesc = pin_desc_get(pctldev, pin);
Stephen Warren57b676f2012-03-02 13:05:44 -0700179 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200180
Stephen Warren57b676f2012-03-02 13:05:44 -0700181 return pindesc != NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200182}
183EXPORT_SYMBOL_GPL(pin_is_valid);
184
185/* Deletes a range of pin descriptors */
186static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
187 const struct pinctrl_pin_desc *pins,
188 unsigned num_pins)
189{
190 int i;
191
Linus Walleij2744e8a2011-05-02 20:50:54 +0200192 for (i = 0; i < num_pins; i++) {
193 struct pin_desc *pindesc;
194
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);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100200 if (pindesc->dynamic_name)
201 kfree(pindesc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200202 }
203 kfree(pindesc);
204 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200205}
206
207static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
208 unsigned number, const char *name)
209{
210 struct pin_desc *pindesc;
211
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;
217 }
218
219 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700220 if (pindesc == NULL) {
221 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200222 return -ENOMEM;
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700223 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200224
Linus Walleij2744e8a2011-05-02 20:50:54 +0200225 /* Set owner */
226 pindesc->pctldev = pctldev;
227
Stephen Warren9af1e442011-10-19 16:19:27 -0600228 /* Copy basic pin info */
Linus Walleij8dc6ae42012-02-01 18:11:40 +0100229 if (name) {
Linus Walleijca53c5f2011-12-14 20:33:37 +0100230 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;
236 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200237
Linus Walleij2744e8a2011-05-02 20:50:54 +0200238 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200239 pr_debug("registered pin %d (%s) on %s\n",
Linus Walleijca53c5f2011-12-14 20:33:37 +0100240 number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200241 return 0;
242}
243
244static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
245 struct pinctrl_pin_desc const *pins,
246 unsigned num_descs)
247{
248 unsigned i;
249 int ret = 0;
250
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;
256 }
257
258 return 0;
259}
260
261/**
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
265 *
266 * Tries to match a GPIO pin number to the ranges handled by a certain pin
267 * controller, return the range or NULL
268 */
269static struct pinctrl_gpio_range *
270pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
271{
272 struct pinctrl_gpio_range *range = NULL;
273
274 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200275 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) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200279 return range;
280 }
281 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200282
283 return NULL;
284}
285
286/**
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
291 *
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
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800294 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
295 * may still have not been registered.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200296 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700297static int pinctrl_get_device_gpio_range(unsigned gpio,
298 struct pinctrl_dev **outdev,
299 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200300{
301 struct pinctrl_dev *pctldev = NULL;
302
303 /* Loop over the pin controllers */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200304 list_for_each_entry(pctldev, &pinctrldev_list, node) {
305 struct pinctrl_gpio_range *range;
306
307 range = pinctrl_match_gpio_range(pctldev, gpio);
308 if (range != NULL) {
309 *outdev = pctldev;
310 *outrange = range;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200311 return 0;
312 }
313 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200314
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800315 return -EPROBE_DEFER;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200316}
317
318/**
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
322 *
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.
325 */
326void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
327 struct pinctrl_gpio_range *range)
328{
Stephen Warren57b676f2012-03-02 13:05:44 -0700329 mutex_lock(&pinctrl_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700330 list_add_tail(&range->node, &pctldev->gpio_ranges);
Stephen Warren57b676f2012-03-02 13:05:44 -0700331 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200332}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700333EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200334
Dong Aisheng3e5e00b2012-05-23 21:22:41 +0800335void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
336 struct pinctrl_gpio_range *ranges,
337 unsigned nranges)
338{
339 int i;
340
341 for (i = 0; i < nranges; i++)
342 pinctrl_add_gpio_range(pctldev, &ranges[i]);
343}
344EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
345
Linus Walleij2744e8a2011-05-02 20:50:54 +0200346/**
Linus Walleij7afde8b2011-10-19 17:07:16 +0200347 * 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
350 */
351int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
352 const char *pin_group)
353{
354 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530355 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200356 unsigned group_selector = 0;
357
Viresh Kumard1e90e92012-03-30 11:25:40 +0530358 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200359 const char *gname = pctlops->get_group_name(pctldev,
360 group_selector);
361 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700362 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200363 "found group selector %u for %s\n",
364 group_selector,
365 pin_group);
366 return group_selector;
367 }
368
369 group_selector++;
370 }
371
Stephen Warren51cd24e2011-12-09 16:59:05 -0700372 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200373 pin_group);
374
375 return -EINVAL;
376}
377
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100378/**
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
381 *
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.
385 */
386int pinctrl_request_gpio(unsigned gpio)
387{
388 struct pinctrl_dev *pctldev;
389 struct pinctrl_gpio_range *range;
390 int ret;
391 int pin;
392
Stephen Warren57b676f2012-03-02 13:05:44 -0700393 mutex_lock(&pinctrl_mutex);
394
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100395 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700396 if (ret) {
397 mutex_unlock(&pinctrl_mutex);
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800398 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700399 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100400
401 /* Convert to the pin controllers number space */
402 pin = gpio - range->base + range->pin_base;
403
Stephen Warren57b676f2012-03-02 13:05:44 -0700404 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
405
406 mutex_unlock(&pinctrl_mutex);
407 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100408}
409EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
410
411/**
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
414 *
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.
418 */
419void pinctrl_free_gpio(unsigned gpio)
420{
421 struct pinctrl_dev *pctldev;
422 struct pinctrl_gpio_range *range;
423 int ret;
424 int pin;
425
Stephen Warren57b676f2012-03-02 13:05:44 -0700426 mutex_lock(&pinctrl_mutex);
427
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100428 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700429 if (ret) {
430 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100431 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700432 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100433
434 /* Convert to the pin controllers number space */
435 pin = gpio - range->base + range->pin_base;
436
Stephen Warren57b676f2012-03-02 13:05:44 -0700437 pinmux_free_gpio(pctldev, pin, range);
438
439 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100440}
441EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
442
443static int pinctrl_gpio_direction(unsigned gpio, bool input)
444{
445 struct pinctrl_dev *pctldev;
446 struct pinctrl_gpio_range *range;
447 int ret;
448 int pin;
449
450 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
451 if (ret)
452 return ret;
453
454 /* Convert to the pin controllers number space */
455 pin = gpio - range->base + range->pin_base;
456
457 return pinmux_gpio_direction(pctldev, range, pin, input);
458}
459
460/**
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
463 *
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.
467 */
468int pinctrl_gpio_direction_input(unsigned gpio)
469{
Stephen Warren57b676f2012-03-02 13:05:44 -0700470 int ret;
471 mutex_lock(&pinctrl_mutex);
472 ret = pinctrl_gpio_direction(gpio, true);
473 mutex_unlock(&pinctrl_mutex);
474 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100475}
476EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
477
478/**
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
481 *
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.
485 */
486int pinctrl_gpio_direction_output(unsigned gpio)
487{
Stephen Warren57b676f2012-03-02 13:05:44 -0700488 int ret;
489 mutex_lock(&pinctrl_mutex);
490 ret = pinctrl_gpio_direction(gpio, false);
491 mutex_unlock(&pinctrl_mutex);
492 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100493}
494EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
495
Stephen Warren6e5e9592012-03-02 13:05:47 -0700496static struct pinctrl_state *find_state(struct pinctrl *p,
497 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100498{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700499 struct pinctrl_state *state;
500
501 list_for_each_entry(state, &p->states, node)
502 if (!strcmp(state->name, name))
503 return state;
504
505 return NULL;
506}
507
508static struct pinctrl_state *create_state(struct pinctrl *p,
509 const char *name)
510{
511 struct pinctrl_state *state;
512
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);
518 }
519
520 state->name = name;
521 INIT_LIST_HEAD(&state->settings);
522
523 list_add_tail(&state->node, &p->states);
524
525 return state;
526}
527
528static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
529{
530 struct pinctrl_state *state;
531 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700532 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700533
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);
539
Stephen Warren1e2082b2012-03-02 13:05:48 -0700540 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
541 return 0;
542
Stephen Warren6e5e9592012-03-02 13:05:47 -0700543 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;
548 }
549
Stephen Warren1e2082b2012-03-02 13:05:48 -0700550 setting->type = map->type;
551
Stephen Warren6e5e9592012-03-02 13:05:47 -0700552 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
553 if (setting->pctldev == NULL) {
Linus Walleijc05127c2012-04-10 10:00:38 +0200554 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
Stephen Warren6e5e9592012-03-02 13:05:47 -0700555 map->ctrl_dev_name);
556 kfree(setting);
Linus Walleijc05127c2012-04-10 10:00:38 +0200557 /*
558 * OK let us guess that the driver is not there yet, and
559 * let's defer obtaining this pinctrl handle to later...
560 */
561 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700562 }
563
Stephen Warren1e2082b2012-03-02 13:05:48 -0700564 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;
575 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700576 if (ret < 0) {
577 kfree(setting);
578 return ret;
579 }
580
581 list_add_tail(&setting->node, &state->settings);
582
583 return 0;
584}
585
586static struct pinctrl *find_pinctrl(struct device *dev)
587{
588 struct pinctrl *p;
589
Stephen Warren1e2082b2012-03-02 13:05:48 -0700590 list_for_each_entry(p, &pinctrl_list, node)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700591 if (p->dev == dev)
592 return p;
593
594 return NULL;
595}
596
597static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
598
599static struct pinctrl *create_pinctrl(struct device *dev)
600{
601 struct pinctrl *p;
602 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700603 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100604 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700605 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700606 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100607
608 /*
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()
612 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700613 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700614 if (p == NULL) {
615 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100616 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700617 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700618 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700619 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -0600620 INIT_LIST_HEAD(&p->dt_maps);
621
622 ret = pinctrl_dt_to_map(p);
623 if (ret < 0) {
624 kfree(p);
625 return ERR_PTR(ret);
626 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700627
628 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100629
630 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700631 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -0700632 /* Map must be for this device */
633 if (strcmp(map->dev_name, devname))
634 continue;
635
Stephen Warren6e5e9592012-03-02 13:05:47 -0700636 ret = add_setting(p, map);
637 if (ret < 0) {
638 pinctrl_put_locked(p, false);
639 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100640 }
641 }
642
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100643 /* Add the pinmux to the global list */
Stephen Warren8b9c1392012-02-19 23:45:42 -0700644 list_add_tail(&p->node, &pinctrl_list);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100645
646 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700647}
Stephen Warren7ecdb162012-03-02 13:05:45 -0700648
Stephen Warren6e5e9592012-03-02 13:05:47 -0700649static struct pinctrl *pinctrl_get_locked(struct device *dev)
650{
651 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700652
Stephen Warren6e5e9592012-03-02 13:05:47 -0700653 if (WARN_ON(!dev))
654 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700655
Stephen Warren6e5e9592012-03-02 13:05:47 -0700656 p = find_pinctrl(dev);
657 if (p != NULL)
658 return ERR_PTR(-EBUSY);
659
Richard Genoudd599bfb2012-08-10 16:53:49 +0200660 return create_pinctrl(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100661}
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700662
663/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700664 * pinctrl_get() - retrieves the pinctrl handle for a device
665 * @dev: the device to obtain the handle for
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700666 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700667struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700668{
669 struct pinctrl *p;
670
Stephen Warren57b676f2012-03-02 13:05:44 -0700671 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700672 p = pinctrl_get_locked(dev);
Stephen Warren57b676f2012-03-02 13:05:44 -0700673 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700674
675 return p;
676}
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100677EXPORT_SYMBOL_GPL(pinctrl_get);
678
Stephen Warren6e5e9592012-03-02 13:05:47 -0700679static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -0700680{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700681 struct pinctrl_state *state, *n1;
682 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700683
Stephen Warren6e5e9592012-03-02 13:05:47 -0700684 list_for_each_entry_safe(state, n1, &p->states, node) {
685 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700686 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;
698 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700699 list_del(&setting->node);
700 kfree(setting);
701 }
702 list_del(&state->node);
703 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700704 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700705
Stephen Warren57291ce2012-03-23 10:29:46 -0600706 pinctrl_dt_free_maps(p);
707
Stephen Warren6e5e9592012-03-02 13:05:47 -0700708 if (inlist)
709 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700710 kfree(p);
711}
712
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100713/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700714 * pinctrl_put() - release a previously claimed pinctrl handle
715 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100716 */
717void pinctrl_put(struct pinctrl *p)
718{
Stephen Warren57b676f2012-03-02 13:05:44 -0700719 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700720 pinctrl_put_locked(p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -0700721 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100722}
723EXPORT_SYMBOL_GPL(pinctrl_put);
724
Stephen Warren6e5e9592012-03-02 13:05:47 -0700725static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
726 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -0700727{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700728 struct pinctrl_state *state;
729
730 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800731 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);
Richard Genoudd599bfb2012-08-10 16:53:49 +0200737 } else
738 state = ERR_PTR(-ENODEV);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800739 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700740
741 return state;
742}
743
744/**
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
748 */
749struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
750{
751 struct pinctrl_state *s;
752
753 mutex_lock(&pinctrl_mutex);
754 s = pinctrl_lookup_state_locked(p, name);
755 mutex_unlock(&pinctrl_mutex);
756
757 return s;
758}
759EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
760
761static int pinctrl_select_state_locked(struct pinctrl *p,
762 struct pinctrl_state *state)
763{
764 struct pinctrl_setting *setting, *setting2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700765 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700766
Stephen Warren6e5e9592012-03-02 13:05:47 -0700767 if (p->state == state)
768 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700769
Stephen Warren6e5e9592012-03-02 13:05:47 -0700770 if (p->state) {
771 /*
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.
779 */
780 list_for_each_entry(setting, &p->state->settings, node) {
781 bool found = false;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700782 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
783 continue;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700784 list_for_each_entry(setting2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700785 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
786 continue;
787 if (setting2->data.mux.group ==
788 setting->data.mux.group) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700789 found = true;
790 break;
791 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700792 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700793 if (!found)
794 pinmux_disable_setting(setting);
795 }
796 }
797
798 p->state = state;
799
800 /* Apply all the settings for the new state */
801 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700802 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;
813 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700814 if (ret < 0) {
815 /* FIXME: Difficult to return to prev state */
816 return ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700817 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700818 }
819
Stephen Warren7ecdb162012-03-02 13:05:45 -0700820 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700821}
822
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100823/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700824 * 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
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100827 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700828int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100829{
Stephen Warren57b676f2012-03-02 13:05:44 -0700830 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700831
Stephen Warren57b676f2012-03-02 13:05:44 -0700832 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700833 ret = pinctrl_select_state_locked(p, state);
Stephen Warren57b676f2012-03-02 13:05:44 -0700834 mutex_unlock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700835
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100836 return ret;
837}
Stephen Warren6e5e9592012-03-02 13:05:47 -0700838EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100839
Stephen Warren6d4ca1f2012-04-16 10:51:00 -0600840static void devm_pinctrl_release(struct device *dev, void *res)
841{
842 pinctrl_put(*(struct pinctrl **)res);
843}
844
845/**
846 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
847 * @dev: the device to obtain the handle for
848 *
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().
851 */
852struct pinctrl *devm_pinctrl_get(struct device *dev)
853{
854 struct pinctrl **ptr, *p;
855
856 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
857 if (!ptr)
858 return ERR_PTR(-ENOMEM);
859
860 p = pinctrl_get(dev);
861 if (!IS_ERR(p)) {
862 *ptr = p;
863 devres_add(dev, ptr);
864 } else {
865 devres_free(ptr);
866 }
867
868 return p;
869}
870EXPORT_SYMBOL_GPL(devm_pinctrl_get);
871
872static int devm_pinctrl_match(struct device *dev, void *res, void *data)
873{
874 struct pinctrl **p = res;
875
876 return *p == data;
877}
878
879/**
880 * devm_pinctrl_put() - Resource managed pinctrl_put()
881 * @p: the pinctrl handle to release
882 *
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.
886 */
887void devm_pinctrl_put(struct pinctrl *p)
888{
889 WARN_ON(devres_destroy(p->dev, devm_pinctrl_release,
890 devm_pinctrl_match, p));
891 pinctrl_put(p);
892}
893EXPORT_SYMBOL_GPL(devm_pinctrl_put);
894
Stephen Warren57291ce2012-03-23 10:29:46 -0600895int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
896 bool dup, bool locked)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100897{
Stephen Warren1e2082b2012-03-02 13:05:48 -0700898 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700899 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100900
901 pr_debug("add %d pinmux maps\n", num_maps);
902
903 /* First sanity check the new mapping */
904 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700905 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;
909 }
910
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100911 if (!maps[i].name) {
912 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700913 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100914 return -EINVAL;
915 }
916
Stephen Warren1e2082b2012-03-02 13:05:48 -0700917 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
918 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100919 pr_err("failed to register map %s (%d): no pin control device given\n",
920 maps[i].name, i);
921 return -EINVAL;
922 }
923
Stephen Warren1e2082b2012-03-02 13:05:48 -0700924 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)
Stephen Warrenfde04f42012-04-25 10:32:16 -0600930 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700931 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)
Stephen Warrenfde04f42012-04-25 10:32:16 -0600936 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700937 break;
938 default:
939 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700940 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -0700941 return -EINVAL;
942 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100943 }
944
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700945 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;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100949 }
950
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700951 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -0600952 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;
959 }
960 } else {
961 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700962 }
963
Stephen Warren57291ce2012-03-23 10:29:46 -0600964 if (!locked)
965 mutex_lock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700966 list_add_tail(&maps_node->node, &pinctrl_maps);
Stephen Warren57291ce2012-03-23 10:29:46 -0600967 if (!locked)
968 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700969
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100970 return 0;
971}
972
Stephen Warren57291ce2012-03-23 10:29:46 -0600973/**
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
979 */
980int pinctrl_register_mappings(struct pinctrl_map const *maps,
981 unsigned num_maps)
982{
983 return pinctrl_register_map(maps, num_maps, true, false);
984}
985
986void pinctrl_unregister_map(struct pinctrl_map const *map)
987{
988 struct pinctrl_maps *maps_node;
989
990 list_for_each_entry(maps_node, &pinctrl_maps, node) {
991 if (maps_node->maps == map) {
992 list_del(&maps_node->node);
993 return;
994 }
995 }
996}
997
Linus Walleij2744e8a2011-05-02 20:50:54 +0200998#ifdef CONFIG_DEBUG_FS
999
1000static int pinctrl_pins_show(struct seq_file *s, void *what)
1001{
1002 struct pinctrl_dev *pctldev = s->private;
1003 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001004 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001005
1006 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001007
Stephen Warren57b676f2012-03-02 13:05:44 -07001008 mutex_lock(&pinctrl_mutex);
1009
Chanho Park706e8522012-01-03 16:47:50 +09001010 /* The pin number can be retrived from the pin controller descriptor */
1011 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001012 struct pin_desc *desc;
1013
Chanho Park706e8522012-01-03 16:47:50 +09001014 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001015 desc = pin_desc_get(pctldev, pin);
1016 /* Pin space may be sparse */
1017 if (desc == NULL)
1018 continue;
1019
1020 seq_printf(s, "pin %d (%s) ", pin,
1021 desc->name ? desc->name : "unnamed");
1022
1023 /* Driver-specific info per pin */
1024 if (ops->pin_dbg_show)
1025 ops->pin_dbg_show(pctldev, s, pin);
1026
1027 seq_puts(s, "\n");
1028 }
1029
Stephen Warren57b676f2012-03-02 13:05:44 -07001030 mutex_unlock(&pinctrl_mutex);
1031
Linus Walleij2744e8a2011-05-02 20:50:54 +02001032 return 0;
1033}
1034
1035static int pinctrl_groups_show(struct seq_file *s, void *what)
1036{
1037 struct pinctrl_dev *pctldev = s->private;
1038 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301039 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001040
Viresh Kumard1e90e92012-03-30 11:25:40 +05301041 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001042 mutex_lock(&pinctrl_mutex);
1043
Linus Walleij2744e8a2011-05-02 20:50:54 +02001044 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301045 while (selector < ngroups) {
Stephen Warrena5818a82011-10-19 16:19:25 -06001046 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001047 unsigned num_pins;
1048 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001049 const char *pname;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001050 int ret;
1051 int i;
1052
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 {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001059 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 return -EINVAL;
1064 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1065 }
1066 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001067 }
1068 selector++;
1069 }
1070
Stephen Warren57b676f2012-03-02 13:05:44 -07001071 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001072
1073 return 0;
1074}
1075
1076static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1077{
1078 struct pinctrl_dev *pctldev = s->private;
1079 struct pinctrl_gpio_range *range = NULL;
1080
1081 seq_puts(s, "GPIO ranges handled:\n");
1082
Stephen Warren57b676f2012-03-02 13:05:44 -07001083 mutex_lock(&pinctrl_mutex);
1084
Linus Walleij2744e8a2011-05-02 20:50:54 +02001085 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001086 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Linus Walleij75d66422011-11-16 09:58:51 +01001087 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1088 range->id, range->name,
1089 range->base, (range->base + range->npins - 1),
1090 range->pin_base,
1091 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001092 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001093
1094 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001095
1096 return 0;
1097}
1098
1099static int pinctrl_devices_show(struct seq_file *s, void *what)
1100{
1101 struct pinctrl_dev *pctldev;
1102
Linus Walleijae6b4d82011-10-19 18:14:33 +02001103 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001104
1105 mutex_lock(&pinctrl_mutex);
1106
Linus Walleij2744e8a2011-05-02 20:50:54 +02001107 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1108 seq_printf(s, "%s ", pctldev->desc->name);
1109 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001110 seq_puts(s, "yes ");
1111 else
1112 seq_puts(s, "no ");
1113 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001114 seq_puts(s, "yes");
1115 else
1116 seq_puts(s, "no");
1117 seq_puts(s, "\n");
1118 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001119
1120 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001121
1122 return 0;
1123}
1124
Stephen Warren1e2082b2012-03-02 13:05:48 -07001125static inline const char *map_type(enum pinctrl_map_type type)
1126{
1127 static const char * const names[] = {
1128 "INVALID",
1129 "DUMMY_STATE",
1130 "MUX_GROUP",
1131 "CONFIGS_PIN",
1132 "CONFIGS_GROUP",
1133 };
1134
1135 if (type >= ARRAY_SIZE(names))
1136 return "UNKNOWN";
1137
1138 return names[type];
1139}
1140
Stephen Warren3eedb432012-02-23 17:04:40 -07001141static int pinctrl_maps_show(struct seq_file *s, void *what)
1142{
1143 struct pinctrl_maps *maps_node;
1144 int i;
1145 struct pinctrl_map const *map;
1146
1147 seq_puts(s, "Pinctrl maps:\n");
1148
Stephen Warren57b676f2012-03-02 13:05:44 -07001149 mutex_lock(&pinctrl_mutex);
1150
Stephen Warren3eedb432012-02-23 17:04:40 -07001151 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001152 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1153 map->dev_name, map->name, map_type(map->type),
1154 map->type);
1155
1156 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1157 seq_printf(s, "controlling device %s\n",
1158 map->ctrl_dev_name);
1159
1160 switch (map->type) {
1161 case PIN_MAP_TYPE_MUX_GROUP:
1162 pinmux_show_map(s, map);
1163 break;
1164 case PIN_MAP_TYPE_CONFIGS_PIN:
1165 case PIN_MAP_TYPE_CONFIGS_GROUP:
1166 pinconf_show_map(s, map);
1167 break;
1168 default:
1169 break;
1170 }
1171
1172 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001173 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001174
1175 mutex_unlock(&pinctrl_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001176
1177 return 0;
1178}
1179
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001180static int pinctrl_show(struct seq_file *s, void *what)
1181{
1182 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001183 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001184 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001185
1186 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001187
1188 mutex_lock(&pinctrl_mutex);
1189
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001190 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001191 seq_printf(s, "device: %s current state: %s\n",
1192 dev_name(p->dev),
1193 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001194
Stephen Warren6e5e9592012-03-02 13:05:47 -07001195 list_for_each_entry(state, &p->states, node) {
1196 seq_printf(s, " state: %s\n", state->name);
1197
1198 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001199 struct pinctrl_dev *pctldev = setting->pctldev;
1200
1201 seq_printf(s, " type: %s controller %s ",
1202 map_type(setting->type),
1203 pinctrl_dev_get_name(pctldev));
1204
1205 switch (setting->type) {
1206 case PIN_MAP_TYPE_MUX_GROUP:
1207 pinmux_show_setting(s, setting);
1208 break;
1209 case PIN_MAP_TYPE_CONFIGS_PIN:
1210 case PIN_MAP_TYPE_CONFIGS_GROUP:
1211 pinconf_show_setting(s, setting);
1212 break;
1213 default:
1214 break;
1215 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001216 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001217 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001218 }
1219
Stephen Warren57b676f2012-03-02 13:05:44 -07001220 mutex_unlock(&pinctrl_mutex);
1221
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001222 return 0;
1223}
1224
Linus Walleij2744e8a2011-05-02 20:50:54 +02001225static int pinctrl_pins_open(struct inode *inode, struct file *file)
1226{
1227 return single_open(file, pinctrl_pins_show, inode->i_private);
1228}
1229
1230static int pinctrl_groups_open(struct inode *inode, struct file *file)
1231{
1232 return single_open(file, pinctrl_groups_show, inode->i_private);
1233}
1234
1235static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1236{
1237 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1238}
1239
1240static int pinctrl_devices_open(struct inode *inode, struct file *file)
1241{
1242 return single_open(file, pinctrl_devices_show, NULL);
1243}
1244
Stephen Warren3eedb432012-02-23 17:04:40 -07001245static int pinctrl_maps_open(struct inode *inode, struct file *file)
1246{
1247 return single_open(file, pinctrl_maps_show, NULL);
1248}
1249
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001250static int pinctrl_open(struct inode *inode, struct file *file)
1251{
1252 return single_open(file, pinctrl_show, NULL);
1253}
1254
Linus Walleij2744e8a2011-05-02 20:50:54 +02001255static const struct file_operations pinctrl_pins_ops = {
1256 .open = pinctrl_pins_open,
1257 .read = seq_read,
1258 .llseek = seq_lseek,
1259 .release = single_release,
1260};
1261
1262static const struct file_operations pinctrl_groups_ops = {
1263 .open = pinctrl_groups_open,
1264 .read = seq_read,
1265 .llseek = seq_lseek,
1266 .release = single_release,
1267};
1268
1269static const struct file_operations pinctrl_gpioranges_ops = {
1270 .open = pinctrl_gpioranges_open,
1271 .read = seq_read,
1272 .llseek = seq_lseek,
1273 .release = single_release,
1274};
1275
1276static const struct file_operations pinctrl_devices_ops = {
1277 .open = pinctrl_devices_open,
1278 .read = seq_read,
1279 .llseek = seq_lseek,
1280 .release = single_release,
1281};
1282
Stephen Warren3eedb432012-02-23 17:04:40 -07001283static const struct file_operations pinctrl_maps_ops = {
1284 .open = pinctrl_maps_open,
1285 .read = seq_read,
1286 .llseek = seq_lseek,
1287 .release = single_release,
1288};
1289
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001290static const struct file_operations pinctrl_ops = {
1291 .open = pinctrl_open,
1292 .read = seq_read,
1293 .llseek = seq_lseek,
1294 .release = single_release,
1295};
1296
Linus Walleij2744e8a2011-05-02 20:50:54 +02001297static struct dentry *debugfs_root;
1298
1299static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1300{
Tony Lindgren02157162012-01-20 08:17:22 -08001301 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001302
Stephen Warren51cd24e2011-12-09 16:59:05 -07001303 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001304 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001305 pctldev->device_root = device_root;
1306
Linus Walleij2744e8a2011-05-02 20:50:54 +02001307 if (IS_ERR(device_root) || !device_root) {
1308 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001309 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001310 return;
1311 }
1312 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1313 device_root, pctldev, &pinctrl_pins_ops);
1314 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1315 device_root, pctldev, &pinctrl_groups_ops);
1316 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1317 device_root, pctldev, &pinctrl_gpioranges_ops);
1318 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +02001319 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001320}
1321
Tony Lindgren02157162012-01-20 08:17:22 -08001322static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1323{
1324 debugfs_remove_recursive(pctldev->device_root);
1325}
1326
Linus Walleij2744e8a2011-05-02 20:50:54 +02001327static void pinctrl_init_debugfs(void)
1328{
1329 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1330 if (IS_ERR(debugfs_root) || !debugfs_root) {
1331 pr_warn("failed to create debugfs directory\n");
1332 debugfs_root = NULL;
1333 return;
1334 }
1335
1336 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1337 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001338 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1339 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001340 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1341 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001342}
1343
1344#else /* CONFIG_DEBUG_FS */
1345
1346static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1347{
1348}
1349
1350static void pinctrl_init_debugfs(void)
1351{
1352}
1353
Tony Lindgren02157162012-01-20 08:17:22 -08001354static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1355{
1356}
1357
Linus Walleij2744e8a2011-05-02 20:50:54 +02001358#endif
1359
Stephen Warrend26bc492012-03-16 14:54:25 -06001360static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1361{
1362 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1363
1364 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301365 !ops->get_groups_count ||
Stephen Warrend26bc492012-03-16 14:54:25 -06001366 !ops->get_group_name ||
1367 !ops->get_group_pins)
1368 return -EINVAL;
1369
Stephen Warren57291ce2012-03-23 10:29:46 -06001370 if (ops->dt_node_to_map && !ops->dt_free_map)
1371 return -EINVAL;
1372
Stephen Warrend26bc492012-03-16 14:54:25 -06001373 return 0;
1374}
1375
Linus Walleij2744e8a2011-05-02 20:50:54 +02001376/**
1377 * pinctrl_register() - register a pin controller device
1378 * @pctldesc: descriptor for this pin controller
1379 * @dev: parent device for this pin controller
1380 * @driver_data: private pin controller data for this pin controller
1381 */
1382struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1383 struct device *dev, void *driver_data)
1384{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001385 struct pinctrl_dev *pctldev;
1386 int ret;
1387
Devendra Nagada9aecb2012-06-16 23:43:16 +05301388 if (!pctldesc)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001389 return NULL;
Devendra Nagada9aecb2012-06-16 23:43:16 +05301390 if (!pctldesc->name)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001391 return NULL;
1392
Stephen Warren02f5b982012-02-22 14:26:00 -07001393 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001394 if (pctldev == NULL) {
1395 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001396 return NULL;
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001397 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001398
1399 /* Initialize pin control device struct */
1400 pctldev->owner = pctldesc->owner;
1401 pctldev->desc = pctldesc;
1402 pctldev->driver_data = driver_data;
1403 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001404 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001405 pctldev->dev = dev;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001406
Stephen Warrend26bc492012-03-16 14:54:25 -06001407 /* check core ops for sanity */
Devendra Nagada9aecb2012-06-16 23:43:16 +05301408 if (pinctrl_check_ops(pctldev)) {
John Crispinad6e1102012-04-26 16:47:11 +02001409 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06001410 goto out_err;
1411 }
1412
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001413 /* If we're implementing pinmuxing, check the ops for sanity */
1414 if (pctldesc->pmxops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301415 if (pinmux_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001416 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001417 }
1418
1419 /* If we're implementing pinconfig, check the ops for sanity */
1420 if (pctldesc->confops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301421 if (pinconf_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001422 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001423 }
1424
Linus Walleij2744e8a2011-05-02 20:50:54 +02001425 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02001426 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001427 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1428 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001429 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001430 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1431 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001432 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001433 }
1434
Stephen Warren57b676f2012-03-02 13:05:44 -07001435 mutex_lock(&pinctrl_mutex);
1436
Stephen Warren8b9c1392012-02-19 23:45:42 -07001437 list_add_tail(&pctldev->node, &pinctrldev_list);
Stephen Warren57b676f2012-03-02 13:05:44 -07001438
Stephen Warren6e5e9592012-03-02 13:05:47 -07001439 pctldev->p = pinctrl_get_locked(pctldev->dev);
1440 if (!IS_ERR(pctldev->p)) {
1441 struct pinctrl_state *s =
1442 pinctrl_lookup_state_locked(pctldev->p,
1443 PINCTRL_STATE_DEFAULT);
John Crispinad6e1102012-04-26 16:47:11 +02001444 if (IS_ERR(s)) {
1445 dev_dbg(dev, "failed to lookup the default state\n");
1446 } else {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301447 if (pinctrl_select_state_locked(pctldev->p, s))
John Crispinad6e1102012-04-26 16:47:11 +02001448 dev_err(dev,
1449 "failed to select default state\n");
John Crispinad6e1102012-04-26 16:47:11 +02001450 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001451 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001452
1453 mutex_unlock(&pinctrl_mutex);
1454
Stephen Warren2304b472012-02-22 14:26:01 -07001455 pinctrl_init_device_debugfs(pctldev);
1456
Linus Walleij2744e8a2011-05-02 20:50:54 +02001457 return pctldev;
1458
Stephen Warren51cd24e2011-12-09 16:59:05 -07001459out_err:
1460 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001461 return NULL;
1462}
1463EXPORT_SYMBOL_GPL(pinctrl_register);
1464
1465/**
1466 * pinctrl_unregister() - unregister pinmux
1467 * @pctldev: pin controller to unregister
1468 *
1469 * Called by pinmux drivers to unregister a pinmux.
1470 */
1471void pinctrl_unregister(struct pinctrl_dev *pctldev)
1472{
Dong Aisheng5d589b02012-05-23 21:22:40 +08001473 struct pinctrl_gpio_range *range, *n;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001474 if (pctldev == NULL)
1475 return;
1476
Tony Lindgren02157162012-01-20 08:17:22 -08001477 pinctrl_remove_device_debugfs(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001478
1479 mutex_lock(&pinctrl_mutex);
1480
Stephen Warren6e5e9592012-03-02 13:05:47 -07001481 if (!IS_ERR(pctldev->p))
1482 pinctrl_put_locked(pctldev->p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -07001483
Linus Walleij2744e8a2011-05-02 20:50:54 +02001484 /* TODO: check that no pinmuxes are still active? */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001485 list_del(&pctldev->node);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001486 /* Destroy descriptor tree */
1487 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1488 pctldev->desc->npins);
Dong Aisheng5d589b02012-05-23 21:22:40 +08001489 /* remove gpio ranges map */
1490 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1491 list_del(&range->node);
1492
Stephen Warren51cd24e2011-12-09 16:59:05 -07001493 kfree(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001494
1495 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001496}
1497EXPORT_SYMBOL_GPL(pinctrl_unregister);
1498
1499static int __init pinctrl_init(void)
1500{
1501 pr_info("initialized pinctrl subsystem\n");
1502 pinctrl_init_debugfs();
1503 return 0;
1504}
1505
1506/* init early since many drivers really need to initialized pinmux early */
1507core_initcall(pinctrl_init);