blob: 3ff971aeba2126c868a5aa3be54a522f248c997f [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
Richard Purdie40e0aa62006-10-06 18:36:07 +020032struct snd_soc_codec_device soc_codec_dev_wm8731;
33
Frank Mandarinob36d61d2007-02-02 17:14:56 +010034/* codec private data */
35struct wm8731_priv {
36 unsigned int sysclk;
37};
38
Mark Browna8035c82009-02-16 19:35:43 +000039#ifdef CONFIG_SPI_MASTER
40static int wm8731_spi_write(struct spi_device *spi, const char *data, int len);
41static struct spi_driver wm8731_spi_driver;
42#endif
43
Richard Purdie40e0aa62006-10-06 18:36:07 +020044/*
45 * wm8731 register cache
46 * We can't read the WM8731 register space when we are
47 * using 2 wire for device control, so we cache them instead.
48 * There is no point in caching the reset register
49 */
50static const u16 wm8731_reg[WM8731_CACHEREGNUM] = {
51 0x0097, 0x0097, 0x0079, 0x0079,
52 0x000a, 0x0008, 0x009f, 0x000a,
53 0x0000, 0x0000
54};
55
Richard Purdie40e0aa62006-10-06 18:36:07 +020056/*
57 * read wm8731 register cache
58 */
59static inline unsigned int wm8731_read_reg_cache(struct snd_soc_codec *codec,
60 unsigned int reg)
61{
62 u16 *cache = codec->reg_cache;
63 if (reg == WM8731_RESET)
64 return 0;
65 if (reg >= WM8731_CACHEREGNUM)
66 return -1;
67 return cache[reg];
68}
69
70/*
71 * write wm8731 register cache
72 */
73static inline void wm8731_write_reg_cache(struct snd_soc_codec *codec,
74 u16 reg, unsigned int value)
75{
76 u16 *cache = codec->reg_cache;
77 if (reg >= WM8731_CACHEREGNUM)
78 return;
79 cache[reg] = value;
80}
81
82/*
83 * write to the WM8731 register space
84 */
85static int wm8731_write(struct snd_soc_codec *codec, unsigned int reg,
86 unsigned int value)
87{
88 u8 data[2];
89
90 /* data is
91 * D15..D9 WM8731 register offset
92 * D8...D0 register data
93 */
94 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
95 data[1] = value & 0x00ff;
96
Mark Brownd454aee2008-04-23 15:16:46 +020097 wm8731_write_reg_cache(codec, reg, value);
Richard Purdie40e0aa62006-10-06 18:36:07 +020098 if (codec->hw_write(codec->control_data, data, 2) == 2)
99 return 0;
100 else
101 return -EIO;
102}
103
104#define wm8731_reset(c) wm8731_write(c, WM8731_RESET, 0)
105
106static const char *wm8731_input_select[] = {"Line In", "Mic"};
107static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
108
109static const struct soc_enum wm8731_enum[] = {
110 SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select),
111 SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph),
112};
113
114static const struct snd_kcontrol_new wm8731_snd_controls[] = {
115
Liam Girdwoodbd903b62006-11-09 16:35:01 +0100116SOC_DOUBLE_R("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
117 0, 127, 0),
118SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
119 7, 1, 0),
Richard Purdie40e0aa62006-10-06 18:36:07 +0200120
121SOC_DOUBLE_R("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0),
122SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
123
124SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0),
125SOC_SINGLE("Capture Mic Switch", WM8731_APANA, 1, 1, 1),
126
127SOC_SINGLE("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1),
128
129SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
130SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
131
132SOC_ENUM("Playback De-emphasis", wm8731_enum[1]),
133};
134
Richard Purdie40e0aa62006-10-06 18:36:07 +0200135/* Output Mixer */
136static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
137SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
138SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
139SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
140};
141
142/* Input mux */
143static const struct snd_kcontrol_new wm8731_input_mux_controls =
144SOC_DAPM_ENUM("Input Select", wm8731_enum[0]);
145
146static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
147SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
148 &wm8731_output_mixer_controls[0],
149 ARRAY_SIZE(wm8731_output_mixer_controls)),
150SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
151SND_SOC_DAPM_OUTPUT("LOUT"),
152SND_SOC_DAPM_OUTPUT("LHPOUT"),
153SND_SOC_DAPM_OUTPUT("ROUT"),
154SND_SOC_DAPM_OUTPUT("RHPOUT"),
155SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
156SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
157SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
158SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
159SND_SOC_DAPM_INPUT("MICIN"),
160SND_SOC_DAPM_INPUT("RLINEIN"),
161SND_SOC_DAPM_INPUT("LLINEIN"),
162};
163
Mark Browna65f0562008-05-13 14:54:43 +0200164static const struct snd_soc_dapm_route intercon[] = {
Richard Purdie40e0aa62006-10-06 18:36:07 +0200165 /* output mixer */
166 {"Output Mixer", "Line Bypass Switch", "Line Input"},
167 {"Output Mixer", "HiFi Playback Switch", "DAC"},
168 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
169
170 /* outputs */
171 {"RHPOUT", NULL, "Output Mixer"},
172 {"ROUT", NULL, "Output Mixer"},
173 {"LHPOUT", NULL, "Output Mixer"},
174 {"LOUT", NULL, "Output Mixer"},
175
176 /* input mux */
177 {"Input Mux", "Line In", "Line Input"},
178 {"Input Mux", "Mic", "Mic Bias"},
179 {"ADC", NULL, "Input Mux"},
180
181 /* inputs */
182 {"Line Input", NULL, "LLINEIN"},
183 {"Line Input", NULL, "RLINEIN"},
184 {"Mic Bias", NULL, "MICIN"},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200185};
186
187static int wm8731_add_widgets(struct snd_soc_codec *codec)
188{
Mark Browna65f0562008-05-13 14:54:43 +0200189 snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
190 ARRAY_SIZE(wm8731_dapm_widgets));
Richard Purdie40e0aa62006-10-06 18:36:07 +0200191
Mark Browna65f0562008-05-13 14:54:43 +0200192 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
Richard Purdie40e0aa62006-10-06 18:36:07 +0200193
194 snd_soc_dapm_new_widgets(codec);
195 return 0;
196}
197
198struct _coeff_div {
199 u32 mclk;
200 u32 rate;
201 u16 fs;
202 u8 sr:4;
203 u8 bosr:1;
204 u8 usb:1;
205};
206
207/* codec mclk clock divider coefficients */
208static const struct _coeff_div coeff_div[] = {
209 /* 48k */
210 {12288000, 48000, 256, 0x0, 0x0, 0x0},
211 {18432000, 48000, 384, 0x0, 0x1, 0x0},
212 {12000000, 48000, 250, 0x0, 0x0, 0x1},
213
214 /* 32k */
215 {12288000, 32000, 384, 0x6, 0x0, 0x0},
216 {18432000, 32000, 576, 0x6, 0x1, 0x0},
Frank Mandarino298a2c72007-01-31 10:02:56 +0100217 {12000000, 32000, 375, 0x6, 0x0, 0x1},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200218
219 /* 8k */
220 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
221 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
222 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
223 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
224 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
225
226 /* 96k */
227 {12288000, 96000, 128, 0x7, 0x0, 0x0},
228 {18432000, 96000, 192, 0x7, 0x1, 0x0},
229 {12000000, 96000, 125, 0x7, 0x0, 0x1},
230
231 /* 44.1k */
232 {11289600, 44100, 256, 0x8, 0x0, 0x0},
233 {16934400, 44100, 384, 0x8, 0x1, 0x0},
234 {12000000, 44100, 272, 0x8, 0x1, 0x1},
235
236 /* 88.2k */
237 {11289600, 88200, 128, 0xf, 0x0, 0x0},
238 {16934400, 88200, 192, 0xf, 0x1, 0x0},
239 {12000000, 88200, 136, 0xf, 0x1, 0x1},
240};
241
242static inline int get_coeff(int mclk, int rate)
243{
244 int i;
245
246 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
247 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
248 return i;
249 }
250 return 0;
251}
252
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100253static int wm8731_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000254 struct snd_pcm_hw_params *params,
255 struct snd_soc_dai *dai)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200256{
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100257 struct snd_soc_pcm_runtime *rtd = substream->private_data;
258 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000259 struct snd_soc_codec *codec = socdev->card->codec;
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100260 struct wm8731_priv *wm8731 = codec->private_data;
261 u16 iface = wm8731_read_reg_cache(codec, WM8731_IFACE) & 0xfff3;
262 int i = get_coeff(wm8731->sysclk, params_rate(params));
263 u16 srate = (coeff_div[i].sr << 2) |
264 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200265
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100266 wm8731_write(codec, WM8731_SRATE, srate);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200267
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100268 /* bit size */
269 switch (params_format(params)) {
270 case SNDRV_PCM_FORMAT_S16_LE:
271 break;
272 case SNDRV_PCM_FORMAT_S20_3LE:
273 iface |= 0x0004;
274 break;
275 case SNDRV_PCM_FORMAT_S24_LE:
276 iface |= 0x0008;
277 break;
278 }
279
280 wm8731_write(codec, WM8731_IFACE, iface);
281 return 0;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200282}
283
Mark Browndee89c42008-11-18 22:11:38 +0000284static int wm8731_pcm_prepare(struct snd_pcm_substream *substream,
285 struct snd_soc_dai *dai)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200286{
287 struct snd_soc_pcm_runtime *rtd = substream->private_data;
288 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000289 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200290
291 /* set active */
292 wm8731_write(codec, WM8731_ACTIVE, 0x0001);
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100293
Richard Purdie40e0aa62006-10-06 18:36:07 +0200294 return 0;
295}
296
Mark Browndee89c42008-11-18 22:11:38 +0000297static void wm8731_shutdown(struct snd_pcm_substream *substream,
298 struct snd_soc_dai *dai)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200299{
300 struct snd_soc_pcm_runtime *rtd = substream->private_data;
301 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000302 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200303
304 /* deactivate */
305 if (!codec->active) {
306 udelay(50);
307 wm8731_write(codec, WM8731_ACTIVE, 0x0);
308 }
309}
310
Liam Girdwoode550e172008-07-07 16:07:52 +0100311static int wm8731_mute(struct snd_soc_dai *dai, int mute)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200312{
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100313 struct snd_soc_codec *codec = dai->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200314 u16 mute_reg = wm8731_read_reg_cache(codec, WM8731_APDIGI) & 0xfff7;
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100315
Richard Purdie40e0aa62006-10-06 18:36:07 +0200316 if (mute)
317 wm8731_write(codec, WM8731_APDIGI, mute_reg | 0x8);
318 else
319 wm8731_write(codec, WM8731_APDIGI, mute_reg);
320 return 0;
321}
322
Liam Girdwoode550e172008-07-07 16:07:52 +0100323static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100324 int clk_id, unsigned int freq, int dir)
325{
326 struct snd_soc_codec *codec = codec_dai->codec;
327 struct wm8731_priv *wm8731 = codec->private_data;
328
329 switch (freq) {
330 case 11289600:
331 case 12000000:
332 case 12288000:
333 case 16934400:
334 case 18432000:
335 wm8731->sysclk = freq;
336 return 0;
337 }
338 return -EINVAL;
339}
340
341
Liam Girdwoode550e172008-07-07 16:07:52 +0100342static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100343 unsigned int fmt)
344{
345 struct snd_soc_codec *codec = codec_dai->codec;
346 u16 iface = 0;
347
348 /* set master/slave audio interface */
349 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
350 case SND_SOC_DAIFMT_CBM_CFM:
351 iface |= 0x0040;
352 break;
353 case SND_SOC_DAIFMT_CBS_CFS:
354 break;
355 default:
356 return -EINVAL;
357 }
358
359 /* interface format */
360 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
361 case SND_SOC_DAIFMT_I2S:
362 iface |= 0x0002;
363 break;
364 case SND_SOC_DAIFMT_RIGHT_J:
365 break;
366 case SND_SOC_DAIFMT_LEFT_J:
367 iface |= 0x0001;
368 break;
369 case SND_SOC_DAIFMT_DSP_A:
370 iface |= 0x0003;
371 break;
372 case SND_SOC_DAIFMT_DSP_B:
373 iface |= 0x0013;
374 break;
375 default:
376 return -EINVAL;
377 }
378
379 /* clock inversion */
380 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
381 case SND_SOC_DAIFMT_NB_NF:
382 break;
383 case SND_SOC_DAIFMT_IB_IF:
384 iface |= 0x0090;
385 break;
386 case SND_SOC_DAIFMT_IB_NF:
387 iface |= 0x0080;
388 break;
389 case SND_SOC_DAIFMT_NB_IF:
390 iface |= 0x0010;
391 break;
392 default:
393 return -EINVAL;
394 }
395
396 /* set iface */
397 wm8731_write(codec, WM8731_IFACE, iface);
398 return 0;
399}
400
Mark Brown0be98982008-05-19 12:31:28 +0200401static int wm8731_set_bias_level(struct snd_soc_codec *codec,
402 enum snd_soc_bias_level level)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200403{
Mark Brown22d22ee2009-02-16 19:20:15 +0000404 u16 reg;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200405
Mark Brown0be98982008-05-19 12:31:28 +0200406 switch (level) {
407 case SND_SOC_BIAS_ON:
Richard Purdie40e0aa62006-10-06 18:36:07 +0200408 break;
Mark Brown0be98982008-05-19 12:31:28 +0200409 case SND_SOC_BIAS_PREPARE:
Richard Purdie40e0aa62006-10-06 18:36:07 +0200410 break;
Mark Brown0be98982008-05-19 12:31:28 +0200411 case SND_SOC_BIAS_STANDBY:
Mark Brown22d22ee2009-02-16 19:20:15 +0000412 /* Clear PWROFF, gate CLKOUT, everything else as-is */
413 reg = wm8731_read_reg_cache(codec, WM8731_PWR) & 0xff7f;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200414 wm8731_write(codec, WM8731_PWR, reg | 0x0040);
415 break;
Mark Brown0be98982008-05-19 12:31:28 +0200416 case SND_SOC_BIAS_OFF:
Richard Purdie40e0aa62006-10-06 18:36:07 +0200417 wm8731_write(codec, WM8731_ACTIVE, 0x0);
418 wm8731_write(codec, WM8731_PWR, 0xffff);
419 break;
420 }
Mark Brown0be98982008-05-19 12:31:28 +0200421 codec->bias_level = level;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200422 return 0;
423}
424
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100425#define WM8731_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
426 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
427 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
428 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
429 SNDRV_PCM_RATE_96000)
430
431#define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
432 SNDRV_PCM_FMTBIT_S24_LE)
433
Liam Girdwoode550e172008-07-07 16:07:52 +0100434struct snd_soc_dai wm8731_dai = {
Richard Purdie40e0aa62006-10-06 18:36:07 +0200435 .name = "WM8731",
436 .playback = {
437 .stream_name = "Playback",
438 .channels_min = 1,
439 .channels_max = 2,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100440 .rates = WM8731_RATES,
441 .formats = WM8731_FORMATS,},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200442 .capture = {
443 .stream_name = "Capture",
444 .channels_min = 1,
445 .channels_max = 2,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100446 .rates = WM8731_RATES,
447 .formats = WM8731_FORMATS,},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200448 .ops = {
449 .prepare = wm8731_pcm_prepare,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100450 .hw_params = wm8731_hw_params,
Richard Purdie40e0aa62006-10-06 18:36:07 +0200451 .shutdown = wm8731_shutdown,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100452 .digital_mute = wm8731_mute,
453 .set_sysclk = wm8731_set_dai_sysclk,
454 .set_fmt = wm8731_set_dai_fmt,
455 }
Richard Purdie40e0aa62006-10-06 18:36:07 +0200456};
457EXPORT_SYMBOL_GPL(wm8731_dai);
458
459static int wm8731_suspend(struct platform_device *pdev, pm_message_t state)
460{
461 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +0000462 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200463
464 wm8731_write(codec, WM8731_ACTIVE, 0x0);
Mark Brown0be98982008-05-19 12:31:28 +0200465 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200466 return 0;
467}
468
469static int wm8731_resume(struct platform_device *pdev)
470{
471 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +0000472 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200473 int i;
474 u8 data[2];
475 u16 *cache = codec->reg_cache;
476
477 /* Sync reg_cache with the hardware */
478 for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
479 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
480 data[1] = cache[i] & 0x00ff;
481 codec->hw_write(codec->control_data, data, 2);
482 }
Mark Brown0be98982008-05-19 12:31:28 +0200483 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
484 wm8731_set_bias_level(codec, codec->suspend_bias_level);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200485 return 0;
486}
487
488/*
489 * initialise the WM8731 driver
490 * register the mixer and dsp interfaces with the kernel
491 */
492static int wm8731_init(struct snd_soc_device *socdev)
493{
Mark Brown6627a652009-01-23 22:55:23 +0000494 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200495 int reg, ret = 0;
496
497 codec->name = "WM8731";
498 codec->owner = THIS_MODULE;
499 codec->read = wm8731_read_reg_cache;
500 codec->write = wm8731_write;
Mark Brown0be98982008-05-19 12:31:28 +0200501 codec->set_bias_level = wm8731_set_bias_level;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200502 codec->dai = &wm8731_dai;
503 codec->num_dai = 1;
Mark Brownd751b232008-06-11 13:47:06 +0100504 codec->reg_cache_size = ARRAY_SIZE(wm8731_reg);
Takashi Iwai88cb4292007-02-05 14:56:20 +0100505 codec->reg_cache = kmemdup(wm8731_reg, sizeof(wm8731_reg), GFP_KERNEL);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200506 if (codec->reg_cache == NULL)
507 return -ENOMEM;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200508
509 wm8731_reset(codec);
510
511 /* register pcms */
512 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
513 if (ret < 0) {
Liam Girdwoode35115a2007-01-31 10:02:23 +0100514 printk(KERN_ERR "wm8731: failed to create pcms\n");
515 goto pcm_err;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200516 }
517
518 /* power on device */
Mark Brown0be98982008-05-19 12:31:28 +0200519 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200520
521 /* set the update bits */
522 reg = wm8731_read_reg_cache(codec, WM8731_LOUT1V);
Ville Syrjala389619f2007-11-26 08:58:24 +0100523 wm8731_write(codec, WM8731_LOUT1V, reg & ~0x0100);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200524 reg = wm8731_read_reg_cache(codec, WM8731_ROUT1V);
Ville Syrjala389619f2007-11-26 08:58:24 +0100525 wm8731_write(codec, WM8731_ROUT1V, reg & ~0x0100);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200526 reg = wm8731_read_reg_cache(codec, WM8731_LINVOL);
Ville Syrjala389619f2007-11-26 08:58:24 +0100527 wm8731_write(codec, WM8731_LINVOL, reg & ~0x0100);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200528 reg = wm8731_read_reg_cache(codec, WM8731_RINVOL);
Ville Syrjala389619f2007-11-26 08:58:24 +0100529 wm8731_write(codec, WM8731_RINVOL, reg & ~0x0100);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200530
Ian Molton3e8e1952009-01-09 00:23:21 +0000531 snd_soc_add_controls(codec, wm8731_snd_controls,
532 ARRAY_SIZE(wm8731_snd_controls));
Richard Purdie40e0aa62006-10-06 18:36:07 +0200533 wm8731_add_widgets(codec);
Mark Brown968a6022008-11-28 11:49:07 +0000534 ret = snd_soc_init_card(socdev);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200535 if (ret < 0) {
Liam Girdwoode35115a2007-01-31 10:02:23 +0100536 printk(KERN_ERR "wm8731: failed to register card\n");
537 goto card_err;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200538 }
539
540 return ret;
Liam Girdwoode35115a2007-01-31 10:02:23 +0100541
542card_err:
543 snd_soc_free_pcms(socdev);
544 snd_soc_dapm_free(socdev);
545pcm_err:
546 kfree(codec->reg_cache);
547 return ret;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200548}
549
550static struct snd_soc_device *wm8731_socdev;
551
Mark Browna8035c82009-02-16 19:35:43 +0000552
Mark Brownd454aee2008-04-23 15:16:46 +0200553#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Mark Browna8035c82009-02-16 19:35:43 +0000554static struct i2c_driver wm8731_i2c_driver;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200555
Jean Delvare81297c8a2008-09-01 18:47:01 +0100556static int wm8731_add_i2c_device(struct platform_device *pdev,
557 const struct wm8731_setup_data *setup)
558{
559 struct i2c_board_info info;
560 struct i2c_adapter *adapter;
561 struct i2c_client *client;
562 int ret;
563
564 ret = i2c_add_driver(&wm8731_i2c_driver);
565 if (ret != 0) {
566 dev_err(&pdev->dev, "can't add i2c driver\n");
567 return ret;
568 }
569
570 memset(&info, 0, sizeof(struct i2c_board_info));
571 info.addr = setup->i2c_address;
572 strlcpy(info.type, "wm8731", I2C_NAME_SIZE);
573
574 adapter = i2c_get_adapter(setup->i2c_bus);
575 if (!adapter) {
576 dev_err(&pdev->dev, "can't get i2c adapter %d\n",
577 setup->i2c_bus);
578 goto err_driver;
579 }
580
581 client = i2c_new_device(adapter, &info);
582 i2c_put_adapter(adapter);
583 if (!client) {
584 dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
585 (unsigned int)info.addr);
586 goto err_driver;
587 }
588
589 return 0;
590
591err_driver:
592 i2c_del_driver(&wm8731_i2c_driver);
593 return -ENODEV;
594}
Richard Purdie40e0aa62006-10-06 18:36:07 +0200595#endif
596
597static int wm8731_probe(struct platform_device *pdev)
598{
599 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
600 struct wm8731_setup_data *setup;
601 struct snd_soc_codec *codec;
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100602 struct wm8731_priv *wm8731;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200603 int ret = 0;
604
Richard Purdie40e0aa62006-10-06 18:36:07 +0200605 setup = socdev->codec_data;
606 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
607 if (codec == NULL)
608 return -ENOMEM;
609
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100610 wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
611 if (wm8731 == NULL) {
612 kfree(codec);
613 return -ENOMEM;
614 }
615
616 codec->private_data = wm8731;
Mark Brown6627a652009-01-23 22:55:23 +0000617 socdev->card->codec = codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200618 mutex_init(&codec->mutex);
619 INIT_LIST_HEAD(&codec->dapm_widgets);
620 INIT_LIST_HEAD(&codec->dapm_paths);
621
622 wm8731_socdev = socdev;
Cliff Caid2a40352008-09-01 18:47:03 +0100623 ret = -ENODEV;
624
Mark Brownd454aee2008-04-23 15:16:46 +0200625#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200626 if (setup->i2c_address) {
Richard Purdie40e0aa62006-10-06 18:36:07 +0200627 codec->hw_write = (hw_write_t)i2c_master_send;
Jean Delvare81297c8a2008-09-01 18:47:01 +0100628 ret = wm8731_add_i2c_device(pdev, setup);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200629 }
Cliff Caid2a40352008-09-01 18:47:03 +0100630#endif
631#if defined(CONFIG_SPI_MASTER)
632 if (setup->spi) {
633 codec->hw_write = (hw_write_t)wm8731_spi_write;
634 ret = spi_register_driver(&wm8731_spi_driver);
635 if (ret != 0)
636 printk(KERN_ERR "can't add spi driver");
637 }
Richard Purdie40e0aa62006-10-06 18:36:07 +0200638#endif
Jean Delvare3051e412008-08-25 11:49:20 +0100639
640 if (ret != 0) {
641 kfree(codec->private_data);
642 kfree(codec);
643 }
Richard Purdie40e0aa62006-10-06 18:36:07 +0200644 return ret;
645}
646
647/* power down chip */
648static int wm8731_remove(struct platform_device *pdev)
649{
650 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +0000651 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200652
653 if (codec->control_data)
Mark Brown0be98982008-05-19 12:31:28 +0200654 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200655
656 snd_soc_free_pcms(socdev);
657 snd_soc_dapm_free(socdev);
Mark Brownd454aee2008-04-23 15:16:46 +0200658#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Jean Delvare81297c8a2008-09-01 18:47:01 +0100659 i2c_unregister_device(codec->control_data);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200660 i2c_del_driver(&wm8731_i2c_driver);
661#endif
Cliff Caid2a40352008-09-01 18:47:03 +0100662#if defined(CONFIG_SPI_MASTER)
663 spi_unregister_driver(&wm8731_spi_driver);
664#endif
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100665 kfree(codec->private_data);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200666 kfree(codec);
667
668 return 0;
669}
670
671struct snd_soc_codec_device soc_codec_dev_wm8731 = {
672 .probe = wm8731_probe,
673 .remove = wm8731_remove,
674 .suspend = wm8731_suspend,
675 .resume = wm8731_resume,
676};
Richard Purdie40e0aa62006-10-06 18:36:07 +0200677EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731);
678
Mark Browna8035c82009-02-16 19:35:43 +0000679#if defined(CONFIG_SPI_MASTER)
680static int __devinit wm8731_spi_probe(struct spi_device *spi)
681{
682 struct snd_soc_device *socdev = wm8731_socdev;
683 struct snd_soc_codec *codec = socdev->card->codec;
684 int ret;
685
686 codec->control_data = spi;
687
688 ret = wm8731_init(socdev);
689 if (ret < 0)
690 dev_err(&spi->dev, "failed to initialise WM8731\n");
691
692 return ret;
693}
694
695static int __devexit wm8731_spi_remove(struct spi_device *spi)
696{
697 return 0;
698}
699
700static struct spi_driver wm8731_spi_driver = {
701 .driver = {
702 .name = "wm8731",
703 .bus = &spi_bus_type,
704 .owner = THIS_MODULE,
705 },
706 .probe = wm8731_spi_probe,
707 .remove = __devexit_p(wm8731_spi_remove),
708};
709
710static int wm8731_spi_write(struct spi_device *spi, const char *data, int len)
711{
712 struct spi_transfer t;
713 struct spi_message m;
714 u8 msg[2];
715
716 if (len <= 0)
717 return 0;
718
719 msg[0] = data[0];
720 msg[1] = data[1];
721
722 spi_message_init(&m);
723 memset(&t, 0, (sizeof t));
724
725 t.tx_buf = &msg[0];
726 t.len = len;
727
728 spi_message_add_tail(&t, &m);
729 spi_sync(spi, &m);
730
731 return len;
732}
733#endif /* CONFIG_SPI_MASTER */
734
735#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
736/*
737 * WM8731 2 wire address is determined by GPIO5
738 * state during powerup.
739 * low = 0x1a
740 * high = 0x1b
741 */
742
743static int wm8731_i2c_probe(struct i2c_client *i2c,
744 const struct i2c_device_id *id)
745{
746 struct snd_soc_device *socdev = wm8731_socdev;
747 struct snd_soc_codec *codec = socdev->card->codec;
748 int ret;
749
750 i2c_set_clientdata(i2c, codec);
751 codec->control_data = i2c;
752
753 ret = wm8731_init(socdev);
754 if (ret < 0)
755 pr_err("failed to initialise WM8731\n");
756
757 return ret;
758}
759
760static int wm8731_i2c_remove(struct i2c_client *client)
761{
762 struct snd_soc_codec *codec = i2c_get_clientdata(client);
763 kfree(codec->reg_cache);
764 return 0;
765}
766
767static const struct i2c_device_id wm8731_i2c_id[] = {
768 { "wm8731", 0 },
769 { }
770};
771MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
772
773static struct i2c_driver wm8731_i2c_driver = {
774 .driver = {
775 .name = "WM8731 I2C Codec",
776 .owner = THIS_MODULE,
777 },
778 .probe = wm8731_i2c_probe,
779 .remove = wm8731_i2c_remove,
780 .id_table = wm8731_i2c_id,
781};
782#endif
783
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100784static int __init wm8731_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000785{
786 return snd_soc_register_dai(&wm8731_dai);
787}
788module_init(wm8731_modinit);
789
790static void __exit wm8731_exit(void)
791{
792 snd_soc_unregister_dai(&wm8731_dai);
793}
794module_exit(wm8731_exit);
795
Richard Purdie40e0aa62006-10-06 18:36:07 +0200796MODULE_DESCRIPTION("ASoC WM8731 driver");
797MODULE_AUTHOR("Richard Purdie");
798MODULE_LICENSE("GPL");