pinctrl: show pin name when request pins
[deliverable/linux.git] / drivers / pinctrl / pinmux.c
1 /*
2 * Core driver for the pin muxing portions of the pin control subsystem
3 *
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
7 *
8 * Author: Linus Walleij <linus.walleij@linaro.org>
9 *
10 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
11 *
12 * License terms: GNU General Public License (GPL) version 2
13 */
14 #define pr_fmt(fmt) "pinmux core: " fmt
15
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>
30 #include "core.h"
31 #include "pinmux.h"
32
33 int pinmux_check_ops(struct pinctrl_dev *pctldev)
34 {
35 const struct pinmux_ops *ops = pctldev->desc->pmxops;
36 unsigned nfuncs;
37 unsigned selector = 0;
38
39 /* Check that we implement required operations */
40 if (!ops ||
41 !ops->get_functions_count ||
42 !ops->get_function_name ||
43 !ops->get_function_groups ||
44 !ops->enable ||
45 !ops->disable)
46 return -EINVAL;
47
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,
52 selector);
53 if (!fname) {
54 dev_err(pctldev->dev, "pinmux ops has no name for function%u\n",
55 selector);
56 return -EINVAL;
57 }
58 selector++;
59 }
60
61 return 0;
62 }
63
64 int pinmux_validate_map(struct pinctrl_map const *map, int i)
65 {
66 if (!map->data.mux.function) {
67 pr_err("failed to register map %s (%d): no function given\n",
68 map->name, i);
69 return -EINVAL;
70 }
71
72 return 0;
73 }
74
75 /**
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
81 * single GPIO pin
82 */
83 static int pin_request(struct pinctrl_dev *pctldev,
84 int pin, const char *owner,
85 struct pinctrl_gpio_range *gpio_range)
86 {
87 struct pin_desc *desc;
88 const struct pinmux_ops *ops = pctldev->desc->pmxops;
89 int status = -EINVAL;
90
91 desc = pin_desc_get(pctldev, pin);
92 if (desc == NULL) {
93 dev_err(pctldev->dev,
94 "pin is not registered so it cannot be requested\n");
95 goto out;
96 }
97
98 dev_dbg(pctldev->dev, "request pin %d (%s) for %s\n",
99 pin, desc->name, owner);
100
101 if (gpio_range) {
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");
106 goto out;
107 }
108
109 desc->gpio_owner = owner;
110 } else {
111 if (desc->mux_usecount && strcmp(desc->mux_owner, owner)) {
112 dev_err(pctldev->dev,
113 "pin already requested\n");
114 goto out;
115 }
116
117 desc->mux_usecount++;
118 if (desc->mux_usecount > 1)
119 return 0;
120
121 desc->mux_owner = owner;
122 }
123
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",
128 pin);
129 status = -EINVAL;
130 goto out_free_pin;
131 }
132
133 /*
134 * If there is no kind of request function for the pin we just assume
135 * we got it by default and proceed.
136 */
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);
142 else
143 status = 0;
144
145 if (status) {
146 dev_err(pctldev->dev, "->request on device %s failed for pin %d\n",
147 pctldev->desc->name, pin);
148 module_put(pctldev->owner);
149 }
150
151 out_free_pin:
152 if (status) {
153 if (gpio_range) {
154 desc->gpio_owner = NULL;
155 } else {
156 desc->mux_usecount--;
157 if (!desc->mux_usecount)
158 desc->mux_owner = NULL;
159 }
160 }
161 out:
162 if (status)
163 dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
164 pin, owner, status);
165
166 return status;
167 }
168
169 /**
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
174 * single GPIO pin
175 *
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.
179 */
180 static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
181 struct pinctrl_gpio_range *gpio_range)
182 {
183 const struct pinmux_ops *ops = pctldev->desc->pmxops;
184 struct pin_desc *desc;
185 const char *owner;
186
187 desc = pin_desc_get(pctldev, pin);
188 if (desc == NULL) {
189 dev_err(pctldev->dev,
190 "pin is not registered so it cannot be freed\n");
191 return NULL;
192 }
193
194 if (!gpio_range) {
195 desc->mux_usecount--;
196 if (desc->mux_usecount)
197 return NULL;
198 }
199
200 /*
201 * If there is no kind of request function for the pin we just assume
202 * we got it by default and proceed.
203 */
204 if (gpio_range && ops->gpio_disable_free)
205 ops->gpio_disable_free(pctldev, gpio_range, pin);
206 else if (ops->free)
207 ops->free(pctldev, pin);
208
209 if (gpio_range) {
210 owner = desc->gpio_owner;
211 desc->gpio_owner = NULL;
212 } else {
213 owner = desc->mux_owner;
214 desc->mux_owner = NULL;
215 desc->mux_setting = NULL;
216 }
217
218 module_put(pctldev->owner);
219
220 return owner;
221 }
222
223 /**
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
228 */
229 int pinmux_request_gpio(struct pinctrl_dev *pctldev,
230 struct pinctrl_gpio_range *range,
231 unsigned pin, unsigned gpio)
232 {
233 char gpiostr[16];
234 const char *owner;
235 int ret;
236
237 /* Conjure some name stating what chip and pin this is taken by */
238 snprintf(gpiostr, 15, "%s:%d", range->name, gpio);
239
240 owner = kstrdup(gpiostr, GFP_KERNEL);
241 if (!owner)
242 return -EINVAL;
243
244 ret = pin_request(pctldev, pin, owner, range);
245 if (ret < 0)
246 kfree(owner);
247
248 return ret;
249 }
250
251 /**
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
256 */
257 void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
258 struct pinctrl_gpio_range *range)
259 {
260 const char *owner;
261
262 owner = pin_free(pctldev, pin, range);
263 kfree(owner);
264 }
265
266 /**
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
272 */
273 int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
274 struct pinctrl_gpio_range *range,
275 unsigned pin, bool input)
276 {
277 const struct pinmux_ops *ops;
278 int ret;
279
280 ops = pctldev->desc->pmxops;
281
282 if (ops->gpio_set_direction)
283 ret = ops->gpio_set_direction(pctldev, range, pin, input);
284 else
285 ret = 0;
286
287 return ret;
288 }
289
290 static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
291 const char *function)
292 {
293 const struct pinmux_ops *ops = pctldev->desc->pmxops;
294 unsigned nfuncs = ops->get_functions_count(pctldev);
295 unsigned selector = 0;
296
297 /* See if this pctldev has this function */
298 while (selector < nfuncs) {
299 const char *fname = ops->get_function_name(pctldev,
300 selector);
301
302 if (!strcmp(function, fname))
303 return selector;
304
305 selector++;
306 }
307
308 pr_err("%s does not support function %s\n",
309 pinctrl_dev_get_name(pctldev), function);
310 return -EINVAL;
311 }
312
313 int pinmux_map_to_setting(struct pinctrl_map const *map,
314 struct pinctrl_setting *setting)
315 {
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;
320 unsigned num_groups;
321 int ret;
322 const char *group;
323 int i;
324 const unsigned *pins;
325 unsigned num_pins;
326
327 if (!pmxops) {
328 dev_err(pctldev->dev, "does not support mux function\n");
329 return -EINVAL;
330 }
331
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;
336
337 ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
338 &groups, &num_groups);
339 if (ret < 0)
340 return ret;
341 if (!num_groups)
342 return -EINVAL;
343
344 if (map->data.mux.group) {
345 bool found = false;
346 group = map->data.mux.group;
347 for (i = 0; i < num_groups; i++) {
348 if (!strcmp(group, groups[i])) {
349 found = true;
350 break;
351 }
352 }
353 if (!found)
354 return -EINVAL;
355 } else {
356 group = groups[0];
357 }
358
359 setting->data.mux.group = pinctrl_get_group_selector(pctldev, group);
360 if (setting->data.mux.group < 0)
361 return setting->data.mux.group;
362
363 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group, &pins,
364 &num_pins);
365 if (ret) {
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);
369 return -ENODEV;
370 }
371
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);
375 if (ret) {
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 */
381 for (; i >= 0; i--)
382 pin_free(pctldev, pins[i], NULL);
383 return -ENODEV;
384 }
385 }
386
387 return 0;
388 }
389
390 void pinmux_free_setting(struct pinctrl_setting const *setting)
391 {
392 struct pinctrl_dev *pctldev = setting->pctldev;
393 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
394 const unsigned *pins;
395 unsigned num_pins;
396 int ret;
397 int i;
398
399 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
400 &pins, &num_pins);
401 if (ret) {
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);
405 return;
406 }
407
408 for (i = 0; i < num_pins; i++)
409 pin_free(pctldev, pins[i], NULL);
410 }
411
412 int pinmux_enable_setting(struct pinctrl_setting const *setting)
413 {
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;
417 int ret;
418 const unsigned *pins;
419 unsigned num_pins;
420 int i;
421 struct pin_desc *desc;
422
423 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
424 &pins, &num_pins);
425 if (ret) {
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);
430 num_pins = 0;
431 }
432
433 for (i = 0; i < num_pins; i++) {
434 desc = pin_desc_get(pctldev, pins[i]);
435 if (desc == NULL) {
436 dev_warn(pctldev->dev,
437 "could not get pin desc for pin %d\n",
438 pins[i]);
439 continue;
440 }
441 desc->mux_setting = &(setting->data.mux);
442 }
443
444 return ops->enable(pctldev, setting->data.mux.func,
445 setting->data.mux.group);
446 }
447
448 void pinmux_disable_setting(struct pinctrl_setting const *setting)
449 {
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;
453 int ret;
454 const unsigned *pins;
455 unsigned num_pins;
456 int i;
457 struct pin_desc *desc;
458
459 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
460 &pins, &num_pins);
461 if (ret) {
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);
466 num_pins = 0;
467 }
468
469 for (i = 0; i < num_pins; i++) {
470 desc = pin_desc_get(pctldev, pins[i]);
471 if (desc == NULL) {
472 dev_warn(pctldev->dev,
473 "could not get pin desc for pin %d\n",
474 pins[i]);
475 continue;
476 }
477 desc->mux_setting = NULL;
478 }
479
480 ops->disable(pctldev, setting->data.mux.func, setting->data.mux.group);
481 }
482
483 #ifdef CONFIG_DEBUG_FS
484
485 /* Called from pincontrol core */
486 static int pinmux_functions_show(struct seq_file *s, void *what)
487 {
488 struct pinctrl_dev *pctldev = s->private;
489 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
490 unsigned nfuncs;
491 unsigned func_selector = 0;
492
493 if (!pmxops)
494 return 0;
495
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,
500 func_selector);
501 const char * const *groups;
502 unsigned num_groups;
503 int ret;
504 int i;
505
506 ret = pmxops->get_function_groups(pctldev, func_selector,
507 &groups, &num_groups);
508 if (ret)
509 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
510 func);
511
512 seq_printf(s, "function: %s, groups = [ ", func);
513 for (i = 0; i < num_groups; i++)
514 seq_printf(s, "%s ", groups[i]);
515 seq_puts(s, "]\n");
516
517 func_selector++;
518 }
519
520 mutex_unlock(&pinctrl_mutex);
521
522 return 0;
523 }
524
525 static int pinmux_pins_show(struct seq_file *s, void *what)
526 {
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;
530 unsigned i, pin;
531
532 if (!pmxops)
533 return 0;
534
535 seq_puts(s, "Pinmux settings per pin\n");
536 seq_puts(s, "Format: pin (name): mux_owner gpio_owner hog?\n");
537
538 mutex_lock(&pinctrl_mutex);
539
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;
543 bool is_hog = false;
544
545 pin = pctldev->desc->pins[i].number;
546 desc = pin_desc_get(pctldev, pin);
547 /* Skip if we cannot search the pin */
548 if (desc == NULL)
549 continue;
550
551 if (desc->mux_owner &&
552 !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
553 is_hog = true;
554
555 seq_printf(s, "pin %d (%s): %s %s%s", pin,
556 desc->name ? desc->name : "unnamed",
557 desc->mux_owner ? desc->mux_owner
558 : "(MUX UNCLAIMED)",
559 desc->gpio_owner ? desc->gpio_owner
560 : "(GPIO UNCLAIMED)",
561 is_hog ? " (HOG)" : "");
562
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));
569 else
570 seq_printf(s, "\n");
571 }
572
573 mutex_unlock(&pinctrl_mutex);
574
575 return 0;
576 }
577
578 void pinmux_show_map(struct seq_file *s, struct pinctrl_map const *map)
579 {
580 seq_printf(s, "group %s\nfunction %s\n",
581 map->data.mux.group ? map->data.mux.group : "(default)",
582 map->data.mux.function);
583 }
584
585 void pinmux_show_setting(struct seq_file *s,
586 struct pinctrl_setting const *setting)
587 {
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;
591
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);
597 }
598
599 static int pinmux_functions_open(struct inode *inode, struct file *file)
600 {
601 return single_open(file, pinmux_functions_show, inode->i_private);
602 }
603
604 static int pinmux_pins_open(struct inode *inode, struct file *file)
605 {
606 return single_open(file, pinmux_pins_show, inode->i_private);
607 }
608
609 static const struct file_operations pinmux_functions_ops = {
610 .open = pinmux_functions_open,
611 .read = seq_read,
612 .llseek = seq_lseek,
613 .release = single_release,
614 };
615
616 static const struct file_operations pinmux_pins_ops = {
617 .open = pinmux_pins_open,
618 .read = seq_read,
619 .llseek = seq_lseek,
620 .release = single_release,
621 };
622
623 void pinmux_init_device_debugfs(struct dentry *devroot,
624 struct pinctrl_dev *pctldev)
625 {
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);
630 }
631
632 #endif /* CONFIG_DEBUG_FS */
This page took 0.058537 seconds and 5 git commands to generate.