pinmux.c 21.1 KB
Newer Older
1 2 3
/*
 * Core driver for the pin muxing portions of the pin control subsystem
 *
4
 * Copyright (C) 2011-2012 ST-Ericsson SA
5 6 7 8 9
 * Written on behalf of Linaro for ST-Ericsson
 * Based on bits of regulator core, gpio core and clk core
 *
 * Author: Linus Walleij <linus.walleij@linaro.org>
 *
10 11
 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
 *
12 13 14 15 16 17 18 19 20 21 22 23
 * License terms: GNU General Public License (GPL) version 2
 */
#define pr_fmt(fmt) "pinmux core: " fmt

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/radix-tree.h>
#include <linux/err.h>
#include <linux/list.h>
24
#include <linux/string.h>
25 26 27 28 29 30
#include <linux/sysfs.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
#include <linux/pinctrl/machine.h>
#include <linux/pinctrl/pinmux.h>
#include "core.h"
31
#include "pinmux.h"
32

33 34 35
int pinmux_check_ops(struct pinctrl_dev *pctldev)
{
	const struct pinmux_ops *ops = pctldev->desc->pmxops;
36
	unsigned nfuncs;
37 38 39
	unsigned selector = 0;

	/* Check that we implement required operations */
40 41
	if (!ops ||
	    !ops->get_functions_count ||
42 43
	    !ops->get_function_name ||
	    !ops->get_function_groups ||
44
	    !ops->set_mux) {
45
		dev_err(pctldev->dev, "pinmux ops lacks necessary functions\n");
46
		return -EINVAL;
47
	}
48
	/* Check that all functions registered have names */
49
	nfuncs = ops->get_functions_count(pctldev);
50
	while (selector < nfuncs) {
51 52 53
		const char *fname = ops->get_function_name(pctldev,
							   selector);
		if (!fname) {
54
			dev_err(pctldev->dev, "pinmux ops has no name for function%u\n",
55 56 57 58 59 60 61 62 63
				selector);
			return -EINVAL;
		}
		selector++;
	}

	return 0;
}

64
int pinmux_validate_map(const struct pinctrl_map *map, int i)
65 66 67 68 69 70 71 72 73 74
{
	if (!map->data.mux.function) {
		pr_err("failed to register map %s (%d): no function given\n",
		       map->name, i);
		return -EINVAL;
	}

	return 0;
}

75 76 77
/**
 * pin_request() - request a single pin to be muxed in, typically for GPIO
 * @pin: the pin number in the global pin space
78 79
 * @owner: a representation of the owner of this pin; typically the device
 *	name that controls its mux function, or the requested GPIO name
80 81 82 83
 * @gpio_range: the range matching the GPIO pin if this is a request for a
 *	single GPIO pin
 */
static int pin_request(struct pinctrl_dev *pctldev,
84
		       int pin, const char *owner,
85 86 87 88 89 90 91 92
		       struct pinctrl_gpio_range *gpio_range)
{
	struct pin_desc *desc;
	const struct pinmux_ops *ops = pctldev->desc->pmxops;
	int status = -EINVAL;

	desc = pin_desc_get(pctldev, pin);
	if (desc == NULL) {
93
		dev_err(pctldev->dev,
94 95
			"pin %d is not registered so it cannot be requested\n",
			pin);
96 97 98
		goto out;
	}

99 100 101
	dev_dbg(pctldev->dev, "request pin %d (%s) for %s\n",
		pin, desc->name, owner);

102 103 104 105 106 107 108 109 110 111 112 113 114 115
	if ((!gpio_range || ops->strict) &&
	    desc->mux_usecount && strcmp(desc->mux_owner, owner)) {
		dev_err(pctldev->dev,
			"pin %s already requested by %s; cannot claim for %s\n",
			desc->name, desc->mux_owner, owner);
		goto out;
	}

	if ((gpio_range || ops->strict) && desc->gpio_owner) {
		dev_err(pctldev->dev,
			"pin %s already requested by %s; cannot claim for %s\n",
			desc->name, desc->gpio_owner, owner);
		goto out;
	}
116

117
	if (gpio_range) {
118 119 120 121 122 123 124 125
		desc->gpio_owner = owner;
	} else {
		desc->mux_usecount++;
		if (desc->mux_usecount > 1)
			return 0;

		desc->mux_owner = owner;
	}
126 127 128

