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