blob: a97b717e3ad31eedb41a73196122b8af6ab3eee7 [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>
Linus Walleijab780292013-01-22 10:56:14 -070017#include <linux/kref.h>
Stephen Rothwella5a697c2011-09-30 14:39:04 +100018#include <linux/export.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020019#include <linux/init.h>
20#include <linux/device.h>
21#include <linux/slab.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020022#include <linux/err.h>
23#include <linux/list.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020024#include <linux/sysfs.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
Stephen Warren6d4ca1f2012-04-16 10:51:00 -060027#include <linux/pinctrl/consumer.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020028#include <linux/pinctrl/pinctrl.h>
29#include <linux/pinctrl/machine.h>
Haojian Zhuang2afe8222013-03-28 07:34:19 +080030
31#ifdef CONFIG_GPIOLIB
Haojian Zhuang51e13c22013-02-17 19:42:50 +080032#include <asm-generic/gpio.h>
Haojian Zhuang2afe8222013-03-28 07:34:19 +080033#endif
34
Linus Walleij2744e8a2011-05-02 20:50:54 +020035#include "core.h"
Stephen Warren57291ce2012-03-23 10:29:46 -060036#include "devicetree.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020037#include "pinmux.h"
Linus Walleijae6b4d82011-10-19 18:14:33 +020038#include "pinconf.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020039
Stephen Warrenb2b3e662012-02-19 23:45:43 -070040
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080041static bool pinctrl_dummy_state;
42
Patrice Chotard42fed7b2013-04-11 11:01:27 +020043/* Mutex taken to protect pinctrl_list */
Sachin Kamat843aec92013-06-18 14:34:27 +053044static DEFINE_MUTEX(pinctrl_list_mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +020045
46/* Mutex taken to protect pinctrl_maps */
47DEFINE_MUTEX(pinctrl_maps_mutex);
48
49/* Mutex taken to protect pinctrldev_list */
Sachin Kamat843aec92013-06-18 14:34:27 +053050static DEFINE_MUTEX(pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -070051
52/* Global list of pin control devices (struct pinctrl_dev) */
Patrice Chotard42fed7b2013-04-11 11:01:27 +020053static LIST_HEAD(pinctrldev_list);
Linus Walleij2744e8a2011-05-02 20:50:54 +020054
Stephen Warren57b676f2012-03-02 13:05:44 -070055/* List of pin controller handles (struct pinctrl) */
Linus Walleijbefe5bd2012-02-09 19:47:48 +010056static LIST_HEAD(pinctrl_list);
57
Stephen Warren57b676f2012-03-02 13:05:44 -070058/* List of pinctrl maps (struct pinctrl_maps) */
Laurent Meunier6f9e41f2013-02-06 09:09:44 +010059LIST_HEAD(pinctrl_maps);
Stephen Warrenb2b3e662012-02-19 23:45:43 -070060
Linus Walleijbefe5bd2012-02-09 19:47:48 +010061
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080062/**
63 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
64 *
65 * Usually this function is called by platforms without pinctrl driver support
66 * but run with some shared drivers using pinctrl APIs.
67 * After calling this function, the pinctrl core will return successfully
68 * with creating a dummy state for the driver to keep going smoothly.
69 */
70void pinctrl_provide_dummies(void)
71{
72 pinctrl_dummy_state = true;
73}
74
Linus Walleij2744e8a2011-05-02 20:50:54 +020075const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
76{
77 /* We're not allowed to register devices without name */
78 return pctldev->desc->name;
79}
80EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
81
Haojian Zhuangd6e99ab2013-01-18 15:31:06 +080082const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
83{
84 return dev_name(pctldev->dev);
85}
86EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
87
Linus Walleij2744e8a2011-05-02 20:50:54 +020088void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
89{
90 return pctldev->driver_data;
91}
92EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
93
94/**
Linus Walleij9dfac4f2012-02-01 18:02:47 +010095 * get_pinctrl_dev_from_devname() - look up pin controller device
96 * @devname: the name of a device instance, as returned by dev_name()
Linus Walleij2744e8a2011-05-02 20:50:54 +020097 *
98 * Looks up a pin control device matching a certain device name or pure device
99 * pointer, the pure device pointer will take precedence.
100 */
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100101struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200102{
103 struct pinctrl_dev *pctldev = NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200104
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100105 if (!devname)
106 return NULL;
107
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200108 mutex_lock(&pinctrldev_list_mutex);
109
Linus Walleij2744e8a2011-05-02 20:50:54 +0200110 list_for_each_entry(pctldev, &pinctrldev_list, node) {
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100111 if (!strcmp(dev_name(pctldev->dev), devname)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200112 /* Matched on device name */
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200113 mutex_unlock(&pinctrldev_list_mutex);
114 return pctldev;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200115 }
116 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200117
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200118 mutex_unlock(&pinctrldev_list_mutex);
119
120 return NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200121}
122
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200123struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np)
124{
125 struct pinctrl_dev *pctldev;
126
127 mutex_lock(&pinctrldev_list_mutex);
128
129 list_for_each_entry(pctldev, &pinctrldev_list, node)
130 if (pctldev->dev->of_node == np) {
131 mutex_unlock(&pinctrldev_list_mutex);
132 return pctldev;
133 }
134
Daniel Mackd463f822013-04-26 18:57:02 +0200135 mutex_unlock(&pinctrldev_list_mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200136
137 return NULL;
138}
139
Linus Walleij2744e8a2011-05-02 20:50:54 +0200140/**
Linus Walleijae6b4d82011-10-19 18:14:33 +0200141 * pin_get_from_name() - look up a pin number from a name
142 * @pctldev: the pin control device to lookup the pin on
143 * @name: the name of the pin to look up
144 */
145int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
146{
Chanho Park706e8522012-01-03 16:47:50 +0900147 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200148
Chanho Park706e8522012-01-03 16:47:50 +0900149 /* The pin number can be retrived from the pin controller descriptor */
150 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200151 struct pin_desc *desc;
152
Chanho Park706e8522012-01-03 16:47:50 +0900153 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200154 desc = pin_desc_get(pctldev, pin);
155 /* Pin space may be sparse */
156 if (desc == NULL)
157 continue;
158 if (desc->name && !strcmp(name, desc->name))
159 return pin;
160 }
161
162 return -EINVAL;
163}
164
165/**
Dong Aishengdcb5dbc2012-04-17 15:00:46 +0800166 * pin_get_name_from_id() - look up a pin name from a pin id
167 * @pctldev: the pin control device to lookup the pin on
168 * @name: the name of the pin to look up
169 */
170const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
171{
172 const struct pin_desc *desc;
173
174 desc = pin_desc_get(pctldev, pin);
175 if (desc == NULL) {
176 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
177 pin);
178 return NULL;
179 }
180
181 return desc->name;
182}
183
184/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200185 * pin_is_valid() - check if pin exists on controller
186 * @pctldev: the pin control device to check the pin on
187 * @pin: pin to check, use the local pin controller index number
188 *
189 * This tells us whether a certain pin exist on a certain pin controller or
190 * not. Pin lists may be sparse, so some pins may not exist.
191 */
192bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
193{
194 struct pin_desc *pindesc;
195
196 if (pin < 0)
197 return false;
198
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200199 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200200 pindesc = pin_desc_get(pctldev, pin);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200201 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200202
Stephen Warren57b676f2012-03-02 13:05:44 -0700203 return pindesc != NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200204}
205EXPORT_SYMBOL_GPL(pin_is_valid);
206
207/* Deletes a range of pin descriptors */
208static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
209 const struct pinctrl_pin_desc *pins,
210 unsigned num_pins)
211{
212 int i;
213
Linus Walleij2744e8a2011-05-02 20:50:54 +0200214 for (i = 0; i < num_pins; i++) {
215 struct pin_desc *pindesc;
216
217 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
218 pins[i].number);
219 if (pindesc != NULL) {
220 radix_tree_delete(&pctldev->pin_desc_tree,
221 pins[i].number);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100222 if (pindesc->dynamic_name)
223 kfree(pindesc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200224 }
225 kfree(pindesc);
226 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200227}
228
229static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
230 unsigned number, const char *name)
231{
232 struct pin_desc *pindesc;
233
234 pindesc = pin_desc_get(pctldev, number);
235 if (pindesc != NULL) {
236 pr_err("pin %d already registered on %s\n", number,
237 pctldev->desc->name);
238 return -EINVAL;
239 }
240
241 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700242 if (pindesc == NULL) {
243 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200244 return -ENOMEM;
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700245 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200246
Linus Walleij2744e8a2011-05-02 20:50:54 +0200247 /* Set owner */
248 pindesc->pctldev = pctldev;
249
Stephen Warren9af1e442011-10-19 16:19:27 -0600250 /* Copy basic pin info */
Linus Walleij8dc6ae42012-02-01 18:11:40 +0100251 if (name) {
Linus Walleijca53c5f2011-12-14 20:33:37 +0100252 pindesc->name = name;
253 } else {
254 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
Sachin Kamateb26cc92012-09-25 12:41:40 +0530255 if (pindesc->name == NULL) {
256 kfree(pindesc);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100257 return -ENOMEM;
Sachin Kamateb26cc92012-09-25 12:41:40 +0530258 }
Linus Walleijca53c5f2011-12-14 20:33:37 +0100259 pindesc->dynamic_name = true;
260 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200261
Linus Walleij2744e8a2011-05-02 20:50:54 +0200262 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200263 pr_debug("registered pin %d (%s) on %s\n",
Linus Walleijca53c5f2011-12-14 20:33:37 +0100264 number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200265 return 0;
266}
267
268static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
269 struct pinctrl_pin_desc const *pins,
270 unsigned num_descs)
271{
272 unsigned i;
273 int ret = 0;
274
275 for (i = 0; i < num_descs; i++) {
276 ret = pinctrl_register_one_pin(pctldev,
277 pins[i].number, pins[i].name);
278 if (ret)
279 return ret;
280 }
281
282 return 0;
283}
284
285/**
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200286 * gpio_to_pin() - GPIO range GPIO number to pin number translation
287 * @range: GPIO range used for the translation
288 * @gpio: gpio pin to translate to a pin number
289 *
290 * Finds the pin number for a given GPIO using the specified GPIO range
291 * as a base for translation. The distinction between linear GPIO ranges
292 * and pin list based GPIO ranges is managed correctly by this function.
293 *
294 * This function assumes the gpio is part of the specified GPIO range, use
295 * only after making sure this is the case (e.g. by calling it on the
296 * result of successful pinctrl_get_device_gpio_range calls)!
297 */
298static inline int gpio_to_pin(struct pinctrl_gpio_range *range,
299 unsigned int gpio)
300{
301 unsigned int offset = gpio - range->base;
302 if (range->pins)
303 return range->pins[offset];
304 else
305 return range->pin_base + offset;
306}
307
308/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200309 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
310 * @pctldev: pin controller device to check
311 * @gpio: gpio pin to check taken from the global GPIO pin space
312 *
313 * Tries to match a GPIO pin number to the ranges handled by a certain pin
314 * controller, return the range or NULL
315 */
316static struct pinctrl_gpio_range *
317pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
318{
319 struct pinctrl_gpio_range *range = NULL;
320
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200321 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200322 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200323 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
324 /* Check if we're in the valid range */
325 if (gpio >= range->base &&
326 gpio < range->base + range->npins) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200327 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200328 return range;
329 }
330 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200331 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200332 return NULL;
333}
334
335/**
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800336 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
337 * the same GPIO chip are in range
338 * @gpio: gpio pin to check taken from the global GPIO pin space
339 *
340 * This function is complement of pinctrl_match_gpio_range(). If the return
341 * value of pinctrl_match_gpio_range() is NULL, this function could be used
342 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
343 * of the same GPIO chip don't have back-end pinctrl interface.
344 * If the return value is true, it means that pinctrl device is ready & the
345 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
346 * is false, it means that pinctrl device may not be ready.
347 */
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800348#ifdef CONFIG_GPIOLIB
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800349static bool pinctrl_ready_for_gpio_range(unsigned gpio)
350{
351 struct pinctrl_dev *pctldev;
352 struct pinctrl_gpio_range *range = NULL;
353 struct gpio_chip *chip = gpio_to_chip(gpio);
354
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200355 mutex_lock(&pinctrldev_list_mutex);
356
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800357 /* Loop over the pin controllers */
358 list_for_each_entry(pctldev, &pinctrldev_list, node) {
359 /* Loop over the ranges */
360 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
361 /* Check if any gpio range overlapped with gpio chip */
362 if (range->base + range->npins - 1 < chip->base ||
363 range->base > chip->base + chip->ngpio - 1)
364 continue;
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200365 mutex_unlock(&pinctrldev_list_mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800366 return true;
367 }
368 }
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200369
370 mutex_unlock(&pinctrldev_list_mutex);
371
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800372 return false;
373}
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800374#else
375static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
376#endif
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800377
378/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200379 * pinctrl_get_device_gpio_range() - find device for GPIO range
380 * @gpio: the pin to locate the pin controller for
381 * @outdev: the pin control device if found
382 * @outrange: the GPIO range if found
383 *
384 * Find the pin controller handling a certain GPIO pin from the pinspace of
385 * the GPIO subsystem, return the device and the matching GPIO range. Returns
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800386 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
387 * may still have not been registered.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200388 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700389static int pinctrl_get_device_gpio_range(unsigned gpio,
390 struct pinctrl_dev **outdev,
391 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200392{
393 struct pinctrl_dev *pctldev = NULL;
394
395 /* Loop over the pin controllers */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200396 list_for_each_entry(pctldev, &pinctrldev_list, node) {
397 struct pinctrl_gpio_range *range;
398
399 range = pinctrl_match_gpio_range(pctldev, gpio);
400 if (range != NULL) {
401 *outdev = pctldev;
402 *outrange = range;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200403 return 0;
404 }
405 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200406
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800407 return -EPROBE_DEFER;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200408}
409
410/**
411 * pinctrl_add_gpio_range() - register a GPIO range for a controller
412 * @pctldev: pin controller device to add the range to
413 * @range: the GPIO range to add
414 *
415 * This adds a range of GPIOs to be handled by a certain pin controller. Call
416 * this to register handled ranges after registering your pin controller.
417 */
418void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
419 struct pinctrl_gpio_range *range)
420{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200421 mutex_lock(&pctldev->mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700422 list_add_tail(&range->node, &pctldev->gpio_ranges);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200423 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200424}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700425EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200426
Dong Aisheng3e5e00b2012-05-23 21:22:41 +0800427void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
428 struct pinctrl_gpio_range *ranges,
429 unsigned nranges)
430{
431 int i;
432
433 for (i = 0; i < nranges; i++)
434 pinctrl_add_gpio_range(pctldev, &ranges[i]);
435}
436EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
437
Linus Walleij192c3692012-11-20 14:03:37 +0100438struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530439 struct pinctrl_gpio_range *range)
440{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200441 struct pinctrl_dev *pctldev;
442
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200443 pctldev = get_pinctrl_dev_from_devname(devname);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530444
Linus Walleijdfa97512012-11-20 14:54:18 +0100445 /*
446 * If we can't find this device, let's assume that is because
447 * it has not probed yet, so the driver trying to register this
448 * range need to defer probing.
449 */
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200450 if (!pctldev) {
Linus Walleijdfa97512012-11-20 14:54:18 +0100451 return ERR_PTR(-EPROBE_DEFER);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200452 }
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530453 pinctrl_add_gpio_range(pctldev, range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200454
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530455 return pctldev;
456}
Linus Walleij192c3692012-11-20 14:03:37 +0100457EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530458
Linus Walleij2744e8a2011-05-02 20:50:54 +0200459/**
Linus Walleij9afbefb2012-11-20 14:25:07 +0100460 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
461 * @pctldev: the pin controller device to look in
462 * @pin: a controller-local number to find the range for
463 */
464struct pinctrl_gpio_range *
465pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
466 unsigned int pin)
467{
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800468 struct pinctrl_gpio_range *range;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100469
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200470 mutex_lock(&pctldev->mutex);
Linus Walleij9afbefb2012-11-20 14:25:07 +0100471 /* Loop over the ranges */
472 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
473 /* Check if we're in the valid range */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200474 if (range->pins) {
475 int a;
476 for (a = 0; a < range->npins; a++) {
477 if (range->pins[a] == pin)
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800478 goto out;
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200479 }
480 } else if (pin >= range->pin_base &&
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800481 pin < range->pin_base + range->npins)
482 goto out;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100483 }
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800484 range = NULL;
485out:
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200486 mutex_unlock(&pctldev->mutex);
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800487 return range;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100488}
489EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
490
491/**
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530492 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
493 * @pctldev: pin controller device to remove the range from
494 * @range: the GPIO range to remove
495 */
496void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
497 struct pinctrl_gpio_range *range)
498{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200499 mutex_lock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530500 list_del(&range->node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200501 mutex_unlock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530502}
503EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
504
505/**
Linus Walleij7afde8b2011-10-19 17:07:16 +0200506 * pinctrl_get_group_selector() - returns the group selector for a group
507 * @pctldev: the pin controller handling the group
508 * @pin_group: the pin group to look up
509 */
510int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
511 const char *pin_group)
512{
513 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530514 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200515 unsigned group_selector = 0;
516
Viresh Kumard1e90e92012-03-30 11:25:40 +0530517 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200518 const char *gname = pctlops->get_group_name(pctldev,
519 group_selector);
520 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700521 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200522 "found group selector %u for %s\n",
523 group_selector,
524 pin_group);
525 return group_selector;
526 }
527
528 group_selector++;
529 }
530
Stephen Warren51cd24e2011-12-09 16:59:05 -0700531 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200532 pin_group);
533
534 return -EINVAL;
535}
536
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100537/**
538 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
539 * @gpio: the GPIO pin number from the GPIO subsystem number space
540 *
541 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
542 * as part of their gpio_request() semantics, platforms and individual drivers
543 * shall *NOT* request GPIO pins to be muxed in.
544 */
545int pinctrl_request_gpio(unsigned gpio)
546{
547 struct pinctrl_dev *pctldev;
548 struct pinctrl_gpio_range *range;
549 int ret;
550 int pin;
551
552 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700553 if (ret) {
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800554 if (pinctrl_ready_for_gpio_range(gpio))
555 ret = 0;
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800556 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700557 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100558
559 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200560 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100561
Stephen Warren57b676f2012-03-02 13:05:44 -0700562 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
563
Stephen Warren57b676f2012-03-02 13:05:44 -0700564 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100565}
566EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
567
568/**
569 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
570 * @gpio: the GPIO pin number from the GPIO subsystem number space
571 *
572 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
573 * as part of their gpio_free() semantics, platforms and individual drivers
574 * shall *NOT* request GPIO pins to be muxed out.
575 */
576void pinctrl_free_gpio(unsigned gpio)
577{
578 struct pinctrl_dev *pctldev;
579 struct pinctrl_gpio_range *range;
580 int ret;
581 int pin;
582
583 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700584 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100585 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700586 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200587 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100588
589 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200590 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100591
Stephen Warren57b676f2012-03-02 13:05:44 -0700592 pinmux_free_gpio(pctldev, pin, range);
593
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200594 mutex_unlock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100595}
596EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
597
598static int pinctrl_gpio_direction(unsigned gpio, bool input)
599{
600 struct pinctrl_dev *pctldev;
601 struct pinctrl_gpio_range *range;
602 int ret;
603 int pin;
604
605 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200606 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100607 return ret;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200608 }
609
610 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100611
612 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200613 pin = gpio_to_pin(range, gpio);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200614 ret = pinmux_gpio_direction(pctldev, range, pin, input);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100615
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200616 mutex_unlock(&pctldev->mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200617
618 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100619}
620
621/**
622 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
623 * @gpio: the GPIO pin number from the GPIO subsystem number space
624 *
625 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
626 * as part of their gpio_direction_input() semantics, platforms and individual
627 * drivers shall *NOT* touch pin control GPIO calls.
628 */
629int pinctrl_gpio_direction_input(unsigned gpio)
630{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200631 return pinctrl_gpio_direction(gpio, true);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100632}
633EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
634
635/**
636 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
637 * @gpio: the GPIO pin number from the GPIO subsystem number space
638 *
639 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
640 * as part of their gpio_direction_output() semantics, platforms and individual
641 * drivers shall *NOT* touch pin control GPIO calls.
642 */
643int pinctrl_gpio_direction_output(unsigned gpio)
644{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200645 return pinctrl_gpio_direction(gpio, false);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100646}
647EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
648
Stephen Warren6e5e9592012-03-02 13:05:47 -0700649static struct pinctrl_state *find_state(struct pinctrl *p,
650 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100651{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700652 struct pinctrl_state *state;
653
654 list_for_each_entry(state, &p->states, node)
655 if (!strcmp(state->name, name))
656 return state;
657
658 return NULL;
659}
660
661static struct pinctrl_state *create_state(struct pinctrl *p,
662 const char *name)
663{
664 struct pinctrl_state *state;
665
666 state = kzalloc(sizeof(*state), GFP_KERNEL);
667 if (state == NULL) {
668 dev_err(p->dev,
669 "failed to alloc struct pinctrl_state\n");
670 return ERR_PTR(-ENOMEM);
671 }
672
673 state->name = name;
674 INIT_LIST_HEAD(&state->settings);
675
676 list_add_tail(&state->node, &p->states);
677
678 return state;
679}
680
681static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
682{
683 struct pinctrl_state *state;
684 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700685 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700686
687 state = find_state(p, map->name);
688 if (!state)
689 state = create_state(p, map->name);
690 if (IS_ERR(state))
691 return PTR_ERR(state);
692
Stephen Warren1e2082b2012-03-02 13:05:48 -0700693 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
694 return 0;
695
Stephen Warren6e5e9592012-03-02 13:05:47 -0700696 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
697 if (setting == NULL) {
698 dev_err(p->dev,
699 "failed to alloc struct pinctrl_setting\n");
700 return -ENOMEM;
701 }
702
Stephen Warren1e2082b2012-03-02 13:05:48 -0700703 setting->type = map->type;
704
Stephen Warren6e5e9592012-03-02 13:05:47 -0700705 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
706 if (setting->pctldev == NULL) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700707 kfree(setting);
Linus Walleij89216492012-12-11 14:14:32 +0100708 /* Do not defer probing of hogs (circular loop) */
709 if (!strcmp(map->ctrl_dev_name, map->dev_name))
710 return -ENODEV;
Linus Walleijc05127c2012-04-10 10:00:38 +0200711 /*
712 * OK let us guess that the driver is not there yet, and
713 * let's defer obtaining this pinctrl handle to later...
714 */
Linus Walleij89216492012-12-11 14:14:32 +0100715 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
716 map->ctrl_dev_name);
Linus Walleijc05127c2012-04-10 10:00:38 +0200717 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700718 }
719
Linus Walleij1a789582012-10-17 20:51:54 +0200720 setting->dev_name = map->dev_name;
721
Stephen Warren1e2082b2012-03-02 13:05:48 -0700722 switch (map->type) {
723 case PIN_MAP_TYPE_MUX_GROUP:
724 ret = pinmux_map_to_setting(map, setting);
725 break;
726 case PIN_MAP_TYPE_CONFIGS_PIN:
727 case PIN_MAP_TYPE_CONFIGS_GROUP:
728 ret = pinconf_map_to_setting(map, setting);
729 break;
730 default:
731 ret = -EINVAL;
732 break;
733 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700734 if (ret < 0) {
735 kfree(setting);
736 return ret;
737 }
738
739 list_add_tail(&setting->node, &state->settings);
740
741 return 0;
742}
743
744static struct pinctrl *find_pinctrl(struct device *dev)
745{
746 struct pinctrl *p;
747
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200748 mutex_lock(&pinctrl_list_mutex);
Stephen Warren1e2082b2012-03-02 13:05:48 -0700749 list_for_each_entry(p, &pinctrl_list, node)
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200750 if (p->dev == dev) {
751 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700752 return p;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200753 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700754
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200755 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700756 return NULL;
757}
758
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200759static void pinctrl_free(struct pinctrl *p, bool inlist);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700760
761static struct pinctrl *create_pinctrl(struct device *dev)
762{
763 struct pinctrl *p;
764 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700765 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100766 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700767 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700768 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100769
770 /*
771 * create the state cookie holder struct pinctrl for each
772 * mapping, this is what consumers will get when requesting
773 * a pin control handle with pinctrl_get()
774 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700775 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700776 if (p == NULL) {
777 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100778 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700779 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700780 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700781 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -0600782 INIT_LIST_HEAD(&p->dt_maps);
783
784 ret = pinctrl_dt_to_map(p);
785 if (ret < 0) {
786 kfree(p);
787 return ERR_PTR(ret);
788 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700789
790 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100791
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200792 mutex_lock(&pinctrl_maps_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100793 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700794 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -0700795 /* Map must be for this device */
796 if (strcmp(map->dev_name, devname))
797 continue;
798
Stephen Warren6e5e9592012-03-02 13:05:47 -0700799 ret = add_setting(p, map);
Linus Walleij89216492012-12-11 14:14:32 +0100800 /*
801 * At this point the adding of a setting may:
802 *
803 * - Defer, if the pinctrl device is not yet available
804 * - Fail, if the pinctrl device is not yet available,
805 * AND the setting is a hog. We cannot defer that, since
806 * the hog will kick in immediately after the device
807 * is registered.
808 *
809 * If the error returned was not -EPROBE_DEFER then we
810 * accumulate the errors to see if we end up with
811 * an -EPROBE_DEFER later, as that is the worst case.
812 */
813 if (ret == -EPROBE_DEFER) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200814 pinctrl_free(p, false);
815 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700816 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100817 }
818 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200819 mutex_unlock(&pinctrl_maps_mutex);
820
Linus Walleij89216492012-12-11 14:14:32 +0100821 if (ret < 0) {
822 /* If some other error than deferral occured, return here */
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200823 pinctrl_free(p, false);
Linus Walleij89216492012-12-11 14:14:32 +0100824 return ERR_PTR(ret);
825 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100826
Linus Walleijab780292013-01-22 10:56:14 -0700827 kref_init(&p->users);
828
Linus Walleijb0666ba2012-12-11 13:12:35 +0100829 /* Add the pinctrl handle to the global list */
Stephen Warren8b9c1392012-02-19 23:45:42 -0700830 list_add_tail(&p->node, &pinctrl_list);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100831
832 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700833}
Stephen Warren7ecdb162012-03-02 13:05:45 -0700834
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200835/**
836 * pinctrl_get() - retrieves the pinctrl handle for a device
837 * @dev: the device to obtain the handle for
838 */
839struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700840{
841 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700842
Stephen Warren6e5e9592012-03-02 13:05:47 -0700843 if (WARN_ON(!dev))
844 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700845
Linus Walleijab780292013-01-22 10:56:14 -0700846 /*
847 * See if somebody else (such as the device core) has already
848 * obtained a handle to the pinctrl for this device. In that case,
849 * return another pointer to it.
850 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700851 p = find_pinctrl(dev);
Linus Walleijab780292013-01-22 10:56:14 -0700852 if (p != NULL) {
853 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
854 kref_get(&p->users);
855 return p;
856 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700857
Richard Genoudd599bfb2012-08-10 16:53:49 +0200858 return create_pinctrl(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100859}
860EXPORT_SYMBOL_GPL(pinctrl_get);
861
Richard Genoudd3cee8302013-03-25 15:47:21 +0100862static void pinctrl_free_setting(bool disable_setting,
863 struct pinctrl_setting *setting)
864{
865 switch (setting->type) {
866 case PIN_MAP_TYPE_MUX_GROUP:
867 if (disable_setting)
868 pinmux_disable_setting(setting);
869 pinmux_free_setting(setting);
870 break;
871 case PIN_MAP_TYPE_CONFIGS_PIN:
872 case PIN_MAP_TYPE_CONFIGS_GROUP:
873 pinconf_free_setting(setting);
874 break;
875 default:
876 break;
877 }
878}
879
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200880static void pinctrl_free(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -0700881{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700882 struct pinctrl_state *state, *n1;
883 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700884
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200885 mutex_lock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700886 list_for_each_entry_safe(state, n1, &p->states, node) {
887 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Richard Genoudd3cee8302013-03-25 15:47:21 +0100888 pinctrl_free_setting(state == p->state, setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700889 list_del(&setting->node);
890 kfree(setting);
891 }
892 list_del(&state->node);
893 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700894 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700895
Stephen Warren57291ce2012-03-23 10:29:46 -0600896 pinctrl_dt_free_maps(p);
897
Stephen Warren6e5e9592012-03-02 13:05:47 -0700898 if (inlist)
899 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700900 kfree(p);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200901 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700902}
903
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100904/**
Linus Walleijab780292013-01-22 10:56:14 -0700905 * pinctrl_release() - release the pinctrl handle
906 * @kref: the kref in the pinctrl being released
907 */
Sachin Kamat2917e832013-01-24 15:01:00 +0530908static void pinctrl_release(struct kref *kref)
Linus Walleijab780292013-01-22 10:56:14 -0700909{
910 struct pinctrl *p = container_of(kref, struct pinctrl, users);
911
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200912 pinctrl_free(p, true);
Linus Walleijab780292013-01-22 10:56:14 -0700913}
914
915/**
916 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
Stephen Warren6e5e9592012-03-02 13:05:47 -0700917 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100918 */
919void pinctrl_put(struct pinctrl *p)
920{
Linus Walleijab780292013-01-22 10:56:14 -0700921 kref_put(&p->users, pinctrl_release);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100922}
923EXPORT_SYMBOL_GPL(pinctrl_put);
924
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200925/**
926 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
927 * @p: the pinctrl handle to retrieve the state from
928 * @name: the state name to retrieve
929 */
930struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
931 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -0700932{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700933 struct pinctrl_state *state;
934
935 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800936 if (!state) {
937 if (pinctrl_dummy_state) {
938 /* create dummy state */
939 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
940 name);
941 state = create_state(p, name);
Richard Genoudd599bfb2012-08-10 16:53:49 +0200942 } else
943 state = ERR_PTR(-ENODEV);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800944 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700945
946 return state;
947}
Stephen Warren6e5e9592012-03-02 13:05:47 -0700948EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
949
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200950/**
951 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
952 * @p: the pinctrl handle for the device that requests configuration
953 * @state: the state handle to select/activate/program
954 */
955int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700956{
957 struct pinctrl_setting *setting, *setting2;
Richard Genoud50cf7c82013-03-25 15:47:23 +0100958 struct pinctrl_state *old_state = p->state;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700959 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700960
Stephen Warren6e5e9592012-03-02 13:05:47 -0700961 if (p->state == state)
962 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700963
Stephen Warren6e5e9592012-03-02 13:05:47 -0700964 if (p->state) {
965 /*
966 * The set of groups with a mux configuration in the old state
967 * may not be identical to the set of groups with a mux setting
968 * in the new state. While this might be unusual, it's entirely
969 * possible for the "user"-supplied mapping table to be written
970 * that way. For each group that was configured in the old state
971 * but not in the new state, this code puts that group into a
972 * safe/disabled state.
973 */
974 list_for_each_entry(setting, &p->state->settings, node) {
975 bool found = false;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700976 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
977 continue;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700978 list_for_each_entry(setting2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700979 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
980 continue;
981 if (setting2->data.mux.group ==
982 setting->data.mux.group) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700983 found = true;
984 break;
985 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700986 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700987 if (!found)
988 pinmux_disable_setting(setting);
989 }
990 }
991
Richard Genoud3102a762013-03-25 15:47:22 +0100992 p->state = NULL;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700993
994 /* Apply all the settings for the new state */
995 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700996 switch (setting->type) {
997 case PIN_MAP_TYPE_MUX_GROUP:
998 ret = pinmux_enable_setting(setting);
999 break;
1000 case PIN_MAP_TYPE_CONFIGS_PIN:
1001 case PIN_MAP_TYPE_CONFIGS_GROUP:
1002 ret = pinconf_apply_setting(setting);
1003 break;
1004 default:
1005 ret = -EINVAL;
1006 break;
1007 }
Richard Genoud3102a762013-03-25 15:47:22 +01001008
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001009 if (ret < 0) {
Richard Genoud3102a762013-03-25 15:47:22 +01001010 goto unapply_new_state;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001011 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001012 }
1013
Richard Genoud3102a762013-03-25 15:47:22 +01001014 p->state = state;
1015
Stephen Warren7ecdb162012-03-02 13:05:45 -07001016 return 0;
Richard Genoud3102a762013-03-25 15:47:22 +01001017
1018unapply_new_state:
Richard Genoudda587512013-03-28 12:55:46 +01001019 dev_err(p->dev, "Error applying setting, reverse things back\n");
Richard Genoud3102a762013-03-25 15:47:22 +01001020
Richard Genoud3102a762013-03-25 15:47:22 +01001021 list_for_each_entry(setting2, &state->settings, node) {
1022 if (&setting2->node == &setting->node)
1023 break;
Richard Genoudaf606172013-03-29 10:03:26 +01001024 /*
1025 * All we can do here is pinmux_disable_setting.
1026 * That means that some pins are muxed differently now
1027 * than they were before applying the setting (We can't
1028 * "unmux a pin"!), but it's not a big deal since the pins
1029 * are free to be muxed by another apply_setting.
1030 */
1031 if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
1032 pinmux_disable_setting(setting2);
Richard Genoud3102a762013-03-25 15:47:22 +01001033 }
Richard Genoud8009d5f2013-03-28 12:55:47 +01001034
Richard Genoud385d9422013-03-29 10:03:27 +01001035 /* There's no infinite recursive loop here because p->state is NULL */
1036 if (old_state)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001037 pinctrl_select_state(p, old_state);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001038
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001039 return ret;
1040}
Stephen Warren6e5e9592012-03-02 13:05:47 -07001041EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001042
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001043static void devm_pinctrl_release(struct device *dev, void *res)
1044{
1045 pinctrl_put(*(struct pinctrl **)res);
1046}
1047
1048/**
1049 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
1050 * @dev: the device to obtain the handle for
1051 *
1052 * If there is a need to explicitly destroy the returned struct pinctrl,
1053 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1054 */
1055struct pinctrl *devm_pinctrl_get(struct device *dev)
1056{
1057 struct pinctrl **ptr, *p;
1058
1059 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
1060 if (!ptr)
1061 return ERR_PTR(-ENOMEM);
1062
1063 p = pinctrl_get(dev);
1064 if (!IS_ERR(p)) {
1065 *ptr = p;
1066 devres_add(dev, ptr);
1067 } else {
1068 devres_free(ptr);
1069 }
1070
1071 return p;
1072}
1073EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1074
1075static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1076{
1077 struct pinctrl **p = res;
1078
1079 return *p == data;
1080}
1081
1082/**
1083 * devm_pinctrl_put() - Resource managed pinctrl_put()
1084 * @p: the pinctrl handle to release
1085 *
1086 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1087 * this function will not need to be called and the resource management
1088 * code will ensure that the resource is freed.
1089 */
1090void devm_pinctrl_put(struct pinctrl *p)
1091{
Jingoo Hana72149e2013-02-26 11:34:07 +09001092 WARN_ON(devres_release(p->dev, devm_pinctrl_release,
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001093 devm_pinctrl_match, p));
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001094}
1095EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1096
Stephen Warren57291ce2012-03-23 10:29:46 -06001097int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
1098 bool dup, bool locked)
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001099{
Stephen Warren1e2082b2012-03-02 13:05:48 -07001100 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001101 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001102
1103 pr_debug("add %d pinmux maps\n", num_maps);
1104
1105 /* First sanity check the new mapping */
1106 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001107 if (!maps[i].dev_name) {
1108 pr_err("failed to register map %s (%d): no device given\n",
1109 maps[i].name, i);
1110 return -EINVAL;
1111 }
1112
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001113 if (!maps[i].name) {
1114 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001115 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001116 return -EINVAL;
1117 }
1118
Stephen Warren1e2082b2012-03-02 13:05:48 -07001119 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1120 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001121 pr_err("failed to register map %s (%d): no pin control device given\n",
1122 maps[i].name, i);
1123 return -EINVAL;
1124 }
1125
Stephen Warren1e2082b2012-03-02 13:05:48 -07001126 switch (maps[i].type) {
1127 case PIN_MAP_TYPE_DUMMY_STATE:
1128 break;
1129 case PIN_MAP_TYPE_MUX_GROUP:
1130 ret = pinmux_validate_map(&maps[i], i);
1131 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001132 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001133 break;
1134 case PIN_MAP_TYPE_CONFIGS_PIN:
1135 case PIN_MAP_TYPE_CONFIGS_GROUP:
1136 ret = pinconf_validate_map(&maps[i], i);
1137 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001138 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001139 break;
1140 default:
1141 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001142 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -07001143 return -EINVAL;
1144 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001145 }
1146
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001147 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
1148 if (!maps_node) {
1149 pr_err("failed to alloc struct pinctrl_maps\n");
1150 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001151 }
1152
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001153 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -06001154 if (dup) {
1155 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
1156 GFP_KERNEL);
1157 if (!maps_node->maps) {
1158 pr_err("failed to duplicate mapping table\n");
1159 kfree(maps_node);
1160 return -ENOMEM;
1161 }
1162 } else {
1163 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001164 }
1165
Stephen Warren57291ce2012-03-23 10:29:46 -06001166 if (!locked)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001167 mutex_lock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001168 list_add_tail(&maps_node->node, &pinctrl_maps);
Stephen Warren57291ce2012-03-23 10:29:46 -06001169 if (!locked)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001170 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001171
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001172 return 0;
1173}
1174
Stephen Warren57291ce2012-03-23 10:29:46 -06001175/**
1176 * pinctrl_register_mappings() - register a set of pin controller mappings
1177 * @maps: the pincontrol mappings table to register. This should probably be
1178 * marked with __initdata so it can be discarded after boot. This
1179 * function will perform a shallow copy for the mapping entries.
1180 * @num_maps: the number of maps in the mapping table
1181 */
1182int pinctrl_register_mappings(struct pinctrl_map const *maps,
1183 unsigned num_maps)
1184{
1185 return pinctrl_register_map(maps, num_maps, true, false);
1186}
1187
1188void pinctrl_unregister_map(struct pinctrl_map const *map)
1189{
1190 struct pinctrl_maps *maps_node;
1191
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001192 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001193 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1194 if (maps_node->maps == map) {
1195 list_del(&maps_node->node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001196 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001197 return;
1198 }
1199 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001200 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001201}
1202
Julien Delacou840a47b2012-12-10 14:47:33 +01001203/**
1204 * pinctrl_force_sleep() - turn a given controller device into sleep state
1205 * @pctldev: pin controller device
1206 */
1207int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1208{
1209 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
1210 return pinctrl_select_state(pctldev->p, pctldev->hog_sleep);
1211 return 0;
1212}
1213EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1214
1215/**
1216 * pinctrl_force_default() - turn a given controller device into default state
1217 * @pctldev: pin controller device
1218 */
1219int pinctrl_force_default(struct pinctrl_dev *pctldev)
1220{
1221 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
1222 return pinctrl_select_state(pctldev->p, pctldev->hog_default);
1223 return 0;
1224}
1225EXPORT_SYMBOL_GPL(pinctrl_force_default);
1226
Linus Walleij14005ee2013-06-05 15:30:33 +02001227#ifdef CONFIG_PM
1228
1229/**
Tony Lindgrenf3333492013-07-18 08:15:04 -07001230 * pinctrl_pm_select_state() - select pinctrl state for PM
1231 * @dev: device to select default state for
1232 * @state: state to set
1233 */
1234static int pinctrl_pm_select_state(struct device *dev,
1235 struct pinctrl_state *state)
1236{
1237 struct dev_pin_info *pins = dev->pins;
1238 int ret;
1239
1240 if (IS_ERR(state))
1241 return 0; /* No such state */
1242 ret = pinctrl_select_state(pins->p, state);
1243 if (ret)
1244 dev_err(dev, "failed to activate pinctrl state %s\n",
1245 state->name);
1246 return ret;
1247}
1248
1249/**
Linus Walleij14005ee2013-06-05 15:30:33 +02001250 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1251 * @dev: device to select default state for
1252 */
1253int pinctrl_pm_select_default_state(struct device *dev)
1254{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001255 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001256 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001257
1258 return pinctrl_pm_select_state(dev, dev->pins->default_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001259}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001260EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001261
1262/**
1263 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1264 * @dev: device to select sleep state for
1265 */
1266int pinctrl_pm_select_sleep_state(struct device *dev)
1267{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001268 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001269 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001270
1271 return pinctrl_pm_select_state(dev, dev->pins->sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001272}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001273EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001274
1275/**
1276 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1277 * @dev: device to select idle state for
1278 */
1279int pinctrl_pm_select_idle_state(struct device *dev)
1280{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001281 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001282 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001283
1284 return pinctrl_pm_select_state(dev, dev->pins->idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001285}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001286EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001287#endif
1288
Linus Walleij2744e8a2011-05-02 20:50:54 +02001289#ifdef CONFIG_DEBUG_FS
1290
1291static int pinctrl_pins_show(struct seq_file *s, void *what)
1292{
1293 struct pinctrl_dev *pctldev = s->private;
1294 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001295 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001296
1297 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001298
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001299 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001300
Chanho Park706e8522012-01-03 16:47:50 +09001301 /* The pin number can be retrived from the pin controller descriptor */
1302 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001303 struct pin_desc *desc;
1304
Chanho Park706e8522012-01-03 16:47:50 +09001305 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001306 desc = pin_desc_get(pctldev, pin);
1307 /* Pin space may be sparse */
1308 if (desc == NULL)
1309 continue;
1310
1311 seq_printf(s, "pin %d (%s) ", pin,
1312 desc->name ? desc->name : "unnamed");
1313
1314 /* Driver-specific info per pin */
1315 if (ops->pin_dbg_show)
1316 ops->pin_dbg_show(pctldev, s, pin);
1317
1318 seq_puts(s, "\n");
1319 }
1320
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001321 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001322
Linus Walleij2744e8a2011-05-02 20:50:54 +02001323 return 0;
1324}
1325
1326static int pinctrl_groups_show(struct seq_file *s, void *what)
1327{
1328 struct pinctrl_dev *pctldev = s->private;
1329 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301330 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001331
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001332 mutex_lock(&pctldev->mutex);
1333
Viresh Kumard1e90e92012-03-30 11:25:40 +05301334 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001335
Linus Walleij2744e8a2011-05-02 20:50:54 +02001336 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301337 while (selector < ngroups) {
Stephen Warrena5818a82011-10-19 16:19:25 -06001338 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001339 unsigned num_pins;
1340 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001341 const char *pname;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001342 int ret;
1343 int i;
1344
1345 ret = ops->get_group_pins(pctldev, selector,
1346 &pins, &num_pins);
1347 if (ret)
1348 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1349 gname);
1350 else {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001351 seq_printf(s, "group: %s\n", gname);
1352 for (i = 0; i < num_pins; i++) {
1353 pname = pin_get_name(pctldev, pins[i]);
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001354 if (WARN_ON(!pname)) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001355 mutex_unlock(&pctldev->mutex);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001356 return -EINVAL;
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001357 }
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001358 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1359 }
1360 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001361 }
1362 selector++;
1363 }
1364
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001365 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001366
1367 return 0;
1368}
1369
1370static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1371{
1372 struct pinctrl_dev *pctldev = s->private;
1373 struct pinctrl_gpio_range *range = NULL;
1374
1375 seq_puts(s, "GPIO ranges handled:\n");
1376
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001377 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001378
Linus Walleij2744e8a2011-05-02 20:50:54 +02001379 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001380 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Christian Ruppertc8587ee2013-06-13 14:55:31 +02001381 if (range->pins) {
1382 int a;
1383 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
1384 range->id, range->name,
1385 range->base, (range->base + range->npins - 1));
1386 for (a = 0; a < range->npins - 1; a++)
1387 seq_printf(s, "%u, ", range->pins[a]);
1388 seq_printf(s, "%u}\n", range->pins[a]);
1389 }
1390 else
1391 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1392 range->id, range->name,
1393 range->base, (range->base + range->npins - 1),
1394 range->pin_base,
1395 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001396 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001397
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001398 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001399
1400 return 0;
1401}
1402
1403static int pinctrl_devices_show(struct seq_file *s, void *what)
1404{
1405 struct pinctrl_dev *pctldev;
1406
Linus Walleijae6b4d82011-10-19 18:14:33 +02001407 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001408
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001409 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001410
Linus Walleij2744e8a2011-05-02 20:50:54 +02001411 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1412 seq_printf(s, "%s ", pctldev->desc->name);
1413 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001414 seq_puts(s, "yes ");
1415 else
1416 seq_puts(s, "no ");
1417 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001418 seq_puts(s, "yes");
1419 else
1420 seq_puts(s, "no");
1421 seq_puts(s, "\n");
1422 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001423
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001424 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001425
1426 return 0;
1427}
1428
Stephen Warren1e2082b2012-03-02 13:05:48 -07001429static inline const char *map_type(enum pinctrl_map_type type)
1430{
1431 static const char * const names[] = {
1432 "INVALID",
1433 "DUMMY_STATE",
1434 "MUX_GROUP",
1435 "CONFIGS_PIN",
1436 "CONFIGS_GROUP",
1437 };
1438
1439 if (type >= ARRAY_SIZE(names))
1440 return "UNKNOWN";
1441
1442 return names[type];
1443}
1444
Stephen Warren3eedb432012-02-23 17:04:40 -07001445static int pinctrl_maps_show(struct seq_file *s, void *what)
1446{
1447 struct pinctrl_maps *maps_node;
1448 int i;
1449 struct pinctrl_map const *map;
1450
1451 seq_puts(s, "Pinctrl maps:\n");
1452
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001453 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001454 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001455 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1456 map->dev_name, map->name, map_type(map->type),
1457 map->type);
1458
1459 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1460 seq_printf(s, "controlling device %s\n",
1461 map->ctrl_dev_name);
1462
1463 switch (map->type) {
1464 case PIN_MAP_TYPE_MUX_GROUP:
1465 pinmux_show_map(s, map);
1466 break;
1467 case PIN_MAP_TYPE_CONFIGS_PIN:
1468 case PIN_MAP_TYPE_CONFIGS_GROUP:
1469 pinconf_show_map(s, map);
1470 break;
1471 default:
1472 break;
1473 }
1474
1475 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001476 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001477 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001478
1479 return 0;
1480}
1481
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001482static int pinctrl_show(struct seq_file *s, void *what)
1483{
1484 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001485 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001486 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001487
1488 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001489
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001490 mutex_lock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001491
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001492 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001493 seq_printf(s, "device: %s current state: %s\n",
1494 dev_name(p->dev),
1495 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001496
Stephen Warren6e5e9592012-03-02 13:05:47 -07001497 list_for_each_entry(state, &p->states, node) {
1498 seq_printf(s, " state: %s\n", state->name);
1499
1500 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001501 struct pinctrl_dev *pctldev = setting->pctldev;
1502
1503 seq_printf(s, " type: %s controller %s ",
1504 map_type(setting->type),
1505 pinctrl_dev_get_name(pctldev));
1506
1507 switch (setting->type) {
1508 case PIN_MAP_TYPE_MUX_GROUP:
1509 pinmux_show_setting(s, setting);
1510 break;
1511 case PIN_MAP_TYPE_CONFIGS_PIN:
1512 case PIN_MAP_TYPE_CONFIGS_GROUP:
1513 pinconf_show_setting(s, setting);
1514 break;
1515 default:
1516 break;
1517 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001518 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001519 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001520 }
1521
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001522 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001523
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001524 return 0;
1525}
1526
Linus Walleij2744e8a2011-05-02 20:50:54 +02001527static int pinctrl_pins_open(struct inode *inode, struct file *file)
1528{
1529 return single_open(file, pinctrl_pins_show, inode->i_private);
1530}
1531
1532static int pinctrl_groups_open(struct inode *inode, struct file *file)
1533{
1534 return single_open(file, pinctrl_groups_show, inode->i_private);
1535}
1536
1537static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1538{
1539 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1540}
1541
1542static int pinctrl_devices_open(struct inode *inode, struct file *file)
1543{
1544 return single_open(file, pinctrl_devices_show, NULL);
1545}
1546
Stephen Warren3eedb432012-02-23 17:04:40 -07001547static int pinctrl_maps_open(struct inode *inode, struct file *file)
1548{
1549 return single_open(file, pinctrl_maps_show, NULL);
1550}
1551
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001552static int pinctrl_open(struct inode *inode, struct file *file)
1553{
1554 return single_open(file, pinctrl_show, NULL);
1555}
1556
Linus Walleij2744e8a2011-05-02 20:50:54 +02001557static const struct file_operations pinctrl_pins_ops = {
1558 .open = pinctrl_pins_open,
1559 .read = seq_read,
1560 .llseek = seq_lseek,
1561 .release = single_release,
1562};
1563
1564static const struct file_operations pinctrl_groups_ops = {
1565 .open = pinctrl_groups_open,
1566 .read = seq_read,
1567 .llseek = seq_lseek,
1568 .release = single_release,
1569};
1570
1571static const struct file_operations pinctrl_gpioranges_ops = {
1572 .open = pinctrl_gpioranges_open,
1573 .read = seq_read,
1574 .llseek = seq_lseek,
1575 .release = single_release,
1576};
1577
1578static const struct file_operations pinctrl_devices_ops = {
1579 .open = pinctrl_devices_open,
1580 .read = seq_read,
1581 .llseek = seq_lseek,
1582 .release = single_release,
1583};
1584
Stephen Warren3eedb432012-02-23 17:04:40 -07001585static const struct file_operations pinctrl_maps_ops = {
1586 .open = pinctrl_maps_open,
1587 .read = seq_read,
1588 .llseek = seq_lseek,
1589 .release = single_release,
1590};
1591
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001592static const struct file_operations pinctrl_ops = {
1593 .open = pinctrl_open,
1594 .read = seq_read,
1595 .llseek = seq_lseek,
1596 .release = single_release,
1597};
1598
Linus Walleij2744e8a2011-05-02 20:50:54 +02001599static struct dentry *debugfs_root;
1600
1601static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1602{
Tony Lindgren02157162012-01-20 08:17:22 -08001603 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001604
Stephen Warren51cd24e2011-12-09 16:59:05 -07001605 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001606 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001607 pctldev->device_root = device_root;
1608
Linus Walleij2744e8a2011-05-02 20:50:54 +02001609 if (IS_ERR(device_root) || !device_root) {
1610 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001611 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001612 return;
1613 }
1614 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1615 device_root, pctldev, &pinctrl_pins_ops);
1616 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1617 device_root, pctldev, &pinctrl_groups_ops);
1618 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1619 device_root, pctldev, &pinctrl_gpioranges_ops);
1620 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +02001621 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001622}
1623
Tony Lindgren02157162012-01-20 08:17:22 -08001624static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1625{
1626 debugfs_remove_recursive(pctldev->device_root);
1627}
1628
Linus Walleij2744e8a2011-05-02 20:50:54 +02001629static void pinctrl_init_debugfs(void)
1630{
1631 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1632 if (IS_ERR(debugfs_root) || !debugfs_root) {
1633 pr_warn("failed to create debugfs directory\n");
1634 debugfs_root = NULL;
1635 return;
1636 }
1637
1638 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1639 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001640 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1641 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001642 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1643 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001644}
1645
1646#else /* CONFIG_DEBUG_FS */
1647
1648static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1649{
1650}
1651
1652static void pinctrl_init_debugfs(void)
1653{
1654}
1655
Tony Lindgren02157162012-01-20 08:17:22 -08001656static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1657{
1658}
1659
Linus Walleij2744e8a2011-05-02 20:50:54 +02001660#endif
1661
Stephen Warrend26bc492012-03-16 14:54:25 -06001662static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1663{
1664 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1665
1666 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301667 !ops->get_groups_count ||
Stephen Warrend26bc492012-03-16 14:54:25 -06001668 !ops->get_group_name ||
1669 !ops->get_group_pins)
1670 return -EINVAL;
1671
Stephen Warren57291ce2012-03-23 10:29:46 -06001672 if (ops->dt_node_to_map && !ops->dt_free_map)
1673 return -EINVAL;
1674
Stephen Warrend26bc492012-03-16 14:54:25 -06001675 return 0;
1676}
1677
Linus Walleij2744e8a2011-05-02 20:50:54 +02001678/**
1679 * pinctrl_register() - register a pin controller device
1680 * @pctldesc: descriptor for this pin controller
1681 * @dev: parent device for this pin controller
1682 * @driver_data: private pin controller data for this pin controller
1683 */
1684struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1685 struct device *dev, void *driver_data)
1686{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001687 struct pinctrl_dev *pctldev;
1688 int ret;
1689
Devendra Nagada9aecb2012-06-16 23:43:16 +05301690 if (!pctldesc)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001691 return NULL;
Devendra Nagada9aecb2012-06-16 23:43:16 +05301692 if (!pctldesc->name)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001693 return NULL;
1694
Stephen Warren02f5b982012-02-22 14:26:00 -07001695 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001696 if (pctldev == NULL) {
1697 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001698 return NULL;
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001699 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001700
1701 /* Initialize pin control device struct */
1702 pctldev->owner = pctldesc->owner;
1703 pctldev->desc = pctldesc;
1704 pctldev->driver_data = driver_data;
1705 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001706 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001707 pctldev->dev = dev;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001708 mutex_init(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001709
Stephen Warrend26bc492012-03-16 14:54:25 -06001710 /* check core ops for sanity */
Devendra Nagada9aecb2012-06-16 23:43:16 +05301711 if (pinctrl_check_ops(pctldev)) {
John Crispinad6e1102012-04-26 16:47:11 +02001712 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06001713 goto out_err;
1714 }
1715
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001716 /* If we're implementing pinmuxing, check the ops for sanity */
1717 if (pctldesc->pmxops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301718 if (pinmux_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001719 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001720 }
1721
1722 /* If we're implementing pinconfig, check the ops for sanity */
1723 if (pctldesc->confops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301724 if (pinconf_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001725 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001726 }
1727
Linus Walleij2744e8a2011-05-02 20:50:54 +02001728 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02001729 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001730 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1731 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001732 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001733 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1734 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001735 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001736 }
1737
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001738 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -07001739 list_add_tail(&pctldev->node, &pinctrldev_list);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001740 mutex_unlock(&pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001741
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001742 pctldev->p = pinctrl_get(pctldev->dev);
1743
Stephen Warren6e5e9592012-03-02 13:05:47 -07001744 if (!IS_ERR(pctldev->p)) {
Julien Delacou840a47b2012-12-10 14:47:33 +01001745 pctldev->hog_default =
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001746 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
Julien Delacou840a47b2012-12-10 14:47:33 +01001747 if (IS_ERR(pctldev->hog_default)) {
John Crispinad6e1102012-04-26 16:47:11 +02001748 dev_dbg(dev, "failed to lookup the default state\n");
1749 } else {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001750 if (pinctrl_select_state(pctldev->p,
Julien Delacou840a47b2012-12-10 14:47:33 +01001751 pctldev->hog_default))
John Crispinad6e1102012-04-26 16:47:11 +02001752 dev_err(dev,
1753 "failed to select default state\n");
John Crispinad6e1102012-04-26 16:47:11 +02001754 }
Julien Delacou840a47b2012-12-10 14:47:33 +01001755
1756 pctldev->hog_sleep =
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001757 pinctrl_lookup_state(pctldev->p,
Julien Delacou840a47b2012-12-10 14:47:33 +01001758 PINCTRL_STATE_SLEEP);
1759 if (IS_ERR(pctldev->hog_sleep))
1760 dev_dbg(dev, "failed to lookup the sleep state\n");
Stephen Warren6e5e9592012-03-02 13:05:47 -07001761 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001762
Stephen Warren2304b472012-02-22 14:26:01 -07001763 pinctrl_init_device_debugfs(pctldev);
1764
Linus Walleij2744e8a2011-05-02 20:50:54 +02001765 return pctldev;
1766
Stephen Warren51cd24e2011-12-09 16:59:05 -07001767out_err:
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001768 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001769 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001770 return NULL;
1771}
1772EXPORT_SYMBOL_GPL(pinctrl_register);
1773
1774/**
1775 * pinctrl_unregister() - unregister pinmux
1776 * @pctldev: pin controller to unregister
1777 *
1778 * Called by pinmux drivers to unregister a pinmux.
1779 */
1780void pinctrl_unregister(struct pinctrl_dev *pctldev)
1781{
Dong Aisheng5d589b02012-05-23 21:22:40 +08001782 struct pinctrl_gpio_range *range, *n;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001783 if (pctldev == NULL)
1784 return;
1785
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001786 mutex_lock(&pinctrldev_list_mutex);
1787 mutex_lock(&pctldev->mutex);
1788
Tony Lindgren02157162012-01-20 08:17:22 -08001789 pinctrl_remove_device_debugfs(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001790
Stephen Warren6e5e9592012-03-02 13:05:47 -07001791 if (!IS_ERR(pctldev->p))
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001792 pinctrl_put(pctldev->p);
Stephen Warren57b676f2012-03-02 13:05:44 -07001793
Linus Walleij2744e8a2011-05-02 20:50:54 +02001794 /* TODO: check that no pinmuxes are still active? */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001795 list_del(&pctldev->node);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001796 /* Destroy descriptor tree */
1797 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1798 pctldev->desc->npins);
Dong Aisheng5d589b02012-05-23 21:22:40 +08001799 /* remove gpio ranges map */
1800 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1801 list_del(&range->node);
1802
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001803 mutex_unlock(&pctldev->mutex);
1804 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001805 kfree(pctldev);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001806 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001807}
1808EXPORT_SYMBOL_GPL(pinctrl_unregister);
1809
1810static int __init pinctrl_init(void)
1811{
1812 pr_info("initialized pinctrl subsystem\n");
1813 pinctrl_init_debugfs();
1814 return 0;
1815}
1816
1817/* init early since many drivers really need to initialized pinmux early */
1818core_initcall(pinctrl_init);