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