asic3.c 19.7 KB
Newer Older
S
Samuel Ortiz 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * driver/mfd/asic3.c
 *
 * Compaq ASIC3 support.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Copyright 2001 Compaq Computer Corporation.
 * Copyright 2004-2005 Phil Blundell
S
Samuel Ortiz 已提交
12
 * Copyright 2007-2008 OpenedHand Ltd.
S
Samuel Ortiz 已提交
13 14 15 16 17 18 19
 *
 * Authors: Phil Blundell <pb@handhelds.org>,
 *	    Samuel Ortiz <sameo@openedhand.com>
 *
 */

#include <linux/kernel.h>
P
Philipp Zabel 已提交
20
#include <linux/delay.h>
S
Samuel Ortiz 已提交
21
#include <linux/irq.h>
S
Samuel Ortiz 已提交
22
#include <linux/gpio.h>
S
Samuel Ortiz 已提交
23 24 25 26 27
#include <linux/io.h>
#include <linux/spinlock.h>
#include <linux/platform_device.h>

#include <linux/mfd/asic3.h>
P
Philipp Zabel 已提交
28 29
#include <linux/mfd/core.h>
#include <linux/mfd/ds1wm.h>
S
Samuel Ortiz 已提交
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
enum {
	ASIC3_CLOCK_SPI,
	ASIC3_CLOCK_OWM,
	ASIC3_CLOCK_PWM0,
	ASIC3_CLOCK_PWM1,
	ASIC3_CLOCK_LED0,
	ASIC3_CLOCK_LED1,
	ASIC3_CLOCK_LED2,
	ASIC3_CLOCK_SD_HOST,
	ASIC3_CLOCK_SD_BUS,
	ASIC3_CLOCK_SMBUS,
	ASIC3_CLOCK_EX0,
	ASIC3_CLOCK_EX1,
};

struct asic3_clk {
	int enabled;
	unsigned int cdex;
	unsigned long rate;
};

#define INIT_CDEX(_name, _rate)	\
	[ASIC3_CLOCK_##_name] = {		\
		.cdex = CLOCK_CDEX_##_name,	\
		.rate = _rate,			\
	}

struct asic3_clk asic3_clk_init[] __initdata = {
	INIT_CDEX(SPI, 0),
	INIT_CDEX(OWM, 5000000),
	INIT_CDEX(PWM0, 0),
	INIT_CDEX(PWM1, 0),
	INIT_CDEX(LED0, 0),
	INIT_CDEX(LED1, 0),
	INIT_CDEX(LED2, 0),
	INIT_CDEX(SD_HOST, 24576000),
	INIT_CDEX(SD_BUS, 12288000),
	INIT_CDEX(SMBUS, 0),
	INIT_CDEX(EX0, 32768),
	INIT_CDEX(EX1, 24576000),
};

S
Samuel Ortiz 已提交
73 74 75 76 77 78 79 80 81
struct asic3 {
	void __iomem *mapping;
	unsigned int bus_shift;
	unsigned int irq_nr;
	unsigned int irq_base;
	spinlock_t lock;
	u16 irq_bothedge[4];
	struct gpio_chip gpio;
	struct device *dev;
82 83

	struct asic3_clk clocks[ARRAY_SIZE(asic3_clk_init)];
S
Samuel Ortiz 已提交
84 85 86 87
};

static int asic3_gpio_get(struct gpio_chip *chip, unsigned offset);

S
Samuel Ortiz 已提交
88 89 90
static inline void asic3_write_register(struct asic3 *asic,
				 unsigned int reg, u32 value)
{
91
	iowrite16(value, asic->mapping +
S
Samuel Ortiz 已提交
92 93 94 95 96 97
		  (reg >> asic->bus_shift));
}

static inline u32 asic3_read_register(struct asic3 *asic,
			       unsigned int reg)
{
98
	return ioread16(asic->mapping +
S
Samuel Ortiz 已提交
99 100 101
			(reg >> asic->bus_shift));
}

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
void asic3_set_register(struct asic3 *asic, u32 reg, u32 bits, bool set)
{
	unsigned long flags;
	u32 val;

	spin_lock_irqsave(&asic->lock, flags);
	val = asic3_read_register(asic, reg);
	if (set)
		val |= bits;
	else
		val &= ~bits;
	asic3_write_register(asic, reg, val);
	spin_unlock_irqrestore(&asic->lock, flags);
}

