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> |
| 21 | #include <linux/platform_device.h> |
Cliff Cai | d2a4035 | 2008-09-01 18:47:03 +0100 | [diff] [blame] | 22 | #include <linux/spi/spi.h> |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 23 | #include <sound/core.h> |
| 24 | #include <sound/pcm.h> |
| 25 | #include <sound/pcm_params.h> |
| 26 | #include <sound/soc.h> |
| 27 | #include <sound/soc-dapm.h> |
| 28 | #include <sound/initval.h> |
Mark Brown | d00efa6 | 2009-07-08 16:53:12 +0100 | [diff] [blame] | 29 | #include <sound/tlv.h> |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 30 | |
| 31 | #include "wm8731.h" |
| 32 | |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 33 | static struct snd_soc_codec *wm8731_codec; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 34 | struct snd_soc_codec_device soc_codec_dev_wm8731; |
| 35 | |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 36 | /* codec private data */ |
| 37 | struct wm8731_priv { |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 38 | struct snd_soc_codec codec; |
| 39 | u16 reg_cache[WM8731_CACHEREGNUM]; |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 40 | unsigned int sysclk; |
| 41 | }; |
| 42 | |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 43 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 44 | /* |
| 45 | * wm8731 register cache |
| 46 | * We can't read the WM8731 register space when we are |
| 47 | * using 2 wire for device control, so we cache them instead. |
| 48 | * There is no point in caching the reset register |
| 49 | */ |
| 50 | static const u16 wm8731_reg[WM8731_CACHEREGNUM] = { |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 51 | 0x0097, 0x0097, 0x0079, 0x0079, |
| 52 | 0x000a, 0x0008, 0x009f, 0x000a, |
| 53 | 0x0000, 0x0000 |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 54 | }; |
| 55 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 56 | #define wm8731_reset(c) snd_soc_write(c, WM8731_RESET, 0) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 57 | |
| 58 | static const char *wm8731_input_select[] = {"Line In", "Mic"}; |
| 59 | static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"}; |
| 60 | |
| 61 | static const struct soc_enum wm8731_enum[] = { |
| 62 | SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select), |
| 63 | SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph), |
| 64 | }; |
| 65 | |
Mark Brown | d00efa6 | 2009-07-08 16:53:12 +0100 | [diff] [blame] | 66 | static const DECLARE_TLV_DB_SCALE(in_tlv, -3450, 150, 0); |
| 67 | static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -1500, 300, 0); |
| 68 | static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1); |
| 69 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 70 | static const struct snd_kcontrol_new wm8731_snd_controls[] = { |
| 71 | |
Mark Brown | d00efa6 | 2009-07-08 16:53:12 +0100 | [diff] [blame] | 72 | SOC_DOUBLE_R_TLV("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V, |
| 73 | 0, 127, 0, out_tlv), |
Liam Girdwood | bd903b6 | 2006-11-09 16:35:01 +0100 | [diff] [blame] | 74 | SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V, |
| 75 | 7, 1, 0), |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 76 | |
Mark Brown | d00efa6 | 2009-07-08 16:53:12 +0100 | [diff] [blame] | 77 | SOC_DOUBLE_R_TLV("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0, |
| 78 | in_tlv), |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 79 | SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1), |
| 80 | |
| 81 | SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0), |
Mark Brown | ef38ed8 | 2009-07-08 17:05:43 +0100 | [diff] [blame] | 82 | SOC_SINGLE("Mic Capture Switch", WM8731_APANA, 1, 1, 1), |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 83 | |
Mark Brown | d00efa6 | 2009-07-08 16:53:12 +0100 | [diff] [blame] | 84 | SOC_SINGLE_TLV("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1, |
| 85 | sidetone_tlv), |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 86 | |
| 87 | SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1), |
| 88 | SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0), |
| 89 | |
| 90 | SOC_ENUM("Playback De-emphasis", wm8731_enum[1]), |
| 91 | }; |
| 92 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 93 | /* Output Mixer */ |
| 94 | static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = { |
| 95 | SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0), |
| 96 | SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0), |
| 97 | SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0), |
| 98 | }; |
| 99 | |
| 100 | /* Input mux */ |
| 101 | static const struct snd_kcontrol_new wm8731_input_mux_controls = |
| 102 | SOC_DAPM_ENUM("Input Select", wm8731_enum[0]); |
| 103 | |
| 104 | static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = { |
| 105 | SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1, |
| 106 | &wm8731_output_mixer_controls[0], |
| 107 | ARRAY_SIZE(wm8731_output_mixer_controls)), |
| 108 | SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1), |
| 109 | SND_SOC_DAPM_OUTPUT("LOUT"), |
| 110 | SND_SOC_DAPM_OUTPUT("LHPOUT"), |
| 111 | SND_SOC_DAPM_OUTPUT("ROUT"), |
| 112 | SND_SOC_DAPM_OUTPUT("RHPOUT"), |
| 113 | SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1), |
| 114 | SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls), |
| 115 | SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0), |
| 116 | SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1), |
| 117 | SND_SOC_DAPM_INPUT("MICIN"), |
| 118 | SND_SOC_DAPM_INPUT("RLINEIN"), |
| 119 | SND_SOC_DAPM_INPUT("LLINEIN"), |
| 120 | }; |
| 121 | |
Mark Brown | a65f056 | 2008-05-13 14:54:43 +0200 | [diff] [blame] | 122 | static const struct snd_soc_dapm_route intercon[] = { |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 123 | /* output mixer */ |
| 124 | {"Output Mixer", "Line Bypass Switch", "Line Input"}, |
| 125 | {"Output Mixer", "HiFi Playback Switch", "DAC"}, |
| 126 | {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"}, |
| 127 | |
| 128 | /* outputs */ |
| 129 | {"RHPOUT", NULL, "Output Mixer"}, |
| 130 | {"ROUT", NULL, "Output Mixer"}, |
| 131 | {"LHPOUT", NULL, "Output Mixer"}, |
| 132 | {"LOUT", NULL, "Output Mixer"}, |
| 133 | |
| 134 | /* input mux */ |
| 135 | {"Input Mux", "Line In", "Line Input"}, |
| 136 | {"Input Mux", "Mic", "Mic Bias"}, |
| 137 | {"ADC", NULL, "Input Mux"}, |
| 138 | |
| 139 | /* inputs */ |
| 140 | {"Line Input", NULL, "LLINEIN"}, |
| 141 | {"Line Input", NULL, "RLINEIN"}, |
| 142 | {"Mic Bias", NULL, "MICIN"}, |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | static int wm8731_add_widgets(struct snd_soc_codec *codec) |
| 146 | { |
Mark Brown | a65f056 | 2008-05-13 14:54:43 +0200 | [diff] [blame] | 147 | snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets, |
| 148 | ARRAY_SIZE(wm8731_dapm_widgets)); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 149 | |
Mark Brown | a65f056 | 2008-05-13 14:54:43 +0200 | [diff] [blame] | 150 | snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon)); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 151 | |
| 152 | snd_soc_dapm_new_widgets(codec); |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | struct _coeff_div { |
| 157 | u32 mclk; |
| 158 | u32 rate; |
| 159 | u16 fs; |
| 160 | u8 sr:4; |
| 161 | u8 bosr:1; |
| 162 | u8 usb:1; |
| 163 | }; |
| 164 | |
| 165 | /* codec mclk clock divider coefficients */ |
| 166 | static const struct _coeff_div coeff_div[] = { |
| 167 | /* 48k */ |
| 168 | {12288000, 48000, 256, 0x0, 0x0, 0x0}, |
| 169 | {18432000, 48000, 384, 0x0, 0x1, 0x0}, |
| 170 | {12000000, 48000, 250, 0x0, 0x0, 0x1}, |
| 171 | |
| 172 | /* 32k */ |
| 173 | {12288000, 32000, 384, 0x6, 0x0, 0x0}, |
| 174 | {18432000, 32000, 576, 0x6, 0x1, 0x0}, |
Frank Mandarino | 298a2c7 | 2007-01-31 10:02:56 +0100 | [diff] [blame] | 175 | {12000000, 32000, 375, 0x6, 0x0, 0x1}, |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 176 | |
| 177 | /* 8k */ |
| 178 | {12288000, 8000, 1536, 0x3, 0x0, 0x0}, |
| 179 | {18432000, 8000, 2304, 0x3, 0x1, 0x0}, |
| 180 | {11289600, 8000, 1408, 0xb, 0x0, 0x0}, |
| 181 | {16934400, 8000, 2112, 0xb, 0x1, 0x0}, |
| 182 | {12000000, 8000, 1500, 0x3, 0x0, 0x1}, |
| 183 | |
| 184 | /* 96k */ |
| 185 | {12288000, 96000, 128, 0x7, 0x0, 0x0}, |
| 186 | {18432000, 96000, 192, 0x7, 0x1, 0x0}, |
| 187 | {12000000, 96000, 125, 0x7, 0x0, 0x1}, |
| 188 | |
| 189 | /* 44.1k */ |
| 190 | {11289600, 44100, 256, 0x8, 0x0, 0x0}, |
| 191 | {16934400, 44100, 384, 0x8, 0x1, 0x0}, |
| 192 | {12000000, 44100, 272, 0x8, 0x1, 0x1}, |
| 193 | |
| 194 | /* 88.2k */ |
| 195 | {11289600, 88200, 128, 0xf, 0x0, 0x0}, |
| 196 | {16934400, 88200, 192, 0xf, 0x1, 0x0}, |
| 197 | {12000000, 88200, 136, 0xf, 0x1, 0x1}, |
| 198 | }; |
| 199 | |
| 200 | static inline int get_coeff(int mclk, int rate) |
| 201 | { |
| 202 | int i; |
| 203 | |
| 204 | for (i = 0; i < ARRAY_SIZE(coeff_div); i++) { |
| 205 | if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk) |
| 206 | return i; |
| 207 | } |
| 208 | return 0; |
| 209 | } |
| 210 | |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 211 | static int wm8731_hw_params(struct snd_pcm_substream *substream, |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 212 | struct snd_pcm_hw_params *params, |
| 213 | struct snd_soc_dai *dai) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 214 | { |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 215 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 216 | struct snd_soc_device *socdev = rtd->socdev; |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 217 | struct snd_soc_codec *codec = socdev->card->codec; |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 218 | struct wm8731_priv *wm8731 = codec->private_data; |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 219 | u16 iface = snd_soc_read(codec, WM8731_IFACE) & 0xfff3; |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 220 | int i = get_coeff(wm8731->sysclk, params_rate(params)); |
| 221 | u16 srate = (coeff_div[i].sr << 2) | |
| 222 | (coeff_div[i].bosr << 1) | coeff_div[i].usb; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 223 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 224 | snd_soc_write(codec, WM8731_SRATE, srate); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 225 | |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 226 | /* bit size */ |
| 227 | switch (params_format(params)) { |
| 228 | case SNDRV_PCM_FORMAT_S16_LE: |
| 229 | break; |
| 230 | case SNDRV_PCM_FORMAT_S20_3LE: |
| 231 | iface |= 0x0004; |
| 232 | break; |
| 233 | case SNDRV_PCM_FORMAT_S24_LE: |
| 234 | iface |= 0x0008; |
| 235 | break; |
| 236 | } |
| 237 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 238 | snd_soc_write(codec, WM8731_IFACE, iface); |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 239 | return 0; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 240 | } |
| 241 | |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 242 | static int wm8731_pcm_prepare(struct snd_pcm_substream *substream, |
| 243 | struct snd_soc_dai *dai) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 244 | { |
| 245 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 246 | struct snd_soc_device *socdev = rtd->socdev; |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 247 | struct snd_soc_codec *codec = socdev->card->codec; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 248 | |
| 249 | /* set active */ |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 250 | snd_soc_write(codec, WM8731_ACTIVE, 0x0001); |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 251 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 252 | return 0; |
| 253 | } |
| 254 | |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 255 | static void wm8731_shutdown(struct snd_pcm_substream *substream, |
| 256 | struct snd_soc_dai *dai) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 257 | { |
| 258 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 259 | struct snd_soc_device *socdev = rtd->socdev; |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 260 | struct snd_soc_codec *codec = socdev->card->codec; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 261 | |
| 262 | /* deactivate */ |
| 263 | if (!codec->active) { |
| 264 | udelay(50); |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 265 | snd_soc_write(codec, WM8731_ACTIVE, 0x0); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 269 | static int wm8731_mute(struct snd_soc_dai *dai, int mute) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 270 | { |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 271 | struct snd_soc_codec *codec = dai->codec; |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 272 | u16 mute_reg = snd_soc_read(codec, WM8731_APDIGI) & 0xfff7; |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 273 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 274 | if (mute) |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 275 | snd_soc_write(codec, WM8731_APDIGI, mute_reg | 0x8); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 276 | else |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 277 | snd_soc_write(codec, WM8731_APDIGI, mute_reg); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 278 | return 0; |
| 279 | } |
| 280 | |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 281 | static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 282 | int clk_id, unsigned int freq, int dir) |
| 283 | { |
| 284 | struct snd_soc_codec *codec = codec_dai->codec; |
| 285 | struct wm8731_priv *wm8731 = codec->private_data; |
| 286 | |
| 287 | switch (freq) { |
| 288 | case 11289600: |
| 289 | case 12000000: |
| 290 | case 12288000: |
| 291 | case 16934400: |
| 292 | case 18432000: |
| 293 | wm8731->sysclk = freq; |
| 294 | return 0; |
| 295 | } |
| 296 | return -EINVAL; |
| 297 | } |
| 298 | |
| 299 | |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 300 | static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai, |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 301 | unsigned int fmt) |
| 302 | { |
| 303 | struct snd_soc_codec *codec = codec_dai->codec; |
| 304 | u16 iface = 0; |
| 305 | |
| 306 | /* set master/slave audio interface */ |
| 307 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 308 | case SND_SOC_DAIFMT_CBM_CFM: |
| 309 | iface |= 0x0040; |
| 310 | break; |
| 311 | case SND_SOC_DAIFMT_CBS_CFS: |
| 312 | break; |
| 313 | default: |
| 314 | return -EINVAL; |
| 315 | } |
| 316 | |
| 317 | /* interface format */ |
| 318 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 319 | case SND_SOC_DAIFMT_I2S: |
| 320 | iface |= 0x0002; |
| 321 | break; |
| 322 | case SND_SOC_DAIFMT_RIGHT_J: |
| 323 | break; |
| 324 | case SND_SOC_DAIFMT_LEFT_J: |
| 325 | iface |= 0x0001; |
| 326 | break; |
| 327 | case SND_SOC_DAIFMT_DSP_A: |
| 328 | iface |= 0x0003; |
| 329 | break; |
| 330 | case SND_SOC_DAIFMT_DSP_B: |
| 331 | iface |= 0x0013; |
| 332 | break; |
| 333 | default: |
| 334 | return -EINVAL; |
| 335 | } |
| 336 | |
| 337 | /* clock inversion */ |
| 338 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 339 | case SND_SOC_DAIFMT_NB_NF: |
| 340 | break; |
| 341 | case SND_SOC_DAIFMT_IB_IF: |
| 342 | iface |= 0x0090; |
| 343 | break; |
| 344 | case SND_SOC_DAIFMT_IB_NF: |
| 345 | iface |= 0x0080; |
| 346 | break; |
| 347 | case SND_SOC_DAIFMT_NB_IF: |
| 348 | iface |= 0x0010; |
| 349 | break; |
| 350 | default: |
| 351 | return -EINVAL; |
| 352 | } |
| 353 | |
| 354 | /* set iface */ |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 355 | snd_soc_write(codec, WM8731_IFACE, iface); |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 356 | return 0; |
| 357 | } |
| 358 | |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 359 | static int wm8731_set_bias_level(struct snd_soc_codec *codec, |
| 360 | enum snd_soc_bias_level level) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 361 | { |
Mark Brown | 22d22ee | 2009-02-16 19:20:15 +0000 | [diff] [blame] | 362 | u16 reg; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 363 | |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 364 | switch (level) { |
| 365 | case SND_SOC_BIAS_ON: |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 366 | break; |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 367 | case SND_SOC_BIAS_PREPARE: |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 368 | break; |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 369 | case SND_SOC_BIAS_STANDBY: |
Mark Brown | 22d22ee | 2009-02-16 19:20:15 +0000 | [diff] [blame] | 370 | /* Clear PWROFF, gate CLKOUT, everything else as-is */ |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 371 | reg = snd_soc_read(codec, WM8731_PWR) & 0xff7f; |
| 372 | snd_soc_write(codec, WM8731_PWR, reg | 0x0040); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 373 | break; |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 374 | case SND_SOC_BIAS_OFF: |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 375 | snd_soc_write(codec, WM8731_ACTIVE, 0x0); |
| 376 | snd_soc_write(codec, WM8731_PWR, 0xffff); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 377 | break; |
| 378 | } |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 379 | codec->bias_level = level; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 380 | return 0; |
| 381 | } |
| 382 | |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 383 | #define WM8731_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\ |
| 384 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\ |
| 385 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\ |
| 386 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\ |
| 387 | SNDRV_PCM_RATE_96000) |
| 388 | |
| 389 | #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ |
| 390 | SNDRV_PCM_FMTBIT_S24_LE) |
| 391 | |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 392 | static struct snd_soc_dai_ops wm8731_dai_ops = { |
| 393 | .prepare = wm8731_pcm_prepare, |
| 394 | .hw_params = wm8731_hw_params, |
| 395 | .shutdown = wm8731_shutdown, |
| 396 | .digital_mute = wm8731_mute, |
| 397 | .set_sysclk = wm8731_set_dai_sysclk, |
| 398 | .set_fmt = wm8731_set_dai_fmt, |
| 399 | }; |
| 400 | |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 401 | struct snd_soc_dai wm8731_dai = { |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 402 | .name = "WM8731", |
| 403 | .playback = { |
| 404 | .stream_name = "Playback", |
| 405 | .channels_min = 1, |
| 406 | .channels_max = 2, |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 407 | .rates = WM8731_RATES, |
| 408 | .formats = WM8731_FORMATS,}, |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 409 | .capture = { |
| 410 | .stream_name = "Capture", |
| 411 | .channels_min = 1, |
| 412 | .channels_max = 2, |
Frank Mandarino | b36d61d | 2007-02-02 17:14:56 +0100 | [diff] [blame] | 413 | .rates = WM8731_RATES, |
| 414 | .formats = WM8731_FORMATS,}, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 415 | .ops = &wm8731_dai_ops, |
Mark Brown | 4934482 | 2009-07-08 17:48:12 +0100 | [diff] [blame] | 416 | .symmetric_rates = 1, |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 417 | }; |
| 418 | EXPORT_SYMBOL_GPL(wm8731_dai); |
| 419 | |
Mark Brown | b3b50b3 | 2009-06-13 22:30:18 +0100 | [diff] [blame] | 420 | #ifdef CONFIG_PM |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 421 | static int wm8731_suspend(struct platform_device *pdev, pm_message_t state) |
| 422 | { |
| 423 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 424 | struct snd_soc_codec *codec = socdev->card->codec; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 425 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 426 | snd_soc_write(codec, WM8731_ACTIVE, 0x0); |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 427 | wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | static int wm8731_resume(struct platform_device *pdev) |
| 432 | { |
| 433 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 434 | struct snd_soc_codec *codec = socdev->card->codec; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 435 | int i; |
| 436 | u8 data[2]; |
| 437 | u16 *cache = codec->reg_cache; |
| 438 | |
| 439 | /* Sync reg_cache with the hardware */ |
| 440 | for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) { |
| 441 | data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001); |
| 442 | data[1] = cache[i] & 0x00ff; |
| 443 | codec->hw_write(codec->control_data, data, 2); |
| 444 | } |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 445 | wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
| 446 | wm8731_set_bias_level(codec, codec->suspend_bias_level); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 447 | return 0; |
| 448 | } |
Mark Brown | b3b50b3 | 2009-06-13 22:30:18 +0100 | [diff] [blame] | 449 | #else |
| 450 | #define wm8731_suspend NULL |
| 451 | #define wm8731_resume NULL |
| 452 | #endif |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 453 | |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 454 | static int wm8731_probe(struct platform_device *pdev) |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 455 | { |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 456 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 457 | struct snd_soc_codec *codec; |
| 458 | int ret = 0; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 459 | |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 460 | if (wm8731_codec == NULL) { |
| 461 | dev_err(&pdev->dev, "Codec device not registered\n"); |
| 462 | return -ENODEV; |
| 463 | } |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 464 | |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 465 | socdev->card->codec = wm8731_codec; |
| 466 | codec = wm8731_codec; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 467 | |
| 468 | /* register pcms */ |
| 469 | ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); |
| 470 | if (ret < 0) { |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 471 | dev_err(codec->dev, "failed to create pcms: %d\n", ret); |
Liam Girdwood | e35115a | 2007-01-31 10:02:23 +0100 | [diff] [blame] | 472 | goto pcm_err; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 473 | } |
| 474 | |
Ian Molton | 3e8e195 | 2009-01-09 00:23:21 +0000 | [diff] [blame] | 475 | snd_soc_add_controls(codec, wm8731_snd_controls, |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 476 | ARRAY_SIZE(wm8731_snd_controls)); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 477 | wm8731_add_widgets(codec); |
Mark Brown | 968a602 | 2008-11-28 11:49:07 +0000 | [diff] [blame] | 478 | ret = snd_soc_init_card(socdev); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 479 | if (ret < 0) { |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 480 | dev_err(codec->dev, "failed to register card: %d\n", ret); |
Liam Girdwood | e35115a | 2007-01-31 10:02:23 +0100 | [diff] [blame] | 481 | goto card_err; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | return ret; |
Liam Girdwood | e35115a | 2007-01-31 10:02:23 +0100 | [diff] [blame] | 485 | |
| 486 | card_err: |
| 487 | snd_soc_free_pcms(socdev); |
| 488 | snd_soc_dapm_free(socdev); |
| 489 | pcm_err: |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 490 | return ret; |
| 491 | } |
| 492 | |
| 493 | /* power down chip */ |
| 494 | static int wm8731_remove(struct platform_device *pdev) |
| 495 | { |
| 496 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 497 | |
| 498 | snd_soc_free_pcms(socdev); |
| 499 | snd_soc_dapm_free(socdev); |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 500 | |
| 501 | return 0; |
| 502 | } |
| 503 | |
| 504 | struct snd_soc_codec_device soc_codec_dev_wm8731 = { |
| 505 | .probe = wm8731_probe, |
| 506 | .remove = wm8731_remove, |
| 507 | .suspend = wm8731_suspend, |
| 508 | .resume = wm8731_resume, |
| 509 | }; |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 510 | EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731); |
| 511 | |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 512 | static int wm8731_register(struct wm8731_priv *wm8731, |
| 513 | enum snd_soc_control_type control) |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 514 | { |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 515 | int ret; |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 516 | struct snd_soc_codec *codec = &wm8731->codec; |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 517 | |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 518 | if (wm8731_codec) { |
| 519 | dev_err(codec->dev, "Another WM8731 is registered\n"); |
Mark Brown | fe5422f | 2009-07-05 15:18:01 +0100 | [diff] [blame] | 520 | ret = -EINVAL; |
| 521 | goto err; |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 522 | } |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 523 | |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 524 | mutex_init(&codec->mutex); |
| 525 | INIT_LIST_HEAD(&codec->dapm_widgets); |
| 526 | INIT_LIST_HEAD(&codec->dapm_paths); |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 527 | |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 528 | codec->private_data = wm8731; |
| 529 | codec->name = "WM8731"; |
| 530 | codec->owner = THIS_MODULE; |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 531 | codec->bias_level = SND_SOC_BIAS_OFF; |
| 532 | codec->set_bias_level = wm8731_set_bias_level; |
| 533 | codec->dai = &wm8731_dai; |
| 534 | codec->num_dai = 1; |
| 535 | codec->reg_cache_size = WM8731_CACHEREGNUM; |
| 536 | codec->reg_cache = &wm8731->reg_cache; |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 537 | |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 538 | memcpy(codec->reg_cache, wm8731_reg, sizeof(wm8731_reg)); |
| 539 | |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 540 | ret = snd_soc_codec_set_cache_io(codec, 7, 9, control); |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 541 | if (ret < 0) { |
| 542 | dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret); |
| 543 | goto err; |
| 544 | } |
| 545 | |
Mark Brown | 519cf2d | 2009-02-18 21:06:01 +0000 | [diff] [blame] | 546 | ret = wm8731_reset(codec); |
| 547 | if (ret < 0) { |
Mark Brown | fe5422f | 2009-07-05 15:18:01 +0100 | [diff] [blame] | 548 | dev_err(codec->dev, "Failed to issue reset: %d\n", ret); |
| 549 | goto err; |
Mark Brown | 519cf2d | 2009-02-18 21:06:01 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 552 | wm8731_dai.dev = codec->dev; |
| 553 | |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 554 | wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
| 555 | |
| 556 | /* Latch the update bits */ |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 557 | snd_soc_update_bits(codec, WM8731_LOUT1V, 0x100, 0); |
| 558 | snd_soc_update_bits(codec, WM8731_ROUT1V, 0x100, 0); |
| 559 | snd_soc_update_bits(codec, WM8731_LINVOL, 0x100, 0); |
| 560 | snd_soc_update_bits(codec, WM8731_RINVOL, 0x100, 0); |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 561 | |
Mark Brown | ce3bdaa | 2009-02-19 14:29:49 +0000 | [diff] [blame] | 562 | /* Disable bypass path by default */ |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 563 | snd_soc_update_bits(codec, WM8731_APANA, 0x4, 0); |
Mark Brown | ce3bdaa | 2009-02-19 14:29:49 +0000 | [diff] [blame] | 564 | |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 565 | wm8731_codec = codec; |
| 566 | |
| 567 | ret = snd_soc_register_codec(codec); |
| 568 | if (ret != 0) { |
| 569 | dev_err(codec->dev, "Failed to register codec: %d\n", ret); |
Mark Brown | fe5422f | 2009-07-05 15:18:01 +0100 | [diff] [blame] | 570 | goto err; |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | ret = snd_soc_register_dai(&wm8731_dai); |
| 574 | if (ret != 0) { |
| 575 | dev_err(codec->dev, "Failed to register DAI: %d\n", ret); |
| 576 | snd_soc_unregister_codec(codec); |
Mark Brown | fe5422f | 2009-07-05 15:18:01 +0100 | [diff] [blame] | 577 | goto err_codec; |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 580 | return 0; |
Mark Brown | fe5422f | 2009-07-05 15:18:01 +0100 | [diff] [blame] | 581 | |
| 582 | err_codec: |
| 583 | snd_soc_unregister_codec(codec); |
| 584 | err: |
| 585 | kfree(wm8731); |
| 586 | return ret; |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 589 | static void wm8731_unregister(struct wm8731_priv *wm8731) |
| 590 | { |
| 591 | wm8731_set_bias_level(&wm8731->codec, SND_SOC_BIAS_OFF); |
| 592 | snd_soc_unregister_dai(&wm8731_dai); |
| 593 | snd_soc_unregister_codec(&wm8731->codec); |
| 594 | kfree(wm8731); |
| 595 | wm8731_codec = NULL; |
| 596 | } |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 597 | |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 598 | #if defined(CONFIG_SPI_MASTER) |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 599 | static int __devinit wm8731_spi_probe(struct spi_device *spi) |
| 600 | { |
| 601 | struct snd_soc_codec *codec; |
| 602 | struct wm8731_priv *wm8731; |
| 603 | |
| 604 | wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL); |
| 605 | if (wm8731 == NULL) |
| 606 | return -ENOMEM; |
| 607 | |
| 608 | codec = &wm8731->codec; |
| 609 | codec->control_data = spi; |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 610 | codec->dev = &spi->dev; |
| 611 | |
Greg Kroah-Hartman | ae31c1f | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 612 | dev_set_drvdata(&spi->dev, wm8731); |
Mark Brown | 93b760b | 2009-02-18 12:44:40 +0000 | [diff] [blame] | 613 | |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 614 | return wm8731_register(wm8731, SND_SOC_SPI); |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | static int __devexit wm8731_spi_remove(struct spi_device *spi) |
| 618 | { |
Greg Kroah-Hartman | ae31c1f | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 619 | struct wm8731_priv *wm8731 = dev_get_drvdata(&spi->dev); |
Mark Brown | 93b760b | 2009-02-18 12:44:40 +0000 | [diff] [blame] | 620 | |
| 621 | wm8731_unregister(wm8731); |
| 622 | |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 623 | return 0; |
| 624 | } |
| 625 | |
Mark Brown | b3b50b3 | 2009-06-13 22:30:18 +0100 | [diff] [blame] | 626 | #ifdef CONFIG_PM |
| 627 | static int wm8731_spi_suspend(struct spi_device *spi, pm_message_t msg) |
| 628 | { |
| 629 | return snd_soc_suspend_device(&spi->dev); |
| 630 | } |
| 631 | |
| 632 | static int wm8731_spi_resume(struct spi_device *spi) |
| 633 | { |
| 634 | return snd_soc_resume_device(&spi->dev); |
| 635 | } |
| 636 | #else |
| 637 | #define wm8731_spi_suspend NULL |
| 638 | #define wm8731_spi_resume NULL |
| 639 | #endif |
| 640 | |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 641 | static struct spi_driver wm8731_spi_driver = { |
| 642 | .driver = { |
| 643 | .name = "wm8731", |
| 644 | .bus = &spi_bus_type, |
| 645 | .owner = THIS_MODULE, |
| 646 | }, |
| 647 | .probe = wm8731_spi_probe, |
Mark Brown | b3b50b3 | 2009-06-13 22:30:18 +0100 | [diff] [blame] | 648 | .suspend = wm8731_spi_suspend, |
| 649 | .resume = wm8731_spi_resume, |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 650 | .remove = __devexit_p(wm8731_spi_remove), |
| 651 | }; |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 652 | #endif /* CONFIG_SPI_MASTER */ |
| 653 | |
| 654 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
Mark Brown | c6f2981 | 2009-02-18 21:25:40 +0000 | [diff] [blame] | 655 | static __devinit int wm8731_i2c_probe(struct i2c_client *i2c, |
| 656 | const struct i2c_device_id *id) |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 657 | { |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 658 | struct wm8731_priv *wm8731; |
| 659 | struct snd_soc_codec *codec; |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 660 | |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 661 | wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL); |
| 662 | if (wm8731 == NULL) |
| 663 | return -ENOMEM; |
| 664 | |
| 665 | codec = &wm8731->codec; |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 666 | |
| 667 | i2c_set_clientdata(i2c, wm8731); |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 668 | codec->control_data = i2c; |
| 669 | |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 670 | codec->dev = &i2c->dev; |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 671 | |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 672 | return wm8731_register(wm8731, SND_SOC_I2C); |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Mark Brown | c6f2981 | 2009-02-18 21:25:40 +0000 | [diff] [blame] | 675 | static __devexit int wm8731_i2c_remove(struct i2c_client *client) |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 676 | { |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 677 | struct wm8731_priv *wm8731 = i2c_get_clientdata(client); |
| 678 | wm8731_unregister(wm8731); |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 679 | return 0; |
| 680 | } |
| 681 | |
Mark Brown | b3b50b3 | 2009-06-13 22:30:18 +0100 | [diff] [blame] | 682 | #ifdef CONFIG_PM |
| 683 | static int wm8731_i2c_suspend(struct i2c_client *i2c, pm_message_t msg) |
| 684 | { |
| 685 | return snd_soc_suspend_device(&i2c->dev); |
| 686 | } |
| 687 | |
| 688 | static int wm8731_i2c_resume(struct i2c_client *i2c) |
| 689 | { |
| 690 | return snd_soc_resume_device(&i2c->dev); |
| 691 | } |
| 692 | #else |
| 693 | #define wm8731_i2c_suspend NULL |
| 694 | #define wm8731_i2c_resume NULL |
| 695 | #endif |
| 696 | |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 697 | static const struct i2c_device_id wm8731_i2c_id[] = { |
| 698 | { "wm8731", 0 }, |
| 699 | { } |
| 700 | }; |
| 701 | MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id); |
| 702 | |
| 703 | static struct i2c_driver wm8731_i2c_driver = { |
| 704 | .driver = { |
| 705 | .name = "WM8731 I2C Codec", |
| 706 | .owner = THIS_MODULE, |
| 707 | }, |
| 708 | .probe = wm8731_i2c_probe, |
Mark Brown | c6f2981 | 2009-02-18 21:25:40 +0000 | [diff] [blame] | 709 | .remove = __devexit_p(wm8731_i2c_remove), |
Mark Brown | b3b50b3 | 2009-06-13 22:30:18 +0100 | [diff] [blame] | 710 | .suspend = wm8731_i2c_suspend, |
| 711 | .resume = wm8731_i2c_resume, |
Mark Brown | a8035c8 | 2009-02-16 19:35:43 +0000 | [diff] [blame] | 712 | .id_table = wm8731_i2c_id, |
| 713 | }; |
| 714 | #endif |
| 715 | |
Takashi Iwai | c9b3a40 | 2008-12-10 07:47:22 +0100 | [diff] [blame] | 716 | static int __init wm8731_modinit(void) |
Mark Brown | 64089b8 | 2008-12-08 19:17:58 +0000 | [diff] [blame] | 717 | { |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 718 | int ret; |
| 719 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 720 | ret = i2c_add_driver(&wm8731_i2c_driver); |
| 721 | if (ret != 0) { |
| 722 | printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n", |
| 723 | ret); |
| 724 | } |
| 725 | #endif |
| 726 | #if defined(CONFIG_SPI_MASTER) |
| 727 | ret = spi_register_driver(&wm8731_spi_driver); |
| 728 | if (ret != 0) { |
| 729 | printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n", |
| 730 | ret); |
| 731 | } |
| 732 | #endif |
| 733 | return 0; |
Mark Brown | 64089b8 | 2008-12-08 19:17:58 +0000 | [diff] [blame] | 734 | } |
| 735 | module_init(wm8731_modinit); |
| 736 | |
| 737 | static void __exit wm8731_exit(void) |
| 738 | { |
Mark Brown | 5998102 | 2009-02-16 20:49:16 +0000 | [diff] [blame] | 739 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 740 | i2c_del_driver(&wm8731_i2c_driver); |
| 741 | #endif |
| 742 | #if defined(CONFIG_SPI_MASTER) |
| 743 | spi_unregister_driver(&wm8731_spi_driver); |
| 744 | #endif |
Mark Brown | 64089b8 | 2008-12-08 19:17:58 +0000 | [diff] [blame] | 745 | } |
| 746 | module_exit(wm8731_exit); |
| 747 | |
Richard Purdie | 40e0aa6 | 2006-10-06 18:36:07 +0200 | [diff] [blame] | 748 | MODULE_DESCRIPTION("ASoC WM8731 driver"); |
| 749 | MODULE_AUTHOR("Richard Purdie"); |
| 750 | MODULE_LICENSE("GPL"); |