wm8994-core.c revision 20fb277250816d6c3ff326552be0fea4173fd8ca
1/*
2 * wm8994-core.c  --  Device access for Wolfson WM8994
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/slab.h>
18#include <linux/i2c.h>
19#include <linux/err.h>
20#include <linux/delay.h>
21#include <linux/mfd/core.h>
22#include <linux/of.h>
23#include <linux/of_device.h>
24#include <linux/of_gpio.h>
25#include <linux/pm_runtime.h>
26#include <linux/regmap.h>
27#include <linux/regulator/consumer.h>
28#include <linux/regulator/machine.h>
29
30#include <linux/mfd/wm8994/core.h>
31#include <linux/mfd/wm8994/pdata.h>
32#include <linux/mfd/wm8994/registers.h>
33
34#include "wm8994.h"
35
36/**
37 * wm8994_reg_read: Read a single WM8994 register.
38 *
39 * @wm8994: Device to read from.
40 * @reg: Register to read.
41 */
42int wm8994_reg_read(struct wm8994 *wm8994, unsigned short reg)
43{
44	unsigned int val;
45	int ret;
46
47	ret = regmap_read(wm8994->regmap, reg, &val);
48
49	if (ret < 0)
50		return ret;
51	else
52		return val;
53}
54EXPORT_SYMBOL_GPL(wm8994_reg_read);
55
56/**
57 * wm8994_bulk_read: Read multiple WM8994 registers
58 *
59 * @wm8994: Device to read from
60 * @reg: First register
61 * @count: Number of registers
62 * @buf: Buffer to fill.  The data will be returned big endian.
63 */
64int wm8994_bulk_read(struct wm8994 *wm8994, unsigned short reg,
65		     int count, u16 *buf)
66{
67	return regmap_bulk_read(wm8994->regmap, reg, buf, count);
68}
69
70/**
71 * wm8994_reg_write: Write a single WM8994 register.
72 *
73 * @wm8994: Device to write to.
74 * @reg: Register to write to.
75 * @val: Value to write.
76 */
77int wm8994_reg_write(struct wm8994 *wm8994, unsigned short reg,
78		     unsigned short val)
79{
80	return regmap_write(wm8994->regmap, reg, val);
81}
82EXPORT_SYMBOL_GPL(wm8994_reg_write);
83
84/**
85 * wm8994_bulk_write: Write multiple WM8994 registers
86 *
87 * @wm8994: Device to write to
88 * @reg: First register
89 * @count: Number of registers
90 * @buf: Buffer to write from.  Data must be big-endian formatted.
91 */
92int wm8994_bulk_write(struct wm8994 *wm8994, unsigned short reg,
93		      int count, const u16 *buf)
94{
95	return regmap_raw_write(wm8994->regmap, reg, buf, count * sizeof(u16));
96}
97EXPORT_SYMBOL_GPL(wm8994_bulk_write);
98
99/**
100 * wm8994_set_bits: Set the value of a bitfield in a WM8994 register
101 *
102 * @wm8994: Device to write to.
103 * @reg: Register to write to.
104 * @mask: Mask of bits to set.
105 * @val: Value to set (unshifted)
106 */
107int wm8994_set_bits(struct wm8994 *wm8994, unsigned short reg,
108		    unsigned short mask, unsigned short val)
109{
110	return regmap_update_bits(wm8994->regmap, reg, mask, val);
111}
112EXPORT_SYMBOL_GPL(wm8994_set_bits);
113
114static struct mfd_cell wm8994_regulator_devs[] = {
115	{
116		.name = "wm8994-ldo",
117		.id = 1,
118		.pm_runtime_no_callbacks = true,
119	},
120	{
121		.name = "wm8994-ldo",
122		.id = 2,
123		.pm_runtime_no_callbacks = true,
124	},
125};
126
127static struct resource wm8994_codec_resources[] = {
128	{
129		.start = WM8994_IRQ_TEMP_SHUT,
130		.end   = WM8994_IRQ_TEMP_WARN,
131		.flags = IORESOURCE_IRQ,
132	},
133};
134
135static struct resource wm8994_gpio_resources[] = {
136	{
137		.start = WM8994_IRQ_GPIO(1),
138		.end   = WM8994_IRQ_GPIO(11),
139		.flags = IORESOURCE_IRQ,
140	},
141};
142
143static struct mfd_cell wm8994_devs[] = {
144	{
145		.name = "wm8994-codec",
146		.num_resources = ARRAY_SIZE(wm8994_codec_resources),
147		.resources = wm8994_codec_resources,
148	},
149
150	{
151		.name = "wm8994-gpio",
152		.num_resources = ARRAY_SIZE(wm8994_gpio_resources),
153		.resources = wm8994_gpio_resources,
154		.pm_runtime_no_callbacks = true,
155	},
156};
157
158/*
159 * Supplies for the main bulk of CODEC; the LDO supplies are ignored
160 * and should be handled via the standard regulator API supply
161 * management.
162 */
163static const char *wm1811_main_supplies[] = {
164	"DBVDD1",
165	"DBVDD2",
166	"DBVDD3",
167	"DCVDD",
168	"AVDD1",
169	"AVDD2",
170	"CPVDD",
171	"SPKVDD1",
172	"SPKVDD2",
173};
174
175static const char *wm8994_main_supplies[] = {
176	"DBVDD",
177	"DCVDD",
178	"AVDD1",
179	"AVDD2",
180	"CPVDD",
181	"SPKVDD1",
182	"SPKVDD2",
183};
184
185static const char *wm8958_main_supplies[] = {
186	"DBVDD1",
187	"DBVDD2",
188	"DBVDD3",
189	"DCVDD",
190	"AVDD1",
191	"AVDD2",
192	"CPVDD",
193	"SPKVDD1",
194	"SPKVDD2",
195};
196
197#ifdef CONFIG_PM_RUNTIME
198static int wm8994_suspend(struct device *dev)
199{
200	struct wm8994 *wm8994 = dev_get_drvdata(dev);
201	int ret;
202
203	/* Don't actually go through with the suspend if the CODEC is
204	 * still active (eg, for audio passthrough from CP. */
205	ret = wm8994_reg_read(wm8994, WM8994_POWER_MANAGEMENT_1);
206	if (ret < 0) {
207		dev_err(dev, "Failed to read power status: %d\n", ret);
208	} else if (ret & WM8994_VMID_SEL_MASK) {
209		dev_dbg(dev, "CODEC still active, ignoring suspend\n");
210		return 0;
211	}
212
213	ret = wm8994_reg_read(wm8994, WM8994_POWER_MANAGEMENT_4);
214	if (ret < 0) {
215		dev_err(dev, "Failed to read power status: %d\n", ret);
216	} else if (ret & (WM8994_AIF2ADCL_ENA | WM8994_AIF2ADCR_ENA |
217			  WM8994_AIF1ADC2L_ENA | WM8994_AIF1ADC2R_ENA |
218			  WM8994_AIF1ADC1L_ENA | WM8994_AIF1ADC1R_ENA)) {
219		dev_dbg(dev, "CODEC still active, ignoring suspend\n");
220		return 0;
221	}
222
223	ret = wm8994_reg_read(wm8994, WM8994_POWER_MANAGEMENT_5);
224	if (ret < 0) {
225		dev_err(dev, "Failed to read power status: %d\n", ret);
226	} else if (ret & (WM8994_AIF2DACL_ENA | WM8994_AIF2DACR_ENA |
227			  WM8994_AIF1DAC2L_ENA | WM8994_AIF1DAC2R_ENA |
228			  WM8994_AIF1DAC1L_ENA | WM8994_AIF1DAC1R_ENA)) {
229		dev_dbg(dev, "CODEC still active, ignoring suspend\n");
230		return 0;
231	}
232
233	switch (wm8994->type) {
234	case WM8958:
235	case WM1811:
236		ret = wm8994_reg_read(wm8994, WM8958_MIC_DETECT_1);
237		if (ret < 0) {
238			dev_err(dev, "Failed to read power status: %d\n", ret);
239		} else if (ret & WM8958_MICD_ENA) {
240			dev_dbg(dev, "CODEC still active, ignoring suspend\n");
241			return 0;
242		}
243		break;
244	default:
245		break;
246	}
247
248	switch (wm8994->type) {
249	case WM1811:
250		ret = wm8994_reg_read(wm8994, WM8994_ANTIPOP_2);
251		if (ret < 0) {
252			dev_err(dev, "Failed to read jackdet: %d\n", ret);
253		} else if (ret & WM1811_JACKDET_MODE_MASK) {
254			dev_dbg(dev, "CODEC still active, ignoring suspend\n");
255			return 0;
256		}
257		break;
258	default:
259		break;
260	}
261
262	switch (wm8994->type) {
263	case WM1811:
264		ret = wm8994_reg_read(wm8994, WM8994_ANTIPOP_2);
265		if (ret < 0) {
266			dev_err(dev, "Failed to read jackdet: %d\n", ret);
267		} else if (ret & WM1811_JACKDET_MODE_MASK) {
268			dev_dbg(dev, "CODEC still active, ignoring suspend\n");
269			return 0;
270		}
271		break;
272	default:
273		break;
274	}
275
276	/* Disable LDO pulldowns while the device is suspended if we
277	 * don't know that something will be driving them. */
278	if (!wm8994->ldo_ena_always_driven)
279		wm8994_set_bits(wm8994, WM8994_PULL_CONTROL_2,
280				WM8994_LDO1ENA_PD | WM8994_LDO2ENA_PD,
281				WM8994_LDO1ENA_PD | WM8994_LDO2ENA_PD);
282
283	/* Explicitly put the device into reset in case regulators
284	 * don't get disabled in order to ensure consistent restart.
285	 */
286	wm8994_reg_write(wm8994, WM8994_SOFTWARE_RESET,
287			 wm8994_reg_read(wm8994, WM8994_SOFTWARE_RESET));
288
289	regcache_mark_dirty(wm8994->regmap);
290
291	/* Restore GPIO registers to prevent problems with mismatched
292	 * pin configurations.
293	 */
294	ret = regcache_sync_region(wm8994->regmap, WM8994_GPIO_1,
295				   WM8994_GPIO_11);
296	if (ret != 0)
297		dev_err(dev, "Failed to restore GPIO registers: %d\n", ret);
298
299	/* In case one of the GPIOs is used as a wake input. */
300	ret = regcache_sync_region(wm8994->regmap,
301				   WM8994_INTERRUPT_STATUS_1_MASK,
302				   WM8994_INTERRUPT_STATUS_1_MASK);
303	if (ret != 0)
304		dev_err(dev, "Failed to restore interrupt mask: %d\n", ret);
305
306	regcache_cache_only(wm8994->regmap, true);
307	wm8994->suspended = true;
308
309	ret = regulator_bulk_disable(wm8994->num_supplies,
310				     wm8994->supplies);
311	if (ret != 0) {
312		dev_err(dev, "Failed to disable supplies: %d\n", ret);
313		return ret;
314	}
315
316	return 0;
317}
318
319static int wm8994_resume(struct device *dev)
320{
321	struct wm8994 *wm8994 = dev_get_drvdata(dev);
322	int ret;
323
324	/* We may have lied to the PM core about suspending */
325	if (!wm8994->suspended)
326		return 0;
327
328	ret = regulator_bulk_enable(wm8994->num_supplies,
329				    wm8994->supplies);
330	if (ret != 0) {
331		dev_err(dev, "Failed to enable supplies: %d\n", ret);
332		return ret;
333	}
334
335	regcache_cache_only(wm8994->regmap, false);
336	ret = regcache_sync(wm8994->regmap);
337	if (ret != 0) {
338		dev_err(dev, "Failed to restore register map: %d\n", ret);
339		goto err_enable;
340	}
341
342	/* Disable LDO pulldowns while the device is active */
343	wm8994_set_bits(wm8994, WM8994_PULL_CONTROL_2,
344			WM8994_LDO1ENA_PD | WM8994_LDO2ENA_PD,
345			0);
346
347	wm8994->suspended = false;
348
349	return 0;
350
351err_enable:
352	regulator_bulk_disable(wm8994->num_supplies, wm8994->supplies);
353
354	return ret;
355}
356#endif
357
358#ifdef CONFIG_REGULATOR
359static int wm8994_ldo_in_use(struct wm8994_pdata *pdata, int ldo)
360{
361	struct wm8994_ldo_pdata *ldo_pdata;
362
363	if (!pdata)
364		return 0;
365
366	ldo_pdata = &pdata->ldo[ldo];
367
368	if (!ldo_pdata->init_data)
369		return 0;
370
371	return ldo_pdata->init_data->num_consumer_supplies != 0;
372}
373#else
374static int wm8994_ldo_in_use(struct wm8994_pdata *pdata, int ldo)
375{
376	return 0;
377}
378#endif
379
380static const struct reg_default wm8994_revc_patch[] = {
381	{ 0x102, 0x3 },
382	{ 0x56, 0x3 },
383	{ 0x817, 0x0 },
384	{ 0x102, 0x0 },
385};
386
387static const struct reg_default wm8958_reva_patch[] = {
388	{ 0x102, 0x3 },
389	{ 0xcb, 0x81 },
390	{ 0x817, 0x0 },
391	{ 0x102, 0x0 },
392};
393
394static const struct reg_default wm1811_reva_patch[] = {
395	{ 0x102, 0x3 },
396	{ 0x56, 0xc07 },
397	{ 0x5d, 0x7e },
398	{ 0x5e, 0x0 },
399	{ 0x102, 0x0 },
400};
401
402#ifdef CONFIG_OF
403static int wm8994_set_pdata_from_of(struct wm8994 *wm8994)
404{
405	struct device_node *np = wm8994->dev->of_node;
406	struct wm8994_pdata *pdata = &wm8994->pdata;
407	int i;
408
409	if (!np)
410		return 0;
411
412	if (of_property_read_u32_array(np, "wlf,gpio-cfg", pdata->gpio_defaults,
413				       ARRAY_SIZE(pdata->gpio_defaults)) >= 0) {
414		for (i = 0; i < ARRAY_SIZE(pdata->gpio_defaults); i++) {
415			if (wm8994->pdata.gpio_defaults[i] == 0)
416				pdata->gpio_defaults[i]
417					= WM8994_CONFIGURE_GPIO;
418		}
419	}
420
421	of_property_read_u32_array(np, "wlf,micbias-cfg", pdata->micbias,
422				   ARRAY_SIZE(pdata->micbias));
423
424	pdata->lineout1_diff = true;
425	pdata->lineout2_diff = true;
426	if (of_find_property(np, "wlf,lineout1-se", NULL))
427		pdata->lineout1_diff = false;
428	if (of_find_property(np, "wlf,lineout2-se", NULL))
429		pdata->lineout2_diff = false;
430
431	if (of_find_property(np, "wlf,lineout1-feedback", NULL))
432		pdata->lineout1fb = true;
433	if (of_find_property(np, "wlf,lineout2-feedback", NULL))
434		pdata->lineout2fb = true;
435
436	if (of_find_property(np, "wlf,ldoena-always-driven", NULL))
437		pdata->lineout2fb = true;
438
439	pdata->ldo[0].enable = of_get_named_gpio(np, "wlf,ldo1ena", 0);
440	if (pdata->ldo[0].enable < 0)
441		pdata->ldo[0].enable = 0;
442
443	pdata->ldo[1].enable = of_get_named_gpio(np, "wlf,ldo2ena", 0);
444	if (pdata->ldo[1].enable < 0)
445		pdata->ldo[1].enable = 0;
446
447	return 0;
448}
449#else
450static int wm8994_set_pdata_from_of(struct wm8994 *wm8994)
451{
452	return 0;
453}
454#endif
455
456/*
457 * Instantiate the generic non-control parts of the device.
458 */
459static int wm8994_device_init(struct wm8994 *wm8994, int irq)
460{
461	struct wm8994_pdata *pdata;
462	struct regmap_config *regmap_config;
463	const struct reg_default *regmap_patch = NULL;
464	const char *devname;
465	int ret, i, patch_regs = 0;
466	int pulls = 0;
467
468	if (dev_get_platdata(wm8994->dev)) {
469		pdata = dev_get_platdata(wm8994->dev);
470		wm8994->pdata = *pdata;
471	}
472	pdata = &wm8994->pdata;
473
474	ret = wm8994_set_pdata_from_of(wm8994);
475	if (ret != 0)
476		return ret;
477
478	dev_set_drvdata(wm8994->dev, wm8994);
479
480	/* Add the on-chip regulators first for bootstrapping */
481	ret = mfd_add_devices(wm8994->dev, -1,
482			      wm8994_regulator_devs,
483			      ARRAY_SIZE(wm8994_regulator_devs),
484			      NULL, 0, NULL);
485	if (ret != 0) {
486		dev_err(wm8994->dev, "Failed to add children: %d\n", ret);
487		goto err;
488	}
489
490	switch (wm8994->type) {
491	case WM1811:
492		wm8994->num_supplies = ARRAY_SIZE(wm1811_main_supplies);
493		break;
494	case WM8994:
495		wm8994->num_supplies = ARRAY_SIZE(wm8994_main_supplies);
496		break;
497	case WM8958:
498		wm8994->num_supplies = ARRAY_SIZE(wm8958_main_supplies);
499		break;
500	default:
501		BUG();
502		goto err;
503	}
504
505	wm8994->supplies = devm_kzalloc(wm8994->dev,
506					sizeof(struct regulator_bulk_data) *
507					wm8994->num_supplies, GFP_KERNEL);
508	if (!wm8994->supplies) {
509		ret = -ENOMEM;
510		goto err;
511	}
512
513	switch (wm8994->type) {
514	case WM1811:
515		for (i = 0; i < ARRAY_SIZE(wm1811_main_supplies); i++)
516			wm8994->supplies[i].supply = wm1811_main_supplies[i];
517		break;
518	case WM8994:
519		for (i = 0; i < ARRAY_SIZE(wm8994_main_supplies); i++)
520			wm8994->supplies[i].supply = wm8994_main_supplies[i];
521		break;
522	case WM8958:
523		for (i = 0; i < ARRAY_SIZE(wm8958_main_supplies); i++)
524			wm8994->supplies[i].supply = wm8958_main_supplies[i];
525		break;
526	default:
527		BUG();
528		goto err;
529	}
530
531	ret = devm_regulator_bulk_get(wm8994->dev, wm8994->num_supplies,
532				 wm8994->supplies);
533	if (ret != 0) {
534		dev_err(wm8994->dev, "Failed to get supplies: %d\n", ret);
535		goto err;
536	}
537
538	ret = regulator_bulk_enable(wm8994->num_supplies,
539				    wm8994->supplies);
540	if (ret != 0) {
541		dev_err(wm8994->dev, "Failed to enable supplies: %d\n", ret);
542		goto err;
543	}
544
545	ret = wm8994_reg_read(wm8994, WM8994_SOFTWARE_RESET);
546	if (ret < 0) {
547		dev_err(wm8994->dev, "Failed to read ID register\n");
548		goto err_enable;
549	}
550	switch (ret) {
551	case 0x1811:
552		devname = "WM1811";
553		if (wm8994->type != WM1811)
554			dev_warn(wm8994->dev, "Device registered as type %d\n",
555				 wm8994->type);
556		wm8994->type = WM1811;
557		break;
558	case 0x8994:
559		devname = "WM8994";
560		if (wm8994->type != WM8994)
561			dev_warn(wm8994->dev, "Device registered as type %d\n",
562				 wm8994->type);
563		wm8994->type = WM8994;
564		break;
565	case 0x8958:
566		devname = "WM8958";
567		if (wm8994->type != WM8958)
568			dev_warn(wm8994->dev, "Device registered as type %d\n",
569				 wm8994->type);
570		wm8994->type = WM8958;
571		break;
572	default:
573		dev_err(wm8994->dev, "Device is not a WM8994, ID is %x\n",
574			ret);
575		ret = -EINVAL;
576		goto err_enable;
577	}
578
579	ret = wm8994_reg_read(wm8994, WM8994_CHIP_REVISION);
580	if (ret < 0) {
581		dev_err(wm8994->dev, "Failed to read revision register: %d\n",
582			ret);
583		goto err_enable;
584	}
585	wm8994->revision = ret & WM8994_CHIP_REV_MASK;
586	wm8994->cust_id = (ret & WM8994_CUST_ID_MASK) >> WM8994_CUST_ID_SHIFT;
587
588	switch (wm8994->type) {
589	case WM8994:
590		switch (wm8994->revision) {
591		case 0:
592		case 1:
593			dev_warn(wm8994->dev,
594				 "revision %c not fully supported\n",
595				 'A' + wm8994->revision);
596			break;
597		case 2:
598		case 3:
599		default:
600			regmap_patch = wm8994_revc_patch;
601			patch_regs = ARRAY_SIZE(wm8994_revc_patch);
602			break;
603		}
604		break;
605
606	case WM8958:
607		switch (wm8994->revision) {
608		case 0:
609			regmap_patch = wm8958_reva_patch;
610			patch_regs = ARRAY_SIZE(wm8958_reva_patch);
611			break;
612		default:
613			break;
614		}
615		break;
616
617	case WM1811:
618		/* Revision C did not change the relevant layer */
619		if (wm8994->revision > 1)
620			wm8994->revision++;
621
622		regmap_patch = wm1811_reva_patch;
623		patch_regs = ARRAY_SIZE(wm1811_reva_patch);
624		break;
625
626	default:
627		break;
628	}
629
630	dev_info(wm8994->dev, "%s revision %c CUST_ID %02x\n", devname,
631		 'A' + wm8994->revision, wm8994->cust_id);
632
633	switch (wm8994->type) {
634	case WM1811:
635		regmap_config = &wm1811_regmap_config;
636		break;
637	case WM8994:
638		regmap_config = &wm8994_regmap_config;
639		break;
640	case WM8958:
641		regmap_config = &wm8958_regmap_config;
642		break;
643	default:
644		dev_err(wm8994->dev, "Unknown device type %d\n", wm8994->type);
645		return -EINVAL;
646	}
647
648	ret = regmap_reinit_cache(wm8994->regmap, regmap_config);
649	if (ret != 0) {
650		dev_err(wm8994->dev, "Failed to reinit register cache: %d\n",
651			ret);
652		return ret;
653	}
654
655	if (regmap_patch) {
656		ret = regmap_register_patch(wm8994->regmap, regmap_patch,
657					    patch_regs);
658		if (ret != 0) {
659			dev_err(wm8994->dev, "Failed to register patch: %d\n",
660				ret);
661			goto err;
662		}
663	}
664
665	wm8994->irq_base = pdata->irq_base;
666	wm8994->gpio_base = pdata->gpio_base;
667
668	/* GPIO configuration is only applied if it's non-zero */
669	for (i = 0; i < ARRAY_SIZE(pdata->gpio_defaults); i++) {
670		if (pdata->gpio_defaults[i]) {
671			wm8994_set_bits(wm8994, WM8994_GPIO_1 + i,
672					0xffff, pdata->gpio_defaults[i]);
673		}
674	}
675
676	wm8994->ldo_ena_always_driven = pdata->ldo_ena_always_driven;
677
678	if (pdata->spkmode_pu)
679		pulls |= WM8994_SPKMODE_PU;
680
681	/* Disable unneeded pulls */
682	wm8994_set_bits(wm8994, WM8994_PULL_CONTROL_2,
683			WM8994_LDO1ENA_PD | WM8994_LDO2ENA_PD |
684			WM8994_SPKMODE_PU | WM8994_CSNADDR_PD,
685			pulls);
686
687	/* In some system designs where the regulators are not in use,
688	 * we can achieve a small reduction in leakage currents by
689	 * floating LDO outputs.  This bit makes no difference if the
690	 * LDOs are enabled, it only affects cases where the LDOs were
691	 * in operation and are then disabled.
692	 */
693	for (i = 0; i < WM8994_NUM_LDO_REGS; i++) {
694		if (wm8994_ldo_in_use(pdata, i))
695			wm8994_set_bits(wm8994, WM8994_LDO_1 + i,
696					WM8994_LDO1_DISCH, WM8994_LDO1_DISCH);
697		else
698			wm8994_set_bits(wm8994, WM8994_LDO_1 + i,
699					WM8994_LDO1_DISCH, 0);
700	}
701
702	wm8994_irq_init(wm8994);
703
704	ret = mfd_add_devices(wm8994->dev, -1,
705			      wm8994_devs, ARRAY_SIZE(wm8994_devs),
706			      NULL, 0, NULL);
707	if (ret != 0) {
708		dev_err(wm8994->dev, "Failed to add children: %d\n", ret);
709		goto err_irq;
710	}
711
712	pm_runtime_enable(wm8994->dev);
713	pm_runtime_idle(wm8994->dev);
714
715	return 0;
716
717err_irq:
718	wm8994_irq_exit(wm8994);
719err_enable:
720	regulator_bulk_disable(wm8994->num_supplies,
721			       wm8994->supplies);
722err:
723	mfd_remove_devices(wm8994->dev);
724	return ret;
725}
726
727static void wm8994_device_exit(struct wm8994 *wm8994)
728{
729	pm_runtime_disable(wm8994->dev);
730	mfd_remove_devices(wm8994->dev);
731	wm8994_irq_exit(wm8994);
732	regulator_bulk_disable(wm8994->num_supplies,
733			       wm8994->supplies);
734}
735
736static const struct of_device_id wm8994_of_match[] = {
737	{ .compatible = "wlf,wm1811", .data = (void *)WM1811 },
738	{ .compatible = "wlf,wm8994", .data = (void *)WM8994 },
739	{ .compatible = "wlf,wm8958", .data = (void *)WM8958 },
740	{ }
741};
742MODULE_DEVICE_TABLE(of, wm8994_of_match);
743
744static int wm8994_i2c_probe(struct i2c_client *i2c,
745				      const struct i2c_device_id *id)
746{
747	const struct of_device_id *of_id;
748	struct wm8994 *wm8994;
749	int ret;
750
751	wm8994 = devm_kzalloc(&i2c->dev, sizeof(struct wm8994), GFP_KERNEL);
752	if (wm8994 == NULL)
753		return -ENOMEM;
754
755	i2c_set_clientdata(i2c, wm8994);
756	wm8994->dev = &i2c->dev;
757	wm8994->irq = i2c->irq;
758
759	if (i2c->dev.of_node) {
760		of_id = of_match_device(wm8994_of_match, &i2c->dev);
761		if (of_id)
762			wm8994->type = (int)of_id->data;
763	} else {
764		wm8994->type = id->driver_data;
765	}
766
767	wm8994->regmap = devm_regmap_init_i2c(i2c, &wm8994_base_regmap_config);
768	if (IS_ERR(wm8994->regmap)) {
769		ret = PTR_ERR(wm8994->regmap);
770		dev_err(wm8994->dev, "Failed to allocate register map: %d\n",
771			ret);
772		return ret;
773	}
774
775	return wm8994_device_init(wm8994, i2c->irq);
776}
777
778static int wm8994_i2c_remove(struct i2c_client *i2c)
779{
780	struct wm8994 *wm8994 = i2c_get_clientdata(i2c);
781
782	wm8994_device_exit(wm8994);
783
784	return 0;
785}
786
787static const struct i2c_device_id wm8994_i2c_id[] = {
788	{ "wm1811", WM1811 },
789	{ "wm1811a", WM1811 },
790	{ "wm8994", WM8994 },
791	{ "wm8958", WM8958 },
792	{ }
793};
794MODULE_DEVICE_TABLE(i2c, wm8994_i2c_id);
795
796static const struct dev_pm_ops wm8994_pm_ops = {
797	SET_RUNTIME_PM_OPS(wm8994_suspend, wm8994_resume, NULL)
798};
799
800static struct i2c_driver wm8994_i2c_driver = {
801	.driver = {
802		.name = "wm8994",
803		.owner = THIS_MODULE,
804		.pm = &wm8994_pm_ops,
805		.of_match_table = of_match_ptr(wm8994_of_match),
806	},
807	.probe = wm8994_i2c_probe,
808	.remove = wm8994_i2c_remove,
809	.id_table = wm8994_i2c_id,
810};
811
812module_i2c_driver(wm8994_i2c_driver);
813
814MODULE_DESCRIPTION("Core support for the WM8994 audio CODEC");
815MODULE_LICENSE("GPL");
816MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
817