pinctrl.c 19.7 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * SuperH Pin Function Controller pinmux support.
 *
 * Copyright (C) 2012  Paul Mundt
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 */
10

11
#define DRV_NAME "sh-pfc"
12

L
Laurent Pinchart 已提交
13
#include <linux/device.h>
14
#include <linux/err.h>
15 16
#include <linux/init.h>
#include <linux/module.h>
L
Laurent Pinchart 已提交
17
#include <linux/of.h>
18
#include <linux/pinctrl/consumer.h>
L
Laurent Pinchart 已提交
19
#include <linux/pinctrl/machine.h>
20 21
#include <linux/pinctrl/pinconf.h>
#include <linux/pinctrl/pinconf-generic.h>
22 23 24 25
#include <linux/pinctrl/pinctrl.h>
#include <linux/pinctrl/pinmux.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
26

27
#include "core.h"
28 29
#include "../core.h"
#include "../pinconf.h"
30

31 32 33 34
struct sh_pfc_pin_config {
	u32 type;
};

35 36
struct sh_pfc_pinctrl {
	struct pinctrl_dev *pctl;
37 38
	struct pinctrl_desc pctl_desc;

39 40
	struct sh_pfc *pfc;

41
	struct pinctrl_pin_desc *pins;
42
	struct sh_pfc_pin_config *configs;
43 44 45 46

	const char *func_prop_name;
	const char *groups_prop_name;
	const char *pins_prop_name;
47 48
};

49
static int sh_pfc_get_groups_count(struct pinctrl_dev *pctldev)
50
{
51 52
	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);

53
	return pmx->pfc->info->nr_groups;
54 55
}

56
static const char *sh_pfc_get_group_name(struct pinctrl_dev *pctldev,
57 58
					 unsigned selector)
{
59 60
	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);

61
	return pmx->pfc->info->groups[selector].name;
62 63
}

64
static int sh_pfc_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
65 66
				 const unsigned **pins, unsigned *num_pins)
{
67 68
	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);

69 70
	*pins = pmx->pfc->info->groups[selector].pins;
	*num_pins = pmx->pfc->info->groups[selector].nr_pins;
71 72

	return 0;
73 74
}

75 76 77
static void sh_pfc_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
				unsigned offset)
{
78
	seq_puts(s, DRV_NAME);
79 80
}

81
#ifdef CONFIG_OF
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
static int sh_pfc_map_add_config(struct pinctrl_map *map,
				 const char *group_or_pin,
				 enum pinctrl_map_type type,
				 unsigned long *configs,
				 unsigned int num_configs)
{
	unsigned long *cfgs;

	cfgs = kmemdup(configs, num_configs * sizeof(*cfgs),
		       GFP_KERNEL);
	if (cfgs == NULL)
		return -ENOMEM;

	map->type = type;
	map->data.configs.group_or_pin = group_or_pin;
	map->data.configs.configs = cfgs;
	map->data.configs.num_configs = num_configs;

	return 0;
}

