blob: a4120edbccf293319c9d095adabf15430f6a43e2 [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
Lee Jonesa29264b2013-02-25 14:34:26 +000098#define AB8500_IT_MASK25_REG 0x58
Rabin Vincent62579262010-05-19 11:39:02 +020099
Michel JAOUEN7ccfe9b2012-05-07 15:02:03 +0200100/*
101 * latch hierarchy registers
102 */
103#define AB8500_IT_LATCHHIER1_REG 0x60
104#define AB8500_IT_LATCHHIER2_REG 0x61
105#define AB8500_IT_LATCHHIER3_REG 0x62
Lee Jones3e1a4982013-02-25 14:57:35 +0000106#define AB8540_IT_LATCHHIER4_REG 0x63
Michel JAOUEN7ccfe9b2012-05-07 15:02:03 +0200107
108#define AB8500_IT_LATCHHIER_NUM 3
Lee Jones3e1a4982013-02-25 14:57:35 +0000109#define AB8540_IT_LATCHHIER_NUM 4
Michel JAOUEN7ccfe9b2012-05-07 15:02:03 +0200110
Mattias Wallin47c16972010-09-10 17:47:56 +0200111#define AB8500_REV_REG 0x80
Linus Walleij0f6208372012-02-20 21:42:10 +0100112#define AB8500_IC_NAME_REG 0x82
Mattias Walline5c238c2011-03-02 11:52:36 +0100113#define AB8500_SWITCH_OFF_STATUS 0x00
Rabin Vincent62579262010-05-19 11:39:02 +0200114
Andrew Lynnb4a31032011-10-11 10:49:47 +0200115#define AB8500_TURN_ON_STATUS 0x00
116
Rajkumar Kasirajanf04a9d82012-05-30 16:32:37 +0530117#define AB8500_CH_USBCH_STAT1_REG 0x02
118#define VBUS_DET_DBNC100 0x02
119#define VBUS_DET_DBNC1 0x01
120
121static DEFINE_SPINLOCK(on_stat_lock);
122static u8 turn_on_stat_mask = 0xFF;
123static u8 turn_on_stat_set;
Rickard Andersson6ef94182012-04-17 09:30:57 +0200124static bool no_bm; /* No battery management */
125module_param(no_bm, bool, S_IRUGO);
126
Linus Walleijd6255522012-02-20 21:42:24 +0100127#define AB9540_MODEM_CTRL2_REG 0x23
128#define AB9540_MODEM_CTRL2_SWDBBRSTN_BIT BIT(2)
129
Rabin Vincent62579262010-05-19 11:39:02 +0200130/*
131 * Map interrupt numbers to the LATCH and MASK register offsets, Interrupt
Linus Walleij2ced4452012-02-20 21:42:17 +0100132 * numbers are indexed into this array with (num / 8). The interupts are
133 * defined in linux/mfd/ab8500.h
Rabin Vincent62579262010-05-19 11:39:02 +0200134 *
135 * This is one off from the register names, i.e. AB8500_IT_MASK1_REG is at
136 * offset 0.
137 */
Linus Walleij2ced4452012-02-20 21:42:17 +0100138/* AB8500 support */
Rabin Vincent62579262010-05-19 11:39:02 +0200139static const int ab8500_irq_regoffset[AB8500_NUM_IRQ_REGS] = {
Mattias Wallin92d50a42010-12-07 11:20:47 +0100140 0, 1, 2, 3, 4, 6, 7, 8, 9, 11, 18, 19, 20, 21,
Rabin Vincent62579262010-05-19 11:39:02 +0200141};
142
Lee Jonesa29264b2013-02-25 14:34:26 +0000143/* AB9540 / AB8505 support */
Linus Walleijd6255522012-02-20 21:42:24 +0100144static const int ab9540_irq_regoffset[AB9540_NUM_IRQ_REGS] = {
Lee Jonesa29264b2013-02-25 14:34:26 +0000145 0, 1, 2, 3, 4, 6, 7, 8, 9, 11, 18, 19, 20, 21, 12, 13, 24, 5, 22, 23
Linus Walleijd6255522012-02-20 21:42:24 +0100146};
147
Lee Jones3e1a4982013-02-25 14:57:35 +0000148/* AB8540 support */
149static const int ab8540_irq_regoffset[AB8540_NUM_IRQ_REGS] = {
150 0, 1, 2, 3, 4, -1, -1, -1, -1, 11, 18, 19, 20, 21, 12, 13, 24, 5, 22, 23,
151 25, 26, 27, 28, 29, 30, 31,
152};
153
Linus Walleij0f6208372012-02-20 21:42:10 +0100154static const char ab8500_version_str[][7] = {
155 [AB8500_VERSION_AB8500] = "AB8500",
156 [AB8500_VERSION_AB8505] = "AB8505",
157 [AB8500_VERSION_AB9540] = "AB9540",
158 [AB8500_VERSION_AB8540] = "AB8540",
159};
160
Lee Jones822672a2012-06-20 13:56:38 +0100161static int ab8500_prcmu_write(struct ab8500 *ab8500, u16 addr, u8 data)
Lee Jonesd28f1db2012-05-19 17:21:37 +0200162{
163 int ret;
164
165 ret = prcmu_abb_write((u8)(addr >> 8), (u8)(addr & 0xFF), &data, 1);
166 if (ret < 0)
167 dev_err(ab8500->dev, "prcmu i2c error %d\n", ret);
168 return ret;
169}
170
Lee Jones822672a2012-06-20 13:56:38 +0100171static int ab8500_prcmu_write_masked(struct ab8500 *ab8500, u16 addr, u8 mask,
Lee Jonesd28f1db2012-05-19 17:21:37 +0200172 u8 data)
173{
174 int ret;
175
176 ret = prcmu_abb_write_masked((u8)(addr >> 8), (u8)(addr & 0xFF), &data,
177 &mask, 1);
178 if (ret < 0)
179 dev_err(ab8500->dev, "prcmu i2c error %d\n", ret);
180 return ret;
181}
182
Lee Jones822672a2012-06-20 13:56:38 +0100183static int ab8500_prcmu_read(struct ab8500 *ab8500, u16 addr)
Lee Jonesd28f1db2012-05-19 17:21:37 +0200184{
185 int ret;
186 u8 data;
187
188 ret = prcmu_abb_read((u8)(addr >> 8), (u8)(addr & 0xFF), &data, 1);
189 if (ret < 0) {
190 dev_err(ab8500->dev, "prcmu i2c error %d\n", ret);
191 return ret;
192 }
193 return (int)data;
194}
195
Mattias Wallin47c16972010-09-10 17:47:56 +0200196static int ab8500_get_chip_id(struct device *dev)
197{
Mattias Wallin6bce7bf2010-12-02 15:08:32 +0100198 struct ab8500 *ab8500;
199
200 if (!dev)
201 return -EINVAL;
202 ab8500 = dev_get_drvdata(dev->parent);
203 return ab8500 ? (int)ab8500->chip_id : -EINVAL;
Mattias Wallin47c16972010-09-10 17:47:56 +0200204}
205
206static int set_register_interruptible(struct ab8500 *ab8500, u8 bank,
207 u8 reg, u8 data)
Rabin Vincent62579262010-05-19 11:39:02 +0200208{
209 int ret;
Mattias Wallin47c16972010-09-10 17:47:56 +0200210 /*
211 * Put the u8 bank and u8 register together into a an u16.
212 * The bank on higher 8 bits and register in lower 8 bits.
213 * */
214 u16 addr = ((u16)bank) << 8 | reg;
Rabin Vincent62579262010-05-19 11:39:02 +0200215
216 dev_vdbg(ab8500->dev, "wr: addr %#x <= %#x\n", addr, data);
217
Rabin Vincent392cbd12012-03-08 14:01:46 +0100218 mutex_lock(&ab8500->lock);
Mattias Wallin47c16972010-09-10 17:47:56 +0200219
220 ret = ab8500->write(ab8500, addr, data);
221 if (ret < 0)
222 dev_err(ab8500->dev, "failed to write reg %#x: %d\n",
223 addr, ret);
224 mutex_unlock(&ab8500->lock);
225
226 return ret;
227}
228
229static int ab8500_set_register(struct device *dev, u8 bank,
230 u8 reg, u8 value)
231{
Jonas Aaberg112a80d2012-04-17 09:30:33 +0200232 int ret;
Mattias Wallin47c16972010-09-10 17:47:56 +0200233 struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
234
Jonas Aaberg112a80d2012-04-17 09:30:33 +0200235 atomic_inc(&ab8500->transfer_ongoing);
236 ret = set_register_interruptible(ab8500, bank, reg, value);
237 atomic_dec(&ab8500->transfer_ongoing);
238 return ret;
Mattias Wallin47c16972010-09-10 17:47:56 +0200239}
240
241static int get_register_interruptible(struct ab8500 *ab8500, u8 bank,
242 u8 reg, u8 *value)
243{
244 int ret;
245 /* put the u8 bank and u8 reg together into a an u16.
246 * bank on higher 8 bits and reg in lower */
247 u16 addr = ((u16)bank) << 8 | reg;
248
Rabin Vincent392cbd12012-03-08 14:01:46 +0100249 mutex_lock(&ab8500->lock);
Mattias Wallin47c16972010-09-10 17:47:56 +0200250
251 ret = ab8500->read(ab8500, addr);
252 if (ret < 0)
253 dev_err(ab8500->dev, "failed to read reg %#x: %d\n",
254 addr, ret);
255 else
256 *value = ret;
257
258 mutex_unlock(&ab8500->lock);
259 dev_vdbg(ab8500->dev, "rd: addr %#x => data %#x\n", addr, ret);
260
261 return ret;
262}
263
264static int ab8500_get_register(struct device *dev, u8 bank,
265 u8 reg, u8 *value)
266{
Jonas Aaberg112a80d2012-04-17 09:30:33 +0200267 int ret;
Mattias Wallin47c16972010-09-10 17:47:56 +0200268 struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
269
Jonas Aaberg112a80d2012-04-17 09:30:33 +0200270 atomic_inc(&ab8500->transfer_ongoing);
271 ret = get_register_interruptible(ab8500, bank, reg, value);
272 atomic_dec(&ab8500->transfer_ongoing);
273 return ret;
Mattias Wallin47c16972010-09-10 17:47:56 +0200274}
275
276static int mask_and_set_register_interruptible(struct ab8500 *ab8500, u8 bank,
277 u8 reg, u8 bitmask, u8 bitvalues)
278{
279 int ret;
Mattias Wallin47c16972010-09-10 17:47:56 +0200280 /* put the u8 bank and u8 reg together into a an u16.
281 * bank on higher 8 bits and reg in lower */
282 u16 addr = ((u16)bank) << 8 | reg;
283
Rabin Vincent392cbd12012-03-08 14:01:46 +0100284 mutex_lock(&ab8500->lock);
Mattias Wallin47c16972010-09-10 17:47:56 +0200285
Mattias Nilssonbc628fd2012-03-08 14:02:20 +0100286 if (ab8500->write_masked == NULL) {
287 u8 data;
288
289 ret = ab8500->read(ab8500, addr);
290 if (ret < 0) {
291 dev_err(ab8500->dev, "failed to read reg %#x: %d\n",
292 addr, ret);
293 goto out;
294 }
295
296 data = (u8)ret;
297 data = (~bitmask & data) | (bitmask & bitvalues);
298
299 ret = ab8500->write(ab8500, addr, data);
300 if (ret < 0)
301 dev_err(ab8500->dev, "failed to write reg %#x: %d\n",
302 addr, ret);
303
304 dev_vdbg(ab8500->dev, "mask: addr %#x => data %#x\n", addr,
305 data);
Mattias Wallin47c16972010-09-10 17:47:56 +0200306 goto out;
307 }
Mattias Nilssonbc628fd2012-03-08 14:02:20 +0100308 ret = ab8500->write_masked(ab8500, addr, bitmask, bitvalues);
Rabin Vincent62579262010-05-19 11:39:02 +0200309 if (ret < 0)
Mattias Nilssonbc628fd2012-03-08 14:02:20 +0100310 dev_err(ab8500->dev, "failed to modify reg %#x: %d\n", addr,
311 ret);
Rabin Vincent62579262010-05-19 11:39:02 +0200312out:
313 mutex_unlock(&ab8500->lock);
314 return ret;
315}
Mattias Wallin47c16972010-09-10 17:47:56 +0200316
317static int ab8500_mask_and_set_register(struct device *dev,
318 u8 bank, u8 reg, u8 bitmask, u8 bitvalues)
319{
Jonas Aaberg112a80d2012-04-17 09:30:33 +0200320 int ret;
Mattias Wallin47c16972010-09-10 17:47:56 +0200321 struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
322
Jonas Aaberg112a80d2012-04-17 09:30:33 +0200323 atomic_inc(&ab8500->transfer_ongoing);
324 ret= mask_and_set_register_interruptible(ab8500, bank, reg,
325 bitmask, bitvalues);
326 atomic_dec(&ab8500->transfer_ongoing);
327 return ret;
Mattias Wallin47c16972010-09-10 17:47:56 +0200328}
329
330static struct abx500_ops ab8500_ops = {
331 .get_chip_id = ab8500_get_chip_id,
332 .get_register = ab8500_get_register,
333 .set_register = ab8500_set_register,
334 .get_register_page = NULL,
335 .set_register_page = NULL,
336 .mask_and_set_register = ab8500_mask_and_set_register,
337 .event_registers_startup_state_get = NULL,
338 .startup_irq_enabled = NULL,
Mian Yousaf Kaukab1d843a62012-01-27 11:35:41 +0100339 .dump_all_banks = ab8500_dump_all_banks,
Mattias Wallin47c16972010-09-10 17:47:56 +0200340};
Rabin Vincent62579262010-05-19 11:39:02 +0200341
Mark Brown9505a0a2010-12-11 13:16:08 +0000342static void ab8500_irq_lock(struct irq_data *data)
Rabin Vincent62579262010-05-19 11:39:02 +0200343{
Mark Brown9505a0a2010-12-11 13:16:08 +0000344 struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
Rabin Vincent62579262010-05-19 11:39:02 +0200345
346 mutex_lock(&ab8500->irq_lock);
Jonas Aaberg112a80d2012-04-17 09:30:33 +0200347 atomic_inc(&ab8500->transfer_ongoing);
Rabin Vincent62579262010-05-19 11:39:02 +0200348}
349
Mark Brown9505a0a2010-12-11 13:16:08 +0000350static void ab8500_irq_sync_unlock(struct irq_data *data)
Rabin Vincent62579262010-05-19 11:39:02 +0200351{
Mark Brown9505a0a2010-12-11 13:16:08 +0000352 struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
Rabin Vincent62579262010-05-19 11:39:02 +0200353 int i;
354
Linus Walleij2ced4452012-02-20 21:42:17 +0100355 for (i = 0; i < ab8500->mask_size; i++) {
Rabin Vincent62579262010-05-19 11:39:02 +0200356 u8 old = ab8500->oldmask[i];
357 u8 new = ab8500->mask[i];
358 int reg;
359
360 if (new == old)
361 continue;
362
Linus Walleij0f6208372012-02-20 21:42:10 +0100363 /*
364 * Interrupt register 12 doesn't exist prior to AB8500 version
365 * 2.0
366 */
367 if (ab8500->irq_reg_offset[i] == 11 &&
368 is_ab8500_1p1_or_earlier(ab8500))
Mattias Wallin92d50a42010-12-07 11:20:47 +0100369 continue;
370
Lee Jones3e1a4982013-02-25 14:57:35 +0000371 if (ab8500->irq_reg_offset[i] < 0)
372 continue;
373
Rabin Vincent62579262010-05-19 11:39:02 +0200374 ab8500->oldmask[i] = new;
375
Linus Walleij2ced4452012-02-20 21:42:17 +0100376 reg = AB8500_IT_MASK1_REG + ab8500->irq_reg_offset[i];
Mattias Wallin47c16972010-09-10 17:47:56 +0200377 set_register_interruptible(ab8500, AB8500_INTERRUPT, reg, new);
Rabin Vincent62579262010-05-19 11:39:02 +0200378 }
Jonas Aaberg112a80d2012-04-17 09:30:33 +0200379 atomic_dec(&ab8500->transfer_ongoing);
Rabin Vincent62579262010-05-19 11:39:02 +0200380 mutex_unlock(&ab8500->irq_lock);
381}
382
Mark Brown9505a0a2010-12-11 13:16:08 +0000383static void ab8500_irq_mask(struct irq_data *data)
Rabin Vincent62579262010-05-19 11:39:02 +0200384{
Mark Brown9505a0a2010-12-11 13:16:08 +0000385 struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
Lee Jones06e589e2012-06-20 13:56:37 +0100386 int offset = data->hwirq;
Rabin Vincent62579262010-05-19 11:39:02 +0200387 int index = offset / 8;
388 int mask = 1 << (offset % 8);
389
390 ab8500->mask[index] |= mask;
Lee Jones9c677b92012-12-20 11:23:42 +0000391
392 /* The AB8500 GPIOs have two interrupts each (rising & falling). */
393 if (offset >= AB8500_INT_GPIO6R && offset <= AB8500_INT_GPIO41R)
394 ab8500->mask[index + 2] |= mask;
395 if (offset >= AB9540_INT_GPIO50R && offset <= AB9540_INT_GPIO54R)
396 ab8500->mask[index + 1] |= mask;
397 if (offset == AB8540_INT_GPIO43R || offset == AB8540_INT_GPIO44R)
Linus Walleije2ddf462013-02-06 21:54:34 +0100398 /* Here the falling IRQ is one bit lower */
399 ab8500->mask[index] |= (mask << 1);
Rabin Vincent62579262010-05-19 11:39:02 +0200400}
401
Mark Brown9505a0a2010-12-11 13:16:08 +0000402static void ab8500_irq_unmask(struct irq_data *data)
Rabin Vincent62579262010-05-19 11:39:02 +0200403{
Mark Brown9505a0a2010-12-11 13:16:08 +0000404 struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
Lee Jones9c677b92012-12-20 11:23:42 +0000405 unsigned int type = irqd_get_trigger_type(data);
Lee Jones06e589e2012-06-20 13:56:37 +0100406 int offset = data->hwirq;
Rabin Vincent62579262010-05-19 11:39:02 +0200407 int index = offset / 8;
408 int mask = 1 << (offset % 8);
409
Lee Jones9c677b92012-12-20 11:23:42 +0000410 if (type & IRQ_TYPE_EDGE_RISING)
411 ab8500->mask[index] &= ~mask;
412
413 /* The AB8500 GPIOs have two interrupts each (rising & falling). */
414 if (type & IRQ_TYPE_EDGE_FALLING) {
415 if (offset >= AB8500_INT_GPIO6R && offset <= AB8500_INT_GPIO41R)
416 ab8500->mask[index + 2] &= ~mask;
417 else if (offset >= AB9540_INT_GPIO50R && offset <= AB9540_INT_GPIO54R)
418 ab8500->mask[index + 1] &= ~mask;
419 else if (offset == AB8540_INT_GPIO43R || offset == AB8540_INT_GPIO44R)
Linus Walleije2ddf462013-02-06 21:54:34 +0100420 /* Here the falling IRQ is one bit lower */
421 ab8500->mask[index] &= ~(mask << 1);
Lee Jones9c677b92012-12-20 11:23:42 +0000422 else
423 ab8500->mask[index] &= ~mask;
Linus Walleije2ddf462013-02-06 21:54:34 +0100424 } else {
Lee Jones9c677b92012-12-20 11:23:42 +0000425 /* Satisfies the case where type is not set. */
426 ab8500->mask[index] &= ~mask;
Linus Walleije2ddf462013-02-06 21:54:34 +0100427 }
Rabin Vincent62579262010-05-19 11:39:02 +0200428}
429
Lee Jones40f6e5a2013-01-07 12:23:48 +0000430static int ab8500_irq_set_type(struct irq_data *data, unsigned int type)
431{
432 return 0;
Rabin Vincent62579262010-05-19 11:39:02 +0200433}
434
435static struct irq_chip ab8500_irq_chip = {
436 .name = "ab8500",
Mark Brown9505a0a2010-12-11 13:16:08 +0000437 .irq_bus_lock = ab8500_irq_lock,
438 .irq_bus_sync_unlock = ab8500_irq_sync_unlock,
439 .irq_mask = ab8500_irq_mask,
Virupax Sadashivpetimathe6f93062011-10-11 10:49:17 +0200440 .irq_disable = ab8500_irq_mask,
Mark Brown9505a0a2010-12-11 13:16:08 +0000441 .irq_unmask = ab8500_irq_unmask,
Lee Jones40f6e5a2013-01-07 12:23:48 +0000442 .irq_set_type = ab8500_irq_set_type,
Rabin Vincent62579262010-05-19 11:39:02 +0200443};
444
Lee Jones3e1a4982013-02-25 14:57:35 +0000445static void update_latch_offset(u8 *offset, int i)
446{
447 /* Fix inconsistent ITFromLatch25 bit mapping... */
448 if (unlikely(*offset == 17))
449 *offset = 24;
450 /* Fix inconsistent ab8540 bit mapping... */
451 if (unlikely(*offset == 16))
452 *offset = 25;
453 if ((i==3) && (*offset >= 24))
454 *offset += 2;
455}
456
Michel JAOUEN7ccfe9b2012-05-07 15:02:03 +0200457static int ab8500_handle_hierarchical_line(struct ab8500 *ab8500,
458 int latch_offset, u8 latch_val)
459{
460 int int_bit = __ffs(latch_val);
461 int line, i;
462
463 do {
464 int_bit = __ffs(latch_val);
465
466 for (i = 0; i < ab8500->mask_size; i++)
467 if (ab8500->irq_reg_offset[i] == latch_offset)
468 break;
469
470 if (i >= ab8500->mask_size) {
471 dev_err(ab8500->dev, "Register offset 0x%2x not declared\n",
472 latch_offset);
473 return -ENXIO;
474 }
475
476 line = (i << 3) + int_bit;
477 latch_val &= ~(1 << int_bit);
478
Linus Walleije2ddf462013-02-06 21:54:34 +0100479 /*
480 * This handles the falling edge hwirqs from the GPIO
481 * lines. Route them back to the line registered for the
482 * rising IRQ, as this is merely a flag for the same IRQ
483 * in linux terms.
484 */
485 if (line >= AB8500_INT_GPIO6F && line <= AB8500_INT_GPIO41F)
486 line -= 16;
487 if (line >= AB9540_INT_GPIO50F && line <= AB9540_INT_GPIO54F)
488 line -= 8;
489 if (line == AB8540_INT_GPIO43F || line == AB8540_INT_GPIO44F)
490 line += 1;
491
Michel JAOUEN7ccfe9b2012-05-07 15:02:03 +0200492 handle_nested_irq(ab8500->irq_base + line);
493 } while (latch_val);
494
495 return 0;
496}
497
498static int ab8500_handle_hierarchical_latch(struct ab8500 *ab8500,
499 int hier_offset, u8 hier_val)
500{
501 int latch_bit, status;
502 u8 latch_offset, latch_val;
503
504 do {
505 latch_bit = __ffs(hier_val);
506 latch_offset = (hier_offset << 3) + latch_bit;
507
Lee Jones3e1a4982013-02-25 14:57:35 +0000508 update_latch_offset(&latch_offset, hier_offset);
Michel JAOUEN7ccfe9b2012-05-07 15:02:03 +0200509
510 status = get_register_interruptible(ab8500,
511 AB8500_INTERRUPT,
512 AB8500_IT_LATCH1_REG + latch_offset,
513 &latch_val);
514 if (status < 0 || latch_val == 0)
515 goto discard;
516
517 status = ab8500_handle_hierarchical_line(ab8500,
518 latch_offset, latch_val);
519 if (status < 0)
520 return status;
521discard:
522 hier_val &= ~(1 << latch_bit);
523 } while (hier_val);
524
525 return 0;
526}
527
528static irqreturn_t ab8500_hierarchical_irq(int irq, void *dev)
529{
530 struct ab8500 *ab8500 = dev;
531 u8 i;
532
533 dev_vdbg(ab8500->dev, "interrupt\n");
534
535 /* Hierarchical interrupt version */
Lee Jones3e1a4982013-02-25 14:57:35 +0000536 for (i = 0; i < (ab8500->it_latchhier_num); i++) {
Michel JAOUEN7ccfe9b2012-05-07 15:02:03 +0200537 int status;
538 u8 hier_val;
539
540 status = get_register_interruptible(ab8500, AB8500_INTERRUPT,
541 AB8500_IT_LATCHHIER1_REG + i, &hier_val);
542 if (status < 0 || hier_val == 0)
543 continue;
544
545 status = ab8500_handle_hierarchical_latch(ab8500, i, hier_val);
546 if (status < 0)
547 break;
548 }
549 return IRQ_HANDLED;
550}
551
Lee Jones80633f02012-08-20 11:53:36 +0100552/**
553 * ab8500_irq_get_virq(): Map an interrupt on a chip to a virtual IRQ
554 *
555 * @ab8500: ab8500_irq controller to operate on.
556 * @irq: index of the interrupt requested in the chip IRQs
557 *
558 * Useful for drivers to request their own IRQs.
559 */
560static int ab8500_irq_get_virq(struct ab8500 *ab8500, int irq)
561{
562 if (!ab8500)
563 return -EINVAL;
564
565 return irq_create_mapping(ab8500->domain, irq);
566}
567
Rabin Vincent62579262010-05-19 11:39:02 +0200568static irqreturn_t ab8500_irq(int irq, void *dev)
569{
570 struct ab8500 *ab8500 = dev;
571 int i;
572
573 dev_vdbg(ab8500->dev, "interrupt\n");
574
Jonas Aaberg112a80d2012-04-17 09:30:33 +0200575 atomic_inc(&ab8500->transfer_ongoing);
576
Linus Walleij2ced4452012-02-20 21:42:17 +0100577 for (i = 0; i < ab8500->mask_size; i++) {
578 int regoffset = ab8500->irq_reg_offset[i];
Rabin Vincent62579262010-05-19 11:39:02 +0200579 int status;
Mattias Wallin47c16972010-09-10 17:47:56 +0200580 u8 value;
Rabin Vincent62579262010-05-19 11:39:02 +0200581
Linus Walleij0f6208372012-02-20 21:42:10 +0100582 /*
583 * Interrupt register 12 doesn't exist prior to AB8500 version
584 * 2.0
585 */
586 if (regoffset == 11 && is_ab8500_1p1_or_earlier(ab8500))
Mattias Wallin92d50a42010-12-07 11:20:47 +0100587 continue;
588
Lee Jones3e1a4982013-02-25 14:57:35 +0000589 if (regoffset < 0)
590 continue;
591
Mattias Wallin47c16972010-09-10 17:47:56 +0200592 status = get_register_interruptible(ab8500, AB8500_INTERRUPT,
593 AB8500_IT_LATCH1_REG + regoffset, &value);
594 if (status < 0 || value == 0)
Rabin Vincent62579262010-05-19 11:39:02 +0200595 continue;
596
597 do {
Mattias Wallin88aec4f2010-12-02 15:06:49 +0100598 int bit = __ffs(value);
Rabin Vincent62579262010-05-19 11:39:02 +0200599 int line = i * 8 + bit;
Lee Jones0a37fc52012-08-09 16:53:54 +0100600 int virq = ab8500_irq_get_virq(ab8500, line);
Rabin Vincent62579262010-05-19 11:39:02 +0200601
Lee Jones0a37fc52012-08-09 16:53:54 +0100602 handle_nested_irq(virq);
Bengt Jonsson8f0eb432012-02-14 13:01:00 +0100603 ab8500_debug_register_interrupt(line);
Mattias Wallin47c16972010-09-10 17:47:56 +0200604 value &= ~(1 << bit);
Jonas Aaberg112a80d2012-04-17 09:30:33 +0200605
Mattias Wallin47c16972010-09-10 17:47:56 +0200606 } while (value);
Rabin Vincent62579262010-05-19 11:39:02 +0200607 }
Jonas Aaberg112a80d2012-04-17 09:30:33 +0200608 atomic_dec(&ab8500->transfer_ongoing);
Rabin Vincent62579262010-05-19 11:39:02 +0200609 return IRQ_HANDLED;
610}
611
Lee Jones06e589e2012-06-20 13:56:37 +0100612static int ab8500_irq_map(struct irq_domain *d, unsigned int virq,
613 irq_hw_number_t hwirq)
614{
615 struct ab8500 *ab8500 = d->host_data;
616
617 if (!ab8500)
618 return -EINVAL;
619
620 irq_set_chip_data(virq, ab8500);
621 irq_set_chip_and_handler(virq, &ab8500_irq_chip,
622 handle_simple_irq);
623 irq_set_nested_thread(virq, 1);
Rabin Vincent62579262010-05-19 11:39:02 +0200624#ifdef CONFIG_ARM
Lee Jones06e589e2012-06-20 13:56:37 +0100625 set_irq_flags(virq, IRQF_VALID);
Rabin Vincent62579262010-05-19 11:39:02 +0200626#else
Lee Jones06e589e2012-06-20 13:56:37 +0100627 irq_set_noprobe(virq);
Rabin Vincent62579262010-05-19 11:39:02 +0200628#endif
Rabin Vincent62579262010-05-19 11:39:02 +0200629
630 return 0;
631}
632
Lee Jones06e589e2012-06-20 13:56:37 +0100633static struct irq_domain_ops ab8500_irq_ops = {
634 .map = ab8500_irq_map,
635 .xlate = irq_domain_xlate_twocell,
636};
637
638static int ab8500_irq_init(struct ab8500 *ab8500, struct device_node *np)
Rabin Vincent62579262010-05-19 11:39:02 +0200639{
Linus Walleij2ced4452012-02-20 21:42:17 +0100640 int num_irqs;
Rabin Vincent62579262010-05-19 11:39:02 +0200641
Lee Jones3e1a4982013-02-25 14:57:35 +0000642 if (is_ab8540(ab8500))
643 num_irqs = AB8540_NR_IRQS;
644 else if (is_ab9540(ab8500))
Linus Walleijd6255522012-02-20 21:42:24 +0100645 num_irqs = AB9540_NR_IRQS;
Bengt Jonssona9823622012-03-08 14:01:57 +0100646 else if (is_ab8505(ab8500))
647 num_irqs = AB8505_NR_IRQS;
Linus Walleijd6255522012-02-20 21:42:24 +0100648 else
649 num_irqs = AB8500_NR_IRQS;
Linus Walleij2ced4452012-02-20 21:42:17 +0100650
Linus Walleijf1d11f32012-10-18 18:18:44 +0200651 /* If ->irq_base is zero this will give a linear mapping */
652 ab8500->domain = irq_domain_add_simple(NULL,
653 num_irqs, ab8500->irq_base,
654 &ab8500_irq_ops, ab8500);
Lee Jones06e589e2012-06-20 13:56:37 +0100655
656 if (!ab8500->domain) {
657 dev_err(ab8500->dev, "Failed to create irqdomain\n");
658 return -ENOSYS;
659 }
660
661 return 0;
Rabin Vincent62579262010-05-19 11:39:02 +0200662}
663
Jonas Aaberg112a80d2012-04-17 09:30:33 +0200664int ab8500_suspend(struct ab8500 *ab8500)
665{
666 if (atomic_read(&ab8500->transfer_ongoing))
667 return -EINVAL;
668 else
669 return 0;
670}
671
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500672static struct resource ab8500_gpadc_resources[] = {
Rabin Vincent62579262010-05-19 11:39:02 +0200673 {
674 .name = "HW_CONV_END",
675 .start = AB8500_INT_GP_HW_ADC_CONV_END,
676 .end = AB8500_INT_GP_HW_ADC_CONV_END,
677 .flags = IORESOURCE_IRQ,
678 },
679 {
680 .name = "SW_CONV_END",
681 .start = AB8500_INT_GP_SW_ADC_CONV_END,
682 .end = AB8500_INT_GP_SW_ADC_CONV_END,
683 .flags = IORESOURCE_IRQ,
684 },
685};
686
Lee Jones4b106fb2013-02-25 14:42:00 +0000687static struct resource ab8505_gpadc_resources[] = {
Lee Jonesc0eda9a2013-02-12 15:04:09 +0000688 {
689 .name = "SW_CONV_END",
690 .start = AB8500_INT_GP_SW_ADC_CONV_END,
691 .end = AB8500_INT_GP_SW_ADC_CONV_END,
692 .flags = IORESOURCE_IRQ,
693 },
694};
695
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500696static struct resource ab8500_rtc_resources[] = {
Rabin Vincent62579262010-05-19 11:39:02 +0200697 {
698 .name = "60S",
699 .start = AB8500_INT_RTC_60S,
700 .end = AB8500_INT_RTC_60S,
701 .flags = IORESOURCE_IRQ,
702 },
703 {
704 .name = "ALARM",
705 .start = AB8500_INT_RTC_ALARM,
706 .end = AB8500_INT_RTC_ALARM,
707 .flags = IORESOURCE_IRQ,
708 },
709};
710
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500711static struct resource ab8500_poweronkey_db_resources[] = {
Sundar R Iyer77686512010-09-05 12:18:47 -0700712 {
713 .name = "ONKEY_DBF",
714 .start = AB8500_INT_PON_KEY1DB_F,
715 .end = AB8500_INT_PON_KEY1DB_F,
716 .flags = IORESOURCE_IRQ,
717 },
718 {
719 .name = "ONKEY_DBR",
720 .start = AB8500_INT_PON_KEY1DB_R,
721 .end = AB8500_INT_PON_KEY1DB_R,
722 .flags = IORESOURCE_IRQ,
723 },
724};
725
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500726static struct resource ab8500_av_acc_detect_resources[] = {
Mattias Walline098aded72010-12-02 15:40:31 +0100727 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200728 .name = "ACC_DETECT_1DB_F",
729 .start = AB8500_INT_ACC_DETECT_1DB_F,
730 .end = AB8500_INT_ACC_DETECT_1DB_F,
731 .flags = IORESOURCE_IRQ,
Mattias Walline098aded72010-12-02 15:40:31 +0100732 },
733 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200734 .name = "ACC_DETECT_1DB_R",
735 .start = AB8500_INT_ACC_DETECT_1DB_R,
736 .end = AB8500_INT_ACC_DETECT_1DB_R,
737 .flags = IORESOURCE_IRQ,
Mattias Walline098aded72010-12-02 15:40:31 +0100738 },
739 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200740 .name = "ACC_DETECT_21DB_F",
741 .start = AB8500_INT_ACC_DETECT_21DB_F,
742 .end = AB8500_INT_ACC_DETECT_21DB_F,
743 .flags = IORESOURCE_IRQ,
744 },
745 {
746 .name = "ACC_DETECT_21DB_R",
747 .start = AB8500_INT_ACC_DETECT_21DB_R,
748 .end = AB8500_INT_ACC_DETECT_21DB_R,
749 .flags = IORESOURCE_IRQ,
750 },
751 {
752 .name = "ACC_DETECT_22DB_F",
753 .start = AB8500_INT_ACC_DETECT_22DB_F,
754 .end = AB8500_INT_ACC_DETECT_22DB_F,
755 .flags = IORESOURCE_IRQ,
756 },
757 {
758 .name = "ACC_DETECT_22DB_R",
759 .start = AB8500_INT_ACC_DETECT_22DB_R,
760 .end = AB8500_INT_ACC_DETECT_22DB_R,
761 .flags = IORESOURCE_IRQ,
762 },
763};
764
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500765static struct resource ab8500_charger_resources[] = {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200766 {
Mattias Walline098aded72010-12-02 15:40:31 +0100767 .name = "MAIN_CH_UNPLUG_DET",
768 .start = AB8500_INT_MAIN_CH_UNPLUG_DET,
769 .end = AB8500_INT_MAIN_CH_UNPLUG_DET,
770 .flags = IORESOURCE_IRQ,
771 },
772 {
773 .name = "MAIN_CHARGE_PLUG_DET",
774 .start = AB8500_INT_MAIN_CH_PLUG_DET,
775 .end = AB8500_INT_MAIN_CH_PLUG_DET,
776 .flags = IORESOURCE_IRQ,
777 },
778 {
Mattias Walline098aded72010-12-02 15:40:31 +0100779 .name = "VBUS_DET_R",
780 .start = AB8500_INT_VBUS_DET_R,
781 .end = AB8500_INT_VBUS_DET_R,
782 .flags = IORESOURCE_IRQ,
783 },
784 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200785 .name = "VBUS_DET_F",
786 .start = AB8500_INT_VBUS_DET_F,
787 .end = AB8500_INT_VBUS_DET_F,
Mattias Walline098aded72010-12-02 15:40:31 +0100788 .flags = IORESOURCE_IRQ,
789 },
790 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200791 .name = "USB_LINK_STATUS",
792 .start = AB8500_INT_USB_LINK_STATUS,
793 .end = AB8500_INT_USB_LINK_STATUS,
794 .flags = IORESOURCE_IRQ,
795 },
796 {
Mattias Walline098aded72010-12-02 15:40:31 +0100797 .name = "VBUS_OVV",
798 .start = AB8500_INT_VBUS_OVV,
799 .end = AB8500_INT_VBUS_OVV,
800 .flags = IORESOURCE_IRQ,
801 },
802 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200803 .name = "USB_CH_TH_PROT_R",
804 .start = AB8500_INT_USB_CH_TH_PROT_R,
805 .end = AB8500_INT_USB_CH_TH_PROT_R,
Mattias Walline098aded72010-12-02 15:40:31 +0100806 .flags = IORESOURCE_IRQ,
807 },
808 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200809 .name = "USB_CH_TH_PROT_F",
810 .start = AB8500_INT_USB_CH_TH_PROT_F,
811 .end = AB8500_INT_USB_CH_TH_PROT_F,
Mattias Walline098aded72010-12-02 15:40:31 +0100812 .flags = IORESOURCE_IRQ,
813 },
814 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200815 .name = "MAIN_EXT_CH_NOT_OK",
816 .start = AB8500_INT_MAIN_EXT_CH_NOT_OK,
817 .end = AB8500_INT_MAIN_EXT_CH_NOT_OK,
818 .flags = IORESOURCE_IRQ,
819 },
820 {
821 .name = "MAIN_CH_TH_PROT_R",
822 .start = AB8500_INT_MAIN_CH_TH_PROT_R,
823 .end = AB8500_INT_MAIN_CH_TH_PROT_R,
824 .flags = IORESOURCE_IRQ,
825 },
826 {
827 .name = "MAIN_CH_TH_PROT_F",
828 .start = AB8500_INT_MAIN_CH_TH_PROT_F,
829 .end = AB8500_INT_MAIN_CH_TH_PROT_F,
830 .flags = IORESOURCE_IRQ,
831 },
832 {
833 .name = "USB_CHARGER_NOT_OKR",
Bengt Jonssona9823622012-03-08 14:01:57 +0100834 .start = AB8500_INT_USB_CHARGER_NOT_OKR,
835 .end = AB8500_INT_USB_CHARGER_NOT_OKR,
Linus Walleij6af75ec2011-06-09 23:57:45 +0200836 .flags = IORESOURCE_IRQ,
837 },
838 {
839 .name = "CH_WD_EXP",
840 .start = AB8500_INT_CH_WD_EXP,
841 .end = AB8500_INT_CH_WD_EXP,
842 .flags = IORESOURCE_IRQ,
843 },
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100844 {
845 .name = "VBUS_CH_DROP_END",
846 .start = AB8500_INT_VBUS_CH_DROP_END,
847 .end = AB8500_INT_VBUS_CH_DROP_END,
848 .flags = IORESOURCE_IRQ,
849 },
Linus Walleij6af75ec2011-06-09 23:57:45 +0200850};
851
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500852static struct resource ab8500_btemp_resources[] = {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200853 {
854 .name = "BAT_CTRL_INDB",
855 .start = AB8500_INT_BAT_CTRL_INDB,
856 .end = AB8500_INT_BAT_CTRL_INDB,
Mattias Walline098aded72010-12-02 15:40:31 +0100857 .flags = IORESOURCE_IRQ,
858 },
859 {
860 .name = "BTEMP_LOW",
861 .start = AB8500_INT_BTEMP_LOW,
862 .end = AB8500_INT_BTEMP_LOW,
863 .flags = IORESOURCE_IRQ,
864 },
865 {
866 .name = "BTEMP_HIGH",
867 .start = AB8500_INT_BTEMP_HIGH,
868 .end = AB8500_INT_BTEMP_HIGH,
869 .flags = IORESOURCE_IRQ,
870 },
871 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200872 .name = "BTEMP_LOW_MEDIUM",
873 .start = AB8500_INT_BTEMP_LOW_MEDIUM,
874 .end = AB8500_INT_BTEMP_LOW_MEDIUM,
Mattias Walline098aded72010-12-02 15:40:31 +0100875 .flags = IORESOURCE_IRQ,
876 },
877 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200878 .name = "BTEMP_MEDIUM_HIGH",
879 .start = AB8500_INT_BTEMP_MEDIUM_HIGH,
880 .end = AB8500_INT_BTEMP_MEDIUM_HIGH,
Mattias Walline098aded72010-12-02 15:40:31 +0100881 .flags = IORESOURCE_IRQ,
882 },
883};
884
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500885static struct resource ab8500_fg_resources[] = {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200886 {
887 .name = "NCONV_ACCU",
888 .start = AB8500_INT_CCN_CONV_ACC,
889 .end = AB8500_INT_CCN_CONV_ACC,
890 .flags = IORESOURCE_IRQ,
891 },
892 {
893 .name = "BATT_OVV",
894 .start = AB8500_INT_BATT_OVV,
895 .end = AB8500_INT_BATT_OVV,
896 .flags = IORESOURCE_IRQ,
897 },
898 {
899 .name = "LOW_BAT_F",
900 .start = AB8500_INT_LOW_BAT_F,
901 .end = AB8500_INT_LOW_BAT_F,
902 .flags = IORESOURCE_IRQ,
903 },
904 {
905 .name = "LOW_BAT_R",
906 .start = AB8500_INT_LOW_BAT_R,
907 .end = AB8500_INT_LOW_BAT_R,
908 .flags = IORESOURCE_IRQ,
909 },
910 {
911 .name = "CC_INT_CALIB",
912 .start = AB8500_INT_CC_INT_CALIB,
913 .end = AB8500_INT_CC_INT_CALIB,
914 .flags = IORESOURCE_IRQ,
915 },
Bengt Jonssona9823622012-03-08 14:01:57 +0100916 {
917 .name = "CCEOC",
918 .start = AB8500_INT_CCEOC,
919 .end = AB8500_INT_CCEOC,
920 .flags = IORESOURCE_IRQ,
921 },
Linus Walleij6af75ec2011-06-09 23:57:45 +0200922};
923
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500924static struct resource ab8500_chargalg_resources[] = {};
Linus Walleij6af75ec2011-06-09 23:57:45 +0200925
Axel Lindf720642011-11-10 09:56:18 +0800926#ifdef CONFIG_DEBUG_FS
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500927static struct resource ab8500_debug_resources[] = {
Mattias Walline098aded72010-12-02 15:40:31 +0100928 {
929 .name = "IRQ_FIRST",
930 .start = AB8500_INT_MAIN_EXT_CH_NOT_OK,
931 .end = AB8500_INT_MAIN_EXT_CH_NOT_OK,
932 .flags = IORESOURCE_IRQ,
933 },
934 {
935 .name = "IRQ_LAST",
Bengt Jonssona9823622012-03-08 14:01:57 +0100936 .start = AB8500_INT_XTAL32K_KO,
937 .end = AB8500_INT_XTAL32K_KO,
Mattias Walline098aded72010-12-02 15:40:31 +0100938 .flags = IORESOURCE_IRQ,
939 },
940};
Axel Lindf720642011-11-10 09:56:18 +0800941#endif
Mattias Walline098aded72010-12-02 15:40:31 +0100942
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500943static struct resource ab8500_usb_resources[] = {
Mattias Walline098aded72010-12-02 15:40:31 +0100944 {
945 .name = "ID_WAKEUP_R",
946 .start = AB8500_INT_ID_WAKEUP_R,
947 .end = AB8500_INT_ID_WAKEUP_R,
948 .flags = IORESOURCE_IRQ,
949 },
950 {
951 .name = "ID_WAKEUP_F",
952 .start = AB8500_INT_ID_WAKEUP_F,
953 .end = AB8500_INT_ID_WAKEUP_F,
954 .flags = IORESOURCE_IRQ,
955 },
956 {
957 .name = "VBUS_DET_F",
958 .start = AB8500_INT_VBUS_DET_F,
959 .end = AB8500_INT_VBUS_DET_F,
960 .flags = IORESOURCE_IRQ,
961 },
962 {
963 .name = "VBUS_DET_R",
964 .start = AB8500_INT_VBUS_DET_R,
965 .end = AB8500_INT_VBUS_DET_R,
966 .flags = IORESOURCE_IRQ,
967 },
Mattias Wallin92d50a42010-12-07 11:20:47 +0100968 {
969 .name = "USB_LINK_STATUS",
970 .start = AB8500_INT_USB_LINK_STATUS,
971 .end = AB8500_INT_USB_LINK_STATUS,
972 .flags = IORESOURCE_IRQ,
973 },
Linus Walleij6af75ec2011-06-09 23:57:45 +0200974 {
975 .name = "USB_ADP_PROBE_PLUG",
976 .start = AB8500_INT_ADP_PROBE_PLUG,
977 .end = AB8500_INT_ADP_PROBE_PLUG,
978 .flags = IORESOURCE_IRQ,
979 },
980 {
981 .name = "USB_ADP_PROBE_UNPLUG",
982 .start = AB8500_INT_ADP_PROBE_UNPLUG,
983 .end = AB8500_INT_ADP_PROBE_UNPLUG,
984 .flags = IORESOURCE_IRQ,
985 },
Mattias Walline098aded72010-12-02 15:40:31 +0100986};
987
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500988static struct resource ab8505_iddet_resources[] = {
Virupax Sadashivpetimath44f72e52012-04-17 09:30:14 +0200989 {
990 .name = "KeyDeglitch",
991 .start = AB8505_INT_KEYDEGLITCH,
992 .end = AB8505_INT_KEYDEGLITCH,
993 .flags = IORESOURCE_IRQ,
994 },
995 {
996 .name = "KP",
997 .start = AB8505_INT_KP,
998 .end = AB8505_INT_KP,
999 .flags = IORESOURCE_IRQ,
1000 },
1001 {
1002 .name = "IKP",
1003 .start = AB8505_INT_IKP,
1004 .end = AB8505_INT_IKP,
1005 .flags = IORESOURCE_IRQ,
1006 },
1007 {
1008 .name = "IKR",
1009 .start = AB8505_INT_IKR,
1010 .end = AB8505_INT_IKR,
1011 .flags = IORESOURCE_IRQ,
1012 },
1013 {
1014 .name = "KeyStuck",
1015 .start = AB8505_INT_KEYSTUCK,
1016 .end = AB8505_INT_KEYSTUCK,
1017 .flags = IORESOURCE_IRQ,
1018 },
Lee Jones492390c2013-02-25 15:06:18 +00001019 {
1020 .name = "VBUS_DET_R",
1021 .start = AB8500_INT_VBUS_DET_R,
1022 .end = AB8500_INT_VBUS_DET_R,
1023 .flags = IORESOURCE_IRQ,
1024 },
1025 {
1026 .name = "VBUS_DET_F",
1027 .start = AB8500_INT_VBUS_DET_F,
1028 .end = AB8500_INT_VBUS_DET_F,
1029 .flags = IORESOURCE_IRQ,
1030 },
1031 {
1032 .name = "ID_DET_PLUGR",
1033 .start = AB8500_INT_ID_DET_PLUGR,
1034 .end = AB8500_INT_ID_DET_PLUGR,
1035 .flags = IORESOURCE_IRQ,
1036 },
1037 {
1038 .name = "ID_DET_PLUGF",
1039 .start = AB8500_INT_ID_DET_PLUGF,
1040 .end = AB8500_INT_ID_DET_PLUGF,
1041 .flags = IORESOURCE_IRQ,
1042 },
Virupax Sadashivpetimath44f72e52012-04-17 09:30:14 +02001043};
1044
Bill Pembertona9e9ce42012-11-19 13:24:21 -05001045static struct resource ab8500_temp_resources[] = {
Mattias Walline098aded72010-12-02 15:40:31 +01001046 {
Hongbo Zhang151621a2013-01-30 18:21:27 +08001047 .name = "ABX500_TEMP_WARM",
Mattias Walline098aded72010-12-02 15:40:31 +01001048 .start = AB8500_INT_TEMP_WARM,
1049 .end = AB8500_INT_TEMP_WARM,
1050 .flags = IORESOURCE_IRQ,
1051 },
1052};
1053
Bill Pembertona9e9ce42012-11-19 13:24:21 -05001054static struct mfd_cell ab8500_bm_devs[] = {
Rickard Andersson6ef94182012-04-17 09:30:57 +02001055 {
1056 .name = "ab8500-charger",
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08001057 .of_compatible = "stericsson,ab8500-charger",
Rickard Andersson6ef94182012-04-17 09:30:57 +02001058 .num_resources = ARRAY_SIZE(ab8500_charger_resources),
1059 .resources = ab8500_charger_resources,
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08001060 .platform_data = &ab8500_bm_data,
1061 .pdata_size = sizeof(ab8500_bm_data),
Rickard Andersson6ef94182012-04-17 09:30:57 +02001062 },
1063 {
1064 .name = "ab8500-btemp",
Rajanikanth H.Vbd9e8ab2012-11-18 19:16:58 -08001065 .of_compatible = "stericsson,ab8500-btemp",
Rickard Andersson6ef94182012-04-17 09:30:57 +02001066 .num_resources = ARRAY_SIZE(ab8500_btemp_resources),
1067 .resources = ab8500_btemp_resources,
Rajanikanth H.Vbd9e8ab2012-11-18 19:16:58 -08001068 .platform_data = &ab8500_bm_data,
1069 .pdata_size = sizeof(ab8500_bm_data),
Rickard Andersson6ef94182012-04-17 09:30:57 +02001070 },
1071 {
1072 .name = "ab8500-fg",
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -08001073 .of_compatible = "stericsson,ab8500-fg",
Rickard Andersson6ef94182012-04-17 09:30:57 +02001074 .num_resources = ARRAY_SIZE(ab8500_fg_resources),
1075 .resources = ab8500_fg_resources,
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -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-chargalg",
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00001081 .of_compatible = "stericsson,ab8500-chargalg",
Rickard Andersson6ef94182012-04-17 09:30:57 +02001082 .num_resources = ARRAY_SIZE(ab8500_chargalg_resources),
1083 .resources = ab8500_chargalg_resources,
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00001084 .platform_data = &ab8500_bm_data,
1085 .pdata_size = sizeof(ab8500_bm_data),
Rickard Andersson6ef94182012-04-17 09:30:57 +02001086 },
1087};
1088
Bill Pembertona9e9ce42012-11-19 13:24:21 -05001089static struct mfd_cell ab8500_devs[] = {
Lee Jones4b106fb2013-02-25 14:42:00 +00001090#ifdef CONFIG_DEBUG_FS
Linus Walleijd6255522012-02-20 21:42:24 +01001091 {
Lee Jones4b106fb2013-02-25 14:42:00 +00001092 .name = "ab8500-debug",
1093 .of_compatible = "stericsson,ab8500-debug",
1094 .num_resources = ARRAY_SIZE(ab8500_debug_resources),
1095 .resources = ab8500_debug_resources,
1096 },
1097#endif
1098 {
1099 .name = "ab8500-sysctrl",
1100 .of_compatible = "stericsson,ab8500-sysctrl",
1101 },
1102 {
1103 .name = "ab8500-regulator",
1104 .of_compatible = "stericsson,ab8500-regulator",
1105 },
1106 {
1107 .name = "abx500-clk",
1108 .of_compatible = "stericsson,abx500-clk",
1109 },
1110 {
1111 .name = "ab8500-gpadc",
1112 .num_resources = ARRAY_SIZE(ab8500_gpadc_resources),
1113 .resources = ab8500_gpadc_resources,
1114 },
1115 {
1116 .name = "ab8500-rtc",
1117 .of_compatible = "stericsson,ab8500-rtc",
1118 .num_resources = ARRAY_SIZE(ab8500_rtc_resources),
1119 .resources = ab8500_rtc_resources,
1120 },
1121 {
1122 .name = "ab8500-acc-det",
1123 .of_compatible = "stericsson,ab8500-acc-det",
1124 .num_resources = ARRAY_SIZE(ab8500_av_acc_detect_resources),
1125 .resources = ab8500_av_acc_detect_resources,
1126 },
1127 {
1128
1129 .name = "ab8500-poweron-key",
1130 .of_compatible = "stericsson,ab8500-poweron-key",
1131 .num_resources = ARRAY_SIZE(ab8500_poweronkey_db_resources),
1132 .resources = ab8500_poweronkey_db_resources,
1133 },
1134 {
1135 .name = "ab8500-pwm",
1136 .of_compatible = "stericsson,ab8500-pwm",
1137 .id = 1,
1138 },
1139 {
1140 .name = "ab8500-pwm",
1141 .of_compatible = "stericsson,ab8500-pwm",
1142 .id = 2,
1143 },
1144 {
1145 .name = "ab8500-pwm",
1146 .of_compatible = "stericsson,ab8500-pwm",
1147 .id = 3,
1148 },
1149 {
1150 .name = "ab8500-leds",
1151 .of_compatible = "stericsson,ab8500-leds",
1152 },
1153 {
1154 .name = "ab8500-denc",
1155 .of_compatible = "stericsson,ab8500-denc",
1156 },
1157 {
1158 .name = "ab8500-gpio",
Lee Jonesbad76992012-07-02 17:10:56 +02001159 .of_compatible = "stericsson,ab8500-gpio",
Linus Walleijd6255522012-02-20 21:42:24 +01001160 },
1161 {
Lee Jones4b106fb2013-02-25 14:42:00 +00001162 .name = "abx500-temp",
1163 .of_compatible = "stericsson,abx500-temp",
1164 .num_resources = ARRAY_SIZE(ab8500_temp_resources),
1165 .resources = ab8500_temp_resources,
1166 },
1167 {
Linus Walleijd6255522012-02-20 21:42:24 +01001168 .name = "ab8500-usb",
1169 .num_resources = ARRAY_SIZE(ab8500_usb_resources),
1170 .resources = ab8500_usb_resources,
1171 },
Virupax Sadashivpetimath44f72e52012-04-17 09:30:14 +02001172 {
1173 .name = "ab8500-codec",
Lee Jones4b106fb2013-02-25 14:42:00 +00001174 },
1175};
1176
1177static struct mfd_cell ab9540_devs[] = {
1178#ifdef CONFIG_DEBUG_FS
1179 {
1180 .name = "ab8500-debug",
1181 .num_resources = ARRAY_SIZE(ab8500_debug_resources),
1182 .resources = ab8500_debug_resources,
1183 },
1184#endif
1185 {
1186 .name = "ab8500-sysctrl",
1187 },
1188 {
1189 .name = "ab8500-regulator",
Virupax Sadashivpetimath44f72e52012-04-17 09:30:14 +02001190 },
Lee Jonesc0eda9a2013-02-12 15:04:09 +00001191 {
1192 .name = "ab8500-gpadc",
1193 .of_compatible = "stericsson,ab8500-gpadc",
1194 .num_resources = ARRAY_SIZE(ab8500_gpadc_resources),
1195 .resources = ab8500_gpadc_resources,
1196 },
Lee Jones4b106fb2013-02-25 14:42:00 +00001197 {
1198 .name = "ab8500-rtc",
1199 .num_resources = ARRAY_SIZE(ab8500_rtc_resources),
1200 .resources = ab8500_rtc_resources,
1201 },
1202 {
1203 .name = "ab8500-acc-det",
1204 .num_resources = ARRAY_SIZE(ab8500_av_acc_detect_resources),
1205 .resources = ab8500_av_acc_detect_resources,
1206 },
1207 {
1208 .name = "ab8500-poweron-key",
1209 .num_resources = ARRAY_SIZE(ab8500_poweronkey_db_resources),
1210 .resources = ab8500_poweronkey_db_resources,
1211 },
1212 {
1213 .name = "ab8500-pwm",
1214 .id = 1,
1215 },
1216 {
1217 .name = "ab8500-leds",
1218 },
1219 {
1220 .name = "abx500-temp",
1221 .num_resources = ARRAY_SIZE(ab8500_temp_resources),
1222 .resources = ab8500_temp_resources,
1223 },
Linus Walleijd6255522012-02-20 21:42:24 +01001224 {
Lee Jonese64d9052013-01-16 15:09:36 +00001225 .name = "pinctrl-ab9540",
1226 .of_compatible = "stericsson,ab9540-gpio",
Linus Walleijd6255522012-02-20 21:42:24 +01001227 },
1228 {
1229 .name = "ab9540-usb",
1230 .num_resources = ARRAY_SIZE(ab8500_usb_resources),
1231 .resources = ab8500_usb_resources,
1232 },
Virupax Sadashivpetimath44f72e52012-04-17 09:30:14 +02001233 {
1234 .name = "ab9540-codec",
1235 },
Lee Jonesc0eda9a2013-02-12 15:04:09 +00001236 {
Lee Jonesc0eda9a2013-02-12 15:04:09 +00001237 .name = "ab-iddet",
1238 .num_resources = ARRAY_SIZE(ab8505_iddet_resources),
1239 .resources = ab8505_iddet_resources,
1240 },
Virupax Sadashivpetimath44f72e52012-04-17 09:30:14 +02001241};
1242
Lee Jonesc0eda9a2013-02-12 15:04:09 +00001243/* Device list for ab8505 */
1244static struct mfd_cell ab8505_devs[] = {
Lee Jones4b106fb2013-02-25 14:42:00 +00001245#ifdef CONFIG_DEBUG_FS
1246 {
1247 .name = "ab8500-debug",
1248 .num_resources = ARRAY_SIZE(ab8500_debug_resources),
1249 .resources = ab8500_debug_resources,
1250 },
1251#endif
1252 {
1253 .name = "ab8500-sysctrl",
1254 },
1255 {
1256 .name = "ab8500-regulator",
1257 },
1258 {
1259 .name = "ab8500-gpadc",
1260 .num_resources = ARRAY_SIZE(ab8505_gpadc_resources),
1261 .resources = ab8505_gpadc_resources,
1262 },
1263 {
1264 .name = "ab8500-rtc",
1265 .num_resources = ARRAY_SIZE(ab8500_rtc_resources),
1266 .resources = ab8500_rtc_resources,
1267 },
1268 {
1269 .name = "ab8500-acc-det",
1270 .num_resources = ARRAY_SIZE(ab8500_av_acc_detect_resources),
1271 .resources = ab8500_av_acc_detect_resources,
1272 },
1273 {
1274 .name = "ab8500-poweron-key",
1275 .num_resources = ARRAY_SIZE(ab8500_poweronkey_db_resources),
1276 .resources = ab8500_poweronkey_db_resources,
1277 },
1278 {
1279 .name = "ab8500-pwm",
1280 .id = 1,
1281 },
1282 {
1283 .name = "ab8500-leds",
1284 },
1285 {
1286 .name = "ab8500-gpio",
1287 },
1288 {
1289 .name = "ab8500-usb",
1290 .num_resources = ARRAY_SIZE(ab8500_usb_resources),
1291 .resources = ab8500_usb_resources,
1292 },
1293 {
1294 .name = "ab8500-codec",
1295 },
Lee Jonesc0eda9a2013-02-12 15:04:09 +00001296 {
1297 .name = "ab-iddet",
1298 .num_resources = ARRAY_SIZE(ab8505_iddet_resources),
1299 .resources = ab8505_iddet_resources,
1300 },
1301};
1302
1303static struct mfd_cell ab8540_devs[] = {
Lee Jones4b106fb2013-02-25 14:42:00 +00001304#ifdef CONFIG_DEBUG_FS
1305 {
1306 .name = "ab8500-debug",
1307 .num_resources = ARRAY_SIZE(ab8500_debug_resources),
1308 .resources = ab8500_debug_resources,
1309 },
1310#endif
1311 {
1312 .name = "ab8500-sysctrl",
1313 },
1314 {
1315 .name = "ab8500-regulator",
1316 },
1317 {
1318 .name = "ab8500-gpadc",
1319 .num_resources = ARRAY_SIZE(ab8505_gpadc_resources),
1320 .resources = ab8505_gpadc_resources,
1321 },
1322 {
1323 .name = "ab8500-rtc",
1324 .num_resources = ARRAY_SIZE(ab8500_rtc_resources),
1325 .resources = ab8500_rtc_resources,
1326 },
1327 {
1328 .name = "ab8500-acc-det",
1329 .num_resources = ARRAY_SIZE(ab8500_av_acc_detect_resources),
1330 .resources = ab8500_av_acc_detect_resources,
1331 },
1332 {
1333 .name = "ab8500-poweron-key",
1334 .num_resources = ARRAY_SIZE(ab8500_poweronkey_db_resources),
1335 .resources = ab8500_poweronkey_db_resources,
1336 },
1337 {
1338 .name = "ab8500-pwm",
1339 .id = 1,
1340 },
1341 {
1342 .name = "ab8500-leds",
1343 },
1344 {
1345 .name = "abx500-temp",
1346 .num_resources = ARRAY_SIZE(ab8500_temp_resources),
1347 .resources = ab8500_temp_resources,
1348 },
Lee Jonesc0eda9a2013-02-12 15:04:09 +00001349 {
1350 .name = "ab8500-gpio",
1351 },
1352 {
1353 .name = "ab8540-usb",
1354 .num_resources = ARRAY_SIZE(ab8500_usb_resources),
1355 .resources = ab8500_usb_resources,
1356 },
1357 {
1358 .name = "ab8540-codec",
1359 },
1360 {
Virupax Sadashivpetimath44f72e52012-04-17 09:30:14 +02001361 .name = "ab-iddet",
1362 .num_resources = ARRAY_SIZE(ab8505_iddet_resources),
1363 .resources = ab8505_iddet_resources,
1364 },
Linus Walleijd6255522012-02-20 21:42:24 +01001365};
1366
Mattias Wallincca69b62010-12-02 15:09:36 +01001367static ssize_t show_chip_id(struct device *dev,
1368 struct device_attribute *attr, char *buf)
1369{
1370 struct ab8500 *ab8500;
1371
1372 ab8500 = dev_get_drvdata(dev);
Lee Jonese436ddf2013-02-26 10:09:41 +00001373
Mattias Wallincca69b62010-12-02 15:09:36 +01001374 return sprintf(buf, "%#x\n", ab8500 ? ab8500->chip_id : -EINVAL);
1375}
1376
Mattias Walline5c238c2011-03-02 11:52:36 +01001377/*
1378 * ab8500 has switched off due to (SWITCH_OFF_STATUS):
1379 * 0x01 Swoff bit programming
1380 * 0x02 Thermal protection activation
1381 * 0x04 Vbat lower then BattOk falling threshold
1382 * 0x08 Watchdog expired
1383 * 0x10 Non presence of 32kHz clock
1384 * 0x20 Battery level lower than power on reset threshold
1385 * 0x40 Power on key 1 pressed longer than 10 seconds
1386 * 0x80 DB8500 thermal shutdown
1387 */
1388static ssize_t show_switch_off_status(struct device *dev,
1389 struct device_attribute *attr, char *buf)
1390{
1391 int ret;
1392 u8 value;
1393 struct ab8500 *ab8500;
1394
1395 ab8500 = dev_get_drvdata(dev);
1396 ret = get_register_interruptible(ab8500, AB8500_RTC,
1397 AB8500_SWITCH_OFF_STATUS, &value);
1398 if (ret < 0)
1399 return ret;
1400 return sprintf(buf, "%#x\n", value);
1401}
1402
Rajkumar Kasirajanf04a9d82012-05-30 16:32:37 +05301403/* use mask and set to override the register turn_on_stat value */
1404void ab8500_override_turn_on_stat(u8 mask, u8 set)
1405{
1406 spin_lock(&on_stat_lock);
1407 turn_on_stat_mask = mask;
1408 turn_on_stat_set = set;
1409 spin_unlock(&on_stat_lock);
1410}
1411
Andrew Lynnb4a31032011-10-11 10:49:47 +02001412/*
1413 * ab8500 has turned on due to (TURN_ON_STATUS):
1414 * 0x01 PORnVbat
1415 * 0x02 PonKey1dbF
1416 * 0x04 PonKey2dbF
1417 * 0x08 RTCAlarm
1418 * 0x10 MainChDet
1419 * 0x20 VbusDet
1420 * 0x40 UsbIDDetect
1421 * 0x80 Reserved
1422 */
1423static ssize_t show_turn_on_status(struct device *dev,
1424 struct device_attribute *attr, char *buf)
1425{
1426 int ret;
1427 u8 value;
1428 struct ab8500 *ab8500;
1429
1430 ab8500 = dev_get_drvdata(dev);
1431 ret = get_register_interruptible(ab8500, AB8500_SYS_CTRL1_BLOCK,
1432 AB8500_TURN_ON_STATUS, &value);
1433 if (ret < 0)
1434 return ret;
Rajkumar Kasirajanf04a9d82012-05-30 16:32:37 +05301435
1436 /*
1437 * In L9540, turn_on_status register is not updated correctly if
1438 * the device is rebooted with AC/USB charger connected. Due to
1439 * this, the device boots android instead of entering into charge
1440 * only mode. Read the AC/USB status register to detect the charger
1441 * presence and update the turn on status manually.
1442 */
1443 if (is_ab9540(ab8500)) {
1444 spin_lock(&on_stat_lock);
1445 value = (value & turn_on_stat_mask) | turn_on_stat_set;
1446 spin_unlock(&on_stat_lock);
1447 }
1448
Andrew Lynnb4a31032011-10-11 10:49:47 +02001449 return sprintf(buf, "%#x\n", value);
1450}
1451
Linus Walleijd6255522012-02-20 21:42:24 +01001452static ssize_t show_ab9540_dbbrstn(struct device *dev,
1453 struct device_attribute *attr, char *buf)
1454{
1455 struct ab8500 *ab8500;
1456 int ret;
1457 u8 value;
1458
1459 ab8500 = dev_get_drvdata(dev);
1460
1461 ret = get_register_interruptible(ab8500, AB8500_REGU_CTRL2,
1462 AB9540_MODEM_CTRL2_REG, &value);
1463 if (ret < 0)
1464 return ret;
1465
1466 return sprintf(buf, "%d\n",
1467 (value & AB9540_MODEM_CTRL2_SWDBBRSTN_BIT) ? 1 : 0);
1468}
1469
1470static ssize_t store_ab9540_dbbrstn(struct device *dev,
1471 struct device_attribute *attr, const char *buf, size_t count)
1472{
1473 struct ab8500 *ab8500;
1474 int ret = count;
1475 int err;
1476 u8 bitvalues;
1477
1478 ab8500 = dev_get_drvdata(dev);
1479
1480 if (count > 0) {
1481 switch (buf[0]) {
1482 case '0':
1483 bitvalues = 0;
1484 break;
1485 case '1':
1486 bitvalues = AB9540_MODEM_CTRL2_SWDBBRSTN_BIT;
1487 break;
1488 default:
1489 goto exit;
1490 }
1491
1492 err = mask_and_set_register_interruptible(ab8500,
1493 AB8500_REGU_CTRL2, AB9540_MODEM_CTRL2_REG,
1494 AB9540_MODEM_CTRL2_SWDBBRSTN_BIT, bitvalues);
1495 if (err)
1496 dev_info(ab8500->dev,
1497 "Failed to set DBBRSTN %c, err %#x\n",
1498 buf[0], err);
1499 }
1500
1501exit:
1502 return ret;
1503}
1504
Mattias Wallincca69b62010-12-02 15:09:36 +01001505static DEVICE_ATTR(chip_id, S_IRUGO, show_chip_id, NULL);
Mattias Walline5c238c2011-03-02 11:52:36 +01001506static DEVICE_ATTR(switch_off_status, S_IRUGO, show_switch_off_status, NULL);
Andrew Lynnb4a31032011-10-11 10:49:47 +02001507static DEVICE_ATTR(turn_on_status, S_IRUGO, show_turn_on_status, NULL);
Linus Walleijd6255522012-02-20 21:42:24 +01001508static DEVICE_ATTR(dbbrstn, S_IRUGO | S_IWUSR,
1509 show_ab9540_dbbrstn, store_ab9540_dbbrstn);
Mattias Wallincca69b62010-12-02 15:09:36 +01001510
1511static struct attribute *ab8500_sysfs_entries[] = {
1512 &dev_attr_chip_id.attr,
Mattias Walline5c238c2011-03-02 11:52:36 +01001513 &dev_attr_switch_off_status.attr,
Andrew Lynnb4a31032011-10-11 10:49:47 +02001514 &dev_attr_turn_on_status.attr,
Mattias Wallincca69b62010-12-02 15:09:36 +01001515 NULL,
1516};
1517
Linus Walleijd6255522012-02-20 21:42:24 +01001518static struct attribute *ab9540_sysfs_entries[] = {
1519 &dev_attr_chip_id.attr,
1520 &dev_attr_switch_off_status.attr,
1521 &dev_attr_turn_on_status.attr,
1522 &dev_attr_dbbrstn.attr,
1523 NULL,
1524};
1525
Mattias Wallincca69b62010-12-02 15:09:36 +01001526static struct attribute_group ab8500_attr_group = {
1527 .attrs = ab8500_sysfs_entries,
1528};
1529
Linus Walleijd6255522012-02-20 21:42:24 +01001530static struct attribute_group ab9540_attr_group = {
1531 .attrs = ab9540_sysfs_entries,
1532};
1533
Bill Pembertonf791be42012-11-19 13:23:04 -05001534static int ab8500_probe(struct platform_device *pdev)
Rabin Vincent62579262010-05-19 11:39:02 +02001535{
Jonas Aabergb04c530c2012-06-29 17:46:12 +02001536 static char *switch_off_status[] = {
1537 "Swoff bit programming",
1538 "Thermal protection activation",
1539 "Vbat lower then BattOk falling threshold",
1540 "Watchdog expired",
1541 "Non presence of 32kHz clock",
1542 "Battery level lower than power on reset threshold",
1543 "Power on key 1 pressed longer than 10 seconds",
1544 "DB8500 thermal shutdown"};
Mattias Wallinabee26c2012-09-28 09:34:24 +02001545 static char *turn_on_status[] = {
1546 "Battery rising (Vbat)",
1547 "Power On Key 1 dbF",
1548 "Power On Key 2 dbF",
1549 "RTC Alarm",
1550 "Main Charger Detect",
1551 "Vbus Detect (USB)",
1552 "USB ID Detect",
1553 "UART Factory Mode Detect"};
Lee Jonesd28f1db2012-05-19 17:21:37 +02001554 struct ab8500_platform_data *plat = dev_get_platdata(&pdev->dev);
1555 const struct platform_device_id *platid = platform_get_device_id(pdev);
Lee Jones6bc4a562012-05-17 14:45:13 +01001556 enum ab8500_version version = AB8500_VERSION_UNDEFINED;
1557 struct device_node *np = pdev->dev.of_node;
Lee Jonesd28f1db2012-05-19 17:21:37 +02001558 struct ab8500 *ab8500;
1559 struct resource *resource;
Rabin Vincent62579262010-05-19 11:39:02 +02001560 int ret;
1561 int i;
Mattias Wallin47c16972010-09-10 17:47:56 +02001562 u8 value;
Rabin Vincent62579262010-05-19 11:39:02 +02001563
Lee Jones8c4203c2012-11-05 16:10:35 +01001564 ab8500 = devm_kzalloc(&pdev->dev, sizeof *ab8500, GFP_KERNEL);
Lee Jonesd28f1db2012-05-19 17:21:37 +02001565 if (!ab8500)
1566 return -ENOMEM;
1567
Rabin Vincent62579262010-05-19 11:39:02 +02001568 if (plat)
1569 ab8500->irq_base = plat->irq_base;
1570
Lee Jonesd28f1db2012-05-19 17:21:37 +02001571 ab8500->dev = &pdev->dev;
1572
1573 resource = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Lee Jones8c4203c2012-11-05 16:10:35 +01001574 if (!resource)
1575 return -ENODEV;
Lee Jonesd28f1db2012-05-19 17:21:37 +02001576
1577 ab8500->irq = resource->start;
1578
Lee Jones822672a2012-06-20 13:56:38 +01001579 ab8500->read = ab8500_prcmu_read;
1580 ab8500->write = ab8500_prcmu_write;
1581 ab8500->write_masked = ab8500_prcmu_write_masked;
Lee Jonesd28f1db2012-05-19 17:21:37 +02001582
Rabin Vincent62579262010-05-19 11:39:02 +02001583 mutex_init(&ab8500->lock);
1584 mutex_init(&ab8500->irq_lock);
Jonas Aaberg112a80d2012-04-17 09:30:33 +02001585 atomic_set(&ab8500->transfer_ongoing, 0);
Rabin Vincent62579262010-05-19 11:39:02 +02001586
Lee Jonesd28f1db2012-05-19 17:21:37 +02001587 platform_set_drvdata(pdev, ab8500);
1588
Lee Jones6bc4a562012-05-17 14:45:13 +01001589 if (platid)
1590 version = platid->driver_data;
Lee Jones6bc4a562012-05-17 14:45:13 +01001591
Linus Walleij0f6208372012-02-20 21:42:10 +01001592 if (version != AB8500_VERSION_UNDEFINED)
1593 ab8500->version = version;
1594 else {
1595 ret = get_register_interruptible(ab8500, AB8500_MISC,
1596 AB8500_IC_NAME_REG, &value);
1597 if (ret < 0)
Lee Jones8c4203c2012-11-05 16:10:35 +01001598 return ret;
Linus Walleij0f6208372012-02-20 21:42:10 +01001599
1600 ab8500->version = value;
1601 }
1602
Mattias Wallin47c16972010-09-10 17:47:56 +02001603 ret = get_register_interruptible(ab8500, AB8500_MISC,
1604 AB8500_REV_REG, &value);
Rabin Vincent62579262010-05-19 11:39:02 +02001605 if (ret < 0)
Lee Jones8c4203c2012-11-05 16:10:35 +01001606 return ret;
Rabin Vincent62579262010-05-19 11:39:02 +02001607
Mattias Wallin47c16972010-09-10 17:47:56 +02001608 ab8500->chip_id = value;
Rabin Vincent62579262010-05-19 11:39:02 +02001609
Linus Walleij0f6208372012-02-20 21:42:10 +01001610 dev_info(ab8500->dev, "detected chip, %s rev. %1x.%1x\n",
1611 ab8500_version_str[ab8500->version],
1612 ab8500->chip_id >> 4,
1613 ab8500->chip_id & 0x0F);
1614
Lee Jones3e1a4982013-02-25 14:57:35 +00001615 /* Configure AB8540 */
1616 if (is_ab8540(ab8500)) {
1617 ab8500->mask_size = AB8540_NUM_IRQ_REGS;
1618 ab8500->irq_reg_offset = ab8540_irq_regoffset;
1619 ab8500->it_latchhier_num = AB8540_IT_LATCHHIER_NUM;
1620 }/* Configure AB8500 or AB9540 IRQ */
1621 else if (is_ab9540(ab8500) || is_ab8505(ab8500)) {
Linus Walleijd6255522012-02-20 21:42:24 +01001622 ab8500->mask_size = AB9540_NUM_IRQ_REGS;
1623 ab8500->irq_reg_offset = ab9540_irq_regoffset;
Lee Jones3e1a4982013-02-25 14:57:35 +00001624 ab8500->it_latchhier_num = AB8500_IT_LATCHHIER_NUM;
Linus Walleijd6255522012-02-20 21:42:24 +01001625 } else {
1626 ab8500->mask_size = AB8500_NUM_IRQ_REGS;
1627 ab8500->irq_reg_offset = ab8500_irq_regoffset;
Lee Jones3e1a4982013-02-25 14:57:35 +00001628 ab8500->it_latchhier_num = AB8500_IT_LATCHHIER_NUM;
Linus Walleijd6255522012-02-20 21:42:24 +01001629 }
Lee Jones8c4203c2012-11-05 16:10:35 +01001630 ab8500->mask = devm_kzalloc(&pdev->dev, ab8500->mask_size, GFP_KERNEL);
Linus Walleij2ced4452012-02-20 21:42:17 +01001631 if (!ab8500->mask)
1632 return -ENOMEM;
Lee Jones8c4203c2012-11-05 16:10:35 +01001633 ab8500->oldmask = devm_kzalloc(&pdev->dev, ab8500->mask_size, GFP_KERNEL);
1634 if (!ab8500->oldmask)
1635 return -ENOMEM;
1636
Mattias Walline5c238c2011-03-02 11:52:36 +01001637 /*
1638 * ab8500 has switched off due to (SWITCH_OFF_STATUS):
1639 * 0x01 Swoff bit programming
1640 * 0x02 Thermal protection activation
1641 * 0x04 Vbat lower then BattOk falling threshold
1642 * 0x08 Watchdog expired
1643 * 0x10 Non presence of 32kHz clock
1644 * 0x20 Battery level lower than power on reset threshold
1645 * 0x40 Power on key 1 pressed longer than 10 seconds
1646 * 0x80 DB8500 thermal shutdown
1647 */
1648
1649 ret = get_register_interruptible(ab8500, AB8500_RTC,
1650 AB8500_SWITCH_OFF_STATUS, &value);
1651 if (ret < 0)
1652 return ret;
Jonas Aabergb04c530c2012-06-29 17:46:12 +02001653 dev_info(ab8500->dev, "switch off cause(s) (%#x): ", value);
1654
1655 if (value) {
1656 for (i = 0; i < ARRAY_SIZE(switch_off_status); i++) {
1657 if (value & 1)
1658 printk(KERN_CONT " \"%s\"",
1659 switch_off_status[i]);
1660 value = value >> 1;
1661
1662 }
1663 printk(KERN_CONT "\n");
1664 } else {
1665 printk(KERN_CONT " None\n");
1666 }
Mattias Wallinabee26c2012-09-28 09:34:24 +02001667 ret = get_register_interruptible(ab8500, AB8500_SYS_CTRL1_BLOCK,
1668 AB8500_TURN_ON_STATUS, &value);
1669 if (ret < 0)
1670 return ret;
1671 dev_info(ab8500->dev, "turn on reason(s) (%#x): ", value);
1672
1673 if (value) {
1674 for (i = 0; i < ARRAY_SIZE(turn_on_status); i++) {
1675 if (value & 1)
1676 printk("\"%s\" ", turn_on_status[i]);
1677 value = value >> 1;
1678 }
1679 printk("\n");
1680 } else {
1681 printk("None\n");
1682 }
Mattias Walline5c238c2011-03-02 11:52:36 +01001683
Rabin Vincent62579262010-05-19 11:39:02 +02001684 if (plat && plat->init)
1685 plat->init(ab8500);
Mattias Wallinabee26c2012-09-28 09:34:24 +02001686
Rajkumar Kasirajanf04a9d82012-05-30 16:32:37 +05301687 if (is_ab9540(ab8500)) {
1688 ret = get_register_interruptible(ab8500, AB8500_CHARGER,
1689 AB8500_CH_USBCH_STAT1_REG, &value);
1690 if (ret < 0)
1691 return ret;
1692 if ((value & VBUS_DET_DBNC1) && (value & VBUS_DET_DBNC100))
1693 ab8500_override_turn_on_stat(~AB8500_POW_KEY_1_ON,
1694 AB8500_VBUS_DET);
1695 }
Rabin Vincent62579262010-05-19 11:39:02 +02001696
1697 /* Clear and mask all interrupts */
Linus Walleij2ced4452012-02-20 21:42:17 +01001698 for (i = 0; i < ab8500->mask_size; i++) {
Linus Walleij0f6208372012-02-20 21:42:10 +01001699 /*
1700 * Interrupt register 12 doesn't exist prior to AB8500 version
1701 * 2.0
1702 */
1703 if (ab8500->irq_reg_offset[i] == 11 &&
1704 is_ab8500_1p1_or_earlier(ab8500))
Mattias Wallin92d50a42010-12-07 11:20:47 +01001705 continue;
Rabin Vincent62579262010-05-19 11:39:02 +02001706
Lee Jones3e1a4982013-02-25 14:57:35 +00001707 if (ab8500->irq_reg_offset[i] < 0)
1708 continue;
1709
Mattias Wallin47c16972010-09-10 17:47:56 +02001710 get_register_interruptible(ab8500, AB8500_INTERRUPT,
Linus Walleij2ced4452012-02-20 21:42:17 +01001711 AB8500_IT_LATCH1_REG + ab8500->irq_reg_offset[i],
Mattias Wallin92d50a42010-12-07 11:20:47 +01001712 &value);
Mattias Wallin47c16972010-09-10 17:47:56 +02001713 set_register_interruptible(ab8500, AB8500_INTERRUPT,
Linus Walleij2ced4452012-02-20 21:42:17 +01001714 AB8500_IT_MASK1_REG + ab8500->irq_reg_offset[i], 0xff);
Rabin Vincent62579262010-05-19 11:39:02 +02001715 }
1716
Mattias Wallin47c16972010-09-10 17:47:56 +02001717 ret = abx500_register_ops(ab8500->dev, &ab8500_ops);
1718 if (ret)
Lee Jones8c4203c2012-11-05 16:10:35 +01001719 return ret;
Mattias Wallin47c16972010-09-10 17:47:56 +02001720
Linus Walleij2ced4452012-02-20 21:42:17 +01001721 for (i = 0; i < ab8500->mask_size; i++)
Rabin Vincent62579262010-05-19 11:39:02 +02001722 ab8500->mask[i] = ab8500->oldmask[i] = 0xff;
1723
Lee Jones06e589e2012-06-20 13:56:37 +01001724 ret = ab8500_irq_init(ab8500, np);
1725 if (ret)
Lee Jones8c4203c2012-11-05 16:10:35 +01001726 return ret;
Rabin Vincent62579262010-05-19 11:39:02 +02001727
Lee Jones06e589e2012-06-20 13:56:37 +01001728 /* Activate this feature only in ab9540 */
1729 /* till tests are done on ab8500 1p2 or later*/
Lee Jonese436ddf2013-02-26 10:09:41 +00001730 if (is_ab9540(ab8500) || is_ab8540(ab8500))
Lee Jones8c4203c2012-11-05 16:10:35 +01001731 ret = devm_request_threaded_irq(&pdev->dev, ab8500->irq, NULL,
1732 ab8500_hierarchical_irq,
1733 IRQF_ONESHOT | IRQF_NO_SUSPEND,
1734 "ab8500", ab8500);
Lee Jones06e589e2012-06-20 13:56:37 +01001735 }
1736 else {
Lee Jones8c4203c2012-11-05 16:10:35 +01001737 ret = devm_request_threaded_irq(&pdev->dev, ab8500->irq, NULL,
1738 ab8500_irq,
1739 IRQF_ONESHOT | IRQF_NO_SUSPEND,
1740 "ab8500", ab8500);
Rabin Vincent62579262010-05-19 11:39:02 +02001741 if (ret)
Lee Jones8c4203c2012-11-05 16:10:35 +01001742 return ret;
Rabin Vincent62579262010-05-19 11:39:02 +02001743 }
1744
Lee Jonesbad76992012-07-02 17:10:56 +02001745 if (is_ab9540(ab8500))
1746 ret = mfd_add_devices(ab8500->dev, 0, ab9540_devs,
1747 ARRAY_SIZE(ab9540_devs), NULL,
Mark Brown55692af2012-09-11 15:16:36 +08001748 ab8500->irq_base, ab8500->domain);
Lee Jonesc0eda9a2013-02-12 15:04:09 +00001749 else if (is_ab8540(ab8500))
1750 ret = mfd_add_devices(ab8500->dev, 0, ab8540_devs,
1751 ARRAY_SIZE(ab8540_devs), NULL,
1752 ab8500->irq_base, ab8500->domain);
1753 else if (is_ab8505(ab8500))
1754 ret = mfd_add_devices(ab8500->dev, 0, ab8505_devs,
1755 ARRAY_SIZE(ab8505_devs), NULL,
1756 ab8500->irq_base, ab8500->domain);
Lee Jonesbad76992012-07-02 17:10:56 +02001757 else
1758 ret = mfd_add_devices(ab8500->dev, 0, ab8500_devs,
1759 ARRAY_SIZE(ab8500_devs), NULL,
Mark Brown55692af2012-09-11 15:16:36 +08001760 ab8500->irq_base, ab8500->domain);
Lee Jonesbad76992012-07-02 17:10:56 +02001761 if (ret)
Lee Jones8c4203c2012-11-05 16:10:35 +01001762 return ret;
Linus Walleijd6255522012-02-20 21:42:24 +01001763
Rickard Andersson6ef94182012-04-17 09:30:57 +02001764 if (!no_bm) {
1765 /* Add battery management devices */
1766 ret = mfd_add_devices(ab8500->dev, 0, ab8500_bm_devs,
1767 ARRAY_SIZE(ab8500_bm_devs), NULL,
Mark Brown55692af2012-09-11 15:16:36 +08001768 ab8500->irq_base, ab8500->domain);
Rickard Andersson6ef94182012-04-17 09:30:57 +02001769 if (ret)
1770 dev_err(ab8500->dev, "error adding bm devices\n");
1771 }
1772
Lee Jonese436ddf2013-02-26 10:09:41 +00001773 if (((is_ab8505(ab8500) || is_ab9540(ab8500)) &&
1774 ab8500->chip_id >= AB8500_CUT2P0) || is_ab8540(ab8500))
Linus Walleijd6255522012-02-20 21:42:24 +01001775 ret = sysfs_create_group(&ab8500->dev->kobj,
1776 &ab9540_attr_group);
1777 else
1778 ret = sysfs_create_group(&ab8500->dev->kobj,
1779 &ab8500_attr_group);
Mattias Wallincca69b62010-12-02 15:09:36 +01001780 if (ret)
1781 dev_err(ab8500->dev, "error creating sysfs entries\n");
Lee Jones06e589e2012-06-20 13:56:37 +01001782
1783 return ret;
Rabin Vincent62579262010-05-19 11:39:02 +02001784}
1785
Bill Pemberton4740f732012-11-19 13:26:01 -05001786static int ab8500_remove(struct platform_device *pdev)
Rabin Vincent62579262010-05-19 11:39:02 +02001787{
Lee Jonesd28f1db2012-05-19 17:21:37 +02001788 struct ab8500 *ab8500 = platform_get_drvdata(pdev);
1789
Lee Jonese436ddf2013-02-26 10:09:41 +00001790 if (((is_ab8505(ab8500) || is_ab9540(ab8500)) &&
1791 ab8500->chip_id >= AB8500_CUT2P0) || is_ab8540(ab8500))
Linus Walleijd6255522012-02-20 21:42:24 +01001792 sysfs_remove_group(&ab8500->dev->kobj, &ab9540_attr_group);
1793 else
1794 sysfs_remove_group(&ab8500->dev->kobj, &ab8500_attr_group);
Lee Jones06e589e2012-06-20 13:56:37 +01001795
Rabin Vincent62579262010-05-19 11:39:02 +02001796 mfd_remove_devices(ab8500->dev);
Rabin Vincent62579262010-05-19 11:39:02 +02001797
1798 return 0;
1799}
1800
Lee Jonesd28f1db2012-05-19 17:21:37 +02001801static const struct platform_device_id ab8500_id[] = {
1802 { "ab8500-core", AB8500_VERSION_AB8500 },
1803 { "ab8505-i2c", AB8500_VERSION_AB8505 },
1804 { "ab9540-i2c", AB8500_VERSION_AB9540 },
1805 { "ab8540-i2c", AB8500_VERSION_AB8540 },
1806 { }
1807};
1808
1809static struct platform_driver ab8500_core_driver = {
1810 .driver = {
1811 .name = "ab8500-core",
1812 .owner = THIS_MODULE,
1813 },
1814 .probe = ab8500_probe,
Bill Pemberton84449212012-11-19 13:20:24 -05001815 .remove = ab8500_remove,
Lee Jonesd28f1db2012-05-19 17:21:37 +02001816 .id_table = ab8500_id,
1817};
1818
1819static int __init ab8500_core_init(void)
1820{
1821 return platform_driver_register(&ab8500_core_driver);
1822}
1823
1824static void __exit ab8500_core_exit(void)
1825{
1826 platform_driver_unregister(&ab8500_core_driver);
1827}
Lee Jonesba7cbc32012-06-11 16:25:00 +01001828core_initcall(ab8500_core_init);
Lee Jonesd28f1db2012-05-19 17:21:37 +02001829module_exit(ab8500_core_exit);
1830
Mattias Wallinadceed62011-03-02 11:51:11 +01001831MODULE_AUTHOR("Mattias Wallin, Srinidhi Kasagar, Rabin Vincent");
Rabin Vincent62579262010-05-19 11:39:02 +02001832MODULE_DESCRIPTION("AB8500 MFD core");
1833MODULE_LICENSE("GPL v2");