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