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