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