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