blob: 893b1acb3e6e5e91740ed8c1c5602c0f51a500dd [file] [log] [blame]
Thomas Gleixneraf873fc2019-05-28 09:57:21 -07001// SPDX-License-Identifier: GPL-2.0-only
Linus Walleij2744e8a2011-05-02 20:50:54 +02002/*
3 * Core driver for the pin control subsystem
4 *
Linus Walleijbefe5bd2012-02-09 19:47:48 +01005 * Copyright (C) 2011-2012 ST-Ericsson SA
Linus Walleij2744e8a2011-05-02 20:50:54 +02006 * Written on behalf of Linaro for ST-Ericsson
7 * Based on bits of regulator core, gpio core and clk core
8 *
9 * Author: Linus Walleij <linus.walleij@linaro.org>
10 *
Stephen Warrenb2b3e662012-02-19 23:45:43 -070011 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
Linus Walleij2744e8a2011-05-02 20:50:54 +020012 */
13#define pr_fmt(fmt) "pinctrl core: " fmt
14
15#include <linux/kernel.h>
Linus Walleijab780292013-01-22 10:56:14 -070016#include <linux/kref.h>
Stephen Rothwella5a697c2011-09-30 14:39:04 +100017#include <linux/export.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020018#include <linux/init.h>
19#include <linux/device.h>
20#include <linux/slab.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020021#include <linux/err.h>
22#include <linux/list.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020023#include <linux/debugfs.h>
24#include <linux/seq_file.h>
Stephen Warren6d4ca1f2012-04-16 10:51:00 -060025#include <linux/pinctrl/consumer.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020026#include <linux/pinctrl/pinctrl.h>
27#include <linux/pinctrl/machine.h>
Haojian Zhuang2afe8222013-03-28 07:34:19 +080028
29#ifdef CONFIG_GPIOLIB
Haojian Zhuang51e13c22013-02-17 19:42:50 +080030#include <asm-generic/gpio.h>
Haojian Zhuang2afe8222013-03-28 07:34:19 +080031#endif
32
Linus Walleij2744e8a2011-05-02 20:50:54 +020033#include "core.h"
Stephen Warren57291ce2012-03-23 10:29:46 -060034#include "devicetree.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020035#include "pinmux.h"
Linus Walleijae6b4d82011-10-19 18:14:33 +020036#include "pinconf.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020037
Stephen Warrenb2b3e662012-02-19 23:45:43 -070038
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080039static bool pinctrl_dummy_state;
40
Patrice Chotard42fed7b2013-04-11 11:01:27 +020041/* Mutex taken to protect pinctrl_list */
Sachin Kamat843aec92013-06-18 14:34:27 +053042static DEFINE_MUTEX(pinctrl_list_mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +020043
44/* Mutex taken to protect pinctrl_maps */
45DEFINE_MUTEX(pinctrl_maps_mutex);
46
47/* Mutex taken to protect pinctrldev_list */
Sachin Kamat843aec92013-06-18 14:34:27 +053048static DEFINE_MUTEX(pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -070049
50/* Global list of pin control devices (struct pinctrl_dev) */
Patrice Chotard42fed7b2013-04-11 11:01:27 +020051static LIST_HEAD(pinctrldev_list);
Linus Walleij2744e8a2011-05-02 20:50:54 +020052
Stephen Warren57b676f2012-03-02 13:05:44 -070053/* List of pin controller handles (struct pinctrl) */
Linus Walleijbefe5bd2012-02-09 19:47:48 +010054static LIST_HEAD(pinctrl_list);
55
Stephen Warren57b676f2012-03-02 13:05:44 -070056/* List of pinctrl maps (struct pinctrl_maps) */
Laurent Meunier6f9e41f2013-02-06 09:09:44 +010057LIST_HEAD(pinctrl_maps);
Stephen Warrenb2b3e662012-02-19 23:45:43 -070058
Linus Walleijbefe5bd2012-02-09 19:47:48 +010059
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080060/**
61 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
62 *
63 * Usually this function is called by platforms without pinctrl driver support
64 * but run with some shared drivers using pinctrl APIs.
65 * After calling this function, the pinctrl core will return successfully
66 * with creating a dummy state for the driver to keep going smoothly.
67 */
68void pinctrl_provide_dummies(void)
69{
70 pinctrl_dummy_state = true;
71}
72
Linus Walleij2744e8a2011-05-02 20:50:54 +020073const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
74{
75 /* We're not allowed to register devices without name */
76 return pctldev->desc->name;
77}
78EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
79
Haojian Zhuangd6e99ab2013-01-18 15:31:06 +080080const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
81{
82 return dev_name(pctldev->dev);
83}
84EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
85
Linus Walleij2744e8a2011-05-02 20:50:54 +020086void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
87{
88 return pctldev->driver_data;
89}
90EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
91
92/**
Linus Walleij9dfac4f2012-02-01 18:02:47 +010093 * get_pinctrl_dev_from_devname() - look up pin controller device
94 * @devname: the name of a device instance, as returned by dev_name()
Linus Walleij2744e8a2011-05-02 20:50:54 +020095 *
96 * Looks up a pin control device matching a certain device name or pure device
97 * pointer, the pure device pointer will take precedence.
98 */
Linus Walleij9dfac4f2012-02-01 18:02:47 +010099struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200100{
Masahiro Yamada6cadafb2019-06-09 23:55:37 +0900101 struct pinctrl_dev *pctldev;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200102
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100103 if (!devname)
104 return NULL;
105
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200106 mutex_lock(&pinctrldev_list_mutex);
107
Linus Walleij2744e8a2011-05-02 20:50:54 +0200108 list_for_each_entry(pctldev, &pinctrldev_list, node) {
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100109 if (!strcmp(dev_name(pctldev->dev), devname)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200110 /* Matched on device name */
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200111 mutex_unlock(&pinctrldev_list_mutex);
112 return pctldev;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200113 }
114 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200115
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200116 mutex_unlock(&pinctrldev_list_mutex);
117
118 return NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200119}
120
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200121struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np)
122{
123 struct pinctrl_dev *pctldev;
124
125 mutex_lock(&pinctrldev_list_mutex);
126
127 list_for_each_entry(pctldev, &pinctrldev_list, node)
128 if (pctldev->dev->of_node == np) {
129 mutex_unlock(&pinctrldev_list_mutex);
130 return pctldev;
131 }
132
Daniel Mackd463f822013-04-26 18:57:02 +0200133 mutex_unlock(&pinctrldev_list_mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200134
135 return NULL;
136}
137
Linus Walleij2744e8a2011-05-02 20:50:54 +0200138/**
Linus Walleijae6b4d82011-10-19 18:14:33 +0200139 * pin_get_from_name() - look up a pin number from a name
140 * @pctldev: the pin control device to lookup the pin on
141 * @name: the name of the pin to look up
142 */
143int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
144{
Chanho Park706e8522012-01-03 16:47:50 +0900145 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200146
Chanho Park706e8522012-01-03 16:47:50 +0900147 /* The pin number can be retrived from the pin controller descriptor */
148 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200149 struct pin_desc *desc;
150
Chanho Park706e8522012-01-03 16:47:50 +0900151 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200152 desc = pin_desc_get(pctldev, pin);
153 /* Pin space may be sparse */
Axel Lin6c325f82013-08-19 10:06:29 +0800154 if (desc && !strcmp(name, desc->name))
Linus Walleijae6b4d82011-10-19 18:14:33 +0200155 return pin;
156 }
157
158 return -EINVAL;
159}
160
161/**
Dong Aishengdcb5dbc2012-04-17 15:00:46 +0800162 * pin_get_name_from_id() - look up a pin name from a pin id
163 * @pctldev: the pin control device to lookup the pin on
164 * @name: the name of the pin to look up
165 */
166const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
167{
168 const struct pin_desc *desc;
169
170 desc = pin_desc_get(pctldev, pin);
Markus Elfringcea234e2017-05-02 11:04:55 +0200171 if (!desc) {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +0800172 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
173 pin);
174 return NULL;
175 }
176
177 return desc->name;
178}
Baolin Wangb88d1452020-02-27 12:13:46 +0800179EXPORT_SYMBOL_GPL(pin_get_name);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +0800180
Linus Walleij2744e8a2011-05-02 20:50:54 +0200181/* Deletes a range of pin descriptors */
182static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
183 const struct pinctrl_pin_desc *pins,
184 unsigned num_pins)
185{
186 int i;
187
Linus Walleij2744e8a2011-05-02 20:50:54 +0200188 for (i = 0; i < num_pins; i++) {
189 struct pin_desc *pindesc;
190
191 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
192 pins[i].number);
Markus Elfringcea234e2017-05-02 11:04:55 +0200193 if (pindesc) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200194 radix_tree_delete(&pctldev->pin_desc_tree,
195 pins[i].number);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100196 if (pindesc->dynamic_name)
197 kfree(pindesc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200198 }
199 kfree(pindesc);
200 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200201}
202
203static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900204 const struct pinctrl_pin_desc *pin)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200205{
206 struct pin_desc *pindesc;
207
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900208 pindesc = pin_desc_get(pctldev, pin->number);
Markus Elfringcea234e2017-05-02 11:04:55 +0200209 if (pindesc) {
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900210 dev_err(pctldev->dev, "pin %d already registered\n",
211 pin->number);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200212 return -EINVAL;
213 }
214
215 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -0800216 if (!pindesc)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200217 return -ENOMEM;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200218
Linus Walleij2744e8a2011-05-02 20:50:54 +0200219 /* Set owner */
220 pindesc->pctldev = pctldev;
221
Stephen Warren9af1e442011-10-19 16:19:27 -0600222 /* Copy basic pin info */
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900223 if (pin->name) {
224 pindesc->name = pin->name;
Linus Walleijca53c5f2011-12-14 20:33:37 +0100225 } else {
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900226 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", pin->number);
Markus Elfringcea234e2017-05-02 11:04:55 +0200227 if (!pindesc->name) {
Sachin Kamateb26cc92012-09-25 12:41:40 +0530228 kfree(pindesc);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100229 return -ENOMEM;
Sachin Kamateb26cc92012-09-25 12:41:40 +0530230 }
Linus Walleijca53c5f2011-12-14 20:33:37 +0100231 pindesc->dynamic_name = true;
232 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200233
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900234 pindesc->drv_data = pin->drv_data;
235
236 radix_tree_insert(&pctldev->pin_desc_tree, pin->number, pindesc);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200237 pr_debug("registered pin %d (%s) on %s\n",
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900238 pin->number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200239 return 0;
240}
241
242static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900243 const struct pinctrl_pin_desc *pins,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200244 unsigned num_descs)
245{
246 unsigned i;
247 int ret = 0;
248
249 for (i = 0; i < num_descs; i++) {
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900250 ret = pinctrl_register_one_pin(pctldev, &pins[i]);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200251 if (ret)
252 return ret;
253 }
254
255 return 0;
256}
257
258/**
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200259 * gpio_to_pin() - GPIO range GPIO number to pin number translation
260 * @range: GPIO range used for the translation
261 * @gpio: gpio pin to translate to a pin number
262 *
263 * Finds the pin number for a given GPIO using the specified GPIO range
264 * as a base for translation. The distinction between linear GPIO ranges
265 * and pin list based GPIO ranges is managed correctly by this function.
266 *
267 * This function assumes the gpio is part of the specified GPIO range, use
268 * only after making sure this is the case (e.g. by calling it on the
269 * result of successful pinctrl_get_device_gpio_range calls)!
270 */
271static inline int gpio_to_pin(struct pinctrl_gpio_range *range,
272 unsigned int gpio)
273{
274 unsigned int offset = gpio - range->base;
275 if (range->pins)
276 return range->pins[offset];
277 else
278 return range->pin_base + offset;
279}
280
281/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200282 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
283 * @pctldev: pin controller device to check
284 * @gpio: gpio pin to check taken from the global GPIO pin space
285 *
286 * Tries to match a GPIO pin number to the ranges handled by a certain pin
287 * controller, return the range or NULL
288 */
289static struct pinctrl_gpio_range *
290pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
291{
Masahiro Yamada6cadafb2019-06-09 23:55:37 +0900292 struct pinctrl_gpio_range *range;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200293
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200294 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200295 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200296 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
297 /* Check if we're in the valid range */
298 if (gpio >= range->base &&
299 gpio < range->base + range->npins) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200300 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200301 return range;
302 }
303 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200304 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200305 return NULL;
306}
307
308/**
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800309 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
310 * the same GPIO chip are in range
311 * @gpio: gpio pin to check taken from the global GPIO pin space
312 *
313 * This function is complement of pinctrl_match_gpio_range(). If the return
314 * value of pinctrl_match_gpio_range() is NULL, this function could be used
315 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
316 * of the same GPIO chip don't have back-end pinctrl interface.
317 * If the return value is true, it means that pinctrl device is ready & the
318 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
319 * is false, it means that pinctrl device may not be ready.
320 */
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800321#ifdef CONFIG_GPIOLIB
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800322static bool pinctrl_ready_for_gpio_range(unsigned gpio)
323{
324 struct pinctrl_dev *pctldev;
325 struct pinctrl_gpio_range *range = NULL;
326 struct gpio_chip *chip = gpio_to_chip(gpio);
327
Tony Lindgren942cde72015-09-03 10:34:30 -0700328 if (WARN(!chip, "no gpio_chip for gpio%i?", gpio))
329 return false;
330
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200331 mutex_lock(&pinctrldev_list_mutex);
332
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800333 /* Loop over the pin controllers */
334 list_for_each_entry(pctldev, &pinctrldev_list, node) {
335 /* Loop over the ranges */
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800336 mutex_lock(&pctldev->mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800337 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
338 /* Check if any gpio range overlapped with gpio chip */
339 if (range->base + range->npins - 1 < chip->base ||
340 range->base > chip->base + chip->ngpio - 1)
341 continue;
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800342 mutex_unlock(&pctldev->mutex);
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200343 mutex_unlock(&pinctrldev_list_mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800344 return true;
345 }
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800346 mutex_unlock(&pctldev->mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800347 }
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200348
349 mutex_unlock(&pinctrldev_list_mutex);
350
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800351 return false;
352}
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800353#else
354static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
355#endif
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800356
357/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200358 * pinctrl_get_device_gpio_range() - find device for GPIO range
359 * @gpio: the pin to locate the pin controller for
360 * @outdev: the pin control device if found
361 * @outrange: the GPIO range if found
362 *
363 * Find the pin controller handling a certain GPIO pin from the pinspace of
364 * the GPIO subsystem, return the device and the matching GPIO range. Returns
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800365 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
366 * may still have not been registered.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200367 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700368static int pinctrl_get_device_gpio_range(unsigned gpio,
369 struct pinctrl_dev **outdev,
370 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200371{
Masahiro Yamada6cadafb2019-06-09 23:55:37 +0900372 struct pinctrl_dev *pctldev;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200373
Axel Linf0059022013-08-18 20:34:22 +0800374 mutex_lock(&pinctrldev_list_mutex);
375
Linus Walleij2744e8a2011-05-02 20:50:54 +0200376 /* Loop over the pin controllers */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200377 list_for_each_entry(pctldev, &pinctrldev_list, node) {
378 struct pinctrl_gpio_range *range;
379
380 range = pinctrl_match_gpio_range(pctldev, gpio);
Markus Elfringcea234e2017-05-02 11:04:55 +0200381 if (range) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200382 *outdev = pctldev;
383 *outrange = range;
Axel Linf0059022013-08-18 20:34:22 +0800384 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200385 return 0;
386 }
387 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200388
Axel Linf0059022013-08-18 20:34:22 +0800389 mutex_unlock(&pinctrldev_list_mutex);
390
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800391 return -EPROBE_DEFER;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200392}
393
394/**
395 * pinctrl_add_gpio_range() - register a GPIO range for a controller
396 * @pctldev: pin controller device to add the range to
397 * @range: the GPIO range to add
398 *
399 * This adds a range of GPIOs to be handled by a certain pin controller. Call
400 * this to register handled ranges after registering your pin controller.
401 */
402void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
403 struct pinctrl_gpio_range *range)
404{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200405 mutex_lock(&pctldev->mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700406 list_add_tail(&range->node, &pctldev->gpio_ranges);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200407 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200408}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700409EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200410
Dong Aisheng3e5e00b2012-05-23 21:22:41 +0800411void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
412 struct pinctrl_gpio_range *ranges,
413 unsigned nranges)
414{
415 int i;
416
417 for (i = 0; i < nranges; i++)
418 pinctrl_add_gpio_range(pctldev, &ranges[i]);
419}
420EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
421
Linus Walleij192c3692012-11-20 14:03:37 +0100422struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530423 struct pinctrl_gpio_range *range)
424{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200425 struct pinctrl_dev *pctldev;
426
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200427 pctldev = get_pinctrl_dev_from_devname(devname);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530428
Linus Walleijdfa97512012-11-20 14:54:18 +0100429 /*
430 * If we can't find this device, let's assume that is because
431 * it has not probed yet, so the driver trying to register this
432 * range need to defer probing.
433 */
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200434 if (!pctldev) {
Linus Walleijdfa97512012-11-20 14:54:18 +0100435 return ERR_PTR(-EPROBE_DEFER);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200436 }
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530437 pinctrl_add_gpio_range(pctldev, range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200438
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530439 return pctldev;
440}
Linus Walleij192c3692012-11-20 14:03:37 +0100441EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530442
Christian Ruppert586a87e2013-10-15 15:37:54 +0200443int pinctrl_get_group_pins(struct pinctrl_dev *pctldev, const char *pin_group,
444 const unsigned **pins, unsigned *num_pins)
445{
446 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
447 int gs;
448
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +0200449 if (!pctlops->get_group_pins)
450 return -EINVAL;
451
Christian Ruppert586a87e2013-10-15 15:37:54 +0200452 gs = pinctrl_get_group_selector(pctldev, pin_group);
453 if (gs < 0)
454 return gs;
455
456 return pctlops->get_group_pins(pctldev, gs, pins, num_pins);
457}
458EXPORT_SYMBOL_GPL(pinctrl_get_group_pins);
459
Joachim Eastwoodb18537c2016-02-25 22:44:37 +0100460struct pinctrl_gpio_range *
461pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev *pctldev,
462 unsigned int pin)
463{
464 struct pinctrl_gpio_range *range;
465
466 /* Loop over the ranges */
467 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
468 /* Check if we're in the valid range */
469 if (range->pins) {
470 int a;
471 for (a = 0; a < range->npins; a++) {
472 if (range->pins[a] == pin)
473 return range;
474 }
475 } else if (pin >= range->pin_base &&
476 pin < range->pin_base + range->npins)
477 return range;
478 }
479
480 return NULL;
481}
482EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin_nolock);
483
Linus Walleij2744e8a2011-05-02 20:50:54 +0200484/**
Linus Walleij9afbefb2012-11-20 14:25:07 +0100485 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
486 * @pctldev: the pin controller device to look in
487 * @pin: a controller-local number to find the range for
488 */
489struct pinctrl_gpio_range *
490pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
491 unsigned int pin)
492{
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800493 struct pinctrl_gpio_range *range;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100494
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200495 mutex_lock(&pctldev->mutex);
Joachim Eastwoodb18537c2016-02-25 22:44:37 +0100496 range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200497 mutex_unlock(&pctldev->mutex);
Joachim Eastwoodb18537c2016-02-25 22:44:37 +0100498
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800499 return range;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100500}
501EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
502
503/**
Charles Keepax50842cb2017-02-13 10:11:03 +0000504 * pinctrl_remove_gpio_range() - remove a range of GPIOs from a pin controller
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530505 * @pctldev: pin controller device to remove the range from
506 * @range: the GPIO range to remove
507 */
508void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
509 struct pinctrl_gpio_range *range)
510{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200511 mutex_lock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530512 list_del(&range->node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200513 mutex_unlock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530514}
515EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
516
Linus Walleijc033a712016-12-30 15:04:43 +0100517#ifdef CONFIG_GENERIC_PINCTRL_GROUPS
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800518
519/**
520 * pinctrl_generic_get_group_count() - returns the number of pin groups
521 * @pctldev: pin controller device
522 */
523int pinctrl_generic_get_group_count(struct pinctrl_dev *pctldev)
524{
525 return pctldev->num_groups;
526}
527EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_count);
528
529/**
530 * pinctrl_generic_get_group_name() - returns the name of a pin group
531 * @pctldev: pin controller device
532 * @selector: group number
533 */
534const char *pinctrl_generic_get_group_name(struct pinctrl_dev *pctldev,
535 unsigned int selector)
536{
537 struct group_desc *group;
538
539 group = radix_tree_lookup(&pctldev->pin_group_tree,
540 selector);
541 if (!group)
542 return NULL;
543
544 return group->name;
545}
546EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_name);
547
548/**
549 * pinctrl_generic_get_group_pins() - gets the pin group pins
550 * @pctldev: pin controller device
551 * @selector: group number
552 * @pins: pins in the group
553 * @num_pins: number of pins in the group
554 */
555int pinctrl_generic_get_group_pins(struct pinctrl_dev *pctldev,
556 unsigned int selector,
557 const unsigned int **pins,
558 unsigned int *num_pins)
559{
560 struct group_desc *group;
561
562 group = radix_tree_lookup(&pctldev->pin_group_tree,
563 selector);
564 if (!group) {
565 dev_err(pctldev->dev, "%s could not find pingroup%i\n",
566 __func__, selector);
567 return -EINVAL;
568 }
569
570 *pins = group->pins;
571 *num_pins = group->num_pins;
572
573 return 0;
574}
575EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_pins);
576
577/**
578 * pinctrl_generic_get_group() - returns a pin group based on the number
579 * @pctldev: pin controller device
580 * @gselector: group number
581 */
582struct group_desc *pinctrl_generic_get_group(struct pinctrl_dev *pctldev,
583 unsigned int selector)
584{
585 struct group_desc *group;
586
587 group = radix_tree_lookup(&pctldev->pin_group_tree,
588 selector);
589 if (!group)
590 return NULL;
591
592 return group;
593}
594EXPORT_SYMBOL_GPL(pinctrl_generic_get_group);
595
Tony Lindgrena2037282018-07-05 02:10:14 -0700596static int pinctrl_generic_group_name_to_selector(struct pinctrl_dev *pctldev,
597 const char *function)
598{
599 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
600 int ngroups = ops->get_groups_count(pctldev);
601 int selector = 0;
602
603 /* See if this pctldev has this group */
604 while (selector < ngroups) {
605 const char *gname = ops->get_group_name(pctldev, selector);
606
Yanjiang Jin54a58182018-09-29 17:06:55 +0800607 if (gname && !strcmp(function, gname))
Tony Lindgrena2037282018-07-05 02:10:14 -0700608 return selector;
609
610 selector++;
611 }
612
613 return -EINVAL;
614}
615
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800616/**
617 * pinctrl_generic_add_group() - adds a new pin group
618 * @pctldev: pin controller device
619 * @name: name of the pin group
620 * @pins: pins in the pin group
621 * @num_pins: number of pins in the pin group
622 * @data: pin controller driver specific data
623 *
624 * Note that the caller must take care of locking.
625 */
626int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name,
627 int *pins, int num_pins, void *data)
628{
629 struct group_desc *group;
Tony Lindgrena2037282018-07-05 02:10:14 -0700630 int selector;
631
632 if (!name)
633 return -EINVAL;
634
635 selector = pinctrl_generic_group_name_to_selector(pctldev, name);
636 if (selector >= 0)
637 return selector;
638
639 selector = pctldev->num_groups;
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800640
641 group = devm_kzalloc(pctldev->dev, sizeof(*group), GFP_KERNEL);
642 if (!group)
643 return -ENOMEM;
644
645 group->name = name;
646 group->pins = pins;
647 group->num_pins = num_pins;
648 group->data = data;
649
Tony Lindgrena2037282018-07-05 02:10:14 -0700650 radix_tree_insert(&pctldev->pin_group_tree, selector, group);
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800651
652 pctldev->num_groups++;
653
Tony Lindgrena2037282018-07-05 02:10:14 -0700654 return selector;
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800655}
656EXPORT_SYMBOL_GPL(pinctrl_generic_add_group);
657
658/**
659 * pinctrl_generic_remove_group() - removes a numbered pin group
660 * @pctldev: pin controller device
661 * @selector: group number
662 *
663 * Note that the caller must take care of locking.
664 */
665int pinctrl_generic_remove_group(struct pinctrl_dev *pctldev,
666 unsigned int selector)
667{
668 struct group_desc *group;
669
670 group = radix_tree_lookup(&pctldev->pin_group_tree,
671 selector);
672 if (!group)
673 return -ENOENT;
674
675 radix_tree_delete(&pctldev->pin_group_tree, selector);
676 devm_kfree(pctldev->dev, group);
677
678 pctldev->num_groups--;
679
680 return 0;
681}
682EXPORT_SYMBOL_GPL(pinctrl_generic_remove_group);
683
684/**
685 * pinctrl_generic_free_groups() - removes all pin groups
686 * @pctldev: pin controller device
687 *
Tony Lindgren664b7c42017-05-12 08:47:57 -0700688 * Note that the caller must take care of locking. The pinctrl groups
689 * are allocated with devm_kzalloc() so no need to free them here.
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800690 */
691static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
692{
693 struct radix_tree_iter iter;
Masahiro Yamada906a2a32017-08-04 13:52:05 +0900694 void __rcu **slot;
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800695
696 radix_tree_for_each_slot(slot, &pctldev->pin_group_tree, &iter, 0)
Tony Lindgren664b7c42017-05-12 08:47:57 -0700697 radix_tree_delete(&pctldev->pin_group_tree, iter.index);
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800698
699 pctldev->num_groups = 0;
700}
701
702#else
703static inline void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
704{
705}
Linus Walleijc033a712016-12-30 15:04:43 +0100706#endif /* CONFIG_GENERIC_PINCTRL_GROUPS */
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800707
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530708/**
Linus Walleij7afde8b2011-10-19 17:07:16 +0200709 * pinctrl_get_group_selector() - returns the group selector for a group
710 * @pctldev: the pin controller handling the group
711 * @pin_group: the pin group to look up
712 */
713int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
714 const char *pin_group)
715{
716 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530717 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200718 unsigned group_selector = 0;
719
Viresh Kumard1e90e92012-03-30 11:25:40 +0530720 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200721 const char *gname = pctlops->get_group_name(pctldev,
722 group_selector);
Yanjiang Jin54a58182018-09-29 17:06:55 +0800723 if (gname && !strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700724 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200725 "found group selector %u for %s\n",
726 group_selector,
727 pin_group);
728 return group_selector;
729 }
730
731 group_selector++;
732 }
733
Stephen Warren51cd24e2011-12-09 16:59:05 -0700734 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200735 pin_group);
736
737 return -EINVAL;
738}
739
Stefan Wahren472a61e2019-08-14 14:00:35 +0300740bool pinctrl_gpio_can_use_line(unsigned gpio)
741{
742 struct pinctrl_dev *pctldev;
743 struct pinctrl_gpio_range *range;
744 bool result;
745 int pin;
746
747 /*
748 * Try to obtain GPIO range, if it fails
749 * we're probably dealing with GPIO driver
750 * without a backing pin controller - bail out.
751 */
752 if (pinctrl_get_device_gpio_range(gpio, &pctldev, &range))
753 return true;
754
755 mutex_lock(&pctldev->mutex);
756
757 /* Convert to the pin controllers number space */
758 pin = gpio_to_pin(range, gpio);
759
760 result = pinmux_can_be_used_for_gpio(pctldev, pin);
761
762 mutex_unlock(&pctldev->mutex);
763
764 return result;
765}
766EXPORT_SYMBOL_GPL(pinctrl_gpio_can_use_line);
767
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100768/**
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200769 * pinctrl_gpio_request() - request a single pin to be used as GPIO
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100770 * @gpio: the GPIO pin number from the GPIO subsystem number space
771 *
772 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
773 * as part of their gpio_request() semantics, platforms and individual drivers
774 * shall *NOT* request GPIO pins to be muxed in.
775 */
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200776int pinctrl_gpio_request(unsigned gpio)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100777{
778 struct pinctrl_dev *pctldev;
779 struct pinctrl_gpio_range *range;
780 int ret;
781 int pin;
782
783 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700784 if (ret) {
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800785 if (pinctrl_ready_for_gpio_range(gpio))
786 ret = 0;
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800787 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700788 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100789
Axel Lin9b77ace42013-08-19 10:07:46 +0800790 mutex_lock(&pctldev->mutex);
791
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100792 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200793 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100794
Stephen Warren57b676f2012-03-02 13:05:44 -0700795 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
796
Axel Lin9b77ace42013-08-19 10:07:46 +0800797 mutex_unlock(&pctldev->mutex);
798
Stephen Warren57b676f2012-03-02 13:05:44 -0700799 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100800}
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200801EXPORT_SYMBOL_GPL(pinctrl_gpio_request);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100802
803/**
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200804 * pinctrl_gpio_free() - free control on a single pin, currently used as GPIO
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100805 * @gpio: the GPIO pin number from the GPIO subsystem number space
806 *
807 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
808 * as part of their gpio_free() semantics, platforms and individual drivers
809 * shall *NOT* request GPIO pins to be muxed out.
810 */
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200811void pinctrl_gpio_free(unsigned gpio)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100812{
813 struct pinctrl_dev *pctldev;
814 struct pinctrl_gpio_range *range;
815 int ret;
816 int pin;
817
818 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700819 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100820 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700821 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200822 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100823
824 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200825 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100826
Stephen Warren57b676f2012-03-02 13:05:44 -0700827 pinmux_free_gpio(pctldev, pin, range);
828
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200829 mutex_unlock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100830}
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200831EXPORT_SYMBOL_GPL(pinctrl_gpio_free);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100832
833static int pinctrl_gpio_direction(unsigned gpio, bool input)
834{
835 struct pinctrl_dev *pctldev;
836 struct pinctrl_gpio_range *range;
837 int ret;
838 int pin;
839
840 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200841 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100842 return ret;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200843 }
844
845 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100846
847 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200848 pin = gpio_to_pin(range, gpio);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200849 ret = pinmux_gpio_direction(pctldev, range, pin, input);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100850
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200851 mutex_unlock(&pctldev->mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200852
853 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100854}
855
856/**
857 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
858 * @gpio: the GPIO pin number from the GPIO subsystem number space
859 *
860 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
861 * as part of their gpio_direction_input() semantics, platforms and individual
862 * drivers shall *NOT* touch pin control GPIO calls.
863 */
864int pinctrl_gpio_direction_input(unsigned gpio)
865{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200866 return pinctrl_gpio_direction(gpio, true);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100867}
868EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
869
870/**
871 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
872 * @gpio: the GPIO pin number from the GPIO subsystem number space
873 *
874 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
875 * as part of their gpio_direction_output() semantics, platforms and individual
876 * drivers shall *NOT* touch pin control GPIO calls.
877 */
878int pinctrl_gpio_direction_output(unsigned gpio)
879{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200880 return pinctrl_gpio_direction(gpio, false);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100881}
882EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
883
Mika Westerberg15381bc2017-01-23 15:34:33 +0300884/**
885 * pinctrl_gpio_set_config() - Apply config to given GPIO pin
886 * @gpio: the GPIO pin number from the GPIO subsystem number space
887 * @config: the configuration to apply to the GPIO
888 *
889 * This function should *ONLY* be used from gpiolib-based GPIO drivers, if
890 * they need to call the underlying pin controller to change GPIO config
891 * (for example set debounce time).
892 */
893int pinctrl_gpio_set_config(unsigned gpio, unsigned long config)
894{
895 unsigned long configs[] = { config };
896 struct pinctrl_gpio_range *range;
897 struct pinctrl_dev *pctldev;
898 int ret, pin;
899
900 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
901 if (ret)
902 return ret;
903
904 mutex_lock(&pctldev->mutex);
905 pin = gpio_to_pin(range, gpio);
906 ret = pinconf_set_config(pctldev, pin, configs, ARRAY_SIZE(configs));
907 mutex_unlock(&pctldev->mutex);
908
909 return ret;
910}
911EXPORT_SYMBOL_GPL(pinctrl_gpio_set_config);
912
Stephen Warren6e5e9592012-03-02 13:05:47 -0700913static struct pinctrl_state *find_state(struct pinctrl *p,
914 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100915{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700916 struct pinctrl_state *state;
917
918 list_for_each_entry(state, &p->states, node)
919 if (!strcmp(state->name, name))
920 return state;
921
922 return NULL;
923}
924
925static struct pinctrl_state *create_state(struct pinctrl *p,
926 const char *name)
927{
928 struct pinctrl_state *state;
929
930 state = kzalloc(sizeof(*state), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -0800931 if (!state)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700932 return ERR_PTR(-ENOMEM);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700933
934 state->name = name;
935 INIT_LIST_HEAD(&state->settings);
936
937 list_add_tail(&state->node, &p->states);
938
939 return state;
940}
941
Tony Lindgren99e4f672016-12-27 09:19:59 -0800942static int add_setting(struct pinctrl *p, struct pinctrl_dev *pctldev,
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900943 const struct pinctrl_map *map)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700944{
945 struct pinctrl_state *state;
946 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700947 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700948
949 state = find_state(p, map->name);
950 if (!state)
951 state = create_state(p, map->name);
952 if (IS_ERR(state))
953 return PTR_ERR(state);
954
Stephen Warren1e2082b2012-03-02 13:05:48 -0700955 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
956 return 0;
957
Stephen Warren6e5e9592012-03-02 13:05:47 -0700958 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -0800959 if (!setting)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700960 return -ENOMEM;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700961
Stephen Warren1e2082b2012-03-02 13:05:48 -0700962 setting->type = map->type;
963
Tony Lindgren99e4f672016-12-27 09:19:59 -0800964 if (pctldev)
965 setting->pctldev = pctldev;
966 else
967 setting->pctldev =
968 get_pinctrl_dev_from_devname(map->ctrl_dev_name);
Markus Elfringcea234e2017-05-02 11:04:55 +0200969 if (!setting->pctldev) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700970 kfree(setting);
Linus Walleij89216492012-12-11 14:14:32 +0100971 /* Do not defer probing of hogs (circular loop) */
972 if (!strcmp(map->ctrl_dev_name, map->dev_name))
973 return -ENODEV;
Linus Walleijc05127c2012-04-10 10:00:38 +0200974 /*
975 * OK let us guess that the driver is not there yet, and
976 * let's defer obtaining this pinctrl handle to later...
977 */
Linus Walleij89216492012-12-11 14:14:32 +0100978 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
979 map->ctrl_dev_name);
Linus Walleijc05127c2012-04-10 10:00:38 +0200980 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700981 }
982
Linus Walleij1a789582012-10-17 20:51:54 +0200983 setting->dev_name = map->dev_name;
984
Stephen Warren1e2082b2012-03-02 13:05:48 -0700985 switch (map->type) {
986 case PIN_MAP_TYPE_MUX_GROUP:
987 ret = pinmux_map_to_setting(map, setting);
988 break;
989 case PIN_MAP_TYPE_CONFIGS_PIN:
990 case PIN_MAP_TYPE_CONFIGS_GROUP:
991 ret = pinconf_map_to_setting(map, setting);
992 break;
993 default:
994 ret = -EINVAL;
995 break;
996 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700997 if (ret < 0) {
998 kfree(setting);
999 return ret;
1000 }
1001
1002 list_add_tail(&setting->node, &state->settings);
1003
1004 return 0;
1005}
1006
1007static struct pinctrl *find_pinctrl(struct device *dev)
1008{
1009 struct pinctrl *p;
1010
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001011 mutex_lock(&pinctrl_list_mutex);
Stephen Warren1e2082b2012-03-02 13:05:48 -07001012 list_for_each_entry(p, &pinctrl_list, node)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001013 if (p->dev == dev) {
1014 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001015 return p;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001016 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001017
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001018 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001019 return NULL;
1020}
1021
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001022static void pinctrl_free(struct pinctrl *p, bool inlist);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001023
Tony Lindgren99e4f672016-12-27 09:19:59 -08001024static struct pinctrl *create_pinctrl(struct device *dev,
1025 struct pinctrl_dev *pctldev)
Stephen Warren6e5e9592012-03-02 13:05:47 -07001026{
1027 struct pinctrl *p;
1028 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001029 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001030 int i;
Masahiro Yamada3f713b72017-08-04 11:22:31 +09001031 const struct pinctrl_map *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001032 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001033
1034 /*
1035 * create the state cookie holder struct pinctrl for each
1036 * mapping, this is what consumers will get when requesting
1037 * a pin control handle with pinctrl_get()
1038 */
Stephen Warren02f5b982012-02-22 14:26:00 -07001039 p = kzalloc(sizeof(*p), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -08001040 if (!p)
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001041 return ERR_PTR(-ENOMEM);
Stephen Warren7ecdb162012-03-02 13:05:45 -07001042 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001043 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -06001044 INIT_LIST_HEAD(&p->dt_maps);
1045
Tony Lindgren99e4f672016-12-27 09:19:59 -08001046 ret = pinctrl_dt_to_map(p, pctldev);
Stephen Warren57291ce2012-03-23 10:29:46 -06001047 if (ret < 0) {
1048 kfree(p);
1049 return ERR_PTR(ret);
1050 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001051
1052 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001053
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001054 mutex_lock(&pinctrl_maps_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001055 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001056 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -07001057 /* Map must be for this device */
1058 if (strcmp(map->dev_name, devname))
1059 continue;
Nikita Yushchenko7f0ff062017-05-11 23:02:11 +03001060 /*
1061 * If pctldev is not null, we are claiming hog for it,
1062 * that means, setting that is served by pctldev by itself.
1063 *
1064 * Thus we must skip map that is for this device but is served
1065 * by other device.
1066 */
1067 if (pctldev &&
1068 strcmp(dev_name(pctldev->dev), map->ctrl_dev_name))
1069 continue;
Stephen Warren1681f5a2012-02-22 14:25:58 -07001070
Tony Lindgren99e4f672016-12-27 09:19:59 -08001071 ret = add_setting(p, pctldev, map);
Linus Walleij89216492012-12-11 14:14:32 +01001072 /*
1073 * At this point the adding of a setting may:
1074 *
1075 * - Defer, if the pinctrl device is not yet available
1076 * - Fail, if the pinctrl device is not yet available,
1077 * AND the setting is a hog. We cannot defer that, since
1078 * the hog will kick in immediately after the device
1079 * is registered.
1080 *
1081 * If the error returned was not -EPROBE_DEFER then we
1082 * accumulate the errors to see if we end up with
1083 * an -EPROBE_DEFER later, as that is the worst case.
1084 */
1085 if (ret == -EPROBE_DEFER) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001086 pinctrl_free(p, false);
1087 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001088 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001089 }
1090 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001091 mutex_unlock(&pinctrl_maps_mutex);
1092
Linus Walleij89216492012-12-11 14:14:32 +01001093 if (ret < 0) {
Andy Shevchenko3ec440e2017-02-28 16:59:56 +02001094 /* If some other error than deferral occurred, return here */
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001095 pinctrl_free(p, false);
Linus Walleij89216492012-12-11 14:14:32 +01001096 return ERR_PTR(ret);
1097 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001098
Linus Walleijab780292013-01-22 10:56:14 -07001099 kref_init(&p->users);
1100
Linus Walleijb0666ba2012-12-11 13:12:35 +01001101 /* Add the pinctrl handle to the global list */
Stanislaw Gruszka7b320cb2014-02-04 09:07:09 +01001102 mutex_lock(&pinctrl_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -07001103 list_add_tail(&p->node, &pinctrl_list);
Stanislaw Gruszka7b320cb2014-02-04 09:07:09 +01001104 mutex_unlock(&pinctrl_list_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001105
1106 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001107}
Stephen Warren7ecdb162012-03-02 13:05:45 -07001108
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001109/**
1110 * pinctrl_get() - retrieves the pinctrl handle for a device
1111 * @dev: the device to obtain the handle for
1112 */
1113struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warren6e5e9592012-03-02 13:05:47 -07001114{
1115 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001116
Stephen Warren6e5e9592012-03-02 13:05:47 -07001117 if (WARN_ON(!dev))
1118 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -07001119
Linus Walleijab780292013-01-22 10:56:14 -07001120 /*
1121 * See if somebody else (such as the device core) has already
1122 * obtained a handle to the pinctrl for this device. In that case,
1123 * return another pointer to it.
1124 */
Stephen Warren6e5e9592012-03-02 13:05:47 -07001125 p = find_pinctrl(dev);
Markus Elfringcea234e2017-05-02 11:04:55 +02001126 if (p) {
Linus Walleijab780292013-01-22 10:56:14 -07001127 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
1128 kref_get(&p->users);
1129 return p;
1130 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001131
Tony Lindgren99e4f672016-12-27 09:19:59 -08001132 return create_pinctrl(dev, NULL);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001133}
1134EXPORT_SYMBOL_GPL(pinctrl_get);
1135
Richard Genoudd3cee8302013-03-25 15:47:21 +01001136static void pinctrl_free_setting(bool disable_setting,
1137 struct pinctrl_setting *setting)
1138{
1139 switch (setting->type) {
1140 case PIN_MAP_TYPE_MUX_GROUP:
1141 if (disable_setting)
1142 pinmux_disable_setting(setting);
1143 pinmux_free_setting(setting);
1144 break;
1145 case PIN_MAP_TYPE_CONFIGS_PIN:
1146 case PIN_MAP_TYPE_CONFIGS_GROUP:
1147 pinconf_free_setting(setting);
1148 break;
1149 default:
1150 break;
1151 }
1152}
1153
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001154static void pinctrl_free(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -07001155{
Stephen Warren6e5e9592012-03-02 13:05:47 -07001156 struct pinctrl_state *state, *n1;
1157 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001158
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001159 mutex_lock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001160 list_for_each_entry_safe(state, n1, &p->states, node) {
1161 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Richard Genoudd3cee8302013-03-25 15:47:21 +01001162 pinctrl_free_setting(state == p->state, setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001163 list_del(&setting->node);
1164 kfree(setting);
1165 }
1166 list_del(&state->node);
1167 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -07001168 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001169
Stephen Warren57291ce2012-03-23 10:29:46 -06001170 pinctrl_dt_free_maps(p);
1171
Stephen Warren6e5e9592012-03-02 13:05:47 -07001172 if (inlist)
1173 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -07001174 kfree(p);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001175 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001176}
1177
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001178/**
Linus Walleijab780292013-01-22 10:56:14 -07001179 * pinctrl_release() - release the pinctrl handle
1180 * @kref: the kref in the pinctrl being released
1181 */
Sachin Kamat2917e832013-01-24 15:01:00 +05301182static void pinctrl_release(struct kref *kref)
Linus Walleijab780292013-01-22 10:56:14 -07001183{
1184 struct pinctrl *p = container_of(kref, struct pinctrl, users);
1185
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001186 pinctrl_free(p, true);
Linus Walleijab780292013-01-22 10:56:14 -07001187}
1188
1189/**
1190 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
Stephen Warren6e5e9592012-03-02 13:05:47 -07001191 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001192 */
1193void pinctrl_put(struct pinctrl *p)
1194{
Linus Walleijab780292013-01-22 10:56:14 -07001195 kref_put(&p->users, pinctrl_release);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001196}
1197EXPORT_SYMBOL_GPL(pinctrl_put);
1198
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001199/**
1200 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
1201 * @p: the pinctrl handle to retrieve the state from
1202 * @name: the state name to retrieve
1203 */
1204struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
1205 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -07001206{
Stephen Warren6e5e9592012-03-02 13:05:47 -07001207 struct pinctrl_state *state;
1208
1209 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +08001210 if (!state) {
1211 if (pinctrl_dummy_state) {
1212 /* create dummy state */
1213 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
1214 name);
1215 state = create_state(p, name);
Richard Genoudd599bfb2012-08-10 16:53:49 +02001216 } else
1217 state = ERR_PTR(-ENODEV);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +08001218 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001219
1220 return state;
1221}
Stephen Warren6e5e9592012-03-02 13:05:47 -07001222EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
1223
Benjamin Gaignard036f3942019-05-22 17:29:24 +02001224static void pinctrl_link_add(struct pinctrl_dev *pctldev,
1225 struct device *consumer)
1226{
1227 if (pctldev->desc->link_consumers)
1228 device_link_add(consumer, pctldev->dev,
1229 DL_FLAG_PM_RUNTIME |
1230 DL_FLAG_AUTOREMOVE_CONSUMER);
1231}
1232
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001233/**
Florian Fainelli981ed1b2017-03-01 10:32:57 -08001234 * pinctrl_commit_state() - select/activate/program a pinctrl state to HW
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001235 * @p: the pinctrl handle for the device that requests configuration
1236 * @state: the state handle to select/activate/program
1237 */
Florian Fainelli981ed1b2017-03-01 10:32:57 -08001238static int pinctrl_commit_state(struct pinctrl *p, struct pinctrl_state *state)
Stephen Warren6e5e9592012-03-02 13:05:47 -07001239{
1240 struct pinctrl_setting *setting, *setting2;
Richard Genoud50cf7c82013-03-25 15:47:23 +01001241 struct pinctrl_state *old_state = p->state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001242 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -07001243
Stephen Warren6e5e9592012-03-02 13:05:47 -07001244 if (p->state) {
1245 /*
Fan Wu2243a872014-06-09 09:37:56 +08001246 * For each pinmux setting in the old state, forget SW's record
1247 * of mux owner for that pingroup. Any pingroups which are
1248 * still owned by the new state will be re-acquired by the call
1249 * to pinmux_enable_setting() in the loop below.
Stephen Warren6e5e9592012-03-02 13:05:47 -07001250 */
1251 list_for_each_entry(setting, &p->state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001252 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
1253 continue;
Fan Wu2243a872014-06-09 09:37:56 +08001254 pinmux_disable_setting(setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001255 }
1256 }
1257
Richard Genoud3102a762013-03-25 15:47:22 +01001258 p->state = NULL;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001259
1260 /* Apply all the settings for the new state */
1261 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001262 switch (setting->type) {
1263 case PIN_MAP_TYPE_MUX_GROUP:
1264 ret = pinmux_enable_setting(setting);
1265 break;
1266 case PIN_MAP_TYPE_CONFIGS_PIN:
1267 case PIN_MAP_TYPE_CONFIGS_GROUP:
1268 ret = pinconf_apply_setting(setting);
1269 break;
1270 default:
1271 ret = -EINVAL;
1272 break;
1273 }
Richard Genoud3102a762013-03-25 15:47:22 +01001274
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001275 if (ret < 0) {
Richard Genoud3102a762013-03-25 15:47:22 +01001276 goto unapply_new_state;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001277 }
Benjamin Gaignard036f3942019-05-22 17:29:24 +02001278
Linus Walleijb672a872019-05-24 00:11:43 +02001279 /* Do not link hogs (circular dependency) */
1280 if (p != setting->pctldev->p)
1281 pinctrl_link_add(setting->pctldev, p->dev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001282 }
1283
Richard Genoud3102a762013-03-25 15:47:22 +01001284 p->state = state;
1285
Stephen Warren7ecdb162012-03-02 13:05:45 -07001286 return 0;
Richard Genoud3102a762013-03-25 15:47:22 +01001287
1288unapply_new_state:
Richard Genoudda587512013-03-28 12:55:46 +01001289 dev_err(p->dev, "Error applying setting, reverse things back\n");
Richard Genoud3102a762013-03-25 15:47:22 +01001290
Richard Genoud3102a762013-03-25 15:47:22 +01001291 list_for_each_entry(setting2, &state->settings, node) {
1292 if (&setting2->node == &setting->node)
1293 break;
Richard Genoudaf606172013-03-29 10:03:26 +01001294 /*
1295 * All we can do here is pinmux_disable_setting.
1296 * That means that some pins are muxed differently now
1297 * than they were before applying the setting (We can't
1298 * "unmux a pin"!), but it's not a big deal since the pins
1299 * are free to be muxed by another apply_setting.
1300 */
1301 if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
1302 pinmux_disable_setting(setting2);
Richard Genoud3102a762013-03-25 15:47:22 +01001303 }
Richard Genoud8009d5f2013-03-28 12:55:47 +01001304
Richard Genoud385d9422013-03-29 10:03:27 +01001305 /* There's no infinite recursive loop here because p->state is NULL */
1306 if (old_state)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001307 pinctrl_select_state(p, old_state);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001308
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001309 return ret;
1310}
Florian Fainelli981ed1b2017-03-01 10:32:57 -08001311
1312/**
1313 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
1314 * @p: the pinctrl handle for the device that requests configuration
1315 * @state: the state handle to select/activate/program
1316 */
1317int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
1318{
1319 if (p->state == state)
1320 return 0;
1321
1322 return pinctrl_commit_state(p, state);
1323}
Stephen Warren6e5e9592012-03-02 13:05:47 -07001324EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001325
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001326static void devm_pinctrl_release(struct device *dev, void *res)
1327{
1328 pinctrl_put(*(struct pinctrl **)res);
1329}
1330
1331/**
1332 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
1333 * @dev: the device to obtain the handle for
1334 *
1335 * If there is a need to explicitly destroy the returned struct pinctrl,
1336 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1337 */
1338struct pinctrl *devm_pinctrl_get(struct device *dev)
1339{
1340 struct pinctrl **ptr, *p;
1341
1342 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
1343 if (!ptr)
1344 return ERR_PTR(-ENOMEM);
1345
1346 p = pinctrl_get(dev);
1347 if (!IS_ERR(p)) {
1348 *ptr = p;
1349 devres_add(dev, ptr);
1350 } else {
1351 devres_free(ptr);
1352 }
1353
1354 return p;
1355}
1356EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1357
1358static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1359{
1360 struct pinctrl **p = res;
1361
1362 return *p == data;
1363}
1364
1365/**
1366 * devm_pinctrl_put() - Resource managed pinctrl_put()
1367 * @p: the pinctrl handle to release
1368 *
1369 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1370 * this function will not need to be called and the resource management
1371 * code will ensure that the resource is freed.
1372 */
1373void devm_pinctrl_put(struct pinctrl *p)
1374{
Jingoo Hana72149e2013-02-26 11:34:07 +09001375 WARN_ON(devres_release(p->dev, devm_pinctrl_release,
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001376 devm_pinctrl_match, p));
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001377}
1378EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1379
Hans de Goedec72bed22019-12-16 21:51:18 +01001380/**
1381 * pinctrl_register_mappings() - register a set of pin controller mappings
1382 * @maps: the pincontrol mappings table to register. Note the pinctrl-core
1383 * keeps a reference to the passed in maps, so they should _not_ be
1384 * marked with __initdata.
1385 * @num_maps: the number of maps in the mapping table
1386 */
1387int pinctrl_register_mappings(const struct pinctrl_map *maps,
1388 unsigned num_maps)
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001389{
Stephen Warren1e2082b2012-03-02 13:05:48 -07001390 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001391 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001392
Masahiro Yamada7e9236f2015-05-28 21:52:59 +09001393 pr_debug("add %u pinctrl maps\n", num_maps);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001394
1395 /* First sanity check the new mapping */
1396 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001397 if (!maps[i].dev_name) {
1398 pr_err("failed to register map %s (%d): no device given\n",
1399 maps[i].name, i);
1400 return -EINVAL;
1401 }
1402
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001403 if (!maps[i].name) {
1404 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001405 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001406 return -EINVAL;
1407 }
1408
Stephen Warren1e2082b2012-03-02 13:05:48 -07001409 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1410 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001411 pr_err("failed to register map %s (%d): no pin control device given\n",
1412 maps[i].name, i);
1413 return -EINVAL;
1414 }
1415
Stephen Warren1e2082b2012-03-02 13:05:48 -07001416 switch (maps[i].type) {
1417 case PIN_MAP_TYPE_DUMMY_STATE:
1418 break;
1419 case PIN_MAP_TYPE_MUX_GROUP:
1420 ret = pinmux_validate_map(&maps[i], i);
1421 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001422 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001423 break;
1424 case PIN_MAP_TYPE_CONFIGS_PIN:
1425 case PIN_MAP_TYPE_CONFIGS_GROUP:
1426 ret = pinconf_validate_map(&maps[i], i);
1427 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001428 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001429 break;
1430 default:
1431 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001432 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -07001433 return -EINVAL;
1434 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001435 }
1436
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001437 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -08001438 if (!maps_node)
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001439 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001440
Hans de Goedec72bed22019-12-16 21:51:18 +01001441 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001442 maps_node->num_maps = num_maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001443
Doug Andersonc5272a22015-05-01 09:01:27 -07001444 mutex_lock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001445 list_add_tail(&maps_node->node, &pinctrl_maps);
Doug Andersonc5272a22015-05-01 09:01:27 -07001446 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001447
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001448 return 0;
1449}
Richard Fitzgerald8b1b2dc2018-02-23 13:43:52 +00001450EXPORT_SYMBOL_GPL(pinctrl_register_mappings);
Stephen Warren57291ce2012-03-23 10:29:46 -06001451
Hans de Goedec72bed22019-12-16 21:51:18 +01001452/**
1453 * pinctrl_unregister_mappings() - unregister a set of pin controller mappings
1454 * @maps: the pincontrol mappings table passed to pinctrl_register_mappings()
1455 * when registering the mappings.
1456 */
1457void pinctrl_unregister_mappings(const struct pinctrl_map *map)
Stephen Warren57291ce2012-03-23 10:29:46 -06001458{
1459 struct pinctrl_maps *maps_node;
1460
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001461 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001462 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1463 if (maps_node->maps == map) {
1464 list_del(&maps_node->node);
Linus Walleijdb6c2c62013-07-23 01:43:20 +02001465 kfree(maps_node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001466 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001467 return;
1468 }
1469 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001470 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001471}
Hans de Goedec72bed22019-12-16 21:51:18 +01001472EXPORT_SYMBOL_GPL(pinctrl_unregister_mappings);
Stephen Warren57291ce2012-03-23 10:29:46 -06001473
Julien Delacou840a47b2012-12-10 14:47:33 +01001474/**
1475 * pinctrl_force_sleep() - turn a given controller device into sleep state
1476 * @pctldev: pin controller device
1477 */
1478int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1479{
1480 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
Florian Fainelli981ed1b2017-03-01 10:32:57 -08001481 return pinctrl_commit_state(pctldev->p, pctldev->hog_sleep);
Julien Delacou840a47b2012-12-10 14:47:33 +01001482 return 0;
1483}
1484EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1485
1486/**
1487 * pinctrl_force_default() - turn a given controller device into default state
1488 * @pctldev: pin controller device
1489 */
1490int pinctrl_force_default(struct pinctrl_dev *pctldev)
1491{
1492 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
Florian Fainelli981ed1b2017-03-01 10:32:57 -08001493 return pinctrl_commit_state(pctldev->p, pctldev->hog_default);
Julien Delacou840a47b2012-12-10 14:47:33 +01001494 return 0;
1495}
1496EXPORT_SYMBOL_GPL(pinctrl_force_default);
1497
Douglas Andersonef0eebc2015-10-20 21:15:06 -07001498/**
1499 * pinctrl_init_done() - tell pinctrl probe is done
1500 *
1501 * We'll use this time to switch the pins from "init" to "default" unless the
1502 * driver selected some other state.
1503 *
1504 * @dev: device to that's done probing
1505 */
1506int pinctrl_init_done(struct device *dev)
1507{
1508 struct dev_pin_info *pins = dev->pins;
1509 int ret;
1510
1511 if (!pins)
1512 return 0;
1513
1514 if (IS_ERR(pins->init_state))
1515 return 0; /* No such state */
1516
1517 if (pins->p->state != pins->init_state)
1518 return 0; /* Not at init anyway */
1519
1520 if (IS_ERR(pins->default_state))
1521 return 0; /* No default state */
1522
1523 ret = pinctrl_select_state(pins->p, pins->default_state);
1524 if (ret)
1525 dev_err(dev, "failed to activate default pinctrl state\n");
1526
1527 return ret;
1528}
1529
Ulf Hansson55d54d12019-12-06 18:08:13 +01001530static int pinctrl_select_bound_state(struct device *dev,
1531 struct pinctrl_state *state)
Tony Lindgrenf3333492013-07-18 08:15:04 -07001532{
1533 struct dev_pin_info *pins = dev->pins;
1534 int ret;
1535
1536 if (IS_ERR(state))
1537 return 0; /* No such state */
1538 ret = pinctrl_select_state(pins->p, state);
1539 if (ret)
1540 dev_err(dev, "failed to activate pinctrl state %s\n",
1541 state->name);
1542 return ret;
1543}
1544
1545/**
Ulf Hansson55d54d12019-12-06 18:08:13 +01001546 * pinctrl_select_default_state() - select default pinctrl state
1547 * @dev: device to select default state for
1548 */
1549int pinctrl_select_default_state(struct device *dev)
1550{
1551 if (!dev->pins)
1552 return 0;
1553
1554 return pinctrl_select_bound_state(dev, dev->pins->default_state);
1555}
1556EXPORT_SYMBOL_GPL(pinctrl_select_default_state);
1557
1558#ifdef CONFIG_PM
1559
1560/**
Linus Walleij14005ee2013-06-05 15:30:33 +02001561 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1562 * @dev: device to select default state for
1563 */
1564int pinctrl_pm_select_default_state(struct device *dev)
1565{
Ulf Hansson55d54d12019-12-06 18:08:13 +01001566 return pinctrl_select_default_state(dev);
Linus Walleij14005ee2013-06-05 15:30:33 +02001567}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001568EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001569
1570/**
1571 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1572 * @dev: device to select sleep state for
1573 */
1574int pinctrl_pm_select_sleep_state(struct device *dev)
1575{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001576 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001577 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001578
Ulf Hansson55d54d12019-12-06 18:08:13 +01001579 return pinctrl_select_bound_state(dev, dev->pins->sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001580}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001581EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001582
1583/**
1584 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1585 * @dev: device to select idle state for
1586 */
1587int pinctrl_pm_select_idle_state(struct device *dev)
1588{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001589 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001590 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001591
Ulf Hansson55d54d12019-12-06 18:08:13 +01001592 return pinctrl_select_bound_state(dev, dev->pins->idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001593}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001594EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001595#endif
1596
Linus Walleij2744e8a2011-05-02 20:50:54 +02001597#ifdef CONFIG_DEBUG_FS
1598
1599static int pinctrl_pins_show(struct seq_file *s, void *what)
1600{
1601 struct pinctrl_dev *pctldev = s->private;
1602 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001603 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001604
1605 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001606
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001607 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001608
Chanho Park706e8522012-01-03 16:47:50 +09001609 /* The pin number can be retrived from the pin controller descriptor */
1610 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001611 struct pin_desc *desc;
1612
Chanho Park706e8522012-01-03 16:47:50 +09001613 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001614 desc = pin_desc_get(pctldev, pin);
1615 /* Pin space may be sparse */
Markus Elfringcea234e2017-05-02 11:04:55 +02001616 if (!desc)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001617 continue;
1618
Masahiro Yamadacf9d9942016-05-24 14:26:26 +09001619 seq_printf(s, "pin %d (%s) ", pin, desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001620
1621 /* Driver-specific info per pin */
1622 if (ops->pin_dbg_show)
1623 ops->pin_dbg_show(pctldev, s, pin);
1624
1625 seq_puts(s, "\n");
1626 }
1627
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001628 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001629
Linus Walleij2744e8a2011-05-02 20:50:54 +02001630 return 0;
1631}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001632DEFINE_SHOW_ATTRIBUTE(pinctrl_pins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001633
1634static int pinctrl_groups_show(struct seq_file *s, void *what)
1635{
1636 struct pinctrl_dev *pctldev = s->private;
1637 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301638 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001639
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001640 mutex_lock(&pctldev->mutex);
1641
Viresh Kumard1e90e92012-03-30 11:25:40 +05301642 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001643
Linus Walleij2744e8a2011-05-02 20:50:54 +02001644 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301645 while (selector < ngroups) {
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001646 const unsigned *pins = NULL;
1647 unsigned num_pins = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001648 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001649 const char *pname;
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001650 int ret = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001651 int i;
1652
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001653 if (ops->get_group_pins)
1654 ret = ops->get_group_pins(pctldev, selector,
1655 &pins, &num_pins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001656 if (ret)
1657 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1658 gname);
1659 else {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001660 seq_printf(s, "group: %s\n", gname);
1661 for (i = 0; i < num_pins; i++) {
1662 pname = pin_get_name(pctldev, pins[i]);
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001663 if (WARN_ON(!pname)) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001664 mutex_unlock(&pctldev->mutex);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001665 return -EINVAL;
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001666 }
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001667 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1668 }
1669 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001670 }
1671 selector++;
1672 }
1673
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001674 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001675
1676 return 0;
1677}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001678DEFINE_SHOW_ATTRIBUTE(pinctrl_groups);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001679
1680static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1681{
1682 struct pinctrl_dev *pctldev = s->private;
Masahiro Yamada6cadafb2019-06-09 23:55:37 +09001683 struct pinctrl_gpio_range *range;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001684
1685 seq_puts(s, "GPIO ranges handled:\n");
1686
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001687 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001688
Linus Walleij2744e8a2011-05-02 20:50:54 +02001689 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001690 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Christian Ruppertc8587ee2013-06-13 14:55:31 +02001691 if (range->pins) {
1692 int a;
1693 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
1694 range->id, range->name,
1695 range->base, (range->base + range->npins - 1));
1696 for (a = 0; a < range->npins - 1; a++)
1697 seq_printf(s, "%u, ", range->pins[a]);
1698 seq_printf(s, "%u}\n", range->pins[a]);
1699 }
1700 else
1701 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1702 range->id, range->name,
1703 range->base, (range->base + range->npins - 1),
1704 range->pin_base,
1705 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001706 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001707
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001708 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001709
1710 return 0;
1711}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001712DEFINE_SHOW_ATTRIBUTE(pinctrl_gpioranges);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001713
1714static int pinctrl_devices_show(struct seq_file *s, void *what)
1715{
1716 struct pinctrl_dev *pctldev;
1717
Linus Walleijae6b4d82011-10-19 18:14:33 +02001718 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001719
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001720 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001721
Linus Walleij2744e8a2011-05-02 20:50:54 +02001722 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1723 seq_printf(s, "%s ", pctldev->desc->name);
1724 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001725 seq_puts(s, "yes ");
1726 else
1727 seq_puts(s, "no ");
1728 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001729 seq_puts(s, "yes");
1730 else
1731 seq_puts(s, "no");
1732 seq_puts(s, "\n");
1733 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001734
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001735 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001736
1737 return 0;
1738}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001739DEFINE_SHOW_ATTRIBUTE(pinctrl_devices);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001740
Stephen Warren1e2082b2012-03-02 13:05:48 -07001741static inline const char *map_type(enum pinctrl_map_type type)
1742{
1743 static const char * const names[] = {
1744 "INVALID",
1745 "DUMMY_STATE",
1746 "MUX_GROUP",
1747 "CONFIGS_PIN",
1748 "CONFIGS_GROUP",
1749 };
1750
1751 if (type >= ARRAY_SIZE(names))
1752 return "UNKNOWN";
1753
1754 return names[type];
1755}
1756
Stephen Warren3eedb432012-02-23 17:04:40 -07001757static int pinctrl_maps_show(struct seq_file *s, void *what)
1758{
1759 struct pinctrl_maps *maps_node;
1760 int i;
Masahiro Yamada3f713b72017-08-04 11:22:31 +09001761 const struct pinctrl_map *map;
Stephen Warren3eedb432012-02-23 17:04:40 -07001762
1763 seq_puts(s, "Pinctrl maps:\n");
1764
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001765 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001766 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001767 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1768 map->dev_name, map->name, map_type(map->type),
1769 map->type);
1770
1771 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1772 seq_printf(s, "controlling device %s\n",
1773 map->ctrl_dev_name);
1774
1775 switch (map->type) {
1776 case PIN_MAP_TYPE_MUX_GROUP:
1777 pinmux_show_map(s, map);
1778 break;
1779 case PIN_MAP_TYPE_CONFIGS_PIN:
1780 case PIN_MAP_TYPE_CONFIGS_GROUP:
1781 pinconf_show_map(s, map);
1782 break;
1783 default:
1784 break;
1785 }
1786
Markus Elfring390e1042017-05-02 10:47:35 +02001787 seq_putc(s, '\n');
Stephen Warren3eedb432012-02-23 17:04:40 -07001788 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001789 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001790
1791 return 0;
1792}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001793DEFINE_SHOW_ATTRIBUTE(pinctrl_maps);
Stephen Warren3eedb432012-02-23 17:04:40 -07001794
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001795static int pinctrl_show(struct seq_file *s, void *what)
1796{
1797 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001798 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001799 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001800
1801 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001802
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001803 mutex_lock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001804
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001805 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001806 seq_printf(s, "device: %s current state: %s\n",
1807 dev_name(p->dev),
1808 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001809
Stephen Warren6e5e9592012-03-02 13:05:47 -07001810 list_for_each_entry(state, &p->states, node) {
1811 seq_printf(s, " state: %s\n", state->name);
1812
1813 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001814 struct pinctrl_dev *pctldev = setting->pctldev;
1815
1816 seq_printf(s, " type: %s controller %s ",
1817 map_type(setting->type),
1818 pinctrl_dev_get_name(pctldev));
1819
1820 switch (setting->type) {
1821 case PIN_MAP_TYPE_MUX_GROUP:
1822 pinmux_show_setting(s, setting);
1823 break;
1824 case PIN_MAP_TYPE_CONFIGS_PIN:
1825 case PIN_MAP_TYPE_CONFIGS_GROUP:
1826 pinconf_show_setting(s, setting);
1827 break;
1828 default:
1829 break;
1830 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001831 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001832 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001833 }
1834
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001835 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001836
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001837 return 0;
1838}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001839DEFINE_SHOW_ATTRIBUTE(pinctrl);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001840
Linus Walleij2744e8a2011-05-02 20:50:54 +02001841static struct dentry *debugfs_root;
1842
1843static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1844{
Tony Lindgren02157162012-01-20 08:17:22 -08001845 struct dentry *device_root;
Jan Kundrát1781af52018-01-26 16:06:37 +01001846 const char *debugfs_name;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001847
Jan Kundrát1781af52018-01-26 16:06:37 +01001848 if (pctldev->desc->name &&
1849 strcmp(dev_name(pctldev->dev), pctldev->desc->name)) {
1850 debugfs_name = devm_kasprintf(pctldev->dev, GFP_KERNEL,
1851 "%s-%s", dev_name(pctldev->dev),
1852 pctldev->desc->name);
1853 if (!debugfs_name) {
1854 pr_warn("failed to determine debugfs dir name for %s\n",
1855 dev_name(pctldev->dev));
1856 return;
1857 }
1858 } else {
1859 debugfs_name = dev_name(pctldev->dev);
1860 }
1861
1862 device_root = debugfs_create_dir(debugfs_name, debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001863 pctldev->device_root = device_root;
1864
Linus Walleij2744e8a2011-05-02 20:50:54 +02001865 if (IS_ERR(device_root) || !device_root) {
1866 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001867 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001868 return;
1869 }
1870 debugfs_create_file("pins", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001871 device_root, pctldev, &pinctrl_pins_fops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001872 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001873 device_root, pctldev, &pinctrl_groups_fops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001874 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001875 device_root, pctldev, &pinctrl_gpioranges_fops);
Florian Vaussarde7f2a442014-02-05 07:51:22 +01001876 if (pctldev->desc->pmxops)
1877 pinmux_init_device_debugfs(device_root, pctldev);
1878 if (pctldev->desc->confops)
1879 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001880}
1881
Tony Lindgren02157162012-01-20 08:17:22 -08001882static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1883{
1884 debugfs_remove_recursive(pctldev->device_root);
1885}
1886
Linus Walleij2744e8a2011-05-02 20:50:54 +02001887static void pinctrl_init_debugfs(void)
1888{
1889 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1890 if (IS_ERR(debugfs_root) || !debugfs_root) {
1891 pr_warn("failed to create debugfs directory\n");
1892 debugfs_root = NULL;
1893 return;
1894 }
1895
1896 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001897 debugfs_root, NULL, &pinctrl_devices_fops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001898 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001899 debugfs_root, NULL, &pinctrl_maps_fops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001900 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001901 debugfs_root, NULL, &pinctrl_fops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001902}
1903
1904#else /* CONFIG_DEBUG_FS */
1905
1906static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1907{
1908}
1909
1910static void pinctrl_init_debugfs(void)
1911{
1912}
1913
Tony Lindgren02157162012-01-20 08:17:22 -08001914static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1915{
1916}
1917
Linus Walleij2744e8a2011-05-02 20:50:54 +02001918#endif
1919
Stephen Warrend26bc492012-03-16 14:54:25 -06001920static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1921{
1922 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1923
1924 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301925 !ops->get_groups_count ||
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001926 !ops->get_group_name)
Stephen Warrend26bc492012-03-16 14:54:25 -06001927 return -EINVAL;
1928
1929 return 0;
1930}
1931
Linus Walleij2744e8a2011-05-02 20:50:54 +02001932/**
Tony Lindgren950b0d92017-01-11 14:13:34 -08001933 * pinctrl_init_controller() - init a pin controller device
Linus Walleij2744e8a2011-05-02 20:50:54 +02001934 * @pctldesc: descriptor for this pin controller
1935 * @dev: parent device for this pin controller
1936 * @driver_data: private pin controller data for this pin controller
1937 */
Andy Shevchenko0ca49212017-03-27 14:37:09 +03001938static struct pinctrl_dev *
1939pinctrl_init_controller(struct pinctrl_desc *pctldesc, struct device *dev,
1940 void *driver_data)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001941{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001942 struct pinctrl_dev *pctldev;
1943 int ret;
1944
Devendra Nagada9aecb2012-06-16 23:43:16 +05301945 if (!pctldesc)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001946 return ERR_PTR(-EINVAL);
Devendra Nagada9aecb2012-06-16 23:43:16 +05301947 if (!pctldesc->name)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001948 return ERR_PTR(-EINVAL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001949
Stephen Warren02f5b982012-02-22 14:26:00 -07001950 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -08001951 if (!pctldev)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001952 return ERR_PTR(-ENOMEM);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001953
1954 /* Initialize pin control device struct */
1955 pctldev->owner = pctldesc->owner;
1956 pctldev->desc = pctldesc;
1957 pctldev->driver_data = driver_data;
1958 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleijc033a712016-12-30 15:04:43 +01001959#ifdef CONFIG_GENERIC_PINCTRL_GROUPS
Tony Lindgrenc7059c52016-12-27 09:20:00 -08001960 INIT_RADIX_TREE(&pctldev->pin_group_tree, GFP_KERNEL);
Linus Walleijc033a712016-12-30 15:04:43 +01001961#endif
Tony Lindgrena76edc82016-12-27 09:20:01 -08001962#ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
1963 INIT_RADIX_TREE(&pctldev->pin_function_tree, GFP_KERNEL);
1964#endif
Linus Walleij2744e8a2011-05-02 20:50:54 +02001965 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Thierry Reding46daed62017-01-12 17:03:34 +01001966 INIT_LIST_HEAD(&pctldev->node);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001967 pctldev->dev = dev;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001968 mutex_init(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001969
Stephen Warrend26bc492012-03-16 14:54:25 -06001970 /* check core ops for sanity */
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001971 ret = pinctrl_check_ops(pctldev);
1972 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001973 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06001974 goto out_err;
1975 }
1976
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001977 /* If we're implementing pinmuxing, check the ops for sanity */
1978 if (pctldesc->pmxops) {
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001979 ret = pinmux_check_ops(pctldev);
1980 if (ret)
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001981 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001982 }
1983
1984 /* If we're implementing pinconfig, check the ops for sanity */
1985 if (pctldesc->confops) {
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001986 ret = pinconf_check_ops(pctldev);
1987 if (ret)
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001988 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001989 }
1990
Linus Walleij2744e8a2011-05-02 20:50:54 +02001991 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02001992 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001993 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1994 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001995 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001996 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1997 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001998 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001999 }
2000
Linus Walleij2744e8a2011-05-02 20:50:54 +02002001 return pctldev;
2002
Stephen Warren51cd24e2011-12-09 16:59:05 -07002003out_err:
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002004 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07002005 kfree(pctldev);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09002006 return ERR_PTR(ret);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002007}
Tony Lindgren950b0d92017-01-11 14:13:34 -08002008
Tony Lindgren61187142017-03-30 09:16:39 -07002009static int pinctrl_claim_hogs(struct pinctrl_dev *pctldev)
Tony Lindgren950b0d92017-01-11 14:13:34 -08002010{
2011 pctldev->p = create_pinctrl(pctldev->dev, pctldev);
Tony Lindgren61187142017-03-30 09:16:39 -07002012 if (PTR_ERR(pctldev->p) == -ENODEV) {
2013 dev_dbg(pctldev->dev, "no hogs found\n");
Tony Lindgren950b0d92017-01-11 14:13:34 -08002014
Tony Lindgren61187142017-03-30 09:16:39 -07002015 return 0;
2016 }
2017
2018 if (IS_ERR(pctldev->p)) {
2019 dev_err(pctldev->dev, "error claiming hogs: %li\n",
2020 PTR_ERR(pctldev->p));
2021
2022 return PTR_ERR(pctldev->p);
2023 }
2024
2025 kref_get(&pctldev->p->users);
2026 pctldev->hog_default =
2027 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
2028 if (IS_ERR(pctldev->hog_default)) {
2029 dev_dbg(pctldev->dev,
2030 "failed to lookup the default state\n");
2031 } else {
2032 if (pinctrl_select_state(pctldev->p,
2033 pctldev->hog_default))
2034 dev_err(pctldev->dev,
2035 "failed to select default state\n");
2036 }
2037
2038 pctldev->hog_sleep =
2039 pinctrl_lookup_state(pctldev->p,
2040 PINCTRL_STATE_SLEEP);
2041 if (IS_ERR(pctldev->hog_sleep))
2042 dev_dbg(pctldev->dev,
2043 "failed to lookup the sleep state\n");
2044
2045 return 0;
2046}
2047
2048int pinctrl_enable(struct pinctrl_dev *pctldev)
2049{
2050 int error;
2051
2052 error = pinctrl_claim_hogs(pctldev);
2053 if (error) {
2054 dev_err(pctldev->dev, "could not claim hogs: %i\n",
2055 error);
2056 mutex_destroy(&pctldev->mutex);
2057 kfree(pctldev);
2058
2059 return error;
Tony Lindgren950b0d92017-01-11 14:13:34 -08002060 }
2061
2062 mutex_lock(&pinctrldev_list_mutex);
2063 list_add_tail(&pctldev->node, &pinctrldev_list);
2064 mutex_unlock(&pinctrldev_list_mutex);
2065
2066 pinctrl_init_device_debugfs(pctldev);
2067
2068 return 0;
2069}
Tony Lindgren61187142017-03-30 09:16:39 -07002070EXPORT_SYMBOL_GPL(pinctrl_enable);
Tony Lindgren950b0d92017-01-11 14:13:34 -08002071
2072/**
2073 * pinctrl_register() - register a pin controller device
2074 * @pctldesc: descriptor for this pin controller
2075 * @dev: parent device for this pin controller
2076 * @driver_data: private pin controller data for this pin controller
2077 *
2078 * Note that pinctrl_register() is known to have problems as the pin
2079 * controller driver functions are called before the driver has a
2080 * struct pinctrl_dev handle. To avoid issues later on, please use the
2081 * new pinctrl_register_and_init() below instead.
2082 */
2083struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
2084 struct device *dev, void *driver_data)
2085{
2086 struct pinctrl_dev *pctldev;
2087 int error;
2088
2089 pctldev = pinctrl_init_controller(pctldesc, dev, driver_data);
2090 if (IS_ERR(pctldev))
2091 return pctldev;
2092
Tony Lindgren61187142017-03-30 09:16:39 -07002093 error = pinctrl_enable(pctldev);
2094 if (error)
Tony Lindgren950b0d92017-01-11 14:13:34 -08002095 return ERR_PTR(error);
Tony Lindgren950b0d92017-01-11 14:13:34 -08002096
2097 return pctldev;
2098
2099}
Linus Walleij2744e8a2011-05-02 20:50:54 +02002100EXPORT_SYMBOL_GPL(pinctrl_register);
2101
Tony Lindgren61187142017-03-30 09:16:39 -07002102/**
2103 * pinctrl_register_and_init() - register and init pin controller device
2104 * @pctldesc: descriptor for this pin controller
2105 * @dev: parent device for this pin controller
2106 * @driver_data: private pin controller data for this pin controller
2107 * @pctldev: pin controller device
2108 *
2109 * Note that pinctrl_enable() still needs to be manually called after
2110 * this once the driver is ready.
2111 */
Tony Lindgren950b0d92017-01-11 14:13:34 -08002112int pinctrl_register_and_init(struct pinctrl_desc *pctldesc,
2113 struct device *dev, void *driver_data,
2114 struct pinctrl_dev **pctldev)
2115{
2116 struct pinctrl_dev *p;
Tony Lindgren950b0d92017-01-11 14:13:34 -08002117
2118 p = pinctrl_init_controller(pctldesc, dev, driver_data);
2119 if (IS_ERR(p))
2120 return PTR_ERR(p);
2121
2122 /*
2123 * We have pinctrl_start() call functions in the pin controller
2124 * driver with create_pinctrl() for at least dt_node_to_map(). So
2125 * let's make sure pctldev is properly initialized for the
2126 * pin controller driver before we do anything.
2127 */
2128 *pctldev = p;
2129
Tony Lindgren950b0d92017-01-11 14:13:34 -08002130 return 0;
2131}
2132EXPORT_SYMBOL_GPL(pinctrl_register_and_init);
2133
Linus Walleij2744e8a2011-05-02 20:50:54 +02002134/**
2135 * pinctrl_unregister() - unregister pinmux
2136 * @pctldev: pin controller to unregister
2137 *
2138 * Called by pinmux drivers to unregister a pinmux.
2139 */
2140void pinctrl_unregister(struct pinctrl_dev *pctldev)
2141{
Dong Aisheng5d589b02012-05-23 21:22:40 +08002142 struct pinctrl_gpio_range *range, *n;
Jon Hunter3429fb32017-01-05 15:52:55 +00002143
Markus Elfringcea234e2017-05-02 11:04:55 +02002144 if (!pctldev)
Linus Walleij2744e8a2011-05-02 20:50:54 +02002145 return;
2146
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002147 mutex_lock(&pctldev->mutex);
Tony Lindgren02157162012-01-20 08:17:22 -08002148 pinctrl_remove_device_debugfs(pctldev);
Jim Lindb93fac2015-01-08 20:25:05 +08002149 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07002150
Jon Hunter3429fb32017-01-05 15:52:55 +00002151 if (!IS_ERR_OR_NULL(pctldev->p))
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002152 pinctrl_put(pctldev->p);
Stephen Warren57b676f2012-03-02 13:05:44 -07002153
Jim Lindb93fac2015-01-08 20:25:05 +08002154 mutex_lock(&pinctrldev_list_mutex);
2155 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002156 /* TODO: check that no pinmuxes are still active? */
Thierry Reding46daed62017-01-12 17:03:34 +01002157 list_del(&pctldev->node);
Tony Lindgrena76edc82016-12-27 09:20:01 -08002158 pinmux_generic_free_functions(pctldev);
Tony Lindgrenc7059c52016-12-27 09:20:00 -08002159 pinctrl_generic_free_groups(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002160 /* Destroy descriptor tree */
2161 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
2162 pctldev->desc->npins);
Dong Aisheng5d589b02012-05-23 21:22:40 +08002163 /* remove gpio ranges map */
2164 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
2165 list_del(&range->node);
2166
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002167 mutex_unlock(&pctldev->mutex);
2168 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07002169 kfree(pctldev);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002170 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002171}
2172EXPORT_SYMBOL_GPL(pinctrl_unregister);
2173
Laxman Dewangan80e0f8d2016-02-24 14:12:59 +05302174static void devm_pinctrl_dev_release(struct device *dev, void *res)
2175{
2176 struct pinctrl_dev *pctldev = *(struct pinctrl_dev **)res;
2177
2178 pinctrl_unregister(pctldev);
2179}
2180
2181static int devm_pinctrl_dev_match(struct device *dev, void *res, void *data)
2182{
2183 struct pctldev **r = res;
2184
Laxman Dewangan3024f922016-02-24 14:44:07 +05302185 if (WARN_ON(!r || !*r))
Laxman Dewangan80e0f8d2016-02-24 14:12:59 +05302186 return 0;
2187
2188 return *r == data;
2189}
2190
2191/**
2192 * devm_pinctrl_register() - Resource managed version of pinctrl_register().
2193 * @dev: parent device for this pin controller
2194 * @pctldesc: descriptor for this pin controller
2195 * @driver_data: private pin controller data for this pin controller
2196 *
2197 * Returns an error pointer if pincontrol register failed. Otherwise
2198 * it returns valid pinctrl handle.
2199 *
2200 * The pinctrl device will be automatically released when the device is unbound.
2201 */
2202struct pinctrl_dev *devm_pinctrl_register(struct device *dev,
2203 struct pinctrl_desc *pctldesc,
2204 void *driver_data)
2205{
2206 struct pinctrl_dev **ptr, *pctldev;
2207
2208 ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
2209 if (!ptr)
2210 return ERR_PTR(-ENOMEM);
2211
2212 pctldev = pinctrl_register(pctldesc, dev, driver_data);
2213 if (IS_ERR(pctldev)) {
2214 devres_free(ptr);
2215 return pctldev;
2216 }
2217
2218 *ptr = pctldev;
2219 devres_add(dev, ptr);
2220
2221 return pctldev;
2222}
2223EXPORT_SYMBOL_GPL(devm_pinctrl_register);
2224
2225/**
Tony Lindgren950b0d92017-01-11 14:13:34 -08002226 * devm_pinctrl_register_and_init() - Resource managed pinctrl register and init
2227 * @dev: parent device for this pin controller
2228 * @pctldesc: descriptor for this pin controller
2229 * @driver_data: private pin controller data for this pin controller
2230 *
2231 * Returns an error pointer if pincontrol register failed. Otherwise
2232 * it returns valid pinctrl handle.
2233 *
2234 * The pinctrl device will be automatically released when the device is unbound.
2235 */
2236int devm_pinctrl_register_and_init(struct device *dev,
2237 struct pinctrl_desc *pctldesc,
2238 void *driver_data,
2239 struct pinctrl_dev **pctldev)
2240{
2241 struct pinctrl_dev **ptr;
2242 int error;
2243
2244 ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
2245 if (!ptr)
2246 return -ENOMEM;
2247
2248 error = pinctrl_register_and_init(pctldesc, dev, driver_data, pctldev);
2249 if (error) {
2250 devres_free(ptr);
2251 return error;
2252 }
2253
2254 *ptr = *pctldev;
2255 devres_add(dev, ptr);
2256
2257 return 0;
2258}
2259EXPORT_SYMBOL_GPL(devm_pinctrl_register_and_init);
2260
2261/**
Laxman Dewangan80e0f8d2016-02-24 14:12:59 +05302262 * devm_pinctrl_unregister() - Resource managed version of pinctrl_unregister().
2263 * @dev: device for which which resource was allocated
2264 * @pctldev: the pinctrl device to unregister.
2265 */
2266void devm_pinctrl_unregister(struct device *dev, struct pinctrl_dev *pctldev)
2267{
2268 WARN_ON(devres_release(dev, devm_pinctrl_dev_release,
2269 devm_pinctrl_dev_match, pctldev));
2270}
2271EXPORT_SYMBOL_GPL(devm_pinctrl_unregister);
2272
Linus Walleij2744e8a2011-05-02 20:50:54 +02002273static int __init pinctrl_init(void)
2274{
2275 pr_info("initialized pinctrl subsystem\n");
2276 pinctrl_init_debugfs();
2277 return 0;
2278}
2279
2280/* init early since many drivers really need to initialized pinmux early */
2281core_initcall(pinctrl_init);