blob: 59027ab8347a42c4dcc617949c5bf7f07cff1918 [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;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530322 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleij7afde8b2011-10-19 17:07:16 +0200323 unsigned group_selector = 0;
324
Viresh Kumard1e90e92012-03-30 11:25:40 +0530325 while (group_selector < ngroups) {
Linus Walleij7afde8b2011-10-19 17:07:16 +0200326 const char *gname = pctlops->get_group_name(pctldev,
327 group_selector);
328 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700329 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200330 "found group selector %u for %s\n",
331 group_selector,
332 pin_group);
333 return group_selector;
334 }
335
336 group_selector++;
337 }
338
Stephen Warren51cd24e2011-12-09 16:59:05 -0700339 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200340 pin_group);
341
342 return -EINVAL;
343}
344
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100345/**
346 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
347 * @gpio: the GPIO pin number from the GPIO subsystem number space
348 *
349 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
350 * as part of their gpio_request() semantics, platforms and individual drivers
351 * shall *NOT* request GPIO pins to be muxed in.
352 */
353int pinctrl_request_gpio(unsigned gpio)
354{
355 struct pinctrl_dev *pctldev;
356 struct pinctrl_gpio_range *range;
357 int ret;
358 int pin;
359
Stephen Warren57b676f2012-03-02 13:05:44 -0700360 mutex_lock(&pinctrl_mutex);
361
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100362 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700363 if (ret) {
364 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100365 return -EINVAL;
Stephen Warren57b676f2012-03-02 13:05:44 -0700366 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100367
368 /* Convert to the pin controllers number space */
369 pin = gpio - range->base + range->pin_base;
370
Stephen Warren57b676f2012-03-02 13:05:44 -0700371 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
372
373 mutex_unlock(&pinctrl_mutex);
374 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100375}
376EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
377
378/**
379 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
380 * @gpio: the GPIO pin number from the GPIO subsystem number space
381 *
382 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
383 * as part of their gpio_free() semantics, platforms and individual drivers
384 * shall *NOT* request GPIO pins to be muxed out.
385 */
386void pinctrl_free_gpio(unsigned gpio)
387{
388 struct pinctrl_dev *pctldev;
389 struct pinctrl_gpio_range *range;
390 int ret;
391 int pin;
392
Stephen Warren57b676f2012-03-02 13:05:44 -0700393 mutex_lock(&pinctrl_mutex);
394
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100395 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
Stephen Warren57b676f2012-03-02 13:05:44 -0700396 if (ret) {
397 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100398 return;
Stephen Warren57b676f2012-03-02 13:05:44 -0700399 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100400
401 /* Convert to the pin controllers number space */
402 pin = gpio - range->base + range->pin_base;
403
Stephen Warren57b676f2012-03-02 13:05:44 -0700404 pinmux_free_gpio(pctldev, pin, range);
405
406 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100407}
408EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
409
410static int pinctrl_gpio_direction(unsigned gpio, bool input)
411{
412 struct pinctrl_dev *pctldev;
413 struct pinctrl_gpio_range *range;
414 int ret;
415 int pin;
416
417 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
418 if (ret)
419 return ret;
420
421 /* Convert to the pin controllers number space */
422 pin = gpio - range->base + range->pin_base;
423
424 return pinmux_gpio_direction(pctldev, range, pin, input);
425}
426
427/**
428 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
429 * @gpio: the GPIO pin number from the GPIO subsystem number space
430 *
431 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
432 * as part of their gpio_direction_input() semantics, platforms and individual
433 * drivers shall *NOT* touch pin control GPIO calls.
434 */
435int pinctrl_gpio_direction_input(unsigned gpio)
436{
Stephen Warren57b676f2012-03-02 13:05:44 -0700437 int ret;
438 mutex_lock(&pinctrl_mutex);
439 ret = pinctrl_gpio_direction(gpio, true);
440 mutex_unlock(&pinctrl_mutex);
441 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100442}
443EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
444
445/**
446 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
447 * @gpio: the GPIO pin number from the GPIO subsystem number space
448 *
449 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
450 * as part of their gpio_direction_output() semantics, platforms and individual
451 * drivers shall *NOT* touch pin control GPIO calls.
452 */
453int pinctrl_gpio_direction_output(unsigned gpio)
454{
Stephen Warren57b676f2012-03-02 13:05:44 -0700455 int ret;
456 mutex_lock(&pinctrl_mutex);
457 ret = pinctrl_gpio_direction(gpio, false);
458 mutex_unlock(&pinctrl_mutex);
459 return ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100460}
461EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
462
Stephen Warren6e5e9592012-03-02 13:05:47 -0700463static struct pinctrl_state *find_state(struct pinctrl *p,
464 const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100465{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700466 struct pinctrl_state *state;
467
468 list_for_each_entry(state, &p->states, node)
469 if (!strcmp(state->name, name))
470 return state;
471
472 return NULL;
473}
474
475static struct pinctrl_state *create_state(struct pinctrl *p,
476 const char *name)
477{
478 struct pinctrl_state *state;
479
480 state = kzalloc(sizeof(*state), GFP_KERNEL);
481 if (state == NULL) {
482 dev_err(p->dev,
483 "failed to alloc struct pinctrl_state\n");
484 return ERR_PTR(-ENOMEM);
485 }
486
487 state->name = name;
488 INIT_LIST_HEAD(&state->settings);
489
490 list_add_tail(&state->node, &p->states);
491
492 return state;
493}
494
495static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
496{
497 struct pinctrl_state *state;
498 struct pinctrl_setting *setting;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700499 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700500
501 state = find_state(p, map->name);
502 if (!state)
503 state = create_state(p, map->name);
504 if (IS_ERR(state))
505 return PTR_ERR(state);
506
Stephen Warren1e2082b2012-03-02 13:05:48 -0700507 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
508 return 0;
509
Stephen Warren6e5e9592012-03-02 13:05:47 -0700510 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
511 if (setting == NULL) {
512 dev_err(p->dev,
513 "failed to alloc struct pinctrl_setting\n");
514 return -ENOMEM;
515 }
516
Stephen Warren1e2082b2012-03-02 13:05:48 -0700517 setting->type = map->type;
518
Stephen Warren6e5e9592012-03-02 13:05:47 -0700519 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
520 if (setting->pctldev == NULL) {
Linus Walleijc05127c2012-04-10 10:00:38 +0200521 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
Stephen Warren6e5e9592012-03-02 13:05:47 -0700522 map->ctrl_dev_name);
523 kfree(setting);
Linus Walleijc05127c2012-04-10 10:00:38 +0200524 /*
525 * OK let us guess that the driver is not there yet, and
526 * let's defer obtaining this pinctrl handle to later...
527 */
528 return -EPROBE_DEFER;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700529 }
530
Stephen Warren1e2082b2012-03-02 13:05:48 -0700531 switch (map->type) {
532 case PIN_MAP_TYPE_MUX_GROUP:
533 ret = pinmux_map_to_setting(map, setting);
534 break;
535 case PIN_MAP_TYPE_CONFIGS_PIN:
536 case PIN_MAP_TYPE_CONFIGS_GROUP:
537 ret = pinconf_map_to_setting(map, setting);
538 break;
539 default:
540 ret = -EINVAL;
541 break;
542 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700543 if (ret < 0) {
544 kfree(setting);
545 return ret;
546 }
547
548 list_add_tail(&setting->node, &state->settings);
549
550 return 0;
551}
552
553static struct pinctrl *find_pinctrl(struct device *dev)
554{
555 struct pinctrl *p;
556
Stephen Warren1e2082b2012-03-02 13:05:48 -0700557 list_for_each_entry(p, &pinctrl_list, node)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700558 if (p->dev == dev)
559 return p;
560
561 return NULL;
562}
563
564static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
565
566static struct pinctrl *create_pinctrl(struct device *dev)
567{
568 struct pinctrl *p;
569 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700570 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100571 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700572 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700573 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100574
575 /*
576 * create the state cookie holder struct pinctrl for each
577 * mapping, this is what consumers will get when requesting
578 * a pin control handle with pinctrl_get()
579 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700580 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700581 if (p == NULL) {
582 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100583 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700584 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700585 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700586 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -0600587 INIT_LIST_HEAD(&p->dt_maps);
588
589 ret = pinctrl_dt_to_map(p);
590 if (ret < 0) {
591 kfree(p);
592 return ERR_PTR(ret);
593 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700594
595 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100596
597 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700598 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -0700599 /* Map must be for this device */
600 if (strcmp(map->dev_name, devname))
601 continue;
602
Stephen Warren6e5e9592012-03-02 13:05:47 -0700603 ret = add_setting(p, map);
604 if (ret < 0) {
605 pinctrl_put_locked(p, false);
606 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100607 }
608 }
609
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100610 /* Add the pinmux to the global list */
Stephen Warren8b9c1392012-02-19 23:45:42 -0700611 list_add_tail(&p->node, &pinctrl_list);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100612
613 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700614}
Stephen Warren7ecdb162012-03-02 13:05:45 -0700615
Stephen Warren6e5e9592012-03-02 13:05:47 -0700616static struct pinctrl *pinctrl_get_locked(struct device *dev)
617{
618 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700619
Stephen Warren6e5e9592012-03-02 13:05:47 -0700620 if (WARN_ON(!dev))
621 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700622
Stephen Warren6e5e9592012-03-02 13:05:47 -0700623 p = find_pinctrl(dev);
624 if (p != NULL)
625 return ERR_PTR(-EBUSY);
626
627 p = create_pinctrl(dev);
628 if (IS_ERR(p))
629 return p;
630
631 return p;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100632}
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700633
634/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700635 * pinctrl_get() - retrieves the pinctrl handle for a device
636 * @dev: the device to obtain the handle for
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700637 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700638struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700639{
640 struct pinctrl *p;
641
Stephen Warren57b676f2012-03-02 13:05:44 -0700642 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700643 p = pinctrl_get_locked(dev);
Stephen Warren57b676f2012-03-02 13:05:44 -0700644 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700645
646 return p;
647}
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100648EXPORT_SYMBOL_GPL(pinctrl_get);
649
Stephen Warren6e5e9592012-03-02 13:05:47 -0700650static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -0700651{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700652 struct pinctrl_state *state, *n1;
653 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700654
Stephen Warren6e5e9592012-03-02 13:05:47 -0700655 list_for_each_entry_safe(state, n1, &p->states, node) {
656 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700657 switch (setting->type) {
658 case PIN_MAP_TYPE_MUX_GROUP:
659 if (state == p->state)
660 pinmux_disable_setting(setting);
661 pinmux_free_setting(setting);
662 break;
663 case PIN_MAP_TYPE_CONFIGS_PIN:
664 case PIN_MAP_TYPE_CONFIGS_GROUP:
665 pinconf_free_setting(setting);
666 break;
667 default:
668 break;
669 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700670 list_del(&setting->node);
671 kfree(setting);
672 }
673 list_del(&state->node);
674 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700675 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700676
Stephen Warren57291ce2012-03-23 10:29:46 -0600677 pinctrl_dt_free_maps(p);
678
Stephen Warren6e5e9592012-03-02 13:05:47 -0700679 if (inlist)
680 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700681 kfree(p);
682}
683
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100684/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700685 * pinctrl_put() - release a previously claimed pinctrl handle
686 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100687 */
688void pinctrl_put(struct pinctrl *p)
689{
Stephen Warren57b676f2012-03-02 13:05:44 -0700690 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700691 pinctrl_put_locked(p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -0700692 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100693}
694EXPORT_SYMBOL_GPL(pinctrl_put);
695
Stephen Warren6e5e9592012-03-02 13:05:47 -0700696static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
697 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -0700698{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700699 struct pinctrl_state *state;
700
701 state = find_state(p, name);
702 if (!state)
703 return ERR_PTR(-ENODEV);
704
705 return state;
706}
707
708/**
709 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
710 * @p: the pinctrl handle to retrieve the state from
711 * @name: the state name to retrieve
712 */
713struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
714{
715 struct pinctrl_state *s;
716
717 mutex_lock(&pinctrl_mutex);
718 s = pinctrl_lookup_state_locked(p, name);
719 mutex_unlock(&pinctrl_mutex);
720
721 return s;
722}
723EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
724
725static int pinctrl_select_state_locked(struct pinctrl *p,
726 struct pinctrl_state *state)
727{
728 struct pinctrl_setting *setting, *setting2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700729 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700730
Stephen Warren6e5e9592012-03-02 13:05:47 -0700731 if (p->state == state)
732 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700733
Stephen Warren6e5e9592012-03-02 13:05:47 -0700734 if (p->state) {
735 /*
736 * The set of groups with a mux configuration in the old state
737 * may not be identical to the set of groups with a mux setting
738 * in the new state. While this might be unusual, it's entirely
739 * possible for the "user"-supplied mapping table to be written
740 * that way. For each group that was configured in the old state
741 * but not in the new state, this code puts that group into a
742 * safe/disabled state.
743 */
744 list_for_each_entry(setting, &p->state->settings, node) {
745 bool found = false;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700746 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
747 continue;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700748 list_for_each_entry(setting2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700749 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
750 continue;
751 if (setting2->data.mux.group ==
752 setting->data.mux.group) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700753 found = true;
754 break;
755 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700756 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700757 if (!found)
758 pinmux_disable_setting(setting);
759 }
760 }
761
762 p->state = state;
763
764 /* Apply all the settings for the new state */
765 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700766 switch (setting->type) {
767 case PIN_MAP_TYPE_MUX_GROUP:
768 ret = pinmux_enable_setting(setting);
769 break;
770 case PIN_MAP_TYPE_CONFIGS_PIN:
771 case PIN_MAP_TYPE_CONFIGS_GROUP:
772 ret = pinconf_apply_setting(setting);
773 break;
774 default:
775 ret = -EINVAL;
776 break;
777 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700778 if (ret < 0) {
779 /* FIXME: Difficult to return to prev state */
780 return ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700781 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700782 }
783
Stephen Warren7ecdb162012-03-02 13:05:45 -0700784 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700785}
786
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100787/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700788 * pinctrl_select() - select/activate/program a pinctrl state to HW
789 * @p: the pinctrl handle for the device that requests configuratio
790 * @state: the state handle to select/activate/program
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100791 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700792int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100793{
Stephen Warren57b676f2012-03-02 13:05:44 -0700794 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700795
Stephen Warren57b676f2012-03-02 13:05:44 -0700796 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700797 ret = pinctrl_select_state_locked(p, state);
Stephen Warren57b676f2012-03-02 13:05:44 -0700798 mutex_unlock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700799
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100800 return ret;
801}
Stephen Warren6e5e9592012-03-02 13:05:47 -0700802EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100803
Stephen Warren57291ce2012-03-23 10:29:46 -0600804int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
805 bool dup, bool locked)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100806{
Stephen Warren1e2082b2012-03-02 13:05:48 -0700807 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700808 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100809
810 pr_debug("add %d pinmux maps\n", num_maps);
811
812 /* First sanity check the new mapping */
813 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700814 if (!maps[i].dev_name) {
815 pr_err("failed to register map %s (%d): no device given\n",
816 maps[i].name, i);
817 return -EINVAL;
818 }
819
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100820 if (!maps[i].name) {
821 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700822 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100823 return -EINVAL;
824 }
825
Stephen Warren1e2082b2012-03-02 13:05:48 -0700826 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
827 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100828 pr_err("failed to register map %s (%d): no pin control device given\n",
829 maps[i].name, i);
830 return -EINVAL;
831 }
832
Stephen Warren1e2082b2012-03-02 13:05:48 -0700833 switch (maps[i].type) {
834 case PIN_MAP_TYPE_DUMMY_STATE:
835 break;
836 case PIN_MAP_TYPE_MUX_GROUP:
837 ret = pinmux_validate_map(&maps[i], i);
838 if (ret < 0)
839 return 0;
840 break;
841 case PIN_MAP_TYPE_CONFIGS_PIN:
842 case PIN_MAP_TYPE_CONFIGS_GROUP:
843 ret = pinconf_validate_map(&maps[i], i);
844 if (ret < 0)
845 return 0;
846 break;
847 default:
848 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700849 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -0700850 return -EINVAL;
851 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100852 }
853
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700854 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
855 if (!maps_node) {
856 pr_err("failed to alloc struct pinctrl_maps\n");
857 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100858 }
859
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700860 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -0600861 if (dup) {
862 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
863 GFP_KERNEL);
864 if (!maps_node->maps) {
865 pr_err("failed to duplicate mapping table\n");
866 kfree(maps_node);
867 return -ENOMEM;
868 }
869 } else {
870 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700871 }
872
Stephen Warren57291ce2012-03-23 10:29:46 -0600873 if (!locked)
874 mutex_lock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700875 list_add_tail(&maps_node->node, &pinctrl_maps);
Stephen Warren57291ce2012-03-23 10:29:46 -0600876 if (!locked)
877 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700878
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100879 return 0;
880}
881
Stephen Warren57291ce2012-03-23 10:29:46 -0600882/**
883 * pinctrl_register_mappings() - register a set of pin controller mappings
884 * @maps: the pincontrol mappings table to register. This should probably be
885 * marked with __initdata so it can be discarded after boot. This
886 * function will perform a shallow copy for the mapping entries.
887 * @num_maps: the number of maps in the mapping table
888 */
889int pinctrl_register_mappings(struct pinctrl_map const *maps,
890 unsigned num_maps)
891{
892 return pinctrl_register_map(maps, num_maps, true, false);
893}
894
895void pinctrl_unregister_map(struct pinctrl_map const *map)
896{
897 struct pinctrl_maps *maps_node;
898
899 list_for_each_entry(maps_node, &pinctrl_maps, node) {
900 if (maps_node->maps == map) {
901 list_del(&maps_node->node);
902 return;
903 }
904 }
905}
906
Linus Walleij2744e8a2011-05-02 20:50:54 +0200907#ifdef CONFIG_DEBUG_FS
908
909static int pinctrl_pins_show(struct seq_file *s, void *what)
910{
911 struct pinctrl_dev *pctldev = s->private;
912 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +0900913 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200914
915 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200916
Stephen Warren57b676f2012-03-02 13:05:44 -0700917 mutex_lock(&pinctrl_mutex);
918
Chanho Park706e8522012-01-03 16:47:50 +0900919 /* The pin number can be retrived from the pin controller descriptor */
920 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200921 struct pin_desc *desc;
922
Chanho Park706e8522012-01-03 16:47:50 +0900923 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200924 desc = pin_desc_get(pctldev, pin);
925 /* Pin space may be sparse */
926 if (desc == NULL)
927 continue;
928
929 seq_printf(s, "pin %d (%s) ", pin,
930 desc->name ? desc->name : "unnamed");
931
932 /* Driver-specific info per pin */
933 if (ops->pin_dbg_show)
934 ops->pin_dbg_show(pctldev, s, pin);
935
936 seq_puts(s, "\n");
937 }
938
Stephen Warren57b676f2012-03-02 13:05:44 -0700939 mutex_unlock(&pinctrl_mutex);
940
Linus Walleij2744e8a2011-05-02 20:50:54 +0200941 return 0;
942}
943
944static int pinctrl_groups_show(struct seq_file *s, void *what)
945{
946 struct pinctrl_dev *pctldev = s->private;
947 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530948 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200949
Viresh Kumard1e90e92012-03-30 11:25:40 +0530950 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -0700951 mutex_lock(&pinctrl_mutex);
952
Linus Walleij2744e8a2011-05-02 20:50:54 +0200953 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +0530954 while (selector < ngroups) {
Stephen Warrena5818a82011-10-19 16:19:25 -0600955 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200956 unsigned num_pins;
957 const char *gname = ops->get_group_name(pctldev, selector);
958 int ret;
959 int i;
960
961 ret = ops->get_group_pins(pctldev, selector,
962 &pins, &num_pins);
963 if (ret)
964 seq_printf(s, "%s [ERROR GETTING PINS]\n",
965 gname);
966 else {
967 seq_printf(s, "group: %s, pins = [ ", gname);
968 for (i = 0; i < num_pins; i++)
969 seq_printf(s, "%d ", pins[i]);
970 seq_puts(s, "]\n");
971 }
972 selector++;
973 }
974
Stephen Warren57b676f2012-03-02 13:05:44 -0700975 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200976
977 return 0;
978}
979
980static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
981{
982 struct pinctrl_dev *pctldev = s->private;
983 struct pinctrl_gpio_range *range = NULL;
984
985 seq_puts(s, "GPIO ranges handled:\n");
986
Stephen Warren57b676f2012-03-02 13:05:44 -0700987 mutex_lock(&pinctrl_mutex);
988
Linus Walleij2744e8a2011-05-02 20:50:54 +0200989 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200990 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Linus Walleij75d66422011-11-16 09:58:51 +0100991 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
992 range->id, range->name,
993 range->base, (range->base + range->npins - 1),
994 range->pin_base,
995 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +0200996 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700997
998 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200999
1000 return 0;
1001}
1002
1003static int pinctrl_devices_show(struct seq_file *s, void *what)
1004{
1005 struct pinctrl_dev *pctldev;
1006
Linus Walleijae6b4d82011-10-19 18:14:33 +02001007 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001008
1009 mutex_lock(&pinctrl_mutex);
1010
Linus Walleij2744e8a2011-05-02 20:50:54 +02001011 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1012 seq_printf(s, "%s ", pctldev->desc->name);
1013 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001014 seq_puts(s, "yes ");
1015 else
1016 seq_puts(s, "no ");
1017 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001018 seq_puts(s, "yes");
1019 else
1020 seq_puts(s, "no");
1021 seq_puts(s, "\n");
1022 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001023
1024 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001025
1026 return 0;
1027}
1028
Stephen Warren1e2082b2012-03-02 13:05:48 -07001029static inline const char *map_type(enum pinctrl_map_type type)
1030{
1031 static const char * const names[] = {
1032 "INVALID",
1033 "DUMMY_STATE",
1034 "MUX_GROUP",
1035 "CONFIGS_PIN",
1036 "CONFIGS_GROUP",
1037 };
1038
1039 if (type >= ARRAY_SIZE(names))
1040 return "UNKNOWN";
1041
1042 return names[type];
1043}
1044
Stephen Warren3eedb432012-02-23 17:04:40 -07001045static int pinctrl_maps_show(struct seq_file *s, void *what)
1046{
1047 struct pinctrl_maps *maps_node;
1048 int i;
1049 struct pinctrl_map const *map;
1050
1051 seq_puts(s, "Pinctrl maps:\n");
1052
Stephen Warren57b676f2012-03-02 13:05:44 -07001053 mutex_lock(&pinctrl_mutex);
1054
Stephen Warren3eedb432012-02-23 17:04:40 -07001055 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001056 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1057 map->dev_name, map->name, map_type(map->type),
1058 map->type);
1059
1060 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1061 seq_printf(s, "controlling device %s\n",
1062 map->ctrl_dev_name);
1063
1064 switch (map->type) {
1065 case PIN_MAP_TYPE_MUX_GROUP:
1066 pinmux_show_map(s, map);
1067 break;
1068 case PIN_MAP_TYPE_CONFIGS_PIN:
1069 case PIN_MAP_TYPE_CONFIGS_GROUP:
1070 pinconf_show_map(s, map);
1071 break;
1072 default:
1073 break;
1074 }
1075
1076 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001077 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001078
1079 mutex_unlock(&pinctrl_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001080
1081 return 0;
1082}
1083
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001084static int pinctrl_show(struct seq_file *s, void *what)
1085{
1086 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001087 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001088 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001089
1090 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001091
1092 mutex_lock(&pinctrl_mutex);
1093
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001094 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001095 seq_printf(s, "device: %s current state: %s\n",
1096 dev_name(p->dev),
1097 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001098
Stephen Warren6e5e9592012-03-02 13:05:47 -07001099 list_for_each_entry(state, &p->states, node) {
1100 seq_printf(s, " state: %s\n", state->name);
1101
1102 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001103 struct pinctrl_dev *pctldev = setting->pctldev;
1104
1105 seq_printf(s, " type: %s controller %s ",
1106 map_type(setting->type),
1107 pinctrl_dev_get_name(pctldev));
1108
1109 switch (setting->type) {
1110 case PIN_MAP_TYPE_MUX_GROUP:
1111 pinmux_show_setting(s, setting);
1112 break;
1113 case PIN_MAP_TYPE_CONFIGS_PIN:
1114 case PIN_MAP_TYPE_CONFIGS_GROUP:
1115 pinconf_show_setting(s, setting);
1116 break;
1117 default:
1118 break;
1119 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001120 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001121 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001122 }
1123
Stephen Warren57b676f2012-03-02 13:05:44 -07001124 mutex_unlock(&pinctrl_mutex);
1125
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001126 return 0;
1127}
1128
Linus Walleij2744e8a2011-05-02 20:50:54 +02001129static int pinctrl_pins_open(struct inode *inode, struct file *file)
1130{
1131 return single_open(file, pinctrl_pins_show, inode->i_private);
1132}
1133
1134static int pinctrl_groups_open(struct inode *inode, struct file *file)
1135{
1136 return single_open(file, pinctrl_groups_show, inode->i_private);
1137}
1138
1139static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1140{
1141 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1142}
1143
1144static int pinctrl_devices_open(struct inode *inode, struct file *file)
1145{
1146 return single_open(file, pinctrl_devices_show, NULL);
1147}
1148
Stephen Warren3eedb432012-02-23 17:04:40 -07001149static int pinctrl_maps_open(struct inode *inode, struct file *file)
1150{
1151 return single_open(file, pinctrl_maps_show, NULL);
1152}
1153
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001154static int pinctrl_open(struct inode *inode, struct file *file)
1155{
1156 return single_open(file, pinctrl_show, NULL);
1157}
1158
Linus Walleij2744e8a2011-05-02 20:50:54 +02001159static const struct file_operations pinctrl_pins_ops = {
1160 .open = pinctrl_pins_open,
1161 .read = seq_read,
1162 .llseek = seq_lseek,
1163 .release = single_release,
1164};
1165
1166static const struct file_operations pinctrl_groups_ops = {
1167 .open = pinctrl_groups_open,
1168 .read = seq_read,
1169 .llseek = seq_lseek,
1170 .release = single_release,
1171};
1172
1173static const struct file_operations pinctrl_gpioranges_ops = {
1174 .open = pinctrl_gpioranges_open,
1175 .read = seq_read,
1176 .llseek = seq_lseek,
1177 .release = single_release,
1178};
1179
1180static const struct file_operations pinctrl_devices_ops = {
1181 .open = pinctrl_devices_open,
1182 .read = seq_read,
1183 .llseek = seq_lseek,
1184 .release = single_release,
1185};
1186
Stephen Warren3eedb432012-02-23 17:04:40 -07001187static const struct file_operations pinctrl_maps_ops = {
1188 .open = pinctrl_maps_open,
1189 .read = seq_read,
1190 .llseek = seq_lseek,
1191 .release = single_release,
1192};
1193
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001194static const struct file_operations pinctrl_ops = {
1195 .open = pinctrl_open,
1196 .read = seq_read,
1197 .llseek = seq_lseek,
1198 .release = single_release,
1199};
1200
Linus Walleij2744e8a2011-05-02 20:50:54 +02001201static struct dentry *debugfs_root;
1202
1203static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1204{
Tony Lindgren02157162012-01-20 08:17:22 -08001205 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001206
Stephen Warren51cd24e2011-12-09 16:59:05 -07001207 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001208 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001209 pctldev->device_root = device_root;
1210
Linus Walleij2744e8a2011-05-02 20:50:54 +02001211 if (IS_ERR(device_root) || !device_root) {
1212 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001213 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001214 return;
1215 }
1216 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1217 device_root, pctldev, &pinctrl_pins_ops);
1218 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1219 device_root, pctldev, &pinctrl_groups_ops);
1220 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1221 device_root, pctldev, &pinctrl_gpioranges_ops);
1222 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +02001223 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001224}
1225
Tony Lindgren02157162012-01-20 08:17:22 -08001226static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1227{
1228 debugfs_remove_recursive(pctldev->device_root);
1229}
1230
Linus Walleij2744e8a2011-05-02 20:50:54 +02001231static void pinctrl_init_debugfs(void)
1232{
1233 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1234 if (IS_ERR(debugfs_root) || !debugfs_root) {
1235 pr_warn("failed to create debugfs directory\n");
1236 debugfs_root = NULL;
1237 return;
1238 }
1239
1240 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1241 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001242 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1243 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001244 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1245 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001246}
1247
1248#else /* CONFIG_DEBUG_FS */
1249
1250static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1251{
1252}
1253
1254static void pinctrl_init_debugfs(void)
1255{
1256}
1257
Tony Lindgren02157162012-01-20 08:17:22 -08001258static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1259{
1260}
1261
Linus Walleij2744e8a2011-05-02 20:50:54 +02001262#endif
1263
Stephen Warrend26bc492012-03-16 14:54:25 -06001264static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1265{
1266 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1267
1268 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301269 !ops->get_groups_count ||
Stephen Warrend26bc492012-03-16 14:54:25 -06001270 !ops->get_group_name ||
1271 !ops->get_group_pins)
1272 return -EINVAL;
1273
Stephen Warren57291ce2012-03-23 10:29:46 -06001274 if (ops->dt_node_to_map && !ops->dt_free_map)
1275 return -EINVAL;
1276
Stephen Warrend26bc492012-03-16 14:54:25 -06001277 return 0;
1278}
1279
Linus Walleij2744e8a2011-05-02 20:50:54 +02001280/**
1281 * pinctrl_register() - register a pin controller device
1282 * @pctldesc: descriptor for this pin controller
1283 * @dev: parent device for this pin controller
1284 * @driver_data: private pin controller data for this pin controller
1285 */
1286struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1287 struct device *dev, void *driver_data)
1288{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001289 struct pinctrl_dev *pctldev;
1290 int ret;
1291
1292 if (pctldesc == NULL)
1293 return NULL;
1294 if (pctldesc->name == NULL)
1295 return NULL;
1296
Stephen Warren02f5b982012-02-22 14:26:00 -07001297 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001298 if (pctldev == NULL) {
1299 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001300 return NULL;
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001301 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001302
1303 /* Initialize pin control device struct */
1304 pctldev->owner = pctldesc->owner;
1305 pctldev->desc = pctldesc;
1306 pctldev->driver_data = driver_data;
1307 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001308 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001309 pctldev->dev = dev;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001310
Stephen Warrend26bc492012-03-16 14:54:25 -06001311 /* check core ops for sanity */
1312 ret = pinctrl_check_ops(pctldev);
1313 if (ret) {
1314 pr_err("%s pinctrl ops lacks necessary functions\n",
1315 pctldesc->name);
1316 goto out_err;
1317 }
1318
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001319 /* If we're implementing pinmuxing, check the ops for sanity */
1320 if (pctldesc->pmxops) {
1321 ret = pinmux_check_ops(pctldev);
1322 if (ret) {
1323 pr_err("%s pinmux ops lacks necessary functions\n",
1324 pctldesc->name);
1325 goto out_err;
1326 }
1327 }
1328
1329 /* If we're implementing pinconfig, check the ops for sanity */
1330 if (pctldesc->confops) {
1331 ret = pinconf_check_ops(pctldev);
1332 if (ret) {
1333 pr_err("%s pin config ops lacks necessary functions\n",
1334 pctldesc->name);
1335 goto out_err;
1336 }
1337 }
1338
Linus Walleij2744e8a2011-05-02 20:50:54 +02001339 /* Register all the pins */
1340 pr_debug("try to register %d pins on %s...\n",
1341 pctldesc->npins, pctldesc->name);
1342 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1343 if (ret) {
1344 pr_err("error during pin registration\n");
1345 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1346 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001347 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001348 }
1349
Stephen Warren57b676f2012-03-02 13:05:44 -07001350 mutex_lock(&pinctrl_mutex);
1351
Stephen Warren8b9c1392012-02-19 23:45:42 -07001352 list_add_tail(&pctldev->node, &pinctrldev_list);
Stephen Warren57b676f2012-03-02 13:05:44 -07001353
Stephen Warren6e5e9592012-03-02 13:05:47 -07001354 pctldev->p = pinctrl_get_locked(pctldev->dev);
1355 if (!IS_ERR(pctldev->p)) {
1356 struct pinctrl_state *s =
1357 pinctrl_lookup_state_locked(pctldev->p,
1358 PINCTRL_STATE_DEFAULT);
1359 if (!IS_ERR(s))
1360 pinctrl_select_state_locked(pctldev->p, s);
1361 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001362
1363 mutex_unlock(&pinctrl_mutex);
1364
Stephen Warren2304b472012-02-22 14:26:01 -07001365 pinctrl_init_device_debugfs(pctldev);
1366
Linus Walleij2744e8a2011-05-02 20:50:54 +02001367 return pctldev;
1368
Stephen Warren51cd24e2011-12-09 16:59:05 -07001369out_err:
1370 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001371 return NULL;
1372}
1373EXPORT_SYMBOL_GPL(pinctrl_register);
1374
1375/**
1376 * pinctrl_unregister() - unregister pinmux
1377 * @pctldev: pin controller to unregister
1378 *
1379 * Called by pinmux drivers to unregister a pinmux.
1380 */
1381void pinctrl_unregister(struct pinctrl_dev *pctldev)
1382{
1383 if (pctldev == NULL)
1384 return;
1385
Tony Lindgren02157162012-01-20 08:17:22 -08001386 pinctrl_remove_device_debugfs(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001387
1388 mutex_lock(&pinctrl_mutex);
1389
Stephen Warren6e5e9592012-03-02 13:05:47 -07001390 if (!IS_ERR(pctldev->p))
1391 pinctrl_put_locked(pctldev->p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -07001392
Linus Walleij2744e8a2011-05-02 20:50:54 +02001393 /* TODO: check that no pinmuxes are still active? */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001394 list_del(&pctldev->node);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001395 /* Destroy descriptor tree */
1396 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1397 pctldev->desc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001398 kfree(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001399
1400 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001401}
1402EXPORT_SYMBOL_GPL(pinctrl_unregister);
1403
1404static int __init pinctrl_init(void)
1405{
1406 pr_info("initialized pinctrl subsystem\n");
1407 pinctrl_init_debugfs();
1408 return 0;
1409}
1410
1411/* init early since many drivers really need to initialized pinmux early */
1412core_initcall(pinctrl_init);