gpio-mvebu.c 32.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
 * GPIO driver for Marvell SoCs
 *
 * Copyright (C) 2012 Marvell
 *
 * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
 * Andrew Lunn <andrew@lunn.ch>
 * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.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.
 *
 * This driver is a fairly straightforward GPIO driver for the
 * complete family of Marvell EBU SoC platforms (Orion, Dove,
 * Kirkwood, Discovery, Armada 370/XP). The only complexity of this
 * driver is the different register layout that exists between the
 * non-SMP platforms (Orion, Dove, Kirkwood, Armada 370) and the SMP
 * platforms (MV78200 from the Discovery family and the Armada
 * XP). Therefore, this driver handles three variants of the GPIO
 * block:
 * - the basic variant, called "orion-gpio", with the simplest
 *   register set. Used on Orion, Dove, Kirkwoord, Armada 370 and
 *   non-SMP Discovery systems
 * - the mv78200 variant for MV78200 Discovery systems. This variant
 *   turns the edge mask and level mask registers into CPU0 edge
 *   mask/level mask registers, and adds CPU1 edge mask/level mask
 *   registers.
 * - the armadaxp variant for Armada XP systems. This variant keeps
 *   the normal cause/edge mask/level mask registers when the global
 *   interrupts are used, but adds per-CPU cause/edge mask/level mask
 *   registers n a separate memory area for the per-CPU GPIO
 *   interrupts.
 */

36 37
#include <linux/bitops.h>
#include <linux/clk.h>
38
#include <linux/err.h>
39
#include <linux/gpio.h>
40 41
#include <linux/init.h>
#include <linux/io.h>
42
#include <linux/irq.h>
43
#include <linux/irqchip/chained_irq.h>
44
#include <linux/irqdomain.h>
45
#include <linux/mfd/syscon.h>
46
#include <linux/of_device.h>
47
#include <linux/of_irq.h>
48
#include <linux/pinctrl/consumer.h>
49
#include <linux/platform_device.h>
50
#include <linux/pwm.h>
51
#include <linux/regmap.h>
52
#include <linux/slab.h>
53

54 55
#include "gpiolib.h"

56 57 58
/*
 * GPIO unit register offsets.
 */
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#define GPIO_OUT_OFF			0x0000
#define GPIO_IO_CONF_OFF		0x0004
#define GPIO_BLINK_EN_OFF		0x0008
#define GPIO_IN_POL_OFF			0x000c
#define GPIO_DATA_IN_OFF		0x0010
#define GPIO_EDGE_CAUSE_OFF		0x0014
#define GPIO_EDGE_MASK_OFF		0x0018
#define GPIO_LEVEL_MASK_OFF		0x001c
#define GPIO_BLINK_CNT_SELECT_OFF	0x0020

/*
 * PWM register offsets.
 */
#define PWM_BLINK_ON_DURATION_OFF	0x0
#define PWM_BLINK_OFF_DURATION_OFF	0x4

75 76

/* The MV78200 has per-CPU registers for edge mask and level mask */
Andrew Lunn's avatar
Andrew Lunn committed
77
#define GPIO_EDGE_MASK_MV78200_OFF(cpu)	  ((cpu) ? 0x30 : 0x18)
78 79
#define GPIO_LEVEL_MASK_MV78200_OFF(cpu)  ((cpu) ? 0x34 : 0x1C)

80 81
/*
 * The Armada XP has per-CPU registers for interrupt cause, interrupt
82
 * mask and interrupt level mask. Those are relative to the
83 84
 * percpu_membase.
 */
85 86 87 88
#define GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu) ((cpu) * 0x4)
#define GPIO_EDGE_MASK_ARMADAXP_OFF(cpu)  (0x10 + (cpu) * 0x4)
#define GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu) (0x20 + (cpu) * 0x4)

Andrew Lunn's avatar
Andrew Lunn committed
89 90
#define MVEBU_GPIO_SOC_VARIANT_ORION	0x1
#define MVEBU_GPIO_SOC_VARIANT_MV78200	0x2
91
#define MVEBU_GPIO_SOC_VARIANT_ARMADAXP 0x3
92
#define MVEBU_GPIO_SOC_VARIANT_A8K	0x4
93

Andrew Lunn's avatar
Andrew Lunn committed
94
#define MVEBU_MAX_GPIO_PER_BANK		32
95

96 97 98 99 100 101 102 103 104 105 106 107 108 109
struct mvebu_pwm {
	void __iomem		*membase;
	unsigned long		 clk_rate;
	struct gpio_desc	*gpiod;
	struct pwm_chip		 chip;
	spinlock_t		 lock;
	struct mvebu_gpio_chip	*mvchip;

	/* Used to preserve GPIO/PWM registers across suspend/resume */
	u32			 blink_select;
	u32			 blink_on_duration;
	u32			 blink_off_duration;
};

110 111
struct mvebu_gpio_chip {
	struct gpio_chip   chip;
112
	struct regmap     *regs;
113
	u32		   offset;
114
	struct regmap     *percpu_regs;
115
	int		   irqbase;
116
	struct irq_domain *domain;
Andrew Lunn's avatar
Andrew Lunn committed
117
	int		   soc_variant;
118

119 120 121 122
	/* Used for PWM support */
	struct clk	  *clk;
	struct mvebu_pwm  *mvpwm;

Andrew Lunn's avatar
Andrew Lunn committed
123
	/* Used to preserve GPIO registers across suspend/resume */
124 125 126 127 128 129
	u32		   out_reg;
	u32		   io_conf_reg;
	u32		   blink_en_reg;
	u32		   in_pol_reg;
	u32		   edge_mask_regs[4];
	u32		   level_mask_regs[4];
130 131 132 133 134 135 136
};

