blob: 71db586b2afdcdaed07039e7511a817370535b2e [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
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530348struct pinctrl_dev *find_pinctrl_and_add_gpio_range(const char *devname,
349 struct pinctrl_gpio_range *range)
350{
351 struct pinctrl_dev *pctldev = get_pinctrl_dev_from_devname(devname);
352
353 if (!pctldev)
354 return NULL;
355
356 pinctrl_add_gpio_range(pctldev, range);
357 return pctldev;
358}
359EXPORT_SYMBOL_GPL(find_pinctrl_and_add_gpio_range);
360
Linus Walleij2744e8a2011-05-02 20:50:54 +0200361/**
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530362 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
363 * @pctldev: pin controller device to remove the range from
364 * @range: the GPIO range to remove
365 */
366void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
367 struct pinctrl_gpio_range *range)
368{
369 mutex_lock(&pinctrl_mutex);
370 list_del(&range->node);
371 mutex_unlock(&pinctrl_mutex);
372}
373EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
374
375/**
Linus Walleij7afde8b2011-10-19 17:07:16 +0200376 * pinctrl_get_group_selector() - returns the group selector for a group
377 * @pctldev: the pin controller handling the group
378 * @pin_group: the pin group to look up
379 */
380int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
381 const char *pin_group)
382{
383 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530384 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200385 unsigned group_selector = 0;
386
Viresh Kumard1e90e92012-03-30 11:25:40 +0530387 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200388 const char *gname = pctlops->get_group_name(pctldev,
389 group_selector);
390 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700391 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200392 "found group selector %u for %s\n",
393 group_selector,
394 pin_group);
395 return group_selector;
396 }
397
398 group_selector++;
399 }
400
Stephen Warren51cd24e2011-12-09 16:59:05 -0700401 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200402 pin_group);
403
404 return -EINVAL;
405}
406
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100407/**
408 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
409 * @gpio: the GPIO pin number from the GPIO subsystem number space
410 *
411 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
412 * as part of their gpio_request() semantics, platforms and individual drivers
413 * shall *NOT* request GPIO pins to be muxed in.
414 */
415int pinctrl_request_gpio(unsigned gpio)
416{
417 struct pinctrl_dev *pctldev;
418 struct pinctrl_gpio_range *range;
419 int ret;
420 int pin;
421
Stephen Warren57b676f2012-03-02 13:05:44 -0700422 mutex_lock(&pinctrl_mutex);
423
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100424 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700425 if (ret) {
426 mutex_unlock(&pinctrl_mutex);
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800427 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700428 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100429
430 /* Convert to the pin controllers number space */
431 pin = gpio - range->base + range->pin_base;
432
Stephen Warren57b676f2012-03-02 13:05:44 -0700433 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
434
435 mutex_unlock(&pinctrl_mutex);
436 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100437}
438EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
439
440/**
441 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
442 * @gpio: the GPIO pin number from the GPIO subsystem number space
443 *
444 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
445 * as part of their gpio_free() semantics, platforms and individual drivers
446 * shall *NOT* request GPIO pins to be muxed out.
447 */
448void pinctrl_free_gpio(unsigned gpio)
449{
450 struct pinctrl_dev *pctldev;
451 struct pinctrl_gpio_range *range;
452 int ret;
453 int pin;
454
Stephen Warren57b676f2012-03-02 13:05:44 -0700455 mutex_lock(&pinctrl_mutex);
456
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100457 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700458 if (ret) {
459 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100460 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700461 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100462
463 /* Convert to the pin controllers number space */
464 pin = gpio - range->base + range->pin_base;
465
Stephen Warren57b676f2012-03-02 13:05:44 -0700466 pinmux_free_gpio(pctldev, pin, range);
467
468 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100469}
470EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
471
472static int pinctrl_gpio_direction(unsigned gpio, bool input)
473{
474 struct pinctrl_dev *pctldev;
475 struct pinctrl_gpio_range *range;
476 int ret;
477 int pin;
478
479 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
480 if (ret)
481 return ret;
482
483 /* Convert to the pin controllers number space */
484 pin = gpio - range->base + range->pin_base;
485
486 return pinmux_gpio_direction(pctldev, range, pin, input);
487}
488
489/**
490 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
491 * @gpio: the GPIO pin number from the GPIO subsystem number space
492 *
493 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
494 * as part of their gpio_direction_input() semantics, platforms and individual
495 * drivers shall *NOT* touch pin control GPIO calls.
496 */
497int pinctrl_gpio_direction_input(unsigned gpio)
498{
Stephen Warren57b676f2012-03-02 13:05:44 -0700499 int ret;
500 mutex_lock(&pinctrl_mutex);
501 ret = pinctrl_gpio_direction(gpio, true);
502 mutex_unlock(&pinctrl_mutex);
503 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100504}
505EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
506
507/**
508 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
509 * @gpio: the GPIO pin number from the GPIO subsystem number space
510 *
511 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
512 * as part of their gpio_direction_output() semantics, platforms and individual
513 * drivers shall *NOT* touch pin control GPIO calls.
514 */
515int pinctrl_gpio_direction_output(unsigned gpio)
516{
Stephen Warren57b676f2012-03-02 13:05:44 -0700517 int ret;
518 mutex_lock(&pinctrl_mutex);
519 ret = pinctrl_gpio_direction(gpio, false);
520 mutex_unlock(&pinctrl_mutex);
521 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100522}
523EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
524
Stephen Warren6e5e9592012-03-02 13:05:47 -0700525static struct pinctrl_state *find_state(struct pinctrl *p,
526 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100527{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700528 struct pinctrl_state *state;
529
530 list_for_each_entry(state, &p->states, node)
531 if (!strcmp(state->name, name))
532 return state;
533
534 return NULL;
535}
536
537static struct pinctrl_state *create_state(struct pinctrl *p,
538 const char *name)
539{
540 struct pinctrl_state *state;
541
542 state = kzalloc(sizeof(*state), GFP_KERNEL);
543 if (state == NULL) {
544 dev_err(p->dev,
545 "failed to alloc struct pinctrl_state\n");
546 return ERR_PTR(-ENOMEM);
547 }
548
549 state->name = name;
550 INIT_LIST_HEAD(&state->settings);
551
552 list_add_tail(&state->node, &p->states);
553
554 return state;
555}
556
557static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
558{
559 struct pinctrl_state *state;
560 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700561 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700562
563 state = find_state(p, map->name);
564 if (!state)
565 state = create_state(p, map->name);
566 if (IS_ERR(state))
567 return PTR_ERR(state);
568
Stephen Warren1e2082b2012-03-02 13:05:48 -0700569 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
570 return 0;
571
Stephen Warren6e5e9592012-03-02 13:05:47 -0700572 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
573 if (setting == NULL) {
574 dev_err(p->dev,
575 "failed to alloc struct pinctrl_setting\n");
576 return -ENOMEM;
577 }
578
Stephen Warren1e2082b2012-03-02 13:05:48 -0700579 setting->type = map->type;
580
Stephen Warren6e5e9592012-03-02 13:05:47 -0700581 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
582 if (setting->pctldev == NULL) {
Linus Walleijc05127c2012-04-10 10:00:38 +0200583 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
Stephen Warren6e5e9592012-03-02 13:05:47 -0700584 map->ctrl_dev_name);
585 kfree(setting);
Linus Walleijc05127c2012-04-10 10:00:38 +0200586 /*
587 * OK let us guess that the driver is not there yet, and
588 * let's defer obtaining this pinctrl handle to later...
589 */
590 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700591 }
592
Linus Walleij1a789582012-10-17 20:51:54 +0200593 setting->dev_name = map->dev_name;
594
Stephen Warren1e2082b2012-03-02 13:05:48 -0700595 switch (map->type) {
596 case PIN_MAP_TYPE_MUX_GROUP:
597 ret = pinmux_map_to_setting(map, setting);
598 break;
599 case PIN_MAP_TYPE_CONFIGS_PIN:
600 case PIN_MAP_TYPE_CONFIGS_GROUP:
601 ret = pinconf_map_to_setting(map, setting);
602 break;
603 default:
604 ret = -EINVAL;
605 break;
606 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700607 if (ret < 0) {
608 kfree(setting);
609 return ret;
610 }
611
612 list_add_tail(&setting->node, &state->settings);
613
614 return 0;
615}
616
617static struct pinctrl *find_pinctrl(struct device *dev)
618{
619 struct pinctrl *p;
620
Stephen Warren1e2082b2012-03-02 13:05:48 -0700621 list_for_each_entry(p, &pinctrl_list, node)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700622 if (p->dev == dev)
623 return p;
624
625 return NULL;
626}
627
628static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
629
630static struct pinctrl *create_pinctrl(struct device *dev)
631{
632 struct pinctrl *p;
633 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700634 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100635 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700636 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700637 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100638
639 /*
640 * create the state cookie holder struct pinctrl for each
641 * mapping, this is what consumers will get when requesting
642 * a pin control handle with pinctrl_get()
643 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700644 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700645 if (p == NULL) {
646 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100647 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700648 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700649 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700650 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -0600651 INIT_LIST_HEAD(&p->dt_maps);
652
653 ret = pinctrl_dt_to_map(p);
654 if (ret < 0) {
655 kfree(p);
656 return ERR_PTR(ret);
657 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700658
659 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100660
661 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700662 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -0700663 /* Map must be for this device */
664 if (strcmp(map->dev_name, devname))
665 continue;
666
Stephen Warren6e5e9592012-03-02 13:05:47 -0700667 ret = add_setting(p, map);
668 if (ret < 0) {
669 pinctrl_put_locked(p, false);
670 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100671 }
672 }
673
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100674 /* Add the pinmux to the global list */
Stephen Warren8b9c1392012-02-19 23:45:42 -0700675 list_add_tail(&p->node, &pinctrl_list);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100676
677 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700678}
Stephen Warren7ecdb162012-03-02 13:05:45 -0700679
Stephen Warren6e5e9592012-03-02 13:05:47 -0700680static struct pinctrl *pinctrl_get_locked(struct device *dev)
681{
682 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700683
Stephen Warren6e5e9592012-03-02 13:05:47 -0700684 if (WARN_ON(!dev))
685 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700686
Stephen Warren6e5e9592012-03-02 13:05:47 -0700687 p = find_pinctrl(dev);
688 if (p != NULL)
689 return ERR_PTR(-EBUSY);
690
Richard Genoudd599bfb2012-08-10 16:53:49 +0200691 return create_pinctrl(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100692}
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700693
694/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700695 * pinctrl_get() - retrieves the pinctrl handle for a device
696 * @dev: the device to obtain the handle for
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700697 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700698struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700699{
700 struct pinctrl *p;
701
Stephen Warren57b676f2012-03-02 13:05:44 -0700702 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700703 p = pinctrl_get_locked(dev);
Stephen Warren57b676f2012-03-02 13:05:44 -0700704 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700705
706 return p;
707}
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100708EXPORT_SYMBOL_GPL(pinctrl_get);
709
Stephen Warren6e5e9592012-03-02 13:05:47 -0700710static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -0700711{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700712 struct pinctrl_state *state, *n1;
713 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700714
Stephen Warren6e5e9592012-03-02 13:05:47 -0700715 list_for_each_entry_safe(state, n1, &p->states, node) {
716 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700717 switch (setting->type) {
718 case PIN_MAP_TYPE_MUX_GROUP:
719 if (state == p->state)
720 pinmux_disable_setting(setting);
721 pinmux_free_setting(setting);
722 break;
723 case PIN_MAP_TYPE_CONFIGS_PIN:
724 case PIN_MAP_TYPE_CONFIGS_GROUP:
725 pinconf_free_setting(setting);
726 break;
727 default:
728 break;
729 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700730 list_del(&setting->node);
731 kfree(setting);
732 }
733 list_del(&state->node);
734 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700735 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700736
Stephen Warren57291ce2012-03-23 10:29:46 -0600737 pinctrl_dt_free_maps(p);
738
Stephen Warren6e5e9592012-03-02 13:05:47 -0700739 if (inlist)
740 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700741 kfree(p);
742}
743
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100744/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700745 * pinctrl_put() - release a previously claimed pinctrl handle
746 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100747 */
748void pinctrl_put(struct pinctrl *p)
749{
Stephen Warren57b676f2012-03-02 13:05:44 -0700750 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700751 pinctrl_put_locked(p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -0700752 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100753}
754EXPORT_SYMBOL_GPL(pinctrl_put);
755
Stephen Warren6e5e9592012-03-02 13:05:47 -0700756static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
757 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -0700758{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700759 struct pinctrl_state *state;
760
761 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800762 if (!state) {
763 if (pinctrl_dummy_state) {
764 /* create dummy state */
765 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
766 name);
767 state = create_state(p, name);
Richard Genoudd599bfb2012-08-10 16:53:49 +0200768 } else
769 state = ERR_PTR(-ENODEV);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800770 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700771
772 return state;
773}
774
775/**
776 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
777 * @p: the pinctrl handle to retrieve the state from
778 * @name: the state name to retrieve
779 */
780struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
781{
782 struct pinctrl_state *s;
783
784 mutex_lock(&pinctrl_mutex);
785 s = pinctrl_lookup_state_locked(p, name);
786 mutex_unlock(&pinctrl_mutex);
787
788 return s;
789}
790EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
791
792static int pinctrl_select_state_locked(struct pinctrl *p,
793 struct pinctrl_state *state)
794{
795 struct pinctrl_setting *setting, *setting2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700796 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700797
Stephen Warren6e5e9592012-03-02 13:05:47 -0700798 if (p->state == state)
799 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700800
Stephen Warren6e5e9592012-03-02 13:05:47 -0700801 if (p->state) {
802 /*
803 * The set of groups with a mux configuration in the old state
804 * may not be identical to the set of groups with a mux setting
805 * in the new state. While this might be unusual, it's entirely
806 * possible for the "user"-supplied mapping table to be written
807 * that way. For each group that was configured in the old state
808 * but not in the new state, this code puts that group into a
809 * safe/disabled state.
810 */
811 list_for_each_entry(setting, &p->state->settings, node) {
812 bool found = false;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700813 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
814 continue;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700815 list_for_each_entry(setting2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700816 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
817 continue;
818 if (setting2->data.mux.group ==
819 setting->data.mux.group) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700820 found = true;
821 break;
822 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700823 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700824 if (!found)
825 pinmux_disable_setting(setting);
826 }
827 }
828
829 p->state = state;
830
831 /* Apply all the settings for the new state */
832 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700833 switch (setting->type) {
834 case PIN_MAP_TYPE_MUX_GROUP:
835 ret = pinmux_enable_setting(setting);
836 break;
837 case PIN_MAP_TYPE_CONFIGS_PIN:
838 case PIN_MAP_TYPE_CONFIGS_GROUP:
839 ret = pinconf_apply_setting(setting);
840 break;
841 default:
842 ret = -EINVAL;
843 break;
844 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700845 if (ret < 0) {
846 /* FIXME: Difficult to return to prev state */
847 return ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700848 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700849 }
850
Stephen Warren7ecdb162012-03-02 13:05:45 -0700851 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700852}
853
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100854/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700855 * pinctrl_select() - select/activate/program a pinctrl state to HW
856 * @p: the pinctrl handle for the device that requests configuratio
857 * @state: the state handle to select/activate/program
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100858 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700859int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100860{
Stephen Warren57b676f2012-03-02 13:05:44 -0700861 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700862
Stephen Warren57b676f2012-03-02 13:05:44 -0700863 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700864 ret = pinctrl_select_state_locked(p, state);
Stephen Warren57b676f2012-03-02 13:05:44 -0700865 mutex_unlock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700866
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100867 return ret;
868}
Stephen Warren6e5e9592012-03-02 13:05:47 -0700869EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100870
Stephen Warren6d4ca1f2012-04-16 10:51:00 -0600871static void devm_pinctrl_release(struct device *dev, void *res)
872{
873 pinctrl_put(*(struct pinctrl **)res);
874}
875
876/**
877 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
878 * @dev: the device to obtain the handle for
879 *
880 * If there is a need to explicitly destroy the returned struct pinctrl,
881 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
882 */
883struct pinctrl *devm_pinctrl_get(struct device *dev)
884{
885 struct pinctrl **ptr, *p;
886
887 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
888 if (!ptr)
889 return ERR_PTR(-ENOMEM);
890
891 p = pinctrl_get(dev);
892 if (!IS_ERR(p)) {
893 *ptr = p;
894 devres_add(dev, ptr);
895 } else {
896 devres_free(ptr);
897 }
898
899 return p;
900}
901EXPORT_SYMBOL_GPL(devm_pinctrl_get);
902
903static int devm_pinctrl_match(struct device *dev, void *res, void *data)
904{
905 struct pinctrl **p = res;
906
907 return *p == data;
908}
909
910/**
911 * devm_pinctrl_put() - Resource managed pinctrl_put()
912 * @p: the pinctrl handle to release
913 *
914 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
915 * this function will not need to be called and the resource management
916 * code will ensure that the resource is freed.
917 */
918void devm_pinctrl_put(struct pinctrl *p)
919{
920 WARN_ON(devres_destroy(p->dev, devm_pinctrl_release,
921 devm_pinctrl_match, p));
922 pinctrl_put(p);
923}
924EXPORT_SYMBOL_GPL(devm_pinctrl_put);
925
Stephen Warren57291ce2012-03-23 10:29:46 -0600926int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
927 bool dup, bool locked)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100928{
Stephen Warren1e2082b2012-03-02 13:05:48 -0700929 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700930 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100931
932 pr_debug("add %d pinmux maps\n", num_maps);
933
934 /* First sanity check the new mapping */
935 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700936 if (!maps[i].dev_name) {
937 pr_err("failed to register map %s (%d): no device given\n",
938 maps[i].name, i);
939 return -EINVAL;
940 }
941
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100942 if (!maps[i].name) {
943 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700944 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100945 return -EINVAL;
946 }
947
Stephen Warren1e2082b2012-03-02 13:05:48 -0700948 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
949 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100950 pr_err("failed to register map %s (%d): no pin control device given\n",
951 maps[i].name, i);
952 return -EINVAL;
953 }
954
Stephen Warren1e2082b2012-03-02 13:05:48 -0700955 switch (maps[i].type) {
956 case PIN_MAP_TYPE_DUMMY_STATE:
957 break;
958 case PIN_MAP_TYPE_MUX_GROUP:
959 ret = pinmux_validate_map(&maps[i], i);
960 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -0600961 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700962 break;
963 case PIN_MAP_TYPE_CONFIGS_PIN:
964 case PIN_MAP_TYPE_CONFIGS_GROUP:
965 ret = pinconf_validate_map(&maps[i], i);
966 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -0600967 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700968 break;
969 default:
970 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700971 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -0700972 return -EINVAL;
973 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100974 }
975
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700976 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
977 if (!maps_node) {
978 pr_err("failed to alloc struct pinctrl_maps\n");
979 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100980 }
981
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700982 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -0600983 if (dup) {
984 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
985 GFP_KERNEL);
986 if (!maps_node->maps) {
987 pr_err("failed to duplicate mapping table\n");
988 kfree(maps_node);
989 return -ENOMEM;
990 }
991 } else {
992 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700993 }
994
Stephen Warren57291ce2012-03-23 10:29:46 -0600995 if (!locked)
996 mutex_lock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700997 list_add_tail(&maps_node->node, &pinctrl_maps);
Stephen Warren57291ce2012-03-23 10:29:46 -0600998 if (!locked)
999 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001000
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001001 return 0;
1002}
1003
Stephen Warren57291ce2012-03-23 10:29:46 -06001004/**
1005 * pinctrl_register_mappings() - register a set of pin controller mappings
1006 * @maps: the pincontrol mappings table to register. This should probably be
1007 * marked with __initdata so it can be discarded after boot. This
1008 * function will perform a shallow copy for the mapping entries.
1009 * @num_maps: the number of maps in the mapping table
1010 */
1011int pinctrl_register_mappings(struct pinctrl_map const *maps,
1012 unsigned num_maps)
1013{
1014 return pinctrl_register_map(maps, num_maps, true, false);
1015}
1016
1017void pinctrl_unregister_map(struct pinctrl_map const *map)
1018{
1019 struct pinctrl_maps *maps_node;
1020
1021 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1022 if (maps_node->maps == map) {
1023 list_del(&maps_node->node);
1024 return;
1025 }
1026 }
1027}
1028
Linus Walleij2744e8a2011-05-02 20:50:54 +02001029#ifdef CONFIG_DEBUG_FS
1030
1031static int pinctrl_pins_show(struct seq_file *s, void *what)
1032{
1033 struct pinctrl_dev *pctldev = s->private;
1034 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001035 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001036
1037 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001038
Stephen Warren57b676f2012-03-02 13:05:44 -07001039 mutex_lock(&pinctrl_mutex);
1040
Chanho Park706e8522012-01-03 16:47:50 +09001041 /* The pin number can be retrived from the pin controller descriptor */
1042 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001043 struct pin_desc *desc;
1044
Chanho Park706e8522012-01-03 16:47:50 +09001045 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001046 desc = pin_desc_get(pctldev, pin);
1047 /* Pin space may be sparse */
1048 if (desc == NULL)
1049 continue;
1050
1051 seq_printf(s, "pin %d (%s) ", pin,
1052 desc->name ? desc->name : "unnamed");
1053
1054 /* Driver-specific info per pin */
1055 if (ops->pin_dbg_show)
1056 ops->pin_dbg_show(pctldev, s, pin);
1057
1058 seq_puts(s, "\n");
1059 }
1060
Stephen Warren57b676f2012-03-02 13:05:44 -07001061 mutex_unlock(&pinctrl_mutex);
1062
Linus Walleij2744e8a2011-05-02 20:50:54 +02001063 return 0;
1064}
1065
1066static int pinctrl_groups_show(struct seq_file *s, void *what)
1067{
1068 struct pinctrl_dev *pctldev = s->private;
1069 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301070 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001071
Viresh Kumard1e90e92012-03-30 11:25:40 +05301072 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001073 mutex_lock(&pinctrl_mutex);
1074
Linus Walleij2744e8a2011-05-02 20:50:54 +02001075 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301076 while (selector < ngroups) {
Stephen Warrena5818a82011-10-19 16:19:25 -06001077 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001078 unsigned num_pins;
1079 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001080 const char *pname;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001081 int ret;
1082 int i;
1083
1084 ret = ops->get_group_pins(pctldev, selector,
1085 &pins, &num_pins);
1086 if (ret)
1087 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1088 gname);
1089 else {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001090 seq_printf(s, "group: %s\n", gname);
1091 for (i = 0; i < num_pins; i++) {
1092 pname = pin_get_name(pctldev, pins[i]);
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001093 if (WARN_ON(!pname)) {
1094 mutex_unlock(&pinctrl_mutex);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001095 return -EINVAL;
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001096 }
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001097 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1098 }
1099 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001100 }
1101 selector++;
1102 }
1103
Stephen Warren57b676f2012-03-02 13:05:44 -07001104 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001105
1106 return 0;
1107}
1108
1109static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1110{
1111 struct pinctrl_dev *pctldev = s->private;
1112 struct pinctrl_gpio_range *range = NULL;
1113
1114 seq_puts(s, "GPIO ranges handled:\n");
1115
Stephen Warren57b676f2012-03-02 13:05:44 -07001116 mutex_lock(&pinctrl_mutex);
1117
Linus Walleij2744e8a2011-05-02 20:50:54 +02001118 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001119 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Linus Walleij75d66422011-11-16 09:58:51 +01001120 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1121 range->id, range->name,
1122 range->base, (range->base + range->npins - 1),
1123 range->pin_base,
1124 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001125 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001126
1127 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001128
1129 return 0;
1130}
1131
1132static int pinctrl_devices_show(struct seq_file *s, void *what)
1133{
1134 struct pinctrl_dev *pctldev;
1135
Linus Walleijae6b4d82011-10-19 18:14:33 +02001136 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001137
1138 mutex_lock(&pinctrl_mutex);
1139
Linus Walleij2744e8a2011-05-02 20:50:54 +02001140 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1141 seq_printf(s, "%s ", pctldev->desc->name);
1142 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001143 seq_puts(s, "yes ");
1144 else
1145 seq_puts(s, "no ");
1146 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001147 seq_puts(s, "yes");
1148 else
1149 seq_puts(s, "no");
1150 seq_puts(s, "\n");
1151 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001152
1153 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001154
1155 return 0;
1156}
1157
Stephen Warren1e2082b2012-03-02 13:05:48 -07001158static inline const char *map_type(enum pinctrl_map_type type)
1159{
1160 static const char * const names[] = {
1161 "INVALID",
1162 "DUMMY_STATE",
1163 "MUX_GROUP",
1164 "CONFIGS_PIN",
1165 "CONFIGS_GROUP",
1166 };
1167
1168 if (type >= ARRAY_SIZE(names))
1169 return "UNKNOWN";
1170
1171 return names[type];
1172}
1173
Stephen Warren3eedb432012-02-23 17:04:40 -07001174static int pinctrl_maps_show(struct seq_file *s, void *what)
1175{
1176 struct pinctrl_maps *maps_node;
1177 int i;
1178 struct pinctrl_map const *map;
1179
1180 seq_puts(s, "Pinctrl maps:\n");
1181
Stephen Warren57b676f2012-03-02 13:05:44 -07001182 mutex_lock(&pinctrl_mutex);
1183
Stephen Warren3eedb432012-02-23 17:04:40 -07001184 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001185 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1186 map->dev_name, map->name, map_type(map->type),
1187 map->type);
1188
1189 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1190 seq_printf(s, "controlling device %s\n",
1191 map->ctrl_dev_name);
1192
1193 switch (map->type) {
1194 case PIN_MAP_TYPE_MUX_GROUP:
1195 pinmux_show_map(s, map);
1196 break;
1197 case PIN_MAP_TYPE_CONFIGS_PIN:
1198 case PIN_MAP_TYPE_CONFIGS_GROUP:
1199 pinconf_show_map(s, map);
1200 break;
1201 default:
1202 break;
1203 }
1204
1205 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001206 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001207
1208 mutex_unlock(&pinctrl_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001209
1210 return 0;
1211}
1212
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001213static int pinctrl_show(struct seq_file *s, void *what)
1214{
1215 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001216 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001217 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001218
1219 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001220
1221 mutex_lock(&pinctrl_mutex);
1222
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001223 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001224 seq_printf(s, "device: %s current state: %s\n",
1225 dev_name(p->dev),
1226 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001227
Stephen Warren6e5e9592012-03-02 13:05:47 -07001228 list_for_each_entry(state, &p->states, node) {
1229 seq_printf(s, " state: %s\n", state->name);
1230
1231 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001232 struct pinctrl_dev *pctldev = setting->pctldev;
1233
1234 seq_printf(s, " type: %s controller %s ",
1235 map_type(setting->type),
1236 pinctrl_dev_get_name(pctldev));
1237
1238 switch (setting->type) {
1239 case PIN_MAP_TYPE_MUX_GROUP:
1240 pinmux_show_setting(s, setting);
1241 break;
1242 case PIN_MAP_TYPE_CONFIGS_PIN:
1243 case PIN_MAP_TYPE_CONFIGS_GROUP:
1244 pinconf_show_setting(s, setting);
1245 break;
1246 default:
1247 break;
1248 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001249 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001250 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001251 }
1252
Stephen Warren57b676f2012-03-02 13:05:44 -07001253 mutex_unlock(&pinctrl_mutex);
1254
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001255 return 0;
1256}
1257
Linus Walleij2744e8a2011-05-02 20:50:54 +02001258static int pinctrl_pins_open(struct inode *inode, struct file *file)
1259{
1260 return single_open(file, pinctrl_pins_show, inode->i_private);
1261}
1262
1263static int pinctrl_groups_open(struct inode *inode, struct file *file)
1264{
1265 return single_open(file, pinctrl_groups_show, inode->i_private);
1266}
1267
1268static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1269{
1270 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1271}
1272
1273static int pinctrl_devices_open(struct inode *inode, struct file *file)
1274{
1275 return single_open(file, pinctrl_devices_show, NULL);
1276}
1277
Stephen Warren3eedb432012-02-23 17:04:40 -07001278static int pinctrl_maps_open(struct inode *inode, struct file *file)
1279{
1280 return single_open(file, pinctrl_maps_show, NULL);
1281}
1282
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001283static int pinctrl_open(struct inode *inode, struct file *file)
1284{
1285 return single_open(file, pinctrl_show, NULL);
1286}
1287
Linus Walleij2744e8a2011-05-02 20:50:54 +02001288static const struct file_operations pinctrl_pins_ops = {
1289 .open = pinctrl_pins_open,
1290 .read = seq_read,
1291 .llseek = seq_lseek,
1292 .release = single_release,
1293};
1294
1295static const struct file_operations pinctrl_groups_ops = {
1296 .open = pinctrl_groups_open,
1297 .read = seq_read,
1298 .llseek = seq_lseek,
1299 .release = single_release,
1300};
1301
1302static const struct file_operations pinctrl_gpioranges_ops = {
1303 .open = pinctrl_gpioranges_open,
1304 .read = seq_read,
1305 .llseek = seq_lseek,
1306 .release = single_release,
1307};
1308
1309static const struct file_operations pinctrl_devices_ops = {
1310 .open = pinctrl_devices_open,
1311 .read = seq_read,
1312 .llseek = seq_lseek,
1313 .release = single_release,
1314};
1315
Stephen Warren3eedb432012-02-23 17:04:40 -07001316static const struct file_operations pinctrl_maps_ops = {
1317 .open = pinctrl_maps_open,
1318 .read = seq_read,
1319 .llseek = seq_lseek,
1320 .release = single_release,
1321};
1322
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001323static const struct file_operations pinctrl_ops = {
1324 .open = pinctrl_open,
1325 .read = seq_read,
1326 .llseek = seq_lseek,
1327 .release = single_release,
1328};
1329
Linus Walleij2744e8a2011-05-02 20:50:54 +02001330static struct dentry *debugfs_root;
1331
1332static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1333{
Tony Lindgren02157162012-01-20 08:17:22 -08001334 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001335
Stephen Warren51cd24e2011-12-09 16:59:05 -07001336 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001337 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001338 pctldev->device_root = device_root;
1339
Linus Walleij2744e8a2011-05-02 20:50:54 +02001340 if (IS_ERR(device_root) || !device_root) {
1341 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001342 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001343 return;
1344 }
1345 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1346 device_root, pctldev, &pinctrl_pins_ops);
1347 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1348 device_root, pctldev, &pinctrl_groups_ops);
1349 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1350 device_root, pctldev, &pinctrl_gpioranges_ops);
1351 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +02001352 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001353}
1354
Tony Lindgren02157162012-01-20 08:17:22 -08001355static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1356{
1357 debugfs_remove_recursive(pctldev->device_root);
1358}
1359
Linus Walleij2744e8a2011-05-02 20:50:54 +02001360static void pinctrl_init_debugfs(void)
1361{
1362 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1363 if (IS_ERR(debugfs_root) || !debugfs_root) {
1364 pr_warn("failed to create debugfs directory\n");
1365 debugfs_root = NULL;
1366 return;
1367 }
1368
1369 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1370 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001371 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1372 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001373 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1374 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001375}
1376
1377#else /* CONFIG_DEBUG_FS */
1378
1379static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1380{
1381}
1382
1383static void pinctrl_init_debugfs(void)
1384{
1385}
1386
Tony Lindgren02157162012-01-20 08:17:22 -08001387static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1388{
1389}
1390
Linus Walleij2744e8a2011-05-02 20:50:54 +02001391#endif
1392
Stephen Warrend26bc492012-03-16 14:54:25 -06001393static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1394{
1395 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1396
1397 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301398 !ops->get_groups_count ||
Stephen Warrend26bc492012-03-16 14:54:25 -06001399 !ops->get_group_name ||
1400 !ops->get_group_pins)
1401 return -EINVAL;
1402
Stephen Warren57291ce2012-03-23 10:29:46 -06001403 if (ops->dt_node_to_map && !ops->dt_free_map)
1404 return -EINVAL;
1405
Stephen Warrend26bc492012-03-16 14:54:25 -06001406 return 0;
1407}
1408
Linus Walleij2744e8a2011-05-02 20:50:54 +02001409/**
1410 * pinctrl_register() - register a pin controller device
1411 * @pctldesc: descriptor for this pin controller
1412 * @dev: parent device for this pin controller
1413 * @driver_data: private pin controller data for this pin controller
1414 */
1415struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1416 struct device *dev, void *driver_data)
1417{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001418 struct pinctrl_dev *pctldev;
1419 int ret;
1420
Devendra Nagada9aecb2012-06-16 23:43:16 +05301421 if (!pctldesc)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001422 return NULL;
Devendra Nagada9aecb2012-06-16 23:43:16 +05301423 if (!pctldesc->name)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001424 return NULL;
1425
Stephen Warren02f5b982012-02-22 14:26:00 -07001426 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001427 if (pctldev == NULL) {
1428 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001429 return NULL;
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001430 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001431
1432 /* Initialize pin control device struct */
1433 pctldev->owner = pctldesc->owner;
1434 pctldev->desc = pctldesc;
1435 pctldev->driver_data = driver_data;
1436 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001437 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001438 pctldev->dev = dev;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001439
Stephen Warrend26bc492012-03-16 14:54:25 -06001440 /* check core ops for sanity */
Devendra Nagada9aecb2012-06-16 23:43:16 +05301441 if (pinctrl_check_ops(pctldev)) {
John Crispinad6e1102012-04-26 16:47:11 +02001442 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06001443 goto out_err;
1444 }
1445
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001446 /* If we're implementing pinmuxing, check the ops for sanity */
1447 if (pctldesc->pmxops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301448 if (pinmux_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001449 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001450 }
1451
1452 /* If we're implementing pinconfig, check the ops for sanity */
1453 if (pctldesc->confops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301454 if (pinconf_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001455 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001456 }
1457
Linus Walleij2744e8a2011-05-02 20:50:54 +02001458 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02001459 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001460 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1461 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001462 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001463 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1464 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001465 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001466 }
1467
Stephen Warren57b676f2012-03-02 13:05:44 -07001468 mutex_lock(&pinctrl_mutex);
1469
Stephen Warren8b9c1392012-02-19 23:45:42 -07001470 list_add_tail(&pctldev->node, &pinctrldev_list);
Stephen Warren57b676f2012-03-02 13:05:44 -07001471
Stephen Warren6e5e9592012-03-02 13:05:47 -07001472 pctldev->p = pinctrl_get_locked(pctldev->dev);
1473 if (!IS_ERR(pctldev->p)) {
1474 struct pinctrl_state *s =
1475 pinctrl_lookup_state_locked(pctldev->p,
1476 PINCTRL_STATE_DEFAULT);
John Crispinad6e1102012-04-26 16:47:11 +02001477 if (IS_ERR(s)) {
1478 dev_dbg(dev, "failed to lookup the default state\n");
1479 } else {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301480 if (pinctrl_select_state_locked(pctldev->p, s))
John Crispinad6e1102012-04-26 16:47:11 +02001481 dev_err(dev,
1482 "failed to select default state\n");
John Crispinad6e1102012-04-26 16:47:11 +02001483 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001484 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001485
1486 mutex_unlock(&pinctrl_mutex);
1487
Stephen Warren2304b472012-02-22 14:26:01 -07001488 pinctrl_init_device_debugfs(pctldev);
1489
Linus Walleij2744e8a2011-05-02 20:50:54 +02001490 return pctldev;
1491
Stephen Warren51cd24e2011-12-09 16:59:05 -07001492out_err:
1493 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001494 return NULL;
1495}
1496EXPORT_SYMBOL_GPL(pinctrl_register);
1497
1498/**
1499 * pinctrl_unregister() - unregister pinmux
1500 * @pctldev: pin controller to unregister
1501 *
1502 * Called by pinmux drivers to unregister a pinmux.
1503 */
1504void pinctrl_unregister(struct pinctrl_dev *pctldev)
1505{
Dong Aisheng5d589b02012-05-23 21:22:40 +08001506 struct pinctrl_gpio_range *range, *n;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001507 if (pctldev == NULL)
1508 return;
1509
Tony Lindgren02157162012-01-20 08:17:22 -08001510 pinctrl_remove_device_debugfs(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001511
1512 mutex_lock(&pinctrl_mutex);
1513
Stephen Warren6e5e9592012-03-02 13:05:47 -07001514 if (!IS_ERR(pctldev->p))
1515 pinctrl_put_locked(pctldev->p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -07001516
Linus Walleij2744e8a2011-05-02 20:50:54 +02001517 /* TODO: check that no pinmuxes are still active? */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001518 list_del(&pctldev->node);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001519 /* Destroy descriptor tree */
1520 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1521 pctldev->desc->npins);
Dong Aisheng5d589b02012-05-23 21:22:40 +08001522 /* remove gpio ranges map */
1523 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1524 list_del(&range->node);
1525
Stephen Warren51cd24e2011-12-09 16:59:05 -07001526 kfree(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001527
1528 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001529}
1530EXPORT_SYMBOL_GPL(pinctrl_unregister);
1531
1532static int __init pinctrl_init(void)
1533{
1534 pr_info("initialized pinctrl subsystem\n");
1535 pinctrl_init_debugfs();
1536 return 0;
1537}
1538
1539/* init early since many drivers really need to initialized pinmux early */
1540core_initcall(pinctrl_init);