S
Samuel Ortiz 已提交
117 118
/* IRQs */
#define MAX_ASIC_ISR_LOOPS    20
119 120
#define ASIC3_GPIO_BASE_INCR \
	(ASIC3_GPIO_B_BASE - ASIC3_GPIO_A_BASE)
S
Samuel Ortiz 已提交
121 122 123 124 125 126 127 128 129

static void asic3_irq_flip_edge(struct asic3 *asic,
				u32 base, int bit)
{
	u16 edge;
	unsigned long flags;

	spin_lock_irqsave(&asic->lock, flags);
	edge = asic3_read_register(asic,
130
				   base + ASIC3_GPIO_EDGE_TRIGGER);
S
Samuel Ortiz 已提交
131 132
	edge ^= bit;
	asic3_write_register(asic,
133
			     base + ASIC3_GPIO_EDGE_TRIGGER, edge);
S
Samuel Ortiz 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	spin_unlock_irqrestore(&asic->lock, flags);
}

static void asic3_irq_demux(unsigned int irq, struct irq_desc *desc)
{
	int iter, i;
	unsigned long flags;
	struct asic3 *asic;

	desc->chip->ack(irq);

	asic = desc->handler_data;

	for (iter = 0 ; iter < MAX_ASIC_ISR_LOOPS; iter++) {
		u32 status;
		int bank;

		spin_lock_irqsave(&asic->lock, flags);
		status = asic3_read_register(asic,
153
					     ASIC3_OFFSET(INTR, P_INT_STAT));
S
Samuel Ortiz 已提交
154 155 156 157 158 159 160 161 162 163 164
		spin_unlock_irqrestore(&asic->lock, flags);

		/* Check all ten register bits */
		if ((status & 0x3ff) == 0)
			break;

		/* Handle GPIO IRQs */
		for (bank = 0; bank < ASIC3_NUM_GPIO_BANKS; bank++) {
			if (status & (1 << bank)) {
				unsigned long base, istat;

165 166
				base = ASIC3_GPIO_A_BASE
				       + bank * ASIC3_GPIO_BASE_INCR;
S
Samuel Ortiz 已提交
167 168 169 170

				spin_lock_irqsave(&asic->lock, flags);
				istat = asic3_read_register(asic,
							    base +
171
							    ASIC3_GPIO_INT_STATUS);
S
Samuel Ortiz 已提交
172 173 174
				/* Clearing IntStatus */
				asic3_write_register(asic,
						     base +
175
						     ASIC3_GPIO_INT_STATUS, 0);
S
Samuel Ortiz 已提交
176 177 178 179 180 181 182 183 184 185 186 187
				spin_unlock_irqrestore(&asic->lock, flags);

				for (i = 0; i < ASIC3_GPIOS_PER_BANK; i++) {
					int bit = (1 << i);
					unsigned int irqnr;

					if (!(istat & bit))
						continue;

					irqnr = asic->irq_base +
						(ASIC3_GPIOS_PER_BANK * bank)
						+ i;
188
					desc = irq_to_desc(irqnr);
S
Samuel Ortiz 已提交
189 190 191 192 193 194 195 196 197 198 199 200
					desc->handle_irq(irqnr, desc);
					if (asic->irq_bothedge[bank] & bit)
						asic3_irq_flip_edge(asic, base,
								    bit);
				}
			}
		}

		/* Handle remaining IRQs in the status register */
		for (i = ASIC3_NUM_GPIOS; i < ASIC3_NR_IRQS; i++) {
			/* They start at bit 4 and go up */
			if (status & (1 << (i - ASIC3_NUM_GPIOS + 4))) {
201
				desc = irq_to_desc(asic->irq_base + i);
S
Samuel Ortiz 已提交
202 203 204 205 206 207 208
				desc->handle_irq(asic->irq_base + i,
						 desc);
			}
		}
	}

	if (iter >= MAX_ASIC_ISR_LOOPS)
209
		dev_err(asic->dev, "interrupt processing overrun\n");
S
Samuel Ortiz 已提交
210 211 212 213 214 215 216 217
}

static inline int asic3_irq_to_bank(struct asic3 *asic, int irq)
{
	int n;

	n = (irq - asic->irq_base) >> 4;

218
	return (n * (ASIC3_GPIO_B_BASE - ASIC3_GPIO_A_BASE));
S
Samuel Ortiz 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
}