103 104
static int sh_pfc_dt_subnode_to_map(struct pinctrl_dev *pctldev,
				    struct device_node *np,
L
Laurent Pinchart 已提交
105 106 107
				    struct pinctrl_map **map,
				    unsigned int *num_maps, unsigned int *index)
{
108 109
	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
	struct device *dev = pmx->pfc->dev;
L
Laurent Pinchart 已提交
110 111 112
	struct pinctrl_map *maps = *map;
	unsigned int nmaps = *num_maps;
	unsigned int idx = *index;
113
	unsigned int num_configs;
L
Laurent Pinchart 已提交
114
	const char *function = NULL;
115
	unsigned long *configs;
L
Laurent Pinchart 已提交
116
	struct property *prop;
117 118
	unsigned int num_groups;
	unsigned int num_pins;
L
Laurent Pinchart 已提交
119
	const char *group;
120
	const char *pin;
L
Laurent Pinchart 已提交
121 122
	int ret;

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
	/* Support both the old Renesas-specific properties and the new standard
	 * properties. Mixing old and new properties isn't allowed, neither
	 * inside a subnode nor across subnodes.
	 */
	if (!pmx->func_prop_name) {
		if (of_find_property(np, "groups", NULL) ||
		    of_find_property(np, "pins", NULL)) {
			pmx->func_prop_name = "function";
			pmx->groups_prop_name = "groups";
			pmx->pins_prop_name = "pins";
		} else {
			pmx->func_prop_name = "renesas,function";
			pmx->groups_prop_name = "renesas,groups";
			pmx->pins_prop_name = "renesas,pins";
		}
	}

L
Laurent Pinchart 已提交
140 141 142
	/* Parse the function and configuration properties. At least a function
	 * or one configuration must be specified.
	 */
143
	ret = of_property_read_string(np, pmx->func_prop_name, &function);
L
Laurent Pinchart 已提交
144 145 146 147 148
	if (ret < 0 && ret != -EINVAL) {
		dev_err(dev, "Invalid function in DT\n");
		return ret;
	}

149
	ret = pinconf_generic_parse_dt_config(np, NULL, &configs, &num_configs);
150 151 152 153 154 155
	if (ret < 0)
		return ret;

	if (!function && num_configs == 0) {
		dev_err(dev,
			"DT node must contain at least a function or config\n");
156
		ret = -ENODEV;
L
Laurent Pinchart 已提交
157 158 159
		goto done;
	}

160
	/* Count the number of pins and groups and reallocate mappings. */
161
	ret = of_property_count_strings(np, pmx->pins_prop_name);
162 163 164 165 166 167 168 169 170
	if (ret == -EINVAL) {
		num_pins = 0;
	} else if (ret < 0) {
		dev_err(dev, "Invalid pins list in DT\n");
		goto done;
	} else {
		num_pins = ret;
	}

171
	ret = of_property_count_strings(np, pmx->groups_prop_name);
172 173 174
	if (ret == -EINVAL) {
		num_groups = 0;
	} else if (ret < 0) {
L
Laurent Pinchart 已提交
175 176
		dev_err(dev, "Invalid pin groups list in DT\n");
		goto done;
177 178
	} else {
		num_groups = ret;
L
Laurent Pinchart 已提交
179 180
	}

181 182
	if (!num_pins && !num_groups) {
		dev_err(dev, "No pin or group provided in DT node\n");
L
Laurent Pinchart 已提交
183 184 185 186
		ret = -ENODEV;
		goto done;
	}

187 188 189 190
	if (function)
		nmaps += num_groups;
	if (configs)
		nmaps += num_pins + num_groups;
L
Laurent Pinchart 已提交
191 192 193 194 195 196 197 198 199 200 201

	maps = krealloc(maps, sizeof(*maps) * nmaps, GFP_KERNEL);
	if (maps == NULL) {
		ret = -ENOMEM;
		goto done;
	}

	*map = maps;
	*num_maps = nmaps;

	/* Iterate over pins and groups and create the mappings. */
202
	of_property_for_each_string(np, pmx->groups_prop_name, prop, group) {
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
		if (function) {
			maps[idx].type = PIN_MAP_TYPE_MUX_GROUP;
			maps[idx].data.mux.group = group;
			maps[idx].data.mux.function = function;
			idx++;
		}

		if (configs) {
			ret = sh_pfc_map_add_config(&maps[idx], group,
						    PIN_MAP_TYPE_CONFIGS_GROUP,
						    configs, num_configs);
			if (ret < 0)
				goto done;

			idx++;
		}
L
Laurent Pinchart 已提交
219 220
	}

221 222 223 224 225
	if (!configs) {
		ret = 0;
		goto done;
	}

226
	of_property_for_each_string(np, pmx->pins_prop_name, prop, pin) {
227 228 229 230 231 232 233 234
		ret = sh_pfc_map_add_config(&maps[idx], pin,
					    PIN_MAP_TYPE_CONFIGS_PIN,
					    configs, num_configs);
		if (ret < 0)
			goto done;

		idx++;
	}
L
Laurent Pinchart 已提交
235 236 237

done:
	*index = idx;
238
	kfree(configs);
L
Laurent Pinchart 已提交
239 240 241 242 243 244
	return ret;
}

