asic3.c 15.6 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 20
 *
 * Authors: Phil Blundell <pb@handhelds.org>,
 *	    Samuel Ortiz <sameo@openedhand.com>
 *
 */

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

#include <linux/mfd/asic3.h>

S
Samuel Ortiz 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40
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;
};

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

S
Samuel Ortiz 已提交
41 42 43
static inline void asic3_write_register(struct asic3 *asic,
				 unsigned int reg, u32 value)
{
44
	iowrite16(value, asic->mapping +
S
Samuel Ortiz 已提交
45 46 47 48 49 50
		  (reg >> asic->bus_shift));
}

static inline u32 asic3_read_register(struct asic3 *asic,
			       unsigned int reg)
{
51
	return ioread16(asic->mapping +
S
Samuel Ortiz 已提交
52 53 54
			(reg >> asic->bus_shift));
}

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
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 已提交
70 71
/* IRQs */
#define MAX_ASIC_ISR_LOOPS    20
72 73
#define ASIC3_GPIO_BASE_INCR \
	(ASIC3_GPIO_B_BASE - ASIC3_GPIO_A_BASE)
S
Samuel Ortiz 已提交
74 75 76 77 78 79 80 81 82

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,
83
				   base + ASIC3_GPIO_EDGE_TRIGGER);
S
Samuel Ortiz 已提交
84 85
	edge ^= bit;
	asic3_write_register(asic,
86
			     base + ASIC3_GPIO_EDGE_TRIGGER, edge);
S
Samuel Ortiz 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
	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,
106
					     ASIC3_OFFSET(INTR, P_INT_STAT));
S
Samuel Ortiz 已提交
107 108 109 110 111 112 113 114 115 116 117
		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;

118 119
				base = ASIC3_GPIO_A_BASE
				       + bank * ASIC3_GPIO_BASE_INCR;
S
Samuel Ortiz 已提交
120 121 122 123

				spin_lock_irqsave(&asic->lock, flags);
				istat = asic3_read_register(asic,
							    base +
124
							    ASIC3_GPIO_INT_STATUS);
S
Samuel Ortiz 已提交
125 126 127
				/* Clearing IntStatus */
				asic3_write_register(asic,
						     base +
128
						     ASIC3_GPIO_INT_STATUS, 0);
S
Samuel Ortiz 已提交
129 130 131 132 133 134 135 136 137 138 139 140
				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;
141
					desc = irq_to_desc(irqnr);
S
Samuel Ortiz 已提交
142 143 144 145 146 147 148 149 150 151 152 153
					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))) {
154
				desc = irq_to_desc(asic->irq_base + i);
S
Samuel Ortiz 已提交
155 156 157 158 159 160 161
				desc->handle_irq(asic->irq_base + i,
						 desc);
			}
		}
	}

	if (iter >= MAX_ASIC_ISR_LOOPS)
162
		dev_err(asic->dev, "interrupt processing overrun\n");
S
Samuel Ortiz 已提交
163 164 165 166 167 168 169 170
}

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

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

171
	return (n * (ASIC3_GPIO_B_BASE - ASIC3_GPIO_A_BASE));
S
Samuel Ortiz 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
}

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);
189
	val = asic3_read_register(asic, bank + ASIC3_GPIO_MASK);
S
Samuel Ortiz 已提交
190
	val |= 1 << index;
191
	asic3_write_register(asic, bank + ASIC3_GPIO_MASK, val);
S
Samuel Ortiz 已提交
192 193 194 195 196 197 198 199 200 201 202
	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,
203 204
				     ASIC3_INTR_BASE +
				     ASIC3_INTR_INT_MASK);
S
Samuel Ortiz 已提交
205 206 207 208 209

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

	asic3_write_register(asic,
210 211
			     ASIC3_INTR_BASE +
			     ASIC3_INTR_INT_MASK,
S
Samuel Ortiz 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225
			     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);
226
	val = asic3_read_register(asic, bank + ASIC3_GPIO_MASK);
S
Samuel Ortiz 已提交
227
	val &= ~(1 << index);
228
	asic3_write_register(asic, bank + ASIC3_GPIO_MASK, val);
S
Samuel Ortiz 已提交
229 230 231 232 233 234 235 236 237 238 239
	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,
240 241
				     ASIC3_INTR_BASE +
				     ASIC3_INTR_INT_MASK);
S
Samuel Ortiz 已提交
242 243 244 245 246

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

	asic3_write_register(asic,
247 248
			     ASIC3_INTR_BASE +
			     ASIC3_INTR_INT_MASK,
S
Samuel Ortiz 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
			     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,
266
				    bank + ASIC3_GPIO_LEVEL_TRIGGER);
S
Samuel Ortiz 已提交
267
	edge = asic3_read_register(asic,
268
				   bank + ASIC3_GPIO_EDGE_TRIGGER);
S
Samuel Ortiz 已提交
269
	trigger = asic3_read_register(asic,
270
				      bank + ASIC3_GPIO_TRIGGER_TYPE);