static inline int asic3_irq_to_index(struct asic3 *asic, int irq)
{
	return (irq - asic->irq_base) & 0xf;
}

static void asic3_mask_gpio_irq(unsigned int irq)
{
	struct asic3 *asic = get_irq_chip_data(irq);
	u32 val, bank, index;
	unsigned long flags;

	bank = asic3_irq_to_bank(asic, irq);
	index = asic3_irq_to_index(asic, irq);

	spin_lock_irqsave(&asic->lock, flags);
236
	val = asic3_read_register(asic, bank + ASIC3_GPIO_MASK);
S
Samuel Ortiz 已提交
237
	val |= 1 << index;
238
	asic3_write_register(asic, bank + ASIC3_GPIO_MASK, val);
S
Samuel Ortiz 已提交
239 240 241 242 243 244 245 246 247 248 249
	spin_unlock_irqrestore(&asic->lock, flags);
}

static void asic3_mask_irq(unsigned int irq)
{
	struct asic3 *asic = get_irq_chip_data(irq);
	int regval;
	unsigned long flags;

	spin_lock_irqsave(&asic->lock, flags);
	regval = asic3_read_register(asic,
250 251
				     ASIC3_INTR_BASE +
				     ASIC3_INTR_INT_MASK);
S
Samuel Ortiz 已提交
252 253 254 255 256

	regval &= ~(ASIC3_INTMASK_MASK0 <<
		    (irq - (asic->irq_base + ASIC3_NUM_GPIOS)));

	asic3_write_register(asic,
257 258
			     ASIC3_INTR_BASE +
			     ASIC3_INTR_INT_MASK,
S
Samuel Ortiz 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272
			     regval);
	spin_unlock_irqrestore(&asic->lock, flags);
}

static void asic3_unmask_gpio_irq(unsigned int irq)
{
	struct asic3 *asic = get_irq_chip_data(irq);
	u32 val, bank, index;
	unsigned long flags;

	bank = asic3_irq_to_bank(asic, irq);
	index = asic3_irq_to_index(asic, irq);

	spin_lock_irqsave(&asic->lock, flags);
273
	val = asic3_read_register(asic, bank + ASIC3_GPIO_MASK);
S
Samuel Ortiz 已提交
274
	val &= ~(1 << index);
275
	asic3_write_register(asic, bank + ASIC3_GPIO_MASK, val);
S
Samuel Ortiz 已提交
276 277 278 279 280 281 282 283 284 285 286
	spin_unlock_irqrestore(&asic->lock, flags);
}

static void asic3_unmask_irq(unsigned int irq)
{
	struct asic3 *asic = get_irq_chip_data(irq);
	int regval;
	unsigned long flags;

	spin_lock_irqsave(&asic->lock, flags);
	regval = asic3_read_register(asic,
287 288
				     ASIC3_INTR_BASE +
				     ASIC3_INTR_INT_MASK);
S
Samuel Ortiz 已提交
289 290 291 292 293

	regval |= (ASIC3_INTMASK_MASK0 <<
		   (irq - (asic->irq_base + ASIC3_NUM_GPIOS)));

	asic3_write_register(asic,
294 295
			     ASIC3_INTR_BASE +
			     ASIC3_INTR_INT_MASK,
S
Samuel Ortiz 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
			     regval);
	spin_unlock_irqrestore(&asic->lock, flags);
}

