f_phonet: fix page offset of first received fragment
[linux/fpc-iii.git] / drivers / pinctrl / pinmux.c
bloba5467f8709e9a72087ab272e6206f7e8ae1625d0
1 /*
2 * Core driver for the pin muxing portions of the pin control subsystem
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
8 * Author: Linus Walleij <linus.walleij@linaro.org>
10 * License terms: GNU General Public License (GPL) version 2
12 #define pr_fmt(fmt) "pinmux core: " fmt
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"
31 /* List of pinmuxes */
32 static DEFINE_MUTEX(pinmux_list_mutex);
33 static LIST_HEAD(pinmux_list);
35 /* List of pinmux hogs */
36 static DEFINE_MUTEX(pinmux_hoglist_mutex);
37 static LIST_HEAD(pinmux_hoglist);
39 /* Global pinmux maps, we allow one set only */
40 static struct pinmux_map const *pinmux_maps;
41 static unsigned pinmux_maps_num;
43 /**
44 * struct pinmux_group - group list item for pinmux groups
45 * @node: pinmux group list node
46 * @group_selector: the group selector for this group
48 struct pinmux_group {
49 struct list_head node;
50 unsigned group_selector;
53 /**
54 * struct pinmux - per-device pinmux state holder
55 * @node: global list node
56 * @dev: the device using this pinmux
57 * @usecount: the number of active users of this mux setting, used to keep
58 * track of nested use cases
59 * @pins: an array of discrete physical pins used in this mapping, taken
60 * from the global pin enumeration space (copied from pinmux map)
61 * @num_pins: the number of pins in this mapping array, i.e. the number of
62 * elements in .pins so we can iterate over that array (copied from
63 * pinmux map)
64 * @pctldev: pin control device handling this pinmux
65 * @func_selector: the function selector for the pinmux device handling
66 * this pinmux
67 * @groups: the group selectors for the pinmux device and
68 * selector combination handling this pinmux, this is a list that
69 * will be traversed on all pinmux operations such as
70 * get/put/enable/disable
71 * @mutex: a lock for the pinmux state holder
73 struct pinmux {
74 struct list_head node;
75 struct device *dev;
76 unsigned usecount;
77 struct pinctrl_dev *pctldev;
78 unsigned func_selector;
79 struct list_head groups;
80 struct mutex mutex;
83 /**
84 * struct pinmux_hog - a list item to stash mux hogs
85 * @node: pinmux hog list node
86 * @map: map entry responsible for this hogging
87 * @pmx: the pinmux hogged by this item
89 struct pinmux_hog {
90 struct list_head node;
91 struct pinmux_map const *map;
92 struct pinmux *pmx;
95 /**
96 * pin_request() - request a single pin to be muxed in, typically for GPIO
97 * @pin: the pin number in the global pin space
98 * @function: a functional name to give to this pin, passed to the driver
99 * so it knows what function to mux in, e.g. the string "gpioNN"
100 * means that you want to mux in the pin for use as GPIO number NN
101 * @gpio: if this request concerns a single GPIO pin
102 * @gpio_range: the range matching the GPIO pin if this is a request for a
103 * single GPIO pin
105 static int pin_request(struct pinctrl_dev *pctldev,
106 int pin, const char *function, bool gpio,
107 struct pinctrl_gpio_range *gpio_range)
109 struct pin_desc *desc;
110 const struct pinmux_ops *ops = pctldev->desc->pmxops;
111 int status = -EINVAL;
113 dev_dbg(&pctldev->dev, "request pin %d for %s\n", pin, function);
115 if (!pin_is_valid(pctldev, pin)) {
116 dev_err(&pctldev->dev, "pin is invalid\n");
117 return -EINVAL;
120 if (!function) {
121 dev_err(&pctldev->dev, "no function name given\n");
122 return -EINVAL;
125 desc = pin_desc_get(pctldev, pin);
126 if (desc == NULL) {
127 dev_err(&pctldev->dev,
128 "pin is not registered so it cannot be requested\n");
129 goto out;
132 spin_lock(&desc->lock);
133 if (desc->mux_function) {
134 spin_unlock(&desc->lock);
135 dev_err(&pctldev->dev,
136 "pin already requested\n");
137 goto out;
139 desc->mux_function = function;
140 spin_unlock(&desc->lock);
142 /* Let each pin increase references to this module */
143 if (!try_module_get(pctldev->owner)) {
144 dev_err(&pctldev->dev,
145 "could not increase module refcount for pin %d\n",
146 pin);
147 status = -EINVAL;
148 goto out_free_pin;
152 * If there is no kind of request function for the pin we just assume
153 * we got it by default and proceed.
155 if (gpio && ops->gpio_request_enable)
156 /* This requests and enables a single GPIO pin */
157 status = ops->gpio_request_enable(pctldev, gpio_range, pin);
158 else if (ops->request)
159 status = ops->request(pctldev, pin);
160 else
161 status = 0;
163 if (status)
164 dev_err(&pctldev->dev, "->request on device %s failed "
165 "for pin %d\n",
166 pctldev->desc->name, pin);
167 out_free_pin:
168 if (status) {
169 spin_lock(&desc->lock);
170 desc->mux_function = NULL;
171 spin_unlock(&desc->lock);
173 out:
174 if (status)
175 dev_err(&pctldev->dev, "pin-%d (%s) status %d\n",
176 pin, function ? : "?", status);
178 return status;
182 * pin_free() - release a single muxed in pin so something else can be muxed
183 * @pctldev: pin controller device handling this pin
184 * @pin: the pin to free
185 * @free_func: whether to free the pin's assigned function name string
187 static void pin_free(struct pinctrl_dev *pctldev, int pin, int free_func)
189 const struct pinmux_ops *ops = pctldev->desc->pmxops;
190 struct pin_desc *desc;
192 desc = pin_desc_get(pctldev, pin);
193 if (desc == NULL) {
194 dev_err(&pctldev->dev,
195 "pin is not registered so it cannot be freed\n");
196 return;
199 if (ops->free)
200 ops->free(pctldev, pin);
202 spin_lock(&desc->lock);
203 if (free_func)
204 kfree(desc->mux_function);
205 desc->mux_function = NULL;
206 spin_unlock(&desc->lock);
207 module_put(pctldev->owner);
211 * pinmux_request_gpio() - request a single pin to be muxed in as GPIO
212 * @gpio: the GPIO pin number from the GPIO subsystem number space
214 int pinmux_request_gpio(unsigned gpio)
216 char gpiostr[16];
217 const char *function;
218 struct pinctrl_dev *pctldev;
219 struct pinctrl_gpio_range *range;
220 int ret;
221 int pin;
223 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
224 if (ret)
225 return -EINVAL;
227 /* Convert to the pin controllers number space */
228 pin = gpio - range->base;
230 /* Conjure some name stating what chip and pin this is taken by */
231 snprintf(gpiostr, 15, "%s:%d", range->name, gpio);
233 function = kstrdup(gpiostr, GFP_KERNEL);
234 if (!function)
235 return -EINVAL;
237 ret = pin_request(pctldev, pin, function, true, range);
238 if (ret < 0)
239 kfree(function);
241 return ret;
243 EXPORT_SYMBOL_GPL(pinmux_request_gpio);
246 * pinmux_free_gpio() - free a single pin, currently used as GPIO
247 * @gpio: the GPIO pin number from the GPIO subsystem number space
249 void pinmux_free_gpio(unsigned gpio)
251 struct pinctrl_dev *pctldev;
252 struct pinctrl_gpio_range *range;
253 int ret;
254 int pin;
256 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
257 if (ret)
258 return;
260 /* Convert to the pin controllers number space */
261 pin = gpio - range->base;
263 pin_free(pctldev, pin, true);
265 EXPORT_SYMBOL_GPL(pinmux_free_gpio);
268 * pinmux_register_mappings() - register a set of pinmux mappings
269 * @maps: the pinmux mappings table to register
270 * @num_maps: the number of maps in the mapping table
272 * Only call this once during initialization of your machine, the function is
273 * tagged as __init and won't be callable after init has completed. The map
274 * passed into this function will be owned by the pinmux core and cannot be
275 * free:d.
277 int __init pinmux_register_mappings(struct pinmux_map const *maps,
278 unsigned num_maps)
280 int i;
282 if (pinmux_maps != NULL) {
283 pr_err("pinmux mappings already registered, you can only "
284 "register one set of maps\n");
285 return -EINVAL;
288 pr_debug("add %d pinmux maps\n", num_maps);
289 for (i = 0; i < num_maps; i++) {
290 /* Sanity check the mapping */
291 if (!maps[i].name) {
292 pr_err("failed to register map %d: "
293 "no map name given\n", i);
294 return -EINVAL;
296 if (!maps[i].ctrl_dev && !maps[i].ctrl_dev_name) {
297 pr_err("failed to register map %s (%d): "
298 "no pin control device given\n",
299 maps[i].name, i);
300 return -EINVAL;
302 if (!maps[i].function) {
303 pr_err("failed to register map %s (%d): "
304 "no function ID given\n", maps[i].name, i);
305 return -EINVAL;
308 if (!maps[i].dev && !maps[i].dev_name)
309 pr_debug("add system map %s function %s with no device\n",
310 maps[i].name,
311 maps[i].function);
312 else
313 pr_debug("register map %s, function %s\n",
314 maps[i].name,
315 maps[i].function);
318 pinmux_maps = maps;
319 pinmux_maps_num = num_maps;
321 return 0;
325 * acquire_pins() - acquire all the pins for a certain funcion on a pinmux
326 * @pctldev: the device to take the pins on
327 * @func_selector: the function selector to acquire the pins for
328 * @group_selector: the group selector containing the pins to acquire
330 static int acquire_pins(struct pinctrl_dev *pctldev,
331 unsigned func_selector,
332 unsigned group_selector)
334 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
335 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
336 const char *func = pmxops->get_function_name(pctldev,
337 func_selector);
338 const unsigned *pins;
339 unsigned num_pins;
340 int ret;
341 int i;
343 ret = pctlops->get_group_pins(pctldev, group_selector,
344 &pins, &num_pins);
345 if (ret)
346 return ret;
348 dev_dbg(&pctldev->dev, "requesting the %u pins from group %u\n",
349 num_pins, group_selector);
351 /* Try to allocate all pins in this group, one by one */
352 for (i = 0; i < num_pins; i++) {
353 ret = pin_request(pctldev, pins[i], func, false, NULL);
354 if (ret) {
355 dev_err(&pctldev->dev,
356 "could not get pin %d for function %s "
357 "on device %s - conflicting mux mappings?\n",
358 pins[i], func ? : "(undefined)",
359 pinctrl_dev_get_name(pctldev));
360 /* On error release all taken pins */
361 i--; /* this pin just failed */
362 for (; i >= 0; i--)
363 pin_free(pctldev, pins[i], false);
364 return -ENODEV;
367 return 0;
371 * release_pins() - release pins taken by earlier acquirement
372 * @pctldev: the device to free the pinx on
373 * @group_selector: the group selector containing the pins to free
375 static void release_pins(struct pinctrl_dev *pctldev,
376 unsigned group_selector)
378 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
379 const unsigned *pins;
380 unsigned num_pins;
381 int ret;
382 int i;
384 ret = pctlops->get_group_pins(pctldev, group_selector,
385 &pins, &num_pins);
386 if (ret) {
387 dev_err(&pctldev->dev, "could not get pins to release for "
388 "group selector %d\n",
389 group_selector);
390 return;
392 for (i = 0; i < num_pins; i++)
393 pin_free(pctldev, pins[i], false);
397 * pinmux_get_group_selector() - returns the group selector for a group
398 * @pctldev: the pin controller handling the group
399 * @pin_group: the pin group to look up
401 static int pinmux_get_group_selector(struct pinctrl_dev *pctldev,
402 const char *pin_group)
404 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
405 unsigned group_selector = 0;
407 while (pctlops->list_groups(pctldev, group_selector) >= 0) {
408 const char *gname = pctlops->get_group_name(pctldev,
409 group_selector);
410 if (!strcmp(gname, pin_group)) {
411 dev_dbg(&pctldev->dev,
412 "found group selector %u for %s\n",
413 group_selector,
414 pin_group);
415 return group_selector;
418 group_selector++;
421 dev_err(&pctldev->dev, "does not have pin group %s\n",
422 pin_group);
424 return -EINVAL;
428 * pinmux_check_pin_group() - check function and pin group combo
429 * @pctldev: device to check the pin group vs function for
430 * @func_selector: the function selector to check the pin group for, we have
431 * already looked this up in the calling function
432 * @pin_group: the pin group to match to the function
434 * This function will check that the pinmux driver can supply the
435 * selected pin group for a certain function, returns the group selector if
436 * the group and function selector will work fine together, else returns
437 * negative
439 static int pinmux_check_pin_group(struct pinctrl_dev *pctldev,
440 unsigned func_selector,
441 const char *pin_group)
443 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
444 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
445 int ret;
448 * If the driver does not support different pin groups for the
449 * functions, we only support group 0, and assume this exists.
451 if (!pctlops || !pctlops->list_groups)
452 return 0;
455 * Passing NULL (no specific group) will select the first and
456 * hopefully only group of pins available for this function.
458 if (!pin_group) {
459 char const * const *groups;
460 unsigned num_groups;
462 ret = pmxops->get_function_groups(pctldev, func_selector,
463 &groups, &num_groups);
464 if (ret)
465 return ret;
466 if (num_groups < 1)
467 return -EINVAL;
468 ret = pinmux_get_group_selector(pctldev, groups[0]);
469 if (ret < 0) {
470 dev_err(&pctldev->dev,
471 "function %s wants group %s but the pin "
472 "controller does not seem to have that group\n",
473 pmxops->get_function_name(pctldev, func_selector),
474 groups[0]);
475 return ret;
478 if (num_groups > 1)
479 dev_dbg(&pctldev->dev,
480 "function %s support more than one group, "
481 "default-selecting first group %s (%d)\n",
482 pmxops->get_function_name(pctldev, func_selector),
483 groups[0],
484 ret);
486 return ret;
489 dev_dbg(&pctldev->dev,
490 "check if we have pin group %s on controller %s\n",
491 pin_group, pinctrl_dev_get_name(pctldev));
493 ret = pinmux_get_group_selector(pctldev, pin_group);
494 if (ret < 0) {
495 dev_dbg(&pctldev->dev,
496 "%s does not support pin group %s with function %s\n",
497 pinctrl_dev_get_name(pctldev),
498 pin_group,
499 pmxops->get_function_name(pctldev, func_selector));
501 return ret;
505 * pinmux_search_function() - check pin control driver for a certain function
506 * @pctldev: device to check for function and position
507 * @map: function map containing the function and position to look for
508 * @func_selector: returns the applicable function selector if found
509 * @group_selector: returns the applicable group selector if found
511 * This will search the pinmux driver for an applicable
512 * function with a specific pin group, returns 0 if these can be mapped
513 * negative otherwise
515 static int pinmux_search_function(struct pinctrl_dev *pctldev,
516 struct pinmux_map const *map,
517 unsigned *func_selector,
518 unsigned *group_selector)
520 const struct pinmux_ops *ops = pctldev->desc->pmxops;
521 unsigned selector = 0;
523 /* See if this pctldev has this function */
524 while (ops->list_functions(pctldev, selector) >= 0) {
525 const char *fname = ops->get_function_name(pctldev,
526 selector);
527 int ret;
529 if (!strcmp(map->function, fname)) {
530 /* Found the function, check pin group */
531 ret = pinmux_check_pin_group(pctldev, selector,
532 map->group);
533 if (ret < 0)
534 return ret;
536 /* This function and group selector can be used */
537 *func_selector = selector;
538 *group_selector = ret;
539 return 0;
542 selector++;
545 pr_err("%s does not support function %s\n",
546 pinctrl_dev_get_name(pctldev), map->function);
547 return -EINVAL;
551 * pinmux_enable_muxmap() - enable a map entry for a certain pinmux
553 static int pinmux_enable_muxmap(struct pinctrl_dev *pctldev,
554 struct pinmux *pmx,
555 struct device *dev,
556 const char *devname,
557 struct pinmux_map const *map)
559 unsigned func_selector;
560 unsigned group_selector;
561 struct pinmux_group *grp;
562 int ret;
565 * Note that we're not locking the pinmux mutex here, because
566 * this is only called at pinmux initialization time when it
567 * has not been added to any list and thus is not reachable
568 * by anyone else.
571 if (pmx->pctldev && pmx->pctldev != pctldev) {
572 dev_err(&pctldev->dev,
573 "different pin control devices given for device %s, "
574 "function %s\n",
575 devname,
576 map->function);
577 return -EINVAL;
579 pmx->dev = dev;
580 pmx->pctldev = pctldev;
582 /* Now go into the driver and try to match a function and group */
583 ret = pinmux_search_function(pctldev, map, &func_selector,
584 &group_selector);
585 if (ret < 0)
586 return ret;
589 * If the function selector is already set, it needs to be identical,
590 * we support several groups with one function but not several
591 * functions with one or several groups in the same pinmux.
593 if (pmx->func_selector != UINT_MAX &&
594 pmx->func_selector != func_selector) {
595 dev_err(&pctldev->dev,
596 "dual function defines in the map for device %s\n",
597 devname);
598 return -EINVAL;
600 pmx->func_selector = func_selector;
602 /* Now add this group selector, we may have many of them */
603 grp = kmalloc(sizeof(struct pinmux_group), GFP_KERNEL);
604 if (!grp)
605 return -ENOMEM;
606 grp->group_selector = group_selector;
607 ret = acquire_pins(pctldev, func_selector, group_selector);
608 if (ret) {
609 kfree(grp);
610 return ret;
612 list_add(&grp->node, &pmx->groups);
614 return 0;
617 static void pinmux_free_groups(struct pinmux *pmx)
619 struct list_head *node, *tmp;
621 list_for_each_safe(node, tmp, &pmx->groups) {
622 struct pinmux_group *grp =
623 list_entry(node, struct pinmux_group, node);
624 /* Release all pins taken by this group */
625 release_pins(pmx->pctldev, grp->group_selector);
626 list_del(node);
627 kfree(grp);
632 * pinmux_get() - retrieves the pinmux for a certain device
633 * @dev: the device to get the pinmux for
634 * @name: an optional specific mux mapping name or NULL, the name is only
635 * needed if you want to have more than one mapping per device, or if you
636 * need an anonymous pinmux (not tied to any specific device)
638 struct pinmux *pinmux_get(struct device *dev, const char *name)
641 struct pinmux_map const *map = NULL;
642 struct pinctrl_dev *pctldev = NULL;
643 const char *devname = NULL;
644 struct pinmux *pmx;
645 bool found_map;
646 unsigned num_maps = 0;
647 int ret = -ENODEV;
648 int i;
650 /* We must have dev or ID or both */
651 if (!dev && !name)
652 return ERR_PTR(-EINVAL);
654 if (dev)
655 devname = dev_name(dev);
657 pr_debug("get mux %s for device %s\n", name,
658 devname ? devname : "(none)");
661 * create the state cookie holder struct pinmux for each
662 * mapping, this is what consumers will get when requesting
663 * a pinmux handle with pinmux_get()
665 pmx = kzalloc(sizeof(struct pinmux), GFP_KERNEL);
666 if (pmx == NULL)
667 return ERR_PTR(-ENOMEM);
668 mutex_init(&pmx->mutex);
669 pmx->func_selector = UINT_MAX;
670 INIT_LIST_HEAD(&pmx->groups);
672 /* Iterate over the pinmux maps to locate the right ones */
673 for (i = 0; i < pinmux_maps_num; i++) {
674 map = &pinmux_maps[i];
675 found_map = false;
678 * First, try to find the pctldev given in the map
680 pctldev = get_pinctrl_dev_from_dev(map->ctrl_dev,
681 map->ctrl_dev_name);
682 if (!pctldev) {
683 const char *devname = NULL;
685 if (map->ctrl_dev)
686 devname = dev_name(map->ctrl_dev);
687 else if (map->ctrl_dev_name)
688 devname = map->ctrl_dev_name;
690 pr_warning("could not find a pinctrl device for pinmux "
691 "function %s, fishy, they shall all have one\n",
692 map->function);
693 pr_warning("given pinctrl device name: %s",
694 devname ? devname : "UNDEFINED");
696 /* Continue to check the other mappings anyway... */
697 continue;
700 pr_debug("in map, found pctldev %s to handle function %s",
701 dev_name(&pctldev->dev), map->function);
705 * If we're looking for a specific named map, this must match,
706 * else we loop and look for the next.
708 if (name != NULL) {
709 if (map->name == NULL)
710 continue;
711 if (strcmp(map->name, name))
712 continue;
716 * This is for the case where no device name is given, we
717 * already know that the function name matches from above
718 * code.
720 if (!map->dev_name && (name != NULL))
721 found_map = true;
723 /* If the mapping has a device set up it must match */
724 if (map->dev_name &&
725 (!devname || !strcmp(map->dev_name, devname)))
726 /* MATCH! */
727 found_map = true;
729 /* If this map is applicable, then apply it */
730 if (found_map) {
731 ret = pinmux_enable_muxmap(pctldev, pmx, dev,
732 devname, map);
733 if (ret) {
734 pinmux_free_groups(pmx);
735 kfree(pmx);
736 return ERR_PTR(ret);
738 num_maps++;
743 /* We should have atleast one map, right */
744 if (!num_maps) {
745 pr_err("could not find any mux maps for device %s, ID %s\n",
746 devname ? devname : "(anonymous)",
747 name ? name : "(undefined)");
748 kfree(pmx);
749 return ERR_PTR(-EINVAL);
752 pr_debug("found %u mux maps for device %s, UD %s\n",
753 num_maps,
754 devname ? devname : "(anonymous)",
755 name ? name : "(undefined)");
757 /* Add the pinmux to the global list */
758 mutex_lock(&pinmux_list_mutex);
759 list_add(&pmx->node, &pinmux_list);
760 mutex_unlock(&pinmux_list_mutex);
762 return pmx;
764 EXPORT_SYMBOL_GPL(pinmux_get);
767 * pinmux_put() - release a previously claimed pinmux
768 * @pmx: a pinmux previously claimed by pinmux_get()
770 void pinmux_put(struct pinmux *pmx)
772 if (pmx == NULL)
773 return;
775 mutex_lock(&pmx->mutex);
776 if (pmx->usecount)
777 pr_warn("releasing pinmux with active users!\n");
778 /* Free the groups and all acquired pins */
779 pinmux_free_groups(pmx);
780 mutex_unlock(&pmx->mutex);
782 /* Remove from list */
783 mutex_lock(&pinmux_list_mutex);
784 list_del(&pmx->node);
785 mutex_unlock(&pinmux_list_mutex);
787 kfree(pmx);
789 EXPORT_SYMBOL_GPL(pinmux_put);
792 * pinmux_enable() - enable a certain pinmux setting
793 * @pmx: the pinmux to enable, previously claimed by pinmux_get()
795 int pinmux_enable(struct pinmux *pmx)
797 int ret = 0;
799 if (pmx == NULL)
800 return -EINVAL;
801 mutex_lock(&pmx->mutex);
802 if (pmx->usecount++ == 0) {
803 struct pinctrl_dev *pctldev = pmx->pctldev;
804 const struct pinmux_ops *ops = pctldev->desc->pmxops;
805 struct pinmux_group *grp;
807 list_for_each_entry(grp, &pmx->groups, node) {
808 ret = ops->enable(pctldev, pmx->func_selector,
809 grp->group_selector);
810 if (ret) {
812 * TODO: call disable() on all groups we called
813 * enable() on to this point?
815 pmx->usecount--;
816 break;
820 mutex_unlock(&pmx->mutex);
821 return ret;
823 EXPORT_SYMBOL_GPL(pinmux_enable);
826 * pinmux_disable() - disable a certain pinmux setting
827 * @pmx: the pinmux to disable, previously claimed by pinmux_get()
829 void pinmux_disable(struct pinmux *pmx)
831 if (pmx == NULL)
832 return;
834 mutex_lock(&pmx->mutex);
835 if (--pmx->usecount == 0) {
836 struct pinctrl_dev *pctldev = pmx->pctldev;
837 const struct pinmux_ops *ops = pctldev->desc->pmxops;
838 struct pinmux_group *grp;
840 list_for_each_entry(grp, &pmx->groups, node) {
841 ops->disable(pctldev, pmx->func_selector,
842 grp->group_selector);
845 mutex_unlock(&pmx->mutex);
847 EXPORT_SYMBOL_GPL(pinmux_disable);
849 int pinmux_check_ops(const struct pinmux_ops *ops)
851 /* Check that we implement required operations */
852 if (!ops->list_functions ||
853 !ops->get_function_name ||
854 !ops->get_function_groups ||
855 !ops->enable ||
856 !ops->disable)
857 return -EINVAL;
859 return 0;
862 /* Hog a single map entry and add to the hoglist */
863 static int pinmux_hog_map(struct pinctrl_dev *pctldev,
864 struct pinmux_map const *map)
866 struct pinmux_hog *hog;
867 struct pinmux *pmx;
868 int ret;
870 if (map->dev || map->dev_name) {
872 * TODO: the day we have device tree support, we can
873 * traverse the device tree and hog to specific device nodes
874 * without any problems, so then we can hog pinmuxes for
875 * all devices that just want a static pin mux at this point.
877 dev_err(&pctldev->dev, "map %s wants to hog a non-system "
878 "pinmux, this is not going to work\n", map->name);
879 return -EINVAL;
882 hog = kzalloc(sizeof(struct pinmux_hog), GFP_KERNEL);
883 if (!hog)
884 return -ENOMEM;
886 pmx = pinmux_get(NULL, map->name);
887 if (IS_ERR(pmx)) {
888 kfree(hog);
889 dev_err(&pctldev->dev,
890 "could not get the %s pinmux mapping for hogging\n",
891 map->name);
892 return PTR_ERR(pmx);
895 ret = pinmux_enable(pmx);
896 if (ret) {
897 pinmux_put(pmx);
898 kfree(hog);
899 dev_err(&pctldev->dev,
900 "could not enable the %s pinmux mapping for hogging\n",
901 map->name);
902 return ret;
905 hog->map = map;
906 hog->pmx = pmx;
908 dev_info(&pctldev->dev, "hogged map %s, function %s\n", map->name,
909 map->function);
910 mutex_lock(&pctldev->pinmux_hogs_lock);
911 list_add(&hog->node, &pctldev->pinmux_hogs);
912 mutex_unlock(&pctldev->pinmux_hogs_lock);
914 return 0;
918 * pinmux_hog_maps() - hog specific map entries on controller device
919 * @pctldev: the pin control device to hog entries on
921 * When the pin controllers are registered, there may be some specific pinmux
922 * map entries that need to be hogged, i.e. get+enabled until the system shuts
923 * down.
925 int pinmux_hog_maps(struct pinctrl_dev *pctldev)
927 struct device *dev = &pctldev->dev;
928 const char *devname = dev_name(dev);
929 int ret;
930 int i;
932 INIT_LIST_HEAD(&pctldev->pinmux_hogs);
933 mutex_init(&pctldev->pinmux_hogs_lock);
935 for (i = 0; i < pinmux_maps_num; i++) {
936 struct pinmux_map const *map = &pinmux_maps[i];
938 if (((map->ctrl_dev == dev) ||
939 !strcmp(map->ctrl_dev_name, devname)) &&
940 map->hog_on_boot) {
941 /* OK time to hog! */
942 ret = pinmux_hog_map(pctldev, map);
943 if (ret)
944 return ret;
947 return 0;
951 * pinmux_hog_maps() - unhog specific map entries on controller device
952 * @pctldev: the pin control device to unhog entries on
954 void pinmux_unhog_maps(struct pinctrl_dev *pctldev)
956 struct list_head *node, *tmp;
958 mutex_lock(&pctldev->pinmux_hogs_lock);
959 list_for_each_safe(node, tmp, &pctldev->pinmux_hogs) {
960 struct pinmux_hog *hog =
961 list_entry(node, struct pinmux_hog, node);
962 pinmux_disable(hog->pmx);
963 pinmux_put(hog->pmx);
964 list_del(node);
965 kfree(hog);
967 mutex_unlock(&pctldev->pinmux_hogs_lock);
970 #ifdef CONFIG_DEBUG_FS
972 /* Called from pincontrol core */
973 static int pinmux_functions_show(struct seq_file *s, void *what)
975 struct pinctrl_dev *pctldev = s->private;
976 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
977 unsigned func_selector = 0;
979 while (pmxops->list_functions(pctldev, func_selector) >= 0) {
980 const char *func = pmxops->get_function_name(pctldev,
981 func_selector);
982 const char * const *groups;
983 unsigned num_groups;
984 int ret;
985 int i;
987 ret = pmxops->get_function_groups(pctldev, func_selector,
988 &groups, &num_groups);
989 if (ret)
990 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
991 func);
993 seq_printf(s, "function: %s, groups = [ ", func);
994 for (i = 0; i < num_groups; i++)
995 seq_printf(s, "%s ", groups[i]);
996 seq_puts(s, "]\n");
998 func_selector++;
1002 return 0;
1005 static int pinmux_pins_show(struct seq_file *s, void *what)
1007 struct pinctrl_dev *pctldev = s->private;
1008 unsigned pin;
1010 seq_puts(s, "Pinmux settings per pin\n");
1011 seq_puts(s, "Format: pin (name): pinmuxfunction\n");
1013 /* The highest pin number need to be included in the loop, thus <= */
1014 for (pin = 0; pin <= pctldev->desc->maxpin; pin++) {
1016 struct pin_desc *desc;
1018 desc = pin_desc_get(pctldev, pin);
1019 /* Pin space may be sparse */
1020 if (desc == NULL)
1021 continue;
1023 seq_printf(s, "pin %d (%s): %s\n", pin,
1024 desc->name ? desc->name : "unnamed",
1025 desc->mux_function ? desc->mux_function
1026 : "UNCLAIMED");
1029 return 0;
1032 static int pinmux_hogs_show(struct seq_file *s, void *what)
1034 struct pinctrl_dev *pctldev = s->private;
1035 struct pinmux_hog *hog;
1037 seq_puts(s, "Pinmux map hogs held by device\n");
1039 list_for_each_entry(hog, &pctldev->pinmux_hogs, node)
1040 seq_printf(s, "%s\n", hog->map->name);
1042 return 0;
1045 static int pinmux_show(struct seq_file *s, void *what)
1047 struct pinmux *pmx;
1049 seq_puts(s, "Requested pinmuxes and their maps:\n");
1050 list_for_each_entry(pmx, &pinmux_list, node) {
1051 struct pinctrl_dev *pctldev = pmx->pctldev;
1052 const struct pinmux_ops *pmxops;
1053 const struct pinctrl_ops *pctlops;
1054 struct pinmux_group *grp;
1056 if (!pctldev) {
1057 seq_puts(s, "NO PIN CONTROLLER DEVICE\n");
1058 continue;
1061 pmxops = pctldev->desc->pmxops;
1062 pctlops = pctldev->desc->pctlops;
1064 seq_printf(s, "device: %s function: %s (%u),",
1065 pinctrl_dev_get_name(pmx->pctldev),
1066 pmxops->get_function_name(pctldev, pmx->func_selector),
1067 pmx->func_selector);
1069 seq_printf(s, " groups: [");
1070 list_for_each_entry(grp, &pmx->groups, node) {
1071 seq_printf(s, " %s (%u)",
1072 pctlops->get_group_name(pctldev, grp->group_selector),
1073 grp->group_selector);
1075 seq_printf(s, " ]");
1077 seq_printf(s, " users: %u map-> %s\n",
1078 pmx->usecount,
1079 pmx->dev ? dev_name(pmx->dev) : "(system)");
1082 return 0;
1085 static int pinmux_maps_show(struct seq_file *s, void *what)
1087 int i;
1089 seq_puts(s, "Pinmux maps:\n");
1091 for (i = 0; i < pinmux_maps_num; i++) {
1092 struct pinmux_map const *map = &pinmux_maps[i];
1094 seq_printf(s, "%s:\n", map->name);
1095 if (map->dev || map->dev_name)
1096 seq_printf(s, " device: %s\n",
1097 map->dev ? dev_name(map->dev) :
1098 map->dev_name);
1099 else
1100 seq_printf(s, " SYSTEM MUX\n");
1101 seq_printf(s, " controlling device %s\n",
1102 map->ctrl_dev ? dev_name(map->ctrl_dev) :
1103 map->ctrl_dev_name);
1104 seq_printf(s, " function: %s\n", map->function);
1105 seq_printf(s, " group: %s\n", map->group ? map->group :
1106 "(default)");
1108 return 0;
1111 static int pinmux_functions_open(struct inode *inode, struct file *file)
1113 return single_open(file, pinmux_functions_show, inode->i_private);
1116 static int pinmux_pins_open(struct inode *inode, struct file *file)
1118 return single_open(file, pinmux_pins_show, inode->i_private);
1121 static int pinmux_hogs_open(struct inode *inode, struct file *file)
1123 return single_open(file, pinmux_hogs_show, inode->i_private);
1126 static int pinmux_open(struct inode *inode, struct file *file)
1128 return single_open(file, pinmux_show, NULL);
1131 static int pinmux_maps_open(struct inode *inode, struct file *file)
1133 return single_open(file, pinmux_maps_show, NULL);
1136 static const struct file_operations pinmux_functions_ops = {
1137 .open = pinmux_functions_open,
1138 .read = seq_read,
1139 .llseek = seq_lseek,
1140 .release = single_release,
1143 static const struct file_operations pinmux_pins_ops = {
1144 .open = pinmux_pins_open,
1145 .read = seq_read,
1146 .llseek = seq_lseek,
1147 .release = single_release,
1150 static const struct file_operations pinmux_hogs_ops = {
1151 .open = pinmux_hogs_open,
1152 .read = seq_read,
1153 .llseek = seq_lseek,
1154 .release = single_release,
1157 static const struct file_operations pinmux_ops = {
1158 .open = pinmux_open,
1159 .read = seq_read,
1160 .llseek = seq_lseek,
1161 .release = single_release,
1164 static const struct file_operations pinmux_maps_ops = {
1165 .open = pinmux_maps_open,
1166 .read = seq_read,
1167 .llseek = seq_lseek,
1168 .release = single_release,
1171 void pinmux_init_device_debugfs(struct dentry *devroot,
1172 struct pinctrl_dev *pctldev)
1174 debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
1175 devroot, pctldev, &pinmux_functions_ops);
1176 debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
1177 devroot, pctldev, &pinmux_pins_ops);
1178 debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO,
1179 devroot, pctldev, &pinmux_hogs_ops);
1182 void pinmux_init_debugfs(struct dentry *subsys_root)
1184 debugfs_create_file("pinmuxes", S_IFREG | S_IRUGO,
1185 subsys_root, NULL, &pinmux_ops);
1186 debugfs_create_file("pinmux-maps", S_IFREG | S_IRUGO,
1187 subsys_root, NULL, &pinmux_maps_ops);
1190 #endif /* CONFIG_DEBUG_FS */