Linux Audio

Check our new training course

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