pinctrl-sunxi.c 29.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Allwinner A1X SoCs pinctrl driver.
 *
 * Copyright (C) 2012 Maxime Ripard
 *
 * Maxime Ripard <maxime.ripard@free-electrons.com>
 *
 * This file is licensed under the terms of the GNU General Public
 * License version 2.  This program is licensed "as is" without any
 * warranty of any kind, whether express or implied.
 */

#include <linux/io.h>
14
#include <linux/clk.h>
15
#include <linux/gpio/driver.h>
16
#include <linux/irqdomain.h>
17
#include <linux/irqchip/chained_irq.h>
18
#include <linux/export.h>
19 20 21
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_device.h>
22
#include <linux/of_irq.h>
23 24 25 26 27 28 29 30
#include <linux/pinctrl/consumer.h>
#include <linux/pinctrl/machine.h>
#include <linux/pinctrl/pinctrl.h>
#include <linux/pinctrl/pinconf-generic.h>
#include <linux/pinctrl/pinmux.h>
#include <linux/platform_device.h>
#include <linux/slab.h>

31 32
#include <dt-bindings/pinctrl/sun4i-a10.h>

33
#include "../core.h"
34
#include "pinctrl-sunxi.h"
35

36 37 38
static struct irq_chip sunxi_pinctrl_edge_irq_chip;
static struct irq_chip sunxi_pinctrl_level_irq_chip;

39 40 41 42 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
static struct sunxi_pinctrl_group *
sunxi_pinctrl_find_group_by_name(struct sunxi_pinctrl *pctl, const char *group)
{
	int i;

	for (i = 0; i < pctl->ngroups; i++) {
		struct sunxi_pinctrl_group *grp = pctl->groups + i;

		if (!strcmp(grp->name, group))
			return grp;
	}

	return NULL;
}

static struct sunxi_pinctrl_function *
sunxi_pinctrl_find_function_by_name(struct sunxi_pinctrl *pctl,
				    const char *name)
{
	struct sunxi_pinctrl_function *func = pctl->functions;
	int i;

	for (i = 0; i < pctl->nfunctions; i++) {
		if (!func[i].name)
			break;

		if (!strcmp(func[i].name, name))
			return func + i;
	}

	return NULL;
}

static struct sunxi_desc_function *
sunxi_pinctrl_desc_find_function_by_name(struct sunxi_pinctrl *pctl,
					 const char *pin_name,
					 const char *func_name)
{
	int i;

	for (i = 0; i < pctl->desc->npins; i++) {
		const struct sunxi_desc_pin *pin = pctl->desc->pins + i;

		if (!strcmp(pin->pin.name, pin_name)) {
			struct sunxi_desc_function *func = pin->functions;

			while (func->name) {
				if (!strcmp(func->name, func_name))
					return func;

				func++;
			}
		}
	}

	return NULL;
}

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
static struct sunxi_desc_function *
sunxi_pinctrl_desc_find_function_by_pin(struct sunxi_pinctrl *pctl,
					const u16 pin_num,
					const char *func_name)
{
	int i;

	for (i = 0; i < pctl->desc->npins; i++) {
		const struct sunxi_desc_pin *pin = pctl->desc->pins + i;

		if (pin->pin.number == pin_num) {
			struct sunxi_desc_function *func = pin->functions;

			while (func->name) {
				if (!strcmp(func->name, func_name))
					return func;

				func++;
			}
		}
	}

	return NULL;
}

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
static int sunxi_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
{
	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);

	return pctl->ngroups;
}

static const char *sunxi_pctrl_get_group_name(struct pinctrl_dev *pctldev,
					      unsigned group)
{
	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);

	return pctl->groups[group].name;
}

static int sunxi_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
				      unsigned group,
				      const unsigned **pins,
				      unsigned *num_pins)
{
	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);

	*pins = (unsigned *)&pctl->groups[group].pin;
	*num_pins = 1;

	return 0;
}

150 151
static bool sunxi_pctrl_has_bias_prop(struct device_node *node)
{
152 153 154 155
	return of_find_property(node, "bias-pull-up", NULL) ||
		of_find_property(node, "bias-pull-down", NULL) ||
		of_find_property(node, "bias-disable", NULL) ||
		of_find_property(node, "allwinner,pull", NULL);
156 157 158 159
}

static bool sunxi_pctrl_has_drive_prop(struct device_node *node)
{
160 161
	return of_find_property(node, "drive-strength", NULL) ||
		of_find_property(node, "allwinner,drive", NULL);
162 163 164 165 166 167
}

static int sunxi_pctrl_parse_bias_prop(struct device_node *node)
{
	u32 val;

168 169 170 171 172 173 174 175 176 177 178
	/* Try the new style binding */
	if (of_find_property(node, "bias-pull-up", NULL))
		return PIN_CONFIG_BIAS_PULL_UP;

	if (of_find_property(node, "bias-pull-down", NULL))
		return PIN_CONFIG_BIAS_PULL_DOWN;

	if (of_find_property(node, "bias-disable", NULL))
		return PIN_CONFIG_BIAS_DISABLE;

	/* And fall back to the old binding */
179 180 181 182
	if (of_property_read_u32(node, "allwinner,pull", &val))
		return -EINVAL;

	switch (val) {
183 184
	case SUN4I_PINCTRL_NO_PULL:
		return PIN_CONFIG_BIAS_DISABLE;
185
	case SUN4I_PINCTRL_PULL_UP:
186
		return PIN_CONFIG_BIAS_PULL_UP;
187
	case SUN4I_PINCTRL_PULL_DOWN:
188 189 190 191 192 193 194 195 196 197
		return PIN_CONFIG_BIAS_PULL_DOWN;
	}

	return -EINVAL;
}

