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