blob: 9c9fc69a01b347d43cfafe2514fff8a90a1f4943 [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 */
44DEFINE_MUTEX(pinctrl_list_mutex);
45
46/* Mutex taken to protect pinctrl_maps */
47DEFINE_MUTEX(pinctrl_maps_mutex);
48
49/* Mutex taken to protect pinctrldev_list */
50DEFINE_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;
104 bool found = false;
105
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100106 if (!devname)
107 return NULL;
108
Linus Walleij2744e8a2011-05-02 20:50:54 +0200109 list_for_each_entry(pctldev, &pinctrldev_list, node) {
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100110 if (!strcmp(dev_name(pctldev->dev), devname)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200111 /* Matched on device name */
112 found = true;
113 break;
114 }
115 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200116
117 return found ? pctldev : NULL;
118}
119
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200120struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np)
121{
122 struct pinctrl_dev *pctldev;
123
124 mutex_lock(&pinctrldev_list_mutex);
125
126 list_for_each_entry(pctldev, &pinctrldev_list, node)
127 if (pctldev->dev->of_node == np) {
128 mutex_unlock(&pinctrldev_list_mutex);
129 return pctldev;
130 }
131
Daniel Mackd463f822013-04-26 18:57:02 +0200132 mutex_unlock(&pinctrldev_list_mutex);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200133
134 return NULL;
135}
136
Linus Walleij2744e8a2011-05-02 20:50:54 +0200137/**
Linus Walleijae6b4d82011-10-19 18:14:33 +0200138 * pin_get_from_name() - look up a pin number from a name
139 * @pctldev: the pin control device to lookup the pin on
140 * @name: the name of the pin to look up
141 */
142int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
143{
Chanho Park706e8522012-01-03 16:47:50 +0900144 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200145
Chanho Park706e8522012-01-03 16:47:50 +0900146 /* The pin number can be retrived from the pin controller descriptor */
147 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200148 struct pin_desc *desc;
149
Chanho Park706e8522012-01-03 16:47:50 +0900150 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200151 desc = pin_desc_get(pctldev, pin);
152 /* Pin space may be sparse */
153 if (desc == NULL)
154 continue;
155 if (desc->name && !strcmp(name, desc->name))
156 return pin;
157 }
158
159 return -EINVAL;
160}
161
162/**
Dong Aishengdcb5dbc2012-04-17 15:00:46 +0800163 * pin_get_name_from_id() - look up a pin name from a pin id
164 * @pctldev: the pin control device to lookup the pin on
165 * @name: the name of the pin to look up
166 */
167const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
168{
169 const struct pin_desc *desc;
170
171 desc = pin_desc_get(pctldev, pin);
172 if (desc == NULL) {
173 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
174 pin);
175 return NULL;
176 }
177
178 return desc->name;
179}
180
181/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200182 * pin_is_valid() - check if pin exists on controller
183 * @pctldev: the pin control device to check the pin on
184 * @pin: pin to check, use the local pin controller index number
185 *
186 * This tells us whether a certain pin exist on a certain pin controller or
187 * not. Pin lists may be sparse, so some pins may not exist.
188 */
189bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
190{
191 struct pin_desc *pindesc;
192
193 if (pin < 0)
194 return false;
195
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200196 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200197 pindesc = pin_desc_get(pctldev, pin);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200198 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200199
Stephen Warren57b676f2012-03-02 13:05:44 -0700200 return pindesc != NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200201}
202EXPORT_SYMBOL_GPL(pin_is_valid);
203
204/* Deletes a range of pin descriptors */
205static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
206 const struct pinctrl_pin_desc *pins,
207 unsigned num_pins)
208{
209 int i;
210
Linus Walleij2744e8a2011-05-02 20:50:54 +0200211 for (i = 0; i < num_pins; i++) {
212 struct pin_desc *pindesc;
213
214 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
215 pins[i].number);
216 if (pindesc != NULL) {
217 radix_tree_delete(&pctldev->pin_desc_tree,
218 pins[i].number);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100219 if (pindesc->dynamic_name)
220 kfree(pindesc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200221 }
222 kfree(pindesc);
223 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200224}
225
226static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
227 unsigned number, const char *name)
228{
229 struct pin_desc *pindesc;
230
231 pindesc = pin_desc_get(pctldev, number);
232 if (pindesc != NULL) {
233 pr_err("pin %d already registered on %s\n", number,
234 pctldev->desc->name);
235 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/**
283 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
284 * @pctldev: pin controller device to check
285 * @gpio: gpio pin to check taken from the global GPIO pin space
286 *
287 * Tries to match a GPIO pin number to the ranges handled by a certain pin
288 * controller, return the range or NULL
289 */
290static struct pinctrl_gpio_range *
291pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
292{
293 struct pinctrl_gpio_range *range = NULL;
294
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200295 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200296 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200297 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
298 /* Check if we're in the valid range */
299 if (gpio >= range->base &&
300 gpio < range->base + range->npins) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200301 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200302 return range;
303 }
304 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200305 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200306 return NULL;
307}
308
309/**
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800310 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
311 * the same GPIO chip are in range
312 * @gpio: gpio pin to check taken from the global GPIO pin space
313 *
314 * This function is complement of pinctrl_match_gpio_range(). If the return
315 * value of pinctrl_match_gpio_range() is NULL, this function could be used
316 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
317 * of the same GPIO chip don't have back-end pinctrl interface.
318 * If the return value is true, it means that pinctrl device is ready & the
319 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
320 * is false, it means that pinctrl device may not be ready.
321 */
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800322#ifdef CONFIG_GPIOLIB
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800323static bool pinctrl_ready_for_gpio_range(unsigned gpio)
324{
325 struct pinctrl_dev *pctldev;
326 struct pinctrl_gpio_range *range = NULL;
327 struct gpio_chip *chip = gpio_to_chip(gpio);
328
329 /* Loop over the pin controllers */
330 list_for_each_entry(pctldev, &pinctrldev_list, node) {
331 /* Loop over the ranges */
332 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
333 /* Check if any gpio range overlapped with gpio chip */
334 if (range->base + range->npins - 1 < chip->base ||
335 range->base > chip->base + chip->ngpio - 1)
336 continue;
337 return true;
338 }
339 }
340 return false;
341}
Haojian Zhuang2afe8222013-03-28 07:34:19 +0800342#else
343static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
344#endif
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800345
346/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200347 * pinctrl_get_device_gpio_range() - find device for GPIO range
348 * @gpio: the pin to locate the pin controller for
349 * @outdev: the pin control device if found
350 * @outrange: the GPIO range if found
351 *
352 * Find the pin controller handling a certain GPIO pin from the pinspace of
353 * the GPIO subsystem, return the device and the matching GPIO range. Returns
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800354 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
355 * may still have not been registered.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200356 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700357static int pinctrl_get_device_gpio_range(unsigned gpio,
358 struct pinctrl_dev **outdev,
359 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200360{
361 struct pinctrl_dev *pctldev = NULL;
362
363 /* Loop over the pin controllers */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200364 list_for_each_entry(pctldev, &pinctrldev_list, node) {
365 struct pinctrl_gpio_range *range;
366
367 range = pinctrl_match_gpio_range(pctldev, gpio);
368 if (range != NULL) {
369 *outdev = pctldev;
370 *outrange = range;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200371 return 0;
372 }
373 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200374
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800375 return -EPROBE_DEFER;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200376}
377
378/**
379 * pinctrl_add_gpio_range() - register a GPIO range for a controller
380 * @pctldev: pin controller device to add the range to
381 * @range: the GPIO range to add
382 *
383 * This adds a range of GPIOs to be handled by a certain pin controller. Call
384 * this to register handled ranges after registering your pin controller.
385 */
386void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
387 struct pinctrl_gpio_range *range)
388{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200389 mutex_lock(&pctldev->mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700390 list_add_tail(&range->node, &pctldev->gpio_ranges);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200391 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200392}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700393EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200394
Dong Aisheng3e5e00b2012-05-23 21:22:41 +0800395void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
396 struct pinctrl_gpio_range *ranges,
397 unsigned nranges)
398{
399 int i;
400
401 for (i = 0; i < nranges; i++)
402 pinctrl_add_gpio_range(pctldev, &ranges[i]);
403}
404EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
405
Linus Walleij192c3692012-11-20 14:03:37 +0100406struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530407 struct pinctrl_gpio_range *range)
408{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200409 struct pinctrl_dev *pctldev;
410
411 mutex_lock(&pinctrldev_list_mutex);
412
413 pctldev = get_pinctrl_dev_from_devname(devname);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530414
Linus Walleijdfa97512012-11-20 14:54:18 +0100415 /*
416 * If we can't find this device, let's assume that is because
417 * it has not probed yet, so the driver trying to register this
418 * range need to defer probing.
419 */
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200420 if (!pctldev) {
421 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleijdfa97512012-11-20 14:54:18 +0100422 return ERR_PTR(-EPROBE_DEFER);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200423 }
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530424 pinctrl_add_gpio_range(pctldev, range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200425
426 mutex_unlock(&pinctrldev_list_mutex);
427
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530428 return pctldev;
429}
Linus Walleij192c3692012-11-20 14:03:37 +0100430EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530431
Linus Walleij2744e8a2011-05-02 20:50:54 +0200432/**
Linus Walleij9afbefb2012-11-20 14:25:07 +0100433 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
434 * @pctldev: the pin controller device to look in
435 * @pin: a controller-local number to find the range for
436 */
437struct pinctrl_gpio_range *
438pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
439 unsigned int pin)
440{
441 struct pinctrl_gpio_range *range = NULL;
442
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200443 mutex_lock(&pctldev->mutex);
Linus Walleij9afbefb2012-11-20 14:25:07 +0100444 /* Loop over the ranges */
445 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
446 /* Check if we're in the valid range */
447 if (pin >= range->pin_base &&
448 pin < range->pin_base + range->npins) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200449 mutex_unlock(&pctldev->mutex);
Linus Walleij9afbefb2012-11-20 14:25:07 +0100450 return range;
451 }
452 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200453 mutex_unlock(&pctldev->mutex);
Linus Walleij9afbefb2012-11-20 14:25:07 +0100454
455 return NULL;
456}
457EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
458
459/**
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530460 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
461 * @pctldev: pin controller device to remove the range from
462 * @range: the GPIO range to remove
463 */
464void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
465 struct pinctrl_gpio_range *range)
466{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200467 mutex_lock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530468 list_del(&range->node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200469 mutex_unlock(&pctldev->mutex);
Viresh Kumar7e10ee62012-10-27 15:21:35 +0530470}
471EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
472
473/**
Linus Walleij7afde8b2011-10-19 17:07:16 +0200474 * pinctrl_get_group_selector() - returns the group selector for a group
475 * @pctldev: the pin controller handling the group
476 * @pin_group: the pin group to look up
477 */
478int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
479 const char *pin_group)
480{
481 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530482 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200483 unsigned group_selector = 0;
484
Viresh Kumard1e90e92012-03-30 11:25:40 +0530485 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200486 const char *gname = pctlops->get_group_name(pctldev,
487 group_selector);
488 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700489 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200490 "found group selector %u for %s\n",
491 group_selector,
492 pin_group);
493 return group_selector;
494 }
495
496 group_selector++;
497 }
498
Stephen Warren51cd24e2011-12-09 16:59:05 -0700499 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200500 pin_group);
501
502 return -EINVAL;
503}
504
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100505/**
506 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
507 * @gpio: the GPIO pin number from the GPIO subsystem number space
508 *
509 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
510 * as part of their gpio_request() semantics, platforms and individual drivers
511 * shall *NOT* request GPIO pins to be muxed in.
512 */
513int pinctrl_request_gpio(unsigned gpio)
514{
515 struct pinctrl_dev *pctldev;
516 struct pinctrl_gpio_range *range;
517 int ret;
518 int pin;
519
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200520 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700521
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100522 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700523 if (ret) {
Haojian Zhuang51e13c22013-02-17 19:42:50 +0800524 if (pinctrl_ready_for_gpio_range(gpio))
525 ret = 0;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200526 mutex_unlock(&pinctrldev_list_mutex);
Dong Aisheng4650b7c2012-04-25 19:38:13 +0800527 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700528 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100529
530 /* Convert to the pin controllers number space */
531 pin = gpio - range->base + range->pin_base;
532
Stephen Warren57b676f2012-03-02 13:05:44 -0700533 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
534
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200535 mutex_unlock(&pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700536 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100537}
538EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
539
540/**
541 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
542 * @gpio: the GPIO pin number from the GPIO subsystem number space
543 *
544 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
545 * as part of their gpio_free() semantics, platforms and individual drivers
546 * shall *NOT* request GPIO pins to be muxed out.
547 */
548void pinctrl_free_gpio(unsigned gpio)
549{
550 struct pinctrl_dev *pctldev;
551 struct pinctrl_gpio_range *range;
552 int ret;
553 int pin;
554
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200555 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700556
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100557 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700558 if (ret) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200559 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100560 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700561 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200562 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100563
564 /* Convert to the pin controllers number space */
565 pin = gpio - range->base + range->pin_base;
566
Stephen Warren57b676f2012-03-02 13:05:44 -0700567 pinmux_free_gpio(pctldev, pin, range);
568
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200569 mutex_unlock(&pctldev->mutex);
570 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100571}
572EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
573
574static int pinctrl_gpio_direction(unsigned gpio, bool input)
575{
576 struct pinctrl_dev *pctldev;
577 struct pinctrl_gpio_range *range;
578 int ret;
579 int pin;
580
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200581 mutex_lock(&pinctrldev_list_mutex);
582
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100583 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200584 if (ret) {
585 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100586 return ret;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200587 }
588
589 mutex_lock(&pctldev->mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100590
591 /* Convert to the pin controllers number space */
592 pin = gpio - range->base + range->pin_base;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200593 ret = pinmux_gpio_direction(pctldev, range, pin, input);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100594
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200595 mutex_unlock(&pctldev->mutex);
596 mutex_unlock(&pinctrldev_list_mutex);
597
598 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100599}
600
601/**
602 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
603 * @gpio: the GPIO pin number from the GPIO subsystem number space
604 *
605 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
606 * as part of their gpio_direction_input() semantics, platforms and individual
607 * drivers shall *NOT* touch pin control GPIO calls.
608 */
609int pinctrl_gpio_direction_input(unsigned gpio)
610{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200611 return pinctrl_gpio_direction(gpio, true);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100612}
613EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
614
615/**
616 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
617 * @gpio: the GPIO pin number from the GPIO subsystem number space
618 *
619 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
620 * as part of their gpio_direction_output() semantics, platforms and individual
621 * drivers shall *NOT* touch pin control GPIO calls.
622 */
623int pinctrl_gpio_direction_output(unsigned gpio)
624{
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200625 return pinctrl_gpio_direction(gpio, false);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100626}
627EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
628
Stephen Warren6e5e9592012-03-02 13:05:47 -0700629static struct pinctrl_state *find_state(struct pinctrl *p,
630 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100631{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700632 struct pinctrl_state *state;
633
634 list_for_each_entry(state, &p->states, node)
635 if (!strcmp(state->name, name))
636 return state;
637
638 return NULL;
639}
640
641static struct pinctrl_state *create_state(struct pinctrl *p,
642 const char *name)
643{
644 struct pinctrl_state *state;
645
646 state = kzalloc(sizeof(*state), GFP_KERNEL);
647 if (state == NULL) {
648 dev_err(p->dev,
649 "failed to alloc struct pinctrl_state\n");
650 return ERR_PTR(-ENOMEM);
651 }
652
653 state->name = name;
654 INIT_LIST_HEAD(&state->settings);
655
656 list_add_tail(&state->node, &p->states);
657
658 return state;
659}
660
661static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
662{
663 struct pinctrl_state *state;
664 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700665 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700666
667 state = find_state(p, map->name);
668 if (!state)
669 state = create_state(p, map->name);
670 if (IS_ERR(state))
671 return PTR_ERR(state);
672
Stephen Warren1e2082b2012-03-02 13:05:48 -0700673 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
674 return 0;
675
Stephen Warren6e5e9592012-03-02 13:05:47 -0700676 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
677 if (setting == NULL) {
678 dev_err(p->dev,
679 "failed to alloc struct pinctrl_setting\n");
680 return -ENOMEM;
681 }
682
Stephen Warren1e2082b2012-03-02 13:05:48 -0700683 setting->type = map->type;
684
Stephen Warren6e5e9592012-03-02 13:05:47 -0700685 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
686 if (setting->pctldev == NULL) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700687 kfree(setting);
Linus Walleij89216492012-12-11 14:14:32 +0100688 /* Do not defer probing of hogs (circular loop) */
689 if (!strcmp(map->ctrl_dev_name, map->dev_name))
690 return -ENODEV;
Linus Walleijc05127c2012-04-10 10:00:38 +0200691 /*
692 * OK let us guess that the driver is not there yet, and
693 * let's defer obtaining this pinctrl handle to later...
694 */
Linus Walleij89216492012-12-11 14:14:32 +0100695 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
696 map->ctrl_dev_name);
Linus Walleijc05127c2012-04-10 10:00:38 +0200697 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700698 }
699
Linus Walleij1a789582012-10-17 20:51:54 +0200700 setting->dev_name = map->dev_name;
701
Stephen Warren1e2082b2012-03-02 13:05:48 -0700702 switch (map->type) {
703 case PIN_MAP_TYPE_MUX_GROUP:
704 ret = pinmux_map_to_setting(map, setting);
705 break;
706 case PIN_MAP_TYPE_CONFIGS_PIN:
707 case PIN_MAP_TYPE_CONFIGS_GROUP:
708 ret = pinconf_map_to_setting(map, setting);
709 break;
710 default:
711 ret = -EINVAL;
712 break;
713 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700714 if (ret < 0) {
715 kfree(setting);
716 return ret;
717 }
718
719 list_add_tail(&setting->node, &state->settings);
720
721 return 0;
722}
723
724static struct pinctrl *find_pinctrl(struct device *dev)
725{
726 struct pinctrl *p;
727
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200728 mutex_lock(&pinctrl_list_mutex);
Stephen Warren1e2082b2012-03-02 13:05:48 -0700729 list_for_each_entry(p, &pinctrl_list, node)
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200730 if (p->dev == dev) {
731 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700732 return p;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200733 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700734
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200735 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700736 return NULL;
737}
738
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200739static void pinctrl_free(struct pinctrl *p, bool inlist);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700740
741static struct pinctrl *create_pinctrl(struct device *dev)
742{
743 struct pinctrl *p;
744 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700745 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100746 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700747 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700748 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100749
750 /*
751 * create the state cookie holder struct pinctrl for each
752 * mapping, this is what consumers will get when requesting
753 * a pin control handle with pinctrl_get()
754 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700755 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700756 if (p == NULL) {
757 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100758 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700759 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700760 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700761 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -0600762 INIT_LIST_HEAD(&p->dt_maps);
763
764 ret = pinctrl_dt_to_map(p);
765 if (ret < 0) {
766 kfree(p);
767 return ERR_PTR(ret);
768 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700769
770 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100771
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200772 mutex_lock(&pinctrl_maps_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100773 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700774 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -0700775 /* Map must be for this device */
776 if (strcmp(map->dev_name, devname))
777 continue;
778
Stephen Warren6e5e9592012-03-02 13:05:47 -0700779 ret = add_setting(p, map);
Linus Walleij89216492012-12-11 14:14:32 +0100780 /*
781 * At this point the adding of a setting may:
782 *
783 * - Defer, if the pinctrl device is not yet available
784 * - Fail, if the pinctrl device is not yet available,
785 * AND the setting is a hog. We cannot defer that, since
786 * the hog will kick in immediately after the device
787 * is registered.
788 *
789 * If the error returned was not -EPROBE_DEFER then we
790 * accumulate the errors to see if we end up with
791 * an -EPROBE_DEFER later, as that is the worst case.
792 */
793 if (ret == -EPROBE_DEFER) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200794 pinctrl_free(p, false);
795 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700796 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100797 }
798 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200799 mutex_unlock(&pinctrl_maps_mutex);
800
Linus Walleij89216492012-12-11 14:14:32 +0100801 if (ret < 0) {
802 /* If some other error than deferral occured, return here */
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200803 pinctrl_free(p, false);
Linus Walleij89216492012-12-11 14:14:32 +0100804 return ERR_PTR(ret);
805 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100806
Linus Walleijab780292013-01-22 10:56:14 -0700807 kref_init(&p->users);
808
Linus Walleijb0666ba2012-12-11 13:12:35 +0100809 /* Add the pinctrl handle to the global list */
Stanislaw Gruszka1fc42b82014-02-04 09:07:09 +0100810 mutex_lock(&pinctrl_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700811 list_add_tail(&p->node, &pinctrl_list);
Stanislaw Gruszka1fc42b82014-02-04 09:07:09 +0100812 mutex_unlock(&pinctrl_list_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100813
814 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700815}
Stephen Warren7ecdb162012-03-02 13:05:45 -0700816
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200817/**
818 * pinctrl_get() - retrieves the pinctrl handle for a device
819 * @dev: the device to obtain the handle for
820 */
821struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700822{
823 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700824
Stephen Warren6e5e9592012-03-02 13:05:47 -0700825 if (WARN_ON(!dev))
826 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700827
Linus Walleijab780292013-01-22 10:56:14 -0700828 /*
829 * See if somebody else (such as the device core) has already
830 * obtained a handle to the pinctrl for this device. In that case,
831 * return another pointer to it.
832 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700833 p = find_pinctrl(dev);
Linus Walleijab780292013-01-22 10:56:14 -0700834 if (p != NULL) {
835 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
836 kref_get(&p->users);
837 return p;
838 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700839
Richard Genoudd599bfb2012-08-10 16:53:49 +0200840 return create_pinctrl(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100841}
842EXPORT_SYMBOL_GPL(pinctrl_get);
843
Richard Genoudd3cee8302013-03-25 15:47:21 +0100844static void pinctrl_free_setting(bool disable_setting,
845 struct pinctrl_setting *setting)
846{
847 switch (setting->type) {
848 case PIN_MAP_TYPE_MUX_GROUP:
849 if (disable_setting)
850 pinmux_disable_setting(setting);
851 pinmux_free_setting(setting);
852 break;
853 case PIN_MAP_TYPE_CONFIGS_PIN:
854 case PIN_MAP_TYPE_CONFIGS_GROUP:
855 pinconf_free_setting(setting);
856 break;
857 default:
858 break;
859 }
860}
861
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200862static void pinctrl_free(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -0700863{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700864 struct pinctrl_state *state, *n1;
865 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700866
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200867 mutex_lock(&pinctrl_list_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700868 list_for_each_entry_safe(state, n1, &p->states, node) {
869 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Richard Genoudd3cee8302013-03-25 15:47:21 +0100870 pinctrl_free_setting(state == p->state, setting);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700871 list_del(&setting->node);
872 kfree(setting);
873 }
874 list_del(&state->node);
875 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700876 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700877
Stephen Warren57291ce2012-03-23 10:29:46 -0600878 pinctrl_dt_free_maps(p);
879
Stephen Warren6e5e9592012-03-02 13:05:47 -0700880 if (inlist)
881 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700882 kfree(p);
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200883 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700884}
885
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100886/**
Linus Walleijab780292013-01-22 10:56:14 -0700887 * pinctrl_release() - release the pinctrl handle
888 * @kref: the kref in the pinctrl being released
889 */
Sachin Kamat2917e832013-01-24 15:01:00 +0530890static void pinctrl_release(struct kref *kref)
Linus Walleijab780292013-01-22 10:56:14 -0700891{
892 struct pinctrl *p = container_of(kref, struct pinctrl, users);
893
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200894 pinctrl_free(p, true);
Linus Walleijab780292013-01-22 10:56:14 -0700895}
896
897/**
898 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
Stephen Warren6e5e9592012-03-02 13:05:47 -0700899 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100900 */
901void pinctrl_put(struct pinctrl *p)
902{
Linus Walleijab780292013-01-22 10:56:14 -0700903 kref_put(&p->users, pinctrl_release);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100904}
905EXPORT_SYMBOL_GPL(pinctrl_put);
906
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200907/**
908 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
909 * @p: the pinctrl handle to retrieve the state from
910 * @name: the state name to retrieve
911 */
912struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
913 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -0700914{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700915 struct pinctrl_state *state;
916
917 state = find_state(p, name);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800918 if (!state) {
919 if (pinctrl_dummy_state) {
920 /* create dummy state */
921 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
922 name);
923 state = create_state(p, name);
Richard Genoudd599bfb2012-08-10 16:53:49 +0200924 } else
925 state = ERR_PTR(-ENODEV);
Dong Aisheng5b3aa5f2012-04-26 16:15:50 +0800926 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700927
928 return state;
929}
Stephen Warren6e5e9592012-03-02 13:05:47 -0700930EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
931
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200932/**
933 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
934 * @p: the pinctrl handle for the device that requests configuration
935 * @state: the state handle to select/activate/program
936 */
937int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700938{
939 struct pinctrl_setting *setting, *setting2;
Richard Genoud50cf7c82013-03-25 15:47:23 +0100940 struct pinctrl_state *old_state = p->state;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700941 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700942
Stephen Warren6e5e9592012-03-02 13:05:47 -0700943 if (p->state == state)
944 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700945
Stephen Warren6e5e9592012-03-02 13:05:47 -0700946 if (p->state) {
947 /*
948 * The set of groups with a mux configuration in the old state
949 * may not be identical to the set of groups with a mux setting
950 * in the new state. While this might be unusual, it's entirely
951 * possible for the "user"-supplied mapping table to be written
952 * that way. For each group that was configured in the old state
953 * but not in the new state, this code puts that group into a
954 * safe/disabled state.
955 */
956 list_for_each_entry(setting, &p->state->settings, node) {
957 bool found = false;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700958 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
959 continue;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700960 list_for_each_entry(setting2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700961 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
962 continue;
963 if (setting2->data.mux.group ==
964 setting->data.mux.group) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700965 found = true;
966 break;
967 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700968 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700969 if (!found)
970 pinmux_disable_setting(setting);
971 }
972 }
973
Richard Genoud3102a762013-03-25 15:47:22 +0100974 p->state = NULL;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700975
976 /* Apply all the settings for the new state */
977 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700978 switch (setting->type) {
979 case PIN_MAP_TYPE_MUX_GROUP:
980 ret = pinmux_enable_setting(setting);
981 break;
982 case PIN_MAP_TYPE_CONFIGS_PIN:
983 case PIN_MAP_TYPE_CONFIGS_GROUP:
984 ret = pinconf_apply_setting(setting);
985 break;
986 default:
987 ret = -EINVAL;
988 break;
989 }
Richard Genoud3102a762013-03-25 15:47:22 +0100990
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200991 if (ret < 0) {
Richard Genoud3102a762013-03-25 15:47:22 +0100992 goto unapply_new_state;
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200993 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700994 }
995
Richard Genoud3102a762013-03-25 15:47:22 +0100996 p->state = state;
997
Stephen Warren7ecdb162012-03-02 13:05:45 -0700998 return 0;
Richard Genoud3102a762013-03-25 15:47:22 +0100999
1000unapply_new_state:
Richard Genoudda587512013-03-28 12:55:46 +01001001 dev_err(p->dev, "Error applying setting, reverse things back\n");
Richard Genoud3102a762013-03-25 15:47:22 +01001002
Richard Genoud3102a762013-03-25 15:47:22 +01001003 list_for_each_entry(setting2, &state->settings, node) {
1004 if (&setting2->node == &setting->node)
1005 break;
Richard Genoudaf606172013-03-29 10:03:26 +01001006 /*
1007 * All we can do here is pinmux_disable_setting.
1008 * That means that some pins are muxed differently now
1009 * than they were before applying the setting (We can't
1010 * "unmux a pin"!), but it's not a big deal since the pins
1011 * are free to be muxed by another apply_setting.
1012 */
1013 if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
1014 pinmux_disable_setting(setting2);
Richard Genoud3102a762013-03-25 15:47:22 +01001015 }
Richard Genoud8009d5f2013-03-28 12:55:47 +01001016
Richard Genoud385d9422013-03-29 10:03:27 +01001017 /* There's no infinite recursive loop here because p->state is NULL */
1018 if (old_state)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001019 pinctrl_select_state(p, old_state);
Stephen Warren6e5e9592012-03-02 13:05:47 -07001020
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001021 return ret;
1022}
Stephen Warren6e5e9592012-03-02 13:05:47 -07001023EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001024
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001025static void devm_pinctrl_release(struct device *dev, void *res)
1026{
1027 pinctrl_put(*(struct pinctrl **)res);
1028}
1029
1030/**
1031 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
1032 * @dev: the device to obtain the handle for
1033 *
1034 * If there is a need to explicitly destroy the returned struct pinctrl,
1035 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1036 */
1037struct pinctrl *devm_pinctrl_get(struct device *dev)
1038{
1039 struct pinctrl **ptr, *p;
1040
1041 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
1042 if (!ptr)
1043 return ERR_PTR(-ENOMEM);
1044
1045 p = pinctrl_get(dev);
1046 if (!IS_ERR(p)) {
1047 *ptr = p;
1048 devres_add(dev, ptr);
1049 } else {
1050 devres_free(ptr);
1051 }
1052
1053 return p;
1054}
1055EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1056
1057static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1058{
1059 struct pinctrl **p = res;
1060
1061 return *p == data;
1062}
1063
1064/**
1065 * devm_pinctrl_put() - Resource managed pinctrl_put()
1066 * @p: the pinctrl handle to release
1067 *
1068 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1069 * this function will not need to be called and the resource management
1070 * code will ensure that the resource is freed.
1071 */
1072void devm_pinctrl_put(struct pinctrl *p)
1073{
Jingoo Hana72149e2013-02-26 11:34:07 +09001074 WARN_ON(devres_release(p->dev, devm_pinctrl_release,
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001075 devm_pinctrl_match, p));
Stephen Warren6d4ca1f2012-04-16 10:51:00 -06001076}
1077EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1078
Stephen Warren57291ce2012-03-23 10:29:46 -06001079int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
1080 bool dup, bool locked)
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001081{
Stephen Warren1e2082b2012-03-02 13:05:48 -07001082 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001083 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001084
1085 pr_debug("add %d pinmux maps\n", num_maps);
1086
1087 /* First sanity check the new mapping */
1088 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001089 if (!maps[i].dev_name) {
1090 pr_err("failed to register map %s (%d): no device given\n",
1091 maps[i].name, i);
1092 return -EINVAL;
1093 }
1094
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001095 if (!maps[i].name) {
1096 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001097 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001098 return -EINVAL;
1099 }
1100
Stephen Warren1e2082b2012-03-02 13:05:48 -07001101 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1102 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001103 pr_err("failed to register map %s (%d): no pin control device given\n",
1104 maps[i].name, i);
1105 return -EINVAL;
1106 }
1107
Stephen Warren1e2082b2012-03-02 13:05:48 -07001108 switch (maps[i].type) {
1109 case PIN_MAP_TYPE_DUMMY_STATE:
1110 break;
1111 case PIN_MAP_TYPE_MUX_GROUP:
1112 ret = pinmux_validate_map(&maps[i], i);
1113 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001114 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001115 break;
1116 case PIN_MAP_TYPE_CONFIGS_PIN:
1117 case PIN_MAP_TYPE_CONFIGS_GROUP:
1118 ret = pinconf_validate_map(&maps[i], i);
1119 if (ret < 0)
Stephen Warrenfde04f42012-04-25 10:32:16 -06001120 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -07001121 break;
1122 default:
1123 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001124 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -07001125 return -EINVAL;
1126 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001127 }
1128
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001129 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
1130 if (!maps_node) {
1131 pr_err("failed to alloc struct pinctrl_maps\n");
1132 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001133 }
1134
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001135 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -06001136 if (dup) {
1137 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
1138 GFP_KERNEL);
1139 if (!maps_node->maps) {
1140 pr_err("failed to duplicate mapping table\n");
1141 kfree(maps_node);
1142 return -ENOMEM;
1143 }
1144 } else {
1145 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001146 }
1147
Stephen Warren57291ce2012-03-23 10:29:46 -06001148 if (!locked)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001149 mutex_lock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001150 list_add_tail(&maps_node->node, &pinctrl_maps);
Stephen Warren57291ce2012-03-23 10:29:46 -06001151 if (!locked)
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001152 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -07001153
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001154 return 0;
1155}
1156
Stephen Warren57291ce2012-03-23 10:29:46 -06001157/**
1158 * pinctrl_register_mappings() - register a set of pin controller mappings
1159 * @maps: the pincontrol mappings table to register. This should probably be
1160 * marked with __initdata so it can be discarded after boot. This
1161 * function will perform a shallow copy for the mapping entries.
1162 * @num_maps: the number of maps in the mapping table
1163 */
1164int pinctrl_register_mappings(struct pinctrl_map const *maps,
1165 unsigned num_maps)
1166{
1167 return pinctrl_register_map(maps, num_maps, true, false);
1168}
1169
1170void pinctrl_unregister_map(struct pinctrl_map const *map)
1171{
1172 struct pinctrl_maps *maps_node;
1173
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001174 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001175 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1176 if (maps_node->maps == map) {
1177 list_del(&maps_node->node);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001178 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001179 return;
1180 }
1181 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001182 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren57291ce2012-03-23 10:29:46 -06001183}
1184
Julien Delacou840a47b2012-12-10 14:47:33 +01001185/**
1186 * pinctrl_force_sleep() - turn a given controller device into sleep state
1187 * @pctldev: pin controller device
1188 */
1189int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1190{
1191 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
1192 return pinctrl_select_state(pctldev->p, pctldev->hog_sleep);
1193 return 0;
1194}
1195EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1196
1197/**
1198 * pinctrl_force_default() - turn a given controller device into default state
1199 * @pctldev: pin controller device
1200 */
1201int pinctrl_force_default(struct pinctrl_dev *pctldev)
1202{
1203 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
1204 return pinctrl_select_state(pctldev->p, pctldev->hog_default);
1205 return 0;
1206}
1207EXPORT_SYMBOL_GPL(pinctrl_force_default);
1208
Linus Walleij2744e8a2011-05-02 20:50:54 +02001209#ifdef CONFIG_DEBUG_FS
1210
1211static int pinctrl_pins_show(struct seq_file *s, void *what)
1212{
1213 struct pinctrl_dev *pctldev = s->private;
1214 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +09001215 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001216
1217 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001218
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001219 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001220
Chanho Park706e8522012-01-03 16:47:50 +09001221 /* The pin number can be retrived from the pin controller descriptor */
1222 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +02001223 struct pin_desc *desc;
1224
Chanho Park706e8522012-01-03 16:47:50 +09001225 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001226 desc = pin_desc_get(pctldev, pin);
1227 /* Pin space may be sparse */
1228 if (desc == NULL)
1229 continue;
1230
1231 seq_printf(s, "pin %d (%s) ", pin,
1232 desc->name ? desc->name : "unnamed");
1233
1234 /* Driver-specific info per pin */
1235 if (ops->pin_dbg_show)
1236 ops->pin_dbg_show(pctldev, s, pin);
1237
1238 seq_puts(s, "\n");
1239 }
1240
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001241 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001242
Linus Walleij2744e8a2011-05-02 20:50:54 +02001243 return 0;
1244}
1245
1246static int pinctrl_groups_show(struct seq_file *s, void *what)
1247{
1248 struct pinctrl_dev *pctldev = s->private;
1249 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301250 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001251
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001252 mutex_lock(&pctldev->mutex);
1253
Viresh Kumard1e90e92012-03-30 11:25:40 +05301254 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001255
Linus Walleij2744e8a2011-05-02 20:50:54 +02001256 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301257 while (selector < ngroups) {
Stephen Warrena5818a82011-10-19 16:19:25 -06001258 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001259 unsigned num_pins;
1260 const char *gname = ops->get_group_name(pctldev, selector);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001261 const char *pname;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001262 int ret;
1263 int i;
1264
1265 ret = ops->get_group_pins(pctldev, selector,
1266 &pins, &num_pins);
1267 if (ret)
1268 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1269 gname);
1270 else {
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001271 seq_printf(s, "group: %s\n", gname);
1272 for (i = 0; i < num_pins; i++) {
1273 pname = pin_get_name(pctldev, pins[i]);
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001274 if (WARN_ON(!pname)) {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001275 mutex_unlock(&pctldev->mutex);
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001276 return -EINVAL;
Wei Yongjunb4dd7842012-10-22 12:58:09 +08001277 }
Dong Aishengdcb5dbc2012-04-17 15:00:46 +08001278 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1279 }
1280 seq_puts(s, "\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001281 }
1282 selector++;
1283 }
1284
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001285 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001286
1287 return 0;
1288}
1289
1290static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1291{
1292 struct pinctrl_dev *pctldev = s->private;
1293 struct pinctrl_gpio_range *range = NULL;
1294
1295 seq_puts(s, "GPIO ranges handled:\n");
1296
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001297 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001298
Linus Walleij2744e8a2011-05-02 20:50:54 +02001299 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001300 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Linus Walleij75d66422011-11-16 09:58:51 +01001301 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1302 range->id, range->name,
1303 range->base, (range->base + range->npins - 1),
1304 range->pin_base,
1305 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001306 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001307
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001308 mutex_unlock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001309
1310 return 0;
1311}
1312
1313static int pinctrl_devices_show(struct seq_file *s, void *what)
1314{
1315 struct pinctrl_dev *pctldev;
1316
Linus Walleijae6b4d82011-10-19 18:14:33 +02001317 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001318
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001319 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001320
Linus Walleij2744e8a2011-05-02 20:50:54 +02001321 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1322 seq_printf(s, "%s ", pctldev->desc->name);
1323 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001324 seq_puts(s, "yes ");
1325 else
1326 seq_puts(s, "no ");
1327 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001328 seq_puts(s, "yes");
1329 else
1330 seq_puts(s, "no");
1331 seq_puts(s, "\n");
1332 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001333
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001334 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001335
1336 return 0;
1337}
1338
Stephen Warren1e2082b2012-03-02 13:05:48 -07001339static inline const char *map_type(enum pinctrl_map_type type)
1340{
1341 static const char * const names[] = {
1342 "INVALID",
1343 "DUMMY_STATE",
1344 "MUX_GROUP",
1345 "CONFIGS_PIN",
1346 "CONFIGS_GROUP",
1347 };
1348
1349 if (type >= ARRAY_SIZE(names))
1350 return "UNKNOWN";
1351
1352 return names[type];
1353}
1354
Stephen Warren3eedb432012-02-23 17:04:40 -07001355static int pinctrl_maps_show(struct seq_file *s, void *what)
1356{
1357 struct pinctrl_maps *maps_node;
1358 int i;
1359 struct pinctrl_map const *map;
1360
1361 seq_puts(s, "Pinctrl maps:\n");
1362
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001363 mutex_lock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001364 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001365 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1366 map->dev_name, map->name, map_type(map->type),
1367 map->type);
1368
1369 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1370 seq_printf(s, "controlling device %s\n",
1371 map->ctrl_dev_name);
1372
1373 switch (map->type) {
1374 case PIN_MAP_TYPE_MUX_GROUP:
1375 pinmux_show_map(s, map);
1376 break;
1377 case PIN_MAP_TYPE_CONFIGS_PIN:
1378 case PIN_MAP_TYPE_CONFIGS_GROUP:
1379 pinconf_show_map(s, map);
1380 break;
1381 default:
1382 break;
1383 }
1384
1385 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001386 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001387 mutex_unlock(&pinctrl_maps_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001388
1389 return 0;
1390}
1391
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001392static int pinctrl_show(struct seq_file *s, void *what)
1393{
1394 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001395 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001396 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001397
1398 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001399
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001400 mutex_lock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001401
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001402 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001403 seq_printf(s, "device: %s current state: %s\n",
1404 dev_name(p->dev),
1405 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001406
Stephen Warren6e5e9592012-03-02 13:05:47 -07001407 list_for_each_entry(state, &p->states, node) {
1408 seq_printf(s, " state: %s\n", state->name);
1409
1410 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001411 struct pinctrl_dev *pctldev = setting->pctldev;
1412
1413 seq_printf(s, " type: %s controller %s ",
1414 map_type(setting->type),
1415 pinctrl_dev_get_name(pctldev));
1416
1417 switch (setting->type) {
1418 case PIN_MAP_TYPE_MUX_GROUP:
1419 pinmux_show_setting(s, setting);
1420 break;
1421 case PIN_MAP_TYPE_CONFIGS_PIN:
1422 case PIN_MAP_TYPE_CONFIGS_GROUP:
1423 pinconf_show_setting(s, setting);
1424 break;
1425 default:
1426 break;
1427 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001428 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001429 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001430 }
1431
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001432 mutex_unlock(&pinctrl_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001433
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001434 return 0;
1435}
1436
Linus Walleij2744e8a2011-05-02 20:50:54 +02001437static int pinctrl_pins_open(struct inode *inode, struct file *file)
1438{
1439 return single_open(file, pinctrl_pins_show, inode->i_private);
1440}
1441
1442static int pinctrl_groups_open(struct inode *inode, struct file *file)
1443{
1444 return single_open(file, pinctrl_groups_show, inode->i_private);
1445}
1446
1447static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1448{
1449 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1450}
1451
1452static int pinctrl_devices_open(struct inode *inode, struct file *file)
1453{
1454 return single_open(file, pinctrl_devices_show, NULL);
1455}
1456
Stephen Warren3eedb432012-02-23 17:04:40 -07001457static int pinctrl_maps_open(struct inode *inode, struct file *file)
1458{
1459 return single_open(file, pinctrl_maps_show, NULL);
1460}
1461
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001462static int pinctrl_open(struct inode *inode, struct file *file)
1463{
1464 return single_open(file, pinctrl_show, NULL);
1465}
1466
Linus Walleij2744e8a2011-05-02 20:50:54 +02001467static const struct file_operations pinctrl_pins_ops = {
1468 .open = pinctrl_pins_open,
1469 .read = seq_read,
1470 .llseek = seq_lseek,
1471 .release = single_release,
1472};
1473
1474static const struct file_operations pinctrl_groups_ops = {
1475 .open = pinctrl_groups_open,
1476 .read = seq_read,
1477 .llseek = seq_lseek,
1478 .release = single_release,
1479};
1480
1481static const struct file_operations pinctrl_gpioranges_ops = {
1482 .open = pinctrl_gpioranges_open,
1483 .read = seq_read,
1484 .llseek = seq_lseek,
1485 .release = single_release,
1486};
1487
1488static const struct file_operations pinctrl_devices_ops = {
1489 .open = pinctrl_devices_open,
1490 .read = seq_read,
1491 .llseek = seq_lseek,
1492 .release = single_release,
1493};
1494
Stephen Warren3eedb432012-02-23 17:04:40 -07001495static const struct file_operations pinctrl_maps_ops = {
1496 .open = pinctrl_maps_open,
1497 .read = seq_read,
1498 .llseek = seq_lseek,
1499 .release = single_release,
1500};
1501
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001502static const struct file_operations pinctrl_ops = {
1503 .open = pinctrl_open,
1504 .read = seq_read,
1505 .llseek = seq_lseek,
1506 .release = single_release,
1507};
1508
Linus Walleij2744e8a2011-05-02 20:50:54 +02001509static struct dentry *debugfs_root;
1510
1511static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1512{
Tony Lindgren02157162012-01-20 08:17:22 -08001513 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001514
Stephen Warren51cd24e2011-12-09 16:59:05 -07001515 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001516 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001517 pctldev->device_root = device_root;
1518
Linus Walleij2744e8a2011-05-02 20:50:54 +02001519 if (IS_ERR(device_root) || !device_root) {
1520 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001521 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001522 return;
1523 }
1524 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1525 device_root, pctldev, &pinctrl_pins_ops);
1526 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1527 device_root, pctldev, &pinctrl_groups_ops);
1528 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1529 device_root, pctldev, &pinctrl_gpioranges_ops);
1530 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +02001531 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001532}
1533
Tony Lindgren02157162012-01-20 08:17:22 -08001534static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1535{
1536 debugfs_remove_recursive(pctldev->device_root);
1537}
1538
Linus Walleij2744e8a2011-05-02 20:50:54 +02001539static void pinctrl_init_debugfs(void)
1540{
1541 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1542 if (IS_ERR(debugfs_root) || !debugfs_root) {
1543 pr_warn("failed to create debugfs directory\n");
1544 debugfs_root = NULL;
1545 return;
1546 }
1547
1548 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1549 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001550 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1551 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001552 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1553 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001554}
1555
1556#else /* CONFIG_DEBUG_FS */
1557
1558static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1559{
1560}
1561
1562static void pinctrl_init_debugfs(void)
1563{
1564}
1565
Tony Lindgren02157162012-01-20 08:17:22 -08001566static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1567{
1568}
1569
Linus Walleij2744e8a2011-05-02 20:50:54 +02001570#endif
1571
Stephen Warrend26bc492012-03-16 14:54:25 -06001572static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1573{
1574 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1575
1576 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301577 !ops->get_groups_count ||
Stephen Warrend26bc492012-03-16 14:54:25 -06001578 !ops->get_group_name ||
1579 !ops->get_group_pins)
1580 return -EINVAL;
1581
Stephen Warren57291ce2012-03-23 10:29:46 -06001582 if (ops->dt_node_to_map && !ops->dt_free_map)
1583 return -EINVAL;
1584
Stephen Warrend26bc492012-03-16 14:54:25 -06001585 return 0;
1586}
1587
Linus Walleij2744e8a2011-05-02 20:50:54 +02001588/**
1589 * pinctrl_register() - register a pin controller device
1590 * @pctldesc: descriptor for this pin controller
1591 * @dev: parent device for this pin controller
1592 * @driver_data: private pin controller data for this pin controller
1593 */
1594struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1595 struct device *dev, void *driver_data)
1596{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001597 struct pinctrl_dev *pctldev;
1598 int ret;
1599
Devendra Nagada9aecb2012-06-16 23:43:16 +05301600 if (!pctldesc)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001601 return NULL;
Devendra Nagada9aecb2012-06-16 23:43:16 +05301602 if (!pctldesc->name)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001603 return NULL;
1604
Stephen Warren02f5b982012-02-22 14:26:00 -07001605 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001606 if (pctldev == NULL) {
1607 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001608 return NULL;
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001609 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001610
1611 /* Initialize pin control device struct */
1612 pctldev->owner = pctldesc->owner;
1613 pctldev->desc = pctldesc;
1614 pctldev->driver_data = driver_data;
1615 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001616 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001617 pctldev->dev = dev;
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001618 mutex_init(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001619
Stephen Warrend26bc492012-03-16 14:54:25 -06001620 /* check core ops for sanity */
Devendra Nagada9aecb2012-06-16 23:43:16 +05301621 if (pinctrl_check_ops(pctldev)) {
John Crispinad6e1102012-04-26 16:47:11 +02001622 dev_err(dev, "pinctrl ops lacks necessary functions\n");
Stephen Warrend26bc492012-03-16 14:54:25 -06001623 goto out_err;
1624 }
1625
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001626 /* If we're implementing pinmuxing, check the ops for sanity */
1627 if (pctldesc->pmxops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301628 if (pinmux_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001629 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001630 }
1631
1632 /* If we're implementing pinconfig, check the ops for sanity */
1633 if (pctldesc->confops) {
Devendra Nagada9aecb2012-06-16 23:43:16 +05301634 if (pinconf_check_ops(pctldev))
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001635 goto out_err;
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001636 }
1637
Linus Walleij2744e8a2011-05-02 20:50:54 +02001638 /* Register all the pins */
John Crispinad6e1102012-04-26 16:47:11 +02001639 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001640 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1641 if (ret) {
John Crispinad6e1102012-04-26 16:47:11 +02001642 dev_err(dev, "error during pin registration\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001643 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1644 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001645 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001646 }
1647
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001648 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -07001649 list_add_tail(&pctldev->node, &pinctrldev_list);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001650 mutex_unlock(&pinctrldev_list_mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001651
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001652 pctldev->p = pinctrl_get(pctldev->dev);
1653
Stephen Warren6e5e9592012-03-02 13:05:47 -07001654 if (!IS_ERR(pctldev->p)) {
Julien Delacou840a47b2012-12-10 14:47:33 +01001655 pctldev->hog_default =
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001656 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
Julien Delacou840a47b2012-12-10 14:47:33 +01001657 if (IS_ERR(pctldev->hog_default)) {
John Crispinad6e1102012-04-26 16:47:11 +02001658 dev_dbg(dev, "failed to lookup the default state\n");
1659 } else {
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001660 if (pinctrl_select_state(pctldev->p,
Julien Delacou840a47b2012-12-10 14:47:33 +01001661 pctldev->hog_default))
John Crispinad6e1102012-04-26 16:47:11 +02001662 dev_err(dev,
1663 "failed to select default state\n");
John Crispinad6e1102012-04-26 16:47:11 +02001664 }
Julien Delacou840a47b2012-12-10 14:47:33 +01001665
1666 pctldev->hog_sleep =
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001667 pinctrl_lookup_state(pctldev->p,
Julien Delacou840a47b2012-12-10 14:47:33 +01001668 PINCTRL_STATE_SLEEP);
1669 if (IS_ERR(pctldev->hog_sleep))
1670 dev_dbg(dev, "failed to lookup the sleep state\n");
Stephen Warren6e5e9592012-03-02 13:05:47 -07001671 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001672
Stephen Warren2304b472012-02-22 14:26:01 -07001673 pinctrl_init_device_debugfs(pctldev);
1674
Linus Walleij2744e8a2011-05-02 20:50:54 +02001675 return pctldev;
1676
Stephen Warren51cd24e2011-12-09 16:59:05 -07001677out_err:
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001678 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001679 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001680 return NULL;
1681}
1682EXPORT_SYMBOL_GPL(pinctrl_register);
1683
1684/**
1685 * pinctrl_unregister() - unregister pinmux
1686 * @pctldev: pin controller to unregister
1687 *
1688 * Called by pinmux drivers to unregister a pinmux.
1689 */
1690void pinctrl_unregister(struct pinctrl_dev *pctldev)
1691{
Dong Aisheng5d589b02012-05-23 21:22:40 +08001692 struct pinctrl_gpio_range *range, *n;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001693 if (pctldev == NULL)
1694 return;
1695
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001696 mutex_lock(&pctldev->mutex);
Tony Lindgren02157162012-01-20 08:17:22 -08001697 pinctrl_remove_device_debugfs(pctldev);
Jim Lina01bbd72015-01-08 20:25:05 +08001698 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -07001699
Stephen Warren6e5e9592012-03-02 13:05:47 -07001700 if (!IS_ERR(pctldev->p))
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001701 pinctrl_put(pctldev->p);
Stephen Warren57b676f2012-03-02 13:05:44 -07001702
Jim Lina01bbd72015-01-08 20:25:05 +08001703 mutex_lock(&pinctrldev_list_mutex);
1704 mutex_lock(&pctldev->mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001705 /* TODO: check that no pinmuxes are still active? */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001706 list_del(&pctldev->node);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001707 /* Destroy descriptor tree */
1708 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1709 pctldev->desc->npins);
Dong Aisheng5d589b02012-05-23 21:22:40 +08001710 /* remove gpio ranges map */
1711 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1712 list_del(&range->node);
1713
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001714 mutex_unlock(&pctldev->mutex);
1715 mutex_destroy(&pctldev->mutex);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001716 kfree(pctldev);
Patrice Chotard42fed7b2013-04-11 11:01:27 +02001717 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001718}
1719EXPORT_SYMBOL_GPL(pinctrl_unregister);
1720
1721static int __init pinctrl_init(void)
1722{
1723 pr_info("initialized pinctrl subsystem\n");
1724 pinctrl_init_debugfs();
1725 return 0;
1726}
1727
1728/* init early since many drivers really need to initialized pinmux early */
1729core_initcall(pinctrl_init);