blob: 2eaa1876534b24ddc2730393228924fd015e8a47 [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>
Stephen Rothwella5a697c2011-09-30 14:39:04 +100017#include <linux/export.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020018#include <linux/init.h>
19#include <linux/device.h>
20#include <linux/slab.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020021#include <linux/err.h>
22#include <linux/list.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020023#include <linux/sysfs.h>
24#include <linux/debugfs.h>
25#include <linux/seq_file.h>
Stephen Warren6d4ca1f2012-04-16 10:51:00 -060026#include <linux/pinctrl/consumer.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020027#include <linux/pinctrl/pinctrl.h>
28#include <linux/pinctrl/machine.h>
29#include "core.h"
Stephen Warren57291ce2012-03-23 10:29:46 -060030#include "devicetree.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020031#include "pinmux.h"
Linus Walleijae6b4d82011-10-19 18:14:33 +020032#include "pinconf.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020033
Linus Walleijbefe5bd2012-02-09 19:47:48 +010034/**
Stephen Warrenb2b3e662012-02-19 23:45:43 -070035 * struct pinctrl_maps - a list item containing part of the mapping table
36 * @node: mapping table list node
37 * @maps: array of mapping table entries
38 * @num_maps: the number of entries in @maps
39 */
40struct pinctrl_maps {
41 struct list_head node;
42 struct pinctrl_map const *maps;
43 unsigned num_maps;
44};
45
Stephen Warren57b676f2012-03-02 13:05:44 -070046/* Mutex taken by all entry points */
47DEFINE_MUTEX(pinctrl_mutex);
48
49/* Global list of pin control devices (struct pinctrl_dev) */
Stephen Warren57291ce2012-03-23 10:29:46 -060050LIST_HEAD(pinctrldev_list);
Linus Walleij2744e8a2011-05-02 20:50:54 +020051
Stephen Warren57b676f2012-03-02 13:05:44 -070052/* List of pin controller handles (struct pinctrl) */
Linus Walleijbefe5bd2012-02-09 19:47:48 +010053static LIST_HEAD(pinctrl_list);
54
Stephen Warren57b676f2012-03-02 13:05:44 -070055/* List of pinctrl maps (struct pinctrl_maps) */
Stephen Warrenb2b3e662012-02-19 23:45:43 -070056static LIST_HEAD(pinctrl_maps);
57
58#define for_each_maps(_maps_node_, _i_, _map_) \
59 list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
60 for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
61 _i_ < _maps_node_->num_maps; \
62 i++, _map_ = &_maps_node_->maps[_i_])
Linus Walleijbefe5bd2012-02-09 19:47:48 +010063
Linus Walleij2744e8a2011-05-02 20:50:54 +020064const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
65{
66 /* We're not allowed to register devices without name */
67 return pctldev->desc->name;
68}
69EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
70
71void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
72{
73 return pctldev->driver_data;
74}
75EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
76
77/**
Linus Walleij9dfac4f2012-02-01 18:02:47 +010078 * get_pinctrl_dev_from_devname() - look up pin controller device
79 * @devname: the name of a device instance, as returned by dev_name()
Linus Walleij2744e8a2011-05-02 20:50:54 +020080 *
81 * Looks up a pin control device matching a certain device name or pure device
82 * pointer, the pure device pointer will take precedence.
83 */
Linus Walleij9dfac4f2012-02-01 18:02:47 +010084struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
Linus Walleij2744e8a2011-05-02 20:50:54 +020085{
86 struct pinctrl_dev *pctldev = NULL;
87 bool found = false;
88
Linus Walleij9dfac4f2012-02-01 18:02:47 +010089 if (!devname)
90 return NULL;
91
Linus Walleij2744e8a2011-05-02 20:50:54 +020092 list_for_each_entry(pctldev, &pinctrldev_list, node) {
Linus Walleij9dfac4f2012-02-01 18:02:47 +010093 if (!strcmp(dev_name(pctldev->dev), devname)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +020094 /* Matched on device name */
95 found = true;
96 break;
97 }
98 }
Linus Walleij2744e8a2011-05-02 20:50:54 +020099
100 return found ? pctldev : NULL;
101}
102
Linus Walleij2744e8a2011-05-02 20:50:54 +0200103/**
Linus Walleijae6b4d82011-10-19 18:14:33 +0200104 * pin_get_from_name() - look up a pin number from a name
105 * @pctldev: the pin control device to lookup the pin on
106 * @name: the name of the pin to look up
107 */
108int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
109{
Chanho Park706e8522012-01-03 16:47:50 +0900110 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200111
Chanho Park706e8522012-01-03 16:47:50 +0900112 /* The pin number can be retrived from the pin controller descriptor */
113 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200114 struct pin_desc *desc;
115
Chanho Park706e8522012-01-03 16:47:50 +0900116 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200117 desc = pin_desc_get(pctldev, pin);
118 /* Pin space may be sparse */
119 if (desc == NULL)
120 continue;
121 if (desc->name && !strcmp(name, desc->name))
122 return pin;
123 }
124
125 return -EINVAL;
126}
127
128/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200129 * pin_is_valid() - check if pin exists on controller
130 * @pctldev: the pin control device to check the pin on
131 * @pin: pin to check, use the local pin controller index number
132 *
133 * This tells us whether a certain pin exist on a certain pin controller or
134 * not. Pin lists may be sparse, so some pins may not exist.
135 */
136bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
137{
138 struct pin_desc *pindesc;
139
140 if (pin < 0)
141 return false;
142
Stephen Warren57b676f2012-03-02 13:05:44 -0700143 mutex_lock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200144 pindesc = pin_desc_get(pctldev, pin);
Stephen Warren57b676f2012-03-02 13:05:44 -0700145 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200146
Stephen Warren57b676f2012-03-02 13:05:44 -0700147 return pindesc != NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200148}
149EXPORT_SYMBOL_GPL(pin_is_valid);
150
151/* Deletes a range of pin descriptors */
152static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
153 const struct pinctrl_pin_desc *pins,
154 unsigned num_pins)
155{
156 int i;
157
Linus Walleij2744e8a2011-05-02 20:50:54 +0200158 for (i = 0; i < num_pins; i++) {
159 struct pin_desc *pindesc;
160
161 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
162 pins[i].number);
163 if (pindesc != NULL) {
164 radix_tree_delete(&pctldev->pin_desc_tree,
165 pins[i].number);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100166 if (pindesc->dynamic_name)
167 kfree(pindesc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200168 }
169 kfree(pindesc);
170 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200171}
172
173static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
174 unsigned number, const char *name)
175{
176 struct pin_desc *pindesc;
177
178 pindesc = pin_desc_get(pctldev, number);
179 if (pindesc != NULL) {
180 pr_err("pin %d already registered on %s\n", number,
181 pctldev->desc->name);
182 return -EINVAL;
183 }
184
185 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700186 if (pindesc == NULL) {
187 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200188 return -ENOMEM;
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700189 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200190
Linus Walleij2744e8a2011-05-02 20:50:54 +0200191 /* Set owner */
192 pindesc->pctldev = pctldev;
193
Stephen Warren9af1e442011-10-19 16:19:27 -0600194 /* Copy basic pin info */
Linus Walleij8dc6ae42012-02-01 18:11:40 +0100195 if (name) {
Linus Walleijca53c5f2011-12-14 20:33:37 +0100196 pindesc->name = name;
197 } else {
198 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
199 if (pindesc->name == NULL)
200 return -ENOMEM;
201 pindesc->dynamic_name = true;
202 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200203
Linus Walleij2744e8a2011-05-02 20:50:54 +0200204 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200205 pr_debug("registered pin %d (%s) on %s\n",
Linus Walleijca53c5f2011-12-14 20:33:37 +0100206 number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200207 return 0;
208}
209
210static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
211 struct pinctrl_pin_desc const *pins,
212 unsigned num_descs)
213{
214 unsigned i;
215 int ret = 0;
216
217 for (i = 0; i < num_descs; i++) {
218 ret = pinctrl_register_one_pin(pctldev,
219 pins[i].number, pins[i].name);
220 if (ret)
221 return ret;
222 }
223
224 return 0;
225}
226
227/**
228 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
229 * @pctldev: pin controller device to check
230 * @gpio: gpio pin to check taken from the global GPIO pin space
231 *
232 * Tries to match a GPIO pin number to the ranges handled by a certain pin
233 * controller, return the range or NULL
234 */
235static struct pinctrl_gpio_range *
236pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
237{
238 struct pinctrl_gpio_range *range = NULL;
239
240 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200241 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
242 /* Check if we're in the valid range */
243 if (gpio >= range->base &&
244 gpio < range->base + range->npins) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200245 return range;
246 }
247 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200248
249 return NULL;
250}
251
252/**
253 * pinctrl_get_device_gpio_range() - find device for GPIO range
254 * @gpio: the pin to locate the pin controller for
255 * @outdev: the pin control device if found
256 * @outrange: the GPIO range if found
257 *
258 * Find the pin controller handling a certain GPIO pin from the pinspace of
259 * the GPIO subsystem, return the device and the matching GPIO range. Returns
260 * negative if the GPIO range could not be found in any device.
261 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700262static int pinctrl_get_device_gpio_range(unsigned gpio,
263 struct pinctrl_dev **outdev,
264 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200265{
266 struct pinctrl_dev *pctldev = NULL;
267
268 /* Loop over the pin controllers */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200269 list_for_each_entry(pctldev, &pinctrldev_list, node) {
270 struct pinctrl_gpio_range *range;
271
272 range = pinctrl_match_gpio_range(pctldev, gpio);
273 if (range != NULL) {
274 *outdev = pctldev;
275 *outrange = range;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200276 return 0;
277 }
278 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200279
280 return -EINVAL;
281}
282
283/**
284 * pinctrl_add_gpio_range() - register a GPIO range for a controller
285 * @pctldev: pin controller device to add the range to
286 * @range: the GPIO range to add
287 *
288 * This adds a range of GPIOs to be handled by a certain pin controller. Call
289 * this to register handled ranges after registering your pin controller.
290 */
291void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
292 struct pinctrl_gpio_range *range)
293{
Stephen Warren57b676f2012-03-02 13:05:44 -0700294 mutex_lock(&pinctrl_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700295 list_add_tail(&range->node, &pctldev->gpio_ranges);
Stephen Warren57b676f2012-03-02 13:05:44 -0700296 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200297}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700298EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200299
300/**
301 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
302 * @pctldev: pin controller device to remove the range from
303 * @range: the GPIO range to remove
304 */
305void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
306 struct pinctrl_gpio_range *range)
307{
Stephen Warren57b676f2012-03-02 13:05:44 -0700308 mutex_lock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200309 list_del(&range->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700310 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200311}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700312EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200313
Linus Walleij7afde8b2011-10-19 17:07:16 +0200314/**
315 * pinctrl_get_group_selector() - returns the group selector for a group
316 * @pctldev: the pin controller handling the group
317 * @pin_group: the pin group to look up
318 */
319int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
320 const char *pin_group)
321{
322 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530323 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200324 unsigned group_selector = 0;
325
Viresh Kumard1e90e92012-03-30 11:25:40 +0530326 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200327 const char *gname = pctlops->get_group_name(pctldev,
328 group_selector);
329 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700330 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200331 "found group selector %u for %s\n",
332 group_selector,
333 pin_group);
334 return group_selector;
335 }
336
337 group_selector++;
338 }
339
Stephen Warren51cd24e2011-12-09 16:59:05 -0700340 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200341 pin_group);
342
343 return -EINVAL;
344}
345
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100346/**
347 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
348 * @gpio: the GPIO pin number from the GPIO subsystem number space
349 *
350 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
351 * as part of their gpio_request() semantics, platforms and individual drivers
352 * shall *NOT* request GPIO pins to be muxed in.
353 */
354int pinctrl_request_gpio(unsigned gpio)
355{
356 struct pinctrl_dev *pctldev;
357 struct pinctrl_gpio_range *range;
358 int ret;
359 int pin;
360
Stephen Warren57b676f2012-03-02 13:05:44 -0700361 mutex_lock(&pinctrl_mutex);
362
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100363 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700364 if (ret) {
365 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100366 return -EINVAL;
Stephen Warren57b676f2012-03-02 13:05:44 -0700367 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100368
369 /* Convert to the pin controllers number space */
370 pin = gpio - range->base + range->pin_base;
371
Stephen Warren57b676f2012-03-02 13:05:44 -0700372 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
373
374 mutex_unlock(&pinctrl_mutex);
375 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100376}
377EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
378
379/**
380 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
381 * @gpio: the GPIO pin number from the GPIO subsystem number space
382 *
383 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
384 * as part of their gpio_free() semantics, platforms and individual drivers
385 * shall *NOT* request GPIO pins to be muxed out.
386 */
387void pinctrl_free_gpio(unsigned gpio)
388{
389 struct pinctrl_dev *pctldev;
390 struct pinctrl_gpio_range *range;
391 int ret;
392 int pin;
393
Stephen Warren57b676f2012-03-02 13:05:44 -0700394 mutex_lock(&pinctrl_mutex);
395
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100396 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700397 if (ret) {
398 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100399 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700400 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100401
402 /* Convert to the pin controllers number space */
403 pin = gpio - range->base + range->pin_base;
404
Stephen Warren57b676f2012-03-02 13:05:44 -0700405 pinmux_free_gpio(pctldev, pin, range);
406
407 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100408}
409EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
410
411static int pinctrl_gpio_direction(unsigned gpio, bool input)
412{
413 struct pinctrl_dev *pctldev;
414 struct pinctrl_gpio_range *range;
415 int ret;
416 int pin;
417
418 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
419 if (ret)
420 return ret;
421
422 /* Convert to the pin controllers number space */
423 pin = gpio - range->base + range->pin_base;
424
425 return pinmux_gpio_direction(pctldev, range, pin, input);
426}
427
428/**
429 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
430 * @gpio: the GPIO pin number from the GPIO subsystem number space
431 *
432 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
433 * as part of their gpio_direction_input() semantics, platforms and individual
434 * drivers shall *NOT* touch pin control GPIO calls.
435 */
436int pinctrl_gpio_direction_input(unsigned gpio)
437{
Stephen Warren57b676f2012-03-02 13:05:44 -0700438 int ret;
439 mutex_lock(&pinctrl_mutex);
440 ret = pinctrl_gpio_direction(gpio, true);
441 mutex_unlock(&pinctrl_mutex);
442 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100443}
444EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
445
446/**
447 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
448 * @gpio: the GPIO pin number from the GPIO subsystem number space
449 *
450 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
451 * as part of their gpio_direction_output() semantics, platforms and individual
452 * drivers shall *NOT* touch pin control GPIO calls.
453 */
454int pinctrl_gpio_direction_output(unsigned gpio)
455{
Stephen Warren57b676f2012-03-02 13:05:44 -0700456 int ret;
457 mutex_lock(&pinctrl_mutex);
458 ret = pinctrl_gpio_direction(gpio, false);
459 mutex_unlock(&pinctrl_mutex);
460 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100461}
462EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
463
Stephen Warren6e5e9592012-03-02 13:05:47 -0700464static struct pinctrl_state *find_state(struct pinctrl *p,
465 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100466{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700467 struct pinctrl_state *state;
468
469 list_for_each_entry(state, &p->states, node)
470 if (!strcmp(state->name, name))
471 return state;
472
473 return NULL;
474}
475
476static struct pinctrl_state *create_state(struct pinctrl *p,
477 const char *name)
478{
479 struct pinctrl_state *state;
480
481 state = kzalloc(sizeof(*state), GFP_KERNEL);
482 if (state == NULL) {
483 dev_err(p->dev,
484 "failed to alloc struct pinctrl_state\n");
485 return ERR_PTR(-ENOMEM);
486 }
487
488 state->name = name;
489 INIT_LIST_HEAD(&state->settings);
490
491 list_add_tail(&state->node, &p->states);
492
493 return state;
494}
495
496static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
497{
498 struct pinctrl_state *state;
499 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700500 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700501
502 state = find_state(p, map->name);
503 if (!state)
504 state = create_state(p, map->name);
505 if (IS_ERR(state))
506 return PTR_ERR(state);
507
Stephen Warren1e2082b2012-03-02 13:05:48 -0700508 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
509 return 0;
510
Stephen Warren6e5e9592012-03-02 13:05:47 -0700511 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
512 if (setting == NULL) {
513 dev_err(p->dev,
514 "failed to alloc struct pinctrl_setting\n");
515 return -ENOMEM;
516 }
517
Stephen Warren1e2082b2012-03-02 13:05:48 -0700518 setting->type = map->type;
519
Stephen Warren6e5e9592012-03-02 13:05:47 -0700520 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
521 if (setting->pctldev == NULL) {
Linus Walleijc05127c2012-04-10 10:00:38 +0200522 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
Stephen Warren6e5e9592012-03-02 13:05:47 -0700523 map->ctrl_dev_name);
524 kfree(setting);
Linus Walleijc05127c2012-04-10 10:00:38 +0200525 /*
526 * OK let us guess that the driver is not there yet, and
527 * let's defer obtaining this pinctrl handle to later...
528 */
529 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700530 }
531
Stephen Warren1e2082b2012-03-02 13:05:48 -0700532 switch (map->type) {
533 case PIN_MAP_TYPE_MUX_GROUP:
534 ret = pinmux_map_to_setting(map, setting);
535 break;
536 case PIN_MAP_TYPE_CONFIGS_PIN:
537 case PIN_MAP_TYPE_CONFIGS_GROUP:
538 ret = pinconf_map_to_setting(map, setting);
539 break;
540 default:
541 ret = -EINVAL;
542 break;
543 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700544 if (ret < 0) {
545 kfree(setting);
546 return ret;
547 }
548
549 list_add_tail(&setting->node, &state->settings);
550
551 return 0;
552}
553
554static struct pinctrl *find_pinctrl(struct device *dev)
555{
556 struct pinctrl *p;
557
Stephen Warren1e2082b2012-03-02 13:05:48 -0700558 list_for_each_entry(p, &pinctrl_list, node)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700559 if (p->dev == dev)
560 return p;
561
562 return NULL;
563}
564
565static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
566
567static struct pinctrl *create_pinctrl(struct device *dev)
568{
569 struct pinctrl *p;
570 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700571 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100572 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700573 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700574 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100575
576 /*
577 * create the state cookie holder struct pinctrl for each
578 * mapping, this is what consumers will get when requesting
579 * a pin control handle with pinctrl_get()
580 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700581 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700582 if (p == NULL) {
583 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100584 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700585 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700586 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700587 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -0600588 INIT_LIST_HEAD(&p->dt_maps);
589
590 ret = pinctrl_dt_to_map(p);
591 if (ret < 0) {
592 kfree(p);
593 return ERR_PTR(ret);
594 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700595
596 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100597
598 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700599 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -0700600 /* Map must be for this device */
601 if (strcmp(map->dev_name, devname))
602 continue;
603
Stephen Warren6e5e9592012-03-02 13:05:47 -0700604 ret = add_setting(p, map);
605 if (ret < 0) {
606 pinctrl_put_locked(p, false);
607 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100608 }
609 }
610
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100611 /* Add the pinmux to the global list */
Stephen Warren8b9c1392012-02-19 23:45:42 -0700612 list_add_tail(&p->node, &pinctrl_list);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100613
614 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700615}
Stephen Warren7ecdb162012-03-02 13:05:45 -0700616
Stephen Warren6e5e9592012-03-02 13:05:47 -0700617static struct pinctrl *pinctrl_get_locked(struct device *dev)
618{
619 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700620
Stephen Warren6e5e9592012-03-02 13:05:47 -0700621 if (WARN_ON(!dev))
622 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700623
Stephen Warren6e5e9592012-03-02 13:05:47 -0700624 p = find_pinctrl(dev);
625 if (p != NULL)
626 return ERR_PTR(-EBUSY);
627
628 p = create_pinctrl(dev);
629 if (IS_ERR(p))
630 return p;
631
632 return p;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100633}
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700634
635/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700636 * pinctrl_get() - retrieves the pinctrl handle for a device
637 * @dev: the device to obtain the handle for
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700638 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700639struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700640{
641 struct pinctrl *p;
642
Stephen Warren57b676f2012-03-02 13:05:44 -0700643 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700644 p = pinctrl_get_locked(dev);
Stephen Warren57b676f2012-03-02 13:05:44 -0700645 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700646
647 return p;
648}
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100649EXPORT_SYMBOL_GPL(pinctrl_get);
650
Stephen Warren6e5e9592012-03-02 13:05:47 -0700651static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -0700652{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700653 struct pinctrl_state *state, *n1;
654 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700655
Stephen Warren6e5e9592012-03-02 13:05:47 -0700656 list_for_each_entry_safe(state, n1, &p->states, node) {
657 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700658 switch (setting->type) {
659 case PIN_MAP_TYPE_MUX_GROUP:
660 if (state == p->state)
661 pinmux_disable_setting(setting);
662 pinmux_free_setting(setting);
663 break;
664 case PIN_MAP_TYPE_CONFIGS_PIN:
665 case PIN_MAP_TYPE_CONFIGS_GROUP:
666 pinconf_free_setting(setting);
667 break;
668 default:
669 break;
670 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700671 list_del(&setting->node);
672 kfree(setting);
673 }
674 list_del(&state->node);
675 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700676 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700677
Stephen Warren57291ce2012-03-23 10:29:46 -0600678 pinctrl_dt_free_maps(p);
679
Stephen Warren6e5e9592012-03-02 13:05:47 -0700680 if (inlist)
681 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700682 kfree(p);
683}
684
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100685/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700686 * pinctrl_put() - release a previously claimed pinctrl handle
687 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100688 */
689void pinctrl_put(struct pinctrl *p)
690{
Stephen Warren57b676f2012-03-02 13:05:44 -0700691 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700692 pinctrl_put_locked(p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -0700693 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100694}
695EXPORT_SYMBOL_GPL(pinctrl_put);
696
Stephen Warren6e5e9592012-03-02 13:05:47 -0700697static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
698 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -0700699{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700700 struct pinctrl_state *state;
701
702 state = find_state(p, name);
703 if (!state)
704 return ERR_PTR(-ENODEV);
705
706 return state;
707}
708
709/**
710 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
711 * @p: the pinctrl handle to retrieve the state from
712 * @name: the state name to retrieve
713 */
714struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
715{
716 struct pinctrl_state *s;
717
718 mutex_lock(&pinctrl_mutex);
719 s = pinctrl_lookup_state_locked(p, name);
720 mutex_unlock(&pinctrl_mutex);
721
722 return s;
723}
724EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
725
726static int pinctrl_select_state_locked(struct pinctrl *p,
727 struct pinctrl_state *state)
728{
729 struct pinctrl_setting *setting, *setting2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700730 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700731
Stephen Warren6e5e9592012-03-02 13:05:47 -0700732 if (p->state == state)
733 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700734
Stephen Warren6e5e9592012-03-02 13:05:47 -0700735 if (p->state) {
736 /*
737 * The set of groups with a mux configuration in the old state
738 * may not be identical to the set of groups with a mux setting
739 * in the new state. While this might be unusual, it's entirely
740 * possible for the "user"-supplied mapping table to be written
741 * that way. For each group that was configured in the old state
742 * but not in the new state, this code puts that group into a
743 * safe/disabled state.
744 */
745 list_for_each_entry(setting, &p->state->settings, node) {
746 bool found = false;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700747 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
748 continue;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700749 list_for_each_entry(setting2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700750 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
751 continue;
752 if (setting2->data.mux.group ==
753 setting->data.mux.group) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700754 found = true;
755 break;
756 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700757 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700758 if (!found)
759 pinmux_disable_setting(setting);
760 }
761 }
762
763 p->state = state;
764
765 /* Apply all the settings for the new state */
766 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700767 switch (setting->type) {
768 case PIN_MAP_TYPE_MUX_GROUP:
769 ret = pinmux_enable_setting(setting);
770 break;
771 case PIN_MAP_TYPE_CONFIGS_PIN:
772 case PIN_MAP_TYPE_CONFIGS_GROUP:
773 ret = pinconf_apply_setting(setting);
774 break;
775 default:
776 ret = -EINVAL;
777 break;
778 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700779 if (ret < 0) {
780 /* FIXME: Difficult to return to prev state */
781 return ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700782 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700783 }
784
Stephen Warren7ecdb162012-03-02 13:05:45 -0700785 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700786}
787
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100788/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700789 * pinctrl_select() - select/activate/program a pinctrl state to HW
790 * @p: the pinctrl handle for the device that requests configuratio
791 * @state: the state handle to select/activate/program
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100792 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700793int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100794{
Stephen Warren57b676f2012-03-02 13:05:44 -0700795 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700796
Stephen Warren57b676f2012-03-02 13:05:44 -0700797 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700798 ret = pinctrl_select_state_locked(p, state);
Stephen Warren57b676f2012-03-02 13:05:44 -0700799 mutex_unlock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700800
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100801 return ret;
802}
Stephen Warren6e5e9592012-03-02 13:05:47 -0700803EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100804
Stephen Warren6d4ca1f2012-04-16 10:51:00 -0600805static void devm_pinctrl_release(struct device *dev, void *res)
806{
807 pinctrl_put(*(struct pinctrl **)res);
808}
809
810/**
811 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
812 * @dev: the device to obtain the handle for
813 *
814 * If there is a need to explicitly destroy the returned struct pinctrl,
815 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
816 */
817struct pinctrl *devm_pinctrl_get(struct device *dev)
818{
819 struct pinctrl **ptr, *p;
820
821 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
822 if (!ptr)
823 return ERR_PTR(-ENOMEM);
824
825 p = pinctrl_get(dev);
826 if (!IS_ERR(p)) {
827 *ptr = p;
828 devres_add(dev, ptr);
829 } else {
830 devres_free(ptr);
831 }
832
833 return p;
834}
835EXPORT_SYMBOL_GPL(devm_pinctrl_get);
836
837static int devm_pinctrl_match(struct device *dev, void *res, void *data)
838{
839 struct pinctrl **p = res;
840
841 return *p == data;
842}
843
844/**
845 * devm_pinctrl_put() - Resource managed pinctrl_put()
846 * @p: the pinctrl handle to release
847 *
848 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
849 * this function will not need to be called and the resource management
850 * code will ensure that the resource is freed.
851 */
852void devm_pinctrl_put(struct pinctrl *p)
853{
854 WARN_ON(devres_destroy(p->dev, devm_pinctrl_release,
855 devm_pinctrl_match, p));
856 pinctrl_put(p);
857}
858EXPORT_SYMBOL_GPL(devm_pinctrl_put);
859
Stephen Warren57291ce2012-03-23 10:29:46 -0600860int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
861 bool dup, bool locked)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100862{
Stephen Warren1e2082b2012-03-02 13:05:48 -0700863 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700864 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100865
866 pr_debug("add %d pinmux maps\n", num_maps);
867
868 /* First sanity check the new mapping */
869 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700870 if (!maps[i].dev_name) {
871 pr_err("failed to register map %s (%d): no device given\n",
872 maps[i].name, i);
873 return -EINVAL;
874 }
875
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100876 if (!maps[i].name) {
877 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700878 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100879 return -EINVAL;
880 }
881
Stephen Warren1e2082b2012-03-02 13:05:48 -0700882 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
883 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100884 pr_err("failed to register map %s (%d): no pin control device given\n",
885 maps[i].name, i);
886 return -EINVAL;
887 }
888
Stephen Warren1e2082b2012-03-02 13:05:48 -0700889 switch (maps[i].type) {
890 case PIN_MAP_TYPE_DUMMY_STATE:
891 break;
892 case PIN_MAP_TYPE_MUX_GROUP:
893 ret = pinmux_validate_map(&maps[i], i);
894 if (ret < 0)
895 return 0;
896 break;
897 case PIN_MAP_TYPE_CONFIGS_PIN:
898 case PIN_MAP_TYPE_CONFIGS_GROUP:
899 ret = pinconf_validate_map(&maps[i], i);
900 if (ret < 0)
901 return 0;
902 break;
903 default:
904 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700905 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -0700906 return -EINVAL;
907 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100908 }
909
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700910 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
911 if (!maps_node) {
912 pr_err("failed to alloc struct pinctrl_maps\n");
913 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100914 }
915
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700916 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -0600917 if (dup) {
918 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
919 GFP_KERNEL);
920 if (!maps_node->maps) {
921 pr_err("failed to duplicate mapping table\n");
922 kfree(maps_node);
923 return -ENOMEM;
924 }
925 } else {
926 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700927 }
928
Stephen Warren57291ce2012-03-23 10:29:46 -0600929 if (!locked)
930 mutex_lock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700931 list_add_tail(&maps_node->node, &pinctrl_maps);
Stephen Warren57291ce2012-03-23 10:29:46 -0600932 if (!locked)
933 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700934
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100935 return 0;
936}
937
Stephen Warren57291ce2012-03-23 10:29:46 -0600938/**
939 * pinctrl_register_mappings() - register a set of pin controller mappings
940 * @maps: the pincontrol mappings table to register. This should probably be
941 * marked with __initdata so it can be discarded after boot. This
942 * function will perform a shallow copy for the mapping entries.
943 * @num_maps: the number of maps in the mapping table
944 */
945int pinctrl_register_mappings(struct pinctrl_map const *maps,
946 unsigned num_maps)
947{
948 return pinctrl_register_map(maps, num_maps, true, false);
949}
950
951void pinctrl_unregister_map(struct pinctrl_map const *map)
952{
953 struct pinctrl_maps *maps_node;
954
955 list_for_each_entry(maps_node, &pinctrl_maps, node) {
956 if (maps_node->maps == map) {
957 list_del(&maps_node->node);
958 return;
959 }
960 }
961}
962
Linus Walleij2744e8a2011-05-02 20:50:54 +0200963#ifdef CONFIG_DEBUG_FS
964
965static int pinctrl_pins_show(struct seq_file *s, void *what)
966{
967 struct pinctrl_dev *pctldev = s->private;
968 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +0900969 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200970
971 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200972
Stephen Warren57b676f2012-03-02 13:05:44 -0700973 mutex_lock(&pinctrl_mutex);
974
Chanho Park706e8522012-01-03 16:47:50 +0900975 /* The pin number can be retrived from the pin controller descriptor */
976 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200977 struct pin_desc *desc;
978
Chanho Park706e8522012-01-03 16:47:50 +0900979 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200980 desc = pin_desc_get(pctldev, pin);
981 /* Pin space may be sparse */
982 if (desc == NULL)
983 continue;
984
985 seq_printf(s, "pin %d (%s) ", pin,
986 desc->name ? desc->name : "unnamed");
987
988 /* Driver-specific info per pin */
989 if (ops->pin_dbg_show)
990 ops->pin_dbg_show(pctldev, s, pin);
991
992 seq_puts(s, "\n");
993 }
994
Stephen Warren57b676f2012-03-02 13:05:44 -0700995 mutex_unlock(&pinctrl_mutex);
996
Linus Walleij2744e8a2011-05-02 20:50:54 +0200997 return 0;
998}
999
1000static int pinctrl_groups_show(struct seq_file *s, void *what)
1001{
1002 struct pinctrl_dev *pctldev = s->private;
1003 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +05301004 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001005
Viresh Kumard1e90e92012-03-30 11:25:40 +05301006 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001007 mutex_lock(&pinctrl_mutex);
1008
Linus Walleij2744e8a2011-05-02 20:50:54 +02001009 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +05301010 while (selector < ngroups) {
Stephen Warrena5818a82011-10-19 16:19:25 -06001011 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001012 unsigned num_pins;
1013 const char *gname = ops->get_group_name(pctldev, selector);
1014 int ret;
1015 int i;
1016
1017 ret = ops->get_group_pins(pctldev, selector,
1018 &pins, &num_pins);
1019 if (ret)
1020 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1021 gname);
1022 else {
1023 seq_printf(s, "group: %s, pins = [ ", gname);
1024 for (i = 0; i < num_pins; i++)
1025 seq_printf(s, "%d ", pins[i]);
1026 seq_puts(s, "]\n");
1027 }
1028 selector++;
1029 }
1030
Stephen Warren57b676f2012-03-02 13:05:44 -07001031 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001032
1033 return 0;
1034}
1035
1036static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1037{
1038 struct pinctrl_dev *pctldev = s->private;
1039 struct pinctrl_gpio_range *range = NULL;
1040
1041 seq_puts(s, "GPIO ranges handled:\n");
1042
Stephen Warren57b676f2012-03-02 13:05:44 -07001043 mutex_lock(&pinctrl_mutex);
1044
Linus Walleij2744e8a2011-05-02 20:50:54 +02001045 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001046 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Linus Walleij75d66422011-11-16 09:58:51 +01001047 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1048 range->id, range->name,
1049 range->base, (range->base + range->npins - 1),
1050 range->pin_base,
1051 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001052 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001053
1054 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001055
1056 return 0;
1057}
1058
1059static int pinctrl_devices_show(struct seq_file *s, void *what)
1060{
1061 struct pinctrl_dev *pctldev;
1062
Linus Walleijae6b4d82011-10-19 18:14:33 +02001063 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001064
1065 mutex_lock(&pinctrl_mutex);
1066
Linus Walleij2744e8a2011-05-02 20:50:54 +02001067 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1068 seq_printf(s, "%s ", pctldev->desc->name);
1069 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001070 seq_puts(s, "yes ");
1071 else
1072 seq_puts(s, "no ");
1073 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001074 seq_puts(s, "yes");
1075 else
1076 seq_puts(s, "no");
1077 seq_puts(s, "\n");
1078 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001079
1080 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001081
1082 return 0;
1083}
1084
Stephen Warren1e2082b2012-03-02 13:05:48 -07001085static inline const char *map_type(enum pinctrl_map_type type)
1086{
1087 static const char * const names[] = {
1088 "INVALID",
1089 "DUMMY_STATE",
1090 "MUX_GROUP",
1091 "CONFIGS_PIN",
1092 "CONFIGS_GROUP",
1093 };
1094
1095 if (type >= ARRAY_SIZE(names))
1096 return "UNKNOWN";
1097
1098 return names[type];
1099}
1100
Stephen Warren3eedb432012-02-23 17:04:40 -07001101static int pinctrl_maps_show(struct seq_file *s, void *what)
1102{
1103 struct pinctrl_maps *maps_node;
1104 int i;
1105 struct pinctrl_map const *map;
1106
1107 seq_puts(s, "Pinctrl maps:\n");
1108
Stephen Warren57b676f2012-03-02 13:05:44 -07001109 mutex_lock(&pinctrl_mutex);
1110
Stephen Warren3eedb432012-02-23 17:04:40 -07001111 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001112 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1113 map->dev_name, map->name, map_type(map->type),
1114 map->type);
1115
1116 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1117 seq_printf(s, "controlling device %s\n",
1118 map->ctrl_dev_name);
1119
1120 switch (map->type) {
1121 case PIN_MAP_TYPE_MUX_GROUP:
1122 pinmux_show_map(s, map);
1123 break;
1124 case PIN_MAP_TYPE_CONFIGS_PIN:
1125 case PIN_MAP_TYPE_CONFIGS_GROUP:
1126 pinconf_show_map(s, map);
1127 break;
1128 default:
1129 break;
1130 }
1131
1132 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001133 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001134
1135 mutex_unlock(&pinctrl_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001136
1137 return 0;
1138}
1139
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001140static int pinctrl_show(struct seq_file *s, void *what)
1141{
1142 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001143 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001144 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001145
1146 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001147
1148 mutex_lock(&pinctrl_mutex);
1149
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001150 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001151 seq_printf(s, "device: %s current state: %s\n",
1152 dev_name(p->dev),
1153 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001154
Stephen Warren6e5e9592012-03-02 13:05:47 -07001155 list_for_each_entry(state, &p->states, node) {
1156 seq_printf(s, " state: %s\n", state->name);
1157
1158 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001159 struct pinctrl_dev *pctldev = setting->pctldev;
1160
1161 seq_printf(s, " type: %s controller %s ",
1162 map_type(setting->type),
1163 pinctrl_dev_get_name(pctldev));
1164
1165 switch (setting->type) {
1166 case PIN_MAP_TYPE_MUX_GROUP:
1167 pinmux_show_setting(s, setting);
1168 break;
1169 case PIN_MAP_TYPE_CONFIGS_PIN:
1170 case PIN_MAP_TYPE_CONFIGS_GROUP:
1171 pinconf_show_setting(s, setting);
1172 break;
1173 default:
1174 break;
1175 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001176 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001177 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001178 }
1179
Stephen Warren57b676f2012-03-02 13:05:44 -07001180 mutex_unlock(&pinctrl_mutex);
1181
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001182 return 0;
1183}
1184
Linus Walleij2744e8a2011-05-02 20:50:54 +02001185static int pinctrl_pins_open(struct inode *inode, struct file *file)
1186{
1187 return single_open(file, pinctrl_pins_show, inode->i_private);
1188}
1189
1190static int pinctrl_groups_open(struct inode *inode, struct file *file)
1191{
1192 return single_open(file, pinctrl_groups_show, inode->i_private);
1193}
1194
1195static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1196{
1197 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1198}
1199
1200static int pinctrl_devices_open(struct inode *inode, struct file *file)
1201{
1202 return single_open(file, pinctrl_devices_show, NULL);
1203}
1204
Stephen Warren3eedb432012-02-23 17:04:40 -07001205static int pinctrl_maps_open(struct inode *inode, struct file *file)
1206{
1207 return single_open(file, pinctrl_maps_show, NULL);
1208}
1209
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001210static int pinctrl_open(struct inode *inode, struct file *file)
1211{
1212 return single_open(file, pinctrl_show, NULL);
1213}
1214
Linus Walleij2744e8a2011-05-02 20:50:54 +02001215static const struct file_operations pinctrl_pins_ops = {
1216 .open = pinctrl_pins_open,
1217 .read = seq_read,
1218 .llseek = seq_lseek,
1219 .release = single_release,
1220};
1221
1222static const struct file_operations pinctrl_groups_ops = {
1223 .open = pinctrl_groups_open,
1224 .read = seq_read,
1225 .llseek = seq_lseek,
1226 .release = single_release,
1227};
1228
1229static const struct file_operations pinctrl_gpioranges_ops = {
1230 .open = pinctrl_gpioranges_open,
1231 .read = seq_read,
1232 .llseek = seq_lseek,
1233 .release = single_release,
1234};
1235
1236static const struct file_operations pinctrl_devices_ops = {
1237 .open = pinctrl_devices_open,
1238 .read = seq_read,
1239 .llseek = seq_lseek,
1240 .release = single_release,
1241};
1242
Stephen Warren3eedb432012-02-23 17:04:40 -07001243static const struct file_operations pinctrl_maps_ops = {
1244 .open = pinctrl_maps_open,
1245 .read = seq_read,
1246 .llseek = seq_lseek,
1247 .release = single_release,
1248};
1249
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001250static const struct file_operations pinctrl_ops = {
1251 .open = pinctrl_open,
1252 .read = seq_read,
1253 .llseek = seq_lseek,
1254 .release = single_release,
1255};
1256
Linus Walleij2744e8a2011-05-02 20:50:54 +02001257static struct dentry *debugfs_root;
1258
1259static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1260{
Tony Lindgren02157162012-01-20 08:17:22 -08001261 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001262
Stephen Warren51cd24e2011-12-09 16:59:05 -07001263 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001264 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001265 pctldev->device_root = device_root;
1266
Linus Walleij2744e8a2011-05-02 20:50:54 +02001267 if (IS_ERR(device_root) || !device_root) {
1268 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001269 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001270 return;
1271 }
1272 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1273 device_root, pctldev, &pinctrl_pins_ops);
1274 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1275 device_root, pctldev, &pinctrl_groups_ops);
1276 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1277 device_root, pctldev, &pinctrl_gpioranges_ops);
1278 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +02001279 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001280}
1281
Tony Lindgren02157162012-01-20 08:17:22 -08001282static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1283{
1284 debugfs_remove_recursive(pctldev->device_root);
1285}
1286
Linus Walleij2744e8a2011-05-02 20:50:54 +02001287static void pinctrl_init_debugfs(void)
1288{
1289 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1290 if (IS_ERR(debugfs_root) || !debugfs_root) {
1291 pr_warn("failed to create debugfs directory\n");
1292 debugfs_root = NULL;
1293 return;
1294 }
1295
1296 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1297 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001298 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1299 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001300 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1301 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001302}
1303
1304#else /* CONFIG_DEBUG_FS */
1305
1306static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1307{
1308}
1309
1310static void pinctrl_init_debugfs(void)
1311{
1312}
1313
Tony Lindgren02157162012-01-20 08:17:22 -08001314static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1315{
1316}
1317
Linus Walleij2744e8a2011-05-02 20:50:54 +02001318#endif
1319
Stephen Warrend26bc492012-03-16 14:54:25 -06001320static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1321{
1322 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1323
1324 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301325 !ops->get_groups_count ||
Stephen Warrend26bc492012-03-16 14:54:25 -06001326 !ops->get_group_name ||
1327 !ops->get_group_pins)
1328 return -EINVAL;
1329
Stephen Warren57291ce2012-03-23 10:29:46 -06001330 if (ops->dt_node_to_map && !ops->dt_free_map)
1331 return -EINVAL;
1332
Stephen Warrend26bc492012-03-16 14:54:25 -06001333 return 0;
1334}
1335
Linus Walleij2744e8a2011-05-02 20:50:54 +02001336/**
1337 * pinctrl_register() - register a pin controller device
1338 * @pctldesc: descriptor for this pin controller
1339 * @dev: parent device for this pin controller
1340 * @driver_data: private pin controller data for this pin controller
1341 */
1342struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1343 struct device *dev, void *driver_data)
1344{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001345 struct pinctrl_dev *pctldev;
1346 int ret;
1347
1348 if (pctldesc == NULL)
1349 return NULL;
1350 if (pctldesc->name == NULL)
1351 return NULL;
1352
Stephen Warren02f5b982012-02-22 14:26:00 -07001353 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001354 if (pctldev == NULL) {
1355 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001356 return NULL;
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001357 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001358
1359 /* Initialize pin control device struct */
1360 pctldev->owner = pctldesc->owner;
1361 pctldev->desc = pctldesc;
1362 pctldev->driver_data = driver_data;
1363 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001364 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001365 pctldev->dev = dev;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001366
Stephen Warrend26bc492012-03-16 14:54:25 -06001367 /* check core ops for sanity */
1368 ret = pinctrl_check_ops(pctldev);
1369 if (ret) {
1370 pr_err("%s pinctrl ops lacks necessary functions\n",
1371 pctldesc->name);
1372 goto out_err;
1373 }
1374
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001375 /* If we're implementing pinmuxing, check the ops for sanity */
1376 if (pctldesc->pmxops) {
1377 ret = pinmux_check_ops(pctldev);
1378 if (ret) {
1379 pr_err("%s pinmux ops lacks necessary functions\n",
1380 pctldesc->name);
1381 goto out_err;
1382 }
1383 }
1384
1385 /* If we're implementing pinconfig, check the ops for sanity */
1386 if (pctldesc->confops) {
1387 ret = pinconf_check_ops(pctldev);
1388 if (ret) {
1389 pr_err("%s pin config ops lacks necessary functions\n",
1390 pctldesc->name);
1391 goto out_err;
1392 }
1393 }
1394
Linus Walleij2744e8a2011-05-02 20:50:54 +02001395 /* Register all the pins */
1396 pr_debug("try to register %d pins on %s...\n",
1397 pctldesc->npins, pctldesc->name);
1398 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1399 if (ret) {
1400 pr_err("error during pin registration\n");
1401 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1402 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001403 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001404 }
1405
Stephen Warren57b676f2012-03-02 13:05:44 -07001406 mutex_lock(&pinctrl_mutex);
1407
Stephen Warren8b9c1392012-02-19 23:45:42 -07001408 list_add_tail(&pctldev->node, &pinctrldev_list);
Stephen Warren57b676f2012-03-02 13:05:44 -07001409
Stephen Warren6e5e9592012-03-02 13:05:47 -07001410 pctldev->p = pinctrl_get_locked(pctldev->dev);
1411 if (!IS_ERR(pctldev->p)) {
1412 struct pinctrl_state *s =
1413 pinctrl_lookup_state_locked(pctldev->p,
1414 PINCTRL_STATE_DEFAULT);
1415 if (!IS_ERR(s))
1416 pinctrl_select_state_locked(pctldev->p, s);
1417 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001418
1419 mutex_unlock(&pinctrl_mutex);
1420
Stephen Warren2304b472012-02-22 14:26:01 -07001421 pinctrl_init_device_debugfs(pctldev);
1422
Linus Walleij2744e8a2011-05-02 20:50:54 +02001423 return pctldev;
1424
Stephen Warren51cd24e2011-12-09 16:59:05 -07001425out_err:
1426 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001427 return NULL;
1428}
1429EXPORT_SYMBOL_GPL(pinctrl_register);
1430
1431/**
1432 * pinctrl_unregister() - unregister pinmux
1433 * @pctldev: pin controller to unregister
1434 *
1435 * Called by pinmux drivers to unregister a pinmux.
1436 */
1437void pinctrl_unregister(struct pinctrl_dev *pctldev)
1438{
1439 if (pctldev == NULL)
1440 return;
1441
Tony Lindgren02157162012-01-20 08:17:22 -08001442 pinctrl_remove_device_debugfs(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001443
1444 mutex_lock(&pinctrl_mutex);
1445
Stephen Warren6e5e9592012-03-02 13:05:47 -07001446 if (!IS_ERR(pctldev->p))
1447 pinctrl_put_locked(pctldev->p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -07001448
Linus Walleij2744e8a2011-05-02 20:50:54 +02001449 /* TODO: check that no pinmuxes are still active? */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001450 list_del(&pctldev->node);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001451 /* Destroy descriptor tree */
1452 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1453 pctldev->desc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001454 kfree(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001455
1456 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001457}
1458EXPORT_SYMBOL_GPL(pinctrl_unregister);
1459
1460static int __init pinctrl_init(void)
1461{
1462 pr_info("initialized pinctrl subsystem\n");
1463 pinctrl_init_debugfs();
1464 return 0;
1465}
1466
1467/* init early since many drivers really need to initialized pinmux early */
1468core_initcall(pinctrl_init);