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