/*
 * Functions returning addresses of individual registers for a given
 * GPIO controller.
 */

137 138
static void mvebu_gpioreg_edge_cause(struct mvebu_gpio_chip *mvchip,
			 struct regmap **map, unsigned int *offset)
139
{
140
	int cpu;
141

142 143 144
	switch (mvchip->soc_variant) {
	case MVEBU_GPIO_SOC_VARIANT_ORION:
	case MVEBU_GPIO_SOC_VARIANT_MV78200:
145
	case MVEBU_GPIO_SOC_VARIANT_A8K:
146
		*map = mvchip->regs;
147
		*offset = GPIO_EDGE_CAUSE_OFF + mvchip->offset;
148 149 150 151 152 153 154 155 156
		break;
	case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
		cpu = smp_processor_id();
		*map = mvchip->percpu_regs;
		*offset = GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu);
		break;
	default:
		BUG();
	}
157 158
}

159 160
static u32
mvebu_gpio_read_edge_cause(struct mvebu_gpio_chip *mvchip)
161
{
162 163 164
	struct regmap *map;
	unsigned int offset;
	u32 val;
165

166 167 168 169
	mvebu_gpioreg_edge_cause(mvchip, &map, &offset);
	regmap_read(map, offset, &val);

	return val;
170 171
}

172 173
static void
mvebu_gpio_write_edge_cause(struct mvebu_gpio_chip *mvchip, u32 val)
174
{
175 176 177 178 179
	struct regmap *map;
	unsigned int offset;

	mvebu_gpioreg_edge_cause(mvchip, &map, &offset);
	regmap_write(map, offset, val);
180 181
}

182 183 184
static inline void
mvebu_gpioreg_edge_mask(struct mvebu_gpio_chip *mvchip,
			struct regmap **map, unsigned int *offset)
185 186 187
{
	int cpu;

188
	switch (mvchip->soc_variant) {
189
	case MVEBU_GPIO_SOC_VARIANT_ORION:
190
	case MVEBU_GPIO_SOC_VARIANT_A8K:
191
		*map = mvchip->regs;
192
		*offset = GPIO_EDGE_MASK_OFF + mvchip->offset;
193
		break;
194
	case MVEBU_GPIO_SOC_VARIANT_MV78200:
195 196 197 198
		cpu = smp_processor_id();
		*map = mvchip->regs;
		*offset = GPIO_EDGE_MASK_MV78200_OFF(cpu);
		break;
199 200
	case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
		cpu = smp_processor_id();
201 202 203
		*map = mvchip->percpu_regs;
		*offset = GPIO_EDGE_MASK_ARMADAXP_OFF(cpu);
		break;
204 205 206 207 208
	default:
		BUG();
	}
}

209 210
static u32
mvebu_gpio_read_edge_mask(struct mvebu_gpio_chip *mvchip)
211
{
212 213 214
	struct regmap *map;
	unsigned int offset;
	u32 val;
215

216 217 218 219
	mvebu_gpioreg_edge_mask(mvchip, &map, &offset);
	regmap_read(map, offset, &val);

	return val;
220 221
}

222 223 224 225 226 227 228 229 230 231 232 233 234
static void
mvebu_gpio_write_edge_mask(struct mvebu_gpio_chip *mvchip, u32 val)
{
	struct regmap *map;
	unsigned int offset;

	mvebu_gpioreg_edge_mask(mvchip, &map, &offset);
	regmap_write(map, offset, val);
}

static void
mvebu_gpioreg_level_mask(struct mvebu_gpio_chip *mvchip,
			 struct regmap **map, unsigned int *offset)
235 236 237
{
	int cpu;

238
	switch (mvchip->soc_variant) {
239
	case MVEBU_GPIO_SOC_VARIANT_ORION:
240
	case MVEBU_GPIO_SOC_VARIANT_A8K:
241
		*map = mvchip->regs;
242
		*offset = GPIO_LEVEL_MASK_OFF + mvchip->offset;
243
		break;
244 245
	case MVEBU_GPIO_SOC_VARIANT_MV78200:
		cpu = smp_processor_id();
246 247 248
		*map = mvchip->regs;
		*offset = GPIO_LEVEL_MASK_MV78200_OFF(cpu);
		break;
249 250
	case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
		cpu = smp_processor_id();
251 252 253
		*map = mvchip->percpu_regs;
		*offset = GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu);
		break;
254 255 256 257 258
	default:
		BUG();
	}
}

259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
static u32
mvebu_gpio_read_level_mask(struct mvebu_gpio_chip *mvchip)
{
	struct regmap *map;
	unsigned int offset;
	u32 val;

	mvebu_gpioreg_level_mask(mvchip, &map, &offset);
	regmap_read(map, offset, &val);

	return val;
}

static void
mvebu_gpio_write_level_mask(struct mvebu_gpio_chip *mvchip, u32 val)
{
	struct regmap *map;
	unsigned int offset;

	mvebu_gpioreg_level_mask(mvchip, &map, &offset);
	regmap_write(map, offset, val);
}

282 283 284 285 286 287 288 289 290 291 292 293 294 295
/*
 * Functions returning addresses of individual registers for a given
 * PWM controller.
 */
static void __iomem *mvebu_pwmreg_blink_on_duration(struct mvebu_pwm *mvpwm)
{
	return mvpwm->membase + PWM_BLINK_ON_DURATION_OFF;
}

static void __iomem *mvebu_pwmreg_blink_off_duration(struct mvebu_pwm *mvpwm)
{
	return mvpwm->membase + PWM_BLINK_OFF_DURATION_OFF;
}

