pinmux.c 16.4 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->enable) {
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 65 66 67 68 69 70 71 72 73 74
int pinmux_validate_map(struct pinctrl_map const *map, int i)
{
	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
	if (gpio_range) {
		/* There's no need to support multiple GPIO requests */
		if (desc->gpio_owner) {
			dev_err(pctldev->dev,
106 107
				"pin %s already requested by %s; cannot claim for %s\n",
				desc->name, desc->gpio_owner, owner);
108 109
			goto out;
		}
110

111 112 113 114
		desc->gpio_owner = owner;
	} else {
		if (desc->mux_usecount && strcmp(desc->mux_owner, owner)) {
			dev_err(pctldev->dev,
115 116
				"pin %s already requested by %s; cannot claim for %s\n",
				desc->name, desc->mux_owner, owner);
117 118
			goto out;
		}
119

120 121 122 123 124 125
		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 197 198 199 200
	if (!gpio_range) {
		desc->mux_usecount--;
		if (desc->mux_usecount)
			return NULL;
	}
201

202 203 204 205 206 207 208
	/*
	 * 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)
209 210
		ops->free(pctldev, pin);

211 212 213 214 215 216 217 218 219
	if (gpio_range) {
		owner = desc->gpio_owner;
		desc->gpio_owner = NULL;
	} else {
		owner = desc->mux_owner;
		desc->mux_owner = NULL;
		desc->mux_setting = NULL;
	}

220
	module_put(pctldev->owner);
221

222
	return owner;
223 224 225
}

/**
226 227 228 229
 * 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
230
 */
231 232 233
int pinmux_request_gpio(struct pinctrl_dev *pctldev,
			struct pinctrl_gpio_range *range,
			unsigned pin, unsigned gpio)
234 235
{
	char gpiostr[16];
236
	const char *owner;
237 238 239 240 241
	int ret;

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

242 243
	owner = kstrdup(gpiostr, GFP_KERNEL);
	if (!owner)
244 245
		return -EINVAL;

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

	return ret;
251 252 253
}

/**
254 255 256 257
 * 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
258
 */
259 260
void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
		      struct pinctrl_gpio_range *range)
261
{
262
	const char *owner;
263

264 265
	owner = pin_free(pctldev, pin, range);
	kfree(owner);
266 267
}

268 269 270 271 272 273 274 275 276 277
/**
 * 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)
278 279 280 281 282 283 284 285 286 287 288 289 290 291
{
	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;
}

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

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

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

		selector++;
	}

	pr_err("%s does not support function %s\n",
311
	       pinctrl_dev_get_name(pctldev), function);
312 313 314
	return -EINVAL;
}

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

329 330 331 332 333
	if (!pmxops) {
		dev_err(pctldev->dev, "does not support mux function\n");
		return -EINVAL;
	}

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

342
	ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
343
					  &groups, &num_groups);
344 345 346
	if (ret < 0) {
		dev_err(pctldev->dev, "can't query groups for function %s\n",
			map->data.mux.function);
347
		return ret;
348 349 350 351 352
	}
	if (!num_groups) {
		dev_err(pctldev->dev,
			"function %s can't be selected on any group\n",
			map->data.mux.function);
353
		return -EINVAL;
354
	}
355
	if (map->data.mux.group) {
356
		bool found = false;
357
		group = map->data.mux.group;
358 359 360 361 362 363
		for (i = 0; i < num_groups; i++) {
			if (!strcmp(group, groups[i])) {
				found = true;
				break;
			}
		}
364 365 366 367
		if (!found) {
			dev_err(pctldev->dev,
				"invalid group \"%s\" for function \"%s\"\n",
				group, map->data.mux.function);
368
			return -EINVAL;
369
		}
370 371
	} else {
		group = groups[0];
372 373
	}

374
	ret = pinctrl_get_group_selector(pctldev, group);
375 376 377
	if (ret < 0) {
		dev_err(pctldev->dev, "invalid group %s in map table\n",
			map->data.mux.group);
378
		return ret;
379
	}
380
	setting->data.mux.group = ret;
381

382 383
	ret = pctlops->get_group_pins(pctldev, setting->data.mux.group, &pins,
				      &num_pins);
384
	if (ret) {
385 386
		dev_err(pctldev->dev,
			"could not get pins for device %s group selector %d\n",
387
			pinctrl_dev_get_name(pctldev), setting->data.mux.group);
388 389 390 391 392 393 394 395
			return -ENODEV;
	}

	/* Try to allocate all pins in this group, one by one */
	for (i = 0; i < num_pins; i++) {
		ret = pin_request(pctldev, pins[i], map->dev_name, NULL);
		if (ret) {
			dev_err(pctldev->dev,
396
				"could not request pin %d on device %s\n",
397 398 399 400 401 402 403
				pins[i], pinctrl_dev_get_name(pctldev));
			/* On error release all taken pins */
			i--; /* this pin just failed */
			for (; i >= 0; i--)
				pin_free(pctldev, pins[i], NULL);
			return -ENODEV;
		}
404 405 406 407 408
	}

	return 0;
}

409
void pinmux_free_setting(struct pinctrl_setting const *setting)
410
{
411 412 413 414
	struct pinctrl_dev *pctldev = setting->pctldev;
	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
	const unsigned *pins;
	unsigned num_pins;
415
	int ret;
416
	int i;
417

418
	ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
419
				      &pins, &num_pins);
420
	if (ret) {
421 422
		dev_err(pctldev->dev,
			"could not get pins for device %s group selector %d\n",
423
			pinctrl_dev_get_name(pctldev), setting->data.mux.group);
424
		return;
425 426
	}

427 428
	for (i = 0; i < num_pins; i++)
		pin_free(pctldev, pins[i], NULL);
429 430
}