static void sh_pfc_dt_free_map(struct pinctrl_dev *pctldev,
			       struct pinctrl_map *map, unsigned num_maps)
{
245 246 247 248 249 250 251 252 253 254 255
	unsigned int i;

	if (map == NULL)
		return;

	for (i = 0; i < num_maps; ++i) {
		if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP ||
		    map[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
			kfree(map[i].data.configs.configs);
	}

L
Laurent Pinchart 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
	kfree(map);
}

static int sh_pfc_dt_node_to_map(struct pinctrl_dev *pctldev,
				 struct device_node *np,
				 struct pinctrl_map **map, unsigned *num_maps)
{
	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
	struct device *dev = pmx->pfc->dev;
	struct device_node *child;
	unsigned int index;
	int ret;

	*map = NULL;
	*num_maps = 0;
	index = 0;

	for_each_child_of_node(np, child) {
274
		ret = sh_pfc_dt_subnode_to_map(pctldev, child, map, num_maps,
L
Laurent Pinchart 已提交
275
					       &index);
276 277
		if (ret < 0) {
			of_node_put(child);
L
Laurent Pinchart 已提交
278
			goto done;
279
		}
L
Laurent Pinchart 已提交
280 281 282 283
	}

	/* If no mapping has been found in child nodes try the config node. */
	if (*num_maps == 0) {
284 285
		ret = sh_pfc_dt_subnode_to_map(pctldev, np, map, num_maps,
					       &index);
L
Laurent Pinchart 已提交
286 287 288 289 290 291 292
		if (ret < 0)
			goto done;
	}

	if (*num_maps)
		return 0;

293
	dev_err(dev, "no mapping found in node %pOF\n", np);
L
Laurent Pinchart 已提交
294 295 296 297 298 299 300 301
	ret = -EINVAL;

done:
	if (ret < 0)
		sh_pfc_dt_free_map(pctldev, *map, *num_maps);

	return ret;
}
302
#endif /* CONFIG_OF */
L
Laurent Pinchart 已提交
303

304
static const struct pinctrl_ops sh_pfc_pinctrl_ops = {
305 306
	.get_groups_count	= sh_pfc_get_groups_count,
	.get_group_name		= sh_pfc_get_group_name,
307
	.get_group_pins		= sh_pfc_get_group_pins,
308
	.pin_dbg_show		= sh_pfc_pin_dbg_show,
309
#ifdef CONFIG_OF
L
Laurent Pinchart 已提交
310 311
	.dt_node_to_map		= sh_pfc_dt_node_to_map,
	.dt_free_map		= sh_pfc_dt_free_map,
312
#endif
313 314
};

315 316 317 318
static int sh_pfc_get_functions_count(struct pinctrl_dev *pctldev)
{
	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);

319
	return pmx->pfc->info->nr_functions;
320 321 322 323 324 325 326
}

static const char *sh_pfc_get_function_name(struct pinctrl_dev *pctldev,
					    unsigned selector)
{
	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);

327
	return pmx->pfc->info->functions[selector].name;
328
}
329

330 331
static int sh_pfc_get_function_groups(struct pinctrl_dev *pctldev,
				      unsigned selector,
332 333 334
				      const char * const **groups,
				      unsigned * const num_groups)
{
335 336
	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);

337 338
	*groups = pmx->pfc->info->functions[selector].groups;
	*num_groups = pmx->pfc->info->functions[selector].nr_groups;
339

340 341 342
	return 0;
}

343 344
static int sh_pfc_func_set_mux(struct pinctrl_dev *pctldev, unsigned selector,
			       unsigned group)
345
{
346 347 348 349 350
	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
	struct sh_pfc *pfc = pmx->pfc;
	const struct sh_pfc_pin_group *grp = &pfc->info->groups[group];
	unsigned long flags;
	unsigned int i;
351
	int ret = 0;
352 353 354

	spin_lock_irqsave(&pfc->lock, flags);

355 356 357 358 359 360 361 362 363 364
	for (i = 0; i < grp->nr_pins; ++i) {
		int idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
		struct sh_pfc_pin_config *cfg = &pmx->configs[idx];

		if (cfg->type != PINMUX_TYPE_NONE) {
			ret = -EBUSY;
			goto done;
		}
	}

365
	for (i = 0; i < grp->nr_pins; ++i) {
366 367 368
		ret = sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION);
		if (ret < 0)
			break;
369 370
	}

