blob: b6e3c35c028e6768b35c3d52eb02c56e5713a650 [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
Marek Belisko33d58942011-10-31 21:27:52 +0100118struct pin_desc *pin_desc_get(struct pinctrl_dev *pctldev, unsigned int pin)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200119{
120 struct pin_desc *pindesc;
121 unsigned long flags;
122
123 spin_lock_irqsave(&pctldev->pin_desc_tree_lock, flags);
124 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree, pin);
125 spin_unlock_irqrestore(&pctldev->pin_desc_tree_lock, flags);
126
127 return pindesc;
128}
129
130/**
Linus Walleijae6b4d82011-10-19 18:14:33 +0200131 * pin_get_from_name() - look up a pin number from a name
132 * @pctldev: the pin control device to lookup the pin on
133 * @name: the name of the pin to look up
134 */
135int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
136{
Chanho Park706e8522012-01-03 16:47:50 +0900137 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200138
Chanho Park706e8522012-01-03 16:47:50 +0900139 /* The pin number can be retrived from the pin controller descriptor */
140 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200141 struct pin_desc *desc;
142
Chanho Park706e8522012-01-03 16:47:50 +0900143 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200144 desc = pin_desc_get(pctldev, pin);
145 /* Pin space may be sparse */
146 if (desc == NULL)
147 continue;
148 if (desc->name && !strcmp(name, desc->name))
149 return pin;
150 }
151
152 return -EINVAL;
153}
154
155/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200156 * pin_is_valid() - check if pin exists on controller
157 * @pctldev: the pin control device to check the pin on
158 * @pin: pin to check, use the local pin controller index number
159 *
160 * This tells us whether a certain pin exist on a certain pin controller or
161 * not. Pin lists may be sparse, so some pins may not exist.
162 */
163bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
164{
165 struct pin_desc *pindesc;
166
167 if (pin < 0)
168 return false;
169
170 pindesc = pin_desc_get(pctldev, pin);
171 if (pindesc == NULL)
172 return false;
173
174 return true;
175}
176EXPORT_SYMBOL_GPL(pin_is_valid);
177
178/* Deletes a range of pin descriptors */
179static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
180 const struct pinctrl_pin_desc *pins,
181 unsigned num_pins)
182{
183 int i;
184
185 spin_lock(&pctldev->pin_desc_tree_lock);
186 for (i = 0; i < num_pins; i++) {
187 struct pin_desc *pindesc;
188
189 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
190 pins[i].number);
191 if (pindesc != NULL) {
192 radix_tree_delete(&pctldev->pin_desc_tree,
193 pins[i].number);
Linus Walleijca53c5f2011-12-14 20:33:37 +0100194 if (pindesc->dynamic_name)
195 kfree(pindesc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200196 }
197 kfree(pindesc);
198 }
199 spin_unlock(&pctldev->pin_desc_tree_lock);
200}
201
202static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
203 unsigned number, const char *name)
204{
205 struct pin_desc *pindesc;
206
207 pindesc = pin_desc_get(pctldev, number);
208 if (pindesc != NULL) {
209 pr_err("pin %d already registered on %s\n", number,
210 pctldev->desc->name);
211 return -EINVAL;
212 }
213
214 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
215 if (pindesc == NULL)
216 return -ENOMEM;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200217
Linus Walleij2744e8a2011-05-02 20:50:54 +0200218 spin_lock_init(&pindesc->lock);
219
220 /* Set owner */
221 pindesc->pctldev = pctldev;
222
Stephen Warren9af1e442011-10-19 16:19:27 -0600223 /* Copy basic pin info */
Linus Walleij8dc6ae42012-02-01 18:11:40 +0100224 if (name) {
Linus Walleijca53c5f2011-12-14 20:33:37 +0100225 pindesc->name = name;
226 } else {
227 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
228 if (pindesc->name == NULL)
229 return -ENOMEM;
230 pindesc->dynamic_name = true;
231 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200232
233 spin_lock(&pctldev->pin_desc_tree_lock);
234 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
235 spin_unlock(&pctldev->pin_desc_tree_lock);
236 pr_debug("registered pin %d (%s) on %s\n",
Linus Walleijca53c5f2011-12-14 20:33:37 +0100237 number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200238 return 0;
239}
240
241static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
242 struct pinctrl_pin_desc const *pins,
243 unsigned num_descs)
244{
245 unsigned i;
246 int ret = 0;
247
248 for (i = 0; i < num_descs; i++) {
249 ret = pinctrl_register_one_pin(pctldev,
250 pins[i].number, pins[i].name);
251 if (ret)
252 return ret;
253 }
254
255 return 0;
256}
257
258/**
259 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
260 * @pctldev: pin controller device to check
261 * @gpio: gpio pin to check taken from the global GPIO pin space
262 *
263 * Tries to match a GPIO pin number to the ranges handled by a certain pin
264 * controller, return the range or NULL
265 */
266static struct pinctrl_gpio_range *
267pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
268{
269 struct pinctrl_gpio_range *range = NULL;
270
271 /* Loop over the ranges */
272 mutex_lock(&pctldev->gpio_ranges_lock);
273 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
274 /* Check if we're in the valid range */
275 if (gpio >= range->base &&
276 gpio < range->base + range->npins) {
277 mutex_unlock(&pctldev->gpio_ranges_lock);
278 return range;
279 }
280 }
281 mutex_unlock(&pctldev->gpio_ranges_lock);
282
283 return NULL;
284}
285
286/**
287 * pinctrl_get_device_gpio_range() - find device for GPIO range
288 * @gpio: the pin to locate the pin controller for
289 * @outdev: the pin control device if found
290 * @outrange: the GPIO range if found
291 *
292 * Find the pin controller handling a certain GPIO pin from the pinspace of
293 * the GPIO subsystem, return the device and the matching GPIO range. Returns
294 * negative if the GPIO range could not be found in any device.
295 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700296static int pinctrl_get_device_gpio_range(unsigned gpio,
297 struct pinctrl_dev **outdev,
298 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200299{
300 struct pinctrl_dev *pctldev = NULL;
301
302 /* Loop over the pin controllers */
303 mutex_lock(&pinctrldev_list_mutex);
304 list_for_each_entry(pctldev, &pinctrldev_list, node) {
305 struct pinctrl_gpio_range *range;
306
307 range = pinctrl_match_gpio_range(pctldev, gpio);
308 if (range != NULL) {
309 *outdev = pctldev;
310 *outrange = range;
311 mutex_unlock(&pinctrldev_list_mutex);
312 return 0;
313 }
314 }
315 mutex_unlock(&pinctrldev_list_mutex);
316
317 return -EINVAL;
318}
319
320/**
321 * pinctrl_add_gpio_range() - register a GPIO range for a controller
322 * @pctldev: pin controller device to add the range to
323 * @range: the GPIO range to add
324 *
325 * This adds a range of GPIOs to be handled by a certain pin controller. Call
326 * this to register handled ranges after registering your pin controller.
327 */
328void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
329 struct pinctrl_gpio_range *range)
330{
331 mutex_lock(&pctldev->gpio_ranges_lock);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700332 list_add_tail(&range->node, &pctldev->gpio_ranges);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200333 mutex_unlock(&pctldev->gpio_ranges_lock);
334}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700335EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200336
337/**
338 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
339 * @pctldev: pin controller device to remove the range from
340 * @range: the GPIO range to remove
341 */
342void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
343 struct pinctrl_gpio_range *range)
344{
345 mutex_lock(&pctldev->gpio_ranges_lock);
346 list_del(&range->node);
347 mutex_unlock(&pctldev->gpio_ranges_lock);
348}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700349EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200350
Linus Walleij7afde8b2011-10-19 17:07:16 +0200351/**
352 * pinctrl_get_group_selector() - returns the group selector for a group
353 * @pctldev: the pin controller handling the group
354 * @pin_group: the pin group to look up
355 */
356int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
357 const char *pin_group)
358{
359 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
360 unsigned group_selector = 0;
361
362 while (pctlops->list_groups(pctldev, group_selector) >= 0) {
363 const char *gname = pctlops->get_group_name(pctldev,
364 group_selector);
365 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700366 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200367 "found group selector %u for %s\n",
368 group_selector,
369 pin_group);
370 return group_selector;
371 }
372
373 group_selector++;
374 }
375
Stephen Warren51cd24e2011-12-09 16:59:05 -0700376 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200377 pin_group);
378
379 return -EINVAL;
380}
381
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100382/**
383 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
384 * @gpio: the GPIO pin number from the GPIO subsystem number space
385 *
386 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
387 * as part of their gpio_request() semantics, platforms and individual drivers
388 * shall *NOT* request GPIO pins to be muxed in.
389 */
390int pinctrl_request_gpio(unsigned gpio)
391{
392 struct pinctrl_dev *pctldev;
393 struct pinctrl_gpio_range *range;
394 int ret;
395 int pin;
396
397 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
398 if (ret)
399 return -EINVAL;
400
401 /* Convert to the pin controllers number space */
402 pin = gpio - range->base + range->pin_base;
403
404 return pinmux_request_gpio(pctldev, range, pin, gpio);
405}
406EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
407
408/**
409 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
410 * @gpio: the GPIO pin number from the GPIO subsystem number space
411 *
412 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
413 * as part of their gpio_free() semantics, platforms and individual drivers
414 * shall *NOT* request GPIO pins to be muxed out.
415 */
416void pinctrl_free_gpio(unsigned gpio)
417{
418 struct pinctrl_dev *pctldev;
419 struct pinctrl_gpio_range *range;
420 int ret;
421 int pin;
422
423 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
424 if (ret)
425 return;
426
427 /* Convert to the pin controllers number space */
428 pin = gpio - range->base + range->pin_base;
429
430 return pinmux_free_gpio(pctldev, pin, range);
431}
432EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
433
434static int pinctrl_gpio_direction(unsigned gpio, bool input)
435{
436 struct pinctrl_dev *pctldev;
437 struct pinctrl_gpio_range *range;
438 int ret;
439 int pin;
440
441 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
442 if (ret)
443 return ret;
444
445 /* Convert to the pin controllers number space */
446 pin = gpio - range->base + range->pin_base;
447
448 return pinmux_gpio_direction(pctldev, range, pin, input);
449}
450
451/**
452 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input 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_input() semantics, platforms and individual
457 * drivers shall *NOT* touch pin control GPIO calls.
458 */
459int pinctrl_gpio_direction_input(unsigned gpio)
460{
461 return pinctrl_gpio_direction(gpio, true);
462}
463EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
464
465/**
466 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
467 * @gpio: the GPIO pin number from the GPIO subsystem number space
468 *
469 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
470 * as part of their gpio_direction_output() semantics, platforms and individual
471 * drivers shall *NOT* touch pin control GPIO calls.
472 */
473int pinctrl_gpio_direction_output(unsigned gpio)
474{
475 return pinctrl_gpio_direction(gpio, false);
476}
477EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
478
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700479static struct pinctrl *pinctrl_get_locked(struct device *dev, const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100480{
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100481 struct pinctrl_dev *pctldev = NULL;
482 const char *devname = NULL;
483 struct pinctrl *p;
484 bool found_map;
485 unsigned num_maps = 0;
486 int ret = -ENODEV;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700487 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100488 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700489 struct pinctrl_map const *map;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100490
491 /* We must have dev or ID or both */
492 if (!dev && !name)
493 return ERR_PTR(-EINVAL);
494
495 if (dev)
496 devname = dev_name(dev);
497
498 pr_debug("get pin control handle %s for device %s\n", name,
499 devname ? devname : "(none)");
500
501 /*
502 * create the state cookie holder struct pinctrl for each
503 * mapping, this is what consumers will get when requesting
504 * a pin control handle with pinctrl_get()
505 */
506 p = kzalloc(sizeof(struct pinctrl), GFP_KERNEL);
507 if (p == NULL)
508 return ERR_PTR(-ENOMEM);
509 mutex_init(&p->mutex);
510 pinmux_init_pinctrl_handle(p);
511
512 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700513 for_each_maps(maps_node, i, map) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100514 found_map = false;
515
516 /*
517 * First, try to find the pctldev given in the map
518 */
519 pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
520 if (!pctldev) {
521 pr_warning("could not find a pinctrl device for pinmux function %s, fishy, they shall all have one\n",
522 map->function);
523 pr_warning("given pinctrl device name: %s",
524 map->ctrl_dev_name);
525
526 /* Continue to check the other mappings anyway... */
527 continue;
528 }
529
530 pr_debug("in map, found pctldev %s to handle function %s",
531 dev_name(pctldev->dev), map->function);
532
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100533 /*
534 * If we're looking for a specific named map, this must match,
535 * else we loop and look for the next.
536 */
537 if (name != NULL) {
538 if (map->name == NULL)
539 continue;
540 if (strcmp(map->name, name))
541 continue;
542 }
543
544 /*
545 * This is for the case where no device name is given, we
546 * already know that the function name matches from above
547 * code.
548 */
549 if (!map->dev_name && (name != NULL))
550 found_map = true;
551
552 /* If the mapping has a device set up it must match */
553 if (map->dev_name &&
554 (!devname || !strcmp(map->dev_name, devname)))
555 /* MATCH! */
556 found_map = true;
557
558 /* If this map is applicable, then apply it */
559 if (found_map) {
560 ret = pinmux_apply_muxmap(pctldev, p, dev,
561 devname, map);
562 if (ret) {
563 kfree(p);
564 return ERR_PTR(ret);
565 }
566 num_maps++;
567 }
568 }
569
570 /* We should have atleast one map, right */
571 if (!num_maps) {
572 pr_err("could not find any mux maps for device %s, ID %s\n",
573 devname ? devname : "(anonymous)",
574 name ? name : "(undefined)");
575 kfree(p);
576 return ERR_PTR(-EINVAL);
577 }
578
579 pr_debug("found %u mux maps for device %s, UD %s\n",
580 num_maps,
581 devname ? devname : "(anonymous)",
582 name ? name : "(undefined)");
583
584 /* Add the pinmux to the global list */
585 mutex_lock(&pinctrl_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700586 list_add_tail(&p->node, &pinctrl_list);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100587 mutex_unlock(&pinctrl_list_mutex);
588
589 return p;
590}
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700591
592/**
593 * pinctrl_get() - retrieves the pin controller handle for a certain device
594 * @dev: the device to get the pin controller handle for
595 * @name: an optional specific control mapping name or NULL, the name is only
596 * needed if you want to have more than one mapping per device, or if you
597 * need an anonymous pin control (not tied to any specific device)
598 */
599struct pinctrl *pinctrl_get(struct device *dev, const char *name)
600{
601 struct pinctrl *p;
602
603 mutex_lock(&pinctrl_maps_mutex);
604 p = pinctrl_get_locked(dev, name);
605 mutex_unlock(&pinctrl_maps_mutex);
606
607 return p;
608}
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100609EXPORT_SYMBOL_GPL(pinctrl_get);
610
611/**
612 * pinctrl_put() - release a previously claimed pin control handle
613 * @p: a pin control handle previously claimed by pinctrl_get()
614 */
615void pinctrl_put(struct pinctrl *p)
616{
617 if (p == NULL)
618 return;
619
620 mutex_lock(&p->mutex);
621 if (p->usecount)
622 pr_warn("releasing pin control handle with active users!\n");
623 /* Free the groups and all acquired pins */
624 pinmux_put(p);
625 mutex_unlock(&p->mutex);
626
627 /* Remove from list */
628 mutex_lock(&pinctrl_list_mutex);
629 list_del(&p->node);
630 mutex_unlock(&pinctrl_list_mutex);
631
632 kfree(p);
633}
634EXPORT_SYMBOL_GPL(pinctrl_put);
635
636/**
637 * pinctrl_enable() - enable a certain pin controller setting
638 * @p: the pin control handle to enable, previously claimed by pinctrl_get()
639 */
640int pinctrl_enable(struct pinctrl *p)
641{
642 int ret = 0;
643
644 if (p == NULL)
645 return -EINVAL;
646 mutex_lock(&p->mutex);
647 if (p->usecount++ == 0) {
648 ret = pinmux_enable(p);
649 if (ret)
650 p->usecount--;
651 }
652 mutex_unlock(&p->mutex);
653 return ret;
654}
655EXPORT_SYMBOL_GPL(pinctrl_enable);
656
657/**
658 * pinctrl_disable() - disable a certain pin control setting
659 * @p: the pin control handle to disable, previously claimed by pinctrl_get()
660 */
661void pinctrl_disable(struct pinctrl *p)
662{
663 if (p == NULL)
664 return;
665
666 mutex_lock(&p->mutex);
667 if (--p->usecount == 0) {
668 pinmux_disable(p);
669 }
670 mutex_unlock(&p->mutex);
671}
672EXPORT_SYMBOL_GPL(pinctrl_disable);
673
674/**
675 * pinctrl_register_mappings() - register a set of pin controller mappings
Stephen Warren13398a42012-02-19 23:45:41 -0700676 * @maps: the pincontrol mappings table to register. This should probably be
677 * marked with __initdata so it can be discarded after boot. This
678 * function will perform a shallow copy for the mapping entries.
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100679 * @num_maps: the number of maps in the mapping table
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100680 */
Stephen Warren13398a42012-02-19 23:45:41 -0700681int pinctrl_register_mappings(struct pinctrl_map const *maps,
682 unsigned num_maps)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100683{
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100684 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700685 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100686
687 pr_debug("add %d pinmux maps\n", num_maps);
688
689 /* First sanity check the new mapping */
690 for (i = 0; i < num_maps; i++) {
691 if (!maps[i].name) {
692 pr_err("failed to register map %d: no map name given\n",
693 i);
694 return -EINVAL;
695 }
696
697 if (!maps[i].ctrl_dev_name) {
698 pr_err("failed to register map %s (%d): no pin control device given\n",
699 maps[i].name, i);
700 return -EINVAL;
701 }
702
703 if (!maps[i].function) {
704 pr_err("failed to register map %s (%d): no function ID given\n",
705 maps[i].name, i);
706 return -EINVAL;
707 }
708
709 if (!maps[i].dev_name)
710 pr_debug("add system map %s function %s with no device\n",
711 maps[i].name,
712 maps[i].function);
713 else
714 pr_debug("register map %s, function %s\n",
715 maps[i].name,
716 maps[i].function);
717 }
718
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700719 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
720 if (!maps_node) {
721 pr_err("failed to alloc struct pinctrl_maps\n");
722 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100723 }
724
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700725 maps_node->num_maps = num_maps;
726 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps, GFP_KERNEL);
727 if (!maps_node->maps) {
728 kfree(maps_node);
729 return -ENOMEM;
730 }
731
732 mutex_lock(&pinctrl_maps_mutex);
733 list_add_tail(&maps_node->node, &pinctrl_maps);
734 mutex_unlock(&pinctrl_maps_mutex);
735
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100736 return 0;
737}
738
739/* Hog a single map entry and add to the hoglist */
740static int pinctrl_hog_map(struct pinctrl_dev *pctldev,
741 struct pinctrl_map const *map)
742{
743 struct pinctrl_hog *hog;
744 struct pinctrl *p;
745 int ret;
746
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100747 hog = kzalloc(sizeof(struct pinctrl_hog), GFP_KERNEL);
748 if (!hog)
749 return -ENOMEM;
750
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700751 p = pinctrl_get_locked(pctldev->dev, map->name);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100752 if (IS_ERR(p)) {
753 kfree(hog);
754 dev_err(pctldev->dev,
755 "could not get the %s pin control mapping for hogging\n",
756 map->name);
757 return PTR_ERR(p);
758 }
759
760 ret = pinctrl_enable(p);
761 if (ret) {
762 pinctrl_put(p);
763 kfree(hog);
764 dev_err(pctldev->dev,
765 "could not enable the %s pin control mapping for hogging\n",
766 map->name);
767 return ret;
768 }
769
770 hog->map = map;
771 hog->p = p;
772
773 dev_info(pctldev->dev, "hogged map %s, function %s\n", map->name,
774 map->function);
775 mutex_lock(&pctldev->pinctrl_hogs_lock);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700776 list_add_tail(&hog->node, &pctldev->pinctrl_hogs);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100777 mutex_unlock(&pctldev->pinctrl_hogs_lock);
778
779 return 0;
780}
781
782/**
783 * pinctrl_hog_maps() - hog specific map entries on controller device
784 * @pctldev: the pin control device to hog entries on
785 *
786 * When the pin controllers are registered, there may be some specific pinmux
787 * map entries that need to be hogged, i.e. get+enabled until the system shuts
788 * down.
789 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700790static int pinctrl_hog_maps(struct pinctrl_dev *pctldev)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100791{
792 struct device *dev = pctldev->dev;
793 const char *devname = dev_name(dev);
794 int ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700795 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100796 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700797 struct pinctrl_map const *map;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100798
799 INIT_LIST_HEAD(&pctldev->pinctrl_hogs);
800 mutex_init(&pctldev->pinctrl_hogs_lock);
801
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700802 mutex_lock(&pinctrl_maps_mutex);
803 for_each_maps(maps_node, i, map) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100804 if (map->ctrl_dev_name &&
Linus Walleij77a59882012-02-10 01:34:12 +0100805 !strcmp(map->ctrl_dev_name, devname) &&
806 !strcmp(map->dev_name, devname)) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100807 /* OK time to hog! */
808 ret = pinctrl_hog_map(pctldev, map);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700809 if (ret) {
810 mutex_unlock(&pinctrl_maps_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100811 return ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700812 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100813 }
814 }
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700815 mutex_unlock(&pinctrl_maps_mutex);
816
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100817 return 0;
818}
819
820/**
821 * pinctrl_unhog_maps() - unhog specific map entries on controller device
822 * @pctldev: the pin control device to unhog entries on
823 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700824static void pinctrl_unhog_maps(struct pinctrl_dev *pctldev)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100825{
826 struct list_head *node, *tmp;
827
828 mutex_lock(&pctldev->pinctrl_hogs_lock);
829 list_for_each_safe(node, tmp, &pctldev->pinctrl_hogs) {
830 struct pinctrl_hog *hog =
831 list_entry(node, struct pinctrl_hog, node);
832 pinctrl_disable(hog->p);
833 pinctrl_put(hog->p);
834 list_del(node);
835 kfree(hog);
836 }
837 mutex_unlock(&pctldev->pinctrl_hogs_lock);
838}
839
Linus Walleij2744e8a2011-05-02 20:50:54 +0200840#ifdef CONFIG_DEBUG_FS
841
842static int pinctrl_pins_show(struct seq_file *s, void *what)
843{
844 struct pinctrl_dev *pctldev = s->private;
845 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +0900846 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200847
848 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200849
Chanho Park706e8522012-01-03 16:47:50 +0900850 /* The pin number can be retrived from the pin controller descriptor */
851 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200852 struct pin_desc *desc;
853
Chanho Park706e8522012-01-03 16:47:50 +0900854 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200855 desc = pin_desc_get(pctldev, pin);
856 /* Pin space may be sparse */
857 if (desc == NULL)
858 continue;
859
860 seq_printf(s, "pin %d (%s) ", pin,
861 desc->name ? desc->name : "unnamed");
862
863 /* Driver-specific info per pin */
864 if (ops->pin_dbg_show)
865 ops->pin_dbg_show(pctldev, s, pin);
866
867 seq_puts(s, "\n");
868 }
869
870 return 0;
871}
872
873static int pinctrl_groups_show(struct seq_file *s, void *what)
874{
875 struct pinctrl_dev *pctldev = s->private;
876 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
877 unsigned selector = 0;
878
879 /* No grouping */
880 if (!ops)
881 return 0;
882
883 seq_puts(s, "registered pin groups:\n");
884 while (ops->list_groups(pctldev, selector) >= 0) {
Stephen Warrena5818a82011-10-19 16:19:25 -0600885 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200886 unsigned num_pins;
887 const char *gname = ops->get_group_name(pctldev, selector);
888 int ret;
889 int i;
890
891 ret = ops->get_group_pins(pctldev, selector,
892 &pins, &num_pins);
893 if (ret)
894 seq_printf(s, "%s [ERROR GETTING PINS]\n",
895 gname);
896 else {
897 seq_printf(s, "group: %s, pins = [ ", gname);
898 for (i = 0; i < num_pins; i++)
899 seq_printf(s, "%d ", pins[i]);
900 seq_puts(s, "]\n");
901 }
902 selector++;
903 }
904
905
906 return 0;
907}
908
909static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
910{
911 struct pinctrl_dev *pctldev = s->private;
912 struct pinctrl_gpio_range *range = NULL;
913
914 seq_puts(s, "GPIO ranges handled:\n");
915
916 /* Loop over the ranges */
917 mutex_lock(&pctldev->gpio_ranges_lock);
918 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Linus Walleij75d66422011-11-16 09:58:51 +0100919 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
920 range->id, range->name,
921 range->base, (range->base + range->npins - 1),
922 range->pin_base,
923 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +0200924 }
925 mutex_unlock(&pctldev->gpio_ranges_lock);
926
927 return 0;
928}
929
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100930static int pinctrl_maps_show(struct seq_file *s, void *what)
931{
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700932 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100933 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700934 struct pinctrl_map const *map;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100935
936 seq_puts(s, "Pinctrl maps:\n");
937
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700938 mutex_lock(&pinctrl_maps_mutex);
939 for_each_maps(maps_node, i, map) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100940 seq_printf(s, "%s:\n", map->name);
941 if (map->dev_name)
942 seq_printf(s, " device: %s\n",
943 map->dev_name);
944 else
945 seq_printf(s, " SYSTEM MUX\n");
946 seq_printf(s, " controlling device %s\n",
947 map->ctrl_dev_name);
948 seq_printf(s, " function: %s\n", map->function);
949 seq_printf(s, " group: %s\n", map->group ? map->group :
950 "(default)");
951 }
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700952 mutex_unlock(&pinctrl_maps_mutex);
953
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100954 return 0;
955}
956
957static int pinmux_hogs_show(struct seq_file *s, void *what)
958{
959 struct pinctrl_dev *pctldev = s->private;
960 struct pinctrl_hog *hog;
961
962 seq_puts(s, "Pin control map hogs held by device\n");
963
964 list_for_each_entry(hog, &pctldev->pinctrl_hogs, node)
965 seq_printf(s, "%s\n", hog->map->name);
966
967 return 0;
968}
969
Linus Walleij2744e8a2011-05-02 20:50:54 +0200970static int pinctrl_devices_show(struct seq_file *s, void *what)
971{
972 struct pinctrl_dev *pctldev;
973
Linus Walleijae6b4d82011-10-19 18:14:33 +0200974 seq_puts(s, "name [pinmux] [pinconf]\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200975 mutex_lock(&pinctrldev_list_mutex);
976 list_for_each_entry(pctldev, &pinctrldev_list, node) {
977 seq_printf(s, "%s ", pctldev->desc->name);
978 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +0200979 seq_puts(s, "yes ");
980 else
981 seq_puts(s, "no ");
982 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200983 seq_puts(s, "yes");
984 else
985 seq_puts(s, "no");
986 seq_puts(s, "\n");
987 }
988 mutex_unlock(&pinctrldev_list_mutex);
989
990 return 0;
991}
992
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100993static int pinctrl_show(struct seq_file *s, void *what)
994{
995 struct pinctrl *p;
996
997 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
998 list_for_each_entry(p, &pinctrl_list, node) {
999 struct pinctrl_dev *pctldev = p->pctldev;
1000
1001 if (!pctldev) {
1002 seq_puts(s, "NO PIN CONTROLLER DEVICE\n");
1003 continue;
1004 }
1005
1006 seq_printf(s, "device: %s",
1007 pinctrl_dev_get_name(p->pctldev));
1008
1009 pinmux_dbg_show(s, p);
1010
1011 seq_printf(s, " users: %u map-> %s\n",
1012 p->usecount,
1013 p->dev ? dev_name(p->dev) : "(system)");
1014 }
1015
1016 return 0;
1017}
1018
Linus Walleij2744e8a2011-05-02 20:50:54 +02001019static int pinctrl_pins_open(struct inode *inode, struct file *file)
1020{
1021 return single_open(file, pinctrl_pins_show, inode->i_private);
1022}
1023
1024static int pinctrl_groups_open(struct inode *inode, struct file *file)
1025{
1026 return single_open(file, pinctrl_groups_show, inode->i_private);
1027}
1028
1029static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1030{
1031 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1032}
1033
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001034static int pinctrl_maps_open(struct inode *inode, struct file *file)
1035{
1036 return single_open(file, pinctrl_maps_show, inode->i_private);
1037}
1038
1039static int pinmux_hogs_open(struct inode *inode, struct file *file)
1040{
1041 return single_open(file, pinmux_hogs_show, inode->i_private);
1042}
1043
Linus Walleij2744e8a2011-05-02 20:50:54 +02001044static int pinctrl_devices_open(struct inode *inode, struct file *file)
1045{
1046 return single_open(file, pinctrl_devices_show, NULL);
1047}
1048
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001049static int pinctrl_open(struct inode *inode, struct file *file)
1050{
1051 return single_open(file, pinctrl_show, NULL);
1052}
1053
Linus Walleij2744e8a2011-05-02 20:50:54 +02001054static const struct file_operations pinctrl_pins_ops = {
1055 .open = pinctrl_pins_open,
1056 .read = seq_read,
1057 .llseek = seq_lseek,
1058 .release = single_release,
1059};
1060
1061static const struct file_operations pinctrl_groups_ops = {
1062 .open = pinctrl_groups_open,
1063 .read = seq_read,
1064 .llseek = seq_lseek,
1065 .release = single_release,
1066};
1067
1068static const struct file_operations pinctrl_gpioranges_ops = {
1069 .open = pinctrl_gpioranges_open,
1070 .read = seq_read,
1071 .llseek = seq_lseek,
1072 .release = single_release,
1073};
1074
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001075static const struct file_operations pinctrl_maps_ops = {
1076 .open = pinctrl_maps_open,
1077 .read = seq_read,
1078 .llseek = seq_lseek,
1079 .release = single_release,
1080};
1081
1082static const struct file_operations pinmux_hogs_ops = {
1083 .open = pinmux_hogs_open,
1084 .read = seq_read,
1085 .llseek = seq_lseek,
1086 .release = single_release,
1087};
1088
Linus Walleij2744e8a2011-05-02 20:50:54 +02001089static const struct file_operations pinctrl_devices_ops = {
1090 .open = pinctrl_devices_open,
1091 .read = seq_read,
1092 .llseek = seq_lseek,
1093 .release = single_release,
1094};
1095
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001096static const struct file_operations pinctrl_ops = {
1097 .open = pinctrl_open,
1098 .read = seq_read,
1099 .llseek = seq_lseek,
1100 .release = single_release,
1101};
1102
Linus Walleij2744e8a2011-05-02 20:50:54 +02001103static struct dentry *debugfs_root;
1104
1105static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1106{
Tony Lindgren02157162012-01-20 08:17:22 -08001107 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001108
Stephen Warren51cd24e2011-12-09 16:59:05 -07001109 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001110 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001111 pctldev->device_root = device_root;
1112
Linus Walleij2744e8a2011-05-02 20:50:54 +02001113 if (IS_ERR(device_root) || !device_root) {
1114 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001115 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001116 return;
1117 }
1118 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1119 device_root, pctldev, &pinctrl_pins_ops);
1120 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1121 device_root, pctldev, &pinctrl_groups_ops);
1122 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1123 device_root, pctldev, &pinctrl_gpioranges_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001124 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1125 device_root, pctldev, &pinctrl_maps_ops);
1126 debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO,
1127 device_root, pctldev, &pinmux_hogs_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001128 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +02001129 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001130}
1131
Tony Lindgren02157162012-01-20 08:17:22 -08001132static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1133{
1134 debugfs_remove_recursive(pctldev->device_root);
1135}
1136
Linus Walleij2744e8a2011-05-02 20:50:54 +02001137static void pinctrl_init_debugfs(void)
1138{
1139 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1140 if (IS_ERR(debugfs_root) || !debugfs_root) {
1141 pr_warn("failed to create debugfs directory\n");
1142 debugfs_root = NULL;
1143 return;
1144 }
1145
1146 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1147 debugfs_root, NULL, &pinctrl_devices_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001148 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1149 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001150}
1151
1152#else /* CONFIG_DEBUG_FS */
1153
1154static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1155{
1156}
1157
1158static void pinctrl_init_debugfs(void)
1159{
1160}
1161
Tony Lindgren02157162012-01-20 08:17:22 -08001162static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1163{
1164}
1165
Linus Walleij2744e8a2011-05-02 20:50:54 +02001166#endif
1167
1168/**
1169 * pinctrl_register() - register a pin controller device
1170 * @pctldesc: descriptor for this pin controller
1171 * @dev: parent device for this pin controller
1172 * @driver_data: private pin controller data for this pin controller
1173 */
1174struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1175 struct device *dev, void *driver_data)
1176{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001177 struct pinctrl_dev *pctldev;
1178 int ret;
1179
1180 if (pctldesc == NULL)
1181 return NULL;
1182 if (pctldesc->name == NULL)
1183 return NULL;
1184
Linus Walleij2744e8a2011-05-02 20:50:54 +02001185 pctldev = kzalloc(sizeof(struct pinctrl_dev), GFP_KERNEL);
1186 if (pctldev == NULL)
1187 return NULL;
1188
1189 /* Initialize pin control device struct */
1190 pctldev->owner = pctldesc->owner;
1191 pctldev->desc = pctldesc;
1192 pctldev->driver_data = driver_data;
1193 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
1194 spin_lock_init(&pctldev->pin_desc_tree_lock);
1195 INIT_LIST_HEAD(&pctldev->gpio_ranges);
1196 mutex_init(&pctldev->gpio_ranges_lock);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001197 pctldev->dev = dev;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001198
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001199 /* If we're implementing pinmuxing, check the ops for sanity */
1200 if (pctldesc->pmxops) {
1201 ret = pinmux_check_ops(pctldev);
1202 if (ret) {
1203 pr_err("%s pinmux ops lacks necessary functions\n",
1204 pctldesc->name);
1205 goto out_err;
1206 }
1207 }
1208
1209 /* If we're implementing pinconfig, check the ops for sanity */
1210 if (pctldesc->confops) {
1211 ret = pinconf_check_ops(pctldev);
1212 if (ret) {
1213 pr_err("%s pin config ops lacks necessary functions\n",
1214 pctldesc->name);
1215 goto out_err;
1216 }
1217 }
1218
Linus Walleij2744e8a2011-05-02 20:50:54 +02001219 /* Register all the pins */
1220 pr_debug("try to register %d pins on %s...\n",
1221 pctldesc->npins, pctldesc->name);
1222 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1223 if (ret) {
1224 pr_err("error during pin registration\n");
1225 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1226 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001227 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001228 }
1229
1230 pinctrl_init_device_debugfs(pctldev);
1231 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -07001232 list_add_tail(&pctldev->node, &pinctrldev_list);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001233 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleije93bcee2012-02-09 07:23:28 +01001234 pinctrl_hog_maps(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001235 return pctldev;
1236
Stephen Warren51cd24e2011-12-09 16:59:05 -07001237out_err:
1238 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001239 return NULL;
1240}
1241EXPORT_SYMBOL_GPL(pinctrl_register);
1242
1243/**
1244 * pinctrl_unregister() - unregister pinmux
1245 * @pctldev: pin controller to unregister
1246 *
1247 * Called by pinmux drivers to unregister a pinmux.
1248 */
1249void pinctrl_unregister(struct pinctrl_dev *pctldev)
1250{
1251 if (pctldev == NULL)
1252 return;
1253
Tony Lindgren02157162012-01-20 08:17:22 -08001254 pinctrl_remove_device_debugfs(pctldev);
Linus Walleije93bcee2012-02-09 07:23:28 +01001255 pinctrl_unhog_maps(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001256 /* TODO: check that no pinmuxes are still active? */
1257 mutex_lock(&pinctrldev_list_mutex);
1258 list_del(&pctldev->node);
1259 mutex_unlock(&pinctrldev_list_mutex);
1260 /* Destroy descriptor tree */
1261 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1262 pctldev->desc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001263 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001264}
1265EXPORT_SYMBOL_GPL(pinctrl_unregister);
1266
1267static int __init pinctrl_init(void)
1268{
1269 pr_info("initialized pinctrl subsystem\n");
1270 pinctrl_init_debugfs();
1271 return 0;
1272}
1273
1274/* init early since many drivers really need to initialized pinmux early */
1275core_initcall(pinctrl_init);