Mike Arthur | bd6d417 | 2009-08-18 20:37:49 +0100 | [diff] [blame] | 1 | /* |
| 2 | * wm8711.c -- WM8711 ALSA SoC Audio driver |
| 3 | * |
| 4 | * Copyright 2006 Wolfson Microelectronics |
| 5 | * |
| 6 | * Author: Mike Arthur <linux@wolfsonmicro.com> |
| 7 | * |
| 8 | * Based on wm8731.c by Richard Purdie |
| 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> |
| 22 | #include <sound/core.h> |
| 23 | #include <sound/pcm.h> |
| 24 | #include <sound/pcm_params.h> |
| 25 | #include <sound/soc.h> |
| 26 | #include <sound/soc-dapm.h> |
| 27 | #include <sound/initval.h> |
| 28 | |
| 29 | #include "wm8711.h" |
| 30 | |
Mark Brown | 318b0b8 | 2009-08-18 20:57:33 +0100 | [diff] [blame^] | 31 | static struct snd_soc_codec *wm8711_codec; |
Mike Arthur | bd6d417 | 2009-08-18 20:37:49 +0100 | [diff] [blame] | 32 | |
| 33 | /* codec private data */ |
| 34 | struct wm8711_priv { |
Mark Brown | 318b0b8 | 2009-08-18 20:57:33 +0100 | [diff] [blame^] | 35 | struct snd_soc_codec codec; |
| 36 | u16 reg_cache[WM8711_CACHEREGNUM]; |
Mike Arthur | bd6d417 | 2009-08-18 20:37:49 +0100 | [diff] [blame] | 37 | unsigned int sysclk; |
| 38 | }; |
| 39 | |
| 40 | /* |
| 41 | * wm8711 register cache |
| 42 | * We can't read the WM8711 register space when we are |
| 43 | * using 2 wire for device control, so we cache them instead. |
| 44 | * There is no point in caching the reset register |
| 45 | */ |
| 46 | static const u16 wm8711_reg[WM8711_CACHEREGNUM] = { |
| 47 | 0x0079, 0x0079, 0x000a, 0x0008, |
| 48 | 0x009f, 0x000a, 0x0000, 0x0000 |
| 49 | }; |
| 50 | |
| 51 | /* |
| 52 | * read wm8711 register cache |
| 53 | */ |
| 54 | static inline unsigned int wm8711_read_reg_cache(struct snd_soc_codec *codec, |
| 55 | unsigned int reg) |
| 56 | { |
| 57 | u16 *cache = codec->reg_cache; |
| 58 | if (reg == WM8711_RESET) |
| 59 | return 0; |
| 60 | if (reg >= WM8711_CACHEREGNUM) |
| 61 | return -1; |
| 62 | return cache[reg]; |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * write wm8711 register cache |
| 67 | */ |
| 68 | static inline void wm8711_write_reg_cache(struct snd_soc_codec *codec, |
| 69 | u16 reg, unsigned int value) |
| 70 | { |
| 71 | u16 *cache = codec->reg_cache; |
| 72 | if (reg >= WM8711_CACHEREGNUM) |
| 73 | return; |
| 74 | cache[reg] = value; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * write to the WM8711 register space |
| 79 | */ |
| 80 | static int wm8711_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 WM8753 register offset |
| 87 | * D8...D0 register data |
| 88 | */ |
| 89 | data[0] = (reg << 1) | ((value >> 8) & 0x0001); |
| 90 | data[1] = value & 0x00ff; |
| 91 | |
| 92 | wm8711_write_reg_cache(codec, reg, value); |
| 93 | if (codec->hw_write(codec->control_data, data, 2) == 2) |
| 94 | return 0; |
| 95 | else |
| 96 | return -EIO; |
| 97 | } |
| 98 | |
| 99 | #define wm8711_reset(c) wm8711_write(c, WM8711_RESET, 0) |
| 100 | |
| 101 | static const struct snd_kcontrol_new wm8711_snd_controls[] = { |
| 102 | |
| 103 | SOC_DOUBLE_R("Master Playback Volume", WM8711_LOUT1V, WM8711_ROUT1V, |
| 104 | 0, 127, 0), |
| 105 | SOC_DOUBLE_R("Master Playback ZC Switch", WM8711_LOUT1V, WM8711_ROUT1V, |
| 106 | 7, 1, 0), |
| 107 | |
| 108 | }; |
| 109 | |
| 110 | /* add non dapm controls */ |
| 111 | static int wm8711_add_controls(struct snd_soc_codec *codec) |
| 112 | { |
| 113 | int err, i; |
| 114 | |
| 115 | for (i = 0; i < ARRAY_SIZE(wm8711_snd_controls); i++) { |
| 116 | err = snd_ctl_add(codec->card, |
| 117 | snd_soc_cnew(&wm8711_snd_controls[i], codec, |
| 118 | NULL)); |
| 119 | if (err < 0) |
| 120 | return err; |
| 121 | } |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | /* Output Mixer */ |
| 127 | static const struct snd_kcontrol_new wm8711_output_mixer_controls[] = { |
| 128 | SOC_DAPM_SINGLE("Line Bypass Switch", WM8711_APANA, 3, 1, 0), |
| 129 | SOC_DAPM_SINGLE("HiFi Playback Switch", WM8711_APANA, 4, 1, 0), |
| 130 | }; |
| 131 | |
| 132 | static const struct snd_soc_dapm_widget wm8711_dapm_widgets[] = { |
| 133 | SND_SOC_DAPM_MIXER("Output Mixer", WM8711_PWR, 4, 1, |
| 134 | &wm8711_output_mixer_controls[0], |
| 135 | ARRAY_SIZE(wm8711_output_mixer_controls)), |
| 136 | SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8711_PWR, 3, 1), |
| 137 | SND_SOC_DAPM_OUTPUT("LOUT"), |
| 138 | SND_SOC_DAPM_OUTPUT("LHPOUT"), |
| 139 | SND_SOC_DAPM_OUTPUT("ROUT"), |
| 140 | SND_SOC_DAPM_OUTPUT("RHPOUT"), |
| 141 | }; |
| 142 | |
| 143 | static const struct snd_soc_dapm_route intercon[] = { |
| 144 | /* output mixer */ |
| 145 | {"Output Mixer", "Line Bypass Switch", "Line Input"}, |
| 146 | {"Output Mixer", "HiFi Playback Switch", "DAC"}, |
| 147 | |
| 148 | /* outputs */ |
| 149 | {"RHPOUT", NULL, "Output Mixer"}, |
| 150 | {"ROUT", NULL, "Output Mixer"}, |
| 151 | {"LHPOUT", NULL, "Output Mixer"}, |
| 152 | {"LOUT", NULL, "Output Mixer"}, |
| 153 | }; |
| 154 | |
| 155 | static int wm8711_add_widgets(struct snd_soc_codec *codec) |
| 156 | { |
| 157 | snd_soc_dapm_new_controls(codec, wm8711_dapm_widgets, |
| 158 | ARRAY_SIZE(wm8711_dapm_widgets)); |
| 159 | |
| 160 | snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon)); |
| 161 | |
| 162 | snd_soc_dapm_new_widgets(codec); |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | struct _coeff_div { |
| 167 | u32 mclk; |
| 168 | u32 rate; |
| 169 | u16 fs; |
| 170 | u8 sr:4; |
| 171 | u8 bosr:1; |
| 172 | u8 usb:1; |
| 173 | }; |
| 174 | |
| 175 | /* codec mclk clock divider coefficients */ |
| 176 | static const struct _coeff_div coeff_div[] = { |
| 177 | /* 48k */ |
| 178 | {12288000, 48000, 256, 0x0, 0x0, 0x0}, |
| 179 | {18432000, 48000, 384, 0x0, 0x1, 0x0}, |
| 180 | {12000000, 48000, 250, 0x0, 0x0, 0x1}, |
| 181 | |
| 182 | /* 32k */ |
| 183 | {12288000, 32000, 384, 0x6, 0x0, 0x0}, |
| 184 | {18432000, 32000, 576, 0x6, 0x1, 0x0}, |
| 185 | {12000000, 32000, 375, 0x6, 0x0, 0x1}, |
| 186 | |
| 187 | /* 8k */ |
| 188 | {12288000, 8000, 1536, 0x3, 0x0, 0x0}, |
| 189 | {18432000, 8000, 2304, 0x3, 0x1, 0x0}, |
| 190 | {11289600, 8000, 1408, 0xb, 0x0, 0x0}, |
| 191 | {16934400, 8000, 2112, 0xb, 0x1, 0x0}, |
| 192 | {12000000, 8000, 1500, 0x3, 0x0, 0x1}, |
| 193 | |
| 194 | /* 96k */ |
| 195 | {12288000, 96000, 128, 0x7, 0x0, 0x0}, |
| 196 | {18432000, 96000, 192, 0x7, 0x1, 0x0}, |
| 197 | {12000000, 96000, 125, 0x7, 0x0, 0x1}, |
| 198 | |
| 199 | /* 44.1k */ |
| 200 | {11289600, 44100, 256, 0x8, 0x0, 0x0}, |
| 201 | {16934400, 44100, 384, 0x8, 0x1, 0x0}, |
| 202 | {12000000, 44100, 272, 0x8, 0x1, 0x1}, |
| 203 | |
| 204 | /* 88.2k */ |
| 205 | {11289600, 88200, 128, 0xf, 0x0, 0x0}, |
| 206 | {16934400, 88200, 192, 0xf, 0x1, 0x0}, |
| 207 | {12000000, 88200, 136, 0xf, 0x1, 0x1}, |
| 208 | }; |
| 209 | |
| 210 | static inline int get_coeff(int mclk, int rate) |
| 211 | { |
| 212 | int i; |
| 213 | |
| 214 | for (i = 0; i < ARRAY_SIZE(coeff_div); i++) { |
| 215 | if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk) |
| 216 | return i; |
| 217 | } |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static int wm8711_hw_params(struct snd_pcm_substream *substream, |
| 222 | struct snd_pcm_hw_params *params, |
| 223 | struct snd_soc_dai *dai) |
| 224 | { |
| 225 | struct snd_soc_codec *codec = dai->codec; |
| 226 | struct wm8711_priv *wm8711 = codec->private_data; |
| 227 | u16 iface = wm8711_read_reg_cache(codec, WM8711_IFACE) & 0xfffc; |
| 228 | int i = get_coeff(wm8711->sysclk, params_rate(params)); |
| 229 | u16 srate = (coeff_div[i].sr << 2) | |
| 230 | (coeff_div[i].bosr << 1) | coeff_div[i].usb; |
| 231 | |
| 232 | wm8711_write(codec, WM8711_SRATE, srate); |
| 233 | |
| 234 | /* bit size */ |
| 235 | switch (params_format(params)) { |
| 236 | case SNDRV_PCM_FORMAT_S16_LE: |
| 237 | break; |
| 238 | case SNDRV_PCM_FORMAT_S20_3LE: |
| 239 | iface |= 0x0004; |
| 240 | break; |
| 241 | case SNDRV_PCM_FORMAT_S24_LE: |
| 242 | iface |= 0x0008; |
| 243 | break; |
| 244 | } |
| 245 | |
| 246 | wm8711_write(codec, WM8711_IFACE, iface); |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | static int wm8711_pcm_prepare(struct snd_pcm_substream *substream, |
| 251 | struct snd_soc_dai *dai) |
| 252 | { |
| 253 | struct snd_soc_codec *codec = dai->codec; |
| 254 | |
| 255 | /* set active */ |
| 256 | wm8711_write(codec, WM8711_ACTIVE, 0x0001); |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | static void wm8711_shutdown(struct snd_pcm_substream *substream, |
| 262 | struct snd_soc_dai *dai) |
| 263 | { |
| 264 | struct snd_soc_codec *codec = dai->codec; |
| 265 | |
| 266 | /* deactivate */ |
| 267 | if (!codec->active) { |
| 268 | udelay(50); |
| 269 | wm8711_write(codec, WM8711_ACTIVE, 0x0); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | static int wm8711_mute(struct snd_soc_dai *dai, int mute) |
| 274 | { |
| 275 | struct snd_soc_codec *codec = dai->codec; |
| 276 | u16 mute_reg = wm8711_read_reg_cache(codec, WM8711_APDIGI) & 0xfff7; |
| 277 | |
| 278 | if (mute) |
| 279 | wm8711_write(codec, WM8711_APDIGI, mute_reg | 0x8); |
| 280 | else |
| 281 | wm8711_write(codec, WM8711_APDIGI, mute_reg); |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | static int wm8711_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
| 287 | int clk_id, unsigned int freq, int dir) |
| 288 | { |
| 289 | struct snd_soc_codec *codec = codec_dai->codec; |
| 290 | struct wm8711_priv *wm8711 = codec->private_data; |
| 291 | |
| 292 | switch (freq) { |
| 293 | case 11289600: |
| 294 | case 12000000: |
| 295 | case 12288000: |
| 296 | case 16934400: |
| 297 | case 18432000: |
| 298 | wm8711->sysclk = freq; |
| 299 | return 0; |
| 300 | } |
| 301 | return -EINVAL; |
| 302 | } |
| 303 | |
| 304 | static int wm8711_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 305 | unsigned int fmt) |
| 306 | { |
| 307 | struct snd_soc_codec *codec = codec_dai->codec; |
| 308 | u16 iface = 0; |
| 309 | |
| 310 | /* set master/slave audio interface */ |
| 311 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 312 | case SND_SOC_DAIFMT_CBM_CFM: |
| 313 | iface |= 0x0040; |
| 314 | break; |
| 315 | case SND_SOC_DAIFMT_CBS_CFS: |
| 316 | break; |
| 317 | default: |
| 318 | return -EINVAL; |
| 319 | } |
| 320 | |
| 321 | /* interface format */ |
| 322 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 323 | case SND_SOC_DAIFMT_I2S: |
| 324 | iface |= 0x0002; |
| 325 | break; |
| 326 | case SND_SOC_DAIFMT_RIGHT_J: |
| 327 | break; |
| 328 | case SND_SOC_DAIFMT_LEFT_J: |
| 329 | iface |= 0x0001; |
| 330 | break; |
| 331 | case SND_SOC_DAIFMT_DSP_A: |
| 332 | iface |= 0x0003; |
| 333 | break; |
| 334 | case SND_SOC_DAIFMT_DSP_B: |
| 335 | iface |= 0x0013; |
| 336 | break; |
| 337 | default: |
| 338 | return -EINVAL; |
| 339 | } |
| 340 | |
| 341 | /* clock inversion */ |
| 342 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 343 | case SND_SOC_DAIFMT_NB_NF: |
| 344 | break; |
| 345 | case SND_SOC_DAIFMT_IB_IF: |
| 346 | iface |= 0x0090; |
| 347 | break; |
| 348 | case SND_SOC_DAIFMT_IB_NF: |
| 349 | iface |= 0x0080; |
| 350 | break; |
| 351 | case SND_SOC_DAIFMT_NB_IF: |
| 352 | iface |= 0x0010; |
| 353 | break; |
| 354 | default: |
| 355 | return -EINVAL; |
| 356 | } |
| 357 | |
| 358 | /* set iface */ |
| 359 | wm8711_write(codec, WM8711_IFACE, iface); |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | |
| 364 | static int wm8711_set_bias_level(struct snd_soc_codec *codec, |
| 365 | enum snd_soc_bias_level level) |
| 366 | { |
| 367 | u16 reg = wm8711_read_reg_cache(codec, WM8711_PWR) & 0xff7f; |
| 368 | |
| 369 | switch (level) { |
| 370 | case SND_SOC_BIAS_ON: |
| 371 | wm8711_write(codec, WM8711_PWR, reg); |
| 372 | break; |
| 373 | case SND_SOC_BIAS_PREPARE: |
| 374 | break; |
| 375 | case SND_SOC_BIAS_STANDBY: |
| 376 | wm8711_write(codec, WM8711_PWR, reg | 0x0040); |
| 377 | break; |
| 378 | case SND_SOC_BIAS_OFF: |
| 379 | wm8711_write(codec, WM8711_ACTIVE, 0x0); |
| 380 | wm8711_write(codec, WM8711_PWR, 0xffff); |
| 381 | break; |
| 382 | } |
| 383 | codec->bias_level = level; |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | #define WM8711_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\ |
| 388 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\ |
| 389 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\ |
| 390 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\ |
| 391 | SNDRV_PCM_RATE_96000) |
| 392 | |
| 393 | #define WM8711_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ |
| 394 | SNDRV_PCM_FMTBIT_S24_LE) |
| 395 | |
| 396 | static struct snd_soc_dai_ops wm8711_ops = { |
| 397 | .prepare = wm8711_pcm_prepare, |
| 398 | .hw_params = wm8711_hw_params, |
| 399 | .shutdown = wm8711_shutdown, |
| 400 | .digital_mute = wm8711_mute, |
| 401 | .set_sysclk = wm8711_set_dai_sysclk, |
| 402 | .set_fmt = wm8711_set_dai_fmt, |
| 403 | }; |
| 404 | |
| 405 | struct snd_soc_dai wm8711_dai = { |
| 406 | .name = "WM8711", |
| 407 | .playback = { |
| 408 | .stream_name = "Playback", |
| 409 | .channels_min = 1, |
| 410 | .channels_max = 2, |
| 411 | .rates = WM8711_RATES, |
| 412 | .formats = WM8711_FORMATS,}, |
| 413 | .ops = &wm8711_ops, |
| 414 | }; |
| 415 | EXPORT_SYMBOL_GPL(wm8711_dai); |
| 416 | |
| 417 | static int wm8711_suspend(struct platform_device *pdev, pm_message_t state) |
| 418 | { |
| 419 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 420 | struct snd_soc_codec *codec = socdev->card->codec; |
| 421 | |
| 422 | wm8711_write(codec, WM8711_ACTIVE, 0x0); |
| 423 | wm8711_set_bias_level(codec, SND_SOC_BIAS_OFF); |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | static int wm8711_resume(struct platform_device *pdev) |
| 428 | { |
| 429 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 430 | struct snd_soc_codec *codec = socdev->card->codec; |
| 431 | int i; |
| 432 | u8 data[2]; |
| 433 | u16 *cache = codec->reg_cache; |
| 434 | |
| 435 | /* Sync reg_cache with the hardware */ |
| 436 | for (i = 0; i < ARRAY_SIZE(wm8711_reg); i++) { |
| 437 | data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001); |
| 438 | data[1] = cache[i] & 0x00ff; |
| 439 | codec->hw_write(codec->control_data, data, 2); |
| 440 | } |
| 441 | wm8711_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
| 442 | wm8711_set_bias_level(codec, codec->suspend_bias_level); |
| 443 | return 0; |
| 444 | } |
| 445 | |
Mark Brown | 318b0b8 | 2009-08-18 20:57:33 +0100 | [diff] [blame^] | 446 | static int wm8711_probe(struct platform_device *pdev) |
Mike Arthur | bd6d417 | 2009-08-18 20:37:49 +0100 | [diff] [blame] | 447 | { |
Mark Brown | 318b0b8 | 2009-08-18 20:57:33 +0100 | [diff] [blame^] | 448 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 449 | struct snd_soc_codec *codec; |
| 450 | int ret = 0; |
Mike Arthur | bd6d417 | 2009-08-18 20:37:49 +0100 | [diff] [blame] | 451 | |
Mark Brown | 318b0b8 | 2009-08-18 20:57:33 +0100 | [diff] [blame^] | 452 | if (wm8711_codec == NULL) { |
| 453 | dev_err(&pdev->dev, "Codec device not registered\n"); |
| 454 | return -ENODEV; |
| 455 | } |
Mike Arthur | bd6d417 | 2009-08-18 20:37:49 +0100 | [diff] [blame] | 456 | |
Mark Brown | 318b0b8 | 2009-08-18 20:57:33 +0100 | [diff] [blame^] | 457 | socdev->card->codec = wm8711_codec; |
| 458 | codec = wm8711_codec; |
Mike Arthur | bd6d417 | 2009-08-18 20:37:49 +0100 | [diff] [blame] | 459 | |
| 460 | /* register pcms */ |
| 461 | ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); |
| 462 | if (ret < 0) { |
Mark Brown | 318b0b8 | 2009-08-18 20:57:33 +0100 | [diff] [blame^] | 463 | dev_err(codec->dev, "failed to create pcms: %d\n", ret); |
Mike Arthur | bd6d417 | 2009-08-18 20:37:49 +0100 | [diff] [blame] | 464 | goto pcm_err; |
| 465 | } |
| 466 | |
Mark Brown | 318b0b8 | 2009-08-18 20:57:33 +0100 | [diff] [blame^] | 467 | snd_soc_add_controls(codec, wm8711_snd_controls, |
| 468 | ARRAY_SIZE(wm8711_snd_controls)); |
Mike Arthur | bd6d417 | 2009-08-18 20:37:49 +0100 | [diff] [blame] | 469 | wm8711_add_widgets(codec); |
| 470 | ret = snd_soc_init_card(socdev); |
| 471 | if (ret < 0) { |
Mark Brown | 318b0b8 | 2009-08-18 20:57:33 +0100 | [diff] [blame^] | 472 | dev_err(codec->dev, "failed to register card: %d\n", ret); |
Mike Arthur | bd6d417 | 2009-08-18 20:37:49 +0100 | [diff] [blame] | 473 | goto card_err; |
| 474 | } |
Mark Brown | 318b0b8 | 2009-08-18 20:57:33 +0100 | [diff] [blame^] | 475 | |
Mike Arthur | bd6d417 | 2009-08-18 20:37:49 +0100 | [diff] [blame] | 476 | return ret; |
| 477 | |
| 478 | card_err: |
| 479 | snd_soc_free_pcms(socdev); |
| 480 | snd_soc_dapm_free(socdev); |
| 481 | pcm_err: |
Mike Arthur | bd6d417 | 2009-08-18 20:37:49 +0100 | [diff] [blame] | 482 | return ret; |
| 483 | } |
| 484 | |
| 485 | /* power down chip */ |
| 486 | static int wm8711_remove(struct platform_device *pdev) |
| 487 | { |
| 488 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
Mike Arthur | bd6d417 | 2009-08-18 20:37:49 +0100 | [diff] [blame] | 489 | |
| 490 | snd_soc_free_pcms(socdev); |
| 491 | snd_soc_dapm_free(socdev); |
Mike Arthur | bd6d417 | 2009-08-18 20:37:49 +0100 | [diff] [blame] | 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | struct snd_soc_codec_device soc_codec_dev_wm8711 = { |
| 497 | .probe = wm8711_probe, |
| 498 | .remove = wm8711_remove, |
| 499 | .suspend = wm8711_suspend, |
| 500 | .resume = wm8711_resume, |
| 501 | }; |
| 502 | EXPORT_SYMBOL_GPL(soc_codec_dev_wm8711); |
| 503 | |
Mark Brown | 318b0b8 | 2009-08-18 20:57:33 +0100 | [diff] [blame^] | 504 | static int wm8711_register(struct wm8711_priv *wm8711) |
| 505 | { |
| 506 | int ret; |
| 507 | struct snd_soc_codec *codec = &wm8711->codec; |
| 508 | u16 reg; |
| 509 | |
| 510 | if (wm8711_codec) { |
| 511 | dev_err(codec->dev, "Another WM8711 is registered\n"); |
| 512 | return -EINVAL; |
| 513 | } |
| 514 | |
| 515 | mutex_init(&codec->mutex); |
| 516 | INIT_LIST_HEAD(&codec->dapm_widgets); |
| 517 | INIT_LIST_HEAD(&codec->dapm_paths); |
| 518 | |
| 519 | codec->private_data = wm8711; |
| 520 | codec->name = "WM8711"; |
| 521 | codec->owner = THIS_MODULE; |
| 522 | codec->read = wm8711_read_reg_cache; |
| 523 | codec->write = wm8711_write; |
| 524 | codec->bias_level = SND_SOC_BIAS_OFF; |
| 525 | codec->set_bias_level = wm8711_set_bias_level; |
| 526 | codec->dai = &wm8711_dai; |
| 527 | codec->num_dai = 1; |
| 528 | codec->reg_cache_size = WM8711_CACHEREGNUM; |
| 529 | codec->reg_cache = &wm8711->reg_cache; |
| 530 | |
| 531 | memcpy(codec->reg_cache, wm8711_reg, sizeof(wm8711_reg)); |
| 532 | |
| 533 | ret = wm8711_reset(codec); |
| 534 | if (ret < 0) { |
| 535 | dev_err(codec->dev, "Failed to issue reset\n"); |
| 536 | return ret; |
| 537 | } |
| 538 | |
| 539 | wm8711_dai.dev = codec->dev; |
| 540 | |
| 541 | wm8711_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
| 542 | |
| 543 | /* Latch the update bits */ |
| 544 | reg = wm8711_read_reg_cache(codec, WM8711_LOUT1V); |
| 545 | wm8711_write(codec, WM8711_LOUT1V, reg | 0x0100); |
| 546 | reg = wm8711_read_reg_cache(codec, WM8711_ROUT1V); |
| 547 | wm8711_write(codec, WM8711_ROUT1V, reg | 0x0100); |
| 548 | |
| 549 | wm8711_codec = codec; |
| 550 | |
| 551 | ret = snd_soc_register_codec(codec); |
| 552 | if (ret != 0) { |
| 553 | dev_err(codec->dev, "Failed to register codec: %d\n", ret); |
| 554 | return ret; |
| 555 | } |
| 556 | |
| 557 | ret = snd_soc_register_dai(&wm8711_dai); |
| 558 | if (ret != 0) { |
| 559 | dev_err(codec->dev, "Failed to register DAI: %d\n", ret); |
| 560 | snd_soc_unregister_codec(codec); |
| 561 | return ret; |
| 562 | } |
| 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | static void wm8711_unregister(struct wm8711_priv *wm8711) |
| 568 | { |
| 569 | wm8711_set_bias_level(&wm8711->codec, SND_SOC_BIAS_OFF); |
| 570 | snd_soc_unregister_dai(&wm8711_dai); |
| 571 | snd_soc_unregister_codec(&wm8711->codec); |
| 572 | kfree(wm8711); |
| 573 | wm8711_codec = NULL; |
| 574 | } |
| 575 | |
| 576 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 577 | static __devinit int wm8711_i2c_probe(struct i2c_client *i2c, |
| 578 | const struct i2c_device_id *id) |
| 579 | { |
| 580 | struct wm8711_priv *wm8711; |
| 581 | struct snd_soc_codec *codec; |
| 582 | |
| 583 | wm8711 = kzalloc(sizeof(struct wm8711_priv), GFP_KERNEL); |
| 584 | if (wm8711 == NULL) |
| 585 | return -ENOMEM; |
| 586 | |
| 587 | codec = &wm8711->codec; |
| 588 | codec->hw_write = (hw_write_t)i2c_master_send; |
| 589 | |
| 590 | i2c_set_clientdata(i2c, wm8711); |
| 591 | codec->control_data = i2c; |
| 592 | |
| 593 | codec->dev = &i2c->dev; |
| 594 | |
| 595 | return wm8711_register(wm8711); |
| 596 | } |
| 597 | |
| 598 | static __devexit int wm8711_i2c_remove(struct i2c_client *client) |
| 599 | { |
| 600 | struct wm8711_priv *wm8711 = i2c_get_clientdata(client); |
| 601 | wm8711_unregister(wm8711); |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | static const struct i2c_device_id wm8711_i2c_id[] = { |
| 606 | { "wm8711", 0 }, |
| 607 | { } |
| 608 | }; |
| 609 | MODULE_DEVICE_TABLE(i2c, wm8711_i2c_id); |
| 610 | |
| 611 | static struct i2c_driver wm8711_i2c_driver = { |
| 612 | .driver = { |
| 613 | .name = "WM8711 I2C Codec", |
| 614 | .owner = THIS_MODULE, |
| 615 | }, |
| 616 | .probe = wm8711_i2c_probe, |
| 617 | .remove = __devexit_p(wm8711_i2c_remove), |
| 618 | .id_table = wm8711_i2c_id, |
| 619 | }; |
| 620 | #endif |
| 621 | |
Mike Arthur | bd6d417 | 2009-08-18 20:37:49 +0100 | [diff] [blame] | 622 | static int __init wm8711_modinit(void) |
| 623 | { |
Mark Brown | 318b0b8 | 2009-08-18 20:57:33 +0100 | [diff] [blame^] | 624 | int ret; |
| 625 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 626 | ret = i2c_add_driver(&wm8711_i2c_driver); |
| 627 | if (ret != 0) { |
| 628 | printk(KERN_ERR "Failed to register WM8711 I2C driver: %d\n", |
| 629 | ret); |
| 630 | } |
| 631 | #endif |
| 632 | return 0; |
Mike Arthur | bd6d417 | 2009-08-18 20:37:49 +0100 | [diff] [blame] | 633 | } |
| 634 | module_init(wm8711_modinit); |
| 635 | |
| 636 | static void __exit wm8711_exit(void) |
| 637 | { |
Mark Brown | 318b0b8 | 2009-08-18 20:57:33 +0100 | [diff] [blame^] | 638 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 639 | i2c_del_driver(&wm8711_i2c_driver); |
| 640 | #endif |
Mike Arthur | bd6d417 | 2009-08-18 20:37:49 +0100 | [diff] [blame] | 641 | } |
| 642 | module_exit(wm8711_exit); |
| 643 | |
| 644 | MODULE_DESCRIPTION("ASoC WM8711 driver"); |
| 645 | MODULE_AUTHOR("Mike Arthur"); |
| 646 | MODULE_LICENSE("GPL"); |