pinmux.c 12.8 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
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;
}

61 62 63
/**
 * pin_request() - request a single pin to be muxed in, typically for GPIO
 * @pin: the pin number in the global pin space
64 65
 * @owner: a representation of the owner of this pin; typically the device
 *	name that controls its mux function, or the requested GPIO name
66 67 68 69
 * @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,
70
		       int pin, const char *owner,
71 72 73 74 75 76
		       struct pinctrl_gpio_range *gpio_range)
{
	struct pin_desc *desc;
	const struct pinmux_ops *ops = pctldev->desc->pmxops;
	int status = -EINVAL;

77
	dev_dbg(pctldev->dev, "request pin %d for %s\n", pin, owner);
78 79 80

	desc = pin_desc_get(pctldev, pin);
	if (desc == NULL) {
81
		dev_err(pctldev->dev,
82 83 84 85
			"pin is not registered so it cannot be requested\n");
		goto out;
	}

86
	if (desc->usecount && strcmp(desc->owner, owner)) {
87
		dev_err(pctldev->dev,
88 89 90
			"pin already requested\n");
		goto out;
	}
91 92 93 94 95

	desc->usecount++;
	if (desc->usecount > 1)
		return 0;

96
	desc->owner = owner;
97 98 99

	/* Let each pin increase references to this module */
	if (!try_module_get(pctldev->owner)) {
100
		dev_err(pctldev->dev,
101 102 103 104 105 106 107 108 109 110
			"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.
	 */
111
	if (gpio_range && ops->gpio_request_enable)
112 113 114 115 116 117 118
		/* 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;

119
	if (status) {
120
		dev_err(pctldev->dev, "->request on device %s failed for pin %d\n",
121
		       pctldev->desc->name, pin);
122 123 124
		module_put(pctldev->owner);
	}

125
out_free_pin:
126 127 128 129 130
	if (status) {
		desc->usecount--;
		if (!desc->usecount)
			desc->owner = NULL;
	}
131 132
out:
	if (status)
133
		dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
134
		       pin, owner, status);
135 136 137 138 139 140 141 142

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

	desc = pin_desc_get(pctldev, pin);
	if (desc == NULL) {
159
		dev_err(pctldev->dev,
160
			"pin is not registered so it cannot be freed\n");
161
		return NULL;
162 163
	}

164 165 166 167
	desc->usecount--;
	if (desc->usecount)
		return NULL;

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

177 178
	owner = desc->owner;
	desc->owner = NULL;
179
	module_put(pctldev->owner);
180

181
	return owner;
182 183 184
}

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

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

201 202
	owner = kstrdup(gpiostr, GFP_KERNEL);
	if (!owner)
203 204
		return -EINVAL;

205
	ret = pin_request(pctldev, pin, owner, range);
206
	if (ret < 0)
207
		kfree(owner);
208 209

	return ret;
210 211 212
}

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

223 224
	owner = pin_free(pctldev, pin, range);
	kfree(owner);
225 226
}

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

251 252
static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
					const char *function)
253 254 255 256 257 258 259 260 261
{
	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);

262 263
		if (!strcmp(function, fname))
			return selector;
264 265 266 267 268

		selector++;
	}

	pr_err("%s does not support function %s\n",
269
	       pinctrl_dev_get_name(pctldev), function);
270 271 272
	return -EINVAL;
}

273 274
int pinmux_map_to_setting(struct pinctrl_map const *map,
			  struct pinctrl_setting *setting)
275
{
276 277 278 279 280
	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;
281
	int ret;
282 283 284 285
	const char *group;
	int i;
	const unsigned *pins;
	unsigned num_pins;
286

287 288 289 290
	setting->func_selector =
		pinmux_func_name_to_selector(pctldev, map->function);
	if (setting->func_selector < 0)
		return setting->func_selector;
291

292 293 294 295 296
	ret = pmxops->get_function_groups(pctldev, setting->func_selector,
					  &groups, &num_groups);
	if (ret < 0)
		return ret;
	if (!num_groups)
297
		return -EINVAL;
298 299 300 301 302 303 304 305 306 307 308 309 310 311

	if (map->group) {
		bool found = false;
		group = map->group;
		for (i = 0; i < num_groups; i++) {
			if (!strcmp(group, groups[i])) {
				found = true;
				break;
			}
		}
		if (!found)
			return -EINVAL;
	} else {
		group = groups[0];
312 313
	}

314 315 316 317
	setting->group_selector =
		pinctrl_get_group_selector(pctldev, group);
	if (setting->group_selector < 0)
		return setting->group_selector;
318

319 320
	ret = pctlops->get_group_pins(pctldev, setting->group_selector,
				      &pins, &num_pins);
321
	if (ret) {
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
		dev_err(pctldev->dev,
			"could not get pins for device %s group selector %d\n",
			pinctrl_dev_get_name(pctldev), setting->group_selector);
			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,
				"could not get request pin %d on device %s\n",
				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;
		}
341 342 343 344 345
	}

	return 0;
}

346
void pinmux_free_setting(struct pinctrl_setting const *setting)
347
{
348 349 350 351
	struct pinctrl_dev *pctldev = setting->pctldev;
	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
	const unsigned *pins;
	unsigned num_pins;
352
	int ret;
353
	int i;
354

355 356
	ret = pctlops->get_group_pins(pctldev, setting->group_selector,
				      &pins, &num_pins);
357
	if (ret) {
358 359 360 361
		dev_err(pctldev->dev,
			"could not get pins for device %s group selector %d\n",
			pinctrl_dev_get_name(pctldev), setting->group_selector);
		return;
362 363
	}

364 365
	for (i = 0; i < num_pins; i++)
		pin_free(pctldev, pins[i], NULL);
366 367
}

368
int pinmux_enable_setting(struct pinctrl_setting const *setting)
369
{
370
	struct pinctrl_dev *pctldev = setting->pctldev;
371
	const struct pinmux_ops *ops = pctldev->desc->pmxops;
372

373 374
	return ops->enable(pctldev, setting->func_selector,
			   setting->group_selector);
375 376
}

377
void pinmux_disable_setting(struct pinctrl_setting const *setting)
378
{
379
	struct pinctrl_dev *pctldev = setting->pctldev;
380
	const struct pinmux_ops *ops = pctldev->desc->pmxops;
381

382
	ops->disable(pctldev, setting->func_selector, setting->group_selector);
383 384 385 386 387 388 389 390 391 392 393
}

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

394 395
	mutex_lock(&pinctrl_mutex);

396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
	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++;
	}

418 419
	mutex_unlock(&pinctrl_mutex);

420 421 422 423 424 425
	return 0;
}

static int pinmux_pins_show(struct seq_file *s, void *what)
{
	struct pinctrl_dev *pctldev = s->private;
426
	unsigned i, pin;
427 428

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

431 432
	mutex_lock(&pinctrl_mutex);

433 434
	/* The pin number can be retrived from the pin controller descriptor */
	for (i = 0; i < pctldev->desc->npins; i++) {
435
		struct pin_desc *desc;
436
		bool is_hog = false;
437

438
		pin = pctldev->desc->pins[i].number;
439
		desc = pin_desc_get(pctldev, pin);
440
		/* Skip if we cannot search the pin */
441 442 443
		if (desc == NULL)
			continue;

444 445 446 447 448
		if (desc->owner &&
		    !strcmp(desc->owner, pinctrl_dev_get_name(pctldev)))
			is_hog = true;

		seq_printf(s, "pin %d (%s): %s%s\n", pin,
449
			   desc->name ? desc->name : "unnamed",
450 451
			   desc->owner ? desc->owner : "UNCLAIMED",
			   is_hog ? " (HOG)" : "");
452 453
	}

454 455
	mutex_unlock(&pinctrl_mutex);

456 457 458
	return 0;
}

459
void pinmux_dbg_show(struct seq_file *s, struct pinctrl_setting const *setting)
460
{
461 462 463 464 465 466 467 468 469 470
	struct pinctrl_dev *pctldev = setting->pctldev;
	const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;

	seq_printf(s, "controller: %s group: %s (%u) function: %s (%u)\n",
		   pinctrl_dev_get_name(pctldev),
		   pctlops->get_group_name(pctldev, setting->group_selector),
		   setting->group_selector,
		   pmxops->get_function_name(pctldev, setting->func_selector),
		   setting->func_selector);
471 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 498 499 500 501 502 503 504 505 506
}

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