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