blob: 69723e07036bafbd29c6d6deb66e2f7bd7f0133e [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,
228 unsigned number, const char *name)
229{
230 struct pin_desc *pindesc;
231
232 pindesc = pin_desc_get(pctldev, number);
233 if (pindesc != NULL) {
Masahiro Yamada2b38ca62015-07-21 15:25:26 +0900234 dev_err(pctldev->dev, "pin %d already registered\n", number);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200235 return -EINVAL;
236 }
237
238 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700239 if (pindesc == NULL) {
240 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200241 return -ENOMEM;
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700242 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200243
Linus Walleij2744e8a2011-05-02 20:50:54 +0200244 /* Set owner */
245 pindesc->pctldev = pctldev;
246
Stephen Warren9af1e442011-10-19 16:19:27 -0600247 /* Copy basic pin info */
Linus Walleij8dc6ae42012-02-01 18:11:40 +0100248 if (name) {
Linus Walleijca53c5f2011-12-14 20:33:37 +0100249 pindesc->name = name;
250 } else {
251 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
Sachin Kamateb26cc92012-09-25 12:41:40 +0530252 if (pindesc->name == NULL) {
253 kfree(pindesc);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100254 return -ENOMEM;
Sachin Kamateb26cc92012-09-25 12:41:40 +0530255 }
Linus Walleijca53c5f2011-12-14 20:33:37 +0100256 pindesc->dynamic_name = true;
257 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200258
Linus Walleij2744e8a2011-05-02 20:50:54 +0200259 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200260 pr_debug("registered pin %d (%s) on %s\n",
Linus Walleijca53c5f2011-12-14 20:33:37 +0100261 number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200262 return 0;
263}
264
265static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
266 struct pinctrl_pin_desc const *pins,
267 unsigned num_descs)
268{
269 unsigned i;
270 int ret = 0;
271
272 for (i = 0; i < num_descs; i++) {
273 ret = pinctrl_register_one_pin(pctldev,
274 pins[i].number, pins[i].name);
275 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
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200352 mutex_lock(&pinctrldev_list_mutex);
353
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800354 /* Loop over the pin controllers */
355 list_for_each_entry(pctldev, &pinctrldev_list, node) {
356 /* Loop over the ranges */
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800357 mutex_lock(&pctldev->mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800358 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
359 /* Check if any gpio range overlapped with gpio chip */
360 if (range->base + range->npins - 1 < chip->base ||
361 range->base > chip->base + chip->ngpio - 1)
362 continue;
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800363 mutex_unlock(&pctldev->mutex);
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200364 mutex_unlock(&pinctrldev_list_mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800365 return true;
366 }
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800367 mutex_unlock(&pctldev->mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800368 }
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200369
370 mutex_unlock(&pinctrldev_list_mutex);
371
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800372 return false;
373}
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800374#else
375static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
376#endif
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800377
378/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200379 * pinctrl_get_device_gpio_range() - find device for GPIO range
380 * @gpio: the pin to locate the pin controller for
381 * @outdev: the pin control device if found
382 * @outrange: the GPIO range if found
383 *
384 * Find the pin controller handling a certain GPIO pin from the pinspace of
385 * the GPIO subsystem, return the device and the matching GPIO range. Returns
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800386 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
387 * may still have not been registered.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200388 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700389static int pinctrl_get_device_gpio_range(unsigned gpio,
390 struct pinctrl_dev **outdev,
391 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200392{
393 struct pinctrl_dev *pctldev = NULL;
394
Axel Linf0059022013-08-18 20:34:22 +0800395 mutex_lock(&pinctrldev_list_mutex);
396
Linus Walleij2744e8a2011-05-02 20:50:54 +0200397 /* Loop over the pin controllers */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200398 list_for_each_entry(pctldev, &pinctrldev_list, node) {
399 struct pinctrl_gpio_range *range;
400
401 range = pinctrl_match_gpio_range(pctldev, gpio);
402 if (range != NULL) {
403 *outdev = pctldev;
404 *outrange = range;
Axel Linf0059022013-08-18 20:34:22 +0800405 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200406 return 0;
407 }
408 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200409
Axel Linf0059022013-08-18 20:34:22 +0800410 mutex_unlock(&pinctrldev_list_mutex);
411
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800412 return -EPROBE_DEFER;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200413}
414
415/**
416 * pinctrl_add_gpio_range() - register a GPIO range for a controller
417 * @pctldev: pin controller device to add the range to
418 * @range: the GPIO range to add
419 *
420 * This adds a range of GPIOs to be handled by a certain pin controller. Call
421 * this to register handled ranges after registering your pin controller.
422 */
423void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
424 struct pinctrl_gpio_range *range)
425{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200426 mutex_lock(&pctldev->mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700427 list_add_tail(&range->node, &pctldev->gpio_ranges);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200428 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200429}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700430EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200431
Dong Aisheng3e5e00b2012-05-23 21:22:41 +0800432void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
433 struct pinctrl_gpio_range *ranges,
434 unsigned nranges)
435{
436 int i;
437
438 for (i = 0; i < nranges; i++)
439 pinctrl_add_gpio_range(pctldev, &ranges[i]);
440}
441EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
442
Linus Walleij192c3692012-11-20 14:03:37 +0100443struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530444 struct pinctrl_gpio_range *range)
445{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200446 struct pinctrl_dev *pctldev;
447
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200448 pctldev = get_pinctrl_dev_from_devname(devname);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530449
Linus Walleijdfa97512012-11-20 14:54:18 +0100450 /*
451 * If we can't find this device, let's assume that is because
452 * it has not probed yet, so the driver trying to register this
453 * range need to defer probing.
454 */
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200455 if (!pctldev) {
Linus Walleijdfa97512012-11-20 14:54:18 +0100456 return ERR_PTR(-EPROBE_DEFER);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200457 }
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530458 pinctrl_add_gpio_range(pctldev, range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200459
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530460 return pctldev;
461}
Linus Walleij192c3692012-11-20 14:03:37 +0100462EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530463
Christian Ruppert586a87e2013-10-15 15:37:54 +0200464int pinctrl_get_group_pins(struct pinctrl_dev *pctldev, const char *pin_group,
465 const unsigned **pins, unsigned *num_pins)
466{
467 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
468 int gs;
469
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +0200470 if (!pctlops->get_group_pins)
471 return -EINVAL;
472
Christian Ruppert586a87e2013-10-15 15:37:54 +0200473 gs = pinctrl_get_group_selector(pctldev, pin_group);
474 if (gs < 0)
475 return gs;
476
477 return pctlops->get_group_pins(pctldev, gs, pins, num_pins);
478}
479EXPORT_SYMBOL_GPL(pinctrl_get_group_pins);
480
Linus Walleij2744e8a2011-05-02 20:50:54 +0200481/**
Linus Walleij9afbefb2012-11-20 14:25:07 +0100482 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
483 * @pctldev: the pin controller device to look in
484 * @pin: a controller-local number to find the range for
485 */
486struct pinctrl_gpio_range *
487pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
488 unsigned int pin)
489{
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800490 struct pinctrl_gpio_range *range;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100491
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200492 mutex_lock(&pctldev->mutex);
Linus Walleij9afbefb2012-11-20 14:25:07 +0100493 /* Loop over the ranges */
494 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
495 /* Check if we're in the valid range */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200496 if (range->pins) {
497 int a;
498 for (a = 0; a < range->npins; a++) {
499 if (range->pins[a] == pin)
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800500 goto out;
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200501 }
502 } else if (pin >= range->pin_base &&
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800503 pin < range->pin_base + range->npins)
504 goto out;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100505 }
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800506 range = NULL;
507out:
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200508 mutex_unlock(&pctldev->mutex);
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800509 return range;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100510}
511EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
512
513/**
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530514 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
515 * @pctldev: pin controller device to remove the range from
516 * @range: the GPIO range to remove
517 */
518void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
519 struct pinctrl_gpio_range *range)
520{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200521 mutex_lock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530522 list_del(&range->node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200523 mutex_unlock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530524}
525EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
526
527/**
Linus Walleij7afde8b2011-10-19 17:07:16 +0200528 * pinctrl_get_group_selector() - returns the group selector for a group
529 * @pctldev: the pin controller handling the group
530 * @pin_group: the pin group to look up
531 */
532int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
533 const char *pin_group)
534{
535 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530536 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200537 unsigned group_selector = 0;
538
Viresh Kumard1e90e92012-03-30 11:25:40 +0530539 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200540 const char *gname = pctlops->get_group_name(pctldev,
541 group_selector);
542 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700543 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200544 "found group selector %u for %s\n",
545 group_selector,
546 pin_group);
547 return group_selector;
548 }
549
550 group_selector++;
551 }
552
Stephen Warren51cd24e2011-12-09 16:59:05 -0700553 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200554 pin_group);
555
556 return -EINVAL;
557}
558
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100559/**
Geert Uytterhoevenb217e432015-05-04 19:46:57 +0200560 * pinctrl_request_gpio() - request a single pin to be used as GPIO
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100561 * @gpio: the GPIO pin number from the GPIO subsystem number space
562 *
563 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
564 * as part of their gpio_request() semantics, platforms and individual drivers
565 * shall *NOT* request GPIO pins to be muxed in.
566 */
567int pinctrl_request_gpio(unsigned gpio)
568{
569 struct pinctrl_dev *pctldev;
570 struct pinctrl_gpio_range *range;
571 int ret;
572 int pin;
573
574 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700575 if (ret) {
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800576 if (pinctrl_ready_for_gpio_range(gpio))
577 ret = 0;
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800578 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700579 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100580
Axel Lin9b77ace2013-08-19 10:07:46 +0800581 mutex_lock(&pctldev->mutex);
582
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100583 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200584 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100585
Stephen Warren57b676f2012-03-02 13:05:44 -0700586 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
587
Axel Lin9b77ace2013-08-19 10:07:46 +0800588 mutex_unlock(&pctldev->mutex);
589
Stephen Warren57b676f2012-03-02 13:05:44 -0700590 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100591}
592EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
593
594/**
595 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
596 * @gpio: the GPIO pin number from the GPIO subsystem number space
597 *
598 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
599 * as part of their gpio_free() semantics, platforms and individual drivers
600 * shall *NOT* request GPIO pins to be muxed out.
601 */
602void pinctrl_free_gpio(unsigned gpio)
603{
604 struct pinctrl_dev *pctldev;
605 struct pinctrl_gpio_range *range;
606 int ret;
607 int pin;
608
609 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700610 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100611 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700612 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200613 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100614
615 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200616 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100617
Stephen Warren57b676f2012-03-02 13:05:44 -0700618 pinmux_free_gpio(pctldev, pin, range);
619
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200620 mutex_unlock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100621}
622EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
623
624static int pinctrl_gpio_direction(unsigned gpio, bool input)
625{
626 struct pinctrl_dev *pctldev;
627 struct pinctrl_gpio_range *range;
628 int ret;
629 int pin;
630
631 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200632 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100633 return ret;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200634 }
635
636 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100637
638 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200639 pin = gpio_to_pin(range, gpio);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200640 ret = pinmux_gpio_direction(pctldev, range, pin, input);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100641
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200642 mutex_unlock(&pctldev->mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200643
644 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100645}
646
647/**
648 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
649 * @gpio: the GPIO pin number from the GPIO subsystem number space
650 *
651 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
652 * as part of their gpio_direction_input() semantics, platforms and individual
653 * drivers shall *NOT* touch pin control GPIO calls.
654 */
655int pinctrl_gpio_direction_input(unsigned gpio)
656{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200657 return pinctrl_gpio_direction(gpio, true);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100658}
659EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
660
661/**
662 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
663 * @gpio: the GPIO pin number from the GPIO subsystem number space
664 *
665 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
666 * as part of their gpio_direction_output() semantics, platforms and individual
667 * drivers shall *NOT* touch pin control GPIO calls.
668 */
669int pinctrl_gpio_direction_output(unsigned gpio)
670{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200671 return pinctrl_gpio_direction(gpio, false);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100672}
673EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
674
Stephen Warren6e5e9592012-03-02 13:05:47 -0700675static struct pinctrl_state *find_state(struct pinctrl *p,
676 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100677{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700678 struct pinctrl_state *state;
679
680 list_for_each_entry(state, &p->states, node)
681 if (!strcmp(state->name, name))
682 return state;
683
684 return NULL;
685}
686
687static struct pinctrl_state *create_state(struct pinctrl *p,
688 const char *name)
689{
690 struct pinctrl_state *state;
691
692 state = kzalloc(sizeof(*state), GFP_KERNEL);
693 if (state == NULL) {
694 dev_err(p->dev,
695 "failed to alloc struct pinctrl_state\n");
696 return ERR_PTR(-ENOMEM);
697 }
698
699 state->name = name;
700 INIT_LIST_HEAD(&state->settings);
701
702 list_add_tail(&state->node, &p->states);
703
704 return state;
705}
706
707static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
708{
709 struct pinctrl_state *state;
710 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700711 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700712
713 state = find_state(p, map->name);
714 if (!state)
715 state = create_state(p, map->name);
716 if (IS_ERR(state))
717 return PTR_ERR(state);
718
Stephen Warren1e2082b2012-03-02 13:05:48 -0700719 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
720 return 0;
721
Stephen Warren6e5e9592012-03-02 13:05:47 -0700722 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
723 if (setting == NULL) {
724 dev_err(p->dev,
725 "failed to alloc struct pinctrl_setting\n");
726 return -ENOMEM;
727 }
728
Stephen Warren1e2082b2012-03-02 13:05:48 -0700729 setting->type = map->type;
730
Stephen Warren6e5e9592012-03-02 13:05:47 -0700731 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
732 if (setting->pctldev == NULL) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700733 kfree(setting);
Linus Walleij89216492012-12-11 14:14:32 +0100734 /* Do not defer probing of hogs (circular loop) */
735 if (!strcmp(map->ctrl_dev_name, map->dev_name))
736 return -ENODEV;
Linus Walleijc05127c2012-04-10 10:00:38 +0200737 /*
738 * OK let us guess that the driver is not there yet, and
739 * let's defer obtaining this pinctrl handle to later...
740 */
Linus Walleij89216492012-12-11 14:14:32 +0100741 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
742 map->ctrl_dev_name);
Linus Walleijc05127c2012-04-10 10:00:38 +0200743 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700744 }
745
Linus Walleij1a789582012-10-17 20:51:54 +0200746 setting->dev_name = map->dev_name;
747
Stephen Warren1e2082b2012-03-02 13:05:48 -0700748 switch (map->type) {
749 case PIN_MAP_TYPE_MUX_GROUP:
750 ret = pinmux_map_to_setting(map, setting);
751 break;
752 case PIN_MAP_TYPE_CONFIGS_PIN:
753 case PIN_MAP_TYPE_CONFIGS_GROUP:
754 ret = pinconf_map_to_setting(map, setting);
755 break;
756 default:
757 ret = -EINVAL;
758 break;
759 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700760 if (ret < 0) {
761 kfree(setting);
762 return ret;
763 }
764
765 list_add_tail(&setting->node, &state->settings);
766
767 return 0;
768}
769
770static struct pinctrl *find_pinctrl(struct device *dev)
771{
772 struct pinctrl *p;
773
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200774 mutex_lock(&pinctrl_list_mutex);
Stephen Warren1e2082b2012-03-02 13:05:48 -0700775 list_for_each_entry(p, &pinctrl_list, node)
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200776 if (p->dev == dev) {
777 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700778 return p;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200779 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700780
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200781 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700782 return NULL;
783}
784
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200785static void pinctrl_free(struct pinctrl *p, bool inlist);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700786
787static struct pinctrl *create_pinctrl(struct device *dev)
788{
789 struct pinctrl *p;
790 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700791 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100792 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700793 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700794 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100795
796 /*
797 * create the state cookie holder struct pinctrl for each
798 * mapping, this is what consumers will get when requesting
799 * a pin control handle with pinctrl_get()
800 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700801 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700802 if (p == NULL) {
803 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100804 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700805 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700806 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700807 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -0600808 INIT_LIST_HEAD(&p->dt_maps);
809
810 ret = pinctrl_dt_to_map(p);
811 if (ret < 0) {
812 kfree(p);
813 return ERR_PTR(ret);
814 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700815
816 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100817
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200818 mutex_lock(&pinctrl_maps_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100819 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700820 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -0700821 /* Map must be for this device */
822 if (strcmp(map->dev_name, devname))
823 continue;
824
Stephen Warren6e5e9592012-03-02 13:05:47 -0700825 ret = add_setting(p, map);
Linus Walleij89216492012-12-11 14:14:32 +0100826 /*
827 * At this point the adding of a setting may:
828 *
829 * - Defer, if the pinctrl device is not yet available
830 * - Fail, if the pinctrl device is not yet available,
831 * AND the setting is a hog. We cannot defer that, since
832 * the hog will kick in immediately after the device
833 * is registered.
834 *
835 * If the error returned was not -EPROBE_DEFER then we
836 * accumulate the errors to see if we end up with
837 * an -EPROBE_DEFER later, as that is the worst case.
838 */
839 if (ret == -EPROBE_DEFER) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200840 pinctrl_free(p, false);
841 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700842 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100843 }
844 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200845 mutex_unlock(&pinctrl_maps_mutex);
846
Linus Walleij89216492012-12-11 14:14:32 +0100847 if (ret < 0) {
848 /* If some other error than deferral occured, return here */
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200849 pinctrl_free(p, false);
Linus Walleij89216492012-12-11 14:14:32 +0100850 return ERR_PTR(ret);
851 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100852
Linus Walleijab780292013-01-22 10:56:14 -0700853 kref_init(&p->users);
854
Linus Walleijb0666ba2012-12-11 13:12:35 +0100855 /* Add the pinctrl handle to the global list */
Stanislaw Gruszka7b320cb2014-02-04 09:07:09 +0100856 mutex_lock(&pinctrl_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700857 list_add_tail(&p->node, &pinctrl_list);
Stanislaw Gruszka7b320cb2014-02-04 09:07:09 +0100858 mutex_unlock(&pinctrl_list_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100859
860 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700861}
Stephen Warren7ecdb162012-03-02 13:05:45 -0700862
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200863/**
864 * pinctrl_get() - retrieves the pinctrl handle for a device
865 * @dev: the device to obtain the handle for
866 */
867struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700868{
869 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700870
Stephen Warren6e5e9592012-03-02 13:05:47 -0700871 if (WARN_ON(!dev))
872 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700873
Linus Walleijab780292013-01-22 10:56:14 -0700874 /*
875 * See if somebody else (such as the device core) has already
876 * obtained a handle to the pinctrl for this device. In that case,
877 * return another pointer to it.
878 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700879 p = find_pinctrl(dev);
Linus Walleijab780292013-01-22 10:56:14 -0700880 if (p != NULL) {
881 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
882 kref_get(&p->users);
883 return p;
884 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700885
Richard Genoudd599bfb2012-08-10 16:53:49 +0200886 return create_pinctrl(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100887}
888EXPORT_SYMBOL_GPL(pinctrl_get);
889
Richard Genoudd3cee8302013-03-25 15:47:21 +0100890static void pinctrl_free_setting(bool disable_setting,
891 struct pinctrl_setting *setting)
892{
893 switch (setting->type) {
894 case PIN_MAP_TYPE_MUX_GROUP:
895 if (disable_setting)
896 pinmux_disable_setting(setting);
897 pinmux_free_setting(setting);
898 break;
899 case PIN_MAP_TYPE_CONFIGS_PIN:
900 case PIN_MAP_TYPE_CONFIGS_GROUP:
901 pinconf_free_setting(setting);
902 break;
903 default:
904 break;
905 }
906}
907
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200908static void pinctrl_free(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -0700909{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700910 struct pinctrl_state *state, *n1;
911 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700912
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200913 mutex_lock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700914 list_for_each_entry_safe(state, n1, &p->states, node) {
915 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Richard Genoudd3cee8302013-03-25 15:47:21 +0100916 pinctrl_free_setting(state == p->state, setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700917 list_del(&setting->node);
918 kfree(setting);
919 }
920 list_del(&state->node);
921 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700922 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700923
Stephen Warren57291ce2012-03-23 10:29:46 -0600924 pinctrl_dt_free_maps(p);
925
Stephen Warren6e5e9592012-03-02 13:05:47 -0700926 if (inlist)
927 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700928 kfree(p);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200929 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700930}
931
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100932/**
Linus Walleijab780292013-01-22 10:56:14 -0700933 * pinctrl_release() - release the pinctrl handle
934 * @kref: the kref in the pinctrl being released
935 */
Sachin Kamat2917e832013-01-24 15:01:00 +0530936static void pinctrl_release(struct kref *kref)
Linus Walleijab780292013-01-22 10:56:14 -0700937{
938 struct pinctrl *p = container_of(kref, struct pinctrl, users);
939
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200940 pinctrl_free(p, true);
Linus Walleijab780292013-01-22 10:56:14 -0700941}
942
943/**
944 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
Stephen Warren6e5e9592012-03-02 13:05:47 -0700945 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100946 */
947void pinctrl_put(struct pinctrl *p)
948{
Linus Walleijab780292013-01-22 10:56:14 -0700949 kref_put(&p->users, pinctrl_release);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100950}
951EXPORT_SYMBOL_GPL(pinctrl_put);
952
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200953/**
954 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
955 * @p: the pinctrl handle to retrieve the state from
956 * @name: the state name to retrieve
957 */
958struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
959 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -0700960{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700961 struct pinctrl_state *state;
962
963 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800964 if (!state) {
965 if (pinctrl_dummy_state) {
966 /* create dummy state */
967 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
968 name);
969 state = create_state(p, name);
Richard Genoudd599bfb2012-08-10 16:53:49 +0200970 } else
971 state = ERR_PTR(-ENODEV);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800972 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700973
974 return state;
975}
Stephen Warren6e5e9592012-03-02 13:05:47 -0700976EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
977
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200978/**
979 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
980 * @p: the pinctrl handle for the device that requests configuration
981 * @state: the state handle to select/activate/program
982 */
983int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700984{
985 struct pinctrl_setting *setting, *setting2;
Richard Genoud50cf7c82013-03-25 15:47:23 +0100986 struct pinctrl_state *old_state = p->state;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700987 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700988
Stephen Warren6e5e9592012-03-02 13:05:47 -0700989 if (p->state == state)
990 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700991
Stephen Warren6e5e9592012-03-02 13:05:47 -0700992 if (p->state) {
993 /*
Fan Wu2243a872014-06-09 09:37:56 +0800994 * For each pinmux setting in the old state, forget SW's record
995 * of mux owner for that pingroup. Any pingroups which are
996 * still owned by the new state will be re-acquired by the call
997 * to pinmux_enable_setting() in the loop below.
Stephen Warren6e5e9592012-03-02 13:05:47 -0700998 */
999 list_for_each_entry(setting, &p->state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001000 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
1001 continue;
Fan Wu2243a872014-06-09 09:37:56 +08001002 pinmux_disable_setting(setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001003 }
1004 }
1005
Richard Genoud3102a762013-03-25 15:47:22 +01001006 p->state = NULL;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001007
1008 /* Apply all the settings for the new state */
1009 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001010 switch (setting->type) {
1011 case PIN_MAP_TYPE_MUX_GROUP:
1012 ret = pinmux_enable_setting(setting);
1013 break;
1014 case PIN_MAP_TYPE_CONFIGS_PIN:
1015 case PIN_MAP_TYPE_CONFIGS_GROUP:
1016 ret = pinconf_apply_setting(setting);
1017 break;
1018 default:
1019 ret = -EINVAL;
1020 break;
1021 }
Richard Genoud3102a762013-03-25 15:47:22 +01001022
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001023 if (ret < 0) {
Richard Genoud3102a762013-03-25 15:47:22 +01001024 goto unapply_new_state;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001025 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001026 }
1027
Richard Genoud3102a762013-03-25 15:47:22 +01001028 p->state = state;
1029
Stephen Warren7ecdb162012-03-02 13:05:45 -07001030 return 0;
Richard Genoud3102a762013-03-25 15:47:22 +01001031
1032unapply_new_state:
Richard Genoudda587512013-03-28 12:55:46 +01001033 dev_err(p->dev, "Error applying setting, reverse things back\n");
Richard Genoud3102a762013-03-25 15:47:22 +01001034
Richard Genoud3102a762013-03-25 15:47:22 +01001035 list_for_each_entry(setting2, &state->settings, node) {
1036 if (&setting2->node == &setting->node)
1037 break;
Richard Genoudaf606172013-03-29 10:03:26 +01001038 /*
1039 * All we can do here is pinmux_disable_setting.
1040 * That means that some pins are muxed differently now
1041 * than they were before applying the setting (We can't
1042 * "unmux a pin"!), but it's not a big deal since the pins
1043 * are free to be muxed by another apply_setting.
1044 */
1045 if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
1046 pinmux_disable_setting(setting2);
Richard Genoud3102a762013-03-25 15:47:22 +01001047 }
Richard Genoud8009d5f2013-03-28 12:55:47 +01001048
Richard Genoud385d9422013-03-29 10:03:27 +01001049 /* There's no infinite recursive loop here because p->state is NULL */
1050 if (old_state)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001051 pinctrl_select_state(p, old_state);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001052
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001053 return ret;
1054}
Stephen Warren6e5e9592012-03-02 13:05:47 -07001055EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001056
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001057static void devm_pinctrl_release(struct device *dev, void *res)
1058{
1059 pinctrl_put(*(struct pinctrl **)res);
1060}
1061
1062/**
1063 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
1064 * @dev: the device to obtain the handle for
1065 *
1066 * If there is a need to explicitly destroy the returned struct pinctrl,
1067 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1068 */
1069struct pinctrl *devm_pinctrl_get(struct device *dev)
1070{
1071 struct pinctrl **ptr, *p;
1072
1073 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
1074 if (!ptr)
1075 return ERR_PTR(-ENOMEM);
1076
1077 p = pinctrl_get(dev);
1078 if (!IS_ERR(p)) {
1079 *ptr = p;
1080 devres_add(dev, ptr);
1081 } else {
1082 devres_free(ptr);
1083 }
1084
1085 return p;
1086}
1087EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1088
1089static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1090{
1091 struct pinctrl **p = res;
1092
1093 return *p == data;
1094}
1095
1096/**
1097 * devm_pinctrl_put() - Resource managed pinctrl_put()
1098 * @p: the pinctrl handle to release
1099 *
1100 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1101 * this function will not need to be called and the resource management
1102 * code will ensure that the resource is freed.
1103 */
1104void devm_pinctrl_put(struct pinctrl *p)
1105{
Jingoo Hana72149e2013-02-26 11:34:07 +09001106 WARN_ON(devres_release(p->dev, devm_pinctrl_release,
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001107 devm_pinctrl_match, p));
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001108}
1109EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1110
Stephen Warren57291ce2012-03-23 10:29:46 -06001111int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
Doug Andersonc5272a22015-05-01 09:01:27 -07001112 bool dup)
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001113{
Stephen Warren1e2082b2012-03-02 13:05:48 -07001114 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001115 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001116
Masahiro Yamada7e9236f2015-05-28 21:52:59 +09001117 pr_debug("add %u pinctrl maps\n", num_maps);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001118
1119 /* First sanity check the new mapping */
1120 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001121 if (!maps[i].dev_name) {
1122 pr_err("failed to register map %s (%d): no device given\n",
1123 maps[i].name, i);
1124 return -EINVAL;
1125 }
1126
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001127 if (!maps[i].name) {
1128 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001129 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001130 return -EINVAL;
1131 }
1132
Stephen Warren1e2082b2012-03-02 13:05:48 -07001133 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1134 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001135 pr_err("failed to register map %s (%d): no pin control device given\n",
1136 maps[i].name, i);
1137 return -EINVAL;
1138 }
1139
Stephen Warren1e2082b2012-03-02 13:05:48 -07001140 switch (maps[i].type) {
1141 case PIN_MAP_TYPE_DUMMY_STATE:
1142 break;
1143 case PIN_MAP_TYPE_MUX_GROUP:
1144 ret = pinmux_validate_map(&maps[i], i);
1145 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001146 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001147 break;
1148 case PIN_MAP_TYPE_CONFIGS_PIN:
1149 case PIN_MAP_TYPE_CONFIGS_GROUP:
1150 ret = pinconf_validate_map(&maps[i], i);
1151 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001152 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001153 break;
1154 default:
1155 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001156 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -07001157 return -EINVAL;
1158 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001159 }
1160
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001161 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
1162 if (!maps_node) {
1163 pr_err("failed to alloc struct pinctrl_maps\n");
1164 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001165 }
1166
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001167 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -06001168 if (dup) {
1169 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
1170 GFP_KERNEL);
1171 if (!maps_node->maps) {
1172 pr_err("failed to duplicate mapping table\n");
1173 kfree(maps_node);
1174 return -ENOMEM;
1175 }
1176 } else {
1177 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001178 }
1179
Doug Andersonc5272a22015-05-01 09:01:27 -07001180 mutex_lock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001181 list_add_tail(&maps_node->node, &pinctrl_maps);
Doug Andersonc5272a22015-05-01 09:01:27 -07001182 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001183
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001184 return 0;
1185}
1186
Stephen Warren57291ce2012-03-23 10:29:46 -06001187/**
1188 * pinctrl_register_mappings() - register a set of pin controller mappings
1189 * @maps: the pincontrol mappings table to register. This should probably be
1190 * marked with __initdata so it can be discarded after boot. This
1191 * function will perform a shallow copy for the mapping entries.
1192 * @num_maps: the number of maps in the mapping table
1193 */
1194int pinctrl_register_mappings(struct pinctrl_map const *maps,
1195 unsigned num_maps)
1196{
Doug Andersonc5272a22015-05-01 09:01:27 -07001197 return pinctrl_register_map(maps, num_maps, true);
Stephen Warren57291ce2012-03-23 10:29:46 -06001198}
1199
1200void pinctrl_unregister_map(struct pinctrl_map const *map)
1201{
1202 struct pinctrl_maps *maps_node;
1203
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001204 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001205 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1206 if (maps_node->maps == map) {
1207 list_del(&maps_node->node);
Linus Walleijdb6c2c62013-07-23 01:43:20 +02001208 kfree(maps_node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001209 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001210 return;
1211 }
1212 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001213 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001214}
1215
Julien Delacou840a47b2012-12-10 14:47:33 +01001216/**
1217 * pinctrl_force_sleep() - turn a given controller device into sleep state
1218 * @pctldev: pin controller device
1219 */
1220int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1221{
1222 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
1223 return pinctrl_select_state(pctldev->p, pctldev->hog_sleep);
1224 return 0;
1225}
1226EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1227
1228/**
1229 * pinctrl_force_default() - turn a given controller device into default state
1230 * @pctldev: pin controller device
1231 */
1232int pinctrl_force_default(struct pinctrl_dev *pctldev)
1233{
1234 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
1235 return pinctrl_select_state(pctldev->p, pctldev->hog_default);
1236 return 0;
1237}
1238EXPORT_SYMBOL_GPL(pinctrl_force_default);
1239
Linus Walleij14005ee2013-06-05 15:30:33 +02001240#ifdef CONFIG_PM
1241
1242/**
Tony Lindgrenf3333492013-07-18 08:15:04 -07001243 * pinctrl_pm_select_state() - select pinctrl state for PM
1244 * @dev: device to select default state for
1245 * @state: state to set
1246 */
1247static int pinctrl_pm_select_state(struct device *dev,
1248 struct pinctrl_state *state)
1249{
1250 struct dev_pin_info *pins = dev->pins;
1251 int ret;
1252
1253 if (IS_ERR(state))
1254 return 0; /* No such state */
1255 ret = pinctrl_select_state(pins->p, state);
1256 if (ret)
1257 dev_err(dev, "failed to activate pinctrl state %s\n",
1258 state->name);
1259 return ret;
1260}
1261
1262/**
Linus Walleij14005ee2013-06-05 15:30:33 +02001263 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1264 * @dev: device to select default state for
1265 */
1266int pinctrl_pm_select_default_state(struct device *dev)
1267{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001268 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001269 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001270
1271 return pinctrl_pm_select_state(dev, dev->pins->default_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001272}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001273EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001274
1275/**
1276 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1277 * @dev: device to select sleep state for
1278 */
1279int pinctrl_pm_select_sleep_state(struct device *dev)
1280{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001281 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001282 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001283
1284 return pinctrl_pm_select_state(dev, dev->pins->sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001285}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001286EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001287
1288/**
1289 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1290 * @dev: device to select idle state for
1291 */
1292int pinctrl_pm_select_idle_state(struct device *dev)
1293{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001294 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001295 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001296
1297 return pinctrl_pm_select_state(dev, dev->pins->idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001298}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001299EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001300#endif
1301
Linus Walleij2744e8a2011-05-02 20:50:54 +02001302#ifdef CONFIG_DEBUG_FS
1303
1304static int pinctrl_pins_show(struct seq_file *s, void *what)
1305{
1306 struct pinctrl_dev *pctldev = s->private;
1307 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001308 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001309
1310 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001311
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001312 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001313
Chanho Park706e8522012-01-03 16:47:50 +09001314 /* The pin number can be retrived from the pin controller descriptor */
1315 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001316 struct pin_desc *desc;
1317
Chanho Park706e8522012-01-03 16:47:50 +09001318 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001319 desc = pin_desc_get(pctldev, pin);
1320 /* Pin space may be sparse */
1321 if (desc == NULL)
1322 continue;
1323
1324 seq_printf(s, "pin %d (%s) ", pin,
1325 desc->name ? desc->name : "unnamed");
1326
1327 /* Driver-specific info per pin */
1328 if (ops->pin_dbg_show)
1329 ops->pin_dbg_show(pctldev, s, pin);
1330
1331 seq_puts(s, "\n");
1332 }
1333
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001334 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001335
Linus Walleij2744e8a2011-05-02 20:50:54 +02001336 return 0;
1337}
1338
1339static int pinctrl_groups_show(struct seq_file *s, void *what)
1340{
1341 struct pinctrl_dev *pctldev = s->private;
1342 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301343 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001344
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001345 mutex_lock(&pctldev->mutex);
1346
Viresh Kumard1e90e92012-03-30 11:25:40 +05301347 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001348
Linus Walleij2744e8a2011-05-02 20:50:54 +02001349 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301350 while (selector < ngroups) {
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001351 const unsigned *pins = NULL;
1352 unsigned num_pins = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001353 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001354 const char *pname;
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001355 int ret = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001356 int i;
1357
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001358 if (ops->get_group_pins)
1359 ret = ops->get_group_pins(pctldev, selector,
1360 &pins, &num_pins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001361 if (ret)
1362 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1363 gname);
1364 else {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001365 seq_printf(s, "group: %s\n", gname);
1366 for (i = 0; i < num_pins; i++) {
1367 pname = pin_get_name(pctldev, pins[i]);
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001368 if (WARN_ON(!pname)) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001369 mutex_unlock(&pctldev->mutex);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001370 return -EINVAL;
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001371 }
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001372 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1373 }
1374 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001375 }
1376 selector++;
1377 }
1378
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001379 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001380
1381 return 0;
1382}
1383
1384static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1385{
1386 struct pinctrl_dev *pctldev = s->private;
1387 struct pinctrl_gpio_range *range = NULL;
1388
1389 seq_puts(s, "GPIO ranges handled:\n");
1390
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001391 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001392
Linus Walleij2744e8a2011-05-02 20:50:54 +02001393 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001394 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Christian Ruppertc8587ee2013-06-13 14:55:31 +02001395 if (range->pins) {
1396 int a;
1397 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
1398 range->id, range->name,
1399 range->base, (range->base + range->npins - 1));
1400 for (a = 0; a < range->npins - 1; a++)
1401 seq_printf(s, "%u, ", range->pins[a]);
1402 seq_printf(s, "%u}\n", range->pins[a]);
1403 }
1404 else
1405 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1406 range->id, range->name,
1407 range->base, (range->base + range->npins - 1),
1408 range->pin_base,
1409 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001410 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001411
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001412 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001413
1414 return 0;
1415}
1416
1417static int pinctrl_devices_show(struct seq_file *s, void *what)
1418{
1419 struct pinctrl_dev *pctldev;
1420
Linus Walleijae6b4d82011-10-19 18:14:33 +02001421 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001422
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001423 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001424
Linus Walleij2744e8a2011-05-02 20:50:54 +02001425 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1426 seq_printf(s, "%s ", pctldev->desc->name);
1427 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001428 seq_puts(s, "yes ");
1429 else
1430 seq_puts(s, "no ");
1431 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001432 seq_puts(s, "yes");
1433 else
1434 seq_puts(s, "no");
1435 seq_puts(s, "\n");
1436 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001437
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001438 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001439
1440 return 0;
1441}
1442
Stephen Warren1e2082b2012-03-02 13:05:48 -07001443static inline const char *map_type(enum pinctrl_map_type type)
1444{
1445 static const char * const names[] = {
1446 "INVALID",
1447 "DUMMY_STATE",
1448 "MUX_GROUP",
1449 "CONFIGS_PIN",
1450 "CONFIGS_GROUP",
1451 };
1452
1453 if (type >= ARRAY_SIZE(names))
1454 return "UNKNOWN";
1455
1456 return names[type];
1457}
1458
Stephen Warren3eedb432012-02-23 17:04:40 -07001459static int pinctrl_maps_show(struct seq_file *s, void *what)
1460{
1461 struct pinctrl_maps *maps_node;
1462 int i;
1463 struct pinctrl_map const *map;
1464
1465 seq_puts(s, "Pinctrl maps:\n");
1466
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001467 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001468 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001469 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1470 map->dev_name, map->name, map_type(map->type),
1471 map->type);
1472
1473 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1474 seq_printf(s, "controlling device %s\n",
1475 map->ctrl_dev_name);
1476
1477 switch (map->type) {
1478 case PIN_MAP_TYPE_MUX_GROUP:
1479 pinmux_show_map(s, map);
1480 break;
1481 case PIN_MAP_TYPE_CONFIGS_PIN:
1482 case PIN_MAP_TYPE_CONFIGS_GROUP:
1483 pinconf_show_map(s, map);
1484 break;
1485 default:
1486 break;
1487 }
1488
1489 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001490 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001491 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001492
1493 return 0;
1494}
1495
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001496static int pinctrl_show(struct seq_file *s, void *what)
1497{
1498 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001499 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001500 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001501
1502 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001503
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001504 mutex_lock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001505
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001506 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001507 seq_printf(s, "device: %s current state: %s\n",
1508 dev_name(p->dev),
1509 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001510
Stephen Warren6e5e9592012-03-02 13:05:47 -07001511 list_for_each_entry(state, &p->states, node) {
1512 seq_printf(s, " state: %s\n", state->name);
1513
1514 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001515 struct pinctrl_dev *pctldev = setting->pctldev;
1516
1517 seq_printf(s, " type: %s controller %s ",
1518 map_type(setting->type),
1519 pinctrl_dev_get_name(pctldev));
1520
1521 switch (setting->type) {
1522 case PIN_MAP_TYPE_MUX_GROUP:
1523 pinmux_show_setting(s, setting);
1524 break;
1525 case PIN_MAP_TYPE_CONFIGS_PIN:
1526 case PIN_MAP_TYPE_CONFIGS_GROUP:
1527 pinconf_show_setting(s, setting);
1528 break;
1529 default:
1530 break;
1531 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001532 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001533 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001534 }
1535
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001536 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001537
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001538 return 0;
1539}
1540
Linus Walleij2744e8a2011-05-02 20:50:54 +02001541static int pinctrl_pins_open(struct inode *inode, struct file *file)
1542{
1543 return single_open(file, pinctrl_pins_show, inode->i_private);
1544}
1545
1546static int pinctrl_groups_open(struct inode *inode, struct file *file)
1547{
1548 return single_open(file, pinctrl_groups_show, inode->i_private);
1549}
1550
1551static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1552{
1553 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1554}
1555
1556static int pinctrl_devices_open(struct inode *inode, struct file *file)
1557{
1558 return single_open(file, pinctrl_devices_show, NULL);
1559}
1560
Stephen Warren3eedb432012-02-23 17:04:40 -07001561static int pinctrl_maps_open(struct inode *inode, struct file *file)
1562{
1563 return single_open(file, pinctrl_maps_show, NULL);
1564}
1565
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001566static int pinctrl_open(struct inode *inode, struct file *file)
1567{
1568 return single_open(file, pinctrl_show, NULL);
1569}
1570
Linus Walleij2744e8a2011-05-02 20:50:54 +02001571static const struct file_operations pinctrl_pins_ops = {
1572 .open = pinctrl_pins_open,
1573 .read = seq_read,
1574 .llseek = seq_lseek,
1575 .release = single_release,
1576};
1577
1578static const struct file_operations pinctrl_groups_ops = {
1579 .open = pinctrl_groups_open,
1580 .read = seq_read,
1581 .llseek = seq_lseek,
1582 .release = single_release,
1583};
1584
1585static const struct file_operations pinctrl_gpioranges_ops = {
1586 .open = pinctrl_gpioranges_open,
1587 .read = seq_read,
1588 .llseek = seq_lseek,
1589 .release = single_release,
1590};
1591
1592static const struct file_operations pinctrl_devices_ops = {
1593 .open = pinctrl_devices_open,
1594 .read = seq_read,
1595 .llseek = seq_lseek,
1596 .release = single_release,
1597};
1598
Stephen Warren3eedb432012-02-23 17:04:40 -07001599static const struct file_operations pinctrl_maps_ops = {
1600 .open = pinctrl_maps_open,
1601 .read = seq_read,
1602 .llseek = seq_lseek,
1603 .release = single_release,
1604};
1605
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001606static const struct file_operations pinctrl_ops = {
1607 .open = pinctrl_open,
1608 .read = seq_read,
1609 .llseek = seq_lseek,
1610 .release = single_release,
1611};
1612
Linus Walleij2744e8a2011-05-02 20:50:54 +02001613static struct dentry *debugfs_root;
1614
1615static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1616{
Tony Lindgren02157162012-01-20 08:17:22 -08001617 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001618
Stephen Warren51cd24e2011-12-09 16:59:05 -07001619 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001620 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001621 pctldev->device_root = device_root;
1622
Linus Walleij2744e8a2011-05-02 20:50:54 +02001623 if (IS_ERR(device_root) || !device_root) {
1624 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001625 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001626 return;
1627 }
1628 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1629 device_root, pctldev, &pinctrl_pins_ops);
1630 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1631 device_root, pctldev, &pinctrl_groups_ops);
1632 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1633 device_root, pctldev, &pinctrl_gpioranges_ops);
Florian Vaussarde7f2a442014-02-05 07:51:22 +01001634 if (pctldev->desc->pmxops)
1635 pinmux_init_device_debugfs(device_root, pctldev);
1636 if (pctldev->desc->confops)
1637 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001638}
1639
Tony Lindgren02157162012-01-20 08:17:22 -08001640static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1641{
1642 debugfs_remove_recursive(pctldev->device_root);
1643}
1644
Linus Walleij2744e8a2011-05-02 20:50:54 +02001645static void pinctrl_init_debugfs(void)
1646{
1647 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1648 if (IS_ERR(debugfs_root) || !debugfs_root) {
1649 pr_warn("failed to create debugfs directory\n");
1650 debugfs_root = NULL;
1651 return;
1652 }
1653
1654 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1655 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001656 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1657 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001658 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1659 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001660}
1661
1662#else /* CONFIG_DEBUG_FS */
1663
1664static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1665{
1666}
1667
1668static void pinctrl_init_debugfs(void)
1669{
1670}
1671
Tony Lindgren02157162012-01-20 08:17:22 -08001672static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1673{
1674}
1675
Linus Walleij2744e8a2011-05-02 20:50:54 +02001676#endif
1677
Stephen Warrend26bc492012-03-16 14:54:25 -06001678static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1679{
1680 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1681
1682 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301683 !ops->get_groups_count ||
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001684 !ops->get_group_name)
Stephen Warrend26bc492012-03-16 14:54:25 -06001685 return -EINVAL;
1686
Stephen Warren57291ce2012-03-23 10:29:46 -06001687 if (ops->dt_node_to_map && !ops->dt_free_map)
1688 return -EINVAL;
1689
Stephen Warrend26bc492012-03-16 14:54:25 -06001690 return 0;
1691}
1692
Linus Walleij2744e8a2011-05-02 20:50:54 +02001693/**
1694 * pinctrl_register() - register a pin controller device
1695 * @pctldesc: descriptor for this pin controller
1696 * @dev: parent device for this pin controller
1697 * @driver_data: private pin controller data for this pin controller
1698 */
1699struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1700 struct device *dev, void *driver_data)
1701{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001702 struct pinctrl_dev *pctldev;
1703 int ret;
1704
Devendra Nagada9aecb2012-06-16 23:43:16 +05301705 if (!pctldesc)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001706 return ERR_PTR(-EINVAL);
Devendra Nagada9aecb2012-06-16 23:43:16 +05301707 if (!pctldesc->name)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001708 return ERR_PTR(-EINVAL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001709
Stephen Warren02f5b982012-02-22 14:26:00 -07001710 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001711 if (pctldev == NULL) {
1712 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001713 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001714 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001715
1716 /* Initialize pin control device struct */
1717 pctldev->owner = pctldesc->owner;
1718 pctldev->desc = pctldesc;
1719 pctldev->driver_data = driver_data;
1720 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001721 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001722 pctldev->dev = dev;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001723 mutex_init(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001724
Stephen Warrend26bc492012-03-16 14:54:25 -06001725 /* check core ops for sanity */
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001726 ret = pinctrl_check_ops(pctldev);
1727 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001728 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06001729 goto out_err;
1730 }
1731
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001732 /* If we're implementing pinmuxing, check the ops for sanity */
1733 if (pctldesc->pmxops) {
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001734 ret = pinmux_check_ops(pctldev);
1735 if (ret)
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001736 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001737 }
1738
1739 /* If we're implementing pinconfig, check the ops for sanity */
1740 if (pctldesc->confops) {
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001741 ret = pinconf_check_ops(pctldev);
1742 if (ret)
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001743 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001744 }
1745
Linus Walleij2744e8a2011-05-02 20:50:54 +02001746 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02001747 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001748 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1749 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001750 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001751 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1752 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001753 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001754 }
1755
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001756 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -07001757 list_add_tail(&pctldev->node, &pinctrldev_list);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001758 mutex_unlock(&pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001759
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001760 pctldev->p = pinctrl_get(pctldev->dev);
1761
Stephen Warren6e5e9592012-03-02 13:05:47 -07001762 if (!IS_ERR(pctldev->p)) {
Julien Delacou840a47b2012-12-10 14:47:33 +01001763 pctldev->hog_default =
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001764 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
Julien Delacou840a47b2012-12-10 14:47:33 +01001765 if (IS_ERR(pctldev->hog_default)) {
John Crispinad6e1102012-04-26 16:47:11 +02001766 dev_dbg(dev, "failed to lookup the default state\n");
1767 } else {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001768 if (pinctrl_select_state(pctldev->p,
Julien Delacou840a47b2012-12-10 14:47:33 +01001769 pctldev->hog_default))
John Crispinad6e1102012-04-26 16:47:11 +02001770 dev_err(dev,
1771 "failed to select default state\n");
John Crispinad6e1102012-04-26 16:47:11 +02001772 }
Julien Delacou840a47b2012-12-10 14:47:33 +01001773
1774 pctldev->hog_sleep =
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001775 pinctrl_lookup_state(pctldev->p,
Julien Delacou840a47b2012-12-10 14:47:33 +01001776 PINCTRL_STATE_SLEEP);
1777 if (IS_ERR(pctldev->hog_sleep))
1778 dev_dbg(dev, "failed to lookup the sleep state\n");
Stephen Warren6e5e9592012-03-02 13:05:47 -07001779 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001780
Stephen Warren2304b472012-02-22 14:26:01 -07001781 pinctrl_init_device_debugfs(pctldev);
1782
Linus Walleij2744e8a2011-05-02 20:50:54 +02001783 return pctldev;
1784
Stephen Warren51cd24e2011-12-09 16:59:05 -07001785out_err:
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001786 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001787 kfree(pctldev);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001788 return ERR_PTR(ret);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001789}
1790EXPORT_SYMBOL_GPL(pinctrl_register);
1791
1792/**
1793 * pinctrl_unregister() - unregister pinmux
1794 * @pctldev: pin controller to unregister
1795 *
1796 * Called by pinmux drivers to unregister a pinmux.
1797 */
1798void pinctrl_unregister(struct pinctrl_dev *pctldev)
1799{
Dong Aisheng5d589b02012-05-23 21:22:40 +08001800 struct pinctrl_gpio_range *range, *n;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001801 if (pctldev == NULL)
1802 return;
1803
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001804 mutex_lock(&pctldev->mutex);
Tony Lindgren02157162012-01-20 08:17:22 -08001805 pinctrl_remove_device_debugfs(pctldev);
Jim Lindb93fac2015-01-08 20:25:05 +08001806 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001807
Stephen Warren6e5e9592012-03-02 13:05:47 -07001808 if (!IS_ERR(pctldev->p))
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001809 pinctrl_put(pctldev->p);
Stephen Warren57b676f2012-03-02 13:05:44 -07001810
Jim Lindb93fac2015-01-08 20:25:05 +08001811 mutex_lock(&pinctrldev_list_mutex);
1812 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001813 /* TODO: check that no pinmuxes are still active? */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001814 list_del(&pctldev->node);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001815 /* Destroy descriptor tree */
1816 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1817 pctldev->desc->npins);
Dong Aisheng5d589b02012-05-23 21:22:40 +08001818 /* remove gpio ranges map */
1819 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1820 list_del(&range->node);
1821
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001822 mutex_unlock(&pctldev->mutex);
1823 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001824 kfree(pctldev);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001825 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001826}
1827EXPORT_SYMBOL_GPL(pinctrl_unregister);
1828
1829static int __init pinctrl_init(void)
1830{
1831 pr_info("initialized pinctrl subsystem\n");
1832 pinctrl_init_debugfs();
1833 return 0;
1834}
1835
1836/* init early since many drivers really need to initialized pinmux early */
1837core_initcall(pinctrl_init);