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
;
37 unsigned selector
= 0;
39 /* Check that we implement required operations */
41 !ops
->get_functions_count
||
42 !ops
->get_function_name
||
43 !ops
->get_function_groups
||
48 /* Check that all functions registered have names */
49 nfuncs
= ops
->get_functions_count(pctldev
);
50 while (selector
< nfuncs
) {
51 const char *fname
= ops
->get_function_name(pctldev
,
54 dev_err(pctldev
->dev
, "pinmux ops has no name for function%u\n",
64 int pinmux_validate_map(struct pinctrl_map
const *map
, int i
)
66 if (!map
->data
.mux
.function
) {
67 pr_err("failed to register map %s (%d): no function given\n",
76 * pin_request() - request a single pin to be muxed in, typically for GPIO
77 * @pin: the pin number in the global pin space
78 * @owner: a representation of the owner of this pin; typically the device
79 * name that controls its mux function, or the requested GPIO name
80 * @gpio_range: the range matching the GPIO pin if this is a request for a
83 static int pin_request(struct pinctrl_dev
*pctldev
,
84 int pin
, const char *owner
,
85 struct pinctrl_gpio_range
*gpio_range
)
87 struct pin_desc
*desc
;
88 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
91 desc
= pin_desc_get(pctldev
, pin
);
94 "pin is not registered so it cannot be requested\n");
98 dev_dbg(pctldev
->dev
, "request pin %d (%s) for %s\n",
99 pin
, desc
->name
, owner
);
102 /* There's no need to support multiple GPIO requests */
103 if (desc
->gpio_owner
) {
104 dev_err(pctldev
->dev
,
105 "pin already requested\n");
109 desc
->gpio_owner
= owner
;
111 if (desc
->mux_usecount
&& strcmp(desc
->mux_owner
, owner
)) {
112 dev_err(pctldev
->dev
,
113 "pin already requested\n");
117 desc
->mux_usecount
++;
118 if (desc
->mux_usecount
> 1)
121 desc
->mux_owner
= owner
;
124 /* Let each pin increase references to this module */
125 if (!try_module_get(pctldev
->owner
)) {
126 dev_err(pctldev
->dev
,
127 "could not increase module refcount for pin %d\n",
134 * If there is no kind of request function for the pin we just assume
135 * we got it by default and proceed.
137 if (gpio_range
&& ops
->gpio_request_enable
)
138 /* This requests and enables a single GPIO pin */
139 status
= ops
->gpio_request_enable(pctldev
, gpio_range
, pin
);
140 else if (ops
->request
)
141 status
= ops
->request(pctldev
, pin
);
146 dev_err(pctldev
->dev
, "->request on device %s failed for pin %d\n",
147 pctldev
->desc
->name
, pin
);
148 module_put(pctldev
->owner
);
154 desc
->gpio_owner
= NULL
;
156 desc
->mux_usecount
--;
157 if (!desc
->mux_usecount
)
158 desc
->mux_owner
= NULL
;
163 dev_err(pctldev
->dev
, "pin-%d (%s) status %d\n",
170 * pin_free() - release a single muxed in pin so something else can be muxed
171 * @pctldev: pin controller device handling this pin
172 * @pin: the pin to free
173 * @gpio_range: the range matching the GPIO pin if this is a request for a
176 * This function returns a pointer to the previous owner. This is used
177 * for callers that dynamically allocate an owner name so it can be freed
178 * once the pin is free. This is done for GPIO request functions.
180 static const char *pin_free(struct pinctrl_dev
*pctldev
, int pin
,
181 struct pinctrl_gpio_range
*gpio_range
)
183 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
184 struct pin_desc
*desc
;
187 desc
= pin_desc_get(pctldev
, pin
);
189 dev_err(pctldev
->dev
,
190 "pin is not registered so it cannot be freed\n");
195 desc
->mux_usecount
--;
196 if (desc
->mux_usecount
)
201 * If there is no kind of request function for the pin we just assume
202 * we got it by default and proceed.
204 if (gpio_range
&& ops
->gpio_disable_free
)
205 ops
->gpio_disable_free(pctldev
, gpio_range
, pin
);
207 ops
->free(pctldev
, pin
);
210 owner
= desc
->gpio_owner
;
211 desc
->gpio_owner
= NULL
;
213 owner
= desc
->mux_owner
;
214 desc
->mux_owner
= NULL
;
215 desc
->mux_setting
= NULL
;
218 module_put(pctldev
->owner
);
224 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
225 * @pctldev: pin controller device affected
226 * @pin: the pin to mux in for GPIO
227 * @range: the applicable GPIO range
229 int pinmux_request_gpio(struct pinctrl_dev
*pctldev
,
230 struct pinctrl_gpio_range
*range
,
231 unsigned pin
, unsigned gpio
)
237 /* Conjure some name stating what chip and pin this is taken by */
238 snprintf(gpiostr
, 15, "%s:%d", range
->name
, gpio
);
240 owner
= kstrdup(gpiostr
, GFP_KERNEL
);
244 ret
= pin_request(pctldev
, pin
, owner
, range
);
252 * pinmux_free_gpio() - release a pin from GPIO muxing
253 * @pctldev: the pin controller device for the pin
254 * @pin: the affected currently GPIO-muxed in pin
255 * @range: applicable GPIO range
257 void pinmux_free_gpio(struct pinctrl_dev
*pctldev
, unsigned pin
,
258 struct pinctrl_gpio_range
*range
)
262 owner
= pin_free(pctldev
, pin
, range
);
267 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
268 * @pctldev: the pin controller handling this pin
269 * @range: applicable GPIO range
270 * @pin: the affected GPIO pin in this controller
271 * @input: true if we set the pin as input, false for output
273 int pinmux_gpio_direction(struct pinctrl_dev
*pctldev
,
274 struct pinctrl_gpio_range
*range
,
275 unsigned pin
, bool input
)
277 const struct pinmux_ops
*ops
;
280 ops
= pctldev
->desc
->pmxops
;
282 if (ops
->gpio_set_direction
)
283 ret
= ops
->gpio_set_direction(pctldev
, range
, pin
, input
);
290 static int pinmux_func_name_to_selector(struct pinctrl_dev
*pctldev
,
291 const char *function
)
293 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
294 unsigned nfuncs
= ops
->get_functions_count(pctldev
);
295 unsigned selector
= 0;
297 /* See if this pctldev has this function */
298 while (selector
< nfuncs
) {
299 const char *fname
= ops
->get_function_name(pctldev
,
302 if (!strcmp(function
, fname
))
308 pr_err("%s does not support function %s\n",
309 pinctrl_dev_get_name(pctldev
), function
);
313 int pinmux_map_to_setting(struct pinctrl_map
const *map
,
314 struct pinctrl_setting
*setting
)
316 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
317 const struct pinmux_ops
*pmxops
= pctldev
->desc
->pmxops
;
318 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
319 char const * const *groups
;
324 const unsigned *pins
;
328 dev_err(pctldev
->dev
, "does not support mux function\n");
332 setting
->data
.mux
.func
=
333 pinmux_func_name_to_selector(pctldev
, map
->data
.mux
.function
);
334 if (setting
->data
.mux
.func
< 0)
335 return setting
->data
.mux
.func
;
337 ret
= pmxops
->get_function_groups(pctldev
, setting
->data
.mux
.func
,
338 &groups
, &num_groups
);
344 if (map
->data
.mux
.group
) {
346 group
= map
->data
.mux
.group
;
347 for (i
= 0; i
< num_groups
; i
++) {
348 if (!strcmp(group
, groups
[i
])) {
359 setting
->data
.mux
.group
= pinctrl_get_group_selector(pctldev
, group
);
360 if (setting
->data
.mux
.group
< 0)
361 return setting
->data
.mux
.group
;
363 ret
= pctlops
->get_group_pins(pctldev
, setting
->data
.mux
.group
, &pins
,
366 dev_err(pctldev
->dev
,
367 "could not get pins for device %s group selector %d\n",
368 pinctrl_dev_get_name(pctldev
), setting
->data
.mux
.group
);
372 /* Try to allocate all pins in this group, one by one */
373 for (i
= 0; i
< num_pins
; i
++) {
374 ret
= pin_request(pctldev
, pins
[i
], map
->dev_name
, NULL
);
376 dev_err(pctldev
->dev
,
377 "could not get request pin %d on device %s\n",
378 pins
[i
], pinctrl_dev_get_name(pctldev
));
379 /* On error release all taken pins */
380 i
--; /* this pin just failed */
382 pin_free(pctldev
, pins
[i
], NULL
);
390 void pinmux_free_setting(struct pinctrl_setting
const *setting
)
392 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
393 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
394 const unsigned *pins
;
399 ret
= pctlops
->get_group_pins(pctldev
, setting
->data
.mux
.group
,
402 dev_err(pctldev
->dev
,
403 "could not get pins for device %s group selector %d\n",
404 pinctrl_dev_get_name(pctldev
), setting
->data
.mux
.group
);
408 for (i
= 0; i
< num_pins
; i
++)
409 pin_free(pctldev
, pins
[i
], NULL
);
412 int pinmux_enable_setting(struct pinctrl_setting
const *setting
)
414 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
415 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
416 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
418 const unsigned *pins
;
421 struct pin_desc
*desc
;
423 ret
= pctlops
->get_group_pins(pctldev
, setting
->data
.mux
.group
,
426 /* errors only affect debug data, so just warn */
427 dev_warn(pctldev
->dev
,
428 "could not get pins for group selector %d\n",
429 setting
->data
.mux
.group
);
433 for (i
= 0; i
< num_pins
; i
++) {
434 desc
= pin_desc_get(pctldev
, pins
[i
]);
436 dev_warn(pctldev
->dev
,
437 "could not get pin desc for pin %d\n",
441 desc
->mux_setting
= &(setting
->data
.mux
);
444 return ops
->enable(pctldev
, setting
->data
.mux
.func
,
445 setting
->data
.mux
.group
);
448 void pinmux_disable_setting(struct pinctrl_setting
const *setting
)
450 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
451 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
452 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
454 const unsigned *pins
;
457 struct pin_desc
*desc
;
459 ret
= pctlops
->get_group_pins(pctldev
, setting
->data
.mux
.group
,
462 /* errors only affect debug data, so just warn */
463 dev_warn(pctldev
->dev
,
464 "could not get pins for group selector %d\n",
465 setting
->data
.mux
.group
);
469 for (i
= 0; i
< num_pins
; i
++) {
470 desc
= pin_desc_get(pctldev
, pins
[i
]);
472 dev_warn(pctldev
->dev
,
473 "could not get pin desc for pin %d\n",
477 desc
->mux_setting
= NULL
;
480 ops
->disable(pctldev
, setting
->data
.mux
.func
, setting
->data
.mux
.group
);
483 #ifdef CONFIG_DEBUG_FS
485 /* Called from pincontrol core */
486 static int pinmux_functions_show(struct seq_file
*s
, void *what
)
488 struct pinctrl_dev
*pctldev
= s
->private;
489 const struct pinmux_ops
*pmxops
= pctldev
->desc
->pmxops
;
491 unsigned func_selector
= 0;
496 mutex_lock(&pinctrl_mutex
);
497 nfuncs
= pmxops
->get_functions_count(pctldev
);
498 while (func_selector
< nfuncs
) {
499 const char *func
= pmxops
->get_function_name(pctldev
,
501 const char * const *groups
;
506 ret
= pmxops
->get_function_groups(pctldev
, func_selector
,
507 &groups
, &num_groups
);
509 seq_printf(s
, "function %s: COULD NOT GET GROUPS\n",
512 seq_printf(s
, "function: %s, groups = [ ", func
);
513 for (i
= 0; i
< num_groups
; i
++)
514 seq_printf(s
, "%s ", groups
[i
]);
520 mutex_unlock(&pinctrl_mutex
);
525 static int pinmux_pins_show(struct seq_file
*s
, void *what
)
527 struct pinctrl_dev
*pctldev
= s
->private;
528 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
529 const struct pinmux_ops
*pmxops
= pctldev
->desc
->pmxops
;
535 seq_puts(s
, "Pinmux settings per pin\n");
536 seq_puts(s
, "Format: pin (name): mux_owner gpio_owner hog?\n");
538 mutex_lock(&pinctrl_mutex
);
540 /* The pin number can be retrived from the pin controller descriptor */
541 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
542 struct pin_desc
*desc
;
545 pin
= pctldev
->desc
->pins
[i
].number
;
546 desc
= pin_desc_get(pctldev
, pin
);
547 /* Skip if we cannot search the pin */
551 if (desc
->mux_owner
&&
552 !strcmp(desc
->mux_owner
, pinctrl_dev_get_name(pctldev
)))
555 seq_printf(s
, "pin %d (%s): %s %s%s", pin
,
556 desc
->name
? desc
->name
: "unnamed",
557 desc
->mux_owner
? desc
->mux_owner
559 desc
->gpio_owner
? desc
->gpio_owner
560 : "(GPIO UNCLAIMED)",
561 is_hog
? " (HOG)" : "");
563 if (desc
->mux_setting
)
564 seq_printf(s
, " function %s group %s\n",
565 pmxops
->get_function_name(pctldev
,
566 desc
->mux_setting
->func
),
567 pctlops
->get_group_name(pctldev
,
568 desc
->mux_setting
->group
));
573 mutex_unlock(&pinctrl_mutex
);
578 void pinmux_show_map(struct seq_file
*s
, struct pinctrl_map
const *map
)
580 seq_printf(s
, "group %s\nfunction %s\n",
581 map
->data
.mux
.group
? map
->data
.mux
.group
: "(default)",
582 map
->data
.mux
.function
);
585 void pinmux_show_setting(struct seq_file
*s
,
586 struct pinctrl_setting
const *setting
)
588 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
589 const struct pinmux_ops
*pmxops
= pctldev
->desc
->pmxops
;
590 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
592 seq_printf(s
, "group: %s (%u) function: %s (%u)\n",
593 pctlops
->get_group_name(pctldev
, setting
->data
.mux
.group
),
594 setting
->data
.mux
.group
,
595 pmxops
->get_function_name(pctldev
, setting
->data
.mux
.func
),
596 setting
->data
.mux
.func
);
599 static int pinmux_functions_open(struct inode
*inode
, struct file
*file
)
601 return single_open(file
, pinmux_functions_show
, inode
->i_private
);
604 static int pinmux_pins_open(struct inode
*inode
, struct file
*file
)
606 return single_open(file
, pinmux_pins_show
, inode
->i_private
);
609 static const struct file_operations pinmux_functions_ops
= {
610 .open
= pinmux_functions_open
,
613 .release
= single_release
,
616 static const struct file_operations pinmux_pins_ops
= {
617 .open
= pinmux_pins_open
,
620 .release
= single_release
,
623 void pinmux_init_device_debugfs(struct dentry
*devroot
,
624 struct pinctrl_dev
*pctldev
)
626 debugfs_create_file("pinmux-functions", S_IFREG
| S_IRUGO
,
627 devroot
, pctldev
, &pinmux_functions_ops
);
628 debugfs_create_file("pinmux-pins", S_IFREG
| S_IRUGO
,
629 devroot
, pctldev
, &pinmux_pins_ops
);
632 #endif /* CONFIG_DEBUG_FS */