ASoC: Add I/O control bus information to factored out cache setup
[deliverable/linux.git] / sound / soc / codecs / wm8731.c
CommitLineData
40e0aa64
RP
1/*
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>
d2a40355 22#include <linux/spi/spi.h>
40e0aa64
RP
23#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
5998102b 32static struct snd_soc_codec *wm8731_codec;
40e0aa64
RP
33struct snd_soc_codec_device soc_codec_dev_wm8731;
34
b36d61d4
FM
35/* codec private data */
36struct wm8731_priv {
5998102b
MB
37 struct snd_soc_codec codec;
38 u16 reg_cache[WM8731_CACHEREGNUM];
b36d61d4
FM
39 unsigned int sysclk;
40};
41
a8035c8f
MB
42#ifdef CONFIG_SPI_MASTER
43static int wm8731_spi_write(struct spi_device *spi, const char *data, int len);
a8035c8f
MB
44#endif
45
40e0aa64
RP
46/*
47 * wm8731 register cache
48 * We can't read the WM8731 register space when we are
49 * using 2 wire for device control, so we cache them instead.
50 * There is no point in caching the reset register
51 */
52static const u16 wm8731_reg[WM8731_CACHEREGNUM] = {
17a52fd6
MB
53 0x0097, 0x0097, 0x0079, 0x0079,
54 0x000a, 0x0008, 0x009f, 0x000a,
55 0x0000, 0x0000
40e0aa64
RP
56};
57
17a52fd6 58#define wm8731_reset(c) snd_soc_write(c, WM8731_RESET, 0)
40e0aa64
RP
59
60static const char *wm8731_input_select[] = {"Line In", "Mic"};
61static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
62
63static const struct soc_enum wm8731_enum[] = {
64 SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select),
65 SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph),
66};
67
68static const struct snd_kcontrol_new wm8731_snd_controls[] = {
69
bd903b6e
LG
70SOC_DOUBLE_R("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
71 0, 127, 0),
72SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
73 7, 1, 0),
40e0aa64
RP
74
75SOC_DOUBLE_R("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0),
76SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
77
78SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0),
79SOC_SINGLE("Capture Mic Switch", WM8731_APANA, 1, 1, 1),
80
81SOC_SINGLE("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1),
82
83SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
84SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
85
86SOC_ENUM("Playback De-emphasis", wm8731_enum[1]),
87};
88
40e0aa64
RP
89/* Output Mixer */
90static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
91SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
92SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
93SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
94};
95
96/* Input mux */
97static const struct snd_kcontrol_new wm8731_input_mux_controls =
98SOC_DAPM_ENUM("Input Select", wm8731_enum[0]);
99
100static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
101SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
102 &wm8731_output_mixer_controls[0],
103 ARRAY_SIZE(wm8731_output_mixer_controls)),
104SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
105SND_SOC_DAPM_OUTPUT("LOUT"),
106SND_SOC_DAPM_OUTPUT("LHPOUT"),
107SND_SOC_DAPM_OUTPUT("ROUT"),
108SND_SOC_DAPM_OUTPUT("RHPOUT"),
109SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
110SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
111SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
112SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
113SND_SOC_DAPM_INPUT("MICIN"),
114SND_SOC_DAPM_INPUT("RLINEIN"),
115SND_SOC_DAPM_INPUT("LLINEIN"),
116};
117
a65f0568 118static const struct snd_soc_dapm_route intercon[] = {
40e0aa64
RP
119 /* output mixer */
120 {"Output Mixer", "Line Bypass Switch", "Line Input"},
121 {"Output Mixer", "HiFi Playback Switch", "DAC"},
122 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
123
124 /* outputs */
125 {"RHPOUT", NULL, "Output Mixer"},
126 {"ROUT", NULL, "Output Mixer"},
127 {"LHPOUT", NULL, "Output Mixer"},
128 {"LOUT", NULL, "Output Mixer"},
129
130 /* input mux */
131 {"Input Mux", "Line In", "Line Input"},
132 {"Input Mux", "Mic", "Mic Bias"},
133 {"ADC", NULL, "Input Mux"},
134
135 /* inputs */
136 {"Line Input", NULL, "LLINEIN"},
137 {"Line Input", NULL, "RLINEIN"},
138 {"Mic Bias", NULL, "MICIN"},
40e0aa64
RP
139};
140
141static int wm8731_add_widgets(struct snd_soc_codec *codec)
142{
a65f0568
MB
143 snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
144 ARRAY_SIZE(wm8731_dapm_widgets));
40e0aa64 145
a65f0568 146 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
40e0aa64
RP
147
148 snd_soc_dapm_new_widgets(codec);
149 return 0;
150}
151
152struct _coeff_div {
153 u32 mclk;
154 u32 rate;
155 u16 fs;
156 u8 sr:4;
157 u8 bosr:1;
158 u8 usb:1;
159};
160
161/* codec mclk clock divider coefficients */
162static const struct _coeff_div coeff_div[] = {
163 /* 48k */
164 {12288000, 48000, 256, 0x0, 0x0, 0x0},
165 {18432000, 48000, 384, 0x0, 0x1, 0x0},
166 {12000000, 48000, 250, 0x0, 0x0, 0x1},
167
168 /* 32k */
169 {12288000, 32000, 384, 0x6, 0x0, 0x0},
170 {18432000, 32000, 576, 0x6, 0x1, 0x0},
298a2c75 171 {12000000, 32000, 375, 0x6, 0x0, 0x1},
40e0aa64
RP
172
173 /* 8k */
174 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
175 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
176 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
177 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
178 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
179
180 /* 96k */
181 {12288000, 96000, 128, 0x7, 0x0, 0x0},
182 {18432000, 96000, 192, 0x7, 0x1, 0x0},
183 {12000000, 96000, 125, 0x7, 0x0, 0x1},
184
185 /* 44.1k */
186 {11289600, 44100, 256, 0x8, 0x0, 0x0},
187 {16934400, 44100, 384, 0x8, 0x1, 0x0},
188 {12000000, 44100, 272, 0x8, 0x1, 0x1},
189
190 /* 88.2k */
191 {11289600, 88200, 128, 0xf, 0x0, 0x0},
192 {16934400, 88200, 192, 0xf, 0x1, 0x0},
193 {12000000, 88200, 136, 0xf, 0x1, 0x1},
194};
195
196static inline int get_coeff(int mclk, int rate)
197{
198 int i;
199
200 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
201 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
202 return i;
203 }
204 return 0;
205}
206
b36d61d4 207static int wm8731_hw_params(struct snd_pcm_substream *substream,
dee89c4d
MB
208 struct snd_pcm_hw_params *params,
209 struct snd_soc_dai *dai)
40e0aa64 210{
b36d61d4
FM
211 struct snd_soc_pcm_runtime *rtd = substream->private_data;
212 struct snd_soc_device *socdev = rtd->socdev;
6627a653 213 struct snd_soc_codec *codec = socdev->card->codec;
b36d61d4 214 struct wm8731_priv *wm8731 = codec->private_data;
17a52fd6 215 u16 iface = snd_soc_read(codec, WM8731_IFACE) & 0xfff3;
b36d61d4
FM
216 int i = get_coeff(wm8731->sysclk, params_rate(params));
217 u16 srate = (coeff_div[i].sr << 2) |
218 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
40e0aa64 219
17a52fd6 220 snd_soc_write(codec, WM8731_SRATE, srate);
b36d61d4
FM
221
222 /* bit size */
223 switch (params_format(params)) {
224 case SNDRV_PCM_FORMAT_S16_LE:
225 break;
226 case SNDRV_PCM_FORMAT_S20_3LE:
227 iface |= 0x0004;
228 break;
229 case SNDRV_PCM_FORMAT_S24_LE:
230 iface |= 0x0008;
231 break;
232 }
40e0aa64 233
17a52fd6 234 snd_soc_write(codec, WM8731_IFACE, iface);
b36d61d4 235 return 0;
40e0aa64
RP
236}
237
dee89c4d
MB
238static int wm8731_pcm_prepare(struct snd_pcm_substream *substream,
239 struct snd_soc_dai *dai)
40e0aa64
RP
240{
241 struct snd_soc_pcm_runtime *rtd = substream->private_data;
242 struct snd_soc_device *socdev = rtd->socdev;
6627a653 243 struct snd_soc_codec *codec = socdev->card->codec;
b36d61d4
FM
244
245 /* set active */
17a52fd6 246 snd_soc_write(codec, WM8731_ACTIVE, 0x0001);
b36d61d4
FM
247
248 return 0;
249}
250
dee89c4d
MB
251static void wm8731_shutdown(struct snd_pcm_substream *substream,
252 struct snd_soc_dai *dai)
b36d61d4
FM
253{
254 struct snd_soc_pcm_runtime *rtd = substream->private_data;
255 struct snd_soc_device *socdev = rtd->socdev;
6627a653 256 struct snd_soc_codec *codec = socdev->card->codec;
b36d61d4
FM
257
258 /* deactivate */
259 if (!codec->active) {
260 udelay(50);
17a52fd6 261 snd_soc_write(codec, WM8731_ACTIVE, 0x0);
b36d61d4
FM
262 }
263}
264
e550e17f 265static int wm8731_mute(struct snd_soc_dai *dai, int mute)
b36d61d4
FM
266{
267 struct snd_soc_codec *codec = dai->codec;
17a52fd6 268 u16 mute_reg = snd_soc_read(codec, WM8731_APDIGI) & 0xfff7;
b36d61d4
FM
269
270 if (mute)
17a52fd6 271 snd_soc_write(codec, WM8731_APDIGI, mute_reg | 0x8);
b36d61d4 272 else
17a52fd6 273 snd_soc_write(codec, WM8731_APDIGI, mute_reg);
b36d61d4
FM
274 return 0;
275}
276
e550e17f 277static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
b36d61d4
FM
278 int clk_id, unsigned int freq, int dir)
279{
280 struct snd_soc_codec *codec = codec_dai->codec;
281 struct wm8731_priv *wm8731 = codec->private_data;
282
283 switch (freq) {
284 case 11289600:
285 case 12000000:
286 case 12288000:
287 case 16934400:
288 case 18432000:
289 wm8731->sysclk = freq;
290 return 0;
291 }
292 return -EINVAL;
293}
294
295
e550e17f 296static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
b36d61d4
FM
297 unsigned int fmt)
298{
299 struct snd_soc_codec *codec = codec_dai->codec;
300 u16 iface = 0;
40e0aa64
RP
301
302 /* set master/slave audio interface */
b36d61d4 303 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
40e0aa64
RP
304 case SND_SOC_DAIFMT_CBM_CFM:
305 iface |= 0x0040;
306 break;
307 case SND_SOC_DAIFMT_CBS_CFS:
308 break;
b36d61d4
FM
309 default:
310 return -EINVAL;
40e0aa64 311 }
40e0aa64
RP
312
313 /* interface format */
b36d61d4 314 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
40e0aa64
RP
315 case SND_SOC_DAIFMT_I2S:
316 iface |= 0x0002;
317 break;
318 case SND_SOC_DAIFMT_RIGHT_J:
319 break;
320 case SND_SOC_DAIFMT_LEFT_J:
321 iface |= 0x0001;
322 break;
323 case SND_SOC_DAIFMT_DSP_A:
324 iface |= 0x0003;
325 break;
326 case SND_SOC_DAIFMT_DSP_B:
327 iface |= 0x0013;
328 break;
b36d61d4
FM
329 default:
330 return -EINVAL;
40e0aa64
RP
331 }
332
333 /* clock inversion */
b36d61d4 334 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
40e0aa64
RP
335 case SND_SOC_DAIFMT_NB_NF:
336 break;
337 case SND_SOC_DAIFMT_IB_IF:
338 iface |= 0x0090;
339 break;
340 case SND_SOC_DAIFMT_IB_NF:
341 iface |= 0x0080;
342 break;
343 case SND_SOC_DAIFMT_NB_IF:
344 iface |= 0x0010;
345 break;
b36d61d4
FM
346 default:
347 return -EINVAL;
40e0aa64
RP
348 }
349
350 /* set iface */
17a52fd6 351 snd_soc_write(codec, WM8731_IFACE, iface);
40e0aa64
RP
352 return 0;
353}
354
0be9898a
MB
355static int wm8731_set_bias_level(struct snd_soc_codec *codec,
356 enum snd_soc_bias_level level)
40e0aa64 357{
22d22ee5 358 u16 reg;
40e0aa64 359
0be9898a
MB
360 switch (level) {
361 case SND_SOC_BIAS_ON:
40e0aa64 362 break;
0be9898a 363 case SND_SOC_BIAS_PREPARE:
40e0aa64 364 break;
0be9898a 365 case SND_SOC_BIAS_STANDBY:
22d22ee5 366 /* Clear PWROFF, gate CLKOUT, everything else as-is */
17a52fd6
MB
367 reg = snd_soc_read(codec, WM8731_PWR) & 0xff7f;
368 snd_soc_write(codec, WM8731_PWR, reg | 0x0040);
40e0aa64 369 break;
0be9898a 370 case SND_SOC_BIAS_OFF:
17a52fd6
MB
371 snd_soc_write(codec, WM8731_ACTIVE, 0x0);
372 snd_soc_write(codec, WM8731_PWR, 0xffff);
40e0aa64
RP
373 break;
374 }
0be9898a 375 codec->bias_level = level;
40e0aa64
RP
376 return 0;
377}
378
b36d61d4
FM
379#define WM8731_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
380 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
381 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
382 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
383 SNDRV_PCM_RATE_96000)
384
385#define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
386 SNDRV_PCM_FMTBIT_S24_LE)
387
6335d055
EM
388static struct snd_soc_dai_ops wm8731_dai_ops = {
389 .prepare = wm8731_pcm_prepare,
390 .hw_params = wm8731_hw_params,
391 .shutdown = wm8731_shutdown,
392 .digital_mute = wm8731_mute,
393 .set_sysclk = wm8731_set_dai_sysclk,
394 .set_fmt = wm8731_set_dai_fmt,
395};
396
e550e17f 397struct snd_soc_dai wm8731_dai = {
40e0aa64
RP
398 .name = "WM8731",
399 .playback = {
400 .stream_name = "Playback",
401 .channels_min = 1,
402 .channels_max = 2,
b36d61d4
FM
403 .rates = WM8731_RATES,
404 .formats = WM8731_FORMATS,},
40e0aa64
RP
405 .capture = {
406 .stream_name = "Capture",
407 .channels_min = 1,
408 .channels_max = 2,
b36d61d4
FM
409 .rates = WM8731_RATES,
410 .formats = WM8731_FORMATS,},
6335d055 411 .ops = &wm8731_dai_ops,
40e0aa64
RP
412};
413EXPORT_SYMBOL_GPL(wm8731_dai);
414
b3b50b3f 415#ifdef CONFIG_PM
40e0aa64
RP
416static int wm8731_suspend(struct platform_device *pdev, pm_message_t state)
417{
418 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
6627a653 419 struct snd_soc_codec *codec = socdev->card->codec;
40e0aa64 420
17a52fd6 421 snd_soc_write(codec, WM8731_ACTIVE, 0x0);
0be9898a 422 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
40e0aa64
RP
423 return 0;
424}
425
426static int wm8731_resume(struct platform_device *pdev)
427{
428 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
6627a653 429 struct snd_soc_codec *codec = socdev->card->codec;
40e0aa64
RP
430 int i;
431 u8 data[2];
432 u16 *cache = codec->reg_cache;
433
434 /* Sync reg_cache with the hardware */
435 for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
436 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
437 data[1] = cache[i] & 0x00ff;
438 codec->hw_write(codec->control_data, data, 2);
439 }
0be9898a
MB
440 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
441 wm8731_set_bias_level(codec, codec->suspend_bias_level);
40e0aa64
RP
442 return 0;
443}
b3b50b3f
MB
444#else
445#define wm8731_suspend NULL
446#define wm8731_resume NULL
447#endif
40e0aa64 448
5998102b 449static int wm8731_probe(struct platform_device *pdev)
40e0aa64 450{
5998102b
MB
451 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
452 struct snd_soc_codec *codec;
453 int ret = 0;
40e0aa64 454
5998102b
MB
455 if (wm8731_codec == NULL) {
456 dev_err(&pdev->dev, "Codec device not registered\n");
457 return -ENODEV;
458 }
40e0aa64 459
5998102b
MB
460 socdev->card->codec = wm8731_codec;
461 codec = wm8731_codec;
40e0aa64
RP
462
463 /* register pcms */
464 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
465 if (ret < 0) {
5998102b 466 dev_err(codec->dev, "failed to create pcms: %d\n", ret);
e35115a5 467 goto pcm_err;
40e0aa64
RP
468 }
469
3e8e1952 470 snd_soc_add_controls(codec, wm8731_snd_controls,
5998102b 471 ARRAY_SIZE(wm8731_snd_controls));
40e0aa64 472 wm8731_add_widgets(codec);
968a6025 473 ret = snd_soc_init_card(socdev);
40e0aa64 474 if (ret < 0) {
5998102b 475 dev_err(codec->dev, "failed to register card: %d\n", ret);
e35115a5 476 goto card_err;
40e0aa64
RP
477 }
478
479 return ret;
e35115a5
LG
480
481card_err:
482 snd_soc_free_pcms(socdev);
483 snd_soc_dapm_free(socdev);
484pcm_err:
40e0aa64
RP
485 return ret;
486}
487
488/* power down chip */
489static int wm8731_remove(struct platform_device *pdev)
490{
491 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
40e0aa64
RP
492
493 snd_soc_free_pcms(socdev);
494 snd_soc_dapm_free(socdev);
40e0aa64
RP
495
496 return 0;
497}
498
499struct snd_soc_codec_device soc_codec_dev_wm8731 = {
500 .probe = wm8731_probe,
501 .remove = wm8731_remove,
502 .suspend = wm8731_suspend,
503 .resume = wm8731_resume,
504};
40e0aa64
RP
505EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731);
506
7084a42b
MB
507static int wm8731_register(struct wm8731_priv *wm8731,
508 enum snd_soc_control_type control)
a8035c8f 509{
a8035c8f 510 int ret;
5998102b 511 struct snd_soc_codec *codec = &wm8731->codec;
a8035c8f 512
5998102b
MB
513 if (wm8731_codec) {
514 dev_err(codec->dev, "Another WM8731 is registered\n");
fe5422fc
MB
515 ret = -EINVAL;
516 goto err;
5998102b 517 }
a8035c8f 518
5998102b
MB
519 mutex_init(&codec->mutex);
520 INIT_LIST_HEAD(&codec->dapm_widgets);
521 INIT_LIST_HEAD(&codec->dapm_paths);
a8035c8f 522
5998102b
MB
523 codec->private_data = wm8731;
524 codec->name = "WM8731";
525 codec->owner = THIS_MODULE;
5998102b
MB
526 codec->bias_level = SND_SOC_BIAS_OFF;
527 codec->set_bias_level = wm8731_set_bias_level;
528 codec->dai = &wm8731_dai;
529 codec->num_dai = 1;
530 codec->reg_cache_size = WM8731_CACHEREGNUM;
531 codec->reg_cache = &wm8731->reg_cache;
532
533 memcpy(codec->reg_cache, wm8731_reg, sizeof(wm8731_reg));
534
7084a42b 535 ret = snd_soc_codec_set_cache_io(codec, 7, 9, control);
17a52fd6
MB
536 if (ret < 0) {
537 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
538 goto err;
539 }
540
519cf2df
MB
541 ret = wm8731_reset(codec);
542 if (ret < 0) {
fe5422fc
MB
543 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
544 goto err;
519cf2df
MB
545 }
546
5998102b
MB
547 wm8731_dai.dev = codec->dev;
548
5998102b
MB
549 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
550
551 /* Latch the update bits */
17a52fd6
MB
552 snd_soc_update_bits(codec, WM8731_LOUT1V, 0x100, 0);
553 snd_soc_update_bits(codec, WM8731_ROUT1V, 0x100, 0);
554 snd_soc_update_bits(codec, WM8731_LINVOL, 0x100, 0);
555 snd_soc_update_bits(codec, WM8731_RINVOL, 0x100, 0);
5998102b 556
ce3bdaa8 557 /* Disable bypass path by default */
17a52fd6 558 snd_soc_update_bits(codec, WM8731_APANA, 0x4, 0);
ce3bdaa8 559
5998102b
MB
560 wm8731_codec = codec;
561
562 ret = snd_soc_register_codec(codec);
563 if (ret != 0) {
564 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
fe5422fc 565 goto err;
5998102b
MB
566 }
567
568 ret = snd_soc_register_dai(&wm8731_dai);
569 if (ret != 0) {
570 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
571 snd_soc_unregister_codec(codec);
fe5422fc 572 goto err_codec;
5998102b 573 }
a8035c8f 574
a8035c8f 575 return 0;
fe5422fc
MB
576
577err_codec:
578 snd_soc_unregister_codec(codec);
579err:
580 kfree(wm8731);
581 return ret;
a8035c8f
MB
582}
583
5998102b
MB
584static void wm8731_unregister(struct wm8731_priv *wm8731)
585{
586 wm8731_set_bias_level(&wm8731->codec, SND_SOC_BIAS_OFF);
587 snd_soc_unregister_dai(&wm8731_dai);
588 snd_soc_unregister_codec(&wm8731->codec);
589 kfree(wm8731);
590 wm8731_codec = NULL;
591}
a8035c8f 592
5998102b 593#if defined(CONFIG_SPI_MASTER)
a8035c8f
MB
594static int wm8731_spi_write(struct spi_device *spi, const char *data, int len)
595{
596 struct spi_transfer t;
597 struct spi_message m;
598 u8 msg[2];
599
600 if (len <= 0)
601 return 0;
602
603 msg[0] = data[0];
604 msg[1] = data[1];
605
606 spi_message_init(&m);
607 memset(&t, 0, (sizeof t));
608
609 t.tx_buf = &msg[0];
610 t.len = len;
611
612 spi_message_add_tail(&t, &m);
613 spi_sync(spi, &m);
614
615 return len;
616}
5998102b
MB
617
618static int __devinit wm8731_spi_probe(struct spi_device *spi)
619{
620 struct snd_soc_codec *codec;
621 struct wm8731_priv *wm8731;
622
623 wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
624 if (wm8731 == NULL)
625 return -ENOMEM;
626
627 codec = &wm8731->codec;
628 codec->control_data = spi;
629 codec->hw_write = (hw_write_t)wm8731_spi_write;
630 codec->dev = &spi->dev;
631
ae31c1fb 632 dev_set_drvdata(&spi->dev, wm8731);
93b760b7 633
7084a42b 634 return wm8731_register(wm8731, SND_SOC_SPI);
5998102b
MB
635}
636
637static int __devexit wm8731_spi_remove(struct spi_device *spi)
638{
ae31c1fb 639 struct wm8731_priv *wm8731 = dev_get_drvdata(&spi->dev);
93b760b7
MB
640
641 wm8731_unregister(wm8731);
642
5998102b
MB
643 return 0;
644}
645
b3b50b3f
MB
646#ifdef CONFIG_PM
647static int wm8731_spi_suspend(struct spi_device *spi, pm_message_t msg)
648{
649 return snd_soc_suspend_device(&spi->dev);
650}
651
652static int wm8731_spi_resume(struct spi_device *spi)
653{
654 return snd_soc_resume_device(&spi->dev);
655}
656#else
657#define wm8731_spi_suspend NULL
658#define wm8731_spi_resume NULL
659#endif
660
5998102b
MB
661static struct spi_driver wm8731_spi_driver = {
662 .driver = {
663 .name = "wm8731",
664 .bus = &spi_bus_type,
665 .owner = THIS_MODULE,
666 },
667 .probe = wm8731_spi_probe,
b3b50b3f
MB
668 .suspend = wm8731_spi_suspend,
669 .resume = wm8731_spi_resume,
5998102b
MB
670 .remove = __devexit_p(wm8731_spi_remove),
671};
a8035c8f
MB
672#endif /* CONFIG_SPI_MASTER */
673
674#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
c6f29811
MB
675static __devinit int wm8731_i2c_probe(struct i2c_client *i2c,
676 const struct i2c_device_id *id)
a8035c8f 677{
5998102b
MB
678 struct wm8731_priv *wm8731;
679 struct snd_soc_codec *codec;
a8035c8f 680
5998102b
MB
681 wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
682 if (wm8731 == NULL)
683 return -ENOMEM;
684
685 codec = &wm8731->codec;
5998102b
MB
686
687 i2c_set_clientdata(i2c, wm8731);
a8035c8f
MB
688 codec->control_data = i2c;
689
5998102b 690 codec->dev = &i2c->dev;
a8035c8f 691
7084a42b 692 return wm8731_register(wm8731, SND_SOC_I2C);
a8035c8f
MB
693}
694
c6f29811 695static __devexit int wm8731_i2c_remove(struct i2c_client *client)
a8035c8f 696{
5998102b
MB
697 struct wm8731_priv *wm8731 = i2c_get_clientdata(client);
698 wm8731_unregister(wm8731);
a8035c8f
MB
699 return 0;
700}
701
b3b50b3f
MB
702#ifdef CONFIG_PM
703static int wm8731_i2c_suspend(struct i2c_client *i2c, pm_message_t msg)
704{
705 return snd_soc_suspend_device(&i2c->dev);
706}
707
708static int wm8731_i2c_resume(struct i2c_client *i2c)
709{
710 return snd_soc_resume_device(&i2c->dev);
711}
712#else
713#define wm8731_i2c_suspend NULL
714#define wm8731_i2c_resume NULL
715#endif
716
a8035c8f
MB
717static const struct i2c_device_id wm8731_i2c_id[] = {
718 { "wm8731", 0 },
719 { }
720};
721MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
722
723static struct i2c_driver wm8731_i2c_driver = {
724 .driver = {
725 .name = "WM8731 I2C Codec",
726 .owner = THIS_MODULE,
727 },
728 .probe = wm8731_i2c_probe,
c6f29811 729 .remove = __devexit_p(wm8731_i2c_remove),
b3b50b3f
MB
730 .suspend = wm8731_i2c_suspend,
731 .resume = wm8731_i2c_resume,
a8035c8f
MB
732 .id_table = wm8731_i2c_id,
733};
734#endif
735
c9b3a40f 736static int __init wm8731_modinit(void)
64089b84 737{
5998102b
MB
738 int ret;
739#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
740 ret = i2c_add_driver(&wm8731_i2c_driver);
741 if (ret != 0) {
742 printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n",
743 ret);
744 }
745#endif
746#if defined(CONFIG_SPI_MASTER)
747 ret = spi_register_driver(&wm8731_spi_driver);
748 if (ret != 0) {
749 printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n",
750 ret);
751 }
752#endif
753 return 0;
64089b84
MB
754}
755module_init(wm8731_modinit);
756
757static void __exit wm8731_exit(void)
758{
5998102b
MB
759#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
760 i2c_del_driver(&wm8731_i2c_driver);
761#endif
762#if defined(CONFIG_SPI_MASTER)
763 spi_unregister_driver(&wm8731_spi_driver);
764#endif
64089b84
MB
765}
766module_exit(wm8731_exit);
767
40e0aa64
RP
768MODULE_DESCRIPTION("ASoC WM8731 driver");
769MODULE_AUTHOR("Richard Purdie");
770MODULE_LICENSE("GPL");
This page took 0.255259 seconds and 5 git commands to generate.