S
Samuel Ortiz 已提交
271 272
	asic->irq_bothedge[(irq - asic->irq_base) >> 4] &= ~bit;

273
	if (type == IRQ_TYPE_EDGE_RISING) {
S
Samuel Ortiz 已提交
274 275
		trigger |= bit;
		edge |= bit;
276
	} else if (type == IRQ_TYPE_EDGE_FALLING) {
S
Samuel Ortiz 已提交
277 278
		trigger |= bit;
		edge &= ~bit;
279
	} else if (type == IRQ_TYPE_EDGE_BOTH) {
S
Samuel Ortiz 已提交
280
		trigger |= bit;
S
Samuel Ortiz 已提交
281
		if (asic3_gpio_get(&asic->gpio, irq - asic->irq_base))
S
Samuel Ortiz 已提交
282 283 284 285
			edge &= ~bit;
		else
			edge |= bit;
		asic->irq_bothedge[(irq - asic->irq_base) >> 4] |= bit;
286
	} else if (type == IRQ_TYPE_LEVEL_LOW) {
S
Samuel Ortiz 已提交
287 288
		trigger &= ~bit;
		level &= ~bit;
289
	} else if (type == IRQ_TYPE_LEVEL_HIGH) {
S
Samuel Ortiz 已提交
290 291 292 293
		trigger &= ~bit;
		level |= bit;
	} else {
		/*
294
		 * if type == IRQ_TYPE_NONE, we should mask interrupts, but
S
Samuel Ortiz 已提交
295 296 297
		 * be careful to not unmask them if mask was also called.
		 * Probably need internal state for mask.
		 */
298
		dev_notice(asic->dev, "irq type not changed\n");
S
Samuel Ortiz 已提交
299
	}
300
	asic3_write_register(asic, bank + ASIC3_GPIO_LEVEL_TRIGGER,
S
Samuel Ortiz 已提交
301
			     level);
302
	asic3_write_register(asic, bank + ASIC3_GPIO_EDGE_TRIGGER,
S
Samuel Ortiz 已提交
303
			     edge);
304
	asic3_write_register(asic, bank + ASIC3_GPIO_TRIGGER_TYPE,
S
Samuel Ortiz 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
			     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,
};

325
static int __init asic3_irq_probe(struct platform_device *pdev)
S
Samuel Ortiz 已提交
326 327 328 329
{
	struct asic3 *asic = platform_get_drvdata(pdev);
	unsigned long clksel = 0;
	unsigned int irq, irq_base;
330
	int ret;
S
Samuel Ortiz 已提交
331

332 333 334 335
	ret = platform_get_irq(pdev, 0);
	if (ret < 0)
		return ret;
	asic->irq_nr = ret;
S
Samuel Ortiz 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354

	/* 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);
	}

355
	asic3_write_register(asic, ASIC3_OFFSET(INTR, INT_MASK),
S
Samuel Ortiz 已提交
356 357 358
			     ASIC3_INTMASK_GINTMASK);

	set_irq_chained_handler(asic->irq_nr, asic3_irq_demux);
359
	set_irq_type(asic->irq_nr, IRQ_TYPE_EDGE_RISING);
S
Samuel Ortiz 已提交
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
	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 已提交
382 383 384 385 386 387 388 389 390 391 392
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);

393
	if (gpio_base > ASIC3_GPIO_D_BASE) {
394 395
		dev_err(asic->dev, "Invalid base (0x%x) for gpio %d\n",
			gpio_base, offset);
S
Samuel Ortiz 已提交
396 397 398 399 400
		return -EINVAL;
	}

	spin_lock_irqsave(&asic->lock, flags);

401
	out_reg = asic3_read_register(asic, gpio_base + ASIC3_GPIO_DIRECTION);
S
Samuel Ortiz 已提交
402 403 404 405 406 407 408

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

409
	asic3_write_register(asic, gpio_base + ASIC3_GPIO_DIRECTION, out_reg);
S
Samuel Ortiz 已提交
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438

	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);

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

445
	return asic3_read_register(asic, gpio_base + ASIC3_GPIO_STATUS) & mask;
S
Samuel Ortiz 已提交
446 447 448 449 450 451 452 453 454 455 456 457 458
}

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);

459
	if (gpio_base > ASIC3_GPIO_D_BASE) {
460 461
		dev_err(asic->dev, "Invalid base (0x%x) for gpio %d\n",
			gpio_base, offset);
S
Samuel Ortiz 已提交
462 463 464 465 466 467 468
		return;
	}

	mask = ASIC3_GPIO_TO_MASK(offset);

	spin_lock_irqsave(&asic->lock, flags);

469
	out_reg = asic3_read_register(asic, gpio_base + ASIC3_GPIO_OUT);
S
Samuel Ortiz 已提交
470 471 472 473 474 475

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

476
	asic3_write_register(asic, gpio_base + ASIC3_GPIO_OUT, out_reg);
S
Samuel Ortiz 已提交
477 478 479 480 481 482

	spin_unlock_irqrestore(&asic->lock, flags);

	return;
}

483 484
static __init int asic3_gpio_probe(struct platform_device *pdev,
				   u16 *gpio_config, int num)
S
Samuel Ortiz 已提交
485 486
{
	struct asic3 *asic = platform_get_drvdata(pdev);
487 488 489 490
	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 已提交
491

R
Russell King 已提交
492 493 494
	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));
495 496

	/* Enable all GPIOs */
497 498 499 500
	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 已提交
501

502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
	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) +
