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