static int asic3_gpio_irq_type(unsigned int irq, unsigned int type)
{
	struct asic3 *asic = get_irq_chip_data(irq);
	u32 bank, index;
	u16 trigger, level, edge, bit;
	unsigned long flags;

	bank = asic3_irq_to_bank(asic, irq);
	index = asic3_irq_to_index(asic, irq);
	bit = 1<<index;

	spin_lock_irqsave(&asic->lock, flags);
	level = asic3_read_register(asic,
313
				    bank + ASIC3_GPIO_LEVEL_TRIGGER);
S
Samuel Ortiz 已提交
314
	edge = asic3_read_register(asic,
315
				   bank + ASIC3_GPIO_EDGE_TRIGGER);
S
Samuel Ortiz 已提交
316
	trigger = asic3_read_register(asic,
317
				      bank + ASIC3_GPIO_TRIGGER_TYPE);
S
Samuel Ortiz 已提交
318 319
	asic->irq_bothedge[(irq - asic->irq_base) >> 4] &= ~bit;

320
	if (type == IRQ_TYPE_EDGE_RISING) {
S
Samuel Ortiz 已提交
321 322
		trigger |= bit;
		edge |= bit;
323
	} else if (type == IRQ_TYPE_EDGE_FALLING) {
S
Samuel Ortiz 已提交
324 325
		trigger |= bit;
		edge &= ~bit;
326
	} else if (type == IRQ_TYPE_EDGE_BOTH) {
S
Samuel Ortiz 已提交
327
		trigger |= bit;
S
Samuel Ortiz 已提交
328
		if (asic3_gpio_get(&asic->gpio, irq - asic->irq_base))
S
Samuel Ortiz 已提交
329 330 331 332
			edge &= ~bit;
		else
			edge |= bit;
		asic->irq_bothedge[(irq - asic->irq_base) >> 4] |= bit;
333
	} else if (type == IRQ_TYPE_LEVEL_LOW) {
S
Samuel Ortiz 已提交
334 335
		trigger &= ~bit;
		level &= ~bit;
336
	} else if (type == IRQ_TYPE_LEVEL_HIGH) {
S
Samuel Ortiz 已提交
337 338 339 340
		trigger &= ~bit;
		level |= bit;
	} else {
		/*
341
		 * if type == IRQ_TYPE_NONE, we should mask interrupts, but
S
Samuel Ortiz 已提交
342 343 344
		 * be careful to not unmask them if mask was also called.
		 * Probably need internal state for mask.
		 */
345
		dev_notice(asic->dev, "irq type not changed\n");
S
Samuel Ortiz 已提交
346
	}
347
	asic3_write_register(asic, bank + ASIC3_GPIO_LEVEL_TRIGGER,
S
Samuel Ortiz 已提交
348
			     level);
349
	asic3_write_register(asic, bank + ASIC3_GPIO_EDGE_TRIGGER,
S
Samuel Ortiz 已提交
350
			     edge);
351
	asic3_write_register(asic, bank + ASIC3_GPIO_TRIGGER_TYPE,
S
Samuel Ortiz 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
			     trigger);
	spin_unlock_irqrestore(&asic->lock, flags);
	return 0;
}

static struct irq_chip asic3_gpio_irq_chip = {
	.name		= "ASIC3-GPIO",
	.ack		= asic3_mask_gpio_irq,
	.mask		= asic3_mask_gpio_irq,
	.unmask		= asic3_unmask_gpio_irq,
	.set_type	= asic3_gpio_irq_type,
};

static struct irq_chip asic3_irq_chip = {
	.name		= "ASIC3",
	.ack		= asic3_mask_irq,
	.mask		= asic3_mask_irq,
	.unmask		= asic3_unmask_irq,
};

372
static int __init asic3_irq_probe(struct platform_device *pdev)
S
Samuel Ortiz 已提交
373 374 375 376
{
	struct asic3 *asic = platform_get_drvdata(pdev);
	unsigned long clksel = 0;
	unsigned int irq, irq_base;
377
	int ret;
S
Samuel Ortiz 已提交
378

379 380 381 382
	ret = platform_get_irq(pdev, 0);
	if (ret < 0)
		return ret;
	asic->irq_nr = ret;
S
Samuel Ortiz 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401

	/* turn on clock to IRQ controller */
	clksel |= CLOCK_SEL_CX;
	asic3_write_register(asic, ASIC3_OFFSET(CLOCK, SEL),
			     clksel);

	irq_base = asic->irq_base;

	for (irq = irq_base; irq < irq_base + ASIC3_NR_IRQS; irq++) {
		if (irq < asic->irq_base + ASIC3_NUM_GPIOS)
			set_irq_chip(irq, &asic3_gpio_irq_chip);
		else
			set_irq_chip(irq, &asic3_irq_chip);

		set_irq_chip_data(irq, asic);
		set_irq_handler(irq, handle_level_irq);
		set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
	}

402
	asic3_write_register(asic, ASIC3_OFFSET(INTR, INT_MASK),
S
Samuel Ortiz 已提交
403 404 405
			     ASIC3_INTMASK_GINTMASK);

	set_irq_chained_handler(asic->irq_nr, asic3_irq_demux);
406
	set_irq_type(asic->irq_nr, IRQ_TYPE_EDGE_RISING);
S
Samuel Ortiz 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
	set_irq_data(asic->irq_nr, asic);

	return 0;
}

