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