pinmux.c 18.2 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 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * 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>
 *
 * 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>
#include <linux/mutex.h>
#include <linux/spinlock.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 36 37 38 39 40 41 42

/**
 * struct pinmux_group - group list item for pinmux groups
 * @node: pinmux group list node
 * @group_selector: the group selector for this group
 */
struct pinmux_group {
	struct list_head node;
	unsigned group_selector;
};

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
int pinmux_check_ops(struct pinctrl_dev *pctldev)
{
	const struct pinmux_ops *ops = pctldev->desc->pmxops;
	unsigned selector = 0;

	/* Check that we implement required operations */
	if (!ops->list_functions ||
	    !ops->get_function_name ||
	    !ops->get_function_groups ||
	    !ops->enable ||
	    !ops->disable)
		return -EINVAL;

	/* Check that all functions registered have names */
	while (ops->list_functions(pctldev, selector) >= 0) {
		const char *fname = ops->get_function_name(pctldev,
							   selector);
		if (!fname) {
			pr_err("pinmux ops has no name for function%u\n",
				selector);
			return -EINVAL;
		}
		selector++;
	}

	return 0;
}

71 72 73
/**
 * pin_request() - request a single pin to be muxed in, typically for GPIO
 * @pin: the pin number in the global pin space
74 75
 * @owner: a representation of the owner of this pin; typically the device
 *	name that controls its mux function, or the requested GPIO name
76 77 78 79
 * @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,
80
		       int pin, const char *owner,
81 82 83 84 85 86
		       struct pinctrl_gpio_range *gpio_range)
{
	struct pin_desc *desc;
	const struct pinmux_ops *ops = pctldev->desc->pmxops;
	int status = -EINVAL;

87
	dev_dbg(pctldev->dev, "request pin %d for %s\n", pin, owner);
88 89 90

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

	spin_lock(&desc->lock);
97
	if (desc->owner && strcmp(desc->owner, owner)) {
98
		spin_unlock(&desc->lock);
99
		dev_err(pctldev->dev,
100 101 102
			"pin already requested\n");
		goto out;
	}
103
	desc->owner = owner;
104 105 106 107
	spin_unlock(&desc->lock);

	/* Let each pin increase references to this module */
	if (!try_module_get(pctldev->owner)) {
108
		dev_err(pctldev->dev,
109 110 111 112 113 114 115 116 117 118
			"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.
	 */
119
	if (gpio_range && ops->gpio_request_enable)
120 121 122 123 124 125 126 127
		/* 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;

	if (status)
128
		dev_err(pctldev->dev, "->request on device %s failed for pin %d\n",
129 130 131 132
		       pctldev->desc->name, pin);
out_free_pin:
	if (status) {
		spin_lock(&desc->lock);
133
		desc->owner = NULL;
134 135 136 137
		spin_unlock(&desc->lock);
	}
out:
	if (status)
138
		dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
139
		       pin, owner, status);
140 141 142 143 144 145 146 147

	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
148 149
 * @gpio_range: the range matching the GPIO pin if this is a request for a
 *	single GPIO pin
L
Linus Walleij 已提交
150
 *
151 152
 * 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 已提交
153
 * once the pin is free. This is done for GPIO request functions.
154
 */
155 156
static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
			    struct pinctrl_gpio_range *gpio_range)
157 158 159
{
	const struct pinmux_ops *ops = pctldev->desc->pmxops;
	struct pin_desc *desc;
160
	const char *owner;
161 162 163

	desc = pin_desc_get(pctldev, pin);
	if (desc == NULL) {
164
		dev_err(pctldev->dev,
165
			"pin is not registered so it cannot be freed\n");
166
		return NULL;
167 168
	}

169 170 171 172 173 174 175
	/*
	 * 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)
176 177 178
		ops->free(pctldev, pin);

	spin_lock(&desc->lock);
179 180
	owner = desc->owner;
	desc->owner = NULL;
181 182
	spin_unlock(&desc->lock);
	module_put(pctldev->owner);
183

184
	return owner;
185 186 187
}

/**
188 189 190 191
 * 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
192
 */
193 194 195
int pinmux_request_gpio(struct pinctrl_dev *pctldev,
			struct pinctrl_gpio_range *range,
			unsigned pin, unsigned gpio)
196 197
{
	char gpiostr[16];
198
	const char *owner;
199 200 201 202 203
	int ret;

	/* Conjure some name stating what chip and pin this is taken by */
	snprintf(gpiostr, 15, "%s:%d", range->name, gpio);

204 205
	owner = kstrdup(gpiostr, GFP_KERNEL);
	if (!owner)
206 207
		return -EINVAL;

208
	ret = pin_request(pctldev, pin, owner, range);
209
	if (ret < 0)
210
		kfree(owner);
211 212

	return ret;
213 214 215
}

/**
216 217 218 219
 * 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
220
 */
221 222
void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
		      struct pinctrl_gpio_range *range)
223
{
224
	const char *owner;
225

226 227
	owner = pin_free(pctldev, pin, range);
	kfree(owner);
228 229
}

230 231 232 233 234 235 236 237 238 239
/**
 * 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)
240 241 242 243 244 245 246 247 248 249 250 251 252 253
{
	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;
}

254
/**
T
Tony Lindgren 已提交
255
 * acquire_pins() - acquire all the pins for a certain function on a pinmux
256
 * @pctldev: the device to take the pins on
257 258
 * @owner: a representation of the owner of this pin; typically the device
 *	name that controls its mux function
259 260 261
 * @group_selector: the group selector containing the pins to acquire
 */
static int acquire_pins(struct pinctrl_dev *pctldev,
262
			const char *owner,
263 264 265
			unsigned group_selector)
{
	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
266
	const unsigned *pins;
267 268 269 270 271 272 273 274 275
	unsigned num_pins;
	int ret;
	int i;

	ret = pctlops->get_group_pins(pctldev, group_selector,
				      &pins, &num_pins);
	if (ret)
		return ret;

276
	dev_dbg(pctldev->dev, "requesting the %u pins from group %u\n",
277 278 279 280
		num_pins, group_selector);

	/* Try to allocate all pins in this group, one by one */
	for (i = 0; i < num_pins; i++) {
281
		ret = pin_request(pctldev, pins[i], owner, NULL);
282
		if (ret) {
283
			dev_err(pctldev->dev,
284 285
				"could not get request pin %d on device %s - conflicting mux mappings?\n",
				pins[i],
286 287 288 289
				pinctrl_dev_get_name(pctldev));
			/* On error release all taken pins */
			i--; /* this pin just failed */
			for (; i >= 0; i--)
290
				pin_free(pctldev, pins[i], NULL);
291 292 293 294 295 296 297 298
			return -ENODEV;
		}
	}
	return 0;
}

/**
 * release_pins() - release pins taken by earlier acquirement
T
Tony Lindgren 已提交
299
 * @pctldev: the device to free the pins on
300 301 302 303 304 305
 * @group_selector: the group selector containing the pins to free
 */
static void release_pins(struct pinctrl_dev *pctldev,
			 unsigned group_selector)
{
	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
306
	const unsigned *pins;
307 308 309 310 311 312 313
	unsigned num_pins;
	int ret;
	int i;

	ret = pctlops->get_group_pins(pctldev, group_selector,
				      &pins, &num_pins);
	if (ret) {
314
		dev_err(pctldev->dev, "could not get pins to release for group selector %d\n",
315 316 317 318
			group_selector);
		return;
	}
	for (i = 0; i < num_pins; i++)
319
		pin_free(pctldev, pins[i], NULL);
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
}

/**
 * pinmux_check_pin_group() - check function and pin group combo
 * @pctldev: device to check the pin group vs function for
 * @func_selector: the function selector to check the pin group for, we have
 *	already looked this up in the calling function
 * @pin_group: the pin group to match to the function
 *
 * This function will check that the pinmux driver can supply the
 * selected pin group for a certain function, returns the group selector if
 * the group and function selector will work fine together, else returns
 * negative
 */
static int pinmux_check_pin_group(struct pinctrl_dev *pctldev,
				  unsigned func_selector,
				  const char *pin_group)
{
	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
	int ret;

	/*
	 * If the driver does not support different pin groups for the
	 * functions, we only support group 0, and assume this exists.
	 */
	if (!pctlops || !pctlops->list_groups)
		return 0;

	/*
	 * Passing NULL (no specific group) will select the first and
	 * hopefully only group of pins available for this function.
	 */
	if (!pin_group) {
		char const * const *groups;
		unsigned num_groups;

		ret = pmxops->get_function_groups(pctldev, func_selector,
						  &groups, &num_groups);
		if (ret)
			return ret;
		if (num_groups < 1)
			return -EINVAL;
363
		ret = pinctrl_get_group_selector(pctldev, groups[0]);
364
		if (ret < 0) {
365
			dev_err(pctldev->dev,
366
				"function %s wants group %s but the pin controller does not seem to have that group\n",
367 368 369 370 371 372
				pmxops->get_function_name(pctldev, func_selector),
				groups[0]);
			return ret;
		}

		if (num_groups > 1)
373
			dev_dbg(pctldev->dev,
374
				"function %s support more than one group, default-selecting first group %s (%d)\n",
375 376 377 378 379 380 381
				pmxops->get_function_name(pctldev, func_selector),
				groups[0],
				ret);

		return ret;
	}

382
	dev_dbg(pctldev->dev,
383 384 385
		"check if we have pin group %s on controller %s\n",
		pin_group, pinctrl_dev_get_name(pctldev));

386
	ret = pinctrl_get_group_selector(pctldev, pin_group);
387
	if (ret < 0) {
388
		dev_dbg(pctldev->dev,
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
			"%s does not support pin group %s with function %s\n",
			pinctrl_dev_get_name(pctldev),
			pin_group,
			pmxops->get_function_name(pctldev, func_selector));
	}
	return ret;
}

/**
 * pinmux_search_function() - check pin control driver for a certain function
 * @pctldev: device to check for function and position
 * @map: function map containing the function and position to look for
 * @func_selector: returns the applicable function selector if found
 * @group_selector: returns the applicable group selector if found
 *
 * This will search the pinmux driver for an applicable
 * function with a specific pin group, returns 0 if these can be mapped
 * negative otherwise
 */
static int pinmux_search_function(struct pinctrl_dev *pctldev,
409
				  struct pinctrl_map const *map,
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
				  unsigned *func_selector,
				  unsigned *group_selector)
{
	const struct pinmux_ops *ops = pctldev->desc->pmxops;
	unsigned selector = 0;

	/* See if this pctldev has this function */
	while (ops->list_functions(pctldev, selector) >= 0) {
		const char *fname = ops->get_function_name(pctldev,
							   selector);
		int ret;

		if (!strcmp(map->function, fname)) {
			/* Found the function, check pin group */
			ret = pinmux_check_pin_group(pctldev, selector,
						     map->group);
			if (ret < 0)
				return ret;

			/* This function and group selector can be used */
			*func_selector = selector;
			*group_selector = ret;
			return 0;

		}
		selector++;
	}

	pr_err("%s does not support function %s\n",
	       pinctrl_dev_get_name(pctldev), map->function);
	return -EINVAL;
}

/**
 * pinmux_enable_muxmap() - enable a map entry for a certain pinmux
 */
static int pinmux_enable_muxmap(struct pinctrl_dev *pctldev,
447
				struct pinctrl *p,
448 449
				struct device *dev,
				const char *devname,
450
				struct pinctrl_map const *map)
451 452 453 454 455 456 457 458 459 460 461 462 463
{
	unsigned func_selector;
	unsigned group_selector;
	struct pinmux_group *grp;
	int ret;

	/*
	 * Note that we're not locking the pinmux mutex here, because
	 * this is only called at pinmux initialization time when it
	 * has not been added to any list and thus is not reachable
	 * by anyone else.
	 */

464
	if (p->pctldev && p->pctldev != pctldev) {
465
		dev_err(pctldev->dev,
466 467
			"different pin control devices given for device %s, function %s\n",
			devname, map->function);
468 469
		return -EINVAL;
	}
470 471
	p->dev = dev;
	p->pctldev = pctldev;
472 473 474 475 476 477 478 479 480 481 482 483

	/* Now go into the driver and try to match a function and group */
	ret = pinmux_search_function(pctldev, map, &func_selector,
				     &group_selector);
	if (ret < 0)
		return ret;

	/*
	 * If the function selector is already set, it needs to be identical,
	 * we support several groups with one function but not several
	 * functions with one or several groups in the same pinmux.
	 */
484 485
	if (p->func_selector != UINT_MAX &&
	    p->func_selector != func_selector) {
486
		dev_err(pctldev->dev,
487 488 489 490
			"dual function defines in the map for device %s\n",
		       devname);
		return -EINVAL;
	}
491
	p->func_selector = func_selector;
492 493

	/* Now add this group selector, we may have many of them */
494
	grp = kmalloc(sizeof(*grp), GFP_KERNEL);
495 496 497
	if (!grp)
		return -ENOMEM;
	grp->group_selector = group_selector;
498
	ret = acquire_pins(pctldev, devname, group_selector);
499 500 501 502
	if (ret) {
		kfree(grp);
		return ret;
	}
503
	list_add_tail(&grp->node, &p->groups);
504 505 506 507 508

	return 0;
}

/**
509
 * pinmux_apply_muxmap() - apply a certain mux mapping entry
510
 */
511 512 513 514 515
int pinmux_apply_muxmap(struct pinctrl_dev *pctldev,
			struct pinctrl *p,
			struct device *dev,
			const char *devname,
			struct pinctrl_map const *map)
516
{
517
	int ret;
518

519 520 521 522 523
	ret = pinmux_enable_muxmap(pctldev, p, dev,
				   devname, map);
	if (ret) {
		pinmux_put(p);
		return ret;
524 525
	}

526
	return 0;
527 528 529
}

/**
530
 * pinmux_put() - free up the pinmux portions of a pin controller handle
531
 */
532
void pinmux_put(struct pinctrl *p)
533
{
534
	struct list_head *node, *tmp;
535

536 537 538 539 540 541 542 543
	list_for_each_safe(node, tmp, &p->groups) {
		struct pinmux_group *grp =
			list_entry(node, struct pinmux_group, node);
		/* Release all pins taken by this group */
		release_pins(p->pctldev, grp->group_selector);
		list_del(node);
		kfree(grp);
	}
544 545 546
}

/**
547
 * pinmux_enable() - enable the pinmux portion of a pin control handle
548
 */
549
int pinmux_enable(struct pinctrl *p)
550
{
551 552 553 554
	struct pinctrl_dev *pctldev = p->pctldev;
	const struct pinmux_ops *ops = pctldev->desc->pmxops;
	struct pinmux_group *grp;
	int ret;
555

556 557 558 559 560 561 562 563 564
	list_for_each_entry(grp, &p->groups, node) {
		ret = ops->enable(pctldev, p->func_selector,
				  grp->group_selector);
		if (ret)
			/*
			 * TODO: call disable() on all groups we called
			 * enable() on to this point?
			 */
			return ret;
565
	}
566
	return 0;
567 568 569
}

/**
570
 * pinmux_disable() - disable the pinmux portions of a pin control handle
571
 */
572
void pinmux_disable(struct pinctrl *p)
573
{
574 575 576
	struct pinctrl_dev *pctldev = p->pctldev;
	const struct pinmux_ops *ops = pctldev->desc->pmxops;
	struct pinmux_group *grp;
577

578 579 580
	list_for_each_entry(grp, &p->groups, node) {
		ops->disable(pctldev, p->func_selector,
			     grp->group_selector);
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
	}
}

#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;
	unsigned func_selector = 0;

	while (pmxops->list_functions(pctldev, func_selector) >= 0) {
		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);
		if (ret)
			seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
				   func);

		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++;

	}

	return 0;
}

