Liam Girdwood | 1f53aee | 2007-04-16 19:17:44 +0200 | [diff] [blame] | 1 | /* |
| 2 | * wm8753.c -- WM8753 ALSA Soc Audio driver |
| 3 | * |
| 4 | * Copyright 2003 Wolfson Microelectronics PLC. |
| 5 | * Author: Liam Girdwood |
| 6 | * liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | * |
| 13 | * Notes: |
| 14 | * The WM8753 is a low power, high quality stereo codec with integrated PCM |
| 15 | * codec designed for portable digital telephony applications. |
| 16 | * |
| 17 | * Dual DAI:- |
| 18 | * |
| 19 | * This driver support 2 DAI PCM's. This makes the default PCM available for |
| 20 | * HiFi audio (e.g. MP3, ogg) playback/capture and the other PCM available for |
| 21 | * voice. |
| 22 | * |
| 23 | * Please note that the voice PCM can be connected directly to a Bluetooth |
| 24 | * codec or GSM modem and thus cannot be read or written to, although it is |
| 25 | * available to be configured with snd_hw_params(), etc and kcontrols in the |
| 26 | * normal alsa manner. |
| 27 | * |
| 28 | * Fast DAI switching:- |
| 29 | * |
| 30 | * The driver can now fast switch between the DAI configurations via a |
| 31 | * an alsa kcontrol. This allows the PCM to remain open. |
| 32 | * |
| 33 | */ |
| 34 | |
| 35 | #include <linux/module.h> |
| 36 | #include <linux/moduleparam.h> |
| 37 | #include <linux/version.h> |
| 38 | #include <linux/kernel.h> |
| 39 | #include <linux/init.h> |
| 40 | #include <linux/delay.h> |
| 41 | #include <linux/pm.h> |
| 42 | #include <linux/i2c.h> |
| 43 | #include <linux/platform_device.h> |
Liam Girdwood | 1f53aee | 2007-04-16 19:17:44 +0200 | [diff] [blame] | 44 | #include <sound/core.h> |
| 45 | #include <sound/pcm.h> |
| 46 | #include <sound/pcm_params.h> |
| 47 | #include <sound/soc.h> |
| 48 | #include <sound/soc-dapm.h> |
| 49 | #include <sound/initval.h> |
Liam Girdwood | 2d6a4ac | 2008-01-10 14:43:48 +0100 | [diff] [blame] | 50 | #include <sound/tlv.h> |
Liam Girdwood | 1f53aee | 2007-04-16 19:17:44 +0200 | [diff] [blame] | 51 | #include <asm/div64.h> |
| 52 | |
| 53 | #include "wm8753.h" |
| 54 | |
| 55 | #define AUDIO_NAME "wm8753" |
| 56 | #define WM8753_VERSION "0.16" |
| 57 | |
| 58 | /* |
| 59 | * Debug |
| 60 | */ |
| 61 | |
| 62 | #define WM8753_DEBUG 0 |
| 63 | |
| 64 | #ifdef WM8753_DEBUG |
| 65 | #define dbg(format, arg...) \ |
| 66 | printk(KERN_DEBUG AUDIO_NAME ": " format "\n" , ## arg) |
| 67 | #else |
| 68 | #define dbg(format, arg...) do {} while (0) |
| 69 | #endif |
| 70 | #define err(format, arg...) \ |
| 71 | printk(KERN_ERR AUDIO_NAME ": " format "\n" , ## arg) |
| 72 | #define info(format, arg...) \ |
| 73 | printk(KERN_INFO AUDIO_NAME ": " format "\n" , ## arg) |
| 74 | #define warn(format, arg...) \ |
| 75 | printk(KERN_WARNING AUDIO_NAME ": " format "\n" , ## arg) |
| 76 | |
| 77 | static int caps_charge = 2000; |
| 78 | module_param(caps_charge, int, 0); |
| 79 | MODULE_PARM_DESC(caps_charge, "WM8753 cap charge time (msecs)"); |
| 80 | |
| 81 | static void wm8753_set_dai_mode(struct snd_soc_codec *codec, |
| 82 | unsigned int mode); |
| 83 | |
| 84 | /* codec private data */ |
| 85 | struct wm8753_priv { |
| 86 | unsigned int sysclk; |
| 87 | unsigned int pcmclk; |
| 88 | }; |
| 89 | |
| 90 | /* |
| 91 | * wm8753 register cache |
| 92 | * We can't read the WM8753 register space when we |
| 93 | * are using 2 wire for device control, so we cache them instead. |
| 94 | */ |
| 95 | static const u16 wm8753_reg[] = { |
| 96 | 0x0008, 0x0000, 0x000a, 0x000a, |
| 97 | 0x0033, 0x0000, 0x0007, 0x00ff, |
| 98 | 0x00ff, 0x000f, 0x000f, 0x007b, |
| 99 | 0x0000, 0x0032, 0x0000, 0x00c3, |
| 100 | 0x00c3, 0x00c0, 0x0000, 0x0000, |
| 101 | 0x0000, 0x0000, 0x0000, 0x0000, |
| 102 | 0x0000, 0x0000, 0x0000, 0x0000, |
| 103 | 0x0000, 0x0000, 0x0000, 0x0055, |
| 104 | 0x0005, 0x0050, 0x0055, 0x0050, |
| 105 | 0x0055, 0x0050, 0x0055, 0x0079, |
| 106 | 0x0079, 0x0079, 0x0079, 0x0079, |
| 107 | 0x0000, 0x0000, 0x0000, 0x0000, |
| 108 | 0x0097, 0x0097, 0x0000, 0x0004, |
| 109 | 0x0000, 0x0083, 0x0024, 0x01ba, |
| 110 | 0x0000, 0x0083, 0x0024, 0x01ba, |
| 111 | 0x0000, 0x0000 |
| 112 | }; |
| 113 | |
| 114 | /* |
| 115 | * read wm8753 register cache |
| 116 | */ |
| 117 | static inline unsigned int wm8753_read_reg_cache(struct snd_soc_codec *codec, |
| 118 | unsigned int reg) |
| 119 | { |
| 120 | u16 *cache = codec->reg_cache; |
| 121 | if (reg < 1 || reg > (ARRAY_SIZE(wm8753_reg) + 1)) |
| 122 | return -1; |
| 123 | return cache[reg - 1]; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * write wm8753 register cache |
| 128 | */ |
| 129 | static inline void wm8753_write_reg_cache(struct snd_soc_codec *codec, |
| 130 | unsigned int reg, unsigned int value) |
| 131 | { |
| 132 | u16 *cache = codec->reg_cache; |
| 133 | if (reg < 1 || reg > 0x3f) |
| 134 | return; |
| 135 | cache[reg - 1] = value; |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * write to the WM8753 register space |
| 140 | */ |
| 141 | static int wm8753_write(struct snd_soc_codec *codec, unsigned int reg, |
| 142 | unsigned int value) |
| 143 | { |
| 144 | u8 data[2]; |
| 145 | |
| 146 | /* data is |
| 147 | * D15..D9 WM8753 register offset |
| 148 | * D8...D0 register data |
| 149 | */ |
| 150 | data[0] = (reg << 1) | ((value >> 8) & 0x0001); |
| 151 | data[1] = value & 0x00ff; |
| 152 | |
Mark Brown | 60fc684 | 2008-04-30 17:18:43 +0200 | [diff] [blame^] | 153 | wm8753_write_reg_cache(codec, reg, value); |
Liam Girdwood | 1f53aee | 2007-04-16 19:17:44 +0200 | [diff] [blame] | 154 | if (codec->hw_write(codec->control_data, data, 2) == 2) |
| 155 | return 0; |
| 156 | else |
| 157 | return -EIO; |
| 158 | } |
| 159 | |
| 160 | #define wm8753_reset(c) wm8753_write(c, WM8753_RESET, 0) |
| 161 | |
| 162 | /* |
| 163 | * WM8753 Controls |
| 164 | */ |
| 165 | static const char *wm8753_base[] = {"Linear Control", "Adaptive Boost"}; |
| 166 | static const char *wm8753_base_filter[] = |
| 167 | {"130Hz @ 48kHz", "200Hz @ 48kHz", "100Hz @ 16kHz", "400Hz @ 48kHz", |
| 168 | "100Hz @ 8kHz", "200Hz @ 8kHz"}; |
| 169 | static const char *wm8753_treble[] = {"8kHz", "4kHz"}; |
| 170 | static const char *wm8753_alc_func[] = {"Off", "Right", "Left", "Stereo"}; |
| 171 | static const char *wm8753_ng_type[] = {"Constant PGA Gain", "Mute ADC Output"}; |
| 172 | static const char *wm8753_3d_func[] = {"Capture", "Playback"}; |
| 173 | static const char *wm8753_3d_uc[] = {"2.2kHz", "1.5kHz"}; |
| 174 | static const char *wm8753_3d_lc[] = {"200Hz", "500Hz"}; |
| 175 | static const char *wm8753_deemp[] = {"None", "32kHz", "44.1kHz", "48kHz"}; |
| 176 | static const char *wm8753_mono_mix[] = {"Stereo", "Left", "Right", "Mono"}; |
| 177 | static const char *wm8753_dac_phase[] = {"Non Inverted", "Inverted"}; |
| 178 | static const char *wm8753_line_mix[] = {"Line 1 + 2", "Line 1 - 2", |
| 179 | "Line 1", "Line 2"}; |
| 180 | static const char *wm8753_mono_mux[] = {"Line Mix", "Rx Mix"}; |
| 181 | static const char *wm8753_right_mux[] = {"Line 2", "Rx Mix"}; |
| 182 | static const char *wm8753_left_mux[] = {"Line 1", "Rx Mix"}; |
| 183 | static const char *wm8753_rxmsel[] = {"RXP - RXN", "RXP + RXN", "RXP", "RXN"}; |
| 184 | static const char *wm8753_sidetone_mux[] = {"Left PGA", "Mic 1", "Mic 2", |
| 185 | "Right PGA"}; |
| 186 | static const char *wm8753_mono2_src[] = {"Inverted Mono 1", "Left", "Right", |
| 187 | "Left + Right"}; |
| 188 | static const char *wm8753_out3[] = {"VREF", "ROUT2", "Left + Right"}; |
| 189 | static const char *wm8753_out4[] = {"VREF", "Capture ST", "LOUT2"}; |
| 190 | static const char *wm8753_radcsel[] = {"PGA", "Line or RXP-RXN", "Sidetone"}; |
| 191 | static const char *wm8753_ladcsel[] = {"PGA", "Line or RXP-RXN", "Line"}; |
| 192 | static const char *wm8753_mono_adc[] = {"Stereo", "Analogue Mix Left", |
| 193 | "Analogue Mix Right", "Digital Mono Mix"}; |
| 194 | static const char *wm8753_adc_hp[] = {"3.4Hz @ 48kHz", "82Hz @ 16k", |
| 195 | "82Hz @ 8kHz", "170Hz @ 8kHz"}; |
| 196 | static const char *wm8753_adc_filter[] = {"HiFi", "Voice"}; |
| 197 | static const char *wm8753_mic_sel[] = {"Mic 1", "Mic 2", "Mic 3"}; |
| 198 | static const char *wm8753_dai_mode[] = {"DAI 0", "DAI 1", "DAI 2", "DAI 3"}; |
| 199 | static const char *wm8753_dat_sel[] = {"Stereo", "Left ADC", "Right ADC", |
| 200 | "Channel Swap"}; |
Graeme Gregory | ae092c9 | 2008-03-03 17:19:45 +0100 | [diff] [blame] | 201 | static const char *wm8753_rout2_phase[] = {"Non Inverted", "Inverted"}; |
Liam Girdwood | 1f53aee | 2007-04-16 19:17:44 +0200 | [diff] [blame] | 202 | |
| 203 | static const struct soc_enum wm8753_enum[] = { |
| 204 | SOC_ENUM_SINGLE(WM8753_BASS, 7, 2, wm8753_base), |
| 205 | SOC_ENUM_SINGLE(WM8753_BASS, 4, 6, wm8753_base_filter), |
| 206 | SOC_ENUM_SINGLE(WM8753_TREBLE, 6, 2, wm8753_treble), |
| 207 | SOC_ENUM_SINGLE(WM8753_ALC1, 7, 4, wm8753_alc_func), |
| 208 | SOC_ENUM_SINGLE(WM8753_NGATE, 1, 2, wm8753_ng_type), |
| 209 | SOC_ENUM_SINGLE(WM8753_3D, 7, 2, wm8753_3d_func), |
| 210 | SOC_ENUM_SINGLE(WM8753_3D, 6, 2, wm8753_3d_uc), |
| 211 | SOC_ENUM_SINGLE(WM8753_3D, 5, 2, wm8753_3d_lc), |
| 212 | SOC_ENUM_SINGLE(WM8753_DAC, 1, 4, wm8753_deemp), |
| 213 | SOC_ENUM_SINGLE(WM8753_DAC, 4, 4, wm8753_mono_mix), |
| 214 | SOC_ENUM_SINGLE(WM8753_DAC, 6, 2, wm8753_dac_phase), |
| 215 | SOC_ENUM_SINGLE(WM8753_INCTL1, 3, 4, wm8753_line_mix), |
| 216 | SOC_ENUM_SINGLE(WM8753_INCTL1, 2, 2, wm8753_mono_mux), |
| 217 | SOC_ENUM_SINGLE(WM8753_INCTL1, 1, 2, wm8753_right_mux), |
| 218 | SOC_ENUM_SINGLE(WM8753_INCTL1, 0, 2, wm8753_left_mux), |
| 219 | SOC_ENUM_SINGLE(WM8753_INCTL2, 6, 4, wm8753_rxmsel), |
| 220 | SOC_ENUM_SINGLE(WM8753_INCTL2, 4, 4, wm8753_sidetone_mux), |
| 221 | SOC_ENUM_SINGLE(WM8753_OUTCTL, 7, 4, wm8753_mono2_src), |
| 222 | SOC_ENUM_SINGLE(WM8753_OUTCTL, 0, 3, wm8753_out3), |
| 223 | SOC_ENUM_SINGLE(WM8753_ADCTL2, 7, 3, wm8753_out4), |
| 224 | SOC_ENUM_SINGLE(WM8753_ADCIN, 2, 3, wm8753_radcsel), |
| 225 | SOC_ENUM_SINGLE(WM8753_ADCIN, 0, 3, wm8753_ladcsel), |
| 226 | SOC_ENUM_SINGLE(WM8753_ADCIN, 4, 4, wm8753_mono_adc), |
| 227 | SOC_ENUM_SINGLE(WM8753_ADC, 2, 4, wm8753_adc_hp), |
| 228 | SOC_ENUM_SINGLE(WM8753_ADC, 4, 2, wm8753_adc_filter), |
| 229 | SOC_ENUM_SINGLE(WM8753_MICBIAS, 6, 3, wm8753_mic_sel), |
| 230 | SOC_ENUM_SINGLE(WM8753_IOCTL, 2, 4, wm8753_dai_mode), |
| 231 | SOC_ENUM_SINGLE(WM8753_ADC, 7, 4, wm8753_dat_sel), |
Graeme Gregory | ae092c9 | 2008-03-03 17:19:45 +0100 | [diff] [blame] | 232 | SOC_ENUM_SINGLE(WM8753_OUTCTL, 2, 2, wm8753_rout2_phase), |
Liam Girdwood | 1f53aee | 2007-04-16 19:17:44 +0200 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | |
| 236 | static int wm8753_get_dai(struct snd_kcontrol *kcontrol, |
| 237 | struct snd_ctl_elem_value *ucontrol) |
| 238 | { |
| 239 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 240 | int mode = wm8753_read_reg_cache(codec, WM8753_IOCTL); |
| 241 | |
| 242 | ucontrol->value.integer.value[0] = (mode & 0xc) >> 2; |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static int wm8753_set_dai(struct snd_kcontrol *kcontrol, |
| 247 | struct snd_ctl_elem_value *ucontrol) |
| 248 | { |
| 249 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 250 | int mode = wm8753_read_reg_cache(codec, WM8753_IOCTL); |
| 251 | |
Mark Brown | 60fc684 | 2008-04-30 17:18:43 +0200 | [diff] [blame^] | 252 | if (((mode & 0xc) >> 2) == ucontrol->value.integer.value[0]) |
Liam Girdwood | 1f53aee | 2007-04-16 19:17:44 +0200 | [diff] [blame] | 253 | return 0; |
| 254 | |
| 255 | mode &= 0xfff3; |
| 256 | mode |= (ucontrol->value.integer.value[0] << 2); |
| 257 | |
| 258 | wm8753_write(codec, WM8753_IOCTL, mode); |
| 259 | wm8753_set_dai_mode(codec, ucontrol->value.integer.value[0]); |
| 260 | return 1; |
| 261 | } |
| 262 | |
Liam Girdwood | 2d6a4ac | 2008-01-10 14:43:48 +0100 | [diff] [blame] | 263 | static const DECLARE_TLV_DB_LINEAR(rec_mix_tlv, -1500, 600); |
| 264 | |
Liam Girdwood | 1f53aee | 2007-04-16 19:17:44 +0200 | [diff] [blame] | 265 | static const struct snd_kcontrol_new wm8753_snd_controls[] = { |
| 266 | SOC_DOUBLE_R("PCM Volume", WM8753_LDAC, WM8753_RDAC, 0, 255, 0), |
| 267 | |
| 268 | SOC_DOUBLE_R("ADC Capture Volume", WM8753_LADC, WM8753_RADC, 0, 255, 0), |
| 269 | |
| 270 | SOC_DOUBLE_R("Headphone Playback Volume", WM8753_LOUT1V, WM8753_ROUT1V, 0, 127, 0), |
| 271 | SOC_DOUBLE_R("Speaker Playback Volume", WM8753_LOUT2V, WM8753_ROUT2V, 0, 127, 0), |
| 272 | |
| 273 | SOC_SINGLE("Mono Playback Volume", WM8753_MOUTV, 0, 127, 0), |
| 274 | |
| 275 | SOC_DOUBLE_R("Bypass Playback Volume", WM8753_LOUTM1, WM8753_ROUTM1, 4, 7, 1), |
| 276 | SOC_DOUBLE_R("Sidetone Playback Volume", WM8753_LOUTM2, WM8753_ROUTM2, 4, 7, 1), |
| 277 | SOC_DOUBLE_R("Voice Playback Volume", WM8753_LOUTM2, WM8753_ROUTM2, 0, 7, 1), |
| 278 | |
| 279 | SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8753_LOUT1V, WM8753_ROUT1V, 7, 1, 0), |
| 280 | SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8753_LOUT2V, WM8753_ROUT2V, 7, 1, 0), |
| 281 | |
| 282 | SOC_SINGLE("Mono Bypass Playback Volume", WM8753_MOUTM1, 4, 7, 1), |
| 283 | SOC_SINGLE("Mono Sidetone Playback Volume", WM8753_MOUTM2, 4, 7, 1), |
Mike Montour | 1026460 | 2008-02-01 13:12:12 +0100 | [diff] [blame] | 284 | SOC_SINGLE("Mono Voice Playback Volume", WM8753_MOUTM2, 0, 7, 1), |
Liam Girdwood | 1f53aee | 2007-04-16 19:17:44 +0200 | [diff] [blame] | 285 | SOC_SINGLE("Mono Playback ZC Switch", WM8753_MOUTV, 7, 1, 0), |
| 286 | |
| 287 | SOC_ENUM("Bass Boost", wm8753_enum[0]), |
| 288 | SOC_ENUM("Bass Filter", wm8753_enum[1]), |
| 289 | SOC_SINGLE("Bass Volume", WM8753_BASS, 0, 15, 1), |
| 290 | |
| 291 | SOC_SINGLE("Treble Volume", WM8753_TREBLE, 0, 15, 1), |
| 292 | SOC_ENUM("Treble Cut-off", wm8753_enum[2]), |
| 293 | |
Liam Girdwood | 2d6a4ac | 2008-01-10 14:43:48 +0100 | [diff] [blame] | 294 | SOC_DOUBLE_TLV("Sidetone Capture Volume", WM8753_RECMIX1, 0, 4, 7, 1, rec_mix_tlv), |
| 295 | SOC_SINGLE_TLV("Voice Sidetone Capture Volume", WM8753_RECMIX2, 0, 7, 1, rec_mix_tlv), |
Liam Girdwood | 1f53aee | 2007-04-16 19:17:44 +0200 | [diff] [blame] | 296 | |
| 297 | SOC_DOUBLE_R("Capture Volume", WM8753_LINVOL, WM8753_RINVOL, 0, 63, 0), |
| 298 | SOC_DOUBLE_R("Capture ZC Switch", WM8753_LINVOL, WM8753_RINVOL, 6, 1, 0), |
| 299 | SOC_DOUBLE_R("Capture Switch", WM8753_LINVOL, WM8753_RINVOL, 7, 1, 1), |
| 300 | |
| 301 | SOC_ENUM("Capture Filter Select", wm8753_enum[23]), |
| 302 | SOC_ENUM("Capture Filter Cut-off", wm8753_enum[24]), |
| 303 | SOC_SINGLE("Capture Filter Switch", WM8753_ADC, 0, 1, 1), |
| 304 | |
| 305 | SOC_SINGLE("ALC Capture Target Volume", WM8753_ALC1, 0, 7, 0), |
| 306 | SOC_SINGLE("ALC Capture Max Volume", WM8753_ALC1, 4, 7, 0), |
| 307 | SOC_ENUM("ALC Capture Function", wm8753_enum[3]), |
| 308 | SOC_SINGLE("ALC Capture ZC Switch", WM8753_ALC2, 8, 1, 0), |
| 309 | SOC_SINGLE("ALC Capture Hold Time", WM8753_ALC2, 0, 15, 1), |
| 310 | SOC_SINGLE("ALC Capture Decay Time", WM8753_ALC3, 4, 15, 1), |
| 311 | SOC_SINGLE("ALC Capture Attack Time", WM8753_ALC3, 0, 15, 0), |
| 312 | SOC_SINGLE("ALC Capture NG Threshold", WM8753_NGATE, 3, 31, 0), |
| 313 | SOC_ENUM("ALC Capture NG Type", wm8753_enum[4]), |
| 314 | SOC_SINGLE("ALC Capture NG Switch", WM8753_NGATE, 0, 1, 0), |
| 315 | |
| 316 | SOC_ENUM("3D Function", wm8753_enum[5]), |
| 317 | SOC_ENUM("3D Upper Cut-off", wm8753_enum[6]), |
| 318 | SOC_ENUM("3D Lower Cut-off", wm8753_enum[7]), |
| 319 | SOC_SINGLE("3D Volume", WM8753_3D, 1, 15, 0), |
| 320 | SOC_SINGLE("3D Switch", WM8753_3D, 0, 1, 0), |
| 321 | |
| 322 | SOC_SINGLE("Capture 6dB Attenuate", WM8753_ADCTL1, 2, 1, 0), |
| 323 | SOC_SINGLE("Playback 6dB Attenuate", WM8753_ADCTL1, 1, 1, 0), |
| 324 | |
| 325 | SOC_ENUM("De-emphasis", wm8753_enum[8]), |
| 326 | SOC_ENUM("Playback Mono Mix", wm8753_enum[9]), |
| 327 | SOC_ENUM("Playback Phase", wm8753_enum[10]), |
| 328 | |
| 329 | SOC_SINGLE("Mic2 Capture Volume", WM8753_INCTL1, 7, 3, 0), |
| 330 | SOC_SINGLE("Mic1 Capture Volume", WM8753_INCTL1, 5, 3, 0), |
| 331 | |
| 332 | SOC_ENUM_EXT("DAI Mode", wm8753_enum[26], wm8753_get_dai, wm8753_set_dai), |
| 333 | |
| 334 | SOC_ENUM("ADC Data Select", wm8753_enum[27]), |
Graeme Gregory | ae092c9 | 2008-03-03 17:19:45 +0100 | [diff] [blame] | 335 | SOC_ENUM("ROUT2 Phase", wm8753_enum[28]), |
Liam Girdwood | 1f53aee | 2007-04-16 19:17:44 +0200 | [diff] [blame] | 336 | }; |
| 337 | |
| 338 | /* add non dapm controls */ |
| 339 | static int wm8753_add_controls(struct snd_soc_codec *codec) |
| 340 | { |
| 341 | int err, i; |
| 342 | |
| 343 | for (i = 0; i < ARRAY_SIZE(wm8753_snd_controls); i++) { |
| 344 | err = snd_ctl_add(codec->card, |
Mark Brown | 60fc684 | 2008-04-30 17:18:43 +0200 | [diff] [blame^] | 345 | snd_soc_cnew(&wm8753_snd_controls[i], |
| 346 | codec, NULL)); |
Liam Girdwood | 1f53aee | 2007-04-16 19:17:44 +0200 | [diff] [blame] | 347 | if (err < 0) |
| 348 | return err; |
| 349 | } |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * _DAPM_ Controls |
| 355 | */ |
| 356 | |
| 357 | /* Left Mixer */ |
| 358 | static const struct snd_kcontrol_new wm8753_left_mixer_controls[] = { |
| 359 | SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_LOUTM2, 8, 1, 0), |
| 360 | SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_LOUTM2, 7, 1, 0), |
| 361 | SOC_DAPM_SINGLE("Left Playback Switch", WM8753_LOUTM1, 8, 1, 0), |
| 362 | SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_LOUTM1, 7, 1, 0), |
| 363 | }; |
| 364 | |
| 365 | /* Right mixer */ |
| 366 | static const struct snd_kcontrol_new wm8753_right_mixer_controls[] = { |
| 367 | SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_ROUTM2, 8, 1, 0), |
| 368 | SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_ROUTM2, 7, 1, 0), |
| 369 | SOC_DAPM_SINGLE("Right Playback Switch", WM8753_ROUTM1, 8, 1, 0), |
| 370 | SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_ROUTM1, 7, 1, 0), |
| 371 | }; |
| 372 | |
| 373 | /* Mono mixer */ |
| 374 | static const struct snd_kcontrol_new wm8753_mono_mixer_controls[] = { |
| 375 | SOC_DAPM_SINGLE("Left Playback Switch", WM8753_MOUTM1, 8, 1, 0), |
| 376 | SOC_DAPM_SINGLE("Right Playback Switch", WM8753_MOUTM2, 8, 1, 0), |
| 377 | SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_MOUTM2, 3, 1, 0), |
| 378 | SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_MOUTM2, 7, 1, 0), |
| 379 | SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_MOUTM1, 7, 1, 0), |
| 380 | }; |
| 381 | |
| 382 | /* Mono 2 Mux */ |
| 383 | static const struct snd_kcontrol_new wm8753_mono2_controls = |
| 384 | SOC_DAPM_ENUM("Route", wm8753_enum[17]); |
| 385 | |
| 386 | /* Out 3 Mux */ |
| 387 | static const struct snd_kcontrol_new wm8753_out3_controls = |
| 388 | SOC_DAPM_ENUM("Route", wm8753_enum[18]); |
| 389 | |
| 390 | /* Out 4 Mux */ |
| 391 | static const struct snd_kcontrol_new wm8753_out4_controls = |
| 392 | SOC_DAPM_ENUM("Route", wm8753_enum[19]); |
| 393 | |
| 394 | /* ADC Mono Mix */ |
| 395 | static const struct snd_kcontrol_new wm8753_adc_mono_controls = |
| 396 | SOC_DAPM_ENUM("Route", wm8753_enum[22]); |
| 397 | |
| 398 | /* Record mixer */ |
| 399 | static const struct snd_kcontrol_new wm8753_record_mixer_controls[] = { |
| 400 | SOC_DAPM_SINGLE("Voice Capture Switch", WM8753_RECMIX2, 3, 1, 0), |
| 401 | SOC_DAPM_SINGLE("Left Capture Switch", WM8753_RECMIX1, 3, 1, 0), |
| 402 | SOC_DAPM_SINGLE("Right Capture Switch", WM8753_RECMIX1, 7, 1, 0), |
| 403 | }; |
| 404 | |
| 405 | /* Left ADC mux */ |
| 406 | static const struct snd_kcontrol_new wm8753_adc_left_controls = |
| 407 | SOC_DAPM_ENUM("Route", wm8753_enum[21]); |
| 408 | |
| 409 | /* Right ADC mux */ |
| 410 | static const struct snd_kcontrol_new wm8753_adc_right_controls = |
| 411 | SOC_DAPM_ENUM("Route", wm8753_enum[20]); |
| 412 | |
| 413 | /* MIC mux */ |
| 414 | static const struct snd_kcontrol_new wm8753_mic_mux_controls = |
| 415 | SOC_DAPM_ENUM("Route", wm8753_enum[16]); |
| 416 | |
| 417 | /* ALC mixer */ |
| 418 | static const struct snd_kcontrol_new wm8753_alc_mixer_controls[] = { |
| 419 | SOC_DAPM_SINGLE("Line Capture Switch", WM8753_INCTL2, 3, 1, 0), |
| 420 | SOC_DAPM_SINGLE("Mic2 Capture Switch", WM8753_INCTL2, 2, 1, 0), |
| 421 | SOC_DAPM_SINGLE("Mic1 Capture Switch", WM8753_INCTL2, 1, 1, 0), |
| 422 | SOC_DAPM_SINGLE("Rx Capture Switch", WM8753_INCTL2, 0, 1, 0), |
| 423 | }; |
| 424 | |
| 425 | /* Left Line mux */ |
| 426 | static const struct snd_kcontrol_new wm8753_line_left_controls = |
| 427 | SOC_DAPM_ENUM("Route", wm8753_enum[14]); |
| 428 | |
| 429 | /* Right Line mux */ |
| 430 | static const struct snd_kcontrol_new wm8753_line_right_controls = |
| 431 | SOC_DAPM_ENUM("Route", wm8753_enum[13]); |
| 432 | |
| 433 | /* Mono Line mux */ |
| 434 | static const struct snd_kcontrol_new wm8753_line_mono_controls = |
| 435 | SOC_DAPM_ENUM("Route", wm8753_enum[12]); |
| 436 | |
| 437 | /* Line mux and mixer */ |
| 438 | static const struct snd_kcontrol_new wm8753_line_mux_mix_controls = |
| 439 | SOC_DAPM_ENUM("Route", wm8753_enum[11]); |
| 440 | |
| 441 | /* Rx mux and mixer */ |
| 442 | static const struct snd_kcontrol_new wm8753_rx_mux_mix_controls = |
| 443 | SOC_DAPM_ENUM("Route", wm8753_enum[15]); |
| 444 | |
| 445 | /* Mic Selector Mux */ |
| 446 | static const struct snd_kcontrol_new wm8753_mic_sel_mux_controls = |
| 447 | SOC_DAPM_ENUM("Route", wm8753_enum[25]); |
| 448 | |
| 449 | static const struct snd_soc_dapm_widget wm8753_dapm_widgets[] = { |
| 450 | SND_SOC_DAPM_MICBIAS("Mic Bias", WM8753_PWR1, 5, 0), |
| 451 | SND_SOC_DAPM_MIXER("Left Mixer", WM8753_PWR4, 0, 0, |
| 452 | &wm8753_left_mixer_controls[0], ARRAY_SIZE(wm8753_left_mixer_controls)), |
| 453 | SND_SOC_DAPM_PGA("Left Out 1", WM8753_PWR3, 8, 0, NULL, 0), |
| 454 | SND_SOC_DAPM_PGA("Left Out 2", WM8753_PWR3, 6, 0, NULL, 0), |
| 455 | SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback", WM8753_PWR1, 3, 0), |
| 456 | SND_SOC_DAPM_OUTPUT("LOUT1"), |
| 457 | SND_SOC_DAPM_OUTPUT("LOUT2"), |
| 458 | SND_SOC_DAPM_MIXER("Right Mixer", WM8753_PWR4, 1, 0, |
| 459 | &wm8753_right_mixer_controls[0], ARRAY_SIZE(wm8753_right_mixer_controls)), |
| 460 | SND_SOC_DAPM_PGA("Right Out 1", WM8753_PWR3, 7, 0, NULL, 0), |
| 461 | SND_SOC_DAPM_PGA("Right Out 2", WM8753_PWR3, 5, 0, NULL, 0), |
| 462 | SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback", WM8753_PWR1, 2, 0), |
| 463 | SND_SOC_DAPM_OUTPUT("ROUT1"), |
| 464 | SND_SOC_DAPM_OUTPUT("ROUT2"), |
| 465 | SND_SOC_DAPM_MIXER("Mono Mixer", WM8753_PWR4, 2, 0, |
| 466 | &wm8753_mono_mixer_controls[0], ARRAY_SIZE(wm8753_mono_mixer_controls)), |
| 467 | SND_SOC_DAPM_PGA("Mono Out 1", WM8753_PWR3, 2, 0, NULL, 0), |
| 468 | SND_SOC_DAPM_PGA("Mono Out 2", WM8753_PWR3, 1, 0, NULL, 0), |
| 469 | SND_SOC_DAPM_DAC("Voice DAC", "Voice Playback", WM8753_PWR1, 4, 0), |
| 470 | SND_SOC_DAPM_OUTPUT("MONO1"), |
| 471 | SND_SOC_DAPM_MUX("Mono 2 Mux", SND_SOC_NOPM, 0, 0, &wm8753_mono2_controls), |
| 472 | SND_SOC_DAPM_OUTPUT("MONO2"), |
| 473 | SND_SOC_DAPM_MIXER("Out3 Left + Right", -1, 0, 0, NULL, 0), |
| 474 | SND_SOC_DAPM_MUX("Out3 Mux", SND_SOC_NOPM, 0, 0, &wm8753_out3_controls), |
| 475 | SND_SOC_DAPM_PGA("Out 3", WM8753_PWR3, 4, 0, NULL, 0), |
| 476 | SND_SOC_DAPM_OUTPUT("OUT3"), |
| 477 | SND_SOC_DAPM_MUX("Out4 Mux", SND_SOC_NOPM, 0, 0, &wm8753_out4_controls), |
| 478 | SND_SOC_DAPM_PGA("Out 4", WM8753_PWR3, 3, 0, NULL, 0), |
| 479 | SND_SOC_DAPM_OUTPUT("OUT4"), |
| 480 | SND_SOC_DAPM_MIXER("Playback Mixer", WM8753_PWR4, 3, 0, |
| 481 | &wm8753_record_mixer_controls[0], |
| 482 | ARRAY_SIZE(wm8753_record_mixer_controls)), |
| 483 | SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8753_PWR2, 3, 0), |
| 484 | SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8753_PWR2, 2, 0), |
| 485 | SND_SOC_DAPM_MUX("Capture Left Mixer", SND_SOC_NOPM, 0, 0, |
| 486 | &wm8753_adc_mono_controls), |
| 487 | SND_SOC_DAPM_MUX("Capture Right Mixer", SND_SOC_NOPM, 0, 0, |
| 488 | &wm8753_adc_mono_controls), |
| 489 | SND_SOC_DAPM_MUX("Capture Left Mux", SND_SOC_NOPM, 0, 0, |
| 490 | &wm8753_adc_left_controls), |
| 491 | SND_SOC_DAPM_MUX("Capture Right Mux", SND_SOC_NOPM, 0, 0, |
| 492 | &wm8753_adc_right_controls), |
| 493 | SND_SOC_DAPM_MUX("Mic Sidetone Mux", SND_SOC_NOPM, 0, 0, |
| 494 | &wm8753_mic_mux_controls), |
| 495 | SND_SOC_DAPM_PGA("Left Capture Volume", WM8753_PWR2, 5, 0, NULL, 0), |
| 496 | SND_SOC_DAPM_PGA("Right Capture Volume", WM8753_PWR2, 4, 0, NULL, 0), |
| 497 | SND_SOC_DAPM_MIXER("ALC Mixer", WM8753_PWR2, 6, 0, |
| 498 | &wm8753_alc_mixer_controls[0], ARRAY_SIZE(wm8753_alc_mixer_controls)), |
| 499 | SND_SOC_DAPM_MUX("Line Left Mux", SND_SOC_NOPM, 0, 0, |
| 500 | &wm8753_line_left_controls), |
| 501 | SND_SOC_DAPM_MUX("Line Right Mux", SND_SOC_NOPM, 0, 0, |
| 502 | &wm8753_line_right_controls), |
| 503 | SND_SOC_DAPM_MUX("Line Mono Mux", SND_SOC_NOPM, 0, 0, |
| 504 | &wm8753_line_mono_controls), |
| 505 | SND_SOC_DAPM_MUX("Line Mixer", WM8753_PWR2, 0, 0, |
| 506 | &wm8753_line_mux_mix_controls), |
| 507 | SND_SOC_DAPM_MUX("Rx Mixer", WM8753_PWR2, 1, 0, |
| 508 | &wm8753_rx_mux_mix_controls), |
| 509 | SND_SOC_DAPM_PGA("Mic 1 Volume", WM8753_PWR2, 8, 0, NULL, 0), |
| 510 | SND_SOC_DAPM_PGA("Mic 2 Volume", WM8753_PWR2, 7, 0, NULL, 0), |
| 511 | SND_SOC_DAPM_MUX("Mic Selection Mux", SND_SOC_NOPM, 0, 0, |
| 512 | &wm8753_mic_sel_mux_controls), |
| 513 | SND_SOC_DAPM_INPUT("LINE1"), |
| 514 | SND_SOC_DAPM_INPUT("LINE2"), |
| 515 | SND_SOC_DAPM_INPUT("RXP"), |
| 516 | SND_SOC_DAPM_INPUT("RXN"), |
| 517 | SND_SOC_DAPM_INPUT("ACIN"), |
| 518 | SND_SOC_DAPM_OUTPUT("ACOP"), |
| 519 | SND_SOC_DAPM_INPUT("MIC1N"), |
| 520 | SND_SOC_DAPM_INPUT("MIC1"), |
| 521 | SND_SOC_DAPM_INPUT("MIC2N"), |
| 522 | SND_SOC_DAPM_INPUT("MIC2"), |
| 523 | SND_SOC_DAPM_VMID("VREF"), |
| 524 | }; |
| 525 | |
| 526 | static const char *audio_map[][3] = { |
| 527 | /* left mixer */ |
| 528 | {"Left Mixer", "Left Playback Switch", "Left DAC"}, |
| 529 | {"Left Mixer", "Voice Playback Switch", "Voice DAC"}, |
| 530 | {"Left Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"}, |
| 531 | {"Left Mixer", "Bypass Playback Switch", "Line Left Mux"}, |
| 532 | |
| 533 | /* right mixer */ |
| 534 | {"Right Mixer", "Right Playback Switch", "Right DAC"}, |
| 535 | {"Right Mixer", "Voice Playback Switch", "Voice DAC"}, |
| 536 | {"Right Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"}, |
| 537 | {"Right Mixer", "Bypass Playback Switch", "Line Right Mux"}, |
| 538 | |
| 539 | /* mono mixer */ |
| 540 | {"Mono Mixer", "Voice Playback Switch", "Voice DAC"}, |
| 541 | {"Mono Mixer", "Left Playback Switch", "Left DAC"}, |
| 542 | {"Mono Mixer", "Right Playback Switch", "Right DAC"}, |
| 543 | {"Mono Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"}, |
| 544 | {"Mono Mixer", "Bypass Playback Switch", "Line Mono Mux"}, |
| 545 | |
| 546 | /* left out */ |
| 547 | {"Left Out 1", NULL, "Left Mixer"}, |
| 548 | {"Left Out 2", NULL, "Left Mixer"}, |
| 549 | {"LOUT1", NULL, "Left Out 1"}, |
| 550 | {"LOUT2", NULL, "Left Out 2"}, |
| 551 | |
| 552 | /* right out */ |
| 553 | {"Right Out 1", NULL, "Right Mixer"}, |
| 554 | {"Right Out 2", NULL, "Right Mixer"}, |
| 555 | {"ROUT1", NULL, "Right Out 1"}, |
| 556 | {"ROUT2", NULL, "Right Out 2"}, |
| 557 | |
| 558 | /* mono 1 out */ |
| 559 | {"Mono Out 1", NULL, "Mono Mixer"}, |
| 560 | {"MONO1", NULL, "Mono Out 1"}, |
| 561 | |
| 562 | /* mono 2 out */ |
| 563 | {"Mono 2 Mux", "Left + Right", "Out3 Left + Right"}, |
| 564 | {"Mono 2 Mux", "Inverted Mono 1", "MONO1"}, |
| 565 | {"Mono 2 Mux", "Left", "Left Mixer"}, |
| 566 | {"Mono 2 Mux", "Right", "Right Mixer"}, |
| 567 | {"Mono Out 2", NULL, "Mono 2 Mux"}, |
| 568 | {"MONO2", NULL, "Mono Out 2"}, |
| 569 | |
| 570 | /* out 3 */ |
| 571 | {"Out3 Left + Right", NULL, "Left Mixer"}, |
| 572 | {"Out3 Left + Right", NULL, "Right Mixer"}, |
| 573 | {"Out3 Mux", "VREF", "VREF"}, |
| 574 | {"Out3 Mux", "Left + Right", "Out3 Left + Right"}, |
| 575 | {"Out3 Mux", "ROUT2", "ROUT2"}, |
| 576 | {"Out 3", NULL, "Out3 Mux"}, |
| 577 | {"OUT3", NULL, "Out 3"}, |
| 578 | |
| 579 | /* out 4 */ |
| 580 | {"Out4 Mux", "VREF", "VREF"}, |
| 581 | {"Out4 Mux", "Capture ST", "Capture ST Mixer"}, |
| 582 | {"Out4 Mux", "LOUT2", "LOUT2"}, |
| 583 | {"Out 4", NULL, "Out4 Mux"}, |
| 584 | {"OUT4", NULL, "Out 4"}, |
| 585 | |
| 586 | /* record mixer */ |
| 587 | {"Playback Mixer", "Left Capture Switch", "Left Mixer"}, |
| 588 | {"Playback Mixer", "Voice Capture Switch", "Mono Mixer"}, |
| 589 | {"Playback Mixer", "Right Capture Switch", "Right Mixer"}, |
| 590 | |
| 591 | /* Mic/SideTone Mux */ |
| 592 | {"Mic Sidetone Mux", "Left PGA", "Left Capture Volume"}, |
| 593 | {"Mic Sidetone Mux", "Right PGA", "Right Capture Volume"}, |
| 594 | {"Mic Sidetone Mux", "Mic 1", "Mic 1 Volume"}, |
| 595 | {"Mic Sidetone Mux", "Mic 2", "Mic 2 Volume"}, |
| 596 | |
| 597 | /* Capture Left Mux */ |
| 598 | {"Capture Left Mux", "PGA", "Left Capture Volume"}, |
| 599 | {"Capture Left Mux", "Line or RXP-RXN", "Line Left Mux"}, |
| 600 | {"Capture Left Mux", "Line", "LINE1"}, |
| 601 | |
| 602 | /* Capture Right Mux */ |
| 603 | {"Capture Right Mux", "PGA", "Right Capture Volume"}, |
| 604 | {"Capture Right Mux", "Line or RXP-RXN", "Line Right Mux"}, |
| 605 | {"Capture Right Mux", "Sidetone", "Capture ST Mixer"}, |
| 606 | |
| 607 | /* Mono Capture mixer-mux */ |
| 608 | {"Capture Right Mixer", "Stereo", "Capture Right Mux"}, |
| 609 | {"Capture Left Mixer", "Analogue Mix Left", "Capture Left Mux"}, |
| 610 | {"Capture Left Mixer", "Analogue Mix Left", "Capture Right Mux"}, |
| 611 | {"Capture Right Mixer", "Analogue Mix Right", "Capture Left Mux"}, |
| 612 | {"Capture Right Mixer", "Analogue Mix Right", "Capture Right Mux"}, |
| 613 | {"Capture Left Mixer", "Digital Mono Mix", "Capture Left Mux"}, |
| 614 | {"Capture Left Mixer", "Digital Mono Mix", "Capture Right Mux"}, |
| 615 | {"Capture Right Mixer", "Digital Mono Mix", "Capture Left Mux"}, |
| 616 | {"Capture Right Mixer", "Digital Mono Mix", "Capture Right Mux"}, |
| 617 | |
| 618 | /* ADC */ |
| 619 | {"Left ADC", NULL, "Capture Left Mixer"}, |
| 620 | {"Right ADC", NULL, "Capture Right Mixer"}, |
| 621 | |
| 622 | /* Left Capture Volume */ |
| 623 | {"Left Capture Volume", NULL, "ACIN"}, |
| 624 | |
| 625 | /* Right Capture Volume */ |
| 626 | {"Right Capture Volume", NULL, "Mic 2 Volume"}, |
| 627 | |
| 628 | /* ALC Mixer */ |
| 629 | {"ALC Mixer", "Line Capture Switch", "Line Mixer"}, |
| 630 | {"ALC Mixer", "Mic2 Capture Switch", "Mic 2 Volume"}, |
| 631 | {"ALC Mixer", "Mic1 Capture Switch", "Mic 1 Volume"}, |
| 632 | {"ALC Mixer", "Rx Capture Switch", "Rx Mixer"}, |
| 633 | |
| 634 | /* Line Left Mux */ |
| 635 | {"Line Left Mux", "Line 1", "LINE1"}, |
| 636 | {"Line Left Mux", "Rx Mix", "Rx Mixer"}, |
| 637 | |
| 638 | /* Line Right Mux */ |
| 639 | {"Line Right Mux", "Line 2", "LINE2"}, |
| 640 | {"Line Right Mux", "Rx Mix", "Rx Mixer"}, |
| 641 | |
| 642 | /* Line Mono Mux */ |
| 643 | {"Line Mono Mux", "Line Mix", "Line Mixer"}, |
| 644 | {"Line Mono Mux", "Rx Mix", "Rx Mixer"}, |
| 645 | |
| 646 | /* Line Mixer/Mux */ |
| 647 | {"Line Mixer", "Line 1 + 2", "LINE1"}, |
| 648 | {"Line Mixer", "Line 1 - 2", "LINE1"}, |
| 649 | {"Line Mixer", "Line 1 + 2", "LINE2"}, |
| 650 | {"Line Mixer", "Line 1 - 2", "LINE2"}, |
| 651 | {"Line Mixer", "Line 1", "LINE1"}, |
| 652 | {"Line Mixer", "Line 2", "LINE2"}, |
| 653 | |
| 654 | /* Rx Mixer/Mux */ |
| 655 | {"Rx Mixer", "RXP - RXN", "RXP"}, |
| 656 | {"Rx Mixer", "RXP + RXN", "RXP"}, |
| 657 | {"Rx Mixer", "RXP - RXN", "RXN"}, |
| 658 | {"Rx Mixer", "RXP + RXN", "RXN"}, |
| 659 | {"Rx Mixer", "RXP", "RXP"}, |
| 660 | {"Rx Mixer", "RXN", "RXN"}, |
| 661 | |
| 662 | /* Mic 1 Volume */ |
| 663 | {"Mic 1 Volume", NULL, "MIC1N"}, |
| 664 | {"Mic 1 Volume", NULL, "Mic Selection Mux"}, |
| 665 | |
| 666 | /* Mic 2 Volume */ |
| 667 | {"Mic 2 Volume", NULL, "MIC2N"}, |
| 668 | {"Mic 2 Volume", NULL, "MIC2"}, |
| 669 | |
| 670 | /* Mic Selector Mux */ |
| 671 | {"Mic Selection Mux", "Mic 1", "MIC1"}, |
| 672 | {"Mic Selection Mux", "Mic 2", "MIC2N"}, |
| 673 | {"Mic Selection Mux", "Mic 3", "MIC2"}, |
| 674 | |
| 675 | /* ACOP */ |
| 676 | {"ACOP", NULL, "ALC Mixer"}, |
| 677 | |
| 678 | /* terminator */ |
| 679 | {NULL, NULL, NULL}, |
| 680 | }; |
| 681 | |
| 682 | static int wm8753_add_widgets(struct snd_soc_codec *codec) |
| 683 | { |
| 684 | int i; |
| 685 | |
| 686 | for (i = 0; i < ARRAY_SIZE(wm8753_dapm_widgets); i++) |
| 687 | snd_soc_dapm_new_control(codec, &wm8753_dapm_widgets[i]); |
| 688 | |
| 689 | /* set up the WM8753 audio map */ |
| 690 | for (i = 0; audio_map[i][0] != NULL; i++) { |
| 691 | snd_soc_dapm_connect_input(codec, audio_map[i][0], |
| 692 | audio_map[i][1], audio_map[i][2]); |
| 693 | } |
| 694 | |
| 695 | snd_soc_dapm_new_widgets(codec); |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | /* PLL divisors */ |
| 700 | struct _pll_div { |
| 701 | u32 div2:1; |
| 702 | u32 n:4; |
| 703 | u32 k:24; |
| 704 | }; |
| 705 | |
| 706 | /* The size in bits of the pll divide multiplied by 10 |
| 707 | * to allow rounding later */ |
| 708 | #define FIXED_PLL_SIZE ((1 << 22) * 10) |
| 709 | |
| 710 | static void pll_factors(struct _pll_div *pll_div, unsigned int target, |
| 711 | unsigned int source) |
| 712 | { |
| 713 | u64 Kpart; |
| 714 | unsigned int K, Ndiv, Nmod; |
| 715 | |
| 716 | Ndiv = target / source; |
| 717 | if (Ndiv < 6) { |
| 718 | source >>= 1; |
| 719 | pll_div->div2 = 1; |
| 720 | Ndiv = target / source; |
| 721 | } else |
| 722 | pll_div->div2 = 0; |
| 723 | |
| 724 | if ((Ndiv < 6) || (Ndiv > 12)) |
| 725 | printk(KERN_WARNING |
Mark Brown | 60fc684 | 2008-04-30 17:18:43 +0200 | [diff] [blame^] | 726 | "wm8753: unsupported N = %d\n", Ndiv); |
Liam Girdwood | 1f53aee | 2007-04-16 19:17:44 +0200 | [diff] [blame] | 727 | |
| 728 | pll_div->n = Ndiv; |
| 729 | Nmod = target % source; |
| 730 | Kpart = FIXED_PLL_SIZE * (long long)Nmod; |
| 731 | |
| 732 | do_div(Kpart, source); |
| 733 | |
| 734 | K = Kpart & 0xFFFFFFFF; |
| 735 | |
| 736 | /* Check if we need to round */ |
| 737 | if ((K % 10) >= 5) |
| 738 | K += 5; |
| 739 | |
| 740 | /* Move down to proper range now rounding is done */ |
| 741 | K /= 10; |
| 742 | |
| 743 | pll_div->k = K; |
| 744 | } |
| 745 | |
| 746 | static int wm8753_set_dai_pll(struct snd_soc_codec_dai *codec_dai, |
| 747 | int pll_id, unsigned int freq_in, unsigned int freq_out) |
| 748 | { |
| 749 | u16 reg, enable; |
| 750 | int offset; |
| 751 | struct snd_soc_codec *codec = codec_dai->codec; |
| 752 | |
| 753 | if (pll_id < WM8753_PLL1 || pll_id > WM8753_PLL2) |
| 754 | return -ENODEV; |
| 755 | |
| 756 | if (pll_id == WM8753_PLL1) { |
| 757 | offset = 0; |
| 758 | enable = 0x10; |
| 759 | reg = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xffef; |
| 760 | } else { |
| 761 | offset = 4; |
| 762 | enable = 0x8; |
| 763 | reg = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xfff7; |
| 764 | } |
| 765 | |
| 766 | if (!freq_in || !freq_out) { |
| 767 | /* disable PLL */ |
| 768 | wm8753_write(codec, WM8753_PLL1CTL1 + offset, 0x0026); |
| 769 | wm8753_write(codec, WM8753_CLOCK, reg); |
| 770 | return 0; |
| 771 | } else { |
| 772 | u16 value = 0; |
| 773 | struct _pll_div pll_div; |
| 774 | |
| 775 | pll_factors(&pll_div, freq_out * 8, freq_in); |
| 776 | |
| 777 | /* set up N and K PLL divisor ratios */ |
| 778 | /* bits 8:5 = PLL_N, bits 3:0 = PLL_K[21:18] */ |
| 779 | value = (pll_div.n << 5) + ((pll_div.k & 0x3c0000) >> 18); |
| 780 | wm8753_write(codec, WM8753_PLL1CTL2 + offset, value); |
| 781 | |
| 782 | /* bits 8:0 = PLL_K[17:9] */ |
| 783 | value = (pll_div.k & 0x03fe00) >> 9; |
| 784 | wm8753_write(codec, WM8753_PLL1CTL3 + offset, value); |
| 785 | |
| 786 | /* bits 8:0 = PLL_K[8:0] */ |
| 787 | value = pll_div.k & 0x0001ff; |
| 788 | wm8753_write(codec, WM8753_PLL1CTL4 + offset, value); |
| 789 | |
| 790 | /* set PLL as input and enable */ |
| 791 | wm8753_write(codec, WM8753_PLL1CTL1 + offset, 0x0027 | |
| 792 | (pll_div.div2 << 3)); |
| 793 | wm8753_write(codec, WM8753_CLOCK, reg | enable); |
| 794 | } |
| 795 | return 0; |
| 796 | } |
| 797 | |
| 798 | struct _coeff_div { |
| 799 | u32 mclk; |
| 800 | u32 rate; |
| 801 | u8 sr:5; |
| 802 | u8 usb:1; |
| 803 | }; |
| 804 | |
| 805 | /* codec hifi mclk (after PLL) clock divider coefficients */ |
| 806 | static const struct _coeff_div coeff_div[] = { |
| 807 | /* 8k */ |
| 808 | {12288000, 8000, 0x6, 0x0}, |
| 809 | {11289600, 8000, 0x16, 0x0}, |
| 810 | {18432000, 8000, 0x7, 0x0}, |
| 811 | {16934400, 8000, 0x17, 0x0}, |
| 812 | {12000000, 8000, 0x6, 0x1}, |
| 813 | |
| 814 | /* 11.025k */ |
| 815 | {11289600, 11025, 0x18, 0x0}, |
| 816 | {16934400, 11025, 0x19, 0x0}, |
| 817 | {12000000, 11025, 0x19, 0x1}, |
| 818 | |
| 819 | /* 16k */ |
| 820 | {12288000, 16000, 0xa, 0x0}, |
| 821 | {18432000, 16000, 0xb, 0x0}, |
| 822 | {12000000, 16000, 0xa, 0x1}, |
| 823 | |
| 824 | /* 22.05k */ |
| 825 | {11289600, 22050, 0x1a, 0x0}, |
| 826 | {16934400, 22050, 0x1b, 0x0}, |
| 827 | {12000000, 22050, 0x1b, 0x1}, |
| 828 | |
| 829 | /* 32k */ |
| 830 | {12288000, 32000, 0xc, 0x0}, |
| 831 | {18432000, 32000, 0xd, 0x0}, |
| 832 | {12000000, 32000, 0xa, 0x1}, |
| 833 | |
| 834 | /* 44.1k */ |
| 835 | {11289600, 44100, 0x10, 0x0}, |
| 836 | {16934400, 44100, 0x11, 0x0}, |
| 837 | {12000000, 44100, 0x11, 0x1}, |
| 838 | |
| 839 | /* 48k */ |
| 840 | {12288000, 48000, 0x0, 0x0}, |
| 841 | {18432000, 48000, 0x1, 0x0}, |
| 842 | {12000000, 48000, 0x0, 0x1}, |
| 843 | |
| 844 | /* 88.2k */ |
| 845 | {11289600, 88200, 0x1e, 0x0}, |
| 846 | {16934400, 88200, 0x1f, 0x0}, |
| 847 | {12000000, 88200, 0x1f, 0x1}, |
| 848 | |
| 849 | /* 96k */ |
| 850 | {12288000, 96000, 0xe, 0x0}, |
| 851 | {18432000, 96000, 0xf, 0x0}, |
| 852 | {12000000, 96000, 0xe, 0x1}, |
| 853 | }; |
| 854 | |
| 855 | static int get_coeff(int mclk, int rate) |
| 856 | { |
| 857 | int i; |
| 858 | |
| 859 | for (i = 0; i < ARRAY_SIZE(coeff_div); i++) { |
| 860 | if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk) |
| 861 | return i; |
| 862 | } |
| 863 | return -EINVAL; |
| 864 | } |
| 865 | |
| 866 | /* |
| 867 | * Clock after PLL and dividers |
| 868 | */ |
| 869 | static int wm8753_set_dai_sysclk(struct snd_soc_codec_dai *codec_dai, |
| 870 | int clk_id, unsigned int freq, int dir) |
| 871 | { |
| 872 | struct snd_soc_codec *codec = codec_dai->codec; |
| 873 | struct wm8753_priv *wm8753 = codec->private_data; |
| 874 | |
| 875 | switch (freq) { |
| 876 | case 11289600: |
| 877 | case 12000000: |
| 878 | case 12288000: |
| 879 | case 16934400: |
| 880 | case 18432000: |
| 881 | if (clk_id == WM8753_MCLK) { |
| 882 | wm8753->sysclk = freq; |
| 883 | return 0; |
| 884 | } else if (clk_id == WM8753_PCMCLK) { |
| 885 | wm8753->pcmclk = freq; |
| 886 | return 0; |
| 887 | } |
| 888 | break; |
| 889 | } |
| 890 | return -EINVAL; |
| 891 | } |
| 892 | |
| 893 | /* |
| 894 | * Set's ADC and Voice DAC format. |
| 895 | */ |
| 896 | static int wm8753_vdac_adc_set_dai_fmt(struct snd_soc_codec_dai *codec_dai, |
| 897 | unsigned int fmt) |
| 898 | { |
| 899 | struct snd_soc_codec *codec = codec_dai->codec; |
| 900 | u16 voice = wm8753_read_reg_cache(codec, WM8753_PCM) & 0x01ec; |
| 901 | |
| 902 | /* interface format */ |
| 903 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 904 | case SND_SOC_DAIFMT_I2S: |
| 905 | voice |= 0x0002; |
| 906 | break; |
| 907 | case SND_SOC_DAIFMT_RIGHT_J: |
| 908 | break; |
| 909 | case SND_SOC_DAIFMT_LEFT_J: |
| 910 | voice |= 0x0001; |
| 911 | break; |
| 912 | case SND_SOC_DAIFMT_DSP_A: |
| 913 | voice |= 0x0003; |
| 914 | break; |
| 915 | case SND_SOC_DAIFMT_DSP_B: |
| 916 | voice |= 0x0013; |
| 917 | break; |
| 918 | default: |
| 919 | return -EINVAL; |
| 920 | } |
| 921 | |
| 922 | wm8753_write(codec, WM8753_PCM, voice); |
| 923 | return 0; |
| 924 | } |
| 925 | |
| 926 | /* |
| 927 | * Set PCM DAI bit size and sample rate. |
| 928 | */ |
| 929 | static int wm8753_pcm_hw_params(struct snd_pcm_substream *substream, |
| 930 | struct snd_pcm_hw_params *params) |
| 931 | { |
| 932 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 933 | struct snd_soc_device *socdev = rtd->socdev; |
| 934 | struct snd_soc_codec *codec = socdev->codec; |
| 935 | struct wm8753_priv *wm8753 = codec->private_data; |
| 936 | u16 voice = wm8753_read_reg_cache(codec, WM8753_PCM) & 0x01f3; |
| 937 | u16 srate = wm8753_read_reg_cache(codec, WM8753_SRATE1) & 0x017f; |
| 938 | |
| 939 | /* bit size */ |
| 940 | switch (params_format(params)) { |
| 941 | case SNDRV_PCM_FORMAT_S16_LE: |
| 942 | break; |
| 943 | case SNDRV_PCM_FORMAT_S20_3LE: |
| 944 | voice |= 0x0004; |
| 945 | break; |
| 946 | case SNDRV_PCM_FORMAT_S24_LE: |
| 947 | voice |= 0x0008; |
| 948 | break; |
| 949 | case SNDRV_PCM_FORMAT_S32_LE: |
| 950 | voice |= 0x000c; |
| 951 | break; |
| 952 | } |
| 953 | |
| 954 | /* sample rate */ |
| 955 | if (params_rate(params) * 384 == wm8753->pcmclk) |
| 956 | srate |= 0x80; |
| 957 | wm8753_write(codec, WM8753_SRATE1, srate); |
| 958 | |
| 959 | wm8753_write(codec, WM8753_PCM, voice); |
| 960 | return 0; |
| 961 | } |
| 962 | |
| 963 | /* |
| 964 | * Set's PCM dai fmt and BCLK. |
| 965 | */ |
| 966 | static int wm8753_pcm_set_dai_fmt(struct snd_soc_codec_dai *codec_dai, |
| 967 | unsigned int fmt) |
| 968 | { |
| 969 | struct snd_soc_codec *codec = codec_dai->codec; |
| 970 | u16 voice, ioctl; |
| 971 | |
| 972 | voice = wm8753_read_reg_cache(codec, WM8753_PCM) & 0x011f; |
| 973 | ioctl = wm8753_read_reg_cache(codec, WM8753_IOCTL) & 0x015d; |
| 974 | |
| 975 | /* set master/slave audio interface */ |
| 976 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 977 | case SND_SOC_DAIFMT_CBS_CFS: |
| 978 | break; |
| 979 | case SND_SOC_DAIFMT_CBM_CFM: |
| 980 | ioctl |= 0x2; |
| 981 | case SND_SOC_DAIFMT_CBM_CFS: |
| 982 | voice |= 0x0040; |
| 983 | break; |
| 984 | default: |
| 985 | return -EINVAL; |
| 986 | } |
| 987 | |
| 988 | /* clock inversion */ |
| 989 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 990 | case SND_SOC_DAIFMT_DSP_A: |
| 991 | case SND_SOC_DAIFMT_DSP_B: |
| 992 | /* frame inversion not valid for DSP modes */ |
| 993 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 994 | case SND_SOC_DAIFMT_NB_NF: |
| 995 | break; |
| 996 | case SND_SOC_DAIFMT_IB_NF: |
| 997 | voice |= 0x0080; |
| 998 | break; |
| 999 | default: |
| 1000 | return -EINVAL; |
| 1001 | } |
| 1002 | break; |
| 1003 | case SND_SOC_DAIFMT_I2S: |
| 1004 | case SND_SOC_DAIFMT_RIGHT_J: |
| 1005 | case SND_SOC_DAIFMT_LEFT_J: |
| 1006 | voice &= ~0x0010; |
| 1007 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 1008 | case SND_SOC_DAIFMT_NB_NF: |
| 1009 | break; |
| 1010 | case SND_SOC_DAIFMT_IB_IF: |
| 1011 | voice |= 0x0090; |
| 1012 | break; |
| 1013 | case SND_SOC_DAIFMT_IB_NF: |
| 1014 | voice |= 0x0080; |
| 1015 | break; |
| 1016 | case SND_SOC_DAIFMT_NB_IF: |
| 1017 | voice |= 0x0010; |
| 1018 | break; |
| 1019 | default: |
| 1020 | return -EINVAL; |
| 1021 | } |
| 1022 | break; |
| 1023 | default: |
| 1024 | return -EINVAL; |
| 1025 | } |
| 1026 | |
| 1027 | wm8753_write(codec, WM8753_PCM, voice); |
| 1028 | wm8753_write(codec, WM8753_IOCTL, ioctl); |
| 1029 | return 0; |
| 1030 | } |
| 1031 | |
| 1032 | static int wm8753_set_dai_clkdiv(struct snd_soc_codec_dai *codec_dai, |
| 1033 | int div_id, int div) |
| 1034 | { |
| 1035 | struct snd_soc_codec *codec = codec_dai->codec; |
| 1036 | u16 reg; |
| 1037 | |
| 1038 | switch (div_id) { |
| 1039 | case WM8753_PCMDIV: |
| 1040 | reg = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0x003f; |
| 1041 | wm8753_write(codec, WM8753_CLOCK, reg | div); |
| 1042 | break; |
| 1043 | case WM8753_BCLKDIV: |
| 1044 | reg = wm8753_read_reg_cache(codec, WM8753_SRATE2) & 0x01c7; |
| 1045 | wm8753_write(codec, WM8753_SRATE2, reg | div); |
| 1046 | break; |
| 1047 | case WM8753_VXCLKDIV: |
| 1048 | reg = wm8753_read_reg_cache(codec, WM8753_SRATE2) & 0x003f; |
| 1049 | wm8753_write(codec, WM8753_SRATE2, reg | div); |
| 1050 | break; |
| 1051 | default: |
| 1052 | return -EINVAL; |
| 1053 | } |
| 1054 | return 0; |
| 1055 | } |
| 1056 | |
| 1057 | /* |
| 1058 | * Set's HiFi DAC format. |
| 1059 | */ |
| 1060 | static int wm8753_hdac_set_dai_fmt(struct snd_soc_codec_dai *codec_dai, |
| 1061 | unsigned int fmt) |
| 1062 | { |
| 1063 | struct snd_soc_codec *codec = codec_dai->codec; |
| 1064 | u16 hifi = wm8753_read_reg_cache(codec, WM8753_HIFI) & 0x01e0; |
| 1065 | |
| 1066 | /* interface format */ |
| 1067 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 1068 | case SND_SOC_DAIFMT_I2S: |
| 1069 | hifi |= 0x0002; |
| 1070 | break; |
| 1071 | case SND_SOC_DAIFMT_RIGHT_J: |
| 1072 | break; |
| 1073 | case SND_SOC_DAIFMT_LEFT_J: |
| 1074 | hifi |= 0x0001; |
| 1075 | break; |
| 1076 | case SND_SOC_DAIFMT_DSP_A: |
| 1077 | hifi |= 0x0003; |
| 1078 | break; |
| 1079 | case SND_SOC_DAIFMT_DSP_B: |
| 1080 | hifi |= 0x0013; |
| 1081 | break; |
| 1082 | default: |
| 1083 | return -EINVAL; |
| 1084 | } |
| 1085 | |
| 1086 | wm8753_write(codec, WM8753_HIFI, hifi); |
| 1087 | return 0; |
| 1088 | } |
| 1089 | |
| 1090 | /* |
| 1091 | * Set's I2S DAI format. |
| 1092 | */ |
| 1093 | static int wm8753_i2s_set_dai_fmt(struct snd_soc_codec_dai *codec_dai, |
| 1094 | unsigned int fmt) |
| 1095 | { |
| 1096 | struct snd_soc_codec *codec = codec_dai->codec; |
| 1097 | u16 ioctl, hifi; |
| 1098 | |
| 1099 | hifi = wm8753_read_reg_cache(codec, WM8753_HIFI) & 0x011f; |
| 1100 | ioctl = wm8753_read_reg_cache(codec, WM8753_IOCTL) & 0x00ae; |
| 1101 | |
| 1102 | /* set master/slave audio interface */ |
| 1103 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 1104 | case SND_SOC_DAIFMT_CBS_CFS: |
| 1105 | break; |
| 1106 | case SND_SOC_DAIFMT_CBM_CFM: |
| 1107 | ioctl |= 0x1; |
| 1108 | case SND_SOC_DAIFMT_CBM_CFS: |
| 1109 | hifi |= 0x0040; |
| 1110 | break; |
| 1111 | default: |
| 1112 | return -EINVAL; |
| 1113 | } |
| 1114 | |
| 1115 | /* clock inversion */ |
| 1116 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 1117 | case SND_SOC_DAIFMT_DSP_A: |
| 1118 | case SND_SOC_DAIFMT_DSP_B: |
| 1119 | /* frame inversion not valid for DSP modes */ |
| 1120 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 1121 | case SND_SOC_DAIFMT_NB_NF: |
| 1122 | break; |
| 1123 | case SND_SOC_DAIFMT_IB_NF: |
| 1124 | hifi |= 0x0080; |
| 1125 | break; |
| 1126 | default: |
| 1127 | return -EINVAL; |
| 1128 | } |
| 1129 | break; |
| 1130 | case SND_SOC_DAIFMT_I2S: |
| 1131 | case SND_SOC_DAIFMT_RIGHT_J: |
| 1132 | case SND_SOC_DAIFMT_LEFT_J: |
| 1133 | hifi &= ~0x0010; |
| 1134 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 1135 | case SND_SOC_DAIFMT_NB_NF: |
| 1136 | break; |
| 1137 | case SND_SOC_DAIFMT_IB_IF: |
| 1138 | hifi |= 0x0090; |
| 1139 | break; |
| 1140 | case SND_SOC_DAIFMT_IB_NF: |
| 1141 | hifi |= 0x0080; |
| 1142 | break; |
| 1143 | case SND_SOC_DAIFMT_NB_IF: |
| 1144 | hifi |= 0x0010; |
| 1145 | break; |
| 1146 | default: |
| 1147 | return -EINVAL; |
| 1148 | } |
| 1149 | break; |
| 1150 | default: |
| 1151 | return -EINVAL; |
| 1152 | } |
| 1153 | |
| 1154 | wm8753_write(codec, WM8753_HIFI, hifi); |
| 1155 | wm8753_write(codec, WM8753_IOCTL, ioctl); |
| 1156 | return 0; |
| 1157 | } |
| 1158 | |
| 1159 | /* |
| 1160 | * Set PCM DAI bit size and sample rate. |
| 1161 | */ |
| 1162 | static int wm8753_i2s_hw_params(struct snd_pcm_substream *substream, |
| 1163 | struct snd_pcm_hw_params *params) |
| 1164 | { |
| 1165 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 1166 | struct snd_soc_device *socdev = rtd->socdev; |
| 1167 | struct snd_soc_codec *codec = socdev->codec; |
| 1168 | struct wm8753_priv *wm8753 = codec->private_data; |
| 1169 | u16 srate = wm8753_read_reg_cache(codec, WM8753_SRATE1) & 0x01c0; |
| 1170 | u16 hifi = wm8753_read_reg_cache(codec, WM8753_HIFI) & 0x01f3; |
| 1171 | int coeff; |
| 1172 | |
| 1173 | /* is digital filter coefficient valid ? */ |
| 1174 | coeff = get_coeff(wm8753->sysclk, params_rate(params)); |
| 1175 | if (coeff < 0) { |
| 1176 | printk(KERN_ERR "wm8753 invalid MCLK or rate\n"); |
| 1177 | return coeff; |
| 1178 | } |
| 1179 | wm8753_write(codec, WM8753_SRATE1, srate | (coeff_div[coeff].sr << 1) | |
| 1180 | coeff_div[coeff].usb); |
| 1181 | |
| 1182 | /* bit size */ |
| 1183 | switch (params_format(params)) { |
| 1184 | case SNDRV_PCM_FORMAT_S16_LE: |
| 1185 | break; |
| 1186 | case SNDRV_PCM_FORMAT_S20_3LE: |
| 1187 | hifi |= 0x0004; |
| 1188 | break; |
| 1189 | case SNDRV_PCM_FORMAT_S24_LE: |
| 1190 | hifi |= 0x0008; |
| 1191 | break; |
| 1192 | case SNDRV_PCM_FORMAT_S32_LE: |
| 1193 | hifi |= 0x000c; |
| 1194 | break; |
| 1195 | } |
| 1196 | |
| 1197 | wm8753_write(codec, WM8753_HIFI, hifi); |
| 1198 | return 0; |
| 1199 | } |
| 1200 | |
| 1201 | static int wm8753_mode1v_set_dai_fmt(struct snd_soc_codec_dai *codec_dai, |
| 1202 | unsigned int fmt) |
| 1203 | { |
| 1204 | struct snd_soc_codec *codec = codec_dai->codec; |
| 1205 | u16 clock; |
| 1206 | |
| 1207 | /* set clk source as pcmclk */ |
| 1208 | clock = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xfffb; |
| 1209 | wm8753_write(codec, WM8753_CLOCK, clock); |
| 1210 | |
| 1211 | if (wm8753_vdac_adc_set_dai_fmt(codec_dai, fmt) < 0) |
| 1212 | return -EINVAL; |
| 1213 | return wm8753_pcm_set_dai_fmt(codec_dai, fmt); |
| 1214 | } |
| 1215 | |
| 1216 | static int wm8753_mode1h_set_dai_fmt(struct snd_soc_codec_dai *codec_dai, |
| 1217 | unsigned int fmt) |
| 1218 | { |
| 1219 | if (wm8753_hdac_set_dai_fmt(codec_dai, fmt) < 0) |
| 1220 | return -EINVAL; |
| 1221 | return wm8753_i2s_set_dai_fmt(codec_dai, fmt); |
| 1222 | } |
| 1223 | |
| 1224 | static int wm8753_mode2_set_dai_fmt(struct snd_soc_codec_dai *codec_dai, |
| 1225 | unsigned int fmt) |
| 1226 | { |
| 1227 | struct snd_soc_codec *codec = codec_dai->codec; |
| 1228 | u16 clock; |
| 1229 | |
| 1230 | /* set clk source as pcmclk */ |
| 1231 | clock = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xfffb; |
| 1232 | wm8753_write(codec, WM8753_CLOCK, clock); |
| 1233 | |
| 1234 | if (wm8753_vdac_adc_set_dai_fmt(codec_dai, fmt) < 0) |
| 1235 | return -EINVAL; |
| 1236 | return wm8753_i2s_set_dai_fmt(codec_dai, fmt); |
| 1237 | } |
| 1238 | |
| 1239 | static int wm8753_mode3_4_set_dai_fmt(struct snd_soc_codec_dai *codec_dai, |
| 1240 | unsigned int fmt) |
| 1241 | { |
| 1242 | struct snd_soc_codec *codec = codec_dai->codec; |
| 1243 | u16 clock; |
| 1244 | |
| 1245 | /* set clk source as mclk */ |
| 1246 | clock = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xfffb; |
| 1247 | wm8753_write(codec, WM8753_CLOCK, clock | 0x4); |
| 1248 | |
| 1249 | if (wm8753_hdac_set_dai_fmt(codec_dai, fmt) < 0) |
| 1250 | return -EINVAL; |
| 1251 | if (wm8753_vdac_adc_set_dai_fmt(codec_dai, fmt) < 0) |
| 1252 | return -EINVAL; |
| 1253 | return wm8753_i2s_set_dai_fmt(codec_dai, fmt); |
| 1254 | } |
| 1255 | |
| 1256 | static int wm8753_mute(struct snd_soc_codec_dai *dai, int mute) |
| 1257 | { |
| 1258 | struct snd_soc_codec *codec = dai->codec; |
| 1259 | u16 mute_reg = wm8753_read_reg_cache(codec, WM8753_DAC) & 0xfff7; |
| 1260 | |
| 1261 | /* the digital mute covers the HiFi and Voice DAC's on the WM8753. |
| 1262 | * make sure we check if they are not both active when we mute */ |
| 1263 | if (mute && dai->id == 1) { |
| 1264 | if (!wm8753_dai[WM8753_DAI_VOICE].playback.active || |
| 1265 | !wm8753_dai[WM8753_DAI_HIFI].playback.active) |
| 1266 | wm8753_write(codec, WM8753_DAC, mute_reg | 0x8); |
| 1267 | } else { |
| 1268 | if (mute) |
| 1269 | wm8753_write(codec, WM8753_DAC, mute_reg | 0x8); |
| 1270 | else |
| 1271 | wm8753_write(codec, WM8753_DAC, mute_reg); |
| 1272 | } |
| 1273 | |
| 1274 | return 0; |
| 1275 | } |
| 1276 | |
| 1277 | static int wm8753_dapm_event(struct snd_soc_codec *codec, int event) |
| 1278 | { |
| 1279 | u16 pwr_reg = wm8753_read_reg_cache(codec, WM8753_PWR1) & 0xfe3e; |
| 1280 | |
| 1281 | switch (event) { |
| 1282 | case SNDRV_CTL_POWER_D0: /* full On */ |
| 1283 | /* set vmid to 50k and unmute dac */ |
| 1284 | wm8753_write(codec, WM8753_PWR1, pwr_reg | 0x00c0); |
| 1285 | break; |
| 1286 | case SNDRV_CTL_POWER_D1: /* partial On */ |
| 1287 | case SNDRV_CTL_POWER_D2: /* partial On */ |
| 1288 | /* set vmid to 5k for quick power up */ |
| 1289 | wm8753_write(codec, WM8753_PWR1, pwr_reg | 0x01c1); |
| 1290 | break; |
| 1291 | case SNDRV_CTL_POWER_D3hot: /* Off, with power */ |
| 1292 | /* mute dac and set vmid to 500k, enable VREF */ |
| 1293 | wm8753_write(codec, WM8753_PWR1, pwr_reg | 0x0141); |
| 1294 | break; |
| 1295 | case SNDRV_CTL_POWER_D3cold: /* Off, without power */ |
| 1296 | wm8753_write(codec, WM8753_PWR1, 0x0001); |
| 1297 | break; |
| 1298 | } |
| 1299 | codec->dapm_state = event; |
| 1300 | return 0; |
| 1301 | } |
| 1302 | |
| 1303 | #define WM8753_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\ |
Mark Brown | 60fc684 | 2008-04-30 17:18:43 +0200 | [diff] [blame^] | 1304 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\ |
| 1305 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\ |
| 1306 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000) |
Liam Girdwood | 1f53aee | 2007-04-16 19:17:44 +0200 | [diff] [blame] | 1307 | |
| 1308 | #define WM8753_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ |
| 1309 | SNDRV_PCM_FMTBIT_S24_LE) |
| 1310 | |
| 1311 | /* |
| 1312 | * The WM8753 supports upto 4 different and mutually exclusive DAI |
| 1313 | * configurations. This gives 2 PCM's available for use, hifi and voice. |
| 1314 | * NOTE: The Voice PCM cannot play or capture audio to the CPU as it's DAI |
| 1315 | * is connected between the wm8753 and a BT codec or GSM modem. |
| 1316 | * |
| 1317 | * 1. Voice over PCM DAI - HIFI DAC over HIFI DAI |
| 1318 | * 2. Voice over HIFI DAI - HIFI disabled |
| 1319 | * 3. Voice disabled - HIFI over HIFI |
| 1320 | * 4. Voice disabled - HIFI over HIFI, uses voice DAI LRC for capture |
| 1321 | */ |
| 1322 | static const struct snd_soc_codec_dai wm8753_all_dai[] = { |
| 1323 | /* DAI HiFi mode 1 */ |
| 1324 | { .name = "WM8753 HiFi", |
| 1325 | .id = 1, |
| 1326 | .playback = { |
| 1327 | .stream_name = "HiFi Playback", |
| 1328 | .channels_min = 1, |
| 1329 | .channels_max = 2, |
| 1330 | .rates = WM8753_RATES, |
| 1331 | .formats = WM8753_FORMATS,}, |
| 1332 | .capture = { /* dummy for fast DAI switching */ |
| 1333 | .stream_name = "Capture", |
| 1334 | .channels_min = 1, |
| 1335 | .channels_max = 2, |
| 1336 | .rates = WM8753_RATES, |
| 1337 | .formats = WM8753_FORMATS,}, |
| 1338 | .ops = { |
| 1339 | .hw_params = wm8753_i2s_hw_params,}, |
| 1340 | .dai_ops = { |
| 1341 | .digital_mute = wm8753_mute, |
| 1342 | .set_fmt = wm8753_mode1h_set_dai_fmt, |
| 1343 | .set_clkdiv = wm8753_set_dai_clkdiv, |
| 1344 | .set_pll = wm8753_set_dai_pll, |
| 1345 | .set_sysclk = wm8753_set_dai_sysclk, |
| 1346 | }, |
| 1347 | }, |
| 1348 | /* DAI Voice mode 1 */ |
| 1349 | { .name = "WM8753 Voice", |
| 1350 | .id = 1, |
| 1351 | .playback = { |
| 1352 | .stream_name = "Voice Playback", |
| 1353 | .channels_min = 1, |
| 1354 | .channels_max = 1, |
| 1355 | .rates = WM8753_RATES, |
| 1356 | .formats = WM8753_FORMATS,}, |
| 1357 | .capture = { |
| 1358 | .stream_name = "Capture", |
| 1359 | .channels_min = 1, |
| 1360 | .channels_max = 2, |
| 1361 | .rates = WM8753_RATES, |
| 1362 | .formats = WM8753_FORMATS,}, |
| 1363 | .ops = { |
| 1364 | .hw_params = wm8753_pcm_hw_params,}, |
| 1365 | .dai_ops = { |
| 1366 | .digital_mute = wm8753_mute, |
| 1367 | .set_fmt = wm8753_mode1v_set_dai_fmt, |
| 1368 | .set_clkdiv = wm8753_set_dai_clkdiv, |
| 1369 | .set_pll = wm8753_set_dai_pll, |
| 1370 | .set_sysclk = wm8753_set_dai_sysclk, |
| 1371 | }, |
| 1372 | }, |
| 1373 | /* DAI HiFi mode 2 - dummy */ |
| 1374 | { .name = "WM8753 HiFi", |
| 1375 | .id = 2, |
| 1376 | }, |
| 1377 | /* DAI Voice mode 2 */ |
| 1378 | { .name = "WM8753 Voice", |
| 1379 | .id = 2, |
| 1380 | .playback = { |
| 1381 | .stream_name = "Voice Playback", |
| 1382 | .channels_min = 1, |
| 1383 | .channels_max = 1, |
| 1384 | .rates = WM8753_RATES, |
| 1385 | .formats = WM8753_FORMATS,}, |
| 1386 | .capture = { |
| 1387 | .stream_name = "Capture", |
| 1388 | .channels_min = 1, |
| 1389 | .channels_max = 2, |
| 1390 | .rates = WM8753_RATES, |
| 1391 | .formats = WM8753_FORMATS,}, |
| 1392 | .ops = { |
| 1393 | .hw_params = wm8753_pcm_hw_params,}, |
| 1394 | .dai_ops = { |
| 1395 | .digital_mute = wm8753_mute, |
| 1396 | .set_fmt = wm8753_mode2_set_dai_fmt, |
| 1397 | .set_clkdiv = wm8753_set_dai_clkdiv, |
| 1398 | .set_pll = wm8753_set_dai_pll, |
| 1399 | .set_sysclk = wm8753_set_dai_sysclk, |
| 1400 | }, |
| 1401 | }, |
| 1402 | /* DAI HiFi mode 3 */ |
| 1403 | { .name = "WM8753 HiFi", |
| 1404 | .id = 3, |
| 1405 | .playback = { |
| 1406 | .stream_name = "HiFi Playback", |
| 1407 | .channels_min = 1, |
| 1408 | .channels_max = 2, |
| 1409 | .rates = WM8753_RATES, |
| 1410 | .formats = WM8753_FORMATS,}, |
| 1411 | .capture = { |
| 1412 | .stream_name = "Capture", |
| 1413 | .channels_min = 1, |
| 1414 | .channels_max = 2, |
| 1415 | .rates = WM8753_RATES, |
| 1416 | .formats = WM8753_FORMATS,}, |
| 1417 | .ops = { |
| 1418 | .hw_params = wm8753_i2s_hw_params,}, |
| 1419 | .dai_ops = { |
| 1420 | .digital_mute = wm8753_mute, |
| 1421 | .set_fmt = wm8753_mode3_4_set_dai_fmt, |
| 1422 | .set_clkdiv = wm8753_set_dai_clkdiv, |
| 1423 | .set_pll = wm8753_set_dai_pll, |
| 1424 | .set_sysclk = wm8753_set_dai_sysclk, |
| 1425 | }, |
| 1426 | }, |
| 1427 | /* DAI Voice mode 3 - dummy */ |
| 1428 | { .name = "WM8753 Voice", |
| 1429 | .id = 3, |
| 1430 | }, |
| 1431 | /* DAI HiFi mode 4 */ |
| 1432 | { .name = "WM8753 HiFi", |
| 1433 | .id = 4, |
| 1434 | .playback = { |
| 1435 | .stream_name = "HiFi Playback", |
| 1436 | .channels_min = 1, |
| 1437 | .channels_max = 2, |
| 1438 | .rates = WM8753_RATES, |
| 1439 | .formats = WM8753_FORMATS,}, |
| 1440 | .capture = { |
| 1441 | .stream_name = "Capture", |
| 1442 | .channels_min = 1, |
| 1443 | .channels_max = 2, |
| 1444 | .rates = WM8753_RATES, |
| 1445 | .formats = WM8753_FORMATS,}, |
| 1446 | .ops = { |
| 1447 | .hw_params = wm8753_i2s_hw_params,}, |
| 1448 | .dai_ops = { |
| 1449 | .digital_mute = wm8753_mute, |
| 1450 | .set_fmt = wm8753_mode3_4_set_dai_fmt, |
| 1451 | .set_clkdiv = wm8753_set_dai_clkdiv, |
| 1452 | .set_pll = wm8753_set_dai_pll, |
| 1453 | .set_sysclk = wm8753_set_dai_sysclk, |
| 1454 | }, |
| 1455 | }, |
| 1456 | /* DAI Voice mode 4 - dummy */ |
| 1457 | { .name = "WM8753 Voice", |
| 1458 | .id = 4, |
| 1459 | }, |
| 1460 | }; |
| 1461 | |
| 1462 | struct snd_soc_codec_dai wm8753_dai[2]; |
| 1463 | EXPORT_SYMBOL_GPL(wm8753_dai); |
| 1464 | |
| 1465 | static void wm8753_set_dai_mode(struct snd_soc_codec *codec, unsigned int mode) |
| 1466 | { |
| 1467 | if (mode < 4) { |
| 1468 | int playback_active, capture_active, codec_active, pop_wait; |
| 1469 | void *private_data; |
| 1470 | |
| 1471 | playback_active = wm8753_dai[0].playback.active; |
| 1472 | capture_active = wm8753_dai[0].capture.active; |
| 1473 | codec_active = wm8753_dai[0].active; |
| 1474 | private_data = wm8753_dai[0].private_data; |
| 1475 | pop_wait = wm8753_dai[0].pop_wait; |
| 1476 | wm8753_dai[0] = wm8753_all_dai[mode << 1]; |
| 1477 | wm8753_dai[0].playback.active = playback_active; |
| 1478 | wm8753_dai[0].capture.active = capture_active; |
| 1479 | wm8753_dai[0].active = codec_active; |
| 1480 | wm8753_dai[0].private_data = private_data; |
| 1481 | wm8753_dai[0].pop_wait = pop_wait; |
| 1482 | |
| 1483 | playback_active = wm8753_dai[1].playback.active; |
| 1484 | capture_active = wm8753_dai[1].capture.active; |
| 1485 | codec_active = wm8753_dai[1].active; |
| 1486 | private_data = wm8753_dai[1].private_data; |
| 1487 | pop_wait = wm8753_dai[1].pop_wait; |
| 1488 | wm8753_dai[1] = wm8753_all_dai[(mode << 1) + 1]; |
| 1489 | wm8753_dai[1].playback.active = playback_active; |
| 1490 | wm8753_dai[1].capture.active = capture_active; |
| 1491 | wm8753_dai[1].active = codec_active; |
| 1492 | wm8753_dai[1].private_data = private_data; |
| 1493 | wm8753_dai[1].pop_wait = pop_wait; |
| 1494 | } |
| 1495 | wm8753_dai[0].codec = codec; |
| 1496 | wm8753_dai[1].codec = codec; |
| 1497 | } |
| 1498 | |
| 1499 | static void wm8753_work(struct work_struct *work) |
| 1500 | { |
| 1501 | struct snd_soc_codec *codec = |
| 1502 | container_of(work, struct snd_soc_codec, delayed_work.work); |
| 1503 | wm8753_dapm_event(codec, codec->dapm_state); |
| 1504 | } |
| 1505 | |
| 1506 | static int wm8753_suspend(struct platform_device *pdev, pm_message_t state) |
| 1507 | { |
| 1508 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 1509 | struct snd_soc_codec *codec = socdev->codec; |
| 1510 | |
| 1511 | /* we only need to suspend if we are a valid card */ |
Mark Brown | 60fc684 | 2008-04-30 17:18:43 +0200 | [diff] [blame^] | 1512 | if (!codec->card) |
Liam Girdwood | 1f53aee | 2007-04-16 19:17:44 +0200 | [diff] [blame] | 1513 | return 0; |
Mark Brown | 60fc684 | 2008-04-30 17:18:43 +0200 | [diff] [blame^] | 1514 | |
Liam Girdwood | 1f53aee | 2007-04-16 19:17:44 +0200 | [diff] [blame] | 1515 | wm8753_dapm_event(codec, SNDRV_CTL_POWER_D3cold); |
| 1516 | return 0; |
| 1517 | } |
| 1518 | |
| 1519 | static int wm8753_resume(struct platform_device *pdev) |
| 1520 | { |
| 1521 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 1522 | struct snd_soc_codec *codec = socdev->codec; |
| 1523 | int i; |
| 1524 | u8 data[2]; |
| 1525 | u16 *cache = codec->reg_cache; |
| 1526 | |
| 1527 | /* we only need to resume if we are a valid card */ |
Mark Brown | 60fc684 | 2008-04-30 17:18:43 +0200 | [diff] [blame^] | 1528 | if (!codec->card) |
Liam Girdwood | 1f53aee | 2007-04-16 19:17:44 +0200 | [diff] [blame] | 1529 | return 0; |
| 1530 | |
| 1531 | /* Sync reg_cache with the hardware */ |
| 1532 | for (i = 0; i < ARRAY_SIZE(wm8753_reg); i++) { |
| 1533 | if (i + 1 == WM8753_RESET) |
| 1534 | continue; |
| 1535 | data[0] = ((i + 1) << 1) | ((cache[i] >> 8) & 0x0001); |
| 1536 | data[1] = cache[i] & 0x00ff; |
| 1537 | codec->hw_write(codec->control_data, data, 2); |
| 1538 | } |
| 1539 | |
| 1540 | wm8753_dapm_event(codec, SNDRV_CTL_POWER_D3hot); |
| 1541 | |
| 1542 | /* charge wm8753 caps */ |
| 1543 | if (codec->suspend_dapm_state == SNDRV_CTL_POWER_D0) { |
| 1544 | wm8753_dapm_event(codec, SNDRV_CTL_POWER_D2); |
| 1545 | codec->dapm_state = SNDRV_CTL_POWER_D0; |
| 1546 | schedule_delayed_work(&codec->delayed_work, |
| 1547 | msecs_to_jiffies(caps_charge)); |
| 1548 | } |
| 1549 | |
| 1550 | return 0; |
| 1551 | } |
| 1552 | |
| 1553 | /* |
| 1554 | * initialise the WM8753 driver |
| 1555 | * register the mixer and dsp interfaces with the kernel |
| 1556 | */ |
| 1557 | static int wm8753_init(struct snd_soc_device *socdev) |
| 1558 | { |
| 1559 | struct snd_soc_codec *codec = socdev->codec; |
| 1560 | int reg, ret = 0; |
| 1561 | |
| 1562 | codec->name = "WM8753"; |
| 1563 | codec->owner = THIS_MODULE; |
| 1564 | codec->read = wm8753_read_reg_cache; |
| 1565 | codec->write = wm8753_write; |
| 1566 | codec->dapm_event = wm8753_dapm_event; |
| 1567 | codec->dai = wm8753_dai; |
| 1568 | codec->num_dai = 2; |
| 1569 | codec->reg_cache_size = sizeof(wm8753_reg); |
| 1570 | codec->reg_cache = kmemdup(wm8753_reg, sizeof(wm8753_reg), GFP_KERNEL); |
| 1571 | |
| 1572 | if (codec->reg_cache == NULL) |
| 1573 | return -ENOMEM; |
| 1574 | |
| 1575 | wm8753_set_dai_mode(codec, 0); |
| 1576 | |
| 1577 | wm8753_reset(codec); |
| 1578 | |
| 1579 | /* register pcms */ |
| 1580 | ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); |
| 1581 | if (ret < 0) { |
| 1582 | printk(KERN_ERR "wm8753: failed to create pcms\n"); |
| 1583 | goto pcm_err; |
| 1584 | } |
| 1585 | |
| 1586 | /* charge output caps */ |
| 1587 | wm8753_dapm_event(codec, SNDRV_CTL_POWER_D2); |
| 1588 | codec->dapm_state = SNDRV_CTL_POWER_D3hot; |
| 1589 | schedule_delayed_work(&codec->delayed_work, |
| 1590 | msecs_to_jiffies(caps_charge)); |
| 1591 | |
| 1592 | /* set the update bits */ |
| 1593 | reg = wm8753_read_reg_cache(codec, WM8753_LDAC); |
| 1594 | wm8753_write(codec, WM8753_LDAC, reg | 0x0100); |
| 1595 | reg = wm8753_read_reg_cache(codec, WM8753_RDAC); |
| 1596 | wm8753_write(codec, WM8753_RDAC, reg | 0x0100); |
| 1597 | reg = wm8753_read_reg_cache(codec, WM8753_LADC); |
| 1598 | wm8753_write(codec, WM8753_LADC, reg | 0x0100); |
| 1599 | reg = wm8753_read_reg_cache(codec, WM8753_RADC); |
| 1600 | wm8753_write(codec, WM8753_RADC, reg | 0x0100); |
| 1601 | reg = wm8753_read_reg_cache(codec, WM8753_LOUT1V); |
| 1602 | wm8753_write(codec, WM8753_LOUT1V, reg | 0x0100); |
| 1603 | reg = wm8753_read_reg_cache(codec, WM8753_ROUT1V); |
| 1604 | wm8753_write(codec, WM8753_ROUT1V, reg | 0x0100); |
| 1605 | reg = wm8753_read_reg_cache(codec, WM8753_LOUT2V); |
| 1606 | wm8753_write(codec, WM8753_LOUT2V, reg | 0x0100); |
| 1607 | reg = wm8753_read_reg_cache(codec, WM8753_ROUT2V); |
| 1608 | wm8753_write(codec, WM8753_ROUT2V, reg | 0x0100); |
| 1609 | reg = wm8753_read_reg_cache(codec, WM8753_LINVOL); |
| 1610 | wm8753_write(codec, WM8753_LINVOL, reg | 0x0100); |
| 1611 | reg = wm8753_read_reg_cache(codec, WM8753_RINVOL); |
| 1612 | wm8753_write(codec, WM8753_RINVOL, reg | 0x0100); |
| 1613 | |
| 1614 | wm8753_add_controls(codec); |
| 1615 | wm8753_add_widgets(codec); |
| 1616 | ret = snd_soc_register_card(socdev); |
| 1617 | if (ret < 0) { |
Mark Brown | 60fc684 | 2008-04-30 17:18:43 +0200 | [diff] [blame^] | 1618 | printk(KERN_ERR "wm8753: failed to register card\n"); |
Liam Girdwood | 1f53aee | 2007-04-16 19:17:44 +0200 | [diff] [blame] | 1619 | goto card_err; |
Mark Brown | 60fc684 | 2008-04-30 17:18:43 +0200 | [diff] [blame^] | 1620 | } |
| 1621 | |
Liam Girdwood | 1f53aee | 2007-04-16 19:17:44 +0200 | [diff] [blame] | 1622 | return ret; |
| 1623 | |
| 1624 | card_err: |
| 1625 | snd_soc_free_pcms(socdev); |
| 1626 | snd_soc_dapm_free(socdev); |
| 1627 | pcm_err: |
| 1628 | kfree(codec->reg_cache); |
| 1629 | return ret; |
| 1630 | } |
| 1631 | |
| 1632 | /* If the i2c layer weren't so broken, we could pass this kind of data |
| 1633 | around */ |
| 1634 | static struct snd_soc_device *wm8753_socdev; |
| 1635 | |
Mark Brown | 60fc684 | 2008-04-30 17:18:43 +0200 | [diff] [blame^] | 1636 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
Liam Girdwood | 1f53aee | 2007-04-16 19:17:44 +0200 | [diff] [blame] | 1637 | |
| 1638 | /* |
| 1639 | * WM8753 2 wire address is determined by GPIO5 |
| 1640 | * state during powerup. |
| 1641 | * low = 0x1a |
| 1642 | * high = 0x1b |
| 1643 | */ |
| 1644 | static unsigned short normal_i2c[] = { 0, I2C_CLIENT_END }; |
| 1645 | |
| 1646 | /* Magic definition of all other variables and things */ |
| 1647 | I2C_CLIENT_INSMOD; |
| 1648 | |
| 1649 | static struct i2c_driver wm8753_i2c_driver; |
| 1650 | static struct i2c_client client_template; |
| 1651 | |
| 1652 | static int wm8753_codec_probe(struct i2c_adapter *adap, int addr, int kind) |
| 1653 | { |
| 1654 | struct snd_soc_device *socdev = wm8753_socdev; |
| 1655 | struct wm8753_setup_data *setup = socdev->codec_data; |
| 1656 | struct snd_soc_codec *codec = socdev->codec; |
| 1657 | struct i2c_client *i2c; |
| 1658 | int ret; |
| 1659 | |
| 1660 | if (addr != setup->i2c_address) |
| 1661 | return -ENODEV; |
| 1662 | |
| 1663 | client_template.adapter = adap; |
| 1664 | client_template.addr = addr; |
| 1665 | |
| 1666 | i2c = kmemdup(&client_template, sizeof(client_template), GFP_KERNEL); |
Mark Brown | 60fc684 | 2008-04-30 17:18:43 +0200 | [diff] [blame^] | 1667 | if (!i2c) { |
Liam Girdwood | 1f53aee | 2007-04-16 19:17:44 +0200 | [diff] [blame] | 1668 | kfree(codec); |
| 1669 | return -ENOMEM; |
| 1670 | } |
| 1671 | i2c_set_clientdata(i2c, codec); |
| 1672 | codec->control_data = i2c; |
| 1673 | |
| 1674 | ret = i2c_attach_client(i2c); |
| 1675 | if (ret < 0) { |
| 1676 | err("failed to attach codec at addr %x\n", addr); |
| 1677 | goto err; |
| 1678 | } |
| 1679 | |
| 1680 | ret = wm8753_init(socdev); |
| 1681 | if (ret < 0) { |
| 1682 | err("failed to initialise WM8753\n"); |
| 1683 | goto err; |
| 1684 | } |
| 1685 | |
| 1686 | return ret; |
| 1687 | |
| 1688 | err: |
| 1689 | kfree(codec); |
| 1690 | kfree(i2c); |
| 1691 | return ret; |
| 1692 | } |
| 1693 | |
| 1694 | static int wm8753_i2c_detach(struct i2c_client *client) |
| 1695 | { |
| 1696 | struct snd_soc_codec *codec = i2c_get_clientdata(client); |
| 1697 | i2c_detach_client(client); |
| 1698 | kfree(codec->reg_cache); |
| 1699 | kfree(client); |
| 1700 | return 0; |
| 1701 | } |
| 1702 | |
| 1703 | static int wm8753_i2c_attach(struct i2c_adapter *adap) |
| 1704 | { |
| 1705 | return i2c_probe(adap, &addr_data, wm8753_codec_probe); |
| 1706 | } |
| 1707 | |
| 1708 | /* corgi i2c codec control layer */ |
| 1709 | static struct i2c_driver wm8753_i2c_driver = { |
| 1710 | .driver = { |
| 1711 | .name = "WM8753 I2C Codec", |
| 1712 | .owner = THIS_MODULE, |
| 1713 | }, |
| 1714 | .id = I2C_DRIVERID_WM8753, |
| 1715 | .attach_adapter = wm8753_i2c_attach, |
| 1716 | .detach_client = wm8753_i2c_detach, |
| 1717 | .command = NULL, |
| 1718 | }; |
| 1719 | |
| 1720 | static struct i2c_client client_template = { |
| 1721 | .name = "WM8753", |
| 1722 | .driver = &wm8753_i2c_driver, |
| 1723 | }; |
| 1724 | #endif |
| 1725 | |
| 1726 | static int wm8753_probe(struct platform_device *pdev) |
| 1727 | { |
| 1728 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 1729 | struct wm8753_setup_data *setup; |
| 1730 | struct snd_soc_codec *codec; |
| 1731 | struct wm8753_priv *wm8753; |
| 1732 | int ret = 0; |
| 1733 | |
| 1734 | info("WM8753 Audio Codec %s", WM8753_VERSION); |
| 1735 | |
| 1736 | setup = socdev->codec_data; |
| 1737 | codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL); |
| 1738 | if (codec == NULL) |
| 1739 | return -ENOMEM; |
| 1740 | |
| 1741 | wm8753 = kzalloc(sizeof(struct wm8753_priv), GFP_KERNEL); |
| 1742 | if (wm8753 == NULL) { |
| 1743 | kfree(codec); |
| 1744 | return -ENOMEM; |
| 1745 | } |
| 1746 | |
| 1747 | codec->private_data = wm8753; |
| 1748 | socdev->codec = codec; |
| 1749 | mutex_init(&codec->mutex); |
| 1750 | INIT_LIST_HEAD(&codec->dapm_widgets); |
| 1751 | INIT_LIST_HEAD(&codec->dapm_paths); |
| 1752 | wm8753_socdev = socdev; |
| 1753 | INIT_DELAYED_WORK(&codec->delayed_work, wm8753_work); |
| 1754 | |
Mark Brown | 60fc684 | 2008-04-30 17:18:43 +0200 | [diff] [blame^] | 1755 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
Liam Girdwood | 1f53aee | 2007-04-16 19:17:44 +0200 | [diff] [blame] | 1756 | if (setup->i2c_address) { |
| 1757 | normal_i2c[0] = setup->i2c_address; |
| 1758 | codec->hw_write = (hw_write_t)i2c_master_send; |
| 1759 | ret = i2c_add_driver(&wm8753_i2c_driver); |
| 1760 | if (ret != 0) |
| 1761 | printk(KERN_ERR "can't add i2c driver"); |
| 1762 | } |
| 1763 | #else |
| 1764 | /* Add other interfaces here */ |
| 1765 | #endif |
| 1766 | return ret; |
| 1767 | } |
| 1768 | |
| 1769 | /* |
| 1770 | * This function forces any delayed work to be queued and run. |
| 1771 | */ |
| 1772 | static int run_delayed_work(struct delayed_work *dwork) |
| 1773 | { |
| 1774 | int ret; |
| 1775 | |
| 1776 | /* cancel any work waiting to be queued. */ |
| 1777 | ret = cancel_delayed_work(dwork); |
| 1778 | |
| 1779 | /* if there was any work waiting then we run it now and |
| 1780 | * wait for it's completion */ |
| 1781 | if (ret) { |
| 1782 | schedule_delayed_work(dwork, 0); |
| 1783 | flush_scheduled_work(); |
| 1784 | } |
| 1785 | return ret; |
| 1786 | } |
| 1787 | |
| 1788 | /* power down chip */ |
| 1789 | static int wm8753_remove(struct platform_device *pdev) |
| 1790 | { |
| 1791 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 1792 | struct snd_soc_codec *codec = socdev->codec; |
| 1793 | |
| 1794 | if (codec->control_data) |
| 1795 | wm8753_dapm_event(codec, SNDRV_CTL_POWER_D3cold); |
| 1796 | run_delayed_work(&codec->delayed_work); |
| 1797 | snd_soc_free_pcms(socdev); |
| 1798 | snd_soc_dapm_free(socdev); |
Mark Brown | 60fc684 | 2008-04-30 17:18:43 +0200 | [diff] [blame^] | 1799 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
Liam Girdwood | 1f53aee | 2007-04-16 19:17:44 +0200 | [diff] [blame] | 1800 | i2c_del_driver(&wm8753_i2c_driver); |
| 1801 | #endif |
| 1802 | kfree(codec->private_data); |
| 1803 | kfree(codec); |
| 1804 | |
| 1805 | return 0; |
| 1806 | } |
| 1807 | |
| 1808 | struct snd_soc_codec_device soc_codec_dev_wm8753 = { |
| 1809 | .probe = wm8753_probe, |
| 1810 | .remove = wm8753_remove, |
| 1811 | .suspend = wm8753_suspend, |
| 1812 | .resume = wm8753_resume, |
| 1813 | }; |
Liam Girdwood | 1f53aee | 2007-04-16 19:17:44 +0200 | [diff] [blame] | 1814 | EXPORT_SYMBOL_GPL(soc_codec_dev_wm8753); |
| 1815 | |
| 1816 | MODULE_DESCRIPTION("ASoC WM8753 driver"); |
| 1817 | MODULE_AUTHOR("Liam Girdwood"); |
| 1818 | MODULE_LICENSE("GPL"); |