pinctrl: fix typo in header
[GitHub/LineageOS/android_kernel_motorola_exynos9610.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>
ab78029e 17#include <linux/kref.h>
a5a697cd 18#include <linux/export.h>
2744e8af
LW
19#include <linux/init.h>
20#include <linux/device.h>
21#include <linux/slab.h>
2744e8af
LW
22#include <linux/err.h>
23#include <linux/list.h>
2744e8af
LW
24#include <linux/sysfs.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
6d4ca1fb 27#include <linux/pinctrl/consumer.h>
2744e8af
LW
28#include <linux/pinctrl/pinctrl.h>
29#include <linux/pinctrl/machine.h>
51e13c24 30#include <asm-generic/gpio.h>
2744e8af 31#include "core.h"
57291ce2 32#include "devicetree.h"
2744e8af 33#include "pinmux.h"
ae6b4d85 34#include "pinconf.h"
2744e8af 35
b2b3e66e 36
5b3aa5f7
DA
37static bool pinctrl_dummy_state;
38
57b676f9
SW
39/* Mutex taken by all entry points */
40DEFINE_MUTEX(pinctrl_mutex);
41
42/* Global list of pin control devices (struct pinctrl_dev) */
57291ce2 43LIST_HEAD(pinctrldev_list);
2744e8af 44
57b676f9 45/* List of pin controller handles (struct pinctrl) */
befe5bdf
LW
46static LIST_HEAD(pinctrl_list);
47
57b676f9 48/* List of pinctrl maps (struct pinctrl_maps) */
6f9e41f4 49LIST_HEAD(pinctrl_maps);
b2b3e66e 50
befe5bdf 51
5b3aa5f7
DA
52/**
53 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
54 *
55 * Usually this function is called by platforms without pinctrl driver support
56 * but run with some shared drivers using pinctrl APIs.
57 * After calling this function, the pinctrl core will return successfully
58 * with creating a dummy state for the driver to keep going smoothly.
59 */
60void pinctrl_provide_dummies(void)
61{
62 pinctrl_dummy_state = true;
63}
64
2744e8af
LW
65const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
66{
67 /* We're not allowed to register devices without name */
68 return pctldev->desc->name;
69}
70EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
71
d6e99abb
HZ
72const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
73{
74 return dev_name(pctldev->dev);
75}
76EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
77
2744e8af
LW
78void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
79{
80 return pctldev->driver_data;
81}
82EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
83
84/**
9dfac4fd
LW
85 * get_pinctrl_dev_from_devname() - look up pin controller device
86 * @devname: the name of a device instance, as returned by dev_name()
2744e8af
LW
87 *
88 * Looks up a pin control device matching a certain device name or pure device
89 * pointer, the pure device pointer will take precedence.
90 */
9dfac4fd 91struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
2744e8af
LW
92{
93 struct pinctrl_dev *pctldev = NULL;
94 bool found = false;
95
9dfac4fd
LW
96 if (!devname)
97 return NULL;
98
2744e8af 99 list_for_each_entry(pctldev, &pinctrldev_list, node) {
9dfac4fd 100 if (!strcmp(dev_name(pctldev->dev), devname)) {
2744e8af
LW
101 /* Matched on device name */
102 found = true;
103 break;
104 }
105 }
2744e8af
LW
106
107 return found ? pctldev : NULL;
108}
109
ae6b4d85
LW
110/**
111 * pin_get_from_name() - look up a pin number from a name
112 * @pctldev: the pin control device to lookup the pin on
113 * @name: the name of the pin to look up
114 */
115int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
116{
706e8520 117 unsigned i, pin;
ae6b4d85 118
706e8520
CP
119 /* The pin number can be retrived from the pin controller descriptor */
120 for (i = 0; i < pctldev->desc->npins; i++) {
ae6b4d85
LW
121 struct pin_desc *desc;
122
706e8520 123 pin = pctldev->desc->pins[i].number;
ae6b4d85
LW
124 desc = pin_desc_get(pctldev, pin);
125 /* Pin space may be sparse */
126 if (desc == NULL)
127 continue;
128 if (desc->name && !strcmp(name, desc->name))
129 return pin;
130 }
131
132 return -EINVAL;
133}
134
dcb5dbc3
DA
135/**
136 * pin_get_name_from_id() - look up a pin name from a pin id
137 * @pctldev: the pin control device to lookup the pin on
138 * @name: the name of the pin to look up
139 */
140const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
141{
142 const struct pin_desc *desc;
143
144 desc = pin_desc_get(pctldev, pin);
145 if (desc == NULL) {
146 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
147 pin);
148 return NULL;
149 }
150
151 return desc->name;
152}
153
2744e8af
LW
154/**
155 * pin_is_valid() - check if pin exists on controller
156 * @pctldev: the pin control device to check the pin on
157 * @pin: pin to check, use the local pin controller index number
158 *
159 * This tells us whether a certain pin exist on a certain pin controller or
160 * not. Pin lists may be sparse, so some pins may not exist.
161 */
162bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
163{
164 struct pin_desc *pindesc;
165
166 if (pin < 0)
167 return false;
168
57b676f9 169 mutex_lock(&pinctrl_mutex);
2744e8af 170 pindesc = pin_desc_get(pctldev, pin);
57b676f9 171 mutex_unlock(&pinctrl_mutex);
2744e8af 172
57b676f9 173 return pindesc != NULL;
2744e8af
LW
174}
175EXPORT_SYMBOL_GPL(pin_is_valid);
176
177/* Deletes a range of pin descriptors */
178static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
179 const struct pinctrl_pin_desc *pins,
180 unsigned num_pins)
181{
182 int i;
183
2744e8af
LW
184 for (i = 0; i < num_pins; i++) {
185 struct pin_desc *pindesc;
186
187 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
188 pins[i].number);
189 if (pindesc != NULL) {
190 radix_tree_delete(&pctldev->pin_desc_tree,
191 pins[i].number);
ca53c5f1
LW
192 if (pindesc->dynamic_name)
193 kfree(pindesc->name);
2744e8af
LW
194 }
195 kfree(pindesc);
196 }
2744e8af
LW
197}
198
199static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
200 unsigned number, const char *name)
201{
202 struct pin_desc *pindesc;
203
204 pindesc = pin_desc_get(pctldev, number);
205 if (pindesc != NULL) {
206 pr_err("pin %d already registered on %s\n", number,
207 pctldev->desc->name);
208 return -EINVAL;
209 }
210
211 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
95dcd4ae
SW
212 if (pindesc == NULL) {
213 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
2744e8af 214 return -ENOMEM;
95dcd4ae 215 }
ae6b4d85 216
2744e8af
LW
217 /* Set owner */
218 pindesc->pctldev = pctldev;
219
9af1e44f 220 /* Copy basic pin info */
8dc6ae4d 221 if (name) {
ca53c5f1
LW
222 pindesc->name = name;
223 } else {
224 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
eb26cc9c
SK
225 if (pindesc->name == NULL) {
226 kfree(pindesc);
ca53c5f1 227 return -ENOMEM;
eb26cc9c 228 }
ca53c5f1
LW
229 pindesc->dynamic_name = true;
230 }
2744e8af 231
2744e8af 232 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
2744e8af 233 pr_debug("registered pin %d (%s) on %s\n",
ca53c5f1 234 number, pindesc->name, pctldev->desc->name);
2744e8af
LW
235 return 0;
236}
237
238static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
239 struct pinctrl_pin_desc const *pins,
240 unsigned num_descs)
241{
242 unsigned i;
243 int ret = 0;
244
245 for (i = 0; i < num_descs; i++) {
246 ret = pinctrl_register_one_pin(pctldev,
247 pins[i].number, pins[i].name);
248 if (ret)
249 return ret;
250 }
251
252 return 0;
253}
254
255/**
256 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
257 * @pctldev: pin controller device to check
258 * @gpio: gpio pin to check taken from the global GPIO pin space
259 *
260 * Tries to match a GPIO pin number to the ranges handled by a certain pin
261 * controller, return the range or NULL
262 */
263static struct pinctrl_gpio_range *
264pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
265{
266 struct pinctrl_gpio_range *range = NULL;
267
268 /* Loop over the ranges */
2744e8af
LW
269 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
270 /* Check if we're in the valid range */
271 if (gpio >= range->base &&
272 gpio < range->base + range->npins) {
2744e8af
LW
273 return range;
274 }
275 }
2744e8af
LW
276
277 return NULL;
278}
279
51e13c24
HZ
280/**
281 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
282 * the same GPIO chip are in range
283 * @gpio: gpio pin to check taken from the global GPIO pin space
284 *
285 * This function is complement of pinctrl_match_gpio_range(). If the return
286 * value of pinctrl_match_gpio_range() is NULL, this function could be used
287 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
288 * of the same GPIO chip don't have back-end pinctrl interface.
289 * If the return value is true, it means that pinctrl device is ready & the
290 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
291 * is false, it means that pinctrl device may not be ready.
292 */
293static bool pinctrl_ready_for_gpio_range(unsigned gpio)
294{
295 struct pinctrl_dev *pctldev;
296 struct pinctrl_gpio_range *range = NULL;
297 struct gpio_chip *chip = gpio_to_chip(gpio);
298
299 /* Loop over the pin controllers */
300 list_for_each_entry(pctldev, &pinctrldev_list, node) {
301 /* Loop over the ranges */
302 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
303 /* Check if any gpio range overlapped with gpio chip */
304 if (range->base + range->npins - 1 < chip->base ||
305 range->base > chip->base + chip->ngpio - 1)
306 continue;
307 return true;
308 }
309 }
310 return false;
311}
312
2744e8af
LW
313/**
314 * pinctrl_get_device_gpio_range() - find device for GPIO range
315 * @gpio: the pin to locate the pin controller for
316 * @outdev: the pin control device if found
317 * @outrange: the GPIO range if found
318 *
319 * Find the pin controller handling a certain GPIO pin from the pinspace of
320 * the GPIO subsystem, return the device and the matching GPIO range. Returns
4650b7cb
DA
321 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
322 * may still have not been registered.
2744e8af 323 */
4ecce45d
SW
324static int pinctrl_get_device_gpio_range(unsigned gpio,
325 struct pinctrl_dev **outdev,
326 struct pinctrl_gpio_range **outrange)
2744e8af
LW
327{
328 struct pinctrl_dev *pctldev = NULL;
329
330 /* Loop over the pin controllers */
2744e8af
LW
331 list_for_each_entry(pctldev, &pinctrldev_list, node) {
332 struct pinctrl_gpio_range *range;
333
334 range = pinctrl_match_gpio_range(pctldev, gpio);
335 if (range != NULL) {
336 *outdev = pctldev;
337 *outrange = range;
2744e8af
LW
338 return 0;
339 }
340 }
2744e8af 341
4650b7cb 342 return -EPROBE_DEFER;
2744e8af
LW
343}
344
345/**
346 * pinctrl_add_gpio_range() - register a GPIO range for a controller
347 * @pctldev: pin controller device to add the range to
348 * @range: the GPIO range to add
349 *
350 * This adds a range of GPIOs to be handled by a certain pin controller. Call
351 * this to register handled ranges after registering your pin controller.
352 */
353void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
354 struct pinctrl_gpio_range *range)
355{
57b676f9 356 mutex_lock(&pinctrl_mutex);
8b9c139f 357 list_add_tail(&range->node, &pctldev->gpio_ranges);
57b676f9 358 mutex_unlock(&pinctrl_mutex);
2744e8af 359}
4ecce45d 360EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
2744e8af 361
3e5e00b6
DA
362void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
363 struct pinctrl_gpio_range *ranges,
364 unsigned nranges)
365{
366 int i;
367
368 for (i = 0; i < nranges; i++)
369 pinctrl_add_gpio_range(pctldev, &ranges[i]);
370}
371EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
372
192c369c 373struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
f23f1516
SH
374 struct pinctrl_gpio_range *range)
375{
376 struct pinctrl_dev *pctldev = get_pinctrl_dev_from_devname(devname);
377
dfa97515
LW
378 /*
379 * If we can't find this device, let's assume that is because
380 * it has not probed yet, so the driver trying to register this
381 * range need to defer probing.
382 */
f23f1516 383 if (!pctldev)
dfa97515 384 return ERR_PTR(-EPROBE_DEFER);
f23f1516
SH
385
386 pinctrl_add_gpio_range(pctldev, range);
387 return pctldev;
388}
192c369c 389EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
f23f1516 390
9afbefb2
LW
391/**
392 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
393 * @pctldev: the pin controller device to look in
394 * @pin: a controller-local number to find the range for
395 */
396struct pinctrl_gpio_range *
397pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
398 unsigned int pin)
399{
400 struct pinctrl_gpio_range *range = NULL;
401
402 /* Loop over the ranges */
403 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
404 /* Check if we're in the valid range */
405 if (pin >= range->pin_base &&
406 pin < range->pin_base + range->npins) {
407 return range;
408 }
409 }
410
411 return NULL;
412}
413EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
414
7e10ee68
VK
415/**
416 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
417 * @pctldev: pin controller device to remove the range from
418 * @range: the GPIO range to remove
419 */
420void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
421 struct pinctrl_gpio_range *range)
422{
423 mutex_lock(&pinctrl_mutex);
424 list_del(&range->node);
425 mutex_unlock(&pinctrl_mutex);
426}
427EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
428
7afde8ba
LW
429/**
430 * pinctrl_get_group_selector() - returns the group selector for a group
431 * @pctldev: the pin controller handling the group
432 * @pin_group: the pin group to look up
433 */
434int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
435 const char *pin_group)
436{
437 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
d1e90e9e 438 unsigned ngroups = pctlops->get_groups_count(pctldev);
7afde8ba
LW
439 unsigned group_selector = 0;
440
d1e90e9e 441 while (group_selector < ngroups) {
7afde8ba
LW
442 const char *gname = pctlops->get_group_name(pctldev,
443 group_selector);
444 if (!strcmp(gname, pin_group)) {
51cd24ee 445 dev_dbg(pctldev->dev,
7afde8ba
LW
446 "found group selector %u for %s\n",
447 group_selector,
448 pin_group);
449 return group_selector;
450 }
451
452 group_selector++;
453 }
454
51cd24ee 455 dev_err(pctldev->dev, "does not have pin group %s\n",
7afde8ba
LW
456 pin_group);
457
458 return -EINVAL;
459}
460
befe5bdf
LW
461/**
462 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
463 * @gpio: the GPIO pin number from the GPIO subsystem number space
464 *
465 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
466 * as part of their gpio_request() semantics, platforms and individual drivers
467 * shall *NOT* request GPIO pins to be muxed in.
468 */
469int pinctrl_request_gpio(unsigned gpio)
470{
471 struct pinctrl_dev *pctldev;
472 struct pinctrl_gpio_range *range;
473 int ret;
474 int pin;
475
57b676f9
SW
476 mutex_lock(&pinctrl_mutex);
477
befe5bdf 478 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
57b676f9 479 if (ret) {
51e13c24
HZ
480 if (pinctrl_ready_for_gpio_range(gpio))
481 ret = 0;
57b676f9 482 mutex_unlock(&pinctrl_mutex);
4650b7cb 483 return ret;
57b676f9 484 }
befe5bdf
LW
485
486 /* Convert to the pin controllers number space */
487 pin = gpio - range->base + range->pin_base;
488
57b676f9
SW
489 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
490
491 mutex_unlock(&pinctrl_mutex);
492 return ret;
befe5bdf
LW
493}
494EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
495
496/**
497 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
498 * @gpio: the GPIO pin number from the GPIO subsystem number space
499 *
500 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
501 * as part of their gpio_free() semantics, platforms and individual drivers
502 * shall *NOT* request GPIO pins to be muxed out.
503 */
504void pinctrl_free_gpio(unsigned gpio)
505{
506 struct pinctrl_dev *pctldev;
507 struct pinctrl_gpio_range *range;
508 int ret;
509 int pin;
510
57b676f9
SW
511 mutex_lock(&pinctrl_mutex);
512
befe5bdf 513 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
57b676f9
SW
514 if (ret) {
515 mutex_unlock(&pinctrl_mutex);
befe5bdf 516 return;
57b676f9 517 }
befe5bdf
LW
518
519 /* Convert to the pin controllers number space */
520 pin = gpio - range->base + range->pin_base;
521
57b676f9
SW
522 pinmux_free_gpio(pctldev, pin, range);
523
524 mutex_unlock(&pinctrl_mutex);
befe5bdf
LW
525}
526EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
527
528static int pinctrl_gpio_direction(unsigned gpio, bool input)
529{
530 struct pinctrl_dev *pctldev;
531 struct pinctrl_gpio_range *range;
532 int ret;
533 int pin;
534
535 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
536 if (ret)
537 return ret;
538
539 /* Convert to the pin controllers number space */
540 pin = gpio - range->base + range->pin_base;
541
542 return pinmux_gpio_direction(pctldev, range, pin, input);
543}
544
545/**
546 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
547 * @gpio: the GPIO pin number from the GPIO subsystem number space
548 *
549 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
550 * as part of their gpio_direction_input() semantics, platforms and individual
551 * drivers shall *NOT* touch pin control GPIO calls.
552 */
553int pinctrl_gpio_direction_input(unsigned gpio)
554{
57b676f9
SW
555 int ret;
556 mutex_lock(&pinctrl_mutex);
557 ret = pinctrl_gpio_direction(gpio, true);
558 mutex_unlock(&pinctrl_mutex);
559 return ret;
befe5bdf
LW
560}
561EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
562
563/**
564 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
565 * @gpio: the GPIO pin number from the GPIO subsystem number space
566 *
567 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
568 * as part of their gpio_direction_output() semantics, platforms and individual
569 * drivers shall *NOT* touch pin control GPIO calls.
570 */
571int pinctrl_gpio_direction_output(unsigned gpio)
572{
57b676f9
SW
573 int ret;
574 mutex_lock(&pinctrl_mutex);
575 ret = pinctrl_gpio_direction(gpio, false);
576 mutex_unlock(&pinctrl_mutex);
577 return ret;
befe5bdf
LW
578}
579EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
580
6e5e959d
SW
581static struct pinctrl_state *find_state(struct pinctrl *p,
582 const char *name)
befe5bdf 583{
6e5e959d
SW
584 struct pinctrl_state *state;
585
586 list_for_each_entry(state, &p->states, node)
587 if (!strcmp(state->name, name))
588 return state;
589
590 return NULL;
591}
592
593static struct pinctrl_state *create_state(struct pinctrl *p,
594 const char *name)
595{
596 struct pinctrl_state *state;
597
598 state = kzalloc(sizeof(*state), GFP_KERNEL);
599 if (state == NULL) {
600 dev_err(p->dev,
601 "failed to alloc struct pinctrl_state\n");
602 return ERR_PTR(-ENOMEM);
603 }
604
605 state->name = name;
606 INIT_LIST_HEAD(&state->settings);
607
608 list_add_tail(&state->node, &p->states);
609
610 return state;
611}
612
613static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
614{
615 struct pinctrl_state *state;
7ecdb16f 616 struct pinctrl_setting *setting;
6e5e959d 617 int ret;
befe5bdf 618
6e5e959d
SW
619 state = find_state(p, map->name);
620 if (!state)
621 state = create_state(p, map->name);
622 if (IS_ERR(state))
623 return PTR_ERR(state);
befe5bdf 624
1e2082b5
SW
625 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
626 return 0;
627
6e5e959d
SW
628 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
629 if (setting == NULL) {
630 dev_err(p->dev,
631 "failed to alloc struct pinctrl_setting\n");
632 return -ENOMEM;
633 }
befe5bdf 634
1e2082b5
SW
635 setting->type = map->type;
636
6e5e959d
SW
637 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
638 if (setting->pctldev == NULL) {
6e5e959d 639 kfree(setting);
89216494
LW
640 /* Do not defer probing of hogs (circular loop) */
641 if (!strcmp(map->ctrl_dev_name, map->dev_name))
642 return -ENODEV;
c05127c4
LW
643 /*
644 * OK let us guess that the driver is not there yet, and
645 * let's defer obtaining this pinctrl handle to later...
646 */
89216494
LW
647 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
648 map->ctrl_dev_name);
c05127c4 649 return -EPROBE_DEFER;
6e5e959d
SW
650 }
651
1a78958d
LW
652 setting->dev_name = map->dev_name;
653
1e2082b5
SW
654 switch (map->type) {
655 case PIN_MAP_TYPE_MUX_GROUP:
656 ret = pinmux_map_to_setting(map, setting);
657 break;
658 case PIN_MAP_TYPE_CONFIGS_PIN:
659 case PIN_MAP_TYPE_CONFIGS_GROUP:
660 ret = pinconf_map_to_setting(map, setting);
661 break;
662 default:
663 ret = -EINVAL;
664 break;
665 }
6e5e959d
SW
666 if (ret < 0) {
667 kfree(setting);
668 return ret;
669 }
670
671 list_add_tail(&setting->node, &state->settings);
672
673 return 0;
674}
675
676static struct pinctrl *find_pinctrl(struct device *dev)
677{
678 struct pinctrl *p;
679
1e2082b5 680 list_for_each_entry(p, &pinctrl_list, node)
6e5e959d
SW
681 if (p->dev == dev)
682 return p;
683
684 return NULL;
685}
686
687static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
688
689static struct pinctrl *create_pinctrl(struct device *dev)
690{
691 struct pinctrl *p;
692 const char *devname;
693 struct pinctrl_maps *maps_node;
694 int i;
695 struct pinctrl_map const *map;
696 int ret;
befe5bdf
LW
697
698 /*
699 * create the state cookie holder struct pinctrl for each
700 * mapping, this is what consumers will get when requesting
701 * a pin control handle with pinctrl_get()
702 */
02f5b989 703 p = kzalloc(sizeof(*p), GFP_KERNEL);
95dcd4ae
SW
704 if (p == NULL) {
705 dev_err(dev, "failed to alloc struct pinctrl\n");
befe5bdf 706 return ERR_PTR(-ENOMEM);
95dcd4ae 707 }
7ecdb16f 708 p->dev = dev;
6e5e959d 709 INIT_LIST_HEAD(&p->states);
57291ce2
SW
710 INIT_LIST_HEAD(&p->dt_maps);
711
712 ret = pinctrl_dt_to_map(p);
713 if (ret < 0) {
714 kfree(p);
715 return ERR_PTR(ret);
716 }
6e5e959d
SW
717
718 devname = dev_name(dev);
befe5bdf
LW
719
720 /* Iterate over the pin control maps to locate the right ones */
b2b3e66e 721 for_each_maps(maps_node, i, map) {
7ecdb16f
SW
722 /* Map must be for this device */
723 if (strcmp(map->dev_name, devname))
724 continue;
725
6e5e959d 726 ret = add_setting(p, map);
89216494
LW
727 /*
728 * At this point the adding of a setting may:
729 *
730 * - Defer, if the pinctrl device is not yet available
731 * - Fail, if the pinctrl device is not yet available,
732 * AND the setting is a hog. We cannot defer that, since
733 * the hog will kick in immediately after the device
734 * is registered.
735 *
736 * If the error returned was not -EPROBE_DEFER then we
737 * accumulate the errors to see if we end up with
738 * an -EPROBE_DEFER later, as that is the worst case.
739 */
740 if (ret == -EPROBE_DEFER) {
6e5e959d
SW
741 pinctrl_put_locked(p, false);
742 return ERR_PTR(ret);
7ecdb16f 743 }
befe5bdf 744 }
89216494
LW
745 if (ret < 0) {
746 /* If some other error than deferral occured, return here */
747 pinctrl_put_locked(p, false);
748 return ERR_PTR(ret);
749 }
befe5bdf 750
ab78029e
LW
751 kref_init(&p->users);
752
b0666ba4 753 /* Add the pinctrl handle to the global list */
8b9c139f 754 list_add_tail(&p->node, &pinctrl_list);
befe5bdf
LW
755
756 return p;
6e5e959d 757}
7ecdb16f 758
6e5e959d
SW
759static struct pinctrl *pinctrl_get_locked(struct device *dev)
760{
761 struct pinctrl *p;
7ecdb16f 762
6e5e959d
SW
763 if (WARN_ON(!dev))
764 return ERR_PTR(-EINVAL);
765
ab78029e
LW
766 /*
767 * See if somebody else (such as the device core) has already
768 * obtained a handle to the pinctrl for this device. In that case,
769 * return another pointer to it.
770 */
6e5e959d 771 p = find_pinctrl(dev);
ab78029e
LW
772 if (p != NULL) {
773 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
774 kref_get(&p->users);
775 return p;
776 }
7ecdb16f 777
d599bfb3 778 return create_pinctrl(dev);
befe5bdf 779}
b2b3e66e
SW
780
781/**
6e5e959d
SW
782 * pinctrl_get() - retrieves the pinctrl handle for a device
783 * @dev: the device to obtain the handle for
b2b3e66e 784 */
6e5e959d 785struct pinctrl *pinctrl_get(struct device *dev)
b2b3e66e
SW
786{
787 struct pinctrl *p;
788
57b676f9 789 mutex_lock(&pinctrl_mutex);
6e5e959d 790 p = pinctrl_get_locked(dev);
57b676f9 791 mutex_unlock(&pinctrl_mutex);
b2b3e66e
SW
792
793 return p;
794}
befe5bdf
LW
795EXPORT_SYMBOL_GPL(pinctrl_get);
796
6e5e959d 797static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
befe5bdf 798{
6e5e959d
SW
799 struct pinctrl_state *state, *n1;
800 struct pinctrl_setting *setting, *n2;
801
802 list_for_each_entry_safe(state, n1, &p->states, node) {
803 list_for_each_entry_safe(setting, n2, &state->settings, node) {
1e2082b5
SW
804 switch (setting->type) {
805 case PIN_MAP_TYPE_MUX_GROUP:
806 if (state == p->state)
807 pinmux_disable_setting(setting);
808 pinmux_free_setting(setting);
809 break;
810 case PIN_MAP_TYPE_CONFIGS_PIN:
811 case PIN_MAP_TYPE_CONFIGS_GROUP:
812 pinconf_free_setting(setting);
813 break;
814 default:
815 break;
816 }
6e5e959d
SW
817 list_del(&setting->node);
818 kfree(setting);
819 }
820 list_del(&state->node);
821 kfree(state);
7ecdb16f 822 }
befe5bdf 823
57291ce2
SW
824 pinctrl_dt_free_maps(p);
825
6e5e959d
SW
826 if (inlist)
827 list_del(&p->node);
befe5bdf
LW
828 kfree(p);
829}
befe5bdf
LW
830
831/**
ab78029e
LW
832 * pinctrl_release() - release the pinctrl handle
833 * @kref: the kref in the pinctrl being released
834 */
2917e833 835static void pinctrl_release(struct kref *kref)
ab78029e
LW
836{
837 struct pinctrl *p = container_of(kref, struct pinctrl, users);
838
839 pinctrl_put_locked(p, true);
840}
841
842/**
843 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
6e5e959d 844 * @p: the pinctrl handle to release
befe5bdf 845 */
57b676f9
SW
846void pinctrl_put(struct pinctrl *p)
847{
848 mutex_lock(&pinctrl_mutex);
ab78029e 849 kref_put(&p->users, pinctrl_release);
57b676f9
SW
850 mutex_unlock(&pinctrl_mutex);
851}
852EXPORT_SYMBOL_GPL(pinctrl_put);
853
6e5e959d
SW
854static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
855 const char *name)
befe5bdf 856{
6e5e959d 857 struct pinctrl_state *state;
befe5bdf 858
6e5e959d 859 state = find_state(p, name);
5b3aa5f7
DA
860 if (!state) {
861 if (pinctrl_dummy_state) {
862 /* create dummy state */
863 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
864 name);
865 state = create_state(p, name);
d599bfb3
RG
866 } else
867 state = ERR_PTR(-ENODEV);
5b3aa5f7 868 }
57b676f9 869
6e5e959d 870 return state;
befe5bdf 871}
befe5bdf
LW
872
873/**
6e5e959d
SW
874 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
875 * @p: the pinctrl handle to retrieve the state from
876 * @name: the state name to retrieve
befe5bdf 877 */
6e5e959d 878struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
57b676f9 879{
6e5e959d
SW
880 struct pinctrl_state *s;
881
57b676f9 882 mutex_lock(&pinctrl_mutex);
6e5e959d 883 s = pinctrl_lookup_state_locked(p, name);
57b676f9 884 mutex_unlock(&pinctrl_mutex);
6e5e959d
SW
885
886 return s;
57b676f9 887}
6e5e959d 888EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
57b676f9 889
6e5e959d
SW
890static int pinctrl_select_state_locked(struct pinctrl *p,
891 struct pinctrl_state *state)
befe5bdf 892{
6e5e959d
SW
893 struct pinctrl_setting *setting, *setting2;
894 int ret;
7ecdb16f 895
6e5e959d
SW
896 if (p->state == state)
897 return 0;
befe5bdf 898
6e5e959d
SW
899 if (p->state) {
900 /*
901 * The set of groups with a mux configuration in the old state
902 * may not be identical to the set of groups with a mux setting
903 * in the new state. While this might be unusual, it's entirely
904 * possible for the "user"-supplied mapping table to be written
905 * that way. For each group that was configured in the old state
906 * but not in the new state, this code puts that group into a
907 * safe/disabled state.
908 */
909 list_for_each_entry(setting, &p->state->settings, node) {
910 bool found = false;
1e2082b5
SW
911 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
912 continue;
6e5e959d 913 list_for_each_entry(setting2, &state->settings, node) {
1e2082b5
SW
914 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
915 continue;
916 if (setting2->data.mux.group ==
917 setting->data.mux.group) {
6e5e959d
SW
918 found = true;
919 break;
920 }
921 }
922 if (!found)
923 pinmux_disable_setting(setting);
924 }
925 }
926
927 p->state = state;
928
929 /* Apply all the settings for the new state */
930 list_for_each_entry(setting, &state->settings, node) {
1e2082b5
SW
931 switch (setting->type) {
932 case PIN_MAP_TYPE_MUX_GROUP:
933 ret = pinmux_enable_setting(setting);
934 break;
935 case PIN_MAP_TYPE_CONFIGS_PIN:
936 case PIN_MAP_TYPE_CONFIGS_GROUP:
937 ret = pinconf_apply_setting(setting);
938 break;
939 default:
940 ret = -EINVAL;
941 break;
942 }
6e5e959d
SW
943 if (ret < 0) {
944 /* FIXME: Difficult to return to prev state */
945 return ret;
946 }
befe5bdf 947 }
6e5e959d
SW
948
949 return 0;
57b676f9
SW
950}
951
952/**
6e5e959d
SW
953 * pinctrl_select() - select/activate/program a pinctrl state to HW
954 * @p: the pinctrl handle for the device that requests configuratio
955 * @state: the state handle to select/activate/program
57b676f9 956 */
6e5e959d 957int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
57b676f9 958{
6e5e959d
SW
959 int ret;
960
57b676f9 961 mutex_lock(&pinctrl_mutex);
6e5e959d 962 ret = pinctrl_select_state_locked(p, state);
57b676f9 963 mutex_unlock(&pinctrl_mutex);
6e5e959d
SW
964
965 return ret;
befe5bdf 966}
6e5e959d 967EXPORT_SYMBOL_GPL(pinctrl_select_state);
befe5bdf 968
6d4ca1fb
SW
969static void devm_pinctrl_release(struct device *dev, void *res)
970{
971 pinctrl_put(*(struct pinctrl **)res);
972}
973
974/**
975 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
976 * @dev: the device to obtain the handle for
977 *
978 * If there is a need to explicitly destroy the returned struct pinctrl,
979 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
980 */
981struct pinctrl *devm_pinctrl_get(struct device *dev)
982{
983 struct pinctrl **ptr, *p;
984
985 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
986 if (!ptr)
987 return ERR_PTR(-ENOMEM);
988
989 p = pinctrl_get(dev);
990 if (!IS_ERR(p)) {
991 *ptr = p;
992 devres_add(dev, ptr);
993 } else {
994 devres_free(ptr);
995 }
996
997 return p;
998}
999EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1000
1001static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1002{
1003 struct pinctrl **p = res;
1004
1005 return *p == data;
1006}
1007
1008/**
1009 * devm_pinctrl_put() - Resource managed pinctrl_put()
1010 * @p: the pinctrl handle to release
1011 *
1012 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1013 * this function will not need to be called and the resource management
1014 * code will ensure that the resource is freed.
1015 */
1016void devm_pinctrl_put(struct pinctrl *p)
1017{
a72149e8 1018 WARN_ON(devres_release(p->dev, devm_pinctrl_release,
6d4ca1fb 1019 devm_pinctrl_match, p));
6d4ca1fb
SW
1020}
1021EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1022
57291ce2
SW
1023int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
1024 bool dup, bool locked)
befe5bdf 1025{
1e2082b5 1026 int i, ret;
b2b3e66e 1027 struct pinctrl_maps *maps_node;
befe5bdf
LW
1028
1029 pr_debug("add %d pinmux maps\n", num_maps);
1030
1031 /* First sanity check the new mapping */
1032 for (i = 0; i < num_maps; i++) {
1e2082b5
SW
1033 if (!maps[i].dev_name) {
1034 pr_err("failed to register map %s (%d): no device given\n",
1035 maps[i].name, i);
1036 return -EINVAL;
1037 }
1038
befe5bdf
LW
1039 if (!maps[i].name) {
1040 pr_err("failed to register map %d: no map name given\n",
95dcd4ae 1041 i);
befe5bdf
LW
1042 return -EINVAL;
1043 }
1044
1e2082b5
SW
1045 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1046 !maps[i].ctrl_dev_name) {
befe5bdf
LW
1047 pr_err("failed to register map %s (%d): no pin control device given\n",
1048 maps[i].name, i);
1049 return -EINVAL;
1050 }
1051
1e2082b5
SW
1052 switch (maps[i].type) {
1053 case PIN_MAP_TYPE_DUMMY_STATE:
1054 break;
1055 case PIN_MAP_TYPE_MUX_GROUP:
1056 ret = pinmux_validate_map(&maps[i], i);
1057 if (ret < 0)
fde04f41 1058 return ret;
1e2082b5
SW
1059 break;
1060 case PIN_MAP_TYPE_CONFIGS_PIN:
1061 case PIN_MAP_TYPE_CONFIGS_GROUP:
1062 ret = pinconf_validate_map(&maps[i], i);
1063 if (ret < 0)
fde04f41 1064 return ret;
1e2082b5
SW
1065 break;
1066 default:
1067 pr_err("failed to register map %s (%d): invalid type given\n",
95dcd4ae 1068 maps[i].name, i);
1681f5ae
SW
1069 return -EINVAL;
1070 }
befe5bdf
LW
1071 }
1072
b2b3e66e
SW
1073 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
1074 if (!maps_node) {
1075 pr_err("failed to alloc struct pinctrl_maps\n");
1076 return -ENOMEM;
1077 }
befe5bdf 1078
b2b3e66e 1079 maps_node->num_maps = num_maps;
57291ce2
SW
1080 if (dup) {
1081 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
1082 GFP_KERNEL);
1083 if (!maps_node->maps) {
1084 pr_err("failed to duplicate mapping table\n");
1085 kfree(maps_node);
1086 return -ENOMEM;
1087 }
1088 } else {
1089 maps_node->maps = maps;
befe5bdf
LW
1090 }
1091
57291ce2
SW
1092 if (!locked)
1093 mutex_lock(&pinctrl_mutex);
b2b3e66e 1094 list_add_tail(&maps_node->node, &pinctrl_maps);
57291ce2
SW
1095 if (!locked)
1096 mutex_unlock(&pinctrl_mutex);
b2b3e66e 1097
befe5bdf
LW
1098 return 0;
1099}
1100
57291ce2
SW
1101/**
1102 * pinctrl_register_mappings() - register a set of pin controller mappings
1103 * @maps: the pincontrol mappings table to register. This should probably be
1104 * marked with __initdata so it can be discarded after boot. This
1105 * function will perform a shallow copy for the mapping entries.
1106 * @num_maps: the number of maps in the mapping table
1107 */
1108int pinctrl_register_mappings(struct pinctrl_map const *maps,
1109 unsigned num_maps)
1110{
1111 return pinctrl_register_map(maps, num_maps, true, false);
1112}
1113
1114void pinctrl_unregister_map(struct pinctrl_map const *map)
1115{
1116 struct pinctrl_maps *maps_node;
1117
1118 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1119 if (maps_node->maps == map) {
1120 list_del(&maps_node->node);
1121 return;
1122 }
1123 }
1124}
1125
840a47ba
JD
1126/**
1127 * pinctrl_force_sleep() - turn a given controller device into sleep state
1128 * @pctldev: pin controller device
1129 */
1130int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1131{
1132 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
1133 return pinctrl_select_state(pctldev->p, pctldev->hog_sleep);
1134 return 0;
1135}
1136EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1137
1138/**
1139 * pinctrl_force_default() - turn a given controller device into default state
1140 * @pctldev: pin controller device
1141 */
1142int pinctrl_force_default(struct pinctrl_dev *pctldev)
1143{
1144 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
1145 return pinctrl_select_state(pctldev->p, pctldev->hog_default);
1146 return 0;
1147}
1148EXPORT_SYMBOL_GPL(pinctrl_force_default);
1149
2744e8af
LW
1150#ifdef CONFIG_DEBUG_FS
1151
1152static int pinctrl_pins_show(struct seq_file *s, void *what)
1153{
1154 struct pinctrl_dev *pctldev = s->private;
1155 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
706e8520 1156 unsigned i, pin;
2744e8af
LW
1157
1158 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
2744e8af 1159
57b676f9
SW
1160 mutex_lock(&pinctrl_mutex);
1161
706e8520
CP
1162 /* The pin number can be retrived from the pin controller descriptor */
1163 for (i = 0; i < pctldev->desc->npins; i++) {
2744e8af
LW
1164 struct pin_desc *desc;
1165
706e8520 1166 pin = pctldev->desc->pins[i].number;
2744e8af
LW
1167 desc = pin_desc_get(pctldev, pin);
1168 /* Pin space may be sparse */
1169 if (desc == NULL)
1170 continue;
1171
1172 seq_printf(s, "pin %d (%s) ", pin,
1173 desc->name ? desc->name : "unnamed");
1174
1175 /* Driver-specific info per pin */
1176 if (ops->pin_dbg_show)
1177 ops->pin_dbg_show(pctldev, s, pin);
1178
1179 seq_puts(s, "\n");
1180 }
1181
57b676f9
SW
1182 mutex_unlock(&pinctrl_mutex);
1183
2744e8af
LW
1184 return 0;
1185}
1186
1187static int pinctrl_groups_show(struct seq_file *s, void *what)
1188{
1189 struct pinctrl_dev *pctldev = s->private;
1190 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
d1e90e9e 1191 unsigned ngroups, selector = 0;
2744e8af 1192
d1e90e9e 1193 ngroups = ops->get_groups_count(pctldev);
57b676f9
SW
1194 mutex_lock(&pinctrl_mutex);
1195
2744e8af 1196 seq_puts(s, "registered pin groups:\n");
d1e90e9e 1197 while (selector < ngroups) {
a5818a8b 1198 const unsigned *pins;
2744e8af
LW
1199 unsigned num_pins;
1200 const char *gname = ops->get_group_name(pctldev, selector);
dcb5dbc3 1201 const char *pname;
2744e8af
LW
1202 int ret;
1203 int i;
1204
1205 ret = ops->get_group_pins(pctldev, selector,
1206 &pins, &num_pins);
1207 if (ret)
1208 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1209 gname);
1210 else {
dcb5dbc3
DA
1211 seq_printf(s, "group: %s\n", gname);
1212 for (i = 0; i < num_pins; i++) {
1213 pname = pin_get_name(pctldev, pins[i]);
b4dd784b
WY
1214 if (WARN_ON(!pname)) {
1215 mutex_unlock(&pinctrl_mutex);
dcb5dbc3 1216 return -EINVAL;
b4dd784b 1217 }
dcb5dbc3
DA
1218 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1219 }
1220 seq_puts(s, "\n");
2744e8af
LW
1221 }
1222 selector++;
1223 }
1224
57b676f9 1225 mutex_unlock(&pinctrl_mutex);
2744e8af
LW
1226
1227 return 0;
1228}
1229
1230static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1231{
1232 struct pinctrl_dev *pctldev = s->private;
1233 struct pinctrl_gpio_range *range = NULL;
1234
1235 seq_puts(s, "GPIO ranges handled:\n");
1236
57b676f9
SW
1237 mutex_lock(&pinctrl_mutex);
1238
2744e8af 1239 /* Loop over the ranges */
2744e8af 1240 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
75d6642a
LW
1241 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1242 range->id, range->name,
1243 range->base, (range->base + range->npins - 1),
1244 range->pin_base,
1245 (range->pin_base + range->npins - 1));
2744e8af 1246 }
57b676f9
SW
1247
1248 mutex_unlock(&pinctrl_mutex);
2744e8af
LW
1249
1250 return 0;
1251}
1252
1253static int pinctrl_devices_show(struct seq_file *s, void *what)
1254{
1255 struct pinctrl_dev *pctldev;
1256
ae6b4d85 1257 seq_puts(s, "name [pinmux] [pinconf]\n");
57b676f9
SW
1258
1259 mutex_lock(&pinctrl_mutex);
1260
2744e8af
LW
1261 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1262 seq_printf(s, "%s ", pctldev->desc->name);
1263 if (pctldev->desc->pmxops)
ae6b4d85
LW
1264 seq_puts(s, "yes ");
1265 else
1266 seq_puts(s, "no ");
1267 if (pctldev->desc->confops)
2744e8af
LW
1268 seq_puts(s, "yes");
1269 else
1270 seq_puts(s, "no");
1271 seq_puts(s, "\n");
1272 }
57b676f9
SW
1273
1274 mutex_unlock(&pinctrl_mutex);
2744e8af
LW
1275
1276 return 0;
1277}
1278
1e2082b5
SW
1279static inline const char *map_type(enum pinctrl_map_type type)
1280{
1281 static const char * const names[] = {
1282 "INVALID",
1283 "DUMMY_STATE",
1284 "MUX_GROUP",
1285 "CONFIGS_PIN",
1286 "CONFIGS_GROUP",
1287 };
1288
1289 if (type >= ARRAY_SIZE(names))
1290 return "UNKNOWN";
1291
1292 return names[type];
1293}
1294
3eedb437
SW
1295static int pinctrl_maps_show(struct seq_file *s, void *what)
1296{
1297 struct pinctrl_maps *maps_node;
1298 int i;
1299 struct pinctrl_map const *map;
1300
1301 seq_puts(s, "Pinctrl maps:\n");
1302
57b676f9
SW
1303 mutex_lock(&pinctrl_mutex);
1304
3eedb437 1305 for_each_maps(maps_node, i, map) {
1e2082b5
SW
1306 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1307 map->dev_name, map->name, map_type(map->type),
1308 map->type);
1309
1310 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1311 seq_printf(s, "controlling device %s\n",
1312 map->ctrl_dev_name);
1313
1314 switch (map->type) {
1315 case PIN_MAP_TYPE_MUX_GROUP:
1316 pinmux_show_map(s, map);
1317 break;
1318 case PIN_MAP_TYPE_CONFIGS_PIN:
1319 case PIN_MAP_TYPE_CONFIGS_GROUP:
1320 pinconf_show_map(s, map);
1321 break;
1322 default:
1323 break;
1324 }
1325
1326 seq_printf(s, "\n");
3eedb437 1327 }
57b676f9
SW
1328
1329 mutex_unlock(&pinctrl_mutex);
3eedb437
SW
1330
1331 return 0;
1332}
1333
befe5bdf
LW
1334static int pinctrl_show(struct seq_file *s, void *what)
1335{
1336 struct pinctrl *p;
6e5e959d 1337 struct pinctrl_state *state;
7ecdb16f 1338 struct pinctrl_setting *setting;
befe5bdf
LW
1339
1340 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
57b676f9
SW
1341
1342 mutex_lock(&pinctrl_mutex);
1343
befe5bdf 1344 list_for_each_entry(p, &pinctrl_list, node) {
6e5e959d
SW
1345 seq_printf(s, "device: %s current state: %s\n",
1346 dev_name(p->dev),
1347 p->state ? p->state->name : "none");
1348
1349 list_for_each_entry(state, &p->states, node) {
1350 seq_printf(s, " state: %s\n", state->name);
befe5bdf 1351
6e5e959d 1352 list_for_each_entry(setting, &state->settings, node) {
1e2082b5
SW
1353 struct pinctrl_dev *pctldev = setting->pctldev;
1354
1355 seq_printf(s, " type: %s controller %s ",
1356 map_type(setting->type),
1357 pinctrl_dev_get_name(pctldev));
1358
1359 switch (setting->type) {
1360 case PIN_MAP_TYPE_MUX_GROUP:
1361 pinmux_show_setting(s, setting);
1362 break;
1363 case PIN_MAP_TYPE_CONFIGS_PIN:
1364 case PIN_MAP_TYPE_CONFIGS_GROUP:
1365 pinconf_show_setting(s, setting);
1366 break;
1367 default:
1368 break;
1369 }
6e5e959d 1370 }
befe5bdf 1371 }
befe5bdf
LW
1372 }
1373
57b676f9
SW
1374 mutex_unlock(&pinctrl_mutex);
1375
befe5bdf
LW
1376 return 0;
1377}
1378
2744e8af
LW
1379static int pinctrl_pins_open(struct inode *inode, struct file *file)
1380{
1381 return single_open(file, pinctrl_pins_show, inode->i_private);
1382}
1383
1384static int pinctrl_groups_open(struct inode *inode, struct file *file)
1385{
1386 return single_open(file, pinctrl_groups_show, inode->i_private);
1387}
1388
1389static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1390{
1391 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1392}
1393
1394static int pinctrl_devices_open(struct inode *inode, struct file *file)
1395{
1396 return single_open(file, pinctrl_devices_show, NULL);
1397}
1398
3eedb437
SW
1399static int pinctrl_maps_open(struct inode *inode, struct file *file)
1400{
1401 return single_open(file, pinctrl_maps_show, NULL);
1402}
1403
befe5bdf
LW
1404static int pinctrl_open(struct inode *inode, struct file *file)
1405{
1406 return single_open(file, pinctrl_show, NULL);
1407}
1408
2744e8af
LW
1409static const struct file_operations pinctrl_pins_ops = {
1410 .open = pinctrl_pins_open,
1411 .read = seq_read,
1412 .llseek = seq_lseek,
1413 .release = single_release,
1414};
1415
1416static const struct file_operations pinctrl_groups_ops = {
1417 .open = pinctrl_groups_open,
1418 .read = seq_read,
1419 .llseek = seq_lseek,
1420 .release = single_release,
1421};
1422
1423static const struct file_operations pinctrl_gpioranges_ops = {
1424 .open = pinctrl_gpioranges_open,
1425 .read = seq_read,
1426 .llseek = seq_lseek,
1427 .release = single_release,
1428};
1429
3eedb437
SW
1430static const struct file_operations pinctrl_devices_ops = {
1431 .open = pinctrl_devices_open,
befe5bdf
LW
1432 .read = seq_read,
1433 .llseek = seq_lseek,
1434 .release = single_release,
1435};
1436
3eedb437
SW
1437static const struct file_operations pinctrl_maps_ops = {
1438 .open = pinctrl_maps_open,
2744e8af
LW
1439 .read = seq_read,
1440 .llseek = seq_lseek,
1441 .release = single_release,
1442};
1443
befe5bdf
LW
1444static const struct file_operations pinctrl_ops = {
1445 .open = pinctrl_open,
1446 .read = seq_read,
1447 .llseek = seq_lseek,
1448 .release = single_release,
1449};
1450
2744e8af
LW
1451static struct dentry *debugfs_root;
1452
1453static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1454{
02157160 1455 struct dentry *device_root;
2744e8af 1456
51cd24ee 1457 device_root = debugfs_create_dir(dev_name(pctldev->dev),
2744e8af 1458 debugfs_root);
02157160
TL
1459 pctldev->device_root = device_root;
1460
2744e8af
LW
1461 if (IS_ERR(device_root) || !device_root) {
1462 pr_warn("failed to create debugfs directory for %s\n",
51cd24ee 1463 dev_name(pctldev->dev));
2744e8af
LW
1464 return;
1465 }
1466 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1467 device_root, pctldev, &pinctrl_pins_ops);
1468 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1469 device_root, pctldev, &pinctrl_groups_ops);
1470 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1471 device_root, pctldev, &pinctrl_gpioranges_ops);
1472 pinmux_init_device_debugfs(device_root, pctldev);
ae6b4d85 1473 pinconf_init_device_debugfs(device_root, pctldev);
2744e8af
LW
1474}
1475
02157160
TL
1476static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1477{
1478 debugfs_remove_recursive(pctldev->device_root);
1479}
1480
2744e8af
LW
1481static void pinctrl_init_debugfs(void)
1482{
1483 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1484 if (IS_ERR(debugfs_root) || !debugfs_root) {
1485 pr_warn("failed to create debugfs directory\n");
1486 debugfs_root = NULL;
1487 return;
1488 }
1489
1490 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1491 debugfs_root, NULL, &pinctrl_devices_ops);
3eedb437
SW
1492 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1493 debugfs_root, NULL, &pinctrl_maps_ops);
befe5bdf
LW
1494 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1495 debugfs_root, NULL, &pinctrl_ops);
2744e8af
LW
1496}
1497
1498#else /* CONFIG_DEBUG_FS */
1499
1500static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1501{
1502}
1503
1504static void pinctrl_init_debugfs(void)
1505{
1506}
1507
02157160
TL
1508static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1509{
1510}
1511
2744e8af
LW
1512#endif
1513
d26bc49f
SW
1514static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1515{
1516 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1517
1518 if (!ops ||
d1e90e9e 1519 !ops->get_groups_count ||
d26bc49f
SW
1520 !ops->get_group_name ||
1521 !ops->get_group_pins)
1522 return -EINVAL;
1523
57291ce2
SW
1524 if (ops->dt_node_to_map && !ops->dt_free_map)
1525 return -EINVAL;
1526
d26bc49f
SW
1527 return 0;
1528}
1529
2744e8af
LW
1530/**
1531 * pinctrl_register() - register a pin controller device
1532 * @pctldesc: descriptor for this pin controller
1533 * @dev: parent device for this pin controller
1534 * @driver_data: private pin controller data for this pin controller
1535 */
1536struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1537 struct device *dev, void *driver_data)
1538{
2744e8af
LW
1539 struct pinctrl_dev *pctldev;
1540 int ret;
1541
da9aecb0 1542 if (!pctldesc)
2744e8af 1543 return NULL;
da9aecb0 1544 if (!pctldesc->name)
2744e8af
LW
1545 return NULL;
1546
02f5b989 1547 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
95dcd4ae
SW
1548 if (pctldev == NULL) {
1549 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
b9130b77 1550 return NULL;
95dcd4ae 1551 }
b9130b77
TL
1552
1553 /* Initialize pin control device struct */
1554 pctldev->owner = pctldesc->owner;
1555 pctldev->desc = pctldesc;
1556 pctldev->driver_data = driver_data;
1557 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
b9130b77 1558 INIT_LIST_HEAD(&pctldev->gpio_ranges);
b9130b77
TL
1559 pctldev->dev = dev;
1560
d26bc49f 1561 /* check core ops for sanity */
da9aecb0 1562 if (pinctrl_check_ops(pctldev)) {
ad6e1107 1563 dev_err(dev, "pinctrl ops lacks necessary functions\n");
d26bc49f
SW
1564 goto out_err;
1565 }
1566
2744e8af
LW
1567 /* If we're implementing pinmuxing, check the ops for sanity */
1568 if (pctldesc->pmxops) {
da9aecb0 1569 if (pinmux_check_ops(pctldev))
b9130b77 1570 goto out_err;
2744e8af
LW
1571 }
1572
ae6b4d85
LW
1573 /* If we're implementing pinconfig, check the ops for sanity */
1574 if (pctldesc->confops) {
da9aecb0 1575 if (pinconf_check_ops(pctldev))
b9130b77 1576 goto out_err;
ae6b4d85
LW
1577 }
1578
2744e8af 1579 /* Register all the pins */
ad6e1107 1580 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
2744e8af
LW
1581 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1582 if (ret) {
ad6e1107 1583 dev_err(dev, "error during pin registration\n");
2744e8af
LW
1584 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1585 pctldesc->npins);
51cd24ee 1586 goto out_err;
2744e8af
LW
1587 }
1588
57b676f9
SW
1589 mutex_lock(&pinctrl_mutex);
1590
8b9c139f 1591 list_add_tail(&pctldev->node, &pinctrldev_list);
57b676f9 1592
6e5e959d
SW
1593 pctldev->p = pinctrl_get_locked(pctldev->dev);
1594 if (!IS_ERR(pctldev->p)) {
840a47ba 1595 pctldev->hog_default =
6e5e959d
SW
1596 pinctrl_lookup_state_locked(pctldev->p,
1597 PINCTRL_STATE_DEFAULT);
840a47ba 1598 if (IS_ERR(pctldev->hog_default)) {
ad6e1107
JC
1599 dev_dbg(dev, "failed to lookup the default state\n");
1600 } else {
840a47ba
JD
1601 if (pinctrl_select_state_locked(pctldev->p,
1602 pctldev->hog_default))
ad6e1107
JC
1603 dev_err(dev,
1604 "failed to select default state\n");
ad6e1107 1605 }
840a47ba
JD
1606
1607 pctldev->hog_sleep =
1608 pinctrl_lookup_state_locked(pctldev->p,
1609 PINCTRL_STATE_SLEEP);
1610 if (IS_ERR(pctldev->hog_sleep))
1611 dev_dbg(dev, "failed to lookup the sleep state\n");
6e5e959d 1612 }
57b676f9
SW
1613
1614 mutex_unlock(&pinctrl_mutex);
1615
2304b473
SW
1616 pinctrl_init_device_debugfs(pctldev);
1617
2744e8af
LW
1618 return pctldev;
1619
51cd24ee
SW
1620out_err:
1621 kfree(pctldev);
2744e8af
LW
1622 return NULL;
1623}
1624EXPORT_SYMBOL_GPL(pinctrl_register);
1625
1626/**
1627 * pinctrl_unregister() - unregister pinmux
1628 * @pctldev: pin controller to unregister
1629 *
1630 * Called by pinmux drivers to unregister a pinmux.
1631 */
1632void pinctrl_unregister(struct pinctrl_dev *pctldev)
1633{
5d589b09 1634 struct pinctrl_gpio_range *range, *n;
2744e8af
LW
1635 if (pctldev == NULL)
1636 return;
1637
02157160 1638 pinctrl_remove_device_debugfs(pctldev);
57b676f9
SW
1639
1640 mutex_lock(&pinctrl_mutex);
1641
6e5e959d
SW
1642 if (!IS_ERR(pctldev->p))
1643 pinctrl_put_locked(pctldev->p, true);
57b676f9 1644
2744e8af 1645 /* TODO: check that no pinmuxes are still active? */
2744e8af 1646 list_del(&pctldev->node);
2744e8af
LW
1647 /* Destroy descriptor tree */
1648 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1649 pctldev->desc->npins);
5d589b09
DA
1650 /* remove gpio ranges map */
1651 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1652 list_del(&range->node);
1653
51cd24ee 1654 kfree(pctldev);
57b676f9
SW
1655
1656 mutex_unlock(&pinctrl_mutex);
2744e8af
LW
1657}
1658EXPORT_SYMBOL_GPL(pinctrl_unregister);
1659
1660static int __init pinctrl_init(void)
1661{
1662 pr_info("initialized pinctrl subsystem\n");
1663 pinctrl_init_debugfs();
1664 return 0;
1665}
1666
1667/* init early since many drivers really need to initialized pinmux early */
1668core_initcall(pinctrl_init);