Mark Brown | 924914e | 2009-08-04 23:50:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | * wm8776.c -- WM8776 ALSA SoC Audio driver |
| 3 | * |
| 4 | * Copyright 2009 Wolfson Microelectronics plc |
| 5 | * |
| 6 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | * TODO: Input ALC/limiter support |
| 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 <linux/spi/spi.h> |
| 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 | #include <sound/tlv.h> |
| 30 | |
| 31 | #include "wm8776.h" |
| 32 | |
| 33 | static struct snd_soc_codec *wm8776_codec; |
| 34 | struct snd_soc_codec_device soc_codec_dev_wm8776; |
| 35 | |
| 36 | /* codec private data */ |
| 37 | struct wm8776_priv { |
| 38 | struct snd_soc_codec codec; |
| 39 | u16 reg_cache[WM8776_CACHEREGNUM]; |
| 40 | int sysclk[2]; |
| 41 | }; |
| 42 | |
| 43 | #ifdef CONFIG_SPI_MASTER |
| 44 | static int wm8776_spi_write(struct spi_device *spi, const char *data, int len); |
| 45 | #endif |
| 46 | |
| 47 | static const u16 wm8776_reg[WM8776_CACHEREGNUM] = { |
| 48 | 0x79, 0x79, 0x79, 0xff, 0xff, /* 4 */ |
| 49 | 0xff, 0x00, 0x90, 0x00, 0x00, /* 9 */ |
| 50 | 0x22, 0x22, 0x22, 0x08, 0xcf, /* 14 */ |
| 51 | 0xcf, 0x7b, 0x00, 0x32, 0x00, /* 19 */ |
| 52 | 0xa6, 0x01, 0x01 |
| 53 | }; |
| 54 | |
Mark Brown | 924914e | 2009-08-04 23:50:16 +0100 | [diff] [blame] | 55 | static int wm8776_reset(struct snd_soc_codec *codec) |
| 56 | { |
Mark Brown | 35b1207 | 2009-08-08 10:37:33 +0100 | [diff] [blame] | 57 | return snd_soc_write(codec, WM8776_RESET, 0); |
Mark Brown | 924914e | 2009-08-04 23:50:16 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static const DECLARE_TLV_DB_SCALE(hp_tlv, -12100, 100, 1); |
| 61 | static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1); |
| 62 | static const DECLARE_TLV_DB_SCALE(adc_tlv, -10350, 50, 1); |
| 63 | |
| 64 | static const struct snd_kcontrol_new wm8776_snd_controls[] = { |
| 65 | SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8776_HPLVOL, WM8776_HPRVOL, |
| 66 | 0, 127, 0, hp_tlv), |
| 67 | SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8776_DACLVOL, WM8776_DACRVOL, |
| 68 | 0, 255, 0, dac_tlv), |
| 69 | SOC_SINGLE("Digital Playback ZC Switch", WM8776_DACCTRL1, 0, 1, 0), |
| 70 | |
| 71 | SOC_SINGLE("Deemphasis Switch", WM8776_DACCTRL2, 0, 1, 0), |
| 72 | |
| 73 | SOC_DOUBLE_R_TLV("Capture Volume", WM8776_ADCLVOL, WM8776_ADCRVOL, |
| 74 | 0, 255, 0, adc_tlv), |
| 75 | SOC_DOUBLE("Capture Switch", WM8776_ADCMUX, 7, 6, 1, 1), |
| 76 | SOC_DOUBLE_R("Capture ZC Switch", WM8776_ADCLVOL, WM8776_ADCRVOL, 8, 1, 0), |
| 77 | SOC_SINGLE("Capture HPF Switch", WM8776_ADCIFCTRL, 8, 1, 1), |
| 78 | }; |
| 79 | |
| 80 | static const struct snd_kcontrol_new inmix_controls[] = { |
| 81 | SOC_DAPM_SINGLE("AIN1 Switch", WM8776_ADCMUX, 0, 1, 0), |
| 82 | SOC_DAPM_SINGLE("AIN2 Switch", WM8776_ADCMUX, 1, 1, 0), |
| 83 | SOC_DAPM_SINGLE("AIN3 Switch", WM8776_ADCMUX, 2, 1, 0), |
| 84 | SOC_DAPM_SINGLE("AIN4 Switch", WM8776_ADCMUX, 3, 1, 0), |
| 85 | SOC_DAPM_SINGLE("AIN5 Switch", WM8776_ADCMUX, 4, 1, 0), |
| 86 | }; |
| 87 | |
| 88 | static const struct snd_kcontrol_new outmix_controls[] = { |
| 89 | SOC_DAPM_SINGLE("DAC Switch", WM8776_OUTMUX, 0, 1, 0), |
| 90 | SOC_DAPM_SINGLE("AUX Switch", WM8776_OUTMUX, 1, 1, 0), |
| 91 | SOC_DAPM_SINGLE("Bypass Switch", WM8776_OUTMUX, 2, 1, 0), |
| 92 | }; |
| 93 | |
| 94 | static const struct snd_soc_dapm_widget wm8776_dapm_widgets[] = { |
| 95 | SND_SOC_DAPM_INPUT("AUX"), |
| 96 | SND_SOC_DAPM_INPUT("AUX"), |
| 97 | |
| 98 | SND_SOC_DAPM_INPUT("AIN1"), |
| 99 | SND_SOC_DAPM_INPUT("AIN2"), |
| 100 | SND_SOC_DAPM_INPUT("AIN3"), |
| 101 | SND_SOC_DAPM_INPUT("AIN4"), |
| 102 | SND_SOC_DAPM_INPUT("AIN5"), |
| 103 | |
| 104 | SND_SOC_DAPM_MIXER("Input Mixer", WM8776_PWRDOWN, 6, 1, |
| 105 | inmix_controls, ARRAY_SIZE(inmix_controls)), |
| 106 | |
| 107 | SND_SOC_DAPM_ADC("ADC", "Capture", WM8776_PWRDOWN, 1, 1), |
| 108 | SND_SOC_DAPM_DAC("DAC", "Playback", WM8776_PWRDOWN, 2, 1), |
| 109 | |
| 110 | SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM, 0, 0, |
| 111 | outmix_controls, ARRAY_SIZE(outmix_controls)), |
| 112 | |
| 113 | SND_SOC_DAPM_PGA("Headphone PGA", WM8776_PWRDOWN, 3, 1, NULL, 0), |
| 114 | |
| 115 | SND_SOC_DAPM_OUTPUT("VOUT"), |
| 116 | |
| 117 | SND_SOC_DAPM_OUTPUT("HPOUTL"), |
| 118 | SND_SOC_DAPM_OUTPUT("HPOUTR"), |
| 119 | }; |
| 120 | |
| 121 | static const struct snd_soc_dapm_route routes[] = { |
| 122 | { "Input Mixer", "AIN1 Switch", "AIN1" }, |
| 123 | { "Input Mixer", "AIN2 Switch", "AIN2" }, |
| 124 | { "Input Mixer", "AIN3 Switch", "AIN3" }, |
| 125 | { "Input Mixer", "AIN4 Switch", "AIN4" }, |
| 126 | { "Input Mixer", "AIN5 Switch", "AIN5" }, |
| 127 | |
| 128 | { "ADC", NULL, "Input Mixer" }, |
| 129 | |
| 130 | { "Output Mixer", "DAC Switch", "DAC" }, |
| 131 | { "Output Mixer", "AUX Switch", "AUX" }, |
| 132 | { "Output Mixer", "Bypass Switch", "Input Mixer" }, |
| 133 | |
| 134 | { "VOUT", NULL, "Output Mixer" }, |
| 135 | |
| 136 | { "Headphone PGA", NULL, "Output Mixer" }, |
| 137 | |
| 138 | { "HPOUTL", NULL, "Headphone PGA" }, |
| 139 | { "HPOUTR", NULL, "Headphone PGA" }, |
| 140 | }; |
| 141 | |
| 142 | static int wm8776_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) |
| 143 | { |
| 144 | struct snd_soc_codec *codec = dai->codec; |
| 145 | int reg, iface, master; |
| 146 | |
| 147 | switch (dai->id) { |
| 148 | case WM8776_DAI_DAC: |
| 149 | reg = WM8776_DACIFCTRL; |
| 150 | master = 0x80; |
| 151 | break; |
| 152 | case WM8776_DAI_ADC: |
| 153 | reg = WM8776_ADCIFCTRL; |
| 154 | master = 0x100; |
| 155 | break; |
| 156 | default: |
| 157 | return -EINVAL; |
| 158 | } |
| 159 | |
| 160 | iface = 0; |
| 161 | |
| 162 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 163 | case SND_SOC_DAIFMT_CBM_CFM: |
| 164 | break; |
| 165 | case SND_SOC_DAIFMT_CBS_CFS: |
| 166 | master = 0; |
| 167 | break; |
| 168 | default: |
| 169 | return -EINVAL; |
| 170 | } |
| 171 | |
| 172 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 173 | case SND_SOC_DAIFMT_I2S: |
| 174 | iface |= 0x0002; |
| 175 | break; |
| 176 | case SND_SOC_DAIFMT_RIGHT_J: |
| 177 | break; |
| 178 | case SND_SOC_DAIFMT_LEFT_J: |
| 179 | iface |= 0x0001; |
| 180 | break; |
| 181 | /* FIXME: CHECK A/B */ |
| 182 | case SND_SOC_DAIFMT_DSP_A: |
| 183 | iface |= 0x0003; |
| 184 | break; |
| 185 | case SND_SOC_DAIFMT_DSP_B: |
| 186 | iface |= 0x0007; |
| 187 | break; |
| 188 | default: |
| 189 | return -EINVAL; |
| 190 | } |
| 191 | |
| 192 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 193 | case SND_SOC_DAIFMT_NB_NF: |
| 194 | break; |
| 195 | case SND_SOC_DAIFMT_IB_IF: |
| 196 | iface |= 0x00c; |
| 197 | break; |
| 198 | case SND_SOC_DAIFMT_IB_NF: |
| 199 | iface |= 0x008; |
| 200 | break; |
| 201 | case SND_SOC_DAIFMT_NB_IF: |
| 202 | iface |= 0x004; |
| 203 | break; |
| 204 | default: |
| 205 | return -EINVAL; |
| 206 | } |
| 207 | |
| 208 | /* Finally, write out the values */ |
| 209 | snd_soc_update_bits(codec, reg, 0xf, iface); |
| 210 | snd_soc_update_bits(codec, WM8776_MSTRCTRL, 0x180, master); |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static int mclk_ratios[] = { |
| 216 | 128, |
| 217 | 192, |
| 218 | 256, |
| 219 | 384, |
| 220 | 512, |
| 221 | 768, |
| 222 | }; |
| 223 | |
| 224 | static int wm8776_hw_params(struct snd_pcm_substream *substream, |
| 225 | struct snd_pcm_hw_params *params, |
| 226 | struct snd_soc_dai *dai) |
| 227 | { |
| 228 | struct snd_soc_codec *codec = dai->codec; |
| 229 | struct wm8776_priv *wm8776 = codec->private_data; |
| 230 | int iface_reg, iface; |
| 231 | int ratio_shift, master; |
| 232 | int i; |
| 233 | |
| 234 | iface = 0; |
| 235 | |
| 236 | switch (dai->id) { |
| 237 | case WM8776_DAI_DAC: |
| 238 | iface_reg = WM8776_DACIFCTRL; |
| 239 | master = 0x80; |
| 240 | ratio_shift = 4; |
| 241 | break; |
| 242 | case WM8776_DAI_ADC: |
| 243 | iface_reg = WM8776_ADCIFCTRL; |
| 244 | master = 0x100; |
| 245 | ratio_shift = 0; |
| 246 | break; |
| 247 | default: |
| 248 | return -EINVAL; |
| 249 | } |
| 250 | |
| 251 | |
| 252 | /* Set word length */ |
| 253 | switch (params_format(params)) { |
| 254 | case SNDRV_PCM_FORMAT_S16_LE: |
| 255 | break; |
| 256 | case SNDRV_PCM_FORMAT_S20_3LE: |
| 257 | iface |= 0x10; |
| 258 | break; |
| 259 | case SNDRV_PCM_FORMAT_S24_LE: |
| 260 | iface |= 0x20; |
| 261 | break; |
| 262 | case SNDRV_PCM_FORMAT_S32_LE: |
| 263 | iface |= 0x30; |
| 264 | break; |
| 265 | } |
| 266 | |
| 267 | /* Only need to set MCLK/LRCLK ratio if we're master */ |
| 268 | if (snd_soc_read(codec, WM8776_MSTRCTRL) & master) { |
| 269 | for (i = 0; i < ARRAY_SIZE(mclk_ratios); i++) { |
| 270 | if (wm8776->sysclk[dai->id] / params_rate(params) |
| 271 | == mclk_ratios[i]) |
| 272 | break; |
| 273 | } |
| 274 | |
| 275 | if (i == ARRAY_SIZE(mclk_ratios)) { |
| 276 | dev_err(codec->dev, |
| 277 | "Unable to configure MCLK ratio %d/%d\n", |
| 278 | wm8776->sysclk[dai->id], params_rate(params)); |
| 279 | return -EINVAL; |
| 280 | } |
| 281 | |
| 282 | dev_dbg(codec->dev, "MCLK is %dfs\n", mclk_ratios[i]); |
| 283 | |
| 284 | snd_soc_update_bits(codec, WM8776_MSTRCTRL, |
| 285 | 0x7 << ratio_shift, i << ratio_shift); |
| 286 | } else { |
| 287 | dev_dbg(codec->dev, "DAI in slave mode\n"); |
| 288 | } |
| 289 | |
| 290 | snd_soc_update_bits(codec, iface_reg, 0x30, iface); |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | static int wm8776_mute(struct snd_soc_dai *dai, int mute) |
| 296 | { |
| 297 | struct snd_soc_codec *codec = dai->codec; |
| 298 | |
| 299 | return snd_soc_write(codec, WM8776_DACMUTE, !!mute); |
| 300 | } |
| 301 | |
| 302 | static int wm8776_set_sysclk(struct snd_soc_dai *dai, |
| 303 | int clk_id, unsigned int freq, int dir) |
| 304 | { |
| 305 | struct snd_soc_codec *codec = dai->codec; |
| 306 | struct wm8776_priv *wm8776 = codec->private_data; |
| 307 | |
| 308 | BUG_ON(dai->id >= ARRAY_SIZE(wm8776->sysclk)); |
| 309 | |
| 310 | wm8776->sysclk[dai->id] = freq; |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | static int wm8776_set_bias_level(struct snd_soc_codec *codec, |
| 316 | enum snd_soc_bias_level level) |
| 317 | { |
| 318 | switch (level) { |
| 319 | case SND_SOC_BIAS_ON: |
| 320 | break; |
| 321 | case SND_SOC_BIAS_PREPARE: |
| 322 | break; |
| 323 | case SND_SOC_BIAS_STANDBY: |
| 324 | if (codec->bias_level == SND_SOC_BIAS_OFF) { |
| 325 | /* Disable the global powerdown; DAPM does the rest */ |
| 326 | snd_soc_update_bits(codec, WM8776_PWRDOWN, 1, 0); |
| 327 | } |
| 328 | |
| 329 | break; |
| 330 | case SND_SOC_BIAS_OFF: |
| 331 | snd_soc_update_bits(codec, WM8776_PWRDOWN, 1, 1); |
| 332 | break; |
| 333 | } |
| 334 | |
| 335 | codec->bias_level = level; |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | #define WM8776_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\ |
| 340 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\ |
| 341 | SNDRV_PCM_RATE_96000) |
| 342 | |
| 343 | |
| 344 | #define WM8776_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ |
| 345 | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE) |
| 346 | |
| 347 | static struct snd_soc_dai_ops wm8776_dac_ops = { |
| 348 | .digital_mute = wm8776_mute, |
| 349 | .hw_params = wm8776_hw_params, |
| 350 | .set_fmt = wm8776_set_fmt, |
| 351 | .set_sysclk = wm8776_set_sysclk, |
| 352 | }; |
| 353 | |
| 354 | static struct snd_soc_dai_ops wm8776_adc_ops = { |
| 355 | .hw_params = wm8776_hw_params, |
| 356 | .set_fmt = wm8776_set_fmt, |
| 357 | .set_sysclk = wm8776_set_sysclk, |
| 358 | }; |
| 359 | |
| 360 | struct snd_soc_dai wm8776_dai[] = { |
| 361 | { |
| 362 | .name = "WM8776 Playback", |
| 363 | .id = WM8776_DAI_DAC, |
| 364 | .playback = { |
| 365 | .stream_name = "Playback", |
| 366 | .channels_min = 2, |
| 367 | .channels_max = 2, |
| 368 | .rates = WM8776_RATES, |
| 369 | .formats = WM8776_FORMATS, |
| 370 | }, |
| 371 | .ops = &wm8776_dac_ops, |
| 372 | }, |
| 373 | { |
| 374 | .name = "WM8776 Capture", |
| 375 | .id = WM8776_DAI_ADC, |
| 376 | .capture = { |
| 377 | .stream_name = "Capture", |
| 378 | .channels_min = 2, |
| 379 | .channels_max = 2, |
| 380 | .rates = WM8776_RATES, |
| 381 | .formats = WM8776_FORMATS, |
| 382 | }, |
| 383 | .ops = &wm8776_adc_ops, |
| 384 | }, |
| 385 | }; |
| 386 | EXPORT_SYMBOL_GPL(wm8776_dai); |
| 387 | |
| 388 | #ifdef CONFIG_PM |
| 389 | static int wm8776_suspend(struct platform_device *pdev, pm_message_t state) |
| 390 | { |
| 391 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 392 | struct snd_soc_codec *codec = socdev->card->codec; |
| 393 | |
| 394 | wm8776_set_bias_level(codec, SND_SOC_BIAS_OFF); |
| 395 | |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | static int wm8776_resume(struct platform_device *pdev) |
| 400 | { |
| 401 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 402 | struct snd_soc_codec *codec = socdev->card->codec; |
| 403 | int i; |
| 404 | u8 data[2]; |
| 405 | u16 *cache = codec->reg_cache; |
| 406 | |
| 407 | /* Sync reg_cache with the hardware */ |
| 408 | for (i = 0; i < ARRAY_SIZE(wm8776_reg); i++) { |
Mark Brown | e0fb28e | 2010-01-02 13:14:23 +0000 | [diff] [blame] | 409 | if (cache[i] == wm8776_reg[i]) |
| 410 | continue; |
Mark Brown | 924914e | 2009-08-04 23:50:16 +0100 | [diff] [blame] | 411 | data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001); |
| 412 | data[1] = cache[i] & 0x00ff; |
| 413 | codec->hw_write(codec->control_data, data, 2); |
| 414 | } |
| 415 | |
| 416 | wm8776_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
| 417 | |
| 418 | return 0; |
| 419 | } |
| 420 | #else |
| 421 | #define wm8776_suspend NULL |
| 422 | #define wm8776_resume NULL |
| 423 | #endif |
| 424 | |
| 425 | static int wm8776_probe(struct platform_device *pdev) |
| 426 | { |
| 427 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 428 | struct snd_soc_codec *codec; |
| 429 | int ret = 0; |
| 430 | |
| 431 | if (wm8776_codec == NULL) { |
| 432 | dev_err(&pdev->dev, "Codec device not registered\n"); |
| 433 | return -ENODEV; |
| 434 | } |
| 435 | |
| 436 | socdev->card->codec = wm8776_codec; |
| 437 | codec = wm8776_codec; |
| 438 | |
| 439 | /* register pcms */ |
| 440 | ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); |
| 441 | if (ret < 0) { |
| 442 | dev_err(codec->dev, "failed to create pcms: %d\n", ret); |
| 443 | goto pcm_err; |
| 444 | } |
| 445 | |
| 446 | snd_soc_add_controls(codec, wm8776_snd_controls, |
| 447 | ARRAY_SIZE(wm8776_snd_controls)); |
| 448 | snd_soc_dapm_new_controls(codec, wm8776_dapm_widgets, |
| 449 | ARRAY_SIZE(wm8776_dapm_widgets)); |
| 450 | snd_soc_dapm_add_routes(codec, routes, ARRAY_SIZE(routes)); |
| 451 | |
Mark Brown | 924914e | 2009-08-04 23:50:16 +0100 | [diff] [blame] | 452 | return ret; |
| 453 | |
Mark Brown | 924914e | 2009-08-04 23:50:16 +0100 | [diff] [blame] | 454 | pcm_err: |
| 455 | return ret; |
| 456 | } |
| 457 | |
| 458 | /* power down chip */ |
| 459 | static int wm8776_remove(struct platform_device *pdev) |
| 460 | { |
| 461 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 462 | |
| 463 | snd_soc_free_pcms(socdev); |
| 464 | snd_soc_dapm_free(socdev); |
| 465 | |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | struct snd_soc_codec_device soc_codec_dev_wm8776 = { |
| 470 | .probe = wm8776_probe, |
| 471 | .remove = wm8776_remove, |
| 472 | .suspend = wm8776_suspend, |
| 473 | .resume = wm8776_resume, |
| 474 | }; |
| 475 | EXPORT_SYMBOL_GPL(soc_codec_dev_wm8776); |
| 476 | |
Mark Brown | 35b1207 | 2009-08-08 10:37:33 +0100 | [diff] [blame] | 477 | static int wm8776_register(struct wm8776_priv *wm8776, |
| 478 | enum snd_soc_control_type control) |
Mark Brown | 924914e | 2009-08-04 23:50:16 +0100 | [diff] [blame] | 479 | { |
| 480 | int ret, i; |
| 481 | struct snd_soc_codec *codec = &wm8776->codec; |
| 482 | |
| 483 | if (wm8776_codec) { |
| 484 | dev_err(codec->dev, "Another WM8776 is registered\n"); |
| 485 | ret = -EINVAL; |
| 486 | goto err; |
| 487 | } |
| 488 | |
| 489 | mutex_init(&codec->mutex); |
| 490 | INIT_LIST_HEAD(&codec->dapm_widgets); |
| 491 | INIT_LIST_HEAD(&codec->dapm_paths); |
| 492 | |
| 493 | codec->private_data = wm8776; |
| 494 | codec->name = "WM8776"; |
| 495 | codec->owner = THIS_MODULE; |
Mark Brown | 924914e | 2009-08-04 23:50:16 +0100 | [diff] [blame] | 496 | codec->bias_level = SND_SOC_BIAS_OFF; |
| 497 | codec->set_bias_level = wm8776_set_bias_level; |
| 498 | codec->dai = wm8776_dai; |
| 499 | codec->num_dai = ARRAY_SIZE(wm8776_dai); |
| 500 | codec->reg_cache_size = WM8776_CACHEREGNUM; |
| 501 | codec->reg_cache = &wm8776->reg_cache; |
| 502 | |
| 503 | memcpy(codec->reg_cache, wm8776_reg, sizeof(wm8776_reg)); |
| 504 | |
Mark Brown | 35b1207 | 2009-08-08 10:37:33 +0100 | [diff] [blame] | 505 | ret = snd_soc_codec_set_cache_io(codec, 7, 9, control); |
| 506 | if (ret < 0) { |
| 507 | dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret); |
| 508 | goto err; |
| 509 | } |
| 510 | |
Mark Brown | 924914e | 2009-08-04 23:50:16 +0100 | [diff] [blame] | 511 | for (i = 0; i < ARRAY_SIZE(wm8776_dai); i++) |
| 512 | wm8776_dai[i].dev = codec->dev; |
| 513 | |
| 514 | ret = wm8776_reset(codec); |
| 515 | if (ret < 0) { |
| 516 | dev_err(codec->dev, "Failed to issue reset: %d\n", ret); |
| 517 | goto err; |
| 518 | } |
| 519 | |
| 520 | wm8776_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
| 521 | |
| 522 | /* Latch the update bits; right channel only since we always |
| 523 | * update both. */ |
| 524 | snd_soc_update_bits(codec, WM8776_HPRVOL, 0x100, 0x100); |
| 525 | snd_soc_update_bits(codec, WM8776_DACRVOL, 0x100, 0x100); |
| 526 | |
| 527 | wm8776_codec = codec; |
| 528 | |
| 529 | ret = snd_soc_register_codec(codec); |
| 530 | if (ret != 0) { |
| 531 | dev_err(codec->dev, "Failed to register codec: %d\n", ret); |
| 532 | goto err; |
| 533 | } |
| 534 | |
| 535 | ret = snd_soc_register_dais(wm8776_dai, ARRAY_SIZE(wm8776_dai)); |
| 536 | if (ret != 0) { |
| 537 | dev_err(codec->dev, "Failed to register DAIs: %d\n", ret); |
| 538 | goto err_codec; |
| 539 | } |
| 540 | |
| 541 | return 0; |
| 542 | |
| 543 | err_codec: |
| 544 | snd_soc_unregister_codec(codec); |
| 545 | err: |
| 546 | kfree(wm8776); |
| 547 | return ret; |
| 548 | } |
| 549 | |
| 550 | static void wm8776_unregister(struct wm8776_priv *wm8776) |
| 551 | { |
| 552 | wm8776_set_bias_level(&wm8776->codec, SND_SOC_BIAS_OFF); |
| 553 | snd_soc_unregister_dais(wm8776_dai, ARRAY_SIZE(wm8776_dai)); |
| 554 | snd_soc_unregister_codec(&wm8776->codec); |
| 555 | kfree(wm8776); |
| 556 | wm8776_codec = NULL; |
| 557 | } |
| 558 | |
| 559 | #if defined(CONFIG_SPI_MASTER) |
| 560 | static int wm8776_spi_write(struct spi_device *spi, const char *data, int len) |
| 561 | { |
| 562 | struct spi_transfer t; |
| 563 | struct spi_message m; |
| 564 | u8 msg[2]; |
| 565 | |
| 566 | if (len <= 0) |
| 567 | return 0; |
| 568 | |
| 569 | msg[0] = data[0]; |
| 570 | msg[1] = data[1]; |
| 571 | |
| 572 | spi_message_init(&m); |
| 573 | memset(&t, 0, (sizeof t)); |
| 574 | |
| 575 | t.tx_buf = &msg[0]; |
| 576 | t.len = len; |
| 577 | |
| 578 | spi_message_add_tail(&t, &m); |
| 579 | spi_sync(spi, &m); |
| 580 | |
| 581 | return len; |
| 582 | } |
| 583 | |
| 584 | static int __devinit wm8776_spi_probe(struct spi_device *spi) |
| 585 | { |
| 586 | struct snd_soc_codec *codec; |
| 587 | struct wm8776_priv *wm8776; |
| 588 | |
| 589 | wm8776 = kzalloc(sizeof(struct wm8776_priv), GFP_KERNEL); |
| 590 | if (wm8776 == NULL) |
| 591 | return -ENOMEM; |
| 592 | |
| 593 | codec = &wm8776->codec; |
| 594 | codec->control_data = spi; |
| 595 | codec->hw_write = (hw_write_t)wm8776_spi_write; |
| 596 | codec->dev = &spi->dev; |
| 597 | |
| 598 | dev_set_drvdata(&spi->dev, wm8776); |
| 599 | |
Mark Brown | 35b1207 | 2009-08-08 10:37:33 +0100 | [diff] [blame] | 600 | return wm8776_register(wm8776, SND_SOC_SPI); |
Mark Brown | 924914e | 2009-08-04 23:50:16 +0100 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | static int __devexit wm8776_spi_remove(struct spi_device *spi) |
| 604 | { |
| 605 | struct wm8776_priv *wm8776 = dev_get_drvdata(&spi->dev); |
| 606 | |
| 607 | wm8776_unregister(wm8776); |
| 608 | |
| 609 | return 0; |
| 610 | } |
| 611 | |
Mark Brown | 924914e | 2009-08-04 23:50:16 +0100 | [diff] [blame] | 612 | static struct spi_driver wm8776_spi_driver = { |
| 613 | .driver = { |
| 614 | .name = "wm8776", |
| 615 | .bus = &spi_bus_type, |
| 616 | .owner = THIS_MODULE, |
| 617 | }, |
| 618 | .probe = wm8776_spi_probe, |
Mark Brown | 924914e | 2009-08-04 23:50:16 +0100 | [diff] [blame] | 619 | .remove = __devexit_p(wm8776_spi_remove), |
| 620 | }; |
| 621 | #endif /* CONFIG_SPI_MASTER */ |
| 622 | |
| 623 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 624 | static __devinit int wm8776_i2c_probe(struct i2c_client *i2c, |
| 625 | const struct i2c_device_id *id) |
| 626 | { |
| 627 | struct wm8776_priv *wm8776; |
| 628 | struct snd_soc_codec *codec; |
| 629 | |
| 630 | wm8776 = kzalloc(sizeof(struct wm8776_priv), GFP_KERNEL); |
| 631 | if (wm8776 == NULL) |
| 632 | return -ENOMEM; |
| 633 | |
| 634 | codec = &wm8776->codec; |
| 635 | codec->hw_write = (hw_write_t)i2c_master_send; |
| 636 | |
| 637 | i2c_set_clientdata(i2c, wm8776); |
| 638 | codec->control_data = i2c; |
| 639 | |
| 640 | codec->dev = &i2c->dev; |
| 641 | |
Mark Brown | 35b1207 | 2009-08-08 10:37:33 +0100 | [diff] [blame] | 642 | return wm8776_register(wm8776, SND_SOC_I2C); |
Mark Brown | 924914e | 2009-08-04 23:50:16 +0100 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | static __devexit int wm8776_i2c_remove(struct i2c_client *client) |
| 646 | { |
| 647 | struct wm8776_priv *wm8776 = i2c_get_clientdata(client); |
| 648 | wm8776_unregister(wm8776); |
| 649 | return 0; |
| 650 | } |
| 651 | |
Mark Brown | 924914e | 2009-08-04 23:50:16 +0100 | [diff] [blame] | 652 | static const struct i2c_device_id wm8776_i2c_id[] = { |
| 653 | { "wm8776", 0 }, |
| 654 | { } |
| 655 | }; |
| 656 | MODULE_DEVICE_TABLE(i2c, wm8776_i2c_id); |
| 657 | |
| 658 | static struct i2c_driver wm8776_i2c_driver = { |
| 659 | .driver = { |
| 660 | .name = "wm8776", |
| 661 | .owner = THIS_MODULE, |
| 662 | }, |
| 663 | .probe = wm8776_i2c_probe, |
| 664 | .remove = __devexit_p(wm8776_i2c_remove), |
Mark Brown | 924914e | 2009-08-04 23:50:16 +0100 | [diff] [blame] | 665 | .id_table = wm8776_i2c_id, |
| 666 | }; |
| 667 | #endif |
| 668 | |
| 669 | static int __init wm8776_modinit(void) |
| 670 | { |
| 671 | int ret; |
| 672 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 673 | ret = i2c_add_driver(&wm8776_i2c_driver); |
| 674 | if (ret != 0) { |
| 675 | printk(KERN_ERR "Failed to register WM8776 I2C driver: %d\n", |
| 676 | ret); |
| 677 | } |
| 678 | #endif |
| 679 | #if defined(CONFIG_SPI_MASTER) |
| 680 | ret = spi_register_driver(&wm8776_spi_driver); |
| 681 | if (ret != 0) { |
| 682 | printk(KERN_ERR "Failed to register WM8776 SPI driver: %d\n", |
| 683 | ret); |
| 684 | } |
| 685 | #endif |
| 686 | return 0; |
| 687 | } |
| 688 | module_init(wm8776_modinit); |
| 689 | |
| 690 | static void __exit wm8776_exit(void) |
| 691 | { |
| 692 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 693 | i2c_del_driver(&wm8776_i2c_driver); |
| 694 | #endif |
| 695 | #if defined(CONFIG_SPI_MASTER) |
| 696 | spi_unregister_driver(&wm8776_spi_driver); |
| 697 | #endif |
| 698 | } |
| 699 | module_exit(wm8776_exit); |
| 700 | |
| 701 | MODULE_DESCRIPTION("ASoC WM8776 driver"); |
| 702 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 703 | MODULE_LICENSE("GPL"); |