camera: fix af thread also receive cmd if af isn't ready,fix 5640 720p array and...
[firefly-linux-kernel-4.4.55.git] / drivers / mfd / wm831x-core.c
1 /*
2  * wm831x-core.c  --  Device access for Wolfson WM831x PMICs
3  *
4  * Copyright 2009 Wolfson Microelectronics PLC.
5  *
6  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7  *
8  *  This program is free software; you can redistribute  it and/or modify it
9  *  under  the terms of  the GNU General  Public License as published by the
10  *  Free Software Foundation;  either version 2 of the  License, or (at your
11  *  option) any later version.
12  *
13  */
14
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/bcd.h>
18 #include <linux/delay.h>
19 #include <linux/mfd/core.h>
20 #include <linux/slab.h>
21 #include <linux/irq.h>
22
23 #include <linux/mfd/wm831x/core.h>
24 #include <linux/mfd/wm831x/pdata.h>
25 #include <linux/mfd/wm831x/irq.h>
26 #include <linux/mfd/wm831x/auxadc.h>
27 #include <linux/mfd/wm831x/otp.h>
28 #include <linux/mfd/wm831x/regulator.h>
29 #include <linux/mfd/wm831x/pmu.h>
30
31
32 /* Current settings - values are 2*2^(reg_val/4) microamps.  These are
33  * exported since they are used by multiple drivers.
34  */
35  extern int reboot_cmd_get(void);
36 int wm831x_isinkv_values[WM831X_ISINK_MAX_ISEL + 1] = {
37         2,
38         2,
39         3,
40         3,
41         4,
42         5,
43         6,
44         7,
45         8,
46         10,
47         11,
48         13,
49         16,
50         19,
51         23,
52         27,
53         32,
54         38,
55         45,
56         54,
57         64,
58         76,
59         91,
60         108,
61         128,
62         152,
63         181,
64         215,
65         256,
66         304,
67         362,
68         431,
69         512,
70         609,
71         724,
72         861,
73         1024,
74         1218,
75         1448,
76         1722,
77         2048,
78         2435,
79         2896,
80         3444,
81         4096,
82         4871,
83         5793,
84         6889,
85         8192,
86         9742,
87         11585,
88         13777,
89         16384,
90         19484,
91         23170,
92         27554,
93 };
94 EXPORT_SYMBOL_GPL(wm831x_isinkv_values);
95
96 static int wm831x_reg_locked(struct wm831x *wm831x, unsigned short reg)
97 {
98         if (!wm831x->locked)
99                 return 0;
100
101         switch (reg) {
102         case WM831X_WATCHDOG:
103         case WM831X_DC4_CONTROL:
104         case WM831X_ON_PIN_CONTROL:
105         case WM831X_BACKUP_CHARGER_CONTROL:
106         case WM831X_CHARGER_CONTROL_1:
107         case WM831X_CHARGER_CONTROL_2:
108                 return 1;
109
110         default:
111                 return 0;
112         }
113 }
114
115 /**
116  * wm831x_reg_unlock: Unlock user keyed registers
117  *
118  * The WM831x has a user key preventing writes to particularly
119  * critical registers.  This function locks those registers,
120  * allowing writes to them.
121  */
122 void wm831x_reg_lock(struct wm831x *wm831x)
123 {
124         int ret;
125
126         ret = wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0);
127         if (ret == 0) {
128                 dev_vdbg(wm831x->dev, "Registers locked\n");
129
130                 mutex_lock(&wm831x->io_lock);
131                 WARN_ON(wm831x->locked);
132                 wm831x->locked = 1;
133                 mutex_unlock(&wm831x->io_lock);
134         } else {
135                 dev_err(wm831x->dev, "Failed to lock registers: %d\n", ret);
136         }
137
138 }
139 EXPORT_SYMBOL_GPL(wm831x_reg_lock);
140
141 /**
142  * wm831x_reg_unlock: Unlock user keyed registers
143  *
144  * The WM831x has a user key preventing writes to particularly
145  * critical registers.  This function locks those registers,
146  * preventing spurious writes.
147  */
148 int wm831x_reg_unlock(struct wm831x *wm831x)
149 {
150         int ret;
151
152         /* 0x9716 is the value required to unlock the registers */
153         ret = wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0x9716);
154         if (ret == 0) {
155                 dev_vdbg(wm831x->dev, "Registers unlocked\n");
156
157                 mutex_lock(&wm831x->io_lock);
158                 WARN_ON(!wm831x->locked);
159                 wm831x->locked = 0;
160                 mutex_unlock(&wm831x->io_lock);
161         }
162
163         return ret;
164 }
165 EXPORT_SYMBOL_GPL(wm831x_reg_unlock);
166
167 static int wm831x_read(struct wm831x *wm831x, unsigned short reg,
168                        int bytes, void *dest)
169 {
170         int ret, i;
171         u16 *buf = dest;
172
173         BUG_ON(bytes % 2);
174         BUG_ON(bytes <= 0);
175
176         ret = wm831x->read_dev(wm831x, reg, bytes, dest);
177         if (ret < 0)
178                 return ret;
179
180         for (i = 0; i < bytes / 2; i++) {
181                 buf[i] = be16_to_cpu(buf[i]);
182
183                 dev_vdbg(wm831x->dev, "Read %04x from R%d(0x%x)\n",
184                          buf[i], reg + i, reg + i);
185         }
186
187         return 0;
188 }
189
190 /**
191  * wm831x_reg_read: Read a single WM831x register.
192  *
193  * @wm831x: Device to read from.
194  * @reg: Register to read.
195  */
196 int wm831x_reg_read(struct wm831x *wm831x, unsigned short reg)
197 {
198         unsigned short val;
199         int ret;
200
201         mutex_lock(&wm831x->io_lock);
202
203         ret = wm831x_read(wm831x, reg, 2, &val);
204
205         mutex_unlock(&wm831x->io_lock);
206
207         if (ret < 0)
208                 return ret;
209         else
210                 return val;
211 }
212 EXPORT_SYMBOL_GPL(wm831x_reg_read);
213
214 /**
215  * wm831x_bulk_read: Read multiple WM831x registers
216  *
217  * @wm831x: Device to read from
218  * @reg: First register
219  * @count: Number of registers
220  * @buf: Buffer to fill.
221  */
222 int wm831x_bulk_read(struct wm831x *wm831x, unsigned short reg,
223                      int count, u16 *buf)
224 {
225         int ret;
226
227         mutex_lock(&wm831x->io_lock);
228
229         ret = wm831x_read(wm831x, reg, count * 2, buf);
230
231         mutex_unlock(&wm831x->io_lock);
232
233         return ret;
234 }
235 EXPORT_SYMBOL_GPL(wm831x_bulk_read);
236
237 static int wm831x_write(struct wm831x *wm831x, unsigned short reg,
238                         int bytes, void *src)
239 {
240         u16 *buf = src;
241         int i;
242
243         BUG_ON(bytes % 2);
244         BUG_ON(bytes <= 0);
245
246         for (i = 0; i < bytes / 2; i++) {
247                 if (wm831x_reg_locked(wm831x, reg))
248                         return -EPERM;
249
250                 dev_vdbg(wm831x->dev, "Write %04x to R%d(0x%x)\n",
251                          buf[i], reg + i, reg + i);
252
253                 buf[i] = cpu_to_be16(buf[i]);
254         }
255
256         return wm831x->write_dev(wm831x, reg, bytes, src);
257 }
258
259 /**
260  * wm831x_reg_write: Write a single WM831x register.
261  *
262  * @wm831x: Device to write to.
263  * @reg: Register to write to.
264  * @val: Value to write.
265  */
266 int wm831x_reg_write(struct wm831x *wm831x, unsigned short reg,
267                      unsigned short val)
268 {
269         int ret;
270
271         mutex_lock(&wm831x->io_lock);
272
273         ret = wm831x_write(wm831x, reg, 2, &val);
274
275         mutex_unlock(&wm831x->io_lock);
276
277         return ret;
278 }
279 EXPORT_SYMBOL_GPL(wm831x_reg_write);
280
281 /**
282  * wm831x_set_bits: Set the value of a bitfield in a WM831x register
283  *
284  * @wm831x: Device to write to.
285  * @reg: Register to write to.
286  * @mask: Mask of bits to set.
287  * @val: Value to set (unshifted)
288  */
289 int wm831x_set_bits(struct wm831x *wm831x, unsigned short reg,
290                     unsigned short mask, unsigned short val)
291 {
292         int ret;
293         u16 r;
294
295         mutex_lock(&wm831x->io_lock);
296
297         ret = wm831x_read(wm831x, reg, 2, &r);
298         if (ret < 0)
299                 goto out;
300
301         r &= ~mask;
302         r |= val;
303
304         ret = wm831x_write(wm831x, reg, 2, &r);
305
306 out:
307         mutex_unlock(&wm831x->io_lock);
308
309         return ret;
310 }
311 EXPORT_SYMBOL_GPL(wm831x_set_bits);
312
313 /**
314  * wm831x_auxadc_read: Read a value from the WM831x AUXADC
315  *
316  * @wm831x: Device to read from.
317  * @input: AUXADC input to read.
318  */
319 int wm831x_auxadc_read(struct wm831x *wm831x, enum wm831x_auxadc input)
320 {
321         int ret, src, irq_masked, timeout;
322
323         /* Are we using the interrupt? */
324         irq_masked = wm831x_reg_read(wm831x, WM831X_INTERRUPT_STATUS_1_MASK);
325         irq_masked &= WM831X_AUXADC_DATA_EINT;
326
327         mutex_lock(&wm831x->auxadc_lock);
328
329         ret = wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL,
330                               WM831X_AUX_ENA, WM831X_AUX_ENA);
331         if (ret < 0) {
332                 dev_err(wm831x->dev, "Failed to enable AUXADC: %d\n", ret);
333                 goto out;
334         }
335
336         /* We force a single source at present */
337         src = input;
338         ret = wm831x_reg_write(wm831x, WM831X_AUXADC_SOURCE,
339                                1 << src);
340         if (ret < 0) {
341                 dev_err(wm831x->dev, "Failed to set AUXADC source: %d\n", ret);
342                 goto out;
343         }
344
345         /* Clear any notification from a very late arriving interrupt */
346         try_wait_for_completion(&wm831x->auxadc_done);
347
348         ret = wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL,
349                               WM831X_AUX_CVT_ENA, WM831X_AUX_CVT_ENA);
350         if (ret < 0) {
351                 dev_err(wm831x->dev, "Failed to start AUXADC: %d\n", ret);
352                 goto disable;
353         }
354
355         if (irq_masked) {
356                 /* If we're not using interrupts then poll the
357                  * interrupt status register */
358                 timeout = 5;
359                 while (timeout) {
360                         msleep(1);
361
362                         ret = wm831x_reg_read(wm831x,
363                                               WM831X_INTERRUPT_STATUS_1);
364                         if (ret < 0) {
365                                 dev_err(wm831x->dev,
366                                         "ISR 1 read failed: %d\n", ret);
367                                 goto disable;
368                         }
369
370                         /* Did it complete? */
371                         if (ret & WM831X_AUXADC_DATA_EINT) {
372                                 wm831x_reg_write(wm831x,
373                                                  WM831X_INTERRUPT_STATUS_1,
374                                                  WM831X_AUXADC_DATA_EINT);
375                                 break;
376                         } else {
377                                 dev_err(wm831x->dev,
378                                         "AUXADC conversion timeout\n");
379                                 ret = -EBUSY;
380                                 goto disable;
381                         }
382                 }
383         } else {
384                 /* If we are using interrupts then wait for the
385                  * interrupt to complete.  Use an extremely long
386                  * timeout to handle situations with heavy load where
387                  * the notification of the interrupt may be delayed by
388                  * threaded IRQ handling. */
389                 if (!wait_for_completion_timeout(&wm831x->auxadc_done,
390                                                  msecs_to_jiffies(2000))) {
391                         dev_err(wm831x->dev, "Timed out waiting for AUXADC\n");
392                         ret = -EBUSY;
393                         goto disable;
394                 }
395         }
396
397         ret = wm831x_reg_read(wm831x, WM831X_AUXADC_DATA);
398         if (ret < 0) {
399                 dev_err(wm831x->dev, "Failed to read AUXADC data: %d\n", ret);
400         } else {
401                 src = ((ret & WM831X_AUX_DATA_SRC_MASK)
402                        >> WM831X_AUX_DATA_SRC_SHIFT) - 1;
403
404                 if (src == 14)
405                         src = WM831X_AUX_CAL;
406
407                 if (src != input) {
408                         dev_err(wm831x->dev, "Data from source %d not %d\n",
409                                 src, input);
410                         ret = -EINVAL;
411                 } else {
412                         ret &= WM831X_AUX_DATA_MASK;
413                 }
414         }
415
416 disable:
417         wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL, WM831X_AUX_ENA, 0);
418 out:
419         mutex_unlock(&wm831x->auxadc_lock);
420         return ret;
421 }
422 EXPORT_SYMBOL_GPL(wm831x_auxadc_read);
423
424 static irqreturn_t wm831x_auxadc_irq(int irq, void *irq_data)
425 {
426         struct wm831x *wm831x = irq_data;
427
428         complete(&wm831x->auxadc_done);
429
430         return IRQ_HANDLED;
431 }
432
433 /**
434  * wm831x_auxadc_read_uv: Read a voltage from the WM831x AUXADC
435  *
436  * @wm831x: Device to read from.
437  * @input: AUXADC input to read.
438  */
439 int wm831x_auxadc_read_uv(struct wm831x *wm831x, enum wm831x_auxadc input)
440 {
441         int ret;
442
443         ret = wm831x_auxadc_read(wm831x, input);
444         if (ret < 0)
445                 return ret;
446
447         ret *= 1465;
448
449         return ret;
450 }
451 EXPORT_SYMBOL_GPL(wm831x_auxadc_read_uv);
452
453 static struct resource wm831x_dcdc1_resources[] = {
454         {
455                 .start = WM831X_DC1_CONTROL_1,
456                 .end   = WM831X_DC1_DVS_CONTROL,
457                 .flags = IORESOURCE_IO,
458         },
459         {
460                 .name  = "UV",
461                 .start = WM831X_IRQ_UV_DC1,
462                 .end   = WM831X_IRQ_UV_DC1,
463                 .flags = IORESOURCE_IRQ,
464         },
465         {
466                 .name  = "HC",
467                 .start = WM831X_IRQ_HC_DC1,
468                 .end   = WM831X_IRQ_HC_DC1,
469                 .flags = IORESOURCE_IRQ,
470         },
471 };
472
473
474 static struct resource wm831x_dcdc2_resources[] = {
475         {
476                 .start = WM831X_DC2_CONTROL_1,
477                 .end   = WM831X_DC2_DVS_CONTROL,
478                 .flags = IORESOURCE_IO,
479         },
480         {
481                 .name  = "UV",
482                 .start = WM831X_IRQ_UV_DC2,
483                 .end   = WM831X_IRQ_UV_DC2,
484                 .flags = IORESOURCE_IRQ,
485         },
486         {
487                 .name  = "HC",
488                 .start = WM831X_IRQ_HC_DC2,
489                 .end   = WM831X_IRQ_HC_DC2,
490                 .flags = IORESOURCE_IRQ,
491         },
492 };
493
494 static struct resource wm831x_dcdc3_resources[] = {
495         {
496                 .start = WM831X_DC3_CONTROL_1,
497                 .end   = WM831X_DC3_SLEEP_CONTROL,
498                 .flags = IORESOURCE_IO,
499         },
500         {
501                 .name  = "UV",
502                 .start = WM831X_IRQ_UV_DC3,
503                 .end   = WM831X_IRQ_UV_DC3,
504                 .flags = IORESOURCE_IRQ,
505         },
506 };
507
508 static struct resource wm831x_dcdc4_resources[] = {
509         {
510                 .start = WM831X_DC4_CONTROL,
511                 .end   = WM831X_DC4_SLEEP_CONTROL,
512                 .flags = IORESOURCE_IO,
513         },
514         {
515                 .name  = "UV",
516                 .start = WM831X_IRQ_UV_DC4,
517                 .end   = WM831X_IRQ_UV_DC4,
518                 .flags = IORESOURCE_IRQ,
519         },
520 };
521
522 static struct resource wm8320_dcdc4_buck_resources[] = {
523         {
524                 .start = WM831X_DC4_CONTROL,
525                 .end   = WM832X_DC4_SLEEP_CONTROL,
526                 .flags = IORESOURCE_IO,
527         },
528         {
529                 .name  = "UV",
530                 .start = WM831X_IRQ_UV_DC4,
531                 .end   = WM831X_IRQ_UV_DC4,
532                 .flags = IORESOURCE_IRQ,
533         },
534 };
535
536 static struct resource wm831x_gpio_resources[] = {
537         {
538                 .start = WM831X_IRQ_GPIO_1,
539                 .end   = WM831X_IRQ_GPIO_16,
540                 .flags = IORESOURCE_IRQ,
541         },
542 };
543
544 static struct resource wm831x_isink1_resources[] = {
545         {
546                 .start = WM831X_CURRENT_SINK_1,
547                 .end   = WM831X_CURRENT_SINK_1,
548                 .flags = IORESOURCE_IO,
549         },
550         {
551                 .start = WM831X_IRQ_CS1,
552                 .end   = WM831X_IRQ_CS1,
553                 .flags = IORESOURCE_IRQ,
554         },
555 };
556
557 static struct resource wm831x_isink2_resources[] = {
558         {
559                 .start = WM831X_CURRENT_SINK_2,
560                 .end   = WM831X_CURRENT_SINK_2,
561                 .flags = IORESOURCE_IO,
562         },
563         {
564                 .start = WM831X_IRQ_CS2,
565                 .end   = WM831X_IRQ_CS2,
566                 .flags = IORESOURCE_IRQ,
567         },
568 };
569
570 static struct resource wm831x_ldo1_resources[] = {
571         {
572                 .start = WM831X_LDO1_CONTROL,
573                 .end   = WM831X_LDO1_SLEEP_CONTROL,
574                 .flags = IORESOURCE_IO,
575         },
576         {
577                 .name  = "UV",
578                 .start = WM831X_IRQ_UV_LDO1,
579                 .end   = WM831X_IRQ_UV_LDO1,
580                 .flags = IORESOURCE_IRQ,
581         },
582 };
583
584 static struct resource wm831x_ldo2_resources[] = {
585         {
586                 .start = WM831X_LDO2_CONTROL,
587                 .end   = WM831X_LDO2_SLEEP_CONTROL,
588                 .flags = IORESOURCE_IO,
589         },
590         {
591                 .name  = "UV",
592                 .start = WM831X_IRQ_UV_LDO2,
593                 .end   = WM831X_IRQ_UV_LDO2,
594                 .flags = IORESOURCE_IRQ,
595         },
596 };
597
598 static struct resource wm831x_ldo3_resources[] = {
599         {
600                 .start = WM831X_LDO3_CONTROL,
601                 .end   = WM831X_LDO3_SLEEP_CONTROL,
602                 .flags = IORESOURCE_IO,
603         },
604         {
605                 .name  = "UV",
606                 .start = WM831X_IRQ_UV_LDO3,
607                 .end   = WM831X_IRQ_UV_LDO3,
608                 .flags = IORESOURCE_IRQ,
609         },
610 };
611
612 static struct resource wm831x_ldo4_resources[] = {
613         {
614                 .start = WM831X_LDO4_CONTROL,
615                 .end   = WM831X_LDO4_SLEEP_CONTROL,
616                 .flags = IORESOURCE_IO,
617         },
618         {
619                 .name  = "UV",
620                 .start = WM831X_IRQ_UV_LDO4,
621                 .end   = WM831X_IRQ_UV_LDO4,
622                 .flags = IORESOURCE_IRQ,
623         },
624 };
625
626 static struct resource wm831x_ldo5_resources[] = {
627         {
628                 .start = WM831X_LDO5_CONTROL,
629                 .end   = WM831X_LDO5_SLEEP_CONTROL,
630                 .flags = IORESOURCE_IO,
631         },
632         {
633                 .name  = "UV",
634                 .start = WM831X_IRQ_UV_LDO5,
635                 .end   = WM831X_IRQ_UV_LDO5,
636                 .flags = IORESOURCE_IRQ,
637         },
638 };
639
640 static struct resource wm831x_ldo6_resources[] = {
641         {
642                 .start = WM831X_LDO6_CONTROL,
643                 .end   = WM831X_LDO6_SLEEP_CONTROL,
644                 .flags = IORESOURCE_IO,
645         },
646         {
647                 .name  = "UV",
648                 .start = WM831X_IRQ_UV_LDO6,
649                 .end   = WM831X_IRQ_UV_LDO6,
650                 .flags = IORESOURCE_IRQ,
651         },
652 };
653
654 static struct resource wm831x_ldo7_resources[] = {
655         {
656                 .start = WM831X_LDO7_CONTROL,
657                 .end   = WM831X_LDO7_SLEEP_CONTROL,
658                 .flags = IORESOURCE_IO,
659         },
660         {
661                 .name  = "UV",
662                 .start = WM831X_IRQ_UV_LDO7,
663                 .end   = WM831X_IRQ_UV_LDO7,
664                 .flags = IORESOURCE_IRQ,
665         },
666 };
667
668 static struct resource wm831x_ldo8_resources[] = {
669         {
670                 .start = WM831X_LDO8_CONTROL,
671                 .end   = WM831X_LDO8_SLEEP_CONTROL,
672                 .flags = IORESOURCE_IO,
673         },
674         {
675                 .name  = "UV",
676                 .start = WM831X_IRQ_UV_LDO8,
677                 .end   = WM831X_IRQ_UV_LDO8,
678                 .flags = IORESOURCE_IRQ,
679         },
680 };
681
682 static struct resource wm831x_ldo9_resources[] = {
683         {
684                 .start = WM831X_LDO9_CONTROL,
685                 .end   = WM831X_LDO9_SLEEP_CONTROL,
686                 .flags = IORESOURCE_IO,
687         },
688         {
689                 .name  = "UV",
690                 .start = WM831X_IRQ_UV_LDO9,
691                 .end   = WM831X_IRQ_UV_LDO9,
692                 .flags = IORESOURCE_IRQ,
693         },
694 };
695
696 static struct resource wm831x_ldo10_resources[] = {
697         {
698                 .start = WM831X_LDO10_CONTROL,
699                 .end   = WM831X_LDO10_SLEEP_CONTROL,
700                 .flags = IORESOURCE_IO,
701         },
702         {
703                 .name  = "UV",
704                 .start = WM831X_IRQ_UV_LDO10,
705                 .end   = WM831X_IRQ_UV_LDO10,
706                 .flags = IORESOURCE_IRQ,
707         },
708 };
709
710 static struct resource wm831x_ldo11_resources[] = {
711         {
712                 .start = WM831X_LDO11_ON_CONTROL,
713                 .end   = WM831X_LDO11_SLEEP_CONTROL,
714                 .flags = IORESOURCE_IO,
715         },
716 };
717
718 static struct resource wm831x_on_resources[] = {
719         {
720                 .start = WM831X_IRQ_ON,
721                 .end   = WM831X_IRQ_ON,
722                 .flags = IORESOURCE_IRQ,
723         },
724 };
725
726
727 static struct resource wm831x_power_resources[] = {
728         {
729                 .name = "SYSLO",
730                 .start = WM831X_IRQ_PPM_SYSLO,
731                 .end   = WM831X_IRQ_PPM_SYSLO,
732                 .flags = IORESOURCE_IRQ,
733         },
734         {
735                 .name = "PWR SRC",
736                 .start = WM831X_IRQ_PPM_PWR_SRC,
737                 .end   = WM831X_IRQ_PPM_PWR_SRC,
738                 .flags = IORESOURCE_IRQ,
739         },
740         {
741                 .name = "USB CURR",
742                 .start = WM831X_IRQ_PPM_USB_CURR,
743                 .end   = WM831X_IRQ_PPM_USB_CURR,
744                 .flags = IORESOURCE_IRQ,
745         },
746         {
747                 .name = "BATT HOT",
748                 .start = WM831X_IRQ_CHG_BATT_HOT,
749                 .end   = WM831X_IRQ_CHG_BATT_HOT,
750                 .flags = IORESOURCE_IRQ,
751         },
752         {
753                 .name = "BATT COLD",
754                 .start = WM831X_IRQ_CHG_BATT_COLD,
755                 .end   = WM831X_IRQ_CHG_BATT_COLD,
756                 .flags = IORESOURCE_IRQ,
757         },
758         {
759                 .name = "BATT FAIL",
760                 .start = WM831X_IRQ_CHG_BATT_FAIL,
761                 .end   = WM831X_IRQ_CHG_BATT_FAIL,
762                 .flags = IORESOURCE_IRQ,
763         },
764         {
765                 .name = "OV",
766                 .start = WM831X_IRQ_CHG_OV,
767                 .end   = WM831X_IRQ_CHG_OV,
768                 .flags = IORESOURCE_IRQ,
769         },
770         {
771                 .name = "END",
772                 .start = WM831X_IRQ_CHG_END,
773                 .end   = WM831X_IRQ_CHG_END,
774                 .flags = IORESOURCE_IRQ,
775         },
776         {
777                 .name = "TO",
778                 .start = WM831X_IRQ_CHG_TO,
779                 .end   = WM831X_IRQ_CHG_TO,
780                 .flags = IORESOURCE_IRQ,
781         },
782         {
783                 .name = "MODE",
784                 .start = WM831X_IRQ_CHG_MODE,
785                 .end   = WM831X_IRQ_CHG_MODE,
786                 .flags = IORESOURCE_IRQ,
787         },
788         {
789                 .name = "START",
790                 .start = WM831X_IRQ_CHG_START,
791                 .end   = WM831X_IRQ_CHG_START,
792                 .flags = IORESOURCE_IRQ,
793         },
794 };
795
796 static struct resource wm831x_rtc_resources[] = {
797         {
798                 .name = "PER",
799                 .start = WM831X_IRQ_RTC_PER,
800                 .end   = WM831X_IRQ_RTC_PER,
801                 .flags = IORESOURCE_IRQ,
802         },
803         {
804                 .name = "ALM",
805                 .start = WM831X_IRQ_RTC_ALM,
806                 .end   = WM831X_IRQ_RTC_ALM,
807                 .flags = IORESOURCE_IRQ,
808         },
809 };
810
811 static struct resource wm831x_status1_resources[] = {
812         {
813                 .start = WM831X_STATUS_LED_1,
814                 .end   = WM831X_STATUS_LED_1,
815                 .flags = IORESOURCE_IO,
816         },
817 };
818
819 static struct resource wm831x_status2_resources[] = {
820         {
821                 .start = WM831X_STATUS_LED_2,
822                 .end   = WM831X_STATUS_LED_2,
823                 .flags = IORESOURCE_IO,
824         },
825 };
826
827 static struct resource wm831x_touch_resources[] = {
828         {
829                 .name = "TCHPD",
830                 .start = WM831X_IRQ_TCHPD,
831                 .end   = WM831X_IRQ_TCHPD,
832                 .flags = IORESOURCE_IRQ,
833         },
834         {
835                 .name = "TCHDATA",
836                 .start = WM831X_IRQ_TCHDATA,
837                 .end   = WM831X_IRQ_TCHDATA,
838                 .flags = IORESOURCE_IRQ,
839         },
840 };
841
842 static struct resource wm831x_wdt_resources[] = {
843         {
844                 .start = WM831X_IRQ_WDOG_TO,
845                 .end   = WM831X_IRQ_WDOG_TO,
846                 .flags = IORESOURCE_IRQ,
847         },
848 };
849
850 static struct mfd_cell wm8310_devs[] = {
851         {
852                 .name = "wm831x-backup",
853         },
854         {
855                 .name = "wm831x-buckv",
856                 .id = 1,
857                 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
858                 .resources = wm831x_dcdc1_resources,
859         },
860         {
861                 .name = "wm831x-buckv",
862                 .id = 2,
863                 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
864                 .resources = wm831x_dcdc2_resources,
865         },
866         {
867                 .name = "wm831x-buckp",
868                 .id = 3,
869                 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
870                 .resources = wm831x_dcdc3_resources,
871         },
872         {
873                 .name = "wm831x-boostp",
874                 .id = 4,
875                 .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
876                 .resources = wm831x_dcdc4_resources,
877         },
878         {
879                 .name = "wm831x-epe",
880                 .id = 1,
881         },
882         {
883                 .name = "wm831x-epe",
884                 .id = 2,
885         },
886         {
887                 .name = "wm831x-gpio",
888                 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
889                 .resources = wm831x_gpio_resources,
890         },
891         {
892                 .name = "wm831x-hwmon",
893         },
894         {
895                 .name = "wm831x-isink",
896                 .id = 1,
897                 .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
898                 .resources = wm831x_isink1_resources,
899         },
900         {
901                 .name = "wm831x-isink",
902                 .id = 2,
903                 .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
904                 .resources = wm831x_isink2_resources,
905         },
906         {
907                 .name = "wm831x-ldo",
908                 .id = 1,
909                 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
910                 .resources = wm831x_ldo1_resources,
911         },
912         {
913                 .name = "wm831x-ldo",
914                 .id = 2,
915                 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
916                 .resources = wm831x_ldo2_resources,
917         },
918         {
919                 .name = "wm831x-ldo",
920                 .id = 3,
921                 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
922                 .resources = wm831x_ldo3_resources,
923         },
924         {
925                 .name = "wm831x-ldo",
926                 .id = 4,
927                 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
928                 .resources = wm831x_ldo4_resources,
929         },
930         {
931                 .name = "wm831x-ldo",
932                 .id = 5,
933                 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
934                 .resources = wm831x_ldo5_resources,
935         },
936         {
937                 .name = "wm831x-ldo",
938                 .id = 6,
939                 .num_resources = ARRAY_SIZE(wm831x_ldo6_resources),
940                 .resources = wm831x_ldo6_resources,
941         },
942         {
943                 .name = "wm831x-aldo",
944                 .id = 7,
945                 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
946                 .resources = wm831x_ldo7_resources,
947         },
948         {
949                 .name = "wm831x-aldo",
950                 .id = 8,
951                 .num_resources = ARRAY_SIZE(wm831x_ldo8_resources),
952                 .resources = wm831x_ldo8_resources,
953         },
954         {
955                 .name = "wm831x-aldo",
956                 .id = 9,
957                 .num_resources = ARRAY_SIZE(wm831x_ldo9_resources),
958                 .resources = wm831x_ldo9_resources,
959         },
960         {
961                 .name = "wm831x-aldo",
962                 .id = 10,
963                 .num_resources = ARRAY_SIZE(wm831x_ldo10_resources),
964                 .resources = wm831x_ldo10_resources,
965         },
966         {
967                 .name = "wm831x-alive-ldo",
968                 .id = 11,
969                 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
970                 .resources = wm831x_ldo11_resources,
971         },
972         {
973                 .name = "wm831x-on",
974                 .num_resources = ARRAY_SIZE(wm831x_on_resources),
975                 .resources = wm831x_on_resources,
976         },
977         {
978                 .name = "wm831x-power",
979                 .num_resources = ARRAY_SIZE(wm831x_power_resources),
980                 .resources = wm831x_power_resources,
981         },
982         {
983                 .name = "wm831x-rtc",
984                 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
985                 .resources = wm831x_rtc_resources,
986         },
987         {
988                 .name = "wm831x-status",
989                 .id = 1,
990                 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
991                 .resources = wm831x_status1_resources,
992         },
993         {
994                 .name = "wm831x-status",
995                 .id = 2,
996                 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
997                 .resources = wm831x_status2_resources,
998         },
999         {
1000                 .name = "wm831x-watchdog",
1001                 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
1002                 .resources = wm831x_wdt_resources,
1003         },
1004 #if defined(CONFIG_KEYBOARD_WM831X_GPIO)
1005         {
1006                 .name           = "wm831x_gpio-keys",
1007                 .num_resources  = 0,
1008         },
1009 #endif
1010 #if defined(CONFIG_WM831X_CHARGER_DISPLAY)
1011         {
1012                 .name           = "wm831x_charger_display",
1013                 .num_resources  = 0,
1014         },
1015 #endif
1016
1017
1018 };
1019
1020 static struct mfd_cell wm8311_devs[] = {
1021         {
1022                 .name = "wm831x-backup",
1023         },
1024         {
1025                 .name = "wm831x-buckv",
1026                 .id = 1,
1027                 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
1028                 .resources = wm831x_dcdc1_resources,
1029         },
1030         {
1031                 .name = "wm831x-buckv",
1032                 .id = 2,
1033                 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
1034                 .resources = wm831x_dcdc2_resources,
1035         },
1036         {
1037                 .name = "wm831x-buckp",
1038                 .id = 3,
1039                 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
1040                 .resources = wm831x_dcdc3_resources,
1041         },
1042         {
1043                 .name = "wm831x-boostp",
1044                 .id = 4,
1045                 .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
1046                 .resources = wm831x_dcdc4_resources,
1047         },
1048         {
1049                 .name = "wm831x-epe",
1050                 .id = 1,
1051         },
1052         {
1053                 .name = "wm831x-epe",
1054                 .id = 2,
1055         },
1056         {
1057                 .name = "wm831x-gpio",
1058                 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
1059                 .resources = wm831x_gpio_resources,
1060         },
1061         {
1062                 .name = "wm831x-hwmon",
1063         },
1064         {
1065                 .name = "wm831x-isink",
1066                 .id = 1,
1067                 .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
1068                 .resources = wm831x_isink1_resources,
1069         },
1070         {
1071                 .name = "wm831x-isink",
1072                 .id = 2,
1073                 .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
1074                 .resources = wm831x_isink2_resources,
1075         },
1076         {
1077                 .name = "wm831x-ldo",
1078                 .id = 1,
1079                 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
1080                 .resources = wm831x_ldo1_resources,
1081         },
1082         {
1083                 .name = "wm831x-ldo",
1084                 .id = 2,
1085                 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
1086                 .resources = wm831x_ldo2_resources,
1087         },
1088         {
1089                 .name = "wm831x-ldo",
1090                 .id = 3,
1091                 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
1092                 .resources = wm831x_ldo3_resources,
1093         },
1094         {
1095                 .name = "wm831x-ldo",
1096                 .id = 4,
1097                 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
1098                 .resources = wm831x_ldo4_resources,
1099         },
1100         {
1101                 .name = "wm831x-ldo",
1102                 .id = 5,
1103                 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
1104                 .resources = wm831x_ldo5_resources,
1105         },
1106         {
1107                 .name = "wm831x-aldo",
1108                 .id = 7,
1109                 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
1110                 .resources = wm831x_ldo7_resources,
1111         },
1112         {
1113                 .name = "wm831x-alive-ldo",
1114                 .id = 11,
1115                 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
1116                 .resources = wm831x_ldo11_resources,
1117         },
1118         {
1119                 .name = "wm831x-on",
1120                 .num_resources = ARRAY_SIZE(wm831x_on_resources),
1121                 .resources = wm831x_on_resources,
1122         },
1123         {
1124                 .name = "wm831x-power",
1125                 .num_resources = ARRAY_SIZE(wm831x_power_resources),
1126                 .resources = wm831x_power_resources,
1127         },
1128         {
1129                 .name = "wm831x-rtc",
1130                 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
1131                 .resources = wm831x_rtc_resources,
1132         },
1133         {
1134                 .name = "wm831x-status",
1135                 .id = 1,
1136                 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
1137                 .resources = wm831x_status1_resources,
1138         },
1139         {
1140                 .name = "wm831x-status",
1141                 .id = 2,
1142                 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
1143                 .resources = wm831x_status2_resources,
1144         },
1145         {
1146                 .name = "wm831x-touch",
1147                 .num_resources = ARRAY_SIZE(wm831x_touch_resources),
1148                 .resources = wm831x_touch_resources,
1149         },
1150         {
1151                 .name = "wm831x-watchdog",
1152                 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
1153                 .resources = wm831x_wdt_resources,
1154         },
1155 };
1156
1157 static struct mfd_cell wm8312_devs[] = {
1158         {
1159                 .name = "wm831x-backup",
1160         },
1161         {
1162                 .name = "wm831x-buckv",
1163                 .id = 1,
1164                 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
1165                 .resources = wm831x_dcdc1_resources,
1166         },
1167         {
1168                 .name = "wm831x-buckv",
1169                 .id = 2,
1170                 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
1171                 .resources = wm831x_dcdc2_resources,
1172         },
1173         {
1174                 .name = "wm831x-buckp",
1175                 .id = 3,
1176                 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
1177                 .resources = wm831x_dcdc3_resources,
1178         },
1179         {
1180                 .name = "wm831x-boostp",
1181                 .id = 4,
1182                 .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
1183                 .resources = wm831x_dcdc4_resources,
1184         },
1185         {
1186                 .name = "wm831x-epe",
1187                 .id = 1,
1188         },
1189         {
1190                 .name = "wm831x-epe",
1191                 .id = 2,
1192         },
1193         {
1194                 .name = "wm831x-gpio",
1195                 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
1196                 .resources = wm831x_gpio_resources,
1197         },
1198         {
1199                 .name = "wm831x-hwmon",
1200         },
1201         {
1202                 .name = "wm831x-isink",
1203                 .id = 1,
1204                 .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
1205                 .resources = wm831x_isink1_resources,
1206         },
1207         {
1208                 .name = "wm831x-isink",
1209                 .id = 2,
1210                 .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
1211                 .resources = wm831x_isink2_resources,
1212         },
1213         {
1214                 .name = "wm831x-ldo",
1215                 .id = 1,
1216                 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
1217                 .resources = wm831x_ldo1_resources,
1218         },
1219         {
1220                 .name = "wm831x-ldo",
1221                 .id = 2,
1222                 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
1223                 .resources = wm831x_ldo2_resources,
1224         },
1225         {
1226                 .name = "wm831x-ldo",
1227                 .id = 3,
1228                 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
1229                 .resources = wm831x_ldo3_resources,
1230         },
1231         {
1232                 .name = "wm831x-ldo",
1233                 .id = 4,
1234                 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
1235                 .resources = wm831x_ldo4_resources,
1236         },
1237         {
1238                 .name = "wm831x-ldo",
1239                 .id = 5,
1240                 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
1241                 .resources = wm831x_ldo5_resources,
1242         },
1243         {
1244                 .name = "wm831x-ldo",
1245                 .id = 6,
1246                 .num_resources = ARRAY_SIZE(wm831x_ldo6_resources),
1247                 .resources = wm831x_ldo6_resources,
1248         },
1249         {
1250                 .name = "wm831x-aldo",
1251                 .id = 7,
1252                 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
1253                 .resources = wm831x_ldo7_resources,
1254         },
1255         {
1256                 .name = "wm831x-aldo",
1257                 .id = 8,
1258                 .num_resources = ARRAY_SIZE(wm831x_ldo8_resources),
1259                 .resources = wm831x_ldo8_resources,
1260         },
1261         {
1262                 .name = "wm831x-aldo",
1263                 .id = 9,
1264                 .num_resources = ARRAY_SIZE(wm831x_ldo9_resources),
1265                 .resources = wm831x_ldo9_resources,
1266         },
1267         {
1268                 .name = "wm831x-aldo",
1269                 .id = 10,
1270                 .num_resources = ARRAY_SIZE(wm831x_ldo10_resources),
1271                 .resources = wm831x_ldo10_resources,
1272         },
1273         {
1274                 .name = "wm831x-alive-ldo",
1275                 .id = 11,
1276                 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
1277                 .resources = wm831x_ldo11_resources,
1278         },
1279         {
1280                 .name = "wm831x-on",
1281                 .num_resources = ARRAY_SIZE(wm831x_on_resources),
1282                 .resources = wm831x_on_resources,
1283         },
1284         {
1285                 .name = "wm831x-power",
1286                 .num_resources = ARRAY_SIZE(wm831x_power_resources),
1287                 .resources = wm831x_power_resources,
1288         },
1289         {
1290                 .name = "wm831x-rtc",
1291                 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
1292                 .resources = wm831x_rtc_resources,
1293         },
1294         {
1295                 .name = "wm831x-status",
1296                 .id = 1,
1297                 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
1298                 .resources = wm831x_status1_resources,
1299         },
1300         {
1301                 .name = "wm831x-status",
1302                 .id = 2,
1303                 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
1304                 .resources = wm831x_status2_resources,
1305         },
1306         {
1307                 .name = "wm831x-touch",
1308                 .num_resources = ARRAY_SIZE(wm831x_touch_resources),
1309                 .resources = wm831x_touch_resources,
1310         },
1311         {
1312                 .name = "wm831x-watchdog",
1313                 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
1314                 .resources = wm831x_wdt_resources,
1315         },
1316 };
1317
1318 static struct mfd_cell wm8320_devs[] = {
1319         {
1320                 .name = "wm831x-backup",
1321         },
1322         {
1323                 .name = "wm831x-buckv",
1324                 .id = 1,
1325                 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
1326                 .resources = wm831x_dcdc1_resources,
1327         },
1328         {
1329                 .name = "wm831x-buckv",
1330                 .id = 2,
1331                 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
1332                 .resources = wm831x_dcdc2_resources,
1333         },
1334         {
1335                 .name = "wm831x-buckp",
1336                 .id = 3,
1337                 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
1338                 .resources = wm831x_dcdc3_resources,
1339         },
1340         {
1341                 .name = "wm831x-buckp",
1342                 .id = 4,
1343                 .num_resources = ARRAY_SIZE(wm8320_dcdc4_buck_resources),
1344                 .resources = wm8320_dcdc4_buck_resources,
1345         },
1346         {
1347                 .name = "wm831x-gpio",
1348                 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
1349                 .resources = wm831x_gpio_resources,
1350         },
1351         {
1352                 .name = "wm831x-hwmon",
1353         },
1354         {
1355                 .name = "wm831x-ldo",
1356                 .id = 1,
1357                 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
1358                 .resources = wm831x_ldo1_resources,
1359         },
1360         {
1361                 .name = "wm831x-ldo",
1362                 .id = 2,
1363                 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
1364                 .resources = wm831x_ldo2_resources,
1365         },
1366         {
1367                 .name = "wm831x-ldo",
1368                 .id = 3,
1369                 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
1370                 .resources = wm831x_ldo3_resources,
1371         },
1372         {
1373                 .name = "wm831x-ldo",
1374                 .id = 4,
1375                 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
1376                 .resources = wm831x_ldo4_resources,
1377         },
1378         {
1379                 .name = "wm831x-ldo",
1380                 .id = 5,
1381                 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
1382                 .resources = wm831x_ldo5_resources,
1383         },
1384         {
1385                 .name = "wm831x-ldo",
1386                 .id = 6,
1387                 .num_resources = ARRAY_SIZE(wm831x_ldo6_resources),
1388                 .resources = wm831x_ldo6_resources,
1389         },
1390         {
1391                 .name = "wm831x-aldo",
1392                 .id = 7,
1393                 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
1394                 .resources = wm831x_ldo7_resources,
1395         },
1396         {
1397                 .name = "wm831x-aldo",
1398                 .id = 8,
1399                 .num_resources = ARRAY_SIZE(wm831x_ldo8_resources),
1400                 .resources = wm831x_ldo8_resources,
1401         },
1402         {
1403                 .name = "wm831x-aldo",
1404                 .id = 9,
1405                 .num_resources = ARRAY_SIZE(wm831x_ldo9_resources),
1406                 .resources = wm831x_ldo9_resources,
1407         },
1408         {
1409                 .name = "wm831x-aldo",
1410                 .id = 10,
1411                 .num_resources = ARRAY_SIZE(wm831x_ldo10_resources),
1412                 .resources = wm831x_ldo10_resources,
1413         },
1414         {
1415                 .name = "wm831x-alive-ldo",
1416                 .id = 11,
1417                 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
1418                 .resources = wm831x_ldo11_resources,
1419         },
1420         {
1421                 .name = "wm831x-on",
1422                 .num_resources = ARRAY_SIZE(wm831x_on_resources),
1423                 .resources = wm831x_on_resources,
1424         },
1425         {
1426                 .name = "wm831x-rtc",
1427                 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
1428                 .resources = wm831x_rtc_resources,
1429         },
1430         {
1431                 .name = "wm831x-status",
1432                 .id = 1,
1433                 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
1434                 .resources = wm831x_status1_resources,
1435         },
1436         {
1437                 .name = "wm831x-status",
1438                 .id = 2,
1439                 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
1440                 .resources = wm831x_status2_resources,
1441         },
1442         {
1443                 .name = "wm831x-watchdog",
1444                 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
1445                 .resources = wm831x_wdt_resources,
1446         },
1447 };
1448
1449 static struct mfd_cell backlight_devs[] = {
1450         {
1451                 .name = "wm831x-backlight",
1452         },
1453 };
1454
1455 /*
1456  * Instantiate the generic non-control parts of the device.
1457  */
1458 int wm831x_device_init(struct wm831x *wm831x, unsigned long id, int irq)
1459 {
1460         struct wm831x_pdata *pdata = wm831x->dev->platform_data;
1461         int rev;
1462         enum wm831x_parent parent;
1463         int ret, i;
1464
1465         mutex_init(&wm831x->io_lock);
1466         mutex_init(&wm831x->key_lock);
1467         mutex_init(&wm831x->auxadc_lock);
1468         init_completion(&wm831x->auxadc_done);
1469         dev_set_drvdata(wm831x->dev, wm831x);
1470
1471         ret = wm831x_reg_read(wm831x, WM831X_PARENT_ID);
1472         if (ret < 0) {
1473                 dev_err(wm831x->dev, "Failed to read parent ID: %d\n", ret);
1474                 goto err;
1475         }
1476         switch (ret) {
1477         case 0x6204:
1478         case 0x6246:
1479                 break;
1480         default:
1481                 dev_err(wm831x->dev, "Device is not a WM831x: ID %x\n", ret);
1482                 ret = -EINVAL;
1483                 goto err;
1484         }
1485
1486         ret = wm831x_reg_read(wm831x, WM831X_REVISION);
1487         if (ret < 0) {
1488                 dev_err(wm831x->dev, "Failed to read revision: %d\n", ret);
1489                 goto err;
1490         }
1491         rev = (ret & WM831X_PARENT_REV_MASK) >> WM831X_PARENT_REV_SHIFT;
1492
1493         ret = wm831x_reg_read(wm831x, WM831X_RESET_ID);
1494         if (ret < 0) {
1495                 dev_err(wm831x->dev, "Failed to read device ID: %d\n", ret);
1496                 goto err;
1497         }
1498
1499         /* Some engineering samples do not have the ID set, rely on
1500          * the device being registered correctly.
1501          */
1502         if (ret == 0) {
1503                 dev_info(wm831x->dev, "Device is an engineering sample\n");
1504                 ret = id;
1505         }
1506
1507         switch (ret) {
1508         case WM8310:
1509                 parent = WM8310;
1510                 wm831x->num_gpio = 12;
1511                 wm831x->charger_irq_wake = 1;
1512                 if (rev > 0) {
1513                         wm831x->has_gpio_ena = 1;
1514                         wm831x->has_cs_sts = 1;
1515                 }
1516                 //ILIM = 900ma
1517                 ret = wm831x_reg_read(wm831x, WM831X_POWER_STATE) & 0xffff;
1518                 wm831x_reg_write(wm831x, WM831X_POWER_STATE, (ret&0xfff8) | 0x04);      
1519
1520                 dev_info(wm831x->dev, "WM8310 revision %c\n", 'A' + rev);
1521                 break;
1522
1523         case WM8311:
1524                 parent = WM8311;
1525                 wm831x->num_gpio = 16;
1526                 wm831x->charger_irq_wake = 1;
1527                 if (rev > 0) {
1528                         wm831x->has_gpio_ena = 1;
1529                         wm831x->has_cs_sts = 1;
1530                 }
1531
1532                 dev_info(wm831x->dev, "WM8311 revision %c\n", 'A' + rev);
1533                 break;
1534
1535         case WM8312:
1536                 parent = WM8312;
1537                 wm831x->num_gpio = 16;
1538                 wm831x->charger_irq_wake = 1;
1539                 if (rev > 0) {
1540                         wm831x->has_gpio_ena = 1;
1541                         wm831x->has_cs_sts = 1;
1542                 }
1543
1544                 dev_info(wm831x->dev, "WM8312 revision %c\n", 'A' + rev);
1545                 break;
1546
1547         case WM8320:
1548                 parent = WM8320;
1549                 wm831x->num_gpio = 12;
1550                 dev_info(wm831x->dev, "WM8320 revision %c\n", 'A' + rev);
1551                 break;
1552
1553         case WM8321:
1554                 parent = WM8321;
1555                 wm831x->num_gpio = 12;
1556                 dev_info(wm831x->dev, "WM8321 revision %c\n", 'A' + rev);
1557                 break;
1558
1559         case WM8325:
1560                 parent = WM8325;
1561                 wm831x->num_gpio = 12;
1562                 dev_info(wm831x->dev, "WM8325 revision %c\n", 'A' + rev);
1563                 break;
1564
1565         case WM8326:
1566                 parent = WM8326;
1567                 wm831x->num_gpio = 12;
1568                 dev_info(wm831x->dev, "WM8326 revision %c\n", 'A' + rev);
1569                 break;
1570
1571         default:
1572                 dev_err(wm831x->dev, "Unknown WM831x device %04x\n", ret);
1573                 ret = -EINVAL;
1574                 goto err;
1575         }
1576
1577         /* This will need revisiting in future but is OK for all
1578          * current parts.
1579          */
1580         if (parent != id)
1581                 dev_warn(wm831x->dev, "Device was registered as a WM%lx\n",
1582                          id);
1583
1584         /* Bootstrap the user key */
1585         ret = wm831x_reg_read(wm831x, WM831X_SECURITY_KEY);
1586         if (ret < 0) {
1587                 dev_err(wm831x->dev, "Failed to read security key: %d\n", ret);
1588                 goto err;
1589         }
1590         if (ret != 0) {
1591                 dev_warn(wm831x->dev, "Security key had non-zero value %x\n",
1592                          ret);
1593                 wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0);
1594         }
1595         wm831x->locked = 1;
1596
1597         if (pdata && pdata->pre_init) {
1598                 ret = pdata->pre_init(wm831x);
1599                 if (ret != 0) {
1600                         dev_err(wm831x->dev, "pre_init() failed: %d\n", ret);
1601                         goto err;
1602                 }
1603         }
1604
1605         if (pdata) {
1606                 for (i = 0; i < ARRAY_SIZE(pdata->gpio_defaults); i++) {
1607                         if (!pdata->gpio_defaults[i])
1608                                 continue;
1609
1610                         wm831x_reg_write(wm831x,
1611                                          WM831X_GPIO1_CONTROL + i,
1612                                          pdata->gpio_defaults[i] & 0xffff);
1613                 }
1614         }
1615
1616         ret = wm831x_irq_init(wm831x, irq);
1617         if (ret != 0)
1618                 goto err;
1619
1620         if (wm831x->irq_base) {
1621                 ret = request_threaded_irq(wm831x->irq_base +
1622                                            WM831X_IRQ_AUXADC_DATA,
1623                                            NULL, wm831x_auxadc_irq, 0,
1624                                            "auxadc", wm831x);
1625                 if (ret < 0)
1626                         dev_err(wm831x->dev, "AUXADC IRQ request failed: %d\n",
1627                                 ret);
1628         }
1629
1630         /* The core device is up, instantiate the subdevices. */
1631         switch (parent) {
1632         case WM8310:
1633                 ret = mfd_add_devices(wm831x->dev, -1,
1634                                       wm8310_devs, ARRAY_SIZE(wm8310_devs),
1635                                       NULL, wm831x->irq_base);
1636                 break;
1637
1638         case WM8311:
1639                 ret = mfd_add_devices(wm831x->dev, -1,
1640                                       wm8311_devs, ARRAY_SIZE(wm8311_devs),
1641                                       NULL, wm831x->irq_base);
1642                 break;
1643
1644         case WM8312:
1645                 ret = mfd_add_devices(wm831x->dev, -1,
1646                                       wm8312_devs, ARRAY_SIZE(wm8312_devs),
1647                                       NULL, wm831x->irq_base);
1648                 break;
1649
1650         case WM8320:
1651         case WM8321:
1652         case WM8325:
1653         case WM8326:
1654                 ret = mfd_add_devices(wm831x->dev, -1,
1655                                       wm8320_devs, ARRAY_SIZE(wm8320_devs),
1656                                       NULL, wm831x->irq_base);
1657                 break;
1658
1659         default:
1660                 /* If this happens the bus probe function is buggy */
1661                 BUG();
1662         }
1663
1664         if (ret != 0) {
1665                 dev_err(wm831x->dev, "Failed to add children\n");
1666                 goto err_irq;
1667         }
1668
1669         if (pdata && pdata->backlight) {
1670                 /* Treat errors as non-critical */
1671                 ret = mfd_add_devices(wm831x->dev, -1, backlight_devs,
1672                                       ARRAY_SIZE(backlight_devs), NULL,
1673                                       wm831x->irq_base);
1674                 if (ret < 0)
1675                         dev_err(wm831x->dev, "Failed to add backlight: %d\n",
1676                                 ret);
1677         }
1678
1679         wm831x_otp_init(wm831x);
1680
1681         if (pdata && pdata->post_init) {
1682                 wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0x9716); //wm831x_reg_unlock
1683                 wm831x_set_bits(wm831x, WM831X_RESET_CONTROL,0x0010,0x0000);
1684                 wm831x_set_bits(wm831x, WM831X_LDO_ENABLE,0Xf800,0Xf800);
1685                 ret = pdata->post_init(wm831x);
1686                 wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0x0000);   //wm831x_reg_lock
1687                 if (ret != 0) {
1688                         dev_err(wm831x->dev, "post_init() failed: %d\n", ret);
1689                         goto err_irq;
1690                 }
1691         }
1692         
1693         return 0;
1694
1695 err_irq:
1696         wm831x_irq_exit(wm831x);
1697 err:
1698         mfd_remove_devices(wm831x->dev);
1699         kfree(wm831x);
1700         return ret;
1701 }
1702
1703 void wm831x_device_exit(struct wm831x *wm831x)
1704 {
1705         wm831x_otp_exit(wm831x);
1706         mfd_remove_devices(wm831x->dev);
1707         if (wm831x->irq_base)
1708                 free_irq(wm831x->irq_base + WM831X_IRQ_AUXADC_DATA, wm831x);
1709         wm831x_irq_exit(wm831x);
1710         kfree(wm831x);
1711 }
1712
1713 int wm831x_device_suspend(struct wm831x *wm831x)
1714 {
1715         int reg, mask;
1716         int i;
1717         
1718         //mask some intterupt avoid wakeing up system while suspending
1719         for (i = 0; i < ARRAY_SIZE(wm831x->irq_masks_cur); i++) {
1720                 /* If there's been a change in the mask write it back
1721                  * to the hardware. */
1722                 //printk("irq_masks_cur[%d]=0x%x\n",i,wm831x->irq_masks_cur[i]);
1723
1724                 if (wm831x->irq_masks_cur[i] != wm831x->irq_masks_cache[i]) {
1725                         wm831x->irq_masks_cache[i] = wm831x->irq_masks_cur[i];
1726                         wm831x_reg_write(wm831x,
1727                                          WM831X_INTERRUPT_STATUS_1_MASK + i,
1728                                          wm831x->irq_masks_cur[i]);
1729                 }
1730         
1731         }
1732
1733         /* If the charger IRQs are a wake source then make sure we ack
1734          * them even if they're not actively being used (eg, no power
1735          * driver or no IRQ line wired up) then acknowledge the
1736          * interrupts otherwise suspend won't last very long.
1737          */
1738         if (wm831x->charger_irq_wake) {
1739                 reg = wm831x_reg_read(wm831x, WM831X_INTERRUPT_STATUS_2_MASK);
1740
1741                 mask = WM831X_CHG_BATT_HOT_EINT |
1742                         WM831X_CHG_BATT_COLD_EINT |
1743                         WM831X_CHG_BATT_FAIL_EINT |
1744                         WM831X_CHG_OV_EINT | WM831X_CHG_END_EINT |
1745                         WM831X_CHG_TO_EINT | WM831X_CHG_MODE_EINT |
1746                         WM831X_CHG_START_EINT;
1747
1748                 /* If any of the interrupts are masked read the statuses */
1749                 if (reg & mask)
1750                         reg = wm831x_reg_read(wm831x,
1751                                               WM831X_INTERRUPT_STATUS_2);
1752
1753                 if (reg & mask) {
1754                         dev_info(wm831x->dev,
1755                                  "Acknowledging masked charger IRQs: %x\n",
1756                                  reg & mask);
1757                         wm831x_reg_write(wm831x, WM831X_INTERRUPT_STATUS_2,
1758                                          reg & mask);
1759                 }
1760         }
1761
1762         return 0;
1763 }
1764
1765 void wm831x_enter_sleep(void){
1766         struct regulator *dcdc = regulator_get(NULL, "dcdc1");
1767         int i;          
1768         struct wm831x_dcdc *dc = regulator_get_drvdata(dcdc);
1769         struct wm831x *wm831x = dc->wm831x;
1770         if(wm831x){
1771                 wm831x_set_bits(wm831x, WM831X_POWER_STATE, 0x4000, 0x4000); // SYSTEM SLEEP MODE
1772                 for (i=0; i<5; i++)
1773                         wm831x_reg_write(wm831x,WM831X_INTERRUPT_STATUS_1+i, 0xffff);  // INTRUPT FLAG CLEAR 
1774                         
1775                 printk("%s:complete! \n",__func__);
1776                 
1777         }else{
1778                 printk("%s:error!",__func__);
1779         }
1780         regulator_put(dcdc);
1781 }
1782 EXPORT_SYMBOL_GPL(wm831x_enter_sleep);
1783
1784 void wm831x_exit_sleep(void){
1785         struct regulator *dcdc = regulator_get(NULL, "dcdc1");
1786         struct wm831x_dcdc *dc = regulator_get_drvdata(dcdc);
1787         struct wm831x *wm831x = dc->wm831x;
1788         if(wm831x){
1789                 wm831x_set_bits(wm831x, WM831X_POWER_STATE, 0x4000, 0);  // SYSTEM ON MODE
1790                 printk("%s:complete! \n",__func__);
1791                 
1792         }else{
1793                 printk("%s:error!",__func__);
1794         }
1795         regulator_put(dcdc);
1796 }
1797 EXPORT_SYMBOL_GPL(wm831x_exit_sleep);
1798
1799 int wm831x_device_shutdown(struct wm831x *wm831x)
1800 {
1801         struct wm831x_pdata *pdata = wm831x->dev->platform_data;
1802         int ret = 0;
1803         
1804         printk("pre WM831X_POWER_STATE = 0x%x\n", wm831x_reg_read(wm831x, WM831X_POWER_STATE));
1805
1806         if (pdata && pdata->last_deinit) {
1807                 ret = pdata->last_deinit(wm831x);
1808                 if (ret != 0) {
1809                         dev_info(wm831x->dev, "last_deinit() failed: %d\n", ret);
1810                 }
1811         }
1812
1813         //if(0 == reboot_cmd_get())
1814         {
1815                 if(wm831x_set_bits(wm831x, WM831X_POWER_STATE, WM831X_CHIP_ON_MASK, 0) < 0)
1816                         printk("%s wm831x_set_bits err\n", __FUNCTION__);
1817                 //printk("post WM831X_POWER_STATE = 0x%x\n", wm831x_reg_read(wm831x, WM831X_POWER_STATE));
1818         }
1819
1820         return 0;       
1821 }
1822
1823 EXPORT_SYMBOL_GPL(wm831x_device_shutdown);
1824
1825
1826 int wm831x_read_usb(struct wm831x *wm831x)
1827 {
1828         int ret, usb_chg = 0, wall_chg = 0;
1829         
1830         ret = wm831x_reg_read(wm831x, WM831X_SYSTEM_STATUS);
1831         if (ret < 0)
1832                 return ret;
1833
1834         if (ret & WM831X_PWR_USB)
1835                 usb_chg = 1;
1836         if (ret & WM831X_PWR_WALL)
1837                 wall_chg = 1;
1838
1839         return ((usb_chg | wall_chg) ? 1 : 0);
1840
1841 }
1842
1843
1844 int wm831x_device_restart(struct wm831x *wm831x)
1845 {
1846         wm831x_reg_write(wm831x,WM831X_RESET_ID, 0xffff); 
1847
1848         return 0;
1849 }
1850
1851
1852 MODULE_DESCRIPTION("Core support for the WM831X AudioPlus PMIC");
1853 MODULE_LICENSE("GPL");
1854 MODULE_AUTHOR("Mark Brown");