static int sunxi_pctrl_parse_drive_prop(struct device_node *node)
{
	u32 val;

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	/* Try the new style binding */
	if (!of_property_read_u32(node, "drive-strength", &val)) {
		/* We can't go below 10mA ... */
		if (val < 10)
			return -EINVAL;

		/* ... and only up to 40 mA ... */
		if (val > 40)
			val = 40;

		/* by steps of 10 mA */
		return rounddown(val, 10);
	}

	/* And then fall back to the old binding */
213 214 215 216 217 218 219 220 221 222 223
	if (of_property_read_u32(node, "allwinner,drive", &val))
		return -EINVAL;

	return (val + 1) * 10;
}

static const char *sunxi_pctrl_parse_function_prop(struct device_node *node)
{
	const char *function;
	int ret;

224 225 226 227 228 229
	/* Try the generic binding */
	ret = of_property_read_string(node, "function", &function);
	if (!ret)
		return function;

	/* And fall back to our legacy one */
230 231 232 233 234 235 236 237 238 239 240 241
	ret = of_property_read_string(node, "allwinner,function", &function);
	if (!ret)
		return function;

	return NULL;
}

static const char *sunxi_pctrl_find_pins_prop(struct device_node *node,
					      int *npins)
{
	int count;

242 243 244 245 246 247 248 249
	/* Try the generic binding */
	count = of_property_count_strings(node, "pins");
	if (count > 0) {
		*npins = count;
		return "pins";
	}

	/* And fall back to our legacy one */
250 251 252 253 254 255 256 257 258 259 260 261 262 263
	count = of_property_count_strings(node, "allwinner,pins");
	if (count > 0) {
		*npins = count;
		return "allwinner,pins";
	}

	return NULL;
}

static unsigned long *sunxi_pctrl_build_pin_config(struct device_node *node,
						   unsigned int *len)
{
	unsigned long *pinconfig;
	unsigned int configlen = 0, idx = 0;
264
	int ret;
265 266 267 268 269 270

	if (sunxi_pctrl_has_drive_prop(node))
		configlen++;
	if (sunxi_pctrl_has_bias_prop(node))
		configlen++;

271 272 273 274 275 276
	/*
	 * If we don't have any configuration, bail out
	 */
	if (!configlen)
		return NULL;

277 278
	pinconfig = kzalloc(configlen * sizeof(*pinconfig), GFP_KERNEL);
	if (!pinconfig)
279
		return ERR_PTR(-ENOMEM);
280 281 282

	if (sunxi_pctrl_has_drive_prop(node)) {
		int drive = sunxi_pctrl_parse_drive_prop(node);
283 284
		if (drive < 0) {
			ret = drive;
285
			goto err_free;
286
		}
287 288 289 290 291 292 293

		pinconfig[idx++] = pinconf_to_config_packed(PIN_CONFIG_DRIVE_STRENGTH,
							  drive);
	}

	if (sunxi_pctrl_has_bias_prop(node)) {
		int pull = sunxi_pctrl_parse_bias_prop(node);
294 295
		if (pull < 0) {
			ret = pull;
296
			goto err_free;
297
		}
298 299 300 301 302 303 304 305 306 307

		pinconfig[idx++] = pinconf_to_config_packed(pull, 0);
	}


	*len = configlen;
	return pinconfig;

err_free:
	kfree(pinconfig);
308
	return ERR_PTR(ret);
309 310
}

