blob: 19844fc8cb1dfd8edf39e929853b573e68d25298 [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>
29#include <sound/soc-dapm.h>
30#include <sound/initval.h>
Mark Brownd00efa62009-07-08 16:53:12 +010031#include <sound/tlv.h>
Richard Purdie40e0aa62006-10-06 18:36:07 +020032
33#include "wm8731.h"
34
Mark Brown7dea7c02009-10-26 15:20:17 +000035#define WM8731_NUM_SUPPLIES 4
36static const char *wm8731_supply_names[WM8731_NUM_SUPPLIES] = {
37 "AVDD",
38 "HPVDD",
39 "DCVDD",
40 "DBVDD",
41};
42
Frank Mandarinob36d61d4562007-02-02 17:14:56 +010043/* codec private data */
44struct wm8731_priv {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000045 enum snd_soc_control_type control_type;
Mark Brown7dea7c02009-10-26 15:20:17 +000046 struct regulator_bulk_data supplies[WM8731_NUM_SUPPLIES];
Mark Brown59981022009-02-16 20:49:16 +000047 u16 reg_cache[WM8731_CACHEREGNUM];
Frank Mandarinob36d61d4562007-02-02 17:14:56 +010048 unsigned int sysclk;
49};
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"};
67static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
68
69static const struct soc_enum wm8731_enum[] = {
70 SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select),
71 SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph),
72};
73
Mark Brownd00efa62009-07-08 16:53:12 +010074static const DECLARE_TLV_DB_SCALE(in_tlv, -3450, 150, 0);
75static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -1500, 300, 0);
76static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
77
Richard Purdie40e0aa62006-10-06 18:36:07 +020078static const struct snd_kcontrol_new wm8731_snd_controls[] = {
79
Mark Brownd00efa62009-07-08 16:53:12 +010080SOC_DOUBLE_R_TLV("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
81 0, 127, 0, out_tlv),
Liam Girdwoodbd903b62006-11-09 16:35:01 +010082SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
83 7, 1, 0),
Richard Purdie40e0aa62006-10-06 18:36:07 +020084
Mark Brownd00efa62009-07-08 16:53:12 +010085SOC_DOUBLE_R_TLV("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0,
86 in_tlv),
Richard Purdie40e0aa62006-10-06 18:36:07 +020087SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
88
89SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0),
Mark Brownef38ed82009-07-08 17:05:43 +010090SOC_SINGLE("Mic Capture Switch", WM8731_APANA, 1, 1, 1),
Richard Purdie40e0aa62006-10-06 18:36:07 +020091
Mark Brownd00efa62009-07-08 16:53:12 +010092SOC_SINGLE_TLV("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1,
93 sidetone_tlv),
Richard Purdie40e0aa62006-10-06 18:36:07 +020094
95SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
96SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
97
98SOC_ENUM("Playback De-emphasis", wm8731_enum[1]),
99};
100
Richard Purdie40e0aa62006-10-06 18:36:07 +0200101/* Output Mixer */
102static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
103SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
104SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
105SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
106};
107
108/* Input mux */
109static const struct snd_kcontrol_new wm8731_input_mux_controls =
110SOC_DAPM_ENUM("Input Select", wm8731_enum[0]);
111
112static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
113SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
114 &wm8731_output_mixer_controls[0],
115 ARRAY_SIZE(wm8731_output_mixer_controls)),
116SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
117SND_SOC_DAPM_OUTPUT("LOUT"),
118SND_SOC_DAPM_OUTPUT("LHPOUT"),
119SND_SOC_DAPM_OUTPUT("ROUT"),
120SND_SOC_DAPM_OUTPUT("RHPOUT"),
121SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
122SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
123SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
124SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
125SND_SOC_DAPM_INPUT("MICIN"),
126SND_SOC_DAPM_INPUT("RLINEIN"),
127SND_SOC_DAPM_INPUT("LLINEIN"),
128};
129
Mark Browna65f0562008-05-13 14:54:43 +0200130static const struct snd_soc_dapm_route intercon[] = {
Richard Purdie40e0aa62006-10-06 18:36:07 +0200131 /* output mixer */
132 {"Output Mixer", "Line Bypass Switch", "Line Input"},
133 {"Output Mixer", "HiFi Playback Switch", "DAC"},
134 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
135
136 /* outputs */
137 {"RHPOUT", NULL, "Output Mixer"},
138 {"ROUT", NULL, "Output Mixer"},
139 {"LHPOUT", NULL, "Output Mixer"},
140 {"LOUT", NULL, "Output Mixer"},
141
142 /* input mux */
143 {"Input Mux", "Line In", "Line Input"},
144 {"Input Mux", "Mic", "Mic Bias"},
145 {"ADC", NULL, "Input Mux"},
146
147 /* inputs */
148 {"Line Input", NULL, "LLINEIN"},
149 {"Line Input", NULL, "RLINEIN"},
150 {"Mic Bias", NULL, "MICIN"},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200151};
152
153static int wm8731_add_widgets(struct snd_soc_codec *codec)
154{
Mark Browna65f0562008-05-13 14:54:43 +0200155 snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
156 ARRAY_SIZE(wm8731_dapm_widgets));
Richard Purdie40e0aa62006-10-06 18:36:07 +0200157
Mark Browna65f0562008-05-13 14:54:43 +0200158 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
Richard Purdie40e0aa62006-10-06 18:36:07 +0200159
Richard Purdie40e0aa62006-10-06 18:36:07 +0200160 return 0;
161}
162
163struct _coeff_div {
164 u32 mclk;
165 u32 rate;
166 u16 fs;
167 u8 sr:4;
168 u8 bosr:1;
169 u8 usb:1;
170};
171
172/* codec mclk clock divider coefficients */
173static const struct _coeff_div coeff_div[] = {
174 /* 48k */
175 {12288000, 48000, 256, 0x0, 0x0, 0x0},
176 {18432000, 48000, 384, 0x0, 0x1, 0x0},
177 {12000000, 48000, 250, 0x0, 0x0, 0x1},
178
179 /* 32k */
180 {12288000, 32000, 384, 0x6, 0x0, 0x0},
181 {18432000, 32000, 576, 0x6, 0x1, 0x0},
Frank Mandarino298a2c72007-01-31 10:02:56 +0100182 {12000000, 32000, 375, 0x6, 0x0, 0x1},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200183
184 /* 8k */
185 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
186 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
187 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
188 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
189 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
190
191 /* 96k */
192 {12288000, 96000, 128, 0x7, 0x0, 0x0},
193 {18432000, 96000, 192, 0x7, 0x1, 0x0},
194 {12000000, 96000, 125, 0x7, 0x0, 0x1},
195
196 /* 44.1k */
197 {11289600, 44100, 256, 0x8, 0x0, 0x0},
198 {16934400, 44100, 384, 0x8, 0x1, 0x0},
199 {12000000, 44100, 272, 0x8, 0x1, 0x1},
200
201 /* 88.2k */
202 {11289600, 88200, 128, 0xf, 0x0, 0x0},
203 {16934400, 88200, 192, 0xf, 0x1, 0x0},
204 {12000000, 88200, 136, 0xf, 0x1, 0x1},
205};
206
207static inline int get_coeff(int mclk, int rate)
208{
209 int i;
210
211 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
212 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
213 return i;
214 }
215 return 0;
216}
217
Frank Mandarinob36d61d4562007-02-02 17:14:56 +0100218static int wm8731_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000219 struct snd_pcm_hw_params *params,
220 struct snd_soc_dai *dai)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200221{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000222 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900223 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
Mark Brown17a52fd2009-07-05 17:24:50 +0100224 u16 iface = snd_soc_read(codec, WM8731_IFACE) & 0xfff3;
Frank Mandarinob36d61d4562007-02-02 17:14:56 +0100225 int i = get_coeff(wm8731->sysclk, params_rate(params));
226 u16 srate = (coeff_div[i].sr << 2) |
227 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200228
Mark Brown17a52fd2009-07-05 17:24:50 +0100229 snd_soc_write(codec, WM8731_SRATE, srate);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200230
Frank Mandarinob36d61d4562007-02-02 17:14:56 +0100231 /* bit size */
232 switch (params_format(params)) {
233 case SNDRV_PCM_FORMAT_S16_LE:
234 break;
235 case SNDRV_PCM_FORMAT_S20_3LE:
236 iface |= 0x0004;
237 break;
238 case SNDRV_PCM_FORMAT_S24_LE:
239 iface |= 0x0008;
240 break;
241 }
242
Mark Brown17a52fd2009-07-05 17:24:50 +0100243 snd_soc_write(codec, WM8731_IFACE, iface);
Frank Mandarinob36d61d4562007-02-02 17:14:56 +0100244 return 0;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200245}
246
Mark Browndee89c42008-11-18 22:11:38 +0000247static int wm8731_pcm_prepare(struct snd_pcm_substream *substream,
248 struct snd_soc_dai *dai)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200249{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000250 struct snd_soc_codec *codec = dai->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200251
252 /* set active */
Mark Brown17a52fd2009-07-05 17:24:50 +0100253 snd_soc_write(codec, WM8731_ACTIVE, 0x0001);
Frank Mandarinob36d61d4562007-02-02 17:14:56 +0100254
Richard Purdie40e0aa62006-10-06 18:36:07 +0200255 return 0;
256}
257
Mark Browndee89c42008-11-18 22:11:38 +0000258static void wm8731_shutdown(struct snd_pcm_substream *substream,
259 struct snd_soc_dai *dai)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200260{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000261 struct snd_soc_codec *codec = dai->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200262
263 /* deactivate */
264 if (!codec->active) {
265 udelay(50);
Mark Brown17a52fd2009-07-05 17:24:50 +0100266 snd_soc_write(codec, WM8731_ACTIVE, 0x0);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200267 }
268}
269
Liam Girdwoode550e172008-07-07 16:07:52 +0100270static int wm8731_mute(struct snd_soc_dai *dai, int mute)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200271{
Frank Mandarinob36d61d4562007-02-02 17:14:56 +0100272 struct snd_soc_codec *codec = dai->codec;
Mark Brown17a52fd2009-07-05 17:24:50 +0100273 u16 mute_reg = snd_soc_read(codec, WM8731_APDIGI) & 0xfff7;
Frank Mandarinob36d61d4562007-02-02 17:14:56 +0100274
Richard Purdie40e0aa62006-10-06 18:36:07 +0200275 if (mute)
Mark Brown17a52fd2009-07-05 17:24:50 +0100276 snd_soc_write(codec, WM8731_APDIGI, mute_reg | 0x8);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200277 else
Mark Brown17a52fd2009-07-05 17:24:50 +0100278 snd_soc_write(codec, WM8731_APDIGI, mute_reg);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200279 return 0;
280}
281
Liam Girdwoode550e172008-07-07 16:07:52 +0100282static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
Frank Mandarinob36d61d4562007-02-02 17:14:56 +0100283 int clk_id, unsigned int freq, int dir)
284{
285 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900286 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
Frank Mandarinob36d61d4562007-02-02 17:14:56 +0100287
288 switch (freq) {
289 case 11289600:
290 case 12000000:
291 case 12288000:
292 case 16934400:
293 case 18432000:
294 wm8731->sysclk = freq;
295 return 0;
296 }
297 return -EINVAL;
298}
299
300
Liam Girdwoode550e172008-07-07 16:07:52 +0100301static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
Frank Mandarinob36d61d4562007-02-02 17:14:56 +0100302 unsigned int fmt)
303{
304 struct snd_soc_codec *codec = codec_dai->codec;
305 u16 iface = 0;
306
307 /* set master/slave audio interface */
308 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
309 case SND_SOC_DAIFMT_CBM_CFM:
310 iface |= 0x0040;
311 break;
312 case SND_SOC_DAIFMT_CBS_CFS:
313 break;
314 default:
315 return -EINVAL;
316 }
317
318 /* interface format */
319 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
320 case SND_SOC_DAIFMT_I2S:
321 iface |= 0x0002;
322 break;
323 case SND_SOC_DAIFMT_RIGHT_J:
324 break;
325 case SND_SOC_DAIFMT_LEFT_J:
326 iface |= 0x0001;
327 break;
328 case SND_SOC_DAIFMT_DSP_A:
329 iface |= 0x0003;
330 break;
331 case SND_SOC_DAIFMT_DSP_B:
332 iface |= 0x0013;
333 break;
334 default:
335 return -EINVAL;
336 }
337
338 /* clock inversion */
339 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
340 case SND_SOC_DAIFMT_NB_NF:
341 break;
342 case SND_SOC_DAIFMT_IB_IF:
343 iface |= 0x0090;
344 break;
345 case SND_SOC_DAIFMT_IB_NF:
346 iface |= 0x0080;
347 break;
348 case SND_SOC_DAIFMT_NB_IF:
349 iface |= 0x0010;
350 break;
351 default:
352 return -EINVAL;
353 }
354
355 /* set iface */
Mark Brown17a52fd2009-07-05 17:24:50 +0100356 snd_soc_write(codec, WM8731_IFACE, iface);
Frank Mandarinob36d61d4562007-02-02 17:14:56 +0100357 return 0;
358}
359
Mark Brown0be98982008-05-19 12:31:28 +0200360static int wm8731_set_bias_level(struct snd_soc_codec *codec,
361 enum snd_soc_bias_level level)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200362{
Mark Brown06ae9982010-05-07 19:14:45 +0100363 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
364 int i, ret;
365 u8 data[2];
366 u16 *cache = codec->reg_cache;
Mark Brown22d22ee2009-02-16 19:20:15 +0000367 u16 reg;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200368
Mark Brown0be98982008-05-19 12:31:28 +0200369 switch (level) {
370 case SND_SOC_BIAS_ON:
Richard Purdie40e0aa62006-10-06 18:36:07 +0200371 break;
Mark Brown0be98982008-05-19 12:31:28 +0200372 case SND_SOC_BIAS_PREPARE:
Richard Purdie40e0aa62006-10-06 18:36:07 +0200373 break;
Mark Brown0be98982008-05-19 12:31:28 +0200374 case SND_SOC_BIAS_STANDBY:
Mark Brown06ae9982010-05-07 19:14:45 +0100375 if (codec->bias_level == SND_SOC_BIAS_OFF) {
376 ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
377 wm8731->supplies);
378 if (ret != 0)
379 return ret;
380
381 /* Sync reg_cache with the hardware */
382 for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
383 if (cache[i] == wm8731_reg[i])
384 continue;
385
386 data[0] = (i << 1) | ((cache[i] >> 8)
387 & 0x0001);
388 data[1] = cache[i] & 0x00ff;
389 codec->hw_write(codec->control_data, data, 2);
390 }
391 }
392
Mark Brown22d22ee2009-02-16 19:20:15 +0000393 /* Clear PWROFF, gate CLKOUT, everything else as-is */
Mark Brown17a52fd2009-07-05 17:24:50 +0100394 reg = snd_soc_read(codec, WM8731_PWR) & 0xff7f;
395 snd_soc_write(codec, WM8731_PWR, reg | 0x0040);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200396 break;
Mark Brown0be98982008-05-19 12:31:28 +0200397 case SND_SOC_BIAS_OFF:
Mark Brown17a52fd2009-07-05 17:24:50 +0100398 snd_soc_write(codec, WM8731_ACTIVE, 0x0);
399 snd_soc_write(codec, WM8731_PWR, 0xffff);
Mark Brown06ae9982010-05-07 19:14:45 +0100400 regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies),
401 wm8731->supplies);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200402 break;
403 }
Mark Brown0be98982008-05-19 12:31:28 +0200404 codec->bias_level = level;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200405 return 0;
406}
407
Bill Gatliffe1354432010-04-09 18:08:08 +0100408#define WM8731_RATES SNDRV_PCM_RATE_8000_96000
Frank Mandarinob36d61d4562007-02-02 17:14:56 +0100409
410#define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
411 SNDRV_PCM_FMTBIT_S24_LE)
412
Eric Miao6335d052009-03-03 09:41:00 +0800413static struct snd_soc_dai_ops wm8731_dai_ops = {
414 .prepare = wm8731_pcm_prepare,
415 .hw_params = wm8731_hw_params,
416 .shutdown = wm8731_shutdown,
417 .digital_mute = wm8731_mute,
418 .set_sysclk = wm8731_set_dai_sysclk,
419 .set_fmt = wm8731_set_dai_fmt,
420};
421
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000422static struct snd_soc_dai_driver wm8731_dai = {
423 .name = "wm8731-hifi",
Richard Purdie40e0aa62006-10-06 18:36:07 +0200424 .playback = {
425 .stream_name = "Playback",
426 .channels_min = 1,
427 .channels_max = 2,
Frank Mandarinob36d61d4562007-02-02 17:14:56 +0100428 .rates = WM8731_RATES,
429 .formats = WM8731_FORMATS,},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200430 .capture = {
431 .stream_name = "Capture",
432 .channels_min = 1,
433 .channels_max = 2,
Frank Mandarinob36d61d4562007-02-02 17:14:56 +0100434 .rates = WM8731_RATES,
435 .formats = WM8731_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800436 .ops = &wm8731_dai_ops,
Mark Brown49344822009-07-08 17:48:12 +0100437 .symmetric_rates = 1,
Richard Purdie40e0aa62006-10-06 18:36:07 +0200438};
Richard Purdie40e0aa62006-10-06 18:36:07 +0200439
Mark Brownb3b50b32009-06-13 22:30:18 +0100440#ifdef CONFIG_PM
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000441static int wm8731_suspend(struct snd_soc_codec *codec, pm_message_t state)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200442{
Mark Brown0be98982008-05-19 12:31:28 +0200443 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
Mark Brown06ae9982010-05-07 19:14:45 +0100444
Richard Purdie40e0aa62006-10-06 18:36:07 +0200445 return 0;
446}
447
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000448static int wm8731_resume(struct snd_soc_codec *codec)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200449{
Mark Brown0be98982008-05-19 12:31:28 +0200450 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Mark Brown7dea7c02009-10-26 15:20:17 +0000451
Richard Purdie40e0aa62006-10-06 18:36:07 +0200452 return 0;
453}
Mark Brownb3b50b32009-06-13 22:30:18 +0100454#else
455#define wm8731_suspend NULL
456#define wm8731_resume NULL
457#endif
Richard Purdie40e0aa62006-10-06 18:36:07 +0200458
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000459static int wm8731_probe(struct snd_soc_codec *codec)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200460{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000461 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
462 int ret = 0, i;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200463
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000464 codec->bias_level = SND_SOC_BIAS_OFF,
Richard Purdie40e0aa62006-10-06 18:36:07 +0200465
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000466 ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8731->control_type);
Mark Brown17a52fd2009-07-05 17:24:50 +0100467 if (ret < 0) {
468 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000469 return ret;
Mark Brown17a52fd2009-07-05 17:24:50 +0100470 }
471
Mark Brown7dea7c02009-10-26 15:20:17 +0000472 for (i = 0; i < ARRAY_SIZE(wm8731->supplies); i++)
473 wm8731->supplies[i].supply = wm8731_supply_names[i];
474
475 ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8731->supplies),
476 wm8731->supplies);
477 if (ret != 0) {
478 dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000479 return ret;
Mark Brown7dea7c02009-10-26 15:20:17 +0000480 }
481
482 ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
483 wm8731->supplies);
484 if (ret != 0) {
485 dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
486 goto err_regulator_get;
487 }
488
Mark Brown519cf2d2009-02-18 21:06:01 +0000489 ret = wm8731_reset(codec);
490 if (ret < 0) {
Mark Brownfe5422f2009-07-05 15:18:01 +0100491 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
Mark Brown7dea7c02009-10-26 15:20:17 +0000492 goto err_regulator_enable;
Mark Brown519cf2d2009-02-18 21:06:01 +0000493 }
494
Mark Brown59981022009-02-16 20:49:16 +0000495 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
496
497 /* Latch the update bits */
Mark Brown17a52fd2009-07-05 17:24:50 +0100498 snd_soc_update_bits(codec, WM8731_LOUT1V, 0x100, 0);
499 snd_soc_update_bits(codec, WM8731_ROUT1V, 0x100, 0);
500 snd_soc_update_bits(codec, WM8731_LINVOL, 0x100, 0);
501 snd_soc_update_bits(codec, WM8731_RINVOL, 0x100, 0);
Mark Brown59981022009-02-16 20:49:16 +0000502
Mark Brownce3bdaa2009-02-19 14:29:49 +0000503 /* Disable bypass path by default */
Mark Brown17a52fd2009-07-05 17:24:50 +0100504 snd_soc_update_bits(codec, WM8731_APANA, 0x4, 0);
Mark Brownce3bdaa2009-02-19 14:29:49 +0000505
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000506 snd_soc_add_controls(codec, wm8731_snd_controls,
507 ARRAY_SIZE(wm8731_snd_controls));
508 wm8731_add_widgets(codec);
Mark Brown59981022009-02-16 20:49:16 +0000509
Mark Brown06ae9982010-05-07 19:14:45 +0100510 /* Regulators will have been enabled by bias management */
511 regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
512
Mark Browna8035c82009-02-16 19:35:43 +0000513 return 0;
Mark Brownfe5422f2009-07-05 15:18:01 +0100514
Mark Brown7dea7c02009-10-26 15:20:17 +0000515err_regulator_enable:
516 regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
517err_regulator_get:
518 regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000519
Mark Brownfe5422f2009-07-05 15:18:01 +0100520 kfree(wm8731);
521 return ret;
Mark Browna8035c82009-02-16 19:35:43 +0000522}
523
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000524/* power down chip */
525static int wm8731_remove(struct snd_soc_codec *codec)
Mark Brown59981022009-02-16 20:49:16 +0000526{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000527 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
528
529 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
530
531 regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
Mark Brown7dea7c02009-10-26 15:20:17 +0000532 regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000533
534 return 0;
Mark Brown59981022009-02-16 20:49:16 +0000535}
Mark Browna8035c82009-02-16 19:35:43 +0000536
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000537static struct snd_soc_codec_driver soc_codec_dev_wm8731 = {
538 .probe = wm8731_probe,
539 .remove = wm8731_remove,
540 .suspend = wm8731_suspend,
541 .resume = wm8731_resume,
542 .set_bias_level = wm8731_set_bias_level,
543 .reg_cache_size = sizeof(wm8731_reg),
544 .reg_word_size = sizeof(u16),
545 .reg_cache_default = wm8731_reg,
546};
547
Mark Brown59981022009-02-16 20:49:16 +0000548#if defined(CONFIG_SPI_MASTER)
Mark Brown59981022009-02-16 20:49:16 +0000549static int __devinit wm8731_spi_probe(struct spi_device *spi)
550{
Mark Brown59981022009-02-16 20:49:16 +0000551 struct wm8731_priv *wm8731;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000552 int ret;
Mark Brown59981022009-02-16 20:49:16 +0000553
554 wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
555 if (wm8731 == NULL)
556 return -ENOMEM;
557
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000558 wm8731->control_type = SND_SOC_SPI;
559 spi_set_drvdata(spi, wm8731);
Mark Brown59981022009-02-16 20:49:16 +0000560
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000561 ret = snd_soc_register_codec(&spi->dev,
562 &soc_codec_dev_wm8731, &wm8731_dai, 1);
563 if (ret < 0)
564 kfree(wm8731);
565 return ret;
Mark Brown59981022009-02-16 20:49:16 +0000566}
567
568static int __devexit wm8731_spi_remove(struct spi_device *spi)
569{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000570 snd_soc_unregister_codec(&spi->dev);
571 kfree(spi_get_drvdata(spi));
Mark Brown59981022009-02-16 20:49:16 +0000572 return 0;
573}
574
575static struct spi_driver wm8731_spi_driver = {
576 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000577 .name = "wm8731-codec",
Mark Brown59981022009-02-16 20:49:16 +0000578 .bus = &spi_bus_type,
579 .owner = THIS_MODULE,
580 },
581 .probe = wm8731_spi_probe,
582 .remove = __devexit_p(wm8731_spi_remove),
583};
Mark Browna8035c82009-02-16 19:35:43 +0000584#endif /* CONFIG_SPI_MASTER */
585
586#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Mark Brownc6f29812009-02-18 21:25:40 +0000587static __devinit int wm8731_i2c_probe(struct i2c_client *i2c,
588 const struct i2c_device_id *id)
Mark Browna8035c82009-02-16 19:35:43 +0000589{
Mark Brown59981022009-02-16 20:49:16 +0000590 struct wm8731_priv *wm8731;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000591 int ret;
Mark Browna8035c82009-02-16 19:35:43 +0000592
Mark Brown59981022009-02-16 20:49:16 +0000593 wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
594 if (wm8731 == NULL)
595 return -ENOMEM;
596
Mark Brown59981022009-02-16 20:49:16 +0000597 i2c_set_clientdata(i2c, wm8731);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000598 wm8731->control_type = SND_SOC_I2C;
Mark Browna8035c82009-02-16 19:35:43 +0000599
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000600 ret = snd_soc_register_codec(&i2c->dev,
601 &soc_codec_dev_wm8731, &wm8731_dai, 1);
602 if (ret < 0)
603 kfree(wm8731);
604 return ret;
Mark Browna8035c82009-02-16 19:35:43 +0000605}
606
Mark Brownc6f29812009-02-18 21:25:40 +0000607static __devexit int wm8731_i2c_remove(struct i2c_client *client)
Mark Browna8035c82009-02-16 19:35:43 +0000608{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000609 snd_soc_unregister_codec(&client->dev);
610 kfree(i2c_get_clientdata(client));
Mark Browna8035c82009-02-16 19:35:43 +0000611 return 0;
612}
613
614static const struct i2c_device_id wm8731_i2c_id[] = {
615 { "wm8731", 0 },
616 { }
617};
618MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
619
620static struct i2c_driver wm8731_i2c_driver = {
621 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000622 .name = "wm8731-codec",
Mark Browna8035c82009-02-16 19:35:43 +0000623 .owner = THIS_MODULE,
624 },
625 .probe = wm8731_i2c_probe,
Mark Brownc6f29812009-02-18 21:25:40 +0000626 .remove = __devexit_p(wm8731_i2c_remove),
Mark Browna8035c82009-02-16 19:35:43 +0000627 .id_table = wm8731_i2c_id,
628};
629#endif
630
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100631static int __init wm8731_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000632{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000633 int ret = 0;
Mark Brown59981022009-02-16 20:49:16 +0000634#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
635 ret = i2c_add_driver(&wm8731_i2c_driver);
636 if (ret != 0) {
637 printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n",
638 ret);
639 }
640#endif
641#if defined(CONFIG_SPI_MASTER)
642 ret = spi_register_driver(&wm8731_spi_driver);
643 if (ret != 0) {
644 printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n",
645 ret);
646 }
647#endif
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000648 return ret;
Mark Brown64089b82008-12-08 19:17:58 +0000649}
650module_init(wm8731_modinit);
651
652static void __exit wm8731_exit(void)
653{
Mark Brown59981022009-02-16 20:49:16 +0000654#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
655 i2c_del_driver(&wm8731_i2c_driver);
656#endif
657#if defined(CONFIG_SPI_MASTER)
658 spi_unregister_driver(&wm8731_spi_driver);
659#endif
Mark Brown64089b82008-12-08 19:17:58 +0000660}
661module_exit(wm8731_exit);
662
Richard Purdie40e0aa62006-10-06 18:36:07 +0200663MODULE_DESCRIPTION("ASoC WM8731 driver");
664MODULE_AUTHOR("Richard Purdie");
665MODULE_LICENSE("GPL");