522
				     ASIC3_GPIO_DIRECTION,
523 524
				     dir_reg[i]);
		asic3_write_register(asic,
525
				     ASIC3_BANK_TO_BASE(i) + ASIC3_GPIO_OUT,
526 527 528
				     out_reg[i]);
		asic3_write_register(asic,
				     ASIC3_BANK_TO_BASE(i) +
529
				     ASIC3_GPIO_ALT_FUNCTION,
530
				     alt_reg[i]);
S
Samuel Ortiz 已提交
531 532
	}

S
Samuel Ortiz 已提交
533
	return gpiochip_add(&asic->gpio);
S
Samuel Ortiz 已提交
534 535
}

S
Samuel Ortiz 已提交
536
static int asic3_gpio_remove(struct platform_device *pdev)
S
Samuel Ortiz 已提交
537
{
S
Samuel Ortiz 已提交
538 539 540
	struct asic3 *asic = platform_get_drvdata(pdev);

	return gpiochip_remove(&asic->gpio);
S
Samuel Ortiz 已提交
541 542 543 544
}


/* Core */
545
static int __init asic3_probe(struct platform_device *pdev)
S
Samuel Ortiz 已提交
546 547 548 549 550
{
	struct asic3_platform_data *pdata = pdev->dev.platform_data;
	struct asic3 *asic;
	struct resource *mem;
	unsigned long clksel;
S
Samuel Ortiz 已提交
551
	int map_size;
S
Samuel Ortiz 已提交
552
	int ret = 0;
S
Samuel Ortiz 已提交
553 554

	asic = kzalloc(sizeof(struct asic3), GFP_KERNEL);
S
Samuel Ortiz 已提交
555 556
	if (asic == NULL) {
		printk(KERN_ERR "kzalloc failed\n");
S
Samuel Ortiz 已提交
557
		return -ENOMEM;
S
Samuel Ortiz 已提交
558
	}
S
Samuel Ortiz 已提交
559 560 561 562 563 564 565 566

	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;
567
		dev_err(asic->dev, "no MEM resource\n");
S
Samuel Ortiz 已提交
568
		goto out_free;
S
Samuel Ortiz 已提交
569 570
	}

571 572
	map_size = mem->end - mem->start + 1;
	asic->mapping = ioremap(mem->start, map_size);
S
Samuel Ortiz 已提交
573 574
	if (!asic->mapping) {
		ret = -ENOMEM;
575
		dev_err(asic->dev, "Couldn't ioremap\n");
S
Samuel Ortiz 已提交
576
		goto out_free;
S
Samuel Ortiz 已提交
577 578 579 580
	}

	asic->irq_base = pdata->irq_base;

581 582
	/* calculate bus shift from mem resource */
	asic->bus_shift = 2 - (map_size >> 12);
S
Samuel Ortiz 已提交
583 584 585 586 587 588

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

	ret = asic3_irq_probe(pdev);
	if (ret < 0) {
589
		dev_err(asic->dev, "Couldn't probe IRQs\n");
S
Samuel Ortiz 已提交
590 591 592 593 594 595 596 597 598 599
		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;

600 601 602
	ret = asic3_gpio_probe(pdev,
			       pdata->gpio_config,
			       pdata->gpio_config_num);
S
Samuel Ortiz 已提交
603
	if (ret < 0) {
604
		dev_err(asic->dev, "GPIO probe failed\n");
S
Samuel Ortiz 已提交
605
		goto out_irq;
S
Samuel Ortiz 已提交
606 607
	}

608
	dev_info(asic->dev, "ASIC3 Core driver\n");
S
Samuel Ortiz 已提交
609 610 611

	return 0;

S
Samuel Ortiz 已提交
612 613 614 615
 out_irq:
	asic3_irq_remove(pdev);

 out_unmap:
S
Samuel Ortiz 已提交
616
	iounmap(asic->mapping);
S
Samuel Ortiz 已提交
617 618

 out_free:
S
Samuel Ortiz 已提交
619 620 621 622 623 624 625
	kfree(asic);

	return ret;
}

static int asic3_remove(struct platform_device *pdev)
{
S
Samuel Ortiz 已提交
626
	int ret;
S
Samuel Ortiz 已提交
627 628
	struct asic3 *asic = platform_get_drvdata(pdev);

S
Samuel Ortiz 已提交
629 630 631
	ret = asic3_gpio_remove(pdev);
	if (ret < 0)
		return ret;
S
Samuel Ortiz 已提交
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
	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;
658
	retval = platform_driver_probe(&asic3_device_driver, asic3_probe);
S
Samuel Ortiz 已提交
659 660 661 662
	return retval;
}

subsys_initcall(asic3_init);