static void asic3_irq_remove(struct platform_device *pdev)
{
	struct asic3 *asic = platform_get_drvdata(pdev);
	unsigned int irq, irq_base;

	irq_base = asic->irq_base;

	for (irq = irq_base; irq < irq_base + ASIC3_NR_IRQS; irq++) {
		set_irq_flags(irq, 0);
		set_irq_handler(irq, NULL);
		set_irq_chip(irq, NULL);
		set_irq_chip_data(irq, NULL);
	}
	set_irq_chained_handler(asic->irq_nr, NULL);
}

/* GPIOs */
S
Samuel Ortiz 已提交
429 430 431 432 433 434 435 436 437 438 439
static int asic3_gpio_direction(struct gpio_chip *chip,
				unsigned offset, int out)
{
	u32 mask = ASIC3_GPIO_TO_MASK(offset), out_reg;
	unsigned int gpio_base;
	unsigned long flags;
	struct asic3 *asic;

	asic = container_of(chip, struct asic3, gpio);
	gpio_base = ASIC3_GPIO_TO_BASE(offset);

440
	if (gpio_base > ASIC3_GPIO_D_BASE) {
441 442
		dev_err(asic->dev, "Invalid base (0x%x) for gpio %d\n",
			gpio_base, offset);
S
Samuel Ortiz 已提交
443 444 445 446 447
		return -EINVAL;
	}

	spin_lock_irqsave(&asic->lock, flags);

448
	out_reg = asic3_read_register(asic, gpio_base + ASIC3_GPIO_DIRECTION);
S
Samuel Ortiz 已提交
449 450 451 452 453 454 455

	/* Input is 0, Output is 1 */
	if (out)
		out_reg |= mask;
	else
		out_reg &= ~mask;

456
	asic3_write_register(asic, gpio_base + ASIC3_GPIO_DIRECTION, out_reg);
S
Samuel Ortiz 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485

	spin_unlock_irqrestore(&asic->lock, flags);

	return 0;

}

static int asic3_gpio_direction_input(struct gpio_chip *chip,
				      unsigned offset)
{
	return asic3_gpio_direction(chip, offset, 0);
}

static int asic3_gpio_direction_output(struct gpio_chip *chip,
				       unsigned offset, int value)
{
	return asic3_gpio_direction(chip, offset, 1);
}

static int asic3_gpio_get(struct gpio_chip *chip,
			  unsigned offset)
{
	unsigned int gpio_base;
	u32 mask = ASIC3_GPIO_TO_MASK(offset);
	struct asic3 *asic;

	asic = container_of(chip, struct asic3, gpio);
	gpio_base = ASIC3_GPIO_TO_BASE(offset);

486
	if (gpio_base > ASIC3_GPIO_D_BASE) {
487 488
		dev_err(asic->dev, "Invalid base (0x%x) for gpio %d\n",
			gpio_base, offset);
S
Samuel Ortiz 已提交
489 490 491
		return -EINVAL;
	}

492
	return asic3_read_register(asic, gpio_base + ASIC3_GPIO_STATUS) & mask;
S
Samuel Ortiz 已提交
493 494 495 496 497 498 499 500 501 502 503 504 505
}

static void asic3_gpio_set(struct gpio_chip *chip,
			   unsigned offset, int value)
{
	u32 mask, out_reg;
	unsigned int gpio_base;
	unsigned long flags;
	struct asic3 *asic;

	asic = container_of(chip, struct asic3, gpio);
	gpio_base = ASIC3_GPIO_TO_BASE(offset);

506
	if (gpio_base > ASIC3_GPIO_D_BASE) {
507 508
		dev_err(asic->dev, "Invalid base (0x%x) for gpio %d\n",
			gpio_base, offset);
S
Samuel Ortiz 已提交
509 510 511 512 513 514 515
		return;
	}

	mask = ASIC3_GPIO_TO_MASK(offset);

	spin_lock_irqsave(&asic->lock, flags);

516
	out_reg = asic3_read_register(asic, gpio_base + ASIC3_GPIO_OUT);
S
Samuel Ortiz 已提交
517 518 519 520 521 522

	if (value)
		out_reg |= mask;
	else
		out_reg &= ~mask;

523
	asic3_write_register(asic, gpio_base + ASIC3_GPIO_OUT, out_reg);
S
Samuel Ortiz 已提交
524 525 526 527 528 529

	spin_unlock_irqrestore(&asic->lock, flags);

	return;
}

530 531
static __init int asic3_gpio_probe(struct platform_device *pdev,
				   u16 *gpio_config, int num)
