blob: 5ee61a470016fa8f2753cf21fc2e6ff0966d8d78 [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 */
Axel Lin6c325f82013-08-19 10:06:29 +0800156 if (desc && !strcmp(name, desc->name))
Linus Walleijae6b4d82011-10-19 18:14:33 +0200157 return pin;
158 }
159
160 return -EINVAL;
161}
162
163/**
Dong Aishengdcb5dbc2012-04-17 15:00:46 +0800164 * pin_get_name_from_id() - look up a pin name from a pin id
165 * @pctldev: the pin control device to lookup the pin on
166 * @name: the name of the pin to look up
167 */
168const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
169{
170 const struct pin_desc *desc;
171
172 desc = pin_desc_get(pctldev, pin);
173 if (desc == NULL) {
174 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
175 pin);
176 return NULL;
177 }
178
179 return desc->name;
180}
181
182/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200183 * pin_is_valid() - check if pin exists on controller
184 * @pctldev: the pin control device to check the pin on
185 * @pin: pin to check, use the local pin controller index number
186 *
187 * This tells us whether a certain pin exist on a certain pin controller or
188 * not. Pin lists may be sparse, so some pins may not exist.
189 */
190bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
191{
192 struct pin_desc *pindesc;
193
194 if (pin < 0)
195 return false;
196
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200197 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200198 pindesc = pin_desc_get(pctldev, pin);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200199 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200200
Stephen Warren57b676f2012-03-02 13:05:44 -0700201 return pindesc != NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200202}
203EXPORT_SYMBOL_GPL(pin_is_valid);
204
205/* Deletes a range of pin descriptors */
206static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
207 const struct pinctrl_pin_desc *pins,
208 unsigned num_pins)
209{
210 int i;
211
Linus Walleij2744e8a2011-05-02 20:50:54 +0200212 for (i = 0; i < num_pins; i++) {
213 struct pin_desc *pindesc;
214
215 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
216 pins[i].number);
217 if (pindesc != NULL) {
218 radix_tree_delete(&pctldev->pin_desc_tree,
219 pins[i].number);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100220 if (pindesc->dynamic_name)
221 kfree(pindesc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200222 }
223 kfree(pindesc);
224 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200225}
226
227static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
228 unsigned number, const char *name)
229{
230 struct pin_desc *pindesc;
231
232 pindesc = pin_desc_get(pctldev, number);
233 if (pindesc != NULL) {
234 pr_err("pin %d already registered on %s\n", number,
235 pctldev->desc->name);
236 return -EINVAL;
237 }
238
239 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700240 if (pindesc == NULL) {
241 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200242 return -ENOMEM;
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700243 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200244
Linus Walleij2744e8a2011-05-02 20:50:54 +0200245 /* Set owner */
246 pindesc->pctldev = pctldev;
247
Stephen Warren9af1e442011-10-19 16:19:27 -0600248 /* Copy basic pin info */
Linus Walleij8dc6ae42012-02-01 18:11:40 +0100249 if (name) {
Linus Walleijca53c5f2011-12-14 20:33:37 +0100250 pindesc->name = name;
251 } else {
252 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
Sachin Kamateb26cc92012-09-25 12:41:40 +0530253 if (pindesc->name == NULL) {
254 kfree(pindesc);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100255 return -ENOMEM;
Sachin Kamateb26cc92012-09-25 12:41:40 +0530256 }
Linus Walleijca53c5f2011-12-14 20:33:37 +0100257 pindesc->dynamic_name = true;
258 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200259
Linus Walleij2744e8a2011-05-02 20:50:54 +0200260 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200261 pr_debug("registered pin %d (%s) on %s\n",
Linus Walleijca53c5f2011-12-14 20:33:37 +0100262 number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200263 return 0;
264}
265
266static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
267 struct pinctrl_pin_desc const *pins,
268 unsigned num_descs)
269{
270 unsigned i;
271 int ret = 0;
272
273 for (i = 0; i < num_descs; i++) {
274 ret = pinctrl_register_one_pin(pctldev,
275 pins[i].number, pins[i].name);
276 if (ret)
277 return ret;
278 }
279
280 return 0;
281}
282
283/**
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200284 * gpio_to_pin() - GPIO range GPIO number to pin number translation
285 * @range: GPIO range used for the translation
286 * @gpio: gpio pin to translate to a pin number
287 *
288 * Finds the pin number for a given GPIO using the specified GPIO range
289 * as a base for translation. The distinction between linear GPIO ranges
290 * and pin list based GPIO ranges is managed correctly by this function.
291 *
292 * This function assumes the gpio is part of the specified GPIO range, use
293 * only after making sure this is the case (e.g. by calling it on the
294 * result of successful pinctrl_get_device_gpio_range calls)!
295 */
296static inline int gpio_to_pin(struct pinctrl_gpio_range *range,
297 unsigned int gpio)
298{
299 unsigned int offset = gpio - range->base;
300 if (range->pins)
301 return range->pins[offset];
302 else
303 return range->pin_base + offset;
304}
305
306/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200307 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
308 * @pctldev: pin controller device to check
309 * @gpio: gpio pin to check taken from the global GPIO pin space
310 *
311 * Tries to match a GPIO pin number to the ranges handled by a certain pin
312 * controller, return the range or NULL
313 */
314static struct pinctrl_gpio_range *
315pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
316{
317 struct pinctrl_gpio_range *range = NULL;
318
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200319 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200320 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200321 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
322 /* Check if we're in the valid range */
323 if (gpio >= range->base &&
324 gpio < range->base + range->npins) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200325 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200326 return range;
327 }
328 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200329 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200330 return NULL;
331}
332
333/**
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800334 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
335 * the same GPIO chip are in range
336 * @gpio: gpio pin to check taken from the global GPIO pin space
337 *
338 * This function is complement of pinctrl_match_gpio_range(). If the return
339 * value of pinctrl_match_gpio_range() is NULL, this function could be used
340 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
341 * of the same GPIO chip don't have back-end pinctrl interface.
342 * If the return value is true, it means that pinctrl device is ready & the
343 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
344 * is false, it means that pinctrl device may not be ready.
345 */
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800346#ifdef CONFIG_GPIOLIB
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800347static bool pinctrl_ready_for_gpio_range(unsigned gpio)
348{
349 struct pinctrl_dev *pctldev;
350 struct pinctrl_gpio_range *range = NULL;
351 struct gpio_chip *chip = gpio_to_chip(gpio);
352
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200353 mutex_lock(&pinctrldev_list_mutex);
354
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800355 /* Loop over the pin controllers */
356 list_for_each_entry(pctldev, &pinctrldev_list, node) {
357 /* Loop over the ranges */
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800358 mutex_lock(&pctldev->mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800359 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
360 /* Check if any gpio range overlapped with gpio chip */
361 if (range->base + range->npins - 1 < chip->base ||
362 range->base > chip->base + chip->ngpio - 1)
363 continue;
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800364 mutex_unlock(&pctldev->mutex);
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200365 mutex_unlock(&pinctrldev_list_mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800366 return true;
367 }
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800368 mutex_unlock(&pctldev->mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800369 }
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200370
371 mutex_unlock(&pinctrldev_list_mutex);
372
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800373 return false;
374}
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800375#else
376static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
377#endif
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800378
379/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200380 * pinctrl_get_device_gpio_range() - find device for GPIO range
381 * @gpio: the pin to locate the pin controller for
382 * @outdev: the pin control device if found
383 * @outrange: the GPIO range if found
384 *
385 * Find the pin controller handling a certain GPIO pin from the pinspace of
386 * the GPIO subsystem, return the device and the matching GPIO range. Returns
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800387 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
388 * may still have not been registered.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200389 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700390static int pinctrl_get_device_gpio_range(unsigned gpio,
391 struct pinctrl_dev **outdev,
392 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200393{
394 struct pinctrl_dev *pctldev = NULL;
395
Axel Linf0059022013-08-18 20:34:22 +0800396 mutex_lock(&pinctrldev_list_mutex);
397
Linus Walleij2744e8a2011-05-02 20:50:54 +0200398 /* Loop over the pin controllers */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200399 list_for_each_entry(pctldev, &pinctrldev_list, node) {
400 struct pinctrl_gpio_range *range;
401
402 range = pinctrl_match_gpio_range(pctldev, gpio);
403 if (range != NULL) {
404 *outdev = pctldev;
405 *outrange = range;
Axel Linf0059022013-08-18 20:34:22 +0800406 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200407 return 0;
408 }
409 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200410
Axel Linf0059022013-08-18 20:34:22 +0800411 mutex_unlock(&pinctrldev_list_mutex);
412
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800413 return -EPROBE_DEFER;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200414}
415
416/**
417 * pinctrl_add_gpio_range() - register a GPIO range for a controller
418 * @pctldev: pin controller device to add the range to
419 * @range: the GPIO range to add
420 *
421 * This adds a range of GPIOs to be handled by a certain pin controller. Call
422 * this to register handled ranges after registering your pin controller.
423 */
424void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
425 struct pinctrl_gpio_range *range)
426{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200427 mutex_lock(&pctldev->mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700428 list_add_tail(&range->node, &pctldev->gpio_ranges);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200429 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200430}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700431EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200432
Dong Aisheng3e5e00b2012-05-23 21:22:41 +0800433void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
434 struct pinctrl_gpio_range *ranges,
435 unsigned nranges)
436{
437 int i;
438
439 for (i = 0; i < nranges; i++)
440 pinctrl_add_gpio_range(pctldev, &ranges[i]);
441}
442EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
443
Linus Walleij192c3692012-11-20 14:03:37 +0100444struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530445 struct pinctrl_gpio_range *range)
446{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200447 struct pinctrl_dev *pctldev;
448
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200449 pctldev = get_pinctrl_dev_from_devname(devname);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530450
Linus Walleijdfa97512012-11-20 14:54:18 +0100451 /*
452 * If we can't find this device, let's assume that is because
453 * it has not probed yet, so the driver trying to register this
454 * range need to defer probing.
455 */
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200456 if (!pctldev) {
Linus Walleijdfa97512012-11-20 14:54:18 +0100457 return ERR_PTR(-EPROBE_DEFER);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200458 }
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530459 pinctrl_add_gpio_range(pctldev, range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200460
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530461 return pctldev;
462}
Linus Walleij192c3692012-11-20 14:03:37 +0100463EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530464
Christian Ruppert586a87e2013-10-15 15:37:54 +0200465int pinctrl_get_group_pins(struct pinctrl_dev *pctldev, const char *pin_group,
466 const unsigned **pins, unsigned *num_pins)
467{
468 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
469 int gs;
470
471 gs = pinctrl_get_group_selector(pctldev, pin_group);
472 if (gs < 0)
473 return gs;
474
475 return pctlops->get_group_pins(pctldev, gs, pins, num_pins);
476}
477EXPORT_SYMBOL_GPL(pinctrl_get_group_pins);
478
Linus Walleij2744e8a2011-05-02 20:50:54 +0200479/**
Linus Walleij9afbefb2012-11-20 14:25:07 +0100480 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
481 * @pctldev: the pin controller device to look in
482 * @pin: a controller-local number to find the range for
483 */
484struct pinctrl_gpio_range *
485pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
486 unsigned int pin)
487{
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800488 struct pinctrl_gpio_range *range;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100489
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200490 mutex_lock(&pctldev->mutex);
Linus Walleij9afbefb2012-11-20 14:25:07 +0100491 /* Loop over the ranges */
492 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
493 /* Check if we're in the valid range */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200494 if (range->pins) {
495 int a;
496 for (a = 0; a < range->npins; a++) {
497 if (range->pins[a] == pin)
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800498 goto out;
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200499 }
500 } else if (pin >= range->pin_base &&
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800501 pin < range->pin_base + range->npins)
502 goto out;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100503 }
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800504 range = NULL;
505out:
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200506 mutex_unlock(&pctldev->mutex);
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800507 return range;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100508}
509EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
510
511/**
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530512 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
513 * @pctldev: pin controller device to remove the range from
514 * @range: the GPIO range to remove
515 */
516void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
517 struct pinctrl_gpio_range *range)
518{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200519 mutex_lock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530520 list_del(&range->node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200521 mutex_unlock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530522}
523EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
524
525/**
Linus Walleij7afde8b2011-10-19 17:07:16 +0200526 * pinctrl_get_group_selector() - returns the group selector for a group
527 * @pctldev: the pin controller handling the group
528 * @pin_group: the pin group to look up
529 */
530int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
531 const char *pin_group)
532{
533 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530534 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200535 unsigned group_selector = 0;
536
Viresh Kumard1e90e92012-03-30 11:25:40 +0530537 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200538 const char *gname = pctlops->get_group_name(pctldev,
539 group_selector);
540 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700541 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200542 "found group selector %u for %s\n",
543 group_selector,
544 pin_group);
545 return group_selector;
546 }
547
548 group_selector++;
549 }
550
Stephen Warren51cd24e2011-12-09 16:59:05 -0700551 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200552 pin_group);
553
554 return -EINVAL;
555}
556
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100557/**
558 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
559 * @gpio: the GPIO pin number from the GPIO subsystem number space
560 *
561 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
562 * as part of their gpio_request() semantics, platforms and individual drivers
563 * shall *NOT* request GPIO pins to be muxed in.
564 */
565int pinctrl_request_gpio(unsigned gpio)
566{
567 struct pinctrl_dev *pctldev;
568 struct pinctrl_gpio_range *range;
569 int ret;
570 int pin;
571
572 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700573 if (ret) {
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800574 if (pinctrl_ready_for_gpio_range(gpio))
575 ret = 0;
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800576 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700577 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100578
Axel Lin9b77ace2013-08-19 10:07:46 +0800579 mutex_lock(&pctldev->mutex);
580
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100581 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200582 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100583
Stephen Warren57b676f2012-03-02 13:05:44 -0700584 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
585
Axel Lin9b77ace2013-08-19 10:07:46 +0800586 mutex_unlock(&pctldev->mutex);
587
Stephen Warren57b676f2012-03-02 13:05:44 -0700588 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100589}
590EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
591
592/**
593 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
594 * @gpio: the GPIO pin number from the GPIO subsystem number space
595 *
596 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
597 * as part of their gpio_free() semantics, platforms and individual drivers
598 * shall *NOT* request GPIO pins to be muxed out.
599 */
600void pinctrl_free_gpio(unsigned gpio)
601{
602 struct pinctrl_dev *pctldev;
603 struct pinctrl_gpio_range *range;
604 int ret;
605 int pin;
606
607 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700608 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100609 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700610 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200611 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100612
613 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200614 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100615
Stephen Warren57b676f2012-03-02 13:05:44 -0700616 pinmux_free_gpio(pctldev, pin, range);
617
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200618 mutex_unlock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100619}
620EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
621
622static int pinctrl_gpio_direction(unsigned gpio, bool input)
623{
624 struct pinctrl_dev *pctldev;
625 struct pinctrl_gpio_range *range;
626 int ret;
627 int pin;
628
629 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200630 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100631 return ret;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200632 }
633
634 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100635
636 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200637 pin = gpio_to_pin(range, gpio);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200638 ret = pinmux_gpio_direction(pctldev, range, pin, input);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100639
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200640 mutex_unlock(&pctldev->mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200641
642 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100643}
644
645/**
646 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
647 * @gpio: the GPIO pin number from the GPIO subsystem number space
648 *
649 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
650 * as part of their gpio_direction_input() semantics, platforms and individual
651 * drivers shall *NOT* touch pin control GPIO calls.
652 */
653int pinctrl_gpio_direction_input(unsigned gpio)
654{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200655 return pinctrl_gpio_direction(gpio, true);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100656}
657EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
658
659/**
660 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
661 * @gpio: the GPIO pin number from the GPIO subsystem number space
662 *
663 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
664 * as part of their gpio_direction_output() semantics, platforms and individual
665 * drivers shall *NOT* touch pin control GPIO calls.
666 */
667int pinctrl_gpio_direction_output(unsigned gpio)
668{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200669 return pinctrl_gpio_direction(gpio, false);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100670}
671EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
672
Stephen Warren6e5e9592012-03-02 13:05:47 -0700673static struct pinctrl_state *find_state(struct pinctrl *p,
674 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100675{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700676 struct pinctrl_state *state;
677
678 list_for_each_entry(state, &p->states, node)
679 if (!strcmp(state->name, name))
680 return state;
681
682 return NULL;
683}
684
685static struct pinctrl_state *create_state(struct pinctrl *p,
686 const char *name)
687{
688 struct pinctrl_state *state;
689
690 state = kzalloc(sizeof(*state), GFP_KERNEL);
691 if (state == NULL) {
692 dev_err(p->dev,
693 "failed to alloc struct pinctrl_state\n");
694 return ERR_PTR(-ENOMEM);
695 }
696
697 state->name = name;
698 INIT_LIST_HEAD(&state->settings);
699
700 list_add_tail(&state->node, &p->states);
701
702 return state;
703}
704
705static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
706{
707 struct pinctrl_state *state;
708 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700709 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700710
711 state = find_state(p, map->name);
712 if (!state)
713 state = create_state(p, map->name);
714 if (IS_ERR(state))
715 return PTR_ERR(state);
716
Stephen Warren1e2082b2012-03-02 13:05:48 -0700717 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
718 return 0;
719
Stephen Warren6e5e9592012-03-02 13:05:47 -0700720 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
721 if (setting == NULL) {
722 dev_err(p->dev,
723 "failed to alloc struct pinctrl_setting\n");
724 return -ENOMEM;
725 }
726
Stephen Warren1e2082b2012-03-02 13:05:48 -0700727 setting->type = map->type;
728
Stephen Warren6e5e9592012-03-02 13:05:47 -0700729 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
730 if (setting->pctldev == NULL) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700731 kfree(setting);
Linus Walleij89216492012-12-11 14:14:32 +0100732 /* Do not defer probing of hogs (circular loop) */
733 if (!strcmp(map->ctrl_dev_name, map->dev_name))
734 return -ENODEV;
Linus Walleijc05127c2012-04-10 10:00:38 +0200735 /*
736 * OK let us guess that the driver is not there yet, and
737 * let's defer obtaining this pinctrl handle to later...
738 */
Linus Walleij89216492012-12-11 14:14:32 +0100739 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
740 map->ctrl_dev_name);
Linus Walleijc05127c2012-04-10 10:00:38 +0200741 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700742 }
743
Linus Walleij1a789582012-10-17 20:51:54 +0200744 setting->dev_name = map->dev_name;
745
Stephen Warren1e2082b2012-03-02 13:05:48 -0700746 switch (map->type) {
747 case PIN_MAP_TYPE_MUX_GROUP:
748 ret = pinmux_map_to_setting(map, setting);
749 break;
750 case PIN_MAP_TYPE_CONFIGS_PIN:
751 case PIN_MAP_TYPE_CONFIGS_GROUP:
752 ret = pinconf_map_to_setting(map, setting);
753 break;
754 default:
755 ret = -EINVAL;
756 break;
757 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700758 if (ret < 0) {
759 kfree(setting);
760 return ret;
761 }
762
763 list_add_tail(&setting->node, &state->settings);
764
765 return 0;
766}
767
768static struct pinctrl *find_pinctrl(struct device *dev)
769{
770 struct pinctrl *p;
771
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200772 mutex_lock(&pinctrl_list_mutex);
Stephen Warren1e2082b2012-03-02 13:05:48 -0700773 list_for_each_entry(p, &pinctrl_list, node)
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200774 if (p->dev == dev) {
775 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700776 return p;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200777 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700778
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200779 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700780 return NULL;
781}
782
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200783static void pinctrl_free(struct pinctrl *p, bool inlist);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700784
785static struct pinctrl *create_pinctrl(struct device *dev)
786{
787 struct pinctrl *p;
788 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700789 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100790 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700791 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700792 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100793
794 /*
795 * create the state cookie holder struct pinctrl for each
796 * mapping, this is what consumers will get when requesting
797 * a pin control handle with pinctrl_get()
798 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700799 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700800 if (p == NULL) {
801 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100802 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700803 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700804 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700805 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -0600806 INIT_LIST_HEAD(&p->dt_maps);
807
808 ret = pinctrl_dt_to_map(p);
809 if (ret < 0) {
810 kfree(p);
811 return ERR_PTR(ret);
812 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700813
814 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100815
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200816 mutex_lock(&pinctrl_maps_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100817 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700818 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -0700819 /* Map must be for this device */
820 if (strcmp(map->dev_name, devname))
821 continue;
822
Stephen Warren6e5e9592012-03-02 13:05:47 -0700823 ret = add_setting(p, map);
Linus Walleij89216492012-12-11 14:14:32 +0100824 /*
825 * At this point the adding of a setting may:
826 *
827 * - Defer, if the pinctrl device is not yet available
828 * - Fail, if the pinctrl device is not yet available,
829 * AND the setting is a hog. We cannot defer that, since
830 * the hog will kick in immediately after the device
831 * is registered.
832 *
833 * If the error returned was not -EPROBE_DEFER then we
834 * accumulate the errors to see if we end up with
835 * an -EPROBE_DEFER later, as that is the worst case.
836 */
837 if (ret == -EPROBE_DEFER) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200838 pinctrl_free(p, false);
839 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700840 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100841 }
842 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200843 mutex_unlock(&pinctrl_maps_mutex);
844
Linus Walleij89216492012-12-11 14:14:32 +0100845 if (ret < 0) {
846 /* If some other error than deferral occured, return here */
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200847 pinctrl_free(p, false);
Linus Walleij89216492012-12-11 14:14:32 +0100848 return ERR_PTR(ret);
849 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100850
Linus Walleijab780292013-01-22 10:56:14 -0700851 kref_init(&p->users);
852
Linus Walleijb0666ba2012-12-11 13:12:35 +0100853 /* Add the pinctrl handle to the global list */
Stephen Warren8b9c1392012-02-19 23:45:42 -0700854 list_add_tail(&p->node, &pinctrl_list);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100855
856 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700857}
Stephen Warren7ecdb162012-03-02 13:05:45 -0700858
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200859/**
860 * pinctrl_get() - retrieves the pinctrl handle for a device
861 * @dev: the device to obtain the handle for
862 */
863struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700864{
865 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700866
Stephen Warren6e5e9592012-03-02 13:05:47 -0700867 if (WARN_ON(!dev))
868 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700869
Linus Walleijab780292013-01-22 10:56:14 -0700870 /*
871 * See if somebody else (such as the device core) has already
872 * obtained a handle to the pinctrl for this device. In that case,
873 * return another pointer to it.
874 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700875 p = find_pinctrl(dev);
Linus Walleijab780292013-01-22 10:56:14 -0700876 if (p != NULL) {
877 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
878 kref_get(&p->users);
879 return p;
880 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700881
Richard Genoudd599bfb2012-08-10 16:53:49 +0200882 return create_pinctrl(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100883}
884EXPORT_SYMBOL_GPL(pinctrl_get);
885
Richard Genoudd3cee8302013-03-25 15:47:21 +0100886static void pinctrl_free_setting(bool disable_setting,
887 struct pinctrl_setting *setting)
888{
889 switch (setting->type) {
890 case PIN_MAP_TYPE_MUX_GROUP:
891 if (disable_setting)
892 pinmux_disable_setting(setting);
893 pinmux_free_setting(setting);
894 break;
895 case PIN_MAP_TYPE_CONFIGS_PIN:
896 case PIN_MAP_TYPE_CONFIGS_GROUP:
897 pinconf_free_setting(setting);
898 break;
899 default:
900 break;
901 }
902}
903
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200904static void pinctrl_free(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -0700905{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700906 struct pinctrl_state *state, *n1;
907 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700908
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200909 mutex_lock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700910 list_for_each_entry_safe(state, n1, &p->states, node) {
911 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Richard Genoudd3cee8302013-03-25 15:47:21 +0100912 pinctrl_free_setting(state == p->state, setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700913 list_del(&setting->node);
914 kfree(setting);
915 }
916 list_del(&state->node);
917 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700918 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700919
Stephen Warren57291ce2012-03-23 10:29:46 -0600920 pinctrl_dt_free_maps(p);
921
Stephen Warren6e5e9592012-03-02 13:05:47 -0700922 if (inlist)
923 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700924 kfree(p);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200925 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700926}
927
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100928/**
Linus Walleijab780292013-01-22 10:56:14 -0700929 * pinctrl_release() - release the pinctrl handle
930 * @kref: the kref in the pinctrl being released
931 */
Sachin Kamat2917e832013-01-24 15:01:00 +0530932static void pinctrl_release(struct kref *kref)
Linus Walleijab780292013-01-22 10:56:14 -0700933{
934 struct pinctrl *p = container_of(kref, struct pinctrl, users);
935
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200936 pinctrl_free(p, true);
Linus Walleijab780292013-01-22 10:56:14 -0700937}
938
939/**
940 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
Stephen Warren6e5e9592012-03-02 13:05:47 -0700941 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100942 */
943void pinctrl_put(struct pinctrl *p)
944{
Linus Walleijab780292013-01-22 10:56:14 -0700945 kref_put(&p->users, pinctrl_release);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100946}
947EXPORT_SYMBOL_GPL(pinctrl_put);
948
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200949/**
950 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
951 * @p: the pinctrl handle to retrieve the state from
952 * @name: the state name to retrieve
953 */
954struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
955 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -0700956{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700957 struct pinctrl_state *state;
958
959 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800960 if (!state) {
961 if (pinctrl_dummy_state) {
962 /* create dummy state */
963 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
964 name);
965 state = create_state(p, name);
Richard Genoudd599bfb2012-08-10 16:53:49 +0200966 } else
967 state = ERR_PTR(-ENODEV);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800968 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700969
970 return state;
971}
Stephen Warren6e5e9592012-03-02 13:05:47 -0700972EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
973
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200974/**
975 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
976 * @p: the pinctrl handle for the device that requests configuration
977 * @state: the state handle to select/activate/program
978 */
979int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700980{
981 struct pinctrl_setting *setting, *setting2;
Richard Genoud50cf7c82013-03-25 15:47:23 +0100982 struct pinctrl_state *old_state = p->state;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700983 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700984
Stephen Warren6e5e9592012-03-02 13:05:47 -0700985 if (p->state == state)
986 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700987
Stephen Warren6e5e9592012-03-02 13:05:47 -0700988 if (p->state) {
989 /*
990 * The set of groups with a mux configuration in the old state
991 * may not be identical to the set of groups with a mux setting
992 * in the new state. While this might be unusual, it's entirely
993 * possible for the "user"-supplied mapping table to be written
994 * that way. For each group that was configured in the old state
995 * but not in the new state, this code puts that group into a
996 * safe/disabled state.
997 */
998 list_for_each_entry(setting, &p->state->settings, node) {
999 bool found = false;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001000 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
1001 continue;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001002 list_for_each_entry(setting2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001003 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
1004 continue;
1005 if (setting2->data.mux.group ==
1006 setting->data.mux.group) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001007 found = true;
1008 break;
1009 }
Stephen Warren7ecdb162012-03-02 13:05:45 -07001010 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001011 if (!found)
1012 pinmux_disable_setting(setting);
1013 }
1014 }
1015
Richard Genoud3102a762013-03-25 15:47:22 +01001016 p->state = NULL;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001017
1018 /* Apply all the settings for the new state */
1019 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001020 switch (setting->type) {
1021 case PIN_MAP_TYPE_MUX_GROUP:
1022 ret = pinmux_enable_setting(setting);
1023 break;
1024 case PIN_MAP_TYPE_CONFIGS_PIN:
1025 case PIN_MAP_TYPE_CONFIGS_GROUP:
1026 ret = pinconf_apply_setting(setting);
1027 break;
1028 default:
1029 ret = -EINVAL;
1030 break;
1031 }
Richard Genoud3102a762013-03-25 15:47:22 +01001032
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001033 if (ret < 0) {
Richard Genoud3102a762013-03-25 15:47:22 +01001034 goto unapply_new_state;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001035 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001036 }
1037
Richard Genoud3102a762013-03-25 15:47:22 +01001038 p->state = state;
1039
Stephen Warren7ecdb162012-03-02 13:05:45 -07001040 return 0;
Richard Genoud3102a762013-03-25 15:47:22 +01001041
1042unapply_new_state:
Richard Genoudda587512013-03-28 12:55:46 +01001043 dev_err(p->dev, "Error applying setting, reverse things back\n");
Richard Genoud3102a762013-03-25 15:47:22 +01001044
Richard Genoud3102a762013-03-25 15:47:22 +01001045 list_for_each_entry(setting2, &state->settings, node) {
1046 if (&setting2->node == &setting->node)
1047 break;
Richard Genoudaf606172013-03-29 10:03:26 +01001048 /*
1049 * All we can do here is pinmux_disable_setting.
1050 * That means that some pins are muxed differently now
1051 * than they were before applying the setting (We can't
1052 * "unmux a pin"!), but it's not a big deal since the pins
1053 * are free to be muxed by another apply_setting.
1054 */
1055 if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
1056 pinmux_disable_setting(setting2);
Richard Genoud3102a762013-03-25 15:47:22 +01001057 }
Richard Genoud8009d5f2013-03-28 12:55:47 +01001058
Richard Genoud385d9422013-03-29 10:03:27 +01001059 /* There's no infinite recursive loop here because p->state is NULL */
1060 if (old_state)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001061 pinctrl_select_state(p, old_state);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001062
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001063 return ret;
1064}
Stephen Warren6e5e9592012-03-02 13:05:47 -07001065EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001066
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001067static void devm_pinctrl_release(struct device *dev, void *res)
1068{
1069 pinctrl_put(*(struct pinctrl **)res);
1070}
1071
1072/**
1073 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
1074 * @dev: the device to obtain the handle for
1075 *
1076 * If there is a need to explicitly destroy the returned struct pinctrl,
1077 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1078 */
1079struct pinctrl *devm_pinctrl_get(struct device *dev)
1080{
1081 struct pinctrl **ptr, *p;
1082
1083 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
1084 if (!ptr)
1085 return ERR_PTR(-ENOMEM);
1086
1087 p = pinctrl_get(dev);
1088 if (!IS_ERR(p)) {
1089 *ptr = p;
1090 devres_add(dev, ptr);
1091 } else {
1092 devres_free(ptr);
1093 }
1094
1095 return p;
1096}
1097EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1098
1099static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1100{
1101 struct pinctrl **p = res;
1102
1103 return *p == data;
1104}
1105
1106/**
1107 * devm_pinctrl_put() - Resource managed pinctrl_put()
1108 * @p: the pinctrl handle to release
1109 *
1110 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1111 * this function will not need to be called and the resource management
1112 * code will ensure that the resource is freed.
1113 */
1114void devm_pinctrl_put(struct pinctrl *p)
1115{
Jingoo Hana72149e2013-02-26 11:34:07 +09001116 WARN_ON(devres_release(p->dev, devm_pinctrl_release,
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001117 devm_pinctrl_match, p));
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001118}
1119EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1120
Stephen Warren57291ce2012-03-23 10:29:46 -06001121int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
1122 bool dup, bool locked)
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001123{
Stephen Warren1e2082b2012-03-02 13:05:48 -07001124 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001125 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001126
1127 pr_debug("add %d pinmux maps\n", num_maps);
1128
1129 /* First sanity check the new mapping */
1130 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001131 if (!maps[i].dev_name) {
1132 pr_err("failed to register map %s (%d): no device given\n",
1133 maps[i].name, i);
1134 return -EINVAL;
1135 }
1136
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001137 if (!maps[i].name) {
1138 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001139 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001140 return -EINVAL;
1141 }
1142
Stephen Warren1e2082b2012-03-02 13:05:48 -07001143 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1144 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001145 pr_err("failed to register map %s (%d): no pin control device given\n",
1146 maps[i].name, i);
1147 return -EINVAL;
1148 }
1149
Stephen Warren1e2082b2012-03-02 13:05:48 -07001150 switch (maps[i].type) {
1151 case PIN_MAP_TYPE_DUMMY_STATE:
1152 break;
1153 case PIN_MAP_TYPE_MUX_GROUP:
1154 ret = pinmux_validate_map(&maps[i], i);
1155 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001156 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001157 break;
1158 case PIN_MAP_TYPE_CONFIGS_PIN:
1159 case PIN_MAP_TYPE_CONFIGS_GROUP:
1160 ret = pinconf_validate_map(&maps[i], i);
1161 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001162 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001163 break;
1164 default:
1165 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001166 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -07001167 return -EINVAL;
1168 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001169 }
1170
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001171 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
1172 if (!maps_node) {
1173 pr_err("failed to alloc struct pinctrl_maps\n");
1174 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001175 }
1176
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001177 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -06001178 if (dup) {
1179 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
1180 GFP_KERNEL);
1181 if (!maps_node->maps) {
1182 pr_err("failed to duplicate mapping table\n");
1183 kfree(maps_node);
1184 return -ENOMEM;
1185 }
1186 } else {
1187 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001188 }
1189
Stephen Warren57291ce2012-03-23 10:29:46 -06001190 if (!locked)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001191 mutex_lock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001192 list_add_tail(&maps_node->node, &pinctrl_maps);
Stephen Warren57291ce2012-03-23 10:29:46 -06001193 if (!locked)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001194 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001195
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001196 return 0;
1197}
1198
Stephen Warren57291ce2012-03-23 10:29:46 -06001199/**
1200 * pinctrl_register_mappings() - register a set of pin controller mappings
1201 * @maps: the pincontrol mappings table to register. This should probably be
1202 * marked with __initdata so it can be discarded after boot. This
1203 * function will perform a shallow copy for the mapping entries.
1204 * @num_maps: the number of maps in the mapping table
1205 */
1206int pinctrl_register_mappings(struct pinctrl_map const *maps,
1207 unsigned num_maps)
1208{
1209 return pinctrl_register_map(maps, num_maps, true, false);
1210}
1211
1212void pinctrl_unregister_map(struct pinctrl_map const *map)
1213{
1214 struct pinctrl_maps *maps_node;
1215
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001216 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001217 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1218 if (maps_node->maps == map) {
1219 list_del(&maps_node->node);
Linus Walleijdb6c2c62013-07-23 01:43:20 +02001220 kfree(maps_node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001221 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001222 return;
1223 }
1224 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001225 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001226}
1227
Julien Delacou840a47b2012-12-10 14:47:33 +01001228/**
1229 * pinctrl_force_sleep() - turn a given controller device into sleep state
1230 * @pctldev: pin controller device
1231 */
1232int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1233{
1234 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
1235 return pinctrl_select_state(pctldev->p, pctldev->hog_sleep);
1236 return 0;
1237}
1238EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1239
1240/**
1241 * pinctrl_force_default() - turn a given controller device into default state
1242 * @pctldev: pin controller device
1243 */
1244int pinctrl_force_default(struct pinctrl_dev *pctldev)
1245{
1246 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
1247 return pinctrl_select_state(pctldev->p, pctldev->hog_default);
1248 return 0;
1249}
1250EXPORT_SYMBOL_GPL(pinctrl_force_default);
1251
Linus Walleij14005ee2013-06-05 15:30:33 +02001252#ifdef CONFIG_PM
1253
1254/**
Tony Lindgrenf3333492013-07-18 08:15:04 -07001255 * pinctrl_pm_select_state() - select pinctrl state for PM
1256 * @dev: device to select default state for
1257 * @state: state to set
1258 */
1259static int pinctrl_pm_select_state(struct device *dev,
1260 struct pinctrl_state *state)
1261{
1262 struct dev_pin_info *pins = dev->pins;
1263 int ret;
1264
1265 if (IS_ERR(state))
1266 return 0; /* No such state */
1267 ret = pinctrl_select_state(pins->p, state);
1268 if (ret)
1269 dev_err(dev, "failed to activate pinctrl state %s\n",
1270 state->name);
1271 return ret;
1272}
1273
1274/**
Linus Walleij14005ee2013-06-05 15:30:33 +02001275 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1276 * @dev: device to select default state for
1277 */
1278int pinctrl_pm_select_default_state(struct device *dev)
1279{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001280 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001281 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001282
1283 return pinctrl_pm_select_state(dev, dev->pins->default_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001284}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001285EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001286
1287/**
1288 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1289 * @dev: device to select sleep state for
1290 */
1291int pinctrl_pm_select_sleep_state(struct device *dev)
1292{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001293 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001294 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001295
1296 return pinctrl_pm_select_state(dev, dev->pins->sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001297}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001298EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001299
1300/**
1301 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1302 * @dev: device to select idle state for
1303 */
1304int pinctrl_pm_select_idle_state(struct device *dev)
1305{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001306 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001307 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001308
1309 return pinctrl_pm_select_state(dev, dev->pins->idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001310}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001311EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001312#endif
1313
Linus Walleij2744e8a2011-05-02 20:50:54 +02001314#ifdef CONFIG_DEBUG_FS
1315
1316static int pinctrl_pins_show(struct seq_file *s, void *what)
1317{
1318 struct pinctrl_dev *pctldev = s->private;
1319 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001320 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001321
1322 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001323
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001324 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001325
Chanho Park706e8522012-01-03 16:47:50 +09001326 /* The pin number can be retrived from the pin controller descriptor */
1327 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001328 struct pin_desc *desc;
1329
Chanho Park706e8522012-01-03 16:47:50 +09001330 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001331 desc = pin_desc_get(pctldev, pin);
1332 /* Pin space may be sparse */
1333 if (desc == NULL)
1334 continue;
1335
1336 seq_printf(s, "pin %d (%s) ", pin,
1337 desc->name ? desc->name : "unnamed");
1338
1339 /* Driver-specific info per pin */
1340 if (ops->pin_dbg_show)
1341 ops->pin_dbg_show(pctldev, s, pin);
1342
1343 seq_puts(s, "\n");
1344 }
1345
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001346 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001347
Linus Walleij2744e8a2011-05-02 20:50:54 +02001348 return 0;
1349}
1350
1351static int pinctrl_groups_show(struct seq_file *s, void *what)
1352{
1353 struct pinctrl_dev *pctldev = s->private;
1354 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301355 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001356
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001357 mutex_lock(&pctldev->mutex);
1358
Viresh Kumard1e90e92012-03-30 11:25:40 +05301359 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001360
Linus Walleij2744e8a2011-05-02 20:50:54 +02001361 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301362 while (selector < ngroups) {
Stephen Warrena5818a82011-10-19 16:19:25 -06001363 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001364 unsigned num_pins;
1365 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001366 const char *pname;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001367 int ret;
1368 int i;
1369
1370 ret = ops->get_group_pins(pctldev, selector,
1371 &pins, &num_pins);
1372 if (ret)
1373 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1374 gname);
1375 else {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001376 seq_printf(s, "group: %s\n", gname);
1377 for (i = 0; i < num_pins; i++) {
1378 pname = pin_get_name(pctldev, pins[i]);
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001379 if (WARN_ON(!pname)) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001380 mutex_unlock(&pctldev->mutex);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001381 return -EINVAL;
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001382 }
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001383 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1384 }
1385 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001386 }
1387 selector++;
1388 }
1389
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001390 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001391
1392 return 0;
1393}
1394
1395static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1396{
1397 struct pinctrl_dev *pctldev = s->private;
1398 struct pinctrl_gpio_range *range = NULL;
1399
1400 seq_puts(s, "GPIO ranges handled:\n");
1401
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001402 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001403
Linus Walleij2744e8a2011-05-02 20:50:54 +02001404 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001405 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Christian Ruppertc8587ee2013-06-13 14:55:31 +02001406 if (range->pins) {
1407 int a;
1408 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
1409 range->id, range->name,
1410 range->base, (range->base + range->npins - 1));
1411 for (a = 0; a < range->npins - 1; a++)
1412 seq_printf(s, "%u, ", range->pins[a]);
1413 seq_printf(s, "%u}\n", range->pins[a]);
1414 }
1415 else
1416 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1417 range->id, range->name,
1418 range->base, (range->base + range->npins - 1),
1419 range->pin_base,
1420 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001421 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001422
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001423 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001424
1425 return 0;
1426}
1427
1428static int pinctrl_devices_show(struct seq_file *s, void *what)
1429{
1430 struct pinctrl_dev *pctldev;
1431
Linus Walleijae6b4d82011-10-19 18:14:33 +02001432 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001433
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001434 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001435
Linus Walleij2744e8a2011-05-02 20:50:54 +02001436 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1437 seq_printf(s, "%s ", pctldev->desc->name);
1438 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001439 seq_puts(s, "yes ");
1440 else
1441 seq_puts(s, "no ");
1442 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001443 seq_puts(s, "yes");
1444 else
1445 seq_puts(s, "no");
1446 seq_puts(s, "\n");
1447 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001448
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001449 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001450
1451 return 0;
1452}
1453
Stephen Warren1e2082b2012-03-02 13:05:48 -07001454static inline const char *map_type(enum pinctrl_map_type type)
1455{
1456 static const char * const names[] = {
1457 "INVALID",
1458 "DUMMY_STATE",
1459 "MUX_GROUP",
1460 "CONFIGS_PIN",
1461 "CONFIGS_GROUP",
1462 };
1463
1464 if (type >= ARRAY_SIZE(names))
1465 return "UNKNOWN";
1466
1467 return names[type];
1468}
1469
Stephen Warren3eedb432012-02-23 17:04:40 -07001470static int pinctrl_maps_show(struct seq_file *s, void *what)
1471{
1472 struct pinctrl_maps *maps_node;
1473 int i;
1474 struct pinctrl_map const *map;
1475
1476 seq_puts(s, "Pinctrl maps:\n");
1477
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001478 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001479 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001480 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1481 map->dev_name, map->name, map_type(map->type),
1482 map->type);
1483
1484 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1485 seq_printf(s, "controlling device %s\n",
1486 map->ctrl_dev_name);
1487
1488 switch (map->type) {
1489 case PIN_MAP_TYPE_MUX_GROUP:
1490 pinmux_show_map(s, map);
1491 break;
1492 case PIN_MAP_TYPE_CONFIGS_PIN:
1493 case PIN_MAP_TYPE_CONFIGS_GROUP:
1494 pinconf_show_map(s, map);
1495 break;
1496 default:
1497 break;
1498 }
1499
1500 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001501 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001502 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001503
1504 return 0;
1505}
1506
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001507static int pinctrl_show(struct seq_file *s, void *what)
1508{
1509 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001510 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001511 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001512
1513 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001514
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001515 mutex_lock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001516
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001517 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001518 seq_printf(s, "device: %s current state: %s\n",
1519 dev_name(p->dev),
1520 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001521
Stephen Warren6e5e9592012-03-02 13:05:47 -07001522 list_for_each_entry(state, &p->states, node) {
1523 seq_printf(s, " state: %s\n", state->name);
1524
1525 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001526 struct pinctrl_dev *pctldev = setting->pctldev;
1527
1528 seq_printf(s, " type: %s controller %s ",
1529 map_type(setting->type),
1530 pinctrl_dev_get_name(pctldev));
1531
1532 switch (setting->type) {
1533 case PIN_MAP_TYPE_MUX_GROUP:
1534 pinmux_show_setting(s, setting);
1535 break;
1536 case PIN_MAP_TYPE_CONFIGS_PIN:
1537 case PIN_MAP_TYPE_CONFIGS_GROUP:
1538 pinconf_show_setting(s, setting);
1539 break;
1540 default:
1541 break;
1542 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001543 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001544 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001545 }
1546
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001547 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001548
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001549 return 0;
1550}
1551
Linus Walleij2744e8a2011-05-02 20:50:54 +02001552static int pinctrl_pins_open(struct inode *inode, struct file *file)
1553{
1554 return single_open(file, pinctrl_pins_show, inode->i_private);
1555}
1556
1557static int pinctrl_groups_open(struct inode *inode, struct file *file)
1558{
1559 return single_open(file, pinctrl_groups_show, inode->i_private);
1560}
1561
1562static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1563{
1564 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1565}
1566
1567static int pinctrl_devices_open(struct inode *inode, struct file *file)
1568{
1569 return single_open(file, pinctrl_devices_show, NULL);
1570}
1571
Stephen Warren3eedb432012-02-23 17:04:40 -07001572static int pinctrl_maps_open(struct inode *inode, struct file *file)
1573{
1574 return single_open(file, pinctrl_maps_show, NULL);
1575}
1576
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001577static int pinctrl_open(struct inode *inode, struct file *file)
1578{
1579 return single_open(file, pinctrl_show, NULL);
1580}
1581
Linus Walleij2744e8a2011-05-02 20:50:54 +02001582static const struct file_operations pinctrl_pins_ops = {
1583 .open = pinctrl_pins_open,
1584 .read = seq_read,
1585 .llseek = seq_lseek,
1586 .release = single_release,
1587};
1588
1589static const struct file_operations pinctrl_groups_ops = {
1590 .open = pinctrl_groups_open,
1591 .read = seq_read,
1592 .llseek = seq_lseek,
1593 .release = single_release,
1594};
1595
1596static const struct file_operations pinctrl_gpioranges_ops = {
1597 .open = pinctrl_gpioranges_open,
1598 .read = seq_read,
1599 .llseek = seq_lseek,
1600 .release = single_release,
1601};
1602
1603static const struct file_operations pinctrl_devices_ops = {
1604 .open = pinctrl_devices_open,
1605 .read = seq_read,
1606 .llseek = seq_lseek,
1607 .release = single_release,
1608};
1609
Stephen Warren3eedb432012-02-23 17:04:40 -07001610static const struct file_operations pinctrl_maps_ops = {
1611 .open = pinctrl_maps_open,
1612 .read = seq_read,
1613 .llseek = seq_lseek,
1614 .release = single_release,
1615};
1616
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001617static const struct file_operations pinctrl_ops = {
1618 .open = pinctrl_open,
1619 .read = seq_read,
1620 .llseek = seq_lseek,
1621 .release = single_release,
1622};
1623
Linus Walleij2744e8a2011-05-02 20:50:54 +02001624static struct dentry *debugfs_root;
1625
1626static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1627{
Tony Lindgren02157162012-01-20 08:17:22 -08001628 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001629
Stephen Warren51cd24e2011-12-09 16:59:05 -07001630 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001631 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001632 pctldev->device_root = device_root;
1633
Linus Walleij2744e8a2011-05-02 20:50:54 +02001634 if (IS_ERR(device_root) || !device_root) {
1635 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001636 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001637 return;
1638 }
1639 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1640 device_root, pctldev, &pinctrl_pins_ops);
1641 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1642 device_root, pctldev, &pinctrl_groups_ops);
1643 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1644 device_root, pctldev, &pinctrl_gpioranges_ops);
1645 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +02001646 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001647}
1648
Tony Lindgren02157162012-01-20 08:17:22 -08001649static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1650{
1651 debugfs_remove_recursive(pctldev->device_root);
1652}
1653
Linus Walleij2744e8a2011-05-02 20:50:54 +02001654static void pinctrl_init_debugfs(void)
1655{
1656 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1657 if (IS_ERR(debugfs_root) || !debugfs_root) {
1658 pr_warn("failed to create debugfs directory\n");
1659 debugfs_root = NULL;
1660 return;
1661 }
1662
1663 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1664 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001665 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1666 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001667 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1668 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001669}
1670
1671#else /* CONFIG_DEBUG_FS */
1672
1673static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1674{
1675}
1676
1677static void pinctrl_init_debugfs(void)
1678{
1679}
1680
Tony Lindgren02157162012-01-20 08:17:22 -08001681static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1682{
1683}
1684
Linus Walleij2744e8a2011-05-02 20:50:54 +02001685#endif
1686
Stephen Warrend26bc492012-03-16 14:54:25 -06001687static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1688{
1689 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1690
1691 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301692 !ops->get_groups_count ||
Stephen Warrend26bc492012-03-16 14:54:25 -06001693 !ops->get_group_name ||
1694 !ops->get_group_pins)
1695 return -EINVAL;
1696
Stephen Warren57291ce2012-03-23 10:29:46 -06001697 if (ops->dt_node_to_map && !ops->dt_free_map)
1698 return -EINVAL;
1699
Stephen Warrend26bc492012-03-16 14:54:25 -06001700 return 0;
1701}
1702
Linus Walleij2744e8a2011-05-02 20:50:54 +02001703/**
1704 * pinctrl_register() - register a pin controller device
1705 * @pctldesc: descriptor for this pin controller
1706 * @dev: parent device for this pin controller
1707 * @driver_data: private pin controller data for this pin controller
1708 */
1709struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1710 struct device *dev, void *driver_data)
1711{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001712 struct pinctrl_dev *pctldev;
1713 int ret;
1714
Devendra Nagada9aecb2012-06-16 23:43:16 +05301715 if (!pctldesc)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001716 return NULL;
Devendra Nagada9aecb2012-06-16 23:43:16 +05301717 if (!pctldesc->name)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001718 return NULL;
1719
Stephen Warren02f5b982012-02-22 14:26:00 -07001720 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001721 if (pctldev == NULL) {
1722 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001723 return NULL;
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001724 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001725
1726 /* Initialize pin control device struct */
1727 pctldev->owner = pctldesc->owner;
1728 pctldev->desc = pctldesc;
1729 pctldev->driver_data = driver_data;
1730 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001731 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001732 pctldev->dev = dev;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001733 mutex_init(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001734
Stephen Warrend26bc492012-03-16 14:54:25 -06001735 /* check core ops for sanity */
Devendra Nagada9aecb2012-06-16 23:43:16 +05301736 if (pinctrl_check_ops(pctldev)) {
John Crispinad6e1102012-04-26 16:47:11 +02001737 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06001738 goto out_err;
1739 }
1740
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001741 /* If we're implementing pinmuxing, check the ops for sanity */
1742 if (pctldesc->pmxops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301743 if (pinmux_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001744 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001745 }
1746
1747 /* If we're implementing pinconfig, check the ops for sanity */
1748 if (pctldesc->confops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301749 if (pinconf_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001750 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001751 }
1752
Linus Walleij2744e8a2011-05-02 20:50:54 +02001753 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02001754 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001755 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1756 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001757 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001758 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1759 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001760 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001761 }
1762
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001763 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -07001764 list_add_tail(&pctldev->node, &pinctrldev_list);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001765 mutex_unlock(&pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001766
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001767 pctldev->p = pinctrl_get(pctldev->dev);
1768
Stephen Warren6e5e9592012-03-02 13:05:47 -07001769 if (!IS_ERR(pctldev->p)) {
Julien Delacou840a47b2012-12-10 14:47:33 +01001770 pctldev->hog_default =
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001771 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
Julien Delacou840a47b2012-12-10 14:47:33 +01001772 if (IS_ERR(pctldev->hog_default)) {
John Crispinad6e1102012-04-26 16:47:11 +02001773 dev_dbg(dev, "failed to lookup the default state\n");
1774 } else {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001775 if (pinctrl_select_state(pctldev->p,
Julien Delacou840a47b2012-12-10 14:47:33 +01001776 pctldev->hog_default))
John Crispinad6e1102012-04-26 16:47:11 +02001777 dev_err(dev,
1778 "failed to select default state\n");
John Crispinad6e1102012-04-26 16:47:11 +02001779 }
Julien Delacou840a47b2012-12-10 14:47:33 +01001780
1781 pctldev->hog_sleep =
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001782 pinctrl_lookup_state(pctldev->p,
Julien Delacou840a47b2012-12-10 14:47:33 +01001783 PINCTRL_STATE_SLEEP);
1784 if (IS_ERR(pctldev->hog_sleep))
1785 dev_dbg(dev, "failed to lookup the sleep state\n");
Stephen Warren6e5e9592012-03-02 13:05:47 -07001786 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001787
Stephen Warren2304b472012-02-22 14:26:01 -07001788 pinctrl_init_device_debugfs(pctldev);
1789
Linus Walleij2744e8a2011-05-02 20:50:54 +02001790 return pctldev;
1791
Stephen Warren51cd24e2011-12-09 16:59:05 -07001792out_err:
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001793 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001794 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001795 return NULL;
1796}
1797EXPORT_SYMBOL_GPL(pinctrl_register);
1798
1799/**
1800 * pinctrl_unregister() - unregister pinmux
1801 * @pctldev: pin controller to unregister
1802 *
1803 * Called by pinmux drivers to unregister a pinmux.
1804 */
1805void pinctrl_unregister(struct pinctrl_dev *pctldev)
1806{
Dong Aisheng5d589b02012-05-23 21:22:40 +08001807 struct pinctrl_gpio_range *range, *n;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001808 if (pctldev == NULL)
1809 return;
1810
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001811 mutex_lock(&pinctrldev_list_mutex);
1812 mutex_lock(&pctldev->mutex);
1813
Tony Lindgren02157162012-01-20 08:17:22 -08001814 pinctrl_remove_device_debugfs(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001815
Stephen Warren6e5e9592012-03-02 13:05:47 -07001816 if (!IS_ERR(pctldev->p))
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001817 pinctrl_put(pctldev->p);
Stephen Warren57b676f2012-03-02 13:05:44 -07001818
Linus Walleij2744e8a2011-05-02 20:50:54 +02001819 /* TODO: check that no pinmuxes are still active? */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001820 list_del(&pctldev->node);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001821 /* Destroy descriptor tree */
1822 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1823 pctldev->desc->npins);
Dong Aisheng5d589b02012-05-23 21:22:40 +08001824 /* remove gpio ranges map */
1825 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1826 list_del(&range->node);
1827
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001828 mutex_unlock(&pctldev->mutex);
1829 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001830 kfree(pctldev);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001831 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001832}
1833EXPORT_SYMBOL_GPL(pinctrl_unregister);
1834
1835static int __init pinctrl_init(void)
1836{
1837 pr_info("initialized pinctrl subsystem\n");
1838 pinctrl_init_debugfs();
1839 return 0;
1840}
1841
1842/* init early since many drivers really need to initialized pinmux early */
1843core_initcall(pinctrl_init);