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>
7 * Author: Mattias Wallin <mattias.wallin@stericsson.com>
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>
19 #include <linux/mfd/abx500.h>
20 #include <linux/mfd/abx500/ab8500.h>
21 #include <linux/regulator/ab8500.h>
24 * Interrupt register offsets
27 #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 AB9540_IT_SOURCE13_REG 0x0C
36 #define AB8500_IT_SOURCE19_REG 0x12
37 #define AB8500_IT_SOURCE20_REG 0x13
38 #define AB8500_IT_SOURCE21_REG 0x14
39 #define AB8500_IT_SOURCE22_REG 0x15
40 #define AB8500_IT_SOURCE23_REG 0x16
41 #define AB8500_IT_SOURCE24_REG 0x17
46 #define AB8500_IT_LATCH1_REG 0x20
47 #define AB8500_IT_LATCH2_REG 0x21
48 #define AB8500_IT_LATCH3_REG 0x22
49 #define AB8500_IT_LATCH4_REG 0x23
50 #define AB8500_IT_LATCH5_REG 0x24
51 #define AB8500_IT_LATCH6_REG 0x25
52 #define AB8500_IT_LATCH7_REG 0x26
53 #define AB8500_IT_LATCH8_REG 0x27
54 #define AB8500_IT_LATCH9_REG 0x28
55 #define AB8500_IT_LATCH10_REG 0x29
56 #define AB8500_IT_LATCH12_REG 0x2B
57 #define AB9540_IT_LATCH13_REG 0x2C
58 #define AB8500_IT_LATCH19_REG 0x32
59 #define AB8500_IT_LATCH20_REG 0x33
60 #define AB8500_IT_LATCH21_REG 0x34
61 #define AB8500_IT_LATCH22_REG 0x35
62 #define AB8500_IT_LATCH23_REG 0x36
63 #define AB8500_IT_LATCH24_REG 0x37
69 #define AB8500_IT_MASK1_REG 0x40
70 #define AB8500_IT_MASK2_REG 0x41
71 #define AB8500_IT_MASK3_REG 0x42
72 #define AB8500_IT_MASK4_REG 0x43
73 #define AB8500_IT_MASK5_REG 0x44
74 #define AB8500_IT_MASK6_REG 0x45
75 #define AB8500_IT_MASK7_REG 0x46
76 #define AB8500_IT_MASK8_REG 0x47
77 #define AB8500_IT_MASK9_REG 0x48
78 #define AB8500_IT_MASK10_REG 0x49
79 #define AB8500_IT_MASK11_REG 0x4A
80 #define AB8500_IT_MASK12_REG 0x4B
81 #define AB8500_IT_MASK13_REG 0x4C
82 #define AB8500_IT_MASK14_REG 0x4D
83 #define AB8500_IT_MASK15_REG 0x4E
84 #define AB8500_IT_MASK16_REG 0x4F
85 #define AB8500_IT_MASK17_REG 0x50
86 #define AB8500_IT_MASK18_REG 0x51
87 #define AB8500_IT_MASK19_REG 0x52
88 #define AB8500_IT_MASK20_REG 0x53
89 #define AB8500_IT_MASK21_REG 0x54
90 #define AB8500_IT_MASK22_REG 0x55
91 #define AB8500_IT_MASK23_REG 0x56
92 #define AB8500_IT_MASK24_REG 0x57
95 * latch hierarchy registers
97 #define AB8500_IT_LATCHHIER1_REG 0x60
98 #define AB8500_IT_LATCHHIER2_REG 0x61
99 #define AB8500_IT_LATCHHIER3_REG 0x62
101 #define AB8500_IT_LATCHHIER_NUM 3
103 #define AB8500_REV_REG 0x80
104 #define AB8500_IC_NAME_REG 0x82
105 #define AB8500_SWITCH_OFF_STATUS 0x00
107 #define AB8500_TURN_ON_STATUS 0x00
109 static bool no_bm; /* No battery management */
110 module_param(no_bm, bool, S_IRUGO);
112 #define AB9540_MODEM_CTRL2_REG 0x23
113 #define AB9540_MODEM_CTRL2_SWDBBRSTN_BIT BIT(2)
116 * Map interrupt numbers to the LATCH and MASK register offsets, Interrupt
117 * numbers are indexed into this array with (num / 8). The interupts are
118 * defined in linux/mfd/ab8500.h
120 * This is one off from the register names, i.e. AB8500_IT_MASK1_REG is at
124 static const int ab8500_irq_regoffset[AB8500_NUM_IRQ_REGS] = {
125 0, 1, 2, 3, 4, 6, 7, 8, 9, 11, 18, 19, 20, 21,
129 static const int ab9540_irq_regoffset[AB9540_NUM_IRQ_REGS] = {
130 0, 1, 2, 3, 4, 6, 7, 8, 9, 11, 18, 19, 20, 21, 12, 13, 24,
133 static const char ab8500_version_str[][7] = {
134 [AB8500_VERSION_AB8500] = "AB8500",
135 [AB8500_VERSION_AB8505] = "AB8505",
136 [AB8500_VERSION_AB9540] = "AB9540",
137 [AB8500_VERSION_AB8540] = "AB8540",
140 static int ab8500_get_chip_id(struct device *dev)
142 struct ab8500 *ab8500;
146 ab8500 = dev_get_drvdata(dev->parent);
147 return ab8500 ? (int)ab8500->chip_id : -EINVAL;
150 static int set_register_interruptible(struct ab8500 *ab8500, u8 bank,
155 * Put the u8 bank and u8 register together into a an u16.
156 * The bank on higher 8 bits and register in lower 8 bits.
158 u16 addr = ((u16)bank) << 8 | reg;
160 dev_vdbg(ab8500->dev, "wr: addr %#x <= %#x\n", addr, data);
162 mutex_lock(&ab8500->lock);
164 ret = ab8500->write(ab8500, addr, data);
166 dev_err(ab8500->dev, "failed to write reg %#x: %d\n",
168 mutex_unlock(&ab8500->lock);
173 static int ab8500_set_register(struct device *dev, u8 bank,
177 struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
179 atomic_inc(&ab8500->transfer_ongoing);
180 ret = set_register_interruptible(ab8500, bank, reg, value);
181 atomic_dec(&ab8500->transfer_ongoing);
185 static int get_register_interruptible(struct ab8500 *ab8500, u8 bank,
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;
193 mutex_lock(&ab8500->lock);
195 ret = ab8500->read(ab8500, addr);
197 dev_err(ab8500->dev, "failed to read reg %#x: %d\n",
202 mutex_unlock(&ab8500->lock);
203 dev_vdbg(ab8500->dev, "rd: addr %#x => data %#x\n", addr, ret);
208 static int ab8500_get_register(struct device *dev, u8 bank,
212 struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
214 atomic_inc(&ab8500->transfer_ongoing);
215 ret = get_register_interruptible(ab8500, bank, reg, value);
216 atomic_dec(&ab8500->transfer_ongoing);
220 static int mask_and_set_register_interruptible(struct ab8500 *ab8500, u8 bank,
221 u8 reg, u8 bitmask, u8 bitvalues)
224 /* put the u8 bank and u8 reg together into a an u16.
225 * bank on higher 8 bits and reg in lower */
226 u16 addr = ((u16)bank) << 8 | reg;
228 mutex_lock(&ab8500->lock);
230 if (ab8500->write_masked == NULL) {
233 ret = ab8500->read(ab8500, addr);
235 dev_err(ab8500->dev, "failed to read reg %#x: %d\n",
241 data = (~bitmask & data) | (bitmask & bitvalues);
243 ret = ab8500->write(ab8500, addr, data);
245 dev_err(ab8500->dev, "failed to write reg %#x: %d\n",
248 dev_vdbg(ab8500->dev, "mask: addr %#x => data %#x\n", addr,
252 ret = ab8500->write_masked(ab8500, addr, bitmask, bitvalues);
254 dev_err(ab8500->dev, "failed to modify reg %#x: %d\n", addr,
257 mutex_unlock(&ab8500->lock);
261 static int ab8500_mask_and_set_register(struct device *dev,
262 u8 bank, u8 reg, u8 bitmask, u8 bitvalues)
265 struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
267 atomic_inc(&ab8500->transfer_ongoing);
268 ret= mask_and_set_register_interruptible(ab8500, bank, reg,
270 atomic_dec(&ab8500->transfer_ongoing);
274 static struct abx500_ops ab8500_ops = {
275 .get_chip_id = ab8500_get_chip_id,
276 .get_register = ab8500_get_register,
277 .set_register = ab8500_set_register,
278 .get_register_page = NULL,
279 .set_register_page = NULL,
280 .mask_and_set_register = ab8500_mask_and_set_register,
281 .event_registers_startup_state_get = NULL,
282 .startup_irq_enabled = NULL,
285 static void ab8500_irq_lock(struct irq_data *data)
287 struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
289 mutex_lock(&ab8500->irq_lock);
290 atomic_inc(&ab8500->transfer_ongoing);
293 static void ab8500_irq_sync_unlock(struct irq_data *data)
295 struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
298 for (i = 0; i < ab8500->mask_size; i++) {
299 u8 old = ab8500->oldmask[i];
300 u8 new = ab8500->mask[i];
307 * Interrupt register 12 doesn't exist prior to AB8500 version
310 if (ab8500->irq_reg_offset[i] == 11 &&
311 is_ab8500_1p1_or_earlier(ab8500))
314 ab8500->oldmask[i] = new;
316 reg = AB8500_IT_MASK1_REG + ab8500->irq_reg_offset[i];
317 set_register_interruptible(ab8500, AB8500_INTERRUPT, reg, new);
319 atomic_dec(&ab8500->transfer_ongoing);
320 mutex_unlock(&ab8500->irq_lock);
323 static void ab8500_irq_mask(struct irq_data *data)
325 struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
326 int offset = data->irq - ab8500->irq_base;
327 int index = offset / 8;
328 int mask = 1 << (offset % 8);
330 ab8500->mask[index] |= mask;
333 static void ab8500_irq_unmask(struct irq_data *data)
335 struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
336 int offset = data->irq - ab8500->irq_base;
337 int index = offset / 8;
338 int mask = 1 << (offset % 8);
340 ab8500->mask[index] &= ~mask;
343 static struct irq_chip ab8500_irq_chip = {
345 .irq_bus_lock = ab8500_irq_lock,
346 .irq_bus_sync_unlock = ab8500_irq_sync_unlock,
347 .irq_mask = ab8500_irq_mask,
348 .irq_disable = ab8500_irq_mask,
349 .irq_unmask = ab8500_irq_unmask,
352 static int ab8500_handle_hierarchical_line(struct ab8500 *ab8500,
353 int latch_offset, u8 latch_val)
355 int int_bit = __ffs(latch_val);
359 int_bit = __ffs(latch_val);
361 for (i = 0; i < ab8500->mask_size; i++)
362 if (ab8500->irq_reg_offset[i] == latch_offset)
365 if (i >= ab8500->mask_size) {
366 dev_err(ab8500->dev, "Register offset 0x%2x not declared\n",
371 line = (i << 3) + int_bit;
372 latch_val &= ~(1 << int_bit);
374 handle_nested_irq(ab8500->irq_base + line);
380 static int ab8500_handle_hierarchical_latch(struct ab8500 *ab8500,
381 int hier_offset, u8 hier_val)
383 int latch_bit, status;
384 u8 latch_offset, latch_val;
387 latch_bit = __ffs(hier_val);
388 latch_offset = (hier_offset << 3) + latch_bit;
390 /* Fix inconsistent ITFromLatch25 bit mapping... */
391 if (unlikely(latch_offset == 17))
394 status = get_register_interruptible(ab8500,
396 AB8500_IT_LATCH1_REG + latch_offset,
398 if (status < 0 || latch_val == 0)
401 status = ab8500_handle_hierarchical_line(ab8500,
402 latch_offset, latch_val);
406 hier_val &= ~(1 << latch_bit);
412 static irqreturn_t ab8500_hierarchical_irq(int irq, void *dev)
414 struct ab8500 *ab8500 = dev;
417 dev_vdbg(ab8500->dev, "interrupt\n");
419 /* Hierarchical interrupt version */
420 for (i = 0; i < AB8500_IT_LATCHHIER_NUM; i++) {
424 status = get_register_interruptible(ab8500, AB8500_INTERRUPT,
425 AB8500_IT_LATCHHIER1_REG + i, &hier_val);
426 if (status < 0 || hier_val == 0)
429 status = ab8500_handle_hierarchical_latch(ab8500, i, hier_val);
436 static irqreturn_t ab8500_irq(int irq, void *dev)
438 struct ab8500 *ab8500 = dev;
441 dev_vdbg(ab8500->dev, "interrupt\n");
443 atomic_inc(&ab8500->transfer_ongoing);
445 for (i = 0; i < ab8500->mask_size; i++) {
446 int regoffset = ab8500->irq_reg_offset[i];
451 * Interrupt register 12 doesn't exist prior to AB8500 version
454 if (regoffset == 11 && is_ab8500_1p1_or_earlier(ab8500))
457 status = get_register_interruptible(ab8500, AB8500_INTERRUPT,
458 AB8500_IT_LATCH1_REG + regoffset, &value);
459 if (status < 0 || value == 0)
463 int bit = __ffs(value);
464 int line = i * 8 + bit;
466 handle_nested_irq(ab8500->irq_base + line);
467 value &= ~(1 << bit);
471 atomic_dec(&ab8500->transfer_ongoing);
475 static int ab8500_irq_init(struct ab8500 *ab8500)
477 int base = ab8500->irq_base;
481 if (is_ab9540(ab8500))
482 num_irqs = AB9540_NR_IRQS;
483 else if (is_ab8505(ab8500))
484 num_irqs = AB8505_NR_IRQS;
486 num_irqs = AB8500_NR_IRQS;
488 for (irq = base; irq < base + num_irqs; irq++) {
489 irq_set_chip_data(irq, ab8500);
490 irq_set_chip_and_handler(irq, &ab8500_irq_chip,
492 irq_set_nested_thread(irq, 1);
494 set_irq_flags(irq, IRQF_VALID);
496 irq_set_noprobe(irq);
503 static void ab8500_irq_remove(struct ab8500 *ab8500)
505 int base = ab8500->irq_base;
509 if (is_ab9540(ab8500))
510 num_irqs = AB9540_NR_IRQS;
511 else if (is_ab8505(ab8500))
512 num_irqs = AB8505_NR_IRQS;
514 num_irqs = AB8500_NR_IRQS;
516 for (irq = base; irq < base + num_irqs; irq++) {
518 set_irq_flags(irq, 0);
520 irq_set_chip_and_handler(irq, NULL, NULL);
521 irq_set_chip_data(irq, NULL);
525 int ab8500_suspend(struct ab8500 *ab8500)
527 if (atomic_read(&ab8500->transfer_ongoing))
533 /* AB8500 GPIO Resources */
534 static struct resource __devinitdata ab8500_gpio_resources[] = {
537 .start = AB8500_INT_GPIO6R,
538 .end = AB8500_INT_GPIO41F,
539 .flags = IORESOURCE_IRQ,
543 /* AB9540 GPIO Resources */
544 static struct resource __devinitdata ab9540_gpio_resources[] = {
547 .start = AB8500_INT_GPIO6R,
548 .end = AB8500_INT_GPIO41F,
549 .flags = IORESOURCE_IRQ,
552 .name = "GPIO_INT14",
553 .start = AB9540_INT_GPIO50R,
554 .end = AB9540_INT_GPIO54R,
555 .flags = IORESOURCE_IRQ,
558 .name = "GPIO_INT15",
559 .start = AB9540_INT_GPIO50F,
560 .end = AB9540_INT_GPIO54F,
561 .flags = IORESOURCE_IRQ,
565 static struct resource __devinitdata ab8500_gpadc_resources[] = {
567 .name = "HW_CONV_END",
568 .start = AB8500_INT_GP_HW_ADC_CONV_END,
569 .end = AB8500_INT_GP_HW_ADC_CONV_END,
570 .flags = IORESOURCE_IRQ,
573 .name = "SW_CONV_END",
574 .start = AB8500_INT_GP_SW_ADC_CONV_END,
575 .end = AB8500_INT_GP_SW_ADC_CONV_END,
576 .flags = IORESOURCE_IRQ,
580 static struct resource __devinitdata ab8500_rtc_resources[] = {
583 .start = AB8500_INT_RTC_60S,
584 .end = AB8500_INT_RTC_60S,
585 .flags = IORESOURCE_IRQ,
589 .start = AB8500_INT_RTC_ALARM,
590 .end = AB8500_INT_RTC_ALARM,
591 .flags = IORESOURCE_IRQ,
595 static struct resource __devinitdata ab8500_poweronkey_db_resources[] = {
598 .start = AB8500_INT_PON_KEY1DB_F,
599 .end = AB8500_INT_PON_KEY1DB_F,
600 .flags = IORESOURCE_IRQ,
604 .start = AB8500_INT_PON_KEY1DB_R,
605 .end = AB8500_INT_PON_KEY1DB_R,
606 .flags = IORESOURCE_IRQ,
610 static struct resource __devinitdata ab8500_av_acc_detect_resources[] = {
612 .name = "ACC_DETECT_1DB_F",
613 .start = AB8500_INT_ACC_DETECT_1DB_F,
614 .end = AB8500_INT_ACC_DETECT_1DB_F,
615 .flags = IORESOURCE_IRQ,
618 .name = "ACC_DETECT_1DB_R",
619 .start = AB8500_INT_ACC_DETECT_1DB_R,
620 .end = AB8500_INT_ACC_DETECT_1DB_R,
621 .flags = IORESOURCE_IRQ,
624 .name = "ACC_DETECT_21DB_F",
625 .start = AB8500_INT_ACC_DETECT_21DB_F,
626 .end = AB8500_INT_ACC_DETECT_21DB_F,
627 .flags = IORESOURCE_IRQ,
630 .name = "ACC_DETECT_21DB_R",
631 .start = AB8500_INT_ACC_DETECT_21DB_R,
632 .end = AB8500_INT_ACC_DETECT_21DB_R,
633 .flags = IORESOURCE_IRQ,
636 .name = "ACC_DETECT_22DB_F",
637 .start = AB8500_INT_ACC_DETECT_22DB_F,
638 .end = AB8500_INT_ACC_DETECT_22DB_F,
639 .flags = IORESOURCE_IRQ,
642 .name = "ACC_DETECT_22DB_R",
643 .start = AB8500_INT_ACC_DETECT_22DB_R,
644 .end = AB8500_INT_ACC_DETECT_22DB_R,
645 .flags = IORESOURCE_IRQ,
649 static struct resource __devinitdata ab8500_charger_resources[] = {
651 .name = "MAIN_CH_UNPLUG_DET",
652 .start = AB8500_INT_MAIN_CH_UNPLUG_DET,
653 .end = AB8500_INT_MAIN_CH_UNPLUG_DET,
654 .flags = IORESOURCE_IRQ,
657 .name = "MAIN_CHARGE_PLUG_DET",
658 .start = AB8500_INT_MAIN_CH_PLUG_DET,
659 .end = AB8500_INT_MAIN_CH_PLUG_DET,
660 .flags = IORESOURCE_IRQ,
663 .name = "VBUS_DET_R",
664 .start = AB8500_INT_VBUS_DET_R,
665 .end = AB8500_INT_VBUS_DET_R,
666 .flags = IORESOURCE_IRQ,
669 .name = "VBUS_DET_F",
670 .start = AB8500_INT_VBUS_DET_F,
671 .end = AB8500_INT_VBUS_DET_F,
672 .flags = IORESOURCE_IRQ,
675 .name = "USB_LINK_STATUS",
676 .start = AB8500_INT_USB_LINK_STATUS,
677 .end = AB8500_INT_USB_LINK_STATUS,
678 .flags = IORESOURCE_IRQ,
682 .start = AB8500_INT_VBUS_OVV,
683 .end = AB8500_INT_VBUS_OVV,
684 .flags = IORESOURCE_IRQ,
687 .name = "USB_CH_TH_PROT_R",
688 .start = AB8500_INT_USB_CH_TH_PROT_R,
689 .end = AB8500_INT_USB_CH_TH_PROT_R,
690 .flags = IORESOURCE_IRQ,
693 .name = "USB_CH_TH_PROT_F",
694 .start = AB8500_INT_USB_CH_TH_PROT_F,
695 .end = AB8500_INT_USB_CH_TH_PROT_F,
696 .flags = IORESOURCE_IRQ,
699 .name = "MAIN_EXT_CH_NOT_OK",
700 .start = AB8500_INT_MAIN_EXT_CH_NOT_OK,
701 .end = AB8500_INT_MAIN_EXT_CH_NOT_OK,
702 .flags = IORESOURCE_IRQ,
705 .name = "MAIN_CH_TH_PROT_R",
706 .start = AB8500_INT_MAIN_CH_TH_PROT_R,
707 .end = AB8500_INT_MAIN_CH_TH_PROT_R,
708 .flags = IORESOURCE_IRQ,
711 .name = "MAIN_CH_TH_PROT_F",
712 .start = AB8500_INT_MAIN_CH_TH_PROT_F,
713 .end = AB8500_INT_MAIN_CH_TH_PROT_F,
714 .flags = IORESOURCE_IRQ,
717 .name = "USB_CHARGER_NOT_OKR",
718 .start = AB8500_INT_USB_CHARGER_NOT_OKR,
719 .end = AB8500_INT_USB_CHARGER_NOT_OKR,
720 .flags = IORESOURCE_IRQ,
724 .start = AB8500_INT_CH_WD_EXP,
725 .end = AB8500_INT_CH_WD_EXP,
726 .flags = IORESOURCE_IRQ,
730 static struct resource __devinitdata ab8500_btemp_resources[] = {
732 .name = "BAT_CTRL_INDB",
733 .start = AB8500_INT_BAT_CTRL_INDB,
734 .end = AB8500_INT_BAT_CTRL_INDB,
735 .flags = IORESOURCE_IRQ,
739 .start = AB8500_INT_BTEMP_LOW,
740 .end = AB8500_INT_BTEMP_LOW,
741 .flags = IORESOURCE_IRQ,
744 .name = "BTEMP_HIGH",
745 .start = AB8500_INT_BTEMP_HIGH,
746 .end = AB8500_INT_BTEMP_HIGH,
747 .flags = IORESOURCE_IRQ,
750 .name = "BTEMP_LOW_MEDIUM",
751 .start = AB8500_INT_BTEMP_LOW_MEDIUM,
752 .end = AB8500_INT_BTEMP_LOW_MEDIUM,
753 .flags = IORESOURCE_IRQ,
756 .name = "BTEMP_MEDIUM_HIGH",
757 .start = AB8500_INT_BTEMP_MEDIUM_HIGH,
758 .end = AB8500_INT_BTEMP_MEDIUM_HIGH,
759 .flags = IORESOURCE_IRQ,
763 static struct resource __devinitdata ab8500_fg_resources[] = {
765 .name = "NCONV_ACCU",
766 .start = AB8500_INT_CCN_CONV_ACC,
767 .end = AB8500_INT_CCN_CONV_ACC,
768 .flags = IORESOURCE_IRQ,
772 .start = AB8500_INT_BATT_OVV,
773 .end = AB8500_INT_BATT_OVV,
774 .flags = IORESOURCE_IRQ,
778 .start = AB8500_INT_LOW_BAT_F,
779 .end = AB8500_INT_LOW_BAT_F,
780 .flags = IORESOURCE_IRQ,
784 .start = AB8500_INT_LOW_BAT_R,
785 .end = AB8500_INT_LOW_BAT_R,
786 .flags = IORESOURCE_IRQ,
789 .name = "CC_INT_CALIB",
790 .start = AB8500_INT_CC_INT_CALIB,
791 .end = AB8500_INT_CC_INT_CALIB,
792 .flags = IORESOURCE_IRQ,
796 .start = AB8500_INT_CCEOC,
797 .end = AB8500_INT_CCEOC,
798 .flags = IORESOURCE_IRQ,
802 static struct resource __devinitdata ab8500_chargalg_resources[] = {};
804 #ifdef CONFIG_DEBUG_FS
805 static struct resource __devinitdata ab8500_debug_resources[] = {
808 .start = AB8500_INT_MAIN_EXT_CH_NOT_OK,
809 .end = AB8500_INT_MAIN_EXT_CH_NOT_OK,
810 .flags = IORESOURCE_IRQ,
814 .start = AB8500_INT_XTAL32K_KO,
815 .end = AB8500_INT_XTAL32K_KO,
816 .flags = IORESOURCE_IRQ,
821 static struct resource __devinitdata ab8500_usb_resources[] = {
823 .name = "ID_WAKEUP_R",
824 .start = AB8500_INT_ID_WAKEUP_R,
825 .end = AB8500_INT_ID_WAKEUP_R,
826 .flags = IORESOURCE_IRQ,
829 .name = "ID_WAKEUP_F",
830 .start = AB8500_INT_ID_WAKEUP_F,
831 .end = AB8500_INT_ID_WAKEUP_F,
832 .flags = IORESOURCE_IRQ,
835 .name = "VBUS_DET_F",
836 .start = AB8500_INT_VBUS_DET_F,
837 .end = AB8500_INT_VBUS_DET_F,
838 .flags = IORESOURCE_IRQ,
841 .name = "VBUS_DET_R",
842 .start = AB8500_INT_VBUS_DET_R,
843 .end = AB8500_INT_VBUS_DET_R,
844 .flags = IORESOURCE_IRQ,
847 .name = "USB_LINK_STATUS",
848 .start = AB8500_INT_USB_LINK_STATUS,
849 .end = AB8500_INT_USB_LINK_STATUS,
850 .flags = IORESOURCE_IRQ,
853 .name = "USB_ADP_PROBE_PLUG",
854 .start = AB8500_INT_ADP_PROBE_PLUG,
855 .end = AB8500_INT_ADP_PROBE_PLUG,
856 .flags = IORESOURCE_IRQ,
859 .name = "USB_ADP_PROBE_UNPLUG",
860 .start = AB8500_INT_ADP_PROBE_UNPLUG,
861 .end = AB8500_INT_ADP_PROBE_UNPLUG,
862 .flags = IORESOURCE_IRQ,
866 static struct resource __devinitdata ab8505_iddet_resources[] = {
868 .name = "KeyDeglitch",
869 .start = AB8505_INT_KEYDEGLITCH,
870 .end = AB8505_INT_KEYDEGLITCH,
871 .flags = IORESOURCE_IRQ,
875 .start = AB8505_INT_KP,
876 .end = AB8505_INT_KP,
877 .flags = IORESOURCE_IRQ,
881 .start = AB8505_INT_IKP,
882 .end = AB8505_INT_IKP,
883 .flags = IORESOURCE_IRQ,
887 .start = AB8505_INT_IKR,
888 .end = AB8505_INT_IKR,
889 .flags = IORESOURCE_IRQ,
893 .start = AB8505_INT_KEYSTUCK,
894 .end = AB8505_INT_KEYSTUCK,
895 .flags = IORESOURCE_IRQ,
899 static struct resource __devinitdata ab8500_temp_resources[] = {
901 .name = "AB8500_TEMP_WARM",
902 .start = AB8500_INT_TEMP_WARM,
903 .end = AB8500_INT_TEMP_WARM,
904 .flags = IORESOURCE_IRQ,
908 static struct mfd_cell __devinitdata abx500_common_devs[] = {
909 #ifdef CONFIG_DEBUG_FS
911 .name = "ab8500-debug",
912 .num_resources = ARRAY_SIZE(ab8500_debug_resources),
913 .resources = ab8500_debug_resources,
917 .name = "ab8500-sysctrl",
920 .name = "ab8500-regulator",
923 .name = "ab8500-gpadc",
924 .num_resources = ARRAY_SIZE(ab8500_gpadc_resources),
925 .resources = ab8500_gpadc_resources,
928 .name = "ab8500-rtc",
929 .num_resources = ARRAY_SIZE(ab8500_rtc_resources),
930 .resources = ab8500_rtc_resources,
933 .name = "ab8500-acc-det",
934 .num_resources = ARRAY_SIZE(ab8500_av_acc_detect_resources),
935 .resources = ab8500_av_acc_detect_resources,
938 .name = "ab8500-poweron-key",
939 .num_resources = ARRAY_SIZE(ab8500_poweronkey_db_resources),
940 .resources = ab8500_poweronkey_db_resources,
943 .name = "ab8500-pwm",
947 .name = "ab8500-pwm",
951 .name = "ab8500-pwm",
954 { .name = "ab8500-leds", },
956 .name = "ab8500-denc",
959 .name = "ab8500-temp",
960 .num_resources = ARRAY_SIZE(ab8500_temp_resources),
961 .resources = ab8500_temp_resources,
965 static struct mfd_cell __devinitdata ab8500_bm_devs[] = {
967 .name = "ab8500-charger",
968 .num_resources = ARRAY_SIZE(ab8500_charger_resources),
969 .resources = ab8500_charger_resources,
972 .name = "ab8500-btemp",
973 .num_resources = ARRAY_SIZE(ab8500_btemp_resources),
974 .resources = ab8500_btemp_resources,
978 .num_resources = ARRAY_SIZE(ab8500_fg_resources),
979 .resources = ab8500_fg_resources,
982 .name = "ab8500-chargalg",
983 .num_resources = ARRAY_SIZE(ab8500_chargalg_resources),
984 .resources = ab8500_chargalg_resources,
988 static struct mfd_cell __devinitdata ab8500_devs[] = {
990 .name = "ab8500-gpio",
991 .num_resources = ARRAY_SIZE(ab8500_gpio_resources),
992 .resources = ab8500_gpio_resources,
995 .name = "ab8500-usb",
996 .num_resources = ARRAY_SIZE(ab8500_usb_resources),
997 .resources = ab8500_usb_resources,
1000 .name = "ab8500-codec",
1004 static struct mfd_cell __devinitdata ab9540_devs[] = {
1006 .name = "ab8500-gpio",
1007 .num_resources = ARRAY_SIZE(ab9540_gpio_resources),
1008 .resources = ab9540_gpio_resources,
1011 .name = "ab9540-usb",
1012 .num_resources = ARRAY_SIZE(ab8500_usb_resources),
1013 .resources = ab8500_usb_resources,
1016 .name = "ab9540-codec",
1020 /* Device list common to ab9540 and ab8505 */
1021 static struct mfd_cell __devinitdata ab9540_ab8505_devs[] = {
1024 .num_resources = ARRAY_SIZE(ab8505_iddet_resources),
1025 .resources = ab8505_iddet_resources,
1029 static ssize_t show_chip_id(struct device *dev,
1030 struct device_attribute *attr, char *buf)
1032 struct ab8500 *ab8500;
1034 ab8500 = dev_get_drvdata(dev);
1035 return sprintf(buf, "%#x\n", ab8500 ? ab8500->chip_id : -EINVAL);
1039 * ab8500 has switched off due to (SWITCH_OFF_STATUS):
1040 * 0x01 Swoff bit programming
1041 * 0x02 Thermal protection activation
1042 * 0x04 Vbat lower then BattOk falling threshold
1043 * 0x08 Watchdog expired
1044 * 0x10 Non presence of 32kHz clock
1045 * 0x20 Battery level lower than power on reset threshold
1046 * 0x40 Power on key 1 pressed longer than 10 seconds
1047 * 0x80 DB8500 thermal shutdown
1049 static ssize_t show_switch_off_status(struct device *dev,
1050 struct device_attribute *attr, char *buf)
1054 struct ab8500 *ab8500;
1056 ab8500 = dev_get_drvdata(dev);
1057 ret = get_register_interruptible(ab8500, AB8500_RTC,
1058 AB8500_SWITCH_OFF_STATUS, &value);
1061 return sprintf(buf, "%#x\n", value);
1065 * ab8500 has turned on due to (TURN_ON_STATUS):
1075 static ssize_t show_turn_on_status(struct device *dev,
1076 struct device_attribute *attr, char *buf)
1080 struct ab8500 *ab8500;
1082 ab8500 = dev_get_drvdata(dev);
1083 ret = get_register_interruptible(ab8500, AB8500_SYS_CTRL1_BLOCK,
1084 AB8500_TURN_ON_STATUS, &value);
1087 return sprintf(buf, "%#x\n", value);
1090 static ssize_t show_ab9540_dbbrstn(struct device *dev,
1091 struct device_attribute *attr, char *buf)
1093 struct ab8500 *ab8500;
1097 ab8500 = dev_get_drvdata(dev);
1099 ret = get_register_interruptible(ab8500, AB8500_REGU_CTRL2,
1100 AB9540_MODEM_CTRL2_REG, &value);
1104 return sprintf(buf, "%d\n",
1105 (value & AB9540_MODEM_CTRL2_SWDBBRSTN_BIT) ? 1 : 0);
1108 static ssize_t store_ab9540_dbbrstn(struct device *dev,
1109 struct device_attribute *attr, const char *buf, size_t count)
1111 struct ab8500 *ab8500;
1116 ab8500 = dev_get_drvdata(dev);
1124 bitvalues = AB9540_MODEM_CTRL2_SWDBBRSTN_BIT;
1130 err = mask_and_set_register_interruptible(ab8500,
1131 AB8500_REGU_CTRL2, AB9540_MODEM_CTRL2_REG,
1132 AB9540_MODEM_CTRL2_SWDBBRSTN_BIT, bitvalues);
1134 dev_info(ab8500->dev,
1135 "Failed to set DBBRSTN %c, err %#x\n",
1143 static DEVICE_ATTR(chip_id, S_IRUGO, show_chip_id, NULL);
1144 static DEVICE_ATTR(switch_off_status, S_IRUGO, show_switch_off_status, NULL);
1145 static DEVICE_ATTR(turn_on_status, S_IRUGO, show_turn_on_status, NULL);
1146 static DEVICE_ATTR(dbbrstn, S_IRUGO | S_IWUSR,
1147 show_ab9540_dbbrstn, store_ab9540_dbbrstn);
1149 static struct attribute *ab8500_sysfs_entries[] = {
1150 &dev_attr_chip_id.attr,
1151 &dev_attr_switch_off_status.attr,
1152 &dev_attr_turn_on_status.attr,
1156 static struct attribute *ab9540_sysfs_entries[] = {
1157 &dev_attr_chip_id.attr,
1158 &dev_attr_switch_off_status.attr,
1159 &dev_attr_turn_on_status.attr,
1160 &dev_attr_dbbrstn.attr,
1164 static struct attribute_group ab8500_attr_group = {
1165 .attrs = ab8500_sysfs_entries,
1168 static struct attribute_group ab9540_attr_group = {
1169 .attrs = ab9540_sysfs_entries,
1172 int __devinit ab8500_init(struct ab8500 *ab8500, enum ab8500_version version)
1174 struct ab8500_platform_data *plat = dev_get_platdata(ab8500->dev);
1180 ab8500->irq_base = plat->irq_base;
1182 mutex_init(&ab8500->lock);
1183 mutex_init(&ab8500->irq_lock);
1184 atomic_set(&ab8500->transfer_ongoing, 0);
1186 if (version != AB8500_VERSION_UNDEFINED)
1187 ab8500->version = version;
1189 ret = get_register_interruptible(ab8500, AB8500_MISC,
1190 AB8500_IC_NAME_REG, &value);
1194 ab8500->version = value;
1197 ret = get_register_interruptible(ab8500, AB8500_MISC,
1198 AB8500_REV_REG, &value);
1202 ab8500->chip_id = value;
1204 dev_info(ab8500->dev, "detected chip, %s rev. %1x.%1x\n",
1205 ab8500_version_str[ab8500->version],
1206 ab8500->chip_id >> 4,
1207 ab8500->chip_id & 0x0F);
1209 /* Configure AB8500 or AB9540 IRQ */
1210 if (is_ab9540(ab8500) || is_ab8505(ab8500)) {
1211 ab8500->mask_size = AB9540_NUM_IRQ_REGS;
1212 ab8500->irq_reg_offset = ab9540_irq_regoffset;
1214 ab8500->mask_size = AB8500_NUM_IRQ_REGS;
1215 ab8500->irq_reg_offset = ab8500_irq_regoffset;
1217 ab8500->mask = kzalloc(ab8500->mask_size, GFP_KERNEL);
1220 ab8500->oldmask = kzalloc(ab8500->mask_size, GFP_KERNEL);
1221 if (!ab8500->oldmask) {
1226 * ab8500 has switched off due to (SWITCH_OFF_STATUS):
1227 * 0x01 Swoff bit programming
1228 * 0x02 Thermal protection activation
1229 * 0x04 Vbat lower then BattOk falling threshold
1230 * 0x08 Watchdog expired
1231 * 0x10 Non presence of 32kHz clock
1232 * 0x20 Battery level lower than power on reset threshold
1233 * 0x40 Power on key 1 pressed longer than 10 seconds
1234 * 0x80 DB8500 thermal shutdown
1237 ret = get_register_interruptible(ab8500, AB8500_RTC,
1238 AB8500_SWITCH_OFF_STATUS, &value);
1241 dev_info(ab8500->dev, "switch off status: %#x", value);
1243 if (plat && plat->init)
1246 /* Clear and mask all interrupts */
1247 for (i = 0; i < ab8500->mask_size; i++) {
1249 * Interrupt register 12 doesn't exist prior to AB8500 version
1252 if (ab8500->irq_reg_offset[i] == 11 &&
1253 is_ab8500_1p1_or_earlier(ab8500))
1256 get_register_interruptible(ab8500, AB8500_INTERRUPT,
1257 AB8500_IT_LATCH1_REG + ab8500->irq_reg_offset[i],
1259 set_register_interruptible(ab8500, AB8500_INTERRUPT,
1260 AB8500_IT_MASK1_REG + ab8500->irq_reg_offset[i], 0xff);
1263 ret = abx500_register_ops(ab8500->dev, &ab8500_ops);
1265 goto out_freeoldmask;
1267 for (i = 0; i < ab8500->mask_size; i++)
1268 ab8500->mask[i] = ab8500->oldmask[i] = 0xff;
1270 if (ab8500->irq_base) {
1271 ret = ab8500_irq_init(ab8500);
1273 goto out_freeoldmask;
1275 /* Activate this feature only in ab9540 */
1276 /* till tests are done on ab8500 1p2 or later*/
1277 if (is_ab9540(ab8500))
1278 ret = request_threaded_irq(ab8500->irq, NULL,
1279 ab8500_hierarchical_irq,
1280 IRQF_ONESHOT | IRQF_NO_SUSPEND,
1283 ret = request_threaded_irq(ab8500->irq, NULL,
1285 IRQF_ONESHOT | IRQF_NO_SUSPEND,
1291 ret = mfd_add_devices(ab8500->dev, 0, abx500_common_devs,
1292 ARRAY_SIZE(abx500_common_devs), NULL,
1298 if (is_ab9540(ab8500))
1299 ret = mfd_add_devices(ab8500->dev, 0, ab9540_devs,
1300 ARRAY_SIZE(ab9540_devs), NULL,
1303 ret = mfd_add_devices(ab8500->dev, 0, ab8500_devs,
1304 ARRAY_SIZE(ab8500_devs), NULL,
1307 if (is_ab9540(ab8500) || is_ab8505(ab8500))
1308 ret = mfd_add_devices(ab8500->dev, 0, ab9540_ab8505_devs,
1309 ARRAY_SIZE(ab9540_ab8505_devs), NULL,
1316 /* Add battery management devices */
1317 ret = mfd_add_devices(ab8500->dev, 0, ab8500_bm_devs,
1318 ARRAY_SIZE(ab8500_bm_devs), NULL,
1321 dev_err(ab8500->dev, "error adding bm devices\n");
1324 if (is_ab9540(ab8500))
1325 ret = sysfs_create_group(&ab8500->dev->kobj,
1326 &ab9540_attr_group);
1328 ret = sysfs_create_group(&ab8500->dev->kobj,
1329 &ab8500_attr_group);
1331 dev_err(ab8500->dev, "error creating sysfs entries\n");
1336 if (ab8500->irq_base)
1337 free_irq(ab8500->irq, ab8500);
1339 if (ab8500->irq_base)
1340 ab8500_irq_remove(ab8500);
1342 kfree(ab8500->oldmask);
1344 kfree(ab8500->mask);
1349 int __devexit ab8500_exit(struct ab8500 *ab8500)
1351 if (is_ab9540(ab8500))
1352 sysfs_remove_group(&ab8500->dev->kobj, &ab9540_attr_group);
1354 sysfs_remove_group(&ab8500->dev->kobj, &ab8500_attr_group);
1355 mfd_remove_devices(ab8500->dev);
1356 if (ab8500->irq_base) {
1357 free_irq(ab8500->irq, ab8500);
1358 ab8500_irq_remove(ab8500);
1360 kfree(ab8500->oldmask);
1361 kfree(ab8500->mask);
1366 MODULE_AUTHOR("Mattias Wallin, Srinidhi Kasagar, Rabin Vincent");
1367 MODULE_DESCRIPTION("AB8500 MFD core");
1368 MODULE_LICENSE("GPL v2");