296 297 298
/*
 * Functions implementing the gpio_chip methods
 */
299
static void mvebu_gpio_set(struct gpio_chip *chip, unsigned int pin, int value)
300
{
301
	struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
302

303
	regmap_update_bits(mvchip->regs, GPIO_OUT_OFF + mvchip->offset,
304
			   BIT(pin), value ? BIT(pin) : 0);
305 306
}

307
static int mvebu_gpio_get(struct gpio_chip *chip, unsigned int pin)
308
{
309
	struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
310 311
	u32 u;

312
	regmap_read(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset, &u);
313 314 315 316

	if (u & BIT(pin)) {
		u32 data_in, in_pol;

317 318 319 320
		regmap_read(mvchip->regs, GPIO_DATA_IN_OFF + mvchip->offset,
			    &data_in);
		regmap_read(mvchip->regs, GPIO_IN_POL_OFF + mvchip->offset,
			    &in_pol);
321
		u = data_in ^ in_pol;
322
	} else {
323
		regmap_read(mvchip->regs, GPIO_OUT_OFF + mvchip->offset, &u);
324 325 326 327 328
	}

	return (u >> pin) & 1;
}

329 330
static void mvebu_gpio_blink(struct gpio_chip *chip, unsigned int pin,
			     int value)
331
{
332
	struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
333

334
	regmap_update_bits(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset,
335
			   BIT(pin), value ? BIT(pin) : 0);
336 337
}

338
static int mvebu_gpio_direction_input(struct gpio_chip *chip, unsigned int pin)
339
{
340
	struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
341 342
	int ret;

343 344 345 346
	/*
	 * Check with the pinctrl driver whether this pin is usable as
	 * an input GPIO
	 */
347 348 349 350
	ret = pinctrl_gpio_direction_input(chip->base + pin);
	if (ret)
		return ret;

351
	regmap_update_bits(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset,
352
			   BIT(pin), BIT(pin));
353 354 355 356

	return 0;
}

357
static int mvebu_gpio_direction_output(struct gpio_chip *chip, unsigned int pin,
358 359
				       int value)
{
360
	struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
361 362
	int ret;

363 364 365 366
	/*
	 * Check with the pinctrl driver whether this pin is usable as
	 * an output GPIO
	 */
367 368 369 370
	ret = pinctrl_gpio_direction_output(chip->base + pin);
	if (ret)
		return ret;

371
	mvebu_gpio_blink(chip, pin, 0);
372 373
	mvebu_gpio_set(chip, pin, value);

374
	regmap_update_bits(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset,
375
			   BIT(pin), 0);
376 377 378 379

	return 0;
}

380
static int mvebu_gpio_to_irq(struct gpio_chip *chip, unsigned int pin)
381
{
382
	struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
383

384 385 386 387 388 389 390 391 392 393
	return irq_create_mapping(mvchip->domain, pin);
}

/*
 * Functions implementing the irq_chip methods
 */
static void mvebu_gpio_irq_ack(struct irq_data *d)
{
	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
	struct mvebu_gpio_chip *mvchip = gc->private;
394
	u32 mask = d->mask;
395 396

	irq_gc_lock(gc);
397
	mvebu_gpio_write_edge_cause(mvchip, ~mask);
398 399 400 401 402 403 404
	irq_gc_unlock(gc);
}

static void mvebu_gpio_edge_irq_mask(struct irq_data *d)
{
	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
	struct mvebu_gpio_chip *mvchip = gc->private;
405
	struct irq_chip_type *ct = irq_data_get_chip_type(d);
406
	u32 mask = d->mask;
407 408

	irq_gc_lock(gc);
409
	ct->mask_cache_priv &= ~mask;
410
	mvebu_gpio_write_edge_mask(mvchip, ct->mask_cache_priv);
411 412 413 414 415 416 417
	irq_gc_unlock(gc);
}

static void mvebu_gpio_edge_irq_unmask(struct irq_data *d)
{
	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
	struct mvebu_gpio_chip *mvchip = gc->private;
418
	struct irq_chip_type *ct = irq_data_get_chip_type(d);
419
	u32 mask = d->mask;
420 421

	irq_gc_lock(gc);
422
	ct->mask_cache_priv |= mask;
423
	mvebu_gpio_write_edge_mask(mvchip, ct->mask_cache_priv);
424 425 426 427 428 429 430
	irq_gc_unlock(gc);
}

static void mvebu_gpio_level_irq_mask(struct irq_data *d)
{
	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
	struct mvebu_gpio_chip *mvchip = gc->private;
431
	struct irq_chip_type *ct = irq_data_get_chip_type(d);
432
	u32 mask = d->mask;
433 434

	irq_gc_lock(gc);
435
	ct->mask_cache_priv &= ~mask;
436
	mvebu_gpio_write_level_mask(mvchip, ct->mask_cache_priv);
437 438 439 440 441 442 443
	irq_gc_unlock(gc);
}

static void mvebu_gpio_level_irq_unmask(struct irq_data *d)
{
	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
	struct mvebu_gpio_chip *mvchip = gc->private;
444
	struct irq_chip_type *ct = irq_data_get_chip_type(d);
445
	u32 mask = d->mask;
446 447

	irq_gc_lock(gc);
448
	ct->mask_cache_priv |= mask;
449
	mvebu_gpio_write_level_mask(mvchip, ct->mask_cache_priv);
450 451 452 453 454 455 456 457 458 459
	irq_gc_unlock(gc);
}