431
int pinmux_enable_setting(struct pinctrl_setting const *setting)
432
{
433
	struct pinctrl_dev *pctldev = setting->pctldev;
434
	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
435
	const struct pinmux_ops *ops = pctldev->desc->pmxops;
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
	int ret;
	const unsigned *pins;
	unsigned num_pins;
	int i;
	struct pin_desc *desc;

	ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
				      &pins, &num_pins);
	if (ret) {
		/* errors only affect debug data, so just warn */
		dev_warn(pctldev->dev,
			 "could not get pins for group selector %d\n",
			 setting->data.mux.group);
		num_pins = 0;
	}

	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);
	}
462

463 464
	return ops->enable(pctldev, setting->data.mux.func,
			   setting->data.mux.group);
465 466
}

467
void pinmux_disable_setting(struct pinctrl_setting const *setting)
468
{
469
	struct pinctrl_dev *pctldev = setting->pctldev;
470
	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
471
	const struct pinmux_ops *ops = pctldev->desc->pmxops;
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
	int ret;
	const unsigned *pins;
	unsigned num_pins;
	int i;
	struct pin_desc *desc;

	ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
				      &pins, &num_pins);
	if (ret) {
		/* errors only affect debug data, so just warn */
		dev_warn(pctldev->dev,
			 "could not get pins for group selector %d\n",
			 setting->data.mux.group);
		num_pins = 0;
	}

	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 = NULL;
	}
498

499 500
	if (ops->disable)
		ops->disable(pctldev, setting->data.mux.func, setting->data.mux.group);
501 502 503 504 505 506 507 508 509
}

#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;
510
	unsigned nfuncs;
511 512
	unsigned func_selector = 0;

513 514
	if (!pmxops)
		return 0;
515

516 517
	mutex_lock(&pinctrl_mutex);
	nfuncs = pmxops->get_functions_count(pctldev);
518
	while (func_selector < nfuncs) {
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
		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++;
	}

540 541
	mutex_unlock(&pinctrl_mutex);

542 543 544 545 546 547
	return 0;
}

static int pinmux_pins_show(struct seq_file *s, void *what)
{
	struct pinctrl_dev *pctldev = s->private;
548 549
	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
550
	unsigned i, pin;
551

552 553 554
	if (!pmxops)
		return 0;

555
	seq_puts(s, "Pinmux settings per pin\n");
556
	seq_puts(s, "Format: pin (name): mux_owner gpio_owner hog?\n");
557

558 559
	mutex_lock(&pinctrl_mutex);

560 561
	/* The pin number can be retrived from the pin controller descriptor */
	for (i = 0; i < pctldev->desc->npins; i++) {
562
		struct pin_desc *desc;
563
		bool is_hog = false;
564

565
		pin = pctldev->desc->pins[i].number;
566
		desc = pin_desc_get(pctldev, pin);
567
		/* Skip if we cannot search the pin */
568 569 570
		if (desc == NULL)
			continue;

571 572
		if (desc->mux_owner &&
		    !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
573 574
			is_hog = true;

575
		seq_printf(s, "pin %d (%s): %s %s%s", pin,
576
			   desc->name ? desc->name : "unnamed",
577 578 579 580
			   desc->mux_owner ? desc->mux_owner
				: "(MUX UNCLAIMED)",
			   desc->gpio_owner ? desc->gpio_owner
				: "(GPIO UNCLAIMED)",
581
			   is_hog ? " (HOG)" : "");
582 583 584 585 586 587 588 589 590

		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");
591 592
	}

593 594
	mutex_unlock(&pinctrl_mutex);

595 596 597
	return 0;
}

598 599 600 601 602 603 604 605 606
void pinmux_show_map(struct seq_file *s, struct pinctrl_map const *map)
{
	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,
			 struct pinctrl_setting const *setting)
607
{
608 609 610 611
	struct pinctrl_dev *pctldev = setting->pctldev;
	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;

612 613 614 615 616
	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);
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
}

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