blob: 0f28aa4bcccba8db5da62e04be3a5906181f94df [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>
Richard Purdie40e0aa62006-10-06 18:36:07 +020022#include <sound/core.h>
23#include <sound/pcm.h>
24#include <sound/pcm_params.h>
25#include <sound/soc.h>
26#include <sound/soc-dapm.h>
27#include <sound/initval.h>
28
29#include "wm8731.h"
30
31#define AUDIO_NAME "wm8731"
Frank Mandarinob36d61d2007-02-02 17:14:56 +010032#define WM8731_VERSION "0.13"
Richard Purdie40e0aa62006-10-06 18:36:07 +020033
34/*
35 * Debug
36 */
37
38#define WM8731_DEBUG 0
39
40#ifdef WM8731_DEBUG
41#define dbg(format, arg...) \
42 printk(KERN_DEBUG AUDIO_NAME ": " format "\n" , ## arg)
43#else
44#define dbg(format, arg...) do {} while (0)
45#endif
46#define err(format, arg...) \
47 printk(KERN_ERR AUDIO_NAME ": " format "\n" , ## arg)
48#define info(format, arg...) \
49 printk(KERN_INFO AUDIO_NAME ": " format "\n" , ## arg)
50#define warn(format, arg...) \
51 printk(KERN_WARNING AUDIO_NAME ": " format "\n" , ## arg)
52
53struct snd_soc_codec_device soc_codec_dev_wm8731;
54
Frank Mandarinob36d61d2007-02-02 17:14:56 +010055/* codec private data */
56struct wm8731_priv {
57 unsigned int sysclk;
58};
59
Richard Purdie40e0aa62006-10-06 18:36:07 +020060/*
61 * wm8731 register cache
62 * We can't read the WM8731 register space when we are
63 * using 2 wire for device control, so we cache them instead.
64 * There is no point in caching the reset register
65 */
66static const u16 wm8731_reg[WM8731_CACHEREGNUM] = {
67 0x0097, 0x0097, 0x0079, 0x0079,
68 0x000a, 0x0008, 0x009f, 0x000a,
69 0x0000, 0x0000
70};
71
Richard Purdie40e0aa62006-10-06 18:36:07 +020072/*
73 * read wm8731 register cache
74 */
75static inline unsigned int wm8731_read_reg_cache(struct snd_soc_codec *codec,
76 unsigned int reg)
77{
78 u16 *cache = codec->reg_cache;
79 if (reg == WM8731_RESET)
80 return 0;
81 if (reg >= WM8731_CACHEREGNUM)
82 return -1;
83 return cache[reg];
84}
85
86/*
87 * write wm8731 register cache
88 */
89static inline void wm8731_write_reg_cache(struct snd_soc_codec *codec,
90 u16 reg, unsigned int value)
91{
92 u16 *cache = codec->reg_cache;
93 if (reg >= WM8731_CACHEREGNUM)
94 return;
95 cache[reg] = value;
96}
97
98/*
99 * write to the WM8731 register space
100 */
101static int wm8731_write(struct snd_soc_codec *codec, unsigned int reg,
102 unsigned int value)
103{
104 u8 data[2];
105
106 /* data is
107 * D15..D9 WM8731 register offset
108 * D8...D0 register data
109 */
110 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
111 data[1] = value & 0x00ff;
112
Mark Brownd454aee2008-04-23 15:16:46 +0200113 wm8731_write_reg_cache(codec, reg, value);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200114 if (codec->hw_write(codec->control_data, data, 2) == 2)
115 return 0;
116 else
117 return -EIO;
118}
119
120#define wm8731_reset(c) wm8731_write(c, WM8731_RESET, 0)
121
122static const char *wm8731_input_select[] = {"Line In", "Mic"};
123static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
124
125static const struct soc_enum wm8731_enum[] = {
126 SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select),
127 SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph),
128};
129
130static const struct snd_kcontrol_new wm8731_snd_controls[] = {
131
Liam Girdwoodbd903b62006-11-09 16:35:01 +0100132SOC_DOUBLE_R("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
133 0, 127, 0),
134SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
135 7, 1, 0),
Richard Purdie40e0aa62006-10-06 18:36:07 +0200136
137SOC_DOUBLE_R("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0),
138SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
139
140SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0),
141SOC_SINGLE("Capture Mic Switch", WM8731_APANA, 1, 1, 1),
142
143SOC_SINGLE("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1),
144
145SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
146SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
147
148SOC_ENUM("Playback De-emphasis", wm8731_enum[1]),
149};
150
151/* add non dapm controls */
152static int wm8731_add_controls(struct snd_soc_codec *codec)
153{
154 int err, i;
155
156 for (i = 0; i < ARRAY_SIZE(wm8731_snd_controls); i++) {
Mark Brownd454aee2008-04-23 15:16:46 +0200157 err = snd_ctl_add(codec->card,
158 snd_soc_cnew(&wm8731_snd_controls[i],
159 codec, NULL));
160 if (err < 0)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200161 return err;
162 }
163
164 return 0;
165}
166
167/* Output Mixer */
168static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
169SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
170SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
171SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
172};
173
174/* Input mux */
175static const struct snd_kcontrol_new wm8731_input_mux_controls =
176SOC_DAPM_ENUM("Input Select", wm8731_enum[0]);
177
178static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
179SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
180 &wm8731_output_mixer_controls[0],
181 ARRAY_SIZE(wm8731_output_mixer_controls)),
182SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
183SND_SOC_DAPM_OUTPUT("LOUT"),
184SND_SOC_DAPM_OUTPUT("LHPOUT"),
185SND_SOC_DAPM_OUTPUT("ROUT"),
186SND_SOC_DAPM_OUTPUT("RHPOUT"),
187SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
188SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
189SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
190SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
191SND_SOC_DAPM_INPUT("MICIN"),
192SND_SOC_DAPM_INPUT("RLINEIN"),
193SND_SOC_DAPM_INPUT("LLINEIN"),
194};
195
196static const char *intercon[][3] = {
197 /* output mixer */
198 {"Output Mixer", "Line Bypass Switch", "Line Input"},
199 {"Output Mixer", "HiFi Playback Switch", "DAC"},
200 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
201
202 /* outputs */
203 {"RHPOUT", NULL, "Output Mixer"},
204 {"ROUT", NULL, "Output Mixer"},
205 {"LHPOUT", NULL, "Output Mixer"},
206 {"LOUT", NULL, "Output Mixer"},
207
208 /* input mux */
209 {"Input Mux", "Line In", "Line Input"},
210 {"Input Mux", "Mic", "Mic Bias"},
211 {"ADC", NULL, "Input Mux"},
212
213 /* inputs */
214 {"Line Input", NULL, "LLINEIN"},
215 {"Line Input", NULL, "RLINEIN"},
216 {"Mic Bias", NULL, "MICIN"},
217
218 /* terminator */
219 {NULL, NULL, NULL},
220};
221
222static int wm8731_add_widgets(struct snd_soc_codec *codec)
223{
224 int i;
225
Mark Brownd454aee2008-04-23 15:16:46 +0200226 for (i = 0; i < ARRAY_SIZE(wm8731_dapm_widgets); i++)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200227 snd_soc_dapm_new_control(codec, &wm8731_dapm_widgets[i]);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200228
229 /* set up audio path interconnects */
Mark Brownd454aee2008-04-23 15:16:46 +0200230 for (i = 0; intercon[i][0] != NULL; i++)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200231 snd_soc_dapm_connect_input(codec, intercon[i][0],
232 intercon[i][1], intercon[i][2]);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200233
234 snd_soc_dapm_new_widgets(codec);
235 return 0;
236}
237
238struct _coeff_div {
239 u32 mclk;
240 u32 rate;
241 u16 fs;
242 u8 sr:4;
243 u8 bosr:1;
244 u8 usb:1;
245};
246
247/* codec mclk clock divider coefficients */
248static const struct _coeff_div coeff_div[] = {
249 /* 48k */
250 {12288000, 48000, 256, 0x0, 0x0, 0x0},
251 {18432000, 48000, 384, 0x0, 0x1, 0x0},
252 {12000000, 48000, 250, 0x0, 0x0, 0x1},
253
254 /* 32k */
255 {12288000, 32000, 384, 0x6, 0x0, 0x0},
256 {18432000, 32000, 576, 0x6, 0x1, 0x0},
Frank Mandarino298a2c72007-01-31 10:02:56 +0100257 {12000000, 32000, 375, 0x6, 0x0, 0x1},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200258
259 /* 8k */
260 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
261 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
262 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
263 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
264 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
265
266 /* 96k */
267 {12288000, 96000, 128, 0x7, 0x0, 0x0},
268 {18432000, 96000, 192, 0x7, 0x1, 0x0},
269 {12000000, 96000, 125, 0x7, 0x0, 0x1},
270
271 /* 44.1k */
272 {11289600, 44100, 256, 0x8, 0x0, 0x0},
273 {16934400, 44100, 384, 0x8, 0x1, 0x0},
274 {12000000, 44100, 272, 0x8, 0x1, 0x1},
275
276 /* 88.2k */
277 {11289600, 88200, 128, 0xf, 0x0, 0x0},
278 {16934400, 88200, 192, 0xf, 0x1, 0x0},
279 {12000000, 88200, 136, 0xf, 0x1, 0x1},
280};
281
282static inline int get_coeff(int mclk, int rate)
283{
284 int i;
285
286 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
287 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
288 return i;
289 }
290 return 0;
291}
292
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100293static int wm8731_hw_params(struct snd_pcm_substream *substream,
294 struct snd_pcm_hw_params *params)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200295{
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100296 struct snd_soc_pcm_runtime *rtd = substream->private_data;
297 struct snd_soc_device *socdev = rtd->socdev;
298 struct snd_soc_codec *codec = socdev->codec;
299 struct wm8731_priv *wm8731 = codec->private_data;
300 u16 iface = wm8731_read_reg_cache(codec, WM8731_IFACE) & 0xfff3;
301 int i = get_coeff(wm8731->sysclk, params_rate(params));
302 u16 srate = (coeff_div[i].sr << 2) |
303 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200304
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100305 wm8731_write(codec, WM8731_SRATE, srate);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200306
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100307 /* bit size */
308 switch (params_format(params)) {
309 case SNDRV_PCM_FORMAT_S16_LE:
310 break;
311 case SNDRV_PCM_FORMAT_S20_3LE:
312 iface |= 0x0004;
313 break;
314 case SNDRV_PCM_FORMAT_S24_LE:
315 iface |= 0x0008;
316 break;
317 }
318
319 wm8731_write(codec, WM8731_IFACE, iface);
320 return 0;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200321}
322
323static int wm8731_pcm_prepare(struct snd_pcm_substream *substream)
324{
325 struct snd_soc_pcm_runtime *rtd = substream->private_data;
326 struct snd_soc_device *socdev = rtd->socdev;
327 struct snd_soc_codec *codec = socdev->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200328
329 /* set active */
330 wm8731_write(codec, WM8731_ACTIVE, 0x0001);
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100331
Richard Purdie40e0aa62006-10-06 18:36:07 +0200332 return 0;
333}
334
335static void wm8731_shutdown(struct snd_pcm_substream *substream)
336{
337 struct snd_soc_pcm_runtime *rtd = substream->private_data;
338 struct snd_soc_device *socdev = rtd->socdev;
339 struct snd_soc_codec *codec = socdev->codec;
340
341 /* deactivate */
342 if (!codec->active) {
343 udelay(50);
344 wm8731_write(codec, WM8731_ACTIVE, 0x0);
345 }
346}
347
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100348static int wm8731_mute(struct snd_soc_codec_dai *dai, int mute)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200349{
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100350 struct snd_soc_codec *codec = dai->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200351 u16 mute_reg = wm8731_read_reg_cache(codec, WM8731_APDIGI) & 0xfff7;
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100352
Richard Purdie40e0aa62006-10-06 18:36:07 +0200353 if (mute)
354 wm8731_write(codec, WM8731_APDIGI, mute_reg | 0x8);
355 else
356 wm8731_write(codec, WM8731_APDIGI, mute_reg);
357 return 0;
358}
359
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100360static int wm8731_set_dai_sysclk(struct snd_soc_codec_dai *codec_dai,
361 int clk_id, unsigned int freq, int dir)
362{
363 struct snd_soc_codec *codec = codec_dai->codec;
364 struct wm8731_priv *wm8731 = codec->private_data;
365
366 switch (freq) {
367 case 11289600:
368 case 12000000:
369 case 12288000:
370 case 16934400:
371 case 18432000:
372 wm8731->sysclk = freq;
373 return 0;
374 }
375 return -EINVAL;
376}
377
378
379static int wm8731_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
380 unsigned int fmt)
381{
382 struct snd_soc_codec *codec = codec_dai->codec;
383 u16 iface = 0;
384
385 /* set master/slave audio interface */
386 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
387 case SND_SOC_DAIFMT_CBM_CFM:
388 iface |= 0x0040;
389 break;
390 case SND_SOC_DAIFMT_CBS_CFS:
391 break;
392 default:
393 return -EINVAL;
394 }
395
396 /* interface format */
397 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
398 case SND_SOC_DAIFMT_I2S:
399 iface |= 0x0002;
400 break;
401 case SND_SOC_DAIFMT_RIGHT_J:
402 break;
403 case SND_SOC_DAIFMT_LEFT_J:
404 iface |= 0x0001;
405 break;
406 case SND_SOC_DAIFMT_DSP_A:
407 iface |= 0x0003;
408 break;
409 case SND_SOC_DAIFMT_DSP_B:
410 iface |= 0x0013;
411 break;
412 default:
413 return -EINVAL;
414 }
415
416 /* clock inversion */
417 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
418 case SND_SOC_DAIFMT_NB_NF:
419 break;
420 case SND_SOC_DAIFMT_IB_IF:
421 iface |= 0x0090;
422 break;
423 case SND_SOC_DAIFMT_IB_NF:
424 iface |= 0x0080;
425 break;
426 case SND_SOC_DAIFMT_NB_IF:
427 iface |= 0x0010;
428 break;
429 default:
430 return -EINVAL;
431 }
432
433 /* set iface */
434 wm8731_write(codec, WM8731_IFACE, iface);
435 return 0;
436}
437
Mark Brown0be98982008-05-19 12:31:28 +0200438static int wm8731_set_bias_level(struct snd_soc_codec *codec,
439 enum snd_soc_bias_level level)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200440{
441 u16 reg = wm8731_read_reg_cache(codec, WM8731_PWR) & 0xff7f;
442
Mark Brown0be98982008-05-19 12:31:28 +0200443 switch (level) {
444 case SND_SOC_BIAS_ON:
Richard Purdie40e0aa62006-10-06 18:36:07 +0200445 /* vref/mid, osc on, dac unmute */
446 wm8731_write(codec, WM8731_PWR, reg);
447 break;
Mark Brown0be98982008-05-19 12:31:28 +0200448 case SND_SOC_BIAS_PREPARE:
Richard Purdie40e0aa62006-10-06 18:36:07 +0200449 break;
Mark Brown0be98982008-05-19 12:31:28 +0200450 case SND_SOC_BIAS_STANDBY:
Richard Purdie40e0aa62006-10-06 18:36:07 +0200451 /* everything off except vref/vmid, */
452 wm8731_write(codec, WM8731_PWR, reg | 0x0040);
453 break;
Mark Brown0be98982008-05-19 12:31:28 +0200454 case SND_SOC_BIAS_OFF:
Richard Purdie40e0aa62006-10-06 18:36:07 +0200455 /* everything off, dac mute, inactive */
456 wm8731_write(codec, WM8731_ACTIVE, 0x0);
457 wm8731_write(codec, WM8731_PWR, 0xffff);
458 break;
459 }
Mark Brown0be98982008-05-19 12:31:28 +0200460 codec->bias_level = level;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200461 return 0;
462}
463
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100464#define WM8731_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
465 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
466 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
467 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
468 SNDRV_PCM_RATE_96000)
469
470#define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
471 SNDRV_PCM_FMTBIT_S24_LE)
472
Richard Purdie40e0aa62006-10-06 18:36:07 +0200473struct snd_soc_codec_dai wm8731_dai = {
474 .name = "WM8731",
475 .playback = {
476 .stream_name = "Playback",
477 .channels_min = 1,
478 .channels_max = 2,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100479 .rates = WM8731_RATES,
480 .formats = WM8731_FORMATS,},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200481 .capture = {
482 .stream_name = "Capture",
483 .channels_min = 1,
484 .channels_max = 2,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100485 .rates = WM8731_RATES,
486 .formats = WM8731_FORMATS,},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200487 .ops = {
488 .prepare = wm8731_pcm_prepare,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100489 .hw_params = wm8731_hw_params,
Richard Purdie40e0aa62006-10-06 18:36:07 +0200490 .shutdown = wm8731_shutdown,
491 },
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100492 .dai_ops = {
493 .digital_mute = wm8731_mute,
494 .set_sysclk = wm8731_set_dai_sysclk,
495 .set_fmt = wm8731_set_dai_fmt,
496 }
Richard Purdie40e0aa62006-10-06 18:36:07 +0200497};
498EXPORT_SYMBOL_GPL(wm8731_dai);
499
500static int wm8731_suspend(struct platform_device *pdev, pm_message_t state)
501{
502 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
503 struct snd_soc_codec *codec = socdev->codec;
504
505 wm8731_write(codec, WM8731_ACTIVE, 0x0);
Mark Brown0be98982008-05-19 12:31:28 +0200506 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200507 return 0;
508}
509
510static int wm8731_resume(struct platform_device *pdev)
511{
512 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
513 struct snd_soc_codec *codec = socdev->codec;
514 int i;
515 u8 data[2];
516 u16 *cache = codec->reg_cache;
517
518 /* Sync reg_cache with the hardware */
519 for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
520 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
521 data[1] = cache[i] & 0x00ff;
522 codec->hw_write(codec->control_data, data, 2);
523 }
Mark Brown0be98982008-05-19 12:31:28 +0200524 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
525 wm8731_set_bias_level(codec, codec->suspend_bias_level);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200526 return 0;
527}
528
529/*
530 * initialise the WM8731 driver
531 * register the mixer and dsp interfaces with the kernel
532 */
533static int wm8731_init(struct snd_soc_device *socdev)
534{
535 struct snd_soc_codec *codec = socdev->codec;
536 int reg, ret = 0;
537
538 codec->name = "WM8731";
539 codec->owner = THIS_MODULE;
540 codec->read = wm8731_read_reg_cache;
541 codec->write = wm8731_write;
Mark Brown0be98982008-05-19 12:31:28 +0200542 codec->set_bias_level = wm8731_set_bias_level;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200543 codec->dai = &wm8731_dai;
544 codec->num_dai = 1;
Takashi Iwai88cb4292007-02-05 14:56:20 +0100545 codec->reg_cache_size = sizeof(wm8731_reg);
546 codec->reg_cache = kmemdup(wm8731_reg, sizeof(wm8731_reg), GFP_KERNEL);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200547 if (codec->reg_cache == NULL)
548 return -ENOMEM;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200549
550 wm8731_reset(codec);
551
552 /* register pcms */
553 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
554 if (ret < 0) {
Liam Girdwoode35115a2007-01-31 10:02:23 +0100555 printk(KERN_ERR "wm8731: failed to create pcms\n");
556 goto pcm_err;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200557 }
558
559 /* power on device */
Mark Brown0be98982008-05-19 12:31:28 +0200560 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200561
562 /* set the update bits */
563 reg = wm8731_read_reg_cache(codec, WM8731_LOUT1V);
Ville Syrjala389619f2007-11-26 08:58:24 +0100564 wm8731_write(codec, WM8731_LOUT1V, reg & ~0x0100);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200565 reg = wm8731_read_reg_cache(codec, WM8731_ROUT1V);
Ville Syrjala389619f2007-11-26 08:58:24 +0100566 wm8731_write(codec, WM8731_ROUT1V, reg & ~0x0100);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200567 reg = wm8731_read_reg_cache(codec, WM8731_LINVOL);
Ville Syrjala389619f2007-11-26 08:58:24 +0100568 wm8731_write(codec, WM8731_LINVOL, reg & ~0x0100);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200569 reg = wm8731_read_reg_cache(codec, WM8731_RINVOL);
Ville Syrjala389619f2007-11-26 08:58:24 +0100570 wm8731_write(codec, WM8731_RINVOL, reg & ~0x0100);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200571
572 wm8731_add_controls(codec);
573 wm8731_add_widgets(codec);
574 ret = snd_soc_register_card(socdev);
575 if (ret < 0) {
Liam Girdwoode35115a2007-01-31 10:02:23 +0100576 printk(KERN_ERR "wm8731: failed to register card\n");
577 goto card_err;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200578 }
579
580 return ret;
Liam Girdwoode35115a2007-01-31 10:02:23 +0100581
582card_err:
583 snd_soc_free_pcms(socdev);
584 snd_soc_dapm_free(socdev);
585pcm_err:
586 kfree(codec->reg_cache);
587 return ret;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200588}
589
590static struct snd_soc_device *wm8731_socdev;
591
Mark Brownd454aee2008-04-23 15:16:46 +0200592#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200593
594/*
595 * WM8731 2 wire address is determined by GPIO5
596 * state during powerup.
597 * low = 0x1a
598 * high = 0x1b
599 */
600static unsigned short normal_i2c[] = { 0, I2C_CLIENT_END };
601
602/* Magic definition of all other variables and things */
603I2C_CLIENT_INSMOD;
604
605static struct i2c_driver wm8731_i2c_driver;
606static struct i2c_client client_template;
607
608/* If the i2c layer weren't so broken, we could pass this kind of data
609 around */
610
611static int wm8731_codec_probe(struct i2c_adapter *adap, int addr, int kind)
612{
613 struct snd_soc_device *socdev = wm8731_socdev;
614 struct wm8731_setup_data *setup = socdev->codec_data;
615 struct snd_soc_codec *codec = socdev->codec;
616 struct i2c_client *i2c;
617 int ret;
618
619 if (addr != setup->i2c_address)
620 return -ENODEV;
621
622 client_template.adapter = adap;
623 client_template.addr = addr;
624
Takashi Iwai88cb4292007-02-05 14:56:20 +0100625 i2c = kmemdup(&client_template, sizeof(client_template), GFP_KERNEL);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200626 if (i2c == NULL) {
627 kfree(codec);
628 return -ENOMEM;
629 }
Richard Purdie40e0aa62006-10-06 18:36:07 +0200630 i2c_set_clientdata(i2c, codec);
631 codec->control_data = i2c;
632
633 ret = i2c_attach_client(i2c);
634 if (ret < 0) {
635 err("failed to attach codec at addr %x\n", addr);
636 goto err;
637 }
638
639 ret = wm8731_init(socdev);
640 if (ret < 0) {
641 err("failed to initialise WM8731\n");
642 goto err;
643 }
644 return ret;
645
646err:
647 kfree(codec);
648 kfree(i2c);
649 return ret;
650}
651
652static int wm8731_i2c_detach(struct i2c_client *client)
653{
Mark Brownd454aee2008-04-23 15:16:46 +0200654 struct snd_soc_codec *codec = i2c_get_clientdata(client);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200655 i2c_detach_client(client);
656 kfree(codec->reg_cache);
657 kfree(client);
658 return 0;
659}
660
661static int wm8731_i2c_attach(struct i2c_adapter *adap)
662{
663 return i2c_probe(adap, &addr_data, wm8731_codec_probe);
664}
665
666/* corgi i2c codec control layer */
667static struct i2c_driver wm8731_i2c_driver = {
668 .driver = {
669 .name = "WM8731 I2C Codec",
670 .owner = THIS_MODULE,
671 },
672 .id = I2C_DRIVERID_WM8731,
673 .attach_adapter = wm8731_i2c_attach,
674 .detach_client = wm8731_i2c_detach,
675 .command = NULL,
676};
677
678static struct i2c_client client_template = {
679 .name = "WM8731",
680 .driver = &wm8731_i2c_driver,
681};
682#endif
683
684static int wm8731_probe(struct platform_device *pdev)
685{
686 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
687 struct wm8731_setup_data *setup;
688 struct snd_soc_codec *codec;
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100689 struct wm8731_priv *wm8731;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200690 int ret = 0;
691
692 info("WM8731 Audio Codec %s", WM8731_VERSION);
693
694 setup = socdev->codec_data;
695 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
696 if (codec == NULL)
697 return -ENOMEM;
698
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100699 wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
700 if (wm8731 == NULL) {
701 kfree(codec);
702 return -ENOMEM;
703 }
704
705 codec->private_data = wm8731;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200706 socdev->codec = codec;
707 mutex_init(&codec->mutex);
708 INIT_LIST_HEAD(&codec->dapm_widgets);
709 INIT_LIST_HEAD(&codec->dapm_paths);
710
711 wm8731_socdev = socdev;
Mark Brownd454aee2008-04-23 15:16:46 +0200712#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200713 if (setup->i2c_address) {
714 normal_i2c[0] = setup->i2c_address;
715 codec->hw_write = (hw_write_t)i2c_master_send;
716 ret = i2c_add_driver(&wm8731_i2c_driver);
717 if (ret != 0)
718 printk(KERN_ERR "can't add i2c driver");
719 }
720#else
721 /* Add other interfaces here */
722#endif
723 return ret;
724}
725
726/* power down chip */
727static int wm8731_remove(struct platform_device *pdev)
728{
729 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
730 struct snd_soc_codec *codec = socdev->codec;
731
732 if (codec->control_data)
Mark Brown0be98982008-05-19 12:31:28 +0200733 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200734
735 snd_soc_free_pcms(socdev);
736 snd_soc_dapm_free(socdev);
Mark Brownd454aee2008-04-23 15:16:46 +0200737#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200738 i2c_del_driver(&wm8731_i2c_driver);
739#endif
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100740 kfree(codec->private_data);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200741 kfree(codec);
742
743 return 0;
744}
745
746struct snd_soc_codec_device soc_codec_dev_wm8731 = {
747 .probe = wm8731_probe,
748 .remove = wm8731_remove,
749 .suspend = wm8731_suspend,
750 .resume = wm8731_resume,
751};
Richard Purdie40e0aa62006-10-06 18:36:07 +0200752EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731);
753
754MODULE_DESCRIPTION("ASoC WM8731 driver");
755MODULE_AUTHOR("Richard Purdie");
756MODULE_LICENSE("GPL");