blob: f67a8b7a4e18252338c54c282152b78971d34225 [file] [log] [blame]
Linus Walleij2744e8a2011-05-02 20:50:54 +02001/*
2 * Core driver for the pin control subsystem
3 *
Linus Walleijbefe5bd2012-02-09 19:47:48 +01004 * Copyright (C) 2011-2012 ST-Ericsson SA
Linus Walleij2744e8a2011-05-02 20:50:54 +02005 * Written on behalf of Linaro for ST-Ericsson
6 * Based on bits of regulator core, gpio core and clk core
7 *
8 * Author: Linus Walleij <linus.walleij@linaro.org>
9 *
Stephen Warrenb2b3e662012-02-19 23:45:43 -070010 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
11 *
Linus Walleij2744e8a2011-05-02 20:50:54 +020012 * License terms: GNU General Public License (GPL) version 2
13 */
14#define pr_fmt(fmt) "pinctrl core: " fmt
15
16#include <linux/kernel.h>
Linus Walleijab780292013-01-22 10:56:14 -070017#include <linux/kref.h>
Stephen Rothwella5a697c2011-09-30 14:39:04 +100018#include <linux/export.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020019#include <linux/init.h>
20#include <linux/device.h>
21#include <linux/slab.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020022#include <linux/err.h>
23#include <linux/list.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020024#include <linux/sysfs.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
Stephen Warren6d4ca1f2012-04-16 10:51:00 -060027#include <linux/pinctrl/consumer.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020028#include <linux/pinctrl/pinctrl.h>
29#include <linux/pinctrl/machine.h>
Haojian Zhuang2afe8222013-03-28 07:34:19 +080030
31#ifdef CONFIG_GPIOLIB
Haojian Zhuang51e13c22013-02-17 19:42:50 +080032#include <asm-generic/gpio.h>
Haojian Zhuang2afe8222013-03-28 07:34:19 +080033#endif
34
Linus Walleij2744e8a2011-05-02 20:50:54 +020035#include "core.h"
Stephen Warren57291ce2012-03-23 10:29:46 -060036#include "devicetree.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020037#include "pinmux.h"
Linus Walleijae6b4d82011-10-19 18:14:33 +020038#include "pinconf.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020039
Stephen Warrenb2b3e662012-02-19 23:45:43 -070040
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080041static bool pinctrl_dummy_state;
42
Patrice Chotard42fed7b2013-04-11 11:01:27 +020043/* Mutex taken to protect pinctrl_list */
Sachin Kamat843aec92013-06-18 14:34:27 +053044static DEFINE_MUTEX(pinctrl_list_mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +020045
46/* Mutex taken to protect pinctrl_maps */
47DEFINE_MUTEX(pinctrl_maps_mutex);
48
49/* Mutex taken to protect pinctrldev_list */
Sachin Kamat843aec92013-06-18 14:34:27 +053050static DEFINE_MUTEX(pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -070051
52/* Global list of pin control devices (struct pinctrl_dev) */
Patrice Chotard42fed7b2013-04-11 11:01:27 +020053static LIST_HEAD(pinctrldev_list);
Linus Walleij2744e8a2011-05-02 20:50:54 +020054
Stephen Warren57b676f2012-03-02 13:05:44 -070055/* List of pin controller handles (struct pinctrl) */
Linus Walleijbefe5bd2012-02-09 19:47:48 +010056static LIST_HEAD(pinctrl_list);
57
Stephen Warren57b676f2012-03-02 13:05:44 -070058/* List of pinctrl maps (struct pinctrl_maps) */
Laurent Meunier6f9e41f2013-02-06 09:09:44 +010059LIST_HEAD(pinctrl_maps);
Stephen Warrenb2b3e662012-02-19 23:45:43 -070060
Linus Walleijbefe5bd2012-02-09 19:47:48 +010061
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080062/**
63 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
64 *
65 * Usually this function is called by platforms without pinctrl driver support
66 * but run with some shared drivers using pinctrl APIs.
67 * After calling this function, the pinctrl core will return successfully
68 * with creating a dummy state for the driver to keep going smoothly.
69 */
70void pinctrl_provide_dummies(void)
71{
72 pinctrl_dummy_state = true;
73}
74
Linus Walleij2744e8a2011-05-02 20:50:54 +020075const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
76{
77 /* We're not allowed to register devices without name */
78 return pctldev->desc->name;
79}
80EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
81
Haojian Zhuangd6e99ab2013-01-18 15:31:06 +080082const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
83{
84 return dev_name(pctldev->dev);
85}
86EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
87
Linus Walleij2744e8a2011-05-02 20:50:54 +020088void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
89{
90 return pctldev->driver_data;
91}
92EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
93
94/**
Linus Walleij9dfac4f2012-02-01 18:02:47 +010095 * get_pinctrl_dev_from_devname() - look up pin controller device
96 * @devname: the name of a device instance, as returned by dev_name()
Linus Walleij2744e8a2011-05-02 20:50:54 +020097 *
98 * Looks up a pin control device matching a certain device name or pure device
99 * pointer, the pure device pointer will take precedence.
100 */
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100101struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200102{
103 struct pinctrl_dev *pctldev = NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200104
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100105 if (!devname)
106 return NULL;
107
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200108 mutex_lock(&pinctrldev_list_mutex);
109
Linus Walleij2744e8a2011-05-02 20:50:54 +0200110 list_for_each_entry(pctldev, &pinctrldev_list, node) {
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100111 if (!strcmp(dev_name(pctldev->dev), devname)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200112 /* Matched on device name */
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200113 mutex_unlock(&pinctrldev_list_mutex);
114 return pctldev;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200115 }
116 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200117
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200118 mutex_unlock(&pinctrldev_list_mutex);
119
120 return NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200121}
122
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200123struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np)
124{
125 struct pinctrl_dev *pctldev;
126
127 mutex_lock(&pinctrldev_list_mutex);
128
129 list_for_each_entry(pctldev, &pinctrldev_list, node)
130 if (pctldev->dev->of_node == np) {
131 mutex_unlock(&pinctrldev_list_mutex);
132 return pctldev;
133 }
134
Daniel Mackd463f822013-04-26 18:57:02 +0200135 mutex_unlock(&pinctrldev_list_mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200136
137 return NULL;
138}
139
Linus Walleij2744e8a2011-05-02 20:50:54 +0200140/**
Linus Walleijae6b4d82011-10-19 18:14:33 +0200141 * pin_get_from_name() - look up a pin number from a name
142 * @pctldev: the pin control device to lookup the pin on
143 * @name: the name of the pin to look up
144 */
145int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
146{
Chanho Park706e8522012-01-03 16:47:50 +0900147 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200148
Chanho Park706e8522012-01-03 16:47:50 +0900149 /* The pin number can be retrived from the pin controller descriptor */
150 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200151 struct pin_desc *desc;
152
Chanho Park706e8522012-01-03 16:47:50 +0900153 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200154 desc = pin_desc_get(pctldev, pin);
155 /* Pin space may be sparse */
Axel Lin6c325f82013-08-19 10:06:29 +0800156 if (desc && !strcmp(name, desc->name))
Linus Walleijae6b4d82011-10-19 18:14:33 +0200157 return pin;
158 }
159
160 return -EINVAL;
161}
162
163/**
Dong Aishengdcb5dbc2012-04-17 15:00:46 +0800164 * pin_get_name_from_id() - look up a pin name from a pin id
165 * @pctldev: the pin control device to lookup the pin on
166 * @name: the name of the pin to look up
167 */
168const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
169{
170 const struct pin_desc *desc;
171
172 desc = pin_desc_get(pctldev, pin);
173 if (desc == NULL) {
174 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
175 pin);
176 return NULL;
177 }
178
179 return desc->name;
180}
181
182/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200183 * pin_is_valid() - check if pin exists on controller
184 * @pctldev: the pin control device to check the pin on
185 * @pin: pin to check, use the local pin controller index number
186 *
187 * This tells us whether a certain pin exist on a certain pin controller or
188 * not. Pin lists may be sparse, so some pins may not exist.
189 */
190bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
191{
192 struct pin_desc *pindesc;
193
194 if (pin < 0)
195 return false;
196
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200197 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200198 pindesc = pin_desc_get(pctldev, pin);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200199 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200200
Stephen Warren57b676f2012-03-02 13:05:44 -0700201 return pindesc != NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200202}
203EXPORT_SYMBOL_GPL(pin_is_valid);
204
205/* Deletes a range of pin descriptors */
206static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
207 const struct pinctrl_pin_desc *pins,
208 unsigned num_pins)
209{
210 int i;
211
Linus Walleij2744e8a2011-05-02 20:50:54 +0200212 for (i = 0; i < num_pins; i++) {
213 struct pin_desc *pindesc;
214
215 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
216 pins[i].number);
217 if (pindesc != NULL) {
218 radix_tree_delete(&pctldev->pin_desc_tree,
219 pins[i].number);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100220 if (pindesc->dynamic_name)
221 kfree(pindesc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200222 }
223 kfree(pindesc);
224 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200225}
226
227static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
228 unsigned number, const char *name)
229{
230 struct pin_desc *pindesc;
231
232 pindesc = pin_desc_get(pctldev, number);
233 if (pindesc != NULL) {
Masahiro Yamada2b38ca62015-07-21 15:25:26 +0900234 dev_err(pctldev->dev, "pin %d already registered\n", number);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200235 return -EINVAL;
236 }
237
238 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700239 if (pindesc == NULL) {
240 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200241 return -ENOMEM;
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700242 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200243
Linus Walleij2744e8a2011-05-02 20:50:54 +0200244 /* Set owner */
245 pindesc->pctldev = pctldev;
246
Stephen Warren9af1e442011-10-19 16:19:27 -0600247 /* Copy basic pin info */
Linus Walleij8dc6ae42012-02-01 18:11:40 +0100248 if (name) {
Linus Walleijca53c5f2011-12-14 20:33:37 +0100249 pindesc->name = name;
250 } else {
251 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
Sachin Kamateb26cc92012-09-25 12:41:40 +0530252 if (pindesc->name == NULL) {
253 kfree(pindesc);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100254 return -ENOMEM;
Sachin Kamateb26cc92012-09-25 12:41:40 +0530255 }
Linus Walleijca53c5f2011-12-14 20:33:37 +0100256 pindesc->dynamic_name = true;
257 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200258
Linus Walleij2744e8a2011-05-02 20:50:54 +0200259 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200260 pr_debug("registered pin %d (%s) on %s\n",
Linus Walleijca53c5f2011-12-14 20:33:37 +0100261 number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200262 return 0;
263}
264
265static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
266 struct pinctrl_pin_desc const *pins,
267 unsigned num_descs)
268{
269 unsigned i;
270 int ret = 0;
271
272 for (i = 0; i < num_descs; i++) {
273 ret = pinctrl_register_one_pin(pctldev,
274 pins[i].number, pins[i].name);
275 if (ret)
276 return ret;
277 }
278
279 return 0;
280}
281
282/**
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200283 * gpio_to_pin() - GPIO range GPIO number to pin number translation
284 * @range: GPIO range used for the translation
285 * @gpio: gpio pin to translate to a pin number
286 *
287 * Finds the pin number for a given GPIO using the specified GPIO range
288 * as a base for translation. The distinction between linear GPIO ranges
289 * and pin list based GPIO ranges is managed correctly by this function.
290 *
291 * This function assumes the gpio is part of the specified GPIO range, use
292 * only after making sure this is the case (e.g. by calling it on the
293 * result of successful pinctrl_get_device_gpio_range calls)!
294 */
295static inline int gpio_to_pin(struct pinctrl_gpio_range *range,
296 unsigned int gpio)
297{
298 unsigned int offset = gpio - range->base;
299 if (range->pins)
300 return range->pins[offset];
301 else
302 return range->pin_base + offset;
303}
304
305/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200306 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
307 * @pctldev: pin controller device to check
308 * @gpio: gpio pin to check taken from the global GPIO pin space
309 *
310 * Tries to match a GPIO pin number to the ranges handled by a certain pin
311 * controller, return the range or NULL
312 */
313static struct pinctrl_gpio_range *
314pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
315{
316 struct pinctrl_gpio_range *range = NULL;
317
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200318 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200319 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200320 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
321 /* Check if we're in the valid range */
322 if (gpio >= range->base &&
323 gpio < range->base + range->npins) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200324 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200325 return range;
326 }
327 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200328 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200329 return NULL;
330}
331
332/**
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800333 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
334 * the same GPIO chip are in range
335 * @gpio: gpio pin to check taken from the global GPIO pin space
336 *
337 * This function is complement of pinctrl_match_gpio_range(). If the return
338 * value of pinctrl_match_gpio_range() is NULL, this function could be used
339 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
340 * of the same GPIO chip don't have back-end pinctrl interface.
341 * If the return value is true, it means that pinctrl device is ready & the
342 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
343 * is false, it means that pinctrl device may not be ready.
344 */
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800345#ifdef CONFIG_GPIOLIB
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800346static bool pinctrl_ready_for_gpio_range(unsigned gpio)
347{
348 struct pinctrl_dev *pctldev;
349 struct pinctrl_gpio_range *range = NULL;
350 struct gpio_chip *chip = gpio_to_chip(gpio);
351
Tony Lindgren942cde72015-09-03 10:34:30 -0700352 if (WARN(!chip, "no gpio_chip for gpio%i?", gpio))
353 return false;
354
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200355 mutex_lock(&pinctrldev_list_mutex);
356
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800357 /* Loop over the pin controllers */
358 list_for_each_entry(pctldev, &pinctrldev_list, node) {
359 /* Loop over the ranges */
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800360 mutex_lock(&pctldev->mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800361 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
362 /* Check if any gpio range overlapped with gpio chip */
363 if (range->base + range->npins - 1 < chip->base ||
364 range->base > chip->base + chip->ngpio - 1)
365 continue;
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800366 mutex_unlock(&pctldev->mutex);
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200367 mutex_unlock(&pinctrldev_list_mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800368 return true;
369 }
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800370 mutex_unlock(&pctldev->mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800371 }
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200372
373 mutex_unlock(&pinctrldev_list_mutex);
374
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800375 return false;
376}
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800377#else
378static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
379#endif
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800380
381/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200382 * pinctrl_get_device_gpio_range() - find device for GPIO range
383 * @gpio: the pin to locate the pin controller for
384 * @outdev: the pin control device if found
385 * @outrange: the GPIO range if found
386 *
387 * Find the pin controller handling a certain GPIO pin from the pinspace of
388 * the GPIO subsystem, return the device and the matching GPIO range. Returns
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800389 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
390 * may still have not been registered.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200391 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700392static int pinctrl_get_device_gpio_range(unsigned gpio,
393 struct pinctrl_dev **outdev,
394 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200395{
396 struct pinctrl_dev *pctldev = NULL;
397
Axel Linf0059022013-08-18 20:34:22 +0800398 mutex_lock(&pinctrldev_list_mutex);
399
Linus Walleij2744e8a2011-05-02 20:50:54 +0200400 /* Loop over the pin controllers */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200401 list_for_each_entry(pctldev, &pinctrldev_list, node) {
402 struct pinctrl_gpio_range *range;
403
404 range = pinctrl_match_gpio_range(pctldev, gpio);
405 if (range != NULL) {
406 *outdev = pctldev;
407 *outrange = range;
Axel Linf0059022013-08-18 20:34:22 +0800408 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200409 return 0;
410 }
411 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200412
Axel Linf0059022013-08-18 20:34:22 +0800413 mutex_unlock(&pinctrldev_list_mutex);
414
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800415 return -EPROBE_DEFER;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200416}
417
418/**
419 * pinctrl_add_gpio_range() - register a GPIO range for a controller
420 * @pctldev: pin controller device to add the range to
421 * @range: the GPIO range to add
422 *
423 * This adds a range of GPIOs to be handled by a certain pin controller. Call
424 * this to register handled ranges after registering your pin controller.
425 */
426void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
427 struct pinctrl_gpio_range *range)
428{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200429 mutex_lock(&pctldev->mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700430 list_add_tail(&range->node, &pctldev->gpio_ranges);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200431 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200432}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700433EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200434
Dong Aisheng3e5e00b2012-05-23 21:22:41 +0800435void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
436 struct pinctrl_gpio_range *ranges,
437 unsigned nranges)
438{
439 int i;
440
441 for (i = 0; i < nranges; i++)
442 pinctrl_add_gpio_range(pctldev, &ranges[i]);
443}
444EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
445
Linus Walleij192c3692012-11-20 14:03:37 +0100446struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530447 struct pinctrl_gpio_range *range)
448{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200449 struct pinctrl_dev *pctldev;
450
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200451 pctldev = get_pinctrl_dev_from_devname(devname);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530452
Linus Walleijdfa97512012-11-20 14:54:18 +0100453 /*
454 * If we can't find this device, let's assume that is because
455 * it has not probed yet, so the driver trying to register this
456 * range need to defer probing.
457 */
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200458 if (!pctldev) {
Linus Walleijdfa97512012-11-20 14:54:18 +0100459 return ERR_PTR(-EPROBE_DEFER);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200460 }
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530461 pinctrl_add_gpio_range(pctldev, range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200462
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530463 return pctldev;
464}
Linus Walleij192c3692012-11-20 14:03:37 +0100465EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530466
Christian Ruppert586a87e2013-10-15 15:37:54 +0200467int pinctrl_get_group_pins(struct pinctrl_dev *pctldev, const char *pin_group,
468 const unsigned **pins, unsigned *num_pins)
469{
470 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
471 int gs;
472
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +0200473 if (!pctlops->get_group_pins)
474 return -EINVAL;
475
Christian Ruppert586a87e2013-10-15 15:37:54 +0200476 gs = pinctrl_get_group_selector(pctldev, pin_group);
477 if (gs < 0)
478 return gs;
479
480 return pctlops->get_group_pins(pctldev, gs, pins, num_pins);
481}
482EXPORT_SYMBOL_GPL(pinctrl_get_group_pins);
483
Joachim Eastwoodb18537c2016-02-25 22:44:37 +0100484struct pinctrl_gpio_range *
485pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev *pctldev,
486 unsigned int pin)
487{
488 struct pinctrl_gpio_range *range;
489
490 /* Loop over the ranges */
491 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
492 /* Check if we're in the valid range */
493 if (range->pins) {
494 int a;
495 for (a = 0; a < range->npins; a++) {
496 if (range->pins[a] == pin)
497 return range;
498 }
499 } else if (pin >= range->pin_base &&
500 pin < range->pin_base + range->npins)
501 return range;
502 }
503
504 return NULL;
505}
506EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin_nolock);
507
Linus Walleij2744e8a2011-05-02 20:50:54 +0200508/**
Linus Walleij9afbefb2012-11-20 14:25:07 +0100509 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
510 * @pctldev: the pin controller device to look in
511 * @pin: a controller-local number to find the range for
512 */
513struct pinctrl_gpio_range *
514pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
515 unsigned int pin)
516{
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800517 struct pinctrl_gpio_range *range;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100518
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200519 mutex_lock(&pctldev->mutex);
Joachim Eastwoodb18537c2016-02-25 22:44:37 +0100520 range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200521 mutex_unlock(&pctldev->mutex);
Joachim Eastwoodb18537c2016-02-25 22:44:37 +0100522
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800523 return range;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100524}
525EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
526
527/**
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530528 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
529 * @pctldev: pin controller device to remove the range from
530 * @range: the GPIO range to remove
531 */
532void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
533 struct pinctrl_gpio_range *range)
534{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200535 mutex_lock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530536 list_del(&range->node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200537 mutex_unlock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530538}
539EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
540
541/**
Linus Walleij7afde8b2011-10-19 17:07:16 +0200542 * pinctrl_get_group_selector() - returns the group selector for a group
543 * @pctldev: the pin controller handling the group
544 * @pin_group: the pin group to look up
545 */
546int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
547 const char *pin_group)
548{
549 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530550 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200551 unsigned group_selector = 0;
552
Viresh Kumard1e90e92012-03-30 11:25:40 +0530553 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200554 const char *gname = pctlops->get_group_name(pctldev,
555 group_selector);
556 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700557 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200558 "found group selector %u for %s\n",
559 group_selector,
560 pin_group);
561 return group_selector;
562 }
563
564 group_selector++;
565 }
566
Stephen Warren51cd24e2011-12-09 16:59:05 -0700567 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200568 pin_group);
569
570 return -EINVAL;
571}
572
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100573/**
Geert Uytterhoevenb217e432015-05-04 19:46:57 +0200574 * pinctrl_request_gpio() - request a single pin to be used as GPIO
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100575 * @gpio: the GPIO pin number from the GPIO subsystem number space
576 *
577 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
578 * as part of their gpio_request() semantics, platforms and individual drivers
579 * shall *NOT* request GPIO pins to be muxed in.
580 */
581int pinctrl_request_gpio(unsigned gpio)
582{
583 struct pinctrl_dev *pctldev;
584 struct pinctrl_gpio_range *range;
585 int ret;
586 int pin;
587
588 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700589 if (ret) {
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800590 if (pinctrl_ready_for_gpio_range(gpio))
591 ret = 0;
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800592 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700593 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100594
Axel Lin9b77ace2013-08-19 10:07:46 +0800595 mutex_lock(&pctldev->mutex);
596
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100597 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200598 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100599
Stephen Warren57b676f2012-03-02 13:05:44 -0700600 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
601
Axel Lin9b77ace2013-08-19 10:07:46 +0800602 mutex_unlock(&pctldev->mutex);
603
Stephen Warren57b676f2012-03-02 13:05:44 -0700604 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100605}
606EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
607
608/**
609 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
610 * @gpio: the GPIO pin number from the GPIO subsystem number space
611 *
612 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
613 * as part of their gpio_free() semantics, platforms and individual drivers
614 * shall *NOT* request GPIO pins to be muxed out.
615 */
616void pinctrl_free_gpio(unsigned gpio)
617{
618 struct pinctrl_dev *pctldev;
619 struct pinctrl_gpio_range *range;
620 int ret;
621 int pin;
622
623 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700624 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100625 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700626 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200627 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100628
629 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200630 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100631
Stephen Warren57b676f2012-03-02 13:05:44 -0700632 pinmux_free_gpio(pctldev, pin, range);
633
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200634 mutex_unlock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100635}
636EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
637
638static int pinctrl_gpio_direction(unsigned gpio, bool input)
639{
640 struct pinctrl_dev *pctldev;
641 struct pinctrl_gpio_range *range;
642 int ret;
643 int pin;
644
645 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200646 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100647 return ret;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200648 }
649
650 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100651
652 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200653 pin = gpio_to_pin(range, gpio);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200654 ret = pinmux_gpio_direction(pctldev, range, pin, input);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100655
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200656 mutex_unlock(&pctldev->mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200657
658 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100659}
660
661/**
662 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
663 * @gpio: the GPIO pin number from the GPIO subsystem number space
664 *
665 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
666 * as part of their gpio_direction_input() semantics, platforms and individual
667 * drivers shall *NOT* touch pin control GPIO calls.
668 */
669int pinctrl_gpio_direction_input(unsigned gpio)
670{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200671 return pinctrl_gpio_direction(gpio, true);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100672}
673EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
674
675/**
676 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
677 * @gpio: the GPIO pin number from the GPIO subsystem number space
678 *
679 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
680 * as part of their gpio_direction_output() semantics, platforms and individual
681 * drivers shall *NOT* touch pin control GPIO calls.
682 */
683int pinctrl_gpio_direction_output(unsigned gpio)
684{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200685 return pinctrl_gpio_direction(gpio, false);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100686}
687EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
688
Stephen Warren6e5e9592012-03-02 13:05:47 -0700689static struct pinctrl_state *find_state(struct pinctrl *p,
690 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100691{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700692 struct pinctrl_state *state;
693
694 list_for_each_entry(state, &p->states, node)
695 if (!strcmp(state->name, name))
696 return state;
697
698 return NULL;
699}
700
701static struct pinctrl_state *create_state(struct pinctrl *p,
702 const char *name)
703{
704 struct pinctrl_state *state;
705
706 state = kzalloc(sizeof(*state), GFP_KERNEL);
707 if (state == NULL) {
708 dev_err(p->dev,
709 "failed to alloc struct pinctrl_state\n");
710 return ERR_PTR(-ENOMEM);
711 }
712
713 state->name = name;
714 INIT_LIST_HEAD(&state->settings);
715
716 list_add_tail(&state->node, &p->states);
717
718 return state;
719}
720
721static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
722{
723 struct pinctrl_state *state;
724 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700725 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700726
727 state = find_state(p, map->name);
728 if (!state)
729 state = create_state(p, map->name);
730 if (IS_ERR(state))
731 return PTR_ERR(state);
732
Stephen Warren1e2082b2012-03-02 13:05:48 -0700733 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
734 return 0;
735
Stephen Warren6e5e9592012-03-02 13:05:47 -0700736 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
737 if (setting == NULL) {
738 dev_err(p->dev,
739 "failed to alloc struct pinctrl_setting\n");
740 return -ENOMEM;
741 }
742
Stephen Warren1e2082b2012-03-02 13:05:48 -0700743 setting->type = map->type;
744
Stephen Warren6e5e9592012-03-02 13:05:47 -0700745 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
746 if (setting->pctldev == NULL) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700747 kfree(setting);
Linus Walleij89216492012-12-11 14:14:32 +0100748 /* Do not defer probing of hogs (circular loop) */
749 if (!strcmp(map->ctrl_dev_name, map->dev_name))
750 return -ENODEV;
Linus Walleijc05127c2012-04-10 10:00:38 +0200751 /*
752 * OK let us guess that the driver is not there yet, and
753 * let's defer obtaining this pinctrl handle to later...
754 */
Linus Walleij89216492012-12-11 14:14:32 +0100755 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
756 map->ctrl_dev_name);
Linus Walleijc05127c2012-04-10 10:00:38 +0200757 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700758 }
759
Linus Walleij1a789582012-10-17 20:51:54 +0200760 setting->dev_name = map->dev_name;
761
Stephen Warren1e2082b2012-03-02 13:05:48 -0700762 switch (map->type) {
763 case PIN_MAP_TYPE_MUX_GROUP:
764 ret = pinmux_map_to_setting(map, setting);
765 break;
766 case PIN_MAP_TYPE_CONFIGS_PIN:
767 case PIN_MAP_TYPE_CONFIGS_GROUP:
768 ret = pinconf_map_to_setting(map, setting);
769 break;
770 default:
771 ret = -EINVAL;
772 break;
773 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700774 if (ret < 0) {
775 kfree(setting);
776 return ret;
777 }
778
779 list_add_tail(&setting->node, &state->settings);
780
781 return 0;
782}
783
784static struct pinctrl *find_pinctrl(struct device *dev)
785{
786 struct pinctrl *p;
787
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200788 mutex_lock(&pinctrl_list_mutex);
Stephen Warren1e2082b2012-03-02 13:05:48 -0700789 list_for_each_entry(p, &pinctrl_list, node)
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200790 if (p->dev == dev) {
791 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700792 return p;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200793 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700794
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200795 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700796 return NULL;
797}
798
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200799static void pinctrl_free(struct pinctrl *p, bool inlist);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700800
801static struct pinctrl *create_pinctrl(struct device *dev)
802{
803 struct pinctrl *p;
804 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700805 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100806 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700807 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700808 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100809
810 /*
811 * create the state cookie holder struct pinctrl for each
812 * mapping, this is what consumers will get when requesting
813 * a pin control handle with pinctrl_get()
814 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700815 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700816 if (p == NULL) {
817 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100818 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700819 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700820 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700821 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -0600822 INIT_LIST_HEAD(&p->dt_maps);
823
824 ret = pinctrl_dt_to_map(p);
825 if (ret < 0) {
826 kfree(p);
827 return ERR_PTR(ret);
828 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700829
830 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100831
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200832 mutex_lock(&pinctrl_maps_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100833 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700834 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -0700835 /* Map must be for this device */
836 if (strcmp(map->dev_name, devname))
837 continue;
838
Stephen Warren6e5e9592012-03-02 13:05:47 -0700839 ret = add_setting(p, map);
Linus Walleij89216492012-12-11 14:14:32 +0100840 /*
841 * At this point the adding of a setting may:
842 *
843 * - Defer, if the pinctrl device is not yet available
844 * - Fail, if the pinctrl device is not yet available,
845 * AND the setting is a hog. We cannot defer that, since
846 * the hog will kick in immediately after the device
847 * is registered.
848 *
849 * If the error returned was not -EPROBE_DEFER then we
850 * accumulate the errors to see if we end up with
851 * an -EPROBE_DEFER later, as that is the worst case.
852 */
853 if (ret == -EPROBE_DEFER) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200854 pinctrl_free(p, false);
855 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700856 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100857 }
858 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200859 mutex_unlock(&pinctrl_maps_mutex);
860
Linus Walleij89216492012-12-11 14:14:32 +0100861 if (ret < 0) {
862 /* If some other error than deferral occured, return here */
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200863 pinctrl_free(p, false);
Linus Walleij89216492012-12-11 14:14:32 +0100864 return ERR_PTR(ret);
865 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100866
Linus Walleijab780292013-01-22 10:56:14 -0700867 kref_init(&p->users);
868
Linus Walleijb0666ba2012-12-11 13:12:35 +0100869 /* Add the pinctrl handle to the global list */
Stanislaw Gruszka7b320cb2014-02-04 09:07:09 +0100870 mutex_lock(&pinctrl_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700871 list_add_tail(&p->node, &pinctrl_list);
Stanislaw Gruszka7b320cb2014-02-04 09:07:09 +0100872 mutex_unlock(&pinctrl_list_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100873
874 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700875}
Stephen Warren7ecdb162012-03-02 13:05:45 -0700876
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200877/**
878 * pinctrl_get() - retrieves the pinctrl handle for a device
879 * @dev: the device to obtain the handle for
880 */
881struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700882{
883 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700884
Stephen Warren6e5e9592012-03-02 13:05:47 -0700885 if (WARN_ON(!dev))
886 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700887
Linus Walleijab780292013-01-22 10:56:14 -0700888 /*
889 * See if somebody else (such as the device core) has already
890 * obtained a handle to the pinctrl for this device. In that case,
891 * return another pointer to it.
892 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700893 p = find_pinctrl(dev);
Linus Walleijab780292013-01-22 10:56:14 -0700894 if (p != NULL) {
895 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
896 kref_get(&p->users);
897 return p;
898 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700899
Richard Genoudd599bfb2012-08-10 16:53:49 +0200900 return create_pinctrl(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100901}
902EXPORT_SYMBOL_GPL(pinctrl_get);
903
Richard Genoudd3cee8302013-03-25 15:47:21 +0100904static void pinctrl_free_setting(bool disable_setting,
905 struct pinctrl_setting *setting)
906{
907 switch (setting->type) {
908 case PIN_MAP_TYPE_MUX_GROUP:
909 if (disable_setting)
910 pinmux_disable_setting(setting);
911 pinmux_free_setting(setting);
912 break;
913 case PIN_MAP_TYPE_CONFIGS_PIN:
914 case PIN_MAP_TYPE_CONFIGS_GROUP:
915 pinconf_free_setting(setting);
916 break;
917 default:
918 break;
919 }
920}
921
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200922static void pinctrl_free(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -0700923{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700924 struct pinctrl_state *state, *n1;
925 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700926
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200927 mutex_lock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700928 list_for_each_entry_safe(state, n1, &p->states, node) {
929 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Richard Genoudd3cee8302013-03-25 15:47:21 +0100930 pinctrl_free_setting(state == p->state, setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700931 list_del(&setting->node);
932 kfree(setting);
933 }
934 list_del(&state->node);
935 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700936 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700937
Stephen Warren57291ce2012-03-23 10:29:46 -0600938 pinctrl_dt_free_maps(p);
939
Stephen Warren6e5e9592012-03-02 13:05:47 -0700940 if (inlist)
941 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700942 kfree(p);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200943 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700944}
945
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100946/**
Linus Walleijab780292013-01-22 10:56:14 -0700947 * pinctrl_release() - release the pinctrl handle
948 * @kref: the kref in the pinctrl being released
949 */
Sachin Kamat2917e832013-01-24 15:01:00 +0530950static void pinctrl_release(struct kref *kref)
Linus Walleijab780292013-01-22 10:56:14 -0700951{
952 struct pinctrl *p = container_of(kref, struct pinctrl, users);
953
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200954 pinctrl_free(p, true);
Linus Walleijab780292013-01-22 10:56:14 -0700955}
956
957/**
958 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
Stephen Warren6e5e9592012-03-02 13:05:47 -0700959 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100960 */
961void pinctrl_put(struct pinctrl *p)
962{
Linus Walleijab780292013-01-22 10:56:14 -0700963 kref_put(&p->users, pinctrl_release);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100964}
965EXPORT_SYMBOL_GPL(pinctrl_put);
966
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200967/**
968 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
969 * @p: the pinctrl handle to retrieve the state from
970 * @name: the state name to retrieve
971 */
972struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
973 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -0700974{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700975 struct pinctrl_state *state;
976
977 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800978 if (!state) {
979 if (pinctrl_dummy_state) {
980 /* create dummy state */
981 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
982 name);
983 state = create_state(p, name);
Richard Genoudd599bfb2012-08-10 16:53:49 +0200984 } else
985 state = ERR_PTR(-ENODEV);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800986 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700987
988 return state;
989}
Stephen Warren6e5e9592012-03-02 13:05:47 -0700990EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
991
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200992/**
993 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
994 * @p: the pinctrl handle for the device that requests configuration
995 * @state: the state handle to select/activate/program
996 */
997int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700998{
999 struct pinctrl_setting *setting, *setting2;
Richard Genoud50cf7c82013-03-25 15:47:23 +01001000 struct pinctrl_state *old_state = p->state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001001 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -07001002
Stephen Warren6e5e9592012-03-02 13:05:47 -07001003 if (p->state == state)
1004 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -07001005
Stephen Warren6e5e9592012-03-02 13:05:47 -07001006 if (p->state) {
1007 /*
Fan Wu2243a872014-06-09 09:37:56 +08001008 * For each pinmux setting in the old state, forget SW's record
1009 * of mux owner for that pingroup. Any pingroups which are
1010 * still owned by the new state will be re-acquired by the call
1011 * to pinmux_enable_setting() in the loop below.
Stephen Warren6e5e9592012-03-02 13:05:47 -07001012 */
1013 list_for_each_entry(setting, &p->state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001014 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
1015 continue;
Fan Wu2243a872014-06-09 09:37:56 +08001016 pinmux_disable_setting(setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001017 }
1018 }
1019
Richard Genoud3102a762013-03-25 15:47:22 +01001020 p->state = NULL;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001021
1022 /* Apply all the settings for the new state */
1023 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001024 switch (setting->type) {
1025 case PIN_MAP_TYPE_MUX_GROUP:
1026 ret = pinmux_enable_setting(setting);
1027 break;
1028 case PIN_MAP_TYPE_CONFIGS_PIN:
1029 case PIN_MAP_TYPE_CONFIGS_GROUP:
1030 ret = pinconf_apply_setting(setting);
1031 break;
1032 default:
1033 ret = -EINVAL;
1034 break;
1035 }
Richard Genoud3102a762013-03-25 15:47:22 +01001036
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001037 if (ret < 0) {
Richard Genoud3102a762013-03-25 15:47:22 +01001038 goto unapply_new_state;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001039 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001040 }
1041
Richard Genoud3102a762013-03-25 15:47:22 +01001042 p->state = state;
1043
Stephen Warren7ecdb162012-03-02 13:05:45 -07001044 return 0;
Richard Genoud3102a762013-03-25 15:47:22 +01001045
1046unapply_new_state:
Richard Genoudda587512013-03-28 12:55:46 +01001047 dev_err(p->dev, "Error applying setting, reverse things back\n");
Richard Genoud3102a762013-03-25 15:47:22 +01001048
Richard Genoud3102a762013-03-25 15:47:22 +01001049 list_for_each_entry(setting2, &state->settings, node) {
1050 if (&setting2->node == &setting->node)
1051 break;
Richard Genoudaf606172013-03-29 10:03:26 +01001052 /*
1053 * All we can do here is pinmux_disable_setting.
1054 * That means that some pins are muxed differently now
1055 * than they were before applying the setting (We can't
1056 * "unmux a pin"!), but it's not a big deal since the pins
1057 * are free to be muxed by another apply_setting.
1058 */
1059 if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
1060 pinmux_disable_setting(setting2);
Richard Genoud3102a762013-03-25 15:47:22 +01001061 }
Richard Genoud8009d5f2013-03-28 12:55:47 +01001062
Richard Genoud385d9422013-03-29 10:03:27 +01001063 /* There's no infinite recursive loop here because p->state is NULL */
1064 if (old_state)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001065 pinctrl_select_state(p, old_state);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001066
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001067 return ret;
1068}
Stephen Warren6e5e9592012-03-02 13:05:47 -07001069EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001070
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001071static void devm_pinctrl_release(struct device *dev, void *res)
1072{
1073 pinctrl_put(*(struct pinctrl **)res);
1074}
1075
1076/**
1077 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
1078 * @dev: the device to obtain the handle for
1079 *
1080 * If there is a need to explicitly destroy the returned struct pinctrl,
1081 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1082 */
1083struct pinctrl *devm_pinctrl_get(struct device *dev)
1084{
1085 struct pinctrl **ptr, *p;
1086
1087 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
1088 if (!ptr)
1089 return ERR_PTR(-ENOMEM);
1090
1091 p = pinctrl_get(dev);
1092 if (!IS_ERR(p)) {
1093 *ptr = p;
1094 devres_add(dev, ptr);
1095 } else {
1096 devres_free(ptr);
1097 }
1098
1099 return p;
1100}
1101EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1102
1103static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1104{
1105 struct pinctrl **p = res;
1106
1107 return *p == data;
1108}
1109
1110/**
1111 * devm_pinctrl_put() - Resource managed pinctrl_put()
1112 * @p: the pinctrl handle to release
1113 *
1114 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1115 * this function will not need to be called and the resource management
1116 * code will ensure that the resource is freed.
1117 */
1118void devm_pinctrl_put(struct pinctrl *p)
1119{
Jingoo Hana72149e2013-02-26 11:34:07 +09001120 WARN_ON(devres_release(p->dev, devm_pinctrl_release,
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001121 devm_pinctrl_match, p));
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001122}
1123EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1124
Stephen Warren57291ce2012-03-23 10:29:46 -06001125int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
Doug Andersonc5272a22015-05-01 09:01:27 -07001126 bool dup)
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001127{
Stephen Warren1e2082b2012-03-02 13:05:48 -07001128 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001129 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001130
Masahiro Yamada7e9236f2015-05-28 21:52:59 +09001131 pr_debug("add %u pinctrl maps\n", num_maps);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001132
1133 /* First sanity check the new mapping */
1134 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001135 if (!maps[i].dev_name) {
1136 pr_err("failed to register map %s (%d): no device given\n",
1137 maps[i].name, i);
1138 return -EINVAL;
1139 }
1140
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001141 if (!maps[i].name) {
1142 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001143 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001144 return -EINVAL;
1145 }
1146
Stephen Warren1e2082b2012-03-02 13:05:48 -07001147 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1148 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001149 pr_err("failed to register map %s (%d): no pin control device given\n",
1150 maps[i].name, i);
1151 return -EINVAL;
1152 }
1153
Stephen Warren1e2082b2012-03-02 13:05:48 -07001154 switch (maps[i].type) {
1155 case PIN_MAP_TYPE_DUMMY_STATE:
1156 break;
1157 case PIN_MAP_TYPE_MUX_GROUP:
1158 ret = pinmux_validate_map(&maps[i], i);
1159 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001160 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001161 break;
1162 case PIN_MAP_TYPE_CONFIGS_PIN:
1163 case PIN_MAP_TYPE_CONFIGS_GROUP:
1164 ret = pinconf_validate_map(&maps[i], i);
1165 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001166 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001167 break;
1168 default:
1169 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001170 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -07001171 return -EINVAL;
1172 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001173 }
1174
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001175 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
1176 if (!maps_node) {
1177 pr_err("failed to alloc struct pinctrl_maps\n");
1178 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001179 }
1180
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001181 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -06001182 if (dup) {
1183 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
1184 GFP_KERNEL);
1185 if (!maps_node->maps) {
1186 pr_err("failed to duplicate mapping table\n");
1187 kfree(maps_node);
1188 return -ENOMEM;
1189 }
1190 } else {
1191 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001192 }
1193
Doug Andersonc5272a22015-05-01 09:01:27 -07001194 mutex_lock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001195 list_add_tail(&maps_node->node, &pinctrl_maps);
Doug Andersonc5272a22015-05-01 09:01:27 -07001196 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001197
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001198 return 0;
1199}
1200
Stephen Warren57291ce2012-03-23 10:29:46 -06001201/**
1202 * pinctrl_register_mappings() - register a set of pin controller mappings
1203 * @maps: the pincontrol mappings table to register. This should probably be
1204 * marked with __initdata so it can be discarded after boot. This
1205 * function will perform a shallow copy for the mapping entries.
1206 * @num_maps: the number of maps in the mapping table
1207 */
1208int pinctrl_register_mappings(struct pinctrl_map const *maps,
1209 unsigned num_maps)
1210{
Doug Andersonc5272a22015-05-01 09:01:27 -07001211 return pinctrl_register_map(maps, num_maps, true);
Stephen Warren57291ce2012-03-23 10:29:46 -06001212}
1213
1214void pinctrl_unregister_map(struct pinctrl_map const *map)
1215{
1216 struct pinctrl_maps *maps_node;
1217
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001218 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001219 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1220 if (maps_node->maps == map) {
1221 list_del(&maps_node->node);
Linus Walleijdb6c2c62013-07-23 01:43:20 +02001222 kfree(maps_node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001223 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001224 return;
1225 }
1226 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001227 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001228}
1229
Julien Delacou840a47b2012-12-10 14:47:33 +01001230/**
1231 * pinctrl_force_sleep() - turn a given controller device into sleep state
1232 * @pctldev: pin controller device
1233 */
1234int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1235{
1236 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
1237 return pinctrl_select_state(pctldev->p, pctldev->hog_sleep);
1238 return 0;
1239}
1240EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1241
1242/**
1243 * pinctrl_force_default() - turn a given controller device into default state
1244 * @pctldev: pin controller device
1245 */
1246int pinctrl_force_default(struct pinctrl_dev *pctldev)
1247{
1248 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
1249 return pinctrl_select_state(pctldev->p, pctldev->hog_default);
1250 return 0;
1251}
1252EXPORT_SYMBOL_GPL(pinctrl_force_default);
1253
Douglas Andersonef0eebc2015-10-20 21:15:06 -07001254/**
1255 * pinctrl_init_done() - tell pinctrl probe is done
1256 *
1257 * We'll use this time to switch the pins from "init" to "default" unless the
1258 * driver selected some other state.
1259 *
1260 * @dev: device to that's done probing
1261 */
1262int pinctrl_init_done(struct device *dev)
1263{
1264 struct dev_pin_info *pins = dev->pins;
1265 int ret;
1266
1267 if (!pins)
1268 return 0;
1269
1270 if (IS_ERR(pins->init_state))
1271 return 0; /* No such state */
1272
1273 if (pins->p->state != pins->init_state)
1274 return 0; /* Not at init anyway */
1275
1276 if (IS_ERR(pins->default_state))
1277 return 0; /* No default state */
1278
1279 ret = pinctrl_select_state(pins->p, pins->default_state);
1280 if (ret)
1281 dev_err(dev, "failed to activate default pinctrl state\n");
1282
1283 return ret;
1284}
1285
Linus Walleij14005ee2013-06-05 15:30:33 +02001286#ifdef CONFIG_PM
1287
1288/**
Tony Lindgrenf3333492013-07-18 08:15:04 -07001289 * pinctrl_pm_select_state() - select pinctrl state for PM
1290 * @dev: device to select default state for
1291 * @state: state to set
1292 */
1293static int pinctrl_pm_select_state(struct device *dev,
1294 struct pinctrl_state *state)
1295{
1296 struct dev_pin_info *pins = dev->pins;
1297 int ret;
1298
1299 if (IS_ERR(state))
1300 return 0; /* No such state */
1301 ret = pinctrl_select_state(pins->p, state);
1302 if (ret)
1303 dev_err(dev, "failed to activate pinctrl state %s\n",
1304 state->name);
1305 return ret;
1306}
1307
1308/**
Linus Walleij14005ee2013-06-05 15:30:33 +02001309 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1310 * @dev: device to select default state for
1311 */
1312int pinctrl_pm_select_default_state(struct device *dev)
1313{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001314 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001315 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001316
1317 return pinctrl_pm_select_state(dev, dev->pins->default_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001318}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001319EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001320
1321/**
1322 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1323 * @dev: device to select sleep state for
1324 */
1325int pinctrl_pm_select_sleep_state(struct device *dev)
1326{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001327 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001328 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001329
1330 return pinctrl_pm_select_state(dev, dev->pins->sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001331}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001332EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001333
1334/**
1335 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1336 * @dev: device to select idle state for
1337 */
1338int pinctrl_pm_select_idle_state(struct device *dev)
1339{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001340 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001341 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001342
1343 return pinctrl_pm_select_state(dev, dev->pins->idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001344}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001345EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001346#endif
1347
Linus Walleij2744e8a2011-05-02 20:50:54 +02001348#ifdef CONFIG_DEBUG_FS
1349
1350static int pinctrl_pins_show(struct seq_file *s, void *what)
1351{
1352 struct pinctrl_dev *pctldev = s->private;
1353 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001354 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001355
1356 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001357
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001358 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001359
Chanho Park706e8522012-01-03 16:47:50 +09001360 /* The pin number can be retrived from the pin controller descriptor */
1361 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001362 struct pin_desc *desc;
1363
Chanho Park706e8522012-01-03 16:47:50 +09001364 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001365 desc = pin_desc_get(pctldev, pin);
1366 /* Pin space may be sparse */
1367 if (desc == NULL)
1368 continue;
1369
1370 seq_printf(s, "pin %d (%s) ", pin,
1371 desc->name ? desc->name : "unnamed");
1372
1373 /* Driver-specific info per pin */
1374 if (ops->pin_dbg_show)
1375 ops->pin_dbg_show(pctldev, s, pin);
1376
1377 seq_puts(s, "\n");
1378 }
1379
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001380 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001381
Linus Walleij2744e8a2011-05-02 20:50:54 +02001382 return 0;
1383}
1384
1385static int pinctrl_groups_show(struct seq_file *s, void *what)
1386{
1387 struct pinctrl_dev *pctldev = s->private;
1388 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301389 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001390
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001391 mutex_lock(&pctldev->mutex);
1392
Viresh Kumard1e90e92012-03-30 11:25:40 +05301393 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001394
Linus Walleij2744e8a2011-05-02 20:50:54 +02001395 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301396 while (selector < ngroups) {
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001397 const unsigned *pins = NULL;
1398 unsigned num_pins = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001399 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001400 const char *pname;
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001401 int ret = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001402 int i;
1403
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001404 if (ops->get_group_pins)
1405 ret = ops->get_group_pins(pctldev, selector,
1406 &pins, &num_pins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001407 if (ret)
1408 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1409 gname);
1410 else {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001411 seq_printf(s, "group: %s\n", gname);
1412 for (i = 0; i < num_pins; i++) {
1413 pname = pin_get_name(pctldev, pins[i]);
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001414 if (WARN_ON(!pname)) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001415 mutex_unlock(&pctldev->mutex);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001416 return -EINVAL;
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001417 }
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001418 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1419 }
1420 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001421 }
1422 selector++;
1423 }
1424
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001425 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001426
1427 return 0;
1428}
1429
1430static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1431{
1432 struct pinctrl_dev *pctldev = s->private;
1433 struct pinctrl_gpio_range *range = NULL;
1434
1435 seq_puts(s, "GPIO ranges handled:\n");
1436
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001437 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001438
Linus Walleij2744e8a2011-05-02 20:50:54 +02001439 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001440 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Christian Ruppertc8587ee2013-06-13 14:55:31 +02001441 if (range->pins) {
1442 int a;
1443 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
1444 range->id, range->name,
1445 range->base, (range->base + range->npins - 1));
1446 for (a = 0; a < range->npins - 1; a++)
1447 seq_printf(s, "%u, ", range->pins[a]);
1448 seq_printf(s, "%u}\n", range->pins[a]);
1449 }
1450 else
1451 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1452 range->id, range->name,
1453 range->base, (range->base + range->npins - 1),
1454 range->pin_base,
1455 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001456 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001457
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001458 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001459
1460 return 0;
1461}
1462
1463static int pinctrl_devices_show(struct seq_file *s, void *what)
1464{
1465 struct pinctrl_dev *pctldev;
1466
Linus Walleijae6b4d82011-10-19 18:14:33 +02001467 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001468
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001469 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001470
Linus Walleij2744e8a2011-05-02 20:50:54 +02001471 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1472 seq_printf(s, "%s ", pctldev->desc->name);
1473 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001474 seq_puts(s, "yes ");
1475 else
1476 seq_puts(s, "no ");
1477 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001478 seq_puts(s, "yes");
1479 else
1480 seq_puts(s, "no");
1481 seq_puts(s, "\n");
1482 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001483
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001484 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001485
1486 return 0;
1487}
1488
Stephen Warren1e2082b2012-03-02 13:05:48 -07001489static inline const char *map_type(enum pinctrl_map_type type)
1490{
1491 static const char * const names[] = {
1492 "INVALID",
1493 "DUMMY_STATE",
1494 "MUX_GROUP",
1495 "CONFIGS_PIN",
1496 "CONFIGS_GROUP",
1497 };
1498
1499 if (type >= ARRAY_SIZE(names))
1500 return "UNKNOWN";
1501
1502 return names[type];
1503}
1504
Stephen Warren3eedb432012-02-23 17:04:40 -07001505static int pinctrl_maps_show(struct seq_file *s, void *what)
1506{
1507 struct pinctrl_maps *maps_node;
1508 int i;
1509 struct pinctrl_map const *map;
1510
1511 seq_puts(s, "Pinctrl maps:\n");
1512
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001513 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001514 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001515 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1516 map->dev_name, map->name, map_type(map->type),
1517 map->type);
1518
1519 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1520 seq_printf(s, "controlling device %s\n",
1521 map->ctrl_dev_name);
1522
1523 switch (map->type) {
1524 case PIN_MAP_TYPE_MUX_GROUP:
1525 pinmux_show_map(s, map);
1526 break;
1527 case PIN_MAP_TYPE_CONFIGS_PIN:
1528 case PIN_MAP_TYPE_CONFIGS_GROUP:
1529 pinconf_show_map(s, map);
1530 break;
1531 default:
1532 break;
1533 }
1534
1535 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001536 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001537 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001538
1539 return 0;
1540}
1541
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001542static int pinctrl_show(struct seq_file *s, void *what)
1543{
1544 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001545 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001546 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001547
1548 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001549
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001550 mutex_lock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001551
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001552 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001553 seq_printf(s, "device: %s current state: %s\n",
1554 dev_name(p->dev),
1555 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001556
Stephen Warren6e5e9592012-03-02 13:05:47 -07001557 list_for_each_entry(state, &p->states, node) {
1558 seq_printf(s, " state: %s\n", state->name);
1559
1560 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001561 struct pinctrl_dev *pctldev = setting->pctldev;
1562
1563 seq_printf(s, " type: %s controller %s ",
1564 map_type(setting->type),
1565 pinctrl_dev_get_name(pctldev));
1566
1567 switch (setting->type) {
1568 case PIN_MAP_TYPE_MUX_GROUP:
1569 pinmux_show_setting(s, setting);
1570 break;
1571 case PIN_MAP_TYPE_CONFIGS_PIN:
1572 case PIN_MAP_TYPE_CONFIGS_GROUP:
1573 pinconf_show_setting(s, setting);
1574 break;
1575 default:
1576 break;
1577 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001578 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001579 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001580 }
1581
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001582 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001583
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001584 return 0;
1585}
1586
Linus Walleij2744e8a2011-05-02 20:50:54 +02001587static int pinctrl_pins_open(struct inode *inode, struct file *file)
1588{
1589 return single_open(file, pinctrl_pins_show, inode->i_private);
1590}
1591
1592static int pinctrl_groups_open(struct inode *inode, struct file *file)
1593{
1594 return single_open(file, pinctrl_groups_show, inode->i_private);
1595}
1596
1597static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1598{
1599 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1600}
1601
1602static int pinctrl_devices_open(struct inode *inode, struct file *file)
1603{
1604 return single_open(file, pinctrl_devices_show, NULL);
1605}
1606
Stephen Warren3eedb432012-02-23 17:04:40 -07001607static int pinctrl_maps_open(struct inode *inode, struct file *file)
1608{
1609 return single_open(file, pinctrl_maps_show, NULL);
1610}
1611
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001612static int pinctrl_open(struct inode *inode, struct file *file)
1613{
1614 return single_open(file, pinctrl_show, NULL);
1615}
1616
Linus Walleij2744e8a2011-05-02 20:50:54 +02001617static const struct file_operations pinctrl_pins_ops = {
1618 .open = pinctrl_pins_open,
1619 .read = seq_read,
1620 .llseek = seq_lseek,
1621 .release = single_release,
1622};
1623
1624static const struct file_operations pinctrl_groups_ops = {
1625 .open = pinctrl_groups_open,
1626 .read = seq_read,
1627 .llseek = seq_lseek,
1628 .release = single_release,
1629};
1630
1631static const struct file_operations pinctrl_gpioranges_ops = {
1632 .open = pinctrl_gpioranges_open,
1633 .read = seq_read,
1634 .llseek = seq_lseek,
1635 .release = single_release,
1636};
1637
1638static const struct file_operations pinctrl_devices_ops = {
1639 .open = pinctrl_devices_open,
1640 .read = seq_read,
1641 .llseek = seq_lseek,
1642 .release = single_release,
1643};
1644
Stephen Warren3eedb432012-02-23 17:04:40 -07001645static const struct file_operations pinctrl_maps_ops = {
1646 .open = pinctrl_maps_open,
1647 .read = seq_read,
1648 .llseek = seq_lseek,
1649 .release = single_release,
1650};
1651
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001652static const struct file_operations pinctrl_ops = {
1653 .open = pinctrl_open,
1654 .read = seq_read,
1655 .llseek = seq_lseek,
1656 .release = single_release,
1657};
1658
Linus Walleij2744e8a2011-05-02 20:50:54 +02001659static struct dentry *debugfs_root;
1660
1661static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1662{
Tony Lindgren02157162012-01-20 08:17:22 -08001663 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001664
Stephen Warren51cd24e2011-12-09 16:59:05 -07001665 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001666 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001667 pctldev->device_root = device_root;
1668
Linus Walleij2744e8a2011-05-02 20:50:54 +02001669 if (IS_ERR(device_root) || !device_root) {
1670 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001671 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001672 return;
1673 }
1674 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1675 device_root, pctldev, &pinctrl_pins_ops);
1676 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1677 device_root, pctldev, &pinctrl_groups_ops);
1678 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1679 device_root, pctldev, &pinctrl_gpioranges_ops);
Florian Vaussarde7f2a442014-02-05 07:51:22 +01001680 if (pctldev->desc->pmxops)
1681 pinmux_init_device_debugfs(device_root, pctldev);
1682 if (pctldev->desc->confops)
1683 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001684}
1685
Tony Lindgren02157162012-01-20 08:17:22 -08001686static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1687{
1688 debugfs_remove_recursive(pctldev->device_root);
1689}
1690
Linus Walleij2744e8a2011-05-02 20:50:54 +02001691static void pinctrl_init_debugfs(void)
1692{
1693 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1694 if (IS_ERR(debugfs_root) || !debugfs_root) {
1695 pr_warn("failed to create debugfs directory\n");
1696 debugfs_root = NULL;
1697 return;
1698 }
1699
1700 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1701 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001702 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1703 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001704 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1705 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001706}
1707
1708#else /* CONFIG_DEBUG_FS */
1709
1710static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1711{
1712}
1713
1714static void pinctrl_init_debugfs(void)
1715{
1716}
1717
Tony Lindgren02157162012-01-20 08:17:22 -08001718static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1719{
1720}
1721
Linus Walleij2744e8a2011-05-02 20:50:54 +02001722#endif
1723
Stephen Warrend26bc492012-03-16 14:54:25 -06001724static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1725{
1726 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1727
1728 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301729 !ops->get_groups_count ||
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001730 !ops->get_group_name)
Stephen Warrend26bc492012-03-16 14:54:25 -06001731 return -EINVAL;
1732
Stephen Warren57291ce2012-03-23 10:29:46 -06001733 if (ops->dt_node_to_map && !ops->dt_free_map)
1734 return -EINVAL;
1735
Stephen Warrend26bc492012-03-16 14:54:25 -06001736 return 0;
1737}
1738
Linus Walleij2744e8a2011-05-02 20:50:54 +02001739/**
1740 * pinctrl_register() - register a pin controller device
1741 * @pctldesc: descriptor for this pin controller
1742 * @dev: parent device for this pin controller
1743 * @driver_data: private pin controller data for this pin controller
1744 */
1745struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1746 struct device *dev, void *driver_data)
1747{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001748 struct pinctrl_dev *pctldev;
1749 int ret;
1750
Devendra Nagada9aecb2012-06-16 23:43:16 +05301751 if (!pctldesc)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001752 return ERR_PTR(-EINVAL);
Devendra Nagada9aecb2012-06-16 23:43:16 +05301753 if (!pctldesc->name)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001754 return ERR_PTR(-EINVAL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001755
Stephen Warren02f5b982012-02-22 14:26:00 -07001756 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001757 if (pctldev == NULL) {
1758 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001759 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001760 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001761
1762 /* Initialize pin control device struct */
1763 pctldev->owner = pctldesc->owner;
1764 pctldev->desc = pctldesc;
1765 pctldev->driver_data = driver_data;
1766 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001767 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001768 pctldev->dev = dev;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001769 mutex_init(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001770
Stephen Warrend26bc492012-03-16 14:54:25 -06001771 /* check core ops for sanity */
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001772 ret = pinctrl_check_ops(pctldev);
1773 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001774 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06001775 goto out_err;
1776 }
1777
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001778 /* If we're implementing pinmuxing, check the ops for sanity */
1779 if (pctldesc->pmxops) {
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001780 ret = pinmux_check_ops(pctldev);
1781 if (ret)
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001782 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001783 }
1784
1785 /* If we're implementing pinconfig, check the ops for sanity */
1786 if (pctldesc->confops) {
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001787 ret = pinconf_check_ops(pctldev);
1788 if (ret)
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001789 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001790 }
1791
Linus Walleij2744e8a2011-05-02 20:50:54 +02001792 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02001793 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001794 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1795 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001796 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001797 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1798 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001799 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001800 }
1801
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001802 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -07001803 list_add_tail(&pctldev->node, &pinctrldev_list);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001804 mutex_unlock(&pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001805
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001806 pctldev->p = pinctrl_get(pctldev->dev);
1807
Stephen Warren6e5e9592012-03-02 13:05:47 -07001808 if (!IS_ERR(pctldev->p)) {
Julien Delacou840a47b2012-12-10 14:47:33 +01001809 pctldev->hog_default =
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001810 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
Julien Delacou840a47b2012-12-10 14:47:33 +01001811 if (IS_ERR(pctldev->hog_default)) {
John Crispinad6e1102012-04-26 16:47:11 +02001812 dev_dbg(dev, "failed to lookup the default state\n");
1813 } else {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001814 if (pinctrl_select_state(pctldev->p,
Julien Delacou840a47b2012-12-10 14:47:33 +01001815 pctldev->hog_default))
John Crispinad6e1102012-04-26 16:47:11 +02001816 dev_err(dev,
1817 "failed to select default state\n");
John Crispinad6e1102012-04-26 16:47:11 +02001818 }
Julien Delacou840a47b2012-12-10 14:47:33 +01001819
1820 pctldev->hog_sleep =
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001821 pinctrl_lookup_state(pctldev->p,
Julien Delacou840a47b2012-12-10 14:47:33 +01001822 PINCTRL_STATE_SLEEP);
1823 if (IS_ERR(pctldev->hog_sleep))
1824 dev_dbg(dev, "failed to lookup the sleep state\n");
Stephen Warren6e5e9592012-03-02 13:05:47 -07001825 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001826
Stephen Warren2304b472012-02-22 14:26:01 -07001827 pinctrl_init_device_debugfs(pctldev);
1828
Linus Walleij2744e8a2011-05-02 20:50:54 +02001829 return pctldev;
1830
Stephen Warren51cd24e2011-12-09 16:59:05 -07001831out_err:
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001832 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001833 kfree(pctldev);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001834 return ERR_PTR(ret);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001835}
1836EXPORT_SYMBOL_GPL(pinctrl_register);
1837
1838/**
1839 * pinctrl_unregister() - unregister pinmux
1840 * @pctldev: pin controller to unregister
1841 *
1842 * Called by pinmux drivers to unregister a pinmux.
1843 */
1844void pinctrl_unregister(struct pinctrl_dev *pctldev)
1845{
Dong Aisheng5d589b02012-05-23 21:22:40 +08001846 struct pinctrl_gpio_range *range, *n;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001847 if (pctldev == NULL)
1848 return;
1849
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001850 mutex_lock(&pctldev->mutex);
Tony Lindgren02157162012-01-20 08:17:22 -08001851 pinctrl_remove_device_debugfs(pctldev);
Jim Lindb93fac2015-01-08 20:25:05 +08001852 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001853
Stephen Warren6e5e9592012-03-02 13:05:47 -07001854 if (!IS_ERR(pctldev->p))
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001855 pinctrl_put(pctldev->p);
Stephen Warren57b676f2012-03-02 13:05:44 -07001856
Jim Lindb93fac2015-01-08 20:25:05 +08001857 mutex_lock(&pinctrldev_list_mutex);
1858 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001859 /* TODO: check that no pinmuxes are still active? */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001860 list_del(&pctldev->node);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001861 /* Destroy descriptor tree */
1862 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1863 pctldev->desc->npins);
Dong Aisheng5d589b02012-05-23 21:22:40 +08001864 /* remove gpio ranges map */
1865 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1866 list_del(&range->node);
1867
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001868 mutex_unlock(&pctldev->mutex);
1869 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001870 kfree(pctldev);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001871 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001872}
1873EXPORT_SYMBOL_GPL(pinctrl_unregister);
1874
1875static int __init pinctrl_init(void)
1876{
1877 pr_info("initialized pinctrl subsystem\n");
1878 pinctrl_init_debugfs();
1879 return 0;
1880}
1881
1882/* init early since many drivers really need to initialized pinmux early */
1883core_initcall(pinctrl_init);