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