blob: e5a3030020213ebcfabf2f24460041c758000811 [file] [log] [blame]
Linus Walleij2744e8a2011-05-02 20:50:54 +02001/*
2 * Core driver for the pin control subsystem
3 *
Linus Walleijbefe5bd2012-02-09 19:47:48 +01004 * Copyright (C) 2011-2012 ST-Ericsson SA
Linus Walleij2744e8a2011-05-02 20:50:54 +02005 * Written on behalf of Linaro for ST-Ericsson
6 * Based on bits of regulator core, gpio core and clk core
7 *
8 * Author: Linus Walleij <linus.walleij@linaro.org>
9 *
Stephen Warrenb2b3e662012-02-19 23:45:43 -070010 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
11 *
Linus Walleij2744e8a2011-05-02 20:50:54 +020012 * License terms: GNU General Public License (GPL) version 2
13 */
14#define pr_fmt(fmt) "pinctrl core: " fmt
15
16#include <linux/kernel.h>
Linus Walleijab780292013-01-22 10:56:14 -070017#include <linux/kref.h>
Stephen Rothwella5a697c2011-09-30 14:39:04 +100018#include <linux/export.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020019#include <linux/init.h>
20#include <linux/device.h>
21#include <linux/slab.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020022#include <linux/err.h>
23#include <linux/list.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020024#include <linux/sysfs.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
Stephen Warren6d4ca1f2012-04-16 10:51:00 -060027#include <linux/pinctrl/consumer.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020028#include <linux/pinctrl/pinctrl.h>
29#include <linux/pinctrl/machine.h>
Haojian Zhuang2afe8222013-03-28 07:34:19 +080030
31#ifdef CONFIG_GPIOLIB
Haojian Zhuang51e13c22013-02-17 19:42:50 +080032#include <asm-generic/gpio.h>
Haojian Zhuang2afe8222013-03-28 07:34:19 +080033#endif
34
Linus Walleij2744e8a2011-05-02 20:50:54 +020035#include "core.h"
Stephen Warren57291ce2012-03-23 10:29:46 -060036#include "devicetree.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020037#include "pinmux.h"
Linus Walleijae6b4d82011-10-19 18:14:33 +020038#include "pinconf.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020039
Stephen Warrenb2b3e662012-02-19 23:45:43 -070040
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080041static bool pinctrl_dummy_state;
42
Patrice Chotard42fed7b2013-04-11 11:01:27 +020043/* Mutex taken to protect pinctrl_list */
Sachin Kamat843aec92013-06-18 14:34:27 +053044static DEFINE_MUTEX(pinctrl_list_mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +020045
46/* Mutex taken to protect pinctrl_maps */
47DEFINE_MUTEX(pinctrl_maps_mutex);
48
49/* Mutex taken to protect pinctrldev_list */
Sachin Kamat843aec92013-06-18 14:34:27 +053050static DEFINE_MUTEX(pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -070051
52/* Global list of pin control devices (struct pinctrl_dev) */
Patrice Chotard42fed7b2013-04-11 11:01:27 +020053static LIST_HEAD(pinctrldev_list);
Linus Walleij2744e8a2011-05-02 20:50:54 +020054
Stephen Warren57b676f2012-03-02 13:05:44 -070055/* List of pin controller handles (struct pinctrl) */
Linus Walleijbefe5bd2012-02-09 19:47:48 +010056static LIST_HEAD(pinctrl_list);
57
Stephen Warren57b676f2012-03-02 13:05:44 -070058/* List of pinctrl maps (struct pinctrl_maps) */
Laurent Meunier6f9e41f2013-02-06 09:09:44 +010059LIST_HEAD(pinctrl_maps);
Stephen Warrenb2b3e662012-02-19 23:45:43 -070060
Linus Walleijbefe5bd2012-02-09 19:47:48 +010061
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080062/**
63 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
64 *
65 * Usually this function is called by platforms without pinctrl driver support
66 * but run with some shared drivers using pinctrl APIs.
67 * After calling this function, the pinctrl core will return successfully
68 * with creating a dummy state for the driver to keep going smoothly.
69 */
70void pinctrl_provide_dummies(void)
71{
72 pinctrl_dummy_state = true;
73}
74
Linus Walleij2744e8a2011-05-02 20:50:54 +020075const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
76{
77 /* We're not allowed to register devices without name */
78 return pctldev->desc->name;
79}
80EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
81
Haojian Zhuangd6e99ab2013-01-18 15:31:06 +080082const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
83{
84 return dev_name(pctldev->dev);
85}
86EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
87
Linus Walleij2744e8a2011-05-02 20:50:54 +020088void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
89{
90 return pctldev->driver_data;
91}
92EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
93
94/**
Linus Walleij9dfac4f2012-02-01 18:02:47 +010095 * get_pinctrl_dev_from_devname() - look up pin controller device
96 * @devname: the name of a device instance, as returned by dev_name()
Linus Walleij2744e8a2011-05-02 20:50:54 +020097 *
98 * Looks up a pin control device matching a certain device name or pure device
99 * pointer, the pure device pointer will take precedence.
100 */
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100101struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200102{
103 struct pinctrl_dev *pctldev = NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200104
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100105 if (!devname)
106 return NULL;
107
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200108 mutex_lock(&pinctrldev_list_mutex);
109
Linus Walleij2744e8a2011-05-02 20:50:54 +0200110 list_for_each_entry(pctldev, &pinctrldev_list, node) {
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100111 if (!strcmp(dev_name(pctldev->dev), devname)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200112 /* Matched on device name */
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200113 mutex_unlock(&pinctrldev_list_mutex);
114 return pctldev;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200115 }
116 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200117
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200118 mutex_unlock(&pinctrldev_list_mutex);
119
120 return NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200121}
122
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200123struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np)
124{
125 struct pinctrl_dev *pctldev;
126
127 mutex_lock(&pinctrldev_list_mutex);
128
129 list_for_each_entry(pctldev, &pinctrldev_list, node)
130 if (pctldev->dev->of_node == np) {
131 mutex_unlock(&pinctrldev_list_mutex);
132 return pctldev;
133 }
134
Daniel Mackd463f822013-04-26 18:57:02 +0200135 mutex_unlock(&pinctrldev_list_mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200136
137 return NULL;
138}
139
Linus Walleij2744e8a2011-05-02 20:50:54 +0200140/**
Linus Walleijae6b4d82011-10-19 18:14:33 +0200141 * pin_get_from_name() - look up a pin number from a name
142 * @pctldev: the pin control device to lookup the pin on
143 * @name: the name of the pin to look up
144 */
145int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
146{
Chanho Park706e8522012-01-03 16:47:50 +0900147 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200148
Chanho Park706e8522012-01-03 16:47:50 +0900149 /* The pin number can be retrived from the pin controller descriptor */
150 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200151 struct pin_desc *desc;
152
Chanho Park706e8522012-01-03 16:47:50 +0900153 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200154 desc = pin_desc_get(pctldev, pin);
155 /* Pin space may be sparse */
Axel Lin6c325f82013-08-19 10:06:29 +0800156 if (desc && !strcmp(name, desc->name))
Linus Walleijae6b4d82011-10-19 18:14:33 +0200157 return pin;
158 }
159
160 return -EINVAL;
161}
162
163/**
Dong Aishengdcb5dbc2012-04-17 15:00:46 +0800164 * pin_get_name_from_id() - look up a pin name from a pin id
165 * @pctldev: the pin control device to lookup the pin on
166 * @name: the name of the pin to look up
167 */
168const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
169{
170 const struct pin_desc *desc;
171
172 desc = pin_desc_get(pctldev, pin);
Markus Elfringcea234e2017-05-02 11:04:55 +0200173 if (!desc) {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +0800174 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
175 pin);
176 return NULL;
177 }
178
179 return desc->name;
180}
181
182/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200183 * pin_is_valid() - check if pin exists on controller
184 * @pctldev: the pin control device to check the pin on
185 * @pin: pin to check, use the local pin controller index number
186 *
187 * This tells us whether a certain pin exist on a certain pin controller or
188 * not. Pin lists may be sparse, so some pins may not exist.
189 */
190bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
191{
192 struct pin_desc *pindesc;
193
194 if (pin < 0)
195 return false;
196
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200197 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200198 pindesc = pin_desc_get(pctldev, pin);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200199 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200200
Stephen Warren57b676f2012-03-02 13:05:44 -0700201 return pindesc != NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200202}
203EXPORT_SYMBOL_GPL(pin_is_valid);
204
205/* Deletes a range of pin descriptors */
206static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
207 const struct pinctrl_pin_desc *pins,
208 unsigned num_pins)
209{
210 int i;
211
Linus Walleij2744e8a2011-05-02 20:50:54 +0200212 for (i = 0; i < num_pins; i++) {
213 struct pin_desc *pindesc;
214
215 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
216 pins[i].number);
Markus Elfringcea234e2017-05-02 11:04:55 +0200217 if (pindesc) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200218 radix_tree_delete(&pctldev->pin_desc_tree,
219 pins[i].number);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100220 if (pindesc->dynamic_name)
221 kfree(pindesc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200222 }
223 kfree(pindesc);
224 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200225}
226
227static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900228 const struct pinctrl_pin_desc *pin)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200229{
230 struct pin_desc *pindesc;
231
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900232 pindesc = pin_desc_get(pctldev, pin->number);
Markus Elfringcea234e2017-05-02 11:04:55 +0200233 if (pindesc) {
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900234 dev_err(pctldev->dev, "pin %d already registered\n",
235 pin->number);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200236 return -EINVAL;
237 }
238
239 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -0800240 if (!pindesc)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200241 return -ENOMEM;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200242
Linus Walleij2744e8a2011-05-02 20:50:54 +0200243 /* Set owner */
244 pindesc->pctldev = pctldev;
245
Stephen Warren9af1e442011-10-19 16:19:27 -0600246 /* Copy basic pin info */
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900247 if (pin->name) {
248 pindesc->name = pin->name;
Linus Walleijca53c5f2011-12-14 20:33:37 +0100249 } else {
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900250 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", pin->number);
Markus Elfringcea234e2017-05-02 11:04:55 +0200251 if (!pindesc->name) {
Sachin Kamateb26cc92012-09-25 12:41:40 +0530252 kfree(pindesc);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100253 return -ENOMEM;
Sachin Kamateb26cc92012-09-25 12:41:40 +0530254 }
Linus Walleijca53c5f2011-12-14 20:33:37 +0100255 pindesc->dynamic_name = true;
256 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200257
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900258 pindesc->drv_data = pin->drv_data;
259
260 radix_tree_insert(&pctldev->pin_desc_tree, pin->number, pindesc);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200261 pr_debug("registered pin %d (%s) on %s\n",
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900262 pin->number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200263 return 0;
264}
265
266static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900267 const struct pinctrl_pin_desc *pins,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200268 unsigned num_descs)
269{
270 unsigned i;
271 int ret = 0;
272
273 for (i = 0; i < num_descs; i++) {
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900274 ret = pinctrl_register_one_pin(pctldev, &pins[i]);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200275 if (ret)
276 return ret;
277 }
278
279 return 0;
280}
281
282/**
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200283 * gpio_to_pin() - GPIO range GPIO number to pin number translation
284 * @range: GPIO range used for the translation
285 * @gpio: gpio pin to translate to a pin number
286 *
287 * Finds the pin number for a given GPIO using the specified GPIO range
288 * as a base for translation. The distinction between linear GPIO ranges
289 * and pin list based GPIO ranges is managed correctly by this function.
290 *
291 * This function assumes the gpio is part of the specified GPIO range, use
292 * only after making sure this is the case (e.g. by calling it on the
293 * result of successful pinctrl_get_device_gpio_range calls)!
294 */
295static inline int gpio_to_pin(struct pinctrl_gpio_range *range,
296 unsigned int gpio)
297{
298 unsigned int offset = gpio - range->base;
299 if (range->pins)
300 return range->pins[offset];
301 else
302 return range->pin_base + offset;
303}
304
305/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200306 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
307 * @pctldev: pin controller device to check
308 * @gpio: gpio pin to check taken from the global GPIO pin space
309 *
310 * Tries to match a GPIO pin number to the ranges handled by a certain pin
311 * controller, return the range or NULL
312 */
313static struct pinctrl_gpio_range *
314pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
315{
316 struct pinctrl_gpio_range *range = NULL;
317
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200318 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200319 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200320 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
321 /* Check if we're in the valid range */
322 if (gpio >= range->base &&
323 gpio < range->base + range->npins) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200324 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200325 return range;
326 }
327 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200328 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200329 return NULL;
330}
331
332/**
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800333 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
334 * the same GPIO chip are in range
335 * @gpio: gpio pin to check taken from the global GPIO pin space
336 *
337 * This function is complement of pinctrl_match_gpio_range(). If the return
338 * value of pinctrl_match_gpio_range() is NULL, this function could be used
339 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
340 * of the same GPIO chip don't have back-end pinctrl interface.
341 * If the return value is true, it means that pinctrl device is ready & the
342 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
343 * is false, it means that pinctrl device may not be ready.
344 */
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800345#ifdef CONFIG_GPIOLIB
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800346static bool pinctrl_ready_for_gpio_range(unsigned gpio)
347{
348 struct pinctrl_dev *pctldev;
349 struct pinctrl_gpio_range *range = NULL;
350 struct gpio_chip *chip = gpio_to_chip(gpio);
351
Tony Lindgren942cde72015-09-03 10:34:30 -0700352 if (WARN(!chip, "no gpio_chip for gpio%i?", gpio))
353 return false;
354
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200355 mutex_lock(&pinctrldev_list_mutex);
356
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800357 /* Loop over the pin controllers */
358 list_for_each_entry(pctldev, &pinctrldev_list, node) {
359 /* Loop over the ranges */
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800360 mutex_lock(&pctldev->mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800361 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
362 /* Check if any gpio range overlapped with gpio chip */
363 if (range->base + range->npins - 1 < chip->base ||
364 range->base > chip->base + chip->ngpio - 1)
365 continue;
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800366 mutex_unlock(&pctldev->mutex);
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200367 mutex_unlock(&pinctrldev_list_mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800368 return true;
369 }
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800370 mutex_unlock(&pctldev->mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800371 }
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200372
373 mutex_unlock(&pinctrldev_list_mutex);
374
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800375 return false;
376}
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800377#else
378static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
379#endif
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800380
381/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200382 * pinctrl_get_device_gpio_range() - find device for GPIO range
383 * @gpio: the pin to locate the pin controller for
384 * @outdev: the pin control device if found
385 * @outrange: the GPIO range if found
386 *
387 * Find the pin controller handling a certain GPIO pin from the pinspace of
388 * the GPIO subsystem, return the device and the matching GPIO range. Returns
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800389 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
390 * may still have not been registered.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200391 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700392static int pinctrl_get_device_gpio_range(unsigned gpio,
393 struct pinctrl_dev **outdev,
394 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200395{
396 struct pinctrl_dev *pctldev = NULL;
397
Axel Linf0059022013-08-18 20:34:22 +0800398 mutex_lock(&pinctrldev_list_mutex);
399
Linus Walleij2744e8a2011-05-02 20:50:54 +0200400 /* Loop over the pin controllers */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200401 list_for_each_entry(pctldev, &pinctrldev_list, node) {
402 struct pinctrl_gpio_range *range;
403
404 range = pinctrl_match_gpio_range(pctldev, gpio);
Markus Elfringcea234e2017-05-02 11:04:55 +0200405 if (range) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200406 *outdev = pctldev;
407 *outrange = range;
Axel Linf0059022013-08-18 20:34:22 +0800408 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200409 return 0;
410 }
411 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200412
Axel Linf0059022013-08-18 20:34:22 +0800413 mutex_unlock(&pinctrldev_list_mutex);
414
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800415 return -EPROBE_DEFER;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200416}
417
418/**
419 * pinctrl_add_gpio_range() - register a GPIO range for a controller
420 * @pctldev: pin controller device to add the range to
421 * @range: the GPIO range to add
422 *
423 * This adds a range of GPIOs to be handled by a certain pin controller. Call
424 * this to register handled ranges after registering your pin controller.
425 */
426void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
427 struct pinctrl_gpio_range *range)
428{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200429 mutex_lock(&pctldev->mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700430 list_add_tail(&range->node, &pctldev->gpio_ranges);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200431 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200432}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700433EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200434
Dong Aisheng3e5e00b2012-05-23 21:22:41 +0800435void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
436 struct pinctrl_gpio_range *ranges,
437 unsigned nranges)
438{
439 int i;
440
441 for (i = 0; i < nranges; i++)
442 pinctrl_add_gpio_range(pctldev, &ranges[i]);
443}
444EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
445
Linus Walleij192c3692012-11-20 14:03:37 +0100446struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530447 struct pinctrl_gpio_range *range)
448{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200449 struct pinctrl_dev *pctldev;
450
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200451 pctldev = get_pinctrl_dev_from_devname(devname);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530452
Linus Walleijdfa97512012-11-20 14:54:18 +0100453 /*
454 * If we can't find this device, let's assume that is because
455 * it has not probed yet, so the driver trying to register this
456 * range need to defer probing.
457 */
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200458 if (!pctldev) {
Linus Walleijdfa97512012-11-20 14:54:18 +0100459 return ERR_PTR(-EPROBE_DEFER);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200460 }
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530461 pinctrl_add_gpio_range(pctldev, range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200462
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530463 return pctldev;
464}
Linus Walleij192c3692012-11-20 14:03:37 +0100465EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530466
Christian Ruppert586a87e2013-10-15 15:37:54 +0200467int pinctrl_get_group_pins(struct pinctrl_dev *pctldev, const char *pin_group,
468 const unsigned **pins, unsigned *num_pins)
469{
470 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
471 int gs;
472
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +0200473 if (!pctlops->get_group_pins)
474 return -EINVAL;
475
Christian Ruppert586a87e2013-10-15 15:37:54 +0200476 gs = pinctrl_get_group_selector(pctldev, pin_group);
477 if (gs < 0)
478 return gs;
479
480 return pctlops->get_group_pins(pctldev, gs, pins, num_pins);
481}
482EXPORT_SYMBOL_GPL(pinctrl_get_group_pins);
483
Joachim Eastwoodb18537c2016-02-25 22:44:37 +0100484struct pinctrl_gpio_range *
485pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev *pctldev,
486 unsigned int pin)
487{
488 struct pinctrl_gpio_range *range;
489
490 /* Loop over the ranges */
491 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
492 /* Check if we're in the valid range */
493 if (range->pins) {
494 int a;
495 for (a = 0; a < range->npins; a++) {
496 if (range->pins[a] == pin)
497 return range;
498 }
499 } else if (pin >= range->pin_base &&
500 pin < range->pin_base + range->npins)
501 return range;
502 }
503
504 return NULL;
505}
506EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin_nolock);
507
Linus Walleij2744e8a2011-05-02 20:50:54 +0200508/**
Linus Walleij9afbefb2012-11-20 14:25:07 +0100509 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
510 * @pctldev: the pin controller device to look in
511 * @pin: a controller-local number to find the range for
512 */
513struct pinctrl_gpio_range *
514pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
515 unsigned int pin)
516{
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800517 struct pinctrl_gpio_range *range;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100518
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200519 mutex_lock(&pctldev->mutex);
Joachim Eastwoodb18537c2016-02-25 22:44:37 +0100520 range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200521 mutex_unlock(&pctldev->mutex);
Joachim Eastwoodb18537c2016-02-25 22:44:37 +0100522
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800523 return range;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100524}
525EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
526
527/**
Charles Keepax50842cb2017-02-13 10:11:03 +0000528 * pinctrl_remove_gpio_range() - remove a range of GPIOs from a pin controller
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530529 * @pctldev: pin controller device to remove the range from
530 * @range: the GPIO range to remove
531 */
532void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
533 struct pinctrl_gpio_range *range)
534{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200535 mutex_lock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530536 list_del(&range->node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200537 mutex_unlock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530538}
539EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
540
Linus Walleijc033a712016-12-30 15:04:43 +0100541#ifdef CONFIG_GENERIC_PINCTRL_GROUPS
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800542
543/**
544 * pinctrl_generic_get_group_count() - returns the number of pin groups
545 * @pctldev: pin controller device
546 */
547int pinctrl_generic_get_group_count(struct pinctrl_dev *pctldev)
548{
549 return pctldev->num_groups;
550}
551EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_count);
552
553/**
554 * pinctrl_generic_get_group_name() - returns the name of a pin group
555 * @pctldev: pin controller device
556 * @selector: group number
557 */
558const char *pinctrl_generic_get_group_name(struct pinctrl_dev *pctldev,
559 unsigned int selector)
560{
561 struct group_desc *group;
562
563 group = radix_tree_lookup(&pctldev->pin_group_tree,
564 selector);
565 if (!group)
566 return NULL;
567
568 return group->name;
569}
570EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_name);
571
572/**
573 * pinctrl_generic_get_group_pins() - gets the pin group pins
574 * @pctldev: pin controller device
575 * @selector: group number
576 * @pins: pins in the group
577 * @num_pins: number of pins in the group
578 */
579int pinctrl_generic_get_group_pins(struct pinctrl_dev *pctldev,
580 unsigned int selector,
581 const unsigned int **pins,
582 unsigned int *num_pins)
583{
584 struct group_desc *group;
585
586 group = radix_tree_lookup(&pctldev->pin_group_tree,
587 selector);
588 if (!group) {
589 dev_err(pctldev->dev, "%s could not find pingroup%i\n",
590 __func__, selector);
591 return -EINVAL;
592 }
593
594 *pins = group->pins;
595 *num_pins = group->num_pins;
596
597 return 0;
598}
599EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_pins);
600
601/**
602 * pinctrl_generic_get_group() - returns a pin group based on the number
603 * @pctldev: pin controller device
604 * @gselector: group number
605 */
606struct group_desc *pinctrl_generic_get_group(struct pinctrl_dev *pctldev,
607 unsigned int selector)
608{
609 struct group_desc *group;
610
611 group = radix_tree_lookup(&pctldev->pin_group_tree,
612 selector);
613 if (!group)
614 return NULL;
615
616 return group;
617}
618EXPORT_SYMBOL_GPL(pinctrl_generic_get_group);
619
620/**
621 * pinctrl_generic_add_group() - adds a new pin group
622 * @pctldev: pin controller device
623 * @name: name of the pin group
624 * @pins: pins in the pin group
625 * @num_pins: number of pins in the pin group
626 * @data: pin controller driver specific data
627 *
628 * Note that the caller must take care of locking.
629 */
630int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name,
631 int *pins, int num_pins, void *data)
632{
633 struct group_desc *group;
634
635 group = devm_kzalloc(pctldev->dev, sizeof(*group), GFP_KERNEL);
636 if (!group)
637 return -ENOMEM;
638
639 group->name = name;
640 group->pins = pins;
641 group->num_pins = num_pins;
642 group->data = data;
643
644 radix_tree_insert(&pctldev->pin_group_tree, pctldev->num_groups,
645 group);
646
647 pctldev->num_groups++;
648
649 return 0;
650}
651EXPORT_SYMBOL_GPL(pinctrl_generic_add_group);
652
653/**
654 * pinctrl_generic_remove_group() - removes a numbered pin group
655 * @pctldev: pin controller device
656 * @selector: group number
657 *
658 * Note that the caller must take care of locking.
659 */
660int pinctrl_generic_remove_group(struct pinctrl_dev *pctldev,
661 unsigned int selector)
662{
663 struct group_desc *group;
664
665 group = radix_tree_lookup(&pctldev->pin_group_tree,
666 selector);
667 if (!group)
668 return -ENOENT;
669
670 radix_tree_delete(&pctldev->pin_group_tree, selector);
671 devm_kfree(pctldev->dev, group);
672
673 pctldev->num_groups--;
674
675 return 0;
676}
677EXPORT_SYMBOL_GPL(pinctrl_generic_remove_group);
678
679/**
680 * pinctrl_generic_free_groups() - removes all pin groups
681 * @pctldev: pin controller device
682 *
Tony Lindgren664b7c42017-05-12 08:47:57 -0700683 * Note that the caller must take care of locking. The pinctrl groups
684 * are allocated with devm_kzalloc() so no need to free them here.
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800685 */
686static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
687{
688 struct radix_tree_iter iter;
Masahiro Yamada906a2a32017-08-04 13:52:05 +0900689 void __rcu **slot;
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800690
691 radix_tree_for_each_slot(slot, &pctldev->pin_group_tree, &iter, 0)
Tony Lindgren664b7c42017-05-12 08:47:57 -0700692 radix_tree_delete(&pctldev->pin_group_tree, iter.index);
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800693
694 pctldev->num_groups = 0;
695}
696
697#else
698static inline void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
699{
700}
Linus Walleijc033a712016-12-30 15:04:43 +0100701#endif /* CONFIG_GENERIC_PINCTRL_GROUPS */
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800702
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530703/**
Linus Walleij7afde8b2011-10-19 17:07:16 +0200704 * pinctrl_get_group_selector() - returns the group selector for a group
705 * @pctldev: the pin controller handling the group
706 * @pin_group: the pin group to look up
707 */
708int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
709 const char *pin_group)
710{
711 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530712 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200713 unsigned group_selector = 0;
714
Viresh Kumard1e90e92012-03-30 11:25:40 +0530715 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200716 const char *gname = pctlops->get_group_name(pctldev,
717 group_selector);
718 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700719 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200720 "found group selector %u for %s\n",
721 group_selector,
722 pin_group);
723 return group_selector;
724 }
725
726 group_selector++;
727 }
728
Stephen Warren51cd24e2011-12-09 16:59:05 -0700729 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200730 pin_group);
731
732 return -EINVAL;
733}
734
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100735/**
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200736 * pinctrl_gpio_request() - request a single pin to be used as GPIO
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100737 * @gpio: the GPIO pin number from the GPIO subsystem number space
738 *
739 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
740 * as part of their gpio_request() semantics, platforms and individual drivers
741 * shall *NOT* request GPIO pins to be muxed in.
742 */
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200743int pinctrl_gpio_request(unsigned gpio)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100744{
745 struct pinctrl_dev *pctldev;
746 struct pinctrl_gpio_range *range;
747 int ret;
748 int pin;
749
750 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700751 if (ret) {
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800752 if (pinctrl_ready_for_gpio_range(gpio))
753 ret = 0;
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800754 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700755 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100756
Axel Lin9b77ace42013-08-19 10:07:46 +0800757 mutex_lock(&pctldev->mutex);
758
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100759 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200760 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100761
Stephen Warren57b676f2012-03-02 13:05:44 -0700762 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
763
Axel Lin9b77ace42013-08-19 10:07:46 +0800764 mutex_unlock(&pctldev->mutex);
765
Stephen Warren57b676f2012-03-02 13:05:44 -0700766 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100767}
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200768EXPORT_SYMBOL_GPL(pinctrl_gpio_request);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100769
770/**
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200771 * pinctrl_gpio_free() - free control on a single pin, currently used as GPIO
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100772 * @gpio: the GPIO pin number from the GPIO subsystem number space
773 *
774 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
775 * as part of their gpio_free() semantics, platforms and individual drivers
776 * shall *NOT* request GPIO pins to be muxed out.
777 */
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200778void pinctrl_gpio_free(unsigned gpio)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100779{
780 struct pinctrl_dev *pctldev;
781 struct pinctrl_gpio_range *range;
782 int ret;
783 int pin;
784
785 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700786 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100787 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700788 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200789 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100790
791 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200792 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100793
Stephen Warren57b676f2012-03-02 13:05:44 -0700794 pinmux_free_gpio(pctldev, pin, range);
795
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200796 mutex_unlock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100797}
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200798EXPORT_SYMBOL_GPL(pinctrl_gpio_free);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100799
800static int pinctrl_gpio_direction(unsigned gpio, bool input)
801{
802 struct pinctrl_dev *pctldev;
803 struct pinctrl_gpio_range *range;
804 int ret;
805 int pin;
806
807 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200808 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100809 return ret;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200810 }
811
812 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100813
814 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200815 pin = gpio_to_pin(range, gpio);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200816 ret = pinmux_gpio_direction(pctldev, range, pin, input);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100817
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200818 mutex_unlock(&pctldev->mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200819
820 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100821}
822
823/**
824 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
825 * @gpio: the GPIO pin number from the GPIO subsystem number space
826 *
827 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
828 * as part of their gpio_direction_input() semantics, platforms and individual
829 * drivers shall *NOT* touch pin control GPIO calls.
830 */
831int pinctrl_gpio_direction_input(unsigned gpio)
832{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200833 return pinctrl_gpio_direction(gpio, true);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100834}
835EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
836
837/**
838 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
839 * @gpio: the GPIO pin number from the GPIO subsystem number space
840 *
841 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
842 * as part of their gpio_direction_output() semantics, platforms and individual
843 * drivers shall *NOT* touch pin control GPIO calls.
844 */
845int pinctrl_gpio_direction_output(unsigned gpio)
846{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200847 return pinctrl_gpio_direction(gpio, false);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100848}
849EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
850
Mika Westerberg15381bc2017-01-23 15:34:33 +0300851/**
852 * pinctrl_gpio_set_config() - Apply config to given GPIO pin
853 * @gpio: the GPIO pin number from the GPIO subsystem number space
854 * @config: the configuration to apply to the GPIO
855 *
856 * This function should *ONLY* be used from gpiolib-based GPIO drivers, if
857 * they need to call the underlying pin controller to change GPIO config
858 * (for example set debounce time).
859 */
860int pinctrl_gpio_set_config(unsigned gpio, unsigned long config)
861{
862 unsigned long configs[] = { config };
863 struct pinctrl_gpio_range *range;
864 struct pinctrl_dev *pctldev;
865 int ret, pin;
866
867 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
868 if (ret)
869 return ret;
870
871 mutex_lock(&pctldev->mutex);
872 pin = gpio_to_pin(range, gpio);
873 ret = pinconf_set_config(pctldev, pin, configs, ARRAY_SIZE(configs));
874 mutex_unlock(&pctldev->mutex);
875
876 return ret;
877}
878EXPORT_SYMBOL_GPL(pinctrl_gpio_set_config);
879
Stephen Warren6e5e9592012-03-02 13:05:47 -0700880static struct pinctrl_state *find_state(struct pinctrl *p,
881 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100882{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700883 struct pinctrl_state *state;
884
885 list_for_each_entry(state, &p->states, node)
886 if (!strcmp(state->name, name))
887 return state;
888
889 return NULL;
890}
891
892static struct pinctrl_state *create_state(struct pinctrl *p,
893 const char *name)
894{
895 struct pinctrl_state *state;
896
897 state = kzalloc(sizeof(*state), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -0800898 if (!state)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700899 return ERR_PTR(-ENOMEM);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700900
901 state->name = name;
902 INIT_LIST_HEAD(&state->settings);
903
904 list_add_tail(&state->node, &p->states);
905
906 return state;
907}
908
Tony Lindgren99e4f672016-12-27 09:19:59 -0800909static int add_setting(struct pinctrl *p, struct pinctrl_dev *pctldev,
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900910 const struct pinctrl_map *map)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700911{
912 struct pinctrl_state *state;
913 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700914 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700915
916 state = find_state(p, map->name);
917 if (!state)
918 state = create_state(p, map->name);
919 if (IS_ERR(state))
920 return PTR_ERR(state);
921
Stephen Warren1e2082b2012-03-02 13:05:48 -0700922 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
923 return 0;
924
Stephen Warren6e5e9592012-03-02 13:05:47 -0700925 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -0800926 if (!setting)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700927 return -ENOMEM;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700928
Stephen Warren1e2082b2012-03-02 13:05:48 -0700929 setting->type = map->type;
930
Tony Lindgren99e4f672016-12-27 09:19:59 -0800931 if (pctldev)
932 setting->pctldev = pctldev;
933 else
934 setting->pctldev =
935 get_pinctrl_dev_from_devname(map->ctrl_dev_name);
Markus Elfringcea234e2017-05-02 11:04:55 +0200936 if (!setting->pctldev) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700937 kfree(setting);
Linus Walleij89216492012-12-11 14:14:32 +0100938 /* Do not defer probing of hogs (circular loop) */
939 if (!strcmp(map->ctrl_dev_name, map->dev_name))
940 return -ENODEV;
Linus Walleijc05127c2012-04-10 10:00:38 +0200941 /*
942 * OK let us guess that the driver is not there yet, and
943 * let's defer obtaining this pinctrl handle to later...
944 */
Linus Walleij89216492012-12-11 14:14:32 +0100945 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
946 map->ctrl_dev_name);
Linus Walleijc05127c2012-04-10 10:00:38 +0200947 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700948 }
949
Linus Walleij1a789582012-10-17 20:51:54 +0200950 setting->dev_name = map->dev_name;
951
Stephen Warren1e2082b2012-03-02 13:05:48 -0700952 switch (map->type) {
953 case PIN_MAP_TYPE_MUX_GROUP:
954 ret = pinmux_map_to_setting(map, setting);
955 break;
956 case PIN_MAP_TYPE_CONFIGS_PIN:
957 case PIN_MAP_TYPE_CONFIGS_GROUP:
958 ret = pinconf_map_to_setting(map, setting);
959 break;
960 default:
961 ret = -EINVAL;
962 break;
963 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700964 if (ret < 0) {
965 kfree(setting);
966 return ret;
967 }
968
969 list_add_tail(&setting->node, &state->settings);
970
971 return 0;
972}
973
974static struct pinctrl *find_pinctrl(struct device *dev)
975{
976 struct pinctrl *p;
977
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200978 mutex_lock(&pinctrl_list_mutex);
Stephen Warren1e2082b2012-03-02 13:05:48 -0700979 list_for_each_entry(p, &pinctrl_list, node)
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200980 if (p->dev == dev) {
981 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700982 return p;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200983 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700984
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200985 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700986 return NULL;
987}
988
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200989static void pinctrl_free(struct pinctrl *p, bool inlist);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700990
Tony Lindgren99e4f672016-12-27 09:19:59 -0800991static struct pinctrl *create_pinctrl(struct device *dev,
992 struct pinctrl_dev *pctldev)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700993{
994 struct pinctrl *p;
995 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700996 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100997 int i;
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900998 const struct pinctrl_map *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700999 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001000
1001 /*
1002 * create the state cookie holder struct pinctrl for each
1003 * mapping, this is what consumers will get when requesting
1004 * a pin control handle with pinctrl_get()
1005 */
Stephen Warren02f5b982012-02-22 14:26:00 -07001006 p = kzalloc(sizeof(*p), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -08001007 if (!p)
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001008 return ERR_PTR(-ENOMEM);
Stephen Warren7ecdb162012-03-02 13:05:45 -07001009 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001010 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -06001011 INIT_LIST_HEAD(&p->dt_maps);
1012
Tony Lindgren99e4f672016-12-27 09:19:59 -08001013 ret = pinctrl_dt_to_map(p, pctldev);
Stephen Warren57291ce2012-03-23 10:29:46 -06001014 if (ret < 0) {
1015 kfree(p);
1016 return ERR_PTR(ret);
1017 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001018
1019 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001020
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001021 mutex_lock(&pinctrl_maps_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001022 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001023 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -07001024 /* Map must be for this device */
1025 if (strcmp(map->dev_name, devname))
1026 continue;
Nikita Yushchenko7f0ff062017-05-11 23:02:11 +03001027 /*
1028 * If pctldev is not null, we are claiming hog for it,
1029 * that means, setting that is served by pctldev by itself.
1030 *
1031 * Thus we must skip map that is for this device but is served
1032 * by other device.
1033 */
1034 if (pctldev &&
1035 strcmp(dev_name(pctldev->dev), map->ctrl_dev_name))
1036 continue;
Stephen Warren1681f5a2012-02-22 14:25:58 -07001037
Tony Lindgren99e4f672016-12-27 09:19:59 -08001038 ret = add_setting(p, pctldev, map);
Linus Walleij89216492012-12-11 14:14:32 +01001039 /*
1040 * At this point the adding of a setting may:
1041 *
1042 * - Defer, if the pinctrl device is not yet available
1043 * - Fail, if the pinctrl device is not yet available,
1044 * AND the setting is a hog. We cannot defer that, since
1045 * the hog will kick in immediately after the device
1046 * is registered.
1047 *
1048 * If the error returned was not -EPROBE_DEFER then we
1049 * accumulate the errors to see if we end up with
1050 * an -EPROBE_DEFER later, as that is the worst case.
1051 */
1052 if (ret == -EPROBE_DEFER) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001053 pinctrl_free(p, false);
1054 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001055 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001056 }
1057 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001058 mutex_unlock(&pinctrl_maps_mutex);
1059
Linus Walleij89216492012-12-11 14:14:32 +01001060 if (ret < 0) {
Andy Shevchenko3ec440e2017-02-28 16:59:56 +02001061 /* If some other error than deferral occurred, return here */
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001062 pinctrl_free(p, false);
Linus Walleij89216492012-12-11 14:14:32 +01001063 return ERR_PTR(ret);
1064 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001065
Linus Walleijab780292013-01-22 10:56:14 -07001066 kref_init(&p->users);
1067
Linus Walleijb0666ba2012-12-11 13:12:35 +01001068 /* Add the pinctrl handle to the global list */
Stanislaw Gruszka7b320cb2014-02-04 09:07:09 +01001069 mutex_lock(&pinctrl_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -07001070 list_add_tail(&p->node, &pinctrl_list);
Stanislaw Gruszka7b320cb2014-02-04 09:07:09 +01001071 mutex_unlock(&pinctrl_list_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001072
1073 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001074}
Stephen Warren7ecdb162012-03-02 13:05:45 -07001075
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001076/**
1077 * pinctrl_get() - retrieves the pinctrl handle for a device
1078 * @dev: the device to obtain the handle for
1079 */
1080struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warren6e5e9592012-03-02 13:05:47 -07001081{
1082 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001083
Stephen Warren6e5e9592012-03-02 13:05:47 -07001084 if (WARN_ON(!dev))
1085 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -07001086
Linus Walleijab780292013-01-22 10:56:14 -07001087 /*
1088 * See if somebody else (such as the device core) has already
1089 * obtained a handle to the pinctrl for this device. In that case,
1090 * return another pointer to it.
1091 */
Stephen Warren6e5e9592012-03-02 13:05:47 -07001092 p = find_pinctrl(dev);
Markus Elfringcea234e2017-05-02 11:04:55 +02001093 if (p) {
Linus Walleijab780292013-01-22 10:56:14 -07001094 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
1095 kref_get(&p->users);
1096 return p;
1097 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001098
Tony Lindgren99e4f672016-12-27 09:19:59 -08001099 return create_pinctrl(dev, NULL);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001100}
1101EXPORT_SYMBOL_GPL(pinctrl_get);
1102
Richard Genoudd3cee8302013-03-25 15:47:21 +01001103static void pinctrl_free_setting(bool disable_setting,
1104 struct pinctrl_setting *setting)
1105{
1106 switch (setting->type) {
1107 case PIN_MAP_TYPE_MUX_GROUP:
1108 if (disable_setting)
1109 pinmux_disable_setting(setting);
1110 pinmux_free_setting(setting);
1111 break;
1112 case PIN_MAP_TYPE_CONFIGS_PIN:
1113 case PIN_MAP_TYPE_CONFIGS_GROUP:
1114 pinconf_free_setting(setting);
1115 break;
1116 default:
1117 break;
1118 }
1119}
1120
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001121static void pinctrl_free(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -07001122{
Stephen Warren6e5e9592012-03-02 13:05:47 -07001123 struct pinctrl_state *state, *n1;
1124 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001125
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001126 mutex_lock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001127 list_for_each_entry_safe(state, n1, &p->states, node) {
1128 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Richard Genoudd3cee8302013-03-25 15:47:21 +01001129 pinctrl_free_setting(state == p->state, setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001130 list_del(&setting->node);
1131 kfree(setting);
1132 }
1133 list_del(&state->node);
1134 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -07001135 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001136
Stephen Warren57291ce2012-03-23 10:29:46 -06001137 pinctrl_dt_free_maps(p);
1138
Stephen Warren6e5e9592012-03-02 13:05:47 -07001139 if (inlist)
1140 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -07001141 kfree(p);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001142 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001143}
1144
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001145/**
Linus Walleijab780292013-01-22 10:56:14 -07001146 * pinctrl_release() - release the pinctrl handle
1147 * @kref: the kref in the pinctrl being released
1148 */
Sachin Kamat2917e832013-01-24 15:01:00 +05301149static void pinctrl_release(struct kref *kref)
Linus Walleijab780292013-01-22 10:56:14 -07001150{
1151 struct pinctrl *p = container_of(kref, struct pinctrl, users);
1152
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001153 pinctrl_free(p, true);
Linus Walleijab780292013-01-22 10:56:14 -07001154}
1155
1156/**
1157 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
Stephen Warren6e5e9592012-03-02 13:05:47 -07001158 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001159 */
1160void pinctrl_put(struct pinctrl *p)
1161{
Linus Walleijab780292013-01-22 10:56:14 -07001162 kref_put(&p->users, pinctrl_release);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001163}
1164EXPORT_SYMBOL_GPL(pinctrl_put);
1165
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001166/**
1167 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
1168 * @p: the pinctrl handle to retrieve the state from
1169 * @name: the state name to retrieve
1170 */
1171struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
1172 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -07001173{
Stephen Warren6e5e9592012-03-02 13:05:47 -07001174 struct pinctrl_state *state;
1175
1176 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +08001177 if (!state) {
1178 if (pinctrl_dummy_state) {
1179 /* create dummy state */
1180 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
1181 name);
1182 state = create_state(p, name);
Richard Genoudd599bfb2012-08-10 16:53:49 +02001183 } else
1184 state = ERR_PTR(-ENODEV);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +08001185 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001186
1187 return state;
1188}
Stephen Warren6e5e9592012-03-02 13:05:47 -07001189EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
1190
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001191/**
Florian Fainelli981ed1b2017-03-01 10:32:57 -08001192 * pinctrl_commit_state() - select/activate/program a pinctrl state to HW
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001193 * @p: the pinctrl handle for the device that requests configuration
1194 * @state: the state handle to select/activate/program
1195 */
Florian Fainelli981ed1b2017-03-01 10:32:57 -08001196static int pinctrl_commit_state(struct pinctrl *p, struct pinctrl_state *state)
Stephen Warren6e5e9592012-03-02 13:05:47 -07001197{
1198 struct pinctrl_setting *setting, *setting2;
Richard Genoud50cf7c82013-03-25 15:47:23 +01001199 struct pinctrl_state *old_state = p->state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001200 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -07001201
Stephen Warren6e5e9592012-03-02 13:05:47 -07001202 if (p->state) {
1203 /*
Fan Wu2243a872014-06-09 09:37:56 +08001204 * For each pinmux setting in the old state, forget SW's record
1205 * of mux owner for that pingroup. Any pingroups which are
1206 * still owned by the new state will be re-acquired by the call
1207 * to pinmux_enable_setting() in the loop below.
Stephen Warren6e5e9592012-03-02 13:05:47 -07001208 */
1209 list_for_each_entry(setting, &p->state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001210 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
1211 continue;
Fan Wu2243a872014-06-09 09:37:56 +08001212 pinmux_disable_setting(setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001213 }
1214 }
1215
Richard Genoud3102a762013-03-25 15:47:22 +01001216 p->state = NULL;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001217
1218 /* Apply all the settings for the new state */
1219 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001220 switch (setting->type) {
1221 case PIN_MAP_TYPE_MUX_GROUP:
1222 ret = pinmux_enable_setting(setting);
1223 break;
1224 case PIN_MAP_TYPE_CONFIGS_PIN:
1225 case PIN_MAP_TYPE_CONFIGS_GROUP:
1226 ret = pinconf_apply_setting(setting);
1227 break;
1228 default:
1229 ret = -EINVAL;
1230 break;
1231 }
Richard Genoud3102a762013-03-25 15:47:22 +01001232
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001233 if (ret < 0) {
Richard Genoud3102a762013-03-25 15:47:22 +01001234 goto unapply_new_state;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001235 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001236 }
1237
Richard Genoud3102a762013-03-25 15:47:22 +01001238 p->state = state;
1239
Stephen Warren7ecdb162012-03-02 13:05:45 -07001240 return 0;
Richard Genoud3102a762013-03-25 15:47:22 +01001241
1242unapply_new_state:
Richard Genoudda587512013-03-28 12:55:46 +01001243 dev_err(p->dev, "Error applying setting, reverse things back\n");
Richard Genoud3102a762013-03-25 15:47:22 +01001244
Richard Genoud3102a762013-03-25 15:47:22 +01001245 list_for_each_entry(setting2, &state->settings, node) {
1246 if (&setting2->node == &setting->node)
1247 break;
Richard Genoudaf606172013-03-29 10:03:26 +01001248 /*
1249 * All we can do here is pinmux_disable_setting.
1250 * That means that some pins are muxed differently now
1251 * than they were before applying the setting (We can't
1252 * "unmux a pin"!), but it's not a big deal since the pins
1253 * are free to be muxed by another apply_setting.
1254 */
1255 if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
1256 pinmux_disable_setting(setting2);
Richard Genoud3102a762013-03-25 15:47:22 +01001257 }
Richard Genoud8009d5f2013-03-28 12:55:47 +01001258
Richard Genoud385d9422013-03-29 10:03:27 +01001259 /* There's no infinite recursive loop here because p->state is NULL */
1260 if (old_state)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001261 pinctrl_select_state(p, old_state);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001262
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001263 return ret;
1264}
Florian Fainelli981ed1b2017-03-01 10:32:57 -08001265
1266/**
1267 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
1268 * @p: the pinctrl handle for the device that requests configuration
1269 * @state: the state handle to select/activate/program
1270 */
1271int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
1272{
1273 if (p->state == state)
1274 return 0;
1275
1276 return pinctrl_commit_state(p, state);
1277}
Stephen Warren6e5e9592012-03-02 13:05:47 -07001278EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001279
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001280static void devm_pinctrl_release(struct device *dev, void *res)
1281{
1282 pinctrl_put(*(struct pinctrl **)res);
1283}
1284
1285/**
1286 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
1287 * @dev: the device to obtain the handle for
1288 *
1289 * If there is a need to explicitly destroy the returned struct pinctrl,
1290 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1291 */
1292struct pinctrl *devm_pinctrl_get(struct device *dev)
1293{
1294 struct pinctrl **ptr, *p;
1295
1296 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
1297 if (!ptr)
1298 return ERR_PTR(-ENOMEM);
1299
1300 p = pinctrl_get(dev);
1301 if (!IS_ERR(p)) {
1302 *ptr = p;
1303 devres_add(dev, ptr);
1304 } else {
1305 devres_free(ptr);
1306 }
1307
1308 return p;
1309}
1310EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1311
1312static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1313{
1314 struct pinctrl **p = res;
1315
1316 return *p == data;
1317}
1318
1319/**
1320 * devm_pinctrl_put() - Resource managed pinctrl_put()
1321 * @p: the pinctrl handle to release
1322 *
1323 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1324 * this function will not need to be called and the resource management
1325 * code will ensure that the resource is freed.
1326 */
1327void devm_pinctrl_put(struct pinctrl *p)
1328{
Jingoo Hana72149e2013-02-26 11:34:07 +09001329 WARN_ON(devres_release(p->dev, devm_pinctrl_release,
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001330 devm_pinctrl_match, p));
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001331}
1332EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1333
Masahiro Yamada3f713b72017-08-04 11:22:31 +09001334int pinctrl_register_map(const struct pinctrl_map *maps, unsigned num_maps,
Doug Andersonc5272a22015-05-01 09:01:27 -07001335 bool dup)
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001336{
Stephen Warren1e2082b2012-03-02 13:05:48 -07001337 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001338 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001339
Masahiro Yamada7e9236f2015-05-28 21:52:59 +09001340 pr_debug("add %u pinctrl maps\n", num_maps);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001341
1342 /* First sanity check the new mapping */
1343 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001344 if (!maps[i].dev_name) {
1345 pr_err("failed to register map %s (%d): no device given\n",
1346 maps[i].name, i);
1347 return -EINVAL;
1348 }
1349
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001350 if (!maps[i].name) {
1351 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001352 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001353 return -EINVAL;
1354 }
1355
Stephen Warren1e2082b2012-03-02 13:05:48 -07001356 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1357 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001358 pr_err("failed to register map %s (%d): no pin control device given\n",
1359 maps[i].name, i);
1360 return -EINVAL;
1361 }
1362
Stephen Warren1e2082b2012-03-02 13:05:48 -07001363 switch (maps[i].type) {
1364 case PIN_MAP_TYPE_DUMMY_STATE:
1365 break;
1366 case PIN_MAP_TYPE_MUX_GROUP:
1367 ret = pinmux_validate_map(&maps[i], i);
1368 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001369 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001370 break;
1371 case PIN_MAP_TYPE_CONFIGS_PIN:
1372 case PIN_MAP_TYPE_CONFIGS_GROUP:
1373 ret = pinconf_validate_map(&maps[i], i);
1374 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001375 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001376 break;
1377 default:
1378 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001379 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -07001380 return -EINVAL;
1381 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001382 }
1383
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001384 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -08001385 if (!maps_node)
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001386 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001387
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001388 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -06001389 if (dup) {
1390 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
1391 GFP_KERNEL);
1392 if (!maps_node->maps) {
Stephen Warren57291ce2012-03-23 10:29:46 -06001393 kfree(maps_node);
1394 return -ENOMEM;
1395 }
1396 } else {
1397 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001398 }
1399
Doug Andersonc5272a22015-05-01 09:01:27 -07001400 mutex_lock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001401 list_add_tail(&maps_node->node, &pinctrl_maps);
Doug Andersonc5272a22015-05-01 09:01:27 -07001402 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001403
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001404 return 0;
1405}
1406
Stephen Warren57291ce2012-03-23 10:29:46 -06001407/**
1408 * pinctrl_register_mappings() - register a set of pin controller mappings
1409 * @maps: the pincontrol mappings table to register. This should probably be
1410 * marked with __initdata so it can be discarded after boot. This
1411 * function will perform a shallow copy for the mapping entries.
1412 * @num_maps: the number of maps in the mapping table
1413 */
Masahiro Yamada3f713b72017-08-04 11:22:31 +09001414int pinctrl_register_mappings(const struct pinctrl_map *maps,
Stephen Warren57291ce2012-03-23 10:29:46 -06001415 unsigned num_maps)
1416{
Doug Andersonc5272a22015-05-01 09:01:27 -07001417 return pinctrl_register_map(maps, num_maps, true);
Stephen Warren57291ce2012-03-23 10:29:46 -06001418}
Richard Fitzgerald8b1b2dc2018-02-23 13:43:52 +00001419EXPORT_SYMBOL_GPL(pinctrl_register_mappings);
Stephen Warren57291ce2012-03-23 10:29:46 -06001420
Masahiro Yamada3f713b72017-08-04 11:22:31 +09001421void pinctrl_unregister_map(const struct pinctrl_map *map)
Stephen Warren57291ce2012-03-23 10:29:46 -06001422{
1423 struct pinctrl_maps *maps_node;
1424
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001425 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001426 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1427 if (maps_node->maps == map) {
1428 list_del(&maps_node->node);
Linus Walleijdb6c2c62013-07-23 01:43:20 +02001429 kfree(maps_node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001430 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001431 return;
1432 }
1433 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001434 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001435}
1436
Julien Delacou840a47b2012-12-10 14:47:33 +01001437/**
1438 * pinctrl_force_sleep() - turn a given controller device into sleep state
1439 * @pctldev: pin controller device
1440 */
1441int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1442{
1443 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
Florian Fainelli981ed1b2017-03-01 10:32:57 -08001444 return pinctrl_commit_state(pctldev->p, pctldev->hog_sleep);
Julien Delacou840a47b2012-12-10 14:47:33 +01001445 return 0;
1446}
1447EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1448
1449/**
1450 * pinctrl_force_default() - turn a given controller device into default state
1451 * @pctldev: pin controller device
1452 */
1453int pinctrl_force_default(struct pinctrl_dev *pctldev)
1454{
1455 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
Florian Fainelli981ed1b2017-03-01 10:32:57 -08001456 return pinctrl_commit_state(pctldev->p, pctldev->hog_default);
Julien Delacou840a47b2012-12-10 14:47:33 +01001457 return 0;
1458}
1459EXPORT_SYMBOL_GPL(pinctrl_force_default);
1460
Douglas Andersonef0eebc2015-10-20 21:15:06 -07001461/**
1462 * pinctrl_init_done() - tell pinctrl probe is done
1463 *
1464 * We'll use this time to switch the pins from "init" to "default" unless the
1465 * driver selected some other state.
1466 *
1467 * @dev: device to that's done probing
1468 */
1469int pinctrl_init_done(struct device *dev)
1470{
1471 struct dev_pin_info *pins = dev->pins;
1472 int ret;
1473
1474 if (!pins)
1475 return 0;
1476
1477 if (IS_ERR(pins->init_state))
1478 return 0; /* No such state */
1479
1480 if (pins->p->state != pins->init_state)
1481 return 0; /* Not at init anyway */
1482
1483 if (IS_ERR(pins->default_state))
1484 return 0; /* No default state */
1485
1486 ret = pinctrl_select_state(pins->p, pins->default_state);
1487 if (ret)
1488 dev_err(dev, "failed to activate default pinctrl state\n");
1489
1490 return ret;
1491}
1492
Linus Walleij14005ee2013-06-05 15:30:33 +02001493#ifdef CONFIG_PM
1494
1495/**
Tony Lindgrenf3333492013-07-18 08:15:04 -07001496 * pinctrl_pm_select_state() - select pinctrl state for PM
1497 * @dev: device to select default state for
1498 * @state: state to set
1499 */
1500static int pinctrl_pm_select_state(struct device *dev,
1501 struct pinctrl_state *state)
1502{
1503 struct dev_pin_info *pins = dev->pins;
1504 int ret;
1505
1506 if (IS_ERR(state))
1507 return 0; /* No such state */
1508 ret = pinctrl_select_state(pins->p, state);
1509 if (ret)
1510 dev_err(dev, "failed to activate pinctrl state %s\n",
1511 state->name);
1512 return ret;
1513}
1514
1515/**
Linus Walleij14005ee2013-06-05 15:30:33 +02001516 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1517 * @dev: device to select default state for
1518 */
1519int pinctrl_pm_select_default_state(struct device *dev)
1520{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001521 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001522 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001523
1524 return pinctrl_pm_select_state(dev, dev->pins->default_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001525}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001526EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001527
1528/**
1529 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1530 * @dev: device to select sleep state for
1531 */
1532int pinctrl_pm_select_sleep_state(struct device *dev)
1533{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001534 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001535 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001536
1537 return pinctrl_pm_select_state(dev, dev->pins->sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001538}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001539EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001540
1541/**
1542 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1543 * @dev: device to select idle state for
1544 */
1545int pinctrl_pm_select_idle_state(struct device *dev)
1546{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001547 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001548 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001549
1550 return pinctrl_pm_select_state(dev, dev->pins->idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001551}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001552EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001553#endif
1554
Linus Walleij2744e8a2011-05-02 20:50:54 +02001555#ifdef CONFIG_DEBUG_FS
1556
1557static int pinctrl_pins_show(struct seq_file *s, void *what)
1558{
1559 struct pinctrl_dev *pctldev = s->private;
1560 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001561 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001562
1563 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001564
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001565 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001566
Chanho Park706e8522012-01-03 16:47:50 +09001567 /* The pin number can be retrived from the pin controller descriptor */
1568 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001569 struct pin_desc *desc;
1570
Chanho Park706e8522012-01-03 16:47:50 +09001571 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001572 desc = pin_desc_get(pctldev, pin);
1573 /* Pin space may be sparse */
Markus Elfringcea234e2017-05-02 11:04:55 +02001574 if (!desc)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001575 continue;
1576
Masahiro Yamadacf9d9942016-05-24 14:26:26 +09001577 seq_printf(s, "pin %d (%s) ", pin, desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001578
1579 /* Driver-specific info per pin */
1580 if (ops->pin_dbg_show)
1581 ops->pin_dbg_show(pctldev, s, pin);
1582
1583 seq_puts(s, "\n");
1584 }
1585
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001586 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001587
Linus Walleij2744e8a2011-05-02 20:50:54 +02001588 return 0;
1589}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001590DEFINE_SHOW_ATTRIBUTE(pinctrl_pins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001591
1592static int pinctrl_groups_show(struct seq_file *s, void *what)
1593{
1594 struct pinctrl_dev *pctldev = s->private;
1595 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301596 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001597
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001598 mutex_lock(&pctldev->mutex);
1599
Viresh Kumard1e90e92012-03-30 11:25:40 +05301600 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001601
Linus Walleij2744e8a2011-05-02 20:50:54 +02001602 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301603 while (selector < ngroups) {
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001604 const unsigned *pins = NULL;
1605 unsigned num_pins = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001606 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001607 const char *pname;
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001608 int ret = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001609 int i;
1610
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001611 if (ops->get_group_pins)
1612 ret = ops->get_group_pins(pctldev, selector,
1613 &pins, &num_pins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001614 if (ret)
1615 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1616 gname);
1617 else {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001618 seq_printf(s, "group: %s\n", gname);
1619 for (i = 0; i < num_pins; i++) {
1620 pname = pin_get_name(pctldev, pins[i]);
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001621 if (WARN_ON(!pname)) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001622 mutex_unlock(&pctldev->mutex);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001623 return -EINVAL;
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001624 }
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001625 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1626 }
1627 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001628 }
1629 selector++;
1630 }
1631
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001632 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001633
1634 return 0;
1635}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001636DEFINE_SHOW_ATTRIBUTE(pinctrl_groups);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001637
1638static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1639{
1640 struct pinctrl_dev *pctldev = s->private;
1641 struct pinctrl_gpio_range *range = NULL;
1642
1643 seq_puts(s, "GPIO ranges handled:\n");
1644
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001645 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001646
Linus Walleij2744e8a2011-05-02 20:50:54 +02001647 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001648 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Christian Ruppertc8587ee2013-06-13 14:55:31 +02001649 if (range->pins) {
1650 int a;
1651 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
1652 range->id, range->name,
1653 range->base, (range->base + range->npins - 1));
1654 for (a = 0; a < range->npins - 1; a++)
1655 seq_printf(s, "%u, ", range->pins[a]);
1656 seq_printf(s, "%u}\n", range->pins[a]);
1657 }
1658 else
1659 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1660 range->id, range->name,
1661 range->base, (range->base + range->npins - 1),
1662 range->pin_base,
1663 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001664 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001665
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001666 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001667
1668 return 0;
1669}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001670DEFINE_SHOW_ATTRIBUTE(pinctrl_gpioranges);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001671
1672static int pinctrl_devices_show(struct seq_file *s, void *what)
1673{
1674 struct pinctrl_dev *pctldev;
1675
Linus Walleijae6b4d82011-10-19 18:14:33 +02001676 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001677
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001678 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001679
Linus Walleij2744e8a2011-05-02 20:50:54 +02001680 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1681 seq_printf(s, "%s ", pctldev->desc->name);
1682 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001683 seq_puts(s, "yes ");
1684 else
1685 seq_puts(s, "no ");
1686 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001687 seq_puts(s, "yes");
1688 else
1689 seq_puts(s, "no");
1690 seq_puts(s, "\n");
1691 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001692
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001693 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001694
1695 return 0;
1696}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001697DEFINE_SHOW_ATTRIBUTE(pinctrl_devices);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001698
Stephen Warren1e2082b2012-03-02 13:05:48 -07001699static inline const char *map_type(enum pinctrl_map_type type)
1700{
1701 static const char * const names[] = {
1702 "INVALID",
1703 "DUMMY_STATE",
1704 "MUX_GROUP",
1705 "CONFIGS_PIN",
1706 "CONFIGS_GROUP",
1707 };
1708
1709 if (type >= ARRAY_SIZE(names))
1710 return "UNKNOWN";
1711
1712 return names[type];
1713}
1714
Stephen Warren3eedb432012-02-23 17:04:40 -07001715static int pinctrl_maps_show(struct seq_file *s, void *what)
1716{
1717 struct pinctrl_maps *maps_node;
1718 int i;
Masahiro Yamada3f713b72017-08-04 11:22:31 +09001719 const struct pinctrl_map *map;
Stephen Warren3eedb432012-02-23 17:04:40 -07001720
1721 seq_puts(s, "Pinctrl maps:\n");
1722
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001723 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001724 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001725 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1726 map->dev_name, map->name, map_type(map->type),
1727 map->type);
1728
1729 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1730 seq_printf(s, "controlling device %s\n",
1731 map->ctrl_dev_name);
1732
1733 switch (map->type) {
1734 case PIN_MAP_TYPE_MUX_GROUP:
1735 pinmux_show_map(s, map);
1736 break;
1737 case PIN_MAP_TYPE_CONFIGS_PIN:
1738 case PIN_MAP_TYPE_CONFIGS_GROUP:
1739 pinconf_show_map(s, map);
1740 break;
1741 default:
1742 break;
1743 }
1744
Markus Elfring390e1042017-05-02 10:47:35 +02001745 seq_putc(s, '\n');
Stephen Warren3eedb432012-02-23 17:04:40 -07001746 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001747 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001748
1749 return 0;
1750}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001751DEFINE_SHOW_ATTRIBUTE(pinctrl_maps);
Stephen Warren3eedb432012-02-23 17:04:40 -07001752
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001753static int pinctrl_show(struct seq_file *s, void *what)
1754{
1755 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001756 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001757 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001758
1759 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001760
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001761 mutex_lock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001762
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001763 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001764 seq_printf(s, "device: %s current state: %s\n",
1765 dev_name(p->dev),
1766 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001767
Stephen Warren6e5e9592012-03-02 13:05:47 -07001768 list_for_each_entry(state, &p->states, node) {
1769 seq_printf(s, " state: %s\n", state->name);
1770
1771 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001772 struct pinctrl_dev *pctldev = setting->pctldev;
1773
1774 seq_printf(s, " type: %s controller %s ",
1775 map_type(setting->type),
1776 pinctrl_dev_get_name(pctldev));
1777
1778 switch (setting->type) {
1779 case PIN_MAP_TYPE_MUX_GROUP:
1780 pinmux_show_setting(s, setting);
1781 break;
1782 case PIN_MAP_TYPE_CONFIGS_PIN:
1783 case PIN_MAP_TYPE_CONFIGS_GROUP:
1784 pinconf_show_setting(s, setting);
1785 break;
1786 default:
1787 break;
1788 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001789 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001790 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001791 }
1792
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001793 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001794
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001795 return 0;
1796}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001797DEFINE_SHOW_ATTRIBUTE(pinctrl);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001798
Linus Walleij2744e8a2011-05-02 20:50:54 +02001799static struct dentry *debugfs_root;
1800
1801static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1802{
Tony Lindgren02157162012-01-20 08:17:22 -08001803 struct dentry *device_root;
Jan Kundrát1781af52018-01-26 16:06:37 +01001804 const char *debugfs_name;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001805
Jan Kundrát1781af52018-01-26 16:06:37 +01001806 if (pctldev->desc->name &&
1807 strcmp(dev_name(pctldev->dev), pctldev->desc->name)) {
1808 debugfs_name = devm_kasprintf(pctldev->dev, GFP_KERNEL,
1809 "%s-%s", dev_name(pctldev->dev),
1810 pctldev->desc->name);
1811 if (!debugfs_name) {
1812 pr_warn("failed to determine debugfs dir name for %s\n",
1813 dev_name(pctldev->dev));
1814 return;
1815 }
1816 } else {
1817 debugfs_name = dev_name(pctldev->dev);
1818 }
1819
1820 device_root = debugfs_create_dir(debugfs_name, debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001821 pctldev->device_root = device_root;
1822
Linus Walleij2744e8a2011-05-02 20:50:54 +02001823 if (IS_ERR(device_root) || !device_root) {
1824 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001825 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001826 return;
1827 }
1828 debugfs_create_file("pins", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001829 device_root, pctldev, &pinctrl_pins_fops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001830 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001831 device_root, pctldev, &pinctrl_groups_fops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001832 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001833 device_root, pctldev, &pinctrl_gpioranges_fops);
Florian Vaussarde7f2a442014-02-05 07:51:22 +01001834 if (pctldev->desc->pmxops)
1835 pinmux_init_device_debugfs(device_root, pctldev);
1836 if (pctldev->desc->confops)
1837 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001838}
1839
Tony Lindgren02157162012-01-20 08:17:22 -08001840static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1841{
1842 debugfs_remove_recursive(pctldev->device_root);
1843}
1844
Linus Walleij2744e8a2011-05-02 20:50:54 +02001845static void pinctrl_init_debugfs(void)
1846{
1847 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1848 if (IS_ERR(debugfs_root) || !debugfs_root) {
1849 pr_warn("failed to create debugfs directory\n");
1850 debugfs_root = NULL;
1851 return;
1852 }
1853
1854 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001855 debugfs_root, NULL, &pinctrl_devices_fops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001856 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001857 debugfs_root, NULL, &pinctrl_maps_fops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001858 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001859 debugfs_root, NULL, &pinctrl_fops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001860}
1861
1862#else /* CONFIG_DEBUG_FS */
1863
1864static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1865{
1866}
1867
1868static void pinctrl_init_debugfs(void)
1869{
1870}
1871
Tony Lindgren02157162012-01-20 08:17:22 -08001872static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1873{
1874}
1875
Linus Walleij2744e8a2011-05-02 20:50:54 +02001876#endif
1877
Stephen Warrend26bc492012-03-16 14:54:25 -06001878static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1879{
1880 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1881
1882 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301883 !ops->get_groups_count ||
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001884 !ops->get_group_name)
Stephen Warrend26bc492012-03-16 14:54:25 -06001885 return -EINVAL;
1886
1887 return 0;
1888}
1889
Linus Walleij2744e8a2011-05-02 20:50:54 +02001890/**
Tony Lindgren950b0d92017-01-11 14:13:34 -08001891 * pinctrl_init_controller() - init a pin controller device
Linus Walleij2744e8a2011-05-02 20:50:54 +02001892 * @pctldesc: descriptor for this pin controller
1893 * @dev: parent device for this pin controller
1894 * @driver_data: private pin controller data for this pin controller
1895 */
Andy Shevchenko0ca49212017-03-27 14:37:09 +03001896static struct pinctrl_dev *
1897pinctrl_init_controller(struct pinctrl_desc *pctldesc, struct device *dev,
1898 void *driver_data)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001899{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001900 struct pinctrl_dev *pctldev;
1901 int ret;
1902
Devendra Nagada9aecb2012-06-16 23:43:16 +05301903 if (!pctldesc)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001904 return ERR_PTR(-EINVAL);
Devendra Nagada9aecb2012-06-16 23:43:16 +05301905 if (!pctldesc->name)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001906 return ERR_PTR(-EINVAL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001907
Stephen Warren02f5b982012-02-22 14:26:00 -07001908 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -08001909 if (!pctldev)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001910 return ERR_PTR(-ENOMEM);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001911
1912 /* Initialize pin control device struct */
1913 pctldev->owner = pctldesc->owner;
1914 pctldev->desc = pctldesc;
1915 pctldev->driver_data = driver_data;
1916 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleijc033a712016-12-30 15:04:43 +01001917#ifdef CONFIG_GENERIC_PINCTRL_GROUPS
Tony Lindgrenc7059c52016-12-27 09:20:00 -08001918 INIT_RADIX_TREE(&pctldev->pin_group_tree, GFP_KERNEL);
Linus Walleijc033a712016-12-30 15:04:43 +01001919#endif
Tony Lindgrena76edc82016-12-27 09:20:01 -08001920#ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
1921 INIT_RADIX_TREE(&pctldev->pin_function_tree, GFP_KERNEL);
1922#endif
Linus Walleij2744e8a2011-05-02 20:50:54 +02001923 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Thierry Reding46daed62017-01-12 17:03:34 +01001924 INIT_LIST_HEAD(&pctldev->node);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001925 pctldev->dev = dev;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001926 mutex_init(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001927
Stephen Warrend26bc492012-03-16 14:54:25 -06001928 /* check core ops for sanity */
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001929 ret = pinctrl_check_ops(pctldev);
1930 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001931 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06001932 goto out_err;
1933 }
1934
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001935 /* If we're implementing pinmuxing, check the ops for sanity */
1936 if (pctldesc->pmxops) {
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001937 ret = pinmux_check_ops(pctldev);
1938 if (ret)
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001939 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001940 }
1941
1942 /* If we're implementing pinconfig, check the ops for sanity */
1943 if (pctldesc->confops) {
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001944 ret = pinconf_check_ops(pctldev);
1945 if (ret)
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001946 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001947 }
1948
Linus Walleij2744e8a2011-05-02 20:50:54 +02001949 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02001950 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001951 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1952 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001953 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001954 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1955 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001956 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001957 }
1958
Linus Walleij2744e8a2011-05-02 20:50:54 +02001959 return pctldev;
1960
Stephen Warren51cd24e2011-12-09 16:59:05 -07001961out_err:
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001962 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001963 kfree(pctldev);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001964 return ERR_PTR(ret);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001965}
Tony Lindgren950b0d92017-01-11 14:13:34 -08001966
Tony Lindgren61187142017-03-30 09:16:39 -07001967static int pinctrl_claim_hogs(struct pinctrl_dev *pctldev)
Tony Lindgren950b0d92017-01-11 14:13:34 -08001968{
1969 pctldev->p = create_pinctrl(pctldev->dev, pctldev);
Tony Lindgren61187142017-03-30 09:16:39 -07001970 if (PTR_ERR(pctldev->p) == -ENODEV) {
1971 dev_dbg(pctldev->dev, "no hogs found\n");
Tony Lindgren950b0d92017-01-11 14:13:34 -08001972
Tony Lindgren61187142017-03-30 09:16:39 -07001973 return 0;
1974 }
1975
1976 if (IS_ERR(pctldev->p)) {
1977 dev_err(pctldev->dev, "error claiming hogs: %li\n",
1978 PTR_ERR(pctldev->p));
1979
1980 return PTR_ERR(pctldev->p);
1981 }
1982
1983 kref_get(&pctldev->p->users);
1984 pctldev->hog_default =
1985 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
1986 if (IS_ERR(pctldev->hog_default)) {
1987 dev_dbg(pctldev->dev,
1988 "failed to lookup the default state\n");
1989 } else {
1990 if (pinctrl_select_state(pctldev->p,
1991 pctldev->hog_default))
1992 dev_err(pctldev->dev,
1993 "failed to select default state\n");
1994 }
1995
1996 pctldev->hog_sleep =
1997 pinctrl_lookup_state(pctldev->p,
1998 PINCTRL_STATE_SLEEP);
1999 if (IS_ERR(pctldev->hog_sleep))
2000 dev_dbg(pctldev->dev,
2001 "failed to lookup the sleep state\n");
2002
2003 return 0;
2004}
2005
2006int pinctrl_enable(struct pinctrl_dev *pctldev)
2007{
2008 int error;
2009
2010 error = pinctrl_claim_hogs(pctldev);
2011 if (error) {
2012 dev_err(pctldev->dev, "could not claim hogs: %i\n",
2013 error);
2014 mutex_destroy(&pctldev->mutex);
2015 kfree(pctldev);
2016
2017 return error;
Tony Lindgren950b0d92017-01-11 14:13:34 -08002018 }
2019
2020 mutex_lock(&pinctrldev_list_mutex);
2021 list_add_tail(&pctldev->node, &pinctrldev_list);
2022 mutex_unlock(&pinctrldev_list_mutex);
2023
2024 pinctrl_init_device_debugfs(pctldev);
2025
2026 return 0;
2027}
Tony Lindgren61187142017-03-30 09:16:39 -07002028EXPORT_SYMBOL_GPL(pinctrl_enable);
Tony Lindgren950b0d92017-01-11 14:13:34 -08002029
2030/**
2031 * pinctrl_register() - register a pin controller device
2032 * @pctldesc: descriptor for this pin controller
2033 * @dev: parent device for this pin controller
2034 * @driver_data: private pin controller data for this pin controller
2035 *
2036 * Note that pinctrl_register() is known to have problems as the pin
2037 * controller driver functions are called before the driver has a
2038 * struct pinctrl_dev handle. To avoid issues later on, please use the
2039 * new pinctrl_register_and_init() below instead.
2040 */
2041struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
2042 struct device *dev, void *driver_data)
2043{
2044 struct pinctrl_dev *pctldev;
2045 int error;
2046
2047 pctldev = pinctrl_init_controller(pctldesc, dev, driver_data);
2048 if (IS_ERR(pctldev))
2049 return pctldev;
2050
Tony Lindgren61187142017-03-30 09:16:39 -07002051 error = pinctrl_enable(pctldev);
2052 if (error)
Tony Lindgren950b0d92017-01-11 14:13:34 -08002053 return ERR_PTR(error);
Tony Lindgren950b0d92017-01-11 14:13:34 -08002054
2055 return pctldev;
2056
2057}
Linus Walleij2744e8a2011-05-02 20:50:54 +02002058EXPORT_SYMBOL_GPL(pinctrl_register);
2059
Tony Lindgren61187142017-03-30 09:16:39 -07002060/**
2061 * pinctrl_register_and_init() - register and init pin controller device
2062 * @pctldesc: descriptor for this pin controller
2063 * @dev: parent device for this pin controller
2064 * @driver_data: private pin controller data for this pin controller
2065 * @pctldev: pin controller device
2066 *
2067 * Note that pinctrl_enable() still needs to be manually called after
2068 * this once the driver is ready.
2069 */
Tony Lindgren950b0d92017-01-11 14:13:34 -08002070int pinctrl_register_and_init(struct pinctrl_desc *pctldesc,
2071 struct device *dev, void *driver_data,
2072 struct pinctrl_dev **pctldev)
2073{
2074 struct pinctrl_dev *p;
Tony Lindgren950b0d92017-01-11 14:13:34 -08002075
2076 p = pinctrl_init_controller(pctldesc, dev, driver_data);
2077 if (IS_ERR(p))
2078 return PTR_ERR(p);
2079
2080 /*
2081 * We have pinctrl_start() call functions in the pin controller
2082 * driver with create_pinctrl() for at least dt_node_to_map(). So
2083 * let's make sure pctldev is properly initialized for the
2084 * pin controller driver before we do anything.
2085 */
2086 *pctldev = p;
2087
Tony Lindgren950b0d92017-01-11 14:13:34 -08002088 return 0;
2089}
2090EXPORT_SYMBOL_GPL(pinctrl_register_and_init);
2091
Linus Walleij2744e8a2011-05-02 20:50:54 +02002092/**
2093 * pinctrl_unregister() - unregister pinmux
2094 * @pctldev: pin controller to unregister
2095 *
2096 * Called by pinmux drivers to unregister a pinmux.
2097 */
2098void pinctrl_unregister(struct pinctrl_dev *pctldev)
2099{
Dong Aisheng5d589b02012-05-23 21:22:40 +08002100 struct pinctrl_gpio_range *range, *n;
Jon Hunter3429fb32017-01-05 15:52:55 +00002101
Markus Elfringcea234e2017-05-02 11:04:55 +02002102 if (!pctldev)
Linus Walleij2744e8a2011-05-02 20:50:54 +02002103 return;
2104
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002105 mutex_lock(&pctldev->mutex);
Tony Lindgren02157162012-01-20 08:17:22 -08002106 pinctrl_remove_device_debugfs(pctldev);
Jim Lindb93fac2015-01-08 20:25:05 +08002107 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07002108
Jon Hunter3429fb32017-01-05 15:52:55 +00002109 if (!IS_ERR_OR_NULL(pctldev->p))
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002110 pinctrl_put(pctldev->p);
Stephen Warren57b676f2012-03-02 13:05:44 -07002111
Jim Lindb93fac2015-01-08 20:25:05 +08002112 mutex_lock(&pinctrldev_list_mutex);
2113 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002114 /* TODO: check that no pinmuxes are still active? */
Thierry Reding46daed62017-01-12 17:03:34 +01002115 list_del(&pctldev->node);
Tony Lindgrena76edc82016-12-27 09:20:01 -08002116 pinmux_generic_free_functions(pctldev);
Tony Lindgrenc7059c52016-12-27 09:20:00 -08002117 pinctrl_generic_free_groups(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002118 /* Destroy descriptor tree */
2119 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
2120 pctldev->desc->npins);
Dong Aisheng5d589b02012-05-23 21:22:40 +08002121 /* remove gpio ranges map */
2122 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
2123 list_del(&range->node);
2124
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002125 mutex_unlock(&pctldev->mutex);
2126 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07002127 kfree(pctldev);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002128 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002129}
2130EXPORT_SYMBOL_GPL(pinctrl_unregister);
2131
Laxman Dewangan80e0f8d2016-02-24 14:12:59 +05302132static void devm_pinctrl_dev_release(struct device *dev, void *res)
2133{
2134 struct pinctrl_dev *pctldev = *(struct pinctrl_dev **)res;
2135
2136 pinctrl_unregister(pctldev);
2137}
2138
2139static int devm_pinctrl_dev_match(struct device *dev, void *res, void *data)
2140{
2141 struct pctldev **r = res;
2142
Laxman Dewangan3024f922016-02-24 14:44:07 +05302143 if (WARN_ON(!r || !*r))
Laxman Dewangan80e0f8d2016-02-24 14:12:59 +05302144 return 0;
2145
2146 return *r == data;
2147}
2148
2149/**
2150 * devm_pinctrl_register() - Resource managed version of pinctrl_register().
2151 * @dev: parent device for this pin controller
2152 * @pctldesc: descriptor for this pin controller
2153 * @driver_data: private pin controller data for this pin controller
2154 *
2155 * Returns an error pointer if pincontrol register failed. Otherwise
2156 * it returns valid pinctrl handle.
2157 *
2158 * The pinctrl device will be automatically released when the device is unbound.
2159 */
2160struct pinctrl_dev *devm_pinctrl_register(struct device *dev,
2161 struct pinctrl_desc *pctldesc,
2162 void *driver_data)
2163{
2164 struct pinctrl_dev **ptr, *pctldev;
2165
2166 ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
2167 if (!ptr)
2168 return ERR_PTR(-ENOMEM);
2169
2170 pctldev = pinctrl_register(pctldesc, dev, driver_data);
2171 if (IS_ERR(pctldev)) {
2172 devres_free(ptr);
2173 return pctldev;
2174 }
2175
2176 *ptr = pctldev;
2177 devres_add(dev, ptr);
2178
2179 return pctldev;
2180}
2181EXPORT_SYMBOL_GPL(devm_pinctrl_register);
2182
2183/**
Tony Lindgren950b0d92017-01-11 14:13:34 -08002184 * devm_pinctrl_register_and_init() - Resource managed pinctrl register and init
2185 * @dev: parent device for this pin controller
2186 * @pctldesc: descriptor for this pin controller
2187 * @driver_data: private pin controller data for this pin controller
2188 *
2189 * Returns an error pointer if pincontrol register failed. Otherwise
2190 * it returns valid pinctrl handle.
2191 *
2192 * The pinctrl device will be automatically released when the device is unbound.
2193 */
2194int devm_pinctrl_register_and_init(struct device *dev,
2195 struct pinctrl_desc *pctldesc,
2196 void *driver_data,
2197 struct pinctrl_dev **pctldev)
2198{
2199 struct pinctrl_dev **ptr;
2200 int error;
2201
2202 ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
2203 if (!ptr)
2204 return -ENOMEM;
2205
2206 error = pinctrl_register_and_init(pctldesc, dev, driver_data, pctldev);
2207 if (error) {
2208 devres_free(ptr);
2209 return error;
2210 }
2211
2212 *ptr = *pctldev;
2213 devres_add(dev, ptr);
2214
2215 return 0;
2216}
2217EXPORT_SYMBOL_GPL(devm_pinctrl_register_and_init);
2218
2219/**
Laxman Dewangan80e0f8d2016-02-24 14:12:59 +05302220 * devm_pinctrl_unregister() - Resource managed version of pinctrl_unregister().
2221 * @dev: device for which which resource was allocated
2222 * @pctldev: the pinctrl device to unregister.
2223 */
2224void devm_pinctrl_unregister(struct device *dev, struct pinctrl_dev *pctldev)
2225{
2226 WARN_ON(devres_release(dev, devm_pinctrl_dev_release,
2227 devm_pinctrl_dev_match, pctldev));
2228}
2229EXPORT_SYMBOL_GPL(devm_pinctrl_unregister);
2230
Linus Walleij2744e8a2011-05-02 20:50:54 +02002231static int __init pinctrl_init(void)
2232{
2233 pr_info("initialized pinctrl subsystem\n");
2234 pinctrl_init_debugfs();
2235 return 0;
2236}
2237
2238/* init early since many drivers really need to initialized pinmux early */
2239core_initcall(pinctrl_init);