pinconf.c 16.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Core driver for the pin config portions of the pin control subsystem
 *
 * Copyright (C) 2011 ST-Ericsson SA
 * Written on behalf of Linaro for ST-Ericsson
 *
 * Author: Linus Walleij <linus.walleij@linaro.org>
 *
 * License terms: GNU General Public License (GPL) version 2
 */
#define pr_fmt(fmt) "pinconfig core: " fmt

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
20
#include <linux/uaccess.h>
21 22 23 24 25 26
#include <linux/pinctrl/machine.h>
#include <linux/pinctrl/pinctrl.h>
#include <linux/pinctrl/pinconf.h>
#include "core.h"
#include "pinconf.h"

27 28 29 30 31
int pinconf_check_ops(struct pinctrl_dev *pctldev)
{
	const struct pinconf_ops *ops = pctldev->desc->confops;

	/* We have to be able to config the pins in SOME way */
32 33 34
	if (!ops->pin_config_set && !ops->pin_config_group_set) {
		dev_err(pctldev->dev,
			"pinconf has to be able to set a pins config\n");
35
		return -EINVAL;
36
	}
37 38 39
	return 0;
}

40 41 42 43 44 45 46 47
int pinconf_validate_map(struct pinctrl_map const *map, int i)
{
	if (!map->data.configs.group_or_pin) {
		pr_err("failed to register map %s (%d): no group/pin given\n",
		       map->name, i);
		return -EINVAL;
	}

48
	if (!map->data.configs.num_configs ||
49
			!map->data.configs.configs) {
50
		pr_err("failed to register map %s (%d): no configs given\n",
51 52 53 54 55 56 57
		       map->name, i);
		return -EINVAL;
	}

	return 0;
}

58
int pin_config_get_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
59 60 61 62 63
			   unsigned long *config)
{
	const struct pinconf_ops *ops = pctldev->desc->confops;

	if (!ops || !ops->pin_config_get) {
64
		dev_dbg(pctldev->dev, "cannot get pin configuration, missing "
65
			"pin_config_get() function in driver\n");
66
		return -ENOTSUPP;
67 68 69 70 71
	}

	return ops->pin_config_get(pctldev, pin, config);
}

72
int pin_config_group_get(const char *dev_name, const char *pin_group,
73 74
			 unsigned long *config)
{
75 76
	struct pinctrl_dev *pctldev;
	const struct pinconf_ops *ops;
77 78
	int selector, ret;

79
	pctldev = get_pinctrl_dev_from_devname(dev_name);
80 81
	if (!pctldev) {
		ret = -EINVAL;
82
		return ret;
83
	}
84 85 86

	mutex_lock(&pctldev->mutex);

87 88
	ops = pctldev->desc->confops;

89
	if (!ops || !ops->pin_config_group_get) {
90
		dev_dbg(pctldev->dev, "cannot get configuration for pin "
91 92
			"group, missing group config get function in "
			"driver\n");
93
		ret = -ENOTSUPP;
94
		goto unlock;
95 96 97
	}

	selector = pinctrl_get_group_selector(pctldev, pin_group);
98 99 100 101
	if (selector < 0) {
		ret = selector;
		goto unlock;
	}
102

103 104 105
	ret = ops->pin_config_group_get(pctldev, selector, config);

unlock:
106
	mutex_unlock(&pctldev->mutex);
107
	return ret;
108 109
}

110 111 112 113
int pinconf_map_to_setting(struct pinctrl_map const *map,
			  struct pinctrl_setting *setting)
{
	struct pinctrl_dev *pctldev = setting->pctldev;
114
	int pin;
115 116 117

	switch (setting->type) {
	case PIN_MAP_TYPE_CONFIGS_PIN:
118 119 120 121 122 123 124 125
		pin = pin_get_from_name(pctldev,
					map->data.configs.group_or_pin);
		if (pin < 0) {
			dev_err(pctldev->dev, "could not map pin config for \"%s\"",
				map->data.configs.group_or_pin);
			return pin;
		}
		setting->data.configs.group_or_pin = pin;
126 127
		break;
	case PIN_MAP_TYPE_CONFIGS_GROUP:
128 129 130 131 132 133 134 135
		pin = pinctrl_get_group_selector(pctldev,
					 map->data.configs.group_or_pin);
		if (pin < 0) {
			dev_err(pctldev->dev, "could not map group config for \"%s\"",
				map->data.configs.group_or_pin);
			return pin;
		}
		setting->data.configs.group_or_pin = pin;
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
		break;
	default:
		return -EINVAL;
	}

