blob: 832f71dcd8c41027330a0097582a6d71ff821be7 [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"
Stephen Warren57291ce2012-03-23 10:29:46 -060029#include "devicetree.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020030#include "pinmux.h"
Linus Walleijae6b4d82011-10-19 18:14:33 +020031#include "pinconf.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020032
Linus Walleijbefe5bd2012-02-09 19:47:48 +010033/**
Stephen Warrenb2b3e662012-02-19 23:45:43 -070034 * struct pinctrl_maps - a list item containing part of the mapping table
35 * @node: mapping table list node
36 * @maps: array of mapping table entries
37 * @num_maps: the number of entries in @maps
38 */
39struct pinctrl_maps {
40 struct list_head node;
41 struct pinctrl_map const *maps;
42 unsigned num_maps;
43};
44
Stephen Warren57b676f2012-03-02 13:05:44 -070045/* Mutex taken by all entry points */
46DEFINE_MUTEX(pinctrl_mutex);
47
48/* Global list of pin control devices (struct pinctrl_dev) */
Stephen Warren57291ce2012-03-23 10:29:46 -060049LIST_HEAD(pinctrldev_list);
Linus Walleij2744e8a2011-05-02 20:50:54 +020050
Stephen Warren57b676f2012-03-02 13:05:44 -070051/* List of pin controller handles (struct pinctrl) */
Linus Walleijbefe5bd2012-02-09 19:47:48 +010052static LIST_HEAD(pinctrl_list);
53
Stephen Warren57b676f2012-03-02 13:05:44 -070054/* List of pinctrl maps (struct pinctrl_maps) */
Stephen Warrenb2b3e662012-02-19 23:45:43 -070055static LIST_HEAD(pinctrl_maps);
56
57#define for_each_maps(_maps_node_, _i_, _map_) \
58 list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
59 for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
60 _i_ < _maps_node_->num_maps; \
61 i++, _map_ = &_maps_node_->maps[_i_])
Linus Walleijbefe5bd2012-02-09 19:47:48 +010062
Linus Walleij2744e8a2011-05-02 20:50:54 +020063const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
64{
65 /* We're not allowed to register devices without name */
66 return pctldev->desc->name;
67}
68EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
69
70void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
71{
72 return pctldev->driver_data;
73}
74EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
75
76/**
Linus Walleij9dfac4f2012-02-01 18:02:47 +010077 * get_pinctrl_dev_from_devname() - look up pin controller device
78 * @devname: the name of a device instance, as returned by dev_name()
Linus Walleij2744e8a2011-05-02 20:50:54 +020079 *
80 * Looks up a pin control device matching a certain device name or pure device
81 * pointer, the pure device pointer will take precedence.
82 */
Linus Walleij9dfac4f2012-02-01 18:02:47 +010083struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
Linus Walleij2744e8a2011-05-02 20:50:54 +020084{
85 struct pinctrl_dev *pctldev = NULL;
86 bool found = false;
87
Linus Walleij9dfac4f2012-02-01 18:02:47 +010088 if (!devname)
89 return NULL;
90
Linus Walleij2744e8a2011-05-02 20:50:54 +020091 list_for_each_entry(pctldev, &pinctrldev_list, node) {
Linus Walleij9dfac4f2012-02-01 18:02:47 +010092 if (!strcmp(dev_name(pctldev->dev), devname)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +020093 /* Matched on device name */
94 found = true;
95 break;
96 }
97 }
Linus Walleij2744e8a2011-05-02 20:50:54 +020098
99 return found ? pctldev : NULL;
100}
101
Linus Walleij2744e8a2011-05-02 20:50:54 +0200102/**
Linus Walleijae6b4d82011-10-19 18:14:33 +0200103 * pin_get_from_name() - look up a pin number from a name
104 * @pctldev: the pin control device to lookup the pin on
105 * @name: the name of the pin to look up
106 */
107int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
108{
Chanho Park706e8522012-01-03 16:47:50 +0900109 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200110
Chanho Park706e8522012-01-03 16:47:50 +0900111 /* The pin number can be retrived from the pin controller descriptor */
112 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200113 struct pin_desc *desc;
114
Chanho Park706e8522012-01-03 16:47:50 +0900115 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200116 desc = pin_desc_get(pctldev, pin);
117 /* Pin space may be sparse */
118 if (desc == NULL)
119 continue;
120 if (desc->name && !strcmp(name, desc->name))
121 return pin;
122 }
123
124 return -EINVAL;
125}
126
127/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200128 * pin_is_valid() - check if pin exists on controller
129 * @pctldev: the pin control device to check the pin on
130 * @pin: pin to check, use the local pin controller index number
131 *
132 * This tells us whether a certain pin exist on a certain pin controller or
133 * not. Pin lists may be sparse, so some pins may not exist.
134 */
135bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
136{
137 struct pin_desc *pindesc;
138
139 if (pin < 0)
140 return false;
141
Stephen Warren57b676f2012-03-02 13:05:44 -0700142 mutex_lock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200143 pindesc = pin_desc_get(pctldev, pin);
Stephen Warren57b676f2012-03-02 13:05:44 -0700144 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200145
Stephen Warren57b676f2012-03-02 13:05:44 -0700146 return pindesc != NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200147}
148EXPORT_SYMBOL_GPL(pin_is_valid);
149
150/* Deletes a range of pin descriptors */
151static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
152 const struct pinctrl_pin_desc *pins,
153 unsigned num_pins)
154{
155 int i;
156
Linus Walleij2744e8a2011-05-02 20:50:54 +0200157 for (i = 0; i < num_pins; i++) {
158 struct pin_desc *pindesc;
159
160 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
161 pins[i].number);
162 if (pindesc != NULL) {
163 radix_tree_delete(&pctldev->pin_desc_tree,
164 pins[i].number);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100165 if (pindesc->dynamic_name)
166 kfree(pindesc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200167 }
168 kfree(pindesc);
169 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200170}
171
172static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
173 unsigned number, const char *name)
174{
175 struct pin_desc *pindesc;
176
177 pindesc = pin_desc_get(pctldev, number);
178 if (pindesc != NULL) {
179 pr_err("pin %d already registered on %s\n", number,
180 pctldev->desc->name);
181 return -EINVAL;
182 }
183
184 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700185 if (pindesc == NULL) {
186 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200187 return -ENOMEM;
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700188 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200189
Linus Walleij2744e8a2011-05-02 20:50:54 +0200190 /* Set owner */
191 pindesc->pctldev = pctldev;
192
Stephen Warren9af1e442011-10-19 16:19:27 -0600193 /* Copy basic pin info */
Linus Walleij8dc6ae42012-02-01 18:11:40 +0100194 if (name) {
Linus Walleijca53c5f2011-12-14 20:33:37 +0100195 pindesc->name = name;
196 } else {
197 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
198 if (pindesc->name == NULL)
199 return -ENOMEM;
200 pindesc->dynamic_name = true;
201 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200202
Linus Walleij2744e8a2011-05-02 20:50:54 +0200203 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200204 pr_debug("registered pin %d (%s) on %s\n",
Linus Walleijca53c5f2011-12-14 20:33:37 +0100205 number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200206 return 0;
207}
208
209static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
210 struct pinctrl_pin_desc const *pins,
211 unsigned num_descs)
212{
213 unsigned i;
214 int ret = 0;
215
216 for (i = 0; i < num_descs; i++) {
217 ret = pinctrl_register_one_pin(pctldev,
218 pins[i].number, pins[i].name);
219 if (ret)
220 return ret;
221 }
222
223 return 0;
224}
225
226/**
227 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
228 * @pctldev: pin controller device to check
229 * @gpio: gpio pin to check taken from the global GPIO pin space
230 *
231 * Tries to match a GPIO pin number to the ranges handled by a certain pin
232 * controller, return the range or NULL
233 */
234static struct pinctrl_gpio_range *
235pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
236{
237 struct pinctrl_gpio_range *range = NULL;
238
239 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200240 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
241 /* Check if we're in the valid range */
242 if (gpio >= range->base &&
243 gpio < range->base + range->npins) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200244 return range;
245 }
246 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200247
248 return NULL;
249}
250
251/**
252 * pinctrl_get_device_gpio_range() - find device for GPIO range
253 * @gpio: the pin to locate the pin controller for
254 * @outdev: the pin control device if found
255 * @outrange: the GPIO range if found
256 *
257 * Find the pin controller handling a certain GPIO pin from the pinspace of
258 * the GPIO subsystem, return the device and the matching GPIO range. Returns
259 * negative if the GPIO range could not be found in any device.
260 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700261static int pinctrl_get_device_gpio_range(unsigned gpio,
262 struct pinctrl_dev **outdev,
263 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200264{
265 struct pinctrl_dev *pctldev = NULL;
266
267 /* Loop over the pin controllers */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200268 list_for_each_entry(pctldev, &pinctrldev_list, node) {
269 struct pinctrl_gpio_range *range;
270
271 range = pinctrl_match_gpio_range(pctldev, gpio);
272 if (range != NULL) {
273 *outdev = pctldev;
274 *outrange = range;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200275 return 0;
276 }
277 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200278
279 return -EINVAL;
280}
281
282/**
283 * pinctrl_add_gpio_range() - register a GPIO range for a controller
284 * @pctldev: pin controller device to add the range to
285 * @range: the GPIO range to add
286 *
287 * This adds a range of GPIOs to be handled by a certain pin controller. Call
288 * this to register handled ranges after registering your pin controller.
289 */
290void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
291 struct pinctrl_gpio_range *range)
292{
Stephen Warren57b676f2012-03-02 13:05:44 -0700293 mutex_lock(&pinctrl_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700294 list_add_tail(&range->node, &pctldev->gpio_ranges);
Stephen Warren57b676f2012-03-02 13:05:44 -0700295 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200296}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700297EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200298
299/**
300 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
301 * @pctldev: pin controller device to remove the range from
302 * @range: the GPIO range to remove
303 */
304void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
305 struct pinctrl_gpio_range *range)
306{
Stephen Warren57b676f2012-03-02 13:05:44 -0700307 mutex_lock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200308 list_del(&range->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700309 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200310}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700311EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200312
Linus Walleij7afde8b2011-10-19 17:07:16 +0200313/**
314 * pinctrl_get_group_selector() - returns the group selector for a group
315 * @pctldev: the pin controller handling the group
316 * @pin_group: the pin group to look up
317 */
318int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
319 const char *pin_group)
320{
321 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
322 unsigned group_selector = 0;
323
324 while (pctlops->list_groups(pctldev, group_selector) >= 0) {
325 const char *gname = pctlops->get_group_name(pctldev,
326 group_selector);
327 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700328 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200329 "found group selector %u for %s\n",
330 group_selector,
331 pin_group);
332 return group_selector;
333 }
334
335 group_selector++;
336 }
337
Stephen Warren51cd24e2011-12-09 16:59:05 -0700338 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200339 pin_group);
340
341 return -EINVAL;
342}
343
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100344/**
345 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
346 * @gpio: the GPIO pin number from the GPIO subsystem number space
347 *
348 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
349 * as part of their gpio_request() semantics, platforms and individual drivers
350 * shall *NOT* request GPIO pins to be muxed in.
351 */
352int pinctrl_request_gpio(unsigned gpio)
353{
354 struct pinctrl_dev *pctldev;
355 struct pinctrl_gpio_range *range;
356 int ret;
357 int pin;
358
Stephen Warren57b676f2012-03-02 13:05:44 -0700359 mutex_lock(&pinctrl_mutex);
360
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100361 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700362 if (ret) {
363 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100364 return -EINVAL;
Stephen Warren57b676f2012-03-02 13:05:44 -0700365 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100366
367 /* Convert to the pin controllers number space */
368 pin = gpio - range->base + range->pin_base;
369
Stephen Warren57b676f2012-03-02 13:05:44 -0700370 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
371
372 mutex_unlock(&pinctrl_mutex);
373 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100374}
375EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
376
377/**
378 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
379 * @gpio: the GPIO pin number from the GPIO subsystem number space
380 *
381 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
382 * as part of their gpio_free() semantics, platforms and individual drivers
383 * shall *NOT* request GPIO pins to be muxed out.
384 */
385void pinctrl_free_gpio(unsigned gpio)
386{
387 struct pinctrl_dev *pctldev;
388 struct pinctrl_gpio_range *range;
389 int ret;
390 int pin;
391
Stephen Warren57b676f2012-03-02 13:05:44 -0700392 mutex_lock(&pinctrl_mutex);
393
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100394 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700395 if (ret) {
396 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100397 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700398 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100399
400 /* Convert to the pin controllers number space */
401 pin = gpio - range->base + range->pin_base;
402
Stephen Warren57b676f2012-03-02 13:05:44 -0700403 pinmux_free_gpio(pctldev, pin, range);
404
405 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100406}
407EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
408
409static int pinctrl_gpio_direction(unsigned gpio, bool input)
410{
411 struct pinctrl_dev *pctldev;
412 struct pinctrl_gpio_range *range;
413 int ret;
414 int pin;
415
416 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
417 if (ret)
418 return ret;
419
420 /* Convert to the pin controllers number space */
421 pin = gpio - range->base + range->pin_base;
422
423 return pinmux_gpio_direction(pctldev, range, pin, input);
424}
425
426/**
427 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
428 * @gpio: the GPIO pin number from the GPIO subsystem number space
429 *
430 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
431 * as part of their gpio_direction_input() semantics, platforms and individual
432 * drivers shall *NOT* touch pin control GPIO calls.
433 */
434int pinctrl_gpio_direction_input(unsigned gpio)
435{
Stephen Warren57b676f2012-03-02 13:05:44 -0700436 int ret;
437 mutex_lock(&pinctrl_mutex);
438 ret = pinctrl_gpio_direction(gpio, true);
439 mutex_unlock(&pinctrl_mutex);
440 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100441}
442EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
443
444/**
445 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
446 * @gpio: the GPIO pin number from the GPIO subsystem number space
447 *
448 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
449 * as part of their gpio_direction_output() semantics, platforms and individual
450 * drivers shall *NOT* touch pin control GPIO calls.
451 */
452int pinctrl_gpio_direction_output(unsigned gpio)
453{
Stephen Warren57b676f2012-03-02 13:05:44 -0700454 int ret;
455 mutex_lock(&pinctrl_mutex);
456 ret = pinctrl_gpio_direction(gpio, false);
457 mutex_unlock(&pinctrl_mutex);
458 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100459}
460EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
461
Stephen Warren6e5e9592012-03-02 13:05:47 -0700462static struct pinctrl_state *find_state(struct pinctrl *p,
463 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100464{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700465 struct pinctrl_state *state;
466
467 list_for_each_entry(state, &p->states, node)
468 if (!strcmp(state->name, name))
469 return state;
470
471 return NULL;
472}
473
474static struct pinctrl_state *create_state(struct pinctrl *p,
475 const char *name)
476{
477 struct pinctrl_state *state;
478
479 state = kzalloc(sizeof(*state), GFP_KERNEL);
480 if (state == NULL) {
481 dev_err(p->dev,
482 "failed to alloc struct pinctrl_state\n");
483 return ERR_PTR(-ENOMEM);
484 }
485
486 state->name = name;
487 INIT_LIST_HEAD(&state->settings);
488
489 list_add_tail(&state->node, &p->states);
490
491 return state;
492}
493
494static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
495{
496 struct pinctrl_state *state;
497 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700498 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700499
500 state = find_state(p, map->name);
501 if (!state)
502 state = create_state(p, map->name);
503 if (IS_ERR(state))
504 return PTR_ERR(state);
505
Stephen Warren1e2082b2012-03-02 13:05:48 -0700506 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
507 return 0;
508
Stephen Warren6e5e9592012-03-02 13:05:47 -0700509 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
510 if (setting == NULL) {
511 dev_err(p->dev,
512 "failed to alloc struct pinctrl_setting\n");
513 return -ENOMEM;
514 }
515
Stephen Warren1e2082b2012-03-02 13:05:48 -0700516 setting->type = map->type;
517
Stephen Warren6e5e9592012-03-02 13:05:47 -0700518 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
519 if (setting->pctldev == NULL) {
520 dev_err(p->dev, "unknown pinctrl device %s in map entry",
521 map->ctrl_dev_name);
522 kfree(setting);
523 /* Eventually, this should trigger deferred probe */
524 return -ENODEV;
525 }
526
Stephen Warren1e2082b2012-03-02 13:05:48 -0700527 switch (map->type) {
528 case PIN_MAP_TYPE_MUX_GROUP:
529 ret = pinmux_map_to_setting(map, setting);
530 break;
531 case PIN_MAP_TYPE_CONFIGS_PIN:
532 case PIN_MAP_TYPE_CONFIGS_GROUP:
533 ret = pinconf_map_to_setting(map, setting);
534 break;
535 default:
536 ret = -EINVAL;
537 break;
538 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700539 if (ret < 0) {
540 kfree(setting);
541 return ret;
542 }
543
544 list_add_tail(&setting->node, &state->settings);
545
546 return 0;
547}
548
549static struct pinctrl *find_pinctrl(struct device *dev)
550{
551 struct pinctrl *p;
552
Stephen Warren1e2082b2012-03-02 13:05:48 -0700553 list_for_each_entry(p, &pinctrl_list, node)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700554 if (p->dev == dev)
555 return p;
556
557 return NULL;
558}
559
560static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
561
562static struct pinctrl *create_pinctrl(struct device *dev)
563{
564 struct pinctrl *p;
565 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700566 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100567 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700568 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700569 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100570
571 /*
572 * create the state cookie holder struct pinctrl for each
573 * mapping, this is what consumers will get when requesting
574 * a pin control handle with pinctrl_get()
575 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700576 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700577 if (p == NULL) {
578 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100579 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700580 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700581 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700582 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -0600583 INIT_LIST_HEAD(&p->dt_maps);
584
585 ret = pinctrl_dt_to_map(p);
586 if (ret < 0) {
587 kfree(p);
588 return ERR_PTR(ret);
589 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700590
591 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100592
593 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700594 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -0700595 /* Map must be for this device */
596 if (strcmp(map->dev_name, devname))
597 continue;
598
Stephen Warren6e5e9592012-03-02 13:05:47 -0700599 ret = add_setting(p, map);
600 if (ret < 0) {
601 pinctrl_put_locked(p, false);
602 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100603 }
604 }
605
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100606 /* Add the pinmux to the global list */
Stephen Warren8b9c1392012-02-19 23:45:42 -0700607 list_add_tail(&p->node, &pinctrl_list);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100608
609 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700610}
Stephen Warren7ecdb162012-03-02 13:05:45 -0700611
Stephen Warren6e5e9592012-03-02 13:05:47 -0700612static struct pinctrl *pinctrl_get_locked(struct device *dev)
613{
614 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700615
Stephen Warren6e5e9592012-03-02 13:05:47 -0700616 if (WARN_ON(!dev))
617 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700618
Stephen Warren6e5e9592012-03-02 13:05:47 -0700619 p = find_pinctrl(dev);
620 if (p != NULL)
621 return ERR_PTR(-EBUSY);
622
623 p = create_pinctrl(dev);
624 if (IS_ERR(p))
625 return p;
626
627 return p;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100628}
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700629
630/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700631 * pinctrl_get() - retrieves the pinctrl handle for a device
632 * @dev: the device to obtain the handle for
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700633 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700634struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700635{
636 struct pinctrl *p;
637
Stephen Warren57b676f2012-03-02 13:05:44 -0700638 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700639 p = pinctrl_get_locked(dev);
Stephen Warren57b676f2012-03-02 13:05:44 -0700640 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700641
642 return p;
643}
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100644EXPORT_SYMBOL_GPL(pinctrl_get);
645
Stephen Warren6e5e9592012-03-02 13:05:47 -0700646static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -0700647{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700648 struct pinctrl_state *state, *n1;
649 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700650
Stephen Warren6e5e9592012-03-02 13:05:47 -0700651 list_for_each_entry_safe(state, n1, &p->states, node) {
652 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700653 switch (setting->type) {
654 case PIN_MAP_TYPE_MUX_GROUP:
655 if (state == p->state)
656 pinmux_disable_setting(setting);
657 pinmux_free_setting(setting);
658 break;
659 case PIN_MAP_TYPE_CONFIGS_PIN:
660 case PIN_MAP_TYPE_CONFIGS_GROUP:
661 pinconf_free_setting(setting);
662 break;
663 default:
664 break;
665 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700666 list_del(&setting->node);
667 kfree(setting);
668 }
669 list_del(&state->node);
670 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700671 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700672
Stephen Warren57291ce2012-03-23 10:29:46 -0600673 pinctrl_dt_free_maps(p);
674
Stephen Warren6e5e9592012-03-02 13:05:47 -0700675 if (inlist)
676 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700677 kfree(p);
678}
679
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100680/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700681 * pinctrl_put() - release a previously claimed pinctrl handle
682 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100683 */
684void pinctrl_put(struct pinctrl *p)
685{
Stephen Warren57b676f2012-03-02 13:05:44 -0700686 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700687 pinctrl_put_locked(p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -0700688 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100689}
690EXPORT_SYMBOL_GPL(pinctrl_put);
691
Stephen Warren6e5e9592012-03-02 13:05:47 -0700692static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
693 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -0700694{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700695 struct pinctrl_state *state;
696
697 state = find_state(p, name);
698 if (!state)
699 return ERR_PTR(-ENODEV);
700
701 return state;
702}
703
704/**
705 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
706 * @p: the pinctrl handle to retrieve the state from
707 * @name: the state name to retrieve
708 */
709struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
710{
711 struct pinctrl_state *s;
712
713 mutex_lock(&pinctrl_mutex);
714 s = pinctrl_lookup_state_locked(p, name);
715 mutex_unlock(&pinctrl_mutex);
716
717 return s;
718}
719EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
720
721static int pinctrl_select_state_locked(struct pinctrl *p,
722 struct pinctrl_state *state)
723{
724 struct pinctrl_setting *setting, *setting2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700725 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700726
Stephen Warren6e5e9592012-03-02 13:05:47 -0700727 if (p->state == state)
728 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700729
Stephen Warren6e5e9592012-03-02 13:05:47 -0700730 if (p->state) {
731 /*
732 * The set of groups with a mux configuration in the old state
733 * may not be identical to the set of groups with a mux setting
734 * in the new state. While this might be unusual, it's entirely
735 * possible for the "user"-supplied mapping table to be written
736 * that way. For each group that was configured in the old state
737 * but not in the new state, this code puts that group into a
738 * safe/disabled state.
739 */
740 list_for_each_entry(setting, &p->state->settings, node) {
741 bool found = false;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700742 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
743 continue;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700744 list_for_each_entry(setting2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700745 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
746 continue;
747 if (setting2->data.mux.group ==
748 setting->data.mux.group) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700749 found = true;
750 break;
751 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700752 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700753 if (!found)
754 pinmux_disable_setting(setting);
755 }
756 }
757
758 p->state = state;
759
760 /* Apply all the settings for the new state */
761 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700762 switch (setting->type) {
763 case PIN_MAP_TYPE_MUX_GROUP:
764 ret = pinmux_enable_setting(setting);
765 break;
766 case PIN_MAP_TYPE_CONFIGS_PIN:
767 case PIN_MAP_TYPE_CONFIGS_GROUP:
768 ret = pinconf_apply_setting(setting);
769 break;
770 default:
771 ret = -EINVAL;
772 break;
773 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700774 if (ret < 0) {
775 /* FIXME: Difficult to return to prev state */
776 return ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700777 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700778 }
779
Stephen Warren7ecdb162012-03-02 13:05:45 -0700780 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700781}
782
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100783/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700784 * pinctrl_select() - select/activate/program a pinctrl state to HW
785 * @p: the pinctrl handle for the device that requests configuratio
786 * @state: the state handle to select/activate/program
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100787 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700788int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100789{
Stephen Warren57b676f2012-03-02 13:05:44 -0700790 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700791
Stephen Warren57b676f2012-03-02 13:05:44 -0700792 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700793 ret = pinctrl_select_state_locked(p, state);
Stephen Warren57b676f2012-03-02 13:05:44 -0700794 mutex_unlock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700795
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100796 return ret;
797}
Stephen Warren6e5e9592012-03-02 13:05:47 -0700798EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100799
Stephen Warren57291ce2012-03-23 10:29:46 -0600800int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
801 bool dup, bool locked)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100802{
Stephen Warren1e2082b2012-03-02 13:05:48 -0700803 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700804 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100805
806 pr_debug("add %d pinmux maps\n", num_maps);
807
808 /* First sanity check the new mapping */
809 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700810 if (!maps[i].dev_name) {
811 pr_err("failed to register map %s (%d): no device given\n",
812 maps[i].name, i);
813 return -EINVAL;
814 }
815
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100816 if (!maps[i].name) {
817 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700818 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100819 return -EINVAL;
820 }
821
Stephen Warren1e2082b2012-03-02 13:05:48 -0700822 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
823 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100824 pr_err("failed to register map %s (%d): no pin control device given\n",
825 maps[i].name, i);
826 return -EINVAL;
827 }
828
Stephen Warren1e2082b2012-03-02 13:05:48 -0700829 switch (maps[i].type) {
830 case PIN_MAP_TYPE_DUMMY_STATE:
831 break;
832 case PIN_MAP_TYPE_MUX_GROUP:
833 ret = pinmux_validate_map(&maps[i], i);
834 if (ret < 0)
835 return 0;
836 break;
837 case PIN_MAP_TYPE_CONFIGS_PIN:
838 case PIN_MAP_TYPE_CONFIGS_GROUP:
839 ret = pinconf_validate_map(&maps[i], i);
840 if (ret < 0)
841 return 0;
842 break;
843 default:
844 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700845 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -0700846 return -EINVAL;
847 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100848 }
849
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700850 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
851 if (!maps_node) {
852 pr_err("failed to alloc struct pinctrl_maps\n");
853 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100854 }
855
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700856 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -0600857 if (dup) {
858 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
859 GFP_KERNEL);
860 if (!maps_node->maps) {
861 pr_err("failed to duplicate mapping table\n");
862 kfree(maps_node);
863 return -ENOMEM;
864 }
865 } else {
866 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700867 }
868
Stephen Warren57291ce2012-03-23 10:29:46 -0600869 if (!locked)
870 mutex_lock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700871 list_add_tail(&maps_node->node, &pinctrl_maps);
Stephen Warren57291ce2012-03-23 10:29:46 -0600872 if (!locked)
873 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700874
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100875 return 0;
876}
877
Stephen Warren57291ce2012-03-23 10:29:46 -0600878/**
879 * pinctrl_register_mappings() - register a set of pin controller mappings
880 * @maps: the pincontrol mappings table to register. This should probably be
881 * marked with __initdata so it can be discarded after boot. This
882 * function will perform a shallow copy for the mapping entries.
883 * @num_maps: the number of maps in the mapping table
884 */
885int pinctrl_register_mappings(struct pinctrl_map const *maps,
886 unsigned num_maps)
887{
888 return pinctrl_register_map(maps, num_maps, true, false);
889}
890
891void pinctrl_unregister_map(struct pinctrl_map const *map)
892{
893 struct pinctrl_maps *maps_node;
894
895 list_for_each_entry(maps_node, &pinctrl_maps, node) {
896 if (maps_node->maps == map) {
897 list_del(&maps_node->node);
898 return;
899 }
900 }
901}
902
Linus Walleij2744e8a2011-05-02 20:50:54 +0200903#ifdef CONFIG_DEBUG_FS
904
905static int pinctrl_pins_show(struct seq_file *s, void *what)
906{
907 struct pinctrl_dev *pctldev = s->private;
908 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +0900909 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200910
911 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200912
Stephen Warren57b676f2012-03-02 13:05:44 -0700913 mutex_lock(&pinctrl_mutex);
914
Chanho Park706e8522012-01-03 16:47:50 +0900915 /* The pin number can be retrived from the pin controller descriptor */
916 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200917 struct pin_desc *desc;
918
Chanho Park706e8522012-01-03 16:47:50 +0900919 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200920 desc = pin_desc_get(pctldev, pin);
921 /* Pin space may be sparse */
922 if (desc == NULL)
923 continue;
924
925 seq_printf(s, "pin %d (%s) ", pin,
926 desc->name ? desc->name : "unnamed");
927
928 /* Driver-specific info per pin */
929 if (ops->pin_dbg_show)
930 ops->pin_dbg_show(pctldev, s, pin);
931
932 seq_puts(s, "\n");
933 }
934
Stephen Warren57b676f2012-03-02 13:05:44 -0700935 mutex_unlock(&pinctrl_mutex);
936
Linus Walleij2744e8a2011-05-02 20:50:54 +0200937 return 0;
938}
939
940static int pinctrl_groups_show(struct seq_file *s, void *what)
941{
942 struct pinctrl_dev *pctldev = s->private;
943 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
944 unsigned selector = 0;
945
Stephen Warren57b676f2012-03-02 13:05:44 -0700946 mutex_lock(&pinctrl_mutex);
947
Linus Walleij2744e8a2011-05-02 20:50:54 +0200948 seq_puts(s, "registered pin groups:\n");
949 while (ops->list_groups(pctldev, selector) >= 0) {
Stephen Warrena5818a82011-10-19 16:19:25 -0600950 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200951 unsigned num_pins;
952 const char *gname = ops->get_group_name(pctldev, selector);
953 int ret;
954 int i;
955
956 ret = ops->get_group_pins(pctldev, selector,
957 &pins, &num_pins);
958 if (ret)
959 seq_printf(s, "%s [ERROR GETTING PINS]\n",
960 gname);
961 else {
962 seq_printf(s, "group: %s, pins = [ ", gname);
963 for (i = 0; i < num_pins; i++)
964 seq_printf(s, "%d ", pins[i]);
965 seq_puts(s, "]\n");
966 }
967 selector++;
968 }
969
Stephen Warren57b676f2012-03-02 13:05:44 -0700970 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200971
972 return 0;
973}
974
975static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
976{
977 struct pinctrl_dev *pctldev = s->private;
978 struct pinctrl_gpio_range *range = NULL;
979
980 seq_puts(s, "GPIO ranges handled:\n");
981
Stephen Warren57b676f2012-03-02 13:05:44 -0700982 mutex_lock(&pinctrl_mutex);
983
Linus Walleij2744e8a2011-05-02 20:50:54 +0200984 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200985 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Linus Walleij75d66422011-11-16 09:58:51 +0100986 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
987 range->id, range->name,
988 range->base, (range->base + range->npins - 1),
989 range->pin_base,
990 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +0200991 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700992
993 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200994
995 return 0;
996}
997
998static int pinctrl_devices_show(struct seq_file *s, void *what)
999{
1000 struct pinctrl_dev *pctldev;
1001
Linus Walleijae6b4d82011-10-19 18:14:33 +02001002 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001003
1004 mutex_lock(&pinctrl_mutex);
1005
Linus Walleij2744e8a2011-05-02 20:50:54 +02001006 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1007 seq_printf(s, "%s ", pctldev->desc->name);
1008 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001009 seq_puts(s, "yes ");
1010 else
1011 seq_puts(s, "no ");
1012 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001013 seq_puts(s, "yes");
1014 else
1015 seq_puts(s, "no");
1016 seq_puts(s, "\n");
1017 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001018
1019 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001020
1021 return 0;
1022}
1023
Stephen Warren1e2082b2012-03-02 13:05:48 -07001024static inline const char *map_type(enum pinctrl_map_type type)
1025{
1026 static const char * const names[] = {
1027 "INVALID",
1028 "DUMMY_STATE",
1029 "MUX_GROUP",
1030 "CONFIGS_PIN",
1031 "CONFIGS_GROUP",
1032 };
1033
1034 if (type >= ARRAY_SIZE(names))
1035 return "UNKNOWN";
1036
1037 return names[type];
1038}
1039
Stephen Warren3eedb432012-02-23 17:04:40 -07001040static int pinctrl_maps_show(struct seq_file *s, void *what)
1041{
1042 struct pinctrl_maps *maps_node;
1043 int i;
1044 struct pinctrl_map const *map;
1045
1046 seq_puts(s, "Pinctrl maps:\n");
1047
Stephen Warren57b676f2012-03-02 13:05:44 -07001048 mutex_lock(&pinctrl_mutex);
1049
Stephen Warren3eedb432012-02-23 17:04:40 -07001050 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001051 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1052 map->dev_name, map->name, map_type(map->type),
1053 map->type);
1054
1055 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1056 seq_printf(s, "controlling device %s\n",
1057 map->ctrl_dev_name);
1058
1059 switch (map->type) {
1060 case PIN_MAP_TYPE_MUX_GROUP:
1061 pinmux_show_map(s, map);
1062 break;
1063 case PIN_MAP_TYPE_CONFIGS_PIN:
1064 case PIN_MAP_TYPE_CONFIGS_GROUP:
1065 pinconf_show_map(s, map);
1066 break;
1067 default:
1068 break;
1069 }
1070
1071 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001072 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001073
1074 mutex_unlock(&pinctrl_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001075
1076 return 0;
1077}
1078
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001079static int pinctrl_show(struct seq_file *s, void *what)
1080{
1081 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001082 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001083 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001084
1085 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001086
1087 mutex_lock(&pinctrl_mutex);
1088
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001089 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001090 seq_printf(s, "device: %s current state: %s\n",
1091 dev_name(p->dev),
1092 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001093
Stephen Warren6e5e9592012-03-02 13:05:47 -07001094 list_for_each_entry(state, &p->states, node) {
1095 seq_printf(s, " state: %s\n", state->name);
1096
1097 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001098 struct pinctrl_dev *pctldev = setting->pctldev;
1099
1100 seq_printf(s, " type: %s controller %s ",
1101 map_type(setting->type),
1102 pinctrl_dev_get_name(pctldev));
1103
1104 switch (setting->type) {
1105 case PIN_MAP_TYPE_MUX_GROUP:
1106 pinmux_show_setting(s, setting);
1107 break;
1108 case PIN_MAP_TYPE_CONFIGS_PIN:
1109 case PIN_MAP_TYPE_CONFIGS_GROUP:
1110 pinconf_show_setting(s, setting);
1111 break;
1112 default:
1113 break;
1114 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001115 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001116 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001117 }
1118
Stephen Warren57b676f2012-03-02 13:05:44 -07001119 mutex_unlock(&pinctrl_mutex);
1120
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001121 return 0;
1122}
1123
Linus Walleij2744e8a2011-05-02 20:50:54 +02001124static int pinctrl_pins_open(struct inode *inode, struct file *file)
1125{
1126 return single_open(file, pinctrl_pins_show, inode->i_private);
1127}
1128
1129static int pinctrl_groups_open(struct inode *inode, struct file *file)
1130{
1131 return single_open(file, pinctrl_groups_show, inode->i_private);
1132}
1133
1134static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1135{
1136 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1137}
1138
1139static int pinctrl_devices_open(struct inode *inode, struct file *file)
1140{
1141 return single_open(file, pinctrl_devices_show, NULL);
1142}
1143
Stephen Warren3eedb432012-02-23 17:04:40 -07001144static int pinctrl_maps_open(struct inode *inode, struct file *file)
1145{
1146 return single_open(file, pinctrl_maps_show, NULL);
1147}
1148
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001149static int pinctrl_open(struct inode *inode, struct file *file)
1150{
1151 return single_open(file, pinctrl_show, NULL);
1152}
1153
Linus Walleij2744e8a2011-05-02 20:50:54 +02001154static const struct file_operations pinctrl_pins_ops = {
1155 .open = pinctrl_pins_open,
1156 .read = seq_read,
1157 .llseek = seq_lseek,
1158 .release = single_release,
1159};
1160
1161static const struct file_operations pinctrl_groups_ops = {
1162 .open = pinctrl_groups_open,
1163 .read = seq_read,
1164 .llseek = seq_lseek,
1165 .release = single_release,
1166};
1167
1168static const struct file_operations pinctrl_gpioranges_ops = {
1169 .open = pinctrl_gpioranges_open,
1170 .read = seq_read,
1171 .llseek = seq_lseek,
1172 .release = single_release,
1173};
1174
1175static const struct file_operations pinctrl_devices_ops = {
1176 .open = pinctrl_devices_open,
1177 .read = seq_read,
1178 .llseek = seq_lseek,
1179 .release = single_release,
1180};
1181
Stephen Warren3eedb432012-02-23 17:04:40 -07001182static const struct file_operations pinctrl_maps_ops = {
1183 .open = pinctrl_maps_open,
1184 .read = seq_read,
1185 .llseek = seq_lseek,
1186 .release = single_release,
1187};
1188
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001189static const struct file_operations pinctrl_ops = {
1190 .open = pinctrl_open,
1191 .read = seq_read,
1192 .llseek = seq_lseek,
1193 .release = single_release,
1194};
1195
Linus Walleij2744e8a2011-05-02 20:50:54 +02001196static struct dentry *debugfs_root;
1197
1198static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1199{
Tony Lindgren02157162012-01-20 08:17:22 -08001200 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001201
Stephen Warren51cd24e2011-12-09 16:59:05 -07001202 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001203 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001204 pctldev->device_root = device_root;
1205
Linus Walleij2744e8a2011-05-02 20:50:54 +02001206 if (IS_ERR(device_root) || !device_root) {
1207 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001208 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001209 return;
1210 }
1211 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1212 device_root, pctldev, &pinctrl_pins_ops);
1213 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1214 device_root, pctldev, &pinctrl_groups_ops);
1215 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1216 device_root, pctldev, &pinctrl_gpioranges_ops);
1217 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +02001218 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001219}
1220
Tony Lindgren02157162012-01-20 08:17:22 -08001221static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1222{
1223 debugfs_remove_recursive(pctldev->device_root);
1224}
1225
Linus Walleij2744e8a2011-05-02 20:50:54 +02001226static void pinctrl_init_debugfs(void)
1227{
1228 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1229 if (IS_ERR(debugfs_root) || !debugfs_root) {
1230 pr_warn("failed to create debugfs directory\n");
1231 debugfs_root = NULL;
1232 return;
1233 }
1234
1235 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1236 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001237 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1238 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001239 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1240 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001241}
1242
1243#else /* CONFIG_DEBUG_FS */
1244
1245static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1246{
1247}
1248
1249static void pinctrl_init_debugfs(void)
1250{
1251}
1252
Tony Lindgren02157162012-01-20 08:17:22 -08001253static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1254{
1255}
1256
Linus Walleij2744e8a2011-05-02 20:50:54 +02001257#endif
1258
Stephen Warrend26bc492012-03-16 14:54:25 -06001259static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1260{
1261 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1262
1263 if (!ops ||
1264 !ops->list_groups ||
1265 !ops->get_group_name ||
1266 !ops->get_group_pins)
1267 return -EINVAL;
1268
Stephen Warren57291ce2012-03-23 10:29:46 -06001269 if (ops->dt_node_to_map && !ops->dt_free_map)
1270 return -EINVAL;
1271
Stephen Warrend26bc492012-03-16 14:54:25 -06001272 return 0;
1273}
1274
Linus Walleij2744e8a2011-05-02 20:50:54 +02001275/**
1276 * pinctrl_register() - register a pin controller device
1277 * @pctldesc: descriptor for this pin controller
1278 * @dev: parent device for this pin controller
1279 * @driver_data: private pin controller data for this pin controller
1280 */
1281struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1282 struct device *dev, void *driver_data)
1283{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001284 struct pinctrl_dev *pctldev;
1285 int ret;
1286
1287 if (pctldesc == NULL)
1288 return NULL;
1289 if (pctldesc->name == NULL)
1290 return NULL;
1291
Stephen Warren02f5b982012-02-22 14:26:00 -07001292 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001293 if (pctldev == NULL) {
1294 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001295 return NULL;
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001296 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001297
1298 /* Initialize pin control device struct */
1299 pctldev->owner = pctldesc->owner;
1300 pctldev->desc = pctldesc;
1301 pctldev->driver_data = driver_data;
1302 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001303 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001304 pctldev->dev = dev;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001305
Stephen Warrend26bc492012-03-16 14:54:25 -06001306 /* check core ops for sanity */
1307 ret = pinctrl_check_ops(pctldev);
1308 if (ret) {
1309 pr_err("%s pinctrl ops lacks necessary functions\n",
1310 pctldesc->name);
1311 goto out_err;
1312 }
1313
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001314 /* If we're implementing pinmuxing, check the ops for sanity */
1315 if (pctldesc->pmxops) {
1316 ret = pinmux_check_ops(pctldev);
1317 if (ret) {
1318 pr_err("%s pinmux ops lacks necessary functions\n",
1319 pctldesc->name);
1320 goto out_err;
1321 }
1322 }
1323
1324 /* If we're implementing pinconfig, check the ops for sanity */
1325 if (pctldesc->confops) {
1326 ret = pinconf_check_ops(pctldev);
1327 if (ret) {
1328 pr_err("%s pin config ops lacks necessary functions\n",
1329 pctldesc->name);
1330 goto out_err;
1331 }
1332 }
1333
Linus Walleij2744e8a2011-05-02 20:50:54 +02001334 /* Register all the pins */
1335 pr_debug("try to register %d pins on %s...\n",
1336 pctldesc->npins, pctldesc->name);
1337 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1338 if (ret) {
1339 pr_err("error during pin registration\n");
1340 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1341 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001342 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001343 }
1344
Stephen Warren57b676f2012-03-02 13:05:44 -07001345 mutex_lock(&pinctrl_mutex);
1346
Stephen Warren8b9c1392012-02-19 23:45:42 -07001347 list_add_tail(&pctldev->node, &pinctrldev_list);
Stephen Warren57b676f2012-03-02 13:05:44 -07001348
Stephen Warren6e5e9592012-03-02 13:05:47 -07001349 pctldev->p = pinctrl_get_locked(pctldev->dev);
1350 if (!IS_ERR(pctldev->p)) {
1351 struct pinctrl_state *s =
1352 pinctrl_lookup_state_locked(pctldev->p,
1353 PINCTRL_STATE_DEFAULT);
1354 if (!IS_ERR(s))
1355 pinctrl_select_state_locked(pctldev->p, s);
1356 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001357
1358 mutex_unlock(&pinctrl_mutex);
1359
Stephen Warren2304b472012-02-22 14:26:01 -07001360 pinctrl_init_device_debugfs(pctldev);
1361
Linus Walleij2744e8a2011-05-02 20:50:54 +02001362 return pctldev;
1363
Stephen Warren51cd24e2011-12-09 16:59:05 -07001364out_err:
1365 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001366 return NULL;
1367}
1368EXPORT_SYMBOL_GPL(pinctrl_register);
1369
1370/**
1371 * pinctrl_unregister() - unregister pinmux
1372 * @pctldev: pin controller to unregister
1373 *
1374 * Called by pinmux drivers to unregister a pinmux.
1375 */
1376void pinctrl_unregister(struct pinctrl_dev *pctldev)
1377{
1378 if (pctldev == NULL)
1379 return;
1380
Tony Lindgren02157162012-01-20 08:17:22 -08001381 pinctrl_remove_device_debugfs(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001382
1383 mutex_lock(&pinctrl_mutex);
1384
Stephen Warren6e5e9592012-03-02 13:05:47 -07001385 if (!IS_ERR(pctldev->p))
1386 pinctrl_put_locked(pctldev->p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -07001387
Linus Walleij2744e8a2011-05-02 20:50:54 +02001388 /* TODO: check that no pinmuxes are still active? */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001389 list_del(&pctldev->node);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001390 /* Destroy descriptor tree */
1391 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1392 pctldev->desc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001393 kfree(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001394
1395 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001396}
1397EXPORT_SYMBOL_GPL(pinctrl_unregister);
1398
1399static int __init pinctrl_init(void)
1400{
1401 pr_info("initialized pinctrl subsystem\n");
1402 pinctrl_init_debugfs();
1403 return 0;
1404}
1405
1406/* init early since many drivers really need to initialized pinmux early */
1407core_initcall(pinctrl_init);