blob: d69046537b75f31d0b1c881686677967865001e0 [file] [log] [blame]
Linus Walleij2744e8a2011-05-02 20:50:54 +02001/*
2 * Core driver for the pin control subsystem
3 *
Linus Walleijbefe5bd2012-02-09 19:47:48 +01004 * Copyright (C) 2011-2012 ST-Ericsson SA
Linus Walleij2744e8a2011-05-02 20:50:54 +02005 * Written on behalf of Linaro for ST-Ericsson
6 * Based on bits of regulator core, gpio core and clk core
7 *
8 * Author: Linus Walleij <linus.walleij@linaro.org>
9 *
Stephen Warrenb2b3e662012-02-19 23:45:43 -070010 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
11 *
Linus Walleij2744e8a2011-05-02 20:50:54 +020012 * License terms: GNU General Public License (GPL) version 2
13 */
14#define pr_fmt(fmt) "pinctrl core: " fmt
15
16#include <linux/kernel.h>
Linus Walleijab780292013-01-22 10:56:14 -070017#include <linux/kref.h>
Stephen Rothwella5a697c2011-09-30 14:39:04 +100018#include <linux/export.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020019#include <linux/init.h>
20#include <linux/device.h>
21#include <linux/slab.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020022#include <linux/err.h>
23#include <linux/list.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020024#include <linux/sysfs.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
Stephen Warren6d4ca1f2012-04-16 10:51:00 -060027#include <linux/pinctrl/consumer.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020028#include <linux/pinctrl/pinctrl.h>
29#include <linux/pinctrl/machine.h>
Haojian Zhuang2afe8222013-03-28 07:34:19 +080030
31#ifdef CONFIG_GPIOLIB
Haojian Zhuang51e13c22013-02-17 19:42:50 +080032#include <asm-generic/gpio.h>
Haojian Zhuang2afe8222013-03-28 07:34:19 +080033#endif
34
Linus Walleij2744e8a2011-05-02 20:50:54 +020035#include "core.h"
Stephen Warren57291ce2012-03-23 10:29:46 -060036#include "devicetree.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020037#include "pinmux.h"
Linus Walleijae6b4d82011-10-19 18:14:33 +020038#include "pinconf.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020039
Stephen Warrenb2b3e662012-02-19 23:45:43 -070040
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080041static bool pinctrl_dummy_state;
42
Patrice Chotard42fed7b2013-04-11 11:01:27 +020043/* Mutex taken to protect pinctrl_list */
Sachin Kamat843aec92013-06-18 14:34:27 +053044static DEFINE_MUTEX(pinctrl_list_mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +020045
46/* Mutex taken to protect pinctrl_maps */
47DEFINE_MUTEX(pinctrl_maps_mutex);
48
49/* Mutex taken to protect pinctrldev_list */
Sachin Kamat843aec92013-06-18 14:34:27 +053050static DEFINE_MUTEX(pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -070051
52/* Global list of pin control devices (struct pinctrl_dev) */
Patrice Chotard42fed7b2013-04-11 11:01:27 +020053static LIST_HEAD(pinctrldev_list);
Linus Walleij2744e8a2011-05-02 20:50:54 +020054
Stephen Warren57b676f2012-03-02 13:05:44 -070055/* List of pin controller handles (struct pinctrl) */
Linus Walleijbefe5bd2012-02-09 19:47:48 +010056static LIST_HEAD(pinctrl_list);
57
Stephen Warren57b676f2012-03-02 13:05:44 -070058/* List of pinctrl maps (struct pinctrl_maps) */
Laurent Meunier6f9e41f2013-02-06 09:09:44 +010059LIST_HEAD(pinctrl_maps);
Stephen Warrenb2b3e662012-02-19 23:45:43 -070060
Linus Walleijbefe5bd2012-02-09 19:47:48 +010061
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +080062/**
63 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
64 *
65 * Usually this function is called by platforms without pinctrl driver support
66 * but run with some shared drivers using pinctrl APIs.
67 * After calling this function, the pinctrl core will return successfully
68 * with creating a dummy state for the driver to keep going smoothly.
69 */
70void pinctrl_provide_dummies(void)
71{
72 pinctrl_dummy_state = true;
73}
74
Linus Walleij2744e8a2011-05-02 20:50:54 +020075const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
76{
77 /* We're not allowed to register devices without name */
78 return pctldev->desc->name;
79}
80EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
81
Haojian Zhuangd6e99ab2013-01-18 15:31:06 +080082const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
83{
84 return dev_name(pctldev->dev);
85}
86EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
87
Linus Walleij2744e8a2011-05-02 20:50:54 +020088void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
89{
90 return pctldev->driver_data;
91}
92EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
93
94/**
Linus Walleij9dfac4f2012-02-01 18:02:47 +010095 * get_pinctrl_dev_from_devname() - look up pin controller device
96 * @devname: the name of a device instance, as returned by dev_name()
Linus Walleij2744e8a2011-05-02 20:50:54 +020097 *
98 * Looks up a pin control device matching a certain device name or pure device
99 * pointer, the pure device pointer will take precedence.
100 */
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100101struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200102{
103 struct pinctrl_dev *pctldev = NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200104
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100105 if (!devname)
106 return NULL;
107
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200108 mutex_lock(&pinctrldev_list_mutex);
109
Linus Walleij2744e8a2011-05-02 20:50:54 +0200110 list_for_each_entry(pctldev, &pinctrldev_list, node) {
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100111 if (!strcmp(dev_name(pctldev->dev), devname)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200112 /* Matched on device name */
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200113 mutex_unlock(&pinctrldev_list_mutex);
114 return pctldev;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200115 }
116 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200117
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200118 mutex_unlock(&pinctrldev_list_mutex);
119
120 return NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200121}
122
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200123struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np)
124{
125 struct pinctrl_dev *pctldev;
126
127 mutex_lock(&pinctrldev_list_mutex);
128
129 list_for_each_entry(pctldev, &pinctrldev_list, node)
130 if (pctldev->dev->of_node == np) {
131 mutex_unlock(&pinctrldev_list_mutex);
132 return pctldev;
133 }
134
Daniel Mackd463f822013-04-26 18:57:02 +0200135 mutex_unlock(&pinctrldev_list_mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200136
137 return NULL;
138}
139
Linus Walleij2744e8a2011-05-02 20:50:54 +0200140/**
Linus Walleijae6b4d82011-10-19 18:14:33 +0200141 * pin_get_from_name() - look up a pin number from a name
142 * @pctldev: the pin control device to lookup the pin on
143 * @name: the name of the pin to look up
144 */
145int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
146{
Chanho Park706e8522012-01-03 16:47:50 +0900147 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200148
Chanho Park706e8522012-01-03 16:47:50 +0900149 /* The pin number can be retrived from the pin controller descriptor */
150 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200151 struct pin_desc *desc;
152
Chanho Park706e8522012-01-03 16:47:50 +0900153 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200154 desc = pin_desc_get(pctldev, pin);
155 /* Pin space may be sparse */
Axel Lin6c325f82013-08-19 10:06:29 +0800156 if (desc && !strcmp(name, desc->name))
Linus Walleijae6b4d82011-10-19 18:14:33 +0200157 return pin;
158 }
159
160 return -EINVAL;
161}
162
163/**
Dong Aishengdcb5dbc2012-04-17 15:00:46 +0800164 * pin_get_name_from_id() - look up a pin name from a pin id
165 * @pctldev: the pin control device to lookup the pin on
166 * @name: the name of the pin to look up
167 */
168const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
169{
170 const struct pin_desc *desc;
171
172 desc = pin_desc_get(pctldev, pin);
173 if (desc == NULL) {
174 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
175 pin);
176 return NULL;
177 }
178
179 return desc->name;
180}
181
182/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200183 * pin_is_valid() - check if pin exists on controller
184 * @pctldev: the pin control device to check the pin on
185 * @pin: pin to check, use the local pin controller index number
186 *
187 * This tells us whether a certain pin exist on a certain pin controller or
188 * not. Pin lists may be sparse, so some pins may not exist.
189 */
190bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
191{
192 struct pin_desc *pindesc;
193
194 if (pin < 0)
195 return false;
196
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200197 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200198 pindesc = pin_desc_get(pctldev, pin);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200199 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200200
Stephen Warren57b676f2012-03-02 13:05:44 -0700201 return pindesc != NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200202}
203EXPORT_SYMBOL_GPL(pin_is_valid);
204
205/* Deletes a range of pin descriptors */
206static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
207 const struct pinctrl_pin_desc *pins,
208 unsigned num_pins)
209{
210 int i;
211
Linus Walleij2744e8a2011-05-02 20:50:54 +0200212 for (i = 0; i < num_pins; i++) {
213 struct pin_desc *pindesc;
214
215 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
216 pins[i].number);
217 if (pindesc != NULL) {
218 radix_tree_delete(&pctldev->pin_desc_tree,
219 pins[i].number);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100220 if (pindesc->dynamic_name)
221 kfree(pindesc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200222 }
223 kfree(pindesc);
224 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200225}
226
227static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900228 const struct pinctrl_pin_desc *pin)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200229{
230 struct pin_desc *pindesc;
231
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900232 pindesc = pin_desc_get(pctldev, pin->number);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200233 if (pindesc != NULL) {
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900234 dev_err(pctldev->dev, "pin %d already registered\n",
235 pin->number);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200236 return -EINVAL;
237 }
238
239 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -0800240 if (!pindesc)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200241 return -ENOMEM;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200242
Linus Walleij2744e8a2011-05-02 20:50:54 +0200243 /* Set owner */
244 pindesc->pctldev = pctldev;
245
Stephen Warren9af1e442011-10-19 16:19:27 -0600246 /* Copy basic pin info */
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900247 if (pin->name) {
248 pindesc->name = pin->name;
Linus Walleijca53c5f2011-12-14 20:33:37 +0100249 } else {
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900250 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", pin->number);
Sachin Kamateb26cc92012-09-25 12:41:40 +0530251 if (pindesc->name == NULL) {
252 kfree(pindesc);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100253 return -ENOMEM;
Sachin Kamateb26cc92012-09-25 12:41:40 +0530254 }
Linus Walleijca53c5f2011-12-14 20:33:37 +0100255 pindesc->dynamic_name = true;
256 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200257
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900258 pindesc->drv_data = pin->drv_data;
259
260 radix_tree_insert(&pctldev->pin_desc_tree, pin->number, pindesc);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200261 pr_debug("registered pin %d (%s) on %s\n",
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900262 pin->number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200263 return 0;
264}
265
266static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
267 struct pinctrl_pin_desc const *pins,
268 unsigned num_descs)
269{
270 unsigned i;
271 int ret = 0;
272
273 for (i = 0; i < num_descs; i++) {
Masahiro Yamadacd8f61f2016-05-25 14:09:31 +0900274 ret = pinctrl_register_one_pin(pctldev, &pins[i]);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200275 if (ret)
276 return ret;
277 }
278
279 return 0;
280}
281
282/**
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200283 * gpio_to_pin() - GPIO range GPIO number to pin number translation
284 * @range: GPIO range used for the translation
285 * @gpio: gpio pin to translate to a pin number
286 *
287 * Finds the pin number for a given GPIO using the specified GPIO range
288 * as a base for translation. The distinction between linear GPIO ranges
289 * and pin list based GPIO ranges is managed correctly by this function.
290 *
291 * This function assumes the gpio is part of the specified GPIO range, use
292 * only after making sure this is the case (e.g. by calling it on the
293 * result of successful pinctrl_get_device_gpio_range calls)!
294 */
295static inline int gpio_to_pin(struct pinctrl_gpio_range *range,
296 unsigned int gpio)
297{
298 unsigned int offset = gpio - range->base;
299 if (range->pins)
300 return range->pins[offset];
301 else
302 return range->pin_base + offset;
303}
304
305/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200306 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
307 * @pctldev: pin controller device to check
308 * @gpio: gpio pin to check taken from the global GPIO pin space
309 *
310 * Tries to match a GPIO pin number to the ranges handled by a certain pin
311 * controller, return the range or NULL
312 */
313static struct pinctrl_gpio_range *
314pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
315{
316 struct pinctrl_gpio_range *range = NULL;
317
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200318 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200319 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200320 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
321 /* Check if we're in the valid range */
322 if (gpio >= range->base &&
323 gpio < range->base + range->npins) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200324 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200325 return range;
326 }
327 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200328 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200329 return NULL;
330}
331
332/**
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800333 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
334 * the same GPIO chip are in range
335 * @gpio: gpio pin to check taken from the global GPIO pin space
336 *
337 * This function is complement of pinctrl_match_gpio_range(). If the return
338 * value of pinctrl_match_gpio_range() is NULL, this function could be used
339 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
340 * of the same GPIO chip don't have back-end pinctrl interface.
341 * If the return value is true, it means that pinctrl device is ready & the
342 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
343 * is false, it means that pinctrl device may not be ready.
344 */
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800345#ifdef CONFIG_GPIOLIB
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800346static bool pinctrl_ready_for_gpio_range(unsigned gpio)
347{
348 struct pinctrl_dev *pctldev;
349 struct pinctrl_gpio_range *range = NULL;
350 struct gpio_chip *chip = gpio_to_chip(gpio);
351
Tony Lindgren942cde72015-09-03 10:34:30 -0700352 if (WARN(!chip, "no gpio_chip for gpio%i?", gpio))
353 return false;
354
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200355 mutex_lock(&pinctrldev_list_mutex);
356
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800357 /* Loop over the pin controllers */
358 list_for_each_entry(pctldev, &pinctrldev_list, node) {
359 /* Loop over the ranges */
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800360 mutex_lock(&pctldev->mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800361 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
362 /* Check if any gpio range overlapped with gpio chip */
363 if (range->base + range->npins - 1 < chip->base ||
364 range->base > chip->base + chip->ngpio - 1)
365 continue;
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800366 mutex_unlock(&pctldev->mutex);
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200367 mutex_unlock(&pinctrldev_list_mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800368 return true;
369 }
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800370 mutex_unlock(&pctldev->mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800371 }
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200372
373 mutex_unlock(&pinctrldev_list_mutex);
374
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800375 return false;
376}
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800377#else
378static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
379#endif
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800380
381/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200382 * pinctrl_get_device_gpio_range() - find device for GPIO range
383 * @gpio: the pin to locate the pin controller for
384 * @outdev: the pin control device if found
385 * @outrange: the GPIO range if found
386 *
387 * Find the pin controller handling a certain GPIO pin from the pinspace of
388 * the GPIO subsystem, return the device and the matching GPIO range. Returns
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800389 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
390 * may still have not been registered.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200391 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700392static int pinctrl_get_device_gpio_range(unsigned gpio,
393 struct pinctrl_dev **outdev,
394 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200395{
396 struct pinctrl_dev *pctldev = NULL;
397
Axel Linf0059022013-08-18 20:34:22 +0800398 mutex_lock(&pinctrldev_list_mutex);
399
Linus Walleij2744e8a2011-05-02 20:50:54 +0200400 /* Loop over the pin controllers */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200401 list_for_each_entry(pctldev, &pinctrldev_list, node) {
402 struct pinctrl_gpio_range *range;
403
404 range = pinctrl_match_gpio_range(pctldev, gpio);
405 if (range != NULL) {
406 *outdev = pctldev;
407 *outrange = range;
Axel Linf0059022013-08-18 20:34:22 +0800408 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200409 return 0;
410 }
411 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200412
Axel Linf0059022013-08-18 20:34:22 +0800413 mutex_unlock(&pinctrldev_list_mutex);
414
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800415 return -EPROBE_DEFER;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200416}
417
418/**
419 * pinctrl_add_gpio_range() - register a GPIO range for a controller
420 * @pctldev: pin controller device to add the range to
421 * @range: the GPIO range to add
422 *
423 * This adds a range of GPIOs to be handled by a certain pin controller. Call
424 * this to register handled ranges after registering your pin controller.
425 */
426void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
427 struct pinctrl_gpio_range *range)
428{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200429 mutex_lock(&pctldev->mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700430 list_add_tail(&range->node, &pctldev->gpio_ranges);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200431 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200432}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700433EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200434
Dong Aisheng3e5e00b2012-05-23 21:22:41 +0800435void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
436 struct pinctrl_gpio_range *ranges,
437 unsigned nranges)
438{
439 int i;
440
441 for (i = 0; i < nranges; i++)
442 pinctrl_add_gpio_range(pctldev, &ranges[i]);
443}
444EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
445
Linus Walleij192c3692012-11-20 14:03:37 +0100446struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530447 struct pinctrl_gpio_range *range)
448{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200449 struct pinctrl_dev *pctldev;
450
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200451 pctldev = get_pinctrl_dev_from_devname(devname);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530452
Linus Walleijdfa97512012-11-20 14:54:18 +0100453 /*
454 * If we can't find this device, let's assume that is because
455 * it has not probed yet, so the driver trying to register this
456 * range need to defer probing.
457 */
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200458 if (!pctldev) {
Linus Walleijdfa97512012-11-20 14:54:18 +0100459 return ERR_PTR(-EPROBE_DEFER);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200460 }
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530461 pinctrl_add_gpio_range(pctldev, range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200462
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530463 return pctldev;
464}
Linus Walleij192c3692012-11-20 14:03:37 +0100465EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530466
Christian Ruppert586a87e2013-10-15 15:37:54 +0200467int pinctrl_get_group_pins(struct pinctrl_dev *pctldev, const char *pin_group,
468 const unsigned **pins, unsigned *num_pins)
469{
470 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
471 int gs;
472
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +0200473 if (!pctlops->get_group_pins)
474 return -EINVAL;
475
Christian Ruppert586a87e2013-10-15 15:37:54 +0200476 gs = pinctrl_get_group_selector(pctldev, pin_group);
477 if (gs < 0)
478 return gs;
479
480 return pctlops->get_group_pins(pctldev, gs, pins, num_pins);
481}
482EXPORT_SYMBOL_GPL(pinctrl_get_group_pins);
483
Joachim Eastwoodb18537c2016-02-25 22:44:37 +0100484struct pinctrl_gpio_range *
485pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev *pctldev,
486 unsigned int pin)
487{
488 struct pinctrl_gpio_range *range;
489
490 /* Loop over the ranges */
491 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
492 /* Check if we're in the valid range */
493 if (range->pins) {
494 int a;
495 for (a = 0; a < range->npins; a++) {
496 if (range->pins[a] == pin)
497 return range;
498 }
499 } else if (pin >= range->pin_base &&
500 pin < range->pin_base + range->npins)
501 return range;
502 }
503
504 return NULL;
505}
506EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin_nolock);
507
Linus Walleij2744e8a2011-05-02 20:50:54 +0200508/**
Linus Walleij9afbefb2012-11-20 14:25:07 +0100509 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
510 * @pctldev: the pin controller device to look in
511 * @pin: a controller-local number to find the range for
512 */
513struct pinctrl_gpio_range *
514pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
515 unsigned int pin)
516{
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800517 struct pinctrl_gpio_range *range;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100518
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200519 mutex_lock(&pctldev->mutex);
Joachim Eastwoodb18537c2016-02-25 22:44:37 +0100520 range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200521 mutex_unlock(&pctldev->mutex);
Joachim Eastwoodb18537c2016-02-25 22:44:37 +0100522
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800523 return range;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100524}
525EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
526
527/**
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530528 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
529 * @pctldev: pin controller device to remove the range from
530 * @range: the GPIO range to remove
531 */
532void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
533 struct pinctrl_gpio_range *range)
534{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200535 mutex_lock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530536 list_del(&range->node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200537 mutex_unlock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530538}
539EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
540
Linus Walleijc033a712016-12-30 15:04:43 +0100541#ifdef CONFIG_GENERIC_PINCTRL_GROUPS
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800542
543/**
544 * pinctrl_generic_get_group_count() - returns the number of pin groups
545 * @pctldev: pin controller device
546 */
547int pinctrl_generic_get_group_count(struct pinctrl_dev *pctldev)
548{
549 return pctldev->num_groups;
550}
551EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_count);
552
553/**
554 * pinctrl_generic_get_group_name() - returns the name of a pin group
555 * @pctldev: pin controller device
556 * @selector: group number
557 */
558const char *pinctrl_generic_get_group_name(struct pinctrl_dev *pctldev,
559 unsigned int selector)
560{
561 struct group_desc *group;
562
563 group = radix_tree_lookup(&pctldev->pin_group_tree,
564 selector);
565 if (!group)
566 return NULL;
567
568 return group->name;
569}
570EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_name);
571
572/**
573 * pinctrl_generic_get_group_pins() - gets the pin group pins
574 * @pctldev: pin controller device
575 * @selector: group number
576 * @pins: pins in the group
577 * @num_pins: number of pins in the group
578 */
579int pinctrl_generic_get_group_pins(struct pinctrl_dev *pctldev,
580 unsigned int selector,
581 const unsigned int **pins,
582 unsigned int *num_pins)
583{
584 struct group_desc *group;
585
586 group = radix_tree_lookup(&pctldev->pin_group_tree,
587 selector);
588 if (!group) {
589 dev_err(pctldev->dev, "%s could not find pingroup%i\n",
590 __func__, selector);
591 return -EINVAL;
592 }
593
594 *pins = group->pins;
595 *num_pins = group->num_pins;
596
597 return 0;
598}
599EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_pins);
600
601/**
602 * pinctrl_generic_get_group() - returns a pin group based on the number
603 * @pctldev: pin controller device
604 * @gselector: group number
605 */
606struct group_desc *pinctrl_generic_get_group(struct pinctrl_dev *pctldev,
607 unsigned int selector)
608{
609 struct group_desc *group;
610
611 group = radix_tree_lookup(&pctldev->pin_group_tree,
612 selector);
613 if (!group)
614 return NULL;
615
616 return group;
617}
618EXPORT_SYMBOL_GPL(pinctrl_generic_get_group);
619
620/**
621 * pinctrl_generic_add_group() - adds a new pin group
622 * @pctldev: pin controller device
623 * @name: name of the pin group
624 * @pins: pins in the pin group
625 * @num_pins: number of pins in the pin group
626 * @data: pin controller driver specific data
627 *
628 * Note that the caller must take care of locking.
629 */
630int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name,
631 int *pins, int num_pins, void *data)
632{
633 struct group_desc *group;
634
635 group = devm_kzalloc(pctldev->dev, sizeof(*group), GFP_KERNEL);
636 if (!group)
637 return -ENOMEM;
638
639 group->name = name;
640 group->pins = pins;
641 group->num_pins = num_pins;
642 group->data = data;
643
644 radix_tree_insert(&pctldev->pin_group_tree, pctldev->num_groups,
645 group);
646
647 pctldev->num_groups++;
648
649 return 0;
650}
651EXPORT_SYMBOL_GPL(pinctrl_generic_add_group);
652
653/**
654 * pinctrl_generic_remove_group() - removes a numbered pin group
655 * @pctldev: pin controller device
656 * @selector: group number
657 *
658 * Note that the caller must take care of locking.
659 */
660int pinctrl_generic_remove_group(struct pinctrl_dev *pctldev,
661 unsigned int selector)
662{
663 struct group_desc *group;
664
665 group = radix_tree_lookup(&pctldev->pin_group_tree,
666 selector);
667 if (!group)
668 return -ENOENT;
669
670 radix_tree_delete(&pctldev->pin_group_tree, selector);
671 devm_kfree(pctldev->dev, group);
672
673 pctldev->num_groups--;
674
675 return 0;
676}
677EXPORT_SYMBOL_GPL(pinctrl_generic_remove_group);
678
679/**
680 * pinctrl_generic_free_groups() - removes all pin groups
681 * @pctldev: pin controller device
682 *
683 * Note that the caller must take care of locking.
684 */
685static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
686{
687 struct radix_tree_iter iter;
688 struct group_desc *group;
689 unsigned long *indices;
690 void **slot;
691 int i = 0;
692
693 indices = devm_kzalloc(pctldev->dev, sizeof(*indices) *
694 pctldev->num_groups, GFP_KERNEL);
695 if (!indices)
696 return;
697
698 radix_tree_for_each_slot(slot, &pctldev->pin_group_tree, &iter, 0)
699 indices[i++] = iter.index;
700
701 for (i = 0; i < pctldev->num_groups; i++) {
702 group = radix_tree_lookup(&pctldev->pin_group_tree,
703 indices[i]);
704 radix_tree_delete(&pctldev->pin_group_tree, indices[i]);
705 devm_kfree(pctldev->dev, group);
706 }
707
708 pctldev->num_groups = 0;
709}
710
711#else
712static inline void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
713{
714}
Linus Walleijc033a712016-12-30 15:04:43 +0100715#endif /* CONFIG_GENERIC_PINCTRL_GROUPS */
Tony Lindgrenc7059c52016-12-27 09:20:00 -0800716
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530717/**
Linus Walleij7afde8b2011-10-19 17:07:16 +0200718 * pinctrl_get_group_selector() - returns the group selector for a group
719 * @pctldev: the pin controller handling the group
720 * @pin_group: the pin group to look up
721 */
722int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
723 const char *pin_group)
724{
725 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530726 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200727 unsigned group_selector = 0;
728
Viresh Kumard1e90e92012-03-30 11:25:40 +0530729 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200730 const char *gname = pctlops->get_group_name(pctldev,
731 group_selector);
732 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700733 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200734 "found group selector %u for %s\n",
735 group_selector,
736 pin_group);
737 return group_selector;
738 }
739
740 group_selector++;
741 }
742
Stephen Warren51cd24e2011-12-09 16:59:05 -0700743 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200744 pin_group);
745
746 return -EINVAL;
747}
748
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100749/**
Geert Uytterhoevenb217e432015-05-04 19:46:57 +0200750 * pinctrl_request_gpio() - request a single pin to be used as GPIO
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100751 * @gpio: the GPIO pin number from the GPIO subsystem number space
752 *
753 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
754 * as part of their gpio_request() semantics, platforms and individual drivers
755 * shall *NOT* request GPIO pins to be muxed in.
756 */
757int pinctrl_request_gpio(unsigned gpio)
758{
759 struct pinctrl_dev *pctldev;
760 struct pinctrl_gpio_range *range;
761 int ret;
762 int pin;
763
764 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700765 if (ret) {
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800766 if (pinctrl_ready_for_gpio_range(gpio))
767 ret = 0;
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800768 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700769 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100770
Axel Lin9b77ace2013-08-19 10:07:46 +0800771 mutex_lock(&pctldev->mutex);
772
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100773 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200774 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100775
Stephen Warren57b676f2012-03-02 13:05:44 -0700776 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
777
Axel Lin9b77ace2013-08-19 10:07:46 +0800778 mutex_unlock(&pctldev->mutex);
779
Stephen Warren57b676f2012-03-02 13:05:44 -0700780 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100781}
782EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
783
784/**
785 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
786 * @gpio: the GPIO pin number from the GPIO subsystem number space
787 *
788 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
789 * as part of their gpio_free() semantics, platforms and individual drivers
790 * shall *NOT* request GPIO pins to be muxed out.
791 */
792void pinctrl_free_gpio(unsigned gpio)
793{
794 struct pinctrl_dev *pctldev;
795 struct pinctrl_gpio_range *range;
796 int ret;
797 int pin;
798
799 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700800 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100801 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700802 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200803 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100804
805 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200806 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100807
Stephen Warren57b676f2012-03-02 13:05:44 -0700808 pinmux_free_gpio(pctldev, pin, range);
809
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200810 mutex_unlock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100811}
812EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
813
814static int pinctrl_gpio_direction(unsigned gpio, bool input)
815{
816 struct pinctrl_dev *pctldev;
817 struct pinctrl_gpio_range *range;
818 int ret;
819 int pin;
820
821 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200822 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100823 return ret;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200824 }
825
826 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100827
828 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200829 pin = gpio_to_pin(range, gpio);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200830 ret = pinmux_gpio_direction(pctldev, range, pin, input);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100831
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200832 mutex_unlock(&pctldev->mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200833
834 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100835}
836
837/**
838 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
839 * @gpio: the GPIO pin number from the GPIO subsystem number space
840 *
841 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
842 * as part of their gpio_direction_input() semantics, platforms and individual
843 * drivers shall *NOT* touch pin control GPIO calls.
844 */
845int pinctrl_gpio_direction_input(unsigned gpio)
846{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200847 return pinctrl_gpio_direction(gpio, true);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100848}
849EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
850
851/**
852 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output 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_output() semantics, platforms and individual
857 * drivers shall *NOT* touch pin control GPIO calls.
858 */
859int pinctrl_gpio_direction_output(unsigned gpio)
860{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200861 return pinctrl_gpio_direction(gpio, false);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100862}
863EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
864
Mika Westerberg15381bc2017-01-23 15:34:33 +0300865/**
866 * pinctrl_gpio_set_config() - Apply config to given GPIO pin
867 * @gpio: the GPIO pin number from the GPIO subsystem number space
868 * @config: the configuration to apply to the GPIO
869 *
870 * This function should *ONLY* be used from gpiolib-based GPIO drivers, if
871 * they need to call the underlying pin controller to change GPIO config
872 * (for example set debounce time).
873 */
874int pinctrl_gpio_set_config(unsigned gpio, unsigned long config)
875{
876 unsigned long configs[] = { config };
877 struct pinctrl_gpio_range *range;
878 struct pinctrl_dev *pctldev;
879 int ret, pin;
880
881 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
882 if (ret)
883 return ret;
884
885 mutex_lock(&pctldev->mutex);
886 pin = gpio_to_pin(range, gpio);
887 ret = pinconf_set_config(pctldev, pin, configs, ARRAY_SIZE(configs));
888 mutex_unlock(&pctldev->mutex);
889
890 return ret;
891}
892EXPORT_SYMBOL_GPL(pinctrl_gpio_set_config);
893
Stephen Warren6e5e9592012-03-02 13:05:47 -0700894static struct pinctrl_state *find_state(struct pinctrl *p,
895 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100896{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700897 struct pinctrl_state *state;
898
899 list_for_each_entry(state, &p->states, node)
900 if (!strcmp(state->name, name))
901 return state;
902
903 return NULL;
904}
905
906static struct pinctrl_state *create_state(struct pinctrl *p,
907 const char *name)
908{
909 struct pinctrl_state *state;
910
911 state = kzalloc(sizeof(*state), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -0800912 if (!state)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700913 return ERR_PTR(-ENOMEM);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700914
915 state->name = name;
916 INIT_LIST_HEAD(&state->settings);
917
918 list_add_tail(&state->node, &p->states);
919
920 return state;
921}
922
Tony Lindgren99e4f672016-12-27 09:19:59 -0800923static int add_setting(struct pinctrl *p, struct pinctrl_dev *pctldev,
924 struct pinctrl_map const *map)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700925{
926 struct pinctrl_state *state;
927 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700928 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700929
930 state = find_state(p, map->name);
931 if (!state)
932 state = create_state(p, map->name);
933 if (IS_ERR(state))
934 return PTR_ERR(state);
935
Stephen Warren1e2082b2012-03-02 13:05:48 -0700936 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
937 return 0;
938
Stephen Warren6e5e9592012-03-02 13:05:47 -0700939 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -0800940 if (!setting)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700941 return -ENOMEM;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700942
Stephen Warren1e2082b2012-03-02 13:05:48 -0700943 setting->type = map->type;
944
Tony Lindgren99e4f672016-12-27 09:19:59 -0800945 if (pctldev)
946 setting->pctldev = pctldev;
947 else
948 setting->pctldev =
949 get_pinctrl_dev_from_devname(map->ctrl_dev_name);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700950 if (setting->pctldev == NULL) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700951 kfree(setting);
Linus Walleij89216492012-12-11 14:14:32 +0100952 /* Do not defer probing of hogs (circular loop) */
953 if (!strcmp(map->ctrl_dev_name, map->dev_name))
954 return -ENODEV;
Linus Walleijc05127c2012-04-10 10:00:38 +0200955 /*
956 * OK let us guess that the driver is not there yet, and
957 * let's defer obtaining this pinctrl handle to later...
958 */
Linus Walleij89216492012-12-11 14:14:32 +0100959 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
960 map->ctrl_dev_name);
Linus Walleijc05127c2012-04-10 10:00:38 +0200961 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700962 }
963
Linus Walleij1a789582012-10-17 20:51:54 +0200964 setting->dev_name = map->dev_name;
965
Stephen Warren1e2082b2012-03-02 13:05:48 -0700966 switch (map->type) {
967 case PIN_MAP_TYPE_MUX_GROUP:
968 ret = pinmux_map_to_setting(map, setting);
969 break;
970 case PIN_MAP_TYPE_CONFIGS_PIN:
971 case PIN_MAP_TYPE_CONFIGS_GROUP:
972 ret = pinconf_map_to_setting(map, setting);
973 break;
974 default:
975 ret = -EINVAL;
976 break;
977 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700978 if (ret < 0) {
979 kfree(setting);
980 return ret;
981 }
982
983 list_add_tail(&setting->node, &state->settings);
984
985 return 0;
986}
987
988static struct pinctrl *find_pinctrl(struct device *dev)
989{
990 struct pinctrl *p;
991
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200992 mutex_lock(&pinctrl_list_mutex);
Stephen Warren1e2082b2012-03-02 13:05:48 -0700993 list_for_each_entry(p, &pinctrl_list, node)
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200994 if (p->dev == dev) {
995 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700996 return p;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200997 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700998
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200999 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001000 return NULL;
1001}
1002
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001003static void pinctrl_free(struct pinctrl *p, bool inlist);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001004
Tony Lindgren99e4f672016-12-27 09:19:59 -08001005static struct pinctrl *create_pinctrl(struct device *dev,
1006 struct pinctrl_dev *pctldev)
Stephen Warren6e5e9592012-03-02 13:05:47 -07001007{
1008 struct pinctrl *p;
1009 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001010 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001011 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001012 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001013 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001014
1015 /*
1016 * create the state cookie holder struct pinctrl for each
1017 * mapping, this is what consumers will get when requesting
1018 * a pin control handle with pinctrl_get()
1019 */
Stephen Warren02f5b982012-02-22 14:26:00 -07001020 p = kzalloc(sizeof(*p), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -08001021 if (!p)
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001022 return ERR_PTR(-ENOMEM);
Stephen Warren7ecdb162012-03-02 13:05:45 -07001023 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001024 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -06001025 INIT_LIST_HEAD(&p->dt_maps);
1026
Tony Lindgren99e4f672016-12-27 09:19:59 -08001027 ret = pinctrl_dt_to_map(p, pctldev);
Stephen Warren57291ce2012-03-23 10:29:46 -06001028 if (ret < 0) {
1029 kfree(p);
1030 return ERR_PTR(ret);
1031 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001032
1033 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001034
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001035 mutex_lock(&pinctrl_maps_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001036 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001037 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -07001038 /* Map must be for this device */
1039 if (strcmp(map->dev_name, devname))
1040 continue;
1041
Tony Lindgren99e4f672016-12-27 09:19:59 -08001042 ret = add_setting(p, pctldev, map);
Linus Walleij89216492012-12-11 14:14:32 +01001043 /*
1044 * At this point the adding of a setting may:
1045 *
1046 * - Defer, if the pinctrl device is not yet available
1047 * - Fail, if the pinctrl device is not yet available,
1048 * AND the setting is a hog. We cannot defer that, since
1049 * the hog will kick in immediately after the device
1050 * is registered.
1051 *
1052 * If the error returned was not -EPROBE_DEFER then we
1053 * accumulate the errors to see if we end up with
1054 * an -EPROBE_DEFER later, as that is the worst case.
1055 */
1056 if (ret == -EPROBE_DEFER) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001057 pinctrl_free(p, false);
1058 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001059 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001060 }
1061 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001062 mutex_unlock(&pinctrl_maps_mutex);
1063
Linus Walleij89216492012-12-11 14:14:32 +01001064 if (ret < 0) {
1065 /* If some other error than deferral occured, return here */
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001066 pinctrl_free(p, false);
Linus Walleij89216492012-12-11 14:14:32 +01001067 return ERR_PTR(ret);
1068 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001069
Linus Walleijab780292013-01-22 10:56:14 -07001070 kref_init(&p->users);
1071
Linus Walleijb0666ba2012-12-11 13:12:35 +01001072 /* Add the pinctrl handle to the global list */
Stanislaw Gruszka7b320cb2014-02-04 09:07:09 +01001073 mutex_lock(&pinctrl_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -07001074 list_add_tail(&p->node, &pinctrl_list);
Stanislaw Gruszka7b320cb2014-02-04 09:07:09 +01001075 mutex_unlock(&pinctrl_list_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001076
1077 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001078}
Stephen Warren7ecdb162012-03-02 13:05:45 -07001079
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001080/**
1081 * pinctrl_get() - retrieves the pinctrl handle for a device
1082 * @dev: the device to obtain the handle for
1083 */
1084struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warren6e5e9592012-03-02 13:05:47 -07001085{
1086 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001087
Stephen Warren6e5e9592012-03-02 13:05:47 -07001088 if (WARN_ON(!dev))
1089 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -07001090
Linus Walleijab780292013-01-22 10:56:14 -07001091 /*
1092 * See if somebody else (such as the device core) has already
1093 * obtained a handle to the pinctrl for this device. In that case,
1094 * return another pointer to it.
1095 */
Stephen Warren6e5e9592012-03-02 13:05:47 -07001096 p = find_pinctrl(dev);
Linus Walleijab780292013-01-22 10:56:14 -07001097 if (p != NULL) {
1098 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
1099 kref_get(&p->users);
1100 return p;
1101 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001102
Tony Lindgren99e4f672016-12-27 09:19:59 -08001103 return create_pinctrl(dev, NULL);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001104}
1105EXPORT_SYMBOL_GPL(pinctrl_get);
1106
Richard Genoudd3cee8302013-03-25 15:47:21 +01001107static void pinctrl_free_setting(bool disable_setting,
1108 struct pinctrl_setting *setting)
1109{
1110 switch (setting->type) {
1111 case PIN_MAP_TYPE_MUX_GROUP:
1112 if (disable_setting)
1113 pinmux_disable_setting(setting);
1114 pinmux_free_setting(setting);
1115 break;
1116 case PIN_MAP_TYPE_CONFIGS_PIN:
1117 case PIN_MAP_TYPE_CONFIGS_GROUP:
1118 pinconf_free_setting(setting);
1119 break;
1120 default:
1121 break;
1122 }
1123}
1124
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001125static void pinctrl_free(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -07001126{
Stephen Warren6e5e9592012-03-02 13:05:47 -07001127 struct pinctrl_state *state, *n1;
1128 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001129
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001130 mutex_lock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001131 list_for_each_entry_safe(state, n1, &p->states, node) {
1132 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Richard Genoudd3cee8302013-03-25 15:47:21 +01001133 pinctrl_free_setting(state == p->state, setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001134 list_del(&setting->node);
1135 kfree(setting);
1136 }
1137 list_del(&state->node);
1138 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -07001139 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001140
Stephen Warren57291ce2012-03-23 10:29:46 -06001141 pinctrl_dt_free_maps(p);
1142
Stephen Warren6e5e9592012-03-02 13:05:47 -07001143 if (inlist)
1144 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -07001145 kfree(p);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001146 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001147}
1148
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001149/**
Linus Walleijab780292013-01-22 10:56:14 -07001150 * pinctrl_release() - release the pinctrl handle
1151 * @kref: the kref in the pinctrl being released
1152 */
Sachin Kamat2917e832013-01-24 15:01:00 +05301153static void pinctrl_release(struct kref *kref)
Linus Walleijab780292013-01-22 10:56:14 -07001154{
1155 struct pinctrl *p = container_of(kref, struct pinctrl, users);
1156
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001157 pinctrl_free(p, true);
Linus Walleijab780292013-01-22 10:56:14 -07001158}
1159
1160/**
1161 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
Stephen Warren6e5e9592012-03-02 13:05:47 -07001162 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001163 */
1164void pinctrl_put(struct pinctrl *p)
1165{
Linus Walleijab780292013-01-22 10:56:14 -07001166 kref_put(&p->users, pinctrl_release);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001167}
1168EXPORT_SYMBOL_GPL(pinctrl_put);
1169
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001170/**
1171 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
1172 * @p: the pinctrl handle to retrieve the state from
1173 * @name: the state name to retrieve
1174 */
1175struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
1176 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -07001177{
Stephen Warren6e5e9592012-03-02 13:05:47 -07001178 struct pinctrl_state *state;
1179
1180 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +08001181 if (!state) {
1182 if (pinctrl_dummy_state) {
1183 /* create dummy state */
1184 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
1185 name);
1186 state = create_state(p, name);
Richard Genoudd599bfb2012-08-10 16:53:49 +02001187 } else
1188 state = ERR_PTR(-ENODEV);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +08001189 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001190
1191 return state;
1192}
Stephen Warren6e5e9592012-03-02 13:05:47 -07001193EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
1194
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001195/**
1196 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
1197 * @p: the pinctrl handle for the device that requests configuration
1198 * @state: the state handle to select/activate/program
1199 */
1200int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
Stephen Warren6e5e9592012-03-02 13:05:47 -07001201{
1202 struct pinctrl_setting *setting, *setting2;
Richard Genoud50cf7c82013-03-25 15:47:23 +01001203 struct pinctrl_state *old_state = p->state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001204 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -07001205
Stephen Warren6e5e9592012-03-02 13:05:47 -07001206 if (p->state == state)
1207 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -07001208
Stephen Warren6e5e9592012-03-02 13:05:47 -07001209 if (p->state) {
1210 /*
Fan Wu2243a872014-06-09 09:37:56 +08001211 * For each pinmux setting in the old state, forget SW's record
1212 * of mux owner for that pingroup. Any pingroups which are
1213 * still owned by the new state will be re-acquired by the call
1214 * to pinmux_enable_setting() in the loop below.
Stephen Warren6e5e9592012-03-02 13:05:47 -07001215 */
1216 list_for_each_entry(setting, &p->state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001217 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
1218 continue;
Fan Wu2243a872014-06-09 09:37:56 +08001219 pinmux_disable_setting(setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001220 }
1221 }
1222
Richard Genoud3102a762013-03-25 15:47:22 +01001223 p->state = NULL;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001224
1225 /* Apply all the settings for the new state */
1226 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001227 switch (setting->type) {
1228 case PIN_MAP_TYPE_MUX_GROUP:
1229 ret = pinmux_enable_setting(setting);
1230 break;
1231 case PIN_MAP_TYPE_CONFIGS_PIN:
1232 case PIN_MAP_TYPE_CONFIGS_GROUP:
1233 ret = pinconf_apply_setting(setting);
1234 break;
1235 default:
1236 ret = -EINVAL;
1237 break;
1238 }
Richard Genoud3102a762013-03-25 15:47:22 +01001239
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001240 if (ret < 0) {
Richard Genoud3102a762013-03-25 15:47:22 +01001241 goto unapply_new_state;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001242 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001243 }
1244
Richard Genoud3102a762013-03-25 15:47:22 +01001245 p->state = state;
1246
Stephen Warren7ecdb162012-03-02 13:05:45 -07001247 return 0;
Richard Genoud3102a762013-03-25 15:47:22 +01001248
1249unapply_new_state:
Richard Genoudda587512013-03-28 12:55:46 +01001250 dev_err(p->dev, "Error applying setting, reverse things back\n");
Richard Genoud3102a762013-03-25 15:47:22 +01001251
Richard Genoud3102a762013-03-25 15:47:22 +01001252 list_for_each_entry(setting2, &state->settings, node) {
1253 if (&setting2->node == &setting->node)
1254 break;
Richard Genoudaf606172013-03-29 10:03:26 +01001255 /*
1256 * All we can do here is pinmux_disable_setting.
1257 * That means that some pins are muxed differently now
1258 * than they were before applying the setting (We can't
1259 * "unmux a pin"!), but it's not a big deal since the pins
1260 * are free to be muxed by another apply_setting.
1261 */
1262 if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
1263 pinmux_disable_setting(setting2);
Richard Genoud3102a762013-03-25 15:47:22 +01001264 }
Richard Genoud8009d5f2013-03-28 12:55:47 +01001265
Richard Genoud385d9422013-03-29 10:03:27 +01001266 /* There's no infinite recursive loop here because p->state is NULL */
1267 if (old_state)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001268 pinctrl_select_state(p, old_state);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001269
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001270 return ret;
1271}
Stephen Warren6e5e9592012-03-02 13:05:47 -07001272EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001273
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001274static void devm_pinctrl_release(struct device *dev, void *res)
1275{
1276 pinctrl_put(*(struct pinctrl **)res);
1277}
1278
1279/**
1280 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
1281 * @dev: the device to obtain the handle for
1282 *
1283 * If there is a need to explicitly destroy the returned struct pinctrl,
1284 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1285 */
1286struct pinctrl *devm_pinctrl_get(struct device *dev)
1287{
1288 struct pinctrl **ptr, *p;
1289
1290 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
1291 if (!ptr)
1292 return ERR_PTR(-ENOMEM);
1293
1294 p = pinctrl_get(dev);
1295 if (!IS_ERR(p)) {
1296 *ptr = p;
1297 devres_add(dev, ptr);
1298 } else {
1299 devres_free(ptr);
1300 }
1301
1302 return p;
1303}
1304EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1305
1306static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1307{
1308 struct pinctrl **p = res;
1309
1310 return *p == data;
1311}
1312
1313/**
1314 * devm_pinctrl_put() - Resource managed pinctrl_put()
1315 * @p: the pinctrl handle to release
1316 *
1317 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1318 * this function will not need to be called and the resource management
1319 * code will ensure that the resource is freed.
1320 */
1321void devm_pinctrl_put(struct pinctrl *p)
1322{
Jingoo Hana72149e2013-02-26 11:34:07 +09001323 WARN_ON(devres_release(p->dev, devm_pinctrl_release,
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001324 devm_pinctrl_match, p));
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001325}
1326EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1327
Stephen Warren57291ce2012-03-23 10:29:46 -06001328int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
Doug Andersonc5272a22015-05-01 09:01:27 -07001329 bool dup)
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001330{
Stephen Warren1e2082b2012-03-02 13:05:48 -07001331 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001332 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001333
Masahiro Yamada7e9236f2015-05-28 21:52:59 +09001334 pr_debug("add %u pinctrl maps\n", num_maps);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001335
1336 /* First sanity check the new mapping */
1337 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001338 if (!maps[i].dev_name) {
1339 pr_err("failed to register map %s (%d): no device given\n",
1340 maps[i].name, i);
1341 return -EINVAL;
1342 }
1343
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001344 if (!maps[i].name) {
1345 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001346 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001347 return -EINVAL;
1348 }
1349
Stephen Warren1e2082b2012-03-02 13:05:48 -07001350 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1351 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001352 pr_err("failed to register map %s (%d): no pin control device given\n",
1353 maps[i].name, i);
1354 return -EINVAL;
1355 }
1356
Stephen Warren1e2082b2012-03-02 13:05:48 -07001357 switch (maps[i].type) {
1358 case PIN_MAP_TYPE_DUMMY_STATE:
1359 break;
1360 case PIN_MAP_TYPE_MUX_GROUP:
1361 ret = pinmux_validate_map(&maps[i], i);
1362 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001363 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001364 break;
1365 case PIN_MAP_TYPE_CONFIGS_PIN:
1366 case PIN_MAP_TYPE_CONFIGS_GROUP:
1367 ret = pinconf_validate_map(&maps[i], i);
1368 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001369 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001370 break;
1371 default:
1372 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001373 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -07001374 return -EINVAL;
1375 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001376 }
1377
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001378 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -08001379 if (!maps_node)
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001380 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001381
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001382 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -06001383 if (dup) {
1384 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
1385 GFP_KERNEL);
1386 if (!maps_node->maps) {
1387 pr_err("failed to duplicate mapping table\n");
1388 kfree(maps_node);
1389 return -ENOMEM;
1390 }
1391 } else {
1392 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001393 }
1394
Doug Andersonc5272a22015-05-01 09:01:27 -07001395 mutex_lock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001396 list_add_tail(&maps_node->node, &pinctrl_maps);
Doug Andersonc5272a22015-05-01 09:01:27 -07001397 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001398
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001399 return 0;
1400}
1401
Stephen Warren57291ce2012-03-23 10:29:46 -06001402/**
1403 * pinctrl_register_mappings() - register a set of pin controller mappings
1404 * @maps: the pincontrol mappings table to register. This should probably be
1405 * marked with __initdata so it can be discarded after boot. This
1406 * function will perform a shallow copy for the mapping entries.
1407 * @num_maps: the number of maps in the mapping table
1408 */
1409int pinctrl_register_mappings(struct pinctrl_map const *maps,
1410 unsigned num_maps)
1411{
Doug Andersonc5272a22015-05-01 09:01:27 -07001412 return pinctrl_register_map(maps, num_maps, true);
Stephen Warren57291ce2012-03-23 10:29:46 -06001413}
1414
1415void pinctrl_unregister_map(struct pinctrl_map const *map)
1416{
1417 struct pinctrl_maps *maps_node;
1418
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001419 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001420 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1421 if (maps_node->maps == map) {
1422 list_del(&maps_node->node);
Linus Walleijdb6c2c62013-07-23 01:43:20 +02001423 kfree(maps_node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001424 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001425 return;
1426 }
1427 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001428 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001429}
1430
Julien Delacou840a47b2012-12-10 14:47:33 +01001431/**
1432 * pinctrl_force_sleep() - turn a given controller device into sleep state
1433 * @pctldev: pin controller device
1434 */
1435int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1436{
1437 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
1438 return pinctrl_select_state(pctldev->p, pctldev->hog_sleep);
1439 return 0;
1440}
1441EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1442
1443/**
1444 * pinctrl_force_default() - turn a given controller device into default state
1445 * @pctldev: pin controller device
1446 */
1447int pinctrl_force_default(struct pinctrl_dev *pctldev)
1448{
1449 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
1450 return pinctrl_select_state(pctldev->p, pctldev->hog_default);
1451 return 0;
1452}
1453EXPORT_SYMBOL_GPL(pinctrl_force_default);
1454
Douglas Andersonef0eebc2015-10-20 21:15:06 -07001455/**
1456 * pinctrl_init_done() - tell pinctrl probe is done
1457 *
1458 * We'll use this time to switch the pins from "init" to "default" unless the
1459 * driver selected some other state.
1460 *
1461 * @dev: device to that's done probing
1462 */
1463int pinctrl_init_done(struct device *dev)
1464{
1465 struct dev_pin_info *pins = dev->pins;
1466 int ret;
1467
1468 if (!pins)
1469 return 0;
1470
1471 if (IS_ERR(pins->init_state))
1472 return 0; /* No such state */
1473
1474 if (pins->p->state != pins->init_state)
1475 return 0; /* Not at init anyway */
1476
1477 if (IS_ERR(pins->default_state))
1478 return 0; /* No default state */
1479
1480 ret = pinctrl_select_state(pins->p, pins->default_state);
1481 if (ret)
1482 dev_err(dev, "failed to activate default pinctrl state\n");
1483
1484 return ret;
1485}
1486
Linus Walleij14005ee2013-06-05 15:30:33 +02001487#ifdef CONFIG_PM
1488
1489/**
Tony Lindgrenf3333492013-07-18 08:15:04 -07001490 * pinctrl_pm_select_state() - select pinctrl state for PM
1491 * @dev: device to select default state for
1492 * @state: state to set
1493 */
1494static int pinctrl_pm_select_state(struct device *dev,
1495 struct pinctrl_state *state)
1496{
1497 struct dev_pin_info *pins = dev->pins;
1498 int ret;
1499
1500 if (IS_ERR(state))
1501 return 0; /* No such state */
1502 ret = pinctrl_select_state(pins->p, state);
1503 if (ret)
1504 dev_err(dev, "failed to activate pinctrl state %s\n",
1505 state->name);
1506 return ret;
1507}
1508
1509/**
Linus Walleij14005ee2013-06-05 15:30:33 +02001510 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1511 * @dev: device to select default state for
1512 */
1513int pinctrl_pm_select_default_state(struct device *dev)
1514{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001515 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001516 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001517
1518 return pinctrl_pm_select_state(dev, dev->pins->default_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001519}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001520EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001521
1522/**
1523 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1524 * @dev: device to select sleep state for
1525 */
1526int pinctrl_pm_select_sleep_state(struct device *dev)
1527{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001528 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001529 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001530
1531 return pinctrl_pm_select_state(dev, dev->pins->sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001532}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001533EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001534
1535/**
1536 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1537 * @dev: device to select idle state for
1538 */
1539int pinctrl_pm_select_idle_state(struct device *dev)
1540{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001541 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001542 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001543
1544 return pinctrl_pm_select_state(dev, dev->pins->idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001545}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001546EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001547#endif
1548
Linus Walleij2744e8a2011-05-02 20:50:54 +02001549#ifdef CONFIG_DEBUG_FS
1550
1551static int pinctrl_pins_show(struct seq_file *s, void *what)
1552{
1553 struct pinctrl_dev *pctldev = s->private;
1554 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001555 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001556
1557 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001558
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001559 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001560
Chanho Park706e8522012-01-03 16:47:50 +09001561 /* The pin number can be retrived from the pin controller descriptor */
1562 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001563 struct pin_desc *desc;
1564
Chanho Park706e8522012-01-03 16:47:50 +09001565 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001566 desc = pin_desc_get(pctldev, pin);
1567 /* Pin space may be sparse */
1568 if (desc == NULL)
1569 continue;
1570
Masahiro Yamadacf9d9942016-05-24 14:26:26 +09001571 seq_printf(s, "pin %d (%s) ", pin, desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001572
1573 /* Driver-specific info per pin */
1574 if (ops->pin_dbg_show)
1575 ops->pin_dbg_show(pctldev, s, pin);
1576
1577 seq_puts(s, "\n");
1578 }
1579
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001580 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001581
Linus Walleij2744e8a2011-05-02 20:50:54 +02001582 return 0;
1583}
1584
1585static int pinctrl_groups_show(struct seq_file *s, void *what)
1586{
1587 struct pinctrl_dev *pctldev = s->private;
1588 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301589 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001590
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001591 mutex_lock(&pctldev->mutex);
1592
Viresh Kumard1e90e92012-03-30 11:25:40 +05301593 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001594
Linus Walleij2744e8a2011-05-02 20:50:54 +02001595 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301596 while (selector < ngroups) {
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001597 const unsigned *pins = NULL;
1598 unsigned num_pins = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001599 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001600 const char *pname;
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001601 int ret = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001602 int i;
1603
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001604 if (ops->get_group_pins)
1605 ret = ops->get_group_pins(pctldev, selector,
1606 &pins, &num_pins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001607 if (ret)
1608 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1609 gname);
1610 else {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001611 seq_printf(s, "group: %s\n", gname);
1612 for (i = 0; i < num_pins; i++) {
1613 pname = pin_get_name(pctldev, pins[i]);
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001614 if (WARN_ON(!pname)) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001615 mutex_unlock(&pctldev->mutex);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001616 return -EINVAL;
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001617 }
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001618 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1619 }
1620 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001621 }
1622 selector++;
1623 }
1624
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001625 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001626
1627 return 0;
1628}
1629
1630static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1631{
1632 struct pinctrl_dev *pctldev = s->private;
1633 struct pinctrl_gpio_range *range = NULL;
1634
1635 seq_puts(s, "GPIO ranges handled:\n");
1636
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001637 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001638
Linus Walleij2744e8a2011-05-02 20:50:54 +02001639 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001640 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Christian Ruppertc8587ee2013-06-13 14:55:31 +02001641 if (range->pins) {
1642 int a;
1643 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
1644 range->id, range->name,
1645 range->base, (range->base + range->npins - 1));
1646 for (a = 0; a < range->npins - 1; a++)
1647 seq_printf(s, "%u, ", range->pins[a]);
1648 seq_printf(s, "%u}\n", range->pins[a]);
1649 }
1650 else
1651 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1652 range->id, range->name,
1653 range->base, (range->base + range->npins - 1),
1654 range->pin_base,
1655 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001656 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001657
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001658 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001659
1660 return 0;
1661}
1662
1663static int pinctrl_devices_show(struct seq_file *s, void *what)
1664{
1665 struct pinctrl_dev *pctldev;
1666
Linus Walleijae6b4d82011-10-19 18:14:33 +02001667 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001668
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001669 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001670
Linus Walleij2744e8a2011-05-02 20:50:54 +02001671 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1672 seq_printf(s, "%s ", pctldev->desc->name);
1673 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001674 seq_puts(s, "yes ");
1675 else
1676 seq_puts(s, "no ");
1677 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001678 seq_puts(s, "yes");
1679 else
1680 seq_puts(s, "no");
1681 seq_puts(s, "\n");
1682 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001683
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001684 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001685
1686 return 0;
1687}
1688
Stephen Warren1e2082b2012-03-02 13:05:48 -07001689static inline const char *map_type(enum pinctrl_map_type type)
1690{
1691 static const char * const names[] = {
1692 "INVALID",
1693 "DUMMY_STATE",
1694 "MUX_GROUP",
1695 "CONFIGS_PIN",
1696 "CONFIGS_GROUP",
1697 };
1698
1699 if (type >= ARRAY_SIZE(names))
1700 return "UNKNOWN";
1701
1702 return names[type];
1703}
1704
Stephen Warren3eedb432012-02-23 17:04:40 -07001705static int pinctrl_maps_show(struct seq_file *s, void *what)
1706{
1707 struct pinctrl_maps *maps_node;
1708 int i;
1709 struct pinctrl_map const *map;
1710
1711 seq_puts(s, "Pinctrl maps:\n");
1712
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001713 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001714 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001715 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1716 map->dev_name, map->name, map_type(map->type),
1717 map->type);
1718
1719 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1720 seq_printf(s, "controlling device %s\n",
1721 map->ctrl_dev_name);
1722
1723 switch (map->type) {
1724 case PIN_MAP_TYPE_MUX_GROUP:
1725 pinmux_show_map(s, map);
1726 break;
1727 case PIN_MAP_TYPE_CONFIGS_PIN:
1728 case PIN_MAP_TYPE_CONFIGS_GROUP:
1729 pinconf_show_map(s, map);
1730 break;
1731 default:
1732 break;
1733 }
1734
1735 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001736 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001737 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001738
1739 return 0;
1740}
1741
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001742static int pinctrl_show(struct seq_file *s, void *what)
1743{
1744 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001745 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001746 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001747
1748 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001749
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001750 mutex_lock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001751
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001752 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001753 seq_printf(s, "device: %s current state: %s\n",
1754 dev_name(p->dev),
1755 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001756
Stephen Warren6e5e9592012-03-02 13:05:47 -07001757 list_for_each_entry(state, &p->states, node) {
1758 seq_printf(s, " state: %s\n", state->name);
1759
1760 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001761 struct pinctrl_dev *pctldev = setting->pctldev;
1762
1763 seq_printf(s, " type: %s controller %s ",
1764 map_type(setting->type),
1765 pinctrl_dev_get_name(pctldev));
1766
1767 switch (setting->type) {
1768 case PIN_MAP_TYPE_MUX_GROUP:
1769 pinmux_show_setting(s, setting);
1770 break;
1771 case PIN_MAP_TYPE_CONFIGS_PIN:
1772 case PIN_MAP_TYPE_CONFIGS_GROUP:
1773 pinconf_show_setting(s, setting);
1774 break;
1775 default:
1776 break;
1777 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001778 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001779 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001780 }
1781
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001782 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001783
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001784 return 0;
1785}
1786
Linus Walleij2744e8a2011-05-02 20:50:54 +02001787static int pinctrl_pins_open(struct inode *inode, struct file *file)
1788{
1789 return single_open(file, pinctrl_pins_show, inode->i_private);
1790}
1791
1792static int pinctrl_groups_open(struct inode *inode, struct file *file)
1793{
1794 return single_open(file, pinctrl_groups_show, inode->i_private);
1795}
1796
1797static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1798{
1799 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1800}
1801
1802static int pinctrl_devices_open(struct inode *inode, struct file *file)
1803{
1804 return single_open(file, pinctrl_devices_show, NULL);
1805}
1806
Stephen Warren3eedb432012-02-23 17:04:40 -07001807static int pinctrl_maps_open(struct inode *inode, struct file *file)
1808{
1809 return single_open(file, pinctrl_maps_show, NULL);
1810}
1811
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001812static int pinctrl_open(struct inode *inode, struct file *file)
1813{
1814 return single_open(file, pinctrl_show, NULL);
1815}
1816
Linus Walleij2744e8a2011-05-02 20:50:54 +02001817static const struct file_operations pinctrl_pins_ops = {
1818 .open = pinctrl_pins_open,
1819 .read = seq_read,
1820 .llseek = seq_lseek,
1821 .release = single_release,
1822};
1823
1824static const struct file_operations pinctrl_groups_ops = {
1825 .open = pinctrl_groups_open,
1826 .read = seq_read,
1827 .llseek = seq_lseek,
1828 .release = single_release,
1829};
1830
1831static const struct file_operations pinctrl_gpioranges_ops = {
1832 .open = pinctrl_gpioranges_open,
1833 .read = seq_read,
1834 .llseek = seq_lseek,
1835 .release = single_release,
1836};
1837
1838static const struct file_operations pinctrl_devices_ops = {
1839 .open = pinctrl_devices_open,
1840 .read = seq_read,
1841 .llseek = seq_lseek,
1842 .release = single_release,
1843};
1844
Stephen Warren3eedb432012-02-23 17:04:40 -07001845static const struct file_operations pinctrl_maps_ops = {
1846 .open = pinctrl_maps_open,
1847 .read = seq_read,
1848 .llseek = seq_lseek,
1849 .release = single_release,
1850};
1851
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001852static const struct file_operations pinctrl_ops = {
1853 .open = pinctrl_open,
1854 .read = seq_read,
1855 .llseek = seq_lseek,
1856 .release = single_release,
1857};
1858
Linus Walleij2744e8a2011-05-02 20:50:54 +02001859static struct dentry *debugfs_root;
1860
1861static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1862{
Tony Lindgren02157162012-01-20 08:17:22 -08001863 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001864
Stephen Warren51cd24e2011-12-09 16:59:05 -07001865 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001866 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001867 pctldev->device_root = device_root;
1868
Linus Walleij2744e8a2011-05-02 20:50:54 +02001869 if (IS_ERR(device_root) || !device_root) {
1870 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001871 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001872 return;
1873 }
1874 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1875 device_root, pctldev, &pinctrl_pins_ops);
1876 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1877 device_root, pctldev, &pinctrl_groups_ops);
1878 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1879 device_root, pctldev, &pinctrl_gpioranges_ops);
Florian Vaussarde7f2a442014-02-05 07:51:22 +01001880 if (pctldev->desc->pmxops)
1881 pinmux_init_device_debugfs(device_root, pctldev);
1882 if (pctldev->desc->confops)
1883 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001884}
1885
Tony Lindgren02157162012-01-20 08:17:22 -08001886static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1887{
1888 debugfs_remove_recursive(pctldev->device_root);
1889}
1890
Linus Walleij2744e8a2011-05-02 20:50:54 +02001891static void pinctrl_init_debugfs(void)
1892{
1893 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1894 if (IS_ERR(debugfs_root) || !debugfs_root) {
1895 pr_warn("failed to create debugfs directory\n");
1896 debugfs_root = NULL;
1897 return;
1898 }
1899
1900 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1901 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001902 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1903 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001904 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1905 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001906}
1907
1908#else /* CONFIG_DEBUG_FS */
1909
1910static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1911{
1912}
1913
1914static void pinctrl_init_debugfs(void)
1915{
1916}
1917
Tony Lindgren02157162012-01-20 08:17:22 -08001918static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1919{
1920}
1921
Linus Walleij2744e8a2011-05-02 20:50:54 +02001922#endif
1923
Stephen Warrend26bc492012-03-16 14:54:25 -06001924static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1925{
1926 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1927
1928 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301929 !ops->get_groups_count ||
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001930 !ops->get_group_name)
Stephen Warrend26bc492012-03-16 14:54:25 -06001931 return -EINVAL;
1932
1933 return 0;
1934}
1935
Linus Walleij2744e8a2011-05-02 20:50:54 +02001936/**
Tony Lindgren950b0d92017-01-11 14:13:34 -08001937 * pinctrl_init_controller() - init a pin controller device
Linus Walleij2744e8a2011-05-02 20:50:54 +02001938 * @pctldesc: descriptor for this pin controller
1939 * @dev: parent device for this pin controller
1940 * @driver_data: private pin controller data for this pin controller
1941 */
Tony Lindgren950b0d92017-01-11 14:13:34 -08001942struct pinctrl_dev *pinctrl_init_controller(struct pinctrl_desc *pctldesc,
1943 struct device *dev,
1944 void *driver_data)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001945{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001946 struct pinctrl_dev *pctldev;
1947 int ret;
1948
Devendra Nagada9aecb2012-06-16 23:43:16 +05301949 if (!pctldesc)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001950 return ERR_PTR(-EINVAL);
Devendra Nagada9aecb2012-06-16 23:43:16 +05301951 if (!pctldesc->name)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001952 return ERR_PTR(-EINVAL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001953
Stephen Warren02f5b982012-02-22 14:26:00 -07001954 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Bjorn Andersson2104d122017-01-05 08:07:55 -08001955 if (!pctldev)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001956 return ERR_PTR(-ENOMEM);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001957
1958 /* Initialize pin control device struct */
1959 pctldev->owner = pctldesc->owner;
1960 pctldev->desc = pctldesc;
1961 pctldev->driver_data = driver_data;
1962 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleijc033a712016-12-30 15:04:43 +01001963#ifdef CONFIG_GENERIC_PINCTRL_GROUPS
Tony Lindgrenc7059c52016-12-27 09:20:00 -08001964 INIT_RADIX_TREE(&pctldev->pin_group_tree, GFP_KERNEL);
Linus Walleijc033a712016-12-30 15:04:43 +01001965#endif
Tony Lindgrena76edc82016-12-27 09:20:01 -08001966#ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
1967 INIT_RADIX_TREE(&pctldev->pin_function_tree, GFP_KERNEL);
1968#endif
Linus Walleij2744e8a2011-05-02 20:50:54 +02001969 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Thierry Reding46daed62017-01-12 17:03:34 +01001970 INIT_LIST_HEAD(&pctldev->node);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001971 pctldev->dev = dev;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001972 mutex_init(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001973
Stephen Warrend26bc492012-03-16 14:54:25 -06001974 /* check core ops for sanity */
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001975 ret = pinctrl_check_ops(pctldev);
1976 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001977 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06001978 goto out_err;
1979 }
1980
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001981 /* If we're implementing pinmuxing, check the ops for sanity */
1982 if (pctldesc->pmxops) {
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001983 ret = pinmux_check_ops(pctldev);
1984 if (ret)
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001985 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001986 }
1987
1988 /* If we're implementing pinconfig, check the ops for sanity */
1989 if (pctldesc->confops) {
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001990 ret = pinconf_check_ops(pctldev);
1991 if (ret)
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001992 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001993 }
1994
Linus Walleij2744e8a2011-05-02 20:50:54 +02001995 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02001996 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001997 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1998 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001999 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02002000 pinctrl_free_pindescs(pctldev, pctldesc->pins,
2001 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07002002 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02002003 }
2004
Linus Walleij2744e8a2011-05-02 20:50:54 +02002005 return pctldev;
2006
Stephen Warren51cd24e2011-12-09 16:59:05 -07002007out_err:
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002008 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07002009 kfree(pctldev);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09002010 return ERR_PTR(ret);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002011}
Tony Lindgren950b0d92017-01-11 14:13:34 -08002012
2013static int pinctrl_create_and_start(struct pinctrl_dev *pctldev)
2014{
2015 pctldev->p = create_pinctrl(pctldev->dev, pctldev);
2016 if (!IS_ERR(pctldev->p)) {
2017 kref_get(&pctldev->p->users);
2018 pctldev->hog_default =
2019 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
2020 if (IS_ERR(pctldev->hog_default)) {
2021 dev_dbg(pctldev->dev,
2022 "failed to lookup the default state\n");
2023 } else {
2024 if (pinctrl_select_state(pctldev->p,
2025 pctldev->hog_default))
2026 dev_err(pctldev->dev,
2027 "failed to select default state\n");
2028 }
2029
2030 pctldev->hog_sleep =
2031 pinctrl_lookup_state(pctldev->p,
2032 PINCTRL_STATE_SLEEP);
2033 if (IS_ERR(pctldev->hog_sleep))
2034 dev_dbg(pctldev->dev,
2035 "failed to lookup the sleep state\n");
2036 }
2037
2038 mutex_lock(&pinctrldev_list_mutex);
2039 list_add_tail(&pctldev->node, &pinctrldev_list);
2040 mutex_unlock(&pinctrldev_list_mutex);
2041
2042 pinctrl_init_device_debugfs(pctldev);
2043
2044 return 0;
2045}
2046
2047/**
2048 * pinctrl_register() - register a pin controller device
2049 * @pctldesc: descriptor for this pin controller
2050 * @dev: parent device for this pin controller
2051 * @driver_data: private pin controller data for this pin controller
2052 *
2053 * Note that pinctrl_register() is known to have problems as the pin
2054 * controller driver functions are called before the driver has a
2055 * struct pinctrl_dev handle. To avoid issues later on, please use the
2056 * new pinctrl_register_and_init() below instead.
2057 */
2058struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
2059 struct device *dev, void *driver_data)
2060{
2061 struct pinctrl_dev *pctldev;
2062 int error;
2063
2064 pctldev = pinctrl_init_controller(pctldesc, dev, driver_data);
2065 if (IS_ERR(pctldev))
2066 return pctldev;
2067
2068 error = pinctrl_create_and_start(pctldev);
2069 if (error) {
2070 mutex_destroy(&pctldev->mutex);
2071 kfree(pctldev);
2072
2073 return ERR_PTR(error);
2074 }
2075
2076 return pctldev;
2077
2078}
Linus Walleij2744e8a2011-05-02 20:50:54 +02002079EXPORT_SYMBOL_GPL(pinctrl_register);
2080
Tony Lindgren950b0d92017-01-11 14:13:34 -08002081int pinctrl_register_and_init(struct pinctrl_desc *pctldesc,
2082 struct device *dev, void *driver_data,
2083 struct pinctrl_dev **pctldev)
2084{
2085 struct pinctrl_dev *p;
2086 int error;
2087
2088 p = pinctrl_init_controller(pctldesc, dev, driver_data);
2089 if (IS_ERR(p))
2090 return PTR_ERR(p);
2091
2092 /*
2093 * We have pinctrl_start() call functions in the pin controller
2094 * driver with create_pinctrl() for at least dt_node_to_map(). So
2095 * let's make sure pctldev is properly initialized for the
2096 * pin controller driver before we do anything.
2097 */
2098 *pctldev = p;
2099
2100 error = pinctrl_create_and_start(p);
2101 if (error) {
2102 mutex_destroy(&p->mutex);
2103 kfree(p);
2104 *pctldev = NULL;
2105
2106 return error;
2107 }
2108
2109 return 0;
2110}
2111EXPORT_SYMBOL_GPL(pinctrl_register_and_init);
2112
Linus Walleij2744e8a2011-05-02 20:50:54 +02002113/**
2114 * pinctrl_unregister() - unregister pinmux
2115 * @pctldev: pin controller to unregister
2116 *
2117 * Called by pinmux drivers to unregister a pinmux.
2118 */
2119void pinctrl_unregister(struct pinctrl_dev *pctldev)
2120{
Dong Aisheng5d589b02012-05-23 21:22:40 +08002121 struct pinctrl_gpio_range *range, *n;
Jon Hunter3429fb32017-01-05 15:52:55 +00002122
Linus Walleij2744e8a2011-05-02 20:50:54 +02002123 if (pctldev == NULL)
2124 return;
2125
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002126 mutex_lock(&pctldev->mutex);
Tony Lindgren02157162012-01-20 08:17:22 -08002127 pinctrl_remove_device_debugfs(pctldev);
Jim Lindb93fac2015-01-08 20:25:05 +08002128 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07002129
Jon Hunter3429fb32017-01-05 15:52:55 +00002130 if (!IS_ERR_OR_NULL(pctldev->p))
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002131 pinctrl_put(pctldev->p);
Stephen Warren57b676f2012-03-02 13:05:44 -07002132
Jim Lindb93fac2015-01-08 20:25:05 +08002133 mutex_lock(&pinctrldev_list_mutex);
2134 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002135 /* TODO: check that no pinmuxes are still active? */
Thierry Reding46daed62017-01-12 17:03:34 +01002136 list_del(&pctldev->node);
Tony Lindgrena76edc82016-12-27 09:20:01 -08002137 pinmux_generic_free_functions(pctldev);
Tony Lindgrenc7059c52016-12-27 09:20:00 -08002138 pinctrl_generic_free_groups(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002139 /* Destroy descriptor tree */
2140 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
2141 pctldev->desc->npins);
Dong Aisheng5d589b02012-05-23 21:22:40 +08002142 /* remove gpio ranges map */
2143 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
2144 list_del(&range->node);
2145
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002146 mutex_unlock(&pctldev->mutex);
2147 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07002148 kfree(pctldev);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02002149 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02002150}
2151EXPORT_SYMBOL_GPL(pinctrl_unregister);
2152
Laxman Dewangan80e0f8d2016-02-24 14:12:59 +05302153static void devm_pinctrl_dev_release(struct device *dev, void *res)
2154{
2155 struct pinctrl_dev *pctldev = *(struct pinctrl_dev **)res;
2156
2157 pinctrl_unregister(pctldev);
2158}
2159
2160static int devm_pinctrl_dev_match(struct device *dev, void *res, void *data)
2161{
2162 struct pctldev **r = res;
2163
Laxman Dewangan3024f922016-02-24 14:44:07 +05302164 if (WARN_ON(!r || !*r))
Laxman Dewangan80e0f8d2016-02-24 14:12:59 +05302165 return 0;
2166
2167 return *r == data;
2168}
2169
2170/**
2171 * devm_pinctrl_register() - Resource managed version of pinctrl_register().
2172 * @dev: parent device for this pin controller
2173 * @pctldesc: descriptor for this pin controller
2174 * @driver_data: private pin controller data for this pin controller
2175 *
2176 * Returns an error pointer if pincontrol register failed. Otherwise
2177 * it returns valid pinctrl handle.
2178 *
2179 * The pinctrl device will be automatically released when the device is unbound.
2180 */
2181struct pinctrl_dev *devm_pinctrl_register(struct device *dev,
2182 struct pinctrl_desc *pctldesc,
2183 void *driver_data)
2184{
2185 struct pinctrl_dev **ptr, *pctldev;
2186
2187 ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
2188 if (!ptr)
2189 return ERR_PTR(-ENOMEM);
2190
2191 pctldev = pinctrl_register(pctldesc, dev, driver_data);
2192 if (IS_ERR(pctldev)) {
2193 devres_free(ptr);
2194 return pctldev;
2195 }
2196
2197 *ptr = pctldev;
2198 devres_add(dev, ptr);
2199
2200 return pctldev;
2201}
2202EXPORT_SYMBOL_GPL(devm_pinctrl_register);
2203
2204/**
Tony Lindgren950b0d92017-01-11 14:13:34 -08002205 * devm_pinctrl_register_and_init() - Resource managed pinctrl register and init
2206 * @dev: parent device for this pin controller
2207 * @pctldesc: descriptor for this pin controller
2208 * @driver_data: private pin controller data for this pin controller
2209 *
2210 * Returns an error pointer if pincontrol register failed. Otherwise
2211 * it returns valid pinctrl handle.
2212 *
2213 * The pinctrl device will be automatically released when the device is unbound.
2214 */
2215int devm_pinctrl_register_and_init(struct device *dev,
2216 struct pinctrl_desc *pctldesc,
2217 void *driver_data,
2218 struct pinctrl_dev **pctldev)
2219{
2220 struct pinctrl_dev **ptr;
2221 int error;
2222
2223 ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
2224 if (!ptr)
2225 return -ENOMEM;
2226
2227 error = pinctrl_register_and_init(pctldesc, dev, driver_data, pctldev);
2228 if (error) {
2229 devres_free(ptr);
2230 return error;
2231 }
2232
2233 *ptr = *pctldev;
2234 devres_add(dev, ptr);
2235
2236 return 0;
2237}
2238EXPORT_SYMBOL_GPL(devm_pinctrl_register_and_init);
2239
2240/**
Laxman Dewangan80e0f8d2016-02-24 14:12:59 +05302241 * devm_pinctrl_unregister() - Resource managed version of pinctrl_unregister().
2242 * @dev: device for which which resource was allocated
2243 * @pctldev: the pinctrl device to unregister.
2244 */
2245void devm_pinctrl_unregister(struct device *dev, struct pinctrl_dev *pctldev)
2246{
2247 WARN_ON(devres_release(dev, devm_pinctrl_dev_release,
2248 devm_pinctrl_dev_match, pctldev));
2249}
2250EXPORT_SYMBOL_GPL(devm_pinctrl_unregister);
2251
Linus Walleij2744e8a2011-05-02 20:50:54 +02002252static int __init pinctrl_init(void)
2253{
2254 pr_info("initialized pinctrl subsystem\n");
2255 pinctrl_init_debugfs();
2256 return 0;
2257}
2258
2259/* init early since many drivers really need to initialized pinmux early */
2260core_initcall(pinctrl_init);