2 * Core driver for the pin muxing portions of the pin control subsystem
4 * Copyright (C) 2011-2012 ST-Ericsson SA
5 * Written on behalf of Linaro for ST-Ericsson
6 * Based on bits of regulator core, gpio core and clk core
8 * Author: Linus Walleij <linus.walleij@linaro.org>
10 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
12 * License terms: GNU General Public License (GPL) version 2
14 #define pr_fmt(fmt) "pinmux core: " fmt
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/device.h>
20 #include <linux/slab.h>
21 #include <linux/radix-tree.h>
22 #include <linux/err.h>
23 #include <linux/list.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>
33 int pinmux_check_ops(struct pinctrl_dev
*pctldev
)
35 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
36 unsigned selector
= 0;
38 /* Check that we implement required operations */
39 if (!ops
->list_functions
||
40 !ops
->get_function_name
||
41 !ops
->get_function_groups
||
46 /* Check that all functions registered have names */
47 while (ops
->list_functions(pctldev
, selector
) >= 0) {
48 const char *fname
= ops
->get_function_name(pctldev
,
51 pr_err("pinmux ops has no name for function%u\n",
61 int pinmux_validate_map(struct pinctrl_map
const *map
, int i
)
63 if (!map
->data
.mux
.function
) {
64 pr_err("failed to register map %s (%d): no function given\n",
73 * pin_request() - request a single pin to be muxed in, typically for GPIO
74 * @pin: the pin number in the global pin space
75 * @owner: a representation of the owner of this pin; typically the device
76 * name that controls its mux function, or the requested GPIO name
77 * @gpio_range: the range matching the GPIO pin if this is a request for a
80 static int pin_request(struct pinctrl_dev
*pctldev
,
81 int pin
, const char *owner
,
82 struct pinctrl_gpio_range
*gpio_range
)
84 struct pin_desc
*desc
;
85 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
88 dev_dbg(pctldev
->dev
, "request pin %d for %s\n", pin
, owner
);
90 desc
= pin_desc_get(pctldev
, pin
);
93 "pin is not registered so it cannot be requested\n");
98 /* There's no need to support multiple GPIO requests */
99 if (desc
->gpio_owner
) {
100 dev_err(pctldev
->dev
,
101 "pin already requested\n");
105 desc
->gpio_owner
= owner
;
107 if (desc
->mux_usecount
&& strcmp(desc
->mux_owner
, owner
)) {
108 dev_err(pctldev
->dev
,
109 "pin already requested\n");
113 desc
->mux_usecount
++;
114 if (desc
->mux_usecount
> 1)
117 desc
->mux_owner
= owner
;
120 /* Let each pin increase references to this module */
121 if (!try_module_get(pctldev
->owner
)) {
122 dev_err(pctldev
->dev
,
123 "could not increase module refcount for pin %d\n",
130 * If there is no kind of request function for the pin we just assume
131 * we got it by default and proceed.
133 if (gpio_range
&& ops
->gpio_request_enable
)
134 /* This requests and enables a single GPIO pin */
135 status
= ops
->gpio_request_enable(pctldev
, gpio_range
, pin
);
136 else if (ops
->request
)
137 status
= ops
->request(pctldev
, pin
);
142 dev_err(pctldev
->dev
, "->request on device %s failed for pin %d\n",
143 pctldev
->desc
->name
, pin
);
144 module_put(pctldev
->owner
);
150 desc
->gpio_owner
= NULL
;
152 desc
->mux_usecount
--;
153 if (!desc
->mux_usecount
)
154 desc
->mux_owner
= NULL
;
159 dev_err(pctldev
->dev
, "pin-%d (%s) status %d\n",
166 * pin_free() - release a single muxed in pin so something else can be muxed
167 * @pctldev: pin controller device handling this pin
168 * @pin: the pin to free
169 * @gpio_range: the range matching the GPIO pin if this is a request for a
172 * This function returns a pointer to the previous owner. This is used
173 * for callers that dynamically allocate an owner name so it can be freed
174 * once the pin is free. This is done for GPIO request functions.
176 static const char *pin_free(struct pinctrl_dev
*pctldev
, int pin
,
177 struct pinctrl_gpio_range
*gpio_range
)
179 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
180 struct pin_desc
*desc
;
183 desc
= pin_desc_get(pctldev
, pin
);
185 dev_err(pctldev
->dev
,
186 "pin is not registered so it cannot be freed\n");
191 desc
->mux_usecount
--;
192 if (desc
->mux_usecount
)
197 * If there is no kind of request function for the pin we just assume
198 * we got it by default and proceed.
200 if (gpio_range
&& ops
->gpio_disable_free
)
201 ops
->gpio_disable_free(pctldev
, gpio_range
, pin
);
203 ops
->free(pctldev
, pin
);
206 owner
= desc
->gpio_owner
;
207 desc
->gpio_owner
= NULL
;
209 owner
= desc
->mux_owner
;
210 desc
->mux_owner
= NULL
;
211 desc
->mux_setting
= NULL
;
214 module_put(pctldev
->owner
);
220 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
221 * @pctldev: pin controller device affected
222 * @pin: the pin to mux in for GPIO
223 * @range: the applicable GPIO range
225 int pinmux_request_gpio(struct pinctrl_dev
*pctldev
,
226 struct pinctrl_gpio_range
*range
,
227 unsigned pin
, unsigned gpio
)
233 /* Conjure some name stating what chip and pin this is taken by */
234 snprintf(gpiostr
, 15, "%s:%d", range
->name
, gpio
);
236 owner
= kstrdup(gpiostr
, GFP_KERNEL
);
240 ret
= pin_request(pctldev
, pin
, owner
, range
);
248 * pinmux_free_gpio() - release a pin from GPIO muxing
249 * @pctldev: the pin controller device for the pin
250 * @pin: the affected currently GPIO-muxed in pin
251 * @range: applicable GPIO range
253 void pinmux_free_gpio(struct pinctrl_dev
*pctldev
, unsigned pin
,
254 struct pinctrl_gpio_range
*range
)
258 owner
= pin_free(pctldev
, pin
, range
);
263 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
264 * @pctldev: the pin controller handling this pin
265 * @range: applicable GPIO range
266 * @pin: the affected GPIO pin in this controller
267 * @input: true if we set the pin as input, false for output
269 int pinmux_gpio_direction(struct pinctrl_dev
*pctldev
,
270 struct pinctrl_gpio_range
*range
,
271 unsigned pin
, bool input
)
273 const struct pinmux_ops
*ops
;
276 ops
= pctldev
->desc
->pmxops
;
278 if (ops
->gpio_set_direction
)
279 ret
= ops
->gpio_set_direction(pctldev
, range
, pin
, input
);
286 static int pinmux_func_name_to_selector(struct pinctrl_dev
*pctldev
,
287 const char *function
)
289 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
290 unsigned selector
= 0;
292 /* See if this pctldev has this function */
293 while (ops
->list_functions(pctldev
, selector
) >= 0) {
294 const char *fname
= ops
->get_function_name(pctldev
,
297 if (!strcmp(function
, fname
))
303 pr_err("%s does not support function %s\n",
304 pinctrl_dev_get_name(pctldev
), function
);
308 int pinmux_map_to_setting(struct pinctrl_map
const *map
,
309 struct pinctrl_setting
*setting
)
311 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
312 const struct pinmux_ops
*pmxops
= pctldev
->desc
->pmxops
;
313 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
314 char const * const *groups
;
319 const unsigned *pins
;
322 setting
->data
.mux
.func
=
323 pinmux_func_name_to_selector(pctldev
, map
->data
.mux
.function
);
324 if (setting
->data
.mux
.func
< 0)
325 return setting
->data
.mux
.func
;
327 ret
= pmxops
->get_function_groups(pctldev
, setting
->data
.mux
.func
,
328 &groups
, &num_groups
);
334 if (map
->data
.mux
.group
) {
336 group
= map
->data
.mux
.group
;
337 for (i
= 0; i
< num_groups
; i
++) {
338 if (!strcmp(group
, groups
[i
])) {
349 setting
->data
.mux
.group
= pinctrl_get_group_selector(pctldev
, group
);
350 if (setting
->data
.mux
.group
< 0)
351 return setting
->data
.mux
.group
;
353 ret
= pctlops
->get_group_pins(pctldev
, setting
->data
.mux
.group
, &pins
,
356 dev_err(pctldev
->dev
,
357 "could not get pins for device %s group selector %d\n",
358 pinctrl_dev_get_name(pctldev
), setting
->data
.mux
.group
);
362 /* Try to allocate all pins in this group, one by one */
363 for (i
= 0; i
< num_pins
; i
++) {
364 ret
= pin_request(pctldev
, pins
[i
], map
->dev_name
, NULL
);
366 dev_err(pctldev
->dev
,
367 "could not get request pin %d on device %s\n",
368 pins
[i
], pinctrl_dev_get_name(pctldev
));
369 /* On error release all taken pins */
370 i
--; /* this pin just failed */
372 pin_free(pctldev
, pins
[i
], NULL
);
380 void pinmux_free_setting(struct pinctrl_setting
const *setting
)
382 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
383 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
384 const unsigned *pins
;
389 ret
= pctlops
->get_group_pins(pctldev
, setting
->data
.mux
.group
,
392 dev_err(pctldev
->dev
,
393 "could not get pins for device %s group selector %d\n",
394 pinctrl_dev_get_name(pctldev
), setting
->data
.mux
.group
);
398 for (i
= 0; i
< num_pins
; i
++)
399 pin_free(pctldev
, pins
[i
], NULL
);
402 int pinmux_enable_setting(struct pinctrl_setting
const *setting
)
404 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
405 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
406 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
408 const unsigned *pins
;
411 struct pin_desc
*desc
;
413 ret
= pctlops
->get_group_pins(pctldev
, setting
->data
.mux
.group
,
416 /* errors only affect debug data, so just warn */
417 dev_warn(pctldev
->dev
,
418 "could not get pins for group selector %d\n",
419 setting
->data
.mux
.group
);
423 for (i
= 0; i
< num_pins
; i
++) {
424 desc
= pin_desc_get(pctldev
, pins
[i
]);
426 dev_warn(pctldev
->dev
,
427 "could not get pin desc for pin %d\n",
431 desc
->mux_setting
= &(setting
->data
.mux
);
434 return ops
->enable(pctldev
, setting
->data
.mux
.func
,
435 setting
->data
.mux
.group
);
438 void pinmux_disable_setting(struct pinctrl_setting
const *setting
)
440 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
441 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
442 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
444 const unsigned *pins
;
447 struct pin_desc
*desc
;
449 ret
= pctlops
->get_group_pins(pctldev
, setting
->data
.mux
.group
,
452 /* errors only affect debug data, so just warn */
453 dev_warn(pctldev
->dev
,
454 "could not get pins for group selector %d\n",
455 setting
->data
.mux
.group
);
459 for (i
= 0; i
< num_pins
; i
++) {
460 desc
= pin_desc_get(pctldev
, pins
[i
]);
462 dev_warn(pctldev
->dev
,
463 "could not get pin desc for pin %d\n",
467 desc
->mux_setting
= NULL
;
470 ops
->disable(pctldev
, setting
->data
.mux
.func
, setting
->data
.mux
.group
);
473 #ifdef CONFIG_DEBUG_FS
475 /* Called from pincontrol core */
476 static int pinmux_functions_show(struct seq_file
*s
, void *what
)
478 struct pinctrl_dev
*pctldev
= s
->private;
479 const struct pinmux_ops
*pmxops
= pctldev
->desc
->pmxops
;
480 unsigned func_selector
= 0;
482 mutex_lock(&pinctrl_mutex
);
484 while (pmxops
->list_functions(pctldev
, func_selector
) >= 0) {
485 const char *func
= pmxops
->get_function_name(pctldev
,
487 const char * const *groups
;
492 ret
= pmxops
->get_function_groups(pctldev
, func_selector
,
493 &groups
, &num_groups
);
495 seq_printf(s
, "function %s: COULD NOT GET GROUPS\n",
498 seq_printf(s
, "function: %s, groups = [ ", func
);
499 for (i
= 0; i
< num_groups
; i
++)
500 seq_printf(s
, "%s ", groups
[i
]);
506 mutex_unlock(&pinctrl_mutex
);
511 static int pinmux_pins_show(struct seq_file
*s
, void *what
)
513 struct pinctrl_dev
*pctldev
= s
->private;
514 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
515 const struct pinmux_ops
*pmxops
= pctldev
->desc
->pmxops
;
518 seq_puts(s
, "Pinmux settings per pin\n");
519 seq_puts(s
, "Format: pin (name): mux_owner gpio_owner hog?\n");
521 mutex_lock(&pinctrl_mutex
);
523 /* The pin number can be retrived from the pin controller descriptor */
524 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
525 struct pin_desc
*desc
;
528 pin
= pctldev
->desc
->pins
[i
].number
;
529 desc
= pin_desc_get(pctldev
, pin
);
530 /* Skip if we cannot search the pin */
534 if (desc
->mux_owner
&&
535 !strcmp(desc
->mux_owner
, pinctrl_dev_get_name(pctldev
)))
538 seq_printf(s
, "pin %d (%s): %s %s%s", pin
,
539 desc
->name
? desc
->name
: "unnamed",
540 desc
->mux_owner
? desc
->mux_owner
542 desc
->gpio_owner
? desc
->gpio_owner
543 : "(GPIO UNCLAIMED)",
544 is_hog
? " (HOG)" : "");
546 if (desc
->mux_setting
)
547 seq_printf(s
, " function %s group %s\n",
548 pmxops
->get_function_name(pctldev
,
549 desc
->mux_setting
->func
),
550 pctlops
->get_group_name(pctldev
,
551 desc
->mux_setting
->group
));
556 mutex_unlock(&pinctrl_mutex
);
561 void pinmux_show_map(struct seq_file
*s
, struct pinctrl_map
const *map
)
563 seq_printf(s
, "group %s\nfunction %s\n",
564 map
->data
.mux
.group
? map
->data
.mux
.group
: "(default)",
565 map
->data
.mux
.function
);
568 void pinmux_show_setting(struct seq_file
*s
,
569 struct pinctrl_setting
const *setting
)
571 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
572 const struct pinmux_ops
*pmxops
= pctldev
->desc
->pmxops
;
573 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
575 seq_printf(s
, "group: %s (%u) function: %s (%u)\n",
576 pctlops
->get_group_name(pctldev
, setting
->data
.mux
.group
),
577 setting
->data
.mux
.group
,
578 pmxops
->get_function_name(pctldev
, setting
->data
.mux
.func
),
579 setting
->data
.mux
.func
);
582 static int pinmux_functions_open(struct inode
*inode
, struct file
*file
)
584 return single_open(file
, pinmux_functions_show
, inode
->i_private
);
587 static int pinmux_pins_open(struct inode
*inode
, struct file
*file
)
589 return single_open(file
, pinmux_pins_show
, inode
->i_private
);
592 static const struct file_operations pinmux_functions_ops
= {
593 .open
= pinmux_functions_open
,
596 .release
= single_release
,
599 static const struct file_operations pinmux_pins_ops
= {
600 .open
= pinmux_pins_open
,
603 .release
= single_release
,
606 void pinmux_init_device_debugfs(struct dentry
*devroot
,
607 struct pinctrl_dev
*pctldev
)
609 debugfs_create_file("pinmux-functions", S_IFREG
| S_IRUGO
,
610 devroot
, pctldev
, &pinmux_functions_ops
);
611 debugfs_create_file("pinmux-pins", S_IFREG
| S_IRUGO
,
612 devroot
, pctldev
, &pinmux_pins_ops
);
615 #endif /* CONFIG_DEBUG_FS */