blob: 1e9173804ede2bacb93a7179d5d7c817c79dfcf5 [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>
14#include <linux/delay.h>
15#include <linux/interrupt.h>
16#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/mfd/core.h>
Mattias Wallin47c16972010-09-10 17:47:56 +020019#include <linux/mfd/abx500.h>
Rabin Vincent62579262010-05-19 11:39:02 +020020#include <linux/mfd/ab8500.h>
Sundar R Iyer549931f2010-07-13 11:51:28 +053021#include <linux/regulator/ab8500.h>
Rabin Vincent62579262010-05-19 11:39:02 +020022
23/*
24 * Interrupt register offsets
25 * Bank : 0x0E
26 */
Mattias Wallin47c16972010-09-10 17:47:56 +020027#define AB8500_IT_SOURCE1_REG 0x00
28#define AB8500_IT_SOURCE2_REG 0x01
29#define AB8500_IT_SOURCE3_REG 0x02
30#define AB8500_IT_SOURCE4_REG 0x03
31#define AB8500_IT_SOURCE5_REG 0x04
32#define AB8500_IT_SOURCE6_REG 0x05
33#define AB8500_IT_SOURCE7_REG 0x06
34#define AB8500_IT_SOURCE8_REG 0x07
35#define AB8500_IT_SOURCE19_REG 0x12
36#define AB8500_IT_SOURCE20_REG 0x13
37#define AB8500_IT_SOURCE21_REG 0x14
38#define AB8500_IT_SOURCE22_REG 0x15
39#define AB8500_IT_SOURCE23_REG 0x16
40#define AB8500_IT_SOURCE24_REG 0x17
Rabin Vincent62579262010-05-19 11:39:02 +020041
42/*
43 * latch registers
44 */
Mattias Wallin47c16972010-09-10 17:47:56 +020045#define AB8500_IT_LATCH1_REG 0x20
46#define AB8500_IT_LATCH2_REG 0x21
47#define AB8500_IT_LATCH3_REG 0x22
48#define AB8500_IT_LATCH4_REG 0x23
49#define AB8500_IT_LATCH5_REG 0x24
50#define AB8500_IT_LATCH6_REG 0x25
51#define AB8500_IT_LATCH7_REG 0x26
52#define AB8500_IT_LATCH8_REG 0x27
53#define AB8500_IT_LATCH9_REG 0x28
54#define AB8500_IT_LATCH10_REG 0x29
Mattias Wallin92d50a42010-12-07 11:20:47 +010055#define AB8500_IT_LATCH12_REG 0x2B
Mattias Wallin47c16972010-09-10 17:47:56 +020056#define AB8500_IT_LATCH19_REG 0x32
57#define AB8500_IT_LATCH20_REG 0x33
58#define AB8500_IT_LATCH21_REG 0x34
59#define AB8500_IT_LATCH22_REG 0x35
60#define AB8500_IT_LATCH23_REG 0x36
61#define AB8500_IT_LATCH24_REG 0x37
Rabin Vincent62579262010-05-19 11:39:02 +020062
63/*
64 * mask registers
65 */
66
Mattias Wallin47c16972010-09-10 17:47:56 +020067#define AB8500_IT_MASK1_REG 0x40
68#define AB8500_IT_MASK2_REG 0x41
69#define AB8500_IT_MASK3_REG 0x42
70#define AB8500_IT_MASK4_REG 0x43
71#define AB8500_IT_MASK5_REG 0x44
72#define AB8500_IT_MASK6_REG 0x45
73#define AB8500_IT_MASK7_REG 0x46
74#define AB8500_IT_MASK8_REG 0x47
75#define AB8500_IT_MASK9_REG 0x48
76#define AB8500_IT_MASK10_REG 0x49
77#define AB8500_IT_MASK11_REG 0x4A
78#define AB8500_IT_MASK12_REG 0x4B
79#define AB8500_IT_MASK13_REG 0x4C
80#define AB8500_IT_MASK14_REG 0x4D
81#define AB8500_IT_MASK15_REG 0x4E
82#define AB8500_IT_MASK16_REG 0x4F
83#define AB8500_IT_MASK17_REG 0x50
84#define AB8500_IT_MASK18_REG 0x51
85#define AB8500_IT_MASK19_REG 0x52
86#define AB8500_IT_MASK20_REG 0x53
87#define AB8500_IT_MASK21_REG 0x54
88#define AB8500_IT_MASK22_REG 0x55
89#define AB8500_IT_MASK23_REG 0x56
90#define AB8500_IT_MASK24_REG 0x57
Rabin Vincent62579262010-05-19 11:39:02 +020091
Mattias Wallin47c16972010-09-10 17:47:56 +020092#define AB8500_REV_REG 0x80
Mattias Walline5c238c2011-03-02 11:52:36 +010093#define AB8500_SWITCH_OFF_STATUS 0x00
Rabin Vincent62579262010-05-19 11:39:02 +020094
Andrew Lynnb4a31032011-10-11 10:49:47 +020095#define AB8500_TURN_ON_STATUS 0x00
96
Rabin Vincent62579262010-05-19 11:39:02 +020097/*
98 * Map interrupt numbers to the LATCH and MASK register offsets, Interrupt
99 * numbers are indexed into this array with (num / 8).
100 *
101 * This is one off from the register names, i.e. AB8500_IT_MASK1_REG is at
102 * offset 0.
103 */
104static const int ab8500_irq_regoffset[AB8500_NUM_IRQ_REGS] = {
Mattias Wallin92d50a42010-12-07 11:20:47 +0100105 0, 1, 2, 3, 4, 6, 7, 8, 9, 11, 18, 19, 20, 21,
Rabin Vincent62579262010-05-19 11:39:02 +0200106};
107
Mattias Wallin47c16972010-09-10 17:47:56 +0200108static int ab8500_get_chip_id(struct device *dev)
109{
Mattias Wallin6bce7bf2010-12-02 15:08:32 +0100110 struct ab8500 *ab8500;
111
112 if (!dev)
113 return -EINVAL;
114 ab8500 = dev_get_drvdata(dev->parent);
115 return ab8500 ? (int)ab8500->chip_id : -EINVAL;
Mattias Wallin47c16972010-09-10 17:47:56 +0200116}
117
118static int set_register_interruptible(struct ab8500 *ab8500, u8 bank,
119 u8 reg, u8 data)
Rabin Vincent62579262010-05-19 11:39:02 +0200120{
121 int ret;
Mattias Wallin47c16972010-09-10 17:47:56 +0200122 /*
123 * Put the u8 bank and u8 register together into a an u16.
124 * The bank on higher 8 bits and register in lower 8 bits.
125 * */
126 u16 addr = ((u16)bank) << 8 | reg;
Rabin Vincent62579262010-05-19 11:39:02 +0200127
128 dev_vdbg(ab8500->dev, "wr: addr %#x <= %#x\n", addr, data);
129
Mattias Wallin47c16972010-09-10 17:47:56 +0200130 ret = mutex_lock_interruptible(&ab8500->lock);
131 if (ret)
132 return ret;
133
134 ret = ab8500->write(ab8500, addr, data);
135 if (ret < 0)
136 dev_err(ab8500->dev, "failed to write reg %#x: %d\n",
137 addr, ret);
138 mutex_unlock(&ab8500->lock);
139
140 return ret;
141}
142
143static int ab8500_set_register(struct device *dev, u8 bank,
144 u8 reg, u8 value)
145{
146 struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
147
148 return set_register_interruptible(ab8500, bank, reg, value);
149}
150
151static int get_register_interruptible(struct ab8500 *ab8500, u8 bank,
152 u8 reg, u8 *value)
153{
154 int ret;
155 /* put the u8 bank and u8 reg together into a an u16.
156 * bank on higher 8 bits and reg in lower */
157 u16 addr = ((u16)bank) << 8 | reg;
158
159 ret = mutex_lock_interruptible(&ab8500->lock);
160 if (ret)
161 return ret;
162
163 ret = ab8500->read(ab8500, addr);
164 if (ret < 0)
165 dev_err(ab8500->dev, "failed to read reg %#x: %d\n",
166 addr, ret);
167 else
168 *value = ret;
169
170 mutex_unlock(&ab8500->lock);
171 dev_vdbg(ab8500->dev, "rd: addr %#x => data %#x\n", addr, ret);
172
173 return ret;
174}
175
176static int ab8500_get_register(struct device *dev, u8 bank,
177 u8 reg, u8 *value)
178{
179 struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
180
181 return get_register_interruptible(ab8500, bank, reg, value);
182}
183
184static int mask_and_set_register_interruptible(struct ab8500 *ab8500, u8 bank,
185 u8 reg, u8 bitmask, u8 bitvalues)
186{
187 int ret;
188 u8 data;
189 /* put the u8 bank and u8 reg together into a an u16.
190 * bank on higher 8 bits and reg in lower */
191 u16 addr = ((u16)bank) << 8 | reg;
192
193 ret = mutex_lock_interruptible(&ab8500->lock);
194 if (ret)
195 return ret;
196
197 ret = ab8500->read(ab8500, addr);
198 if (ret < 0) {
199 dev_err(ab8500->dev, "failed to read reg %#x: %d\n",
200 addr, ret);
201 goto out;
202 }
203
204 data = (u8)ret;
205 data = (~bitmask & data) | (bitmask & bitvalues);
206
Rabin Vincent62579262010-05-19 11:39:02 +0200207 ret = ab8500->write(ab8500, addr, data);
208 if (ret < 0)
209 dev_err(ab8500->dev, "failed to write reg %#x: %d\n",
210 addr, ret);
211
Mattias Wallin47c16972010-09-10 17:47:56 +0200212 dev_vdbg(ab8500->dev, "mask: addr %#x => data %#x\n", addr, data);
Rabin Vincent62579262010-05-19 11:39:02 +0200213out:
214 mutex_unlock(&ab8500->lock);
215 return ret;
216}
Mattias Wallin47c16972010-09-10 17:47:56 +0200217
218static int ab8500_mask_and_set_register(struct device *dev,
219 u8 bank, u8 reg, u8 bitmask, u8 bitvalues)
220{
221 struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
222
223 return mask_and_set_register_interruptible(ab8500, bank, reg,
224 bitmask, bitvalues);
225
226}
227
228static struct abx500_ops ab8500_ops = {
229 .get_chip_id = ab8500_get_chip_id,
230 .get_register = ab8500_get_register,
231 .set_register = ab8500_set_register,
232 .get_register_page = NULL,
233 .set_register_page = NULL,
234 .mask_and_set_register = ab8500_mask_and_set_register,
235 .event_registers_startup_state_get = NULL,
236 .startup_irq_enabled = NULL,
237};
Rabin Vincent62579262010-05-19 11:39:02 +0200238
Mark Brown9505a0a2010-12-11 13:16:08 +0000239static void ab8500_irq_lock(struct irq_data *data)
Rabin Vincent62579262010-05-19 11:39:02 +0200240{
Mark Brown9505a0a2010-12-11 13:16:08 +0000241 struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
Rabin Vincent62579262010-05-19 11:39:02 +0200242
243 mutex_lock(&ab8500->irq_lock);
244}
245
Mark Brown9505a0a2010-12-11 13:16:08 +0000246static void ab8500_irq_sync_unlock(struct irq_data *data)
Rabin Vincent62579262010-05-19 11:39:02 +0200247{
Mark Brown9505a0a2010-12-11 13:16:08 +0000248 struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
Rabin Vincent62579262010-05-19 11:39:02 +0200249 int i;
250
251 for (i = 0; i < AB8500_NUM_IRQ_REGS; i++) {
252 u8 old = ab8500->oldmask[i];
253 u8 new = ab8500->mask[i];
254 int reg;
255
256 if (new == old)
257 continue;
258
Linus Walleij863dde52011-05-08 00:54:45 +0200259 /* Interrupt register 12 doesn't exist prior to version 2.0 */
260 if (ab8500_irq_regoffset[i] == 11 &&
261 ab8500->chip_id < AB8500_CUT2P0)
Mattias Wallin92d50a42010-12-07 11:20:47 +0100262 continue;
263
Rabin Vincent62579262010-05-19 11:39:02 +0200264 ab8500->oldmask[i] = new;
265
266 reg = AB8500_IT_MASK1_REG + ab8500_irq_regoffset[i];
Mattias Wallin47c16972010-09-10 17:47:56 +0200267 set_register_interruptible(ab8500, AB8500_INTERRUPT, reg, new);
Rabin Vincent62579262010-05-19 11:39:02 +0200268 }
269
270 mutex_unlock(&ab8500->irq_lock);
271}
272
Mark Brown9505a0a2010-12-11 13:16:08 +0000273static void ab8500_irq_mask(struct irq_data *data)
Rabin Vincent62579262010-05-19 11:39:02 +0200274{
Mark Brown9505a0a2010-12-11 13:16:08 +0000275 struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
276 int offset = data->irq - ab8500->irq_base;
Rabin Vincent62579262010-05-19 11:39:02 +0200277 int index = offset / 8;
278 int mask = 1 << (offset % 8);
279
280 ab8500->mask[index] |= mask;
281}
282
Mark Brown9505a0a2010-12-11 13:16:08 +0000283static void ab8500_irq_unmask(struct irq_data *data)
Rabin Vincent62579262010-05-19 11:39:02 +0200284{
Mark Brown9505a0a2010-12-11 13:16:08 +0000285 struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
286 int offset = data->irq - ab8500->irq_base;
Rabin Vincent62579262010-05-19 11:39:02 +0200287 int index = offset / 8;
288 int mask = 1 << (offset % 8);
289
290 ab8500->mask[index] &= ~mask;
291}
292
293static struct irq_chip ab8500_irq_chip = {
294 .name = "ab8500",
Mark Brown9505a0a2010-12-11 13:16:08 +0000295 .irq_bus_lock = ab8500_irq_lock,
296 .irq_bus_sync_unlock = ab8500_irq_sync_unlock,
297 .irq_mask = ab8500_irq_mask,
Virupax Sadashivpetimathe6f93062011-10-11 10:49:17 +0200298 .irq_disable = ab8500_irq_mask,
Mark Brown9505a0a2010-12-11 13:16:08 +0000299 .irq_unmask = ab8500_irq_unmask,
Rabin Vincent62579262010-05-19 11:39:02 +0200300};
301
302static irqreturn_t ab8500_irq(int irq, void *dev)
303{
304 struct ab8500 *ab8500 = dev;
305 int i;
306
307 dev_vdbg(ab8500->dev, "interrupt\n");
308
309 for (i = 0; i < AB8500_NUM_IRQ_REGS; i++) {
310 int regoffset = ab8500_irq_regoffset[i];
311 int status;
Mattias Wallin47c16972010-09-10 17:47:56 +0200312 u8 value;
Rabin Vincent62579262010-05-19 11:39:02 +0200313
Linus Walleij863dde52011-05-08 00:54:45 +0200314 /* Interrupt register 12 doesn't exist prior to version 2.0 */
315 if (regoffset == 11 && ab8500->chip_id < AB8500_CUT2P0)
Mattias Wallin92d50a42010-12-07 11:20:47 +0100316 continue;
317
Mattias Wallin47c16972010-09-10 17:47:56 +0200318 status = get_register_interruptible(ab8500, AB8500_INTERRUPT,
319 AB8500_IT_LATCH1_REG + regoffset, &value);
320 if (status < 0 || value == 0)
Rabin Vincent62579262010-05-19 11:39:02 +0200321 continue;
322
323 do {
Mattias Wallin88aec4f2010-12-02 15:06:49 +0100324 int bit = __ffs(value);
Rabin Vincent62579262010-05-19 11:39:02 +0200325 int line = i * 8 + bit;
326
327 handle_nested_irq(ab8500->irq_base + line);
Mattias Wallin47c16972010-09-10 17:47:56 +0200328 value &= ~(1 << bit);
329 } while (value);
Rabin Vincent62579262010-05-19 11:39:02 +0200330 }
331
332 return IRQ_HANDLED;
333}
334
335static int ab8500_irq_init(struct ab8500 *ab8500)
336{
337 int base = ab8500->irq_base;
338 int irq;
339
340 for (irq = base; irq < base + AB8500_NR_IRQS; irq++) {
Thomas Gleixnerd5bb1222011-03-25 11:12:32 +0000341 irq_set_chip_data(irq, ab8500);
342 irq_set_chip_and_handler(irq, &ab8500_irq_chip,
Rabin Vincent62579262010-05-19 11:39:02 +0200343 handle_simple_irq);
Thomas Gleixnerd5bb1222011-03-25 11:12:32 +0000344 irq_set_nested_thread(irq, 1);
Rabin Vincent62579262010-05-19 11:39:02 +0200345#ifdef CONFIG_ARM
346 set_irq_flags(irq, IRQF_VALID);
347#else
Thomas Gleixnerd5bb1222011-03-25 11:12:32 +0000348 irq_set_noprobe(irq);
Rabin Vincent62579262010-05-19 11:39:02 +0200349#endif
350 }
351
352 return 0;
353}
354
355static void ab8500_irq_remove(struct ab8500 *ab8500)
356{
357 int base = ab8500->irq_base;
358 int irq;
359
360 for (irq = base; irq < base + AB8500_NR_IRQS; irq++) {
361#ifdef CONFIG_ARM
362 set_irq_flags(irq, 0);
363#endif
Thomas Gleixnerd5bb1222011-03-25 11:12:32 +0000364 irq_set_chip_and_handler(irq, NULL, NULL);
365 irq_set_chip_data(irq, NULL);
Rabin Vincent62579262010-05-19 11:39:02 +0200366 }
367}
368
Robert Rosengren5cef8df2011-06-09 23:57:33 +0200369static struct resource __devinitdata ab8500_gpio_resources[] = {
Bibek Basu0cb3fcd2011-02-09 11:02:35 +0530370 {
371 .name = "GPIO_INT6",
372 .start = AB8500_INT_GPIO6R,
373 .end = AB8500_INT_GPIO41F,
374 .flags = IORESOURCE_IRQ,
375 }
376};
377
Robert Rosengren5cef8df2011-06-09 23:57:33 +0200378static struct resource __devinitdata ab8500_gpadc_resources[] = {
Rabin Vincent62579262010-05-19 11:39:02 +0200379 {
380 .name = "HW_CONV_END",
381 .start = AB8500_INT_GP_HW_ADC_CONV_END,
382 .end = AB8500_INT_GP_HW_ADC_CONV_END,
383 .flags = IORESOURCE_IRQ,
384 },
385 {
386 .name = "SW_CONV_END",
387 .start = AB8500_INT_GP_SW_ADC_CONV_END,
388 .end = AB8500_INT_GP_SW_ADC_CONV_END,
389 .flags = IORESOURCE_IRQ,
390 },
391};
392
Robert Rosengren5cef8df2011-06-09 23:57:33 +0200393static struct resource __devinitdata ab8500_rtc_resources[] = {
Rabin Vincent62579262010-05-19 11:39:02 +0200394 {
395 .name = "60S",
396 .start = AB8500_INT_RTC_60S,
397 .end = AB8500_INT_RTC_60S,
398 .flags = IORESOURCE_IRQ,
399 },
400 {
401 .name = "ALARM",
402 .start = AB8500_INT_RTC_ALARM,
403 .end = AB8500_INT_RTC_ALARM,
404 .flags = IORESOURCE_IRQ,
405 },
406};
407
Robert Rosengren5cef8df2011-06-09 23:57:33 +0200408static struct resource __devinitdata ab8500_poweronkey_db_resources[] = {
Sundar R Iyer77686512010-09-05 12:18:47 -0700409 {
410 .name = "ONKEY_DBF",
411 .start = AB8500_INT_PON_KEY1DB_F,
412 .end = AB8500_INT_PON_KEY1DB_F,
413 .flags = IORESOURCE_IRQ,
414 },
415 {
416 .name = "ONKEY_DBR",
417 .start = AB8500_INT_PON_KEY1DB_R,
418 .end = AB8500_INT_PON_KEY1DB_R,
419 .flags = IORESOURCE_IRQ,
420 },
421};
422
Linus Walleij6af75ec2011-06-09 23:57:45 +0200423static struct resource __devinitdata ab8500_av_acc_detect_resources[] = {
Mattias Walline098aded72010-12-02 15:40:31 +0100424 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200425 .name = "ACC_DETECT_1DB_F",
426 .start = AB8500_INT_ACC_DETECT_1DB_F,
427 .end = AB8500_INT_ACC_DETECT_1DB_F,
428 .flags = IORESOURCE_IRQ,
Mattias Walline098aded72010-12-02 15:40:31 +0100429 },
430 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200431 .name = "ACC_DETECT_1DB_R",
432 .start = AB8500_INT_ACC_DETECT_1DB_R,
433 .end = AB8500_INT_ACC_DETECT_1DB_R,
434 .flags = IORESOURCE_IRQ,
Mattias Walline098aded72010-12-02 15:40:31 +0100435 },
436 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200437 .name = "ACC_DETECT_21DB_F",
438 .start = AB8500_INT_ACC_DETECT_21DB_F,
439 .end = AB8500_INT_ACC_DETECT_21DB_F,
440 .flags = IORESOURCE_IRQ,
441 },
442 {
443 .name = "ACC_DETECT_21DB_R",
444 .start = AB8500_INT_ACC_DETECT_21DB_R,
445 .end = AB8500_INT_ACC_DETECT_21DB_R,
446 .flags = IORESOURCE_IRQ,
447 },
448 {
449 .name = "ACC_DETECT_22DB_F",
450 .start = AB8500_INT_ACC_DETECT_22DB_F,
451 .end = AB8500_INT_ACC_DETECT_22DB_F,
452 .flags = IORESOURCE_IRQ,
453 },
454 {
455 .name = "ACC_DETECT_22DB_R",
456 .start = AB8500_INT_ACC_DETECT_22DB_R,
457 .end = AB8500_INT_ACC_DETECT_22DB_R,
458 .flags = IORESOURCE_IRQ,
459 },
460};
461
462static struct resource __devinitdata ab8500_charger_resources[] = {
463 {
Mattias Walline098aded72010-12-02 15:40:31 +0100464 .name = "MAIN_CH_UNPLUG_DET",
465 .start = AB8500_INT_MAIN_CH_UNPLUG_DET,
466 .end = AB8500_INT_MAIN_CH_UNPLUG_DET,
467 .flags = IORESOURCE_IRQ,
468 },
469 {
470 .name = "MAIN_CHARGE_PLUG_DET",
471 .start = AB8500_INT_MAIN_CH_PLUG_DET,
472 .end = AB8500_INT_MAIN_CH_PLUG_DET,
473 .flags = IORESOURCE_IRQ,
474 },
475 {
Mattias Walline098aded72010-12-02 15:40:31 +0100476 .name = "VBUS_DET_R",
477 .start = AB8500_INT_VBUS_DET_R,
478 .end = AB8500_INT_VBUS_DET_R,
479 .flags = IORESOURCE_IRQ,
480 },
481 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200482 .name = "VBUS_DET_F",
483 .start = AB8500_INT_VBUS_DET_F,
484 .end = AB8500_INT_VBUS_DET_F,
Mattias Walline098aded72010-12-02 15:40:31 +0100485 .flags = IORESOURCE_IRQ,
486 },
487 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200488 .name = "USB_LINK_STATUS",
489 .start = AB8500_INT_USB_LINK_STATUS,
490 .end = AB8500_INT_USB_LINK_STATUS,
491 .flags = IORESOURCE_IRQ,
492 },
493 {
494 .name = "USB_CHARGE_DET_DONE",
495 .start = AB8500_INT_USB_CHG_DET_DONE,
496 .end = AB8500_INT_USB_CHG_DET_DONE,
Mattias Walline098aded72010-12-02 15:40:31 +0100497 .flags = IORESOURCE_IRQ,
498 },
499 {
500 .name = "VBUS_OVV",
501 .start = AB8500_INT_VBUS_OVV,
502 .end = AB8500_INT_VBUS_OVV,
503 .flags = IORESOURCE_IRQ,
504 },
505 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200506 .name = "USB_CH_TH_PROT_R",
507 .start = AB8500_INT_USB_CH_TH_PROT_R,
508 .end = AB8500_INT_USB_CH_TH_PROT_R,
Mattias Walline098aded72010-12-02 15:40:31 +0100509 .flags = IORESOURCE_IRQ,
510 },
511 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200512 .name = "USB_CH_TH_PROT_F",
513 .start = AB8500_INT_USB_CH_TH_PROT_F,
514 .end = AB8500_INT_USB_CH_TH_PROT_F,
Mattias Walline098aded72010-12-02 15:40:31 +0100515 .flags = IORESOURCE_IRQ,
516 },
517 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200518 .name = "MAIN_EXT_CH_NOT_OK",
519 .start = AB8500_INT_MAIN_EXT_CH_NOT_OK,
520 .end = AB8500_INT_MAIN_EXT_CH_NOT_OK,
521 .flags = IORESOURCE_IRQ,
522 },
523 {
524 .name = "MAIN_CH_TH_PROT_R",
525 .start = AB8500_INT_MAIN_CH_TH_PROT_R,
526 .end = AB8500_INT_MAIN_CH_TH_PROT_R,
527 .flags = IORESOURCE_IRQ,
528 },
529 {
530 .name = "MAIN_CH_TH_PROT_F",
531 .start = AB8500_INT_MAIN_CH_TH_PROT_F,
532 .end = AB8500_INT_MAIN_CH_TH_PROT_F,
533 .flags = IORESOURCE_IRQ,
534 },
535 {
536 .name = "USB_CHARGER_NOT_OKR",
537 .start = AB8500_INT_USB_CHARGER_NOT_OK,
538 .end = AB8500_INT_USB_CHARGER_NOT_OK,
539 .flags = IORESOURCE_IRQ,
540 },
541 {
542 .name = "USB_CHARGER_NOT_OKF",
543 .start = AB8500_INT_USB_CHARGER_NOT_OKF,
544 .end = AB8500_INT_USB_CHARGER_NOT_OKF,
545 .flags = IORESOURCE_IRQ,
546 },
547 {
548 .name = "CH_WD_EXP",
549 .start = AB8500_INT_CH_WD_EXP,
550 .end = AB8500_INT_CH_WD_EXP,
551 .flags = IORESOURCE_IRQ,
552 },
553};
554
555static struct resource __devinitdata ab8500_btemp_resources[] = {
556 {
557 .name = "BAT_CTRL_INDB",
558 .start = AB8500_INT_BAT_CTRL_INDB,
559 .end = AB8500_INT_BAT_CTRL_INDB,
Mattias Walline098aded72010-12-02 15:40:31 +0100560 .flags = IORESOURCE_IRQ,
561 },
562 {
563 .name = "BTEMP_LOW",
564 .start = AB8500_INT_BTEMP_LOW,
565 .end = AB8500_INT_BTEMP_LOW,
566 .flags = IORESOURCE_IRQ,
567 },
568 {
569 .name = "BTEMP_HIGH",
570 .start = AB8500_INT_BTEMP_HIGH,
571 .end = AB8500_INT_BTEMP_HIGH,
572 .flags = IORESOURCE_IRQ,
573 },
574 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200575 .name = "BTEMP_LOW_MEDIUM",
576 .start = AB8500_INT_BTEMP_LOW_MEDIUM,
577 .end = AB8500_INT_BTEMP_LOW_MEDIUM,
Mattias Walline098aded72010-12-02 15:40:31 +0100578 .flags = IORESOURCE_IRQ,
579 },
580 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200581 .name = "BTEMP_MEDIUM_HIGH",
582 .start = AB8500_INT_BTEMP_MEDIUM_HIGH,
583 .end = AB8500_INT_BTEMP_MEDIUM_HIGH,
Mattias Walline098aded72010-12-02 15:40:31 +0100584 .flags = IORESOURCE_IRQ,
585 },
586};
587
Linus Walleij6af75ec2011-06-09 23:57:45 +0200588static struct resource __devinitdata ab8500_fg_resources[] = {
589 {
590 .name = "NCONV_ACCU",
591 .start = AB8500_INT_CCN_CONV_ACC,
592 .end = AB8500_INT_CCN_CONV_ACC,
593 .flags = IORESOURCE_IRQ,
594 },
595 {
596 .name = "BATT_OVV",
597 .start = AB8500_INT_BATT_OVV,
598 .end = AB8500_INT_BATT_OVV,
599 .flags = IORESOURCE_IRQ,
600 },
601 {
602 .name = "LOW_BAT_F",
603 .start = AB8500_INT_LOW_BAT_F,
604 .end = AB8500_INT_LOW_BAT_F,
605 .flags = IORESOURCE_IRQ,
606 },
607 {
608 .name = "LOW_BAT_R",
609 .start = AB8500_INT_LOW_BAT_R,
610 .end = AB8500_INT_LOW_BAT_R,
611 .flags = IORESOURCE_IRQ,
612 },
613 {
614 .name = "CC_INT_CALIB",
615 .start = AB8500_INT_CC_INT_CALIB,
616 .end = AB8500_INT_CC_INT_CALIB,
617 .flags = IORESOURCE_IRQ,
618 },
619};
620
621static struct resource __devinitdata ab8500_chargalg_resources[] = {};
622
Robert Rosengren5cef8df2011-06-09 23:57:33 +0200623static struct resource __devinitdata ab8500_debug_resources[] = {
Mattias Walline098aded72010-12-02 15:40:31 +0100624 {
625 .name = "IRQ_FIRST",
626 .start = AB8500_INT_MAIN_EXT_CH_NOT_OK,
627 .end = AB8500_INT_MAIN_EXT_CH_NOT_OK,
628 .flags = IORESOURCE_IRQ,
629 },
630 {
631 .name = "IRQ_LAST",
632 .start = AB8500_INT_USB_CHARGER_NOT_OKF,
633 .end = AB8500_INT_USB_CHARGER_NOT_OKF,
634 .flags = IORESOURCE_IRQ,
635 },
636};
637
Robert Rosengren5cef8df2011-06-09 23:57:33 +0200638static struct resource __devinitdata ab8500_usb_resources[] = {
Mattias Walline098aded72010-12-02 15:40:31 +0100639 {
640 .name = "ID_WAKEUP_R",
641 .start = AB8500_INT_ID_WAKEUP_R,
642 .end = AB8500_INT_ID_WAKEUP_R,
643 .flags = IORESOURCE_IRQ,
644 },
645 {
646 .name = "ID_WAKEUP_F",
647 .start = AB8500_INT_ID_WAKEUP_F,
648 .end = AB8500_INT_ID_WAKEUP_F,
649 .flags = IORESOURCE_IRQ,
650 },
651 {
652 .name = "VBUS_DET_F",
653 .start = AB8500_INT_VBUS_DET_F,
654 .end = AB8500_INT_VBUS_DET_F,
655 .flags = IORESOURCE_IRQ,
656 },
657 {
658 .name = "VBUS_DET_R",
659 .start = AB8500_INT_VBUS_DET_R,
660 .end = AB8500_INT_VBUS_DET_R,
661 .flags = IORESOURCE_IRQ,
662 },
Mattias Wallin92d50a42010-12-07 11:20:47 +0100663 {
664 .name = "USB_LINK_STATUS",
665 .start = AB8500_INT_USB_LINK_STATUS,
666 .end = AB8500_INT_USB_LINK_STATUS,
667 .flags = IORESOURCE_IRQ,
668 },
Linus Walleij6af75ec2011-06-09 23:57:45 +0200669 {
670 .name = "USB_ADP_PROBE_PLUG",
671 .start = AB8500_INT_ADP_PROBE_PLUG,
672 .end = AB8500_INT_ADP_PROBE_PLUG,
673 .flags = IORESOURCE_IRQ,
674 },
675 {
676 .name = "USB_ADP_PROBE_UNPLUG",
677 .start = AB8500_INT_ADP_PROBE_UNPLUG,
678 .end = AB8500_INT_ADP_PROBE_UNPLUG,
679 .flags = IORESOURCE_IRQ,
680 },
Mattias Walline098aded72010-12-02 15:40:31 +0100681};
682
Robert Rosengren5cef8df2011-06-09 23:57:33 +0200683static struct resource __devinitdata ab8500_temp_resources[] = {
Mattias Walline098aded72010-12-02 15:40:31 +0100684 {
685 .name = "AB8500_TEMP_WARM",
686 .start = AB8500_INT_TEMP_WARM,
687 .end = AB8500_INT_TEMP_WARM,
688 .flags = IORESOURCE_IRQ,
689 },
690};
691
Robert Rosengren5cef8df2011-06-09 23:57:33 +0200692static struct mfd_cell __devinitdata ab8500_devs[] = {
Mattias Wallin5814fc32010-09-13 16:05:04 +0200693#ifdef CONFIG_DEBUG_FS
694 {
695 .name = "ab8500-debug",
Mattias Walline098aded72010-12-02 15:40:31 +0100696 .num_resources = ARRAY_SIZE(ab8500_debug_resources),
697 .resources = ab8500_debug_resources,
Mattias Wallin5814fc32010-09-13 16:05:04 +0200698 },
699#endif
Rabin Vincent62579262010-05-19 11:39:02 +0200700 {
Mattias Walline098aded72010-12-02 15:40:31 +0100701 .name = "ab8500-sysctrl",
702 },
703 {
704 .name = "ab8500-regulator",
705 },
706 {
Bibek Basu0cb3fcd2011-02-09 11:02:35 +0530707 .name = "ab8500-gpio",
708 .num_resources = ARRAY_SIZE(ab8500_gpio_resources),
709 .resources = ab8500_gpio_resources,
710 },
711 {
Rabin Vincent62579262010-05-19 11:39:02 +0200712 .name = "ab8500-gpadc",
713 .num_resources = ARRAY_SIZE(ab8500_gpadc_resources),
714 .resources = ab8500_gpadc_resources,
715 },
716 {
717 .name = "ab8500-rtc",
718 .num_resources = ARRAY_SIZE(ab8500_rtc_resources),
719 .resources = ab8500_rtc_resources,
720 },
Arun Murthyf0f05b12010-09-06 12:24:52 +0530721 {
Linus Walleij6af75ec2011-06-09 23:57:45 +0200722 .name = "ab8500-charger",
723 .num_resources = ARRAY_SIZE(ab8500_charger_resources),
724 .resources = ab8500_charger_resources,
Mattias Walline098aded72010-12-02 15:40:31 +0100725 },
Linus Walleij6af75ec2011-06-09 23:57:45 +0200726 {
727 .name = "ab8500-btemp",
728 .num_resources = ARRAY_SIZE(ab8500_btemp_resources),
729 .resources = ab8500_btemp_resources,
730 },
731 {
732 .name = "ab8500-fg",
733 .num_resources = ARRAY_SIZE(ab8500_fg_resources),
734 .resources = ab8500_fg_resources,
735 },
736 {
737 .name = "ab8500-chargalg",
738 .num_resources = ARRAY_SIZE(ab8500_chargalg_resources),
739 .resources = ab8500_chargalg_resources,
740 },
741 {
742 .name = "ab8500-acc-det",
743 .num_resources = ARRAY_SIZE(ab8500_av_acc_detect_resources),
744 .resources = ab8500_av_acc_detect_resources,
745 },
746 {
747 .name = "ab8500-codec",
748 },
Mattias Walline098aded72010-12-02 15:40:31 +0100749 {
750 .name = "ab8500-usb",
751 .num_resources = ARRAY_SIZE(ab8500_usb_resources),
752 .resources = ab8500_usb_resources,
753 },
754 {
755 .name = "ab8500-poweron-key",
756 .num_resources = ARRAY_SIZE(ab8500_poweronkey_db_resources),
757 .resources = ab8500_poweronkey_db_resources,
758 },
759 {
Arun Murthyf0f05b12010-09-06 12:24:52 +0530760 .name = "ab8500-pwm",
761 .id = 1,
762 },
763 {
764 .name = "ab8500-pwm",
765 .id = 2,
766 },
767 {
768 .name = "ab8500-pwm",
769 .id = 3,
770 },
Mattias Walline098aded72010-12-02 15:40:31 +0100771 { .name = "ab8500-leds", },
Sundar R Iyer77686512010-09-05 12:18:47 -0700772 {
Mattias Walline098aded72010-12-02 15:40:31 +0100773 .name = "ab8500-denc",
774 },
775 {
776 .name = "ab8500-temp",
777 .num_resources = ARRAY_SIZE(ab8500_temp_resources),
778 .resources = ab8500_temp_resources,
Sundar R Iyer77686512010-09-05 12:18:47 -0700779 },
Rabin Vincent62579262010-05-19 11:39:02 +0200780};
781
Mattias Wallincca69b62010-12-02 15:09:36 +0100782static ssize_t show_chip_id(struct device *dev,
783 struct device_attribute *attr, char *buf)
784{
785 struct ab8500 *ab8500;
786
787 ab8500 = dev_get_drvdata(dev);
788 return sprintf(buf, "%#x\n", ab8500 ? ab8500->chip_id : -EINVAL);
789}
790
Mattias Walline5c238c2011-03-02 11:52:36 +0100791/*
792 * ab8500 has switched off due to (SWITCH_OFF_STATUS):
793 * 0x01 Swoff bit programming
794 * 0x02 Thermal protection activation
795 * 0x04 Vbat lower then BattOk falling threshold
796 * 0x08 Watchdog expired
797 * 0x10 Non presence of 32kHz clock
798 * 0x20 Battery level lower than power on reset threshold
799 * 0x40 Power on key 1 pressed longer than 10 seconds
800 * 0x80 DB8500 thermal shutdown
801 */
802static ssize_t show_switch_off_status(struct device *dev,
803 struct device_attribute *attr, char *buf)
804{
805 int ret;
806 u8 value;
807 struct ab8500 *ab8500;
808
809 ab8500 = dev_get_drvdata(dev);
810 ret = get_register_interruptible(ab8500, AB8500_RTC,
811 AB8500_SWITCH_OFF_STATUS, &value);
812 if (ret < 0)
813 return ret;
814 return sprintf(buf, "%#x\n", value);
815}
816
Andrew Lynnb4a31032011-10-11 10:49:47 +0200817/*
818 * ab8500 has turned on due to (TURN_ON_STATUS):
819 * 0x01 PORnVbat
820 * 0x02 PonKey1dbF
821 * 0x04 PonKey2dbF
822 * 0x08 RTCAlarm
823 * 0x10 MainChDet
824 * 0x20 VbusDet
825 * 0x40 UsbIDDetect
826 * 0x80 Reserved
827 */
828static ssize_t show_turn_on_status(struct device *dev,
829 struct device_attribute *attr, char *buf)
830{
831 int ret;
832 u8 value;
833 struct ab8500 *ab8500;
834
835 ab8500 = dev_get_drvdata(dev);
836 ret = get_register_interruptible(ab8500, AB8500_SYS_CTRL1_BLOCK,
837 AB8500_TURN_ON_STATUS, &value);
838 if (ret < 0)
839 return ret;
840 return sprintf(buf, "%#x\n", value);
841}
842
Mattias Wallincca69b62010-12-02 15:09:36 +0100843static DEVICE_ATTR(chip_id, S_IRUGO, show_chip_id, NULL);
Mattias Walline5c238c2011-03-02 11:52:36 +0100844static DEVICE_ATTR(switch_off_status, S_IRUGO, show_switch_off_status, NULL);
Andrew Lynnb4a31032011-10-11 10:49:47 +0200845static DEVICE_ATTR(turn_on_status, S_IRUGO, show_turn_on_status, NULL);
Mattias Wallincca69b62010-12-02 15:09:36 +0100846
847static struct attribute *ab8500_sysfs_entries[] = {
848 &dev_attr_chip_id.attr,
Mattias Walline5c238c2011-03-02 11:52:36 +0100849 &dev_attr_switch_off_status.attr,
Andrew Lynnb4a31032011-10-11 10:49:47 +0200850 &dev_attr_turn_on_status.attr,
Mattias Wallincca69b62010-12-02 15:09:36 +0100851 NULL,
852};
853
854static struct attribute_group ab8500_attr_group = {
855 .attrs = ab8500_sysfs_entries,
856};
857
Rabin Vincent62579262010-05-19 11:39:02 +0200858int __devinit ab8500_init(struct ab8500 *ab8500)
859{
860 struct ab8500_platform_data *plat = dev_get_platdata(ab8500->dev);
861 int ret;
862 int i;
Mattias Wallin47c16972010-09-10 17:47:56 +0200863 u8 value;
Rabin Vincent62579262010-05-19 11:39:02 +0200864
865 if (plat)
866 ab8500->irq_base = plat->irq_base;
867
868 mutex_init(&ab8500->lock);
869 mutex_init(&ab8500->irq_lock);
870
Mattias Wallin47c16972010-09-10 17:47:56 +0200871 ret = get_register_interruptible(ab8500, AB8500_MISC,
872 AB8500_REV_REG, &value);
Rabin Vincent62579262010-05-19 11:39:02 +0200873 if (ret < 0)
874 return ret;
875
Linus Walleij863dde52011-05-08 00:54:45 +0200876 switch (value) {
Linus Walleij863dde52011-05-08 00:54:45 +0200877 case AB8500_CUT1P0:
878 case AB8500_CUT1P1:
879 case AB8500_CUT2P0:
880 case AB8500_CUT3P0:
Mattias Wallin0e9049e2011-10-11 10:49:32 +0200881 case AB8500_CUT3P3:
Mattias Wallin47c16972010-09-10 17:47:56 +0200882 dev_info(ab8500->dev, "detected chip, revision: %#x\n", value);
Linus Walleij863dde52011-05-08 00:54:45 +0200883 break;
884 default:
Mattias Wallin47c16972010-09-10 17:47:56 +0200885 dev_err(ab8500->dev, "unknown chip, revision: %#x\n", value);
Rabin Vincent62579262010-05-19 11:39:02 +0200886 return -EINVAL;
887 }
Mattias Wallin47c16972010-09-10 17:47:56 +0200888 ab8500->chip_id = value;
Rabin Vincent62579262010-05-19 11:39:02 +0200889
Mattias Walline5c238c2011-03-02 11:52:36 +0100890 /*
891 * ab8500 has switched off due to (SWITCH_OFF_STATUS):
892 * 0x01 Swoff bit programming
893 * 0x02 Thermal protection activation
894 * 0x04 Vbat lower then BattOk falling threshold
895 * 0x08 Watchdog expired
896 * 0x10 Non presence of 32kHz clock
897 * 0x20 Battery level lower than power on reset threshold
898 * 0x40 Power on key 1 pressed longer than 10 seconds
899 * 0x80 DB8500 thermal shutdown
900 */
901
902 ret = get_register_interruptible(ab8500, AB8500_RTC,
903 AB8500_SWITCH_OFF_STATUS, &value);
904 if (ret < 0)
905 return ret;
906 dev_info(ab8500->dev, "switch off status: %#x", value);
907
Rabin Vincent62579262010-05-19 11:39:02 +0200908 if (plat && plat->init)
909 plat->init(ab8500);
910
911 /* Clear and mask all interrupts */
Mattias Wallin92d50a42010-12-07 11:20:47 +0100912 for (i = 0; i < AB8500_NUM_IRQ_REGS; i++) {
Linus Walleij863dde52011-05-08 00:54:45 +0200913 /* Interrupt register 12 doesn't exist prior to version 2.0 */
914 if (ab8500_irq_regoffset[i] == 11 &&
915 ab8500->chip_id < AB8500_CUT2P0)
Mattias Wallin92d50a42010-12-07 11:20:47 +0100916 continue;
Rabin Vincent62579262010-05-19 11:39:02 +0200917
Mattias Wallin47c16972010-09-10 17:47:56 +0200918 get_register_interruptible(ab8500, AB8500_INTERRUPT,
Mattias Wallin92d50a42010-12-07 11:20:47 +0100919 AB8500_IT_LATCH1_REG + ab8500_irq_regoffset[i],
920 &value);
Mattias Wallin47c16972010-09-10 17:47:56 +0200921 set_register_interruptible(ab8500, AB8500_INTERRUPT,
Mattias Wallin92d50a42010-12-07 11:20:47 +0100922 AB8500_IT_MASK1_REG + ab8500_irq_regoffset[i], 0xff);
Rabin Vincent62579262010-05-19 11:39:02 +0200923 }
924
Mattias Wallin47c16972010-09-10 17:47:56 +0200925 ret = abx500_register_ops(ab8500->dev, &ab8500_ops);
926 if (ret)
927 return ret;
928
Rabin Vincent62579262010-05-19 11:39:02 +0200929 for (i = 0; i < AB8500_NUM_IRQ_REGS; i++)
930 ab8500->mask[i] = ab8500->oldmask[i] = 0xff;
931
932 if (ab8500->irq_base) {
933 ret = ab8500_irq_init(ab8500);
934 if (ret)
935 return ret;
936
937 ret = request_threaded_irq(ab8500->irq, NULL, ab8500_irq,
Mattias Wallin4f079982010-12-02 15:10:27 +0100938 IRQF_ONESHOT | IRQF_NO_SUSPEND,
939 "ab8500", ab8500);
Rabin Vincent62579262010-05-19 11:39:02 +0200940 if (ret)
941 goto out_removeirq;
942 }
943
Sundar R Iyer549931f2010-07-13 11:51:28 +0530944 ret = mfd_add_devices(ab8500->dev, 0, ab8500_devs,
Rabin Vincent62579262010-05-19 11:39:02 +0200945 ARRAY_SIZE(ab8500_devs), NULL,
946 ab8500->irq_base);
947 if (ret)
948 goto out_freeirq;
949
Mattias Wallincca69b62010-12-02 15:09:36 +0100950 ret = sysfs_create_group(&ab8500->dev->kobj, &ab8500_attr_group);
951 if (ret)
952 dev_err(ab8500->dev, "error creating sysfs entries\n");
953
Rabin Vincent62579262010-05-19 11:39:02 +0200954 return ret;
955
956out_freeirq:
957 if (ab8500->irq_base) {
958 free_irq(ab8500->irq, ab8500);
959out_removeirq:
960 ab8500_irq_remove(ab8500);
961 }
962 return ret;
963}
964
965int __devexit ab8500_exit(struct ab8500 *ab8500)
966{
Mattias Wallincca69b62010-12-02 15:09:36 +0100967 sysfs_remove_group(&ab8500->dev->kobj, &ab8500_attr_group);
Rabin Vincent62579262010-05-19 11:39:02 +0200968 mfd_remove_devices(ab8500->dev);
969 if (ab8500->irq_base) {
970 free_irq(ab8500->irq, ab8500);
971 ab8500_irq_remove(ab8500);
972 }
973
974 return 0;
975}
976
Mattias Wallinadceed62011-03-02 11:51:11 +0100977MODULE_AUTHOR("Mattias Wallin, Srinidhi Kasagar, Rabin Vincent");
Rabin Vincent62579262010-05-19 11:39:02 +0200978MODULE_DESCRIPTION("AB8500 MFD core");
979MODULE_LICENSE("GPL v2");