blob: 9fc4433fece4f0fb1941c278dead2e53d480fb11 [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
Drew Fustinif1b206c2020-07-22 14:27:52 +020030#include "../gpio/gpiolib.h"
Haojian Zhuang51e13c22013-02-17 19:42:50 +080031#include <asm-generic/gpio.h>
Haojian Zhuang2afe8222013-03-28 07:34:19 +080032#endif
33
Linus Walleij2744e8a2011-05-02 20:50:54 +020034#include "core.h"
Stephen Warren57291ce2012-03-23 10:29:46 -060035#include "devicetree.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020036#include "pinmux.h"
Linus Walleijae6b4d82011-10-19 18:14:33 +020037#include "pinconf.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020038
Stephen Warrenb2b3e662012-02-19 23:45:43 -070039
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080040static bool pinctrl_dummy_state;
41
Patrice Chotard42fed7b2013-04-11 11:01:27 +020042/* Mutex taken to protect pinctrl_list */
Sachin Kamat843aec92013-06-18 14:34:27 +053043static DEFINE_MUTEX(pinctrl_list_mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +020044
45/* Mutex taken to protect pinctrl_maps */
46DEFINE_MUTEX(pinctrl_maps_mutex);
47
48/* Mutex taken to protect pinctrldev_list */
Sachin Kamat843aec92013-06-18 14:34:27 +053049static DEFINE_MUTEX(pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -070050
51/* Global list of pin control devices (struct pinctrl_dev) */
Patrice Chotard42fed7b2013-04-11 11:01:27 +020052static LIST_HEAD(pinctrldev_list);
Linus Walleij2744e8a2011-05-02 20:50:54 +020053
Stephen Warren57b676f2012-03-02 13:05:44 -070054/* List of pin controller handles (struct pinctrl) */
Linus Walleijbefe5bd2012-02-09 19:47:48 +010055static LIST_HEAD(pinctrl_list);
56
Stephen Warren57b676f2012-03-02 13:05:44 -070057/* List of pinctrl maps (struct pinctrl_maps) */
Laurent Meunier6f9e41f2013-02-06 09:09:44 +010058LIST_HEAD(pinctrl_maps);
Stephen Warrenb2b3e662012-02-19 23:45:43 -070059
Linus Walleijbefe5bd2012-02-09 19:47:48 +010060
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080061/**
62 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
63 *
64 * Usually this function is called by platforms without pinctrl driver support
65 * but run with some shared drivers using pinctrl APIs.
66 * After calling this function, the pinctrl core will return successfully
67 * with creating a dummy state for the driver to keep going smoothly.
68 */
69void pinctrl_provide_dummies(void)
70{
71 pinctrl_dummy_state = true;
72}
73
Linus Walleij2744e8a2011-05-02 20:50:54 +020074const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
75{
76 /* We're not allowed to register devices without name */
77 return pctldev->desc->name;
78}
79EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
80
Haojian Zhuangd6e99ab2013-01-18 15:31:06 +080081const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
82{
83 return dev_name(pctldev->dev);
84}
85EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
86
Linus Walleij2744e8a2011-05-02 20:50:54 +020087void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
88{
89 return pctldev->driver_data;
90}
91EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
92
93/**
Linus Walleij9dfac4f2012-02-01 18:02:47 +010094 * get_pinctrl_dev_from_devname() - look up pin controller device
95 * @devname: the name of a device instance, as returned by dev_name()
Linus Walleij2744e8a2011-05-02 20:50:54 +020096 *
97 * Looks up a pin control device matching a certain device name or pure device
98 * pointer, the pure device pointer will take precedence.
99 */
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100100struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200101{
Masahiro Yamada6cadafb2019-06-09 23:55:37 +0900102 struct pinctrl_dev *pctldev;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200103
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100104 if (!devname)
105 return NULL;
106
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200107 mutex_lock(&pinctrldev_list_mutex);
108
Linus Walleij2744e8a2011-05-02 20:50:54 +0200109 list_for_each_entry(pctldev, &pinctrldev_list, node) {
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100110 if (!strcmp(dev_name(pctldev->dev), devname)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200111 /* Matched on device name */
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200112 mutex_unlock(&pinctrldev_list_mutex);
113 return pctldev;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200114 }
115 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200116
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200117 mutex_unlock(&pinctrldev_list_mutex);
118
119 return NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200120}
121
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200122struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np)
123{
124 struct pinctrl_dev *pctldev;
125
126 mutex_lock(&pinctrldev_list_mutex);
127
128 list_for_each_entry(pctldev, &pinctrldev_list, node)
129 if (pctldev->dev->of_node == np) {
130 mutex_unlock(&pinctrldev_list_mutex);
131 return pctldev;
132 }
133
Daniel Mackd463f822013-04-26 18:57:02 +0200134 mutex_unlock(&pinctrldev_list_mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200135
136 return NULL;
137}
138
Linus Walleij2744e8a2011-05-02 20:50:54 +0200139/**
Linus Walleijae6b4d82011-10-19 18:14:33 +0200140 * pin_get_from_name() - look up a pin number from a name
141 * @pctldev: the pin control device to lookup the pin on
142 * @name: the name of the pin to look up
143 */
144int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
145{
Chanho Park706e8522012-01-03 16:47:50 +0900146 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200147
Chanho Park706e8522012-01-03 16:47:50 +0900148 /* The pin number can be retrived from the pin controller descriptor */
149 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200150 struct pin_desc *desc;
151
Chanho Park706e8522012-01-03 16:47:50 +0900152 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200153 desc = pin_desc_get(pctldev, pin);
154 /* Pin space may be sparse */
Axel Lin6c325f82013-08-19 10:06:29 +0800155 if (desc && !strcmp(name, desc->name))
Linus Walleijae6b4d82011-10-19 18:14:33 +0200156 return pin;
157 }
158
159 return -EINVAL;
160}
161
162/**
Dong Aishengdcb5dbc2012-04-17 15:00:46 +0800163 * pin_get_name_from_id() - look up a pin name from a pin id
164 * @pctldev: the pin control device to lookup the pin on
Lee Jones9c340bb2020-07-13 15:49:16 +0100165 * @pin: pin number/id to look up
Dong Aishengdcb5dbc2012-04-17 15:00:46 +0800166 */
167const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
168{
169 const struct pin_desc *desc;
170
171 desc = pin_desc_get(pctldev, pin);
Markus Elfringcea234e2017-05-02 11:04:55 +0200172 if (!desc) {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +0800173 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
174 pin);
175 return NULL;
176 }
177
178 return desc->name;
179}
Baolin Wangb88d1452020-02-27 12:13:46 +0800180EXPORT_SYMBOL_GPL(pin_get_name);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +0800181
Linus Walleij2744e8a2011-05-02 20:50:54 +0200182/* Deletes a range of pin descriptors */
183static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
184 const struct pinctrl_pin_desc *pins,
185 unsigned num_pins)
186{
187 int i;
188
Linus Walleij2744e8a2011-05-02 20:50:54 +0200189 for (i = 0; i < num_pins; i++) {
190 struct pin_desc *pindesc;
191
192 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
193 pins[i].number);
Markus Elfringcea234e2017-05-02 11:04:55 +0200194 if (pindesc) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200195 radix_tree_delete(&pctldev->pin_desc_tree,
196 pins[i].number);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100197 if (pindesc->dynamic_name)
198 kfree(pindesc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200199 }
200 kfree(pindesc);
201 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200202}
203
204static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900205 const struct pinctrl_pin_desc *pin)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200206{
207 struct pin_desc *pindesc;
208
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900209 pindesc = pin_desc_get(pctldev, pin->number);
Markus Elfringcea234e2017-05-02 11:04:55 +0200210 if (pindesc) {
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900211 dev_err(pctldev->dev, "pin %d already registered\n",
212 pin->number);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200213 return -EINVAL;
214 }
215
216 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -0800217 if (!pindesc)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200218 return -ENOMEM;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200219
Linus Walleij2744e8a2011-05-02 20:50:54 +0200220 /* Set owner */
221 pindesc->pctldev = pctldev;
222
Stephen Warren9af1e442011-10-19 16:19:27 -0600223 /* Copy basic pin info */
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900224 if (pin->name) {
225 pindesc->name = pin->name;
Linus Walleijca53c5f2011-12-14 20:33:37 +0100226 } else {
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900227 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", pin->number);
Markus Elfringcea234e2017-05-02 11:04:55 +0200228 if (!pindesc->name) {
Sachin Kamateb26cc92012-09-25 12:41:40 +0530229 kfree(pindesc);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100230 return -ENOMEM;
Sachin Kamateb26cc92012-09-25 12:41:40 +0530231 }
Linus Walleijca53c5f2011-12-14 20:33:37 +0100232 pindesc->dynamic_name = true;
233 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200234
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900235 pindesc->drv_data = pin->drv_data;
236
237 radix_tree_insert(&pctldev->pin_desc_tree, pin->number, pindesc);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200238 pr_debug("registered pin %d (%s) on %s\n",
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900239 pin->number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200240 return 0;
241}
242
243static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900244 const struct pinctrl_pin_desc *pins,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200245 unsigned num_descs)
246{
247 unsigned i;
248 int ret = 0;
249
250 for (i = 0; i < num_descs; i++) {
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900251 ret = pinctrl_register_one_pin(pctldev, &pins[i]);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200252 if (ret)
253 return ret;
254 }
255
256 return 0;
257}
258
259/**
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200260 * gpio_to_pin() - GPIO range GPIO number to pin number translation
261 * @range: GPIO range used for the translation
262 * @gpio: gpio pin to translate to a pin number
263 *
264 * Finds the pin number for a given GPIO using the specified GPIO range
265 * as a base for translation. The distinction between linear GPIO ranges
266 * and pin list based GPIO ranges is managed correctly by this function.
267 *
268 * This function assumes the gpio is part of the specified GPIO range, use
269 * only after making sure this is the case (e.g. by calling it on the
270 * result of successful pinctrl_get_device_gpio_range calls)!
271 */
272static inline int gpio_to_pin(struct pinctrl_gpio_range *range,
273 unsigned int gpio)
274{
275 unsigned int offset = gpio - range->base;
276 if (range->pins)
277 return range->pins[offset];
278 else
279 return range->pin_base + offset;
280}
281
282/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200283 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
284 * @pctldev: pin controller device to check
285 * @gpio: gpio pin to check taken from the global GPIO pin space
286 *
287 * Tries to match a GPIO pin number to the ranges handled by a certain pin
288 * controller, return the range or NULL
289 */
290static struct pinctrl_gpio_range *
291pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
292{
Masahiro Yamada6cadafb2019-06-09 23:55:37 +0900293 struct pinctrl_gpio_range *range;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200294
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200295 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200296 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200297 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
298 /* Check if we're in the valid range */
299 if (gpio >= range->base &&
300 gpio < range->base + range->npins) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200301 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200302 return range;
303 }
304 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200305 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200306 return NULL;
307}
308
309/**
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800310 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
311 * the same GPIO chip are in range
312 * @gpio: gpio pin to check taken from the global GPIO pin space
313 *
314 * This function is complement of pinctrl_match_gpio_range(). If the return
315 * value of pinctrl_match_gpio_range() is NULL, this function could be used
316 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
317 * of the same GPIO chip don't have back-end pinctrl interface.
318 * If the return value is true, it means that pinctrl device is ready & the
319 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
320 * is false, it means that pinctrl device may not be ready.
321 */
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800322#ifdef CONFIG_GPIOLIB
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800323static bool pinctrl_ready_for_gpio_range(unsigned gpio)
324{
325 struct pinctrl_dev *pctldev;
326 struct pinctrl_gpio_range *range = NULL;
327 struct gpio_chip *chip = gpio_to_chip(gpio);
328
Tony Lindgren942cde72015-09-03 10:34:30 -0700329 if (WARN(!chip, "no gpio_chip for gpio%i?", gpio))
330 return false;
331
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200332 mutex_lock(&pinctrldev_list_mutex);
333
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800334 /* Loop over the pin controllers */
335 list_for_each_entry(pctldev, &pinctrldev_list, node) {
336 /* Loop over the ranges */
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800337 mutex_lock(&pctldev->mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800338 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
339 /* Check if any gpio range overlapped with gpio chip */
340 if (range->base + range->npins - 1 < chip->base ||
341 range->base > chip->base + chip->ngpio - 1)
342 continue;
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800343 mutex_unlock(&pctldev->mutex);
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200344 mutex_unlock(&pinctrldev_list_mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800345 return true;
346 }
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800347 mutex_unlock(&pctldev->mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800348 }
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200349
350 mutex_unlock(&pinctrldev_list_mutex);
351
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800352 return false;
353}
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800354#else
355static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
356#endif
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800357
358/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200359 * pinctrl_get_device_gpio_range() - find device for GPIO range
360 * @gpio: the pin to locate the pin controller for
361 * @outdev: the pin control device if found
362 * @outrange: the GPIO range if found
363 *
364 * Find the pin controller handling a certain GPIO pin from the pinspace of
365 * the GPIO subsystem, return the device and the matching GPIO range. Returns
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800366 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
367 * may still have not been registered.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200368 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700369static int pinctrl_get_device_gpio_range(unsigned gpio,
370 struct pinctrl_dev **outdev,
371 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200372{
Masahiro Yamada6cadafb2019-06-09 23:55:37 +0900373 struct pinctrl_dev *pctldev;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200374
Axel Linf0059022013-08-18 20:34:22 +0800375 mutex_lock(&pinctrldev_list_mutex);
376
Linus Walleij2744e8a2011-05-02 20:50:54 +0200377 /* Loop over the pin controllers */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200378 list_for_each_entry(pctldev, &pinctrldev_list, node) {
379 struct pinctrl_gpio_range *range;
380
381 range = pinctrl_match_gpio_range(pctldev, gpio);
Markus Elfringcea234e2017-05-02 11:04:55 +0200382 if (range) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200383 *outdev = pctldev;
384 *outrange = range;
Axel Linf0059022013-08-18 20:34:22 +0800385 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200386 return 0;
387 }
388 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200389
Axel Linf0059022013-08-18 20:34:22 +0800390 mutex_unlock(&pinctrldev_list_mutex);
391
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800392 return -EPROBE_DEFER;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200393}
394
395/**
396 * pinctrl_add_gpio_range() - register a GPIO range for a controller
397 * @pctldev: pin controller device to add the range to
398 * @range: the GPIO range to add
399 *
400 * This adds a range of GPIOs to be handled by a certain pin controller. Call
401 * this to register handled ranges after registering your pin controller.
402 */
403void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
404 struct pinctrl_gpio_range *range)
405{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200406 mutex_lock(&pctldev->mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700407 list_add_tail(&range->node, &pctldev->gpio_ranges);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200408 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200409}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700410EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200411
Dong Aisheng3e5e00b2012-05-23 21:22:41 +0800412void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
413 struct pinctrl_gpio_range *ranges,
414 unsigned nranges)
415{
416 int i;
417
418 for (i = 0; i < nranges; i++)
419 pinctrl_add_gpio_range(pctldev, &ranges[i]);
420}
421EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
422
Linus Walleij192c3692012-11-20 14:03:37 +0100423struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530424 struct pinctrl_gpio_range *range)
425{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200426 struct pinctrl_dev *pctldev;
427
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200428 pctldev = get_pinctrl_dev_from_devname(devname);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530429
Linus Walleijdfa97512012-11-20 14:54:18 +0100430 /*
431 * If we can't find this device, let's assume that is because
432 * it has not probed yet, so the driver trying to register this
433 * range need to defer probing.
434 */
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200435 if (!pctldev) {
Linus Walleijdfa97512012-11-20 14:54:18 +0100436 return ERR_PTR(-EPROBE_DEFER);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200437 }
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530438 pinctrl_add_gpio_range(pctldev, range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200439
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530440 return pctldev;
441}
Linus Walleij192c3692012-11-20 14:03:37 +0100442EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530443
Christian Ruppert586a87e2013-10-15 15:37:54 +0200444int pinctrl_get_group_pins(struct pinctrl_dev *pctldev, const char *pin_group,
445 const unsigned **pins, unsigned *num_pins)
446{
447 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
448 int gs;
449
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +0200450 if (!pctlops->get_group_pins)
451 return -EINVAL;
452
Christian Ruppert586a87e2013-10-15 15:37:54 +0200453 gs = pinctrl_get_group_selector(pctldev, pin_group);
454 if (gs < 0)
455 return gs;
456
457 return pctlops->get_group_pins(pctldev, gs, pins, num_pins);
458}
459EXPORT_SYMBOL_GPL(pinctrl_get_group_pins);
460
Joachim Eastwoodb18537c2016-02-25 22:44:37 +0100461struct pinctrl_gpio_range *
462pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev *pctldev,
463 unsigned int pin)
464{
465 struct pinctrl_gpio_range *range;
466
467 /* Loop over the ranges */
468 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
469 /* Check if we're in the valid range */
470 if (range->pins) {
471 int a;
472 for (a = 0; a < range->npins; a++) {
473 if (range->pins[a] == pin)
474 return range;
475 }
476 } else if (pin >= range->pin_base &&
477 pin < range->pin_base + range->npins)
478 return range;
479 }
480
481 return NULL;
482}
483EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin_nolock);
484
Linus Walleij2744e8a2011-05-02 20:50:54 +0200485/**
Linus Walleij9afbefb2012-11-20 14:25:07 +0100486 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
487 * @pctldev: the pin controller device to look in
488 * @pin: a controller-local number to find the range for
489 */
490struct pinctrl_gpio_range *
491pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
492 unsigned int pin)
493{
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800494 struct pinctrl_gpio_range *range;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100495
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200496 mutex_lock(&pctldev->mutex);
Joachim Eastwoodb18537c2016-02-25 22:44:37 +0100497 range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200498 mutex_unlock(&pctldev->mutex);
Joachim Eastwoodb18537c2016-02-25 22:44:37 +0100499
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800500 return range;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100501}
502EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
503
504/**
Charles Keepax50842cb2017-02-13 10:11:03 +0000505 * pinctrl_remove_gpio_range() - remove a range of GPIOs from a pin controller
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530506 * @pctldev: pin controller device to remove the range from
507 * @range: the GPIO range to remove
508 */
509void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
510 struct pinctrl_gpio_range *range)
511{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200512 mutex_lock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530513 list_del(&range->node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200514 mutex_unlock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530515}
516EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
517
Linus Walleijc033a712016-12-30 15:04:43 +0100518#ifdef CONFIG_GENERIC_PINCTRL_GROUPS
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800519
520/**
521 * pinctrl_generic_get_group_count() - returns the number of pin groups
522 * @pctldev: pin controller device
523 */
524int pinctrl_generic_get_group_count(struct pinctrl_dev *pctldev)
525{
526 return pctldev->num_groups;
527}
528EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_count);
529
530/**
531 * pinctrl_generic_get_group_name() - returns the name of a pin group
532 * @pctldev: pin controller device
533 * @selector: group number
534 */
535const char *pinctrl_generic_get_group_name(struct pinctrl_dev *pctldev,
536 unsigned int selector)
537{
538 struct group_desc *group;
539
540 group = radix_tree_lookup(&pctldev->pin_group_tree,
541 selector);
542 if (!group)
543 return NULL;
544
545 return group->name;
546}
547EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_name);
548
549/**
550 * pinctrl_generic_get_group_pins() - gets the pin group pins
551 * @pctldev: pin controller device
552 * @selector: group number
553 * @pins: pins in the group
554 * @num_pins: number of pins in the group
555 */
556int pinctrl_generic_get_group_pins(struct pinctrl_dev *pctldev,
557 unsigned int selector,
558 const unsigned int **pins,
559 unsigned int *num_pins)
560{
561 struct group_desc *group;
562
563 group = radix_tree_lookup(&pctldev->pin_group_tree,
564 selector);
565 if (!group) {
566 dev_err(pctldev->dev, "%s could not find pingroup%i\n",
567 __func__, selector);
568 return -EINVAL;
569 }
570
571 *pins = group->pins;
572 *num_pins = group->num_pins;
573
574 return 0;
575}
576EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_pins);
577
578/**
579 * pinctrl_generic_get_group() - returns a pin group based on the number
580 * @pctldev: pin controller device
Lee Jones9c340bb2020-07-13 15:49:16 +0100581 * @selector: group number
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800582 */
583struct group_desc *pinctrl_generic_get_group(struct pinctrl_dev *pctldev,
584 unsigned int selector)
585{
586 struct group_desc *group;
587
588 group = radix_tree_lookup(&pctldev->pin_group_tree,
589 selector);
590 if (!group)
591 return NULL;
592
593 return group;
594}
595EXPORT_SYMBOL_GPL(pinctrl_generic_get_group);
596
Tony Lindgrena2037282018-07-05 02:10:14 -0700597static int pinctrl_generic_group_name_to_selector(struct pinctrl_dev *pctldev,
598 const char *function)
599{
600 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
601 int ngroups = ops->get_groups_count(pctldev);
602 int selector = 0;
603
604 /* See if this pctldev has this group */
605 while (selector < ngroups) {
606 const char *gname = ops->get_group_name(pctldev, selector);
607
Yanjiang Jin54a58182018-09-29 17:06:55 +0800608 if (gname && !strcmp(function, gname))
Tony Lindgrena2037282018-07-05 02:10:14 -0700609 return selector;
610
611 selector++;
612 }
613
614 return -EINVAL;
615}
616
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800617/**
618 * pinctrl_generic_add_group() - adds a new pin group
619 * @pctldev: pin controller device
620 * @name: name of the pin group
621 * @pins: pins in the pin group
622 * @num_pins: number of pins in the pin group
623 * @data: pin controller driver specific data
624 *
625 * Note that the caller must take care of locking.
626 */
627int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name,
628 int *pins, int num_pins, void *data)
629{
630 struct group_desc *group;
Tony Lindgrena2037282018-07-05 02:10:14 -0700631 int selector;
632
633 if (!name)
634 return -EINVAL;
635
636 selector = pinctrl_generic_group_name_to_selector(pctldev, name);
637 if (selector >= 0)
638 return selector;
639
640 selector = pctldev->num_groups;
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800641
642 group = devm_kzalloc(pctldev->dev, sizeof(*group), GFP_KERNEL);
643 if (!group)
644 return -ENOMEM;
645
646 group->name = name;
647 group->pins = pins;
648 group->num_pins = num_pins;
649 group->data = data;
650
Tony Lindgrena2037282018-07-05 02:10:14 -0700651 radix_tree_insert(&pctldev->pin_group_tree, selector, group);
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800652
653 pctldev->num_groups++;
654
Tony Lindgrena2037282018-07-05 02:10:14 -0700655 return selector;
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800656}
657EXPORT_SYMBOL_GPL(pinctrl_generic_add_group);
658
659/**
660 * pinctrl_generic_remove_group() - removes a numbered pin group
661 * @pctldev: pin controller device
662 * @selector: group number
663 *
664 * Note that the caller must take care of locking.
665 */
666int pinctrl_generic_remove_group(struct pinctrl_dev *pctldev,
667 unsigned int selector)
668{
669 struct group_desc *group;
670
671 group = radix_tree_lookup(&pctldev->pin_group_tree,
672 selector);
673 if (!group)
674 return -ENOENT;
675
676 radix_tree_delete(&pctldev->pin_group_tree, selector);
677 devm_kfree(pctldev->dev, group);
678
679 pctldev->num_groups--;
680
681 return 0;
682}
683EXPORT_SYMBOL_GPL(pinctrl_generic_remove_group);
684
685/**
686 * pinctrl_generic_free_groups() - removes all pin groups
687 * @pctldev: pin controller device
688 *
Tony Lindgren664b7c42017-05-12 08:47:57 -0700689 * Note that the caller must take care of locking. The pinctrl groups
690 * are allocated with devm_kzalloc() so no need to free them here.
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800691 */
692static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
693{
694 struct radix_tree_iter iter;
Masahiro Yamada906a2a32017-08-04 13:52:05 +0900695 void __rcu **slot;
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800696
697 radix_tree_for_each_slot(slot, &pctldev->pin_group_tree, &iter, 0)
Tony Lindgren664b7c42017-05-12 08:47:57 -0700698 radix_tree_delete(&pctldev->pin_group_tree, iter.index);
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800699
700 pctldev->num_groups = 0;
701}
702
703#else
704static inline void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
705{
706}
Linus Walleijc033a712016-12-30 15:04:43 +0100707#endif /* CONFIG_GENERIC_PINCTRL_GROUPS */
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800708
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530709/**
Linus Walleij7afde8b2011-10-19 17:07:16 +0200710 * pinctrl_get_group_selector() - returns the group selector for a group
711 * @pctldev: the pin controller handling the group
712 * @pin_group: the pin group to look up
713 */
714int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
715 const char *pin_group)
716{
717 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530718 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200719 unsigned group_selector = 0;
720
Viresh Kumard1e90e92012-03-30 11:25:40 +0530721 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200722 const char *gname = pctlops->get_group_name(pctldev,
723 group_selector);
Yanjiang Jin54a58182018-09-29 17:06:55 +0800724 if (gname && !strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700725 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200726 "found group selector %u for %s\n",
727 group_selector,
728 pin_group);
729 return group_selector;
730 }
731
732 group_selector++;
733 }
734
Stephen Warren51cd24e2011-12-09 16:59:05 -0700735 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200736 pin_group);
737
738 return -EINVAL;
739}
740
Stefan Wahren472a61e2019-08-14 14:00:35 +0300741bool pinctrl_gpio_can_use_line(unsigned gpio)
742{
743 struct pinctrl_dev *pctldev;
744 struct pinctrl_gpio_range *range;
745 bool result;
746 int pin;
747
748 /*
749 * Try to obtain GPIO range, if it fails
750 * we're probably dealing with GPIO driver
751 * without a backing pin controller - bail out.
752 */
753 if (pinctrl_get_device_gpio_range(gpio, &pctldev, &range))
754 return true;
755
756 mutex_lock(&pctldev->mutex);
757
758 /* Convert to the pin controllers number space */
759 pin = gpio_to_pin(range, gpio);
760
761 result = pinmux_can_be_used_for_gpio(pctldev, pin);
762
763 mutex_unlock(&pctldev->mutex);
764
765 return result;
766}
767EXPORT_SYMBOL_GPL(pinctrl_gpio_can_use_line);
768
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100769/**
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200770 * pinctrl_gpio_request() - request a single pin to be used as GPIO
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100771 * @gpio: the GPIO pin number from the GPIO subsystem number space
772 *
773 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
774 * as part of their gpio_request() semantics, platforms and individual drivers
775 * shall *NOT* request GPIO pins to be muxed in.
776 */
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200777int pinctrl_gpio_request(unsigned gpio)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100778{
779 struct pinctrl_dev *pctldev;
780 struct pinctrl_gpio_range *range;
781 int ret;
782 int pin;
783
784 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700785 if (ret) {
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800786 if (pinctrl_ready_for_gpio_range(gpio))
787 ret = 0;
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800788 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700789 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100790
Axel Lin9b77ace42013-08-19 10:07:46 +0800791 mutex_lock(&pctldev->mutex);
792
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100793 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200794 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100795
Stephen Warren57b676f2012-03-02 13:05:44 -0700796 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
797
Axel Lin9b77ace42013-08-19 10:07:46 +0800798 mutex_unlock(&pctldev->mutex);
799
Stephen Warren57b676f2012-03-02 13:05:44 -0700800 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100801}
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200802EXPORT_SYMBOL_GPL(pinctrl_gpio_request);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100803
804/**
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200805 * pinctrl_gpio_free() - free control on a single pin, currently used as GPIO
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100806 * @gpio: the GPIO pin number from the GPIO subsystem number space
807 *
808 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
809 * as part of their gpio_free() semantics, platforms and individual drivers
810 * shall *NOT* request GPIO pins to be muxed out.
811 */
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200812void pinctrl_gpio_free(unsigned gpio)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100813{
814 struct pinctrl_dev *pctldev;
815 struct pinctrl_gpio_range *range;
816 int ret;
817 int pin;
818
819 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700820 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100821 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700822 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200823 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100824
825 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200826 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100827
Stephen Warren57b676f2012-03-02 13:05:44 -0700828 pinmux_free_gpio(pctldev, pin, range);
829
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200830 mutex_unlock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100831}
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200832EXPORT_SYMBOL_GPL(pinctrl_gpio_free);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100833
834static int pinctrl_gpio_direction(unsigned gpio, bool input)
835{
836 struct pinctrl_dev *pctldev;
837 struct pinctrl_gpio_range *range;
838 int ret;
839 int pin;
840
841 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200842 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100843 return ret;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200844 }
845
846 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100847
848 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200849 pin = gpio_to_pin(range, gpio);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200850 ret = pinmux_gpio_direction(pctldev, range, pin, input);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100851
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200852 mutex_unlock(&pctldev->mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200853
854 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100855}
856
857/**
858 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
859 * @gpio: the GPIO pin number from the GPIO subsystem number space
860 *
861 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
862 * as part of their gpio_direction_input() semantics, platforms and individual
863 * drivers shall *NOT* touch pin control GPIO calls.
864 */
865int pinctrl_gpio_direction_input(unsigned gpio)
866{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200867 return pinctrl_gpio_direction(gpio, true);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100868}
869EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
870
871/**
872 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
873 * @gpio: the GPIO pin number from the GPIO subsystem number space
874 *
875 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
876 * as part of their gpio_direction_output() semantics, platforms and individual
877 * drivers shall *NOT* touch pin control GPIO calls.
878 */
879int pinctrl_gpio_direction_output(unsigned gpio)
880{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200881 return pinctrl_gpio_direction(gpio, false);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100882}
883EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
884
Mika Westerberg15381bc2017-01-23 15:34:33 +0300885/**
886 * pinctrl_gpio_set_config() - Apply config to given GPIO pin
887 * @gpio: the GPIO pin number from the GPIO subsystem number space
888 * @config: the configuration to apply to the GPIO
889 *
890 * This function should *ONLY* be used from gpiolib-based GPIO drivers, if
891 * they need to call the underlying pin controller to change GPIO config
892 * (for example set debounce time).
893 */
894int pinctrl_gpio_set_config(unsigned gpio, unsigned long config)
895{
896 unsigned long configs[] = { config };
897 struct pinctrl_gpio_range *range;
898 struct pinctrl_dev *pctldev;
899 int ret, pin;
900
901 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
902 if (ret)
903 return ret;
904
905 mutex_lock(&pctldev->mutex);
906 pin = gpio_to_pin(range, gpio);
907 ret = pinconf_set_config(pctldev, pin, configs, ARRAY_SIZE(configs));
908 mutex_unlock(&pctldev->mutex);
909
910 return ret;
911}
912EXPORT_SYMBOL_GPL(pinctrl_gpio_set_config);
913
Stephen Warren6e5e9592012-03-02 13:05:47 -0700914static struct pinctrl_state *find_state(struct pinctrl *p,
915 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100916{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700917 struct pinctrl_state *state;
918
919 list_for_each_entry(state, &p->states, node)
920 if (!strcmp(state->name, name))
921 return state;
922
923 return NULL;
924}
925
926static struct pinctrl_state *create_state(struct pinctrl *p,
927 const char *name)
928{
929 struct pinctrl_state *state;
930
931 state = kzalloc(sizeof(*state), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -0800932 if (!state)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700933 return ERR_PTR(-ENOMEM);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700934
935 state->name = name;
936 INIT_LIST_HEAD(&state->settings);
937
938 list_add_tail(&state->node, &p->states);
939
940 return state;
941}
942
Tony Lindgren99e4f672016-12-27 09:19:59 -0800943static int add_setting(struct pinctrl *p, struct pinctrl_dev *pctldev,
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900944 const struct pinctrl_map *map)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700945{
946 struct pinctrl_state *state;
947 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700948 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700949
950 state = find_state(p, map->name);
951 if (!state)
952 state = create_state(p, map->name);
953 if (IS_ERR(state))
954 return PTR_ERR(state);
955
Stephen Warren1e2082b2012-03-02 13:05:48 -0700956 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
957 return 0;
958
Stephen Warren6e5e9592012-03-02 13:05:47 -0700959 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -0800960 if (!setting)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700961 return -ENOMEM;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700962
Stephen Warren1e2082b2012-03-02 13:05:48 -0700963 setting->type = map->type;
964
Tony Lindgren99e4f672016-12-27 09:19:59 -0800965 if (pctldev)
966 setting->pctldev = pctldev;
967 else
968 setting->pctldev =
969 get_pinctrl_dev_from_devname(map->ctrl_dev_name);
Markus Elfringcea234e2017-05-02 11:04:55 +0200970 if (!setting->pctldev) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700971 kfree(setting);
Linus Walleij89216492012-12-11 14:14:32 +0100972 /* Do not defer probing of hogs (circular loop) */
973 if (!strcmp(map->ctrl_dev_name, map->dev_name))
974 return -ENODEV;
Linus Walleijc05127c2012-04-10 10:00:38 +0200975 /*
976 * OK let us guess that the driver is not there yet, and
977 * let's defer obtaining this pinctrl handle to later...
978 */
Linus Walleij89216492012-12-11 14:14:32 +0100979 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
980 map->ctrl_dev_name);
Linus Walleijc05127c2012-04-10 10:00:38 +0200981 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700982 }
983
Linus Walleij1a789582012-10-17 20:51:54 +0200984 setting->dev_name = map->dev_name;
985
Stephen Warren1e2082b2012-03-02 13:05:48 -0700986 switch (map->type) {
987 case PIN_MAP_TYPE_MUX_GROUP:
988 ret = pinmux_map_to_setting(map, setting);
989 break;
990 case PIN_MAP_TYPE_CONFIGS_PIN:
991 case PIN_MAP_TYPE_CONFIGS_GROUP:
992 ret = pinconf_map_to_setting(map, setting);
993 break;
994 default:
995 ret = -EINVAL;
996 break;
997 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700998 if (ret < 0) {
999 kfree(setting);
1000 return ret;
1001 }
1002
1003 list_add_tail(&setting->node, &state->settings);
1004
1005 return 0;
1006}
1007
1008static struct pinctrl *find_pinctrl(struct device *dev)
1009{
1010 struct pinctrl *p;
1011
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001012 mutex_lock(&pinctrl_list_mutex);
Stephen Warren1e2082b2012-03-02 13:05:48 -07001013 list_for_each_entry(p, &pinctrl_list, node)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001014 if (p->dev == dev) {
1015 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001016 return p;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001017 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001018
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001019 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001020 return NULL;
1021}
1022
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001023static void pinctrl_free(struct pinctrl *p, bool inlist);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001024
Tony Lindgren99e4f672016-12-27 09:19:59 -08001025static struct pinctrl *create_pinctrl(struct device *dev,
1026 struct pinctrl_dev *pctldev)
Stephen Warren6e5e9592012-03-02 13:05:47 -07001027{
1028 struct pinctrl *p;
1029 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001030 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001031 int i;
Masahiro Yamada3f713b72017-08-04 11:22:31 +09001032 const struct pinctrl_map *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001033 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001034
1035 /*
1036 * create the state cookie holder struct pinctrl for each
1037 * mapping, this is what consumers will get when requesting
1038 * a pin control handle with pinctrl_get()
1039 */
Stephen Warren02f5b982012-02-22 14:26:00 -07001040 p = kzalloc(sizeof(*p), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -08001041 if (!p)
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001042 return ERR_PTR(-ENOMEM);
Stephen Warren7ecdb162012-03-02 13:05:45 -07001043 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001044 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -06001045 INIT_LIST_HEAD(&p->dt_maps);
1046
Tony Lindgren99e4f672016-12-27 09:19:59 -08001047 ret = pinctrl_dt_to_map(p, pctldev);
Stephen Warren57291ce2012-03-23 10:29:46 -06001048 if (ret < 0) {
1049 kfree(p);
1050 return ERR_PTR(ret);
1051 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001052
1053 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001054
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001055 mutex_lock(&pinctrl_maps_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001056 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001057 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -07001058 /* Map must be for this device */
1059 if (strcmp(map->dev_name, devname))
1060 continue;
Nikita Yushchenko7f0ff062017-05-11 23:02:11 +03001061 /*
1062 * If pctldev is not null, we are claiming hog for it,
1063 * that means, setting that is served by pctldev by itself.
1064 *
1065 * Thus we must skip map that is for this device but is served
1066 * by other device.
1067 */
1068 if (pctldev &&
1069 strcmp(dev_name(pctldev->dev), map->ctrl_dev_name))
1070 continue;
Stephen Warren1681f5a2012-02-22 14:25:58 -07001071
Tony Lindgren99e4f672016-12-27 09:19:59 -08001072 ret = add_setting(p, pctldev, map);
Linus Walleij89216492012-12-11 14:14:32 +01001073 /*
1074 * At this point the adding of a setting may:
1075 *
1076 * - Defer, if the pinctrl device is not yet available
1077 * - Fail, if the pinctrl device is not yet available,
1078 * AND the setting is a hog. We cannot defer that, since
1079 * the hog will kick in immediately after the device
1080 * is registered.
1081 *
1082 * If the error returned was not -EPROBE_DEFER then we
1083 * accumulate the errors to see if we end up with
1084 * an -EPROBE_DEFER later, as that is the worst case.
1085 */
1086 if (ret == -EPROBE_DEFER) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001087 pinctrl_free(p, false);
1088 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001089 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001090 }
1091 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001092 mutex_unlock(&pinctrl_maps_mutex);
1093
Linus Walleij89216492012-12-11 14:14:32 +01001094 if (ret < 0) {
Andy Shevchenko3ec440e2017-02-28 16:59:56 +02001095 /* If some other error than deferral occurred, return here */
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001096 pinctrl_free(p, false);
Linus Walleij89216492012-12-11 14:14:32 +01001097 return ERR_PTR(ret);
1098 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001099
Linus Walleijab780292013-01-22 10:56:14 -07001100 kref_init(&p->users);
1101
Linus Walleijb0666ba2012-12-11 13:12:35 +01001102 /* Add the pinctrl handle to the global list */
Stanislaw Gruszka7b320cb2014-02-04 09:07:09 +01001103 mutex_lock(&pinctrl_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -07001104 list_add_tail(&p->node, &pinctrl_list);
Stanislaw Gruszka7b320cb2014-02-04 09:07:09 +01001105 mutex_unlock(&pinctrl_list_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001106
1107 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001108}
Stephen Warren7ecdb162012-03-02 13:05:45 -07001109
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001110/**
1111 * pinctrl_get() - retrieves the pinctrl handle for a device
1112 * @dev: the device to obtain the handle for
1113 */
1114struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warren6e5e9592012-03-02 13:05:47 -07001115{
1116 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001117
Stephen Warren6e5e9592012-03-02 13:05:47 -07001118 if (WARN_ON(!dev))
1119 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -07001120
Linus Walleijab780292013-01-22 10:56:14 -07001121 /*
1122 * See if somebody else (such as the device core) has already
1123 * obtained a handle to the pinctrl for this device. In that case,
1124 * return another pointer to it.
1125 */
Stephen Warren6e5e9592012-03-02 13:05:47 -07001126 p = find_pinctrl(dev);
Markus Elfringcea234e2017-05-02 11:04:55 +02001127 if (p) {
Linus Walleijab780292013-01-22 10:56:14 -07001128 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
1129 kref_get(&p->users);
1130 return p;
1131 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001132
Tony Lindgren99e4f672016-12-27 09:19:59 -08001133 return create_pinctrl(dev, NULL);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001134}
1135EXPORT_SYMBOL_GPL(pinctrl_get);
1136
Richard Genoudd3cee8302013-03-25 15:47:21 +01001137static void pinctrl_free_setting(bool disable_setting,
1138 struct pinctrl_setting *setting)
1139{
1140 switch (setting->type) {
1141 case PIN_MAP_TYPE_MUX_GROUP:
1142 if (disable_setting)
1143 pinmux_disable_setting(setting);
1144 pinmux_free_setting(setting);
1145 break;
1146 case PIN_MAP_TYPE_CONFIGS_PIN:
1147 case PIN_MAP_TYPE_CONFIGS_GROUP:
1148 pinconf_free_setting(setting);
1149 break;
1150 default:
1151 break;
1152 }
1153}
1154
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001155static void pinctrl_free(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -07001156{
Stephen Warren6e5e9592012-03-02 13:05:47 -07001157 struct pinctrl_state *state, *n1;
1158 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001159
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001160 mutex_lock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001161 list_for_each_entry_safe(state, n1, &p->states, node) {
1162 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Richard Genoudd3cee8302013-03-25 15:47:21 +01001163 pinctrl_free_setting(state == p->state, setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001164 list_del(&setting->node);
1165 kfree(setting);
1166 }
1167 list_del(&state->node);
1168 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -07001169 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001170
Stephen Warren57291ce2012-03-23 10:29:46 -06001171 pinctrl_dt_free_maps(p);
1172
Stephen Warren6e5e9592012-03-02 13:05:47 -07001173 if (inlist)
1174 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -07001175 kfree(p);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001176 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001177}
1178
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001179/**
Linus Walleijab780292013-01-22 10:56:14 -07001180 * pinctrl_release() - release the pinctrl handle
1181 * @kref: the kref in the pinctrl being released
1182 */
Sachin Kamat2917e832013-01-24 15:01:00 +05301183static void pinctrl_release(struct kref *kref)
Linus Walleijab780292013-01-22 10:56:14 -07001184{
1185 struct pinctrl *p = container_of(kref, struct pinctrl, users);
1186
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001187 pinctrl_free(p, true);
Linus Walleijab780292013-01-22 10:56:14 -07001188}
1189
1190/**
1191 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
Stephen Warren6e5e9592012-03-02 13:05:47 -07001192 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001193 */
1194void pinctrl_put(struct pinctrl *p)
1195{
Linus Walleijab780292013-01-22 10:56:14 -07001196 kref_put(&p->users, pinctrl_release);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001197}
1198EXPORT_SYMBOL_GPL(pinctrl_put);
1199
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001200/**
1201 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
1202 * @p: the pinctrl handle to retrieve the state from
1203 * @name: the state name to retrieve
1204 */
1205struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
1206 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -07001207{
Stephen Warren6e5e9592012-03-02 13:05:47 -07001208 struct pinctrl_state *state;
1209
1210 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +08001211 if (!state) {
1212 if (pinctrl_dummy_state) {
1213 /* create dummy state */
1214 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
1215 name);
1216 state = create_state(p, name);
Richard Genoudd599bfb2012-08-10 16:53:49 +02001217 } else
1218 state = ERR_PTR(-ENODEV);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +08001219 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001220
1221 return state;
1222}
Stephen Warren6e5e9592012-03-02 13:05:47 -07001223EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
1224
Benjamin Gaignard036f3942019-05-22 17:29:24 +02001225static void pinctrl_link_add(struct pinctrl_dev *pctldev,
1226 struct device *consumer)
1227{
1228 if (pctldev->desc->link_consumers)
1229 device_link_add(consumer, pctldev->dev,
1230 DL_FLAG_PM_RUNTIME |
1231 DL_FLAG_AUTOREMOVE_CONSUMER);
1232}
1233
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001234/**
Florian Fainelli981ed1b2017-03-01 10:32:57 -08001235 * pinctrl_commit_state() - select/activate/program a pinctrl state to HW
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001236 * @p: the pinctrl handle for the device that requests configuration
1237 * @state: the state handle to select/activate/program
1238 */
Florian Fainelli981ed1b2017-03-01 10:32:57 -08001239static int pinctrl_commit_state(struct pinctrl *p, struct pinctrl_state *state)
Stephen Warren6e5e9592012-03-02 13:05:47 -07001240{
1241 struct pinctrl_setting *setting, *setting2;
Richard Genoud50cf7c82013-03-25 15:47:23 +01001242 struct pinctrl_state *old_state = p->state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001243 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -07001244
Stephen Warren6e5e9592012-03-02 13:05:47 -07001245 if (p->state) {
1246 /*
Fan Wu2243a872014-06-09 09:37:56 +08001247 * For each pinmux setting in the old state, forget SW's record
1248 * of mux owner for that pingroup. Any pingroups which are
1249 * still owned by the new state will be re-acquired by the call
1250 * to pinmux_enable_setting() in the loop below.
Stephen Warren6e5e9592012-03-02 13:05:47 -07001251 */
1252 list_for_each_entry(setting, &p->state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001253 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
1254 continue;
Fan Wu2243a872014-06-09 09:37:56 +08001255 pinmux_disable_setting(setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001256 }
1257 }
1258
Richard Genoud3102a762013-03-25 15:47:22 +01001259 p->state = NULL;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001260
1261 /* Apply all the settings for the new state */
1262 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001263 switch (setting->type) {
1264 case PIN_MAP_TYPE_MUX_GROUP:
1265 ret = pinmux_enable_setting(setting);
1266 break;
1267 case PIN_MAP_TYPE_CONFIGS_PIN:
1268 case PIN_MAP_TYPE_CONFIGS_GROUP:
1269 ret = pinconf_apply_setting(setting);
1270 break;
1271 default:
1272 ret = -EINVAL;
1273 break;
1274 }
Richard Genoud3102a762013-03-25 15:47:22 +01001275
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001276 if (ret < 0) {
Richard Genoud3102a762013-03-25 15:47:22 +01001277 goto unapply_new_state;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001278 }
Benjamin Gaignard036f3942019-05-22 17:29:24 +02001279
Linus Walleijb672a872019-05-24 00:11:43 +02001280 /* Do not link hogs (circular dependency) */
1281 if (p != setting->pctldev->p)
1282 pinctrl_link_add(setting->pctldev, p->dev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001283 }
1284
Richard Genoud3102a762013-03-25 15:47:22 +01001285 p->state = state;
1286
Stephen Warren7ecdb162012-03-02 13:05:45 -07001287 return 0;
Richard Genoud3102a762013-03-25 15:47:22 +01001288
1289unapply_new_state:
Richard Genoudda587512013-03-28 12:55:46 +01001290 dev_err(p->dev, "Error applying setting, reverse things back\n");
Richard Genoud3102a762013-03-25 15:47:22 +01001291
Richard Genoud3102a762013-03-25 15:47:22 +01001292 list_for_each_entry(setting2, &state->settings, node) {
1293 if (&setting2->node == &setting->node)
1294 break;
Richard Genoudaf606172013-03-29 10:03:26 +01001295 /*
1296 * All we can do here is pinmux_disable_setting.
1297 * That means that some pins are muxed differently now
1298 * than they were before applying the setting (We can't
1299 * "unmux a pin"!), but it's not a big deal since the pins
1300 * are free to be muxed by another apply_setting.
1301 */
1302 if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
1303 pinmux_disable_setting(setting2);
Richard Genoud3102a762013-03-25 15:47:22 +01001304 }
Richard Genoud8009d5f2013-03-28 12:55:47 +01001305
Richard Genoud385d9422013-03-29 10:03:27 +01001306 /* There's no infinite recursive loop here because p->state is NULL */
1307 if (old_state)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001308 pinctrl_select_state(p, old_state);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001309
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001310 return ret;
1311}
Florian Fainelli981ed1b2017-03-01 10:32:57 -08001312
1313/**
1314 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
1315 * @p: the pinctrl handle for the device that requests configuration
1316 * @state: the state handle to select/activate/program
1317 */
1318int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
1319{
1320 if (p->state == state)
1321 return 0;
1322
1323 return pinctrl_commit_state(p, state);
1324}
Stephen Warren6e5e9592012-03-02 13:05:47 -07001325EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001326
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001327static void devm_pinctrl_release(struct device *dev, void *res)
1328{
1329 pinctrl_put(*(struct pinctrl **)res);
1330}
1331
1332/**
Lee Jones9c340bb2020-07-13 15:49:16 +01001333 * devm_pinctrl_get() - Resource managed pinctrl_get()
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001334 * @dev: the device to obtain the handle for
1335 *
1336 * If there is a need to explicitly destroy the returned struct pinctrl,
1337 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1338 */
1339struct pinctrl *devm_pinctrl_get(struct device *dev)
1340{
1341 struct pinctrl **ptr, *p;
1342
1343 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
1344 if (!ptr)
1345 return ERR_PTR(-ENOMEM);
1346
1347 p = pinctrl_get(dev);
1348 if (!IS_ERR(p)) {
1349 *ptr = p;
1350 devres_add(dev, ptr);
1351 } else {
1352 devres_free(ptr);
1353 }
1354
1355 return p;
1356}
1357EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1358
1359static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1360{
1361 struct pinctrl **p = res;
1362
1363 return *p == data;
1364}
1365
1366/**
1367 * devm_pinctrl_put() - Resource managed pinctrl_put()
1368 * @p: the pinctrl handle to release
1369 *
1370 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1371 * this function will not need to be called and the resource management
1372 * code will ensure that the resource is freed.
1373 */
1374void devm_pinctrl_put(struct pinctrl *p)
1375{
Jingoo Hana72149e2013-02-26 11:34:07 +09001376 WARN_ON(devres_release(p->dev, devm_pinctrl_release,
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001377 devm_pinctrl_match, p));
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001378}
1379EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1380
Hans de Goedec72bed22019-12-16 21:51:18 +01001381/**
1382 * pinctrl_register_mappings() - register a set of pin controller mappings
1383 * @maps: the pincontrol mappings table to register. Note the pinctrl-core
1384 * keeps a reference to the passed in maps, so they should _not_ be
1385 * marked with __initdata.
1386 * @num_maps: the number of maps in the mapping table
1387 */
1388int pinctrl_register_mappings(const struct pinctrl_map *maps,
1389 unsigned num_maps)
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001390{
Stephen Warren1e2082b2012-03-02 13:05:48 -07001391 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001392 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001393
Masahiro Yamada7e9236f2015-05-28 21:52:59 +09001394 pr_debug("add %u pinctrl maps\n", num_maps);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001395
1396 /* First sanity check the new mapping */
1397 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001398 if (!maps[i].dev_name) {
1399 pr_err("failed to register map %s (%d): no device given\n",
1400 maps[i].name, i);
1401 return -EINVAL;
1402 }
1403
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001404 if (!maps[i].name) {
1405 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001406 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001407 return -EINVAL;
1408 }
1409
Stephen Warren1e2082b2012-03-02 13:05:48 -07001410 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1411 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001412 pr_err("failed to register map %s (%d): no pin control device given\n",
1413 maps[i].name, i);
1414 return -EINVAL;
1415 }
1416
Stephen Warren1e2082b2012-03-02 13:05:48 -07001417 switch (maps[i].type) {
1418 case PIN_MAP_TYPE_DUMMY_STATE:
1419 break;
1420 case PIN_MAP_TYPE_MUX_GROUP:
1421 ret = pinmux_validate_map(&maps[i], i);
1422 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001423 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001424 break;
1425 case PIN_MAP_TYPE_CONFIGS_PIN:
1426 case PIN_MAP_TYPE_CONFIGS_GROUP:
1427 ret = pinconf_validate_map(&maps[i], i);
1428 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001429 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001430 break;
1431 default:
1432 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001433 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -07001434 return -EINVAL;
1435 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001436 }
1437
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001438 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -08001439 if (!maps_node)
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001440 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001441
Hans de Goedec72bed22019-12-16 21:51:18 +01001442 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001443 maps_node->num_maps = num_maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001444
Doug Andersonc5272a22015-05-01 09:01:27 -07001445 mutex_lock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001446 list_add_tail(&maps_node->node, &pinctrl_maps);
Doug Andersonc5272a22015-05-01 09:01:27 -07001447 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001448
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001449 return 0;
1450}
Richard Fitzgerald8b1b2dc2018-02-23 13:43:52 +00001451EXPORT_SYMBOL_GPL(pinctrl_register_mappings);
Stephen Warren57291ce2012-03-23 10:29:46 -06001452
Hans de Goedec72bed22019-12-16 21:51:18 +01001453/**
1454 * pinctrl_unregister_mappings() - unregister a set of pin controller mappings
Lee Jones9c340bb2020-07-13 15:49:16 +01001455 * @map: the pincontrol mappings table passed to pinctrl_register_mappings()
Hans de Goedec72bed22019-12-16 21:51:18 +01001456 * when registering the mappings.
1457 */
1458void pinctrl_unregister_mappings(const struct pinctrl_map *map)
Stephen Warren57291ce2012-03-23 10:29:46 -06001459{
1460 struct pinctrl_maps *maps_node;
1461
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001462 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001463 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1464 if (maps_node->maps == map) {
1465 list_del(&maps_node->node);
Linus Walleijdb6c2c62013-07-23 01:43:20 +02001466 kfree(maps_node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001467 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001468 return;
1469 }
1470 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001471 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001472}
Hans de Goedec72bed22019-12-16 21:51:18 +01001473EXPORT_SYMBOL_GPL(pinctrl_unregister_mappings);
Stephen Warren57291ce2012-03-23 10:29:46 -06001474
Julien Delacou840a47b2012-12-10 14:47:33 +01001475/**
1476 * pinctrl_force_sleep() - turn a given controller device into sleep state
1477 * @pctldev: pin controller device
1478 */
1479int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1480{
1481 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
Florian Fainelli981ed1b2017-03-01 10:32:57 -08001482 return pinctrl_commit_state(pctldev->p, pctldev->hog_sleep);
Julien Delacou840a47b2012-12-10 14:47:33 +01001483 return 0;
1484}
1485EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1486
1487/**
1488 * pinctrl_force_default() - turn a given controller device into default state
1489 * @pctldev: pin controller device
1490 */
1491int pinctrl_force_default(struct pinctrl_dev *pctldev)
1492{
1493 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
Florian Fainelli981ed1b2017-03-01 10:32:57 -08001494 return pinctrl_commit_state(pctldev->p, pctldev->hog_default);
Julien Delacou840a47b2012-12-10 14:47:33 +01001495 return 0;
1496}
1497EXPORT_SYMBOL_GPL(pinctrl_force_default);
1498
Douglas Andersonef0eebc2015-10-20 21:15:06 -07001499/**
1500 * pinctrl_init_done() - tell pinctrl probe is done
1501 *
1502 * We'll use this time to switch the pins from "init" to "default" unless the
1503 * driver selected some other state.
1504 *
1505 * @dev: device to that's done probing
1506 */
1507int pinctrl_init_done(struct device *dev)
1508{
1509 struct dev_pin_info *pins = dev->pins;
1510 int ret;
1511
1512 if (!pins)
1513 return 0;
1514
1515 if (IS_ERR(pins->init_state))
1516 return 0; /* No such state */
1517
1518 if (pins->p->state != pins->init_state)
1519 return 0; /* Not at init anyway */
1520
1521 if (IS_ERR(pins->default_state))
1522 return 0; /* No default state */
1523
1524 ret = pinctrl_select_state(pins->p, pins->default_state);
1525 if (ret)
1526 dev_err(dev, "failed to activate default pinctrl state\n");
1527
1528 return ret;
1529}
1530
Ulf Hansson55d54d12019-12-06 18:08:13 +01001531static int pinctrl_select_bound_state(struct device *dev,
1532 struct pinctrl_state *state)
Tony Lindgrenf3333492013-07-18 08:15:04 -07001533{
1534 struct dev_pin_info *pins = dev->pins;
1535 int ret;
1536
1537 if (IS_ERR(state))
1538 return 0; /* No such state */
1539 ret = pinctrl_select_state(pins->p, state);
1540 if (ret)
1541 dev_err(dev, "failed to activate pinctrl state %s\n",
1542 state->name);
1543 return ret;
1544}
1545
1546/**
Ulf Hansson55d54d12019-12-06 18:08:13 +01001547 * pinctrl_select_default_state() - select default pinctrl state
1548 * @dev: device to select default state for
1549 */
1550int pinctrl_select_default_state(struct device *dev)
1551{
1552 if (!dev->pins)
1553 return 0;
1554
1555 return pinctrl_select_bound_state(dev, dev->pins->default_state);
1556}
1557EXPORT_SYMBOL_GPL(pinctrl_select_default_state);
1558
1559#ifdef CONFIG_PM
1560
1561/**
Linus Walleij14005ee2013-06-05 15:30:33 +02001562 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1563 * @dev: device to select default state for
1564 */
1565int pinctrl_pm_select_default_state(struct device *dev)
1566{
Ulf Hansson55d54d12019-12-06 18:08:13 +01001567 return pinctrl_select_default_state(dev);
Linus Walleij14005ee2013-06-05 15:30:33 +02001568}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001569EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001570
1571/**
1572 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1573 * @dev: device to select sleep state for
1574 */
1575int pinctrl_pm_select_sleep_state(struct device *dev)
1576{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001577 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001578 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001579
Ulf Hansson55d54d12019-12-06 18:08:13 +01001580 return pinctrl_select_bound_state(dev, dev->pins->sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001581}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001582EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001583
1584/**
1585 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1586 * @dev: device to select idle state for
1587 */
1588int pinctrl_pm_select_idle_state(struct device *dev)
1589{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001590 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001591 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001592
Ulf Hansson55d54d12019-12-06 18:08:13 +01001593 return pinctrl_select_bound_state(dev, dev->pins->idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001594}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001595EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001596#endif
1597
Linus Walleij2744e8a2011-05-02 20:50:54 +02001598#ifdef CONFIG_DEBUG_FS
1599
1600static int pinctrl_pins_show(struct seq_file *s, void *what)
1601{
1602 struct pinctrl_dev *pctldev = s->private;
1603 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001604 unsigned i, pin;
He Zheb507cb92020-10-28 18:39:21 +08001605#ifdef CONFIG_GPIOLIB
Drew Fustinif1b206c2020-07-22 14:27:52 +02001606 struct pinctrl_gpio_range *range;
1607 unsigned int gpio_num;
1608 struct gpio_chip *chip;
He Zheb507cb92020-10-28 18:39:21 +08001609#endif
Linus Walleij2744e8a2011-05-02 20:50:54 +02001610
1611 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001612
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001613 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001614
Chanho Park706e8522012-01-03 16:47:50 +09001615 /* The pin number can be retrived from the pin controller descriptor */
1616 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001617 struct pin_desc *desc;
1618
Chanho Park706e8522012-01-03 16:47:50 +09001619 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001620 desc = pin_desc_get(pctldev, pin);
1621 /* Pin space may be sparse */
Markus Elfringcea234e2017-05-02 11:04:55 +02001622 if (!desc)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001623 continue;
1624
Masahiro Yamadacf9d9942016-05-24 14:26:26 +09001625 seq_printf(s, "pin %d (%s) ", pin, desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001626
Drew Fustinif1b206c2020-07-22 14:27:52 +02001627#ifdef CONFIG_GPIOLIB
1628 gpio_num = 0;
1629 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
1630 if ((pin >= range->pin_base) &&
1631 (pin < (range->pin_base + range->npins))) {
1632 gpio_num = range->base + (pin - range->pin_base);
1633 break;
1634 }
1635 }
1636 chip = gpio_to_chip(gpio_num);
1637 if (chip && chip->gpiodev && chip->gpiodev->base)
1638 seq_printf(s, "%u:%s ", gpio_num -
1639 chip->gpiodev->base, chip->label);
1640 else
1641 seq_puts(s, "0:? ");
1642#endif
1643
Linus Walleij2744e8a2011-05-02 20:50:54 +02001644 /* Driver-specific info per pin */
1645 if (ops->pin_dbg_show)
1646 ops->pin_dbg_show(pctldev, s, pin);
1647
1648 seq_puts(s, "\n");
1649 }
1650
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001651 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001652
Linus Walleij2744e8a2011-05-02 20:50:54 +02001653 return 0;
1654}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001655DEFINE_SHOW_ATTRIBUTE(pinctrl_pins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001656
1657static int pinctrl_groups_show(struct seq_file *s, void *what)
1658{
1659 struct pinctrl_dev *pctldev = s->private;
1660 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301661 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001662
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001663 mutex_lock(&pctldev->mutex);
1664
Viresh Kumard1e90e92012-03-30 11:25:40 +05301665 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001666
Linus Walleij2744e8a2011-05-02 20:50:54 +02001667 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301668 while (selector < ngroups) {
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001669 const unsigned *pins = NULL;
1670 unsigned num_pins = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001671 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001672 const char *pname;
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001673 int ret = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001674 int i;
1675
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001676 if (ops->get_group_pins)
1677 ret = ops->get_group_pins(pctldev, selector,
1678 &pins, &num_pins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001679 if (ret)
1680 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1681 gname);
1682 else {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001683 seq_printf(s, "group: %s\n", gname);
1684 for (i = 0; i < num_pins; i++) {
1685 pname = pin_get_name(pctldev, pins[i]);
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001686 if (WARN_ON(!pname)) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001687 mutex_unlock(&pctldev->mutex);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001688 return -EINVAL;
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001689 }
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001690 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1691 }
1692 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001693 }
1694 selector++;
1695 }
1696
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001697 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001698
1699 return 0;
1700}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001701DEFINE_SHOW_ATTRIBUTE(pinctrl_groups);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001702
1703static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1704{
1705 struct pinctrl_dev *pctldev = s->private;
Masahiro Yamada6cadafb2019-06-09 23:55:37 +09001706 struct pinctrl_gpio_range *range;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001707
1708 seq_puts(s, "GPIO ranges handled:\n");
1709
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001710 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001711
Linus Walleij2744e8a2011-05-02 20:50:54 +02001712 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001713 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Christian Ruppertc8587ee2013-06-13 14:55:31 +02001714 if (range->pins) {
1715 int a;
1716 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
1717 range->id, range->name,
1718 range->base, (range->base + range->npins - 1));
1719 for (a = 0; a < range->npins - 1; a++)
1720 seq_printf(s, "%u, ", range->pins[a]);
1721 seq_printf(s, "%u}\n", range->pins[a]);
1722 }
1723 else
1724 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1725 range->id, range->name,
1726 range->base, (range->base + range->npins - 1),
1727 range->pin_base,
1728 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001729 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001730
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001731 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001732
1733 return 0;
1734}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001735DEFINE_SHOW_ATTRIBUTE(pinctrl_gpioranges);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001736
1737static int pinctrl_devices_show(struct seq_file *s, void *what)
1738{
1739 struct pinctrl_dev *pctldev;
1740
Linus Walleijae6b4d82011-10-19 18:14:33 +02001741 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001742
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001743 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001744
Linus Walleij2744e8a2011-05-02 20:50:54 +02001745 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1746 seq_printf(s, "%s ", pctldev->desc->name);
1747 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001748 seq_puts(s, "yes ");
1749 else
1750 seq_puts(s, "no ");
1751 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001752 seq_puts(s, "yes");
1753 else
1754 seq_puts(s, "no");
1755 seq_puts(s, "\n");
1756 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001757
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001758 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001759
1760 return 0;
1761}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001762DEFINE_SHOW_ATTRIBUTE(pinctrl_devices);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001763
Stephen Warren1e2082b2012-03-02 13:05:48 -07001764static inline const char *map_type(enum pinctrl_map_type type)
1765{
1766 static const char * const names[] = {
1767 "INVALID",
1768 "DUMMY_STATE",
1769 "MUX_GROUP",
1770 "CONFIGS_PIN",
1771 "CONFIGS_GROUP",
1772 };
1773
1774 if (type >= ARRAY_SIZE(names))
1775 return "UNKNOWN";
1776
1777 return names[type];
1778}
1779
Stephen Warren3eedb432012-02-23 17:04:40 -07001780static int pinctrl_maps_show(struct seq_file *s, void *what)
1781{
1782 struct pinctrl_maps *maps_node;
1783 int i;
Masahiro Yamada3f713b72017-08-04 11:22:31 +09001784 const struct pinctrl_map *map;
Stephen Warren3eedb432012-02-23 17:04:40 -07001785
1786 seq_puts(s, "Pinctrl maps:\n");
1787
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001788 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001789 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001790 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1791 map->dev_name, map->name, map_type(map->type),
1792 map->type);
1793
1794 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1795 seq_printf(s, "controlling device %s\n",
1796 map->ctrl_dev_name);
1797
1798 switch (map->type) {
1799 case PIN_MAP_TYPE_MUX_GROUP:
1800 pinmux_show_map(s, map);
1801 break;
1802 case PIN_MAP_TYPE_CONFIGS_PIN:
1803 case PIN_MAP_TYPE_CONFIGS_GROUP:
1804 pinconf_show_map(s, map);
1805 break;
1806 default:
1807 break;
1808 }
1809
Markus Elfring390e1042017-05-02 10:47:35 +02001810 seq_putc(s, '\n');
Stephen Warren3eedb432012-02-23 17:04:40 -07001811 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001812 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001813
1814 return 0;
1815}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001816DEFINE_SHOW_ATTRIBUTE(pinctrl_maps);
Stephen Warren3eedb432012-02-23 17:04:40 -07001817
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001818static int pinctrl_show(struct seq_file *s, void *what)
1819{
1820 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001821 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001822 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001823
1824 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001825
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001826 mutex_lock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001827
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001828 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001829 seq_printf(s, "device: %s current state: %s\n",
1830 dev_name(p->dev),
1831 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001832
Stephen Warren6e5e9592012-03-02 13:05:47 -07001833 list_for_each_entry(state, &p->states, node) {
1834 seq_printf(s, " state: %s\n", state->name);
1835
1836 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001837 struct pinctrl_dev *pctldev = setting->pctldev;
1838
1839 seq_printf(s, " type: %s controller %s ",
1840 map_type(setting->type),
1841 pinctrl_dev_get_name(pctldev));
1842
1843 switch (setting->type) {
1844 case PIN_MAP_TYPE_MUX_GROUP:
1845 pinmux_show_setting(s, setting);
1846 break;
1847 case PIN_MAP_TYPE_CONFIGS_PIN:
1848 case PIN_MAP_TYPE_CONFIGS_GROUP:
1849 pinconf_show_setting(s, setting);
1850 break;
1851 default:
1852 break;
1853 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001854 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001855 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001856 }
1857
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001858 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001859
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001860 return 0;
1861}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001862DEFINE_SHOW_ATTRIBUTE(pinctrl);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001863
Linus Walleij2744e8a2011-05-02 20:50:54 +02001864static struct dentry *debugfs_root;
1865
1866static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1867{
Tony Lindgren02157162012-01-20 08:17:22 -08001868 struct dentry *device_root;
Jan Kundrát1781af52018-01-26 16:06:37 +01001869 const char *debugfs_name;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001870
Jan Kundrát1781af52018-01-26 16:06:37 +01001871 if (pctldev->desc->name &&
1872 strcmp(dev_name(pctldev->dev), pctldev->desc->name)) {
1873 debugfs_name = devm_kasprintf(pctldev->dev, GFP_KERNEL,
1874 "%s-%s", dev_name(pctldev->dev),
1875 pctldev->desc->name);
1876 if (!debugfs_name) {
1877 pr_warn("failed to determine debugfs dir name for %s\n",
1878 dev_name(pctldev->dev));
1879 return;
1880 }
1881 } else {
1882 debugfs_name = dev_name(pctldev->dev);
1883 }
1884
1885 device_root = debugfs_create_dir(debugfs_name, debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001886 pctldev->device_root = device_root;
1887
Linus Walleij2744e8a2011-05-02 20:50:54 +02001888 if (IS_ERR(device_root) || !device_root) {
1889 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001890 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001891 return;
1892 }
1893 debugfs_create_file("pins", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001894 device_root, pctldev, &pinctrl_pins_fops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001895 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001896 device_root, pctldev, &pinctrl_groups_fops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001897 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001898 device_root, pctldev, &pinctrl_gpioranges_fops);
Florian Vaussarde7f2a442014-02-05 07:51:22 +01001899 if (pctldev->desc->pmxops)
1900 pinmux_init_device_debugfs(device_root, pctldev);
1901 if (pctldev->desc->confops)
1902 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001903}
1904
Tony Lindgren02157162012-01-20 08:17:22 -08001905static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1906{
1907 debugfs_remove_recursive(pctldev->device_root);
1908}
1909
Linus Walleij2744e8a2011-05-02 20:50:54 +02001910static void pinctrl_init_debugfs(void)
1911{
1912 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1913 if (IS_ERR(debugfs_root) || !debugfs_root) {
1914 pr_warn("failed to create debugfs directory\n");
1915 debugfs_root = NULL;
1916 return;
1917 }
1918
1919 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001920 debugfs_root, NULL, &pinctrl_devices_fops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001921 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001922 debugfs_root, NULL, &pinctrl_maps_fops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001923 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001924 debugfs_root, NULL, &pinctrl_fops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001925}
1926
1927#else /* CONFIG_DEBUG_FS */
1928
1929static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1930{
1931}
1932
1933static void pinctrl_init_debugfs(void)
1934{
1935}
1936
Tony Lindgren02157162012-01-20 08:17:22 -08001937static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1938{
1939}
1940
Linus Walleij2744e8a2011-05-02 20:50:54 +02001941#endif
1942
Stephen Warrend26bc492012-03-16 14:54:25 -06001943static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1944{
1945 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1946
1947 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301948 !ops->get_groups_count ||
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001949 !ops->get_group_name)
Stephen Warrend26bc492012-03-16 14:54:25 -06001950 return -EINVAL;
1951
1952 return 0;
1953}
1954
Linus Walleij2744e8a2011-05-02 20:50:54 +02001955/**
Tony Lindgren950b0d92017-01-11 14:13:34 -08001956 * pinctrl_init_controller() - init a pin controller device
Linus Walleij2744e8a2011-05-02 20:50:54 +02001957 * @pctldesc: descriptor for this pin controller
1958 * @dev: parent device for this pin controller
1959 * @driver_data: private pin controller data for this pin controller
1960 */
Andy Shevchenko0ca49212017-03-27 14:37:09 +03001961static struct pinctrl_dev *
1962pinctrl_init_controller(struct pinctrl_desc *pctldesc, struct device *dev,
1963 void *driver_data)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001964{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001965 struct pinctrl_dev *pctldev;
1966 int ret;
1967
Devendra Nagada9aecb2012-06-16 23:43:16 +05301968 if (!pctldesc)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001969 return ERR_PTR(-EINVAL);
Devendra Nagada9aecb2012-06-16 23:43:16 +05301970 if (!pctldesc->name)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001971 return ERR_PTR(-EINVAL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001972
Stephen Warren02f5b982012-02-22 14:26:00 -07001973 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -08001974 if (!pctldev)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001975 return ERR_PTR(-ENOMEM);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001976
1977 /* Initialize pin control device struct */
1978 pctldev->owner = pctldesc->owner;
1979 pctldev->desc = pctldesc;
1980 pctldev->driver_data = driver_data;
1981 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleijc033a712016-12-30 15:04:43 +01001982#ifdef CONFIG_GENERIC_PINCTRL_GROUPS
Tony Lindgrenc7059c52016-12-27 09:20:00 -08001983 INIT_RADIX_TREE(&pctldev->pin_group_tree, GFP_KERNEL);
Linus Walleijc033a712016-12-30 15:04:43 +01001984#endif
Tony Lindgrena76edc82016-12-27 09:20:01 -08001985#ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
1986 INIT_RADIX_TREE(&pctldev->pin_function_tree, GFP_KERNEL);
1987#endif
Linus Walleij2744e8a2011-05-02 20:50:54 +02001988 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Thierry Reding46daed62017-01-12 17:03:34 +01001989 INIT_LIST_HEAD(&pctldev->node);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001990 pctldev->dev = dev;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001991 mutex_init(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001992
Stephen Warrend26bc492012-03-16 14:54:25 -06001993 /* check core ops for sanity */
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001994 ret = pinctrl_check_ops(pctldev);
1995 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001996 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06001997 goto out_err;
1998 }
1999
Tony Lindgrenb9130b72012-01-24 16:28:08 -08002000 /* If we're implementing pinmuxing, check the ops for sanity */
2001 if (pctldesc->pmxops) {
Masahiro Yamada323de9e2015-06-09 13:01:16 +09002002 ret = pinmux_check_ops(pctldev);
2003 if (ret)
Tony Lindgrenb9130b72012-01-24 16:28:08 -08002004 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08002005 }
2006
2007 /* If we're implementing pinconfig, check the ops for sanity */
2008 if (pctldesc->confops) {
Masahiro Yamada323de9e2015-06-09 13:01:16 +09002009 ret = pinconf_check_ops(pctldev);
2010 if (ret)
Tony Lindgrenb9130b72012-01-24 16:28:08 -08002011 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08002012 }
2013
Linus Walleij2744e8a2011-05-02 20:50:54 +02002014 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02002015 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002016 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
2017 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02002018 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02002019 pinctrl_free_pindescs(pctldev, pctldesc->pins,
2020 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07002021 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02002022 }
2023
Linus Walleij2744e8a2011-05-02 20:50:54 +02002024 return pctldev;
2025
Stephen Warren51cd24e2011-12-09 16:59:05 -07002026out_err:
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002027 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07002028 kfree(pctldev);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09002029 return ERR_PTR(ret);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002030}
Tony Lindgren950b0d92017-01-11 14:13:34 -08002031
Tony Lindgren61187142017-03-30 09:16:39 -07002032static int pinctrl_claim_hogs(struct pinctrl_dev *pctldev)
Tony Lindgren950b0d92017-01-11 14:13:34 -08002033{
2034 pctldev->p = create_pinctrl(pctldev->dev, pctldev);
Tony Lindgren61187142017-03-30 09:16:39 -07002035 if (PTR_ERR(pctldev->p) == -ENODEV) {
2036 dev_dbg(pctldev->dev, "no hogs found\n");
Tony Lindgren950b0d92017-01-11 14:13:34 -08002037
Tony Lindgren61187142017-03-30 09:16:39 -07002038 return 0;
2039 }
2040
2041 if (IS_ERR(pctldev->p)) {
2042 dev_err(pctldev->dev, "error claiming hogs: %li\n",
2043 PTR_ERR(pctldev->p));
2044
2045 return PTR_ERR(pctldev->p);
2046 }
2047
Tony Lindgren61187142017-03-30 09:16:39 -07002048 pctldev->hog_default =
2049 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
2050 if (IS_ERR(pctldev->hog_default)) {
2051 dev_dbg(pctldev->dev,
2052 "failed to lookup the default state\n");
2053 } else {
2054 if (pinctrl_select_state(pctldev->p,
2055 pctldev->hog_default))
2056 dev_err(pctldev->dev,
2057 "failed to select default state\n");
2058 }
2059
2060 pctldev->hog_sleep =
2061 pinctrl_lookup_state(pctldev->p,
2062 PINCTRL_STATE_SLEEP);
2063 if (IS_ERR(pctldev->hog_sleep))
2064 dev_dbg(pctldev->dev,
2065 "failed to lookup the sleep state\n");
2066
2067 return 0;
2068}
2069
2070int pinctrl_enable(struct pinctrl_dev *pctldev)
2071{
2072 int error;
2073
2074 error = pinctrl_claim_hogs(pctldev);
2075 if (error) {
2076 dev_err(pctldev->dev, "could not claim hogs: %i\n",
2077 error);
2078 mutex_destroy(&pctldev->mutex);
2079 kfree(pctldev);
2080
2081 return error;
Tony Lindgren950b0d92017-01-11 14:13:34 -08002082 }
2083
2084 mutex_lock(&pinctrldev_list_mutex);
2085 list_add_tail(&pctldev->node, &pinctrldev_list);
2086 mutex_unlock(&pinctrldev_list_mutex);
2087
2088 pinctrl_init_device_debugfs(pctldev);
2089
2090 return 0;
2091}
Tony Lindgren61187142017-03-30 09:16:39 -07002092EXPORT_SYMBOL_GPL(pinctrl_enable);
Tony Lindgren950b0d92017-01-11 14:13:34 -08002093
2094/**
2095 * pinctrl_register() - register a pin controller device
2096 * @pctldesc: descriptor for this pin controller
2097 * @dev: parent device for this pin controller
2098 * @driver_data: private pin controller data for this pin controller
2099 *
2100 * Note that pinctrl_register() is known to have problems as the pin
2101 * controller driver functions are called before the driver has a
2102 * struct pinctrl_dev handle. To avoid issues later on, please use the
2103 * new pinctrl_register_and_init() below instead.
2104 */
2105struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
2106 struct device *dev, void *driver_data)
2107{
2108 struct pinctrl_dev *pctldev;
2109 int error;
2110
2111 pctldev = pinctrl_init_controller(pctldesc, dev, driver_data);
2112 if (IS_ERR(pctldev))
2113 return pctldev;
2114
Tony Lindgren61187142017-03-30 09:16:39 -07002115 error = pinctrl_enable(pctldev);
2116 if (error)
Tony Lindgren950b0d92017-01-11 14:13:34 -08002117 return ERR_PTR(error);
Tony Lindgren950b0d92017-01-11 14:13:34 -08002118
2119 return pctldev;
2120
2121}
Linus Walleij2744e8a2011-05-02 20:50:54 +02002122EXPORT_SYMBOL_GPL(pinctrl_register);
2123
Tony Lindgren61187142017-03-30 09:16:39 -07002124/**
2125 * pinctrl_register_and_init() - register and init pin controller device
2126 * @pctldesc: descriptor for this pin controller
2127 * @dev: parent device for this pin controller
2128 * @driver_data: private pin controller data for this pin controller
2129 * @pctldev: pin controller device
2130 *
2131 * Note that pinctrl_enable() still needs to be manually called after
2132 * this once the driver is ready.
2133 */
Tony Lindgren950b0d92017-01-11 14:13:34 -08002134int pinctrl_register_and_init(struct pinctrl_desc *pctldesc,
2135 struct device *dev, void *driver_data,
2136 struct pinctrl_dev **pctldev)
2137{
2138 struct pinctrl_dev *p;
Tony Lindgren950b0d92017-01-11 14:13:34 -08002139
2140 p = pinctrl_init_controller(pctldesc, dev, driver_data);
2141 if (IS_ERR(p))
2142 return PTR_ERR(p);
2143
2144 /*
2145 * We have pinctrl_start() call functions in the pin controller
2146 * driver with create_pinctrl() for at least dt_node_to_map(). So
2147 * let's make sure pctldev is properly initialized for the
2148 * pin controller driver before we do anything.
2149 */
2150 *pctldev = p;
2151
Tony Lindgren950b0d92017-01-11 14:13:34 -08002152 return 0;
2153}
2154EXPORT_SYMBOL_GPL(pinctrl_register_and_init);
2155
Linus Walleij2744e8a2011-05-02 20:50:54 +02002156/**
2157 * pinctrl_unregister() - unregister pinmux
2158 * @pctldev: pin controller to unregister
2159 *
2160 * Called by pinmux drivers to unregister a pinmux.
2161 */
2162void pinctrl_unregister(struct pinctrl_dev *pctldev)
2163{
Dong Aisheng5d589b02012-05-23 21:22:40 +08002164 struct pinctrl_gpio_range *range, *n;
Jon Hunter3429fb32017-01-05 15:52:55 +00002165
Markus Elfringcea234e2017-05-02 11:04:55 +02002166 if (!pctldev)
Linus Walleij2744e8a2011-05-02 20:50:54 +02002167 return;
2168
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002169 mutex_lock(&pctldev->mutex);
Tony Lindgren02157162012-01-20 08:17:22 -08002170 pinctrl_remove_device_debugfs(pctldev);
Jim Lindb93fac2015-01-08 20:25:05 +08002171 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07002172
Jon Hunter3429fb32017-01-05 15:52:55 +00002173 if (!IS_ERR_OR_NULL(pctldev->p))
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002174 pinctrl_put(pctldev->p);
Stephen Warren57b676f2012-03-02 13:05:44 -07002175
Jim Lindb93fac2015-01-08 20:25:05 +08002176 mutex_lock(&pinctrldev_list_mutex);
2177 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002178 /* TODO: check that no pinmuxes are still active? */
Thierry Reding46daed62017-01-12 17:03:34 +01002179 list_del(&pctldev->node);
Tony Lindgrena76edc82016-12-27 09:20:01 -08002180 pinmux_generic_free_functions(pctldev);
Tony Lindgrenc7059c52016-12-27 09:20:00 -08002181 pinctrl_generic_free_groups(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002182 /* Destroy descriptor tree */
2183 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
2184 pctldev->desc->npins);
Dong Aisheng5d589b02012-05-23 21:22:40 +08002185 /* remove gpio ranges map */
2186 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
2187 list_del(&range->node);
2188
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002189 mutex_unlock(&pctldev->mutex);
2190 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07002191 kfree(pctldev);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002192 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002193}
2194EXPORT_SYMBOL_GPL(pinctrl_unregister);
2195
Laxman Dewangan80e0f8d2016-02-24 14:12:59 +05302196static void devm_pinctrl_dev_release(struct device *dev, void *res)
2197{
2198 struct pinctrl_dev *pctldev = *(struct pinctrl_dev **)res;
2199
2200 pinctrl_unregister(pctldev);
2201}
2202
2203static int devm_pinctrl_dev_match(struct device *dev, void *res, void *data)
2204{
2205 struct pctldev **r = res;
2206
Laxman Dewangan3024f922016-02-24 14:44:07 +05302207 if (WARN_ON(!r || !*r))
Laxman Dewangan80e0f8d2016-02-24 14:12:59 +05302208 return 0;
2209
2210 return *r == data;
2211}
2212
2213/**
2214 * devm_pinctrl_register() - Resource managed version of pinctrl_register().
2215 * @dev: parent device for this pin controller
2216 * @pctldesc: descriptor for this pin controller
2217 * @driver_data: private pin controller data for this pin controller
2218 *
2219 * Returns an error pointer if pincontrol register failed. Otherwise
2220 * it returns valid pinctrl handle.
2221 *
2222 * The pinctrl device will be automatically released when the device is unbound.
2223 */
2224struct pinctrl_dev *devm_pinctrl_register(struct device *dev,
2225 struct pinctrl_desc *pctldesc,
2226 void *driver_data)
2227{
2228 struct pinctrl_dev **ptr, *pctldev;
2229
2230 ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
2231 if (!ptr)
2232 return ERR_PTR(-ENOMEM);
2233
2234 pctldev = pinctrl_register(pctldesc, dev, driver_data);
2235 if (IS_ERR(pctldev)) {
2236 devres_free(ptr);
2237 return pctldev;
2238 }
2239
2240 *ptr = pctldev;
2241 devres_add(dev, ptr);
2242
2243 return pctldev;
2244}
2245EXPORT_SYMBOL_GPL(devm_pinctrl_register);
2246
2247/**
Tony Lindgren950b0d92017-01-11 14:13:34 -08002248 * devm_pinctrl_register_and_init() - Resource managed pinctrl register and init
2249 * @dev: parent device for this pin controller
2250 * @pctldesc: descriptor for this pin controller
2251 * @driver_data: private pin controller data for this pin controller
Lee Jones9c340bb2020-07-13 15:49:16 +01002252 * @pctldev: pin controller device
Tony Lindgren950b0d92017-01-11 14:13:34 -08002253 *
Lee Jones9c340bb2020-07-13 15:49:16 +01002254 * Returns zero on success or an error number on failure.
Tony Lindgren950b0d92017-01-11 14:13:34 -08002255 *
2256 * The pinctrl device will be automatically released when the device is unbound.
2257 */
2258int devm_pinctrl_register_and_init(struct device *dev,
2259 struct pinctrl_desc *pctldesc,
2260 void *driver_data,
2261 struct pinctrl_dev **pctldev)
2262{
2263 struct pinctrl_dev **ptr;
2264 int error;
2265
2266 ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
2267 if (!ptr)
2268 return -ENOMEM;
2269
2270 error = pinctrl_register_and_init(pctldesc, dev, driver_data, pctldev);
2271 if (error) {
2272 devres_free(ptr);
2273 return error;
2274 }
2275
2276 *ptr = *pctldev;
2277 devres_add(dev, ptr);
2278
2279 return 0;
2280}
2281EXPORT_SYMBOL_GPL(devm_pinctrl_register_and_init);
2282
2283/**
Laxman Dewangan80e0f8d2016-02-24 14:12:59 +05302284 * devm_pinctrl_unregister() - Resource managed version of pinctrl_unregister().
2285 * @dev: device for which which resource was allocated
2286 * @pctldev: the pinctrl device to unregister.
2287 */
2288void devm_pinctrl_unregister(struct device *dev, struct pinctrl_dev *pctldev)
2289{
2290 WARN_ON(devres_release(dev, devm_pinctrl_dev_release,
2291 devm_pinctrl_dev_match, pctldev));
2292}
2293EXPORT_SYMBOL_GPL(devm_pinctrl_unregister);
2294
Linus Walleij2744e8a2011-05-02 20:50:54 +02002295static int __init pinctrl_init(void)
2296{
2297 pr_info("initialized pinctrl subsystem\n");
2298 pinctrl_init_debugfs();
2299 return 0;
2300}
2301
2302/* init early since many drivers really need to initialized pinmux early */
2303core_initcall(pinctrl_init);