371
done:
372 373
	spin_unlock_irqrestore(&pfc->lock, flags);
	return ret;
374 375 376 377 378 379 380 381
}

static int sh_pfc_gpio_request_enable(struct pinctrl_dev *pctldev,
				      struct pinctrl_gpio_range *range,
				      unsigned offset)
{
	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
	struct sh_pfc *pfc = pmx->pfc;
382 383
	int idx = sh_pfc_get_pin_index(pfc, offset);
	struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
384
	unsigned long flags;
385
	int ret;
386 387 388

	spin_lock_irqsave(&pfc->lock, flags);

389
	if (cfg->type != PINMUX_TYPE_NONE) {
390 391 392
		dev_err(pfc->dev,
			"Pin %u is busy, can't configure it as GPIO.\n",
			offset);
393 394
		ret = -EBUSY;
		goto done;
395
	}
396

397 398 399 400 401 402 403 404 405 406 407
	if (!pfc->gpio) {
		/* If GPIOs are handled externally the pin mux type need to be
		 * set to GPIO here.
		 */
		const struct sh_pfc_pin *pin = &pfc->info->pins[idx];

		ret = sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_GPIO);
		if (ret < 0)
			goto done;
	}

408 409
	cfg->type = PINMUX_TYPE_GPIO;

410 411
	ret = 0;

412
done:
413 414 415 416 417 418 419 420 421
	spin_unlock_irqrestore(&pfc->lock, flags);

	return ret;
}

static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev,
				     struct pinctrl_gpio_range *range,
				     unsigned offset)
{
422 423 424 425 426 427 428 429 430
	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
	struct sh_pfc *pfc = pmx->pfc;
	int idx = sh_pfc_get_pin_index(pfc, offset);
	struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
	unsigned long flags;

	spin_lock_irqsave(&pfc->lock, flags);
	cfg->type = PINMUX_TYPE_NONE;
	spin_unlock_irqrestore(&pfc->lock, flags);
431 432 433 434 435 436 437
}

static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev,
				     struct pinctrl_gpio_range *range,
				     unsigned offset, bool input)
{
	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
438 439 440 441
	struct sh_pfc *pfc = pmx->pfc;
	int new_type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT;
	int idx = sh_pfc_get_pin_index(pfc, offset);
	const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
442
	struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
443
	unsigned long flags;
444
	unsigned int dir;
445
	int ret;
446

447 448 449 450 451 452 453 454 455
	/* Check if the requested direction is supported by the pin. Not all SoC
	 * provide pin config data, so perform the check conditionally.
	 */
	if (pin->configs) {
		dir = input ? SH_PFC_PIN_CFG_INPUT : SH_PFC_PIN_CFG_OUTPUT;
		if (!(pin->configs & dir))
			return -EINVAL;
	}

456 457
	spin_lock_irqsave(&pfc->lock, flags);

458
	ret = sh_pfc_config_mux(pfc, pin->enum_id, new_type);
459 460 461 462 463 464 465 466
	if (ret < 0)
		goto done;

	cfg->type = new_type;

done:
	spin_unlock_irqrestore(&pfc->lock, flags);
	return ret;
467 468
}

469
static const struct pinmux_ops sh_pfc_pinmux_ops = {
470 471
	.get_functions_count	= sh_pfc_get_functions_count,
	.get_function_name	= sh_pfc_get_function_name,
472
	.get_function_groups	= sh_pfc_get_function_groups,
473
	.set_mux		= sh_pfc_func_set_mux,
474 475 476 477 478
	.gpio_request_enable	= sh_pfc_gpio_request_enable,
	.gpio_disable_free	= sh_pfc_gpio_disable_free,
	.gpio_set_direction	= sh_pfc_gpio_set_direction,
};

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 507 508 509 510 511 512 513 514 515
static u32 sh_pfc_pinconf_find_drive_strength_reg(struct sh_pfc *pfc,
		unsigned int pin, unsigned int *offset, unsigned int *size)
{
	const struct pinmux_drive_reg_field *field;
	const struct pinmux_drive_reg *reg;
	unsigned int i;

	for (reg = pfc->info->drive_regs; reg->reg; ++reg) {
		for (i = 0; i < ARRAY_SIZE(reg->fields); ++i) {
			field = &reg->fields[i];

			if (field->size && field->pin == pin) {
				*offset = field->offset;
				*size = field->size;

				return reg->reg;
			}
		}
	}

	return 0;
}

