Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 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> |
Cliff Cai | d2a4035 | 2008-09-01 18:47:03 +0100 | [diff] [blame] | 22 | #include <linux/spi/spi.h> |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 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 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 32 | struct snd_soc_codec_device soc_codec_dev_wm8731; |
| 33 | |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 34 | /* codec private data */ |
| 35 | struct wm8731_priv { |
| 36 | unsigned int sysclk; |
| 37 | }; |
| 38 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 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 | */ |
| 45 | static const u16 wm8731_reg[WM8731_CACHEREGNUM] = { |
| 46 | 0x0097, 0x0097, 0x0079, 0x0079, |
| 47 | 0x000a, 0x0008, 0x009f, 0x000a, |
| 48 | 0x0000, 0x0000 |
| 49 | }; |
| 50 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 51 | /* |
| 52 | * read wm8731 register cache |
| 53 | */ |
| 54 | static 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 | */ |
| 68 | static 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 | */ |
| 80 | static 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 Brown | d454aee | 2008-04-23 15:16:46 +0200 | [diff] [blame] | 92 | wm8731_write_reg_cache(codec, reg, value); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 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 | |
| 101 | static const char *wm8731_input_select[] = {"Line In", "Mic"}; |
| 102 | static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"}; |
| 103 | |
| 104 | static 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 | |
| 109 | static const struct snd_kcontrol_new wm8731_snd_controls[] = { |
| 110 | |
Liam Girdwood | bd903b6 | 2006-11-09 16:35:01 +0100 | [diff] [blame] | 111 | SOC_DOUBLE_R("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V, |
| 112 | 0, 127, 0), |
| 113 | SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V, |
| 114 | 7, 1, 0), |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 115 | |
| 116 | SOC_DOUBLE_R("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0), |
| 117 | SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1), |
| 118 | |
| 119 | SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0), |
| 120 | SOC_SINGLE("Capture Mic Switch", WM8731_APANA, 1, 1, 1), |
| 121 | |
| 122 | SOC_SINGLE("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1), |
| 123 | |
| 124 | SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1), |
| 125 | SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0), |
| 126 | |
| 127 | SOC_ENUM("Playback De-emphasis", wm8731_enum[1]), |
| 128 | }; |
| 129 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 130 | /* Output Mixer */ |
| 131 | static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = { |
| 132 | SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0), |
| 133 | SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0), |
| 134 | SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0), |
| 135 | }; |
| 136 | |
| 137 | /* Input mux */ |
| 138 | static const struct snd_kcontrol_new wm8731_input_mux_controls = |
| 139 | SOC_DAPM_ENUM("Input Select", wm8731_enum[0]); |
| 140 | |
| 141 | static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = { |
| 142 | SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1, |
| 143 | &wm8731_output_mixer_controls[0], |
| 144 | ARRAY_SIZE(wm8731_output_mixer_controls)), |
| 145 | SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1), |
| 146 | SND_SOC_DAPM_OUTPUT("LOUT"), |
| 147 | SND_SOC_DAPM_OUTPUT("LHPOUT"), |
| 148 | SND_SOC_DAPM_OUTPUT("ROUT"), |
| 149 | SND_SOC_DAPM_OUTPUT("RHPOUT"), |
| 150 | SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1), |
| 151 | SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls), |
| 152 | SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0), |
| 153 | SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1), |
| 154 | SND_SOC_DAPM_INPUT("MICIN"), |
| 155 | SND_SOC_DAPM_INPUT("RLINEIN"), |
| 156 | SND_SOC_DAPM_INPUT("LLINEIN"), |
| 157 | }; |
| 158 | |
Mark Brown | a65f056 | 2008-05-13 14:54:43 +0200 | [diff] [blame] | 159 | static const struct snd_soc_dapm_route intercon[] = { |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 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"}, |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | static int wm8731_add_widgets(struct snd_soc_codec *codec) |
| 183 | { |
Mark Brown | a65f056 | 2008-05-13 14:54:43 +0200 | [diff] [blame] | 184 | snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets, |
| 185 | ARRAY_SIZE(wm8731_dapm_widgets)); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 186 | |
Mark Brown | a65f056 | 2008-05-13 14:54:43 +0200 | [diff] [blame] | 187 | snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon)); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 188 | |
| 189 | snd_soc_dapm_new_widgets(codec); |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | struct _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 */ |
| 203 | static 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 Mandarino | 298a2c7 | 2007-01-31 10:02:56 +0100 | [diff] [blame] | 212 | {12000000, 32000, 375, 0x6, 0x0, 0x1}, |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 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 | |
| 237 | static 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 Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 248 | static int wm8731_hw_params(struct snd_pcm_substream *substream, |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 249 | struct snd_pcm_hw_params *params, |
| 250 | struct snd_soc_dai *dai) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 251 | { |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 252 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 253 | struct snd_soc_device *socdev = rtd->socdev; |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 254 | struct snd_soc_codec *codec = socdev->card->codec; |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 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; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 260 | |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 261 | wm8731_write(codec, WM8731_SRATE, srate); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 262 | |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 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 | } |
| 274 | |
| 275 | wm8731_write(codec, WM8731_IFACE, iface); |
| 276 | return 0; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 277 | } |
| 278 | |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 279 | static int wm8731_pcm_prepare(struct snd_pcm_substream *substream, |
| 280 | struct snd_soc_dai *dai) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 281 | { |
| 282 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 283 | struct snd_soc_device *socdev = rtd->socdev; |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 284 | struct snd_soc_codec *codec = socdev->card->codec; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 285 | |
| 286 | /* set active */ |
| 287 | wm8731_write(codec, WM8731_ACTIVE, 0x0001); |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 288 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 289 | return 0; |
| 290 | } |
| 291 | |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 292 | static void wm8731_shutdown(struct snd_pcm_substream *substream, |
| 293 | struct snd_soc_dai *dai) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 294 | { |
| 295 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 296 | struct snd_soc_device *socdev = rtd->socdev; |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 297 | struct snd_soc_codec *codec = socdev->card->codec; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 298 | |
| 299 | /* deactivate */ |
| 300 | if (!codec->active) { |
| 301 | udelay(50); |
| 302 | wm8731_write(codec, WM8731_ACTIVE, 0x0); |
| 303 | } |
| 304 | } |
| 305 | |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 306 | static int wm8731_mute(struct snd_soc_dai *dai, int mute) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 307 | { |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 308 | struct snd_soc_codec *codec = dai->codec; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 309 | u16 mute_reg = wm8731_read_reg_cache(codec, WM8731_APDIGI) & 0xfff7; |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 310 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 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 | |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 318 | static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 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 | |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 337 | static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai, |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 338 | 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 Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 396 | static int wm8731_set_bias_level(struct snd_soc_codec *codec, |
| 397 | enum snd_soc_bias_level level) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 398 | { |
Mark Brown | 22d22ee | 2009-02-16 19:20:15 +0000 | [diff] [blame^] | 399 | u16 reg; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 400 | |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 401 | switch (level) { |
| 402 | case SND_SOC_BIAS_ON: |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 403 | break; |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 404 | case SND_SOC_BIAS_PREPARE: |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 405 | break; |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 406 | case SND_SOC_BIAS_STANDBY: |
Mark Brown | 22d22ee | 2009-02-16 19:20:15 +0000 | [diff] [blame^] | 407 | /* Clear PWROFF, gate CLKOUT, everything else as-is */ |
| 408 | reg = wm8731_read_reg_cache(codec, WM8731_PWR) & 0xff7f; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 409 | wm8731_write(codec, WM8731_PWR, reg | 0x0040); |
| 410 | break; |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 411 | case SND_SOC_BIAS_OFF: |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 412 | wm8731_write(codec, WM8731_ACTIVE, 0x0); |
| 413 | wm8731_write(codec, WM8731_PWR, 0xffff); |
| 414 | break; |
| 415 | } |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 416 | codec->bias_level = level; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 417 | return 0; |
| 418 | } |
| 419 | |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 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 | |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 429 | struct snd_soc_dai wm8731_dai = { |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 430 | .name = "WM8731", |
| 431 | .playback = { |
| 432 | .stream_name = "Playback", |
| 433 | .channels_min = 1, |
| 434 | .channels_max = 2, |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 435 | .rates = WM8731_RATES, |
| 436 | .formats = WM8731_FORMATS,}, |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 437 | .capture = { |
| 438 | .stream_name = "Capture", |
| 439 | .channels_min = 1, |
| 440 | .channels_max = 2, |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 441 | .rates = WM8731_RATES, |
| 442 | .formats = WM8731_FORMATS,}, |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 443 | .ops = { |
| 444 | .prepare = wm8731_pcm_prepare, |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 445 | .hw_params = wm8731_hw_params, |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 446 | .shutdown = wm8731_shutdown, |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 447 | .digital_mute = wm8731_mute, |
| 448 | .set_sysclk = wm8731_set_dai_sysclk, |
| 449 | .set_fmt = wm8731_set_dai_fmt, |
| 450 | } |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 451 | }; |
| 452 | EXPORT_SYMBOL_GPL(wm8731_dai); |
| 453 | |
| 454 | static int wm8731_suspend(struct platform_device *pdev, pm_message_t state) |
| 455 | { |
| 456 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 457 | struct snd_soc_codec *codec = socdev->card->codec; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 458 | |
| 459 | wm8731_write(codec, WM8731_ACTIVE, 0x0); |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 460 | wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | static int wm8731_resume(struct platform_device *pdev) |
| 465 | { |
| 466 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 467 | struct snd_soc_codec *codec = socdev->card->codec; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 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 | } |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 478 | wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
| 479 | wm8731_set_bias_level(codec, codec->suspend_bias_level); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * initialise the WM8731 driver |
| 485 | * register the mixer and dsp interfaces with the kernel |
| 486 | */ |
| 487 | static int wm8731_init(struct snd_soc_device *socdev) |
| 488 | { |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 489 | struct snd_soc_codec *codec = socdev->card->codec; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 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; |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 496 | codec->set_bias_level = wm8731_set_bias_level; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 497 | codec->dai = &wm8731_dai; |
| 498 | codec->num_dai = 1; |
Mark Brown | d751b23 | 2008-06-11 13:47:06 +0100 | [diff] [blame] | 499 | codec->reg_cache_size = ARRAY_SIZE(wm8731_reg); |
Takashi Iwai | 88cb429 | 2007-02-05 14:56:20 +0100 | [diff] [blame] | 500 | codec->reg_cache = kmemdup(wm8731_reg, sizeof(wm8731_reg), GFP_KERNEL); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 501 | if (codec->reg_cache == NULL) |
| 502 | return -ENOMEM; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 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) { |
Liam Girdwood | e35115a | 2007-01-31 10:02:23 +0100 | [diff] [blame] | 509 | printk(KERN_ERR "wm8731: failed to create pcms\n"); |
| 510 | goto pcm_err; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | /* power on device */ |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 514 | wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 515 | |
| 516 | /* set the update bits */ |
| 517 | reg = wm8731_read_reg_cache(codec, WM8731_LOUT1V); |
Ville Syrjala | 389619f | 2007-11-26 08:58:24 +0100 | [diff] [blame] | 518 | wm8731_write(codec, WM8731_LOUT1V, reg & ~0x0100); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 519 | reg = wm8731_read_reg_cache(codec, WM8731_ROUT1V); |
Ville Syrjala | 389619f | 2007-11-26 08:58:24 +0100 | [diff] [blame] | 520 | wm8731_write(codec, WM8731_ROUT1V, reg & ~0x0100); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 521 | reg = wm8731_read_reg_cache(codec, WM8731_LINVOL); |
Ville Syrjala | 389619f | 2007-11-26 08:58:24 +0100 | [diff] [blame] | 522 | wm8731_write(codec, WM8731_LINVOL, reg & ~0x0100); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 523 | reg = wm8731_read_reg_cache(codec, WM8731_RINVOL); |
Ville Syrjala | 389619f | 2007-11-26 08:58:24 +0100 | [diff] [blame] | 524 | wm8731_write(codec, WM8731_RINVOL, reg & ~0x0100); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 525 | |
Ian Molton | 3e8e195 | 2009-01-09 00:23:21 +0000 | [diff] [blame] | 526 | snd_soc_add_controls(codec, wm8731_snd_controls, |
| 527 | ARRAY_SIZE(wm8731_snd_controls)); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 528 | wm8731_add_widgets(codec); |
Mark Brown | 968a602 | 2008-11-28 11:49:07 +0000 | [diff] [blame] | 529 | ret = snd_soc_init_card(socdev); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 530 | if (ret < 0) { |
Liam Girdwood | e35115a | 2007-01-31 10:02:23 +0100 | [diff] [blame] | 531 | printk(KERN_ERR "wm8731: failed to register card\n"); |
| 532 | goto card_err; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | return ret; |
Liam Girdwood | e35115a | 2007-01-31 10:02:23 +0100 | [diff] [blame] | 536 | |
| 537 | card_err: |
| 538 | snd_soc_free_pcms(socdev); |
| 539 | snd_soc_dapm_free(socdev); |
| 540 | pcm_err: |
| 541 | kfree(codec->reg_cache); |
| 542 | return ret; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | static struct snd_soc_device *wm8731_socdev; |
| 546 | |
Mark Brown | d454aee | 2008-04-23 15:16:46 +0200 | [diff] [blame] | 547 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 548 | |
| 549 | /* |
| 550 | * WM8731 2 wire address is determined by GPIO5 |
| 551 | * state during powerup. |
| 552 | * low = 0x1a |
| 553 | * high = 0x1b |
| 554 | */ |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 555 | |
Jean Delvare | 81297c8 | 2008-09-01 18:47:01 +0100 | [diff] [blame] | 556 | static int wm8731_i2c_probe(struct i2c_client *i2c, |
| 557 | const struct i2c_device_id *id) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 558 | { |
| 559 | struct snd_soc_device *socdev = wm8731_socdev; |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 560 | struct snd_soc_codec *codec = socdev->card->codec; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 561 | int ret; |
| 562 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 563 | i2c_set_clientdata(i2c, codec); |
| 564 | codec->control_data = i2c; |
| 565 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 566 | ret = wm8731_init(socdev); |
Jean Delvare | 81297c8 | 2008-09-01 18:47:01 +0100 | [diff] [blame] | 567 | if (ret < 0) |
Mark Brown | a5c95e9 | 2008-06-23 14:51:29 +0100 | [diff] [blame] | 568 | pr_err("failed to initialise WM8731\n"); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 569 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 570 | return ret; |
| 571 | } |
| 572 | |
Jean Delvare | 81297c8 | 2008-09-01 18:47:01 +0100 | [diff] [blame] | 573 | static int wm8731_i2c_remove(struct i2c_client *client) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 574 | { |
Mark Brown | d454aee | 2008-04-23 15:16:46 +0200 | [diff] [blame] | 575 | struct snd_soc_codec *codec = i2c_get_clientdata(client); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 576 | kfree(codec->reg_cache); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 577 | return 0; |
| 578 | } |
| 579 | |
Jean Delvare | 81297c8 | 2008-09-01 18:47:01 +0100 | [diff] [blame] | 580 | static const struct i2c_device_id wm8731_i2c_id[] = { |
| 581 | { "wm8731", 0 }, |
| 582 | { } |
| 583 | }; |
| 584 | MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 585 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 586 | static struct i2c_driver wm8731_i2c_driver = { |
| 587 | .driver = { |
| 588 | .name = "WM8731 I2C Codec", |
| 589 | .owner = THIS_MODULE, |
| 590 | }, |
Jean Delvare | 81297c8 | 2008-09-01 18:47:01 +0100 | [diff] [blame] | 591 | .probe = wm8731_i2c_probe, |
| 592 | .remove = wm8731_i2c_remove, |
| 593 | .id_table = wm8731_i2c_id, |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 594 | }; |
| 595 | |
Jean Delvare | 81297c8 | 2008-09-01 18:47:01 +0100 | [diff] [blame] | 596 | static 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 | |
| 631 | err_driver: |
| 632 | i2c_del_driver(&wm8731_i2c_driver); |
| 633 | return -ENODEV; |
| 634 | } |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 635 | #endif |
| 636 | |
Cliff Cai | d2a4035 | 2008-09-01 18:47:03 +0100 | [diff] [blame] | 637 | #if defined(CONFIG_SPI_MASTER) |
| 638 | static int __devinit wm8731_spi_probe(struct spi_device *spi) |
| 639 | { |
| 640 | struct snd_soc_device *socdev = wm8731_socdev; |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 641 | struct snd_soc_codec *codec = socdev->card->codec; |
Cliff Cai | d2a4035 | 2008-09-01 18:47:03 +0100 | [diff] [blame] | 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 | |
| 653 | static int __devexit wm8731_spi_remove(struct spi_device *spi) |
| 654 | { |
| 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | static 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 | |
| 668 | static 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 Horstmann | 8569be3 | 2008-09-09 19:25:49 +0100 | [diff] [blame] | 672 | u8 msg[2]; |
Cliff Cai | d2a4035 | 2008-09-01 18:47:03 +0100 | [diff] [blame] | 673 | |
| 674 | if (len <= 0) |
| 675 | return 0; |
| 676 | |
Alan Horstmann | 8569be3 | 2008-09-09 19:25:49 +0100 | [diff] [blame] | 677 | msg[0] = data[0]; |
| 678 | msg[1] = data[1]; |
Cliff Cai | d2a4035 | 2008-09-01 18:47:03 +0100 | [diff] [blame] | 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 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 693 | static 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 Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 698 | struct wm8731_priv *wm8731; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 699 | int ret = 0; |
| 700 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 701 | setup = socdev->codec_data; |
| 702 | codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL); |
| 703 | if (codec == NULL) |
| 704 | return -ENOMEM; |
| 705 | |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 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; |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 713 | socdev->card->codec = codec; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 714 | mutex_init(&codec->mutex); |
| 715 | INIT_LIST_HEAD(&codec->dapm_widgets); |
| 716 | INIT_LIST_HEAD(&codec->dapm_paths); |
| 717 | |
| 718 | wm8731_socdev = socdev; |
Cliff Cai | d2a4035 | 2008-09-01 18:47:03 +0100 | [diff] [blame] | 719 | ret = -ENODEV; |
| 720 | |
Mark Brown | d454aee | 2008-04-23 15:16:46 +0200 | [diff] [blame] | 721 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 722 | if (setup->i2c_address) { |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 723 | codec->hw_write = (hw_write_t)i2c_master_send; |
Jean Delvare | 81297c8 | 2008-09-01 18:47:01 +0100 | [diff] [blame] | 724 | ret = wm8731_add_i2c_device(pdev, setup); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 725 | } |
Cliff Cai | d2a4035 | 2008-09-01 18:47:03 +0100 | [diff] [blame] | 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 | } |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 734 | #endif |
Jean Delvare | 3051e41 | 2008-08-25 11:49:20 +0100 | [diff] [blame] | 735 | |
| 736 | if (ret != 0) { |
| 737 | kfree(codec->private_data); |
| 738 | kfree(codec); |
| 739 | } |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 740 | return ret; |
| 741 | } |
| 742 | |
| 743 | /* power down chip */ |
| 744 | static int wm8731_remove(struct platform_device *pdev) |
| 745 | { |
| 746 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 747 | struct snd_soc_codec *codec = socdev->card->codec; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 748 | |
| 749 | if (codec->control_data) |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 750 | wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 751 | |
| 752 | snd_soc_free_pcms(socdev); |
| 753 | snd_soc_dapm_free(socdev); |
Mark Brown | d454aee | 2008-04-23 15:16:46 +0200 | [diff] [blame] | 754 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
Jean Delvare | 81297c8 | 2008-09-01 18:47:01 +0100 | [diff] [blame] | 755 | i2c_unregister_device(codec->control_data); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 756 | i2c_del_driver(&wm8731_i2c_driver); |
| 757 | #endif |
Cliff Cai | d2a4035 | 2008-09-01 18:47:03 +0100 | [diff] [blame] | 758 | #if defined(CONFIG_SPI_MASTER) |
| 759 | spi_unregister_driver(&wm8731_spi_driver); |
| 760 | #endif |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 761 | kfree(codec->private_data); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 762 | kfree(codec); |
| 763 | |
| 764 | return 0; |
| 765 | } |
| 766 | |
| 767 | struct 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 Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 773 | EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731); |
| 774 | |
Takashi Iwai | c9b3a40 | 2008-12-10 07:47:22 +0100 | [diff] [blame] | 775 | static int __init wm8731_modinit(void) |
Mark Brown | 64089b8 | 2008-12-08 19:17:58 +0000 | [diff] [blame] | 776 | { |
| 777 | return snd_soc_register_dai(&wm8731_dai); |
| 778 | } |
| 779 | module_init(wm8731_modinit); |
| 780 | |
| 781 | static void __exit wm8731_exit(void) |
| 782 | { |
| 783 | snd_soc_unregister_dai(&wm8731_dai); |
| 784 | } |
| 785 | module_exit(wm8731_exit); |
| 786 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 787 | MODULE_DESCRIPTION("ASoC WM8731 driver"); |
| 788 | MODULE_AUTHOR("Richard Purdie"); |
| 789 | MODULE_LICENSE("GPL"); |