Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1 | /* |
| 2 | * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management |
| 3 | * |
| 4 | * Copyright 2005 Wolfson Microelectronics PLC. |
Liam Girdwood | d331124 | 2008-10-12 13:17:36 +0100 | [diff] [blame] | 5 | * Author: Liam Girdwood <lrg@slimlogic.co.uk> |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the |
| 9 | * Free Software Foundation; either version 2 of the License, or (at your |
| 10 | * option) any later version. |
| 11 | * |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 12 | * Features: |
| 13 | * o Changes power status of internal codec blocks depending on the |
| 14 | * dynamic configuration of codec internal audio paths and active |
Mark Brown | 74b8f95 | 2009-06-06 11:26:15 +0100 | [diff] [blame] | 15 | * DACs/ADCs. |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 16 | * o Platform power domain - can support external components i.e. amps and |
| 17 | * mic/meadphone insertion events. |
| 18 | * o Automatic Mic Bias support |
| 19 | * o Jack insertion power event initiation - e.g. hp insertion will enable |
| 20 | * sinks, dacs, etc |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 21 | * o Delayed powerdown of audio susbsystem to reduce pops between a quick |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 22 | * device reopen. |
| 23 | * |
| 24 | * Todo: |
| 25 | * o DAPM power change sequencing - allow for configurable per |
| 26 | * codec sequences. |
| 27 | * o Support for analogue bias optimisation. |
| 28 | * o Support for reduced codec oversampling rates. |
| 29 | * o Support for reduced codec bias currents. |
| 30 | */ |
| 31 | |
| 32 | #include <linux/module.h> |
| 33 | #include <linux/moduleparam.h> |
| 34 | #include <linux/init.h> |
| 35 | #include <linux/delay.h> |
| 36 | #include <linux/pm.h> |
| 37 | #include <linux/bitops.h> |
| 38 | #include <linux/platform_device.h> |
| 39 | #include <linux/jiffies.h> |
Takashi Iwai | 20496ff | 2009-08-24 09:40:34 +0200 | [diff] [blame] | 40 | #include <linux/debugfs.h> |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 41 | #include <sound/core.h> |
| 42 | #include <sound/pcm.h> |
| 43 | #include <sound/pcm_params.h> |
| 44 | #include <sound/soc-dapm.h> |
| 45 | #include <sound/initval.h> |
| 46 | |
| 47 | /* debug */ |
Mark Brown | c1286b8 | 2008-07-07 19:26:03 +0100 | [diff] [blame] | 48 | #ifdef DEBUG |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 49 | #define dump_dapm(codec, action) dbg_dump_dapm(codec, action) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 50 | #else |
| 51 | #define dump_dapm(codec, action) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 52 | #endif |
| 53 | |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 54 | /* dapm power sequences - make this per codec in the future */ |
| 55 | static int dapm_up_seq[] = { |
Mark Brown | 38357ab | 2009-06-06 19:03:23 +0100 | [diff] [blame] | 56 | [snd_soc_dapm_pre] = 0, |
| 57 | [snd_soc_dapm_supply] = 1, |
| 58 | [snd_soc_dapm_micbias] = 2, |
Mark Brown | 010ff26 | 2009-08-17 17:39:22 +0100 | [diff] [blame] | 59 | [snd_soc_dapm_aif_in] = 3, |
| 60 | [snd_soc_dapm_aif_out] = 3, |
| 61 | [snd_soc_dapm_mic] = 4, |
| 62 | [snd_soc_dapm_mux] = 5, |
| 63 | [snd_soc_dapm_value_mux] = 5, |
| 64 | [snd_soc_dapm_dac] = 6, |
| 65 | [snd_soc_dapm_mixer] = 7, |
| 66 | [snd_soc_dapm_mixer_named_ctl] = 7, |
| 67 | [snd_soc_dapm_pga] = 8, |
| 68 | [snd_soc_dapm_adc] = 9, |
| 69 | [snd_soc_dapm_hp] = 10, |
| 70 | [snd_soc_dapm_spk] = 10, |
| 71 | [snd_soc_dapm_post] = 11, |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 72 | }; |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 73 | |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 74 | static int dapm_down_seq[] = { |
Mark Brown | 38357ab | 2009-06-06 19:03:23 +0100 | [diff] [blame] | 75 | [snd_soc_dapm_pre] = 0, |
| 76 | [snd_soc_dapm_adc] = 1, |
| 77 | [snd_soc_dapm_hp] = 2, |
Mark Brown | 1ca0406 | 2009-08-17 16:26:59 +0100 | [diff] [blame] | 78 | [snd_soc_dapm_spk] = 2, |
Mark Brown | 38357ab | 2009-06-06 19:03:23 +0100 | [diff] [blame] | 79 | [snd_soc_dapm_pga] = 4, |
| 80 | [snd_soc_dapm_mixer_named_ctl] = 5, |
Mark Brown | e3d4dab | 2009-06-07 13:08:45 +0100 | [diff] [blame] | 81 | [snd_soc_dapm_mixer] = 5, |
| 82 | [snd_soc_dapm_dac] = 6, |
| 83 | [snd_soc_dapm_mic] = 7, |
| 84 | [snd_soc_dapm_micbias] = 8, |
| 85 | [snd_soc_dapm_mux] = 9, |
| 86 | [snd_soc_dapm_value_mux] = 9, |
Mark Brown | 010ff26 | 2009-08-17 17:39:22 +0100 | [diff] [blame] | 87 | [snd_soc_dapm_aif_in] = 10, |
| 88 | [snd_soc_dapm_aif_out] = 10, |
| 89 | [snd_soc_dapm_supply] = 11, |
| 90 | [snd_soc_dapm_post] = 12, |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 91 | }; |
| 92 | |
Troy Kisky | 12ef193 | 2008-10-13 17:42:14 -0700 | [diff] [blame] | 93 | static void pop_wait(u32 pop_time) |
Mark Brown | 15e4c72 | 2008-07-02 11:51:20 +0100 | [diff] [blame] | 94 | { |
| 95 | if (pop_time) |
| 96 | schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time)); |
| 97 | } |
| 98 | |
Troy Kisky | 12ef193 | 2008-10-13 17:42:14 -0700 | [diff] [blame] | 99 | static void pop_dbg(u32 pop_time, const char *fmt, ...) |
Mark Brown | 15e4c72 | 2008-07-02 11:51:20 +0100 | [diff] [blame] | 100 | { |
| 101 | va_list args; |
| 102 | |
| 103 | va_start(args, fmt); |
| 104 | |
| 105 | if (pop_time) { |
| 106 | vprintk(fmt, args); |
Troy Kisky | 12ef193 | 2008-10-13 17:42:14 -0700 | [diff] [blame] | 107 | pop_wait(pop_time); |
Mark Brown | 15e4c72 | 2008-07-02 11:51:20 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | va_end(args); |
| 111 | } |
| 112 | |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 113 | /* create a new dapm widget */ |
Takashi Iwai | 88cb429 | 2007-02-05 14:56:20 +0100 | [diff] [blame] | 114 | static inline struct snd_soc_dapm_widget *dapm_cnew_widget( |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 115 | const struct snd_soc_dapm_widget *_widget) |
| 116 | { |
Takashi Iwai | 88cb429 | 2007-02-05 14:56:20 +0100 | [diff] [blame] | 117 | return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 118 | } |
| 119 | |
Mark Brown | 452c5ea | 2009-05-17 21:41:23 +0100 | [diff] [blame] | 120 | /** |
| 121 | * snd_soc_dapm_set_bias_level - set the bias level for the system |
| 122 | * @socdev: audio device |
| 123 | * @level: level to configure |
| 124 | * |
| 125 | * Configure the bias (power) levels for the SoC audio device. |
| 126 | * |
| 127 | * Returns 0 for success else error. |
| 128 | */ |
| 129 | static int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev, |
| 130 | enum snd_soc_bias_level level) |
| 131 | { |
| 132 | struct snd_soc_card *card = socdev->card; |
| 133 | struct snd_soc_codec *codec = socdev->card->codec; |
| 134 | int ret = 0; |
| 135 | |
Mark Brown | f83fba8 | 2009-05-18 15:44:43 +0100 | [diff] [blame] | 136 | switch (level) { |
| 137 | case SND_SOC_BIAS_ON: |
| 138 | dev_dbg(socdev->dev, "Setting full bias\n"); |
| 139 | break; |
| 140 | case SND_SOC_BIAS_PREPARE: |
| 141 | dev_dbg(socdev->dev, "Setting bias prepare\n"); |
| 142 | break; |
| 143 | case SND_SOC_BIAS_STANDBY: |
| 144 | dev_dbg(socdev->dev, "Setting standby bias\n"); |
| 145 | break; |
| 146 | case SND_SOC_BIAS_OFF: |
| 147 | dev_dbg(socdev->dev, "Setting bias off\n"); |
| 148 | break; |
| 149 | default: |
| 150 | dev_err(socdev->dev, "Setting invalid bias %d\n", level); |
| 151 | return -EINVAL; |
| 152 | } |
| 153 | |
Mark Brown | 452c5ea | 2009-05-17 21:41:23 +0100 | [diff] [blame] | 154 | if (card->set_bias_level) |
| 155 | ret = card->set_bias_level(card, level); |
Mark Brown | 474e09c | 2009-08-19 14:18:53 +0100 | [diff] [blame] | 156 | if (ret == 0) { |
| 157 | if (codec->set_bias_level) |
| 158 | ret = codec->set_bias_level(codec, level); |
| 159 | else |
| 160 | codec->bias_level = level; |
| 161 | } |
Mark Brown | 452c5ea | 2009-05-17 21:41:23 +0100 | [diff] [blame] | 162 | |
| 163 | return ret; |
| 164 | } |
| 165 | |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 166 | /* set up initial codec paths */ |
| 167 | static void dapm_set_path_status(struct snd_soc_dapm_widget *w, |
| 168 | struct snd_soc_dapm_path *p, int i) |
| 169 | { |
| 170 | switch (w->id) { |
| 171 | case snd_soc_dapm_switch: |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 172 | case snd_soc_dapm_mixer: |
| 173 | case snd_soc_dapm_mixer_named_ctl: { |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 174 | int val; |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 175 | struct soc_mixer_control *mc = (struct soc_mixer_control *) |
| 176 | w->kcontrols[i].private_value; |
Jon Smirl | 815ecf8d | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 177 | unsigned int reg = mc->reg; |
| 178 | unsigned int shift = mc->shift; |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 179 | int max = mc->max; |
Jon Smirl | 815ecf8d | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 180 | unsigned int mask = (1 << fls(max)) - 1; |
| 181 | unsigned int invert = mc->invert; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 182 | |
| 183 | val = snd_soc_read(w->codec, reg); |
| 184 | val = (val >> shift) & mask; |
| 185 | |
| 186 | if ((invert && !val) || (!invert && val)) |
| 187 | p->connect = 1; |
| 188 | else |
| 189 | p->connect = 0; |
| 190 | } |
| 191 | break; |
| 192 | case snd_soc_dapm_mux: { |
| 193 | struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value; |
| 194 | int val, item, bitmask; |
| 195 | |
Jon Smirl | f8ba0b7 | 2008-07-29 11:42:27 +0100 | [diff] [blame] | 196 | for (bitmask = 1; bitmask < e->max; bitmask <<= 1) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 197 | ; |
| 198 | val = snd_soc_read(w->codec, e->reg); |
| 199 | item = (val >> e->shift_l) & (bitmask - 1); |
| 200 | |
| 201 | p->connect = 0; |
Jon Smirl | f8ba0b7 | 2008-07-29 11:42:27 +0100 | [diff] [blame] | 202 | for (i = 0; i < e->max; i++) { |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 203 | if (!(strcmp(p->name, e->texts[i])) && item == i) |
| 204 | p->connect = 1; |
| 205 | } |
| 206 | } |
| 207 | break; |
Peter Ujfalusi | 2e72f8e | 2009-01-05 09:54:57 +0200 | [diff] [blame] | 208 | case snd_soc_dapm_value_mux: { |
Peter Ujfalusi | 7415555 | 2009-01-08 13:34:29 +0200 | [diff] [blame] | 209 | struct soc_enum *e = (struct soc_enum *) |
Peter Ujfalusi | 2e72f8e | 2009-01-05 09:54:57 +0200 | [diff] [blame] | 210 | w->kcontrols[i].private_value; |
| 211 | int val, item; |
| 212 | |
| 213 | val = snd_soc_read(w->codec, e->reg); |
| 214 | val = (val >> e->shift_l) & e->mask; |
| 215 | for (item = 0; item < e->max; item++) { |
| 216 | if (val == e->values[item]) |
| 217 | break; |
| 218 | } |
| 219 | |
| 220 | p->connect = 0; |
| 221 | for (i = 0; i < e->max; i++) { |
| 222 | if (!(strcmp(p->name, e->texts[i])) && item == i) |
| 223 | p->connect = 1; |
| 224 | } |
| 225 | } |
| 226 | break; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 227 | /* does not effect routing - always connected */ |
| 228 | case snd_soc_dapm_pga: |
| 229 | case snd_soc_dapm_output: |
| 230 | case snd_soc_dapm_adc: |
| 231 | case snd_soc_dapm_input: |
| 232 | case snd_soc_dapm_dac: |
| 233 | case snd_soc_dapm_micbias: |
| 234 | case snd_soc_dapm_vmid: |
Mark Brown | 246d0a1 | 2009-04-22 18:24:55 +0100 | [diff] [blame] | 235 | case snd_soc_dapm_supply: |
Mark Brown | 010ff26 | 2009-08-17 17:39:22 +0100 | [diff] [blame] | 236 | case snd_soc_dapm_aif_in: |
| 237 | case snd_soc_dapm_aif_out: |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 238 | p->connect = 1; |
| 239 | break; |
| 240 | /* does effect routing - dynamically connected */ |
| 241 | case snd_soc_dapm_hp: |
| 242 | case snd_soc_dapm_mic: |
| 243 | case snd_soc_dapm_spk: |
| 244 | case snd_soc_dapm_line: |
| 245 | case snd_soc_dapm_pre: |
| 246 | case snd_soc_dapm_post: |
| 247 | p->connect = 0; |
| 248 | break; |
| 249 | } |
| 250 | } |
| 251 | |
Mark Brown | 74b8f95 | 2009-06-06 11:26:15 +0100 | [diff] [blame] | 252 | /* connect mux widget to its interconnecting audio paths */ |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 253 | static int dapm_connect_mux(struct snd_soc_codec *codec, |
| 254 | struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest, |
| 255 | struct snd_soc_dapm_path *path, const char *control_name, |
| 256 | const struct snd_kcontrol_new *kcontrol) |
| 257 | { |
| 258 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
| 259 | int i; |
| 260 | |
Jon Smirl | f8ba0b7 | 2008-07-29 11:42:27 +0100 | [diff] [blame] | 261 | for (i = 0; i < e->max; i++) { |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 262 | if (!(strcmp(control_name, e->texts[i]))) { |
| 263 | list_add(&path->list, &codec->dapm_paths); |
| 264 | list_add(&path->list_sink, &dest->sources); |
| 265 | list_add(&path->list_source, &src->sinks); |
| 266 | path->name = (char*)e->texts[i]; |
| 267 | dapm_set_path_status(dest, path, 0); |
| 268 | return 0; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | return -ENODEV; |
| 273 | } |
| 274 | |
Mark Brown | 74b8f95 | 2009-06-06 11:26:15 +0100 | [diff] [blame] | 275 | /* connect mixer widget to its interconnecting audio paths */ |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 276 | static int dapm_connect_mixer(struct snd_soc_codec *codec, |
| 277 | struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest, |
| 278 | struct snd_soc_dapm_path *path, const char *control_name) |
| 279 | { |
| 280 | int i; |
| 281 | |
| 282 | /* search for mixer kcontrol */ |
| 283 | for (i = 0; i < dest->num_kcontrols; i++) { |
| 284 | if (!strcmp(control_name, dest->kcontrols[i].name)) { |
| 285 | list_add(&path->list, &codec->dapm_paths); |
| 286 | list_add(&path->list_sink, &dest->sources); |
| 287 | list_add(&path->list_source, &src->sinks); |
| 288 | path->name = dest->kcontrols[i].name; |
| 289 | dapm_set_path_status(dest, path, i); |
| 290 | return 0; |
| 291 | } |
| 292 | } |
| 293 | return -ENODEV; |
| 294 | } |
| 295 | |
| 296 | /* update dapm codec register bits */ |
| 297 | static int dapm_update_bits(struct snd_soc_dapm_widget *widget) |
| 298 | { |
| 299 | int change, power; |
Daniel Ribeiro | 46f5822 | 2009-06-07 02:49:11 -0300 | [diff] [blame] | 300 | unsigned int old, new; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 301 | struct snd_soc_codec *codec = widget->codec; |
| 302 | |
| 303 | /* check for valid widgets */ |
| 304 | if (widget->reg < 0 || widget->id == snd_soc_dapm_input || |
| 305 | widget->id == snd_soc_dapm_output || |
| 306 | widget->id == snd_soc_dapm_hp || |
| 307 | widget->id == snd_soc_dapm_mic || |
| 308 | widget->id == snd_soc_dapm_line || |
| 309 | widget->id == snd_soc_dapm_spk) |
| 310 | return 0; |
| 311 | |
| 312 | power = widget->power; |
| 313 | if (widget->invert) |
| 314 | power = (power ? 0:1); |
| 315 | |
| 316 | old = snd_soc_read(codec, widget->reg); |
| 317 | new = (old & ~(0x1 << widget->shift)) | (power << widget->shift); |
| 318 | |
| 319 | change = old != new; |
| 320 | if (change) { |
Troy Kisky | 12ef193 | 2008-10-13 17:42:14 -0700 | [diff] [blame] | 321 | pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n", |
| 322 | widget->name, widget->power ? "on" : "off", |
| 323 | codec->pop_time); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 324 | snd_soc_write(codec, widget->reg, new); |
Troy Kisky | 12ef193 | 2008-10-13 17:42:14 -0700 | [diff] [blame] | 325 | pop_wait(codec->pop_time); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 326 | } |
Mark Brown | c1286b8 | 2008-07-07 19:26:03 +0100 | [diff] [blame] | 327 | pr_debug("reg %x old %x new %x change %d\n", widget->reg, |
| 328 | old, new, change); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 329 | return change; |
| 330 | } |
| 331 | |
| 332 | /* ramps the volume up or down to minimise pops before or after a |
| 333 | * DAPM power event */ |
| 334 | static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power) |
| 335 | { |
| 336 | const struct snd_kcontrol_new *k = widget->kcontrols; |
| 337 | |
| 338 | if (widget->muted && !power) |
| 339 | return 0; |
| 340 | if (!widget->muted && power) |
| 341 | return 0; |
| 342 | |
| 343 | if (widget->num_kcontrols && k) { |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 344 | struct soc_mixer_control *mc = |
| 345 | (struct soc_mixer_control *)k->private_value; |
Jon Smirl | 815ecf8d | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 346 | unsigned int reg = mc->reg; |
| 347 | unsigned int shift = mc->shift; |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 348 | int max = mc->max; |
Jon Smirl | 815ecf8d | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 349 | unsigned int mask = (1 << fls(max)) - 1; |
| 350 | unsigned int invert = mc->invert; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 351 | |
| 352 | if (power) { |
| 353 | int i; |
| 354 | /* power up has happended, increase volume to last level */ |
| 355 | if (invert) { |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 356 | for (i = max; i > widget->saved_value; i--) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 357 | snd_soc_update_bits(widget->codec, reg, mask, i); |
| 358 | } else { |
| 359 | for (i = 0; i < widget->saved_value; i++) |
| 360 | snd_soc_update_bits(widget->codec, reg, mask, i); |
| 361 | } |
| 362 | widget->muted = 0; |
| 363 | } else { |
| 364 | /* power down is about to occur, decrease volume to mute */ |
| 365 | int val = snd_soc_read(widget->codec, reg); |
| 366 | int i = widget->saved_value = (val >> shift) & mask; |
| 367 | if (invert) { |
| 368 | for (; i < mask; i++) |
| 369 | snd_soc_update_bits(widget->codec, reg, mask, i); |
| 370 | } else { |
| 371 | for (; i > 0; i--) |
| 372 | snd_soc_update_bits(widget->codec, reg, mask, i); |
| 373 | } |
| 374 | widget->muted = 1; |
| 375 | } |
| 376 | } |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | /* create new dapm mixer control */ |
| 381 | static int dapm_new_mixer(struct snd_soc_codec *codec, |
| 382 | struct snd_soc_dapm_widget *w) |
| 383 | { |
| 384 | int i, ret = 0; |
Mark Brown | 219b93f | 2008-10-28 13:02:31 +0000 | [diff] [blame] | 385 | size_t name_len; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 386 | struct snd_soc_dapm_path *path; |
| 387 | |
| 388 | /* add kcontrol */ |
| 389 | for (i = 0; i < w->num_kcontrols; i++) { |
| 390 | |
| 391 | /* match name */ |
| 392 | list_for_each_entry(path, &w->sources, list_sink) { |
| 393 | |
| 394 | /* mixer/mux paths name must match control name */ |
| 395 | if (path->name != (char*)w->kcontrols[i].name) |
| 396 | continue; |
| 397 | |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 398 | /* add dapm control with long name. |
| 399 | * for dapm_mixer this is the concatenation of the |
| 400 | * mixer and kcontrol name. |
| 401 | * for dapm_mixer_named_ctl this is simply the |
| 402 | * kcontrol name. |
| 403 | */ |
| 404 | name_len = strlen(w->kcontrols[i].name) + 1; |
Mark Brown | 07495f3 | 2009-03-05 17:06:23 +0000 | [diff] [blame] | 405 | if (w->id != snd_soc_dapm_mixer_named_ctl) |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 406 | name_len += 1 + strlen(w->name); |
| 407 | |
Mark Brown | 219b93f | 2008-10-28 13:02:31 +0000 | [diff] [blame] | 408 | path->long_name = kmalloc(name_len, GFP_KERNEL); |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 409 | |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 410 | if (path->long_name == NULL) |
| 411 | return -ENOMEM; |
| 412 | |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 413 | switch (w->id) { |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 414 | default: |
| 415 | snprintf(path->long_name, name_len, "%s %s", |
| 416 | w->name, w->kcontrols[i].name); |
Mark Brown | 07495f3 | 2009-03-05 17:06:23 +0000 | [diff] [blame] | 417 | break; |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 418 | case snd_soc_dapm_mixer_named_ctl: |
| 419 | snprintf(path->long_name, name_len, "%s", |
| 420 | w->kcontrols[i].name); |
Mark Brown | 07495f3 | 2009-03-05 17:06:23 +0000 | [diff] [blame] | 421 | break; |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Mark Brown | 219b93f | 2008-10-28 13:02:31 +0000 | [diff] [blame] | 424 | path->long_name[name_len - 1] = '\0'; |
| 425 | |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 426 | path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w, |
| 427 | path->long_name); |
| 428 | ret = snd_ctl_add(codec->card, path->kcontrol); |
| 429 | if (ret < 0) { |
Mark Brown | 6553e19 | 2009-04-06 16:59:32 +0100 | [diff] [blame] | 430 | printk(KERN_ERR "asoc: failed to add dapm kcontrol %s: %d\n", |
| 431 | path->long_name, |
| 432 | ret); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 433 | kfree(path->long_name); |
| 434 | path->long_name = NULL; |
| 435 | return ret; |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | return ret; |
| 440 | } |
| 441 | |
| 442 | /* create new dapm mux control */ |
| 443 | static int dapm_new_mux(struct snd_soc_codec *codec, |
| 444 | struct snd_soc_dapm_widget *w) |
| 445 | { |
| 446 | struct snd_soc_dapm_path *path = NULL; |
| 447 | struct snd_kcontrol *kcontrol; |
| 448 | int ret = 0; |
| 449 | |
| 450 | if (!w->num_kcontrols) { |
| 451 | printk(KERN_ERR "asoc: mux %s has no controls\n", w->name); |
| 452 | return -EINVAL; |
| 453 | } |
| 454 | |
| 455 | kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name); |
| 456 | ret = snd_ctl_add(codec->card, kcontrol); |
| 457 | if (ret < 0) |
| 458 | goto err; |
| 459 | |
| 460 | list_for_each_entry(path, &w->sources, list_sink) |
| 461 | path->kcontrol = kcontrol; |
| 462 | |
| 463 | return ret; |
| 464 | |
| 465 | err: |
| 466 | printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name); |
| 467 | return ret; |
| 468 | } |
| 469 | |
| 470 | /* create new dapm volume control */ |
| 471 | static int dapm_new_pga(struct snd_soc_codec *codec, |
| 472 | struct snd_soc_dapm_widget *w) |
| 473 | { |
| 474 | struct snd_kcontrol *kcontrol; |
| 475 | int ret = 0; |
| 476 | |
| 477 | if (!w->num_kcontrols) |
| 478 | return -EINVAL; |
| 479 | |
| 480 | kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name); |
| 481 | ret = snd_ctl_add(codec->card, kcontrol); |
| 482 | if (ret < 0) { |
| 483 | printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name); |
| 484 | return ret; |
| 485 | } |
| 486 | |
| 487 | return ret; |
| 488 | } |
| 489 | |
| 490 | /* reset 'walked' bit for each dapm path */ |
| 491 | static inline void dapm_clear_walk(struct snd_soc_codec *codec) |
| 492 | { |
| 493 | struct snd_soc_dapm_path *p; |
| 494 | |
| 495 | list_for_each_entry(p, &codec->dapm_paths, list) |
| 496 | p->walked = 0; |
| 497 | } |
| 498 | |
| 499 | /* |
| 500 | * Recursively check for a completed path to an active or physically connected |
| 501 | * output widget. Returns number of complete paths. |
| 502 | */ |
| 503 | static int is_connected_output_ep(struct snd_soc_dapm_widget *widget) |
| 504 | { |
| 505 | struct snd_soc_dapm_path *path; |
| 506 | int con = 0; |
| 507 | |
Mark Brown | 246d0a1 | 2009-04-22 18:24:55 +0100 | [diff] [blame] | 508 | if (widget->id == snd_soc_dapm_supply) |
| 509 | return 0; |
| 510 | |
Mark Brown | 010ff26 | 2009-08-17 17:39:22 +0100 | [diff] [blame] | 511 | switch (widget->id) { |
| 512 | case snd_soc_dapm_adc: |
| 513 | case snd_soc_dapm_aif_out: |
| 514 | if (widget->active) |
| 515 | return 1; |
| 516 | default: |
| 517 | break; |
| 518 | } |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 519 | |
| 520 | if (widget->connected) { |
| 521 | /* connected pin ? */ |
| 522 | if (widget->id == snd_soc_dapm_output && !widget->ext) |
| 523 | return 1; |
| 524 | |
| 525 | /* connected jack or spk ? */ |
| 526 | if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk || |
Peter Ujfalusi | eaeae5d | 2009-09-30 09:27:24 +0300 | [diff] [blame] | 527 | (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources))) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 528 | return 1; |
| 529 | } |
| 530 | |
| 531 | list_for_each_entry(path, &widget->sinks, list_source) { |
| 532 | if (path->walked) |
| 533 | continue; |
| 534 | |
| 535 | if (path->sink && path->connect) { |
| 536 | path->walked = 1; |
| 537 | con += is_connected_output_ep(path->sink); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | return con; |
| 542 | } |
| 543 | |
| 544 | /* |
| 545 | * Recursively check for a completed path to an active or physically connected |
| 546 | * input widget. Returns number of complete paths. |
| 547 | */ |
| 548 | static int is_connected_input_ep(struct snd_soc_dapm_widget *widget) |
| 549 | { |
| 550 | struct snd_soc_dapm_path *path; |
| 551 | int con = 0; |
| 552 | |
Mark Brown | 246d0a1 | 2009-04-22 18:24:55 +0100 | [diff] [blame] | 553 | if (widget->id == snd_soc_dapm_supply) |
| 554 | return 0; |
| 555 | |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 556 | /* active stream ? */ |
Mark Brown | 010ff26 | 2009-08-17 17:39:22 +0100 | [diff] [blame] | 557 | switch (widget->id) { |
| 558 | case snd_soc_dapm_dac: |
| 559 | case snd_soc_dapm_aif_in: |
| 560 | if (widget->active) |
| 561 | return 1; |
| 562 | default: |
| 563 | break; |
| 564 | } |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 565 | |
| 566 | if (widget->connected) { |
| 567 | /* connected pin ? */ |
| 568 | if (widget->id == snd_soc_dapm_input && !widget->ext) |
| 569 | return 1; |
| 570 | |
| 571 | /* connected VMID/Bias for lower pops */ |
| 572 | if (widget->id == snd_soc_dapm_vmid) |
| 573 | return 1; |
| 574 | |
| 575 | /* connected jack ? */ |
Peter Ujfalusi | eaeae5d | 2009-09-30 09:27:24 +0300 | [diff] [blame] | 576 | if (widget->id == snd_soc_dapm_mic || |
| 577 | (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks))) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 578 | return 1; |
| 579 | } |
| 580 | |
| 581 | list_for_each_entry(path, &widget->sources, list_sink) { |
| 582 | if (path->walked) |
| 583 | continue; |
| 584 | |
| 585 | if (path->source && path->connect) { |
| 586 | path->walked = 1; |
| 587 | con += is_connected_input_ep(path->source); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | return con; |
| 592 | } |
| 593 | |
| 594 | /* |
Jarkko Nikula | e2be2cc | 2008-06-25 14:42:07 +0300 | [diff] [blame] | 595 | * Handler for generic register modifier widget. |
| 596 | */ |
| 597 | int dapm_reg_event(struct snd_soc_dapm_widget *w, |
| 598 | struct snd_kcontrol *kcontrol, int event) |
| 599 | { |
| 600 | unsigned int val; |
| 601 | |
| 602 | if (SND_SOC_DAPM_EVENT_ON(event)) |
| 603 | val = w->on_val; |
| 604 | else |
| 605 | val = w->off_val; |
| 606 | |
| 607 | snd_soc_update_bits(w->codec, -(w->reg + 1), |
| 608 | w->mask << w->shift, val << w->shift); |
| 609 | |
| 610 | return 0; |
| 611 | } |
Mark Brown | 1158941 | 2008-07-29 11:42:23 +0100 | [diff] [blame] | 612 | EXPORT_SYMBOL_GPL(dapm_reg_event); |
Jarkko Nikula | e2be2cc | 2008-06-25 14:42:07 +0300 | [diff] [blame] | 613 | |
Mark Brown | 025756e | 2009-04-13 11:09:18 +0100 | [diff] [blame] | 614 | /* Standard power change method, used to apply power changes to most |
| 615 | * widgets. |
| 616 | */ |
| 617 | static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w) |
| 618 | { |
| 619 | int ret; |
| 620 | |
| 621 | /* call any power change event handlers */ |
| 622 | if (w->event) |
| 623 | pr_debug("power %s event for %s flags %x\n", |
| 624 | w->power ? "on" : "off", |
| 625 | w->name, w->event_flags); |
| 626 | |
| 627 | /* power up pre event */ |
| 628 | if (w->power && w->event && |
| 629 | (w->event_flags & SND_SOC_DAPM_PRE_PMU)) { |
| 630 | ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU); |
| 631 | if (ret < 0) |
| 632 | return ret; |
| 633 | } |
| 634 | |
| 635 | /* power down pre event */ |
| 636 | if (!w->power && w->event && |
| 637 | (w->event_flags & SND_SOC_DAPM_PRE_PMD)) { |
| 638 | ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD); |
| 639 | if (ret < 0) |
| 640 | return ret; |
| 641 | } |
| 642 | |
| 643 | /* Lower PGA volume to reduce pops */ |
| 644 | if (w->id == snd_soc_dapm_pga && !w->power) |
| 645 | dapm_set_pga(w, w->power); |
| 646 | |
| 647 | dapm_update_bits(w); |
| 648 | |
| 649 | /* Raise PGA volume to reduce pops */ |
| 650 | if (w->id == snd_soc_dapm_pga && w->power) |
| 651 | dapm_set_pga(w, w->power); |
| 652 | |
| 653 | /* power up post event */ |
| 654 | if (w->power && w->event && |
| 655 | (w->event_flags & SND_SOC_DAPM_POST_PMU)) { |
| 656 | ret = w->event(w, |
| 657 | NULL, SND_SOC_DAPM_POST_PMU); |
| 658 | if (ret < 0) |
| 659 | return ret; |
| 660 | } |
| 661 | |
| 662 | /* power down post event */ |
| 663 | if (!w->power && w->event && |
| 664 | (w->event_flags & SND_SOC_DAPM_POST_PMD)) { |
| 665 | ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD); |
| 666 | if (ret < 0) |
| 667 | return ret; |
| 668 | } |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
Mark Brown | cd0f2d4 | 2009-04-20 16:56:59 +0100 | [diff] [blame] | 673 | /* Generic check to see if a widget should be powered. |
| 674 | */ |
| 675 | static int dapm_generic_check_power(struct snd_soc_dapm_widget *w) |
| 676 | { |
| 677 | int in, out; |
| 678 | |
| 679 | in = is_connected_input_ep(w); |
| 680 | dapm_clear_walk(w->codec); |
| 681 | out = is_connected_output_ep(w); |
| 682 | dapm_clear_walk(w->codec); |
| 683 | return out != 0 && in != 0; |
| 684 | } |
| 685 | |
Mark Brown | 6ea31b9 | 2009-04-20 17:15:41 +0100 | [diff] [blame] | 686 | /* Check to see if an ADC has power */ |
| 687 | static int dapm_adc_check_power(struct snd_soc_dapm_widget *w) |
| 688 | { |
| 689 | int in; |
| 690 | |
| 691 | if (w->active) { |
| 692 | in = is_connected_input_ep(w); |
| 693 | dapm_clear_walk(w->codec); |
| 694 | return in != 0; |
| 695 | } else { |
| 696 | return dapm_generic_check_power(w); |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | /* Check to see if a DAC has power */ |
| 701 | static int dapm_dac_check_power(struct snd_soc_dapm_widget *w) |
| 702 | { |
| 703 | int out; |
| 704 | |
| 705 | if (w->active) { |
| 706 | out = is_connected_output_ep(w); |
| 707 | dapm_clear_walk(w->codec); |
| 708 | return out != 0; |
| 709 | } else { |
| 710 | return dapm_generic_check_power(w); |
| 711 | } |
| 712 | } |
| 713 | |
Mark Brown | 246d0a1 | 2009-04-22 18:24:55 +0100 | [diff] [blame] | 714 | /* Check to see if a power supply is needed */ |
| 715 | static int dapm_supply_check_power(struct snd_soc_dapm_widget *w) |
| 716 | { |
| 717 | struct snd_soc_dapm_path *path; |
| 718 | int power = 0; |
| 719 | |
| 720 | /* Check if one of our outputs is connected */ |
| 721 | list_for_each_entry(path, &w->sinks, list_source) { |
Mark Brown | 215edda | 2009-09-08 18:59:05 +0100 | [diff] [blame] | 722 | if (path->connected && |
| 723 | !path->connected(path->source, path->sink)) |
| 724 | continue; |
| 725 | |
Mark Brown | 246d0a1 | 2009-04-22 18:24:55 +0100 | [diff] [blame] | 726 | if (path->sink && path->sink->power_check && |
| 727 | path->sink->power_check(path->sink)) { |
| 728 | power = 1; |
| 729 | break; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | dapm_clear_walk(w->codec); |
| 734 | |
| 735 | return power; |
| 736 | } |
| 737 | |
Mark Brown | 38357ab | 2009-06-06 19:03:23 +0100 | [diff] [blame] | 738 | static int dapm_seq_compare(struct snd_soc_dapm_widget *a, |
| 739 | struct snd_soc_dapm_widget *b, |
| 740 | int sort[]) |
Mark Brown | 42aa341 | 2009-03-01 19:21:10 +0000 | [diff] [blame] | 741 | { |
Mark Brown | 38357ab | 2009-06-06 19:03:23 +0100 | [diff] [blame] | 742 | if (sort[a->id] != sort[b->id]) |
| 743 | return sort[a->id] - sort[b->id]; |
Mark Brown | b22ead2 | 2009-06-07 12:51:26 +0100 | [diff] [blame] | 744 | if (a->reg != b->reg) |
| 745 | return a->reg - b->reg; |
Mark Brown | 42aa341 | 2009-03-01 19:21:10 +0000 | [diff] [blame] | 746 | |
Mark Brown | 38357ab | 2009-06-06 19:03:23 +0100 | [diff] [blame] | 747 | return 0; |
| 748 | } |
Mark Brown | 42aa341 | 2009-03-01 19:21:10 +0000 | [diff] [blame] | 749 | |
Mark Brown | 38357ab | 2009-06-06 19:03:23 +0100 | [diff] [blame] | 750 | /* Insert a widget in order into a DAPM power sequence. */ |
| 751 | static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget, |
| 752 | struct list_head *list, |
| 753 | int sort[]) |
| 754 | { |
| 755 | struct snd_soc_dapm_widget *w; |
| 756 | |
| 757 | list_for_each_entry(w, list, power_list) |
| 758 | if (dapm_seq_compare(new_widget, w, sort) < 0) { |
| 759 | list_add_tail(&new_widget->power_list, &w->power_list); |
| 760 | return; |
Mark Brown | 42aa341 | 2009-03-01 19:21:10 +0000 | [diff] [blame] | 761 | } |
Mark Brown | 6ea31b9 | 2009-04-20 17:15:41 +0100 | [diff] [blame] | 762 | |
Mark Brown | 38357ab | 2009-06-06 19:03:23 +0100 | [diff] [blame] | 763 | list_add_tail(&new_widget->power_list, list); |
| 764 | } |
Mark Brown | 42aa341 | 2009-03-01 19:21:10 +0000 | [diff] [blame] | 765 | |
Mark Brown | b22ead2 | 2009-06-07 12:51:26 +0100 | [diff] [blame] | 766 | /* Apply the coalesced changes from a DAPM sequence */ |
| 767 | static void dapm_seq_run_coalesced(struct snd_soc_codec *codec, |
| 768 | struct list_head *pending) |
Mark Brown | 163cac0 | 2009-06-07 10:12:52 +0100 | [diff] [blame] | 769 | { |
| 770 | struct snd_soc_dapm_widget *w; |
Mark Brown | 8162810 | 2009-06-07 13:21:24 +0100 | [diff] [blame] | 771 | int reg, power, ret; |
Mark Brown | b22ead2 | 2009-06-07 12:51:26 +0100 | [diff] [blame] | 772 | unsigned int value = 0; |
| 773 | unsigned int mask = 0; |
| 774 | unsigned int cur_mask; |
| 775 | |
| 776 | reg = list_first_entry(pending, struct snd_soc_dapm_widget, |
| 777 | power_list)->reg; |
| 778 | |
| 779 | list_for_each_entry(w, pending, power_list) { |
| 780 | cur_mask = 1 << w->shift; |
| 781 | BUG_ON(reg != w->reg); |
| 782 | |
| 783 | if (w->invert) |
| 784 | power = !w->power; |
| 785 | else |
| 786 | power = w->power; |
| 787 | |
| 788 | mask |= cur_mask; |
| 789 | if (power) |
| 790 | value |= cur_mask; |
| 791 | |
| 792 | pop_dbg(codec->pop_time, |
| 793 | "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n", |
| 794 | w->name, reg, value, mask); |
Mark Brown | 8162810 | 2009-06-07 13:21:24 +0100 | [diff] [blame] | 795 | |
| 796 | /* power up pre event */ |
| 797 | if (w->power && w->event && |
| 798 | (w->event_flags & SND_SOC_DAPM_PRE_PMU)) { |
| 799 | pop_dbg(codec->pop_time, "pop test : %s PRE_PMU\n", |
| 800 | w->name); |
| 801 | ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU); |
| 802 | if (ret < 0) |
| 803 | pr_err("%s: pre event failed: %d\n", |
| 804 | w->name, ret); |
| 805 | } |
| 806 | |
| 807 | /* power down pre event */ |
| 808 | if (!w->power && w->event && |
| 809 | (w->event_flags & SND_SOC_DAPM_PRE_PMD)) { |
| 810 | pop_dbg(codec->pop_time, "pop test : %s PRE_PMD\n", |
| 811 | w->name); |
| 812 | ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD); |
| 813 | if (ret < 0) |
| 814 | pr_err("%s: pre event failed: %d\n", |
| 815 | w->name, ret); |
| 816 | } |
Mark Brown | 4f1c192 | 2009-06-07 13:37:44 +0100 | [diff] [blame] | 817 | |
| 818 | /* Lower PGA volume to reduce pops */ |
| 819 | if (w->id == snd_soc_dapm_pga && !w->power) |
| 820 | dapm_set_pga(w, w->power); |
Mark Brown | b22ead2 | 2009-06-07 12:51:26 +0100 | [diff] [blame] | 821 | } |
| 822 | |
Mark Brown | 8162810 | 2009-06-07 13:21:24 +0100 | [diff] [blame] | 823 | if (reg >= 0) { |
| 824 | pop_dbg(codec->pop_time, |
| 825 | "pop test : Applying 0x%x/0x%x to %x in %dms\n", |
| 826 | value, mask, reg, codec->pop_time); |
| 827 | pop_wait(codec->pop_time); |
| 828 | snd_soc_update_bits(codec, reg, mask, value); |
| 829 | } |
| 830 | |
| 831 | list_for_each_entry(w, pending, power_list) { |
Mark Brown | 4f1c192 | 2009-06-07 13:37:44 +0100 | [diff] [blame] | 832 | /* Raise PGA volume to reduce pops */ |
| 833 | if (w->id == snd_soc_dapm_pga && w->power) |
| 834 | dapm_set_pga(w, w->power); |
| 835 | |
Mark Brown | 8162810 | 2009-06-07 13:21:24 +0100 | [diff] [blame] | 836 | /* power up post event */ |
| 837 | if (w->power && w->event && |
| 838 | (w->event_flags & SND_SOC_DAPM_POST_PMU)) { |
| 839 | pop_dbg(codec->pop_time, "pop test : %s POST_PMU\n", |
| 840 | w->name); |
Mark Brown | 42aa341 | 2009-03-01 19:21:10 +0000 | [diff] [blame] | 841 | ret = w->event(w, |
| 842 | NULL, SND_SOC_DAPM_POST_PMU); |
| 843 | if (ret < 0) |
Mark Brown | 8162810 | 2009-06-07 13:21:24 +0100 | [diff] [blame] | 844 | pr_err("%s: post event failed: %d\n", |
| 845 | w->name, ret); |
Mark Brown | 42aa341 | 2009-03-01 19:21:10 +0000 | [diff] [blame] | 846 | } |
Mark Brown | 6ea31b9 | 2009-04-20 17:15:41 +0100 | [diff] [blame] | 847 | |
Mark Brown | 8162810 | 2009-06-07 13:21:24 +0100 | [diff] [blame] | 848 | /* power down post event */ |
| 849 | if (!w->power && w->event && |
| 850 | (w->event_flags & SND_SOC_DAPM_POST_PMD)) { |
| 851 | pop_dbg(codec->pop_time, "pop test : %s POST_PMD\n", |
| 852 | w->name); |
| 853 | ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD); |
| 854 | if (ret < 0) |
| 855 | pr_err("%s: post event failed: %d\n", |
| 856 | w->name, ret); |
| 857 | } |
Mark Brown | 42aa341 | 2009-03-01 19:21:10 +0000 | [diff] [blame] | 858 | } |
Mark Brown | 42aa341 | 2009-03-01 19:21:10 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Mark Brown | b22ead2 | 2009-06-07 12:51:26 +0100 | [diff] [blame] | 861 | /* Apply a DAPM power sequence. |
| 862 | * |
| 863 | * We walk over a pre-sorted list of widgets to apply power to. In |
| 864 | * order to minimise the number of writes to the device required |
| 865 | * multiple widgets will be updated in a single write where possible. |
| 866 | * Currently anything that requires more than a single write is not |
| 867 | * handled. |
| 868 | */ |
| 869 | static void dapm_seq_run(struct snd_soc_codec *codec, struct list_head *list, |
| 870 | int event, int sort[]) |
| 871 | { |
| 872 | struct snd_soc_dapm_widget *w, *n; |
| 873 | LIST_HEAD(pending); |
| 874 | int cur_sort = -1; |
| 875 | int cur_reg = SND_SOC_NOPM; |
Mark Brown | 163cac0 | 2009-06-07 10:12:52 +0100 | [diff] [blame] | 876 | int ret; |
| 877 | |
Mark Brown | b22ead2 | 2009-06-07 12:51:26 +0100 | [diff] [blame] | 878 | list_for_each_entry_safe(w, n, list, power_list) { |
| 879 | ret = 0; |
| 880 | |
| 881 | /* Do we need to apply any queued changes? */ |
| 882 | if (sort[w->id] != cur_sort || w->reg != cur_reg) { |
| 883 | if (!list_empty(&pending)) |
| 884 | dapm_seq_run_coalesced(codec, &pending); |
| 885 | |
| 886 | INIT_LIST_HEAD(&pending); |
| 887 | cur_sort = -1; |
| 888 | cur_reg = SND_SOC_NOPM; |
| 889 | } |
| 890 | |
Mark Brown | 163cac0 | 2009-06-07 10:12:52 +0100 | [diff] [blame] | 891 | switch (w->id) { |
| 892 | case snd_soc_dapm_pre: |
| 893 | if (!w->event) |
Mark Brown | b22ead2 | 2009-06-07 12:51:26 +0100 | [diff] [blame] | 894 | list_for_each_entry_safe_continue(w, n, list, |
| 895 | power_list); |
Mark Brown | 163cac0 | 2009-06-07 10:12:52 +0100 | [diff] [blame] | 896 | |
Mark Brown | b22ead2 | 2009-06-07 12:51:26 +0100 | [diff] [blame] | 897 | if (event == SND_SOC_DAPM_STREAM_START) |
Mark Brown | 163cac0 | 2009-06-07 10:12:52 +0100 | [diff] [blame] | 898 | ret = w->event(w, |
| 899 | NULL, SND_SOC_DAPM_PRE_PMU); |
Mark Brown | b22ead2 | 2009-06-07 12:51:26 +0100 | [diff] [blame] | 900 | else if (event == SND_SOC_DAPM_STREAM_STOP) |
Mark Brown | 163cac0 | 2009-06-07 10:12:52 +0100 | [diff] [blame] | 901 | ret = w->event(w, |
| 902 | NULL, SND_SOC_DAPM_PRE_PMD); |
Mark Brown | 163cac0 | 2009-06-07 10:12:52 +0100 | [diff] [blame] | 903 | break; |
| 904 | |
| 905 | case snd_soc_dapm_post: |
| 906 | if (!w->event) |
Mark Brown | b22ead2 | 2009-06-07 12:51:26 +0100 | [diff] [blame] | 907 | list_for_each_entry_safe_continue(w, n, list, |
| 908 | power_list); |
Mark Brown | 163cac0 | 2009-06-07 10:12:52 +0100 | [diff] [blame] | 909 | |
Mark Brown | b22ead2 | 2009-06-07 12:51:26 +0100 | [diff] [blame] | 910 | if (event == SND_SOC_DAPM_STREAM_START) |
Mark Brown | 163cac0 | 2009-06-07 10:12:52 +0100 | [diff] [blame] | 911 | ret = w->event(w, |
| 912 | NULL, SND_SOC_DAPM_POST_PMU); |
Mark Brown | b22ead2 | 2009-06-07 12:51:26 +0100 | [diff] [blame] | 913 | else if (event == SND_SOC_DAPM_STREAM_STOP) |
Mark Brown | 163cac0 | 2009-06-07 10:12:52 +0100 | [diff] [blame] | 914 | ret = w->event(w, |
| 915 | NULL, SND_SOC_DAPM_POST_PMD); |
Mark Brown | b22ead2 | 2009-06-07 12:51:26 +0100 | [diff] [blame] | 916 | break; |
| 917 | |
| 918 | case snd_soc_dapm_input: |
| 919 | case snd_soc_dapm_output: |
| 920 | case snd_soc_dapm_hp: |
| 921 | case snd_soc_dapm_mic: |
| 922 | case snd_soc_dapm_line: |
| 923 | case snd_soc_dapm_spk: |
| 924 | /* No register support currently */ |
Mark Brown | b22ead2 | 2009-06-07 12:51:26 +0100 | [diff] [blame] | 925 | ret = dapm_generic_apply_power(w); |
Mark Brown | 163cac0 | 2009-06-07 10:12:52 +0100 | [diff] [blame] | 926 | break; |
| 927 | |
| 928 | default: |
Mark Brown | 8162810 | 2009-06-07 13:21:24 +0100 | [diff] [blame] | 929 | /* Queue it up for application */ |
| 930 | cur_sort = sort[w->id]; |
| 931 | cur_reg = w->reg; |
| 932 | list_move(&w->power_list, &pending); |
| 933 | break; |
Mark Brown | 163cac0 | 2009-06-07 10:12:52 +0100 | [diff] [blame] | 934 | } |
Mark Brown | b22ead2 | 2009-06-07 12:51:26 +0100 | [diff] [blame] | 935 | |
| 936 | if (ret < 0) |
| 937 | pr_err("Failed to apply widget power: %d\n", |
| 938 | ret); |
Mark Brown | 163cac0 | 2009-06-07 10:12:52 +0100 | [diff] [blame] | 939 | } |
Mark Brown | b22ead2 | 2009-06-07 12:51:26 +0100 | [diff] [blame] | 940 | |
| 941 | if (!list_empty(&pending)) |
| 942 | dapm_seq_run_coalesced(codec, &pending); |
Mark Brown | 163cac0 | 2009-06-07 10:12:52 +0100 | [diff] [blame] | 943 | } |
| 944 | |
Mark Brown | 42aa341 | 2009-03-01 19:21:10 +0000 | [diff] [blame] | 945 | /* |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 946 | * Scan each dapm widget for complete audio path. |
| 947 | * A complete path is a route that has valid endpoints i.e.:- |
| 948 | * |
| 949 | * o DAC to output pin. |
| 950 | * o Input Pin to ADC. |
| 951 | * o Input pin to Output pin (bypass, sidetone) |
| 952 | * o DAC to ADC (loopback). |
| 953 | */ |
Adrian Bunk | d9c96cf | 2006-11-28 12:10:09 +0100 | [diff] [blame] | 954 | static int dapm_power_widgets(struct snd_soc_codec *codec, int event) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 955 | { |
Mark Brown | 452c5ea | 2009-05-17 21:41:23 +0100 | [diff] [blame] | 956 | struct snd_soc_device *socdev = codec->socdev; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 957 | struct snd_soc_dapm_widget *w; |
Mark Brown | 291f3bb | 2009-06-07 13:57:17 +0100 | [diff] [blame] | 958 | LIST_HEAD(up_list); |
| 959 | LIST_HEAD(down_list); |
Mark Brown | 6d3ddc8 | 2009-05-16 17:47:29 +0100 | [diff] [blame] | 960 | int ret = 0; |
Mark Brown | 38357ab | 2009-06-06 19:03:23 +0100 | [diff] [blame] | 961 | int power; |
Mark Brown | 452c5ea | 2009-05-17 21:41:23 +0100 | [diff] [blame] | 962 | int sys_power = 0; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 963 | |
Mark Brown | 6d3ddc8 | 2009-05-16 17:47:29 +0100 | [diff] [blame] | 964 | /* Check which widgets we need to power and store them in |
| 965 | * lists indicating if they should be powered up or down. |
| 966 | */ |
| 967 | list_for_each_entry(w, &codec->dapm_widgets, list) { |
| 968 | switch (w->id) { |
| 969 | case snd_soc_dapm_pre: |
Mark Brown | 291f3bb | 2009-06-07 13:57:17 +0100 | [diff] [blame] | 970 | dapm_seq_insert(w, &down_list, dapm_down_seq); |
Mark Brown | 6d3ddc8 | 2009-05-16 17:47:29 +0100 | [diff] [blame] | 971 | break; |
| 972 | case snd_soc_dapm_post: |
Mark Brown | 291f3bb | 2009-06-07 13:57:17 +0100 | [diff] [blame] | 973 | dapm_seq_insert(w, &up_list, dapm_up_seq); |
Mark Brown | 6d3ddc8 | 2009-05-16 17:47:29 +0100 | [diff] [blame] | 974 | break; |
| 975 | |
| 976 | default: |
| 977 | if (!w->power_check) |
| 978 | continue; |
| 979 | |
| 980 | power = w->power_check(w); |
Mark Brown | 452c5ea | 2009-05-17 21:41:23 +0100 | [diff] [blame] | 981 | if (power) |
| 982 | sys_power = 1; |
| 983 | |
Mark Brown | 6d3ddc8 | 2009-05-16 17:47:29 +0100 | [diff] [blame] | 984 | if (w->power == power) |
| 985 | continue; |
| 986 | |
| 987 | if (power) |
Mark Brown | 291f3bb | 2009-06-07 13:57:17 +0100 | [diff] [blame] | 988 | dapm_seq_insert(w, &up_list, dapm_up_seq); |
Mark Brown | 6d3ddc8 | 2009-05-16 17:47:29 +0100 | [diff] [blame] | 989 | else |
Mark Brown | 291f3bb | 2009-06-07 13:57:17 +0100 | [diff] [blame] | 990 | dapm_seq_insert(w, &down_list, dapm_down_seq); |
Mark Brown | 6d3ddc8 | 2009-05-16 17:47:29 +0100 | [diff] [blame] | 991 | |
| 992 | w->power = power; |
| 993 | break; |
| 994 | } |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 995 | } |
| 996 | |
Mark Brown | b14b76a | 2009-08-17 11:55:38 +0100 | [diff] [blame] | 997 | /* If there are no DAPM widgets then try to figure out power from the |
| 998 | * event type. |
| 999 | */ |
| 1000 | if (list_empty(&codec->dapm_widgets)) { |
| 1001 | switch (event) { |
| 1002 | case SND_SOC_DAPM_STREAM_START: |
| 1003 | case SND_SOC_DAPM_STREAM_RESUME: |
| 1004 | sys_power = 1; |
| 1005 | break; |
| 1006 | case SND_SOC_DAPM_STREAM_NOP: |
| 1007 | sys_power = codec->bias_level != SND_SOC_BIAS_STANDBY; |
| 1008 | default: |
| 1009 | break; |
| 1010 | } |
| 1011 | } |
| 1012 | |
Mark Brown | 452c5ea | 2009-05-17 21:41:23 +0100 | [diff] [blame] | 1013 | /* If we're changing to all on or all off then prepare */ |
| 1014 | if ((sys_power && codec->bias_level == SND_SOC_BIAS_STANDBY) || |
| 1015 | (!sys_power && codec->bias_level == SND_SOC_BIAS_ON)) { |
| 1016 | ret = snd_soc_dapm_set_bias_level(socdev, |
| 1017 | SND_SOC_BIAS_PREPARE); |
| 1018 | if (ret != 0) |
| 1019 | pr_err("Failed to prepare bias: %d\n", ret); |
| 1020 | } |
| 1021 | |
Mark Brown | 6d3ddc8 | 2009-05-16 17:47:29 +0100 | [diff] [blame] | 1022 | /* Power down widgets first; try to avoid amplifying pops. */ |
Mark Brown | 291f3bb | 2009-06-07 13:57:17 +0100 | [diff] [blame] | 1023 | dapm_seq_run(codec, &down_list, event, dapm_down_seq); |
Mark Brown | 6d3ddc8 | 2009-05-16 17:47:29 +0100 | [diff] [blame] | 1024 | |
| 1025 | /* Now power up. */ |
Mark Brown | 291f3bb | 2009-06-07 13:57:17 +0100 | [diff] [blame] | 1026 | dapm_seq_run(codec, &up_list, event, dapm_up_seq); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1027 | |
Mark Brown | 452c5ea | 2009-05-17 21:41:23 +0100 | [diff] [blame] | 1028 | /* If we just powered the last thing off drop to standby bias */ |
| 1029 | if (codec->bias_level == SND_SOC_BIAS_PREPARE && !sys_power) { |
| 1030 | ret = snd_soc_dapm_set_bias_level(socdev, |
| 1031 | SND_SOC_BIAS_STANDBY); |
| 1032 | if (ret != 0) |
| 1033 | pr_err("Failed to apply standby bias: %d\n", ret); |
| 1034 | } |
| 1035 | |
| 1036 | /* If we just powered up then move to active bias */ |
| 1037 | if (codec->bias_level == SND_SOC_BIAS_PREPARE && sys_power) { |
| 1038 | ret = snd_soc_dapm_set_bias_level(socdev, |
| 1039 | SND_SOC_BIAS_ON); |
| 1040 | if (ret != 0) |
| 1041 | pr_err("Failed to apply active bias: %d\n", ret); |
| 1042 | } |
| 1043 | |
Mark Brown | cb507e7 | 2009-07-08 18:54:57 +0100 | [diff] [blame] | 1044 | pop_dbg(codec->pop_time, "DAPM sequencing finished, waiting %dms\n", |
| 1045 | codec->pop_time); |
| 1046 | |
Mark Brown | 42aa341 | 2009-03-01 19:21:10 +0000 | [diff] [blame] | 1047 | return 0; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1048 | } |
| 1049 | |
Mark Brown | c1286b8 | 2008-07-07 19:26:03 +0100 | [diff] [blame] | 1050 | #ifdef DEBUG |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1051 | static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action) |
| 1052 | { |
| 1053 | struct snd_soc_dapm_widget *w; |
| 1054 | struct snd_soc_dapm_path *p = NULL; |
| 1055 | int in, out; |
| 1056 | |
| 1057 | printk("DAPM %s %s\n", codec->name, action); |
| 1058 | |
| 1059 | list_for_each_entry(w, &codec->dapm_widgets, list) { |
| 1060 | |
| 1061 | /* only display widgets that effect routing */ |
| 1062 | switch (w->id) { |
| 1063 | case snd_soc_dapm_pre: |
| 1064 | case snd_soc_dapm_post: |
| 1065 | case snd_soc_dapm_vmid: |
| 1066 | continue; |
| 1067 | case snd_soc_dapm_mux: |
Peter Ujfalusi | 2e72f8e | 2009-01-05 09:54:57 +0200 | [diff] [blame] | 1068 | case snd_soc_dapm_value_mux: |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1069 | case snd_soc_dapm_output: |
| 1070 | case snd_soc_dapm_input: |
| 1071 | case snd_soc_dapm_switch: |
| 1072 | case snd_soc_dapm_hp: |
| 1073 | case snd_soc_dapm_mic: |
| 1074 | case snd_soc_dapm_spk: |
| 1075 | case snd_soc_dapm_line: |
| 1076 | case snd_soc_dapm_micbias: |
| 1077 | case snd_soc_dapm_dac: |
| 1078 | case snd_soc_dapm_adc: |
| 1079 | case snd_soc_dapm_pga: |
| 1080 | case snd_soc_dapm_mixer: |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 1081 | case snd_soc_dapm_mixer_named_ctl: |
Mark Brown | 246d0a1 | 2009-04-22 18:24:55 +0100 | [diff] [blame] | 1082 | case snd_soc_dapm_supply: |
Mark Brown | 010ff26 | 2009-08-17 17:39:22 +0100 | [diff] [blame] | 1083 | case snd_soc_dapm_aif_in: |
| 1084 | case snd_soc_dapm_aif_out: |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1085 | if (w->name) { |
| 1086 | in = is_connected_input_ep(w); |
| 1087 | dapm_clear_walk(w->codec); |
| 1088 | out = is_connected_output_ep(w); |
| 1089 | dapm_clear_walk(w->codec); |
| 1090 | printk("%s: %s in %d out %d\n", w->name, |
| 1091 | w->power ? "On":"Off",in, out); |
| 1092 | |
| 1093 | list_for_each_entry(p, &w->sources, list_sink) { |
| 1094 | if (p->connect) |
| 1095 | printk(" in %s %s\n", p->name ? p->name : "static", |
| 1096 | p->source->name); |
| 1097 | } |
| 1098 | list_for_each_entry(p, &w->sinks, list_source) { |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1099 | if (p->connect) |
| 1100 | printk(" out %s %s\n", p->name ? p->name : "static", |
| 1101 | p->sink->name); |
| 1102 | } |
| 1103 | } |
| 1104 | break; |
| 1105 | } |
| 1106 | } |
| 1107 | } |
| 1108 | #endif |
| 1109 | |
Mark Brown | 79fb938 | 2009-08-21 16:38:13 +0100 | [diff] [blame] | 1110 | #ifdef CONFIG_DEBUG_FS |
| 1111 | static int dapm_widget_power_open_file(struct inode *inode, struct file *file) |
| 1112 | { |
| 1113 | file->private_data = inode->i_private; |
| 1114 | return 0; |
| 1115 | } |
| 1116 | |
| 1117 | static ssize_t dapm_widget_power_read_file(struct file *file, |
| 1118 | char __user *user_buf, |
| 1119 | size_t count, loff_t *ppos) |
| 1120 | { |
| 1121 | struct snd_soc_dapm_widget *w = file->private_data; |
| 1122 | char *buf; |
| 1123 | int in, out; |
| 1124 | ssize_t ret; |
| 1125 | struct snd_soc_dapm_path *p = NULL; |
| 1126 | |
| 1127 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 1128 | if (!buf) |
| 1129 | return -ENOMEM; |
| 1130 | |
| 1131 | in = is_connected_input_ep(w); |
| 1132 | dapm_clear_walk(w->codec); |
| 1133 | out = is_connected_output_ep(w); |
| 1134 | dapm_clear_walk(w->codec); |
| 1135 | |
| 1136 | ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d\n", |
| 1137 | w->name, w->power ? "On" : "Off", in, out); |
| 1138 | |
Mark Brown | 3eef08b | 2009-09-14 16:49:00 +0100 | [diff] [blame] | 1139 | if (w->sname) |
| 1140 | ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n", |
| 1141 | w->sname, |
| 1142 | w->active ? "active" : "inactive"); |
Mark Brown | 79fb938 | 2009-08-21 16:38:13 +0100 | [diff] [blame] | 1143 | |
| 1144 | list_for_each_entry(p, &w->sources, list_sink) { |
Mark Brown | 215edda | 2009-09-08 18:59:05 +0100 | [diff] [blame] | 1145 | if (p->connected && !p->connected(w, p->sink)) |
| 1146 | continue; |
| 1147 | |
Mark Brown | 79fb938 | 2009-08-21 16:38:13 +0100 | [diff] [blame] | 1148 | if (p->connect) |
| 1149 | ret += snprintf(buf + ret, PAGE_SIZE - ret, |
| 1150 | " in %s %s\n", |
| 1151 | p->name ? p->name : "static", |
| 1152 | p->source->name); |
| 1153 | } |
| 1154 | list_for_each_entry(p, &w->sinks, list_source) { |
Mark Brown | 215edda | 2009-09-08 18:59:05 +0100 | [diff] [blame] | 1155 | if (p->connected && !p->connected(w, p->sink)) |
| 1156 | continue; |
| 1157 | |
Mark Brown | 79fb938 | 2009-08-21 16:38:13 +0100 | [diff] [blame] | 1158 | if (p->connect) |
| 1159 | ret += snprintf(buf + ret, PAGE_SIZE - ret, |
| 1160 | " out %s %s\n", |
| 1161 | p->name ? p->name : "static", |
| 1162 | p->sink->name); |
| 1163 | } |
| 1164 | |
| 1165 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); |
| 1166 | |
| 1167 | kfree(buf); |
| 1168 | return ret; |
| 1169 | } |
| 1170 | |
| 1171 | static const struct file_operations dapm_widget_power_fops = { |
| 1172 | .open = dapm_widget_power_open_file, |
| 1173 | .read = dapm_widget_power_read_file, |
| 1174 | }; |
| 1175 | |
| 1176 | void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec) |
| 1177 | { |
| 1178 | struct snd_soc_dapm_widget *w; |
| 1179 | struct dentry *d; |
| 1180 | |
| 1181 | if (!codec->debugfs_dapm) |
| 1182 | return; |
| 1183 | |
| 1184 | list_for_each_entry(w, &codec->dapm_widgets, list) { |
| 1185 | if (!w->name) |
| 1186 | continue; |
| 1187 | |
| 1188 | d = debugfs_create_file(w->name, 0444, |
| 1189 | codec->debugfs_dapm, w, |
| 1190 | &dapm_widget_power_fops); |
| 1191 | if (!d) |
| 1192 | printk(KERN_WARNING |
| 1193 | "ASoC: Failed to create %s debugfs file\n", |
| 1194 | w->name); |
| 1195 | } |
| 1196 | } |
| 1197 | #else |
| 1198 | void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec) |
| 1199 | { |
| 1200 | } |
| 1201 | #endif |
| 1202 | |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1203 | /* test and update the power status of a mux widget */ |
Adrian Bunk | d9c96cf | 2006-11-28 12:10:09 +0100 | [diff] [blame] | 1204 | static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget, |
Mark Brown | 3a65577 | 2009-10-05 17:23:30 +0100 | [diff] [blame^] | 1205 | struct snd_kcontrol *kcontrol, int change, |
| 1206 | int mux, struct soc_enum *e) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1207 | { |
| 1208 | struct snd_soc_dapm_path *path; |
| 1209 | int found = 0; |
| 1210 | |
Peter Ujfalusi | eff317d | 2009-01-15 14:40:47 +0200 | [diff] [blame] | 1211 | if (widget->id != snd_soc_dapm_mux && |
| 1212 | widget->id != snd_soc_dapm_value_mux) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1213 | return -ENODEV; |
| 1214 | |
Mark Brown | 3a65577 | 2009-10-05 17:23:30 +0100 | [diff] [blame^] | 1215 | if (!change) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1216 | return 0; |
| 1217 | |
| 1218 | /* find dapm widget path assoc with kcontrol */ |
| 1219 | list_for_each_entry(path, &widget->codec->dapm_paths, list) { |
| 1220 | if (path->kcontrol != kcontrol) |
| 1221 | continue; |
| 1222 | |
Richard Zhao | cb01e2b | 2008-10-07 08:05:20 +0800 | [diff] [blame] | 1223 | if (!path->name || !e->texts[mux]) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1224 | continue; |
| 1225 | |
| 1226 | found = 1; |
| 1227 | /* we now need to match the string in the enum to the path */ |
Richard Zhao | cb01e2b | 2008-10-07 08:05:20 +0800 | [diff] [blame] | 1228 | if (!(strcmp(path->name, e->texts[mux]))) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1229 | path->connect = 1; /* new connection */ |
| 1230 | else |
| 1231 | path->connect = 0; /* old connection must be powered down */ |
| 1232 | } |
| 1233 | |
Mark Brown | 3fccd8b | 2008-07-07 19:26:04 +0100 | [diff] [blame] | 1234 | if (found) { |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1235 | dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP); |
Mark Brown | 3fccd8b | 2008-07-07 19:26:04 +0100 | [diff] [blame] | 1236 | dump_dapm(widget->codec, "mux power update"); |
| 1237 | } |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1238 | |
| 1239 | return 0; |
| 1240 | } |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1241 | |
Milan plzik | 1b075e3 | 2008-01-10 14:39:46 +0100 | [diff] [blame] | 1242 | /* test and update the power status of a mixer or switch widget */ |
Adrian Bunk | d9c96cf | 2006-11-28 12:10:09 +0100 | [diff] [blame] | 1243 | static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget, |
| 1244 | struct snd_kcontrol *kcontrol, int reg, |
| 1245 | int val_mask, int val, int invert) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1246 | { |
| 1247 | struct snd_soc_dapm_path *path; |
| 1248 | int found = 0; |
| 1249 | |
Milan plzik | 1b075e3 | 2008-01-10 14:39:46 +0100 | [diff] [blame] | 1250 | if (widget->id != snd_soc_dapm_mixer && |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 1251 | widget->id != snd_soc_dapm_mixer_named_ctl && |
Milan plzik | 1b075e3 | 2008-01-10 14:39:46 +0100 | [diff] [blame] | 1252 | widget->id != snd_soc_dapm_switch) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1253 | return -ENODEV; |
| 1254 | |
| 1255 | if (!snd_soc_test_bits(widget->codec, reg, val_mask, val)) |
| 1256 | return 0; |
| 1257 | |
| 1258 | /* find dapm widget path assoc with kcontrol */ |
| 1259 | list_for_each_entry(path, &widget->codec->dapm_paths, list) { |
| 1260 | if (path->kcontrol != kcontrol) |
| 1261 | continue; |
| 1262 | |
| 1263 | /* found, now check type */ |
| 1264 | found = 1; |
| 1265 | if (val) |
| 1266 | /* new connection */ |
| 1267 | path->connect = invert ? 0:1; |
| 1268 | else |
| 1269 | /* old connection must be powered down */ |
| 1270 | path->connect = invert ? 1:0; |
| 1271 | break; |
| 1272 | } |
| 1273 | |
Mark Brown | 3fccd8b | 2008-07-07 19:26:04 +0100 | [diff] [blame] | 1274 | if (found) { |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1275 | dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP); |
Mark Brown | 3fccd8b | 2008-07-07 19:26:04 +0100 | [diff] [blame] | 1276 | dump_dapm(widget->codec, "mixer power update"); |
| 1277 | } |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1278 | |
| 1279 | return 0; |
| 1280 | } |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1281 | |
| 1282 | /* show dapm widget status in sys fs */ |
| 1283 | static ssize_t dapm_widget_show(struct device *dev, |
| 1284 | struct device_attribute *attr, char *buf) |
| 1285 | { |
| 1286 | struct snd_soc_device *devdata = dev_get_drvdata(dev); |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 1287 | struct snd_soc_codec *codec = devdata->card->codec; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1288 | struct snd_soc_dapm_widget *w; |
| 1289 | int count = 0; |
| 1290 | char *state = "not set"; |
| 1291 | |
| 1292 | list_for_each_entry(w, &codec->dapm_widgets, list) { |
| 1293 | |
| 1294 | /* only display widgets that burnm power */ |
| 1295 | switch (w->id) { |
| 1296 | case snd_soc_dapm_hp: |
| 1297 | case snd_soc_dapm_mic: |
| 1298 | case snd_soc_dapm_spk: |
| 1299 | case snd_soc_dapm_line: |
| 1300 | case snd_soc_dapm_micbias: |
| 1301 | case snd_soc_dapm_dac: |
| 1302 | case snd_soc_dapm_adc: |
| 1303 | case snd_soc_dapm_pga: |
| 1304 | case snd_soc_dapm_mixer: |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 1305 | case snd_soc_dapm_mixer_named_ctl: |
Mark Brown | 246d0a1 | 2009-04-22 18:24:55 +0100 | [diff] [blame] | 1306 | case snd_soc_dapm_supply: |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1307 | if (w->name) |
| 1308 | count += sprintf(buf + count, "%s: %s\n", |
| 1309 | w->name, w->power ? "On":"Off"); |
| 1310 | break; |
| 1311 | default: |
| 1312 | break; |
| 1313 | } |
| 1314 | } |
| 1315 | |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 1316 | switch (codec->bias_level) { |
| 1317 | case SND_SOC_BIAS_ON: |
| 1318 | state = "On"; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1319 | break; |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 1320 | case SND_SOC_BIAS_PREPARE: |
| 1321 | state = "Prepare"; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1322 | break; |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 1323 | case SND_SOC_BIAS_STANDBY: |
| 1324 | state = "Standby"; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1325 | break; |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 1326 | case SND_SOC_BIAS_OFF: |
| 1327 | state = "Off"; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1328 | break; |
| 1329 | } |
| 1330 | count += sprintf(buf + count, "PM State: %s\n", state); |
| 1331 | |
| 1332 | return count; |
| 1333 | } |
| 1334 | |
| 1335 | static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL); |
| 1336 | |
| 1337 | int snd_soc_dapm_sys_add(struct device *dev) |
| 1338 | { |
Troy Kisky | 12ef193 | 2008-10-13 17:42:14 -0700 | [diff] [blame] | 1339 | return device_create_file(dev, &dev_attr_dapm_widget); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | static void snd_soc_dapm_sys_remove(struct device *dev) |
| 1343 | { |
Mark Brown | aef9084 | 2009-05-16 17:53:16 +0100 | [diff] [blame] | 1344 | device_remove_file(dev, &dev_attr_dapm_widget); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | /* free all dapm widgets and resources */ |
Adrian Bunk | d9c96cf | 2006-11-28 12:10:09 +0100 | [diff] [blame] | 1348 | static void dapm_free_widgets(struct snd_soc_codec *codec) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1349 | { |
| 1350 | struct snd_soc_dapm_widget *w, *next_w; |
| 1351 | struct snd_soc_dapm_path *p, *next_p; |
| 1352 | |
| 1353 | list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) { |
| 1354 | list_del(&w->list); |
| 1355 | kfree(w); |
| 1356 | } |
| 1357 | |
| 1358 | list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) { |
| 1359 | list_del(&p->list); |
| 1360 | kfree(p->long_name); |
| 1361 | kfree(p); |
| 1362 | } |
| 1363 | } |
| 1364 | |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 1365 | static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec, |
Mark Brown | 1649923 | 2009-01-07 18:25:13 +0000 | [diff] [blame] | 1366 | const char *pin, int status) |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 1367 | { |
| 1368 | struct snd_soc_dapm_widget *w; |
| 1369 | |
| 1370 | list_for_each_entry(w, &codec->dapm_widgets, list) { |
| 1371 | if (!strcmp(w->name, pin)) { |
Mark Brown | c1286b8 | 2008-07-07 19:26:03 +0100 | [diff] [blame] | 1372 | pr_debug("dapm: %s: pin %s\n", codec->name, pin); |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 1373 | w->connected = status; |
| 1374 | return 0; |
| 1375 | } |
| 1376 | } |
| 1377 | |
Mark Brown | c1286b8 | 2008-07-07 19:26:03 +0100 | [diff] [blame] | 1378 | pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin); |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 1379 | return -EINVAL; |
| 1380 | } |
| 1381 | |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1382 | /** |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 1383 | * snd_soc_dapm_sync - scan and power dapm paths |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1384 | * @codec: audio codec |
| 1385 | * |
| 1386 | * Walks all dapm audio paths and powers widgets according to their |
| 1387 | * stream or path usage. |
| 1388 | * |
| 1389 | * Returns 0 for success. |
| 1390 | */ |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 1391 | int snd_soc_dapm_sync(struct snd_soc_codec *codec) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1392 | { |
Mark Brown | 3fccd8b | 2008-07-07 19:26:04 +0100 | [diff] [blame] | 1393 | int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP); |
| 1394 | dump_dapm(codec, "sync"); |
| 1395 | return ret; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1396 | } |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 1397 | EXPORT_SYMBOL_GPL(snd_soc_dapm_sync); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1398 | |
Mark Brown | 105f1c2 | 2008-05-13 14:52:19 +0200 | [diff] [blame] | 1399 | static int snd_soc_dapm_add_route(struct snd_soc_codec *codec, |
Mark Brown | 215edda | 2009-09-08 18:59:05 +0100 | [diff] [blame] | 1400 | const struct snd_soc_dapm_route *route) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1401 | { |
| 1402 | struct snd_soc_dapm_path *path; |
| 1403 | struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w; |
Mark Brown | 215edda | 2009-09-08 18:59:05 +0100 | [diff] [blame] | 1404 | const char *sink = route->sink; |
| 1405 | const char *control = route->control; |
| 1406 | const char *source = route->source; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1407 | int ret = 0; |
| 1408 | |
| 1409 | /* find src and dest widgets */ |
| 1410 | list_for_each_entry(w, &codec->dapm_widgets, list) { |
| 1411 | |
| 1412 | if (!wsink && !(strcmp(w->name, sink))) { |
| 1413 | wsink = w; |
| 1414 | continue; |
| 1415 | } |
| 1416 | if (!wsource && !(strcmp(w->name, source))) { |
| 1417 | wsource = w; |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | if (wsource == NULL || wsink == NULL) |
| 1422 | return -ENODEV; |
| 1423 | |
| 1424 | path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL); |
| 1425 | if (!path) |
| 1426 | return -ENOMEM; |
| 1427 | |
| 1428 | path->source = wsource; |
| 1429 | path->sink = wsink; |
Mark Brown | 215edda | 2009-09-08 18:59:05 +0100 | [diff] [blame] | 1430 | path->connected = route->connected; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1431 | INIT_LIST_HEAD(&path->list); |
| 1432 | INIT_LIST_HEAD(&path->list_source); |
| 1433 | INIT_LIST_HEAD(&path->list_sink); |
| 1434 | |
| 1435 | /* check for external widgets */ |
| 1436 | if (wsink->id == snd_soc_dapm_input) { |
| 1437 | if (wsource->id == snd_soc_dapm_micbias || |
| 1438 | wsource->id == snd_soc_dapm_mic || |
Rongrong Cao | 087d53a | 2009-07-10 20:13:30 +0100 | [diff] [blame] | 1439 | wsource->id == snd_soc_dapm_line || |
| 1440 | wsource->id == snd_soc_dapm_output) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1441 | wsink->ext = 1; |
| 1442 | } |
| 1443 | if (wsource->id == snd_soc_dapm_output) { |
| 1444 | if (wsink->id == snd_soc_dapm_spk || |
| 1445 | wsink->id == snd_soc_dapm_hp || |
Seth Forshee | 1e39221 | 2007-04-16 15:36:42 +0200 | [diff] [blame] | 1446 | wsink->id == snd_soc_dapm_line || |
| 1447 | wsink->id == snd_soc_dapm_input) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1448 | wsource->ext = 1; |
| 1449 | } |
| 1450 | |
| 1451 | /* connect static paths */ |
| 1452 | if (control == NULL) { |
| 1453 | list_add(&path->list, &codec->dapm_paths); |
| 1454 | list_add(&path->list_sink, &wsink->sources); |
| 1455 | list_add(&path->list_source, &wsource->sinks); |
| 1456 | path->connect = 1; |
| 1457 | return 0; |
| 1458 | } |
| 1459 | |
| 1460 | /* connect dynamic paths */ |
| 1461 | switch(wsink->id) { |
| 1462 | case snd_soc_dapm_adc: |
| 1463 | case snd_soc_dapm_dac: |
| 1464 | case snd_soc_dapm_pga: |
| 1465 | case snd_soc_dapm_input: |
| 1466 | case snd_soc_dapm_output: |
| 1467 | case snd_soc_dapm_micbias: |
| 1468 | case snd_soc_dapm_vmid: |
| 1469 | case snd_soc_dapm_pre: |
| 1470 | case snd_soc_dapm_post: |
Mark Brown | 246d0a1 | 2009-04-22 18:24:55 +0100 | [diff] [blame] | 1471 | case snd_soc_dapm_supply: |
Mark Brown | 010ff26 | 2009-08-17 17:39:22 +0100 | [diff] [blame] | 1472 | case snd_soc_dapm_aif_in: |
| 1473 | case snd_soc_dapm_aif_out: |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1474 | list_add(&path->list, &codec->dapm_paths); |
| 1475 | list_add(&path->list_sink, &wsink->sources); |
| 1476 | list_add(&path->list_source, &wsource->sinks); |
| 1477 | path->connect = 1; |
| 1478 | return 0; |
| 1479 | case snd_soc_dapm_mux: |
Peter Ujfalusi | 7415555 | 2009-01-08 13:34:29 +0200 | [diff] [blame] | 1480 | case snd_soc_dapm_value_mux: |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1481 | ret = dapm_connect_mux(codec, wsource, wsink, path, control, |
| 1482 | &wsink->kcontrols[0]); |
| 1483 | if (ret != 0) |
| 1484 | goto err; |
| 1485 | break; |
| 1486 | case snd_soc_dapm_switch: |
| 1487 | case snd_soc_dapm_mixer: |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 1488 | case snd_soc_dapm_mixer_named_ctl: |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1489 | ret = dapm_connect_mixer(codec, wsource, wsink, path, control); |
| 1490 | if (ret != 0) |
| 1491 | goto err; |
| 1492 | break; |
| 1493 | case snd_soc_dapm_hp: |
| 1494 | case snd_soc_dapm_mic: |
| 1495 | case snd_soc_dapm_line: |
| 1496 | case snd_soc_dapm_spk: |
| 1497 | list_add(&path->list, &codec->dapm_paths); |
| 1498 | list_add(&path->list_sink, &wsink->sources); |
| 1499 | list_add(&path->list_source, &wsource->sinks); |
| 1500 | path->connect = 0; |
| 1501 | return 0; |
| 1502 | } |
| 1503 | return 0; |
| 1504 | |
| 1505 | err: |
| 1506 | printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source, |
| 1507 | control, sink); |
| 1508 | kfree(path); |
| 1509 | return ret; |
| 1510 | } |
Mark Brown | 105f1c2 | 2008-05-13 14:52:19 +0200 | [diff] [blame] | 1511 | |
| 1512 | /** |
Mark Brown | 105f1c2 | 2008-05-13 14:52:19 +0200 | [diff] [blame] | 1513 | * snd_soc_dapm_add_routes - Add routes between DAPM widgets |
| 1514 | * @codec: codec |
| 1515 | * @route: audio routes |
| 1516 | * @num: number of routes |
| 1517 | * |
| 1518 | * Connects 2 dapm widgets together via a named audio path. The sink is |
| 1519 | * the widget receiving the audio signal, whilst the source is the sender |
| 1520 | * of the audio signal. |
| 1521 | * |
| 1522 | * Returns 0 for success else error. On error all resources can be freed |
| 1523 | * with a call to snd_soc_card_free(). |
| 1524 | */ |
| 1525 | int snd_soc_dapm_add_routes(struct snd_soc_codec *codec, |
| 1526 | const struct snd_soc_dapm_route *route, int num) |
| 1527 | { |
| 1528 | int i, ret; |
| 1529 | |
| 1530 | for (i = 0; i < num; i++) { |
Mark Brown | 215edda | 2009-09-08 18:59:05 +0100 | [diff] [blame] | 1531 | ret = snd_soc_dapm_add_route(codec, route); |
Mark Brown | 105f1c2 | 2008-05-13 14:52:19 +0200 | [diff] [blame] | 1532 | if (ret < 0) { |
| 1533 | printk(KERN_ERR "Failed to add route %s->%s\n", |
| 1534 | route->source, |
| 1535 | route->sink); |
| 1536 | return ret; |
| 1537 | } |
| 1538 | route++; |
| 1539 | } |
| 1540 | |
| 1541 | return 0; |
| 1542 | } |
| 1543 | EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes); |
| 1544 | |
| 1545 | /** |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1546 | * snd_soc_dapm_new_widgets - add new dapm widgets |
| 1547 | * @codec: audio codec |
| 1548 | * |
| 1549 | * Checks the codec for any new dapm widgets and creates them if found. |
| 1550 | * |
| 1551 | * Returns 0 for success. |
| 1552 | */ |
| 1553 | int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec) |
| 1554 | { |
| 1555 | struct snd_soc_dapm_widget *w; |
| 1556 | |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1557 | list_for_each_entry(w, &codec->dapm_widgets, list) |
| 1558 | { |
| 1559 | if (w->new) |
| 1560 | continue; |
| 1561 | |
| 1562 | switch(w->id) { |
| 1563 | case snd_soc_dapm_switch: |
| 1564 | case snd_soc_dapm_mixer: |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 1565 | case snd_soc_dapm_mixer_named_ctl: |
Mark Brown | b75576d | 2009-04-20 17:56:13 +0100 | [diff] [blame] | 1566 | w->power_check = dapm_generic_check_power; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1567 | dapm_new_mixer(codec, w); |
| 1568 | break; |
| 1569 | case snd_soc_dapm_mux: |
Peter Ujfalusi | 2e72f8e | 2009-01-05 09:54:57 +0200 | [diff] [blame] | 1570 | case snd_soc_dapm_value_mux: |
Mark Brown | b75576d | 2009-04-20 17:56:13 +0100 | [diff] [blame] | 1571 | w->power_check = dapm_generic_check_power; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1572 | dapm_new_mux(codec, w); |
| 1573 | break; |
| 1574 | case snd_soc_dapm_adc: |
Mark Brown | 010ff26 | 2009-08-17 17:39:22 +0100 | [diff] [blame] | 1575 | case snd_soc_dapm_aif_out: |
Mark Brown | b75576d | 2009-04-20 17:56:13 +0100 | [diff] [blame] | 1576 | w->power_check = dapm_adc_check_power; |
| 1577 | break; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1578 | case snd_soc_dapm_dac: |
Mark Brown | 010ff26 | 2009-08-17 17:39:22 +0100 | [diff] [blame] | 1579 | case snd_soc_dapm_aif_in: |
Mark Brown | b75576d | 2009-04-20 17:56:13 +0100 | [diff] [blame] | 1580 | w->power_check = dapm_dac_check_power; |
| 1581 | break; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1582 | case snd_soc_dapm_pga: |
Mark Brown | b75576d | 2009-04-20 17:56:13 +0100 | [diff] [blame] | 1583 | w->power_check = dapm_generic_check_power; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1584 | dapm_new_pga(codec, w); |
| 1585 | break; |
| 1586 | case snd_soc_dapm_input: |
| 1587 | case snd_soc_dapm_output: |
| 1588 | case snd_soc_dapm_micbias: |
| 1589 | case snd_soc_dapm_spk: |
| 1590 | case snd_soc_dapm_hp: |
| 1591 | case snd_soc_dapm_mic: |
| 1592 | case snd_soc_dapm_line: |
Mark Brown | b75576d | 2009-04-20 17:56:13 +0100 | [diff] [blame] | 1593 | w->power_check = dapm_generic_check_power; |
| 1594 | break; |
Mark Brown | 246d0a1 | 2009-04-22 18:24:55 +0100 | [diff] [blame] | 1595 | case snd_soc_dapm_supply: |
| 1596 | w->power_check = dapm_supply_check_power; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1597 | case snd_soc_dapm_vmid: |
| 1598 | case snd_soc_dapm_pre: |
| 1599 | case snd_soc_dapm_post: |
| 1600 | break; |
| 1601 | } |
| 1602 | w->new = 1; |
| 1603 | } |
| 1604 | |
| 1605 | dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1606 | return 0; |
| 1607 | } |
| 1608 | EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets); |
| 1609 | |
| 1610 | /** |
| 1611 | * snd_soc_dapm_get_volsw - dapm mixer get callback |
| 1612 | * @kcontrol: mixer control |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 1613 | * @ucontrol: control element information |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1614 | * |
| 1615 | * Callback to get the value of a dapm mixer control. |
| 1616 | * |
| 1617 | * Returns 0 for success. |
| 1618 | */ |
| 1619 | int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol, |
| 1620 | struct snd_ctl_elem_value *ucontrol) |
| 1621 | { |
| 1622 | struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol); |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 1623 | struct soc_mixer_control *mc = |
| 1624 | (struct soc_mixer_control *)kcontrol->private_value; |
Jon Smirl | 815ecf8d | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 1625 | unsigned int reg = mc->reg; |
| 1626 | unsigned int shift = mc->shift; |
| 1627 | unsigned int rshift = mc->rshift; |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 1628 | int max = mc->max; |
Jon Smirl | 815ecf8d | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 1629 | unsigned int invert = mc->invert; |
| 1630 | unsigned int mask = (1 << fls(max)) - 1; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1631 | |
| 1632 | /* return the saved value if we are powered down */ |
| 1633 | if (widget->id == snd_soc_dapm_pga && !widget->power) { |
| 1634 | ucontrol->value.integer.value[0] = widget->saved_value; |
| 1635 | return 0; |
| 1636 | } |
| 1637 | |
| 1638 | ucontrol->value.integer.value[0] = |
| 1639 | (snd_soc_read(widget->codec, reg) >> shift) & mask; |
| 1640 | if (shift != rshift) |
| 1641 | ucontrol->value.integer.value[1] = |
| 1642 | (snd_soc_read(widget->codec, reg) >> rshift) & mask; |
| 1643 | if (invert) { |
| 1644 | ucontrol->value.integer.value[0] = |
Philipp Zabel | a7a4ac8 | 2008-01-10 14:37:42 +0100 | [diff] [blame] | 1645 | max - ucontrol->value.integer.value[0]; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1646 | if (shift != rshift) |
| 1647 | ucontrol->value.integer.value[1] = |
Philipp Zabel | a7a4ac8 | 2008-01-10 14:37:42 +0100 | [diff] [blame] | 1648 | max - ucontrol->value.integer.value[1]; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | return 0; |
| 1652 | } |
| 1653 | EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw); |
| 1654 | |
| 1655 | /** |
| 1656 | * snd_soc_dapm_put_volsw - dapm mixer set callback |
| 1657 | * @kcontrol: mixer control |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 1658 | * @ucontrol: control element information |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1659 | * |
| 1660 | * Callback to set the value of a dapm mixer control. |
| 1661 | * |
| 1662 | * Returns 0 for success. |
| 1663 | */ |
| 1664 | int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol, |
| 1665 | struct snd_ctl_elem_value *ucontrol) |
| 1666 | { |
| 1667 | struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol); |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 1668 | struct soc_mixer_control *mc = |
| 1669 | (struct soc_mixer_control *)kcontrol->private_value; |
Jon Smirl | 815ecf8d | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 1670 | unsigned int reg = mc->reg; |
| 1671 | unsigned int shift = mc->shift; |
| 1672 | unsigned int rshift = mc->rshift; |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 1673 | int max = mc->max; |
Jon Smirl | 815ecf8d | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 1674 | unsigned int mask = (1 << fls(max)) - 1; |
| 1675 | unsigned int invert = mc->invert; |
Daniel Ribeiro | 46f5822 | 2009-06-07 02:49:11 -0300 | [diff] [blame] | 1676 | unsigned int val, val2, val_mask; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1677 | int ret; |
| 1678 | |
| 1679 | val = (ucontrol->value.integer.value[0] & mask); |
| 1680 | |
| 1681 | if (invert) |
Philipp Zabel | a7a4ac8 | 2008-01-10 14:37:42 +0100 | [diff] [blame] | 1682 | val = max - val; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1683 | val_mask = mask << shift; |
| 1684 | val = val << shift; |
| 1685 | if (shift != rshift) { |
| 1686 | val2 = (ucontrol->value.integer.value[1] & mask); |
| 1687 | if (invert) |
Philipp Zabel | a7a4ac8 | 2008-01-10 14:37:42 +0100 | [diff] [blame] | 1688 | val2 = max - val2; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1689 | val_mask |= mask << rshift; |
| 1690 | val |= val2 << rshift; |
| 1691 | } |
| 1692 | |
| 1693 | mutex_lock(&widget->codec->mutex); |
| 1694 | widget->value = val; |
| 1695 | |
| 1696 | /* save volume value if the widget is powered down */ |
| 1697 | if (widget->id == snd_soc_dapm_pga && !widget->power) { |
| 1698 | widget->saved_value = val; |
| 1699 | mutex_unlock(&widget->codec->mutex); |
| 1700 | return 1; |
| 1701 | } |
| 1702 | |
| 1703 | dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert); |
| 1704 | if (widget->event) { |
| 1705 | if (widget->event_flags & SND_SOC_DAPM_PRE_REG) { |
Laim Girdwood | 9af6d95 | 2008-01-10 14:41:02 +0100 | [diff] [blame] | 1706 | ret = widget->event(widget, kcontrol, |
| 1707 | SND_SOC_DAPM_PRE_REG); |
| 1708 | if (ret < 0) { |
| 1709 | ret = 1; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1710 | goto out; |
Laim Girdwood | 9af6d95 | 2008-01-10 14:41:02 +0100 | [diff] [blame] | 1711 | } |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1712 | } |
| 1713 | ret = snd_soc_update_bits(widget->codec, reg, val_mask, val); |
| 1714 | if (widget->event_flags & SND_SOC_DAPM_POST_REG) |
Laim Girdwood | 9af6d95 | 2008-01-10 14:41:02 +0100 | [diff] [blame] | 1715 | ret = widget->event(widget, kcontrol, |
| 1716 | SND_SOC_DAPM_POST_REG); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1717 | } else |
| 1718 | ret = snd_soc_update_bits(widget->codec, reg, val_mask, val); |
| 1719 | |
| 1720 | out: |
| 1721 | mutex_unlock(&widget->codec->mutex); |
| 1722 | return ret; |
| 1723 | } |
| 1724 | EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw); |
| 1725 | |
| 1726 | /** |
| 1727 | * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback |
| 1728 | * @kcontrol: mixer control |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 1729 | * @ucontrol: control element information |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1730 | * |
| 1731 | * Callback to get the value of a dapm enumerated double mixer control. |
| 1732 | * |
| 1733 | * Returns 0 for success. |
| 1734 | */ |
| 1735 | int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol, |
| 1736 | struct snd_ctl_elem_value *ucontrol) |
| 1737 | { |
| 1738 | struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol); |
| 1739 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
Daniel Ribeiro | 46f5822 | 2009-06-07 02:49:11 -0300 | [diff] [blame] | 1740 | unsigned int val, bitmask; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1741 | |
Jon Smirl | f8ba0b7 | 2008-07-29 11:42:27 +0100 | [diff] [blame] | 1742 | for (bitmask = 1; bitmask < e->max; bitmask <<= 1) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1743 | ; |
| 1744 | val = snd_soc_read(widget->codec, e->reg); |
| 1745 | ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1); |
| 1746 | if (e->shift_l != e->shift_r) |
| 1747 | ucontrol->value.enumerated.item[1] = |
| 1748 | (val >> e->shift_r) & (bitmask - 1); |
| 1749 | |
| 1750 | return 0; |
| 1751 | } |
| 1752 | EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double); |
| 1753 | |
| 1754 | /** |
| 1755 | * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback |
| 1756 | * @kcontrol: mixer control |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 1757 | * @ucontrol: control element information |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1758 | * |
| 1759 | * Callback to set the value of a dapm enumerated double mixer control. |
| 1760 | * |
| 1761 | * Returns 0 for success. |
| 1762 | */ |
| 1763 | int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol, |
| 1764 | struct snd_ctl_elem_value *ucontrol) |
| 1765 | { |
| 1766 | struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol); |
| 1767 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
Mark Brown | 3a65577 | 2009-10-05 17:23:30 +0100 | [diff] [blame^] | 1768 | unsigned int val, mux, change; |
Daniel Ribeiro | 46f5822 | 2009-06-07 02:49:11 -0300 | [diff] [blame] | 1769 | unsigned int mask, bitmask; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1770 | int ret = 0; |
| 1771 | |
Jon Smirl | f8ba0b7 | 2008-07-29 11:42:27 +0100 | [diff] [blame] | 1772 | for (bitmask = 1; bitmask < e->max; bitmask <<= 1) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1773 | ; |
Jon Smirl | f8ba0b7 | 2008-07-29 11:42:27 +0100 | [diff] [blame] | 1774 | if (ucontrol->value.enumerated.item[0] > e->max - 1) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1775 | return -EINVAL; |
| 1776 | mux = ucontrol->value.enumerated.item[0]; |
| 1777 | val = mux << e->shift_l; |
| 1778 | mask = (bitmask - 1) << e->shift_l; |
| 1779 | if (e->shift_l != e->shift_r) { |
Jon Smirl | f8ba0b7 | 2008-07-29 11:42:27 +0100 | [diff] [blame] | 1780 | if (ucontrol->value.enumerated.item[1] > e->max - 1) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1781 | return -EINVAL; |
| 1782 | val |= ucontrol->value.enumerated.item[1] << e->shift_r; |
| 1783 | mask |= (bitmask - 1) << e->shift_r; |
| 1784 | } |
| 1785 | |
| 1786 | mutex_lock(&widget->codec->mutex); |
| 1787 | widget->value = val; |
Mark Brown | 3a65577 | 2009-10-05 17:23:30 +0100 | [diff] [blame^] | 1788 | change = snd_soc_test_bits(widget->codec, e->reg, mask, val); |
| 1789 | dapm_mux_update_power(widget, kcontrol, change, mux, e); |
Mark Brown | 1642e3d | 2009-10-05 16:24:26 +0100 | [diff] [blame] | 1790 | |
| 1791 | if (widget->event_flags & SND_SOC_DAPM_PRE_REG) { |
| 1792 | ret = widget->event(widget, |
| 1793 | kcontrol, SND_SOC_DAPM_PRE_REG); |
| 1794 | if (ret < 0) |
| 1795 | goto out; |
| 1796 | } |
| 1797 | |
| 1798 | ret = snd_soc_update_bits(widget->codec, e->reg, mask, val); |
| 1799 | |
| 1800 | if (widget->event_flags & SND_SOC_DAPM_POST_REG) |
| 1801 | ret = widget->event(widget, |
| 1802 | kcontrol, SND_SOC_DAPM_POST_REG); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1803 | |
| 1804 | out: |
| 1805 | mutex_unlock(&widget->codec->mutex); |
| 1806 | return ret; |
| 1807 | } |
| 1808 | EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double); |
| 1809 | |
| 1810 | /** |
Peter Ujfalusi | 2e72f8e | 2009-01-05 09:54:57 +0200 | [diff] [blame] | 1811 | * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get |
| 1812 | * callback |
| 1813 | * @kcontrol: mixer control |
| 1814 | * @ucontrol: control element information |
| 1815 | * |
| 1816 | * Callback to get the value of a dapm semi enumerated double mixer control. |
| 1817 | * |
| 1818 | * Semi enumerated mixer: the enumerated items are referred as values. Can be |
| 1819 | * used for handling bitfield coded enumeration for example. |
| 1820 | * |
| 1821 | * Returns 0 for success. |
| 1822 | */ |
| 1823 | int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol, |
| 1824 | struct snd_ctl_elem_value *ucontrol) |
| 1825 | { |
| 1826 | struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol); |
Peter Ujfalusi | 7415555 | 2009-01-08 13:34:29 +0200 | [diff] [blame] | 1827 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
Daniel Ribeiro | 46f5822 | 2009-06-07 02:49:11 -0300 | [diff] [blame] | 1828 | unsigned int reg_val, val, mux; |
Peter Ujfalusi | 2e72f8e | 2009-01-05 09:54:57 +0200 | [diff] [blame] | 1829 | |
| 1830 | reg_val = snd_soc_read(widget->codec, e->reg); |
| 1831 | val = (reg_val >> e->shift_l) & e->mask; |
| 1832 | for (mux = 0; mux < e->max; mux++) { |
| 1833 | if (val == e->values[mux]) |
| 1834 | break; |
| 1835 | } |
| 1836 | ucontrol->value.enumerated.item[0] = mux; |
| 1837 | if (e->shift_l != e->shift_r) { |
| 1838 | val = (reg_val >> e->shift_r) & e->mask; |
| 1839 | for (mux = 0; mux < e->max; mux++) { |
| 1840 | if (val == e->values[mux]) |
| 1841 | break; |
| 1842 | } |
| 1843 | ucontrol->value.enumerated.item[1] = mux; |
| 1844 | } |
| 1845 | |
| 1846 | return 0; |
| 1847 | } |
| 1848 | EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double); |
| 1849 | |
| 1850 | /** |
| 1851 | * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set |
| 1852 | * callback |
| 1853 | * @kcontrol: mixer control |
| 1854 | * @ucontrol: control element information |
| 1855 | * |
| 1856 | * Callback to set the value of a dapm semi enumerated double mixer control. |
| 1857 | * |
| 1858 | * Semi enumerated mixer: the enumerated items are referred as values. Can be |
| 1859 | * used for handling bitfield coded enumeration for example. |
| 1860 | * |
| 1861 | * Returns 0 for success. |
| 1862 | */ |
| 1863 | int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol, |
| 1864 | struct snd_ctl_elem_value *ucontrol) |
| 1865 | { |
| 1866 | struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol); |
Peter Ujfalusi | 7415555 | 2009-01-08 13:34:29 +0200 | [diff] [blame] | 1867 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
Mark Brown | 3a65577 | 2009-10-05 17:23:30 +0100 | [diff] [blame^] | 1868 | unsigned int val, mux, change; |
Daniel Ribeiro | 46f5822 | 2009-06-07 02:49:11 -0300 | [diff] [blame] | 1869 | unsigned int mask; |
Peter Ujfalusi | 2e72f8e | 2009-01-05 09:54:57 +0200 | [diff] [blame] | 1870 | int ret = 0; |
| 1871 | |
| 1872 | if (ucontrol->value.enumerated.item[0] > e->max - 1) |
| 1873 | return -EINVAL; |
| 1874 | mux = ucontrol->value.enumerated.item[0]; |
| 1875 | val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l; |
| 1876 | mask = e->mask << e->shift_l; |
| 1877 | if (e->shift_l != e->shift_r) { |
| 1878 | if (ucontrol->value.enumerated.item[1] > e->max - 1) |
| 1879 | return -EINVAL; |
| 1880 | val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r; |
| 1881 | mask |= e->mask << e->shift_r; |
| 1882 | } |
| 1883 | |
| 1884 | mutex_lock(&widget->codec->mutex); |
| 1885 | widget->value = val; |
Mark Brown | 3a65577 | 2009-10-05 17:23:30 +0100 | [diff] [blame^] | 1886 | change = snd_soc_test_bits(widget->codec, e->reg, mask, val); |
| 1887 | dapm_mux_update_power(widget, kcontrol, change, mux, e); |
Mark Brown | 1642e3d | 2009-10-05 16:24:26 +0100 | [diff] [blame] | 1888 | |
| 1889 | if (widget->event_flags & SND_SOC_DAPM_PRE_REG) { |
| 1890 | ret = widget->event(widget, |
| 1891 | kcontrol, SND_SOC_DAPM_PRE_REG); |
| 1892 | if (ret < 0) |
| 1893 | goto out; |
| 1894 | } |
| 1895 | |
| 1896 | ret = snd_soc_update_bits(widget->codec, e->reg, mask, val); |
| 1897 | |
| 1898 | if (widget->event_flags & SND_SOC_DAPM_POST_REG) |
| 1899 | ret = widget->event(widget, |
| 1900 | kcontrol, SND_SOC_DAPM_POST_REG); |
Peter Ujfalusi | 2e72f8e | 2009-01-05 09:54:57 +0200 | [diff] [blame] | 1901 | |
| 1902 | out: |
| 1903 | mutex_unlock(&widget->codec->mutex); |
| 1904 | return ret; |
| 1905 | } |
| 1906 | EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double); |
| 1907 | |
| 1908 | /** |
Mark Brown | 8b37dbd | 2009-02-28 21:14:20 +0000 | [diff] [blame] | 1909 | * snd_soc_dapm_info_pin_switch - Info for a pin switch |
| 1910 | * |
| 1911 | * @kcontrol: mixer control |
| 1912 | * @uinfo: control element information |
| 1913 | * |
| 1914 | * Callback to provide information about a pin switch control. |
| 1915 | */ |
| 1916 | int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol, |
| 1917 | struct snd_ctl_elem_info *uinfo) |
| 1918 | { |
| 1919 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 1920 | uinfo->count = 1; |
| 1921 | uinfo->value.integer.min = 0; |
| 1922 | uinfo->value.integer.max = 1; |
| 1923 | |
| 1924 | return 0; |
| 1925 | } |
| 1926 | EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch); |
| 1927 | |
| 1928 | /** |
| 1929 | * snd_soc_dapm_get_pin_switch - Get information for a pin switch |
| 1930 | * |
| 1931 | * @kcontrol: mixer control |
| 1932 | * @ucontrol: Value |
| 1933 | */ |
| 1934 | int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol, |
| 1935 | struct snd_ctl_elem_value *ucontrol) |
| 1936 | { |
| 1937 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1938 | const char *pin = (const char *)kcontrol->private_value; |
| 1939 | |
| 1940 | mutex_lock(&codec->mutex); |
| 1941 | |
| 1942 | ucontrol->value.integer.value[0] = |
| 1943 | snd_soc_dapm_get_pin_status(codec, pin); |
| 1944 | |
| 1945 | mutex_unlock(&codec->mutex); |
| 1946 | |
| 1947 | return 0; |
| 1948 | } |
| 1949 | EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch); |
| 1950 | |
| 1951 | /** |
| 1952 | * snd_soc_dapm_put_pin_switch - Set information for a pin switch |
| 1953 | * |
| 1954 | * @kcontrol: mixer control |
| 1955 | * @ucontrol: Value |
| 1956 | */ |
| 1957 | int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol, |
| 1958 | struct snd_ctl_elem_value *ucontrol) |
| 1959 | { |
| 1960 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1961 | const char *pin = (const char *)kcontrol->private_value; |
| 1962 | |
| 1963 | mutex_lock(&codec->mutex); |
| 1964 | |
| 1965 | if (ucontrol->value.integer.value[0]) |
| 1966 | snd_soc_dapm_enable_pin(codec, pin); |
| 1967 | else |
| 1968 | snd_soc_dapm_disable_pin(codec, pin); |
| 1969 | |
| 1970 | snd_soc_dapm_sync(codec); |
| 1971 | |
| 1972 | mutex_unlock(&codec->mutex); |
| 1973 | |
| 1974 | return 0; |
| 1975 | } |
| 1976 | EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch); |
| 1977 | |
| 1978 | /** |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1979 | * snd_soc_dapm_new_control - create new dapm control |
| 1980 | * @codec: audio codec |
| 1981 | * @widget: widget template |
| 1982 | * |
| 1983 | * Creates a new dapm control based upon the template. |
| 1984 | * |
| 1985 | * Returns 0 for success else error. |
| 1986 | */ |
| 1987 | int snd_soc_dapm_new_control(struct snd_soc_codec *codec, |
| 1988 | const struct snd_soc_dapm_widget *widget) |
| 1989 | { |
| 1990 | struct snd_soc_dapm_widget *w; |
| 1991 | |
| 1992 | if ((w = dapm_cnew_widget(widget)) == NULL) |
| 1993 | return -ENOMEM; |
| 1994 | |
| 1995 | w->codec = codec; |
| 1996 | INIT_LIST_HEAD(&w->sources); |
| 1997 | INIT_LIST_HEAD(&w->sinks); |
| 1998 | INIT_LIST_HEAD(&w->list); |
| 1999 | list_add(&w->list, &codec->dapm_widgets); |
| 2000 | |
| 2001 | /* machine layer set ups unconnected pins and insertions */ |
| 2002 | w->connected = 1; |
| 2003 | return 0; |
| 2004 | } |
| 2005 | EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control); |
| 2006 | |
| 2007 | /** |
Mark Brown | 4ba1327 | 2008-05-13 14:51:19 +0200 | [diff] [blame] | 2008 | * snd_soc_dapm_new_controls - create new dapm controls |
| 2009 | * @codec: audio codec |
| 2010 | * @widget: widget array |
| 2011 | * @num: number of widgets |
| 2012 | * |
| 2013 | * Creates new DAPM controls based upon the templates. |
| 2014 | * |
| 2015 | * Returns 0 for success else error. |
| 2016 | */ |
| 2017 | int snd_soc_dapm_new_controls(struct snd_soc_codec *codec, |
| 2018 | const struct snd_soc_dapm_widget *widget, |
| 2019 | int num) |
| 2020 | { |
| 2021 | int i, ret; |
| 2022 | |
| 2023 | for (i = 0; i < num; i++) { |
| 2024 | ret = snd_soc_dapm_new_control(codec, widget); |
Mark Brown | b8b33cb | 2008-12-18 11:19:30 +0000 | [diff] [blame] | 2025 | if (ret < 0) { |
| 2026 | printk(KERN_ERR |
| 2027 | "ASoC: Failed to create DAPM control %s: %d\n", |
| 2028 | widget->name, ret); |
Mark Brown | 4ba1327 | 2008-05-13 14:51:19 +0200 | [diff] [blame] | 2029 | return ret; |
Mark Brown | b8b33cb | 2008-12-18 11:19:30 +0000 | [diff] [blame] | 2030 | } |
Mark Brown | 4ba1327 | 2008-05-13 14:51:19 +0200 | [diff] [blame] | 2031 | widget++; |
| 2032 | } |
| 2033 | return 0; |
| 2034 | } |
| 2035 | EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls); |
| 2036 | |
| 2037 | |
| 2038 | /** |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 2039 | * snd_soc_dapm_stream_event - send a stream event to the dapm core |
| 2040 | * @codec: audio codec |
| 2041 | * @stream: stream name |
| 2042 | * @event: stream event |
| 2043 | * |
| 2044 | * Sends a stream event to the dapm core. The core then makes any |
| 2045 | * necessary widget power changes. |
| 2046 | * |
| 2047 | * Returns 0 for success else error. |
| 2048 | */ |
| 2049 | int snd_soc_dapm_stream_event(struct snd_soc_codec *codec, |
| 2050 | char *stream, int event) |
| 2051 | { |
| 2052 | struct snd_soc_dapm_widget *w; |
| 2053 | |
Seth Forshee | 11da21a | 2007-02-02 17:14:19 +0100 | [diff] [blame] | 2054 | if (stream == NULL) |
| 2055 | return 0; |
| 2056 | |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 2057 | mutex_lock(&codec->mutex); |
| 2058 | list_for_each_entry(w, &codec->dapm_widgets, list) |
| 2059 | { |
| 2060 | if (!w->sname) |
| 2061 | continue; |
Mark Brown | c1286b8 | 2008-07-07 19:26:03 +0100 | [diff] [blame] | 2062 | pr_debug("widget %s\n %s stream %s event %d\n", |
| 2063 | w->name, w->sname, stream, event); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 2064 | if (strstr(w->sname, stream)) { |
| 2065 | switch(event) { |
| 2066 | case SND_SOC_DAPM_STREAM_START: |
| 2067 | w->active = 1; |
| 2068 | break; |
| 2069 | case SND_SOC_DAPM_STREAM_STOP: |
| 2070 | w->active = 0; |
| 2071 | break; |
| 2072 | case SND_SOC_DAPM_STREAM_SUSPEND: |
| 2073 | if (w->active) |
| 2074 | w->suspend = 1; |
| 2075 | w->active = 0; |
| 2076 | break; |
| 2077 | case SND_SOC_DAPM_STREAM_RESUME: |
| 2078 | if (w->suspend) { |
| 2079 | w->active = 1; |
| 2080 | w->suspend = 0; |
| 2081 | } |
| 2082 | break; |
| 2083 | case SND_SOC_DAPM_STREAM_PAUSE_PUSH: |
| 2084 | break; |
| 2085 | case SND_SOC_DAPM_STREAM_PAUSE_RELEASE: |
| 2086 | break; |
| 2087 | } |
| 2088 | } |
| 2089 | } |
| 2090 | mutex_unlock(&codec->mutex); |
| 2091 | |
| 2092 | dapm_power_widgets(codec, event); |
Harvey Harrison | 9bf8e7d | 2008-03-03 15:32:18 -0800 | [diff] [blame] | 2093 | dump_dapm(codec, __func__); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 2094 | return 0; |
| 2095 | } |
| 2096 | EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event); |
| 2097 | |
| 2098 | /** |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 2099 | * snd_soc_dapm_enable_pin - enable pin. |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 2100 | * @codec: SoC codec |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 2101 | * @pin: pin name |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 2102 | * |
Mark Brown | 74b8f95 | 2009-06-06 11:26:15 +0100 | [diff] [blame] | 2103 | * Enables input/output pin and its parents or children widgets iff there is |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 2104 | * a valid audio route and active audio stream. |
| 2105 | * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to |
| 2106 | * do any widget power switching. |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 2107 | */ |
Mark Brown | 1649923 | 2009-01-07 18:25:13 +0000 | [diff] [blame] | 2108 | int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 2109 | { |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 2110 | return snd_soc_dapm_set_pin(codec, pin, 1); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 2111 | } |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 2112 | EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 2113 | |
| 2114 | /** |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 2115 | * snd_soc_dapm_disable_pin - disable pin. |
| 2116 | * @codec: SoC codec |
| 2117 | * @pin: pin name |
Graeme Gregory | eeec12b | 2008-04-30 19:27:40 +0200 | [diff] [blame] | 2118 | * |
Mark Brown | 74b8f95 | 2009-06-06 11:26:15 +0100 | [diff] [blame] | 2119 | * Disables input/output pin and its parents or children widgets. |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 2120 | * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to |
| 2121 | * do any widget power switching. |
Graeme Gregory | eeec12b | 2008-04-30 19:27:40 +0200 | [diff] [blame] | 2122 | */ |
Mark Brown | 1649923 | 2009-01-07 18:25:13 +0000 | [diff] [blame] | 2123 | int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin) |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 2124 | { |
| 2125 | return snd_soc_dapm_set_pin(codec, pin, 0); |
| 2126 | } |
| 2127 | EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin); |
| 2128 | |
| 2129 | /** |
Mark Brown | 5817b52 | 2008-09-24 11:23:11 +0100 | [diff] [blame] | 2130 | * snd_soc_dapm_nc_pin - permanently disable pin. |
| 2131 | * @codec: SoC codec |
| 2132 | * @pin: pin name |
| 2133 | * |
| 2134 | * Marks the specified pin as being not connected, disabling it along |
| 2135 | * any parent or child widgets. At present this is identical to |
| 2136 | * snd_soc_dapm_disable_pin() but in future it will be extended to do |
| 2137 | * additional things such as disabling controls which only affect |
| 2138 | * paths through the pin. |
| 2139 | * |
| 2140 | * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to |
| 2141 | * do any widget power switching. |
| 2142 | */ |
Mark Brown | 1649923 | 2009-01-07 18:25:13 +0000 | [diff] [blame] | 2143 | int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin) |
Mark Brown | 5817b52 | 2008-09-24 11:23:11 +0100 | [diff] [blame] | 2144 | { |
| 2145 | return snd_soc_dapm_set_pin(codec, pin, 0); |
| 2146 | } |
| 2147 | EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin); |
| 2148 | |
| 2149 | /** |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 2150 | * snd_soc_dapm_get_pin_status - get audio pin status |
| 2151 | * @codec: audio codec |
| 2152 | * @pin: audio signal pin endpoint (or start point) |
| 2153 | * |
| 2154 | * Get audio pin status - connected or disconnected. |
| 2155 | * |
| 2156 | * Returns 1 for connected otherwise 0. |
| 2157 | */ |
Mark Brown | 1649923 | 2009-01-07 18:25:13 +0000 | [diff] [blame] | 2158 | int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin) |
Graeme Gregory | eeec12b | 2008-04-30 19:27:40 +0200 | [diff] [blame] | 2159 | { |
| 2160 | struct snd_soc_dapm_widget *w; |
| 2161 | |
| 2162 | list_for_each_entry(w, &codec->dapm_widgets, list) { |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 2163 | if (!strcmp(w->name, pin)) |
Graeme Gregory | eeec12b | 2008-04-30 19:27:40 +0200 | [diff] [blame] | 2164 | return w->connected; |
| 2165 | } |
| 2166 | |
| 2167 | return 0; |
| 2168 | } |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 2169 | EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status); |
Graeme Gregory | eeec12b | 2008-04-30 19:27:40 +0200 | [diff] [blame] | 2170 | |
| 2171 | /** |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 2172 | * snd_soc_dapm_free - free dapm resources |
| 2173 | * @socdev: SoC device |
| 2174 | * |
| 2175 | * Free all dapm widgets and resources. |
| 2176 | */ |
| 2177 | void snd_soc_dapm_free(struct snd_soc_device *socdev) |
| 2178 | { |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 2179 | struct snd_soc_codec *codec = socdev->card->codec; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 2180 | |
| 2181 | snd_soc_dapm_sys_remove(socdev->dev); |
| 2182 | dapm_free_widgets(codec); |
| 2183 | } |
| 2184 | EXPORT_SYMBOL_GPL(snd_soc_dapm_free); |
| 2185 | |
Mark Brown | 5173747 | 2009-06-22 13:16:51 +0100 | [diff] [blame] | 2186 | /* |
| 2187 | * snd_soc_dapm_shutdown - callback for system shutdown |
| 2188 | */ |
| 2189 | void snd_soc_dapm_shutdown(struct snd_soc_device *socdev) |
| 2190 | { |
| 2191 | struct snd_soc_codec *codec = socdev->card->codec; |
| 2192 | struct snd_soc_dapm_widget *w; |
| 2193 | LIST_HEAD(down_list); |
| 2194 | int powerdown = 0; |
| 2195 | |
| 2196 | list_for_each_entry(w, &codec->dapm_widgets, list) { |
| 2197 | if (w->power) { |
| 2198 | dapm_seq_insert(w, &down_list, dapm_down_seq); |
Mark Brown | c2caa4d | 2009-06-26 15:36:56 +0100 | [diff] [blame] | 2199 | w->power = 0; |
Mark Brown | 5173747 | 2009-06-22 13:16:51 +0100 | [diff] [blame] | 2200 | powerdown = 1; |
| 2201 | } |
| 2202 | } |
| 2203 | |
| 2204 | /* If there were no widgets to power down we're already in |
| 2205 | * standby. |
| 2206 | */ |
| 2207 | if (powerdown) { |
| 2208 | snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_PREPARE); |
| 2209 | dapm_seq_run(codec, &down_list, 0, dapm_down_seq); |
| 2210 | snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_STANDBY); |
| 2211 | } |
| 2212 | |
| 2213 | snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF); |
| 2214 | } |
| 2215 | |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 2216 | /* Module information */ |
Liam Girdwood | d331124 | 2008-10-12 13:17:36 +0100 | [diff] [blame] | 2217 | MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 2218 | MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC"); |
| 2219 | MODULE_LICENSE("GPL"); |