blob: 56f540838745a0407d3f1611cbd50eac4b8377b8 [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 Mandarinob36d61d2007-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 Mandarinob36d61d2007-02-02 17:14:56 +010048 unsigned int sysclk;
Mark Browne7712512010-08-17 23:40:24 +010049 int sysclk_type;
Frank Mandarinob36d61d2007-02-02 17:14:56 +010050};
51
Mark Browna8035c82009-02-16 19:35:43 +000052
Richard Purdie40e0aa62006-10-06 18:36:07 +020053/*
54 * wm8731 register cache
55 * We can't read the WM8731 register space when we are
56 * using 2 wire for device control, so we cache them instead.
57 * There is no point in caching the reset register
58 */
59static const u16 wm8731_reg[WM8731_CACHEREGNUM] = {
Mark Brown17a52fd2009-07-05 17:24:50 +010060 0x0097, 0x0097, 0x0079, 0x0079,
61 0x000a, 0x0008, 0x009f, 0x000a,
62 0x0000, 0x0000
Richard Purdie40e0aa62006-10-06 18:36:07 +020063};
64
Mark Brown17a52fd2009-07-05 17:24:50 +010065#define wm8731_reset(c) snd_soc_write(c, WM8731_RESET, 0)
Richard Purdie40e0aa62006-10-06 18:36:07 +020066
67static const char *wm8731_input_select[] = {"Line In", "Mic"};
68static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
69
70static const struct soc_enum wm8731_enum[] = {
71 SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select),
72 SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph),
73};
74
Mark Brownd00efa62009-07-08 16:53:12 +010075static const DECLARE_TLV_DB_SCALE(in_tlv, -3450, 150, 0);
76static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -1500, 300, 0);
77static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
78
Richard Purdie40e0aa62006-10-06 18:36:07 +020079static const struct snd_kcontrol_new wm8731_snd_controls[] = {
80
Mark Brownd00efa62009-07-08 16:53:12 +010081SOC_DOUBLE_R_TLV("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
82 0, 127, 0, out_tlv),
Liam Girdwoodbd903b62006-11-09 16:35:01 +010083SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
84 7, 1, 0),
Richard Purdie40e0aa62006-10-06 18:36:07 +020085
Mark Brownd00efa62009-07-08 16:53:12 +010086SOC_DOUBLE_R_TLV("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0,
87 in_tlv),
Richard Purdie40e0aa62006-10-06 18:36:07 +020088SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
89
90SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0),
Mark Brownef38ed82009-07-08 17:05:43 +010091SOC_SINGLE("Mic Capture Switch", WM8731_APANA, 1, 1, 1),
Richard Purdie40e0aa62006-10-06 18:36:07 +020092
Mark Brownd00efa62009-07-08 16:53:12 +010093SOC_SINGLE_TLV("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1,
94 sidetone_tlv),
Richard Purdie40e0aa62006-10-06 18:36:07 +020095
96SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
97SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
98
99SOC_ENUM("Playback De-emphasis", wm8731_enum[1]),
100};
101
Richard Purdie40e0aa62006-10-06 18:36:07 +0200102/* Output Mixer */
103static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
104SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
105SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
106SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
107};
108
109/* Input mux */
110static const struct snd_kcontrol_new wm8731_input_mux_controls =
111SOC_DAPM_ENUM("Input Select", wm8731_enum[0]);
112
113static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
Mark Browne7712512010-08-17 23:40:24 +0100114SND_SOC_DAPM_SUPPLY("OSC", WM8731_PWR, 5, 1, NULL, 0),
Richard Purdie40e0aa62006-10-06 18:36:07 +0200115SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
116 &wm8731_output_mixer_controls[0],
117 ARRAY_SIZE(wm8731_output_mixer_controls)),
118SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
119SND_SOC_DAPM_OUTPUT("LOUT"),
120SND_SOC_DAPM_OUTPUT("LHPOUT"),
121SND_SOC_DAPM_OUTPUT("ROUT"),
122SND_SOC_DAPM_OUTPUT("RHPOUT"),
123SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
124SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
125SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
126SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
127SND_SOC_DAPM_INPUT("MICIN"),
128SND_SOC_DAPM_INPUT("RLINEIN"),
129SND_SOC_DAPM_INPUT("LLINEIN"),
130};
131
Mark Browne7712512010-08-17 23:40:24 +0100132static int wm8731_check_osc(struct snd_soc_dapm_widget *source,
133 struct snd_soc_dapm_widget *sink)
134{
135 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(source->codec);
136
137 return wm8731->sysclk_type == WM8731_SYSCLK_MCLK;
138}
139
Mark Browna65f0562008-05-13 14:54:43 +0200140static const struct snd_soc_dapm_route intercon[] = {
Mark Browne7712512010-08-17 23:40:24 +0100141 {"DAC", NULL, "OSC", wm8731_check_osc},
142 {"ADC", NULL, "OSC", wm8731_check_osc},
143
Richard Purdie40e0aa62006-10-06 18:36:07 +0200144 /* output mixer */
145 {"Output Mixer", "Line Bypass Switch", "Line Input"},
146 {"Output Mixer", "HiFi Playback Switch", "DAC"},
147 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
148
149 /* outputs */
150 {"RHPOUT", NULL, "Output Mixer"},
151 {"ROUT", NULL, "Output Mixer"},
152 {"LHPOUT", NULL, "Output Mixer"},
153 {"LOUT", NULL, "Output Mixer"},
154
155 /* input mux */
156 {"Input Mux", "Line In", "Line Input"},
157 {"Input Mux", "Mic", "Mic Bias"},
158 {"ADC", NULL, "Input Mux"},
159
160 /* inputs */
161 {"Line Input", NULL, "LLINEIN"},
162 {"Line Input", NULL, "RLINEIN"},
163 {"Mic Bias", NULL, "MICIN"},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200164};
165
166static int wm8731_add_widgets(struct snd_soc_codec *codec)
167{
Mark Browna65f0562008-05-13 14:54:43 +0200168 snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
169 ARRAY_SIZE(wm8731_dapm_widgets));
Richard Purdie40e0aa62006-10-06 18:36:07 +0200170
Mark Browna65f0562008-05-13 14:54:43 +0200171 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
Richard Purdie40e0aa62006-10-06 18:36:07 +0200172
Richard Purdie40e0aa62006-10-06 18:36:07 +0200173 return 0;
174}
175
176struct _coeff_div {
177 u32 mclk;
178 u32 rate;
179 u16 fs;
180 u8 sr:4;
181 u8 bosr:1;
182 u8 usb:1;
183};
184
185/* codec mclk clock divider coefficients */
186static const struct _coeff_div coeff_div[] = {
187 /* 48k */
188 {12288000, 48000, 256, 0x0, 0x0, 0x0},
189 {18432000, 48000, 384, 0x0, 0x1, 0x0},
190 {12000000, 48000, 250, 0x0, 0x0, 0x1},
191
192 /* 32k */
193 {12288000, 32000, 384, 0x6, 0x0, 0x0},
194 {18432000, 32000, 576, 0x6, 0x1, 0x0},
Frank Mandarino298a2c72007-01-31 10:02:56 +0100195 {12000000, 32000, 375, 0x6, 0x0, 0x1},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200196
197 /* 8k */
198 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
199 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
200 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
201 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
202 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
203
204 /* 96k */
205 {12288000, 96000, 128, 0x7, 0x0, 0x0},
206 {18432000, 96000, 192, 0x7, 0x1, 0x0},
207 {12000000, 96000, 125, 0x7, 0x0, 0x1},
208
209 /* 44.1k */
210 {11289600, 44100, 256, 0x8, 0x0, 0x0},
211 {16934400, 44100, 384, 0x8, 0x1, 0x0},
212 {12000000, 44100, 272, 0x8, 0x1, 0x1},
213
214 /* 88.2k */
215 {11289600, 88200, 128, 0xf, 0x0, 0x0},
216 {16934400, 88200, 192, 0xf, 0x1, 0x0},
217 {12000000, 88200, 136, 0xf, 0x1, 0x1},
218};
219
220static inline int get_coeff(int mclk, int rate)
221{
222 int i;
223
224 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
225 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
226 return i;
227 }
228 return 0;
229}
230
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100231static int wm8731_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000232 struct snd_pcm_hw_params *params,
233 struct snd_soc_dai *dai)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200234{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000235 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900236 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
Mark Brown17a52fd2009-07-05 17:24:50 +0100237 u16 iface = snd_soc_read(codec, WM8731_IFACE) & 0xfff3;
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100238 int i = get_coeff(wm8731->sysclk, params_rate(params));
239 u16 srate = (coeff_div[i].sr << 2) |
240 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200241
Mark Brown17a52fd2009-07-05 17:24:50 +0100242 snd_soc_write(codec, WM8731_SRATE, srate);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200243
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100244 /* bit size */
245 switch (params_format(params)) {
246 case SNDRV_PCM_FORMAT_S16_LE:
247 break;
248 case SNDRV_PCM_FORMAT_S20_3LE:
249 iface |= 0x0004;
250 break;
251 case SNDRV_PCM_FORMAT_S24_LE:
252 iface |= 0x0008;
253 break;
254 }
255
Mark Brown17a52fd2009-07-05 17:24:50 +0100256 snd_soc_write(codec, WM8731_IFACE, iface);
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100257 return 0;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200258}
259
Mark Browndee89c42008-11-18 22:11:38 +0000260static int wm8731_pcm_prepare(struct snd_pcm_substream *substream,
261 struct snd_soc_dai *dai)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200262{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000263 struct snd_soc_codec *codec = dai->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200264
265 /* set active */
Mark Brown17a52fd2009-07-05 17:24:50 +0100266 snd_soc_write(codec, WM8731_ACTIVE, 0x0001);
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100267
Richard Purdie40e0aa62006-10-06 18:36:07 +0200268 return 0;
269}
270
Mark Browndee89c42008-11-18 22:11:38 +0000271static void wm8731_shutdown(struct snd_pcm_substream *substream,
272 struct snd_soc_dai *dai)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200273{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000274 struct snd_soc_codec *codec = dai->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200275
276 /* deactivate */
277 if (!codec->active) {
278 udelay(50);
Mark Brown17a52fd2009-07-05 17:24:50 +0100279 snd_soc_write(codec, WM8731_ACTIVE, 0x0);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200280 }
281}
282
Liam Girdwoode550e172008-07-07 16:07:52 +0100283static int wm8731_mute(struct snd_soc_dai *dai, int mute)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200284{
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100285 struct snd_soc_codec *codec = dai->codec;
Mark Brown17a52fd2009-07-05 17:24:50 +0100286 u16 mute_reg = snd_soc_read(codec, WM8731_APDIGI) & 0xfff7;
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100287
Richard Purdie40e0aa62006-10-06 18:36:07 +0200288 if (mute)
Mark Brown17a52fd2009-07-05 17:24:50 +0100289 snd_soc_write(codec, WM8731_APDIGI, mute_reg | 0x8);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200290 else
Mark Brown17a52fd2009-07-05 17:24:50 +0100291 snd_soc_write(codec, WM8731_APDIGI, mute_reg);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200292 return 0;
293}
294
Liam Girdwoode550e172008-07-07 16:07:52 +0100295static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100296 int clk_id, unsigned int freq, int dir)
297{
298 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900299 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100300
Mark Browne7712512010-08-17 23:40:24 +0100301 switch (clk_id) {
302 case WM8731_SYSCLK_XTAL:
303 case WM8731_SYSCLK_MCLK:
304 wm8731->sysclk_type = clk_id;
305 break;
306 default:
307 return -EINVAL;
308 }
309
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100310 switch (freq) {
311 case 11289600:
312 case 12000000:
313 case 12288000:
314 case 16934400:
315 case 18432000:
316 wm8731->sysclk = freq;
Mark Browne7712512010-08-17 23:40:24 +0100317 break;
318 default:
319 return -EINVAL;
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100320 }
Mark Browne7712512010-08-17 23:40:24 +0100321
322 snd_soc_dapm_sync(codec);
323
324 return 0;
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100325}
326
327
Liam Girdwoode550e172008-07-07 16:07:52 +0100328static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100329 unsigned int fmt)
330{
331 struct snd_soc_codec *codec = codec_dai->codec;
332 u16 iface = 0;
333
334 /* set master/slave audio interface */
335 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
336 case SND_SOC_DAIFMT_CBM_CFM:
337 iface |= 0x0040;
338 break;
339 case SND_SOC_DAIFMT_CBS_CFS:
340 break;
341 default:
342 return -EINVAL;
343 }
344
345 /* interface format */
346 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
347 case SND_SOC_DAIFMT_I2S:
348 iface |= 0x0002;
349 break;
350 case SND_SOC_DAIFMT_RIGHT_J:
351 break;
352 case SND_SOC_DAIFMT_LEFT_J:
353 iface |= 0x0001;
354 break;
355 case SND_SOC_DAIFMT_DSP_A:
356 iface |= 0x0003;
357 break;
358 case SND_SOC_DAIFMT_DSP_B:
359 iface |= 0x0013;
360 break;
361 default:
362 return -EINVAL;
363 }
364
365 /* clock inversion */
366 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
367 case SND_SOC_DAIFMT_NB_NF:
368 break;
369 case SND_SOC_DAIFMT_IB_IF:
370 iface |= 0x0090;
371 break;
372 case SND_SOC_DAIFMT_IB_NF:
373 iface |= 0x0080;
374 break;
375 case SND_SOC_DAIFMT_NB_IF:
376 iface |= 0x0010;
377 break;
378 default:
379 return -EINVAL;
380 }
381
382 /* set iface */
Mark Brown17a52fd2009-07-05 17:24:50 +0100383 snd_soc_write(codec, WM8731_IFACE, iface);
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100384 return 0;
385}
386
Mark Brown0be98982008-05-19 12:31:28 +0200387static int wm8731_set_bias_level(struct snd_soc_codec *codec,
388 enum snd_soc_bias_level level)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200389{
Mark Brown06ae9982010-05-07 19:14:45 +0100390 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
391 int i, ret;
392 u8 data[2];
393 u16 *cache = codec->reg_cache;
Mark Brown22d22ee2009-02-16 19:20:15 +0000394 u16 reg;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200395
Mark Brown0be98982008-05-19 12:31:28 +0200396 switch (level) {
397 case SND_SOC_BIAS_ON:
Richard Purdie40e0aa62006-10-06 18:36:07 +0200398 break;
Mark Brown0be98982008-05-19 12:31:28 +0200399 case SND_SOC_BIAS_PREPARE:
Richard Purdie40e0aa62006-10-06 18:36:07 +0200400 break;
Mark Brown0be98982008-05-19 12:31:28 +0200401 case SND_SOC_BIAS_STANDBY:
Mark Brown06ae9982010-05-07 19:14:45 +0100402 if (codec->bias_level == SND_SOC_BIAS_OFF) {
403 ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
404 wm8731->supplies);
405 if (ret != 0)
406 return ret;
407
408 /* Sync reg_cache with the hardware */
409 for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
410 if (cache[i] == wm8731_reg[i])
411 continue;
412
413 data[0] = (i << 1) | ((cache[i] >> 8)
414 & 0x0001);
415 data[1] = cache[i] & 0x00ff;
416 codec->hw_write(codec->control_data, data, 2);
417 }
418 }
419
Mark Brown22d22ee2009-02-16 19:20:15 +0000420 /* Clear PWROFF, gate CLKOUT, everything else as-is */
Mark Brown17a52fd2009-07-05 17:24:50 +0100421 reg = snd_soc_read(codec, WM8731_PWR) & 0xff7f;
422 snd_soc_write(codec, WM8731_PWR, reg | 0x0040);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200423 break;
Mark Brown0be98982008-05-19 12:31:28 +0200424 case SND_SOC_BIAS_OFF:
Mark Brown17a52fd2009-07-05 17:24:50 +0100425 snd_soc_write(codec, WM8731_ACTIVE, 0x0);
426 snd_soc_write(codec, WM8731_PWR, 0xffff);
Mark Brown06ae9982010-05-07 19:14:45 +0100427 regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies),
428 wm8731->supplies);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200429 break;
430 }
Mark Brown0be98982008-05-19 12:31:28 +0200431 codec->bias_level = level;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200432 return 0;
433}
434
Bill Gatliffe1354432010-04-09 18:08:08 +0100435#define WM8731_RATES SNDRV_PCM_RATE_8000_96000
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100436
437#define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
438 SNDRV_PCM_FMTBIT_S24_LE)
439
Eric Miao6335d052009-03-03 09:41:00 +0800440static struct snd_soc_dai_ops wm8731_dai_ops = {
441 .prepare = wm8731_pcm_prepare,
442 .hw_params = wm8731_hw_params,
443 .shutdown = wm8731_shutdown,
444 .digital_mute = wm8731_mute,
445 .set_sysclk = wm8731_set_dai_sysclk,
446 .set_fmt = wm8731_set_dai_fmt,
447};
448
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000449static struct snd_soc_dai_driver wm8731_dai = {
450 .name = "wm8731-hifi",
Richard Purdie40e0aa62006-10-06 18:36:07 +0200451 .playback = {
452 .stream_name = "Playback",
453 .channels_min = 1,
454 .channels_max = 2,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100455 .rates = WM8731_RATES,
456 .formats = WM8731_FORMATS,},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200457 .capture = {
458 .stream_name = "Capture",
459 .channels_min = 1,
460 .channels_max = 2,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100461 .rates = WM8731_RATES,
462 .formats = WM8731_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800463 .ops = &wm8731_dai_ops,
Mark Brown49344822009-07-08 17:48:12 +0100464 .symmetric_rates = 1,
Richard Purdie40e0aa62006-10-06 18:36:07 +0200465};
Richard Purdie40e0aa62006-10-06 18:36:07 +0200466
Mark Brownb3b50b32009-06-13 22:30:18 +0100467#ifdef CONFIG_PM
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000468static int wm8731_suspend(struct snd_soc_codec *codec, pm_message_t state)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200469{
Mark Brown0be98982008-05-19 12:31:28 +0200470 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
Mark Brown06ae9982010-05-07 19:14:45 +0100471
Richard Purdie40e0aa62006-10-06 18:36:07 +0200472 return 0;
473}
474
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000475static int wm8731_resume(struct snd_soc_codec *codec)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200476{
Mark Brown0be98982008-05-19 12:31:28 +0200477 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Mark Brown7dea7c02009-10-26 15:20:17 +0000478
Richard Purdie40e0aa62006-10-06 18:36:07 +0200479 return 0;
480}
Mark Brownb3b50b32009-06-13 22:30:18 +0100481#else
482#define wm8731_suspend NULL
483#define wm8731_resume NULL
484#endif
Richard Purdie40e0aa62006-10-06 18:36:07 +0200485
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000486static int wm8731_probe(struct snd_soc_codec *codec)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200487{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000488 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
489 int ret = 0, i;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200490
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000491 codec->bias_level = SND_SOC_BIAS_OFF,
Richard Purdie40e0aa62006-10-06 18:36:07 +0200492
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000493 ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8731->control_type);
Mark Brown17a52fd2009-07-05 17:24:50 +0100494 if (ret < 0) {
495 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000496 return ret;
Mark Brown17a52fd2009-07-05 17:24:50 +0100497 }
498
Mark Brown7dea7c02009-10-26 15:20:17 +0000499 for (i = 0; i < ARRAY_SIZE(wm8731->supplies); i++)
500 wm8731->supplies[i].supply = wm8731_supply_names[i];
501
502 ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8731->supplies),
503 wm8731->supplies);
504 if (ret != 0) {
505 dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000506 return ret;
Mark Brown7dea7c02009-10-26 15:20:17 +0000507 }
508
509 ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
510 wm8731->supplies);
511 if (ret != 0) {
512 dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
513 goto err_regulator_get;
514 }
515
Mark Brown519cf2d2009-02-18 21:06:01 +0000516 ret = wm8731_reset(codec);
517 if (ret < 0) {
Mark Brownfe5422f2009-07-05 15:18:01 +0100518 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
Mark Brown7dea7c02009-10-26 15:20:17 +0000519 goto err_regulator_enable;
Mark Brown519cf2d2009-02-18 21:06:01 +0000520 }
521
Mark Brown59981022009-02-16 20:49:16 +0000522 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
523
524 /* Latch the update bits */
Mark Brown17a52fd2009-07-05 17:24:50 +0100525 snd_soc_update_bits(codec, WM8731_LOUT1V, 0x100, 0);
526 snd_soc_update_bits(codec, WM8731_ROUT1V, 0x100, 0);
527 snd_soc_update_bits(codec, WM8731_LINVOL, 0x100, 0);
528 snd_soc_update_bits(codec, WM8731_RINVOL, 0x100, 0);
Mark Brown59981022009-02-16 20:49:16 +0000529
Mark Brownce3bdaa2009-02-19 14:29:49 +0000530 /* Disable bypass path by default */
Mark Brown17a52fd2009-07-05 17:24:50 +0100531 snd_soc_update_bits(codec, WM8731_APANA, 0x4, 0);
Mark Brownce3bdaa2009-02-19 14:29:49 +0000532
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000533 snd_soc_add_controls(codec, wm8731_snd_controls,
534 ARRAY_SIZE(wm8731_snd_controls));
535 wm8731_add_widgets(codec);
Mark Brown59981022009-02-16 20:49:16 +0000536
Mark Brown06ae9982010-05-07 19:14:45 +0100537 /* Regulators will have been enabled by bias management */
538 regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
539
Mark Browna8035c82009-02-16 19:35:43 +0000540 return 0;
Mark Brownfe5422f2009-07-05 15:18:01 +0100541
Mark Brown7dea7c02009-10-26 15:20:17 +0000542err_regulator_enable:
543 regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
544err_regulator_get:
545 regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000546
Mark Brownfe5422f2009-07-05 15:18:01 +0100547 kfree(wm8731);
548 return ret;
Mark Browna8035c82009-02-16 19:35:43 +0000549}
550
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000551/* power down chip */
552static int wm8731_remove(struct snd_soc_codec *codec)
Mark Brown59981022009-02-16 20:49:16 +0000553{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000554 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
555
556 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
557
558 regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
Mark Brown7dea7c02009-10-26 15:20:17 +0000559 regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000560
561 return 0;
Mark Brown59981022009-02-16 20:49:16 +0000562}
Mark Browna8035c82009-02-16 19:35:43 +0000563
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000564static struct snd_soc_codec_driver soc_codec_dev_wm8731 = {
565 .probe = wm8731_probe,
566 .remove = wm8731_remove,
567 .suspend = wm8731_suspend,
568 .resume = wm8731_resume,
569 .set_bias_level = wm8731_set_bias_level,
570 .reg_cache_size = sizeof(wm8731_reg),
571 .reg_word_size = sizeof(u16),
572 .reg_cache_default = wm8731_reg,
573};
574
Mark Brown59981022009-02-16 20:49:16 +0000575#if defined(CONFIG_SPI_MASTER)
Mark Brown59981022009-02-16 20:49:16 +0000576static int __devinit wm8731_spi_probe(struct spi_device *spi)
577{
Mark Brown59981022009-02-16 20:49:16 +0000578 struct wm8731_priv *wm8731;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000579 int ret;
Mark Brown59981022009-02-16 20:49:16 +0000580
581 wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
582 if (wm8731 == NULL)
583 return -ENOMEM;
584
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000585 wm8731->control_type = SND_SOC_SPI;
586 spi_set_drvdata(spi, wm8731);
Mark Brown59981022009-02-16 20:49:16 +0000587
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000588 ret = snd_soc_register_codec(&spi->dev,
589 &soc_codec_dev_wm8731, &wm8731_dai, 1);
590 if (ret < 0)
591 kfree(wm8731);
592 return ret;
Mark Brown59981022009-02-16 20:49:16 +0000593}
594
595static int __devexit wm8731_spi_remove(struct spi_device *spi)
596{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000597 snd_soc_unregister_codec(&spi->dev);
598 kfree(spi_get_drvdata(spi));
Mark Brown59981022009-02-16 20:49:16 +0000599 return 0;
600}
601
602static struct spi_driver wm8731_spi_driver = {
603 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000604 .name = "wm8731-codec",
Mark Brown59981022009-02-16 20:49:16 +0000605 .bus = &spi_bus_type,
606 .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");