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