blob: 9f305ac06275d6f45adb05ff1c7567b71f0c6bde [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,
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900228 const struct pinctrl_pin_desc *pin)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200229{
230 struct pin_desc *pindesc;
231
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900232 pindesc = pin_desc_get(pctldev, pin->number);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200233 if (pindesc != NULL) {
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900234 dev_err(pctldev->dev, "pin %d already registered\n",
235 pin->number);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200236 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 */
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900249 if (pin->name) {
250 pindesc->name = pin->name;
Linus Walleijca53c5f2011-12-14 20:33:37 +0100251 } else {
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900252 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", pin->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
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900260 pindesc->drv_data = pin->drv_data;
261
262 radix_tree_insert(&pctldev->pin_desc_tree, pin->number, pindesc);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200263 pr_debug("registered pin %d (%s) on %s\n",
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900264 pin->number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200265 return 0;
266}
267
268static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
269 struct pinctrl_pin_desc const *pins,
270 unsigned num_descs)
271{
272 unsigned i;
273 int ret = 0;
274
275 for (i = 0; i < num_descs; i++) {
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900276 ret = pinctrl_register_one_pin(pctldev, &pins[i]);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200277 if (ret)
278 return ret;
279 }
280
281 return 0;
282}
283
284/**
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200285 * gpio_to_pin() - GPIO range GPIO number to pin number translation
286 * @range: GPIO range used for the translation
287 * @gpio: gpio pin to translate to a pin number
288 *
289 * Finds the pin number for a given GPIO using the specified GPIO range
290 * as a base for translation. The distinction between linear GPIO ranges
291 * and pin list based GPIO ranges is managed correctly by this function.
292 *
293 * This function assumes the gpio is part of the specified GPIO range, use
294 * only after making sure this is the case (e.g. by calling it on the
295 * result of successful pinctrl_get_device_gpio_range calls)!
296 */
297static inline int gpio_to_pin(struct pinctrl_gpio_range *range,
298 unsigned int gpio)
299{
300 unsigned int offset = gpio - range->base;
301 if (range->pins)
302 return range->pins[offset];
303 else
304 return range->pin_base + offset;
305}
306
307/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200308 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
309 * @pctldev: pin controller device to check
310 * @gpio: gpio pin to check taken from the global GPIO pin space
311 *
312 * Tries to match a GPIO pin number to the ranges handled by a certain pin
313 * controller, return the range or NULL
314 */
315static struct pinctrl_gpio_range *
316pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
317{
318 struct pinctrl_gpio_range *range = NULL;
319
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200320 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200321 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200322 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
323 /* Check if we're in the valid range */
324 if (gpio >= range->base &&
325 gpio < range->base + range->npins) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200326 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200327 return range;
328 }
329 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200330 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200331 return NULL;
332}
333
334/**
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800335 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
336 * the same GPIO chip are in range
337 * @gpio: gpio pin to check taken from the global GPIO pin space
338 *
339 * This function is complement of pinctrl_match_gpio_range(). If the return
340 * value of pinctrl_match_gpio_range() is NULL, this function could be used
341 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
342 * of the same GPIO chip don't have back-end pinctrl interface.
343 * If the return value is true, it means that pinctrl device is ready & the
344 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
345 * is false, it means that pinctrl device may not be ready.
346 */
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800347#ifdef CONFIG_GPIOLIB
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800348static bool pinctrl_ready_for_gpio_range(unsigned gpio)
349{
350 struct pinctrl_dev *pctldev;
351 struct pinctrl_gpio_range *range = NULL;
352 struct gpio_chip *chip = gpio_to_chip(gpio);
353
Tony Lindgren942cde72015-09-03 10:34:30 -0700354 if (WARN(!chip, "no gpio_chip for gpio%i?", gpio))
355 return false;
356
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200357 mutex_lock(&pinctrldev_list_mutex);
358
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800359 /* Loop over the pin controllers */
360 list_for_each_entry(pctldev, &pinctrldev_list, node) {
361 /* Loop over the ranges */
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800362 mutex_lock(&pctldev->mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800363 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
364 /* Check if any gpio range overlapped with gpio chip */
365 if (range->base + range->npins - 1 < chip->base ||
366 range->base > chip->base + chip->ngpio - 1)
367 continue;
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800368 mutex_unlock(&pctldev->mutex);
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200369 mutex_unlock(&pinctrldev_list_mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800370 return true;
371 }
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800372 mutex_unlock(&pctldev->mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800373 }
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200374
375 mutex_unlock(&pinctrldev_list_mutex);
376
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800377 return false;
378}
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800379#else
380static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
381#endif
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800382
383/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200384 * pinctrl_get_device_gpio_range() - find device for GPIO range
385 * @gpio: the pin to locate the pin controller for
386 * @outdev: the pin control device if found
387 * @outrange: the GPIO range if found
388 *
389 * Find the pin controller handling a certain GPIO pin from the pinspace of
390 * the GPIO subsystem, return the device and the matching GPIO range. Returns
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800391 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
392 * may still have not been registered.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200393 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700394static int pinctrl_get_device_gpio_range(unsigned gpio,
395 struct pinctrl_dev **outdev,
396 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200397{
398 struct pinctrl_dev *pctldev = NULL;
399
Axel Linf0059022013-08-18 20:34:22 +0800400 mutex_lock(&pinctrldev_list_mutex);
401
Linus Walleij2744e8a2011-05-02 20:50:54 +0200402 /* Loop over the pin controllers */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200403 list_for_each_entry(pctldev, &pinctrldev_list, node) {
404 struct pinctrl_gpio_range *range;
405
406 range = pinctrl_match_gpio_range(pctldev, gpio);
407 if (range != NULL) {
408 *outdev = pctldev;
409 *outrange = range;
Axel Linf0059022013-08-18 20:34:22 +0800410 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200411 return 0;
412 }
413 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200414
Axel Linf0059022013-08-18 20:34:22 +0800415 mutex_unlock(&pinctrldev_list_mutex);
416
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800417 return -EPROBE_DEFER;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200418}
419
420/**
421 * pinctrl_add_gpio_range() - register a GPIO range for a controller
422 * @pctldev: pin controller device to add the range to
423 * @range: the GPIO range to add
424 *
425 * This adds a range of GPIOs to be handled by a certain pin controller. Call
426 * this to register handled ranges after registering your pin controller.
427 */
428void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
429 struct pinctrl_gpio_range *range)
430{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200431 mutex_lock(&pctldev->mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700432 list_add_tail(&range->node, &pctldev->gpio_ranges);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200433 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200434}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700435EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200436
Dong Aisheng3e5e00b2012-05-23 21:22:41 +0800437void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
438 struct pinctrl_gpio_range *ranges,
439 unsigned nranges)
440{
441 int i;
442
443 for (i = 0; i < nranges; i++)
444 pinctrl_add_gpio_range(pctldev, &ranges[i]);
445}
446EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
447
Linus Walleij192c3692012-11-20 14:03:37 +0100448struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530449 struct pinctrl_gpio_range *range)
450{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200451 struct pinctrl_dev *pctldev;
452
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200453 pctldev = get_pinctrl_dev_from_devname(devname);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530454
Linus Walleijdfa97512012-11-20 14:54:18 +0100455 /*
456 * If we can't find this device, let's assume that is because
457 * it has not probed yet, so the driver trying to register this
458 * range need to defer probing.
459 */
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200460 if (!pctldev) {
Linus Walleijdfa97512012-11-20 14:54:18 +0100461 return ERR_PTR(-EPROBE_DEFER);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200462 }
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530463 pinctrl_add_gpio_range(pctldev, range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200464
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530465 return pctldev;
466}
Linus Walleij192c3692012-11-20 14:03:37 +0100467EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530468
Christian Ruppert586a87e2013-10-15 15:37:54 +0200469int pinctrl_get_group_pins(struct pinctrl_dev *pctldev, const char *pin_group,
470 const unsigned **pins, unsigned *num_pins)
471{
472 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
473 int gs;
474
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +0200475 if (!pctlops->get_group_pins)
476 return -EINVAL;
477
Christian Ruppert586a87e2013-10-15 15:37:54 +0200478 gs = pinctrl_get_group_selector(pctldev, pin_group);
479 if (gs < 0)
480 return gs;
481
482 return pctlops->get_group_pins(pctldev, gs, pins, num_pins);
483}
484EXPORT_SYMBOL_GPL(pinctrl_get_group_pins);
485
Joachim Eastwoodb18537c2016-02-25 22:44:37 +0100486struct pinctrl_gpio_range *
487pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev *pctldev,
488 unsigned int pin)
489{
490 struct pinctrl_gpio_range *range;
491
492 /* Loop over the ranges */
493 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
494 /* Check if we're in the valid range */
495 if (range->pins) {
496 int a;
497 for (a = 0; a < range->npins; a++) {
498 if (range->pins[a] == pin)
499 return range;
500 }
501 } else if (pin >= range->pin_base &&
502 pin < range->pin_base + range->npins)
503 return range;
504 }
505
506 return NULL;
507}
508EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin_nolock);
509
Linus Walleij2744e8a2011-05-02 20:50:54 +0200510/**
Linus Walleij9afbefb2012-11-20 14:25:07 +0100511 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
512 * @pctldev: the pin controller device to look in
513 * @pin: a controller-local number to find the range for
514 */
515struct pinctrl_gpio_range *
516pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
517 unsigned int pin)
518{
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800519 struct pinctrl_gpio_range *range;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100520
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200521 mutex_lock(&pctldev->mutex);
Joachim Eastwoodb18537c2016-02-25 22:44:37 +0100522 range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200523 mutex_unlock(&pctldev->mutex);
Joachim Eastwoodb18537c2016-02-25 22:44:37 +0100524
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800525 return range;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100526}
527EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
528
529/**
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530530 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
531 * @pctldev: pin controller device to remove the range from
532 * @range: the GPIO range to remove
533 */
534void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
535 struct pinctrl_gpio_range *range)
536{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200537 mutex_lock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530538 list_del(&range->node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200539 mutex_unlock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530540}
541EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
542
Linus Walleijc033a712016-12-30 15:04:43 +0100543#ifdef CONFIG_GENERIC_PINCTRL_GROUPS
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800544
545/**
546 * pinctrl_generic_get_group_count() - returns the number of pin groups
547 * @pctldev: pin controller device
548 */
549int pinctrl_generic_get_group_count(struct pinctrl_dev *pctldev)
550{
551 return pctldev->num_groups;
552}
553EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_count);
554
555/**
556 * pinctrl_generic_get_group_name() - returns the name of a pin group
557 * @pctldev: pin controller device
558 * @selector: group number
559 */
560const char *pinctrl_generic_get_group_name(struct pinctrl_dev *pctldev,
561 unsigned int selector)
562{
563 struct group_desc *group;
564
565 group = radix_tree_lookup(&pctldev->pin_group_tree,
566 selector);
567 if (!group)
568 return NULL;
569
570 return group->name;
571}
572EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_name);
573
574/**
575 * pinctrl_generic_get_group_pins() - gets the pin group pins
576 * @pctldev: pin controller device
577 * @selector: group number
578 * @pins: pins in the group
579 * @num_pins: number of pins in the group
580 */
581int pinctrl_generic_get_group_pins(struct pinctrl_dev *pctldev,
582 unsigned int selector,
583 const unsigned int **pins,
584 unsigned int *num_pins)
585{
586 struct group_desc *group;
587
588 group = radix_tree_lookup(&pctldev->pin_group_tree,
589 selector);
590 if (!group) {
591 dev_err(pctldev->dev, "%s could not find pingroup%i\n",
592 __func__, selector);
593 return -EINVAL;
594 }
595
596 *pins = group->pins;
597 *num_pins = group->num_pins;
598
599 return 0;
600}
601EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_pins);
602
603/**
604 * pinctrl_generic_get_group() - returns a pin group based on the number
605 * @pctldev: pin controller device
606 * @gselector: group number
607 */
608struct group_desc *pinctrl_generic_get_group(struct pinctrl_dev *pctldev,
609 unsigned int selector)
610{
611 struct group_desc *group;
612
613 group = radix_tree_lookup(&pctldev->pin_group_tree,
614 selector);
615 if (!group)
616 return NULL;
617
618 return group;
619}
620EXPORT_SYMBOL_GPL(pinctrl_generic_get_group);
621
622/**
623 * pinctrl_generic_add_group() - adds a new pin group
624 * @pctldev: pin controller device
625 * @name: name of the pin group
626 * @pins: pins in the pin group
627 * @num_pins: number of pins in the pin group
628 * @data: pin controller driver specific data
629 *
630 * Note that the caller must take care of locking.
631 */
632int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name,
633 int *pins, int num_pins, void *data)
634{
635 struct group_desc *group;
636
637 group = devm_kzalloc(pctldev->dev, sizeof(*group), GFP_KERNEL);
638 if (!group)
639 return -ENOMEM;
640
641 group->name = name;
642 group->pins = pins;
643 group->num_pins = num_pins;
644 group->data = data;
645
646 radix_tree_insert(&pctldev->pin_group_tree, pctldev->num_groups,
647 group);
648
649 pctldev->num_groups++;
650
651 return 0;
652}
653EXPORT_SYMBOL_GPL(pinctrl_generic_add_group);
654
655/**
656 * pinctrl_generic_remove_group() - removes a numbered pin group
657 * @pctldev: pin controller device
658 * @selector: group number
659 *
660 * Note that the caller must take care of locking.
661 */
662int pinctrl_generic_remove_group(struct pinctrl_dev *pctldev,
663 unsigned int selector)
664{
665 struct group_desc *group;
666
667 group = radix_tree_lookup(&pctldev->pin_group_tree,
668 selector);
669 if (!group)
670 return -ENOENT;
671
672 radix_tree_delete(&pctldev->pin_group_tree, selector);
673 devm_kfree(pctldev->dev, group);
674
675 pctldev->num_groups--;
676
677 return 0;
678}
679EXPORT_SYMBOL_GPL(pinctrl_generic_remove_group);
680
681/**
682 * pinctrl_generic_free_groups() - removes all pin groups
683 * @pctldev: pin controller device
684 *
685 * Note that the caller must take care of locking.
686 */
687static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
688{
689 struct radix_tree_iter iter;
690 struct group_desc *group;
691 unsigned long *indices;
692 void **slot;
693 int i = 0;
694
695 indices = devm_kzalloc(pctldev->dev, sizeof(*indices) *
696 pctldev->num_groups, GFP_KERNEL);
697 if (!indices)
698 return;
699
700 radix_tree_for_each_slot(slot, &pctldev->pin_group_tree, &iter, 0)
701 indices[i++] = iter.index;
702
703 for (i = 0; i < pctldev->num_groups; i++) {
704 group = radix_tree_lookup(&pctldev->pin_group_tree,
705 indices[i]);
706 radix_tree_delete(&pctldev->pin_group_tree, indices[i]);
707 devm_kfree(pctldev->dev, group);
708 }
709
710 pctldev->num_groups = 0;
711}
712
713#else
714static inline void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
715{
716}
Linus Walleijc033a712016-12-30 15:04:43 +0100717#endif /* CONFIG_GENERIC_PINCTRL_GROUPS */
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800718
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530719/**
Linus Walleij7afde8b2011-10-19 17:07:16 +0200720 * pinctrl_get_group_selector() - returns the group selector for a group
721 * @pctldev: the pin controller handling the group
722 * @pin_group: the pin group to look up
723 */
724int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
725 const char *pin_group)
726{
727 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530728 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200729 unsigned group_selector = 0;
730
Viresh Kumard1e90e92012-03-30 11:25:40 +0530731 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200732 const char *gname = pctlops->get_group_name(pctldev,
733 group_selector);
734 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700735 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200736 "found group selector %u for %s\n",
737 group_selector,
738 pin_group);
739 return group_selector;
740 }
741
742 group_selector++;
743 }
744
Stephen Warren51cd24e2011-12-09 16:59:05 -0700745 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200746 pin_group);
747
748 return -EINVAL;
749}
750
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100751/**
Geert Uytterhoevenb217e432015-05-04 19:46:57 +0200752 * pinctrl_request_gpio() - request a single pin to be used as GPIO
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100753 * @gpio: the GPIO pin number from the GPIO subsystem number space
754 *
755 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
756 * as part of their gpio_request() semantics, platforms and individual drivers
757 * shall *NOT* request GPIO pins to be muxed in.
758 */
759int pinctrl_request_gpio(unsigned gpio)
760{
761 struct pinctrl_dev *pctldev;
762 struct pinctrl_gpio_range *range;
763 int ret;
764 int pin;
765
766 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700767 if (ret) {
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800768 if (pinctrl_ready_for_gpio_range(gpio))
769 ret = 0;
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800770 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700771 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100772
Axel Lin9b77ace42013-08-19 10:07:46 +0800773 mutex_lock(&pctldev->mutex);
774
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100775 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200776 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100777
Stephen Warren57b676f2012-03-02 13:05:44 -0700778 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
779
Axel Lin9b77ace42013-08-19 10:07:46 +0800780 mutex_unlock(&pctldev->mutex);
781
Stephen Warren57b676f2012-03-02 13:05:44 -0700782 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100783}
784EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
785
786/**
787 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
788 * @gpio: the GPIO pin number from the GPIO subsystem number space
789 *
790 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
791 * as part of their gpio_free() semantics, platforms and individual drivers
792 * shall *NOT* request GPIO pins to be muxed out.
793 */
794void pinctrl_free_gpio(unsigned gpio)
795{
796 struct pinctrl_dev *pctldev;
797 struct pinctrl_gpio_range *range;
798 int ret;
799 int pin;
800
801 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700802 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100803 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700804 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200805 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100806
807 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200808 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100809
Stephen Warren57b676f2012-03-02 13:05:44 -0700810 pinmux_free_gpio(pctldev, pin, range);
811
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200812 mutex_unlock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100813}
814EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
815
816static int pinctrl_gpio_direction(unsigned gpio, bool input)
817{
818 struct pinctrl_dev *pctldev;
819 struct pinctrl_gpio_range *range;
820 int ret;
821 int pin;
822
823 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200824 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100825 return ret;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200826 }
827
828 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100829
830 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200831 pin = gpio_to_pin(range, gpio);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200832 ret = pinmux_gpio_direction(pctldev, range, pin, input);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100833
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200834 mutex_unlock(&pctldev->mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200835
836 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100837}
838
839/**
840 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
841 * @gpio: the GPIO pin number from the GPIO subsystem number space
842 *
843 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
844 * as part of their gpio_direction_input() semantics, platforms and individual
845 * drivers shall *NOT* touch pin control GPIO calls.
846 */
847int pinctrl_gpio_direction_input(unsigned gpio)
848{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200849 return pinctrl_gpio_direction(gpio, true);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100850}
851EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
852
853/**
854 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
855 * @gpio: the GPIO pin number from the GPIO subsystem number space
856 *
857 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
858 * as part of their gpio_direction_output() semantics, platforms and individual
859 * drivers shall *NOT* touch pin control GPIO calls.
860 */
861int pinctrl_gpio_direction_output(unsigned gpio)
862{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200863 return pinctrl_gpio_direction(gpio, false);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100864}
865EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
866
Stephen Warren6e5e9592012-03-02 13:05:47 -0700867static struct pinctrl_state *find_state(struct pinctrl *p,
868 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100869{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700870 struct pinctrl_state *state;
871
872 list_for_each_entry(state, &p->states, node)
873 if (!strcmp(state->name, name))
874 return state;
875
876 return NULL;
877}
878
879static struct pinctrl_state *create_state(struct pinctrl *p,
880 const char *name)
881{
882 struct pinctrl_state *state;
883
884 state = kzalloc(sizeof(*state), GFP_KERNEL);
885 if (state == NULL) {
886 dev_err(p->dev,
887 "failed to alloc struct pinctrl_state\n");
888 return ERR_PTR(-ENOMEM);
889 }
890
891 state->name = name;
892 INIT_LIST_HEAD(&state->settings);
893
894 list_add_tail(&state->node, &p->states);
895
896 return state;
897}
898
Tony Lindgren99e4f672016-12-27 09:19:59 -0800899static int add_setting(struct pinctrl *p, struct pinctrl_dev *pctldev,
900 struct pinctrl_map const *map)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700901{
902 struct pinctrl_state *state;
903 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700904 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700905
906 state = find_state(p, map->name);
907 if (!state)
908 state = create_state(p, map->name);
909 if (IS_ERR(state))
910 return PTR_ERR(state);
911
Stephen Warren1e2082b2012-03-02 13:05:48 -0700912 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
913 return 0;
914
Stephen Warren6e5e9592012-03-02 13:05:47 -0700915 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
916 if (setting == NULL) {
917 dev_err(p->dev,
918 "failed to alloc struct pinctrl_setting\n");
919 return -ENOMEM;
920 }
921
Stephen Warren1e2082b2012-03-02 13:05:48 -0700922 setting->type = map->type;
923
Tony Lindgren99e4f672016-12-27 09:19:59 -0800924 if (pctldev)
925 setting->pctldev = pctldev;
926 else
927 setting->pctldev =
928 get_pinctrl_dev_from_devname(map->ctrl_dev_name);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700929 if (setting->pctldev == NULL) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700930 kfree(setting);
Linus Walleij89216492012-12-11 14:14:32 +0100931 /* Do not defer probing of hogs (circular loop) */
932 if (!strcmp(map->ctrl_dev_name, map->dev_name))
933 return -ENODEV;
Linus Walleijc05127c2012-04-10 10:00:38 +0200934 /*
935 * OK let us guess that the driver is not there yet, and
936 * let's defer obtaining this pinctrl handle to later...
937 */
Linus Walleij89216492012-12-11 14:14:32 +0100938 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
939 map->ctrl_dev_name);
Linus Walleijc05127c2012-04-10 10:00:38 +0200940 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700941 }
942
Linus Walleij1a789582012-10-17 20:51:54 +0200943 setting->dev_name = map->dev_name;
944
Stephen Warren1e2082b2012-03-02 13:05:48 -0700945 switch (map->type) {
946 case PIN_MAP_TYPE_MUX_GROUP:
947 ret = pinmux_map_to_setting(map, setting);
948 break;
949 case PIN_MAP_TYPE_CONFIGS_PIN:
950 case PIN_MAP_TYPE_CONFIGS_GROUP:
951 ret = pinconf_map_to_setting(map, setting);
952 break;
953 default:
954 ret = -EINVAL;
955 break;
956 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700957 if (ret < 0) {
958 kfree(setting);
959 return ret;
960 }
961
962 list_add_tail(&setting->node, &state->settings);
963
964 return 0;
965}
966
967static struct pinctrl *find_pinctrl(struct device *dev)
968{
969 struct pinctrl *p;
970
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200971 mutex_lock(&pinctrl_list_mutex);
Stephen Warren1e2082b2012-03-02 13:05:48 -0700972 list_for_each_entry(p, &pinctrl_list, node)
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200973 if (p->dev == dev) {
974 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700975 return p;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200976 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700977
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200978 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700979 return NULL;
980}
981
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200982static void pinctrl_free(struct pinctrl *p, bool inlist);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700983
Tony Lindgren99e4f672016-12-27 09:19:59 -0800984static struct pinctrl *create_pinctrl(struct device *dev,
985 struct pinctrl_dev *pctldev)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700986{
987 struct pinctrl *p;
988 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700989 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100990 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700991 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700992 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100993
994 /*
995 * create the state cookie holder struct pinctrl for each
996 * mapping, this is what consumers will get when requesting
997 * a pin control handle with pinctrl_get()
998 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700999 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001000 if (p == NULL) {
1001 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001002 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001003 }
Stephen Warren7ecdb162012-03-02 13:05:45 -07001004 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001005 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -06001006 INIT_LIST_HEAD(&p->dt_maps);
1007
Tony Lindgren99e4f672016-12-27 09:19:59 -08001008 ret = pinctrl_dt_to_map(p, pctldev);
Stephen Warren57291ce2012-03-23 10:29:46 -06001009 if (ret < 0) {
1010 kfree(p);
1011 return ERR_PTR(ret);
1012 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001013
1014 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001015
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001016 mutex_lock(&pinctrl_maps_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001017 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001018 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -07001019 /* Map must be for this device */
1020 if (strcmp(map->dev_name, devname))
1021 continue;
1022
Tony Lindgren99e4f672016-12-27 09:19:59 -08001023 ret = add_setting(p, pctldev, map);
Linus Walleij89216492012-12-11 14:14:32 +01001024 /*
1025 * At this point the adding of a setting may:
1026 *
1027 * - Defer, if the pinctrl device is not yet available
1028 * - Fail, if the pinctrl device is not yet available,
1029 * AND the setting is a hog. We cannot defer that, since
1030 * the hog will kick in immediately after the device
1031 * is registered.
1032 *
1033 * If the error returned was not -EPROBE_DEFER then we
1034 * accumulate the errors to see if we end up with
1035 * an -EPROBE_DEFER later, as that is the worst case.
1036 */
1037 if (ret == -EPROBE_DEFER) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001038 pinctrl_free(p, false);
1039 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001040 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001041 }
1042 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001043 mutex_unlock(&pinctrl_maps_mutex);
1044
Linus Walleij89216492012-12-11 14:14:32 +01001045 if (ret < 0) {
1046 /* If some other error than deferral occured, return here */
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001047 pinctrl_free(p, false);
Linus Walleij89216492012-12-11 14:14:32 +01001048 return ERR_PTR(ret);
1049 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001050
Linus Walleijab780292013-01-22 10:56:14 -07001051 kref_init(&p->users);
1052
Linus Walleijb0666ba2012-12-11 13:12:35 +01001053 /* Add the pinctrl handle to the global list */
Stanislaw Gruszka7b320cb2014-02-04 09:07:09 +01001054 mutex_lock(&pinctrl_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -07001055 list_add_tail(&p->node, &pinctrl_list);
Stanislaw Gruszka7b320cb2014-02-04 09:07:09 +01001056 mutex_unlock(&pinctrl_list_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001057
1058 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001059}
Stephen Warren7ecdb162012-03-02 13:05:45 -07001060
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001061/**
1062 * pinctrl_get() - retrieves the pinctrl handle for a device
1063 * @dev: the device to obtain the handle for
1064 */
1065struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warren6e5e9592012-03-02 13:05:47 -07001066{
1067 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001068
Stephen Warren6e5e9592012-03-02 13:05:47 -07001069 if (WARN_ON(!dev))
1070 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -07001071
Linus Walleijab780292013-01-22 10:56:14 -07001072 /*
1073 * See if somebody else (such as the device core) has already
1074 * obtained a handle to the pinctrl for this device. In that case,
1075 * return another pointer to it.
1076 */
Stephen Warren6e5e9592012-03-02 13:05:47 -07001077 p = find_pinctrl(dev);
Linus Walleijab780292013-01-22 10:56:14 -07001078 if (p != NULL) {
1079 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
1080 kref_get(&p->users);
1081 return p;
1082 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001083
Tony Lindgren99e4f672016-12-27 09:19:59 -08001084 return create_pinctrl(dev, NULL);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001085}
1086EXPORT_SYMBOL_GPL(pinctrl_get);
1087
Richard Genoudd3cee8302013-03-25 15:47:21 +01001088static void pinctrl_free_setting(bool disable_setting,
1089 struct pinctrl_setting *setting)
1090{
1091 switch (setting->type) {
1092 case PIN_MAP_TYPE_MUX_GROUP:
1093 if (disable_setting)
1094 pinmux_disable_setting(setting);
1095 pinmux_free_setting(setting);
1096 break;
1097 case PIN_MAP_TYPE_CONFIGS_PIN:
1098 case PIN_MAP_TYPE_CONFIGS_GROUP:
1099 pinconf_free_setting(setting);
1100 break;
1101 default:
1102 break;
1103 }
1104}
1105
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001106static void pinctrl_free(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -07001107{
Stephen Warren6e5e9592012-03-02 13:05:47 -07001108 struct pinctrl_state *state, *n1;
1109 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001110
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001111 mutex_lock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001112 list_for_each_entry_safe(state, n1, &p->states, node) {
1113 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Richard Genoudd3cee8302013-03-25 15:47:21 +01001114 pinctrl_free_setting(state == p->state, setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001115 list_del(&setting->node);
1116 kfree(setting);
1117 }
1118 list_del(&state->node);
1119 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -07001120 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001121
Stephen Warren57291ce2012-03-23 10:29:46 -06001122 pinctrl_dt_free_maps(p);
1123
Stephen Warren6e5e9592012-03-02 13:05:47 -07001124 if (inlist)
1125 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -07001126 kfree(p);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001127 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001128}
1129
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001130/**
Linus Walleijab780292013-01-22 10:56:14 -07001131 * pinctrl_release() - release the pinctrl handle
1132 * @kref: the kref in the pinctrl being released
1133 */
Sachin Kamat2917e832013-01-24 15:01:00 +05301134static void pinctrl_release(struct kref *kref)
Linus Walleijab780292013-01-22 10:56:14 -07001135{
1136 struct pinctrl *p = container_of(kref, struct pinctrl, users);
1137
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001138 pinctrl_free(p, true);
Linus Walleijab780292013-01-22 10:56:14 -07001139}
1140
1141/**
1142 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
Stephen Warren6e5e9592012-03-02 13:05:47 -07001143 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001144 */
1145void pinctrl_put(struct pinctrl *p)
1146{
Linus Walleijab780292013-01-22 10:56:14 -07001147 kref_put(&p->users, pinctrl_release);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001148}
1149EXPORT_SYMBOL_GPL(pinctrl_put);
1150
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001151/**
1152 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
1153 * @p: the pinctrl handle to retrieve the state from
1154 * @name: the state name to retrieve
1155 */
1156struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
1157 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -07001158{
Stephen Warren6e5e9592012-03-02 13:05:47 -07001159 struct pinctrl_state *state;
1160
1161 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +08001162 if (!state) {
1163 if (pinctrl_dummy_state) {
1164 /* create dummy state */
1165 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
1166 name);
1167 state = create_state(p, name);
Richard Genoudd599bfb2012-08-10 16:53:49 +02001168 } else
1169 state = ERR_PTR(-ENODEV);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +08001170 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001171
1172 return state;
1173}
Stephen Warren6e5e9592012-03-02 13:05:47 -07001174EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
1175
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001176/**
1177 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
1178 * @p: the pinctrl handle for the device that requests configuration
1179 * @state: the state handle to select/activate/program
1180 */
1181int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
Stephen Warren6e5e9592012-03-02 13:05:47 -07001182{
1183 struct pinctrl_setting *setting, *setting2;
Richard Genoud50cf7c82013-03-25 15:47:23 +01001184 struct pinctrl_state *old_state = p->state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001185 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -07001186
Stephen Warren6e5e9592012-03-02 13:05:47 -07001187 if (p->state == state)
1188 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -07001189
Stephen Warren6e5e9592012-03-02 13:05:47 -07001190 if (p->state) {
1191 /*
Fan Wu2243a872014-06-09 09:37:56 +08001192 * For each pinmux setting in the old state, forget SW's record
1193 * of mux owner for that pingroup. Any pingroups which are
1194 * still owned by the new state will be re-acquired by the call
1195 * to pinmux_enable_setting() in the loop below.
Stephen Warren6e5e9592012-03-02 13:05:47 -07001196 */
1197 list_for_each_entry(setting, &p->state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001198 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
1199 continue;
Fan Wu2243a872014-06-09 09:37:56 +08001200 pinmux_disable_setting(setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001201 }
1202 }
1203
Richard Genoud3102a762013-03-25 15:47:22 +01001204 p->state = NULL;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001205
1206 /* Apply all the settings for the new state */
1207 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001208 switch (setting->type) {
1209 case PIN_MAP_TYPE_MUX_GROUP:
1210 ret = pinmux_enable_setting(setting);
1211 break;
1212 case PIN_MAP_TYPE_CONFIGS_PIN:
1213 case PIN_MAP_TYPE_CONFIGS_GROUP:
1214 ret = pinconf_apply_setting(setting);
1215 break;
1216 default:
1217 ret = -EINVAL;
1218 break;
1219 }
Richard Genoud3102a762013-03-25 15:47:22 +01001220
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001221 if (ret < 0) {
Richard Genoud3102a762013-03-25 15:47:22 +01001222 goto unapply_new_state;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001223 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001224 }
1225
Richard Genoud3102a762013-03-25 15:47:22 +01001226 p->state = state;
1227
Stephen Warren7ecdb162012-03-02 13:05:45 -07001228 return 0;
Richard Genoud3102a762013-03-25 15:47:22 +01001229
1230unapply_new_state:
Richard Genoudda587512013-03-28 12:55:46 +01001231 dev_err(p->dev, "Error applying setting, reverse things back\n");
Richard Genoud3102a762013-03-25 15:47:22 +01001232
Richard Genoud3102a762013-03-25 15:47:22 +01001233 list_for_each_entry(setting2, &state->settings, node) {
1234 if (&setting2->node == &setting->node)
1235 break;
Richard Genoudaf606172013-03-29 10:03:26 +01001236 /*
1237 * All we can do here is pinmux_disable_setting.
1238 * That means that some pins are muxed differently now
1239 * than they were before applying the setting (We can't
1240 * "unmux a pin"!), but it's not a big deal since the pins
1241 * are free to be muxed by another apply_setting.
1242 */
1243 if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
1244 pinmux_disable_setting(setting2);
Richard Genoud3102a762013-03-25 15:47:22 +01001245 }
Richard Genoud8009d5f2013-03-28 12:55:47 +01001246
Richard Genoud385d9422013-03-29 10:03:27 +01001247 /* There's no infinite recursive loop here because p->state is NULL */
1248 if (old_state)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001249 pinctrl_select_state(p, old_state);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001250
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001251 return ret;
1252}
Stephen Warren6e5e9592012-03-02 13:05:47 -07001253EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001254
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001255static void devm_pinctrl_release(struct device *dev, void *res)
1256{
1257 pinctrl_put(*(struct pinctrl **)res);
1258}
1259
1260/**
1261 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
1262 * @dev: the device to obtain the handle for
1263 *
1264 * If there is a need to explicitly destroy the returned struct pinctrl,
1265 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1266 */
1267struct pinctrl *devm_pinctrl_get(struct device *dev)
1268{
1269 struct pinctrl **ptr, *p;
1270
1271 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
1272 if (!ptr)
1273 return ERR_PTR(-ENOMEM);
1274
1275 p = pinctrl_get(dev);
1276 if (!IS_ERR(p)) {
1277 *ptr = p;
1278 devres_add(dev, ptr);
1279 } else {
1280 devres_free(ptr);
1281 }
1282
1283 return p;
1284}
1285EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1286
1287static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1288{
1289 struct pinctrl **p = res;
1290
1291 return *p == data;
1292}
1293
1294/**
1295 * devm_pinctrl_put() - Resource managed pinctrl_put()
1296 * @p: the pinctrl handle to release
1297 *
1298 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1299 * this function will not need to be called and the resource management
1300 * code will ensure that the resource is freed.
1301 */
1302void devm_pinctrl_put(struct pinctrl *p)
1303{
Jingoo Hana72149e2013-02-26 11:34:07 +09001304 WARN_ON(devres_release(p->dev, devm_pinctrl_release,
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001305 devm_pinctrl_match, p));
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001306}
1307EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1308
Stephen Warren57291ce2012-03-23 10:29:46 -06001309int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
Doug Andersonc5272a22015-05-01 09:01:27 -07001310 bool dup)
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001311{
Stephen Warren1e2082b2012-03-02 13:05:48 -07001312 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001313 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001314
Masahiro Yamada7e9236f2015-05-28 21:52:59 +09001315 pr_debug("add %u pinctrl maps\n", num_maps);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001316
1317 /* First sanity check the new mapping */
1318 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001319 if (!maps[i].dev_name) {
1320 pr_err("failed to register map %s (%d): no device given\n",
1321 maps[i].name, i);
1322 return -EINVAL;
1323 }
1324
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001325 if (!maps[i].name) {
1326 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001327 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001328 return -EINVAL;
1329 }
1330
Stephen Warren1e2082b2012-03-02 13:05:48 -07001331 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1332 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001333 pr_err("failed to register map %s (%d): no pin control device given\n",
1334 maps[i].name, i);
1335 return -EINVAL;
1336 }
1337
Stephen Warren1e2082b2012-03-02 13:05:48 -07001338 switch (maps[i].type) {
1339 case PIN_MAP_TYPE_DUMMY_STATE:
1340 break;
1341 case PIN_MAP_TYPE_MUX_GROUP:
1342 ret = pinmux_validate_map(&maps[i], i);
1343 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001344 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001345 break;
1346 case PIN_MAP_TYPE_CONFIGS_PIN:
1347 case PIN_MAP_TYPE_CONFIGS_GROUP:
1348 ret = pinconf_validate_map(&maps[i], i);
1349 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001350 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001351 break;
1352 default:
1353 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001354 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -07001355 return -EINVAL;
1356 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001357 }
1358
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001359 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
1360 if (!maps_node) {
1361 pr_err("failed to alloc struct pinctrl_maps\n");
1362 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001363 }
1364
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001365 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -06001366 if (dup) {
1367 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
1368 GFP_KERNEL);
1369 if (!maps_node->maps) {
1370 pr_err("failed to duplicate mapping table\n");
1371 kfree(maps_node);
1372 return -ENOMEM;
1373 }
1374 } else {
1375 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001376 }
1377
Doug Andersonc5272a22015-05-01 09:01:27 -07001378 mutex_lock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001379 list_add_tail(&maps_node->node, &pinctrl_maps);
Doug Andersonc5272a22015-05-01 09:01:27 -07001380 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001381
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001382 return 0;
1383}
1384
Stephen Warren57291ce2012-03-23 10:29:46 -06001385/**
1386 * pinctrl_register_mappings() - register a set of pin controller mappings
1387 * @maps: the pincontrol mappings table to register. This should probably be
1388 * marked with __initdata so it can be discarded after boot. This
1389 * function will perform a shallow copy for the mapping entries.
1390 * @num_maps: the number of maps in the mapping table
1391 */
1392int pinctrl_register_mappings(struct pinctrl_map const *maps,
1393 unsigned num_maps)
1394{
Doug Andersonc5272a22015-05-01 09:01:27 -07001395 return pinctrl_register_map(maps, num_maps, true);
Stephen Warren57291ce2012-03-23 10:29:46 -06001396}
1397
1398void pinctrl_unregister_map(struct pinctrl_map const *map)
1399{
1400 struct pinctrl_maps *maps_node;
1401
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001402 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001403 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1404 if (maps_node->maps == map) {
1405 list_del(&maps_node->node);
Linus Walleijdb6c2c62013-07-23 01:43:20 +02001406 kfree(maps_node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001407 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001408 return;
1409 }
1410 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001411 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001412}
1413
Julien Delacou840a47b2012-12-10 14:47:33 +01001414/**
1415 * pinctrl_force_sleep() - turn a given controller device into sleep state
1416 * @pctldev: pin controller device
1417 */
1418int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1419{
1420 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
1421 return pinctrl_select_state(pctldev->p, pctldev->hog_sleep);
1422 return 0;
1423}
1424EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1425
1426/**
1427 * pinctrl_force_default() - turn a given controller device into default state
1428 * @pctldev: pin controller device
1429 */
1430int pinctrl_force_default(struct pinctrl_dev *pctldev)
1431{
1432 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
1433 return pinctrl_select_state(pctldev->p, pctldev->hog_default);
1434 return 0;
1435}
1436EXPORT_SYMBOL_GPL(pinctrl_force_default);
1437
Douglas Andersonef0eebc2015-10-20 21:15:06 -07001438/**
1439 * pinctrl_init_done() - tell pinctrl probe is done
1440 *
1441 * We'll use this time to switch the pins from "init" to "default" unless the
1442 * driver selected some other state.
1443 *
1444 * @dev: device to that's done probing
1445 */
1446int pinctrl_init_done(struct device *dev)
1447{
1448 struct dev_pin_info *pins = dev->pins;
1449 int ret;
1450
1451 if (!pins)
1452 return 0;
1453
1454 if (IS_ERR(pins->init_state))
1455 return 0; /* No such state */
1456
1457 if (pins->p->state != pins->init_state)
1458 return 0; /* Not at init anyway */
1459
1460 if (IS_ERR(pins->default_state))
1461 return 0; /* No default state */
1462
1463 ret = pinctrl_select_state(pins->p, pins->default_state);
1464 if (ret)
1465 dev_err(dev, "failed to activate default pinctrl state\n");
1466
1467 return ret;
1468}
1469
Linus Walleij14005ee2013-06-05 15:30:33 +02001470#ifdef CONFIG_PM
1471
1472/**
Tony Lindgrenf3333492013-07-18 08:15:04 -07001473 * pinctrl_pm_select_state() - select pinctrl state for PM
1474 * @dev: device to select default state for
1475 * @state: state to set
1476 */
1477static int pinctrl_pm_select_state(struct device *dev,
1478 struct pinctrl_state *state)
1479{
1480 struct dev_pin_info *pins = dev->pins;
1481 int ret;
1482
1483 if (IS_ERR(state))
1484 return 0; /* No such state */
1485 ret = pinctrl_select_state(pins->p, state);
1486 if (ret)
1487 dev_err(dev, "failed to activate pinctrl state %s\n",
1488 state->name);
1489 return ret;
1490}
1491
1492/**
Linus Walleij14005ee2013-06-05 15:30:33 +02001493 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1494 * @dev: device to select default state for
1495 */
1496int pinctrl_pm_select_default_state(struct device *dev)
1497{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001498 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001499 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001500
1501 return pinctrl_pm_select_state(dev, dev->pins->default_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001502}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001503EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001504
1505/**
1506 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1507 * @dev: device to select sleep state for
1508 */
1509int pinctrl_pm_select_sleep_state(struct device *dev)
1510{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001511 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001512 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001513
1514 return pinctrl_pm_select_state(dev, dev->pins->sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001515}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001516EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001517
1518/**
1519 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1520 * @dev: device to select idle state for
1521 */
1522int pinctrl_pm_select_idle_state(struct device *dev)
1523{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001524 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001525 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001526
1527 return pinctrl_pm_select_state(dev, dev->pins->idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001528}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001529EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001530#endif
1531
Linus Walleij2744e8a2011-05-02 20:50:54 +02001532#ifdef CONFIG_DEBUG_FS
1533
1534static int pinctrl_pins_show(struct seq_file *s, void *what)
1535{
1536 struct pinctrl_dev *pctldev = s->private;
1537 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001538 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001539
1540 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001541
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001542 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001543
Chanho Park706e8522012-01-03 16:47:50 +09001544 /* The pin number can be retrived from the pin controller descriptor */
1545 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001546 struct pin_desc *desc;
1547
Chanho Park706e8522012-01-03 16:47:50 +09001548 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001549 desc = pin_desc_get(pctldev, pin);
1550 /* Pin space may be sparse */
1551 if (desc == NULL)
1552 continue;
1553
Masahiro Yamadacf9d9942016-05-24 14:26:26 +09001554 seq_printf(s, "pin %d (%s) ", pin, desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001555
1556 /* Driver-specific info per pin */
1557 if (ops->pin_dbg_show)
1558 ops->pin_dbg_show(pctldev, s, pin);
1559
1560 seq_puts(s, "\n");
1561 }
1562
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001563 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001564
Linus Walleij2744e8a2011-05-02 20:50:54 +02001565 return 0;
1566}
1567
1568static int pinctrl_groups_show(struct seq_file *s, void *what)
1569{
1570 struct pinctrl_dev *pctldev = s->private;
1571 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301572 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001573
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001574 mutex_lock(&pctldev->mutex);
1575
Viresh Kumard1e90e92012-03-30 11:25:40 +05301576 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001577
Linus Walleij2744e8a2011-05-02 20:50:54 +02001578 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301579 while (selector < ngroups) {
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001580 const unsigned *pins = NULL;
1581 unsigned num_pins = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001582 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001583 const char *pname;
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001584 int ret = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001585 int i;
1586
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001587 if (ops->get_group_pins)
1588 ret = ops->get_group_pins(pctldev, selector,
1589 &pins, &num_pins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001590 if (ret)
1591 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1592 gname);
1593 else {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001594 seq_printf(s, "group: %s\n", gname);
1595 for (i = 0; i < num_pins; i++) {
1596 pname = pin_get_name(pctldev, pins[i]);
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001597 if (WARN_ON(!pname)) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001598 mutex_unlock(&pctldev->mutex);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001599 return -EINVAL;
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001600 }
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001601 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1602 }
1603 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001604 }
1605 selector++;
1606 }
1607
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001608 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001609
1610 return 0;
1611}
1612
1613static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1614{
1615 struct pinctrl_dev *pctldev = s->private;
1616 struct pinctrl_gpio_range *range = NULL;
1617
1618 seq_puts(s, "GPIO ranges handled:\n");
1619
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001620 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001621
Linus Walleij2744e8a2011-05-02 20:50:54 +02001622 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001623 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Christian Ruppertc8587ee2013-06-13 14:55:31 +02001624 if (range->pins) {
1625 int a;
1626 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
1627 range->id, range->name,
1628 range->base, (range->base + range->npins - 1));
1629 for (a = 0; a < range->npins - 1; a++)
1630 seq_printf(s, "%u, ", range->pins[a]);
1631 seq_printf(s, "%u}\n", range->pins[a]);
1632 }
1633 else
1634 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1635 range->id, range->name,
1636 range->base, (range->base + range->npins - 1),
1637 range->pin_base,
1638 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001639 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001640
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001641 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001642
1643 return 0;
1644}
1645
1646static int pinctrl_devices_show(struct seq_file *s, void *what)
1647{
1648 struct pinctrl_dev *pctldev;
1649
Linus Walleijae6b4d82011-10-19 18:14:33 +02001650 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001651
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001652 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001653
Linus Walleij2744e8a2011-05-02 20:50:54 +02001654 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1655 seq_printf(s, "%s ", pctldev->desc->name);
1656 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001657 seq_puts(s, "yes ");
1658 else
1659 seq_puts(s, "no ");
1660 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001661 seq_puts(s, "yes");
1662 else
1663 seq_puts(s, "no");
1664 seq_puts(s, "\n");
1665 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001666
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001667 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001668
1669 return 0;
1670}
1671
Stephen Warren1e2082b2012-03-02 13:05:48 -07001672static inline const char *map_type(enum pinctrl_map_type type)
1673{
1674 static const char * const names[] = {
1675 "INVALID",
1676 "DUMMY_STATE",
1677 "MUX_GROUP",
1678 "CONFIGS_PIN",
1679 "CONFIGS_GROUP",
1680 };
1681
1682 if (type >= ARRAY_SIZE(names))
1683 return "UNKNOWN";
1684
1685 return names[type];
1686}
1687
Stephen Warren3eedb432012-02-23 17:04:40 -07001688static int pinctrl_maps_show(struct seq_file *s, void *what)
1689{
1690 struct pinctrl_maps *maps_node;
1691 int i;
1692 struct pinctrl_map const *map;
1693
1694 seq_puts(s, "Pinctrl maps:\n");
1695
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001696 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001697 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001698 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1699 map->dev_name, map->name, map_type(map->type),
1700 map->type);
1701
1702 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1703 seq_printf(s, "controlling device %s\n",
1704 map->ctrl_dev_name);
1705
1706 switch (map->type) {
1707 case PIN_MAP_TYPE_MUX_GROUP:
1708 pinmux_show_map(s, map);
1709 break;
1710 case PIN_MAP_TYPE_CONFIGS_PIN:
1711 case PIN_MAP_TYPE_CONFIGS_GROUP:
1712 pinconf_show_map(s, map);
1713 break;
1714 default:
1715 break;
1716 }
1717
1718 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001719 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001720 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001721
1722 return 0;
1723}
1724
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001725static int pinctrl_show(struct seq_file *s, void *what)
1726{
1727 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001728 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001729 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001730
1731 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001732
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001733 mutex_lock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001734
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001735 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001736 seq_printf(s, "device: %s current state: %s\n",
1737 dev_name(p->dev),
1738 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001739
Stephen Warren6e5e9592012-03-02 13:05:47 -07001740 list_for_each_entry(state, &p->states, node) {
1741 seq_printf(s, " state: %s\n", state->name);
1742
1743 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001744 struct pinctrl_dev *pctldev = setting->pctldev;
1745
1746 seq_printf(s, " type: %s controller %s ",
1747 map_type(setting->type),
1748 pinctrl_dev_get_name(pctldev));
1749
1750 switch (setting->type) {
1751 case PIN_MAP_TYPE_MUX_GROUP:
1752 pinmux_show_setting(s, setting);
1753 break;
1754 case PIN_MAP_TYPE_CONFIGS_PIN:
1755 case PIN_MAP_TYPE_CONFIGS_GROUP:
1756 pinconf_show_setting(s, setting);
1757 break;
1758 default:
1759 break;
1760 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001761 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001762 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001763 }
1764
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001765 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001766
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001767 return 0;
1768}
1769
Linus Walleij2744e8a2011-05-02 20:50:54 +02001770static int pinctrl_pins_open(struct inode *inode, struct file *file)
1771{
1772 return single_open(file, pinctrl_pins_show, inode->i_private);
1773}
1774
1775static int pinctrl_groups_open(struct inode *inode, struct file *file)
1776{
1777 return single_open(file, pinctrl_groups_show, inode->i_private);
1778}
1779
1780static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1781{
1782 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1783}
1784
1785static int pinctrl_devices_open(struct inode *inode, struct file *file)
1786{
1787 return single_open(file, pinctrl_devices_show, NULL);
1788}
1789
Stephen Warren3eedb432012-02-23 17:04:40 -07001790static int pinctrl_maps_open(struct inode *inode, struct file *file)
1791{
1792 return single_open(file, pinctrl_maps_show, NULL);
1793}
1794
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001795static int pinctrl_open(struct inode *inode, struct file *file)
1796{
1797 return single_open(file, pinctrl_show, NULL);
1798}
1799
Linus Walleij2744e8a2011-05-02 20:50:54 +02001800static const struct file_operations pinctrl_pins_ops = {
1801 .open = pinctrl_pins_open,
1802 .read = seq_read,
1803 .llseek = seq_lseek,
1804 .release = single_release,
1805};
1806
1807static const struct file_operations pinctrl_groups_ops = {
1808 .open = pinctrl_groups_open,
1809 .read = seq_read,
1810 .llseek = seq_lseek,
1811 .release = single_release,
1812};
1813
1814static const struct file_operations pinctrl_gpioranges_ops = {
1815 .open = pinctrl_gpioranges_open,
1816 .read = seq_read,
1817 .llseek = seq_lseek,
1818 .release = single_release,
1819};
1820
1821static const struct file_operations pinctrl_devices_ops = {
1822 .open = pinctrl_devices_open,
1823 .read = seq_read,
1824 .llseek = seq_lseek,
1825 .release = single_release,
1826};
1827
Stephen Warren3eedb432012-02-23 17:04:40 -07001828static const struct file_operations pinctrl_maps_ops = {
1829 .open = pinctrl_maps_open,
1830 .read = seq_read,
1831 .llseek = seq_lseek,
1832 .release = single_release,
1833};
1834
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001835static const struct file_operations pinctrl_ops = {
1836 .open = pinctrl_open,
1837 .read = seq_read,
1838 .llseek = seq_lseek,
1839 .release = single_release,
1840};
1841
Linus Walleij2744e8a2011-05-02 20:50:54 +02001842static struct dentry *debugfs_root;
1843
1844static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1845{
Tony Lindgren02157162012-01-20 08:17:22 -08001846 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001847
Stephen Warren51cd24e2011-12-09 16:59:05 -07001848 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001849 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001850 pctldev->device_root = device_root;
1851
Linus Walleij2744e8a2011-05-02 20:50:54 +02001852 if (IS_ERR(device_root) || !device_root) {
1853 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001854 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001855 return;
1856 }
1857 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1858 device_root, pctldev, &pinctrl_pins_ops);
1859 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1860 device_root, pctldev, &pinctrl_groups_ops);
1861 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1862 device_root, pctldev, &pinctrl_gpioranges_ops);
Florian Vaussarde7f2a442014-02-05 07:51:22 +01001863 if (pctldev->desc->pmxops)
1864 pinmux_init_device_debugfs(device_root, pctldev);
1865 if (pctldev->desc->confops)
1866 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001867}
1868
Tony Lindgren02157162012-01-20 08:17:22 -08001869static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1870{
1871 debugfs_remove_recursive(pctldev->device_root);
1872}
1873
Linus Walleij2744e8a2011-05-02 20:50:54 +02001874static void pinctrl_init_debugfs(void)
1875{
1876 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1877 if (IS_ERR(debugfs_root) || !debugfs_root) {
1878 pr_warn("failed to create debugfs directory\n");
1879 debugfs_root = NULL;
1880 return;
1881 }
1882
1883 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1884 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001885 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1886 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001887 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1888 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001889}
1890
1891#else /* CONFIG_DEBUG_FS */
1892
1893static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1894{
1895}
1896
1897static void pinctrl_init_debugfs(void)
1898{
1899}
1900
Tony Lindgren02157162012-01-20 08:17:22 -08001901static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1902{
1903}
1904
Linus Walleij2744e8a2011-05-02 20:50:54 +02001905#endif
1906
Stephen Warrend26bc492012-03-16 14:54:25 -06001907static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1908{
1909 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1910
1911 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301912 !ops->get_groups_count ||
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001913 !ops->get_group_name)
Stephen Warrend26bc492012-03-16 14:54:25 -06001914 return -EINVAL;
1915
Stephen Warren57291ce2012-03-23 10:29:46 -06001916 if (ops->dt_node_to_map && !ops->dt_free_map)
1917 return -EINVAL;
1918
Stephen Warrend26bc492012-03-16 14:54:25 -06001919 return 0;
1920}
1921
Linus Walleij2744e8a2011-05-02 20:50:54 +02001922/**
Tony Lindgren99e4f672016-12-27 09:19:59 -08001923 * pinctrl_late_init() - finish pin controller device registration
1924 * @work: work struct
1925 */
1926static void pinctrl_late_init(struct work_struct *work)
1927{
1928 struct pinctrl_dev *pctldev;
1929
1930 pctldev = container_of(work, struct pinctrl_dev, late_init.work);
1931
Linus Walleij2d22e5b2016-12-30 14:44:18 +01001932 /*
1933 * If the pin controller does NOT have hogs, this will report an
1934 * error and we skip over this entire branch. This is why we can
1935 * call this function directly when we do not have hogs on the
1936 * device.
1937 */
Tony Lindgren99e4f672016-12-27 09:19:59 -08001938 pctldev->p = create_pinctrl(pctldev->dev, pctldev);
1939 if (!IS_ERR(pctldev->p)) {
1940 kref_get(&pctldev->p->users);
1941 pctldev->hog_default =
1942 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
1943 if (IS_ERR(pctldev->hog_default)) {
1944 dev_dbg(pctldev->dev,
1945 "failed to lookup the default state\n");
1946 } else {
1947 if (pinctrl_select_state(pctldev->p,
1948 pctldev->hog_default))
1949 dev_err(pctldev->dev,
1950 "failed to select default state\n");
1951 }
1952
1953 pctldev->hog_sleep =
1954 pinctrl_lookup_state(pctldev->p,
1955 PINCTRL_STATE_SLEEP);
1956 if (IS_ERR(pctldev->hog_sleep))
1957 dev_dbg(pctldev->dev,
1958 "failed to lookup the sleep state\n");
1959 }
1960
1961 mutex_lock(&pinctrldev_list_mutex);
1962 list_add_tail(&pctldev->node, &pinctrldev_list);
1963 mutex_unlock(&pinctrldev_list_mutex);
1964
1965 pinctrl_init_device_debugfs(pctldev);
1966}
1967
1968/**
Linus Walleij2744e8a2011-05-02 20:50:54 +02001969 * pinctrl_register() - register a pin controller device
1970 * @pctldesc: descriptor for this pin controller
1971 * @dev: parent device for this pin controller
1972 * @driver_data: private pin controller data for this pin controller
1973 */
1974struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1975 struct device *dev, void *driver_data)
1976{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001977 struct pinctrl_dev *pctldev;
1978 int ret;
1979
Devendra Nagada9aecb2012-06-16 23:43:16 +05301980 if (!pctldesc)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001981 return ERR_PTR(-EINVAL);
Devendra Nagada9aecb2012-06-16 23:43:16 +05301982 if (!pctldesc->name)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001983 return ERR_PTR(-EINVAL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001984
Stephen Warren02f5b982012-02-22 14:26:00 -07001985 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001986 if (pctldev == NULL) {
1987 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001988 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001989 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001990
1991 /* Initialize pin control device struct */
1992 pctldev->owner = pctldesc->owner;
1993 pctldev->desc = pctldesc;
1994 pctldev->driver_data = driver_data;
1995 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleijc033a712016-12-30 15:04:43 +01001996#ifdef CONFIG_GENERIC_PINCTRL_GROUPS
Tony Lindgrenc7059c52016-12-27 09:20:00 -08001997 INIT_RADIX_TREE(&pctldev->pin_group_tree, GFP_KERNEL);
Linus Walleijc033a712016-12-30 15:04:43 +01001998#endif
Tony Lindgrena76edc82016-12-27 09:20:01 -08001999#ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
2000 INIT_RADIX_TREE(&pctldev->pin_function_tree, GFP_KERNEL);
2001#endif
Linus Walleij2744e8a2011-05-02 20:50:54 +02002002 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Tony Lindgren99e4f672016-12-27 09:19:59 -08002003 INIT_DELAYED_WORK(&pctldev->late_init, pinctrl_late_init);
Stephen Warren51cd24e2011-12-09 16:59:05 -07002004 pctldev->dev = dev;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002005 mutex_init(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002006
Stephen Warrend26bc492012-03-16 14:54:25 -06002007 /* check core ops for sanity */
Masahiro Yamada323de9e2015-06-09 13:01:16 +09002008 ret = pinctrl_check_ops(pctldev);
2009 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02002010 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06002011 goto out_err;
2012 }
2013
Tony Lindgrenb9130b72012-01-24 16:28:08 -08002014 /* If we're implementing pinmuxing, check the ops for sanity */
2015 if (pctldesc->pmxops) {
Masahiro Yamada323de9e2015-06-09 13:01:16 +09002016 ret = pinmux_check_ops(pctldev);
2017 if (ret)
Tony Lindgrenb9130b72012-01-24 16:28:08 -08002018 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08002019 }
2020
2021 /* If we're implementing pinconfig, check the ops for sanity */
2022 if (pctldesc->confops) {
Masahiro Yamada323de9e2015-06-09 13:01:16 +09002023 ret = pinconf_check_ops(pctldev);
2024 if (ret)
Tony Lindgrenb9130b72012-01-24 16:28:08 -08002025 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08002026 }
2027
Linus Walleij2744e8a2011-05-02 20:50:54 +02002028 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02002029 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002030 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
2031 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02002032 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02002033 pinctrl_free_pindescs(pctldev, pctldesc->pins,
2034 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07002035 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02002036 }
2037
Linus Walleij2d22e5b2016-12-30 14:44:18 +01002038 /*
2039 * If the device has hogs we want the probe() function of the driver
2040 * to complete before we go in and hog them and add the pin controller
2041 * to the list of controllers. If it has no hogs, we can just complete
2042 * the registration immediately.
2043 */
Tony Lindgren99e4f672016-12-27 09:19:59 -08002044 if (pinctrl_dt_has_hogs(pctldev))
2045 schedule_delayed_work(&pctldev->late_init, 0);
2046 else
2047 pinctrl_late_init(&pctldev->late_init.work);
Stephen Warren2304b472012-02-22 14:26:01 -07002048
Linus Walleij2744e8a2011-05-02 20:50:54 +02002049 return pctldev;
2050
Stephen Warren51cd24e2011-12-09 16:59:05 -07002051out_err:
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002052 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07002053 kfree(pctldev);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09002054 return ERR_PTR(ret);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002055}
2056EXPORT_SYMBOL_GPL(pinctrl_register);
2057
2058/**
2059 * pinctrl_unregister() - unregister pinmux
2060 * @pctldev: pin controller to unregister
2061 *
2062 * Called by pinmux drivers to unregister a pinmux.
2063 */
2064void pinctrl_unregister(struct pinctrl_dev *pctldev)
2065{
Dong Aisheng5d589b02012-05-23 21:22:40 +08002066 struct pinctrl_gpio_range *range, *n;
Jon Hunter3429fb32017-01-05 15:52:55 +00002067 struct pinctrl_dev *p, *p1;
2068
Linus Walleij2744e8a2011-05-02 20:50:54 +02002069 if (pctldev == NULL)
2070 return;
2071
Tony Lindgren99e4f672016-12-27 09:19:59 -08002072 cancel_delayed_work_sync(&pctldev->late_init);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002073 mutex_lock(&pctldev->mutex);
Tony Lindgren02157162012-01-20 08:17:22 -08002074 pinctrl_remove_device_debugfs(pctldev);
Jim Lindb93fac2015-01-08 20:25:05 +08002075 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07002076
Jon Hunter3429fb32017-01-05 15:52:55 +00002077 if (!IS_ERR_OR_NULL(pctldev->p))
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002078 pinctrl_put(pctldev->p);
Stephen Warren57b676f2012-03-02 13:05:44 -07002079
Jim Lindb93fac2015-01-08 20:25:05 +08002080 mutex_lock(&pinctrldev_list_mutex);
2081 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002082 /* TODO: check that no pinmuxes are still active? */
Jon Hunter3429fb32017-01-05 15:52:55 +00002083 list_for_each_entry_safe(p, p1, &pinctrldev_list, node)
2084 if (p == pctldev)
2085 list_del(&p->node);
Tony Lindgrena76edc82016-12-27 09:20:01 -08002086 pinmux_generic_free_functions(pctldev);
Tony Lindgrenc7059c52016-12-27 09:20:00 -08002087 pinctrl_generic_free_groups(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002088 /* Destroy descriptor tree */
2089 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
2090 pctldev->desc->npins);
Dong Aisheng5d589b02012-05-23 21:22:40 +08002091 /* remove gpio ranges map */
2092 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
2093 list_del(&range->node);
2094
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002095 mutex_unlock(&pctldev->mutex);
2096 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07002097 kfree(pctldev);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002098 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002099}
2100EXPORT_SYMBOL_GPL(pinctrl_unregister);
2101
Laxman Dewangan80e0f8d2016-02-24 14:12:59 +05302102static void devm_pinctrl_dev_release(struct device *dev, void *res)
2103{
2104 struct pinctrl_dev *pctldev = *(struct pinctrl_dev **)res;
2105
2106 pinctrl_unregister(pctldev);
2107}
2108
2109static int devm_pinctrl_dev_match(struct device *dev, void *res, void *data)
2110{
2111 struct pctldev **r = res;
2112
Laxman Dewangan3024f922016-02-24 14:44:07 +05302113 if (WARN_ON(!r || !*r))
Laxman Dewangan80e0f8d2016-02-24 14:12:59 +05302114 return 0;
2115
2116 return *r == data;
2117}
2118
2119/**
2120 * devm_pinctrl_register() - Resource managed version of pinctrl_register().
2121 * @dev: parent device for this pin controller
2122 * @pctldesc: descriptor for this pin controller
2123 * @driver_data: private pin controller data for this pin controller
2124 *
2125 * Returns an error pointer if pincontrol register failed. Otherwise
2126 * it returns valid pinctrl handle.
2127 *
2128 * The pinctrl device will be automatically released when the device is unbound.
2129 */
2130struct pinctrl_dev *devm_pinctrl_register(struct device *dev,
2131 struct pinctrl_desc *pctldesc,
2132 void *driver_data)
2133{
2134 struct pinctrl_dev **ptr, *pctldev;
2135
2136 ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
2137 if (!ptr)
2138 return ERR_PTR(-ENOMEM);
2139
2140 pctldev = pinctrl_register(pctldesc, dev, driver_data);
2141 if (IS_ERR(pctldev)) {
2142 devres_free(ptr);
2143 return pctldev;
2144 }
2145
2146 *ptr = pctldev;
2147 devres_add(dev, ptr);
2148
2149 return pctldev;
2150}
2151EXPORT_SYMBOL_GPL(devm_pinctrl_register);
2152
2153/**
2154 * devm_pinctrl_unregister() - Resource managed version of pinctrl_unregister().
2155 * @dev: device for which which resource was allocated
2156 * @pctldev: the pinctrl device to unregister.
2157 */
2158void devm_pinctrl_unregister(struct device *dev, struct pinctrl_dev *pctldev)
2159{
2160 WARN_ON(devres_release(dev, devm_pinctrl_dev_release,
2161 devm_pinctrl_dev_match, pctldev));
2162}
2163EXPORT_SYMBOL_GPL(devm_pinctrl_unregister);
2164
Linus Walleij2744e8a2011-05-02 20:50:54 +02002165static int __init pinctrl_init(void)
2166{
2167 pr_info("initialized pinctrl subsystem\n");
2168 pinctrl_init_debugfs();
2169 return 0;
2170}
2171
2172/* init early since many drivers really need to initialized pinmux early */
2173core_initcall(pinctrl_init);