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