1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Core driver for the pin muxing portions of the pin control subsystem
4 *
5 * Copyright (C) 2011-2012 ST-Ericsson SA
6 * Written on behalf of Linaro for ST-Ericsson
7 * Based on bits of regulator core, gpio core and clk core
8 *
9 * Author: Linus Walleij <linus.walleij@linaro.org>
10 *
11 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
12 */
13 #define pr_fmt(fmt) "pinmux core: " fmt
14
15 #include <linux/array_size.h>
16 #include <linux/ctype.h>
17 #include <linux/cleanup.h>
18 #include <linux/debugfs.h>
19 #include <linux/device.h>
20 #include <linux/err.h>
21 #include <linux/init.h>
22 #include <linux/list.h>
23 #include <linux/module.h>
24 #include <linux/radix-tree.h>
25 #include <linux/seq_file.h>
26 #include <linux/slab.h>
27 #include <linux/string.h>
28
29 #include <linux/pinctrl/machine.h>
30 #include <linux/pinctrl/pinctrl.h>
31 #include <linux/pinctrl/pinmux.h>
32
33 #include "core.h"
34 #include "pinmux.h"
35
pinmux_check_ops(struct pinctrl_dev * pctldev)36 int pinmux_check_ops(struct pinctrl_dev *pctldev)
37 {
38 const struct pinmux_ops *ops = pctldev->desc->pmxops;
39 unsigned int nfuncs;
40 unsigned int selector = 0;
41
42 /* Check that we implement required operations */
43 if (!ops ||
44 !ops->get_functions_count ||
45 !ops->get_function_name ||
46 !ops->get_function_groups ||
47 !ops->set_mux) {
48 dev_err(pctldev->dev, "pinmux ops lacks necessary functions\n");
49 return -EINVAL;
50 }
51 /* Check that all functions registered have names */
52 nfuncs = ops->get_functions_count(pctldev);
53 while (selector < nfuncs) {
54 const char *fname = ops->get_function_name(pctldev,
55 selector);
56 if (!fname) {
57 dev_err(pctldev->dev, "pinmux ops has no name for function%u\n",
58 selector);
59 return -EINVAL;
60 }
61 selector++;
62 }
63
64 return 0;
65 }
66
pinmux_validate_map(const struct pinctrl_map * map,int i)67 int pinmux_validate_map(const struct pinctrl_map *map, int i)
68 {
69 if (!map->data.mux.function) {
70 pr_err("failed to register map %s (%d): no function given\n",
71 map->name, i);
72 return -EINVAL;
73 }
74
75 return 0;
76 }
77
78 /**
79 * pinmux_can_be_used_for_gpio() - check if a specific pin
80 * is either muxed to a different function or used as gpio.
81 *
82 * @pctldev: the associated pin controller device
83 * @pin: the pin number in the global pin space
84 *
85 * Controllers not defined as strict will always return true,
86 * menaning that the gpio can be used.
87 */
pinmux_can_be_used_for_gpio(struct pinctrl_dev * pctldev,unsigned int pin)88 bool pinmux_can_be_used_for_gpio(struct pinctrl_dev *pctldev, unsigned int pin)
89 {
90 struct pin_desc *desc = pin_desc_get(pctldev, pin);
91 const struct pinmux_ops *ops = pctldev->desc->pmxops;
92 const struct pinctrl_setting_mux *mux_setting;
93 bool func_is_gpio = false;
94
95 /* Can't inspect pin, assume it can be used */
96 if (!desc || !ops)
97 return true;
98
99 mux_setting = desc->mux_setting;
100
101 guard(mutex)(&desc->mux_lock);
102 if (mux_setting && ops->function_is_gpio)
103 func_is_gpio = ops->function_is_gpio(pctldev, mux_setting->func);
104
105 if (ops->strict && desc->mux_usecount && !func_is_gpio)
106 return false;
107
108 return !(ops->strict && !!desc->gpio_owner);
109 }
110
111 /**
112 * pin_request() - request a single pin to be muxed in, typically for GPIO
113 * @pctldev: the associated pin controller device
114 * @pin: the pin number in the global pin space
115 * @owner: a representation of the owner of this pin; typically the device
116 * name that controls its mux function, or the requested GPIO name
117 * @gpio_range: the range matching the GPIO pin if this is a request for a
118 * single GPIO pin
119 */
pin_request(struct pinctrl_dev * pctldev,int pin,const char * owner,struct pinctrl_gpio_range * gpio_range)120 static int pin_request(struct pinctrl_dev *pctldev,
121 int pin, const char *owner,
122 struct pinctrl_gpio_range *gpio_range)
123 {
124 struct pin_desc *desc;
125 const struct pinmux_ops *ops = pctldev->desc->pmxops;
126 const struct pinctrl_setting_mux *mux_setting;
127 int status = -EINVAL;
128 bool gpio_ok = false;
129
130 desc = pin_desc_get(pctldev, pin);
131 if (desc == NULL) {
132 dev_err(pctldev->dev,
133 "pin %d is not registered so it cannot be requested\n",
134 pin);
135 goto out;
136 }
137
138 mux_setting = desc->mux_setting;
139
140 dev_dbg(pctldev->dev, "request pin %d (%s) for %s\n",
141 pin, desc->name, owner);
142
143 scoped_guard(mutex, &desc->mux_lock) {
144 if (mux_setting) {
145 if (ops->function_is_gpio)
146 gpio_ok = ops->function_is_gpio(pctldev,
147 mux_setting->func);
148 } else {
149 gpio_ok = true;
150 }
151
152 if ((!gpio_range || ops->strict) && !gpio_ok &&
153 desc->mux_usecount && strcmp(desc->mux_owner, owner)) {
154 dev_err(pctldev->dev,
155 "pin %s already requested by %s; cannot claim for %s\n",
156 desc->name, desc->mux_owner, owner);
157 goto out;
158 }
159
160 if ((gpio_range || ops->strict) && !gpio_ok && desc->gpio_owner) {
161 dev_err(pctldev->dev,
162 "pin %s already requested by %s; cannot claim for %s\n",
163 desc->name, desc->gpio_owner, owner);
164 goto out;
165 }
166
167 if (gpio_range) {
168 desc->gpio_owner = owner;
169 } else {
170 desc->mux_usecount++;
171 if (desc->mux_usecount > 1)
172 return 0;
173
174 desc->mux_owner = owner;
175 }
176 }
177
178 /* Let each pin increase references to this module */
179 if (!try_module_get(pctldev->owner)) {
180 dev_err(pctldev->dev,
181 "could not increase module refcount for pin %d\n",
182 pin);
183 status = -EINVAL;
184 goto out_free_pin;
185 }
186
187 /*
188 * If there is no kind of request function for the pin we just assume
189 * we got it by default and proceed.
190 */
191 if (gpio_range && ops->gpio_request_enable)
192 /* This requests and enables a single GPIO pin */
193 status = ops->gpio_request_enable(pctldev, gpio_range, pin);
194 else if (ops->request)
195 status = ops->request(pctldev, pin);
196 else
197 status = 0;
198
199 if (status)
200 module_put(pctldev->owner);
201
202 out_free_pin:
203 if (status) {
204 scoped_guard(mutex, &desc->mux_lock) {
205 if (gpio_range) {
206 desc->gpio_owner = NULL;
207 } else {
208 desc->mux_usecount--;
209 if (!desc->mux_usecount)
210 desc->mux_owner = NULL;
211 }
212 }
213 }
214 out:
215 if (status)
216 dev_err_probe(pctldev->dev, status, "pin-%d (%s)\n",
217 pin, owner);
218
219 return status;
220 }
221
222 /**
223 * pin_free() - release a single muxed in pin so something else can be muxed
224 * @pctldev: pin controller device handling this pin
225 * @pin: the pin to free
226 * @gpio_range: the range matching the GPIO pin if this is a request for a
227 * single GPIO pin
228 *
229 * This function returns a pointer to the previous owner. This is used
230 * for callers that dynamically allocate an owner name so it can be freed
231 * once the pin is free. This is done for GPIO request functions.
232 */
pin_free(struct pinctrl_dev * pctldev,int pin,struct pinctrl_gpio_range * gpio_range)233 static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
234 struct pinctrl_gpio_range *gpio_range)
235 {
236 const struct pinmux_ops *ops = pctldev->desc->pmxops;
237 struct pin_desc *desc;
238 const char *owner;
239
240 desc = pin_desc_get(pctldev, pin);
241 if (desc == NULL) {
242 dev_err(pctldev->dev,
243 "pin is not registered so it cannot be freed\n");
244 return NULL;
245 }
246
247 scoped_guard(mutex, &desc->mux_lock) {
248 if (!gpio_range) {
249 /*
250 * A pin should not be freed more times than allocated.
251 */
252 if (WARN_ON(!desc->mux_usecount))
253 return NULL;
254 desc->mux_usecount--;
255 if (desc->mux_usecount)
256 return NULL;
257 }
258
259 if (gpio_range) {
260 owner = desc->gpio_owner;
261 desc->gpio_owner = NULL;
262 } else {
263 owner = desc->mux_owner;
264 desc->mux_owner = NULL;
265 desc->mux_setting = NULL;
266 }
267 }
268
269 /*
270 * If there is no kind of request function for the pin we just assume
271 * we got it by default and proceed.
272 */
273 if (gpio_range && ops->gpio_disable_free)
274 ops->gpio_disable_free(pctldev, gpio_range, pin);
275 else if (ops->free)
276 ops->free(pctldev, pin);
277
278 module_put(pctldev->owner);
279
280 return owner;
281 }
282
283 /**
284 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
285 * @pctldev: pin controller device affected
286 * @pin: the pin to mux in for GPIO
287 * @range: the applicable GPIO range
288 * @gpio: number of requested GPIO
289 */
pinmux_request_gpio(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned int pin,unsigned int gpio)290 int pinmux_request_gpio(struct pinctrl_dev *pctldev,
291 struct pinctrl_gpio_range *range,
292 unsigned int pin, unsigned int gpio)
293 {
294 const char *owner;
295 int ret;
296
297 /* Conjure some name stating what chip and pin this is taken by */
298 owner = kasprintf(GFP_KERNEL, "%s:%d", range->name, gpio);
299 if (!owner)
300 return -ENOMEM;
301
302 ret = pin_request(pctldev, pin, owner, range);
303 if (ret < 0)
304 kfree(owner);
305
306 return ret;
307 }
308
309 /**
310 * pinmux_free_gpio() - release a pin from GPIO muxing
311 * @pctldev: the pin controller device for the pin
312 * @pin: the affected currently GPIO-muxed in pin
313 * @range: applicable GPIO range
314 */
pinmux_free_gpio(struct pinctrl_dev * pctldev,unsigned int pin,struct pinctrl_gpio_range * range)315 void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned int pin,
316 struct pinctrl_gpio_range *range)
317 {
318 const char *owner;
319
320 owner = pin_free(pctldev, pin, range);
321 kfree(owner);
322 }
323
324 /**
325 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
326 * @pctldev: the pin controller handling this pin
327 * @range: applicable GPIO range
328 * @pin: the affected GPIO pin in this controller
329 * @input: true if we set the pin as input, false for output
330 */
pinmux_gpio_direction(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned int pin,bool input)331 int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
332 struct pinctrl_gpio_range *range,
333 unsigned int pin, bool input)
334 {
335 const struct pinmux_ops *ops;
336 int ret;
337
338 ops = pctldev->desc->pmxops;
339
340 if (ops->gpio_set_direction)
341 ret = ops->gpio_set_direction(pctldev, range, pin, input);
342 else
343 ret = 0;
344
345 return ret;
346 }
347
pinmux_func_name_to_selector(struct pinctrl_dev * pctldev,const char * function)348 static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
349 const char *function)
350 {
351 const struct pinmux_ops *ops = pctldev->desc->pmxops;
352 unsigned int nfuncs = ops->get_functions_count(pctldev);
353 unsigned int selector = 0;
354
355 /* See if this pctldev has this function */
356 while (selector < nfuncs) {
357 const char *fname = ops->get_function_name(pctldev, selector);
358
359 if (fname && !strcmp(function, fname))
360 return selector;
361
362 selector++;
363 }
364
365 return -EINVAL;
366 }
367
pinmux_map_to_setting(const struct pinctrl_map * map,struct pinctrl_setting * setting)368 int pinmux_map_to_setting(const struct pinctrl_map *map,
369 struct pinctrl_setting *setting)
370 {
371 struct pinctrl_dev *pctldev = setting->pctldev;
372 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
373 char const * const *groups;
374 unsigned int num_groups;
375 int ret;
376 const char *group;
377
378 if (!pmxops) {
379 dev_err(pctldev->dev, "does not support mux function\n");
380 return -EINVAL;
381 }
382
383 ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function);
384 if (ret < 0) {
385 dev_err(pctldev->dev, "invalid function %s in map table\n",
386 map->data.mux.function);
387 return ret;
388 }
389 setting->data.mux.func = ret;
390
391 ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
392 &groups, &num_groups);
393 if (ret < 0) {
394 dev_err(pctldev->dev, "can't query groups for function %s\n",
395 map->data.mux.function);
396 return ret;
397 }
398 if (!num_groups) {
399 dev_err(pctldev->dev,
400 "function %s can't be selected on any group\n",
401 map->data.mux.function);
402 return -EINVAL;
403 }
404 if (map->data.mux.group) {
405 group = map->data.mux.group;
406 ret = match_string(groups, num_groups, group);
407 if (ret < 0) {
408 dev_err(pctldev->dev,
409 "invalid group \"%s\" for function \"%s\"\n",
410 group, map->data.mux.function);
411 return ret;
412 }
413 } else {
414 group = groups[0];
415 }
416
417 ret = pinctrl_get_group_selector(pctldev, group);
418 if (ret < 0) {
419 dev_err(pctldev->dev, "invalid group %s in map table\n",
420 map->data.mux.group);
421 return ret;
422 }
423 setting->data.mux.group = ret;
424
425 return 0;
426 }
427
pinmux_free_setting(const struct pinctrl_setting * setting)428 void pinmux_free_setting(const struct pinctrl_setting *setting)
429 {
430 /* This function is currently unused */
431 }
432
pinmux_enable_setting(const struct pinctrl_setting * setting)433 int pinmux_enable_setting(const struct pinctrl_setting *setting)
434 {
435 struct pinctrl_dev *pctldev = setting->pctldev;
436 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
437 const struct pinmux_ops *ops = pctldev->desc->pmxops;
438 int ret = 0;
439 const unsigned int *pins = NULL;
440 unsigned int num_pins = 0;
441 int i;
442 struct pin_desc *desc;
443
444 if (pctlops->get_group_pins)
445 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
446 &pins, &num_pins);
447
448 if (ret) {
449 const char *gname;
450
451 /* errors only affect debug data, so just warn */
452 gname = pctlops->get_group_name(pctldev,
453 setting->data.mux.group);
454 dev_warn(pctldev->dev,
455 "could not get pins for group %s\n",
456 gname);
457 num_pins = 0;
458 }
459
460 /* Try to allocate all pins in this group, one by one */
461 for (i = 0; i < num_pins; i++) {
462 ret = pin_request(pctldev, pins[i], setting->dev_name, NULL);
463 if (ret) {
464 const char *gname;
465 const char *pname;
466
467 desc = pin_desc_get(pctldev, pins[i]);
468 pname = desc ? desc->name : "non-existing";
469 gname = pctlops->get_group_name(pctldev,
470 setting->data.mux.group);
471 dev_err_probe(pctldev->dev, ret,
472 "could not request pin %d (%s) from group %s on device %s\n",
473 pins[i], pname, gname,
474 pinctrl_dev_get_name(pctldev));
475 goto err_pin_request;
476 }
477 }
478
479 /* Now that we have acquired the pins, encode the mux setting */
480 for (i = 0; i < num_pins; i++) {
481 desc = pin_desc_get(pctldev, pins[i]);
482 if (desc == NULL) {
483 dev_warn(pctldev->dev,
484 "could not get pin desc for pin %d\n",
485 pins[i]);
486 continue;
487 }
488 scoped_guard(mutex, &desc->mux_lock)
489 desc->mux_setting = &(setting->data.mux);
490 }
491
492 ret = ops->set_mux(pctldev, setting->data.mux.func,
493 setting->data.mux.group);
494
495 if (ret)
496 goto err_set_mux;
497
498 return 0;
499
500 err_set_mux:
501 for (i = 0; i < num_pins; i++) {
502 desc = pin_desc_get(pctldev, pins[i]);
503 if (desc) {
504 scoped_guard(mutex, &desc->mux_lock)
505 desc->mux_setting = NULL;
506 }
507 }
508 err_pin_request:
509 /* On error release all taken pins */
510 while (--i >= 0)
511 pin_free(pctldev, pins[i], NULL);
512
513 return ret;
514 }
515
pinmux_disable_setting(const struct pinctrl_setting * setting)516 void pinmux_disable_setting(const struct pinctrl_setting *setting)
517 {
518 struct pinctrl_dev *pctldev = setting->pctldev;
519 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
520 int ret = 0;
521 const unsigned int *pins = NULL;
522 unsigned int num_pins = 0;
523 int i;
524 struct pin_desc *desc;
525 bool is_equal;
526
527 if (pctlops->get_group_pins)
528 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
529 &pins, &num_pins);
530 if (ret) {
531 const char *gname;
532
533 /* errors only affect debug data, so just warn */
534 gname = pctlops->get_group_name(pctldev,
535 setting->data.mux.group);
536 dev_warn(pctldev->dev,
537 "could not get pins for group %s\n",
538 gname);
539 num_pins = 0;
540 }
541
542 /* Flag the descs that no setting is active */
543 for (i = 0; i < num_pins; i++) {
544 desc = pin_desc_get(pctldev, pins[i]);
545 if (desc == NULL) {
546 dev_warn(pctldev->dev,
547 "could not get pin desc for pin %d\n",
548 pins[i]);
549 continue;
550 }
551 scoped_guard(mutex, &desc->mux_lock)
552 is_equal = (desc->mux_setting == &(setting->data.mux));
553
554 if (is_equal) {
555 pin_free(pctldev, pins[i], NULL);
556 } else {
557 const char *gname;
558
559 gname = pctlops->get_group_name(pctldev,
560 setting->data.mux.group);
561 dev_warn(pctldev->dev,
562 "not freeing pin %d (%s) as part of deactivating group %s - it is already used for some other setting",
563 pins[i], desc->name, gname);
564 }
565 }
566 }
567
568 #ifdef CONFIG_DEBUG_FS
569
570 /* Called from pincontrol core */
pinmux_functions_show(struct seq_file * s,void * what)571 static int pinmux_functions_show(struct seq_file *s, void *what)
572 {
573 struct pinctrl_dev *pctldev = s->private;
574 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
575 unsigned int nfuncs;
576 unsigned int func_selector = 0;
577
578 if (!pmxops)
579 return 0;
580
581 mutex_lock(&pctldev->mutex);
582 nfuncs = pmxops->get_functions_count(pctldev);
583 while (func_selector < nfuncs) {
584 const char *func = pmxops->get_function_name(pctldev,
585 func_selector);
586 const char * const *groups;
587 unsigned int num_groups;
588 int ret;
589 int i;
590
591 ret = pmxops->get_function_groups(pctldev, func_selector,
592 &groups, &num_groups);
593 if (ret) {
594 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
595 func);
596 func_selector++;
597 continue;
598 }
599
600 seq_printf(s, "function %d: %s, groups = [ ", func_selector, func);
601 for (i = 0; i < num_groups; i++)
602 seq_printf(s, "%s ", groups[i]);
603 seq_puts(s, "]\n");
604
605 func_selector++;
606 }
607
608 mutex_unlock(&pctldev->mutex);
609
610 return 0;
611 }
612 DEFINE_SHOW_ATTRIBUTE(pinmux_functions);
613
pinmux_pins_show(struct seq_file * s,void * what)614 static int pinmux_pins_show(struct seq_file *s, void *what)
615 {
616 struct pinctrl_dev *pctldev = s->private;
617 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
618 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
619 unsigned int i, pin;
620
621 if (!pmxops)
622 return 0;
623
624 seq_puts(s, "Pinmux settings per pin\n");
625 if (pmxops->strict)
626 seq_puts(s,
627 "Format: pin (name): mux_owner|gpio_owner (strict) hog?\n");
628 else
629 seq_puts(s,
630 "Format: pin (name): mux_owner gpio_owner hog?\n");
631
632 mutex_lock(&pctldev->mutex);
633
634 /* The pin number can be retrived from the pin controller descriptor */
635 for (i = 0; i < pctldev->desc->npins; i++) {
636 struct pin_desc *desc;
637 bool is_hog = false;
638
639 pin = pctldev->desc->pins[i].number;
640 desc = pin_desc_get(pctldev, pin);
641 /* Skip if we cannot search the pin */
642 if (desc == NULL)
643 continue;
644
645 scoped_guard(mutex, &desc->mux_lock) {
646 if (desc->mux_owner &&
647 !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
648 is_hog = true;
649
650 if (pmxops->strict) {
651 if (desc->mux_owner)
652 seq_printf(s, "pin %d (%s): device %s%s",
653 pin, desc->name, desc->mux_owner,
654 is_hog ? " (HOG)" : "");
655 else if (desc->gpio_owner)
656 seq_printf(s, "pin %d (%s): GPIO %s",
657 pin, desc->name, desc->gpio_owner);
658 else
659 seq_printf(s, "pin %d (%s): UNCLAIMED",
660 pin, desc->name);
661 } else {
662 /* For non-strict controllers */
663 seq_printf(s, "pin %d (%s): %s %s%s", pin, desc->name,
664 desc->mux_owner ? desc->mux_owner
665 : "(MUX UNCLAIMED)",
666 desc->gpio_owner ? desc->gpio_owner
667 : "(GPIO UNCLAIMED)",
668 is_hog ? " (HOG)" : "");
669 }
670
671 /* If mux: print function+group claiming the pin */
672 if (desc->mux_setting)
673 seq_printf(s, " function %s group %s\n",
674 pmxops->get_function_name(pctldev,
675 desc->mux_setting->func),
676 pctlops->get_group_name(pctldev,
677 desc->mux_setting->group));
678 else
679 seq_putc(s, '\n');
680 }
681 }
682
683 mutex_unlock(&pctldev->mutex);
684
685 return 0;
686 }
687 DEFINE_SHOW_ATTRIBUTE(pinmux_pins);
688
pinmux_show_map(struct seq_file * s,const struct pinctrl_map * map)689 void pinmux_show_map(struct seq_file *s, const struct pinctrl_map *map)
690 {
691 seq_printf(s, "group %s\nfunction %s\n",
692 map->data.mux.group ? map->data.mux.group : "(default)",
693 map->data.mux.function);
694 }
695
pinmux_show_setting(struct seq_file * s,const struct pinctrl_setting * setting)696 void pinmux_show_setting(struct seq_file *s,
697 const struct pinctrl_setting *setting)
698 {
699 struct pinctrl_dev *pctldev = setting->pctldev;
700 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
701 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
702
703 seq_printf(s, "group: %s (%u) function: %s (%u)\n",
704 pctlops->get_group_name(pctldev, setting->data.mux.group),
705 setting->data.mux.group,
706 pmxops->get_function_name(pctldev, setting->data.mux.func),
707 setting->data.mux.func);
708 }
709
pinmux_select_show(struct seq_file * s,void * unused)710 static int pinmux_select_show(struct seq_file *s, void *unused)
711 {
712 return -EPERM;
713 }
714
pinmux_select_write(struct file * file,const char __user * user_buf,size_t len,loff_t * ppos)715 static ssize_t pinmux_select_write(struct file *file, const char __user *user_buf,
716 size_t len, loff_t *ppos)
717 {
718 struct seq_file *sfile = file->private_data;
719 struct pinctrl_dev *pctldev = sfile->private;
720 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
721 const char *const *groups;
722 char *buf, *gname, *fname;
723 unsigned int num_groups;
724 int fsel, gsel, ret;
725
726 buf = memdup_user_nul(user_buf, len);
727 if (IS_ERR(buf))
728 return PTR_ERR(buf);
729
730 /* remove leading and trailing spaces of input buffer */
731 gname = strstrip(buf);
732 if (*gname == '\0') {
733 ret = -EINVAL;
734 goto exit_free_buf;
735 }
736
737 /* find a separator which is a spacelike character */
738 for (fname = gname; !isspace(*fname); fname++) {
739 if (*fname == '\0') {
740 ret = -EINVAL;
741 goto exit_free_buf;
742 }
743 }
744 *fname = '\0';
745
746 /* drop extra spaces between function and group names */
747 fname = skip_spaces(fname + 1);
748 if (*fname == '\0') {
749 ret = -EINVAL;
750 goto exit_free_buf;
751 }
752
753 ret = pinmux_func_name_to_selector(pctldev, fname);
754 if (ret < 0) {
755 dev_err(pctldev->dev, "invalid function %s in map table\n", fname);
756 goto exit_free_buf;
757 }
758 fsel = ret;
759
760 ret = pmxops->get_function_groups(pctldev, fsel, &groups, &num_groups);
761 if (ret) {
762 dev_err(pctldev->dev, "no groups for function %d (%s)", fsel, fname);
763 goto exit_free_buf;
764 }
765
766 ret = match_string(groups, num_groups, gname);
767 if (ret < 0) {
768 dev_err(pctldev->dev, "invalid group %s", gname);
769 goto exit_free_buf;
770 }
771
772 ret = pinctrl_get_group_selector(pctldev, gname);
773 if (ret < 0)
774 goto exit_free_buf;
775 gsel = ret;
776
777 ret = pmxops->set_mux(pctldev, fsel, gsel);
778 if (ret) {
779 dev_err(pctldev->dev, "set_mux() failed: %d", ret);
780 goto exit_free_buf;
781 }
782 ret = len;
783
784 exit_free_buf:
785 kfree(buf);
786
787 return ret;
788 }
789 DEFINE_SHOW_STORE_ATTRIBUTE(pinmux_select);
790
pinmux_init_device_debugfs(struct dentry * devroot,struct pinctrl_dev * pctldev)791 void pinmux_init_device_debugfs(struct dentry *devroot,
792 struct pinctrl_dev *pctldev)
793 {
794 debugfs_create_file("pinmux-functions", 0444,
795 devroot, pctldev, &pinmux_functions_fops);
796 debugfs_create_file("pinmux-pins", 0444,
797 devroot, pctldev, &pinmux_pins_fops);
798 debugfs_create_file("pinmux-select", 0200,
799 devroot, pctldev, &pinmux_select_fops);
800 }
801
802 #endif /* CONFIG_DEBUG_FS */
803
804 #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
805
806 /**
807 * pinmux_generic_get_function_count() - returns number of functions
808 * @pctldev: pin controller device
809 */
pinmux_generic_get_function_count(struct pinctrl_dev * pctldev)810 int pinmux_generic_get_function_count(struct pinctrl_dev *pctldev)
811 {
812 return pctldev->num_functions;
813 }
814 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_count);
815
816 /**
817 * pinmux_generic_get_function_name() - returns the function name
818 * @pctldev: pin controller device
819 * @selector: function number
820 */
821 const char *
pinmux_generic_get_function_name(struct pinctrl_dev * pctldev,unsigned int selector)822 pinmux_generic_get_function_name(struct pinctrl_dev *pctldev,
823 unsigned int selector)
824 {
825 struct function_desc *function;
826
827 function = radix_tree_lookup(&pctldev->pin_function_tree,
828 selector);
829 if (!function)
830 return NULL;
831
832 return function->func->name;
833 }
834 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_name);
835
836 /**
837 * pinmux_generic_get_function_groups() - gets the function groups
838 * @pctldev: pin controller device
839 * @selector: function number
840 * @groups: array of pin groups
841 * @ngroups: number of pin groups
842 */
pinmux_generic_get_function_groups(struct pinctrl_dev * pctldev,unsigned int selector,const char * const ** groups,unsigned int * const ngroups)843 int pinmux_generic_get_function_groups(struct pinctrl_dev *pctldev,
844 unsigned int selector,
845 const char * const **groups,
846 unsigned int * const ngroups)
847 {
848 struct function_desc *function;
849
850 function = radix_tree_lookup(&pctldev->pin_function_tree,
851 selector);
852 if (!function) {
853 dev_err(pctldev->dev, "%s could not find function%i\n",
854 __func__, selector);
855 return -EINVAL;
856 }
857 *groups = function->func->groups;
858 *ngroups = function->func->ngroups;
859
860 return 0;
861 }
862 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_groups);
863
864 /**
865 * pinmux_generic_get_function() - returns a function based on the number
866 * @pctldev: pin controller device
867 * @selector: function number
868 */
869 const struct function_desc *
pinmux_generic_get_function(struct pinctrl_dev * pctldev,unsigned int selector)870 pinmux_generic_get_function(struct pinctrl_dev *pctldev, unsigned int selector)
871 {
872 struct function_desc *function;
873
874 function = radix_tree_lookup(&pctldev->pin_function_tree,
875 selector);
876 if (!function)
877 return NULL;
878
879 return function;
880 }
881 EXPORT_SYMBOL_GPL(pinmux_generic_get_function);
882
883 /**
884 * pinmux_generic_function_is_gpio() - returns true if given function is a GPIO
885 * @pctldev: pin controller device
886 * @selector: function number
887 *
888 * Returns:
889 * True if given function is a GPIO, false otherwise.
890 */
pinmux_generic_function_is_gpio(struct pinctrl_dev * pctldev,unsigned int selector)891 bool pinmux_generic_function_is_gpio(struct pinctrl_dev *pctldev,
892 unsigned int selector)
893 {
894 struct function_desc *function;
895
896 function = radix_tree_lookup(&pctldev->pin_function_tree, selector);
897 if (!function)
898 return false;
899
900 return function->func->flags & PINFUNCTION_FLAG_GPIO;
901 }
902 EXPORT_SYMBOL_GPL(pinmux_generic_function_is_gpio);
903
904 /**
905 * pinmux_generic_add_function() - adds a function group
906 * @pctldev: pin controller device
907 * @name: name of the function
908 * @groups: array of pin groups
909 * @ngroups: number of pin groups
910 * @data: pin controller driver specific data
911 */
pinmux_generic_add_function(struct pinctrl_dev * pctldev,const char * name,const char * const * groups,const unsigned int ngroups,void * data)912 int pinmux_generic_add_function(struct pinctrl_dev *pctldev,
913 const char *name,
914 const char * const *groups,
915 const unsigned int ngroups,
916 void *data)
917 {
918 struct pinfunction func = PINCTRL_PINFUNCTION(name, groups, ngroups);
919
920 return pinmux_generic_add_pinfunction(pctldev, &func, data);
921 }
922 EXPORT_SYMBOL_GPL(pinmux_generic_add_function);
923
924 /**
925 * pinmux_generic_add_pinfunction() - adds a function group
926 * @pctldev: pin controller device
927 * @func: pinfunction structure describing the function group
928 * @data: pin controller driver specific data
929 */
pinmux_generic_add_pinfunction(struct pinctrl_dev * pctldev,const struct pinfunction * func,void * data)930 int pinmux_generic_add_pinfunction(struct pinctrl_dev *pctldev,
931 const struct pinfunction *func, void *data)
932 {
933 struct function_desc *function;
934 int selector, error;
935
936 selector = pinmux_func_name_to_selector(pctldev, func->name);
937 if (selector >= 0)
938 return selector;
939
940 selector = pctldev->num_functions;
941
942 function = devm_kzalloc(pctldev->dev, sizeof(*function), GFP_KERNEL);
943 if (!function)
944 return -ENOMEM;
945
946 /*
947 * FIXME: It's generally a bad idea to use devres in subsystem core
948 * code - managed interfaces are aimed at drivers - but pinctrl already
949 * uses it all over the place so it's a larger piece of technical debt
950 * to fix.
951 */
952 function->func = devm_kmemdup_const(pctldev->dev, func,
953 sizeof(*func), GFP_KERNEL);
954 if (!function->func)
955 return -ENOMEM;
956
957 function->data = data;
958
959 error = radix_tree_insert(&pctldev->pin_function_tree, selector, function);
960 if (error)
961 return error;
962
963 pctldev->num_functions++;
964
965 return selector;
966 }
967 EXPORT_SYMBOL_GPL(pinmux_generic_add_pinfunction);
968
969 /**
970 * pinmux_generic_remove_function() - removes a numbered function
971 * @pctldev: pin controller device
972 * @selector: function number
973 *
974 * Note that the caller must take care of locking.
975 */
pinmux_generic_remove_function(struct pinctrl_dev * pctldev,unsigned int selector)976 int pinmux_generic_remove_function(struct pinctrl_dev *pctldev,
977 unsigned int selector)
978 {
979 struct function_desc *function;
980
981 function = radix_tree_lookup(&pctldev->pin_function_tree,
982 selector);
983 if (!function)
984 return -ENOENT;
985
986 radix_tree_delete(&pctldev->pin_function_tree, selector);
987 devm_kfree(pctldev->dev, function);
988
989 pctldev->num_functions--;
990
991 return 0;
992 }
993 EXPORT_SYMBOL_GPL(pinmux_generic_remove_function);
994
995 /**
996 * pinmux_generic_free_functions() - removes all functions
997 * @pctldev: pin controller device
998 *
999 * Note that the caller must take care of locking. The pinctrl
1000 * functions are allocated with devm_kzalloc() so no need to free
1001 * them here.
1002 */
pinmux_generic_free_functions(struct pinctrl_dev * pctldev)1003 void pinmux_generic_free_functions(struct pinctrl_dev *pctldev)
1004 {
1005 struct radix_tree_iter iter;
1006 void __rcu **slot;
1007
1008 radix_tree_for_each_slot(slot, &pctldev->pin_function_tree, &iter, 0)
1009 radix_tree_delete(&pctldev->pin_function_tree, iter.index);
1010
1011 pctldev->num_functions = 0;
1012 }
1013
1014 #endif /* CONFIG_GENERIC_PINMUX_FUNCTIONS */
1015