	/* Let each pin increase references to this module */
	if (!try_module_get(pctldev->owner)) {
129
		dev_err(pctldev->dev,
130 131 132 133 134 135 136 137 138 139
			"could not increase module refcount for pin %d\n",
			pin);
		status = -EINVAL;
		goto out_free_pin;
	}

	/*
	 * If there is no kind of request function for the pin we just assume
	 * we got it by default and proceed.
	 */
140
	if (gpio_range && ops->gpio_request_enable)
141 142 143 144 145 146 147
		/* This requests and enables a single GPIO pin */
		status = ops->gpio_request_enable(pctldev, gpio_range, pin);
	else if (ops->request)
		status = ops->request(pctldev, pin);
	else
		status = 0;

148
	if (status) {
149
		dev_err(pctldev->dev, "request() failed for pin %d\n", pin);
150 151 152
		module_put(pctldev->owner);
	}

153
out_free_pin:
154
	if (status) {
155 156 157 158 159 160 161
		if (gpio_range) {
			desc->gpio_owner = NULL;
		} else {
			desc->mux_usecount--;
			if (!desc->mux_usecount)
				desc->mux_owner = NULL;
		}
162
	}
163 164
out:
	if (status)
165
		dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
166
			pin, owner, status);
167 168 169 170 171 172 173 174

	return status;
}

/**
 * pin_free() - release a single muxed in pin so something else can be muxed
 * @pctldev: pin controller device handling this pin
 * @pin: the pin to free
175 176
 * @gpio_range: the range matching the GPIO pin if this is a request for a
 *	single GPIO pin
L
Linus Walleij 已提交
177
 *
178 179
 * This function returns a pointer to the previous owner. This is used
 * for callers that dynamically allocate an owner name so it can be freed
L
Linus Walleij 已提交
180
 * once the pin is free. This is done for GPIO request functions.
181
 */
182 183
static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
			    struct pinctrl_gpio_range *gpio_range)
184 185 186
{
	const struct pinmux_ops *ops = pctldev->desc->pmxops;
	struct pin_desc *desc;
187
	const char *owner;
188 189 190

	desc = pin_desc_get(pctldev, pin);
	if (desc == NULL) {
191
		dev_err(pctldev->dev,
192
			"pin is not registered so it cannot be freed\n");
193
		return NULL;
194 195
	}

196
	if (!gpio_range) {
197 198 199 200 201
		/*
		 * A pin should not be freed more times than allocated.
		 */
		if (WARN_ON(!desc->mux_usecount))
			return NULL;
202 203 204 205
		desc->mux_usecount--;
		if (desc->mux_usecount)
			return NULL;
	}
206

207 208 209 210 211 212 213
	/*
	 * If there is no kind of request function for the pin we just assume
	 * we got it by default and proceed.
	 */
	if (gpio_range && ops->gpio_disable_free)
		ops->gpio_disable_free(pctldev, gpio_range, pin);
	else if (ops->free)
214 215
		ops->free(pctldev, pin);

216 217 218 219 220 221 222 223 224
	if (gpio_range) {
		owner = desc->gpio_owner;
		desc->gpio_owner = NULL;
	} else {
		owner = desc->mux_owner;
		desc->mux_owner = NULL;
		desc->mux_setting = NULL;
	}

225
	module_put(pctldev->owner);
226

227
	return owner;
228 229 230
}

/**
231 232 233 234
 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
 * @pctldev: pin controller device affected
 * @pin: the pin to mux in for GPIO
 * @range: the applicable GPIO range
235
 */
236 237 238
int pinmux_request_gpio(struct pinctrl_dev *pctldev,
			struct pinctrl_gpio_range *range,
			unsigned pin, unsigned gpio)
239
{
240
	const char *owner;
241 242 243
	int ret;

	/* Conjure some name stating what chip and pin this is taken by */
244
	owner = kasprintf(GFP_KERNEL, "%s:%d", range->name, gpio);
245
	if (!owner)
246
		return -ENOMEM;
247

248
	ret = pin_request(pctldev, pin, owner, range);
249
	if (ret < 0)
250
		kfree(owner);
251 252

	return ret;
253 254 255
}

/**
256 257 258 259
 * pinmux_free_gpio() - release a pin from GPIO muxing
 * @pctldev: the pin controller device for the pin
 * @pin: the affected currently GPIO-muxed in pin
 * @range: applicable GPIO range
260
 */
261 262
void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
		      struct pinctrl_gpio_range *range)
263
{
264
	const char *owner;
265

266 267
	owner = pin_free(pctldev, pin, range);
	kfree(owner);
268 269
}

