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