311 312 313 314 315 316 317 318
static int sunxi_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
				      struct device_node *node,
				      struct pinctrl_map **map,
				      unsigned *num_maps)
{
	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
	unsigned long *pinconfig;
	struct property *prop;
319
	const char *function, *pin_prop;
320
	const char *group;
321
	int ret, npins, nmaps, configlen = 0, i = 0;
322 323 324 325

	*map = NULL;
	*num_maps = 0;

326 327 328
	function = sunxi_pctrl_parse_function_prop(node);
	if (!function) {
		dev_err(pctl->dev, "missing function property in node %s\n",
329 330 331 332
			node->name);
		return -EINVAL;
	}

333 334 335
	pin_prop = sunxi_pctrl_find_pins_prop(node, &npins);
	if (!pin_prop) {
		dev_err(pctl->dev, "missing pins property in node %s\n",
336 337 338 339
			node->name);
		return -EINVAL;
	}

340 341
	/*
	 * We have two maps for each pin: one for the function, one
342 343 344 345
	 * for the configuration (bias, strength, etc).
	 *
	 * We might be slightly overshooting, since we might not have
	 * any configuration.
346 347
	 */
	nmaps = npins * 2;
348
	*map = kmalloc(nmaps * sizeof(struct pinctrl_map), GFP_KERNEL);
349
	if (!*map)
350 351
		return -ENOMEM;

352
	pinconfig = sunxi_pctrl_build_pin_config(node, &configlen);
353 354
	if (IS_ERR(pinconfig)) {
		ret = PTR_ERR(pinconfig);
355 356 357 358
		goto err_free_map;
	}

	of_property_for_each_string(node, pin_prop, prop, group) {
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
		struct sunxi_pinctrl_group *grp =
			sunxi_pinctrl_find_group_by_name(pctl, group);

		if (!grp) {
			dev_err(pctl->dev, "unknown pin %s", group);
			continue;
		}

		if (!sunxi_pinctrl_desc_find_function_by_name(pctl,
							      grp->name,
							      function)) {
			dev_err(pctl->dev, "unsupported function %s on pin %s",
				function, group);
			continue;
		}

		(*map)[i].type = PIN_MAP_TYPE_MUX_GROUP;
		(*map)[i].data.mux.group = group;
		(*map)[i].data.mux.function = function;

		i++;

381 382 383 384 385 386 387
		if (pinconfig) {
			(*map)[i].type = PIN_MAP_TYPE_CONFIGS_GROUP;
			(*map)[i].data.configs.group_or_pin = group;
			(*map)[i].data.configs.configs = pinconfig;
			(*map)[i].data.configs.num_configs = configlen;
			i++;
		}
388 389
	}

390 391 392 393 394 395 396 397 398
	*num_maps = i;

	/*
	 * We know have the number of maps we need, we can resize our
	 * map array
	 */
	*map = krealloc(*map, i * sizeof(struct pinctrl_map), GFP_KERNEL);
	if (!map)
		return -ENOMEM;
399 400

	return 0;
401 402 403 404

err_free_map:
	kfree(map);
	return ret;
405 406 407 408 409 410
}

static void sunxi_pctrl_dt_free_map(struct pinctrl_dev *pctldev,
				    struct pinctrl_map *map,
				    unsigned num_maps)
{
411 412
	/* All the maps have the same pin config, free only the first one */
	kfree(map[0].data.configs.configs);
413 414 415
	kfree(map);
}

416
static const struct pinctrl_ops sunxi_pctrl_ops = {
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
	.dt_node_to_map		= sunxi_pctrl_dt_node_to_map,
	.dt_free_map		= sunxi_pctrl_dt_free_map,
	.get_groups_count	= sunxi_pctrl_get_groups_count,
	.get_group_name		= sunxi_pctrl_get_group_name,
	.get_group_pins		= sunxi_pctrl_get_group_pins,
};

static int sunxi_pconf_group_get(struct pinctrl_dev *pctldev,
				 unsigned group,
				 unsigned long *config)
{
	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);

	*config = pctl->groups[group].config;

	return 0;
}

static int sunxi_pconf_group_set(struct pinctrl_dev *pctldev,
				 unsigned group,
437 438
				 unsigned long *configs,
				 unsigned num_configs)
439 440 441
{
	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
	struct sunxi_pinctrl_group *g = &pctl->groups[group];
M
Maxime Ripard 已提交
442
	unsigned long flags;
443
	unsigned pin = g->pin - pctl->desc->pin_base;
444 445 446
	u32 val, mask;
	u16 strength;
	u8 dlevel;
447
	int i;
448

L
Linus Walleij 已提交
449
	spin_lock_irqsave(&pctl->lock, flags);
M
Maxime Ripard 已提交
450

451 452 453 454
	for (i = 0; i < num_configs; i++) {
		switch (pinconf_to_config_param(configs[i])) {
		case PIN_CONFIG_DRIVE_STRENGTH:
			strength = pinconf_to_config_argument(configs[i]);
455 456
			if (strength > 40) {
				spin_unlock_irqrestore(&pctl->lock, flags);
457
				return -EINVAL;
458
			}
459 460 461 462 463 464 465 466
			/*
			 * We convert from mA to what the register expects:
			 *   0: 10mA
			 *   1: 20mA
			 *   2: 30mA
			 *   3: 40mA
			 */
			dlevel = strength / 10 - 1;
467 468
			val = readl(pctl->membase + sunxi_dlevel_reg(pin));
			mask = DLEVEL_PINS_MASK << sunxi_dlevel_offset(pin);
469
			writel((val & ~mask)
470 471
				| dlevel << sunxi_dlevel_offset(pin),
				pctl->membase + sunxi_dlevel_reg(pin));
472
			break;
473 474 475 476 477 478
		case PIN_CONFIG_BIAS_DISABLE:
			val = readl(pctl->membase + sunxi_pull_reg(pin));
			mask = PULL_PINS_MASK << sunxi_pull_offset(pin);
			writel((val & ~mask),
			       pctl->membase + sunxi_pull_reg(pin));
			break;
479
		case PIN_CONFIG_BIAS_PULL_UP:
480 481 482 483
			val = readl(pctl->membase + sunxi_pull_reg(pin));
			mask = PULL_PINS_MASK << sunxi_pull_offset(pin);
			writel((val & ~mask) | 1 << sunxi_pull_offset(pin),
				pctl->membase + sunxi_pull_reg(pin));
484 485
			break;
		case PIN_CONFIG_BIAS_PULL_DOWN:
486 487 488 489
			val = readl(pctl->membase + sunxi_pull_reg(pin));
			mask = PULL_PINS_MASK << sunxi_pull_offset(pin);
			writel((val & ~mask) | 2 << sunxi_pull_offset(pin),
				pctl->membase + sunxi_pull_reg(pin));
490 491 492 493 494 495 496
			break;
		default:
			break;
		}
		/* cache the config value */
		g->config = configs[i];
	} /* for each config */
497

L
Linus Walleij 已提交
498
	spin_unlock_irqrestore(&pctl->lock, flags);
499 500 501 502

	return 0;
}

