Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 1 | /* |
| 2 | * wm8523.c -- WM8523 ALSA SoC Audio driver |
| 3 | * |
| 4 | * Copyright 2009 Wolfson Microelectronics plc |
| 5 | * |
| 6 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> |
| 7 | * |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/moduleparam.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/pm.h> |
| 19 | #include <linux/i2c.h> |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 20 | #include <linux/regulator/consumer.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Mark Brown | bf5a85b | 2011-08-02 13:08:13 +0900 | [diff] [blame] | 22 | #include <linux/of_device.h> |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 23 | #include <sound/core.h> |
| 24 | #include <sound/pcm.h> |
| 25 | #include <sound/pcm_params.h> |
| 26 | #include <sound/soc.h> |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 27 | #include <sound/initval.h> |
| 28 | #include <sound/tlv.h> |
| 29 | |
| 30 | #include "wm8523.h" |
| 31 | |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 32 | #define WM8523_NUM_SUPPLIES 2 |
| 33 | static const char *wm8523_supply_names[WM8523_NUM_SUPPLIES] = { |
| 34 | "AVDD", |
| 35 | "LINEVDD", |
| 36 | }; |
| 37 | |
| 38 | #define WM8523_NUM_RATES 7 |
| 39 | |
| 40 | /* codec private data */ |
| 41 | struct wm8523_priv { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 42 | enum snd_soc_control_type control_type; |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 43 | struct regulator_bulk_data supplies[WM8523_NUM_SUPPLIES]; |
| 44 | unsigned int sysclk; |
| 45 | unsigned int rate_constraint_list[WM8523_NUM_RATES]; |
| 46 | struct snd_pcm_hw_constraint_list rate_constraint; |
| 47 | }; |
| 48 | |
| 49 | static const u16 wm8523_reg[WM8523_REGISTER_COUNT] = { |
| 50 | 0x8523, /* R0 - DEVICE_ID */ |
| 51 | 0x0001, /* R1 - REVISION */ |
| 52 | 0x0000, /* R2 - PSCTRL1 */ |
| 53 | 0x1812, /* R3 - AIF_CTRL1 */ |
| 54 | 0x0000, /* R4 - AIF_CTRL2 */ |
| 55 | 0x0001, /* R5 - DAC_CTRL3 */ |
| 56 | 0x0190, /* R6 - DAC_GAINL */ |
| 57 | 0x0190, /* R7 - DAC_GAINR */ |
| 58 | 0x0000, /* R8 - ZERO_DETECT */ |
| 59 | }; |
| 60 | |
Dimitris Papastamos | d4754ec | 2011-01-13 12:20:37 +0000 | [diff] [blame] | 61 | static int wm8523_volatile_register(struct snd_soc_codec *codec, unsigned int reg) |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 62 | { |
| 63 | switch (reg) { |
| 64 | case WM8523_DEVICE_ID: |
| 65 | case WM8523_REVISION: |
| 66 | return 1; |
| 67 | default: |
| 68 | return 0; |
| 69 | } |
| 70 | } |
| 71 | |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 72 | static int wm8523_reset(struct snd_soc_codec *codec) |
| 73 | { |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 74 | return snd_soc_write(codec, WM8523_DEVICE_ID, 0); |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static const DECLARE_TLV_DB_SCALE(dac_tlv, -10000, 25, 0); |
| 78 | |
| 79 | static const char *wm8523_zd_count_text[] = { |
| 80 | "1024", |
| 81 | "2048", |
| 82 | }; |
| 83 | |
| 84 | static const struct soc_enum wm8523_zc_count = |
| 85 | SOC_ENUM_SINGLE(WM8523_ZERO_DETECT, 0, 2, wm8523_zd_count_text); |
| 86 | |
Mark Brown | 1661699 | 2011-08-22 16:02:43 +0100 | [diff] [blame] | 87 | static const struct snd_kcontrol_new wm8523_controls[] = { |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 88 | SOC_DOUBLE_R_TLV("Playback Volume", WM8523_DAC_GAINL, WM8523_DAC_GAINR, |
| 89 | 0, 448, 0, dac_tlv), |
| 90 | SOC_SINGLE("ZC Switch", WM8523_DAC_CTRL3, 4, 1, 0), |
| 91 | SOC_SINGLE("Playback Deemphasis Switch", WM8523_AIF_CTRL1, 8, 1, 0), |
| 92 | SOC_DOUBLE("Playback Switch", WM8523_DAC_CTRL3, 2, 3, 1, 1), |
| 93 | SOC_SINGLE("Volume Ramp Up Switch", WM8523_DAC_CTRL3, 1, 1, 0), |
| 94 | SOC_SINGLE("Volume Ramp Down Switch", WM8523_DAC_CTRL3, 0, 1, 0), |
| 95 | SOC_ENUM("Zero Detect Count", wm8523_zc_count), |
| 96 | }; |
| 97 | |
| 98 | static const struct snd_soc_dapm_widget wm8523_dapm_widgets[] = { |
| 99 | SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0), |
| 100 | SND_SOC_DAPM_OUTPUT("LINEVOUTL"), |
| 101 | SND_SOC_DAPM_OUTPUT("LINEVOUTR"), |
| 102 | }; |
| 103 | |
Mark Brown | 1661699 | 2011-08-22 16:02:43 +0100 | [diff] [blame] | 104 | static const struct snd_soc_dapm_route wm8523_dapm_routes[] = { |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 105 | { "LINEVOUTL", NULL, "DAC" }, |
| 106 | { "LINEVOUTR", NULL, "DAC" }, |
| 107 | }; |
| 108 | |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 109 | static struct { |
| 110 | int value; |
| 111 | int ratio; |
| 112 | } lrclk_ratios[WM8523_NUM_RATES] = { |
| 113 | { 1, 128 }, |
| 114 | { 2, 192 }, |
| 115 | { 3, 256 }, |
| 116 | { 4, 384 }, |
| 117 | { 5, 512 }, |
| 118 | { 6, 768 }, |
| 119 | { 7, 1152 }, |
| 120 | }; |
| 121 | |
| 122 | static int wm8523_startup(struct snd_pcm_substream *substream, |
| 123 | struct snd_soc_dai *dai) |
| 124 | { |
| 125 | struct snd_soc_codec *codec = dai->codec; |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 126 | struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec); |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 127 | |
| 128 | /* The set of sample rates that can be supported depends on the |
| 129 | * MCLK supplied to the CODEC - enforce this. |
| 130 | */ |
| 131 | if (!wm8523->sysclk) { |
| 132 | dev_err(codec->dev, |
| 133 | "No MCLK configured, call set_sysclk() on init\n"); |
| 134 | return -EINVAL; |
| 135 | } |
| 136 | |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 137 | snd_pcm_hw_constraint_list(substream->runtime, 0, |
| 138 | SNDRV_PCM_HW_PARAM_RATE, |
| 139 | &wm8523->rate_constraint); |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | static int wm8523_hw_params(struct snd_pcm_substream *substream, |
| 145 | struct snd_pcm_hw_params *params, |
| 146 | struct snd_soc_dai *dai) |
| 147 | { |
| 148 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 149 | struct snd_soc_codec *codec = rtd->codec; |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 150 | struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec); |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 151 | int i; |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 152 | u16 aifctrl1 = snd_soc_read(codec, WM8523_AIF_CTRL1); |
| 153 | u16 aifctrl2 = snd_soc_read(codec, WM8523_AIF_CTRL2); |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 154 | |
| 155 | /* Find a supported LRCLK ratio */ |
| 156 | for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) { |
| 157 | if (wm8523->sysclk / params_rate(params) == |
| 158 | lrclk_ratios[i].ratio) |
| 159 | break; |
| 160 | } |
| 161 | |
| 162 | /* Should never happen, should be handled by constraints */ |
| 163 | if (i == ARRAY_SIZE(lrclk_ratios)) { |
| 164 | dev_err(codec->dev, "MCLK/fs ratio %d unsupported\n", |
| 165 | wm8523->sysclk / params_rate(params)); |
| 166 | return -EINVAL; |
| 167 | } |
| 168 | |
| 169 | aifctrl2 &= ~WM8523_SR_MASK; |
| 170 | aifctrl2 |= lrclk_ratios[i].value; |
| 171 | |
| 172 | aifctrl1 &= ~WM8523_WL_MASK; |
| 173 | switch (params_format(params)) { |
| 174 | case SNDRV_PCM_FORMAT_S16_LE: |
| 175 | break; |
| 176 | case SNDRV_PCM_FORMAT_S20_3LE: |
| 177 | aifctrl1 |= 0x8; |
| 178 | break; |
| 179 | case SNDRV_PCM_FORMAT_S24_LE: |
| 180 | aifctrl1 |= 0x10; |
| 181 | break; |
| 182 | case SNDRV_PCM_FORMAT_S32_LE: |
| 183 | aifctrl1 |= 0x18; |
| 184 | break; |
| 185 | } |
| 186 | |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 187 | snd_soc_write(codec, WM8523_AIF_CTRL1, aifctrl1); |
| 188 | snd_soc_write(codec, WM8523_AIF_CTRL2, aifctrl2); |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static int wm8523_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
| 194 | int clk_id, unsigned int freq, int dir) |
| 195 | { |
| 196 | struct snd_soc_codec *codec = codec_dai->codec; |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 197 | struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec); |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 198 | unsigned int val; |
| 199 | int i; |
| 200 | |
| 201 | wm8523->sysclk = freq; |
| 202 | |
| 203 | wm8523->rate_constraint.count = 0; |
| 204 | for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) { |
| 205 | val = freq / lrclk_ratios[i].ratio; |
| 206 | /* Check that it's a standard rate since core can't |
| 207 | * cope with others and having the odd rates confuses |
| 208 | * constraint matching. |
| 209 | */ |
| 210 | switch (val) { |
| 211 | case 8000: |
| 212 | case 11025: |
| 213 | case 16000: |
| 214 | case 22050: |
| 215 | case 32000: |
| 216 | case 44100: |
| 217 | case 48000: |
| 218 | case 64000: |
| 219 | case 88200: |
| 220 | case 96000: |
| 221 | case 176400: |
| 222 | case 192000: |
| 223 | dev_dbg(codec->dev, "Supported sample rate: %dHz\n", |
| 224 | val); |
| 225 | wm8523->rate_constraint_list[i] = val; |
| 226 | wm8523->rate_constraint.count++; |
| 227 | break; |
| 228 | default: |
| 229 | dev_dbg(codec->dev, "Skipping sample rate: %dHz\n", |
| 230 | val); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /* Need at least one supported rate... */ |
| 235 | if (wm8523->rate_constraint.count == 0) |
| 236 | return -EINVAL; |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | |
| 242 | static int wm8523_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 243 | unsigned int fmt) |
| 244 | { |
| 245 | struct snd_soc_codec *codec = codec_dai->codec; |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 246 | u16 aifctrl1 = snd_soc_read(codec, WM8523_AIF_CTRL1); |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 247 | |
| 248 | aifctrl1 &= ~(WM8523_BCLK_INV_MASK | WM8523_LRCLK_INV_MASK | |
| 249 | WM8523_FMT_MASK | WM8523_AIF_MSTR_MASK); |
| 250 | |
| 251 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 252 | case SND_SOC_DAIFMT_CBM_CFM: |
| 253 | aifctrl1 |= WM8523_AIF_MSTR; |
| 254 | break; |
| 255 | case SND_SOC_DAIFMT_CBS_CFS: |
| 256 | break; |
| 257 | default: |
| 258 | return -EINVAL; |
| 259 | } |
| 260 | |
| 261 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 262 | case SND_SOC_DAIFMT_I2S: |
| 263 | aifctrl1 |= 0x0002; |
| 264 | break; |
| 265 | case SND_SOC_DAIFMT_RIGHT_J: |
| 266 | break; |
| 267 | case SND_SOC_DAIFMT_LEFT_J: |
| 268 | aifctrl1 |= 0x0001; |
| 269 | break; |
| 270 | case SND_SOC_DAIFMT_DSP_A: |
| 271 | aifctrl1 |= 0x0003; |
| 272 | break; |
| 273 | case SND_SOC_DAIFMT_DSP_B: |
| 274 | aifctrl1 |= 0x0023; |
| 275 | break; |
| 276 | default: |
| 277 | return -EINVAL; |
| 278 | } |
| 279 | |
| 280 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 281 | case SND_SOC_DAIFMT_NB_NF: |
| 282 | break; |
| 283 | case SND_SOC_DAIFMT_IB_IF: |
| 284 | aifctrl1 |= WM8523_BCLK_INV | WM8523_LRCLK_INV; |
| 285 | break; |
| 286 | case SND_SOC_DAIFMT_IB_NF: |
| 287 | aifctrl1 |= WM8523_BCLK_INV; |
| 288 | break; |
| 289 | case SND_SOC_DAIFMT_NB_IF: |
| 290 | aifctrl1 |= WM8523_LRCLK_INV; |
| 291 | break; |
| 292 | default: |
| 293 | return -EINVAL; |
| 294 | } |
| 295 | |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 296 | snd_soc_write(codec, WM8523_AIF_CTRL1, aifctrl1); |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | static int wm8523_set_bias_level(struct snd_soc_codec *codec, |
| 302 | enum snd_soc_bias_level level) |
| 303 | { |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 304 | struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec); |
Lars-Peter Clausen | beebca31 | 2010-12-28 21:37:57 +0100 | [diff] [blame] | 305 | u16 *reg_cache = codec->reg_cache; |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 306 | int ret, i; |
| 307 | |
| 308 | switch (level) { |
| 309 | case SND_SOC_BIAS_ON: |
| 310 | break; |
| 311 | |
| 312 | case SND_SOC_BIAS_PREPARE: |
| 313 | /* Full power on */ |
| 314 | snd_soc_update_bits(codec, WM8523_PSCTRL1, |
| 315 | WM8523_SYS_ENA_MASK, 3); |
| 316 | break; |
| 317 | |
| 318 | case SND_SOC_BIAS_STANDBY: |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 319 | if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) { |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 320 | ret = regulator_bulk_enable(ARRAY_SIZE(wm8523->supplies), |
| 321 | wm8523->supplies); |
| 322 | if (ret != 0) { |
| 323 | dev_err(codec->dev, |
| 324 | "Failed to enable supplies: %d\n", |
| 325 | ret); |
| 326 | return ret; |
| 327 | } |
| 328 | |
| 329 | /* Initial power up */ |
| 330 | snd_soc_update_bits(codec, WM8523_PSCTRL1, |
| 331 | WM8523_SYS_ENA_MASK, 1); |
| 332 | |
| 333 | /* Sync back default/cached values */ |
| 334 | for (i = WM8523_AIF_CTRL1; |
| 335 | i < WM8523_MAX_REGISTER; i++) |
Lars-Peter Clausen | beebca31 | 2010-12-28 21:37:57 +0100 | [diff] [blame] | 336 | snd_soc_write(codec, i, reg_cache[i]); |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 337 | |
| 338 | |
| 339 | msleep(100); |
| 340 | } |
| 341 | |
| 342 | /* Power up to mute */ |
| 343 | snd_soc_update_bits(codec, WM8523_PSCTRL1, |
| 344 | WM8523_SYS_ENA_MASK, 2); |
| 345 | |
| 346 | break; |
| 347 | |
| 348 | case SND_SOC_BIAS_OFF: |
| 349 | /* The chip runs through the power down sequence for us. */ |
| 350 | snd_soc_update_bits(codec, WM8523_PSCTRL1, |
| 351 | WM8523_SYS_ENA_MASK, 0); |
| 352 | msleep(100); |
| 353 | |
| 354 | regulator_bulk_disable(ARRAY_SIZE(wm8523->supplies), |
| 355 | wm8523->supplies); |
| 356 | break; |
| 357 | } |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 358 | codec->dapm.bias_level = level; |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | #define WM8523_RATES SNDRV_PCM_RATE_8000_192000 |
| 363 | |
| 364 | #define WM8523_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ |
| 365 | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE) |
| 366 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 367 | static const struct snd_soc_dai_ops wm8523_dai_ops = { |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 368 | .startup = wm8523_startup, |
| 369 | .hw_params = wm8523_hw_params, |
| 370 | .set_sysclk = wm8523_set_dai_sysclk, |
| 371 | .set_fmt = wm8523_set_dai_fmt, |
| 372 | }; |
| 373 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 374 | static struct snd_soc_dai_driver wm8523_dai = { |
| 375 | .name = "wm8523-hifi", |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 376 | .playback = { |
| 377 | .stream_name = "Playback", |
| 378 | .channels_min = 2, /* Mono modes not yet supported */ |
| 379 | .channels_max = 2, |
| 380 | .rates = WM8523_RATES, |
| 381 | .formats = WM8523_FORMATS, |
| 382 | }, |
| 383 | .ops = &wm8523_dai_ops, |
| 384 | }; |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 385 | |
| 386 | #ifdef CONFIG_PM |
Lars-Peter Clausen | 84b315e | 2011-12-02 10:18:28 +0100 | [diff] [blame] | 387 | static int wm8523_suspend(struct snd_soc_codec *codec) |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 388 | { |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 389 | wm8523_set_bias_level(codec, SND_SOC_BIAS_OFF); |
| 390 | return 0; |
| 391 | } |
| 392 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 393 | static int wm8523_resume(struct snd_soc_codec *codec) |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 394 | { |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 395 | wm8523_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 396 | return 0; |
| 397 | } |
| 398 | #else |
| 399 | #define wm8523_suspend NULL |
| 400 | #define wm8523_resume NULL |
| 401 | #endif |
| 402 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 403 | static int wm8523_probe(struct snd_soc_codec *codec) |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 404 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 405 | struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec); |
| 406 | int ret, i; |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 407 | |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 408 | wm8523->rate_constraint.list = &wm8523->rate_constraint_list[0]; |
| 409 | wm8523->rate_constraint.count = |
| 410 | ARRAY_SIZE(wm8523->rate_constraint_list); |
| 411 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 412 | ret = snd_soc_codec_set_cache_io(codec, 8, 16, wm8523->control_type); |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 413 | if (ret != 0) { |
| 414 | 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] | 415 | return ret; |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 416 | } |
| 417 | |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 418 | for (i = 0; i < ARRAY_SIZE(wm8523->supplies); i++) |
| 419 | wm8523->supplies[i].supply = wm8523_supply_names[i]; |
| 420 | |
| 421 | ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8523->supplies), |
| 422 | wm8523->supplies); |
| 423 | if (ret != 0) { |
| 424 | dev_err(codec->dev, "Failed to request supplies: %d\n", ret); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 425 | return ret; |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | ret = regulator_bulk_enable(ARRAY_SIZE(wm8523->supplies), |
| 429 | wm8523->supplies); |
| 430 | if (ret != 0) { |
| 431 | dev_err(codec->dev, "Failed to enable supplies: %d\n", ret); |
| 432 | goto err_get; |
| 433 | } |
| 434 | |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 435 | ret = snd_soc_read(codec, WM8523_DEVICE_ID); |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 436 | if (ret < 0) { |
| 437 | dev_err(codec->dev, "Failed to read ID register\n"); |
| 438 | goto err_enable; |
| 439 | } |
| 440 | if (ret != wm8523_reg[WM8523_DEVICE_ID]) { |
| 441 | dev_err(codec->dev, "Device is not a WM8523, ID is %x\n", ret); |
| 442 | ret = -EINVAL; |
| 443 | goto err_enable; |
| 444 | } |
| 445 | |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 446 | ret = snd_soc_read(codec, WM8523_REVISION); |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 447 | if (ret < 0) { |
| 448 | dev_err(codec->dev, "Failed to read revision register\n"); |
| 449 | goto err_enable; |
| 450 | } |
| 451 | dev_info(codec->dev, "revision %c\n", |
| 452 | (ret & WM8523_CHIP_REV_MASK) + 'A'); |
| 453 | |
| 454 | ret = wm8523_reset(codec); |
| 455 | if (ret < 0) { |
| 456 | dev_err(codec->dev, "Failed to issue reset\n"); |
| 457 | goto err_enable; |
| 458 | } |
| 459 | |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 460 | /* Change some default settings - latch VU and enable ZC */ |
Mark Brown | a1b3b5e | 2010-12-24 16:59:30 +0000 | [diff] [blame] | 461 | snd_soc_update_bits(codec, WM8523_DAC_GAINR, |
| 462 | WM8523_DACR_VU, WM8523_DACR_VU); |
| 463 | snd_soc_update_bits(codec, WM8523_DAC_CTRL3, WM8523_ZC, WM8523_ZC); |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 464 | |
| 465 | wm8523_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
| 466 | |
| 467 | /* Bias level configuration will have done an extra enable */ |
| 468 | regulator_bulk_disable(ARRAY_SIZE(wm8523->supplies), wm8523->supplies); |
| 469 | |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 470 | return 0; |
| 471 | |
| 472 | err_enable: |
| 473 | regulator_bulk_disable(ARRAY_SIZE(wm8523->supplies), wm8523->supplies); |
| 474 | err_get: |
| 475 | regulator_bulk_free(ARRAY_SIZE(wm8523->supplies), wm8523->supplies); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 476 | |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 477 | return ret; |
| 478 | } |
| 479 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 480 | static int wm8523_remove(struct snd_soc_codec *codec) |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 481 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 482 | struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec); |
| 483 | |
| 484 | wm8523_set_bias_level(codec, SND_SOC_BIAS_OFF); |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 485 | regulator_bulk_free(ARRAY_SIZE(wm8523->supplies), wm8523->supplies); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 486 | return 0; |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 487 | } |
| 488 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 489 | static struct snd_soc_codec_driver soc_codec_dev_wm8523 = { |
| 490 | .probe = wm8523_probe, |
| 491 | .remove = wm8523_remove, |
| 492 | .suspend = wm8523_suspend, |
| 493 | .resume = wm8523_resume, |
| 494 | .set_bias_level = wm8523_set_bias_level, |
| 495 | .reg_cache_size = WM8523_REGISTER_COUNT, |
| 496 | .reg_word_size = sizeof(u16), |
| 497 | .reg_cache_default = wm8523_reg, |
| 498 | .volatile_register = wm8523_volatile_register, |
Mark Brown | 1661699 | 2011-08-22 16:02:43 +0100 | [diff] [blame] | 499 | |
| 500 | .controls = wm8523_controls, |
| 501 | .num_controls = ARRAY_SIZE(wm8523_controls), |
| 502 | .dapm_widgets = wm8523_dapm_widgets, |
| 503 | .num_dapm_widgets = ARRAY_SIZE(wm8523_dapm_widgets), |
| 504 | .dapm_routes = wm8523_dapm_routes, |
| 505 | .num_dapm_routes = ARRAY_SIZE(wm8523_dapm_routes), |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 506 | }; |
| 507 | |
Mark Brown | bf5a85b | 2011-08-02 13:08:13 +0900 | [diff] [blame] | 508 | static const struct of_device_id wm8523_of_match[] = { |
| 509 | { .compatible = "wlf,wm8523" }, |
| 510 | { }, |
| 511 | }; |
| 512 | |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 513 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 514 | static __devinit int wm8523_i2c_probe(struct i2c_client *i2c, |
| 515 | const struct i2c_device_id *id) |
| 516 | { |
| 517 | struct wm8523_priv *wm8523; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 518 | int ret; |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 519 | |
| 520 | wm8523 = kzalloc(sizeof(struct wm8523_priv), GFP_KERNEL); |
| 521 | if (wm8523 == NULL) |
| 522 | return -ENOMEM; |
| 523 | |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 524 | i2c_set_clientdata(i2c, wm8523); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 525 | wm8523->control_type = SND_SOC_I2C; |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 526 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 527 | ret = snd_soc_register_codec(&i2c->dev, |
| 528 | &soc_codec_dev_wm8523, &wm8523_dai, 1); |
| 529 | if (ret < 0) |
| 530 | kfree(wm8523); |
| 531 | return ret; |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 532 | |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | static __devexit int wm8523_i2c_remove(struct i2c_client *client) |
| 536 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 537 | snd_soc_unregister_codec(&client->dev); |
| 538 | kfree(i2c_get_clientdata(client)); |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 539 | return 0; |
| 540 | } |
| 541 | |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 542 | static const struct i2c_device_id wm8523_i2c_id[] = { |
| 543 | { "wm8523", 0 }, |
| 544 | { } |
| 545 | }; |
| 546 | MODULE_DEVICE_TABLE(i2c, wm8523_i2c_id); |
| 547 | |
| 548 | static struct i2c_driver wm8523_i2c_driver = { |
| 549 | .driver = { |
Mark Brown | 9665408 | 2011-08-02 13:04:14 +0900 | [diff] [blame] | 550 | .name = "wm8523", |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 551 | .owner = THIS_MODULE, |
Mark Brown | bf5a85b | 2011-08-02 13:08:13 +0900 | [diff] [blame] | 552 | .of_match_table = wm8523_of_match, |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 553 | }, |
| 554 | .probe = wm8523_i2c_probe, |
| 555 | .remove = __devexit_p(wm8523_i2c_remove), |
Mark Brown | 1dcf98f | 2009-07-01 18:28:54 +0100 | [diff] [blame] | 556 | .id_table = wm8523_i2c_id, |
| 557 | }; |
| 558 | #endif |
| 559 | |
| 560 | static int __init wm8523_modinit(void) |
| 561 | { |
| 562 | int ret; |
| 563 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 564 | ret = i2c_add_driver(&wm8523_i2c_driver); |
| 565 | if (ret != 0) { |
| 566 | printk(KERN_ERR "Failed to register WM8523 I2C driver: %d\n", |
| 567 | ret); |
| 568 | } |
| 569 | #endif |
| 570 | return 0; |
| 571 | } |
| 572 | module_init(wm8523_modinit); |
| 573 | |
| 574 | static void __exit wm8523_exit(void) |
| 575 | { |
| 576 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 577 | i2c_del_driver(&wm8523_i2c_driver); |
| 578 | #endif |
| 579 | } |
| 580 | module_exit(wm8523_exit); |
| 581 | |
| 582 | MODULE_DESCRIPTION("ASoC WM8523 driver"); |
| 583 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 584 | MODULE_LICENSE("GPL"); |