Steve Sakoman | cc17557 | 2008-10-30 21:35:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ALSA SoC TWL4030 codec driver |
| 3 | * |
| 4 | * Author: Steve Sakoman, <steve@sakoman.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * version 2 as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 18 | * 02110-1301 USA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/moduleparam.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/delay.h> |
| 26 | #include <linux/pm.h> |
| 27 | #include <linux/i2c.h> |
| 28 | #include <linux/platform_device.h> |
| 29 | #include <linux/i2c/twl4030.h> |
| 30 | #include <sound/core.h> |
| 31 | #include <sound/pcm.h> |
| 32 | #include <sound/pcm_params.h> |
| 33 | #include <sound/soc.h> |
| 34 | #include <sound/soc-dapm.h> |
| 35 | #include <sound/initval.h> |
Peter Ujfalusi | c10b82c | 2008-11-24 13:49:35 +0200 | [diff] [blame] | 36 | #include <sound/tlv.h> |
Steve Sakoman | cc17557 | 2008-10-30 21:35:26 -0700 | [diff] [blame] | 37 | |
| 38 | #include "twl4030.h" |
| 39 | |
| 40 | /* |
| 41 | * twl4030 register cache & default register settings |
| 42 | */ |
| 43 | static const u8 twl4030_reg[TWL4030_CACHEREGNUM] = { |
| 44 | 0x00, /* this register not used */ |
| 45 | 0x93, /* REG_CODEC_MODE (0x1) */ |
| 46 | 0xc3, /* REG_OPTION (0x2) */ |
| 47 | 0x00, /* REG_UNKNOWN (0x3) */ |
| 48 | 0x00, /* REG_MICBIAS_CTL (0x4) */ |
| 49 | 0x24, /* REG_ANAMICL (0x5) */ |
| 50 | 0x04, /* REG_ANAMICR (0x6) */ |
| 51 | 0x0a, /* REG_AVADC_CTL (0x7) */ |
| 52 | 0x00, /* REG_ADCMICSEL (0x8) */ |
| 53 | 0x00, /* REG_DIGMIXING (0x9) */ |
| 54 | 0x0c, /* REG_ATXL1PGA (0xA) */ |
| 55 | 0x0c, /* REG_ATXR1PGA (0xB) */ |
| 56 | 0x00, /* REG_AVTXL2PGA (0xC) */ |
| 57 | 0x00, /* REG_AVTXR2PGA (0xD) */ |
| 58 | 0x01, /* REG_AUDIO_IF (0xE) */ |
| 59 | 0x00, /* REG_VOICE_IF (0xF) */ |
| 60 | 0x00, /* REG_ARXR1PGA (0x10) */ |
| 61 | 0x00, /* REG_ARXL1PGA (0x11) */ |
| 62 | 0x6c, /* REG_ARXR2PGA (0x12) */ |
| 63 | 0x6c, /* REG_ARXL2PGA (0x13) */ |
| 64 | 0x00, /* REG_VRXPGA (0x14) */ |
| 65 | 0x00, /* REG_VSTPGA (0x15) */ |
| 66 | 0x00, /* REG_VRX2ARXPGA (0x16) */ |
| 67 | 0x0c, /* REG_AVDAC_CTL (0x17) */ |
| 68 | 0x00, /* REG_ARX2VTXPGA (0x18) */ |
| 69 | 0x00, /* REG_ARXL1_APGA_CTL (0x19) */ |
| 70 | 0x00, /* REG_ARXR1_APGA_CTL (0x1A) */ |
| 71 | 0x4b, /* REG_ARXL2_APGA_CTL (0x1B) */ |
| 72 | 0x4b, /* REG_ARXR2_APGA_CTL (0x1C) */ |
| 73 | 0x00, /* REG_ATX2ARXPGA (0x1D) */ |
| 74 | 0x00, /* REG_BT_IF (0x1E) */ |
| 75 | 0x00, /* REG_BTPGA (0x1F) */ |
| 76 | 0x00, /* REG_BTSTPGA (0x20) */ |
| 77 | 0x00, /* REG_EAR_CTL (0x21) */ |
| 78 | 0x24, /* REG_HS_SEL (0x22) */ |
| 79 | 0x0a, /* REG_HS_GAIN_SET (0x23) */ |
| 80 | 0x00, /* REG_HS_POPN_SET (0x24) */ |
| 81 | 0x00, /* REG_PREDL_CTL (0x25) */ |
| 82 | 0x00, /* REG_PREDR_CTL (0x26) */ |
| 83 | 0x00, /* REG_PRECKL_CTL (0x27) */ |
| 84 | 0x00, /* REG_PRECKR_CTL (0x28) */ |
| 85 | 0x00, /* REG_HFL_CTL (0x29) */ |
| 86 | 0x00, /* REG_HFR_CTL (0x2A) */ |
| 87 | 0x00, /* REG_ALC_CTL (0x2B) */ |
| 88 | 0x00, /* REG_ALC_SET1 (0x2C) */ |
| 89 | 0x00, /* REG_ALC_SET2 (0x2D) */ |
| 90 | 0x00, /* REG_BOOST_CTL (0x2E) */ |
Peter Ujfalusi | f8d05bd | 2008-11-24 08:25:45 +0200 | [diff] [blame] | 91 | 0x00, /* REG_SOFTVOL_CTL (0x2F) */ |
Steve Sakoman | cc17557 | 2008-10-30 21:35:26 -0700 | [diff] [blame] | 92 | 0x00, /* REG_DTMF_FREQSEL (0x30) */ |
| 93 | 0x00, /* REG_DTMF_TONEXT1H (0x31) */ |
| 94 | 0x00, /* REG_DTMF_TONEXT1L (0x32) */ |
| 95 | 0x00, /* REG_DTMF_TONEXT2H (0x33) */ |
| 96 | 0x00, /* REG_DTMF_TONEXT2L (0x34) */ |
| 97 | 0x00, /* REG_DTMF_TONOFF (0x35) */ |
| 98 | 0x00, /* REG_DTMF_WANONOFF (0x36) */ |
| 99 | 0x00, /* REG_I2S_RX_SCRAMBLE_H (0x37) */ |
| 100 | 0x00, /* REG_I2S_RX_SCRAMBLE_M (0x38) */ |
| 101 | 0x00, /* REG_I2S_RX_SCRAMBLE_L (0x39) */ |
| 102 | 0x16, /* REG_APLL_CTL (0x3A) */ |
| 103 | 0x00, /* REG_DTMF_CTL (0x3B) */ |
| 104 | 0x00, /* REG_DTMF_PGA_CTL2 (0x3C) */ |
| 105 | 0x00, /* REG_DTMF_PGA_CTL1 (0x3D) */ |
| 106 | 0x00, /* REG_MISC_SET_1 (0x3E) */ |
| 107 | 0x00, /* REG_PCMBTMUX (0x3F) */ |
| 108 | 0x00, /* not used (0x40) */ |
| 109 | 0x00, /* not used (0x41) */ |
| 110 | 0x00, /* not used (0x42) */ |
| 111 | 0x00, /* REG_RX_PATH_SEL (0x43) */ |
| 112 | 0x00, /* REG_VDL_APGA_CTL (0x44) */ |
| 113 | 0x00, /* REG_VIBRA_CTL (0x45) */ |
| 114 | 0x00, /* REG_VIBRA_SET (0x46) */ |
| 115 | 0x00, /* REG_VIBRA_PWM_SET (0x47) */ |
| 116 | 0x00, /* REG_ANAMIC_GAIN (0x48) */ |
| 117 | 0x00, /* REG_MISC_SET_2 (0x49) */ |
| 118 | }; |
| 119 | |
| 120 | /* |
| 121 | * read twl4030 register cache |
| 122 | */ |
| 123 | static inline unsigned int twl4030_read_reg_cache(struct snd_soc_codec *codec, |
| 124 | unsigned int reg) |
| 125 | { |
| 126 | u8 *cache = codec->reg_cache; |
| 127 | |
| 128 | return cache[reg]; |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * write twl4030 register cache |
| 133 | */ |
| 134 | static inline void twl4030_write_reg_cache(struct snd_soc_codec *codec, |
| 135 | u8 reg, u8 value) |
| 136 | { |
| 137 | u8 *cache = codec->reg_cache; |
| 138 | |
| 139 | if (reg >= TWL4030_CACHEREGNUM) |
| 140 | return; |
| 141 | cache[reg] = value; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * write to the twl4030 register space |
| 146 | */ |
| 147 | static int twl4030_write(struct snd_soc_codec *codec, |
| 148 | unsigned int reg, unsigned int value) |
| 149 | { |
| 150 | twl4030_write_reg_cache(codec, reg, value); |
| 151 | return twl4030_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE, value, reg); |
| 152 | } |
| 153 | |
| 154 | static void twl4030_clear_codecpdz(struct snd_soc_codec *codec) |
| 155 | { |
| 156 | u8 mode; |
| 157 | |
| 158 | mode = twl4030_read_reg_cache(codec, TWL4030_REG_CODEC_MODE); |
| 159 | twl4030_write(codec, TWL4030_REG_CODEC_MODE, |
| 160 | mode & ~TWL4030_CODECPDZ); |
| 161 | |
| 162 | /* REVISIT: this delay is present in TI sample drivers */ |
| 163 | /* but there seems to be no TRM requirement for it */ |
| 164 | udelay(10); |
| 165 | } |
| 166 | |
| 167 | static void twl4030_set_codecpdz(struct snd_soc_codec *codec) |
| 168 | { |
| 169 | u8 mode; |
| 170 | |
| 171 | mode = twl4030_read_reg_cache(codec, TWL4030_REG_CODEC_MODE); |
| 172 | twl4030_write(codec, TWL4030_REG_CODEC_MODE, |
| 173 | mode | TWL4030_CODECPDZ); |
| 174 | |
| 175 | /* REVISIT: this delay is present in TI sample drivers */ |
| 176 | /* but there seems to be no TRM requirement for it */ |
| 177 | udelay(10); |
| 178 | } |
| 179 | |
| 180 | static void twl4030_init_chip(struct snd_soc_codec *codec) |
| 181 | { |
| 182 | int i; |
| 183 | |
| 184 | /* clear CODECPDZ prior to setting register defaults */ |
| 185 | twl4030_clear_codecpdz(codec); |
| 186 | |
| 187 | /* set all audio section registers to reasonable defaults */ |
| 188 | for (i = TWL4030_REG_OPTION; i <= TWL4030_REG_MISC_SET_2; i++) |
| 189 | twl4030_write(codec, i, twl4030_reg[i]); |
| 190 | |
| 191 | } |
| 192 | |
Peter Ujfalusi | c10b82c | 2008-11-24 13:49:35 +0200 | [diff] [blame] | 193 | /* |
Peter Ujfalusi | b0bd53a | 2008-11-24 13:49:38 +0200 | [diff] [blame] | 194 | * Some of the gain controls in TWL (mostly those which are associated with |
| 195 | * the outputs) are implemented in an interesting way: |
| 196 | * 0x0 : Power down (mute) |
| 197 | * 0x1 : 6dB |
| 198 | * 0x2 : 0 dB |
| 199 | * 0x3 : -6 dB |
| 200 | * Inverting not going to help with these. |
| 201 | * Custom volsw and volsw_2r get/put functions to handle these gain bits. |
| 202 | */ |
| 203 | #define SOC_DOUBLE_TLV_TWL4030(xname, xreg, shift_left, shift_right, xmax,\ |
| 204 | xinvert, tlv_array) \ |
| 205 | { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\ |
| 206 | .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\ |
| 207 | SNDRV_CTL_ELEM_ACCESS_READWRITE,\ |
| 208 | .tlv.p = (tlv_array), \ |
| 209 | .info = snd_soc_info_volsw, \ |
| 210 | .get = snd_soc_get_volsw_twl4030, \ |
| 211 | .put = snd_soc_put_volsw_twl4030, \ |
| 212 | .private_value = (unsigned long)&(struct soc_mixer_control) \ |
| 213 | {.reg = xreg, .shift = shift_left, .rshift = shift_right,\ |
| 214 | .max = xmax, .invert = xinvert} } |
| 215 | #define SOC_DOUBLE_R_TLV_TWL4030(xname, reg_left, reg_right, xshift, xmax,\ |
| 216 | xinvert, tlv_array) \ |
| 217 | { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\ |
| 218 | .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\ |
| 219 | SNDRV_CTL_ELEM_ACCESS_READWRITE,\ |
| 220 | .tlv.p = (tlv_array), \ |
| 221 | .info = snd_soc_info_volsw_2r, \ |
| 222 | .get = snd_soc_get_volsw_r2_twl4030,\ |
| 223 | .put = snd_soc_put_volsw_r2_twl4030, \ |
| 224 | .private_value = (unsigned long)&(struct soc_mixer_control) \ |
| 225 | {.reg = reg_left, .rreg = reg_right, .shift = xshift, \ |
| 226 | .max = xmax, .invert = xinvert} } |
| 227 | #define SOC_SINGLE_TLV_TWL4030(xname, xreg, xshift, xmax, xinvert, tlv_array) \ |
| 228 | SOC_DOUBLE_TLV_TWL4030(xname, xreg, xshift, xshift, xmax, \ |
| 229 | xinvert, tlv_array) |
| 230 | |
| 231 | static int snd_soc_get_volsw_twl4030(struct snd_kcontrol *kcontrol, |
| 232 | struct snd_ctl_elem_value *ucontrol) |
| 233 | { |
| 234 | struct soc_mixer_control *mc = |
| 235 | (struct soc_mixer_control *)kcontrol->private_value; |
| 236 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 237 | unsigned int reg = mc->reg; |
| 238 | unsigned int shift = mc->shift; |
| 239 | unsigned int rshift = mc->rshift; |
| 240 | int max = mc->max; |
| 241 | int mask = (1 << fls(max)) - 1; |
| 242 | |
| 243 | ucontrol->value.integer.value[0] = |
| 244 | (snd_soc_read(codec, reg) >> shift) & mask; |
| 245 | if (ucontrol->value.integer.value[0]) |
| 246 | ucontrol->value.integer.value[0] = |
| 247 | max + 1 - ucontrol->value.integer.value[0]; |
| 248 | |
| 249 | if (shift != rshift) { |
| 250 | ucontrol->value.integer.value[1] = |
| 251 | (snd_soc_read(codec, reg) >> rshift) & mask; |
| 252 | if (ucontrol->value.integer.value[1]) |
| 253 | ucontrol->value.integer.value[1] = |
| 254 | max + 1 - ucontrol->value.integer.value[1]; |
| 255 | } |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static int snd_soc_put_volsw_twl4030(struct snd_kcontrol *kcontrol, |
| 261 | struct snd_ctl_elem_value *ucontrol) |
| 262 | { |
| 263 | struct soc_mixer_control *mc = |
| 264 | (struct soc_mixer_control *)kcontrol->private_value; |
| 265 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 266 | unsigned int reg = mc->reg; |
| 267 | unsigned int shift = mc->shift; |
| 268 | unsigned int rshift = mc->rshift; |
| 269 | int max = mc->max; |
| 270 | int mask = (1 << fls(max)) - 1; |
| 271 | unsigned short val, val2, val_mask; |
| 272 | |
| 273 | val = (ucontrol->value.integer.value[0] & mask); |
| 274 | |
| 275 | val_mask = mask << shift; |
| 276 | if (val) |
| 277 | val = max + 1 - val; |
| 278 | val = val << shift; |
| 279 | if (shift != rshift) { |
| 280 | val2 = (ucontrol->value.integer.value[1] & mask); |
| 281 | val_mask |= mask << rshift; |
| 282 | if (val2) |
| 283 | val2 = max + 1 - val2; |
| 284 | val |= val2 << rshift; |
| 285 | } |
| 286 | return snd_soc_update_bits(codec, reg, val_mask, val); |
| 287 | } |
| 288 | |
| 289 | static int snd_soc_get_volsw_r2_twl4030(struct snd_kcontrol *kcontrol, |
| 290 | struct snd_ctl_elem_value *ucontrol) |
| 291 | { |
| 292 | struct soc_mixer_control *mc = |
| 293 | (struct soc_mixer_control *)kcontrol->private_value; |
| 294 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 295 | unsigned int reg = mc->reg; |
| 296 | unsigned int reg2 = mc->rreg; |
| 297 | unsigned int shift = mc->shift; |
| 298 | int max = mc->max; |
| 299 | int mask = (1<<fls(max))-1; |
| 300 | |
| 301 | ucontrol->value.integer.value[0] = |
| 302 | (snd_soc_read(codec, reg) >> shift) & mask; |
| 303 | ucontrol->value.integer.value[1] = |
| 304 | (snd_soc_read(codec, reg2) >> shift) & mask; |
| 305 | |
| 306 | if (ucontrol->value.integer.value[0]) |
| 307 | ucontrol->value.integer.value[0] = |
| 308 | max + 1 - ucontrol->value.integer.value[0]; |
| 309 | if (ucontrol->value.integer.value[1]) |
| 310 | ucontrol->value.integer.value[1] = |
| 311 | max + 1 - ucontrol->value.integer.value[1]; |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | static int snd_soc_put_volsw_r2_twl4030(struct snd_kcontrol *kcontrol, |
| 317 | struct snd_ctl_elem_value *ucontrol) |
| 318 | { |
| 319 | struct soc_mixer_control *mc = |
| 320 | (struct soc_mixer_control *)kcontrol->private_value; |
| 321 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 322 | unsigned int reg = mc->reg; |
| 323 | unsigned int reg2 = mc->rreg; |
| 324 | unsigned int shift = mc->shift; |
| 325 | int max = mc->max; |
| 326 | int mask = (1 << fls(max)) - 1; |
| 327 | int err; |
| 328 | unsigned short val, val2, val_mask; |
| 329 | |
| 330 | val_mask = mask << shift; |
| 331 | val = (ucontrol->value.integer.value[0] & mask); |
| 332 | val2 = (ucontrol->value.integer.value[1] & mask); |
| 333 | |
| 334 | if (val) |
| 335 | val = max + 1 - val; |
| 336 | if (val2) |
| 337 | val2 = max + 1 - val2; |
| 338 | |
| 339 | val = val << shift; |
| 340 | val2 = val2 << shift; |
| 341 | |
| 342 | err = snd_soc_update_bits(codec, reg, val_mask, val); |
| 343 | if (err < 0) |
| 344 | return err; |
| 345 | |
| 346 | err = snd_soc_update_bits(codec, reg2, val_mask, val2); |
| 347 | return err; |
| 348 | } |
| 349 | |
| 350 | /* |
Peter Ujfalusi | c10b82c | 2008-11-24 13:49:35 +0200 | [diff] [blame] | 351 | * FGAIN volume control: |
| 352 | * from -62 to 0 dB in 1 dB steps (mute instead of -63 dB) |
| 353 | */ |
Peter Ujfalusi | d889a72 | 2008-12-01 10:03:46 +0200 | [diff] [blame^] | 354 | static DECLARE_TLV_DB_SCALE(digital_fine_tlv, -6300, 100, 1); |
Peter Ujfalusi | c10b82c | 2008-11-24 13:49:35 +0200 | [diff] [blame] | 355 | |
Peter Ujfalusi | 0d33ea0 | 2008-11-24 13:49:36 +0200 | [diff] [blame] | 356 | /* |
| 357 | * CGAIN volume control: |
| 358 | * 0 dB to 12 dB in 6 dB steps |
| 359 | * value 2 and 3 means 12 dB |
| 360 | */ |
Peter Ujfalusi | d889a72 | 2008-12-01 10:03:46 +0200 | [diff] [blame^] | 361 | static DECLARE_TLV_DB_SCALE(digital_coarse_tlv, 0, 600, 0); |
| 362 | |
| 363 | /* |
| 364 | * Analog playback gain |
| 365 | * -24 dB to 12 dB in 2 dB steps |
| 366 | */ |
| 367 | static DECLARE_TLV_DB_SCALE(analog_tlv, -2400, 200, 0); |
Peter Ujfalusi | 0d33ea0 | 2008-11-24 13:49:36 +0200 | [diff] [blame] | 368 | |
Peter Ujfalusi | 381a22b | 2008-12-01 10:03:45 +0200 | [diff] [blame] | 369 | /* |
| 370 | * Capture gain after the ADCs |
| 371 | * from 0 dB to 31 dB in 1 dB steps |
| 372 | */ |
| 373 | static DECLARE_TLV_DB_SCALE(digital_capture_tlv, 0, 100, 0); |
| 374 | |
Steve Sakoman | cc17557 | 2008-10-30 21:35:26 -0700 | [diff] [blame] | 375 | static const struct snd_kcontrol_new twl4030_snd_controls[] = { |
Peter Ujfalusi | d889a72 | 2008-12-01 10:03:46 +0200 | [diff] [blame^] | 376 | /* Common playback gain controls */ |
| 377 | SOC_DOUBLE_R_TLV("DAC1 Digital Fine Playback Volume", |
| 378 | TWL4030_REG_ARXL1PGA, TWL4030_REG_ARXR1PGA, |
| 379 | 0, 0x3f, 0, digital_fine_tlv), |
| 380 | SOC_DOUBLE_R_TLV("DAC2 Digital Fine Playback Volume", |
| 381 | TWL4030_REG_ARXL2PGA, TWL4030_REG_ARXR2PGA, |
| 382 | 0, 0x3f, 0, digital_fine_tlv), |
| 383 | |
| 384 | SOC_DOUBLE_R_TLV("DAC1 Digital Coarse Playback Volume", |
| 385 | TWL4030_REG_ARXL1PGA, TWL4030_REG_ARXR1PGA, |
| 386 | 6, 0x2, 0, digital_coarse_tlv), |
| 387 | SOC_DOUBLE_R_TLV("DAC2 Digital Coarse Playback Volume", |
| 388 | TWL4030_REG_ARXL2PGA, TWL4030_REG_ARXR2PGA, |
| 389 | 6, 0x2, 0, digital_coarse_tlv), |
| 390 | |
| 391 | SOC_DOUBLE_R_TLV("DAC1 Analog Playback Volume", |
| 392 | TWL4030_REG_ARXL1_APGA_CTL, TWL4030_REG_ARXR1_APGA_CTL, |
| 393 | 3, 0x12, 1, analog_tlv), |
| 394 | SOC_DOUBLE_R_TLV("DAC2 Analog Playback Volume", |
| 395 | TWL4030_REG_ARXL2_APGA_CTL, TWL4030_REG_ARXR2_APGA_CTL, |
| 396 | 3, 0x12, 1, analog_tlv), |
Peter Ujfalusi | 381a22b | 2008-12-01 10:03:45 +0200 | [diff] [blame] | 397 | |
| 398 | /* Common capture gain controls */ |
| 399 | SOC_DOUBLE_R_TLV("Capture Volume", |
| 400 | TWL4030_REG_ATXL1PGA, TWL4030_REG_ATXR1PGA, |
| 401 | 0, 0x1f, 0, digital_capture_tlv), |
Steve Sakoman | cc17557 | 2008-10-30 21:35:26 -0700 | [diff] [blame] | 402 | }; |
| 403 | |
| 404 | /* add non dapm controls */ |
| 405 | static int twl4030_add_controls(struct snd_soc_codec *codec) |
| 406 | { |
| 407 | int err, i; |
| 408 | |
| 409 | for (i = 0; i < ARRAY_SIZE(twl4030_snd_controls); i++) { |
| 410 | err = snd_ctl_add(codec->card, |
| 411 | snd_soc_cnew(&twl4030_snd_controls[i], |
| 412 | codec, NULL)); |
| 413 | if (err < 0) |
| 414 | return err; |
| 415 | } |
| 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | static const struct snd_soc_dapm_widget twl4030_dapm_widgets[] = { |
| 421 | SND_SOC_DAPM_INPUT("INL"), |
| 422 | SND_SOC_DAPM_INPUT("INR"), |
| 423 | |
| 424 | SND_SOC_DAPM_OUTPUT("OUTL"), |
| 425 | SND_SOC_DAPM_OUTPUT("OUTR"), |
| 426 | |
| 427 | SND_SOC_DAPM_DAC("DACL", "Left Playback", SND_SOC_NOPM, 0, 0), |
| 428 | SND_SOC_DAPM_DAC("DACR", "Right Playback", SND_SOC_NOPM, 0, 0), |
| 429 | |
| 430 | SND_SOC_DAPM_ADC("ADCL", "Left Capture", SND_SOC_NOPM, 0, 0), |
| 431 | SND_SOC_DAPM_ADC("ADCR", "Right Capture", SND_SOC_NOPM, 0, 0), |
| 432 | }; |
| 433 | |
| 434 | static const struct snd_soc_dapm_route intercon[] = { |
| 435 | /* outputs */ |
| 436 | {"OUTL", NULL, "DACL"}, |
| 437 | {"OUTR", NULL, "DACR"}, |
| 438 | |
| 439 | /* inputs */ |
| 440 | {"ADCL", NULL, "INL"}, |
| 441 | {"ADCR", NULL, "INR"}, |
| 442 | }; |
| 443 | |
| 444 | static int twl4030_add_widgets(struct snd_soc_codec *codec) |
| 445 | { |
| 446 | snd_soc_dapm_new_controls(codec, twl4030_dapm_widgets, |
| 447 | ARRAY_SIZE(twl4030_dapm_widgets)); |
| 448 | |
| 449 | snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon)); |
| 450 | |
| 451 | snd_soc_dapm_new_widgets(codec); |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | static void twl4030_power_up(struct snd_soc_codec *codec) |
| 456 | { |
| 457 | u8 anamicl, regmisc1, byte, popn, hsgain; |
| 458 | int i = 0; |
| 459 | |
| 460 | /* set CODECPDZ to turn on codec */ |
| 461 | twl4030_set_codecpdz(codec); |
| 462 | |
| 463 | /* initiate offset cancellation */ |
| 464 | anamicl = twl4030_read_reg_cache(codec, TWL4030_REG_ANAMICL); |
| 465 | twl4030_write(codec, TWL4030_REG_ANAMICL, |
| 466 | anamicl | TWL4030_CNCL_OFFSET_START); |
| 467 | |
| 468 | /* wait for offset cancellation to complete */ |
| 469 | do { |
| 470 | /* this takes a little while, so don't slam i2c */ |
| 471 | udelay(2000); |
| 472 | twl4030_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &byte, |
| 473 | TWL4030_REG_ANAMICL); |
| 474 | } while ((i++ < 100) && |
| 475 | ((byte & TWL4030_CNCL_OFFSET_START) == |
| 476 | TWL4030_CNCL_OFFSET_START)); |
| 477 | |
| 478 | /* anti-pop when changing analog gain */ |
| 479 | regmisc1 = twl4030_read_reg_cache(codec, TWL4030_REG_MISC_SET_1); |
| 480 | twl4030_write(codec, TWL4030_REG_MISC_SET_1, |
| 481 | regmisc1 | TWL4030_SMOOTH_ANAVOL_EN); |
| 482 | |
| 483 | /* toggle CODECPDZ as per TRM */ |
| 484 | twl4030_clear_codecpdz(codec); |
| 485 | twl4030_set_codecpdz(codec); |
| 486 | |
| 487 | /* program anti-pop with bias ramp delay */ |
| 488 | popn = twl4030_read_reg_cache(codec, TWL4030_REG_HS_POPN_SET); |
| 489 | popn &= TWL4030_RAMP_DELAY; |
| 490 | popn |= TWL4030_RAMP_DELAY_645MS; |
| 491 | twl4030_write(codec, TWL4030_REG_HS_POPN_SET, popn); |
| 492 | popn |= TWL4030_VMID_EN; |
| 493 | twl4030_write(codec, TWL4030_REG_HS_POPN_SET, popn); |
| 494 | |
| 495 | /* enable output stage and gain setting */ |
| 496 | hsgain = TWL4030_HSR_GAIN_0DB | TWL4030_HSL_GAIN_0DB; |
| 497 | twl4030_write(codec, TWL4030_REG_HS_GAIN_SET, hsgain); |
| 498 | |
| 499 | /* enable anti-pop ramp */ |
| 500 | popn |= TWL4030_RAMP_EN; |
| 501 | twl4030_write(codec, TWL4030_REG_HS_POPN_SET, popn); |
| 502 | } |
| 503 | |
| 504 | static void twl4030_power_down(struct snd_soc_codec *codec) |
| 505 | { |
| 506 | u8 popn, hsgain; |
| 507 | |
| 508 | /* disable anti-pop ramp */ |
| 509 | popn = twl4030_read_reg_cache(codec, TWL4030_REG_HS_POPN_SET); |
| 510 | popn &= ~TWL4030_RAMP_EN; |
| 511 | twl4030_write(codec, TWL4030_REG_HS_POPN_SET, popn); |
| 512 | |
| 513 | /* disable output stage and gain setting */ |
| 514 | hsgain = TWL4030_HSR_GAIN_PWR_DOWN | TWL4030_HSL_GAIN_PWR_DOWN; |
| 515 | twl4030_write(codec, TWL4030_REG_HS_GAIN_SET, hsgain); |
| 516 | |
| 517 | /* disable bias out */ |
| 518 | popn &= ~TWL4030_VMID_EN; |
| 519 | twl4030_write(codec, TWL4030_REG_HS_POPN_SET, popn); |
| 520 | |
| 521 | /* power down */ |
| 522 | twl4030_clear_codecpdz(codec); |
| 523 | } |
| 524 | |
| 525 | static int twl4030_set_bias_level(struct snd_soc_codec *codec, |
| 526 | enum snd_soc_bias_level level) |
| 527 | { |
| 528 | switch (level) { |
| 529 | case SND_SOC_BIAS_ON: |
| 530 | twl4030_power_up(codec); |
| 531 | break; |
| 532 | case SND_SOC_BIAS_PREPARE: |
| 533 | /* TODO: develop a twl4030_prepare function */ |
| 534 | break; |
| 535 | case SND_SOC_BIAS_STANDBY: |
| 536 | /* TODO: develop a twl4030_standby function */ |
| 537 | twl4030_power_down(codec); |
| 538 | break; |
| 539 | case SND_SOC_BIAS_OFF: |
| 540 | twl4030_power_down(codec); |
| 541 | break; |
| 542 | } |
| 543 | codec->bias_level = level; |
| 544 | |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | static int twl4030_hw_params(struct snd_pcm_substream *substream, |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 549 | struct snd_pcm_hw_params *params, |
| 550 | struct snd_soc_dai *dai) |
Steve Sakoman | cc17557 | 2008-10-30 21:35:26 -0700 | [diff] [blame] | 551 | { |
| 552 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 553 | struct snd_soc_device *socdev = rtd->socdev; |
| 554 | struct snd_soc_codec *codec = socdev->codec; |
| 555 | u8 mode, old_mode, format, old_format; |
| 556 | |
| 557 | |
| 558 | /* bit rate */ |
| 559 | old_mode = twl4030_read_reg_cache(codec, |
| 560 | TWL4030_REG_CODEC_MODE) & ~TWL4030_CODECPDZ; |
| 561 | mode = old_mode & ~TWL4030_APLL_RATE; |
| 562 | |
| 563 | switch (params_rate(params)) { |
| 564 | case 8000: |
| 565 | mode |= TWL4030_APLL_RATE_8000; |
| 566 | break; |
| 567 | case 11025: |
| 568 | mode |= TWL4030_APLL_RATE_11025; |
| 569 | break; |
| 570 | case 12000: |
| 571 | mode |= TWL4030_APLL_RATE_12000; |
| 572 | break; |
| 573 | case 16000: |
| 574 | mode |= TWL4030_APLL_RATE_16000; |
| 575 | break; |
| 576 | case 22050: |
| 577 | mode |= TWL4030_APLL_RATE_22050; |
| 578 | break; |
| 579 | case 24000: |
| 580 | mode |= TWL4030_APLL_RATE_24000; |
| 581 | break; |
| 582 | case 32000: |
| 583 | mode |= TWL4030_APLL_RATE_32000; |
| 584 | break; |
| 585 | case 44100: |
| 586 | mode |= TWL4030_APLL_RATE_44100; |
| 587 | break; |
| 588 | case 48000: |
| 589 | mode |= TWL4030_APLL_RATE_48000; |
| 590 | break; |
| 591 | default: |
| 592 | printk(KERN_ERR "TWL4030 hw params: unknown rate %d\n", |
| 593 | params_rate(params)); |
| 594 | return -EINVAL; |
| 595 | } |
| 596 | |
| 597 | if (mode != old_mode) { |
| 598 | /* change rate and set CODECPDZ */ |
| 599 | twl4030_write(codec, TWL4030_REG_CODEC_MODE, mode); |
| 600 | twl4030_set_codecpdz(codec); |
| 601 | } |
| 602 | |
| 603 | /* sample size */ |
| 604 | old_format = twl4030_read_reg_cache(codec, TWL4030_REG_AUDIO_IF); |
| 605 | format = old_format; |
| 606 | format &= ~TWL4030_DATA_WIDTH; |
| 607 | switch (params_format(params)) { |
| 608 | case SNDRV_PCM_FORMAT_S16_LE: |
| 609 | format |= TWL4030_DATA_WIDTH_16S_16W; |
| 610 | break; |
| 611 | case SNDRV_PCM_FORMAT_S24_LE: |
| 612 | format |= TWL4030_DATA_WIDTH_32S_24W; |
| 613 | break; |
| 614 | default: |
| 615 | printk(KERN_ERR "TWL4030 hw params: unknown format %d\n", |
| 616 | params_format(params)); |
| 617 | return -EINVAL; |
| 618 | } |
| 619 | |
| 620 | if (format != old_format) { |
| 621 | |
| 622 | /* clear CODECPDZ before changing format (codec requirement) */ |
| 623 | twl4030_clear_codecpdz(codec); |
| 624 | |
| 625 | /* change format */ |
| 626 | twl4030_write(codec, TWL4030_REG_AUDIO_IF, format); |
| 627 | |
| 628 | /* set CODECPDZ afterwards */ |
| 629 | twl4030_set_codecpdz(codec); |
| 630 | } |
| 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | static int twl4030_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
| 635 | int clk_id, unsigned int freq, int dir) |
| 636 | { |
| 637 | struct snd_soc_codec *codec = codec_dai->codec; |
| 638 | u8 infreq; |
| 639 | |
| 640 | switch (freq) { |
| 641 | case 19200000: |
| 642 | infreq = TWL4030_APLL_INFREQ_19200KHZ; |
| 643 | break; |
| 644 | case 26000000: |
| 645 | infreq = TWL4030_APLL_INFREQ_26000KHZ; |
| 646 | break; |
| 647 | case 38400000: |
| 648 | infreq = TWL4030_APLL_INFREQ_38400KHZ; |
| 649 | break; |
| 650 | default: |
| 651 | printk(KERN_ERR "TWL4030 set sysclk: unknown rate %d\n", |
| 652 | freq); |
| 653 | return -EINVAL; |
| 654 | } |
| 655 | |
| 656 | infreq |= TWL4030_APLL_EN; |
| 657 | twl4030_write(codec, TWL4030_REG_APLL_CTL, infreq); |
| 658 | |
| 659 | return 0; |
| 660 | } |
| 661 | |
| 662 | static int twl4030_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 663 | unsigned int fmt) |
| 664 | { |
| 665 | struct snd_soc_codec *codec = codec_dai->codec; |
| 666 | u8 old_format, format; |
| 667 | |
| 668 | /* get format */ |
| 669 | old_format = twl4030_read_reg_cache(codec, TWL4030_REG_AUDIO_IF); |
| 670 | format = old_format; |
| 671 | |
| 672 | /* set master/slave audio interface */ |
| 673 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 674 | case SND_SOC_DAIFMT_CBM_CFM: |
| 675 | format &= ~(TWL4030_AIF_SLAVE_EN); |
Grazvydas Ignotas | e18c94d | 2008-11-05 23:51:05 +0200 | [diff] [blame] | 676 | format &= ~(TWL4030_CLK256FS_EN); |
Steve Sakoman | cc17557 | 2008-10-30 21:35:26 -0700 | [diff] [blame] | 677 | break; |
| 678 | case SND_SOC_DAIFMT_CBS_CFS: |
Steve Sakoman | cc17557 | 2008-10-30 21:35:26 -0700 | [diff] [blame] | 679 | format |= TWL4030_AIF_SLAVE_EN; |
Grazvydas Ignotas | e18c94d | 2008-11-05 23:51:05 +0200 | [diff] [blame] | 680 | format |= TWL4030_CLK256FS_EN; |
Steve Sakoman | cc17557 | 2008-10-30 21:35:26 -0700 | [diff] [blame] | 681 | break; |
| 682 | default: |
| 683 | return -EINVAL; |
| 684 | } |
| 685 | |
| 686 | /* interface format */ |
| 687 | format &= ~TWL4030_AIF_FORMAT; |
| 688 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 689 | case SND_SOC_DAIFMT_I2S: |
| 690 | format |= TWL4030_AIF_FORMAT_CODEC; |
| 691 | break; |
| 692 | default: |
| 693 | return -EINVAL; |
| 694 | } |
| 695 | |
| 696 | if (format != old_format) { |
| 697 | |
| 698 | /* clear CODECPDZ before changing format (codec requirement) */ |
| 699 | twl4030_clear_codecpdz(codec); |
| 700 | |
| 701 | /* change format */ |
| 702 | twl4030_write(codec, TWL4030_REG_AUDIO_IF, format); |
| 703 | |
| 704 | /* set CODECPDZ afterwards */ |
| 705 | twl4030_set_codecpdz(codec); |
| 706 | } |
| 707 | |
| 708 | return 0; |
| 709 | } |
| 710 | |
Jarkko Nikula | bbba944 | 2008-11-12 17:05:41 +0200 | [diff] [blame] | 711 | #define TWL4030_RATES (SNDRV_PCM_RATE_8000_48000) |
Steve Sakoman | cc17557 | 2008-10-30 21:35:26 -0700 | [diff] [blame] | 712 | #define TWL4030_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FORMAT_S24_LE) |
| 713 | |
| 714 | struct snd_soc_dai twl4030_dai = { |
| 715 | .name = "twl4030", |
| 716 | .playback = { |
| 717 | .stream_name = "Playback", |
| 718 | .channels_min = 2, |
| 719 | .channels_max = 2, |
| 720 | .rates = TWL4030_RATES, |
| 721 | .formats = TWL4030_FORMATS,}, |
| 722 | .capture = { |
| 723 | .stream_name = "Capture", |
| 724 | .channels_min = 2, |
| 725 | .channels_max = 2, |
| 726 | .rates = TWL4030_RATES, |
| 727 | .formats = TWL4030_FORMATS,}, |
| 728 | .ops = { |
| 729 | .hw_params = twl4030_hw_params, |
Steve Sakoman | cc17557 | 2008-10-30 21:35:26 -0700 | [diff] [blame] | 730 | .set_sysclk = twl4030_set_dai_sysclk, |
| 731 | .set_fmt = twl4030_set_dai_fmt, |
| 732 | } |
| 733 | }; |
| 734 | EXPORT_SYMBOL_GPL(twl4030_dai); |
| 735 | |
| 736 | static int twl4030_suspend(struct platform_device *pdev, pm_message_t state) |
| 737 | { |
| 738 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 739 | struct snd_soc_codec *codec = socdev->codec; |
| 740 | |
| 741 | twl4030_set_bias_level(codec, SND_SOC_BIAS_OFF); |
| 742 | |
| 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | static int twl4030_resume(struct platform_device *pdev) |
| 747 | { |
| 748 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 749 | struct snd_soc_codec *codec = socdev->codec; |
| 750 | |
| 751 | twl4030_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
| 752 | twl4030_set_bias_level(codec, codec->suspend_bias_level); |
| 753 | return 0; |
| 754 | } |
| 755 | |
| 756 | /* |
| 757 | * initialize the driver |
| 758 | * register the mixer and dsp interfaces with the kernel |
| 759 | */ |
| 760 | |
| 761 | static int twl4030_init(struct snd_soc_device *socdev) |
| 762 | { |
| 763 | struct snd_soc_codec *codec = socdev->codec; |
| 764 | int ret = 0; |
| 765 | |
| 766 | printk(KERN_INFO "TWL4030 Audio Codec init \n"); |
| 767 | |
| 768 | codec->name = "twl4030"; |
| 769 | codec->owner = THIS_MODULE; |
| 770 | codec->read = twl4030_read_reg_cache; |
| 771 | codec->write = twl4030_write; |
| 772 | codec->set_bias_level = twl4030_set_bias_level; |
| 773 | codec->dai = &twl4030_dai; |
| 774 | codec->num_dai = 1; |
| 775 | codec->reg_cache_size = sizeof(twl4030_reg); |
| 776 | codec->reg_cache = kmemdup(twl4030_reg, sizeof(twl4030_reg), |
| 777 | GFP_KERNEL); |
| 778 | if (codec->reg_cache == NULL) |
| 779 | return -ENOMEM; |
| 780 | |
| 781 | /* register pcms */ |
| 782 | ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); |
| 783 | if (ret < 0) { |
| 784 | printk(KERN_ERR "twl4030: failed to create pcms\n"); |
| 785 | goto pcm_err; |
| 786 | } |
| 787 | |
| 788 | twl4030_init_chip(codec); |
| 789 | |
| 790 | /* power on device */ |
| 791 | twl4030_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
| 792 | |
| 793 | twl4030_add_controls(codec); |
| 794 | twl4030_add_widgets(codec); |
| 795 | |
Mark Brown | 968a602 | 2008-11-28 11:49:07 +0000 | [diff] [blame] | 796 | ret = snd_soc_init_card(socdev); |
Steve Sakoman | cc17557 | 2008-10-30 21:35:26 -0700 | [diff] [blame] | 797 | if (ret < 0) { |
| 798 | printk(KERN_ERR "twl4030: failed to register card\n"); |
| 799 | goto card_err; |
| 800 | } |
| 801 | |
| 802 | return ret; |
| 803 | |
| 804 | card_err: |
| 805 | snd_soc_free_pcms(socdev); |
| 806 | snd_soc_dapm_free(socdev); |
| 807 | pcm_err: |
| 808 | kfree(codec->reg_cache); |
| 809 | return ret; |
| 810 | } |
| 811 | |
| 812 | static struct snd_soc_device *twl4030_socdev; |
| 813 | |
| 814 | static int twl4030_probe(struct platform_device *pdev) |
| 815 | { |
| 816 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 817 | struct snd_soc_codec *codec; |
| 818 | |
| 819 | codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL); |
| 820 | if (codec == NULL) |
| 821 | return -ENOMEM; |
| 822 | |
| 823 | socdev->codec = codec; |
| 824 | mutex_init(&codec->mutex); |
| 825 | INIT_LIST_HEAD(&codec->dapm_widgets); |
| 826 | INIT_LIST_HEAD(&codec->dapm_paths); |
| 827 | |
| 828 | twl4030_socdev = socdev; |
| 829 | twl4030_init(socdev); |
| 830 | |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | static int twl4030_remove(struct platform_device *pdev) |
| 835 | { |
| 836 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 837 | struct snd_soc_codec *codec = socdev->codec; |
| 838 | |
| 839 | printk(KERN_INFO "TWL4030 Audio Codec remove\n"); |
| 840 | kfree(codec); |
| 841 | |
| 842 | return 0; |
| 843 | } |
| 844 | |
| 845 | struct snd_soc_codec_device soc_codec_dev_twl4030 = { |
| 846 | .probe = twl4030_probe, |
| 847 | .remove = twl4030_remove, |
| 848 | .suspend = twl4030_suspend, |
| 849 | .resume = twl4030_resume, |
| 850 | }; |
| 851 | EXPORT_SYMBOL_GPL(soc_codec_dev_twl4030); |
| 852 | |
| 853 | MODULE_DESCRIPTION("ASoC TWL4030 codec driver"); |
| 854 | MODULE_AUTHOR("Steve Sakoman"); |
| 855 | MODULE_LICENSE("GPL"); |