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