blob: 96d6e1aeaf430f0c99bb804e4b7d4ea1eb0bc2fc [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
Frank Mandarinob36d61d2007-02-02 17:14:56 +010032#define WM8731_VERSION "0.13"
Richard Purdie40e0aa62006-10-06 18:36:07 +020033
Richard Purdie40e0aa62006-10-06 18:36:07 +020034struct snd_soc_codec_device soc_codec_dev_wm8731;
35
Frank Mandarinob36d61d2007-02-02 17:14:56 +010036/* codec private data */
37struct wm8731_priv {
38 unsigned int sysclk;
39};
40
Richard Purdie40e0aa62006-10-06 18:36:07 +020041/*
42 * wm8731 register cache
43 * We can't read the WM8731 register space when we are
44 * using 2 wire for device control, so we cache them instead.
45 * There is no point in caching the reset register
46 */
47static const u16 wm8731_reg[WM8731_CACHEREGNUM] = {
48 0x0097, 0x0097, 0x0079, 0x0079,
49 0x000a, 0x0008, 0x009f, 0x000a,
50 0x0000, 0x0000
51};
52
Richard Purdie40e0aa62006-10-06 18:36:07 +020053/*
54 * read wm8731 register cache
55 */
56static inline unsigned int wm8731_read_reg_cache(struct snd_soc_codec *codec,
57 unsigned int reg)
58{
59 u16 *cache = codec->reg_cache;
60 if (reg == WM8731_RESET)
61 return 0;
62 if (reg >= WM8731_CACHEREGNUM)
63 return -1;
64 return cache[reg];
65}
66
67/*
68 * write wm8731 register cache
69 */
70static inline void wm8731_write_reg_cache(struct snd_soc_codec *codec,
71 u16 reg, unsigned int value)
72{
73 u16 *cache = codec->reg_cache;
74 if (reg >= WM8731_CACHEREGNUM)
75 return;
76 cache[reg] = value;
77}
78
79/*
80 * write to the WM8731 register space
81 */
82static int wm8731_write(struct snd_soc_codec *codec, unsigned int reg,
83 unsigned int value)
84{
85 u8 data[2];
86
87 /* data is
88 * D15..D9 WM8731 register offset
89 * D8...D0 register data
90 */
91 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
92 data[1] = value & 0x00ff;
93
Mark Brownd454aee2008-04-23 15:16:46 +020094 wm8731_write_reg_cache(codec, reg, value);
Richard Purdie40e0aa62006-10-06 18:36:07 +020095 if (codec->hw_write(codec->control_data, data, 2) == 2)
96 return 0;
97 else
98 return -EIO;
99}
100
101#define wm8731_reset(c) wm8731_write(c, WM8731_RESET, 0)
102
103static const char *wm8731_input_select[] = {"Line In", "Mic"};
104static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
105
106static const struct soc_enum wm8731_enum[] = {
107 SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select),
108 SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph),
109};
110
111static const struct snd_kcontrol_new wm8731_snd_controls[] = {
112
Liam Girdwoodbd903b62006-11-09 16:35:01 +0100113SOC_DOUBLE_R("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
114 0, 127, 0),
115SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
116 7, 1, 0),
Richard Purdie40e0aa62006-10-06 18:36:07 +0200117
118SOC_DOUBLE_R("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0),
119SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
120
121SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0),
122SOC_SINGLE("Capture Mic Switch", WM8731_APANA, 1, 1, 1),
123
124SOC_SINGLE("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1),
125
126SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
127SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
128
129SOC_ENUM("Playback De-emphasis", wm8731_enum[1]),
130};
131
Richard Purdie40e0aa62006-10-06 18:36:07 +0200132/* Output Mixer */
133static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
134SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
135SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
136SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
137};
138
139/* Input mux */
140static const struct snd_kcontrol_new wm8731_input_mux_controls =
141SOC_DAPM_ENUM("Input Select", wm8731_enum[0]);
142
143static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
144SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
145 &wm8731_output_mixer_controls[0],
146 ARRAY_SIZE(wm8731_output_mixer_controls)),
147SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
148SND_SOC_DAPM_OUTPUT("LOUT"),
149SND_SOC_DAPM_OUTPUT("LHPOUT"),
150SND_SOC_DAPM_OUTPUT("ROUT"),
151SND_SOC_DAPM_OUTPUT("RHPOUT"),
152SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
153SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
154SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
155SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
156SND_SOC_DAPM_INPUT("MICIN"),
157SND_SOC_DAPM_INPUT("RLINEIN"),
158SND_SOC_DAPM_INPUT("LLINEIN"),
159};
160
Mark Browna65f0562008-05-13 14:54:43 +0200161static const struct snd_soc_dapm_route intercon[] = {
Richard Purdie40e0aa62006-10-06 18:36:07 +0200162 /* output mixer */
163 {"Output Mixer", "Line Bypass Switch", "Line Input"},
164 {"Output Mixer", "HiFi Playback Switch", "DAC"},
165 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
166
167 /* outputs */
168 {"RHPOUT", NULL, "Output Mixer"},
169 {"ROUT", NULL, "Output Mixer"},
170 {"LHPOUT", NULL, "Output Mixer"},
171 {"LOUT", NULL, "Output Mixer"},
172
173 /* input mux */
174 {"Input Mux", "Line In", "Line Input"},
175 {"Input Mux", "Mic", "Mic Bias"},
176 {"ADC", NULL, "Input Mux"},
177
178 /* inputs */
179 {"Line Input", NULL, "LLINEIN"},
180 {"Line Input", NULL, "RLINEIN"},
181 {"Mic Bias", NULL, "MICIN"},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200182};
183
184static int wm8731_add_widgets(struct snd_soc_codec *codec)
185{
Mark Browna65f0562008-05-13 14:54:43 +0200186 snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
187 ARRAY_SIZE(wm8731_dapm_widgets));
Richard Purdie40e0aa62006-10-06 18:36:07 +0200188
Mark Browna65f0562008-05-13 14:54:43 +0200189 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
Richard Purdie40e0aa62006-10-06 18:36:07 +0200190
191 snd_soc_dapm_new_widgets(codec);
192 return 0;
193}
194
195struct _coeff_div {
196 u32 mclk;
197 u32 rate;
198 u16 fs;
199 u8 sr:4;
200 u8 bosr:1;
201 u8 usb:1;
202};
203
204/* codec mclk clock divider coefficients */
205static const struct _coeff_div coeff_div[] = {
206 /* 48k */
207 {12288000, 48000, 256, 0x0, 0x0, 0x0},
208 {18432000, 48000, 384, 0x0, 0x1, 0x0},
209 {12000000, 48000, 250, 0x0, 0x0, 0x1},
210
211 /* 32k */
212 {12288000, 32000, 384, 0x6, 0x0, 0x0},
213 {18432000, 32000, 576, 0x6, 0x1, 0x0},
Frank Mandarino298a2c72007-01-31 10:02:56 +0100214 {12000000, 32000, 375, 0x6, 0x0, 0x1},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200215
216 /* 8k */
217 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
218 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
219 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
220 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
221 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
222
223 /* 96k */
224 {12288000, 96000, 128, 0x7, 0x0, 0x0},
225 {18432000, 96000, 192, 0x7, 0x1, 0x0},
226 {12000000, 96000, 125, 0x7, 0x0, 0x1},
227
228 /* 44.1k */
229 {11289600, 44100, 256, 0x8, 0x0, 0x0},
230 {16934400, 44100, 384, 0x8, 0x1, 0x0},
231 {12000000, 44100, 272, 0x8, 0x1, 0x1},
232
233 /* 88.2k */
234 {11289600, 88200, 128, 0xf, 0x0, 0x0},
235 {16934400, 88200, 192, 0xf, 0x1, 0x0},
236 {12000000, 88200, 136, 0xf, 0x1, 0x1},
237};
238
239static inline int get_coeff(int mclk, int rate)
240{
241 int i;
242
243 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
244 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
245 return i;
246 }
247 return 0;
248}
249
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100250static int wm8731_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000251 struct snd_pcm_hw_params *params,
252 struct snd_soc_dai *dai)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200253{
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100254 struct snd_soc_pcm_runtime *rtd = substream->private_data;
255 struct snd_soc_device *socdev = rtd->socdev;
256 struct snd_soc_codec *codec = socdev->codec;
257 struct wm8731_priv *wm8731 = codec->private_data;
258 u16 iface = wm8731_read_reg_cache(codec, WM8731_IFACE) & 0xfff3;
259 int i = get_coeff(wm8731->sysclk, params_rate(params));
260 u16 srate = (coeff_div[i].sr << 2) |
261 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200262
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100263 wm8731_write(codec, WM8731_SRATE, srate);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200264
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100265 /* bit size */
266 switch (params_format(params)) {
267 case SNDRV_PCM_FORMAT_S16_LE:
268 break;
269 case SNDRV_PCM_FORMAT_S20_3LE:
270 iface |= 0x0004;
271 break;
272 case SNDRV_PCM_FORMAT_S24_LE:
273 iface |= 0x0008;
274 break;
275 }
276
277 wm8731_write(codec, WM8731_IFACE, iface);
278 return 0;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200279}
280
Mark Browndee89c42008-11-18 22:11:38 +0000281static int wm8731_pcm_prepare(struct snd_pcm_substream *substream,
282 struct snd_soc_dai *dai)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200283{
284 struct snd_soc_pcm_runtime *rtd = substream->private_data;
285 struct snd_soc_device *socdev = rtd->socdev;
286 struct snd_soc_codec *codec = socdev->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200287
288 /* set active */
289 wm8731_write(codec, WM8731_ACTIVE, 0x0001);
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100290
Richard Purdie40e0aa62006-10-06 18:36:07 +0200291 return 0;
292}
293
Mark Browndee89c42008-11-18 22:11:38 +0000294static void wm8731_shutdown(struct snd_pcm_substream *substream,
295 struct snd_soc_dai *dai)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200296{
297 struct snd_soc_pcm_runtime *rtd = substream->private_data;
298 struct snd_soc_device *socdev = rtd->socdev;
299 struct snd_soc_codec *codec = socdev->codec;
300
301 /* deactivate */
302 if (!codec->active) {
303 udelay(50);
304 wm8731_write(codec, WM8731_ACTIVE, 0x0);
305 }
306}
307
Liam Girdwoode550e172008-07-07 16:07:52 +0100308static int wm8731_mute(struct snd_soc_dai *dai, int mute)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200309{
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100310 struct snd_soc_codec *codec = dai->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200311 u16 mute_reg = wm8731_read_reg_cache(codec, WM8731_APDIGI) & 0xfff7;
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100312
Richard Purdie40e0aa62006-10-06 18:36:07 +0200313 if (mute)
314 wm8731_write(codec, WM8731_APDIGI, mute_reg | 0x8);
315 else
316 wm8731_write(codec, WM8731_APDIGI, mute_reg);
317 return 0;
318}
319
Liam Girdwoode550e172008-07-07 16:07:52 +0100320static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100321 int clk_id, unsigned int freq, int dir)
322{
323 struct snd_soc_codec *codec = codec_dai->codec;
324 struct wm8731_priv *wm8731 = codec->private_data;
325
326 switch (freq) {
327 case 11289600:
328 case 12000000:
329 case 12288000:
330 case 16934400:
331 case 18432000:
332 wm8731->sysclk = freq;
333 return 0;
334 }
335 return -EINVAL;
336}
337
338
Liam Girdwoode550e172008-07-07 16:07:52 +0100339static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100340 unsigned int fmt)
341{
342 struct snd_soc_codec *codec = codec_dai->codec;
343 u16 iface = 0;
344
345 /* set master/slave audio interface */
346 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
347 case SND_SOC_DAIFMT_CBM_CFM:
348 iface |= 0x0040;
349 break;
350 case SND_SOC_DAIFMT_CBS_CFS:
351 break;
352 default:
353 return -EINVAL;
354 }
355
356 /* interface format */
357 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
358 case SND_SOC_DAIFMT_I2S:
359 iface |= 0x0002;
360 break;
361 case SND_SOC_DAIFMT_RIGHT_J:
362 break;
363 case SND_SOC_DAIFMT_LEFT_J:
364 iface |= 0x0001;
365 break;
366 case SND_SOC_DAIFMT_DSP_A:
367 iface |= 0x0003;
368 break;
369 case SND_SOC_DAIFMT_DSP_B:
370 iface |= 0x0013;
371 break;
372 default:
373 return -EINVAL;
374 }
375
376 /* clock inversion */
377 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
378 case SND_SOC_DAIFMT_NB_NF:
379 break;
380 case SND_SOC_DAIFMT_IB_IF:
381 iface |= 0x0090;
382 break;
383 case SND_SOC_DAIFMT_IB_NF:
384 iface |= 0x0080;
385 break;
386 case SND_SOC_DAIFMT_NB_IF:
387 iface |= 0x0010;
388 break;
389 default:
390 return -EINVAL;
391 }
392
393 /* set iface */
394 wm8731_write(codec, WM8731_IFACE, iface);
395 return 0;
396}
397
Mark Brown0be98982008-05-19 12:31:28 +0200398static int wm8731_set_bias_level(struct snd_soc_codec *codec,
399 enum snd_soc_bias_level level)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200400{
401 u16 reg = wm8731_read_reg_cache(codec, WM8731_PWR) & 0xff7f;
402
Mark Brown0be98982008-05-19 12:31:28 +0200403 switch (level) {
404 case SND_SOC_BIAS_ON:
Richard Purdie40e0aa62006-10-06 18:36:07 +0200405 /* vref/mid, osc on, dac unmute */
406 wm8731_write(codec, WM8731_PWR, reg);
407 break;
Mark Brown0be98982008-05-19 12:31:28 +0200408 case SND_SOC_BIAS_PREPARE:
Richard Purdie40e0aa62006-10-06 18:36:07 +0200409 break;
Mark Brown0be98982008-05-19 12:31:28 +0200410 case SND_SOC_BIAS_STANDBY:
Richard Purdie40e0aa62006-10-06 18:36:07 +0200411 /* everything off except vref/vmid, */
412 wm8731_write(codec, WM8731_PWR, reg | 0x0040);
413 break;
Mark Brown0be98982008-05-19 12:31:28 +0200414 case SND_SOC_BIAS_OFF:
Richard Purdie40e0aa62006-10-06 18:36:07 +0200415 /* everything off, dac mute, inactive */
416 wm8731_write(codec, WM8731_ACTIVE, 0x0);
417 wm8731_write(codec, WM8731_PWR, 0xffff);
418 break;
419 }
Mark Brown0be98982008-05-19 12:31:28 +0200420 codec->bias_level = level;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200421 return 0;
422}
423
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100424#define WM8731_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
425 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
426 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
427 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
428 SNDRV_PCM_RATE_96000)
429
430#define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
431 SNDRV_PCM_FMTBIT_S24_LE)
432
Liam Girdwoode550e172008-07-07 16:07:52 +0100433struct snd_soc_dai wm8731_dai = {
Richard Purdie40e0aa62006-10-06 18:36:07 +0200434 .name = "WM8731",
435 .playback = {
436 .stream_name = "Playback",
437 .channels_min = 1,
438 .channels_max = 2,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100439 .rates = WM8731_RATES,
440 .formats = WM8731_FORMATS,},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200441 .capture = {
442 .stream_name = "Capture",
443 .channels_min = 1,
444 .channels_max = 2,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100445 .rates = WM8731_RATES,
446 .formats = WM8731_FORMATS,},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200447 .ops = {
448 .prepare = wm8731_pcm_prepare,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100449 .hw_params = wm8731_hw_params,
Richard Purdie40e0aa62006-10-06 18:36:07 +0200450 .shutdown = wm8731_shutdown,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100451 .digital_mute = wm8731_mute,
452 .set_sysclk = wm8731_set_dai_sysclk,
453 .set_fmt = wm8731_set_dai_fmt,
454 }
Richard Purdie40e0aa62006-10-06 18:36:07 +0200455};
456EXPORT_SYMBOL_GPL(wm8731_dai);
457
458static int wm8731_suspend(struct platform_device *pdev, pm_message_t state)
459{
460 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
461 struct snd_soc_codec *codec = socdev->codec;
462
463 wm8731_write(codec, WM8731_ACTIVE, 0x0);
Mark Brown0be98982008-05-19 12:31:28 +0200464 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200465 return 0;
466}
467
468static int wm8731_resume(struct platform_device *pdev)
469{
470 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
471 struct snd_soc_codec *codec = socdev->codec;
472 int i;
473 u8 data[2];
474 u16 *cache = codec->reg_cache;
475
476 /* Sync reg_cache with the hardware */
477 for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
478 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
479 data[1] = cache[i] & 0x00ff;
480 codec->hw_write(codec->control_data, data, 2);
481 }
Mark Brown0be98982008-05-19 12:31:28 +0200482 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
483 wm8731_set_bias_level(codec, codec->suspend_bias_level);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200484 return 0;
485}
486
487/*
488 * initialise the WM8731 driver
489 * register the mixer and dsp interfaces with the kernel
490 */
491static int wm8731_init(struct snd_soc_device *socdev)
492{
493 struct snd_soc_codec *codec = socdev->codec;
494 int reg, ret = 0;
495
496 codec->name = "WM8731";
497 codec->owner = THIS_MODULE;
498 codec->read = wm8731_read_reg_cache;
499 codec->write = wm8731_write;
Mark Brown0be98982008-05-19 12:31:28 +0200500 codec->set_bias_level = wm8731_set_bias_level;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200501 codec->dai = &wm8731_dai;
502 codec->num_dai = 1;
Mark Brownd751b232008-06-11 13:47:06 +0100503 codec->reg_cache_size = ARRAY_SIZE(wm8731_reg);
Takashi Iwai88cb4292007-02-05 14:56:20 +0100504 codec->reg_cache = kmemdup(wm8731_reg, sizeof(wm8731_reg), GFP_KERNEL);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200505 if (codec->reg_cache == NULL)
506 return -ENOMEM;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200507
508 wm8731_reset(codec);
509
510 /* register pcms */
511 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
512 if (ret < 0) {
Liam Girdwoode35115a2007-01-31 10:02:23 +0100513 printk(KERN_ERR "wm8731: failed to create pcms\n");
514 goto pcm_err;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200515 }
516
517 /* power on device */
Mark Brown0be98982008-05-19 12:31:28 +0200518 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200519
520 /* set the update bits */
521 reg = wm8731_read_reg_cache(codec, WM8731_LOUT1V);
Ville Syrjala389619f2007-11-26 08:58:24 +0100522 wm8731_write(codec, WM8731_LOUT1V, reg & ~0x0100);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200523 reg = wm8731_read_reg_cache(codec, WM8731_ROUT1V);
Ville Syrjala389619f2007-11-26 08:58:24 +0100524 wm8731_write(codec, WM8731_ROUT1V, reg & ~0x0100);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200525 reg = wm8731_read_reg_cache(codec, WM8731_LINVOL);
Ville Syrjala389619f2007-11-26 08:58:24 +0100526 wm8731_write(codec, WM8731_LINVOL, reg & ~0x0100);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200527 reg = wm8731_read_reg_cache(codec, WM8731_RINVOL);
Ville Syrjala389619f2007-11-26 08:58:24 +0100528 wm8731_write(codec, WM8731_RINVOL, reg & ~0x0100);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200529
Ian Molton3e8e1952009-01-09 00:23:21 +0000530 snd_soc_add_controls(codec, wm8731_snd_controls,
531 ARRAY_SIZE(wm8731_snd_controls));
Richard Purdie40e0aa62006-10-06 18:36:07 +0200532 wm8731_add_widgets(codec);
Mark Brown968a6022008-11-28 11:49:07 +0000533 ret = snd_soc_init_card(socdev);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200534 if (ret < 0) {
Liam Girdwoode35115a2007-01-31 10:02:23 +0100535 printk(KERN_ERR "wm8731: failed to register card\n");
536 goto card_err;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200537 }
538
539 return ret;
Liam Girdwoode35115a2007-01-31 10:02:23 +0100540
541card_err:
542 snd_soc_free_pcms(socdev);
543 snd_soc_dapm_free(socdev);
544pcm_err:
545 kfree(codec->reg_cache);
546 return ret;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200547}
548
549static struct snd_soc_device *wm8731_socdev;
550
Mark Brownd454aee2008-04-23 15:16:46 +0200551#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200552
553/*
554 * WM8731 2 wire address is determined by GPIO5
555 * state during powerup.
556 * low = 0x1a
557 * high = 0x1b
558 */
Richard Purdie40e0aa62006-10-06 18:36:07 +0200559
Jean Delvare81297c82008-09-01 18:47:01 +0100560static int wm8731_i2c_probe(struct i2c_client *i2c,
561 const struct i2c_device_id *id)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200562{
563 struct snd_soc_device *socdev = wm8731_socdev;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200564 struct snd_soc_codec *codec = socdev->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200565 int ret;
566
Richard Purdie40e0aa62006-10-06 18:36:07 +0200567 i2c_set_clientdata(i2c, codec);
568 codec->control_data = i2c;
569
Richard Purdie40e0aa62006-10-06 18:36:07 +0200570 ret = wm8731_init(socdev);
Jean Delvare81297c82008-09-01 18:47:01 +0100571 if (ret < 0)
Mark Browna5c95e92008-06-23 14:51:29 +0100572 pr_err("failed to initialise WM8731\n");
Richard Purdie40e0aa62006-10-06 18:36:07 +0200573
Richard Purdie40e0aa62006-10-06 18:36:07 +0200574 return ret;
575}
576
Jean Delvare81297c82008-09-01 18:47:01 +0100577static int wm8731_i2c_remove(struct i2c_client *client)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200578{
Mark Brownd454aee2008-04-23 15:16:46 +0200579 struct snd_soc_codec *codec = i2c_get_clientdata(client);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200580 kfree(codec->reg_cache);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200581 return 0;
582}
583
Jean Delvare81297c82008-09-01 18:47:01 +0100584static const struct i2c_device_id wm8731_i2c_id[] = {
585 { "wm8731", 0 },
586 { }
587};
588MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200589
Richard Purdie40e0aa62006-10-06 18:36:07 +0200590static struct i2c_driver wm8731_i2c_driver = {
591 .driver = {
592 .name = "WM8731 I2C Codec",
593 .owner = THIS_MODULE,
594 },
Jean Delvare81297c82008-09-01 18:47:01 +0100595 .probe = wm8731_i2c_probe,
596 .remove = wm8731_i2c_remove,
597 .id_table = wm8731_i2c_id,
Richard Purdie40e0aa62006-10-06 18:36:07 +0200598};
599
Jean Delvare81297c82008-09-01 18:47:01 +0100600static int wm8731_add_i2c_device(struct platform_device *pdev,
601 const struct wm8731_setup_data *setup)
602{
603 struct i2c_board_info info;
604 struct i2c_adapter *adapter;
605 struct i2c_client *client;
606 int ret;
607
608 ret = i2c_add_driver(&wm8731_i2c_driver);
609 if (ret != 0) {
610 dev_err(&pdev->dev, "can't add i2c driver\n");
611 return ret;
612 }
613
614 memset(&info, 0, sizeof(struct i2c_board_info));
615 info.addr = setup->i2c_address;
616 strlcpy(info.type, "wm8731", I2C_NAME_SIZE);
617
618 adapter = i2c_get_adapter(setup->i2c_bus);
619 if (!adapter) {
620 dev_err(&pdev->dev, "can't get i2c adapter %d\n",
621 setup->i2c_bus);
622 goto err_driver;
623 }
624
625 client = i2c_new_device(adapter, &info);
626 i2c_put_adapter(adapter);
627 if (!client) {
628 dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
629 (unsigned int)info.addr);
630 goto err_driver;
631 }
632
633 return 0;
634
635err_driver:
636 i2c_del_driver(&wm8731_i2c_driver);
637 return -ENODEV;
638}
Richard Purdie40e0aa62006-10-06 18:36:07 +0200639#endif
640
Cliff Caid2a40352008-09-01 18:47:03 +0100641#if defined(CONFIG_SPI_MASTER)
642static int __devinit wm8731_spi_probe(struct spi_device *spi)
643{
644 struct snd_soc_device *socdev = wm8731_socdev;
645 struct snd_soc_codec *codec = socdev->codec;
646 int ret;
647
648 codec->control_data = spi;
649
650 ret = wm8731_init(socdev);
651 if (ret < 0)
652 dev_err(&spi->dev, "failed to initialise WM8731\n");
653
654 return ret;
655}
656
657static int __devexit wm8731_spi_remove(struct spi_device *spi)
658{
659 return 0;
660}
661
662static struct spi_driver wm8731_spi_driver = {
663 .driver = {
664 .name = "wm8731",
665 .bus = &spi_bus_type,
666 .owner = THIS_MODULE,
667 },
668 .probe = wm8731_spi_probe,
669 .remove = __devexit_p(wm8731_spi_remove),
670};
671
672static int wm8731_spi_write(struct spi_device *spi, const char *data, int len)
673{
674 struct spi_transfer t;
675 struct spi_message m;
Alan Horstmann8569be32008-09-09 19:25:49 +0100676 u8 msg[2];
Cliff Caid2a40352008-09-01 18:47:03 +0100677
678 if (len <= 0)
679 return 0;
680
Alan Horstmann8569be32008-09-09 19:25:49 +0100681 msg[0] = data[0];
682 msg[1] = data[1];
Cliff Caid2a40352008-09-01 18:47:03 +0100683
684 spi_message_init(&m);
685 memset(&t, 0, (sizeof t));
686
687 t.tx_buf = &msg[0];
688 t.len = len;
689
690 spi_message_add_tail(&t, &m);
691 spi_sync(spi, &m);
692
693 return len;
694}
695#endif /* CONFIG_SPI_MASTER */
696
Richard Purdie40e0aa62006-10-06 18:36:07 +0200697static int wm8731_probe(struct platform_device *pdev)
698{
699 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
700 struct wm8731_setup_data *setup;
701 struct snd_soc_codec *codec;
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100702 struct wm8731_priv *wm8731;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200703 int ret = 0;
704
Mark Browna5c95e92008-06-23 14:51:29 +0100705 pr_info("WM8731 Audio Codec %s", WM8731_VERSION);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200706
707 setup = socdev->codec_data;
708 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
709 if (codec == NULL)
710 return -ENOMEM;
711
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100712 wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
713 if (wm8731 == NULL) {
714 kfree(codec);
715 return -ENOMEM;
716 }
717
718 codec->private_data = wm8731;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200719 socdev->codec = codec;
720 mutex_init(&codec->mutex);
721 INIT_LIST_HEAD(&codec->dapm_widgets);
722 INIT_LIST_HEAD(&codec->dapm_paths);
723
724 wm8731_socdev = socdev;
Cliff Caid2a40352008-09-01 18:47:03 +0100725 ret = -ENODEV;
726
Mark Brownd454aee2008-04-23 15:16:46 +0200727#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200728 if (setup->i2c_address) {
Richard Purdie40e0aa62006-10-06 18:36:07 +0200729 codec->hw_write = (hw_write_t)i2c_master_send;
Jean Delvare81297c82008-09-01 18:47:01 +0100730 ret = wm8731_add_i2c_device(pdev, setup);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200731 }
Cliff Caid2a40352008-09-01 18:47:03 +0100732#endif
733#if defined(CONFIG_SPI_MASTER)
734 if (setup->spi) {
735 codec->hw_write = (hw_write_t)wm8731_spi_write;
736 ret = spi_register_driver(&wm8731_spi_driver);
737 if (ret != 0)
738 printk(KERN_ERR "can't add spi driver");
739 }
Richard Purdie40e0aa62006-10-06 18:36:07 +0200740#endif
Jean Delvare3051e412008-08-25 11:49:20 +0100741
742 if (ret != 0) {
743 kfree(codec->private_data);
744 kfree(codec);
745 }
Richard Purdie40e0aa62006-10-06 18:36:07 +0200746 return ret;
747}
748
749/* power down chip */
750static int wm8731_remove(struct platform_device *pdev)
751{
752 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
753 struct snd_soc_codec *codec = socdev->codec;
754
755 if (codec->control_data)
Mark Brown0be98982008-05-19 12:31:28 +0200756 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200757
758 snd_soc_free_pcms(socdev);
759 snd_soc_dapm_free(socdev);
Mark Brownd454aee2008-04-23 15:16:46 +0200760#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Jean Delvare81297c82008-09-01 18:47:01 +0100761 i2c_unregister_device(codec->control_data);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200762 i2c_del_driver(&wm8731_i2c_driver);
763#endif
Cliff Caid2a40352008-09-01 18:47:03 +0100764#if defined(CONFIG_SPI_MASTER)
765 spi_unregister_driver(&wm8731_spi_driver);
766#endif
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100767 kfree(codec->private_data);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200768 kfree(codec);
769
770 return 0;
771}
772
773struct snd_soc_codec_device soc_codec_dev_wm8731 = {
774 .probe = wm8731_probe,
775 .remove = wm8731_remove,
776 .suspend = wm8731_suspend,
777 .resume = wm8731_resume,
778};
Richard Purdie40e0aa62006-10-06 18:36:07 +0200779EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731);
780
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100781static int __init wm8731_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000782{
783 return snd_soc_register_dai(&wm8731_dai);
784}
785module_init(wm8731_modinit);
786
787static void __exit wm8731_exit(void)
788{
789 snd_soc_unregister_dai(&wm8731_dai);
790}
791module_exit(wm8731_exit);
792
Richard Purdie40e0aa62006-10-06 18:36:07 +0200793MODULE_DESCRIPTION("ASoC WM8731 driver");
794MODULE_AUTHOR("Richard Purdie");
795MODULE_LICENSE("GPL");