	setting->data.configs.num_configs = map->data.configs.num_configs;
	setting->data.configs.configs = map->data.configs.configs;

	return 0;
}

void pinconf_free_setting(struct pinctrl_setting const *setting)
{
}

int pinconf_apply_setting(struct pinctrl_setting const *setting)
{
	struct pinctrl_dev *pctldev = setting->pctldev;
	const struct pinconf_ops *ops = pctldev->desc->confops;
155
	int ret;
156 157 158 159 160 161 162 163 164 165 166 167

	if (!ops) {
		dev_err(pctldev->dev, "missing confops\n");
		return -EINVAL;
	}

	switch (setting->type) {
	case PIN_MAP_TYPE_CONFIGS_PIN:
		if (!ops->pin_config_set) {
			dev_err(pctldev->dev, "missing pin_config_set op\n");
			return -EINVAL;
		}
168 169 170 171 172 173 174 175 176
		ret = ops->pin_config_set(pctldev,
				setting->data.configs.group_or_pin,
				setting->data.configs.configs,
				setting->data.configs.num_configs);
		if (ret < 0) {
			dev_err(pctldev->dev,
				"pin_config_set op failed for pin %d\n",
				setting->data.configs.group_or_pin);
			return ret;
177 178 179 180 181 182 183 184
		}
		break;
	case PIN_MAP_TYPE_CONFIGS_GROUP:
		if (!ops->pin_config_group_set) {
			dev_err(pctldev->dev,
				"missing pin_config_group_set op\n");
			return -EINVAL;
		}
185 186 187 188 189 190 191 192 193
		ret = ops->pin_config_group_set(pctldev,
				setting->data.configs.group_or_pin,
				setting->data.configs.configs,
				setting->data.configs.num_configs);
		if (ret < 0) {
			dev_err(pctldev->dev,
				"pin_config_group_set op failed for group %d\n",
				setting->data.configs.group_or_pin);
			return ret;
194 195 196 197 198 199 200 201 202
		}
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

203 204
#ifdef CONFIG_DEBUG_FS

205 206
void pinconf_show_map(struct seq_file *s, struct pinctrl_map const *map)
{
207 208
	struct pinctrl_dev *pctldev;
	const struct pinconf_ops *confops;
209 210
	int i;

211 212 213 214 215 216
	pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
	if (pctldev)
		confops = pctldev->desc->confops;
	else
		confops = NULL;

217 218 219 220 221 222 223 224 225 226 227 228 229
	switch (map->type) {
	case PIN_MAP_TYPE_CONFIGS_PIN:
		seq_printf(s, "pin ");
		break;
	case PIN_MAP_TYPE_CONFIGS_GROUP:
		seq_printf(s, "group ");
		break;
	default:
		break;
	}

	seq_printf(s, "%s\n", map->data.configs.group_or_pin);

230 231 232 233 234 235 236 237 238
	for (i = 0; i < map->data.configs.num_configs; i++) {
		seq_printf(s, "config ");
		if (confops && confops->pin_config_config_dbg_show)
			confops->pin_config_config_dbg_show(pctldev, s,
						map->data.configs.configs[i]);
		else
			seq_printf(s, "%08lx", map->data.configs.configs[i]);
		seq_printf(s, "\n");
	}
239 240 241 242 243 244 245
}

void pinconf_show_setting(struct seq_file *s,
			  struct pinctrl_setting const *setting)
{
	struct pinctrl_dev *pctldev = setting->pctldev;
	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
246
	const struct pinconf_ops *confops = pctldev->desc->confops;
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
	struct pin_desc *desc;
	int i;

	switch (setting->type) {
	case PIN_MAP_TYPE_CONFIGS_PIN:
		desc = pin_desc_get(setting->pctldev,
				    setting->data.configs.group_or_pin);
		seq_printf(s, "pin %s (%d)",
			   desc->name ? desc->name : "unnamed",
			   setting->data.configs.group_or_pin);
		break;
	case PIN_MAP_TYPE_CONFIGS_GROUP:
		seq_printf(s, "group %s (%d)",
			   pctlops->get_group_name(pctldev,
					setting->data.configs.group_or_pin),
			   setting->data.configs.group_or_pin);
		break;
	default:
		break;
	}

	/*
	 * FIXME: We should really get the pin controler to dump the config
	 * values, so they can be decoded to something meaningful.
	 */
272 273 274 275 276 277 278 279 280
	for (i = 0; i < setting->data.configs.num_configs; i++) {
		seq_printf(s, " ");
		if (confops && confops->pin_config_config_dbg_show)
			confops->pin_config_config_dbg_show(pctldev, s,
				setting->data.configs.configs[i]);
		else
			seq_printf(s, "%08lx",
				   setting->data.configs.configs[i]);
	}
281 282 283 284

	seq_printf(s, "\n");
}

285 286 287 288 289
static void pinconf_dump_pin(struct pinctrl_dev *pctldev,
			     struct seq_file *s, int pin)
{
	const struct pinconf_ops *ops = pctldev->desc->confops;

290
	/* no-op when not using generic pin config */
291
	pinconf_generic_dump_pins(pctldev, s, NULL, pin);
292 293 294 295 296 297 298
	if (ops && ops->pin_config_dbg_show)
		ops->pin_config_dbg_show(pctldev, s, pin);
}

static int pinconf_pins_show(struct seq_file *s, void *what)
{
	struct pinctrl_dev *pctldev = s->private;
299
	unsigned i, pin;
300 301

	seq_puts(s, "Pin config settings per pin\n");
302
	seq_puts(s, "Format: pin (name): configs\n");
303

304
	mutex_lock(&pctldev->mutex);
305

306
	/* The pin number can be retrived from the pin controller descriptor */
307
	for (i = 0; i < pctldev->desc->npins; i++) {
308 309
		struct pin_desc *desc;

310
		pin = pctldev->desc->pins[i].number;
311
		desc = pin_desc_get(pctldev, pin);
312
		/* Skip if we cannot search the pin */
313 314 315 316 317 318 319 320 321 322 323
		if (desc == NULL)
			continue;

		seq_printf(s, "pin %d (%s):", pin,
			   desc->name ? desc->name : "unnamed");

		pinconf_dump_pin(pctldev, s, pin);

		seq_printf(s, "\n");
	}

324
	mutex_unlock(&pctldev->mutex);
325

326 327 328 329 330 331 332 333 334
	return 0;
}

static void pinconf_dump_group(struct pinctrl_dev *pctldev,
			       struct seq_file *s, unsigned selector,
			       const char *gname)
{
	const struct pinconf_ops *ops = pctldev->desc->confops;

335
	/* no-op when not using generic pin config */
336
	pinconf_generic_dump_pins(pctldev, s, gname, 0);
337 338 339 340 341 342 343 344
	if (ops && ops->pin_config_group_dbg_show)
		ops->pin_config_group_dbg_show(pctldev, s, selector);
}

static int pinconf_groups_show(struct seq_file *s, void *what)
{
	struct pinctrl_dev *pctldev = s->private;
	const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
345
	unsigned ngroups = pctlops->get_groups_count(pctldev);
346 347 348
	unsigned selector = 0;

	seq_puts(s, "Pin config settings per pin group\n");
349
	seq_puts(s, "Format: group (name): configs\n");
350

351
	while (selector < ngroups) {
352 353 354 355
		const char *gname = pctlops->get_group_name(pctldev, selector);

		seq_printf(s, "%u (%s):", selector, gname);
		pinconf_dump_group(pctldev, s, selector, gname);
356 357
		seq_printf(s, "\n");

358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
		selector++;
	}

	return 0;
}

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

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

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

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

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
#define MAX_NAME_LEN 15

struct dbg_cfg {
	enum pinctrl_map_type map_type;
	char dev_name[MAX_NAME_LEN+1];
	char state_name[MAX_NAME_LEN+1];
	char pin_name[MAX_NAME_LEN+1];
};

/*
 * Goal is to keep this structure as global in order to simply read the
 * pinconf-config file after a write to check config is as expected
 */
static struct dbg_cfg pinconf_dbg_conf;

/**
 * pinconf_dbg_config_print() - display the pinctrl config from the pinctrl
 * map, of the dev/pin/state that was last written to pinconf-config file.
 * @s: string filled in  with config description
 * @d: not used
 */
static int pinconf_dbg_config_print(struct seq_file *s, void *d)
{
	struct pinctrl_maps *maps_node;
	const struct pinctrl_map *map;
413 414
	const struct pinctrl_map *found = NULL;
	struct pinctrl_dev *pctldev;
415 416 417 418 419
	const struct pinconf_ops *confops = NULL;
	struct dbg_cfg *dbg = &pinconf_dbg_conf;
	int i, j;
	unsigned long config;

420
	mutex_lock(&pinctrl_maps_mutex);
421 422 423 424 425 426 427 428 429 430 431 432 433

	/* Parse the pinctrl map and look for the elected pin/state */
	for_each_maps(maps_node, i, map) {
		if (map->type != dbg->map_type)
			continue;
		if (strcmp(map->dev_name, dbg->dev_name))
			continue;
		if (strcmp(map->name, dbg->state_name))
			continue;

		for (j = 0; j < map->data.configs.num_configs; j++) {
			if (!strcmp(map->data.configs.group_or_pin,
					dbg->pin_name)) {
434 435
				/* We found the right pin / state */
				found = map;
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
				break;
			}
		}
	}

	if (!found) {
		seq_printf(s, "No config found for dev/state/pin, expected:\n");
		seq_printf(s, "Searched dev:%s\n", dbg->dev_name);
		seq_printf(s, "Searched state:%s\n", dbg->state_name);
		seq_printf(s, "Searched pin:%s\n", dbg->pin_name);
		seq_printf(s, "Use: modify config_pin <devname> "\
				"<state> <pinname> <value>\n");
		goto exit;
	}

451 452
	pctldev = get_pinctrl_dev_from_devname(found->ctrl_dev_name);
	config = *found->data.configs.configs;
453 454 455 456 457 458 459 460 461 462 463
	seq_printf(s, "Dev %s has config of %s in state %s: 0x%08lX\n",
			dbg->dev_name, dbg->pin_name,
			dbg->state_name, config);

	if (pctldev)
		confops = pctldev->desc->confops;

	if (confops && confops->pin_config_config_dbg_show)
		confops->pin_config_config_dbg_show(pctldev, s, config);

exit:
464
	mutex_unlock(&pinctrl_maps_mutex);
465 466 467 468 469 470 471 472

	return 0;
}

/**
 * pinconf_dbg_config_write() - modify the pinctrl config in the pinctrl
 * map, of a dev/pin/state entry based on user entries to pinconf-config
 * @user_buf: contains the modification request with expected format:
473
 *     modify <config> <devicename> <state> <name> <newvalue>
474
 * modify is literal string, alternatives like add/delete not supported yet
475 476 477 478
 * <config> is the configuration to be changed. Supported configs are
 *     "config_pin" or "config_group", alternatives like config_mux are not
 *     supported yet.
 * <devicename> <state> <name> are values that should match the pinctrl-maps
479 480
 * <newvalue> reflects the new config and is driver dependant
 */
481
static ssize_t pinconf_dbg_config_write(struct file *file,
482 483 484 485
	const char __user *user_buf, size_t count, loff_t *ppos)
{
	struct pinctrl_maps *maps_node;
	const struct pinctrl_map *map;
486 487
	const struct pinctrl_map *found = NULL;
	struct pinctrl_dev *pctldev;
488 489 490 491 492 493 494 495 496 497 498
	const struct pinconf_ops *confops = NULL;
	struct dbg_cfg *dbg = &pinconf_dbg_conf;
	const struct pinctrl_map_configs *configs;
	char config[MAX_NAME_LEN+1];
	char buf[128];
	char *b = &buf[0];
	int buf_size;
	char *token;
	int i;

	/* Get userspace string and assure termination */
499
	buf_size = min(count, sizeof(buf) - 1);
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
	if (copy_from_user(buf, user_buf, buf_size))
		return -EFAULT;
	buf[buf_size] = 0;

	/*
	 * need to parse entry and extract parameters:
	 * modify configs_pin devicename state pinname newvalue
	 */

	/* Get arg: 'modify' */
	token = strsep(&b, " ");
	if (!token)
		return -EINVAL;
	if (strcmp(token, "modify"))
		return -EINVAL;

516 517 518 519
	/*
	 * Get arg type: "config_pin" and "config_group"
	 *                types are supported so far
	 */
520 521 522
	token = strsep(&b, " ");
	if (!token)
		return -EINVAL;
523 524 525 526 527
	if (!strcmp(token, "config_pin"))
		dbg->map_type = PIN_MAP_TYPE_CONFIGS_PIN;
	else if (!strcmp(token, "config_group"))
		dbg->map_type = PIN_MAP_TYPE_CONFIGS_GROUP;
	else
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
		return -EINVAL;

	/* get arg 'device_name' */
	token = strsep(&b, " ");
	if (token == NULL)
		return -EINVAL;
	if (strlen(token) >= MAX_NAME_LEN)
		return -EINVAL;
	strncpy(dbg->dev_name, token, MAX_NAME_LEN);

	/* get arg 'state_name' */
	token = strsep(&b, " ");
	if (token == NULL)
		return -EINVAL;
	if (strlen(token) >= MAX_NAME_LEN)
		return -EINVAL;
	strncpy(dbg->state_name, token, MAX_NAME_LEN);

	/* get arg 'pin_name' */
	token = strsep(&b, " ");
	if (token == NULL)
		return -EINVAL;
	if (strlen(token) >= MAX_NAME_LEN)
		return -EINVAL;
	strncpy(dbg->pin_name, token, MAX_NAME_LEN);

	/* get new_value of config' */
	token = strsep(&b, " ");
	if (token == NULL)
		return -EINVAL;
	if (strlen(token) >= MAX_NAME_LEN)
		return -EINVAL;
	strncpy(config, token, MAX_NAME_LEN);

562
	mutex_lock(&pinctrl_maps_mutex);
563 564 565 566 567 568 569 570 571 572 573 574

	/* Parse the pinctrl map and look for the selected dev/state/pin */
	for_each_maps(maps_node, i, map) {
		if (strcmp(map->dev_name, dbg->dev_name))
			continue;
		if (map->type != dbg->map_type)
			continue;
		if (strcmp(map->name, dbg->state_name))
			continue;

		/*  we found the right pin / state, so overwrite config */
		if (!strcmp(map->data.configs.group_or_pin, dbg->pin_name)) {
575
			found = map;
576 577 578 579 580 581
			break;
		}
	}

	if (!found) {
		count = -EINVAL;
582
		goto exit;
583 584
	}

585
	pctldev = get_pinctrl_dev_from_devname(found->ctrl_dev_name);
586 587 588 589
	if (pctldev)
		confops = pctldev->desc->confops;

	if (confops && confops->pin_config_dbg_parse_modify) {
590
		configs = &found->data.configs;
591 592 593 594 595 596 597 598
		for (i = 0; i < configs->num_configs; i++) {
			confops->pin_config_dbg_parse_modify(pctldev,
						     config,
						     &configs->configs[i]);
		}
	}

exit:
599
	mutex_unlock(&pinctrl_maps_mutex);
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617

	return count;
}

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

static const struct file_operations pinconf_dbg_pinconfig_fops = {
	.open = pinconf_dbg_config_open,
	.write = pinconf_dbg_config_write,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

618 619 620 621 622 623 624
void pinconf_init_device_debugfs(struct dentry *devroot,
			 struct pinctrl_dev *pctldev)
{
	debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO,
			    devroot, pctldev, &pinconf_pins_ops);
	debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO,
			    devroot, pctldev, &pinconf_groups_ops);
625 626
	debugfs_create_file("pinconf-config",  (S_IRUGO | S_IWUSR | S_IWGRP),
			    devroot, pctldev, &pinconf_dbg_pinconfig_fops);
627 628 629
}

#endif