blob: 00e4fe2f3c75cf0d4c6300972fbe331ad5825052 [file] [log] [blame]
Mark Brown9e501082010-01-29 18:20:29 +00001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Mark Brown9e501082010-01-29 18:20:29 +000018#include <linux/i2c.h>
Mark Brownd6c645fc2011-06-17 13:02:27 +010019#include <linux/err.h>
Mark Brown9e501082010-01-29 18:20:29 +000020#include <linux/delay.h>
21#include <linux/mfd/core.h>
Mark Brown20fb2772013-04-11 18:11:51 +010022#include <linux/of.h>
23#include <linux/of_device.h>
24#include <linux/of_gpio.h>
Mark Brownd450f192010-11-26 17:19:35 +000025#include <linux/pm_runtime.h>
Mark Brownd6c645fc2011-06-17 13:02:27 +010026#include <linux/regmap.h>
Mark Brown9e501082010-01-29 18:20:29 +000027#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
Mark Brownc3f13862011-10-25 14:23:53 +020034#include "wm8994.h"
Mark Brown9e501082010-01-29 18:20:29 +000035
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{
Mark Brownd6c645fc2011-06-17 13:02:27 +010044 unsigned int val;
Mark Brown9e501082010-01-29 18:20:29 +000045 int ret;
46
Mark Brownd6c645fc2011-06-17 13:02:27 +010047 ret = regmap_read(wm8994->regmap, reg, &val);
Mark Brown9e501082010-01-29 18:20:29 +000048
49 if (ret < 0)
50 return ret;
51 else
Mark Brownd6c645fc2011-06-17 13:02:27 +010052 return val;
Mark Brown9e501082010-01-29 18:20:29 +000053}
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
Mark Brown316b6cc2011-03-18 12:50:10 +000062 * @buf: Buffer to fill. The data will be returned big endian.
Mark Brown9e501082010-01-29 18:20:29 +000063 */
64int wm8994_bulk_read(struct wm8994 *wm8994, unsigned short reg,
65 int count, u16 *buf)
66{
Mark Brownd6c645fc2011-06-17 13:02:27 +010067 return regmap_bulk_read(wm8994->regmap, reg, buf, count);
Mark Brown9e501082010-01-29 18:20:29 +000068}
Mark Brown9e501082010-01-29 18:20:29 +000069
Mark Brown9e501082010-01-29 18:20:29 +000070/**
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{
Mark Brownd6c645fc2011-06-17 13:02:27 +010080 return regmap_write(wm8994->regmap, reg, val);
Mark Brown9e501082010-01-29 18:20:29 +000081}
82EXPORT_SYMBOL_GPL(wm8994_reg_write);
83
84/**
Mark Browne93c5382011-03-10 13:54:07 +000085 * wm8994_bulk_write: Write multiple WM8994 registers
86 *
87 * @wm8994: Device to write to
88 * @reg: First register
89 * @count: Number of registers
Mark Brown42771632011-03-17 21:42:29 +000090 * @buf: Buffer to write from. Data must be big-endian formatted.
Mark Browne93c5382011-03-10 13:54:07 +000091 */
92int wm8994_bulk_write(struct wm8994 *wm8994, unsigned short reg,
Mark Brown07e73fb2011-03-17 21:42:30 +000093 int count, const u16 *buf)
Mark Browne93c5382011-03-10 13:54:07 +000094{
Mark Brownd6c645fc2011-06-17 13:02:27 +010095 return regmap_raw_write(wm8994->regmap, reg, buf, count * sizeof(u16));
Mark Browne93c5382011-03-10 13:54:07 +000096}
97EXPORT_SYMBOL_GPL(wm8994_bulk_write);
98
99/**
Mark Brown9e501082010-01-29 18:20:29 +0000100 * 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{
Mark Brownd6c645fc2011-06-17 13:02:27 +0100110 return regmap_update_bits(wm8994->regmap, reg, mask, val);
Mark Brown9e501082010-01-29 18:20:29 +0000111}
112EXPORT_SYMBOL_GPL(wm8994_set_bits);
113
114static struct mfd_cell wm8994_regulator_devs[] = {
Mark Brownd450f192010-11-26 17:19:35 +0000115 {
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 },
Mark Brown9e501082010-01-29 18:20:29 +0000125};
126
Mark Brownc9fbf7e2010-03-26 16:49:15 +0000127static 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
Mark Brown9e501082010-01-29 18:20:29 +0000143static struct mfd_cell wm8994_devs[] = {
Mark Brownc9fbf7e2010-03-26 16:49:15 +0000144 {
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,
Mark Brownd450f192010-11-26 17:19:35 +0000154 .pm_runtime_no_callbacks = true,
Mark Brownc9fbf7e2010-03-26 16:49:15 +0000155 },
Mark Brown9e501082010-01-29 18:20:29 +0000156};
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 */
Mark Brownb1f43bf2011-05-24 17:35:40 +0800163static 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
Mark Brown9e501082010-01-29 18:20:29 +0000175static const char *wm8994_main_supplies[] = {
176 "DBVDD",
177 "DCVDD",
178 "AVDD1",
179 "AVDD2",
180 "CPVDD",
181 "SPKVDD1",
182 "SPKVDD2",
183};
184
Mark Brown559e0df2010-08-31 19:25:12 +0100185static 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
Mark Brown63946782013-03-22 12:40:22 +0100197#ifdef CONFIG_PM_RUNTIME
Mark Brownd450f192010-11-26 17:19:35 +0000198static int wm8994_suspend(struct device *dev)
Mark Brown9e501082010-01-29 18:20:29 +0000199{
200 struct wm8994 *wm8994 = dev_get_drvdata(dev);
201 int ret;
202
Mark Brown77bd70e2011-02-04 14:57:43 +0000203 /* 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
Mark Brown5f40c6b2011-08-13 14:37:48 +0900213 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:
Mark Brownb5488b62011-11-04 23:14:46 +0000235 case WM1811:
Mark Brown5f40c6b2011-08-13 14:37:48 +0900236 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
Mark Browna3462492011-12-01 17:19:44 +0000248 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
Mark Browne7c248a2012-02-20 21:32:32 +0000262 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
Mark Brown881de672011-08-22 15:43:55 +0200276 /* 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
Mark Brownf40dff92011-02-21 18:30:31 +0000283 /* Explicitly put the device into reset in case regulators
284 * don't get disabled in order to ensure consistent restart.
285 */
Mark Brownbe79cf22011-10-25 13:25:43 +0200286 wm8994_reg_write(wm8994, WM8994_SOFTWARE_RESET,
287 wm8994_reg_read(wm8994, WM8994_SOFTWARE_RESET));
Mark Brownf40dff92011-02-21 18:30:31 +0000288
Mark Browned393dc2012-06-29 14:55:39 +0100289 regcache_mark_dirty(wm8994->regmap);
290
Chris Rattray78a27cd2012-06-12 00:43:35 +0800291 /* Restore GPIO registers to prevent problems with mismatched
292 * pin configurations.
293 */
Mark Browned393dc2012-06-29 14:55:39 +0100294 ret = regcache_sync_region(wm8994->regmap, WM8994_GPIO_1,
295 WM8994_GPIO_11);
Chris Rattray78a27cd2012-06-12 00:43:35 +0800296 if (ret != 0)
297 dev_err(dev, "Failed to restore GPIO registers: %d\n", ret);
298
Mark Brown1a2017b2012-06-29 14:55:40 +0100299 /* 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
Mark Brown3befc922012-01-09 00:36:42 -0800306 regcache_cache_only(wm8994->regmap, true);
Mark Brown77bd70e2011-02-04 14:57:43 +0000307 wm8994->suspended = true;
308
Mark Brown559e0df2010-08-31 19:25:12 +0100309 ret = regulator_bulk_disable(wm8994->num_supplies,
Mark Brown9e501082010-01-29 18:20:29 +0000310 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
Mark Brownd450f192010-11-26 17:19:35 +0000319static int wm8994_resume(struct device *dev)
Mark Brown9e501082010-01-29 18:20:29 +0000320{
321 struct wm8994 *wm8994 = dev_get_drvdata(dev);
Mark Brownc3f13862011-10-25 14:23:53 +0200322 int ret;
Mark Brown9e501082010-01-29 18:20:29 +0000323
Mark Brown77bd70e2011-02-04 14:57:43 +0000324 /* We may have lied to the PM core about suspending */
325 if (!wm8994->suspended)
326 return 0;
327
Mark Brown559e0df2010-08-31 19:25:12 +0100328 ret = regulator_bulk_enable(wm8994->num_supplies,
Mark Brown9e501082010-01-29 18:20:29 +0000329 wm8994->supplies);
330 if (ret != 0) {
331 dev_err(dev, "Failed to enable supplies: %d\n", ret);
332 return ret;
333 }
334
Mark Brown3befc922012-01-09 00:36:42 -0800335 regcache_cache_only(wm8994->regmap, false);
Mark Brownc3f13862011-10-25 14:23:53 +0200336 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;
Mark Brown98ae1cc2011-06-07 11:49:42 +0100340 }
Mark Brownc9fbf7e2010-03-26 16:49:15 +0000341
Mark Brown881de672011-08-22 15:43:55 +0200342 /* 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
Mark Brown77bd70e2011-02-04 14:57:43 +0000347 wm8994->suspended = false;
348
Mark Brown9e501082010-01-29 18:20:29 +0000349 return 0;
Mark Brownc3f13862011-10-25 14:23:53 +0200350
351err_enable:
352 regulator_bulk_disable(wm8994->num_supplies, wm8994->supplies);
353
354 return ret;
Mark Brown9e501082010-01-29 18:20:29 +0000355}
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
Bill Pembertona73e5df2012-11-19 13:25:09 -0500380static const struct reg_default wm8994_revc_patch[] = {
Mark Brown462835e2012-01-21 12:11:53 +0000381 { 0x102, 0x3 },
382 { 0x56, 0x3 },
383 { 0x817, 0x0 },
384 { 0x102, 0x0 },
385};
386
Bill Pembertona73e5df2012-11-19 13:25:09 -0500387static const struct reg_default wm8958_reva_patch[] = {
Mark Brown462835e2012-01-21 12:11:53 +0000388 { 0x102, 0x3 },
389 { 0xcb, 0x81 },
390 { 0x817, 0x0 },
391 { 0x102, 0x0 },
392};
393
Bill Pembertona73e5df2012-11-19 13:25:09 -0500394static const struct reg_default wm1811_reva_patch[] = {
Mark Brown462835e2012-01-21 12:11:53 +0000395 { 0x102, 0x3 },
Mark Brown9282a7b2012-08-23 16:10:29 +0100396 { 0x56, 0xc07 },
Mark Brown462835e2012-01-21 12:11:53 +0000397 { 0x5d, 0x7e },
398 { 0x5e, 0x0 },
399 { 0x102, 0x0 },
400};
401
Mark Brown20fb2772013-04-11 18:11:51 +0100402#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
Mark Brown9e501082010-01-29 18:20:29 +0000456/*
457 * Instantiate the generic non-control parts of the device.
458 */
Bill Pembertonf791be42012-11-19 13:23:04 -0500459static int wm8994_device_init(struct wm8994 *wm8994, int irq)
Mark Brown9e501082010-01-29 18:20:29 +0000460{
Mark Browna8a8fc22012-10-08 11:21:09 +0900461 struct wm8994_pdata *pdata;
Mark Brown34697892011-12-03 17:10:32 +0000462 struct regmap_config *regmap_config;
Mark Brown462835e2012-01-21 12:11:53 +0000463 const struct reg_default *regmap_patch = NULL;
Mark Brown559e0df2010-08-31 19:25:12 +0100464 const char *devname;
Jingoo Han222bc782013-02-20 11:53:36 +0900465 int ret, i, patch_regs = 0;
Mark Brown26c34c22011-11-03 13:20:38 +0000466 int pulls = 0;
Mark Brown9e501082010-01-29 18:20:29 +0000467
Mark Browna8a8fc22012-10-08 11:21:09 +0900468 if (dev_get_platdata(wm8994->dev)) {
469 pdata = dev_get_platdata(wm8994->dev);
470 wm8994->pdata = *pdata;
471 }
472 pdata = &wm8994->pdata;
473
Mark Brown20fb2772013-04-11 18:11:51 +0100474 ret = wm8994_set_pdata_from_of(wm8994);
475 if (ret != 0)
476 return ret;
477
Mark Brown9e501082010-01-29 18:20:29 +0000478 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),
Mark Brown55692af2012-09-11 15:16:36 +0800484 NULL, 0, NULL);
Mark Brown9e501082010-01-29 18:20:29 +0000485 if (ret != 0) {
486 dev_err(wm8994->dev, "Failed to add children: %d\n", ret);
Mark Brown9db42492012-01-30 20:06:05 +0000487 goto err;
Mark Brown9e501082010-01-29 18:20:29 +0000488 }
489
Mark Brown559e0df2010-08-31 19:25:12 +0100490 switch (wm8994->type) {
Mark Brownb1f43bf2011-05-24 17:35:40 +0800491 case WM1811:
492 wm8994->num_supplies = ARRAY_SIZE(wm1811_main_supplies);
493 break;
Mark Brown559e0df2010-08-31 19:25:12 +0100494 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();
Mark Brown9db42492012-01-30 20:06:05 +0000502 goto err;
Mark Brown559e0df2010-08-31 19:25:12 +0100503 }
504
Mark Brown2fa33492011-10-25 13:45:40 +0200505 wm8994->supplies = devm_kzalloc(wm8994->dev,
506 sizeof(struct regulator_bulk_data) *
507 wm8994->num_supplies, GFP_KERNEL);
Axel Linfccbd212010-08-04 09:44:47 +0800508 if (!wm8994->supplies) {
509 ret = -ENOMEM;
Mark Brown9db42492012-01-30 20:06:05 +0000510 goto err;
Axel Linfccbd212010-08-04 09:44:47 +0800511 }
Mark Brown9e501082010-01-29 18:20:29 +0000512
Mark Brown559e0df2010-08-31 19:25:12 +0100513 switch (wm8994->type) {
Mark Brownb1f43bf2011-05-24 17:35:40 +0800514 case WM1811:
515 for (i = 0; i < ARRAY_SIZE(wm1811_main_supplies); i++)
516 wm8994->supplies[i].supply = wm1811_main_supplies[i];
517 break;
Mark Brown559e0df2010-08-31 19:25:12 +0100518 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();
Mark Brown9db42492012-01-30 20:06:05 +0000528 goto err;
Mark Brown559e0df2010-08-31 19:25:12 +0100529 }
530
Sachin Kamatf1c68e42013-01-08 14:01:22 +0530531 ret = devm_regulator_bulk_get(wm8994->dev, wm8994->num_supplies,
Mark Brown9e501082010-01-29 18:20:29 +0000532 wm8994->supplies);
533 if (ret != 0) {
534 dev_err(wm8994->dev, "Failed to get supplies: %d\n", ret);
Mark Brown9db42492012-01-30 20:06:05 +0000535 goto err;
Mark Brown9e501082010-01-29 18:20:29 +0000536 }
537
Mark Brown559e0df2010-08-31 19:25:12 +0100538 ret = regulator_bulk_enable(wm8994->num_supplies,
Mark Brown9e501082010-01-29 18:20:29 +0000539 wm8994->supplies);
540 if (ret != 0) {
541 dev_err(wm8994->dev, "Failed to enable supplies: %d\n", ret);
Sachin Kamatf1c68e42013-01-08 14:01:22 +0530542 goto err;
Mark Brown9e501082010-01-29 18:20:29 +0000543 }
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 }
Mark Brown559e0df2010-08-31 19:25:12 +0100550 switch (ret) {
Mark Brownb1f43bf2011-05-24 17:35:40 +0800551 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;
Mark Brown559e0df2010-08-31 19:25:12 +0100558 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:
Mark Brown9e501082010-01-29 18:20:29 +0000573 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 }
Mark Browncc7a7272012-05-07 10:03:22 +0100585 wm8994->revision = ret & WM8994_CHIP_REV_MASK;
586 wm8994->cust_id = (ret & WM8994_CUST_ID_MASK) >> WM8994_CUST_ID_SHIFT;
Mark Brown9e501082010-01-29 18:20:29 +0000587
Mark Browna2495bc2011-06-02 19:18:55 +0100588 switch (wm8994->type) {
589 case WM8994:
Mark Brown7ed58492011-12-01 13:55:49 +0000590 switch (wm8994->revision) {
Mark Browna2495bc2011-06-02 19:18:55 +0100591 case 0:
592 case 1:
Mark Brown559e0df2010-08-31 19:25:12 +0100593 dev_warn(wm8994->dev,
594 "revision %c not fully supported\n",
Mark Brown7ed58492011-12-01 13:55:49 +0000595 'A' + wm8994->revision);
Mark Browna2495bc2011-06-02 19:18:55 +0100596 break;
Mark Brown462835e2012-01-21 12:11:53 +0000597 case 2:
598 case 3:
Mark Brownd54e17f2012-11-27 15:23:30 +0000599 default:
Mark Brown462835e2012-01-21 12:11:53 +0000600 regmap_patch = wm8994_revc_patch;
601 patch_regs = ARRAY_SIZE(wm8994_revc_patch);
602 break;
Mark Browna2495bc2011-06-02 19:18:55 +0100603 }
Mark Brown9e501082010-01-29 18:20:29 +0000604 break;
Mark Brown462835e2012-01-21 12:11:53 +0000605
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
Mark Brown443e67e2011-11-30 16:51:04 +0000617 case WM1811:
618 /* Revision C did not change the relevant layer */
Mark Brown7ed58492011-12-01 13:55:49 +0000619 if (wm8994->revision > 1)
620 wm8994->revision++;
Mark Brownd54e17f2012-11-27 15:23:30 +0000621
622 regmap_patch = wm1811_reva_patch;
623 patch_regs = ARRAY_SIZE(wm1811_reva_patch);
Mark Brown443e67e2011-11-30 16:51:04 +0000624 break;
Mark Brown462835e2012-01-21 12:11:53 +0000625
Mark Brown9e501082010-01-29 18:20:29 +0000626 default:
Mark Brown9e501082010-01-29 18:20:29 +0000627 break;
628 }
629
Mark Browncc7a7272012-05-07 10:03:22 +0100630 dev_info(wm8994->dev, "%s revision %c CUST_ID %02x\n", devname,
631 'A' + wm8994->revision, wm8994->cust_id);
Mark Brown9e501082010-01-29 18:20:29 +0000632
Mark Brown34697892011-12-03 17:10:32 +0000633 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 }
Mark Brown9e501082010-01-29 18:20:29 +0000654
Mark Brown462835e2012-01-21 12:11:53 +0000655 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);
Mark Brown9db42492012-01-30 20:06:05 +0000661 goto err;
Mark Brown462835e2012-01-21 12:11:53 +0000662 }
663 }
664
Mark Browna8a8fc22012-10-08 11:21:09 +0900665 wm8994->irq_base = pdata->irq_base;
666 wm8994->gpio_base = pdata->gpio_base;
Mark Brown9e501082010-01-29 18:20:29 +0000667
Mark Browna8a8fc22012-10-08 11:21:09 +0900668 /* 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]);
Mark Brown9e501082010-01-29 18:20:29 +0000673 }
674 }
675
Mark Browna8a8fc22012-10-08 11:21:09 +0900676 wm8994->ldo_ena_always_driven = pdata->ldo_ena_always_driven;
677
678 if (pdata->spkmode_pu)
679 pulls |= WM8994_SPKMODE_PU;
680
Mark Brown26c34c22011-11-03 13:20:38 +0000681 /* Disable unneeded pulls */
Mark Brown881de672011-08-22 15:43:55 +0200682 wm8994_set_bits(wm8994, WM8994_PULL_CONTROL_2,
Mark Brown26c34c22011-11-03 13:20:38 +0000683 WM8994_LDO1ENA_PD | WM8994_LDO2ENA_PD |
684 WM8994_SPKMODE_PU | WM8994_CSNADDR_PD,
685 pulls);
Mark Brown881de672011-08-22 15:43:55 +0200686
Mark Brown9e501082010-01-29 18:20:29 +0000687 /* 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
Mark Brownc9fbf7e2010-03-26 16:49:15 +0000702 wm8994_irq_init(wm8994);
703
Mark Brown9e501082010-01-29 18:20:29 +0000704 ret = mfd_add_devices(wm8994->dev, -1,
705 wm8994_devs, ARRAY_SIZE(wm8994_devs),
Mark Brown55692af2012-09-11 15:16:36 +0800706 NULL, 0, NULL);
Mark Brown9e501082010-01-29 18:20:29 +0000707 if (ret != 0) {
708 dev_err(wm8994->dev, "Failed to add children: %d\n", ret);
Mark Brownc9fbf7e2010-03-26 16:49:15 +0000709 goto err_irq;
Mark Brown9e501082010-01-29 18:20:29 +0000710 }
711
Mark Brownd450f192010-11-26 17:19:35 +0000712 pm_runtime_enable(wm8994->dev);
Mark Brownee67b0c2012-02-06 18:47:37 +0000713 pm_runtime_idle(wm8994->dev);
Mark Brownd450f192010-11-26 17:19:35 +0000714
Mark Brown9e501082010-01-29 18:20:29 +0000715 return 0;
716
Mark Brownc9fbf7e2010-03-26 16:49:15 +0000717err_irq:
718 wm8994_irq_exit(wm8994);
Mark Brown9e501082010-01-29 18:20:29 +0000719err_enable:
Mark Brown559e0df2010-08-31 19:25:12 +0100720 regulator_bulk_disable(wm8994->num_supplies,
Mark Brown9e501082010-01-29 18:20:29 +0000721 wm8994->supplies);
Mark Brown9db42492012-01-30 20:06:05 +0000722err:
Mark Brown9e501082010-01-29 18:20:29 +0000723 mfd_remove_devices(wm8994->dev);
Mark Brown9e501082010-01-29 18:20:29 +0000724 return ret;
725}
726
Bill Pemberton4740f732012-11-19 13:26:01 -0500727static void wm8994_device_exit(struct wm8994 *wm8994)
Mark Brown9e501082010-01-29 18:20:29 +0000728{
Mark Brownd450f192010-11-26 17:19:35 +0000729 pm_runtime_disable(wm8994->dev);
Mark Brown9e501082010-01-29 18:20:29 +0000730 mfd_remove_devices(wm8994->dev);
Mark Brownc9fbf7e2010-03-26 16:49:15 +0000731 wm8994_irq_exit(wm8994);
Mark Brown559e0df2010-08-31 19:25:12 +0100732 regulator_bulk_disable(wm8994->num_supplies,
Mark Brown9e501082010-01-29 18:20:29 +0000733 wm8994->supplies);
Mark Brown9e501082010-01-29 18:20:29 +0000734}
735
Mark Browncf763c22011-11-22 18:22:29 +0000736static const struct of_device_id wm8994_of_match[] = {
Mark Brown39aa3b52013-04-11 18:11:50 +0100737 { .compatible = "wlf,wm1811", .data = (void *)WM1811 },
738 { .compatible = "wlf,wm8994", .data = (void *)WM8994 },
739 { .compatible = "wlf,wm8958", .data = (void *)WM8958 },
Mark Browncf763c22011-11-22 18:22:29 +0000740 { }
741};
742MODULE_DEVICE_TABLE(of, wm8994_of_match);
743
Bill Pembertonf791be42012-11-19 13:23:04 -0500744static int wm8994_i2c_probe(struct i2c_client *i2c,
Mark Brown027d6762012-02-17 15:57:01 -0800745 const struct i2c_device_id *id)
Mark Brown9e501082010-01-29 18:20:29 +0000746{
Mark Brown20fb2772013-04-11 18:11:51 +0100747 const struct of_device_id *of_id;
Mark Brown9e501082010-01-29 18:20:29 +0000748 struct wm8994 *wm8994;
Mark Brownd6c645fc2011-06-17 13:02:27 +0100749 int ret;
Mark Brown9e501082010-01-29 18:20:29 +0000750
Mark Brown2fa33492011-10-25 13:45:40 +0200751 wm8994 = devm_kzalloc(&i2c->dev, sizeof(struct wm8994), GFP_KERNEL);
Axel Lind0a11692010-08-09 14:52:16 +0800752 if (wm8994 == NULL)
Mark Brown9e501082010-01-29 18:20:29 +0000753 return -ENOMEM;
Mark Brown9e501082010-01-29 18:20:29 +0000754
755 i2c_set_clientdata(i2c, wm8994);
756 wm8994->dev = &i2c->dev;
Mark Brownc9fbf7e2010-03-26 16:49:15 +0000757 wm8994->irq = i2c->irq;
Mark Brown20fb2772013-04-11 18:11:51 +0100758
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 }
Mark Brown9e501082010-01-29 18:20:29 +0000766
Mark Brown9db42492012-01-30 20:06:05 +0000767 wm8994->regmap = devm_regmap_init_i2c(i2c, &wm8994_base_regmap_config);
Mark Brownd6c645fc2011-06-17 13:02:27 +0100768 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);
Mark Brownd6c645fc2011-06-17 13:02:27 +0100772 return ret;
773 }
774
Mark Brown559e0df2010-08-31 19:25:12 +0100775 return wm8994_device_init(wm8994, i2c->irq);
Mark Brown9e501082010-01-29 18:20:29 +0000776}
777
Bill Pemberton4740f732012-11-19 13:26:01 -0500778static int wm8994_i2c_remove(struct i2c_client *i2c)
Mark Brown9e501082010-01-29 18:20:29 +0000779{
780 struct wm8994 *wm8994 = i2c_get_clientdata(i2c);
781
782 wm8994_device_exit(wm8994);
783
784 return 0;
785}
786
Mark Brown9e501082010-01-29 18:20:29 +0000787static const struct i2c_device_id wm8994_i2c_id[] = {
Mark Brownb1f43bf2011-05-24 17:35:40 +0800788 { "wm1811", WM1811 },
Mark Brown71d17182011-11-30 20:16:42 +0000789 { "wm1811a", WM1811 },
Mark Brown559e0df2010-08-31 19:25:12 +0100790 { "wm8994", WM8994 },
791 { "wm8958", WM8958 },
Mark Brown9e501082010-01-29 18:20:29 +0000792 { }
793};
794MODULE_DEVICE_TABLE(i2c, wm8994_i2c_id);
795
Mark Brown63946782013-03-22 12:40:22 +0100796static const struct dev_pm_ops wm8994_pm_ops = {
797 SET_RUNTIME_PM_OPS(wm8994_suspend, wm8994_resume, NULL)
798};
Mark Brownd450f192010-11-26 17:19:35 +0000799
Mark Brown9e501082010-01-29 18:20:29 +0000800static struct i2c_driver wm8994_i2c_driver = {
801 .driver = {
Mark Brownd450f192010-11-26 17:19:35 +0000802 .name = "wm8994",
803 .owner = THIS_MODULE,
804 .pm = &wm8994_pm_ops,
Mark Brown20fb2772013-04-11 18:11:51 +0100805 .of_match_table = of_match_ptr(wm8994_of_match),
Mark Brown9e501082010-01-29 18:20:29 +0000806 },
807 .probe = wm8994_i2c_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500808 .remove = wm8994_i2c_remove,
Mark Brown9e501082010-01-29 18:20:29 +0000809 .id_table = wm8994_i2c_id,
810};
811
Mark Brownceb57d22012-05-07 10:03:23 +0100812module_i2c_driver(wm8994_i2c_driver);
Mark Brown9e501082010-01-29 18:20:29 +0000813
814MODULE_DESCRIPTION("Core support for the WM8994 audio CODEC");
815MODULE_LICENSE("GPL");
816MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");