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