blob: 633b97e5ff6ef573360d4624411f159e66671ec2 [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>
21#include <linux/radix-tree.h>
22#include <linux/err.h>
23#include <linux/list.h>
24#include <linux/mutex.h>
25#include <linux/spinlock.h>
26#include <linux/sysfs.h>
27#include <linux/debugfs.h>
28#include <linux/seq_file.h>
29#include <linux/pinctrl/pinctrl.h>
30#include <linux/pinctrl/machine.h>
31#include "core.h"
32#include "pinmux.h"
Linus Walleijae6b4d82011-10-19 18:14:33 +020033#include "pinconf.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020034
Linus Walleijbefe5bd2012-02-09 19:47:48 +010035/**
Stephen Warrenb2b3e662012-02-19 23:45:43 -070036 * struct pinctrl_maps - a list item containing part of the mapping table
37 * @node: mapping table list node
38 * @maps: array of mapping table entries
39 * @num_maps: the number of entries in @maps
40 */
41struct pinctrl_maps {
42 struct list_head node;
43 struct pinctrl_map const *maps;
44 unsigned num_maps;
45};
46
47/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +010048 * struct pinctrl_hog - a list item to stash control hogs
49 * @node: pin control hog list node
50 * @map: map entry responsible for this hogging
51 * @pmx: the pin control hogged by this item
52 */
53struct pinctrl_hog {
54 struct list_head node;
55 struct pinctrl_map const *map;
56 struct pinctrl *p;
57};
58
Linus Walleij2744e8a2011-05-02 20:50:54 +020059/* Global list of pin control devices */
60static DEFINE_MUTEX(pinctrldev_list_mutex);
61static LIST_HEAD(pinctrldev_list);
62
Linus Walleijbefe5bd2012-02-09 19:47:48 +010063/* List of pin controller handles */
64static DEFINE_MUTEX(pinctrl_list_mutex);
65static LIST_HEAD(pinctrl_list);
66
67/* Global pinctrl maps */
Stephen Warrenb2b3e662012-02-19 23:45:43 -070068static DEFINE_MUTEX(pinctrl_maps_mutex);
69static LIST_HEAD(pinctrl_maps);
70
71#define for_each_maps(_maps_node_, _i_, _map_) \
72 list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
73 for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
74 _i_ < _maps_node_->num_maps; \
75 i++, _map_ = &_maps_node_->maps[_i_])
Linus Walleijbefe5bd2012-02-09 19:47:48 +010076
Linus Walleij2744e8a2011-05-02 20:50:54 +020077const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
78{
79 /* We're not allowed to register devices without name */
80 return pctldev->desc->name;
81}
82EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
83
84void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
85{
86 return pctldev->driver_data;
87}
88EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
89
90/**
Linus Walleij9dfac4f2012-02-01 18:02:47 +010091 * get_pinctrl_dev_from_devname() - look up pin controller device
92 * @devname: the name of a device instance, as returned by dev_name()
Linus Walleij2744e8a2011-05-02 20:50:54 +020093 *
94 * Looks up a pin control device matching a certain device name or pure device
95 * pointer, the pure device pointer will take precedence.
96 */
Linus Walleij9dfac4f2012-02-01 18:02:47 +010097struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
Linus Walleij2744e8a2011-05-02 20:50:54 +020098{
99 struct pinctrl_dev *pctldev = NULL;
100 bool found = false;
101
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100102 if (!devname)
103 return NULL;
104
Linus Walleij2744e8a2011-05-02 20:50:54 +0200105 mutex_lock(&pinctrldev_list_mutex);
106 list_for_each_entry(pctldev, &pinctrldev_list, node) {
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100107 if (!strcmp(dev_name(pctldev->dev), devname)) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200108 /* Matched on device name */
109 found = true;
110 break;
111 }
112 }
113 mutex_unlock(&pinctrldev_list_mutex);
114
115 return found ? pctldev : NULL;
116}
117
Linus Walleij2744e8a2011-05-02 20:50:54 +0200118/**
Linus Walleijae6b4d82011-10-19 18:14:33 +0200119 * pin_get_from_name() - look up a pin number from a name
120 * @pctldev: the pin control device to lookup the pin on
121 * @name: the name of the pin to look up
122 */
123int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
124{
Chanho Park706e8522012-01-03 16:47:50 +0900125 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200126
Chanho Park706e8522012-01-03 16:47:50 +0900127 /* The pin number can be retrived from the pin controller descriptor */
128 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200129 struct pin_desc *desc;
130
Chanho Park706e8522012-01-03 16:47:50 +0900131 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200132 desc = pin_desc_get(pctldev, pin);
133 /* Pin space may be sparse */
134 if (desc == NULL)
135 continue;
136 if (desc->name && !strcmp(name, desc->name))
137 return pin;
138 }
139
140 return -EINVAL;
141}
142
143/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200144 * pin_is_valid() - check if pin exists on controller
145 * @pctldev: the pin control device to check the pin on
146 * @pin: pin to check, use the local pin controller index number
147 *
148 * This tells us whether a certain pin exist on a certain pin controller or
149 * not. Pin lists may be sparse, so some pins may not exist.
150 */
151bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
152{
153 struct pin_desc *pindesc;
154
155 if (pin < 0)
156 return false;
157
158 pindesc = pin_desc_get(pctldev, pin);
159 if (pindesc == NULL)
160 return false;
161
162 return true;
163}
164EXPORT_SYMBOL_GPL(pin_is_valid);
165
166/* Deletes a range of pin descriptors */
167static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
168 const struct pinctrl_pin_desc *pins,
169 unsigned num_pins)
170{
171 int i;
172
Linus Walleij2744e8a2011-05-02 20:50:54 +0200173 for (i = 0; i < num_pins; i++) {
174 struct pin_desc *pindesc;
175
176 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
177 pins[i].number);
178 if (pindesc != NULL) {
179 radix_tree_delete(&pctldev->pin_desc_tree,
180 pins[i].number);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100181 if (pindesc->dynamic_name)
182 kfree(pindesc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200183 }
184 kfree(pindesc);
185 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200186}
187
188static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
189 unsigned number, const char *name)
190{
191 struct pin_desc *pindesc;
192
193 pindesc = pin_desc_get(pctldev, number);
194 if (pindesc != NULL) {
195 pr_err("pin %d already registered on %s\n", number,
196 pctldev->desc->name);
197 return -EINVAL;
198 }
199
200 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700201 if (pindesc == NULL) {
202 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200203 return -ENOMEM;
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700204 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200205
Linus Walleij2744e8a2011-05-02 20:50:54 +0200206 spin_lock_init(&pindesc->lock);
207
208 /* Set owner */
209 pindesc->pctldev = pctldev;
210
Stephen Warren9af1e442011-10-19 16:19:27 -0600211 /* Copy basic pin info */
Linus Walleij8dc6ae42012-02-01 18:11:40 +0100212 if (name) {
Linus Walleijca53c5f2011-12-14 20:33:37 +0100213 pindesc->name = name;
214 } else {
215 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
216 if (pindesc->name == NULL)
217 return -ENOMEM;
218 pindesc->dynamic_name = true;
219 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200220
Linus Walleij2744e8a2011-05-02 20:50:54 +0200221 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200222 pr_debug("registered pin %d (%s) on %s\n",
Linus Walleijca53c5f2011-12-14 20:33:37 +0100223 number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200224 return 0;
225}
226
227static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
228 struct pinctrl_pin_desc const *pins,
229 unsigned num_descs)
230{
231 unsigned i;
232 int ret = 0;
233
234 for (i = 0; i < num_descs; i++) {
235 ret = pinctrl_register_one_pin(pctldev,
236 pins[i].number, pins[i].name);
237 if (ret)
238 return ret;
239 }
240
241 return 0;
242}
243
244/**
245 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
246 * @pctldev: pin controller device to check
247 * @gpio: gpio pin to check taken from the global GPIO pin space
248 *
249 * Tries to match a GPIO pin number to the ranges handled by a certain pin
250 * controller, return the range or NULL
251 */
252static struct pinctrl_gpio_range *
253pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
254{
255 struct pinctrl_gpio_range *range = NULL;
256
257 /* Loop over the ranges */
258 mutex_lock(&pctldev->gpio_ranges_lock);
259 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
260 /* Check if we're in the valid range */
261 if (gpio >= range->base &&
262 gpio < range->base + range->npins) {
263 mutex_unlock(&pctldev->gpio_ranges_lock);
264 return range;
265 }
266 }
267 mutex_unlock(&pctldev->gpio_ranges_lock);
268
269 return NULL;
270}
271
272/**
273 * pinctrl_get_device_gpio_range() - find device for GPIO range
274 * @gpio: the pin to locate the pin controller for
275 * @outdev: the pin control device if found
276 * @outrange: the GPIO range if found
277 *
278 * Find the pin controller handling a certain GPIO pin from the pinspace of
279 * the GPIO subsystem, return the device and the matching GPIO range. Returns
280 * negative if the GPIO range could not be found in any device.
281 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700282static int pinctrl_get_device_gpio_range(unsigned gpio,
283 struct pinctrl_dev **outdev,
284 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200285{
286 struct pinctrl_dev *pctldev = NULL;
287
288 /* Loop over the pin controllers */
289 mutex_lock(&pinctrldev_list_mutex);
290 list_for_each_entry(pctldev, &pinctrldev_list, node) {
291 struct pinctrl_gpio_range *range;
292
293 range = pinctrl_match_gpio_range(pctldev, gpio);
294 if (range != NULL) {
295 *outdev = pctldev;
296 *outrange = range;
297 mutex_unlock(&pinctrldev_list_mutex);
298 return 0;
299 }
300 }
301 mutex_unlock(&pinctrldev_list_mutex);
302
303 return -EINVAL;
304}
305
306/**
307 * pinctrl_add_gpio_range() - register a GPIO range for a controller
308 * @pctldev: pin controller device to add the range to
309 * @range: the GPIO range to add
310 *
311 * This adds a range of GPIOs to be handled by a certain pin controller. Call
312 * this to register handled ranges after registering your pin controller.
313 */
314void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
315 struct pinctrl_gpio_range *range)
316{
317 mutex_lock(&pctldev->gpio_ranges_lock);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700318 list_add_tail(&range->node, &pctldev->gpio_ranges);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200319 mutex_unlock(&pctldev->gpio_ranges_lock);
320}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700321EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200322
323/**
324 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
325 * @pctldev: pin controller device to remove the range from
326 * @range: the GPIO range to remove
327 */
328void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
329 struct pinctrl_gpio_range *range)
330{
331 mutex_lock(&pctldev->gpio_ranges_lock);
332 list_del(&range->node);
333 mutex_unlock(&pctldev->gpio_ranges_lock);
334}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700335EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200336
Linus Walleij7afde8b2011-10-19 17:07:16 +0200337/**
338 * pinctrl_get_group_selector() - returns the group selector for a group
339 * @pctldev: the pin controller handling the group
340 * @pin_group: the pin group to look up
341 */
342int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
343 const char *pin_group)
344{
345 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
346 unsigned group_selector = 0;
347
348 while (pctlops->list_groups(pctldev, group_selector) >= 0) {
349 const char *gname = pctlops->get_group_name(pctldev,
350 group_selector);
351 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700352 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200353 "found group selector %u for %s\n",
354 group_selector,
355 pin_group);
356 return group_selector;
357 }
358
359 group_selector++;
360 }
361
Stephen Warren51cd24e2011-12-09 16:59:05 -0700362 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200363 pin_group);
364
365 return -EINVAL;
366}
367
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100368/**
369 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
370 * @gpio: the GPIO pin number from the GPIO subsystem number space
371 *
372 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
373 * as part of their gpio_request() semantics, platforms and individual drivers
374 * shall *NOT* request GPIO pins to be muxed in.
375 */
376int pinctrl_request_gpio(unsigned gpio)
377{
378 struct pinctrl_dev *pctldev;
379 struct pinctrl_gpio_range *range;
380 int ret;
381 int pin;
382
383 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
384 if (ret)
385 return -EINVAL;
386
387 /* Convert to the pin controllers number space */
388 pin = gpio - range->base + range->pin_base;
389
390 return pinmux_request_gpio(pctldev, range, pin, gpio);
391}
392EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
393
394/**
395 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
396 * @gpio: the GPIO pin number from the GPIO subsystem number space
397 *
398 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
399 * as part of their gpio_free() semantics, platforms and individual drivers
400 * shall *NOT* request GPIO pins to be muxed out.
401 */
402void pinctrl_free_gpio(unsigned gpio)
403{
404 struct pinctrl_dev *pctldev;
405 struct pinctrl_gpio_range *range;
406 int ret;
407 int pin;
408
409 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
410 if (ret)
411 return;
412
413 /* Convert to the pin controllers number space */
414 pin = gpio - range->base + range->pin_base;
415
416 return pinmux_free_gpio(pctldev, pin, range);
417}
418EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
419
420static int pinctrl_gpio_direction(unsigned gpio, bool input)
421{
422 struct pinctrl_dev *pctldev;
423 struct pinctrl_gpio_range *range;
424 int ret;
425 int pin;
426
427 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
428 if (ret)
429 return ret;
430
431 /* Convert to the pin controllers number space */
432 pin = gpio - range->base + range->pin_base;
433
434 return pinmux_gpio_direction(pctldev, range, pin, input);
435}
436
437/**
438 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
439 * @gpio: the GPIO pin number from the GPIO subsystem number space
440 *
441 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
442 * as part of their gpio_direction_input() semantics, platforms and individual
443 * drivers shall *NOT* touch pin control GPIO calls.
444 */
445int pinctrl_gpio_direction_input(unsigned gpio)
446{
447 return pinctrl_gpio_direction(gpio, true);
448}
449EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
450
451/**
452 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
453 * @gpio: the GPIO pin number from the GPIO subsystem number space
454 *
455 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
456 * as part of their gpio_direction_output() semantics, platforms and individual
457 * drivers shall *NOT* touch pin control GPIO calls.
458 */
459int pinctrl_gpio_direction_output(unsigned gpio)
460{
461 return pinctrl_gpio_direction(gpio, false);
462}
463EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
464
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700465static struct pinctrl *pinctrl_get_locked(struct device *dev, const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100466{
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100467 struct pinctrl_dev *pctldev = NULL;
Stephen Warren1681f5a2012-02-22 14:25:58 -0700468 const char *devname;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100469 struct pinctrl *p;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100470 unsigned num_maps = 0;
471 int ret = -ENODEV;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700472 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100473 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700474 struct pinctrl_map const *map;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100475
Stephen Warren1681f5a2012-02-22 14:25:58 -0700476 /* We must have a dev name */
477 if (WARN_ON(!dev))
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100478 return ERR_PTR(-EINVAL);
479
Stephen Warren1681f5a2012-02-22 14:25:58 -0700480 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100481
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700482 dev_dbg(dev, "pinctrl_get() for device %s state %s\n", devname, name);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100483
484 /*
485 * create the state cookie holder struct pinctrl for each
486 * mapping, this is what consumers will get when requesting
487 * a pin control handle with pinctrl_get()
488 */
Stephen Warren02f5b982012-02-22 14:26:00 -0700489 p = kzalloc(sizeof(*p), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700490 if (p == NULL) {
491 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100492 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700493 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100494 mutex_init(&p->mutex);
495 pinmux_init_pinctrl_handle(p);
496
497 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700498 for_each_maps(maps_node, i, map) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100499 /*
500 * First, try to find the pctldev given in the map
501 */
502 pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
503 if (!pctldev) {
Stephen Warrenb1eed4e2012-02-19 23:45:53 -0700504 dev_err(dev, "unknown pinctrl device %s in map entry",
505 map->ctrl_dev_name);
506 pinmux_put(p);
507 kfree(p);
508 /* Eventually, this should trigger deferred probe */
509 return ERR_PTR(-ENODEV);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100510 }
511
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700512 dev_dbg(dev, "in map, found pctldev %s to handle function %s",
513 dev_name(pctldev->dev), map->function);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100514
Stephen Warren1681f5a2012-02-22 14:25:58 -0700515 /* Map must be for this device */
516 if (strcmp(map->dev_name, devname))
517 continue;
518
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100519 /*
520 * If we're looking for a specific named map, this must match,
521 * else we loop and look for the next.
522 */
523 if (name != NULL) {
524 if (map->name == NULL)
525 continue;
526 if (strcmp(map->name, name))
527 continue;
528 }
529
Stephen Warren1681f5a2012-02-22 14:25:58 -0700530 ret = pinmux_apply_muxmap(pctldev, p, dev, devname, map);
531 if (ret) {
532 kfree(p);
533 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100534 }
Stephen Warren1681f5a2012-02-22 14:25:58 -0700535 num_maps++;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100536 }
537
Stephen Warrenf026fe32012-02-19 23:45:51 -0700538 /*
539 * This may be perfectly legitimate. An IP block may get re-used
540 * across SoCs. Not all of those SoCs may need pinmux settings for the
541 * IP block, e.g. if one SoC dedicates pins to that function but
542 * another doesn't. The driver won't know this, and will always
543 * attempt to set up the pinmux. The mapping table defines whether any
544 * HW programming is actually needed.
545 */
546 if (!num_maps)
547 dev_info(dev, "zero maps found for mapping %s\n", name);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100548
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700549 dev_dbg(dev, "found %u maps for device %s state %s\n",
550 num_maps, devname, name ? name : "(undefined)");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100551
552 /* Add the pinmux to the global list */
553 mutex_lock(&pinctrl_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700554 list_add_tail(&p->node, &pinctrl_list);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100555 mutex_unlock(&pinctrl_list_mutex);
556
557 return p;
558}
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700559
560/**
561 * pinctrl_get() - retrieves the pin controller handle for a certain device
562 * @dev: the device to get the pin controller handle for
563 * @name: an optional specific control mapping name or NULL, the name is only
564 * needed if you want to have more than one mapping per device, or if you
565 * need an anonymous pin control (not tied to any specific device)
566 */
567struct pinctrl *pinctrl_get(struct device *dev, const char *name)
568{
569 struct pinctrl *p;
570
571 mutex_lock(&pinctrl_maps_mutex);
572 p = pinctrl_get_locked(dev, name);
573 mutex_unlock(&pinctrl_maps_mutex);
574
575 return p;
576}
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100577EXPORT_SYMBOL_GPL(pinctrl_get);
578
579/**
580 * pinctrl_put() - release a previously claimed pin control handle
581 * @p: a pin control handle previously claimed by pinctrl_get()
582 */
583void pinctrl_put(struct pinctrl *p)
584{
585 if (p == NULL)
586 return;
587
588 mutex_lock(&p->mutex);
589 if (p->usecount)
590 pr_warn("releasing pin control handle with active users!\n");
591 /* Free the groups and all acquired pins */
592 pinmux_put(p);
593 mutex_unlock(&p->mutex);
594
595 /* Remove from list */
596 mutex_lock(&pinctrl_list_mutex);
597 list_del(&p->node);
598 mutex_unlock(&pinctrl_list_mutex);
599
600 kfree(p);
601}
602EXPORT_SYMBOL_GPL(pinctrl_put);
603
604/**
605 * pinctrl_enable() - enable a certain pin controller setting
606 * @p: the pin control handle to enable, previously claimed by pinctrl_get()
607 */
608int pinctrl_enable(struct pinctrl *p)
609{
610 int ret = 0;
611
612 if (p == NULL)
613 return -EINVAL;
614 mutex_lock(&p->mutex);
615 if (p->usecount++ == 0) {
616 ret = pinmux_enable(p);
617 if (ret)
618 p->usecount--;
619 }
620 mutex_unlock(&p->mutex);
621 return ret;
622}
623EXPORT_SYMBOL_GPL(pinctrl_enable);
624
625/**
626 * pinctrl_disable() - disable a certain pin control setting
627 * @p: the pin control handle to disable, previously claimed by pinctrl_get()
628 */
629void pinctrl_disable(struct pinctrl *p)
630{
631 if (p == NULL)
632 return;
633
634 mutex_lock(&p->mutex);
635 if (--p->usecount == 0) {
636 pinmux_disable(p);
637 }
638 mutex_unlock(&p->mutex);
639}
640EXPORT_SYMBOL_GPL(pinctrl_disable);
641
642/**
643 * pinctrl_register_mappings() - register a set of pin controller mappings
Stephen Warren13398a42012-02-19 23:45:41 -0700644 * @maps: the pincontrol mappings table to register. This should probably be
645 * marked with __initdata so it can be discarded after boot. This
646 * function will perform a shallow copy for the mapping entries.
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100647 * @num_maps: the number of maps in the mapping table
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100648 */
Stephen Warren13398a42012-02-19 23:45:41 -0700649int pinctrl_register_mappings(struct pinctrl_map const *maps,
650 unsigned num_maps)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100651{
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100652 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700653 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100654
655 pr_debug("add %d pinmux maps\n", num_maps);
656
657 /* First sanity check the new mapping */
658 for (i = 0; i < num_maps; i++) {
659 if (!maps[i].name) {
660 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700661 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100662 return -EINVAL;
663 }
664
665 if (!maps[i].ctrl_dev_name) {
666 pr_err("failed to register map %s (%d): no pin control device given\n",
667 maps[i].name, i);
668 return -EINVAL;
669 }
670
671 if (!maps[i].function) {
672 pr_err("failed to register map %s (%d): no function ID given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700673 maps[i].name, i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100674 return -EINVAL;
675 }
676
Stephen Warren1681f5a2012-02-22 14:25:58 -0700677 if (!maps[i].dev_name) {
678 pr_err("failed to register map %s (%d): no device given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700679 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -0700680 return -EINVAL;
681 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100682 }
683
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700684 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
685 if (!maps_node) {
686 pr_err("failed to alloc struct pinctrl_maps\n");
687 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100688 }
689
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700690 maps_node->num_maps = num_maps;
691 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps, GFP_KERNEL);
692 if (!maps_node->maps) {
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700693 pr_err("failed to duplicate mapping table\n");
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700694 kfree(maps_node);
695 return -ENOMEM;
696 }
697
698 mutex_lock(&pinctrl_maps_mutex);
699 list_add_tail(&maps_node->node, &pinctrl_maps);
700 mutex_unlock(&pinctrl_maps_mutex);
701
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100702 return 0;
703}
704
705/* Hog a single map entry and add to the hoglist */
706static int pinctrl_hog_map(struct pinctrl_dev *pctldev,
707 struct pinctrl_map const *map)
708{
709 struct pinctrl_hog *hog;
710 struct pinctrl *p;
711 int ret;
712
Stephen Warren02f5b982012-02-22 14:26:00 -0700713 hog = kzalloc(sizeof(*hog), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700714 if (!hog) {
715 dev_err(pctldev->dev, "failed to alloc struct pinctrl_hog\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100716 return -ENOMEM;
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700717 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100718
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700719 p = pinctrl_get_locked(pctldev->dev, map->name);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100720 if (IS_ERR(p)) {
721 kfree(hog);
722 dev_err(pctldev->dev,
723 "could not get the %s pin control mapping for hogging\n",
724 map->name);
725 return PTR_ERR(p);
726 }
727
728 ret = pinctrl_enable(p);
729 if (ret) {
730 pinctrl_put(p);
731 kfree(hog);
732 dev_err(pctldev->dev,
733 "could not enable the %s pin control mapping for hogging\n",
734 map->name);
735 return ret;
736 }
737
738 hog->map = map;
739 hog->p = p;
740
741 dev_info(pctldev->dev, "hogged map %s, function %s\n", map->name,
742 map->function);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700743 list_add_tail(&hog->node, &pctldev->pinctrl_hogs);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100744
745 return 0;
746}
747
748/**
749 * pinctrl_hog_maps() - hog specific map entries on controller device
750 * @pctldev: the pin control device to hog entries on
751 *
752 * When the pin controllers are registered, there may be some specific pinmux
753 * map entries that need to be hogged, i.e. get+enabled until the system shuts
754 * down.
755 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700756static int pinctrl_hog_maps(struct pinctrl_dev *pctldev)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100757{
758 struct device *dev = pctldev->dev;
759 const char *devname = dev_name(dev);
760 int ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700761 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100762 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700763 struct pinctrl_map const *map;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100764
765 INIT_LIST_HEAD(&pctldev->pinctrl_hogs);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100766
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700767 mutex_lock(&pinctrl_maps_mutex);
768 for_each_maps(maps_node, i, map) {
Stephen Warren9891d982012-02-19 23:45:50 -0700769 if (!strcmp(map->ctrl_dev_name, devname) &&
Linus Walleij77a59882012-02-10 01:34:12 +0100770 !strcmp(map->dev_name, devname)) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100771 /* OK time to hog! */
772 ret = pinctrl_hog_map(pctldev, map);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700773 if (ret) {
774 mutex_unlock(&pinctrl_maps_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100775 return ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700776 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100777 }
778 }
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700779 mutex_unlock(&pinctrl_maps_mutex);
780
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100781 return 0;
782}
783
784/**
785 * pinctrl_unhog_maps() - unhog specific map entries on controller device
786 * @pctldev: the pin control device to unhog entries on
787 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700788static void pinctrl_unhog_maps(struct pinctrl_dev *pctldev)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100789{
790 struct list_head *node, *tmp;
791
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100792 list_for_each_safe(node, tmp, &pctldev->pinctrl_hogs) {
793 struct pinctrl_hog *hog =
794 list_entry(node, struct pinctrl_hog, node);
795 pinctrl_disable(hog->p);
796 pinctrl_put(hog->p);
797 list_del(node);
798 kfree(hog);
799 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100800}
801
Linus Walleij2744e8a2011-05-02 20:50:54 +0200802#ifdef CONFIG_DEBUG_FS
803
804static int pinctrl_pins_show(struct seq_file *s, void *what)
805{
806 struct pinctrl_dev *pctldev = s->private;
807 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +0900808 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200809
810 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200811
Chanho Park706e8522012-01-03 16:47:50 +0900812 /* The pin number can be retrived from the pin controller descriptor */
813 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200814 struct pin_desc *desc;
815
Chanho Park706e8522012-01-03 16:47:50 +0900816 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200817 desc = pin_desc_get(pctldev, pin);
818 /* Pin space may be sparse */
819 if (desc == NULL)
820 continue;
821
822 seq_printf(s, "pin %d (%s) ", pin,
823 desc->name ? desc->name : "unnamed");
824
825 /* Driver-specific info per pin */
826 if (ops->pin_dbg_show)
827 ops->pin_dbg_show(pctldev, s, pin);
828
829 seq_puts(s, "\n");
830 }
831
832 return 0;
833}
834
835static int pinctrl_groups_show(struct seq_file *s, void *what)
836{
837 struct pinctrl_dev *pctldev = s->private;
838 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
839 unsigned selector = 0;
840
841 /* No grouping */
842 if (!ops)
843 return 0;
844
845 seq_puts(s, "registered pin groups:\n");
846 while (ops->list_groups(pctldev, selector) >= 0) {
Stephen Warrena5818a82011-10-19 16:19:25 -0600847 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200848 unsigned num_pins;
849 const char *gname = ops->get_group_name(pctldev, selector);
850 int ret;
851 int i;
852
853 ret = ops->get_group_pins(pctldev, selector,
854 &pins, &num_pins);
855 if (ret)
856 seq_printf(s, "%s [ERROR GETTING PINS]\n",
857 gname);
858 else {
859 seq_printf(s, "group: %s, pins = [ ", gname);
860 for (i = 0; i < num_pins; i++)
861 seq_printf(s, "%d ", pins[i]);
862 seq_puts(s, "]\n");
863 }
864 selector++;
865 }
866
867
868 return 0;
869}
870
871static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
872{
873 struct pinctrl_dev *pctldev = s->private;
874 struct pinctrl_gpio_range *range = NULL;
875
876 seq_puts(s, "GPIO ranges handled:\n");
877
878 /* Loop over the ranges */
879 mutex_lock(&pctldev->gpio_ranges_lock);
880 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Linus Walleij75d66422011-11-16 09:58:51 +0100881 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
882 range->id, range->name,
883 range->base, (range->base + range->npins - 1),
884 range->pin_base,
885 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +0200886 }
887 mutex_unlock(&pctldev->gpio_ranges_lock);
888
889 return 0;
890}
891
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100892static int pinctrl_maps_show(struct seq_file *s, void *what)
893{
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700894 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100895 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700896 struct pinctrl_map const *map;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100897
898 seq_puts(s, "Pinctrl maps:\n");
899
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700900 mutex_lock(&pinctrl_maps_mutex);
901 for_each_maps(maps_node, i, map) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100902 seq_printf(s, "%s:\n", map->name);
Stephen Warren1681f5a2012-02-22 14:25:58 -0700903 seq_printf(s, " device: %s\n", map->dev_name);
904 seq_printf(s, " controlling device %s\n", map->ctrl_dev_name);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100905 seq_printf(s, " function: %s\n", map->function);
906 seq_printf(s, " group: %s\n", map->group ? map->group :
907 "(default)");
908 }
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700909 mutex_unlock(&pinctrl_maps_mutex);
910
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100911 return 0;
912}
913
914static int pinmux_hogs_show(struct seq_file *s, void *what)
915{
916 struct pinctrl_dev *pctldev = s->private;
917 struct pinctrl_hog *hog;
918
919 seq_puts(s, "Pin control map hogs held by device\n");
920
921 list_for_each_entry(hog, &pctldev->pinctrl_hogs, node)
922 seq_printf(s, "%s\n", hog->map->name);
923
924 return 0;
925}
926
Linus Walleij2744e8a2011-05-02 20:50:54 +0200927static int pinctrl_devices_show(struct seq_file *s, void *what)
928{
929 struct pinctrl_dev *pctldev;
930
Linus Walleijae6b4d82011-10-19 18:14:33 +0200931 seq_puts(s, "name [pinmux] [pinconf]\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200932 mutex_lock(&pinctrldev_list_mutex);
933 list_for_each_entry(pctldev, &pinctrldev_list, node) {
934 seq_printf(s, "%s ", pctldev->desc->name);
935 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +0200936 seq_puts(s, "yes ");
937 else
938 seq_puts(s, "no ");
939 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200940 seq_puts(s, "yes");
941 else
942 seq_puts(s, "no");
943 seq_puts(s, "\n");
944 }
945 mutex_unlock(&pinctrldev_list_mutex);
946
947 return 0;
948}
949
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100950static int pinctrl_show(struct seq_file *s, void *what)
951{
952 struct pinctrl *p;
953
954 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
955 list_for_each_entry(p, &pinctrl_list, node) {
956 struct pinctrl_dev *pctldev = p->pctldev;
957
958 if (!pctldev) {
959 seq_puts(s, "NO PIN CONTROLLER DEVICE\n");
960 continue;
961 }
962
963 seq_printf(s, "device: %s",
964 pinctrl_dev_get_name(p->pctldev));
965
966 pinmux_dbg_show(s, p);
967
968 seq_printf(s, " users: %u map-> %s\n",
969 p->usecount,
970 p->dev ? dev_name(p->dev) : "(system)");
971 }
972
973 return 0;
974}
975
Linus Walleij2744e8a2011-05-02 20:50:54 +0200976static int pinctrl_pins_open(struct inode *inode, struct file *file)
977{
978 return single_open(file, pinctrl_pins_show, inode->i_private);
979}
980
981static int pinctrl_groups_open(struct inode *inode, struct file *file)
982{
983 return single_open(file, pinctrl_groups_show, inode->i_private);
984}
985
986static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
987{
988 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
989}
990
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100991static int pinctrl_maps_open(struct inode *inode, struct file *file)
992{
993 return single_open(file, pinctrl_maps_show, inode->i_private);
994}
995
996static int pinmux_hogs_open(struct inode *inode, struct file *file)
997{
998 return single_open(file, pinmux_hogs_show, inode->i_private);
999}
1000
Linus Walleij2744e8a2011-05-02 20:50:54 +02001001static int pinctrl_devices_open(struct inode *inode, struct file *file)
1002{
1003 return single_open(file, pinctrl_devices_show, NULL);
1004}
1005
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001006static int pinctrl_open(struct inode *inode, struct file *file)
1007{
1008 return single_open(file, pinctrl_show, NULL);
1009}
1010
Linus Walleij2744e8a2011-05-02 20:50:54 +02001011static const struct file_operations pinctrl_pins_ops = {
1012 .open = pinctrl_pins_open,
1013 .read = seq_read,
1014 .llseek = seq_lseek,
1015 .release = single_release,
1016};
1017
1018static const struct file_operations pinctrl_groups_ops = {
1019 .open = pinctrl_groups_open,
1020 .read = seq_read,
1021 .llseek = seq_lseek,
1022 .release = single_release,
1023};
1024
1025static const struct file_operations pinctrl_gpioranges_ops = {
1026 .open = pinctrl_gpioranges_open,
1027 .read = seq_read,
1028 .llseek = seq_lseek,
1029 .release = single_release,
1030};
1031
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001032static const struct file_operations pinctrl_maps_ops = {
1033 .open = pinctrl_maps_open,
1034 .read = seq_read,
1035 .llseek = seq_lseek,
1036 .release = single_release,
1037};
1038
1039static const struct file_operations pinmux_hogs_ops = {
1040 .open = pinmux_hogs_open,
1041 .read = seq_read,
1042 .llseek = seq_lseek,
1043 .release = single_release,
1044};
1045
Linus Walleij2744e8a2011-05-02 20:50:54 +02001046static const struct file_operations pinctrl_devices_ops = {
1047 .open = pinctrl_devices_open,
1048 .read = seq_read,
1049 .llseek = seq_lseek,
1050 .release = single_release,
1051};
1052
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001053static const struct file_operations pinctrl_ops = {
1054 .open = pinctrl_open,
1055 .read = seq_read,
1056 .llseek = seq_lseek,
1057 .release = single_release,
1058};
1059
Linus Walleij2744e8a2011-05-02 20:50:54 +02001060static struct dentry *debugfs_root;
1061
1062static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1063{
Tony Lindgren02157162012-01-20 08:17:22 -08001064 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001065
Stephen Warren51cd24e2011-12-09 16:59:05 -07001066 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001067 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001068 pctldev->device_root = device_root;
1069
Linus Walleij2744e8a2011-05-02 20:50:54 +02001070 if (IS_ERR(device_root) || !device_root) {
1071 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001072 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001073 return;
1074 }
1075 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1076 device_root, pctldev, &pinctrl_pins_ops);
1077 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1078 device_root, pctldev, &pinctrl_groups_ops);
1079 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1080 device_root, pctldev, &pinctrl_gpioranges_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001081 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1082 device_root, pctldev, &pinctrl_maps_ops);
1083 debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO,
1084 device_root, pctldev, &pinmux_hogs_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001085 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +02001086 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001087}
1088
Tony Lindgren02157162012-01-20 08:17:22 -08001089static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1090{
1091 debugfs_remove_recursive(pctldev->device_root);
1092}
1093
Linus Walleij2744e8a2011-05-02 20:50:54 +02001094static void pinctrl_init_debugfs(void)
1095{
1096 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1097 if (IS_ERR(debugfs_root) || !debugfs_root) {
1098 pr_warn("failed to create debugfs directory\n");
1099 debugfs_root = NULL;
1100 return;
1101 }
1102
1103 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1104 debugfs_root, NULL, &pinctrl_devices_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001105 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1106 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001107}
1108
1109#else /* CONFIG_DEBUG_FS */
1110
1111static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1112{
1113}
1114
1115static void pinctrl_init_debugfs(void)
1116{
1117}
1118
Tony Lindgren02157162012-01-20 08:17:22 -08001119static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1120{
1121}
1122
Linus Walleij2744e8a2011-05-02 20:50:54 +02001123#endif
1124
1125/**
1126 * pinctrl_register() - register a pin controller device
1127 * @pctldesc: descriptor for this pin controller
1128 * @dev: parent device for this pin controller
1129 * @driver_data: private pin controller data for this pin controller
1130 */
1131struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1132 struct device *dev, void *driver_data)
1133{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001134 struct pinctrl_dev *pctldev;
1135 int ret;
1136
1137 if (pctldesc == NULL)
1138 return NULL;
1139 if (pctldesc->name == NULL)
1140 return NULL;
1141
Stephen Warren02f5b982012-02-22 14:26:00 -07001142 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001143 if (pctldev == NULL) {
1144 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001145 return NULL;
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001146 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001147
1148 /* Initialize pin control device struct */
1149 pctldev->owner = pctldesc->owner;
1150 pctldev->desc = pctldesc;
1151 pctldev->driver_data = driver_data;
1152 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001153 INIT_LIST_HEAD(&pctldev->gpio_ranges);
1154 mutex_init(&pctldev->gpio_ranges_lock);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001155 pctldev->dev = dev;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001156
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001157 /* If we're implementing pinmuxing, check the ops for sanity */
1158 if (pctldesc->pmxops) {
1159 ret = pinmux_check_ops(pctldev);
1160 if (ret) {
1161 pr_err("%s pinmux ops lacks necessary functions\n",
1162 pctldesc->name);
1163 goto out_err;
1164 }
1165 }
1166
1167 /* If we're implementing pinconfig, check the ops for sanity */
1168 if (pctldesc->confops) {
1169 ret = pinconf_check_ops(pctldev);
1170 if (ret) {
1171 pr_err("%s pin config ops lacks necessary functions\n",
1172 pctldesc->name);
1173 goto out_err;
1174 }
1175 }
1176
Linus Walleij2744e8a2011-05-02 20:50:54 +02001177 /* Register all the pins */
1178 pr_debug("try to register %d pins on %s...\n",
1179 pctldesc->npins, pctldesc->name);
1180 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1181 if (ret) {
1182 pr_err("error during pin registration\n");
1183 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1184 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001185 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001186 }
1187
Linus Walleij2744e8a2011-05-02 20:50:54 +02001188 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -07001189 list_add_tail(&pctldev->node, &pinctrldev_list);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001190 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleije93bcee2012-02-09 07:23:28 +01001191 pinctrl_hog_maps(pctldev);
Stephen Warren2304b472012-02-22 14:26:01 -07001192 pinctrl_init_device_debugfs(pctldev);
1193
Linus Walleij2744e8a2011-05-02 20:50:54 +02001194 return pctldev;
1195
Stephen Warren51cd24e2011-12-09 16:59:05 -07001196out_err:
1197 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001198 return NULL;
1199}
1200EXPORT_SYMBOL_GPL(pinctrl_register);
1201
1202/**
1203 * pinctrl_unregister() - unregister pinmux
1204 * @pctldev: pin controller to unregister
1205 *
1206 * Called by pinmux drivers to unregister a pinmux.
1207 */
1208void pinctrl_unregister(struct pinctrl_dev *pctldev)
1209{
1210 if (pctldev == NULL)
1211 return;
1212
Tony Lindgren02157162012-01-20 08:17:22 -08001213 pinctrl_remove_device_debugfs(pctldev);
Linus Walleije93bcee2012-02-09 07:23:28 +01001214 pinctrl_unhog_maps(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001215 /* TODO: check that no pinmuxes are still active? */
1216 mutex_lock(&pinctrldev_list_mutex);
1217 list_del(&pctldev->node);
1218 mutex_unlock(&pinctrldev_list_mutex);
1219 /* Destroy descriptor tree */
1220 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1221 pctldev->desc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001222 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001223}
1224EXPORT_SYMBOL_GPL(pinctrl_unregister);
1225
1226static int __init pinctrl_init(void)
1227{
1228 pr_info("initialized pinctrl subsystem\n");
1229 pinctrl_init_debugfs();
1230 return 0;
1231}
1232
1233/* init early since many drivers really need to initialized pinmux early */
1234core_initcall(pinctrl_init);