/*****************************************************************************
 * MVEBU GPIO IRQ
 *
 * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
 * value of the line or the opposite value.
 *
 * Level IRQ handlers: DATA_IN is used directly as cause register.
Andrew Lunn's avatar
Andrew Lunn committed
460
 *		       Interrupt are masked by LEVEL_MASK registers.
461
 * Edge IRQ handlers:  Change in DATA_IN are latched in EDGE_CAUSE.
Andrew Lunn's avatar
Andrew Lunn committed
462
 *		       Interrupt are masked by EDGE_MASK registers.
463
 * Both-edge handlers: Similar to regular Edge handlers, but also swaps
Andrew Lunn's avatar
Andrew Lunn committed
464 465 466
 *		       the polarity to catch the next line transaction.
 *		       This is a race condition that might not perfectly
 *		       work on some use cases.
467 468 469 470
 *
 * Every eight GPIO lines are grouped (OR'ed) before going up to main
 * cause register.
 *
Andrew Lunn's avatar
Andrew Lunn committed
471 472 473 474 475
 *		      EDGE  cause    mask
 *	  data-in   /--------| |-----| |----\
 *     -----| |-----			     ---- to main cause reg
 *	     X	    \----------------| |----/
 *	  polarity    LEVEL	     mask
476 477 478 479 480 481 482 483 484 485 486 487 488
 *
 ****************************************************************************/

static int mvebu_gpio_irq_set_type(struct irq_data *d, unsigned int type)
{
	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
	struct irq_chip_type *ct = irq_data_get_chip_type(d);
	struct mvebu_gpio_chip *mvchip = gc->private;
	int pin;
	u32 u;

	pin = d->hwirq;

489
	regmap_read(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset, &u);
490
	if ((u & BIT(pin)) == 0)
491 492 493 494 495 496 497 498 499 500 501 502 503 504
		return -EINVAL;

	type &= IRQ_TYPE_SENSE_MASK;
	if (type == IRQ_TYPE_NONE)
		return -EINVAL;

	/* Check if we need to change chip and handler */
	if (!(ct->type & type))
		if (irq_setup_alt_chip(d, type))
			return -EINVAL;

	/*
	 * Configure interrupt polarity.
	 */
505
	switch (type) {
506 507
	case IRQ_TYPE_EDGE_RISING:
	case IRQ_TYPE_LEVEL_HIGH:
508 509
		regmap_update_bits(mvchip->regs,
				   GPIO_IN_POL_OFF + mvchip->offset,
510
				   BIT(pin), 0);
511
		break;
512 513
	case IRQ_TYPE_EDGE_FALLING:
	case IRQ_TYPE_LEVEL_LOW:
514 515
		regmap_update_bits(mvchip->regs,
				   GPIO_IN_POL_OFF + mvchip->offset,
516
				   BIT(pin), BIT(pin));
517
		break;
518
	case IRQ_TYPE_EDGE_BOTH: {
519
		u32 data_in, in_pol, val;
520

521 522 523 524
		regmap_read(mvchip->regs,
			    GPIO_IN_POL_OFF + mvchip->offset, &in_pol);
		regmap_read(mvchip->regs,
			    GPIO_DATA_IN_OFF + mvchip->offset, &data_in);
525 526 527 528

		/*
		 * set initial polarity based on current input level
		 */
529 530
		if ((data_in ^ in_pol) & BIT(pin))
			val = BIT(pin); /* falling */
531
		else
532 533
			val = 0; /* raising */

534 535
		regmap_update_bits(mvchip->regs,
				   GPIO_IN_POL_OFF + mvchip->offset,
536
				   BIT(pin), val);
537
		break;
538 539 540 541 542
	}
	}
	return 0;
}

