blob: c6ff4d5fa482e09f3aac5ad723e1510d364a099d [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/debugfs.h>
25#include <linux/seq_file.h>
Stephen Warren6d4ca1f2012-04-16 10:51:00 -060026#include <linux/pinctrl/consumer.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020027#include <linux/pinctrl/pinctrl.h>
28#include <linux/pinctrl/machine.h>
Haojian Zhuang2afe8222013-03-28 07:34:19 +080029
30#ifdef CONFIG_GPIOLIB
Haojian Zhuang51e13c22013-02-17 19:42:50 +080031#include <asm-generic/gpio.h>
Haojian Zhuang2afe8222013-03-28 07:34:19 +080032#endif
33
Linus Walleij2744e8a2011-05-02 20:50:54 +020034#include "core.h"
Stephen Warren57291ce2012-03-23 10:29:46 -060035#include "devicetree.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020036#include "pinmux.h"
Linus Walleijae6b4d82011-10-19 18:14:33 +020037#include "pinconf.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020038
Stephen Warrenb2b3e662012-02-19 23:45:43 -070039
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080040static bool pinctrl_dummy_state;
41
Patrice Chotard42fed7b2013-04-11 11:01:27 +020042/* Mutex taken to protect pinctrl_list */
Sachin Kamat843aec92013-06-18 14:34:27 +053043static DEFINE_MUTEX(pinctrl_list_mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +020044
45/* Mutex taken to protect pinctrl_maps */
46DEFINE_MUTEX(pinctrl_maps_mutex);
47
48/* Mutex taken to protect pinctrldev_list */
Sachin Kamat843aec92013-06-18 14:34:27 +053049static DEFINE_MUTEX(pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -070050
51/* Global list of pin control devices (struct pinctrl_dev) */
Patrice Chotard42fed7b2013-04-11 11:01:27 +020052static LIST_HEAD(pinctrldev_list);
Linus Walleij2744e8a2011-05-02 20:50:54 +020053
Stephen Warren57b676f2012-03-02 13:05:44 -070054/* List of pin controller handles (struct pinctrl) */
Linus Walleijbefe5bd2012-02-09 19:47:48 +010055static LIST_HEAD(pinctrl_list);
56
Stephen Warren57b676f2012-03-02 13:05:44 -070057/* List of pinctrl maps (struct pinctrl_maps) */
Laurent Meunier6f9e41f2013-02-06 09:09:44 +010058LIST_HEAD(pinctrl_maps);
Stephen Warrenb2b3e662012-02-19 23:45:43 -070059
Linus Walleijbefe5bd2012-02-09 19:47:48 +010060
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080061/**
62 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
63 *
64 * Usually this function is called by platforms without pinctrl driver support
65 * but run with some shared drivers using pinctrl APIs.
66 * After calling this function, the pinctrl core will return successfully
67 * with creating a dummy state for the driver to keep going smoothly.
68 */
69void pinctrl_provide_dummies(void)
70{
71 pinctrl_dummy_state = true;
72}
73
Linus Walleij2744e8a2011-05-02 20:50:54 +020074const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
75{
76 /* We're not allowed to register devices without name */
77 return pctldev->desc->name;
78}
79EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
80
Haojian Zhuangd6e99ab2013-01-18 15:31:06 +080081const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
82{
83 return dev_name(pctldev->dev);
84}
85EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
86
Linus Walleij2744e8a2011-05-02 20:50:54 +020087void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
88{
89 return pctldev->driver_data;
90}
91EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
92
93/**
Linus Walleij9dfac4f2012-02-01 18:02:47 +010094 * get_pinctrl_dev_from_devname() - look up pin controller device
95 * @devname: the name of a device instance, as returned by dev_name()
Linus Walleij2744e8a2011-05-02 20:50:54 +020096 *
97 * Looks up a pin control device matching a certain device name or pure device
98 * pointer, the pure device pointer will take precedence.
99 */
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100100struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200101{
102 struct pinctrl_dev *pctldev = NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200103
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100104 if (!devname)
105 return NULL;
106
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200107 mutex_lock(&pinctrldev_list_mutex);
108
Linus Walleij2744e8a2011-05-02 20:50:54 +0200109 list_for_each_entry(pctldev, &pinctrldev_list, node) {
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100110 if (!strcmp(dev_name(pctldev->dev), devname)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200111 /* Matched on device name */
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200112 mutex_unlock(&pinctrldev_list_mutex);
113 return pctldev;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200114 }
115 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200116
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200117 mutex_unlock(&pinctrldev_list_mutex);
118
119 return NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200120}
121
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200122struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np)
123{
124 struct pinctrl_dev *pctldev;
125
126 mutex_lock(&pinctrldev_list_mutex);
127
128 list_for_each_entry(pctldev, &pinctrldev_list, node)
129 if (pctldev->dev->of_node == np) {
130 mutex_unlock(&pinctrldev_list_mutex);
131 return pctldev;
132 }
133
Daniel Mackd463f822013-04-26 18:57:02 +0200134 mutex_unlock(&pinctrldev_list_mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200135
136 return NULL;
137}
138
Linus Walleij2744e8a2011-05-02 20:50:54 +0200139/**
Linus Walleijae6b4d82011-10-19 18:14:33 +0200140 * pin_get_from_name() - look up a pin number from a name
141 * @pctldev: the pin control device to lookup the pin on
142 * @name: the name of the pin to look up
143 */
144int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
145{
Chanho Park706e8522012-01-03 16:47:50 +0900146 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200147
Chanho Park706e8522012-01-03 16:47:50 +0900148 /* The pin number can be retrived from the pin controller descriptor */
149 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200150 struct pin_desc *desc;
151
Chanho Park706e8522012-01-03 16:47:50 +0900152 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200153 desc = pin_desc_get(pctldev, pin);
154 /* Pin space may be sparse */
Axel Lin6c325f82013-08-19 10:06:29 +0800155 if (desc && !strcmp(name, desc->name))
Linus Walleijae6b4d82011-10-19 18:14:33 +0200156 return pin;
157 }
158
159 return -EINVAL;
160}
161
162/**
Dong Aishengdcb5dbc2012-04-17 15:00:46 +0800163 * pin_get_name_from_id() - look up a pin name from a pin id
164 * @pctldev: the pin control device to lookup the pin on
165 * @name: the name of the pin to look up
166 */
167const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
168{
169 const struct pin_desc *desc;
170
171 desc = pin_desc_get(pctldev, pin);
Markus Elfringcea234e2017-05-02 11:04:55 +0200172 if (!desc) {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +0800173 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
174 pin);
175 return NULL;
176 }
177
178 return desc->name;
179}
180
181/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200182 * pin_is_valid() - check if pin exists on controller
183 * @pctldev: the pin control device to check the pin on
184 * @pin: pin to check, use the local pin controller index number
185 *
186 * This tells us whether a certain pin exist on a certain pin controller or
187 * not. Pin lists may be sparse, so some pins may not exist.
188 */
189bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
190{
191 struct pin_desc *pindesc;
192
193 if (pin < 0)
194 return false;
195
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200196 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200197 pindesc = pin_desc_get(pctldev, pin);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200198 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200199
Stephen Warren57b676f2012-03-02 13:05:44 -0700200 return pindesc != NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200201}
202EXPORT_SYMBOL_GPL(pin_is_valid);
203
204/* Deletes a range of pin descriptors */
205static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
206 const struct pinctrl_pin_desc *pins,
207 unsigned num_pins)
208{
209 int i;
210
Linus Walleij2744e8a2011-05-02 20:50:54 +0200211 for (i = 0; i < num_pins; i++) {
212 struct pin_desc *pindesc;
213
214 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
215 pins[i].number);
Markus Elfringcea234e2017-05-02 11:04:55 +0200216 if (pindesc) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200217 radix_tree_delete(&pctldev->pin_desc_tree,
218 pins[i].number);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100219 if (pindesc->dynamic_name)
220 kfree(pindesc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200221 }
222 kfree(pindesc);
223 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200224}
225
226static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900227 const struct pinctrl_pin_desc *pin)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200228{
229 struct pin_desc *pindesc;
230
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900231 pindesc = pin_desc_get(pctldev, pin->number);
Markus Elfringcea234e2017-05-02 11:04:55 +0200232 if (pindesc) {
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900233 dev_err(pctldev->dev, "pin %d already registered\n",
234 pin->number);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200235 return -EINVAL;
236 }
237
238 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -0800239 if (!pindesc)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200240 return -ENOMEM;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200241
Linus Walleij2744e8a2011-05-02 20:50:54 +0200242 /* Set owner */
243 pindesc->pctldev = pctldev;
244
Stephen Warren9af1e442011-10-19 16:19:27 -0600245 /* Copy basic pin info */
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900246 if (pin->name) {
247 pindesc->name = pin->name;
Linus Walleijca53c5f2011-12-14 20:33:37 +0100248 } else {
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900249 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", pin->number);
Markus Elfringcea234e2017-05-02 11:04:55 +0200250 if (!pindesc->name) {
Sachin Kamateb26cc92012-09-25 12:41:40 +0530251 kfree(pindesc);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100252 return -ENOMEM;
Sachin Kamateb26cc92012-09-25 12:41:40 +0530253 }
Linus Walleijca53c5f2011-12-14 20:33:37 +0100254 pindesc->dynamic_name = true;
255 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200256
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900257 pindesc->drv_data = pin->drv_data;
258
259 radix_tree_insert(&pctldev->pin_desc_tree, pin->number, pindesc);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200260 pr_debug("registered pin %d (%s) on %s\n",
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900261 pin->number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200262 return 0;
263}
264
265static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900266 const struct pinctrl_pin_desc *pins,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200267 unsigned num_descs)
268{
269 unsigned i;
270 int ret = 0;
271
272 for (i = 0; i < num_descs; i++) {
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900273 ret = pinctrl_register_one_pin(pctldev, &pins[i]);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200274 if (ret)
275 return ret;
276 }
277
278 return 0;
279}
280
281/**
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200282 * gpio_to_pin() - GPIO range GPIO number to pin number translation
283 * @range: GPIO range used for the translation
284 * @gpio: gpio pin to translate to a pin number
285 *
286 * Finds the pin number for a given GPIO using the specified GPIO range
287 * as a base for translation. The distinction between linear GPIO ranges
288 * and pin list based GPIO ranges is managed correctly by this function.
289 *
290 * This function assumes the gpio is part of the specified GPIO range, use
291 * only after making sure this is the case (e.g. by calling it on the
292 * result of successful pinctrl_get_device_gpio_range calls)!
293 */
294static inline int gpio_to_pin(struct pinctrl_gpio_range *range,
295 unsigned int gpio)
296{
297 unsigned int offset = gpio - range->base;
298 if (range->pins)
299 return range->pins[offset];
300 else
301 return range->pin_base + offset;
302}
303
304/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200305 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
306 * @pctldev: pin controller device to check
307 * @gpio: gpio pin to check taken from the global GPIO pin space
308 *
309 * Tries to match a GPIO pin number to the ranges handled by a certain pin
310 * controller, return the range or NULL
311 */
312static struct pinctrl_gpio_range *
313pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
314{
315 struct pinctrl_gpio_range *range = NULL;
316
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200317 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200318 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200319 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
320 /* Check if we're in the valid range */
321 if (gpio >= range->base &&
322 gpio < range->base + range->npins) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200323 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200324 return range;
325 }
326 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200327 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200328 return NULL;
329}
330
331/**
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800332 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
333 * the same GPIO chip are in range
334 * @gpio: gpio pin to check taken from the global GPIO pin space
335 *
336 * This function is complement of pinctrl_match_gpio_range(). If the return
337 * value of pinctrl_match_gpio_range() is NULL, this function could be used
338 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
339 * of the same GPIO chip don't have back-end pinctrl interface.
340 * If the return value is true, it means that pinctrl device is ready & the
341 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
342 * is false, it means that pinctrl device may not be ready.
343 */
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800344#ifdef CONFIG_GPIOLIB
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800345static bool pinctrl_ready_for_gpio_range(unsigned gpio)
346{
347 struct pinctrl_dev *pctldev;
348 struct pinctrl_gpio_range *range = NULL;
349 struct gpio_chip *chip = gpio_to_chip(gpio);
350
Tony Lindgren942cde72015-09-03 10:34:30 -0700351 if (WARN(!chip, "no gpio_chip for gpio%i?", gpio))
352 return false;
353
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200354 mutex_lock(&pinctrldev_list_mutex);
355
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800356 /* Loop over the pin controllers */
357 list_for_each_entry(pctldev, &pinctrldev_list, node) {
358 /* Loop over the ranges */
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800359 mutex_lock(&pctldev->mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800360 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
361 /* Check if any gpio range overlapped with gpio chip */
362 if (range->base + range->npins - 1 < chip->base ||
363 range->base > chip->base + chip->ngpio - 1)
364 continue;
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800365 mutex_unlock(&pctldev->mutex);
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200366 mutex_unlock(&pinctrldev_list_mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800367 return true;
368 }
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800369 mutex_unlock(&pctldev->mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800370 }
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200371
372 mutex_unlock(&pinctrldev_list_mutex);
373
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800374 return false;
375}
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800376#else
377static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
378#endif
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800379
380/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200381 * pinctrl_get_device_gpio_range() - find device for GPIO range
382 * @gpio: the pin to locate the pin controller for
383 * @outdev: the pin control device if found
384 * @outrange: the GPIO range if found
385 *
386 * Find the pin controller handling a certain GPIO pin from the pinspace of
387 * the GPIO subsystem, return the device and the matching GPIO range. Returns
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800388 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
389 * may still have not been registered.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200390 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700391static int pinctrl_get_device_gpio_range(unsigned gpio,
392 struct pinctrl_dev **outdev,
393 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200394{
395 struct pinctrl_dev *pctldev = NULL;
396
Axel Linf0059022013-08-18 20:34:22 +0800397 mutex_lock(&pinctrldev_list_mutex);
398
Linus Walleij2744e8a2011-05-02 20:50:54 +0200399 /* Loop over the pin controllers */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200400 list_for_each_entry(pctldev, &pinctrldev_list, node) {
401 struct pinctrl_gpio_range *range;
402
403 range = pinctrl_match_gpio_range(pctldev, gpio);
Markus Elfringcea234e2017-05-02 11:04:55 +0200404 if (range) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200405 *outdev = pctldev;
406 *outrange = range;
Axel Linf0059022013-08-18 20:34:22 +0800407 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200408 return 0;
409 }
410 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200411
Axel Linf0059022013-08-18 20:34:22 +0800412 mutex_unlock(&pinctrldev_list_mutex);
413
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800414 return -EPROBE_DEFER;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200415}
416
417/**
418 * pinctrl_add_gpio_range() - register a GPIO range for a controller
419 * @pctldev: pin controller device to add the range to
420 * @range: the GPIO range to add
421 *
422 * This adds a range of GPIOs to be handled by a certain pin controller. Call
423 * this to register handled ranges after registering your pin controller.
424 */
425void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
426 struct pinctrl_gpio_range *range)
427{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200428 mutex_lock(&pctldev->mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700429 list_add_tail(&range->node, &pctldev->gpio_ranges);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200430 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200431}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700432EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200433
Dong Aisheng3e5e00b2012-05-23 21:22:41 +0800434void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
435 struct pinctrl_gpio_range *ranges,
436 unsigned nranges)
437{
438 int i;
439
440 for (i = 0; i < nranges; i++)
441 pinctrl_add_gpio_range(pctldev, &ranges[i]);
442}
443EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
444
Linus Walleij192c3692012-11-20 14:03:37 +0100445struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530446 struct pinctrl_gpio_range *range)
447{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200448 struct pinctrl_dev *pctldev;
449
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200450 pctldev = get_pinctrl_dev_from_devname(devname);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530451
Linus Walleijdfa97512012-11-20 14:54:18 +0100452 /*
453 * If we can't find this device, let's assume that is because
454 * it has not probed yet, so the driver trying to register this
455 * range need to defer probing.
456 */
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200457 if (!pctldev) {
Linus Walleijdfa97512012-11-20 14:54:18 +0100458 return ERR_PTR(-EPROBE_DEFER);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200459 }
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530460 pinctrl_add_gpio_range(pctldev, range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200461
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530462 return pctldev;
463}
Linus Walleij192c3692012-11-20 14:03:37 +0100464EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530465
Christian Ruppert586a87e2013-10-15 15:37:54 +0200466int pinctrl_get_group_pins(struct pinctrl_dev *pctldev, const char *pin_group,
467 const unsigned **pins, unsigned *num_pins)
468{
469 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
470 int gs;
471
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +0200472 if (!pctlops->get_group_pins)
473 return -EINVAL;
474
Christian Ruppert586a87e2013-10-15 15:37:54 +0200475 gs = pinctrl_get_group_selector(pctldev, pin_group);
476 if (gs < 0)
477 return gs;
478
479 return pctlops->get_group_pins(pctldev, gs, pins, num_pins);
480}
481EXPORT_SYMBOL_GPL(pinctrl_get_group_pins);
482
Joachim Eastwoodb18537c2016-02-25 22:44:37 +0100483struct pinctrl_gpio_range *
484pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev *pctldev,
485 unsigned int pin)
486{
487 struct pinctrl_gpio_range *range;
488
489 /* Loop over the ranges */
490 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
491 /* Check if we're in the valid range */
492 if (range->pins) {
493 int a;
494 for (a = 0; a < range->npins; a++) {
495 if (range->pins[a] == pin)
496 return range;
497 }
498 } else if (pin >= range->pin_base &&
499 pin < range->pin_base + range->npins)
500 return range;
501 }
502
503 return NULL;
504}
505EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin_nolock);
506
Linus Walleij2744e8a2011-05-02 20:50:54 +0200507/**
Linus Walleij9afbefb2012-11-20 14:25:07 +0100508 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
509 * @pctldev: the pin controller device to look in
510 * @pin: a controller-local number to find the range for
511 */
512struct pinctrl_gpio_range *
513pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
514 unsigned int pin)
515{
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800516 struct pinctrl_gpio_range *range;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100517
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200518 mutex_lock(&pctldev->mutex);
Joachim Eastwoodb18537c2016-02-25 22:44:37 +0100519 range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200520 mutex_unlock(&pctldev->mutex);
Joachim Eastwoodb18537c2016-02-25 22:44:37 +0100521
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800522 return range;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100523}
524EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
525
526/**
Charles Keepax50842cb2017-02-13 10:11:03 +0000527 * pinctrl_remove_gpio_range() - remove a range of GPIOs from a pin controller
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530528 * @pctldev: pin controller device to remove the range from
529 * @range: the GPIO range to remove
530 */
531void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
532 struct pinctrl_gpio_range *range)
533{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200534 mutex_lock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530535 list_del(&range->node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200536 mutex_unlock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530537}
538EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
539
Linus Walleijc033a712016-12-30 15:04:43 +0100540#ifdef CONFIG_GENERIC_PINCTRL_GROUPS
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800541
542/**
543 * pinctrl_generic_get_group_count() - returns the number of pin groups
544 * @pctldev: pin controller device
545 */
546int pinctrl_generic_get_group_count(struct pinctrl_dev *pctldev)
547{
548 return pctldev->num_groups;
549}
550EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_count);
551
552/**
553 * pinctrl_generic_get_group_name() - returns the name of a pin group
554 * @pctldev: pin controller device
555 * @selector: group number
556 */
557const char *pinctrl_generic_get_group_name(struct pinctrl_dev *pctldev,
558 unsigned int selector)
559{
560 struct group_desc *group;
561
562 group = radix_tree_lookup(&pctldev->pin_group_tree,
563 selector);
564 if (!group)
565 return NULL;
566
567 return group->name;
568}
569EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_name);
570
571/**
572 * pinctrl_generic_get_group_pins() - gets the pin group pins
573 * @pctldev: pin controller device
574 * @selector: group number
575 * @pins: pins in the group
576 * @num_pins: number of pins in the group
577 */
578int pinctrl_generic_get_group_pins(struct pinctrl_dev *pctldev,
579 unsigned int selector,
580 const unsigned int **pins,
581 unsigned int *num_pins)
582{
583 struct group_desc *group;
584
585 group = radix_tree_lookup(&pctldev->pin_group_tree,
586 selector);
587 if (!group) {
588 dev_err(pctldev->dev, "%s could not find pingroup%i\n",
589 __func__, selector);
590 return -EINVAL;
591 }
592
593 *pins = group->pins;
594 *num_pins = group->num_pins;
595
596 return 0;
597}
598EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_pins);
599
600/**
601 * pinctrl_generic_get_group() - returns a pin group based on the number
602 * @pctldev: pin controller device
603 * @gselector: group number
604 */
605struct group_desc *pinctrl_generic_get_group(struct pinctrl_dev *pctldev,
606 unsigned int selector)
607{
608 struct group_desc *group;
609
610 group = radix_tree_lookup(&pctldev->pin_group_tree,
611 selector);
612 if (!group)
613 return NULL;
614
615 return group;
616}
617EXPORT_SYMBOL_GPL(pinctrl_generic_get_group);
618
Tony Lindgrena2037282018-07-05 02:10:14 -0700619static int pinctrl_generic_group_name_to_selector(struct pinctrl_dev *pctldev,
620 const char *function)
621{
622 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
623 int ngroups = ops->get_groups_count(pctldev);
624 int selector = 0;
625
626 /* See if this pctldev has this group */
627 while (selector < ngroups) {
628 const char *gname = ops->get_group_name(pctldev, selector);
629
Yanjiang Jin54a58182018-09-29 17:06:55 +0800630 if (gname && !strcmp(function, gname))
Tony Lindgrena2037282018-07-05 02:10:14 -0700631 return selector;
632
633 selector++;
634 }
635
636 return -EINVAL;
637}
638
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800639/**
640 * pinctrl_generic_add_group() - adds a new pin group
641 * @pctldev: pin controller device
642 * @name: name of the pin group
643 * @pins: pins in the pin group
644 * @num_pins: number of pins in the pin group
645 * @data: pin controller driver specific data
646 *
647 * Note that the caller must take care of locking.
648 */
649int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name,
650 int *pins, int num_pins, void *data)
651{
652 struct group_desc *group;
Tony Lindgrena2037282018-07-05 02:10:14 -0700653 int selector;
654
655 if (!name)
656 return -EINVAL;
657
658 selector = pinctrl_generic_group_name_to_selector(pctldev, name);
659 if (selector >= 0)
660 return selector;
661
662 selector = pctldev->num_groups;
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800663
664 group = devm_kzalloc(pctldev->dev, sizeof(*group), GFP_KERNEL);
665 if (!group)
666 return -ENOMEM;
667
668 group->name = name;
669 group->pins = pins;
670 group->num_pins = num_pins;
671 group->data = data;
672
Tony Lindgrena2037282018-07-05 02:10:14 -0700673 radix_tree_insert(&pctldev->pin_group_tree, selector, group);
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800674
675 pctldev->num_groups++;
676
Tony Lindgrena2037282018-07-05 02:10:14 -0700677 return selector;
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800678}
679EXPORT_SYMBOL_GPL(pinctrl_generic_add_group);
680
681/**
682 * pinctrl_generic_remove_group() - removes a numbered pin group
683 * @pctldev: pin controller device
684 * @selector: group number
685 *
686 * Note that the caller must take care of locking.
687 */
688int pinctrl_generic_remove_group(struct pinctrl_dev *pctldev,
689 unsigned int selector)
690{
691 struct group_desc *group;
692
693 group = radix_tree_lookup(&pctldev->pin_group_tree,
694 selector);
695 if (!group)
696 return -ENOENT;
697
698 radix_tree_delete(&pctldev->pin_group_tree, selector);
699 devm_kfree(pctldev->dev, group);
700
701 pctldev->num_groups--;
702
703 return 0;
704}
705EXPORT_SYMBOL_GPL(pinctrl_generic_remove_group);
706
707/**
708 * pinctrl_generic_free_groups() - removes all pin groups
709 * @pctldev: pin controller device
710 *
Tony Lindgren664b7c42017-05-12 08:47:57 -0700711 * Note that the caller must take care of locking. The pinctrl groups
712 * are allocated with devm_kzalloc() so no need to free them here.
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800713 */
714static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
715{
716 struct radix_tree_iter iter;
Masahiro Yamada906a2a32017-08-04 13:52:05 +0900717 void __rcu **slot;
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800718
719 radix_tree_for_each_slot(slot, &pctldev->pin_group_tree, &iter, 0)
Tony Lindgren664b7c42017-05-12 08:47:57 -0700720 radix_tree_delete(&pctldev->pin_group_tree, iter.index);
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800721
722 pctldev->num_groups = 0;
723}
724
725#else
726static inline void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
727{
728}
Linus Walleijc033a712016-12-30 15:04:43 +0100729#endif /* CONFIG_GENERIC_PINCTRL_GROUPS */
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800730
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530731/**
Linus Walleij7afde8b2011-10-19 17:07:16 +0200732 * pinctrl_get_group_selector() - returns the group selector for a group
733 * @pctldev: the pin controller handling the group
734 * @pin_group: the pin group to look up
735 */
736int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
737 const char *pin_group)
738{
739 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530740 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200741 unsigned group_selector = 0;
742
Viresh Kumard1e90e92012-03-30 11:25:40 +0530743 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200744 const char *gname = pctlops->get_group_name(pctldev,
745 group_selector);
Yanjiang Jin54a58182018-09-29 17:06:55 +0800746 if (gname && !strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700747 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200748 "found group selector %u for %s\n",
749 group_selector,
750 pin_group);
751 return group_selector;
752 }
753
754 group_selector++;
755 }
756
Stephen Warren51cd24e2011-12-09 16:59:05 -0700757 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200758 pin_group);
759
760 return -EINVAL;
761}
762
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100763/**
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200764 * pinctrl_gpio_request() - request a single pin to be used as GPIO
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100765 * @gpio: the GPIO pin number from the GPIO subsystem number space
766 *
767 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
768 * as part of their gpio_request() semantics, platforms and individual drivers
769 * shall *NOT* request GPIO pins to be muxed in.
770 */
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200771int pinctrl_gpio_request(unsigned gpio)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100772{
773 struct pinctrl_dev *pctldev;
774 struct pinctrl_gpio_range *range;
775 int ret;
776 int pin;
777
778 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700779 if (ret) {
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800780 if (pinctrl_ready_for_gpio_range(gpio))
781 ret = 0;
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800782 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700783 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100784
Axel Lin9b77ace42013-08-19 10:07:46 +0800785 mutex_lock(&pctldev->mutex);
786
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100787 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200788 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100789
Stephen Warren57b676f2012-03-02 13:05:44 -0700790 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
791
Axel Lin9b77ace42013-08-19 10:07:46 +0800792 mutex_unlock(&pctldev->mutex);
793
Stephen Warren57b676f2012-03-02 13:05:44 -0700794 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100795}
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200796EXPORT_SYMBOL_GPL(pinctrl_gpio_request);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100797
798/**
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200799 * pinctrl_gpio_free() - free control on a single pin, currently used as GPIO
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100800 * @gpio: the GPIO pin number from the GPIO subsystem number space
801 *
802 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
803 * as part of their gpio_free() semantics, platforms and individual drivers
804 * shall *NOT* request GPIO pins to be muxed out.
805 */
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200806void pinctrl_gpio_free(unsigned gpio)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100807{
808 struct pinctrl_dev *pctldev;
809 struct pinctrl_gpio_range *range;
810 int ret;
811 int pin;
812
813 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700814 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100815 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700816 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200817 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100818
819 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200820 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100821
Stephen Warren57b676f2012-03-02 13:05:44 -0700822 pinmux_free_gpio(pctldev, pin, range);
823
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200824 mutex_unlock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100825}
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200826EXPORT_SYMBOL_GPL(pinctrl_gpio_free);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100827
828static int pinctrl_gpio_direction(unsigned gpio, bool input)
829{
830 struct pinctrl_dev *pctldev;
831 struct pinctrl_gpio_range *range;
832 int ret;
833 int pin;
834
835 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200836 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100837 return ret;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200838 }
839
840 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100841
842 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200843 pin = gpio_to_pin(range, gpio);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200844 ret = pinmux_gpio_direction(pctldev, range, pin, input);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100845
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200846 mutex_unlock(&pctldev->mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200847
848 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100849}
850
851/**
852 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
853 * @gpio: the GPIO pin number from the GPIO subsystem number space
854 *
855 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
856 * as part of their gpio_direction_input() semantics, platforms and individual
857 * drivers shall *NOT* touch pin control GPIO calls.
858 */
859int pinctrl_gpio_direction_input(unsigned gpio)
860{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200861 return pinctrl_gpio_direction(gpio, true);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100862}
863EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
864
865/**
866 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
867 * @gpio: the GPIO pin number from the GPIO subsystem number space
868 *
869 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
870 * as part of their gpio_direction_output() semantics, platforms and individual
871 * drivers shall *NOT* touch pin control GPIO calls.
872 */
873int pinctrl_gpio_direction_output(unsigned gpio)
874{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200875 return pinctrl_gpio_direction(gpio, false);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100876}
877EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
878
Mika Westerberg15381bc2017-01-23 15:34:33 +0300879/**
880 * pinctrl_gpio_set_config() - Apply config to given GPIO pin
881 * @gpio: the GPIO pin number from the GPIO subsystem number space
882 * @config: the configuration to apply to the GPIO
883 *
884 * This function should *ONLY* be used from gpiolib-based GPIO drivers, if
885 * they need to call the underlying pin controller to change GPIO config
886 * (for example set debounce time).
887 */
888int pinctrl_gpio_set_config(unsigned gpio, unsigned long config)
889{
890 unsigned long configs[] = { config };
891 struct pinctrl_gpio_range *range;
892 struct pinctrl_dev *pctldev;
893 int ret, pin;
894
895 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
896 if (ret)
897 return ret;
898
899 mutex_lock(&pctldev->mutex);
900 pin = gpio_to_pin(range, gpio);
901 ret = pinconf_set_config(pctldev, pin, configs, ARRAY_SIZE(configs));
902 mutex_unlock(&pctldev->mutex);
903
904 return ret;
905}
906EXPORT_SYMBOL_GPL(pinctrl_gpio_set_config);
907
Stephen Warren6e5e9592012-03-02 13:05:47 -0700908static struct pinctrl_state *find_state(struct pinctrl *p,
909 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100910{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700911 struct pinctrl_state *state;
912
913 list_for_each_entry(state, &p->states, node)
914 if (!strcmp(state->name, name))
915 return state;
916
917 return NULL;
918}
919
920static struct pinctrl_state *create_state(struct pinctrl *p,
921 const char *name)
922{
923 struct pinctrl_state *state;
924
925 state = kzalloc(sizeof(*state), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -0800926 if (!state)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700927 return ERR_PTR(-ENOMEM);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700928
929 state->name = name;
930 INIT_LIST_HEAD(&state->settings);
931
932 list_add_tail(&state->node, &p->states);
933
934 return state;
935}
936
Tony Lindgren99e4f672016-12-27 09:19:59 -0800937static int add_setting(struct pinctrl *p, struct pinctrl_dev *pctldev,
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900938 const struct pinctrl_map *map)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700939{
940 struct pinctrl_state *state;
941 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700942 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700943
944 state = find_state(p, map->name);
945 if (!state)
946 state = create_state(p, map->name);
947 if (IS_ERR(state))
948 return PTR_ERR(state);
949
Stephen Warren1e2082b2012-03-02 13:05:48 -0700950 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
951 return 0;
952
Stephen Warren6e5e9592012-03-02 13:05:47 -0700953 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -0800954 if (!setting)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700955 return -ENOMEM;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700956
Stephen Warren1e2082b2012-03-02 13:05:48 -0700957 setting->type = map->type;
958
Tony Lindgren99e4f672016-12-27 09:19:59 -0800959 if (pctldev)
960 setting->pctldev = pctldev;
961 else
962 setting->pctldev =
963 get_pinctrl_dev_from_devname(map->ctrl_dev_name);
Markus Elfringcea234e2017-05-02 11:04:55 +0200964 if (!setting->pctldev) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700965 kfree(setting);
Linus Walleij89216492012-12-11 14:14:32 +0100966 /* Do not defer probing of hogs (circular loop) */
967 if (!strcmp(map->ctrl_dev_name, map->dev_name))
968 return -ENODEV;
Linus Walleijc05127c2012-04-10 10:00:38 +0200969 /*
970 * OK let us guess that the driver is not there yet, and
971 * let's defer obtaining this pinctrl handle to later...
972 */
Linus Walleij89216492012-12-11 14:14:32 +0100973 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
974 map->ctrl_dev_name);
Linus Walleijc05127c2012-04-10 10:00:38 +0200975 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700976 }
977
Linus Walleij1a789582012-10-17 20:51:54 +0200978 setting->dev_name = map->dev_name;
979
Stephen Warren1e2082b2012-03-02 13:05:48 -0700980 switch (map->type) {
981 case PIN_MAP_TYPE_MUX_GROUP:
982 ret = pinmux_map_to_setting(map, setting);
983 break;
984 case PIN_MAP_TYPE_CONFIGS_PIN:
985 case PIN_MAP_TYPE_CONFIGS_GROUP:
986 ret = pinconf_map_to_setting(map, setting);
987 break;
988 default:
989 ret = -EINVAL;
990 break;
991 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700992 if (ret < 0) {
993 kfree(setting);
994 return ret;
995 }
996
997 list_add_tail(&setting->node, &state->settings);
998
999 return 0;
1000}
1001
1002static struct pinctrl *find_pinctrl(struct device *dev)
1003{
1004 struct pinctrl *p;
1005
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001006 mutex_lock(&pinctrl_list_mutex);
Stephen Warren1e2082b2012-03-02 13:05:48 -07001007 list_for_each_entry(p, &pinctrl_list, node)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001008 if (p->dev == dev) {
1009 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001010 return p;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001011 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001012
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001013 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001014 return NULL;
1015}
1016
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001017static void pinctrl_free(struct pinctrl *p, bool inlist);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001018
Tony Lindgren99e4f672016-12-27 09:19:59 -08001019static struct pinctrl *create_pinctrl(struct device *dev,
1020 struct pinctrl_dev *pctldev)
Stephen Warren6e5e9592012-03-02 13:05:47 -07001021{
1022 struct pinctrl *p;
1023 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001024 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001025 int i;
Masahiro Yamada3f713b72017-08-04 11:22:31 +09001026 const struct pinctrl_map *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001027 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001028
1029 /*
1030 * create the state cookie holder struct pinctrl for each
1031 * mapping, this is what consumers will get when requesting
1032 * a pin control handle with pinctrl_get()
1033 */
Stephen Warren02f5b982012-02-22 14:26:00 -07001034 p = kzalloc(sizeof(*p), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -08001035 if (!p)
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001036 return ERR_PTR(-ENOMEM);
Stephen Warren7ecdb162012-03-02 13:05:45 -07001037 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001038 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -06001039 INIT_LIST_HEAD(&p->dt_maps);
1040
Tony Lindgren99e4f672016-12-27 09:19:59 -08001041 ret = pinctrl_dt_to_map(p, pctldev);
Stephen Warren57291ce2012-03-23 10:29:46 -06001042 if (ret < 0) {
1043 kfree(p);
1044 return ERR_PTR(ret);
1045 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001046
1047 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001048
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001049 mutex_lock(&pinctrl_maps_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001050 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001051 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -07001052 /* Map must be for this device */
1053 if (strcmp(map->dev_name, devname))
1054 continue;
Nikita Yushchenko7f0ff062017-05-11 23:02:11 +03001055 /*
1056 * If pctldev is not null, we are claiming hog for it,
1057 * that means, setting that is served by pctldev by itself.
1058 *
1059 * Thus we must skip map that is for this device but is served
1060 * by other device.
1061 */
1062 if (pctldev &&
1063 strcmp(dev_name(pctldev->dev), map->ctrl_dev_name))
1064 continue;
Stephen Warren1681f5a2012-02-22 14:25:58 -07001065
Tony Lindgren99e4f672016-12-27 09:19:59 -08001066 ret = add_setting(p, pctldev, map);
Linus Walleij89216492012-12-11 14:14:32 +01001067 /*
1068 * At this point the adding of a setting may:
1069 *
1070 * - Defer, if the pinctrl device is not yet available
1071 * - Fail, if the pinctrl device is not yet available,
1072 * AND the setting is a hog. We cannot defer that, since
1073 * the hog will kick in immediately after the device
1074 * is registered.
1075 *
1076 * If the error returned was not -EPROBE_DEFER then we
1077 * accumulate the errors to see if we end up with
1078 * an -EPROBE_DEFER later, as that is the worst case.
1079 */
1080 if (ret == -EPROBE_DEFER) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001081 pinctrl_free(p, false);
1082 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001083 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001084 }
1085 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001086 mutex_unlock(&pinctrl_maps_mutex);
1087
Linus Walleij89216492012-12-11 14:14:32 +01001088 if (ret < 0) {
Andy Shevchenko3ec440e2017-02-28 16:59:56 +02001089 /* If some other error than deferral occurred, return here */
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001090 pinctrl_free(p, false);
Linus Walleij89216492012-12-11 14:14:32 +01001091 return ERR_PTR(ret);
1092 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001093
Linus Walleijab780292013-01-22 10:56:14 -07001094 kref_init(&p->users);
1095
Linus Walleijb0666ba2012-12-11 13:12:35 +01001096 /* Add the pinctrl handle to the global list */
Stanislaw Gruszka7b320cb2014-02-04 09:07:09 +01001097 mutex_lock(&pinctrl_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -07001098 list_add_tail(&p->node, &pinctrl_list);
Stanislaw Gruszka7b320cb2014-02-04 09:07:09 +01001099 mutex_unlock(&pinctrl_list_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001100
1101 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001102}
Stephen Warren7ecdb162012-03-02 13:05:45 -07001103
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001104/**
1105 * pinctrl_get() - retrieves the pinctrl handle for a device
1106 * @dev: the device to obtain the handle for
1107 */
1108struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warren6e5e9592012-03-02 13:05:47 -07001109{
1110 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001111
Stephen Warren6e5e9592012-03-02 13:05:47 -07001112 if (WARN_ON(!dev))
1113 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -07001114
Linus Walleijab780292013-01-22 10:56:14 -07001115 /*
1116 * See if somebody else (such as the device core) has already
1117 * obtained a handle to the pinctrl for this device. In that case,
1118 * return another pointer to it.
1119 */
Stephen Warren6e5e9592012-03-02 13:05:47 -07001120 p = find_pinctrl(dev);
Markus Elfringcea234e2017-05-02 11:04:55 +02001121 if (p) {
Linus Walleijab780292013-01-22 10:56:14 -07001122 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
1123 kref_get(&p->users);
1124 return p;
1125 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001126
Tony Lindgren99e4f672016-12-27 09:19:59 -08001127 return create_pinctrl(dev, NULL);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001128}
1129EXPORT_SYMBOL_GPL(pinctrl_get);
1130
Richard Genoudd3cee8302013-03-25 15:47:21 +01001131static void pinctrl_free_setting(bool disable_setting,
1132 struct pinctrl_setting *setting)
1133{
1134 switch (setting->type) {
1135 case PIN_MAP_TYPE_MUX_GROUP:
1136 if (disable_setting)
1137 pinmux_disable_setting(setting);
1138 pinmux_free_setting(setting);
1139 break;
1140 case PIN_MAP_TYPE_CONFIGS_PIN:
1141 case PIN_MAP_TYPE_CONFIGS_GROUP:
1142 pinconf_free_setting(setting);
1143 break;
1144 default:
1145 break;
1146 }
1147}
1148
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001149static void pinctrl_free(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -07001150{
Stephen Warren6e5e9592012-03-02 13:05:47 -07001151 struct pinctrl_state *state, *n1;
1152 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001153
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001154 mutex_lock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001155 list_for_each_entry_safe(state, n1, &p->states, node) {
1156 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Richard Genoudd3cee8302013-03-25 15:47:21 +01001157 pinctrl_free_setting(state == p->state, setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001158 list_del(&setting->node);
1159 kfree(setting);
1160 }
1161 list_del(&state->node);
1162 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -07001163 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001164
Stephen Warren57291ce2012-03-23 10:29:46 -06001165 pinctrl_dt_free_maps(p);
1166
Stephen Warren6e5e9592012-03-02 13:05:47 -07001167 if (inlist)
1168 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -07001169 kfree(p);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001170 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001171}
1172
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001173/**
Linus Walleijab780292013-01-22 10:56:14 -07001174 * pinctrl_release() - release the pinctrl handle
1175 * @kref: the kref in the pinctrl being released
1176 */
Sachin Kamat2917e832013-01-24 15:01:00 +05301177static void pinctrl_release(struct kref *kref)
Linus Walleijab780292013-01-22 10:56:14 -07001178{
1179 struct pinctrl *p = container_of(kref, struct pinctrl, users);
1180
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001181 pinctrl_free(p, true);
Linus Walleijab780292013-01-22 10:56:14 -07001182}
1183
1184/**
1185 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
Stephen Warren6e5e9592012-03-02 13:05:47 -07001186 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001187 */
1188void pinctrl_put(struct pinctrl *p)
1189{
Linus Walleijab780292013-01-22 10:56:14 -07001190 kref_put(&p->users, pinctrl_release);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001191}
1192EXPORT_SYMBOL_GPL(pinctrl_put);
1193
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001194/**
1195 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
1196 * @p: the pinctrl handle to retrieve the state from
1197 * @name: the state name to retrieve
1198 */
1199struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
1200 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -07001201{
Stephen Warren6e5e9592012-03-02 13:05:47 -07001202 struct pinctrl_state *state;
1203
1204 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +08001205 if (!state) {
1206 if (pinctrl_dummy_state) {
1207 /* create dummy state */
1208 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
1209 name);
1210 state = create_state(p, name);
Richard Genoudd599bfb2012-08-10 16:53:49 +02001211 } else
1212 state = ERR_PTR(-ENODEV);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +08001213 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001214
1215 return state;
1216}
Stephen Warren6e5e9592012-03-02 13:05:47 -07001217EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
1218
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001219/**
Florian Fainelli981ed1b2017-03-01 10:32:57 -08001220 * pinctrl_commit_state() - select/activate/program a pinctrl state to HW
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001221 * @p: the pinctrl handle for the device that requests configuration
1222 * @state: the state handle to select/activate/program
1223 */
Florian Fainelli981ed1b2017-03-01 10:32:57 -08001224static int pinctrl_commit_state(struct pinctrl *p, struct pinctrl_state *state)
Stephen Warren6e5e9592012-03-02 13:05:47 -07001225{
1226 struct pinctrl_setting *setting, *setting2;
Richard Genoud50cf7c82013-03-25 15:47:23 +01001227 struct pinctrl_state *old_state = p->state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001228 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -07001229
Stephen Warren6e5e9592012-03-02 13:05:47 -07001230 if (p->state) {
1231 /*
Fan Wu2243a872014-06-09 09:37:56 +08001232 * For each pinmux setting in the old state, forget SW's record
1233 * of mux owner for that pingroup. Any pingroups which are
1234 * still owned by the new state will be re-acquired by the call
1235 * to pinmux_enable_setting() in the loop below.
Stephen Warren6e5e9592012-03-02 13:05:47 -07001236 */
1237 list_for_each_entry(setting, &p->state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001238 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
1239 continue;
Fan Wu2243a872014-06-09 09:37:56 +08001240 pinmux_disable_setting(setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001241 }
1242 }
1243
Richard Genoud3102a762013-03-25 15:47:22 +01001244 p->state = NULL;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001245
1246 /* Apply all the settings for the new state */
1247 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001248 switch (setting->type) {
1249 case PIN_MAP_TYPE_MUX_GROUP:
1250 ret = pinmux_enable_setting(setting);
1251 break;
1252 case PIN_MAP_TYPE_CONFIGS_PIN:
1253 case PIN_MAP_TYPE_CONFIGS_GROUP:
1254 ret = pinconf_apply_setting(setting);
1255 break;
1256 default:
1257 ret = -EINVAL;
1258 break;
1259 }
Richard Genoud3102a762013-03-25 15:47:22 +01001260
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001261 if (ret < 0) {
Richard Genoud3102a762013-03-25 15:47:22 +01001262 goto unapply_new_state;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001263 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001264 }
1265
Richard Genoud3102a762013-03-25 15:47:22 +01001266 p->state = state;
1267
Stephen Warren7ecdb162012-03-02 13:05:45 -07001268 return 0;
Richard Genoud3102a762013-03-25 15:47:22 +01001269
1270unapply_new_state:
Richard Genoudda587512013-03-28 12:55:46 +01001271 dev_err(p->dev, "Error applying setting, reverse things back\n");
Richard Genoud3102a762013-03-25 15:47:22 +01001272
Richard Genoud3102a762013-03-25 15:47:22 +01001273 list_for_each_entry(setting2, &state->settings, node) {
1274 if (&setting2->node == &setting->node)
1275 break;
Richard Genoudaf606172013-03-29 10:03:26 +01001276 /*
1277 * All we can do here is pinmux_disable_setting.
1278 * That means that some pins are muxed differently now
1279 * than they were before applying the setting (We can't
1280 * "unmux a pin"!), but it's not a big deal since the pins
1281 * are free to be muxed by another apply_setting.
1282 */
1283 if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
1284 pinmux_disable_setting(setting2);
Richard Genoud3102a762013-03-25 15:47:22 +01001285 }
Richard Genoud8009d5f2013-03-28 12:55:47 +01001286
Richard Genoud385d9422013-03-29 10:03:27 +01001287 /* There's no infinite recursive loop here because p->state is NULL */
1288 if (old_state)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001289 pinctrl_select_state(p, old_state);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001290
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001291 return ret;
1292}
Florian Fainelli981ed1b2017-03-01 10:32:57 -08001293
1294/**
1295 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
1296 * @p: the pinctrl handle for the device that requests configuration
1297 * @state: the state handle to select/activate/program
1298 */
1299int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
1300{
1301 if (p->state == state)
1302 return 0;
1303
1304 return pinctrl_commit_state(p, state);
1305}
Stephen Warren6e5e9592012-03-02 13:05:47 -07001306EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001307
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001308static void devm_pinctrl_release(struct device *dev, void *res)
1309{
1310 pinctrl_put(*(struct pinctrl **)res);
1311}
1312
1313/**
1314 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
1315 * @dev: the device to obtain the handle for
1316 *
1317 * If there is a need to explicitly destroy the returned struct pinctrl,
1318 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1319 */
1320struct pinctrl *devm_pinctrl_get(struct device *dev)
1321{
1322 struct pinctrl **ptr, *p;
1323
1324 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
1325 if (!ptr)
1326 return ERR_PTR(-ENOMEM);
1327
1328 p = pinctrl_get(dev);
1329 if (!IS_ERR(p)) {
1330 *ptr = p;
1331 devres_add(dev, ptr);
1332 } else {
1333 devres_free(ptr);
1334 }
1335
1336 return p;
1337}
1338EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1339
1340static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1341{
1342 struct pinctrl **p = res;
1343
1344 return *p == data;
1345}
1346
1347/**
1348 * devm_pinctrl_put() - Resource managed pinctrl_put()
1349 * @p: the pinctrl handle to release
1350 *
1351 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1352 * this function will not need to be called and the resource management
1353 * code will ensure that the resource is freed.
1354 */
1355void devm_pinctrl_put(struct pinctrl *p)
1356{
Jingoo Hana72149e2013-02-26 11:34:07 +09001357 WARN_ON(devres_release(p->dev, devm_pinctrl_release,
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001358 devm_pinctrl_match, p));
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001359}
1360EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1361
Masahiro Yamada3f713b72017-08-04 11:22:31 +09001362int pinctrl_register_map(const struct pinctrl_map *maps, unsigned num_maps,
Doug Andersonc5272a22015-05-01 09:01:27 -07001363 bool dup)
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001364{
Stephen Warren1e2082b2012-03-02 13:05:48 -07001365 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001366 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001367
Masahiro Yamada7e9236f2015-05-28 21:52:59 +09001368 pr_debug("add %u pinctrl maps\n", num_maps);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001369
1370 /* First sanity check the new mapping */
1371 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001372 if (!maps[i].dev_name) {
1373 pr_err("failed to register map %s (%d): no device given\n",
1374 maps[i].name, i);
1375 return -EINVAL;
1376 }
1377
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001378 if (!maps[i].name) {
1379 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001380 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001381 return -EINVAL;
1382 }
1383
Stephen Warren1e2082b2012-03-02 13:05:48 -07001384 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1385 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001386 pr_err("failed to register map %s (%d): no pin control device given\n",
1387 maps[i].name, i);
1388 return -EINVAL;
1389 }
1390
Stephen Warren1e2082b2012-03-02 13:05:48 -07001391 switch (maps[i].type) {
1392 case PIN_MAP_TYPE_DUMMY_STATE:
1393 break;
1394 case PIN_MAP_TYPE_MUX_GROUP:
1395 ret = pinmux_validate_map(&maps[i], i);
1396 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001397 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001398 break;
1399 case PIN_MAP_TYPE_CONFIGS_PIN:
1400 case PIN_MAP_TYPE_CONFIGS_GROUP:
1401 ret = pinconf_validate_map(&maps[i], i);
1402 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001403 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001404 break;
1405 default:
1406 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001407 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -07001408 return -EINVAL;
1409 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001410 }
1411
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001412 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -08001413 if (!maps_node)
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001414 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001415
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001416 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -06001417 if (dup) {
1418 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
1419 GFP_KERNEL);
1420 if (!maps_node->maps) {
Stephen Warren57291ce2012-03-23 10:29:46 -06001421 kfree(maps_node);
1422 return -ENOMEM;
1423 }
1424 } else {
1425 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001426 }
1427
Doug Andersonc5272a22015-05-01 09:01:27 -07001428 mutex_lock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001429 list_add_tail(&maps_node->node, &pinctrl_maps);
Doug Andersonc5272a22015-05-01 09:01:27 -07001430 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001431
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001432 return 0;
1433}
1434
Stephen Warren57291ce2012-03-23 10:29:46 -06001435/**
1436 * pinctrl_register_mappings() - register a set of pin controller mappings
1437 * @maps: the pincontrol mappings table to register. This should probably be
1438 * marked with __initdata so it can be discarded after boot. This
1439 * function will perform a shallow copy for the mapping entries.
1440 * @num_maps: the number of maps in the mapping table
1441 */
Masahiro Yamada3f713b72017-08-04 11:22:31 +09001442int pinctrl_register_mappings(const struct pinctrl_map *maps,
Stephen Warren57291ce2012-03-23 10:29:46 -06001443 unsigned num_maps)
1444{
Doug Andersonc5272a22015-05-01 09:01:27 -07001445 return pinctrl_register_map(maps, num_maps, true);
Stephen Warren57291ce2012-03-23 10:29:46 -06001446}
Richard Fitzgerald8b1b2dc2018-02-23 13:43:52 +00001447EXPORT_SYMBOL_GPL(pinctrl_register_mappings);
Stephen Warren57291ce2012-03-23 10:29:46 -06001448
Masahiro Yamada3f713b72017-08-04 11:22:31 +09001449void pinctrl_unregister_map(const struct pinctrl_map *map)
Stephen Warren57291ce2012-03-23 10:29:46 -06001450{
1451 struct pinctrl_maps *maps_node;
1452
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001453 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001454 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1455 if (maps_node->maps == map) {
1456 list_del(&maps_node->node);
Linus Walleijdb6c2c62013-07-23 01:43:20 +02001457 kfree(maps_node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001458 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001459 return;
1460 }
1461 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001462 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001463}
1464
Julien Delacou840a47b2012-12-10 14:47:33 +01001465/**
1466 * pinctrl_force_sleep() - turn a given controller device into sleep state
1467 * @pctldev: pin controller device
1468 */
1469int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1470{
1471 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
Florian Fainelli981ed1b2017-03-01 10:32:57 -08001472 return pinctrl_commit_state(pctldev->p, pctldev->hog_sleep);
Julien Delacou840a47b2012-12-10 14:47:33 +01001473 return 0;
1474}
1475EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1476
1477/**
1478 * pinctrl_force_default() - turn a given controller device into default state
1479 * @pctldev: pin controller device
1480 */
1481int pinctrl_force_default(struct pinctrl_dev *pctldev)
1482{
1483 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
Florian Fainelli981ed1b2017-03-01 10:32:57 -08001484 return pinctrl_commit_state(pctldev->p, pctldev->hog_default);
Julien Delacou840a47b2012-12-10 14:47:33 +01001485 return 0;
1486}
1487EXPORT_SYMBOL_GPL(pinctrl_force_default);
1488
Douglas Andersonef0eebc2015-10-20 21:15:06 -07001489/**
1490 * pinctrl_init_done() - tell pinctrl probe is done
1491 *
1492 * We'll use this time to switch the pins from "init" to "default" unless the
1493 * driver selected some other state.
1494 *
1495 * @dev: device to that's done probing
1496 */
1497int pinctrl_init_done(struct device *dev)
1498{
1499 struct dev_pin_info *pins = dev->pins;
1500 int ret;
1501
1502 if (!pins)
1503 return 0;
1504
1505 if (IS_ERR(pins->init_state))
1506 return 0; /* No such state */
1507
1508 if (pins->p->state != pins->init_state)
1509 return 0; /* Not at init anyway */
1510
1511 if (IS_ERR(pins->default_state))
1512 return 0; /* No default state */
1513
1514 ret = pinctrl_select_state(pins->p, pins->default_state);
1515 if (ret)
1516 dev_err(dev, "failed to activate default pinctrl state\n");
1517
1518 return ret;
1519}
1520
Linus Walleij14005ee2013-06-05 15:30:33 +02001521#ifdef CONFIG_PM
1522
1523/**
Tony Lindgrenf3333492013-07-18 08:15:04 -07001524 * pinctrl_pm_select_state() - select pinctrl state for PM
1525 * @dev: device to select default state for
1526 * @state: state to set
1527 */
1528static int pinctrl_pm_select_state(struct device *dev,
1529 struct pinctrl_state *state)
1530{
1531 struct dev_pin_info *pins = dev->pins;
1532 int ret;
1533
1534 if (IS_ERR(state))
1535 return 0; /* No such state */
1536 ret = pinctrl_select_state(pins->p, state);
1537 if (ret)
1538 dev_err(dev, "failed to activate pinctrl state %s\n",
1539 state->name);
1540 return ret;
1541}
1542
1543/**
Linus Walleij14005ee2013-06-05 15:30:33 +02001544 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1545 * @dev: device to select default state for
1546 */
1547int pinctrl_pm_select_default_state(struct device *dev)
1548{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001549 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001550 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001551
1552 return pinctrl_pm_select_state(dev, dev->pins->default_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001553}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001554EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001555
1556/**
1557 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1558 * @dev: device to select sleep state for
1559 */
1560int pinctrl_pm_select_sleep_state(struct device *dev)
1561{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001562 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001563 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001564
1565 return pinctrl_pm_select_state(dev, dev->pins->sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001566}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001567EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001568
1569/**
1570 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1571 * @dev: device to select idle state for
1572 */
1573int pinctrl_pm_select_idle_state(struct device *dev)
1574{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001575 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001576 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001577
1578 return pinctrl_pm_select_state(dev, dev->pins->idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001579}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001580EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001581#endif
1582
Linus Walleij2744e8a2011-05-02 20:50:54 +02001583#ifdef CONFIG_DEBUG_FS
1584
1585static int pinctrl_pins_show(struct seq_file *s, void *what)
1586{
1587 struct pinctrl_dev *pctldev = s->private;
1588 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001589 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001590
1591 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001592
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001593 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001594
Chanho Park706e8522012-01-03 16:47:50 +09001595 /* The pin number can be retrived from the pin controller descriptor */
1596 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001597 struct pin_desc *desc;
1598
Chanho Park706e8522012-01-03 16:47:50 +09001599 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001600 desc = pin_desc_get(pctldev, pin);
1601 /* Pin space may be sparse */
Markus Elfringcea234e2017-05-02 11:04:55 +02001602 if (!desc)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001603 continue;
1604
Masahiro Yamadacf9d9942016-05-24 14:26:26 +09001605 seq_printf(s, "pin %d (%s) ", pin, desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001606
1607 /* Driver-specific info per pin */
1608 if (ops->pin_dbg_show)
1609 ops->pin_dbg_show(pctldev, s, pin);
1610
1611 seq_puts(s, "\n");
1612 }
1613
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001614 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001615
Linus Walleij2744e8a2011-05-02 20:50:54 +02001616 return 0;
1617}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001618DEFINE_SHOW_ATTRIBUTE(pinctrl_pins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001619
1620static int pinctrl_groups_show(struct seq_file *s, void *what)
1621{
1622 struct pinctrl_dev *pctldev = s->private;
1623 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301624 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001625
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001626 mutex_lock(&pctldev->mutex);
1627
Viresh Kumard1e90e92012-03-30 11:25:40 +05301628 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001629
Linus Walleij2744e8a2011-05-02 20:50:54 +02001630 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301631 while (selector < ngroups) {
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001632 const unsigned *pins = NULL;
1633 unsigned num_pins = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001634 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001635 const char *pname;
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001636 int ret = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001637 int i;
1638
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001639 if (ops->get_group_pins)
1640 ret = ops->get_group_pins(pctldev, selector,
1641 &pins, &num_pins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001642 if (ret)
1643 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1644 gname);
1645 else {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001646 seq_printf(s, "group: %s\n", gname);
1647 for (i = 0; i < num_pins; i++) {
1648 pname = pin_get_name(pctldev, pins[i]);
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001649 if (WARN_ON(!pname)) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001650 mutex_unlock(&pctldev->mutex);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001651 return -EINVAL;
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001652 }
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001653 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1654 }
1655 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001656 }
1657 selector++;
1658 }
1659
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001660 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001661
1662 return 0;
1663}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001664DEFINE_SHOW_ATTRIBUTE(pinctrl_groups);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001665
1666static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1667{
1668 struct pinctrl_dev *pctldev = s->private;
1669 struct pinctrl_gpio_range *range = NULL;
1670
1671 seq_puts(s, "GPIO ranges handled:\n");
1672
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001673 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001674
Linus Walleij2744e8a2011-05-02 20:50:54 +02001675 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001676 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Christian Ruppertc8587ee2013-06-13 14:55:31 +02001677 if (range->pins) {
1678 int a;
1679 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
1680 range->id, range->name,
1681 range->base, (range->base + range->npins - 1));
1682 for (a = 0; a < range->npins - 1; a++)
1683 seq_printf(s, "%u, ", range->pins[a]);
1684 seq_printf(s, "%u}\n", range->pins[a]);
1685 }
1686 else
1687 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1688 range->id, range->name,
1689 range->base, (range->base + range->npins - 1),
1690 range->pin_base,
1691 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001692 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001693
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001694 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001695
1696 return 0;
1697}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001698DEFINE_SHOW_ATTRIBUTE(pinctrl_gpioranges);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001699
1700static int pinctrl_devices_show(struct seq_file *s, void *what)
1701{
1702 struct pinctrl_dev *pctldev;
1703
Linus Walleijae6b4d82011-10-19 18:14:33 +02001704 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001705
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001706 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001707
Linus Walleij2744e8a2011-05-02 20:50:54 +02001708 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1709 seq_printf(s, "%s ", pctldev->desc->name);
1710 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001711 seq_puts(s, "yes ");
1712 else
1713 seq_puts(s, "no ");
1714 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001715 seq_puts(s, "yes");
1716 else
1717 seq_puts(s, "no");
1718 seq_puts(s, "\n");
1719 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001720
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001721 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001722
1723 return 0;
1724}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001725DEFINE_SHOW_ATTRIBUTE(pinctrl_devices);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001726
Stephen Warren1e2082b2012-03-02 13:05:48 -07001727static inline const char *map_type(enum pinctrl_map_type type)
1728{
1729 static const char * const names[] = {
1730 "INVALID",
1731 "DUMMY_STATE",
1732 "MUX_GROUP",
1733 "CONFIGS_PIN",
1734 "CONFIGS_GROUP",
1735 };
1736
1737 if (type >= ARRAY_SIZE(names))
1738 return "UNKNOWN";
1739
1740 return names[type];
1741}
1742
Stephen Warren3eedb432012-02-23 17:04:40 -07001743static int pinctrl_maps_show(struct seq_file *s, void *what)
1744{
1745 struct pinctrl_maps *maps_node;
1746 int i;
Masahiro Yamada3f713b72017-08-04 11:22:31 +09001747 const struct pinctrl_map *map;
Stephen Warren3eedb432012-02-23 17:04:40 -07001748
1749 seq_puts(s, "Pinctrl maps:\n");
1750
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001751 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001752 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001753 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1754 map->dev_name, map->name, map_type(map->type),
1755 map->type);
1756
1757 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1758 seq_printf(s, "controlling device %s\n",
1759 map->ctrl_dev_name);
1760
1761 switch (map->type) {
1762 case PIN_MAP_TYPE_MUX_GROUP:
1763 pinmux_show_map(s, map);
1764 break;
1765 case PIN_MAP_TYPE_CONFIGS_PIN:
1766 case PIN_MAP_TYPE_CONFIGS_GROUP:
1767 pinconf_show_map(s, map);
1768 break;
1769 default:
1770 break;
1771 }
1772
Markus Elfring390e1042017-05-02 10:47:35 +02001773 seq_putc(s, '\n');
Stephen Warren3eedb432012-02-23 17:04:40 -07001774 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001775 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001776
1777 return 0;
1778}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001779DEFINE_SHOW_ATTRIBUTE(pinctrl_maps);
Stephen Warren3eedb432012-02-23 17:04:40 -07001780
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001781static int pinctrl_show(struct seq_file *s, void *what)
1782{
1783 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001784 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001785 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001786
1787 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001788
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001789 mutex_lock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001790
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001791 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001792 seq_printf(s, "device: %s current state: %s\n",
1793 dev_name(p->dev),
1794 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001795
Stephen Warren6e5e9592012-03-02 13:05:47 -07001796 list_for_each_entry(state, &p->states, node) {
1797 seq_printf(s, " state: %s\n", state->name);
1798
1799 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001800 struct pinctrl_dev *pctldev = setting->pctldev;
1801
1802 seq_printf(s, " type: %s controller %s ",
1803 map_type(setting->type),
1804 pinctrl_dev_get_name(pctldev));
1805
1806 switch (setting->type) {
1807 case PIN_MAP_TYPE_MUX_GROUP:
1808 pinmux_show_setting(s, setting);
1809 break;
1810 case PIN_MAP_TYPE_CONFIGS_PIN:
1811 case PIN_MAP_TYPE_CONFIGS_GROUP:
1812 pinconf_show_setting(s, setting);
1813 break;
1814 default:
1815 break;
1816 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001817 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001818 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001819 }
1820
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001821 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001822
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001823 return 0;
1824}
Andy Shevchenkob5520892018-02-17 17:25:11 +02001825DEFINE_SHOW_ATTRIBUTE(pinctrl);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001826
Linus Walleij2744e8a2011-05-02 20:50:54 +02001827static struct dentry *debugfs_root;
1828
1829static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1830{
Tony Lindgren02157162012-01-20 08:17:22 -08001831 struct dentry *device_root;
Jan Kundrát1781af52018-01-26 16:06:37 +01001832 const char *debugfs_name;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001833
Jan Kundrát1781af52018-01-26 16:06:37 +01001834 if (pctldev->desc->name &&
1835 strcmp(dev_name(pctldev->dev), pctldev->desc->name)) {
1836 debugfs_name = devm_kasprintf(pctldev->dev, GFP_KERNEL,
1837 "%s-%s", dev_name(pctldev->dev),
1838 pctldev->desc->name);
1839 if (!debugfs_name) {
1840 pr_warn("failed to determine debugfs dir name for %s\n",
1841 dev_name(pctldev->dev));
1842 return;
1843 }
1844 } else {
1845 debugfs_name = dev_name(pctldev->dev);
1846 }
1847
1848 device_root = debugfs_create_dir(debugfs_name, debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001849 pctldev->device_root = device_root;
1850
Linus Walleij2744e8a2011-05-02 20:50:54 +02001851 if (IS_ERR(device_root) || !device_root) {
1852 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001853 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001854 return;
1855 }
1856 debugfs_create_file("pins", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001857 device_root, pctldev, &pinctrl_pins_fops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001858 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001859 device_root, pctldev, &pinctrl_groups_fops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001860 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001861 device_root, pctldev, &pinctrl_gpioranges_fops);
Florian Vaussarde7f2a442014-02-05 07:51:22 +01001862 if (pctldev->desc->pmxops)
1863 pinmux_init_device_debugfs(device_root, pctldev);
1864 if (pctldev->desc->confops)
1865 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001866}
1867
Tony Lindgren02157162012-01-20 08:17:22 -08001868static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1869{
1870 debugfs_remove_recursive(pctldev->device_root);
1871}
1872
Linus Walleij2744e8a2011-05-02 20:50:54 +02001873static void pinctrl_init_debugfs(void)
1874{
1875 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1876 if (IS_ERR(debugfs_root) || !debugfs_root) {
1877 pr_warn("failed to create debugfs directory\n");
1878 debugfs_root = NULL;
1879 return;
1880 }
1881
1882 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001883 debugfs_root, NULL, &pinctrl_devices_fops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001884 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001885 debugfs_root, NULL, &pinctrl_maps_fops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001886 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
Andy Shevchenkob5520892018-02-17 17:25:11 +02001887 debugfs_root, NULL, &pinctrl_fops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001888}
1889
1890#else /* CONFIG_DEBUG_FS */
1891
1892static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1893{
1894}
1895
1896static void pinctrl_init_debugfs(void)
1897{
1898}
1899
Tony Lindgren02157162012-01-20 08:17:22 -08001900static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1901{
1902}
1903
Linus Walleij2744e8a2011-05-02 20:50:54 +02001904#endif
1905
Stephen Warrend26bc492012-03-16 14:54:25 -06001906static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1907{
1908 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1909
1910 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301911 !ops->get_groups_count ||
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001912 !ops->get_group_name)
Stephen Warrend26bc492012-03-16 14:54:25 -06001913 return -EINVAL;
1914
1915 return 0;
1916}
1917
Linus Walleij2744e8a2011-05-02 20:50:54 +02001918/**
Tony Lindgren950b0d92017-01-11 14:13:34 -08001919 * pinctrl_init_controller() - init a pin controller device
Linus Walleij2744e8a2011-05-02 20:50:54 +02001920 * @pctldesc: descriptor for this pin controller
1921 * @dev: parent device for this pin controller
1922 * @driver_data: private pin controller data for this pin controller
1923 */
Andy Shevchenko0ca49212017-03-27 14:37:09 +03001924static struct pinctrl_dev *
1925pinctrl_init_controller(struct pinctrl_desc *pctldesc, struct device *dev,
1926 void *driver_data)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001927{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001928 struct pinctrl_dev *pctldev;
1929 int ret;
1930
Devendra Nagada9aecb2012-06-16 23:43:16 +05301931 if (!pctldesc)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001932 return ERR_PTR(-EINVAL);
Devendra Nagada9aecb2012-06-16 23:43:16 +05301933 if (!pctldesc->name)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001934 return ERR_PTR(-EINVAL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001935
Stephen Warren02f5b982012-02-22 14:26:00 -07001936 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -08001937 if (!pctldev)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001938 return ERR_PTR(-ENOMEM);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001939
1940 /* Initialize pin control device struct */
1941 pctldev->owner = pctldesc->owner;
1942 pctldev->desc = pctldesc;
1943 pctldev->driver_data = driver_data;
1944 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleijc033a712016-12-30 15:04:43 +01001945#ifdef CONFIG_GENERIC_PINCTRL_GROUPS
Tony Lindgrenc7059c52016-12-27 09:20:00 -08001946 INIT_RADIX_TREE(&pctldev->pin_group_tree, GFP_KERNEL);
Linus Walleijc033a712016-12-30 15:04:43 +01001947#endif
Tony Lindgrena76edc82016-12-27 09:20:01 -08001948#ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
1949 INIT_RADIX_TREE(&pctldev->pin_function_tree, GFP_KERNEL);
1950#endif
Linus Walleij2744e8a2011-05-02 20:50:54 +02001951 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Thierry Reding46daed62017-01-12 17:03:34 +01001952 INIT_LIST_HEAD(&pctldev->node);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001953 pctldev->dev = dev;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001954 mutex_init(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001955
Stephen Warrend26bc492012-03-16 14:54:25 -06001956 /* check core ops for sanity */
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001957 ret = pinctrl_check_ops(pctldev);
1958 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001959 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06001960 goto out_err;
1961 }
1962
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001963 /* If we're implementing pinmuxing, check the ops for sanity */
1964 if (pctldesc->pmxops) {
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001965 ret = pinmux_check_ops(pctldev);
1966 if (ret)
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001967 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001968 }
1969
1970 /* If we're implementing pinconfig, check the ops for sanity */
1971 if (pctldesc->confops) {
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001972 ret = pinconf_check_ops(pctldev);
1973 if (ret)
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001974 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001975 }
1976
Linus Walleij2744e8a2011-05-02 20:50:54 +02001977 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02001978 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001979 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1980 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001981 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001982 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1983 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001984 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001985 }
1986
Linus Walleij2744e8a2011-05-02 20:50:54 +02001987 return pctldev;
1988
Stephen Warren51cd24e2011-12-09 16:59:05 -07001989out_err:
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001990 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001991 kfree(pctldev);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001992 return ERR_PTR(ret);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001993}
Tony Lindgren950b0d92017-01-11 14:13:34 -08001994
Tony Lindgren61187142017-03-30 09:16:39 -07001995static int pinctrl_claim_hogs(struct pinctrl_dev *pctldev)
Tony Lindgren950b0d92017-01-11 14:13:34 -08001996{
1997 pctldev->p = create_pinctrl(pctldev->dev, pctldev);
Tony Lindgren61187142017-03-30 09:16:39 -07001998 if (PTR_ERR(pctldev->p) == -ENODEV) {
1999 dev_dbg(pctldev->dev, "no hogs found\n");
Tony Lindgren950b0d92017-01-11 14:13:34 -08002000
Tony Lindgren61187142017-03-30 09:16:39 -07002001 return 0;
2002 }
2003
2004 if (IS_ERR(pctldev->p)) {
2005 dev_err(pctldev->dev, "error claiming hogs: %li\n",
2006 PTR_ERR(pctldev->p));
2007
2008 return PTR_ERR(pctldev->p);
2009 }
2010
2011 kref_get(&pctldev->p->users);
2012 pctldev->hog_default =
2013 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
2014 if (IS_ERR(pctldev->hog_default)) {
2015 dev_dbg(pctldev->dev,
2016 "failed to lookup the default state\n");
2017 } else {
2018 if (pinctrl_select_state(pctldev->p,
2019 pctldev->hog_default))
2020 dev_err(pctldev->dev,
2021 "failed to select default state\n");
2022 }
2023
2024 pctldev->hog_sleep =
2025 pinctrl_lookup_state(pctldev->p,
2026 PINCTRL_STATE_SLEEP);
2027 if (IS_ERR(pctldev->hog_sleep))
2028 dev_dbg(pctldev->dev,
2029 "failed to lookup the sleep state\n");
2030
2031 return 0;
2032}
2033
2034int pinctrl_enable(struct pinctrl_dev *pctldev)
2035{
2036 int error;
2037
2038 error = pinctrl_claim_hogs(pctldev);
2039 if (error) {
2040 dev_err(pctldev->dev, "could not claim hogs: %i\n",
2041 error);
2042 mutex_destroy(&pctldev->mutex);
2043 kfree(pctldev);
2044
2045 return error;
Tony Lindgren950b0d92017-01-11 14:13:34 -08002046 }
2047
2048 mutex_lock(&pinctrldev_list_mutex);
2049 list_add_tail(&pctldev->node, &pinctrldev_list);
2050 mutex_unlock(&pinctrldev_list_mutex);
2051
2052 pinctrl_init_device_debugfs(pctldev);
2053
2054 return 0;
2055}
Tony Lindgren61187142017-03-30 09:16:39 -07002056EXPORT_SYMBOL_GPL(pinctrl_enable);
Tony Lindgren950b0d92017-01-11 14:13:34 -08002057
2058/**
2059 * pinctrl_register() - register a pin controller device
2060 * @pctldesc: descriptor for this pin controller
2061 * @dev: parent device for this pin controller
2062 * @driver_data: private pin controller data for this pin controller
2063 *
2064 * Note that pinctrl_register() is known to have problems as the pin
2065 * controller driver functions are called before the driver has a
2066 * struct pinctrl_dev handle. To avoid issues later on, please use the
2067 * new pinctrl_register_and_init() below instead.
2068 */
2069struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
2070 struct device *dev, void *driver_data)
2071{
2072 struct pinctrl_dev *pctldev;
2073 int error;
2074
2075 pctldev = pinctrl_init_controller(pctldesc, dev, driver_data);
2076 if (IS_ERR(pctldev))
2077 return pctldev;
2078
Tony Lindgren61187142017-03-30 09:16:39 -07002079 error = pinctrl_enable(pctldev);
2080 if (error)
Tony Lindgren950b0d92017-01-11 14:13:34 -08002081 return ERR_PTR(error);
Tony Lindgren950b0d92017-01-11 14:13:34 -08002082
2083 return pctldev;
2084
2085}
Linus Walleij2744e8a2011-05-02 20:50:54 +02002086EXPORT_SYMBOL_GPL(pinctrl_register);
2087
Tony Lindgren61187142017-03-30 09:16:39 -07002088/**
2089 * pinctrl_register_and_init() - register and init pin controller device
2090 * @pctldesc: descriptor for this pin controller
2091 * @dev: parent device for this pin controller
2092 * @driver_data: private pin controller data for this pin controller
2093 * @pctldev: pin controller device
2094 *
2095 * Note that pinctrl_enable() still needs to be manually called after
2096 * this once the driver is ready.
2097 */
Tony Lindgren950b0d92017-01-11 14:13:34 -08002098int pinctrl_register_and_init(struct pinctrl_desc *pctldesc,
2099 struct device *dev, void *driver_data,
2100 struct pinctrl_dev **pctldev)
2101{
2102 struct pinctrl_dev *p;
Tony Lindgren950b0d92017-01-11 14:13:34 -08002103
2104 p = pinctrl_init_controller(pctldesc, dev, driver_data);
2105 if (IS_ERR(p))
2106 return PTR_ERR(p);
2107
2108 /*
2109 * We have pinctrl_start() call functions in the pin controller
2110 * driver with create_pinctrl() for at least dt_node_to_map(). So
2111 * let's make sure pctldev is properly initialized for the
2112 * pin controller driver before we do anything.
2113 */
2114 *pctldev = p;
2115
Tony Lindgren950b0d92017-01-11 14:13:34 -08002116 return 0;
2117}
2118EXPORT_SYMBOL_GPL(pinctrl_register_and_init);
2119
Linus Walleij2744e8a2011-05-02 20:50:54 +02002120/**
2121 * pinctrl_unregister() - unregister pinmux
2122 * @pctldev: pin controller to unregister
2123 *
2124 * Called by pinmux drivers to unregister a pinmux.
2125 */
2126void pinctrl_unregister(struct pinctrl_dev *pctldev)
2127{
Dong Aisheng5d589b02012-05-23 21:22:40 +08002128 struct pinctrl_gpio_range *range, *n;
Jon Hunter3429fb32017-01-05 15:52:55 +00002129
Markus Elfringcea234e2017-05-02 11:04:55 +02002130 if (!pctldev)
Linus Walleij2744e8a2011-05-02 20:50:54 +02002131 return;
2132
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002133 mutex_lock(&pctldev->mutex);
Tony Lindgren02157162012-01-20 08:17:22 -08002134 pinctrl_remove_device_debugfs(pctldev);
Jim Lindb93fac2015-01-08 20:25:05 +08002135 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07002136
Jon Hunter3429fb32017-01-05 15:52:55 +00002137 if (!IS_ERR_OR_NULL(pctldev->p))
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002138 pinctrl_put(pctldev->p);
Stephen Warren57b676f2012-03-02 13:05:44 -07002139
Jim Lindb93fac2015-01-08 20:25:05 +08002140 mutex_lock(&pinctrldev_list_mutex);
2141 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002142 /* TODO: check that no pinmuxes are still active? */
Thierry Reding46daed62017-01-12 17:03:34 +01002143 list_del(&pctldev->node);
Tony Lindgrena76edc82016-12-27 09:20:01 -08002144 pinmux_generic_free_functions(pctldev);
Tony Lindgrenc7059c52016-12-27 09:20:00 -08002145 pinctrl_generic_free_groups(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002146 /* Destroy descriptor tree */
2147 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
2148 pctldev->desc->npins);
Dong Aisheng5d589b02012-05-23 21:22:40 +08002149 /* remove gpio ranges map */
2150 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
2151 list_del(&range->node);
2152
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002153 mutex_unlock(&pctldev->mutex);
2154 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07002155 kfree(pctldev);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002156 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002157}
2158EXPORT_SYMBOL_GPL(pinctrl_unregister);
2159
Laxman Dewangan80e0f8d2016-02-24 14:12:59 +05302160static void devm_pinctrl_dev_release(struct device *dev, void *res)
2161{
2162 struct pinctrl_dev *pctldev = *(struct pinctrl_dev **)res;
2163
2164 pinctrl_unregister(pctldev);
2165}
2166
2167static int devm_pinctrl_dev_match(struct device *dev, void *res, void *data)
2168{
2169 struct pctldev **r = res;
2170
Laxman Dewangan3024f922016-02-24 14:44:07 +05302171 if (WARN_ON(!r || !*r))
Laxman Dewangan80e0f8d2016-02-24 14:12:59 +05302172 return 0;
2173
2174 return *r == data;
2175}
2176
2177/**
2178 * devm_pinctrl_register() - Resource managed version of pinctrl_register().
2179 * @dev: parent device for this pin controller
2180 * @pctldesc: descriptor for this pin controller
2181 * @driver_data: private pin controller data for this pin controller
2182 *
2183 * Returns an error pointer if pincontrol register failed. Otherwise
2184 * it returns valid pinctrl handle.
2185 *
2186 * The pinctrl device will be automatically released when the device is unbound.
2187 */
2188struct pinctrl_dev *devm_pinctrl_register(struct device *dev,
2189 struct pinctrl_desc *pctldesc,
2190 void *driver_data)
2191{
2192 struct pinctrl_dev **ptr, *pctldev;
2193
2194 ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
2195 if (!ptr)
2196 return ERR_PTR(-ENOMEM);
2197
2198 pctldev = pinctrl_register(pctldesc, dev, driver_data);
2199 if (IS_ERR(pctldev)) {
2200 devres_free(ptr);
2201 return pctldev;
2202 }
2203
2204 *ptr = pctldev;
2205 devres_add(dev, ptr);
2206
2207 return pctldev;
2208}
2209EXPORT_SYMBOL_GPL(devm_pinctrl_register);
2210
2211/**
Tony Lindgren950b0d92017-01-11 14:13:34 -08002212 * devm_pinctrl_register_and_init() - Resource managed pinctrl register and init
2213 * @dev: parent device for this pin controller
2214 * @pctldesc: descriptor for this pin controller
2215 * @driver_data: private pin controller data for this pin controller
2216 *
2217 * Returns an error pointer if pincontrol register failed. Otherwise
2218 * it returns valid pinctrl handle.
2219 *
2220 * The pinctrl device will be automatically released when the device is unbound.
2221 */
2222int devm_pinctrl_register_and_init(struct device *dev,
2223 struct pinctrl_desc *pctldesc,
2224 void *driver_data,
2225 struct pinctrl_dev **pctldev)
2226{
2227 struct pinctrl_dev **ptr;
2228 int error;
2229
2230 ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
2231 if (!ptr)
2232 return -ENOMEM;
2233
2234 error = pinctrl_register_and_init(pctldesc, dev, driver_data, pctldev);
2235 if (error) {
2236 devres_free(ptr);
2237 return error;
2238 }
2239
2240 *ptr = *pctldev;
2241 devres_add(dev, ptr);
2242
2243 return 0;
2244}
2245EXPORT_SYMBOL_GPL(devm_pinctrl_register_and_init);
2246
2247/**
Laxman Dewangan80e0f8d2016-02-24 14:12:59 +05302248 * devm_pinctrl_unregister() - Resource managed version of pinctrl_unregister().
2249 * @dev: device for which which resource was allocated
2250 * @pctldev: the pinctrl device to unregister.
2251 */
2252void devm_pinctrl_unregister(struct device *dev, struct pinctrl_dev *pctldev)
2253{
2254 WARN_ON(devres_release(dev, devm_pinctrl_dev_release,
2255 devm_pinctrl_dev_match, pctldev));
2256}
2257EXPORT_SYMBOL_GPL(devm_pinctrl_unregister);
2258
Linus Walleij2744e8a2011-05-02 20:50:54 +02002259static int __init pinctrl_init(void)
2260{
2261 pr_info("initialized pinctrl subsystem\n");
2262 pinctrl_init_debugfs();
2263 return 0;
2264}
2265
2266/* init early since many drivers really need to initialized pinmux early */
2267core_initcall(pinctrl_init);