blob: b0eea728455de0d5e974d6c6369a07d202cb5988 [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}
179
Linus Walleij2744e8a2011-05-02 20:50:54 +0200180/* Deletes a range of pin descriptors */
181static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
182 const struct pinctrl_pin_desc *pins,
183 unsigned num_pins)
184{
185 int i;
186
Linus Walleij2744e8a2011-05-02 20:50:54 +0200187 for (i = 0; i < num_pins; i++) {
188 struct pin_desc *pindesc;
189
190 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
191 pins[i].number);
Markus Elfringcea234e2017-05-02 11:04:55 +0200192 if (pindesc) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200193 radix_tree_delete(&pctldev->pin_desc_tree,
194 pins[i].number);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100195 if (pindesc->dynamic_name)
196 kfree(pindesc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200197 }
198 kfree(pindesc);
199 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200200}
201
202static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900203 const struct pinctrl_pin_desc *pin)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200204{
205 struct pin_desc *pindesc;
206
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900207 pindesc = pin_desc_get(pctldev, pin->number);
Markus Elfringcea234e2017-05-02 11:04:55 +0200208 if (pindesc) {
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900209 dev_err(pctldev->dev, "pin %d already registered\n",
210 pin->number);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200211 return -EINVAL;
212 }
213
214 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -0800215 if (!pindesc)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200216 return -ENOMEM;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200217
Linus Walleij2744e8a2011-05-02 20:50:54 +0200218 /* Set owner */
219 pindesc->pctldev = pctldev;
220
Stephen Warren9af1e442011-10-19 16:19:27 -0600221 /* Copy basic pin info */
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900222 if (pin->name) {
223 pindesc->name = pin->name;
Linus Walleijca53c5f2011-12-14 20:33:37 +0100224 } else {
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900225 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", pin->number);
Markus Elfringcea234e2017-05-02 11:04:55 +0200226 if (!pindesc->name) {
Sachin Kamateb26cc92012-09-25 12:41:40 +0530227 kfree(pindesc);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100228 return -ENOMEM;
Sachin Kamateb26cc92012-09-25 12:41:40 +0530229 }
Linus Walleijca53c5f2011-12-14 20:33:37 +0100230 pindesc->dynamic_name = true;
231 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200232
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900233 pindesc->drv_data = pin->drv_data;
234
235 radix_tree_insert(&pctldev->pin_desc_tree, pin->number, pindesc);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200236 pr_debug("registered pin %d (%s) on %s\n",
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900237 pin->number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200238 return 0;
239}
240
241static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900242 const struct pinctrl_pin_desc *pins,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200243 unsigned num_descs)
244{
245 unsigned i;
246 int ret = 0;
247
248 for (i = 0; i < num_descs; i++) {
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900249 ret = pinctrl_register_one_pin(pctldev, &pins[i]);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200250 if (ret)
251 return ret;
252 }
253
254 return 0;
255}
256
257/**
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200258 * gpio_to_pin() - GPIO range GPIO number to pin number translation
259 * @range: GPIO range used for the translation
260 * @gpio: gpio pin to translate to a pin number
261 *
262 * Finds the pin number for a given GPIO using the specified GPIO range
263 * as a base for translation. The distinction between linear GPIO ranges
264 * and pin list based GPIO ranges is managed correctly by this function.
265 *
266 * This function assumes the gpio is part of the specified GPIO range, use
267 * only after making sure this is the case (e.g. by calling it on the
268 * result of successful pinctrl_get_device_gpio_range calls)!
269 */
270static inline int gpio_to_pin(struct pinctrl_gpio_range *range,
271 unsigned int gpio)
272{
273 unsigned int offset = gpio - range->base;
274 if (range->pins)
275 return range->pins[offset];
276 else
277 return range->pin_base + offset;
278}
279
280/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200281 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
282 * @pctldev: pin controller device to check
283 * @gpio: gpio pin to check taken from the global GPIO pin space
284 *
285 * Tries to match a GPIO pin number to the ranges handled by a certain pin
286 * controller, return the range or NULL
287 */
288static struct pinctrl_gpio_range *
289pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
290{
Masahiro Yamada6cadafb2019-06-09 23:55:37 +0900291 struct pinctrl_gpio_range *range;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200292
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200293 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200294 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200295 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
296 /* Check if we're in the valid range */
297 if (gpio >= range->base &&
298 gpio < range->base + range->npins) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200299 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200300 return range;
301 }
302 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200303 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200304 return NULL;
305}
306
307/**
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800308 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
309 * the same GPIO chip are in range
310 * @gpio: gpio pin to check taken from the global GPIO pin space
311 *
312 * This function is complement of pinctrl_match_gpio_range(). If the return
313 * value of pinctrl_match_gpio_range() is NULL, this function could be used
314 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
315 * of the same GPIO chip don't have back-end pinctrl interface.
316 * If the return value is true, it means that pinctrl device is ready & the
317 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
318 * is false, it means that pinctrl device may not be ready.
319 */
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800320#ifdef CONFIG_GPIOLIB
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800321static bool pinctrl_ready_for_gpio_range(unsigned gpio)
322{
323 struct pinctrl_dev *pctldev;
324 struct pinctrl_gpio_range *range = NULL;
325 struct gpio_chip *chip = gpio_to_chip(gpio);
326
Tony Lindgren942cde72015-09-03 10:34:30 -0700327 if (WARN(!chip, "no gpio_chip for gpio%i?", gpio))
328 return false;
329
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200330 mutex_lock(&pinctrldev_list_mutex);
331
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800332 /* Loop over the pin controllers */
333 list_for_each_entry(pctldev, &pinctrldev_list, node) {
334 /* Loop over the ranges */
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800335 mutex_lock(&pctldev->mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800336 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
337 /* Check if any gpio range overlapped with gpio chip */
338 if (range->base + range->npins - 1 < chip->base ||
339 range->base > chip->base + chip->ngpio - 1)
340 continue;
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800341 mutex_unlock(&pctldev->mutex);
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200342 mutex_unlock(&pinctrldev_list_mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800343 return true;
344 }
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800345 mutex_unlock(&pctldev->mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800346 }
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200347
348 mutex_unlock(&pinctrldev_list_mutex);
349
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800350 return false;
351}
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800352#else
353static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
354#endif
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800355
356/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200357 * pinctrl_get_device_gpio_range() - find device for GPIO range
358 * @gpio: the pin to locate the pin controller for
359 * @outdev: the pin control device if found
360 * @outrange: the GPIO range if found
361 *
362 * Find the pin controller handling a certain GPIO pin from the pinspace of
363 * the GPIO subsystem, return the device and the matching GPIO range. Returns
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800364 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
365 * may still have not been registered.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200366 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700367static int pinctrl_get_device_gpio_range(unsigned gpio,
368 struct pinctrl_dev **outdev,
369 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200370{
Masahiro Yamada6cadafb2019-06-09 23:55:37 +0900371 struct pinctrl_dev *pctldev;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200372
Axel Linf0059022013-08-18 20:34:22 +0800373 mutex_lock(&pinctrldev_list_mutex);
374
Linus Walleij2744e8a2011-05-02 20:50:54 +0200375 /* Loop over the pin controllers */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200376 list_for_each_entry(pctldev, &pinctrldev_list, node) {
377 struct pinctrl_gpio_range *range;
378
379 range = pinctrl_match_gpio_range(pctldev, gpio);
Markus Elfringcea234e2017-05-02 11:04:55 +0200380 if (range) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200381 *outdev = pctldev;
382 *outrange = range;
Axel Linf0059022013-08-18 20:34:22 +0800383 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200384 return 0;
385 }
386 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200387
Axel Linf0059022013-08-18 20:34:22 +0800388 mutex_unlock(&pinctrldev_list_mutex);
389
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800390 return -EPROBE_DEFER;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200391}
392
393/**
394 * pinctrl_add_gpio_range() - register a GPIO range for a controller
395 * @pctldev: pin controller device to add the range to
396 * @range: the GPIO range to add
397 *
398 * This adds a range of GPIOs to be handled by a certain pin controller. Call
399 * this to register handled ranges after registering your pin controller.
400 */
401void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
402 struct pinctrl_gpio_range *range)
403{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200404 mutex_lock(&pctldev->mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700405 list_add_tail(&range->node, &pctldev->gpio_ranges);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200406 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200407}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700408EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200409
Dong Aisheng3e5e00b2012-05-23 21:22:41 +0800410void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
411 struct pinctrl_gpio_range *ranges,
412 unsigned nranges)
413{
414 int i;
415
416 for (i = 0; i < nranges; i++)
417 pinctrl_add_gpio_range(pctldev, &ranges[i]);
418}
419EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
420
Linus Walleij192c3692012-11-20 14:03:37 +0100421struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530422 struct pinctrl_gpio_range *range)
423{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200424 struct pinctrl_dev *pctldev;
425
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200426 pctldev = get_pinctrl_dev_from_devname(devname);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530427
Linus Walleijdfa97512012-11-20 14:54:18 +0100428 /*
429 * If we can't find this device, let's assume that is because
430 * it has not probed yet, so the driver trying to register this
431 * range need to defer probing.
432 */
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200433 if (!pctldev) {
Linus Walleijdfa97512012-11-20 14:54:18 +0100434 return ERR_PTR(-EPROBE_DEFER);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200435 }
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530436 pinctrl_add_gpio_range(pctldev, range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200437
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530438 return pctldev;
439}
Linus Walleij192c3692012-11-20 14:03:37 +0100440EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530441
Christian Ruppert586a87e2013-10-15 15:37:54 +0200442int pinctrl_get_group_pins(struct pinctrl_dev *pctldev, const char *pin_group,
443 const unsigned **pins, unsigned *num_pins)
444{
445 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
446 int gs;
447
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +0200448 if (!pctlops->get_group_pins)
449 return -EINVAL;
450
Christian Ruppert586a87e2013-10-15 15:37:54 +0200451 gs = pinctrl_get_group_selector(pctldev, pin_group);
452 if (gs < 0)
453 return gs;
454
455 return pctlops->get_group_pins(pctldev, gs, pins, num_pins);
456}
457EXPORT_SYMBOL_GPL(pinctrl_get_group_pins);
458
Joachim Eastwoodb18537c2016-02-25 22:44:37 +0100459struct pinctrl_gpio_range *
460pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev *pctldev,
461 unsigned int pin)
462{
463 struct pinctrl_gpio_range *range;
464
465 /* Loop over the ranges */
466 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
467 /* Check if we're in the valid range */
468 if (range->pins) {
469 int a;
470 for (a = 0; a < range->npins; a++) {
471 if (range->pins[a] == pin)
472 return range;
473 }
474 } else if (pin >= range->pin_base &&
475 pin < range->pin_base + range->npins)
476 return range;
477 }
478
479 return NULL;
480}
481EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin_nolock);
482
Linus Walleij2744e8a2011-05-02 20:50:54 +0200483/**
Linus Walleij9afbefb2012-11-20 14:25:07 +0100484 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
485 * @pctldev: the pin controller device to look in
486 * @pin: a controller-local number to find the range for
487 */
488struct pinctrl_gpio_range *
489pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
490 unsigned int pin)
491{
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800492 struct pinctrl_gpio_range *range;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100493
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200494 mutex_lock(&pctldev->mutex);
Joachim Eastwoodb18537c2016-02-25 22:44:37 +0100495 range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200496 mutex_unlock(&pctldev->mutex);
Joachim Eastwoodb18537c2016-02-25 22:44:37 +0100497
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800498 return range;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100499}
500EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
501
502/**
Charles Keepax50842cb2017-02-13 10:11:03 +0000503 * pinctrl_remove_gpio_range() - remove a range of GPIOs from a pin controller
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530504 * @pctldev: pin controller device to remove the range from
505 * @range: the GPIO range to remove
506 */
507void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
508 struct pinctrl_gpio_range *range)
509{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200510 mutex_lock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530511 list_del(&range->node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200512 mutex_unlock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530513}
514EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
515
Linus Walleijc033a712016-12-30 15:04:43 +0100516#ifdef CONFIG_GENERIC_PINCTRL_GROUPS
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800517
518/**
519 * pinctrl_generic_get_group_count() - returns the number of pin groups
520 * @pctldev: pin controller device
521 */
522int pinctrl_generic_get_group_count(struct pinctrl_dev *pctldev)
523{
524 return pctldev->num_groups;
525}
526EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_count);
527
528/**
529 * pinctrl_generic_get_group_name() - returns the name of a pin group
530 * @pctldev: pin controller device
531 * @selector: group number
532 */
533const char *pinctrl_generic_get_group_name(struct pinctrl_dev *pctldev,
534 unsigned int selector)
535{
536 struct group_desc *group;
537
538 group = radix_tree_lookup(&pctldev->pin_group_tree,
539 selector);
540 if (!group)
541 return NULL;
542
543 return group->name;
544}
545EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_name);
546
547/**
548 * pinctrl_generic_get_group_pins() - gets the pin group pins
549 * @pctldev: pin controller device
550 * @selector: group number
551 * @pins: pins in the group
552 * @num_pins: number of pins in the group
553 */
554int pinctrl_generic_get_group_pins(struct pinctrl_dev *pctldev,
555 unsigned int selector,
556 const unsigned int **pins,
557 unsigned int *num_pins)
558{
559 struct group_desc *group;
560
561 group = radix_tree_lookup(&pctldev->pin_group_tree,
562 selector);
563 if (!group) {
564 dev_err(pctldev->dev, "%s could not find pingroup%i\n",
565 __func__, selector);
566 return -EINVAL;
567 }
568
569 *pins = group->pins;
570 *num_pins = group->num_pins;
571
572 return 0;
573}
574EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_pins);
575
576/**
577 * pinctrl_generic_get_group() - returns a pin group based on the number
578 * @pctldev: pin controller device
579 * @gselector: group number
580 */
581struct group_desc *pinctrl_generic_get_group(struct pinctrl_dev *pctldev,
582 unsigned int selector)
583{
584 struct group_desc *group;
585
586 group = radix_tree_lookup(&pctldev->pin_group_tree,
587 selector);
588 if (!group)
589 return NULL;
590
591 return group;
592}
593EXPORT_SYMBOL_GPL(pinctrl_generic_get_group);
594
Tony Lindgrena2037282018-07-05 02:10:14 -0700595static int pinctrl_generic_group_name_to_selector(struct pinctrl_dev *pctldev,
596 const char *function)
597{
598 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
599 int ngroups = ops->get_groups_count(pctldev);
600 int selector = 0;
601
602 /* See if this pctldev has this group */
603 while (selector < ngroups) {
604 const char *gname = ops->get_group_name(pctldev, selector);
605
Yanjiang Jin54a58182018-09-29 17:06:55 +0800606 if (gname && !strcmp(function, gname))
Tony Lindgrena2037282018-07-05 02:10:14 -0700607 return selector;
608
609 selector++;
610 }
611
612 return -EINVAL;
613}
614
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800615/**
616 * pinctrl_generic_add_group() - adds a new pin group
617 * @pctldev: pin controller device
618 * @name: name of the pin group
619 * @pins: pins in the pin group
620 * @num_pins: number of pins in the pin group
621 * @data: pin controller driver specific data
622 *
623 * Note that the caller must take care of locking.
624 */
625int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name,
626 int *pins, int num_pins, void *data)
627{
628 struct group_desc *group;
Tony Lindgrena2037282018-07-05 02:10:14 -0700629 int selector;
630
631 if (!name)
632 return -EINVAL;
633
634 selector = pinctrl_generic_group_name_to_selector(pctldev, name);
635 if (selector >= 0)
636 return selector;
637
638 selector = pctldev->num_groups;
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800639
640 group = devm_kzalloc(pctldev->dev, sizeof(*group), GFP_KERNEL);
641 if (!group)
642 return -ENOMEM;
643
644 group->name = name;
645 group->pins = pins;
646 group->num_pins = num_pins;
647 group->data = data;
648
Tony Lindgrena2037282018-07-05 02:10:14 -0700649 radix_tree_insert(&pctldev->pin_group_tree, selector, group);
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800650
651 pctldev->num_groups++;
652
Tony Lindgrena2037282018-07-05 02:10:14 -0700653 return selector;
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800654}
655EXPORT_SYMBOL_GPL(pinctrl_generic_add_group);
656
657/**
658 * pinctrl_generic_remove_group() - removes a numbered pin group
659 * @pctldev: pin controller device
660 * @selector: group number
661 *
662 * Note that the caller must take care of locking.
663 */
664int pinctrl_generic_remove_group(struct pinctrl_dev *pctldev,
665 unsigned int selector)
666{
667 struct group_desc *group;
668
669 group = radix_tree_lookup(&pctldev->pin_group_tree,
670 selector);
671 if (!group)
672 return -ENOENT;
673
674 radix_tree_delete(&pctldev->pin_group_tree, selector);
675 devm_kfree(pctldev->dev, group);
676
677 pctldev->num_groups--;
678
679 return 0;
680}
681EXPORT_SYMBOL_GPL(pinctrl_generic_remove_group);
682
683/**
684 * pinctrl_generic_free_groups() - removes all pin groups
685 * @pctldev: pin controller device
686 *
Tony Lindgren664b7c42017-05-12 08:47:57 -0700687 * Note that the caller must take care of locking. The pinctrl groups
688 * are allocated with devm_kzalloc() so no need to free them here.
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800689 */
690static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
691{
692 struct radix_tree_iter iter;
Masahiro Yamada906a2a32017-08-04 13:52:05 +0900693 void __rcu **slot;
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800694
695 radix_tree_for_each_slot(slot, &pctldev->pin_group_tree, &iter, 0)
Tony Lindgren664b7c42017-05-12 08:47:57 -0700696 radix_tree_delete(&pctldev->pin_group_tree, iter.index);
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800697
698 pctldev->num_groups = 0;
699}
700
701#else
702static inline void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
703{
704}
Linus Walleijc033a712016-12-30 15:04:43 +0100705#endif /* CONFIG_GENERIC_PINCTRL_GROUPS */
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800706
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530707/**
Linus Walleij7afde8b2011-10-19 17:07:16 +0200708 * pinctrl_get_group_selector() - returns the group selector for a group
709 * @pctldev: the pin controller handling the group
710 * @pin_group: the pin group to look up
711 */
712int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
713 const char *pin_group)
714{
715 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530716 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200717 unsigned group_selector = 0;
718
Viresh Kumard1e90e92012-03-30 11:25:40 +0530719 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200720 const char *gname = pctlops->get_group_name(pctldev,
721 group_selector);
Yanjiang Jin54a58182018-09-29 17:06:55 +0800722 if (gname && !strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700723 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200724 "found group selector %u for %s\n",
725 group_selector,
726 pin_group);
727 return group_selector;
728 }
729
730 group_selector++;
731 }
732
Stephen Warren51cd24e2011-12-09 16:59:05 -0700733 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200734 pin_group);
735
736 return -EINVAL;
737}
738
Stefan Wahren472a61e2019-08-14 14:00:35 +0300739bool pinctrl_gpio_can_use_line(unsigned gpio)
740{
741 struct pinctrl_dev *pctldev;
742 struct pinctrl_gpio_range *range;
743 bool result;
744 int pin;
745
746 /*
747 * Try to obtain GPIO range, if it fails
748 * we're probably dealing with GPIO driver
749 * without a backing pin controller - bail out.
750 */
751 if (pinctrl_get_device_gpio_range(gpio, &pctldev, &range))
752 return true;
753
754 mutex_lock(&pctldev->mutex);
755
756 /* Convert to the pin controllers number space */
757 pin = gpio_to_pin(range, gpio);
758
759 result = pinmux_can_be_used_for_gpio(pctldev, pin);
760
761 mutex_unlock(&pctldev->mutex);
762
763 return result;
764}
765EXPORT_SYMBOL_GPL(pinctrl_gpio_can_use_line);
766
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100767/**
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200768 * pinctrl_gpio_request() - request a single pin to be used as GPIO
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100769 * @gpio: the GPIO pin number from the GPIO subsystem number space
770 *
771 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
772 * as part of their gpio_request() semantics, platforms and individual drivers
773 * shall *NOT* request GPIO pins to be muxed in.
774 */
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200775int pinctrl_gpio_request(unsigned gpio)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100776{
777 struct pinctrl_dev *pctldev;
778 struct pinctrl_gpio_range *range;
779 int ret;
780 int pin;
781
782 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700783 if (ret) {
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800784 if (pinctrl_ready_for_gpio_range(gpio))
785 ret = 0;
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800786 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700787 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100788
Axel Lin9b77ace42013-08-19 10:07:46 +0800789 mutex_lock(&pctldev->mutex);
790
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100791 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200792 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100793
Stephen Warren57b676f2012-03-02 13:05:44 -0700794 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
795
Axel Lin9b77ace42013-08-19 10:07:46 +0800796 mutex_unlock(&pctldev->mutex);
797
Stephen Warren57b676f2012-03-02 13:05:44 -0700798 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100799}
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200800EXPORT_SYMBOL_GPL(pinctrl_gpio_request);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100801
802/**
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200803 * pinctrl_gpio_free() - free control on a single pin, currently used as GPIO
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100804 * @gpio: the GPIO pin number from the GPIO subsystem number space
805 *
806 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
807 * as part of their gpio_free() semantics, platforms and individual drivers
808 * shall *NOT* request GPIO pins to be muxed out.
809 */
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200810void pinctrl_gpio_free(unsigned gpio)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100811{
812 struct pinctrl_dev *pctldev;
813 struct pinctrl_gpio_range *range;
814 int ret;
815 int pin;
816
817 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700818 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100819 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700820 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200821 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100822
823 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200824 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100825
Stephen Warren57b676f2012-03-02 13:05:44 -0700826 pinmux_free_gpio(pctldev, pin, range);
827
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200828 mutex_unlock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100829}
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200830EXPORT_SYMBOL_GPL(pinctrl_gpio_free);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100831
832static int pinctrl_gpio_direction(unsigned gpio, bool input)
833{
834 struct pinctrl_dev *pctldev;
835 struct pinctrl_gpio_range *range;
836 int ret;
837 int pin;
838
839 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200840 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100841 return ret;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200842 }
843
844 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100845
846 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200847 pin = gpio_to_pin(range, gpio);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200848 ret = pinmux_gpio_direction(pctldev, range, pin, input);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100849
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200850 mutex_unlock(&pctldev->mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200851
852 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100853}
854
855/**
856 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
857 * @gpio: the GPIO pin number from the GPIO subsystem number space
858 *
859 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
860 * as part of their gpio_direction_input() semantics, platforms and individual
861 * drivers shall *NOT* touch pin control GPIO calls.
862 */
863int pinctrl_gpio_direction_input(unsigned gpio)
864{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200865 return pinctrl_gpio_direction(gpio, true);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100866}
867EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
868
869/**
870 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
871 * @gpio: the GPIO pin number from the GPIO subsystem number space
872 *
873 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
874 * as part of their gpio_direction_output() semantics, platforms and individual
875 * drivers shall *NOT* touch pin control GPIO calls.
876 */
877int pinctrl_gpio_direction_output(unsigned gpio)
878{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200879 return pinctrl_gpio_direction(gpio, false);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100880}
881EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
882
Mika Westerberg15381bc2017-01-23 15:34:33 +0300883/**
884 * pinctrl_gpio_set_config() - Apply config to given GPIO pin
885 * @gpio: the GPIO pin number from the GPIO subsystem number space
886 * @config: the configuration to apply to the GPIO
887 *
888 * This function should *ONLY* be used from gpiolib-based GPIO drivers, if
889 * they need to call the underlying pin controller to change GPIO config
890 * (for example set debounce time).
891 */
892int pinctrl_gpio_set_config(unsigned gpio, unsigned long config)
893{
894 unsigned long configs[] = { config };
895 struct pinctrl_gpio_range *range;
896 struct pinctrl_dev *pctldev;
897 int ret, pin;
898
899 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
900 if (ret)
901 return ret;
902
903 mutex_lock(&pctldev->mutex);
904 pin = gpio_to_pin(range, gpio);
905 ret = pinconf_set_config(pctldev, pin, configs, ARRAY_SIZE(configs));
906 mutex_unlock(&pctldev->mutex);
907
908 return ret;
909}
910EXPORT_SYMBOL_GPL(pinctrl_gpio_set_config);
911
Stephen Warren6e5e9592012-03-02 13:05:47 -0700912static struct pinctrl_state *find_state(struct pinctrl *p,
913 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100914{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700915 struct pinctrl_state *state;
916
917 list_for_each_entry(state, &p->states, node)
918 if (!strcmp(state->name, name))
919 return state;
920
921 return NULL;
922}
923
924static struct pinctrl_state *create_state(struct pinctrl *p,
925 const char *name)
926{
927 struct pinctrl_state *state;
928
929 state = kzalloc(sizeof(*state), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -0800930 if (!state)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700931 return ERR_PTR(-ENOMEM);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700932
933 state->name = name;
934 INIT_LIST_HEAD(&state->settings);
935
936 list_add_tail(&state->node, &p->states);
937
938 return state;
939}
940
Tony Lindgren99e4f672016-12-27 09:19:59 -0800941static int add_setting(struct pinctrl *p, struct pinctrl_dev *pctldev,
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900942 const struct pinctrl_map *map)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700943{
944 struct pinctrl_state *state;
945 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700946 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700947
948 state = find_state(p, map->name);
949 if (!state)
950 state = create_state(p, map->name);
951 if (IS_ERR(state))
952 return PTR_ERR(state);
953
Stephen Warren1e2082b2012-03-02 13:05:48 -0700954 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
955 return 0;
956
Stephen Warren6e5e9592012-03-02 13:05:47 -0700957 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -0800958 if (!setting)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700959 return -ENOMEM;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700960
Stephen Warren1e2082b2012-03-02 13:05:48 -0700961 setting->type = map->type;
962
Tony Lindgren99e4f672016-12-27 09:19:59 -0800963 if (pctldev)
964 setting->pctldev = pctldev;
965 else
966 setting->pctldev =
967 get_pinctrl_dev_from_devname(map->ctrl_dev_name);
Markus Elfringcea234e2017-05-02 11:04:55 +0200968 if (!setting->pctldev) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700969 kfree(setting);
Linus Walleij89216492012-12-11 14:14:32 +0100970 /* Do not defer probing of hogs (circular loop) */
971 if (!strcmp(map->ctrl_dev_name, map->dev_name))
972 return -ENODEV;
Linus Walleijc05127c2012-04-10 10:00:38 +0200973 /*
974 * OK let us guess that the driver is not there yet, and
975 * let's defer obtaining this pinctrl handle to later...
976 */
Linus Walleij89216492012-12-11 14:14:32 +0100977 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
978 map->ctrl_dev_name);
Linus Walleijc05127c2012-04-10 10:00:38 +0200979 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700980 }
981
Linus Walleij1a789582012-10-17 20:51:54 +0200982 setting->dev_name = map->dev_name;
983
Stephen Warren1e2082b2012-03-02 13:05:48 -0700984 switch (map->type) {
985 case PIN_MAP_TYPE_MUX_GROUP:
986 ret = pinmux_map_to_setting(map, setting);
987 break;
988 case PIN_MAP_TYPE_CONFIGS_PIN:
989 case PIN_MAP_TYPE_CONFIGS_GROUP:
990 ret = pinconf_map_to_setting(map, setting);
991 break;
992 default:
993 ret = -EINVAL;
994 break;
995 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700996 if (ret < 0) {
997 kfree(setting);
998 return ret;
999 }
1000
1001 list_add_tail(&setting->node, &state->settings);
1002
1003 return 0;
1004}
1005
1006static struct pinctrl *find_pinctrl(struct device *dev)
1007{
1008 struct pinctrl *p;
1009
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001010 mutex_lock(&pinctrl_list_mutex);
Stephen Warren1e2082b2012-03-02 13:05:48 -07001011 list_for_each_entry(p, &pinctrl_list, node)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001012 if (p->dev == dev) {
1013 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001014 return p;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001015 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001016
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001017 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001018 return NULL;
1019}
1020
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001021static void pinctrl_free(struct pinctrl *p, bool inlist);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001022
Tony Lindgren99e4f672016-12-27 09:19:59 -08001023static struct pinctrl *create_pinctrl(struct device *dev,
1024 struct pinctrl_dev *pctldev)
Stephen Warren6e5e9592012-03-02 13:05:47 -07001025{
1026 struct pinctrl *p;
1027 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001028 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001029 int i;
Masahiro Yamada3f713b72017-08-04 11:22:31 +09001030 const struct pinctrl_map *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001031 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001032
1033 /*
1034 * create the state cookie holder struct pinctrl for each
1035 * mapping, this is what consumers will get when requesting
1036 * a pin control handle with pinctrl_get()
1037 */
Stephen Warren02f5b982012-02-22 14:26:00 -07001038 p = kzalloc(sizeof(*p), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -08001039 if (!p)
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001040 return ERR_PTR(-ENOMEM);
Stephen Warren7ecdb162012-03-02 13:05:45 -07001041 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001042 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -06001043 INIT_LIST_HEAD(&p->dt_maps);
1044
Tony Lindgren99e4f672016-12-27 09:19:59 -08001045 ret = pinctrl_dt_to_map(p, pctldev);
Stephen Warren57291ce2012-03-23 10:29:46 -06001046 if (ret < 0) {
1047 kfree(p);
1048 return ERR_PTR(ret);
1049 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001050
1051 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001052
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001053 mutex_lock(&pinctrl_maps_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001054 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001055 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -07001056 /* Map must be for this device */
1057 if (strcmp(map->dev_name, devname))
1058 continue;
Nikita Yushchenko7f0ff062017-05-11 23:02:11 +03001059 /*
1060 * If pctldev is not null, we are claiming hog for it,
1061 * that means, setting that is served by pctldev by itself.
1062 *
1063 * Thus we must skip map that is for this device but is served
1064 * by other device.
1065 */
1066 if (pctldev &&
1067 strcmp(dev_name(pctldev->dev), map->ctrl_dev_name))
1068 continue;
Stephen Warren1681f5a2012-02-22 14:25:58 -07001069
Tony Lindgren99e4f672016-12-27 09:19:59 -08001070 ret = add_setting(p, pctldev, map);
Linus Walleij89216492012-12-11 14:14:32 +01001071 /*
1072 * At this point the adding of a setting may:
1073 *
1074 * - Defer, if the pinctrl device is not yet available
1075 * - Fail, if the pinctrl device is not yet available,
1076 * AND the setting is a hog. We cannot defer that, since
1077 * the hog will kick in immediately after the device
1078 * is registered.
1079 *
1080 * If the error returned was not -EPROBE_DEFER then we
1081 * accumulate the errors to see if we end up with
1082 * an -EPROBE_DEFER later, as that is the worst case.
1083 */
1084 if (ret == -EPROBE_DEFER) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001085 pinctrl_free(p, false);
1086 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001087 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001088 }
1089 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001090 mutex_unlock(&pinctrl_maps_mutex);
1091
Linus Walleij89216492012-12-11 14:14:32 +01001092 if (ret < 0) {
Andy Shevchenko3ec440e2017-02-28 16:59:56 +02001093 /* If some other error than deferral occurred, return here */
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001094 pinctrl_free(p, false);
Linus Walleij89216492012-12-11 14:14:32 +01001095 return ERR_PTR(ret);
1096 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001097
Linus Walleijab780292013-01-22 10:56:14 -07001098 kref_init(&p->users);
1099
Linus Walleijb0666ba2012-12-11 13:12:35 +01001100 /* Add the pinctrl handle to the global list */
Stanislaw Gruszka7b320cb2014-02-04 09:07:09 +01001101 mutex_lock(&pinctrl_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -07001102 list_add_tail(&p->node, &pinctrl_list);
Stanislaw Gruszka7b320cb2014-02-04 09:07:09 +01001103 mutex_unlock(&pinctrl_list_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001104
1105 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001106}
Stephen Warren7ecdb162012-03-02 13:05:45 -07001107
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001108/**
1109 * pinctrl_get() - retrieves the pinctrl handle for a device
1110 * @dev: the device to obtain the handle for
1111 */
1112struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warren6e5e9592012-03-02 13:05:47 -07001113{
1114 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001115
Stephen Warren6e5e9592012-03-02 13:05:47 -07001116 if (WARN_ON(!dev))
1117 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -07001118
Linus Walleijab780292013-01-22 10:56:14 -07001119 /*
1120 * See if somebody else (such as the device core) has already
1121 * obtained a handle to the pinctrl for this device. In that case,
1122 * return another pointer to it.
1123 */
Stephen Warren6e5e9592012-03-02 13:05:47 -07001124 p = find_pinctrl(dev);
Markus Elfringcea234e2017-05-02 11:04:55 +02001125 if (p) {
Linus Walleijab780292013-01-22 10:56:14 -07001126 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
1127 kref_get(&p->users);
1128 return p;
1129 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001130
Tony Lindgren99e4f672016-12-27 09:19:59 -08001131 return create_pinctrl(dev, NULL);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001132}
1133EXPORT_SYMBOL_GPL(pinctrl_get);
1134
Richard Genoudd3cee8302013-03-25 15:47:21 +01001135static void pinctrl_free_setting(bool disable_setting,
1136 struct pinctrl_setting *setting)
1137{
1138 switch (setting->type) {
1139 case PIN_MAP_TYPE_MUX_GROUP:
1140 if (disable_setting)
1141 pinmux_disable_setting(setting);
1142 pinmux_free_setting(setting);
1143 break;
1144 case PIN_MAP_TYPE_CONFIGS_PIN:
1145 case PIN_MAP_TYPE_CONFIGS_GROUP:
1146 pinconf_free_setting(setting);
1147 break;
1148 default:
1149 break;
1150 }
1151}
1152
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001153static void pinctrl_free(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -07001154{
Stephen Warren6e5e9592012-03-02 13:05:47 -07001155 struct pinctrl_state *state, *n1;
1156 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001157
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001158 mutex_lock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001159 list_for_each_entry_safe(state, n1, &p->states, node) {
1160 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Richard Genoudd3cee8302013-03-25 15:47:21 +01001161 pinctrl_free_setting(state == p->state, setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001162 list_del(&setting->node);
1163 kfree(setting);
1164 }
1165 list_del(&state->node);
1166 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -07001167 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001168
Stephen Warren57291ce2012-03-23 10:29:46 -06001169 pinctrl_dt_free_maps(p);
1170
Stephen Warren6e5e9592012-03-02 13:05:47 -07001171 if (inlist)
1172 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -07001173 kfree(p);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001174 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001175}
1176
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001177/**
Linus Walleijab780292013-01-22 10:56:14 -07001178 * pinctrl_release() - release the pinctrl handle
1179 * @kref: the kref in the pinctrl being released
1180 */
Sachin Kamat2917e832013-01-24 15:01:00 +05301181static void pinctrl_release(struct kref *kref)
Linus Walleijab780292013-01-22 10:56:14 -07001182{
1183 struct pinctrl *p = container_of(kref, struct pinctrl, users);
1184
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001185 pinctrl_free(p, true);
Linus Walleijab780292013-01-22 10:56:14 -07001186}
1187
1188/**
1189 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
Stephen Warren6e5e9592012-03-02 13:05:47 -07001190 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001191 */
1192void pinctrl_put(struct pinctrl *p)
1193{
Linus Walleijab780292013-01-22 10:56:14 -07001194 kref_put(&p->users, pinctrl_release);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001195}
1196EXPORT_SYMBOL_GPL(pinctrl_put);
1197
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001198/**
1199 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
1200 * @p: the pinctrl handle to retrieve the state from
1201 * @name: the state name to retrieve
1202 */
1203struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
1204 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -07001205{
Stephen Warren6e5e9592012-03-02 13:05:47 -07001206 struct pinctrl_state *state;
1207
1208 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +08001209 if (!state) {
1210 if (pinctrl_dummy_state) {
1211 /* create dummy state */
1212 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
1213 name);
1214 state = create_state(p, name);
Richard Genoudd599bfb2012-08-10 16:53:49 +02001215 } else
1216 state = ERR_PTR(-ENODEV);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +08001217 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001218
1219 return state;
1220}
Stephen Warren6e5e9592012-03-02 13:05:47 -07001221EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
1222
Benjamin Gaignard036f3942019-05-22 17:29:24 +02001223static void pinctrl_link_add(struct pinctrl_dev *pctldev,
1224 struct device *consumer)
1225{
1226 if (pctldev->desc->link_consumers)
1227 device_link_add(consumer, pctldev->dev,
1228 DL_FLAG_PM_RUNTIME |
1229 DL_FLAG_AUTOREMOVE_CONSUMER);
1230}
1231
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001232/**
Florian Fainelli981ed1b2017-03-01 10:32:57 -08001233 * pinctrl_commit_state() - select/activate/program a pinctrl state to HW
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001234 * @p: the pinctrl handle for the device that requests configuration
1235 * @state: the state handle to select/activate/program
1236 */
Florian Fainelli981ed1b2017-03-01 10:32:57 -08001237static int pinctrl_commit_state(struct pinctrl *p, struct pinctrl_state *state)
Stephen Warren6e5e9592012-03-02 13:05:47 -07001238{
1239 struct pinctrl_setting *setting, *setting2;
Richard Genoud50cf7c82013-03-25 15:47:23 +01001240 struct pinctrl_state *old_state = p->state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001241 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -07001242
Stephen Warren6e5e9592012-03-02 13:05:47 -07001243 if (p->state) {
1244 /*
Fan Wu2243a872014-06-09 09:37:56 +08001245 * For each pinmux setting in the old state, forget SW's record
1246 * of mux owner for that pingroup. Any pingroups which are
1247 * still owned by the new state will be re-acquired by the call
1248 * to pinmux_enable_setting() in the loop below.
Stephen Warren6e5e9592012-03-02 13:05:47 -07001249 */
1250 list_for_each_entry(setting, &p->state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001251 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
1252 continue;
Fan Wu2243a872014-06-09 09:37:56 +08001253 pinmux_disable_setting(setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001254 }
1255 }
1256
Richard Genoud3102a762013-03-25 15:47:22 +01001257 p->state = NULL;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001258
1259 /* Apply all the settings for the new state */
1260 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001261 switch (setting->type) {
1262 case PIN_MAP_TYPE_MUX_GROUP:
1263 ret = pinmux_enable_setting(setting);
1264 break;
1265 case PIN_MAP_TYPE_CONFIGS_PIN:
1266 case PIN_MAP_TYPE_CONFIGS_GROUP:
1267 ret = pinconf_apply_setting(setting);
1268 break;
1269 default:
1270 ret = -EINVAL;
1271 break;
1272 }
Richard Genoud3102a762013-03-25 15:47:22 +01001273
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001274 if (ret < 0) {
Richard Genoud3102a762013-03-25 15:47:22 +01001275 goto unapply_new_state;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001276 }
Benjamin Gaignard036f3942019-05-22 17:29:24 +02001277
Linus Walleijb672a872019-05-24 00:11:43 +02001278 /* Do not link hogs (circular dependency) */
1279 if (p != setting->pctldev->p)
1280 pinctrl_link_add(setting->pctldev, p->dev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001281 }
1282
Richard Genoud3102a762013-03-25 15:47:22 +01001283 p->state = state;
1284
Stephen Warren7ecdb162012-03-02 13:05:45 -07001285 return 0;
Richard Genoud3102a762013-03-25 15:47:22 +01001286
1287unapply_new_state:
Richard Genoudda587512013-03-28 12:55:46 +01001288 dev_err(p->dev, "Error applying setting, reverse things back\n");
Richard Genoud3102a762013-03-25 15:47:22 +01001289
Richard Genoud3102a762013-03-25 15:47:22 +01001290 list_for_each_entry(setting2, &state->settings, node) {
1291 if (&setting2->node == &setting->node)
1292 break;
Richard Genoudaf606172013-03-29 10:03:26 +01001293 /*
1294 * All we can do here is pinmux_disable_setting.
1295 * That means that some pins are muxed differently now
1296 * than they were before applying the setting (We can't
1297 * "unmux a pin"!), but it's not a big deal since the pins
1298 * are free to be muxed by another apply_setting.
1299 */
1300 if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
1301 pinmux_disable_setting(setting2);
Richard Genoud3102a762013-03-25 15:47:22 +01001302 }
Richard Genoud8009d5f2013-03-28 12:55:47 +01001303
Richard Genoud385d9422013-03-29 10:03:27 +01001304 /* There's no infinite recursive loop here because p->state is NULL */
1305 if (old_state)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001306 pinctrl_select_state(p, old_state);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001307
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001308 return ret;
1309}
Florian Fainelli981ed1b2017-03-01 10:32:57 -08001310
1311/**
1312 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
1313 * @p: the pinctrl handle for the device that requests configuration
1314 * @state: the state handle to select/activate/program
1315 */
1316int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
1317{
1318 if (p->state == state)
1319 return 0;
1320
1321 return pinctrl_commit_state(p, state);
1322}
Stephen Warren6e5e9592012-03-02 13:05:47 -07001323EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001324
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001325static void devm_pinctrl_release(struct device *dev, void *res)
1326{
1327 pinctrl_put(*(struct pinctrl **)res);
1328}
1329
1330/**
1331 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
1332 * @dev: the device to obtain the handle for
1333 *
1334 * If there is a need to explicitly destroy the returned struct pinctrl,
1335 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1336 */
1337struct pinctrl *devm_pinctrl_get(struct device *dev)
1338{
1339 struct pinctrl **ptr, *p;
1340
1341 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
1342 if (!ptr)
1343 return ERR_PTR(-ENOMEM);
1344
1345 p = pinctrl_get(dev);
1346 if (!IS_ERR(p)) {
1347 *ptr = p;
1348 devres_add(dev, ptr);
1349 } else {
1350 devres_free(ptr);
1351 }
1352
1353 return p;
1354}
1355EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1356
1357static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1358{
1359 struct pinctrl **p = res;
1360
1361 return *p == data;
1362}
1363
1364/**
1365 * devm_pinctrl_put() - Resource managed pinctrl_put()
1366 * @p: the pinctrl handle to release
1367 *
1368 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1369 * this function will not need to be called and the resource management
1370 * code will ensure that the resource is freed.
1371 */
1372void devm_pinctrl_put(struct pinctrl *p)
1373{
Jingoo Hana72149e2013-02-26 11:34:07 +09001374 WARN_ON(devres_release(p->dev, devm_pinctrl_release,
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001375 devm_pinctrl_match, p));
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001376}
1377EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1378
Hans de Goedec72bed22019-12-16 21:51:18 +01001379/**
1380 * pinctrl_register_mappings() - register a set of pin controller mappings
1381 * @maps: the pincontrol mappings table to register. Note the pinctrl-core
1382 * keeps a reference to the passed in maps, so they should _not_ be
1383 * marked with __initdata.
1384 * @num_maps: the number of maps in the mapping table
1385 */
1386int pinctrl_register_mappings(const struct pinctrl_map *maps,
1387 unsigned num_maps)
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001388{
Stephen Warren1e2082b2012-03-02 13:05:48 -07001389 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001390 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001391
Masahiro Yamada7e9236f2015-05-28 21:52:59 +09001392 pr_debug("add %u pinctrl maps\n", num_maps);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001393
1394 /* First sanity check the new mapping */
1395 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001396 if (!maps[i].dev_name) {
1397 pr_err("failed to register map %s (%d): no device given\n",
1398 maps[i].name, i);
1399 return -EINVAL;
1400 }
1401
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001402 if (!maps[i].name) {
1403 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001404 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001405 return -EINVAL;
1406 }
1407
Stephen Warren1e2082b2012-03-02 13:05:48 -07001408 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1409 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001410 pr_err("failed to register map %s (%d): no pin control device given\n",
1411 maps[i].name, i);
1412 return -EINVAL;
1413 }
1414
Stephen Warren1e2082b2012-03-02 13:05:48 -07001415 switch (maps[i].type) {
1416 case PIN_MAP_TYPE_DUMMY_STATE:
1417 break;
1418 case PIN_MAP_TYPE_MUX_GROUP:
1419 ret = pinmux_validate_map(&maps[i], i);
1420 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001421 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001422 break;
1423 case PIN_MAP_TYPE_CONFIGS_PIN:
1424 case PIN_MAP_TYPE_CONFIGS_GROUP:
1425 ret = pinconf_validate_map(&maps[i], i);
1426 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001427 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001428 break;
1429 default:
1430 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001431 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -07001432 return -EINVAL;
1433 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001434 }
1435
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001436 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -08001437 if (!maps_node)
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001438 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001439
Hans de Goedec72bed22019-12-16 21:51:18 +01001440 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001441 maps_node->num_maps = num_maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001442
Doug Andersonc5272a22015-05-01 09:01:27 -07001443 mutex_lock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001444 list_add_tail(&maps_node->node, &pinctrl_maps);
Doug Andersonc5272a22015-05-01 09:01:27 -07001445 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001446
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001447 return 0;
1448}
Richard Fitzgerald8b1b2dc2018-02-23 13:43:52 +00001449EXPORT_SYMBOL_GPL(pinctrl_register_mappings);
Stephen Warren57291ce2012-03-23 10:29:46 -06001450
Hans de Goedec72bed22019-12-16 21:51:18 +01001451/**
1452 * pinctrl_unregister_mappings() - unregister a set of pin controller mappings
1453 * @maps: the pincontrol mappings table passed to pinctrl_register_mappings()
1454 * when registering the mappings.
1455 */
1456void pinctrl_unregister_mappings(const struct pinctrl_map *map)
Stephen Warren57291ce2012-03-23 10:29:46 -06001457{
1458 struct pinctrl_maps *maps_node;
1459
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001460 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001461 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1462 if (maps_node->maps == map) {
1463 list_del(&maps_node->node);
Linus Walleijdb6c2c62013-07-23 01:43:20 +02001464 kfree(maps_node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001465 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001466 return;
1467 }
1468 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001469 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001470}
Hans de Goedec72bed22019-12-16 21:51:18 +01001471EXPORT_SYMBOL_GPL(pinctrl_unregister_mappings);
Stephen Warren57291ce2012-03-23 10:29:46 -06001472
Julien Delacou840a47b2012-12-10 14:47:33 +01001473/**
1474 * pinctrl_force_sleep() - turn a given controller device into sleep state
1475 * @pctldev: pin controller device
1476 */
1477int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1478{
1479 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
Florian Fainelli981ed1b2017-03-01 10:32:57 -08001480 return pinctrl_commit_state(pctldev->p, pctldev->hog_sleep);
Julien Delacou840a47b2012-12-10 14:47:33 +01001481 return 0;
1482}
1483EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1484
1485/**
1486 * pinctrl_force_default() - turn a given controller device into default state
1487 * @pctldev: pin controller device
1488 */
1489int pinctrl_force_default(struct pinctrl_dev *pctldev)
1490{
1491 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
Florian Fainelli981ed1b2017-03-01 10:32:57 -08001492 return pinctrl_commit_state(pctldev->p, pctldev->hog_default);
Julien Delacou840a47b2012-12-10 14:47:33 +01001493 return 0;
1494}
1495EXPORT_SYMBOL_GPL(pinctrl_force_default);
1496
Douglas Andersonef0eebc2015-10-20 21:15:06 -07001497/**
1498 * pinctrl_init_done() - tell pinctrl probe is done
1499 *
1500 * We'll use this time to switch the pins from "init" to "default" unless the
1501 * driver selected some other state.
1502 *
1503 * @dev: device to that's done probing
1504 */
1505int pinctrl_init_done(struct device *dev)
1506{
1507 struct dev_pin_info *pins = dev->pins;
1508 int ret;
1509
1510 if (!pins)
1511 return 0;
1512
1513 if (IS_ERR(pins->init_state))
1514 return 0; /* No such state */
1515
1516 if (pins->p->state != pins->init_state)
1517 return 0; /* Not at init anyway */
1518
1519 if (IS_ERR(pins->default_state))
1520 return 0; /* No default state */
1521
1522 ret = pinctrl_select_state(pins->p, pins->default_state);
1523 if (ret)
1524 dev_err(dev, "failed to activate default pinctrl state\n");
1525
1526 return ret;
1527}
1528
Linus Walleij14005ee2013-06-05 15:30:33 +02001529#ifdef CONFIG_PM
1530
1531/**
Tony Lindgrenf3333492013-07-18 08:15:04 -07001532 * pinctrl_pm_select_state() - select pinctrl state for PM
1533 * @dev: device to select default state for
1534 * @state: state to set
1535 */
1536static int pinctrl_pm_select_state(struct device *dev,
1537 struct pinctrl_state *state)
1538{
1539 struct dev_pin_info *pins = dev->pins;
1540 int ret;
1541
1542 if (IS_ERR(state))
1543 return 0; /* No such state */
1544 ret = pinctrl_select_state(pins->p, state);
1545 if (ret)
1546 dev_err(dev, "failed to activate pinctrl state %s\n",
1547 state->name);
1548 return ret;
1549}
1550
1551/**
Linus Walleij14005ee2013-06-05 15:30:33 +02001552 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1553 * @dev: device to select default state for
1554 */
1555int pinctrl_pm_select_default_state(struct device *dev)
1556{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001557 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001558 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001559
1560 return pinctrl_pm_select_state(dev, dev->pins->default_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001561}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001562EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001563
1564/**
1565 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1566 * @dev: device to select sleep state for
1567 */
1568int pinctrl_pm_select_sleep_state(struct device *dev)
1569{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001570 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001571 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001572
1573 return pinctrl_pm_select_state(dev, dev->pins->sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001574}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001575EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001576
1577/**
1578 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1579 * @dev: device to select idle state for
1580 */
1581int pinctrl_pm_select_idle_state(struct device *dev)
1582{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001583 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001584 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001585
1586 return pinctrl_pm_select_state(dev, dev->pins->idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001587}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001588EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001589#endif
1590
Linus Walleij2744e8a2011-05-02 20:50:54 +02001591#ifdef CONFIG_DEBUG_FS
1592
1593static int pinctrl_pins_show(struct seq_file *s, void *what)
1594{
1595 struct pinctrl_dev *pctldev = s->private;
1596 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001597 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001598
1599 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001600
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001601 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001602
Chanho Park706e8522012-01-03 16:47:50 +09001603 /* The pin number can be retrived from the pin controller descriptor */
1604 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001605 struct pin_desc *desc;
1606
Chanho Park706e8522012-01-03 16:47:50 +09001607 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001608 desc = pin_desc_get(pctldev, pin);
1609 /* Pin space may be sparse */
Markus Elfringcea234e2017-05-02 11:04:55 +02001610 if (!desc)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001611 continue;
1612
Masahiro Yamadacf9d9942016-05-24 14:26:26 +09001613 seq_printf(s, "pin %d (%s) ", pin, desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001614
1615 /* Driver-specific info per pin */
1616 if (ops->pin_dbg_show)
1617 ops->pin_dbg_show(pctldev, s, pin);
1618
1619 seq_puts(s, "\n");
1620 }
1621
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001622 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001623
Linus Walleij2744e8a2011-05-02 20:50:54 +02001624 return 0;
1625}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001626DEFINE_SHOW_ATTRIBUTE(pinctrl_pins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001627
1628static int pinctrl_groups_show(struct seq_file *s, void *what)
1629{
1630 struct pinctrl_dev *pctldev = s->private;
1631 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301632 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001633
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001634 mutex_lock(&pctldev->mutex);
1635
Viresh Kumard1e90e92012-03-30 11:25:40 +05301636 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001637
Linus Walleij2744e8a2011-05-02 20:50:54 +02001638 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301639 while (selector < ngroups) {
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001640 const unsigned *pins = NULL;
1641 unsigned num_pins = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001642 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001643 const char *pname;
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001644 int ret = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001645 int i;
1646
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001647 if (ops->get_group_pins)
1648 ret = ops->get_group_pins(pctldev, selector,
1649 &pins, &num_pins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001650 if (ret)
1651 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1652 gname);
1653 else {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001654 seq_printf(s, "group: %s\n", gname);
1655 for (i = 0; i < num_pins; i++) {
1656 pname = pin_get_name(pctldev, pins[i]);
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001657 if (WARN_ON(!pname)) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001658 mutex_unlock(&pctldev->mutex);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001659 return -EINVAL;
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001660 }
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001661 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1662 }
1663 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001664 }
1665 selector++;
1666 }
1667
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001668 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001669
1670 return 0;
1671}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001672DEFINE_SHOW_ATTRIBUTE(pinctrl_groups);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001673
1674static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1675{
1676 struct pinctrl_dev *pctldev = s->private;
Masahiro Yamada6cadafb2019-06-09 23:55:37 +09001677 struct pinctrl_gpio_range *range;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001678
1679 seq_puts(s, "GPIO ranges handled:\n");
1680
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001681 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001682
Linus Walleij2744e8a2011-05-02 20:50:54 +02001683 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001684 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Christian Ruppertc8587ee2013-06-13 14:55:31 +02001685 if (range->pins) {
1686 int a;
1687 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
1688 range->id, range->name,
1689 range->base, (range->base + range->npins - 1));
1690 for (a = 0; a < range->npins - 1; a++)
1691 seq_printf(s, "%u, ", range->pins[a]);
1692 seq_printf(s, "%u}\n", range->pins[a]);
1693 }
1694 else
1695 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1696 range->id, range->name,
1697 range->base, (range->base + range->npins - 1),
1698 range->pin_base,
1699 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001700 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001701
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001702 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001703
1704 return 0;
1705}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001706DEFINE_SHOW_ATTRIBUTE(pinctrl_gpioranges);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001707
1708static int pinctrl_devices_show(struct seq_file *s, void *what)
1709{
1710 struct pinctrl_dev *pctldev;
1711
Linus Walleijae6b4d82011-10-19 18:14:33 +02001712 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001713
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001714 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001715
Linus Walleij2744e8a2011-05-02 20:50:54 +02001716 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1717 seq_printf(s, "%s ", pctldev->desc->name);
1718 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001719 seq_puts(s, "yes ");
1720 else
1721 seq_puts(s, "no ");
1722 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001723 seq_puts(s, "yes");
1724 else
1725 seq_puts(s, "no");
1726 seq_puts(s, "\n");
1727 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001728
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001729 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001730
1731 return 0;
1732}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001733DEFINE_SHOW_ATTRIBUTE(pinctrl_devices);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001734
Stephen Warren1e2082b2012-03-02 13:05:48 -07001735static inline const char *map_type(enum pinctrl_map_type type)
1736{
1737 static const char * const names[] = {
1738 "INVALID",
1739 "DUMMY_STATE",
1740 "MUX_GROUP",
1741 "CONFIGS_PIN",
1742 "CONFIGS_GROUP",
1743 };
1744
1745 if (type >= ARRAY_SIZE(names))
1746 return "UNKNOWN";
1747
1748 return names[type];
1749}
1750
Stephen Warren3eedb432012-02-23 17:04:40 -07001751static int pinctrl_maps_show(struct seq_file *s, void *what)
1752{
1753 struct pinctrl_maps *maps_node;
1754 int i;
Masahiro Yamada3f713b72017-08-04 11:22:31 +09001755 const struct pinctrl_map *map;
Stephen Warren3eedb432012-02-23 17:04:40 -07001756
1757 seq_puts(s, "Pinctrl maps:\n");
1758
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001759 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001760 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001761 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1762 map->dev_name, map->name, map_type(map->type),
1763 map->type);
1764
1765 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1766 seq_printf(s, "controlling device %s\n",
1767 map->ctrl_dev_name);
1768
1769 switch (map->type) {
1770 case PIN_MAP_TYPE_MUX_GROUP:
1771 pinmux_show_map(s, map);
1772 break;
1773 case PIN_MAP_TYPE_CONFIGS_PIN:
1774 case PIN_MAP_TYPE_CONFIGS_GROUP:
1775 pinconf_show_map(s, map);
1776 break;
1777 default:
1778 break;
1779 }
1780
Markus Elfring390e1042017-05-02 10:47:35 +02001781 seq_putc(s, '\n');
Stephen Warren3eedb432012-02-23 17:04:40 -07001782 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001783 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001784
1785 return 0;
1786}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001787DEFINE_SHOW_ATTRIBUTE(pinctrl_maps);
Stephen Warren3eedb432012-02-23 17:04:40 -07001788
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001789static int pinctrl_show(struct seq_file *s, void *what)
1790{
1791 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001792 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001793 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001794
1795 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001796
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001797 mutex_lock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001798
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001799 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001800 seq_printf(s, "device: %s current state: %s\n",
1801 dev_name(p->dev),
1802 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001803
Stephen Warren6e5e9592012-03-02 13:05:47 -07001804 list_for_each_entry(state, &p->states, node) {
1805 seq_printf(s, " state: %s\n", state->name);
1806
1807 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001808 struct pinctrl_dev *pctldev = setting->pctldev;
1809
1810 seq_printf(s, " type: %s controller %s ",
1811 map_type(setting->type),
1812 pinctrl_dev_get_name(pctldev));
1813
1814 switch (setting->type) {
1815 case PIN_MAP_TYPE_MUX_GROUP:
1816 pinmux_show_setting(s, setting);
1817 break;
1818 case PIN_MAP_TYPE_CONFIGS_PIN:
1819 case PIN_MAP_TYPE_CONFIGS_GROUP:
1820 pinconf_show_setting(s, setting);
1821 break;
1822 default:
1823 break;
1824 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001825 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001826 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001827 }
1828
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001829 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001830
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001831 return 0;
1832}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001833DEFINE_SHOW_ATTRIBUTE(pinctrl);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001834
Linus Walleij2744e8a2011-05-02 20:50:54 +02001835static struct dentry *debugfs_root;
1836
1837static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1838{
Tony Lindgren02157162012-01-20 08:17:22 -08001839 struct dentry *device_root;
Jan Kundrát1781af52018-01-26 16:06:37 +01001840 const char *debugfs_name;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001841
Jan Kundrát1781af52018-01-26 16:06:37 +01001842 if (pctldev->desc->name &&
1843 strcmp(dev_name(pctldev->dev), pctldev->desc->name)) {
1844 debugfs_name = devm_kasprintf(pctldev->dev, GFP_KERNEL,
1845 "%s-%s", dev_name(pctldev->dev),
1846 pctldev->desc->name);
1847 if (!debugfs_name) {
1848 pr_warn("failed to determine debugfs dir name for %s\n",
1849 dev_name(pctldev->dev));
1850 return;
1851 }
1852 } else {
1853 debugfs_name = dev_name(pctldev->dev);
1854 }
1855
1856 device_root = debugfs_create_dir(debugfs_name, debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001857 pctldev->device_root = device_root;
1858
Linus Walleij2744e8a2011-05-02 20:50:54 +02001859 if (IS_ERR(device_root) || !device_root) {
1860 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001861 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001862 return;
1863 }
1864 debugfs_create_file("pins", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001865 device_root, pctldev, &pinctrl_pins_fops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001866 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001867 device_root, pctldev, &pinctrl_groups_fops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001868 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001869 device_root, pctldev, &pinctrl_gpioranges_fops);
Florian Vaussarde7f2a442014-02-05 07:51:22 +01001870 if (pctldev->desc->pmxops)
1871 pinmux_init_device_debugfs(device_root, pctldev);
1872 if (pctldev->desc->confops)
1873 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001874}
1875
Tony Lindgren02157162012-01-20 08:17:22 -08001876static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1877{
1878 debugfs_remove_recursive(pctldev->device_root);
1879}
1880
Linus Walleij2744e8a2011-05-02 20:50:54 +02001881static void pinctrl_init_debugfs(void)
1882{
1883 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1884 if (IS_ERR(debugfs_root) || !debugfs_root) {
1885 pr_warn("failed to create debugfs directory\n");
1886 debugfs_root = NULL;
1887 return;
1888 }
1889
1890 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001891 debugfs_root, NULL, &pinctrl_devices_fops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001892 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001893 debugfs_root, NULL, &pinctrl_maps_fops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001894 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001895 debugfs_root, NULL, &pinctrl_fops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001896}
1897
1898#else /* CONFIG_DEBUG_FS */
1899
1900static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1901{
1902}
1903
1904static void pinctrl_init_debugfs(void)
1905{
1906}
1907
Tony Lindgren02157162012-01-20 08:17:22 -08001908static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1909{
1910}
1911
Linus Walleij2744e8a2011-05-02 20:50:54 +02001912#endif
1913
Stephen Warrend26bc492012-03-16 14:54:25 -06001914static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1915{
1916 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1917
1918 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301919 !ops->get_groups_count ||
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001920 !ops->get_group_name)
Stephen Warrend26bc492012-03-16 14:54:25 -06001921 return -EINVAL;
1922
1923 return 0;
1924}
1925
Linus Walleij2744e8a2011-05-02 20:50:54 +02001926/**
Tony Lindgren950b0d92017-01-11 14:13:34 -08001927 * pinctrl_init_controller() - init a pin controller device
Linus Walleij2744e8a2011-05-02 20:50:54 +02001928 * @pctldesc: descriptor for this pin controller
1929 * @dev: parent device for this pin controller
1930 * @driver_data: private pin controller data for this pin controller
1931 */
Andy Shevchenko0ca49212017-03-27 14:37:09 +03001932static struct pinctrl_dev *
1933pinctrl_init_controller(struct pinctrl_desc *pctldesc, struct device *dev,
1934 void *driver_data)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001935{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001936 struct pinctrl_dev *pctldev;
1937 int ret;
1938
Devendra Nagada9aecb2012-06-16 23:43:16 +05301939 if (!pctldesc)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001940 return ERR_PTR(-EINVAL);
Devendra Nagada9aecb2012-06-16 23:43:16 +05301941 if (!pctldesc->name)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001942 return ERR_PTR(-EINVAL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001943
Stephen Warren02f5b982012-02-22 14:26:00 -07001944 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -08001945 if (!pctldev)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001946 return ERR_PTR(-ENOMEM);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001947
1948 /* Initialize pin control device struct */
1949 pctldev->owner = pctldesc->owner;
1950 pctldev->desc = pctldesc;
1951 pctldev->driver_data = driver_data;
1952 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleijc033a712016-12-30 15:04:43 +01001953#ifdef CONFIG_GENERIC_PINCTRL_GROUPS
Tony Lindgrenc7059c52016-12-27 09:20:00 -08001954 INIT_RADIX_TREE(&pctldev->pin_group_tree, GFP_KERNEL);
Linus Walleijc033a712016-12-30 15:04:43 +01001955#endif
Tony Lindgrena76edc82016-12-27 09:20:01 -08001956#ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
1957 INIT_RADIX_TREE(&pctldev->pin_function_tree, GFP_KERNEL);
1958#endif
Linus Walleij2744e8a2011-05-02 20:50:54 +02001959 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Thierry Reding46daed62017-01-12 17:03:34 +01001960 INIT_LIST_HEAD(&pctldev->node);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001961 pctldev->dev = dev;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001962 mutex_init(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001963
Stephen Warrend26bc492012-03-16 14:54:25 -06001964 /* check core ops for sanity */
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001965 ret = pinctrl_check_ops(pctldev);
1966 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001967 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06001968 goto out_err;
1969 }
1970
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001971 /* If we're implementing pinmuxing, check the ops for sanity */
1972 if (pctldesc->pmxops) {
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001973 ret = pinmux_check_ops(pctldev);
1974 if (ret)
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001975 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001976 }
1977
1978 /* If we're implementing pinconfig, check the ops for sanity */
1979 if (pctldesc->confops) {
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001980 ret = pinconf_check_ops(pctldev);
1981 if (ret)
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001982 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001983 }
1984
Linus Walleij2744e8a2011-05-02 20:50:54 +02001985 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02001986 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001987 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1988 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001989 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001990 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1991 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001992 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001993 }
1994
Linus Walleij2744e8a2011-05-02 20:50:54 +02001995 return pctldev;
1996
Stephen Warren51cd24e2011-12-09 16:59:05 -07001997out_err:
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001998 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001999 kfree(pctldev);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09002000 return ERR_PTR(ret);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002001}
Tony Lindgren950b0d92017-01-11 14:13:34 -08002002
Tony Lindgren61187142017-03-30 09:16:39 -07002003static int pinctrl_claim_hogs(struct pinctrl_dev *pctldev)
Tony Lindgren950b0d92017-01-11 14:13:34 -08002004{
2005 pctldev->p = create_pinctrl(pctldev->dev, pctldev);
Tony Lindgren61187142017-03-30 09:16:39 -07002006 if (PTR_ERR(pctldev->p) == -ENODEV) {
2007 dev_dbg(pctldev->dev, "no hogs found\n");
Tony Lindgren950b0d92017-01-11 14:13:34 -08002008
Tony Lindgren61187142017-03-30 09:16:39 -07002009 return 0;
2010 }
2011
2012 if (IS_ERR(pctldev->p)) {
2013 dev_err(pctldev->dev, "error claiming hogs: %li\n",
2014 PTR_ERR(pctldev->p));
2015
2016 return PTR_ERR(pctldev->p);
2017 }
2018
2019 kref_get(&pctldev->p->users);
2020 pctldev->hog_default =
2021 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
2022 if (IS_ERR(pctldev->hog_default)) {
2023 dev_dbg(pctldev->dev,
2024 "failed to lookup the default state\n");
2025 } else {
2026 if (pinctrl_select_state(pctldev->p,
2027 pctldev->hog_default))
2028 dev_err(pctldev->dev,
2029 "failed to select default state\n");
2030 }
2031
2032 pctldev->hog_sleep =
2033 pinctrl_lookup_state(pctldev->p,
2034 PINCTRL_STATE_SLEEP);
2035 if (IS_ERR(pctldev->hog_sleep))
2036 dev_dbg(pctldev->dev,
2037 "failed to lookup the sleep state\n");
2038
2039 return 0;
2040}
2041
2042int pinctrl_enable(struct pinctrl_dev *pctldev)
2043{
2044 int error;
2045
2046 error = pinctrl_claim_hogs(pctldev);
2047 if (error) {
2048 dev_err(pctldev->dev, "could not claim hogs: %i\n",
2049 error);
2050 mutex_destroy(&pctldev->mutex);
2051 kfree(pctldev);
2052
2053 return error;
Tony Lindgren950b0d92017-01-11 14:13:34 -08002054 }
2055
2056 mutex_lock(&pinctrldev_list_mutex);
2057 list_add_tail(&pctldev->node, &pinctrldev_list);
2058 mutex_unlock(&pinctrldev_list_mutex);
2059
2060 pinctrl_init_device_debugfs(pctldev);
2061
2062 return 0;
2063}
Tony Lindgren61187142017-03-30 09:16:39 -07002064EXPORT_SYMBOL_GPL(pinctrl_enable);
Tony Lindgren950b0d92017-01-11 14:13:34 -08002065
2066/**
2067 * pinctrl_register() - register a pin controller device
2068 * @pctldesc: descriptor for this pin controller
2069 * @dev: parent device for this pin controller
2070 * @driver_data: private pin controller data for this pin controller
2071 *
2072 * Note that pinctrl_register() is known to have problems as the pin
2073 * controller driver functions are called before the driver has a
2074 * struct pinctrl_dev handle. To avoid issues later on, please use the
2075 * new pinctrl_register_and_init() below instead.
2076 */
2077struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
2078 struct device *dev, void *driver_data)
2079{
2080 struct pinctrl_dev *pctldev;
2081 int error;
2082
2083 pctldev = pinctrl_init_controller(pctldesc, dev, driver_data);
2084 if (IS_ERR(pctldev))
2085 return pctldev;
2086
Tony Lindgren61187142017-03-30 09:16:39 -07002087 error = pinctrl_enable(pctldev);
2088 if (error)
Tony Lindgren950b0d92017-01-11 14:13:34 -08002089 return ERR_PTR(error);
Tony Lindgren950b0d92017-01-11 14:13:34 -08002090
2091 return pctldev;
2092
2093}
Linus Walleij2744e8a2011-05-02 20:50:54 +02002094EXPORT_SYMBOL_GPL(pinctrl_register);
2095
Tony Lindgren61187142017-03-30 09:16:39 -07002096/**
2097 * pinctrl_register_and_init() - register and init pin controller device
2098 * @pctldesc: descriptor for this pin controller
2099 * @dev: parent device for this pin controller
2100 * @driver_data: private pin controller data for this pin controller
2101 * @pctldev: pin controller device
2102 *
2103 * Note that pinctrl_enable() still needs to be manually called after
2104 * this once the driver is ready.
2105 */
Tony Lindgren950b0d92017-01-11 14:13:34 -08002106int pinctrl_register_and_init(struct pinctrl_desc *pctldesc,
2107 struct device *dev, void *driver_data,
2108 struct pinctrl_dev **pctldev)
2109{
2110 struct pinctrl_dev *p;
Tony Lindgren950b0d92017-01-11 14:13:34 -08002111
2112 p = pinctrl_init_controller(pctldesc, dev, driver_data);
2113 if (IS_ERR(p))
2114 return PTR_ERR(p);
2115
2116 /*
2117 * We have pinctrl_start() call functions in the pin controller
2118 * driver with create_pinctrl() for at least dt_node_to_map(). So
2119 * let's make sure pctldev is properly initialized for the
2120 * pin controller driver before we do anything.
2121 */
2122 *pctldev = p;
2123
Tony Lindgren950b0d92017-01-11 14:13:34 -08002124 return 0;
2125}
2126EXPORT_SYMBOL_GPL(pinctrl_register_and_init);
2127
Linus Walleij2744e8a2011-05-02 20:50:54 +02002128/**
2129 * pinctrl_unregister() - unregister pinmux
2130 * @pctldev: pin controller to unregister
2131 *
2132 * Called by pinmux drivers to unregister a pinmux.
2133 */
2134void pinctrl_unregister(struct pinctrl_dev *pctldev)
2135{
Dong Aisheng5d589b02012-05-23 21:22:40 +08002136 struct pinctrl_gpio_range *range, *n;
Jon Hunter3429fb32017-01-05 15:52:55 +00002137
Markus Elfringcea234e2017-05-02 11:04:55 +02002138 if (!pctldev)
Linus Walleij2744e8a2011-05-02 20:50:54 +02002139 return;
2140
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002141 mutex_lock(&pctldev->mutex);
Tony Lindgren02157162012-01-20 08:17:22 -08002142 pinctrl_remove_device_debugfs(pctldev);
Jim Lindb93fac2015-01-08 20:25:05 +08002143 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07002144
Jon Hunter3429fb32017-01-05 15:52:55 +00002145 if (!IS_ERR_OR_NULL(pctldev->p))
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002146 pinctrl_put(pctldev->p);
Stephen Warren57b676f2012-03-02 13:05:44 -07002147
Jim Lindb93fac2015-01-08 20:25:05 +08002148 mutex_lock(&pinctrldev_list_mutex);
2149 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002150 /* TODO: check that no pinmuxes are still active? */
Thierry Reding46daed62017-01-12 17:03:34 +01002151 list_del(&pctldev->node);
Tony Lindgrena76edc82016-12-27 09:20:01 -08002152 pinmux_generic_free_functions(pctldev);
Tony Lindgrenc7059c52016-12-27 09:20:00 -08002153 pinctrl_generic_free_groups(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002154 /* Destroy descriptor tree */
2155 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
2156 pctldev->desc->npins);
Dong Aisheng5d589b02012-05-23 21:22:40 +08002157 /* remove gpio ranges map */
2158 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
2159 list_del(&range->node);
2160
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002161 mutex_unlock(&pctldev->mutex);
2162 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07002163 kfree(pctldev);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002164 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002165}
2166EXPORT_SYMBOL_GPL(pinctrl_unregister);
2167
Laxman Dewangan80e0f8d2016-02-24 14:12:59 +05302168static void devm_pinctrl_dev_release(struct device *dev, void *res)
2169{
2170 struct pinctrl_dev *pctldev = *(struct pinctrl_dev **)res;
2171
2172 pinctrl_unregister(pctldev);
2173}
2174
2175static int devm_pinctrl_dev_match(struct device *dev, void *res, void *data)
2176{
2177 struct pctldev **r = res;
2178
Laxman Dewangan3024f922016-02-24 14:44:07 +05302179 if (WARN_ON(!r || !*r))
Laxman Dewangan80e0f8d2016-02-24 14:12:59 +05302180 return 0;
2181
2182 return *r == data;
2183}
2184
2185/**
2186 * devm_pinctrl_register() - Resource managed version of pinctrl_register().
2187 * @dev: parent device for this pin controller
2188 * @pctldesc: descriptor for this pin controller
2189 * @driver_data: private pin controller data for this pin controller
2190 *
2191 * Returns an error pointer if pincontrol register failed. Otherwise
2192 * it returns valid pinctrl handle.
2193 *
2194 * The pinctrl device will be automatically released when the device is unbound.
2195 */
2196struct pinctrl_dev *devm_pinctrl_register(struct device *dev,
2197 struct pinctrl_desc *pctldesc,
2198 void *driver_data)
2199{
2200 struct pinctrl_dev **ptr, *pctldev;
2201
2202 ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
2203 if (!ptr)
2204 return ERR_PTR(-ENOMEM);
2205
2206 pctldev = pinctrl_register(pctldesc, dev, driver_data);
2207 if (IS_ERR(pctldev)) {
2208 devres_free(ptr);
2209 return pctldev;
2210 }
2211
2212 *ptr = pctldev;
2213 devres_add(dev, ptr);
2214
2215 return pctldev;
2216}
2217EXPORT_SYMBOL_GPL(devm_pinctrl_register);
2218
2219/**
Tony Lindgren950b0d92017-01-11 14:13:34 -08002220 * devm_pinctrl_register_and_init() - Resource managed pinctrl register and init
2221 * @dev: parent device for this pin controller
2222 * @pctldesc: descriptor for this pin controller
2223 * @driver_data: private pin controller data for this pin controller
2224 *
2225 * Returns an error pointer if pincontrol register failed. Otherwise
2226 * it returns valid pinctrl handle.
2227 *
2228 * The pinctrl device will be automatically released when the device is unbound.
2229 */
2230int devm_pinctrl_register_and_init(struct device *dev,
2231 struct pinctrl_desc *pctldesc,
2232 void *driver_data,
2233 struct pinctrl_dev **pctldev)
2234{
2235 struct pinctrl_dev **ptr;
2236 int error;
2237
2238 ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
2239 if (!ptr)
2240 return -ENOMEM;
2241
2242 error = pinctrl_register_and_init(pctldesc, dev, driver_data, pctldev);
2243 if (error) {
2244 devres_free(ptr);
2245 return error;
2246 }
2247
2248 *ptr = *pctldev;
2249 devres_add(dev, ptr);
2250
2251 return 0;
2252}
2253EXPORT_SYMBOL_GPL(devm_pinctrl_register_and_init);
2254
2255/**
Laxman Dewangan80e0f8d2016-02-24 14:12:59 +05302256 * devm_pinctrl_unregister() - Resource managed version of pinctrl_unregister().
2257 * @dev: device for which which resource was allocated
2258 * @pctldev: the pinctrl device to unregister.
2259 */
2260void devm_pinctrl_unregister(struct device *dev, struct pinctrl_dev *pctldev)
2261{
2262 WARN_ON(devres_release(dev, devm_pinctrl_dev_release,
2263 devm_pinctrl_dev_match, pctldev));
2264}
2265EXPORT_SYMBOL_GPL(devm_pinctrl_unregister);
2266
Linus Walleij2744e8a2011-05-02 20:50:54 +02002267static int __init pinctrl_init(void)
2268{
2269 pr_info("initialized pinctrl subsystem\n");
2270 pinctrl_init_debugfs();
2271 return 0;
2272}
2273
2274/* init early since many drivers really need to initialized pinmux early */
2275core_initcall(pinctrl_init);