blob: 902428dfb37e81a02371024ba7514a4942970b4a [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
335/**
Linus Walleij7afde8b2011-10-19 17:07:16 +0200336 * pinctrl_get_group_selector() - returns the group selector for a group
337 * @pctldev: the pin controller handling the group
338 * @pin_group: the pin group to look up
339 */
340int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
341 const char *pin_group)
342{
343 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530344 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200345 unsigned group_selector = 0;
346
Viresh Kumard1e90e92012-03-30 11:25:40 +0530347 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200348 const char *gname = pctlops->get_group_name(pctldev,
349 group_selector);
350 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700351 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200352 "found group selector %u for %s\n",
353 group_selector,
354 pin_group);
355 return group_selector;
356 }
357
358 group_selector++;
359 }
360
Stephen Warren51cd24e2011-12-09 16:59:05 -0700361 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200362 pin_group);
363
364 return -EINVAL;
365}
366
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100367/**
368 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
369 * @gpio: the GPIO pin number from the GPIO subsystem number space
370 *
371 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
372 * as part of their gpio_request() semantics, platforms and individual drivers
373 * shall *NOT* request GPIO pins to be muxed in.
374 */
375int pinctrl_request_gpio(unsigned gpio)
376{
377 struct pinctrl_dev *pctldev;
378 struct pinctrl_gpio_range *range;
379 int ret;
380 int pin;
381
Stephen Warren57b676f2012-03-02 13:05:44 -0700382 mutex_lock(&pinctrl_mutex);
383
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100384 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700385 if (ret) {
386 mutex_unlock(&pinctrl_mutex);
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800387 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700388 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100389
390 /* Convert to the pin controllers number space */
391 pin = gpio - range->base + range->pin_base;
392
Stephen Warren57b676f2012-03-02 13:05:44 -0700393 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
394
395 mutex_unlock(&pinctrl_mutex);
396 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100397}
398EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
399
400/**
401 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
402 * @gpio: the GPIO pin number from the GPIO subsystem number space
403 *
404 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
405 * as part of their gpio_free() semantics, platforms and individual drivers
406 * shall *NOT* request GPIO pins to be muxed out.
407 */
408void pinctrl_free_gpio(unsigned gpio)
409{
410 struct pinctrl_dev *pctldev;
411 struct pinctrl_gpio_range *range;
412 int ret;
413 int pin;
414
Stephen Warren57b676f2012-03-02 13:05:44 -0700415 mutex_lock(&pinctrl_mutex);
416
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100417 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700418 if (ret) {
419 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100420 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700421 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100422
423 /* Convert to the pin controllers number space */
424 pin = gpio - range->base + range->pin_base;
425
Stephen Warren57b676f2012-03-02 13:05:44 -0700426 pinmux_free_gpio(pctldev, pin, range);
427
428 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100429}
430EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
431
432static int pinctrl_gpio_direction(unsigned gpio, bool input)
433{
434 struct pinctrl_dev *pctldev;
435 struct pinctrl_gpio_range *range;
436 int ret;
437 int pin;
438
439 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
440 if (ret)
441 return ret;
442
443 /* Convert to the pin controllers number space */
444 pin = gpio - range->base + range->pin_base;
445
446 return pinmux_gpio_direction(pctldev, range, pin, input);
447}
448
449/**
450 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
451 * @gpio: the GPIO pin number from the GPIO subsystem number space
452 *
453 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
454 * as part of their gpio_direction_input() semantics, platforms and individual
455 * drivers shall *NOT* touch pin control GPIO calls.
456 */
457int pinctrl_gpio_direction_input(unsigned gpio)
458{
Stephen Warren57b676f2012-03-02 13:05:44 -0700459 int ret;
460 mutex_lock(&pinctrl_mutex);
461 ret = pinctrl_gpio_direction(gpio, true);
462 mutex_unlock(&pinctrl_mutex);
463 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100464}
465EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
466
467/**
468 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
469 * @gpio: the GPIO pin number from the GPIO subsystem number space
470 *
471 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
472 * as part of their gpio_direction_output() semantics, platforms and individual
473 * drivers shall *NOT* touch pin control GPIO calls.
474 */
475int pinctrl_gpio_direction_output(unsigned gpio)
476{
Stephen Warren57b676f2012-03-02 13:05:44 -0700477 int ret;
478 mutex_lock(&pinctrl_mutex);
479 ret = pinctrl_gpio_direction(gpio, false);
480 mutex_unlock(&pinctrl_mutex);
481 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100482}
483EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
484
Stephen Warren6e5e9592012-03-02 13:05:47 -0700485static struct pinctrl_state *find_state(struct pinctrl *p,
486 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100487{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700488 struct pinctrl_state *state;
489
490 list_for_each_entry(state, &p->states, node)
491 if (!strcmp(state->name, name))
492 return state;
493
494 return NULL;
495}
496
497static struct pinctrl_state *create_state(struct pinctrl *p,
498 const char *name)
499{
500 struct pinctrl_state *state;
501
502 state = kzalloc(sizeof(*state), GFP_KERNEL);
503 if (state == NULL) {
504 dev_err(p->dev,
505 "failed to alloc struct pinctrl_state\n");
506 return ERR_PTR(-ENOMEM);
507 }
508
509 state->name = name;
510 INIT_LIST_HEAD(&state->settings);
511
512 list_add_tail(&state->node, &p->states);
513
514 return state;
515}
516
517static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
518{
519 struct pinctrl_state *state;
520 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700521 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700522
523 state = find_state(p, map->name);
524 if (!state)
525 state = create_state(p, map->name);
526 if (IS_ERR(state))
527 return PTR_ERR(state);
528
Stephen Warren1e2082b2012-03-02 13:05:48 -0700529 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
530 return 0;
531
Stephen Warren6e5e9592012-03-02 13:05:47 -0700532 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
533 if (setting == NULL) {
534 dev_err(p->dev,
535 "failed to alloc struct pinctrl_setting\n");
536 return -ENOMEM;
537 }
538
Stephen Warren1e2082b2012-03-02 13:05:48 -0700539 setting->type = map->type;
540
Stephen Warren6e5e9592012-03-02 13:05:47 -0700541 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
542 if (setting->pctldev == NULL) {
Linus Walleijc05127c2012-04-10 10:00:38 +0200543 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
Stephen Warren6e5e9592012-03-02 13:05:47 -0700544 map->ctrl_dev_name);
545 kfree(setting);
Linus Walleijc05127c2012-04-10 10:00:38 +0200546 /*
547 * OK let us guess that the driver is not there yet, and
548 * let's defer obtaining this pinctrl handle to later...
549 */
550 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700551 }
552
Stephen Warren1e2082b2012-03-02 13:05:48 -0700553 switch (map->type) {
554 case PIN_MAP_TYPE_MUX_GROUP:
555 ret = pinmux_map_to_setting(map, setting);
556 break;
557 case PIN_MAP_TYPE_CONFIGS_PIN:
558 case PIN_MAP_TYPE_CONFIGS_GROUP:
559 ret = pinconf_map_to_setting(map, setting);
560 break;
561 default:
562 ret = -EINVAL;
563 break;
564 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700565 if (ret < 0) {
566 kfree(setting);
567 return ret;
568 }
569
570 list_add_tail(&setting->node, &state->settings);
571
572 return 0;
573}
574
575static struct pinctrl *find_pinctrl(struct device *dev)
576{
577 struct pinctrl *p;
578
Stephen Warren1e2082b2012-03-02 13:05:48 -0700579 list_for_each_entry(p, &pinctrl_list, node)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700580 if (p->dev == dev)
581 return p;
582
583 return NULL;
584}
585
586static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
587
588static struct pinctrl *create_pinctrl(struct device *dev)
589{
590 struct pinctrl *p;
591 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700592 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100593 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700594 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700595 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100596
597 /*
598 * create the state cookie holder struct pinctrl for each
599 * mapping, this is what consumers will get when requesting
600 * a pin control handle with pinctrl_get()
601 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700602 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700603 if (p == NULL) {
604 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100605 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700606 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700607 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700608 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -0600609 INIT_LIST_HEAD(&p->dt_maps);
610
611 ret = pinctrl_dt_to_map(p);
612 if (ret < 0) {
613 kfree(p);
614 return ERR_PTR(ret);
615 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700616
617 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100618
619 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700620 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -0700621 /* Map must be for this device */
622 if (strcmp(map->dev_name, devname))
623 continue;
624
Stephen Warren6e5e9592012-03-02 13:05:47 -0700625 ret = add_setting(p, map);
626 if (ret < 0) {
627 pinctrl_put_locked(p, false);
628 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100629 }
630 }
631
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100632 /* Add the pinmux to the global list */
Stephen Warren8b9c1392012-02-19 23:45:42 -0700633 list_add_tail(&p->node, &pinctrl_list);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100634
635 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700636}
Stephen Warren7ecdb162012-03-02 13:05:45 -0700637
Stephen Warren6e5e9592012-03-02 13:05:47 -0700638static struct pinctrl *pinctrl_get_locked(struct device *dev)
639{
640 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700641
Stephen Warren6e5e9592012-03-02 13:05:47 -0700642 if (WARN_ON(!dev))
643 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700644
Stephen Warren6e5e9592012-03-02 13:05:47 -0700645 p = find_pinctrl(dev);
646 if (p != NULL)
647 return ERR_PTR(-EBUSY);
648
649 p = create_pinctrl(dev);
650 if (IS_ERR(p))
651 return p;
652
653 return p;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100654}
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700655
656/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700657 * pinctrl_get() - retrieves the pinctrl handle for a device
658 * @dev: the device to obtain the handle for
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700659 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700660struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700661{
662 struct pinctrl *p;
663
Stephen Warren57b676f2012-03-02 13:05:44 -0700664 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700665 p = pinctrl_get_locked(dev);
Stephen Warren57b676f2012-03-02 13:05:44 -0700666 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700667
668 return p;
669}
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100670EXPORT_SYMBOL_GPL(pinctrl_get);
671
Stephen Warren6e5e9592012-03-02 13:05:47 -0700672static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -0700673{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700674 struct pinctrl_state *state, *n1;
675 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700676
Stephen Warren6e5e9592012-03-02 13:05:47 -0700677 list_for_each_entry_safe(state, n1, &p->states, node) {
678 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700679 switch (setting->type) {
680 case PIN_MAP_TYPE_MUX_GROUP:
681 if (state == p->state)
682 pinmux_disable_setting(setting);
683 pinmux_free_setting(setting);
684 break;
685 case PIN_MAP_TYPE_CONFIGS_PIN:
686 case PIN_MAP_TYPE_CONFIGS_GROUP:
687 pinconf_free_setting(setting);
688 break;
689 default:
690 break;
691 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700692 list_del(&setting->node);
693 kfree(setting);
694 }
695 list_del(&state->node);
696 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700697 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700698
Stephen Warren57291ce2012-03-23 10:29:46 -0600699 pinctrl_dt_free_maps(p);
700
Stephen Warren6e5e9592012-03-02 13:05:47 -0700701 if (inlist)
702 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700703 kfree(p);
704}
705
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100706/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700707 * pinctrl_put() - release a previously claimed pinctrl handle
708 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100709 */
710void pinctrl_put(struct pinctrl *p)
711{
Stephen Warren57b676f2012-03-02 13:05:44 -0700712 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700713 pinctrl_put_locked(p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -0700714 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100715}
716EXPORT_SYMBOL_GPL(pinctrl_put);
717
Stephen Warren6e5e9592012-03-02 13:05:47 -0700718static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
719 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -0700720{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700721 struct pinctrl_state *state;
722
723 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800724 if (!state) {
725 if (pinctrl_dummy_state) {
726 /* create dummy state */
727 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
728 name);
729 state = create_state(p, name);
730 if (IS_ERR(state))
731 return state;
732 } else {
733 return ERR_PTR(-ENODEV);
734 }
735 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700736
737 return state;
738}
739
740/**
741 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
742 * @p: the pinctrl handle to retrieve the state from
743 * @name: the state name to retrieve
744 */
745struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
746{
747 struct pinctrl_state *s;
748
749 mutex_lock(&pinctrl_mutex);
750 s = pinctrl_lookup_state_locked(p, name);
751 mutex_unlock(&pinctrl_mutex);
752
753 return s;
754}
755EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
756
757static int pinctrl_select_state_locked(struct pinctrl *p,
758 struct pinctrl_state *state)
759{
760 struct pinctrl_setting *setting, *setting2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700761 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700762
Stephen Warren6e5e9592012-03-02 13:05:47 -0700763 if (p->state == state)
764 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700765
Stephen Warren6e5e9592012-03-02 13:05:47 -0700766 if (p->state) {
767 /*
768 * The set of groups with a mux configuration in the old state
769 * may not be identical to the set of groups with a mux setting
770 * in the new state. While this might be unusual, it's entirely
771 * possible for the "user"-supplied mapping table to be written
772 * that way. For each group that was configured in the old state
773 * but not in the new state, this code puts that group into a
774 * safe/disabled state.
775 */
776 list_for_each_entry(setting, &p->state->settings, node) {
777 bool found = false;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700778 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
779 continue;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700780 list_for_each_entry(setting2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700781 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
782 continue;
783 if (setting2->data.mux.group ==
784 setting->data.mux.group) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700785 found = true;
786 break;
787 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700788 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700789 if (!found)
790 pinmux_disable_setting(setting);
791 }
792 }
793
794 p->state = state;
795
796 /* Apply all the settings for the new state */
797 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700798 switch (setting->type) {
799 case PIN_MAP_TYPE_MUX_GROUP:
800 ret = pinmux_enable_setting(setting);
801 break;
802 case PIN_MAP_TYPE_CONFIGS_PIN:
803 case PIN_MAP_TYPE_CONFIGS_GROUP:
804 ret = pinconf_apply_setting(setting);
805 break;
806 default:
807 ret = -EINVAL;
808 break;
809 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700810 if (ret < 0) {
811 /* FIXME: Difficult to return to prev state */
812 return ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700813 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700814 }
815
Stephen Warren7ecdb162012-03-02 13:05:45 -0700816 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700817}
818
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100819/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700820 * pinctrl_select() - select/activate/program a pinctrl state to HW
821 * @p: the pinctrl handle for the device that requests configuratio
822 * @state: the state handle to select/activate/program
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100823 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700824int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100825{
Stephen Warren57b676f2012-03-02 13:05:44 -0700826 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700827
Stephen Warren57b676f2012-03-02 13:05:44 -0700828 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700829 ret = pinctrl_select_state_locked(p, state);
Stephen Warren57b676f2012-03-02 13:05:44 -0700830 mutex_unlock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700831
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100832 return ret;
833}
Stephen Warren6e5e9592012-03-02 13:05:47 -0700834EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100835
Stephen Warren6d4ca1f2012-04-16 10:51:00 -0600836static void devm_pinctrl_release(struct device *dev, void *res)
837{
838 pinctrl_put(*(struct pinctrl **)res);
839}
840
841/**
842 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
843 * @dev: the device to obtain the handle for
844 *
845 * If there is a need to explicitly destroy the returned struct pinctrl,
846 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
847 */
848struct pinctrl *devm_pinctrl_get(struct device *dev)
849{
850 struct pinctrl **ptr, *p;
851
852 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
853 if (!ptr)
854 return ERR_PTR(-ENOMEM);
855
856 p = pinctrl_get(dev);
857 if (!IS_ERR(p)) {
858 *ptr = p;
859 devres_add(dev, ptr);
860 } else {
861 devres_free(ptr);
862 }
863
864 return p;
865}
866EXPORT_SYMBOL_GPL(devm_pinctrl_get);
867
868static int devm_pinctrl_match(struct device *dev, void *res, void *data)
869{
870 struct pinctrl **p = res;
871
872 return *p == data;
873}
874
875/**
876 * devm_pinctrl_put() - Resource managed pinctrl_put()
877 * @p: the pinctrl handle to release
878 *
879 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
880 * this function will not need to be called and the resource management
881 * code will ensure that the resource is freed.
882 */
883void devm_pinctrl_put(struct pinctrl *p)
884{
885 WARN_ON(devres_destroy(p->dev, devm_pinctrl_release,
886 devm_pinctrl_match, p));
887 pinctrl_put(p);
888}
889EXPORT_SYMBOL_GPL(devm_pinctrl_put);
890
Stephen Warren57291ce2012-03-23 10:29:46 -0600891int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
892 bool dup, bool locked)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100893{
Stephen Warren1e2082b2012-03-02 13:05:48 -0700894 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700895 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100896
897 pr_debug("add %d pinmux maps\n", num_maps);
898
899 /* First sanity check the new mapping */
900 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700901 if (!maps[i].dev_name) {
902 pr_err("failed to register map %s (%d): no device given\n",
903 maps[i].name, i);
904 return -EINVAL;
905 }
906
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100907 if (!maps[i].name) {
908 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700909 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100910 return -EINVAL;
911 }
912
Stephen Warren1e2082b2012-03-02 13:05:48 -0700913 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
914 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100915 pr_err("failed to register map %s (%d): no pin control device given\n",
916 maps[i].name, i);
917 return -EINVAL;
918 }
919
Stephen Warren1e2082b2012-03-02 13:05:48 -0700920 switch (maps[i].type) {
921 case PIN_MAP_TYPE_DUMMY_STATE:
922 break;
923 case PIN_MAP_TYPE_MUX_GROUP:
924 ret = pinmux_validate_map(&maps[i], i);
925 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -0600926 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700927 break;
928 case PIN_MAP_TYPE_CONFIGS_PIN:
929 case PIN_MAP_TYPE_CONFIGS_GROUP:
930 ret = pinconf_validate_map(&maps[i], i);
931 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -0600932 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700933 break;
934 default:
935 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700936 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -0700937 return -EINVAL;
938 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100939 }
940
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700941 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
942 if (!maps_node) {
943 pr_err("failed to alloc struct pinctrl_maps\n");
944 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100945 }
946
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700947 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -0600948 if (dup) {
949 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
950 GFP_KERNEL);
951 if (!maps_node->maps) {
952 pr_err("failed to duplicate mapping table\n");
953 kfree(maps_node);
954 return -ENOMEM;
955 }
956 } else {
957 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700958 }
959
Stephen Warren57291ce2012-03-23 10:29:46 -0600960 if (!locked)
961 mutex_lock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700962 list_add_tail(&maps_node->node, &pinctrl_maps);
Stephen Warren57291ce2012-03-23 10:29:46 -0600963 if (!locked)
964 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700965
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100966 return 0;
967}
968
Stephen Warren57291ce2012-03-23 10:29:46 -0600969/**
970 * pinctrl_register_mappings() - register a set of pin controller mappings
971 * @maps: the pincontrol mappings table to register. This should probably be
972 * marked with __initdata so it can be discarded after boot. This
973 * function will perform a shallow copy for the mapping entries.
974 * @num_maps: the number of maps in the mapping table
975 */
976int pinctrl_register_mappings(struct pinctrl_map const *maps,
977 unsigned num_maps)
978{
979 return pinctrl_register_map(maps, num_maps, true, false);
980}
981
982void pinctrl_unregister_map(struct pinctrl_map const *map)
983{
984 struct pinctrl_maps *maps_node;
985
986 list_for_each_entry(maps_node, &pinctrl_maps, node) {
987 if (maps_node->maps == map) {
988 list_del(&maps_node->node);
989 return;
990 }
991 }
992}
993
Linus Walleij2744e8a2011-05-02 20:50:54 +0200994#ifdef CONFIG_DEBUG_FS
995
996static int pinctrl_pins_show(struct seq_file *s, void *what)
997{
998 struct pinctrl_dev *pctldev = s->private;
999 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001000 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001001
1002 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001003
Stephen Warren57b676f2012-03-02 13:05:44 -07001004 mutex_lock(&pinctrl_mutex);
1005
Chanho Park706e8522012-01-03 16:47:50 +09001006 /* The pin number can be retrived from the pin controller descriptor */
1007 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001008 struct pin_desc *desc;
1009
Chanho Park706e8522012-01-03 16:47:50 +09001010 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001011 desc = pin_desc_get(pctldev, pin);
1012 /* Pin space may be sparse */
1013 if (desc == NULL)
1014 continue;
1015
1016 seq_printf(s, "pin %d (%s) ", pin,
1017 desc->name ? desc->name : "unnamed");
1018
1019 /* Driver-specific info per pin */
1020 if (ops->pin_dbg_show)
1021 ops->pin_dbg_show(pctldev, s, pin);
1022
1023 seq_puts(s, "\n");
1024 }
1025
Stephen Warren57b676f2012-03-02 13:05:44 -07001026 mutex_unlock(&pinctrl_mutex);
1027
Linus Walleij2744e8a2011-05-02 20:50:54 +02001028 return 0;
1029}
1030
1031static int pinctrl_groups_show(struct seq_file *s, void *what)
1032{
1033 struct pinctrl_dev *pctldev = s->private;
1034 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301035 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001036
Viresh Kumard1e90e92012-03-30 11:25:40 +05301037 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001038 mutex_lock(&pinctrl_mutex);
1039
Linus Walleij2744e8a2011-05-02 20:50:54 +02001040 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301041 while (selector < ngroups) {
Stephen Warrena5818a82011-10-19 16:19:25 -06001042 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001043 unsigned num_pins;
1044 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001045 const char *pname;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001046 int ret;
1047 int i;
1048
1049 ret = ops->get_group_pins(pctldev, selector,
1050 &pins, &num_pins);
1051 if (ret)
1052 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1053 gname);
1054 else {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001055 seq_printf(s, "group: %s\n", gname);
1056 for (i = 0; i < num_pins; i++) {
1057 pname = pin_get_name(pctldev, pins[i]);
1058 if (WARN_ON(!pname))
1059 return -EINVAL;
1060 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1061 }
1062 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001063 }
1064 selector++;
1065 }
1066
Stephen Warren57b676f2012-03-02 13:05:44 -07001067 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001068
1069 return 0;
1070}
1071
1072static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1073{
1074 struct pinctrl_dev *pctldev = s->private;
1075 struct pinctrl_gpio_range *range = NULL;
1076
1077 seq_puts(s, "GPIO ranges handled:\n");
1078
Stephen Warren57b676f2012-03-02 13:05:44 -07001079 mutex_lock(&pinctrl_mutex);
1080
Linus Walleij2744e8a2011-05-02 20:50:54 +02001081 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001082 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Linus Walleij75d66422011-11-16 09:58:51 +01001083 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1084 range->id, range->name,
1085 range->base, (range->base + range->npins - 1),
1086 range->pin_base,
1087 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001088 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001089
1090 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001091
1092 return 0;
1093}
1094
1095static int pinctrl_devices_show(struct seq_file *s, void *what)
1096{
1097 struct pinctrl_dev *pctldev;
1098
Linus Walleijae6b4d82011-10-19 18:14:33 +02001099 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001100
1101 mutex_lock(&pinctrl_mutex);
1102
Linus Walleij2744e8a2011-05-02 20:50:54 +02001103 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1104 seq_printf(s, "%s ", pctldev->desc->name);
1105 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001106 seq_puts(s, "yes ");
1107 else
1108 seq_puts(s, "no ");
1109 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001110 seq_puts(s, "yes");
1111 else
1112 seq_puts(s, "no");
1113 seq_puts(s, "\n");
1114 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001115
1116 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001117
1118 return 0;
1119}
1120
Stephen Warren1e2082b2012-03-02 13:05:48 -07001121static inline const char *map_type(enum pinctrl_map_type type)
1122{
1123 static const char * const names[] = {
1124 "INVALID",
1125 "DUMMY_STATE",
1126 "MUX_GROUP",
1127 "CONFIGS_PIN",
1128 "CONFIGS_GROUP",
1129 };
1130
1131 if (type >= ARRAY_SIZE(names))
1132 return "UNKNOWN";
1133
1134 return names[type];
1135}
1136
Stephen Warren3eedb432012-02-23 17:04:40 -07001137static int pinctrl_maps_show(struct seq_file *s, void *what)
1138{
1139 struct pinctrl_maps *maps_node;
1140 int i;
1141 struct pinctrl_map const *map;
1142
1143 seq_puts(s, "Pinctrl maps:\n");
1144
Stephen Warren57b676f2012-03-02 13:05:44 -07001145 mutex_lock(&pinctrl_mutex);
1146
Stephen Warren3eedb432012-02-23 17:04:40 -07001147 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001148 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1149 map->dev_name, map->name, map_type(map->type),
1150 map->type);
1151
1152 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1153 seq_printf(s, "controlling device %s\n",
1154 map->ctrl_dev_name);
1155
1156 switch (map->type) {
1157 case PIN_MAP_TYPE_MUX_GROUP:
1158 pinmux_show_map(s, map);
1159 break;
1160 case PIN_MAP_TYPE_CONFIGS_PIN:
1161 case PIN_MAP_TYPE_CONFIGS_GROUP:
1162 pinconf_show_map(s, map);
1163 break;
1164 default:
1165 break;
1166 }
1167
1168 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001169 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001170
1171 mutex_unlock(&pinctrl_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001172
1173 return 0;
1174}
1175
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001176static int pinctrl_show(struct seq_file *s, void *what)
1177{
1178 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001179 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001180 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001181
1182 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001183
1184 mutex_lock(&pinctrl_mutex);
1185
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001186 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001187 seq_printf(s, "device: %s current state: %s\n",
1188 dev_name(p->dev),
1189 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001190
Stephen Warren6e5e9592012-03-02 13:05:47 -07001191 list_for_each_entry(state, &p->states, node) {
1192 seq_printf(s, " state: %s\n", state->name);
1193
1194 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001195 struct pinctrl_dev *pctldev = setting->pctldev;
1196
1197 seq_printf(s, " type: %s controller %s ",
1198 map_type(setting->type),
1199 pinctrl_dev_get_name(pctldev));
1200
1201 switch (setting->type) {
1202 case PIN_MAP_TYPE_MUX_GROUP:
1203 pinmux_show_setting(s, setting);
1204 break;
1205 case PIN_MAP_TYPE_CONFIGS_PIN:
1206 case PIN_MAP_TYPE_CONFIGS_GROUP:
1207 pinconf_show_setting(s, setting);
1208 break;
1209 default:
1210 break;
1211 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001212 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001213 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001214 }
1215
Stephen Warren57b676f2012-03-02 13:05:44 -07001216 mutex_unlock(&pinctrl_mutex);
1217
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001218 return 0;
1219}
1220
Linus Walleij2744e8a2011-05-02 20:50:54 +02001221static int pinctrl_pins_open(struct inode *inode, struct file *file)
1222{
1223 return single_open(file, pinctrl_pins_show, inode->i_private);
1224}
1225
1226static int pinctrl_groups_open(struct inode *inode, struct file *file)
1227{
1228 return single_open(file, pinctrl_groups_show, inode->i_private);
1229}
1230
1231static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1232{
1233 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1234}
1235
1236static int pinctrl_devices_open(struct inode *inode, struct file *file)
1237{
1238 return single_open(file, pinctrl_devices_show, NULL);
1239}
1240
Stephen Warren3eedb432012-02-23 17:04:40 -07001241static int pinctrl_maps_open(struct inode *inode, struct file *file)
1242{
1243 return single_open(file, pinctrl_maps_show, NULL);
1244}
1245
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001246static int pinctrl_open(struct inode *inode, struct file *file)
1247{
1248 return single_open(file, pinctrl_show, NULL);
1249}
1250
Linus Walleij2744e8a2011-05-02 20:50:54 +02001251static const struct file_operations pinctrl_pins_ops = {
1252 .open = pinctrl_pins_open,
1253 .read = seq_read,
1254 .llseek = seq_lseek,
1255 .release = single_release,
1256};
1257
1258static const struct file_operations pinctrl_groups_ops = {
1259 .open = pinctrl_groups_open,
1260 .read = seq_read,
1261 .llseek = seq_lseek,
1262 .release = single_release,
1263};
1264
1265static const struct file_operations pinctrl_gpioranges_ops = {
1266 .open = pinctrl_gpioranges_open,
1267 .read = seq_read,
1268 .llseek = seq_lseek,
1269 .release = single_release,
1270};
1271
1272static const struct file_operations pinctrl_devices_ops = {
1273 .open = pinctrl_devices_open,
1274 .read = seq_read,
1275 .llseek = seq_lseek,
1276 .release = single_release,
1277};
1278
Stephen Warren3eedb432012-02-23 17:04:40 -07001279static const struct file_operations pinctrl_maps_ops = {
1280 .open = pinctrl_maps_open,
1281 .read = seq_read,
1282 .llseek = seq_lseek,
1283 .release = single_release,
1284};
1285
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001286static const struct file_operations pinctrl_ops = {
1287 .open = pinctrl_open,
1288 .read = seq_read,
1289 .llseek = seq_lseek,
1290 .release = single_release,
1291};
1292
Linus Walleij2744e8a2011-05-02 20:50:54 +02001293static struct dentry *debugfs_root;
1294
1295static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1296{
Tony Lindgren02157162012-01-20 08:17:22 -08001297 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001298
Stephen Warren51cd24e2011-12-09 16:59:05 -07001299 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001300 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001301 pctldev->device_root = device_root;
1302
Linus Walleij2744e8a2011-05-02 20:50:54 +02001303 if (IS_ERR(device_root) || !device_root) {
1304 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001305 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001306 return;
1307 }
1308 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1309 device_root, pctldev, &pinctrl_pins_ops);
1310 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1311 device_root, pctldev, &pinctrl_groups_ops);
1312 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1313 device_root, pctldev, &pinctrl_gpioranges_ops);
1314 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +02001315 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001316}
1317
Tony Lindgren02157162012-01-20 08:17:22 -08001318static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1319{
1320 debugfs_remove_recursive(pctldev->device_root);
1321}
1322
Linus Walleij2744e8a2011-05-02 20:50:54 +02001323static void pinctrl_init_debugfs(void)
1324{
1325 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1326 if (IS_ERR(debugfs_root) || !debugfs_root) {
1327 pr_warn("failed to create debugfs directory\n");
1328 debugfs_root = NULL;
1329 return;
1330 }
1331
1332 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1333 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001334 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1335 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001336 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1337 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001338}
1339
1340#else /* CONFIG_DEBUG_FS */
1341
1342static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1343{
1344}
1345
1346static void pinctrl_init_debugfs(void)
1347{
1348}
1349
Tony Lindgren02157162012-01-20 08:17:22 -08001350static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1351{
1352}
1353
Linus Walleij2744e8a2011-05-02 20:50:54 +02001354#endif
1355
Stephen Warrend26bc492012-03-16 14:54:25 -06001356static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1357{
1358 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1359
1360 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301361 !ops->get_groups_count ||
Stephen Warrend26bc492012-03-16 14:54:25 -06001362 !ops->get_group_name ||
1363 !ops->get_group_pins)
1364 return -EINVAL;
1365
Stephen Warren57291ce2012-03-23 10:29:46 -06001366 if (ops->dt_node_to_map && !ops->dt_free_map)
1367 return -EINVAL;
1368
Stephen Warrend26bc492012-03-16 14:54:25 -06001369 return 0;
1370}
1371
Linus Walleij2744e8a2011-05-02 20:50:54 +02001372/**
1373 * pinctrl_register() - register a pin controller device
1374 * @pctldesc: descriptor for this pin controller
1375 * @dev: parent device for this pin controller
1376 * @driver_data: private pin controller data for this pin controller
1377 */
1378struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1379 struct device *dev, void *driver_data)
1380{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001381 struct pinctrl_dev *pctldev;
1382 int ret;
1383
Devendra Nagada9aecb2012-06-16 23:43:16 +05301384 if (!pctldesc)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001385 return NULL;
Devendra Nagada9aecb2012-06-16 23:43:16 +05301386 if (!pctldesc->name)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001387 return NULL;
1388
Stephen Warren02f5b982012-02-22 14:26:00 -07001389 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001390 if (pctldev == NULL) {
1391 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001392 return NULL;
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001393 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001394
1395 /* Initialize pin control device struct */
1396 pctldev->owner = pctldesc->owner;
1397 pctldev->desc = pctldesc;
1398 pctldev->driver_data = driver_data;
1399 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001400 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001401 pctldev->dev = dev;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001402
Stephen Warrend26bc492012-03-16 14:54:25 -06001403 /* check core ops for sanity */
Devendra Nagada9aecb2012-06-16 23:43:16 +05301404 if (pinctrl_check_ops(pctldev)) {
John Crispinad6e1102012-04-26 16:47:11 +02001405 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06001406 goto out_err;
1407 }
1408
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001409 /* If we're implementing pinmuxing, check the ops for sanity */
1410 if (pctldesc->pmxops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301411 if (pinmux_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001412 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001413 }
1414
1415 /* If we're implementing pinconfig, check the ops for sanity */
1416 if (pctldesc->confops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301417 if (pinconf_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001418 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001419 }
1420
Linus Walleij2744e8a2011-05-02 20:50:54 +02001421 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02001422 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001423 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1424 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001425 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001426 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1427 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001428 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001429 }
1430
Stephen Warren57b676f2012-03-02 13:05:44 -07001431 mutex_lock(&pinctrl_mutex);
1432
Stephen Warren8b9c1392012-02-19 23:45:42 -07001433 list_add_tail(&pctldev->node, &pinctrldev_list);
Stephen Warren57b676f2012-03-02 13:05:44 -07001434
Stephen Warren6e5e9592012-03-02 13:05:47 -07001435 pctldev->p = pinctrl_get_locked(pctldev->dev);
1436 if (!IS_ERR(pctldev->p)) {
1437 struct pinctrl_state *s =
1438 pinctrl_lookup_state_locked(pctldev->p,
1439 PINCTRL_STATE_DEFAULT);
John Crispinad6e1102012-04-26 16:47:11 +02001440 if (IS_ERR(s)) {
1441 dev_dbg(dev, "failed to lookup the default state\n");
1442 } else {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301443 if (pinctrl_select_state_locked(pctldev->p, s))
John Crispinad6e1102012-04-26 16:47:11 +02001444 dev_err(dev,
1445 "failed to select default state\n");
John Crispinad6e1102012-04-26 16:47:11 +02001446 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001447 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001448
1449 mutex_unlock(&pinctrl_mutex);
1450
Stephen Warren2304b472012-02-22 14:26:01 -07001451 pinctrl_init_device_debugfs(pctldev);
1452
Linus Walleij2744e8a2011-05-02 20:50:54 +02001453 return pctldev;
1454
Stephen Warren51cd24e2011-12-09 16:59:05 -07001455out_err:
1456 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001457 return NULL;
1458}
1459EXPORT_SYMBOL_GPL(pinctrl_register);
1460
1461/**
1462 * pinctrl_unregister() - unregister pinmux
1463 * @pctldev: pin controller to unregister
1464 *
1465 * Called by pinmux drivers to unregister a pinmux.
1466 */
1467void pinctrl_unregister(struct pinctrl_dev *pctldev)
1468{
Dong Aisheng5d589b02012-05-23 21:22:40 +08001469 struct pinctrl_gpio_range *range, *n;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001470 if (pctldev == NULL)
1471 return;
1472
Tony Lindgren02157162012-01-20 08:17:22 -08001473 pinctrl_remove_device_debugfs(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001474
1475 mutex_lock(&pinctrl_mutex);
1476
Stephen Warren6e5e9592012-03-02 13:05:47 -07001477 if (!IS_ERR(pctldev->p))
1478 pinctrl_put_locked(pctldev->p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -07001479
Linus Walleij2744e8a2011-05-02 20:50:54 +02001480 /* TODO: check that no pinmuxes are still active? */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001481 list_del(&pctldev->node);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001482 /* Destroy descriptor tree */
1483 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1484 pctldev->desc->npins);
Dong Aisheng5d589b02012-05-23 21:22:40 +08001485 /* remove gpio ranges map */
1486 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1487 list_del(&range->node);
1488
Stephen Warren51cd24e2011-12-09 16:59:05 -07001489 kfree(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001490
1491 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001492}
1493EXPORT_SYMBOL_GPL(pinctrl_unregister);
1494
1495static int __init pinctrl_init(void)
1496{
1497 pr_info("initialized pinctrl subsystem\n");
1498 pinctrl_init_debugfs();
1499 return 0;
1500}
1501
1502/* init early since many drivers really need to initialized pinmux early */
1503core_initcall(pinctrl_init);