]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/mfd/ab8500-core.c
Merge tag 'sound-3.9' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound
[karo-tx-linux.git] / drivers / mfd / ab8500-core.c
1 /*
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>
7  * Author: Mattias Wallin <mattias.wallin@stericsson.com>
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/init.h>
13 #include <linux/irq.h>
14 #include <linux/irqdomain.h>
15 #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>
20 #include <linux/mfd/abx500.h>
21 #include <linux/mfd/abx500/ab8500.h>
22 #include <linux/mfd/abx500/ab8500-bm.h>
23 #include <linux/mfd/dbx500-prcmu.h>
24 #include <linux/regulator/ab8500.h>
25 #include <linux/of.h>
26 #include <linux/of_device.h>
27
28 /*
29  * Interrupt register offsets
30  * Bank : 0x0E
31  */
32 #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
40 #define AB9540_IT_SOURCE13_REG          0x0C
41 #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
47
48 /*
49  * latch registers
50  */
51 #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
61 #define AB8500_IT_LATCH12_REG           0x2B
62 #define AB9540_IT_LATCH13_REG           0x2C
63 #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
69
70 /*
71  * mask registers
72  */
73
74 #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
98
99 /*
100  * latch hierarchy registers
101  */
102 #define AB8500_IT_LATCHHIER1_REG        0x60
103 #define AB8500_IT_LATCHHIER2_REG        0x61
104 #define AB8500_IT_LATCHHIER3_REG        0x62
105
106 #define AB8500_IT_LATCHHIER_NUM         3
107
108 #define AB8500_REV_REG                  0x80
109 #define AB8500_IC_NAME_REG              0x82
110 #define AB8500_SWITCH_OFF_STATUS        0x00
111
112 #define AB8500_TURN_ON_STATUS           0x00
113
114 static bool no_bm; /* No battery management */
115 module_param(no_bm, bool, S_IRUGO);
116
117 #define AB9540_MODEM_CTRL2_REG                  0x23
118 #define AB9540_MODEM_CTRL2_SWDBBRSTN_BIT        BIT(2)
119
120 /*
121  * Map interrupt numbers to the LATCH and MASK register offsets, Interrupt
122  * numbers are indexed into this array with (num / 8). The interupts are
123  * defined in linux/mfd/ab8500.h
124  *
125  * This is one off from the register names, i.e. AB8500_IT_MASK1_REG is at
126  * offset 0.
127  */
128 /* AB8500 support */
129 static const int ab8500_irq_regoffset[AB8500_NUM_IRQ_REGS] = {
130         0, 1, 2, 3, 4, 6, 7, 8, 9, 11, 18, 19, 20, 21,
131 };
132
133 /* AB9540 support */
134 static const int ab9540_irq_regoffset[AB9540_NUM_IRQ_REGS] = {
135         0, 1, 2, 3, 4, 6, 7, 8, 9, 11, 18, 19, 20, 21, 12, 13, 24,
136 };
137
138 static const char ab8500_version_str[][7] = {
139         [AB8500_VERSION_AB8500] = "AB8500",
140         [AB8500_VERSION_AB8505] = "AB8505",
141         [AB8500_VERSION_AB9540] = "AB9540",
142         [AB8500_VERSION_AB8540] = "AB8540",
143 };
144
145 static int ab8500_prcmu_write(struct ab8500 *ab8500, u16 addr, u8 data)
146 {
147         int ret;
148
149         ret = prcmu_abb_write((u8)(addr >> 8), (u8)(addr & 0xFF), &data, 1);
150         if (ret < 0)
151                 dev_err(ab8500->dev, "prcmu i2c error %d\n", ret);
152         return ret;
153 }
154
155 static int ab8500_prcmu_write_masked(struct ab8500 *ab8500, u16 addr, u8 mask,
156         u8 data)
157 {
158         int ret;
159
160         ret = prcmu_abb_write_masked((u8)(addr >> 8), (u8)(addr & 0xFF), &data,
161                 &mask, 1);
162         if (ret < 0)
163                 dev_err(ab8500->dev, "prcmu i2c error %d\n", ret);
164         return ret;
165 }
166
167 static int ab8500_prcmu_read(struct ab8500 *ab8500, u16 addr)
168 {
169         int ret;
170         u8 data;
171
172         ret = prcmu_abb_read((u8)(addr >> 8), (u8)(addr & 0xFF), &data, 1);
173         if (ret < 0) {
174                 dev_err(ab8500->dev, "prcmu i2c error %d\n", ret);
175                 return ret;
176         }
177         return (int)data;
178 }
179
180 static int ab8500_get_chip_id(struct device *dev)
181 {
182         struct ab8500 *ab8500;
183
184         if (!dev)
185                 return -EINVAL;
186         ab8500 = dev_get_drvdata(dev->parent);
187         return ab8500 ? (int)ab8500->chip_id : -EINVAL;
188 }
189
190 static int set_register_interruptible(struct ab8500 *ab8500, u8 bank,
191         u8 reg, u8 data)
192 {
193         int ret;
194         /*
195          * Put the u8 bank and u8 register together into a an u16.
196          * The bank on higher 8 bits and register in lower 8 bits.
197          * */
198         u16 addr = ((u16)bank) << 8 | reg;
199
200         dev_vdbg(ab8500->dev, "wr: addr %#x <= %#x\n", addr, data);
201
202         mutex_lock(&ab8500->lock);
203
204         ret = ab8500->write(ab8500, addr, data);
205         if (ret < 0)
206                 dev_err(ab8500->dev, "failed to write reg %#x: %d\n",
207                         addr, ret);
208         mutex_unlock(&ab8500->lock);
209
210         return ret;
211 }
212
213 static int ab8500_set_register(struct device *dev, u8 bank,
214         u8 reg, u8 value)
215 {
216         int ret;
217         struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
218
219         atomic_inc(&ab8500->transfer_ongoing);
220         ret = set_register_interruptible(ab8500, bank, reg, value);
221         atomic_dec(&ab8500->transfer_ongoing);
222         return ret;
223 }
224
225 static int get_register_interruptible(struct ab8500 *ab8500, u8 bank,
226         u8 reg, u8 *value)
227 {
228         int ret;
229         /* put the u8 bank and u8 reg together into a an u16.
230          * bank on higher 8 bits and reg in lower */
231         u16 addr = ((u16)bank) << 8 | reg;
232
233         mutex_lock(&ab8500->lock);
234
235         ret = ab8500->read(ab8500, addr);
236         if (ret < 0)
237                 dev_err(ab8500->dev, "failed to read reg %#x: %d\n",
238                         addr, ret);
239         else
240                 *value = ret;
241
242         mutex_unlock(&ab8500->lock);
243         dev_vdbg(ab8500->dev, "rd: addr %#x => data %#x\n", addr, ret);
244
245         return ret;
246 }
247
248 static int ab8500_get_register(struct device *dev, u8 bank,
249         u8 reg, u8 *value)
250 {
251         int ret;
252         struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
253
254         atomic_inc(&ab8500->transfer_ongoing);
255         ret = get_register_interruptible(ab8500, bank, reg, value);
256         atomic_dec(&ab8500->transfer_ongoing);
257         return ret;
258 }
259
260 static int mask_and_set_register_interruptible(struct ab8500 *ab8500, u8 bank,
261         u8 reg, u8 bitmask, u8 bitvalues)
262 {
263         int ret;
264         /* put the u8 bank and u8 reg together into a an u16.
265          * bank on higher 8 bits and reg in lower */
266         u16 addr = ((u16)bank) << 8 | reg;
267
268         mutex_lock(&ab8500->lock);
269
270         if (ab8500->write_masked == NULL) {
271                 u8 data;
272
273                 ret = ab8500->read(ab8500, addr);
274                 if (ret < 0) {
275                         dev_err(ab8500->dev, "failed to read reg %#x: %d\n",
276                                 addr, ret);
277                         goto out;
278                 }
279
280                 data = (u8)ret;
281                 data = (~bitmask & data) | (bitmask & bitvalues);
282
283                 ret = ab8500->write(ab8500, addr, data);
284                 if (ret < 0)
285                         dev_err(ab8500->dev, "failed to write reg %#x: %d\n",
286                                 addr, ret);
287
288                 dev_vdbg(ab8500->dev, "mask: addr %#x => data %#x\n", addr,
289                         data);
290                 goto out;
291         }
292         ret = ab8500->write_masked(ab8500, addr, bitmask, bitvalues);
293         if (ret < 0)
294                 dev_err(ab8500->dev, "failed to modify reg %#x: %d\n", addr,
295                         ret);
296 out:
297         mutex_unlock(&ab8500->lock);
298         return ret;
299 }
300
301 static int ab8500_mask_and_set_register(struct device *dev,
302         u8 bank, u8 reg, u8 bitmask, u8 bitvalues)
303 {
304         int ret;
305         struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
306
307         atomic_inc(&ab8500->transfer_ongoing);
308         ret= mask_and_set_register_interruptible(ab8500, bank, reg,
309                                                  bitmask, bitvalues);
310         atomic_dec(&ab8500->transfer_ongoing);
311         return ret;
312 }
313
314 static struct abx500_ops ab8500_ops = {
315         .get_chip_id = ab8500_get_chip_id,
316         .get_register = ab8500_get_register,
317         .set_register = ab8500_set_register,
318         .get_register_page = NULL,
319         .set_register_page = NULL,
320         .mask_and_set_register = ab8500_mask_and_set_register,
321         .event_registers_startup_state_get = NULL,
322         .startup_irq_enabled = NULL,
323 };
324
325 static void ab8500_irq_lock(struct irq_data *data)
326 {
327         struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
328
329         mutex_lock(&ab8500->irq_lock);
330         atomic_inc(&ab8500->transfer_ongoing);
331 }
332
333 static void ab8500_irq_sync_unlock(struct irq_data *data)
334 {
335         struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
336         int i;
337
338         for (i = 0; i < ab8500->mask_size; i++) {
339                 u8 old = ab8500->oldmask[i];
340                 u8 new = ab8500->mask[i];
341                 int reg;
342
343                 if (new == old)
344                         continue;
345
346                 /*
347                  * Interrupt register 12 doesn't exist prior to AB8500 version
348                  * 2.0
349                  */
350                 if (ab8500->irq_reg_offset[i] == 11 &&
351                         is_ab8500_1p1_or_earlier(ab8500))
352                         continue;
353
354                 ab8500->oldmask[i] = new;
355
356                 reg = AB8500_IT_MASK1_REG + ab8500->irq_reg_offset[i];
357                 set_register_interruptible(ab8500, AB8500_INTERRUPT, reg, new);
358         }
359         atomic_dec(&ab8500->transfer_ongoing);
360         mutex_unlock(&ab8500->irq_lock);
361 }
362
363 static void ab8500_irq_mask(struct irq_data *data)
364 {
365         struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
366         int offset = data->hwirq;
367         int index = offset / 8;
368         int mask = 1 << (offset % 8);
369
370         ab8500->mask[index] |= mask;
371 }
372
373 static void ab8500_irq_unmask(struct irq_data *data)
374 {
375         struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
376         int offset = data->hwirq;
377         int index = offset / 8;
378         int mask = 1 << (offset % 8);
379
380         ab8500->mask[index] &= ~mask;
381 }
382
383 static struct irq_chip ab8500_irq_chip = {
384         .name                   = "ab8500",
385         .irq_bus_lock           = ab8500_irq_lock,
386         .irq_bus_sync_unlock    = ab8500_irq_sync_unlock,
387         .irq_mask               = ab8500_irq_mask,
388         .irq_disable            = ab8500_irq_mask,
389         .irq_unmask             = ab8500_irq_unmask,
390 };
391
392 static int ab8500_handle_hierarchical_line(struct ab8500 *ab8500,
393                                         int latch_offset, u8 latch_val)
394 {
395         int int_bit = __ffs(latch_val);
396         int line, i;
397
398         do {
399                 int_bit = __ffs(latch_val);
400
401                 for (i = 0; i < ab8500->mask_size; i++)
402                         if (ab8500->irq_reg_offset[i] == latch_offset)
403                                 break;
404
405                 if (i >= ab8500->mask_size) {
406                         dev_err(ab8500->dev, "Register offset 0x%2x not declared\n",
407                                         latch_offset);
408                         return -ENXIO;
409                 }
410
411                 line = (i << 3) + int_bit;
412                 latch_val &= ~(1 << int_bit);
413
414                 handle_nested_irq(ab8500->irq_base + line);
415         } while (latch_val);
416
417         return 0;
418 }
419
420 static int ab8500_handle_hierarchical_latch(struct ab8500 *ab8500,
421                                         int hier_offset, u8 hier_val)
422 {
423         int latch_bit, status;
424         u8 latch_offset, latch_val;
425
426         do {
427                 latch_bit = __ffs(hier_val);
428                 latch_offset = (hier_offset << 3) + latch_bit;
429
430                 /* Fix inconsistent ITFromLatch25 bit mapping... */
431                 if (unlikely(latch_offset == 17))
432                         latch_offset = 24;
433
434                 status = get_register_interruptible(ab8500,
435                                 AB8500_INTERRUPT,
436                                 AB8500_IT_LATCH1_REG + latch_offset,
437                                 &latch_val);
438                 if (status < 0 || latch_val == 0)
439                         goto discard;
440
441                 status = ab8500_handle_hierarchical_line(ab8500,
442                                 latch_offset, latch_val);
443                 if (status < 0)
444                         return status;
445 discard:
446                 hier_val &= ~(1 << latch_bit);
447         } while (hier_val);
448
449         return 0;
450 }
451
452 static irqreturn_t ab8500_hierarchical_irq(int irq, void *dev)
453 {
454         struct ab8500 *ab8500 = dev;
455         u8 i;
456
457         dev_vdbg(ab8500->dev, "interrupt\n");
458
459         /*  Hierarchical interrupt version */
460         for (i = 0; i < AB8500_IT_LATCHHIER_NUM; i++) {
461                 int status;
462                 u8 hier_val;
463
464                 status = get_register_interruptible(ab8500, AB8500_INTERRUPT,
465                         AB8500_IT_LATCHHIER1_REG + i, &hier_val);
466                 if (status < 0 || hier_val == 0)
467                         continue;
468
469                 status = ab8500_handle_hierarchical_latch(ab8500, i, hier_val);
470                 if (status < 0)
471                         break;
472         }
473         return IRQ_HANDLED;
474 }
475
476 /**
477  * ab8500_irq_get_virq(): Map an interrupt on a chip to a virtual IRQ
478  *
479  * @ab8500: ab8500_irq controller to operate on.
480  * @irq: index of the interrupt requested in the chip IRQs
481  *
482  * Useful for drivers to request their own IRQs.
483  */
484 static int ab8500_irq_get_virq(struct ab8500 *ab8500, int irq)
485 {
486         if (!ab8500)
487                 return -EINVAL;
488
489         return irq_create_mapping(ab8500->domain, irq);
490 }
491
492 static irqreturn_t ab8500_irq(int irq, void *dev)
493 {
494         struct ab8500 *ab8500 = dev;
495         int i;
496
497         dev_vdbg(ab8500->dev, "interrupt\n");
498
499         atomic_inc(&ab8500->transfer_ongoing);
500
501         for (i = 0; i < ab8500->mask_size; i++) {
502                 int regoffset = ab8500->irq_reg_offset[i];
503                 int status;
504                 u8 value;
505
506                 /*
507                  * Interrupt register 12 doesn't exist prior to AB8500 version
508                  * 2.0
509                  */
510                 if (regoffset == 11 && is_ab8500_1p1_or_earlier(ab8500))
511                         continue;
512
513                 status = get_register_interruptible(ab8500, AB8500_INTERRUPT,
514                         AB8500_IT_LATCH1_REG + regoffset, &value);
515                 if (status < 0 || value == 0)
516                         continue;
517
518                 do {
519                         int bit = __ffs(value);
520                         int line = i * 8 + bit;
521                         int virq = ab8500_irq_get_virq(ab8500, line);
522
523                         handle_nested_irq(virq);
524                         value &= ~(1 << bit);
525
526                 } while (value);
527         }
528         atomic_dec(&ab8500->transfer_ongoing);
529         return IRQ_HANDLED;
530 }
531
532 static int ab8500_irq_map(struct irq_domain *d, unsigned int virq,
533                                 irq_hw_number_t hwirq)
534 {
535         struct ab8500 *ab8500 = d->host_data;
536
537         if (!ab8500)
538                 return -EINVAL;
539
540         irq_set_chip_data(virq, ab8500);
541         irq_set_chip_and_handler(virq, &ab8500_irq_chip,
542                                 handle_simple_irq);
543         irq_set_nested_thread(virq, 1);
544 #ifdef CONFIG_ARM
545         set_irq_flags(virq, IRQF_VALID);
546 #else
547         irq_set_noprobe(virq);
548 #endif
549
550         return 0;
551 }
552
553 static struct irq_domain_ops ab8500_irq_ops = {
554         .map    = ab8500_irq_map,
555         .xlate  = irq_domain_xlate_twocell,
556 };
557
558 static int ab8500_irq_init(struct ab8500 *ab8500, struct device_node *np)
559 {
560         int num_irqs;
561
562         if (is_ab9540(ab8500))
563                 num_irqs = AB9540_NR_IRQS;
564         else if (is_ab8505(ab8500))
565                 num_irqs = AB8505_NR_IRQS;
566         else
567                 num_irqs = AB8500_NR_IRQS;
568
569         /* If ->irq_base is zero this will give a linear mapping */
570         ab8500->domain = irq_domain_add_simple(NULL,
571                         num_irqs, ab8500->irq_base,
572                         &ab8500_irq_ops, ab8500);
573
574         if (!ab8500->domain) {
575                 dev_err(ab8500->dev, "Failed to create irqdomain\n");
576                 return -ENOSYS;
577         }
578
579         return 0;
580 }
581
582 int ab8500_suspend(struct ab8500 *ab8500)
583 {
584         if (atomic_read(&ab8500->transfer_ongoing))
585                 return -EINVAL;
586         else
587                 return 0;
588 }
589
590 static struct resource ab8500_gpadc_resources[] = {
591         {
592                 .name   = "HW_CONV_END",
593                 .start  = AB8500_INT_GP_HW_ADC_CONV_END,
594                 .end    = AB8500_INT_GP_HW_ADC_CONV_END,
595                 .flags  = IORESOURCE_IRQ,
596         },
597         {
598                 .name   = "SW_CONV_END",
599                 .start  = AB8500_INT_GP_SW_ADC_CONV_END,
600                 .end    = AB8500_INT_GP_SW_ADC_CONV_END,
601                 .flags  = IORESOURCE_IRQ,
602         },
603 };
604
605 static struct resource ab8500_rtc_resources[] = {
606         {
607                 .name   = "60S",
608                 .start  = AB8500_INT_RTC_60S,
609                 .end    = AB8500_INT_RTC_60S,
610                 .flags  = IORESOURCE_IRQ,
611         },
612         {
613                 .name   = "ALARM",
614                 .start  = AB8500_INT_RTC_ALARM,
615                 .end    = AB8500_INT_RTC_ALARM,
616                 .flags  = IORESOURCE_IRQ,
617         },
618 };
619
620 static struct resource ab8500_poweronkey_db_resources[] = {
621         {
622                 .name   = "ONKEY_DBF",
623                 .start  = AB8500_INT_PON_KEY1DB_F,
624                 .end    = AB8500_INT_PON_KEY1DB_F,
625                 .flags  = IORESOURCE_IRQ,
626         },
627         {
628                 .name   = "ONKEY_DBR",
629                 .start  = AB8500_INT_PON_KEY1DB_R,
630                 .end    = AB8500_INT_PON_KEY1DB_R,
631                 .flags  = IORESOURCE_IRQ,
632         },
633 };
634
635 static struct resource ab8500_av_acc_detect_resources[] = {
636         {
637                .name = "ACC_DETECT_1DB_F",
638                .start = AB8500_INT_ACC_DETECT_1DB_F,
639                .end = AB8500_INT_ACC_DETECT_1DB_F,
640                .flags = IORESOURCE_IRQ,
641         },
642         {
643                .name = "ACC_DETECT_1DB_R",
644                .start = AB8500_INT_ACC_DETECT_1DB_R,
645                .end = AB8500_INT_ACC_DETECT_1DB_R,
646                .flags = IORESOURCE_IRQ,
647         },
648         {
649                .name = "ACC_DETECT_21DB_F",
650                .start = AB8500_INT_ACC_DETECT_21DB_F,
651                .end = AB8500_INT_ACC_DETECT_21DB_F,
652                .flags = IORESOURCE_IRQ,
653         },
654         {
655                .name = "ACC_DETECT_21DB_R",
656                .start = AB8500_INT_ACC_DETECT_21DB_R,
657                .end = AB8500_INT_ACC_DETECT_21DB_R,
658                .flags = IORESOURCE_IRQ,
659         },
660         {
661                .name = "ACC_DETECT_22DB_F",
662                .start = AB8500_INT_ACC_DETECT_22DB_F,
663                .end = AB8500_INT_ACC_DETECT_22DB_F,
664                .flags = IORESOURCE_IRQ,
665         },
666         {
667                .name = "ACC_DETECT_22DB_R",
668                .start = AB8500_INT_ACC_DETECT_22DB_R,
669                .end = AB8500_INT_ACC_DETECT_22DB_R,
670                .flags = IORESOURCE_IRQ,
671         },
672 };
673
674 static struct resource ab8500_charger_resources[] = {
675         {
676                 .name = "MAIN_CH_UNPLUG_DET",
677                 .start = AB8500_INT_MAIN_CH_UNPLUG_DET,
678                 .end = AB8500_INT_MAIN_CH_UNPLUG_DET,
679                 .flags = IORESOURCE_IRQ,
680         },
681         {
682                 .name = "MAIN_CHARGE_PLUG_DET",
683                 .start = AB8500_INT_MAIN_CH_PLUG_DET,
684                 .end = AB8500_INT_MAIN_CH_PLUG_DET,
685                 .flags = IORESOURCE_IRQ,
686         },
687         {
688                 .name = "VBUS_DET_R",
689                 .start = AB8500_INT_VBUS_DET_R,
690                 .end = AB8500_INT_VBUS_DET_R,
691                 .flags = IORESOURCE_IRQ,
692         },
693         {
694                 .name = "VBUS_DET_F",
695                 .start = AB8500_INT_VBUS_DET_F,
696                 .end = AB8500_INT_VBUS_DET_F,
697                 .flags = IORESOURCE_IRQ,
698         },
699         {
700                 .name = "USB_LINK_STATUS",
701                 .start = AB8500_INT_USB_LINK_STATUS,
702                 .end = AB8500_INT_USB_LINK_STATUS,
703                 .flags = IORESOURCE_IRQ,
704         },
705         {
706                 .name = "VBUS_OVV",
707                 .start = AB8500_INT_VBUS_OVV,
708                 .end = AB8500_INT_VBUS_OVV,
709                 .flags = IORESOURCE_IRQ,
710         },
711         {
712                 .name = "USB_CH_TH_PROT_R",
713                 .start = AB8500_INT_USB_CH_TH_PROT_R,
714                 .end = AB8500_INT_USB_CH_TH_PROT_R,
715                 .flags = IORESOURCE_IRQ,
716         },
717         {
718                 .name = "USB_CH_TH_PROT_F",
719                 .start = AB8500_INT_USB_CH_TH_PROT_F,
720                 .end = AB8500_INT_USB_CH_TH_PROT_F,
721                 .flags = IORESOURCE_IRQ,
722         },
723         {
724                 .name = "MAIN_EXT_CH_NOT_OK",
725                 .start = AB8500_INT_MAIN_EXT_CH_NOT_OK,
726                 .end = AB8500_INT_MAIN_EXT_CH_NOT_OK,
727                 .flags = IORESOURCE_IRQ,
728         },
729         {
730                 .name = "MAIN_CH_TH_PROT_R",
731                 .start = AB8500_INT_MAIN_CH_TH_PROT_R,
732                 .end = AB8500_INT_MAIN_CH_TH_PROT_R,
733                 .flags = IORESOURCE_IRQ,
734         },
735         {
736                 .name = "MAIN_CH_TH_PROT_F",
737                 .start = AB8500_INT_MAIN_CH_TH_PROT_F,
738                 .end = AB8500_INT_MAIN_CH_TH_PROT_F,
739                 .flags = IORESOURCE_IRQ,
740         },
741         {
742                 .name = "USB_CHARGER_NOT_OKR",
743                 .start = AB8500_INT_USB_CHARGER_NOT_OKR,
744                 .end = AB8500_INT_USB_CHARGER_NOT_OKR,
745                 .flags = IORESOURCE_IRQ,
746         },
747         {
748                 .name = "CH_WD_EXP",
749                 .start = AB8500_INT_CH_WD_EXP,
750                 .end = AB8500_INT_CH_WD_EXP,
751                 .flags = IORESOURCE_IRQ,
752         },
753         {
754                 .name = "VBUS_CH_DROP_END",
755                 .start = AB8500_INT_VBUS_CH_DROP_END,
756                 .end = AB8500_INT_VBUS_CH_DROP_END,
757                 .flags = IORESOURCE_IRQ,
758         },
759 };
760
761 static struct resource ab8500_btemp_resources[] = {
762         {
763                 .name = "BAT_CTRL_INDB",
764                 .start = AB8500_INT_BAT_CTRL_INDB,
765                 .end = AB8500_INT_BAT_CTRL_INDB,
766                 .flags = IORESOURCE_IRQ,
767         },
768         {
769                 .name = "BTEMP_LOW",
770                 .start = AB8500_INT_BTEMP_LOW,
771                 .end = AB8500_INT_BTEMP_LOW,
772                 .flags = IORESOURCE_IRQ,
773         },
774         {
775                 .name = "BTEMP_HIGH",
776                 .start = AB8500_INT_BTEMP_HIGH,
777                 .end = AB8500_INT_BTEMP_HIGH,
778                 .flags = IORESOURCE_IRQ,
779         },
780         {
781                 .name = "BTEMP_LOW_MEDIUM",
782                 .start = AB8500_INT_BTEMP_LOW_MEDIUM,
783                 .end = AB8500_INT_BTEMP_LOW_MEDIUM,
784                 .flags = IORESOURCE_IRQ,
785         },
786         {
787                 .name = "BTEMP_MEDIUM_HIGH",
788                 .start = AB8500_INT_BTEMP_MEDIUM_HIGH,
789                 .end = AB8500_INT_BTEMP_MEDIUM_HIGH,
790                 .flags = IORESOURCE_IRQ,
791         },
792 };
793
794 static struct resource ab8500_fg_resources[] = {
795         {
796                 .name = "NCONV_ACCU",
797                 .start = AB8500_INT_CCN_CONV_ACC,
798                 .end = AB8500_INT_CCN_CONV_ACC,
799                 .flags = IORESOURCE_IRQ,
800         },
801         {
802                 .name = "BATT_OVV",
803                 .start = AB8500_INT_BATT_OVV,
804                 .end = AB8500_INT_BATT_OVV,
805                 .flags = IORESOURCE_IRQ,
806         },
807         {
808                 .name = "LOW_BAT_F",
809                 .start = AB8500_INT_LOW_BAT_F,
810                 .end = AB8500_INT_LOW_BAT_F,
811                 .flags = IORESOURCE_IRQ,
812         },
813         {
814                 .name = "LOW_BAT_R",
815                 .start = AB8500_INT_LOW_BAT_R,
816                 .end = AB8500_INT_LOW_BAT_R,
817                 .flags = IORESOURCE_IRQ,
818         },
819         {
820                 .name = "CC_INT_CALIB",
821                 .start = AB8500_INT_CC_INT_CALIB,
822                 .end = AB8500_INT_CC_INT_CALIB,
823                 .flags = IORESOURCE_IRQ,
824         },
825         {
826                 .name = "CCEOC",
827                 .start = AB8500_INT_CCEOC,
828                 .end = AB8500_INT_CCEOC,
829                 .flags = IORESOURCE_IRQ,
830         },
831 };
832
833 static struct resource ab8500_chargalg_resources[] = {};
834
835 #ifdef CONFIG_DEBUG_FS
836 static struct resource ab8500_debug_resources[] = {
837         {
838                 .name   = "IRQ_FIRST",
839                 .start  = AB8500_INT_MAIN_EXT_CH_NOT_OK,
840                 .end    = AB8500_INT_MAIN_EXT_CH_NOT_OK,
841                 .flags  = IORESOURCE_IRQ,
842         },
843         {
844                 .name   = "IRQ_LAST",
845                 .start  = AB8500_INT_XTAL32K_KO,
846                 .end    = AB8500_INT_XTAL32K_KO,
847                 .flags  = IORESOURCE_IRQ,
848         },
849 };
850 #endif
851
852 static struct resource ab8500_usb_resources[] = {
853         {
854                 .name = "ID_WAKEUP_R",
855                 .start = AB8500_INT_ID_WAKEUP_R,
856                 .end = AB8500_INT_ID_WAKEUP_R,
857                 .flags = IORESOURCE_IRQ,
858         },
859         {
860                 .name = "ID_WAKEUP_F",
861                 .start = AB8500_INT_ID_WAKEUP_F,
862                 .end = AB8500_INT_ID_WAKEUP_F,
863                 .flags = IORESOURCE_IRQ,
864         },
865         {
866                 .name = "VBUS_DET_F",
867                 .start = AB8500_INT_VBUS_DET_F,
868                 .end = AB8500_INT_VBUS_DET_F,
869                 .flags = IORESOURCE_IRQ,
870         },
871         {
872                 .name = "VBUS_DET_R",
873                 .start = AB8500_INT_VBUS_DET_R,
874                 .end = AB8500_INT_VBUS_DET_R,
875                 .flags = IORESOURCE_IRQ,
876         },
877         {
878                 .name = "USB_LINK_STATUS",
879                 .start = AB8500_INT_USB_LINK_STATUS,
880                 .end = AB8500_INT_USB_LINK_STATUS,
881                 .flags = IORESOURCE_IRQ,
882         },
883         {
884                 .name = "USB_ADP_PROBE_PLUG",
885                 .start = AB8500_INT_ADP_PROBE_PLUG,
886                 .end = AB8500_INT_ADP_PROBE_PLUG,
887                 .flags = IORESOURCE_IRQ,
888         },
889         {
890                 .name = "USB_ADP_PROBE_UNPLUG",
891                 .start = AB8500_INT_ADP_PROBE_UNPLUG,
892                 .end = AB8500_INT_ADP_PROBE_UNPLUG,
893                 .flags = IORESOURCE_IRQ,
894         },
895 };
896
897 static struct resource ab8505_iddet_resources[] = {
898         {
899                 .name  = "KeyDeglitch",
900                 .start = AB8505_INT_KEYDEGLITCH,
901                 .end   = AB8505_INT_KEYDEGLITCH,
902                 .flags = IORESOURCE_IRQ,
903         },
904         {
905                 .name  = "KP",
906                 .start = AB8505_INT_KP,
907                 .end   = AB8505_INT_KP,
908                 .flags = IORESOURCE_IRQ,
909         },
910         {
911                 .name  = "IKP",
912                 .start = AB8505_INT_IKP,
913                 .end   = AB8505_INT_IKP,
914                 .flags = IORESOURCE_IRQ,
915         },
916         {
917                 .name  = "IKR",
918                 .start = AB8505_INT_IKR,
919                 .end   = AB8505_INT_IKR,
920                 .flags = IORESOURCE_IRQ,
921         },
922         {
923                 .name  = "KeyStuck",
924                 .start = AB8505_INT_KEYSTUCK,
925                 .end   = AB8505_INT_KEYSTUCK,
926                 .flags = IORESOURCE_IRQ,
927         },
928 };
929
930 static struct resource ab8500_temp_resources[] = {
931         {
932                 .name  = "AB8500_TEMP_WARM",
933                 .start = AB8500_INT_TEMP_WARM,
934                 .end   = AB8500_INT_TEMP_WARM,
935                 .flags = IORESOURCE_IRQ,
936         },
937 };
938
939 static struct mfd_cell abx500_common_devs[] = {
940 #ifdef CONFIG_DEBUG_FS
941         {
942                 .name = "ab8500-debug",
943                 .of_compatible = "stericsson,ab8500-debug",
944                 .num_resources = ARRAY_SIZE(ab8500_debug_resources),
945                 .resources = ab8500_debug_resources,
946         },
947 #endif
948         {
949                 .name = "ab8500-sysctrl",
950                 .of_compatible = "stericsson,ab8500-sysctrl",
951         },
952         {
953                 .name = "ab8500-regulator",
954                 .of_compatible = "stericsson,ab8500-regulator",
955         },
956         {
957                 .name = "abx500-clk",
958                 .of_compatible = "stericsson,abx500-clk",
959         },
960         {
961                 .name = "ab8500-gpadc",
962                 .of_compatible = "stericsson,ab8500-gpadc",
963                 .num_resources = ARRAY_SIZE(ab8500_gpadc_resources),
964                 .resources = ab8500_gpadc_resources,
965         },
966         {
967                 .name = "ab8500-rtc",
968                 .of_compatible = "stericsson,ab8500-rtc",
969                 .num_resources = ARRAY_SIZE(ab8500_rtc_resources),
970                 .resources = ab8500_rtc_resources,
971         },
972         {
973                 .name = "ab8500-acc-det",
974                 .of_compatible = "stericsson,ab8500-acc-det",
975                 .num_resources = ARRAY_SIZE(ab8500_av_acc_detect_resources),
976                 .resources = ab8500_av_acc_detect_resources,
977         },
978         {
979                 .name = "ab8500-poweron-key",
980                 .of_compatible = "stericsson,ab8500-poweron-key",
981                 .num_resources = ARRAY_SIZE(ab8500_poweronkey_db_resources),
982                 .resources = ab8500_poweronkey_db_resources,
983         },
984         {
985                 .name = "ab8500-pwm",
986                 .of_compatible = "stericsson,ab8500-pwm",
987                 .id = 1,
988         },
989         {
990                 .name = "ab8500-pwm",
991                 .of_compatible = "stericsson,ab8500-pwm",
992                 .id = 2,
993         },
994         {
995                 .name = "ab8500-pwm",
996                 .of_compatible = "stericsson,ab8500-pwm",
997                 .id = 3,
998         },
999         {
1000                 .name = "ab8500-leds",
1001                 .of_compatible = "stericsson,ab8500-leds",
1002         },
1003         {
1004                 .name = "ab8500-denc",
1005                 .of_compatible = "stericsson,ab8500-denc",
1006         },
1007         {
1008                 .name = "ab8500-temp",
1009                 .of_compatible = "stericsson,ab8500-temp",
1010                 .num_resources = ARRAY_SIZE(ab8500_temp_resources),
1011                 .resources = ab8500_temp_resources,
1012         },
1013 };
1014
1015 static struct mfd_cell ab8500_bm_devs[] = {
1016         {
1017                 .name = "ab8500-charger",
1018                 .of_compatible = "stericsson,ab8500-charger",
1019                 .num_resources = ARRAY_SIZE(ab8500_charger_resources),
1020                 .resources = ab8500_charger_resources,
1021                 .platform_data = &ab8500_bm_data,
1022                 .pdata_size = sizeof(ab8500_bm_data),
1023         },
1024         {
1025                 .name = "ab8500-btemp",
1026                 .of_compatible = "stericsson,ab8500-btemp",
1027                 .num_resources = ARRAY_SIZE(ab8500_btemp_resources),
1028                 .resources = ab8500_btemp_resources,
1029                 .platform_data = &ab8500_bm_data,
1030                 .pdata_size = sizeof(ab8500_bm_data),
1031         },
1032         {
1033                 .name = "ab8500-fg",
1034                 .of_compatible = "stericsson,ab8500-fg",
1035                 .num_resources = ARRAY_SIZE(ab8500_fg_resources),
1036                 .resources = ab8500_fg_resources,
1037                 .platform_data = &ab8500_bm_data,
1038                 .pdata_size = sizeof(ab8500_bm_data),
1039         },
1040         {
1041                 .name = "ab8500-chargalg",
1042                 .of_compatible = "stericsson,ab8500-chargalg",
1043                 .num_resources = ARRAY_SIZE(ab8500_chargalg_resources),
1044                 .resources = ab8500_chargalg_resources,
1045                 .platform_data = &ab8500_bm_data,
1046                 .pdata_size = sizeof(ab8500_bm_data),
1047         },
1048 };
1049
1050 static struct mfd_cell ab8500_devs[] = {
1051         {
1052                 .name = "ab8500-gpio",
1053                 .of_compatible = "stericsson,ab8500-gpio",
1054         },
1055         {
1056                 .name = "ab8500-usb",
1057                 .of_compatible = "stericsson,ab8500-usb",
1058                 .num_resources = ARRAY_SIZE(ab8500_usb_resources),
1059                 .resources = ab8500_usb_resources,
1060         },
1061         {
1062                 .name = "ab8500-codec",
1063                 .of_compatible = "stericsson,ab8500-codec",
1064         },
1065 };
1066
1067 static struct mfd_cell ab9540_devs[] = {
1068         {
1069                 .name = "ab8500-gpio",
1070         },
1071         {
1072                 .name = "ab9540-usb",
1073                 .num_resources = ARRAY_SIZE(ab8500_usb_resources),
1074                 .resources = ab8500_usb_resources,
1075         },
1076         {
1077                 .name = "ab9540-codec",
1078         },
1079 };
1080
1081 /* Device list common to ab9540 and ab8505 */
1082 static struct mfd_cell ab9540_ab8505_devs[] = {
1083         {
1084                 .name = "ab-iddet",
1085                 .num_resources = ARRAY_SIZE(ab8505_iddet_resources),
1086                 .resources = ab8505_iddet_resources,
1087         },
1088 };
1089
1090 static ssize_t show_chip_id(struct device *dev,
1091                                 struct device_attribute *attr, char *buf)
1092 {
1093         struct ab8500 *ab8500;
1094
1095         ab8500 = dev_get_drvdata(dev);
1096         return sprintf(buf, "%#x\n", ab8500 ? ab8500->chip_id : -EINVAL);
1097 }
1098
1099 /*
1100  * ab8500 has switched off due to (SWITCH_OFF_STATUS):
1101  * 0x01 Swoff bit programming
1102  * 0x02 Thermal protection activation
1103  * 0x04 Vbat lower then BattOk falling threshold
1104  * 0x08 Watchdog expired
1105  * 0x10 Non presence of 32kHz clock
1106  * 0x20 Battery level lower than power on reset threshold
1107  * 0x40 Power on key 1 pressed longer than 10 seconds
1108  * 0x80 DB8500 thermal shutdown
1109  */
1110 static ssize_t show_switch_off_status(struct device *dev,
1111                                 struct device_attribute *attr, char *buf)
1112 {
1113         int ret;
1114         u8 value;
1115         struct ab8500 *ab8500;
1116
1117         ab8500 = dev_get_drvdata(dev);
1118         ret = get_register_interruptible(ab8500, AB8500_RTC,
1119                 AB8500_SWITCH_OFF_STATUS, &value);
1120         if (ret < 0)
1121                 return ret;
1122         return sprintf(buf, "%#x\n", value);
1123 }
1124
1125 /*
1126  * ab8500 has turned on due to (TURN_ON_STATUS):
1127  * 0x01 PORnVbat
1128  * 0x02 PonKey1dbF
1129  * 0x04 PonKey2dbF
1130  * 0x08 RTCAlarm
1131  * 0x10 MainChDet
1132  * 0x20 VbusDet
1133  * 0x40 UsbIDDetect
1134  * 0x80 Reserved
1135  */
1136 static ssize_t show_turn_on_status(struct device *dev,
1137                                 struct device_attribute *attr, char *buf)
1138 {
1139         int ret;
1140         u8 value;
1141         struct ab8500 *ab8500;
1142
1143         ab8500 = dev_get_drvdata(dev);
1144         ret = get_register_interruptible(ab8500, AB8500_SYS_CTRL1_BLOCK,
1145                 AB8500_TURN_ON_STATUS, &value);
1146         if (ret < 0)
1147                 return ret;
1148         return sprintf(buf, "%#x\n", value);
1149 }
1150
1151 static ssize_t show_ab9540_dbbrstn(struct device *dev,
1152                                 struct device_attribute *attr, char *buf)
1153 {
1154         struct ab8500 *ab8500;
1155         int ret;
1156         u8 value;
1157
1158         ab8500 = dev_get_drvdata(dev);
1159
1160         ret = get_register_interruptible(ab8500, AB8500_REGU_CTRL2,
1161                 AB9540_MODEM_CTRL2_REG, &value);
1162         if (ret < 0)
1163                 return ret;
1164
1165         return sprintf(buf, "%d\n",
1166                         (value & AB9540_MODEM_CTRL2_SWDBBRSTN_BIT) ? 1 : 0);
1167 }
1168
1169 static ssize_t store_ab9540_dbbrstn(struct device *dev,
1170         struct device_attribute *attr, const char *buf, size_t count)
1171 {
1172         struct ab8500 *ab8500;
1173         int ret = count;
1174         int err;
1175         u8 bitvalues;
1176
1177         ab8500 = dev_get_drvdata(dev);
1178
1179         if (count > 0) {
1180                 switch (buf[0]) {
1181                 case '0':
1182                         bitvalues = 0;
1183                         break;
1184                 case '1':
1185                         bitvalues = AB9540_MODEM_CTRL2_SWDBBRSTN_BIT;
1186                         break;
1187                 default:
1188                         goto exit;
1189                 }
1190
1191                 err = mask_and_set_register_interruptible(ab8500,
1192                         AB8500_REGU_CTRL2, AB9540_MODEM_CTRL2_REG,
1193                         AB9540_MODEM_CTRL2_SWDBBRSTN_BIT, bitvalues);
1194                 if (err)
1195                         dev_info(ab8500->dev,
1196                                 "Failed to set DBBRSTN %c, err %#x\n",
1197                                 buf[0], err);
1198         }
1199
1200 exit:
1201         return ret;
1202 }
1203
1204 static DEVICE_ATTR(chip_id, S_IRUGO, show_chip_id, NULL);
1205 static DEVICE_ATTR(switch_off_status, S_IRUGO, show_switch_off_status, NULL);
1206 static DEVICE_ATTR(turn_on_status, S_IRUGO, show_turn_on_status, NULL);
1207 static DEVICE_ATTR(dbbrstn, S_IRUGO | S_IWUSR,
1208                         show_ab9540_dbbrstn, store_ab9540_dbbrstn);
1209
1210 static struct attribute *ab8500_sysfs_entries[] = {
1211         &dev_attr_chip_id.attr,
1212         &dev_attr_switch_off_status.attr,
1213         &dev_attr_turn_on_status.attr,
1214         NULL,
1215 };
1216
1217 static struct attribute *ab9540_sysfs_entries[] = {
1218         &dev_attr_chip_id.attr,
1219         &dev_attr_switch_off_status.attr,
1220         &dev_attr_turn_on_status.attr,
1221         &dev_attr_dbbrstn.attr,
1222         NULL,
1223 };
1224
1225 static struct attribute_group ab8500_attr_group = {
1226         .attrs  = ab8500_sysfs_entries,
1227 };
1228
1229 static struct attribute_group ab9540_attr_group = {
1230         .attrs  = ab9540_sysfs_entries,
1231 };
1232
1233 static int ab8500_probe(struct platform_device *pdev)
1234 {
1235         static char *switch_off_status[] = {
1236                 "Swoff bit programming",
1237                 "Thermal protection activation",
1238                 "Vbat lower then BattOk falling threshold",
1239                 "Watchdog expired",
1240                 "Non presence of 32kHz clock",
1241                 "Battery level lower than power on reset threshold",
1242                 "Power on key 1 pressed longer than 10 seconds",
1243                 "DB8500 thermal shutdown"};
1244         struct ab8500_platform_data *plat = dev_get_platdata(&pdev->dev);
1245         const struct platform_device_id *platid = platform_get_device_id(pdev);
1246         enum ab8500_version version = AB8500_VERSION_UNDEFINED;
1247         struct device_node *np = pdev->dev.of_node;
1248         struct ab8500 *ab8500;
1249         struct resource *resource;
1250         int ret;
1251         int i;
1252         u8 value;
1253
1254         ab8500 = devm_kzalloc(&pdev->dev, sizeof *ab8500, GFP_KERNEL);
1255         if (!ab8500)
1256                 return -ENOMEM;
1257
1258         if (plat)
1259                 ab8500->irq_base = plat->irq_base;
1260
1261         ab8500->dev = &pdev->dev;
1262
1263         resource = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1264         if (!resource)
1265                 return -ENODEV;
1266
1267         ab8500->irq = resource->start;
1268
1269         ab8500->read = ab8500_prcmu_read;
1270         ab8500->write = ab8500_prcmu_write;
1271         ab8500->write_masked = ab8500_prcmu_write_masked;
1272
1273         mutex_init(&ab8500->lock);
1274         mutex_init(&ab8500->irq_lock);
1275         atomic_set(&ab8500->transfer_ongoing, 0);
1276
1277         platform_set_drvdata(pdev, ab8500);
1278
1279         if (platid)
1280                 version = platid->driver_data;
1281
1282         if (version != AB8500_VERSION_UNDEFINED)
1283                 ab8500->version = version;
1284         else {
1285                 ret = get_register_interruptible(ab8500, AB8500_MISC,
1286                         AB8500_IC_NAME_REG, &value);
1287                 if (ret < 0)
1288                         return ret;
1289
1290                 ab8500->version = value;
1291         }
1292
1293         ret = get_register_interruptible(ab8500, AB8500_MISC,
1294                 AB8500_REV_REG, &value);
1295         if (ret < 0)
1296                 return ret;
1297
1298         ab8500->chip_id = value;
1299
1300         dev_info(ab8500->dev, "detected chip, %s rev. %1x.%1x\n",
1301                         ab8500_version_str[ab8500->version],
1302                         ab8500->chip_id >> 4,
1303                         ab8500->chip_id & 0x0F);
1304
1305         /* Configure AB8500 or AB9540 IRQ */
1306         if (is_ab9540(ab8500) || is_ab8505(ab8500)) {
1307                 ab8500->mask_size = AB9540_NUM_IRQ_REGS;
1308                 ab8500->irq_reg_offset = ab9540_irq_regoffset;
1309         } else {
1310                 ab8500->mask_size = AB8500_NUM_IRQ_REGS;
1311                 ab8500->irq_reg_offset = ab8500_irq_regoffset;
1312         }
1313         ab8500->mask = devm_kzalloc(&pdev->dev, ab8500->mask_size, GFP_KERNEL);
1314         if (!ab8500->mask)
1315                 return -ENOMEM;
1316         ab8500->oldmask = devm_kzalloc(&pdev->dev, ab8500->mask_size, GFP_KERNEL);
1317         if (!ab8500->oldmask)
1318                 return -ENOMEM;
1319
1320         /*
1321          * ab8500 has switched off due to (SWITCH_OFF_STATUS):
1322          * 0x01 Swoff bit programming
1323          * 0x02 Thermal protection activation
1324          * 0x04 Vbat lower then BattOk falling threshold
1325          * 0x08 Watchdog expired
1326          * 0x10 Non presence of 32kHz clock
1327          * 0x20 Battery level lower than power on reset threshold
1328          * 0x40 Power on key 1 pressed longer than 10 seconds
1329          * 0x80 DB8500 thermal shutdown
1330          */
1331
1332         ret = get_register_interruptible(ab8500, AB8500_RTC,
1333                 AB8500_SWITCH_OFF_STATUS, &value);
1334         if (ret < 0)
1335                 return ret;
1336         dev_info(ab8500->dev, "switch off cause(s) (%#x): ", value);
1337
1338         if (value) {
1339                 for (i = 0; i < ARRAY_SIZE(switch_off_status); i++) {
1340                         if (value & 1)
1341                                 printk(KERN_CONT " \"%s\"",
1342                                        switch_off_status[i]);
1343                         value = value >> 1;
1344
1345                 }
1346                 printk(KERN_CONT "\n");
1347         } else {
1348                 printk(KERN_CONT " None\n");
1349         }
1350
1351         if (plat && plat->init)
1352                 plat->init(ab8500);
1353
1354         /* Clear and mask all interrupts */
1355         for (i = 0; i < ab8500->mask_size; i++) {
1356                 /*
1357                  * Interrupt register 12 doesn't exist prior to AB8500 version
1358                  * 2.0
1359                  */
1360                 if (ab8500->irq_reg_offset[i] == 11 &&
1361                                 is_ab8500_1p1_or_earlier(ab8500))
1362                         continue;
1363
1364                 get_register_interruptible(ab8500, AB8500_INTERRUPT,
1365                         AB8500_IT_LATCH1_REG + ab8500->irq_reg_offset[i],
1366                         &value);
1367                 set_register_interruptible(ab8500, AB8500_INTERRUPT,
1368                         AB8500_IT_MASK1_REG + ab8500->irq_reg_offset[i], 0xff);
1369         }
1370
1371         ret = abx500_register_ops(ab8500->dev, &ab8500_ops);
1372         if (ret)
1373                 return ret;
1374
1375         for (i = 0; i < ab8500->mask_size; i++)
1376                 ab8500->mask[i] = ab8500->oldmask[i] = 0xff;
1377
1378         ret = ab8500_irq_init(ab8500, np);
1379         if (ret)
1380                 return ret;
1381
1382         /*  Activate this feature only in ab9540 */
1383         /*  till tests are done on ab8500 1p2 or later*/
1384         if (is_ab9540(ab8500)) {
1385                 ret = devm_request_threaded_irq(&pdev->dev, ab8500->irq, NULL,
1386                                                 ab8500_hierarchical_irq,
1387                                                 IRQF_ONESHOT | IRQF_NO_SUSPEND,
1388                                                 "ab8500", ab8500);
1389         }
1390         else {
1391                 ret = devm_request_threaded_irq(&pdev->dev, ab8500->irq, NULL,
1392                                                 ab8500_irq,
1393                                                 IRQF_ONESHOT | IRQF_NO_SUSPEND,
1394                                                 "ab8500", ab8500);
1395                 if (ret)
1396                         return ret;
1397         }
1398
1399         ret = mfd_add_devices(ab8500->dev, 0, abx500_common_devs,
1400                         ARRAY_SIZE(abx500_common_devs), NULL,
1401                         ab8500->irq_base, ab8500->domain);
1402         if (ret)
1403                 return ret;
1404
1405         if (is_ab9540(ab8500))
1406                 ret = mfd_add_devices(ab8500->dev, 0, ab9540_devs,
1407                                 ARRAY_SIZE(ab9540_devs), NULL,
1408                                 ab8500->irq_base, ab8500->domain);
1409         else
1410                 ret = mfd_add_devices(ab8500->dev, 0, ab8500_devs,
1411                                 ARRAY_SIZE(ab8500_devs), NULL,
1412                                 ab8500->irq_base, ab8500->domain);
1413         if (ret)
1414                 return ret;
1415
1416         if (is_ab9540(ab8500) || is_ab8505(ab8500))
1417                 ret = mfd_add_devices(ab8500->dev, 0, ab9540_ab8505_devs,
1418                                 ARRAY_SIZE(ab9540_ab8505_devs), NULL,
1419                                 ab8500->irq_base, ab8500->domain);
1420         if (ret)
1421                 return ret;
1422
1423         if (!no_bm) {
1424                 /* Add battery management devices */
1425                 ret = mfd_add_devices(ab8500->dev, 0, ab8500_bm_devs,
1426                                       ARRAY_SIZE(ab8500_bm_devs), NULL,
1427                                       ab8500->irq_base, ab8500->domain);
1428                 if (ret)
1429                         dev_err(ab8500->dev, "error adding bm devices\n");
1430         }
1431
1432         if (is_ab9540(ab8500))
1433                 ret = sysfs_create_group(&ab8500->dev->kobj,
1434                                         &ab9540_attr_group);
1435         else
1436                 ret = sysfs_create_group(&ab8500->dev->kobj,
1437                                         &ab8500_attr_group);
1438         if (ret)
1439                 dev_err(ab8500->dev, "error creating sysfs entries\n");
1440
1441         return ret;
1442 }
1443
1444 static int ab8500_remove(struct platform_device *pdev)
1445 {
1446         struct ab8500 *ab8500 = platform_get_drvdata(pdev);
1447
1448         if (is_ab9540(ab8500))
1449                 sysfs_remove_group(&ab8500->dev->kobj, &ab9540_attr_group);
1450         else
1451                 sysfs_remove_group(&ab8500->dev->kobj, &ab8500_attr_group);
1452
1453         mfd_remove_devices(ab8500->dev);
1454
1455         return 0;
1456 }
1457
1458 static const struct platform_device_id ab8500_id[] = {
1459         { "ab8500-core", AB8500_VERSION_AB8500 },
1460         { "ab8505-i2c", AB8500_VERSION_AB8505 },
1461         { "ab9540-i2c", AB8500_VERSION_AB9540 },
1462         { "ab8540-i2c", AB8500_VERSION_AB8540 },
1463         { }
1464 };
1465
1466 static struct platform_driver ab8500_core_driver = {
1467         .driver = {
1468                 .name = "ab8500-core",
1469                 .owner = THIS_MODULE,
1470         },
1471         .probe  = ab8500_probe,
1472         .remove = ab8500_remove,
1473         .id_table = ab8500_id,
1474 };
1475
1476 static int __init ab8500_core_init(void)
1477 {
1478         return platform_driver_register(&ab8500_core_driver);
1479 }
1480
1481 static void __exit ab8500_core_exit(void)
1482 {
1483         platform_driver_unregister(&ab8500_core_driver);
1484 }
1485 core_initcall(ab8500_core_init);
1486 module_exit(ab8500_core_exit);
1487
1488 MODULE_AUTHOR("Mattias Wallin, Srinidhi Kasagar, Rabin Vincent");
1489 MODULE_DESCRIPTION("AB8500 MFD core");
1490 MODULE_LICENSE("GPL v2");