Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 1 | /* |
| 2 | * wm8731.c -- WM8731 ALSA SoC Audio driver |
| 3 | * |
| 4 | * Copyright 2005 Openedhand Ltd. |
Mark Brown | 656baae | 2012-05-23 12:39:07 +0100 | [diff] [blame] | 5 | * Copyright 2006-12 Wolfson Microelectronics, plc |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 6 | * |
| 7 | * Author: Richard Purdie <richard@openedhand.com> |
| 8 | * |
| 9 | * Based on wm8753.c by Liam Girdwood |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License version 2 as |
| 13 | * published by the Free Software Foundation. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/moduleparam.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/pm.h> |
| 21 | #include <linux/i2c.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Mark Brown | 05d448e | 2011-11-21 12:10:03 +0000 | [diff] [blame] | 23 | #include <linux/regmap.h> |
Mark Brown | 7dea7c0 | 2009-10-26 15:20:17 +0000 | [diff] [blame] | 24 | #include <linux/regulator/consumer.h> |
Cliff Cai | d2a4035 | 2008-09-01 18:47:03 +0100 | [diff] [blame] | 25 | #include <linux/spi/spi.h> |
Mark Brown | a7f96e4 | 2011-07-26 21:00:13 +0100 | [diff] [blame] | 26 | #include <linux/of_device.h> |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 27 | #include <sound/core.h> |
| 28 | #include <sound/pcm.h> |
| 29 | #include <sound/pcm_params.h> |
| 30 | #include <sound/soc.h> |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 31 | #include <sound/initval.h> |
Mark Brown | d00efa6 | 2009-07-08 16:53:12 +0100 | [diff] [blame] | 32 | #include <sound/tlv.h> |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 33 | |
| 34 | #include "wm8731.h" |
| 35 | |
Mark Brown | 7dea7c0 | 2009-10-26 15:20:17 +0000 | [diff] [blame] | 36 | #define WM8731_NUM_SUPPLIES 4 |
| 37 | static const char *wm8731_supply_names[WM8731_NUM_SUPPLIES] = { |
| 38 | "AVDD", |
| 39 | "HPVDD", |
| 40 | "DCVDD", |
| 41 | "DBVDD", |
| 42 | }; |
| 43 | |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 44 | /* codec private data */ |
| 45 | struct wm8731_priv { |
Mark Brown | 05d448e | 2011-11-21 12:10:03 +0000 | [diff] [blame] | 46 | struct regmap *regmap; |
Mark Brown | 7dea7c0 | 2009-10-26 15:20:17 +0000 | [diff] [blame] | 47 | struct regulator_bulk_data supplies[WM8731_NUM_SUPPLIES]; |
Richard Genoud | 0890c2b | 2013-07-30 11:59:45 +0200 | [diff] [blame] | 48 | const struct snd_pcm_hw_constraint_list *constraints; |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 49 | unsigned int sysclk; |
Mark Brown | 9745e82 | 2010-08-17 23:40:24 +0100 | [diff] [blame] | 50 | int sysclk_type; |
Mark Brown | dd31b31 | 2010-12-02 11:44:00 +0000 | [diff] [blame] | 51 | int playback_fs; |
| 52 | bool deemph; |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 53 | }; |
| 54 | |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 55 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 56 | /* |
| 57 | * wm8731 register cache |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 58 | */ |
Mark Brown | 05d448e | 2011-11-21 12:10:03 +0000 | [diff] [blame] | 59 | static const struct reg_default wm8731_reg_defaults[] = { |
| 60 | { 0, 0x0097 }, |
| 61 | { 1, 0x0097 }, |
| 62 | { 2, 0x0079 }, |
| 63 | { 3, 0x0079 }, |
| 64 | { 4, 0x000a }, |
| 65 | { 5, 0x0008 }, |
| 66 | { 6, 0x009f }, |
| 67 | { 7, 0x000a }, |
| 68 | { 8, 0x0000 }, |
| 69 | { 9, 0x0000 }, |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 70 | }; |
| 71 | |
Mark Brown | 05d448e | 2011-11-21 12:10:03 +0000 | [diff] [blame] | 72 | static bool wm8731_volatile(struct device *dev, unsigned int reg) |
| 73 | { |
| 74 | return reg == WM8731_RESET; |
| 75 | } |
| 76 | |
| 77 | static bool wm8731_writeable(struct device *dev, unsigned int reg) |
| 78 | { |
| 79 | return reg <= WM8731_RESET; |
| 80 | } |
| 81 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 82 | #define wm8731_reset(c) snd_soc_write(c, WM8731_RESET, 0) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 83 | |
| 84 | static const char *wm8731_input_select[] = {"Line In", "Mic"}; |
Mark Brown | 59f7297 | 2010-11-30 21:03:44 +0000 | [diff] [blame] | 85 | |
Takashi Iwai | 9e74b14 | 2014-02-18 10:36:16 +0100 | [diff] [blame] | 86 | static SOC_ENUM_SINGLE_DECL(wm8731_insel_enum, |
| 87 | WM8731_APANA, 2, wm8731_input_select); |
Mark Brown | 59f7297 | 2010-11-30 21:03:44 +0000 | [diff] [blame] | 88 | |
Mark Brown | dd31b31 | 2010-12-02 11:44:00 +0000 | [diff] [blame] | 89 | static int wm8731_deemph[] = { 0, 32000, 44100, 48000 }; |
Mark Brown | 59f7297 | 2010-11-30 21:03:44 +0000 | [diff] [blame] | 90 | |
Mark Brown | dd31b31 | 2010-12-02 11:44:00 +0000 | [diff] [blame] | 91 | static int wm8731_set_deemph(struct snd_soc_codec *codec) |
| 92 | { |
| 93 | struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); |
| 94 | int val, i, best; |
| 95 | |
| 96 | /* If we're using deemphasis select the nearest available sample |
| 97 | * rate. |
| 98 | */ |
| 99 | if (wm8731->deemph) { |
| 100 | best = 1; |
| 101 | for (i = 2; i < ARRAY_SIZE(wm8731_deemph); i++) { |
| 102 | if (abs(wm8731_deemph[i] - wm8731->playback_fs) < |
| 103 | abs(wm8731_deemph[best] - wm8731->playback_fs)) |
| 104 | best = i; |
| 105 | } |
| 106 | |
| 107 | val = best << 1; |
| 108 | } else { |
| 109 | best = 0; |
| 110 | val = 0; |
| 111 | } |
| 112 | |
| 113 | dev_dbg(codec->dev, "Set deemphasis %d (%dHz)\n", |
| 114 | best, wm8731_deemph[best]); |
| 115 | |
| 116 | return snd_soc_update_bits(codec, WM8731_APDIGI, 0x6, val); |
| 117 | } |
| 118 | |
| 119 | static int wm8731_get_deemph(struct snd_kcontrol *kcontrol, |
| 120 | struct snd_ctl_elem_value *ucontrol) |
| 121 | { |
Lars-Peter Clausen | ea53bf7 | 2014-03-18 09:02:04 +0100 | [diff] [blame^] | 122 | struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol); |
Mark Brown | dd31b31 | 2010-12-02 11:44:00 +0000 | [diff] [blame] | 123 | struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); |
| 124 | |
| 125 | ucontrol->value.enumerated.item[0] = wm8731->deemph; |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static int wm8731_put_deemph(struct snd_kcontrol *kcontrol, |
| 131 | struct snd_ctl_elem_value *ucontrol) |
| 132 | { |
Lars-Peter Clausen | ea53bf7 | 2014-03-18 09:02:04 +0100 | [diff] [blame^] | 133 | struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol); |
Mark Brown | dd31b31 | 2010-12-02 11:44:00 +0000 | [diff] [blame] | 134 | struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); |
| 135 | int deemph = ucontrol->value.enumerated.item[0]; |
| 136 | int ret = 0; |
| 137 | |
| 138 | if (deemph > 1) |
| 139 | return -EINVAL; |
| 140 | |
| 141 | mutex_lock(&codec->mutex); |
| 142 | if (wm8731->deemph != deemph) { |
| 143 | wm8731->deemph = deemph; |
| 144 | |
| 145 | wm8731_set_deemph(codec); |
| 146 | |
| 147 | ret = 1; |
| 148 | } |
| 149 | mutex_unlock(&codec->mutex); |
| 150 | |
| 151 | return ret; |
| 152 | } |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 153 | |
Mark Brown | d00efa6 | 2009-07-08 16:53:12 +0100 | [diff] [blame] | 154 | static const DECLARE_TLV_DB_SCALE(in_tlv, -3450, 150, 0); |
| 155 | static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -1500, 300, 0); |
| 156 | static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1); |
Mark Brown | d921184 | 2010-11-30 21:03:09 +0000 | [diff] [blame] | 157 | static const DECLARE_TLV_DB_SCALE(mic_tlv, 0, 2000, 0); |
Mark Brown | d00efa6 | 2009-07-08 16:53:12 +0100 | [diff] [blame] | 158 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 159 | static const struct snd_kcontrol_new wm8731_snd_controls[] = { |
| 160 | |
Mark Brown | d00efa6 | 2009-07-08 16:53:12 +0100 | [diff] [blame] | 161 | SOC_DOUBLE_R_TLV("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V, |
| 162 | 0, 127, 0, out_tlv), |
Liam Girdwood | bd903b6 | 2006-11-09 16:35:01 +0100 | [diff] [blame] | 163 | SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V, |
| 164 | 7, 1, 0), |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 165 | |
Mark Brown | d00efa6 | 2009-07-08 16:53:12 +0100 | [diff] [blame] | 166 | SOC_DOUBLE_R_TLV("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0, |
| 167 | in_tlv), |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 168 | SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1), |
| 169 | |
Mark Brown | d921184 | 2010-11-30 21:03:09 +0000 | [diff] [blame] | 170 | SOC_SINGLE_TLV("Mic Boost Volume", WM8731_APANA, 0, 1, 0, mic_tlv), |
Mark Brown | ef38ed8 | 2009-07-08 17:05:43 +0100 | [diff] [blame] | 171 | SOC_SINGLE("Mic Capture Switch", WM8731_APANA, 1, 1, 1), |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 172 | |
Mark Brown | d00efa6 | 2009-07-08 16:53:12 +0100 | [diff] [blame] | 173 | SOC_SINGLE_TLV("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1, |
| 174 | sidetone_tlv), |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 175 | |
| 176 | SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1), |
| 177 | SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0), |
| 178 | |
Mark Brown | dd31b31 | 2010-12-02 11:44:00 +0000 | [diff] [blame] | 179 | SOC_SINGLE_BOOL_EXT("Playback Deemphasis Switch", 0, |
| 180 | wm8731_get_deemph, wm8731_put_deemph), |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 181 | }; |
| 182 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 183 | /* Output Mixer */ |
| 184 | static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = { |
| 185 | SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0), |
| 186 | SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0), |
| 187 | SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0), |
| 188 | }; |
| 189 | |
| 190 | /* Input mux */ |
| 191 | static const struct snd_kcontrol_new wm8731_input_mux_controls = |
Mark Brown | 59f7297 | 2010-11-30 21:03:44 +0000 | [diff] [blame] | 192 | SOC_DAPM_ENUM("Input Select", wm8731_insel_enum); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 193 | |
| 194 | static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = { |
Mark Brown | 8a27bd9 | 2011-07-04 10:27:51 -0700 | [diff] [blame] | 195 | SND_SOC_DAPM_SUPPLY("ACTIVE",WM8731_ACTIVE, 0, 0, NULL, 0), |
Mark Brown | 9745e82 | 2010-08-17 23:40:24 +0100 | [diff] [blame] | 196 | SND_SOC_DAPM_SUPPLY("OSC", WM8731_PWR, 5, 1, NULL, 0), |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 197 | SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1, |
| 198 | &wm8731_output_mixer_controls[0], |
| 199 | ARRAY_SIZE(wm8731_output_mixer_controls)), |
| 200 | SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1), |
| 201 | SND_SOC_DAPM_OUTPUT("LOUT"), |
| 202 | SND_SOC_DAPM_OUTPUT("LHPOUT"), |
| 203 | SND_SOC_DAPM_OUTPUT("ROUT"), |
| 204 | SND_SOC_DAPM_OUTPUT("RHPOUT"), |
| 205 | SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1), |
| 206 | SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls), |
| 207 | SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0), |
| 208 | SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1), |
| 209 | SND_SOC_DAPM_INPUT("MICIN"), |
| 210 | SND_SOC_DAPM_INPUT("RLINEIN"), |
| 211 | SND_SOC_DAPM_INPUT("LLINEIN"), |
| 212 | }; |
| 213 | |
Mark Brown | 9745e82 | 2010-08-17 23:40:24 +0100 | [diff] [blame] | 214 | static int wm8731_check_osc(struct snd_soc_dapm_widget *source, |
| 215 | struct snd_soc_dapm_widget *sink) |
| 216 | { |
| 217 | struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(source->codec); |
| 218 | |
Nicolas Ferre | 5a195b4 | 2011-05-24 11:50:57 +0200 | [diff] [blame] | 219 | return wm8731->sysclk_type == WM8731_SYSCLK_XTAL; |
Mark Brown | 9745e82 | 2010-08-17 23:40:24 +0100 | [diff] [blame] | 220 | } |
| 221 | |
Mark Brown | 5e251ae | 2011-03-27 14:33:03 +0100 | [diff] [blame] | 222 | static const struct snd_soc_dapm_route wm8731_intercon[] = { |
Mark Brown | 9745e82 | 2010-08-17 23:40:24 +0100 | [diff] [blame] | 223 | {"DAC", NULL, "OSC", wm8731_check_osc}, |
| 224 | {"ADC", NULL, "OSC", wm8731_check_osc}, |
Mark Brown | 8a27bd9 | 2011-07-04 10:27:51 -0700 | [diff] [blame] | 225 | {"DAC", NULL, "ACTIVE"}, |
| 226 | {"ADC", NULL, "ACTIVE"}, |
Mark Brown | 9745e82 | 2010-08-17 23:40:24 +0100 | [diff] [blame] | 227 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 228 | /* output mixer */ |
| 229 | {"Output Mixer", "Line Bypass Switch", "Line Input"}, |
| 230 | {"Output Mixer", "HiFi Playback Switch", "DAC"}, |
| 231 | {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"}, |
| 232 | |
| 233 | /* outputs */ |
| 234 | {"RHPOUT", NULL, "Output Mixer"}, |
| 235 | {"ROUT", NULL, "Output Mixer"}, |
| 236 | {"LHPOUT", NULL, "Output Mixer"}, |
| 237 | {"LOUT", NULL, "Output Mixer"}, |
| 238 | |
| 239 | /* input mux */ |
| 240 | {"Input Mux", "Line In", "Line Input"}, |
| 241 | {"Input Mux", "Mic", "Mic Bias"}, |
| 242 | {"ADC", NULL, "Input Mux"}, |
| 243 | |
| 244 | /* inputs */ |
| 245 | {"Line Input", NULL, "LLINEIN"}, |
| 246 | {"Line Input", NULL, "RLINEIN"}, |
| 247 | {"Mic Bias", NULL, "MICIN"}, |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 248 | }; |
| 249 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 250 | struct _coeff_div { |
| 251 | u32 mclk; |
| 252 | u32 rate; |
| 253 | u16 fs; |
| 254 | u8 sr:4; |
| 255 | u8 bosr:1; |
| 256 | u8 usb:1; |
| 257 | }; |
| 258 | |
| 259 | /* codec mclk clock divider coefficients */ |
| 260 | static const struct _coeff_div coeff_div[] = { |
| 261 | /* 48k */ |
| 262 | {12288000, 48000, 256, 0x0, 0x0, 0x0}, |
| 263 | {18432000, 48000, 384, 0x0, 0x1, 0x0}, |
| 264 | {12000000, 48000, 250, 0x0, 0x0, 0x1}, |
| 265 | |
| 266 | /* 32k */ |
| 267 | {12288000, 32000, 384, 0x6, 0x0, 0x0}, |
| 268 | {18432000, 32000, 576, 0x6, 0x1, 0x0}, |
Frank Mandarino | 298a2c7 | 2007-01-31 10:02:56 +0100 | [diff] [blame] | 269 | {12000000, 32000, 375, 0x6, 0x0, 0x1}, |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 270 | |
| 271 | /* 8k */ |
| 272 | {12288000, 8000, 1536, 0x3, 0x0, 0x0}, |
| 273 | {18432000, 8000, 2304, 0x3, 0x1, 0x0}, |
| 274 | {11289600, 8000, 1408, 0xb, 0x0, 0x0}, |
| 275 | {16934400, 8000, 2112, 0xb, 0x1, 0x0}, |
| 276 | {12000000, 8000, 1500, 0x3, 0x0, 0x1}, |
| 277 | |
| 278 | /* 96k */ |
| 279 | {12288000, 96000, 128, 0x7, 0x0, 0x0}, |
| 280 | {18432000, 96000, 192, 0x7, 0x1, 0x0}, |
| 281 | {12000000, 96000, 125, 0x7, 0x0, 0x1}, |
| 282 | |
| 283 | /* 44.1k */ |
| 284 | {11289600, 44100, 256, 0x8, 0x0, 0x0}, |
| 285 | {16934400, 44100, 384, 0x8, 0x1, 0x0}, |
| 286 | {12000000, 44100, 272, 0x8, 0x1, 0x1}, |
| 287 | |
| 288 | /* 88.2k */ |
| 289 | {11289600, 88200, 128, 0xf, 0x0, 0x0}, |
| 290 | {16934400, 88200, 192, 0xf, 0x1, 0x0}, |
| 291 | {12000000, 88200, 136, 0xf, 0x1, 0x1}, |
| 292 | }; |
| 293 | |
Richard Genoud | 0890c2b | 2013-07-30 11:59:45 +0200 | [diff] [blame] | 294 | /* rates constraints */ |
| 295 | static const unsigned int wm8731_rates_12000000[] = { |
| 296 | 8000, 32000, 44100, 48000, 96000, 88200, |
| 297 | }; |
| 298 | |
| 299 | static const unsigned int wm8731_rates_12288000_18432000[] = { |
| 300 | 8000, 32000, 48000, 96000, |
| 301 | }; |
| 302 | |
| 303 | static const unsigned int wm8731_rates_11289600_16934400[] = { |
| 304 | 8000, 44100, 88200, |
| 305 | }; |
| 306 | |
| 307 | static const struct snd_pcm_hw_constraint_list wm8731_constraints_12000000 = { |
| 308 | .list = wm8731_rates_12000000, |
| 309 | .count = ARRAY_SIZE(wm8731_rates_12000000), |
| 310 | }; |
| 311 | |
| 312 | static const |
| 313 | struct snd_pcm_hw_constraint_list wm8731_constraints_12288000_18432000 = { |
| 314 | .list = wm8731_rates_12288000_18432000, |
| 315 | .count = ARRAY_SIZE(wm8731_rates_12288000_18432000), |
| 316 | }; |
| 317 | |
| 318 | static const |
| 319 | struct snd_pcm_hw_constraint_list wm8731_constraints_11289600_16934400 = { |
| 320 | .list = wm8731_rates_11289600_16934400, |
| 321 | .count = ARRAY_SIZE(wm8731_rates_11289600_16934400), |
| 322 | }; |
| 323 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 324 | static inline int get_coeff(int mclk, int rate) |
| 325 | { |
| 326 | int i; |
| 327 | |
| 328 | for (i = 0; i < ARRAY_SIZE(coeff_div); i++) { |
| 329 | if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk) |
| 330 | return i; |
| 331 | } |
| 332 | return 0; |
| 333 | } |
| 334 | |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 335 | static int wm8731_hw_params(struct snd_pcm_substream *substream, |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 336 | struct snd_pcm_hw_params *params, |
| 337 | struct snd_soc_dai *dai) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 338 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 339 | struct snd_soc_codec *codec = dai->codec; |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 340 | struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 341 | u16 iface = snd_soc_read(codec, WM8731_IFACE) & 0xfff3; |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 342 | int i = get_coeff(wm8731->sysclk, params_rate(params)); |
| 343 | u16 srate = (coeff_div[i].sr << 2) | |
| 344 | (coeff_div[i].bosr << 1) | coeff_div[i].usb; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 345 | |
Mark Brown | dd31b31 | 2010-12-02 11:44:00 +0000 | [diff] [blame] | 346 | wm8731->playback_fs = params_rate(params); |
| 347 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 348 | snd_soc_write(codec, WM8731_SRATE, srate); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 349 | |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 350 | /* bit size */ |
| 351 | switch (params_format(params)) { |
| 352 | case SNDRV_PCM_FORMAT_S16_LE: |
| 353 | break; |
| 354 | case SNDRV_PCM_FORMAT_S20_3LE: |
| 355 | iface |= 0x0004; |
| 356 | break; |
| 357 | case SNDRV_PCM_FORMAT_S24_LE: |
| 358 | iface |= 0x0008; |
| 359 | break; |
| 360 | } |
| 361 | |
Mark Brown | dd31b31 | 2010-12-02 11:44:00 +0000 | [diff] [blame] | 362 | wm8731_set_deemph(codec); |
| 363 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 364 | snd_soc_write(codec, WM8731_IFACE, iface); |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 365 | return 0; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 366 | } |
| 367 | |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 368 | static int wm8731_mute(struct snd_soc_dai *dai, int mute) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 369 | { |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 370 | struct snd_soc_codec *codec = dai->codec; |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 371 | u16 mute_reg = snd_soc_read(codec, WM8731_APDIGI) & 0xfff7; |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 372 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 373 | if (mute) |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 374 | snd_soc_write(codec, WM8731_APDIGI, mute_reg | 0x8); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 375 | else |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 376 | snd_soc_write(codec, WM8731_APDIGI, mute_reg); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 377 | return 0; |
| 378 | } |
| 379 | |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 380 | static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 381 | int clk_id, unsigned int freq, int dir) |
| 382 | { |
| 383 | struct snd_soc_codec *codec = codec_dai->codec; |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 384 | struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 385 | |
Mark Brown | 9745e82 | 2010-08-17 23:40:24 +0100 | [diff] [blame] | 386 | switch (clk_id) { |
| 387 | case WM8731_SYSCLK_XTAL: |
| 388 | case WM8731_SYSCLK_MCLK: |
| 389 | wm8731->sysclk_type = clk_id; |
| 390 | break; |
| 391 | default: |
| 392 | return -EINVAL; |
| 393 | } |
| 394 | |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 395 | switch (freq) { |
Richard Genoud | 0890c2b | 2013-07-30 11:59:45 +0200 | [diff] [blame] | 396 | case 0: |
| 397 | wm8731->constraints = NULL; |
| 398 | break; |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 399 | case 12000000: |
Richard Genoud | 0890c2b | 2013-07-30 11:59:45 +0200 | [diff] [blame] | 400 | wm8731->constraints = &wm8731_constraints_12000000; |
| 401 | break; |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 402 | case 12288000: |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 403 | case 18432000: |
Richard Genoud | 0890c2b | 2013-07-30 11:59:45 +0200 | [diff] [blame] | 404 | wm8731->constraints = &wm8731_constraints_12288000_18432000; |
| 405 | break; |
| 406 | case 16934400: |
| 407 | case 11289600: |
| 408 | wm8731->constraints = &wm8731_constraints_11289600_16934400; |
Mark Brown | 9745e82 | 2010-08-17 23:40:24 +0100 | [diff] [blame] | 409 | break; |
| 410 | default: |
| 411 | return -EINVAL; |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 412 | } |
Mark Brown | 9745e82 | 2010-08-17 23:40:24 +0100 | [diff] [blame] | 413 | |
Richard Genoud | 0890c2b | 2013-07-30 11:59:45 +0200 | [diff] [blame] | 414 | wm8731->sysclk = freq; |
| 415 | |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 416 | snd_soc_dapm_sync(&codec->dapm); |
Mark Brown | 9745e82 | 2010-08-17 23:40:24 +0100 | [diff] [blame] | 417 | |
| 418 | return 0; |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 422 | static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai, |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 423 | unsigned int fmt) |
| 424 | { |
| 425 | struct snd_soc_codec *codec = codec_dai->codec; |
| 426 | u16 iface = 0; |
| 427 | |
| 428 | /* set master/slave audio interface */ |
| 429 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 430 | case SND_SOC_DAIFMT_CBM_CFM: |
| 431 | iface |= 0x0040; |
| 432 | break; |
| 433 | case SND_SOC_DAIFMT_CBS_CFS: |
| 434 | break; |
| 435 | default: |
| 436 | return -EINVAL; |
| 437 | } |
| 438 | |
| 439 | /* interface format */ |
| 440 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 441 | case SND_SOC_DAIFMT_I2S: |
| 442 | iface |= 0x0002; |
| 443 | break; |
| 444 | case SND_SOC_DAIFMT_RIGHT_J: |
| 445 | break; |
| 446 | case SND_SOC_DAIFMT_LEFT_J: |
| 447 | iface |= 0x0001; |
| 448 | break; |
| 449 | case SND_SOC_DAIFMT_DSP_A: |
Bo Shen | b4af6ef | 2013-12-03 18:04:54 +0800 | [diff] [blame] | 450 | iface |= 0x0013; |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 451 | break; |
| 452 | case SND_SOC_DAIFMT_DSP_B: |
Bo Shen | b4af6ef | 2013-12-03 18:04:54 +0800 | [diff] [blame] | 453 | iface |= 0x0003; |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 454 | break; |
| 455 | default: |
| 456 | return -EINVAL; |
| 457 | } |
| 458 | |
| 459 | /* clock inversion */ |
| 460 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 461 | case SND_SOC_DAIFMT_NB_NF: |
| 462 | break; |
| 463 | case SND_SOC_DAIFMT_IB_IF: |
| 464 | iface |= 0x0090; |
| 465 | break; |
| 466 | case SND_SOC_DAIFMT_IB_NF: |
| 467 | iface |= 0x0080; |
| 468 | break; |
| 469 | case SND_SOC_DAIFMT_NB_IF: |
| 470 | iface |= 0x0010; |
| 471 | break; |
| 472 | default: |
| 473 | return -EINVAL; |
| 474 | } |
| 475 | |
| 476 | /* set iface */ |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 477 | snd_soc_write(codec, WM8731_IFACE, iface); |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 478 | return 0; |
| 479 | } |
| 480 | |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 481 | static int wm8731_set_bias_level(struct snd_soc_codec *codec, |
| 482 | enum snd_soc_bias_level level) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 483 | { |
Mark Brown | 06ae998 | 2010-05-07 19:14:45 +0100 | [diff] [blame] | 484 | struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); |
Axel Lin | 9bf311f | 2011-10-07 21:39:09 +0800 | [diff] [blame] | 485 | int ret; |
Mark Brown | 22d22ee | 2009-02-16 19:20:15 +0000 | [diff] [blame] | 486 | u16 reg; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 487 | |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 488 | switch (level) { |
| 489 | case SND_SOC_BIAS_ON: |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 490 | break; |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 491 | case SND_SOC_BIAS_PREPARE: |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 492 | break; |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 493 | case SND_SOC_BIAS_STANDBY: |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 494 | if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) { |
Mark Brown | 06ae998 | 2010-05-07 19:14:45 +0100 | [diff] [blame] | 495 | ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies), |
| 496 | wm8731->supplies); |
| 497 | if (ret != 0) |
| 498 | return ret; |
| 499 | |
Mark Brown | 05d448e | 2011-11-21 12:10:03 +0000 | [diff] [blame] | 500 | regcache_sync(wm8731->regmap); |
Mark Brown | 06ae998 | 2010-05-07 19:14:45 +0100 | [diff] [blame] | 501 | } |
| 502 | |
Mark Brown | 22d22ee | 2009-02-16 19:20:15 +0000 | [diff] [blame] | 503 | /* Clear PWROFF, gate CLKOUT, everything else as-is */ |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 504 | reg = snd_soc_read(codec, WM8731_PWR) & 0xff7f; |
| 505 | snd_soc_write(codec, WM8731_PWR, reg | 0x0040); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 506 | break; |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 507 | case SND_SOC_BIAS_OFF: |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 508 | snd_soc_write(codec, WM8731_PWR, 0xffff); |
Mark Brown | 06ae998 | 2010-05-07 19:14:45 +0100 | [diff] [blame] | 509 | regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), |
| 510 | wm8731->supplies); |
Mark Brown | 05d448e | 2011-11-21 12:10:03 +0000 | [diff] [blame] | 511 | regcache_mark_dirty(wm8731->regmap); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 512 | break; |
| 513 | } |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 514 | codec->dapm.bias_level = level; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 515 | return 0; |
| 516 | } |
| 517 | |
Richard Genoud | 0890c2b | 2013-07-30 11:59:45 +0200 | [diff] [blame] | 518 | static int wm8731_startup(struct snd_pcm_substream *substream, |
| 519 | struct snd_soc_dai *dai) |
| 520 | { |
| 521 | struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(dai->codec); |
| 522 | |
| 523 | if (wm8731->constraints) |
| 524 | snd_pcm_hw_constraint_list(substream->runtime, 0, |
| 525 | SNDRV_PCM_HW_PARAM_RATE, |
| 526 | wm8731->constraints); |
| 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | |
Bill Gatliff | e135443 | 2010-04-09 18:08:08 +0100 | [diff] [blame] | 531 | #define WM8731_RATES SNDRV_PCM_RATE_8000_96000 |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 532 | |
| 533 | #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ |
| 534 | SNDRV_PCM_FMTBIT_S24_LE) |
| 535 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 536 | static const struct snd_soc_dai_ops wm8731_dai_ops = { |
Richard Genoud | 0890c2b | 2013-07-30 11:59:45 +0200 | [diff] [blame] | 537 | .startup = wm8731_startup, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 538 | .hw_params = wm8731_hw_params, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 539 | .digital_mute = wm8731_mute, |
| 540 | .set_sysclk = wm8731_set_dai_sysclk, |
| 541 | .set_fmt = wm8731_set_dai_fmt, |
| 542 | }; |
| 543 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 544 | static struct snd_soc_dai_driver wm8731_dai = { |
| 545 | .name = "wm8731-hifi", |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 546 | .playback = { |
| 547 | .stream_name = "Playback", |
| 548 | .channels_min = 1, |
| 549 | .channels_max = 2, |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 550 | .rates = WM8731_RATES, |
| 551 | .formats = WM8731_FORMATS,}, |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 552 | .capture = { |
| 553 | .stream_name = "Capture", |
| 554 | .channels_min = 1, |
| 555 | .channels_max = 2, |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 556 | .rates = WM8731_RATES, |
| 557 | .formats = WM8731_FORMATS,}, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 558 | .ops = &wm8731_dai_ops, |
Mark Brown | 4934482 | 2009-07-08 17:48:12 +0100 | [diff] [blame] | 559 | .symmetric_rates = 1, |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 560 | }; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 561 | |
Mark Brown | b3b50b3 | 2009-06-13 22:30:18 +0100 | [diff] [blame] | 562 | #ifdef CONFIG_PM |
Lars-Peter Clausen | 84b315e | 2011-12-02 10:18:28 +0100 | [diff] [blame] | 563 | static int wm8731_suspend(struct snd_soc_codec *codec) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 564 | { |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 565 | wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF); |
Mark Brown | 06ae998 | 2010-05-07 19:14:45 +0100 | [diff] [blame] | 566 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 567 | return 0; |
| 568 | } |
| 569 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 570 | static int wm8731_resume(struct snd_soc_codec *codec) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 571 | { |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 572 | wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
Mark Brown | 7dea7c0 | 2009-10-26 15:20:17 +0000 | [diff] [blame] | 573 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 574 | return 0; |
| 575 | } |
Mark Brown | b3b50b3 | 2009-06-13 22:30:18 +0100 | [diff] [blame] | 576 | #else |
| 577 | #define wm8731_suspend NULL |
| 578 | #define wm8731_resume NULL |
| 579 | #endif |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 580 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 581 | static int wm8731_probe(struct snd_soc_codec *codec) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 582 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 583 | struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); |
| 584 | int ret = 0, i; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 585 | |
Mark Brown | 7dea7c0 | 2009-10-26 15:20:17 +0000 | [diff] [blame] | 586 | for (i = 0; i < ARRAY_SIZE(wm8731->supplies); i++) |
| 587 | wm8731->supplies[i].supply = wm8731_supply_names[i]; |
| 588 | |
| 589 | ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8731->supplies), |
| 590 | wm8731->supplies); |
| 591 | if (ret != 0) { |
| 592 | dev_err(codec->dev, "Failed to request supplies: %d\n", ret); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 593 | return ret; |
Mark Brown | 7dea7c0 | 2009-10-26 15:20:17 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies), |
| 597 | wm8731->supplies); |
| 598 | if (ret != 0) { |
| 599 | dev_err(codec->dev, "Failed to enable supplies: %d\n", ret); |
| 600 | goto err_regulator_get; |
| 601 | } |
| 602 | |
Mark Brown | 519cf2d | 2009-02-18 21:06:01 +0000 | [diff] [blame] | 603 | ret = wm8731_reset(codec); |
| 604 | if (ret < 0) { |
Mark Brown | fe5422f | 2009-07-05 15:18:01 +0100 | [diff] [blame] | 605 | dev_err(codec->dev, "Failed to issue reset: %d\n", ret); |
Mark Brown | 7dea7c0 | 2009-10-26 15:20:17 +0000 | [diff] [blame] | 606 | goto err_regulator_enable; |
Mark Brown | 519cf2d | 2009-02-18 21:06:01 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 609 | wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
| 610 | |
| 611 | /* Latch the update bits */ |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 612 | snd_soc_update_bits(codec, WM8731_LOUT1V, 0x100, 0); |
| 613 | snd_soc_update_bits(codec, WM8731_ROUT1V, 0x100, 0); |
| 614 | snd_soc_update_bits(codec, WM8731_LINVOL, 0x100, 0); |
| 615 | snd_soc_update_bits(codec, WM8731_RINVOL, 0x100, 0); |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 616 | |
Mark Brown | ce3bdaa | 2009-02-19 14:29:49 +0000 | [diff] [blame] | 617 | /* Disable bypass path by default */ |
Dimitris Papastamos | 2062ea5 | 2010-12-01 09:38:55 +0000 | [diff] [blame] | 618 | snd_soc_update_bits(codec, WM8731_APANA, 0x8, 0); |
Mark Brown | ce3bdaa | 2009-02-19 14:29:49 +0000 | [diff] [blame] | 619 | |
Mark Brown | 06ae998 | 2010-05-07 19:14:45 +0100 | [diff] [blame] | 620 | /* Regulators will have been enabled by bias management */ |
| 621 | regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); |
| 622 | |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 623 | return 0; |
Mark Brown | fe5422f | 2009-07-05 15:18:01 +0100 | [diff] [blame] | 624 | |
Mark Brown | 7dea7c0 | 2009-10-26 15:20:17 +0000 | [diff] [blame] | 625 | err_regulator_enable: |
| 626 | regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); |
| 627 | err_regulator_get: |
| 628 | regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 629 | |
Mark Brown | fe5422f | 2009-07-05 15:18:01 +0100 | [diff] [blame] | 630 | return ret; |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 633 | /* power down chip */ |
| 634 | static int wm8731_remove(struct snd_soc_codec *codec) |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 635 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 636 | struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); |
| 637 | |
| 638 | wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF); |
| 639 | |
| 640 | regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); |
Mark Brown | 7dea7c0 | 2009-10-26 15:20:17 +0000 | [diff] [blame] | 641 | regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 642 | |
| 643 | return 0; |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 644 | } |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 645 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 646 | static struct snd_soc_codec_driver soc_codec_dev_wm8731 = { |
| 647 | .probe = wm8731_probe, |
| 648 | .remove = wm8731_remove, |
| 649 | .suspend = wm8731_suspend, |
| 650 | .resume = wm8731_resume, |
| 651 | .set_bias_level = wm8731_set_bias_level, |
Mark Brown | 5e251ae | 2011-03-27 14:33:03 +0100 | [diff] [blame] | 652 | .dapm_widgets = wm8731_dapm_widgets, |
| 653 | .num_dapm_widgets = ARRAY_SIZE(wm8731_dapm_widgets), |
| 654 | .dapm_routes = wm8731_intercon, |
| 655 | .num_dapm_routes = ARRAY_SIZE(wm8731_intercon), |
Mark Brown | cb55531 | 2011-11-21 12:59:52 +0000 | [diff] [blame] | 656 | .controls = wm8731_snd_controls, |
| 657 | .num_controls = ARRAY_SIZE(wm8731_snd_controls), |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 658 | }; |
| 659 | |
Mark Brown | a7f96e4 | 2011-07-26 21:00:13 +0100 | [diff] [blame] | 660 | static const struct of_device_id wm8731_of_match[] = { |
| 661 | { .compatible = "wlf,wm8731", }, |
| 662 | { } |
| 663 | }; |
| 664 | |
| 665 | MODULE_DEVICE_TABLE(of, wm8731_of_match); |
| 666 | |
Mark Brown | 05d448e | 2011-11-21 12:10:03 +0000 | [diff] [blame] | 667 | static const struct regmap_config wm8731_regmap = { |
| 668 | .reg_bits = 7, |
| 669 | .val_bits = 9, |
| 670 | |
| 671 | .max_register = WM8731_RESET, |
| 672 | .volatile_reg = wm8731_volatile, |
| 673 | .writeable_reg = wm8731_writeable, |
| 674 | |
| 675 | .cache_type = REGCACHE_RBTREE, |
| 676 | .reg_defaults = wm8731_reg_defaults, |
| 677 | .num_reg_defaults = ARRAY_SIZE(wm8731_reg_defaults), |
| 678 | }; |
| 679 | |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 680 | #if defined(CONFIG_SPI_MASTER) |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 681 | static int wm8731_spi_probe(struct spi_device *spi) |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 682 | { |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 683 | struct wm8731_priv *wm8731; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 684 | int ret; |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 685 | |
Mark Brown | f1992dd | 2012-05-12 20:08:43 +0100 | [diff] [blame] | 686 | wm8731 = devm_kzalloc(&spi->dev, sizeof(struct wm8731_priv), |
| 687 | GFP_KERNEL); |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 688 | if (wm8731 == NULL) |
| 689 | return -ENOMEM; |
| 690 | |
Mark Brown | f1992dd | 2012-05-12 20:08:43 +0100 | [diff] [blame] | 691 | wm8731->regmap = devm_regmap_init_spi(spi, &wm8731_regmap); |
Mark Brown | 05d448e | 2011-11-21 12:10:03 +0000 | [diff] [blame] | 692 | if (IS_ERR(wm8731->regmap)) { |
| 693 | ret = PTR_ERR(wm8731->regmap); |
| 694 | dev_err(&spi->dev, "Failed to allocate register map: %d\n", |
| 695 | ret); |
Mark Brown | f1992dd | 2012-05-12 20:08:43 +0100 | [diff] [blame] | 696 | return ret; |
Mark Brown | 05d448e | 2011-11-21 12:10:03 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 699 | spi_set_drvdata(spi, wm8731); |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 700 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 701 | ret = snd_soc_register_codec(&spi->dev, |
| 702 | &soc_codec_dev_wm8731, &wm8731_dai, 1); |
Mark Brown | 05d448e | 2011-11-21 12:10:03 +0000 | [diff] [blame] | 703 | if (ret != 0) { |
| 704 | dev_err(&spi->dev, "Failed to register CODEC: %d\n", ret); |
Mark Brown | f1992dd | 2012-05-12 20:08:43 +0100 | [diff] [blame] | 705 | return ret; |
Mark Brown | 05d448e | 2011-11-21 12:10:03 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | return 0; |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 711 | static int wm8731_spi_remove(struct spi_device *spi) |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 712 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 713 | snd_soc_unregister_codec(&spi->dev); |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | static struct spi_driver wm8731_spi_driver = { |
| 718 | .driver = { |
Mark Brown | 99b59f3 | 2011-03-27 14:35:15 +0100 | [diff] [blame] | 719 | .name = "wm8731", |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 720 | .owner = THIS_MODULE, |
Mark Brown | a7f96e4 | 2011-07-26 21:00:13 +0100 | [diff] [blame] | 721 | .of_match_table = wm8731_of_match, |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 722 | }, |
| 723 | .probe = wm8731_spi_probe, |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 724 | .remove = wm8731_spi_remove, |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 725 | }; |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 726 | #endif /* CONFIG_SPI_MASTER */ |
| 727 | |
Fabio Estevam | b65ab73 | 2013-11-21 12:38:43 -0200 | [diff] [blame] | 728 | #if IS_ENABLED(CONFIG_I2C) |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 729 | static int wm8731_i2c_probe(struct i2c_client *i2c, |
| 730 | const struct i2c_device_id *id) |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 731 | { |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 732 | struct wm8731_priv *wm8731; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 733 | int ret; |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 734 | |
Mark Brown | f1992dd | 2012-05-12 20:08:43 +0100 | [diff] [blame] | 735 | wm8731 = devm_kzalloc(&i2c->dev, sizeof(struct wm8731_priv), |
| 736 | GFP_KERNEL); |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 737 | if (wm8731 == NULL) |
| 738 | return -ENOMEM; |
| 739 | |
Mark Brown | f1992dd | 2012-05-12 20:08:43 +0100 | [diff] [blame] | 740 | wm8731->regmap = devm_regmap_init_i2c(i2c, &wm8731_regmap); |
Mark Brown | 05d448e | 2011-11-21 12:10:03 +0000 | [diff] [blame] | 741 | if (IS_ERR(wm8731->regmap)) { |
| 742 | ret = PTR_ERR(wm8731->regmap); |
| 743 | dev_err(&i2c->dev, "Failed to allocate register map: %d\n", |
| 744 | ret); |
Mark Brown | f1992dd | 2012-05-12 20:08:43 +0100 | [diff] [blame] | 745 | return ret; |
Mark Brown | 05d448e | 2011-11-21 12:10:03 +0000 | [diff] [blame] | 746 | } |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 747 | |
Mark Brown | 05d448e | 2011-11-21 12:10:03 +0000 | [diff] [blame] | 748 | i2c_set_clientdata(i2c, wm8731); |
| 749 | |
| 750 | ret = snd_soc_register_codec(&i2c->dev, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 751 | &soc_codec_dev_wm8731, &wm8731_dai, 1); |
Mark Brown | 05d448e | 2011-11-21 12:10:03 +0000 | [diff] [blame] | 752 | if (ret != 0) { |
| 753 | dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret); |
Mark Brown | f1992dd | 2012-05-12 20:08:43 +0100 | [diff] [blame] | 754 | return ret; |
Mark Brown | 05d448e | 2011-11-21 12:10:03 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | return 0; |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 760 | static int wm8731_i2c_remove(struct i2c_client *client) |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 761 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 762 | snd_soc_unregister_codec(&client->dev); |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 763 | return 0; |
| 764 | } |
| 765 | |
| 766 | static const struct i2c_device_id wm8731_i2c_id[] = { |
| 767 | { "wm8731", 0 }, |
| 768 | { } |
| 769 | }; |
| 770 | MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id); |
| 771 | |
| 772 | static struct i2c_driver wm8731_i2c_driver = { |
| 773 | .driver = { |
Mark Brown | 99b59f3 | 2011-03-27 14:35:15 +0100 | [diff] [blame] | 774 | .name = "wm8731", |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 775 | .owner = THIS_MODULE, |
Mark Brown | a7f96e4 | 2011-07-26 21:00:13 +0100 | [diff] [blame] | 776 | .of_match_table = wm8731_of_match, |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 777 | }, |
| 778 | .probe = wm8731_i2c_probe, |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 779 | .remove = wm8731_i2c_remove, |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 780 | .id_table = wm8731_i2c_id, |
| 781 | }; |
| 782 | #endif |
| 783 | |
Takashi Iwai | c9b3a40 | 2008-12-10 07:47:22 +0100 | [diff] [blame] | 784 | static int __init wm8731_modinit(void) |
Mark Brown | 64089b8 | 2008-12-08 19:17:58 +0000 | [diff] [blame] | 785 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 786 | int ret = 0; |
Fabio Estevam | b65ab73 | 2013-11-21 12:38:43 -0200 | [diff] [blame] | 787 | #if IS_ENABLED(CONFIG_I2C) |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 788 | ret = i2c_add_driver(&wm8731_i2c_driver); |
| 789 | if (ret != 0) { |
| 790 | printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n", |
| 791 | ret); |
| 792 | } |
| 793 | #endif |
| 794 | #if defined(CONFIG_SPI_MASTER) |
| 795 | ret = spi_register_driver(&wm8731_spi_driver); |
| 796 | if (ret != 0) { |
| 797 | printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n", |
| 798 | ret); |
| 799 | } |
| 800 | #endif |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 801 | return ret; |
Mark Brown | 64089b8 | 2008-12-08 19:17:58 +0000 | [diff] [blame] | 802 | } |
| 803 | module_init(wm8731_modinit); |
| 804 | |
| 805 | static void __exit wm8731_exit(void) |
| 806 | { |
Fabio Estevam | b65ab73 | 2013-11-21 12:38:43 -0200 | [diff] [blame] | 807 | #if IS_ENABLED(CONFIG_I2C) |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 808 | i2c_del_driver(&wm8731_i2c_driver); |
| 809 | #endif |
| 810 | #if defined(CONFIG_SPI_MASTER) |
| 811 | spi_unregister_driver(&wm8731_spi_driver); |
| 812 | #endif |
Mark Brown | 64089b8 | 2008-12-08 19:17:58 +0000 | [diff] [blame] | 813 | } |
| 814 | module_exit(wm8731_exit); |
| 815 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 816 | MODULE_DESCRIPTION("ASoC WM8731 driver"); |
| 817 | MODULE_AUTHOR("Richard Purdie"); |
| 818 | MODULE_LICENSE("GPL"); |