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