270 271 272 273 274 275 276 277 278 279
/**
 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
 * @pctldev: the pin controller handling this pin
 * @range: applicable GPIO range
 * @pin: the affected GPIO pin in this controller
 * @input: true if we set the pin as input, false for output
 */
int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
			  struct pinctrl_gpio_range *range,
			  unsigned pin, bool input)
280 281 282 283 284 285 286 287 288 289 290 291 292 293
{
	const struct pinmux_ops *ops;
	int ret;

	ops = pctldev->desc->pmxops;

	if (ops->gpio_set_direction)
		ret = ops->gpio_set_direction(pctldev, range, pin, input);
	else
		ret = 0;

	return ret;
}

294 295
static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
					const char *function)
296 297
{
	const struct pinmux_ops *ops = pctldev->desc->pmxops;
298
	unsigned nfuncs = ops->get_functions_count(pctldev);
299 300 301
	unsigned selector = 0;

	/* See if this pctldev has this function */
302
	while (selector < nfuncs) {
303
		const char *fname = ops->get_function_name(pctldev, selector);
304

305 306
		if (!strcmp(function, fname))
			return selector;
307 308 309 310

		selector++;
	}

311
	dev_err(pctldev->dev, "function '%s' not supported\n", function);
312 313 314
	return -EINVAL;
}

315
int pinmux_map_to_setting(const struct pinctrl_map *map,
316
			  struct pinctrl_setting *setting)
317
{
318 319 320 321
	struct pinctrl_dev *pctldev = setting->pctldev;
	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
	char const * const *groups;
	unsigned num_groups;
322
	int ret;
323
	const char *group;
324

325 326 327 328 329
	if (!pmxops) {
		dev_err(pctldev->dev, "does not support mux function\n");
		return -EINVAL;
	}

330
	ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function);
331 332 333
	if (ret < 0) {
		dev_err(pctldev->dev, "invalid function %s in map table\n",
			map->data.mux.function);
334
		return ret;
335
	}
336
	setting->data.mux.func = ret;
337

338
	ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
339
					  &groups, &num_groups);
340 341 342
	if (ret < 0) {
		dev_err(pctldev->dev, "can't query groups for function %s\n",
			map->data.mux.function);
343
		return ret;
344 345 346 347 348
	}
	if (!num_groups) {
		dev_err(pctldev->dev,
			"function %s can't be selected on any group\n",
			map->data.mux.function);
349
		return -EINVAL;
350
	}
351 352
	if (map->data.mux.group) {
		group = map->data.mux.group;
353 354
		ret = match_string(groups, num_groups, group);
		if (ret < 0) {
355 356 357
			dev_err(pctldev->dev,
				"invalid group \"%s\" for function \"%s\"\n",
				group, map->data.mux.function);
358
			return ret;
359
		}
360 361
	} else {
		group = groups[0];
362 363
	}

364
	ret = pinctrl_get_group_selector(pctldev, group);
365 366 367
	if (ret < 0) {
		dev_err(pctldev->dev, "invalid group %s in map table\n",
			map->data.mux.group);
368
		return ret;
369
	}
370
	setting->data.mux.group = ret;
371 372 373 374

	return 0;
}

375
void pinmux_free_setting(const struct pinctrl_setting *setting)
376
{
377
	/* This function is currently unused */
378 379
}

380
int pinmux_enable_setting(const struct pinctrl_setting *setting)
381
{
382
	struct pinctrl_dev *pctldev = setting->pctldev;
383
	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
384
	const struct pinmux_ops *ops = pctldev->desc->pmxops;
385 386 387
	int ret = 0;
	const unsigned *pins = NULL;
	unsigned num_pins = 0;
388 389 390
	int i;
	struct pin_desc *desc;

391 392 393 394
	if (pctlops->get_group_pins)
		ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
					      &pins, &num_pins);

395
	if (ret) {
396 397
		const char *gname;

398
		/* errors only affect debug data, so just warn */
399 400
		gname = pctlops->get_group_name(pctldev,
						setting->data.mux.group);
401
		dev_warn(pctldev->dev,
402 403
			 "could not get pins for group %s\n",
			 gname);
404 405 406
		num_pins = 0;
	}

