]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/pinctrl/pinmux.c
UBUNTU: Ubuntu-5.15.0-39.42
[mirror_ubuntu-jammy-kernel.git] / drivers / pinctrl / pinmux.c
CommitLineData
af873fce 1// SPDX-License-Identifier: GPL-2.0-only
2744e8af
LW
2/*
3 * Core driver for the pin muxing portions of the pin control subsystem
4 *
e93bcee0 5 * Copyright (C) 2011-2012 ST-Ericsson SA
2744e8af
LW
6 * Written on behalf of Linaro for ST-Ericsson
7 * Based on bits of regulator core, gpio core and clk core
8 *
9 * Author: Linus Walleij <linus.walleij@linaro.org>
10 *
7ecdb16f 11 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
2744e8af
LW
12 */
13#define pr_fmt(fmt) "pinmux core: " fmt
14
6199f6be 15#include <linux/ctype.h>
2744e8af
LW
16#include <linux/kernel.h>
17#include <linux/module.h>
18#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>
97607d15 24#include <linux/string.h>
2744e8af
LW
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
27#include <linux/pinctrl/machine.h>
28#include <linux/pinctrl/pinmux.h>
29#include "core.h"
befe5bdf 30#include "pinmux.h"
2744e8af 31
03665e0f
SW
32int pinmux_check_ops(struct pinctrl_dev *pctldev)
33{
34 const struct pinmux_ops *ops = pctldev->desc->pmxops;
a1d31f71 35 unsigned nfuncs;
03665e0f
SW
36 unsigned selector = 0;
37
38 /* Check that we implement required operations */
a1d31f71
DA
39 if (!ops ||
40 !ops->get_functions_count ||
03665e0f
SW
41 !ops->get_function_name ||
42 !ops->get_function_groups ||
03e9f0ca 43 !ops->set_mux) {
ad6e1107 44 dev_err(pctldev->dev, "pinmux ops lacks necessary functions\n");
03665e0f 45 return -EINVAL;
ad6e1107 46 }
03665e0f 47 /* Check that all functions registered have names */
a1d31f71 48 nfuncs = ops->get_functions_count(pctldev);
d1e90e9e 49 while (selector < nfuncs) {
03665e0f
SW
50 const char *fname = ops->get_function_name(pctldev,
51 selector);
52 if (!fname) {
a1d31f71 53 dev_err(pctldev->dev, "pinmux ops has no name for function%u\n",
03665e0f
SW
54 selector);
55 return -EINVAL;
56 }
57 selector++;
58 }
59
60 return 0;
61}
62
3f713b7c 63int pinmux_validate_map(const struct pinctrl_map *map, int i)
1e2082b5
SW
64{
65 if (!map->data.mux.function) {
66 pr_err("failed to register map %s (%d): no function given\n",
67 map->name, i);
68 return -EINVAL;
69 }
70
71 return 0;
72}
73
472a61e7
SW
74/**
75 * pinmux_can_be_used_for_gpio() - check if a specific pin
76 * is either muxed to a different function or used as gpio.
77 *
d340351f 78 * @pctldev: the associated pin controller device
472a61e7
SW
79 * @pin: the pin number in the global pin space
80 *
81 * Controllers not defined as strict will always return true,
82 * menaning that the gpio can be used.
83 */
84bool pinmux_can_be_used_for_gpio(struct pinctrl_dev *pctldev, unsigned pin)
85{
86 struct pin_desc *desc = pin_desc_get(pctldev, pin);
87 const struct pinmux_ops *ops = pctldev->desc->pmxops;
88
89 /* Can't inspect pin, assume it can be used */
6ba2fd39 90 if (!desc || !ops)
472a61e7
SW
91 return true;
92
93 if (ops->strict && desc->mux_usecount)
94 return false;
95
96 return !(ops->strict && !!desc->gpio_owner);
97}
98
2744e8af
LW
99/**
100 * pin_request() - request a single pin to be muxed in, typically for GPIO
d340351f 101 * @pctldev: the associated pin controller device
2744e8af 102 * @pin: the pin number in the global pin space
3cc70ed3
SW
103 * @owner: a representation of the owner of this pin; typically the device
104 * name that controls its mux function, or the requested GPIO name
2744e8af
LW
105 * @gpio_range: the range matching the GPIO pin if this is a request for a
106 * single GPIO pin
107 */
108static int pin_request(struct pinctrl_dev *pctldev,
3cc70ed3 109 int pin, const char *owner,
2744e8af
LW
110 struct pinctrl_gpio_range *gpio_range)
111{
112 struct pin_desc *desc;
113 const struct pinmux_ops *ops = pctldev->desc->pmxops;
114 int status = -EINVAL;
115
2744e8af
LW
116 desc = pin_desc_get(pctldev, pin);
117 if (desc == NULL) {
51cd24ee 118 dev_err(pctldev->dev,
d4705316
SW
119 "pin %d is not registered so it cannot be requested\n",
120 pin);
2744e8af
LW
121 goto out;
122 }
123
d0bd8df5
DA
124 dev_dbg(pctldev->dev, "request pin %d (%s) for %s\n",
125 pin, desc->name, owner);
126
b1eb8fab
VZ
127 if ((!gpio_range || ops->strict) &&
128 desc->mux_usecount && strcmp(desc->mux_owner, owner)) {
129 dev_err(pctldev->dev,
130 "pin %s already requested by %s; cannot claim for %s\n",
131 desc->name, desc->mux_owner, owner);
132 goto out;
133 }
134
135 if ((gpio_range || ops->strict) && desc->gpio_owner) {
136 dev_err(pctldev->dev,
137 "pin %s already requested by %s; cannot claim for %s\n",
138 desc->name, desc->gpio_owner, owner);
139 goto out;
140 }
0e3db173 141
b1eb8fab 142 if (gpio_range) {
652162d4
SW
143 desc->gpio_owner = owner;
144 } else {
652162d4
SW
145 desc->mux_usecount++;
146 if (desc->mux_usecount > 1)
147 return 0;
148
149 desc->mux_owner = owner;
150 }
2744e8af
LW
151
152 /* Let each pin increase references to this module */
153 if (!try_module_get(pctldev->owner)) {
51cd24ee 154 dev_err(pctldev->dev,
2744e8af
LW
155 "could not increase module refcount for pin %d\n",
156 pin);
157 status = -EINVAL;
158 goto out_free_pin;
159 }
160
161 /*
162 * If there is no kind of request function for the pin we just assume
163 * we got it by default and proceed.
164 */
3712a3c4 165 if (gpio_range && ops->gpio_request_enable)
2744e8af
LW
166 /* This requests and enables a single GPIO pin */
167 status = ops->gpio_request_enable(pctldev, gpio_range, pin);
168 else if (ops->request)
169 status = ops->request(pctldev, pin);
170 else
171 status = 0;
172
0e3db173 173 if (status) {
d4705316 174 dev_err(pctldev->dev, "request() failed for pin %d\n", pin);
0e3db173
SW
175 module_put(pctldev->owner);
176 }
177
2744e8af 178out_free_pin:
0e3db173 179 if (status) {
652162d4
SW
180 if (gpio_range) {
181 desc->gpio_owner = NULL;
182 } else {
183 desc->mux_usecount--;
184 if (!desc->mux_usecount)
185 desc->mux_owner = NULL;
186 }
0e3db173 187 }
2744e8af
LW
188out:
189 if (status)
51cd24ee 190 dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
d4705316 191 pin, owner, status);
2744e8af
LW
192
193 return status;
194}
195
196/**
197 * pin_free() - release a single muxed in pin so something else can be muxed
198 * @pctldev: pin controller device handling this pin
199 * @pin: the pin to free
3712a3c4
SW
200 * @gpio_range: the range matching the GPIO pin if this is a request for a
201 * single GPIO pin
336cdba0 202 *
3cc70ed3
SW
203 * This function returns a pointer to the previous owner. This is used
204 * for callers that dynamically allocate an owner name so it can be freed
336cdba0 205 * once the pin is free. This is done for GPIO request functions.
2744e8af 206 */
3712a3c4
SW
207static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
208 struct pinctrl_gpio_range *gpio_range)
2744e8af
LW
209{
210 const struct pinmux_ops *ops = pctldev->desc->pmxops;
211 struct pin_desc *desc;
3cc70ed3 212 const char *owner;
2744e8af
LW
213
214 desc = pin_desc_get(pctldev, pin);
215 if (desc == NULL) {
51cd24ee 216 dev_err(pctldev->dev,
2744e8af 217 "pin is not registered so it cannot be freed\n");
3712a3c4 218 return NULL;
2744e8af
LW
219 }
220
652162d4 221 if (!gpio_range) {
740924a2
RG
222 /*
223 * A pin should not be freed more times than allocated.
224 */
225 if (WARN_ON(!desc->mux_usecount))
226 return NULL;
652162d4
SW
227 desc->mux_usecount--;
228 if (desc->mux_usecount)
229 return NULL;
230 }
0e3db173 231
3712a3c4
SW
232 /*
233 * If there is no kind of request function for the pin we just assume
234 * we got it by default and proceed.
235 */
236 if (gpio_range && ops->gpio_disable_free)
237 ops->gpio_disable_free(pctldev, gpio_range, pin);
238 else if (ops->free)
2744e8af
LW
239 ops->free(pctldev, pin);
240
652162d4
SW
241 if (gpio_range) {
242 owner = desc->gpio_owner;
243 desc->gpio_owner = NULL;
244 } else {
245 owner = desc->mux_owner;
246 desc->mux_owner = NULL;
247 desc->mux_setting = NULL;
248 }
249
2744e8af 250 module_put(pctldev->owner);
3712a3c4 251
3cc70ed3 252 return owner;
2744e8af
LW
253}
254
255/**
befe5bdf
LW
256 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
257 * @pctldev: pin controller device affected
258 * @pin: the pin to mux in for GPIO
259 * @range: the applicable GPIO range
d340351f 260 * @gpio: number of requested GPIO
2744e8af 261 */
befe5bdf
LW
262int pinmux_request_gpio(struct pinctrl_dev *pctldev,
263 struct pinctrl_gpio_range *range,
264 unsigned pin, unsigned gpio)
2744e8af 265{
3cc70ed3 266 const char *owner;
2744e8af 267 int ret;
2744e8af
LW
268
269 /* Conjure some name stating what chip and pin this is taken by */
23a895ae 270 owner = kasprintf(GFP_KERNEL, "%s:%d", range->name, gpio);
3cc70ed3 271 if (!owner)
1fb1f054 272 return -ENOMEM;
5d2eaf80 273
3cc70ed3 274 ret = pin_request(pctldev, pin, owner, range);
5d2eaf80 275 if (ret < 0)
3cc70ed3 276 kfree(owner);
5d2eaf80
SW
277
278 return ret;
2744e8af 279}
2744e8af
LW
280
281/**
befe5bdf
LW
282 * pinmux_free_gpio() - release a pin from GPIO muxing
283 * @pctldev: the pin controller device for the pin
284 * @pin: the affected currently GPIO-muxed in pin
285 * @range: applicable GPIO range
2744e8af 286 */
befe5bdf
LW
287void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
288 struct pinctrl_gpio_range *range)
2744e8af 289{
3cc70ed3 290 const char *owner;
2744e8af 291
3cc70ed3
SW
292 owner = pin_free(pctldev, pin, range);
293 kfree(owner);
2744e8af 294}
2744e8af 295
befe5bdf
LW
296/**
297 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
298 * @pctldev: the pin controller handling this pin
299 * @range: applicable GPIO range
300 * @pin: the affected GPIO pin in this controller
301 * @input: true if we set the pin as input, false for output
302 */
303int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
304 struct pinctrl_gpio_range *range,
305 unsigned pin, bool input)
542e704f 306{
542e704f
LW
307 const struct pinmux_ops *ops;
308 int ret;
542e704f
LW
309
310 ops = pctldev->desc->pmxops;
311
542e704f
LW
312 if (ops->gpio_set_direction)
313 ret = ops->gpio_set_direction(pctldev, range, pin, input);
314 else
315 ret = 0;
316
317 return ret;
318}
319
7ecdb16f
SW
320static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
321 const char *function)
2744e8af
LW
322{
323 const struct pinmux_ops *ops = pctldev->desc->pmxops;
d1e90e9e 324 unsigned nfuncs = ops->get_functions_count(pctldev);
2744e8af
LW
325 unsigned selector = 0;
326
327 /* See if this pctldev has this function */
d1e90e9e 328 while (selector < nfuncs) {
163dc9f3 329 const char *fname = ops->get_function_name(pctldev, selector);
2744e8af 330
7ecdb16f
SW
331 if (!strcmp(function, fname))
332 return selector;
2744e8af 333
2744e8af
LW
334 selector++;
335 }
336
2744e8af
LW
337 return -EINVAL;
338}
339
3f713b7c 340int pinmux_map_to_setting(const struct pinctrl_map *map,
7ecdb16f 341 struct pinctrl_setting *setting)
2744e8af 342{
7ecdb16f
SW
343 struct pinctrl_dev *pctldev = setting->pctldev;
344 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
7ecdb16f
SW
345 char const * const *groups;
346 unsigned num_groups;
2744e8af 347 int ret;
7ecdb16f 348 const char *group;
2744e8af 349
ad8bb720
DA
350 if (!pmxops) {
351 dev_err(pctldev->dev, "does not support mux function\n");
352 return -EINVAL;
353 }
354
15f70e1b 355 ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function);
ad6e1107
JC
356 if (ret < 0) {
357 dev_err(pctldev->dev, "invalid function %s in map table\n",
358 map->data.mux.function);
15f70e1b 359 return ret;
ad6e1107 360 }
15f70e1b 361 setting->data.mux.func = ret;
2744e8af 362
1e2082b5 363 ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
7ecdb16f 364 &groups, &num_groups);
ad6e1107
JC
365 if (ret < 0) {
366 dev_err(pctldev->dev, "can't query groups for function %s\n",
367 map->data.mux.function);
7ecdb16f 368 return ret;
ad6e1107
JC
369 }
370 if (!num_groups) {
371 dev_err(pctldev->dev,
372 "function %s can't be selected on any group\n",
373 map->data.mux.function);
2744e8af 374 return -EINVAL;
ad6e1107 375 }
1e2082b5 376 if (map->data.mux.group) {
1e2082b5 377 group = map->data.mux.group;
dff43594
AS
378 ret = match_string(groups, num_groups, group);
379 if (ret < 0) {
ad6e1107
JC
380 dev_err(pctldev->dev,
381 "invalid group \"%s\" for function \"%s\"\n",
382 group, map->data.mux.function);
dff43594 383 return ret;
ad6e1107 384 }
7ecdb16f
SW
385 } else {
386 group = groups[0];
2744e8af 387 }
2744e8af 388
15f70e1b 389 ret = pinctrl_get_group_selector(pctldev, group);
ad6e1107
JC
390 if (ret < 0) {
391 dev_err(pctldev->dev, "invalid group %s in map table\n",
392 map->data.mux.group);
15f70e1b 393 return ret;
ad6e1107 394 }
15f70e1b 395 setting->data.mux.group = ret;
2744e8af 396
2744e8af
LW
397 return 0;
398}
399
3f713b7c 400void pinmux_free_setting(const struct pinctrl_setting *setting)
2744e8af 401{
1a78958d 402 /* This function is currently unused */
2744e8af 403}
2744e8af 404
3f713b7c 405int pinmux_enable_setting(const struct pinctrl_setting *setting)
2744e8af 406{
7ecdb16f 407 struct pinctrl_dev *pctldev = setting->pctldev;
ba110d90 408 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
befe5bdf 409 const struct pinmux_ops *ops = pctldev->desc->pmxops;
e5b3b2d9
AT
410 int ret = 0;
411 const unsigned *pins = NULL;
412 unsigned num_pins = 0;
ba110d90
SW
413 int i;
414 struct pin_desc *desc;
415
e5b3b2d9
AT
416 if (pctlops->get_group_pins)
417 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
418 &pins, &num_pins);
419
ba110d90 420 if (ret) {
1c8e7944
LW
421 const char *gname;
422
ba110d90 423 /* errors only affect debug data, so just warn */
1c8e7944
LW
424 gname = pctlops->get_group_name(pctldev,
425 setting->data.mux.group);
ba110d90 426 dev_warn(pctldev->dev,
1c8e7944
LW
427 "could not get pins for group %s\n",
428 gname);
ba110d90
SW
429 num_pins = 0;
430 }
431
1a78958d
LW
432 /* Try to allocate all pins in this group, one by one */
433 for (i = 0; i < num_pins; i++) {
434 ret = pin_request(pctldev, pins[i], setting->dev_name, NULL);
435 if (ret) {
1c8e7944
LW
436 const char *gname;
437 const char *pname;
438
439 desc = pin_desc_get(pctldev, pins[i]);
440 pname = desc ? desc->name : "non-existing";
441 gname = pctlops->get_group_name(pctldev,
442 setting->data.mux.group);
1a78958d 443 dev_err(pctldev->dev,
1c8e7944
LW
444 "could not request pin %d (%s) from group %s "
445 " on device %s\n",
446 pins[i], pname, gname,
447 pinctrl_dev_get_name(pctldev));
e38d457d 448 goto err_pin_request;
1a78958d
LW
449 }
450 }
451
452 /* Now that we have acquired the pins, encode the mux setting */
ba110d90
SW
453 for (i = 0; i < num_pins; i++) {
454 desc = pin_desc_get(pctldev, pins[i]);
455 if (desc == NULL) {
456 dev_warn(pctldev->dev,
457 "could not get pin desc for pin %d\n",
458 pins[i]);
459 continue;
460 }
461 desc->mux_setting = &(setting->data.mux);
462 }
2744e8af 463
03e9f0ca
LW
464 ret = ops->set_mux(pctldev, setting->data.mux.func,
465 setting->data.mux.group);
e38d457d
AL
466
467 if (ret)
03e9f0ca 468 goto err_set_mux;
e38d457d
AL
469
470 return 0;
471
03e9f0ca 472err_set_mux:
e38d457d
AL
473 for (i = 0; i < num_pins; i++) {
474 desc = pin_desc_get(pctldev, pins[i]);
475 if (desc)
476 desc->mux_setting = NULL;
477 }
478err_pin_request:
479 /* On error release all taken pins */
480 while (--i >= 0)
481 pin_free(pctldev, pins[i], NULL);
482
483 return ret;
2744e8af 484}
2744e8af 485
3f713b7c 486void pinmux_disable_setting(const struct pinctrl_setting *setting)
2744e8af 487{
7ecdb16f 488 struct pinctrl_dev *pctldev = setting->pctldev;
ba110d90 489 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
e5b3b2d9
AT
490 int ret = 0;
491 const unsigned *pins = NULL;
492 unsigned num_pins = 0;
ba110d90
SW
493 int i;
494 struct pin_desc *desc;
495
e5b3b2d9
AT
496 if (pctlops->get_group_pins)
497 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
498 &pins, &num_pins);
ba110d90 499 if (ret) {
1c8e7944
LW
500 const char *gname;
501
ba110d90 502 /* errors only affect debug data, so just warn */
1c8e7944
LW
503 gname = pctlops->get_group_name(pctldev,
504 setting->data.mux.group);
ba110d90 505 dev_warn(pctldev->dev,
1c8e7944
LW
506 "could not get pins for group %s\n",
507 gname);
ba110d90
SW
508 num_pins = 0;
509 }
510
1a78958d 511 /* Flag the descs that no setting is active */
ba110d90
SW
512 for (i = 0; i < num_pins; i++) {
513 desc = pin_desc_get(pctldev, pins[i]);
514 if (desc == NULL) {
515 dev_warn(pctldev->dev,
516 "could not get pin desc for pin %d\n",
517 pins[i]);
518 continue;
519 }
744f0a9a 520 if (desc->mux_setting == &(setting->data.mux)) {
744f0a9a 521 pin_free(pctldev, pins[i], NULL);
1c8e7944
LW
522 } else {
523 const char *gname;
1c8e7944 524
1c8e7944
LW
525 gname = pctlops->get_group_name(pctldev,
526 setting->data.mux.group);
527 dev_warn(pctldev->dev,
528 "not freeing pin %d (%s) as part of "
529 "deactivating group %s - it is already "
530 "used for some other setting",
808e657c 531 pins[i], desc->name, gname);
744f0a9a 532 }
ba110d90 533 }
2744e8af 534}
2744e8af 535
2744e8af
LW
536#ifdef CONFIG_DEBUG_FS
537
538/* Called from pincontrol core */
539static int pinmux_functions_show(struct seq_file *s, void *what)
540{
541 struct pinctrl_dev *pctldev = s->private;
542 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
ad8bb720 543 unsigned nfuncs;
2744e8af
LW
544 unsigned func_selector = 0;
545
ad8bb720
DA
546 if (!pmxops)
547 return 0;
57b676f9 548
42fed7ba 549 mutex_lock(&pctldev->mutex);
ad8bb720 550 nfuncs = pmxops->get_functions_count(pctldev);
d1e90e9e 551 while (func_selector < nfuncs) {
2744e8af
LW
552 const char *func = pmxops->get_function_name(pctldev,
553 func_selector);
554 const char * const *groups;
555 unsigned num_groups;
556 int ret;
557 int i;
558
559 ret = pmxops->get_function_groups(pctldev, func_selector,
560 &groups, &num_groups);
9d7ebbbf 561 if (ret) {
2744e8af
LW
562 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
563 func);
9d7ebbbf
LD
564 func_selector++;
565 continue;
566 }
2744e8af 567
3bbf9b89 568 seq_printf(s, "function %d: %s, groups = [ ", func_selector, func);
2744e8af
LW
569 for (i = 0; i < num_groups; i++)
570 seq_printf(s, "%s ", groups[i]);
571 seq_puts(s, "]\n");
572
573 func_selector++;
2744e8af
LW
574 }
575
42fed7ba 576 mutex_unlock(&pctldev->mutex);
57b676f9 577
2744e8af
LW
578 return 0;
579}
580
581static int pinmux_pins_show(struct seq_file *s, void *what)
582{
583 struct pinctrl_dev *pctldev = s->private;
ba110d90
SW
584 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
585 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
706e8520 586 unsigned i, pin;
2744e8af 587
ad8bb720
DA
588 if (!pmxops)
589 return 0;
590
2744e8af 591 seq_puts(s, "Pinmux settings per pin\n");
81508855
LW
592 if (pmxops->strict)
593 seq_puts(s,
594 "Format: pin (name): mux_owner|gpio_owner (strict) hog?\n");
595 else
596 seq_puts(s,
597 "Format: pin (name): mux_owner gpio_owner hog?\n");
2744e8af 598
42fed7ba 599 mutex_lock(&pctldev->mutex);
57b676f9 600
706e8520
CP
601 /* The pin number can be retrived from the pin controller descriptor */
602 for (i = 0; i < pctldev->desc->npins; i++) {
2744e8af 603 struct pin_desc *desc;
1cf94c45 604 bool is_hog = false;
2744e8af 605
706e8520 606 pin = pctldev->desc->pins[i].number;
2744e8af 607 desc = pin_desc_get(pctldev, pin);
706e8520 608 /* Skip if we cannot search the pin */
2744e8af
LW
609 if (desc == NULL)
610 continue;
611
652162d4
SW
612 if (desc->mux_owner &&
613 !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
1cf94c45
LW
614 is_hog = true;
615
81508855
LW
616 if (pmxops->strict) {
617 if (desc->mux_owner)
618 seq_printf(s, "pin %d (%s): device %s%s",
cf9d994d 619 pin, desc->name, desc->mux_owner,
81508855
LW
620 is_hog ? " (HOG)" : "");
621 else if (desc->gpio_owner)
622 seq_printf(s, "pin %d (%s): GPIO %s",
cf9d994d 623 pin, desc->name, desc->gpio_owner);
81508855
LW
624 else
625 seq_printf(s, "pin %d (%s): UNCLAIMED",
cf9d994d 626 pin, desc->name);
81508855
LW
627 } else {
628 /* For non-strict controllers */
cf9d994d 629 seq_printf(s, "pin %d (%s): %s %s%s", pin, desc->name,
81508855
LW
630 desc->mux_owner ? desc->mux_owner
631 : "(MUX UNCLAIMED)",
632 desc->gpio_owner ? desc->gpio_owner
633 : "(GPIO UNCLAIMED)",
634 is_hog ? " (HOG)" : "");
635 }
ba110d90 636
81508855 637 /* If mux: print function+group claiming the pin */
ba110d90
SW
638 if (desc->mux_setting)
639 seq_printf(s, " function %s group %s\n",
640 pmxops->get_function_name(pctldev,
641 desc->mux_setting->func),
642 pctlops->get_group_name(pctldev,
643 desc->mux_setting->group));
644 else
ffd10c2e 645 seq_putc(s, '\n');
2744e8af
LW
646 }
647
42fed7ba 648 mutex_unlock(&pctldev->mutex);
57b676f9 649
2744e8af
LW
650 return 0;
651}
652
3f713b7c 653void pinmux_show_map(struct seq_file *s, const struct pinctrl_map *map)
1e2082b5
SW
654{
655 seq_printf(s, "group %s\nfunction %s\n",
656 map->data.mux.group ? map->data.mux.group : "(default)",
657 map->data.mux.function);
658}
659
660void pinmux_show_setting(struct seq_file *s,
3f713b7c 661 const struct pinctrl_setting *setting)
2744e8af 662{
7ecdb16f
SW
663 struct pinctrl_dev *pctldev = setting->pctldev;
664 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
665 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
666
1e2082b5
SW
667 seq_printf(s, "group: %s (%u) function: %s (%u)\n",
668 pctlops->get_group_name(pctldev, setting->data.mux.group),
669 setting->data.mux.group,
670 pmxops->get_function_name(pctldev, setting->data.mux.func),
671 setting->data.mux.func);
2744e8af
LW
672}
673
0819dc72
YL
674DEFINE_SHOW_ATTRIBUTE(pinmux_functions);
675DEFINE_SHOW_ATTRIBUTE(pinmux_pins);
2744e8af 676
6199f6be
DF
677#define PINMUX_SELECT_MAX 128
678static ssize_t pinmux_select(struct file *file, const char __user *user_buf,
679 size_t len, loff_t *ppos)
680{
681 struct seq_file *sfile = file->private_data;
682 struct pinctrl_dev *pctldev = sfile->private;
683 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
684 const char *const *groups;
685 char *buf, *gname, *fname;
686 unsigned int num_groups;
687 int fsel, gsel, ret;
688
689 if (len > PINMUX_SELECT_MAX)
690 return -ENOMEM;
691
692 buf = kzalloc(PINMUX_SELECT_MAX, GFP_KERNEL);
693 if (!buf)
694 return -ENOMEM;
695
696 ret = strncpy_from_user(buf, user_buf, PINMUX_SELECT_MAX);
697 if (ret < 0)
698 goto exit_free_buf;
699 buf[len-1] = '\0';
700
701 /* remove leading and trailing spaces of input buffer */
702 gname = strstrip(buf);
703 if (*gname == '\0') {
704 ret = -EINVAL;
705 goto exit_free_buf;
706 }
707
708 /* find a separator which is a spacelike character */
709 for (fname = gname; !isspace(*fname); fname++) {
710 if (*fname == '\0') {
711 ret = -EINVAL;
712 goto exit_free_buf;
713 }
714 }
715 *fname = '\0';
716
717 /* drop extra spaces between function and group names */
718 fname = skip_spaces(fname + 1);
719 if (*fname == '\0') {
720 ret = -EINVAL;
721 goto exit_free_buf;
722 }
723
724 ret = pinmux_func_name_to_selector(pctldev, fname);
725 if (ret < 0) {
726 dev_err(pctldev->dev, "invalid function %s in map table\n", fname);
727 goto exit_free_buf;
728 }
729 fsel = ret;
730
731 ret = pmxops->get_function_groups(pctldev, fsel, &groups, &num_groups);
732 if (ret) {
733 dev_err(pctldev->dev, "no groups for function %d (%s)", fsel, fname);
734 goto exit_free_buf;
735 }
736
737 ret = match_string(groups, num_groups, gname);
738 if (ret < 0) {
739 dev_err(pctldev->dev, "invalid group %s", gname);
740 goto exit_free_buf;
741 }
742
743 ret = pinctrl_get_group_selector(pctldev, gname);
744 if (ret < 0) {
745 dev_err(pctldev->dev, "failed to get group selector for %s", gname);
746 goto exit_free_buf;
747 }
748 gsel = ret;
749
750 ret = pmxops->set_mux(pctldev, fsel, gsel);
751 if (ret) {
752 dev_err(pctldev->dev, "set_mux() failed: %d", ret);
753 goto exit_free_buf;
754 }
755 ret = len;
756
757exit_free_buf:
758 kfree(buf);
759
760 return ret;
761}
762
763static int pinmux_select_open(struct inode *inode, struct file *file)
764{
765 return single_open(file, NULL, inode->i_private);
766}
767
768static const struct file_operations pinmux_select_ops = {
769 .owner = THIS_MODULE,
770 .open = pinmux_select_open,
771 .write = pinmux_select,
772 .llseek = no_llseek,
773 .release = single_release,
774};
775
2744e8af
LW
776void pinmux_init_device_debugfs(struct dentry *devroot,
777 struct pinctrl_dev *pctldev)
778{
47473813 779 debugfs_create_file("pinmux-functions", 0444,
0819dc72 780 devroot, pctldev, &pinmux_functions_fops);
47473813 781 debugfs_create_file("pinmux-pins", 0444,
0819dc72 782 devroot, pctldev, &pinmux_pins_fops);
6199f6be
DF
783 debugfs_create_file("pinmux-select", 0200,
784 devroot, pctldev, &pinmux_select_ops);
2744e8af
LW
785}
786
787#endif /* CONFIG_DEBUG_FS */
a76edc89
TL
788
789#ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
790
791/**
792 * pinmux_generic_get_function_count() - returns number of functions
793 * @pctldev: pin controller device
794 */
795int pinmux_generic_get_function_count(struct pinctrl_dev *pctldev)
796{
797 return pctldev->num_functions;
798}
799EXPORT_SYMBOL_GPL(pinmux_generic_get_function_count);
800
801/**
802 * pinmux_generic_get_function_name() - returns the function name
803 * @pctldev: pin controller device
804 * @selector: function number
805 */
806const char *
807pinmux_generic_get_function_name(struct pinctrl_dev *pctldev,
808 unsigned int selector)
809{
810 struct function_desc *function;
811
812 function = radix_tree_lookup(&pctldev->pin_function_tree,
813 selector);
814 if (!function)
815 return NULL;
816
817 return function->name;
818}
819EXPORT_SYMBOL_GPL(pinmux_generic_get_function_name);
820
821/**
822 * pinmux_generic_get_function_groups() - gets the function groups
823 * @pctldev: pin controller device
824 * @selector: function number
825 * @groups: array of pin groups
826 * @num_groups: number of pin groups
827 */
828int pinmux_generic_get_function_groups(struct pinctrl_dev *pctldev,
829 unsigned int selector,
830 const char * const **groups,
831 unsigned * const num_groups)
832{
833 struct function_desc *function;
834
835 function = radix_tree_lookup(&pctldev->pin_function_tree,
836 selector);
837 if (!function) {
838 dev_err(pctldev->dev, "%s could not find function%i\n",
839 __func__, selector);
840 return -EINVAL;
841 }
842 *groups = function->group_names;
843 *num_groups = function->num_group_names;
844
845 return 0;
846}
847EXPORT_SYMBOL_GPL(pinmux_generic_get_function_groups);
848
849/**
850 * pinmux_generic_get_function() - returns a function based on the number
851 * @pctldev: pin controller device
d340351f 852 * @selector: function number
a76edc89
TL
853 */
854struct function_desc *pinmux_generic_get_function(struct pinctrl_dev *pctldev,
855 unsigned int selector)
856{
857 struct function_desc *function;
858
859 function = radix_tree_lookup(&pctldev->pin_function_tree,
860 selector);
861 if (!function)
862 return NULL;
863
864 return function;
865}
866EXPORT_SYMBOL_GPL(pinmux_generic_get_function);
867
868/**
6bffa7e1 869 * pinmux_generic_add_function() - adds a function group
a76edc89
TL
870 * @pctldev: pin controller device
871 * @name: name of the function
872 * @groups: array of pin groups
873 * @num_groups: number of pin groups
874 * @data: pin controller driver specific data
875 */
876int pinmux_generic_add_function(struct pinctrl_dev *pctldev,
877 const char *name,
878 const char **groups,
879 const unsigned int num_groups,
880 void *data)
881{
882 struct function_desc *function;
f913cfce
TL
883 int selector;
884
885 if (!name)
886 return -EINVAL;
887
888 selector = pinmux_func_name_to_selector(pctldev, name);
889 if (selector >= 0)
890 return selector;
891
892 selector = pctldev->num_functions;
a76edc89
TL
893
894 function = devm_kzalloc(pctldev->dev, sizeof(*function), GFP_KERNEL);
895 if (!function)
896 return -ENOMEM;
897
898 function->name = name;
899 function->group_names = groups;
900 function->num_group_names = num_groups;
901 function->data = data;
902
f913cfce 903 radix_tree_insert(&pctldev->pin_function_tree, selector, function);
a76edc89
TL
904
905 pctldev->num_functions++;
906
f913cfce 907 return selector;
a76edc89
TL
908}
909EXPORT_SYMBOL_GPL(pinmux_generic_add_function);
910
911/**
912 * pinmux_generic_remove_function() - removes a numbered function
913 * @pctldev: pin controller device
914 * @selector: function number
915 *
916 * Note that the caller must take care of locking.
917 */
918int pinmux_generic_remove_function(struct pinctrl_dev *pctldev,
919 unsigned int selector)
920{
921 struct function_desc *function;
922
923 function = radix_tree_lookup(&pctldev->pin_function_tree,
924 selector);
925 if (!function)
926 return -ENOENT;
927
928 radix_tree_delete(&pctldev->pin_function_tree, selector);
929 devm_kfree(pctldev->dev, function);
930
931 pctldev->num_functions--;
932
933 return 0;
934}
935EXPORT_SYMBOL_GPL(pinmux_generic_remove_function);
936
937/**
938 * pinmux_generic_free_functions() - removes all functions
939 * @pctldev: pin controller device
940 *
664b7c47
TL
941 * Note that the caller must take care of locking. The pinctrl
942 * functions are allocated with devm_kzalloc() so no need to free
943 * them here.
a76edc89
TL
944 */
945void pinmux_generic_free_functions(struct pinctrl_dev *pctldev)
946{
947 struct radix_tree_iter iter;
906a2a39 948 void __rcu **slot;
a76edc89
TL
949
950 radix_tree_for_each_slot(slot, &pctldev->pin_function_tree, &iter, 0)
664b7c47 951 radix_tree_delete(&pctldev->pin_function_tree, iter.index);
a76edc89
TL
952
953 pctldev->num_functions = 0;
954}
955
956#endif /* CONFIG_GENERIC_PINMUX_FUNCTIONS */