blob: 7094a812900880c7224846ae471150adbecff902 [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
Linus Walleij2744e8a2011-05-02 20:50:54 +0200465/**
Linus Walleij9afbefb2012-11-20 14:25:07 +0100466 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
467 * @pctldev: the pin controller device to look in
468 * @pin: a controller-local number to find the range for
469 */
470struct pinctrl_gpio_range *
471pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
472 unsigned int pin)
473{
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800474 struct pinctrl_gpio_range *range;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100475
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200476 mutex_lock(&pctldev->mutex);
Linus Walleij9afbefb2012-11-20 14:25:07 +0100477 /* Loop over the ranges */
478 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
479 /* Check if we're in the valid range */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200480 if (range->pins) {
481 int a;
482 for (a = 0; a < range->npins; a++) {
483 if (range->pins[a] == pin)
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800484 goto out;
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200485 }
486 } else if (pin >= range->pin_base &&
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800487 pin < range->pin_base + range->npins)
488 goto out;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100489 }
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800490 range = NULL;
491out:
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200492 mutex_unlock(&pctldev->mutex);
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800493 return range;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100494}
495EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
496
497/**
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530498 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
499 * @pctldev: pin controller device to remove the range from
500 * @range: the GPIO range to remove
501 */
502void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
503 struct pinctrl_gpio_range *range)
504{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200505 mutex_lock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530506 list_del(&range->node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200507 mutex_unlock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530508}
509EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
510
511/**
Linus Walleij7afde8b2011-10-19 17:07:16 +0200512 * pinctrl_get_group_selector() - returns the group selector for a group
513 * @pctldev: the pin controller handling the group
514 * @pin_group: the pin group to look up
515 */
516int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
517 const char *pin_group)
518{
519 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530520 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200521 unsigned group_selector = 0;
522
Viresh Kumard1e90e92012-03-30 11:25:40 +0530523 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200524 const char *gname = pctlops->get_group_name(pctldev,
525 group_selector);
526 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700527 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200528 "found group selector %u for %s\n",
529 group_selector,
530 pin_group);
531 return group_selector;
532 }
533
534 group_selector++;
535 }
536
Stephen Warren51cd24e2011-12-09 16:59:05 -0700537 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200538 pin_group);
539
540 return -EINVAL;
541}
542
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100543/**
544 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
545 * @gpio: the GPIO pin number from the GPIO subsystem number space
546 *
547 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
548 * as part of their gpio_request() semantics, platforms and individual drivers
549 * shall *NOT* request GPIO pins to be muxed in.
550 */
551int pinctrl_request_gpio(unsigned gpio)
552{
553 struct pinctrl_dev *pctldev;
554 struct pinctrl_gpio_range *range;
555 int ret;
556 int pin;
557
558 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700559 if (ret) {
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800560 if (pinctrl_ready_for_gpio_range(gpio))
561 ret = 0;
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800562 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700563 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100564
565 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200566 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100567
Stephen Warren57b676f2012-03-02 13:05:44 -0700568 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
569
Stephen Warren57b676f2012-03-02 13:05:44 -0700570 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100571}
572EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
573
574/**
575 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
576 * @gpio: the GPIO pin number from the GPIO subsystem number space
577 *
578 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
579 * as part of their gpio_free() semantics, platforms and individual drivers
580 * shall *NOT* request GPIO pins to be muxed out.
581 */
582void pinctrl_free_gpio(unsigned gpio)
583{
584 struct pinctrl_dev *pctldev;
585 struct pinctrl_gpio_range *range;
586 int ret;
587 int pin;
588
589 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700590 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100591 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700592 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200593 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100594
595 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200596 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100597
Stephen Warren57b676f2012-03-02 13:05:44 -0700598 pinmux_free_gpio(pctldev, pin, range);
599
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200600 mutex_unlock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100601}
602EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
603
604static int pinctrl_gpio_direction(unsigned gpio, bool input)
605{
606 struct pinctrl_dev *pctldev;
607 struct pinctrl_gpio_range *range;
608 int ret;
609 int pin;
610
611 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200612 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100613 return ret;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200614 }
615
616 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100617
618 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200619 pin = gpio_to_pin(range, gpio);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200620 ret = pinmux_gpio_direction(pctldev, range, pin, input);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100621
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200622 mutex_unlock(&pctldev->mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200623
624 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100625}
626
627/**
628 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
629 * @gpio: the GPIO pin number from the GPIO subsystem number space
630 *
631 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
632 * as part of their gpio_direction_input() semantics, platforms and individual
633 * drivers shall *NOT* touch pin control GPIO calls.
634 */
635int pinctrl_gpio_direction_input(unsigned gpio)
636{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200637 return pinctrl_gpio_direction(gpio, true);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100638}
639EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
640
641/**
642 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
643 * @gpio: the GPIO pin number from the GPIO subsystem number space
644 *
645 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
646 * as part of their gpio_direction_output() semantics, platforms and individual
647 * drivers shall *NOT* touch pin control GPIO calls.
648 */
649int pinctrl_gpio_direction_output(unsigned gpio)
650{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200651 return pinctrl_gpio_direction(gpio, false);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100652}
653EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
654
Stephen Warren6e5e9592012-03-02 13:05:47 -0700655static struct pinctrl_state *find_state(struct pinctrl *p,
656 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100657{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700658 struct pinctrl_state *state;
659
660 list_for_each_entry(state, &p->states, node)
661 if (!strcmp(state->name, name))
662 return state;
663
664 return NULL;
665}
666
667static struct pinctrl_state *create_state(struct pinctrl *p,
668 const char *name)
669{
670 struct pinctrl_state *state;
671
672 state = kzalloc(sizeof(*state), GFP_KERNEL);
673 if (state == NULL) {
674 dev_err(p->dev,
675 "failed to alloc struct pinctrl_state\n");
676 return ERR_PTR(-ENOMEM);
677 }
678
679 state->name = name;
680 INIT_LIST_HEAD(&state->settings);
681
682 list_add_tail(&state->node, &p->states);
683
684 return state;
685}
686
687static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
688{
689 struct pinctrl_state *state;
690 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700691 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700692
693 state = find_state(p, map->name);
694 if (!state)
695 state = create_state(p, map->name);
696 if (IS_ERR(state))
697 return PTR_ERR(state);
698
Stephen Warren1e2082b2012-03-02 13:05:48 -0700699 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
700 return 0;
701
Stephen Warren6e5e9592012-03-02 13:05:47 -0700702 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
703 if (setting == NULL) {
704 dev_err(p->dev,
705 "failed to alloc struct pinctrl_setting\n");
706 return -ENOMEM;
707 }
708
Stephen Warren1e2082b2012-03-02 13:05:48 -0700709 setting->type = map->type;
710
Stephen Warren6e5e9592012-03-02 13:05:47 -0700711 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
712 if (setting->pctldev == NULL) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700713 kfree(setting);
Linus Walleij89216492012-12-11 14:14:32 +0100714 /* Do not defer probing of hogs (circular loop) */
715 if (!strcmp(map->ctrl_dev_name, map->dev_name))
716 return -ENODEV;
Linus Walleijc05127c2012-04-10 10:00:38 +0200717 /*
718 * OK let us guess that the driver is not there yet, and
719 * let's defer obtaining this pinctrl handle to later...
720 */
Linus Walleij89216492012-12-11 14:14:32 +0100721 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
722 map->ctrl_dev_name);
Linus Walleijc05127c2012-04-10 10:00:38 +0200723 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700724 }
725
Linus Walleij1a789582012-10-17 20:51:54 +0200726 setting->dev_name = map->dev_name;
727
Stephen Warren1e2082b2012-03-02 13:05:48 -0700728 switch (map->type) {
729 case PIN_MAP_TYPE_MUX_GROUP:
730 ret = pinmux_map_to_setting(map, setting);
731 break;
732 case PIN_MAP_TYPE_CONFIGS_PIN:
733 case PIN_MAP_TYPE_CONFIGS_GROUP:
734 ret = pinconf_map_to_setting(map, setting);
735 break;
736 default:
737 ret = -EINVAL;
738 break;
739 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700740 if (ret < 0) {
741 kfree(setting);
742 return ret;
743 }
744
745 list_add_tail(&setting->node, &state->settings);
746
747 return 0;
748}
749
750static struct pinctrl *find_pinctrl(struct device *dev)
751{
752 struct pinctrl *p;
753
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200754 mutex_lock(&pinctrl_list_mutex);
Stephen Warren1e2082b2012-03-02 13:05:48 -0700755 list_for_each_entry(p, &pinctrl_list, node)
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200756 if (p->dev == dev) {
757 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700758 return p;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200759 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700760
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200761 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700762 return NULL;
763}
764
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200765static void pinctrl_free(struct pinctrl *p, bool inlist);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700766
767static struct pinctrl *create_pinctrl(struct device *dev)
768{
769 struct pinctrl *p;
770 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700771 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100772 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700773 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700774 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100775
776 /*
777 * create the state cookie holder struct pinctrl for each
778 * mapping, this is what consumers will get when requesting
779 * a pin control handle with pinctrl_get()
780 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700781 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700782 if (p == NULL) {
783 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100784 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700785 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700786 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700787 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -0600788 INIT_LIST_HEAD(&p->dt_maps);
789
790 ret = pinctrl_dt_to_map(p);
791 if (ret < 0) {
792 kfree(p);
793 return ERR_PTR(ret);
794 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700795
796 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100797
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200798 mutex_lock(&pinctrl_maps_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100799 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700800 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -0700801 /* Map must be for this device */
802 if (strcmp(map->dev_name, devname))
803 continue;
804
Stephen Warren6e5e9592012-03-02 13:05:47 -0700805 ret = add_setting(p, map);
Linus Walleij89216492012-12-11 14:14:32 +0100806 /*
807 * At this point the adding of a setting may:
808 *
809 * - Defer, if the pinctrl device is not yet available
810 * - Fail, if the pinctrl device is not yet available,
811 * AND the setting is a hog. We cannot defer that, since
812 * the hog will kick in immediately after the device
813 * is registered.
814 *
815 * If the error returned was not -EPROBE_DEFER then we
816 * accumulate the errors to see if we end up with
817 * an -EPROBE_DEFER later, as that is the worst case.
818 */
819 if (ret == -EPROBE_DEFER) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200820 pinctrl_free(p, false);
821 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700822 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100823 }
824 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200825 mutex_unlock(&pinctrl_maps_mutex);
826
Linus Walleij89216492012-12-11 14:14:32 +0100827 if (ret < 0) {
828 /* If some other error than deferral occured, return here */
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200829 pinctrl_free(p, false);
Linus Walleij89216492012-12-11 14:14:32 +0100830 return ERR_PTR(ret);
831 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100832
Linus Walleijab780292013-01-22 10:56:14 -0700833 kref_init(&p->users);
834
Linus Walleijb0666ba2012-12-11 13:12:35 +0100835 /* Add the pinctrl handle to the global list */
Stephen Warren8b9c1392012-02-19 23:45:42 -0700836 list_add_tail(&p->node, &pinctrl_list);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100837
838 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700839}
Stephen Warren7ecdb162012-03-02 13:05:45 -0700840
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200841/**
842 * pinctrl_get() - retrieves the pinctrl handle for a device
843 * @dev: the device to obtain the handle for
844 */
845struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700846{
847 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700848
Stephen Warren6e5e9592012-03-02 13:05:47 -0700849 if (WARN_ON(!dev))
850 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700851
Linus Walleijab780292013-01-22 10:56:14 -0700852 /*
853 * See if somebody else (such as the device core) has already
854 * obtained a handle to the pinctrl for this device. In that case,
855 * return another pointer to it.
856 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700857 p = find_pinctrl(dev);
Linus Walleijab780292013-01-22 10:56:14 -0700858 if (p != NULL) {
859 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
860 kref_get(&p->users);
861 return p;
862 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700863
Richard Genoudd599bfb2012-08-10 16:53:49 +0200864 return create_pinctrl(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100865}
866EXPORT_SYMBOL_GPL(pinctrl_get);
867
Richard Genoudd3cee8302013-03-25 15:47:21 +0100868static void pinctrl_free_setting(bool disable_setting,
869 struct pinctrl_setting *setting)
870{
871 switch (setting->type) {
872 case PIN_MAP_TYPE_MUX_GROUP:
873 if (disable_setting)
874 pinmux_disable_setting(setting);
875 pinmux_free_setting(setting);
876 break;
877 case PIN_MAP_TYPE_CONFIGS_PIN:
878 case PIN_MAP_TYPE_CONFIGS_GROUP:
879 pinconf_free_setting(setting);
880 break;
881 default:
882 break;
883 }
884}
885
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200886static void pinctrl_free(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -0700887{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700888 struct pinctrl_state *state, *n1;
889 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700890
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200891 mutex_lock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700892 list_for_each_entry_safe(state, n1, &p->states, node) {
893 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Richard Genoudd3cee8302013-03-25 15:47:21 +0100894 pinctrl_free_setting(state == p->state, setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700895 list_del(&setting->node);
896 kfree(setting);
897 }
898 list_del(&state->node);
899 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700900 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700901
Stephen Warren57291ce2012-03-23 10:29:46 -0600902 pinctrl_dt_free_maps(p);
903
Stephen Warren6e5e9592012-03-02 13:05:47 -0700904 if (inlist)
905 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700906 kfree(p);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200907 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700908}
909
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100910/**
Linus Walleijab780292013-01-22 10:56:14 -0700911 * pinctrl_release() - release the pinctrl handle
912 * @kref: the kref in the pinctrl being released
913 */
Sachin Kamat2917e832013-01-24 15:01:00 +0530914static void pinctrl_release(struct kref *kref)
Linus Walleijab780292013-01-22 10:56:14 -0700915{
916 struct pinctrl *p = container_of(kref, struct pinctrl, users);
917
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200918 pinctrl_free(p, true);
Linus Walleijab780292013-01-22 10:56:14 -0700919}
920
921/**
922 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
Stephen Warren6e5e9592012-03-02 13:05:47 -0700923 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100924 */
925void pinctrl_put(struct pinctrl *p)
926{
Linus Walleijab780292013-01-22 10:56:14 -0700927 kref_put(&p->users, pinctrl_release);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100928}
929EXPORT_SYMBOL_GPL(pinctrl_put);
930
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200931/**
932 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
933 * @p: the pinctrl handle to retrieve the state from
934 * @name: the state name to retrieve
935 */
936struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
937 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -0700938{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700939 struct pinctrl_state *state;
940
941 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800942 if (!state) {
943 if (pinctrl_dummy_state) {
944 /* create dummy state */
945 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
946 name);
947 state = create_state(p, name);
Richard Genoudd599bfb2012-08-10 16:53:49 +0200948 } else
949 state = ERR_PTR(-ENODEV);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800950 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700951
952 return state;
953}
Stephen Warren6e5e9592012-03-02 13:05:47 -0700954EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
955
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200956/**
957 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
958 * @p: the pinctrl handle for the device that requests configuration
959 * @state: the state handle to select/activate/program
960 */
961int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700962{
963 struct pinctrl_setting *setting, *setting2;
Richard Genoud50cf7c82013-03-25 15:47:23 +0100964 struct pinctrl_state *old_state = p->state;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700965 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700966
Stephen Warren6e5e9592012-03-02 13:05:47 -0700967 if (p->state == state)
968 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700969
Stephen Warren6e5e9592012-03-02 13:05:47 -0700970 if (p->state) {
971 /*
972 * The set of groups with a mux configuration in the old state
973 * may not be identical to the set of groups with a mux setting
974 * in the new state. While this might be unusual, it's entirely
975 * possible for the "user"-supplied mapping table to be written
976 * that way. For each group that was configured in the old state
977 * but not in the new state, this code puts that group into a
978 * safe/disabled state.
979 */
980 list_for_each_entry(setting, &p->state->settings, node) {
981 bool found = false;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700982 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
983 continue;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700984 list_for_each_entry(setting2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700985 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
986 continue;
987 if (setting2->data.mux.group ==
988 setting->data.mux.group) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700989 found = true;
990 break;
991 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700992 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700993 if (!found)
994 pinmux_disable_setting(setting);
995 }
996 }
997
Richard Genoud3102a762013-03-25 15:47:22 +0100998 p->state = NULL;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700999
1000 /* Apply all the settings for the new state */
1001 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001002 switch (setting->type) {
1003 case PIN_MAP_TYPE_MUX_GROUP:
1004 ret = pinmux_enable_setting(setting);
1005 break;
1006 case PIN_MAP_TYPE_CONFIGS_PIN:
1007 case PIN_MAP_TYPE_CONFIGS_GROUP:
1008 ret = pinconf_apply_setting(setting);
1009 break;
1010 default:
1011 ret = -EINVAL;
1012 break;
1013 }
Richard Genoud3102a762013-03-25 15:47:22 +01001014
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001015 if (ret < 0) {
Richard Genoud3102a762013-03-25 15:47:22 +01001016 goto unapply_new_state;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001017 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001018 }
1019
Richard Genoud3102a762013-03-25 15:47:22 +01001020 p->state = state;
1021
Stephen Warren7ecdb162012-03-02 13:05:45 -07001022 return 0;
Richard Genoud3102a762013-03-25 15:47:22 +01001023
1024unapply_new_state:
Richard Genoudda587512013-03-28 12:55:46 +01001025 dev_err(p->dev, "Error applying setting, reverse things back\n");
Richard Genoud3102a762013-03-25 15:47:22 +01001026
Richard Genoud3102a762013-03-25 15:47:22 +01001027 list_for_each_entry(setting2, &state->settings, node) {
1028 if (&setting2->node == &setting->node)
1029 break;
Richard Genoudaf606172013-03-29 10:03:26 +01001030 /*
1031 * All we can do here is pinmux_disable_setting.
1032 * That means that some pins are muxed differently now
1033 * than they were before applying the setting (We can't
1034 * "unmux a pin"!), but it's not a big deal since the pins
1035 * are free to be muxed by another apply_setting.
1036 */
1037 if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
1038 pinmux_disable_setting(setting2);
Richard Genoud3102a762013-03-25 15:47:22 +01001039 }
Richard Genoud8009d5f2013-03-28 12:55:47 +01001040
Richard Genoud385d9422013-03-29 10:03:27 +01001041 /* There's no infinite recursive loop here because p->state is NULL */
1042 if (old_state)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001043 pinctrl_select_state(p, old_state);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001044
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001045 return ret;
1046}
Stephen Warren6e5e9592012-03-02 13:05:47 -07001047EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001048
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001049static void devm_pinctrl_release(struct device *dev, void *res)
1050{
1051 pinctrl_put(*(struct pinctrl **)res);
1052}
1053
1054/**
1055 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
1056 * @dev: the device to obtain the handle for
1057 *
1058 * If there is a need to explicitly destroy the returned struct pinctrl,
1059 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1060 */
1061struct pinctrl *devm_pinctrl_get(struct device *dev)
1062{
1063 struct pinctrl **ptr, *p;
1064
1065 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
1066 if (!ptr)
1067 return ERR_PTR(-ENOMEM);
1068
1069 p = pinctrl_get(dev);
1070 if (!IS_ERR(p)) {
1071 *ptr = p;
1072 devres_add(dev, ptr);
1073 } else {
1074 devres_free(ptr);
1075 }
1076
1077 return p;
1078}
1079EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1080
1081static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1082{
1083 struct pinctrl **p = res;
1084
1085 return *p == data;
1086}
1087
1088/**
1089 * devm_pinctrl_put() - Resource managed pinctrl_put()
1090 * @p: the pinctrl handle to release
1091 *
1092 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1093 * this function will not need to be called and the resource management
1094 * code will ensure that the resource is freed.
1095 */
1096void devm_pinctrl_put(struct pinctrl *p)
1097{
Jingoo Hana72149e2013-02-26 11:34:07 +09001098 WARN_ON(devres_release(p->dev, devm_pinctrl_release,
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001099 devm_pinctrl_match, p));
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001100}
1101EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1102
Stephen Warren57291ce2012-03-23 10:29:46 -06001103int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
1104 bool dup, bool locked)
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001105{
Stephen Warren1e2082b2012-03-02 13:05:48 -07001106 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001107 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001108
1109 pr_debug("add %d pinmux maps\n", num_maps);
1110
1111 /* First sanity check the new mapping */
1112 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001113 if (!maps[i].dev_name) {
1114 pr_err("failed to register map %s (%d): no device given\n",
1115 maps[i].name, i);
1116 return -EINVAL;
1117 }
1118
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001119 if (!maps[i].name) {
1120 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001121 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001122 return -EINVAL;
1123 }
1124
Stephen Warren1e2082b2012-03-02 13:05:48 -07001125 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1126 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001127 pr_err("failed to register map %s (%d): no pin control device given\n",
1128 maps[i].name, i);
1129 return -EINVAL;
1130 }
1131
Stephen Warren1e2082b2012-03-02 13:05:48 -07001132 switch (maps[i].type) {
1133 case PIN_MAP_TYPE_DUMMY_STATE:
1134 break;
1135 case PIN_MAP_TYPE_MUX_GROUP:
1136 ret = pinmux_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 case PIN_MAP_TYPE_CONFIGS_PIN:
1141 case PIN_MAP_TYPE_CONFIGS_GROUP:
1142 ret = pinconf_validate_map(&maps[i], i);
1143 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001144 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001145 break;
1146 default:
1147 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001148 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -07001149 return -EINVAL;
1150 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001151 }
1152
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001153 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
1154 if (!maps_node) {
1155 pr_err("failed to alloc struct pinctrl_maps\n");
1156 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001157 }
1158
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001159 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -06001160 if (dup) {
1161 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
1162 GFP_KERNEL);
1163 if (!maps_node->maps) {
1164 pr_err("failed to duplicate mapping table\n");
1165 kfree(maps_node);
1166 return -ENOMEM;
1167 }
1168 } else {
1169 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001170 }
1171
Stephen Warren57291ce2012-03-23 10:29:46 -06001172 if (!locked)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001173 mutex_lock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001174 list_add_tail(&maps_node->node, &pinctrl_maps);
Stephen Warren57291ce2012-03-23 10:29:46 -06001175 if (!locked)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001176 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001177
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001178 return 0;
1179}
1180
Stephen Warren57291ce2012-03-23 10:29:46 -06001181/**
1182 * pinctrl_register_mappings() - register a set of pin controller mappings
1183 * @maps: the pincontrol mappings table to register. This should probably be
1184 * marked with __initdata so it can be discarded after boot. This
1185 * function will perform a shallow copy for the mapping entries.
1186 * @num_maps: the number of maps in the mapping table
1187 */
1188int pinctrl_register_mappings(struct pinctrl_map const *maps,
1189 unsigned num_maps)
1190{
1191 return pinctrl_register_map(maps, num_maps, true, false);
1192}
1193
1194void pinctrl_unregister_map(struct pinctrl_map const *map)
1195{
1196 struct pinctrl_maps *maps_node;
1197
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001198 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001199 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1200 if (maps_node->maps == map) {
1201 list_del(&maps_node->node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001202 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001203 return;
1204 }
1205 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001206 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001207}
1208
Julien Delacou840a47b2012-12-10 14:47:33 +01001209/**
1210 * pinctrl_force_sleep() - turn a given controller device into sleep state
1211 * @pctldev: pin controller device
1212 */
1213int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1214{
1215 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
1216 return pinctrl_select_state(pctldev->p, pctldev->hog_sleep);
1217 return 0;
1218}
1219EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1220
1221/**
1222 * pinctrl_force_default() - turn a given controller device into default state
1223 * @pctldev: pin controller device
1224 */
1225int pinctrl_force_default(struct pinctrl_dev *pctldev)
1226{
1227 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
1228 return pinctrl_select_state(pctldev->p, pctldev->hog_default);
1229 return 0;
1230}
1231EXPORT_SYMBOL_GPL(pinctrl_force_default);
1232
Linus Walleij14005ee2013-06-05 15:30:33 +02001233#ifdef CONFIG_PM
1234
1235/**
Tony Lindgrenf3333492013-07-18 08:15:04 -07001236 * pinctrl_pm_select_state() - select pinctrl state for PM
1237 * @dev: device to select default state for
1238 * @state: state to set
1239 */
1240static int pinctrl_pm_select_state(struct device *dev,
1241 struct pinctrl_state *state)
1242{
1243 struct dev_pin_info *pins = dev->pins;
1244 int ret;
1245
1246 if (IS_ERR(state))
1247 return 0; /* No such state */
1248 ret = pinctrl_select_state(pins->p, state);
1249 if (ret)
1250 dev_err(dev, "failed to activate pinctrl state %s\n",
1251 state->name);
1252 return ret;
1253}
1254
1255/**
Linus Walleij14005ee2013-06-05 15:30:33 +02001256 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1257 * @dev: device to select default state for
1258 */
1259int pinctrl_pm_select_default_state(struct device *dev)
1260{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001261 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001262 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001263
1264 return pinctrl_pm_select_state(dev, dev->pins->default_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001265}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001266EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001267
1268/**
1269 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1270 * @dev: device to select sleep state for
1271 */
1272int pinctrl_pm_select_sleep_state(struct device *dev)
1273{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001274 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001275 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001276
1277 return pinctrl_pm_select_state(dev, dev->pins->sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001278}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001279EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001280
1281/**
1282 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1283 * @dev: device to select idle state for
1284 */
1285int pinctrl_pm_select_idle_state(struct device *dev)
1286{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001287 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001288 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001289
1290 return pinctrl_pm_select_state(dev, dev->pins->idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001291}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001292EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001293#endif
1294
Linus Walleij2744e8a2011-05-02 20:50:54 +02001295#ifdef CONFIG_DEBUG_FS
1296
1297static int pinctrl_pins_show(struct seq_file *s, void *what)
1298{
1299 struct pinctrl_dev *pctldev = s->private;
1300 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001301 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001302
1303 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001304
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001305 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001306
Chanho Park706e8522012-01-03 16:47:50 +09001307 /* The pin number can be retrived from the pin controller descriptor */
1308 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001309 struct pin_desc *desc;
1310
Chanho Park706e8522012-01-03 16:47:50 +09001311 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001312 desc = pin_desc_get(pctldev, pin);
1313 /* Pin space may be sparse */
1314 if (desc == NULL)
1315 continue;
1316
1317 seq_printf(s, "pin %d (%s) ", pin,
1318 desc->name ? desc->name : "unnamed");
1319
1320 /* Driver-specific info per pin */
1321 if (ops->pin_dbg_show)
1322 ops->pin_dbg_show(pctldev, s, pin);
1323
1324 seq_puts(s, "\n");
1325 }
1326
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001327 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001328
Linus Walleij2744e8a2011-05-02 20:50:54 +02001329 return 0;
1330}
1331
1332static int pinctrl_groups_show(struct seq_file *s, void *what)
1333{
1334 struct pinctrl_dev *pctldev = s->private;
1335 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301336 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001337
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001338 mutex_lock(&pctldev->mutex);
1339
Viresh Kumard1e90e92012-03-30 11:25:40 +05301340 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001341
Linus Walleij2744e8a2011-05-02 20:50:54 +02001342 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301343 while (selector < ngroups) {
Stephen Warrena5818a82011-10-19 16:19:25 -06001344 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001345 unsigned num_pins;
1346 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001347 const char *pname;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001348 int ret;
1349 int i;
1350
1351 ret = ops->get_group_pins(pctldev, selector,
1352 &pins, &num_pins);
1353 if (ret)
1354 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1355 gname);
1356 else {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001357 seq_printf(s, "group: %s\n", gname);
1358 for (i = 0; i < num_pins; i++) {
1359 pname = pin_get_name(pctldev, pins[i]);
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001360 if (WARN_ON(!pname)) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001361 mutex_unlock(&pctldev->mutex);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001362 return -EINVAL;
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001363 }
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001364 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1365 }
1366 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001367 }
1368 selector++;
1369 }
1370
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001371 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001372
1373 return 0;
1374}
1375
1376static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1377{
1378 struct pinctrl_dev *pctldev = s->private;
1379 struct pinctrl_gpio_range *range = NULL;
1380
1381 seq_puts(s, "GPIO ranges handled:\n");
1382
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001383 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001384
Linus Walleij2744e8a2011-05-02 20:50:54 +02001385 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001386 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Christian Ruppertc8587ee2013-06-13 14:55:31 +02001387 if (range->pins) {
1388 int a;
1389 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
1390 range->id, range->name,
1391 range->base, (range->base + range->npins - 1));
1392 for (a = 0; a < range->npins - 1; a++)
1393 seq_printf(s, "%u, ", range->pins[a]);
1394 seq_printf(s, "%u}\n", range->pins[a]);
1395 }
1396 else
1397 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1398 range->id, range->name,
1399 range->base, (range->base + range->npins - 1),
1400 range->pin_base,
1401 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001402 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001403
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001404 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001405
1406 return 0;
1407}
1408
1409static int pinctrl_devices_show(struct seq_file *s, void *what)
1410{
1411 struct pinctrl_dev *pctldev;
1412
Linus Walleijae6b4d82011-10-19 18:14:33 +02001413 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001414
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001415 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001416
Linus Walleij2744e8a2011-05-02 20:50:54 +02001417 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1418 seq_printf(s, "%s ", pctldev->desc->name);
1419 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001420 seq_puts(s, "yes ");
1421 else
1422 seq_puts(s, "no ");
1423 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001424 seq_puts(s, "yes");
1425 else
1426 seq_puts(s, "no");
1427 seq_puts(s, "\n");
1428 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001429
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001430 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001431
1432 return 0;
1433}
1434
Stephen Warren1e2082b2012-03-02 13:05:48 -07001435static inline const char *map_type(enum pinctrl_map_type type)
1436{
1437 static const char * const names[] = {
1438 "INVALID",
1439 "DUMMY_STATE",
1440 "MUX_GROUP",
1441 "CONFIGS_PIN",
1442 "CONFIGS_GROUP",
1443 };
1444
1445 if (type >= ARRAY_SIZE(names))
1446 return "UNKNOWN";
1447
1448 return names[type];
1449}
1450
Stephen Warren3eedb432012-02-23 17:04:40 -07001451static int pinctrl_maps_show(struct seq_file *s, void *what)
1452{
1453 struct pinctrl_maps *maps_node;
1454 int i;
1455 struct pinctrl_map const *map;
1456
1457 seq_puts(s, "Pinctrl maps:\n");
1458
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001459 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001460 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001461 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1462 map->dev_name, map->name, map_type(map->type),
1463 map->type);
1464
1465 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1466 seq_printf(s, "controlling device %s\n",
1467 map->ctrl_dev_name);
1468
1469 switch (map->type) {
1470 case PIN_MAP_TYPE_MUX_GROUP:
1471 pinmux_show_map(s, map);
1472 break;
1473 case PIN_MAP_TYPE_CONFIGS_PIN:
1474 case PIN_MAP_TYPE_CONFIGS_GROUP:
1475 pinconf_show_map(s, map);
1476 break;
1477 default:
1478 break;
1479 }
1480
1481 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001482 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001483 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001484
1485 return 0;
1486}
1487
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001488static int pinctrl_show(struct seq_file *s, void *what)
1489{
1490 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001491 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001492 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001493
1494 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001495
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001496 mutex_lock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001497
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001498 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001499 seq_printf(s, "device: %s current state: %s\n",
1500 dev_name(p->dev),
1501 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001502
Stephen Warren6e5e9592012-03-02 13:05:47 -07001503 list_for_each_entry(state, &p->states, node) {
1504 seq_printf(s, " state: %s\n", state->name);
1505
1506 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001507 struct pinctrl_dev *pctldev = setting->pctldev;
1508
1509 seq_printf(s, " type: %s controller %s ",
1510 map_type(setting->type),
1511 pinctrl_dev_get_name(pctldev));
1512
1513 switch (setting->type) {
1514 case PIN_MAP_TYPE_MUX_GROUP:
1515 pinmux_show_setting(s, setting);
1516 break;
1517 case PIN_MAP_TYPE_CONFIGS_PIN:
1518 case PIN_MAP_TYPE_CONFIGS_GROUP:
1519 pinconf_show_setting(s, setting);
1520 break;
1521 default:
1522 break;
1523 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001524 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001525 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001526 }
1527
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001528 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001529
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001530 return 0;
1531}
1532
Linus Walleij2744e8a2011-05-02 20:50:54 +02001533static int pinctrl_pins_open(struct inode *inode, struct file *file)
1534{
1535 return single_open(file, pinctrl_pins_show, inode->i_private);
1536}
1537
1538static int pinctrl_groups_open(struct inode *inode, struct file *file)
1539{
1540 return single_open(file, pinctrl_groups_show, inode->i_private);
1541}
1542
1543static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1544{
1545 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1546}
1547
1548static int pinctrl_devices_open(struct inode *inode, struct file *file)
1549{
1550 return single_open(file, pinctrl_devices_show, NULL);
1551}
1552
Stephen Warren3eedb432012-02-23 17:04:40 -07001553static int pinctrl_maps_open(struct inode *inode, struct file *file)
1554{
1555 return single_open(file, pinctrl_maps_show, NULL);
1556}
1557
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001558static int pinctrl_open(struct inode *inode, struct file *file)
1559{
1560 return single_open(file, pinctrl_show, NULL);
1561}
1562
Linus Walleij2744e8a2011-05-02 20:50:54 +02001563static const struct file_operations pinctrl_pins_ops = {
1564 .open = pinctrl_pins_open,
1565 .read = seq_read,
1566 .llseek = seq_lseek,
1567 .release = single_release,
1568};
1569
1570static const struct file_operations pinctrl_groups_ops = {
1571 .open = pinctrl_groups_open,
1572 .read = seq_read,
1573 .llseek = seq_lseek,
1574 .release = single_release,
1575};
1576
1577static const struct file_operations pinctrl_gpioranges_ops = {
1578 .open = pinctrl_gpioranges_open,
1579 .read = seq_read,
1580 .llseek = seq_lseek,
1581 .release = single_release,
1582};
1583
1584static const struct file_operations pinctrl_devices_ops = {
1585 .open = pinctrl_devices_open,
1586 .read = seq_read,
1587 .llseek = seq_lseek,
1588 .release = single_release,
1589};
1590
Stephen Warren3eedb432012-02-23 17:04:40 -07001591static const struct file_operations pinctrl_maps_ops = {
1592 .open = pinctrl_maps_open,
1593 .read = seq_read,
1594 .llseek = seq_lseek,
1595 .release = single_release,
1596};
1597
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001598static const struct file_operations pinctrl_ops = {
1599 .open = pinctrl_open,
1600 .read = seq_read,
1601 .llseek = seq_lseek,
1602 .release = single_release,
1603};
1604
Linus Walleij2744e8a2011-05-02 20:50:54 +02001605static struct dentry *debugfs_root;
1606
1607static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1608{
Tony Lindgren02157162012-01-20 08:17:22 -08001609 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001610
Stephen Warren51cd24e2011-12-09 16:59:05 -07001611 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001612 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001613 pctldev->device_root = device_root;
1614
Linus Walleij2744e8a2011-05-02 20:50:54 +02001615 if (IS_ERR(device_root) || !device_root) {
1616 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001617 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001618 return;
1619 }
1620 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1621 device_root, pctldev, &pinctrl_pins_ops);
1622 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1623 device_root, pctldev, &pinctrl_groups_ops);
1624 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1625 device_root, pctldev, &pinctrl_gpioranges_ops);
1626 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +02001627 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001628}
1629
Tony Lindgren02157162012-01-20 08:17:22 -08001630static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1631{
1632 debugfs_remove_recursive(pctldev->device_root);
1633}
1634
Linus Walleij2744e8a2011-05-02 20:50:54 +02001635static void pinctrl_init_debugfs(void)
1636{
1637 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1638 if (IS_ERR(debugfs_root) || !debugfs_root) {
1639 pr_warn("failed to create debugfs directory\n");
1640 debugfs_root = NULL;
1641 return;
1642 }
1643
1644 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1645 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001646 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1647 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001648 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1649 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001650}
1651
1652#else /* CONFIG_DEBUG_FS */
1653
1654static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1655{
1656}
1657
1658static void pinctrl_init_debugfs(void)
1659{
1660}
1661
Tony Lindgren02157162012-01-20 08:17:22 -08001662static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1663{
1664}
1665
Linus Walleij2744e8a2011-05-02 20:50:54 +02001666#endif
1667
Stephen Warrend26bc492012-03-16 14:54:25 -06001668static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1669{
1670 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1671
1672 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301673 !ops->get_groups_count ||
Stephen Warrend26bc492012-03-16 14:54:25 -06001674 !ops->get_group_name ||
1675 !ops->get_group_pins)
1676 return -EINVAL;
1677
Stephen Warren57291ce2012-03-23 10:29:46 -06001678 if (ops->dt_node_to_map && !ops->dt_free_map)
1679 return -EINVAL;
1680
Stephen Warrend26bc492012-03-16 14:54:25 -06001681 return 0;
1682}
1683
Linus Walleij2744e8a2011-05-02 20:50:54 +02001684/**
1685 * pinctrl_register() - register a pin controller device
1686 * @pctldesc: descriptor for this pin controller
1687 * @dev: parent device for this pin controller
1688 * @driver_data: private pin controller data for this pin controller
1689 */
1690struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1691 struct device *dev, void *driver_data)
1692{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001693 struct pinctrl_dev *pctldev;
1694 int ret;
1695
Devendra Nagada9aecb2012-06-16 23:43:16 +05301696 if (!pctldesc)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001697 return NULL;
Devendra Nagada9aecb2012-06-16 23:43:16 +05301698 if (!pctldesc->name)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001699 return NULL;
1700
Stephen Warren02f5b982012-02-22 14:26:00 -07001701 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001702 if (pctldev == NULL) {
1703 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001704 return NULL;
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001705 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001706
1707 /* Initialize pin control device struct */
1708 pctldev->owner = pctldesc->owner;
1709 pctldev->desc = pctldesc;
1710 pctldev->driver_data = driver_data;
1711 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001712 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001713 pctldev->dev = dev;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001714 mutex_init(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001715
Stephen Warrend26bc492012-03-16 14:54:25 -06001716 /* check core ops for sanity */
Devendra Nagada9aecb2012-06-16 23:43:16 +05301717 if (pinctrl_check_ops(pctldev)) {
John Crispinad6e1102012-04-26 16:47:11 +02001718 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06001719 goto out_err;
1720 }
1721
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001722 /* If we're implementing pinmuxing, check the ops for sanity */
1723 if (pctldesc->pmxops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301724 if (pinmux_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001725 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001726 }
1727
1728 /* If we're implementing pinconfig, check the ops for sanity */
1729 if (pctldesc->confops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301730 if (pinconf_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001731 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001732 }
1733
Linus Walleij2744e8a2011-05-02 20:50:54 +02001734 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02001735 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001736 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1737 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001738 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001739 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1740 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001741 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001742 }
1743
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001744 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -07001745 list_add_tail(&pctldev->node, &pinctrldev_list);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001746 mutex_unlock(&pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001747
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001748 pctldev->p = pinctrl_get(pctldev->dev);
1749
Stephen Warren6e5e9592012-03-02 13:05:47 -07001750 if (!IS_ERR(pctldev->p)) {
Julien Delacou840a47b2012-12-10 14:47:33 +01001751 pctldev->hog_default =
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001752 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
Julien Delacou840a47b2012-12-10 14:47:33 +01001753 if (IS_ERR(pctldev->hog_default)) {
John Crispinad6e1102012-04-26 16:47:11 +02001754 dev_dbg(dev, "failed to lookup the default state\n");
1755 } else {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001756 if (pinctrl_select_state(pctldev->p,
Julien Delacou840a47b2012-12-10 14:47:33 +01001757 pctldev->hog_default))
John Crispinad6e1102012-04-26 16:47:11 +02001758 dev_err(dev,
1759 "failed to select default state\n");
John Crispinad6e1102012-04-26 16:47:11 +02001760 }
Julien Delacou840a47b2012-12-10 14:47:33 +01001761
1762 pctldev->hog_sleep =
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001763 pinctrl_lookup_state(pctldev->p,
Julien Delacou840a47b2012-12-10 14:47:33 +01001764 PINCTRL_STATE_SLEEP);
1765 if (IS_ERR(pctldev->hog_sleep))
1766 dev_dbg(dev, "failed to lookup the sleep state\n");
Stephen Warren6e5e9592012-03-02 13:05:47 -07001767 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001768
Stephen Warren2304b472012-02-22 14:26:01 -07001769 pinctrl_init_device_debugfs(pctldev);
1770
Linus Walleij2744e8a2011-05-02 20:50:54 +02001771 return pctldev;
1772
Stephen Warren51cd24e2011-12-09 16:59:05 -07001773out_err:
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001774 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001775 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001776 return NULL;
1777}
1778EXPORT_SYMBOL_GPL(pinctrl_register);
1779
1780/**
1781 * pinctrl_unregister() - unregister pinmux
1782 * @pctldev: pin controller to unregister
1783 *
1784 * Called by pinmux drivers to unregister a pinmux.
1785 */
1786void pinctrl_unregister(struct pinctrl_dev *pctldev)
1787{
Dong Aisheng5d589b02012-05-23 21:22:40 +08001788 struct pinctrl_gpio_range *range, *n;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001789 if (pctldev == NULL)
1790 return;
1791
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001792 mutex_lock(&pinctrldev_list_mutex);
1793 mutex_lock(&pctldev->mutex);
1794
Tony Lindgren02157162012-01-20 08:17:22 -08001795 pinctrl_remove_device_debugfs(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001796
Stephen Warren6e5e9592012-03-02 13:05:47 -07001797 if (!IS_ERR(pctldev->p))
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001798 pinctrl_put(pctldev->p);
Stephen Warren57b676f2012-03-02 13:05:44 -07001799
Linus Walleij2744e8a2011-05-02 20:50:54 +02001800 /* TODO: check that no pinmuxes are still active? */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001801 list_del(&pctldev->node);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001802 /* Destroy descriptor tree */
1803 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1804 pctldev->desc->npins);
Dong Aisheng5d589b02012-05-23 21:22:40 +08001805 /* remove gpio ranges map */
1806 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1807 list_del(&range->node);
1808
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001809 mutex_unlock(&pctldev->mutex);
1810 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001811 kfree(pctldev);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001812 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001813}
1814EXPORT_SYMBOL_GPL(pinctrl_unregister);
1815
1816static int __init pinctrl_init(void)
1817{
1818 pr_info("initialized pinctrl subsystem\n");
1819 pinctrl_init_debugfs();
1820 return 0;
1821}
1822
1823/* init early since many drivers really need to initialized pinmux early */
1824core_initcall(pinctrl_init);