brintos

brintos / linux-shallow public Read only

0
0
Text · 23.3 KiB · 02033ea Raw
935 lines · c
1// SPDX-License-Identifier: GPL-2.0-only2/*3 * Core driver for the pin muxing portions of the pin control subsystem4 *5 * Copyright (C) 2011-2012 ST-Ericsson SA6 * Written on behalf of Linaro for ST-Ericsson7 * Based on bits of regulator core, gpio core and clk core8 *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: " fmt14 15#include <linux/array_size.h>16#include <linux/ctype.h>17#include <linux/debugfs.h>18#include <linux/device.h>19#include <linux/err.h>20#include <linux/init.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 int nfuncs;39	unsigned int 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 pin79 *	is either muxed to a different function or used as gpio.80 *81 * @pctldev: the associated pin controller device82 * @pin: the pin number in the global pin space83 *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 int 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 GPIO104 * @pctldev: the associated pin controller device105 * @pin: the pin number in the global pin space106 * @owner: a representation of the owner of this pin; typically the device107 *	name that controls its mux function, or the requested GPIO name108 * @gpio_range: the range matching the GPIO pin if this is a request for a109 *	single GPIO pin110 */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 assume166	 * 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	else174		status = 0;175 176	if (status)177		module_put(pctldev->owner);178 179out_free_pin:180	if (status) {181		if (gpio_range) {182			desc->gpio_owner = NULL;183		} else {184			desc->mux_usecount--;185			if (!desc->mux_usecount)186				desc->mux_owner = NULL;187		}188	}189out:190	if (status)191		dev_err_probe(pctldev->dev, status, "pin-%d (%s)\n",192			      pin, owner);193 194	return status;195}196 197/**198 * pin_free() - release a single muxed in pin so something else can be muxed199 * @pctldev: pin controller device handling this pin200 * @pin: the pin to free201 * @gpio_range: the range matching the GPIO pin if this is a request for a202 *	single GPIO pin203 *204 * This function returns a pointer to the previous owner. This is used205 * for callers that dynamically allocate an owner name so it can be freed206 * once the pin is free. This is done for GPIO request functions.207 */208static const char *pin_free(struct pinctrl_dev *pctldev, int pin,209			    struct pinctrl_gpio_range *gpio_range)210{211	const struct pinmux_ops *ops = pctldev->desc->pmxops;212	struct pin_desc *desc;213	const char *owner;214 215	desc = pin_desc_get(pctldev, pin);216	if (desc == NULL) {217		dev_err(pctldev->dev,218			"pin is not registered so it cannot be freed\n");219		return NULL;220	}221 222	if (!gpio_range) {223		/*224		 * A pin should not be freed more times than allocated.225		 */226		if (WARN_ON(!desc->mux_usecount))227			return NULL;228		desc->mux_usecount--;229		if (desc->mux_usecount)230			return NULL;231	}232 233	/*234	 * If there is no kind of request function for the pin we just assume235	 * we got it by default and proceed.236	 */237	if (gpio_range && ops->gpio_disable_free)238		ops->gpio_disable_free(pctldev, gpio_range, pin);239	else if (ops->free)240		ops->free(pctldev, pin);241 242	if (gpio_range) {243		owner = desc->gpio_owner;244		desc->gpio_owner = NULL;245	} else {246		owner = desc->mux_owner;247		desc->mux_owner = NULL;248		desc->mux_setting = NULL;249	}250 251	module_put(pctldev->owner);252 253	return owner;254}255 256/**257 * pinmux_request_gpio() - request pinmuxing for a GPIO pin258 * @pctldev: pin controller device affected259 * @pin: the pin to mux in for GPIO260 * @range: the applicable GPIO range261 * @gpio: number of requested GPIO262 */263int pinmux_request_gpio(struct pinctrl_dev *pctldev,264			struct pinctrl_gpio_range *range,265			unsigned int pin, unsigned int gpio)266{267	const char *owner;268	int ret;269 270	/* Conjure some name stating what chip and pin this is taken by */271	owner = kasprintf(GFP_KERNEL, "%s:%d", range->name, gpio);272	if (!owner)273		return -ENOMEM;274 275	ret = pin_request(pctldev, pin, owner, range);276	if (ret < 0)277		kfree(owner);278 279	return ret;280}281 282/**283 * pinmux_free_gpio() - release a pin from GPIO muxing284 * @pctldev: the pin controller device for the pin285 * @pin: the affected currently GPIO-muxed in pin286 * @range: applicable GPIO range287 */288void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned int pin,289		      struct pinctrl_gpio_range *range)290{291	const char *owner;292 293	owner = pin_free(pctldev, pin, range);294	kfree(owner);295}296 297/**298 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin299 * @pctldev: the pin controller handling this pin300 * @range: applicable GPIO range301 * @pin: the affected GPIO pin in this controller302 * @input: true if we set the pin as input, false for output303 */304int pinmux_gpio_direction(struct pinctrl_dev *pctldev,305			  struct pinctrl_gpio_range *range,306			  unsigned int pin, bool input)307{308	const struct pinmux_ops *ops;309	int ret;310 311	ops = pctldev->desc->pmxops;312 313	if (ops->gpio_set_direction)314		ret = ops->gpio_set_direction(pctldev, range, pin, input);315	else316		ret = 0;317 318	return ret;319}320 321static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,322					const char *function)323{324	const struct pinmux_ops *ops = pctldev->desc->pmxops;325	unsigned int nfuncs = ops->get_functions_count(pctldev);326	unsigned int selector = 0;327 328	/* See if this pctldev has this function */329	while (selector < nfuncs) {330		const char *fname = ops->get_function_name(pctldev, selector);331 332		if (!strcmp(function, fname))333			return selector;334 335		selector++;336	}337 338	return -EINVAL;339}340 341int pinmux_map_to_setting(const struct pinctrl_map *map,342			  struct pinctrl_setting *setting)343{344	struct pinctrl_dev *pctldev = setting->pctldev;345	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;346	char const * const *groups;347	unsigned int num_groups;348	int ret;349	const char *group;350 351	if (!pmxops) {352		dev_err(pctldev->dev, "does not support mux function\n");353		return -EINVAL;354	}355 356	ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function);357	if (ret < 0) {358		dev_err(pctldev->dev, "invalid function %s in map table\n",359			map->data.mux.function);360		return ret;361	}362	setting->data.mux.func = ret;363 364	ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,365					  &groups, &num_groups);366	if (ret < 0) {367		dev_err(pctldev->dev, "can't query groups for function %s\n",368			map->data.mux.function);369		return ret;370	}371	if (!num_groups) {372		dev_err(pctldev->dev,373			"function %s can't be selected on any group\n",374			map->data.mux.function);375		return -EINVAL;376	}377	if (map->data.mux.group) {378		group = map->data.mux.group;379		ret = match_string(groups, num_groups, group);380		if (ret < 0) {381			dev_err(pctldev->dev,382				"invalid group \"%s\" for function \"%s\"\n",383				group, map->data.mux.function);384			return ret;385		}386	} else {387		group = groups[0];388	}389 390	ret = pinctrl_get_group_selector(pctldev, group);391	if (ret < 0) {392		dev_err(pctldev->dev, "invalid group %s in map table\n",393			map->data.mux.group);394		return ret;395	}396	setting->data.mux.group = ret;397 398	return 0;399}400 401void pinmux_free_setting(const struct pinctrl_setting *setting)402{403	/* This function is currently unused */404}405 406int pinmux_enable_setting(const struct pinctrl_setting *setting)407{408	struct pinctrl_dev *pctldev = setting->pctldev;409	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;410	const struct pinmux_ops *ops = pctldev->desc->pmxops;411	int ret = 0;412	const unsigned int *pins = NULL;413	unsigned int num_pins = 0;414	int i;415	struct pin_desc *desc;416 417	if (pctlops->get_group_pins)418		ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,419					      &pins, &num_pins);420 421	if (ret) {422		const char *gname;423 424		/* errors only affect debug data, so just warn */425		gname = pctlops->get_group_name(pctldev,426						setting->data.mux.group);427		dev_warn(pctldev->dev,428			 "could not get pins for group %s\n",429			 gname);430		num_pins = 0;431	}432 433	/* Try to allocate all pins in this group, one by one */434	for (i = 0; i < num_pins; i++) {435		ret = pin_request(pctldev, pins[i], setting->dev_name, NULL);436		if (ret) {437			const char *gname;438			const char *pname;439 440			desc = pin_desc_get(pctldev, pins[i]);441			pname = desc ? desc->name : "non-existing";442			gname = pctlops->get_group_name(pctldev,443						setting->data.mux.group);444			dev_err_probe(pctldev->dev, ret,445				"could not request pin %d (%s) from group %s on device %s\n",446				pins[i], pname, gname,447				pinctrl_dev_get_name(pctldev));448			goto err_pin_request;449		}450	}451 452	/* Now that we have acquired the pins, encode the mux setting */453	for (i = 0; i < num_pins; i++) {454		desc = pin_desc_get(pctldev, pins[i]);455		if (desc == NULL) {456			dev_warn(pctldev->dev,457				 "could not get pin desc for pin %d\n",458				 pins[i]);459			continue;460		}461		desc->mux_setting = &(setting->data.mux);462	}463 464	ret = ops->set_mux(pctldev, setting->data.mux.func,465			   setting->data.mux.group);466 467	if (ret)468		goto err_set_mux;469 470	return 0;471 472err_set_mux:473	for (i = 0; i < num_pins; i++) {474		desc = pin_desc_get(pctldev, pins[i]);475		if (desc)476			desc->mux_setting = NULL;477	}478err_pin_request:479	/* On error release all taken pins */480	while (--i >= 0)481		pin_free(pctldev, pins[i], NULL);482 483	return ret;484}485 486void pinmux_disable_setting(const struct pinctrl_setting *setting)487{488	struct pinctrl_dev *pctldev = setting->pctldev;489	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;490	int ret = 0;491	const unsigned int *pins = NULL;492	unsigned int num_pins = 0;493	int i;494	struct pin_desc *desc;495 496	if (pctlops->get_group_pins)497		ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,498					      &pins, &num_pins);499	if (ret) {500		const char *gname;501 502		/* errors only affect debug data, so just warn */503		gname = pctlops->get_group_name(pctldev,504						setting->data.mux.group);505		dev_warn(pctldev->dev,506			 "could not get pins for group %s\n",507			 gname);508		num_pins = 0;509	}510 511	/* Flag the descs that no setting is active */512	for (i = 0; i < num_pins; i++) {513		desc = pin_desc_get(pctldev, pins[i]);514		if (desc == NULL) {515			dev_warn(pctldev->dev,516				 "could not get pin desc for pin %d\n",517				 pins[i]);518			continue;519		}520		if (desc->mux_setting == &(setting->data.mux)) {521			pin_free(pctldev, pins[i], NULL);522		} else {523			const char *gname;524 525			gname = pctlops->get_group_name(pctldev,526						setting->data.mux.group);527			dev_warn(pctldev->dev,528				 "not freeing pin %d (%s) as part of deactivating group %s - it is already used for some other setting",529				 pins[i], desc->name, gname);530		}531	}532}533 534#ifdef CONFIG_DEBUG_FS535 536/* Called from pincontrol core */537static int pinmux_functions_show(struct seq_file *s, void *what)538{539	struct pinctrl_dev *pctldev = s->private;540	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;541	unsigned int nfuncs;542	unsigned int func_selector = 0;543 544	if (!pmxops)545		return 0;546 547	mutex_lock(&pctldev->mutex);548	nfuncs = pmxops->get_functions_count(pctldev);549	while (func_selector < nfuncs) {550		const char *func = pmxops->get_function_name(pctldev,551							  func_selector);552		const char * const *groups;553		unsigned int num_groups;554		int ret;555		int i;556 557		ret = pmxops->get_function_groups(pctldev, func_selector,558						  &groups, &num_groups);559		if (ret) {560			seq_printf(s, "function %s: COULD NOT GET GROUPS\n",561				   func);562			func_selector++;563			continue;564		}565 566		seq_printf(s, "function %d: %s, groups = [ ", func_selector, func);567		for (i = 0; i < num_groups; i++)568			seq_printf(s, "%s ", groups[i]);569		seq_puts(s, "]\n");570 571		func_selector++;572	}573 574	mutex_unlock(&pctldev->mutex);575 576	return 0;577}578DEFINE_SHOW_ATTRIBUTE(pinmux_functions);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 int 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	else595		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			else624				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_owner630				   : "(MUX UNCLAIMED)",631				   desc->gpio_owner ? desc->gpio_owner632				   : "(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		else644			seq_putc(s, '\n');645	}646 647	mutex_unlock(&pctldev->mutex);648 649	return 0;650}651DEFINE_SHOW_ATTRIBUTE(pinmux_pins);652 653void pinmux_show_map(struct seq_file *s, const struct pinctrl_map *map)654{655	seq_printf(s, "group %s\nfunction %s\n",656		map->data.mux.group ? map->data.mux.group : "(default)",657		map->data.mux.function);658}659 660void pinmux_show_setting(struct seq_file *s,661			 const struct pinctrl_setting *setting)662{663	struct pinctrl_dev *pctldev = setting->pctldev;664	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;665	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;666 667	seq_printf(s, "group: %s (%u) function: %s (%u)\n",668		   pctlops->get_group_name(pctldev, setting->data.mux.group),669		   setting->data.mux.group,670		   pmxops->get_function_name(pctldev, setting->data.mux.func),671		   setting->data.mux.func);672}673 674static int pinmux_select_show(struct seq_file *s, void *unused)675{676	return -EPERM;677}678 679static ssize_t pinmux_select_write(struct file *file, const char __user *user_buf,680				   size_t len, loff_t *ppos)681{682	struct seq_file *sfile = file->private_data;683	struct pinctrl_dev *pctldev = sfile->private;684	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;685	const char *const *groups;686	char *buf, *gname, *fname;687	unsigned int num_groups;688	int fsel, gsel, ret;689 690	buf = memdup_user_nul(user_buf, len);691	if (IS_ERR(buf))692		return PTR_ERR(buf);693 694	/* remove leading and trailing spaces of input buffer */695	gname = strstrip(buf);696	if (*gname == '\0') {697		ret = -EINVAL;698		goto exit_free_buf;699	}700 701	/* find a separator which is a spacelike character */702	for (fname = gname; !isspace(*fname); fname++) {703		if (*fname == '\0') {704			ret = -EINVAL;705			goto exit_free_buf;706		}707	}708	*fname = '\0';709 710	/* drop extra spaces between function and group names */711	fname = skip_spaces(fname + 1);712	if (*fname == '\0') {713		ret = -EINVAL;714		goto exit_free_buf;715	}716 717	ret = pinmux_func_name_to_selector(pctldev, fname);718	if (ret < 0) {719		dev_err(pctldev->dev, "invalid function %s in map table\n", fname);720		goto exit_free_buf;721	}722	fsel = ret;723 724	ret = pmxops->get_function_groups(pctldev, fsel, &groups, &num_groups);725	if (ret) {726		dev_err(pctldev->dev, "no groups for function %d (%s)", fsel, fname);727		goto exit_free_buf;728	}729 730	ret = match_string(groups, num_groups, gname);731	if (ret < 0) {732		dev_err(pctldev->dev, "invalid group %s", gname);733		goto exit_free_buf;734	}735 736	ret = pinctrl_get_group_selector(pctldev, gname);737	if (ret < 0)738		goto exit_free_buf;739	gsel = ret;740 741	ret = pmxops->set_mux(pctldev, fsel, gsel);742	if (ret) {743		dev_err(pctldev->dev, "set_mux() failed: %d", ret);744		goto exit_free_buf;745	}746	ret = len;747 748exit_free_buf:749	kfree(buf);750 751	return ret;752}753DEFINE_SHOW_STORE_ATTRIBUTE(pinmux_select);754 755void pinmux_init_device_debugfs(struct dentry *devroot,756			 struct pinctrl_dev *pctldev)757{758	debugfs_create_file("pinmux-functions", 0444,759			    devroot, pctldev, &pinmux_functions_fops);760	debugfs_create_file("pinmux-pins", 0444,761			    devroot, pctldev, &pinmux_pins_fops);762	debugfs_create_file("pinmux-select", 0200,763			    devroot, pctldev, &pinmux_select_fops);764}765 766#endif /* CONFIG_DEBUG_FS */767 768#ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS769 770/**771 * pinmux_generic_get_function_count() - returns number of functions772 * @pctldev: pin controller device773 */774int pinmux_generic_get_function_count(struct pinctrl_dev *pctldev)775{776	return pctldev->num_functions;777}778EXPORT_SYMBOL_GPL(pinmux_generic_get_function_count);779 780/**781 * pinmux_generic_get_function_name() - returns the function name782 * @pctldev: pin controller device783 * @selector: function number784 */785const char *786pinmux_generic_get_function_name(struct pinctrl_dev *pctldev,787				 unsigned int selector)788{789	struct function_desc *function;790 791	function = radix_tree_lookup(&pctldev->pin_function_tree,792				     selector);793	if (!function)794		return NULL;795 796	return function->func.name;797}798EXPORT_SYMBOL_GPL(pinmux_generic_get_function_name);799 800/**801 * pinmux_generic_get_function_groups() - gets the function groups802 * @pctldev: pin controller device803 * @selector: function number804 * @groups: array of pin groups805 * @ngroups: number of pin groups806 */807int pinmux_generic_get_function_groups(struct pinctrl_dev *pctldev,808				       unsigned int selector,809				       const char * const **groups,810				       unsigned int * const ngroups)811{812	struct function_desc *function;813 814	function = radix_tree_lookup(&pctldev->pin_function_tree,815				     selector);816	if (!function) {817		dev_err(pctldev->dev, "%s could not find function%i\n",818			__func__, selector);819		return -EINVAL;820	}821	*groups = function->func.groups;822	*ngroups = function->func.ngroups;823 824	return 0;825}826EXPORT_SYMBOL_GPL(pinmux_generic_get_function_groups);827 828/**829 * pinmux_generic_get_function() - returns a function based on the number830 * @pctldev: pin controller device831 * @selector: function number832 */833struct function_desc *pinmux_generic_get_function(struct pinctrl_dev *pctldev,834						  unsigned int selector)835{836	struct function_desc *function;837 838	function = radix_tree_lookup(&pctldev->pin_function_tree,839				     selector);840	if (!function)841		return NULL;842 843	return function;844}845EXPORT_SYMBOL_GPL(pinmux_generic_get_function);846 847/**848 * pinmux_generic_add_function() - adds a function group849 * @pctldev: pin controller device850 * @name: name of the function851 * @groups: array of pin groups852 * @ngroups: number of pin groups853 * @data: pin controller driver specific data854 */855int pinmux_generic_add_function(struct pinctrl_dev *pctldev,856				const char *name,857				const char * const *groups,858				const unsigned int ngroups,859				void *data)860{861	struct function_desc *function;862	int selector, error;863 864	if (!name)865		return -EINVAL;866 867	selector = pinmux_func_name_to_selector(pctldev, name);868	if (selector >= 0)869		return selector;870 871	selector = pctldev->num_functions;872 873	function = devm_kzalloc(pctldev->dev, sizeof(*function), GFP_KERNEL);874	if (!function)875		return -ENOMEM;876 877	*function = PINCTRL_FUNCTION_DESC(name, groups, ngroups, data);878 879	error = radix_tree_insert(&pctldev->pin_function_tree, selector, function);880	if (error)881		return error;882 883	pctldev->num_functions++;884 885	return selector;886}887EXPORT_SYMBOL_GPL(pinmux_generic_add_function);888 889/**890 * pinmux_generic_remove_function() - removes a numbered function891 * @pctldev: pin controller device892 * @selector: function number893 *894 * Note that the caller must take care of locking.895 */896int pinmux_generic_remove_function(struct pinctrl_dev *pctldev,897				   unsigned int selector)898{899	struct function_desc *function;900 901	function = radix_tree_lookup(&pctldev->pin_function_tree,902				     selector);903	if (!function)904		return -ENOENT;905 906	radix_tree_delete(&pctldev->pin_function_tree, selector);907	devm_kfree(pctldev->dev, function);908 909	pctldev->num_functions--;910 911	return 0;912}913EXPORT_SYMBOL_GPL(pinmux_generic_remove_function);914 915/**916 * pinmux_generic_free_functions() - removes all functions917 * @pctldev: pin controller device918 *919 * Note that the caller must take care of locking. The pinctrl920 * functions are allocated with devm_kzalloc() so no need to free921 * them here.922 */923void pinmux_generic_free_functions(struct pinctrl_dev *pctldev)924{925	struct radix_tree_iter iter;926	void __rcu **slot;927 928	radix_tree_for_each_slot(slot, &pctldev->pin_function_tree, &iter, 0)929		radix_tree_delete(&pctldev->pin_function_tree, iter.index);930 931	pctldev->num_functions = 0;932}933 934#endif /* CONFIG_GENERIC_PINMUX_FUNCTIONS */935