]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/pinctrl/core.c
pinctrl: fix and simplify locking
[mirror_ubuntu-bionic-kernel.git] / drivers / pinctrl / core.c
CommitLineData
2744e8af
LW
1/*
2 * Core driver for the pin control subsystem
3 *
befe5bdf 4 * Copyright (C) 2011-2012 ST-Ericsson SA
2744e8af
LW
5 * 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 *
b2b3e66e
SW
10 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
11 *
2744e8af
LW
12 * License terms: GNU General Public License (GPL) version 2
13 */
14#define pr_fmt(fmt) "pinctrl core: " fmt
15
16#include <linux/kernel.h>
a5a697cd 17#include <linux/export.h>
2744e8af
LW
18#include <linux/init.h>
19#include <linux/device.h>
20#include <linux/slab.h>
2744e8af
LW
21#include <linux/err.h>
22#include <linux/list.h>
2744e8af
LW
23#include <linux/sysfs.h>
24#include <linux/debugfs.h>
25#include <linux/seq_file.h>
26#include <linux/pinctrl/pinctrl.h>
27#include <linux/pinctrl/machine.h>
28#include "core.h"
29#include "pinmux.h"
ae6b4d85 30#include "pinconf.h"
2744e8af 31
b2b3e66e
SW
32/**
33 * struct pinctrl_maps - a list item containing part of the mapping table
34 * @node: mapping table list node
35 * @maps: array of mapping table entries
36 * @num_maps: the number of entries in @maps
37 */
38struct pinctrl_maps {
39 struct list_head node;
40 struct pinctrl_map const *maps;
41 unsigned num_maps;
42};
43
57b676f9
SW
44/* Mutex taken by all entry points */
45DEFINE_MUTEX(pinctrl_mutex);
46
47/* Global list of pin control devices (struct pinctrl_dev) */
2744e8af
LW
48static LIST_HEAD(pinctrldev_list);
49
57b676f9 50/* List of pin controller handles (struct pinctrl) */
befe5bdf
LW
51static LIST_HEAD(pinctrl_list);
52
57b676f9 53/* List of pinctrl maps (struct pinctrl_maps) */
b2b3e66e
SW
54static LIST_HEAD(pinctrl_maps);
55
56#define for_each_maps(_maps_node_, _i_, _map_) \
57 list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
58 for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
59 _i_ < _maps_node_->num_maps; \
60 i++, _map_ = &_maps_node_->maps[_i_])
befe5bdf 61
2744e8af
LW
62const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
63{
64 /* We're not allowed to register devices without name */
65 return pctldev->desc->name;
66}
67EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
68
69void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
70{
71 return pctldev->driver_data;
72}
73EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
74
75/**
9dfac4fd
LW
76 * get_pinctrl_dev_from_devname() - look up pin controller device
77 * @devname: the name of a device instance, as returned by dev_name()
2744e8af
LW
78 *
79 * Looks up a pin control device matching a certain device name or pure device
80 * pointer, the pure device pointer will take precedence.
81 */
9dfac4fd 82struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
2744e8af
LW
83{
84 struct pinctrl_dev *pctldev = NULL;
85 bool found = false;
86
9dfac4fd
LW
87 if (!devname)
88 return NULL;
89
2744e8af 90 list_for_each_entry(pctldev, &pinctrldev_list, node) {
9dfac4fd 91 if (!strcmp(dev_name(pctldev->dev), devname)) {
2744e8af
LW
92 /* Matched on device name */
93 found = true;
94 break;
95 }
96 }
2744e8af
LW
97
98 return found ? pctldev : NULL;
99}
100
ae6b4d85
LW
101/**
102 * pin_get_from_name() - look up a pin number from a name
103 * @pctldev: the pin control device to lookup the pin on
104 * @name: the name of the pin to look up
105 */
106int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
107{
706e8520 108 unsigned i, pin;
ae6b4d85 109
706e8520
CP
110 /* The pin number can be retrived from the pin controller descriptor */
111 for (i = 0; i < pctldev->desc->npins; i++) {
ae6b4d85
LW
112 struct pin_desc *desc;
113
706e8520 114 pin = pctldev->desc->pins[i].number;
ae6b4d85
LW
115 desc = pin_desc_get(pctldev, pin);
116 /* Pin space may be sparse */
117 if (desc == NULL)
118 continue;
119 if (desc->name && !strcmp(name, desc->name))
120 return pin;
121 }
122
123 return -EINVAL;
124}
125
2744e8af
LW
126/**
127 * pin_is_valid() - check if pin exists on controller
128 * @pctldev: the pin control device to check the pin on
129 * @pin: pin to check, use the local pin controller index number
130 *
131 * This tells us whether a certain pin exist on a certain pin controller or
132 * not. Pin lists may be sparse, so some pins may not exist.
133 */
134bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
135{
136 struct pin_desc *pindesc;
137
138 if (pin < 0)
139 return false;
140
57b676f9 141 mutex_lock(&pinctrl_mutex);
2744e8af 142 pindesc = pin_desc_get(pctldev, pin);
57b676f9 143 mutex_unlock(&pinctrl_mutex);
2744e8af 144
57b676f9 145 return pindesc != NULL;
2744e8af
LW
146}
147EXPORT_SYMBOL_GPL(pin_is_valid);
148
149/* Deletes a range of pin descriptors */
150static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
151 const struct pinctrl_pin_desc *pins,
152 unsigned num_pins)
153{
154 int i;
155
2744e8af
LW
156 for (i = 0; i < num_pins; i++) {
157 struct pin_desc *pindesc;
158
159 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
160 pins[i].number);
161 if (pindesc != NULL) {
162 radix_tree_delete(&pctldev->pin_desc_tree,
163 pins[i].number);
ca53c5f1
LW
164 if (pindesc->dynamic_name)
165 kfree(pindesc->name);
2744e8af
LW
166 }
167 kfree(pindesc);
168 }
2744e8af
LW
169}
170
171static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
172 unsigned number, const char *name)
173{
174 struct pin_desc *pindesc;
175
176 pindesc = pin_desc_get(pctldev, number);
177 if (pindesc != NULL) {
178 pr_err("pin %d already registered on %s\n", number,
179 pctldev->desc->name);
180 return -EINVAL;
181 }
182
183 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
95dcd4ae
SW
184 if (pindesc == NULL) {
185 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
2744e8af 186 return -ENOMEM;
95dcd4ae 187 }
ae6b4d85 188
2744e8af
LW
189 /* Set owner */
190 pindesc->pctldev = pctldev;
191
9af1e44f 192 /* Copy basic pin info */
8dc6ae4d 193 if (name) {
ca53c5f1
LW
194 pindesc->name = name;
195 } else {
196 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
197 if (pindesc->name == NULL)
198 return -ENOMEM;
199 pindesc->dynamic_name = true;
200 }
2744e8af 201
2744e8af 202 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
2744e8af 203 pr_debug("registered pin %d (%s) on %s\n",
ca53c5f1 204 number, pindesc->name, pctldev->desc->name);
2744e8af
LW
205 return 0;
206}
207
208static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
209 struct pinctrl_pin_desc const *pins,
210 unsigned num_descs)
211{
212 unsigned i;
213 int ret = 0;
214
215 for (i = 0; i < num_descs; i++) {
216 ret = pinctrl_register_one_pin(pctldev,
217 pins[i].number, pins[i].name);
218 if (ret)
219 return ret;
220 }
221
222 return 0;
223}
224
225/**
226 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
227 * @pctldev: pin controller device to check
228 * @gpio: gpio pin to check taken from the global GPIO pin space
229 *
230 * Tries to match a GPIO pin number to the ranges handled by a certain pin
231 * controller, return the range or NULL
232 */
233static struct pinctrl_gpio_range *
234pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
235{
236 struct pinctrl_gpio_range *range = NULL;
237
238 /* Loop over the ranges */
2744e8af
LW
239 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
240 /* Check if we're in the valid range */
241 if (gpio >= range->base &&
242 gpio < range->base + range->npins) {
2744e8af
LW
243 return range;
244 }
245 }
2744e8af
LW
246
247 return NULL;
248}
249
250/**
251 * pinctrl_get_device_gpio_range() - find device for GPIO range
252 * @gpio: the pin to locate the pin controller for
253 * @outdev: the pin control device if found
254 * @outrange: the GPIO range if found
255 *
256 * Find the pin controller handling a certain GPIO pin from the pinspace of
257 * the GPIO subsystem, return the device and the matching GPIO range. Returns
258 * negative if the GPIO range could not be found in any device.
259 */
4ecce45d
SW
260static int pinctrl_get_device_gpio_range(unsigned gpio,
261 struct pinctrl_dev **outdev,
262 struct pinctrl_gpio_range **outrange)
2744e8af
LW
263{
264 struct pinctrl_dev *pctldev = NULL;
265
266 /* Loop over the pin controllers */
2744e8af
LW
267 list_for_each_entry(pctldev, &pinctrldev_list, node) {
268 struct pinctrl_gpio_range *range;
269
270 range = pinctrl_match_gpio_range(pctldev, gpio);
271 if (range != NULL) {
272 *outdev = pctldev;
273 *outrange = range;
2744e8af
LW
274 return 0;
275 }
276 }
2744e8af
LW
277
278 return -EINVAL;
279}
280
281/**
282 * pinctrl_add_gpio_range() - register a GPIO range for a controller
283 * @pctldev: pin controller device to add the range to
284 * @range: the GPIO range to add
285 *
286 * This adds a range of GPIOs to be handled by a certain pin controller. Call
287 * this to register handled ranges after registering your pin controller.
288 */
289void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
290 struct pinctrl_gpio_range *range)
291{
57b676f9 292 mutex_lock(&pinctrl_mutex);
8b9c139f 293 list_add_tail(&range->node, &pctldev->gpio_ranges);
57b676f9 294 mutex_unlock(&pinctrl_mutex);
2744e8af 295}
4ecce45d 296EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
2744e8af
LW
297
298/**
299 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
300 * @pctldev: pin controller device to remove the range from
301 * @range: the GPIO range to remove
302 */
303void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
304 struct pinctrl_gpio_range *range)
305{
57b676f9 306 mutex_lock(&pinctrl_mutex);
2744e8af 307 list_del(&range->node);
57b676f9 308 mutex_unlock(&pinctrl_mutex);
2744e8af 309}
4ecce45d 310EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
2744e8af 311
7afde8ba
LW
312/**
313 * pinctrl_get_group_selector() - returns the group selector for a group
314 * @pctldev: the pin controller handling the group
315 * @pin_group: the pin group to look up
316 */
317int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
318 const char *pin_group)
319{
320 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
321 unsigned group_selector = 0;
322
323 while (pctlops->list_groups(pctldev, group_selector) >= 0) {
324 const char *gname = pctlops->get_group_name(pctldev,
325 group_selector);
326 if (!strcmp(gname, pin_group)) {
51cd24ee 327 dev_dbg(pctldev->dev,
7afde8ba
LW
328 "found group selector %u for %s\n",
329 group_selector,
330 pin_group);
331 return group_selector;
332 }
333
334 group_selector++;
335 }
336
51cd24ee 337 dev_err(pctldev->dev, "does not have pin group %s\n",
7afde8ba
LW
338 pin_group);
339
340 return -EINVAL;
341}
342
befe5bdf
LW
343/**
344 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
345 * @gpio: the GPIO pin number from the GPIO subsystem number space
346 *
347 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
348 * as part of their gpio_request() semantics, platforms and individual drivers
349 * shall *NOT* request GPIO pins to be muxed in.
350 */
351int pinctrl_request_gpio(unsigned gpio)
352{
353 struct pinctrl_dev *pctldev;
354 struct pinctrl_gpio_range *range;
355 int ret;
356 int pin;
357
57b676f9
SW
358 mutex_lock(&pinctrl_mutex);
359
befe5bdf 360 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
57b676f9
SW
361 if (ret) {
362 mutex_unlock(&pinctrl_mutex);
befe5bdf 363 return -EINVAL;
57b676f9 364 }
befe5bdf
LW
365
366 /* Convert to the pin controllers number space */
367 pin = gpio - range->base + range->pin_base;
368
57b676f9
SW
369 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
370
371 mutex_unlock(&pinctrl_mutex);
372 return ret;
befe5bdf
LW
373}
374EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
375
376/**
377 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
378 * @gpio: the GPIO pin number from the GPIO subsystem number space
379 *
380 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
381 * as part of their gpio_free() semantics, platforms and individual drivers
382 * shall *NOT* request GPIO pins to be muxed out.
383 */
384void pinctrl_free_gpio(unsigned gpio)
385{
386 struct pinctrl_dev *pctldev;
387 struct pinctrl_gpio_range *range;
388 int ret;
389 int pin;
390
57b676f9
SW
391 mutex_lock(&pinctrl_mutex);
392
befe5bdf 393 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
57b676f9
SW
394 if (ret) {
395 mutex_unlock(&pinctrl_mutex);
befe5bdf 396 return;
57b676f9 397 }
befe5bdf
LW
398
399 /* Convert to the pin controllers number space */
400 pin = gpio - range->base + range->pin_base;
401
57b676f9
SW
402 pinmux_free_gpio(pctldev, pin, range);
403
404 mutex_unlock(&pinctrl_mutex);
befe5bdf
LW
405}
406EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
407
408static int pinctrl_gpio_direction(unsigned gpio, bool input)
409{
410 struct pinctrl_dev *pctldev;
411 struct pinctrl_gpio_range *range;
412 int ret;
413 int pin;
414
415 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
416 if (ret)
417 return ret;
418
419 /* Convert to the pin controllers number space */
420 pin = gpio - range->base + range->pin_base;
421
422 return pinmux_gpio_direction(pctldev, range, pin, input);
423}
424
425/**
426 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
427 * @gpio: the GPIO pin number from the GPIO subsystem number space
428 *
429 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
430 * as part of their gpio_direction_input() semantics, platforms and individual
431 * drivers shall *NOT* touch pin control GPIO calls.
432 */
433int pinctrl_gpio_direction_input(unsigned gpio)
434{
57b676f9
SW
435 int ret;
436 mutex_lock(&pinctrl_mutex);
437 ret = pinctrl_gpio_direction(gpio, true);
438 mutex_unlock(&pinctrl_mutex);
439 return ret;
befe5bdf
LW
440}
441EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
442
443/**
444 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
445 * @gpio: the GPIO pin number from the GPIO subsystem number space
446 *
447 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
448 * as part of their gpio_direction_output() semantics, platforms and individual
449 * drivers shall *NOT* touch pin control GPIO calls.
450 */
451int pinctrl_gpio_direction_output(unsigned gpio)
452{
57b676f9
SW
453 int ret;
454 mutex_lock(&pinctrl_mutex);
455 ret = pinctrl_gpio_direction(gpio, false);
456 mutex_unlock(&pinctrl_mutex);
457 return ret;
befe5bdf
LW
458}
459EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
460
b2b3e66e 461static struct pinctrl *pinctrl_get_locked(struct device *dev, const char *name)
befe5bdf 462{
befe5bdf 463 struct pinctrl_dev *pctldev = NULL;
1681f5ae 464 const char *devname;
befe5bdf 465 struct pinctrl *p;
befe5bdf
LW
466 unsigned num_maps = 0;
467 int ret = -ENODEV;
b2b3e66e 468 struct pinctrl_maps *maps_node;
befe5bdf 469 int i;
b2b3e66e 470 struct pinctrl_map const *map;
befe5bdf 471
110e4ec5
SW
472 /* We must have both a dev and state name */
473 if (WARN_ON(!dev || !name))
befe5bdf
LW
474 return ERR_PTR(-EINVAL);
475
1681f5ae 476 devname = dev_name(dev);
befe5bdf 477
95dcd4ae 478 dev_dbg(dev, "pinctrl_get() for device %s state %s\n", devname, name);
befe5bdf
LW
479
480 /*
481 * create the state cookie holder struct pinctrl for each
482 * mapping, this is what consumers will get when requesting
483 * a pin control handle with pinctrl_get()
484 */
02f5b989 485 p = kzalloc(sizeof(*p), GFP_KERNEL);
95dcd4ae
SW
486 if (p == NULL) {
487 dev_err(dev, "failed to alloc struct pinctrl\n");
befe5bdf 488 return ERR_PTR(-ENOMEM);
95dcd4ae 489 }
befe5bdf
LW
490 pinmux_init_pinctrl_handle(p);
491
492 /* Iterate over the pin control maps to locate the right ones */
b2b3e66e 493 for_each_maps(maps_node, i, map) {
befe5bdf
LW
494 /*
495 * First, try to find the pctldev given in the map
496 */
497 pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
498 if (!pctldev) {
b1eed4ec
SW
499 dev_err(dev, "unknown pinctrl device %s in map entry",
500 map->ctrl_dev_name);
501 pinmux_put(p);
502 kfree(p);
503 /* Eventually, this should trigger deferred probe */
504 return ERR_PTR(-ENODEV);
befe5bdf
LW
505 }
506
95dcd4ae
SW
507 dev_dbg(dev, "in map, found pctldev %s to handle function %s",
508 dev_name(pctldev->dev), map->function);
befe5bdf 509
1681f5ae
SW
510 /* Map must be for this device */
511 if (strcmp(map->dev_name, devname))
512 continue;
513
110e4ec5
SW
514 /* State name must be the one we're looking for */
515 if (strcmp(map->name, name))
516 continue;
befe5bdf 517
1681f5ae
SW
518 ret = pinmux_apply_muxmap(pctldev, p, dev, devname, map);
519 if (ret) {
520 kfree(p);
521 return ERR_PTR(ret);
befe5bdf 522 }
1681f5ae 523 num_maps++;
befe5bdf
LW
524 }
525
f026fe3d
SW
526 /*
527 * This may be perfectly legitimate. An IP block may get re-used
528 * across SoCs. Not all of those SoCs may need pinmux settings for the
529 * IP block, e.g. if one SoC dedicates pins to that function but
530 * another doesn't. The driver won't know this, and will always
531 * attempt to set up the pinmux. The mapping table defines whether any
532 * HW programming is actually needed.
533 */
534 if (!num_maps)
535 dev_info(dev, "zero maps found for mapping %s\n", name);
befe5bdf 536
95dcd4ae
SW
537 dev_dbg(dev, "found %u maps for device %s state %s\n",
538 num_maps, devname, name ? name : "(undefined)");
befe5bdf
LW
539
540 /* Add the pinmux to the global list */
8b9c139f 541 list_add_tail(&p->node, &pinctrl_list);
befe5bdf
LW
542
543 return p;
544}
b2b3e66e
SW
545
546/**
547 * pinctrl_get() - retrieves the pin controller handle for a certain device
548 * @dev: the device to get the pin controller handle for
549 * @name: an optional specific control mapping name or NULL, the name is only
550 * needed if you want to have more than one mapping per device, or if you
551 * need an anonymous pin control (not tied to any specific device)
552 */
553struct pinctrl *pinctrl_get(struct device *dev, const char *name)
554{
555 struct pinctrl *p;
556
57b676f9 557 mutex_lock(&pinctrl_mutex);
b2b3e66e 558 p = pinctrl_get_locked(dev, name);
57b676f9 559 mutex_unlock(&pinctrl_mutex);
b2b3e66e
SW
560
561 return p;
562}
befe5bdf
LW
563EXPORT_SYMBOL_GPL(pinctrl_get);
564
57b676f9 565static void pinctrl_put_locked(struct pinctrl *p)
befe5bdf
LW
566{
567 if (p == NULL)
568 return;
569
befe5bdf
LW
570 if (p->usecount)
571 pr_warn("releasing pin control handle with active users!\n");
572 /* Free the groups and all acquired pins */
573 pinmux_put(p);
befe5bdf
LW
574
575 /* Remove from list */
befe5bdf 576 list_del(&p->node);
befe5bdf
LW
577
578 kfree(p);
579}
befe5bdf
LW
580
581/**
57b676f9
SW
582 * pinctrl_put() - release a previously claimed pin control handle
583 * @p: a pin control handle previously claimed by pinctrl_get()
befe5bdf 584 */
57b676f9
SW
585void pinctrl_put(struct pinctrl *p)
586{
587 mutex_lock(&pinctrl_mutex);
588 pinctrl_put(p);
589 mutex_unlock(&pinctrl_mutex);
590}
591EXPORT_SYMBOL_GPL(pinctrl_put);
592
593static int pinctrl_enable_locked(struct pinctrl *p)
befe5bdf
LW
594{
595 int ret = 0;
596
597 if (p == NULL)
598 return -EINVAL;
57b676f9 599
befe5bdf
LW
600 if (p->usecount++ == 0) {
601 ret = pinmux_enable(p);
602 if (ret)
603 p->usecount--;
604 }
57b676f9 605
befe5bdf
LW
606 return ret;
607}
befe5bdf
LW
608
609/**
57b676f9
SW
610 * pinctrl_enable() - enable a certain pin controller setting
611 * @p: the pin control handle to enable, previously claimed by pinctrl_get()
befe5bdf 612 */
57b676f9
SW
613int pinctrl_enable(struct pinctrl *p)
614{
615 int ret;
616 mutex_lock(&pinctrl_mutex);
617 ret = pinctrl_enable_locked(p);
618 mutex_unlock(&pinctrl_mutex);
619 return ret;
620}
621EXPORT_SYMBOL_GPL(pinctrl_enable);
622
623static void pinctrl_disable_locked(struct pinctrl *p)
befe5bdf
LW
624{
625 if (p == NULL)
626 return;
627
befe5bdf
LW
628 if (--p->usecount == 0) {
629 pinmux_disable(p);
630 }
57b676f9
SW
631}
632
633/**
634 * pinctrl_disable() - disable a certain pin control setting
635 * @p: the pin control handle to disable, previously claimed by pinctrl_get()
636 */
637void pinctrl_disable(struct pinctrl *p)
638{
639 mutex_lock(&pinctrl_mutex);
640 pinctrl_disable_locked(p);
641 mutex_unlock(&pinctrl_mutex);
befe5bdf
LW
642}
643EXPORT_SYMBOL_GPL(pinctrl_disable);
644
645/**
646 * pinctrl_register_mappings() - register a set of pin controller mappings
13398a4b
SW
647 * @maps: the pincontrol mappings table to register. This should probably be
648 * marked with __initdata so it can be discarded after boot. This
649 * function will perform a shallow copy for the mapping entries.
befe5bdf 650 * @num_maps: the number of maps in the mapping table
befe5bdf 651 */
13398a4b
SW
652int pinctrl_register_mappings(struct pinctrl_map const *maps,
653 unsigned num_maps)
befe5bdf 654{
befe5bdf 655 int i;
b2b3e66e 656 struct pinctrl_maps *maps_node;
befe5bdf
LW
657
658 pr_debug("add %d pinmux maps\n", num_maps);
659
660 /* First sanity check the new mapping */
661 for (i = 0; i < num_maps; i++) {
662 if (!maps[i].name) {
663 pr_err("failed to register map %d: no map name given\n",
95dcd4ae 664 i);
befe5bdf
LW
665 return -EINVAL;
666 }
667
668 if (!maps[i].ctrl_dev_name) {
669 pr_err("failed to register map %s (%d): no pin control device given\n",
670 maps[i].name, i);
671 return -EINVAL;
672 }
673
674 if (!maps[i].function) {
675 pr_err("failed to register map %s (%d): no function ID given\n",
95dcd4ae 676 maps[i].name, i);
befe5bdf
LW
677 return -EINVAL;
678 }
679
1681f5ae
SW
680 if (!maps[i].dev_name) {
681 pr_err("failed to register map %s (%d): no device given\n",
95dcd4ae 682 maps[i].name, i);
1681f5ae
SW
683 return -EINVAL;
684 }
befe5bdf
LW
685 }
686
b2b3e66e
SW
687 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
688 if (!maps_node) {
689 pr_err("failed to alloc struct pinctrl_maps\n");
690 return -ENOMEM;
691 }
befe5bdf 692
b2b3e66e
SW
693 maps_node->num_maps = num_maps;
694 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps, GFP_KERNEL);
695 if (!maps_node->maps) {
95dcd4ae 696 pr_err("failed to duplicate mapping table\n");
b2b3e66e
SW
697 kfree(maps_node);
698 return -ENOMEM;
befe5bdf
LW
699 }
700
57b676f9 701 mutex_lock(&pinctrl_mutex);
b2b3e66e 702 list_add_tail(&maps_node->node, &pinctrl_maps);
57b676f9 703 mutex_unlock(&pinctrl_mutex);
b2b3e66e 704
befe5bdf
LW
705 return 0;
706}
707
2744e8af
LW
708#ifdef CONFIG_DEBUG_FS
709
710static int pinctrl_pins_show(struct seq_file *s, void *what)
711{
712 struct pinctrl_dev *pctldev = s->private;
713 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
706e8520 714 unsigned i, pin;
2744e8af
LW
715
716 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
2744e8af 717
57b676f9
SW
718 mutex_lock(&pinctrl_mutex);
719
706e8520
CP
720 /* The pin number can be retrived from the pin controller descriptor */
721 for (i = 0; i < pctldev->desc->npins; i++) {
2744e8af
LW
722 struct pin_desc *desc;
723
706e8520 724 pin = pctldev->desc->pins[i].number;
2744e8af
LW
725 desc = pin_desc_get(pctldev, pin);
726 /* Pin space may be sparse */
727 if (desc == NULL)
728 continue;
729
730 seq_printf(s, "pin %d (%s) ", pin,
731 desc->name ? desc->name : "unnamed");
732
733 /* Driver-specific info per pin */
734 if (ops->pin_dbg_show)
735 ops->pin_dbg_show(pctldev, s, pin);
736
737 seq_puts(s, "\n");
738 }
739
57b676f9
SW
740 mutex_unlock(&pinctrl_mutex);
741
2744e8af
LW
742 return 0;
743}
744
745static int pinctrl_groups_show(struct seq_file *s, void *what)
746{
747 struct pinctrl_dev *pctldev = s->private;
748 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
749 unsigned selector = 0;
750
751 /* No grouping */
752 if (!ops)
753 return 0;
754
57b676f9
SW
755 mutex_lock(&pinctrl_mutex);
756
2744e8af
LW
757 seq_puts(s, "registered pin groups:\n");
758 while (ops->list_groups(pctldev, selector) >= 0) {
a5818a8b 759 const unsigned *pins;
2744e8af
LW
760 unsigned num_pins;
761 const char *gname = ops->get_group_name(pctldev, selector);
762 int ret;
763 int i;
764
765 ret = ops->get_group_pins(pctldev, selector,
766 &pins, &num_pins);
767 if (ret)
768 seq_printf(s, "%s [ERROR GETTING PINS]\n",
769 gname);
770 else {
771 seq_printf(s, "group: %s, pins = [ ", gname);
772 for (i = 0; i < num_pins; i++)
773 seq_printf(s, "%d ", pins[i]);
774 seq_puts(s, "]\n");
775 }
776 selector++;
777 }
778
57b676f9 779 mutex_unlock(&pinctrl_mutex);
2744e8af
LW
780
781 return 0;
782}
783
784static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
785{
786 struct pinctrl_dev *pctldev = s->private;
787 struct pinctrl_gpio_range *range = NULL;
788
789 seq_puts(s, "GPIO ranges handled:\n");
790
57b676f9
SW
791 mutex_lock(&pinctrl_mutex);
792
2744e8af 793 /* Loop over the ranges */
2744e8af 794 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
75d6642a
LW
795 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
796 range->id, range->name,
797 range->base, (range->base + range->npins - 1),
798 range->pin_base,
799 (range->pin_base + range->npins - 1));
2744e8af 800 }
57b676f9
SW
801
802 mutex_unlock(&pinctrl_mutex);
2744e8af
LW
803
804 return 0;
805}
806
807static int pinctrl_devices_show(struct seq_file *s, void *what)
808{
809 struct pinctrl_dev *pctldev;
810
ae6b4d85 811 seq_puts(s, "name [pinmux] [pinconf]\n");
57b676f9
SW
812
813 mutex_lock(&pinctrl_mutex);
814
2744e8af
LW
815 list_for_each_entry(pctldev, &pinctrldev_list, node) {
816 seq_printf(s, "%s ", pctldev->desc->name);
817 if (pctldev->desc->pmxops)
ae6b4d85
LW
818 seq_puts(s, "yes ");
819 else
820 seq_puts(s, "no ");
821 if (pctldev->desc->confops)
2744e8af
LW
822 seq_puts(s, "yes");
823 else
824 seq_puts(s, "no");
825 seq_puts(s, "\n");
826 }
57b676f9
SW
827
828 mutex_unlock(&pinctrl_mutex);
2744e8af
LW
829
830 return 0;
831}
832
3eedb437
SW
833static int pinctrl_maps_show(struct seq_file *s, void *what)
834{
835 struct pinctrl_maps *maps_node;
836 int i;
837 struct pinctrl_map const *map;
838
839 seq_puts(s, "Pinctrl maps:\n");
840
57b676f9
SW
841 mutex_lock(&pinctrl_mutex);
842
3eedb437
SW
843 for_each_maps(maps_node, i, map) {
844 seq_printf(s, "%s:\n", map->name);
845 seq_printf(s, " device: %s\n", map->dev_name);
846 seq_printf(s, " controlling device %s\n", map->ctrl_dev_name);
847 seq_printf(s, " function: %s\n", map->function);
848 seq_printf(s, " group: %s\n", map->group ? map->group :
849 "(default)");
850 }
57b676f9
SW
851
852 mutex_unlock(&pinctrl_mutex);
3eedb437
SW
853
854 return 0;
855}
856
befe5bdf
LW
857static int pinctrl_show(struct seq_file *s, void *what)
858{
859 struct pinctrl *p;
860
861 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
57b676f9
SW
862
863 mutex_lock(&pinctrl_mutex);
864
befe5bdf
LW
865 list_for_each_entry(p, &pinctrl_list, node) {
866 struct pinctrl_dev *pctldev = p->pctldev;
867
868 if (!pctldev) {
869 seq_puts(s, "NO PIN CONTROLLER DEVICE\n");
870 continue;
871 }
872
873 seq_printf(s, "device: %s",
874 pinctrl_dev_get_name(p->pctldev));
875
876 pinmux_dbg_show(s, p);
877
878 seq_printf(s, " users: %u map-> %s\n",
879 p->usecount,
880 p->dev ? dev_name(p->dev) : "(system)");
881 }
882
57b676f9
SW
883 mutex_unlock(&pinctrl_mutex);
884
befe5bdf
LW
885 return 0;
886}
887
2744e8af
LW
888static int pinctrl_pins_open(struct inode *inode, struct file *file)
889{
890 return single_open(file, pinctrl_pins_show, inode->i_private);
891}
892
893static int pinctrl_groups_open(struct inode *inode, struct file *file)
894{
895 return single_open(file, pinctrl_groups_show, inode->i_private);
896}
897
898static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
899{
900 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
901}
902
903static int pinctrl_devices_open(struct inode *inode, struct file *file)
904{
905 return single_open(file, pinctrl_devices_show, NULL);
906}
907
3eedb437
SW
908static int pinctrl_maps_open(struct inode *inode, struct file *file)
909{
910 return single_open(file, pinctrl_maps_show, NULL);
911}
912
befe5bdf
LW
913static int pinctrl_open(struct inode *inode, struct file *file)
914{
915 return single_open(file, pinctrl_show, NULL);
916}
917
2744e8af
LW
918static const struct file_operations pinctrl_pins_ops = {
919 .open = pinctrl_pins_open,
920 .read = seq_read,
921 .llseek = seq_lseek,
922 .release = single_release,
923};
924
925static const struct file_operations pinctrl_groups_ops = {
926 .open = pinctrl_groups_open,
927 .read = seq_read,
928 .llseek = seq_lseek,
929 .release = single_release,
930};
931
932static const struct file_operations pinctrl_gpioranges_ops = {
933 .open = pinctrl_gpioranges_open,
934 .read = seq_read,
935 .llseek = seq_lseek,
936 .release = single_release,
937};
938
3eedb437
SW
939static const struct file_operations pinctrl_devices_ops = {
940 .open = pinctrl_devices_open,
befe5bdf
LW
941 .read = seq_read,
942 .llseek = seq_lseek,
943 .release = single_release,
944};
945
3eedb437
SW
946static const struct file_operations pinctrl_maps_ops = {
947 .open = pinctrl_maps_open,
2744e8af
LW
948 .read = seq_read,
949 .llseek = seq_lseek,
950 .release = single_release,
951};
952
befe5bdf
LW
953static const struct file_operations pinctrl_ops = {
954 .open = pinctrl_open,
955 .read = seq_read,
956 .llseek = seq_lseek,
957 .release = single_release,
958};
959
2744e8af
LW
960static struct dentry *debugfs_root;
961
962static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
963{
02157160 964 struct dentry *device_root;
2744e8af 965
51cd24ee 966 device_root = debugfs_create_dir(dev_name(pctldev->dev),
2744e8af 967 debugfs_root);
02157160
TL
968 pctldev->device_root = device_root;
969
2744e8af
LW
970 if (IS_ERR(device_root) || !device_root) {
971 pr_warn("failed to create debugfs directory for %s\n",
51cd24ee 972 dev_name(pctldev->dev));
2744e8af
LW
973 return;
974 }
975 debugfs_create_file("pins", S_IFREG | S_IRUGO,
976 device_root, pctldev, &pinctrl_pins_ops);
977 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
978 device_root, pctldev, &pinctrl_groups_ops);
979 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
980 device_root, pctldev, &pinctrl_gpioranges_ops);
981 pinmux_init_device_debugfs(device_root, pctldev);
ae6b4d85 982 pinconf_init_device_debugfs(device_root, pctldev);
2744e8af
LW
983}
984
02157160
TL
985static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
986{
987 debugfs_remove_recursive(pctldev->device_root);
988}
989
2744e8af
LW
990static void pinctrl_init_debugfs(void)
991{
992 debugfs_root = debugfs_create_dir("pinctrl", NULL);
993 if (IS_ERR(debugfs_root) || !debugfs_root) {
994 pr_warn("failed to create debugfs directory\n");
995 debugfs_root = NULL;
996 return;
997 }
998
999 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1000 debugfs_root, NULL, &pinctrl_devices_ops);
3eedb437
SW
1001 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1002 debugfs_root, NULL, &pinctrl_maps_ops);
befe5bdf
LW
1003 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1004 debugfs_root, NULL, &pinctrl_ops);
2744e8af
LW
1005}
1006
1007#else /* CONFIG_DEBUG_FS */
1008
1009static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1010{
1011}
1012
1013static void pinctrl_init_debugfs(void)
1014{
1015}
1016
02157160
TL
1017static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1018{
1019}
1020
2744e8af
LW
1021#endif
1022
1023/**
1024 * pinctrl_register() - register a pin controller device
1025 * @pctldesc: descriptor for this pin controller
1026 * @dev: parent device for this pin controller
1027 * @driver_data: private pin controller data for this pin controller
1028 */
1029struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1030 struct device *dev, void *driver_data)
1031{
2744e8af
LW
1032 struct pinctrl_dev *pctldev;
1033 int ret;
1034
1035 if (pctldesc == NULL)
1036 return NULL;
1037 if (pctldesc->name == NULL)
1038 return NULL;
1039
02f5b989 1040 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
95dcd4ae
SW
1041 if (pctldev == NULL) {
1042 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
b9130b77 1043 return NULL;
95dcd4ae 1044 }
b9130b77
TL
1045
1046 /* Initialize pin control device struct */
1047 pctldev->owner = pctldesc->owner;
1048 pctldev->desc = pctldesc;
1049 pctldev->driver_data = driver_data;
1050 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
b9130b77 1051 INIT_LIST_HEAD(&pctldev->gpio_ranges);
b9130b77
TL
1052 pctldev->dev = dev;
1053
2744e8af
LW
1054 /* If we're implementing pinmuxing, check the ops for sanity */
1055 if (pctldesc->pmxops) {
b9130b77 1056 ret = pinmux_check_ops(pctldev);
2744e8af
LW
1057 if (ret) {
1058 pr_err("%s pinmux ops lacks necessary functions\n",
1059 pctldesc->name);
b9130b77 1060 goto out_err;
2744e8af
LW
1061 }
1062 }
1063
ae6b4d85
LW
1064 /* If we're implementing pinconfig, check the ops for sanity */
1065 if (pctldesc->confops) {
b9130b77 1066 ret = pinconf_check_ops(pctldev);
ae6b4d85
LW
1067 if (ret) {
1068 pr_err("%s pin config ops lacks necessary functions\n",
1069 pctldesc->name);
b9130b77 1070 goto out_err;
ae6b4d85
LW
1071 }
1072 }
1073
2744e8af
LW
1074 /* Register all the pins */
1075 pr_debug("try to register %d pins on %s...\n",
1076 pctldesc->npins, pctldesc->name);
1077 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1078 if (ret) {
1079 pr_err("error during pin registration\n");
1080 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1081 pctldesc->npins);
51cd24ee 1082 goto out_err;
2744e8af
LW
1083 }
1084
57b676f9
SW
1085 mutex_lock(&pinctrl_mutex);
1086
8b9c139f 1087 list_add_tail(&pctldev->node, &pinctrldev_list);
57b676f9
SW
1088
1089 pctldev->p = pinctrl_get_locked(pctldev->dev, PINCTRL_STATE_DEFAULT);
46919ae6 1090 if (!IS_ERR(pctldev->p))
57b676f9
SW
1091 pinctrl_enable_locked(pctldev->p);
1092
1093 mutex_unlock(&pinctrl_mutex);
1094
2304b473
SW
1095 pinctrl_init_device_debugfs(pctldev);
1096
2744e8af
LW
1097 return pctldev;
1098
51cd24ee
SW
1099out_err:
1100 kfree(pctldev);
2744e8af
LW
1101 return NULL;
1102}
1103EXPORT_SYMBOL_GPL(pinctrl_register);
1104
1105/**
1106 * pinctrl_unregister() - unregister pinmux
1107 * @pctldev: pin controller to unregister
1108 *
1109 * Called by pinmux drivers to unregister a pinmux.
1110 */
1111void pinctrl_unregister(struct pinctrl_dev *pctldev)
1112{
1113 if (pctldev == NULL)
1114 return;
1115
02157160 1116 pinctrl_remove_device_debugfs(pctldev);
57b676f9
SW
1117
1118 mutex_lock(&pinctrl_mutex);
1119
46919ae6 1120 if (!IS_ERR(pctldev->p)) {
57b676f9
SW
1121 pinctrl_disable_locked(pctldev->p);
1122 pinctrl_put_locked(pctldev->p);
46919ae6 1123 }
57b676f9 1124
2744e8af 1125 /* TODO: check that no pinmuxes are still active? */
2744e8af 1126 list_del(&pctldev->node);
2744e8af
LW
1127 /* Destroy descriptor tree */
1128 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1129 pctldev->desc->npins);
51cd24ee 1130 kfree(pctldev);
57b676f9
SW
1131
1132 mutex_unlock(&pinctrl_mutex);
2744e8af
LW
1133}
1134EXPORT_SYMBOL_GPL(pinctrl_unregister);
1135
1136static int __init pinctrl_init(void)
1137{
1138 pr_info("initialized pinctrl subsystem\n");
1139 pinctrl_init_debugfs();
1140 return 0;
1141}
1142
1143/* init early since many drivers really need to initialized pinmux early */
1144core_initcall(pinctrl_init);