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