blob: 72b760bc880a38acbb4fd31ce7863f4f14a5a1ef [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/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200393 * pinmux_check_pin_group() - check function and pin group combo
394 * @pctldev: device to check the pin group vs function for
395 * @func_selector: the function selector to check the pin group for, we have
396 * already looked this up in the calling function
397 * @pin_group: the pin group to match to the function
398 *
399 * This function will check that the pinmux driver can supply the
400 * selected pin group for a certain function, returns the group selector if
401 * the group and function selector will work fine together, else returns
402 * negative
403 */
404static int pinmux_check_pin_group(struct pinctrl_dev *pctldev,
405 unsigned func_selector,
406 const char *pin_group)
407{
408 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
409 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
410 int ret;
411
412 /*
413 * If the driver does not support different pin groups for the
414 * functions, we only support group 0, and assume this exists.
415 */
416 if (!pctlops || !pctlops->list_groups)
417 return 0;
418
419 /*
420 * Passing NULL (no specific group) will select the first and
421 * hopefully only group of pins available for this function.
422 */
423 if (!pin_group) {
424 char const * const *groups;
425 unsigned num_groups;
426
427 ret = pmxops->get_function_groups(pctldev, func_selector,
428 &groups, &num_groups);
429 if (ret)
430 return ret;
431 if (num_groups < 1)
432 return -EINVAL;
Linus Walleij7afde8b2011-10-19 17:07:16 +0200433 ret = pinctrl_get_group_selector(pctldev, groups[0]);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200434 if (ret < 0) {
435 dev_err(&pctldev->dev,
436 "function %s wants group %s but the pin "
437 "controller does not seem to have that group\n",
438 pmxops->get_function_name(pctldev, func_selector),
439 groups[0]);
440 return ret;
441 }
442
443 if (num_groups > 1)
444 dev_dbg(&pctldev->dev,
445 "function %s support more than one group, "
446 "default-selecting first group %s (%d)\n",
447 pmxops->get_function_name(pctldev, func_selector),
448 groups[0],
449 ret);
450
451 return ret;
452 }
453
454 dev_dbg(&pctldev->dev,
455 "check if we have pin group %s on controller %s\n",
456 pin_group, pinctrl_dev_get_name(pctldev));
457
Linus Walleij7afde8b2011-10-19 17:07:16 +0200458 ret = pinctrl_get_group_selector(pctldev, pin_group);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200459 if (ret < 0) {
460 dev_dbg(&pctldev->dev,
461 "%s does not support pin group %s with function %s\n",
462 pinctrl_dev_get_name(pctldev),
463 pin_group,
464 pmxops->get_function_name(pctldev, func_selector));
465 }
466 return ret;
467}
468
469/**
470 * pinmux_search_function() - check pin control driver for a certain function
471 * @pctldev: device to check for function and position
472 * @map: function map containing the function and position to look for
473 * @func_selector: returns the applicable function selector if found
474 * @group_selector: returns the applicable group selector if found
475 *
476 * This will search the pinmux driver for an applicable
477 * function with a specific pin group, returns 0 if these can be mapped
478 * negative otherwise
479 */
480static int pinmux_search_function(struct pinctrl_dev *pctldev,
481 struct pinmux_map const *map,
482 unsigned *func_selector,
483 unsigned *group_selector)
484{
485 const struct pinmux_ops *ops = pctldev->desc->pmxops;
486 unsigned selector = 0;
487
488 /* See if this pctldev has this function */
489 while (ops->list_functions(pctldev, selector) >= 0) {
490 const char *fname = ops->get_function_name(pctldev,
491 selector);
492 int ret;
493
494 if (!strcmp(map->function, fname)) {
495 /* Found the function, check pin group */
496 ret = pinmux_check_pin_group(pctldev, selector,
497 map->group);
498 if (ret < 0)
499 return ret;
500
501 /* This function and group selector can be used */
502 *func_selector = selector;
503 *group_selector = ret;
504 return 0;
505
506 }
507 selector++;
508 }
509
510 pr_err("%s does not support function %s\n",
511 pinctrl_dev_get_name(pctldev), map->function);
512 return -EINVAL;
513}
514
515/**
516 * pinmux_enable_muxmap() - enable a map entry for a certain pinmux
517 */
518static int pinmux_enable_muxmap(struct pinctrl_dev *pctldev,
519 struct pinmux *pmx,
520 struct device *dev,
521 const char *devname,
522 struct pinmux_map const *map)
523{
524 unsigned func_selector;
525 unsigned group_selector;
526 struct pinmux_group *grp;
527 int ret;
528
529 /*
530 * Note that we're not locking the pinmux mutex here, because
531 * this is only called at pinmux initialization time when it
532 * has not been added to any list and thus is not reachable
533 * by anyone else.
534 */
535
536 if (pmx->pctldev && pmx->pctldev != pctldev) {
537 dev_err(&pctldev->dev,
538 "different pin control devices given for device %s, "
539 "function %s\n",
540 devname,
541 map->function);
542 return -EINVAL;
543 }
544 pmx->dev = dev;
545 pmx->pctldev = pctldev;
546
547 /* Now go into the driver and try to match a function and group */
548 ret = pinmux_search_function(pctldev, map, &func_selector,
549 &group_selector);
550 if (ret < 0)
551 return ret;
552
553 /*
554 * If the function selector is already set, it needs to be identical,
555 * we support several groups with one function but not several
556 * functions with one or several groups in the same pinmux.
557 */
558 if (pmx->func_selector != UINT_MAX &&
559 pmx->func_selector != func_selector) {
560 dev_err(&pctldev->dev,
561 "dual function defines in the map for device %s\n",
562 devname);
563 return -EINVAL;
564 }
565 pmx->func_selector = func_selector;
566
567 /* Now add this group selector, we may have many of them */
568 grp = kmalloc(sizeof(struct pinmux_group), GFP_KERNEL);
569 if (!grp)
570 return -ENOMEM;
571 grp->group_selector = group_selector;
572 ret = acquire_pins(pctldev, func_selector, group_selector);
573 if (ret) {
574 kfree(grp);
575 return ret;
576 }
577 list_add(&grp->node, &pmx->groups);
578
579 return 0;
580}
581
582static void pinmux_free_groups(struct pinmux *pmx)
583{
584 struct list_head *node, *tmp;
585
586 list_for_each_safe(node, tmp, &pmx->groups) {
587 struct pinmux_group *grp =
588 list_entry(node, struct pinmux_group, node);
589 /* Release all pins taken by this group */
590 release_pins(pmx->pctldev, grp->group_selector);
591 list_del(node);
592 kfree(grp);
593 }
594}
595
596/**
597 * pinmux_get() - retrieves the pinmux for a certain device
598 * @dev: the device to get the pinmux for
599 * @name: an optional specific mux mapping name or NULL, the name is only
600 * needed if you want to have more than one mapping per device, or if you
601 * need an anonymous pinmux (not tied to any specific device)
602 */
603struct pinmux *pinmux_get(struct device *dev, const char *name)
604{
605
606 struct pinmux_map const *map = NULL;
607 struct pinctrl_dev *pctldev = NULL;
608 const char *devname = NULL;
609 struct pinmux *pmx;
610 bool found_map;
611 unsigned num_maps = 0;
612 int ret = -ENODEV;
613 int i;
614
615 /* We must have dev or ID or both */
616 if (!dev && !name)
617 return ERR_PTR(-EINVAL);
618
619 if (dev)
620 devname = dev_name(dev);
621
622 pr_debug("get mux %s for device %s\n", name,
623 devname ? devname : "(none)");
624
625 /*
626 * create the state cookie holder struct pinmux for each
627 * mapping, this is what consumers will get when requesting
628 * a pinmux handle with pinmux_get()
629 */
630 pmx = kzalloc(sizeof(struct pinmux), GFP_KERNEL);
631 if (pmx == NULL)
632 return ERR_PTR(-ENOMEM);
633 mutex_init(&pmx->mutex);
634 pmx->func_selector = UINT_MAX;
635 INIT_LIST_HEAD(&pmx->groups);
636
637 /* Iterate over the pinmux maps to locate the right ones */
638 for (i = 0; i < pinmux_maps_num; i++) {
639 map = &pinmux_maps[i];
640 found_map = false;
641
642 /*
643 * First, try to find the pctldev given in the map
644 */
645 pctldev = get_pinctrl_dev_from_dev(map->ctrl_dev,
646 map->ctrl_dev_name);
647 if (!pctldev) {
648 const char *devname = NULL;
649
650 if (map->ctrl_dev)
651 devname = dev_name(map->ctrl_dev);
652 else if (map->ctrl_dev_name)
653 devname = map->ctrl_dev_name;
654
655 pr_warning("could not find a pinctrl device for pinmux "
656 "function %s, fishy, they shall all have one\n",
657 map->function);
658 pr_warning("given pinctrl device name: %s",
659 devname ? devname : "UNDEFINED");
660
661 /* Continue to check the other mappings anyway... */
662 continue;
663 }
664
665 pr_debug("in map, found pctldev %s to handle function %s",
666 dev_name(&pctldev->dev), map->function);
667
668
669 /*
670 * If we're looking for a specific named map, this must match,
671 * else we loop and look for the next.
672 */
673 if (name != NULL) {
674 if (map->name == NULL)
675 continue;
676 if (strcmp(map->name, name))
677 continue;
678 }
679
680 /*
681 * This is for the case where no device name is given, we
682 * already know that the function name matches from above
683 * code.
684 */
685 if (!map->dev_name && (name != NULL))
686 found_map = true;
687
688 /* If the mapping has a device set up it must match */
689 if (map->dev_name &&
690 (!devname || !strcmp(map->dev_name, devname)))
691 /* MATCH! */
692 found_map = true;
693
694 /* If this map is applicable, then apply it */
695 if (found_map) {
696 ret = pinmux_enable_muxmap(pctldev, pmx, dev,
697 devname, map);
698 if (ret) {
699 pinmux_free_groups(pmx);
700 kfree(pmx);
701 return ERR_PTR(ret);
702 }
703 num_maps++;
704 }
705 }
706
707
708 /* We should have atleast one map, right */
709 if (!num_maps) {
710 pr_err("could not find any mux maps for device %s, ID %s\n",
711 devname ? devname : "(anonymous)",
712 name ? name : "(undefined)");
713 kfree(pmx);
714 return ERR_PTR(-EINVAL);
715 }
716
717 pr_debug("found %u mux maps for device %s, UD %s\n",
718 num_maps,
719 devname ? devname : "(anonymous)",
720 name ? name : "(undefined)");
721
722 /* Add the pinmux to the global list */
723 mutex_lock(&pinmux_list_mutex);
724 list_add(&pmx->node, &pinmux_list);
725 mutex_unlock(&pinmux_list_mutex);
726
727 return pmx;
728}
729EXPORT_SYMBOL_GPL(pinmux_get);
730
731/**
732 * pinmux_put() - release a previously claimed pinmux
733 * @pmx: a pinmux previously claimed by pinmux_get()
734 */
735void pinmux_put(struct pinmux *pmx)
736{
737 if (pmx == NULL)
738 return;
739
740 mutex_lock(&pmx->mutex);
741 if (pmx->usecount)
742 pr_warn("releasing pinmux with active users!\n");
743 /* Free the groups and all acquired pins */
744 pinmux_free_groups(pmx);
745 mutex_unlock(&pmx->mutex);
746
747 /* Remove from list */
748 mutex_lock(&pinmux_list_mutex);
749 list_del(&pmx->node);
750 mutex_unlock(&pinmux_list_mutex);
751
752 kfree(pmx);
753}
754EXPORT_SYMBOL_GPL(pinmux_put);
755
756/**
757 * pinmux_enable() - enable a certain pinmux setting
758 * @pmx: the pinmux to enable, previously claimed by pinmux_get()
759 */
760int pinmux_enable(struct pinmux *pmx)
761{
762 int ret = 0;
763
764 if (pmx == NULL)
765 return -EINVAL;
766 mutex_lock(&pmx->mutex);
767 if (pmx->usecount++ == 0) {
768 struct pinctrl_dev *pctldev = pmx->pctldev;
769 const struct pinmux_ops *ops = pctldev->desc->pmxops;
770 struct pinmux_group *grp;
771
772 list_for_each_entry(grp, &pmx->groups, node) {
773 ret = ops->enable(pctldev, pmx->func_selector,
774 grp->group_selector);
775 if (ret) {
776 /*
777 * TODO: call disable() on all groups we called
778 * enable() on to this point?
779 */
780 pmx->usecount--;
781 break;
782 }
783 }
784 }
785 mutex_unlock(&pmx->mutex);
786 return ret;
787}
788EXPORT_SYMBOL_GPL(pinmux_enable);
789
790/**
791 * pinmux_disable() - disable a certain pinmux setting
792 * @pmx: the pinmux to disable, previously claimed by pinmux_get()
793 */
794void pinmux_disable(struct pinmux *pmx)
795{
796 if (pmx == NULL)
797 return;
798
799 mutex_lock(&pmx->mutex);
800 if (--pmx->usecount == 0) {
801 struct pinctrl_dev *pctldev = pmx->pctldev;
802 const struct pinmux_ops *ops = pctldev->desc->pmxops;
803 struct pinmux_group *grp;
804
805 list_for_each_entry(grp, &pmx->groups, node) {
806 ops->disable(pctldev, pmx->func_selector,
807 grp->group_selector);
808 }
809 }
810 mutex_unlock(&pmx->mutex);
811}
812EXPORT_SYMBOL_GPL(pinmux_disable);
813
814int pinmux_check_ops(const struct pinmux_ops *ops)
815{
816 /* Check that we implement required operations */
817 if (!ops->list_functions ||
818 !ops->get_function_name ||
819 !ops->get_function_groups ||
820 !ops->enable ||
821 !ops->disable)
822 return -EINVAL;
823
824 return 0;
825}
826
827/* Hog a single map entry and add to the hoglist */
828static int pinmux_hog_map(struct pinctrl_dev *pctldev,
829 struct pinmux_map const *map)
830{
831 struct pinmux_hog *hog;
832 struct pinmux *pmx;
833 int ret;
834
835 if (map->dev || map->dev_name) {
836 /*
837 * TODO: the day we have device tree support, we can
838 * traverse the device tree and hog to specific device nodes
839 * without any problems, so then we can hog pinmuxes for
840 * all devices that just want a static pin mux at this point.
841 */
842 dev_err(&pctldev->dev, "map %s wants to hog a non-system "
843 "pinmux, this is not going to work\n", map->name);
844 return -EINVAL;
845 }
846
847 hog = kzalloc(sizeof(struct pinmux_hog), GFP_KERNEL);
848 if (!hog)
849 return -ENOMEM;
850
851 pmx = pinmux_get(NULL, map->name);
852 if (IS_ERR(pmx)) {
853 kfree(hog);
854 dev_err(&pctldev->dev,
855 "could not get the %s pinmux mapping for hogging\n",
856 map->name);
857 return PTR_ERR(pmx);
858 }
859
860 ret = pinmux_enable(pmx);
861 if (ret) {
862 pinmux_put(pmx);
863 kfree(hog);
864 dev_err(&pctldev->dev,
865 "could not enable the %s pinmux mapping for hogging\n",
866 map->name);
867 return ret;
868 }
869
870 hog->map = map;
871 hog->pmx = pmx;
872
873 dev_info(&pctldev->dev, "hogged map %s, function %s\n", map->name,
874 map->function);
875 mutex_lock(&pctldev->pinmux_hogs_lock);
876 list_add(&hog->node, &pctldev->pinmux_hogs);
877 mutex_unlock(&pctldev->pinmux_hogs_lock);
878
879 return 0;
880}
881
882/**
883 * pinmux_hog_maps() - hog specific map entries on controller device
884 * @pctldev: the pin control device to hog entries on
885 *
886 * When the pin controllers are registered, there may be some specific pinmux
887 * map entries that need to be hogged, i.e. get+enabled until the system shuts
888 * down.
889 */
890int pinmux_hog_maps(struct pinctrl_dev *pctldev)
891{
892 struct device *dev = &pctldev->dev;
893 const char *devname = dev_name(dev);
894 int ret;
895 int i;
896
897 INIT_LIST_HEAD(&pctldev->pinmux_hogs);
898 mutex_init(&pctldev->pinmux_hogs_lock);
899
900 for (i = 0; i < pinmux_maps_num; i++) {
901 struct pinmux_map const *map = &pinmux_maps[i];
902
903 if (((map->ctrl_dev == dev) ||
904 !strcmp(map->ctrl_dev_name, devname)) &&
905 map->hog_on_boot) {
906 /* OK time to hog! */
907 ret = pinmux_hog_map(pctldev, map);
908 if (ret)
909 return ret;
910 }
911 }
912 return 0;
913}
914
915/**
916 * pinmux_hog_maps() - unhog specific map entries on controller device
917 * @pctldev: the pin control device to unhog entries on
918 */
919void pinmux_unhog_maps(struct pinctrl_dev *pctldev)
920{
921 struct list_head *node, *tmp;
922
923 mutex_lock(&pctldev->pinmux_hogs_lock);
924 list_for_each_safe(node, tmp, &pctldev->pinmux_hogs) {
925 struct pinmux_hog *hog =
926 list_entry(node, struct pinmux_hog, node);
927 pinmux_disable(hog->pmx);
928 pinmux_put(hog->pmx);
929 list_del(node);
930 kfree(hog);
931 }
932 mutex_unlock(&pctldev->pinmux_hogs_lock);
933}
934
935#ifdef CONFIG_DEBUG_FS
936
937/* Called from pincontrol core */
938static int pinmux_functions_show(struct seq_file *s, void *what)
939{
940 struct pinctrl_dev *pctldev = s->private;
941 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
942 unsigned func_selector = 0;
943
944 while (pmxops->list_functions(pctldev, func_selector) >= 0) {
945 const char *func = pmxops->get_function_name(pctldev,
946 func_selector);
947 const char * const *groups;
948 unsigned num_groups;
949 int ret;
950 int i;
951
952 ret = pmxops->get_function_groups(pctldev, func_selector,
953 &groups, &num_groups);
954 if (ret)
955 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
956 func);
957
958 seq_printf(s, "function: %s, groups = [ ", func);
959 for (i = 0; i < num_groups; i++)
960 seq_printf(s, "%s ", groups[i]);
961 seq_puts(s, "]\n");
962
963 func_selector++;
964
965 }
966
967 return 0;
968}
969
970static int pinmux_pins_show(struct seq_file *s, void *what)
971{
972 struct pinctrl_dev *pctldev = s->private;
973 unsigned pin;
974
975 seq_puts(s, "Pinmux settings per pin\n");
976 seq_puts(s, "Format: pin (name): pinmuxfunction\n");
977
978 /* The highest pin number need to be included in the loop, thus <= */
979 for (pin = 0; pin <= pctldev->desc->maxpin; pin++) {
980
981 struct pin_desc *desc;
982
983 desc = pin_desc_get(pctldev, pin);
984 /* Pin space may be sparse */
985 if (desc == NULL)
986 continue;
987
988 seq_printf(s, "pin %d (%s): %s\n", pin,
989 desc->name ? desc->name : "unnamed",
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600990 desc->mux_function ? desc->mux_function
991 : "UNCLAIMED");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200992 }
993
994 return 0;
995}
996
997static int pinmux_hogs_show(struct seq_file *s, void *what)
998{
999 struct pinctrl_dev *pctldev = s->private;
1000 struct pinmux_hog *hog;
1001
1002 seq_puts(s, "Pinmux map hogs held by device\n");
1003
1004 list_for_each_entry(hog, &pctldev->pinmux_hogs, node)
1005 seq_printf(s, "%s\n", hog->map->name);
1006
1007 return 0;
1008}
1009
1010static int pinmux_show(struct seq_file *s, void *what)
1011{
1012 struct pinmux *pmx;
1013
1014 seq_puts(s, "Requested pinmuxes and their maps:\n");
1015 list_for_each_entry(pmx, &pinmux_list, node) {
1016 struct pinctrl_dev *pctldev = pmx->pctldev;
1017 const struct pinmux_ops *pmxops;
1018 const struct pinctrl_ops *pctlops;
1019 struct pinmux_group *grp;
1020
1021 if (!pctldev) {
1022 seq_puts(s, "NO PIN CONTROLLER DEVICE\n");
1023 continue;
1024 }
1025
1026 pmxops = pctldev->desc->pmxops;
1027 pctlops = pctldev->desc->pctlops;
1028
1029 seq_printf(s, "device: %s function: %s (%u),",
1030 pinctrl_dev_get_name(pmx->pctldev),
1031 pmxops->get_function_name(pctldev, pmx->func_selector),
1032 pmx->func_selector);
1033
1034 seq_printf(s, " groups: [");
1035 list_for_each_entry(grp, &pmx->groups, node) {
1036 seq_printf(s, " %s (%u)",
1037 pctlops->get_group_name(pctldev, grp->group_selector),
1038 grp->group_selector);
1039 }
1040 seq_printf(s, " ]");
1041
1042 seq_printf(s, " users: %u map-> %s\n",
1043 pmx->usecount,
1044 pmx->dev ? dev_name(pmx->dev) : "(system)");
1045 }
1046
1047 return 0;
1048}
1049
1050static int pinmux_maps_show(struct seq_file *s, void *what)
1051{
1052 int i;
1053
1054 seq_puts(s, "Pinmux maps:\n");
1055
1056 for (i = 0; i < pinmux_maps_num; i++) {
1057 struct pinmux_map const *map = &pinmux_maps[i];
1058
1059 seq_printf(s, "%s:\n", map->name);
1060 if (map->dev || map->dev_name)
1061 seq_printf(s, " device: %s\n",
1062 map->dev ? dev_name(map->dev) :
1063 map->dev_name);
1064 else
1065 seq_printf(s, " SYSTEM MUX\n");
1066 seq_printf(s, " controlling device %s\n",
1067 map->ctrl_dev ? dev_name(map->ctrl_dev) :
1068 map->ctrl_dev_name);
1069 seq_printf(s, " function: %s\n", map->function);
1070 seq_printf(s, " group: %s\n", map->group ? map->group :
1071 "(default)");
1072 }
1073 return 0;
1074}
1075
1076static int pinmux_functions_open(struct inode *inode, struct file *file)
1077{
1078 return single_open(file, pinmux_functions_show, inode->i_private);
1079}
1080
1081static int pinmux_pins_open(struct inode *inode, struct file *file)
1082{
1083 return single_open(file, pinmux_pins_show, inode->i_private);
1084}
1085
1086static int pinmux_hogs_open(struct inode *inode, struct file *file)
1087{
1088 return single_open(file, pinmux_hogs_show, inode->i_private);
1089}
1090
1091static int pinmux_open(struct inode *inode, struct file *file)
1092{
1093 return single_open(file, pinmux_show, NULL);
1094}
1095
1096static int pinmux_maps_open(struct inode *inode, struct file *file)
1097{
1098 return single_open(file, pinmux_maps_show, NULL);
1099}
1100
1101static const struct file_operations pinmux_functions_ops = {
1102 .open = pinmux_functions_open,
1103 .read = seq_read,
1104 .llseek = seq_lseek,
1105 .release = single_release,
1106};
1107
1108static const struct file_operations pinmux_pins_ops = {
1109 .open = pinmux_pins_open,
1110 .read = seq_read,
1111 .llseek = seq_lseek,
1112 .release = single_release,
1113};
1114
1115static const struct file_operations pinmux_hogs_ops = {
1116 .open = pinmux_hogs_open,
1117 .read = seq_read,
1118 .llseek = seq_lseek,
1119 .release = single_release,
1120};
1121
1122static const struct file_operations pinmux_ops = {
1123 .open = pinmux_open,
1124 .read = seq_read,
1125 .llseek = seq_lseek,
1126 .release = single_release,
1127};
1128
1129static const struct file_operations pinmux_maps_ops = {
1130 .open = pinmux_maps_open,
1131 .read = seq_read,
1132 .llseek = seq_lseek,
1133 .release = single_release,
1134};
1135
1136void pinmux_init_device_debugfs(struct dentry *devroot,
1137 struct pinctrl_dev *pctldev)
1138{
1139 debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
1140 devroot, pctldev, &pinmux_functions_ops);
1141 debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
1142 devroot, pctldev, &pinmux_pins_ops);
1143 debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO,
1144 devroot, pctldev, &pinmux_hogs_ops);
1145}
1146
1147void pinmux_init_debugfs(struct dentry *subsys_root)
1148{
1149 debugfs_create_file("pinmuxes", S_IFREG | S_IRUGO,
1150 subsys_root, NULL, &pinmux_ops);
1151 debugfs_create_file("pinmux-maps", S_IFREG | S_IRUGO,
1152 subsys_root, NULL, &pinmux_maps_ops);
1153}
1154
1155#endif /* CONFIG_DEBUG_FS */