503
static const struct pinconf_ops sunxi_pconf_ops = {
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
	.pin_config_group_get	= sunxi_pconf_group_get,
	.pin_config_group_set	= sunxi_pconf_group_set,
};

static int sunxi_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
{
	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);

	return pctl->nfunctions;
}

static const char *sunxi_pmx_get_func_name(struct pinctrl_dev *pctldev,
					   unsigned function)
{
	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);

	return pctl->functions[function].name;
}

static int sunxi_pmx_get_func_groups(struct pinctrl_dev *pctldev,
				     unsigned function,
				     const char * const **groups,
				     unsigned * const num_groups)
{
	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);

	*groups = pctl->functions[function].groups;
	*num_groups = pctl->functions[function].ngroups;

	return 0;
}

static void sunxi_pmx_set(struct pinctrl_dev *pctldev,
				 unsigned pin,
				 u8 config)
{
	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
M
Maxime Ripard 已提交
541 542 543 544
	unsigned long flags;
	u32 val, mask;

	spin_lock_irqsave(&pctl->lock, flags);
545

546
	pin -= pctl->desc->pin_base;
M
Maxime Ripard 已提交
547 548
	val = readl(pctl->membase + sunxi_mux_reg(pin));
	mask = MUX_PINS_MASK << sunxi_mux_offset(pin);
549 550
	writel((val & ~mask) | config << sunxi_mux_offset(pin),
		pctl->membase + sunxi_mux_reg(pin));
M
Maxime Ripard 已提交
551 552

	spin_unlock_irqrestore(&pctl->lock, flags);
553 554
}

555 556 557
static int sunxi_pmx_set_mux(struct pinctrl_dev *pctldev,
			     unsigned function,
			     unsigned group)
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
{
	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
	struct sunxi_pinctrl_group *g = pctl->groups + group;
	struct sunxi_pinctrl_function *func = pctl->functions + function;
	struct sunxi_desc_function *desc =
		sunxi_pinctrl_desc_find_function_by_name(pctl,
							 g->name,
							 func->name);

	if (!desc)
		return -EINVAL;

	sunxi_pmx_set(pctldev, g->pin, desc->muxval);

	return 0;
}

575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
static int
sunxi_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
			struct pinctrl_gpio_range *range,
			unsigned offset,
			bool input)
{
	struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
	struct sunxi_desc_function *desc;
	const char *func;

	if (input)
		func = "gpio_in";
	else
		func = "gpio_out";

590 591 592
	desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, offset, func);
	if (!desc)
		return -EINVAL;
593 594 595

	sunxi_pmx_set(pctldev, offset, desc->muxval);

596
	return 0;
597 598
}

599
static const struct pinmux_ops sunxi_pmx_ops = {
600 601 602
	.get_functions_count	= sunxi_pmx_get_funcs_cnt,
	.get_function_name	= sunxi_pmx_get_func_name,
	.get_function_groups	= sunxi_pmx_get_func_groups,
603
	.set_mux		= sunxi_pmx_set_mux,
604
	.gpio_set_direction	= sunxi_pmx_gpio_set_direction,
605 606
};

607 608 609 610 611 612 613 614
static int sunxi_pinctrl_gpio_direction_input(struct gpio_chip *chip,
					unsigned offset)
{
	return pinctrl_gpio_direction_input(chip->base + offset);
}

static int sunxi_pinctrl_gpio_get(struct gpio_chip *chip, unsigned offset)
{
615
	struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
616 617
	u32 reg = sunxi_data_reg(offset);
	u8 index = sunxi_data_offset(offset);
618 619
	bool set_mux = pctl->desc->irq_read_needs_mux &&
		gpiochip_line_is_irq(chip, offset);
620
	u32 pin = offset + chip->base;
621 622 623
	u32 val;

	if (set_mux)
624
		sunxi_pmx_set(pctl->pctl_dev, pin, SUN4I_FUNC_INPUT);
625 626 627 628

	val = (readl(pctl->membase + reg) >> index) & DATA_PINS_MASK;

	if (set_mux)
629
		sunxi_pmx_set(pctl->pctl_dev, pin, SUN4I_FUNC_IRQ);
630

631
	return !!val;
632 633 634 635 636
}

static void sunxi_pinctrl_gpio_set(struct gpio_chip *chip,
				unsigned offset, int value)
{
637
	struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
638 639
	u32 reg = sunxi_data_reg(offset);
	u8 index = sunxi_data_offset(offset);
M
Maxime Ripard 已提交
640 641 642 643 644 645
	unsigned long flags;
	u32 regval;

	spin_lock_irqsave(&pctl->lock, flags);

	regval = readl(pctl->membase + reg);
646

647 648 649 650
	if (value)
		regval |= BIT(index);
	else
		regval &= ~(BIT(index));
651

652
	writel(regval, pctl->membase + reg);
M
Maxime Ripard 已提交
653 654

	spin_unlock_irqrestore(&pctl->lock, flags);
655 656
}

