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