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