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