blob: 50e6f1e297277398c344336f29fc39acc9d38f7c [file] [log] [blame]
Rabin Vincent62579262010-05-19 11:39:02 +02001/*
2 * Copyright (C) ST-Ericsson SA 2010
3 *
4 * License Terms: GNU General Public License v2
5 * Author: Srinidhi Kasagar <srinidhi.kasagar@stericsson.com>
6 * Author: Rabin Vincent <rabin.vincent@stericsson.com>
Mattias Wallinadceed62011-03-02 11:51:11 +01007 * Author: Mattias Wallin <mattias.wallin@stericsson.com>
Rabin Vincent62579262010-05-19 11:39:02 +02008 */
9
10#include <linux/kernel.h>
11#include <linux/slab.h>
12#include <linux/init.h>
13#include <linux/irq.h>
Lee Jones06e589e2012-06-20 13:56:37 +010014#include <linux/irqdomain.h>
Rabin Vincent62579262010-05-19 11:39:02 +020015#include <linux/delay.h>
16#include <linux/interrupt.h>
17#include <linux/module.h>
18#include <linux/platform_device.h>
19#include <linux/mfd/core.h>
Mattias Wallin47c16972010-09-10 17:47:56 +020020#include <linux/mfd/abx500.h>
Linus Walleijee66e652011-12-02 14:16:33 +010021#include <linux/mfd/abx500/ab8500.h>
Lee Jones00441b52013-01-09 10:06:03 +000022#include <linux/mfd/abx500/ab8500-bm.h>
Lee Jonesd28f1db2012-05-19 17:21:37 +020023#include <linux/mfd/dbx500-prcmu.h>
Sundar R Iyer549931f2010-07-13 11:51:28 +053024#include <linux/regulator/ab8500.h>
Lee Jones6bc4a562012-05-17 14:45:13 +010025#include <linux/of.h>
26#include <linux/of_device.h>
Rabin Vincent62579262010-05-19 11:39:02 +020027
28/*
29 * Interrupt register offsets
30 * Bank : 0x0E
31 */
Mattias Wallin47c16972010-09-10 17:47:56 +020032#define AB8500_IT_SOURCE1_REG 0x00
33#define AB8500_IT_SOURCE2_REG 0x01
34#define AB8500_IT_SOURCE3_REG 0x02
35#define AB8500_IT_SOURCE4_REG 0x03
36#define AB8500_IT_SOURCE5_REG 0x04
37#define AB8500_IT_SOURCE6_REG 0x05
38#define AB8500_IT_SOURCE7_REG 0x06
39#define AB8500_IT_SOURCE8_REG 0x07
Linus Walleijd6255522012-02-20 21:42:24 +010040#define AB9540_IT_SOURCE13_REG 0x0C
Mattias Wallin47c16972010-09-10 17:47:56 +020041#define AB8500_IT_SOURCE19_REG 0x12
42#define AB8500_IT_SOURCE20_REG 0x13
43#define AB8500_IT_SOURCE21_REG 0x14
44#define AB8500_IT_SOURCE22_REG 0x15
45#define AB8500_IT_SOURCE23_REG 0x16
46#define AB8500_IT_SOURCE24_REG 0x17
Rabin Vincent62579262010-05-19 11:39:02 +020047
48/*
49 * latch registers
50 */
Mattias Wallin47c16972010-09-10 17:47:56 +020051#define AB8500_IT_LATCH1_REG 0x20
52#define AB8500_IT_LATCH2_REG 0x21
53#define AB8500_IT_LATCH3_REG 0x22
54#define AB8500_IT_LATCH4_REG 0x23
55#define AB8500_IT_LATCH5_REG 0x24
56#define AB8500_IT_LATCH6_REG 0x25
57#define AB8500_IT_LATCH7_REG 0x26
58#define AB8500_IT_LATCH8_REG 0x27
59#define AB8500_IT_LATCH9_REG 0x28
60#define AB8500_IT_LATCH10_REG 0x29
Mattias Wallin92d50a42010-12-07 11:20:47 +010061#define AB8500_IT_LATCH12_REG 0x2B
Linus Walleijd6255522012-02-20 21:42:24 +010062#define AB9540_IT_LATCH13_REG 0x2C
Mattias Wallin47c16972010-09-10 17:47:56 +020063#define AB8500_IT_LATCH19_REG 0x32
64#define AB8500_IT_LATCH20_REG 0x33
65#define AB8500_IT_LATCH21_REG 0x34
66#define AB8500_IT_LATCH22_REG 0x35
67#define AB8500_IT_LATCH23_REG 0x36
68#define AB8500_IT_LATCH24_REG 0x37
Rabin Vincent62579262010-05-19 11:39:02 +020069
70/*
71 * mask registers
72 */
73
Mattias Wallin47c16972010-09-10 17:47:56 +020074#define AB8500_IT_MASK1_REG 0x40
75#define AB8500_IT_MASK2_REG 0x41
76#define AB8500_IT_MASK3_REG 0x42
77#define AB8500_IT_MASK4_REG 0x43
78#define AB8500_IT_MASK5_REG 0x44
79#define AB8500_IT_MASK6_REG 0x45
80#define AB8500_IT_MASK7_REG 0x46
81#define AB8500_IT_MASK8_REG 0x47
82#define AB8500_IT_MASK9_REG 0x48
83#define AB8500_IT_MASK10_REG 0x49
84#define AB8500_IT_MASK11_REG 0x4A
85#define AB8500_IT_MASK12_REG 0x4B
86#define AB8500_IT_MASK13_REG 0x4C
87#define AB8500_IT_MASK14_REG 0x4D
88#define AB8500_IT_MASK15_REG 0x4E
89#define AB8500_IT_MASK16_REG 0x4F
90#define AB8500_IT_MASK17_REG 0x50
91#define AB8500_IT_MASK18_REG 0x51
92#define AB8500_IT_MASK19_REG 0x52
93#define AB8500_IT_MASK20_REG 0x53
94#define AB8500_IT_MASK21_REG 0x54
95#define AB8500_IT_MASK22_REG 0x55
96#define AB8500_IT_MASK23_REG 0x56
97#define AB8500_IT_MASK24_REG 0x57
Rabin Vincent62579262010-05-19 11:39:02 +020098
Michel JAOUEN7ccfe9b2012-05-07 15:02:03 +020099/*
100 * latch hierarchy registers
101 */
102#define AB8500_IT_LATCHHIER1_REG 0x60
103#define AB8500_IT_LATCHHIER2_REG 0x61
104#define AB8500_IT_LATCHHIER3_REG 0x62
105
106#define AB8500_IT_LATCHHIER_NUM 3
107
Mattias Wallin47c16972010-09-10 17:47:56 +0200108#define AB8500_REV_REG 0x80
Linus Walleij0f6208372012-02-20 21:42:10 +0100109#define AB8500_IC_NAME_REG 0x82
Mattias Walline5c238c2011-03-02 11:52:36 +0100110#define AB8500_SWITCH_OFF_STATUS 0x00
Rabin Vincent62579262010-05-19 11:39:02 +0200111
Andrew Lynnb4a31032011-10-11 10:49:47 +0200112#define AB8500_TURN_ON_STATUS 0x00
113
Rajkumar Kasirajanf04a9d82012-05-30 16:32:37 +0530114#define AB8500_CH_USBCH_STAT1_REG 0x02
115#define VBUS_DET_DBNC100 0x02
116#define VBUS_DET_DBNC1 0x01
117
118static DEFINE_SPINLOCK(on_stat_lock);
119static u8 turn_on_stat_mask = 0xFF;
120static u8 turn_on_stat_set;
Rickard Andersson6ef94182012-04-17 09:30:57 +0200121static bool no_bm; /* No battery management */
122module_param(no_bm, bool, S_IRUGO);
123
Linus Walleijd6255522012-02-20 21:42:24 +0100124#define AB9540_MODEM_CTRL2_REG 0x23
125#define AB9540_MODEM_CTRL2_SWDBBRSTN_BIT BIT(2)
126
Rabin Vincent62579262010-05-19 11:39:02 +0200127/*
128 * Map interrupt numbers to the LATCH and MASK register offsets, Interrupt
Linus Walleij2ced4452012-02-20 21:42:17 +0100129 * numbers are indexed into this array with (num / 8). The interupts are
130 * defined in linux/mfd/ab8500.h
Rabin Vincent62579262010-05-19 11:39:02 +0200131 *
132 * This is one off from the register names, i.e. AB8500_IT_MASK1_REG is at
133 * offset 0.
134 */
Linus Walleij2ced4452012-02-20 21:42:17 +0100135/* AB8500 support */
Rabin Vincent62579262010-05-19 11:39:02 +0200136static const int ab8500_irq_regoffset[AB8500_NUM_IRQ_REGS] = {
Mattias Wallin92d50a42010-12-07 11:20:47 +0100137 0, 1, 2, 3, 4, 6, 7, 8, 9, 11, 18, 19, 20, 21,
Rabin Vincent62579262010-05-19 11:39:02 +0200138};
139
Linus Walleijd6255522012-02-20 21:42:24 +0100140/* AB9540 support */
141static const int ab9540_irq_regoffset[AB9540_NUM_IRQ_REGS] = {
142 0, 1, 2, 3, 4, 6, 7, 8, 9, 11, 18, 19, 20, 21, 12, 13, 24,
143};
144
Linus Walleij0f6208372012-02-20 21:42:10 +0100145static const char ab8500_version_str[][7] = {
146 [AB8500_VERSION_AB8500] = "AB8500",
147 [AB8500_VERSION_AB8505] = "AB8505",
148 [AB8500_VERSION_AB9540] = "AB9540",
149 [AB8500_VERSION_AB8540] = "AB8540",
150};
151
Lee Jones822672a2012-06-20 13:56:38 +0100152static int ab8500_prcmu_write(struct ab8500 *ab8500, u16 addr, u8 data)
Lee Jonesd28f1db2012-05-19 17:21:37 +0200153{
154 int ret;
155
156 ret = prcmu_abb_write((u8)(addr >> 8), (u8)(addr & 0xFF), &data, 1);
157 if (ret < 0)
158 dev_err(ab8500->dev, "prcmu i2c error %d\n", ret);
159 return ret;
160}
161
Lee Jones822672a2012-06-20 13:56:38 +0100162static int ab8500_prcmu_write_masked(struct ab8500 *ab8500, u16 addr, u8 mask,
Lee Jonesd28f1db2012-05-19 17:21:37 +0200163 u8 data)
164{
165 int ret;
166
167 ret = prcmu_abb_write_masked((u8)(addr >> 8), (u8)(addr & 0xFF), &data,
168 &mask, 1);
169 if (ret < 0)
170 dev_err(ab8500->dev, "prcmu i2c error %d\n", ret);
171 return ret;
172}
173
Lee Jones822672a2012-06-20 13:56:38 +0100174static int ab8500_prcmu_read(struct ab8500 *ab8500, u16 addr)
Lee Jonesd28f1db2012-05-19 17:21:37 +0200175{
176 int ret;
177 u8 data;
178
179 ret = prcmu_abb_read((u8)(addr >> 8), (u8)(addr & 0xFF), &data, 1);
180 if (ret < 0) {
181 dev_err(ab8500->dev, "prcmu i2c error %d\n", ret);
182 return ret;
183 }
184 return (int)data;
185}
186
Mattias Wallin47c16972010-09-10 17:47:56 +0200187static int ab8500_get_chip_id(struct device *dev)
188{
Mattias Wallin6bce7bf2010-12-02 15:08:32 +0100189 struct ab8500 *ab8500;
190
191 if (!dev)
192 return -EINVAL;
193 ab8500 = dev_get_drvdata(dev->parent);
194 return ab8500 ? (int)ab8500->chip_id : -EINVAL;
Mattias Wallin47c16972010-09-10 17:47:56 +0200195}
196
197static int set_register_interruptible(struct ab8500 *ab8500, u8 bank,
198 u8 reg, u8 data)
Rabin Vincent62579262010-05-19 11:39:02 +0200199{
200 int ret;
Mattias Wallin47c16972010-09-10 17:47:56 +0200201 /*
202 * Put the u8 bank and u8 register together into a an u16.
203 * The bank on higher 8 bits and register in lower 8 bits.
204 * */
205 u16 addr = ((u16)bank) << 8 | reg;
Rabin Vincent62579262010-05-19 11:39:02 +0200206
207 dev_vdbg(ab8500->dev, "wr: addr %#x <= %#x\n", addr, data);
208
Rabin Vincent392cbd12012-03-08 14:01:46 +0100209 mutex_lock(&ab8500->lock);
Mattias Wallin47c16972010-09-10 17:47:56 +0200210
211 ret = ab8500->write(ab8500, addr, data);
212 if (ret < 0)
213 dev_err(ab8500->dev, "failed to write reg %#x: %d\n",
214 addr, ret);
215 mutex_unlock(&ab8500->lock);
216
217 return ret;
218}
219
220static int ab8500_set_register(struct device *dev, u8 bank,
221 u8 reg, u8 value)
222{
Jonas Aaberg112a80d2012-04-17 09:30:33 +0200223 int ret;
Mattias Wallin47c16972010-09-10 17:47:56 +0200224 struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
225
Jonas Aaberg112a80d2012-04-17 09:30:33 +0200226 atomic_inc(&ab8500->transfer_ongoing);
227 ret = set_register_interruptible(ab8500, bank, reg, value);
228 atomic_dec(&ab8500->transfer_ongoing);
229 return ret;
Mattias Wallin47c16972010-09-10 17:47:56 +0200230}
231
232static int get_register_interruptible(struct ab8500 *ab8500, u8 bank,
233 u8 reg, u8 *value)
234{
235 int ret;
236 /* put the u8 bank and u8 reg together into a an u16.
237 * bank on higher 8 bits and reg in lower */
238 u16 addr = ((u16)bank) << 8 | reg;
239
Rabin Vincent392cbd12012-03-08 14:01:46 +0100240 mutex_lock(&ab8500->lock);
Mattias Wallin47c16972010-09-10 17:47:56 +0200241
242 ret = ab8500->read(ab8500, addr);
243 if (ret < 0)
244 dev_err(ab8500->dev, "failed to read reg %#x: %d\n",
245 addr, ret);
246 else
247 *value = ret;
248
249 mutex_unlock(&ab8500->lock);
250 dev_vdbg(ab8500->dev, "rd: addr %#x => data %#x\n", addr, ret);
251
252 return ret;
253}
254
255static int ab8500_get_register(struct device *dev, u8 bank,
256 u8 reg, u8 *value)
257{
Jonas Aaberg112a80d2012-04-17 09:30:33 +0200258 int ret;
Mattias Wallin47c16972010-09-10 17:47:56 +0200259 struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
260
Jonas Aaberg112a80d2012-04-17 09:30:33 +0200261 atomic_inc(&ab8500->transfer_ongoing);
262 ret = get_register_interruptible(ab8500, bank, reg, value);
263 atomic_dec(&ab8500->transfer_ongoing);
264 return ret;
Mattias Wallin47c16972010-09-10 17:47:56 +0200265}
266
267static int mask_and_set_register_interruptible(struct ab8500 *ab8500, u8 bank,
268 u8 reg, u8 bitmask, u8 bitvalues)
269{
270 int ret;
Mattias Wallin47c16972010-09-10 17:47:56 +0200271 /* put the u8 bank and u8 reg together into a an u16.
272 * bank on higher 8 bits and reg in lower */
273 u16 addr = ((u16)bank) << 8 | reg;
274
Rabin Vincent392cbd12012-03-08 14:01:46 +0100275 mutex_lock(&ab8500->lock);
Mattias Wallin47c16972010-09-10 17:47:56 +0200276
Mattias Nilssonbc628fd2012-03-08 14:02:20 +0100277 if (ab8500->write_masked == NULL) {
278 u8 data;
279
280 ret = ab8500->read(ab8500, addr);
281 if (ret < 0) {
282 dev_err(ab8500->dev, "failed to read reg %#x: %d\n",
283 addr, ret);
284 goto out;
285 }
286
287 data = (u8)ret;
288 data = (~bitmask & data) | (bitmask & bitvalues);
289
290 ret = ab8500->write(ab8500, addr, data);
291 if (ret < 0)
292 dev_err(ab8500->dev, "failed to write reg %#x: %d\n",
293 addr, ret);
294
295 dev_vdbg(ab8500->dev, "mask: addr %#x => data %#x\n", addr,
296 data);
Mattias Wallin47c16972010-09-10 17:47:56 +0200297 goto out;
298 }
Mattias Nilssonbc628fd2012-03-08 14:02:20 +0100299 ret = ab8500->write_masked(ab8500, addr, bitmask, bitvalues);
Rabin Vincent62579262010-05-19 11:39:02 +0200300 if (ret < 0)
Mattias Nilssonbc628fd2012-03-08 14:02:20 +0100301 dev_err(ab8500->dev, "failed to modify reg %#x: %d\n", addr,
302 ret);
Rabin Vincent62579262010-05-19 11:39:02 +0200303out:
304 mutex_unlock(&ab8500->lock);
305 return ret;
306}
Mattias Wallin47c16972010-09-10 17:47:56 +0200307
308static int ab8500_mask_and_set_register(struct device *dev,
309 u8 bank, u8 reg, u8 bitmask, u8 bitvalues)
310{
Jonas Aaberg112a80d2012-04-17 09:30:33 +0200311 int ret;
Mattias Wallin47c16972010-09-10 17:47:56 +0200312 struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
313
Jonas Aaberg112a80d2012-04-17 09:30:33 +0200314 atomic_inc(&ab8500->transfer_ongoing);
315 ret= mask_and_set_register_interruptible(ab8500, bank, reg,
316 bitmask, bitvalues);
317 atomic_dec(&ab8500->transfer_ongoing);
318 return ret;
Mattias Wallin47c16972010-09-10 17:47:56 +0200319}
320
321static struct abx500_ops ab8500_ops = {
322 .get_chip_id = ab8500_get_chip_id,
323 .get_register = ab8500_get_register,
324 .set_register = ab8500_set_register,
325 .get_register_page = NULL,
326 .set_register_page = NULL,
327 .mask_and_set_register = ab8500_mask_and_set_register,
328 .event_registers_startup_state_get = NULL,
329 .startup_irq_enabled = NULL,
Mian Yousaf Kaukab1d843a62012-01-27 11:35:41 +0100330 .dump_all_banks = ab8500_dump_all_banks,
Mattias Wallin47c16972010-09-10 17:47:56 +0200331};
Rabin Vincent62579262010-05-19 11:39:02 +0200332
Mark Brown9505a0a2010-12-11 13:16:08 +0000333static void ab8500_irq_lock(struct irq_data *data)
Rabin Vincent62579262010-05-19 11:39:02 +0200334{
Mark Brown9505a0a2010-12-11 13:16:08 +0000335 struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
Rabin Vincent62579262010-05-19 11:39:02 +0200336
337 mutex_lock(&ab8500->irq_lock);
Jonas Aaberg112a80d2012-04-17 09:30:33 +0200338 atomic_inc(&ab8500->transfer_ongoing);
Rabin Vincent62579262010-05-19 11:39:02 +0200339}
340
Mark Brown9505a0a2010-12-11 13:16:08 +0000341static void ab8500_irq_sync_unlock(struct irq_data *data)
Rabin Vincent62579262010-05-19 11:39:02 +0200342{
Mark Brown9505a0a2010-12-11 13:16:08 +0000343 struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
Rabin Vincent62579262010-05-19 11:39:02 +0200344 int i;
345
Linus Walleij2ced4452012-02-20 21:42:17 +0100346 for (i = 0; i < ab8500->mask_size; i++) {
Rabin Vincent62579262010-05-19 11:39:02 +0200347 u8 old = ab8500->oldmask[i];
348 u8 new = ab8500->mask[i];
349 int reg;
350
351 if (new == old)
352 continue;
353
Linus Walleij0f6208372012-02-20 21:42:10 +0100354 /*
355 * Interrupt register 12 doesn't exist prior to AB8500 version
356 * 2.0
357 */
358 if (ab8500->irq_reg_offset[i] == 11 &&
359 is_ab8500_1p1_or_earlier(ab8500))
Mattias Wallin92d50a42010-12-07 11:20:47 +0100360 continue;
361
Rabin Vincent62579262010-05-19 11:39:02 +0200362 ab8500->oldmask[i] = new;
363
Linus Walleij2ced4452012-02-20 21:42:17 +0100364 reg = AB8500_IT_MASK1_REG + ab8500->irq_reg_offset[i];
Mattias Wallin47c16972010-09-10 17:47:56 +0200365 set_register_interruptible(ab8500, AB8500_INTERRUPT, reg, new);
Rabin Vincent62579262010-05-19 11:39:02 +0200366 }
Jonas Aaberg112a80d2012-04-17 09:30:33 +0200367 atomic_dec(&ab8500->transfer_ongoing);
Rabin Vincent62579262010-05-19 11:39:02 +0200368 mutex_unlock(&ab8500->irq_lock);
369}
370
Mark Brown9505a0a2010-12-11 13:16:08 +0000371static void ab8500_irq_mask(struct irq_data *data)
Rabin Vincent62579262010-05-19 11:39:02 +0200372{
Mark Brown9505a0a2010-12-11 13:16:08 +0000373 struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
Lee Jones06e589e2012-06-20 13:56:37 +0100374 int offset = data->hwirq;
Rabin Vincent62579262010-05-19 11:39:02 +0200375 int index = offset / 8;
376 int mask = 1 << (offset % 8);
377
378 ab8500->mask[index] |= mask;
Lee Jones9c677b92012-12-20 11:23:42 +0000379
380 /* The AB8500 GPIOs have two interrupts each (rising & falling). */
381 if (offset >= AB8500_INT_GPIO6R && offset <= AB8500_INT_GPIO41R)
382 ab8500->mask[index + 2] |= mask;
383 if (offset >= AB9540_INT_GPIO50R && offset <= AB9540_INT_GPIO54R)
384 ab8500->mask[index + 1] |= mask;
385 if (offset == AB8540_INT_GPIO43R || offset == AB8540_INT_GPIO44R)
Linus Walleije2ddf462013-02-06 21:54:34 +0100386 /* Here the falling IRQ is one bit lower */
387 ab8500->mask[index] |= (mask << 1);
Rabin Vincent62579262010-05-19 11:39:02 +0200388}
389
Mark Brown9505a0a2010-12-11 13:16:08 +0000390static void ab8500_irq_unmask(struct irq_data *data)
Rabin Vincent62579262010-05-19 11:39:02 +0200391{
Mark Brown9505a0a2010-12-11 13:16:08 +0000392 struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
Lee Jones9c677b92012-12-20 11:23:42 +0000393 unsigned int type = irqd_get_trigger_type(data);
Lee Jones06e589e2012-06-20 13:56:37 +0100394 int offset = data->hwirq;
Rabin Vincent62579262010-05-19 11:39:02 +0200395 int index = offset / 8;
396 int mask = 1 << (offset % 8);
397
Lee Jones9c677b92012-12-20 11:23:42 +0000398 if (type & IRQ_TYPE_EDGE_RISING)
399 ab8500->mask[index] &= ~mask;
400
401 /* The AB8500 GPIOs have two interrupts each (rising & falling). */
402 if (type & IRQ_TYPE_EDGE_FALLING) {
403 if (offset >= AB8500_INT_GPIO6R && offset <= AB8500_INT_GPIO41R)
404 ab8500->mask[index + 2] &= ~mask;
405 else if (offset >= AB9540_INT_GPIO50R && offset <= AB9540_INT_GPIO54R)
406 ab8500->mask[index + 1] &= ~mask;
407 else if (offset == AB8540_INT_GPIO43R || offset == AB8540_INT_GPIO44R)
Linus Walleije2ddf462013-02-06 21:54:34 +0100408 /* Here the falling IRQ is one bit lower */
409 ab8500->mask[index] &= ~(mask << 1);
Lee Jones9c677b92012-12-20 11:23:42 +0000410 else
411 ab8500->mask[index] &= ~mask;
Linus Walleije2ddf462013-02-06 21:54:34 +0100412 } else {
Lee Jones9c677b92012-12-20 11:23:42 +0000413 /* Satisfies the case where type is not set. */
414 ab8500->mask[index] &= ~mask;
Linus Walleije2ddf462013-02-06 21:54:34 +0100415 }
Rabin Vincent62579262010-05-19 11:39:02 +0200416}
417
Lee Jones40f6e5a2013-01-07 12:23:48 +0000418static int ab8500_irq_set_type(struct irq_data *data, unsigned int type)
419{
420 return 0;
Rabin Vincent62579262010-05-19 11:39:02 +0200421}
422
423static struct irq_chip ab8500_irq_chip = {
424 .name = "ab8500",
Mark Brown9505a0a2010-12-11 13:16:08 +0000425 .irq_bus_lock = ab8500_irq_lock,
426 .irq_bus_sync_unlock = ab8500_irq_sync_unlock,
427 .irq_mask = ab8500_irq_mask,
Virupax Sadashivpetimathe6f93062011-10-11 10:49:17 +0200428 .irq_disable = ab8500_irq_mask,
Mark Brown9505a0a2010-12-11 13:16:08 +0000429 .irq_unmask = ab8500_irq_unmask,
Lee Jones40f6e5a2013-01-07 12:23:48 +0000430 .irq_set_type = ab8500_irq_set_type,
Rabin Vincent62579262010-05-19 11:39:02 +0200431};
432
Michel JAOUEN7ccfe9b2012-05-07 15:02:03 +0200433static int ab8500_handle_hierarchical_line(struct ab8500 *ab8500,
434 int latch_offset, u8 latch_val)
435{
436 int int_bit = __ffs(latch_val);
437 int line, i;
438
439 do {
440 int_bit = __ffs(latch_val);
441
442 for (i = 0; i < ab8500->mask_size; i++)
443 if (ab8500->irq_reg_offset[i] == latch_offset)
444 break;
445
446 if (i >= ab8500->mask_size) {
447 dev_err(ab8500->dev, "Register offset 0x%2x not declared\n",
448 latch_offset);
449 return -ENXIO;
450 }
451
452 line = (i << 3) + int_bit;
453 latch_val &= ~(1 << int_bit);
454
Linus Walleije2ddf462013-02-06 21:54:34 +0100455 /*
456 * This handles the falling edge hwirqs from the GPIO
457 * lines. Route them back to the line registered for the
458 * rising IRQ, as this is merely a flag for the same IRQ
459 * in linux terms.
460 */
461 if (line >= AB8500_INT_GPIO6F && line <= AB8500_INT_GPIO41F)
462 line -= 16;
463 if (line >= AB9540_INT_GPIO50F && line <= AB9540_INT_GPIO54F)
464 line -= 8;
465 if (line == AB8540_INT_GPIO43F || line == AB8540_INT_GPIO44F)
466 line += 1;
467
Michel JAOUEN7ccfe9b2012-05-07 15:02:03 +0200468 handle_nested_irq(ab8500->irq_base + line);
469 } while (latch_val);
470
471 return 0;
472}
473
474static int ab8500_handle_hierarchical_latch(struct ab8500 *ab8500,
475 int hier_offset, u8 hier_val)
476{
477 int latch_bit, status;
478 u8 latch_offset, latch_val;
479
480 do {
481 latch_bit = __ffs(hier_val);
482 latch_offset = (hier_offset << 3) + latch_bit;
483
484 /* Fix inconsistent ITFromLatch25 bit mapping... */
485 if (unlikely(latch_offset == 17))
486 latch_offset = 24;
487
488 status = get_register_interruptible(ab8500,
489 AB8500_INTERRUPT,
490 AB8500_IT_LATCH1_REG + latch_offset,
491 &latch_val);
492 if (status < 0 || latch_val == 0)
493 goto discard;
494
495 status = ab8500_handle_hierarchical_line(ab8500,
496 latch_offset, latch_val);
497 if (status < 0)
498 return status;
499discard:
500 hier_val &= ~(1 << latch_bit);
501 } while (hier_val);
502
503 return 0;
504}
505
506static irqreturn_t ab8500_hierarchical_irq(int irq, void *dev)
507{
508 struct ab8500 *ab8500 = dev;
509 u8 i;
510
511 dev_vdbg(ab8500->dev, "interrupt\n");
512
513 /* Hierarchical interrupt version */
514 for (i = 0; i < AB8500_IT_LATCHHIER_NUM; i++) {
515 int status;
516 u8 hier_val;
517
518 status = get_register_interruptible(ab8500, AB8500_INTERRUPT,
519 AB8500_IT_LATCHHIER1_REG + i, &hier_val);
520 if (status < 0 || hier_val == 0)
521 continue;
522
523 status = ab8500_handle_hierarchical_latch(ab8500, i, hier_val);
524 if (status < 0)
525 break;
526 }
527 return IRQ_HANDLED;
528}
529
Lee Jones80633f02012-08-20 11:53:36 +0100530/**
531 * ab8500_irq_get_virq(): Map an interrupt on a chip to a virtual IRQ
532 *
533 * @ab8500: ab8500_irq controller to operate on.
534 * @irq: index of the interrupt requested in the chip IRQs
535 *
536 * Useful for drivers to request their own IRQs.
537 */
538static int ab8500_irq_get_virq(struct ab8500 *ab8500, int irq)
539{
540 if (!ab8500)
541 return -EINVAL;
542
543 return irq_create_mapping(ab8500->domain, irq);
544}
545
Rabin Vincent62579262010-05-19 11:39:02 +0200546static irqreturn_t ab8500_irq(int irq, void *dev)
547{
548 struct ab8500 *ab8500 = dev;
549 int i;
550
551 dev_vdbg(ab8500->dev, "interrupt\n");
552
Jonas Aaberg112a80d2012-04-17 09:30:33 +0200553 atomic_inc(&ab8500->transfer_ongoing);
554
Linus Walleij2ced4452012-02-20 21:42:17 +0100555 for (i = 0; i < ab8500->mask_size; i++) {
556 int regoffset = ab8500->irq_reg_offset[i];
Rabin Vincent62579262010-05-19 11:39:02 +0200557 int status;
Mattias Wallin47c16972010-09-10 17:47:56 +0200558 u8 value;
Rabin Vincent62579262010-05-19 11:39:02 +0200559
Linus Walleij0f6208372012-02-20 21:42:10 +0100560 /*
561 * Interrupt register 12 doesn't exist prior to AB8500 version
562 * 2.0
563 */
564 if (regoffset == 11 && is_ab8500_1p1_or_earlier(ab8500))
Mattias Wallin92d50a42010-12-07 11:20:47 +0100565 continue;
566
Mattias Wallin47c16972010-09-10 17:47:56 +0200567 status = get_register_interruptible(ab8500, AB8500_INTERRUPT,
568 AB8500_IT_LATCH1_REG + regoffset, &value);
569 if (status < 0 || value == 0)
Rabin Vincent62579262010-05-19 11:39:02 +0200570 continue;
571
572 do {
Mattias Wallin88aec4f2010-12-02 15:06:49 +0100573 int bit = __ffs(value);
Rabin Vincent62579262010-05-19 11:39:02 +0200574 int line = i * 8 + bit;
Lee Jones0a37fc52012-08-09 16:53:54 +0100575 int virq = ab8500_irq_get_virq(ab8500, line);
Rabin Vincent62579262010-05-19 11:39:02 +0200576
Lee Jones0a37fc52012-08-09 16:53:54 +0100577 handle_nested_irq(virq);
Bengt Jonsson8f0eb432012-02-14 13:01:00 +0100578 ab8500_debug_register_interrupt(line);
Mattias Wallin47c16972010-09-10 17:47:56 +0200579 value &= ~(1 << bit);
Jonas Aaberg112a80d2012-04-17 09:30:33 +0200580
Mattias Wallin47c16972010-09-10 17:47:56 +0200581 } while (value);
Rabin Vincent62579262010-05-19 11:39:02 +0200582 }
Jonas Aaberg112a80d2012-04-17 09:30:33 +0200583 atomic_dec(&ab8500->transfer_ongoing);
Rabin Vincent62579262010-05-19 11:39:02 +0200584 return IRQ_HANDLED;
585}
586
Lee Jones06e589e2012-06-20 13:56:37 +0100587static int ab8500_irq_map(struct irq_domain *d, unsigned int virq,
588 irq_hw_number_t hwirq)
589{
590 struct ab8500 *ab8500 = d->host_data;
591
592 if (!ab8500)
593 return -EINVAL;
594
595 irq_set_chip_data(virq, ab8500);
596 irq_set_chip_and_handler(virq, &ab8500_irq_chip,
597 handle_simple_irq);
598 irq_set_nested_thread(virq, 1);
Rabin Vincent62579262010-05-19 11:39:02 +0200599#ifdef CONFIG_ARM
Lee Jones06e589e2012-06-20 13:56:37 +0100600 set_irq_flags(virq, IRQF_VALID);
Rabin Vincent62579262010-05-19 11:39:02 +0200601#else
Lee Jones06e589e2012-06-20 13:56:37 +0100602 irq_set_noprobe(virq);
Rabin Vincent62579262010-05-19 11:39:02 +0200603#endif
Rabin Vincent62579262010-05-19 11:39:02 +0200604
605 return 0;
606}
607
Lee Jones06e589e2012-06-20 13:56:37 +0100608static struct irq_domain_ops ab8500_irq_ops = {
609 .map = ab8500_irq_map,
610 .xlate = irq_domain_xlate_twocell,
611};
612
613static int ab8500_irq_init(struct ab8500 *ab8500, struct device_node *np)
Rabin Vincent62579262010-05-19 11:39:02 +0200614{
Linus Walleij2ced4452012-02-20 21:42:17 +0100615 int num_irqs;
Rabin Vincent62579262010-05-19 11:39:02 +0200616
Linus Walleijd6255522012-02-20 21:42:24 +0100617 if (is_ab9540(ab8500))
618 num_irqs = AB9540_NR_IRQS;
Bengt Jonssona9823622012-03-08 14:01:57 +0100619 else if (is_ab8505(ab8500))
620 num_irqs = AB8505_NR_IRQS;
Linus Walleijd6255522012-02-20 21:42:24 +0100621 else
622 num_irqs = AB8500_NR_IRQS;
Linus Walleij2ced4452012-02-20 21:42:17 +0100623
Linus Walleijf1d11f32012-10-18 18:18:44 +0200624 /* If ->irq_base is zero this will give a linear mapping */
625 ab8500->domain = irq_domain_add_simple(NULL,
626 num_irqs, ab8500->irq_base,
627 &ab8500_irq_ops, ab8500);
Lee Jones06e589e2012-06-20 13:56:37 +0100628
629 if (!ab8500->domain) {
630 dev_err(ab8500->dev, "Failed to create irqdomain\n");
631 return -ENOSYS;
632 }
633
634 return 0;
Rabin Vincent62579262010-05-19 11:39:02 +0200635}
636
Jonas Aaberg112a80d2012-04-17 09:30:33 +0200637int ab8500_suspend(struct ab8500 *ab8500)
638{
639 if (atomic_read(&ab8500->transfer_ongoing))
640 return -EINVAL;
641 else
642 return 0;
643}
644
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500645static struct resource ab8500_gpadc_resources[] = {
Rabin Vincent62579262010-05-19 11:39:02 +0200646 {
647 .name = "HW_CONV_END",
648 .start = AB8500_INT_GP_HW_ADC_CONV_END,
649 .end = AB8500_INT_GP_HW_ADC_CONV_END,
650 .flags = IORESOURCE_IRQ,
651 },
652 {
653 .name = "SW_CONV_END",
654 .start = AB8500_INT_GP_SW_ADC_CONV_END,
655 .end = AB8500_INT_GP_SW_ADC_CONV_END,
656 .flags = IORESOURCE_IRQ,
657 },
658};
659
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500660static struct resource ab8500_rtc_resources[] = {
Rabin Vincent62579262010-05-19 11:39:02 +0200661 {
662 .name = "60S",
663 .start = AB8500_INT_RTC_60S,
664 .end = AB8500_INT_RTC_60S,
665 .flags = IORESOURCE_IRQ,
666 },
667 {
668 .name = "ALARM",
669 .start = AB8500_INT_RTC_ALARM,
670 .end = AB8500_INT_RTC_ALARM,
671 .flags = IORESOURCE_IRQ,
672 },
673};
674
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500675static struct resource ab8500_poweronkey_db_resources[] = {
Sundar R Iyer77686512010-09-05 12:18:47 -0700676 {
677 .name = "ONKEY_DBF",
678 .start = AB8500_INT_PON_KEY1DB_F,
679 .end = AB8500_INT_PON_KEY1DB_F,
680 .flags = IORESOURCE_IRQ,
681 },
682 {
683 .name = "ONKEY_DBR",
684 .start = AB8500_INT_PON_KEY1DB_R,
685 .end = AB8500_INT_PON_KEY1DB_R,
686 .flags = IORESOURCE_IRQ,
687 },
688};
689
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500690static struct resource ab8500_av_acc_detect_resources[] = {
Mattias Walline098aded72010-12-02 15:40:31 +0100691 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200692 .name = "ACC_DETECT_1DB_F",
693 .start = AB8500_INT_ACC_DETECT_1DB_F,
694 .end = AB8500_INT_ACC_DETECT_1DB_F,
695 .flags = IORESOURCE_IRQ,
Mattias Walline098aded72010-12-02 15:40:31 +0100696 },
697 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200698 .name = "ACC_DETECT_1DB_R",
699 .start = AB8500_INT_ACC_DETECT_1DB_R,
700 .end = AB8500_INT_ACC_DETECT_1DB_R,
701 .flags = IORESOURCE_IRQ,
Mattias Walline098aded72010-12-02 15:40:31 +0100702 },
703 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200704 .name = "ACC_DETECT_21DB_F",
705 .start = AB8500_INT_ACC_DETECT_21DB_F,
706 .end = AB8500_INT_ACC_DETECT_21DB_F,
707 .flags = IORESOURCE_IRQ,
708 },
709 {
710 .name = "ACC_DETECT_21DB_R",
711 .start = AB8500_INT_ACC_DETECT_21DB_R,
712 .end = AB8500_INT_ACC_DETECT_21DB_R,
713 .flags = IORESOURCE_IRQ,
714 },
715 {
716 .name = "ACC_DETECT_22DB_F",
717 .start = AB8500_INT_ACC_DETECT_22DB_F,
718 .end = AB8500_INT_ACC_DETECT_22DB_F,
719 .flags = IORESOURCE_IRQ,
720 },
721 {
722 .name = "ACC_DETECT_22DB_R",
723 .start = AB8500_INT_ACC_DETECT_22DB_R,
724 .end = AB8500_INT_ACC_DETECT_22DB_R,
725 .flags = IORESOURCE_IRQ,
726 },
727};
728
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500729static struct resource ab8500_charger_resources[] = {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200730 {
Mattias Walline098aded72010-12-02 15:40:31 +0100731 .name = "MAIN_CH_UNPLUG_DET",
732 .start = AB8500_INT_MAIN_CH_UNPLUG_DET,
733 .end = AB8500_INT_MAIN_CH_UNPLUG_DET,
734 .flags = IORESOURCE_IRQ,
735 },
736 {
737 .name = "MAIN_CHARGE_PLUG_DET",
738 .start = AB8500_INT_MAIN_CH_PLUG_DET,
739 .end = AB8500_INT_MAIN_CH_PLUG_DET,
740 .flags = IORESOURCE_IRQ,
741 },
742 {
Mattias Walline098aded72010-12-02 15:40:31 +0100743 .name = "VBUS_DET_R",
744 .start = AB8500_INT_VBUS_DET_R,
745 .end = AB8500_INT_VBUS_DET_R,
746 .flags = IORESOURCE_IRQ,
747 },
748 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200749 .name = "VBUS_DET_F",
750 .start = AB8500_INT_VBUS_DET_F,
751 .end = AB8500_INT_VBUS_DET_F,
Mattias Walline098aded72010-12-02 15:40:31 +0100752 .flags = IORESOURCE_IRQ,
753 },
754 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200755 .name = "USB_LINK_STATUS",
756 .start = AB8500_INT_USB_LINK_STATUS,
757 .end = AB8500_INT_USB_LINK_STATUS,
758 .flags = IORESOURCE_IRQ,
759 },
760 {
Mattias Walline098aded72010-12-02 15:40:31 +0100761 .name = "VBUS_OVV",
762 .start = AB8500_INT_VBUS_OVV,
763 .end = AB8500_INT_VBUS_OVV,
764 .flags = IORESOURCE_IRQ,
765 },
766 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200767 .name = "USB_CH_TH_PROT_R",
768 .start = AB8500_INT_USB_CH_TH_PROT_R,
769 .end = AB8500_INT_USB_CH_TH_PROT_R,
Mattias Walline098aded72010-12-02 15:40:31 +0100770 .flags = IORESOURCE_IRQ,
771 },
772 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200773 .name = "USB_CH_TH_PROT_F",
774 .start = AB8500_INT_USB_CH_TH_PROT_F,
775 .end = AB8500_INT_USB_CH_TH_PROT_F,
Mattias Walline098aded72010-12-02 15:40:31 +0100776 .flags = IORESOURCE_IRQ,
777 },
778 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200779 .name = "MAIN_EXT_CH_NOT_OK",
780 .start = AB8500_INT_MAIN_EXT_CH_NOT_OK,
781 .end = AB8500_INT_MAIN_EXT_CH_NOT_OK,
782 .flags = IORESOURCE_IRQ,
783 },
784 {
785 .name = "MAIN_CH_TH_PROT_R",
786 .start = AB8500_INT_MAIN_CH_TH_PROT_R,
787 .end = AB8500_INT_MAIN_CH_TH_PROT_R,
788 .flags = IORESOURCE_IRQ,
789 },
790 {
791 .name = "MAIN_CH_TH_PROT_F",
792 .start = AB8500_INT_MAIN_CH_TH_PROT_F,
793 .end = AB8500_INT_MAIN_CH_TH_PROT_F,
794 .flags = IORESOURCE_IRQ,
795 },
796 {
797 .name = "USB_CHARGER_NOT_OKR",
Bengt Jonssona9823622012-03-08 14:01:57 +0100798 .start = AB8500_INT_USB_CHARGER_NOT_OKR,
799 .end = AB8500_INT_USB_CHARGER_NOT_OKR,
Linus Walleij6af75ec2011-06-09 23:57:45 +0200800 .flags = IORESOURCE_IRQ,
801 },
802 {
803 .name = "CH_WD_EXP",
804 .start = AB8500_INT_CH_WD_EXP,
805 .end = AB8500_INT_CH_WD_EXP,
806 .flags = IORESOURCE_IRQ,
807 },
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100808 {
809 .name = "VBUS_CH_DROP_END",
810 .start = AB8500_INT_VBUS_CH_DROP_END,
811 .end = AB8500_INT_VBUS_CH_DROP_END,
812 .flags = IORESOURCE_IRQ,
813 },
Linus Walleij6af75ec2011-06-09 23:57:45 +0200814};
815
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500816static struct resource ab8500_btemp_resources[] = {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200817 {
818 .name = "BAT_CTRL_INDB",
819 .start = AB8500_INT_BAT_CTRL_INDB,
820 .end = AB8500_INT_BAT_CTRL_INDB,
Mattias Walline098aded72010-12-02 15:40:31 +0100821 .flags = IORESOURCE_IRQ,
822 },
823 {
824 .name = "BTEMP_LOW",
825 .start = AB8500_INT_BTEMP_LOW,
826 .end = AB8500_INT_BTEMP_LOW,
827 .flags = IORESOURCE_IRQ,
828 },
829 {
830 .name = "BTEMP_HIGH",
831 .start = AB8500_INT_BTEMP_HIGH,
832 .end = AB8500_INT_BTEMP_HIGH,
833 .flags = IORESOURCE_IRQ,
834 },
835 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200836 .name = "BTEMP_LOW_MEDIUM",
837 .start = AB8500_INT_BTEMP_LOW_MEDIUM,
838 .end = AB8500_INT_BTEMP_LOW_MEDIUM,
Mattias Walline098aded72010-12-02 15:40:31 +0100839 .flags = IORESOURCE_IRQ,
840 },
841 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200842 .name = "BTEMP_MEDIUM_HIGH",
843 .start = AB8500_INT_BTEMP_MEDIUM_HIGH,
844 .end = AB8500_INT_BTEMP_MEDIUM_HIGH,
Mattias Walline098aded72010-12-02 15:40:31 +0100845 .flags = IORESOURCE_IRQ,
846 },
847};
848
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500849static struct resource ab8500_fg_resources[] = {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200850 {
851 .name = "NCONV_ACCU",
852 .start = AB8500_INT_CCN_CONV_ACC,
853 .end = AB8500_INT_CCN_CONV_ACC,
854 .flags = IORESOURCE_IRQ,
855 },
856 {
857 .name = "BATT_OVV",
858 .start = AB8500_INT_BATT_OVV,
859 .end = AB8500_INT_BATT_OVV,
860 .flags = IORESOURCE_IRQ,
861 },
862 {
863 .name = "LOW_BAT_F",
864 .start = AB8500_INT_LOW_BAT_F,
865 .end = AB8500_INT_LOW_BAT_F,
866 .flags = IORESOURCE_IRQ,
867 },
868 {
869 .name = "LOW_BAT_R",
870 .start = AB8500_INT_LOW_BAT_R,
871 .end = AB8500_INT_LOW_BAT_R,
872 .flags = IORESOURCE_IRQ,
873 },
874 {
875 .name = "CC_INT_CALIB",
876 .start = AB8500_INT_CC_INT_CALIB,
877 .end = AB8500_INT_CC_INT_CALIB,
878 .flags = IORESOURCE_IRQ,
879 },
Bengt Jonssona9823622012-03-08 14:01:57 +0100880 {
881 .name = "CCEOC",
882 .start = AB8500_INT_CCEOC,
883 .end = AB8500_INT_CCEOC,
884 .flags = IORESOURCE_IRQ,
885 },
Linus Walleij6af75ec2011-06-09 23:57:45 +0200886};
887
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500888static struct resource ab8500_chargalg_resources[] = {};
Linus Walleij6af75ec2011-06-09 23:57:45 +0200889
Axel Lindf720642011-11-10 09:56:18 +0800890#ifdef CONFIG_DEBUG_FS
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500891static struct resource ab8500_debug_resources[] = {
Mattias Walline098aded72010-12-02 15:40:31 +0100892 {
893 .name = "IRQ_FIRST",
894 .start = AB8500_INT_MAIN_EXT_CH_NOT_OK,
895 .end = AB8500_INT_MAIN_EXT_CH_NOT_OK,
896 .flags = IORESOURCE_IRQ,
897 },
898 {
899 .name = "IRQ_LAST",
Bengt Jonssona9823622012-03-08 14:01:57 +0100900 .start = AB8500_INT_XTAL32K_KO,
901 .end = AB8500_INT_XTAL32K_KO,
Mattias Walline098aded72010-12-02 15:40:31 +0100902 .flags = IORESOURCE_IRQ,
903 },
904};
Axel Lindf720642011-11-10 09:56:18 +0800905#endif
Mattias Walline098aded72010-12-02 15:40:31 +0100906
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500907static struct resource ab8500_usb_resources[] = {
Mattias Walline098aded72010-12-02 15:40:31 +0100908 {
909 .name = "ID_WAKEUP_R",
910 .start = AB8500_INT_ID_WAKEUP_R,
911 .end = AB8500_INT_ID_WAKEUP_R,
912 .flags = IORESOURCE_IRQ,
913 },
914 {
915 .name = "ID_WAKEUP_F",
916 .start = AB8500_INT_ID_WAKEUP_F,
917 .end = AB8500_INT_ID_WAKEUP_F,
918 .flags = IORESOURCE_IRQ,
919 },
920 {
921 .name = "VBUS_DET_F",
922 .start = AB8500_INT_VBUS_DET_F,
923 .end = AB8500_INT_VBUS_DET_F,
924 .flags = IORESOURCE_IRQ,
925 },
926 {
927 .name = "VBUS_DET_R",
928 .start = AB8500_INT_VBUS_DET_R,
929 .end = AB8500_INT_VBUS_DET_R,
930 .flags = IORESOURCE_IRQ,
931 },
Mattias Wallin92d50a42010-12-07 11:20:47 +0100932 {
933 .name = "USB_LINK_STATUS",
934 .start = AB8500_INT_USB_LINK_STATUS,
935 .end = AB8500_INT_USB_LINK_STATUS,
936 .flags = IORESOURCE_IRQ,
937 },
Linus Walleij6af75ec2011-06-09 23:57:45 +0200938 {
939 .name = "USB_ADP_PROBE_PLUG",
940 .start = AB8500_INT_ADP_PROBE_PLUG,
941 .end = AB8500_INT_ADP_PROBE_PLUG,
942 .flags = IORESOURCE_IRQ,
943 },
944 {
945 .name = "USB_ADP_PROBE_UNPLUG",
946 .start = AB8500_INT_ADP_PROBE_UNPLUG,
947 .end = AB8500_INT_ADP_PROBE_UNPLUG,
948 .flags = IORESOURCE_IRQ,
949 },
Mattias Walline098aded72010-12-02 15:40:31 +0100950};
951
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500952static struct resource ab8505_iddet_resources[] = {
Virupax Sadashivpetimath44f72e52012-04-17 09:30:14 +0200953 {
954 .name = "KeyDeglitch",
955 .start = AB8505_INT_KEYDEGLITCH,
956 .end = AB8505_INT_KEYDEGLITCH,
957 .flags = IORESOURCE_IRQ,
958 },
959 {
960 .name = "KP",
961 .start = AB8505_INT_KP,
962 .end = AB8505_INT_KP,
963 .flags = IORESOURCE_IRQ,
964 },
965 {
966 .name = "IKP",
967 .start = AB8505_INT_IKP,
968 .end = AB8505_INT_IKP,
969 .flags = IORESOURCE_IRQ,
970 },
971 {
972 .name = "IKR",
973 .start = AB8505_INT_IKR,
974 .end = AB8505_INT_IKR,
975 .flags = IORESOURCE_IRQ,
976 },
977 {
978 .name = "KeyStuck",
979 .start = AB8505_INT_KEYSTUCK,
980 .end = AB8505_INT_KEYSTUCK,
981 .flags = IORESOURCE_IRQ,
982 },
983};
984
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500985static struct resource ab8500_temp_resources[] = {
Mattias Walline098aded72010-12-02 15:40:31 +0100986 {
Hongbo Zhang151621a2013-01-30 18:21:27 +0800987 .name = "ABX500_TEMP_WARM",
Mattias Walline098aded72010-12-02 15:40:31 +0100988 .start = AB8500_INT_TEMP_WARM,
989 .end = AB8500_INT_TEMP_WARM,
990 .flags = IORESOURCE_IRQ,
991 },
992};
993
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500994static struct mfd_cell abx500_common_devs[] = {
Mattias Wallin5814fc32010-09-13 16:05:04 +0200995#ifdef CONFIG_DEBUG_FS
996 {
997 .name = "ab8500-debug",
Lee Jonesbad76992012-07-02 17:10:56 +0200998 .of_compatible = "stericsson,ab8500-debug",
Mattias Walline098aded72010-12-02 15:40:31 +0100999 .num_resources = ARRAY_SIZE(ab8500_debug_resources),
1000 .resources = ab8500_debug_resources,
Mattias Wallin5814fc32010-09-13 16:05:04 +02001001 },
1002#endif
Rabin Vincent62579262010-05-19 11:39:02 +02001003 {
Mattias Walline098aded72010-12-02 15:40:31 +01001004 .name = "ab8500-sysctrl",
Lee Jonesbad76992012-07-02 17:10:56 +02001005 .of_compatible = "stericsson,ab8500-sysctrl",
Mattias Walline098aded72010-12-02 15:40:31 +01001006 },
1007 {
1008 .name = "ab8500-regulator",
Lee Jonesbad76992012-07-02 17:10:56 +02001009 .of_compatible = "stericsson,ab8500-regulator",
Mattias Walline098aded72010-12-02 15:40:31 +01001010 },
1011 {
Ulf Hansson916a8712012-11-22 11:35:40 +01001012 .name = "abx500-clk",
1013 .of_compatible = "stericsson,abx500-clk",
1014 },
1015 {
Rabin Vincent62579262010-05-19 11:39:02 +02001016 .name = "ab8500-gpadc",
Lee Jonesbad76992012-07-02 17:10:56 +02001017 .of_compatible = "stericsson,ab8500-gpadc",
Rabin Vincent62579262010-05-19 11:39:02 +02001018 .num_resources = ARRAY_SIZE(ab8500_gpadc_resources),
1019 .resources = ab8500_gpadc_resources,
1020 },
1021 {
1022 .name = "ab8500-rtc",
Lee Jonesbad76992012-07-02 17:10:56 +02001023 .of_compatible = "stericsson,ab8500-rtc",
Rabin Vincent62579262010-05-19 11:39:02 +02001024 .num_resources = ARRAY_SIZE(ab8500_rtc_resources),
1025 .resources = ab8500_rtc_resources,
1026 },
Arun Murthyf0f05b12010-09-06 12:24:52 +05301027 {
Linus Walleij6af75ec2011-06-09 23:57:45 +02001028 .name = "ab8500-acc-det",
Lee Jonesbad76992012-07-02 17:10:56 +02001029 .of_compatible = "stericsson,ab8500-acc-det",
Linus Walleij6af75ec2011-06-09 23:57:45 +02001030 .num_resources = ARRAY_SIZE(ab8500_av_acc_detect_resources),
1031 .resources = ab8500_av_acc_detect_resources,
1032 },
1033 {
Mattias Walline098aded72010-12-02 15:40:31 +01001034 .name = "ab8500-poweron-key",
Lee Jonesbad76992012-07-02 17:10:56 +02001035 .of_compatible = "stericsson,ab8500-poweron-key",
Mattias Walline098aded72010-12-02 15:40:31 +01001036 .num_resources = ARRAY_SIZE(ab8500_poweronkey_db_resources),
1037 .resources = ab8500_poweronkey_db_resources,
1038 },
1039 {
Arun Murthyf0f05b12010-09-06 12:24:52 +05301040 .name = "ab8500-pwm",
Lee Jonesbad76992012-07-02 17:10:56 +02001041 .of_compatible = "stericsson,ab8500-pwm",
Arun Murthyf0f05b12010-09-06 12:24:52 +05301042 .id = 1,
1043 },
1044 {
1045 .name = "ab8500-pwm",
Lee Jonesbad76992012-07-02 17:10:56 +02001046 .of_compatible = "stericsson,ab8500-pwm",
Arun Murthyf0f05b12010-09-06 12:24:52 +05301047 .id = 2,
1048 },
1049 {
1050 .name = "ab8500-pwm",
Lee Jonesbad76992012-07-02 17:10:56 +02001051 .of_compatible = "stericsson,ab8500-pwm",
Arun Murthyf0f05b12010-09-06 12:24:52 +05301052 .id = 3,
1053 },
Lee Jonesbad76992012-07-02 17:10:56 +02001054 {
1055 .name = "ab8500-leds",
1056 .of_compatible = "stericsson,ab8500-leds",
1057 },
Sundar R Iyer77686512010-09-05 12:18:47 -07001058 {
Mattias Walline098aded72010-12-02 15:40:31 +01001059 .name = "ab8500-denc",
Lee Jonesbad76992012-07-02 17:10:56 +02001060 .of_compatible = "stericsson,ab8500-denc",
Mattias Walline098aded72010-12-02 15:40:31 +01001061 },
1062 {
Hongbo Zhang151621a2013-01-30 18:21:27 +08001063 .name = "abx500-temp",
1064 .of_compatible = "stericsson,abx500-temp",
Mattias Walline098aded72010-12-02 15:40:31 +01001065 .num_resources = ARRAY_SIZE(ab8500_temp_resources),
1066 .resources = ab8500_temp_resources,
Sundar R Iyer77686512010-09-05 12:18:47 -07001067 },
Rabin Vincent62579262010-05-19 11:39:02 +02001068};
1069
Bill Pembertona9e9ce42012-11-19 13:24:21 -05001070static struct mfd_cell ab8500_bm_devs[] = {
Rickard Andersson6ef94182012-04-17 09:30:57 +02001071 {
1072 .name = "ab8500-charger",
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08001073 .of_compatible = "stericsson,ab8500-charger",
Rickard Andersson6ef94182012-04-17 09:30:57 +02001074 .num_resources = ARRAY_SIZE(ab8500_charger_resources),
1075 .resources = ab8500_charger_resources,
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08001076 .platform_data = &ab8500_bm_data,
1077 .pdata_size = sizeof(ab8500_bm_data),
Rickard Andersson6ef94182012-04-17 09:30:57 +02001078 },
1079 {
1080 .name = "ab8500-btemp",
Rajanikanth H.Vbd9e8ab2012-11-18 19:16:58 -08001081 .of_compatible = "stericsson,ab8500-btemp",
Rickard Andersson6ef94182012-04-17 09:30:57 +02001082 .num_resources = ARRAY_SIZE(ab8500_btemp_resources),
1083 .resources = ab8500_btemp_resources,
Rajanikanth H.Vbd9e8ab2012-11-18 19:16:58 -08001084 .platform_data = &ab8500_bm_data,
1085 .pdata_size = sizeof(ab8500_bm_data),
Rickard Andersson6ef94182012-04-17 09:30:57 +02001086 },
1087 {
1088 .name = "ab8500-fg",
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -08001089 .of_compatible = "stericsson,ab8500-fg",
Rickard Andersson6ef94182012-04-17 09:30:57 +02001090 .num_resources = ARRAY_SIZE(ab8500_fg_resources),
1091 .resources = ab8500_fg_resources,
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -08001092 .platform_data = &ab8500_bm_data,
1093 .pdata_size = sizeof(ab8500_bm_data),
Rickard Andersson6ef94182012-04-17 09:30:57 +02001094 },
1095 {
1096 .name = "ab8500-chargalg",
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00001097 .of_compatible = "stericsson,ab8500-chargalg",
Rickard Andersson6ef94182012-04-17 09:30:57 +02001098 .num_resources = ARRAY_SIZE(ab8500_chargalg_resources),
1099 .resources = ab8500_chargalg_resources,
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00001100 .platform_data = &ab8500_bm_data,
1101 .pdata_size = sizeof(ab8500_bm_data),
Rickard Andersson6ef94182012-04-17 09:30:57 +02001102 },
1103};
1104
Bill Pembertona9e9ce42012-11-19 13:24:21 -05001105static struct mfd_cell ab8500_devs[] = {
Linus Walleijd6255522012-02-20 21:42:24 +01001106 {
Lee Jones7d56a462013-01-16 14:49:45 +00001107 .name = "pinctrl-ab8500",
Lee Jonesbad76992012-07-02 17:10:56 +02001108 .of_compatible = "stericsson,ab8500-gpio",
Linus Walleijd6255522012-02-20 21:42:24 +01001109 },
1110 {
1111 .name = "ab8500-usb",
Lee Jonesbad76992012-07-02 17:10:56 +02001112 .of_compatible = "stericsson,ab8500-usb",
Linus Walleijd6255522012-02-20 21:42:24 +01001113 .num_resources = ARRAY_SIZE(ab8500_usb_resources),
1114 .resources = ab8500_usb_resources,
1115 },
Virupax Sadashivpetimath44f72e52012-04-17 09:30:14 +02001116 {
1117 .name = "ab8500-codec",
Lee Jones81a21cd2012-07-27 13:38:51 +01001118 .of_compatible = "stericsson,ab8500-codec",
Virupax Sadashivpetimath44f72e52012-04-17 09:30:14 +02001119 },
Linus Walleijd6255522012-02-20 21:42:24 +01001120};
1121
Bill Pembertona9e9ce42012-11-19 13:24:21 -05001122static struct mfd_cell ab9540_devs[] = {
Linus Walleijd6255522012-02-20 21:42:24 +01001123 {
Lee Jonese64d9052013-01-16 15:09:36 +00001124 .name = "pinctrl-ab9540",
1125 .of_compatible = "stericsson,ab9540-gpio",
Linus Walleijd6255522012-02-20 21:42:24 +01001126 },
1127 {
1128 .name = "ab9540-usb",
1129 .num_resources = ARRAY_SIZE(ab8500_usb_resources),
1130 .resources = ab8500_usb_resources,
1131 },
Virupax Sadashivpetimath44f72e52012-04-17 09:30:14 +02001132 {
1133 .name = "ab9540-codec",
1134 },
1135};
1136
1137/* Device list common to ab9540 and ab8505 */
Bill Pembertona9e9ce42012-11-19 13:24:21 -05001138static struct mfd_cell ab9540_ab8505_devs[] = {
Virupax Sadashivpetimath44f72e52012-04-17 09:30:14 +02001139 {
1140 .name = "ab-iddet",
1141 .num_resources = ARRAY_SIZE(ab8505_iddet_resources),
1142 .resources = ab8505_iddet_resources,
1143 },
Linus Walleijd6255522012-02-20 21:42:24 +01001144};
1145
Mattias Wallincca69b62010-12-02 15:09:36 +01001146static ssize_t show_chip_id(struct device *dev,
1147 struct device_attribute *attr, char *buf)
1148{
1149 struct ab8500 *ab8500;
1150
1151 ab8500 = dev_get_drvdata(dev);
1152 return sprintf(buf, "%#x\n", ab8500 ? ab8500->chip_id : -EINVAL);
1153}
1154
Mattias Walline5c238c2011-03-02 11:52:36 +01001155/*
1156 * ab8500 has switched off due to (SWITCH_OFF_STATUS):
1157 * 0x01 Swoff bit programming
1158 * 0x02 Thermal protection activation
1159 * 0x04 Vbat lower then BattOk falling threshold
1160 * 0x08 Watchdog expired
1161 * 0x10 Non presence of 32kHz clock
1162 * 0x20 Battery level lower than power on reset threshold
1163 * 0x40 Power on key 1 pressed longer than 10 seconds
1164 * 0x80 DB8500 thermal shutdown
1165 */
1166static ssize_t show_switch_off_status(struct device *dev,
1167 struct device_attribute *attr, char *buf)
1168{
1169 int ret;
1170 u8 value;
1171 struct ab8500 *ab8500;
1172
1173 ab8500 = dev_get_drvdata(dev);
1174 ret = get_register_interruptible(ab8500, AB8500_RTC,
1175 AB8500_SWITCH_OFF_STATUS, &value);
1176 if (ret < 0)
1177 return ret;
1178 return sprintf(buf, "%#x\n", value);
1179}
1180
Rajkumar Kasirajanf04a9d82012-05-30 16:32:37 +05301181/* use mask and set to override the register turn_on_stat value */
1182void ab8500_override_turn_on_stat(u8 mask, u8 set)
1183{
1184 spin_lock(&on_stat_lock);
1185 turn_on_stat_mask = mask;
1186 turn_on_stat_set = set;
1187 spin_unlock(&on_stat_lock);
1188}
1189
Andrew Lynnb4a31032011-10-11 10:49:47 +02001190/*
1191 * ab8500 has turned on due to (TURN_ON_STATUS):
1192 * 0x01 PORnVbat
1193 * 0x02 PonKey1dbF
1194 * 0x04 PonKey2dbF
1195 * 0x08 RTCAlarm
1196 * 0x10 MainChDet
1197 * 0x20 VbusDet
1198 * 0x40 UsbIDDetect
1199 * 0x80 Reserved
1200 */
1201static ssize_t show_turn_on_status(struct device *dev,
1202 struct device_attribute *attr, char *buf)
1203{
1204 int ret;
1205 u8 value;
1206 struct ab8500 *ab8500;
1207
1208 ab8500 = dev_get_drvdata(dev);
1209 ret = get_register_interruptible(ab8500, AB8500_SYS_CTRL1_BLOCK,
1210 AB8500_TURN_ON_STATUS, &value);
1211 if (ret < 0)
1212 return ret;
Rajkumar Kasirajanf04a9d82012-05-30 16:32:37 +05301213
1214 /*
1215 * In L9540, turn_on_status register is not updated correctly if
1216 * the device is rebooted with AC/USB charger connected. Due to
1217 * this, the device boots android instead of entering into charge
1218 * only mode. Read the AC/USB status register to detect the charger
1219 * presence and update the turn on status manually.
1220 */
1221 if (is_ab9540(ab8500)) {
1222 spin_lock(&on_stat_lock);
1223 value = (value & turn_on_stat_mask) | turn_on_stat_set;
1224 spin_unlock(&on_stat_lock);
1225 }
1226
Andrew Lynnb4a31032011-10-11 10:49:47 +02001227 return sprintf(buf, "%#x\n", value);
1228}
1229
Linus Walleijd6255522012-02-20 21:42:24 +01001230static ssize_t show_ab9540_dbbrstn(struct device *dev,
1231 struct device_attribute *attr, char *buf)
1232{
1233 struct ab8500 *ab8500;
1234 int ret;
1235 u8 value;
1236
1237 ab8500 = dev_get_drvdata(dev);
1238
1239 ret = get_register_interruptible(ab8500, AB8500_REGU_CTRL2,
1240 AB9540_MODEM_CTRL2_REG, &value);
1241 if (ret < 0)
1242 return ret;
1243
1244 return sprintf(buf, "%d\n",
1245 (value & AB9540_MODEM_CTRL2_SWDBBRSTN_BIT) ? 1 : 0);
1246}
1247
1248static ssize_t store_ab9540_dbbrstn(struct device *dev,
1249 struct device_attribute *attr, const char *buf, size_t count)
1250{
1251 struct ab8500 *ab8500;
1252 int ret = count;
1253 int err;
1254 u8 bitvalues;
1255
1256 ab8500 = dev_get_drvdata(dev);
1257
1258 if (count > 0) {
1259 switch (buf[0]) {
1260 case '0':
1261 bitvalues = 0;
1262 break;
1263 case '1':
1264 bitvalues = AB9540_MODEM_CTRL2_SWDBBRSTN_BIT;
1265 break;
1266 default:
1267 goto exit;
1268 }
1269
1270 err = mask_and_set_register_interruptible(ab8500,
1271 AB8500_REGU_CTRL2, AB9540_MODEM_CTRL2_REG,
1272 AB9540_MODEM_CTRL2_SWDBBRSTN_BIT, bitvalues);
1273 if (err)
1274 dev_info(ab8500->dev,
1275 "Failed to set DBBRSTN %c, err %#x\n",
1276 buf[0], err);
1277 }
1278
1279exit:
1280 return ret;
1281}
1282
Mattias Wallincca69b62010-12-02 15:09:36 +01001283static DEVICE_ATTR(chip_id, S_IRUGO, show_chip_id, NULL);
Mattias Walline5c238c2011-03-02 11:52:36 +01001284static DEVICE_ATTR(switch_off_status, S_IRUGO, show_switch_off_status, NULL);
Andrew Lynnb4a31032011-10-11 10:49:47 +02001285static DEVICE_ATTR(turn_on_status, S_IRUGO, show_turn_on_status, NULL);
Linus Walleijd6255522012-02-20 21:42:24 +01001286static DEVICE_ATTR(dbbrstn, S_IRUGO | S_IWUSR,
1287 show_ab9540_dbbrstn, store_ab9540_dbbrstn);
Mattias Wallincca69b62010-12-02 15:09:36 +01001288
1289static struct attribute *ab8500_sysfs_entries[] = {
1290 &dev_attr_chip_id.attr,
Mattias Walline5c238c2011-03-02 11:52:36 +01001291 &dev_attr_switch_off_status.attr,
Andrew Lynnb4a31032011-10-11 10:49:47 +02001292 &dev_attr_turn_on_status.attr,
Mattias Wallincca69b62010-12-02 15:09:36 +01001293 NULL,
1294};
1295
Linus Walleijd6255522012-02-20 21:42:24 +01001296static struct attribute *ab9540_sysfs_entries[] = {
1297 &dev_attr_chip_id.attr,
1298 &dev_attr_switch_off_status.attr,
1299 &dev_attr_turn_on_status.attr,
1300 &dev_attr_dbbrstn.attr,
1301 NULL,
1302};
1303
Mattias Wallincca69b62010-12-02 15:09:36 +01001304static struct attribute_group ab8500_attr_group = {
1305 .attrs = ab8500_sysfs_entries,
1306};
1307
Linus Walleijd6255522012-02-20 21:42:24 +01001308static struct attribute_group ab9540_attr_group = {
1309 .attrs = ab9540_sysfs_entries,
1310};
1311
Bill Pembertonf791be42012-11-19 13:23:04 -05001312static int ab8500_probe(struct platform_device *pdev)
Rabin Vincent62579262010-05-19 11:39:02 +02001313{
Jonas Aabergb04c530c2012-06-29 17:46:12 +02001314 static char *switch_off_status[] = {
1315 "Swoff bit programming",
1316 "Thermal protection activation",
1317 "Vbat lower then BattOk falling threshold",
1318 "Watchdog expired",
1319 "Non presence of 32kHz clock",
1320 "Battery level lower than power on reset threshold",
1321 "Power on key 1 pressed longer than 10 seconds",
1322 "DB8500 thermal shutdown"};
Lee Jonesd28f1db2012-05-19 17:21:37 +02001323 struct ab8500_platform_data *plat = dev_get_platdata(&pdev->dev);
1324 const struct platform_device_id *platid = platform_get_device_id(pdev);
Lee Jones6bc4a562012-05-17 14:45:13 +01001325 enum ab8500_version version = AB8500_VERSION_UNDEFINED;
1326 struct device_node *np = pdev->dev.of_node;
Lee Jonesd28f1db2012-05-19 17:21:37 +02001327 struct ab8500 *ab8500;
1328 struct resource *resource;
Rabin Vincent62579262010-05-19 11:39:02 +02001329 int ret;
1330 int i;
Mattias Wallin47c16972010-09-10 17:47:56 +02001331 u8 value;
Rabin Vincent62579262010-05-19 11:39:02 +02001332
Lee Jones8c4203c2012-11-05 16:10:35 +01001333 ab8500 = devm_kzalloc(&pdev->dev, sizeof *ab8500, GFP_KERNEL);
Lee Jonesd28f1db2012-05-19 17:21:37 +02001334 if (!ab8500)
1335 return -ENOMEM;
1336
Rabin Vincent62579262010-05-19 11:39:02 +02001337 if (plat)
1338 ab8500->irq_base = plat->irq_base;
1339
Lee Jonesd28f1db2012-05-19 17:21:37 +02001340 ab8500->dev = &pdev->dev;
1341
1342 resource = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Lee Jones8c4203c2012-11-05 16:10:35 +01001343 if (!resource)
1344 return -ENODEV;
Lee Jonesd28f1db2012-05-19 17:21:37 +02001345
1346 ab8500->irq = resource->start;
1347
Lee Jones822672a2012-06-20 13:56:38 +01001348 ab8500->read = ab8500_prcmu_read;
1349 ab8500->write = ab8500_prcmu_write;
1350 ab8500->write_masked = ab8500_prcmu_write_masked;
Lee Jonesd28f1db2012-05-19 17:21:37 +02001351
Rabin Vincent62579262010-05-19 11:39:02 +02001352 mutex_init(&ab8500->lock);
1353 mutex_init(&ab8500->irq_lock);
Jonas Aaberg112a80d2012-04-17 09:30:33 +02001354 atomic_set(&ab8500->transfer_ongoing, 0);
Rabin Vincent62579262010-05-19 11:39:02 +02001355
Lee Jonesd28f1db2012-05-19 17:21:37 +02001356 platform_set_drvdata(pdev, ab8500);
1357
Lee Jones6bc4a562012-05-17 14:45:13 +01001358 if (platid)
1359 version = platid->driver_data;
Lee Jones6bc4a562012-05-17 14:45:13 +01001360
Linus Walleij0f6208372012-02-20 21:42:10 +01001361 if (version != AB8500_VERSION_UNDEFINED)
1362 ab8500->version = version;
1363 else {
1364 ret = get_register_interruptible(ab8500, AB8500_MISC,
1365 AB8500_IC_NAME_REG, &value);
1366 if (ret < 0)
Lee Jones8c4203c2012-11-05 16:10:35 +01001367 return ret;
Linus Walleij0f6208372012-02-20 21:42:10 +01001368
1369 ab8500->version = value;
1370 }
1371
Mattias Wallin47c16972010-09-10 17:47:56 +02001372 ret = get_register_interruptible(ab8500, AB8500_MISC,
1373 AB8500_REV_REG, &value);
Rabin Vincent62579262010-05-19 11:39:02 +02001374 if (ret < 0)
Lee Jones8c4203c2012-11-05 16:10:35 +01001375 return ret;
Rabin Vincent62579262010-05-19 11:39:02 +02001376
Mattias Wallin47c16972010-09-10 17:47:56 +02001377 ab8500->chip_id = value;
Rabin Vincent62579262010-05-19 11:39:02 +02001378
Linus Walleij0f6208372012-02-20 21:42:10 +01001379 dev_info(ab8500->dev, "detected chip, %s rev. %1x.%1x\n",
1380 ab8500_version_str[ab8500->version],
1381 ab8500->chip_id >> 4,
1382 ab8500->chip_id & 0x0F);
1383
Linus Walleijd6255522012-02-20 21:42:24 +01001384 /* Configure AB8500 or AB9540 IRQ */
Bengt Jonssona9823622012-03-08 14:01:57 +01001385 if (is_ab9540(ab8500) || is_ab8505(ab8500)) {
Linus Walleijd6255522012-02-20 21:42:24 +01001386 ab8500->mask_size = AB9540_NUM_IRQ_REGS;
1387 ab8500->irq_reg_offset = ab9540_irq_regoffset;
1388 } else {
1389 ab8500->mask_size = AB8500_NUM_IRQ_REGS;
1390 ab8500->irq_reg_offset = ab8500_irq_regoffset;
1391 }
Lee Jones8c4203c2012-11-05 16:10:35 +01001392 ab8500->mask = devm_kzalloc(&pdev->dev, ab8500->mask_size, GFP_KERNEL);
Linus Walleij2ced4452012-02-20 21:42:17 +01001393 if (!ab8500->mask)
1394 return -ENOMEM;
Lee Jones8c4203c2012-11-05 16:10:35 +01001395 ab8500->oldmask = devm_kzalloc(&pdev->dev, ab8500->mask_size, GFP_KERNEL);
1396 if (!ab8500->oldmask)
1397 return -ENOMEM;
1398
Mattias Walline5c238c2011-03-02 11:52:36 +01001399 /*
1400 * ab8500 has switched off due to (SWITCH_OFF_STATUS):
1401 * 0x01 Swoff bit programming
1402 * 0x02 Thermal protection activation
1403 * 0x04 Vbat lower then BattOk falling threshold
1404 * 0x08 Watchdog expired
1405 * 0x10 Non presence of 32kHz clock
1406 * 0x20 Battery level lower than power on reset threshold
1407 * 0x40 Power on key 1 pressed longer than 10 seconds
1408 * 0x80 DB8500 thermal shutdown
1409 */
1410
1411 ret = get_register_interruptible(ab8500, AB8500_RTC,
1412 AB8500_SWITCH_OFF_STATUS, &value);
1413 if (ret < 0)
1414 return ret;
Jonas Aabergb04c530c2012-06-29 17:46:12 +02001415 dev_info(ab8500->dev, "switch off cause(s) (%#x): ", value);
1416
1417 if (value) {
1418 for (i = 0; i < ARRAY_SIZE(switch_off_status); i++) {
1419 if (value & 1)
1420 printk(KERN_CONT " \"%s\"",
1421 switch_off_status[i]);
1422 value = value >> 1;
1423
1424 }
1425 printk(KERN_CONT "\n");
1426 } else {
1427 printk(KERN_CONT " None\n");
1428 }
Mattias Walline5c238c2011-03-02 11:52:36 +01001429
Rabin Vincent62579262010-05-19 11:39:02 +02001430 if (plat && plat->init)
1431 plat->init(ab8500);
Rajkumar Kasirajanf04a9d82012-05-30 16:32:37 +05301432 if (is_ab9540(ab8500)) {
1433 ret = get_register_interruptible(ab8500, AB8500_CHARGER,
1434 AB8500_CH_USBCH_STAT1_REG, &value);
1435 if (ret < 0)
1436 return ret;
1437 if ((value & VBUS_DET_DBNC1) && (value & VBUS_DET_DBNC100))
1438 ab8500_override_turn_on_stat(~AB8500_POW_KEY_1_ON,
1439 AB8500_VBUS_DET);
1440 }
Rabin Vincent62579262010-05-19 11:39:02 +02001441
1442 /* Clear and mask all interrupts */
Linus Walleij2ced4452012-02-20 21:42:17 +01001443 for (i = 0; i < ab8500->mask_size; i++) {
Linus Walleij0f6208372012-02-20 21:42:10 +01001444 /*
1445 * Interrupt register 12 doesn't exist prior to AB8500 version
1446 * 2.0
1447 */
1448 if (ab8500->irq_reg_offset[i] == 11 &&
1449 is_ab8500_1p1_or_earlier(ab8500))
Mattias Wallin92d50a42010-12-07 11:20:47 +01001450 continue;
Rabin Vincent62579262010-05-19 11:39:02 +02001451
Mattias Wallin47c16972010-09-10 17:47:56 +02001452 get_register_interruptible(ab8500, AB8500_INTERRUPT,
Linus Walleij2ced4452012-02-20 21:42:17 +01001453 AB8500_IT_LATCH1_REG + ab8500->irq_reg_offset[i],
Mattias Wallin92d50a42010-12-07 11:20:47 +01001454 &value);
Mattias Wallin47c16972010-09-10 17:47:56 +02001455 set_register_interruptible(ab8500, AB8500_INTERRUPT,
Linus Walleij2ced4452012-02-20 21:42:17 +01001456 AB8500_IT_MASK1_REG + ab8500->irq_reg_offset[i], 0xff);
Rabin Vincent62579262010-05-19 11:39:02 +02001457 }
1458
Mattias Wallin47c16972010-09-10 17:47:56 +02001459 ret = abx500_register_ops(ab8500->dev, &ab8500_ops);
1460 if (ret)
Lee Jones8c4203c2012-11-05 16:10:35 +01001461 return ret;
Mattias Wallin47c16972010-09-10 17:47:56 +02001462
Linus Walleij2ced4452012-02-20 21:42:17 +01001463 for (i = 0; i < ab8500->mask_size; i++)
Rabin Vincent62579262010-05-19 11:39:02 +02001464 ab8500->mask[i] = ab8500->oldmask[i] = 0xff;
1465
Lee Jones06e589e2012-06-20 13:56:37 +01001466 ret = ab8500_irq_init(ab8500, np);
1467 if (ret)
Lee Jones8c4203c2012-11-05 16:10:35 +01001468 return ret;
Rabin Vincent62579262010-05-19 11:39:02 +02001469
Lee Jones06e589e2012-06-20 13:56:37 +01001470 /* Activate this feature only in ab9540 */
1471 /* till tests are done on ab8500 1p2 or later*/
1472 if (is_ab9540(ab8500)) {
Lee Jones8c4203c2012-11-05 16:10:35 +01001473 ret = devm_request_threaded_irq(&pdev->dev, ab8500->irq, NULL,
1474 ab8500_hierarchical_irq,
1475 IRQF_ONESHOT | IRQF_NO_SUSPEND,
1476 "ab8500", ab8500);
Lee Jones06e589e2012-06-20 13:56:37 +01001477 }
1478 else {
Lee Jones8c4203c2012-11-05 16:10:35 +01001479 ret = devm_request_threaded_irq(&pdev->dev, ab8500->irq, NULL,
1480 ab8500_irq,
1481 IRQF_ONESHOT | IRQF_NO_SUSPEND,
1482 "ab8500", ab8500);
Rabin Vincent62579262010-05-19 11:39:02 +02001483 if (ret)
Lee Jones8c4203c2012-11-05 16:10:35 +01001484 return ret;
Rabin Vincent62579262010-05-19 11:39:02 +02001485 }
1486
Lee Jonesbad76992012-07-02 17:10:56 +02001487 ret = mfd_add_devices(ab8500->dev, 0, abx500_common_devs,
1488 ARRAY_SIZE(abx500_common_devs), NULL,
Mark Brown55692af2012-09-11 15:16:36 +08001489 ab8500->irq_base, ab8500->domain);
Lee Jonesbad76992012-07-02 17:10:56 +02001490 if (ret)
Lee Jones8c4203c2012-11-05 16:10:35 +01001491 return ret;
Lee Jonesbad76992012-07-02 17:10:56 +02001492
1493 if (is_ab9540(ab8500))
1494 ret = mfd_add_devices(ab8500->dev, 0, ab9540_devs,
1495 ARRAY_SIZE(ab9540_devs), NULL,
Mark Brown55692af2012-09-11 15:16:36 +08001496 ab8500->irq_base, ab8500->domain);
Lee Jonesbad76992012-07-02 17:10:56 +02001497 else
1498 ret = mfd_add_devices(ab8500->dev, 0, ab8500_devs,
1499 ARRAY_SIZE(ab8500_devs), NULL,
Mark Brown55692af2012-09-11 15:16:36 +08001500 ab8500->irq_base, ab8500->domain);
Lee Jonesbad76992012-07-02 17:10:56 +02001501 if (ret)
Lee Jones8c4203c2012-11-05 16:10:35 +01001502 return ret;
Linus Walleijd6255522012-02-20 21:42:24 +01001503
Lee Jonesbad76992012-07-02 17:10:56 +02001504 if (is_ab9540(ab8500) || is_ab8505(ab8500))
1505 ret = mfd_add_devices(ab8500->dev, 0, ab9540_ab8505_devs,
1506 ARRAY_SIZE(ab9540_ab8505_devs), NULL,
Mark Brown55692af2012-09-11 15:16:36 +08001507 ab8500->irq_base, ab8500->domain);
Lee Jonesbad76992012-07-02 17:10:56 +02001508 if (ret)
Lee Jones8c4203c2012-11-05 16:10:35 +01001509 return ret;
Rabin Vincent62579262010-05-19 11:39:02 +02001510
Rickard Andersson6ef94182012-04-17 09:30:57 +02001511 if (!no_bm) {
1512 /* Add battery management devices */
1513 ret = mfd_add_devices(ab8500->dev, 0, ab8500_bm_devs,
1514 ARRAY_SIZE(ab8500_bm_devs), NULL,
Mark Brown55692af2012-09-11 15:16:36 +08001515 ab8500->irq_base, ab8500->domain);
Rickard Andersson6ef94182012-04-17 09:30:57 +02001516 if (ret)
1517 dev_err(ab8500->dev, "error adding bm devices\n");
1518 }
1519
Linus Walleijd6255522012-02-20 21:42:24 +01001520 if (is_ab9540(ab8500))
1521 ret = sysfs_create_group(&ab8500->dev->kobj,
1522 &ab9540_attr_group);
1523 else
1524 ret = sysfs_create_group(&ab8500->dev->kobj,
1525 &ab8500_attr_group);
Mattias Wallincca69b62010-12-02 15:09:36 +01001526 if (ret)
1527 dev_err(ab8500->dev, "error creating sysfs entries\n");
Lee Jones06e589e2012-06-20 13:56:37 +01001528
1529 return ret;
Rabin Vincent62579262010-05-19 11:39:02 +02001530}
1531
Bill Pemberton4740f732012-11-19 13:26:01 -05001532static int ab8500_remove(struct platform_device *pdev)
Rabin Vincent62579262010-05-19 11:39:02 +02001533{
Lee Jonesd28f1db2012-05-19 17:21:37 +02001534 struct ab8500 *ab8500 = platform_get_drvdata(pdev);
1535
Linus Walleijd6255522012-02-20 21:42:24 +01001536 if (is_ab9540(ab8500))
1537 sysfs_remove_group(&ab8500->dev->kobj, &ab9540_attr_group);
1538 else
1539 sysfs_remove_group(&ab8500->dev->kobj, &ab8500_attr_group);
Lee Jones06e589e2012-06-20 13:56:37 +01001540
Rabin Vincent62579262010-05-19 11:39:02 +02001541 mfd_remove_devices(ab8500->dev);
Rabin Vincent62579262010-05-19 11:39:02 +02001542
1543 return 0;
1544}
1545
Lee Jonesd28f1db2012-05-19 17:21:37 +02001546static const struct platform_device_id ab8500_id[] = {
1547 { "ab8500-core", AB8500_VERSION_AB8500 },
1548 { "ab8505-i2c", AB8500_VERSION_AB8505 },
1549 { "ab9540-i2c", AB8500_VERSION_AB9540 },
1550 { "ab8540-i2c", AB8500_VERSION_AB8540 },
1551 { }
1552};
1553
1554static struct platform_driver ab8500_core_driver = {
1555 .driver = {
1556 .name = "ab8500-core",
1557 .owner = THIS_MODULE,
1558 },
1559 .probe = ab8500_probe,
Bill Pemberton84449212012-11-19 13:20:24 -05001560 .remove = ab8500_remove,
Lee Jonesd28f1db2012-05-19 17:21:37 +02001561 .id_table = ab8500_id,
1562};
1563
1564static int __init ab8500_core_init(void)
1565{
1566 return platform_driver_register(&ab8500_core_driver);
1567}
1568
1569static void __exit ab8500_core_exit(void)
1570{
1571 platform_driver_unregister(&ab8500_core_driver);
1572}
Lee Jonesba7cbc32012-06-11 16:25:00 +01001573core_initcall(ab8500_core_init);
Lee Jonesd28f1db2012-05-19 17:21:37 +02001574module_exit(ab8500_core_exit);
1575
Mattias Wallinadceed62011-03-02 11:51:11 +01001576MODULE_AUTHOR("Mattias Wallin, Srinidhi Kasagar, Rabin Vincent");
Rabin Vincent62579262010-05-19 11:39:02 +02001577MODULE_DESCRIPTION("AB8500 MFD core");
1578MODULE_LICENSE("GPL v2");