S
Samuel Ortiz 已提交
532 533
{
	struct asic3 *asic = platform_get_drvdata(pdev);
534 535 536 537
	u16 alt_reg[ASIC3_NUM_GPIO_BANKS];
	u16 out_reg[ASIC3_NUM_GPIO_BANKS];
	u16 dir_reg[ASIC3_NUM_GPIO_BANKS];
	int i;
S
Samuel Ortiz 已提交
538

R
Russell King 已提交
539 540 541
	memset(alt_reg, 0, ASIC3_NUM_GPIO_BANKS * sizeof(u16));
	memset(out_reg, 0, ASIC3_NUM_GPIO_BANKS * sizeof(u16));
	memset(dir_reg, 0, ASIC3_NUM_GPIO_BANKS * sizeof(u16));
542 543

	/* Enable all GPIOs */
544 545 546 547
	asic3_write_register(asic, ASIC3_GPIO_OFFSET(A, MASK), 0xffff);
	asic3_write_register(asic, ASIC3_GPIO_OFFSET(B, MASK), 0xffff);
	asic3_write_register(asic, ASIC3_GPIO_OFFSET(C, MASK), 0xffff);
	asic3_write_register(asic, ASIC3_GPIO_OFFSET(D, MASK), 0xffff);
S
Samuel Ortiz 已提交
548

549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
	for (i = 0; i < num; i++) {
		u8 alt, pin, dir, init, bank_num, bit_num;
		u16 config = gpio_config[i];

		pin = ASIC3_CONFIG_GPIO_PIN(config);
		alt = ASIC3_CONFIG_GPIO_ALT(config);
		dir = ASIC3_CONFIG_GPIO_DIR(config);
		init = ASIC3_CONFIG_GPIO_INIT(config);

		bank_num = ASIC3_GPIO_TO_BANK(pin);
		bit_num = ASIC3_GPIO_TO_BIT(pin);

		alt_reg[bank_num] |= (alt << bit_num);
		out_reg[bank_num] |= (init << bit_num);
		dir_reg[bank_num] |= (dir << bit_num);
	}

	for (i = 0; i < ASIC3_NUM_GPIO_BANKS; i++) {
		asic3_write_register(asic,
				     ASIC3_BANK_TO_BASE(i) +
569
				     ASIC3_GPIO_DIRECTION,
570 571
				     dir_reg[i]);
		asic3_write_register(asic,
572
				     ASIC3_BANK_TO_BASE(i) + ASIC3_GPIO_OUT,
573 574 575
				     out_reg[i]);
		asic3_write_register(asic,
				     ASIC3_BANK_TO_BASE(i) +
576
				     ASIC3_GPIO_ALT_FUNCTION,
577
				     alt_reg[i]);
S
Samuel Ortiz 已提交
578 579
	}

S
Samuel Ortiz 已提交
580
	return gpiochip_add(&asic->gpio);
S
Samuel Ortiz 已提交
581 582
}

