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