543
static void mvebu_gpio_irq_handler(struct irq_desc *desc)
544
{
545
	struct mvebu_gpio_chip *mvchip = irq_desc_get_handler_data(desc);
546
	struct irq_chip *chip = irq_desc_get_chip(desc);
547
	u32 cause, type, data_in, level_mask, edge_cause, edge_mask;
548 549 550 551 552
	int i;

	if (mvchip == NULL)
		return;

553 554
	chained_irq_enter(chip, desc);

555
	regmap_read(mvchip->regs, GPIO_DATA_IN_OFF + mvchip->offset, &data_in);
556 557 558 559
	level_mask = mvebu_gpio_read_level_mask(mvchip);
	edge_cause = mvebu_gpio_read_edge_cause(mvchip);
	edge_mask  = mvebu_gpio_read_edge_mask(mvchip);

560
	cause = (data_in & level_mask) | (edge_cause & edge_mask);
561 562 563 564

	for (i = 0; i < mvchip->chip.ngpio; i++) {
		int irq;

565
		irq = irq_find_mapping(mvchip->domain, i);
566

567
		if (!(cause & BIT(i)))
568 569
			continue;

570
		type = irq_get_trigger_type(irq);
571 572 573 574
		if ((type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
			/* Swap polarity (race with GPIO line) */
			u32 polarity;

575 576 577
			regmap_read(mvchip->regs,
				    GPIO_IN_POL_OFF + mvchip->offset,
				    &polarity);
578
			polarity ^= BIT(i);
579 580 581
			regmap_write(mvchip->regs,
				     GPIO_IN_POL_OFF + mvchip->offset,
				     polarity);
582
		}
583

584 585
		generic_handle_irq(irq);
	}
586 587

	chained_irq_exit(chip, desc);
588 589
}

590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
/*
 * Functions implementing the pwm_chip methods
 */
static struct mvebu_pwm *to_mvebu_pwm(struct pwm_chip *chip)
{
	return container_of(chip, struct mvebu_pwm, chip);
}

static int mvebu_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
{
	struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
	struct mvebu_gpio_chip *mvchip = mvpwm->mvchip;
	struct gpio_desc *desc;
	unsigned long flags;
	int ret = 0;

	spin_lock_irqsave(&mvpwm->lock, flags);

	if (mvpwm->gpiod) {
		ret = -EBUSY;
	} else {
		desc = gpio_to_desc(mvchip->chip.base + pwm->hwpwm);
		if (!desc) {
			ret = -ENODEV;
			goto out;
		}

		ret = gpiod_request(desc, "mvebu-pwm");
		if (ret)
			goto out;

		ret = gpiod_direction_output(desc, 0);
		if (ret) {
			gpiod_free(desc);
			goto out;
		}

		mvpwm->gpiod = desc;
	}
out:
	spin_unlock_irqrestore(&mvpwm->lock, flags);
	return ret;
}

static void mvebu_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
{
	struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
	unsigned long flags;

	spin_lock_irqsave(&mvpwm->lock, flags);
	gpiod_free(mvpwm->gpiod);
	mvpwm->gpiod = NULL;
	spin_unlock_irqrestore(&mvpwm->lock, flags);
}

static void mvebu_pwm_get_state(struct pwm_chip *chip,
				struct pwm_device *pwm,
				struct pwm_state *state) {

	struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
	struct mvebu_gpio_chip *mvchip = mvpwm->mvchip;
	unsigned long long val;
	unsigned long flags;
	u32 u;

	spin_lock_irqsave(&mvpwm->lock, flags);

	val = (unsigned long long)
		readl_relaxed(mvebu_pwmreg_blink_on_duration(mvpwm));
	val *= NSEC_PER_SEC;
	do_div(val, mvpwm->clk_rate);
	if (val > UINT_MAX)
		state->duty_cycle = UINT_MAX;
	else if (val)
		state->duty_cycle = val;
	else
		state->duty_cycle = 1;

	val = (unsigned long long)
		readl_relaxed(mvebu_pwmreg_blink_off_duration(mvpwm));
	val *= NSEC_PER_SEC;
	do_div(val, mvpwm->clk_rate);
	if (val < state->duty_cycle) {
		state->period = 1;
	} else {
		val -= state->duty_cycle;
		if (val > UINT_MAX)
			state->period = UINT_MAX;
		else if (val)
			state->period = val;
		else
			state->period = 1;
	}

684
	regmap_read(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset, &u);
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
	if (u)
		state->enabled = true;
	else
		state->enabled = false;

	spin_unlock_irqrestore(&mvpwm->lock, flags);
}

static int mvebu_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
			   struct pwm_state *state)
{
	struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
	struct mvebu_gpio_chip *mvchip = mvpwm->mvchip;
	unsigned long long val;
	unsigned long flags;
	unsigned int on, off;

	val = (unsigned long long) mvpwm->clk_rate * state->duty_cycle;
	do_div(val, NSEC_PER_SEC);
	if (val > UINT_MAX)
		return -EINVAL;
	if (val)
		on = val;
	else
		on = 1;

	val = (unsigned long long) mvpwm->clk_rate *
		(state->period - state->duty_cycle);
	do_div(val, NSEC_PER_SEC);
	if (val > UINT_MAX)
		return -EINVAL;
	if (val)
		off = val;
	else
		off = 1;

	spin_lock_irqsave(&mvpwm->lock, flags);

	writel_relaxed(on, mvebu_pwmreg_blink_on_duration(mvpwm));
	writel_relaxed(off, mvebu_pwmreg_blink_off_duration(mvpwm));
	if (state->enabled)
		mvebu_gpio_blink(&mvchip->chip, pwm->hwpwm, 1);
	else
		mvebu_gpio_blink(&mvchip->chip, pwm->hwpwm, 0);

	spin_unlock_irqrestore(&mvpwm->lock, flags);

	return 0;
}

static const struct pwm_ops mvebu_pwm_ops = {
	.request = mvebu_pwm_request,
	.free = mvebu_pwm_free,
	.get_state = mvebu_pwm_get_state,
	.apply = mvebu_pwm_apply,
	.owner = THIS_MODULE,
};

static void __maybe_unused mvebu_pwm_suspend(struct mvebu_gpio_chip *mvchip)
{
	struct mvebu_pwm *mvpwm = mvchip->mvpwm;

747
	regmap_read(mvchip->regs, GPIO_BLINK_CNT_SELECT_OFF + mvchip->offset,
748
		    &mvpwm->blink_select);
749 750 751 752 753 754 755 756 757 758
	mvpwm->blink_on_duration =
		readl_relaxed(mvebu_pwmreg_blink_on_duration(mvpwm));
	mvpwm->blink_off_duration =
		readl_relaxed(mvebu_pwmreg_blink_off_duration(mvpwm));
}

static void __maybe_unused mvebu_pwm_resume(struct mvebu_gpio_chip *mvchip)
{
	struct mvebu_pwm *mvpwm = mvchip->mvpwm;

759
	regmap_write(mvchip->regs, GPIO_BLINK_CNT_SELECT_OFF + mvchip->offset,
760
		     mvpwm->blink_select);
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
	writel_relaxed(mvpwm->blink_on_duration,
		       mvebu_pwmreg_blink_on_duration(mvpwm));
	writel_relaxed(mvpwm->blink_off_duration,
		       mvebu_pwmreg_blink_off_duration(mvpwm));
}

