blob: 8003761a8e02343649b22710a665e7f0a0b159e1 [file] [log] [blame]
Richard Purdie40e0aa62006-10-06 18:36:07 +02001/*
2 * wm8731.c -- WM8731 ALSA SoC Audio driver
3 *
4 * Copyright 2005 Openedhand Ltd.
5 *
6 * Author: Richard Purdie <richard@openedhand.com>
7 *
8 * Based on wm8753.c by Liam Girdwood
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/module.h>
16#include <linux/moduleparam.h>
17#include <linux/init.h>
18#include <linux/delay.h>
19#include <linux/pm.h>
20#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Richard Purdie40e0aa62006-10-06 18:36:07 +020022#include <linux/platform_device.h>
Mark Brown7dea7c02009-10-26 15:20:17 +000023#include <linux/regulator/consumer.h>
Cliff Caid2a40352008-09-01 18:47:03 +010024#include <linux/spi/spi.h>
Richard Purdie40e0aa62006-10-06 18:36:07 +020025#include <sound/core.h>
26#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28#include <sound/soc.h>
Richard Purdie40e0aa62006-10-06 18:36:07 +020029#include <sound/initval.h>
Mark Brownd00efa62009-07-08 16:53:12 +010030#include <sound/tlv.h>
Richard Purdie40e0aa62006-10-06 18:36:07 +020031
32#include "wm8731.h"
33
Mark Brown7dea7c02009-10-26 15:20:17 +000034#define WM8731_NUM_SUPPLIES 4
35static const char *wm8731_supply_names[WM8731_NUM_SUPPLIES] = {
36 "AVDD",
37 "HPVDD",
38 "DCVDD",
39 "DBVDD",
40};
41
Frank Mandarinob36d61d2007-02-02 17:14:56 +010042/* codec private data */
43struct wm8731_priv {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000044 enum snd_soc_control_type control_type;
Mark Brown7dea7c02009-10-26 15:20:17 +000045 struct regulator_bulk_data supplies[WM8731_NUM_SUPPLIES];
Mark Brown59981022009-02-16 20:49:16 +000046 u16 reg_cache[WM8731_CACHEREGNUM];
Frank Mandarinob36d61d2007-02-02 17:14:56 +010047 unsigned int sysclk;
Mark Brown9745e822010-08-17 23:40:24 +010048 int sysclk_type;
Frank Mandarinob36d61d2007-02-02 17:14:56 +010049};
50
Mark Browna8035c82009-02-16 19:35:43 +000051
Richard Purdie40e0aa62006-10-06 18:36:07 +020052/*
53 * wm8731 register cache
54 * We can't read the WM8731 register space when we are
55 * using 2 wire for device control, so we cache them instead.
56 * There is no point in caching the reset register
57 */
58static const u16 wm8731_reg[WM8731_CACHEREGNUM] = {
Mark Brown17a52fd2009-07-05 17:24:50 +010059 0x0097, 0x0097, 0x0079, 0x0079,
60 0x000a, 0x0008, 0x009f, 0x000a,
61 0x0000, 0x0000
Richard Purdie40e0aa62006-10-06 18:36:07 +020062};
63
Mark Brown17a52fd2009-07-05 17:24:50 +010064#define wm8731_reset(c) snd_soc_write(c, WM8731_RESET, 0)
Richard Purdie40e0aa62006-10-06 18:36:07 +020065
66static const char *wm8731_input_select[] = {"Line In", "Mic"};
Mark Brown59f72972010-11-30 21:03:44 +000067
Richard Purdie40e0aa62006-10-06 18:36:07 +020068static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
69
Mark Brown59f72972010-11-30 21:03:44 +000070static const struct soc_enum wm8731_insel_enum =
71 SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select);
72
73static const struct soc_enum wm8731_deemph_enum =
74 SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph);
75
Richard Purdie40e0aa62006-10-06 18:36:07 +020076
Mark Brownd00efa62009-07-08 16:53:12 +010077static const DECLARE_TLV_DB_SCALE(in_tlv, -3450, 150, 0);
78static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -1500, 300, 0);
79static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
Mark Brownd9211842010-11-30 21:03:09 +000080static const DECLARE_TLV_DB_SCALE(mic_tlv, 0, 2000, 0);
Mark Brownd00efa62009-07-08 16:53:12 +010081
Richard Purdie40e0aa62006-10-06 18:36:07 +020082static const struct snd_kcontrol_new wm8731_snd_controls[] = {
83
Mark Brownd00efa62009-07-08 16:53:12 +010084SOC_DOUBLE_R_TLV("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
85 0, 127, 0, out_tlv),
Liam Girdwoodbd903b62006-11-09 16:35:01 +010086SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
87 7, 1, 0),
Richard Purdie40e0aa62006-10-06 18:36:07 +020088
Mark Brownd00efa62009-07-08 16:53:12 +010089SOC_DOUBLE_R_TLV("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0,
90 in_tlv),
Richard Purdie40e0aa62006-10-06 18:36:07 +020091SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
92
Mark Brownd9211842010-11-30 21:03:09 +000093SOC_SINGLE_TLV("Mic Boost Volume", WM8731_APANA, 0, 1, 0, mic_tlv),
Mark Brownef38ed82009-07-08 17:05:43 +010094SOC_SINGLE("Mic Capture Switch", WM8731_APANA, 1, 1, 1),
Richard Purdie40e0aa62006-10-06 18:36:07 +020095
Mark Brownd00efa62009-07-08 16:53:12 +010096SOC_SINGLE_TLV("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1,
97 sidetone_tlv),
Richard Purdie40e0aa62006-10-06 18:36:07 +020098
99SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
100SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
101
Mark Brown59f72972010-11-30 21:03:44 +0000102SOC_ENUM("Playback De-emphasis", wm8731_deemph_enum),
Richard Purdie40e0aa62006-10-06 18:36:07 +0200103};
104
Richard Purdie40e0aa62006-10-06 18:36:07 +0200105/* Output Mixer */
106static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
107SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
108SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
109SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
110};
111
112/* Input mux */
113static const struct snd_kcontrol_new wm8731_input_mux_controls =
Mark Brown59f72972010-11-30 21:03:44 +0000114SOC_DAPM_ENUM("Input Select", wm8731_insel_enum);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200115
116static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
Mark Brown9745e822010-08-17 23:40:24 +0100117SND_SOC_DAPM_SUPPLY("OSC", WM8731_PWR, 5, 1, NULL, 0),
Richard Purdie40e0aa62006-10-06 18:36:07 +0200118SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
119 &wm8731_output_mixer_controls[0],
120 ARRAY_SIZE(wm8731_output_mixer_controls)),
121SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
122SND_SOC_DAPM_OUTPUT("LOUT"),
123SND_SOC_DAPM_OUTPUT("LHPOUT"),
124SND_SOC_DAPM_OUTPUT("ROUT"),
125SND_SOC_DAPM_OUTPUT("RHPOUT"),
126SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
127SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
128SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
129SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
130SND_SOC_DAPM_INPUT("MICIN"),
131SND_SOC_DAPM_INPUT("RLINEIN"),
132SND_SOC_DAPM_INPUT("LLINEIN"),
133};
134
Mark Brown9745e822010-08-17 23:40:24 +0100135static int wm8731_check_osc(struct snd_soc_dapm_widget *source,
136 struct snd_soc_dapm_widget *sink)
137{
138 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(source->codec);
139
140 return wm8731->sysclk_type == WM8731_SYSCLK_MCLK;
141}
142
Mark Browna65f0562008-05-13 14:54:43 +0200143static const struct snd_soc_dapm_route intercon[] = {
Mark Brown9745e822010-08-17 23:40:24 +0100144 {"DAC", NULL, "OSC", wm8731_check_osc},
145 {"ADC", NULL, "OSC", wm8731_check_osc},
146
Richard Purdie40e0aa62006-10-06 18:36:07 +0200147 /* output mixer */
148 {"Output Mixer", "Line Bypass Switch", "Line Input"},
149 {"Output Mixer", "HiFi Playback Switch", "DAC"},
150 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
151
152 /* outputs */
153 {"RHPOUT", NULL, "Output Mixer"},
154 {"ROUT", NULL, "Output Mixer"},
155 {"LHPOUT", NULL, "Output Mixer"},
156 {"LOUT", NULL, "Output Mixer"},
157
158 /* input mux */
159 {"Input Mux", "Line In", "Line Input"},
160 {"Input Mux", "Mic", "Mic Bias"},
161 {"ADC", NULL, "Input Mux"},
162
163 /* inputs */
164 {"Line Input", NULL, "LLINEIN"},
165 {"Line Input", NULL, "RLINEIN"},
166 {"Mic Bias", NULL, "MICIN"},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200167};
168
169static int wm8731_add_widgets(struct snd_soc_codec *codec)
170{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200171 struct snd_soc_dapm_context *dapm = &codec->dapm;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200172
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200173 snd_soc_dapm_new_controls(dapm, wm8731_dapm_widgets,
174 ARRAY_SIZE(wm8731_dapm_widgets));
175 snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
Richard Purdie40e0aa62006-10-06 18:36:07 +0200176
Richard Purdie40e0aa62006-10-06 18:36:07 +0200177 return 0;
178}
179
180struct _coeff_div {
181 u32 mclk;
182 u32 rate;
183 u16 fs;
184 u8 sr:4;
185 u8 bosr:1;
186 u8 usb:1;
187};
188
189/* codec mclk clock divider coefficients */
190static const struct _coeff_div coeff_div[] = {
191 /* 48k */
192 {12288000, 48000, 256, 0x0, 0x0, 0x0},
193 {18432000, 48000, 384, 0x0, 0x1, 0x0},
194 {12000000, 48000, 250, 0x0, 0x0, 0x1},
195
196 /* 32k */
197 {12288000, 32000, 384, 0x6, 0x0, 0x0},
198 {18432000, 32000, 576, 0x6, 0x1, 0x0},
Frank Mandarino298a2c72007-01-31 10:02:56 +0100199 {12000000, 32000, 375, 0x6, 0x0, 0x1},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200200
201 /* 8k */
202 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
203 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
204 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
205 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
206 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
207
208 /* 96k */
209 {12288000, 96000, 128, 0x7, 0x0, 0x0},
210 {18432000, 96000, 192, 0x7, 0x1, 0x0},
211 {12000000, 96000, 125, 0x7, 0x0, 0x1},
212
213 /* 44.1k */
214 {11289600, 44100, 256, 0x8, 0x0, 0x0},
215 {16934400, 44100, 384, 0x8, 0x1, 0x0},
216 {12000000, 44100, 272, 0x8, 0x1, 0x1},
217
218 /* 88.2k */
219 {11289600, 88200, 128, 0xf, 0x0, 0x0},
220 {16934400, 88200, 192, 0xf, 0x1, 0x0},
221 {12000000, 88200, 136, 0xf, 0x1, 0x1},
222};
223
224static inline int get_coeff(int mclk, int rate)
225{
226 int i;
227
228 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
229 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
230 return i;
231 }
232 return 0;
233}
234
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100235static int wm8731_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000236 struct snd_pcm_hw_params *params,
237 struct snd_soc_dai *dai)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200238{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000239 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900240 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
Mark Brown17a52fd2009-07-05 17:24:50 +0100241 u16 iface = snd_soc_read(codec, WM8731_IFACE) & 0xfff3;
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100242 int i = get_coeff(wm8731->sysclk, params_rate(params));
243 u16 srate = (coeff_div[i].sr << 2) |
244 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200245
Mark Brown17a52fd2009-07-05 17:24:50 +0100246 snd_soc_write(codec, WM8731_SRATE, srate);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200247
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100248 /* bit size */
249 switch (params_format(params)) {
250 case SNDRV_PCM_FORMAT_S16_LE:
251 break;
252 case SNDRV_PCM_FORMAT_S20_3LE:
253 iface |= 0x0004;
254 break;
255 case SNDRV_PCM_FORMAT_S24_LE:
256 iface |= 0x0008;
257 break;
258 }
259
Mark Brown17a52fd2009-07-05 17:24:50 +0100260 snd_soc_write(codec, WM8731_IFACE, iface);
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100261 return 0;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200262}
263
Mark Browndee89c42008-11-18 22:11:38 +0000264static int wm8731_pcm_prepare(struct snd_pcm_substream *substream,
265 struct snd_soc_dai *dai)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200266{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000267 struct snd_soc_codec *codec = dai->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200268
269 /* set active */
Mark Brown17a52fd2009-07-05 17:24:50 +0100270 snd_soc_write(codec, WM8731_ACTIVE, 0x0001);
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100271
Richard Purdie40e0aa62006-10-06 18:36:07 +0200272 return 0;
273}
274
Mark Browndee89c42008-11-18 22:11:38 +0000275static void wm8731_shutdown(struct snd_pcm_substream *substream,
276 struct snd_soc_dai *dai)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200277{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000278 struct snd_soc_codec *codec = dai->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200279
280 /* deactivate */
281 if (!codec->active) {
282 udelay(50);
Mark Brown17a52fd2009-07-05 17:24:50 +0100283 snd_soc_write(codec, WM8731_ACTIVE, 0x0);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200284 }
285}
286
Liam Girdwoode550e172008-07-07 16:07:52 +0100287static int wm8731_mute(struct snd_soc_dai *dai, int mute)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200288{
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100289 struct snd_soc_codec *codec = dai->codec;
Mark Brown17a52fd2009-07-05 17:24:50 +0100290 u16 mute_reg = snd_soc_read(codec, WM8731_APDIGI) & 0xfff7;
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100291
Richard Purdie40e0aa62006-10-06 18:36:07 +0200292 if (mute)
Mark Brown17a52fd2009-07-05 17:24:50 +0100293 snd_soc_write(codec, WM8731_APDIGI, mute_reg | 0x8);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200294 else
Mark Brown17a52fd2009-07-05 17:24:50 +0100295 snd_soc_write(codec, WM8731_APDIGI, mute_reg);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200296 return 0;
297}
298
Liam Girdwoode550e172008-07-07 16:07:52 +0100299static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100300 int clk_id, unsigned int freq, int dir)
301{
302 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900303 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100304
Mark Brown9745e822010-08-17 23:40:24 +0100305 switch (clk_id) {
306 case WM8731_SYSCLK_XTAL:
307 case WM8731_SYSCLK_MCLK:
308 wm8731->sysclk_type = clk_id;
309 break;
310 default:
311 return -EINVAL;
312 }
313
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100314 switch (freq) {
315 case 11289600:
316 case 12000000:
317 case 12288000:
318 case 16934400:
319 case 18432000:
320 wm8731->sysclk = freq;
Mark Brown9745e822010-08-17 23:40:24 +0100321 break;
322 default:
323 return -EINVAL;
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100324 }
Mark Brown9745e822010-08-17 23:40:24 +0100325
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200326 snd_soc_dapm_sync(&codec->dapm);
Mark Brown9745e822010-08-17 23:40:24 +0100327
328 return 0;
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100329}
330
331
Liam Girdwoode550e172008-07-07 16:07:52 +0100332static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100333 unsigned int fmt)
334{
335 struct snd_soc_codec *codec = codec_dai->codec;
336 u16 iface = 0;
337
338 /* set master/slave audio interface */
339 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
340 case SND_SOC_DAIFMT_CBM_CFM:
341 iface |= 0x0040;
342 break;
343 case SND_SOC_DAIFMT_CBS_CFS:
344 break;
345 default:
346 return -EINVAL;
347 }
348
349 /* interface format */
350 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
351 case SND_SOC_DAIFMT_I2S:
352 iface |= 0x0002;
353 break;
354 case SND_SOC_DAIFMT_RIGHT_J:
355 break;
356 case SND_SOC_DAIFMT_LEFT_J:
357 iface |= 0x0001;
358 break;
359 case SND_SOC_DAIFMT_DSP_A:
360 iface |= 0x0003;
361 break;
362 case SND_SOC_DAIFMT_DSP_B:
363 iface |= 0x0013;
364 break;
365 default:
366 return -EINVAL;
367 }
368
369 /* clock inversion */
370 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
371 case SND_SOC_DAIFMT_NB_NF:
372 break;
373 case SND_SOC_DAIFMT_IB_IF:
374 iface |= 0x0090;
375 break;
376 case SND_SOC_DAIFMT_IB_NF:
377 iface |= 0x0080;
378 break;
379 case SND_SOC_DAIFMT_NB_IF:
380 iface |= 0x0010;
381 break;
382 default:
383 return -EINVAL;
384 }
385
386 /* set iface */
Mark Brown17a52fd2009-07-05 17:24:50 +0100387 snd_soc_write(codec, WM8731_IFACE, iface);
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100388 return 0;
389}
390
Mark Brown0be98982008-05-19 12:31:28 +0200391static int wm8731_set_bias_level(struct snd_soc_codec *codec,
392 enum snd_soc_bias_level level)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200393{
Mark Brown06ae9982010-05-07 19:14:45 +0100394 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
395 int i, ret;
396 u8 data[2];
397 u16 *cache = codec->reg_cache;
Mark Brown22d22ee2009-02-16 19:20:15 +0000398 u16 reg;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200399
Mark Brown0be98982008-05-19 12:31:28 +0200400 switch (level) {
401 case SND_SOC_BIAS_ON:
Richard Purdie40e0aa62006-10-06 18:36:07 +0200402 break;
Mark Brown0be98982008-05-19 12:31:28 +0200403 case SND_SOC_BIAS_PREPARE:
Richard Purdie40e0aa62006-10-06 18:36:07 +0200404 break;
Mark Brown0be98982008-05-19 12:31:28 +0200405 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200406 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
Mark Brown06ae9982010-05-07 19:14:45 +0100407 ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
408 wm8731->supplies);
409 if (ret != 0)
410 return ret;
411
412 /* Sync reg_cache with the hardware */
413 for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
414 if (cache[i] == wm8731_reg[i])
415 continue;
416
417 data[0] = (i << 1) | ((cache[i] >> 8)
418 & 0x0001);
419 data[1] = cache[i] & 0x00ff;
420 codec->hw_write(codec->control_data, data, 2);
421 }
422 }
423
Mark Brown22d22ee2009-02-16 19:20:15 +0000424 /* Clear PWROFF, gate CLKOUT, everything else as-is */
Mark Brown17a52fd2009-07-05 17:24:50 +0100425 reg = snd_soc_read(codec, WM8731_PWR) & 0xff7f;
426 snd_soc_write(codec, WM8731_PWR, reg | 0x0040);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200427 break;
Mark Brown0be98982008-05-19 12:31:28 +0200428 case SND_SOC_BIAS_OFF:
Mark Brown17a52fd2009-07-05 17:24:50 +0100429 snd_soc_write(codec, WM8731_ACTIVE, 0x0);
430 snd_soc_write(codec, WM8731_PWR, 0xffff);
Mark Brown06ae9982010-05-07 19:14:45 +0100431 regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies),
432 wm8731->supplies);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200433 break;
434 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200435 codec->dapm.bias_level = level;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200436 return 0;
437}
438
Bill Gatliffe1354432010-04-09 18:08:08 +0100439#define WM8731_RATES SNDRV_PCM_RATE_8000_96000
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100440
441#define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
442 SNDRV_PCM_FMTBIT_S24_LE)
443
Eric Miao6335d052009-03-03 09:41:00 +0800444static struct snd_soc_dai_ops wm8731_dai_ops = {
445 .prepare = wm8731_pcm_prepare,
446 .hw_params = wm8731_hw_params,
447 .shutdown = wm8731_shutdown,
448 .digital_mute = wm8731_mute,
449 .set_sysclk = wm8731_set_dai_sysclk,
450 .set_fmt = wm8731_set_dai_fmt,
451};
452
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000453static struct snd_soc_dai_driver wm8731_dai = {
454 .name = "wm8731-hifi",
Richard Purdie40e0aa62006-10-06 18:36:07 +0200455 .playback = {
456 .stream_name = "Playback",
457 .channels_min = 1,
458 .channels_max = 2,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100459 .rates = WM8731_RATES,
460 .formats = WM8731_FORMATS,},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200461 .capture = {
462 .stream_name = "Capture",
463 .channels_min = 1,
464 .channels_max = 2,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100465 .rates = WM8731_RATES,
466 .formats = WM8731_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800467 .ops = &wm8731_dai_ops,
Mark Brown49344822009-07-08 17:48:12 +0100468 .symmetric_rates = 1,
Richard Purdie40e0aa62006-10-06 18:36:07 +0200469};
Richard Purdie40e0aa62006-10-06 18:36:07 +0200470
Mark Brownb3b50b32009-06-13 22:30:18 +0100471#ifdef CONFIG_PM
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000472static int wm8731_suspend(struct snd_soc_codec *codec, pm_message_t state)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200473{
Mark Brown0be98982008-05-19 12:31:28 +0200474 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
Mark Brown06ae9982010-05-07 19:14:45 +0100475
Richard Purdie40e0aa62006-10-06 18:36:07 +0200476 return 0;
477}
478
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000479static int wm8731_resume(struct snd_soc_codec *codec)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200480{
Mark Brown0be98982008-05-19 12:31:28 +0200481 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Mark Brown7dea7c02009-10-26 15:20:17 +0000482
Richard Purdie40e0aa62006-10-06 18:36:07 +0200483 return 0;
484}
Mark Brownb3b50b32009-06-13 22:30:18 +0100485#else
486#define wm8731_suspend NULL
487#define wm8731_resume NULL
488#endif
Richard Purdie40e0aa62006-10-06 18:36:07 +0200489
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000490static int wm8731_probe(struct snd_soc_codec *codec)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200491{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000492 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
493 int ret = 0, i;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200494
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000495 ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8731->control_type);
Mark Brown17a52fd2009-07-05 17:24:50 +0100496 if (ret < 0) {
497 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000498 return ret;
Mark Brown17a52fd2009-07-05 17:24:50 +0100499 }
500
Mark Brown7dea7c02009-10-26 15:20:17 +0000501 for (i = 0; i < ARRAY_SIZE(wm8731->supplies); i++)
502 wm8731->supplies[i].supply = wm8731_supply_names[i];
503
504 ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8731->supplies),
505 wm8731->supplies);
506 if (ret != 0) {
507 dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000508 return ret;
Mark Brown7dea7c02009-10-26 15:20:17 +0000509 }
510
511 ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
512 wm8731->supplies);
513 if (ret != 0) {
514 dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
515 goto err_regulator_get;
516 }
517
Mark Brown519cf2d2009-02-18 21:06:01 +0000518 ret = wm8731_reset(codec);
519 if (ret < 0) {
Mark Brownfe5422f2009-07-05 15:18:01 +0100520 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
Mark Brown7dea7c02009-10-26 15:20:17 +0000521 goto err_regulator_enable;
Mark Brown519cf2d2009-02-18 21:06:01 +0000522 }
523
Mark Brown59981022009-02-16 20:49:16 +0000524 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
525
526 /* Latch the update bits */
Mark Brown17a52fd2009-07-05 17:24:50 +0100527 snd_soc_update_bits(codec, WM8731_LOUT1V, 0x100, 0);
528 snd_soc_update_bits(codec, WM8731_ROUT1V, 0x100, 0);
529 snd_soc_update_bits(codec, WM8731_LINVOL, 0x100, 0);
530 snd_soc_update_bits(codec, WM8731_RINVOL, 0x100, 0);
Mark Brown59981022009-02-16 20:49:16 +0000531
Mark Brownce3bdaa2009-02-19 14:29:49 +0000532 /* Disable bypass path by default */
Dimitris Papastamos2062ea52010-12-01 09:38:55 +0000533 snd_soc_update_bits(codec, WM8731_APANA, 0x8, 0);
Mark Brownce3bdaa2009-02-19 14:29:49 +0000534
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000535 snd_soc_add_controls(codec, wm8731_snd_controls,
536 ARRAY_SIZE(wm8731_snd_controls));
537 wm8731_add_widgets(codec);
Mark Brown59981022009-02-16 20:49:16 +0000538
Mark Brown06ae9982010-05-07 19:14:45 +0100539 /* Regulators will have been enabled by bias management */
540 regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
541
Mark Browna8035c82009-02-16 19:35:43 +0000542 return 0;
Mark Brownfe5422f2009-07-05 15:18:01 +0100543
Mark Brown7dea7c02009-10-26 15:20:17 +0000544err_regulator_enable:
545 regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
546err_regulator_get:
547 regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000548
Mark Brownfe5422f2009-07-05 15:18:01 +0100549 return ret;
Mark Browna8035c82009-02-16 19:35:43 +0000550}
551
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000552/* power down chip */
553static int wm8731_remove(struct snd_soc_codec *codec)
Mark Brown59981022009-02-16 20:49:16 +0000554{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000555 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
556
557 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
558
559 regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
Mark Brown7dea7c02009-10-26 15:20:17 +0000560 regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000561
562 return 0;
Mark Brown59981022009-02-16 20:49:16 +0000563}
Mark Browna8035c82009-02-16 19:35:43 +0000564
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000565static struct snd_soc_codec_driver soc_codec_dev_wm8731 = {
566 .probe = wm8731_probe,
567 .remove = wm8731_remove,
568 .suspend = wm8731_suspend,
569 .resume = wm8731_resume,
570 .set_bias_level = wm8731_set_bias_level,
Dimitris Papastamose5eec342010-09-10 18:14:56 +0100571 .reg_cache_size = ARRAY_SIZE(wm8731_reg),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000572 .reg_word_size = sizeof(u16),
573 .reg_cache_default = wm8731_reg,
574};
575
Mark Brown59981022009-02-16 20:49:16 +0000576#if defined(CONFIG_SPI_MASTER)
Mark Brown59981022009-02-16 20:49:16 +0000577static int __devinit wm8731_spi_probe(struct spi_device *spi)
578{
Mark Brown59981022009-02-16 20:49:16 +0000579 struct wm8731_priv *wm8731;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000580 int ret;
Mark Brown59981022009-02-16 20:49:16 +0000581
582 wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
583 if (wm8731 == NULL)
584 return -ENOMEM;
585
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000586 wm8731->control_type = SND_SOC_SPI;
587 spi_set_drvdata(spi, wm8731);
Mark Brown59981022009-02-16 20:49:16 +0000588
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000589 ret = snd_soc_register_codec(&spi->dev,
590 &soc_codec_dev_wm8731, &wm8731_dai, 1);
591 if (ret < 0)
592 kfree(wm8731);
593 return ret;
Mark Brown59981022009-02-16 20:49:16 +0000594}
595
596static int __devexit wm8731_spi_remove(struct spi_device *spi)
597{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000598 snd_soc_unregister_codec(&spi->dev);
599 kfree(spi_get_drvdata(spi));
Mark Brown59981022009-02-16 20:49:16 +0000600 return 0;
601}
602
603static struct spi_driver wm8731_spi_driver = {
604 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000605 .name = "wm8731-codec",
Mark Brown59981022009-02-16 20:49:16 +0000606 .owner = THIS_MODULE,
607 },
608 .probe = wm8731_spi_probe,
609 .remove = __devexit_p(wm8731_spi_remove),
610};
Mark Browna8035c82009-02-16 19:35:43 +0000611#endif /* CONFIG_SPI_MASTER */
612
613#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Mark Brownc6f29812009-02-18 21:25:40 +0000614static __devinit int wm8731_i2c_probe(struct i2c_client *i2c,
615 const struct i2c_device_id *id)
Mark Browna8035c82009-02-16 19:35:43 +0000616{
Mark Brown59981022009-02-16 20:49:16 +0000617 struct wm8731_priv *wm8731;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000618 int ret;
Mark Browna8035c82009-02-16 19:35:43 +0000619
Mark Brown59981022009-02-16 20:49:16 +0000620 wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
621 if (wm8731 == NULL)
622 return -ENOMEM;
623
Mark Brown59981022009-02-16 20:49:16 +0000624 i2c_set_clientdata(i2c, wm8731);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000625 wm8731->control_type = SND_SOC_I2C;
Mark Browna8035c82009-02-16 19:35:43 +0000626
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000627 ret = snd_soc_register_codec(&i2c->dev,
628 &soc_codec_dev_wm8731, &wm8731_dai, 1);
629 if (ret < 0)
630 kfree(wm8731);
631 return ret;
Mark Browna8035c82009-02-16 19:35:43 +0000632}
633
Mark Brownc6f29812009-02-18 21:25:40 +0000634static __devexit int wm8731_i2c_remove(struct i2c_client *client)
Mark Browna8035c82009-02-16 19:35:43 +0000635{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000636 snd_soc_unregister_codec(&client->dev);
637 kfree(i2c_get_clientdata(client));
Mark Browna8035c82009-02-16 19:35:43 +0000638 return 0;
639}
640
641static const struct i2c_device_id wm8731_i2c_id[] = {
642 { "wm8731", 0 },
643 { }
644};
645MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
646
647static struct i2c_driver wm8731_i2c_driver = {
648 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000649 .name = "wm8731-codec",
Mark Browna8035c82009-02-16 19:35:43 +0000650 .owner = THIS_MODULE,
651 },
652 .probe = wm8731_i2c_probe,
Mark Brownc6f29812009-02-18 21:25:40 +0000653 .remove = __devexit_p(wm8731_i2c_remove),
Mark Browna8035c82009-02-16 19:35:43 +0000654 .id_table = wm8731_i2c_id,
655};
656#endif
657
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100658static int __init wm8731_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000659{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000660 int ret = 0;
Mark Brown59981022009-02-16 20:49:16 +0000661#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
662 ret = i2c_add_driver(&wm8731_i2c_driver);
663 if (ret != 0) {
664 printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n",
665 ret);
666 }
667#endif
668#if defined(CONFIG_SPI_MASTER)
669 ret = spi_register_driver(&wm8731_spi_driver);
670 if (ret != 0) {
671 printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n",
672 ret);
673 }
674#endif
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000675 return ret;
Mark Brown64089b82008-12-08 19:17:58 +0000676}
677module_init(wm8731_modinit);
678
679static void __exit wm8731_exit(void)
680{
Mark Brown59981022009-02-16 20:49:16 +0000681#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
682 i2c_del_driver(&wm8731_i2c_driver);
683#endif
684#if defined(CONFIG_SPI_MASTER)
685 spi_unregister_driver(&wm8731_spi_driver);
686#endif
Mark Brown64089b82008-12-08 19:17:58 +0000687}
688module_exit(wm8731_exit);
689
Richard Purdie40e0aa62006-10-06 18:36:07 +0200690MODULE_DESCRIPTION("ASoC WM8731 driver");
691MODULE_AUTHOR("Richard Purdie");
692MODULE_LICENSE("GPL");