xref: /openbmc/linux/drivers/mfd/ab8500-core.c (revision 5814fc35e1837e30b82c3d57f41310d4c4c52824)
162579266SRabin Vincent /*
262579266SRabin Vincent  * Copyright (C) ST-Ericsson SA 2010
362579266SRabin Vincent  *
462579266SRabin Vincent  * License Terms: GNU General Public License v2
562579266SRabin Vincent  * Author: Srinidhi Kasagar <srinidhi.kasagar@stericsson.com>
662579266SRabin Vincent  * Author: Rabin Vincent <rabin.vincent@stericsson.com>
747c16975SMattias Wallin  * Changes: Mattias Wallin <mattias.wallin@stericsson.com>
862579266SRabin Vincent  */
962579266SRabin Vincent 
1062579266SRabin Vincent #include <linux/kernel.h>
1162579266SRabin Vincent #include <linux/slab.h>
1262579266SRabin Vincent #include <linux/init.h>
1362579266SRabin Vincent #include <linux/irq.h>
1462579266SRabin Vincent #include <linux/delay.h>
1562579266SRabin Vincent #include <linux/interrupt.h>
1662579266SRabin Vincent #include <linux/module.h>
1762579266SRabin Vincent #include <linux/platform_device.h>
1862579266SRabin Vincent #include <linux/mfd/core.h>
1947c16975SMattias Wallin #include <linux/mfd/abx500.h>
2062579266SRabin Vincent #include <linux/mfd/ab8500.h>
21549931f9SSundar R Iyer #include <linux/regulator/ab8500.h>
2262579266SRabin Vincent 
2362579266SRabin Vincent /*
2462579266SRabin Vincent  * Interrupt register offsets
2562579266SRabin Vincent  * Bank : 0x0E
2662579266SRabin Vincent  */
2747c16975SMattias Wallin #define AB8500_IT_SOURCE1_REG		0x00
2847c16975SMattias Wallin #define AB8500_IT_SOURCE2_REG		0x01
2947c16975SMattias Wallin #define AB8500_IT_SOURCE3_REG		0x02
3047c16975SMattias Wallin #define AB8500_IT_SOURCE4_REG		0x03
3147c16975SMattias Wallin #define AB8500_IT_SOURCE5_REG		0x04
3247c16975SMattias Wallin #define AB8500_IT_SOURCE6_REG		0x05
3347c16975SMattias Wallin #define AB8500_IT_SOURCE7_REG		0x06
3447c16975SMattias Wallin #define AB8500_IT_SOURCE8_REG		0x07
3547c16975SMattias Wallin #define AB8500_IT_SOURCE19_REG		0x12
3647c16975SMattias Wallin #define AB8500_IT_SOURCE20_REG		0x13
3747c16975SMattias Wallin #define AB8500_IT_SOURCE21_REG		0x14
3847c16975SMattias Wallin #define AB8500_IT_SOURCE22_REG		0x15
3947c16975SMattias Wallin #define AB8500_IT_SOURCE23_REG		0x16
4047c16975SMattias Wallin #define AB8500_IT_SOURCE24_REG		0x17
4162579266SRabin Vincent 
4262579266SRabin Vincent /*
4362579266SRabin Vincent  * latch registers
4462579266SRabin Vincent  */
4547c16975SMattias Wallin #define AB8500_IT_LATCH1_REG		0x20
4647c16975SMattias Wallin #define AB8500_IT_LATCH2_REG		0x21
4747c16975SMattias Wallin #define AB8500_IT_LATCH3_REG		0x22
4847c16975SMattias Wallin #define AB8500_IT_LATCH4_REG		0x23
4947c16975SMattias Wallin #define AB8500_IT_LATCH5_REG		0x24
5047c16975SMattias Wallin #define AB8500_IT_LATCH6_REG		0x25
5147c16975SMattias Wallin #define AB8500_IT_LATCH7_REG		0x26
5247c16975SMattias Wallin #define AB8500_IT_LATCH8_REG		0x27
5347c16975SMattias Wallin #define AB8500_IT_LATCH9_REG		0x28
5447c16975SMattias Wallin #define AB8500_IT_LATCH10_REG		0x29
5547c16975SMattias Wallin #define AB8500_IT_LATCH19_REG		0x32
5647c16975SMattias Wallin #define AB8500_IT_LATCH20_REG		0x33
5747c16975SMattias Wallin #define AB8500_IT_LATCH21_REG		0x34
5847c16975SMattias Wallin #define AB8500_IT_LATCH22_REG		0x35
5947c16975SMattias Wallin #define AB8500_IT_LATCH23_REG		0x36
6047c16975SMattias Wallin #define AB8500_IT_LATCH24_REG		0x37
6162579266SRabin Vincent 
6262579266SRabin Vincent /*
6362579266SRabin Vincent  * mask registers
6462579266SRabin Vincent  */
6562579266SRabin Vincent 
6647c16975SMattias Wallin #define AB8500_IT_MASK1_REG		0x40
6747c16975SMattias Wallin #define AB8500_IT_MASK2_REG		0x41
6847c16975SMattias Wallin #define AB8500_IT_MASK3_REG		0x42
6947c16975SMattias Wallin #define AB8500_IT_MASK4_REG		0x43
7047c16975SMattias Wallin #define AB8500_IT_MASK5_REG		0x44
7147c16975SMattias Wallin #define AB8500_IT_MASK6_REG		0x45
7247c16975SMattias Wallin #define AB8500_IT_MASK7_REG		0x46
7347c16975SMattias Wallin #define AB8500_IT_MASK8_REG		0x47
7447c16975SMattias Wallin #define AB8500_IT_MASK9_REG		0x48
7547c16975SMattias Wallin #define AB8500_IT_MASK10_REG		0x49
7647c16975SMattias Wallin #define AB8500_IT_MASK11_REG		0x4A
7747c16975SMattias Wallin #define AB8500_IT_MASK12_REG		0x4B
7847c16975SMattias Wallin #define AB8500_IT_MASK13_REG		0x4C
7947c16975SMattias Wallin #define AB8500_IT_MASK14_REG		0x4D
8047c16975SMattias Wallin #define AB8500_IT_MASK15_REG		0x4E
8147c16975SMattias Wallin #define AB8500_IT_MASK16_REG		0x4F
8247c16975SMattias Wallin #define AB8500_IT_MASK17_REG		0x50
8347c16975SMattias Wallin #define AB8500_IT_MASK18_REG		0x51
8447c16975SMattias Wallin #define AB8500_IT_MASK19_REG		0x52
8547c16975SMattias Wallin #define AB8500_IT_MASK20_REG		0x53
8647c16975SMattias Wallin #define AB8500_IT_MASK21_REG		0x54
8747c16975SMattias Wallin #define AB8500_IT_MASK22_REG		0x55
8847c16975SMattias Wallin #define AB8500_IT_MASK23_REG		0x56
8947c16975SMattias Wallin #define AB8500_IT_MASK24_REG		0x57
9062579266SRabin Vincent 
9147c16975SMattias Wallin #define AB8500_REV_REG			0x80
9262579266SRabin Vincent 
9362579266SRabin Vincent /*
9462579266SRabin Vincent  * Map interrupt numbers to the LATCH and MASK register offsets, Interrupt
9562579266SRabin Vincent  * numbers are indexed into this array with (num / 8).
9662579266SRabin Vincent  *
9762579266SRabin Vincent  * This is one off from the register names, i.e. AB8500_IT_MASK1_REG is at
9862579266SRabin Vincent  * offset 0.
9962579266SRabin Vincent  */
10062579266SRabin Vincent static const int ab8500_irq_regoffset[AB8500_NUM_IRQ_REGS] = {
10162579266SRabin Vincent 	0, 1, 2, 3, 4, 6, 7, 8, 9, 18, 19, 20, 21,
10262579266SRabin Vincent };
10362579266SRabin Vincent 
10447c16975SMattias Wallin static int ab8500_get_chip_id(struct device *dev)
10547c16975SMattias Wallin {
10647c16975SMattias Wallin 	struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
10747c16975SMattias Wallin 	return (int)ab8500->chip_id;
10847c16975SMattias Wallin }
10947c16975SMattias Wallin 
11047c16975SMattias Wallin static int set_register_interruptible(struct ab8500 *ab8500, u8 bank,
11147c16975SMattias Wallin 	u8 reg, u8 data)
11262579266SRabin Vincent {
11362579266SRabin Vincent 	int ret;
11447c16975SMattias Wallin 	/*
11547c16975SMattias Wallin 	 * Put the u8 bank and u8 register together into a an u16.
11647c16975SMattias Wallin 	 * The bank on higher 8 bits and register in lower 8 bits.
11747c16975SMattias Wallin 	 * */
11847c16975SMattias Wallin 	u16 addr = ((u16)bank) << 8 | reg;
11962579266SRabin Vincent 
12062579266SRabin Vincent 	dev_vdbg(ab8500->dev, "wr: addr %#x <= %#x\n", addr, data);
12162579266SRabin Vincent 
12247c16975SMattias Wallin 	ret = mutex_lock_interruptible(&ab8500->lock);
12347c16975SMattias Wallin 	if (ret)
12447c16975SMattias Wallin 		return ret;
12547c16975SMattias Wallin 
12647c16975SMattias Wallin 	ret = ab8500->write(ab8500, addr, data);
12747c16975SMattias Wallin 	if (ret < 0)
12847c16975SMattias Wallin 		dev_err(ab8500->dev, "failed to write reg %#x: %d\n",
12947c16975SMattias Wallin 			addr, ret);
13047c16975SMattias Wallin 	mutex_unlock(&ab8500->lock);
13147c16975SMattias Wallin 
13247c16975SMattias Wallin 	return ret;
13347c16975SMattias Wallin }
13447c16975SMattias Wallin 
13547c16975SMattias Wallin static int ab8500_set_register(struct device *dev, u8 bank,
13647c16975SMattias Wallin 	u8 reg, u8 value)
13747c16975SMattias Wallin {
13847c16975SMattias Wallin 	struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
13947c16975SMattias Wallin 
14047c16975SMattias Wallin 	return set_register_interruptible(ab8500, bank, reg, value);
14147c16975SMattias Wallin }
14247c16975SMattias Wallin 
14347c16975SMattias Wallin static int get_register_interruptible(struct ab8500 *ab8500, u8 bank,
14447c16975SMattias Wallin 	u8 reg, u8 *value)
14547c16975SMattias Wallin {
14647c16975SMattias Wallin 	int ret;
14747c16975SMattias Wallin 	/* put the u8 bank and u8 reg together into a an u16.
14847c16975SMattias Wallin 	 * bank on higher 8 bits and reg in lower */
14947c16975SMattias Wallin 	u16 addr = ((u16)bank) << 8 | reg;
15047c16975SMattias Wallin 
15147c16975SMattias Wallin 	ret = mutex_lock_interruptible(&ab8500->lock);
15247c16975SMattias Wallin 	if (ret)
15347c16975SMattias Wallin 		return ret;
15447c16975SMattias Wallin 
15547c16975SMattias Wallin 	ret = ab8500->read(ab8500, addr);
15647c16975SMattias Wallin 	if (ret < 0)
15747c16975SMattias Wallin 		dev_err(ab8500->dev, "failed to read reg %#x: %d\n",
15847c16975SMattias Wallin 			addr, ret);
15947c16975SMattias Wallin 	else
16047c16975SMattias Wallin 		*value = ret;
16147c16975SMattias Wallin 
16247c16975SMattias Wallin 	mutex_unlock(&ab8500->lock);
16347c16975SMattias Wallin 	dev_vdbg(ab8500->dev, "rd: addr %#x => data %#x\n", addr, ret);
16447c16975SMattias Wallin 
16547c16975SMattias Wallin 	return ret;
16647c16975SMattias Wallin }
16747c16975SMattias Wallin 
16847c16975SMattias Wallin static int ab8500_get_register(struct device *dev, u8 bank,
16947c16975SMattias Wallin 	u8 reg, u8 *value)
17047c16975SMattias Wallin {
17147c16975SMattias Wallin 	struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
17247c16975SMattias Wallin 
17347c16975SMattias Wallin 	return get_register_interruptible(ab8500, bank, reg, value);
17447c16975SMattias Wallin }
17547c16975SMattias Wallin 
17647c16975SMattias Wallin static int mask_and_set_register_interruptible(struct ab8500 *ab8500, u8 bank,
17747c16975SMattias Wallin 	u8 reg, u8 bitmask, u8 bitvalues)
17847c16975SMattias Wallin {
17947c16975SMattias Wallin 	int ret;
18047c16975SMattias Wallin 	u8 data;
18147c16975SMattias Wallin 	/* put the u8 bank and u8 reg together into a an u16.
18247c16975SMattias Wallin 	 * bank on higher 8 bits and reg in lower */
18347c16975SMattias Wallin 	u16 addr = ((u16)bank) << 8 | reg;
18447c16975SMattias Wallin 
18547c16975SMattias Wallin 	ret = mutex_lock_interruptible(&ab8500->lock);
18647c16975SMattias Wallin 	if (ret)
18747c16975SMattias Wallin 		return ret;
18847c16975SMattias Wallin 
18947c16975SMattias Wallin 	ret = ab8500->read(ab8500, addr);
19047c16975SMattias Wallin 	if (ret < 0) {
19147c16975SMattias Wallin 		dev_err(ab8500->dev, "failed to read reg %#x: %d\n",
19247c16975SMattias Wallin 			addr, ret);
19347c16975SMattias Wallin 		goto out;
19447c16975SMattias Wallin 	}
19547c16975SMattias Wallin 
19647c16975SMattias Wallin 	data = (u8)ret;
19747c16975SMattias Wallin 	data = (~bitmask & data) | (bitmask & bitvalues);
19847c16975SMattias Wallin 
19962579266SRabin Vincent 	ret = ab8500->write(ab8500, addr, data);
20062579266SRabin Vincent 	if (ret < 0)
20162579266SRabin Vincent 		dev_err(ab8500->dev, "failed to write reg %#x: %d\n",
20262579266SRabin Vincent 			addr, ret);
20362579266SRabin Vincent 
20447c16975SMattias Wallin 	dev_vdbg(ab8500->dev, "mask: addr %#x => data %#x\n", addr, data);
20562579266SRabin Vincent out:
20662579266SRabin Vincent 	mutex_unlock(&ab8500->lock);
20762579266SRabin Vincent 	return ret;
20862579266SRabin Vincent }
20947c16975SMattias Wallin 
21047c16975SMattias Wallin static int ab8500_mask_and_set_register(struct device *dev,
21147c16975SMattias Wallin 	u8 bank, u8 reg, u8 bitmask, u8 bitvalues)
21247c16975SMattias Wallin {
21347c16975SMattias Wallin 	struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
21447c16975SMattias Wallin 
21547c16975SMattias Wallin 	return mask_and_set_register_interruptible(ab8500, bank, reg,
21647c16975SMattias Wallin 		bitmask, bitvalues);
21747c16975SMattias Wallin 
21847c16975SMattias Wallin }
21947c16975SMattias Wallin 
22047c16975SMattias Wallin static struct abx500_ops ab8500_ops = {
22147c16975SMattias Wallin 	.get_chip_id = ab8500_get_chip_id,
22247c16975SMattias Wallin 	.get_register = ab8500_get_register,
22347c16975SMattias Wallin 	.set_register = ab8500_set_register,
22447c16975SMattias Wallin 	.get_register_page = NULL,
22547c16975SMattias Wallin 	.set_register_page = NULL,
22647c16975SMattias Wallin 	.mask_and_set_register = ab8500_mask_and_set_register,
22747c16975SMattias Wallin 	.event_registers_startup_state_get = NULL,
22847c16975SMattias Wallin 	.startup_irq_enabled = NULL,
22947c16975SMattias Wallin };
23062579266SRabin Vincent 
23162579266SRabin Vincent static void ab8500_irq_lock(unsigned int irq)
23262579266SRabin Vincent {
23362579266SRabin Vincent 	struct ab8500 *ab8500 = get_irq_chip_data(irq);
23462579266SRabin Vincent 
23562579266SRabin Vincent 	mutex_lock(&ab8500->irq_lock);
23662579266SRabin Vincent }
23762579266SRabin Vincent 
23862579266SRabin Vincent static void ab8500_irq_sync_unlock(unsigned int irq)
23962579266SRabin Vincent {
24062579266SRabin Vincent 	struct ab8500 *ab8500 = get_irq_chip_data(irq);
24162579266SRabin Vincent 	int i;
24262579266SRabin Vincent 
24362579266SRabin Vincent 	for (i = 0; i < AB8500_NUM_IRQ_REGS; i++) {
24462579266SRabin Vincent 		u8 old = ab8500->oldmask[i];
24562579266SRabin Vincent 		u8 new = ab8500->mask[i];
24662579266SRabin Vincent 		int reg;
24762579266SRabin Vincent 
24862579266SRabin Vincent 		if (new == old)
24962579266SRabin Vincent 			continue;
25062579266SRabin Vincent 
25162579266SRabin Vincent 		ab8500->oldmask[i] = new;
25262579266SRabin Vincent 
25362579266SRabin Vincent 		reg = AB8500_IT_MASK1_REG + ab8500_irq_regoffset[i];
25447c16975SMattias Wallin 		set_register_interruptible(ab8500, AB8500_INTERRUPT, reg, new);
25562579266SRabin Vincent 	}
25662579266SRabin Vincent 
25762579266SRabin Vincent 	mutex_unlock(&ab8500->irq_lock);
25862579266SRabin Vincent }
25962579266SRabin Vincent 
26062579266SRabin Vincent static void ab8500_irq_mask(unsigned int irq)
26162579266SRabin Vincent {
26262579266SRabin Vincent 	struct ab8500 *ab8500 = get_irq_chip_data(irq);
26362579266SRabin Vincent 	int offset = irq - ab8500->irq_base;
26462579266SRabin Vincent 	int index = offset / 8;
26562579266SRabin Vincent 	int mask = 1 << (offset % 8);
26662579266SRabin Vincent 
26762579266SRabin Vincent 	ab8500->mask[index] |= mask;
26862579266SRabin Vincent }
26962579266SRabin Vincent 
27062579266SRabin Vincent static void ab8500_irq_unmask(unsigned int irq)
27162579266SRabin Vincent {
27262579266SRabin Vincent 	struct ab8500 *ab8500 = get_irq_chip_data(irq);
27362579266SRabin Vincent 	int offset = irq - ab8500->irq_base;
27462579266SRabin Vincent 	int index = offset / 8;
27562579266SRabin Vincent 	int mask = 1 << (offset % 8);
27662579266SRabin Vincent 
27762579266SRabin Vincent 	ab8500->mask[index] &= ~mask;
27862579266SRabin Vincent }
27962579266SRabin Vincent 
28062579266SRabin Vincent static struct irq_chip ab8500_irq_chip = {
28162579266SRabin Vincent 	.name			= "ab8500",
28262579266SRabin Vincent 	.bus_lock		= ab8500_irq_lock,
28362579266SRabin Vincent 	.bus_sync_unlock	= ab8500_irq_sync_unlock,
28462579266SRabin Vincent 	.mask			= ab8500_irq_mask,
28562579266SRabin Vincent 	.unmask			= ab8500_irq_unmask,
28662579266SRabin Vincent };
28762579266SRabin Vincent 
28862579266SRabin Vincent static irqreturn_t ab8500_irq(int irq, void *dev)
28962579266SRabin Vincent {
29062579266SRabin Vincent 	struct ab8500 *ab8500 = dev;
29162579266SRabin Vincent 	int i;
29262579266SRabin Vincent 
29362579266SRabin Vincent 	dev_vdbg(ab8500->dev, "interrupt\n");
29462579266SRabin Vincent 
29562579266SRabin Vincent 	for (i = 0; i < AB8500_NUM_IRQ_REGS; i++) {
29662579266SRabin Vincent 		int regoffset = ab8500_irq_regoffset[i];
29762579266SRabin Vincent 		int status;
29847c16975SMattias Wallin 		u8 value;
29962579266SRabin Vincent 
30047c16975SMattias Wallin 		status = get_register_interruptible(ab8500, AB8500_INTERRUPT,
30147c16975SMattias Wallin 			AB8500_IT_LATCH1_REG + regoffset, &value);
30247c16975SMattias Wallin 		if (status < 0 || value == 0)
30362579266SRabin Vincent 			continue;
30462579266SRabin Vincent 
30562579266SRabin Vincent 		do {
30662579266SRabin Vincent 			int bit = __ffs(status);
30762579266SRabin Vincent 			int line = i * 8 + bit;
30862579266SRabin Vincent 
30962579266SRabin Vincent 			handle_nested_irq(ab8500->irq_base + line);
31047c16975SMattias Wallin 			value &= ~(1 << bit);
31147c16975SMattias Wallin 		} while (value);
31262579266SRabin Vincent 	}
31362579266SRabin Vincent 
31462579266SRabin Vincent 	return IRQ_HANDLED;
31562579266SRabin Vincent }
31662579266SRabin Vincent 
31762579266SRabin Vincent static int ab8500_irq_init(struct ab8500 *ab8500)
31862579266SRabin Vincent {
31962579266SRabin Vincent 	int base = ab8500->irq_base;
32062579266SRabin Vincent 	int irq;
32162579266SRabin Vincent 
32262579266SRabin Vincent 	for (irq = base; irq < base + AB8500_NR_IRQS; irq++) {
32362579266SRabin Vincent 		set_irq_chip_data(irq, ab8500);
32462579266SRabin Vincent 		set_irq_chip_and_handler(irq, &ab8500_irq_chip,
32562579266SRabin Vincent 					 handle_simple_irq);
32662579266SRabin Vincent 		set_irq_nested_thread(irq, 1);
32762579266SRabin Vincent #ifdef CONFIG_ARM
32862579266SRabin Vincent 		set_irq_flags(irq, IRQF_VALID);
32962579266SRabin Vincent #else
33062579266SRabin Vincent 		set_irq_noprobe(irq);
33162579266SRabin Vincent #endif
33262579266SRabin Vincent 	}
33362579266SRabin Vincent 
33462579266SRabin Vincent 	return 0;
33562579266SRabin Vincent }
33662579266SRabin Vincent 
33762579266SRabin Vincent static void ab8500_irq_remove(struct ab8500 *ab8500)
33862579266SRabin Vincent {
33962579266SRabin Vincent 	int base = ab8500->irq_base;
34062579266SRabin Vincent 	int irq;
34162579266SRabin Vincent 
34262579266SRabin Vincent 	for (irq = base; irq < base + AB8500_NR_IRQS; irq++) {
34362579266SRabin Vincent #ifdef CONFIG_ARM
34462579266SRabin Vincent 		set_irq_flags(irq, 0);
34562579266SRabin Vincent #endif
34662579266SRabin Vincent 		set_irq_chip_and_handler(irq, NULL, NULL);
34762579266SRabin Vincent 		set_irq_chip_data(irq, NULL);
34862579266SRabin Vincent 	}
34962579266SRabin Vincent }
35062579266SRabin Vincent 
35162579266SRabin Vincent static struct resource ab8500_gpadc_resources[] = {
35262579266SRabin Vincent 	{
35362579266SRabin Vincent 		.name	= "HW_CONV_END",
35462579266SRabin Vincent 		.start	= AB8500_INT_GP_HW_ADC_CONV_END,
35562579266SRabin Vincent 		.end	= AB8500_INT_GP_HW_ADC_CONV_END,
35662579266SRabin Vincent 		.flags	= IORESOURCE_IRQ,
35762579266SRabin Vincent 	},
35862579266SRabin Vincent 	{
35962579266SRabin Vincent 		.name	= "SW_CONV_END",
36062579266SRabin Vincent 		.start	= AB8500_INT_GP_SW_ADC_CONV_END,
36162579266SRabin Vincent 		.end	= AB8500_INT_GP_SW_ADC_CONV_END,
36262579266SRabin Vincent 		.flags	= IORESOURCE_IRQ,
36362579266SRabin Vincent 	},
36462579266SRabin Vincent };
36562579266SRabin Vincent 
36662579266SRabin Vincent static struct resource ab8500_rtc_resources[] = {
36762579266SRabin Vincent 	{
36862579266SRabin Vincent 		.name	= "60S",
36962579266SRabin Vincent 		.start	= AB8500_INT_RTC_60S,
37062579266SRabin Vincent 		.end	= AB8500_INT_RTC_60S,
37162579266SRabin Vincent 		.flags	= IORESOURCE_IRQ,
37262579266SRabin Vincent 	},
37362579266SRabin Vincent 	{
37462579266SRabin Vincent 		.name	= "ALARM",
37562579266SRabin Vincent 		.start	= AB8500_INT_RTC_ALARM,
37662579266SRabin Vincent 		.end	= AB8500_INT_RTC_ALARM,
37762579266SRabin Vincent 		.flags	= IORESOURCE_IRQ,
37862579266SRabin Vincent 	},
37962579266SRabin Vincent };
38062579266SRabin Vincent 
38177686517SSundar R Iyer static struct resource ab8500_poweronkey_db_resources[] = {
38277686517SSundar R Iyer 	{
38377686517SSundar R Iyer 		.name	= "ONKEY_DBF",
38477686517SSundar R Iyer 		.start	= AB8500_INT_PON_KEY1DB_F,
38577686517SSundar R Iyer 		.end	= AB8500_INT_PON_KEY1DB_F,
38677686517SSundar R Iyer 		.flags	= IORESOURCE_IRQ,
38777686517SSundar R Iyer 	},
38877686517SSundar R Iyer 	{
38977686517SSundar R Iyer 		.name	= "ONKEY_DBR",
39077686517SSundar R Iyer 		.start	= AB8500_INT_PON_KEY1DB_R,
39177686517SSundar R Iyer 		.end	= AB8500_INT_PON_KEY1DB_R,
39277686517SSundar R Iyer 		.flags	= IORESOURCE_IRQ,
39377686517SSundar R Iyer 	},
39477686517SSundar R Iyer };
39577686517SSundar R Iyer 
39662579266SRabin Vincent static struct mfd_cell ab8500_devs[] = {
397*5814fc35SMattias Wallin #ifdef CONFIG_DEBUG_FS
398*5814fc35SMattias Wallin 	{
399*5814fc35SMattias Wallin 		.name = "ab8500-debug",
400*5814fc35SMattias Wallin 	},
401*5814fc35SMattias Wallin #endif
40262579266SRabin Vincent 	{
40362579266SRabin Vincent 		.name = "ab8500-gpadc",
40462579266SRabin Vincent 		.num_resources = ARRAY_SIZE(ab8500_gpadc_resources),
40562579266SRabin Vincent 		.resources = ab8500_gpadc_resources,
40662579266SRabin Vincent 	},
40762579266SRabin Vincent 	{
40862579266SRabin Vincent 		.name = "ab8500-rtc",
40962579266SRabin Vincent 		.num_resources = ARRAY_SIZE(ab8500_rtc_resources),
41062579266SRabin Vincent 		.resources = ab8500_rtc_resources,
41162579266SRabin Vincent 	},
412f0f05b1cSArun Murthy 	{
413f0f05b1cSArun Murthy 		.name = "ab8500-pwm",
414f0f05b1cSArun Murthy 		.id = 1,
415f0f05b1cSArun Murthy 	},
416f0f05b1cSArun Murthy 	{
417f0f05b1cSArun Murthy 		.name = "ab8500-pwm",
418f0f05b1cSArun Murthy 		.id = 2,
419f0f05b1cSArun Murthy 	},
420f0f05b1cSArun Murthy 	{
421f0f05b1cSArun Murthy 		.name = "ab8500-pwm",
422f0f05b1cSArun Murthy 		.id = 3,
423f0f05b1cSArun Murthy 	},
42462579266SRabin Vincent 	{ .name = "ab8500-charger", },
42562579266SRabin Vincent 	{ .name = "ab8500-audio", },
42662579266SRabin Vincent 	{ .name = "ab8500-usb", },
427549931f9SSundar R Iyer 	{ .name = "ab8500-regulator", },
42877686517SSundar R Iyer 	{
42977686517SSundar R Iyer 		.name = "ab8500-poweron-key",
43077686517SSundar R Iyer 		.num_resources = ARRAY_SIZE(ab8500_poweronkey_db_resources),
43177686517SSundar R Iyer 		.resources = ab8500_poweronkey_db_resources,
43277686517SSundar R Iyer 	},
43362579266SRabin Vincent };
43462579266SRabin Vincent 
43562579266SRabin Vincent int __devinit ab8500_init(struct ab8500 *ab8500)
43662579266SRabin Vincent {
43762579266SRabin Vincent 	struct ab8500_platform_data *plat = dev_get_platdata(ab8500->dev);
43862579266SRabin Vincent 	int ret;
43962579266SRabin Vincent 	int i;
44047c16975SMattias Wallin 	u8 value;
44162579266SRabin Vincent 
44262579266SRabin Vincent 	if (plat)
44362579266SRabin Vincent 		ab8500->irq_base = plat->irq_base;
44462579266SRabin Vincent 
44562579266SRabin Vincent 	mutex_init(&ab8500->lock);
44662579266SRabin Vincent 	mutex_init(&ab8500->irq_lock);
44762579266SRabin Vincent 
44847c16975SMattias Wallin 	ret = get_register_interruptible(ab8500, AB8500_MISC,
44947c16975SMattias Wallin 		AB8500_REV_REG, &value);
45062579266SRabin Vincent 	if (ret < 0)
45162579266SRabin Vincent 		return ret;
45262579266SRabin Vincent 
45362579266SRabin Vincent 	/*
45462579266SRabin Vincent 	 * 0x0 - Early Drop
45562579266SRabin Vincent 	 * 0x10 - Cut 1.0
45662579266SRabin Vincent 	 * 0x11 - Cut 1.1
45762579266SRabin Vincent 	 */
45847c16975SMattias Wallin 	if (value == 0x0 || value == 0x10 || value == 0x11) {
45947c16975SMattias Wallin 		ab8500->revision = value;
46047c16975SMattias Wallin 		dev_info(ab8500->dev, "detected chip, revision: %#x\n", value);
46162579266SRabin Vincent 	} else {
46247c16975SMattias Wallin 		dev_err(ab8500->dev, "unknown chip, revision: %#x\n", value);
46362579266SRabin Vincent 		return -EINVAL;
46462579266SRabin Vincent 	}
46547c16975SMattias Wallin 	ab8500->chip_id = value;
46662579266SRabin Vincent 
46762579266SRabin Vincent 	if (plat && plat->init)
46862579266SRabin Vincent 		plat->init(ab8500);
46962579266SRabin Vincent 
47062579266SRabin Vincent 	/* Clear and mask all interrupts */
47162579266SRabin Vincent 	for (i = 0; i < 10; i++) {
47247c16975SMattias Wallin 		get_register_interruptible(ab8500, AB8500_INTERRUPT,
47347c16975SMattias Wallin 			AB8500_IT_LATCH1_REG + i, &value);
47447c16975SMattias Wallin 		set_register_interruptible(ab8500, AB8500_INTERRUPT,
47547c16975SMattias Wallin 			AB8500_IT_MASK1_REG + i, 0xff);
47662579266SRabin Vincent 	}
47762579266SRabin Vincent 
47862579266SRabin Vincent 	for (i = 18; i < 24; i++) {
47947c16975SMattias Wallin 		get_register_interruptible(ab8500, AB8500_INTERRUPT,
48047c16975SMattias Wallin 			AB8500_IT_LATCH1_REG + i, &value);
48147c16975SMattias Wallin 		set_register_interruptible(ab8500, AB8500_INTERRUPT,
48247c16975SMattias Wallin 			AB8500_IT_MASK1_REG + i, 0xff);
48362579266SRabin Vincent 	}
48462579266SRabin Vincent 
48547c16975SMattias Wallin 	ret = abx500_register_ops(ab8500->dev, &ab8500_ops);
48647c16975SMattias Wallin 	if (ret)
48747c16975SMattias Wallin 		return ret;
48847c16975SMattias Wallin 
48962579266SRabin Vincent 	for (i = 0; i < AB8500_NUM_IRQ_REGS; i++)
49062579266SRabin Vincent 		ab8500->mask[i] = ab8500->oldmask[i] = 0xff;
49162579266SRabin Vincent 
49262579266SRabin Vincent 	if (ab8500->irq_base) {
49362579266SRabin Vincent 		ret = ab8500_irq_init(ab8500);
49462579266SRabin Vincent 		if (ret)
49562579266SRabin Vincent 			return ret;
49662579266SRabin Vincent 
49762579266SRabin Vincent 		ret = request_threaded_irq(ab8500->irq, NULL, ab8500_irq,
49862579266SRabin Vincent 					   IRQF_ONESHOT, "ab8500", ab8500);
49962579266SRabin Vincent 		if (ret)
50062579266SRabin Vincent 			goto out_removeirq;
50162579266SRabin Vincent 	}
50262579266SRabin Vincent 
503549931f9SSundar R Iyer 	ret = mfd_add_devices(ab8500->dev, 0, ab8500_devs,
50462579266SRabin Vincent 			      ARRAY_SIZE(ab8500_devs), NULL,
50562579266SRabin Vincent 			      ab8500->irq_base);
50662579266SRabin Vincent 	if (ret)
50762579266SRabin Vincent 		goto out_freeirq;
50862579266SRabin Vincent 
50962579266SRabin Vincent 	return ret;
51062579266SRabin Vincent 
51162579266SRabin Vincent out_freeirq:
51262579266SRabin Vincent 	if (ab8500->irq_base) {
51362579266SRabin Vincent 		free_irq(ab8500->irq, ab8500);
51462579266SRabin Vincent out_removeirq:
51562579266SRabin Vincent 		ab8500_irq_remove(ab8500);
51662579266SRabin Vincent 	}
51762579266SRabin Vincent 	return ret;
51862579266SRabin Vincent }
51962579266SRabin Vincent 
52062579266SRabin Vincent int __devexit ab8500_exit(struct ab8500 *ab8500)
52162579266SRabin Vincent {
52262579266SRabin Vincent 	mfd_remove_devices(ab8500->dev);
52362579266SRabin Vincent 	if (ab8500->irq_base) {
52462579266SRabin Vincent 		free_irq(ab8500->irq, ab8500);
52562579266SRabin Vincent 		ab8500_irq_remove(ab8500);
52662579266SRabin Vincent 	}
52762579266SRabin Vincent 
52862579266SRabin Vincent 	return 0;
52962579266SRabin Vincent }
53062579266SRabin Vincent 
53162579266SRabin Vincent MODULE_AUTHOR("Srinidhi Kasagar, Rabin Vincent");
53262579266SRabin Vincent MODULE_DESCRIPTION("AB8500 MFD core");
53362579266SRabin Vincent MODULE_LICENSE("GPL v2");
534