407 408 409 410
	/* Try to allocate all pins in this group, one by one */
	for (i = 0; i < num_pins; i++) {
		ret = pin_request(pctldev, pins[i], setting->dev_name, NULL);
		if (ret) {
411 412 413 414 415 416 417
			const char *gname;
			const char *pname;

			desc = pin_desc_get(pctldev, pins[i]);
			pname = desc ? desc->name : "non-existing";
			gname = pctlops->get_group_name(pctldev,
						setting->data.mux.group);
418
			dev_err(pctldev->dev,
419 420 421 422
				"could not request pin %d (%s) from group %s "
				" on device %s\n",
				pins[i], pname, gname,
				pinctrl_dev_get_name(pctldev));
423
			goto err_pin_request;
424 425 426 427
		}
	}

	/* Now that we have acquired the pins, encode the mux setting */
428 429 430 431 432 433 434 435 436 437
	for (i = 0; i < num_pins; i++) {
		desc = pin_desc_get(pctldev, pins[i]);
		if (desc == NULL) {
			dev_warn(pctldev->dev,
				 "could not get pin desc for pin %d\n",
				 pins[i]);
			continue;
		}
		desc->mux_setting = &(setting->data.mux);
	}
438

439 440
	ret = ops->set_mux(pctldev, setting->data.mux.func,
			   setting->data.mux.group);
441 442

	if (ret)
443
		goto err_set_mux;
444 445 446

	return 0;

447
err_set_mux:
448 449 450 451 452 453 454 455 456 457 458
	for (i = 0; i < num_pins; i++) {
		desc = pin_desc_get(pctldev, pins[i]);
		if (desc)
			desc->mux_setting = NULL;
	}
err_pin_request:
	/* On error release all taken pins */
	while (--i >= 0)
		pin_free(pctldev, pins[i], NULL);

	return ret;
459 460
}

461
void pinmux_disable_setting(const struct pinctrl_setting *setting)
462
{
463
	struct pinctrl_dev *pctldev = setting->pctldev;
464
	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
465 466 467
	int ret = 0;
	const unsigned *pins = NULL;
	unsigned num_pins = 0;
468 469 470
	int i;
	struct pin_desc *desc;

471 472 473
	if (pctlops->get_group_pins)
		ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
					      &pins, &num_pins);
474
	if (ret) {
475 476
		const char *gname;

477
		/* errors only affect debug data, so just warn */
478 479
		gname = pctlops->get_group_name(pctldev,
						setting->data.mux.group);
480
		dev_warn(pctldev->dev,
481 482
			 "could not get pins for group %s\n",
			 gname);
483 484 485
		num_pins = 0;
	}

486
	/* Flag the descs that no setting is active */
487 488 489 490 491 492 493 494
	for (i = 0; i < num_pins; i++) {
		desc = pin_desc_get(pctldev, pins[i]);
		if (desc == NULL) {
			dev_warn(pctldev->dev,
				 "could not get pin desc for pin %d\n",
				 pins[i]);
			continue;
		}
495 496 497 498
		if (desc->mux_setting == &(setting->data.mux)) {
			desc->mux_setting = NULL;
			/* And release the pin */
			pin_free(pctldev, pins[i], NULL);
499 500 501 502 503 504 505 506 507
		} else {
			const char *gname;

			gname = pctlops->get_group_name(pctldev,
						setting->data.mux.group);
			dev_warn(pctldev->dev,
				 "not freeing pin %d (%s) as part of "
				 "deactivating group %s - it is already "
				 "used for some other setting",
508
				 pins[i], desc->name, gname);
509
		}
510
	}
511 512 513 514 515 516 517 518 519
}

#ifdef CONFIG_DEBUG_FS

/* Called from pincontrol core */
static int pinmux_functions_show(struct seq_file *s, void *what)
{
	struct pinctrl_dev *pctldev = s->private;
	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
520
	unsigned nfuncs;
521 522
	unsigned func_selector = 0;

523 524
	if (!pmxops)
		return 0;
525

526
	mutex_lock(&pctldev->mutex);
527
	nfuncs = pmxops->get_functions_count(pctldev);
528
	while (func_selector < nfuncs) {
529 530 531 532 533 534 535 536 537
		const char *func = pmxops->get_function_name(pctldev,
							  func_selector);
		const char * const *groups;
		unsigned num_groups;
		int ret;
		int i;

		ret = pmxops->get_function_groups(pctldev, func_selector,
						  &groups, &num_groups);
538
		if (ret) {
539 540
			seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
				   func);
541 542 543
			func_selector++;
			continue;
		}
544 545 546 547 548 549 550 551 552

		seq_printf(s, "function: %s, groups = [ ", func);
		for (i = 0; i < num_groups; i++)
			seq_printf(s, "%s ", groups[i]);
		seq_puts(s, "]\n");

		func_selector++;
	}