657 658 659 660 661 662 663
static int sunxi_pinctrl_gpio_direction_output(struct gpio_chip *chip,
					unsigned offset, int value)
{
	sunxi_pinctrl_gpio_set(chip, offset, value);
	return pinctrl_gpio_direction_output(chip->base + offset);
}

664 665 666 667 668 669 670 671 672
static int sunxi_pinctrl_gpio_of_xlate(struct gpio_chip *gc,
				const struct of_phandle_args *gpiospec,
				u32 *flags)
{
	int pin, base;

	base = PINS_PER_BANK * gpiospec->args[0];
	pin = base + gpiospec->args[1];

673
	if (pin > gc->ngpio)
674 675 676 677 678 679 680 681
		return -EINVAL;

	if (flags)
		*flags = gpiospec->args[2];

	return pin;
}

682 683
static int sunxi_pinctrl_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
{
684
	struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
685
	struct sunxi_desc_function *desc;
686
	unsigned pinnum = pctl->desc->pin_base + offset;
687
	unsigned irqnum;
688

689
	if (offset >= chip->ngpio)
690 691
		return -ENXIO;

692
	desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, pinnum, "irq");
693 694 695
	if (!desc)
		return -EINVAL;

696 697
	irqnum = desc->irqbank * IRQ_PER_BANK + desc->irqnum;

698
	dev_dbg(chip->parent, "%s: request IRQ for GPIO %d, return %d\n",
699
		chip->label, offset + chip->base, irqnum);
700

701
	return irq_find_mapping(pctl->domain, irqnum);
702 703
}

704 705 706 707
static int sunxi_pinctrl_irq_request_resources(struct irq_data *d)
{
	struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
	struct sunxi_desc_function *func;
708
	int ret;
709 710 711 712 713 714

	func = sunxi_pinctrl_desc_find_function_by_pin(pctl,
					pctl->irq_array[d->hwirq], "irq");
	if (!func)
		return -EINVAL;

715
	ret = gpiochip_lock_as_irq(pctl->chip,
716
			pctl->irq_array[d->hwirq] - pctl->desc->pin_base);
717 718 719 720 721 722
	if (ret) {
		dev_err(pctl->dev, "unable to lock HW IRQ %lu for IRQ\n",
			irqd_to_hwirq(d));
		return ret;
	}

723 724
	/* Change muxing to INT mode */
	sunxi_pmx_set(pctl->pctl_dev, pctl->irq_array[d->hwirq], func->muxval);
725

726 727
	return 0;
}
728

729 730 731 732
static void sunxi_pinctrl_irq_release_resources(struct irq_data *d)
{
	struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);

733 734
	gpiochip_unlock_as_irq(pctl->chip,
			      pctl->irq_array[d->hwirq] - pctl->desc->pin_base);
735 736
}

737
static int sunxi_pinctrl_irq_set_type(struct irq_data *d, unsigned int type)
738 739
{
	struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
740
	u32 reg = sunxi_irq_cfg_reg(d->hwirq, pctl->desc->irq_bank_base);
741
	u8 index = sunxi_irq_cfg_offset(d->hwirq);
M
Maxime Ripard 已提交
742
	unsigned long flags;
743
	u32 regval;
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
	u8 mode;

	switch (type) {
	case IRQ_TYPE_EDGE_RISING:
		mode = IRQ_EDGE_RISING;
		break;
	case IRQ_TYPE_EDGE_FALLING:
		mode = IRQ_EDGE_FALLING;
		break;
	case IRQ_TYPE_EDGE_BOTH:
		mode = IRQ_EDGE_BOTH;
		break;
	case IRQ_TYPE_LEVEL_HIGH:
		mode = IRQ_LEVEL_HIGH;
		break;
	case IRQ_TYPE_LEVEL_LOW:
		mode = IRQ_LEVEL_LOW;
		break;
	default:
		return -EINVAL;
	}

M
Maxime Ripard 已提交
766 767
	spin_lock_irqsave(&pctl->lock, flags);

768
	if (type & IRQ_TYPE_LEVEL_MASK)
769 770
		irq_set_chip_handler_name_locked(d, &sunxi_pinctrl_level_irq_chip,
						 handle_fasteoi_irq, NULL);
771
	else
772 773
		irq_set_chip_handler_name_locked(d, &sunxi_pinctrl_edge_irq_chip,
						 handle_edge_irq, NULL);
774

775
	regval = readl(pctl->membase + reg);
776
	regval &= ~(IRQ_CFG_IRQ_MASK << index);
777
	writel(regval | (mode << index), pctl->membase + reg);
778

M
Maxime Ripard 已提交
779
	spin_unlock_irqrestore(&pctl->lock, flags);
780 781 782 783

	return 0;
}

784
static void sunxi_pinctrl_irq_ack(struct irq_data *d)
785 786
{
	struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
787 788
	u32 status_reg = sunxi_irq_status_reg(d->hwirq,
					      pctl->desc->irq_bank_base);
789 790 791 792 793 794 795 796 797
	u8 status_idx = sunxi_irq_status_offset(d->hwirq);

	/* Clear the IRQ */
	writel(1 << status_idx, pctl->membase + status_reg);
}

