blob: 735d8f7f9d716a98870160de82bd583cf897c12d [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
543/**
Linus Walleij7afde8b2011-10-19 17:07:16 +0200544 * pinctrl_get_group_selector() - returns the group selector for a group
545 * @pctldev: the pin controller handling the group
546 * @pin_group: the pin group to look up
547 */
548int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
549 const char *pin_group)
550{
551 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530552 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200553 unsigned group_selector = 0;
554
Viresh Kumard1e90e92012-03-30 11:25:40 +0530555 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200556 const char *gname = pctlops->get_group_name(pctldev,
557 group_selector);
558 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700559 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200560 "found group selector %u for %s\n",
561 group_selector,
562 pin_group);
563 return group_selector;
564 }
565
566 group_selector++;
567 }
568
Stephen Warren51cd24e2011-12-09 16:59:05 -0700569 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200570 pin_group);
571
572 return -EINVAL;
573}
574
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100575/**
Geert Uytterhoevenb217e432015-05-04 19:46:57 +0200576 * pinctrl_request_gpio() - request a single pin to be used as GPIO
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100577 * @gpio: the GPIO pin number from the GPIO subsystem number space
578 *
579 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
580 * as part of their gpio_request() semantics, platforms and individual drivers
581 * shall *NOT* request GPIO pins to be muxed in.
582 */
583int pinctrl_request_gpio(unsigned gpio)
584{
585 struct pinctrl_dev *pctldev;
586 struct pinctrl_gpio_range *range;
587 int ret;
588 int pin;
589
590 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700591 if (ret) {
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800592 if (pinctrl_ready_for_gpio_range(gpio))
593 ret = 0;
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800594 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700595 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100596
Axel Lin9b77ace2013-08-19 10:07:46 +0800597 mutex_lock(&pctldev->mutex);
598
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100599 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200600 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100601
Stephen Warren57b676f2012-03-02 13:05:44 -0700602 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
603
Axel Lin9b77ace2013-08-19 10:07:46 +0800604 mutex_unlock(&pctldev->mutex);
605
Stephen Warren57b676f2012-03-02 13:05:44 -0700606 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100607}
608EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
609
610/**
611 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
612 * @gpio: the GPIO pin number from the GPIO subsystem number space
613 *
614 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
615 * as part of their gpio_free() semantics, platforms and individual drivers
616 * shall *NOT* request GPIO pins to be muxed out.
617 */
618void pinctrl_free_gpio(unsigned gpio)
619{
620 struct pinctrl_dev *pctldev;
621 struct pinctrl_gpio_range *range;
622 int ret;
623 int pin;
624
625 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700626 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100627 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700628 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200629 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100630
631 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200632 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100633
Stephen Warren57b676f2012-03-02 13:05:44 -0700634 pinmux_free_gpio(pctldev, pin, range);
635
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200636 mutex_unlock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100637}
638EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
639
640static int pinctrl_gpio_direction(unsigned gpio, bool input)
641{
642 struct pinctrl_dev *pctldev;
643 struct pinctrl_gpio_range *range;
644 int ret;
645 int pin;
646
647 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200648 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100649 return ret;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200650 }
651
652 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100653
654 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200655 pin = gpio_to_pin(range, gpio);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200656 ret = pinmux_gpio_direction(pctldev, range, pin, input);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100657
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200658 mutex_unlock(&pctldev->mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200659
660 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100661}
662
663/**
664 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
665 * @gpio: the GPIO pin number from the GPIO subsystem number space
666 *
667 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
668 * as part of their gpio_direction_input() semantics, platforms and individual
669 * drivers shall *NOT* touch pin control GPIO calls.
670 */
671int pinctrl_gpio_direction_input(unsigned gpio)
672{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200673 return pinctrl_gpio_direction(gpio, true);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100674}
675EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
676
677/**
678 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
679 * @gpio: the GPIO pin number from the GPIO subsystem number space
680 *
681 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
682 * as part of their gpio_direction_output() semantics, platforms and individual
683 * drivers shall *NOT* touch pin control GPIO calls.
684 */
685int pinctrl_gpio_direction_output(unsigned gpio)
686{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200687 return pinctrl_gpio_direction(gpio, false);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100688}
689EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
690
Stephen Warren6e5e9592012-03-02 13:05:47 -0700691static struct pinctrl_state *find_state(struct pinctrl *p,
692 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100693{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700694 struct pinctrl_state *state;
695
696 list_for_each_entry(state, &p->states, node)
697 if (!strcmp(state->name, name))
698 return state;
699
700 return NULL;
701}
702
703static struct pinctrl_state *create_state(struct pinctrl *p,
704 const char *name)
705{
706 struct pinctrl_state *state;
707
708 state = kzalloc(sizeof(*state), GFP_KERNEL);
709 if (state == NULL) {
710 dev_err(p->dev,
711 "failed to alloc struct pinctrl_state\n");
712 return ERR_PTR(-ENOMEM);
713 }
714
715 state->name = name;
716 INIT_LIST_HEAD(&state->settings);
717
718 list_add_tail(&state->node, &p->states);
719
720 return state;
721}
722
723static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
724{
725 struct pinctrl_state *state;
726 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700727 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700728
729 state = find_state(p, map->name);
730 if (!state)
731 state = create_state(p, map->name);
732 if (IS_ERR(state))
733 return PTR_ERR(state);
734
Stephen Warren1e2082b2012-03-02 13:05:48 -0700735 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
736 return 0;
737
Stephen Warren6e5e9592012-03-02 13:05:47 -0700738 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
739 if (setting == NULL) {
740 dev_err(p->dev,
741 "failed to alloc struct pinctrl_setting\n");
742 return -ENOMEM;
743 }
744
Stephen Warren1e2082b2012-03-02 13:05:48 -0700745 setting->type = map->type;
746
Stephen Warren6e5e9592012-03-02 13:05:47 -0700747 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
748 if (setting->pctldev == NULL) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700749 kfree(setting);
Linus Walleij89216492012-12-11 14:14:32 +0100750 /* Do not defer probing of hogs (circular loop) */
751 if (!strcmp(map->ctrl_dev_name, map->dev_name))
752 return -ENODEV;
Linus Walleijc05127c2012-04-10 10:00:38 +0200753 /*
754 * OK let us guess that the driver is not there yet, and
755 * let's defer obtaining this pinctrl handle to later...
756 */
Linus Walleij89216492012-12-11 14:14:32 +0100757 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
758 map->ctrl_dev_name);
Linus Walleijc05127c2012-04-10 10:00:38 +0200759 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700760 }
761
Linus Walleij1a789582012-10-17 20:51:54 +0200762 setting->dev_name = map->dev_name;
763
Stephen Warren1e2082b2012-03-02 13:05:48 -0700764 switch (map->type) {
765 case PIN_MAP_TYPE_MUX_GROUP:
766 ret = pinmux_map_to_setting(map, setting);
767 break;
768 case PIN_MAP_TYPE_CONFIGS_PIN:
769 case PIN_MAP_TYPE_CONFIGS_GROUP:
770 ret = pinconf_map_to_setting(map, setting);
771 break;
772 default:
773 ret = -EINVAL;
774 break;
775 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700776 if (ret < 0) {
777 kfree(setting);
778 return ret;
779 }
780
781 list_add_tail(&setting->node, &state->settings);
782
783 return 0;
784}
785
786static struct pinctrl *find_pinctrl(struct device *dev)
787{
788 struct pinctrl *p;
789
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200790 mutex_lock(&pinctrl_list_mutex);
Stephen Warren1e2082b2012-03-02 13:05:48 -0700791 list_for_each_entry(p, &pinctrl_list, node)
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200792 if (p->dev == dev) {
793 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700794 return p;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200795 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700796
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200797 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700798 return NULL;
799}
800
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200801static void pinctrl_free(struct pinctrl *p, bool inlist);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700802
803static struct pinctrl *create_pinctrl(struct device *dev)
804{
805 struct pinctrl *p;
806 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700807 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100808 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700809 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700810 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100811
812 /*
813 * create the state cookie holder struct pinctrl for each
814 * mapping, this is what consumers will get when requesting
815 * a pin control handle with pinctrl_get()
816 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700817 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700818 if (p == NULL) {
819 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100820 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700821 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700822 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700823 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -0600824 INIT_LIST_HEAD(&p->dt_maps);
825
826 ret = pinctrl_dt_to_map(p);
827 if (ret < 0) {
828 kfree(p);
829 return ERR_PTR(ret);
830 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700831
832 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100833
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200834 mutex_lock(&pinctrl_maps_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100835 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700836 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -0700837 /* Map must be for this device */
838 if (strcmp(map->dev_name, devname))
839 continue;
840
Stephen Warren6e5e9592012-03-02 13:05:47 -0700841 ret = add_setting(p, map);
Linus Walleij89216492012-12-11 14:14:32 +0100842 /*
843 * At this point the adding of a setting may:
844 *
845 * - Defer, if the pinctrl device is not yet available
846 * - Fail, if the pinctrl device is not yet available,
847 * AND the setting is a hog. We cannot defer that, since
848 * the hog will kick in immediately after the device
849 * is registered.
850 *
851 * If the error returned was not -EPROBE_DEFER then we
852 * accumulate the errors to see if we end up with
853 * an -EPROBE_DEFER later, as that is the worst case.
854 */
855 if (ret == -EPROBE_DEFER) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200856 pinctrl_free(p, false);
857 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700858 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100859 }
860 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200861 mutex_unlock(&pinctrl_maps_mutex);
862
Linus Walleij89216492012-12-11 14:14:32 +0100863 if (ret < 0) {
864 /* If some other error than deferral occured, return here */
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200865 pinctrl_free(p, false);
Linus Walleij89216492012-12-11 14:14:32 +0100866 return ERR_PTR(ret);
867 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100868
Linus Walleijab780292013-01-22 10:56:14 -0700869 kref_init(&p->users);
870
Linus Walleijb0666ba2012-12-11 13:12:35 +0100871 /* Add the pinctrl handle to the global list */
Stanislaw Gruszka7b320cb2014-02-04 09:07:09 +0100872 mutex_lock(&pinctrl_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700873 list_add_tail(&p->node, &pinctrl_list);
Stanislaw Gruszka7b320cb2014-02-04 09:07:09 +0100874 mutex_unlock(&pinctrl_list_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100875
876 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700877}
Stephen Warren7ecdb162012-03-02 13:05:45 -0700878
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200879/**
880 * pinctrl_get() - retrieves the pinctrl handle for a device
881 * @dev: the device to obtain the handle for
882 */
883struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700884{
885 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700886
Stephen Warren6e5e9592012-03-02 13:05:47 -0700887 if (WARN_ON(!dev))
888 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700889
Linus Walleijab780292013-01-22 10:56:14 -0700890 /*
891 * See if somebody else (such as the device core) has already
892 * obtained a handle to the pinctrl for this device. In that case,
893 * return another pointer to it.
894 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700895 p = find_pinctrl(dev);
Linus Walleijab780292013-01-22 10:56:14 -0700896 if (p != NULL) {
897 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
898 kref_get(&p->users);
899 return p;
900 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700901
Richard Genoudd599bfb2012-08-10 16:53:49 +0200902 return create_pinctrl(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100903}
904EXPORT_SYMBOL_GPL(pinctrl_get);
905
Richard Genoudd3cee8302013-03-25 15:47:21 +0100906static void pinctrl_free_setting(bool disable_setting,
907 struct pinctrl_setting *setting)
908{
909 switch (setting->type) {
910 case PIN_MAP_TYPE_MUX_GROUP:
911 if (disable_setting)
912 pinmux_disable_setting(setting);
913 pinmux_free_setting(setting);
914 break;
915 case PIN_MAP_TYPE_CONFIGS_PIN:
916 case PIN_MAP_TYPE_CONFIGS_GROUP:
917 pinconf_free_setting(setting);
918 break;
919 default:
920 break;
921 }
922}
923
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200924static void pinctrl_free(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -0700925{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700926 struct pinctrl_state *state, *n1;
927 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700928
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200929 mutex_lock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700930 list_for_each_entry_safe(state, n1, &p->states, node) {
931 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Richard Genoudd3cee8302013-03-25 15:47:21 +0100932 pinctrl_free_setting(state == p->state, setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700933 list_del(&setting->node);
934 kfree(setting);
935 }
936 list_del(&state->node);
937 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700938 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700939
Stephen Warren57291ce2012-03-23 10:29:46 -0600940 pinctrl_dt_free_maps(p);
941
Stephen Warren6e5e9592012-03-02 13:05:47 -0700942 if (inlist)
943 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700944 kfree(p);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200945 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700946}
947
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100948/**
Linus Walleijab780292013-01-22 10:56:14 -0700949 * pinctrl_release() - release the pinctrl handle
950 * @kref: the kref in the pinctrl being released
951 */
Sachin Kamat2917e832013-01-24 15:01:00 +0530952static void pinctrl_release(struct kref *kref)
Linus Walleijab780292013-01-22 10:56:14 -0700953{
954 struct pinctrl *p = container_of(kref, struct pinctrl, users);
955
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200956 pinctrl_free(p, true);
Linus Walleijab780292013-01-22 10:56:14 -0700957}
958
959/**
960 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
Stephen Warren6e5e9592012-03-02 13:05:47 -0700961 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100962 */
963void pinctrl_put(struct pinctrl *p)
964{
Linus Walleijab780292013-01-22 10:56:14 -0700965 kref_put(&p->users, pinctrl_release);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100966}
967EXPORT_SYMBOL_GPL(pinctrl_put);
968
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200969/**
970 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
971 * @p: the pinctrl handle to retrieve the state from
972 * @name: the state name to retrieve
973 */
974struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
975 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -0700976{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700977 struct pinctrl_state *state;
978
979 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800980 if (!state) {
981 if (pinctrl_dummy_state) {
982 /* create dummy state */
983 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
984 name);
985 state = create_state(p, name);
Richard Genoudd599bfb2012-08-10 16:53:49 +0200986 } else
987 state = ERR_PTR(-ENODEV);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800988 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700989
990 return state;
991}
Stephen Warren6e5e9592012-03-02 13:05:47 -0700992EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
993
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200994/**
Florian Fainellibd6552c2017-03-01 10:32:57 -0800995 * pinctrl_commit_state() - select/activate/program a pinctrl state to HW
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200996 * @p: the pinctrl handle for the device that requests configuration
997 * @state: the state handle to select/activate/program
998 */
Florian Fainellibd6552c2017-03-01 10:32:57 -0800999static int pinctrl_commit_state(struct pinctrl *p, struct pinctrl_state *state)
Stephen Warren6e5e9592012-03-02 13:05:47 -07001000{
1001 struct pinctrl_setting *setting, *setting2;
Richard Genoud50cf7c82013-03-25 15:47:23 +01001002 struct pinctrl_state *old_state = p->state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001003 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -07001004
Stephen Warren6e5e9592012-03-02 13:05:47 -07001005 if (p->state) {
1006 /*
Fan Wu2243a872014-06-09 09:37:56 +08001007 * For each pinmux setting in the old state, forget SW's record
1008 * of mux owner for that pingroup. Any pingroups which are
1009 * still owned by the new state will be re-acquired by the call
1010 * to pinmux_enable_setting() in the loop below.
Stephen Warren6e5e9592012-03-02 13:05:47 -07001011 */
1012 list_for_each_entry(setting, &p->state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001013 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
1014 continue;
Fan Wu2243a872014-06-09 09:37:56 +08001015 pinmux_disable_setting(setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001016 }
1017 }
1018
Richard Genoud3102a762013-03-25 15:47:22 +01001019 p->state = NULL;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001020
1021 /* Apply all the settings for the new state */
1022 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001023 switch (setting->type) {
1024 case PIN_MAP_TYPE_MUX_GROUP:
1025 ret = pinmux_enable_setting(setting);
1026 break;
1027 case PIN_MAP_TYPE_CONFIGS_PIN:
1028 case PIN_MAP_TYPE_CONFIGS_GROUP:
1029 ret = pinconf_apply_setting(setting);
1030 break;
1031 default:
1032 ret = -EINVAL;
1033 break;
1034 }
Richard Genoud3102a762013-03-25 15:47:22 +01001035
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001036 if (ret < 0) {
Richard Genoud3102a762013-03-25 15:47:22 +01001037 goto unapply_new_state;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001038 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001039 }
1040
Richard Genoud3102a762013-03-25 15:47:22 +01001041 p->state = state;
1042
Stephen Warren7ecdb162012-03-02 13:05:45 -07001043 return 0;
Richard Genoud3102a762013-03-25 15:47:22 +01001044
1045unapply_new_state:
Richard Genoudda587512013-03-28 12:55:46 +01001046 dev_err(p->dev, "Error applying setting, reverse things back\n");
Richard Genoud3102a762013-03-25 15:47:22 +01001047
Richard Genoud3102a762013-03-25 15:47:22 +01001048 list_for_each_entry(setting2, &state->settings, node) {
1049 if (&setting2->node == &setting->node)
1050 break;
Richard Genoudaf606172013-03-29 10:03:26 +01001051 /*
1052 * All we can do here is pinmux_disable_setting.
1053 * That means that some pins are muxed differently now
1054 * than they were before applying the setting (We can't
1055 * "unmux a pin"!), but it's not a big deal since the pins
1056 * are free to be muxed by another apply_setting.
1057 */
1058 if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
1059 pinmux_disable_setting(setting2);
Richard Genoud3102a762013-03-25 15:47:22 +01001060 }
Richard Genoud8009d5f2013-03-28 12:55:47 +01001061
Richard Genoud385d9422013-03-29 10:03:27 +01001062 /* There's no infinite recursive loop here because p->state is NULL */
1063 if (old_state)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001064 pinctrl_select_state(p, old_state);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001065
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001066 return ret;
1067}
Florian Fainellibd6552c2017-03-01 10:32:57 -08001068
1069/**
1070 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
1071 * @p: the pinctrl handle for the device that requests configuration
1072 * @state: the state handle to select/activate/program
1073 */
1074int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
1075{
1076 if (p->state == state)
1077 return 0;
1078
1079 return pinctrl_commit_state(p, state);
1080}
Stephen Warren6e5e9592012-03-02 13:05:47 -07001081EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001082
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001083static void devm_pinctrl_release(struct device *dev, void *res)
1084{
1085 pinctrl_put(*(struct pinctrl **)res);
1086}
1087
1088/**
1089 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
1090 * @dev: the device to obtain the handle for
1091 *
1092 * If there is a need to explicitly destroy the returned struct pinctrl,
1093 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1094 */
1095struct pinctrl *devm_pinctrl_get(struct device *dev)
1096{
1097 struct pinctrl **ptr, *p;
1098
1099 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
1100 if (!ptr)
1101 return ERR_PTR(-ENOMEM);
1102
1103 p = pinctrl_get(dev);
1104 if (!IS_ERR(p)) {
1105 *ptr = p;
1106 devres_add(dev, ptr);
1107 } else {
1108 devres_free(ptr);
1109 }
1110
1111 return p;
1112}
1113EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1114
1115static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1116{
1117 struct pinctrl **p = res;
1118
1119 return *p == data;
1120}
1121
1122/**
1123 * devm_pinctrl_put() - Resource managed pinctrl_put()
1124 * @p: the pinctrl handle to release
1125 *
1126 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1127 * this function will not need to be called and the resource management
1128 * code will ensure that the resource is freed.
1129 */
1130void devm_pinctrl_put(struct pinctrl *p)
1131{
Jingoo Hana72149e2013-02-26 11:34:07 +09001132 WARN_ON(devres_release(p->dev, devm_pinctrl_release,
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001133 devm_pinctrl_match, p));
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001134}
1135EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1136
Stephen Warren57291ce2012-03-23 10:29:46 -06001137int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
Doug Andersonc5272a22015-05-01 09:01:27 -07001138 bool dup)
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001139{
Stephen Warren1e2082b2012-03-02 13:05:48 -07001140 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001141 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001142
Masahiro Yamada7e9236f2015-05-28 21:52:59 +09001143 pr_debug("add %u pinctrl maps\n", num_maps);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001144
1145 /* First sanity check the new mapping */
1146 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001147 if (!maps[i].dev_name) {
1148 pr_err("failed to register map %s (%d): no device given\n",
1149 maps[i].name, i);
1150 return -EINVAL;
1151 }
1152
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001153 if (!maps[i].name) {
1154 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001155 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001156 return -EINVAL;
1157 }
1158
Stephen Warren1e2082b2012-03-02 13:05:48 -07001159 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1160 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001161 pr_err("failed to register map %s (%d): no pin control device given\n",
1162 maps[i].name, i);
1163 return -EINVAL;
1164 }
1165
Stephen Warren1e2082b2012-03-02 13:05:48 -07001166 switch (maps[i].type) {
1167 case PIN_MAP_TYPE_DUMMY_STATE:
1168 break;
1169 case PIN_MAP_TYPE_MUX_GROUP:
1170 ret = pinmux_validate_map(&maps[i], i);
1171 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001172 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001173 break;
1174 case PIN_MAP_TYPE_CONFIGS_PIN:
1175 case PIN_MAP_TYPE_CONFIGS_GROUP:
1176 ret = pinconf_validate_map(&maps[i], i);
1177 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001178 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001179 break;
1180 default:
1181 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001182 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -07001183 return -EINVAL;
1184 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001185 }
1186
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001187 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
1188 if (!maps_node) {
1189 pr_err("failed to alloc struct pinctrl_maps\n");
1190 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001191 }
1192
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001193 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -06001194 if (dup) {
1195 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
1196 GFP_KERNEL);
1197 if (!maps_node->maps) {
1198 pr_err("failed to duplicate mapping table\n");
1199 kfree(maps_node);
1200 return -ENOMEM;
1201 }
1202 } else {
1203 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001204 }
1205
Doug Andersonc5272a22015-05-01 09:01:27 -07001206 mutex_lock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001207 list_add_tail(&maps_node->node, &pinctrl_maps);
Doug Andersonc5272a22015-05-01 09:01:27 -07001208 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001209
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001210 return 0;
1211}
1212
Stephen Warren57291ce2012-03-23 10:29:46 -06001213/**
1214 * pinctrl_register_mappings() - register a set of pin controller mappings
1215 * @maps: the pincontrol mappings table to register. This should probably be
1216 * marked with __initdata so it can be discarded after boot. This
1217 * function will perform a shallow copy for the mapping entries.
1218 * @num_maps: the number of maps in the mapping table
1219 */
1220int pinctrl_register_mappings(struct pinctrl_map const *maps,
1221 unsigned num_maps)
1222{
Doug Andersonc5272a22015-05-01 09:01:27 -07001223 return pinctrl_register_map(maps, num_maps, true);
Stephen Warren57291ce2012-03-23 10:29:46 -06001224}
1225
1226void pinctrl_unregister_map(struct pinctrl_map const *map)
1227{
1228 struct pinctrl_maps *maps_node;
1229
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001230 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001231 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1232 if (maps_node->maps == map) {
1233 list_del(&maps_node->node);
Linus Walleijdb6c2c62013-07-23 01:43:20 +02001234 kfree(maps_node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001235 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001236 return;
1237 }
1238 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001239 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001240}
1241
Julien Delacou840a47b2012-12-10 14:47:33 +01001242/**
1243 * pinctrl_force_sleep() - turn a given controller device into sleep state
1244 * @pctldev: pin controller device
1245 */
1246int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1247{
1248 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
Florian Fainellibd6552c2017-03-01 10:32:57 -08001249 return pinctrl_commit_state(pctldev->p, pctldev->hog_sleep);
Julien Delacou840a47b2012-12-10 14:47:33 +01001250 return 0;
1251}
1252EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1253
1254/**
1255 * pinctrl_force_default() - turn a given controller device into default state
1256 * @pctldev: pin controller device
1257 */
1258int pinctrl_force_default(struct pinctrl_dev *pctldev)
1259{
1260 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
Florian Fainellibd6552c2017-03-01 10:32:57 -08001261 return pinctrl_commit_state(pctldev->p, pctldev->hog_default);
Julien Delacou840a47b2012-12-10 14:47:33 +01001262 return 0;
1263}
1264EXPORT_SYMBOL_GPL(pinctrl_force_default);
1265
Douglas Andersonef0eebc2015-10-20 21:15:06 -07001266/**
1267 * pinctrl_init_done() - tell pinctrl probe is done
1268 *
1269 * We'll use this time to switch the pins from "init" to "default" unless the
1270 * driver selected some other state.
1271 *
1272 * @dev: device to that's done probing
1273 */
1274int pinctrl_init_done(struct device *dev)
1275{
1276 struct dev_pin_info *pins = dev->pins;
1277 int ret;
1278
1279 if (!pins)
1280 return 0;
1281
1282 if (IS_ERR(pins->init_state))
1283 return 0; /* No such state */
1284
1285 if (pins->p->state != pins->init_state)
1286 return 0; /* Not at init anyway */
1287
1288 if (IS_ERR(pins->default_state))
1289 return 0; /* No default state */
1290
1291 ret = pinctrl_select_state(pins->p, pins->default_state);
1292 if (ret)
1293 dev_err(dev, "failed to activate default pinctrl state\n");
1294
1295 return ret;
1296}
1297
Linus Walleij14005ee2013-06-05 15:30:33 +02001298#ifdef CONFIG_PM
1299
1300/**
Tony Lindgrenf3333492013-07-18 08:15:04 -07001301 * pinctrl_pm_select_state() - select pinctrl state for PM
1302 * @dev: device to select default state for
1303 * @state: state to set
1304 */
1305static int pinctrl_pm_select_state(struct device *dev,
1306 struct pinctrl_state *state)
1307{
1308 struct dev_pin_info *pins = dev->pins;
1309 int ret;
1310
1311 if (IS_ERR(state))
1312 return 0; /* No such state */
1313 ret = pinctrl_select_state(pins->p, state);
1314 if (ret)
1315 dev_err(dev, "failed to activate pinctrl state %s\n",
1316 state->name);
1317 return ret;
1318}
1319
1320/**
Linus Walleij14005ee2013-06-05 15:30:33 +02001321 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1322 * @dev: device to select default state for
1323 */
1324int pinctrl_pm_select_default_state(struct device *dev)
1325{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001326 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001327 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001328
1329 return pinctrl_pm_select_state(dev, dev->pins->default_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001330}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001331EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001332
1333/**
1334 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1335 * @dev: device to select sleep state for
1336 */
1337int pinctrl_pm_select_sleep_state(struct device *dev)
1338{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001339 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001340 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001341
1342 return pinctrl_pm_select_state(dev, dev->pins->sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001343}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001344EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001345
1346/**
1347 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1348 * @dev: device to select idle state for
1349 */
1350int pinctrl_pm_select_idle_state(struct device *dev)
1351{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001352 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001353 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001354
1355 return pinctrl_pm_select_state(dev, dev->pins->idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001356}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001357EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001358#endif
1359
Linus Walleij2744e8a2011-05-02 20:50:54 +02001360#ifdef CONFIG_DEBUG_FS
1361
1362static int pinctrl_pins_show(struct seq_file *s, void *what)
1363{
1364 struct pinctrl_dev *pctldev = s->private;
1365 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001366 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001367
1368 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001369
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001370 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001371
Chanho Park706e8522012-01-03 16:47:50 +09001372 /* The pin number can be retrived from the pin controller descriptor */
1373 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001374 struct pin_desc *desc;
1375
Chanho Park706e8522012-01-03 16:47:50 +09001376 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001377 desc = pin_desc_get(pctldev, pin);
1378 /* Pin space may be sparse */
1379 if (desc == NULL)
1380 continue;
1381
Masahiro Yamadacf9d9942016-05-24 14:26:26 +09001382 seq_printf(s, "pin %d (%s) ", pin, desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001383
1384 /* Driver-specific info per pin */
1385 if (ops->pin_dbg_show)
1386 ops->pin_dbg_show(pctldev, s, pin);
1387
1388 seq_puts(s, "\n");
1389 }
1390
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001391 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001392
Linus Walleij2744e8a2011-05-02 20:50:54 +02001393 return 0;
1394}
1395
1396static int pinctrl_groups_show(struct seq_file *s, void *what)
1397{
1398 struct pinctrl_dev *pctldev = s->private;
1399 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301400 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001401
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001402 mutex_lock(&pctldev->mutex);
1403
Viresh Kumard1e90e92012-03-30 11:25:40 +05301404 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001405
Linus Walleij2744e8a2011-05-02 20:50:54 +02001406 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301407 while (selector < ngroups) {
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001408 const unsigned *pins = NULL;
1409 unsigned num_pins = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001410 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001411 const char *pname;
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001412 int ret = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001413 int i;
1414
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001415 if (ops->get_group_pins)
1416 ret = ops->get_group_pins(pctldev, selector,
1417 &pins, &num_pins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001418 if (ret)
1419 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1420 gname);
1421 else {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001422 seq_printf(s, "group: %s\n", gname);
1423 for (i = 0; i < num_pins; i++) {
1424 pname = pin_get_name(pctldev, pins[i]);
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001425 if (WARN_ON(!pname)) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001426 mutex_unlock(&pctldev->mutex);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001427 return -EINVAL;
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001428 }
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001429 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1430 }
1431 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001432 }
1433 selector++;
1434 }
1435
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001436 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001437
1438 return 0;
1439}
1440
1441static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1442{
1443 struct pinctrl_dev *pctldev = s->private;
1444 struct pinctrl_gpio_range *range = NULL;
1445
1446 seq_puts(s, "GPIO ranges handled:\n");
1447
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001448 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001449
Linus Walleij2744e8a2011-05-02 20:50:54 +02001450 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001451 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Christian Ruppertc8587ee2013-06-13 14:55:31 +02001452 if (range->pins) {
1453 int a;
1454 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
1455 range->id, range->name,
1456 range->base, (range->base + range->npins - 1));
1457 for (a = 0; a < range->npins - 1; a++)
1458 seq_printf(s, "%u, ", range->pins[a]);
1459 seq_printf(s, "%u}\n", range->pins[a]);
1460 }
1461 else
1462 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1463 range->id, range->name,
1464 range->base, (range->base + range->npins - 1),
1465 range->pin_base,
1466 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001467 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001468
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001469 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001470
1471 return 0;
1472}
1473
1474static int pinctrl_devices_show(struct seq_file *s, void *what)
1475{
1476 struct pinctrl_dev *pctldev;
1477
Linus Walleijae6b4d82011-10-19 18:14:33 +02001478 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001479
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001480 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001481
Linus Walleij2744e8a2011-05-02 20:50:54 +02001482 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1483 seq_printf(s, "%s ", pctldev->desc->name);
1484 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001485 seq_puts(s, "yes ");
1486 else
1487 seq_puts(s, "no ");
1488 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001489 seq_puts(s, "yes");
1490 else
1491 seq_puts(s, "no");
1492 seq_puts(s, "\n");
1493 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001494
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001495 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001496
1497 return 0;
1498}
1499
Stephen Warren1e2082b2012-03-02 13:05:48 -07001500static inline const char *map_type(enum pinctrl_map_type type)
1501{
1502 static const char * const names[] = {
1503 "INVALID",
1504 "DUMMY_STATE",
1505 "MUX_GROUP",
1506 "CONFIGS_PIN",
1507 "CONFIGS_GROUP",
1508 };
1509
1510 if (type >= ARRAY_SIZE(names))
1511 return "UNKNOWN";
1512
1513 return names[type];
1514}
1515
Stephen Warren3eedb432012-02-23 17:04:40 -07001516static int pinctrl_maps_show(struct seq_file *s, void *what)
1517{
1518 struct pinctrl_maps *maps_node;
1519 int i;
1520 struct pinctrl_map const *map;
1521
1522 seq_puts(s, "Pinctrl maps:\n");
1523
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001524 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001525 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001526 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1527 map->dev_name, map->name, map_type(map->type),
1528 map->type);
1529
1530 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1531 seq_printf(s, "controlling device %s\n",
1532 map->ctrl_dev_name);
1533
1534 switch (map->type) {
1535 case PIN_MAP_TYPE_MUX_GROUP:
1536 pinmux_show_map(s, map);
1537 break;
1538 case PIN_MAP_TYPE_CONFIGS_PIN:
1539 case PIN_MAP_TYPE_CONFIGS_GROUP:
1540 pinconf_show_map(s, map);
1541 break;
1542 default:
1543 break;
1544 }
1545
1546 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001547 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001548 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001549
1550 return 0;
1551}
1552
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001553static int pinctrl_show(struct seq_file *s, void *what)
1554{
1555 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001556 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001557 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001558
1559 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001560
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001561 mutex_lock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001562
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001563 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001564 seq_printf(s, "device: %s current state: %s\n",
1565 dev_name(p->dev),
1566 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001567
Stephen Warren6e5e9592012-03-02 13:05:47 -07001568 list_for_each_entry(state, &p->states, node) {
1569 seq_printf(s, " state: %s\n", state->name);
1570
1571 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001572 struct pinctrl_dev *pctldev = setting->pctldev;
1573
1574 seq_printf(s, " type: %s controller %s ",
1575 map_type(setting->type),
1576 pinctrl_dev_get_name(pctldev));
1577
1578 switch (setting->type) {
1579 case PIN_MAP_TYPE_MUX_GROUP:
1580 pinmux_show_setting(s, setting);
1581 break;
1582 case PIN_MAP_TYPE_CONFIGS_PIN:
1583 case PIN_MAP_TYPE_CONFIGS_GROUP:
1584 pinconf_show_setting(s, setting);
1585 break;
1586 default:
1587 break;
1588 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001589 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001590 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001591 }
1592
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001593 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001594
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001595 return 0;
1596}
1597
Linus Walleij2744e8a2011-05-02 20:50:54 +02001598static int pinctrl_pins_open(struct inode *inode, struct file *file)
1599{
1600 return single_open(file, pinctrl_pins_show, inode->i_private);
1601}
1602
1603static int pinctrl_groups_open(struct inode *inode, struct file *file)
1604{
1605 return single_open(file, pinctrl_groups_show, inode->i_private);
1606}
1607
1608static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1609{
1610 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1611}
1612
1613static int pinctrl_devices_open(struct inode *inode, struct file *file)
1614{
1615 return single_open(file, pinctrl_devices_show, NULL);
1616}
1617
Stephen Warren3eedb432012-02-23 17:04:40 -07001618static int pinctrl_maps_open(struct inode *inode, struct file *file)
1619{
1620 return single_open(file, pinctrl_maps_show, NULL);
1621}
1622
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001623static int pinctrl_open(struct inode *inode, struct file *file)
1624{
1625 return single_open(file, pinctrl_show, NULL);
1626}
1627
Linus Walleij2744e8a2011-05-02 20:50:54 +02001628static const struct file_operations pinctrl_pins_ops = {
1629 .open = pinctrl_pins_open,
1630 .read = seq_read,
1631 .llseek = seq_lseek,
1632 .release = single_release,
1633};
1634
1635static const struct file_operations pinctrl_groups_ops = {
1636 .open = pinctrl_groups_open,
1637 .read = seq_read,
1638 .llseek = seq_lseek,
1639 .release = single_release,
1640};
1641
1642static const struct file_operations pinctrl_gpioranges_ops = {
1643 .open = pinctrl_gpioranges_open,
1644 .read = seq_read,
1645 .llseek = seq_lseek,
1646 .release = single_release,
1647};
1648
1649static const struct file_operations pinctrl_devices_ops = {
1650 .open = pinctrl_devices_open,
1651 .read = seq_read,
1652 .llseek = seq_lseek,
1653 .release = single_release,
1654};
1655
Stephen Warren3eedb432012-02-23 17:04:40 -07001656static const struct file_operations pinctrl_maps_ops = {
1657 .open = pinctrl_maps_open,
1658 .read = seq_read,
1659 .llseek = seq_lseek,
1660 .release = single_release,
1661};
1662
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001663static const struct file_operations pinctrl_ops = {
1664 .open = pinctrl_open,
1665 .read = seq_read,
1666 .llseek = seq_lseek,
1667 .release = single_release,
1668};
1669
Linus Walleij2744e8a2011-05-02 20:50:54 +02001670static struct dentry *debugfs_root;
1671
1672static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1673{
Tony Lindgren02157162012-01-20 08:17:22 -08001674 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001675
Stephen Warren51cd24e2011-12-09 16:59:05 -07001676 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001677 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001678 pctldev->device_root = device_root;
1679
Linus Walleij2744e8a2011-05-02 20:50:54 +02001680 if (IS_ERR(device_root) || !device_root) {
1681 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001682 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001683 return;
1684 }
1685 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1686 device_root, pctldev, &pinctrl_pins_ops);
1687 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1688 device_root, pctldev, &pinctrl_groups_ops);
1689 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1690 device_root, pctldev, &pinctrl_gpioranges_ops);
Florian Vaussarde7f2a442014-02-05 07:51:22 +01001691 if (pctldev->desc->pmxops)
1692 pinmux_init_device_debugfs(device_root, pctldev);
1693 if (pctldev->desc->confops)
1694 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001695}
1696
Tony Lindgren02157162012-01-20 08:17:22 -08001697static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1698{
1699 debugfs_remove_recursive(pctldev->device_root);
1700}
1701
Linus Walleij2744e8a2011-05-02 20:50:54 +02001702static void pinctrl_init_debugfs(void)
1703{
1704 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1705 if (IS_ERR(debugfs_root) || !debugfs_root) {
1706 pr_warn("failed to create debugfs directory\n");
1707 debugfs_root = NULL;
1708 return;
1709 }
1710
1711 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1712 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001713 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1714 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001715 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1716 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001717}
1718
1719#else /* CONFIG_DEBUG_FS */
1720
1721static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1722{
1723}
1724
1725static void pinctrl_init_debugfs(void)
1726{
1727}
1728
Tony Lindgren02157162012-01-20 08:17:22 -08001729static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1730{
1731}
1732
Linus Walleij2744e8a2011-05-02 20:50:54 +02001733#endif
1734
Stephen Warrend26bc492012-03-16 14:54:25 -06001735static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1736{
1737 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1738
1739 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301740 !ops->get_groups_count ||
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001741 !ops->get_group_name)
Stephen Warrend26bc492012-03-16 14:54:25 -06001742 return -EINVAL;
1743
Stephen Warren57291ce2012-03-23 10:29:46 -06001744 if (ops->dt_node_to_map && !ops->dt_free_map)
1745 return -EINVAL;
1746
Stephen Warrend26bc492012-03-16 14:54:25 -06001747 return 0;
1748}
1749
Linus Walleij2744e8a2011-05-02 20:50:54 +02001750/**
1751 * pinctrl_register() - register a pin controller device
1752 * @pctldesc: descriptor for this pin controller
1753 * @dev: parent device for this pin controller
1754 * @driver_data: private pin controller data for this pin controller
1755 */
1756struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1757 struct device *dev, void *driver_data)
1758{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001759 struct pinctrl_dev *pctldev;
1760 int ret;
1761
Devendra Nagada9aecb2012-06-16 23:43:16 +05301762 if (!pctldesc)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001763 return ERR_PTR(-EINVAL);
Devendra Nagada9aecb2012-06-16 23:43:16 +05301764 if (!pctldesc->name)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001765 return ERR_PTR(-EINVAL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001766
Stephen Warren02f5b982012-02-22 14:26:00 -07001767 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001768 if (pctldev == NULL) {
1769 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001770 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001771 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001772
1773 /* Initialize pin control device struct */
1774 pctldev->owner = pctldesc->owner;
1775 pctldev->desc = pctldesc;
1776 pctldev->driver_data = driver_data;
1777 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001778 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001779 pctldev->dev = dev;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001780 mutex_init(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001781
Stephen Warrend26bc492012-03-16 14:54:25 -06001782 /* check core ops for sanity */
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001783 ret = pinctrl_check_ops(pctldev);
1784 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001785 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06001786 goto out_err;
1787 }
1788
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001789 /* If we're implementing pinmuxing, check the ops for sanity */
1790 if (pctldesc->pmxops) {
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001791 ret = pinmux_check_ops(pctldev);
1792 if (ret)
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001793 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001794 }
1795
1796 /* If we're implementing pinconfig, check the ops for sanity */
1797 if (pctldesc->confops) {
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001798 ret = pinconf_check_ops(pctldev);
1799 if (ret)
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001800 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001801 }
1802
Linus Walleij2744e8a2011-05-02 20:50:54 +02001803 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02001804 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001805 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1806 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001807 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001808 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1809 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001810 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001811 }
1812
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001813 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -07001814 list_add_tail(&pctldev->node, &pinctrldev_list);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001815 mutex_unlock(&pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001816
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001817 pctldev->p = pinctrl_get(pctldev->dev);
1818
Stephen Warren6e5e9592012-03-02 13:05:47 -07001819 if (!IS_ERR(pctldev->p)) {
Julien Delacou840a47b2012-12-10 14:47:33 +01001820 pctldev->hog_default =
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001821 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
Julien Delacou840a47b2012-12-10 14:47:33 +01001822 if (IS_ERR(pctldev->hog_default)) {
John Crispinad6e1102012-04-26 16:47:11 +02001823 dev_dbg(dev, "failed to lookup the default state\n");
1824 } else {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001825 if (pinctrl_select_state(pctldev->p,
Julien Delacou840a47b2012-12-10 14:47:33 +01001826 pctldev->hog_default))
John Crispinad6e1102012-04-26 16:47:11 +02001827 dev_err(dev,
1828 "failed to select default state\n");
John Crispinad6e1102012-04-26 16:47:11 +02001829 }
Julien Delacou840a47b2012-12-10 14:47:33 +01001830
1831 pctldev->hog_sleep =
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001832 pinctrl_lookup_state(pctldev->p,
Julien Delacou840a47b2012-12-10 14:47:33 +01001833 PINCTRL_STATE_SLEEP);
1834 if (IS_ERR(pctldev->hog_sleep))
1835 dev_dbg(dev, "failed to lookup the sleep state\n");
Stephen Warren6e5e9592012-03-02 13:05:47 -07001836 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001837
Stephen Warren2304b472012-02-22 14:26:01 -07001838 pinctrl_init_device_debugfs(pctldev);
1839
Linus Walleij2744e8a2011-05-02 20:50:54 +02001840 return pctldev;
1841
Stephen Warren51cd24e2011-12-09 16:59:05 -07001842out_err:
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001843 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001844 kfree(pctldev);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001845 return ERR_PTR(ret);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001846}
1847EXPORT_SYMBOL_GPL(pinctrl_register);
1848
1849/**
1850 * pinctrl_unregister() - unregister pinmux
1851 * @pctldev: pin controller to unregister
1852 *
1853 * Called by pinmux drivers to unregister a pinmux.
1854 */
1855void pinctrl_unregister(struct pinctrl_dev *pctldev)
1856{
Dong Aisheng5d589b02012-05-23 21:22:40 +08001857 struct pinctrl_gpio_range *range, *n;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001858 if (pctldev == NULL)
1859 return;
1860
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001861 mutex_lock(&pctldev->mutex);
Tony Lindgren02157162012-01-20 08:17:22 -08001862 pinctrl_remove_device_debugfs(pctldev);
Jim Lindb93fac2015-01-08 20:25:05 +08001863 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001864
Stephen Warren6e5e9592012-03-02 13:05:47 -07001865 if (!IS_ERR(pctldev->p))
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001866 pinctrl_put(pctldev->p);
Stephen Warren57b676f2012-03-02 13:05:44 -07001867
Jim Lindb93fac2015-01-08 20:25:05 +08001868 mutex_lock(&pinctrldev_list_mutex);
1869 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001870 /* TODO: check that no pinmuxes are still active? */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001871 list_del(&pctldev->node);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001872 /* Destroy descriptor tree */
1873 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1874 pctldev->desc->npins);
Dong Aisheng5d589b02012-05-23 21:22:40 +08001875 /* remove gpio ranges map */
1876 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1877 list_del(&range->node);
1878
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001879 mutex_unlock(&pctldev->mutex);
1880 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001881 kfree(pctldev);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001882 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001883}
1884EXPORT_SYMBOL_GPL(pinctrl_unregister);
1885
Laxman Dewangan80e0f8d2016-02-24 14:12:59 +05301886static void devm_pinctrl_dev_release(struct device *dev, void *res)
1887{
1888 struct pinctrl_dev *pctldev = *(struct pinctrl_dev **)res;
1889
1890 pinctrl_unregister(pctldev);
1891}
1892
1893static int devm_pinctrl_dev_match(struct device *dev, void *res, void *data)
1894{
1895 struct pctldev **r = res;
1896
Laxman Dewangan3024f922016-02-24 14:44:07 +05301897 if (WARN_ON(!r || !*r))
Laxman Dewangan80e0f8d2016-02-24 14:12:59 +05301898 return 0;
1899
1900 return *r == data;
1901}
1902
1903/**
1904 * devm_pinctrl_register() - Resource managed version of pinctrl_register().
1905 * @dev: parent device for this pin controller
1906 * @pctldesc: descriptor for this pin controller
1907 * @driver_data: private pin controller data for this pin controller
1908 *
1909 * Returns an error pointer if pincontrol register failed. Otherwise
1910 * it returns valid pinctrl handle.
1911 *
1912 * The pinctrl device will be automatically released when the device is unbound.
1913 */
1914struct pinctrl_dev *devm_pinctrl_register(struct device *dev,
1915 struct pinctrl_desc *pctldesc,
1916 void *driver_data)
1917{
1918 struct pinctrl_dev **ptr, *pctldev;
1919
1920 ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
1921 if (!ptr)
1922 return ERR_PTR(-ENOMEM);
1923
1924 pctldev = pinctrl_register(pctldesc, dev, driver_data);
1925 if (IS_ERR(pctldev)) {
1926 devres_free(ptr);
1927 return pctldev;
1928 }
1929
1930 *ptr = pctldev;
1931 devres_add(dev, ptr);
1932
1933 return pctldev;
1934}
1935EXPORT_SYMBOL_GPL(devm_pinctrl_register);
1936
1937/**
1938 * devm_pinctrl_unregister() - Resource managed version of pinctrl_unregister().
1939 * @dev: device for which which resource was allocated
1940 * @pctldev: the pinctrl device to unregister.
1941 */
1942void devm_pinctrl_unregister(struct device *dev, struct pinctrl_dev *pctldev)
1943{
1944 WARN_ON(devres_release(dev, devm_pinctrl_dev_release,
1945 devm_pinctrl_dev_match, pctldev));
1946}
1947EXPORT_SYMBOL_GPL(devm_pinctrl_unregister);
1948
Linus Walleij2744e8a2011-05-02 20:50:54 +02001949static int __init pinctrl_init(void)
1950{
1951 pr_info("initialized pinctrl subsystem\n");
1952 pinctrl_init_debugfs();
1953 return 0;
1954}
1955
1956/* init early since many drivers really need to initialized pinmux early */
1957core_initcall(pinctrl_init);