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