553
	mutex_unlock(&pctldev->mutex);
554

555 556 557 558 559 560
	return 0;
}

static int pinmux_pins_show(struct seq_file *s, void *what)
{
	struct pinctrl_dev *pctldev = s->private;
561 562
	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
563
	unsigned i, pin;
564

565 566 567
	if (!pmxops)
		return 0;

568
	seq_puts(s, "Pinmux settings per pin\n");
569 570 571 572 573 574
	if (pmxops->strict)
		seq_puts(s,
		 "Format: pin (name): mux_owner|gpio_owner (strict) hog?\n");
	else
		seq_puts(s,
		"Format: pin (name): mux_owner gpio_owner hog?\n");
575

576
	mutex_lock(&pctldev->mutex);
577

578 579
	/* The pin number can be retrived from the pin controller descriptor */
	for (i = 0; i < pctldev->desc->npins; i++) {
580
		struct pin_desc *desc;
581
		bool is_hog = false;
582

583
		pin = pctldev->desc->pins[i].number;
584
		desc = pin_desc_get(pctldev, pin);
585
		/* Skip if we cannot search the pin */
586 587 588
		if (desc == NULL)
			continue;

589 590
		if (desc->mux_owner &&
		    !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
591 592
			is_hog = true;

593 594 595
		if (pmxops->strict) {
			if (desc->mux_owner)
				seq_printf(s, "pin %d (%s): device %s%s",
596
					   pin, desc->name, desc->mux_owner,
597 598 599
					   is_hog ? " (HOG)" : "");
			else if (desc->gpio_owner)
				seq_printf(s, "pin %d (%s): GPIO %s",
600
					   pin, desc->name, desc->gpio_owner);
601 602
			else
				seq_printf(s, "pin %d (%s): UNCLAIMED",
603
					   pin, desc->name);
604 605
		} else {
			/* For non-strict controllers */
606
			seq_printf(s, "pin %d (%s): %s %s%s", pin, desc->name,
607 608 609 610 611 612
				   desc->mux_owner ? desc->mux_owner
				   : "(MUX UNCLAIMED)",
				   desc->gpio_owner ? desc->gpio_owner
				   : "(GPIO UNCLAIMED)",
				   is_hog ? " (HOG)" : "");
		}
613

614
		/* If mux: print function+group claiming the pin */
615 616 617 618 619 620 621 622
		if (desc->mux_setting)
			seq_printf(s, " function %s group %s\n",
				   pmxops->get_function_name(pctldev,
					desc->mux_setting->func),
				   pctlops->get_group_name(pctldev,
					desc->mux_setting->group));
		else
			seq_printf(s, "\n");
623 624
	}

625
	mutex_unlock(&pctldev->mutex);
626

627 628 629
	return 0;
}

630
void pinmux_show_map(struct seq_file *s, const struct pinctrl_map *map)
631 632 633 634 635 636 637
{
	seq_printf(s, "group %s\nfunction %s\n",
		map->data.mux.group ? map->data.mux.group : "(default)",
		map->data.mux.function);
}

void pinmux_show_setting(struct seq_file *s,
638
			 const struct pinctrl_setting *setting)
639
{
640 641 642 643
	struct pinctrl_dev *pctldev = setting->pctldev;
	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;

644 645 646 647 648
	seq_printf(s, "group: %s (%u) function: %s (%u)\n",
		   pctlops->get_group_name(pctldev, setting->data.mux.group),
		   setting->data.mux.group,
		   pmxops->get_function_name(pctldev, setting->data.mux.func),
		   setting->data.mux.func);
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
}

static int pinmux_functions_open(struct inode *inode, struct file *file)
{
	return single_open(file, pinmux_functions_show, inode->i_private);
}

static int pinmux_pins_open(struct inode *inode, struct file *file)
{
	return single_open(file, pinmux_pins_show, inode->i_private);
}

static const struct file_operations pinmux_functions_ops = {
	.open		= pinmux_functions_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

static const struct file_operations pinmux_pins_ops = {
	.open		= pinmux_pins_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

void pinmux_init_device_debugfs(struct dentry *devroot,
			 struct pinctrl_dev *pctldev)
{
	debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
			    devroot, pctldev, &pinmux_functions_ops);
	debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
			    devroot, pctldev, &pinmux_pins_ops);
}

#endif /* CONFIG_DEBUG_FS */
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765

#ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS

/**
 * pinmux_generic_get_function_count() - returns number of functions
 * @pctldev: pin controller device
 */
int pinmux_generic_get_function_count(struct pinctrl_dev *pctldev)
{
	return pctldev->num_functions;
}
EXPORT_SYMBOL_GPL(pinmux_generic_get_function_count);

/**
 * pinmux_generic_get_function_name() - returns the function name
 * @pctldev: pin controller device
 * @selector: function number
 */
const char *
pinmux_generic_get_function_name(struct pinctrl_dev *pctldev,
				 unsigned int selector)
{
	struct function_desc *function;

	function = radix_tree_lookup(&pctldev->pin_function_tree,
				     selector);
	if (!function)
		return NULL;

	return function->name;
}
EXPORT_SYMBOL_GPL(pinmux_generic_get_function_name);

/**
 * pinmux_generic_get_function_groups() - gets the function groups
 * @pctldev: pin controller device
 * @selector: function number
 * @groups: array of pin groups
 * @num_groups: number of pin groups
 */
int pinmux_generic_get_function_groups(struct pinctrl_dev *pctldev,
				       unsigned int selector,
				       const char * const **groups,
				       unsigned * const num_groups)
{
	struct function_desc *function;

	function = radix_tree_lookup(&pctldev->pin_function_tree,
				     selector);
	if (!function) {
		dev_err(pctldev->dev, "%s could not find function%i\n",
			__func__, selector);
		return -EINVAL;
	}
	*groups = function->group_names;
	*num_groups = function->num_group_names;

	return 0;
}
EXPORT_SYMBOL_GPL(pinmux_generic_get_function_groups);

/**
 * pinmux_generic_get_function() - returns a function based on the number
 * @pctldev: pin controller device
 * @group_selector: function number
 */
struct function_desc *pinmux_generic_get_function(struct pinctrl_dev *pctldev,
						  unsigned int selector)
{
	struct function_desc *function;

	function = radix_tree_lookup(&pctldev->pin_function_tree,
				     selector);
	if (!function)
		return NULL;

	return function;
}
EXPORT_SYMBOL_GPL(pinmux_generic_get_function);

/**
766
 * pinmux_generic_add_function() - adds a function group
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
 * @pctldev: pin controller device
 * @name: name of the function
 * @groups: array of pin groups
 * @num_groups: number of pin groups
 * @data: pin controller driver specific data
 */
int pinmux_generic_add_function(struct pinctrl_dev *pctldev,
				const char *name,
				const char **groups,
				const unsigned int num_groups,
				void *data)
{
	struct function_desc *function;

	function = devm_kzalloc(pctldev->dev, sizeof(*function), GFP_KERNEL);
	if (!function)
		return -ENOMEM;

	function->name = name;
	function->group_names = groups;
	function->num_group_names = num_groups;
	function->data = data;

	radix_tree_insert(&pctldev->pin_function_tree, pctldev->num_functions,
			  function);

	pctldev->num_functions++;

	return 0;
}
EXPORT_SYMBOL_GPL(pinmux_generic_add_function);

/**
 * pinmux_generic_remove_function() - removes a numbered function
 * @pctldev: pin controller device
 * @selector: function number
 *
 * Note that the caller must take care of locking.
 */
int pinmux_generic_remove_function(struct pinctrl_dev *pctldev,
				   unsigned int selector)
{
	struct function_desc *function;

	function = radix_tree_lookup(&pctldev->pin_function_tree,
				     selector);
	if (!function)
		return -ENOENT;

	radix_tree_delete(&pctldev->pin_function_tree, selector);
	devm_kfree(pctldev->dev, function);

	pctldev->num_functions--;

	return 0;
}
EXPORT_SYMBOL_GPL(pinmux_generic_remove_function);

/**
 * pinmux_generic_free_functions() - removes all functions
 * @pctldev: pin controller device
 *
829 830 831
 * Note that the caller must take care of locking. The pinctrl
 * functions are allocated with devm_kzalloc() so no need to free
 * them here.
832 833 834 835 836 837 838
 */
void pinmux_generic_free_functions(struct pinctrl_dev *pctldev)
{
	struct radix_tree_iter iter;
	void **slot;

	radix_tree_for_each_slot(slot, &pctldev->pin_function_tree, &iter, 0)
839
		radix_tree_delete(&pctldev->pin_function_tree, iter.index);
840 841 842 843 844

	pctldev->num_functions = 0;
}

#endif /* CONFIG_GENERIC_PINMUX_FUNCTIONS */