pinctrl: unbreak error messages
[linux-2.6-block.git] / drivers / pinctrl / pinmux.c
1 /*
2  * Core driver for the pin muxing portions of the pin control subsystem
3  *
4  * Copyright (C) 2011 ST-Ericsson SA
5  * Written on behalf of Linaro for ST-Ericsson
6  * Based on bits of regulator core, gpio core and clk core
7  *
8  * Author: Linus Walleij <linus.walleij@linaro.org>
9  *
10  * License terms: GNU General Public License (GPL) version 2
11  */
12 #define pr_fmt(fmt) "pinmux core: " fmt
13
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/device.h>
18 #include <linux/slab.h>
19 #include <linux/radix-tree.h>
20 #include <linux/err.h>
21 #include <linux/list.h>
22 #include <linux/mutex.h>
23 #include <linux/spinlock.h>
24 #include <linux/string.h>
25 #include <linux/sysfs.h>
26 #include <linux/debugfs.h>
27 #include <linux/seq_file.h>
28 #include <linux/pinctrl/machine.h>
29 #include <linux/pinctrl/pinmux.h>
30 #include "core.h"
31
32 /* List of pinmuxes */
33 static DEFINE_MUTEX(pinmux_list_mutex);
34 static LIST_HEAD(pinmux_list);
35
36 /* Global pinmux maps */
37 static struct pinmux_map *pinmux_maps;
38 static unsigned pinmux_maps_num;
39
40 /**
41  * struct pinmux_group - group list item for pinmux groups
42  * @node: pinmux group list node
43  * @group_selector: the group selector for this group
44  */
45 struct pinmux_group {
46         struct list_head node;
47         unsigned group_selector;
48 };
49
50 /**
51  * struct pinmux - per-device pinmux state holder
52  * @node: global list node
53  * @dev: the device using this pinmux
54  * @usecount: the number of active users of this mux setting, used to keep
55  *      track of nested use cases
56  * @pins: an array of discrete physical pins used in this mapping, taken
57  *      from the global pin enumeration space (copied from pinmux map)
58  * @num_pins: the number of pins in this mapping array, i.e. the number of
59  *      elements in .pins so we can iterate over that array (copied from
60  *      pinmux map)
61  * @pctldev: pin control device handling this pinmux
62  * @func_selector: the function selector for the pinmux device handling
63  *      this pinmux
64  * @groups: the group selectors for the pinmux device and
65  *      selector combination handling this pinmux, this is a list that
66  *      will be traversed on all pinmux operations such as
67  *      get/put/enable/disable
68  * @mutex: a lock for the pinmux state holder
69  */
70 struct pinmux {
71         struct list_head node;
72         struct device *dev;
73         unsigned usecount;
74         struct pinctrl_dev *pctldev;
75         unsigned func_selector;
76         struct list_head groups;
77         struct mutex mutex;
78 };
79
80 /**
81  * struct pinmux_hog - a list item to stash mux hogs
82  * @node: pinmux hog list node
83  * @map: map entry responsible for this hogging
84  * @pmx: the pinmux hogged by this item
85  */
86 struct pinmux_hog {
87         struct list_head node;
88         struct pinmux_map const *map;
89         struct pinmux *pmx;
90 };
91
92 /**
93  * pin_request() - request a single pin to be muxed in, typically for GPIO
94  * @pin: the pin number in the global pin space
95  * @function: a functional name to give to this pin, passed to the driver
96  *      so it knows what function to mux in, e.g. the string "gpioNN"
97  *      means that you want to mux in the pin for use as GPIO number NN
98  * @gpio_range: the range matching the GPIO pin if this is a request for a
99  *      single GPIO pin
100  */
101 static int pin_request(struct pinctrl_dev *pctldev,
102                        int pin, const char *function,
103                        struct pinctrl_gpio_range *gpio_range)
104 {
105         struct pin_desc *desc;
106         const struct pinmux_ops *ops = pctldev->desc->pmxops;
107         int status = -EINVAL;
108
109         dev_dbg(pctldev->dev, "request pin %d for %s\n", pin, function);
110
111         desc = pin_desc_get(pctldev, pin);
112         if (desc == NULL) {
113                 dev_err(pctldev->dev,
114                         "pin is not registered so it cannot be requested\n");
115                 goto out;
116         }
117
118         if (!function) {
119                 dev_err(pctldev->dev, "no function name given\n");
120                 return -EINVAL;
121         }
122
123         spin_lock(&desc->lock);
124         if (desc->mux_function) {
125                 spin_unlock(&desc->lock);
126                 dev_err(pctldev->dev,
127                         "pin already requested\n");
128                 goto out;
129         }
130         desc->mux_function = function;
131         spin_unlock(&desc->lock);
132
133         /* Let each pin increase references to this module */
134         if (!try_module_get(pctldev->owner)) {
135                 dev_err(pctldev->dev,
136                         "could not increase module refcount for pin %d\n",
137                         pin);
138                 status = -EINVAL;
139                 goto out_free_pin;
140         }
141
142         /*
143          * If there is no kind of request function for the pin we just assume
144          * we got it by default and proceed.
145          */
146         if (gpio_range && ops->gpio_request_enable)
147                 /* This requests and enables a single GPIO pin */
148                 status = ops->gpio_request_enable(pctldev, gpio_range, pin);
149         else if (ops->request)
150                 status = ops->request(pctldev, pin);
151         else
152                 status = 0;
153
154         if (status)
155                 dev_err(pctldev->dev, "->request on device %s failed for pin %d\n",
156                        pctldev->desc->name, pin);
157 out_free_pin:
158         if (status) {
159                 spin_lock(&desc->lock);
160                 desc->mux_function = NULL;
161                 spin_unlock(&desc->lock);
162         }
163 out:
164         if (status)
165                 dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
166                        pin, function ? : "?", status);
167
168         return status;
169 }
170
171 /**
172  * pin_free() - release a single muxed in pin so something else can be muxed
173  * @pctldev: pin controller device handling this pin
174  * @pin: the pin to free
175  * @gpio_range: the range matching the GPIO pin if this is a request for a
176  *      single GPIO pin
177  *
178  * This function returns a pointer to the function name in use. This is used
179  * for callers that dynamically allocate a function name so it can be freed
180  * once the pin is free. This is done for GPIO request functions.
181  */
182 static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
183                             struct pinctrl_gpio_range *gpio_range)
184 {
185         const struct pinmux_ops *ops = pctldev->desc->pmxops;
186         struct pin_desc *desc;
187         const char *func;
188
189         desc = pin_desc_get(pctldev, pin);
190         if (desc == NULL) {
191                 dev_err(pctldev->dev,
192                         "pin is not registered so it cannot be freed\n");
193                 return NULL;
194         }
195
196         /*
197          * If there is no kind of request function for the pin we just assume
198          * we got it by default and proceed.
199          */
200         if (gpio_range && ops->gpio_disable_free)
201                 ops->gpio_disable_free(pctldev, gpio_range, pin);
202         else if (ops->free)
203                 ops->free(pctldev, pin);
204
205         spin_lock(&desc->lock);
206         func = desc->mux_function;
207         desc->mux_function = NULL;
208         spin_unlock(&desc->lock);
209         module_put(pctldev->owner);
210
211         return func;
212 }
213
214 /**
215  * pinmux_request_gpio() - request a single pin to be muxed in as GPIO
216  * @gpio: the GPIO pin number from the GPIO subsystem number space
217  *
218  * This function should *ONLY* be used from gpiolib-based GPIO drivers,
219  * as part of their gpio_request() semantics, platforms and individual drivers
220  * shall *NOT* request GPIO pins to be muxed in.
221  */
222 int pinmux_request_gpio(unsigned gpio)
223 {
224         char gpiostr[16];
225         const char *function;
226         struct pinctrl_dev *pctldev;
227         struct pinctrl_gpio_range *range;
228         int ret;
229         int pin;
230
231         ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
232         if (ret)
233                 return -EINVAL;
234
235         /* Convert to the pin controllers number space */
236         pin = gpio - range->base + range->pin_base;
237
238         /* Conjure some name stating what chip and pin this is taken by */
239         snprintf(gpiostr, 15, "%s:%d", range->name, gpio);
240
241         function = kstrdup(gpiostr, GFP_KERNEL);
242         if (!function)
243                 return -EINVAL;
244
245         ret = pin_request(pctldev, pin, function, range);
246         if (ret < 0)
247                 kfree(function);
248
249         return ret;
250 }
251 EXPORT_SYMBOL_GPL(pinmux_request_gpio);
252
253 /**
254  * pinmux_free_gpio() - free a single pin, currently used as GPIO
255  * @gpio: the GPIO pin number from the GPIO subsystem number space
256  *
257  * This function should *ONLY* be used from gpiolib-based GPIO drivers,
258  * as part of their gpio_free() semantics, platforms and individual drivers
259  * shall *NOT* request GPIO pins to be muxed out.
260  */
261 void pinmux_free_gpio(unsigned gpio)
262 {
263         struct pinctrl_dev *pctldev;
264         struct pinctrl_gpio_range *range;
265         int ret;
266         int pin;
267         const char *func;
268
269         ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
270         if (ret)
271                 return;
272
273         /* Convert to the pin controllers number space */
274         pin = gpio - range->base + range->pin_base;
275
276         func = pin_free(pctldev, pin, range);
277         kfree(func);
278 }
279 EXPORT_SYMBOL_GPL(pinmux_free_gpio);
280
281 static int pinmux_gpio_direction(unsigned gpio, bool input)
282 {
283         struct pinctrl_dev *pctldev;
284         struct pinctrl_gpio_range *range;
285         const struct pinmux_ops *ops;
286         int ret;
287         int pin;
288
289         ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
290         if (ret)
291                 return ret;
292
293         ops = pctldev->desc->pmxops;
294
295         /* Convert to the pin controllers number space */
296         pin = gpio - range->base + range->pin_base;
297
298         if (ops->gpio_set_direction)
299                 ret = ops->gpio_set_direction(pctldev, range, pin, input);
300         else
301                 ret = 0;
302
303         return ret;
304 }
305
306 /**
307  * pinmux_gpio_direction_input() - request a GPIO pin to go into input mode
308  * @gpio: the GPIO pin number from the GPIO subsystem number space
309  *
310  * This function should *ONLY* be used from gpiolib-based GPIO drivers,
311  * as part of their gpio_direction_input() semantics, platforms and individual
312  * drivers shall *NOT* touch pinmux GPIO calls.
313  */
314 int pinmux_gpio_direction_input(unsigned gpio)
315 {
316         return pinmux_gpio_direction(gpio, true);
317 }
318 EXPORT_SYMBOL_GPL(pinmux_gpio_direction_input);
319
320 /**
321  * pinmux_gpio_direction_output() - request a GPIO pin to go into output mode
322  * @gpio: the GPIO pin number from the GPIO subsystem number space
323  *
324  * This function should *ONLY* be used from gpiolib-based GPIO drivers,
325  * as part of their gpio_direction_output() semantics, platforms and individual
326  * drivers shall *NOT* touch pinmux GPIO calls.
327  */
328 int pinmux_gpio_direction_output(unsigned gpio)
329 {
330         return pinmux_gpio_direction(gpio, false);
331 }
332 EXPORT_SYMBOL_GPL(pinmux_gpio_direction_output);
333
334 /**
335  * pinmux_register_mappings() - register a set of pinmux mappings
336  * @maps: the pinmux mappings table to register, this should be marked with
337  *      __initdata so it can be discarded after boot, this function will
338  *      perform a shallow copy for the mapping entries.
339  * @num_maps: the number of maps in the mapping table
340  *
341  * Only call this once during initialization of your machine, the function is
342  * tagged as __init and won't be callable after init has completed. The map
343  * passed into this function will be owned by the pinmux core and cannot be
344  * freed.
345  */
346 int __init pinmux_register_mappings(struct pinmux_map const *maps,
347                                     unsigned num_maps)
348 {
349         void *tmp_maps;
350         int i;
351
352         pr_debug("add %d pinmux maps\n", num_maps);
353
354         /* First sanity check the new mapping */
355         for (i = 0; i < num_maps; i++) {
356                 if (!maps[i].name) {
357                         pr_err("failed to register map %d: no map name given\n",
358                                         i);
359                         return -EINVAL;
360                 }
361
362                 if (!maps[i].ctrl_dev && !maps[i].ctrl_dev_name) {
363                         pr_err("failed to register map %s (%d): no pin control device given\n",
364                                maps[i].name, i);
365                         return -EINVAL;
366                 }
367
368                 if (!maps[i].function) {
369                         pr_err("failed to register map %s (%d): no function ID given\n",
370                                         maps[i].name, i);
371                         return -EINVAL;
372                 }
373
374                 if (!maps[i].dev && !maps[i].dev_name)
375                         pr_debug("add system map %s function %s with no device\n",
376                                  maps[i].name,
377                                  maps[i].function);
378                 else
379                         pr_debug("register map %s, function %s\n",
380                                  maps[i].name,
381                                  maps[i].function);
382         }
383
384         /*
385          * Make a copy of the map array - string pointers will end up in the
386          * kernel const section anyway so these do not need to be deep copied.
387          */
388         if (!pinmux_maps_num) {
389                 /* On first call, just copy them */
390                 tmp_maps = kmemdup(maps,
391                                    sizeof(struct pinmux_map) * num_maps,
392                                    GFP_KERNEL);
393                 if (!tmp_maps)
394                         return -ENOMEM;
395         } else {
396                 /* Subsequent calls, reallocate array to new size */
397                 size_t oldsize = sizeof(struct pinmux_map) * pinmux_maps_num;
398                 size_t newsize = sizeof(struct pinmux_map) * num_maps;
399
400                 tmp_maps = krealloc(pinmux_maps, oldsize + newsize, GFP_KERNEL);
401                 if (!tmp_maps)
402                         return -ENOMEM;
403                 memcpy((tmp_maps + oldsize), maps, newsize);
404         }
405
406         pinmux_maps = tmp_maps;
407         pinmux_maps_num += num_maps;
408         return 0;
409 }
410
411 /**
412  * acquire_pins() - acquire all the pins for a certain funcion on a pinmux
413  * @pctldev: the device to take the pins on
414  * @func_selector: the function selector to acquire the pins for
415  * @group_selector: the group selector containing the pins to acquire
416  */
417 static int acquire_pins(struct pinctrl_dev *pctldev,
418                         unsigned func_selector,
419                         unsigned group_selector)
420 {
421         const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
422         const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
423         const char *func = pmxops->get_function_name(pctldev,
424                                                      func_selector);
425         const unsigned *pins;
426         unsigned num_pins;
427         int ret;
428         int i;
429
430         ret = pctlops->get_group_pins(pctldev, group_selector,
431                                       &pins, &num_pins);
432         if (ret)
433                 return ret;
434
435         dev_dbg(pctldev->dev, "requesting the %u pins from group %u\n",
436                 num_pins, group_selector);
437
438         /* Try to allocate all pins in this group, one by one */
439         for (i = 0; i < num_pins; i++) {
440                 ret = pin_request(pctldev, pins[i], func, NULL);
441                 if (ret) {
442                         dev_err(pctldev->dev,
443                                 "could not get pin %d for function %s on device %s - conflicting mux mappings?\n",
444                                 pins[i], func ? : "(undefined)",
445                                 pinctrl_dev_get_name(pctldev));
446                         /* On error release all taken pins */
447                         i--; /* this pin just failed */
448                         for (; i >= 0; i--)
449                                 pin_free(pctldev, pins[i], NULL);
450                         return -ENODEV;
451                 }
452         }
453         return 0;
454 }
455
456 /**
457  * release_pins() - release pins taken by earlier acquirement
458  * @pctldev: the device to free the pinx on
459  * @group_selector: the group selector containing the pins to free
460  */
461 static void release_pins(struct pinctrl_dev *pctldev,
462                          unsigned group_selector)
463 {
464         const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
465         const unsigned *pins;
466         unsigned num_pins;
467         int ret;
468         int i;
469
470         ret = pctlops->get_group_pins(pctldev, group_selector,
471                                       &pins, &num_pins);
472         if (ret) {
473                 dev_err(pctldev->dev, "could not get pins to release for group selector %d\n",
474                         group_selector);
475                 return;
476         }
477         for (i = 0; i < num_pins; i++)
478                 pin_free(pctldev, pins[i], NULL);
479 }
480
481 /**
482  * pinmux_check_pin_group() - check function and pin group combo
483  * @pctldev: device to check the pin group vs function for
484  * @func_selector: the function selector to check the pin group for, we have
485  *      already looked this up in the calling function
486  * @pin_group: the pin group to match to the function
487  *
488  * This function will check that the pinmux driver can supply the
489  * selected pin group for a certain function, returns the group selector if
490  * the group and function selector will work fine together, else returns
491  * negative
492  */
493 static int pinmux_check_pin_group(struct pinctrl_dev *pctldev,
494                                   unsigned func_selector,
495                                   const char *pin_group)
496 {
497         const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
498         const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
499         int ret;
500
501         /*
502          * If the driver does not support different pin groups for the
503          * functions, we only support group 0, and assume this exists.
504          */
505         if (!pctlops || !pctlops->list_groups)
506                 return 0;
507
508         /*
509          * Passing NULL (no specific group) will select the first and
510          * hopefully only group of pins available for this function.
511          */
512         if (!pin_group) {
513                 char const * const *groups;
514                 unsigned num_groups;
515
516                 ret = pmxops->get_function_groups(pctldev, func_selector,
517                                                   &groups, &num_groups);
518                 if (ret)
519                         return ret;
520                 if (num_groups < 1)
521                         return -EINVAL;
522                 ret = pinctrl_get_group_selector(pctldev, groups[0]);
523                 if (ret < 0) {
524                         dev_err(pctldev->dev,
525                                 "function %s wants group %s but the pin controller does not seem to have that group\n",
526                                 pmxops->get_function_name(pctldev, func_selector),
527                                 groups[0]);
528                         return ret;
529                 }
530
531                 if (num_groups > 1)
532                         dev_dbg(pctldev->dev,
533                                 "function %s support more than one group, default-selecting first group %s (%d)\n",
534                                 pmxops->get_function_name(pctldev, func_selector),
535                                 groups[0],
536                                 ret);
537
538                 return ret;
539         }
540
541         dev_dbg(pctldev->dev,
542                 "check if we have pin group %s on controller %s\n",
543                 pin_group, pinctrl_dev_get_name(pctldev));
544
545         ret = pinctrl_get_group_selector(pctldev, pin_group);
546         if (ret < 0) {
547                 dev_dbg(pctldev->dev,
548                         "%s does not support pin group %s with function %s\n",
549                         pinctrl_dev_get_name(pctldev),
550                         pin_group,
551                         pmxops->get_function_name(pctldev, func_selector));
552         }
553         return ret;
554 }
555
556 /**
557  * pinmux_search_function() - check pin control driver for a certain function
558  * @pctldev: device to check for function and position
559  * @map: function map containing the function and position to look for
560  * @func_selector: returns the applicable function selector if found
561  * @group_selector: returns the applicable group selector if found
562  *
563  * This will search the pinmux driver for an applicable
564  * function with a specific pin group, returns 0 if these can be mapped
565  * negative otherwise
566  */
567 static int pinmux_search_function(struct pinctrl_dev *pctldev,
568                                   struct pinmux_map const *map,
569                                   unsigned *func_selector,
570                                   unsigned *group_selector)
571 {
572         const struct pinmux_ops *ops = pctldev->desc->pmxops;
573         unsigned selector = 0;
574
575         /* See if this pctldev has this function */
576         while (ops->list_functions(pctldev, selector) >= 0) {
577                 const char *fname = ops->get_function_name(pctldev,
578                                                            selector);
579                 int ret;
580
581                 if (!strcmp(map->function, fname)) {
582                         /* Found the function, check pin group */
583                         ret = pinmux_check_pin_group(pctldev, selector,
584                                                      map->group);
585                         if (ret < 0)
586                                 return ret;
587
588                         /* This function and group selector can be used */
589                         *func_selector = selector;
590                         *group_selector = ret;
591                         return 0;
592
593                 }
594                 selector++;
595         }
596
597         pr_err("%s does not support function %s\n",
598                pinctrl_dev_get_name(pctldev), map->function);
599         return -EINVAL;
600 }
601
602 /**
603  * pinmux_enable_muxmap() - enable a map entry for a certain pinmux
604  */
605 static int pinmux_enable_muxmap(struct pinctrl_dev *pctldev,
606                                 struct pinmux *pmx,
607                                 struct device *dev,
608                                 const char *devname,
609                                 struct pinmux_map const *map)
610 {
611         unsigned func_selector;
612         unsigned group_selector;
613         struct pinmux_group *grp;
614         int ret;
615
616         /*
617          * Note that we're not locking the pinmux mutex here, because
618          * this is only called at pinmux initialization time when it
619          * has not been added to any list and thus is not reachable
620          * by anyone else.
621          */
622
623         if (pmx->pctldev && pmx->pctldev != pctldev) {
624                 dev_err(pctldev->dev,
625                         "different pin control devices given for device %s, function %s\n",
626                         devname, map->function);
627                 return -EINVAL;
628         }
629         pmx->dev = dev;
630         pmx->pctldev = pctldev;
631
632         /* Now go into the driver and try to match a function and group */
633         ret = pinmux_search_function(pctldev, map, &func_selector,
634                                      &group_selector);
635         if (ret < 0)
636                 return ret;
637
638         /*
639          * If the function selector is already set, it needs to be identical,
640          * we support several groups with one function but not several
641          * functions with one or several groups in the same pinmux.
642          */
643         if (pmx->func_selector != UINT_MAX &&
644             pmx->func_selector != func_selector) {
645                 dev_err(pctldev->dev,
646                         "dual function defines in the map for device %s\n",
647                        devname);
648                 return -EINVAL;
649         }
650         pmx->func_selector = func_selector;
651
652         /* Now add this group selector, we may have many of them */
653         grp = kmalloc(sizeof(struct pinmux_group), GFP_KERNEL);
654         if (!grp)
655                 return -ENOMEM;
656         grp->group_selector = group_selector;
657         ret = acquire_pins(pctldev, func_selector, group_selector);
658         if (ret) {
659                 kfree(grp);
660                 return ret;
661         }
662         list_add(&grp->node, &pmx->groups);
663
664         return 0;
665 }
666
667 static void pinmux_free_groups(struct pinmux *pmx)
668 {
669         struct list_head *node, *tmp;
670
671         list_for_each_safe(node, tmp, &pmx->groups) {
672                 struct pinmux_group *grp =
673                         list_entry(node, struct pinmux_group, node);
674                 /* Release all pins taken by this group */
675                 release_pins(pmx->pctldev, grp->group_selector);
676                 list_del(node);
677                 kfree(grp);
678         }
679 }
680
681 /**
682  * pinmux_get() - retrieves the pinmux for a certain device
683  * @dev: the device to get the pinmux for
684  * @name: an optional specific mux mapping name or NULL, the name is only
685  *      needed if you want to have more than one mapping per device, or if you
686  *      need an anonymous pinmux (not tied to any specific device)
687  */
688 struct pinmux *pinmux_get(struct device *dev, const char *name)
689 {
690         struct pinmux_map const *map = NULL;
691         struct pinctrl_dev *pctldev = NULL;
692         const char *devname = NULL;
693         struct pinmux *pmx;
694         bool found_map;
695         unsigned num_maps = 0;
696         int ret = -ENODEV;
697         int i;
698
699         /* We must have dev or ID or both */
700         if (!dev && !name)
701                 return ERR_PTR(-EINVAL);
702
703         if (dev)
704                 devname = dev_name(dev);
705
706         pr_debug("get mux %s for device %s\n", name,
707                  devname ? devname : "(none)");
708
709         /*
710          * create the state cookie holder struct pinmux for each
711          * mapping, this is what consumers will get when requesting
712          * a pinmux handle with pinmux_get()
713          */
714         pmx = kzalloc(sizeof(struct pinmux), GFP_KERNEL);
715         if (pmx == NULL)
716                 return ERR_PTR(-ENOMEM);
717         mutex_init(&pmx->mutex);
718         pmx->func_selector = UINT_MAX;
719         INIT_LIST_HEAD(&pmx->groups);
720
721         /* Iterate over the pinmux maps to locate the right ones */
722         for (i = 0; i < pinmux_maps_num; i++) {
723                 map = &pinmux_maps[i];
724                 found_map = false;
725
726                 /*
727                  * First, try to find the pctldev given in the map
728                  */
729                 pctldev = get_pinctrl_dev_from_dev(map->ctrl_dev,
730                                                    map->ctrl_dev_name);
731                 if (!pctldev) {
732                         const char *devname = NULL;
733
734                         if (map->ctrl_dev)
735                                 devname = dev_name(map->ctrl_dev);
736                         else if (map->ctrl_dev_name)
737                                 devname = map->ctrl_dev_name;
738
739                         pr_warning("could not find a pinctrl device for pinmux function %s, fishy, they shall all have one\n",
740                                    map->function);
741                         pr_warning("given pinctrl device name: %s",
742                                    devname ? devname : "UNDEFINED");
743
744                         /* Continue to check the other mappings anyway... */
745                         continue;
746                 }
747
748                 pr_debug("in map, found pctldev %s to handle function %s",
749                          dev_name(pctldev->dev), map->function);
750
751
752                 /*
753                  * If we're looking for a specific named map, this must match,
754                  * else we loop and look for the next.
755                  */
756                 if (name != NULL) {
757                         if (map->name == NULL)
758                                 continue;
759                         if (strcmp(map->name, name))
760                                 continue;
761                 }
762
763                 /*
764                  * This is for the case where no device name is given, we
765                  * already know that the function name matches from above
766                  * code.
767                  */
768                 if (!map->dev_name && (name != NULL))
769                         found_map = true;
770
771                 /* If the mapping has a device set up it must match */
772                 if (map->dev_name &&
773                     (!devname || !strcmp(map->dev_name, devname)))
774                         /* MATCH! */
775                         found_map = true;
776
777                 /* If this map is applicable, then apply it */
778                 if (found_map) {
779                         ret = pinmux_enable_muxmap(pctldev, pmx, dev,
780                                                    devname, map);
781                         if (ret) {
782                                 pinmux_free_groups(pmx);
783                                 kfree(pmx);
784                                 return ERR_PTR(ret);
785                         }
786                         num_maps++;
787                 }
788         }
789
790
791         /* We should have atleast one map, right */
792         if (!num_maps) {
793                 pr_err("could not find any mux maps for device %s, ID %s\n",
794                        devname ? devname : "(anonymous)",
795                        name ? name : "(undefined)");
796                 kfree(pmx);
797                 return ERR_PTR(-EINVAL);
798         }
799
800         pr_debug("found %u mux maps for device %s, UD %s\n",
801                  num_maps,
802                  devname ? devname : "(anonymous)",
803                  name ? name : "(undefined)");
804
805         /* Add the pinmux to the global list */
806         mutex_lock(&pinmux_list_mutex);
807         list_add(&pmx->node, &pinmux_list);
808         mutex_unlock(&pinmux_list_mutex);
809
810         return pmx;
811 }
812 EXPORT_SYMBOL_GPL(pinmux_get);
813
814 /**
815  * pinmux_put() - release a previously claimed pinmux
816  * @pmx: a pinmux previously claimed by pinmux_get()
817  */
818 void pinmux_put(struct pinmux *pmx)
819 {
820         if (pmx == NULL)
821                 return;
822
823         mutex_lock(&pmx->mutex);
824         if (pmx->usecount)
825                 pr_warn("releasing pinmux with active users!\n");
826         /* Free the groups and all acquired pins */
827         pinmux_free_groups(pmx);
828         mutex_unlock(&pmx->mutex);
829
830         /* Remove from list */
831         mutex_lock(&pinmux_list_mutex);
832         list_del(&pmx->node);
833         mutex_unlock(&pinmux_list_mutex);
834
835         kfree(pmx);
836 }
837 EXPORT_SYMBOL_GPL(pinmux_put);
838
839 /**
840  * pinmux_enable() - enable a certain pinmux setting
841  * @pmx: the pinmux to enable, previously claimed by pinmux_get()
842  */
843 int pinmux_enable(struct pinmux *pmx)
844 {
845         int ret = 0;
846
847         if (pmx == NULL)
848                 return -EINVAL;
849         mutex_lock(&pmx->mutex);
850         if (pmx->usecount++ == 0) {
851                 struct pinctrl_dev *pctldev = pmx->pctldev;
852                 const struct pinmux_ops *ops = pctldev->desc->pmxops;
853                 struct pinmux_group *grp;
854
855                 list_for_each_entry(grp, &pmx->groups, node) {
856                         ret = ops->enable(pctldev, pmx->func_selector,
857                                           grp->group_selector);
858                         if (ret) {
859                                 /*
860                                  * TODO: call disable() on all groups we called
861                                  * enable() on to this point?
862                                  */
863                                 pmx->usecount--;
864                                 break;
865                         }
866                 }
867         }
868         mutex_unlock(&pmx->mutex);
869         return ret;
870 }
871 EXPORT_SYMBOL_GPL(pinmux_enable);
872
873 /**
874  * pinmux_disable() - disable a certain pinmux setting
875  * @pmx: the pinmux to disable, previously claimed by pinmux_get()
876  */
877 void pinmux_disable(struct pinmux *pmx)
878 {
879         if (pmx == NULL)
880                 return;
881
882         mutex_lock(&pmx->mutex);
883         if (--pmx->usecount == 0) {
884                 struct pinctrl_dev *pctldev = pmx->pctldev;
885                 const struct pinmux_ops *ops = pctldev->desc->pmxops;
886                 struct pinmux_group *grp;
887
888                 list_for_each_entry(grp, &pmx->groups, node) {
889                         ops->disable(pctldev, pmx->func_selector,
890                                      grp->group_selector);
891                 }
892         }
893         mutex_unlock(&pmx->mutex);
894 }
895 EXPORT_SYMBOL_GPL(pinmux_disable);
896
897 int pinmux_check_ops(const struct pinmux_ops *ops)
898 {
899         /* Check that we implement required operations */
900         if (!ops->list_functions ||
901             !ops->get_function_name ||
902             !ops->get_function_groups ||
903             !ops->enable ||
904             !ops->disable)
905                 return -EINVAL;
906
907         return 0;
908 }
909
910 /* Hog a single map entry and add to the hoglist */
911 static int pinmux_hog_map(struct pinctrl_dev *pctldev,
912                           struct pinmux_map const *map)
913 {
914         struct pinmux_hog *hog;
915         struct pinmux *pmx;
916         int ret;
917
918         if (map->dev || map->dev_name) {
919                 /*
920                  * TODO: the day we have device tree support, we can
921                  * traverse the device tree and hog to specific device nodes
922                  * without any problems, so then we can hog pinmuxes for
923                  * all devices that just want a static pin mux at this point.
924                  */
925                 dev_err(pctldev->dev, "map %s wants to hog a non-system pinmux, this is not going to work\n",
926                                 map->name);
927                 return -EINVAL;
928         }
929
930         hog = kzalloc(sizeof(struct pinmux_hog), GFP_KERNEL);
931         if (!hog)
932                 return -ENOMEM;
933
934         pmx = pinmux_get(NULL, map->name);
935         if (IS_ERR(pmx)) {
936                 kfree(hog);
937                 dev_err(pctldev->dev,
938                         "could not get the %s pinmux mapping for hogging\n",
939                         map->name);
940                 return PTR_ERR(pmx);
941         }
942
943         ret = pinmux_enable(pmx);
944         if (ret) {
945                 pinmux_put(pmx);
946                 kfree(hog);
947                 dev_err(pctldev->dev,
948                         "could not enable the %s pinmux mapping for hogging\n",
949                         map->name);
950                 return ret;
951         }
952
953         hog->map = map;
954         hog->pmx = pmx;
955
956         dev_info(pctldev->dev, "hogged map %s, function %s\n", map->name,
957                  map->function);
958         mutex_lock(&pctldev->pinmux_hogs_lock);
959         list_add(&hog->node, &pctldev->pinmux_hogs);
960         mutex_unlock(&pctldev->pinmux_hogs_lock);
961
962         return 0;
963 }
964
965 /**
966  * pinmux_hog_maps() - hog specific map entries on controller device
967  * @pctldev: the pin control device to hog entries on
968  *
969  * When the pin controllers are registered, there may be some specific pinmux
970  * map entries that need to be hogged, i.e. get+enabled until the system shuts
971  * down.
972  */
973 int pinmux_hog_maps(struct pinctrl_dev *pctldev)
974 {
975         struct device *dev = pctldev->dev;
976         const char *devname = dev_name(dev);
977         int ret;
978         int i;
979
980         INIT_LIST_HEAD(&pctldev->pinmux_hogs);
981         mutex_init(&pctldev->pinmux_hogs_lock);
982
983         for (i = 0; i < pinmux_maps_num; i++) {
984                 struct pinmux_map const *map = &pinmux_maps[i];
985
986                 if (((map->ctrl_dev == dev) ||
987                      !strcmp(map->ctrl_dev_name, devname)) &&
988                     map->hog_on_boot) {
989                         /* OK time to hog! */
990                         ret = pinmux_hog_map(pctldev, map);
991                         if (ret)
992                                 return ret;
993                 }
994         }
995         return 0;
996 }
997
998 /**
999  * pinmux_unhog_maps() - unhog specific map entries on controller device
1000  * @pctldev: the pin control device to unhog entries on
1001  */
1002 void pinmux_unhog_maps(struct pinctrl_dev *pctldev)
1003 {
1004         struct list_head *node, *tmp;
1005
1006         mutex_lock(&pctldev->pinmux_hogs_lock);
1007         list_for_each_safe(node, tmp, &pctldev->pinmux_hogs) {
1008                 struct pinmux_hog *hog =
1009                         list_entry(node, struct pinmux_hog, node);
1010                 pinmux_disable(hog->pmx);
1011                 pinmux_put(hog->pmx);
1012                 list_del(node);
1013                 kfree(hog);
1014         }
1015         mutex_unlock(&pctldev->pinmux_hogs_lock);
1016 }
1017
1018 #ifdef CONFIG_DEBUG_FS
1019
1020 /* Called from pincontrol core */
1021 static int pinmux_functions_show(struct seq_file *s, void *what)
1022 {
1023         struct pinctrl_dev *pctldev = s->private;
1024         const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
1025         unsigned func_selector = 0;
1026
1027         while (pmxops->list_functions(pctldev, func_selector) >= 0) {
1028                 const char *func = pmxops->get_function_name(pctldev,
1029                                                           func_selector);
1030                 const char * const *groups;
1031                 unsigned num_groups;
1032                 int ret;
1033                 int i;
1034
1035                 ret = pmxops->get_function_groups(pctldev, func_selector,
1036                                                   &groups, &num_groups);
1037                 if (ret)
1038                         seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
1039                                    func);
1040
1041                 seq_printf(s, "function: %s, groups = [ ", func);
1042                 for (i = 0; i < num_groups; i++)
1043                         seq_printf(s, "%s ", groups[i]);
1044                 seq_puts(s, "]\n");
1045
1046                 func_selector++;
1047
1048         }
1049
1050         return 0;
1051 }
1052
1053 static int pinmux_pins_show(struct seq_file *s, void *what)
1054 {
1055         struct pinctrl_dev *pctldev = s->private;
1056         unsigned i, pin;
1057
1058         seq_puts(s, "Pinmux settings per pin\n");
1059         seq_puts(s, "Format: pin (name): pinmuxfunction\n");
1060
1061         /* The pin number can be retrived from the pin controller descriptor */
1062         for (i = 0; i < pctldev->desc->npins; i++) {
1063
1064                 struct pin_desc *desc;
1065
1066                 pin = pctldev->desc->pins[i].number;
1067                 desc = pin_desc_get(pctldev, pin);
1068                 /* Skip if we cannot search the pin */
1069                 if (desc == NULL)
1070                         continue;
1071
1072                 seq_printf(s, "pin %d (%s): %s\n", pin,
1073                            desc->name ? desc->name : "unnamed",
1074                            desc->mux_function ? desc->mux_function
1075                                               : "UNCLAIMED");
1076         }
1077
1078         return 0;
1079 }
1080
1081 static int pinmux_hogs_show(struct seq_file *s, void *what)
1082 {
1083         struct pinctrl_dev *pctldev = s->private;
1084         struct pinmux_hog *hog;
1085
1086         seq_puts(s, "Pinmux map hogs held by device\n");
1087
1088         list_for_each_entry(hog, &pctldev->pinmux_hogs, node)
1089                 seq_printf(s, "%s\n", hog->map->name);
1090
1091         return 0;
1092 }
1093
1094 static int pinmux_show(struct seq_file *s, void *what)
1095 {
1096         struct pinmux *pmx;
1097
1098         seq_puts(s, "Requested pinmuxes and their maps:\n");
1099         list_for_each_entry(pmx, &pinmux_list, node) {
1100                 struct pinctrl_dev *pctldev = pmx->pctldev;
1101                 const struct pinmux_ops *pmxops;
1102                 const struct pinctrl_ops *pctlops;
1103                 struct pinmux_group *grp;
1104
1105                 if (!pctldev) {
1106                         seq_puts(s, "NO PIN CONTROLLER DEVICE\n");
1107                         continue;
1108                 }
1109
1110                 pmxops = pctldev->desc->pmxops;
1111                 pctlops = pctldev->desc->pctlops;
1112
1113                 seq_printf(s, "device: %s function: %s (%u),",
1114                            pinctrl_dev_get_name(pmx->pctldev),
1115                            pmxops->get_function_name(pctldev,
1116                                    pmx->func_selector),
1117                            pmx->func_selector);
1118
1119                 seq_printf(s, " groups: [");
1120                 list_for_each_entry(grp, &pmx->groups, node) {
1121                         seq_printf(s, " %s (%u)",
1122                                    pctlops->get_group_name(pctldev,
1123                                            grp->group_selector),
1124                                    grp->group_selector);
1125                 }
1126                 seq_printf(s, " ]");
1127
1128                 seq_printf(s, " users: %u map-> %s\n",
1129                            pmx->usecount,
1130                            pmx->dev ? dev_name(pmx->dev) : "(system)");
1131         }
1132
1133         return 0;
1134 }
1135
1136 static int pinmux_maps_show(struct seq_file *s, void *what)
1137 {
1138         int i;
1139
1140         seq_puts(s, "Pinmux maps:\n");
1141
1142         for (i = 0; i < pinmux_maps_num; i++) {
1143                 struct pinmux_map const *map = &pinmux_maps[i];
1144
1145                 seq_printf(s, "%s:\n", map->name);
1146                 if (map->dev || map->dev_name)
1147                         seq_printf(s, "  device: %s\n",
1148                                    map->dev ? dev_name(map->dev) :
1149                                    map->dev_name);
1150                 else
1151                         seq_printf(s, "  SYSTEM MUX\n");
1152                 seq_printf(s, "  controlling device %s\n",
1153                            map->ctrl_dev ? dev_name(map->ctrl_dev) :
1154                            map->ctrl_dev_name);
1155                 seq_printf(s, "  function: %s\n", map->function);
1156                 seq_printf(s, "  group: %s\n", map->group ? map->group :
1157                            "(default)");
1158         }
1159         return 0;
1160 }
1161
1162 static int pinmux_functions_open(struct inode *inode, struct file *file)
1163 {
1164         return single_open(file, pinmux_functions_show, inode->i_private);
1165 }
1166
1167 static int pinmux_pins_open(struct inode *inode, struct file *file)
1168 {
1169         return single_open(file, pinmux_pins_show, inode->i_private);
1170 }
1171
1172 static int pinmux_hogs_open(struct inode *inode, struct file *file)
1173 {
1174         return single_open(file, pinmux_hogs_show, inode->i_private);
1175 }
1176
1177 static int pinmux_open(struct inode *inode, struct file *file)
1178 {
1179         return single_open(file, pinmux_show, NULL);
1180 }
1181
1182 static int pinmux_maps_open(struct inode *inode, struct file *file)
1183 {
1184         return single_open(file, pinmux_maps_show, NULL);
1185 }
1186
1187 static const struct file_operations pinmux_functions_ops = {
1188         .open           = pinmux_functions_open,
1189         .read           = seq_read,
1190         .llseek         = seq_lseek,
1191         .release        = single_release,
1192 };
1193
1194 static const struct file_operations pinmux_pins_ops = {
1195         .open           = pinmux_pins_open,
1196         .read           = seq_read,
1197         .llseek         = seq_lseek,
1198         .release        = single_release,
1199 };
1200
1201 static const struct file_operations pinmux_hogs_ops = {
1202         .open           = pinmux_hogs_open,
1203         .read           = seq_read,
1204         .llseek         = seq_lseek,
1205         .release        = single_release,
1206 };
1207
1208 static const struct file_operations pinmux_ops = {
1209         .open           = pinmux_open,
1210         .read           = seq_read,
1211         .llseek         = seq_lseek,
1212         .release        = single_release,
1213 };
1214
1215 static const struct file_operations pinmux_maps_ops = {
1216         .open           = pinmux_maps_open,
1217         .read           = seq_read,
1218         .llseek         = seq_lseek,
1219         .release        = single_release,
1220 };
1221
1222 void pinmux_init_device_debugfs(struct dentry *devroot,
1223                          struct pinctrl_dev *pctldev)
1224 {
1225         debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
1226                             devroot, pctldev, &pinmux_functions_ops);
1227         debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
1228                             devroot, pctldev, &pinmux_pins_ops);
1229         debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO,
1230                             devroot, pctldev, &pinmux_hogs_ops);
1231 }
1232
1233 void pinmux_init_debugfs(struct dentry *subsys_root)
1234 {
1235         debugfs_create_file("pinmuxes", S_IFREG | S_IRUGO,
1236                             subsys_root, NULL, &pinmux_ops);
1237         debugfs_create_file("pinmux-maps", S_IFREG | S_IRUGO,
1238                             subsys_root, NULL, &pinmux_maps_ops);
1239 }
1240
1241 #endif /* CONFIG_DEBUG_FS */