static int mvebu_pwm_probe(struct platform_device *pdev,
			   struct mvebu_gpio_chip *mvchip,
			   int id)
{
	struct device *dev = &pdev->dev;
	struct mvebu_pwm *mvpwm;
	struct resource *res;
	u32 set;

	if (!of_device_is_compatible(mvchip->chip.of_node,
777
				     "marvell,armada-370-gpio"))
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
		return 0;

	if (IS_ERR(mvchip->clk))
		return PTR_ERR(mvchip->clk);

	/*
	 * There are only two sets of PWM configuration registers for
	 * all the GPIO lines on those SoCs which this driver reserves
	 * for the first two GPIO chips. So if the resource is missing
	 * we can't treat it as an error.
	 */
	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pwm");
	if (!res)
		return 0;

	/*
	 * Use set A for lines of GPIO chip with id 0, B for GPIO chip
	 * with id 1. Don't allow further GPIO chips to be used for PWM.
	 */
	if (id == 0)
		set = 0;
	else if (id == 1)
		set = U32_MAX;
	else
		return -EINVAL;
803
	regmap_write(mvchip->regs,
804
		     GPIO_BLINK_CNT_SELECT_OFF + mvchip->offset, set);
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824

	mvpwm = devm_kzalloc(dev, sizeof(struct mvebu_pwm), GFP_KERNEL);
	if (!mvpwm)
		return -ENOMEM;
	mvchip->mvpwm = mvpwm;
	mvpwm->mvchip = mvchip;

	mvpwm->membase = devm_ioremap_resource(dev, res);
	if (IS_ERR(mvpwm->membase))
		return PTR_ERR(mvpwm->membase);

	mvpwm->clk_rate = clk_get_rate(mvchip->clk);
	if (!mvpwm->clk_rate) {
		dev_err(dev, "failed to get clock rate\n");
		return -EINVAL;
	}

	mvpwm->chip.dev = dev;
	mvpwm->chip.ops = &mvebu_pwm_ops;
	mvpwm->chip.npwm = mvchip->chip.ngpio;
825 826 827 828 829 830 831
	/*
	 * There may already be some PWM allocated, so we can't force
	 * mvpwm->chip.base to a fixed point like mvchip->chip.base.
	 * So, we let pwmchip_add() do the numbering and take the next free
	 * region.
	 */
	mvpwm->chip.base = -1;
832 833 834 835 836 837

	spin_lock_init(&mvpwm->lock);

	return pwmchip_add(&mvpwm->chip);
}

838 839 840 841 842
#ifdef CONFIG_DEBUG_FS
#include <linux/seq_file.h>

static void mvebu_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
{
843
	struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
844 845 846
	u32 out, io_conf, blink, in_pol, data_in, cause, edg_msk, lvl_msk;
	int i;

847 848 849 850 851
	regmap_read(mvchip->regs, GPIO_OUT_OFF + mvchip->offset, &out);
	regmap_read(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset, &io_conf);
	regmap_read(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset, &blink);
	regmap_read(mvchip->regs, GPIO_IN_POL_OFF + mvchip->offset, &in_pol);
	regmap_read(mvchip->regs, GPIO_DATA_IN_OFF + mvchip->offset, &data_in);
852 853 854
	cause	= mvebu_gpio_read_edge_cause(mvchip);
	edg_msk	= mvebu_gpio_read_edge_mask(mvchip);
	lvl_msk	= mvebu_gpio_read_level_mask(mvchip);
855 856 857 858 859 860 861 862 863 864

	for (i = 0; i < chip->ngpio; i++) {
		const char *label;
		u32 msk;
		bool is_out;

		label = gpiochip_is_requested(chip, i);
		if (!label)
			continue;

865
		msk = BIT(i);
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
		is_out = !(io_conf & msk);

		seq_printf(s, " gpio-%-3d (%-20.20s)", chip->base + i, label);

		if (is_out) {
			seq_printf(s, " out %s %s\n",
				   out & msk ? "hi" : "lo",
				   blink & msk ? "(blink )" : "");
			continue;
		}

		seq_printf(s, " in  %s (act %s) - IRQ",
			   (data_in ^ in_pol) & msk  ? "hi" : "lo",
			   in_pol & msk ? "lo" : "hi");
		if (!((edg_msk | lvl_msk) & msk)) {
Andrew Lunn's avatar
Andrew Lunn committed
881
			seq_puts(s, " disabled\n");
882 883 884
			continue;
		}
		if (edg_msk & msk)
Andrew Lunn's avatar
Andrew Lunn committed
885
			seq_puts(s, " edge ");
886
		if (lvl_msk & msk)
Andrew Lunn's avatar
Andrew Lunn committed
887
			seq_puts(s, " level");
888 889 890 891 892 893 894
		seq_printf(s, " (%s)\n", cause & msk ? "pending" : "clear  ");
	}
}
#else
#define mvebu_gpio_dbg_show NULL
#endif

895
static const struct of_device_id mvebu_gpio_of_match[] = {
896 897
	{
		.compatible = "marvell,orion-gpio",
Andrew Lunn's avatar
Andrew Lunn committed
898
		.data	    = (void *) MVEBU_GPIO_SOC_VARIANT_ORION,
899 900 901
	},
	{
		.compatible = "marvell,mv78200-gpio",
Andrew Lunn's avatar
Andrew Lunn committed
902
		.data	    = (void *) MVEBU_GPIO_SOC_VARIANT_MV78200,
903 904 905
	},
	{
		.compatible = "marvell,armadaxp-gpio",
Andrew Lunn's avatar
Andrew Lunn committed
906
		.data	    = (void *) MVEBU_GPIO_SOC_VARIANT_ARMADAXP,
907
	},
908
	{
909
		.compatible = "marvell,armada-370-gpio",
910 911
		.data	    = (void *) MVEBU_GPIO_SOC_VARIANT_ORION,
	},
912 913 914 915
	{
		.compatible = "marvell,armada-8k-gpio",
		.data       = (void *) MVEBU_GPIO_SOC_VARIANT_A8K,
	},
916 917 918 919 920
	{
		/* sentinel */
	},
};