static int pinmux_pins_show(struct seq_file *s, void *what)
{
	struct pinctrl_dev *pctldev = s->private;
622
	unsigned i, pin;
623 624

	seq_puts(s, "Pinmux settings per pin\n");
625
	seq_puts(s, "Format: pin (name): owner\n");
626

627 628
	/* The pin number can be retrived from the pin controller descriptor */
	for (i = 0; i < pctldev->desc->npins; i++) {
629 630 631

		struct pin_desc *desc;

632
		pin = pctldev->desc->pins[i].number;
633
		desc = pin_desc_get(pctldev, pin);
634
		/* Skip if we cannot search the pin */
635 636 637 638 639
		if (desc == NULL)
			continue;

		seq_printf(s, "pin %d (%s): %s\n", pin,
			   desc->name ? desc->name : "unnamed",
640
			   desc->owner ? desc->owner : "UNCLAIMED");
641 642 643 644 645
	}

	return 0;
}

646
void pinmux_dbg_show(struct seq_file *s, struct pinctrl *p)
647
{
648 649 650 651
	struct pinctrl_dev *pctldev = p->pctldev;
	const struct pinmux_ops *pmxops;
	const struct pinctrl_ops *pctlops;
	struct pinmux_group *grp;
652

653 654
	pmxops = pctldev->desc->pmxops;
	pctlops = pctldev->desc->pctlops;
655

656 657 658 659
	seq_printf(s, " function: %s (%u),",
		   pmxops->get_function_name(pctldev,
					     p->func_selector),
		   p->func_selector);
660

661 662 663 664 665 666
	seq_printf(s, " groups: [");
	list_for_each_entry(grp, &p->groups, node) {
		seq_printf(s, " %s (%u)",
			   pctlops->get_group_name(pctldev,
						   grp->group_selector),
			   grp->group_selector);
667
	}
668
	seq_printf(s, " ]");
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
}

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 */