static int sh_pfc_pinconf_get_drive_strength(struct sh_pfc *pfc,
					     unsigned int pin)
{
	unsigned long flags;
	unsigned int offset;
	unsigned int size;
	u32 reg;
	u32 val;

	reg = sh_pfc_pinconf_find_drive_strength_reg(pfc, pin, &offset, &size);
	if (!reg)
		return -EINVAL;

	spin_lock_irqsave(&pfc->lock, flags);
516
	val = sh_pfc_read(pfc, reg);
517 518 519 520 521 522 523 524 525 526 527 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
	spin_unlock_irqrestore(&pfc->lock, flags);

	val = (val >> offset) & GENMASK(size - 1, 0);

	/* Convert the value to mA based on a full drive strength value of 24mA.
	 * We can make the full value configurable later if needed.
	 */
	return (val + 1) * (size == 2 ? 6 : 3);
}

static int sh_pfc_pinconf_set_drive_strength(struct sh_pfc *pfc,
					     unsigned int pin, u16 strength)
{
	unsigned long flags;
	unsigned int offset;
	unsigned int size;
	unsigned int step;
	u32 reg;
	u32 val;

	reg = sh_pfc_pinconf_find_drive_strength_reg(pfc, pin, &offset, &size);
	if (!reg)
		return -EINVAL;

	step = size == 2 ? 6 : 3;

	if (strength < step || strength > 24)
		return -EINVAL;

	/* Convert the value from mA based on a full drive strength value of
	 * 24mA. We can make the full value configurable later if needed.
	 */
	strength = strength / step - 1;

	spin_lock_irqsave(&pfc->lock, flags);

553
	val = sh_pfc_read(pfc, reg);
554 555 556
	val &= ~GENMASK(offset + size - 1, offset);
	val |= strength << offset;

557
	sh_pfc_write(pfc, reg, val);
558 559 560 561 562 563

	spin_unlock_irqrestore(&pfc->lock, flags);

	return 0;
}

564 565 566 567 568 569 570 571 572
/* Check whether the requested parameter is supported for a pin. */
static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin,
				    enum pin_config_param param)
{
	int idx = sh_pfc_get_pin_index(pfc, _pin);
	const struct sh_pfc_pin *pin = &pfc->info->pins[idx];

	switch (param) {
	case PIN_CONFIG_BIAS_DISABLE:
573 574
		return pin->configs &
			(SH_PFC_PIN_CFG_PULL_UP | SH_PFC_PIN_CFG_PULL_DOWN);
575 576 577 578 579 580 581

	case PIN_CONFIG_BIAS_PULL_UP:
		return pin->configs & SH_PFC_PIN_CFG_PULL_UP;

	case PIN_CONFIG_BIAS_PULL_DOWN:
		return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN;

582 583 584
	case PIN_CONFIG_DRIVE_STRENGTH:
		return pin->configs & SH_PFC_PIN_CFG_DRIVE_STRENGTH;

585 586 587
	case PIN_CONFIG_POWER_SOURCE:
		return pin->configs & SH_PFC_PIN_CFG_IO_VOLTAGE;

588 589 590 591 592
	default:
		return false;
	}
}