921 922 923 924 925
static int mvebu_gpio_suspend(struct platform_device *pdev, pm_message_t state)
{
	struct mvebu_gpio_chip *mvchip = platform_get_drvdata(pdev);
	int i;

926 927 928 929 930 931 932 933
	regmap_read(mvchip->regs, GPIO_OUT_OFF + mvchip->offset,
		    &mvchip->out_reg);
	regmap_read(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset,
		    &mvchip->io_conf_reg);
	regmap_read(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset,
		    &mvchip->blink_en_reg);
	regmap_read(mvchip->regs, GPIO_IN_POL_OFF + mvchip->offset,
		    &mvchip->in_pol_reg);
934 935 936

	switch (mvchip->soc_variant) {
	case MVEBU_GPIO_SOC_VARIANT_ORION:
937 938
	case MVEBU_GPIO_SOC_VARIANT_A8K:
		regmap_read(mvchip->regs, GPIO_EDGE_MASK_OFF + mvchip->offset,
939
			    &mvchip->edge_mask_regs[0]);
940
		regmap_read(mvchip->regs, GPIO_LEVEL_MASK_OFF + mvchip->offset,
941
			    &mvchip->level_mask_regs[0]);
942 943 944
		break;
	case MVEBU_GPIO_SOC_VARIANT_MV78200:
		for (i = 0; i < 2; i++) {
945 946 947 948 949 950
			regmap_read(mvchip->regs,
				    GPIO_EDGE_MASK_MV78200_OFF(i),
				    &mvchip->edge_mask_regs[i]);
			regmap_read(mvchip->regs,
				    GPIO_LEVEL_MASK_MV78200_OFF(i),
				    &mvchip->level_mask_regs[i]);
951 952 953 954
		}
		break;
	case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
		for (i = 0; i < 4; i++) {
955 956 957 958 959 960
			regmap_read(mvchip->regs,
				    GPIO_EDGE_MASK_ARMADAXP_OFF(i),
				    &mvchip->edge_mask_regs[i]);
			regmap_read(mvchip->regs,
				    GPIO_LEVEL_MASK_ARMADAXP_OFF(i),
				    &mvchip->level_mask_regs[i]);
961 962 963 964 965 966
		}
		break;
	default:
		BUG();
	}

967 968 969
	if (IS_ENABLED(CONFIG_PWM))
		mvebu_pwm_suspend(mvchip);

970 971 972 973 974 975 976 977
	return 0;
}

static int mvebu_gpio_resume(struct platform_device *pdev)
{
	struct mvebu_gpio_chip *mvchip = platform_get_drvdata(pdev);
	int i;

978 979 980 981 982 983 984 985
	regmap_write(mvchip->regs, GPIO_OUT_OFF + mvchip->offset,
		     mvchip->out_reg);
	regmap_write(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset,
		     mvchip->io_conf_reg);
	regmap_write(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset,
		     mvchip->blink_en_reg);
	regmap_write(mvchip->regs, GPIO_IN_POL_OFF + mvchip->offset,
		     mvchip->in_pol_reg);
986 987 988

	switch (mvchip->soc_variant) {
	case MVEBU_GPIO_SOC_VARIANT_ORION:
989 990
	case MVEBU_GPIO_SOC_VARIANT_A8K:
		regmap_write(mvchip->regs, GPIO_EDGE_MASK_OFF + mvchip->offset,
991
			     mvchip->edge_mask_regs[0]);
992
		regmap_write(mvchip->regs, GPIO_LEVEL_MASK_OFF + mvchip->offset,
993
			     mvchip->level_mask_regs[0]);
994 995 996
		break;
	case MVEBU_GPIO_SOC_VARIANT_MV78200:
		for (i = 0; i < 2; i++) {
997 998 999 1000 1001 1002
			regmap_write(mvchip->regs,
				     GPIO_EDGE_MASK_MV78200_OFF(i),
				     mvchip->edge_mask_regs[i]);
			regmap_write(mvchip->regs,
				     GPIO_LEVEL_MASK_MV78200_OFF(i),
				     mvchip->level_mask_regs[i]);
1003 1004 1005 1006
		}
		break;
	case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
		for (i = 0; i < 4; i++) {
1007 1008 1009 1010 1011 1012
			regmap_write(mvchip->regs,
				     GPIO_EDGE_MASK_ARMADAXP_OFF(i),
				     mvchip->edge_mask_regs[i]);
			regmap_write(mvchip->regs,
				     GPIO_LEVEL_MASK_ARMADAXP_OFF(i),
				     mvchip->level_mask_regs[i]);
1013 1014 1015 1016 1017 1018
		}
		break;
	default:
		BUG();
	}

1019 1020 1021
	if (IS_ENABLED(CONFIG_PWM))
		mvebu_pwm_resume(mvchip);

1022 1023 1024
	return 0;
}

1025 1026 1027 1028 1029 1030 1031
static const struct regmap_config mvebu_gpio_regmap_config = {
	.reg_bits = 32,
	.reg_stride = 4,
	.val_bits = 32,
	.fast_io = true,
};

