blob: 7ff869007ba48b8c43442bd4ad1d9a0273e4865e [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) {
521 dev_err(p->dev, "unknown pinctrl device %s in map entry",
522 map->ctrl_dev_name);
523 kfree(setting);
524 /* Eventually, this should trigger deferred probe */
525 return -ENODEV;
526 }
527
Stephen Warren1e2082b2012-03-02 13:05:48 -0700528 switch (map->type) {
529 case PIN_MAP_TYPE_MUX_GROUP:
530 ret = pinmux_map_to_setting(map, setting);
531 break;
532 case PIN_MAP_TYPE_CONFIGS_PIN:
533 case PIN_MAP_TYPE_CONFIGS_GROUP:
534 ret = pinconf_map_to_setting(map, setting);
535 break;
536 default:
537 ret = -EINVAL;
538 break;
539 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700540 if (ret < 0) {
541 kfree(setting);
542 return ret;
543 }
544
545 list_add_tail(&setting->node, &state->settings);
546
547 return 0;
548}
549
550static struct pinctrl *find_pinctrl(struct device *dev)
551{
552 struct pinctrl *p;
553
Stephen Warren1e2082b2012-03-02 13:05:48 -0700554 list_for_each_entry(p, &pinctrl_list, node)
Stephen Warren6e5e9592012-03-02 13:05:47 -0700555 if (p->dev == dev)
556 return p;
557
558 return NULL;
559}
560
561static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
562
563static struct pinctrl *create_pinctrl(struct device *dev)
564{
565 struct pinctrl *p;
566 const char *devname;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700567 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100568 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700569 struct pinctrl_map const *map;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700570 int ret;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100571
572 /*
573 * create the state cookie holder struct pinctrl for each
574 * mapping, this is what consumers will get when requesting
575 * a pin control handle with pinctrl_get()
576 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700577 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700578 if (p == NULL) {
579 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100580 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700581 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700582 p->dev = dev;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700583 INIT_LIST_HEAD(&p->states);
Stephen Warren57291ce2012-03-23 10:29:46 -0600584 INIT_LIST_HEAD(&p->dt_maps);
585
586 ret = pinctrl_dt_to_map(p);
587 if (ret < 0) {
588 kfree(p);
589 return ERR_PTR(ret);
590 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700591
592 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100593
594 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700595 for_each_maps(maps_node, i, map) {
Stephen Warren1681f5a2012-02-22 14:25:58 -0700596 /* Map must be for this device */
597 if (strcmp(map->dev_name, devname))
598 continue;
599
Stephen Warren6e5e9592012-03-02 13:05:47 -0700600 ret = add_setting(p, map);
601 if (ret < 0) {
602 pinctrl_put_locked(p, false);
603 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100604 }
605 }
606
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100607 /* Add the pinmux to the global list */
Stephen Warren8b9c1392012-02-19 23:45:42 -0700608 list_add_tail(&p->node, &pinctrl_list);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100609
610 return p;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700611}
Stephen Warren7ecdb162012-03-02 13:05:45 -0700612
Stephen Warren6e5e9592012-03-02 13:05:47 -0700613static struct pinctrl *pinctrl_get_locked(struct device *dev)
614{
615 struct pinctrl *p;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700616
Stephen Warren6e5e9592012-03-02 13:05:47 -0700617 if (WARN_ON(!dev))
618 return ERR_PTR(-EINVAL);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700619
Stephen Warren6e5e9592012-03-02 13:05:47 -0700620 p = find_pinctrl(dev);
621 if (p != NULL)
622 return ERR_PTR(-EBUSY);
623
624 p = create_pinctrl(dev);
625 if (IS_ERR(p))
626 return p;
627
628 return p;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100629}
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700630
631/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700632 * pinctrl_get() - retrieves the pinctrl handle for a device
633 * @dev: the device to obtain the handle for
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700634 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700635struct pinctrl *pinctrl_get(struct device *dev)
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700636{
637 struct pinctrl *p;
638
Stephen Warren57b676f2012-03-02 13:05:44 -0700639 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700640 p = pinctrl_get_locked(dev);
Stephen Warren57b676f2012-03-02 13:05:44 -0700641 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700642
643 return p;
644}
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100645EXPORT_SYMBOL_GPL(pinctrl_get);
646
Stephen Warren6e5e9592012-03-02 13:05:47 -0700647static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
Stephen Warren57b676f2012-03-02 13:05:44 -0700648{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700649 struct pinctrl_state *state, *n1;
650 struct pinctrl_setting *setting, *n2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700651
Stephen Warren6e5e9592012-03-02 13:05:47 -0700652 list_for_each_entry_safe(state, n1, &p->states, node) {
653 list_for_each_entry_safe(setting, n2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700654 switch (setting->type) {
655 case PIN_MAP_TYPE_MUX_GROUP:
656 if (state == p->state)
657 pinmux_disable_setting(setting);
658 pinmux_free_setting(setting);
659 break;
660 case PIN_MAP_TYPE_CONFIGS_PIN:
661 case PIN_MAP_TYPE_CONFIGS_GROUP:
662 pinconf_free_setting(setting);
663 break;
664 default:
665 break;
666 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700667 list_del(&setting->node);
668 kfree(setting);
669 }
670 list_del(&state->node);
671 kfree(state);
Stephen Warren7ecdb162012-03-02 13:05:45 -0700672 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700673
Stephen Warren57291ce2012-03-23 10:29:46 -0600674 pinctrl_dt_free_maps(p);
675
Stephen Warren6e5e9592012-03-02 13:05:47 -0700676 if (inlist)
677 list_del(&p->node);
Stephen Warren57b676f2012-03-02 13:05:44 -0700678 kfree(p);
679}
680
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100681/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700682 * pinctrl_put() - release a previously claimed pinctrl handle
683 * @p: the pinctrl handle to release
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100684 */
685void pinctrl_put(struct pinctrl *p)
686{
Stephen Warren57b676f2012-03-02 13:05:44 -0700687 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700688 pinctrl_put_locked(p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -0700689 mutex_unlock(&pinctrl_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100690}
691EXPORT_SYMBOL_GPL(pinctrl_put);
692
Stephen Warren6e5e9592012-03-02 13:05:47 -0700693static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
694 const char *name)
Stephen Warren57b676f2012-03-02 13:05:44 -0700695{
Stephen Warren6e5e9592012-03-02 13:05:47 -0700696 struct pinctrl_state *state;
697
698 state = find_state(p, name);
699 if (!state)
700 return ERR_PTR(-ENODEV);
701
702 return state;
703}
704
705/**
706 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
707 * @p: the pinctrl handle to retrieve the state from
708 * @name: the state name to retrieve
709 */
710struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
711{
712 struct pinctrl_state *s;
713
714 mutex_lock(&pinctrl_mutex);
715 s = pinctrl_lookup_state_locked(p, name);
716 mutex_unlock(&pinctrl_mutex);
717
718 return s;
719}
720EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
721
722static int pinctrl_select_state_locked(struct pinctrl *p,
723 struct pinctrl_state *state)
724{
725 struct pinctrl_setting *setting, *setting2;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700726 int ret;
Stephen Warren57b676f2012-03-02 13:05:44 -0700727
Stephen Warren6e5e9592012-03-02 13:05:47 -0700728 if (p->state == state)
729 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700730
Stephen Warren6e5e9592012-03-02 13:05:47 -0700731 if (p->state) {
732 /*
733 * The set of groups with a mux configuration in the old state
734 * may not be identical to the set of groups with a mux setting
735 * in the new state. While this might be unusual, it's entirely
736 * possible for the "user"-supplied mapping table to be written
737 * that way. For each group that was configured in the old state
738 * but not in the new state, this code puts that group into a
739 * safe/disabled state.
740 */
741 list_for_each_entry(setting, &p->state->settings, node) {
742 bool found = false;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700743 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
744 continue;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700745 list_for_each_entry(setting2, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700746 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
747 continue;
748 if (setting2->data.mux.group ==
749 setting->data.mux.group) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700750 found = true;
751 break;
752 }
Stephen Warren7ecdb162012-03-02 13:05:45 -0700753 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700754 if (!found)
755 pinmux_disable_setting(setting);
756 }
757 }
758
759 p->state = state;
760
761 /* Apply all the settings for the new state */
762 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700763 switch (setting->type) {
764 case PIN_MAP_TYPE_MUX_GROUP:
765 ret = pinmux_enable_setting(setting);
766 break;
767 case PIN_MAP_TYPE_CONFIGS_PIN:
768 case PIN_MAP_TYPE_CONFIGS_GROUP:
769 ret = pinconf_apply_setting(setting);
770 break;
771 default:
772 ret = -EINVAL;
773 break;
774 }
Stephen Warren6e5e9592012-03-02 13:05:47 -0700775 if (ret < 0) {
776 /* FIXME: Difficult to return to prev state */
777 return ret;
Stephen Warren7ecdb162012-03-02 13:05:45 -0700778 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700779 }
780
Stephen Warren7ecdb162012-03-02 13:05:45 -0700781 return 0;
Stephen Warren57b676f2012-03-02 13:05:44 -0700782}
783
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100784/**
Stephen Warren6e5e9592012-03-02 13:05:47 -0700785 * pinctrl_select() - select/activate/program a pinctrl state to HW
786 * @p: the pinctrl handle for the device that requests configuratio
787 * @state: the state handle to select/activate/program
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100788 */
Stephen Warren6e5e9592012-03-02 13:05:47 -0700789int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100790{
Stephen Warren57b676f2012-03-02 13:05:44 -0700791 int ret;
Stephen Warren6e5e9592012-03-02 13:05:47 -0700792
Stephen Warren57b676f2012-03-02 13:05:44 -0700793 mutex_lock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700794 ret = pinctrl_select_state_locked(p, state);
Stephen Warren57b676f2012-03-02 13:05:44 -0700795 mutex_unlock(&pinctrl_mutex);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700796
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100797 return ret;
798}
Stephen Warren6e5e9592012-03-02 13:05:47 -0700799EXPORT_SYMBOL_GPL(pinctrl_select_state);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100800
Stephen Warren57291ce2012-03-23 10:29:46 -0600801int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
802 bool dup, bool locked)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100803{
Stephen Warren1e2082b2012-03-02 13:05:48 -0700804 int i, ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700805 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100806
807 pr_debug("add %d pinmux maps\n", num_maps);
808
809 /* First sanity check the new mapping */
810 for (i = 0; i < num_maps; i++) {
Stephen Warren1e2082b2012-03-02 13:05:48 -0700811 if (!maps[i].dev_name) {
812 pr_err("failed to register map %s (%d): no device given\n",
813 maps[i].name, i);
814 return -EINVAL;
815 }
816
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100817 if (!maps[i].name) {
818 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700819 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100820 return -EINVAL;
821 }
822
Stephen Warren1e2082b2012-03-02 13:05:48 -0700823 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
824 !maps[i].ctrl_dev_name) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100825 pr_err("failed to register map %s (%d): no pin control device given\n",
826 maps[i].name, i);
827 return -EINVAL;
828 }
829
Stephen Warren1e2082b2012-03-02 13:05:48 -0700830 switch (maps[i].type) {
831 case PIN_MAP_TYPE_DUMMY_STATE:
832 break;
833 case PIN_MAP_TYPE_MUX_GROUP:
834 ret = pinmux_validate_map(&maps[i], i);
835 if (ret < 0)
836 return 0;
837 break;
838 case PIN_MAP_TYPE_CONFIGS_PIN:
839 case PIN_MAP_TYPE_CONFIGS_GROUP:
840 ret = pinconf_validate_map(&maps[i], i);
841 if (ret < 0)
842 return 0;
843 break;
844 default:
845 pr_err("failed to register map %s (%d): invalid type given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700846 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -0700847 return -EINVAL;
848 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100849 }
850
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700851 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
852 if (!maps_node) {
853 pr_err("failed to alloc struct pinctrl_maps\n");
854 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100855 }
856
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700857 maps_node->num_maps = num_maps;
Stephen Warren57291ce2012-03-23 10:29:46 -0600858 if (dup) {
859 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
860 GFP_KERNEL);
861 if (!maps_node->maps) {
862 pr_err("failed to duplicate mapping table\n");
863 kfree(maps_node);
864 return -ENOMEM;
865 }
866 } else {
867 maps_node->maps = maps;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700868 }
869
Stephen Warren57291ce2012-03-23 10:29:46 -0600870 if (!locked)
871 mutex_lock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700872 list_add_tail(&maps_node->node, &pinctrl_maps);
Stephen Warren57291ce2012-03-23 10:29:46 -0600873 if (!locked)
874 mutex_unlock(&pinctrl_mutex);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700875
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100876 return 0;
877}
878
Stephen Warren57291ce2012-03-23 10:29:46 -0600879/**
880 * pinctrl_register_mappings() - register a set of pin controller mappings
881 * @maps: the pincontrol mappings table to register. This should probably be
882 * marked with __initdata so it can be discarded after boot. This
883 * function will perform a shallow copy for the mapping entries.
884 * @num_maps: the number of maps in the mapping table
885 */
886int pinctrl_register_mappings(struct pinctrl_map const *maps,
887 unsigned num_maps)
888{
889 return pinctrl_register_map(maps, num_maps, true, false);
890}
891
892void pinctrl_unregister_map(struct pinctrl_map const *map)
893{
894 struct pinctrl_maps *maps_node;
895
896 list_for_each_entry(maps_node, &pinctrl_maps, node) {
897 if (maps_node->maps == map) {
898 list_del(&maps_node->node);
899 return;
900 }
901 }
902}
903
Linus Walleij2744e8a2011-05-02 20:50:54 +0200904#ifdef CONFIG_DEBUG_FS
905
906static int pinctrl_pins_show(struct seq_file *s, void *what)
907{
908 struct pinctrl_dev *pctldev = s->private;
909 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +0900910 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200911
912 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200913
Stephen Warren57b676f2012-03-02 13:05:44 -0700914 mutex_lock(&pinctrl_mutex);
915
Chanho Park706e8522012-01-03 16:47:50 +0900916 /* The pin number can be retrived from the pin controller descriptor */
917 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200918 struct pin_desc *desc;
919
Chanho Park706e8522012-01-03 16:47:50 +0900920 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200921 desc = pin_desc_get(pctldev, pin);
922 /* Pin space may be sparse */
923 if (desc == NULL)
924 continue;
925
926 seq_printf(s, "pin %d (%s) ", pin,
927 desc->name ? desc->name : "unnamed");
928
929 /* Driver-specific info per pin */
930 if (ops->pin_dbg_show)
931 ops->pin_dbg_show(pctldev, s, pin);
932
933 seq_puts(s, "\n");
934 }
935
Stephen Warren57b676f2012-03-02 13:05:44 -0700936 mutex_unlock(&pinctrl_mutex);
937
Linus Walleij2744e8a2011-05-02 20:50:54 +0200938 return 0;
939}
940
941static int pinctrl_groups_show(struct seq_file *s, void *what)
942{
943 struct pinctrl_dev *pctldev = s->private;
944 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530945 unsigned ngroups, selector = 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200946
Viresh Kumard1e90e92012-03-30 11:25:40 +0530947 ngroups = ops->get_groups_count(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -0700948 mutex_lock(&pinctrl_mutex);
949
Linus Walleij2744e8a2011-05-02 20:50:54 +0200950 seq_puts(s, "registered pin groups:\n");
Viresh Kumard1e90e92012-03-30 11:25:40 +0530951 while (selector < ngroups) {
Stephen Warrena5818a82011-10-19 16:19:25 -0600952 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200953 unsigned num_pins;
954 const char *gname = ops->get_group_name(pctldev, selector);
955 int ret;
956 int i;
957
958 ret = ops->get_group_pins(pctldev, selector,
959 &pins, &num_pins);
960 if (ret)
961 seq_printf(s, "%s [ERROR GETTING PINS]\n",
962 gname);
963 else {
964 seq_printf(s, "group: %s, pins = [ ", gname);
965 for (i = 0; i < num_pins; i++)
966 seq_printf(s, "%d ", pins[i]);
967 seq_puts(s, "]\n");
968 }
969 selector++;
970 }
971
Stephen Warren57b676f2012-03-02 13:05:44 -0700972 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200973
974 return 0;
975}
976
977static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
978{
979 struct pinctrl_dev *pctldev = s->private;
980 struct pinctrl_gpio_range *range = NULL;
981
982 seq_puts(s, "GPIO ranges handled:\n");
983
Stephen Warren57b676f2012-03-02 13:05:44 -0700984 mutex_lock(&pinctrl_mutex);
985
Linus Walleij2744e8a2011-05-02 20:50:54 +0200986 /* Loop over the ranges */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200987 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Linus Walleij75d66422011-11-16 09:58:51 +0100988 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
989 range->id, range->name,
990 range->base, (range->base + range->npins - 1),
991 range->pin_base,
992 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +0200993 }
Stephen Warren57b676f2012-03-02 13:05:44 -0700994
995 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200996
997 return 0;
998}
999
1000static int pinctrl_devices_show(struct seq_file *s, void *what)
1001{
1002 struct pinctrl_dev *pctldev;
1003
Linus Walleijae6b4d82011-10-19 18:14:33 +02001004 seq_puts(s, "name [pinmux] [pinconf]\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001005
1006 mutex_lock(&pinctrl_mutex);
1007
Linus Walleij2744e8a2011-05-02 20:50:54 +02001008 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1009 seq_printf(s, "%s ", pctldev->desc->name);
1010 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +02001011 seq_puts(s, "yes ");
1012 else
1013 seq_puts(s, "no ");
1014 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +02001015 seq_puts(s, "yes");
1016 else
1017 seq_puts(s, "no");
1018 seq_puts(s, "\n");
1019 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001020
1021 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001022
1023 return 0;
1024}
1025
Stephen Warren1e2082b2012-03-02 13:05:48 -07001026static inline const char *map_type(enum pinctrl_map_type type)
1027{
1028 static const char * const names[] = {
1029 "INVALID",
1030 "DUMMY_STATE",
1031 "MUX_GROUP",
1032 "CONFIGS_PIN",
1033 "CONFIGS_GROUP",
1034 };
1035
1036 if (type >= ARRAY_SIZE(names))
1037 return "UNKNOWN";
1038
1039 return names[type];
1040}
1041
Stephen Warren3eedb432012-02-23 17:04:40 -07001042static int pinctrl_maps_show(struct seq_file *s, void *what)
1043{
1044 struct pinctrl_maps *maps_node;
1045 int i;
1046 struct pinctrl_map const *map;
1047
1048 seq_puts(s, "Pinctrl maps:\n");
1049
Stephen Warren57b676f2012-03-02 13:05:44 -07001050 mutex_lock(&pinctrl_mutex);
1051
Stephen Warren3eedb432012-02-23 17:04:40 -07001052 for_each_maps(maps_node, i, map) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001053 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1054 map->dev_name, map->name, map_type(map->type),
1055 map->type);
1056
1057 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1058 seq_printf(s, "controlling device %s\n",
1059 map->ctrl_dev_name);
1060
1061 switch (map->type) {
1062 case PIN_MAP_TYPE_MUX_GROUP:
1063 pinmux_show_map(s, map);
1064 break;
1065 case PIN_MAP_TYPE_CONFIGS_PIN:
1066 case PIN_MAP_TYPE_CONFIGS_GROUP:
1067 pinconf_show_map(s, map);
1068 break;
1069 default:
1070 break;
1071 }
1072
1073 seq_printf(s, "\n");
Stephen Warren3eedb432012-02-23 17:04:40 -07001074 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001075
1076 mutex_unlock(&pinctrl_mutex);
Stephen Warren3eedb432012-02-23 17:04:40 -07001077
1078 return 0;
1079}
1080
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001081static int pinctrl_show(struct seq_file *s, void *what)
1082{
1083 struct pinctrl *p;
Stephen Warren6e5e9592012-03-02 13:05:47 -07001084 struct pinctrl_state *state;
Stephen Warren7ecdb162012-03-02 13:05:45 -07001085 struct pinctrl_setting *setting;
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001086
1087 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
Stephen Warren57b676f2012-03-02 13:05:44 -07001088
1089 mutex_lock(&pinctrl_mutex);
1090
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001091 list_for_each_entry(p, &pinctrl_list, node) {
Stephen Warren6e5e9592012-03-02 13:05:47 -07001092 seq_printf(s, "device: %s current state: %s\n",
1093 dev_name(p->dev),
1094 p->state ? p->state->name : "none");
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001095
Stephen Warren6e5e9592012-03-02 13:05:47 -07001096 list_for_each_entry(state, &p->states, node) {
1097 seq_printf(s, " state: %s\n", state->name);
1098
1099 list_for_each_entry(setting, &state->settings, node) {
Stephen Warren1e2082b2012-03-02 13:05:48 -07001100 struct pinctrl_dev *pctldev = setting->pctldev;
1101
1102 seq_printf(s, " type: %s controller %s ",
1103 map_type(setting->type),
1104 pinctrl_dev_get_name(pctldev));
1105
1106 switch (setting->type) {
1107 case PIN_MAP_TYPE_MUX_GROUP:
1108 pinmux_show_setting(s, setting);
1109 break;
1110 case PIN_MAP_TYPE_CONFIGS_PIN:
1111 case PIN_MAP_TYPE_CONFIGS_GROUP:
1112 pinconf_show_setting(s, setting);
1113 break;
1114 default:
1115 break;
1116 }
Stephen Warren6e5e9592012-03-02 13:05:47 -07001117 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001118 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001119 }
1120
Stephen Warren57b676f2012-03-02 13:05:44 -07001121 mutex_unlock(&pinctrl_mutex);
1122
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001123 return 0;
1124}
1125
Linus Walleij2744e8a2011-05-02 20:50:54 +02001126static int pinctrl_pins_open(struct inode *inode, struct file *file)
1127{
1128 return single_open(file, pinctrl_pins_show, inode->i_private);
1129}
1130
1131static int pinctrl_groups_open(struct inode *inode, struct file *file)
1132{
1133 return single_open(file, pinctrl_groups_show, inode->i_private);
1134}
1135
1136static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1137{
1138 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1139}
1140
1141static int pinctrl_devices_open(struct inode *inode, struct file *file)
1142{
1143 return single_open(file, pinctrl_devices_show, NULL);
1144}
1145
Stephen Warren3eedb432012-02-23 17:04:40 -07001146static int pinctrl_maps_open(struct inode *inode, struct file *file)
1147{
1148 return single_open(file, pinctrl_maps_show, NULL);
1149}
1150
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001151static int pinctrl_open(struct inode *inode, struct file *file)
1152{
1153 return single_open(file, pinctrl_show, NULL);
1154}
1155
Linus Walleij2744e8a2011-05-02 20:50:54 +02001156static const struct file_operations pinctrl_pins_ops = {
1157 .open = pinctrl_pins_open,
1158 .read = seq_read,
1159 .llseek = seq_lseek,
1160 .release = single_release,
1161};
1162
1163static const struct file_operations pinctrl_groups_ops = {
1164 .open = pinctrl_groups_open,
1165 .read = seq_read,
1166 .llseek = seq_lseek,
1167 .release = single_release,
1168};
1169
1170static const struct file_operations pinctrl_gpioranges_ops = {
1171 .open = pinctrl_gpioranges_open,
1172 .read = seq_read,
1173 .llseek = seq_lseek,
1174 .release = single_release,
1175};
1176
1177static const struct file_operations pinctrl_devices_ops = {
1178 .open = pinctrl_devices_open,
1179 .read = seq_read,
1180 .llseek = seq_lseek,
1181 .release = single_release,
1182};
1183
Stephen Warren3eedb432012-02-23 17:04:40 -07001184static const struct file_operations pinctrl_maps_ops = {
1185 .open = pinctrl_maps_open,
1186 .read = seq_read,
1187 .llseek = seq_lseek,
1188 .release = single_release,
1189};
1190
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001191static const struct file_operations pinctrl_ops = {
1192 .open = pinctrl_open,
1193 .read = seq_read,
1194 .llseek = seq_lseek,
1195 .release = single_release,
1196};
1197
Linus Walleij2744e8a2011-05-02 20:50:54 +02001198static struct dentry *debugfs_root;
1199
1200static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1201{
Tony Lindgren02157162012-01-20 08:17:22 -08001202 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001203
Stephen Warren51cd24e2011-12-09 16:59:05 -07001204 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001205 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001206 pctldev->device_root = device_root;
1207
Linus Walleij2744e8a2011-05-02 20:50:54 +02001208 if (IS_ERR(device_root) || !device_root) {
1209 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001210 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001211 return;
1212 }
1213 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1214 device_root, pctldev, &pinctrl_pins_ops);
1215 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1216 device_root, pctldev, &pinctrl_groups_ops);
1217 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1218 device_root, pctldev, &pinctrl_gpioranges_ops);
1219 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +02001220 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001221}
1222
Tony Lindgren02157162012-01-20 08:17:22 -08001223static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1224{
1225 debugfs_remove_recursive(pctldev->device_root);
1226}
1227
Linus Walleij2744e8a2011-05-02 20:50:54 +02001228static void pinctrl_init_debugfs(void)
1229{
1230 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1231 if (IS_ERR(debugfs_root) || !debugfs_root) {
1232 pr_warn("failed to create debugfs directory\n");
1233 debugfs_root = NULL;
1234 return;
1235 }
1236
1237 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1238 debugfs_root, NULL, &pinctrl_devices_ops);
Stephen Warren3eedb432012-02-23 17:04:40 -07001239 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1240 debugfs_root, NULL, &pinctrl_maps_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001241 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1242 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001243}
1244
1245#else /* CONFIG_DEBUG_FS */
1246
1247static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1248{
1249}
1250
1251static void pinctrl_init_debugfs(void)
1252{
1253}
1254
Tony Lindgren02157162012-01-20 08:17:22 -08001255static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1256{
1257}
1258
Linus Walleij2744e8a2011-05-02 20:50:54 +02001259#endif
1260
Stephen Warrend26bc492012-03-16 14:54:25 -06001261static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1262{
1263 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1264
1265 if (!ops ||
Viresh Kumard1e90e92012-03-30 11:25:40 +05301266 !ops->get_groups_count ||
Stephen Warrend26bc492012-03-16 14:54:25 -06001267 !ops->get_group_name ||
1268 !ops->get_group_pins)
1269 return -EINVAL;
1270
Stephen Warren57291ce2012-03-23 10:29:46 -06001271 if (ops->dt_node_to_map && !ops->dt_free_map)
1272 return -EINVAL;
1273
Stephen Warrend26bc492012-03-16 14:54:25 -06001274 return 0;
1275}
1276
Linus Walleij2744e8a2011-05-02 20:50:54 +02001277/**
1278 * pinctrl_register() - register a pin controller device
1279 * @pctldesc: descriptor for this pin controller
1280 * @dev: parent device for this pin controller
1281 * @driver_data: private pin controller data for this pin controller
1282 */
1283struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1284 struct device *dev, void *driver_data)
1285{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001286 struct pinctrl_dev *pctldev;
1287 int ret;
1288
1289 if (pctldesc == NULL)
1290 return NULL;
1291 if (pctldesc->name == NULL)
1292 return NULL;
1293
Stephen Warren02f5b982012-02-22 14:26:00 -07001294 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001295 if (pctldev == NULL) {
1296 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001297 return NULL;
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001298 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001299
1300 /* Initialize pin control device struct */
1301 pctldev->owner = pctldesc->owner;
1302 pctldev->desc = pctldesc;
1303 pctldev->driver_data = driver_data;
1304 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001305 INIT_LIST_HEAD(&pctldev->gpio_ranges);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001306 pctldev->dev = dev;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001307
Stephen Warrend26bc492012-03-16 14:54:25 -06001308 /* check core ops for sanity */
1309 ret = pinctrl_check_ops(pctldev);
1310 if (ret) {
1311 pr_err("%s pinctrl ops lacks necessary functions\n",
1312 pctldesc->name);
1313 goto out_err;
1314 }
1315
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001316 /* If we're implementing pinmuxing, check the ops for sanity */
1317 if (pctldesc->pmxops) {
1318 ret = pinmux_check_ops(pctldev);
1319 if (ret) {
1320 pr_err("%s pinmux ops lacks necessary functions\n",
1321 pctldesc->name);
1322 goto out_err;
1323 }
1324 }
1325
1326 /* If we're implementing pinconfig, check the ops for sanity */
1327 if (pctldesc->confops) {
1328 ret = pinconf_check_ops(pctldev);
1329 if (ret) {
1330 pr_err("%s pin config ops lacks necessary functions\n",
1331 pctldesc->name);
1332 goto out_err;
1333 }
1334 }
1335
Linus Walleij2744e8a2011-05-02 20:50:54 +02001336 /* Register all the pins */
1337 pr_debug("try to register %d pins on %s...\n",
1338 pctldesc->npins, pctldesc->name);
1339 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1340 if (ret) {
1341 pr_err("error during pin registration\n");
1342 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1343 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001344 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001345 }
1346
Stephen Warren57b676f2012-03-02 13:05:44 -07001347 mutex_lock(&pinctrl_mutex);
1348
Stephen Warren8b9c1392012-02-19 23:45:42 -07001349 list_add_tail(&pctldev->node, &pinctrldev_list);
Stephen Warren57b676f2012-03-02 13:05:44 -07001350
Stephen Warren6e5e9592012-03-02 13:05:47 -07001351 pctldev->p = pinctrl_get_locked(pctldev->dev);
1352 if (!IS_ERR(pctldev->p)) {
1353 struct pinctrl_state *s =
1354 pinctrl_lookup_state_locked(pctldev->p,
1355 PINCTRL_STATE_DEFAULT);
1356 if (!IS_ERR(s))
1357 pinctrl_select_state_locked(pctldev->p, s);
1358 }
Stephen Warren57b676f2012-03-02 13:05:44 -07001359
1360 mutex_unlock(&pinctrl_mutex);
1361
Stephen Warren2304b472012-02-22 14:26:01 -07001362 pinctrl_init_device_debugfs(pctldev);
1363
Linus Walleij2744e8a2011-05-02 20:50:54 +02001364 return pctldev;
1365
Stephen Warren51cd24e2011-12-09 16:59:05 -07001366out_err:
1367 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001368 return NULL;
1369}
1370EXPORT_SYMBOL_GPL(pinctrl_register);
1371
1372/**
1373 * pinctrl_unregister() - unregister pinmux
1374 * @pctldev: pin controller to unregister
1375 *
1376 * Called by pinmux drivers to unregister a pinmux.
1377 */
1378void pinctrl_unregister(struct pinctrl_dev *pctldev)
1379{
1380 if (pctldev == NULL)
1381 return;
1382
Tony Lindgren02157162012-01-20 08:17:22 -08001383 pinctrl_remove_device_debugfs(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001384
1385 mutex_lock(&pinctrl_mutex);
1386
Stephen Warren6e5e9592012-03-02 13:05:47 -07001387 if (!IS_ERR(pctldev->p))
1388 pinctrl_put_locked(pctldev->p, true);
Stephen Warren57b676f2012-03-02 13:05:44 -07001389
Linus Walleij2744e8a2011-05-02 20:50:54 +02001390 /* TODO: check that no pinmuxes are still active? */
Linus Walleij2744e8a2011-05-02 20:50:54 +02001391 list_del(&pctldev->node);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001392 /* Destroy descriptor tree */
1393 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1394 pctldev->desc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001395 kfree(pctldev);
Stephen Warren57b676f2012-03-02 13:05:44 -07001396
1397 mutex_unlock(&pinctrl_mutex);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001398}
1399EXPORT_SYMBOL_GPL(pinctrl_unregister);
1400
1401static int __init pinctrl_init(void)
1402{
1403 pr_info("initialized pinctrl subsystem\n");
1404 pinctrl_init_debugfs();
1405 return 0;
1406}
1407
1408/* init early since many drivers really need to initialized pinmux early */
1409core_initcall(pinctrl_init);