blob: ec3b8cc188af512c2ba3645b6beec39b97f1ae40 [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>
26#include <linux/pinctrl/pinctrl.h>
27#include <linux/pinctrl/machine.h>
28#include "core.h"
29#include "pinmux.h"
Linus Walleijae6b4d82011-10-19 18:14:33 +020030#include "pinconf.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020031
Linus Walleijbefe5bd2012-02-09 19:47:48 +010032/**
Stephen Warrenb2b3e662012-02-19 23:45:43 -070033 * struct pinctrl_maps - a list item containing part of the mapping table
34 * @node: mapping table list node
35 * @maps: array of mapping table entries
36 * @num_maps: the number of entries in @maps
37 */
38struct pinctrl_maps {
39 struct list_head node;
40 struct pinctrl_map const *maps;
41 unsigned num_maps;
42};
43
Stephen Warren57b676f2012-03-02 13:05:44 -070044/* Mutex taken by all entry points */
45DEFINE_MUTEX(pinctrl_mutex);
46
47/* Global list of pin control devices (struct pinctrl_dev) */
Linus Walleij2744e8a2011-05-02 20:50:54 +020048static LIST_HEAD(pinctrldev_list);
49
Stephen Warren57b676f2012-03-02 13:05:44 -070050/* List of pin controller handles (struct pinctrl) */
Linus Walleijbefe5bd2012-02-09 19:47:48 +010051static LIST_HEAD(pinctrl_list);
52
Stephen Warren57b676f2012-03-02 13:05:44 -070053/* List of pinctrl maps (struct pinctrl_maps) */
Stephen Warrenb2b3e662012-02-19 23:45:43 -070054static LIST_HEAD(pinctrl_maps);
55
56#define for_each_maps(_maps_node_, _i_, _map_) \
57 list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
58 for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
59 _i_ < _maps_node_->num_maps; \
60 i++, _map_ = &_maps_node_->maps[_i_])
Linus Walleijbefe5bd2012-02-09 19:47:48 +010061
Linus Walleij2744e8a2011-05-02 20:50:54 +020062const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
63{
64 /* We're not allowed to register devices without name */
65 return pctldev->desc->name;
66}
67EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
68
69void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
70{
71 return pctldev->driver_data;
72}
73EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
74
75/**
Linus Walleij9dfac4f2012-02-01 18:02:47 +010076 * get_pinctrl_dev_from_devname() - look up pin controller device
77 * @devname: the name of a device instance, as returned by dev_name()
Linus Walleij2744e8a2011-05-02 20:50:54 +020078 *
79 * Looks up a pin control device matching a certain device name or pure device
80 * pointer, the pure device pointer will take precedence.
81 */
Linus Walleij9dfac4f2012-02-01 18:02:47 +010082struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
Linus Walleij2744e8a2011-05-02 20:50:54 +020083{
84 struct pinctrl_dev *pctldev = NULL;
85 bool found = false;
86
Linus Walleij9dfac4f2012-02-01 18:02:47 +010087 if (!devname)
88 return NULL;
89
Linus Walleij2744e8a2011-05-02 20:50:54 +020090 list_for_each_entry(pctldev, &pinctrldev_list, node) {
Linus Walleij9dfac4f2012-02-01 18:02:47 +010091 if (!strcmp(dev_name(pctldev->dev), devname)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +020092 /* Matched on device name */
93 found = true;
94 break;
95 }
96 }
Linus Walleij2744e8a2011-05-02 20:50:54 +020097
98 return found ? pctldev : NULL;
99}
100
Linus Walleij2744e8a2011-05-02 20:50:54 +0200101/**
Linus Walleijae6b4d82011-10-19 18:14:33 +0200102 * pin_get_from_name() - look up a pin number from a name
103 * @pctldev: the pin control device to lookup the pin on
104 * @name: the name of the pin to look up
105 */
106int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
107{
Chanho Park706e8522012-01-03 16:47:50 +0900108 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200109
Chanho Park706e8522012-01-03 16:47:50 +0900110 /* The pin number can be retrived from the pin controller descriptor */
111 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200112 struct pin_desc *desc;
113
Chanho Park706e8522012-01-03 16:47:50 +0900114 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200115 desc = pin_desc_get(pctldev, pin);
116 /* Pin space may be sparse */
117 if (desc == NULL)
118 continue;
119 if (desc->name && !strcmp(name, desc->name))
120 return pin;
121 }
122
123 return -EINVAL;
124}
125
126/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200127 * pin_is_valid() - check if pin exists on controller
128 * @pctldev: the pin control device to check the pin on
129 * @pin: pin to check, use the local pin controller index number
130 *
131 * This tells us whether a certain pin exist on a certain pin controller or
132 * not. Pin lists may be sparse, so some pins may not exist.
133 */
134bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
135{
136 struct pin_desc *pindesc;
137
138 if (pin < 0)
139 return false;
140
Stephen Warren57b676f2012-03-02 13:05:44 -0700141 mutex_lock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200142 pindesc = pin_desc_get(pctldev, pin);
Stephen Warren57b676f2012-03-02 13:05:44 -0700143 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200144
Stephen Warren57b676f2012-03-02 13:05:44 -0700145 return pindesc != NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200146}
147EXPORT_SYMBOL_GPL(pin_is_valid);
148
149/* Deletes a range of pin descriptors */
150static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
151 const struct pinctrl_pin_desc *pins,
152 unsigned num_pins)
153{
154 int i;
155
Linus Walleij2744e8a2011-05-02 20:50:54 +0200156 for (i = 0; i < num_pins; i++) {
157 struct pin_desc *pindesc;
158
159 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
160 pins[i].number);
161 if (pindesc != NULL) {
162 radix_tree_delete(&pctldev->pin_desc_tree,
163 pins[i].number);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100164 if (pindesc->dynamic_name)
165 kfree(pindesc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200166 }
167 kfree(pindesc);
168 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200169}
170
171static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
172 unsigned number, const char *name)
173{
174 struct pin_desc *pindesc;
175
176 pindesc = pin_desc_get(pctldev, number);
177 if (pindesc != NULL) {
178 pr_err("pin %d already registered on %s\n", number,
179 pctldev->desc->name);
180 return -EINVAL;
181 }
182
183 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700184 if (pindesc == NULL) {
185 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200186 return -ENOMEM;
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700187 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200188
Linus Walleij2744e8a2011-05-02 20:50:54 +0200189 /* Set owner */
190 pindesc->pctldev = pctldev;
191
Stephen Warren9af1e442011-10-19 16:19:27 -0600192 /* Copy basic pin info */
Linus Walleij8dc6ae42012-02-01 18:11:40 +0100193 if (name) {
Linus Walleijca53c5f2011-12-14 20:33:37 +0100194 pindesc->name = name;
195 } else {
196 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
197 if (pindesc->name == NULL)
198 return -ENOMEM;
199 pindesc->dynamic_name = true;
200 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200201
Linus Walleij2744e8a2011-05-02 20:50:54 +0200202 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200203 pr_debug("registered pin %d (%s) on %s\n",
Linus Walleijca53c5f2011-12-14 20:33:37 +0100204 number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200205 return 0;
206}
207
208static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
209 struct pinctrl_pin_desc const *pins,
210 unsigned num_descs)
211{
212 unsigned i;
213 int ret = 0;
214
215 for (i = 0; i < num_descs; i++) {
216 ret = pinctrl_register_one_pin(pctldev,
217 pins[i].number, pins[i].name);
218 if (ret)
219 return ret;
220 }
221
222 return 0;
223}
224
225/**
226 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
227 * @pctldev: pin controller device to check
228 * @gpio: gpio pin to check taken from the global GPIO pin space
229 *
230 * Tries to match a GPIO pin number to the ranges handled by a certain pin
231 * controller, return the range or NULL
232 */
233static struct pinctrl_gpio_range *
234pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
235{
236 struct pinctrl_gpio_range *range = NULL;
237
238 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200239 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
240 /* Check if we're in the valid range */
241 if (gpio >= range->base &&
242 gpio < range->base + range->npins) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200243 return range;
244 }
245 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200246
247 return NULL;
248}
249
250/**
251 * pinctrl_get_device_gpio_range() - find device for GPIO range
252 * @gpio: the pin to locate the pin controller for
253 * @outdev: the pin control device if found
254 * @outrange: the GPIO range if found
255 *
256 * Find the pin controller handling a certain GPIO pin from the pinspace of
257 * the GPIO subsystem, return the device and the matching GPIO range. Returns
258 * negative if the GPIO range could not be found in any device.
259 */
Stephen Warren4ecce452012-02-19 23:45:47 -0700260static int pinctrl_get_device_gpio_range(unsigned gpio,
261 struct pinctrl_dev **outdev,
262 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200263{
264 struct pinctrl_dev *pctldev = NULL;
265
266 /* Loop over the pin controllers */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200267 list_for_each_entry(pctldev, &pinctrldev_list, node) {
268 struct pinctrl_gpio_range *range;
269
270 range = pinctrl_match_gpio_range(pctldev, gpio);
271 if (range != NULL) {
272 *outdev = pctldev;
273 *outrange = range;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200274 return 0;
275 }
276 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200277
278 return -EINVAL;
279}
280
281/**
282 * pinctrl_add_gpio_range() - register a GPIO range for a controller
283 * @pctldev: pin controller device to add the range to
284 * @range: the GPIO range to add
285 *
286 * This adds a range of GPIOs to be handled by a certain pin controller. Call
287 * this to register handled ranges after registering your pin controller.
288 */
289void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
290 struct pinctrl_gpio_range *range)
291{
Stephen Warren57b676f2012-03-02 13:05:44 -0700292 mutex_lock(&pinctrl_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700293 list_add_tail(&range->node, &pctldev->gpio_ranges);
Stephen Warren57b676f2012-03-02 13:05:44 -0700294 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200295}
Stephen Warren4ecce452012-02-19 23:45:47 -0700296EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200297
298/**
299 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
300 * @pctldev: pin controller device to remove the range from
301 * @range: the GPIO range to remove
302 */
303void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
304 struct pinctrl_gpio_range *range)
305{
Stephen Warren57b676f2012-03-02 13:05:44 -0700306 mutex_lock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200307 list_del(&range->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700308 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200309}
Stephen Warren4ecce452012-02-19 23:45:47 -0700310EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200311
Linus Walleij7afde8b2011-10-19 17:07:16 +0200312/**
313 * pinctrl_get_group_selector() - returns the group selector for a group
314 * @pctldev: the pin controller handling the group
315 * @pin_group: the pin group to look up
316 */
317int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
318 const char *pin_group)
319{
320 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
321 unsigned group_selector = 0;
322
323 while (pctlops->list_groups(pctldev, group_selector) >= 0) {
324 const char *gname = pctlops->get_group_name(pctldev,
325 group_selector);
326 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700327 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200328 "found group selector %u for %s\n",
329 group_selector,
330 pin_group);
331 return group_selector;
332 }
333
334 group_selector++;
335 }
336
Stephen Warren51cd24e2011-12-09 16:59:05 -0700337 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200338 pin_group);
339
340 return -EINVAL;
341}
342
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100343/**
344 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
345 * @gpio: the GPIO pin number from the GPIO subsystem number space
346 *
347 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
348 * as part of their gpio_request() semantics, platforms and individual drivers
349 * shall *NOT* request GPIO pins to be muxed in.
350 */
351int pinctrl_request_gpio(unsigned gpio)
352{
353 struct pinctrl_dev *pctldev;
354 struct pinctrl_gpio_range *range;
355 int ret;
356 int pin;
357
Stephen Warren57b676f2012-03-02 13:05:44 -0700358 mutex_lock(&pinctrl_mutex);
359
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100360 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700361 if (ret) {
362 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100363 return -EINVAL;
Stephen Warren57b676f2012-03-02 13:05:44 -0700364 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100365
366 /* Convert to the pin controllers number space */
367 pin = gpio - range->base + range->pin_base;
368
Stephen Warren57b676f2012-03-02 13:05:44 -0700369 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
370
371 mutex_unlock(&pinctrl_mutex);
372 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100373}
374EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
375
376/**
377 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
378 * @gpio: the GPIO pin number from the GPIO subsystem number space
379 *
380 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
381 * as part of their gpio_free() semantics, platforms and individual drivers
382 * shall *NOT* request GPIO pins to be muxed out.
383 */
384void pinctrl_free_gpio(unsigned gpio)
385{
386 struct pinctrl_dev *pctldev;
387 struct pinctrl_gpio_range *range;
388 int ret;
389 int pin;
390
Stephen Warren57b676f2012-03-02 13:05:44 -0700391 mutex_lock(&pinctrl_mutex);
392
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100393 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700394 if (ret) {
395 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100396 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700397 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100398
399 /* Convert to the pin controllers number space */
400 pin = gpio - range->base + range->pin_base;
401
Stephen Warren57b676f2012-03-02 13:05:44 -0700402 pinmux_free_gpio(pctldev, pin, range);
403
404 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100405}
406EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
407
408static int pinctrl_gpio_direction(unsigned gpio, bool input)
409{
410 struct pinctrl_dev *pctldev;
411 struct pinctrl_gpio_range *range;
412 int ret;
413 int pin;
414
415 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
416 if (ret)
417 return ret;
418
419 /* Convert to the pin controllers number space */
420 pin = gpio - range->base + range->pin_base;
421
422 return pinmux_gpio_direction(pctldev, range, pin, input);
423}
424
425/**
426 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
427 * @gpio: the GPIO pin number from the GPIO subsystem number space
428 *
429 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
430 * as part of their gpio_direction_input() semantics, platforms and individual
431 * drivers shall *NOT* touch pin control GPIO calls.
432 */
433int pinctrl_gpio_direction_input(unsigned gpio)
434{
Stephen Warren57b676f2012-03-02 13:05:44 -0700435 int ret;
436 mutex_lock(&pinctrl_mutex);
437 ret = pinctrl_gpio_direction(gpio, true);
438 mutex_unlock(&pinctrl_mutex);
439 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100440}
441EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
442
443/**
444 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
445 * @gpio: the GPIO pin number from the GPIO subsystem number space
446 *
447 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
448 * as part of their gpio_direction_output() semantics, platforms and individual
449 * drivers shall *NOT* touch pin control GPIO calls.
450 */
451int pinctrl_gpio_direction_output(unsigned gpio)
452{
Stephen Warren57b676f2012-03-02 13:05:44 -0700453 int ret;
454 mutex_lock(&pinctrl_mutex);
455 ret = pinctrl_gpio_direction(gpio, false);
456 mutex_unlock(&pinctrl_mutex);
457 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100458}
459EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
460
Stephen Warren6e5e9592012-03-02 13:05:47 -0700461static struct pinctrl_state *find_state(struct pinctrl *p,
462 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100463{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700464 struct pinctrl_state *state;
465
466 list_for_each_entry(state, &p->states, node)
467 if (!strcmp(state->name, name))
468 return state;
469
470 return NULL;
471}
472
473static struct pinctrl_state *create_state(struct pinctrl *p,
474 const char *name)
475{
476 struct pinctrl_state *state;
477
478 state = kzalloc(sizeof(*state), GFP_KERNEL);
479 if (state == NULL) {
480 dev_err(p->dev,
481 "failed to alloc struct pinctrl_state\n");
482 return ERR_PTR(-ENOMEM);
483 }
484
485 state->name = name;
486 INIT_LIST_HEAD(&state->settings);
487
488 list_add_tail(&state->node, &p->states);
489
490 return state;
491}
492
493static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
494{
495 struct pinctrl_state *state;
496 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700497 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700498
499 state = find_state(p, map->name);
500 if (!state)
501 state = create_state(p, map->name);
502 if (IS_ERR(state))
503 return PTR_ERR(state);
504
Stephen Warren1e2082b2012-03-02 13:05:48 -0700505 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
506 return 0;
507
Stephen Warren6e5e9592012-03-02 13:05:47 -0700508 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
509 if (setting == NULL) {
510 dev_err(p->dev,
511 "failed to alloc struct pinctrl_setting\n");
512 return -ENOMEM;
513 }
514
Stephen Warren1e2082b2012-03-02 13:05:48 -0700515 setting->type = map->type;
516
Stephen Warren6e5e9592012-03-02 13:05:47 -0700517 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
518 if (setting->pctldev == NULL) {
519 dev_err(p->dev, "unknown pinctrl device %s in map entry",
520 map->ctrl_dev_name);
521 kfree(setting);
522 /* Eventually, this should trigger deferred probe */
523 return -ENODEV;
524 }
525
Stephen Warren1e2082b2012-03-02 13:05:48 -0700526 switch (map->type) {
527 case PIN_MAP_TYPE_MUX_GROUP:
528 ret = pinmux_map_to_setting(map, setting);
529 break;
530 case PIN_MAP_TYPE_CONFIGS_PIN:
531 case PIN_MAP_TYPE_CONFIGS_GROUP:
532 ret = pinconf_map_to_setting(map, setting);
533 break;
534 default:
535 ret = -EINVAL;
536 break;
537 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700538 if (ret < 0) {
539 kfree(setting);
540 return ret;
541 }
542
543 list_add_tail(&setting->node, &state->settings);
544
545 return 0;
546}
547
548static struct pinctrl *find_pinctrl(struct device *dev)
549{
550 struct pinctrl *p;
551
Stephen Warren1e2082b2012-03-02 13:05:48 -0700552 list_for_each_entry(p, &pinctrl_list, node)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700553 if (p->dev == dev)
554 return p;
555
556 return NULL;
557}
558
559static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
560
561static struct pinctrl *create_pinctrl(struct device *dev)
562{
563 struct pinctrl *p;
564 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700565 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100566 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700567 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700568 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100569
570 /*
571 * create the state cookie holder struct pinctrl for each
572 * mapping, this is what consumers will get when requesting
573 * a pin control handle with pinctrl_get()
574 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700575 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700576 if (p == NULL) {
577 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100578 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700579 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700580 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700581 INIT_LIST_HEAD(&p->states);
582
583 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100584
585 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700586 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -0700587 /* Map must be for this device */
588 if (strcmp(map->dev_name, devname))
589 continue;
590
Stephen Warren6e5e9592012-03-02 13:05:47 -0700591 ret = add_setting(p, map);
592 if (ret < 0) {
593 pinctrl_put_locked(p, false);
594 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100595 }
596 }
597
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100598 /* Add the pinmux to the global list */
Stephen Warren8b9c1392012-02-19 23:45:42 -0700599 list_add_tail(&p->node, &pinctrl_list);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100600
601 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700602}
Stephen Warren7ecdb162012-03-02 13:05:45 -0700603
Stephen Warren6e5e9592012-03-02 13:05:47 -0700604static struct pinctrl *pinctrl_get_locked(struct device *dev)
605{
606 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700607
Stephen Warren6e5e9592012-03-02 13:05:47 -0700608 if (WARN_ON(!dev))
609 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700610
Stephen Warren6e5e9592012-03-02 13:05:47 -0700611 p = find_pinctrl(dev);
612 if (p != NULL)
613 return ERR_PTR(-EBUSY);
614
615 p = create_pinctrl(dev);
616 if (IS_ERR(p))
617 return p;
618
619 return p;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100620}
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700621
622/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700623 * pinctrl_get() - retrieves the pinctrl handle for a device
624 * @dev: the device to obtain the handle for
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700625 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700626struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700627{
628 struct pinctrl *p;
629
Stephen Warren57b676f2012-03-02 13:05:44 -0700630 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700631 p = pinctrl_get_locked(dev);
Stephen Warren57b676f2012-03-02 13:05:44 -0700632 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700633
634 return p;
635}
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100636EXPORT_SYMBOL_GPL(pinctrl_get);
637
Stephen Warren6e5e9592012-03-02 13:05:47 -0700638static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -0700639{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700640 struct pinctrl_state *state, *n1;
641 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700642
Stephen Warren6e5e9592012-03-02 13:05:47 -0700643 list_for_each_entry_safe(state, n1, &p->states, node) {
644 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700645 switch (setting->type) {
646 case PIN_MAP_TYPE_MUX_GROUP:
647 if (state == p->state)
648 pinmux_disable_setting(setting);
649 pinmux_free_setting(setting);
650 break;
651 case PIN_MAP_TYPE_CONFIGS_PIN:
652 case PIN_MAP_TYPE_CONFIGS_GROUP:
653 pinconf_free_setting(setting);
654 break;
655 default:
656 break;
657 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700658 list_del(&setting->node);
659 kfree(setting);
660 }
661 list_del(&state->node);
662 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700663 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700664
Stephen Warren6e5e9592012-03-02 13:05:47 -0700665 if (inlist)
666 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700667 kfree(p);
668}
669
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100670/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700671 * pinctrl_put() - release a previously claimed pinctrl handle
672 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100673 */
674void pinctrl_put(struct pinctrl *p)
675{
Stephen Warren57b676f2012-03-02 13:05:44 -0700676 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700677 pinctrl_put_locked(p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -0700678 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100679}
680EXPORT_SYMBOL_GPL(pinctrl_put);
681
Stephen Warren6e5e9592012-03-02 13:05:47 -0700682static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
683 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -0700684{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700685 struct pinctrl_state *state;
686
687 state = find_state(p, name);
688 if (!state)
689 return ERR_PTR(-ENODEV);
690
691 return state;
692}
693
694/**
695 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
696 * @p: the pinctrl handle to retrieve the state from
697 * @name: the state name to retrieve
698 */
699struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
700{
701 struct pinctrl_state *s;
702
703 mutex_lock(&pinctrl_mutex);
704 s = pinctrl_lookup_state_locked(p, name);
705 mutex_unlock(&pinctrl_mutex);
706
707 return s;
708}
709EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
710
711static int pinctrl_select_state_locked(struct pinctrl *p,
712 struct pinctrl_state *state)
713{
714 struct pinctrl_setting *setting, *setting2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700715 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700716
Stephen Warren6e5e9592012-03-02 13:05:47 -0700717 if (p->state == state)
718 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700719
Stephen Warren6e5e9592012-03-02 13:05:47 -0700720 if (p->state) {
721 /*
722 * The set of groups with a mux configuration in the old state
723 * may not be identical to the set of groups with a mux setting
724 * in the new state. While this might be unusual, it's entirely
725 * possible for the "user"-supplied mapping table to be written
726 * that way. For each group that was configured in the old state
727 * but not in the new state, this code puts that group into a
728 * safe/disabled state.
729 */
730 list_for_each_entry(setting, &p->state->settings, node) {
731 bool found = false;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700732 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
733 continue;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700734 list_for_each_entry(setting2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700735 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
736 continue;
737 if (setting2->data.mux.group ==
738 setting->data.mux.group) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700739 found = true;
740 break;
741 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700742 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700743 if (!found)
744 pinmux_disable_setting(setting);
745 }
746 }
747
748 p->state = state;
749
750 /* Apply all the settings for the new state */
751 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700752 switch (setting->type) {
753 case PIN_MAP_TYPE_MUX_GROUP:
754 ret = pinmux_enable_setting(setting);
755 break;
756 case PIN_MAP_TYPE_CONFIGS_PIN:
757 case PIN_MAP_TYPE_CONFIGS_GROUP:
758 ret = pinconf_apply_setting(setting);
759 break;
760 default:
761 ret = -EINVAL;
762 break;
763 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700764 if (ret < 0) {
765 /* FIXME: Difficult to return to prev state */
766 return ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700767 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700768 }
769
Stephen Warren7ecdb162012-03-02 13:05:45 -0700770 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700771}
772
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100773/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700774 * pinctrl_select() - select/activate/program a pinctrl state to HW
775 * @p: the pinctrl handle for the device that requests configuratio
776 * @state: the state handle to select/activate/program
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100777 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700778int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100779{
Stephen Warren57b676f2012-03-02 13:05:44 -0700780 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700781
Stephen Warren57b676f2012-03-02 13:05:44 -0700782 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700783 ret = pinctrl_select_state_locked(p, state);
Stephen Warren57b676f2012-03-02 13:05:44 -0700784 mutex_unlock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700785
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100786 return ret;
787}
Stephen Warren6e5e9592012-03-02 13:05:47 -0700788EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100789
790/**
791 * pinctrl_register_mappings() - register a set of pin controller mappings
Stephen Warren13398a42012-02-19 23:45:41 -0700792 * @maps: the pincontrol mappings table to register. This should probably be
793 * marked with __initdata so it can be discarded after boot. This
794 * function will perform a shallow copy for the mapping entries.
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100795 * @num_maps: the number of maps in the mapping table
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100796 */
Stephen Warren13398a42012-02-19 23:45:41 -0700797int pinctrl_register_mappings(struct pinctrl_map const *maps,
798 unsigned num_maps)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100799{
Stephen Warren1e2082b2012-03-02 13:05:48 -0700800 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700801 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100802
803 pr_debug("add %d pinmux maps\n", num_maps);
804
805 /* First sanity check the new mapping */
806 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700807 if (!maps[i].dev_name) {
808 pr_err("failed to register map %s (%d): no device given\n",
809 maps[i].name, i);
810 return -EINVAL;
811 }
812
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100813 if (!maps[i].name) {
814 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700815 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100816 return -EINVAL;
817 }
818
Stephen Warren1e2082b2012-03-02 13:05:48 -0700819 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
820 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100821 pr_err("failed to register map %s (%d): no pin control device given\n",
822 maps[i].name, i);
823 return -EINVAL;
824 }
825
Stephen Warren1e2082b2012-03-02 13:05:48 -0700826 switch (maps[i].type) {
827 case PIN_MAP_TYPE_DUMMY_STATE:
828 break;
829 case PIN_MAP_TYPE_MUX_GROUP:
830 ret = pinmux_validate_map(&maps[i], i);
831 if (ret < 0)
832 return 0;
833 break;
834 case PIN_MAP_TYPE_CONFIGS_PIN:
835 case PIN_MAP_TYPE_CONFIGS_GROUP:
836 ret = pinconf_validate_map(&maps[i], i);
837 if (ret < 0)
838 return 0;
839 break;
840 default:
841 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700842 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -0700843 return -EINVAL;
844 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100845 }
846
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700847 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
848 if (!maps_node) {
849 pr_err("failed to alloc struct pinctrl_maps\n");
850 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100851 }
852
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700853 maps_node->num_maps = num_maps;
854 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps, GFP_KERNEL);
855 if (!maps_node->maps) {
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700856 pr_err("failed to duplicate mapping table\n");
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700857 kfree(maps_node);
858 return -ENOMEM;
859 }
860
Stephen Warren57b676f2012-03-02 13:05:44 -0700861 mutex_lock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700862 list_add_tail(&maps_node->node, &pinctrl_maps);
Stephen Warren57b676f2012-03-02 13:05:44 -0700863 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700864
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100865 return 0;
866}
867
Linus Walleij2744e8a2011-05-02 20:50:54 +0200868#ifdef CONFIG_DEBUG_FS
869
870static int pinctrl_pins_show(struct seq_file *s, void *what)
871{
872 struct pinctrl_dev *pctldev = s->private;
873 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +0900874 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200875
876 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200877
Stephen Warren57b676f2012-03-02 13:05:44 -0700878 mutex_lock(&pinctrl_mutex);
879
Chanho Park706e8522012-01-03 16:47:50 +0900880 /* The pin number can be retrived from the pin controller descriptor */
881 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200882 struct pin_desc *desc;
883
Chanho Park706e8522012-01-03 16:47:50 +0900884 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200885 desc = pin_desc_get(pctldev, pin);
886 /* Pin space may be sparse */
887 if (desc == NULL)
888 continue;
889
890 seq_printf(s, "pin %d (%s) ", pin,
891 desc->name ? desc->name : "unnamed");
892
893 /* Driver-specific info per pin */
894 if (ops->pin_dbg_show)
895 ops->pin_dbg_show(pctldev, s, pin);
896
897 seq_puts(s, "\n");
898 }
899
Stephen Warren57b676f2012-03-02 13:05:44 -0700900 mutex_unlock(&pinctrl_mutex);
901
Linus Walleij2744e8a2011-05-02 20:50:54 +0200902 return 0;
903}
904
905static int pinctrl_groups_show(struct seq_file *s, void *what)
906{
907 struct pinctrl_dev *pctldev = s->private;
908 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
909 unsigned selector = 0;
910
911 /* No grouping */
912 if (!ops)
913 return 0;
914
Stephen Warren57b676f2012-03-02 13:05:44 -0700915 mutex_lock(&pinctrl_mutex);
916
Linus Walleij2744e8a2011-05-02 20:50:54 +0200917 seq_puts(s, "registered pin groups:\n");
918 while (ops->list_groups(pctldev, selector) >= 0) {
Stephen Warrena5818a82011-10-19 16:19:25 -0600919 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200920 unsigned num_pins;
921 const char *gname = ops->get_group_name(pctldev, selector);
922 int ret;
923 int i;
924
925 ret = ops->get_group_pins(pctldev, selector,
926 &pins, &num_pins);
927 if (ret)
928 seq_printf(s, "%s [ERROR GETTING PINS]\n",
929 gname);
930 else {
931 seq_printf(s, "group: %s, pins = [ ", gname);
932 for (i = 0; i < num_pins; i++)
933 seq_printf(s, "%d ", pins[i]);
934 seq_puts(s, "]\n");
935 }
936 selector++;
937 }
938
Stephen Warren57b676f2012-03-02 13:05:44 -0700939 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200940
941 return 0;
942}
943
944static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
945{
946 struct pinctrl_dev *pctldev = s->private;
947 struct pinctrl_gpio_range *range = NULL;
948
949 seq_puts(s, "GPIO ranges handled:\n");
950
Stephen Warren57b676f2012-03-02 13:05:44 -0700951 mutex_lock(&pinctrl_mutex);
952
Linus Walleij2744e8a2011-05-02 20:50:54 +0200953 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200954 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Linus Walleij75d66422011-11-16 09:58:51 +0100955 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
956 range->id, range->name,
957 range->base, (range->base + range->npins - 1),
958 range->pin_base,
959 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +0200960 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700961
962 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200963
964 return 0;
965}
966
967static int pinctrl_devices_show(struct seq_file *s, void *what)
968{
969 struct pinctrl_dev *pctldev;
970
Linus Walleijae6b4d82011-10-19 18:14:33 +0200971 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -0700972
973 mutex_lock(&pinctrl_mutex);
974
Linus Walleij2744e8a2011-05-02 20:50:54 +0200975 list_for_each_entry(pctldev, &pinctrldev_list, node) {
976 seq_printf(s, "%s ", pctldev->desc->name);
977 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +0200978 seq_puts(s, "yes ");
979 else
980 seq_puts(s, "no ");
981 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200982 seq_puts(s, "yes");
983 else
984 seq_puts(s, "no");
985 seq_puts(s, "\n");
986 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700987
988 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200989
990 return 0;
991}
992
Stephen Warren1e2082b2012-03-02 13:05:48 -0700993static inline const char *map_type(enum pinctrl_map_type type)
994{
995 static const char * const names[] = {
996 "INVALID",
997 "DUMMY_STATE",
998 "MUX_GROUP",
999 "CONFIGS_PIN",
1000 "CONFIGS_GROUP",
1001 };
1002
1003 if (type >= ARRAY_SIZE(names))
1004 return "UNKNOWN";
1005
1006 return names[type];
1007}
1008
Stephen Warren3eedb432012-02-23 17:04:40 -07001009static int pinctrl_maps_show(struct seq_file *s, void *what)
1010{
1011 struct pinctrl_maps *maps_node;
1012 int i;
1013 struct pinctrl_map const *map;
1014
1015 seq_puts(s, "Pinctrl maps:\n");
1016
Stephen Warren57b676f2012-03-02 13:05:44 -07001017 mutex_lock(&pinctrl_mutex);
1018
Stephen Warren3eedb432012-02-23 17:04:40 -07001019 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001020 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1021 map->dev_name, map->name, map_type(map->type),
1022 map->type);
1023
1024 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1025 seq_printf(s, "controlling device %s\n",
1026 map->ctrl_dev_name);
1027
1028 switch (map->type) {
1029 case PIN_MAP_TYPE_MUX_GROUP:
1030 pinmux_show_map(s, map);
1031 break;
1032 case PIN_MAP_TYPE_CONFIGS_PIN:
1033 case PIN_MAP_TYPE_CONFIGS_GROUP:
1034 pinconf_show_map(s, map);
1035 break;
1036 default:
1037 break;
1038 }
1039
1040 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001041 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001042
1043 mutex_unlock(&pinctrl_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001044
1045 return 0;
1046}
1047
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001048static int pinctrl_show(struct seq_file *s, void *what)
1049{
1050 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001051 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001052 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001053
1054 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001055
1056 mutex_lock(&pinctrl_mutex);
1057
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001058 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001059 seq_printf(s, "device: %s current state: %s\n",
1060 dev_name(p->dev),
1061 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001062
Stephen Warren6e5e9592012-03-02 13:05:47 -07001063 list_for_each_entry(state, &p->states, node) {
1064 seq_printf(s, " state: %s\n", state->name);
1065
1066 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001067 struct pinctrl_dev *pctldev = setting->pctldev;
1068
1069 seq_printf(s, " type: %s controller %s ",
1070 map_type(setting->type),
1071 pinctrl_dev_get_name(pctldev));
1072
1073 switch (setting->type) {
1074 case PIN_MAP_TYPE_MUX_GROUP:
1075 pinmux_show_setting(s, setting);
1076 break;
1077 case PIN_MAP_TYPE_CONFIGS_PIN:
1078 case PIN_MAP_TYPE_CONFIGS_GROUP:
1079 pinconf_show_setting(s, setting);
1080 break;
1081 default:
1082 break;
1083 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001084 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001085 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001086 }
1087
Stephen Warren57b676f2012-03-02 13:05:44 -07001088 mutex_unlock(&pinctrl_mutex);
1089
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001090 return 0;
1091}
1092
Linus Walleij2744e8a2011-05-02 20:50:54 +02001093static int pinctrl_pins_open(struct inode *inode, struct file *file)
1094{
1095 return single_open(file, pinctrl_pins_show, inode->i_private);
1096}
1097
1098static int pinctrl_groups_open(struct inode *inode, struct file *file)
1099{
1100 return single_open(file, pinctrl_groups_show, inode->i_private);
1101}
1102
1103static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1104{
1105 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1106}
1107
1108static int pinctrl_devices_open(struct inode *inode, struct file *file)
1109{
1110 return single_open(file, pinctrl_devices_show, NULL);
1111}
1112
Stephen Warren3eedb432012-02-23 17:04:40 -07001113static int pinctrl_maps_open(struct inode *inode, struct file *file)
1114{
1115 return single_open(file, pinctrl_maps_show, NULL);
1116}
1117
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001118static int pinctrl_open(struct inode *inode, struct file *file)
1119{
1120 return single_open(file, pinctrl_show, NULL);
1121}
1122
Linus Walleij2744e8a2011-05-02 20:50:54 +02001123static const struct file_operations pinctrl_pins_ops = {
1124 .open = pinctrl_pins_open,
1125 .read = seq_read,
1126 .llseek = seq_lseek,
1127 .release = single_release,
1128};
1129
1130static const struct file_operations pinctrl_groups_ops = {
1131 .open = pinctrl_groups_open,
1132 .read = seq_read,
1133 .llseek = seq_lseek,
1134 .release = single_release,
1135};
1136
1137static const struct file_operations pinctrl_gpioranges_ops = {
1138 .open = pinctrl_gpioranges_open,
1139 .read = seq_read,
1140 .llseek = seq_lseek,
1141 .release = single_release,
1142};
1143
1144static const struct file_operations pinctrl_devices_ops = {
1145 .open = pinctrl_devices_open,
1146 .read = seq_read,
1147 .llseek = seq_lseek,
1148 .release = single_release,
1149};
1150
Stephen Warren3eedb432012-02-23 17:04:40 -07001151static const struct file_operations pinctrl_maps_ops = {
1152 .open = pinctrl_maps_open,
1153 .read = seq_read,
1154 .llseek = seq_lseek,
1155 .release = single_release,
1156};
1157
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001158static const struct file_operations pinctrl_ops = {
1159 .open = pinctrl_open,
1160 .read = seq_read,
1161 .llseek = seq_lseek,
1162 .release = single_release,
1163};
1164
Linus Walleij2744e8a2011-05-02 20:50:54 +02001165static struct dentry *debugfs_root;
1166
1167static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1168{
Tony Lindgren02157162012-01-20 08:17:22 -08001169 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001170
Stephen Warren51cd24e2011-12-09 16:59:05 -07001171 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001172 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001173 pctldev->device_root = device_root;
1174
Linus Walleij2744e8a2011-05-02 20:50:54 +02001175 if (IS_ERR(device_root) || !device_root) {
1176 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001177 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001178 return;
1179 }
1180 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1181 device_root, pctldev, &pinctrl_pins_ops);
1182 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1183 device_root, pctldev, &pinctrl_groups_ops);
1184 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1185 device_root, pctldev, &pinctrl_gpioranges_ops);
1186 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +02001187 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001188}
1189
Tony Lindgren02157162012-01-20 08:17:22 -08001190static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1191{
1192 debugfs_remove_recursive(pctldev->device_root);
1193}
1194
Linus Walleij2744e8a2011-05-02 20:50:54 +02001195static void pinctrl_init_debugfs(void)
1196{
1197 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1198 if (IS_ERR(debugfs_root) || !debugfs_root) {
1199 pr_warn("failed to create debugfs directory\n");
1200 debugfs_root = NULL;
1201 return;
1202 }
1203
1204 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1205 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001206 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1207 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001208 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1209 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001210}
1211
1212#else /* CONFIG_DEBUG_FS */
1213
1214static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1215{
1216}
1217
1218static void pinctrl_init_debugfs(void)
1219{
1220}
1221
Tony Lindgren02157162012-01-20 08:17:22 -08001222static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1223{
1224}
1225
Linus Walleij2744e8a2011-05-02 20:50:54 +02001226#endif
1227
1228/**
1229 * pinctrl_register() - register a pin controller device
1230 * @pctldesc: descriptor for this pin controller
1231 * @dev: parent device for this pin controller
1232 * @driver_data: private pin controller data for this pin controller
1233 */
1234struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1235 struct device *dev, void *driver_data)
1236{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001237 struct pinctrl_dev *pctldev;
1238 int ret;
1239
1240 if (pctldesc == NULL)
1241 return NULL;
1242 if (pctldesc->name == NULL)
1243 return NULL;
1244
Stephen Warren02f5b982012-02-22 14:26:00 -07001245 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001246 if (pctldev == NULL) {
1247 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001248 return NULL;
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001249 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001250
1251 /* Initialize pin control device struct */
1252 pctldev->owner = pctldesc->owner;
1253 pctldev->desc = pctldesc;
1254 pctldev->driver_data = driver_data;
1255 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001256 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001257 pctldev->dev = dev;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001258
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001259 /* If we're implementing pinmuxing, check the ops for sanity */
1260 if (pctldesc->pmxops) {
1261 ret = pinmux_check_ops(pctldev);
1262 if (ret) {
1263 pr_err("%s pinmux ops lacks necessary functions\n",
1264 pctldesc->name);
1265 goto out_err;
1266 }
1267 }
1268
1269 /* If we're implementing pinconfig, check the ops for sanity */
1270 if (pctldesc->confops) {
1271 ret = pinconf_check_ops(pctldev);
1272 if (ret) {
1273 pr_err("%s pin config ops lacks necessary functions\n",
1274 pctldesc->name);
1275 goto out_err;
1276 }
1277 }
1278
Linus Walleij2744e8a2011-05-02 20:50:54 +02001279 /* Register all the pins */
1280 pr_debug("try to register %d pins on %s...\n",
1281 pctldesc->npins, pctldesc->name);
1282 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1283 if (ret) {
1284 pr_err("error during pin registration\n");
1285 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1286 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001287 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001288 }
1289
Stephen Warren57b676f2012-03-02 13:05:44 -07001290 mutex_lock(&pinctrl_mutex);
1291
Stephen Warren8b9c1392012-02-19 23:45:42 -07001292 list_add_tail(&pctldev->node, &pinctrldev_list);
Stephen Warren57b676f2012-03-02 13:05:44 -07001293
Stephen Warren6e5e9592012-03-02 13:05:47 -07001294 pctldev->p = pinctrl_get_locked(pctldev->dev);
1295 if (!IS_ERR(pctldev->p)) {
1296 struct pinctrl_state *s =
1297 pinctrl_lookup_state_locked(pctldev->p,
1298 PINCTRL_STATE_DEFAULT);
1299 if (!IS_ERR(s))
1300 pinctrl_select_state_locked(pctldev->p, s);
1301 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001302
1303 mutex_unlock(&pinctrl_mutex);
1304
Stephen Warren2304b472012-02-22 14:26:01 -07001305 pinctrl_init_device_debugfs(pctldev);
1306
Linus Walleij2744e8a2011-05-02 20:50:54 +02001307 return pctldev;
1308
Stephen Warren51cd24e2011-12-09 16:59:05 -07001309out_err:
1310 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001311 return NULL;
1312}
1313EXPORT_SYMBOL_GPL(pinctrl_register);
1314
1315/**
1316 * pinctrl_unregister() - unregister pinmux
1317 * @pctldev: pin controller to unregister
1318 *
1319 * Called by pinmux drivers to unregister a pinmux.
1320 */
1321void pinctrl_unregister(struct pinctrl_dev *pctldev)
1322{
1323 if (pctldev == NULL)
1324 return;
1325
Tony Lindgren02157162012-01-20 08:17:22 -08001326 pinctrl_remove_device_debugfs(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001327
1328 mutex_lock(&pinctrl_mutex);
1329
Stephen Warren6e5e9592012-03-02 13:05:47 -07001330 if (!IS_ERR(pctldev->p))
1331 pinctrl_put_locked(pctldev->p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -07001332
Linus Walleij2744e8a2011-05-02 20:50:54 +02001333 /* TODO: check that no pinmuxes are still active? */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001334 list_del(&pctldev->node);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001335 /* Destroy descriptor tree */
1336 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1337 pctldev->desc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001338 kfree(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001339
1340 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001341}
1342EXPORT_SYMBOL_GPL(pinctrl_unregister);
1343
1344static int __init pinctrl_init(void)
1345{
1346 pr_info("initialized pinctrl subsystem\n");
1347 pinctrl_init_debugfs();
1348 return 0;
1349}
1350
1351/* init early since many drivers really need to initialized pinmux early */
1352core_initcall(pinctrl_init);