pinctrl: remove double pin validity check.
[deliverable/linux.git] / drivers / pinctrl / pinmux.c
CommitLineData
2744e8af
LW
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 */
32static DEFINE_MUTEX(pinmux_list_mutex);
33static LIST_HEAD(pinmux_list);
34
2744e8af
LW
35/* Global pinmux maps, we allow one set only */
36static struct pinmux_map const *pinmux_maps;
37static 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 */
44struct 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 */
69struct 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 */
85struct 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: if this request concerns a single GPIO pin
98 * @gpio_range: the range matching the GPIO pin if this is a request for a
99 * single GPIO pin
100 */
101static int pin_request(struct pinctrl_dev *pctldev,
102 int pin, const char *function, bool gpio,
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
2744e8af
LW
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
d2f6a1c6
MB
118 if (!function) {
119 dev_err(&pctldev->dev, "no function name given\n");
120 return -EINVAL;
121 }
122
2744e8af 123 spin_lock(&desc->lock);
5d2eaf80 124 if (desc->mux_function) {
2744e8af
LW
125 spin_unlock(&desc->lock);
126 dev_err(&pctldev->dev,
127 "pin already requested\n");
128 goto out;
129 }
5d2eaf80 130 desc->mux_function = function;
2744e8af
LW
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 && 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 "
156 "for pin %d\n",
157 pctldev->desc->name, pin);
158out_free_pin:
159 if (status) {
160 spin_lock(&desc->lock);
5d2eaf80 161 desc->mux_function = NULL;
2744e8af
LW
162 spin_unlock(&desc->lock);
163 }
164out:
165 if (status)
166 dev_err(&pctldev->dev, "pin-%d (%s) status %d\n",
167 pin, function ? : "?", status);
168
169 return status;
170}
171
172/**
173 * pin_free() - release a single muxed in pin so something else can be muxed
174 * @pctldev: pin controller device handling this pin
175 * @pin: the pin to free
5d2eaf80 176 * @free_func: whether to free the pin's assigned function name string
2744e8af 177 */
5d2eaf80 178static void pin_free(struct pinctrl_dev *pctldev, int pin, int free_func)
2744e8af
LW
179{
180 const struct pinmux_ops *ops = pctldev->desc->pmxops;
181 struct pin_desc *desc;
182
183 desc = pin_desc_get(pctldev, pin);
184 if (desc == NULL) {
185 dev_err(&pctldev->dev,
186 "pin is not registered so it cannot be freed\n");
187 return;
188 }
189
190 if (ops->free)
191 ops->free(pctldev, pin);
192
193 spin_lock(&desc->lock);
5d2eaf80
SW
194 if (free_func)
195 kfree(desc->mux_function);
196 desc->mux_function = NULL;
2744e8af
LW
197 spin_unlock(&desc->lock);
198 module_put(pctldev->owner);
199}
200
201/**
202 * pinmux_request_gpio() - request a single pin to be muxed in as GPIO
203 * @gpio: the GPIO pin number from the GPIO subsystem number space
204 */
205int pinmux_request_gpio(unsigned gpio)
206{
207 char gpiostr[16];
5d2eaf80 208 const char *function;
2744e8af
LW
209 struct pinctrl_dev *pctldev;
210 struct pinctrl_gpio_range *range;
211 int ret;
212 int pin;
213
214 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
215 if (ret)
216 return -EINVAL;
217
218 /* Convert to the pin controllers number space */
219 pin = gpio - range->base;
220
221 /* Conjure some name stating what chip and pin this is taken by */
222 snprintf(gpiostr, 15, "%s:%d", range->name, gpio);
223
5d2eaf80
SW
224 function = kstrdup(gpiostr, GFP_KERNEL);
225 if (!function)
226 return -EINVAL;
227
228 ret = pin_request(pctldev, pin, function, true, range);
229 if (ret < 0)
230 kfree(function);
231
232 return ret;
2744e8af
LW
233}
234EXPORT_SYMBOL_GPL(pinmux_request_gpio);
235
236/**
237 * pinmux_free_gpio() - free a single pin, currently used as GPIO
238 * @gpio: the GPIO pin number from the GPIO subsystem number space
239 */
240void pinmux_free_gpio(unsigned gpio)
241{
242 struct pinctrl_dev *pctldev;
243 struct pinctrl_gpio_range *range;
244 int ret;
245 int pin;
246
247 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
248 if (ret)
249 return;
250
251 /* Convert to the pin controllers number space */
252 pin = gpio - range->base;
253
5d2eaf80 254 pin_free(pctldev, pin, true);
2744e8af
LW
255}
256EXPORT_SYMBOL_GPL(pinmux_free_gpio);
257
258/**
259 * pinmux_register_mappings() - register a set of pinmux mappings
260 * @maps: the pinmux mappings table to register
261 * @num_maps: the number of maps in the mapping table
262 *
263 * Only call this once during initialization of your machine, the function is
264 * tagged as __init and won't be callable after init has completed. The map
265 * passed into this function will be owned by the pinmux core and cannot be
266 * free:d.
267 */
268int __init pinmux_register_mappings(struct pinmux_map const *maps,
269 unsigned num_maps)
270{
271 int i;
272
273 if (pinmux_maps != NULL) {
274 pr_err("pinmux mappings already registered, you can only "
275 "register one set of maps\n");
276 return -EINVAL;
277 }
278
279 pr_debug("add %d pinmux maps\n", num_maps);
280 for (i = 0; i < num_maps; i++) {
281 /* Sanity check the mapping */
282 if (!maps[i].name) {
283 pr_err("failed to register map %d: "
284 "no map name given\n", i);
285 return -EINVAL;
286 }
287 if (!maps[i].ctrl_dev && !maps[i].ctrl_dev_name) {
288 pr_err("failed to register map %s (%d): "
289 "no pin control device given\n",
290 maps[i].name, i);
291 return -EINVAL;
292 }
293 if (!maps[i].function) {
294 pr_err("failed to register map %s (%d): "
295 "no function ID given\n", maps[i].name, i);
296 return -EINVAL;
297 }
298
299 if (!maps[i].dev && !maps[i].dev_name)
300 pr_debug("add system map %s function %s with no device\n",
301 maps[i].name,
302 maps[i].function);
303 else
304 pr_debug("register map %s, function %s\n",
305 maps[i].name,
306 maps[i].function);
307 }
308
309 pinmux_maps = maps;
310 pinmux_maps_num = num_maps;
311
312 return 0;
313}
314
315/**
316 * acquire_pins() - acquire all the pins for a certain funcion on a pinmux
317 * @pctldev: the device to take the pins on
318 * @func_selector: the function selector to acquire the pins for
319 * @group_selector: the group selector containing the pins to acquire
320 */
321static int acquire_pins(struct pinctrl_dev *pctldev,
322 unsigned func_selector,
323 unsigned group_selector)
324{
325 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
326 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
327 const char *func = pmxops->get_function_name(pctldev,
328 func_selector);
a5818a8b 329 const unsigned *pins;
2744e8af
LW
330 unsigned num_pins;
331 int ret;
332 int i;
333
334 ret = pctlops->get_group_pins(pctldev, group_selector,
335 &pins, &num_pins);
336 if (ret)
337 return ret;
338
339 dev_dbg(&pctldev->dev, "requesting the %u pins from group %u\n",
340 num_pins, group_selector);
341
342 /* Try to allocate all pins in this group, one by one */
343 for (i = 0; i < num_pins; i++) {
344 ret = pin_request(pctldev, pins[i], func, false, NULL);
345 if (ret) {
346 dev_err(&pctldev->dev,
347 "could not get pin %d for function %s "
348 "on device %s - conflicting mux mappings?\n",
349 pins[i], func ? : "(undefined)",
350 pinctrl_dev_get_name(pctldev));
351 /* On error release all taken pins */
352 i--; /* this pin just failed */
353 for (; i >= 0; i--)
5d2eaf80 354 pin_free(pctldev, pins[i], false);
2744e8af
LW
355 return -ENODEV;
356 }
357 }
358 return 0;
359}
360
361/**
362 * release_pins() - release pins taken by earlier acquirement
363 * @pctldev: the device to free the pinx on
364 * @group_selector: the group selector containing the pins to free
365 */
366static void release_pins(struct pinctrl_dev *pctldev,
367 unsigned group_selector)
368{
369 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
a5818a8b 370 const unsigned *pins;
2744e8af
LW
371 unsigned num_pins;
372 int ret;
373 int i;
374
375 ret = pctlops->get_group_pins(pctldev, group_selector,
376 &pins, &num_pins);
377 if (ret) {
378 dev_err(&pctldev->dev, "could not get pins to release for "
379 "group selector %d\n",
380 group_selector);
381 return;
382 }
383 for (i = 0; i < num_pins; i++)
5d2eaf80 384 pin_free(pctldev, pins[i], false);
2744e8af
LW
385}
386
2744e8af
LW
387/**
388 * pinmux_check_pin_group() - check function and pin group combo
389 * @pctldev: device to check the pin group vs function for
390 * @func_selector: the function selector to check the pin group for, we have
391 * already looked this up in the calling function
392 * @pin_group: the pin group to match to the function
393 *
394 * This function will check that the pinmux driver can supply the
395 * selected pin group for a certain function, returns the group selector if
396 * the group and function selector will work fine together, else returns
397 * negative
398 */
399static int pinmux_check_pin_group(struct pinctrl_dev *pctldev,
400 unsigned func_selector,
401 const char *pin_group)
402{
403 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
404 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
405 int ret;
406
407 /*
408 * If the driver does not support different pin groups for the
409 * functions, we only support group 0, and assume this exists.
410 */
411 if (!pctlops || !pctlops->list_groups)
412 return 0;
413
414 /*
415 * Passing NULL (no specific group) will select the first and
416 * hopefully only group of pins available for this function.
417 */
418 if (!pin_group) {
419 char const * const *groups;
420 unsigned num_groups;
421
422 ret = pmxops->get_function_groups(pctldev, func_selector,
423 &groups, &num_groups);
424 if (ret)
425 return ret;
426 if (num_groups < 1)
427 return -EINVAL;
7afde8ba 428 ret = pinctrl_get_group_selector(pctldev, groups[0]);
2744e8af
LW
429 if (ret < 0) {
430 dev_err(&pctldev->dev,
431 "function %s wants group %s but the pin "
432 "controller does not seem to have that group\n",
433 pmxops->get_function_name(pctldev, func_selector),
434 groups[0]);
435 return ret;
436 }
437
438 if (num_groups > 1)
439 dev_dbg(&pctldev->dev,
440 "function %s support more than one group, "
441 "default-selecting first group %s (%d)\n",
442 pmxops->get_function_name(pctldev, func_selector),
443 groups[0],
444 ret);
445
446 return ret;
447 }
448
449 dev_dbg(&pctldev->dev,
450 "check if we have pin group %s on controller %s\n",
451 pin_group, pinctrl_dev_get_name(pctldev));
452
7afde8ba 453 ret = pinctrl_get_group_selector(pctldev, pin_group);
2744e8af
LW
454 if (ret < 0) {
455 dev_dbg(&pctldev->dev,
456 "%s does not support pin group %s with function %s\n",
457 pinctrl_dev_get_name(pctldev),
458 pin_group,
459 pmxops->get_function_name(pctldev, func_selector));
460 }
461 return ret;
462}
463
464/**
465 * pinmux_search_function() - check pin control driver for a certain function
466 * @pctldev: device to check for function and position
467 * @map: function map containing the function and position to look for
468 * @func_selector: returns the applicable function selector if found
469 * @group_selector: returns the applicable group selector if found
470 *
471 * This will search the pinmux driver for an applicable
472 * function with a specific pin group, returns 0 if these can be mapped
473 * negative otherwise
474 */
475static int pinmux_search_function(struct pinctrl_dev *pctldev,
476 struct pinmux_map const *map,
477 unsigned *func_selector,
478 unsigned *group_selector)
479{
480 const struct pinmux_ops *ops = pctldev->desc->pmxops;
481 unsigned selector = 0;
482
483 /* See if this pctldev has this function */
484 while (ops->list_functions(pctldev, selector) >= 0) {
485 const char *fname = ops->get_function_name(pctldev,
486 selector);
487 int ret;
488
489 if (!strcmp(map->function, fname)) {
490 /* Found the function, check pin group */
491 ret = pinmux_check_pin_group(pctldev, selector,
492 map->group);
493 if (ret < 0)
494 return ret;
495
496 /* This function and group selector can be used */
497 *func_selector = selector;
498 *group_selector = ret;
499 return 0;
500
501 }
502 selector++;
503 }
504
505 pr_err("%s does not support function %s\n",
506 pinctrl_dev_get_name(pctldev), map->function);
507 return -EINVAL;
508}
509
510/**
511 * pinmux_enable_muxmap() - enable a map entry for a certain pinmux
512 */
513static int pinmux_enable_muxmap(struct pinctrl_dev *pctldev,
514 struct pinmux *pmx,
515 struct device *dev,
516 const char *devname,
517 struct pinmux_map const *map)
518{
519 unsigned func_selector;
520 unsigned group_selector;
521 struct pinmux_group *grp;
522 int ret;
523
524 /*
525 * Note that we're not locking the pinmux mutex here, because
526 * this is only called at pinmux initialization time when it
527 * has not been added to any list and thus is not reachable
528 * by anyone else.
529 */
530
531 if (pmx->pctldev && pmx->pctldev != pctldev) {
532 dev_err(&pctldev->dev,
533 "different pin control devices given for device %s, "
534 "function %s\n",
535 devname,
536 map->function);
537 return -EINVAL;
538 }
539 pmx->dev = dev;
540 pmx->pctldev = pctldev;
541
542 /* Now go into the driver and try to match a function and group */
543 ret = pinmux_search_function(pctldev, map, &func_selector,
544 &group_selector);
545 if (ret < 0)
546 return ret;
547
548 /*
549 * If the function selector is already set, it needs to be identical,
550 * we support several groups with one function but not several
551 * functions with one or several groups in the same pinmux.
552 */
553 if (pmx->func_selector != UINT_MAX &&
554 pmx->func_selector != func_selector) {
555 dev_err(&pctldev->dev,
556 "dual function defines in the map for device %s\n",
557 devname);
558 return -EINVAL;
559 }
560 pmx->func_selector = func_selector;
561
562 /* Now add this group selector, we may have many of them */
563 grp = kmalloc(sizeof(struct pinmux_group), GFP_KERNEL);
564 if (!grp)
565 return -ENOMEM;
566 grp->group_selector = group_selector;
567 ret = acquire_pins(pctldev, func_selector, group_selector);
568 if (ret) {
569 kfree(grp);
570 return ret;
571 }
572 list_add(&grp->node, &pmx->groups);
573
574 return 0;
575}
576
577static void pinmux_free_groups(struct pinmux *pmx)
578{
579 struct list_head *node, *tmp;
580
581 list_for_each_safe(node, tmp, &pmx->groups) {
582 struct pinmux_group *grp =
583 list_entry(node, struct pinmux_group, node);
584 /* Release all pins taken by this group */
585 release_pins(pmx->pctldev, grp->group_selector);
586 list_del(node);
587 kfree(grp);
588 }
589}
590
591/**
592 * pinmux_get() - retrieves the pinmux for a certain device
593 * @dev: the device to get the pinmux for
594 * @name: an optional specific mux mapping name or NULL, the name is only
595 * needed if you want to have more than one mapping per device, or if you
596 * need an anonymous pinmux (not tied to any specific device)
597 */
598struct pinmux *pinmux_get(struct device *dev, const char *name)
599{
600
601 struct pinmux_map const *map = NULL;
602 struct pinctrl_dev *pctldev = NULL;
603 const char *devname = NULL;
604 struct pinmux *pmx;
605 bool found_map;
606 unsigned num_maps = 0;
607 int ret = -ENODEV;
608 int i;
609
610 /* We must have dev or ID or both */
611 if (!dev && !name)
612 return ERR_PTR(-EINVAL);
613
614 if (dev)
615 devname = dev_name(dev);
616
617 pr_debug("get mux %s for device %s\n", name,
618 devname ? devname : "(none)");
619
620 /*
621 * create the state cookie holder struct pinmux for each
622 * mapping, this is what consumers will get when requesting
623 * a pinmux handle with pinmux_get()
624 */
625 pmx = kzalloc(sizeof(struct pinmux), GFP_KERNEL);
626 if (pmx == NULL)
627 return ERR_PTR(-ENOMEM);
628 mutex_init(&pmx->mutex);
629 pmx->func_selector = UINT_MAX;
630 INIT_LIST_HEAD(&pmx->groups);
631
632 /* Iterate over the pinmux maps to locate the right ones */
633 for (i = 0; i < pinmux_maps_num; i++) {
634 map = &pinmux_maps[i];
635 found_map = false;
636
637 /*
638 * First, try to find the pctldev given in the map
639 */
640 pctldev = get_pinctrl_dev_from_dev(map->ctrl_dev,
641 map->ctrl_dev_name);
642 if (!pctldev) {
643 const char *devname = NULL;
644
645 if (map->ctrl_dev)
646 devname = dev_name(map->ctrl_dev);
647 else if (map->ctrl_dev_name)
648 devname = map->ctrl_dev_name;
649
650 pr_warning("could not find a pinctrl device for pinmux "
651 "function %s, fishy, they shall all have one\n",
652 map->function);
653 pr_warning("given pinctrl device name: %s",
654 devname ? devname : "UNDEFINED");
655
656 /* Continue to check the other mappings anyway... */
657 continue;
658 }
659
660 pr_debug("in map, found pctldev %s to handle function %s",
661 dev_name(&pctldev->dev), map->function);
662
663
664 /*
665 * If we're looking for a specific named map, this must match,
666 * else we loop and look for the next.
667 */
668 if (name != NULL) {
669 if (map->name == NULL)
670 continue;
671 if (strcmp(map->name, name))
672 continue;
673 }
674
675 /*
676 * This is for the case where no device name is given, we
677 * already know that the function name matches from above
678 * code.
679 */
680 if (!map->dev_name && (name != NULL))
681 found_map = true;
682
683 /* If the mapping has a device set up it must match */
684 if (map->dev_name &&
685 (!devname || !strcmp(map->dev_name, devname)))
686 /* MATCH! */
687 found_map = true;
688
689 /* If this map is applicable, then apply it */
690 if (found_map) {
691 ret = pinmux_enable_muxmap(pctldev, pmx, dev,
692 devname, map);
693 if (ret) {
694 pinmux_free_groups(pmx);
695 kfree(pmx);
696 return ERR_PTR(ret);
697 }
698 num_maps++;
699 }
700 }
701
702
703 /* We should have atleast one map, right */
704 if (!num_maps) {
705 pr_err("could not find any mux maps for device %s, ID %s\n",
706 devname ? devname : "(anonymous)",
707 name ? name : "(undefined)");
708 kfree(pmx);
709 return ERR_PTR(-EINVAL);
710 }
711
712 pr_debug("found %u mux maps for device %s, UD %s\n",
713 num_maps,
714 devname ? devname : "(anonymous)",
715 name ? name : "(undefined)");
716
717 /* Add the pinmux to the global list */
718 mutex_lock(&pinmux_list_mutex);
719 list_add(&pmx->node, &pinmux_list);
720 mutex_unlock(&pinmux_list_mutex);
721
722 return pmx;
723}
724EXPORT_SYMBOL_GPL(pinmux_get);
725
726/**
727 * pinmux_put() - release a previously claimed pinmux
728 * @pmx: a pinmux previously claimed by pinmux_get()
729 */
730void pinmux_put(struct pinmux *pmx)
731{
732 if (pmx == NULL)
733 return;
734
735 mutex_lock(&pmx->mutex);
736 if (pmx->usecount)
737 pr_warn("releasing pinmux with active users!\n");
738 /* Free the groups and all acquired pins */
739 pinmux_free_groups(pmx);
740 mutex_unlock(&pmx->mutex);
741
742 /* Remove from list */
743 mutex_lock(&pinmux_list_mutex);
744 list_del(&pmx->node);
745 mutex_unlock(&pinmux_list_mutex);
746
747 kfree(pmx);
748}
749EXPORT_SYMBOL_GPL(pinmux_put);
750
751/**
752 * pinmux_enable() - enable a certain pinmux setting
753 * @pmx: the pinmux to enable, previously claimed by pinmux_get()
754 */
755int pinmux_enable(struct pinmux *pmx)
756{
757 int ret = 0;
758
759 if (pmx == NULL)
760 return -EINVAL;
761 mutex_lock(&pmx->mutex);
762 if (pmx->usecount++ == 0) {
763 struct pinctrl_dev *pctldev = pmx->pctldev;
764 const struct pinmux_ops *ops = pctldev->desc->pmxops;
765 struct pinmux_group *grp;
766
767 list_for_each_entry(grp, &pmx->groups, node) {
768 ret = ops->enable(pctldev, pmx->func_selector,
769 grp->group_selector);
770 if (ret) {
771 /*
772 * TODO: call disable() on all groups we called
773 * enable() on to this point?
774 */
775 pmx->usecount--;
776 break;
777 }
778 }
779 }
780 mutex_unlock(&pmx->mutex);
781 return ret;
782}
783EXPORT_SYMBOL_GPL(pinmux_enable);
784
785/**
786 * pinmux_disable() - disable a certain pinmux setting
787 * @pmx: the pinmux to disable, previously claimed by pinmux_get()
788 */
789void pinmux_disable(struct pinmux *pmx)
790{
791 if (pmx == NULL)
792 return;
793
794 mutex_lock(&pmx->mutex);
795 if (--pmx->usecount == 0) {
796 struct pinctrl_dev *pctldev = pmx->pctldev;
797 const struct pinmux_ops *ops = pctldev->desc->pmxops;
798 struct pinmux_group *grp;
799
800 list_for_each_entry(grp, &pmx->groups, node) {
801 ops->disable(pctldev, pmx->func_selector,
802 grp->group_selector);
803 }
804 }
805 mutex_unlock(&pmx->mutex);
806}
807EXPORT_SYMBOL_GPL(pinmux_disable);
808
809int pinmux_check_ops(const struct pinmux_ops *ops)
810{
811 /* Check that we implement required operations */
812 if (!ops->list_functions ||
813 !ops->get_function_name ||
814 !ops->get_function_groups ||
815 !ops->enable ||
816 !ops->disable)
817 return -EINVAL;
818
819 return 0;
820}
821
822/* Hog a single map entry and add to the hoglist */
823static int pinmux_hog_map(struct pinctrl_dev *pctldev,
824 struct pinmux_map const *map)
825{
826 struct pinmux_hog *hog;
827 struct pinmux *pmx;
828 int ret;
829
830 if (map->dev || map->dev_name) {
831 /*
832 * TODO: the day we have device tree support, we can
833 * traverse the device tree and hog to specific device nodes
834 * without any problems, so then we can hog pinmuxes for
835 * all devices that just want a static pin mux at this point.
836 */
837 dev_err(&pctldev->dev, "map %s wants to hog a non-system "
838 "pinmux, this is not going to work\n", map->name);
839 return -EINVAL;
840 }
841
842 hog = kzalloc(sizeof(struct pinmux_hog), GFP_KERNEL);
843 if (!hog)
844 return -ENOMEM;
845
846 pmx = pinmux_get(NULL, map->name);
847 if (IS_ERR(pmx)) {
848 kfree(hog);
849 dev_err(&pctldev->dev,
850 "could not get the %s pinmux mapping for hogging\n",
851 map->name);
852 return PTR_ERR(pmx);
853 }
854
855 ret = pinmux_enable(pmx);
856 if (ret) {
857 pinmux_put(pmx);
858 kfree(hog);
859 dev_err(&pctldev->dev,
860 "could not enable the %s pinmux mapping for hogging\n",
861 map->name);
862 return ret;
863 }
864
865 hog->map = map;
866 hog->pmx = pmx;
867
868 dev_info(&pctldev->dev, "hogged map %s, function %s\n", map->name,
869 map->function);
870 mutex_lock(&pctldev->pinmux_hogs_lock);
871 list_add(&hog->node, &pctldev->pinmux_hogs);
872 mutex_unlock(&pctldev->pinmux_hogs_lock);
873
874 return 0;
875}
876
877/**
878 * pinmux_hog_maps() - hog specific map entries on controller device
879 * @pctldev: the pin control device to hog entries on
880 *
881 * When the pin controllers are registered, there may be some specific pinmux
882 * map entries that need to be hogged, i.e. get+enabled until the system shuts
883 * down.
884 */
885int pinmux_hog_maps(struct pinctrl_dev *pctldev)
886{
887 struct device *dev = &pctldev->dev;
888 const char *devname = dev_name(dev);
889 int ret;
890 int i;
891
892 INIT_LIST_HEAD(&pctldev->pinmux_hogs);
893 mutex_init(&pctldev->pinmux_hogs_lock);
894
895 for (i = 0; i < pinmux_maps_num; i++) {
896 struct pinmux_map const *map = &pinmux_maps[i];
897
898 if (((map->ctrl_dev == dev) ||
899 !strcmp(map->ctrl_dev_name, devname)) &&
900 map->hog_on_boot) {
901 /* OK time to hog! */
902 ret = pinmux_hog_map(pctldev, map);
903 if (ret)
904 return ret;
905 }
906 }
907 return 0;
908}
909
910/**
911 * pinmux_hog_maps() - unhog specific map entries on controller device
912 * @pctldev: the pin control device to unhog entries on
913 */
914void pinmux_unhog_maps(struct pinctrl_dev *pctldev)
915{
916 struct list_head *node, *tmp;
917
918 mutex_lock(&pctldev->pinmux_hogs_lock);
919 list_for_each_safe(node, tmp, &pctldev->pinmux_hogs) {
920 struct pinmux_hog *hog =
921 list_entry(node, struct pinmux_hog, node);
922 pinmux_disable(hog->pmx);
923 pinmux_put(hog->pmx);
924 list_del(node);
925 kfree(hog);
926 }
927 mutex_unlock(&pctldev->pinmux_hogs_lock);
928}
929
930#ifdef CONFIG_DEBUG_FS
931
932/* Called from pincontrol core */
933static int pinmux_functions_show(struct seq_file *s, void *what)
934{
935 struct pinctrl_dev *pctldev = s->private;
936 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
937 unsigned func_selector = 0;
938
939 while (pmxops->list_functions(pctldev, func_selector) >= 0) {
940 const char *func = pmxops->get_function_name(pctldev,
941 func_selector);
942 const char * const *groups;
943 unsigned num_groups;
944 int ret;
945 int i;
946
947 ret = pmxops->get_function_groups(pctldev, func_selector,
948 &groups, &num_groups);
949 if (ret)
950 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
951 func);
952
953 seq_printf(s, "function: %s, groups = [ ", func);
954 for (i = 0; i < num_groups; i++)
955 seq_printf(s, "%s ", groups[i]);
956 seq_puts(s, "]\n");
957
958 func_selector++;
959
960 }
961
962 return 0;
963}
964
965static int pinmux_pins_show(struct seq_file *s, void *what)
966{
967 struct pinctrl_dev *pctldev = s->private;
968 unsigned pin;
969
970 seq_puts(s, "Pinmux settings per pin\n");
971 seq_puts(s, "Format: pin (name): pinmuxfunction\n");
972
973 /* The highest pin number need to be included in the loop, thus <= */
974 for (pin = 0; pin <= pctldev->desc->maxpin; pin++) {
975
976 struct pin_desc *desc;
977
978 desc = pin_desc_get(pctldev, pin);
979 /* Pin space may be sparse */
980 if (desc == NULL)
981 continue;
982
983 seq_printf(s, "pin %d (%s): %s\n", pin,
984 desc->name ? desc->name : "unnamed",
5d2eaf80
SW
985 desc->mux_function ? desc->mux_function
986 : "UNCLAIMED");
2744e8af
LW
987 }
988
989 return 0;
990}
991
992static int pinmux_hogs_show(struct seq_file *s, void *what)
993{
994 struct pinctrl_dev *pctldev = s->private;
995 struct pinmux_hog *hog;
996
997 seq_puts(s, "Pinmux map hogs held by device\n");
998
999 list_for_each_entry(hog, &pctldev->pinmux_hogs, node)
1000 seq_printf(s, "%s\n", hog->map->name);
1001
1002 return 0;
1003}
1004
1005static int pinmux_show(struct seq_file *s, void *what)
1006{
1007 struct pinmux *pmx;
1008
1009 seq_puts(s, "Requested pinmuxes and their maps:\n");
1010 list_for_each_entry(pmx, &pinmux_list, node) {
1011 struct pinctrl_dev *pctldev = pmx->pctldev;
1012 const struct pinmux_ops *pmxops;
1013 const struct pinctrl_ops *pctlops;
1014 struct pinmux_group *grp;
1015
1016 if (!pctldev) {
1017 seq_puts(s, "NO PIN CONTROLLER DEVICE\n");
1018 continue;
1019 }
1020
1021 pmxops = pctldev->desc->pmxops;
1022 pctlops = pctldev->desc->pctlops;
1023
1024 seq_printf(s, "device: %s function: %s (%u),",
1025 pinctrl_dev_get_name(pmx->pctldev),
1026 pmxops->get_function_name(pctldev, pmx->func_selector),
1027 pmx->func_selector);
1028
1029 seq_printf(s, " groups: [");
1030 list_for_each_entry(grp, &pmx->groups, node) {
1031 seq_printf(s, " %s (%u)",
1032 pctlops->get_group_name(pctldev, grp->group_selector),
1033 grp->group_selector);
1034 }
1035 seq_printf(s, " ]");
1036
1037 seq_printf(s, " users: %u map-> %s\n",
1038 pmx->usecount,
1039 pmx->dev ? dev_name(pmx->dev) : "(system)");
1040 }
1041
1042 return 0;
1043}
1044
1045static int pinmux_maps_show(struct seq_file *s, void *what)
1046{
1047 int i;
1048
1049 seq_puts(s, "Pinmux maps:\n");
1050
1051 for (i = 0; i < pinmux_maps_num; i++) {
1052 struct pinmux_map const *map = &pinmux_maps[i];
1053
1054 seq_printf(s, "%s:\n", map->name);
1055 if (map->dev || map->dev_name)
1056 seq_printf(s, " device: %s\n",
1057 map->dev ? dev_name(map->dev) :
1058 map->dev_name);
1059 else
1060 seq_printf(s, " SYSTEM MUX\n");
1061 seq_printf(s, " controlling device %s\n",
1062 map->ctrl_dev ? dev_name(map->ctrl_dev) :
1063 map->ctrl_dev_name);
1064 seq_printf(s, " function: %s\n", map->function);
1065 seq_printf(s, " group: %s\n", map->group ? map->group :
1066 "(default)");
1067 }
1068 return 0;
1069}
1070
1071static int pinmux_functions_open(struct inode *inode, struct file *file)
1072{
1073 return single_open(file, pinmux_functions_show, inode->i_private);
1074}
1075
1076static int pinmux_pins_open(struct inode *inode, struct file *file)
1077{
1078 return single_open(file, pinmux_pins_show, inode->i_private);
1079}
1080
1081static int pinmux_hogs_open(struct inode *inode, struct file *file)
1082{
1083 return single_open(file, pinmux_hogs_show, inode->i_private);
1084}
1085
1086static int pinmux_open(struct inode *inode, struct file *file)
1087{
1088 return single_open(file, pinmux_show, NULL);
1089}
1090
1091static int pinmux_maps_open(struct inode *inode, struct file *file)
1092{
1093 return single_open(file, pinmux_maps_show, NULL);
1094}
1095
1096static const struct file_operations pinmux_functions_ops = {
1097 .open = pinmux_functions_open,
1098 .read = seq_read,
1099 .llseek = seq_lseek,
1100 .release = single_release,
1101};
1102
1103static const struct file_operations pinmux_pins_ops = {
1104 .open = pinmux_pins_open,
1105 .read = seq_read,
1106 .llseek = seq_lseek,
1107 .release = single_release,
1108};
1109
1110static const struct file_operations pinmux_hogs_ops = {
1111 .open = pinmux_hogs_open,
1112 .read = seq_read,
1113 .llseek = seq_lseek,
1114 .release = single_release,
1115};
1116
1117static const struct file_operations pinmux_ops = {
1118 .open = pinmux_open,
1119 .read = seq_read,
1120 .llseek = seq_lseek,
1121 .release = single_release,
1122};
1123
1124static const struct file_operations pinmux_maps_ops = {
1125 .open = pinmux_maps_open,
1126 .read = seq_read,
1127 .llseek = seq_lseek,
1128 .release = single_release,
1129};
1130
1131void pinmux_init_device_debugfs(struct dentry *devroot,
1132 struct pinctrl_dev *pctldev)
1133{
1134 debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
1135 devroot, pctldev, &pinmux_functions_ops);
1136 debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
1137 devroot, pctldev, &pinmux_pins_ops);
1138 debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO,
1139 devroot, pctldev, &pinmux_hogs_ops);
1140}
1141
1142void pinmux_init_debugfs(struct dentry *subsys_root)
1143{
1144 debugfs_create_file("pinmuxes", S_IFREG | S_IRUGO,
1145 subsys_root, NULL, &pinmux_ops);
1146 debugfs_create_file("pinmux-maps", S_IFREG | S_IRUGO,
1147 subsys_root, NULL, &pinmux_maps_ops);
1148}
1149
1150#endif /* CONFIG_DEBUG_FS */
This page took 0.097597 seconds and 5 git commands to generate.