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