Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ALSA SoC TWL6040 codec driver |
| 3 | * |
| 4 | * Author: Misael Lopez Cruz <x0052729@ti.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/gpio.h> |
| 29 | #include <linux/platform_device.h> |
Stephen Rothwell | 68b40cc | 2010-03-29 17:55:51 +1100 | [diff] [blame] | 30 | #include <linux/slab.h> |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 31 | #include <linux/i2c/twl.h> |
| 32 | |
| 33 | #include <sound/core.h> |
| 34 | #include <sound/pcm.h> |
| 35 | #include <sound/pcm_params.h> |
| 36 | #include <sound/soc.h> |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 37 | #include <sound/initval.h> |
| 38 | #include <sound/tlv.h> |
| 39 | |
| 40 | #include "twl6040.h" |
| 41 | |
| 42 | #define TWL6040_RATES (SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000) |
| 43 | #define TWL6040_FORMATS (SNDRV_PCM_FMTBIT_S32_LE) |
| 44 | |
| 45 | /* codec private data */ |
| 46 | struct twl6040_data { |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 47 | int audpwron; |
| 48 | int naudint; |
| 49 | int codec_powered; |
| 50 | int pll; |
| 51 | int non_lp; |
| 52 | unsigned int sysclk; |
| 53 | struct snd_pcm_hw_constraint_list *sysclk_constraints; |
| 54 | struct completion ready; |
| 55 | }; |
| 56 | |
| 57 | /* |
| 58 | * twl6040 register cache & default register settings |
| 59 | */ |
| 60 | static const u8 twl6040_reg[TWL6040_CACHEREGNUM] = { |
| 61 | 0x00, /* not used 0x00 */ |
| 62 | 0x4B, /* TWL6040_ASICID (ro) 0x01 */ |
| 63 | 0x00, /* TWL6040_ASICREV (ro) 0x02 */ |
| 64 | 0x00, /* TWL6040_INTID 0x03 */ |
| 65 | 0x00, /* TWL6040_INTMR 0x04 */ |
| 66 | 0x00, /* TWL6040_NCPCTRL 0x05 */ |
| 67 | 0x00, /* TWL6040_LDOCTL 0x06 */ |
| 68 | 0x60, /* TWL6040_HPPLLCTL 0x07 */ |
| 69 | 0x00, /* TWL6040_LPPLLCTL 0x08 */ |
| 70 | 0x4A, /* TWL6040_LPPLLDIV 0x09 */ |
| 71 | 0x00, /* TWL6040_AMICBCTL 0x0A */ |
| 72 | 0x00, /* TWL6040_DMICBCTL 0x0B */ |
| 73 | 0x18, /* TWL6040_MICLCTL 0x0C - No input selected on Left Mic */ |
| 74 | 0x18, /* TWL6040_MICRCTL 0x0D - No input selected on Right Mic */ |
| 75 | 0x00, /* TWL6040_MICGAIN 0x0E */ |
| 76 | 0x1B, /* TWL6040_LINEGAIN 0x0F */ |
| 77 | 0x00, /* TWL6040_HSLCTL 0x10 */ |
| 78 | 0x00, /* TWL6040_HSRCTL 0x11 */ |
| 79 | 0x00, /* TWL6040_HSGAIN 0x12 */ |
| 80 | 0x00, /* TWL6040_EARCTL 0x13 */ |
| 81 | 0x00, /* TWL6040_HFLCTL 0x14 */ |
| 82 | 0x00, /* TWL6040_HFLGAIN 0x15 */ |
| 83 | 0x00, /* TWL6040_HFRCTL 0x16 */ |
| 84 | 0x00, /* TWL6040_HFRGAIN 0x17 */ |
| 85 | 0x00, /* TWL6040_VIBCTLL 0x18 */ |
| 86 | 0x00, /* TWL6040_VIBDATL 0x19 */ |
| 87 | 0x00, /* TWL6040_VIBCTLR 0x1A */ |
| 88 | 0x00, /* TWL6040_VIBDATR 0x1B */ |
| 89 | 0x00, /* TWL6040_HKCTL1 0x1C */ |
| 90 | 0x00, /* TWL6040_HKCTL2 0x1D */ |
| 91 | 0x00, /* TWL6040_GPOCTL 0x1E */ |
| 92 | 0x00, /* TWL6040_ALB 0x1F */ |
| 93 | 0x00, /* TWL6040_DLB 0x20 */ |
| 94 | 0x00, /* not used 0x21 */ |
| 95 | 0x00, /* not used 0x22 */ |
| 96 | 0x00, /* not used 0x23 */ |
| 97 | 0x00, /* not used 0x24 */ |
| 98 | 0x00, /* not used 0x25 */ |
| 99 | 0x00, /* not used 0x26 */ |
| 100 | 0x00, /* not used 0x27 */ |
| 101 | 0x00, /* TWL6040_TRIM1 0x28 */ |
| 102 | 0x00, /* TWL6040_TRIM2 0x29 */ |
| 103 | 0x00, /* TWL6040_TRIM3 0x2A */ |
| 104 | 0x00, /* TWL6040_HSOTRIM 0x2B */ |
| 105 | 0x00, /* TWL6040_HFOTRIM 0x2C */ |
| 106 | 0x09, /* TWL6040_ACCCTL 0x2D */ |
| 107 | 0x00, /* TWL6040_STATUS (ro) 0x2E */ |
| 108 | }; |
| 109 | |
| 110 | /* |
| 111 | * twl6040 vio/gnd registers: |
| 112 | * registers under vio/gnd supply can be accessed |
| 113 | * before the power-up sequence, after NRESPWRON goes high |
| 114 | */ |
| 115 | static const int twl6040_vio_reg[TWL6040_VIOREGNUM] = { |
| 116 | TWL6040_REG_ASICID, |
| 117 | TWL6040_REG_ASICREV, |
| 118 | TWL6040_REG_INTID, |
| 119 | TWL6040_REG_INTMR, |
| 120 | TWL6040_REG_NCPCTL, |
| 121 | TWL6040_REG_LDOCTL, |
| 122 | TWL6040_REG_AMICBCTL, |
| 123 | TWL6040_REG_DMICBCTL, |
| 124 | TWL6040_REG_HKCTL1, |
| 125 | TWL6040_REG_HKCTL2, |
| 126 | TWL6040_REG_GPOCTL, |
| 127 | TWL6040_REG_TRIM1, |
| 128 | TWL6040_REG_TRIM2, |
| 129 | TWL6040_REG_TRIM3, |
| 130 | TWL6040_REG_HSOTRIM, |
| 131 | TWL6040_REG_HFOTRIM, |
| 132 | TWL6040_REG_ACCCTL, |
| 133 | TWL6040_REG_STATUS, |
| 134 | }; |
| 135 | |
| 136 | /* |
| 137 | * twl6040 vdd/vss registers: |
| 138 | * registers under vdd/vss supplies can only be accessed |
| 139 | * after the power-up sequence |
| 140 | */ |
| 141 | static const int twl6040_vdd_reg[TWL6040_VDDREGNUM] = { |
| 142 | TWL6040_REG_HPPLLCTL, |
| 143 | TWL6040_REG_LPPLLCTL, |
| 144 | TWL6040_REG_LPPLLDIV, |
| 145 | TWL6040_REG_MICLCTL, |
| 146 | TWL6040_REG_MICRCTL, |
| 147 | TWL6040_REG_MICGAIN, |
| 148 | TWL6040_REG_LINEGAIN, |
| 149 | TWL6040_REG_HSLCTL, |
| 150 | TWL6040_REG_HSRCTL, |
| 151 | TWL6040_REG_HSGAIN, |
| 152 | TWL6040_REG_EARCTL, |
| 153 | TWL6040_REG_HFLCTL, |
| 154 | TWL6040_REG_HFLGAIN, |
| 155 | TWL6040_REG_HFRCTL, |
| 156 | TWL6040_REG_HFRGAIN, |
| 157 | TWL6040_REG_VIBCTLL, |
| 158 | TWL6040_REG_VIBDATL, |
| 159 | TWL6040_REG_VIBCTLR, |
| 160 | TWL6040_REG_VIBDATR, |
| 161 | TWL6040_REG_ALB, |
| 162 | TWL6040_REG_DLB, |
| 163 | }; |
| 164 | |
| 165 | /* |
| 166 | * read twl6040 register cache |
| 167 | */ |
| 168 | static inline unsigned int twl6040_read_reg_cache(struct snd_soc_codec *codec, |
| 169 | unsigned int reg) |
| 170 | { |
| 171 | u8 *cache = codec->reg_cache; |
| 172 | |
| 173 | if (reg >= TWL6040_CACHEREGNUM) |
| 174 | return -EIO; |
| 175 | |
| 176 | return cache[reg]; |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * write twl6040 register cache |
| 181 | */ |
| 182 | static inline void twl6040_write_reg_cache(struct snd_soc_codec *codec, |
| 183 | u8 reg, u8 value) |
| 184 | { |
| 185 | u8 *cache = codec->reg_cache; |
| 186 | |
| 187 | if (reg >= TWL6040_CACHEREGNUM) |
| 188 | return; |
| 189 | cache[reg] = value; |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * read from twl6040 hardware register |
| 194 | */ |
| 195 | static int twl6040_read_reg_volatile(struct snd_soc_codec *codec, |
| 196 | unsigned int reg) |
| 197 | { |
| 198 | u8 value; |
| 199 | |
| 200 | if (reg >= TWL6040_CACHEREGNUM) |
| 201 | return -EIO; |
| 202 | |
| 203 | twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &value, reg); |
| 204 | twl6040_write_reg_cache(codec, reg, value); |
| 205 | |
| 206 | return value; |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * write to the twl6040 register space |
| 211 | */ |
| 212 | static int twl6040_write(struct snd_soc_codec *codec, |
| 213 | unsigned int reg, unsigned int value) |
| 214 | { |
| 215 | if (reg >= TWL6040_CACHEREGNUM) |
| 216 | return -EIO; |
| 217 | |
| 218 | twl6040_write_reg_cache(codec, reg, value); |
| 219 | return twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE, value, reg); |
| 220 | } |
| 221 | |
| 222 | static void twl6040_init_vio_regs(struct snd_soc_codec *codec) |
| 223 | { |
| 224 | u8 *cache = codec->reg_cache; |
| 225 | int reg, i; |
| 226 | |
| 227 | /* allow registers to be accessed by i2c */ |
| 228 | twl6040_write(codec, TWL6040_REG_ACCCTL, cache[TWL6040_REG_ACCCTL]); |
| 229 | |
| 230 | for (i = 0; i < TWL6040_VIOREGNUM; i++) { |
| 231 | reg = twl6040_vio_reg[i]; |
| 232 | /* skip read-only registers (ASICID, ASICREV, STATUS) */ |
| 233 | switch (reg) { |
| 234 | case TWL6040_REG_ASICID: |
| 235 | case TWL6040_REG_ASICREV: |
| 236 | case TWL6040_REG_STATUS: |
| 237 | continue; |
| 238 | default: |
| 239 | break; |
| 240 | } |
| 241 | twl6040_write(codec, reg, cache[reg]); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | static void twl6040_init_vdd_regs(struct snd_soc_codec *codec) |
| 246 | { |
| 247 | u8 *cache = codec->reg_cache; |
| 248 | int reg, i; |
| 249 | |
| 250 | for (i = 0; i < TWL6040_VDDREGNUM; i++) { |
| 251 | reg = twl6040_vdd_reg[i]; |
| 252 | twl6040_write(codec, reg, cache[reg]); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /* twl6040 codec manual power-up sequence */ |
| 257 | static void twl6040_power_up(struct snd_soc_codec *codec) |
| 258 | { |
| 259 | u8 ncpctl, ldoctl, lppllctl, accctl; |
| 260 | |
| 261 | ncpctl = twl6040_read_reg_cache(codec, TWL6040_REG_NCPCTL); |
| 262 | ldoctl = twl6040_read_reg_cache(codec, TWL6040_REG_LDOCTL); |
| 263 | lppllctl = twl6040_read_reg_cache(codec, TWL6040_REG_LPPLLCTL); |
| 264 | accctl = twl6040_read_reg_cache(codec, TWL6040_REG_ACCCTL); |
| 265 | |
| 266 | /* enable reference system */ |
| 267 | ldoctl |= TWL6040_REFENA; |
| 268 | twl6040_write(codec, TWL6040_REG_LDOCTL, ldoctl); |
| 269 | msleep(10); |
| 270 | /* enable internal oscillator */ |
| 271 | ldoctl |= TWL6040_OSCENA; |
| 272 | twl6040_write(codec, TWL6040_REG_LDOCTL, ldoctl); |
| 273 | udelay(10); |
| 274 | /* enable high-side ldo */ |
| 275 | ldoctl |= TWL6040_HSLDOENA; |
| 276 | twl6040_write(codec, TWL6040_REG_LDOCTL, ldoctl); |
| 277 | udelay(244); |
| 278 | /* enable negative charge pump */ |
| 279 | ncpctl |= TWL6040_NCPENA | TWL6040_NCPOPEN; |
| 280 | twl6040_write(codec, TWL6040_REG_NCPCTL, ncpctl); |
| 281 | udelay(488); |
| 282 | /* enable low-side ldo */ |
| 283 | ldoctl |= TWL6040_LSLDOENA; |
| 284 | twl6040_write(codec, TWL6040_REG_LDOCTL, ldoctl); |
| 285 | udelay(244); |
| 286 | /* enable low-power pll */ |
| 287 | lppllctl |= TWL6040_LPLLENA; |
| 288 | twl6040_write(codec, TWL6040_REG_LPPLLCTL, lppllctl); |
| 289 | /* reset state machine */ |
| 290 | accctl |= TWL6040_RESETSPLIT; |
| 291 | twl6040_write(codec, TWL6040_REG_ACCCTL, accctl); |
| 292 | mdelay(5); |
| 293 | accctl &= ~TWL6040_RESETSPLIT; |
| 294 | twl6040_write(codec, TWL6040_REG_ACCCTL, accctl); |
| 295 | /* disable internal oscillator */ |
| 296 | ldoctl &= ~TWL6040_OSCENA; |
| 297 | twl6040_write(codec, TWL6040_REG_LDOCTL, ldoctl); |
| 298 | } |
| 299 | |
| 300 | /* twl6040 codec manual power-down sequence */ |
| 301 | static void twl6040_power_down(struct snd_soc_codec *codec) |
| 302 | { |
| 303 | u8 ncpctl, ldoctl, lppllctl, accctl; |
| 304 | |
| 305 | ncpctl = twl6040_read_reg_cache(codec, TWL6040_REG_NCPCTL); |
| 306 | ldoctl = twl6040_read_reg_cache(codec, TWL6040_REG_LDOCTL); |
| 307 | lppllctl = twl6040_read_reg_cache(codec, TWL6040_REG_LPPLLCTL); |
| 308 | accctl = twl6040_read_reg_cache(codec, TWL6040_REG_ACCCTL); |
| 309 | |
| 310 | /* enable internal oscillator */ |
| 311 | ldoctl |= TWL6040_OSCENA; |
| 312 | twl6040_write(codec, TWL6040_REG_LDOCTL, ldoctl); |
| 313 | udelay(10); |
| 314 | /* disable low-power pll */ |
| 315 | lppllctl &= ~TWL6040_LPLLENA; |
| 316 | twl6040_write(codec, TWL6040_REG_LPPLLCTL, lppllctl); |
| 317 | /* disable low-side ldo */ |
| 318 | ldoctl &= ~TWL6040_LSLDOENA; |
| 319 | twl6040_write(codec, TWL6040_REG_LDOCTL, ldoctl); |
| 320 | udelay(244); |
| 321 | /* disable negative charge pump */ |
| 322 | ncpctl &= ~(TWL6040_NCPENA | TWL6040_NCPOPEN); |
| 323 | twl6040_write(codec, TWL6040_REG_NCPCTL, ncpctl); |
| 324 | udelay(488); |
| 325 | /* disable high-side ldo */ |
| 326 | ldoctl &= ~TWL6040_HSLDOENA; |
| 327 | twl6040_write(codec, TWL6040_REG_LDOCTL, ldoctl); |
| 328 | udelay(244); |
| 329 | /* disable internal oscillator */ |
| 330 | ldoctl &= ~TWL6040_OSCENA; |
| 331 | twl6040_write(codec, TWL6040_REG_LDOCTL, ldoctl); |
| 332 | /* disable reference system */ |
| 333 | ldoctl &= ~TWL6040_REFENA; |
| 334 | twl6040_write(codec, TWL6040_REG_LDOCTL, ldoctl); |
| 335 | msleep(10); |
| 336 | } |
| 337 | |
| 338 | /* set headset dac and driver power mode */ |
| 339 | static int headset_power_mode(struct snd_soc_codec *codec, int high_perf) |
| 340 | { |
| 341 | int hslctl, hsrctl; |
| 342 | int mask = TWL6040_HSDRVMODEL | TWL6040_HSDACMODEL; |
| 343 | |
| 344 | hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL); |
| 345 | hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL); |
| 346 | |
| 347 | if (high_perf) { |
| 348 | hslctl &= ~mask; |
| 349 | hsrctl &= ~mask; |
| 350 | } else { |
| 351 | hslctl |= mask; |
| 352 | hsrctl |= mask; |
| 353 | } |
| 354 | |
| 355 | twl6040_write(codec, TWL6040_REG_HSLCTL, hslctl); |
| 356 | twl6040_write(codec, TWL6040_REG_HSRCTL, hsrctl); |
| 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
Jorge Eduardo Candelaria | 0fad4ed | 2010-07-15 11:38:01 -0500 | [diff] [blame] | 361 | static int twl6040_hs_dac_event(struct snd_soc_dapm_widget *w, |
| 362 | struct snd_kcontrol *kcontrol, int event) |
| 363 | { |
| 364 | msleep(1); |
| 365 | return 0; |
| 366 | } |
| 367 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 368 | static int twl6040_power_mode_event(struct snd_soc_dapm_widget *w, |
| 369 | struct snd_kcontrol *kcontrol, int event) |
| 370 | { |
| 371 | struct snd_soc_codec *codec = w->codec; |
Takashi Iwai | d4a8ca2 | 2010-04-20 08:20:31 +0200 | [diff] [blame] | 372 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 373 | |
| 374 | if (SND_SOC_DAPM_EVENT_ON(event)) |
| 375 | priv->non_lp++; |
| 376 | else |
| 377 | priv->non_lp--; |
| 378 | |
Jorge Eduardo Candelaria | 0fad4ed | 2010-07-15 11:38:01 -0500 | [diff] [blame] | 379 | msleep(1); |
| 380 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | /* audio interrupt handler */ |
| 385 | static irqreturn_t twl6040_naudint_handler(int irq, void *data) |
| 386 | { |
| 387 | struct snd_soc_codec *codec = data; |
Takashi Iwai | d4a8ca2 | 2010-04-20 08:20:31 +0200 | [diff] [blame] | 388 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 389 | u8 intid; |
| 390 | |
| 391 | twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &intid, TWL6040_REG_INTID); |
| 392 | |
| 393 | switch (intid) { |
| 394 | case TWL6040_THINT: |
| 395 | dev_alert(codec->dev, "die temp over-limit detection\n"); |
| 396 | break; |
| 397 | case TWL6040_PLUGINT: |
| 398 | case TWL6040_UNPLUGINT: |
| 399 | case TWL6040_HOOKINT: |
| 400 | break; |
| 401 | case TWL6040_HFINT: |
| 402 | dev_alert(codec->dev, "hf drivers over current detection\n"); |
| 403 | break; |
| 404 | case TWL6040_VIBINT: |
| 405 | dev_alert(codec->dev, "vib drivers over current detection\n"); |
| 406 | break; |
| 407 | case TWL6040_READYINT: |
| 408 | complete(&priv->ready); |
| 409 | break; |
| 410 | default: |
| 411 | dev_err(codec->dev, "unknown audio interrupt %d\n", intid); |
| 412 | break; |
| 413 | } |
| 414 | |
| 415 | return IRQ_HANDLED; |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | * MICATT volume control: |
| 420 | * from -6 to 0 dB in 6 dB steps |
| 421 | */ |
| 422 | static DECLARE_TLV_DB_SCALE(mic_preamp_tlv, -600, 600, 0); |
| 423 | |
| 424 | /* |
| 425 | * MICGAIN volume control: |
| 426 | * from 6 to 30 dB in 6 dB steps |
| 427 | */ |
| 428 | static DECLARE_TLV_DB_SCALE(mic_amp_tlv, 600, 600, 0); |
| 429 | |
| 430 | /* |
| 431 | * HSGAIN volume control: |
| 432 | * from -30 to 0 dB in 2 dB steps |
| 433 | */ |
| 434 | static DECLARE_TLV_DB_SCALE(hs_tlv, -3000, 200, 0); |
| 435 | |
| 436 | /* |
| 437 | * HFGAIN volume control: |
| 438 | * from -52 to 6 dB in 2 dB steps |
| 439 | */ |
| 440 | static DECLARE_TLV_DB_SCALE(hf_tlv, -5200, 200, 0); |
| 441 | |
Jorge Eduardo Candelaria | 871a05a | 2010-05-18 12:44:18 -0500 | [diff] [blame] | 442 | /* |
| 443 | * EPGAIN volume control: |
| 444 | * from -24 to 6 dB in 2 dB steps |
| 445 | */ |
| 446 | static DECLARE_TLV_DB_SCALE(ep_tlv, -2400, 200, 0); |
| 447 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 448 | /* Left analog microphone selection */ |
| 449 | static const char *twl6040_amicl_texts[] = |
| 450 | {"Headset Mic", "Main Mic", "Aux/FM Left", "Off"}; |
| 451 | |
| 452 | /* Right analog microphone selection */ |
| 453 | static const char *twl6040_amicr_texts[] = |
| 454 | {"Headset Mic", "Sub Mic", "Aux/FM Right", "Off"}; |
| 455 | |
| 456 | static const struct soc_enum twl6040_enum[] = { |
| 457 | SOC_ENUM_SINGLE(TWL6040_REG_MICLCTL, 3, 3, twl6040_amicl_texts), |
| 458 | SOC_ENUM_SINGLE(TWL6040_REG_MICRCTL, 3, 3, twl6040_amicr_texts), |
| 459 | }; |
| 460 | |
| 461 | static const struct snd_kcontrol_new amicl_control = |
| 462 | SOC_DAPM_ENUM("Route", twl6040_enum[0]); |
| 463 | |
| 464 | static const struct snd_kcontrol_new amicr_control = |
| 465 | SOC_DAPM_ENUM("Route", twl6040_enum[1]); |
| 466 | |
| 467 | /* Headset DAC playback switches */ |
| 468 | static const struct snd_kcontrol_new hsdacl_switch_controls = |
| 469 | SOC_DAPM_SINGLE("Switch", TWL6040_REG_HSLCTL, 5, 1, 0); |
| 470 | |
| 471 | static const struct snd_kcontrol_new hsdacr_switch_controls = |
| 472 | SOC_DAPM_SINGLE("Switch", TWL6040_REG_HSRCTL, 5, 1, 0); |
| 473 | |
| 474 | /* Handsfree DAC playback switches */ |
| 475 | static const struct snd_kcontrol_new hfdacl_switch_controls = |
| 476 | SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFLCTL, 2, 1, 0); |
| 477 | |
| 478 | static const struct snd_kcontrol_new hfdacr_switch_controls = |
| 479 | SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFRCTL, 2, 1, 0); |
| 480 | |
Jorge Eduardo Candelaria | 871a05a | 2010-05-18 12:44:18 -0500 | [diff] [blame] | 481 | static const struct snd_kcontrol_new ep_driver_switch_controls = |
| 482 | SOC_DAPM_SINGLE("Switch", TWL6040_REG_EARCTL, 0, 1, 0); |
| 483 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 484 | static const struct snd_kcontrol_new twl6040_snd_controls[] = { |
| 485 | /* Capture gains */ |
| 486 | SOC_DOUBLE_TLV("Capture Preamplifier Volume", |
| 487 | TWL6040_REG_MICGAIN, 6, 7, 1, 1, mic_preamp_tlv), |
| 488 | SOC_DOUBLE_TLV("Capture Volume", |
| 489 | TWL6040_REG_MICGAIN, 0, 3, 4, 0, mic_amp_tlv), |
| 490 | |
| 491 | /* Playback gains */ |
| 492 | SOC_DOUBLE_TLV("Headset Playback Volume", |
| 493 | TWL6040_REG_HSGAIN, 0, 4, 0xF, 1, hs_tlv), |
| 494 | SOC_DOUBLE_R_TLV("Handsfree Playback Volume", |
| 495 | TWL6040_REG_HFLGAIN, TWL6040_REG_HFRGAIN, 0, 0x1D, 1, hf_tlv), |
Jorge Eduardo Candelaria | 871a05a | 2010-05-18 12:44:18 -0500 | [diff] [blame] | 496 | SOC_SINGLE_TLV("Earphone Playback Volume", |
| 497 | TWL6040_REG_EARCTL, 1, 0xF, 1, ep_tlv), |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 498 | }; |
| 499 | |
| 500 | static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = { |
| 501 | /* Inputs */ |
| 502 | SND_SOC_DAPM_INPUT("MAINMIC"), |
| 503 | SND_SOC_DAPM_INPUT("HSMIC"), |
| 504 | SND_SOC_DAPM_INPUT("SUBMIC"), |
| 505 | SND_SOC_DAPM_INPUT("AFML"), |
| 506 | SND_SOC_DAPM_INPUT("AFMR"), |
| 507 | |
| 508 | /* Outputs */ |
| 509 | SND_SOC_DAPM_OUTPUT("HSOL"), |
| 510 | SND_SOC_DAPM_OUTPUT("HSOR"), |
| 511 | SND_SOC_DAPM_OUTPUT("HFL"), |
| 512 | SND_SOC_DAPM_OUTPUT("HFR"), |
Jorge Eduardo Candelaria | 871a05a | 2010-05-18 12:44:18 -0500 | [diff] [blame] | 513 | SND_SOC_DAPM_OUTPUT("EP"), |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 514 | |
| 515 | /* Analog input muxes for the capture amplifiers */ |
| 516 | SND_SOC_DAPM_MUX("Analog Left Capture Route", |
| 517 | SND_SOC_NOPM, 0, 0, &amicl_control), |
| 518 | SND_SOC_DAPM_MUX("Analog Right Capture Route", |
| 519 | SND_SOC_NOPM, 0, 0, &amicr_control), |
| 520 | |
| 521 | /* Analog capture PGAs */ |
| 522 | SND_SOC_DAPM_PGA("MicAmpL", |
| 523 | TWL6040_REG_MICLCTL, 0, 0, NULL, 0), |
| 524 | SND_SOC_DAPM_PGA("MicAmpR", |
| 525 | TWL6040_REG_MICRCTL, 0, 0, NULL, 0), |
| 526 | |
| 527 | /* ADCs */ |
| 528 | SND_SOC_DAPM_ADC("ADC Left", "Left Front Capture", |
| 529 | TWL6040_REG_MICLCTL, 2, 0), |
| 530 | SND_SOC_DAPM_ADC("ADC Right", "Right Front Capture", |
| 531 | TWL6040_REG_MICRCTL, 2, 0), |
| 532 | |
| 533 | /* Microphone bias */ |
| 534 | SND_SOC_DAPM_MICBIAS("Headset Mic Bias", |
| 535 | TWL6040_REG_AMICBCTL, 0, 0), |
| 536 | SND_SOC_DAPM_MICBIAS("Main Mic Bias", |
| 537 | TWL6040_REG_AMICBCTL, 4, 0), |
| 538 | SND_SOC_DAPM_MICBIAS("Digital Mic1 Bias", |
| 539 | TWL6040_REG_DMICBCTL, 0, 0), |
| 540 | SND_SOC_DAPM_MICBIAS("Digital Mic2 Bias", |
| 541 | TWL6040_REG_DMICBCTL, 4, 0), |
| 542 | |
| 543 | /* DACs */ |
Jorge Eduardo Candelaria | 0fad4ed | 2010-07-15 11:38:01 -0500 | [diff] [blame] | 544 | SND_SOC_DAPM_DAC_E("HSDAC Left", "Headset Playback", |
| 545 | TWL6040_REG_HSLCTL, 0, 0, |
| 546 | twl6040_hs_dac_event, |
| 547 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), |
| 548 | SND_SOC_DAPM_DAC_E("HSDAC Right", "Headset Playback", |
| 549 | TWL6040_REG_HSRCTL, 0, 0, |
| 550 | twl6040_hs_dac_event, |
| 551 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 552 | SND_SOC_DAPM_DAC_E("HFDAC Left", "Handsfree Playback", |
| 553 | TWL6040_REG_HFLCTL, 0, 0, |
| 554 | twl6040_power_mode_event, |
| 555 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), |
| 556 | SND_SOC_DAPM_DAC_E("HFDAC Right", "Handsfree Playback", |
| 557 | TWL6040_REG_HFRCTL, 0, 0, |
| 558 | twl6040_power_mode_event, |
| 559 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), |
| 560 | |
| 561 | /* Analog playback switches */ |
| 562 | SND_SOC_DAPM_SWITCH("HSDAC Left Playback", |
| 563 | SND_SOC_NOPM, 0, 0, &hsdacl_switch_controls), |
| 564 | SND_SOC_DAPM_SWITCH("HSDAC Right Playback", |
| 565 | SND_SOC_NOPM, 0, 0, &hsdacr_switch_controls), |
| 566 | SND_SOC_DAPM_SWITCH("HFDAC Left Playback", |
| 567 | SND_SOC_NOPM, 0, 0, &hfdacl_switch_controls), |
| 568 | SND_SOC_DAPM_SWITCH("HFDAC Right Playback", |
| 569 | SND_SOC_NOPM, 0, 0, &hfdacr_switch_controls), |
| 570 | |
Jorge Eduardo Candelaria | 0fad4ed | 2010-07-15 11:38:01 -0500 | [diff] [blame] | 571 | /* Analog playback drivers */ |
| 572 | SND_SOC_DAPM_PGA_E("Handsfree Left Driver", |
| 573 | TWL6040_REG_HFLCTL, 4, 0, NULL, 0, |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 574 | twl6040_power_mode_event, |
| 575 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), |
Jorge Eduardo Candelaria | 0fad4ed | 2010-07-15 11:38:01 -0500 | [diff] [blame] | 576 | SND_SOC_DAPM_PGA_E("Handsfree Right Driver", |
| 577 | TWL6040_REG_HFRCTL, 4, 0, NULL, 0, |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 578 | twl6040_power_mode_event, |
| 579 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), |
Jorge Eduardo Candelaria | 0fad4ed | 2010-07-15 11:38:01 -0500 | [diff] [blame] | 580 | SND_SOC_DAPM_PGA("Headset Left Driver", |
| 581 | TWL6040_REG_HSLCTL, 2, 0, NULL, 0), |
| 582 | SND_SOC_DAPM_PGA("Headset Right Driver", |
| 583 | TWL6040_REG_HSRCTL, 2, 0, NULL, 0), |
Jorge Eduardo Candelaria | 871a05a | 2010-05-18 12:44:18 -0500 | [diff] [blame] | 584 | SND_SOC_DAPM_SWITCH_E("Earphone Driver", |
| 585 | SND_SOC_NOPM, 0, 0, &ep_driver_switch_controls, |
| 586 | twl6040_power_mode_event, |
| 587 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 588 | |
| 589 | /* Analog playback PGAs */ |
| 590 | SND_SOC_DAPM_PGA("HFDAC Left PGA", |
| 591 | TWL6040_REG_HFLCTL, 1, 0, NULL, 0), |
| 592 | SND_SOC_DAPM_PGA("HFDAC Right PGA", |
| 593 | TWL6040_REG_HFRCTL, 1, 0, NULL, 0), |
| 594 | |
| 595 | }; |
| 596 | |
| 597 | static const struct snd_soc_dapm_route intercon[] = { |
| 598 | /* Capture path */ |
| 599 | {"Analog Left Capture Route", "Headset Mic", "HSMIC"}, |
| 600 | {"Analog Left Capture Route", "Main Mic", "MAINMIC"}, |
| 601 | {"Analog Left Capture Route", "Aux/FM Left", "AFML"}, |
| 602 | |
| 603 | {"Analog Right Capture Route", "Headset Mic", "HSMIC"}, |
| 604 | {"Analog Right Capture Route", "Sub Mic", "SUBMIC"}, |
| 605 | {"Analog Right Capture Route", "Aux/FM Right", "AFMR"}, |
| 606 | |
| 607 | {"MicAmpL", NULL, "Analog Left Capture Route"}, |
| 608 | {"MicAmpR", NULL, "Analog Right Capture Route"}, |
| 609 | |
| 610 | {"ADC Left", NULL, "MicAmpL"}, |
| 611 | {"ADC Right", NULL, "MicAmpR"}, |
| 612 | |
| 613 | /* Headset playback path */ |
| 614 | {"HSDAC Left Playback", "Switch", "HSDAC Left"}, |
| 615 | {"HSDAC Right Playback", "Switch", "HSDAC Right"}, |
| 616 | |
Jorge Eduardo Candelaria | 0fad4ed | 2010-07-15 11:38:01 -0500 | [diff] [blame] | 617 | {"Headset Left Driver", NULL, "HSDAC Left Playback"}, |
| 618 | {"Headset Right Driver", NULL, "HSDAC Right Playback"}, |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 619 | |
| 620 | {"HSOL", NULL, "Headset Left Driver"}, |
| 621 | {"HSOR", NULL, "Headset Right Driver"}, |
| 622 | |
Jorge Eduardo Candelaria | 871a05a | 2010-05-18 12:44:18 -0500 | [diff] [blame] | 623 | /* Earphone playback path */ |
| 624 | {"Earphone Driver", "Switch", "HSDAC Left"}, |
| 625 | {"EP", NULL, "Earphone Driver"}, |
| 626 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 627 | /* Handsfree playback path */ |
| 628 | {"HFDAC Left Playback", "Switch", "HFDAC Left"}, |
| 629 | {"HFDAC Right Playback", "Switch", "HFDAC Right"}, |
| 630 | |
| 631 | {"HFDAC Left PGA", NULL, "HFDAC Left Playback"}, |
| 632 | {"HFDAC Right PGA", NULL, "HFDAC Right Playback"}, |
| 633 | |
| 634 | {"Handsfree Left Driver", "Switch", "HFDAC Left PGA"}, |
| 635 | {"Handsfree Right Driver", "Switch", "HFDAC Right PGA"}, |
| 636 | |
| 637 | {"HFL", NULL, "Handsfree Left Driver"}, |
| 638 | {"HFR", NULL, "Handsfree Right Driver"}, |
| 639 | }; |
| 640 | |
| 641 | static int twl6040_add_widgets(struct snd_soc_codec *codec) |
| 642 | { |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 643 | struct snd_soc_dapm_context *dapm = &codec->dapm; |
| 644 | |
| 645 | snd_soc_dapm_new_controls(dapm, twl6040_dapm_widgets, |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 646 | ARRAY_SIZE(twl6040_dapm_widgets)); |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 647 | snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon)); |
| 648 | snd_soc_dapm_new_widgets(dapm); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 649 | |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | static int twl6040_power_up_completion(struct snd_soc_codec *codec, |
| 654 | int naudint) |
| 655 | { |
Takashi Iwai | d4a8ca2 | 2010-04-20 08:20:31 +0200 | [diff] [blame] | 656 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 657 | int time_left; |
| 658 | u8 intid; |
| 659 | |
| 660 | time_left = wait_for_completion_timeout(&priv->ready, |
| 661 | msecs_to_jiffies(48)); |
| 662 | |
| 663 | if (!time_left) { |
| 664 | twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &intid, |
| 665 | TWL6040_REG_INTID); |
| 666 | if (!(intid & TWL6040_READYINT)) { |
| 667 | dev_err(codec->dev, "timeout waiting for READYINT\n"); |
| 668 | return -ETIMEDOUT; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | priv->codec_powered = 1; |
| 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | static int twl6040_set_bias_level(struct snd_soc_codec *codec, |
| 678 | enum snd_soc_bias_level level) |
| 679 | { |
Takashi Iwai | d4a8ca2 | 2010-04-20 08:20:31 +0200 | [diff] [blame] | 680 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 681 | int audpwron = priv->audpwron; |
| 682 | int naudint = priv->naudint; |
| 683 | int ret; |
| 684 | |
| 685 | switch (level) { |
| 686 | case SND_SOC_BIAS_ON: |
| 687 | break; |
| 688 | case SND_SOC_BIAS_PREPARE: |
| 689 | break; |
| 690 | case SND_SOC_BIAS_STANDBY: |
| 691 | if (priv->codec_powered) |
| 692 | break; |
| 693 | |
| 694 | if (gpio_is_valid(audpwron)) { |
| 695 | /* use AUDPWRON line */ |
| 696 | gpio_set_value(audpwron, 1); |
| 697 | |
| 698 | /* wait for power-up completion */ |
| 699 | ret = twl6040_power_up_completion(codec, naudint); |
| 700 | if (ret) |
| 701 | return ret; |
| 702 | |
| 703 | /* sync registers updated during power-up sequence */ |
| 704 | twl6040_read_reg_volatile(codec, TWL6040_REG_NCPCTL); |
| 705 | twl6040_read_reg_volatile(codec, TWL6040_REG_LDOCTL); |
| 706 | twl6040_read_reg_volatile(codec, TWL6040_REG_LPPLLCTL); |
| 707 | } else { |
| 708 | /* use manual power-up sequence */ |
| 709 | twl6040_power_up(codec); |
| 710 | priv->codec_powered = 1; |
| 711 | } |
| 712 | |
| 713 | /* initialize vdd/vss registers with reg_cache */ |
| 714 | twl6040_init_vdd_regs(codec); |
| 715 | break; |
| 716 | case SND_SOC_BIAS_OFF: |
| 717 | if (!priv->codec_powered) |
| 718 | break; |
| 719 | |
| 720 | if (gpio_is_valid(audpwron)) { |
| 721 | /* use AUDPWRON line */ |
| 722 | gpio_set_value(audpwron, 0); |
| 723 | |
| 724 | /* power-down sequence latency */ |
| 725 | udelay(500); |
| 726 | |
| 727 | /* sync registers updated during power-down sequence */ |
| 728 | twl6040_read_reg_volatile(codec, TWL6040_REG_NCPCTL); |
| 729 | twl6040_read_reg_volatile(codec, TWL6040_REG_LDOCTL); |
| 730 | twl6040_write_reg_cache(codec, TWL6040_REG_LPPLLCTL, |
| 731 | 0x00); |
| 732 | } else { |
| 733 | /* use manual power-down sequence */ |
| 734 | twl6040_power_down(codec); |
| 735 | } |
| 736 | |
| 737 | priv->codec_powered = 0; |
| 738 | break; |
| 739 | } |
| 740 | |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 741 | codec->dapm.bias_level = level; |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 742 | |
| 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | /* set of rates for each pll: low-power and high-performance */ |
| 747 | |
| 748 | static unsigned int lp_rates[] = { |
| 749 | 88200, |
| 750 | 96000, |
| 751 | }; |
| 752 | |
| 753 | static struct snd_pcm_hw_constraint_list lp_constraints = { |
| 754 | .count = ARRAY_SIZE(lp_rates), |
| 755 | .list = lp_rates, |
| 756 | }; |
| 757 | |
| 758 | static unsigned int hp_rates[] = { |
| 759 | 96000, |
| 760 | }; |
| 761 | |
| 762 | static struct snd_pcm_hw_constraint_list hp_constraints = { |
| 763 | .count = ARRAY_SIZE(hp_rates), |
| 764 | .list = hp_rates, |
| 765 | }; |
| 766 | |
| 767 | static int twl6040_startup(struct snd_pcm_substream *substream, |
| 768 | struct snd_soc_dai *dai) |
| 769 | { |
| 770 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 771 | struct snd_soc_codec *codec = rtd->codec; |
Takashi Iwai | d4a8ca2 | 2010-04-20 08:20:31 +0200 | [diff] [blame] | 772 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 773 | |
| 774 | if (!priv->sysclk) { |
| 775 | dev_err(codec->dev, |
| 776 | "no mclk configured, call set_sysclk() on init\n"); |
| 777 | return -EINVAL; |
| 778 | } |
| 779 | |
| 780 | /* |
| 781 | * capture is not supported at 17.64 MHz, |
| 782 | * it's reserved for headset low-power playback scenario |
| 783 | */ |
| 784 | if ((priv->sysclk == 17640000) && substream->stream) { |
| 785 | dev_err(codec->dev, |
| 786 | "capture mode is not supported at %dHz\n", |
| 787 | priv->sysclk); |
| 788 | return -EINVAL; |
| 789 | } |
| 790 | |
| 791 | snd_pcm_hw_constraint_list(substream->runtime, 0, |
| 792 | SNDRV_PCM_HW_PARAM_RATE, |
| 793 | priv->sysclk_constraints); |
| 794 | |
| 795 | return 0; |
| 796 | } |
| 797 | |
| 798 | static int twl6040_hw_params(struct snd_pcm_substream *substream, |
| 799 | struct snd_pcm_hw_params *params, |
| 800 | struct snd_soc_dai *dai) |
| 801 | { |
| 802 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 803 | struct snd_soc_codec *codec = rtd->codec; |
Takashi Iwai | d4a8ca2 | 2010-04-20 08:20:31 +0200 | [diff] [blame] | 804 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 805 | u8 lppllctl; |
| 806 | int rate; |
| 807 | |
| 808 | /* nothing to do for high-perf pll, it supports only 48 kHz */ |
| 809 | if (priv->pll == TWL6040_HPPLL_ID) |
| 810 | return 0; |
| 811 | |
| 812 | lppllctl = twl6040_read_reg_cache(codec, TWL6040_REG_LPPLLCTL); |
| 813 | |
| 814 | rate = params_rate(params); |
| 815 | switch (rate) { |
| 816 | case 88200: |
| 817 | lppllctl |= TWL6040_LPLLFIN; |
| 818 | priv->sysclk = 17640000; |
| 819 | break; |
| 820 | case 96000: |
| 821 | lppllctl &= ~TWL6040_LPLLFIN; |
| 822 | priv->sysclk = 19200000; |
| 823 | break; |
| 824 | default: |
| 825 | dev_err(codec->dev, "unsupported rate %d\n", rate); |
| 826 | return -EINVAL; |
| 827 | } |
| 828 | |
| 829 | twl6040_write(codec, TWL6040_REG_LPPLLCTL, lppllctl); |
| 830 | |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | static int twl6040_trigger(struct snd_pcm_substream *substream, |
| 835 | int cmd, struct snd_soc_dai *dai) |
| 836 | { |
| 837 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 838 | struct snd_soc_codec *codec = rtd->codec; |
Takashi Iwai | d4a8ca2 | 2010-04-20 08:20:31 +0200 | [diff] [blame] | 839 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 840 | |
| 841 | switch (cmd) { |
| 842 | case SNDRV_PCM_TRIGGER_START: |
| 843 | case SNDRV_PCM_TRIGGER_RESUME: |
| 844 | /* |
| 845 | * low-power playback mode is restricted |
| 846 | * for headset path only |
| 847 | */ |
| 848 | if ((priv->sysclk == 17640000) && priv->non_lp) { |
| 849 | dev_err(codec->dev, |
| 850 | "some enabled paths aren't supported at %dHz\n", |
| 851 | priv->sysclk); |
| 852 | return -EPERM; |
| 853 | } |
| 854 | break; |
| 855 | default: |
| 856 | break; |
| 857 | } |
| 858 | |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | static int twl6040_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
| 863 | int clk_id, unsigned int freq, int dir) |
| 864 | { |
| 865 | struct snd_soc_codec *codec = codec_dai->codec; |
Takashi Iwai | d4a8ca2 | 2010-04-20 08:20:31 +0200 | [diff] [blame] | 866 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 867 | u8 hppllctl, lppllctl; |
| 868 | |
| 869 | hppllctl = twl6040_read_reg_cache(codec, TWL6040_REG_HPPLLCTL); |
| 870 | lppllctl = twl6040_read_reg_cache(codec, TWL6040_REG_LPPLLCTL); |
| 871 | |
| 872 | switch (clk_id) { |
| 873 | case TWL6040_SYSCLK_SEL_LPPLL: |
| 874 | switch (freq) { |
| 875 | case 32768: |
| 876 | /* headset dac and driver must be in low-power mode */ |
| 877 | headset_power_mode(codec, 0); |
| 878 | |
| 879 | /* clk32k input requires low-power pll */ |
| 880 | lppllctl |= TWL6040_LPLLENA; |
| 881 | twl6040_write(codec, TWL6040_REG_LPPLLCTL, lppllctl); |
| 882 | mdelay(5); |
| 883 | lppllctl &= ~TWL6040_HPLLSEL; |
| 884 | twl6040_write(codec, TWL6040_REG_LPPLLCTL, lppllctl); |
| 885 | hppllctl &= ~TWL6040_HPLLENA; |
| 886 | twl6040_write(codec, TWL6040_REG_HPPLLCTL, hppllctl); |
| 887 | break; |
| 888 | default: |
| 889 | dev_err(codec->dev, "unknown mclk freq %d\n", freq); |
| 890 | return -EINVAL; |
| 891 | } |
| 892 | |
| 893 | /* lppll divider */ |
| 894 | switch (priv->sysclk) { |
| 895 | case 17640000: |
| 896 | lppllctl |= TWL6040_LPLLFIN; |
| 897 | break; |
| 898 | case 19200000: |
| 899 | lppllctl &= ~TWL6040_LPLLFIN; |
| 900 | break; |
| 901 | default: |
| 902 | /* sysclk not yet configured */ |
| 903 | lppllctl &= ~TWL6040_LPLLFIN; |
| 904 | priv->sysclk = 19200000; |
| 905 | break; |
| 906 | } |
| 907 | |
| 908 | twl6040_write(codec, TWL6040_REG_LPPLLCTL, lppllctl); |
| 909 | |
| 910 | priv->pll = TWL6040_LPPLL_ID; |
| 911 | priv->sysclk_constraints = &lp_constraints; |
| 912 | break; |
| 913 | case TWL6040_SYSCLK_SEL_HPPLL: |
| 914 | hppllctl &= ~TWL6040_MCLK_MSK; |
| 915 | |
| 916 | switch (freq) { |
| 917 | case 12000000: |
| 918 | /* mclk input, pll enabled */ |
| 919 | hppllctl |= TWL6040_MCLK_12000KHZ | |
| 920 | TWL6040_HPLLSQRBP | |
| 921 | TWL6040_HPLLENA; |
| 922 | break; |
| 923 | case 19200000: |
| 924 | /* mclk input, pll disabled */ |
| 925 | hppllctl |= TWL6040_MCLK_19200KHZ | |
Jorge Eduardo Candelaria | 44ebaa5 | 2010-05-20 17:53:07 -0500 | [diff] [blame] | 926 | TWL6040_HPLLSQRENA | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 927 | TWL6040_HPLLBP; |
| 928 | break; |
| 929 | case 26000000: |
| 930 | /* mclk input, pll enabled */ |
| 931 | hppllctl |= TWL6040_MCLK_26000KHZ | |
| 932 | TWL6040_HPLLSQRBP | |
| 933 | TWL6040_HPLLENA; |
| 934 | break; |
| 935 | case 38400000: |
| 936 | /* clk slicer, pll disabled */ |
| 937 | hppllctl |= TWL6040_MCLK_38400KHZ | |
| 938 | TWL6040_HPLLSQRENA | |
| 939 | TWL6040_HPLLBP; |
| 940 | break; |
| 941 | default: |
| 942 | dev_err(codec->dev, "unknown mclk freq %d\n", freq); |
| 943 | return -EINVAL; |
| 944 | } |
| 945 | |
| 946 | /* headset dac and driver must be in high-performance mode */ |
| 947 | headset_power_mode(codec, 1); |
| 948 | |
| 949 | twl6040_write(codec, TWL6040_REG_HPPLLCTL, hppllctl); |
| 950 | udelay(500); |
| 951 | lppllctl |= TWL6040_HPLLSEL; |
| 952 | twl6040_write(codec, TWL6040_REG_LPPLLCTL, lppllctl); |
| 953 | lppllctl &= ~TWL6040_LPLLENA; |
| 954 | twl6040_write(codec, TWL6040_REG_LPPLLCTL, lppllctl); |
| 955 | |
| 956 | /* high-performance pll can provide only 19.2 MHz */ |
| 957 | priv->pll = TWL6040_HPPLL_ID; |
| 958 | priv->sysclk = 19200000; |
| 959 | priv->sysclk_constraints = &hp_constraints; |
| 960 | break; |
| 961 | default: |
| 962 | dev_err(codec->dev, "unknown clk_id %d\n", clk_id); |
| 963 | return -EINVAL; |
| 964 | } |
| 965 | |
| 966 | return 0; |
| 967 | } |
| 968 | |
| 969 | static struct snd_soc_dai_ops twl6040_dai_ops = { |
| 970 | .startup = twl6040_startup, |
| 971 | .hw_params = twl6040_hw_params, |
| 972 | .trigger = twl6040_trigger, |
| 973 | .set_sysclk = twl6040_set_dai_sysclk, |
| 974 | }; |
| 975 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 976 | static struct snd_soc_dai_driver twl6040_dai = { |
| 977 | .name = "twl6040-hifi", |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 978 | .playback = { |
| 979 | .stream_name = "Playback", |
| 980 | .channels_min = 1, |
| 981 | .channels_max = 4, |
| 982 | .rates = TWL6040_RATES, |
| 983 | .formats = TWL6040_FORMATS, |
| 984 | }, |
| 985 | .capture = { |
| 986 | .stream_name = "Capture", |
| 987 | .channels_min = 1, |
| 988 | .channels_max = 2, |
| 989 | .rates = TWL6040_RATES, |
| 990 | .formats = TWL6040_FORMATS, |
| 991 | }, |
| 992 | .ops = &twl6040_dai_ops, |
| 993 | }; |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 994 | |
| 995 | #ifdef CONFIG_PM |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 996 | static int twl6040_suspend(struct snd_soc_codec *codec, pm_message_t state) |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 997 | { |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 998 | twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF); |
| 999 | |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1003 | static int twl6040_resume(struct snd_soc_codec *codec) |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1004 | { |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1005 | twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1006 | |
| 1007 | return 0; |
| 1008 | } |
| 1009 | #else |
| 1010 | #define twl6040_suspend NULL |
| 1011 | #define twl6040_resume NULL |
| 1012 | #endif |
| 1013 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1014 | static int twl6040_probe(struct snd_soc_codec *codec) |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1015 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1016 | struct twl4030_codec_data *twl_codec = codec->dev->platform_data; |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1017 | struct twl6040_data *priv; |
| 1018 | int audpwron, naudint; |
| 1019 | int ret = 0; |
| 1020 | |
| 1021 | priv = kzalloc(sizeof(struct twl6040_data), GFP_KERNEL); |
| 1022 | if (priv == NULL) |
| 1023 | return -ENOMEM; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1024 | snd_soc_codec_set_drvdata(codec, priv); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1025 | |
| 1026 | if (twl_codec) { |
| 1027 | audpwron = twl_codec->audpwron_gpio; |
| 1028 | naudint = twl_codec->naudint_irq; |
| 1029 | } else { |
| 1030 | audpwron = -EINVAL; |
| 1031 | naudint = 0; |
| 1032 | } |
| 1033 | |
| 1034 | priv->audpwron = audpwron; |
| 1035 | priv->naudint = naudint; |
| 1036 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1037 | init_completion(&priv->ready); |
| 1038 | |
| 1039 | if (gpio_is_valid(audpwron)) { |
| 1040 | ret = gpio_request(audpwron, "audpwron"); |
| 1041 | if (ret) |
| 1042 | goto gpio1_err; |
| 1043 | |
| 1044 | ret = gpio_direction_output(audpwron, 0); |
| 1045 | if (ret) |
| 1046 | goto gpio2_err; |
| 1047 | |
| 1048 | priv->codec_powered = 0; |
| 1049 | } |
| 1050 | |
| 1051 | if (naudint) { |
| 1052 | /* audio interrupt */ |
| 1053 | ret = request_threaded_irq(naudint, NULL, |
| 1054 | twl6040_naudint_handler, |
| 1055 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
| 1056 | "twl6040_codec", codec); |
| 1057 | if (ret) |
| 1058 | goto gpio2_err; |
| 1059 | } else { |
| 1060 | if (gpio_is_valid(audpwron)) { |
| 1061 | /* enable only codec ready interrupt */ |
| 1062 | twl6040_write_reg_cache(codec, TWL6040_REG_INTMR, |
| 1063 | ~TWL6040_READYMSK & TWL6040_ALLINT_MSK); |
| 1064 | } else { |
| 1065 | /* no interrupts at all */ |
| 1066 | twl6040_write_reg_cache(codec, TWL6040_REG_INTMR, |
| 1067 | TWL6040_ALLINT_MSK); |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | /* init vio registers */ |
| 1072 | twl6040_init_vio_regs(codec); |
| 1073 | |
| 1074 | /* power on device */ |
| 1075 | ret = twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
| 1076 | if (ret) |
| 1077 | goto irq_err; |
| 1078 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1079 | snd_soc_add_controls(codec, twl6040_snd_controls, |
| 1080 | ARRAY_SIZE(twl6040_snd_controls)); |
| 1081 | twl6040_add_widgets(codec); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1082 | |
| 1083 | return 0; |
| 1084 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1085 | irq_err: |
| 1086 | if (naudint) |
| 1087 | free_irq(naudint, codec); |
| 1088 | gpio2_err: |
| 1089 | if (gpio_is_valid(audpwron)) |
| 1090 | gpio_free(audpwron); |
| 1091 | gpio1_err: |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1092 | kfree(priv); |
| 1093 | return ret; |
| 1094 | } |
| 1095 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1096 | static int twl6040_remove(struct snd_soc_codec *codec) |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1097 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1098 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1099 | int audpwron = priv->audpwron; |
| 1100 | int naudint = priv->naudint; |
| 1101 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1102 | twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF); |
| 1103 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1104 | if (gpio_is_valid(audpwron)) |
| 1105 | gpio_free(audpwron); |
| 1106 | |
| 1107 | if (naudint) |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1108 | free_irq(naudint, codec); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1109 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1110 | kfree(priv); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1111 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1112 | return 0; |
| 1113 | } |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1114 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1115 | static struct snd_soc_codec_driver soc_codec_dev_twl6040 = { |
| 1116 | .probe = twl6040_probe, |
| 1117 | .remove = twl6040_remove, |
| 1118 | .suspend = twl6040_suspend, |
| 1119 | .resume = twl6040_resume, |
| 1120 | .read = twl6040_read_reg_cache, |
| 1121 | .write = twl6040_write, |
| 1122 | .set_bias_level = twl6040_set_bias_level, |
| 1123 | .reg_cache_size = ARRAY_SIZE(twl6040_reg), |
| 1124 | .reg_word_size = sizeof(u8), |
| 1125 | .reg_cache_default = twl6040_reg, |
| 1126 | }; |
| 1127 | |
| 1128 | static int __devinit twl6040_codec_probe(struct platform_device *pdev) |
| 1129 | { |
| 1130 | return snd_soc_register_codec(&pdev->dev, |
| 1131 | &soc_codec_dev_twl6040, &twl6040_dai, 1); |
| 1132 | } |
| 1133 | |
| 1134 | static int __devexit twl6040_codec_remove(struct platform_device *pdev) |
| 1135 | { |
| 1136 | snd_soc_unregister_codec(&pdev->dev); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1137 | return 0; |
| 1138 | } |
| 1139 | |
| 1140 | static struct platform_driver twl6040_codec_driver = { |
| 1141 | .driver = { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1142 | .name = "twl6040-codec", |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1143 | .owner = THIS_MODULE, |
| 1144 | }, |
| 1145 | .probe = twl6040_codec_probe, |
| 1146 | .remove = __devexit_p(twl6040_codec_remove), |
| 1147 | }; |
| 1148 | |
| 1149 | static int __init twl6040_codec_init(void) |
| 1150 | { |
| 1151 | return platform_driver_register(&twl6040_codec_driver); |
| 1152 | } |
| 1153 | module_init(twl6040_codec_init); |
| 1154 | |
| 1155 | static void __exit twl6040_codec_exit(void) |
| 1156 | { |
| 1157 | platform_driver_unregister(&twl6040_codec_driver); |
| 1158 | } |
| 1159 | module_exit(twl6040_codec_exit); |
| 1160 | |
| 1161 | MODULE_DESCRIPTION("ASoC TWL6040 codec driver"); |
| 1162 | MODULE_AUTHOR("Misael Lopez Cruz"); |
| 1163 | MODULE_LICENSE("GPL"); |