blob: b1086dcde15dd6d49b9d5cc3153e096164bcffbd [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);
Sachin Kamateb26cc92012-09-25 12:41:40 +0530233 if (pindesc->name == NULL) {
234 kfree(pindesc);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100235 return -ENOMEM;
Sachin Kamateb26cc92012-09-25 12:41:40 +0530236 }
Linus Walleijca53c5f2011-12-14 20:33:37 +0100237 pindesc->dynamic_name = true;
238 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200239
Linus Walleij2744e8a2011-05-02 20:50:54 +0200240 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200241 pr_debug("registered pin %d (%s) on %s\n",
Linus Walleijca53c5f2011-12-14 20:33:37 +0100242 number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200243 return 0;
244}
245
246static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
247 struct pinctrl_pin_desc const *pins,
248 unsigned num_descs)
249{
250 unsigned i;
251 int ret = 0;
252
253 for (i = 0; i < num_descs; i++) {
254 ret = pinctrl_register_one_pin(pctldev,
255 pins[i].number, pins[i].name);
256 if (ret)
257 return ret;
258 }
259
260 return 0;
261}
262
263/**
264 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
265 * @pctldev: pin controller device to check
266 * @gpio: gpio pin to check taken from the global GPIO pin space
267 *
268 * Tries to match a GPIO pin number to the ranges handled by a certain pin
269 * controller, return the range or NULL
270 */
271static struct pinctrl_gpio_range *
272pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
273{
274 struct pinctrl_gpio_range *range = NULL;
275
276 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200277 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
278 /* Check if we're in the valid range */
279 if (gpio >= range->base &&
280 gpio < range->base + range->npins) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200281 return range;
282 }
283 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200284
285 return NULL;
286}
287
288/**
289 * pinctrl_get_device_gpio_range() - find device for GPIO range
290 * @gpio: the pin to locate the pin controller for
291 * @outdev: the pin control device if found
292 * @outrange: the GPIO range if found
293 *
294 * Find the pin controller handling a certain GPIO pin from the pinspace of
295 * the GPIO subsystem, return the device and the matching GPIO range. Returns
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800296 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
297 * may still have not been registered.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200298 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700299static int pinctrl_get_device_gpio_range(unsigned gpio,
300 struct pinctrl_dev **outdev,
301 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200302{
303 struct pinctrl_dev *pctldev = NULL;
304
305 /* Loop over the pin controllers */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200306 list_for_each_entry(pctldev, &pinctrldev_list, node) {
307 struct pinctrl_gpio_range *range;
308
309 range = pinctrl_match_gpio_range(pctldev, gpio);
310 if (range != NULL) {
311 *outdev = pctldev;
312 *outrange = range;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200313 return 0;
314 }
315 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200316
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800317 return -EPROBE_DEFER;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200318}
319
320/**
321 * pinctrl_add_gpio_range() - register a GPIO range for a controller
322 * @pctldev: pin controller device to add the range to
323 * @range: the GPIO range to add
324 *
325 * This adds a range of GPIOs to be handled by a certain pin controller. Call
326 * this to register handled ranges after registering your pin controller.
327 */
328void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
329 struct pinctrl_gpio_range *range)
330{
Stephen Warren57b676f2012-03-02 13:05:44 -0700331 mutex_lock(&pinctrl_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700332 list_add_tail(&range->node, &pctldev->gpio_ranges);
Stephen Warren57b676f2012-03-02 13:05:44 -0700333 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200334}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700335EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200336
Dong Aisheng3e5e00b2012-05-23 21:22:41 +0800337void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
338 struct pinctrl_gpio_range *ranges,
339 unsigned nranges)
340{
341 int i;
342
343 for (i = 0; i < nranges; i++)
344 pinctrl_add_gpio_range(pctldev, &ranges[i]);
345}
346EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
347
Linus Walleij2744e8a2011-05-02 20:50:54 +0200348/**
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530349 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
350 * @pctldev: pin controller device to remove the range from
351 * @range: the GPIO range to remove
352 */
353void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
354 struct pinctrl_gpio_range *range)
355{
356 mutex_lock(&pinctrl_mutex);
357 list_del(&range->node);
358 mutex_unlock(&pinctrl_mutex);
359}
360EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
361
362/**
Linus Walleij7afde8b2011-10-19 17:07:16 +0200363 * pinctrl_get_group_selector() - returns the group selector for a group
364 * @pctldev: the pin controller handling the group
365 * @pin_group: the pin group to look up
366 */
367int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
368 const char *pin_group)
369{
370 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530371 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200372 unsigned group_selector = 0;
373
Viresh Kumard1e90e92012-03-30 11:25:40 +0530374 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200375 const char *gname = pctlops->get_group_name(pctldev,
376 group_selector);
377 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700378 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200379 "found group selector %u for %s\n",
380 group_selector,
381 pin_group);
382 return group_selector;
383 }
384
385 group_selector++;
386 }
387
Stephen Warren51cd24e2011-12-09 16:59:05 -0700388 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200389 pin_group);
390
391 return -EINVAL;
392}
393
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100394/**
395 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
396 * @gpio: the GPIO pin number from the GPIO subsystem number space
397 *
398 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
399 * as part of their gpio_request() semantics, platforms and individual drivers
400 * shall *NOT* request GPIO pins to be muxed in.
401 */
402int pinctrl_request_gpio(unsigned gpio)
403{
404 struct pinctrl_dev *pctldev;
405 struct pinctrl_gpio_range *range;
406 int ret;
407 int pin;
408
Stephen Warren57b676f2012-03-02 13:05:44 -0700409 mutex_lock(&pinctrl_mutex);
410
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100411 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700412 if (ret) {
413 mutex_unlock(&pinctrl_mutex);
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800414 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700415 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100416
417 /* Convert to the pin controllers number space */
418 pin = gpio - range->base + range->pin_base;
419
Stephen Warren57b676f2012-03-02 13:05:44 -0700420 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
421
422 mutex_unlock(&pinctrl_mutex);
423 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100424}
425EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
426
427/**
428 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
429 * @gpio: the GPIO pin number from the GPIO subsystem number space
430 *
431 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
432 * as part of their gpio_free() semantics, platforms and individual drivers
433 * shall *NOT* request GPIO pins to be muxed out.
434 */
435void pinctrl_free_gpio(unsigned gpio)
436{
437 struct pinctrl_dev *pctldev;
438 struct pinctrl_gpio_range *range;
439 int ret;
440 int pin;
441
Stephen Warren57b676f2012-03-02 13:05:44 -0700442 mutex_lock(&pinctrl_mutex);
443
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100444 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700445 if (ret) {
446 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100447 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700448 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100449
450 /* Convert to the pin controllers number space */
451 pin = gpio - range->base + range->pin_base;
452
Stephen Warren57b676f2012-03-02 13:05:44 -0700453 pinmux_free_gpio(pctldev, pin, range);
454
455 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100456}
457EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
458
459static int pinctrl_gpio_direction(unsigned gpio, bool input)
460{
461 struct pinctrl_dev *pctldev;
462 struct pinctrl_gpio_range *range;
463 int ret;
464 int pin;
465
466 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
467 if (ret)
468 return ret;
469
470 /* Convert to the pin controllers number space */
471 pin = gpio - range->base + range->pin_base;
472
473 return pinmux_gpio_direction(pctldev, range, pin, input);
474}
475
476/**
477 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
478 * @gpio: the GPIO pin number from the GPIO subsystem number space
479 *
480 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
481 * as part of their gpio_direction_input() semantics, platforms and individual
482 * drivers shall *NOT* touch pin control GPIO calls.
483 */
484int pinctrl_gpio_direction_input(unsigned gpio)
485{
Stephen Warren57b676f2012-03-02 13:05:44 -0700486 int ret;
487 mutex_lock(&pinctrl_mutex);
488 ret = pinctrl_gpio_direction(gpio, true);
489 mutex_unlock(&pinctrl_mutex);
490 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100491}
492EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
493
494/**
495 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
496 * @gpio: the GPIO pin number from the GPIO subsystem number space
497 *
498 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
499 * as part of their gpio_direction_output() semantics, platforms and individual
500 * drivers shall *NOT* touch pin control GPIO calls.
501 */
502int pinctrl_gpio_direction_output(unsigned gpio)
503{
Stephen Warren57b676f2012-03-02 13:05:44 -0700504 int ret;
505 mutex_lock(&pinctrl_mutex);
506 ret = pinctrl_gpio_direction(gpio, false);
507 mutex_unlock(&pinctrl_mutex);
508 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100509}
510EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
511
Stephen Warren6e5e9592012-03-02 13:05:47 -0700512static struct pinctrl_state *find_state(struct pinctrl *p,
513 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100514{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700515 struct pinctrl_state *state;
516
517 list_for_each_entry(state, &p->states, node)
518 if (!strcmp(state->name, name))
519 return state;
520
521 return NULL;
522}
523
524static struct pinctrl_state *create_state(struct pinctrl *p,
525 const char *name)
526{
527 struct pinctrl_state *state;
528
529 state = kzalloc(sizeof(*state), GFP_KERNEL);
530 if (state == NULL) {
531 dev_err(p->dev,
532 "failed to alloc struct pinctrl_state\n");
533 return ERR_PTR(-ENOMEM);
534 }
535
536 state->name = name;
537 INIT_LIST_HEAD(&state->settings);
538
539 list_add_tail(&state->node, &p->states);
540
541 return state;
542}
543
544static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
545{
546 struct pinctrl_state *state;
547 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700548 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700549
550 state = find_state(p, map->name);
551 if (!state)
552 state = create_state(p, map->name);
553 if (IS_ERR(state))
554 return PTR_ERR(state);
555
Stephen Warren1e2082b2012-03-02 13:05:48 -0700556 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
557 return 0;
558
Stephen Warren6e5e9592012-03-02 13:05:47 -0700559 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
560 if (setting == NULL) {
561 dev_err(p->dev,
562 "failed to alloc struct pinctrl_setting\n");
563 return -ENOMEM;
564 }
565
Stephen Warren1e2082b2012-03-02 13:05:48 -0700566 setting->type = map->type;
567
Stephen Warren6e5e9592012-03-02 13:05:47 -0700568 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
569 if (setting->pctldev == NULL) {
Linus Walleijc05127c2012-04-10 10:00:38 +0200570 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
Stephen Warren6e5e9592012-03-02 13:05:47 -0700571 map->ctrl_dev_name);
572 kfree(setting);
Linus Walleijc05127c2012-04-10 10:00:38 +0200573 /*
574 * OK let us guess that the driver is not there yet, and
575 * let's defer obtaining this pinctrl handle to later...
576 */
577 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700578 }
579
Linus Walleij1a789582012-10-17 20:51:54 +0200580 setting->dev_name = map->dev_name;
581
Stephen Warren1e2082b2012-03-02 13:05:48 -0700582 switch (map->type) {
583 case PIN_MAP_TYPE_MUX_GROUP:
584 ret = pinmux_map_to_setting(map, setting);
585 break;
586 case PIN_MAP_TYPE_CONFIGS_PIN:
587 case PIN_MAP_TYPE_CONFIGS_GROUP:
588 ret = pinconf_map_to_setting(map, setting);
589 break;
590 default:
591 ret = -EINVAL;
592 break;
593 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700594 if (ret < 0) {
595 kfree(setting);
596 return ret;
597 }
598
599 list_add_tail(&setting->node, &state->settings);
600
601 return 0;
602}
603
604static struct pinctrl *find_pinctrl(struct device *dev)
605{
606 struct pinctrl *p;
607
Stephen Warren1e2082b2012-03-02 13:05:48 -0700608 list_for_each_entry(p, &pinctrl_list, node)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700609 if (p->dev == dev)
610 return p;
611
612 return NULL;
613}
614
615static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
616
617static struct pinctrl *create_pinctrl(struct device *dev)
618{
619 struct pinctrl *p;
620 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700621 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100622 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700623 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700624 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100625
626 /*
627 * create the state cookie holder struct pinctrl for each
628 * mapping, this is what consumers will get when requesting
629 * a pin control handle with pinctrl_get()
630 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700631 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700632 if (p == NULL) {
633 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100634 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700635 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700636 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700637 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -0600638 INIT_LIST_HEAD(&p->dt_maps);
639
640 ret = pinctrl_dt_to_map(p);
641 if (ret < 0) {
642 kfree(p);
643 return ERR_PTR(ret);
644 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700645
646 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100647
648 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700649 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -0700650 /* Map must be for this device */
651 if (strcmp(map->dev_name, devname))
652 continue;
653
Stephen Warren6e5e9592012-03-02 13:05:47 -0700654 ret = add_setting(p, map);
655 if (ret < 0) {
656 pinctrl_put_locked(p, false);
657 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100658 }
659 }
660
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100661 /* Add the pinmux to the global list */
Stephen Warren8b9c1392012-02-19 23:45:42 -0700662 list_add_tail(&p->node, &pinctrl_list);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100663
664 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700665}
Stephen Warren7ecdb162012-03-02 13:05:45 -0700666
Stephen Warren6e5e9592012-03-02 13:05:47 -0700667static struct pinctrl *pinctrl_get_locked(struct device *dev)
668{
669 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700670
Stephen Warren6e5e9592012-03-02 13:05:47 -0700671 if (WARN_ON(!dev))
672 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700673
Stephen Warren6e5e9592012-03-02 13:05:47 -0700674 p = find_pinctrl(dev);
675 if (p != NULL)
676 return ERR_PTR(-EBUSY);
677
Richard Genoudd599bfb2012-08-10 16:53:49 +0200678 return create_pinctrl(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100679}
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700680
681/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700682 * pinctrl_get() - retrieves the pinctrl handle for a device
683 * @dev: the device to obtain the handle for
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700684 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700685struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700686{
687 struct pinctrl *p;
688
Stephen Warren57b676f2012-03-02 13:05:44 -0700689 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700690 p = pinctrl_get_locked(dev);
Stephen Warren57b676f2012-03-02 13:05:44 -0700691 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700692
693 return p;
694}
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100695EXPORT_SYMBOL_GPL(pinctrl_get);
696
Stephen Warren6e5e9592012-03-02 13:05:47 -0700697static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -0700698{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700699 struct pinctrl_state *state, *n1;
700 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700701
Stephen Warren6e5e9592012-03-02 13:05:47 -0700702 list_for_each_entry_safe(state, n1, &p->states, node) {
703 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700704 switch (setting->type) {
705 case PIN_MAP_TYPE_MUX_GROUP:
706 if (state == p->state)
707 pinmux_disable_setting(setting);
708 pinmux_free_setting(setting);
709 break;
710 case PIN_MAP_TYPE_CONFIGS_PIN:
711 case PIN_MAP_TYPE_CONFIGS_GROUP:
712 pinconf_free_setting(setting);
713 break;
714 default:
715 break;
716 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700717 list_del(&setting->node);
718 kfree(setting);
719 }
720 list_del(&state->node);
721 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700722 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700723
Stephen Warren57291ce2012-03-23 10:29:46 -0600724 pinctrl_dt_free_maps(p);
725
Stephen Warren6e5e9592012-03-02 13:05:47 -0700726 if (inlist)
727 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700728 kfree(p);
729}
730
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100731/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700732 * pinctrl_put() - release a previously claimed pinctrl handle
733 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100734 */
735void pinctrl_put(struct pinctrl *p)
736{
Stephen Warren57b676f2012-03-02 13:05:44 -0700737 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700738 pinctrl_put_locked(p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -0700739 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100740}
741EXPORT_SYMBOL_GPL(pinctrl_put);
742
Stephen Warren6e5e9592012-03-02 13:05:47 -0700743static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
744 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -0700745{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700746 struct pinctrl_state *state;
747
748 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800749 if (!state) {
750 if (pinctrl_dummy_state) {
751 /* create dummy state */
752 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
753 name);
754 state = create_state(p, name);
Richard Genoudd599bfb2012-08-10 16:53:49 +0200755 } else
756 state = ERR_PTR(-ENODEV);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800757 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700758
759 return state;
760}
761
762/**
763 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
764 * @p: the pinctrl handle to retrieve the state from
765 * @name: the state name to retrieve
766 */
767struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
768{
769 struct pinctrl_state *s;
770
771 mutex_lock(&pinctrl_mutex);
772 s = pinctrl_lookup_state_locked(p, name);
773 mutex_unlock(&pinctrl_mutex);
774
775 return s;
776}
777EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
778
779static int pinctrl_select_state_locked(struct pinctrl *p,
780 struct pinctrl_state *state)
781{
782 struct pinctrl_setting *setting, *setting2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700783 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700784
Stephen Warren6e5e9592012-03-02 13:05:47 -0700785 if (p->state == state)
786 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700787
Stephen Warren6e5e9592012-03-02 13:05:47 -0700788 if (p->state) {
789 /*
790 * The set of groups with a mux configuration in the old state
791 * may not be identical to the set of groups with a mux setting
792 * in the new state. While this might be unusual, it's entirely
793 * possible for the "user"-supplied mapping table to be written
794 * that way. For each group that was configured in the old state
795 * but not in the new state, this code puts that group into a
796 * safe/disabled state.
797 */
798 list_for_each_entry(setting, &p->state->settings, node) {
799 bool found = false;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700800 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
801 continue;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700802 list_for_each_entry(setting2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700803 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
804 continue;
805 if (setting2->data.mux.group ==
806 setting->data.mux.group) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700807 found = true;
808 break;
809 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700810 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700811 if (!found)
812 pinmux_disable_setting(setting);
813 }
814 }
815
816 p->state = state;
817
818 /* Apply all the settings for the new state */
819 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700820 switch (setting->type) {
821 case PIN_MAP_TYPE_MUX_GROUP:
822 ret = pinmux_enable_setting(setting);
823 break;
824 case PIN_MAP_TYPE_CONFIGS_PIN:
825 case PIN_MAP_TYPE_CONFIGS_GROUP:
826 ret = pinconf_apply_setting(setting);
827 break;
828 default:
829 ret = -EINVAL;
830 break;
831 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700832 if (ret < 0) {
833 /* FIXME: Difficult to return to prev state */
834 return ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700835 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700836 }
837
Stephen Warren7ecdb162012-03-02 13:05:45 -0700838 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700839}
840
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100841/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700842 * pinctrl_select() - select/activate/program a pinctrl state to HW
843 * @p: the pinctrl handle for the device that requests configuratio
844 * @state: the state handle to select/activate/program
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100845 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700846int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100847{
Stephen Warren57b676f2012-03-02 13:05:44 -0700848 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700849
Stephen Warren57b676f2012-03-02 13:05:44 -0700850 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700851 ret = pinctrl_select_state_locked(p, state);
Stephen Warren57b676f2012-03-02 13:05:44 -0700852 mutex_unlock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700853
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100854 return ret;
855}
Stephen Warren6e5e9592012-03-02 13:05:47 -0700856EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100857
Stephen Warren6d4ca1f2012-04-16 10:51:00 -0600858static void devm_pinctrl_release(struct device *dev, void *res)
859{
860 pinctrl_put(*(struct pinctrl **)res);
861}
862
863/**
864 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
865 * @dev: the device to obtain the handle for
866 *
867 * If there is a need to explicitly destroy the returned struct pinctrl,
868 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
869 */
870struct pinctrl *devm_pinctrl_get(struct device *dev)
871{
872 struct pinctrl **ptr, *p;
873
874 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
875 if (!ptr)
876 return ERR_PTR(-ENOMEM);
877
878 p = pinctrl_get(dev);
879 if (!IS_ERR(p)) {
880 *ptr = p;
881 devres_add(dev, ptr);
882 } else {
883 devres_free(ptr);
884 }
885
886 return p;
887}
888EXPORT_SYMBOL_GPL(devm_pinctrl_get);
889
890static int devm_pinctrl_match(struct device *dev, void *res, void *data)
891{
892 struct pinctrl **p = res;
893
894 return *p == data;
895}
896
897/**
898 * devm_pinctrl_put() - Resource managed pinctrl_put()
899 * @p: the pinctrl handle to release
900 *
901 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
902 * this function will not need to be called and the resource management
903 * code will ensure that the resource is freed.
904 */
905void devm_pinctrl_put(struct pinctrl *p)
906{
907 WARN_ON(devres_destroy(p->dev, devm_pinctrl_release,
908 devm_pinctrl_match, p));
909 pinctrl_put(p);
910}
911EXPORT_SYMBOL_GPL(devm_pinctrl_put);
912
Stephen Warren57291ce2012-03-23 10:29:46 -0600913int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
914 bool dup, bool locked)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100915{
Stephen Warren1e2082b2012-03-02 13:05:48 -0700916 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700917 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100918
919 pr_debug("add %d pinmux maps\n", num_maps);
920
921 /* First sanity check the new mapping */
922 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700923 if (!maps[i].dev_name) {
924 pr_err("failed to register map %s (%d): no device given\n",
925 maps[i].name, i);
926 return -EINVAL;
927 }
928
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100929 if (!maps[i].name) {
930 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700931 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100932 return -EINVAL;
933 }
934
Stephen Warren1e2082b2012-03-02 13:05:48 -0700935 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
936 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100937 pr_err("failed to register map %s (%d): no pin control device given\n",
938 maps[i].name, i);
939 return -EINVAL;
940 }
941
Stephen Warren1e2082b2012-03-02 13:05:48 -0700942 switch (maps[i].type) {
943 case PIN_MAP_TYPE_DUMMY_STATE:
944 break;
945 case PIN_MAP_TYPE_MUX_GROUP:
946 ret = pinmux_validate_map(&maps[i], i);
947 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -0600948 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700949 break;
950 case PIN_MAP_TYPE_CONFIGS_PIN:
951 case PIN_MAP_TYPE_CONFIGS_GROUP:
952 ret = pinconf_validate_map(&maps[i], i);
953 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -0600954 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700955 break;
956 default:
957 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700958 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -0700959 return -EINVAL;
960 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100961 }
962
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700963 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
964 if (!maps_node) {
965 pr_err("failed to alloc struct pinctrl_maps\n");
966 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100967 }
968
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700969 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -0600970 if (dup) {
971 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
972 GFP_KERNEL);
973 if (!maps_node->maps) {
974 pr_err("failed to duplicate mapping table\n");
975 kfree(maps_node);
976 return -ENOMEM;
977 }
978 } else {
979 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700980 }
981
Stephen Warren57291ce2012-03-23 10:29:46 -0600982 if (!locked)
983 mutex_lock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700984 list_add_tail(&maps_node->node, &pinctrl_maps);
Stephen Warren57291ce2012-03-23 10:29:46 -0600985 if (!locked)
986 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700987
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100988 return 0;
989}
990
Stephen Warren57291ce2012-03-23 10:29:46 -0600991/**
992 * pinctrl_register_mappings() - register a set of pin controller mappings
993 * @maps: the pincontrol mappings table to register. This should probably be
994 * marked with __initdata so it can be discarded after boot. This
995 * function will perform a shallow copy for the mapping entries.
996 * @num_maps: the number of maps in the mapping table
997 */
998int pinctrl_register_mappings(struct pinctrl_map const *maps,
999 unsigned num_maps)
1000{
1001 return pinctrl_register_map(maps, num_maps, true, false);
1002}
1003
1004void pinctrl_unregister_map(struct pinctrl_map const *map)
1005{
1006 struct pinctrl_maps *maps_node;
1007
1008 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1009 if (maps_node->maps == map) {
1010 list_del(&maps_node->node);
1011 return;
1012 }
1013 }
1014}
1015
Linus Walleij2744e8a2011-05-02 20:50:54 +02001016#ifdef CONFIG_DEBUG_FS
1017
1018static int pinctrl_pins_show(struct seq_file *s, void *what)
1019{
1020 struct pinctrl_dev *pctldev = s->private;
1021 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001022 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001023
1024 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001025
Stephen Warren57b676f2012-03-02 13:05:44 -07001026 mutex_lock(&pinctrl_mutex);
1027
Chanho Park706e8522012-01-03 16:47:50 +09001028 /* The pin number can be retrived from the pin controller descriptor */
1029 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001030 struct pin_desc *desc;
1031
Chanho Park706e8522012-01-03 16:47:50 +09001032 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001033 desc = pin_desc_get(pctldev, pin);
1034 /* Pin space may be sparse */
1035 if (desc == NULL)
1036 continue;
1037
1038 seq_printf(s, "pin %d (%s) ", pin,
1039 desc->name ? desc->name : "unnamed");
1040
1041 /* Driver-specific info per pin */
1042 if (ops->pin_dbg_show)
1043 ops->pin_dbg_show(pctldev, s, pin);
1044
1045 seq_puts(s, "\n");
1046 }
1047
Stephen Warren57b676f2012-03-02 13:05:44 -07001048 mutex_unlock(&pinctrl_mutex);
1049
Linus Walleij2744e8a2011-05-02 20:50:54 +02001050 return 0;
1051}
1052
1053static int pinctrl_groups_show(struct seq_file *s, void *what)
1054{
1055 struct pinctrl_dev *pctldev = s->private;
1056 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301057 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001058
Viresh Kumard1e90e92012-03-30 11:25:40 +05301059 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001060 mutex_lock(&pinctrl_mutex);
1061
Linus Walleij2744e8a2011-05-02 20:50:54 +02001062 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301063 while (selector < ngroups) {
Stephen Warrena5818a82011-10-19 16:19:25 -06001064 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001065 unsigned num_pins;
1066 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001067 const char *pname;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001068 int ret;
1069 int i;
1070
1071 ret = ops->get_group_pins(pctldev, selector,
1072 &pins, &num_pins);
1073 if (ret)
1074 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1075 gname);
1076 else {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001077 seq_printf(s, "group: %s\n", gname);
1078 for (i = 0; i < num_pins; i++) {
1079 pname = pin_get_name(pctldev, pins[i]);
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001080 if (WARN_ON(!pname)) {
1081 mutex_unlock(&pinctrl_mutex);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001082 return -EINVAL;
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001083 }
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001084 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1085 }
1086 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001087 }
1088 selector++;
1089 }
1090
Stephen Warren57b676f2012-03-02 13:05:44 -07001091 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001092
1093 return 0;
1094}
1095
1096static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1097{
1098 struct pinctrl_dev *pctldev = s->private;
1099 struct pinctrl_gpio_range *range = NULL;
1100
1101 seq_puts(s, "GPIO ranges handled:\n");
1102
Stephen Warren57b676f2012-03-02 13:05:44 -07001103 mutex_lock(&pinctrl_mutex);
1104
Linus Walleij2744e8a2011-05-02 20:50:54 +02001105 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001106 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Linus Walleij75d66422011-11-16 09:58:51 +01001107 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1108 range->id, range->name,
1109 range->base, (range->base + range->npins - 1),
1110 range->pin_base,
1111 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001112 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001113
1114 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001115
1116 return 0;
1117}
1118
1119static int pinctrl_devices_show(struct seq_file *s, void *what)
1120{
1121 struct pinctrl_dev *pctldev;
1122
Linus Walleijae6b4d82011-10-19 18:14:33 +02001123 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001124
1125 mutex_lock(&pinctrl_mutex);
1126
Linus Walleij2744e8a2011-05-02 20:50:54 +02001127 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1128 seq_printf(s, "%s ", pctldev->desc->name);
1129 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001130 seq_puts(s, "yes ");
1131 else
1132 seq_puts(s, "no ");
1133 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001134 seq_puts(s, "yes");
1135 else
1136 seq_puts(s, "no");
1137 seq_puts(s, "\n");
1138 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001139
1140 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001141
1142 return 0;
1143}
1144
Stephen Warren1e2082b2012-03-02 13:05:48 -07001145static inline const char *map_type(enum pinctrl_map_type type)
1146{
1147 static const char * const names[] = {
1148 "INVALID",
1149 "DUMMY_STATE",
1150 "MUX_GROUP",
1151 "CONFIGS_PIN",
1152 "CONFIGS_GROUP",
1153 };
1154
1155 if (type >= ARRAY_SIZE(names))
1156 return "UNKNOWN";
1157
1158 return names[type];
1159}
1160
Stephen Warren3eedb432012-02-23 17:04:40 -07001161static int pinctrl_maps_show(struct seq_file *s, void *what)
1162{
1163 struct pinctrl_maps *maps_node;
1164 int i;
1165 struct pinctrl_map const *map;
1166
1167 seq_puts(s, "Pinctrl maps:\n");
1168
Stephen Warren57b676f2012-03-02 13:05:44 -07001169 mutex_lock(&pinctrl_mutex);
1170
Stephen Warren3eedb432012-02-23 17:04:40 -07001171 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001172 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1173 map->dev_name, map->name, map_type(map->type),
1174 map->type);
1175
1176 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1177 seq_printf(s, "controlling device %s\n",
1178 map->ctrl_dev_name);
1179
1180 switch (map->type) {
1181 case PIN_MAP_TYPE_MUX_GROUP:
1182 pinmux_show_map(s, map);
1183 break;
1184 case PIN_MAP_TYPE_CONFIGS_PIN:
1185 case PIN_MAP_TYPE_CONFIGS_GROUP:
1186 pinconf_show_map(s, map);
1187 break;
1188 default:
1189 break;
1190 }
1191
1192 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001193 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001194
1195 mutex_unlock(&pinctrl_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001196
1197 return 0;
1198}
1199
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001200static int pinctrl_show(struct seq_file *s, void *what)
1201{
1202 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001203 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001204 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001205
1206 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001207
1208 mutex_lock(&pinctrl_mutex);
1209
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001210 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001211 seq_printf(s, "device: %s current state: %s\n",
1212 dev_name(p->dev),
1213 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001214
Stephen Warren6e5e9592012-03-02 13:05:47 -07001215 list_for_each_entry(state, &p->states, node) {
1216 seq_printf(s, " state: %s\n", state->name);
1217
1218 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001219 struct pinctrl_dev *pctldev = setting->pctldev;
1220
1221 seq_printf(s, " type: %s controller %s ",
1222 map_type(setting->type),
1223 pinctrl_dev_get_name(pctldev));
1224
1225 switch (setting->type) {
1226 case PIN_MAP_TYPE_MUX_GROUP:
1227 pinmux_show_setting(s, setting);
1228 break;
1229 case PIN_MAP_TYPE_CONFIGS_PIN:
1230 case PIN_MAP_TYPE_CONFIGS_GROUP:
1231 pinconf_show_setting(s, setting);
1232 break;
1233 default:
1234 break;
1235 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001236 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001237 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001238 }
1239
Stephen Warren57b676f2012-03-02 13:05:44 -07001240 mutex_unlock(&pinctrl_mutex);
1241
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001242 return 0;
1243}
1244
Linus Walleij2744e8a2011-05-02 20:50:54 +02001245static int pinctrl_pins_open(struct inode *inode, struct file *file)
1246{
1247 return single_open(file, pinctrl_pins_show, inode->i_private);
1248}
1249
1250static int pinctrl_groups_open(struct inode *inode, struct file *file)
1251{
1252 return single_open(file, pinctrl_groups_show, inode->i_private);
1253}
1254
1255static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1256{
1257 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1258}
1259
1260static int pinctrl_devices_open(struct inode *inode, struct file *file)
1261{
1262 return single_open(file, pinctrl_devices_show, NULL);
1263}
1264
Stephen Warren3eedb432012-02-23 17:04:40 -07001265static int pinctrl_maps_open(struct inode *inode, struct file *file)
1266{
1267 return single_open(file, pinctrl_maps_show, NULL);
1268}
1269
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001270static int pinctrl_open(struct inode *inode, struct file *file)
1271{
1272 return single_open(file, pinctrl_show, NULL);
1273}
1274
Linus Walleij2744e8a2011-05-02 20:50:54 +02001275static const struct file_operations pinctrl_pins_ops = {
1276 .open = pinctrl_pins_open,
1277 .read = seq_read,
1278 .llseek = seq_lseek,
1279 .release = single_release,
1280};
1281
1282static const struct file_operations pinctrl_groups_ops = {
1283 .open = pinctrl_groups_open,
1284 .read = seq_read,
1285 .llseek = seq_lseek,
1286 .release = single_release,
1287};
1288
1289static const struct file_operations pinctrl_gpioranges_ops = {
1290 .open = pinctrl_gpioranges_open,
1291 .read = seq_read,
1292 .llseek = seq_lseek,
1293 .release = single_release,
1294};
1295
1296static const struct file_operations pinctrl_devices_ops = {
1297 .open = pinctrl_devices_open,
1298 .read = seq_read,
1299 .llseek = seq_lseek,
1300 .release = single_release,
1301};
1302
Stephen Warren3eedb432012-02-23 17:04:40 -07001303static const struct file_operations pinctrl_maps_ops = {
1304 .open = pinctrl_maps_open,
1305 .read = seq_read,
1306 .llseek = seq_lseek,
1307 .release = single_release,
1308};
1309
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001310static const struct file_operations pinctrl_ops = {
1311 .open = pinctrl_open,
1312 .read = seq_read,
1313 .llseek = seq_lseek,
1314 .release = single_release,
1315};
1316
Linus Walleij2744e8a2011-05-02 20:50:54 +02001317static struct dentry *debugfs_root;
1318
1319static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1320{
Tony Lindgren02157162012-01-20 08:17:22 -08001321 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001322
Stephen Warren51cd24e2011-12-09 16:59:05 -07001323 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001324 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001325 pctldev->device_root = device_root;
1326
Linus Walleij2744e8a2011-05-02 20:50:54 +02001327 if (IS_ERR(device_root) || !device_root) {
1328 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001329 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001330 return;
1331 }
1332 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1333 device_root, pctldev, &pinctrl_pins_ops);
1334 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1335 device_root, pctldev, &pinctrl_groups_ops);
1336 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1337 device_root, pctldev, &pinctrl_gpioranges_ops);
1338 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +02001339 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001340}
1341
Tony Lindgren02157162012-01-20 08:17:22 -08001342static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1343{
1344 debugfs_remove_recursive(pctldev->device_root);
1345}
1346
Linus Walleij2744e8a2011-05-02 20:50:54 +02001347static void pinctrl_init_debugfs(void)
1348{
1349 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1350 if (IS_ERR(debugfs_root) || !debugfs_root) {
1351 pr_warn("failed to create debugfs directory\n");
1352 debugfs_root = NULL;
1353 return;
1354 }
1355
1356 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1357 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001358 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1359 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001360 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1361 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001362}
1363
1364#else /* CONFIG_DEBUG_FS */
1365
1366static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1367{
1368}
1369
1370static void pinctrl_init_debugfs(void)
1371{
1372}
1373
Tony Lindgren02157162012-01-20 08:17:22 -08001374static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1375{
1376}
1377
Linus Walleij2744e8a2011-05-02 20:50:54 +02001378#endif
1379
Stephen Warrend26bc492012-03-16 14:54:25 -06001380static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1381{
1382 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1383
1384 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301385 !ops->get_groups_count ||
Stephen Warrend26bc492012-03-16 14:54:25 -06001386 !ops->get_group_name ||
1387 !ops->get_group_pins)
1388 return -EINVAL;
1389
Stephen Warren57291ce2012-03-23 10:29:46 -06001390 if (ops->dt_node_to_map && !ops->dt_free_map)
1391 return -EINVAL;
1392
Stephen Warrend26bc492012-03-16 14:54:25 -06001393 return 0;
1394}
1395
Linus Walleij2744e8a2011-05-02 20:50:54 +02001396/**
1397 * pinctrl_register() - register a pin controller device
1398 * @pctldesc: descriptor for this pin controller
1399 * @dev: parent device for this pin controller
1400 * @driver_data: private pin controller data for this pin controller
1401 */
1402struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1403 struct device *dev, void *driver_data)
1404{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001405 struct pinctrl_dev *pctldev;
1406 int ret;
1407
Devendra Nagada9aecb2012-06-16 23:43:16 +05301408 if (!pctldesc)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001409 return NULL;
Devendra Nagada9aecb2012-06-16 23:43:16 +05301410 if (!pctldesc->name)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001411 return NULL;
1412
Stephen Warren02f5b982012-02-22 14:26:00 -07001413 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001414 if (pctldev == NULL) {
1415 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001416 return NULL;
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001417 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001418
1419 /* Initialize pin control device struct */
1420 pctldev->owner = pctldesc->owner;
1421 pctldev->desc = pctldesc;
1422 pctldev->driver_data = driver_data;
1423 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001424 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001425 pctldev->dev = dev;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001426
Stephen Warrend26bc492012-03-16 14:54:25 -06001427 /* check core ops for sanity */
Devendra Nagada9aecb2012-06-16 23:43:16 +05301428 if (pinctrl_check_ops(pctldev)) {
John Crispinad6e1102012-04-26 16:47:11 +02001429 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06001430 goto out_err;
1431 }
1432
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001433 /* If we're implementing pinmuxing, check the ops for sanity */
1434 if (pctldesc->pmxops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301435 if (pinmux_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001436 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001437 }
1438
1439 /* If we're implementing pinconfig, check the ops for sanity */
1440 if (pctldesc->confops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301441 if (pinconf_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001442 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001443 }
1444
Linus Walleij2744e8a2011-05-02 20:50:54 +02001445 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02001446 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001447 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1448 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001449 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001450 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1451 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001452 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001453 }
1454
Stephen Warren57b676f2012-03-02 13:05:44 -07001455 mutex_lock(&pinctrl_mutex);
1456
Stephen Warren8b9c1392012-02-19 23:45:42 -07001457 list_add_tail(&pctldev->node, &pinctrldev_list);
Stephen Warren57b676f2012-03-02 13:05:44 -07001458
Stephen Warren6e5e9592012-03-02 13:05:47 -07001459 pctldev->p = pinctrl_get_locked(pctldev->dev);
1460 if (!IS_ERR(pctldev->p)) {
1461 struct pinctrl_state *s =
1462 pinctrl_lookup_state_locked(pctldev->p,
1463 PINCTRL_STATE_DEFAULT);
John Crispinad6e1102012-04-26 16:47:11 +02001464 if (IS_ERR(s)) {
1465 dev_dbg(dev, "failed to lookup the default state\n");
1466 } else {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301467 if (pinctrl_select_state_locked(pctldev->p, s))
John Crispinad6e1102012-04-26 16:47:11 +02001468 dev_err(dev,
1469 "failed to select default state\n");
John Crispinad6e1102012-04-26 16:47:11 +02001470 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001471 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001472
1473 mutex_unlock(&pinctrl_mutex);
1474
Stephen Warren2304b472012-02-22 14:26:01 -07001475 pinctrl_init_device_debugfs(pctldev);
1476
Linus Walleij2744e8a2011-05-02 20:50:54 +02001477 return pctldev;
1478
Stephen Warren51cd24e2011-12-09 16:59:05 -07001479out_err:
1480 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001481 return NULL;
1482}
1483EXPORT_SYMBOL_GPL(pinctrl_register);
1484
1485/**
1486 * pinctrl_unregister() - unregister pinmux
1487 * @pctldev: pin controller to unregister
1488 *
1489 * Called by pinmux drivers to unregister a pinmux.
1490 */
1491void pinctrl_unregister(struct pinctrl_dev *pctldev)
1492{
Dong Aisheng5d589b02012-05-23 21:22:40 +08001493 struct pinctrl_gpio_range *range, *n;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001494 if (pctldev == NULL)
1495 return;
1496
Tony Lindgren02157162012-01-20 08:17:22 -08001497 pinctrl_remove_device_debugfs(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001498
1499 mutex_lock(&pinctrl_mutex);
1500
Stephen Warren6e5e9592012-03-02 13:05:47 -07001501 if (!IS_ERR(pctldev->p))
1502 pinctrl_put_locked(pctldev->p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -07001503
Linus Walleij2744e8a2011-05-02 20:50:54 +02001504 /* TODO: check that no pinmuxes are still active? */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001505 list_del(&pctldev->node);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001506 /* Destroy descriptor tree */
1507 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1508 pctldev->desc->npins);
Dong Aisheng5d589b02012-05-23 21:22:40 +08001509 /* remove gpio ranges map */
1510 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1511 list_del(&range->node);
1512
Stephen Warren51cd24e2011-12-09 16:59:05 -07001513 kfree(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001514
1515 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001516}
1517EXPORT_SYMBOL_GPL(pinctrl_unregister);
1518
1519static int __init pinctrl_init(void)
1520{
1521 pr_info("initialized pinctrl subsystem\n");
1522 pinctrl_init_debugfs();
1523 return 0;
1524}
1525
1526/* init early since many drivers really need to initialized pinmux early */
1527core_initcall(pinctrl_init);