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> |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 27 | #include <linux/platform_device.h> |
Stephen Rothwell | 68b40cc | 2010-03-29 17:55:51 +1100 | [diff] [blame] | 28 | #include <linux/slab.h> |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 29 | #include <linux/i2c/twl.h> |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 30 | #include <linux/mfd/twl6040.h> |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 31 | |
| 32 | #include <sound/core.h> |
| 33 | #include <sound/pcm.h> |
| 34 | #include <sound/pcm_params.h> |
| 35 | #include <sound/soc.h> |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 36 | #include <sound/initval.h> |
| 37 | #include <sound/tlv.h> |
| 38 | |
| 39 | #include "twl6040.h" |
| 40 | |
Olaya, Margarita | 60ea4ce | 2010-12-10 21:05:58 -0600 | [diff] [blame] | 41 | #define TWL6040_RATES SNDRV_PCM_RATE_8000_96000 |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 42 | #define TWL6040_FORMATS (SNDRV_PCM_FMTBIT_S32_LE) |
| 43 | |
| 44 | #define TWL6040_OUTHS_0dB 0x00 |
| 45 | #define TWL6040_OUTHS_M30dB 0x0F |
| 46 | #define TWL6040_OUTHF_0dB 0x03 |
| 47 | #define TWL6040_OUTHF_M52dB 0x1D |
| 48 | |
| 49 | #define TWL6040_RAMP_NONE 0 |
| 50 | #define TWL6040_RAMP_UP 1 |
| 51 | #define TWL6040_RAMP_DOWN 2 |
| 52 | |
| 53 | #define TWL6040_HSL_VOL_MASK 0x0F |
| 54 | #define TWL6040_HSL_VOL_SHIFT 0 |
| 55 | #define TWL6040_HSR_VOL_MASK 0xF0 |
| 56 | #define TWL6040_HSR_VOL_SHIFT 4 |
| 57 | #define TWL6040_HF_VOL_MASK 0x1F |
| 58 | #define TWL6040_HF_VOL_SHIFT 0 |
| 59 | |
| 60 | struct twl6040_output { |
| 61 | u16 active; |
| 62 | u16 left_vol; |
| 63 | u16 right_vol; |
| 64 | u16 left_step; |
| 65 | u16 right_step; |
| 66 | unsigned int step_delay; |
| 67 | u16 ramp; |
| 68 | u16 mute; |
| 69 | struct completion ramp_done; |
| 70 | }; |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 71 | |
Jorge Eduardo Candelaria | a2d2362 | 2010-12-10 20:45:17 -0600 | [diff] [blame] | 72 | struct twl6040_jack_data { |
| 73 | struct snd_soc_jack *jack; |
| 74 | int report; |
| 75 | }; |
| 76 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 77 | /* codec private data */ |
| 78 | struct twl6040_data { |
Peter Ujfalusi | 2a433b9 | 2011-07-04 19:52:26 +0300 | [diff] [blame] | 79 | int plug_irq; |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 80 | int codec_powered; |
| 81 | int pll; |
| 82 | int non_lp; |
Misael Lopez Cruz | 6bba63b | 2011-02-11 17:51:05 -0600 | [diff] [blame] | 83 | int hs_power_mode; |
| 84 | int hs_power_mode_locked; |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 85 | unsigned int clk_in; |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 86 | unsigned int sysclk; |
Axel Castaneda Gonzalez | 1fbe995 | 2011-02-23 20:08:28 -0600 | [diff] [blame] | 87 | u16 hs_left_step; |
| 88 | u16 hs_right_step; |
| 89 | u16 hf_left_step; |
| 90 | u16 hf_right_step; |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 91 | struct snd_pcm_hw_constraint_list *sysclk_constraints; |
Jorge Eduardo Candelaria | a2d2362 | 2010-12-10 20:45:17 -0600 | [diff] [blame] | 92 | struct twl6040_jack_data hs_jack; |
| 93 | struct snd_soc_codec *codec; |
| 94 | struct workqueue_struct *workqueue; |
| 95 | struct delayed_work delayed_work; |
| 96 | struct mutex mutex; |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 97 | struct twl6040_output headset; |
| 98 | struct twl6040_output handsfree; |
| 99 | struct workqueue_struct *hf_workqueue; |
| 100 | struct workqueue_struct *hs_workqueue; |
| 101 | struct delayed_work hs_delayed_work; |
| 102 | struct delayed_work hf_delayed_work; |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | /* |
| 106 | * twl6040 register cache & default register settings |
| 107 | */ |
| 108 | static const u8 twl6040_reg[TWL6040_CACHEREGNUM] = { |
| 109 | 0x00, /* not used 0x00 */ |
| 110 | 0x4B, /* TWL6040_ASICID (ro) 0x01 */ |
| 111 | 0x00, /* TWL6040_ASICREV (ro) 0x02 */ |
| 112 | 0x00, /* TWL6040_INTID 0x03 */ |
| 113 | 0x00, /* TWL6040_INTMR 0x04 */ |
| 114 | 0x00, /* TWL6040_NCPCTRL 0x05 */ |
| 115 | 0x00, /* TWL6040_LDOCTL 0x06 */ |
| 116 | 0x60, /* TWL6040_HPPLLCTL 0x07 */ |
| 117 | 0x00, /* TWL6040_LPPLLCTL 0x08 */ |
| 118 | 0x4A, /* TWL6040_LPPLLDIV 0x09 */ |
| 119 | 0x00, /* TWL6040_AMICBCTL 0x0A */ |
| 120 | 0x00, /* TWL6040_DMICBCTL 0x0B */ |
| 121 | 0x18, /* TWL6040_MICLCTL 0x0C - No input selected on Left Mic */ |
| 122 | 0x18, /* TWL6040_MICRCTL 0x0D - No input selected on Right Mic */ |
| 123 | 0x00, /* TWL6040_MICGAIN 0x0E */ |
| 124 | 0x1B, /* TWL6040_LINEGAIN 0x0F */ |
| 125 | 0x00, /* TWL6040_HSLCTL 0x10 */ |
| 126 | 0x00, /* TWL6040_HSRCTL 0x11 */ |
| 127 | 0x00, /* TWL6040_HSGAIN 0x12 */ |
| 128 | 0x00, /* TWL6040_EARCTL 0x13 */ |
| 129 | 0x00, /* TWL6040_HFLCTL 0x14 */ |
| 130 | 0x00, /* TWL6040_HFLGAIN 0x15 */ |
| 131 | 0x00, /* TWL6040_HFRCTL 0x16 */ |
| 132 | 0x00, /* TWL6040_HFRGAIN 0x17 */ |
| 133 | 0x00, /* TWL6040_VIBCTLL 0x18 */ |
| 134 | 0x00, /* TWL6040_VIBDATL 0x19 */ |
| 135 | 0x00, /* TWL6040_VIBCTLR 0x1A */ |
| 136 | 0x00, /* TWL6040_VIBDATR 0x1B */ |
| 137 | 0x00, /* TWL6040_HKCTL1 0x1C */ |
| 138 | 0x00, /* TWL6040_HKCTL2 0x1D */ |
| 139 | 0x00, /* TWL6040_GPOCTL 0x1E */ |
| 140 | 0x00, /* TWL6040_ALB 0x1F */ |
| 141 | 0x00, /* TWL6040_DLB 0x20 */ |
| 142 | 0x00, /* not used 0x21 */ |
| 143 | 0x00, /* not used 0x22 */ |
| 144 | 0x00, /* not used 0x23 */ |
| 145 | 0x00, /* not used 0x24 */ |
| 146 | 0x00, /* not used 0x25 */ |
| 147 | 0x00, /* not used 0x26 */ |
| 148 | 0x00, /* not used 0x27 */ |
| 149 | 0x00, /* TWL6040_TRIM1 0x28 */ |
| 150 | 0x00, /* TWL6040_TRIM2 0x29 */ |
| 151 | 0x00, /* TWL6040_TRIM3 0x2A */ |
| 152 | 0x00, /* TWL6040_HSOTRIM 0x2B */ |
| 153 | 0x00, /* TWL6040_HFOTRIM 0x2C */ |
| 154 | 0x09, /* TWL6040_ACCCTL 0x2D */ |
| 155 | 0x00, /* TWL6040_STATUS (ro) 0x2E */ |
| 156 | }; |
| 157 | |
| 158 | /* |
| 159 | * twl6040 vio/gnd registers: |
| 160 | * registers under vio/gnd supply can be accessed |
| 161 | * before the power-up sequence, after NRESPWRON goes high |
| 162 | */ |
| 163 | static const int twl6040_vio_reg[TWL6040_VIOREGNUM] = { |
| 164 | TWL6040_REG_ASICID, |
| 165 | TWL6040_REG_ASICREV, |
| 166 | TWL6040_REG_INTID, |
| 167 | TWL6040_REG_INTMR, |
| 168 | TWL6040_REG_NCPCTL, |
| 169 | TWL6040_REG_LDOCTL, |
| 170 | TWL6040_REG_AMICBCTL, |
| 171 | TWL6040_REG_DMICBCTL, |
| 172 | TWL6040_REG_HKCTL1, |
| 173 | TWL6040_REG_HKCTL2, |
| 174 | TWL6040_REG_GPOCTL, |
| 175 | TWL6040_REG_TRIM1, |
| 176 | TWL6040_REG_TRIM2, |
| 177 | TWL6040_REG_TRIM3, |
| 178 | TWL6040_REG_HSOTRIM, |
| 179 | TWL6040_REG_HFOTRIM, |
| 180 | TWL6040_REG_ACCCTL, |
| 181 | TWL6040_REG_STATUS, |
| 182 | }; |
| 183 | |
| 184 | /* |
| 185 | * twl6040 vdd/vss registers: |
| 186 | * registers under vdd/vss supplies can only be accessed |
| 187 | * after the power-up sequence |
| 188 | */ |
| 189 | static const int twl6040_vdd_reg[TWL6040_VDDREGNUM] = { |
| 190 | TWL6040_REG_HPPLLCTL, |
| 191 | TWL6040_REG_LPPLLCTL, |
| 192 | TWL6040_REG_LPPLLDIV, |
| 193 | TWL6040_REG_MICLCTL, |
| 194 | TWL6040_REG_MICRCTL, |
| 195 | TWL6040_REG_MICGAIN, |
| 196 | TWL6040_REG_LINEGAIN, |
| 197 | TWL6040_REG_HSLCTL, |
| 198 | TWL6040_REG_HSRCTL, |
| 199 | TWL6040_REG_HSGAIN, |
| 200 | TWL6040_REG_EARCTL, |
| 201 | TWL6040_REG_HFLCTL, |
| 202 | TWL6040_REG_HFLGAIN, |
| 203 | TWL6040_REG_HFRCTL, |
| 204 | TWL6040_REG_HFRGAIN, |
| 205 | TWL6040_REG_VIBCTLL, |
| 206 | TWL6040_REG_VIBDATL, |
| 207 | TWL6040_REG_VIBCTLR, |
| 208 | TWL6040_REG_VIBDATR, |
| 209 | TWL6040_REG_ALB, |
| 210 | TWL6040_REG_DLB, |
| 211 | }; |
| 212 | |
| 213 | /* |
| 214 | * read twl6040 register cache |
| 215 | */ |
| 216 | static inline unsigned int twl6040_read_reg_cache(struct snd_soc_codec *codec, |
| 217 | unsigned int reg) |
| 218 | { |
| 219 | u8 *cache = codec->reg_cache; |
| 220 | |
| 221 | if (reg >= TWL6040_CACHEREGNUM) |
| 222 | return -EIO; |
| 223 | |
| 224 | return cache[reg]; |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * write twl6040 register cache |
| 229 | */ |
| 230 | static inline void twl6040_write_reg_cache(struct snd_soc_codec *codec, |
| 231 | u8 reg, u8 value) |
| 232 | { |
| 233 | u8 *cache = codec->reg_cache; |
| 234 | |
| 235 | if (reg >= TWL6040_CACHEREGNUM) |
| 236 | return; |
| 237 | cache[reg] = value; |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * read from twl6040 hardware register |
| 242 | */ |
| 243 | static int twl6040_read_reg_volatile(struct snd_soc_codec *codec, |
| 244 | unsigned int reg) |
| 245 | { |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 246 | struct twl6040 *twl6040 = codec->control_data; |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 247 | u8 value; |
| 248 | |
| 249 | if (reg >= TWL6040_CACHEREGNUM) |
| 250 | return -EIO; |
| 251 | |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 252 | value = twl6040_reg_read(twl6040, reg); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 253 | twl6040_write_reg_cache(codec, reg, value); |
| 254 | |
| 255 | return value; |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * write to the twl6040 register space |
| 260 | */ |
| 261 | static int twl6040_write(struct snd_soc_codec *codec, |
| 262 | unsigned int reg, unsigned int value) |
| 263 | { |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 264 | struct twl6040 *twl6040 = codec->control_data; |
| 265 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 266 | if (reg >= TWL6040_CACHEREGNUM) |
| 267 | return -EIO; |
| 268 | |
| 269 | twl6040_write_reg_cache(codec, reg, value); |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 270 | return twl6040_reg_write(twl6040, reg, value); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | static void twl6040_init_vio_regs(struct snd_soc_codec *codec) |
| 274 | { |
| 275 | u8 *cache = codec->reg_cache; |
| 276 | int reg, i; |
| 277 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 278 | for (i = 0; i < TWL6040_VIOREGNUM; i++) { |
| 279 | reg = twl6040_vio_reg[i]; |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 280 | /* |
| 281 | * skip read-only registers (ASICID, ASICREV, STATUS) |
| 282 | * and registers shared among MFD children |
| 283 | */ |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 284 | switch (reg) { |
| 285 | case TWL6040_REG_ASICID: |
| 286 | case TWL6040_REG_ASICREV: |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 287 | case TWL6040_REG_INTID: |
| 288 | case TWL6040_REG_INTMR: |
| 289 | case TWL6040_REG_NCPCTL: |
| 290 | case TWL6040_REG_LDOCTL: |
| 291 | case TWL6040_REG_GPOCTL: |
| 292 | case TWL6040_REG_ACCCTL: |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 293 | case TWL6040_REG_STATUS: |
| 294 | continue; |
| 295 | default: |
| 296 | break; |
| 297 | } |
| 298 | twl6040_write(codec, reg, cache[reg]); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | static void twl6040_init_vdd_regs(struct snd_soc_codec *codec) |
| 303 | { |
| 304 | u8 *cache = codec->reg_cache; |
| 305 | int reg, i; |
| 306 | |
| 307 | for (i = 0; i < TWL6040_VDDREGNUM; i++) { |
| 308 | reg = twl6040_vdd_reg[i]; |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 309 | /* skip vibra and PLL registers */ |
| 310 | switch (reg) { |
| 311 | case TWL6040_REG_VIBCTLL: |
| 312 | case TWL6040_REG_VIBDATL: |
| 313 | case TWL6040_REG_VIBCTLR: |
| 314 | case TWL6040_REG_VIBDATR: |
| 315 | case TWL6040_REG_HPPLLCTL: |
| 316 | case TWL6040_REG_LPPLLCTL: |
| 317 | case TWL6040_REG_LPPLLDIV: |
| 318 | continue; |
| 319 | default: |
| 320 | break; |
| 321 | } |
| 322 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 323 | twl6040_write(codec, reg, cache[reg]); |
| 324 | } |
| 325 | } |
| 326 | |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 327 | /* |
| 328 | * Ramp HS PGA volume to minimise pops at stream startup and shutdown. |
| 329 | */ |
| 330 | static inline int twl6040_hs_ramp_step(struct snd_soc_codec *codec, |
| 331 | unsigned int left_step, unsigned int right_step) |
| 332 | { |
| 333 | |
| 334 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
| 335 | struct twl6040_output *headset = &priv->headset; |
| 336 | int left_complete = 0, right_complete = 0; |
| 337 | u8 reg, val; |
| 338 | |
| 339 | /* left channel */ |
| 340 | left_step = (left_step > 0xF) ? 0xF : left_step; |
| 341 | reg = twl6040_read_reg_cache(codec, TWL6040_REG_HSGAIN); |
| 342 | val = (~reg & TWL6040_HSL_VOL_MASK); |
| 343 | |
| 344 | if (headset->ramp == TWL6040_RAMP_UP) { |
| 345 | /* ramp step up */ |
| 346 | if (val < headset->left_vol) { |
Axel Castaneda Gonzalez | 1fbe995 | 2011-02-23 20:08:28 -0600 | [diff] [blame] | 347 | if (val + left_step > headset->left_vol) |
| 348 | val = headset->left_vol; |
| 349 | else |
| 350 | val += left_step; |
| 351 | |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 352 | reg &= ~TWL6040_HSL_VOL_MASK; |
| 353 | twl6040_write(codec, TWL6040_REG_HSGAIN, |
| 354 | (reg | (~val & TWL6040_HSL_VOL_MASK))); |
| 355 | } else { |
| 356 | left_complete = 1; |
| 357 | } |
| 358 | } else if (headset->ramp == TWL6040_RAMP_DOWN) { |
| 359 | /* ramp step down */ |
| 360 | if (val > 0x0) { |
Axel Castaneda Gonzalez | 1fbe995 | 2011-02-23 20:08:28 -0600 | [diff] [blame] | 361 | if ((int)val - (int)left_step < 0) |
| 362 | val = 0; |
| 363 | else |
| 364 | val -= left_step; |
| 365 | |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 366 | reg &= ~TWL6040_HSL_VOL_MASK; |
| 367 | twl6040_write(codec, TWL6040_REG_HSGAIN, reg | |
| 368 | (~val & TWL6040_HSL_VOL_MASK)); |
| 369 | } else { |
| 370 | left_complete = 1; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /* right channel */ |
| 375 | right_step = (right_step > 0xF) ? 0xF : right_step; |
| 376 | reg = twl6040_read_reg_cache(codec, TWL6040_REG_HSGAIN); |
| 377 | val = (~reg & TWL6040_HSR_VOL_MASK) >> TWL6040_HSR_VOL_SHIFT; |
| 378 | |
| 379 | if (headset->ramp == TWL6040_RAMP_UP) { |
| 380 | /* ramp step up */ |
| 381 | if (val < headset->right_vol) { |
Axel Castaneda Gonzalez | 1fbe995 | 2011-02-23 20:08:28 -0600 | [diff] [blame] | 382 | if (val + right_step > headset->right_vol) |
| 383 | val = headset->right_vol; |
| 384 | else |
| 385 | val += right_step; |
| 386 | |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 387 | reg &= ~TWL6040_HSR_VOL_MASK; |
| 388 | twl6040_write(codec, TWL6040_REG_HSGAIN, |
| 389 | (reg | (~val << TWL6040_HSR_VOL_SHIFT))); |
| 390 | } else { |
| 391 | right_complete = 1; |
| 392 | } |
| 393 | } else if (headset->ramp == TWL6040_RAMP_DOWN) { |
| 394 | /* ramp step down */ |
| 395 | if (val > 0x0) { |
Axel Castaneda Gonzalez | 1fbe995 | 2011-02-23 20:08:28 -0600 | [diff] [blame] | 396 | if ((int)val - (int)right_step < 0) |
| 397 | val = 0; |
| 398 | else |
| 399 | val -= right_step; |
| 400 | |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 401 | reg &= ~TWL6040_HSR_VOL_MASK; |
| 402 | twl6040_write(codec, TWL6040_REG_HSGAIN, |
| 403 | reg | (~val << TWL6040_HSR_VOL_SHIFT)); |
| 404 | } else { |
| 405 | right_complete = 1; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | return left_complete & right_complete; |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * Ramp HF PGA volume to minimise pops at stream startup and shutdown. |
| 414 | */ |
| 415 | static inline int twl6040_hf_ramp_step(struct snd_soc_codec *codec, |
| 416 | unsigned int left_step, unsigned int right_step) |
| 417 | { |
| 418 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
| 419 | struct twl6040_output *handsfree = &priv->handsfree; |
| 420 | int left_complete = 0, right_complete = 0; |
| 421 | u16 reg, val; |
| 422 | |
| 423 | /* left channel */ |
| 424 | left_step = (left_step > 0x1D) ? 0x1D : left_step; |
| 425 | reg = twl6040_read_reg_cache(codec, TWL6040_REG_HFLGAIN); |
| 426 | reg = 0x1D - reg; |
| 427 | val = (reg & TWL6040_HF_VOL_MASK); |
| 428 | if (handsfree->ramp == TWL6040_RAMP_UP) { |
| 429 | /* ramp step up */ |
| 430 | if (val < handsfree->left_vol) { |
Axel Castaneda Gonzalez | 1fbe995 | 2011-02-23 20:08:28 -0600 | [diff] [blame] | 431 | if (val + left_step > handsfree->left_vol) |
| 432 | val = handsfree->left_vol; |
| 433 | else |
| 434 | val += left_step; |
| 435 | |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 436 | reg &= ~TWL6040_HF_VOL_MASK; |
| 437 | twl6040_write(codec, TWL6040_REG_HFLGAIN, |
| 438 | reg | (0x1D - val)); |
| 439 | } else { |
| 440 | left_complete = 1; |
| 441 | } |
| 442 | } else if (handsfree->ramp == TWL6040_RAMP_DOWN) { |
| 443 | /* ramp step down */ |
| 444 | if (val > 0) { |
Axel Castaneda Gonzalez | 1fbe995 | 2011-02-23 20:08:28 -0600 | [diff] [blame] | 445 | if ((int)val - (int)left_step < 0) |
| 446 | val = 0; |
| 447 | else |
| 448 | val -= left_step; |
| 449 | |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 450 | reg &= ~TWL6040_HF_VOL_MASK; |
| 451 | twl6040_write(codec, TWL6040_REG_HFLGAIN, |
| 452 | reg | (0x1D - val)); |
| 453 | } else { |
| 454 | left_complete = 1; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | /* right channel */ |
| 459 | right_step = (right_step > 0x1D) ? 0x1D : right_step; |
| 460 | reg = twl6040_read_reg_cache(codec, TWL6040_REG_HFRGAIN); |
| 461 | reg = 0x1D - reg; |
| 462 | val = (reg & TWL6040_HF_VOL_MASK); |
| 463 | if (handsfree->ramp == TWL6040_RAMP_UP) { |
| 464 | /* ramp step up */ |
| 465 | if (val < handsfree->right_vol) { |
Axel Castaneda Gonzalez | 1fbe995 | 2011-02-23 20:08:28 -0600 | [diff] [blame] | 466 | if (val + right_step > handsfree->right_vol) |
| 467 | val = handsfree->right_vol; |
| 468 | else |
| 469 | val += right_step; |
| 470 | |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 471 | reg &= ~TWL6040_HF_VOL_MASK; |
| 472 | twl6040_write(codec, TWL6040_REG_HFRGAIN, |
| 473 | reg | (0x1D - val)); |
| 474 | } else { |
| 475 | right_complete = 1; |
| 476 | } |
| 477 | } else if (handsfree->ramp == TWL6040_RAMP_DOWN) { |
| 478 | /* ramp step down */ |
| 479 | if (val > 0) { |
Axel Castaneda Gonzalez | 1fbe995 | 2011-02-23 20:08:28 -0600 | [diff] [blame] | 480 | if ((int)val - (int)right_step < 0) |
| 481 | val = 0; |
| 482 | else |
| 483 | val -= right_step; |
| 484 | |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 485 | reg &= ~TWL6040_HF_VOL_MASK; |
| 486 | twl6040_write(codec, TWL6040_REG_HFRGAIN, |
| 487 | reg | (0x1D - val)); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | return left_complete & right_complete; |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * This work ramps both output PGAs at stream start/stop time to |
| 496 | * minimise pop associated with DAPM power switching. |
| 497 | */ |
| 498 | static void twl6040_pga_hs_work(struct work_struct *work) |
| 499 | { |
| 500 | struct twl6040_data *priv = |
| 501 | container_of(work, struct twl6040_data, hs_delayed_work.work); |
| 502 | struct snd_soc_codec *codec = priv->codec; |
| 503 | struct twl6040_output *headset = &priv->headset; |
| 504 | unsigned int delay = headset->step_delay; |
| 505 | int i, headset_complete; |
| 506 | |
| 507 | /* do we need to ramp at all ? */ |
| 508 | if (headset->ramp == TWL6040_RAMP_NONE) |
| 509 | return; |
| 510 | |
| 511 | /* HS PGA volumes have 4 bits of resolution to ramp */ |
| 512 | for (i = 0; i <= 16; i++) { |
Axel Castaneda Gonzalez | 1fbe995 | 2011-02-23 20:08:28 -0600 | [diff] [blame] | 513 | headset_complete = twl6040_hs_ramp_step(codec, |
| 514 | headset->left_step, |
| 515 | headset->right_step); |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 516 | |
| 517 | /* ramp finished ? */ |
| 518 | if (headset_complete) |
| 519 | break; |
| 520 | |
| 521 | /* |
| 522 | * TODO: tune: delay is longer over 0dB |
| 523 | * as increases are larger. |
| 524 | */ |
| 525 | if (i >= 8) |
| 526 | schedule_timeout_interruptible(msecs_to_jiffies(delay + |
| 527 | (delay >> 1))); |
| 528 | else |
| 529 | schedule_timeout_interruptible(msecs_to_jiffies(delay)); |
| 530 | } |
| 531 | |
| 532 | if (headset->ramp == TWL6040_RAMP_DOWN) { |
| 533 | headset->active = 0; |
| 534 | complete(&headset->ramp_done); |
| 535 | } else { |
| 536 | headset->active = 1; |
| 537 | } |
| 538 | headset->ramp = TWL6040_RAMP_NONE; |
| 539 | } |
| 540 | |
| 541 | static void twl6040_pga_hf_work(struct work_struct *work) |
| 542 | { |
| 543 | struct twl6040_data *priv = |
| 544 | container_of(work, struct twl6040_data, hf_delayed_work.work); |
| 545 | struct snd_soc_codec *codec = priv->codec; |
| 546 | struct twl6040_output *handsfree = &priv->handsfree; |
| 547 | unsigned int delay = handsfree->step_delay; |
| 548 | int i, handsfree_complete; |
| 549 | |
| 550 | /* do we need to ramp at all ? */ |
| 551 | if (handsfree->ramp == TWL6040_RAMP_NONE) |
| 552 | return; |
| 553 | |
| 554 | /* HF PGA volumes have 5 bits of resolution to ramp */ |
| 555 | for (i = 0; i <= 32; i++) { |
Axel Castaneda Gonzalez | 1fbe995 | 2011-02-23 20:08:28 -0600 | [diff] [blame] | 556 | handsfree_complete = twl6040_hf_ramp_step(codec, |
| 557 | handsfree->left_step, |
| 558 | handsfree->right_step); |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 559 | |
| 560 | /* ramp finished ? */ |
| 561 | if (handsfree_complete) |
| 562 | break; |
| 563 | |
| 564 | /* |
| 565 | * TODO: tune: delay is longer over 0dB |
| 566 | * as increases are larger. |
| 567 | */ |
| 568 | if (i >= 16) |
| 569 | schedule_timeout_interruptible(msecs_to_jiffies(delay + |
| 570 | (delay >> 1))); |
| 571 | else |
| 572 | schedule_timeout_interruptible(msecs_to_jiffies(delay)); |
| 573 | } |
| 574 | |
| 575 | |
| 576 | if (handsfree->ramp == TWL6040_RAMP_DOWN) { |
| 577 | handsfree->active = 0; |
| 578 | complete(&handsfree->ramp_done); |
| 579 | } else |
| 580 | handsfree->active = 1; |
| 581 | handsfree->ramp = TWL6040_RAMP_NONE; |
| 582 | } |
| 583 | |
| 584 | static int pga_event(struct snd_soc_dapm_widget *w, |
| 585 | struct snd_kcontrol *kcontrol, int event) |
| 586 | { |
| 587 | struct snd_soc_codec *codec = w->codec; |
| 588 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
| 589 | struct twl6040_output *out; |
| 590 | struct delayed_work *work; |
| 591 | struct workqueue_struct *queue; |
| 592 | |
| 593 | switch (w->shift) { |
| 594 | case 2: |
| 595 | case 3: |
| 596 | out = &priv->headset; |
| 597 | work = &priv->hs_delayed_work; |
| 598 | queue = priv->hs_workqueue; |
Axel Castaneda Gonzalez | 1fbe995 | 2011-02-23 20:08:28 -0600 | [diff] [blame] | 599 | out->left_step = priv->hs_left_step; |
| 600 | out->right_step = priv->hs_right_step; |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 601 | out->step_delay = 5; /* 5 ms between volume ramp steps */ |
| 602 | break; |
| 603 | case 4: |
| 604 | out = &priv->handsfree; |
| 605 | work = &priv->hf_delayed_work; |
| 606 | queue = priv->hf_workqueue; |
Axel Castaneda Gonzalez | 1fbe995 | 2011-02-23 20:08:28 -0600 | [diff] [blame] | 607 | out->left_step = priv->hf_left_step; |
| 608 | out->right_step = priv->hf_right_step; |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 609 | out->step_delay = 5; /* 5 ms between volume ramp steps */ |
| 610 | if (SND_SOC_DAPM_EVENT_ON(event)) |
| 611 | priv->non_lp++; |
| 612 | else |
| 613 | priv->non_lp--; |
| 614 | break; |
| 615 | default: |
| 616 | return -1; |
| 617 | } |
| 618 | |
| 619 | switch (event) { |
| 620 | case SND_SOC_DAPM_POST_PMU: |
| 621 | if (out->active) |
| 622 | break; |
| 623 | |
| 624 | /* don't use volume ramp for power-up */ |
| 625 | out->left_step = out->left_vol; |
| 626 | out->right_step = out->right_vol; |
| 627 | |
| 628 | if (!delayed_work_pending(work)) { |
| 629 | out->ramp = TWL6040_RAMP_UP; |
| 630 | queue_delayed_work(queue, work, |
| 631 | msecs_to_jiffies(1)); |
| 632 | } |
| 633 | break; |
| 634 | |
| 635 | case SND_SOC_DAPM_PRE_PMD: |
| 636 | if (!out->active) |
| 637 | break; |
| 638 | |
| 639 | if (!delayed_work_pending(work)) { |
| 640 | /* use volume ramp for power-down */ |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 641 | out->ramp = TWL6040_RAMP_DOWN; |
| 642 | INIT_COMPLETION(out->ramp_done); |
| 643 | |
| 644 | queue_delayed_work(queue, work, |
| 645 | msecs_to_jiffies(1)); |
| 646 | |
| 647 | wait_for_completion_timeout(&out->ramp_done, |
| 648 | msecs_to_jiffies(2000)); |
| 649 | } |
| 650 | break; |
| 651 | } |
| 652 | |
| 653 | return 0; |
| 654 | } |
| 655 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 656 | /* set headset dac and driver power mode */ |
| 657 | static int headset_power_mode(struct snd_soc_codec *codec, int high_perf) |
| 658 | { |
| 659 | int hslctl, hsrctl; |
| 660 | int mask = TWL6040_HSDRVMODEL | TWL6040_HSDACMODEL; |
| 661 | |
| 662 | hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL); |
| 663 | hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL); |
| 664 | |
| 665 | if (high_perf) { |
| 666 | hslctl &= ~mask; |
| 667 | hsrctl &= ~mask; |
| 668 | } else { |
| 669 | hslctl |= mask; |
| 670 | hsrctl |= mask; |
| 671 | } |
| 672 | |
| 673 | twl6040_write(codec, TWL6040_REG_HSLCTL, hslctl); |
| 674 | twl6040_write(codec, TWL6040_REG_HSRCTL, hsrctl); |
| 675 | |
| 676 | return 0; |
| 677 | } |
| 678 | |
Jorge Eduardo Candelaria | 0fad4ed | 2010-07-15 11:38:01 -0500 | [diff] [blame] | 679 | static int twl6040_hs_dac_event(struct snd_soc_dapm_widget *w, |
| 680 | struct snd_kcontrol *kcontrol, int event) |
| 681 | { |
| 682 | msleep(1); |
| 683 | return 0; |
| 684 | } |
| 685 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 686 | static int twl6040_power_mode_event(struct snd_soc_dapm_widget *w, |
| 687 | struct snd_kcontrol *kcontrol, int event) |
| 688 | { |
| 689 | struct snd_soc_codec *codec = w->codec; |
Takashi Iwai | d4a8ca2 | 2010-04-20 08:20:31 +0200 | [diff] [blame] | 690 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
Misael Lopez Cruz | 6bba63b | 2011-02-11 17:51:05 -0600 | [diff] [blame] | 691 | int ret = 0; |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 692 | |
Misael Lopez Cruz | 6bba63b | 2011-02-11 17:51:05 -0600 | [diff] [blame] | 693 | if (SND_SOC_DAPM_EVENT_ON(event)) { |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 694 | priv->non_lp++; |
Misael Lopez Cruz | 6bba63b | 2011-02-11 17:51:05 -0600 | [diff] [blame] | 695 | if (!strcmp(w->name, "Earphone Driver")) { |
| 696 | /* Earphone doesn't support low power mode */ |
| 697 | priv->hs_power_mode_locked = 1; |
| 698 | ret = headset_power_mode(codec, 1); |
| 699 | } |
| 700 | } else { |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 701 | priv->non_lp--; |
Misael Lopez Cruz | 6bba63b | 2011-02-11 17:51:05 -0600 | [diff] [blame] | 702 | if (!strcmp(w->name, "Earphone Driver")) { |
| 703 | priv->hs_power_mode_locked = 0; |
| 704 | ret = headset_power_mode(codec, priv->hs_power_mode); |
| 705 | } |
| 706 | } |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 707 | |
Jorge Eduardo Candelaria | 0fad4ed | 2010-07-15 11:38:01 -0500 | [diff] [blame] | 708 | msleep(1); |
| 709 | |
Misael Lopez Cruz | 6bba63b | 2011-02-11 17:51:05 -0600 | [diff] [blame] | 710 | return ret; |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Mark Brown | 64ed983 | 2011-01-20 21:43:44 +0000 | [diff] [blame] | 713 | static void twl6040_hs_jack_report(struct snd_soc_codec *codec, |
| 714 | struct snd_soc_jack *jack, int report) |
Jorge Eduardo Candelaria | a2d2362 | 2010-12-10 20:45:17 -0600 | [diff] [blame] | 715 | { |
| 716 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
| 717 | int status; |
| 718 | |
| 719 | mutex_lock(&priv->mutex); |
| 720 | |
| 721 | /* Sync status */ |
| 722 | status = twl6040_read_reg_volatile(codec, TWL6040_REG_STATUS); |
| 723 | if (status & TWL6040_PLUGCOMP) |
| 724 | snd_soc_jack_report(jack, report, report); |
| 725 | else |
| 726 | snd_soc_jack_report(jack, 0, report); |
| 727 | |
| 728 | mutex_unlock(&priv->mutex); |
| 729 | } |
| 730 | |
| 731 | void twl6040_hs_jack_detect(struct snd_soc_codec *codec, |
| 732 | struct snd_soc_jack *jack, int report) |
| 733 | { |
| 734 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
| 735 | struct twl6040_jack_data *hs_jack = &priv->hs_jack; |
| 736 | |
| 737 | hs_jack->jack = jack; |
| 738 | hs_jack->report = report; |
| 739 | |
| 740 | twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report); |
| 741 | } |
| 742 | EXPORT_SYMBOL_GPL(twl6040_hs_jack_detect); |
| 743 | |
| 744 | static void twl6040_accessory_work(struct work_struct *work) |
| 745 | { |
| 746 | struct twl6040_data *priv = container_of(work, |
| 747 | struct twl6040_data, delayed_work.work); |
| 748 | struct snd_soc_codec *codec = priv->codec; |
| 749 | struct twl6040_jack_data *hs_jack = &priv->hs_jack; |
| 750 | |
| 751 | twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report); |
| 752 | } |
| 753 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 754 | /* audio interrupt handler */ |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 755 | static irqreturn_t twl6040_audio_handler(int irq, void *data) |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 756 | { |
| 757 | struct snd_soc_codec *codec = data; |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 758 | struct twl6040 *twl6040 = codec->control_data; |
Takashi Iwai | d4a8ca2 | 2010-04-20 08:20:31 +0200 | [diff] [blame] | 759 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 760 | u8 intid; |
| 761 | |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 762 | intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID); |
Olaya, Margarita | cf370a5 | 2010-12-10 21:05:30 -0600 | [diff] [blame] | 763 | |
| 764 | if ((intid & TWL6040_PLUGINT) || (intid & TWL6040_UNPLUGINT)) |
Jorge Eduardo Candelaria | a2d2362 | 2010-12-10 20:45:17 -0600 | [diff] [blame] | 765 | queue_delayed_work(priv->workqueue, &priv->delayed_work, |
| 766 | msecs_to_jiffies(200)); |
Olaya, Margarita | cf370a5 | 2010-12-10 21:05:30 -0600 | [diff] [blame] | 767 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 768 | return IRQ_HANDLED; |
| 769 | } |
| 770 | |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 771 | static int twl6040_put_volsw(struct snd_kcontrol *kcontrol, |
| 772 | struct snd_ctl_elem_value *ucontrol) |
| 773 | { |
| 774 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 775 | struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec); |
| 776 | struct twl6040_output *out = NULL; |
| 777 | struct soc_mixer_control *mc = |
| 778 | (struct soc_mixer_control *)kcontrol->private_value; |
| 779 | int ret; |
| 780 | unsigned int reg = mc->reg; |
| 781 | |
| 782 | /* For HS and HF we shadow the values and only actually write |
| 783 | * them out when active in order to ensure the amplifier comes on |
| 784 | * as quietly as possible. */ |
| 785 | switch (reg) { |
| 786 | case TWL6040_REG_HSGAIN: |
| 787 | out = &twl6040_priv->headset; |
| 788 | break; |
| 789 | default: |
| 790 | break; |
| 791 | } |
| 792 | |
| 793 | if (out) { |
| 794 | out->left_vol = ucontrol->value.integer.value[0]; |
| 795 | out->right_vol = ucontrol->value.integer.value[1]; |
| 796 | if (!out->active) |
| 797 | return 1; |
| 798 | } |
| 799 | |
| 800 | ret = snd_soc_put_volsw(kcontrol, ucontrol); |
| 801 | if (ret < 0) |
| 802 | return ret; |
| 803 | |
| 804 | return 1; |
| 805 | } |
| 806 | |
| 807 | static int twl6040_get_volsw(struct snd_kcontrol *kcontrol, |
| 808 | struct snd_ctl_elem_value *ucontrol) |
| 809 | { |
| 810 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 811 | struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec); |
| 812 | struct twl6040_output *out = &twl6040_priv->headset; |
| 813 | struct soc_mixer_control *mc = |
| 814 | (struct soc_mixer_control *)kcontrol->private_value; |
| 815 | unsigned int reg = mc->reg; |
| 816 | |
| 817 | switch (reg) { |
| 818 | case TWL6040_REG_HSGAIN: |
| 819 | out = &twl6040_priv->headset; |
| 820 | ucontrol->value.integer.value[0] = out->left_vol; |
| 821 | ucontrol->value.integer.value[1] = out->right_vol; |
| 822 | return 0; |
| 823 | |
| 824 | default: |
| 825 | break; |
| 826 | } |
| 827 | |
| 828 | return snd_soc_get_volsw(kcontrol, ucontrol); |
| 829 | } |
| 830 | |
| 831 | static int twl6040_put_volsw_2r_vu(struct snd_kcontrol *kcontrol, |
| 832 | struct snd_ctl_elem_value *ucontrol) |
| 833 | { |
| 834 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 835 | struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec); |
| 836 | struct twl6040_output *out = NULL; |
| 837 | struct soc_mixer_control *mc = |
| 838 | (struct soc_mixer_control *)kcontrol->private_value; |
| 839 | int ret; |
| 840 | unsigned int reg = mc->reg; |
| 841 | |
| 842 | /* For HS and HF we shadow the values and only actually write |
| 843 | * them out when active in order to ensure the amplifier comes on |
| 844 | * as quietly as possible. */ |
| 845 | switch (reg) { |
| 846 | case TWL6040_REG_HFLGAIN: |
| 847 | case TWL6040_REG_HFRGAIN: |
| 848 | out = &twl6040_priv->handsfree; |
| 849 | break; |
| 850 | default: |
| 851 | break; |
| 852 | } |
| 853 | |
| 854 | if (out) { |
| 855 | out->left_vol = ucontrol->value.integer.value[0]; |
| 856 | out->right_vol = ucontrol->value.integer.value[1]; |
| 857 | if (!out->active) |
| 858 | return 1; |
| 859 | } |
| 860 | |
| 861 | ret = snd_soc_put_volsw_2r(kcontrol, ucontrol); |
| 862 | if (ret < 0) |
| 863 | return ret; |
| 864 | |
| 865 | return 1; |
| 866 | } |
| 867 | |
| 868 | static int twl6040_get_volsw_2r(struct snd_kcontrol *kcontrol, |
| 869 | struct snd_ctl_elem_value *ucontrol) |
| 870 | { |
| 871 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 872 | struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec); |
| 873 | struct twl6040_output *out = &twl6040_priv->handsfree; |
| 874 | struct soc_mixer_control *mc = |
| 875 | (struct soc_mixer_control *)kcontrol->private_value; |
| 876 | unsigned int reg = mc->reg; |
| 877 | |
| 878 | /* If these are cached registers use the cache */ |
| 879 | switch (reg) { |
| 880 | case TWL6040_REG_HFLGAIN: |
| 881 | case TWL6040_REG_HFRGAIN: |
| 882 | out = &twl6040_priv->handsfree; |
| 883 | ucontrol->value.integer.value[0] = out->left_vol; |
| 884 | ucontrol->value.integer.value[1] = out->right_vol; |
| 885 | return 0; |
| 886 | |
| 887 | default: |
| 888 | break; |
| 889 | } |
| 890 | |
| 891 | return snd_soc_get_volsw_2r(kcontrol, ucontrol); |
| 892 | } |
| 893 | |
| 894 | /* double control with volume update */ |
| 895 | #define SOC_TWL6040_DOUBLE_TLV(xname, xreg, shift_left, shift_right, xmax,\ |
| 896 | xinvert, tlv_array)\ |
| 897 | { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\ |
| 898 | .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\ |
| 899 | SNDRV_CTL_ELEM_ACCESS_READWRITE,\ |
| 900 | .tlv.p = (tlv_array), \ |
| 901 | .info = snd_soc_info_volsw, .get = twl6040_get_volsw, \ |
| 902 | .put = twl6040_put_volsw, \ |
| 903 | .private_value = (unsigned long)&(struct soc_mixer_control) \ |
| 904 | {.reg = xreg, .shift = shift_left, .rshift = shift_right,\ |
| 905 | .max = xmax, .platform_max = xmax, .invert = xinvert} } |
| 906 | |
| 907 | /* double control with volume update */ |
| 908 | #define SOC_TWL6040_DOUBLE_R_TLV(xname, reg_left, reg_right, xshift, xmax,\ |
| 909 | xinvert, tlv_array)\ |
| 910 | { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\ |
| 911 | .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | \ |
| 912 | SNDRV_CTL_ELEM_ACCESS_READWRITE | \ |
| 913 | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \ |
| 914 | .tlv.p = (tlv_array), \ |
| 915 | .info = snd_soc_info_volsw_2r, \ |
| 916 | .get = twl6040_get_volsw_2r, .put = twl6040_put_volsw_2r_vu, \ |
| 917 | .private_value = (unsigned long)&(struct soc_mixer_control) \ |
| 918 | {.reg = reg_left, .rreg = reg_right, .shift = xshift, \ |
| 919 | .rshift = xshift, .max = xmax, .invert = xinvert}, } |
| 920 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 921 | /* |
| 922 | * MICATT volume control: |
| 923 | * from -6 to 0 dB in 6 dB steps |
| 924 | */ |
| 925 | static DECLARE_TLV_DB_SCALE(mic_preamp_tlv, -600, 600, 0); |
| 926 | |
| 927 | /* |
| 928 | * MICGAIN volume control: |
Olaya, Margarita | 9020808 | 2010-12-10 21:06:39 -0600 | [diff] [blame] | 929 | * from -6 to 30 dB in 6 dB steps |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 930 | */ |
Olaya, Margarita | 9020808 | 2010-12-10 21:06:39 -0600 | [diff] [blame] | 931 | static DECLARE_TLV_DB_SCALE(mic_amp_tlv, -600, 600, 0); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 932 | |
| 933 | /* |
Jorge Eduardo Candelaria | 370a031 | 2010-12-10 21:05:32 -0600 | [diff] [blame] | 934 | * AFMGAIN volume control: |
Liam Girdwood | 1f71a3b | 2011-03-28 19:23:23 +0100 | [diff] [blame] | 935 | * from -18 to 24 dB in 6 dB steps |
Jorge Eduardo Candelaria | 370a031 | 2010-12-10 21:05:32 -0600 | [diff] [blame] | 936 | */ |
Liam Girdwood | 1f71a3b | 2011-03-28 19:23:23 +0100 | [diff] [blame] | 937 | static DECLARE_TLV_DB_SCALE(afm_amp_tlv, -1800, 600, 0); |
Jorge Eduardo Candelaria | 370a031 | 2010-12-10 21:05:32 -0600 | [diff] [blame] | 938 | |
| 939 | /* |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 940 | * HSGAIN volume control: |
| 941 | * from -30 to 0 dB in 2 dB steps |
| 942 | */ |
| 943 | static DECLARE_TLV_DB_SCALE(hs_tlv, -3000, 200, 0); |
| 944 | |
| 945 | /* |
| 946 | * HFGAIN volume control: |
| 947 | * from -52 to 6 dB in 2 dB steps |
| 948 | */ |
| 949 | static DECLARE_TLV_DB_SCALE(hf_tlv, -5200, 200, 0); |
| 950 | |
Jorge Eduardo Candelaria | 871a05a | 2010-05-18 12:44:18 -0500 | [diff] [blame] | 951 | /* |
| 952 | * EPGAIN volume control: |
| 953 | * from -24 to 6 dB in 2 dB steps |
| 954 | */ |
| 955 | static DECLARE_TLV_DB_SCALE(ep_tlv, -2400, 200, 0); |
| 956 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 957 | /* Left analog microphone selection */ |
| 958 | static const char *twl6040_amicl_texts[] = |
| 959 | {"Headset Mic", "Main Mic", "Aux/FM Left", "Off"}; |
| 960 | |
| 961 | /* Right analog microphone selection */ |
| 962 | static const char *twl6040_amicr_texts[] = |
| 963 | {"Headset Mic", "Sub Mic", "Aux/FM Right", "Off"}; |
| 964 | |
| 965 | static const struct soc_enum twl6040_enum[] = { |
Francois Mazard | cb973d7 | 2010-12-10 21:06:03 -0600 | [diff] [blame] | 966 | SOC_ENUM_SINGLE(TWL6040_REG_MICLCTL, 3, 4, twl6040_amicl_texts), |
| 967 | SOC_ENUM_SINGLE(TWL6040_REG_MICRCTL, 3, 4, twl6040_amicr_texts), |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 968 | }; |
| 969 | |
Jorge Eduardo Candelaria | 370a031 | 2010-12-10 21:05:32 -0600 | [diff] [blame] | 970 | static const char *twl6040_hs_texts[] = { |
| 971 | "Off", "HS DAC", "Line-In amp" |
| 972 | }; |
| 973 | |
| 974 | static const struct soc_enum twl6040_hs_enum[] = { |
| 975 | SOC_ENUM_SINGLE(TWL6040_REG_HSLCTL, 5, ARRAY_SIZE(twl6040_hs_texts), |
| 976 | twl6040_hs_texts), |
| 977 | SOC_ENUM_SINGLE(TWL6040_REG_HSRCTL, 5, ARRAY_SIZE(twl6040_hs_texts), |
| 978 | twl6040_hs_texts), |
| 979 | }; |
| 980 | |
| 981 | static const char *twl6040_hf_texts[] = { |
| 982 | "Off", "HF DAC", "Line-In amp" |
| 983 | }; |
| 984 | |
| 985 | static const struct soc_enum twl6040_hf_enum[] = { |
| 986 | SOC_ENUM_SINGLE(TWL6040_REG_HFLCTL, 2, ARRAY_SIZE(twl6040_hf_texts), |
| 987 | twl6040_hf_texts), |
| 988 | SOC_ENUM_SINGLE(TWL6040_REG_HFRCTL, 2, ARRAY_SIZE(twl6040_hf_texts), |
| 989 | twl6040_hf_texts), |
| 990 | }; |
| 991 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 992 | static const struct snd_kcontrol_new amicl_control = |
| 993 | SOC_DAPM_ENUM("Route", twl6040_enum[0]); |
| 994 | |
| 995 | static const struct snd_kcontrol_new amicr_control = |
| 996 | SOC_DAPM_ENUM("Route", twl6040_enum[1]); |
| 997 | |
| 998 | /* Headset DAC playback switches */ |
Jorge Eduardo Candelaria | 370a031 | 2010-12-10 21:05:32 -0600 | [diff] [blame] | 999 | static const struct snd_kcontrol_new hsl_mux_controls = |
| 1000 | SOC_DAPM_ENUM("Route", twl6040_hs_enum[0]); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1001 | |
Jorge Eduardo Candelaria | 370a031 | 2010-12-10 21:05:32 -0600 | [diff] [blame] | 1002 | static const struct snd_kcontrol_new hsr_mux_controls = |
| 1003 | SOC_DAPM_ENUM("Route", twl6040_hs_enum[1]); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1004 | |
| 1005 | /* Handsfree DAC playback switches */ |
Jorge Eduardo Candelaria | 370a031 | 2010-12-10 21:05:32 -0600 | [diff] [blame] | 1006 | static const struct snd_kcontrol_new hfl_mux_controls = |
| 1007 | SOC_DAPM_ENUM("Route", twl6040_hf_enum[0]); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1008 | |
Jorge Eduardo Candelaria | 370a031 | 2010-12-10 21:05:32 -0600 | [diff] [blame] | 1009 | static const struct snd_kcontrol_new hfr_mux_controls = |
| 1010 | SOC_DAPM_ENUM("Route", twl6040_hf_enum[1]); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1011 | |
Jorge Eduardo Candelaria | 871a05a | 2010-05-18 12:44:18 -0500 | [diff] [blame] | 1012 | static const struct snd_kcontrol_new ep_driver_switch_controls = |
| 1013 | SOC_DAPM_SINGLE("Switch", TWL6040_REG_EARCTL, 0, 1, 0); |
| 1014 | |
Misael Lopez Cruz | 6bba63b | 2011-02-11 17:51:05 -0600 | [diff] [blame] | 1015 | /* Headset power mode */ |
Peter Ujfalusi | 7cca606 | 2011-06-27 13:33:14 +0300 | [diff] [blame^] | 1016 | static const char *twl6040_power_mode_texts[] = { |
Misael Lopez Cruz | 6bba63b | 2011-02-11 17:51:05 -0600 | [diff] [blame] | 1017 | "Low-Power", "High-Perfomance", |
| 1018 | }; |
| 1019 | |
Peter Ujfalusi | 7cca606 | 2011-06-27 13:33:14 +0300 | [diff] [blame^] | 1020 | static const struct soc_enum twl6040_power_mode_enum = |
| 1021 | SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(twl6040_power_mode_texts), |
| 1022 | twl6040_power_mode_texts); |
Misael Lopez Cruz | 6bba63b | 2011-02-11 17:51:05 -0600 | [diff] [blame] | 1023 | |
| 1024 | static int twl6040_headset_power_get_enum(struct snd_kcontrol *kcontrol, |
| 1025 | struct snd_ctl_elem_value *ucontrol) |
| 1026 | { |
| 1027 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1028 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
| 1029 | |
| 1030 | ucontrol->value.enumerated.item[0] = priv->hs_power_mode; |
| 1031 | |
| 1032 | return 0; |
| 1033 | } |
| 1034 | |
| 1035 | static int twl6040_headset_power_put_enum(struct snd_kcontrol *kcontrol, |
| 1036 | struct snd_ctl_elem_value *ucontrol) |
| 1037 | { |
| 1038 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1039 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
| 1040 | int high_perf = ucontrol->value.enumerated.item[0]; |
| 1041 | int ret = 0; |
| 1042 | |
| 1043 | if (!priv->hs_power_mode_locked) |
| 1044 | ret = headset_power_mode(codec, high_perf); |
| 1045 | |
| 1046 | if (!ret) |
| 1047 | priv->hs_power_mode = high_perf; |
| 1048 | |
| 1049 | return ret; |
| 1050 | } |
| 1051 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1052 | static const struct snd_kcontrol_new twl6040_snd_controls[] = { |
| 1053 | /* Capture gains */ |
| 1054 | SOC_DOUBLE_TLV("Capture Preamplifier Volume", |
| 1055 | TWL6040_REG_MICGAIN, 6, 7, 1, 1, mic_preamp_tlv), |
| 1056 | SOC_DOUBLE_TLV("Capture Volume", |
| 1057 | TWL6040_REG_MICGAIN, 0, 3, 4, 0, mic_amp_tlv), |
| 1058 | |
Jorge Eduardo Candelaria | 370a031 | 2010-12-10 21:05:32 -0600 | [diff] [blame] | 1059 | /* AFM gains */ |
| 1060 | SOC_DOUBLE_TLV("Aux FM Volume", |
Liam Girdwood | 1f71a3b | 2011-03-28 19:23:23 +0100 | [diff] [blame] | 1061 | TWL6040_REG_LINEGAIN, 0, 3, 7, 0, afm_amp_tlv), |
Jorge Eduardo Candelaria | 370a031 | 2010-12-10 21:05:32 -0600 | [diff] [blame] | 1062 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1063 | /* Playback gains */ |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 1064 | SOC_TWL6040_DOUBLE_TLV("Headset Playback Volume", |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1065 | TWL6040_REG_HSGAIN, 0, 4, 0xF, 1, hs_tlv), |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 1066 | SOC_TWL6040_DOUBLE_R_TLV("Handsfree Playback Volume", |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1067 | TWL6040_REG_HFLGAIN, TWL6040_REG_HFRGAIN, 0, 0x1D, 1, hf_tlv), |
Jorge Eduardo Candelaria | 871a05a | 2010-05-18 12:44:18 -0500 | [diff] [blame] | 1068 | SOC_SINGLE_TLV("Earphone Playback Volume", |
| 1069 | TWL6040_REG_EARCTL, 1, 0xF, 1, ep_tlv), |
Misael Lopez Cruz | 6bba63b | 2011-02-11 17:51:05 -0600 | [diff] [blame] | 1070 | |
Peter Ujfalusi | 7cca606 | 2011-06-27 13:33:14 +0300 | [diff] [blame^] | 1071 | SOC_ENUM_EXT("Headset Power Mode", twl6040_power_mode_enum, |
Misael Lopez Cruz | 6bba63b | 2011-02-11 17:51:05 -0600 | [diff] [blame] | 1072 | twl6040_headset_power_get_enum, |
| 1073 | twl6040_headset_power_put_enum), |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1074 | }; |
| 1075 | |
| 1076 | static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = { |
| 1077 | /* Inputs */ |
| 1078 | SND_SOC_DAPM_INPUT("MAINMIC"), |
| 1079 | SND_SOC_DAPM_INPUT("HSMIC"), |
| 1080 | SND_SOC_DAPM_INPUT("SUBMIC"), |
| 1081 | SND_SOC_DAPM_INPUT("AFML"), |
| 1082 | SND_SOC_DAPM_INPUT("AFMR"), |
| 1083 | |
| 1084 | /* Outputs */ |
| 1085 | SND_SOC_DAPM_OUTPUT("HSOL"), |
| 1086 | SND_SOC_DAPM_OUTPUT("HSOR"), |
| 1087 | SND_SOC_DAPM_OUTPUT("HFL"), |
| 1088 | SND_SOC_DAPM_OUTPUT("HFR"), |
Jorge Eduardo Candelaria | 871a05a | 2010-05-18 12:44:18 -0500 | [diff] [blame] | 1089 | SND_SOC_DAPM_OUTPUT("EP"), |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1090 | |
| 1091 | /* Analog input muxes for the capture amplifiers */ |
| 1092 | SND_SOC_DAPM_MUX("Analog Left Capture Route", |
| 1093 | SND_SOC_NOPM, 0, 0, &amicl_control), |
| 1094 | SND_SOC_DAPM_MUX("Analog Right Capture Route", |
| 1095 | SND_SOC_NOPM, 0, 0, &amicr_control), |
| 1096 | |
| 1097 | /* Analog capture PGAs */ |
| 1098 | SND_SOC_DAPM_PGA("MicAmpL", |
| 1099 | TWL6040_REG_MICLCTL, 0, 0, NULL, 0), |
| 1100 | SND_SOC_DAPM_PGA("MicAmpR", |
| 1101 | TWL6040_REG_MICRCTL, 0, 0, NULL, 0), |
| 1102 | |
Jorge Eduardo Candelaria | 370a031 | 2010-12-10 21:05:32 -0600 | [diff] [blame] | 1103 | /* Auxiliary FM PGAs */ |
| 1104 | SND_SOC_DAPM_PGA("AFMAmpL", |
| 1105 | TWL6040_REG_MICLCTL, 1, 0, NULL, 0), |
| 1106 | SND_SOC_DAPM_PGA("AFMAmpR", |
| 1107 | TWL6040_REG_MICRCTL, 1, 0, NULL, 0), |
| 1108 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1109 | /* ADCs */ |
| 1110 | SND_SOC_DAPM_ADC("ADC Left", "Left Front Capture", |
| 1111 | TWL6040_REG_MICLCTL, 2, 0), |
| 1112 | SND_SOC_DAPM_ADC("ADC Right", "Right Front Capture", |
| 1113 | TWL6040_REG_MICRCTL, 2, 0), |
| 1114 | |
| 1115 | /* Microphone bias */ |
| 1116 | SND_SOC_DAPM_MICBIAS("Headset Mic Bias", |
| 1117 | TWL6040_REG_AMICBCTL, 0, 0), |
| 1118 | SND_SOC_DAPM_MICBIAS("Main Mic Bias", |
| 1119 | TWL6040_REG_AMICBCTL, 4, 0), |
| 1120 | SND_SOC_DAPM_MICBIAS("Digital Mic1 Bias", |
| 1121 | TWL6040_REG_DMICBCTL, 0, 0), |
| 1122 | SND_SOC_DAPM_MICBIAS("Digital Mic2 Bias", |
| 1123 | TWL6040_REG_DMICBCTL, 4, 0), |
| 1124 | |
| 1125 | /* DACs */ |
Jorge Eduardo Candelaria | 0fad4ed | 2010-07-15 11:38:01 -0500 | [diff] [blame] | 1126 | SND_SOC_DAPM_DAC_E("HSDAC Left", "Headset Playback", |
| 1127 | TWL6040_REG_HSLCTL, 0, 0, |
| 1128 | twl6040_hs_dac_event, |
| 1129 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), |
| 1130 | SND_SOC_DAPM_DAC_E("HSDAC Right", "Headset Playback", |
| 1131 | TWL6040_REG_HSRCTL, 0, 0, |
| 1132 | twl6040_hs_dac_event, |
| 1133 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1134 | SND_SOC_DAPM_DAC_E("HFDAC Left", "Handsfree Playback", |
| 1135 | TWL6040_REG_HFLCTL, 0, 0, |
| 1136 | twl6040_power_mode_event, |
| 1137 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), |
| 1138 | SND_SOC_DAPM_DAC_E("HFDAC Right", "Handsfree Playback", |
| 1139 | TWL6040_REG_HFRCTL, 0, 0, |
| 1140 | twl6040_power_mode_event, |
| 1141 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), |
| 1142 | |
Jorge Eduardo Candelaria | 370a031 | 2010-12-10 21:05:32 -0600 | [diff] [blame] | 1143 | SND_SOC_DAPM_MUX("HF Left Playback", |
| 1144 | SND_SOC_NOPM, 0, 0, &hfl_mux_controls), |
| 1145 | SND_SOC_DAPM_MUX("HF Right Playback", |
| 1146 | SND_SOC_NOPM, 0, 0, &hfr_mux_controls), |
| 1147 | /* Analog playback Muxes */ |
| 1148 | SND_SOC_DAPM_MUX("HS Left Playback", |
| 1149 | SND_SOC_NOPM, 0, 0, &hsl_mux_controls), |
| 1150 | SND_SOC_DAPM_MUX("HS Right Playback", |
| 1151 | SND_SOC_NOPM, 0, 0, &hsr_mux_controls), |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1152 | |
Jorge Eduardo Candelaria | 0fad4ed | 2010-07-15 11:38:01 -0500 | [diff] [blame] | 1153 | /* Analog playback drivers */ |
Olaya, Margarita | f769bdf | 2010-12-20 10:39:20 -0600 | [diff] [blame] | 1154 | SND_SOC_DAPM_OUT_DRV_E("Handsfree Left Driver", |
Jorge Eduardo Candelaria | 0fad4ed | 2010-07-15 11:38:01 -0500 | [diff] [blame] | 1155 | TWL6040_REG_HFLCTL, 4, 0, NULL, 0, |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 1156 | pga_event, |
| 1157 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), |
Olaya, Margarita | f769bdf | 2010-12-20 10:39:20 -0600 | [diff] [blame] | 1158 | SND_SOC_DAPM_OUT_DRV_E("Handsfree Right Driver", |
Jorge Eduardo Candelaria | 0fad4ed | 2010-07-15 11:38:01 -0500 | [diff] [blame] | 1159 | TWL6040_REG_HFRCTL, 4, 0, NULL, 0, |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 1160 | pga_event, |
| 1161 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), |
Olaya, Margarita | f769bdf | 2010-12-20 10:39:20 -0600 | [diff] [blame] | 1162 | SND_SOC_DAPM_OUT_DRV_E("Headset Left Driver", |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 1163 | TWL6040_REG_HSLCTL, 2, 0, NULL, 0, |
| 1164 | pga_event, |
| 1165 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), |
Olaya, Margarita | f769bdf | 2010-12-20 10:39:20 -0600 | [diff] [blame] | 1166 | SND_SOC_DAPM_OUT_DRV_E("Headset Right Driver", |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 1167 | TWL6040_REG_HSRCTL, 2, 0, NULL, 0, |
| 1168 | pga_event, |
| 1169 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), |
Jorge Eduardo Candelaria | 871a05a | 2010-05-18 12:44:18 -0500 | [diff] [blame] | 1170 | SND_SOC_DAPM_SWITCH_E("Earphone Driver", |
| 1171 | SND_SOC_NOPM, 0, 0, &ep_driver_switch_controls, |
| 1172 | twl6040_power_mode_event, |
| 1173 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1174 | |
| 1175 | /* Analog playback PGAs */ |
| 1176 | SND_SOC_DAPM_PGA("HFDAC Left PGA", |
| 1177 | TWL6040_REG_HFLCTL, 1, 0, NULL, 0), |
| 1178 | SND_SOC_DAPM_PGA("HFDAC Right PGA", |
| 1179 | TWL6040_REG_HFRCTL, 1, 0, NULL, 0), |
| 1180 | |
| 1181 | }; |
| 1182 | |
| 1183 | static const struct snd_soc_dapm_route intercon[] = { |
| 1184 | /* Capture path */ |
| 1185 | {"Analog Left Capture Route", "Headset Mic", "HSMIC"}, |
| 1186 | {"Analog Left Capture Route", "Main Mic", "MAINMIC"}, |
| 1187 | {"Analog Left Capture Route", "Aux/FM Left", "AFML"}, |
| 1188 | |
| 1189 | {"Analog Right Capture Route", "Headset Mic", "HSMIC"}, |
| 1190 | {"Analog Right Capture Route", "Sub Mic", "SUBMIC"}, |
| 1191 | {"Analog Right Capture Route", "Aux/FM Right", "AFMR"}, |
| 1192 | |
| 1193 | {"MicAmpL", NULL, "Analog Left Capture Route"}, |
| 1194 | {"MicAmpR", NULL, "Analog Right Capture Route"}, |
| 1195 | |
| 1196 | {"ADC Left", NULL, "MicAmpL"}, |
| 1197 | {"ADC Right", NULL, "MicAmpR"}, |
| 1198 | |
Jorge Eduardo Candelaria | 370a031 | 2010-12-10 21:05:32 -0600 | [diff] [blame] | 1199 | /* AFM path */ |
| 1200 | {"AFMAmpL", "NULL", "AFML"}, |
| 1201 | {"AFMAmpR", "NULL", "AFMR"}, |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1202 | |
Jorge Eduardo Candelaria | 370a031 | 2010-12-10 21:05:32 -0600 | [diff] [blame] | 1203 | {"HS Left Playback", "HS DAC", "HSDAC Left"}, |
| 1204 | {"HS Left Playback", "Line-In amp", "AFMAmpL"}, |
| 1205 | |
| 1206 | {"HS Right Playback", "HS DAC", "HSDAC Right"}, |
| 1207 | {"HS Right Playback", "Line-In amp", "AFMAmpR"}, |
| 1208 | |
| 1209 | {"Headset Left Driver", "NULL", "HS Left Playback"}, |
| 1210 | {"Headset Right Driver", "NULL", "HS Right Playback"}, |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1211 | |
| 1212 | {"HSOL", NULL, "Headset Left Driver"}, |
| 1213 | {"HSOR", NULL, "Headset Right Driver"}, |
| 1214 | |
Jorge Eduardo Candelaria | 871a05a | 2010-05-18 12:44:18 -0500 | [diff] [blame] | 1215 | /* Earphone playback path */ |
| 1216 | {"Earphone Driver", "Switch", "HSDAC Left"}, |
| 1217 | {"EP", NULL, "Earphone Driver"}, |
| 1218 | |
Jorge Eduardo Candelaria | 370a031 | 2010-12-10 21:05:32 -0600 | [diff] [blame] | 1219 | {"HF Left Playback", "HF DAC", "HFDAC Left"}, |
| 1220 | {"HF Left Playback", "Line-In amp", "AFMAmpL"}, |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1221 | |
Jorge Eduardo Candelaria | 370a031 | 2010-12-10 21:05:32 -0600 | [diff] [blame] | 1222 | {"HF Right Playback", "HF DAC", "HFDAC Right"}, |
| 1223 | {"HF Right Playback", "Line-In amp", "AFMAmpR"}, |
| 1224 | |
| 1225 | {"HFDAC Left PGA", NULL, "HF Left Playback"}, |
| 1226 | {"HFDAC Right PGA", NULL, "HF Right Playback"}, |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1227 | |
| 1228 | {"Handsfree Left Driver", "Switch", "HFDAC Left PGA"}, |
| 1229 | {"Handsfree Right Driver", "Switch", "HFDAC Right PGA"}, |
| 1230 | |
| 1231 | {"HFL", NULL, "Handsfree Left Driver"}, |
| 1232 | {"HFR", NULL, "Handsfree Right Driver"}, |
| 1233 | }; |
| 1234 | |
| 1235 | static int twl6040_add_widgets(struct snd_soc_codec *codec) |
| 1236 | { |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 1237 | struct snd_soc_dapm_context *dapm = &codec->dapm; |
| 1238 | |
| 1239 | snd_soc_dapm_new_controls(dapm, twl6040_dapm_widgets, |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1240 | ARRAY_SIZE(twl6040_dapm_widgets)); |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 1241 | snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon)); |
| 1242 | snd_soc_dapm_new_widgets(dapm); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1243 | |
| 1244 | return 0; |
| 1245 | } |
| 1246 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1247 | static int twl6040_set_bias_level(struct snd_soc_codec *codec, |
| 1248 | enum snd_soc_bias_level level) |
| 1249 | { |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 1250 | struct twl6040 *twl6040 = codec->control_data; |
Takashi Iwai | d4a8ca2 | 2010-04-20 08:20:31 +0200 | [diff] [blame] | 1251 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1252 | int ret; |
| 1253 | |
| 1254 | switch (level) { |
| 1255 | case SND_SOC_BIAS_ON: |
| 1256 | break; |
| 1257 | case SND_SOC_BIAS_PREPARE: |
| 1258 | break; |
| 1259 | case SND_SOC_BIAS_STANDBY: |
| 1260 | if (priv->codec_powered) |
| 1261 | break; |
| 1262 | |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 1263 | ret = twl6040_power(twl6040, 1); |
| 1264 | if (ret) |
| 1265 | return ret; |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1266 | |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 1267 | priv->codec_powered = 1; |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1268 | |
| 1269 | /* initialize vdd/vss registers with reg_cache */ |
| 1270 | twl6040_init_vdd_regs(codec); |
Olaya, Margarita | 65b7cec | 2010-12-14 19:18:36 -0600 | [diff] [blame] | 1271 | |
| 1272 | /* Set external boost GPO */ |
| 1273 | twl6040_write(codec, TWL6040_REG_GPOCTL, 0x02); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1274 | break; |
| 1275 | case SND_SOC_BIAS_OFF: |
| 1276 | if (!priv->codec_powered) |
| 1277 | break; |
| 1278 | |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 1279 | twl6040_power(twl6040, 0); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1280 | priv->codec_powered = 0; |
| 1281 | break; |
| 1282 | } |
| 1283 | |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 1284 | /* get PLL and sysclk after power transition */ |
| 1285 | priv->pll = twl6040_get_pll(twl6040); |
| 1286 | priv->sysclk = twl6040_get_sysclk(twl6040); |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 1287 | codec->dapm.bias_level = level; |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1288 | |
| 1289 | return 0; |
| 1290 | } |
| 1291 | |
| 1292 | /* set of rates for each pll: low-power and high-performance */ |
| 1293 | |
| 1294 | static unsigned int lp_rates[] = { |
Liam Girdwood | e17e4ab | 2011-03-18 19:00:50 +0000 | [diff] [blame] | 1295 | 8000, |
| 1296 | 11250, |
| 1297 | 16000, |
| 1298 | 22500, |
| 1299 | 32000, |
| 1300 | 44100, |
| 1301 | 48000, |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1302 | 88200, |
| 1303 | 96000, |
| 1304 | }; |
| 1305 | |
| 1306 | static struct snd_pcm_hw_constraint_list lp_constraints = { |
| 1307 | .count = ARRAY_SIZE(lp_rates), |
| 1308 | .list = lp_rates, |
| 1309 | }; |
| 1310 | |
| 1311 | static unsigned int hp_rates[] = { |
Liam Girdwood | e17e4ab | 2011-03-18 19:00:50 +0000 | [diff] [blame] | 1312 | 8000, |
| 1313 | 16000, |
| 1314 | 32000, |
| 1315 | 48000, |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1316 | 96000, |
| 1317 | }; |
| 1318 | |
| 1319 | static struct snd_pcm_hw_constraint_list hp_constraints = { |
| 1320 | .count = ARRAY_SIZE(hp_rates), |
| 1321 | .list = hp_rates, |
| 1322 | }; |
| 1323 | |
| 1324 | static int twl6040_startup(struct snd_pcm_substream *substream, |
| 1325 | struct snd_soc_dai *dai) |
| 1326 | { |
| 1327 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1328 | struct snd_soc_codec *codec = rtd->codec; |
Takashi Iwai | d4a8ca2 | 2010-04-20 08:20:31 +0200 | [diff] [blame] | 1329 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1330 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1331 | snd_pcm_hw_constraint_list(substream->runtime, 0, |
| 1332 | SNDRV_PCM_HW_PARAM_RATE, |
| 1333 | priv->sysclk_constraints); |
| 1334 | |
| 1335 | return 0; |
| 1336 | } |
| 1337 | |
| 1338 | static int twl6040_hw_params(struct snd_pcm_substream *substream, |
| 1339 | struct snd_pcm_hw_params *params, |
| 1340 | struct snd_soc_dai *dai) |
| 1341 | { |
| 1342 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1343 | struct snd_soc_codec *codec = rtd->codec; |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 1344 | struct twl6040 *twl6040 = codec->control_data; |
Takashi Iwai | d4a8ca2 | 2010-04-20 08:20:31 +0200 | [diff] [blame] | 1345 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 1346 | unsigned int sysclk; |
| 1347 | int rate, ret; |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1348 | |
| 1349 | /* nothing to do for high-perf pll, it supports only 48 kHz */ |
| 1350 | if (priv->pll == TWL6040_HPPLL_ID) |
| 1351 | return 0; |
| 1352 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1353 | rate = params_rate(params); |
| 1354 | switch (rate) { |
Olaya, Margarita | 60ea4ce | 2010-12-10 21:05:58 -0600 | [diff] [blame] | 1355 | case 11250: |
| 1356 | case 22500: |
| 1357 | case 44100: |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1358 | case 88200: |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 1359 | sysclk = 17640000; |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1360 | break; |
Olaya, Margarita | 60ea4ce | 2010-12-10 21:05:58 -0600 | [diff] [blame] | 1361 | case 8000: |
| 1362 | case 16000: |
| 1363 | case 32000: |
| 1364 | case 48000: |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1365 | case 96000: |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 1366 | sysclk = 19200000; |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1367 | break; |
| 1368 | default: |
| 1369 | dev_err(codec->dev, "unsupported rate %d\n", rate); |
| 1370 | return -EINVAL; |
| 1371 | } |
| 1372 | |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 1373 | ret = twl6040_set_pll(twl6040, TWL6040_LPPLL_ID, priv->clk_in, sysclk); |
| 1374 | if (ret) |
| 1375 | return ret; |
| 1376 | |
| 1377 | priv->sysclk = twl6040_get_sysclk(twl6040); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1378 | |
| 1379 | return 0; |
| 1380 | } |
| 1381 | |
Olaya, Margarita | 4e624d0 | 2010-12-10 21:05:54 -0600 | [diff] [blame] | 1382 | static int twl6040_prepare(struct snd_pcm_substream *substream, |
| 1383 | struct snd_soc_dai *dai) |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1384 | { |
| 1385 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1386 | struct snd_soc_codec *codec = rtd->codec; |
Takashi Iwai | d4a8ca2 | 2010-04-20 08:20:31 +0200 | [diff] [blame] | 1387 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1388 | |
Olaya, Margarita | 4e624d0 | 2010-12-10 21:05:54 -0600 | [diff] [blame] | 1389 | if (!priv->sysclk) { |
| 1390 | dev_err(codec->dev, |
| 1391 | "no mclk configured, call set_sysclk() on init\n"); |
| 1392 | return -EINVAL; |
| 1393 | } |
| 1394 | |
| 1395 | /* |
| 1396 | * capture is not supported at 17.64 MHz, |
| 1397 | * it's reserved for headset low-power playback scenario |
| 1398 | */ |
| 1399 | if ((priv->sysclk == 17640000) && |
| 1400 | substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
| 1401 | dev_err(codec->dev, |
| 1402 | "capture mode is not supported at %dHz\n", |
| 1403 | priv->sysclk); |
| 1404 | return -EINVAL; |
| 1405 | } |
| 1406 | |
| 1407 | if ((priv->sysclk == 17640000) && priv->non_lp) { |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1408 | dev_err(codec->dev, |
| 1409 | "some enabled paths aren't supported at %dHz\n", |
| 1410 | priv->sysclk); |
| 1411 | return -EPERM; |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1412 | } |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1413 | return 0; |
| 1414 | } |
| 1415 | |
| 1416 | static int twl6040_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
| 1417 | int clk_id, unsigned int freq, int dir) |
| 1418 | { |
| 1419 | struct snd_soc_codec *codec = codec_dai->codec; |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 1420 | struct twl6040 *twl6040 = codec->control_data; |
Takashi Iwai | d4a8ca2 | 2010-04-20 08:20:31 +0200 | [diff] [blame] | 1421 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 1422 | int ret = 0; |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1423 | |
| 1424 | switch (clk_id) { |
| 1425 | case TWL6040_SYSCLK_SEL_LPPLL: |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 1426 | ret = twl6040_set_pll(twl6040, TWL6040_LPPLL_ID, |
| 1427 | freq, priv->sysclk); |
| 1428 | if (ret) |
| 1429 | return ret; |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1430 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1431 | priv->sysclk_constraints = &lp_constraints; |
| 1432 | break; |
| 1433 | case TWL6040_SYSCLK_SEL_HPPLL: |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 1434 | ret = twl6040_set_pll(twl6040, TWL6040_HPPLL_ID, |
| 1435 | freq, priv->sysclk); |
| 1436 | if (ret) |
| 1437 | return ret; |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1438 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1439 | priv->sysclk_constraints = &hp_constraints; |
| 1440 | break; |
| 1441 | default: |
| 1442 | dev_err(codec->dev, "unknown clk_id %d\n", clk_id); |
| 1443 | return -EINVAL; |
| 1444 | } |
| 1445 | |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 1446 | priv->pll = twl6040_get_pll(twl6040); |
| 1447 | priv->clk_in = freq; |
| 1448 | priv->sysclk = twl6040_get_sysclk(twl6040); |
| 1449 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1450 | return 0; |
| 1451 | } |
| 1452 | |
| 1453 | static struct snd_soc_dai_ops twl6040_dai_ops = { |
| 1454 | .startup = twl6040_startup, |
| 1455 | .hw_params = twl6040_hw_params, |
Olaya, Margarita | 4e624d0 | 2010-12-10 21:05:54 -0600 | [diff] [blame] | 1456 | .prepare = twl6040_prepare, |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1457 | .set_sysclk = twl6040_set_dai_sysclk, |
| 1458 | }; |
| 1459 | |
Liam Girdwood | 6510bdc | 2011-02-11 17:37:51 +0000 | [diff] [blame] | 1460 | static struct snd_soc_dai_driver twl6040_dai[] = { |
| 1461 | { |
| 1462 | .name = "twl6040-ul", |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1463 | .capture = { |
| 1464 | .stream_name = "Capture", |
| 1465 | .channels_min = 1, |
| 1466 | .channels_max = 2, |
| 1467 | .rates = TWL6040_RATES, |
| 1468 | .formats = TWL6040_FORMATS, |
| 1469 | }, |
| 1470 | .ops = &twl6040_dai_ops, |
Liam Girdwood | 6510bdc | 2011-02-11 17:37:51 +0000 | [diff] [blame] | 1471 | }, |
| 1472 | { |
| 1473 | .name = "twl6040-dl1", |
| 1474 | .playback = { |
| 1475 | .stream_name = "Headset Playback", |
| 1476 | .channels_min = 1, |
| 1477 | .channels_max = 2, |
| 1478 | .rates = TWL6040_RATES, |
| 1479 | .formats = TWL6040_FORMATS, |
| 1480 | }, |
| 1481 | .ops = &twl6040_dai_ops, |
| 1482 | }, |
| 1483 | { |
| 1484 | .name = "twl6040-dl2", |
| 1485 | .playback = { |
| 1486 | .stream_name = "Handsfree Playback", |
| 1487 | .channels_min = 1, |
| 1488 | .channels_max = 2, |
| 1489 | .rates = TWL6040_RATES, |
| 1490 | .formats = TWL6040_FORMATS, |
| 1491 | }, |
| 1492 | .ops = &twl6040_dai_ops, |
| 1493 | }, |
| 1494 | { |
| 1495 | .name = "twl6040-vib", |
| 1496 | .playback = { |
| 1497 | .stream_name = "Vibra Playback", |
| 1498 | .channels_min = 2, |
| 1499 | .channels_max = 2, |
| 1500 | .rates = SNDRV_PCM_RATE_CONTINUOUS, |
| 1501 | .formats = TWL6040_FORMATS, |
| 1502 | }, |
| 1503 | .ops = &twl6040_dai_ops, |
| 1504 | }, |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1505 | }; |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1506 | |
| 1507 | #ifdef CONFIG_PM |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1508 | 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] | 1509 | { |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1510 | twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF); |
| 1511 | |
| 1512 | return 0; |
| 1513 | } |
| 1514 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1515 | static int twl6040_resume(struct snd_soc_codec *codec) |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1516 | { |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1517 | twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
Olaya, Margarita | 6c31104 | 2010-12-10 21:05:46 -0600 | [diff] [blame] | 1518 | twl6040_set_bias_level(codec, codec->dapm.suspend_bias_level); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1519 | |
| 1520 | return 0; |
| 1521 | } |
| 1522 | #else |
| 1523 | #define twl6040_suspend NULL |
| 1524 | #define twl6040_resume NULL |
| 1525 | #endif |
| 1526 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1527 | static int twl6040_probe(struct snd_soc_codec *codec) |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1528 | { |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1529 | struct twl6040_data *priv; |
Axel Castaneda Gonzalez | 1fbe995 | 2011-02-23 20:08:28 -0600 | [diff] [blame] | 1530 | struct twl4030_codec_data *pdata = dev_get_platdata(codec->dev); |
Peter Ujfalusi | 2a433b9 | 2011-07-04 19:52:26 +0300 | [diff] [blame] | 1531 | struct platform_device *pdev = container_of(codec->dev, |
| 1532 | struct platform_device, dev); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1533 | int ret = 0; |
| 1534 | |
| 1535 | priv = kzalloc(sizeof(struct twl6040_data), GFP_KERNEL); |
| 1536 | if (priv == NULL) |
| 1537 | return -ENOMEM; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1538 | snd_soc_codec_set_drvdata(codec, priv); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1539 | |
Jorge Eduardo Candelaria | a2d2362 | 2010-12-10 20:45:17 -0600 | [diff] [blame] | 1540 | priv->codec = codec; |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 1541 | codec->control_data = dev_get_drvdata(codec->dev->parent); |
Jorge Eduardo Candelaria | a2d2362 | 2010-12-10 20:45:17 -0600 | [diff] [blame] | 1542 | |
Axel Castaneda Gonzalez | 1fbe995 | 2011-02-23 20:08:28 -0600 | [diff] [blame] | 1543 | if (pdata && pdata->hs_left_step && pdata->hs_right_step) { |
| 1544 | priv->hs_left_step = pdata->hs_left_step; |
| 1545 | priv->hs_right_step = pdata->hs_right_step; |
| 1546 | } else { |
| 1547 | priv->hs_left_step = 1; |
| 1548 | priv->hs_right_step = 1; |
| 1549 | } |
| 1550 | |
| 1551 | if (pdata && pdata->hf_left_step && pdata->hf_right_step) { |
| 1552 | priv->hf_left_step = pdata->hf_left_step; |
| 1553 | priv->hf_right_step = pdata->hf_right_step; |
| 1554 | } else { |
| 1555 | priv->hf_left_step = 1; |
| 1556 | priv->hf_right_step = 1; |
| 1557 | } |
| 1558 | |
Peter Ujfalusi | 2a433b9 | 2011-07-04 19:52:26 +0300 | [diff] [blame] | 1559 | priv->plug_irq = platform_get_irq(pdev, 0); |
| 1560 | if (priv->plug_irq < 0) { |
| 1561 | dev_err(codec->dev, "invalid irq\n"); |
| 1562 | ret = -EINVAL; |
| 1563 | goto work_err; |
| 1564 | } |
| 1565 | |
Liam Girdwood | f7026c9 | 2011-03-07 15:25:23 +0000 | [diff] [blame] | 1566 | priv->sysclk_constraints = &hp_constraints; |
Jorge Eduardo Candelaria | a2d2362 | 2010-12-10 20:45:17 -0600 | [diff] [blame] | 1567 | priv->workqueue = create_singlethread_workqueue("twl6040-codec"); |
Axel Lin | 19aab08 | 2011-03-26 15:53:58 +0800 | [diff] [blame] | 1568 | if (!priv->workqueue) { |
| 1569 | ret = -ENOMEM; |
Jorge Eduardo Candelaria | a2d2362 | 2010-12-10 20:45:17 -0600 | [diff] [blame] | 1570 | goto work_err; |
Axel Lin | 19aab08 | 2011-03-26 15:53:58 +0800 | [diff] [blame] | 1571 | } |
Jorge Eduardo Candelaria | a2d2362 | 2010-12-10 20:45:17 -0600 | [diff] [blame] | 1572 | |
| 1573 | INIT_DELAYED_WORK(&priv->delayed_work, twl6040_accessory_work); |
| 1574 | |
| 1575 | mutex_init(&priv->mutex); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1576 | |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 1577 | init_completion(&priv->headset.ramp_done); |
| 1578 | init_completion(&priv->handsfree.ramp_done); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1579 | |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 1580 | priv->hf_workqueue = create_singlethread_workqueue("twl6040-hf"); |
| 1581 | if (priv->hf_workqueue == NULL) { |
| 1582 | ret = -ENOMEM; |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 1583 | goto hfwq_err; |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 1584 | } |
| 1585 | priv->hs_workqueue = create_singlethread_workqueue("twl6040-hs"); |
| 1586 | if (priv->hs_workqueue == NULL) { |
| 1587 | ret = -ENOMEM; |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 1588 | goto hswq_err; |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | INIT_DELAYED_WORK(&priv->hs_delayed_work, twl6040_pga_hs_work); |
| 1592 | INIT_DELAYED_WORK(&priv->hf_delayed_work, twl6040_pga_hf_work); |
| 1593 | |
Peter Ujfalusi | 2a433b9 | 2011-07-04 19:52:26 +0300 | [diff] [blame] | 1594 | ret = request_threaded_irq(priv->plug_irq, NULL, twl6040_audio_handler, |
| 1595 | 0, "twl6040_irq_plug", codec); |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 1596 | if (ret) { |
| 1597 | dev_err(codec->dev, "PLUG IRQ request failed: %d\n", ret); |
| 1598 | goto plugirq_err; |
| 1599 | } |
| 1600 | |
| 1601 | /* init vio registers */ |
| 1602 | twl6040_init_vio_regs(codec); |
| 1603 | |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1604 | /* power on device */ |
| 1605 | ret = twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
| 1606 | if (ret) |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 1607 | goto bias_err; |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1608 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1609 | snd_soc_add_controls(codec, twl6040_snd_controls, |
| 1610 | ARRAY_SIZE(twl6040_snd_controls)); |
| 1611 | twl6040_add_widgets(codec); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1612 | |
| 1613 | return 0; |
| 1614 | |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 1615 | bias_err: |
Peter Ujfalusi | 2a433b9 | 2011-07-04 19:52:26 +0300 | [diff] [blame] | 1616 | free_irq(priv->plug_irq, codec); |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 1617 | plugirq_err: |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 1618 | destroy_workqueue(priv->hs_workqueue); |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 1619 | hswq_err: |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 1620 | destroy_workqueue(priv->hf_workqueue); |
Misael Lopez Cruz | fb34d3d | 2011-05-01 21:27:00 -0500 | [diff] [blame] | 1621 | hfwq_err: |
Jorge Eduardo Candelaria | a2d2362 | 2010-12-10 20:45:17 -0600 | [diff] [blame] | 1622 | destroy_workqueue(priv->workqueue); |
| 1623 | work_err: |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1624 | kfree(priv); |
| 1625 | return ret; |
| 1626 | } |
| 1627 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1628 | static int twl6040_remove(struct snd_soc_codec *codec) |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1629 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1630 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1631 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1632 | twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF); |
Peter Ujfalusi | 2a433b9 | 2011-07-04 19:52:26 +0300 | [diff] [blame] | 1633 | free_irq(priv->plug_irq, codec); |
Jorge Eduardo Candelaria | a2d2362 | 2010-12-10 20:45:17 -0600 | [diff] [blame] | 1634 | destroy_workqueue(priv->workqueue); |
Margarita Olaya Cabrera | 1bf8475 | 2010-12-14 19:00:21 -0600 | [diff] [blame] | 1635 | destroy_workqueue(priv->hf_workqueue); |
| 1636 | destroy_workqueue(priv->hs_workqueue); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1637 | kfree(priv); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1638 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1639 | return 0; |
| 1640 | } |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1641 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1642 | static struct snd_soc_codec_driver soc_codec_dev_twl6040 = { |
| 1643 | .probe = twl6040_probe, |
| 1644 | .remove = twl6040_remove, |
| 1645 | .suspend = twl6040_suspend, |
| 1646 | .resume = twl6040_resume, |
| 1647 | .read = twl6040_read_reg_cache, |
| 1648 | .write = twl6040_write, |
| 1649 | .set_bias_level = twl6040_set_bias_level, |
| 1650 | .reg_cache_size = ARRAY_SIZE(twl6040_reg), |
| 1651 | .reg_word_size = sizeof(u8), |
| 1652 | .reg_cache_default = twl6040_reg, |
| 1653 | }; |
| 1654 | |
| 1655 | static int __devinit twl6040_codec_probe(struct platform_device *pdev) |
| 1656 | { |
Liam Girdwood | 6510bdc | 2011-02-11 17:37:51 +0000 | [diff] [blame] | 1657 | return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_twl6040, |
| 1658 | twl6040_dai, ARRAY_SIZE(twl6040_dai)); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | static int __devexit twl6040_codec_remove(struct platform_device *pdev) |
| 1662 | { |
| 1663 | snd_soc_unregister_codec(&pdev->dev); |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1664 | return 0; |
| 1665 | } |
| 1666 | |
| 1667 | static struct platform_driver twl6040_codec_driver = { |
| 1668 | .driver = { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1669 | .name = "twl6040-codec", |
Misael Lopez Cruz | 8ecbabd | 2010-03-19 11:25:51 +0000 | [diff] [blame] | 1670 | .owner = THIS_MODULE, |
| 1671 | }, |
| 1672 | .probe = twl6040_codec_probe, |
| 1673 | .remove = __devexit_p(twl6040_codec_remove), |
| 1674 | }; |
| 1675 | |
| 1676 | static int __init twl6040_codec_init(void) |
| 1677 | { |
| 1678 | return platform_driver_register(&twl6040_codec_driver); |
| 1679 | } |
| 1680 | module_init(twl6040_codec_init); |
| 1681 | |
| 1682 | static void __exit twl6040_codec_exit(void) |
| 1683 | { |
| 1684 | platform_driver_unregister(&twl6040_codec_driver); |
| 1685 | } |
| 1686 | module_exit(twl6040_codec_exit); |
| 1687 | |
| 1688 | MODULE_DESCRIPTION("ASoC TWL6040 codec driver"); |
| 1689 | MODULE_AUTHOR("Misael Lopez Cruz"); |
| 1690 | MODULE_LICENSE("GPL"); |