593
static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned _pin,
594 595
			      unsigned long *config)
{
596 597
	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
	struct sh_pfc *pfc = pmx->pfc;
598 599
	enum pin_config_param param = pinconf_to_config_param(*config);
	unsigned long flags;
600
	unsigned int arg;
601 602 603 604 605 606 607

	if (!sh_pfc_pinconf_validate(pfc, _pin, param))
		return -ENOTSUPP;

	switch (param) {
	case PIN_CONFIG_BIAS_DISABLE:
	case PIN_CONFIG_BIAS_PULL_UP:
608 609 610
	case PIN_CONFIG_BIAS_PULL_DOWN: {
		unsigned int bias;

611 612 613 614 615 616 617 618 619 620
		if (!pfc->info->ops || !pfc->info->ops->get_bias)
			return -ENOTSUPP;

		spin_lock_irqsave(&pfc->lock, flags);
		bias = pfc->info->ops->get_bias(pfc, _pin);
		spin_unlock_irqrestore(&pfc->lock, flags);

		if (bias != param)
			return -EINVAL;

621
		arg = 0;
622
		break;
623 624
	}

625 626 627 628 629 630 631
	case PIN_CONFIG_DRIVE_STRENGTH: {
		int ret;

		ret = sh_pfc_pinconf_get_drive_strength(pfc, _pin);
		if (ret < 0)
			return ret;

632
		arg = ret;
633 634 635
		break;
	}

636
	case PIN_CONFIG_POWER_SOURCE: {
637 638
		u32 pocctrl, val;
		int bit;
639

640
		if (!pfc->info->ops || !pfc->info->ops->pin_to_pocctrl)
641 642
			return -ENOTSUPP;

643 644 645 646
		bit = pfc->info->ops->pin_to_pocctrl(pfc, _pin, &pocctrl);
		if (WARN(bit < 0, "invalid pin %#x", _pin))
			return bit;

647
		spin_lock_irqsave(&pfc->lock, flags);
648
		val = sh_pfc_read(pfc, pocctrl);
649 650
		spin_unlock_irqrestore(&pfc->lock, flags);

651
		arg = (val & BIT(bit)) ? 3300 : 1800;
652 653
		break;
	}
654

655 656 657
	default:
		return -ENOTSUPP;
	}
658

659
	*config = pinconf_to_config_packed(param, arg);
660
	return 0;
661 662
}

663
static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned _pin,
664
			      unsigned long *configs, unsigned num_configs)
665
{
666
	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
667
	struct sh_pfc *pfc = pmx->pfc;
668
	enum pin_config_param param;
669
	unsigned long flags;
670
	unsigned int i;
671

672 673
	for (i = 0; i < num_configs; i++) {
		param = pinconf_to_config_param(configs[i]);
674

675
		if (!sh_pfc_pinconf_validate(pfc, _pin, param))
676 677
			return -ENOTSUPP;

678 679 680 681 682 683
		switch (param) {
		case PIN_CONFIG_BIAS_PULL_UP:
		case PIN_CONFIG_BIAS_PULL_DOWN:
		case PIN_CONFIG_BIAS_DISABLE:
			if (!pfc->info->ops || !pfc->info->ops->set_bias)
				return -ENOTSUPP;
684

685 686 687
			spin_lock_irqsave(&pfc->lock, flags);
			pfc->info->ops->set_bias(pfc, _pin, param);
			spin_unlock_irqrestore(&pfc->lock, flags);
688

689 690
			break;

691 692 693 694 695 696 697 698 699 700 701 702
		case PIN_CONFIG_DRIVE_STRENGTH: {
			unsigned int arg =
				pinconf_to_config_argument(configs[i]);
			int ret;

			ret = sh_pfc_pinconf_set_drive_strength(pfc, _pin, arg);
			if (ret < 0)
				return ret;

			break;
		}

703
		case PIN_CONFIG_POWER_SOURCE: {
704 705 706
			unsigned int mV = pinconf_to_config_argument(configs[i]);
			u32 pocctrl, val;
			int bit;
707

708
			if (!pfc->info->ops || !pfc->info->ops->pin_to_pocctrl)
709 710
				return -ENOTSUPP;

711 712 713 714 715 716 717
			bit = pfc->info->ops->pin_to_pocctrl(pfc, _pin, &pocctrl);
			if (WARN(bit < 0, "invalid pin %#x", _pin))
				return bit;

			if (mV != 1800 && mV != 3300)
				return -EINVAL;

718
			spin_lock_irqsave(&pfc->lock, flags);
719
			val = sh_pfc_read(pfc, pocctrl);
720 721 722 723
			if (mV == 3300)
				val |= BIT(bit);
			else
				val &= ~BIT(bit);
724
			sh_pfc_write(pfc, pocctrl, val);
725 726 727 728 729
			spin_unlock_irqrestore(&pfc->lock, flags);

			break;
		}

730 731 732 733
		default:
			return -ENOTSUPP;
		}
	} /* for each config */
734 735

	return 0;
736 737
}

