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",
62 * pin_request() - request a single pin to be muxed in, typically for GPIO
63 * @pin: the pin number in the global pin space
64 * @owner: a representation of the owner of this pin; typically the device
65 * name that controls its mux function, or the requested GPIO name
66 * @gpio_range: the range matching the GPIO pin if this is a request for a
69 static int pin_request(struct pinctrl_dev
*pctldev
,
70 int pin
, const char *owner
,
71 struct pinctrl_gpio_range
*gpio_range
)
73 struct pin_desc
*desc
;
74 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
77 dev_dbg(pctldev
->dev
, "request pin %d for %s\n", pin
, owner
);
79 desc
= pin_desc_get(pctldev
, pin
);
82 "pin is not registered so it cannot be requested\n");
86 if (desc
->usecount
&& strcmp(desc
->owner
, owner
)) {
88 "pin already requested\n");
93 if (desc
->usecount
> 1)
98 /* Let each pin increase references to this module */
99 if (!try_module_get(pctldev
->owner
)) {
100 dev_err(pctldev
->dev
,
101 "could not increase module refcount for pin %d\n",
108 * If there is no kind of request function for the pin we just assume
109 * we got it by default and proceed.
111 if (gpio_range
&& ops
->gpio_request_enable
)
112 /* This requests and enables a single GPIO pin */
113 status
= ops
->gpio_request_enable(pctldev
, gpio_range
, pin
);
114 else if (ops
->request
)
115 status
= ops
->request(pctldev
, pin
);
120 dev_err(pctldev
->dev
, "->request on device %s failed for pin %d\n",
121 pctldev
->desc
->name
, pin
);
122 module_put(pctldev
->owner
);
133 dev_err(pctldev
->dev
, "pin-%d (%s) status %d\n",
140 * pin_free() - release a single muxed in pin so something else can be muxed
141 * @pctldev: pin controller device handling this pin
142 * @pin: the pin to free
143 * @gpio_range: the range matching the GPIO pin if this is a request for a
146 * This function returns a pointer to the previous owner. This is used
147 * for callers that dynamically allocate an owner name so it can be freed
148 * once the pin is free. This is done for GPIO request functions.
150 static const char *pin_free(struct pinctrl_dev
*pctldev
, int pin
,
151 struct pinctrl_gpio_range
*gpio_range
)
153 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
154 struct pin_desc
*desc
;
157 desc
= pin_desc_get(pctldev
, pin
);
159 dev_err(pctldev
->dev
,
160 "pin is not registered so it cannot be freed\n");
169 * If there is no kind of request function for the pin we just assume
170 * we got it by default and proceed.
172 if (gpio_range
&& ops
->gpio_disable_free
)
173 ops
->gpio_disable_free(pctldev
, gpio_range
, pin
);
175 ops
->free(pctldev
, pin
);
179 module_put(pctldev
->owner
);
185 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
186 * @pctldev: pin controller device affected
187 * @pin: the pin to mux in for GPIO
188 * @range: the applicable GPIO range
190 int pinmux_request_gpio(struct pinctrl_dev
*pctldev
,
191 struct pinctrl_gpio_range
*range
,
192 unsigned pin
, unsigned gpio
)
198 /* Conjure some name stating what chip and pin this is taken by */
199 snprintf(gpiostr
, 15, "%s:%d", range
->name
, gpio
);
201 owner
= kstrdup(gpiostr
, GFP_KERNEL
);
205 ret
= pin_request(pctldev
, pin
, owner
, range
);
213 * pinmux_free_gpio() - release a pin from GPIO muxing
214 * @pctldev: the pin controller device for the pin
215 * @pin: the affected currently GPIO-muxed in pin
216 * @range: applicable GPIO range
218 void pinmux_free_gpio(struct pinctrl_dev
*pctldev
, unsigned pin
,
219 struct pinctrl_gpio_range
*range
)
223 owner
= pin_free(pctldev
, pin
, range
);
228 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
229 * @pctldev: the pin controller handling this pin
230 * @range: applicable GPIO range
231 * @pin: the affected GPIO pin in this controller
232 * @input: true if we set the pin as input, false for output
234 int pinmux_gpio_direction(struct pinctrl_dev
*pctldev
,
235 struct pinctrl_gpio_range
*range
,
236 unsigned pin
, bool input
)
238 const struct pinmux_ops
*ops
;
241 ops
= pctldev
->desc
->pmxops
;
243 if (ops
->gpio_set_direction
)
244 ret
= ops
->gpio_set_direction(pctldev
, range
, pin
, input
);
251 static int pinmux_func_name_to_selector(struct pinctrl_dev
*pctldev
,
252 const char *function
)
254 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
255 unsigned selector
= 0;
257 /* See if this pctldev has this function */
258 while (ops
->list_functions(pctldev
, selector
) >= 0) {
259 const char *fname
= ops
->get_function_name(pctldev
,
262 if (!strcmp(function
, fname
))
268 pr_err("%s does not support function %s\n",
269 pinctrl_dev_get_name(pctldev
), function
);
273 int pinmux_map_to_setting(struct pinctrl_map
const *map
,
274 struct pinctrl_setting
*setting
)
276 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
277 const struct pinmux_ops
*pmxops
= pctldev
->desc
->pmxops
;
278 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
279 char const * const *groups
;
284 const unsigned *pins
;
287 setting
->func_selector
=
288 pinmux_func_name_to_selector(pctldev
, map
->function
);
289 if (setting
->func_selector
< 0)
290 return setting
->func_selector
;
292 ret
= pmxops
->get_function_groups(pctldev
, setting
->func_selector
,
293 &groups
, &num_groups
);
302 for (i
= 0; i
< num_groups
; i
++) {
303 if (!strcmp(group
, groups
[i
])) {
314 setting
->group_selector
=
315 pinctrl_get_group_selector(pctldev
, group
);
316 if (setting
->group_selector
< 0)
317 return setting
->group_selector
;
319 ret
= pctlops
->get_group_pins(pctldev
, setting
->group_selector
,
322 dev_err(pctldev
->dev
,
323 "could not get pins for device %s group selector %d\n",
324 pinctrl_dev_get_name(pctldev
), setting
->group_selector
);
328 /* Try to allocate all pins in this group, one by one */
329 for (i
= 0; i
< num_pins
; i
++) {
330 ret
= pin_request(pctldev
, pins
[i
], map
->dev_name
, NULL
);
332 dev_err(pctldev
->dev
,
333 "could not get request pin %d on device %s\n",
334 pins
[i
], pinctrl_dev_get_name(pctldev
));
335 /* On error release all taken pins */
336 i
--; /* this pin just failed */
338 pin_free(pctldev
, pins
[i
], NULL
);
346 void pinmux_free_setting(struct pinctrl_setting
const *setting
)
348 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
349 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
350 const unsigned *pins
;
355 ret
= pctlops
->get_group_pins(pctldev
, setting
->group_selector
,
358 dev_err(pctldev
->dev
,
359 "could not get pins for device %s group selector %d\n",
360 pinctrl_dev_get_name(pctldev
), setting
->group_selector
);
364 for (i
= 0; i
< num_pins
; i
++)
365 pin_free(pctldev
, pins
[i
], NULL
);
368 int pinmux_enable_setting(struct pinctrl_setting
const *setting
)
370 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
371 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
373 return ops
->enable(pctldev
, setting
->func_selector
,
374 setting
->group_selector
);
377 void pinmux_disable_setting(struct pinctrl_setting
const *setting
)
379 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
380 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
382 ops
->disable(pctldev
, setting
->func_selector
, setting
->group_selector
);
385 #ifdef CONFIG_DEBUG_FS
387 /* Called from pincontrol core */
388 static int pinmux_functions_show(struct seq_file
*s
, void *what
)
390 struct pinctrl_dev
*pctldev
= s
->private;
391 const struct pinmux_ops
*pmxops
= pctldev
->desc
->pmxops
;
392 unsigned func_selector
= 0;
394 mutex_lock(&pinctrl_mutex
);
396 while (pmxops
->list_functions(pctldev
, func_selector
) >= 0) {
397 const char *func
= pmxops
->get_function_name(pctldev
,
399 const char * const *groups
;
404 ret
= pmxops
->get_function_groups(pctldev
, func_selector
,
405 &groups
, &num_groups
);
407 seq_printf(s
, "function %s: COULD NOT GET GROUPS\n",
410 seq_printf(s
, "function: %s, groups = [ ", func
);
411 for (i
= 0; i
< num_groups
; i
++)
412 seq_printf(s
, "%s ", groups
[i
]);
418 mutex_unlock(&pinctrl_mutex
);
423 static int pinmux_pins_show(struct seq_file
*s
, void *what
)
425 struct pinctrl_dev
*pctldev
= s
->private;
428 seq_puts(s
, "Pinmux settings per pin\n");
429 seq_puts(s
, "Format: pin (name): owner\n");
431 mutex_lock(&pinctrl_mutex
);
433 /* The pin number can be retrived from the pin controller descriptor */
434 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
435 struct pin_desc
*desc
;
438 pin
= pctldev
->desc
->pins
[i
].number
;
439 desc
= pin_desc_get(pctldev
, pin
);
440 /* Skip if we cannot search the pin */
445 !strcmp(desc
->owner
, pinctrl_dev_get_name(pctldev
)))
448 seq_printf(s
, "pin %d (%s): %s%s\n", pin
,
449 desc
->name
? desc
->name
: "unnamed",
450 desc
->owner
? desc
->owner
: "UNCLAIMED",
451 is_hog
? " (HOG)" : "");
454 mutex_unlock(&pinctrl_mutex
);
459 void pinmux_dbg_show(struct seq_file
*s
, struct pinctrl_setting
const *setting
)
461 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
462 const struct pinmux_ops
*pmxops
= pctldev
->desc
->pmxops
;
463 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
465 seq_printf(s
, "controller: %s group: %s (%u) function: %s (%u)\n",
466 pinctrl_dev_get_name(pctldev
),
467 pctlops
->get_group_name(pctldev
, setting
->group_selector
),
468 setting
->group_selector
,
469 pmxops
->get_function_name(pctldev
, setting
->func_selector
),
470 setting
->func_selector
);
473 static int pinmux_functions_open(struct inode
*inode
, struct file
*file
)
475 return single_open(file
, pinmux_functions_show
, inode
->i_private
);
478 static int pinmux_pins_open(struct inode
*inode
, struct file
*file
)
480 return single_open(file
, pinmux_pins_show
, inode
->i_private
);
483 static const struct file_operations pinmux_functions_ops
= {
484 .open
= pinmux_functions_open
,
487 .release
= single_release
,
490 static const struct file_operations pinmux_pins_ops
= {
491 .open
= pinmux_pins_open
,
494 .release
= single_release
,
497 void pinmux_init_device_debugfs(struct dentry
*devroot
,
498 struct pinctrl_dev
*pctldev
)
500 debugfs_create_file("pinmux-functions", S_IFREG
| S_IRUGO
,
501 devroot
, pctldev
, &pinmux_functions_ops
);
502 debugfs_create_file("pinmux-pins", S_IFREG
| S_IRUGO
,
503 devroot
, pctldev
, &pinmux_pins_ops
);
506 #endif /* CONFIG_DEBUG_FS */