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