static void sunxi_pinctrl_irq_mask(struct irq_data *d)
{
	struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
798
	u32 reg = sunxi_irq_ctrl_reg(d->hwirq, pctl->desc->irq_bank_base);
799
	u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
M
Maxime Ripard 已提交
800
	unsigned long flags;
801 802
	u32 val;

M
Maxime Ripard 已提交
803 804
	spin_lock_irqsave(&pctl->lock, flags);

805 806 807
	/* Mask the IRQ */
	val = readl(pctl->membase + reg);
	writel(val & ~(1 << idx), pctl->membase + reg);
M
Maxime Ripard 已提交
808 809

	spin_unlock_irqrestore(&pctl->lock, flags);
810 811 812 813 814
}

static void sunxi_pinctrl_irq_unmask(struct irq_data *d)
{
	struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
815
	u32 reg = sunxi_irq_ctrl_reg(d->hwirq, pctl->desc->irq_bank_base);
816
	u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
M
Maxime Ripard 已提交
817
	unsigned long flags;
818 819
	u32 val;

M
Maxime Ripard 已提交
820 821
	spin_lock_irqsave(&pctl->lock, flags);

822 823 824
	/* Unmask the IRQ */
	val = readl(pctl->membase + reg);
	writel(val | (1 << idx), pctl->membase + reg);
M
Maxime Ripard 已提交
825 826

	spin_unlock_irqrestore(&pctl->lock, flags);
827 828
}

829 830 831 832 833 834
static void sunxi_pinctrl_irq_ack_unmask(struct irq_data *d)
{
	sunxi_pinctrl_irq_ack(d);
	sunxi_pinctrl_irq_unmask(d);
}

835
static struct irq_chip sunxi_pinctrl_edge_irq_chip = {
836
	.name		= "sunxi_pio_edge",
837
	.irq_ack	= sunxi_pinctrl_irq_ack,
838 839
	.irq_mask	= sunxi_pinctrl_irq_mask,
	.irq_unmask	= sunxi_pinctrl_irq_unmask,
840
	.irq_request_resources = sunxi_pinctrl_irq_request_resources,
841
	.irq_release_resources = sunxi_pinctrl_irq_release_resources,
842
	.irq_set_type	= sunxi_pinctrl_irq_set_type,
843
	.flags		= IRQCHIP_SKIP_SET_WAKE,
844 845
};

846
static struct irq_chip sunxi_pinctrl_level_irq_chip = {
847
	.name		= "sunxi_pio_level",
848
	.irq_eoi	= sunxi_pinctrl_irq_ack,
849 850
	.irq_mask	= sunxi_pinctrl_irq_mask,
	.irq_unmask	= sunxi_pinctrl_irq_unmask,
851 852 853 854
	/* Define irq_enable / disable to avoid spurious irqs for drivers
	 * using these to suppress irqs while they clear the irq source */
	.irq_enable	= sunxi_pinctrl_irq_ack_unmask,
	.irq_disable	= sunxi_pinctrl_irq_mask,
855
	.irq_request_resources = sunxi_pinctrl_irq_request_resources,
856
	.irq_release_resources = sunxi_pinctrl_irq_release_resources,
857
	.irq_set_type	= sunxi_pinctrl_irq_set_type,
858 859
	.flags		= IRQCHIP_SKIP_SET_WAKE | IRQCHIP_EOI_THREADED |
			  IRQCHIP_EOI_IF_HANDLED,
860 861
};

862 863 864 865 866 867 868
static int sunxi_pinctrl_irq_of_xlate(struct irq_domain *d,
				      struct device_node *node,
				      const u32 *intspec,
				      unsigned int intsize,
				      unsigned long *out_hwirq,
				      unsigned int *out_type)
{
869
	struct sunxi_pinctrl *pctl = d->host_data;
870 871 872 873 874 875 876
	struct sunxi_desc_function *desc;
	int pin, base;

	if (intsize < 3)
		return -EINVAL;

	base = PINS_PER_BANK * intspec[0];
877
	pin = pctl->desc->pin_base + base + intspec[1];
878

879
	desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, pin, "irq");
880 881 882 883 884 885 886 887 888 889 890 891 892
	if (!desc)
		return -EINVAL;

	*out_hwirq = desc->irqbank * PINS_PER_BANK + desc->irqnum;
	*out_type = intspec[2];

	return 0;
}

static struct irq_domain_ops sunxi_pinctrl_irq_domain_ops = {
	.xlate		= sunxi_pinctrl_irq_of_xlate,
};

893
static void sunxi_pinctrl_irq_handler(struct irq_desc *desc)
894
{
895
	unsigned int irq = irq_desc_get_irq(desc);
896 897
	struct irq_chip *chip = irq_desc_get_chip(desc);
	struct sunxi_pinctrl *pctl = irq_desc_get_handler_data(desc);
898 899 900 901 902 903 904 905
	unsigned long bank, reg, val;

	for (bank = 0; bank < pctl->desc->irq_banks; bank++)
		if (irq == pctl->irq[bank])
			break;

	if (bank == pctl->desc->irq_banks)
		return;
906

907
	reg = sunxi_irq_status_reg_from_bank(bank, pctl->desc->irq_bank_base);
908
	val = readl(pctl->membase + reg);
909

910
	if (val) {
911 912
		int irqoffset;

913
		chained_irq_enter(chip, desc);
914 915 916
		for_each_set_bit(irqoffset, &val, IRQ_PER_BANK) {
			int pin_irq = irq_find_mapping(pctl->domain,
						       bank * IRQ_PER_BANK + irqoffset);
917 918
			generic_handle_irq(pin_irq);
		}
919
		chained_irq_exit(chip, desc);
920 921 922
	}
}