S
Samuel Ortiz 已提交
583
static int asic3_gpio_remove(struct platform_device *pdev)
S
Samuel Ortiz 已提交
584
{
S
Samuel Ortiz 已提交
585 586 587
	struct asic3 *asic = platform_get_drvdata(pdev);

	return gpiochip_remove(&asic->gpio);
S
Samuel Ortiz 已提交
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
static int asic3_clk_enable(struct asic3 *asic, struct asic3_clk *clk)
{
	unsigned long flags;
	u32 cdex;

	spin_lock_irqsave(&asic->lock, flags);
	if (clk->enabled++ == 0) {
		cdex = asic3_read_register(asic, ASIC3_OFFSET(CLOCK, CDEX));
		cdex |= clk->cdex;
		asic3_write_register(asic, ASIC3_OFFSET(CLOCK, CDEX), cdex);
	}
	spin_unlock_irqrestore(&asic->lock, flags);

	return 0;
}

static void asic3_clk_disable(struct asic3 *asic, struct asic3_clk *clk)
{
	unsigned long flags;
	u32 cdex;

	WARN_ON(clk->enabled == 0);

	spin_lock_irqsave(&asic->lock, flags);
	if (--clk->enabled == 0) {
		cdex = asic3_read_register(asic, ASIC3_OFFSET(CLOCK, CDEX));
		cdex &= ~clk->cdex;
		asic3_write_register(asic, ASIC3_OFFSET(CLOCK, CDEX), cdex);
	}
	spin_unlock_irqrestore(&asic->lock, flags);
}
S
Samuel Ortiz 已提交
621

P
Philipp Zabel 已提交
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 684 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
/* MFD cells (SPI, PWM, LED, DS1WM, MMC) */
static struct ds1wm_driver_data ds1wm_pdata = {
	.active_high = 1,
};

static struct resource ds1wm_resources[] = {
	{
		.start = ASIC3_OWM_BASE,
		.end   = ASIC3_OWM_BASE + 0x13,
		.flags = IORESOURCE_MEM,
	},
	{
		.start = ASIC3_IRQ_OWM,
		.start = ASIC3_IRQ_OWM,
		.flags = IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHEDGE,
	},
};

static int ds1wm_enable(struct platform_device *pdev)
{
	struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);

	/* Turn on external clocks and the OWM clock */
	asic3_clk_enable(asic, &asic->clocks[ASIC3_CLOCK_EX0]);
	asic3_clk_enable(asic, &asic->clocks[ASIC3_CLOCK_EX1]);
	asic3_clk_enable(asic, &asic->clocks[ASIC3_CLOCK_OWM]);
	msleep(1);

	/* Reset and enable DS1WM */
	asic3_set_register(asic, ASIC3_OFFSET(EXTCF, RESET),
			   ASIC3_EXTCF_OWM_RESET, 1);
	msleep(1);
	asic3_set_register(asic, ASIC3_OFFSET(EXTCF, RESET),
			   ASIC3_EXTCF_OWM_RESET, 0);
	msleep(1);
	asic3_set_register(asic, ASIC3_OFFSET(EXTCF, SELECT),
			   ASIC3_EXTCF_OWM_EN, 1);
	msleep(1);

	return 0;
}

static int ds1wm_disable(struct platform_device *pdev)
{
	struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);

	asic3_set_register(asic, ASIC3_OFFSET(EXTCF, SELECT),
			   ASIC3_EXTCF_OWM_EN, 0);

	asic3_clk_disable(asic, &asic->clocks[ASIC3_CLOCK_OWM]);
	asic3_clk_disable(asic, &asic->clocks[ASIC3_CLOCK_EX0]);
	asic3_clk_disable(asic, &asic->clocks[ASIC3_CLOCK_EX1]);

	return 0;
}

static struct mfd_cell asic3_cell_ds1wm = {
	.name          = "ds1wm",
	.enable        = ds1wm_enable,
	.disable       = ds1wm_disable,
	.driver_data   = &ds1wm_pdata,
	.num_resources = ARRAY_SIZE(ds1wm_resources),
	.resources     = ds1wm_resources,
};

static int __init asic3_mfd_probe(struct platform_device *pdev,
				  struct resource *mem)
{
	struct asic3 *asic = platform_get_drvdata(pdev);
	int ret;

	/* DS1WM */
	asic3_set_register(asic, ASIC3_OFFSET(EXTCF, SELECT),
			   ASIC3_EXTCF_OWM_SMB, 0);

	ds1wm_resources[0].start >>= asic->bus_shift;
	ds1wm_resources[0].end   >>= asic->bus_shift;

	asic3_cell_ds1wm.platform_data = &asic3_cell_ds1wm;
	asic3_cell_ds1wm.data_size = sizeof(asic3_cell_ds1wm);

	ret = mfd_add_devices(&pdev->dev, pdev->id,
			&asic3_cell_ds1wm, 1, mem, asic->irq_base);

	return ret;
}

static void asic3_mfd_remove(struct platform_device *pdev)
{
	mfd_remove_devices(&pdev->dev);
}

S
Samuel Ortiz 已提交
714
/* Core */
715
static int __init asic3_probe(struct platform_device *pdev)
S
Samuel Ortiz 已提交
716 717 718 719 720
{
	struct asic3_platform_data *pdata = pdev->dev.platform_data;
	struct asic3 *asic;
	struct resource *mem;
	unsigned long clksel;
S
Samuel Ortiz 已提交
721
	int ret = 0;
S
Samuel Ortiz 已提交
722 723

	asic = kzalloc(sizeof(struct asic3), GFP_KERNEL);
S
Samuel Ortiz 已提交
724 725
	if (asic == NULL) {
		printk(KERN_ERR "kzalloc failed\n");
S
Samuel Ortiz 已提交
726
		return -ENOMEM;
S
Samuel Ortiz 已提交
727
	}
S
Samuel Ortiz 已提交
728 729 730 731 732 733 734 735

	spin_lock_init(&asic->lock);
	platform_set_drvdata(pdev, asic);
	asic->dev = &pdev->dev;

	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!mem) {
		ret = -ENOMEM;
736
		dev_err(asic->dev, "no MEM resource\n");
S
Samuel Ortiz 已提交
737
		goto out_free;
S
Samuel Ortiz 已提交
738 739
	}

