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