blob: 8b8f3a04c35371f439697ff6046ad6ee740e8d01 [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) {
234 pr_err("pin %d already registered on %s\n", number,
235 pctldev->desc->name);
236 return -EINVAL;
237 }
238
239 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700240 if (pindesc == NULL) {
241 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200242 return -ENOMEM;
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700243 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200244
Linus Walleij2744e8a2011-05-02 20:50:54 +0200245 /* Set owner */
246 pindesc->pctldev = pctldev;
247
Stephen Warren9af1e442011-10-19 16:19:27 -0600248 /* Copy basic pin info */
Linus Walleij8dc6ae42012-02-01 18:11:40 +0100249 if (name) {
Linus Walleijca53c5f2011-12-14 20:33:37 +0100250 pindesc->name = name;
251 } else {
252 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
Sachin Kamateb26cc92012-09-25 12:41:40 +0530253 if (pindesc->name == NULL) {
254 kfree(pindesc);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100255 return -ENOMEM;
Sachin Kamateb26cc92012-09-25 12:41:40 +0530256 }
Linus Walleijca53c5f2011-12-14 20:33:37 +0100257 pindesc->dynamic_name = true;
258 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200259
Linus Walleij2744e8a2011-05-02 20:50:54 +0200260 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200261 pr_debug("registered pin %d (%s) on %s\n",
Linus Walleijca53c5f2011-12-14 20:33:37 +0100262 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++) {
274 ret = pinctrl_register_one_pin(pctldev,
275 pins[i].number, pins[i].name);
276 if (ret)
277 return ret;
278 }
279
280 return 0;
281}
282
283/**
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200284 * gpio_to_pin() - GPIO range GPIO number to pin number translation
285 * @range: GPIO range used for the translation
286 * @gpio: gpio pin to translate to a pin number
287 *
288 * Finds the pin number for a given GPIO using the specified GPIO range
289 * as a base for translation. The distinction between linear GPIO ranges
290 * and pin list based GPIO ranges is managed correctly by this function.
291 *
292 * This function assumes the gpio is part of the specified GPIO range, use
293 * only after making sure this is the case (e.g. by calling it on the
294 * result of successful pinctrl_get_device_gpio_range calls)!
295 */
296static inline int gpio_to_pin(struct pinctrl_gpio_range *range,
297 unsigned int gpio)
298{
299 unsigned int offset = gpio - range->base;
300 if (range->pins)
301 return range->pins[offset];
302 else
303 return range->pin_base + offset;
304}
305
306/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200307 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
308 * @pctldev: pin controller device to check
309 * @gpio: gpio pin to check taken from the global GPIO pin space
310 *
311 * Tries to match a GPIO pin number to the ranges handled by a certain pin
312 * controller, return the range or NULL
313 */
314static struct pinctrl_gpio_range *
315pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
316{
317 struct pinctrl_gpio_range *range = NULL;
318
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200319 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200320 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200321 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
322 /* Check if we're in the valid range */
323 if (gpio >= range->base &&
324 gpio < range->base + range->npins) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200325 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200326 return range;
327 }
328 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200329 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200330 return NULL;
331}
332
333/**
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800334 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
335 * the same GPIO chip are in range
336 * @gpio: gpio pin to check taken from the global GPIO pin space
337 *
338 * This function is complement of pinctrl_match_gpio_range(). If the return
339 * value of pinctrl_match_gpio_range() is NULL, this function could be used
340 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
341 * of the same GPIO chip don't have back-end pinctrl interface.
342 * If the return value is true, it means that pinctrl device is ready & the
343 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
344 * is false, it means that pinctrl device may not be ready.
345 */
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800346#ifdef CONFIG_GPIOLIB
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800347static bool pinctrl_ready_for_gpio_range(unsigned gpio)
348{
349 struct pinctrl_dev *pctldev;
350 struct pinctrl_gpio_range *range = NULL;
351 struct gpio_chip *chip = gpio_to_chip(gpio);
352
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200353 mutex_lock(&pinctrldev_list_mutex);
354
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800355 /* Loop over the pin controllers */
356 list_for_each_entry(pctldev, &pinctrldev_list, node) {
357 /* Loop over the ranges */
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800358 mutex_lock(&pctldev->mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800359 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
360 /* Check if any gpio range overlapped with gpio chip */
361 if (range->base + range->npins - 1 < chip->base ||
362 range->base > chip->base + chip->ngpio - 1)
363 continue;
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800364 mutex_unlock(&pctldev->mutex);
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200365 mutex_unlock(&pinctrldev_list_mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800366 return true;
367 }
Axel Lin5ffbe2e2013-08-18 20:40:29 +0800368 mutex_unlock(&pctldev->mutex);
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800369 }
Linus Walleij44d5f7b2013-05-16 09:17:04 +0200370
371 mutex_unlock(&pinctrldev_list_mutex);
372
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800373 return false;
374}
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800375#else
376static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
377#endif
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800378
379/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200380 * pinctrl_get_device_gpio_range() - find device for GPIO range
381 * @gpio: the pin to locate the pin controller for
382 * @outdev: the pin control device if found
383 * @outrange: the GPIO range if found
384 *
385 * Find the pin controller handling a certain GPIO pin from the pinspace of
386 * the GPIO subsystem, return the device and the matching GPIO range. Returns
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800387 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
388 * may still have not been registered.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200389 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700390static int pinctrl_get_device_gpio_range(unsigned gpio,
391 struct pinctrl_dev **outdev,
392 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200393{
394 struct pinctrl_dev *pctldev = NULL;
395
Axel Linf0059022013-08-18 20:34:22 +0800396 mutex_lock(&pinctrldev_list_mutex);
397
Linus Walleij2744e8a2011-05-02 20:50:54 +0200398 /* Loop over the pin controllers */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200399 list_for_each_entry(pctldev, &pinctrldev_list, node) {
400 struct pinctrl_gpio_range *range;
401
402 range = pinctrl_match_gpio_range(pctldev, gpio);
403 if (range != NULL) {
404 *outdev = pctldev;
405 *outrange = range;
Axel Linf0059022013-08-18 20:34:22 +0800406 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200407 return 0;
408 }
409 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200410
Axel Linf0059022013-08-18 20:34:22 +0800411 mutex_unlock(&pinctrldev_list_mutex);
412
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800413 return -EPROBE_DEFER;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200414}
415
416/**
417 * pinctrl_add_gpio_range() - register a GPIO range for a controller
418 * @pctldev: pin controller device to add the range to
419 * @range: the GPIO range to add
420 *
421 * This adds a range of GPIOs to be handled by a certain pin controller. Call
422 * this to register handled ranges after registering your pin controller.
423 */
424void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
425 struct pinctrl_gpio_range *range)
426{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200427 mutex_lock(&pctldev->mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700428 list_add_tail(&range->node, &pctldev->gpio_ranges);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200429 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200430}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700431EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200432
Dong Aisheng3e5e00b2012-05-23 21:22:41 +0800433void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
434 struct pinctrl_gpio_range *ranges,
435 unsigned nranges)
436{
437 int i;
438
439 for (i = 0; i < nranges; i++)
440 pinctrl_add_gpio_range(pctldev, &ranges[i]);
441}
442EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
443
Linus Walleij192c3692012-11-20 14:03:37 +0100444struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530445 struct pinctrl_gpio_range *range)
446{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200447 struct pinctrl_dev *pctldev;
448
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200449 pctldev = get_pinctrl_dev_from_devname(devname);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530450
Linus Walleijdfa97512012-11-20 14:54:18 +0100451 /*
452 * If we can't find this device, let's assume that is because
453 * it has not probed yet, so the driver trying to register this
454 * range need to defer probing.
455 */
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200456 if (!pctldev) {
Linus Walleijdfa97512012-11-20 14:54:18 +0100457 return ERR_PTR(-EPROBE_DEFER);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200458 }
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530459 pinctrl_add_gpio_range(pctldev, range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200460
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530461 return pctldev;
462}
Linus Walleij192c3692012-11-20 14:03:37 +0100463EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530464
Christian Ruppert586a87e2013-10-15 15:37:54 +0200465int pinctrl_get_group_pins(struct pinctrl_dev *pctldev, const char *pin_group,
466 const unsigned **pins, unsigned *num_pins)
467{
468 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
469 int gs;
470
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +0200471 if (!pctlops->get_group_pins)
472 return -EINVAL;
473
Christian Ruppert586a87e2013-10-15 15:37:54 +0200474 gs = pinctrl_get_group_selector(pctldev, pin_group);
475 if (gs < 0)
476 return gs;
477
478 return pctlops->get_group_pins(pctldev, gs, pins, num_pins);
479}
480EXPORT_SYMBOL_GPL(pinctrl_get_group_pins);
481
Linus Walleij2744e8a2011-05-02 20:50:54 +0200482/**
Linus Walleij9afbefb2012-11-20 14:25:07 +0100483 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
484 * @pctldev: the pin controller device to look in
485 * @pin: a controller-local number to find the range for
486 */
487struct pinctrl_gpio_range *
488pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
489 unsigned int pin)
490{
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800491 struct pinctrl_gpio_range *range;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100492
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200493 mutex_lock(&pctldev->mutex);
Linus Walleij9afbefb2012-11-20 14:25:07 +0100494 /* Loop over the ranges */
495 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
496 /* Check if we're in the valid range */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200497 if (range->pins) {
498 int a;
499 for (a = 0; a < range->npins; a++) {
500 if (range->pins[a] == pin)
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800501 goto out;
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200502 }
503 } else if (pin >= range->pin_base &&
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800504 pin < range->pin_base + range->npins)
505 goto out;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100506 }
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800507 range = NULL;
508out:
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200509 mutex_unlock(&pctldev->mutex);
Wei Yongjunc8f50e82013-06-18 12:24:58 +0800510 return range;
Linus Walleij9afbefb2012-11-20 14:25:07 +0100511}
512EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
513
514/**
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530515 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
516 * @pctldev: pin controller device to remove the range from
517 * @range: the GPIO range to remove
518 */
519void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
520 struct pinctrl_gpio_range *range)
521{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200522 mutex_lock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530523 list_del(&range->node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200524 mutex_unlock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530525}
526EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
527
528/**
Linus Walleij7afde8b2011-10-19 17:07:16 +0200529 * pinctrl_get_group_selector() - returns the group selector for a group
530 * @pctldev: the pin controller handling the group
531 * @pin_group: the pin group to look up
532 */
533int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
534 const char *pin_group)
535{
536 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530537 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200538 unsigned group_selector = 0;
539
Viresh Kumard1e90e92012-03-30 11:25:40 +0530540 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200541 const char *gname = pctlops->get_group_name(pctldev,
542 group_selector);
543 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700544 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200545 "found group selector %u for %s\n",
546 group_selector,
547 pin_group);
548 return group_selector;
549 }
550
551 group_selector++;
552 }
553
Stephen Warren51cd24e2011-12-09 16:59:05 -0700554 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200555 pin_group);
556
557 return -EINVAL;
558}
559
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100560/**
Geert Uytterhoevenb217e432015-05-04 19:46:57 +0200561 * pinctrl_request_gpio() - request a single pin to be used as GPIO
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100562 * @gpio: the GPIO pin number from the GPIO subsystem number space
563 *
564 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
565 * as part of their gpio_request() semantics, platforms and individual drivers
566 * shall *NOT* request GPIO pins to be muxed in.
567 */
568int pinctrl_request_gpio(unsigned gpio)
569{
570 struct pinctrl_dev *pctldev;
571 struct pinctrl_gpio_range *range;
572 int ret;
573 int pin;
574
575 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700576 if (ret) {
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800577 if (pinctrl_ready_for_gpio_range(gpio))
578 ret = 0;
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800579 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700580 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100581
Axel Lin9b77ace2013-08-19 10:07:46 +0800582 mutex_lock(&pctldev->mutex);
583
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100584 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200585 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100586
Stephen Warren57b676f2012-03-02 13:05:44 -0700587 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
588
Axel Lin9b77ace2013-08-19 10:07:46 +0800589 mutex_unlock(&pctldev->mutex);
590
Stephen Warren57b676f2012-03-02 13:05:44 -0700591 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100592}
593EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
594
595/**
596 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
597 * @gpio: the GPIO pin number from the GPIO subsystem number space
598 *
599 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
600 * as part of their gpio_free() semantics, platforms and individual drivers
601 * shall *NOT* request GPIO pins to be muxed out.
602 */
603void pinctrl_free_gpio(unsigned gpio)
604{
605 struct pinctrl_dev *pctldev;
606 struct pinctrl_gpio_range *range;
607 int ret;
608 int pin;
609
610 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700611 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100612 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700613 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200614 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100615
616 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200617 pin = gpio_to_pin(range, gpio);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100618
Stephen Warren57b676f2012-03-02 13:05:44 -0700619 pinmux_free_gpio(pctldev, pin, range);
620
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200621 mutex_unlock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100622}
623EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
624
625static int pinctrl_gpio_direction(unsigned gpio, bool input)
626{
627 struct pinctrl_dev *pctldev;
628 struct pinctrl_gpio_range *range;
629 int ret;
630 int pin;
631
632 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200633 if (ret) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100634 return ret;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200635 }
636
637 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100638
639 /* Convert to the pin controllers number space */
Christian Ruppertc8587ee2013-06-13 14:55:31 +0200640 pin = gpio_to_pin(range, gpio);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200641 ret = pinmux_gpio_direction(pctldev, range, pin, input);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100642
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200643 mutex_unlock(&pctldev->mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200644
645 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100646}
647
648/**
649 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
650 * @gpio: the GPIO pin number from the GPIO subsystem number space
651 *
652 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
653 * as part of their gpio_direction_input() semantics, platforms and individual
654 * drivers shall *NOT* touch pin control GPIO calls.
655 */
656int pinctrl_gpio_direction_input(unsigned gpio)
657{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200658 return pinctrl_gpio_direction(gpio, true);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100659}
660EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
661
662/**
663 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
664 * @gpio: the GPIO pin number from the GPIO subsystem number space
665 *
666 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
667 * as part of their gpio_direction_output() semantics, platforms and individual
668 * drivers shall *NOT* touch pin control GPIO calls.
669 */
670int pinctrl_gpio_direction_output(unsigned gpio)
671{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200672 return pinctrl_gpio_direction(gpio, false);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100673}
674EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
675
Stephen Warren6e5e9592012-03-02 13:05:47 -0700676static struct pinctrl_state *find_state(struct pinctrl *p,
677 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100678{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700679 struct pinctrl_state *state;
680
681 list_for_each_entry(state, &p->states, node)
682 if (!strcmp(state->name, name))
683 return state;
684
685 return NULL;
686}
687
688static struct pinctrl_state *create_state(struct pinctrl *p,
689 const char *name)
690{
691 struct pinctrl_state *state;
692
693 state = kzalloc(sizeof(*state), GFP_KERNEL);
694 if (state == NULL) {
695 dev_err(p->dev,
696 "failed to alloc struct pinctrl_state\n");
697 return ERR_PTR(-ENOMEM);
698 }
699
700 state->name = name;
701 INIT_LIST_HEAD(&state->settings);
702
703 list_add_tail(&state->node, &p->states);
704
705 return state;
706}
707
708static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
709{
710 struct pinctrl_state *state;
711 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700712 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700713
714 state = find_state(p, map->name);
715 if (!state)
716 state = create_state(p, map->name);
717 if (IS_ERR(state))
718 return PTR_ERR(state);
719
Stephen Warren1e2082b2012-03-02 13:05:48 -0700720 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
721 return 0;
722
Stephen Warren6e5e9592012-03-02 13:05:47 -0700723 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
724 if (setting == NULL) {
725 dev_err(p->dev,
726 "failed to alloc struct pinctrl_setting\n");
727 return -ENOMEM;
728 }
729
Stephen Warren1e2082b2012-03-02 13:05:48 -0700730 setting->type = map->type;
731
Stephen Warren6e5e9592012-03-02 13:05:47 -0700732 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
733 if (setting->pctldev == NULL) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700734 kfree(setting);
Linus Walleij89216492012-12-11 14:14:32 +0100735 /* Do not defer probing of hogs (circular loop) */
736 if (!strcmp(map->ctrl_dev_name, map->dev_name))
737 return -ENODEV;
Linus Walleijc05127c2012-04-10 10:00:38 +0200738 /*
739 * OK let us guess that the driver is not there yet, and
740 * let's defer obtaining this pinctrl handle to later...
741 */
Linus Walleij89216492012-12-11 14:14:32 +0100742 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
743 map->ctrl_dev_name);
Linus Walleijc05127c2012-04-10 10:00:38 +0200744 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700745 }
746
Linus Walleij1a789582012-10-17 20:51:54 +0200747 setting->dev_name = map->dev_name;
748
Stephen Warren1e2082b2012-03-02 13:05:48 -0700749 switch (map->type) {
750 case PIN_MAP_TYPE_MUX_GROUP:
751 ret = pinmux_map_to_setting(map, setting);
752 break;
753 case PIN_MAP_TYPE_CONFIGS_PIN:
754 case PIN_MAP_TYPE_CONFIGS_GROUP:
755 ret = pinconf_map_to_setting(map, setting);
756 break;
757 default:
758 ret = -EINVAL;
759 break;
760 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700761 if (ret < 0) {
762 kfree(setting);
763 return ret;
764 }
765
766 list_add_tail(&setting->node, &state->settings);
767
768 return 0;
769}
770
771static struct pinctrl *find_pinctrl(struct device *dev)
772{
773 struct pinctrl *p;
774
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200775 mutex_lock(&pinctrl_list_mutex);
Stephen Warren1e2082b2012-03-02 13:05:48 -0700776 list_for_each_entry(p, &pinctrl_list, node)
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200777 if (p->dev == dev) {
778 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700779 return p;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200780 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700781
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200782 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700783 return NULL;
784}
785
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200786static void pinctrl_free(struct pinctrl *p, bool inlist);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700787
788static struct pinctrl *create_pinctrl(struct device *dev)
789{
790 struct pinctrl *p;
791 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700792 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100793 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700794 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700795 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100796
797 /*
798 * create the state cookie holder struct pinctrl for each
799 * mapping, this is what consumers will get when requesting
800 * a pin control handle with pinctrl_get()
801 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700802 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700803 if (p == NULL) {
804 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100805 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700806 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700807 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700808 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -0600809 INIT_LIST_HEAD(&p->dt_maps);
810
811 ret = pinctrl_dt_to_map(p);
812 if (ret < 0) {
813 kfree(p);
814 return ERR_PTR(ret);
815 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700816
817 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100818
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200819 mutex_lock(&pinctrl_maps_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100820 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700821 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -0700822 /* Map must be for this device */
823 if (strcmp(map->dev_name, devname))
824 continue;
825
Stephen Warren6e5e9592012-03-02 13:05:47 -0700826 ret = add_setting(p, map);
Linus Walleij89216492012-12-11 14:14:32 +0100827 /*
828 * At this point the adding of a setting may:
829 *
830 * - Defer, if the pinctrl device is not yet available
831 * - Fail, if the pinctrl device is not yet available,
832 * AND the setting is a hog. We cannot defer that, since
833 * the hog will kick in immediately after the device
834 * is registered.
835 *
836 * If the error returned was not -EPROBE_DEFER then we
837 * accumulate the errors to see if we end up with
838 * an -EPROBE_DEFER later, as that is the worst case.
839 */
840 if (ret == -EPROBE_DEFER) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200841 pinctrl_free(p, false);
842 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700843 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100844 }
845 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200846 mutex_unlock(&pinctrl_maps_mutex);
847
Linus Walleij89216492012-12-11 14:14:32 +0100848 if (ret < 0) {
849 /* If some other error than deferral occured, return here */
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200850 pinctrl_free(p, false);
Linus Walleij89216492012-12-11 14:14:32 +0100851 return ERR_PTR(ret);
852 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100853
Linus Walleijab780292013-01-22 10:56:14 -0700854 kref_init(&p->users);
855
Linus Walleijb0666ba2012-12-11 13:12:35 +0100856 /* Add the pinctrl handle to the global list */
Stanislaw Gruszka7b320cb2014-02-04 09:07:09 +0100857 mutex_lock(&pinctrl_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700858 list_add_tail(&p->node, &pinctrl_list);
Stanislaw Gruszka7b320cb2014-02-04 09:07:09 +0100859 mutex_unlock(&pinctrl_list_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100860
861 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700862}
Stephen Warren7ecdb162012-03-02 13:05:45 -0700863
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200864/**
865 * pinctrl_get() - retrieves the pinctrl handle for a device
866 * @dev: the device to obtain the handle for
867 */
868struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700869{
870 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700871
Stephen Warren6e5e9592012-03-02 13:05:47 -0700872 if (WARN_ON(!dev))
873 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700874
Linus Walleijab780292013-01-22 10:56:14 -0700875 /*
876 * See if somebody else (such as the device core) has already
877 * obtained a handle to the pinctrl for this device. In that case,
878 * return another pointer to it.
879 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700880 p = find_pinctrl(dev);
Linus Walleijab780292013-01-22 10:56:14 -0700881 if (p != NULL) {
882 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
883 kref_get(&p->users);
884 return p;
885 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700886
Richard Genoudd599bfb2012-08-10 16:53:49 +0200887 return create_pinctrl(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100888}
889EXPORT_SYMBOL_GPL(pinctrl_get);
890
Richard Genoudd3cee8302013-03-25 15:47:21 +0100891static void pinctrl_free_setting(bool disable_setting,
892 struct pinctrl_setting *setting)
893{
894 switch (setting->type) {
895 case PIN_MAP_TYPE_MUX_GROUP:
896 if (disable_setting)
897 pinmux_disable_setting(setting);
898 pinmux_free_setting(setting);
899 break;
900 case PIN_MAP_TYPE_CONFIGS_PIN:
901 case PIN_MAP_TYPE_CONFIGS_GROUP:
902 pinconf_free_setting(setting);
903 break;
904 default:
905 break;
906 }
907}
908
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200909static void pinctrl_free(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -0700910{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700911 struct pinctrl_state *state, *n1;
912 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700913
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200914 mutex_lock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700915 list_for_each_entry_safe(state, n1, &p->states, node) {
916 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Richard Genoudd3cee8302013-03-25 15:47:21 +0100917 pinctrl_free_setting(state == p->state, setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700918 list_del(&setting->node);
919 kfree(setting);
920 }
921 list_del(&state->node);
922 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700923 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700924
Stephen Warren57291ce2012-03-23 10:29:46 -0600925 pinctrl_dt_free_maps(p);
926
Stephen Warren6e5e9592012-03-02 13:05:47 -0700927 if (inlist)
928 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700929 kfree(p);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200930 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700931}
932
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100933/**
Linus Walleijab780292013-01-22 10:56:14 -0700934 * pinctrl_release() - release the pinctrl handle
935 * @kref: the kref in the pinctrl being released
936 */
Sachin Kamat2917e832013-01-24 15:01:00 +0530937static void pinctrl_release(struct kref *kref)
Linus Walleijab780292013-01-22 10:56:14 -0700938{
939 struct pinctrl *p = container_of(kref, struct pinctrl, users);
940
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200941 pinctrl_free(p, true);
Linus Walleijab780292013-01-22 10:56:14 -0700942}
943
944/**
945 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
Stephen Warren6e5e9592012-03-02 13:05:47 -0700946 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100947 */
948void pinctrl_put(struct pinctrl *p)
949{
Linus Walleijab780292013-01-22 10:56:14 -0700950 kref_put(&p->users, pinctrl_release);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100951}
952EXPORT_SYMBOL_GPL(pinctrl_put);
953
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200954/**
955 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
956 * @p: the pinctrl handle to retrieve the state from
957 * @name: the state name to retrieve
958 */
959struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
960 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -0700961{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700962 struct pinctrl_state *state;
963
964 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800965 if (!state) {
966 if (pinctrl_dummy_state) {
967 /* create dummy state */
968 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
969 name);
970 state = create_state(p, name);
Richard Genoudd599bfb2012-08-10 16:53:49 +0200971 } else
972 state = ERR_PTR(-ENODEV);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800973 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700974
975 return state;
976}
Stephen Warren6e5e9592012-03-02 13:05:47 -0700977EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
978
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200979/**
980 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
981 * @p: the pinctrl handle for the device that requests configuration
982 * @state: the state handle to select/activate/program
983 */
984int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700985{
986 struct pinctrl_setting *setting, *setting2;
Richard Genoud50cf7c82013-03-25 15:47:23 +0100987 struct pinctrl_state *old_state = p->state;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700988 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700989
Stephen Warren6e5e9592012-03-02 13:05:47 -0700990 if (p->state == state)
991 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700992
Stephen Warren6e5e9592012-03-02 13:05:47 -0700993 if (p->state) {
994 /*
Fan Wu2243a872014-06-09 09:37:56 +0800995 * For each pinmux setting in the old state, forget SW's record
996 * of mux owner for that pingroup. Any pingroups which are
997 * still owned by the new state will be re-acquired by the call
998 * to pinmux_enable_setting() in the loop below.
Stephen Warren6e5e9592012-03-02 13:05:47 -0700999 */
1000 list_for_each_entry(setting, &p->state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001001 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
1002 continue;
Fan Wu2243a872014-06-09 09:37:56 +08001003 pinmux_disable_setting(setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001004 }
1005 }
1006
Richard Genoud3102a762013-03-25 15:47:22 +01001007 p->state = NULL;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001008
1009 /* Apply all the settings for the new state */
1010 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001011 switch (setting->type) {
1012 case PIN_MAP_TYPE_MUX_GROUP:
1013 ret = pinmux_enable_setting(setting);
1014 break;
1015 case PIN_MAP_TYPE_CONFIGS_PIN:
1016 case PIN_MAP_TYPE_CONFIGS_GROUP:
1017 ret = pinconf_apply_setting(setting);
1018 break;
1019 default:
1020 ret = -EINVAL;
1021 break;
1022 }
Richard Genoud3102a762013-03-25 15:47:22 +01001023
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001024 if (ret < 0) {
Richard Genoud3102a762013-03-25 15:47:22 +01001025 goto unapply_new_state;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001026 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001027 }
1028
Richard Genoud3102a762013-03-25 15:47:22 +01001029 p->state = state;
1030
Stephen Warren7ecdb162012-03-02 13:05:45 -07001031 return 0;
Richard Genoud3102a762013-03-25 15:47:22 +01001032
1033unapply_new_state:
Richard Genoudda587512013-03-28 12:55:46 +01001034 dev_err(p->dev, "Error applying setting, reverse things back\n");
Richard Genoud3102a762013-03-25 15:47:22 +01001035
Richard Genoud3102a762013-03-25 15:47:22 +01001036 list_for_each_entry(setting2, &state->settings, node) {
1037 if (&setting2->node == &setting->node)
1038 break;
Richard Genoudaf606172013-03-29 10:03:26 +01001039 /*
1040 * All we can do here is pinmux_disable_setting.
1041 * That means that some pins are muxed differently now
1042 * than they were before applying the setting (We can't
1043 * "unmux a pin"!), but it's not a big deal since the pins
1044 * are free to be muxed by another apply_setting.
1045 */
1046 if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
1047 pinmux_disable_setting(setting2);
Richard Genoud3102a762013-03-25 15:47:22 +01001048 }
Richard Genoud8009d5f2013-03-28 12:55:47 +01001049
Richard Genoud385d9422013-03-29 10:03:27 +01001050 /* There's no infinite recursive loop here because p->state is NULL */
1051 if (old_state)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001052 pinctrl_select_state(p, old_state);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001053
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001054 return ret;
1055}
Stephen Warren6e5e9592012-03-02 13:05:47 -07001056EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001057
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001058static void devm_pinctrl_release(struct device *dev, void *res)
1059{
1060 pinctrl_put(*(struct pinctrl **)res);
1061}
1062
1063/**
1064 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
1065 * @dev: the device to obtain the handle for
1066 *
1067 * If there is a need to explicitly destroy the returned struct pinctrl,
1068 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1069 */
1070struct pinctrl *devm_pinctrl_get(struct device *dev)
1071{
1072 struct pinctrl **ptr, *p;
1073
1074 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
1075 if (!ptr)
1076 return ERR_PTR(-ENOMEM);
1077
1078 p = pinctrl_get(dev);
1079 if (!IS_ERR(p)) {
1080 *ptr = p;
1081 devres_add(dev, ptr);
1082 } else {
1083 devres_free(ptr);
1084 }
1085
1086 return p;
1087}
1088EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1089
1090static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1091{
1092 struct pinctrl **p = res;
1093
1094 return *p == data;
1095}
1096
1097/**
1098 * devm_pinctrl_put() - Resource managed pinctrl_put()
1099 * @p: the pinctrl handle to release
1100 *
1101 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1102 * this function will not need to be called and the resource management
1103 * code will ensure that the resource is freed.
1104 */
1105void devm_pinctrl_put(struct pinctrl *p)
1106{
Jingoo Hana72149e2013-02-26 11:34:07 +09001107 WARN_ON(devres_release(p->dev, devm_pinctrl_release,
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001108 devm_pinctrl_match, p));
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001109}
1110EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1111
Stephen Warren57291ce2012-03-23 10:29:46 -06001112int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
Doug Andersonc5272a22015-05-01 09:01:27 -07001113 bool dup)
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001114{
Stephen Warren1e2082b2012-03-02 13:05:48 -07001115 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001116 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001117
Masahiro Yamada7e9236f2015-05-28 21:52:59 +09001118 pr_debug("add %u pinctrl maps\n", num_maps);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001119
1120 /* First sanity check the new mapping */
1121 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001122 if (!maps[i].dev_name) {
1123 pr_err("failed to register map %s (%d): no device given\n",
1124 maps[i].name, i);
1125 return -EINVAL;
1126 }
1127
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001128 if (!maps[i].name) {
1129 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001130 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001131 return -EINVAL;
1132 }
1133
Stephen Warren1e2082b2012-03-02 13:05:48 -07001134 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1135 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001136 pr_err("failed to register map %s (%d): no pin control device given\n",
1137 maps[i].name, i);
1138 return -EINVAL;
1139 }
1140
Stephen Warren1e2082b2012-03-02 13:05:48 -07001141 switch (maps[i].type) {
1142 case PIN_MAP_TYPE_DUMMY_STATE:
1143 break;
1144 case PIN_MAP_TYPE_MUX_GROUP:
1145 ret = pinmux_validate_map(&maps[i], i);
1146 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001147 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001148 break;
1149 case PIN_MAP_TYPE_CONFIGS_PIN:
1150 case PIN_MAP_TYPE_CONFIGS_GROUP:
1151 ret = pinconf_validate_map(&maps[i], i);
1152 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001153 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001154 break;
1155 default:
1156 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001157 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -07001158 return -EINVAL;
1159 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001160 }
1161
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001162 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
1163 if (!maps_node) {
1164 pr_err("failed to alloc struct pinctrl_maps\n");
1165 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001166 }
1167
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001168 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -06001169 if (dup) {
1170 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
1171 GFP_KERNEL);
1172 if (!maps_node->maps) {
1173 pr_err("failed to duplicate mapping table\n");
1174 kfree(maps_node);
1175 return -ENOMEM;
1176 }
1177 } else {
1178 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001179 }
1180
Doug Andersonc5272a22015-05-01 09:01:27 -07001181 mutex_lock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001182 list_add_tail(&maps_node->node, &pinctrl_maps);
Doug Andersonc5272a22015-05-01 09:01:27 -07001183 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001184
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001185 return 0;
1186}
1187
Stephen Warren57291ce2012-03-23 10:29:46 -06001188/**
1189 * pinctrl_register_mappings() - register a set of pin controller mappings
1190 * @maps: the pincontrol mappings table to register. This should probably be
1191 * marked with __initdata so it can be discarded after boot. This
1192 * function will perform a shallow copy for the mapping entries.
1193 * @num_maps: the number of maps in the mapping table
1194 */
1195int pinctrl_register_mappings(struct pinctrl_map const *maps,
1196 unsigned num_maps)
1197{
Doug Andersonc5272a22015-05-01 09:01:27 -07001198 return pinctrl_register_map(maps, num_maps, true);
Stephen Warren57291ce2012-03-23 10:29:46 -06001199}
1200
1201void pinctrl_unregister_map(struct pinctrl_map const *map)
1202{
1203 struct pinctrl_maps *maps_node;
1204
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001205 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001206 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1207 if (maps_node->maps == map) {
1208 list_del(&maps_node->node);
Linus Walleijdb6c2c62013-07-23 01:43:20 +02001209 kfree(maps_node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001210 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001211 return;
1212 }
1213 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001214 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001215}
1216
Julien Delacou840a47b2012-12-10 14:47:33 +01001217/**
1218 * pinctrl_force_sleep() - turn a given controller device into sleep state
1219 * @pctldev: pin controller device
1220 */
1221int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1222{
1223 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
1224 return pinctrl_select_state(pctldev->p, pctldev->hog_sleep);
1225 return 0;
1226}
1227EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1228
1229/**
1230 * pinctrl_force_default() - turn a given controller device into default state
1231 * @pctldev: pin controller device
1232 */
1233int pinctrl_force_default(struct pinctrl_dev *pctldev)
1234{
1235 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
1236 return pinctrl_select_state(pctldev->p, pctldev->hog_default);
1237 return 0;
1238}
1239EXPORT_SYMBOL_GPL(pinctrl_force_default);
1240
Linus Walleij14005ee2013-06-05 15:30:33 +02001241#ifdef CONFIG_PM
1242
1243/**
Tony Lindgrenf3333492013-07-18 08:15:04 -07001244 * pinctrl_pm_select_state() - select pinctrl state for PM
1245 * @dev: device to select default state for
1246 * @state: state to set
1247 */
1248static int pinctrl_pm_select_state(struct device *dev,
1249 struct pinctrl_state *state)
1250{
1251 struct dev_pin_info *pins = dev->pins;
1252 int ret;
1253
1254 if (IS_ERR(state))
1255 return 0; /* No such state */
1256 ret = pinctrl_select_state(pins->p, state);
1257 if (ret)
1258 dev_err(dev, "failed to activate pinctrl state %s\n",
1259 state->name);
1260 return ret;
1261}
1262
1263/**
Linus Walleij14005ee2013-06-05 15:30:33 +02001264 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1265 * @dev: device to select default state for
1266 */
1267int pinctrl_pm_select_default_state(struct device *dev)
1268{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001269 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001270 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001271
1272 return pinctrl_pm_select_state(dev, dev->pins->default_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001273}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001274EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001275
1276/**
1277 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1278 * @dev: device to select sleep state for
1279 */
1280int pinctrl_pm_select_sleep_state(struct device *dev)
1281{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001282 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001283 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001284
1285 return pinctrl_pm_select_state(dev, dev->pins->sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001286}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001287EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001288
1289/**
1290 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1291 * @dev: device to select idle state for
1292 */
1293int pinctrl_pm_select_idle_state(struct device *dev)
1294{
Tony Lindgrenf3333492013-07-18 08:15:04 -07001295 if (!dev->pins)
Linus Walleij14005ee2013-06-05 15:30:33 +02001296 return 0;
Tony Lindgrenf3333492013-07-18 08:15:04 -07001297
1298 return pinctrl_pm_select_state(dev, dev->pins->idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001299}
Arnd Bergmannf472dea2013-06-17 17:12:28 +02001300EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
Linus Walleij14005ee2013-06-05 15:30:33 +02001301#endif
1302
Linus Walleij2744e8a2011-05-02 20:50:54 +02001303#ifdef CONFIG_DEBUG_FS
1304
1305static int pinctrl_pins_show(struct seq_file *s, void *what)
1306{
1307 struct pinctrl_dev *pctldev = s->private;
1308 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001309 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001310
1311 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001312
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001313 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001314
Chanho Park706e8522012-01-03 16:47:50 +09001315 /* The pin number can be retrived from the pin controller descriptor */
1316 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001317 struct pin_desc *desc;
1318
Chanho Park706e8522012-01-03 16:47:50 +09001319 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001320 desc = pin_desc_get(pctldev, pin);
1321 /* Pin space may be sparse */
1322 if (desc == NULL)
1323 continue;
1324
1325 seq_printf(s, "pin %d (%s) ", pin,
1326 desc->name ? desc->name : "unnamed");
1327
1328 /* Driver-specific info per pin */
1329 if (ops->pin_dbg_show)
1330 ops->pin_dbg_show(pctldev, s, pin);
1331
1332 seq_puts(s, "\n");
1333 }
1334
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001335 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001336
Linus Walleij2744e8a2011-05-02 20:50:54 +02001337 return 0;
1338}
1339
1340static int pinctrl_groups_show(struct seq_file *s, void *what)
1341{
1342 struct pinctrl_dev *pctldev = s->private;
1343 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301344 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001345
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001346 mutex_lock(&pctldev->mutex);
1347
Viresh Kumard1e90e92012-03-30 11:25:40 +05301348 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001349
Linus Walleij2744e8a2011-05-02 20:50:54 +02001350 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301351 while (selector < ngroups) {
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001352 const unsigned *pins = NULL;
1353 unsigned num_pins = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001354 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001355 const char *pname;
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001356 int ret = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001357 int i;
1358
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001359 if (ops->get_group_pins)
1360 ret = ops->get_group_pins(pctldev, selector,
1361 &pins, &num_pins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001362 if (ret)
1363 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1364 gname);
1365 else {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001366 seq_printf(s, "group: %s\n", gname);
1367 for (i = 0; i < num_pins; i++) {
1368 pname = pin_get_name(pctldev, pins[i]);
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001369 if (WARN_ON(!pname)) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001370 mutex_unlock(&pctldev->mutex);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001371 return -EINVAL;
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001372 }
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001373 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1374 }
1375 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001376 }
1377 selector++;
1378 }
1379
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001380 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001381
1382 return 0;
1383}
1384
1385static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1386{
1387 struct pinctrl_dev *pctldev = s->private;
1388 struct pinctrl_gpio_range *range = NULL;
1389
1390 seq_puts(s, "GPIO ranges handled:\n");
1391
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001392 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001393
Linus Walleij2744e8a2011-05-02 20:50:54 +02001394 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001395 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Christian Ruppertc8587ee2013-06-13 14:55:31 +02001396 if (range->pins) {
1397 int a;
1398 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
1399 range->id, range->name,
1400 range->base, (range->base + range->npins - 1));
1401 for (a = 0; a < range->npins - 1; a++)
1402 seq_printf(s, "%u, ", range->pins[a]);
1403 seq_printf(s, "%u}\n", range->pins[a]);
1404 }
1405 else
1406 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1407 range->id, range->name,
1408 range->base, (range->base + range->npins - 1),
1409 range->pin_base,
1410 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001411 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001412
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001413 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001414
1415 return 0;
1416}
1417
1418static int pinctrl_devices_show(struct seq_file *s, void *what)
1419{
1420 struct pinctrl_dev *pctldev;
1421
Linus Walleijae6b4d82011-10-19 18:14:33 +02001422 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001423
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001424 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001425
Linus Walleij2744e8a2011-05-02 20:50:54 +02001426 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1427 seq_printf(s, "%s ", pctldev->desc->name);
1428 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001429 seq_puts(s, "yes ");
1430 else
1431 seq_puts(s, "no ");
1432 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001433 seq_puts(s, "yes");
1434 else
1435 seq_puts(s, "no");
1436 seq_puts(s, "\n");
1437 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001438
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001439 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001440
1441 return 0;
1442}
1443
Stephen Warren1e2082b2012-03-02 13:05:48 -07001444static inline const char *map_type(enum pinctrl_map_type type)
1445{
1446 static const char * const names[] = {
1447 "INVALID",
1448 "DUMMY_STATE",
1449 "MUX_GROUP",
1450 "CONFIGS_PIN",
1451 "CONFIGS_GROUP",
1452 };
1453
1454 if (type >= ARRAY_SIZE(names))
1455 return "UNKNOWN";
1456
1457 return names[type];
1458}
1459
Stephen Warren3eedb432012-02-23 17:04:40 -07001460static int pinctrl_maps_show(struct seq_file *s, void *what)
1461{
1462 struct pinctrl_maps *maps_node;
1463 int i;
1464 struct pinctrl_map const *map;
1465
1466 seq_puts(s, "Pinctrl maps:\n");
1467
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001468 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001469 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001470 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1471 map->dev_name, map->name, map_type(map->type),
1472 map->type);
1473
1474 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1475 seq_printf(s, "controlling device %s\n",
1476 map->ctrl_dev_name);
1477
1478 switch (map->type) {
1479 case PIN_MAP_TYPE_MUX_GROUP:
1480 pinmux_show_map(s, map);
1481 break;
1482 case PIN_MAP_TYPE_CONFIGS_PIN:
1483 case PIN_MAP_TYPE_CONFIGS_GROUP:
1484 pinconf_show_map(s, map);
1485 break;
1486 default:
1487 break;
1488 }
1489
1490 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001491 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001492 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001493
1494 return 0;
1495}
1496
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001497static int pinctrl_show(struct seq_file *s, void *what)
1498{
1499 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001500 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001501 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001502
1503 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001504
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001505 mutex_lock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001506
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001507 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001508 seq_printf(s, "device: %s current state: %s\n",
1509 dev_name(p->dev),
1510 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001511
Stephen Warren6e5e9592012-03-02 13:05:47 -07001512 list_for_each_entry(state, &p->states, node) {
1513 seq_printf(s, " state: %s\n", state->name);
1514
1515 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001516 struct pinctrl_dev *pctldev = setting->pctldev;
1517
1518 seq_printf(s, " type: %s controller %s ",
1519 map_type(setting->type),
1520 pinctrl_dev_get_name(pctldev));
1521
1522 switch (setting->type) {
1523 case PIN_MAP_TYPE_MUX_GROUP:
1524 pinmux_show_setting(s, setting);
1525 break;
1526 case PIN_MAP_TYPE_CONFIGS_PIN:
1527 case PIN_MAP_TYPE_CONFIGS_GROUP:
1528 pinconf_show_setting(s, setting);
1529 break;
1530 default:
1531 break;
1532 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001533 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001534 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001535 }
1536
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001537 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001538
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001539 return 0;
1540}
1541
Linus Walleij2744e8a2011-05-02 20:50:54 +02001542static int pinctrl_pins_open(struct inode *inode, struct file *file)
1543{
1544 return single_open(file, pinctrl_pins_show, inode->i_private);
1545}
1546
1547static int pinctrl_groups_open(struct inode *inode, struct file *file)
1548{
1549 return single_open(file, pinctrl_groups_show, inode->i_private);
1550}
1551
1552static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1553{
1554 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1555}
1556
1557static int pinctrl_devices_open(struct inode *inode, struct file *file)
1558{
1559 return single_open(file, pinctrl_devices_show, NULL);
1560}
1561
Stephen Warren3eedb432012-02-23 17:04:40 -07001562static int pinctrl_maps_open(struct inode *inode, struct file *file)
1563{
1564 return single_open(file, pinctrl_maps_show, NULL);
1565}
1566
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001567static int pinctrl_open(struct inode *inode, struct file *file)
1568{
1569 return single_open(file, pinctrl_show, NULL);
1570}
1571
Linus Walleij2744e8a2011-05-02 20:50:54 +02001572static const struct file_operations pinctrl_pins_ops = {
1573 .open = pinctrl_pins_open,
1574 .read = seq_read,
1575 .llseek = seq_lseek,
1576 .release = single_release,
1577};
1578
1579static const struct file_operations pinctrl_groups_ops = {
1580 .open = pinctrl_groups_open,
1581 .read = seq_read,
1582 .llseek = seq_lseek,
1583 .release = single_release,
1584};
1585
1586static const struct file_operations pinctrl_gpioranges_ops = {
1587 .open = pinctrl_gpioranges_open,
1588 .read = seq_read,
1589 .llseek = seq_lseek,
1590 .release = single_release,
1591};
1592
1593static const struct file_operations pinctrl_devices_ops = {
1594 .open = pinctrl_devices_open,
1595 .read = seq_read,
1596 .llseek = seq_lseek,
1597 .release = single_release,
1598};
1599
Stephen Warren3eedb432012-02-23 17:04:40 -07001600static const struct file_operations pinctrl_maps_ops = {
1601 .open = pinctrl_maps_open,
1602 .read = seq_read,
1603 .llseek = seq_lseek,
1604 .release = single_release,
1605};
1606
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001607static const struct file_operations pinctrl_ops = {
1608 .open = pinctrl_open,
1609 .read = seq_read,
1610 .llseek = seq_lseek,
1611 .release = single_release,
1612};
1613
Linus Walleij2744e8a2011-05-02 20:50:54 +02001614static struct dentry *debugfs_root;
1615
1616static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1617{
Tony Lindgren02157162012-01-20 08:17:22 -08001618 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001619
Stephen Warren51cd24e2011-12-09 16:59:05 -07001620 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001621 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001622 pctldev->device_root = device_root;
1623
Linus Walleij2744e8a2011-05-02 20:50:54 +02001624 if (IS_ERR(device_root) || !device_root) {
1625 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001626 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001627 return;
1628 }
1629 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1630 device_root, pctldev, &pinctrl_pins_ops);
1631 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1632 device_root, pctldev, &pinctrl_groups_ops);
1633 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1634 device_root, pctldev, &pinctrl_gpioranges_ops);
Florian Vaussarde7f2a442014-02-05 07:51:22 +01001635 if (pctldev->desc->pmxops)
1636 pinmux_init_device_debugfs(device_root, pctldev);
1637 if (pctldev->desc->confops)
1638 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001639}
1640
Tony Lindgren02157162012-01-20 08:17:22 -08001641static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1642{
1643 debugfs_remove_recursive(pctldev->device_root);
1644}
1645
Linus Walleij2744e8a2011-05-02 20:50:54 +02001646static void pinctrl_init_debugfs(void)
1647{
1648 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1649 if (IS_ERR(debugfs_root) || !debugfs_root) {
1650 pr_warn("failed to create debugfs directory\n");
1651 debugfs_root = NULL;
1652 return;
1653 }
1654
1655 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1656 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001657 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1658 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001659 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1660 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001661}
1662
1663#else /* CONFIG_DEBUG_FS */
1664
1665static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1666{
1667}
1668
1669static void pinctrl_init_debugfs(void)
1670{
1671}
1672
Tony Lindgren02157162012-01-20 08:17:22 -08001673static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1674{
1675}
1676
Linus Walleij2744e8a2011-05-02 20:50:54 +02001677#endif
1678
Stephen Warrend26bc492012-03-16 14:54:25 -06001679static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1680{
1681 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1682
1683 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301684 !ops->get_groups_count ||
Antoine Ténarte5b3b2d2014-04-10 15:07:50 +02001685 !ops->get_group_name)
Stephen Warrend26bc492012-03-16 14:54:25 -06001686 return -EINVAL;
1687
Stephen Warren57291ce2012-03-23 10:29:46 -06001688 if (ops->dt_node_to_map && !ops->dt_free_map)
1689 return -EINVAL;
1690
Stephen Warrend26bc492012-03-16 14:54:25 -06001691 return 0;
1692}
1693
Linus Walleij2744e8a2011-05-02 20:50:54 +02001694/**
1695 * pinctrl_register() - register a pin controller device
1696 * @pctldesc: descriptor for this pin controller
1697 * @dev: parent device for this pin controller
1698 * @driver_data: private pin controller data for this pin controller
1699 */
1700struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1701 struct device *dev, void *driver_data)
1702{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001703 struct pinctrl_dev *pctldev;
1704 int ret;
1705
Devendra Nagada9aecb2012-06-16 23:43:16 +05301706 if (!pctldesc)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001707 return ERR_PTR(-EINVAL);
Devendra Nagada9aecb2012-06-16 23:43:16 +05301708 if (!pctldesc->name)
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001709 return ERR_PTR(-EINVAL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001710
Stephen Warren02f5b982012-02-22 14:26:00 -07001711 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001712 if (pctldev == NULL) {
1713 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001714 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001715 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001716
1717 /* Initialize pin control device struct */
1718 pctldev->owner = pctldesc->owner;
1719 pctldev->desc = pctldesc;
1720 pctldev->driver_data = driver_data;
1721 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001722 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001723 pctldev->dev = dev;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001724 mutex_init(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001725
Stephen Warrend26bc492012-03-16 14:54:25 -06001726 /* check core ops for sanity */
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001727 ret = pinctrl_check_ops(pctldev);
1728 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001729 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06001730 goto out_err;
1731 }
1732
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001733 /* If we're implementing pinmuxing, check the ops for sanity */
1734 if (pctldesc->pmxops) {
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001735 ret = pinmux_check_ops(pctldev);
1736 if (ret)
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001737 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001738 }
1739
1740 /* If we're implementing pinconfig, check the ops for sanity */
1741 if (pctldesc->confops) {
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001742 ret = pinconf_check_ops(pctldev);
1743 if (ret)
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001744 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001745 }
1746
Linus Walleij2744e8a2011-05-02 20:50:54 +02001747 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02001748 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001749 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1750 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001751 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001752 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1753 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001754 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001755 }
1756
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001757 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -07001758 list_add_tail(&pctldev->node, &pinctrldev_list);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001759 mutex_unlock(&pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001760
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001761 pctldev->p = pinctrl_get(pctldev->dev);
1762
Stephen Warren6e5e9592012-03-02 13:05:47 -07001763 if (!IS_ERR(pctldev->p)) {
Julien Delacou840a47b2012-12-10 14:47:33 +01001764 pctldev->hog_default =
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001765 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
Julien Delacou840a47b2012-12-10 14:47:33 +01001766 if (IS_ERR(pctldev->hog_default)) {
John Crispinad6e1102012-04-26 16:47:11 +02001767 dev_dbg(dev, "failed to lookup the default state\n");
1768 } else {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001769 if (pinctrl_select_state(pctldev->p,
Julien Delacou840a47b2012-12-10 14:47:33 +01001770 pctldev->hog_default))
John Crispinad6e1102012-04-26 16:47:11 +02001771 dev_err(dev,
1772 "failed to select default state\n");
John Crispinad6e1102012-04-26 16:47:11 +02001773 }
Julien Delacou840a47b2012-12-10 14:47:33 +01001774
1775 pctldev->hog_sleep =
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001776 pinctrl_lookup_state(pctldev->p,
Julien Delacou840a47b2012-12-10 14:47:33 +01001777 PINCTRL_STATE_SLEEP);
1778 if (IS_ERR(pctldev->hog_sleep))
1779 dev_dbg(dev, "failed to lookup the sleep state\n");
Stephen Warren6e5e9592012-03-02 13:05:47 -07001780 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001781
Stephen Warren2304b472012-02-22 14:26:01 -07001782 pinctrl_init_device_debugfs(pctldev);
1783
Linus Walleij2744e8a2011-05-02 20:50:54 +02001784 return pctldev;
1785
Stephen Warren51cd24e2011-12-09 16:59:05 -07001786out_err:
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001787 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001788 kfree(pctldev);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001789 return ERR_PTR(ret);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001790}
1791EXPORT_SYMBOL_GPL(pinctrl_register);
1792
1793/**
1794 * pinctrl_unregister() - unregister pinmux
1795 * @pctldev: pin controller to unregister
1796 *
1797 * Called by pinmux drivers to unregister a pinmux.
1798 */
1799void pinctrl_unregister(struct pinctrl_dev *pctldev)
1800{
Dong Aisheng5d589b02012-05-23 21:22:40 +08001801 struct pinctrl_gpio_range *range, *n;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001802 if (pctldev == NULL)
1803 return;
1804
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001805 mutex_lock(&pctldev->mutex);
Tony Lindgren02157162012-01-20 08:17:22 -08001806 pinctrl_remove_device_debugfs(pctldev);
Jim Lindb93fac2015-01-08 20:25:05 +08001807 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001808
Stephen Warren6e5e9592012-03-02 13:05:47 -07001809 if (!IS_ERR(pctldev->p))
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001810 pinctrl_put(pctldev->p);
Stephen Warren57b676f2012-03-02 13:05:44 -07001811
Jim Lindb93fac2015-01-08 20:25:05 +08001812 mutex_lock(&pinctrldev_list_mutex);
1813 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001814 /* TODO: check that no pinmuxes are still active? */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001815 list_del(&pctldev->node);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001816 /* Destroy descriptor tree */
1817 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1818 pctldev->desc->npins);
Dong Aisheng5d589b02012-05-23 21:22:40 +08001819 /* remove gpio ranges map */
1820 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1821 list_del(&range->node);
1822
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001823 mutex_unlock(&pctldev->mutex);
1824 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001825 kfree(pctldev);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001826 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001827}
1828EXPORT_SYMBOL_GPL(pinctrl_unregister);
1829
1830static int __init pinctrl_init(void)
1831{
1832 pr_info("initialized pinctrl subsystem\n");
1833 pinctrl_init_debugfs();
1834 return 0;
1835}
1836
1837/* init early since many drivers really need to initialized pinmux early */
1838core_initcall(pinctrl_init);