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