2 * Copyright (C) ST-Ericsson SA 2010
4 * License Terms: GNU General Public License v2
5 * Author: Srinidhi Kasagar <srinidhi.kasagar@stericsson.com>
6 * Author: Rabin Vincent <rabin.vincent@stericsson.com>
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/init.h>
12 #include <linux/irq.h>
13 #include <linux/delay.h>
14 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/mfd/core.h>
18 #include <linux/mfd/ab8500.h>
19 #include <linux/regulator/ab8500.h>
22 * Interrupt register offsets
25 #define AB8500_IT_SOURCE1_REG 0x0E00
26 #define AB8500_IT_SOURCE2_REG 0x0E01
27 #define AB8500_IT_SOURCE3_REG 0x0E02
28 #define AB8500_IT_SOURCE4_REG 0x0E03
29 #define AB8500_IT_SOURCE5_REG 0x0E04
30 #define AB8500_IT_SOURCE6_REG 0x0E05
31 #define AB8500_IT_SOURCE7_REG 0x0E06
32 #define AB8500_IT_SOURCE8_REG 0x0E07
33 #define AB8500_IT_SOURCE19_REG 0x0E12
34 #define AB8500_IT_SOURCE20_REG 0x0E13
35 #define AB8500_IT_SOURCE21_REG 0x0E14
36 #define AB8500_IT_SOURCE22_REG 0x0E15
37 #define AB8500_IT_SOURCE23_REG 0x0E16
38 #define AB8500_IT_SOURCE24_REG 0x0E17
43 #define AB8500_IT_LATCH1_REG 0x0E20
44 #define AB8500_IT_LATCH2_REG 0x0E21
45 #define AB8500_IT_LATCH3_REG 0x0E22
46 #define AB8500_IT_LATCH4_REG 0x0E23
47 #define AB8500_IT_LATCH5_REG 0x0E24
48 #define AB8500_IT_LATCH6_REG 0x0E25
49 #define AB8500_IT_LATCH7_REG 0x0E26
50 #define AB8500_IT_LATCH8_REG 0x0E27
51 #define AB8500_IT_LATCH9_REG 0x0E28
52 #define AB8500_IT_LATCH10_REG 0x0E29
53 #define AB8500_IT_LATCH19_REG 0x0E32
54 #define AB8500_IT_LATCH20_REG 0x0E33
55 #define AB8500_IT_LATCH21_REG 0x0E34
56 #define AB8500_IT_LATCH22_REG 0x0E35
57 #define AB8500_IT_LATCH23_REG 0x0E36
58 #define AB8500_IT_LATCH24_REG 0x0E37
64 #define AB8500_IT_MASK1_REG 0x0E40
65 #define AB8500_IT_MASK2_REG 0x0E41
66 #define AB8500_IT_MASK3_REG 0x0E42
67 #define AB8500_IT_MASK4_REG 0x0E43
68 #define AB8500_IT_MASK5_REG 0x0E44
69 #define AB8500_IT_MASK6_REG 0x0E45
70 #define AB8500_IT_MASK7_REG 0x0E46
71 #define AB8500_IT_MASK8_REG 0x0E47
72 #define AB8500_IT_MASK9_REG 0x0E48
73 #define AB8500_IT_MASK10_REG 0x0E49
74 #define AB8500_IT_MASK11_REG 0x0E4A
75 #define AB8500_IT_MASK12_REG 0x0E4B
76 #define AB8500_IT_MASK13_REG 0x0E4C
77 #define AB8500_IT_MASK14_REG 0x0E4D
78 #define AB8500_IT_MASK15_REG 0x0E4E
79 #define AB8500_IT_MASK16_REG 0x0E4F
80 #define AB8500_IT_MASK17_REG 0x0E50
81 #define AB8500_IT_MASK18_REG 0x0E51
82 #define AB8500_IT_MASK19_REG 0x0E52
83 #define AB8500_IT_MASK20_REG 0x0E53
84 #define AB8500_IT_MASK21_REG 0x0E54
85 #define AB8500_IT_MASK22_REG 0x0E55
86 #define AB8500_IT_MASK23_REG 0x0E56
87 #define AB8500_IT_MASK24_REG 0x0E57
89 #define AB8500_REV_REG 0x1080
92 * Map interrupt numbers to the LATCH and MASK register offsets, Interrupt
93 * numbers are indexed into this array with (num / 8).
95 * This is one off from the register names, i.e. AB8500_IT_MASK1_REG is at
98 static const int ab8500_irq_regoffset
[AB8500_NUM_IRQ_REGS
] = {
99 0, 1, 2, 3, 4, 6, 7, 8, 9, 18, 19, 20, 21,
102 static int __ab8500_write(struct ab8500
*ab8500
, u16 addr
, u8 data
)
106 dev_vdbg(ab8500
->dev
, "wr: addr %#x <= %#x\n", addr
, data
);
108 ret
= ab8500
->write(ab8500
, addr
, data
);
110 dev_err(ab8500
->dev
, "failed to write reg %#x: %d\n",
117 * ab8500_write() - write an AB8500 register
118 * @ab8500: device to write to
119 * @addr: address of the register
120 * @data: value to write
122 int ab8500_write(struct ab8500
*ab8500
, u16 addr
, u8 data
)
126 mutex_lock(&ab8500
->lock
);
127 ret
= __ab8500_write(ab8500
, addr
, data
);
128 mutex_unlock(&ab8500
->lock
);
132 EXPORT_SYMBOL_GPL(ab8500_write
);
134 static int __ab8500_read(struct ab8500
*ab8500
, u16 addr
)
138 ret
= ab8500
->read(ab8500
, addr
);
140 dev_err(ab8500
->dev
, "failed to read reg %#x: %d\n",
143 dev_vdbg(ab8500
->dev
, "rd: addr %#x => data %#x\n", addr
, ret
);
149 * ab8500_read() - read an AB8500 register
150 * @ab8500: device to read from
151 * @addr: address of the register
153 int ab8500_read(struct ab8500
*ab8500
, u16 addr
)
157 mutex_lock(&ab8500
->lock
);
158 ret
= __ab8500_read(ab8500
, addr
);
159 mutex_unlock(&ab8500
->lock
);
163 EXPORT_SYMBOL_GPL(ab8500_read
);
166 * ab8500_set_bits() - set a bitfield in an AB8500 register
167 * @ab8500: device to read from
168 * @addr: address of the register
169 * @mask: mask of the bitfield to modify
170 * @data: value to set to the bitfield
172 int ab8500_set_bits(struct ab8500
*ab8500
, u16 addr
, u8 mask
, u8 data
)
176 mutex_lock(&ab8500
->lock
);
178 ret
= __ab8500_read(ab8500
, addr
);
185 ret
= __ab8500_write(ab8500
, addr
, ret
);
188 mutex_unlock(&ab8500
->lock
);
191 EXPORT_SYMBOL_GPL(ab8500_set_bits
);
193 static void ab8500_irq_lock(unsigned int irq
)
195 struct ab8500
*ab8500
= get_irq_chip_data(irq
);
197 mutex_lock(&ab8500
->irq_lock
);
200 static void ab8500_irq_sync_unlock(unsigned int irq
)
202 struct ab8500
*ab8500
= get_irq_chip_data(irq
);
205 for (i
= 0; i
< AB8500_NUM_IRQ_REGS
; i
++) {
206 u8 old
= ab8500
->oldmask
[i
];
207 u8
new = ab8500
->mask
[i
];
213 ab8500
->oldmask
[i
] = new;
215 reg
= AB8500_IT_MASK1_REG
+ ab8500_irq_regoffset
[i
];
216 ab8500_write(ab8500
, reg
, new);
219 mutex_unlock(&ab8500
->irq_lock
);
222 static void ab8500_irq_mask(unsigned int irq
)
224 struct ab8500
*ab8500
= get_irq_chip_data(irq
);
225 int offset
= irq
- ab8500
->irq_base
;
226 int index
= offset
/ 8;
227 int mask
= 1 << (offset
% 8);
229 ab8500
->mask
[index
] |= mask
;
232 static void ab8500_irq_unmask(unsigned int irq
)
234 struct ab8500
*ab8500
= get_irq_chip_data(irq
);
235 int offset
= irq
- ab8500
->irq_base
;
236 int index
= offset
/ 8;
237 int mask
= 1 << (offset
% 8);
239 ab8500
->mask
[index
] &= ~mask
;
242 static struct irq_chip ab8500_irq_chip
= {
244 .bus_lock
= ab8500_irq_lock
,
245 .bus_sync_unlock
= ab8500_irq_sync_unlock
,
246 .mask
= ab8500_irq_mask
,
247 .unmask
= ab8500_irq_unmask
,
250 static irqreturn_t
ab8500_irq(int irq
, void *dev
)
252 struct ab8500
*ab8500
= dev
;
255 dev_vdbg(ab8500
->dev
, "interrupt\n");
257 for (i
= 0; i
< AB8500_NUM_IRQ_REGS
; i
++) {
258 int regoffset
= ab8500_irq_regoffset
[i
];
261 status
= ab8500_read(ab8500
, AB8500_IT_LATCH1_REG
+ regoffset
);
266 int bit
= __ffs(status
);
267 int line
= i
* 8 + bit
;
269 handle_nested_irq(ab8500
->irq_base
+ line
);
270 status
&= ~(1 << bit
);
277 static int ab8500_irq_init(struct ab8500
*ab8500
)
279 int base
= ab8500
->irq_base
;
282 for (irq
= base
; irq
< base
+ AB8500_NR_IRQS
; irq
++) {
283 set_irq_chip_data(irq
, ab8500
);
284 set_irq_chip_and_handler(irq
, &ab8500_irq_chip
,
286 set_irq_nested_thread(irq
, 1);
288 set_irq_flags(irq
, IRQF_VALID
);
290 set_irq_noprobe(irq
);
297 static void ab8500_irq_remove(struct ab8500
*ab8500
)
299 int base
= ab8500
->irq_base
;
302 for (irq
= base
; irq
< base
+ AB8500_NR_IRQS
; irq
++) {
304 set_irq_flags(irq
, 0);
306 set_irq_chip_and_handler(irq
, NULL
, NULL
);
307 set_irq_chip_data(irq
, NULL
);
311 static struct resource ab8500_gpadc_resources
[] = {
313 .name
= "HW_CONV_END",
314 .start
= AB8500_INT_GP_HW_ADC_CONV_END
,
315 .end
= AB8500_INT_GP_HW_ADC_CONV_END
,
316 .flags
= IORESOURCE_IRQ
,
319 .name
= "SW_CONV_END",
320 .start
= AB8500_INT_GP_SW_ADC_CONV_END
,
321 .end
= AB8500_INT_GP_SW_ADC_CONV_END
,
322 .flags
= IORESOURCE_IRQ
,
326 static struct resource ab8500_rtc_resources
[] = {
329 .start
= AB8500_INT_RTC_60S
,
330 .end
= AB8500_INT_RTC_60S
,
331 .flags
= IORESOURCE_IRQ
,
335 .start
= AB8500_INT_RTC_ALARM
,
336 .end
= AB8500_INT_RTC_ALARM
,
337 .flags
= IORESOURCE_IRQ
,
341 static struct mfd_cell ab8500_devs
[] = {
343 .name
= "ab8500-gpadc",
344 .num_resources
= ARRAY_SIZE(ab8500_gpadc_resources
),
345 .resources
= ab8500_gpadc_resources
,
348 .name
= "ab8500-rtc",
349 .num_resources
= ARRAY_SIZE(ab8500_rtc_resources
),
350 .resources
= ab8500_rtc_resources
,
352 { .name
= "ab8500-charger", },
353 { .name
= "ab8500-audio", },
354 { .name
= "ab8500-usb", },
355 { .name
= "ab8500-pwm", },
356 { .name
= "ab8500-regulator", },
359 int __devinit
ab8500_init(struct ab8500
*ab8500
)
361 struct ab8500_platform_data
*plat
= dev_get_platdata(ab8500
->dev
);
366 ab8500
->irq_base
= plat
->irq_base
;
368 mutex_init(&ab8500
->lock
);
369 mutex_init(&ab8500
->irq_lock
);
371 ret
= ab8500_read(ab8500
, AB8500_REV_REG
);
380 if (ret
== 0x0 || ret
== 0x10 || ret
== 0x11) {
381 ab8500
->revision
= ret
;
382 dev_info(ab8500
->dev
, "detected chip, revision: %#x\n", ret
);
384 dev_err(ab8500
->dev
, "unknown chip, revision: %#x\n", ret
);
388 if (plat
&& plat
->init
)
391 /* Clear and mask all interrupts */
392 for (i
= 0; i
< 10; i
++) {
393 ab8500_read(ab8500
, AB8500_IT_LATCH1_REG
+ i
);
394 ab8500_write(ab8500
, AB8500_IT_MASK1_REG
+ i
, 0xff);
397 for (i
= 18; i
< 24; i
++) {
398 ab8500_read(ab8500
, AB8500_IT_LATCH1_REG
+ i
);
399 ab8500_write(ab8500
, AB8500_IT_MASK1_REG
+ i
, 0xff);
402 for (i
= 0; i
< AB8500_NUM_IRQ_REGS
; i
++)
403 ab8500
->mask
[i
] = ab8500
->oldmask
[i
] = 0xff;
405 if (ab8500
->irq_base
) {
406 ret
= ab8500_irq_init(ab8500
);
410 ret
= request_threaded_irq(ab8500
->irq
, NULL
, ab8500_irq
,
411 IRQF_ONESHOT
, "ab8500", ab8500
);
416 ret
= mfd_add_devices(ab8500
->dev
, 0, ab8500_devs
,
417 ARRAY_SIZE(ab8500_devs
), NULL
,
425 if (ab8500
->irq_base
) {
426 free_irq(ab8500
->irq
, ab8500
);
428 ab8500_irq_remove(ab8500
);
433 int __devexit
ab8500_exit(struct ab8500
*ab8500
)
435 mfd_remove_devices(ab8500
->dev
);
436 if (ab8500
->irq_base
) {
437 free_irq(ab8500
->irq
, ab8500
);
438 ab8500_irq_remove(ab8500
);
444 MODULE_AUTHOR("Srinidhi Kasagar, Rabin Vincent");
445 MODULE_DESCRIPTION("AB8500 MFD core");
446 MODULE_LICENSE("GPL v2");