blob: 975befdfd8854d24d01e5ca638c1b1d0dac0c66c [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>
21#include <linux/platform_device.h>
Cliff Caid2a40352008-09-01 18:47:03 +010022#include <linux/spi/spi.h>
Richard Purdie40e0aa62006-10-06 18:36:07 +020023#include <sound/core.h>
24#include <sound/pcm.h>
25#include <sound/pcm_params.h>
26#include <sound/soc.h>
27#include <sound/soc-dapm.h>
28#include <sound/initval.h>
29
30#include "wm8731.h"
31
32#define AUDIO_NAME "wm8731"
Frank Mandarinob36d61d2007-02-02 17:14:56 +010033#define WM8731_VERSION "0.13"
Richard Purdie40e0aa62006-10-06 18:36:07 +020034
Richard Purdie40e0aa62006-10-06 18:36:07 +020035struct snd_soc_codec_device soc_codec_dev_wm8731;
36
Frank Mandarinob36d61d2007-02-02 17:14:56 +010037/* codec private data */
38struct wm8731_priv {
39 unsigned int sysclk;
40};
41
Richard Purdie40e0aa62006-10-06 18:36:07 +020042/*
43 * wm8731 register cache
44 * We can't read the WM8731 register space when we are
45 * using 2 wire for device control, so we cache them instead.
46 * There is no point in caching the reset register
47 */
48static const u16 wm8731_reg[WM8731_CACHEREGNUM] = {
49 0x0097, 0x0097, 0x0079, 0x0079,
50 0x000a, 0x0008, 0x009f, 0x000a,
51 0x0000, 0x0000
52};
53
Richard Purdie40e0aa62006-10-06 18:36:07 +020054/*
55 * read wm8731 register cache
56 */
57static inline unsigned int wm8731_read_reg_cache(struct snd_soc_codec *codec,
58 unsigned int reg)
59{
60 u16 *cache = codec->reg_cache;
61 if (reg == WM8731_RESET)
62 return 0;
63 if (reg >= WM8731_CACHEREGNUM)
64 return -1;
65 return cache[reg];
66}
67
68/*
69 * write wm8731 register cache
70 */
71static inline void wm8731_write_reg_cache(struct snd_soc_codec *codec,
72 u16 reg, unsigned int value)
73{
74 u16 *cache = codec->reg_cache;
75 if (reg >= WM8731_CACHEREGNUM)
76 return;
77 cache[reg] = value;
78}
79
80/*
81 * write to the WM8731 register space
82 */
83static int wm8731_write(struct snd_soc_codec *codec, unsigned int reg,
84 unsigned int value)
85{
86 u8 data[2];
87
88 /* data is
89 * D15..D9 WM8731 register offset
90 * D8...D0 register data
91 */
92 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
93 data[1] = value & 0x00ff;
94
Mark Brownd454aee2008-04-23 15:16:46 +020095 wm8731_write_reg_cache(codec, reg, value);
Richard Purdie40e0aa62006-10-06 18:36:07 +020096 if (codec->hw_write(codec->control_data, data, 2) == 2)
97 return 0;
98 else
99 return -EIO;
100}
101
102#define wm8731_reset(c) wm8731_write(c, WM8731_RESET, 0)
103
104static const char *wm8731_input_select[] = {"Line In", "Mic"};
105static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
106
107static const struct soc_enum wm8731_enum[] = {
108 SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select),
109 SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph),
110};
111
112static const struct snd_kcontrol_new wm8731_snd_controls[] = {
113
Liam Girdwoodbd903b62006-11-09 16:35:01 +0100114SOC_DOUBLE_R("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
115 0, 127, 0),
116SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
117 7, 1, 0),
Richard Purdie40e0aa62006-10-06 18:36:07 +0200118
119SOC_DOUBLE_R("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0),
120SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
121
122SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0),
123SOC_SINGLE("Capture Mic Switch", WM8731_APANA, 1, 1, 1),
124
125SOC_SINGLE("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1),
126
127SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
128SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
129
130SOC_ENUM("Playback De-emphasis", wm8731_enum[1]),
131};
132
133/* add non dapm controls */
134static int wm8731_add_controls(struct snd_soc_codec *codec)
135{
136 int err, i;
137
138 for (i = 0; i < ARRAY_SIZE(wm8731_snd_controls); i++) {
Mark Brownd454aee2008-04-23 15:16:46 +0200139 err = snd_ctl_add(codec->card,
140 snd_soc_cnew(&wm8731_snd_controls[i],
141 codec, NULL));
142 if (err < 0)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200143 return err;
144 }
145
146 return 0;
147}
148
149/* Output Mixer */
150static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
151SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
152SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
153SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
154};
155
156/* Input mux */
157static const struct snd_kcontrol_new wm8731_input_mux_controls =
158SOC_DAPM_ENUM("Input Select", wm8731_enum[0]);
159
160static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
161SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
162 &wm8731_output_mixer_controls[0],
163 ARRAY_SIZE(wm8731_output_mixer_controls)),
164SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
165SND_SOC_DAPM_OUTPUT("LOUT"),
166SND_SOC_DAPM_OUTPUT("LHPOUT"),
167SND_SOC_DAPM_OUTPUT("ROUT"),
168SND_SOC_DAPM_OUTPUT("RHPOUT"),
169SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
170SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
171SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
172SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
173SND_SOC_DAPM_INPUT("MICIN"),
174SND_SOC_DAPM_INPUT("RLINEIN"),
175SND_SOC_DAPM_INPUT("LLINEIN"),
176};
177
Mark Browna65f0562008-05-13 14:54:43 +0200178static const struct snd_soc_dapm_route intercon[] = {
Richard Purdie40e0aa62006-10-06 18:36:07 +0200179 /* output mixer */
180 {"Output Mixer", "Line Bypass Switch", "Line Input"},
181 {"Output Mixer", "HiFi Playback Switch", "DAC"},
182 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
183
184 /* outputs */
185 {"RHPOUT", NULL, "Output Mixer"},
186 {"ROUT", NULL, "Output Mixer"},
187 {"LHPOUT", NULL, "Output Mixer"},
188 {"LOUT", NULL, "Output Mixer"},
189
190 /* input mux */
191 {"Input Mux", "Line In", "Line Input"},
192 {"Input Mux", "Mic", "Mic Bias"},
193 {"ADC", NULL, "Input Mux"},
194
195 /* inputs */
196 {"Line Input", NULL, "LLINEIN"},
197 {"Line Input", NULL, "RLINEIN"},
198 {"Mic Bias", NULL, "MICIN"},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200199};
200
201static int wm8731_add_widgets(struct snd_soc_codec *codec)
202{
Mark Browna65f0562008-05-13 14:54:43 +0200203 snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
204 ARRAY_SIZE(wm8731_dapm_widgets));
Richard Purdie40e0aa62006-10-06 18:36:07 +0200205
Mark Browna65f0562008-05-13 14:54:43 +0200206 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
Richard Purdie40e0aa62006-10-06 18:36:07 +0200207
208 snd_soc_dapm_new_widgets(codec);
209 return 0;
210}
211
212struct _coeff_div {
213 u32 mclk;
214 u32 rate;
215 u16 fs;
216 u8 sr:4;
217 u8 bosr:1;
218 u8 usb:1;
219};
220
221/* codec mclk clock divider coefficients */
222static const struct _coeff_div coeff_div[] = {
223 /* 48k */
224 {12288000, 48000, 256, 0x0, 0x0, 0x0},
225 {18432000, 48000, 384, 0x0, 0x1, 0x0},
226 {12000000, 48000, 250, 0x0, 0x0, 0x1},
227
228 /* 32k */
229 {12288000, 32000, 384, 0x6, 0x0, 0x0},
230 {18432000, 32000, 576, 0x6, 0x1, 0x0},
Frank Mandarino298a2c72007-01-31 10:02:56 +0100231 {12000000, 32000, 375, 0x6, 0x0, 0x1},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200232
233 /* 8k */
234 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
235 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
236 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
237 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
238 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
239
240 /* 96k */
241 {12288000, 96000, 128, 0x7, 0x0, 0x0},
242 {18432000, 96000, 192, 0x7, 0x1, 0x0},
243 {12000000, 96000, 125, 0x7, 0x0, 0x1},
244
245 /* 44.1k */
246 {11289600, 44100, 256, 0x8, 0x0, 0x0},
247 {16934400, 44100, 384, 0x8, 0x1, 0x0},
248 {12000000, 44100, 272, 0x8, 0x1, 0x1},
249
250 /* 88.2k */
251 {11289600, 88200, 128, 0xf, 0x0, 0x0},
252 {16934400, 88200, 192, 0xf, 0x1, 0x0},
253 {12000000, 88200, 136, 0xf, 0x1, 0x1},
254};
255
256static inline int get_coeff(int mclk, int rate)
257{
258 int i;
259
260 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
261 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
262 return i;
263 }
264 return 0;
265}
266
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100267static int wm8731_hw_params(struct snd_pcm_substream *substream,
268 struct snd_pcm_hw_params *params)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200269{
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100270 struct snd_soc_pcm_runtime *rtd = substream->private_data;
271 struct snd_soc_device *socdev = rtd->socdev;
272 struct snd_soc_codec *codec = socdev->codec;
273 struct wm8731_priv *wm8731 = codec->private_data;
274 u16 iface = wm8731_read_reg_cache(codec, WM8731_IFACE) & 0xfff3;
275 int i = get_coeff(wm8731->sysclk, params_rate(params));
276 u16 srate = (coeff_div[i].sr << 2) |
277 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200278
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100279 wm8731_write(codec, WM8731_SRATE, srate);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200280
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100281 /* bit size */
282 switch (params_format(params)) {
283 case SNDRV_PCM_FORMAT_S16_LE:
284 break;
285 case SNDRV_PCM_FORMAT_S20_3LE:
286 iface |= 0x0004;
287 break;
288 case SNDRV_PCM_FORMAT_S24_LE:
289 iface |= 0x0008;
290 break;
291 }
292
293 wm8731_write(codec, WM8731_IFACE, iface);
294 return 0;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200295}
296
297static int wm8731_pcm_prepare(struct snd_pcm_substream *substream)
298{
299 struct snd_soc_pcm_runtime *rtd = substream->private_data;
300 struct snd_soc_device *socdev = rtd->socdev;
301 struct snd_soc_codec *codec = socdev->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200302
303 /* set active */
304 wm8731_write(codec, WM8731_ACTIVE, 0x0001);
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100305
Richard Purdie40e0aa62006-10-06 18:36:07 +0200306 return 0;
307}
308
309static void wm8731_shutdown(struct snd_pcm_substream *substream)
310{
311 struct snd_soc_pcm_runtime *rtd = substream->private_data;
312 struct snd_soc_device *socdev = rtd->socdev;
313 struct snd_soc_codec *codec = socdev->codec;
314
315 /* deactivate */
316 if (!codec->active) {
317 udelay(50);
318 wm8731_write(codec, WM8731_ACTIVE, 0x0);
319 }
320}
321
Liam Girdwoode550e172008-07-07 16:07:52 +0100322static int wm8731_mute(struct snd_soc_dai *dai, int mute)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200323{
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100324 struct snd_soc_codec *codec = dai->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200325 u16 mute_reg = wm8731_read_reg_cache(codec, WM8731_APDIGI) & 0xfff7;
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100326
Richard Purdie40e0aa62006-10-06 18:36:07 +0200327 if (mute)
328 wm8731_write(codec, WM8731_APDIGI, mute_reg | 0x8);
329 else
330 wm8731_write(codec, WM8731_APDIGI, mute_reg);
331 return 0;
332}
333
Liam Girdwoode550e172008-07-07 16:07:52 +0100334static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100335 int clk_id, unsigned int freq, int dir)
336{
337 struct snd_soc_codec *codec = codec_dai->codec;
338 struct wm8731_priv *wm8731 = codec->private_data;
339
340 switch (freq) {
341 case 11289600:
342 case 12000000:
343 case 12288000:
344 case 16934400:
345 case 18432000:
346 wm8731->sysclk = freq;
347 return 0;
348 }
349 return -EINVAL;
350}
351
352
Liam Girdwoode550e172008-07-07 16:07:52 +0100353static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100354 unsigned int fmt)
355{
356 struct snd_soc_codec *codec = codec_dai->codec;
357 u16 iface = 0;
358
359 /* set master/slave audio interface */
360 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
361 case SND_SOC_DAIFMT_CBM_CFM:
362 iface |= 0x0040;
363 break;
364 case SND_SOC_DAIFMT_CBS_CFS:
365 break;
366 default:
367 return -EINVAL;
368 }
369
370 /* interface format */
371 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
372 case SND_SOC_DAIFMT_I2S:
373 iface |= 0x0002;
374 break;
375 case SND_SOC_DAIFMT_RIGHT_J:
376 break;
377 case SND_SOC_DAIFMT_LEFT_J:
378 iface |= 0x0001;
379 break;
380 case SND_SOC_DAIFMT_DSP_A:
381 iface |= 0x0003;
382 break;
383 case SND_SOC_DAIFMT_DSP_B:
384 iface |= 0x0013;
385 break;
386 default:
387 return -EINVAL;
388 }
389
390 /* clock inversion */
391 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
392 case SND_SOC_DAIFMT_NB_NF:
393 break;
394 case SND_SOC_DAIFMT_IB_IF:
395 iface |= 0x0090;
396 break;
397 case SND_SOC_DAIFMT_IB_NF:
398 iface |= 0x0080;
399 break;
400 case SND_SOC_DAIFMT_NB_IF:
401 iface |= 0x0010;
402 break;
403 default:
404 return -EINVAL;
405 }
406
407 /* set iface */
408 wm8731_write(codec, WM8731_IFACE, iface);
409 return 0;
410}
411
Mark Brown0be98982008-05-19 12:31:28 +0200412static int wm8731_set_bias_level(struct snd_soc_codec *codec,
413 enum snd_soc_bias_level level)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200414{
415 u16 reg = wm8731_read_reg_cache(codec, WM8731_PWR) & 0xff7f;
416
Mark Brown0be98982008-05-19 12:31:28 +0200417 switch (level) {
418 case SND_SOC_BIAS_ON:
Richard Purdie40e0aa62006-10-06 18:36:07 +0200419 /* vref/mid, osc on, dac unmute */
420 wm8731_write(codec, WM8731_PWR, reg);
421 break;
Mark Brown0be98982008-05-19 12:31:28 +0200422 case SND_SOC_BIAS_PREPARE:
Richard Purdie40e0aa62006-10-06 18:36:07 +0200423 break;
Mark Brown0be98982008-05-19 12:31:28 +0200424 case SND_SOC_BIAS_STANDBY:
Richard Purdie40e0aa62006-10-06 18:36:07 +0200425 /* everything off except vref/vmid, */
426 wm8731_write(codec, WM8731_PWR, reg | 0x0040);
427 break;
Mark Brown0be98982008-05-19 12:31:28 +0200428 case SND_SOC_BIAS_OFF:
Richard Purdie40e0aa62006-10-06 18:36:07 +0200429 /* everything off, dac mute, inactive */
430 wm8731_write(codec, WM8731_ACTIVE, 0x0);
431 wm8731_write(codec, WM8731_PWR, 0xffff);
432 break;
433 }
Mark Brown0be98982008-05-19 12:31:28 +0200434 codec->bias_level = level;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200435 return 0;
436}
437
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100438#define WM8731_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
439 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
440 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
441 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
442 SNDRV_PCM_RATE_96000)
443
444#define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
445 SNDRV_PCM_FMTBIT_S24_LE)
446
Liam Girdwoode550e172008-07-07 16:07:52 +0100447struct snd_soc_dai wm8731_dai = {
Richard Purdie40e0aa62006-10-06 18:36:07 +0200448 .name = "WM8731",
449 .playback = {
450 .stream_name = "Playback",
451 .channels_min = 1,
452 .channels_max = 2,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100453 .rates = WM8731_RATES,
454 .formats = WM8731_FORMATS,},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200455 .capture = {
456 .stream_name = "Capture",
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 .ops = {
462 .prepare = wm8731_pcm_prepare,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100463 .hw_params = wm8731_hw_params,
Richard Purdie40e0aa62006-10-06 18:36:07 +0200464 .shutdown = wm8731_shutdown,
465 },
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100466 .dai_ops = {
467 .digital_mute = wm8731_mute,
468 .set_sysclk = wm8731_set_dai_sysclk,
469 .set_fmt = wm8731_set_dai_fmt,
470 }
Richard Purdie40e0aa62006-10-06 18:36:07 +0200471};
472EXPORT_SYMBOL_GPL(wm8731_dai);
473
474static int wm8731_suspend(struct platform_device *pdev, pm_message_t state)
475{
476 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
477 struct snd_soc_codec *codec = socdev->codec;
478
479 wm8731_write(codec, WM8731_ACTIVE, 0x0);
Mark Brown0be98982008-05-19 12:31:28 +0200480 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200481 return 0;
482}
483
484static int wm8731_resume(struct platform_device *pdev)
485{
486 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
487 struct snd_soc_codec *codec = socdev->codec;
488 int i;
489 u8 data[2];
490 u16 *cache = codec->reg_cache;
491
492 /* Sync reg_cache with the hardware */
493 for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
494 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
495 data[1] = cache[i] & 0x00ff;
496 codec->hw_write(codec->control_data, data, 2);
497 }
Mark Brown0be98982008-05-19 12:31:28 +0200498 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
499 wm8731_set_bias_level(codec, codec->suspend_bias_level);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200500 return 0;
501}
502
503/*
504 * initialise the WM8731 driver
505 * register the mixer and dsp interfaces with the kernel
506 */
507static int wm8731_init(struct snd_soc_device *socdev)
508{
509 struct snd_soc_codec *codec = socdev->codec;
510 int reg, ret = 0;
511
512 codec->name = "WM8731";
513 codec->owner = THIS_MODULE;
514 codec->read = wm8731_read_reg_cache;
515 codec->write = wm8731_write;
Mark Brown0be98982008-05-19 12:31:28 +0200516 codec->set_bias_level = wm8731_set_bias_level;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200517 codec->dai = &wm8731_dai;
518 codec->num_dai = 1;
Mark Brownd751b232008-06-11 13:47:06 +0100519 codec->reg_cache_size = ARRAY_SIZE(wm8731_reg);
Takashi Iwai88cb4292007-02-05 14:56:20 +0100520 codec->reg_cache = kmemdup(wm8731_reg, sizeof(wm8731_reg), GFP_KERNEL);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200521 if (codec->reg_cache == NULL)
522 return -ENOMEM;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200523
524 wm8731_reset(codec);
525
526 /* register pcms */
527 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
528 if (ret < 0) {
Liam Girdwoode35115a2007-01-31 10:02:23 +0100529 printk(KERN_ERR "wm8731: failed to create pcms\n");
530 goto pcm_err;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200531 }
532
533 /* power on device */
Mark Brown0be98982008-05-19 12:31:28 +0200534 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200535
536 /* set the update bits */
537 reg = wm8731_read_reg_cache(codec, WM8731_LOUT1V);
Ville Syrjala389619f2007-11-26 08:58:24 +0100538 wm8731_write(codec, WM8731_LOUT1V, reg & ~0x0100);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200539 reg = wm8731_read_reg_cache(codec, WM8731_ROUT1V);
Ville Syrjala389619f2007-11-26 08:58:24 +0100540 wm8731_write(codec, WM8731_ROUT1V, reg & ~0x0100);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200541 reg = wm8731_read_reg_cache(codec, WM8731_LINVOL);
Ville Syrjala389619f2007-11-26 08:58:24 +0100542 wm8731_write(codec, WM8731_LINVOL, reg & ~0x0100);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200543 reg = wm8731_read_reg_cache(codec, WM8731_RINVOL);
Ville Syrjala389619f2007-11-26 08:58:24 +0100544 wm8731_write(codec, WM8731_RINVOL, reg & ~0x0100);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200545
546 wm8731_add_controls(codec);
547 wm8731_add_widgets(codec);
548 ret = snd_soc_register_card(socdev);
549 if (ret < 0) {
Liam Girdwoode35115a2007-01-31 10:02:23 +0100550 printk(KERN_ERR "wm8731: failed to register card\n");
551 goto card_err;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200552 }
553
554 return ret;
Liam Girdwoode35115a2007-01-31 10:02:23 +0100555
556card_err:
557 snd_soc_free_pcms(socdev);
558 snd_soc_dapm_free(socdev);
559pcm_err:
560 kfree(codec->reg_cache);
561 return ret;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200562}
563
564static struct snd_soc_device *wm8731_socdev;
565
Mark Brownd454aee2008-04-23 15:16:46 +0200566#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200567
568/*
569 * WM8731 2 wire address is determined by GPIO5
570 * state during powerup.
571 * low = 0x1a
572 * high = 0x1b
573 */
Richard Purdie40e0aa62006-10-06 18:36:07 +0200574
Jean Delvare81297c82008-09-01 18:47:01 +0100575static int wm8731_i2c_probe(struct i2c_client *i2c,
576 const struct i2c_device_id *id)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200577{
578 struct snd_soc_device *socdev = wm8731_socdev;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200579 struct snd_soc_codec *codec = socdev->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200580 int ret;
581
Richard Purdie40e0aa62006-10-06 18:36:07 +0200582 i2c_set_clientdata(i2c, codec);
583 codec->control_data = i2c;
584
Richard Purdie40e0aa62006-10-06 18:36:07 +0200585 ret = wm8731_init(socdev);
Jean Delvare81297c82008-09-01 18:47:01 +0100586 if (ret < 0)
Mark Browna5c95e92008-06-23 14:51:29 +0100587 pr_err("failed to initialise WM8731\n");
Richard Purdie40e0aa62006-10-06 18:36:07 +0200588
Richard Purdie40e0aa62006-10-06 18:36:07 +0200589 return ret;
590}
591
Jean Delvare81297c82008-09-01 18:47:01 +0100592static int wm8731_i2c_remove(struct i2c_client *client)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200593{
Mark Brownd454aee2008-04-23 15:16:46 +0200594 struct snd_soc_codec *codec = i2c_get_clientdata(client);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200595 kfree(codec->reg_cache);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200596 return 0;
597}
598
Jean Delvare81297c82008-09-01 18:47:01 +0100599static const struct i2c_device_id wm8731_i2c_id[] = {
600 { "wm8731", 0 },
601 { }
602};
603MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200604
Richard Purdie40e0aa62006-10-06 18:36:07 +0200605static struct i2c_driver wm8731_i2c_driver = {
606 .driver = {
607 .name = "WM8731 I2C Codec",
608 .owner = THIS_MODULE,
609 },
Jean Delvare81297c82008-09-01 18:47:01 +0100610 .probe = wm8731_i2c_probe,
611 .remove = wm8731_i2c_remove,
612 .id_table = wm8731_i2c_id,
Richard Purdie40e0aa62006-10-06 18:36:07 +0200613};
614
Jean Delvare81297c82008-09-01 18:47:01 +0100615static int wm8731_add_i2c_device(struct platform_device *pdev,
616 const struct wm8731_setup_data *setup)
617{
618 struct i2c_board_info info;
619 struct i2c_adapter *adapter;
620 struct i2c_client *client;
621 int ret;
622
623 ret = i2c_add_driver(&wm8731_i2c_driver);
624 if (ret != 0) {
625 dev_err(&pdev->dev, "can't add i2c driver\n");
626 return ret;
627 }
628
629 memset(&info, 0, sizeof(struct i2c_board_info));
630 info.addr = setup->i2c_address;
631 strlcpy(info.type, "wm8731", I2C_NAME_SIZE);
632
633 adapter = i2c_get_adapter(setup->i2c_bus);
634 if (!adapter) {
635 dev_err(&pdev->dev, "can't get i2c adapter %d\n",
636 setup->i2c_bus);
637 goto err_driver;
638 }
639
640 client = i2c_new_device(adapter, &info);
641 i2c_put_adapter(adapter);
642 if (!client) {
643 dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
644 (unsigned int)info.addr);
645 goto err_driver;
646 }
647
648 return 0;
649
650err_driver:
651 i2c_del_driver(&wm8731_i2c_driver);
652 return -ENODEV;
653}
Richard Purdie40e0aa62006-10-06 18:36:07 +0200654#endif
655
Cliff Caid2a40352008-09-01 18:47:03 +0100656#if defined(CONFIG_SPI_MASTER)
657static int __devinit wm8731_spi_probe(struct spi_device *spi)
658{
659 struct snd_soc_device *socdev = wm8731_socdev;
660 struct snd_soc_codec *codec = socdev->codec;
661 int ret;
662
663 codec->control_data = spi;
664
665 ret = wm8731_init(socdev);
666 if (ret < 0)
667 dev_err(&spi->dev, "failed to initialise WM8731\n");
668
669 return ret;
670}
671
672static int __devexit wm8731_spi_remove(struct spi_device *spi)
673{
674 return 0;
675}
676
677static struct spi_driver wm8731_spi_driver = {
678 .driver = {
679 .name = "wm8731",
680 .bus = &spi_bus_type,
681 .owner = THIS_MODULE,
682 },
683 .probe = wm8731_spi_probe,
684 .remove = __devexit_p(wm8731_spi_remove),
685};
686
687static int wm8731_spi_write(struct spi_device *spi, const char *data, int len)
688{
689 struct spi_transfer t;
690 struct spi_message m;
691 u16 msg[2];
692
693 if (len <= 0)
694 return 0;
695
696 msg[0] = (data[0] << 8) + data[1];
697
698 spi_message_init(&m);
699 memset(&t, 0, (sizeof t));
700
701 t.tx_buf = &msg[0];
702 t.len = len;
703
704 spi_message_add_tail(&t, &m);
705 spi_sync(spi, &m);
706
707 return len;
708}
709#endif /* CONFIG_SPI_MASTER */
710
Richard Purdie40e0aa62006-10-06 18:36:07 +0200711static int wm8731_probe(struct platform_device *pdev)
712{
713 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
714 struct wm8731_setup_data *setup;
715 struct snd_soc_codec *codec;
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100716 struct wm8731_priv *wm8731;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200717 int ret = 0;
718
Mark Browna5c95e92008-06-23 14:51:29 +0100719 pr_info("WM8731 Audio Codec %s", WM8731_VERSION);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200720
721 setup = socdev->codec_data;
722 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
723 if (codec == NULL)
724 return -ENOMEM;
725
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100726 wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
727 if (wm8731 == NULL) {
728 kfree(codec);
729 return -ENOMEM;
730 }
731
732 codec->private_data = wm8731;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200733 socdev->codec = codec;
734 mutex_init(&codec->mutex);
735 INIT_LIST_HEAD(&codec->dapm_widgets);
736 INIT_LIST_HEAD(&codec->dapm_paths);
737
738 wm8731_socdev = socdev;
Cliff Caid2a40352008-09-01 18:47:03 +0100739 ret = -ENODEV;
740
Mark Brownd454aee2008-04-23 15:16:46 +0200741#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200742 if (setup->i2c_address) {
Richard Purdie40e0aa62006-10-06 18:36:07 +0200743 codec->hw_write = (hw_write_t)i2c_master_send;
Jean Delvare81297c82008-09-01 18:47:01 +0100744 ret = wm8731_add_i2c_device(pdev, setup);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200745 }
Cliff Caid2a40352008-09-01 18:47:03 +0100746#endif
747#if defined(CONFIG_SPI_MASTER)
748 if (setup->spi) {
749 codec->hw_write = (hw_write_t)wm8731_spi_write;
750 ret = spi_register_driver(&wm8731_spi_driver);
751 if (ret != 0)
752 printk(KERN_ERR "can't add spi driver");
753 }
Richard Purdie40e0aa62006-10-06 18:36:07 +0200754#endif
Jean Delvare3051e412008-08-25 11:49:20 +0100755
756 if (ret != 0) {
757 kfree(codec->private_data);
758 kfree(codec);
759 }
Richard Purdie40e0aa62006-10-06 18:36:07 +0200760 return ret;
761}
762
763/* power down chip */
764static int wm8731_remove(struct platform_device *pdev)
765{
766 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
767 struct snd_soc_codec *codec = socdev->codec;
768
769 if (codec->control_data)
Mark Brown0be98982008-05-19 12:31:28 +0200770 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200771
772 snd_soc_free_pcms(socdev);
773 snd_soc_dapm_free(socdev);
Mark Brownd454aee2008-04-23 15:16:46 +0200774#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Jean Delvare81297c82008-09-01 18:47:01 +0100775 i2c_unregister_device(codec->control_data);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200776 i2c_del_driver(&wm8731_i2c_driver);
777#endif
Cliff Caid2a40352008-09-01 18:47:03 +0100778#if defined(CONFIG_SPI_MASTER)
779 spi_unregister_driver(&wm8731_spi_driver);
780#endif
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100781 kfree(codec->private_data);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200782 kfree(codec);
783
784 return 0;
785}
786
787struct snd_soc_codec_device soc_codec_dev_wm8731 = {
788 .probe = wm8731_probe,
789 .remove = wm8731_remove,
790 .suspend = wm8731_suspend,
791 .resume = wm8731_resume,
792};
Richard Purdie40e0aa62006-10-06 18:36:07 +0200793EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731);
794
795MODULE_DESCRIPTION("ASoC WM8731 driver");
796MODULE_AUTHOR("Richard Purdie");
797MODULE_LICENSE("GPL");