923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
static int sunxi_pinctrl_add_function(struct sunxi_pinctrl *pctl,
					const char *name)
{
	struct sunxi_pinctrl_function *func = pctl->functions;

	while (func->name) {
		/* function already there */
		if (strcmp(func->name, name) == 0) {
			func->ngroups++;
			return -EEXIST;
		}
		func++;
	}

	func->name = name;
	func->ngroups = 1;

	pctl->nfunctions++;

	return 0;
}

static int sunxi_pinctrl_build_state(struct platform_device *pdev)
{
	struct sunxi_pinctrl *pctl = platform_get_drvdata(pdev);
	int i;

	pctl->ngroups = pctl->desc->npins;

	/* Allocate groups */
	pctl->groups = devm_kzalloc(&pdev->dev,
				    pctl->ngroups * sizeof(*pctl->groups),
				    GFP_KERNEL);
	if (!pctl->groups)
		return -ENOMEM;

	for (i = 0; i < pctl->desc->npins; i++) {
		const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
		struct sunxi_pinctrl_group *group = pctl->groups + i;

		group->name = pin->pin.name;
		group->pin = pin->pin.number;
	}

	/*
	 * We suppose that we won't have any more functions than pins,
	 * we'll reallocate that later anyway
	 */
	pctl->functions = devm_kzalloc(&pdev->dev,
				pctl->desc->npins * sizeof(*pctl->functions),
				GFP_KERNEL);
	if (!pctl->functions)
		return -ENOMEM;

	/* Count functions and their associated groups */
	for (i = 0; i < pctl->desc->npins; i++) {
		const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
		struct sunxi_desc_function *func = pin->functions;

		while (func->name) {
983
			/* Create interrupt mapping while we're at it */
984 985 986 987 988
			if (!strcmp(func->name, "irq")) {
				int irqnum = func->irqnum + func->irqbank * IRQ_PER_BANK;
				pctl->irq_array[irqnum] = pin->pin.number;
			}

989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
			sunxi_pinctrl_add_function(pctl, func->name);
			func++;
		}
	}

	pctl->functions = krealloc(pctl->functions,
				pctl->nfunctions * sizeof(*pctl->functions),
				GFP_KERNEL);

	for (i = 0; i < pctl->desc->npins; i++) {
		const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
		struct sunxi_desc_function *func = pin->functions;

		while (func->name) {
			struct sunxi_pinctrl_function *func_item;
			const char **func_grp;

			func_item = sunxi_pinctrl_find_function_by_name(pctl,
									func->name);
			if (!func_item)
				return -EINVAL;

			if (!func_item->groups) {
				func_item->groups =
					devm_kzalloc(&pdev->dev,
						     func_item->ngroups * sizeof(*func_item->groups),
						     GFP_KERNEL);
				if (!func_item->groups)
					return -ENOMEM;
			}

			func_grp = func_item->groups;
			while (*func_grp)
				func_grp++;

			*func_grp = pin->pin.name;
			func++;
		}
	}

	return 0;
}

1032 1033
int sunxi_pinctrl_init(struct platform_device *pdev,
		       const struct sunxi_pinctrl_desc *desc)
1034 1035
{
	struct device_node *node = pdev->dev.of_node;
1036
	struct pinctrl_desc *pctrl_desc;
1037 1038
	struct pinctrl_pin_desc *pins;
	struct sunxi_pinctrl *pctl;
1039
	struct resource *res;
1040
	int i, ret, last_pin;
1041
	struct clk *clk;
1042 1043 1044 1045 1046 1047

	pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
	if (!pctl)
		return -ENOMEM;
	platform_set_drvdata(pdev, pctl);

M
Maxime Ripard 已提交
1048 1049
	spin_lock_init(&pctl->lock);

1050 1051 1052 1053
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	pctl->membase = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(pctl->membase))
		return PTR_ERR(pctl->membase);
1054

1055
	pctl->dev = &pdev->dev;
1056
	pctl->desc = desc;
1057

1058 1059 1060 1061 1062 1063 1064
	pctl->irq_array = devm_kcalloc(&pdev->dev,
				       IRQ_PER_BANK * pctl->desc->irq_banks,
				       sizeof(*pctl->irq_array),
				       GFP_KERNEL);
	if (!pctl->irq_array)
		return -ENOMEM;

1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
	ret = sunxi_pinctrl_build_state(pdev);
	if (ret) {
		dev_err(&pdev->dev, "dt probe failed: %d\n", ret);
		return ret;
	}

	pins = devm_kzalloc(&pdev->dev,
			    pctl->desc->npins * sizeof(*pins),
			    GFP_KERNEL);
	if (!pins)
		return -ENOMEM;

	for (i = 0; i < pctl->desc->npins; i++)
		pins[i] = pctl->desc->pins[i].pin;

1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
	pctrl_desc = devm_kzalloc(&pdev->dev,
				  sizeof(*pctrl_desc),
				  GFP_KERNEL);
	if (!pctrl_desc)
		return -ENOMEM;

	pctrl_desc->name = dev_name(&pdev->dev);
	pctrl_desc->owner = THIS_MODULE;
	pctrl_desc->pins = pins;
	pctrl_desc->npins = pctl->desc->npins;
	pctrl_desc->confops = &sunxi_pconf_ops;
	pctrl_desc->pctlops = &sunxi_pctrl_ops;
	pctrl_desc->pmxops =  &sunxi_pmx_ops;