740
	asic->mapping = ioremap(mem->start, resource_size(mem));
S
Samuel Ortiz 已提交
741 742
	if (!asic->mapping) {
		ret = -ENOMEM;
743
		dev_err(asic->dev, "Couldn't ioremap\n");
S
Samuel Ortiz 已提交
744
		goto out_free;
S
Samuel Ortiz 已提交
745 746 747 748
	}

	asic->irq_base = pdata->irq_base;

749
	/* calculate bus shift from mem resource */
750
	asic->bus_shift = 2 - (resource_size(mem) >> 12);
S
Samuel Ortiz 已提交
751 752 753 754 755 756

	clksel = 0;
	asic3_write_register(asic, ASIC3_OFFSET(CLOCK, SEL), clksel);

	ret = asic3_irq_probe(pdev);
	if (ret < 0) {
757
		dev_err(asic->dev, "Couldn't probe IRQs\n");
S
Samuel Ortiz 已提交
758 759 760 761 762 763 764 765 766 767
		goto out_unmap;
	}

	asic->gpio.base = pdata->gpio_base;
	asic->gpio.ngpio = ASIC3_NUM_GPIOS;
	asic->gpio.get = asic3_gpio_get;
	asic->gpio.set = asic3_gpio_set;
	asic->gpio.direction_input = asic3_gpio_direction_input;
	asic->gpio.direction_output = asic3_gpio_direction_output;

768 769 770
	ret = asic3_gpio_probe(pdev,
			       pdata->gpio_config,
			       pdata->gpio_config_num);
S
Samuel Ortiz 已提交
771
	if (ret < 0) {
772
		dev_err(asic->dev, "GPIO probe failed\n");
S
Samuel Ortiz 已提交
773
		goto out_irq;
S
Samuel Ortiz 已提交
774 775
	}

776 777 778 779 780
	/* Making a per-device copy is only needed for the
	 * theoretical case of multiple ASIC3s on one board:
	 */
	memcpy(asic->clocks, asic3_clk_init, sizeof(asic3_clk_init));

P
Philipp Zabel 已提交
781 782
	asic3_mfd_probe(pdev, mem);

783
	dev_info(asic->dev, "ASIC3 Core driver\n");
S
Samuel Ortiz 已提交
784 785 786

	return 0;

S
Samuel Ortiz 已提交
787 788 789 790
 out_irq:
	asic3_irq_remove(pdev);

 out_unmap:
S
Samuel Ortiz 已提交
791
	iounmap(asic->mapping);
S
Samuel Ortiz 已提交
792 793

 out_free:
S
Samuel Ortiz 已提交
794 795 796 797 798 799 800
	kfree(asic);

	return ret;
}

static int asic3_remove(struct platform_device *pdev)
{
S
Samuel Ortiz 已提交
801
	int ret;
S
Samuel Ortiz 已提交
802 803
	struct asic3 *asic = platform_get_drvdata(pdev);

P
Philipp Zabel 已提交
804 805
	asic3_mfd_remove(pdev);

S
Samuel Ortiz 已提交
806 807 808
	ret = asic3_gpio_remove(pdev);
	if (ret < 0)
		return ret;
S
Samuel Ortiz 已提交
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
	asic3_irq_remove(pdev);

	asic3_write_register(asic, ASIC3_OFFSET(CLOCK, SEL), 0);

	iounmap(asic->mapping);

	kfree(asic);

	return 0;
}

static void asic3_shutdown(struct platform_device *pdev)
{
}

static struct platform_driver asic3_device_driver = {
	.driver		= {
		.name	= "asic3",
	},
	.remove		= __devexit_p(asic3_remove),
	.shutdown	= asic3_shutdown,
};

static int __init asic3_init(void)
{
	int retval = 0;
835
	retval = platform_driver_probe(&asic3_device_driver, asic3_probe);
S
Samuel Ortiz 已提交
836 837 838 839
	return retval;
}

subsys_initcall(asic3_init);