This source file includes following definitions.
- pinmux_check_ops
- pinmux_validate_map
- pinmux_can_be_used_for_gpio
- pin_request
- pin_free
- pinmux_request_gpio
- pinmux_free_gpio
- pinmux_gpio_direction
- pinmux_func_name_to_selector
- pinmux_map_to_setting
- pinmux_free_setting
- pinmux_enable_setting
- pinmux_disable_setting
- pinmux_functions_show
- pinmux_pins_show
- pinmux_show_map
- pinmux_show_setting
- pinmux_init_device_debugfs
- pinmux_generic_get_function_count
- pinmux_generic_get_function_name
- pinmux_generic_get_function_groups
- pinmux_generic_get_function
- pinmux_generic_add_function
- pinmux_generic_remove_function
- pinmux_generic_free_functions
1
2
3
4
5
6
7
8
9
10
11
12
13 #define pr_fmt(fmt) "pinmux core: " fmt
14
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/device.h>
19 #include <linux/slab.h>
20 #include <linux/radix-tree.h>
21 #include <linux/err.h>
22 #include <linux/list.h>
23 #include <linux/string.h>
24 #include <linux/debugfs.h>
25 #include <linux/seq_file.h>
26 #include <linux/pinctrl/machine.h>
27 #include <linux/pinctrl/pinmux.h>
28 #include "core.h"
29 #include "pinmux.h"
30
31 int pinmux_check_ops(struct pinctrl_dev *pctldev)
32 {
33 const struct pinmux_ops *ops = pctldev->desc->pmxops;
34 unsigned nfuncs;
35 unsigned selector = 0;
36
37
38 if (!ops ||
39 !ops->get_functions_count ||
40 !ops->get_function_name ||
41 !ops->get_function_groups ||
42 !ops->set_mux) {
43 dev_err(pctldev->dev, "pinmux ops lacks necessary functions\n");
44 return -EINVAL;
45 }
46
47 nfuncs = ops->get_functions_count(pctldev);
48 while (selector < nfuncs) {
49 const char *fname = ops->get_function_name(pctldev,
50 selector);
51 if (!fname) {
52 dev_err(pctldev->dev, "pinmux ops has no name for function%u\n",
53 selector);
54 return -EINVAL;
55 }
56 selector++;
57 }
58
59 return 0;
60 }
61
62 int pinmux_validate_map(const struct pinctrl_map *map, int i)
63 {
64 if (!map->data.mux.function) {
65 pr_err("failed to register map %s (%d): no function given\n",
66 map->name, i);
67 return -EINVAL;
68 }
69
70 return 0;
71 }
72
73
74
75
76
77
78
79
80
81
82 bool pinmux_can_be_used_for_gpio(struct pinctrl_dev *pctldev, unsigned pin)
83 {
84 struct pin_desc *desc = pin_desc_get(pctldev, pin);
85 const struct pinmux_ops *ops = pctldev->desc->pmxops;
86
87
88 if (!desc || !ops)
89 return true;
90
91 if (ops->strict && desc->mux_usecount)
92 return false;
93
94 return !(ops->strict && !!desc->gpio_owner);
95 }
96
97
98
99
100
101
102
103
104
105 static int pin_request(struct pinctrl_dev *pctldev,
106 int pin, const char *owner,
107 struct pinctrl_gpio_range *gpio_range)
108 {
109 struct pin_desc *desc;
110 const struct pinmux_ops *ops = pctldev->desc->pmxops;
111 int status = -EINVAL;
112
113 desc = pin_desc_get(pctldev, pin);
114 if (desc == NULL) {
115 dev_err(pctldev->dev,
116 "pin %d is not registered so it cannot be requested\n",
117 pin);
118 goto out;
119 }
120
121 dev_dbg(pctldev->dev, "request pin %d (%s) for %s\n",
122 pin, desc->name, owner);
123
124 if ((!gpio_range || ops->strict) &&
125 desc->mux_usecount && strcmp(desc->mux_owner, owner)) {
126 dev_err(pctldev->dev,
127 "pin %s already requested by %s; cannot claim for %s\n",
128 desc->name, desc->mux_owner, owner);
129 goto out;
130 }
131
132 if ((gpio_range || ops->strict) && desc->gpio_owner) {
133 dev_err(pctldev->dev,
134 "pin %s already requested by %s; cannot claim for %s\n",
135 desc->name, desc->gpio_owner, owner);
136 goto out;
137 }
138
139 if (gpio_range) {
140 desc->gpio_owner = owner;
141 } else {
142 desc->mux_usecount++;
143 if (desc->mux_usecount > 1)
144 return 0;
145
146 desc->mux_owner = owner;
147 }
148
149
150 if (!try_module_get(pctldev->owner)) {
151 dev_err(pctldev->dev,
152 "could not increase module refcount for pin %d\n",
153 pin);
154 status = -EINVAL;
155 goto out_free_pin;
156 }
157
158
159
160
161
162 if (gpio_range && ops->gpio_request_enable)
163
164 status = ops->gpio_request_enable(pctldev, gpio_range, pin);
165 else if (ops->request)
166 status = ops->request(pctldev, pin);
167 else
168 status = 0;
169
170 if (status) {
171 dev_err(pctldev->dev, "request() failed for pin %d\n", pin);
172 module_put(pctldev->owner);
173 }
174
175 out_free_pin:
176 if (status) {
177 if (gpio_range) {
178 desc->gpio_owner = NULL;
179 } else {
180 desc->mux_usecount--;
181 if (!desc->mux_usecount)
182 desc->mux_owner = NULL;
183 }
184 }
185 out:
186 if (status)
187 dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
188 pin, owner, status);
189
190 return status;
191 }
192
193
194
195
196
197
198
199
200
201
202
203
204 static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
205 struct pinctrl_gpio_range *gpio_range)
206 {
207 const struct pinmux_ops *ops = pctldev->desc->pmxops;
208 struct pin_desc *desc;
209 const char *owner;
210
211 desc = pin_desc_get(pctldev, pin);
212 if (desc == NULL) {
213 dev_err(pctldev->dev,
214 "pin is not registered so it cannot be freed\n");
215 return NULL;
216 }
217
218 if (!gpio_range) {
219
220
221
222 if (WARN_ON(!desc->mux_usecount))
223 return NULL;
224 desc->mux_usecount--;
225 if (desc->mux_usecount)
226 return NULL;
227 }
228
229
230
231
232
233 if (gpio_range && ops->gpio_disable_free)
234 ops->gpio_disable_free(pctldev, gpio_range, pin);
235 else if (ops->free)
236 ops->free(pctldev, pin);
237
238 if (gpio_range) {
239 owner = desc->gpio_owner;
240 desc->gpio_owner = NULL;
241 } else {
242 owner = desc->mux_owner;
243 desc->mux_owner = NULL;
244 desc->mux_setting = NULL;
245 }
246
247 module_put(pctldev->owner);
248
249 return owner;
250 }
251
252
253
254
255
256
257
258 int pinmux_request_gpio(struct pinctrl_dev *pctldev,
259 struct pinctrl_gpio_range *range,
260 unsigned pin, unsigned gpio)
261 {
262 const char *owner;
263 int ret;
264
265
266 owner = kasprintf(GFP_KERNEL, "%s:%d", range->name, gpio);
267 if (!owner)
268 return -ENOMEM;
269
270 ret = pin_request(pctldev, pin, owner, range);
271 if (ret < 0)
272 kfree(owner);
273
274 return ret;
275 }
276
277
278
279
280
281
282
283 void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
284 struct pinctrl_gpio_range *range)
285 {
286 const char *owner;
287
288 owner = pin_free(pctldev, pin, range);
289 kfree(owner);
290 }
291
292
293
294
295
296
297
298
299 int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
300 struct pinctrl_gpio_range *range,
301 unsigned pin, bool input)
302 {
303 const struct pinmux_ops *ops;
304 int ret;
305
306 ops = pctldev->desc->pmxops;
307
308 if (ops->gpio_set_direction)
309 ret = ops->gpio_set_direction(pctldev, range, pin, input);
310 else
311 ret = 0;
312
313 return ret;
314 }
315
316 static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
317 const char *function)
318 {
319 const struct pinmux_ops *ops = pctldev->desc->pmxops;
320 unsigned nfuncs = ops->get_functions_count(pctldev);
321 unsigned selector = 0;
322
323
324 while (selector < nfuncs) {
325 const char *fname = ops->get_function_name(pctldev, selector);
326
327 if (!strcmp(function, fname))
328 return selector;
329
330 selector++;
331 }
332
333 return -EINVAL;
334 }
335
336 int pinmux_map_to_setting(const struct pinctrl_map *map,
337 struct pinctrl_setting *setting)
338 {
339 struct pinctrl_dev *pctldev = setting->pctldev;
340 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
341 char const * const *groups;
342 unsigned num_groups;
343 int ret;
344 const char *group;
345
346 if (!pmxops) {
347 dev_err(pctldev->dev, "does not support mux function\n");
348 return -EINVAL;
349 }
350
351 ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function);
352 if (ret < 0) {
353 dev_err(pctldev->dev, "invalid function %s in map table\n",
354 map->data.mux.function);
355 return ret;
356 }
357 setting->data.mux.func = ret;
358
359 ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
360 &groups, &num_groups);
361 if (ret < 0) {
362 dev_err(pctldev->dev, "can't query groups for function %s\n",
363 map->data.mux.function);
364 return ret;
365 }
366 if (!num_groups) {
367 dev_err(pctldev->dev,
368 "function %s can't be selected on any group\n",
369 map->data.mux.function);
370 return -EINVAL;
371 }
372 if (map->data.mux.group) {
373 group = map->data.mux.group;
374 ret = match_string(groups, num_groups, group);
375 if (ret < 0) {
376 dev_err(pctldev->dev,
377 "invalid group \"%s\" for function \"%s\"\n",
378 group, map->data.mux.function);
379 return ret;
380 }
381 } else {
382 group = groups[0];
383 }
384
385 ret = pinctrl_get_group_selector(pctldev, group);
386 if (ret < 0) {
387 dev_err(pctldev->dev, "invalid group %s in map table\n",
388 map->data.mux.group);
389 return ret;
390 }
391 setting->data.mux.group = ret;
392
393 return 0;
394 }
395
396 void pinmux_free_setting(const struct pinctrl_setting *setting)
397 {
398
399 }
400
401 int pinmux_enable_setting(const struct pinctrl_setting *setting)
402 {
403 struct pinctrl_dev *pctldev = setting->pctldev;
404 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
405 const struct pinmux_ops *ops = pctldev->desc->pmxops;
406 int ret = 0;
407 const unsigned *pins = NULL;
408 unsigned num_pins = 0;
409 int i;
410 struct pin_desc *desc;
411
412 if (pctlops->get_group_pins)
413 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
414 &pins, &num_pins);
415
416 if (ret) {
417 const char *gname;
418
419
420 gname = pctlops->get_group_name(pctldev,
421 setting->data.mux.group);
422 dev_warn(pctldev->dev,
423 "could not get pins for group %s\n",
424 gname);
425 num_pins = 0;
426 }
427
428
429 for (i = 0; i < num_pins; i++) {
430 ret = pin_request(pctldev, pins[i], setting->dev_name, NULL);
431 if (ret) {
432 const char *gname;
433 const char *pname;
434
435 desc = pin_desc_get(pctldev, pins[i]);
436 pname = desc ? desc->name : "non-existing";
437 gname = pctlops->get_group_name(pctldev,
438 setting->data.mux.group);
439 dev_err(pctldev->dev,
440 "could not request pin %d (%s) from group %s "
441 " on device %s\n",
442 pins[i], pname, gname,
443 pinctrl_dev_get_name(pctldev));
444 goto err_pin_request;
445 }
446 }
447
448
449 for (i = 0; i < num_pins; i++) {
450 desc = pin_desc_get(pctldev, pins[i]);
451 if (desc == NULL) {
452 dev_warn(pctldev->dev,
453 "could not get pin desc for pin %d\n",
454 pins[i]);
455 continue;
456 }
457 desc->mux_setting = &(setting->data.mux);
458 }
459
460 ret = ops->set_mux(pctldev, setting->data.mux.func,
461 setting->data.mux.group);
462
463 if (ret)
464 goto err_set_mux;
465
466 return 0;
467
468 err_set_mux:
469 for (i = 0; i < num_pins; i++) {
470 desc = pin_desc_get(pctldev, pins[i]);
471 if (desc)
472 desc->mux_setting = NULL;
473 }
474 err_pin_request:
475
476 while (--i >= 0)
477 pin_free(pctldev, pins[i], NULL);
478
479 return ret;
480 }
481
482 void pinmux_disable_setting(const struct pinctrl_setting *setting)
483 {
484 struct pinctrl_dev *pctldev = setting->pctldev;
485 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
486 int ret = 0;
487 const unsigned *pins = NULL;
488 unsigned num_pins = 0;
489 int i;
490 struct pin_desc *desc;
491
492 if (pctlops->get_group_pins)
493 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
494 &pins, &num_pins);
495 if (ret) {
496 const char *gname;
497
498
499 gname = pctlops->get_group_name(pctldev,
500 setting->data.mux.group);
501 dev_warn(pctldev->dev,
502 "could not get pins for group %s\n",
503 gname);
504 num_pins = 0;
505 }
506
507
508 for (i = 0; i < num_pins; i++) {
509 desc = pin_desc_get(pctldev, pins[i]);
510 if (desc == NULL) {
511 dev_warn(pctldev->dev,
512 "could not get pin desc for pin %d\n",
513 pins[i]);
514 continue;
515 }
516 if (desc->mux_setting == &(setting->data.mux)) {
517 pin_free(pctldev, pins[i], NULL);
518 } else {
519 const char *gname;
520
521 gname = pctlops->get_group_name(pctldev,
522 setting->data.mux.group);
523 dev_warn(pctldev->dev,
524 "not freeing pin %d (%s) as part of "
525 "deactivating group %s - it is already "
526 "used for some other setting",
527 pins[i], desc->name, gname);
528 }
529 }
530 }
531
532 #ifdef CONFIG_DEBUG_FS
533
534
535 static int pinmux_functions_show(struct seq_file *s, void *what)
536 {
537 struct pinctrl_dev *pctldev = s->private;
538 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
539 unsigned nfuncs;
540 unsigned func_selector = 0;
541
542 if (!pmxops)
543 return 0;
544
545 mutex_lock(&pctldev->mutex);
546 nfuncs = pmxops->get_functions_count(pctldev);
547 while (func_selector < nfuncs) {
548 const char *func = pmxops->get_function_name(pctldev,
549 func_selector);
550 const char * const *groups;
551 unsigned num_groups;
552 int ret;
553 int i;
554
555 ret = pmxops->get_function_groups(pctldev, func_selector,
556 &groups, &num_groups);
557 if (ret) {
558 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
559 func);
560 func_selector++;
561 continue;
562 }
563
564 seq_printf(s, "function: %s, groups = [ ", func);
565 for (i = 0; i < num_groups; i++)
566 seq_printf(s, "%s ", groups[i]);
567 seq_puts(s, "]\n");
568
569 func_selector++;
570 }
571
572 mutex_unlock(&pctldev->mutex);
573
574 return 0;
575 }
576
577 static int pinmux_pins_show(struct seq_file *s, void *what)
578 {
579 struct pinctrl_dev *pctldev = s->private;
580 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
581 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
582 unsigned i, pin;
583
584 if (!pmxops)
585 return 0;
586
587 seq_puts(s, "Pinmux settings per pin\n");
588 if (pmxops->strict)
589 seq_puts(s,
590 "Format: pin (name): mux_owner|gpio_owner (strict) hog?\n");
591 else
592 seq_puts(s,
593 "Format: pin (name): mux_owner gpio_owner hog?\n");
594
595 mutex_lock(&pctldev->mutex);
596
597
598 for (i = 0; i < pctldev->desc->npins; i++) {
599 struct pin_desc *desc;
600 bool is_hog = false;
601
602 pin = pctldev->desc->pins[i].number;
603 desc = pin_desc_get(pctldev, pin);
604
605 if (desc == NULL)
606 continue;
607
608 if (desc->mux_owner &&
609 !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
610 is_hog = true;
611
612 if (pmxops->strict) {
613 if (desc->mux_owner)
614 seq_printf(s, "pin %d (%s): device %s%s",
615 pin, desc->name, desc->mux_owner,
616 is_hog ? " (HOG)" : "");
617 else if (desc->gpio_owner)
618 seq_printf(s, "pin %d (%s): GPIO %s",
619 pin, desc->name, desc->gpio_owner);
620 else
621 seq_printf(s, "pin %d (%s): UNCLAIMED",
622 pin, desc->name);
623 } else {
624
625 seq_printf(s, "pin %d (%s): %s %s%s", pin, desc->name,
626 desc->mux_owner ? desc->mux_owner
627 : "(MUX UNCLAIMED)",
628 desc->gpio_owner ? desc->gpio_owner
629 : "(GPIO UNCLAIMED)",
630 is_hog ? " (HOG)" : "");
631 }
632
633
634 if (desc->mux_setting)
635 seq_printf(s, " function %s group %s\n",
636 pmxops->get_function_name(pctldev,
637 desc->mux_setting->func),
638 pctlops->get_group_name(pctldev,
639 desc->mux_setting->group));
640 else
641 seq_putc(s, '\n');
642 }
643
644 mutex_unlock(&pctldev->mutex);
645
646 return 0;
647 }
648
649 void pinmux_show_map(struct seq_file *s, const struct pinctrl_map *map)
650 {
651 seq_printf(s, "group %s\nfunction %s\n",
652 map->data.mux.group ? map->data.mux.group : "(default)",
653 map->data.mux.function);
654 }
655
656 void pinmux_show_setting(struct seq_file *s,
657 const struct pinctrl_setting *setting)
658 {
659 struct pinctrl_dev *pctldev = setting->pctldev;
660 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
661 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
662
663 seq_printf(s, "group: %s (%u) function: %s (%u)\n",
664 pctlops->get_group_name(pctldev, setting->data.mux.group),
665 setting->data.mux.group,
666 pmxops->get_function_name(pctldev, setting->data.mux.func),
667 setting->data.mux.func);
668 }
669
670 DEFINE_SHOW_ATTRIBUTE(pinmux_functions);
671 DEFINE_SHOW_ATTRIBUTE(pinmux_pins);
672
673 void pinmux_init_device_debugfs(struct dentry *devroot,
674 struct pinctrl_dev *pctldev)
675 {
676 debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
677 devroot, pctldev, &pinmux_functions_fops);
678 debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
679 devroot, pctldev, &pinmux_pins_fops);
680 }
681
682 #endif
683
684 #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
685
686
687
688
689
690 int pinmux_generic_get_function_count(struct pinctrl_dev *pctldev)
691 {
692 return pctldev->num_functions;
693 }
694 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_count);
695
696
697
698
699
700
701 const char *
702 pinmux_generic_get_function_name(struct pinctrl_dev *pctldev,
703 unsigned int selector)
704 {
705 struct function_desc *function;
706
707 function = radix_tree_lookup(&pctldev->pin_function_tree,
708 selector);
709 if (!function)
710 return NULL;
711
712 return function->name;
713 }
714 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_name);
715
716
717
718
719
720
721
722
723 int pinmux_generic_get_function_groups(struct pinctrl_dev *pctldev,
724 unsigned int selector,
725 const char * const **groups,
726 unsigned * const num_groups)
727 {
728 struct function_desc *function;
729
730 function = radix_tree_lookup(&pctldev->pin_function_tree,
731 selector);
732 if (!function) {
733 dev_err(pctldev->dev, "%s could not find function%i\n",
734 __func__, selector);
735 return -EINVAL;
736 }
737 *groups = function->group_names;
738 *num_groups = function->num_group_names;
739
740 return 0;
741 }
742 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_groups);
743
744
745
746
747
748
749 struct function_desc *pinmux_generic_get_function(struct pinctrl_dev *pctldev,
750 unsigned int selector)
751 {
752 struct function_desc *function;
753
754 function = radix_tree_lookup(&pctldev->pin_function_tree,
755 selector);
756 if (!function)
757 return NULL;
758
759 return function;
760 }
761 EXPORT_SYMBOL_GPL(pinmux_generic_get_function);
762
763
764
765
766
767
768
769
770
771 int pinmux_generic_add_function(struct pinctrl_dev *pctldev,
772 const char *name,
773 const char **groups,
774 const unsigned int num_groups,
775 void *data)
776 {
777 struct function_desc *function;
778 int selector;
779
780 if (!name)
781 return -EINVAL;
782
783 selector = pinmux_func_name_to_selector(pctldev, name);
784 if (selector >= 0)
785 return selector;
786
787 selector = pctldev->num_functions;
788
789 function = devm_kzalloc(pctldev->dev, sizeof(*function), GFP_KERNEL);
790 if (!function)
791 return -ENOMEM;
792
793 function->name = name;
794 function->group_names = groups;
795 function->num_group_names = num_groups;
796 function->data = data;
797
798 radix_tree_insert(&pctldev->pin_function_tree, selector, function);
799
800 pctldev->num_functions++;
801
802 return selector;
803 }
804 EXPORT_SYMBOL_GPL(pinmux_generic_add_function);
805
806
807
808
809
810
811
812
813 int pinmux_generic_remove_function(struct pinctrl_dev *pctldev,
814 unsigned int selector)
815 {
816 struct function_desc *function;
817
818 function = radix_tree_lookup(&pctldev->pin_function_tree,
819 selector);
820 if (!function)
821 return -ENOENT;
822
823 radix_tree_delete(&pctldev->pin_function_tree, selector);
824 devm_kfree(pctldev->dev, function);
825
826 pctldev->num_functions--;
827
828 return 0;
829 }
830 EXPORT_SYMBOL_GPL(pinmux_generic_remove_function);
831
832
833
834
835
836
837
838
839
840 void pinmux_generic_free_functions(struct pinctrl_dev *pctldev)
841 {
842 struct radix_tree_iter iter;
843 void __rcu **slot;
844
845 radix_tree_for_each_slot(slot, &pctldev->pin_function_tree, &iter, 0)
846 radix_tree_delete(&pctldev->pin_function_tree, iter.index);
847
848 pctldev->num_functions = 0;
849 }
850
851 #endif