mfd: Add ab8500 version detection and enforcing
[firefly-linux-kernel-4.4.55.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/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>
22
23 /*
24  * Interrupt register offsets
25  * Bank : 0x0E
26  */
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 AB8500_IT_SOURCE19_REG          0x12
36 #define AB8500_IT_SOURCE20_REG          0x13
37 #define AB8500_IT_SOURCE21_REG          0x14
38 #define AB8500_IT_SOURCE22_REG          0x15
39 #define AB8500_IT_SOURCE23_REG          0x16
40 #define AB8500_IT_SOURCE24_REG          0x17
41
42 /*
43  * latch registers
44  */
45 #define AB8500_IT_LATCH1_REG            0x20
46 #define AB8500_IT_LATCH2_REG            0x21
47 #define AB8500_IT_LATCH3_REG            0x22
48 #define AB8500_IT_LATCH4_REG            0x23
49 #define AB8500_IT_LATCH5_REG            0x24
50 #define AB8500_IT_LATCH6_REG            0x25
51 #define AB8500_IT_LATCH7_REG            0x26
52 #define AB8500_IT_LATCH8_REG            0x27
53 #define AB8500_IT_LATCH9_REG            0x28
54 #define AB8500_IT_LATCH10_REG           0x29
55 #define AB8500_IT_LATCH12_REG           0x2B
56 #define AB8500_IT_LATCH19_REG           0x32
57 #define AB8500_IT_LATCH20_REG           0x33
58 #define AB8500_IT_LATCH21_REG           0x34
59 #define AB8500_IT_LATCH22_REG           0x35
60 #define AB8500_IT_LATCH23_REG           0x36
61 #define AB8500_IT_LATCH24_REG           0x37
62
63 /*
64  * mask registers
65  */
66
67 #define AB8500_IT_MASK1_REG             0x40
68 #define AB8500_IT_MASK2_REG             0x41
69 #define AB8500_IT_MASK3_REG             0x42
70 #define AB8500_IT_MASK4_REG             0x43
71 #define AB8500_IT_MASK5_REG             0x44
72 #define AB8500_IT_MASK6_REG             0x45
73 #define AB8500_IT_MASK7_REG             0x46
74 #define AB8500_IT_MASK8_REG             0x47
75 #define AB8500_IT_MASK9_REG             0x48
76 #define AB8500_IT_MASK10_REG            0x49
77 #define AB8500_IT_MASK11_REG            0x4A
78 #define AB8500_IT_MASK12_REG            0x4B
79 #define AB8500_IT_MASK13_REG            0x4C
80 #define AB8500_IT_MASK14_REG            0x4D
81 #define AB8500_IT_MASK15_REG            0x4E
82 #define AB8500_IT_MASK16_REG            0x4F
83 #define AB8500_IT_MASK17_REG            0x50
84 #define AB8500_IT_MASK18_REG            0x51
85 #define AB8500_IT_MASK19_REG            0x52
86 #define AB8500_IT_MASK20_REG            0x53
87 #define AB8500_IT_MASK21_REG            0x54
88 #define AB8500_IT_MASK22_REG            0x55
89 #define AB8500_IT_MASK23_REG            0x56
90 #define AB8500_IT_MASK24_REG            0x57
91
92 #define AB8500_REV_REG                  0x80
93 #define AB8500_IC_NAME_REG              0x82
94 #define AB8500_SWITCH_OFF_STATUS        0x00
95
96 #define AB8500_TURN_ON_STATUS           0x00
97
98 /*
99  * Map interrupt numbers to the LATCH and MASK register offsets, Interrupt
100  * numbers are indexed into this array with (num / 8).
101  *
102  * This is one off from the register names, i.e. AB8500_IT_MASK1_REG is at
103  * offset 0.
104  */
105 static const int ab8500_irq_regoffset[AB8500_NUM_IRQ_REGS] = {
106         0, 1, 2, 3, 4, 6, 7, 8, 9, 11, 18, 19, 20, 21,
107 };
108
109 static const char ab8500_version_str[][7] = {
110         [AB8500_VERSION_AB8500] = "AB8500",
111         [AB8500_VERSION_AB8505] = "AB8505",
112         [AB8500_VERSION_AB9540] = "AB9540",
113         [AB8500_VERSION_AB8540] = "AB8540",
114 };
115
116 static int ab8500_get_chip_id(struct device *dev)
117 {
118         struct ab8500 *ab8500;
119
120         if (!dev)
121                 return -EINVAL;
122         ab8500 = dev_get_drvdata(dev->parent);
123         return ab8500 ? (int)ab8500->chip_id : -EINVAL;
124 }
125
126 static int set_register_interruptible(struct ab8500 *ab8500, u8 bank,
127         u8 reg, u8 data)
128 {
129         int ret;
130         /*
131          * Put the u8 bank and u8 register together into a an u16.
132          * The bank on higher 8 bits and register in lower 8 bits.
133          * */
134         u16 addr = ((u16)bank) << 8 | reg;
135
136         dev_vdbg(ab8500->dev, "wr: addr %#x <= %#x\n", addr, data);
137
138         ret = mutex_lock_interruptible(&ab8500->lock);
139         if (ret)
140                 return ret;
141
142         ret = ab8500->write(ab8500, addr, data);
143         if (ret < 0)
144                 dev_err(ab8500->dev, "failed to write reg %#x: %d\n",
145                         addr, ret);
146         mutex_unlock(&ab8500->lock);
147
148         return ret;
149 }
150
151 static int ab8500_set_register(struct device *dev, u8 bank,
152         u8 reg, u8 value)
153 {
154         struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
155
156         return set_register_interruptible(ab8500, bank, reg, value);
157 }
158
159 static int get_register_interruptible(struct ab8500 *ab8500, u8 bank,
160         u8 reg, u8 *value)
161 {
162         int ret;
163         /* put the u8 bank and u8 reg together into a an u16.
164          * bank on higher 8 bits and reg in lower */
165         u16 addr = ((u16)bank) << 8 | reg;
166
167         ret = mutex_lock_interruptible(&ab8500->lock);
168         if (ret)
169                 return ret;
170
171         ret = ab8500->read(ab8500, addr);
172         if (ret < 0)
173                 dev_err(ab8500->dev, "failed to read reg %#x: %d\n",
174                         addr, ret);
175         else
176                 *value = ret;
177
178         mutex_unlock(&ab8500->lock);
179         dev_vdbg(ab8500->dev, "rd: addr %#x => data %#x\n", addr, ret);
180
181         return ret;
182 }
183
184 static int ab8500_get_register(struct device *dev, u8 bank,
185         u8 reg, u8 *value)
186 {
187         struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
188
189         return get_register_interruptible(ab8500, bank, reg, value);
190 }
191
192 static int mask_and_set_register_interruptible(struct ab8500 *ab8500, u8 bank,
193         u8 reg, u8 bitmask, u8 bitvalues)
194 {
195         int ret;
196         u8 data;
197         /* put the u8 bank and u8 reg together into a an u16.
198          * bank on higher 8 bits and reg in lower */
199         u16 addr = ((u16)bank) << 8 | reg;
200
201         ret = mutex_lock_interruptible(&ab8500->lock);
202         if (ret)
203                 return ret;
204
205         ret = ab8500->read(ab8500, addr);
206         if (ret < 0) {
207                 dev_err(ab8500->dev, "failed to read reg %#x: %d\n",
208                         addr, ret);
209                 goto out;
210         }
211
212         data = (u8)ret;
213         data = (~bitmask & data) | (bitmask & bitvalues);
214
215         ret = ab8500->write(ab8500, addr, data);
216         if (ret < 0)
217                 dev_err(ab8500->dev, "failed to write reg %#x: %d\n",
218                         addr, ret);
219
220         dev_vdbg(ab8500->dev, "mask: addr %#x => data %#x\n", addr, data);
221 out:
222         mutex_unlock(&ab8500->lock);
223         return ret;
224 }
225
226 static int ab8500_mask_and_set_register(struct device *dev,
227         u8 bank, u8 reg, u8 bitmask, u8 bitvalues)
228 {
229         struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
230
231         return mask_and_set_register_interruptible(ab8500, bank, reg,
232                 bitmask, bitvalues);
233
234 }
235
236 static struct abx500_ops ab8500_ops = {
237         .get_chip_id = ab8500_get_chip_id,
238         .get_register = ab8500_get_register,
239         .set_register = ab8500_set_register,
240         .get_register_page = NULL,
241         .set_register_page = NULL,
242         .mask_and_set_register = ab8500_mask_and_set_register,
243         .event_registers_startup_state_get = NULL,
244         .startup_irq_enabled = NULL,
245 };
246
247 static void ab8500_irq_lock(struct irq_data *data)
248 {
249         struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
250
251         mutex_lock(&ab8500->irq_lock);
252 }
253
254 static void ab8500_irq_sync_unlock(struct irq_data *data)
255 {
256         struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
257         int i;
258
259         for (i = 0; i < AB8500_NUM_IRQ_REGS; i++) {
260                 u8 old = ab8500->oldmask[i];
261                 u8 new = ab8500->mask[i];
262                 int reg;
263
264                 if (new == old)
265                         continue;
266
267                 /*
268                  * Interrupt register 12 doesn't exist prior to AB8500 version
269                  * 2.0
270                  */
271                 if (ab8500->irq_reg_offset[i] == 11 &&
272                         is_ab8500_1p1_or_earlier(ab8500))
273                         continue;
274
275                 ab8500->oldmask[i] = new;
276
277                 reg = AB8500_IT_MASK1_REG + ab8500_irq_regoffset[i];
278                 set_register_interruptible(ab8500, AB8500_INTERRUPT, reg, new);
279         }
280
281         mutex_unlock(&ab8500->irq_lock);
282 }
283
284 static void ab8500_irq_mask(struct irq_data *data)
285 {
286         struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
287         int offset = data->irq - ab8500->irq_base;
288         int index = offset / 8;
289         int mask = 1 << (offset % 8);
290
291         ab8500->mask[index] |= mask;
292 }
293
294 static void ab8500_irq_unmask(struct irq_data *data)
295 {
296         struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
297         int offset = data->irq - ab8500->irq_base;
298         int index = offset / 8;
299         int mask = 1 << (offset % 8);
300
301         ab8500->mask[index] &= ~mask;
302 }
303
304 static struct irq_chip ab8500_irq_chip = {
305         .name                   = "ab8500",
306         .irq_bus_lock           = ab8500_irq_lock,
307         .irq_bus_sync_unlock    = ab8500_irq_sync_unlock,
308         .irq_mask               = ab8500_irq_mask,
309         .irq_disable            = ab8500_irq_mask,
310         .irq_unmask             = ab8500_irq_unmask,
311 };
312
313 static irqreturn_t ab8500_irq(int irq, void *dev)
314 {
315         struct ab8500 *ab8500 = dev;
316         int i;
317
318         dev_vdbg(ab8500->dev, "interrupt\n");
319
320         for (i = 0; i < AB8500_NUM_IRQ_REGS; i++) {
321                 int regoffset = ab8500_irq_regoffset[i];
322                 int status;
323                 u8 value;
324
325                 /*
326                  * Interrupt register 12 doesn't exist prior to AB8500 version
327                  * 2.0
328                  */
329                 if (regoffset == 11 && is_ab8500_1p1_or_earlier(ab8500))
330                         continue;
331
332                 status = get_register_interruptible(ab8500, AB8500_INTERRUPT,
333                         AB8500_IT_LATCH1_REG + regoffset, &value);
334                 if (status < 0 || value == 0)
335                         continue;
336
337                 do {
338                         int bit = __ffs(value);
339                         int line = i * 8 + bit;
340
341                         handle_nested_irq(ab8500->irq_base + line);
342                         value &= ~(1 << bit);
343                 } while (value);
344         }
345
346         return IRQ_HANDLED;
347 }
348
349 static int ab8500_irq_init(struct ab8500 *ab8500)
350 {
351         int base = ab8500->irq_base;
352         int irq;
353
354         for (irq = base; irq < base + AB8500_NR_IRQS; irq++) {
355                 irq_set_chip_data(irq, ab8500);
356                 irq_set_chip_and_handler(irq, &ab8500_irq_chip,
357                                          handle_simple_irq);
358                 irq_set_nested_thread(irq, 1);
359 #ifdef CONFIG_ARM
360                 set_irq_flags(irq, IRQF_VALID);
361 #else
362                 irq_set_noprobe(irq);
363 #endif
364         }
365
366         return 0;
367 }
368
369 static void ab8500_irq_remove(struct ab8500 *ab8500)
370 {
371         int base = ab8500->irq_base;
372         int irq;
373
374         for (irq = base; irq < base + AB8500_NR_IRQS; irq++) {
375 #ifdef CONFIG_ARM
376                 set_irq_flags(irq, 0);
377 #endif
378                 irq_set_chip_and_handler(irq, NULL, NULL);
379                 irq_set_chip_data(irq, NULL);
380         }
381 }
382
383 static struct resource __devinitdata ab8500_gpio_resources[] = {
384         {
385                 .name   = "GPIO_INT6",
386                 .start  = AB8500_INT_GPIO6R,
387                 .end    = AB8500_INT_GPIO41F,
388                 .flags  = IORESOURCE_IRQ,
389         }
390 };
391
392 static struct resource __devinitdata ab8500_gpadc_resources[] = {
393         {
394                 .name   = "HW_CONV_END",
395                 .start  = AB8500_INT_GP_HW_ADC_CONV_END,
396                 .end    = AB8500_INT_GP_HW_ADC_CONV_END,
397                 .flags  = IORESOURCE_IRQ,
398         },
399         {
400                 .name   = "SW_CONV_END",
401                 .start  = AB8500_INT_GP_SW_ADC_CONV_END,
402                 .end    = AB8500_INT_GP_SW_ADC_CONV_END,
403                 .flags  = IORESOURCE_IRQ,
404         },
405 };
406
407 static struct resource __devinitdata ab8500_rtc_resources[] = {
408         {
409                 .name   = "60S",
410                 .start  = AB8500_INT_RTC_60S,
411                 .end    = AB8500_INT_RTC_60S,
412                 .flags  = IORESOURCE_IRQ,
413         },
414         {
415                 .name   = "ALARM",
416                 .start  = AB8500_INT_RTC_ALARM,
417                 .end    = AB8500_INT_RTC_ALARM,
418                 .flags  = IORESOURCE_IRQ,
419         },
420 };
421
422 static struct resource __devinitdata ab8500_poweronkey_db_resources[] = {
423         {
424                 .name   = "ONKEY_DBF",
425                 .start  = AB8500_INT_PON_KEY1DB_F,
426                 .end    = AB8500_INT_PON_KEY1DB_F,
427                 .flags  = IORESOURCE_IRQ,
428         },
429         {
430                 .name   = "ONKEY_DBR",
431                 .start  = AB8500_INT_PON_KEY1DB_R,
432                 .end    = AB8500_INT_PON_KEY1DB_R,
433                 .flags  = IORESOURCE_IRQ,
434         },
435 };
436
437 static struct resource __devinitdata ab8500_av_acc_detect_resources[] = {
438         {
439                .name = "ACC_DETECT_1DB_F",
440                .start = AB8500_INT_ACC_DETECT_1DB_F,
441                .end = AB8500_INT_ACC_DETECT_1DB_F,
442                .flags = IORESOURCE_IRQ,
443         },
444         {
445                .name = "ACC_DETECT_1DB_R",
446                .start = AB8500_INT_ACC_DETECT_1DB_R,
447                .end = AB8500_INT_ACC_DETECT_1DB_R,
448                .flags = IORESOURCE_IRQ,
449         },
450         {
451                .name = "ACC_DETECT_21DB_F",
452                .start = AB8500_INT_ACC_DETECT_21DB_F,
453                .end = AB8500_INT_ACC_DETECT_21DB_F,
454                .flags = IORESOURCE_IRQ,
455         },
456         {
457                .name = "ACC_DETECT_21DB_R",
458                .start = AB8500_INT_ACC_DETECT_21DB_R,
459                .end = AB8500_INT_ACC_DETECT_21DB_R,
460                .flags = IORESOURCE_IRQ,
461         },
462         {
463                .name = "ACC_DETECT_22DB_F",
464                .start = AB8500_INT_ACC_DETECT_22DB_F,
465                .end = AB8500_INT_ACC_DETECT_22DB_F,
466                .flags = IORESOURCE_IRQ,
467         },
468         {
469                .name = "ACC_DETECT_22DB_R",
470                .start = AB8500_INT_ACC_DETECT_22DB_R,
471                .end = AB8500_INT_ACC_DETECT_22DB_R,
472                .flags = IORESOURCE_IRQ,
473         },
474 };
475
476 static struct resource __devinitdata ab8500_charger_resources[] = {
477         {
478                 .name = "MAIN_CH_UNPLUG_DET",
479                 .start = AB8500_INT_MAIN_CH_UNPLUG_DET,
480                 .end = AB8500_INT_MAIN_CH_UNPLUG_DET,
481                 .flags = IORESOURCE_IRQ,
482         },
483         {
484                 .name = "MAIN_CHARGE_PLUG_DET",
485                 .start = AB8500_INT_MAIN_CH_PLUG_DET,
486                 .end = AB8500_INT_MAIN_CH_PLUG_DET,
487                 .flags = IORESOURCE_IRQ,
488         },
489         {
490                 .name = "VBUS_DET_R",
491                 .start = AB8500_INT_VBUS_DET_R,
492                 .end = AB8500_INT_VBUS_DET_R,
493                 .flags = IORESOURCE_IRQ,
494         },
495         {
496                 .name = "VBUS_DET_F",
497                 .start = AB8500_INT_VBUS_DET_F,
498                 .end = AB8500_INT_VBUS_DET_F,
499                 .flags = IORESOURCE_IRQ,
500         },
501         {
502                 .name = "USB_LINK_STATUS",
503                 .start = AB8500_INT_USB_LINK_STATUS,
504                 .end = AB8500_INT_USB_LINK_STATUS,
505                 .flags = IORESOURCE_IRQ,
506         },
507         {
508                 .name = "USB_CHARGE_DET_DONE",
509                 .start = AB8500_INT_USB_CHG_DET_DONE,
510                 .end = AB8500_INT_USB_CHG_DET_DONE,
511                 .flags = IORESOURCE_IRQ,
512         },
513         {
514                 .name = "VBUS_OVV",
515                 .start = AB8500_INT_VBUS_OVV,
516                 .end = AB8500_INT_VBUS_OVV,
517                 .flags = IORESOURCE_IRQ,
518         },
519         {
520                 .name = "USB_CH_TH_PROT_R",
521                 .start = AB8500_INT_USB_CH_TH_PROT_R,
522                 .end = AB8500_INT_USB_CH_TH_PROT_R,
523                 .flags = IORESOURCE_IRQ,
524         },
525         {
526                 .name = "USB_CH_TH_PROT_F",
527                 .start = AB8500_INT_USB_CH_TH_PROT_F,
528                 .end = AB8500_INT_USB_CH_TH_PROT_F,
529                 .flags = IORESOURCE_IRQ,
530         },
531         {
532                 .name = "MAIN_EXT_CH_NOT_OK",
533                 .start = AB8500_INT_MAIN_EXT_CH_NOT_OK,
534                 .end = AB8500_INT_MAIN_EXT_CH_NOT_OK,
535                 .flags = IORESOURCE_IRQ,
536         },
537         {
538                 .name = "MAIN_CH_TH_PROT_R",
539                 .start = AB8500_INT_MAIN_CH_TH_PROT_R,
540                 .end = AB8500_INT_MAIN_CH_TH_PROT_R,
541                 .flags = IORESOURCE_IRQ,
542         },
543         {
544                 .name = "MAIN_CH_TH_PROT_F",
545                 .start = AB8500_INT_MAIN_CH_TH_PROT_F,
546                 .end = AB8500_INT_MAIN_CH_TH_PROT_F,
547                 .flags = IORESOURCE_IRQ,
548         },
549         {
550                 .name = "USB_CHARGER_NOT_OKR",
551                 .start = AB8500_INT_USB_CHARGER_NOT_OK,
552                 .end = AB8500_INT_USB_CHARGER_NOT_OK,
553                 .flags = IORESOURCE_IRQ,
554         },
555         {
556                 .name = "USB_CHARGER_NOT_OKF",
557                 .start = AB8500_INT_USB_CHARGER_NOT_OKF,
558                 .end = AB8500_INT_USB_CHARGER_NOT_OKF,
559                 .flags = IORESOURCE_IRQ,
560         },
561         {
562                 .name = "CH_WD_EXP",
563                 .start = AB8500_INT_CH_WD_EXP,
564                 .end = AB8500_INT_CH_WD_EXP,
565                 .flags = IORESOURCE_IRQ,
566         },
567 };
568
569 static struct resource __devinitdata ab8500_btemp_resources[] = {
570         {
571                 .name = "BAT_CTRL_INDB",
572                 .start = AB8500_INT_BAT_CTRL_INDB,
573                 .end = AB8500_INT_BAT_CTRL_INDB,
574                 .flags = IORESOURCE_IRQ,
575         },
576         {
577                 .name = "BTEMP_LOW",
578                 .start = AB8500_INT_BTEMP_LOW,
579                 .end = AB8500_INT_BTEMP_LOW,
580                 .flags = IORESOURCE_IRQ,
581         },
582         {
583                 .name = "BTEMP_HIGH",
584                 .start = AB8500_INT_BTEMP_HIGH,
585                 .end = AB8500_INT_BTEMP_HIGH,
586                 .flags = IORESOURCE_IRQ,
587         },
588         {
589                 .name = "BTEMP_LOW_MEDIUM",
590                 .start = AB8500_INT_BTEMP_LOW_MEDIUM,
591                 .end = AB8500_INT_BTEMP_LOW_MEDIUM,
592                 .flags = IORESOURCE_IRQ,
593         },
594         {
595                 .name = "BTEMP_MEDIUM_HIGH",
596                 .start = AB8500_INT_BTEMP_MEDIUM_HIGH,
597                 .end = AB8500_INT_BTEMP_MEDIUM_HIGH,
598                 .flags = IORESOURCE_IRQ,
599         },
600 };
601
602 static struct resource __devinitdata ab8500_fg_resources[] = {
603         {
604                 .name = "NCONV_ACCU",
605                 .start = AB8500_INT_CCN_CONV_ACC,
606                 .end = AB8500_INT_CCN_CONV_ACC,
607                 .flags = IORESOURCE_IRQ,
608         },
609         {
610                 .name = "BATT_OVV",
611                 .start = AB8500_INT_BATT_OVV,
612                 .end = AB8500_INT_BATT_OVV,
613                 .flags = IORESOURCE_IRQ,
614         },
615         {
616                 .name = "LOW_BAT_F",
617                 .start = AB8500_INT_LOW_BAT_F,
618                 .end = AB8500_INT_LOW_BAT_F,
619                 .flags = IORESOURCE_IRQ,
620         },
621         {
622                 .name = "LOW_BAT_R",
623                 .start = AB8500_INT_LOW_BAT_R,
624                 .end = AB8500_INT_LOW_BAT_R,
625                 .flags = IORESOURCE_IRQ,
626         },
627         {
628                 .name = "CC_INT_CALIB",
629                 .start = AB8500_INT_CC_INT_CALIB,
630                 .end = AB8500_INT_CC_INT_CALIB,
631                 .flags = IORESOURCE_IRQ,
632         },
633 };
634
635 static struct resource __devinitdata ab8500_chargalg_resources[] = {};
636
637 #ifdef CONFIG_DEBUG_FS
638 static struct resource __devinitdata ab8500_debug_resources[] = {
639         {
640                 .name   = "IRQ_FIRST",
641                 .start  = AB8500_INT_MAIN_EXT_CH_NOT_OK,
642                 .end    = AB8500_INT_MAIN_EXT_CH_NOT_OK,
643                 .flags  = IORESOURCE_IRQ,
644         },
645         {
646                 .name   = "IRQ_LAST",
647                 .start  = AB8500_INT_USB_CHARGER_NOT_OKF,
648                 .end    = AB8500_INT_USB_CHARGER_NOT_OKF,
649                 .flags  = IORESOURCE_IRQ,
650         },
651 };
652 #endif
653
654 static struct resource __devinitdata ab8500_usb_resources[] = {
655         {
656                 .name = "ID_WAKEUP_R",
657                 .start = AB8500_INT_ID_WAKEUP_R,
658                 .end = AB8500_INT_ID_WAKEUP_R,
659                 .flags = IORESOURCE_IRQ,
660         },
661         {
662                 .name = "ID_WAKEUP_F",
663                 .start = AB8500_INT_ID_WAKEUP_F,
664                 .end = AB8500_INT_ID_WAKEUP_F,
665                 .flags = IORESOURCE_IRQ,
666         },
667         {
668                 .name = "VBUS_DET_F",
669                 .start = AB8500_INT_VBUS_DET_F,
670                 .end = AB8500_INT_VBUS_DET_F,
671                 .flags = IORESOURCE_IRQ,
672         },
673         {
674                 .name = "VBUS_DET_R",
675                 .start = AB8500_INT_VBUS_DET_R,
676                 .end = AB8500_INT_VBUS_DET_R,
677                 .flags = IORESOURCE_IRQ,
678         },
679         {
680                 .name = "USB_LINK_STATUS",
681                 .start = AB8500_INT_USB_LINK_STATUS,
682                 .end = AB8500_INT_USB_LINK_STATUS,
683                 .flags = IORESOURCE_IRQ,
684         },
685         {
686                 .name = "USB_ADP_PROBE_PLUG",
687                 .start = AB8500_INT_ADP_PROBE_PLUG,
688                 .end = AB8500_INT_ADP_PROBE_PLUG,
689                 .flags = IORESOURCE_IRQ,
690         },
691         {
692                 .name = "USB_ADP_PROBE_UNPLUG",
693                 .start = AB8500_INT_ADP_PROBE_UNPLUG,
694                 .end = AB8500_INT_ADP_PROBE_UNPLUG,
695                 .flags = IORESOURCE_IRQ,
696         },
697 };
698
699 static struct resource __devinitdata ab8500_temp_resources[] = {
700         {
701                 .name  = "AB8500_TEMP_WARM",
702                 .start = AB8500_INT_TEMP_WARM,
703                 .end   = AB8500_INT_TEMP_WARM,
704                 .flags = IORESOURCE_IRQ,
705         },
706 };
707
708 static struct mfd_cell __devinitdata ab8500_devs[] = {
709 #ifdef CONFIG_DEBUG_FS
710         {
711                 .name = "ab8500-debug",
712                 .num_resources = ARRAY_SIZE(ab8500_debug_resources),
713                 .resources = ab8500_debug_resources,
714         },
715 #endif
716         {
717                 .name = "ab8500-sysctrl",
718         },
719         {
720                 .name = "ab8500-regulator",
721         },
722         {
723                 .name = "ab8500-gpio",
724                 .num_resources = ARRAY_SIZE(ab8500_gpio_resources),
725                 .resources = ab8500_gpio_resources,
726         },
727         {
728                 .name = "ab8500-gpadc",
729                 .num_resources = ARRAY_SIZE(ab8500_gpadc_resources),
730                 .resources = ab8500_gpadc_resources,
731         },
732         {
733                 .name = "ab8500-rtc",
734                 .num_resources = ARRAY_SIZE(ab8500_rtc_resources),
735                 .resources = ab8500_rtc_resources,
736         },
737         {
738                 .name = "ab8500-charger",
739                 .num_resources = ARRAY_SIZE(ab8500_charger_resources),
740                 .resources = ab8500_charger_resources,
741         },
742         {
743                 .name = "ab8500-btemp",
744                 .num_resources = ARRAY_SIZE(ab8500_btemp_resources),
745                 .resources = ab8500_btemp_resources,
746         },
747         {
748                 .name = "ab8500-fg",
749                 .num_resources = ARRAY_SIZE(ab8500_fg_resources),
750                 .resources = ab8500_fg_resources,
751         },
752         {
753                 .name = "ab8500-chargalg",
754                 .num_resources = ARRAY_SIZE(ab8500_chargalg_resources),
755                 .resources = ab8500_chargalg_resources,
756         },
757         {
758                 .name = "ab8500-acc-det",
759                 .num_resources = ARRAY_SIZE(ab8500_av_acc_detect_resources),
760                 .resources = ab8500_av_acc_detect_resources,
761         },
762         {
763                 .name = "ab8500-codec",
764         },
765         {
766                 .name = "ab8500-usb",
767                 .num_resources = ARRAY_SIZE(ab8500_usb_resources),
768                 .resources = ab8500_usb_resources,
769         },
770         {
771                 .name = "ab8500-poweron-key",
772                 .num_resources = ARRAY_SIZE(ab8500_poweronkey_db_resources),
773                 .resources = ab8500_poweronkey_db_resources,
774         },
775         {
776                 .name = "ab8500-pwm",
777                 .id = 1,
778         },
779         {
780                 .name = "ab8500-pwm",
781                 .id = 2,
782         },
783         {
784                 .name = "ab8500-pwm",
785                 .id = 3,
786         },
787         { .name = "ab8500-leds", },
788         {
789                 .name = "ab8500-denc",
790         },
791         {
792                 .name = "ab8500-temp",
793                 .num_resources = ARRAY_SIZE(ab8500_temp_resources),
794                 .resources = ab8500_temp_resources,
795         },
796 };
797
798 static ssize_t show_chip_id(struct device *dev,
799                                 struct device_attribute *attr, char *buf)
800 {
801         struct ab8500 *ab8500;
802
803         ab8500 = dev_get_drvdata(dev);
804         return sprintf(buf, "%#x\n", ab8500 ? ab8500->chip_id : -EINVAL);
805 }
806
807 /*
808  * ab8500 has switched off due to (SWITCH_OFF_STATUS):
809  * 0x01 Swoff bit programming
810  * 0x02 Thermal protection activation
811  * 0x04 Vbat lower then BattOk falling threshold
812  * 0x08 Watchdog expired
813  * 0x10 Non presence of 32kHz clock
814  * 0x20 Battery level lower than power on reset threshold
815  * 0x40 Power on key 1 pressed longer than 10 seconds
816  * 0x80 DB8500 thermal shutdown
817  */
818 static ssize_t show_switch_off_status(struct device *dev,
819                                 struct device_attribute *attr, char *buf)
820 {
821         int ret;
822         u8 value;
823         struct ab8500 *ab8500;
824
825         ab8500 = dev_get_drvdata(dev);
826         ret = get_register_interruptible(ab8500, AB8500_RTC,
827                 AB8500_SWITCH_OFF_STATUS, &value);
828         if (ret < 0)
829                 return ret;
830         return sprintf(buf, "%#x\n", value);
831 }
832
833 /*
834  * ab8500 has turned on due to (TURN_ON_STATUS):
835  * 0x01 PORnVbat
836  * 0x02 PonKey1dbF
837  * 0x04 PonKey2dbF
838  * 0x08 RTCAlarm
839  * 0x10 MainChDet
840  * 0x20 VbusDet
841  * 0x40 UsbIDDetect
842  * 0x80 Reserved
843  */
844 static ssize_t show_turn_on_status(struct device *dev,
845                                 struct device_attribute *attr, char *buf)
846 {
847         int ret;
848         u8 value;
849         struct ab8500 *ab8500;
850
851         ab8500 = dev_get_drvdata(dev);
852         ret = get_register_interruptible(ab8500, AB8500_SYS_CTRL1_BLOCK,
853                 AB8500_TURN_ON_STATUS, &value);
854         if (ret < 0)
855                 return ret;
856         return sprintf(buf, "%#x\n", value);
857 }
858
859 static DEVICE_ATTR(chip_id, S_IRUGO, show_chip_id, NULL);
860 static DEVICE_ATTR(switch_off_status, S_IRUGO, show_switch_off_status, NULL);
861 static DEVICE_ATTR(turn_on_status, S_IRUGO, show_turn_on_status, NULL);
862
863 static struct attribute *ab8500_sysfs_entries[] = {
864         &dev_attr_chip_id.attr,
865         &dev_attr_switch_off_status.attr,
866         &dev_attr_turn_on_status.attr,
867         NULL,
868 };
869
870 static struct attribute_group ab8500_attr_group = {
871         .attrs  = ab8500_sysfs_entries,
872 };
873
874 int __devinit ab8500_init(struct ab8500 *ab8500, enum ab8500_version version)
875 {
876         struct ab8500_platform_data *plat = dev_get_platdata(ab8500->dev);
877         int ret;
878         int i;
879         u8 value;
880
881         if (plat)
882                 ab8500->irq_base = plat->irq_base;
883
884         mutex_init(&ab8500->lock);
885         mutex_init(&ab8500->irq_lock);
886
887         if (version != AB8500_VERSION_UNDEFINED)
888                 ab8500->version = version;
889         else {
890                 ret = get_register_interruptible(ab8500, AB8500_MISC,
891                         AB8500_IC_NAME_REG, &value);
892                 if (ret < 0)
893                         return ret;
894
895                 ab8500->version = value;
896         }
897
898         ret = get_register_interruptible(ab8500, AB8500_MISC,
899                 AB8500_REV_REG, &value);
900         if (ret < 0)
901                 return ret;
902
903         ab8500->chip_id = value;
904
905         dev_info(ab8500->dev, "detected chip, %s rev. %1x.%1x\n",
906                         ab8500_version_str[ab8500->version],
907                         ab8500->chip_id >> 4,
908                         ab8500->chip_id & 0x0F);
909
910         /*
911          * ab8500 has switched off due to (SWITCH_OFF_STATUS):
912          * 0x01 Swoff bit programming
913          * 0x02 Thermal protection activation
914          * 0x04 Vbat lower then BattOk falling threshold
915          * 0x08 Watchdog expired
916          * 0x10 Non presence of 32kHz clock
917          * 0x20 Battery level lower than power on reset threshold
918          * 0x40 Power on key 1 pressed longer than 10 seconds
919          * 0x80 DB8500 thermal shutdown
920          */
921
922         ret = get_register_interruptible(ab8500, AB8500_RTC,
923                 AB8500_SWITCH_OFF_STATUS, &value);
924         if (ret < 0)
925                 return ret;
926         dev_info(ab8500->dev, "switch off status: %#x", value);
927
928         if (plat && plat->init)
929                 plat->init(ab8500);
930
931         /* Clear and mask all interrupts */
932         for (i = 0; i < AB8500_NUM_IRQ_REGS; i++) {
933                 /*
934                  * Interrupt register 12 doesn't exist prior to AB8500 version
935                  * 2.0
936                  */
937                 if (ab8500->irq_reg_offset[i] == 11 &&
938                                 is_ab8500_1p1_or_earlier(ab8500))
939                         continue;
940
941                 get_register_interruptible(ab8500, AB8500_INTERRUPT,
942                         AB8500_IT_LATCH1_REG + ab8500_irq_regoffset[i],
943                         &value);
944                 set_register_interruptible(ab8500, AB8500_INTERRUPT,
945                         AB8500_IT_MASK1_REG + ab8500_irq_regoffset[i], 0xff);
946         }
947
948         ret = abx500_register_ops(ab8500->dev, &ab8500_ops);
949         if (ret)
950                 return ret;
951
952         for (i = 0; i < AB8500_NUM_IRQ_REGS; i++)
953                 ab8500->mask[i] = ab8500->oldmask[i] = 0xff;
954
955         if (ab8500->irq_base) {
956                 ret = ab8500_irq_init(ab8500);
957                 if (ret)
958                         return ret;
959
960                 ret = request_threaded_irq(ab8500->irq, NULL, ab8500_irq,
961                                            IRQF_ONESHOT | IRQF_NO_SUSPEND,
962                                            "ab8500", ab8500);
963                 if (ret)
964                         goto out_removeirq;
965         }
966
967         ret = mfd_add_devices(ab8500->dev, 0, ab8500_devs,
968                               ARRAY_SIZE(ab8500_devs), NULL,
969                               ab8500->irq_base);
970         if (ret)
971                 goto out_freeirq;
972
973         ret = sysfs_create_group(&ab8500->dev->kobj, &ab8500_attr_group);
974         if (ret)
975                 dev_err(ab8500->dev, "error creating sysfs entries\n");
976
977         return ret;
978
979 out_freeirq:
980         if (ab8500->irq_base)
981                 free_irq(ab8500->irq, ab8500);
982 out_removeirq:
983         if (ab8500->irq_base)
984                 ab8500_irq_remove(ab8500);
985
986         return ret;
987 }
988
989 int __devexit ab8500_exit(struct ab8500 *ab8500)
990 {
991         sysfs_remove_group(&ab8500->dev->kobj, &ab8500_attr_group);
992         mfd_remove_devices(ab8500->dev);
993         if (ab8500->irq_base) {
994                 free_irq(ab8500->irq, ab8500);
995                 ab8500_irq_remove(ab8500);
996         }
997
998         return 0;
999 }
1000
1001 MODULE_AUTHOR("Mattias Wallin, Srinidhi Kasagar, Rabin Vincent");
1002 MODULE_DESCRIPTION("AB8500 MFD core");
1003 MODULE_LICENSE("GPL v2");