1094
	pctl->pctl_dev = devm_pinctrl_register(&pdev->dev, pctrl_desc, pctl);
1095
	if (IS_ERR(pctl->pctl_dev)) {
1096
		dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
1097
		return PTR_ERR(pctl->pctl_dev);
1098 1099
	}

1100
	pctl->chip = devm_kzalloc(&pdev->dev, sizeof(*pctl->chip), GFP_KERNEL);
1101 1102
	if (!pctl->chip)
		return -ENOMEM;
1103 1104

	last_pin = pctl->desc->pins[pctl->desc->npins - 1].pin.number;
1105
	pctl->chip->owner = THIS_MODULE;
1106 1107
	pctl->chip->request = gpiochip_generic_request,
	pctl->chip->free = gpiochip_generic_free,
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
	pctl->chip->direction_input = sunxi_pinctrl_gpio_direction_input,
	pctl->chip->direction_output = sunxi_pinctrl_gpio_direction_output,
	pctl->chip->get = sunxi_pinctrl_gpio_get,
	pctl->chip->set = sunxi_pinctrl_gpio_set,
	pctl->chip->of_xlate = sunxi_pinctrl_gpio_of_xlate,
	pctl->chip->to_irq = sunxi_pinctrl_gpio_to_irq,
	pctl->chip->of_gpio_n_cells = 3,
	pctl->chip->can_sleep = false,
	pctl->chip->ngpio = round_up(last_pin, PINS_PER_BANK) -
			    pctl->desc->pin_base;
1118
	pctl->chip->label = dev_name(&pdev->dev);
1119
	pctl->chip->parent = &pdev->dev;
1120
	pctl->chip->base = pctl->desc->pin_base;
1121

1122
	ret = gpiochip_add_data(pctl->chip, pctl);
1123
	if (ret)
1124
		return ret;
1125 1126 1127 1128 1129

	for (i = 0; i < pctl->desc->npins; i++) {
		const struct sunxi_desc_pin *pin = pctl->desc->pins + i;

		ret = gpiochip_add_pin_range(pctl->chip, dev_name(&pdev->dev),
1130
					     pin->pin.number - pctl->desc->pin_base,
1131 1132 1133 1134 1135
					     pin->pin.number, 1);
		if (ret)
			goto gpiochip_error;
	}

1136
	clk = devm_clk_get(&pdev->dev, NULL);
1137 1138
	if (IS_ERR(clk)) {
		ret = PTR_ERR(clk);
1139
		goto gpiochip_error;
1140
	}
1141

1142 1143 1144
	ret = clk_prepare_enable(clk);
	if (ret)
		goto gpiochip_error;
1145

1146 1147 1148 1149
	pctl->irq = devm_kcalloc(&pdev->dev,
				 pctl->desc->irq_banks,
				 sizeof(*pctl->irq),
				 GFP_KERNEL);
1150
	if (!pctl->irq) {
1151
		ret = -ENOMEM;
1152
		goto clk_error;
1153 1154
	}

1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
	for (i = 0; i < pctl->desc->irq_banks; i++) {
		pctl->irq[i] = platform_get_irq(pdev, i);
		if (pctl->irq[i] < 0) {
			ret = pctl->irq[i];
			goto clk_error;
		}
	}

	pctl->domain = irq_domain_add_linear(node,
					     pctl->desc->irq_banks * IRQ_PER_BANK,
1165 1166
					     &sunxi_pinctrl_irq_domain_ops,
					     pctl);
1167 1168 1169
	if (!pctl->domain) {
		dev_err(&pdev->dev, "Couldn't register IRQ domain\n");
		ret = -ENOMEM;
1170
		goto clk_error;
1171 1172
	}

1173
	for (i = 0; i < (pctl->desc->irq_banks * IRQ_PER_BANK); i++) {
1174 1175
		int irqno = irq_create_mapping(pctl->domain, i);

1176 1177
		irq_set_chip_and_handler(irqno, &sunxi_pinctrl_edge_irq_chip,
					 handle_edge_irq);
1178
		irq_set_chip_data(irqno, pctl);
1179
	}
1180

1181
	for (i = 0; i < pctl->desc->irq_banks; i++) {
1182
		/* Mask and clear all IRQs before registering a handler */
1183 1184
		writel(0, pctl->membase + sunxi_irq_ctrl_reg_from_bank(i,
						pctl->desc->irq_bank_base));
1185
		writel(0xffffffff,
1186 1187
		       pctl->membase + sunxi_irq_status_reg_from_bank(i,
						pctl->desc->irq_bank_base));
1188

1189 1190 1191
		irq_set_chained_handler_and_data(pctl->irq[i],
						 sunxi_pinctrl_irq_handler,
						 pctl);
1192
	}
1193

1194
	dev_info(&pdev->dev, "initialized sunXi PIO driver\n");
1195 1196

	return 0;
1197

1198 1199
clk_error:
	clk_disable_unprepare(clk);
1200
gpiochip_error:
1201
	gpiochip_remove(pctl->chip);
1202
	return ret;
1203
}