blob: 1d5943069a03b67d6f4abbe740336fba5aa2cb23 [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);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700215 if (pindesc == NULL) {
216 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200217 return -ENOMEM;
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700218 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200219
Linus Walleij2744e8a2011-05-02 20:50:54 +0200220 spin_lock_init(&pindesc->lock);
221
222 /* Set owner */
223 pindesc->pctldev = pctldev;
224
Stephen Warren9af1e442011-10-19 16:19:27 -0600225 /* Copy basic pin info */
Linus Walleij8dc6ae42012-02-01 18:11:40 +0100226 if (name) {
Linus Walleijca53c5f2011-12-14 20:33:37 +0100227 pindesc->name = name;
228 } else {
229 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
230 if (pindesc->name == NULL)
231 return -ENOMEM;
232 pindesc->dynamic_name = true;
233 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200234
235 spin_lock(&pctldev->pin_desc_tree_lock);
236 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
237 spin_unlock(&pctldev->pin_desc_tree_lock);
238 pr_debug("registered pin %d (%s) on %s\n",
Linus Walleijca53c5f2011-12-14 20:33:37 +0100239 number, pindesc->name, pctldev->desc->name);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200240 return 0;
241}
242
243static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
244 struct pinctrl_pin_desc const *pins,
245 unsigned num_descs)
246{
247 unsigned i;
248 int ret = 0;
249
250 for (i = 0; i < num_descs; i++) {
251 ret = pinctrl_register_one_pin(pctldev,
252 pins[i].number, pins[i].name);
253 if (ret)
254 return ret;
255 }
256
257 return 0;
258}
259
260/**
261 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
262 * @pctldev: pin controller device to check
263 * @gpio: gpio pin to check taken from the global GPIO pin space
264 *
265 * Tries to match a GPIO pin number to the ranges handled by a certain pin
266 * controller, return the range or NULL
267 */
268static struct pinctrl_gpio_range *
269pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
270{
271 struct pinctrl_gpio_range *range = NULL;
272
273 /* Loop over the ranges */
274 mutex_lock(&pctldev->gpio_ranges_lock);
275 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
276 /* Check if we're in the valid range */
277 if (gpio >= range->base &&
278 gpio < range->base + range->npins) {
279 mutex_unlock(&pctldev->gpio_ranges_lock);
280 return range;
281 }
282 }
283 mutex_unlock(&pctldev->gpio_ranges_lock);
284
285 return NULL;
286}
287
288/**
289 * pinctrl_get_device_gpio_range() - find device for GPIO range
290 * @gpio: the pin to locate the pin controller for
291 * @outdev: the pin control device if found
292 * @outrange: the GPIO range if found
293 *
294 * Find the pin controller handling a certain GPIO pin from the pinspace of
295 * the GPIO subsystem, return the device and the matching GPIO range. Returns
296 * negative if the GPIO range could not be found in any device.
297 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700298static int pinctrl_get_device_gpio_range(unsigned gpio,
299 struct pinctrl_dev **outdev,
300 struct pinctrl_gpio_range **outrange)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200301{
302 struct pinctrl_dev *pctldev = NULL;
303
304 /* Loop over the pin controllers */
305 mutex_lock(&pinctrldev_list_mutex);
306 list_for_each_entry(pctldev, &pinctrldev_list, node) {
307 struct pinctrl_gpio_range *range;
308
309 range = pinctrl_match_gpio_range(pctldev, gpio);
310 if (range != NULL) {
311 *outdev = pctldev;
312 *outrange = range;
313 mutex_unlock(&pinctrldev_list_mutex);
314 return 0;
315 }
316 }
317 mutex_unlock(&pinctrldev_list_mutex);
318
319 return -EINVAL;
320}
321
322/**
323 * pinctrl_add_gpio_range() - register a GPIO range for a controller
324 * @pctldev: pin controller device to add the range to
325 * @range: the GPIO range to add
326 *
327 * This adds a range of GPIOs to be handled by a certain pin controller. Call
328 * this to register handled ranges after registering your pin controller.
329 */
330void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
331 struct pinctrl_gpio_range *range)
332{
333 mutex_lock(&pctldev->gpio_ranges_lock);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700334 list_add_tail(&range->node, &pctldev->gpio_ranges);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200335 mutex_unlock(&pctldev->gpio_ranges_lock);
336}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700337EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200338
339/**
340 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
341 * @pctldev: pin controller device to remove the range from
342 * @range: the GPIO range to remove
343 */
344void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
345 struct pinctrl_gpio_range *range)
346{
347 mutex_lock(&pctldev->gpio_ranges_lock);
348 list_del(&range->node);
349 mutex_unlock(&pctldev->gpio_ranges_lock);
350}
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700351EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200352
Linus Walleij7afde8b2011-10-19 17:07:16 +0200353/**
354 * pinctrl_get_group_selector() - returns the group selector for a group
355 * @pctldev: the pin controller handling the group
356 * @pin_group: the pin group to look up
357 */
358int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
359 const char *pin_group)
360{
361 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
362 unsigned group_selector = 0;
363
364 while (pctlops->list_groups(pctldev, group_selector) >= 0) {
365 const char *gname = pctlops->get_group_name(pctldev,
366 group_selector);
367 if (!strcmp(gname, pin_group)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700368 dev_dbg(pctldev->dev,
Linus Walleij7afde8b2011-10-19 17:07:16 +0200369 "found group selector %u for %s\n",
370 group_selector,
371 pin_group);
372 return group_selector;
373 }
374
375 group_selector++;
376 }
377
Stephen Warren51cd24e2011-12-09 16:59:05 -0700378 dev_err(pctldev->dev, "does not have pin group %s\n",
Linus Walleij7afde8b2011-10-19 17:07:16 +0200379 pin_group);
380
381 return -EINVAL;
382}
383
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100384/**
385 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
386 * @gpio: the GPIO pin number from the GPIO subsystem number space
387 *
388 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
389 * as part of their gpio_request() semantics, platforms and individual drivers
390 * shall *NOT* request GPIO pins to be muxed in.
391 */
392int pinctrl_request_gpio(unsigned gpio)
393{
394 struct pinctrl_dev *pctldev;
395 struct pinctrl_gpio_range *range;
396 int ret;
397 int pin;
398
399 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
400 if (ret)
401 return -EINVAL;
402
403 /* Convert to the pin controllers number space */
404 pin = gpio - range->base + range->pin_base;
405
406 return pinmux_request_gpio(pctldev, range, pin, gpio);
407}
408EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
409
410/**
411 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
412 * @gpio: the GPIO pin number from the GPIO subsystem number space
413 *
414 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
415 * as part of their gpio_free() semantics, platforms and individual drivers
416 * shall *NOT* request GPIO pins to be muxed out.
417 */
418void pinctrl_free_gpio(unsigned gpio)
419{
420 struct pinctrl_dev *pctldev;
421 struct pinctrl_gpio_range *range;
422 int ret;
423 int pin;
424
425 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
426 if (ret)
427 return;
428
429 /* Convert to the pin controllers number space */
430 pin = gpio - range->base + range->pin_base;
431
432 return pinmux_free_gpio(pctldev, pin, range);
433}
434EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
435
436static int pinctrl_gpio_direction(unsigned gpio, bool input)
437{
438 struct pinctrl_dev *pctldev;
439 struct pinctrl_gpio_range *range;
440 int ret;
441 int pin;
442
443 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
444 if (ret)
445 return ret;
446
447 /* Convert to the pin controllers number space */
448 pin = gpio - range->base + range->pin_base;
449
450 return pinmux_gpio_direction(pctldev, range, pin, input);
451}
452
453/**
454 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
455 * @gpio: the GPIO pin number from the GPIO subsystem number space
456 *
457 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
458 * as part of their gpio_direction_input() semantics, platforms and individual
459 * drivers shall *NOT* touch pin control GPIO calls.
460 */
461int pinctrl_gpio_direction_input(unsigned gpio)
462{
463 return pinctrl_gpio_direction(gpio, true);
464}
465EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
466
467/**
468 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
469 * @gpio: the GPIO pin number from the GPIO subsystem number space
470 *
471 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
472 * as part of their gpio_direction_output() semantics, platforms and individual
473 * drivers shall *NOT* touch pin control GPIO calls.
474 */
475int pinctrl_gpio_direction_output(unsigned gpio)
476{
477 return pinctrl_gpio_direction(gpio, false);
478}
479EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
480
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700481static struct pinctrl *pinctrl_get_locked(struct device *dev, const char *name)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100482{
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100483 struct pinctrl_dev *pctldev = NULL;
Stephen Warren1681f5a2012-02-22 14:25:58 -0700484 const char *devname;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100485 struct pinctrl *p;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100486 unsigned num_maps = 0;
487 int ret = -ENODEV;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700488 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100489 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700490 struct pinctrl_map const *map;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100491
Stephen Warren1681f5a2012-02-22 14:25:58 -0700492 /* We must have a dev name */
493 if (WARN_ON(!dev))
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100494 return ERR_PTR(-EINVAL);
495
Stephen Warren1681f5a2012-02-22 14:25:58 -0700496 devname = dev_name(dev);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100497
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700498 dev_dbg(dev, "pinctrl_get() for device %s state %s\n", devname, name);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100499
500 /*
501 * create the state cookie holder struct pinctrl for each
502 * mapping, this is what consumers will get when requesting
503 * a pin control handle with pinctrl_get()
504 */
505 p = kzalloc(sizeof(struct pinctrl), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700506 if (p == NULL) {
507 dev_err(dev, "failed to alloc struct pinctrl\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100508 return ERR_PTR(-ENOMEM);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700509 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100510 mutex_init(&p->mutex);
511 pinmux_init_pinctrl_handle(p);
512
513 /* Iterate over the pin control maps to locate the right ones */
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700514 for_each_maps(maps_node, i, map) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100515 /*
516 * First, try to find the pctldev given in the map
517 */
518 pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
519 if (!pctldev) {
Stephen Warrenb1eed4e2012-02-19 23:45:53 -0700520 dev_err(dev, "unknown pinctrl device %s in map entry",
521 map->ctrl_dev_name);
522 pinmux_put(p);
523 kfree(p);
524 /* Eventually, this should trigger deferred probe */
525 return ERR_PTR(-ENODEV);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100526 }
527
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700528 dev_dbg(dev, "in map, found pctldev %s to handle function %s",
529 dev_name(pctldev->dev), map->function);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100530
Stephen Warren1681f5a2012-02-22 14:25:58 -0700531 /* Map must be for this device */
532 if (strcmp(map->dev_name, devname))
533 continue;
534
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100535 /*
536 * If we're looking for a specific named map, this must match,
537 * else we loop and look for the next.
538 */
539 if (name != NULL) {
540 if (map->name == NULL)
541 continue;
542 if (strcmp(map->name, name))
543 continue;
544 }
545
Stephen Warren1681f5a2012-02-22 14:25:58 -0700546 ret = pinmux_apply_muxmap(pctldev, p, dev, devname, map);
547 if (ret) {
548 kfree(p);
549 return ERR_PTR(ret);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100550 }
Stephen Warren1681f5a2012-02-22 14:25:58 -0700551 num_maps++;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100552 }
553
Stephen Warrenf026fe32012-02-19 23:45:51 -0700554 /*
555 * This may be perfectly legitimate. An IP block may get re-used
556 * across SoCs. Not all of those SoCs may need pinmux settings for the
557 * IP block, e.g. if one SoC dedicates pins to that function but
558 * another doesn't. The driver won't know this, and will always
559 * attempt to set up the pinmux. The mapping table defines whether any
560 * HW programming is actually needed.
561 */
562 if (!num_maps)
563 dev_info(dev, "zero maps found for mapping %s\n", name);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100564
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700565 dev_dbg(dev, "found %u maps for device %s state %s\n",
566 num_maps, devname, name ? name : "(undefined)");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100567
568 /* Add the pinmux to the global list */
569 mutex_lock(&pinctrl_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700570 list_add_tail(&p->node, &pinctrl_list);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100571 mutex_unlock(&pinctrl_list_mutex);
572
573 return p;
574}
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700575
576/**
577 * pinctrl_get() - retrieves the pin controller handle for a certain device
578 * @dev: the device to get the pin controller handle for
579 * @name: an optional specific control mapping name or NULL, the name is only
580 * needed if you want to have more than one mapping per device, or if you
581 * need an anonymous pin control (not tied to any specific device)
582 */
583struct pinctrl *pinctrl_get(struct device *dev, const char *name)
584{
585 struct pinctrl *p;
586
587 mutex_lock(&pinctrl_maps_mutex);
588 p = pinctrl_get_locked(dev, name);
589 mutex_unlock(&pinctrl_maps_mutex);
590
591 return p;
592}
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100593EXPORT_SYMBOL_GPL(pinctrl_get);
594
595/**
596 * pinctrl_put() - release a previously claimed pin control handle
597 * @p: a pin control handle previously claimed by pinctrl_get()
598 */
599void pinctrl_put(struct pinctrl *p)
600{
601 if (p == NULL)
602 return;
603
604 mutex_lock(&p->mutex);
605 if (p->usecount)
606 pr_warn("releasing pin control handle with active users!\n");
607 /* Free the groups and all acquired pins */
608 pinmux_put(p);
609 mutex_unlock(&p->mutex);
610
611 /* Remove from list */
612 mutex_lock(&pinctrl_list_mutex);
613 list_del(&p->node);
614 mutex_unlock(&pinctrl_list_mutex);
615
616 kfree(p);
617}
618EXPORT_SYMBOL_GPL(pinctrl_put);
619
620/**
621 * pinctrl_enable() - enable a certain pin controller setting
622 * @p: the pin control handle to enable, previously claimed by pinctrl_get()
623 */
624int pinctrl_enable(struct pinctrl *p)
625{
626 int ret = 0;
627
628 if (p == NULL)
629 return -EINVAL;
630 mutex_lock(&p->mutex);
631 if (p->usecount++ == 0) {
632 ret = pinmux_enable(p);
633 if (ret)
634 p->usecount--;
635 }
636 mutex_unlock(&p->mutex);
637 return ret;
638}
639EXPORT_SYMBOL_GPL(pinctrl_enable);
640
641/**
642 * pinctrl_disable() - disable a certain pin control setting
643 * @p: the pin control handle to disable, previously claimed by pinctrl_get()
644 */
645void pinctrl_disable(struct pinctrl *p)
646{
647 if (p == NULL)
648 return;
649
650 mutex_lock(&p->mutex);
651 if (--p->usecount == 0) {
652 pinmux_disable(p);
653 }
654 mutex_unlock(&p->mutex);
655}
656EXPORT_SYMBOL_GPL(pinctrl_disable);
657
658/**
659 * pinctrl_register_mappings() - register a set of pin controller mappings
Stephen Warren13398a42012-02-19 23:45:41 -0700660 * @maps: the pincontrol mappings table to register. This should probably be
661 * marked with __initdata so it can be discarded after boot. This
662 * function will perform a shallow copy for the mapping entries.
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100663 * @num_maps: the number of maps in the mapping table
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100664 */
Stephen Warren13398a42012-02-19 23:45:41 -0700665int pinctrl_register_mappings(struct pinctrl_map const *maps,
666 unsigned num_maps)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100667{
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100668 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700669 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100670
671 pr_debug("add %d pinmux maps\n", num_maps);
672
673 /* First sanity check the new mapping */
674 for (i = 0; i < num_maps; i++) {
675 if (!maps[i].name) {
676 pr_err("failed to register map %d: no map name given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700677 i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100678 return -EINVAL;
679 }
680
681 if (!maps[i].ctrl_dev_name) {
682 pr_err("failed to register map %s (%d): no pin control device given\n",
683 maps[i].name, i);
684 return -EINVAL;
685 }
686
687 if (!maps[i].function) {
688 pr_err("failed to register map %s (%d): no function ID given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700689 maps[i].name, i);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100690 return -EINVAL;
691 }
692
Stephen Warren1681f5a2012-02-22 14:25:58 -0700693 if (!maps[i].dev_name) {
694 pr_err("failed to register map %s (%d): no device given\n",
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700695 maps[i].name, i);
Stephen Warren1681f5a2012-02-22 14:25:58 -0700696 return -EINVAL;
697 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100698 }
699
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700700 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
701 if (!maps_node) {
702 pr_err("failed to alloc struct pinctrl_maps\n");
703 return -ENOMEM;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100704 }
705
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700706 maps_node->num_maps = num_maps;
707 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps, GFP_KERNEL);
708 if (!maps_node->maps) {
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700709 pr_err("failed to duplicate mapping table\n");
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700710 kfree(maps_node);
711 return -ENOMEM;
712 }
713
714 mutex_lock(&pinctrl_maps_mutex);
715 list_add_tail(&maps_node->node, &pinctrl_maps);
716 mutex_unlock(&pinctrl_maps_mutex);
717
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100718 return 0;
719}
720
721/* Hog a single map entry and add to the hoglist */
722static int pinctrl_hog_map(struct pinctrl_dev *pctldev,
723 struct pinctrl_map const *map)
724{
725 struct pinctrl_hog *hog;
726 struct pinctrl *p;
727 int ret;
728
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100729 hog = kzalloc(sizeof(struct pinctrl_hog), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700730 if (!hog) {
731 dev_err(pctldev->dev, "failed to alloc struct pinctrl_hog\n");
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100732 return -ENOMEM;
Stephen Warren95dcd4a2012-02-22 14:25:59 -0700733 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100734
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700735 p = pinctrl_get_locked(pctldev->dev, map->name);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100736 if (IS_ERR(p)) {
737 kfree(hog);
738 dev_err(pctldev->dev,
739 "could not get the %s pin control mapping for hogging\n",
740 map->name);
741 return PTR_ERR(p);
742 }
743
744 ret = pinctrl_enable(p);
745 if (ret) {
746 pinctrl_put(p);
747 kfree(hog);
748 dev_err(pctldev->dev,
749 "could not enable the %s pin control mapping for hogging\n",
750 map->name);
751 return ret;
752 }
753
754 hog->map = map;
755 hog->p = p;
756
757 dev_info(pctldev->dev, "hogged map %s, function %s\n", map->name,
758 map->function);
759 mutex_lock(&pctldev->pinctrl_hogs_lock);
Stephen Warren8b9c1392012-02-19 23:45:42 -0700760 list_add_tail(&hog->node, &pctldev->pinctrl_hogs);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100761 mutex_unlock(&pctldev->pinctrl_hogs_lock);
762
763 return 0;
764}
765
766/**
767 * pinctrl_hog_maps() - hog specific map entries on controller device
768 * @pctldev: the pin control device to hog entries on
769 *
770 * When the pin controllers are registered, there may be some specific pinmux
771 * map entries that need to be hogged, i.e. get+enabled until the system shuts
772 * down.
773 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700774static int pinctrl_hog_maps(struct pinctrl_dev *pctldev)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100775{
776 struct device *dev = pctldev->dev;
777 const char *devname = dev_name(dev);
778 int ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700779 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100780 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700781 struct pinctrl_map const *map;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100782
783 INIT_LIST_HEAD(&pctldev->pinctrl_hogs);
784 mutex_init(&pctldev->pinctrl_hogs_lock);
785
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700786 mutex_lock(&pinctrl_maps_mutex);
787 for_each_maps(maps_node, i, map) {
Stephen Warren9891d982012-02-19 23:45:50 -0700788 if (!strcmp(map->ctrl_dev_name, devname) &&
Linus Walleij77a59882012-02-10 01:34:12 +0100789 !strcmp(map->dev_name, devname)) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100790 /* OK time to hog! */
791 ret = pinctrl_hog_map(pctldev, map);
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700792 if (ret) {
793 mutex_unlock(&pinctrl_maps_mutex);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100794 return ret;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700795 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100796 }
797 }
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700798 mutex_unlock(&pinctrl_maps_mutex);
799
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100800 return 0;
801}
802
803/**
804 * pinctrl_unhog_maps() - unhog specific map entries on controller device
805 * @pctldev: the pin control device to unhog entries on
806 */
Stephen Warren4ecce45d2012-02-19 23:45:47 -0700807static void pinctrl_unhog_maps(struct pinctrl_dev *pctldev)
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100808{
809 struct list_head *node, *tmp;
810
811 mutex_lock(&pctldev->pinctrl_hogs_lock);
812 list_for_each_safe(node, tmp, &pctldev->pinctrl_hogs) {
813 struct pinctrl_hog *hog =
814 list_entry(node, struct pinctrl_hog, node);
815 pinctrl_disable(hog->p);
816 pinctrl_put(hog->p);
817 list_del(node);
818 kfree(hog);
819 }
820 mutex_unlock(&pctldev->pinctrl_hogs_lock);
821}
822
Linus Walleij2744e8a2011-05-02 20:50:54 +0200823#ifdef CONFIG_DEBUG_FS
824
825static int pinctrl_pins_show(struct seq_file *s, void *what)
826{
827 struct pinctrl_dev *pctldev = s->private;
828 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
Chanho Park706e8522012-01-03 16:47:50 +0900829 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200830
831 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200832
Chanho Park706e8522012-01-03 16:47:50 +0900833 /* The pin number can be retrived from the pin controller descriptor */
834 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200835 struct pin_desc *desc;
836
Chanho Park706e8522012-01-03 16:47:50 +0900837 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200838 desc = pin_desc_get(pctldev, pin);
839 /* Pin space may be sparse */
840 if (desc == NULL)
841 continue;
842
843 seq_printf(s, "pin %d (%s) ", pin,
844 desc->name ? desc->name : "unnamed");
845
846 /* Driver-specific info per pin */
847 if (ops->pin_dbg_show)
848 ops->pin_dbg_show(pctldev, s, pin);
849
850 seq_puts(s, "\n");
851 }
852
853 return 0;
854}
855
856static int pinctrl_groups_show(struct seq_file *s, void *what)
857{
858 struct pinctrl_dev *pctldev = s->private;
859 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
860 unsigned selector = 0;
861
862 /* No grouping */
863 if (!ops)
864 return 0;
865
866 seq_puts(s, "registered pin groups:\n");
867 while (ops->list_groups(pctldev, selector) >= 0) {
Stephen Warrena5818a82011-10-19 16:19:25 -0600868 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200869 unsigned num_pins;
870 const char *gname = ops->get_group_name(pctldev, selector);
871 int ret;
872 int i;
873
874 ret = ops->get_group_pins(pctldev, selector,
875 &pins, &num_pins);
876 if (ret)
877 seq_printf(s, "%s [ERROR GETTING PINS]\n",
878 gname);
879 else {
880 seq_printf(s, "group: %s, pins = [ ", gname);
881 for (i = 0; i < num_pins; i++)
882 seq_printf(s, "%d ", pins[i]);
883 seq_puts(s, "]\n");
884 }
885 selector++;
886 }
887
888
889 return 0;
890}
891
892static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
893{
894 struct pinctrl_dev *pctldev = s->private;
895 struct pinctrl_gpio_range *range = NULL;
896
897 seq_puts(s, "GPIO ranges handled:\n");
898
899 /* Loop over the ranges */
900 mutex_lock(&pctldev->gpio_ranges_lock);
901 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
Linus Walleij75d66422011-11-16 09:58:51 +0100902 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
903 range->id, range->name,
904 range->base, (range->base + range->npins - 1),
905 range->pin_base,
906 (range->pin_base + range->npins - 1));
Linus Walleij2744e8a2011-05-02 20:50:54 +0200907 }
908 mutex_unlock(&pctldev->gpio_ranges_lock);
909
910 return 0;
911}
912
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100913static int pinctrl_maps_show(struct seq_file *s, void *what)
914{
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700915 struct pinctrl_maps *maps_node;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100916 int i;
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700917 struct pinctrl_map const *map;
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100918
919 seq_puts(s, "Pinctrl maps:\n");
920
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700921 mutex_lock(&pinctrl_maps_mutex);
922 for_each_maps(maps_node, i, map) {
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100923 seq_printf(s, "%s:\n", map->name);
Stephen Warren1681f5a2012-02-22 14:25:58 -0700924 seq_printf(s, " device: %s\n", map->dev_name);
925 seq_printf(s, " controlling device %s\n", map->ctrl_dev_name);
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100926 seq_printf(s, " function: %s\n", map->function);
927 seq_printf(s, " group: %s\n", map->group ? map->group :
928 "(default)");
929 }
Stephen Warrenb2b3e662012-02-19 23:45:43 -0700930 mutex_unlock(&pinctrl_maps_mutex);
931
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100932 return 0;
933}
934
935static int pinmux_hogs_show(struct seq_file *s, void *what)
936{
937 struct pinctrl_dev *pctldev = s->private;
938 struct pinctrl_hog *hog;
939
940 seq_puts(s, "Pin control map hogs held by device\n");
941
942 list_for_each_entry(hog, &pctldev->pinctrl_hogs, node)
943 seq_printf(s, "%s\n", hog->map->name);
944
945 return 0;
946}
947
Linus Walleij2744e8a2011-05-02 20:50:54 +0200948static int pinctrl_devices_show(struct seq_file *s, void *what)
949{
950 struct pinctrl_dev *pctldev;
951
Linus Walleijae6b4d82011-10-19 18:14:33 +0200952 seq_puts(s, "name [pinmux] [pinconf]\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200953 mutex_lock(&pinctrldev_list_mutex);
954 list_for_each_entry(pctldev, &pinctrldev_list, node) {
955 seq_printf(s, "%s ", pctldev->desc->name);
956 if (pctldev->desc->pmxops)
Linus Walleijae6b4d82011-10-19 18:14:33 +0200957 seq_puts(s, "yes ");
958 else
959 seq_puts(s, "no ");
960 if (pctldev->desc->confops)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200961 seq_puts(s, "yes");
962 else
963 seq_puts(s, "no");
964 seq_puts(s, "\n");
965 }
966 mutex_unlock(&pinctrldev_list_mutex);
967
968 return 0;
969}
970
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100971static int pinctrl_show(struct seq_file *s, void *what)
972{
973 struct pinctrl *p;
974
975 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
976 list_for_each_entry(p, &pinctrl_list, node) {
977 struct pinctrl_dev *pctldev = p->pctldev;
978
979 if (!pctldev) {
980 seq_puts(s, "NO PIN CONTROLLER DEVICE\n");
981 continue;
982 }
983
984 seq_printf(s, "device: %s",
985 pinctrl_dev_get_name(p->pctldev));
986
987 pinmux_dbg_show(s, p);
988
989 seq_printf(s, " users: %u map-> %s\n",
990 p->usecount,
991 p->dev ? dev_name(p->dev) : "(system)");
992 }
993
994 return 0;
995}
996
Linus Walleij2744e8a2011-05-02 20:50:54 +0200997static int pinctrl_pins_open(struct inode *inode, struct file *file)
998{
999 return single_open(file, pinctrl_pins_show, inode->i_private);
1000}
1001
1002static int pinctrl_groups_open(struct inode *inode, struct file *file)
1003{
1004 return single_open(file, pinctrl_groups_show, inode->i_private);
1005}
1006
1007static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1008{
1009 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1010}
1011
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001012static int pinctrl_maps_open(struct inode *inode, struct file *file)
1013{
1014 return single_open(file, pinctrl_maps_show, inode->i_private);
1015}
1016
1017static int pinmux_hogs_open(struct inode *inode, struct file *file)
1018{
1019 return single_open(file, pinmux_hogs_show, inode->i_private);
1020}
1021
Linus Walleij2744e8a2011-05-02 20:50:54 +02001022static int pinctrl_devices_open(struct inode *inode, struct file *file)
1023{
1024 return single_open(file, pinctrl_devices_show, NULL);
1025}
1026
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001027static int pinctrl_open(struct inode *inode, struct file *file)
1028{
1029 return single_open(file, pinctrl_show, NULL);
1030}
1031
Linus Walleij2744e8a2011-05-02 20:50:54 +02001032static const struct file_operations pinctrl_pins_ops = {
1033 .open = pinctrl_pins_open,
1034 .read = seq_read,
1035 .llseek = seq_lseek,
1036 .release = single_release,
1037};
1038
1039static const struct file_operations pinctrl_groups_ops = {
1040 .open = pinctrl_groups_open,
1041 .read = seq_read,
1042 .llseek = seq_lseek,
1043 .release = single_release,
1044};
1045
1046static const struct file_operations pinctrl_gpioranges_ops = {
1047 .open = pinctrl_gpioranges_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_maps_ops = {
1054 .open = pinctrl_maps_open,
1055 .read = seq_read,
1056 .llseek = seq_lseek,
1057 .release = single_release,
1058};
1059
1060static const struct file_operations pinmux_hogs_ops = {
1061 .open = pinmux_hogs_open,
1062 .read = seq_read,
1063 .llseek = seq_lseek,
1064 .release = single_release,
1065};
1066
Linus Walleij2744e8a2011-05-02 20:50:54 +02001067static const struct file_operations pinctrl_devices_ops = {
1068 .open = pinctrl_devices_open,
1069 .read = seq_read,
1070 .llseek = seq_lseek,
1071 .release = single_release,
1072};
1073
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001074static const struct file_operations pinctrl_ops = {
1075 .open = pinctrl_open,
1076 .read = seq_read,
1077 .llseek = seq_lseek,
1078 .release = single_release,
1079};
1080
Linus Walleij2744e8a2011-05-02 20:50:54 +02001081static struct dentry *debugfs_root;
1082
1083static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1084{
Tony Lindgren02157162012-01-20 08:17:22 -08001085 struct dentry *device_root;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001086
Stephen Warren51cd24e2011-12-09 16:59:05 -07001087 device_root = debugfs_create_dir(dev_name(pctldev->dev),
Linus Walleij2744e8a2011-05-02 20:50:54 +02001088 debugfs_root);
Tony Lindgren02157162012-01-20 08:17:22 -08001089 pctldev->device_root = device_root;
1090
Linus Walleij2744e8a2011-05-02 20:50:54 +02001091 if (IS_ERR(device_root) || !device_root) {
1092 pr_warn("failed to create debugfs directory for %s\n",
Stephen Warren51cd24e2011-12-09 16:59:05 -07001093 dev_name(pctldev->dev));
Linus Walleij2744e8a2011-05-02 20:50:54 +02001094 return;
1095 }
1096 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1097 device_root, pctldev, &pinctrl_pins_ops);
1098 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1099 device_root, pctldev, &pinctrl_groups_ops);
1100 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1101 device_root, pctldev, &pinctrl_gpioranges_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001102 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1103 device_root, pctldev, &pinctrl_maps_ops);
1104 debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO,
1105 device_root, pctldev, &pinmux_hogs_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001106 pinmux_init_device_debugfs(device_root, pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +02001107 pinconf_init_device_debugfs(device_root, pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001108}
1109
Tony Lindgren02157162012-01-20 08:17:22 -08001110static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1111{
1112 debugfs_remove_recursive(pctldev->device_root);
1113}
1114
Linus Walleij2744e8a2011-05-02 20:50:54 +02001115static void pinctrl_init_debugfs(void)
1116{
1117 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1118 if (IS_ERR(debugfs_root) || !debugfs_root) {
1119 pr_warn("failed to create debugfs directory\n");
1120 debugfs_root = NULL;
1121 return;
1122 }
1123
1124 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1125 debugfs_root, NULL, &pinctrl_devices_ops);
Linus Walleijbefe5bd2012-02-09 19:47:48 +01001126 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1127 debugfs_root, NULL, &pinctrl_ops);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001128}
1129
1130#else /* CONFIG_DEBUG_FS */
1131
1132static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1133{
1134}
1135
1136static void pinctrl_init_debugfs(void)
1137{
1138}
1139
Tony Lindgren02157162012-01-20 08:17:22 -08001140static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1141{
1142}
1143
Linus Walleij2744e8a2011-05-02 20:50:54 +02001144#endif
1145
1146/**
1147 * pinctrl_register() - register a pin controller device
1148 * @pctldesc: descriptor for this pin controller
1149 * @dev: parent device for this pin controller
1150 * @driver_data: private pin controller data for this pin controller
1151 */
1152struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1153 struct device *dev, void *driver_data)
1154{
Linus Walleij2744e8a2011-05-02 20:50:54 +02001155 struct pinctrl_dev *pctldev;
1156 int ret;
1157
1158 if (pctldesc == NULL)
1159 return NULL;
1160 if (pctldesc->name == NULL)
1161 return NULL;
1162
Linus Walleij2744e8a2011-05-02 20:50:54 +02001163 pctldev = kzalloc(sizeof(struct pinctrl_dev), GFP_KERNEL);
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001164 if (pctldev == NULL) {
1165 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
Linus Walleij2744e8a2011-05-02 20:50:54 +02001166 return NULL;
Stephen Warren95dcd4a2012-02-22 14:25:59 -07001167 }
Linus Walleij2744e8a2011-05-02 20:50:54 +02001168
1169 /* Initialize pin control device struct */
1170 pctldev->owner = pctldesc->owner;
1171 pctldev->desc = pctldesc;
1172 pctldev->driver_data = driver_data;
1173 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
1174 spin_lock_init(&pctldev->pin_desc_tree_lock);
1175 INIT_LIST_HEAD(&pctldev->gpio_ranges);
1176 mutex_init(&pctldev->gpio_ranges_lock);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001177 pctldev->dev = dev;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001178
Tony Lindgrenb9130b72012-01-24 16:28:08 -08001179 /* If we're implementing pinmuxing, check the ops for sanity */
1180 if (pctldesc->pmxops) {
1181 ret = pinmux_check_ops(pctldev);
1182 if (ret) {
1183 pr_err("%s pinmux ops lacks necessary functions\n",
1184 pctldesc->name);
1185 goto out_err;
1186 }
1187 }
1188
1189 /* If we're implementing pinconfig, check the ops for sanity */
1190 if (pctldesc->confops) {
1191 ret = pinconf_check_ops(pctldev);
1192 if (ret) {
1193 pr_err("%s pin config ops lacks necessary functions\n",
1194 pctldesc->name);
1195 goto out_err;
1196 }
1197 }
1198
Linus Walleij2744e8a2011-05-02 20:50:54 +02001199 /* Register all the pins */
1200 pr_debug("try to register %d pins on %s...\n",
1201 pctldesc->npins, pctldesc->name);
1202 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1203 if (ret) {
1204 pr_err("error during pin registration\n");
1205 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1206 pctldesc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001207 goto out_err;
Linus Walleij2744e8a2011-05-02 20:50:54 +02001208 }
1209
1210 pinctrl_init_device_debugfs(pctldev);
1211 mutex_lock(&pinctrldev_list_mutex);
Stephen Warren8b9c1392012-02-19 23:45:42 -07001212 list_add_tail(&pctldev->node, &pinctrldev_list);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001213 mutex_unlock(&pinctrldev_list_mutex);
Linus Walleije93bcee2012-02-09 07:23:28 +01001214 pinctrl_hog_maps(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001215 return pctldev;
1216
Stephen Warren51cd24e2011-12-09 16:59:05 -07001217out_err:
1218 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001219 return NULL;
1220}
1221EXPORT_SYMBOL_GPL(pinctrl_register);
1222
1223/**
1224 * pinctrl_unregister() - unregister pinmux
1225 * @pctldev: pin controller to unregister
1226 *
1227 * Called by pinmux drivers to unregister a pinmux.
1228 */
1229void pinctrl_unregister(struct pinctrl_dev *pctldev)
1230{
1231 if (pctldev == NULL)
1232 return;
1233
Tony Lindgren02157162012-01-20 08:17:22 -08001234 pinctrl_remove_device_debugfs(pctldev);
Linus Walleije93bcee2012-02-09 07:23:28 +01001235 pinctrl_unhog_maps(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001236 /* TODO: check that no pinmuxes are still active? */
1237 mutex_lock(&pinctrldev_list_mutex);
1238 list_del(&pctldev->node);
1239 mutex_unlock(&pinctrldev_list_mutex);
1240 /* Destroy descriptor tree */
1241 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1242 pctldev->desc->npins);
Stephen Warren51cd24e2011-12-09 16:59:05 -07001243 kfree(pctldev);
Linus Walleij2744e8a2011-05-02 20:50:54 +02001244}
1245EXPORT_SYMBOL_GPL(pinctrl_unregister);
1246
1247static int __init pinctrl_init(void)
1248{
1249 pr_info("initialized pinctrl subsystem\n");
1250 pinctrl_init_debugfs();
1251 return 0;
1252}
1253
1254/* init early since many drivers really need to initialized pinmux early */
1255core_initcall(pinctrl_init);