blob: a6386b3a3c157c2dbd01573c713b6753a1a1c972 [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; \
64 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
294 * negative if the GPIO range could not be found in any device.
295 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700296static int pinctrl_get_device_gpio_range(unsigned gpio,
297 struct pinctrl_dev **outdev,
298 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200299{
300 struct pinctrl_dev *pctldev = NULL;
301
302 /* Loop over the pin controllers */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200303 list_for_each_entry(pctldev, &pinctrldev_list, node) {
304 struct pinctrl_gpio_range *range;
305
306 range = pinctrl_match_gpio_range(pctldev, gpio);
307 if (range != NULL) {
308 *outdev = pctldev;
309 *outrange = range;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200310 return 0;
311 }
312 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200313
314 return -EINVAL;
315}
316
317/**
318 * pinctrl_add_gpio_range() - register a GPIO range for a controller
319 * @pctldev: pin controller device to add the range to
320 * @range: the GPIO range to add
321 *
322 * This adds a range of GPIOs to be handled by a certain pin controller. Call
323 * this to register handled ranges after registering your pin controller.
324 */
325void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
326 struct pinctrl_gpio_range *range)
327{
Stephen Warren57b676f2012-03-02 13:05:44 -0700328 mutex_lock(&pinctrl_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700329 list_add_tail(&range->node, &pctldev->gpio_ranges);
Stephen Warren57b676f2012-03-02 13:05:44 -0700330 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200331}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700332EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200333
334/**
335 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
336 * @pctldev: pin controller device to remove the range from
337 * @range: the GPIO range to remove
338 */
339void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
340 struct pinctrl_gpio_range *range)
341{
Stephen Warren57b676f2012-03-02 13:05:44 -0700342 mutex_lock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200343 list_del(&range->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700344 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200345}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700346EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200347
Linus Walleij7afde8b2011-10-19 17:07:16 +0200348/**
349 * pinctrl_get_group_selector() - returns the group selector for a group
350 * @pctldev: the pin controller handling the group
351 * @pin_group: the pin group to look up
352 */
353int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
354 const char *pin_group)
355{
356 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530357 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200358 unsigned group_selector = 0;
359
Viresh Kumard1e90e92012-03-30 11:25:40 +0530360 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200361 const char *gname = pctlops->get_group_name(pctldev,
362 group_selector);
363 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700364 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200365 "found group selector %u for %s\n",
366 group_selector,
367 pin_group);
368 return group_selector;
369 }
370
371 group_selector++;
372 }
373
Stephen Warren51cd24e2011-12-09 16:59:05 -0700374 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200375 pin_group);
376
377 return -EINVAL;
378}
379
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100380/**
381 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
382 * @gpio: the GPIO pin number from the GPIO subsystem number space
383 *
384 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
385 * as part of their gpio_request() semantics, platforms and individual drivers
386 * shall *NOT* request GPIO pins to be muxed in.
387 */
388int pinctrl_request_gpio(unsigned gpio)
389{
390 struct pinctrl_dev *pctldev;
391 struct pinctrl_gpio_range *range;
392 int ret;
393 int pin;
394
Stephen Warren57b676f2012-03-02 13:05:44 -0700395 mutex_lock(&pinctrl_mutex);
396
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100397 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700398 if (ret) {
399 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100400 return -EINVAL;
Stephen Warren57b676f2012-03-02 13:05:44 -0700401 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100402
403 /* Convert to the pin controllers number space */
404 pin = gpio - range->base + range->pin_base;
405
Stephen Warren57b676f2012-03-02 13:05:44 -0700406 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
407
408 mutex_unlock(&pinctrl_mutex);
409 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100410}
411EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
412
413/**
414 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
415 * @gpio: the GPIO pin number from the GPIO subsystem number space
416 *
417 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
418 * as part of their gpio_free() semantics, platforms and individual drivers
419 * shall *NOT* request GPIO pins to be muxed out.
420 */
421void pinctrl_free_gpio(unsigned gpio)
422{
423 struct pinctrl_dev *pctldev;
424 struct pinctrl_gpio_range *range;
425 int ret;
426 int pin;
427
Stephen Warren57b676f2012-03-02 13:05:44 -0700428 mutex_lock(&pinctrl_mutex);
429
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100430 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700431 if (ret) {
432 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100433 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700434 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100435
436 /* Convert to the pin controllers number space */
437 pin = gpio - range->base + range->pin_base;
438
Stephen Warren57b676f2012-03-02 13:05:44 -0700439 pinmux_free_gpio(pctldev, pin, range);
440
441 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100442}
443EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
444
445static int pinctrl_gpio_direction(unsigned gpio, bool input)
446{
447 struct pinctrl_dev *pctldev;
448 struct pinctrl_gpio_range *range;
449 int ret;
450 int pin;
451
452 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
453 if (ret)
454 return ret;
455
456 /* Convert to the pin controllers number space */
457 pin = gpio - range->base + range->pin_base;
458
459 return pinmux_gpio_direction(pctldev, range, pin, input);
460}
461
462/**
463 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
464 * @gpio: the GPIO pin number from the GPIO subsystem number space
465 *
466 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
467 * as part of their gpio_direction_input() semantics, platforms and individual
468 * drivers shall *NOT* touch pin control GPIO calls.
469 */
470int pinctrl_gpio_direction_input(unsigned gpio)
471{
Stephen Warren57b676f2012-03-02 13:05:44 -0700472 int ret;
473 mutex_lock(&pinctrl_mutex);
474 ret = pinctrl_gpio_direction(gpio, true);
475 mutex_unlock(&pinctrl_mutex);
476 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100477}
478EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
479
480/**
481 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
482 * @gpio: the GPIO pin number from the GPIO subsystem number space
483 *
484 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
485 * as part of their gpio_direction_output() semantics, platforms and individual
486 * drivers shall *NOT* touch pin control GPIO calls.
487 */
488int pinctrl_gpio_direction_output(unsigned gpio)
489{
Stephen Warren57b676f2012-03-02 13:05:44 -0700490 int ret;
491 mutex_lock(&pinctrl_mutex);
492 ret = pinctrl_gpio_direction(gpio, false);
493 mutex_unlock(&pinctrl_mutex);
494 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100495}
496EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
497
Stephen Warren6e5e9592012-03-02 13:05:47 -0700498static struct pinctrl_state *find_state(struct pinctrl *p,
499 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100500{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700501 struct pinctrl_state *state;
502
503 list_for_each_entry(state, &p->states, node)
504 if (!strcmp(state->name, name))
505 return state;
506
507 return NULL;
508}
509
510static struct pinctrl_state *create_state(struct pinctrl *p,
511 const char *name)
512{
513 struct pinctrl_state *state;
514
515 state = kzalloc(sizeof(*state), GFP_KERNEL);
516 if (state == NULL) {
517 dev_err(p->dev,
518 "failed to alloc struct pinctrl_state\n");
519 return ERR_PTR(-ENOMEM);
520 }
521
522 state->name = name;
523 INIT_LIST_HEAD(&state->settings);
524
525 list_add_tail(&state->node, &p->states);
526
527 return state;
528}
529
530static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
531{
532 struct pinctrl_state *state;
533 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700534 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700535
536 state = find_state(p, map->name);
537 if (!state)
538 state = create_state(p, map->name);
539 if (IS_ERR(state))
540 return PTR_ERR(state);
541
Stephen Warren1e2082b2012-03-02 13:05:48 -0700542 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
543 return 0;
544
Stephen Warren6e5e9592012-03-02 13:05:47 -0700545 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
546 if (setting == NULL) {
547 dev_err(p->dev,
548 "failed to alloc struct pinctrl_setting\n");
549 return -ENOMEM;
550 }
551
Stephen Warren1e2082b2012-03-02 13:05:48 -0700552 setting->type = map->type;
553
Stephen Warren6e5e9592012-03-02 13:05:47 -0700554 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
555 if (setting->pctldev == NULL) {
Linus Walleijc05127c2012-04-10 10:00:38 +0200556 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
Stephen Warren6e5e9592012-03-02 13:05:47 -0700557 map->ctrl_dev_name);
558 kfree(setting);
Linus Walleijc05127c2012-04-10 10:00:38 +0200559 /*
560 * OK let us guess that the driver is not there yet, and
561 * let's defer obtaining this pinctrl handle to later...
562 */
563 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700564 }
565
Stephen Warren1e2082b2012-03-02 13:05:48 -0700566 switch (map->type) {
567 case PIN_MAP_TYPE_MUX_GROUP:
568 ret = pinmux_map_to_setting(map, setting);
569 break;
570 case PIN_MAP_TYPE_CONFIGS_PIN:
571 case PIN_MAP_TYPE_CONFIGS_GROUP:
572 ret = pinconf_map_to_setting(map, setting);
573 break;
574 default:
575 ret = -EINVAL;
576 break;
577 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700578 if (ret < 0) {
579 kfree(setting);
580 return ret;
581 }
582
583 list_add_tail(&setting->node, &state->settings);
584
585 return 0;
586}
587
588static struct pinctrl *find_pinctrl(struct device *dev)
589{
590 struct pinctrl *p;
591
Stephen Warren1e2082b2012-03-02 13:05:48 -0700592 list_for_each_entry(p, &pinctrl_list, node)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700593 if (p->dev == dev)
594 return p;
595
596 return NULL;
597}
598
599static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
600
601static struct pinctrl *create_pinctrl(struct device *dev)
602{
603 struct pinctrl *p;
604 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700605 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100606 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700607 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700608 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100609
610 /*
611 * create the state cookie holder struct pinctrl for each
612 * mapping, this is what consumers will get when requesting
613 * a pin control handle with pinctrl_get()
614 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700615 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700616 if (p == NULL) {
617 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100618 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700619 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700620 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700621 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -0600622 INIT_LIST_HEAD(&p->dt_maps);
623
624 ret = pinctrl_dt_to_map(p);
625 if (ret < 0) {
626 kfree(p);
627 return ERR_PTR(ret);
628 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700629
630 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100631
632 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700633 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -0700634 /* Map must be for this device */
635 if (strcmp(map->dev_name, devname))
636 continue;
637
Stephen Warren6e5e9592012-03-02 13:05:47 -0700638 ret = add_setting(p, map);
639 if (ret < 0) {
640 pinctrl_put_locked(p, false);
641 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100642 }
643 }
644
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100645 /* Add the pinmux to the global list */
Stephen Warren8b9c1392012-02-19 23:45:42 -0700646 list_add_tail(&p->node, &pinctrl_list);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100647
648 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700649}
Stephen Warren7ecdb162012-03-02 13:05:45 -0700650
Stephen Warren6e5e9592012-03-02 13:05:47 -0700651static struct pinctrl *pinctrl_get_locked(struct device *dev)
652{
653 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700654
Stephen Warren6e5e9592012-03-02 13:05:47 -0700655 if (WARN_ON(!dev))
656 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700657
Stephen Warren6e5e9592012-03-02 13:05:47 -0700658 p = find_pinctrl(dev);
659 if (p != NULL)
660 return ERR_PTR(-EBUSY);
661
662 p = create_pinctrl(dev);
663 if (IS_ERR(p))
664 return p;
665
666 return p;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100667}
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700668
669/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700670 * pinctrl_get() - retrieves the pinctrl handle for a device
671 * @dev: the device to obtain the handle for
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700672 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700673struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700674{
675 struct pinctrl *p;
676
Stephen Warren57b676f2012-03-02 13:05:44 -0700677 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700678 p = pinctrl_get_locked(dev);
Stephen Warren57b676f2012-03-02 13:05:44 -0700679 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700680
681 return p;
682}
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100683EXPORT_SYMBOL_GPL(pinctrl_get);
684
Stephen Warren6e5e9592012-03-02 13:05:47 -0700685static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -0700686{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700687 struct pinctrl_state *state, *n1;
688 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700689
Stephen Warren6e5e9592012-03-02 13:05:47 -0700690 list_for_each_entry_safe(state, n1, &p->states, node) {
691 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700692 switch (setting->type) {
693 case PIN_MAP_TYPE_MUX_GROUP:
694 if (state == p->state)
695 pinmux_disable_setting(setting);
696 pinmux_free_setting(setting);
697 break;
698 case PIN_MAP_TYPE_CONFIGS_PIN:
699 case PIN_MAP_TYPE_CONFIGS_GROUP:
700 pinconf_free_setting(setting);
701 break;
702 default:
703 break;
704 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700705 list_del(&setting->node);
706 kfree(setting);
707 }
708 list_del(&state->node);
709 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700710 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700711
Stephen Warren57291ce2012-03-23 10:29:46 -0600712 pinctrl_dt_free_maps(p);
713
Stephen Warren6e5e9592012-03-02 13:05:47 -0700714 if (inlist)
715 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700716 kfree(p);
717}
718
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100719/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700720 * pinctrl_put() - release a previously claimed pinctrl handle
721 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100722 */
723void pinctrl_put(struct pinctrl *p)
724{
Stephen Warren57b676f2012-03-02 13:05:44 -0700725 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700726 pinctrl_put_locked(p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -0700727 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100728}
729EXPORT_SYMBOL_GPL(pinctrl_put);
730
Stephen Warren6e5e9592012-03-02 13:05:47 -0700731static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
732 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -0700733{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700734 struct pinctrl_state *state;
735
736 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800737 if (!state) {
738 if (pinctrl_dummy_state) {
739 /* create dummy state */
740 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
741 name);
742 state = create_state(p, name);
743 if (IS_ERR(state))
744 return state;
745 } else {
746 return ERR_PTR(-ENODEV);
747 }
748 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700749
750 return state;
751}
752
753/**
754 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
755 * @p: the pinctrl handle to retrieve the state from
756 * @name: the state name to retrieve
757 */
758struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
759{
760 struct pinctrl_state *s;
761
762 mutex_lock(&pinctrl_mutex);
763 s = pinctrl_lookup_state_locked(p, name);
764 mutex_unlock(&pinctrl_mutex);
765
766 return s;
767}
768EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
769
770static int pinctrl_select_state_locked(struct pinctrl *p,
771 struct pinctrl_state *state)
772{
773 struct pinctrl_setting *setting, *setting2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700774 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700775
Stephen Warren6e5e9592012-03-02 13:05:47 -0700776 if (p->state == state)
777 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700778
Stephen Warren6e5e9592012-03-02 13:05:47 -0700779 if (p->state) {
780 /*
781 * The set of groups with a mux configuration in the old state
782 * may not be identical to the set of groups with a mux setting
783 * in the new state. While this might be unusual, it's entirely
784 * possible for the "user"-supplied mapping table to be written
785 * that way. For each group that was configured in the old state
786 * but not in the new state, this code puts that group into a
787 * safe/disabled state.
788 */
789 list_for_each_entry(setting, &p->state->settings, node) {
790 bool found = false;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700791 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
792 continue;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700793 list_for_each_entry(setting2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700794 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
795 continue;
796 if (setting2->data.mux.group ==
797 setting->data.mux.group) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700798 found = true;
799 break;
800 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700801 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700802 if (!found)
803 pinmux_disable_setting(setting);
804 }
805 }
806
807 p->state = state;
808
809 /* Apply all the settings for the new state */
810 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700811 switch (setting->type) {
812 case PIN_MAP_TYPE_MUX_GROUP:
813 ret = pinmux_enable_setting(setting);
814 break;
815 case PIN_MAP_TYPE_CONFIGS_PIN:
816 case PIN_MAP_TYPE_CONFIGS_GROUP:
817 ret = pinconf_apply_setting(setting);
818 break;
819 default:
820 ret = -EINVAL;
821 break;
822 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700823 if (ret < 0) {
824 /* FIXME: Difficult to return to prev state */
825 return ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700826 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700827 }
828
Stephen Warren7ecdb162012-03-02 13:05:45 -0700829 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700830}
831
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100832/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700833 * pinctrl_select() - select/activate/program a pinctrl state to HW
834 * @p: the pinctrl handle for the device that requests configuratio
835 * @state: the state handle to select/activate/program
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100836 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700837int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100838{
Stephen Warren57b676f2012-03-02 13:05:44 -0700839 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700840
Stephen Warren57b676f2012-03-02 13:05:44 -0700841 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700842 ret = pinctrl_select_state_locked(p, state);
Stephen Warren57b676f2012-03-02 13:05:44 -0700843 mutex_unlock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700844
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100845 return ret;
846}
Stephen Warren6e5e9592012-03-02 13:05:47 -0700847EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100848
Stephen Warren6d4ca1f2012-04-16 10:51:00 -0600849static void devm_pinctrl_release(struct device *dev, void *res)
850{
851 pinctrl_put(*(struct pinctrl **)res);
852}
853
854/**
855 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
856 * @dev: the device to obtain the handle for
857 *
858 * If there is a need to explicitly destroy the returned struct pinctrl,
859 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
860 */
861struct pinctrl *devm_pinctrl_get(struct device *dev)
862{
863 struct pinctrl **ptr, *p;
864
865 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
866 if (!ptr)
867 return ERR_PTR(-ENOMEM);
868
869 p = pinctrl_get(dev);
870 if (!IS_ERR(p)) {
871 *ptr = p;
872 devres_add(dev, ptr);
873 } else {
874 devres_free(ptr);
875 }
876
877 return p;
878}
879EXPORT_SYMBOL_GPL(devm_pinctrl_get);
880
881static int devm_pinctrl_match(struct device *dev, void *res, void *data)
882{
883 struct pinctrl **p = res;
884
885 return *p == data;
886}
887
888/**
889 * devm_pinctrl_put() - Resource managed pinctrl_put()
890 * @p: the pinctrl handle to release
891 *
892 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
893 * this function will not need to be called and the resource management
894 * code will ensure that the resource is freed.
895 */
896void devm_pinctrl_put(struct pinctrl *p)
897{
898 WARN_ON(devres_destroy(p->dev, devm_pinctrl_release,
899 devm_pinctrl_match, p));
900 pinctrl_put(p);
901}
902EXPORT_SYMBOL_GPL(devm_pinctrl_put);
903
Stephen Warren57291ce2012-03-23 10:29:46 -0600904int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
905 bool dup, bool locked)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100906{
Stephen Warren1e2082b2012-03-02 13:05:48 -0700907 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700908 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100909
910 pr_debug("add %d pinmux maps\n", num_maps);
911
912 /* First sanity check the new mapping */
913 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700914 if (!maps[i].dev_name) {
915 pr_err("failed to register map %s (%d): no device given\n",
916 maps[i].name, i);
917 return -EINVAL;
918 }
919
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100920 if (!maps[i].name) {
921 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700922 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100923 return -EINVAL;
924 }
925
Stephen Warren1e2082b2012-03-02 13:05:48 -0700926 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
927 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100928 pr_err("failed to register map %s (%d): no pin control device given\n",
929 maps[i].name, i);
930 return -EINVAL;
931 }
932
Stephen Warren1e2082b2012-03-02 13:05:48 -0700933 switch (maps[i].type) {
934 case PIN_MAP_TYPE_DUMMY_STATE:
935 break;
936 case PIN_MAP_TYPE_MUX_GROUP:
937 ret = pinmux_validate_map(&maps[i], i);
938 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -0600939 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700940 break;
941 case PIN_MAP_TYPE_CONFIGS_PIN:
942 case PIN_MAP_TYPE_CONFIGS_GROUP:
943 ret = pinconf_validate_map(&maps[i], i);
944 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -0600945 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700946 break;
947 default:
948 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700949 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -0700950 return -EINVAL;
951 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100952 }
953
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700954 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
955 if (!maps_node) {
956 pr_err("failed to alloc struct pinctrl_maps\n");
957 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100958 }
959
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700960 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -0600961 if (dup) {
962 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
963 GFP_KERNEL);
964 if (!maps_node->maps) {
965 pr_err("failed to duplicate mapping table\n");
966 kfree(maps_node);
967 return -ENOMEM;
968 }
969 } else {
970 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700971 }
972
Stephen Warren57291ce2012-03-23 10:29:46 -0600973 if (!locked)
974 mutex_lock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700975 list_add_tail(&maps_node->node, &pinctrl_maps);
Stephen Warren57291ce2012-03-23 10:29:46 -0600976 if (!locked)
977 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700978
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100979 return 0;
980}
981
Stephen Warren57291ce2012-03-23 10:29:46 -0600982/**
983 * pinctrl_register_mappings() - register a set of pin controller mappings
984 * @maps: the pincontrol mappings table to register. This should probably be
985 * marked with __initdata so it can be discarded after boot. This
986 * function will perform a shallow copy for the mapping entries.
987 * @num_maps: the number of maps in the mapping table
988 */
989int pinctrl_register_mappings(struct pinctrl_map const *maps,
990 unsigned num_maps)
991{
992 return pinctrl_register_map(maps, num_maps, true, false);
993}
994
995void pinctrl_unregister_map(struct pinctrl_map const *map)
996{
997 struct pinctrl_maps *maps_node;
998
999 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1000 if (maps_node->maps == map) {
1001 list_del(&maps_node->node);
1002 return;
1003 }
1004 }
1005}
1006
Linus Walleij2744e8a2011-05-02 20:50:54 +02001007#ifdef CONFIG_DEBUG_FS
1008
1009static int pinctrl_pins_show(struct seq_file *s, void *what)
1010{
1011 struct pinctrl_dev *pctldev = s->private;
1012 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001013 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001014
1015 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001016
Stephen Warren57b676f2012-03-02 13:05:44 -07001017 mutex_lock(&pinctrl_mutex);
1018
Chanho Park706e8522012-01-03 16:47:50 +09001019 /* The pin number can be retrived from the pin controller descriptor */
1020 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001021 struct pin_desc *desc;
1022
Chanho Park706e8522012-01-03 16:47:50 +09001023 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001024 desc = pin_desc_get(pctldev, pin);
1025 /* Pin space may be sparse */
1026 if (desc == NULL)
1027 continue;
1028
1029 seq_printf(s, "pin %d (%s) ", pin,
1030 desc->name ? desc->name : "unnamed");
1031
1032 /* Driver-specific info per pin */
1033 if (ops->pin_dbg_show)
1034 ops->pin_dbg_show(pctldev, s, pin);
1035
1036 seq_puts(s, "\n");
1037 }
1038
Stephen Warren57b676f2012-03-02 13:05:44 -07001039 mutex_unlock(&pinctrl_mutex);
1040
Linus Walleij2744e8a2011-05-02 20:50:54 +02001041 return 0;
1042}
1043
1044static int pinctrl_groups_show(struct seq_file *s, void *what)
1045{
1046 struct pinctrl_dev *pctldev = s->private;
1047 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301048 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001049
Viresh Kumard1e90e92012-03-30 11:25:40 +05301050 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001051 mutex_lock(&pinctrl_mutex);
1052
Linus Walleij2744e8a2011-05-02 20:50:54 +02001053 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301054 while (selector < ngroups) {
Stephen Warrena5818a82011-10-19 16:19:25 -06001055 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001056 unsigned num_pins;
1057 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001058 const char *pname;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001059 int ret;
1060 int i;
1061
1062 ret = ops->get_group_pins(pctldev, selector,
1063 &pins, &num_pins);
1064 if (ret)
1065 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1066 gname);
1067 else {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001068 seq_printf(s, "group: %s\n", gname);
1069 for (i = 0; i < num_pins; i++) {
1070 pname = pin_get_name(pctldev, pins[i]);
1071 if (WARN_ON(!pname))
1072 return -EINVAL;
1073 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1074 }
1075 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001076 }
1077 selector++;
1078 }
1079
Stephen Warren57b676f2012-03-02 13:05:44 -07001080 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001081
1082 return 0;
1083}
1084
1085static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1086{
1087 struct pinctrl_dev *pctldev = s->private;
1088 struct pinctrl_gpio_range *range = NULL;
1089
1090 seq_puts(s, "GPIO ranges handled:\n");
1091
Stephen Warren57b676f2012-03-02 13:05:44 -07001092 mutex_lock(&pinctrl_mutex);
1093
Linus Walleij2744e8a2011-05-02 20:50:54 +02001094 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001095 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Linus Walleij75d66422011-11-16 09:58:51 +01001096 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1097 range->id, range->name,
1098 range->base, (range->base + range->npins - 1),
1099 range->pin_base,
1100 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001101 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001102
1103 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001104
1105 return 0;
1106}
1107
1108static int pinctrl_devices_show(struct seq_file *s, void *what)
1109{
1110 struct pinctrl_dev *pctldev;
1111
Linus Walleijae6b4d82011-10-19 18:14:33 +02001112 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001113
1114 mutex_lock(&pinctrl_mutex);
1115
Linus Walleij2744e8a2011-05-02 20:50:54 +02001116 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1117 seq_printf(s, "%s ", pctldev->desc->name);
1118 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001119 seq_puts(s, "yes ");
1120 else
1121 seq_puts(s, "no ");
1122 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001123 seq_puts(s, "yes");
1124 else
1125 seq_puts(s, "no");
1126 seq_puts(s, "\n");
1127 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001128
1129 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001130
1131 return 0;
1132}
1133
Stephen Warren1e2082b2012-03-02 13:05:48 -07001134static inline const char *map_type(enum pinctrl_map_type type)
1135{
1136 static const char * const names[] = {
1137 "INVALID",
1138 "DUMMY_STATE",
1139 "MUX_GROUP",
1140 "CONFIGS_PIN",
1141 "CONFIGS_GROUP",
1142 };
1143
1144 if (type >= ARRAY_SIZE(names))
1145 return "UNKNOWN";
1146
1147 return names[type];
1148}
1149
Stephen Warren3eedb432012-02-23 17:04:40 -07001150static int pinctrl_maps_show(struct seq_file *s, void *what)
1151{
1152 struct pinctrl_maps *maps_node;
1153 int i;
1154 struct pinctrl_map const *map;
1155
1156 seq_puts(s, "Pinctrl maps:\n");
1157
Stephen Warren57b676f2012-03-02 13:05:44 -07001158 mutex_lock(&pinctrl_mutex);
1159
Stephen Warren3eedb432012-02-23 17:04:40 -07001160 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001161 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1162 map->dev_name, map->name, map_type(map->type),
1163 map->type);
1164
1165 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1166 seq_printf(s, "controlling device %s\n",
1167 map->ctrl_dev_name);
1168
1169 switch (map->type) {
1170 case PIN_MAP_TYPE_MUX_GROUP:
1171 pinmux_show_map(s, map);
1172 break;
1173 case PIN_MAP_TYPE_CONFIGS_PIN:
1174 case PIN_MAP_TYPE_CONFIGS_GROUP:
1175 pinconf_show_map(s, map);
1176 break;
1177 default:
1178 break;
1179 }
1180
1181 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001182 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001183
1184 mutex_unlock(&pinctrl_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001185
1186 return 0;
1187}
1188
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001189static int pinctrl_show(struct seq_file *s, void *what)
1190{
1191 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001192 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001193 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001194
1195 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001196
1197 mutex_lock(&pinctrl_mutex);
1198
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001199 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001200 seq_printf(s, "device: %s current state: %s\n",
1201 dev_name(p->dev),
1202 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001203
Stephen Warren6e5e9592012-03-02 13:05:47 -07001204 list_for_each_entry(state, &p->states, node) {
1205 seq_printf(s, " state: %s\n", state->name);
1206
1207 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001208 struct pinctrl_dev *pctldev = setting->pctldev;
1209
1210 seq_printf(s, " type: %s controller %s ",
1211 map_type(setting->type),
1212 pinctrl_dev_get_name(pctldev));
1213
1214 switch (setting->type) {
1215 case PIN_MAP_TYPE_MUX_GROUP:
1216 pinmux_show_setting(s, setting);
1217 break;
1218 case PIN_MAP_TYPE_CONFIGS_PIN:
1219 case PIN_MAP_TYPE_CONFIGS_GROUP:
1220 pinconf_show_setting(s, setting);
1221 break;
1222 default:
1223 break;
1224 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001225 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001226 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001227 }
1228
Stephen Warren57b676f2012-03-02 13:05:44 -07001229 mutex_unlock(&pinctrl_mutex);
1230
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001231 return 0;
1232}
1233
Linus Walleij2744e8a2011-05-02 20:50:54 +02001234static int pinctrl_pins_open(struct inode *inode, struct file *file)
1235{
1236 return single_open(file, pinctrl_pins_show, inode->i_private);
1237}
1238
1239static int pinctrl_groups_open(struct inode *inode, struct file *file)
1240{
1241 return single_open(file, pinctrl_groups_show, inode->i_private);
1242}
1243
1244static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1245{
1246 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1247}
1248
1249static int pinctrl_devices_open(struct inode *inode, struct file *file)
1250{
1251 return single_open(file, pinctrl_devices_show, NULL);
1252}
1253
Stephen Warren3eedb432012-02-23 17:04:40 -07001254static int pinctrl_maps_open(struct inode *inode, struct file *file)
1255{
1256 return single_open(file, pinctrl_maps_show, NULL);
1257}
1258
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001259static int pinctrl_open(struct inode *inode, struct file *file)
1260{
1261 return single_open(file, pinctrl_show, NULL);
1262}
1263
Linus Walleij2744e8a2011-05-02 20:50:54 +02001264static const struct file_operations pinctrl_pins_ops = {
1265 .open = pinctrl_pins_open,
1266 .read = seq_read,
1267 .llseek = seq_lseek,
1268 .release = single_release,
1269};
1270
1271static const struct file_operations pinctrl_groups_ops = {
1272 .open = pinctrl_groups_open,
1273 .read = seq_read,
1274 .llseek = seq_lseek,
1275 .release = single_release,
1276};
1277
1278static const struct file_operations pinctrl_gpioranges_ops = {
1279 .open = pinctrl_gpioranges_open,
1280 .read = seq_read,
1281 .llseek = seq_lseek,
1282 .release = single_release,
1283};
1284
1285static const struct file_operations pinctrl_devices_ops = {
1286 .open = pinctrl_devices_open,
1287 .read = seq_read,
1288 .llseek = seq_lseek,
1289 .release = single_release,
1290};
1291
Stephen Warren3eedb432012-02-23 17:04:40 -07001292static const struct file_operations pinctrl_maps_ops = {
1293 .open = pinctrl_maps_open,
1294 .read = seq_read,
1295 .llseek = seq_lseek,
1296 .release = single_release,
1297};
1298
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001299static const struct file_operations pinctrl_ops = {
1300 .open = pinctrl_open,
1301 .read = seq_read,
1302 .llseek = seq_lseek,
1303 .release = single_release,
1304};
1305
Linus Walleij2744e8a2011-05-02 20:50:54 +02001306static struct dentry *debugfs_root;
1307
1308static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1309{
Tony Lindgren02157162012-01-20 08:17:22 -08001310 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001311
Stephen Warren51cd24e2011-12-09 16:59:05 -07001312 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001313 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001314 pctldev->device_root = device_root;
1315
Linus Walleij2744e8a2011-05-02 20:50:54 +02001316 if (IS_ERR(device_root) || !device_root) {
1317 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001318 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001319 return;
1320 }
1321 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1322 device_root, pctldev, &pinctrl_pins_ops);
1323 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1324 device_root, pctldev, &pinctrl_groups_ops);
1325 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1326 device_root, pctldev, &pinctrl_gpioranges_ops);
1327 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +02001328 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001329}
1330
Tony Lindgren02157162012-01-20 08:17:22 -08001331static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1332{
1333 debugfs_remove_recursive(pctldev->device_root);
1334}
1335
Linus Walleij2744e8a2011-05-02 20:50:54 +02001336static void pinctrl_init_debugfs(void)
1337{
1338 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1339 if (IS_ERR(debugfs_root) || !debugfs_root) {
1340 pr_warn("failed to create debugfs directory\n");
1341 debugfs_root = NULL;
1342 return;
1343 }
1344
1345 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1346 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001347 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1348 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001349 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1350 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001351}
1352
1353#else /* CONFIG_DEBUG_FS */
1354
1355static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1356{
1357}
1358
1359static void pinctrl_init_debugfs(void)
1360{
1361}
1362
Tony Lindgren02157162012-01-20 08:17:22 -08001363static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1364{
1365}
1366
Linus Walleij2744e8a2011-05-02 20:50:54 +02001367#endif
1368
Stephen Warrend26bc492012-03-16 14:54:25 -06001369static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1370{
1371 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1372
1373 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301374 !ops->get_groups_count ||
Stephen Warrend26bc492012-03-16 14:54:25 -06001375 !ops->get_group_name ||
1376 !ops->get_group_pins)
1377 return -EINVAL;
1378
Stephen Warren57291ce2012-03-23 10:29:46 -06001379 if (ops->dt_node_to_map && !ops->dt_free_map)
1380 return -EINVAL;
1381
Stephen Warrend26bc492012-03-16 14:54:25 -06001382 return 0;
1383}
1384
Linus Walleij2744e8a2011-05-02 20:50:54 +02001385/**
1386 * pinctrl_register() - register a pin controller device
1387 * @pctldesc: descriptor for this pin controller
1388 * @dev: parent device for this pin controller
1389 * @driver_data: private pin controller data for this pin controller
1390 */
1391struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1392 struct device *dev, void *driver_data)
1393{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001394 struct pinctrl_dev *pctldev;
1395 int ret;
1396
1397 if (pctldesc == NULL)
1398 return NULL;
1399 if (pctldesc->name == NULL)
1400 return NULL;
1401
Stephen Warren02f5b982012-02-22 14:26:00 -07001402 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001403 if (pctldev == NULL) {
1404 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001405 return NULL;
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001406 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001407
1408 /* Initialize pin control device struct */
1409 pctldev->owner = pctldesc->owner;
1410 pctldev->desc = pctldesc;
1411 pctldev->driver_data = driver_data;
1412 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001413 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001414 pctldev->dev = dev;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001415
Stephen Warrend26bc492012-03-16 14:54:25 -06001416 /* check core ops for sanity */
1417 ret = pinctrl_check_ops(pctldev);
1418 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001419 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06001420 goto out_err;
1421 }
1422
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001423 /* If we're implementing pinmuxing, check the ops for sanity */
1424 if (pctldesc->pmxops) {
1425 ret = pinmux_check_ops(pctldev);
John Crispinad6e1102012-04-26 16:47:11 +02001426 if (ret)
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001427 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001428 }
1429
1430 /* If we're implementing pinconfig, check the ops for sanity */
1431 if (pctldesc->confops) {
1432 ret = pinconf_check_ops(pctldev);
John Crispinad6e1102012-04-26 16:47:11 +02001433 if (ret)
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001434 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001435 }
1436
Linus Walleij2744e8a2011-05-02 20:50:54 +02001437 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02001438 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001439 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1440 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001441 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001442 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1443 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001444 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001445 }
1446
Stephen Warren57b676f2012-03-02 13:05:44 -07001447 mutex_lock(&pinctrl_mutex);
1448
Stephen Warren8b9c1392012-02-19 23:45:42 -07001449 list_add_tail(&pctldev->node, &pinctrldev_list);
Stephen Warren57b676f2012-03-02 13:05:44 -07001450
Stephen Warren6e5e9592012-03-02 13:05:47 -07001451 pctldev->p = pinctrl_get_locked(pctldev->dev);
1452 if (!IS_ERR(pctldev->p)) {
1453 struct pinctrl_state *s =
1454 pinctrl_lookup_state_locked(pctldev->p,
1455 PINCTRL_STATE_DEFAULT);
John Crispinad6e1102012-04-26 16:47:11 +02001456 if (IS_ERR(s)) {
1457 dev_dbg(dev, "failed to lookup the default state\n");
1458 } else {
1459 ret = pinctrl_select_state_locked(pctldev->p, s);
1460 if (ret) {
1461 dev_err(dev,
1462 "failed to select default state\n");
1463 }
1464 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001465 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001466
1467 mutex_unlock(&pinctrl_mutex);
1468
Stephen Warren2304b472012-02-22 14:26:01 -07001469 pinctrl_init_device_debugfs(pctldev);
1470
Linus Walleij2744e8a2011-05-02 20:50:54 +02001471 return pctldev;
1472
Stephen Warren51cd24e2011-12-09 16:59:05 -07001473out_err:
1474 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001475 return NULL;
1476}
1477EXPORT_SYMBOL_GPL(pinctrl_register);
1478
1479/**
1480 * pinctrl_unregister() - unregister pinmux
1481 * @pctldev: pin controller to unregister
1482 *
1483 * Called by pinmux drivers to unregister a pinmux.
1484 */
1485void pinctrl_unregister(struct pinctrl_dev *pctldev)
1486{
1487 if (pctldev == NULL)
1488 return;
1489
Tony Lindgren02157162012-01-20 08:17:22 -08001490 pinctrl_remove_device_debugfs(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001491
1492 mutex_lock(&pinctrl_mutex);
1493
Stephen Warren6e5e9592012-03-02 13:05:47 -07001494 if (!IS_ERR(pctldev->p))
1495 pinctrl_put_locked(pctldev->p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -07001496
Linus Walleij2744e8a2011-05-02 20:50:54 +02001497 /* TODO: check that no pinmuxes are still active? */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001498 list_del(&pctldev->node);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001499 /* Destroy descriptor tree */
1500 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1501 pctldev->desc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001502 kfree(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001503
1504 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001505}
1506EXPORT_SYMBOL_GPL(pinctrl_unregister);
1507
1508static int __init pinctrl_init(void)
1509{
1510 pr_info("initialized pinctrl subsystem\n");
1511 pinctrl_init_debugfs();
1512 return 0;
1513}
1514
1515/* init early since many drivers really need to initialized pinmux early */
1516core_initcall(pinctrl_init);