blob: c6db67793777913a2f80096b0b7a196003425199 [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
Richard Purdie40e0aa62006-10-06 18:36:07 +020039/*
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
Richard Purdie40e0aa62006-10-06 18:36:07 +020051/*
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
Mark Brownd454aee2008-04-23 15:16:46 +020092 wm8731_write_reg_cache(codec, reg, value);
Richard Purdie40e0aa62006-10-06 18:36:07 +020093 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
Liam Girdwoodbd903b62006-11-09 16:35:01 +0100111SOC_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),
Richard Purdie40e0aa62006-10-06 18:36:07 +0200115
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
Richard Purdie40e0aa62006-10-06 18:36:07 +0200130/* 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
Mark Browna65f0562008-05-13 14:54:43 +0200159static const struct snd_soc_dapm_route intercon[] = {
Richard Purdie40e0aa62006-10-06 18:36:07 +0200160 /* 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"},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200180};
181
182static int wm8731_add_widgets(struct snd_soc_codec *codec)
183{
Mark Browna65f0562008-05-13 14:54:43 +0200184 snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
185 ARRAY_SIZE(wm8731_dapm_widgets));
Richard Purdie40e0aa62006-10-06 18:36:07 +0200186
Mark Browna65f0562008-05-13 14:54:43 +0200187 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
Richard Purdie40e0aa62006-10-06 18:36:07 +0200188
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},
Frank Mandarino298a2c72007-01-31 10:02:56 +0100212 {12000000, 32000, 375, 0x6, 0x0, 0x1},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200213
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
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100248static int wm8731_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000249 struct snd_pcm_hw_params *params,
250 struct snd_soc_dai *dai)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200251{
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100252 struct snd_soc_pcm_runtime *rtd = substream->private_data;
253 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000254 struct snd_soc_codec *codec = socdev->card->codec;
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100255 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;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200260
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100261 wm8731_write(codec, WM8731_SRATE, srate);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200262
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100263 /* 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 }
274
275 wm8731_write(codec, WM8731_IFACE, iface);
276 return 0;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200277}
278
Mark Browndee89c42008-11-18 22:11:38 +0000279static int wm8731_pcm_prepare(struct snd_pcm_substream *substream,
280 struct snd_soc_dai *dai)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200281{
282 struct snd_soc_pcm_runtime *rtd = substream->private_data;
283 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000284 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200285
286 /* set active */
287 wm8731_write(codec, WM8731_ACTIVE, 0x0001);
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100288
Richard Purdie40e0aa62006-10-06 18:36:07 +0200289 return 0;
290}
291
Mark Browndee89c42008-11-18 22:11:38 +0000292static void wm8731_shutdown(struct snd_pcm_substream *substream,
293 struct snd_soc_dai *dai)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200294{
295 struct snd_soc_pcm_runtime *rtd = substream->private_data;
296 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000297 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200298
299 /* deactivate */
300 if (!codec->active) {
301 udelay(50);
302 wm8731_write(codec, WM8731_ACTIVE, 0x0);
303 }
304}
305
Liam Girdwoode550e172008-07-07 16:07:52 +0100306static int wm8731_mute(struct snd_soc_dai *dai, int mute)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200307{
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100308 struct snd_soc_codec *codec = dai->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200309 u16 mute_reg = wm8731_read_reg_cache(codec, WM8731_APDIGI) & 0xfff7;
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100310
Richard Purdie40e0aa62006-10-06 18:36:07 +0200311 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
Liam Girdwoode550e172008-07-07 16:07:52 +0100318static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100319 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
Liam Girdwoode550e172008-07-07 16:07:52 +0100337static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100338 unsigned int fmt)
339{
340 struct snd_soc_codec *codec = codec_dai->codec;
341 u16 iface = 0;
342
343 /* set master/slave audio interface */
344 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
345 case SND_SOC_DAIFMT_CBM_CFM:
346 iface |= 0x0040;
347 break;
348 case SND_SOC_DAIFMT_CBS_CFS:
349 break;
350 default:
351 return -EINVAL;
352 }
353
354 /* interface format */
355 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
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;
370 default:
371 return -EINVAL;
372 }
373
374 /* clock inversion */
375 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
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;
387 default:
388 return -EINVAL;
389 }
390
391 /* set iface */
392 wm8731_write(codec, WM8731_IFACE, iface);
393 return 0;
394}
395
Mark Brown0be98982008-05-19 12:31:28 +0200396static int wm8731_set_bias_level(struct snd_soc_codec *codec,
397 enum snd_soc_bias_level level)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200398{
Mark Brown22d22ee2009-02-16 19:20:15 +0000399 u16 reg;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200400
Mark Brown0be98982008-05-19 12:31:28 +0200401 switch (level) {
402 case SND_SOC_BIAS_ON:
Richard Purdie40e0aa62006-10-06 18:36:07 +0200403 break;
Mark Brown0be98982008-05-19 12:31:28 +0200404 case SND_SOC_BIAS_PREPARE:
Richard Purdie40e0aa62006-10-06 18:36:07 +0200405 break;
Mark Brown0be98982008-05-19 12:31:28 +0200406 case SND_SOC_BIAS_STANDBY:
Mark Brown22d22ee2009-02-16 19:20:15 +0000407 /* Clear PWROFF, gate CLKOUT, everything else as-is */
408 reg = wm8731_read_reg_cache(codec, WM8731_PWR) & 0xff7f;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200409 wm8731_write(codec, WM8731_PWR, reg | 0x0040);
410 break;
Mark Brown0be98982008-05-19 12:31:28 +0200411 case SND_SOC_BIAS_OFF:
Richard Purdie40e0aa62006-10-06 18:36:07 +0200412 wm8731_write(codec, WM8731_ACTIVE, 0x0);
413 wm8731_write(codec, WM8731_PWR, 0xffff);
414 break;
415 }
Mark Brown0be98982008-05-19 12:31:28 +0200416 codec->bias_level = level;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200417 return 0;
418}
419
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100420#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
Liam Girdwoode550e172008-07-07 16:07:52 +0100429struct snd_soc_dai wm8731_dai = {
Richard Purdie40e0aa62006-10-06 18:36:07 +0200430 .name = "WM8731",
431 .playback = {
432 .stream_name = "Playback",
433 .channels_min = 1,
434 .channels_max = 2,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100435 .rates = WM8731_RATES,
436 .formats = WM8731_FORMATS,},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200437 .capture = {
438 .stream_name = "Capture",
439 .channels_min = 1,
440 .channels_max = 2,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100441 .rates = WM8731_RATES,
442 .formats = WM8731_FORMATS,},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200443 .ops = {
444 .prepare = wm8731_pcm_prepare,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100445 .hw_params = wm8731_hw_params,
Richard Purdie40e0aa62006-10-06 18:36:07 +0200446 .shutdown = wm8731_shutdown,
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100447 .digital_mute = wm8731_mute,
448 .set_sysclk = wm8731_set_dai_sysclk,
449 .set_fmt = wm8731_set_dai_fmt,
450 }
Richard Purdie40e0aa62006-10-06 18:36:07 +0200451};
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);
Mark Brown6627a652009-01-23 22:55:23 +0000457 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200458
459 wm8731_write(codec, WM8731_ACTIVE, 0x0);
Mark Brown0be98982008-05-19 12:31:28 +0200460 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200461 return 0;
462}
463
464static int wm8731_resume(struct platform_device *pdev)
465{
466 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +0000467 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200468 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 }
Mark Brown0be98982008-05-19 12:31:28 +0200478 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
479 wm8731_set_bias_level(codec, codec->suspend_bias_level);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200480 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{
Mark Brown6627a652009-01-23 22:55:23 +0000489 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200490 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;
Mark Brown0be98982008-05-19 12:31:28 +0200496 codec->set_bias_level = wm8731_set_bias_level;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200497 codec->dai = &wm8731_dai;
498 codec->num_dai = 1;
Mark Brownd751b232008-06-11 13:47:06 +0100499 codec->reg_cache_size = ARRAY_SIZE(wm8731_reg);
Takashi Iwai88cb4292007-02-05 14:56:20 +0100500 codec->reg_cache = kmemdup(wm8731_reg, sizeof(wm8731_reg), GFP_KERNEL);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200501 if (codec->reg_cache == NULL)
502 return -ENOMEM;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200503
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) {
Liam Girdwoode35115a2007-01-31 10:02:23 +0100509 printk(KERN_ERR "wm8731: failed to create pcms\n");
510 goto pcm_err;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200511 }
512
513 /* power on device */
Mark Brown0be98982008-05-19 12:31:28 +0200514 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200515
516 /* set the update bits */
517 reg = wm8731_read_reg_cache(codec, WM8731_LOUT1V);
Ville Syrjala389619f2007-11-26 08:58:24 +0100518 wm8731_write(codec, WM8731_LOUT1V, reg & ~0x0100);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200519 reg = wm8731_read_reg_cache(codec, WM8731_ROUT1V);
Ville Syrjala389619f2007-11-26 08:58:24 +0100520 wm8731_write(codec, WM8731_ROUT1V, reg & ~0x0100);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200521 reg = wm8731_read_reg_cache(codec, WM8731_LINVOL);
Ville Syrjala389619f2007-11-26 08:58:24 +0100522 wm8731_write(codec, WM8731_LINVOL, reg & ~0x0100);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200523 reg = wm8731_read_reg_cache(codec, WM8731_RINVOL);
Ville Syrjala389619f2007-11-26 08:58:24 +0100524 wm8731_write(codec, WM8731_RINVOL, reg & ~0x0100);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200525
Ian Molton3e8e1952009-01-09 00:23:21 +0000526 snd_soc_add_controls(codec, wm8731_snd_controls,
527 ARRAY_SIZE(wm8731_snd_controls));
Richard Purdie40e0aa62006-10-06 18:36:07 +0200528 wm8731_add_widgets(codec);
Mark Brown968a6022008-11-28 11:49:07 +0000529 ret = snd_soc_init_card(socdev);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200530 if (ret < 0) {
Liam Girdwoode35115a2007-01-31 10:02:23 +0100531 printk(KERN_ERR "wm8731: failed to register card\n");
532 goto card_err;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200533 }
534
535 return ret;
Liam Girdwoode35115a2007-01-31 10:02:23 +0100536
537card_err:
538 snd_soc_free_pcms(socdev);
539 snd_soc_dapm_free(socdev);
540pcm_err:
541 kfree(codec->reg_cache);
542 return ret;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200543}
544
545static struct snd_soc_device *wm8731_socdev;
546
Mark Brownd454aee2008-04-23 15:16:46 +0200547#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200548
549/*
550 * WM8731 2 wire address is determined by GPIO5
551 * state during powerup.
552 * low = 0x1a
553 * high = 0x1b
554 */
Richard Purdie40e0aa62006-10-06 18:36:07 +0200555
Jean Delvare81297c8a2008-09-01 18:47:01 +0100556static int wm8731_i2c_probe(struct i2c_client *i2c,
557 const struct i2c_device_id *id)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200558{
559 struct snd_soc_device *socdev = wm8731_socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000560 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200561 int ret;
562
Richard Purdie40e0aa62006-10-06 18:36:07 +0200563 i2c_set_clientdata(i2c, codec);
564 codec->control_data = i2c;
565
Richard Purdie40e0aa62006-10-06 18:36:07 +0200566 ret = wm8731_init(socdev);
Jean Delvare81297c8a2008-09-01 18:47:01 +0100567 if (ret < 0)
Mark Browna5c95e92008-06-23 14:51:29 +0100568 pr_err("failed to initialise WM8731\n");
Richard Purdie40e0aa62006-10-06 18:36:07 +0200569
Richard Purdie40e0aa62006-10-06 18:36:07 +0200570 return ret;
571}
572
Jean Delvare81297c8a2008-09-01 18:47:01 +0100573static int wm8731_i2c_remove(struct i2c_client *client)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200574{
Mark Brownd454aee2008-04-23 15:16:46 +0200575 struct snd_soc_codec *codec = i2c_get_clientdata(client);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200576 kfree(codec->reg_cache);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200577 return 0;
578}
579
Jean Delvare81297c8a2008-09-01 18:47:01 +0100580static const struct i2c_device_id wm8731_i2c_id[] = {
581 { "wm8731", 0 },
582 { }
583};
584MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200585
Richard Purdie40e0aa62006-10-06 18:36:07 +0200586static struct i2c_driver wm8731_i2c_driver = {
587 .driver = {
588 .name = "WM8731 I2C Codec",
589 .owner = THIS_MODULE,
590 },
Jean Delvare81297c8a2008-09-01 18:47:01 +0100591 .probe = wm8731_i2c_probe,
592 .remove = wm8731_i2c_remove,
593 .id_table = wm8731_i2c_id,
Richard Purdie40e0aa62006-10-06 18:36:07 +0200594};
595
Jean Delvare81297c8a2008-09-01 18:47:01 +0100596static 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}
Richard Purdie40e0aa62006-10-06 18:36:07 +0200635#endif
636
Cliff Caid2a40352008-09-01 18:47:03 +0100637#if defined(CONFIG_SPI_MASTER)
638static int __devinit wm8731_spi_probe(struct spi_device *spi)
639{
640 struct snd_soc_device *socdev = wm8731_socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000641 struct snd_soc_codec *codec = socdev->card->codec;
Cliff Caid2a40352008-09-01 18:47:03 +0100642 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;
Alan Horstmann8569be32008-09-09 19:25:49 +0100672 u8 msg[2];
Cliff Caid2a40352008-09-01 18:47:03 +0100673
674 if (len <= 0)
675 return 0;
676
Alan Horstmann8569be32008-09-09 19:25:49 +0100677 msg[0] = data[0];
678 msg[1] = data[1];
Cliff Caid2a40352008-09-01 18:47:03 +0100679
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
Richard Purdie40e0aa62006-10-06 18:36:07 +0200693static 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;
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100698 struct wm8731_priv *wm8731;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200699 int ret = 0;
700
Richard Purdie40e0aa62006-10-06 18:36:07 +0200701 setup = socdev->codec_data;
702 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
703 if (codec == NULL)
704 return -ENOMEM;
705
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100706 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;
Mark Brown6627a652009-01-23 22:55:23 +0000713 socdev->card->codec = codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200714 mutex_init(&codec->mutex);
715 INIT_LIST_HEAD(&codec->dapm_widgets);
716 INIT_LIST_HEAD(&codec->dapm_paths);
717
718 wm8731_socdev = socdev;
Cliff Caid2a40352008-09-01 18:47:03 +0100719 ret = -ENODEV;
720
Mark Brownd454aee2008-04-23 15:16:46 +0200721#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Richard Purdie40e0aa62006-10-06 18:36:07 +0200722 if (setup->i2c_address) {
Richard Purdie40e0aa62006-10-06 18:36:07 +0200723 codec->hw_write = (hw_write_t)i2c_master_send;
Jean Delvare81297c8a2008-09-01 18:47:01 +0100724 ret = wm8731_add_i2c_device(pdev, setup);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200725 }
Cliff Caid2a40352008-09-01 18:47:03 +0100726#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 }
Richard Purdie40e0aa62006-10-06 18:36:07 +0200734#endif
Jean Delvare3051e412008-08-25 11:49:20 +0100735
736 if (ret != 0) {
737 kfree(codec->private_data);
738 kfree(codec);
739 }
Richard Purdie40e0aa62006-10-06 18:36:07 +0200740 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);
Mark Brown6627a652009-01-23 22:55:23 +0000747 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200748
749 if (codec->control_data)
Mark Brown0be98982008-05-19 12:31:28 +0200750 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200751
752 snd_soc_free_pcms(socdev);
753 snd_soc_dapm_free(socdev);
Mark Brownd454aee2008-04-23 15:16:46 +0200754#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Jean Delvare81297c8a2008-09-01 18:47:01 +0100755 i2c_unregister_device(codec->control_data);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200756 i2c_del_driver(&wm8731_i2c_driver);
757#endif
Cliff Caid2a40352008-09-01 18:47:03 +0100758#if defined(CONFIG_SPI_MASTER)
759 spi_unregister_driver(&wm8731_spi_driver);
760#endif
Frank Mandarinob36d61d2007-02-02 17:14:56 +0100761 kfree(codec->private_data);
Richard Purdie40e0aa62006-10-06 18:36:07 +0200762 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};
Richard Purdie40e0aa62006-10-06 18:36:07 +0200773EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731);
774
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100775static int __init wm8731_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000776{
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
Richard Purdie40e0aa62006-10-06 18:36:07 +0200787MODULE_DESCRIPTION("ASoC WM8731 driver");
788MODULE_AUTHOR("Richard Purdie");
789MODULE_LICENSE("GPL");