blob: 50d9c289cffdc3df111cc2e04986e614c467b773 [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/**
336 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
337 * @pctldev: pin controller device to remove the range from
338 * @range: the GPIO range to remove
339 */
340void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
341 struct pinctrl_gpio_range *range)
342{
Stephen Warren57b676f2012-03-02 13:05:44 -0700343 mutex_lock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200344 list_del(&range->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700345 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200346}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700347EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200348
Linus Walleij7afde8b2011-10-19 17:07:16 +0200349/**
350 * pinctrl_get_group_selector() - returns the group selector for a group
351 * @pctldev: the pin controller handling the group
352 * @pin_group: the pin group to look up
353 */
354int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
355 const char *pin_group)
356{
357 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530358 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200359 unsigned group_selector = 0;
360
Viresh Kumard1e90e92012-03-30 11:25:40 +0530361 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200362 const char *gname = pctlops->get_group_name(pctldev,
363 group_selector);
364 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700365 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200366 "found group selector %u for %s\n",
367 group_selector,
368 pin_group);
369 return group_selector;
370 }
371
372 group_selector++;
373 }
374
Stephen Warren51cd24e2011-12-09 16:59:05 -0700375 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200376 pin_group);
377
378 return -EINVAL;
379}
380
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100381/**
382 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
383 * @gpio: the GPIO pin number from the GPIO subsystem number space
384 *
385 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
386 * as part of their gpio_request() semantics, platforms and individual drivers
387 * shall *NOT* request GPIO pins to be muxed in.
388 */
389int pinctrl_request_gpio(unsigned gpio)
390{
391 struct pinctrl_dev *pctldev;
392 struct pinctrl_gpio_range *range;
393 int ret;
394 int pin;
395
Stephen Warren57b676f2012-03-02 13:05:44 -0700396 mutex_lock(&pinctrl_mutex);
397
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100398 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700399 if (ret) {
400 mutex_unlock(&pinctrl_mutex);
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800401 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700402 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100403
404 /* Convert to the pin controllers number space */
405 pin = gpio - range->base + range->pin_base;
406
Stephen Warren57b676f2012-03-02 13:05:44 -0700407 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
408
409 mutex_unlock(&pinctrl_mutex);
410 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100411}
412EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
413
414/**
415 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
416 * @gpio: the GPIO pin number from the GPIO subsystem number space
417 *
418 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
419 * as part of their gpio_free() semantics, platforms and individual drivers
420 * shall *NOT* request GPIO pins to be muxed out.
421 */
422void pinctrl_free_gpio(unsigned gpio)
423{
424 struct pinctrl_dev *pctldev;
425 struct pinctrl_gpio_range *range;
426 int ret;
427 int pin;
428
Stephen Warren57b676f2012-03-02 13:05:44 -0700429 mutex_lock(&pinctrl_mutex);
430
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100431 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700432 if (ret) {
433 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100434 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700435 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100436
437 /* Convert to the pin controllers number space */
438 pin = gpio - range->base + range->pin_base;
439
Stephen Warren57b676f2012-03-02 13:05:44 -0700440 pinmux_free_gpio(pctldev, pin, range);
441
442 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100443}
444EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
445
446static int pinctrl_gpio_direction(unsigned gpio, bool input)
447{
448 struct pinctrl_dev *pctldev;
449 struct pinctrl_gpio_range *range;
450 int ret;
451 int pin;
452
453 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
454 if (ret)
455 return ret;
456
457 /* Convert to the pin controllers number space */
458 pin = gpio - range->base + range->pin_base;
459
460 return pinmux_gpio_direction(pctldev, range, pin, input);
461}
462
463/**
464 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
465 * @gpio: the GPIO pin number from the GPIO subsystem number space
466 *
467 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
468 * as part of their gpio_direction_input() semantics, platforms and individual
469 * drivers shall *NOT* touch pin control GPIO calls.
470 */
471int pinctrl_gpio_direction_input(unsigned gpio)
472{
Stephen Warren57b676f2012-03-02 13:05:44 -0700473 int ret;
474 mutex_lock(&pinctrl_mutex);
475 ret = pinctrl_gpio_direction(gpio, true);
476 mutex_unlock(&pinctrl_mutex);
477 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100478}
479EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
480
481/**
482 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
483 * @gpio: the GPIO pin number from the GPIO subsystem number space
484 *
485 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
486 * as part of their gpio_direction_output() semantics, platforms and individual
487 * drivers shall *NOT* touch pin control GPIO calls.
488 */
489int pinctrl_gpio_direction_output(unsigned gpio)
490{
Stephen Warren57b676f2012-03-02 13:05:44 -0700491 int ret;
492 mutex_lock(&pinctrl_mutex);
493 ret = pinctrl_gpio_direction(gpio, false);
494 mutex_unlock(&pinctrl_mutex);
495 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100496}
497EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
498
Stephen Warren6e5e9592012-03-02 13:05:47 -0700499static struct pinctrl_state *find_state(struct pinctrl *p,
500 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100501{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700502 struct pinctrl_state *state;
503
504 list_for_each_entry(state, &p->states, node)
505 if (!strcmp(state->name, name))
506 return state;
507
508 return NULL;
509}
510
511static struct pinctrl_state *create_state(struct pinctrl *p,
512 const char *name)
513{
514 struct pinctrl_state *state;
515
516 state = kzalloc(sizeof(*state), GFP_KERNEL);
517 if (state == NULL) {
518 dev_err(p->dev,
519 "failed to alloc struct pinctrl_state\n");
520 return ERR_PTR(-ENOMEM);
521 }
522
523 state->name = name;
524 INIT_LIST_HEAD(&state->settings);
525
526 list_add_tail(&state->node, &p->states);
527
528 return state;
529}
530
531static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
532{
533 struct pinctrl_state *state;
534 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700535 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700536
537 state = find_state(p, map->name);
538 if (!state)
539 state = create_state(p, map->name);
540 if (IS_ERR(state))
541 return PTR_ERR(state);
542
Stephen Warren1e2082b2012-03-02 13:05:48 -0700543 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
544 return 0;
545
Stephen Warren6e5e9592012-03-02 13:05:47 -0700546 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
547 if (setting == NULL) {
548 dev_err(p->dev,
549 "failed to alloc struct pinctrl_setting\n");
550 return -ENOMEM;
551 }
552
Stephen Warren1e2082b2012-03-02 13:05:48 -0700553 setting->type = map->type;
554
Stephen Warren6e5e9592012-03-02 13:05:47 -0700555 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
556 if (setting->pctldev == NULL) {
Linus Walleijc05127c2012-04-10 10:00:38 +0200557 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
Stephen Warren6e5e9592012-03-02 13:05:47 -0700558 map->ctrl_dev_name);
559 kfree(setting);
Linus Walleijc05127c2012-04-10 10:00:38 +0200560 /*
561 * OK let us guess that the driver is not there yet, and
562 * let's defer obtaining this pinctrl handle to later...
563 */
564 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700565 }
566
Stephen Warren1e2082b2012-03-02 13:05:48 -0700567 switch (map->type) {
568 case PIN_MAP_TYPE_MUX_GROUP:
569 ret = pinmux_map_to_setting(map, setting);
570 break;
571 case PIN_MAP_TYPE_CONFIGS_PIN:
572 case PIN_MAP_TYPE_CONFIGS_GROUP:
573 ret = pinconf_map_to_setting(map, setting);
574 break;
575 default:
576 ret = -EINVAL;
577 break;
578 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700579 if (ret < 0) {
580 kfree(setting);
581 return ret;
582 }
583
584 list_add_tail(&setting->node, &state->settings);
585
586 return 0;
587}
588
589static struct pinctrl *find_pinctrl(struct device *dev)
590{
591 struct pinctrl *p;
592
Stephen Warren1e2082b2012-03-02 13:05:48 -0700593 list_for_each_entry(p, &pinctrl_list, node)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700594 if (p->dev == dev)
595 return p;
596
597 return NULL;
598}
599
600static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
601
602static struct pinctrl *create_pinctrl(struct device *dev)
603{
604 struct pinctrl *p;
605 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700606 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100607 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700608 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700609 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100610
611 /*
612 * create the state cookie holder struct pinctrl for each
613 * mapping, this is what consumers will get when requesting
614 * a pin control handle with pinctrl_get()
615 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700616 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700617 if (p == NULL) {
618 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100619 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700620 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700621 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700622 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -0600623 INIT_LIST_HEAD(&p->dt_maps);
624
625 ret = pinctrl_dt_to_map(p);
626 if (ret < 0) {
627 kfree(p);
628 return ERR_PTR(ret);
629 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700630
631 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100632
633 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700634 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -0700635 /* Map must be for this device */
636 if (strcmp(map->dev_name, devname))
637 continue;
638
Stephen Warren6e5e9592012-03-02 13:05:47 -0700639 ret = add_setting(p, map);
640 if (ret < 0) {
641 pinctrl_put_locked(p, false);
642 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100643 }
644 }
645
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100646 /* Add the pinmux to the global list */
Stephen Warren8b9c1392012-02-19 23:45:42 -0700647 list_add_tail(&p->node, &pinctrl_list);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100648
649 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700650}
Stephen Warren7ecdb162012-03-02 13:05:45 -0700651
Stephen Warren6e5e9592012-03-02 13:05:47 -0700652static struct pinctrl *pinctrl_get_locked(struct device *dev)
653{
654 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700655
Stephen Warren6e5e9592012-03-02 13:05:47 -0700656 if (WARN_ON(!dev))
657 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700658
Stephen Warren6e5e9592012-03-02 13:05:47 -0700659 p = find_pinctrl(dev);
660 if (p != NULL)
661 return ERR_PTR(-EBUSY);
662
663 p = create_pinctrl(dev);
664 if (IS_ERR(p))
665 return p;
666
667 return p;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100668}
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700669
670/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700671 * pinctrl_get() - retrieves the pinctrl handle for a device
672 * @dev: the device to obtain the handle for
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700673 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700674struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700675{
676 struct pinctrl *p;
677
Stephen Warren57b676f2012-03-02 13:05:44 -0700678 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700679 p = pinctrl_get_locked(dev);
Stephen Warren57b676f2012-03-02 13:05:44 -0700680 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700681
682 return p;
683}
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100684EXPORT_SYMBOL_GPL(pinctrl_get);
685
Stephen Warren6e5e9592012-03-02 13:05:47 -0700686static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -0700687{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700688 struct pinctrl_state *state, *n1;
689 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700690
Stephen Warren6e5e9592012-03-02 13:05:47 -0700691 list_for_each_entry_safe(state, n1, &p->states, node) {
692 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700693 switch (setting->type) {
694 case PIN_MAP_TYPE_MUX_GROUP:
695 if (state == p->state)
696 pinmux_disable_setting(setting);
697 pinmux_free_setting(setting);
698 break;
699 case PIN_MAP_TYPE_CONFIGS_PIN:
700 case PIN_MAP_TYPE_CONFIGS_GROUP:
701 pinconf_free_setting(setting);
702 break;
703 default:
704 break;
705 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700706 list_del(&setting->node);
707 kfree(setting);
708 }
709 list_del(&state->node);
710 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700711 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700712
Stephen Warren57291ce2012-03-23 10:29:46 -0600713 pinctrl_dt_free_maps(p);
714
Stephen Warren6e5e9592012-03-02 13:05:47 -0700715 if (inlist)
716 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700717 kfree(p);
718}
719
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100720/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700721 * pinctrl_put() - release a previously claimed pinctrl handle
722 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100723 */
724void pinctrl_put(struct pinctrl *p)
725{
Stephen Warren57b676f2012-03-02 13:05:44 -0700726 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700727 pinctrl_put_locked(p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -0700728 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100729}
730EXPORT_SYMBOL_GPL(pinctrl_put);
731
Stephen Warren6e5e9592012-03-02 13:05:47 -0700732static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
733 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -0700734{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700735 struct pinctrl_state *state;
736
737 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800738 if (!state) {
739 if (pinctrl_dummy_state) {
740 /* create dummy state */
741 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
742 name);
743 state = create_state(p, name);
744 if (IS_ERR(state))
745 return state;
746 } else {
747 return ERR_PTR(-ENODEV);
748 }
749 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700750
751 return state;
752}
753
754/**
755 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
756 * @p: the pinctrl handle to retrieve the state from
757 * @name: the state name to retrieve
758 */
759struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
760{
761 struct pinctrl_state *s;
762
763 mutex_lock(&pinctrl_mutex);
764 s = pinctrl_lookup_state_locked(p, name);
765 mutex_unlock(&pinctrl_mutex);
766
767 return s;
768}
769EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
770
771static int pinctrl_select_state_locked(struct pinctrl *p,
772 struct pinctrl_state *state)
773{
774 struct pinctrl_setting *setting, *setting2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700775 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700776
Stephen Warren6e5e9592012-03-02 13:05:47 -0700777 if (p->state == state)
778 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700779
Stephen Warren6e5e9592012-03-02 13:05:47 -0700780 if (p->state) {
781 /*
782 * The set of groups with a mux configuration in the old state
783 * may not be identical to the set of groups with a mux setting
784 * in the new state. While this might be unusual, it's entirely
785 * possible for the "user"-supplied mapping table to be written
786 * that way. For each group that was configured in the old state
787 * but not in the new state, this code puts that group into a
788 * safe/disabled state.
789 */
790 list_for_each_entry(setting, &p->state->settings, node) {
791 bool found = false;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700792 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
793 continue;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700794 list_for_each_entry(setting2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700795 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
796 continue;
797 if (setting2->data.mux.group ==
798 setting->data.mux.group) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700799 found = true;
800 break;
801 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700802 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700803 if (!found)
804 pinmux_disable_setting(setting);
805 }
806 }
807
808 p->state = state;
809
810 /* Apply all the settings for the new state */
811 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700812 switch (setting->type) {
813 case PIN_MAP_TYPE_MUX_GROUP:
814 ret = pinmux_enable_setting(setting);
815 break;
816 case PIN_MAP_TYPE_CONFIGS_PIN:
817 case PIN_MAP_TYPE_CONFIGS_GROUP:
818 ret = pinconf_apply_setting(setting);
819 break;
820 default:
821 ret = -EINVAL;
822 break;
823 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700824 if (ret < 0) {
825 /* FIXME: Difficult to return to prev state */
826 return ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700827 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700828 }
829
Stephen Warren7ecdb162012-03-02 13:05:45 -0700830 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700831}
832
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100833/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700834 * pinctrl_select() - select/activate/program a pinctrl state to HW
835 * @p: the pinctrl handle for the device that requests configuratio
836 * @state: the state handle to select/activate/program
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100837 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700838int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100839{
Stephen Warren57b676f2012-03-02 13:05:44 -0700840 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700841
Stephen Warren57b676f2012-03-02 13:05:44 -0700842 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700843 ret = pinctrl_select_state_locked(p, state);
Stephen Warren57b676f2012-03-02 13:05:44 -0700844 mutex_unlock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700845
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100846 return ret;
847}
Stephen Warren6e5e9592012-03-02 13:05:47 -0700848EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100849
Stephen Warren6d4ca1f2012-04-16 10:51:00 -0600850static void devm_pinctrl_release(struct device *dev, void *res)
851{
852 pinctrl_put(*(struct pinctrl **)res);
853}
854
855/**
856 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
857 * @dev: the device to obtain the handle for
858 *
859 * If there is a need to explicitly destroy the returned struct pinctrl,
860 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
861 */
862struct pinctrl *devm_pinctrl_get(struct device *dev)
863{
864 struct pinctrl **ptr, *p;
865
866 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
867 if (!ptr)
868 return ERR_PTR(-ENOMEM);
869
870 p = pinctrl_get(dev);
871 if (!IS_ERR(p)) {
872 *ptr = p;
873 devres_add(dev, ptr);
874 } else {
875 devres_free(ptr);
876 }
877
878 return p;
879}
880EXPORT_SYMBOL_GPL(devm_pinctrl_get);
881
882static int devm_pinctrl_match(struct device *dev, void *res, void *data)
883{
884 struct pinctrl **p = res;
885
886 return *p == data;
887}
888
889/**
890 * devm_pinctrl_put() - Resource managed pinctrl_put()
891 * @p: the pinctrl handle to release
892 *
893 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
894 * this function will not need to be called and the resource management
895 * code will ensure that the resource is freed.
896 */
897void devm_pinctrl_put(struct pinctrl *p)
898{
899 WARN_ON(devres_destroy(p->dev, devm_pinctrl_release,
900 devm_pinctrl_match, p));
901 pinctrl_put(p);
902}
903EXPORT_SYMBOL_GPL(devm_pinctrl_put);
904
Stephen Warren57291ce2012-03-23 10:29:46 -0600905int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
906 bool dup, bool locked)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100907{
Stephen Warren1e2082b2012-03-02 13:05:48 -0700908 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700909 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100910
911 pr_debug("add %d pinmux maps\n", num_maps);
912
913 /* First sanity check the new mapping */
914 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700915 if (!maps[i].dev_name) {
916 pr_err("failed to register map %s (%d): no device given\n",
917 maps[i].name, i);
918 return -EINVAL;
919 }
920
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100921 if (!maps[i].name) {
922 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700923 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100924 return -EINVAL;
925 }
926
Stephen Warren1e2082b2012-03-02 13:05:48 -0700927 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
928 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100929 pr_err("failed to register map %s (%d): no pin control device given\n",
930 maps[i].name, i);
931 return -EINVAL;
932 }
933
Stephen Warren1e2082b2012-03-02 13:05:48 -0700934 switch (maps[i].type) {
935 case PIN_MAP_TYPE_DUMMY_STATE:
936 break;
937 case PIN_MAP_TYPE_MUX_GROUP:
938 ret = pinmux_validate_map(&maps[i], i);
939 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -0600940 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700941 break;
942 case PIN_MAP_TYPE_CONFIGS_PIN:
943 case PIN_MAP_TYPE_CONFIGS_GROUP:
944 ret = pinconf_validate_map(&maps[i], i);
945 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -0600946 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700947 break;
948 default:
949 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700950 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -0700951 return -EINVAL;
952 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100953 }
954
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700955 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
956 if (!maps_node) {
957 pr_err("failed to alloc struct pinctrl_maps\n");
958 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100959 }
960
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700961 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -0600962 if (dup) {
963 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
964 GFP_KERNEL);
965 if (!maps_node->maps) {
966 pr_err("failed to duplicate mapping table\n");
967 kfree(maps_node);
968 return -ENOMEM;
969 }
970 } else {
971 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700972 }
973
Stephen Warren57291ce2012-03-23 10:29:46 -0600974 if (!locked)
975 mutex_lock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700976 list_add_tail(&maps_node->node, &pinctrl_maps);
Stephen Warren57291ce2012-03-23 10:29:46 -0600977 if (!locked)
978 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700979
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100980 return 0;
981}
982
Stephen Warren57291ce2012-03-23 10:29:46 -0600983/**
984 * pinctrl_register_mappings() - register a set of pin controller mappings
985 * @maps: the pincontrol mappings table to register. This should probably be
986 * marked with __initdata so it can be discarded after boot. This
987 * function will perform a shallow copy for the mapping entries.
988 * @num_maps: the number of maps in the mapping table
989 */
990int pinctrl_register_mappings(struct pinctrl_map const *maps,
991 unsigned num_maps)
992{
993 return pinctrl_register_map(maps, num_maps, true, false);
994}
995
996void pinctrl_unregister_map(struct pinctrl_map const *map)
997{
998 struct pinctrl_maps *maps_node;
999
1000 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1001 if (maps_node->maps == map) {
1002 list_del(&maps_node->node);
1003 return;
1004 }
1005 }
1006}
1007
Linus Walleij2744e8a2011-05-02 20:50:54 +02001008#ifdef CONFIG_DEBUG_FS
1009
1010static int pinctrl_pins_show(struct seq_file *s, void *what)
1011{
1012 struct pinctrl_dev *pctldev = s->private;
1013 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001014 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001015
1016 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001017
Stephen Warren57b676f2012-03-02 13:05:44 -07001018 mutex_lock(&pinctrl_mutex);
1019
Chanho Park706e8522012-01-03 16:47:50 +09001020 /* The pin number can be retrived from the pin controller descriptor */
1021 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001022 struct pin_desc *desc;
1023
Chanho Park706e8522012-01-03 16:47:50 +09001024 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001025 desc = pin_desc_get(pctldev, pin);
1026 /* Pin space may be sparse */
1027 if (desc == NULL)
1028 continue;
1029
1030 seq_printf(s, "pin %d (%s) ", pin,
1031 desc->name ? desc->name : "unnamed");
1032
1033 /* Driver-specific info per pin */
1034 if (ops->pin_dbg_show)
1035 ops->pin_dbg_show(pctldev, s, pin);
1036
1037 seq_puts(s, "\n");
1038 }
1039
Stephen Warren57b676f2012-03-02 13:05:44 -07001040 mutex_unlock(&pinctrl_mutex);
1041
Linus Walleij2744e8a2011-05-02 20:50:54 +02001042 return 0;
1043}
1044
1045static int pinctrl_groups_show(struct seq_file *s, void *what)
1046{
1047 struct pinctrl_dev *pctldev = s->private;
1048 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301049 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001050
Viresh Kumard1e90e92012-03-30 11:25:40 +05301051 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001052 mutex_lock(&pinctrl_mutex);
1053
Linus Walleij2744e8a2011-05-02 20:50:54 +02001054 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301055 while (selector < ngroups) {
Stephen Warrena5818a82011-10-19 16:19:25 -06001056 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001057 unsigned num_pins;
1058 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001059 const char *pname;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001060 int ret;
1061 int i;
1062
1063 ret = ops->get_group_pins(pctldev, selector,
1064 &pins, &num_pins);
1065 if (ret)
1066 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1067 gname);
1068 else {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001069 seq_printf(s, "group: %s\n", gname);
1070 for (i = 0; i < num_pins; i++) {
1071 pname = pin_get_name(pctldev, pins[i]);
1072 if (WARN_ON(!pname))
1073 return -EINVAL;
1074 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1075 }
1076 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001077 }
1078 selector++;
1079 }
1080
Stephen Warren57b676f2012-03-02 13:05:44 -07001081 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001082
1083 return 0;
1084}
1085
1086static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1087{
1088 struct pinctrl_dev *pctldev = s->private;
1089 struct pinctrl_gpio_range *range = NULL;
1090
1091 seq_puts(s, "GPIO ranges handled:\n");
1092
Stephen Warren57b676f2012-03-02 13:05:44 -07001093 mutex_lock(&pinctrl_mutex);
1094
Linus Walleij2744e8a2011-05-02 20:50:54 +02001095 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001096 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Linus Walleij75d66422011-11-16 09:58:51 +01001097 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1098 range->id, range->name,
1099 range->base, (range->base + range->npins - 1),
1100 range->pin_base,
1101 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001102 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001103
1104 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001105
1106 return 0;
1107}
1108
1109static int pinctrl_devices_show(struct seq_file *s, void *what)
1110{
1111 struct pinctrl_dev *pctldev;
1112
Linus Walleijae6b4d82011-10-19 18:14:33 +02001113 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001114
1115 mutex_lock(&pinctrl_mutex);
1116
Linus Walleij2744e8a2011-05-02 20:50:54 +02001117 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1118 seq_printf(s, "%s ", pctldev->desc->name);
1119 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001120 seq_puts(s, "yes ");
1121 else
1122 seq_puts(s, "no ");
1123 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001124 seq_puts(s, "yes");
1125 else
1126 seq_puts(s, "no");
1127 seq_puts(s, "\n");
1128 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001129
1130 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001131
1132 return 0;
1133}
1134
Stephen Warren1e2082b2012-03-02 13:05:48 -07001135static inline const char *map_type(enum pinctrl_map_type type)
1136{
1137 static const char * const names[] = {
1138 "INVALID",
1139 "DUMMY_STATE",
1140 "MUX_GROUP",
1141 "CONFIGS_PIN",
1142 "CONFIGS_GROUP",
1143 };
1144
1145 if (type >= ARRAY_SIZE(names))
1146 return "UNKNOWN";
1147
1148 return names[type];
1149}
1150
Stephen Warren3eedb432012-02-23 17:04:40 -07001151static int pinctrl_maps_show(struct seq_file *s, void *what)
1152{
1153 struct pinctrl_maps *maps_node;
1154 int i;
1155 struct pinctrl_map const *map;
1156
1157 seq_puts(s, "Pinctrl maps:\n");
1158
Stephen Warren57b676f2012-03-02 13:05:44 -07001159 mutex_lock(&pinctrl_mutex);
1160
Stephen Warren3eedb432012-02-23 17:04:40 -07001161 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001162 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1163 map->dev_name, map->name, map_type(map->type),
1164 map->type);
1165
1166 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1167 seq_printf(s, "controlling device %s\n",
1168 map->ctrl_dev_name);
1169
1170 switch (map->type) {
1171 case PIN_MAP_TYPE_MUX_GROUP:
1172 pinmux_show_map(s, map);
1173 break;
1174 case PIN_MAP_TYPE_CONFIGS_PIN:
1175 case PIN_MAP_TYPE_CONFIGS_GROUP:
1176 pinconf_show_map(s, map);
1177 break;
1178 default:
1179 break;
1180 }
1181
1182 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001183 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001184
1185 mutex_unlock(&pinctrl_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001186
1187 return 0;
1188}
1189
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001190static int pinctrl_show(struct seq_file *s, void *what)
1191{
1192 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001193 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001194 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001195
1196 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001197
1198 mutex_lock(&pinctrl_mutex);
1199
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001200 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001201 seq_printf(s, "device: %s current state: %s\n",
1202 dev_name(p->dev),
1203 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001204
Stephen Warren6e5e9592012-03-02 13:05:47 -07001205 list_for_each_entry(state, &p->states, node) {
1206 seq_printf(s, " state: %s\n", state->name);
1207
1208 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001209 struct pinctrl_dev *pctldev = setting->pctldev;
1210
1211 seq_printf(s, " type: %s controller %s ",
1212 map_type(setting->type),
1213 pinctrl_dev_get_name(pctldev));
1214
1215 switch (setting->type) {
1216 case PIN_MAP_TYPE_MUX_GROUP:
1217 pinmux_show_setting(s, setting);
1218 break;
1219 case PIN_MAP_TYPE_CONFIGS_PIN:
1220 case PIN_MAP_TYPE_CONFIGS_GROUP:
1221 pinconf_show_setting(s, setting);
1222 break;
1223 default:
1224 break;
1225 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001226 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001227 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001228 }
1229
Stephen Warren57b676f2012-03-02 13:05:44 -07001230 mutex_unlock(&pinctrl_mutex);
1231
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001232 return 0;
1233}
1234
Linus Walleij2744e8a2011-05-02 20:50:54 +02001235static int pinctrl_pins_open(struct inode *inode, struct file *file)
1236{
1237 return single_open(file, pinctrl_pins_show, inode->i_private);
1238}
1239
1240static int pinctrl_groups_open(struct inode *inode, struct file *file)
1241{
1242 return single_open(file, pinctrl_groups_show, inode->i_private);
1243}
1244
1245static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1246{
1247 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1248}
1249
1250static int pinctrl_devices_open(struct inode *inode, struct file *file)
1251{
1252 return single_open(file, pinctrl_devices_show, NULL);
1253}
1254
Stephen Warren3eedb432012-02-23 17:04:40 -07001255static int pinctrl_maps_open(struct inode *inode, struct file *file)
1256{
1257 return single_open(file, pinctrl_maps_show, NULL);
1258}
1259
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001260static int pinctrl_open(struct inode *inode, struct file *file)
1261{
1262 return single_open(file, pinctrl_show, NULL);
1263}
1264
Linus Walleij2744e8a2011-05-02 20:50:54 +02001265static const struct file_operations pinctrl_pins_ops = {
1266 .open = pinctrl_pins_open,
1267 .read = seq_read,
1268 .llseek = seq_lseek,
1269 .release = single_release,
1270};
1271
1272static const struct file_operations pinctrl_groups_ops = {
1273 .open = pinctrl_groups_open,
1274 .read = seq_read,
1275 .llseek = seq_lseek,
1276 .release = single_release,
1277};
1278
1279static const struct file_operations pinctrl_gpioranges_ops = {
1280 .open = pinctrl_gpioranges_open,
1281 .read = seq_read,
1282 .llseek = seq_lseek,
1283 .release = single_release,
1284};
1285
1286static const struct file_operations pinctrl_devices_ops = {
1287 .open = pinctrl_devices_open,
1288 .read = seq_read,
1289 .llseek = seq_lseek,
1290 .release = single_release,
1291};
1292
Stephen Warren3eedb432012-02-23 17:04:40 -07001293static const struct file_operations pinctrl_maps_ops = {
1294 .open = pinctrl_maps_open,
1295 .read = seq_read,
1296 .llseek = seq_lseek,
1297 .release = single_release,
1298};
1299
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001300static const struct file_operations pinctrl_ops = {
1301 .open = pinctrl_open,
1302 .read = seq_read,
1303 .llseek = seq_lseek,
1304 .release = single_release,
1305};
1306
Linus Walleij2744e8a2011-05-02 20:50:54 +02001307static struct dentry *debugfs_root;
1308
1309static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1310{
Tony Lindgren02157162012-01-20 08:17:22 -08001311 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001312
Stephen Warren51cd24e2011-12-09 16:59:05 -07001313 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001314 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001315 pctldev->device_root = device_root;
1316
Linus Walleij2744e8a2011-05-02 20:50:54 +02001317 if (IS_ERR(device_root) || !device_root) {
1318 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001319 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001320 return;
1321 }
1322 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1323 device_root, pctldev, &pinctrl_pins_ops);
1324 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1325 device_root, pctldev, &pinctrl_groups_ops);
1326 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1327 device_root, pctldev, &pinctrl_gpioranges_ops);
1328 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +02001329 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001330}
1331
Tony Lindgren02157162012-01-20 08:17:22 -08001332static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1333{
1334 debugfs_remove_recursive(pctldev->device_root);
1335}
1336
Linus Walleij2744e8a2011-05-02 20:50:54 +02001337static void pinctrl_init_debugfs(void)
1338{
1339 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1340 if (IS_ERR(debugfs_root) || !debugfs_root) {
1341 pr_warn("failed to create debugfs directory\n");
1342 debugfs_root = NULL;
1343 return;
1344 }
1345
1346 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1347 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001348 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1349 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001350 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1351 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001352}
1353
1354#else /* CONFIG_DEBUG_FS */
1355
1356static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1357{
1358}
1359
1360static void pinctrl_init_debugfs(void)
1361{
1362}
1363
Tony Lindgren02157162012-01-20 08:17:22 -08001364static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1365{
1366}
1367
Linus Walleij2744e8a2011-05-02 20:50:54 +02001368#endif
1369
Stephen Warrend26bc492012-03-16 14:54:25 -06001370static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1371{
1372 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1373
1374 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301375 !ops->get_groups_count ||
Stephen Warrend26bc492012-03-16 14:54:25 -06001376 !ops->get_group_name ||
1377 !ops->get_group_pins)
1378 return -EINVAL;
1379
Stephen Warren57291ce2012-03-23 10:29:46 -06001380 if (ops->dt_node_to_map && !ops->dt_free_map)
1381 return -EINVAL;
1382
Stephen Warrend26bc492012-03-16 14:54:25 -06001383 return 0;
1384}
1385
Linus Walleij2744e8a2011-05-02 20:50:54 +02001386/**
1387 * pinctrl_register() - register a pin controller device
1388 * @pctldesc: descriptor for this pin controller
1389 * @dev: parent device for this pin controller
1390 * @driver_data: private pin controller data for this pin controller
1391 */
1392struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1393 struct device *dev, void *driver_data)
1394{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001395 struct pinctrl_dev *pctldev;
1396 int ret;
1397
Devendra Nagada9aecb2012-06-16 23:43:16 +05301398 if (!pctldesc)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001399 return NULL;
Devendra Nagada9aecb2012-06-16 23:43:16 +05301400 if (!pctldesc->name)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001401 return NULL;
1402
Stephen Warren02f5b982012-02-22 14:26:00 -07001403 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001404 if (pctldev == NULL) {
1405 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001406 return NULL;
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001407 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001408
1409 /* Initialize pin control device struct */
1410 pctldev->owner = pctldesc->owner;
1411 pctldev->desc = pctldesc;
1412 pctldev->driver_data = driver_data;
1413 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001414 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001415 pctldev->dev = dev;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001416
Stephen Warrend26bc492012-03-16 14:54:25 -06001417 /* check core ops for sanity */
Devendra Nagada9aecb2012-06-16 23:43:16 +05301418 if (pinctrl_check_ops(pctldev)) {
John Crispinad6e1102012-04-26 16:47:11 +02001419 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06001420 goto out_err;
1421 }
1422
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001423 /* If we're implementing pinmuxing, check the ops for sanity */
1424 if (pctldesc->pmxops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301425 if (pinmux_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001426 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001427 }
1428
1429 /* If we're implementing pinconfig, check the ops for sanity */
1430 if (pctldesc->confops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301431 if (pinconf_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001432 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001433 }
1434
Linus Walleij2744e8a2011-05-02 20:50:54 +02001435 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02001436 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001437 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1438 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001439 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001440 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1441 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001442 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001443 }
1444
Stephen Warren57b676f2012-03-02 13:05:44 -07001445 mutex_lock(&pinctrl_mutex);
1446
Stephen Warren8b9c1392012-02-19 23:45:42 -07001447 list_add_tail(&pctldev->node, &pinctrldev_list);
Stephen Warren57b676f2012-03-02 13:05:44 -07001448
Stephen Warren6e5e9592012-03-02 13:05:47 -07001449 pctldev->p = pinctrl_get_locked(pctldev->dev);
1450 if (!IS_ERR(pctldev->p)) {
1451 struct pinctrl_state *s =
1452 pinctrl_lookup_state_locked(pctldev->p,
1453 PINCTRL_STATE_DEFAULT);
John Crispinad6e1102012-04-26 16:47:11 +02001454 if (IS_ERR(s)) {
1455 dev_dbg(dev, "failed to lookup the default state\n");
1456 } else {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301457 if (pinctrl_select_state_locked(pctldev->p, s))
John Crispinad6e1102012-04-26 16:47:11 +02001458 dev_err(dev,
1459 "failed to select default state\n");
John Crispinad6e1102012-04-26 16:47:11 +02001460 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001461 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001462
1463 mutex_unlock(&pinctrl_mutex);
1464
Stephen Warren2304b472012-02-22 14:26:01 -07001465 pinctrl_init_device_debugfs(pctldev);
1466
Linus Walleij2744e8a2011-05-02 20:50:54 +02001467 return pctldev;
1468
Stephen Warren51cd24e2011-12-09 16:59:05 -07001469out_err:
1470 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001471 return NULL;
1472}
1473EXPORT_SYMBOL_GPL(pinctrl_register);
1474
1475/**
1476 * pinctrl_unregister() - unregister pinmux
1477 * @pctldev: pin controller to unregister
1478 *
1479 * Called by pinmux drivers to unregister a pinmux.
1480 */
1481void pinctrl_unregister(struct pinctrl_dev *pctldev)
1482{
1483 if (pctldev == NULL)
1484 return;
1485
Tony Lindgren02157162012-01-20 08:17:22 -08001486 pinctrl_remove_device_debugfs(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001487
1488 mutex_lock(&pinctrl_mutex);
1489
Stephen Warren6e5e9592012-03-02 13:05:47 -07001490 if (!IS_ERR(pctldev->p))
1491 pinctrl_put_locked(pctldev->p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -07001492
Linus Walleij2744e8a2011-05-02 20:50:54 +02001493 /* TODO: check that no pinmuxes are still active? */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001494 list_del(&pctldev->node);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001495 /* Destroy descriptor tree */
1496 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1497 pctldev->desc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001498 kfree(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001499
1500 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001501}
1502EXPORT_SYMBOL_GPL(pinctrl_unregister);
1503
1504static int __init pinctrl_init(void)
1505{
1506 pr_info("initialized pinctrl subsystem\n");
1507 pinctrl_init_debugfs();
1508 return 0;
1509}
1510
1511/* init early since many drivers really need to initialized pinmux early */
1512core_initcall(pinctrl_init);