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");
97 if (desc
->usecount
&& strcmp(desc
->owner
, owner
)) {
99 "pin already requested\n");
104 if (desc
->usecount
> 1)
109 /* Let each pin increase references to this module */
110 if (!try_module_get(pctldev
->owner
)) {
111 dev_err(pctldev
->dev
,
112 "could not increase module refcount for pin %d\n",
119 * If there is no kind of request function for the pin we just assume
120 * we got it by default and proceed.
122 if (gpio_range
&& ops
->gpio_request_enable
)
123 /* This requests and enables a single GPIO pin */
124 status
= ops
->gpio_request_enable(pctldev
, gpio_range
, pin
);
125 else if (ops
->request
)
126 status
= ops
->request(pctldev
, pin
);
131 dev_err(pctldev
->dev
, "->request on device %s failed for pin %d\n",
132 pctldev
->desc
->name
, pin
);
133 module_put(pctldev
->owner
);
144 dev_err(pctldev
->dev
, "pin-%d (%s) status %d\n",
151 * pin_free() - release a single muxed in pin so something else can be muxed
152 * @pctldev: pin controller device handling this pin
153 * @pin: the pin to free
154 * @gpio_range: the range matching the GPIO pin if this is a request for a
157 * This function returns a pointer to the previous owner. This is used
158 * for callers that dynamically allocate an owner name so it can be freed
159 * once the pin is free. This is done for GPIO request functions.
161 static const char *pin_free(struct pinctrl_dev
*pctldev
, int pin
,
162 struct pinctrl_gpio_range
*gpio_range
)
164 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
165 struct pin_desc
*desc
;
168 desc
= pin_desc_get(pctldev
, pin
);
170 dev_err(pctldev
->dev
,
171 "pin is not registered so it cannot be freed\n");
180 * If there is no kind of request function for the pin we just assume
181 * we got it by default and proceed.
183 if (gpio_range
&& ops
->gpio_disable_free
)
184 ops
->gpio_disable_free(pctldev
, gpio_range
, pin
);
186 ops
->free(pctldev
, pin
);
190 module_put(pctldev
->owner
);
196 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
197 * @pctldev: pin controller device affected
198 * @pin: the pin to mux in for GPIO
199 * @range: the applicable GPIO range
201 int pinmux_request_gpio(struct pinctrl_dev
*pctldev
,
202 struct pinctrl_gpio_range
*range
,
203 unsigned pin
, unsigned gpio
)
209 /* Conjure some name stating what chip and pin this is taken by */
210 snprintf(gpiostr
, 15, "%s:%d", range
->name
, gpio
);
212 owner
= kstrdup(gpiostr
, GFP_KERNEL
);
216 ret
= pin_request(pctldev
, pin
, owner
, range
);
224 * pinmux_free_gpio() - release a pin from GPIO muxing
225 * @pctldev: the pin controller device for the pin
226 * @pin: the affected currently GPIO-muxed in pin
227 * @range: applicable GPIO range
229 void pinmux_free_gpio(struct pinctrl_dev
*pctldev
, unsigned pin
,
230 struct pinctrl_gpio_range
*range
)
234 owner
= pin_free(pctldev
, pin
, range
);
239 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
240 * @pctldev: the pin controller handling this pin
241 * @range: applicable GPIO range
242 * @pin: the affected GPIO pin in this controller
243 * @input: true if we set the pin as input, false for output
245 int pinmux_gpio_direction(struct pinctrl_dev
*pctldev
,
246 struct pinctrl_gpio_range
*range
,
247 unsigned pin
, bool input
)
249 const struct pinmux_ops
*ops
;
252 ops
= pctldev
->desc
->pmxops
;
254 if (ops
->gpio_set_direction
)
255 ret
= ops
->gpio_set_direction(pctldev
, range
, pin
, input
);
262 static int pinmux_func_name_to_selector(struct pinctrl_dev
*pctldev
,
263 const char *function
)
265 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
266 unsigned selector
= 0;
268 /* See if this pctldev has this function */
269 while (ops
->list_functions(pctldev
, selector
) >= 0) {
270 const char *fname
= ops
->get_function_name(pctldev
,
273 if (!strcmp(function
, fname
))
279 pr_err("%s does not support function %s\n",
280 pinctrl_dev_get_name(pctldev
), function
);
284 int pinmux_map_to_setting(struct pinctrl_map
const *map
,
285 struct pinctrl_setting
*setting
)
287 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
288 const struct pinmux_ops
*pmxops
= pctldev
->desc
->pmxops
;
289 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
290 char const * const *groups
;
295 const unsigned *pins
;
298 setting
->data
.mux
.func
=
299 pinmux_func_name_to_selector(pctldev
, map
->data
.mux
.function
);
300 if (setting
->data
.mux
.func
< 0)
301 return setting
->data
.mux
.func
;
303 ret
= pmxops
->get_function_groups(pctldev
, setting
->data
.mux
.func
,
304 &groups
, &num_groups
);
310 if (map
->data
.mux
.group
) {
312 group
= map
->data
.mux
.group
;
313 for (i
= 0; i
< num_groups
; i
++) {
314 if (!strcmp(group
, groups
[i
])) {
325 setting
->data
.mux
.group
= pinctrl_get_group_selector(pctldev
, group
);
326 if (setting
->data
.mux
.group
< 0)
327 return setting
->data
.mux
.group
;
329 ret
= pctlops
->get_group_pins(pctldev
, setting
->data
.mux
.group
, &pins
,
332 dev_err(pctldev
->dev
,
333 "could not get pins for device %s group selector %d\n",
334 pinctrl_dev_get_name(pctldev
), setting
->data
.mux
.group
);
338 /* Try to allocate all pins in this group, one by one */
339 for (i
= 0; i
< num_pins
; i
++) {
340 ret
= pin_request(pctldev
, pins
[i
], map
->dev_name
, NULL
);
342 dev_err(pctldev
->dev
,
343 "could not get request pin %d on device %s\n",
344 pins
[i
], pinctrl_dev_get_name(pctldev
));
345 /* On error release all taken pins */
346 i
--; /* this pin just failed */
348 pin_free(pctldev
, pins
[i
], NULL
);
356 void pinmux_free_setting(struct pinctrl_setting
const *setting
)
358 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
359 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
360 const unsigned *pins
;
365 ret
= pctlops
->get_group_pins(pctldev
, setting
->data
.mux
.group
,
368 dev_err(pctldev
->dev
,
369 "could not get pins for device %s group selector %d\n",
370 pinctrl_dev_get_name(pctldev
), setting
->data
.mux
.group
);
374 for (i
= 0; i
< num_pins
; i
++)
375 pin_free(pctldev
, pins
[i
], NULL
);
378 int pinmux_enable_setting(struct pinctrl_setting
const *setting
)
380 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
381 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
383 return ops
->enable(pctldev
, setting
->data
.mux
.func
,
384 setting
->data
.mux
.group
);
387 void pinmux_disable_setting(struct pinctrl_setting
const *setting
)
389 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
390 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
392 ops
->disable(pctldev
, setting
->data
.mux
.func
, setting
->data
.mux
.group
);
395 #ifdef CONFIG_DEBUG_FS
397 /* Called from pincontrol core */
398 static int pinmux_functions_show(struct seq_file
*s
, void *what
)
400 struct pinctrl_dev
*pctldev
= s
->private;
401 const struct pinmux_ops
*pmxops
= pctldev
->desc
->pmxops
;
402 unsigned func_selector
= 0;
404 mutex_lock(&pinctrl_mutex
);
406 while (pmxops
->list_functions(pctldev
, func_selector
) >= 0) {
407 const char *func
= pmxops
->get_function_name(pctldev
,
409 const char * const *groups
;
414 ret
= pmxops
->get_function_groups(pctldev
, func_selector
,
415 &groups
, &num_groups
);
417 seq_printf(s
, "function %s: COULD NOT GET GROUPS\n",
420 seq_printf(s
, "function: %s, groups = [ ", func
);
421 for (i
= 0; i
< num_groups
; i
++)
422 seq_printf(s
, "%s ", groups
[i
]);
428 mutex_unlock(&pinctrl_mutex
);
433 static int pinmux_pins_show(struct seq_file
*s
, void *what
)
435 struct pinctrl_dev
*pctldev
= s
->private;
438 seq_puts(s
, "Pinmux settings per pin\n");
439 seq_puts(s
, "Format: pin (name): owner\n");
441 mutex_lock(&pinctrl_mutex
);
443 /* The pin number can be retrived from the pin controller descriptor */
444 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
445 struct pin_desc
*desc
;
448 pin
= pctldev
->desc
->pins
[i
].number
;
449 desc
= pin_desc_get(pctldev
, pin
);
450 /* Skip if we cannot search the pin */
455 !strcmp(desc
->owner
, pinctrl_dev_get_name(pctldev
)))
458 seq_printf(s
, "pin %d (%s): %s%s\n", pin
,
459 desc
->name
? desc
->name
: "unnamed",
460 desc
->owner
? desc
->owner
: "UNCLAIMED",
461 is_hog
? " (HOG)" : "");
464 mutex_unlock(&pinctrl_mutex
);
469 void pinmux_show_map(struct seq_file
*s
, struct pinctrl_map
const *map
)
471 seq_printf(s
, "group %s\nfunction %s\n",
472 map
->data
.mux
.group
? map
->data
.mux
.group
: "(default)",
473 map
->data
.mux
.function
);
476 void pinmux_show_setting(struct seq_file
*s
,
477 struct pinctrl_setting
const *setting
)
479 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
480 const struct pinmux_ops
*pmxops
= pctldev
->desc
->pmxops
;
481 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
483 seq_printf(s
, "group: %s (%u) function: %s (%u)\n",
484 pctlops
->get_group_name(pctldev
, setting
->data
.mux
.group
),
485 setting
->data
.mux
.group
,
486 pmxops
->get_function_name(pctldev
, setting
->data
.mux
.func
),
487 setting
->data
.mux
.func
);
490 static int pinmux_functions_open(struct inode
*inode
, struct file
*file
)
492 return single_open(file
, pinmux_functions_show
, inode
->i_private
);
495 static int pinmux_pins_open(struct inode
*inode
, struct file
*file
)
497 return single_open(file
, pinmux_pins_show
, inode
->i_private
);
500 static const struct file_operations pinmux_functions_ops
= {
501 .open
= pinmux_functions_open
,
504 .release
= single_release
,
507 static const struct file_operations pinmux_pins_ops
= {
508 .open
= pinmux_pins_open
,
511 .release
= single_release
,
514 void pinmux_init_device_debugfs(struct dentry
*devroot
,
515 struct pinctrl_dev
*pctldev
)
517 debugfs_create_file("pinmux-functions", S_IFREG
| S_IRUGO
,
518 devroot
, pctldev
, &pinmux_functions_ops
);
519 debugfs_create_file("pinmux-pins", S_IFREG
| S_IRUGO
,
520 devroot
, pctldev
, &pinmux_pins_ops
);
523 #endif /* CONFIG_DEBUG_FS */