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