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