1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
static int mvebu_gpio_probe_raw(struct platform_device *pdev,
				struct mvebu_gpio_chip *mvchip)
{
	struct resource *res;
	void __iomem *base;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	base = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(base))
		return PTR_ERR(base);

	mvchip->regs = devm_regmap_init_mmio(&pdev->dev, base,
					     &mvebu_gpio_regmap_config);
	if (IS_ERR(mvchip->regs))
		return PTR_ERR(mvchip->regs);

	/*
	 * For the legacy SoCs, the regmap directly maps to the GPIO
	 * registers, so no offset is needed.
	 */
	mvchip->offset = 0;

	/*
	 * The Armada XP has a second range of registers for the
	 * per-CPU registers
	 */
	if (mvchip->soc_variant == MVEBU_GPIO_SOC_VARIANT_ARMADAXP) {
		res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
		base = devm_ioremap_resource(&pdev->dev, res);
		if (IS_ERR(base))
			return PTR_ERR(base);

		mvchip->percpu_regs =
			devm_regmap_init_mmio(&pdev->dev, base,
					      &mvebu_gpio_regmap_config);
		if (IS_ERR(mvchip->percpu_regs))
			return PTR_ERR(mvchip->percpu_regs);
	}

	return 0;
}

static int mvebu_gpio_probe_syscon(struct platform_device *pdev,
				   struct mvebu_gpio_chip *mvchip)
{
	mvchip->regs = syscon_node_to_regmap(pdev->dev.parent->of_node);
	if (IS_ERR(mvchip->regs))
		return PTR_ERR(mvchip->regs);

	if (of_property_read_u32(pdev->dev.of_node, "offset", &mvchip->offset))
		return -EINVAL;

	return 0;
}

Bill Pemberton's avatar
Bill Pemberton committed
1087
static int mvebu_gpio_probe(struct platform_device *pdev)
1088 1089 1090 1091 1092 1093 1094
{
	struct mvebu_gpio_chip *mvchip;
	const struct of_device_id *match;
	struct device_node *np = pdev->dev.of_node;
	struct irq_chip_generic *gc;
	struct irq_chip_type *ct;
	unsigned int ngpios;
1095
	bool have_irqs;
1096 1097
	int soc_variant;
	int i, cpu, id;
1098
	int err;
1099 1100 1101

	match = of_match_device(mvebu_gpio_of_match, &pdev->dev);
	if (match)
1102
		soc_variant = (unsigned long) match->data;
1103 1104 1105
	else
		soc_variant = MVEBU_GPIO_SOC_VARIANT_ORION;

1106 1107 1108
	/* Some gpio controllers do not provide irq support */
	have_irqs = of_irq_count(np) != 0;

Andrew Lunn's avatar
Andrew Lunn committed
1109 1110
	mvchip = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_gpio_chip),
			      GFP_KERNEL);
1111
	if (!mvchip)
1112 1113
		return -ENOMEM;

1114 1115
	platform_set_drvdata(pdev, mvchip);

1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
	if (of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpios)) {
		dev_err(&pdev->dev, "Missing ngpios OF property\n");
		return -ENODEV;
	}

	id = of_alias_get_id(pdev->dev.of_node, "gpio");
	if (id < 0) {
		dev_err(&pdev->dev, "Couldn't get OF id\n");
		return id;
	}

1127
	mvchip->clk = devm_clk_get(&pdev->dev, NULL);
1128
	/* Not all SoCs require a clock.*/
1129 1130
	if (!IS_ERR(mvchip->clk))
		clk_prepare_enable(mvchip->clk);
1131

1132 1133
	mvchip->soc_variant = soc_variant;
	mvchip->chip.label = dev_name(&pdev->dev);
1134
	mvchip->chip.parent = &pdev->dev;
1135 1136
	mvchip->chip.request = gpiochip_generic_request;
	mvchip->chip.free = gpiochip_generic_free;
1137 1138 1139 1140
	mvchip->chip.direction_input = mvebu_gpio_direction_input;
	mvchip->chip.get = mvebu_gpio_get;
	mvchip->chip.direction_output = mvebu_gpio_direction_output;
	mvchip->chip.set = mvebu_gpio_set;
1141 1142
	if (have_irqs)
		mvchip->chip.to_irq = mvebu_gpio_to_irq;
1143 1144
	mvchip->chip.base = id * MVEBU_MAX_GPIO_PER_BANK;
	mvchip->chip.ngpio = ngpios;
1145
	mvchip->chip.can_sleep = false;
1146
	mvchip->chip.of_node = np;
1147
	mvchip->chip.dbg_show = mvebu_gpio_dbg_show;
1148

1149 1150 1151 1152
	if (soc_variant == MVEBU_GPIO_SOC_VARIANT_A8K)
		err = mvebu_gpio_probe_syscon(pdev, mvchip);
	else
		err = mvebu_gpio_probe_raw(pdev, mvchip);
1153

1154 1155
	if (err)
		return err;
1156 1157 1158 1159

	/*
	 * Mask and clear GPIO interrupts.
	 */
1160
	switch (soc_variant) {
1161
	case MVEBU_GPIO_SOC_VARIANT_ORION:
1162 1163 1164 1165 1166 1167 1168
	case MVEBU_GPIO_SOC_VARIANT_A8K:
		regmap_write(mvchip->regs,
			     GPIO_EDGE_CAUSE_OFF + mvchip->offset, 0);
		regmap_write(mvchip->regs,
			     GPIO_EDGE_MASK_OFF + mvchip->offset, 0);
		regmap_write(mvchip->regs,
			     GPIO_LEVEL_MASK_OFF + mvchip->offset, 0);
1169 1170
		break;
	case MVEBU_GPIO_SOC_VARIANT_MV78200:
1171
		regmap_write(mvchip->regs, GPIO_EDGE_CAUSE_OFF, 0);
1172
		for (cpu = 0; cpu < 2; cpu++) {
1173 1174 1175 1176
			regmap_write(mvchip->regs,
				     GPIO_EDGE_MASK_MV78200_OFF(cpu), 0);
			regmap_write(mvchip->regs,
				     GPIO_LEVEL_MASK_MV78200_OFF(cpu), 0);