738
static int sh_pfc_pinconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
739 740
				    unsigned long *configs,
				    unsigned num_configs)
741
{
742 743 744
	struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
	const unsigned int *pins;
	unsigned int num_pins;
745
	unsigned int i, ret;
746 747 748 749

	pins = pmx->pfc->info->groups[group].pins;
	num_pins = pmx->pfc->info->groups[group].nr_pins;

750 751 752 753 754
	for (i = 0; i < num_pins; ++i) {
		ret = sh_pfc_pinconf_set(pctldev, pins[i], configs, num_configs);
		if (ret)
			return ret;
	}
755 756

	return 0;
757 758
}

759
static const struct pinconf_ops sh_pfc_pinconf_ops = {
760 761 762 763 764
	.is_generic			= true,
	.pin_config_get			= sh_pfc_pinconf_get,
	.pin_config_set			= sh_pfc_pinconf_set,
	.pin_config_group_set		= sh_pfc_pinconf_group_set,
	.pin_config_config_dbg_show	= pinconf_generic_dump_config,
765 766
};

767 768
/* PFC ranges -> pinctrl pin descs */
static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
769
{
770 771
	unsigned int i;

772
	/* Allocate and initialize the pins and configs arrays. */
773 774
	pmx->pins = devm_kzalloc(pfc->dev,
				 sizeof(*pmx->pins) * pfc->info->nr_pins,
L
Laurent Pinchart 已提交
775
				 GFP_KERNEL);
776
	if (unlikely(!pmx->pins))
777 778
		return -ENOMEM;

779 780 781 782 783 784
	pmx->configs = devm_kzalloc(pfc->dev,
				    sizeof(*pmx->configs) * pfc->info->nr_pins,
				    GFP_KERNEL);
	if (unlikely(!pmx->configs))
		return -ENOMEM;

785 786 787 788
	for (i = 0; i < pfc->info->nr_pins; ++i) {
		const struct sh_pfc_pin *info = &pfc->info->pins[i];
		struct sh_pfc_pin_config *cfg = &pmx->configs[i];
		struct pinctrl_pin_desc *pin = &pmx->pins[i];
789

790 791 792 793
		/* If the pin number is equal to -1 all pins are considered */
		pin->number = info->pin != (u16)-1 ? info->pin : i;
		pin->name = info->name;
		cfg->type = PINMUX_TYPE_NONE;
794 795
	}

796
	return 0;
797 798
}

799
int sh_pfc_register_pinctrl(struct sh_pfc *pfc)
800
{
801
	struct sh_pfc_pinctrl *pmx;
802
	int ret;
803

L
Laurent Pinchart 已提交
804
	pmx = devm_kzalloc(pfc->dev, sizeof(*pmx), GFP_KERNEL);
805 806 807 808
	if (unlikely(!pmx))
		return -ENOMEM;

	pmx->pfc = pfc;
809

810 811 812
	ret = sh_pfc_map_pins(pfc, pmx);
	if (ret < 0)
		return ret;
813

814 815 816 817 818
	pmx->pctl_desc.name = DRV_NAME;
	pmx->pctl_desc.owner = THIS_MODULE;
	pmx->pctl_desc.pctlops = &sh_pfc_pinctrl_ops;
	pmx->pctl_desc.pmxops = &sh_pfc_pinmux_ops;
	pmx->pctl_desc.confops = &sh_pfc_pinconf_ops;
819
	pmx->pctl_desc.pins = pmx->pins;
820
	pmx->pctl_desc.npins = pfc->info->nr_pins;
821

822 823 824 825 826 827 828 829 830
	ret = devm_pinctrl_register_and_init(pfc->dev, &pmx->pctl_desc, pmx,
					     &pmx->pctl);
	if (ret) {
		dev_err(pfc->dev, "could not register: %i\n", ret);

		return ret;
	}

	return pinctrl_enable(pmx->pctl);
831
}