Merge tag 'arm-soc-5.7' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[linux-2.6-microblaze.git] / drivers / pinctrl / pinmux.c
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/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         /* Check that we implement required operations */
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         /* Check that all functions registered have names */
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  * pinmux_can_be_used_for_gpio() - check if a specific pin
75  *      is either muxed to a different function or used as gpio.
76  *
77  * @pin: the pin number in the global pin space
78  *
79  * Controllers not defined as strict will always return true,
80  * menaning that the gpio can be used.
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         /* Can't inspect pin, assume it can be used */
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  * pin_request() - request a single pin to be muxed in, typically for GPIO
99  * @pin: the pin number in the global pin space
100  * @owner: a representation of the owner of this pin; typically the device
101  *      name that controls its mux function, or the requested GPIO name
102  * @gpio_range: the range matching the GPIO pin if this is a request for a
103  *      single GPIO pin
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         /* Let each pin increase references to this module */
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          * If there is no kind of request function for the pin we just assume
160          * we got it by default and proceed.
161          */
162         if (gpio_range && ops->gpio_request_enable)
163                 /* This requests and enables a single GPIO pin */
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  * pin_free() - release a single muxed in pin so something else can be muxed
195  * @pctldev: pin controller device handling this pin
196  * @pin: the pin to free
197  * @gpio_range: the range matching the GPIO pin if this is a request for a
198  *      single GPIO pin
199  *
200  * This function returns a pointer to the previous owner. This is used
201  * for callers that dynamically allocate an owner name so it can be freed
202  * once the pin is free. This is done for GPIO request functions.
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                  * A pin should not be freed more times than allocated.
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          * If there is no kind of request function for the pin we just assume
231          * we got it by default and proceed.
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  * pinmux_request_gpio() - request pinmuxing for a GPIO pin
254  * @pctldev: pin controller device affected
255  * @pin: the pin to mux in for GPIO
256  * @range: the applicable GPIO range
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         /* Conjure some name stating what chip and pin this is taken by */
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  * pinmux_free_gpio() - release a pin from GPIO muxing
279  * @pctldev: the pin controller device for the pin
280  * @pin: the affected currently GPIO-muxed in pin
281  * @range: applicable GPIO range
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  * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
294  * @pctldev: the pin controller handling this pin
295  * @range: applicable GPIO range
296  * @pin: the affected GPIO pin in this controller
297  * @input: true if we set the pin as input, false for output
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         /* See if this pctldev has this function */
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         /* This function is currently unused */
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                 /* errors only affect debug data, so just warn */
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         /* Try to allocate all pins in this group, one by one */
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         /* Now that we have acquired the pins, encode the mux setting */
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         /* On error release all taken pins */
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                 /* errors only affect debug data, so just warn */
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         /* Flag the descs that no setting is active */
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 /* Called from pincontrol core */
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         /* The pin number can be retrived from the pin controller descriptor */
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                 /* Skip if we cannot search the pin */
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                         /* For non-strict controllers */
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                 /* If mux: print function+group claiming the pin */
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 /* CONFIG_DEBUG_FS */
683
684 #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
685
686 /**
687  * pinmux_generic_get_function_count() - returns number of functions
688  * @pctldev: pin controller device
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  * pinmux_generic_get_function_name() - returns the function name
698  * @pctldev: pin controller device
699  * @selector: function number
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  * pinmux_generic_get_function_groups() - gets the function groups
718  * @pctldev: pin controller device
719  * @selector: function number
720  * @groups: array of pin groups
721  * @num_groups: number of pin groups
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  * pinmux_generic_get_function() - returns a function based on the number
746  * @pctldev: pin controller device
747  * @group_selector: function number
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  * pinmux_generic_add_function() - adds a function group
765  * @pctldev: pin controller device
766  * @name: name of the function
767  * @groups: array of pin groups
768  * @num_groups: number of pin groups
769  * @data: pin controller driver specific data
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  * pinmux_generic_remove_function() - removes a numbered function
808  * @pctldev: pin controller device
809  * @selector: function number
810  *
811  * Note that the caller must take care of locking.
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  * pinmux_generic_free_functions() - removes all functions
834  * @pctldev: pin controller device
835  *
836  * Note that the caller must take care of locking. The pinctrl
837  * functions are allocated with devm_kzalloc() so no need to free
838  * them here.
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 /* CONFIG_GENERIC_PINMUX_FUNCTIONS */