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 |
| 15 | * DAC's/ADC's. |
| 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> |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 40 | #include <sound/core.h> |
| 41 | #include <sound/pcm.h> |
| 42 | #include <sound/pcm_params.h> |
| 43 | #include <sound/soc-dapm.h> |
| 44 | #include <sound/initval.h> |
| 45 | |
| 46 | /* debug */ |
Mark Brown | c1286b8 | 2008-07-07 19:26:03 +0100 | [diff] [blame] | 47 | #ifdef DEBUG |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 48 | #define dump_dapm(codec, action) dbg_dump_dapm(codec, action) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 49 | #else |
| 50 | #define dump_dapm(codec, action) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 51 | #endif |
| 52 | |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 53 | /* dapm power sequences - make this per codec in the future */ |
| 54 | static int dapm_up_seq[] = { |
| 55 | snd_soc_dapm_pre, snd_soc_dapm_micbias, snd_soc_dapm_mic, |
Peter Ujfalusi | 2e72f8e | 2009-01-05 09:54:57 +0200 | [diff] [blame] | 56 | snd_soc_dapm_mux, snd_soc_dapm_value_mux, snd_soc_dapm_dac, |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 57 | snd_soc_dapm_mixer, snd_soc_dapm_mixer_named_ctl, snd_soc_dapm_pga, |
| 58 | snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk, snd_soc_dapm_post |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 59 | }; |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 60 | |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 61 | static int dapm_down_seq[] = { |
| 62 | snd_soc_dapm_pre, snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk, |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 63 | snd_soc_dapm_pga, snd_soc_dapm_mixer_named_ctl, snd_soc_dapm_mixer, |
| 64 | snd_soc_dapm_dac, snd_soc_dapm_mic, snd_soc_dapm_micbias, |
| 65 | snd_soc_dapm_mux, snd_soc_dapm_value_mux, snd_soc_dapm_post |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | static int dapm_status = 1; |
| 69 | module_param(dapm_status, int, 0); |
| 70 | MODULE_PARM_DESC(dapm_status, "enable DPM sysfs entries"); |
| 71 | |
Troy Kisky | 12ef193 | 2008-10-13 17:42:14 -0700 | [diff] [blame] | 72 | static void pop_wait(u32 pop_time) |
Mark Brown | 15e4c72 | 2008-07-02 11:51:20 +0100 | [diff] [blame] | 73 | { |
| 74 | if (pop_time) |
| 75 | schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time)); |
| 76 | } |
| 77 | |
Troy Kisky | 12ef193 | 2008-10-13 17:42:14 -0700 | [diff] [blame] | 78 | static void pop_dbg(u32 pop_time, const char *fmt, ...) |
Mark Brown | 15e4c72 | 2008-07-02 11:51:20 +0100 | [diff] [blame] | 79 | { |
| 80 | va_list args; |
| 81 | |
| 82 | va_start(args, fmt); |
| 83 | |
| 84 | if (pop_time) { |
| 85 | vprintk(fmt, args); |
Troy Kisky | 12ef193 | 2008-10-13 17:42:14 -0700 | [diff] [blame] | 86 | pop_wait(pop_time); |
Mark Brown | 15e4c72 | 2008-07-02 11:51:20 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | va_end(args); |
| 90 | } |
| 91 | |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 92 | /* create a new dapm widget */ |
Takashi Iwai | 88cb429 | 2007-02-05 14:56:20 +0100 | [diff] [blame] | 93 | static inline struct snd_soc_dapm_widget *dapm_cnew_widget( |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 94 | const struct snd_soc_dapm_widget *_widget) |
| 95 | { |
Takashi Iwai | 88cb429 | 2007-02-05 14:56:20 +0100 | [diff] [blame] | 96 | return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* set up initial codec paths */ |
| 100 | static void dapm_set_path_status(struct snd_soc_dapm_widget *w, |
| 101 | struct snd_soc_dapm_path *p, int i) |
| 102 | { |
| 103 | switch (w->id) { |
| 104 | case snd_soc_dapm_switch: |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 105 | case snd_soc_dapm_mixer: |
| 106 | case snd_soc_dapm_mixer_named_ctl: { |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 107 | int val; |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 108 | struct soc_mixer_control *mc = (struct soc_mixer_control *) |
| 109 | w->kcontrols[i].private_value; |
Jon Smirl | 815ecf8 | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 110 | unsigned int reg = mc->reg; |
| 111 | unsigned int shift = mc->shift; |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 112 | int max = mc->max; |
Jon Smirl | 815ecf8 | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 113 | unsigned int mask = (1 << fls(max)) - 1; |
| 114 | unsigned int invert = mc->invert; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 115 | |
| 116 | val = snd_soc_read(w->codec, reg); |
| 117 | val = (val >> shift) & mask; |
| 118 | |
| 119 | if ((invert && !val) || (!invert && val)) |
| 120 | p->connect = 1; |
| 121 | else |
| 122 | p->connect = 0; |
| 123 | } |
| 124 | break; |
| 125 | case snd_soc_dapm_mux: { |
| 126 | struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value; |
| 127 | int val, item, bitmask; |
| 128 | |
Jon Smirl | f8ba0b7 | 2008-07-29 11:42:27 +0100 | [diff] [blame] | 129 | for (bitmask = 1; bitmask < e->max; bitmask <<= 1) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 130 | ; |
| 131 | val = snd_soc_read(w->codec, e->reg); |
| 132 | item = (val >> e->shift_l) & (bitmask - 1); |
| 133 | |
| 134 | p->connect = 0; |
Jon Smirl | f8ba0b7 | 2008-07-29 11:42:27 +0100 | [diff] [blame] | 135 | for (i = 0; i < e->max; i++) { |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 136 | if (!(strcmp(p->name, e->texts[i])) && item == i) |
| 137 | p->connect = 1; |
| 138 | } |
| 139 | } |
| 140 | break; |
Peter Ujfalusi | 2e72f8e | 2009-01-05 09:54:57 +0200 | [diff] [blame] | 141 | case snd_soc_dapm_value_mux: { |
Peter Ujfalusi | 7415555 | 2009-01-08 13:34:29 +0200 | [diff] [blame] | 142 | struct soc_enum *e = (struct soc_enum *) |
Peter Ujfalusi | 2e72f8e | 2009-01-05 09:54:57 +0200 | [diff] [blame] | 143 | w->kcontrols[i].private_value; |
| 144 | int val, item; |
| 145 | |
| 146 | val = snd_soc_read(w->codec, e->reg); |
| 147 | val = (val >> e->shift_l) & e->mask; |
| 148 | for (item = 0; item < e->max; item++) { |
| 149 | if (val == e->values[item]) |
| 150 | break; |
| 151 | } |
| 152 | |
| 153 | p->connect = 0; |
| 154 | for (i = 0; i < e->max; i++) { |
| 155 | if (!(strcmp(p->name, e->texts[i])) && item == i) |
| 156 | p->connect = 1; |
| 157 | } |
| 158 | } |
| 159 | break; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 160 | /* does not effect routing - always connected */ |
| 161 | case snd_soc_dapm_pga: |
| 162 | case snd_soc_dapm_output: |
| 163 | case snd_soc_dapm_adc: |
| 164 | case snd_soc_dapm_input: |
| 165 | case snd_soc_dapm_dac: |
| 166 | case snd_soc_dapm_micbias: |
| 167 | case snd_soc_dapm_vmid: |
| 168 | p->connect = 1; |
| 169 | break; |
| 170 | /* does effect routing - dynamically connected */ |
| 171 | case snd_soc_dapm_hp: |
| 172 | case snd_soc_dapm_mic: |
| 173 | case snd_soc_dapm_spk: |
| 174 | case snd_soc_dapm_line: |
| 175 | case snd_soc_dapm_pre: |
| 176 | case snd_soc_dapm_post: |
| 177 | p->connect = 0; |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /* connect mux widget to it's interconnecting audio paths */ |
| 183 | static int dapm_connect_mux(struct snd_soc_codec *codec, |
| 184 | struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest, |
| 185 | struct snd_soc_dapm_path *path, const char *control_name, |
| 186 | const struct snd_kcontrol_new *kcontrol) |
| 187 | { |
| 188 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
| 189 | int i; |
| 190 | |
Jon Smirl | f8ba0b7 | 2008-07-29 11:42:27 +0100 | [diff] [blame] | 191 | for (i = 0; i < e->max; i++) { |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 192 | if (!(strcmp(control_name, e->texts[i]))) { |
| 193 | list_add(&path->list, &codec->dapm_paths); |
| 194 | list_add(&path->list_sink, &dest->sources); |
| 195 | list_add(&path->list_source, &src->sinks); |
| 196 | path->name = (char*)e->texts[i]; |
| 197 | dapm_set_path_status(dest, path, 0); |
| 198 | return 0; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return -ENODEV; |
| 203 | } |
| 204 | |
| 205 | /* connect mixer widget to it's interconnecting audio paths */ |
| 206 | static int dapm_connect_mixer(struct snd_soc_codec *codec, |
| 207 | struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest, |
| 208 | struct snd_soc_dapm_path *path, const char *control_name) |
| 209 | { |
| 210 | int i; |
| 211 | |
| 212 | /* search for mixer kcontrol */ |
| 213 | for (i = 0; i < dest->num_kcontrols; i++) { |
| 214 | if (!strcmp(control_name, dest->kcontrols[i].name)) { |
| 215 | list_add(&path->list, &codec->dapm_paths); |
| 216 | list_add(&path->list_sink, &dest->sources); |
| 217 | list_add(&path->list_source, &src->sinks); |
| 218 | path->name = dest->kcontrols[i].name; |
| 219 | dapm_set_path_status(dest, path, i); |
| 220 | return 0; |
| 221 | } |
| 222 | } |
| 223 | return -ENODEV; |
| 224 | } |
| 225 | |
| 226 | /* update dapm codec register bits */ |
| 227 | static int dapm_update_bits(struct snd_soc_dapm_widget *widget) |
| 228 | { |
| 229 | int change, power; |
| 230 | unsigned short old, new; |
| 231 | struct snd_soc_codec *codec = widget->codec; |
| 232 | |
| 233 | /* check for valid widgets */ |
| 234 | if (widget->reg < 0 || widget->id == snd_soc_dapm_input || |
| 235 | widget->id == snd_soc_dapm_output || |
| 236 | widget->id == snd_soc_dapm_hp || |
| 237 | widget->id == snd_soc_dapm_mic || |
| 238 | widget->id == snd_soc_dapm_line || |
| 239 | widget->id == snd_soc_dapm_spk) |
| 240 | return 0; |
| 241 | |
| 242 | power = widget->power; |
| 243 | if (widget->invert) |
| 244 | power = (power ? 0:1); |
| 245 | |
| 246 | old = snd_soc_read(codec, widget->reg); |
| 247 | new = (old & ~(0x1 << widget->shift)) | (power << widget->shift); |
| 248 | |
| 249 | change = old != new; |
| 250 | if (change) { |
Troy Kisky | 12ef193 | 2008-10-13 17:42:14 -0700 | [diff] [blame] | 251 | pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n", |
| 252 | widget->name, widget->power ? "on" : "off", |
| 253 | codec->pop_time); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 254 | snd_soc_write(codec, widget->reg, new); |
Troy Kisky | 12ef193 | 2008-10-13 17:42:14 -0700 | [diff] [blame] | 255 | pop_wait(codec->pop_time); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 256 | } |
Mark Brown | c1286b8 | 2008-07-07 19:26:03 +0100 | [diff] [blame] | 257 | pr_debug("reg %x old %x new %x change %d\n", widget->reg, |
| 258 | old, new, change); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 259 | return change; |
| 260 | } |
| 261 | |
| 262 | /* ramps the volume up or down to minimise pops before or after a |
| 263 | * DAPM power event */ |
| 264 | static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power) |
| 265 | { |
| 266 | const struct snd_kcontrol_new *k = widget->kcontrols; |
| 267 | |
| 268 | if (widget->muted && !power) |
| 269 | return 0; |
| 270 | if (!widget->muted && power) |
| 271 | return 0; |
| 272 | |
| 273 | if (widget->num_kcontrols && k) { |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 274 | struct soc_mixer_control *mc = |
| 275 | (struct soc_mixer_control *)k->private_value; |
Jon Smirl | 815ecf8 | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 276 | unsigned int reg = mc->reg; |
| 277 | unsigned int shift = mc->shift; |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 278 | int max = mc->max; |
Jon Smirl | 815ecf8 | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 279 | unsigned int mask = (1 << fls(max)) - 1; |
| 280 | unsigned int invert = mc->invert; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 281 | |
| 282 | if (power) { |
| 283 | int i; |
| 284 | /* power up has happended, increase volume to last level */ |
| 285 | if (invert) { |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 286 | for (i = max; i > widget->saved_value; i--) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 287 | snd_soc_update_bits(widget->codec, reg, mask, i); |
| 288 | } else { |
| 289 | for (i = 0; i < widget->saved_value; i++) |
| 290 | snd_soc_update_bits(widget->codec, reg, mask, i); |
| 291 | } |
| 292 | widget->muted = 0; |
| 293 | } else { |
| 294 | /* power down is about to occur, decrease volume to mute */ |
| 295 | int val = snd_soc_read(widget->codec, reg); |
| 296 | int i = widget->saved_value = (val >> shift) & mask; |
| 297 | if (invert) { |
| 298 | for (; i < mask; i++) |
| 299 | snd_soc_update_bits(widget->codec, reg, mask, i); |
| 300 | } else { |
| 301 | for (; i > 0; i--) |
| 302 | snd_soc_update_bits(widget->codec, reg, mask, i); |
| 303 | } |
| 304 | widget->muted = 1; |
| 305 | } |
| 306 | } |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | /* create new dapm mixer control */ |
| 311 | static int dapm_new_mixer(struct snd_soc_codec *codec, |
| 312 | struct snd_soc_dapm_widget *w) |
| 313 | { |
| 314 | int i, ret = 0; |
Mark Brown | 219b93f | 2008-10-28 13:02:31 +0000 | [diff] [blame] | 315 | size_t name_len; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 316 | struct snd_soc_dapm_path *path; |
| 317 | |
| 318 | /* add kcontrol */ |
| 319 | for (i = 0; i < w->num_kcontrols; i++) { |
| 320 | |
| 321 | /* match name */ |
| 322 | list_for_each_entry(path, &w->sources, list_sink) { |
| 323 | |
| 324 | /* mixer/mux paths name must match control name */ |
| 325 | if (path->name != (char*)w->kcontrols[i].name) |
| 326 | continue; |
| 327 | |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 328 | /* add dapm control with long name. |
| 329 | * for dapm_mixer this is the concatenation of the |
| 330 | * mixer and kcontrol name. |
| 331 | * for dapm_mixer_named_ctl this is simply the |
| 332 | * kcontrol name. |
| 333 | */ |
| 334 | name_len = strlen(w->kcontrols[i].name) + 1; |
Mark Brown | 07495f3 | 2009-03-05 17:06:23 +0000 | [diff] [blame] | 335 | if (w->id != snd_soc_dapm_mixer_named_ctl) |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 336 | name_len += 1 + strlen(w->name); |
| 337 | |
Mark Brown | 219b93f | 2008-10-28 13:02:31 +0000 | [diff] [blame] | 338 | path->long_name = kmalloc(name_len, GFP_KERNEL); |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 339 | |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 340 | if (path->long_name == NULL) |
| 341 | return -ENOMEM; |
| 342 | |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 343 | switch (w->id) { |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 344 | default: |
| 345 | snprintf(path->long_name, name_len, "%s %s", |
| 346 | w->name, w->kcontrols[i].name); |
Mark Brown | 07495f3 | 2009-03-05 17:06:23 +0000 | [diff] [blame] | 347 | break; |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 348 | case snd_soc_dapm_mixer_named_ctl: |
| 349 | snprintf(path->long_name, name_len, "%s", |
| 350 | w->kcontrols[i].name); |
Mark Brown | 07495f3 | 2009-03-05 17:06:23 +0000 | [diff] [blame] | 351 | break; |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Mark Brown | 219b93f | 2008-10-28 13:02:31 +0000 | [diff] [blame] | 354 | path->long_name[name_len - 1] = '\0'; |
| 355 | |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 356 | path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w, |
| 357 | path->long_name); |
| 358 | ret = snd_ctl_add(codec->card, path->kcontrol); |
| 359 | if (ret < 0) { |
Mark Brown | 6553e19 | 2009-04-06 16:59:32 +0100 | [diff] [blame] | 360 | printk(KERN_ERR "asoc: failed to add dapm kcontrol %s: %d\n", |
| 361 | path->long_name, |
| 362 | ret); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 363 | kfree(path->long_name); |
| 364 | path->long_name = NULL; |
| 365 | return ret; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | return ret; |
| 370 | } |
| 371 | |
| 372 | /* create new dapm mux control */ |
| 373 | static int dapm_new_mux(struct snd_soc_codec *codec, |
| 374 | struct snd_soc_dapm_widget *w) |
| 375 | { |
| 376 | struct snd_soc_dapm_path *path = NULL; |
| 377 | struct snd_kcontrol *kcontrol; |
| 378 | int ret = 0; |
| 379 | |
| 380 | if (!w->num_kcontrols) { |
| 381 | printk(KERN_ERR "asoc: mux %s has no controls\n", w->name); |
| 382 | return -EINVAL; |
| 383 | } |
| 384 | |
| 385 | kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name); |
| 386 | ret = snd_ctl_add(codec->card, kcontrol); |
| 387 | if (ret < 0) |
| 388 | goto err; |
| 389 | |
| 390 | list_for_each_entry(path, &w->sources, list_sink) |
| 391 | path->kcontrol = kcontrol; |
| 392 | |
| 393 | return ret; |
| 394 | |
| 395 | err: |
| 396 | printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name); |
| 397 | return ret; |
| 398 | } |
| 399 | |
| 400 | /* create new dapm volume control */ |
| 401 | static int dapm_new_pga(struct snd_soc_codec *codec, |
| 402 | struct snd_soc_dapm_widget *w) |
| 403 | { |
| 404 | struct snd_kcontrol *kcontrol; |
| 405 | int ret = 0; |
| 406 | |
| 407 | if (!w->num_kcontrols) |
| 408 | return -EINVAL; |
| 409 | |
| 410 | kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name); |
| 411 | ret = snd_ctl_add(codec->card, kcontrol); |
| 412 | if (ret < 0) { |
| 413 | printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name); |
| 414 | return ret; |
| 415 | } |
| 416 | |
| 417 | return ret; |
| 418 | } |
| 419 | |
| 420 | /* reset 'walked' bit for each dapm path */ |
| 421 | static inline void dapm_clear_walk(struct snd_soc_codec *codec) |
| 422 | { |
| 423 | struct snd_soc_dapm_path *p; |
| 424 | |
| 425 | list_for_each_entry(p, &codec->dapm_paths, list) |
| 426 | p->walked = 0; |
| 427 | } |
| 428 | |
| 429 | /* |
| 430 | * Recursively check for a completed path to an active or physically connected |
| 431 | * output widget. Returns number of complete paths. |
| 432 | */ |
| 433 | static int is_connected_output_ep(struct snd_soc_dapm_widget *widget) |
| 434 | { |
| 435 | struct snd_soc_dapm_path *path; |
| 436 | int con = 0; |
| 437 | |
| 438 | if (widget->id == snd_soc_dapm_adc && widget->active) |
| 439 | return 1; |
| 440 | |
| 441 | if (widget->connected) { |
| 442 | /* connected pin ? */ |
| 443 | if (widget->id == snd_soc_dapm_output && !widget->ext) |
| 444 | return 1; |
| 445 | |
| 446 | /* connected jack or spk ? */ |
| 447 | if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk || |
| 448 | widget->id == snd_soc_dapm_line) |
| 449 | return 1; |
| 450 | } |
| 451 | |
| 452 | list_for_each_entry(path, &widget->sinks, list_source) { |
| 453 | if (path->walked) |
| 454 | continue; |
| 455 | |
| 456 | if (path->sink && path->connect) { |
| 457 | path->walked = 1; |
| 458 | con += is_connected_output_ep(path->sink); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | return con; |
| 463 | } |
| 464 | |
| 465 | /* |
| 466 | * Recursively check for a completed path to an active or physically connected |
| 467 | * input widget. Returns number of complete paths. |
| 468 | */ |
| 469 | static int is_connected_input_ep(struct snd_soc_dapm_widget *widget) |
| 470 | { |
| 471 | struct snd_soc_dapm_path *path; |
| 472 | int con = 0; |
| 473 | |
| 474 | /* active stream ? */ |
| 475 | if (widget->id == snd_soc_dapm_dac && widget->active) |
| 476 | return 1; |
| 477 | |
| 478 | if (widget->connected) { |
| 479 | /* connected pin ? */ |
| 480 | if (widget->id == snd_soc_dapm_input && !widget->ext) |
| 481 | return 1; |
| 482 | |
| 483 | /* connected VMID/Bias for lower pops */ |
| 484 | if (widget->id == snd_soc_dapm_vmid) |
| 485 | return 1; |
| 486 | |
| 487 | /* connected jack ? */ |
| 488 | if (widget->id == snd_soc_dapm_mic || widget->id == snd_soc_dapm_line) |
| 489 | return 1; |
| 490 | } |
| 491 | |
| 492 | list_for_each_entry(path, &widget->sources, list_sink) { |
| 493 | if (path->walked) |
| 494 | continue; |
| 495 | |
| 496 | if (path->source && path->connect) { |
| 497 | path->walked = 1; |
| 498 | con += is_connected_input_ep(path->source); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | return con; |
| 503 | } |
| 504 | |
| 505 | /* |
Jarkko Nikula | e2be2cc | 2008-06-25 14:42:07 +0300 | [diff] [blame] | 506 | * Handler for generic register modifier widget. |
| 507 | */ |
| 508 | int dapm_reg_event(struct snd_soc_dapm_widget *w, |
| 509 | struct snd_kcontrol *kcontrol, int event) |
| 510 | { |
| 511 | unsigned int val; |
| 512 | |
| 513 | if (SND_SOC_DAPM_EVENT_ON(event)) |
| 514 | val = w->on_val; |
| 515 | else |
| 516 | val = w->off_val; |
| 517 | |
| 518 | snd_soc_update_bits(w->codec, -(w->reg + 1), |
| 519 | w->mask << w->shift, val << w->shift); |
| 520 | |
| 521 | return 0; |
| 522 | } |
Mark Brown | 1158941 | 2008-07-29 11:42:23 +0100 | [diff] [blame] | 523 | EXPORT_SYMBOL_GPL(dapm_reg_event); |
Jarkko Nikula | e2be2cc | 2008-06-25 14:42:07 +0300 | [diff] [blame] | 524 | |
Mark Brown | 025756e | 2009-04-13 11:09:18 +0100 | [diff] [blame] | 525 | /* Standard power change method, used to apply power changes to most |
| 526 | * widgets. |
| 527 | */ |
| 528 | static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w) |
| 529 | { |
| 530 | int ret; |
| 531 | |
| 532 | /* call any power change event handlers */ |
| 533 | if (w->event) |
| 534 | pr_debug("power %s event for %s flags %x\n", |
| 535 | w->power ? "on" : "off", |
| 536 | w->name, w->event_flags); |
| 537 | |
| 538 | /* power up pre event */ |
| 539 | if (w->power && w->event && |
| 540 | (w->event_flags & SND_SOC_DAPM_PRE_PMU)) { |
| 541 | ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU); |
| 542 | if (ret < 0) |
| 543 | return ret; |
| 544 | } |
| 545 | |
| 546 | /* power down pre event */ |
| 547 | if (!w->power && w->event && |
| 548 | (w->event_flags & SND_SOC_DAPM_PRE_PMD)) { |
| 549 | ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD); |
| 550 | if (ret < 0) |
| 551 | return ret; |
| 552 | } |
| 553 | |
| 554 | /* Lower PGA volume to reduce pops */ |
| 555 | if (w->id == snd_soc_dapm_pga && !w->power) |
| 556 | dapm_set_pga(w, w->power); |
| 557 | |
| 558 | dapm_update_bits(w); |
| 559 | |
| 560 | /* Raise PGA volume to reduce pops */ |
| 561 | if (w->id == snd_soc_dapm_pga && w->power) |
| 562 | dapm_set_pga(w, w->power); |
| 563 | |
| 564 | /* power up post event */ |
| 565 | if (w->power && w->event && |
| 566 | (w->event_flags & SND_SOC_DAPM_POST_PMU)) { |
| 567 | ret = w->event(w, |
| 568 | NULL, SND_SOC_DAPM_POST_PMU); |
| 569 | if (ret < 0) |
| 570 | return ret; |
| 571 | } |
| 572 | |
| 573 | /* power down post event */ |
| 574 | if (!w->power && w->event && |
| 575 | (w->event_flags & SND_SOC_DAPM_POST_PMD)) { |
| 576 | ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD); |
| 577 | if (ret < 0) |
| 578 | return ret; |
| 579 | } |
| 580 | |
| 581 | return 0; |
| 582 | } |
| 583 | |
Mark Brown | cd0f2d4 | 2009-04-20 16:56:59 +0100 | [diff] [blame] | 584 | /* Generic check to see if a widget should be powered. |
| 585 | */ |
| 586 | static int dapm_generic_check_power(struct snd_soc_dapm_widget *w) |
| 587 | { |
| 588 | int in, out; |
| 589 | |
| 590 | in = is_connected_input_ep(w); |
| 591 | dapm_clear_walk(w->codec); |
| 592 | out = is_connected_output_ep(w); |
| 593 | dapm_clear_walk(w->codec); |
| 594 | return out != 0 && in != 0; |
| 595 | } |
| 596 | |
Mark Brown | 6ea31b9 | 2009-04-20 17:15:41 +0100 | [diff] [blame] | 597 | /* Check to see if an ADC has power */ |
| 598 | static int dapm_adc_check_power(struct snd_soc_dapm_widget *w) |
| 599 | { |
| 600 | int in; |
| 601 | |
| 602 | if (w->active) { |
| 603 | in = is_connected_input_ep(w); |
| 604 | dapm_clear_walk(w->codec); |
| 605 | return in != 0; |
| 606 | } else { |
| 607 | return dapm_generic_check_power(w); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | /* Check to see if a DAC has power */ |
| 612 | static int dapm_dac_check_power(struct snd_soc_dapm_widget *w) |
| 613 | { |
| 614 | int out; |
| 615 | |
| 616 | if (w->active) { |
| 617 | out = is_connected_output_ep(w); |
| 618 | dapm_clear_walk(w->codec); |
| 619 | return out != 0; |
| 620 | } else { |
| 621 | return dapm_generic_check_power(w); |
| 622 | } |
| 623 | } |
| 624 | |
Jarkko Nikula | e2be2cc | 2008-06-25 14:42:07 +0300 | [diff] [blame] | 625 | /* |
Mark Brown | 42aa341 | 2009-03-01 19:21:10 +0000 | [diff] [blame] | 626 | * Scan a single DAPM widget for a complete audio path and update the |
| 627 | * power status appropriately. |
| 628 | */ |
| 629 | static int dapm_power_widget(struct snd_soc_codec *codec, int event, |
| 630 | struct snd_soc_dapm_widget *w) |
| 631 | { |
Mark Brown | 6ea31b9 | 2009-04-20 17:15:41 +0100 | [diff] [blame] | 632 | int power, ret; |
Mark Brown | 42aa341 | 2009-03-01 19:21:10 +0000 | [diff] [blame] | 633 | |
Mark Brown | 6ea31b9 | 2009-04-20 17:15:41 +0100 | [diff] [blame] | 634 | switch (w->id) { |
Mark Brown | 6ea31b9 | 2009-04-20 17:15:41 +0100 | [diff] [blame] | 635 | case snd_soc_dapm_pre: |
Mark Brown | 42aa341 | 2009-03-01 19:21:10 +0000 | [diff] [blame] | 636 | if (!w->event) |
| 637 | return 0; |
| 638 | |
| 639 | if (event == SND_SOC_DAPM_STREAM_START) { |
| 640 | ret = w->event(w, |
| 641 | NULL, SND_SOC_DAPM_PRE_PMU); |
| 642 | if (ret < 0) |
| 643 | return ret; |
| 644 | } else if (event == SND_SOC_DAPM_STREAM_STOP) { |
| 645 | ret = w->event(w, |
| 646 | NULL, SND_SOC_DAPM_PRE_PMD); |
| 647 | if (ret < 0) |
| 648 | return ret; |
| 649 | } |
| 650 | return 0; |
Mark Brown | 6ea31b9 | 2009-04-20 17:15:41 +0100 | [diff] [blame] | 651 | |
| 652 | case snd_soc_dapm_post: |
Mark Brown | 42aa341 | 2009-03-01 19:21:10 +0000 | [diff] [blame] | 653 | if (!w->event) |
| 654 | return 0; |
| 655 | |
| 656 | if (event == SND_SOC_DAPM_STREAM_START) { |
| 657 | ret = w->event(w, |
| 658 | NULL, SND_SOC_DAPM_POST_PMU); |
| 659 | if (ret < 0) |
| 660 | return ret; |
| 661 | } else if (event == SND_SOC_DAPM_STREAM_STOP) { |
| 662 | ret = w->event(w, |
| 663 | NULL, SND_SOC_DAPM_POST_PMD); |
| 664 | if (ret < 0) |
| 665 | return ret; |
| 666 | } |
| 667 | return 0; |
Mark Brown | 6ea31b9 | 2009-04-20 17:15:41 +0100 | [diff] [blame] | 668 | |
| 669 | default: |
Mark Brown | 6ea31b9 | 2009-04-20 17:15:41 +0100 | [diff] [blame] | 670 | break; |
Mark Brown | 42aa341 | 2009-03-01 19:21:10 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Mark Brown | b75576d | 2009-04-20 17:56:13 +0100 | [diff] [blame^] | 673 | if (!w->power_check) |
| 674 | return 0; |
| 675 | |
| 676 | power = w->power_check(w); |
Mark Brown | 6ea31b9 | 2009-04-20 17:15:41 +0100 | [diff] [blame] | 677 | if (w->power == power) |
Mark Brown | 42aa341 | 2009-03-01 19:21:10 +0000 | [diff] [blame] | 678 | return 0; |
Mark Brown | 6ea31b9 | 2009-04-20 17:15:41 +0100 | [diff] [blame] | 679 | w->power = power; |
Mark Brown | 42aa341 | 2009-03-01 19:21:10 +0000 | [diff] [blame] | 680 | |
Mark Brown | 025756e | 2009-04-13 11:09:18 +0100 | [diff] [blame] | 681 | return dapm_generic_apply_power(w); |
Mark Brown | 42aa341 | 2009-03-01 19:21:10 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /* |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 685 | * Scan each dapm widget for complete audio path. |
| 686 | * A complete path is a route that has valid endpoints i.e.:- |
| 687 | * |
| 688 | * o DAC to output pin. |
| 689 | * o Input Pin to ADC. |
| 690 | * o Input pin to Output pin (bypass, sidetone) |
| 691 | * o DAC to ADC (loopback). |
| 692 | */ |
Adrian Bunk | d9c96cf | 2006-11-28 12:10:09 +0100 | [diff] [blame] | 693 | static int dapm_power_widgets(struct snd_soc_codec *codec, int event) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 694 | { |
| 695 | struct snd_soc_dapm_widget *w; |
Mark Brown | 42aa341 | 2009-03-01 19:21:10 +0000 | [diff] [blame] | 696 | int i, c = 1, *seq = NULL, ret = 0; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 697 | |
| 698 | /* do we have a sequenced stream event */ |
| 699 | if (event == SND_SOC_DAPM_STREAM_START) { |
| 700 | c = ARRAY_SIZE(dapm_up_seq); |
| 701 | seq = dapm_up_seq; |
| 702 | } else if (event == SND_SOC_DAPM_STREAM_STOP) { |
| 703 | c = ARRAY_SIZE(dapm_down_seq); |
| 704 | seq = dapm_down_seq; |
| 705 | } |
| 706 | |
Mark Brown | 42aa341 | 2009-03-01 19:21:10 +0000 | [diff] [blame] | 707 | for (i = 0; i < c; i++) { |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 708 | list_for_each_entry(w, &codec->dapm_widgets, list) { |
| 709 | |
| 710 | /* is widget in stream order */ |
| 711 | if (seq && seq[i] && w->id != seq[i]) |
| 712 | continue; |
| 713 | |
Mark Brown | 42aa341 | 2009-03-01 19:21:10 +0000 | [diff] [blame] | 714 | ret = dapm_power_widget(codec, event, w); |
| 715 | if (ret != 0) |
| 716 | return ret; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | |
Mark Brown | 42aa341 | 2009-03-01 19:21:10 +0000 | [diff] [blame] | 720 | return 0; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 721 | } |
| 722 | |
Mark Brown | c1286b8 | 2008-07-07 19:26:03 +0100 | [diff] [blame] | 723 | #ifdef DEBUG |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 724 | static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action) |
| 725 | { |
| 726 | struct snd_soc_dapm_widget *w; |
| 727 | struct snd_soc_dapm_path *p = NULL; |
| 728 | int in, out; |
| 729 | |
| 730 | printk("DAPM %s %s\n", codec->name, action); |
| 731 | |
| 732 | list_for_each_entry(w, &codec->dapm_widgets, list) { |
| 733 | |
| 734 | /* only display widgets that effect routing */ |
| 735 | switch (w->id) { |
| 736 | case snd_soc_dapm_pre: |
| 737 | case snd_soc_dapm_post: |
| 738 | case snd_soc_dapm_vmid: |
| 739 | continue; |
| 740 | case snd_soc_dapm_mux: |
Peter Ujfalusi | 2e72f8e | 2009-01-05 09:54:57 +0200 | [diff] [blame] | 741 | case snd_soc_dapm_value_mux: |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 742 | case snd_soc_dapm_output: |
| 743 | case snd_soc_dapm_input: |
| 744 | case snd_soc_dapm_switch: |
| 745 | case snd_soc_dapm_hp: |
| 746 | case snd_soc_dapm_mic: |
| 747 | case snd_soc_dapm_spk: |
| 748 | case snd_soc_dapm_line: |
| 749 | case snd_soc_dapm_micbias: |
| 750 | case snd_soc_dapm_dac: |
| 751 | case snd_soc_dapm_adc: |
| 752 | case snd_soc_dapm_pga: |
| 753 | case snd_soc_dapm_mixer: |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 754 | case snd_soc_dapm_mixer_named_ctl: |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 755 | if (w->name) { |
| 756 | in = is_connected_input_ep(w); |
| 757 | dapm_clear_walk(w->codec); |
| 758 | out = is_connected_output_ep(w); |
| 759 | dapm_clear_walk(w->codec); |
| 760 | printk("%s: %s in %d out %d\n", w->name, |
| 761 | w->power ? "On":"Off",in, out); |
| 762 | |
| 763 | list_for_each_entry(p, &w->sources, list_sink) { |
| 764 | if (p->connect) |
| 765 | printk(" in %s %s\n", p->name ? p->name : "static", |
| 766 | p->source->name); |
| 767 | } |
| 768 | list_for_each_entry(p, &w->sinks, list_source) { |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 769 | if (p->connect) |
| 770 | printk(" out %s %s\n", p->name ? p->name : "static", |
| 771 | p->sink->name); |
| 772 | } |
| 773 | } |
| 774 | break; |
| 775 | } |
| 776 | } |
| 777 | } |
| 778 | #endif |
| 779 | |
| 780 | /* test and update the power status of a mux widget */ |
Adrian Bunk | d9c96cf | 2006-11-28 12:10:09 +0100 | [diff] [blame] | 781 | static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget, |
| 782 | struct snd_kcontrol *kcontrol, int mask, |
Richard Zhao | cb01e2b | 2008-10-07 08:05:20 +0800 | [diff] [blame] | 783 | int mux, int val, struct soc_enum *e) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 784 | { |
| 785 | struct snd_soc_dapm_path *path; |
| 786 | int found = 0; |
| 787 | |
Peter Ujfalusi | eff317d | 2009-01-15 14:40:47 +0200 | [diff] [blame] | 788 | if (widget->id != snd_soc_dapm_mux && |
| 789 | widget->id != snd_soc_dapm_value_mux) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 790 | return -ENODEV; |
| 791 | |
| 792 | if (!snd_soc_test_bits(widget->codec, e->reg, mask, val)) |
| 793 | return 0; |
| 794 | |
| 795 | /* find dapm widget path assoc with kcontrol */ |
| 796 | list_for_each_entry(path, &widget->codec->dapm_paths, list) { |
| 797 | if (path->kcontrol != kcontrol) |
| 798 | continue; |
| 799 | |
Richard Zhao | cb01e2b | 2008-10-07 08:05:20 +0800 | [diff] [blame] | 800 | if (!path->name || !e->texts[mux]) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 801 | continue; |
| 802 | |
| 803 | found = 1; |
| 804 | /* 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] | 805 | if (!(strcmp(path->name, e->texts[mux]))) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 806 | path->connect = 1; /* new connection */ |
| 807 | else |
| 808 | path->connect = 0; /* old connection must be powered down */ |
| 809 | } |
| 810 | |
Mark Brown | 3fccd8b | 2008-07-07 19:26:04 +0100 | [diff] [blame] | 811 | if (found) { |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 812 | dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP); |
Mark Brown | 3fccd8b | 2008-07-07 19:26:04 +0100 | [diff] [blame] | 813 | dump_dapm(widget->codec, "mux power update"); |
| 814 | } |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 815 | |
| 816 | return 0; |
| 817 | } |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 818 | |
Milan plzik | 1b075e3 | 2008-01-10 14:39:46 +0100 | [diff] [blame] | 819 | /* test and update the power status of a mixer or switch widget */ |
Adrian Bunk | d9c96cf | 2006-11-28 12:10:09 +0100 | [diff] [blame] | 820 | static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget, |
| 821 | struct snd_kcontrol *kcontrol, int reg, |
| 822 | int val_mask, int val, int invert) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 823 | { |
| 824 | struct snd_soc_dapm_path *path; |
| 825 | int found = 0; |
| 826 | |
Milan plzik | 1b075e3 | 2008-01-10 14:39:46 +0100 | [diff] [blame] | 827 | if (widget->id != snd_soc_dapm_mixer && |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 828 | widget->id != snd_soc_dapm_mixer_named_ctl && |
Milan plzik | 1b075e3 | 2008-01-10 14:39:46 +0100 | [diff] [blame] | 829 | widget->id != snd_soc_dapm_switch) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 830 | return -ENODEV; |
| 831 | |
| 832 | if (!snd_soc_test_bits(widget->codec, reg, val_mask, val)) |
| 833 | return 0; |
| 834 | |
| 835 | /* find dapm widget path assoc with kcontrol */ |
| 836 | list_for_each_entry(path, &widget->codec->dapm_paths, list) { |
| 837 | if (path->kcontrol != kcontrol) |
| 838 | continue; |
| 839 | |
| 840 | /* found, now check type */ |
| 841 | found = 1; |
| 842 | if (val) |
| 843 | /* new connection */ |
| 844 | path->connect = invert ? 0:1; |
| 845 | else |
| 846 | /* old connection must be powered down */ |
| 847 | path->connect = invert ? 1:0; |
| 848 | break; |
| 849 | } |
| 850 | |
Mark Brown | 3fccd8b | 2008-07-07 19:26:04 +0100 | [diff] [blame] | 851 | if (found) { |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 852 | dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP); |
Mark Brown | 3fccd8b | 2008-07-07 19:26:04 +0100 | [diff] [blame] | 853 | dump_dapm(widget->codec, "mixer power update"); |
| 854 | } |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 855 | |
| 856 | return 0; |
| 857 | } |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 858 | |
| 859 | /* show dapm widget status in sys fs */ |
| 860 | static ssize_t dapm_widget_show(struct device *dev, |
| 861 | struct device_attribute *attr, char *buf) |
| 862 | { |
| 863 | struct snd_soc_device *devdata = dev_get_drvdata(dev); |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 864 | struct snd_soc_codec *codec = devdata->card->codec; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 865 | struct snd_soc_dapm_widget *w; |
| 866 | int count = 0; |
| 867 | char *state = "not set"; |
| 868 | |
| 869 | list_for_each_entry(w, &codec->dapm_widgets, list) { |
| 870 | |
| 871 | /* only display widgets that burnm power */ |
| 872 | switch (w->id) { |
| 873 | case snd_soc_dapm_hp: |
| 874 | case snd_soc_dapm_mic: |
| 875 | case snd_soc_dapm_spk: |
| 876 | case snd_soc_dapm_line: |
| 877 | case snd_soc_dapm_micbias: |
| 878 | case snd_soc_dapm_dac: |
| 879 | case snd_soc_dapm_adc: |
| 880 | case snd_soc_dapm_pga: |
| 881 | case snd_soc_dapm_mixer: |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 882 | case snd_soc_dapm_mixer_named_ctl: |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 883 | if (w->name) |
| 884 | count += sprintf(buf + count, "%s: %s\n", |
| 885 | w->name, w->power ? "On":"Off"); |
| 886 | break; |
| 887 | default: |
| 888 | break; |
| 889 | } |
| 890 | } |
| 891 | |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 892 | switch (codec->bias_level) { |
| 893 | case SND_SOC_BIAS_ON: |
| 894 | state = "On"; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 895 | break; |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 896 | case SND_SOC_BIAS_PREPARE: |
| 897 | state = "Prepare"; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 898 | break; |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 899 | case SND_SOC_BIAS_STANDBY: |
| 900 | state = "Standby"; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 901 | break; |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 902 | case SND_SOC_BIAS_OFF: |
| 903 | state = "Off"; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 904 | break; |
| 905 | } |
| 906 | count += sprintf(buf + count, "PM State: %s\n", state); |
| 907 | |
| 908 | return count; |
| 909 | } |
| 910 | |
| 911 | static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL); |
| 912 | |
| 913 | int snd_soc_dapm_sys_add(struct device *dev) |
| 914 | { |
Mark Brown | f0062a9 | 2008-08-28 12:46:24 +0100 | [diff] [blame] | 915 | if (!dapm_status) |
| 916 | return 0; |
Troy Kisky | 12ef193 | 2008-10-13 17:42:14 -0700 | [diff] [blame] | 917 | return device_create_file(dev, &dev_attr_dapm_widget); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | static void snd_soc_dapm_sys_remove(struct device *dev) |
| 921 | { |
Mark Brown | 15e4c72 | 2008-07-02 11:51:20 +0100 | [diff] [blame] | 922 | if (dapm_status) { |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 923 | device_remove_file(dev, &dev_attr_dapm_widget); |
Mark Brown | 15e4c72 | 2008-07-02 11:51:20 +0100 | [diff] [blame] | 924 | } |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | /* free all dapm widgets and resources */ |
Adrian Bunk | d9c96cf | 2006-11-28 12:10:09 +0100 | [diff] [blame] | 928 | static void dapm_free_widgets(struct snd_soc_codec *codec) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 929 | { |
| 930 | struct snd_soc_dapm_widget *w, *next_w; |
| 931 | struct snd_soc_dapm_path *p, *next_p; |
| 932 | |
| 933 | list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) { |
| 934 | list_del(&w->list); |
| 935 | kfree(w); |
| 936 | } |
| 937 | |
| 938 | list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) { |
| 939 | list_del(&p->list); |
| 940 | kfree(p->long_name); |
| 941 | kfree(p); |
| 942 | } |
| 943 | } |
| 944 | |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 945 | static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec, |
Mark Brown | 1649923 | 2009-01-07 18:25:13 +0000 | [diff] [blame] | 946 | const char *pin, int status) |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 947 | { |
| 948 | struct snd_soc_dapm_widget *w; |
| 949 | |
| 950 | list_for_each_entry(w, &codec->dapm_widgets, list) { |
| 951 | if (!strcmp(w->name, pin)) { |
Mark Brown | c1286b8 | 2008-07-07 19:26:03 +0100 | [diff] [blame] | 952 | pr_debug("dapm: %s: pin %s\n", codec->name, pin); |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 953 | w->connected = status; |
| 954 | return 0; |
| 955 | } |
| 956 | } |
| 957 | |
Mark Brown | c1286b8 | 2008-07-07 19:26:03 +0100 | [diff] [blame] | 958 | pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin); |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 959 | return -EINVAL; |
| 960 | } |
| 961 | |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 962 | /** |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 963 | * snd_soc_dapm_sync - scan and power dapm paths |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 964 | * @codec: audio codec |
| 965 | * |
| 966 | * Walks all dapm audio paths and powers widgets according to their |
| 967 | * stream or path usage. |
| 968 | * |
| 969 | * Returns 0 for success. |
| 970 | */ |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 971 | int snd_soc_dapm_sync(struct snd_soc_codec *codec) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 972 | { |
Mark Brown | 3fccd8b | 2008-07-07 19:26:04 +0100 | [diff] [blame] | 973 | int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP); |
| 974 | dump_dapm(codec, "sync"); |
| 975 | return ret; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 976 | } |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 977 | EXPORT_SYMBOL_GPL(snd_soc_dapm_sync); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 978 | |
Mark Brown | 105f1c2 | 2008-05-13 14:52:19 +0200 | [diff] [blame] | 979 | static int snd_soc_dapm_add_route(struct snd_soc_codec *codec, |
| 980 | const char *sink, const char *control, const char *source) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 981 | { |
| 982 | struct snd_soc_dapm_path *path; |
| 983 | struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w; |
| 984 | int ret = 0; |
| 985 | |
| 986 | /* find src and dest widgets */ |
| 987 | list_for_each_entry(w, &codec->dapm_widgets, list) { |
| 988 | |
| 989 | if (!wsink && !(strcmp(w->name, sink))) { |
| 990 | wsink = w; |
| 991 | continue; |
| 992 | } |
| 993 | if (!wsource && !(strcmp(w->name, source))) { |
| 994 | wsource = w; |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | if (wsource == NULL || wsink == NULL) |
| 999 | return -ENODEV; |
| 1000 | |
| 1001 | path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL); |
| 1002 | if (!path) |
| 1003 | return -ENOMEM; |
| 1004 | |
| 1005 | path->source = wsource; |
| 1006 | path->sink = wsink; |
| 1007 | INIT_LIST_HEAD(&path->list); |
| 1008 | INIT_LIST_HEAD(&path->list_source); |
| 1009 | INIT_LIST_HEAD(&path->list_sink); |
| 1010 | |
| 1011 | /* check for external widgets */ |
| 1012 | if (wsink->id == snd_soc_dapm_input) { |
| 1013 | if (wsource->id == snd_soc_dapm_micbias || |
| 1014 | wsource->id == snd_soc_dapm_mic || |
Seth Forshee | 1e39221 | 2007-04-16 15:36:42 +0200 | [diff] [blame] | 1015 | wsink->id == snd_soc_dapm_line || |
| 1016 | wsink->id == snd_soc_dapm_output) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1017 | wsink->ext = 1; |
| 1018 | } |
| 1019 | if (wsource->id == snd_soc_dapm_output) { |
| 1020 | if (wsink->id == snd_soc_dapm_spk || |
| 1021 | wsink->id == snd_soc_dapm_hp || |
Seth Forshee | 1e39221 | 2007-04-16 15:36:42 +0200 | [diff] [blame] | 1022 | wsink->id == snd_soc_dapm_line || |
| 1023 | wsink->id == snd_soc_dapm_input) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1024 | wsource->ext = 1; |
| 1025 | } |
| 1026 | |
| 1027 | /* connect static paths */ |
| 1028 | if (control == NULL) { |
| 1029 | list_add(&path->list, &codec->dapm_paths); |
| 1030 | list_add(&path->list_sink, &wsink->sources); |
| 1031 | list_add(&path->list_source, &wsource->sinks); |
| 1032 | path->connect = 1; |
| 1033 | return 0; |
| 1034 | } |
| 1035 | |
| 1036 | /* connect dynamic paths */ |
| 1037 | switch(wsink->id) { |
| 1038 | case snd_soc_dapm_adc: |
| 1039 | case snd_soc_dapm_dac: |
| 1040 | case snd_soc_dapm_pga: |
| 1041 | case snd_soc_dapm_input: |
| 1042 | case snd_soc_dapm_output: |
| 1043 | case snd_soc_dapm_micbias: |
| 1044 | case snd_soc_dapm_vmid: |
| 1045 | case snd_soc_dapm_pre: |
| 1046 | case snd_soc_dapm_post: |
| 1047 | list_add(&path->list, &codec->dapm_paths); |
| 1048 | list_add(&path->list_sink, &wsink->sources); |
| 1049 | list_add(&path->list_source, &wsource->sinks); |
| 1050 | path->connect = 1; |
| 1051 | return 0; |
| 1052 | case snd_soc_dapm_mux: |
Peter Ujfalusi | 7415555 | 2009-01-08 13:34:29 +0200 | [diff] [blame] | 1053 | case snd_soc_dapm_value_mux: |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1054 | ret = dapm_connect_mux(codec, wsource, wsink, path, control, |
| 1055 | &wsink->kcontrols[0]); |
| 1056 | if (ret != 0) |
| 1057 | goto err; |
| 1058 | break; |
| 1059 | case snd_soc_dapm_switch: |
| 1060 | case snd_soc_dapm_mixer: |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 1061 | case snd_soc_dapm_mixer_named_ctl: |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1062 | ret = dapm_connect_mixer(codec, wsource, wsink, path, control); |
| 1063 | if (ret != 0) |
| 1064 | goto err; |
| 1065 | break; |
| 1066 | case snd_soc_dapm_hp: |
| 1067 | case snd_soc_dapm_mic: |
| 1068 | case snd_soc_dapm_line: |
| 1069 | case snd_soc_dapm_spk: |
| 1070 | list_add(&path->list, &codec->dapm_paths); |
| 1071 | list_add(&path->list_sink, &wsink->sources); |
| 1072 | list_add(&path->list_source, &wsource->sinks); |
| 1073 | path->connect = 0; |
| 1074 | return 0; |
| 1075 | } |
| 1076 | return 0; |
| 1077 | |
| 1078 | err: |
| 1079 | printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source, |
| 1080 | control, sink); |
| 1081 | kfree(path); |
| 1082 | return ret; |
| 1083 | } |
Mark Brown | 105f1c2 | 2008-05-13 14:52:19 +0200 | [diff] [blame] | 1084 | |
| 1085 | /** |
Mark Brown | 105f1c2 | 2008-05-13 14:52:19 +0200 | [diff] [blame] | 1086 | * snd_soc_dapm_add_routes - Add routes between DAPM widgets |
| 1087 | * @codec: codec |
| 1088 | * @route: audio routes |
| 1089 | * @num: number of routes |
| 1090 | * |
| 1091 | * Connects 2 dapm widgets together via a named audio path. The sink is |
| 1092 | * the widget receiving the audio signal, whilst the source is the sender |
| 1093 | * of the audio signal. |
| 1094 | * |
| 1095 | * Returns 0 for success else error. On error all resources can be freed |
| 1096 | * with a call to snd_soc_card_free(). |
| 1097 | */ |
| 1098 | int snd_soc_dapm_add_routes(struct snd_soc_codec *codec, |
| 1099 | const struct snd_soc_dapm_route *route, int num) |
| 1100 | { |
| 1101 | int i, ret; |
| 1102 | |
| 1103 | for (i = 0; i < num; i++) { |
| 1104 | ret = snd_soc_dapm_add_route(codec, route->sink, |
| 1105 | route->control, route->source); |
| 1106 | if (ret < 0) { |
| 1107 | printk(KERN_ERR "Failed to add route %s->%s\n", |
| 1108 | route->source, |
| 1109 | route->sink); |
| 1110 | return ret; |
| 1111 | } |
| 1112 | route++; |
| 1113 | } |
| 1114 | |
| 1115 | return 0; |
| 1116 | } |
| 1117 | EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes); |
| 1118 | |
| 1119 | /** |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1120 | * snd_soc_dapm_new_widgets - add new dapm widgets |
| 1121 | * @codec: audio codec |
| 1122 | * |
| 1123 | * Checks the codec for any new dapm widgets and creates them if found. |
| 1124 | * |
| 1125 | * Returns 0 for success. |
| 1126 | */ |
| 1127 | int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec) |
| 1128 | { |
| 1129 | struct snd_soc_dapm_widget *w; |
| 1130 | |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1131 | list_for_each_entry(w, &codec->dapm_widgets, list) |
| 1132 | { |
| 1133 | if (w->new) |
| 1134 | continue; |
| 1135 | |
| 1136 | switch(w->id) { |
| 1137 | case snd_soc_dapm_switch: |
| 1138 | case snd_soc_dapm_mixer: |
Ian Molton | ca9c1aa | 2009-01-06 20:11:51 +0000 | [diff] [blame] | 1139 | case snd_soc_dapm_mixer_named_ctl: |
Mark Brown | b75576d | 2009-04-20 17:56:13 +0100 | [diff] [blame^] | 1140 | w->power_check = dapm_generic_check_power; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1141 | dapm_new_mixer(codec, w); |
| 1142 | break; |
| 1143 | case snd_soc_dapm_mux: |
Peter Ujfalusi | 2e72f8e | 2009-01-05 09:54:57 +0200 | [diff] [blame] | 1144 | case snd_soc_dapm_value_mux: |
Mark Brown | b75576d | 2009-04-20 17:56:13 +0100 | [diff] [blame^] | 1145 | w->power_check = dapm_generic_check_power; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1146 | dapm_new_mux(codec, w); |
| 1147 | break; |
| 1148 | case snd_soc_dapm_adc: |
Mark Brown | b75576d | 2009-04-20 17:56:13 +0100 | [diff] [blame^] | 1149 | w->power_check = dapm_adc_check_power; |
| 1150 | break; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1151 | case snd_soc_dapm_dac: |
Mark Brown | b75576d | 2009-04-20 17:56:13 +0100 | [diff] [blame^] | 1152 | w->power_check = dapm_dac_check_power; |
| 1153 | break; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1154 | case snd_soc_dapm_pga: |
Mark Brown | b75576d | 2009-04-20 17:56:13 +0100 | [diff] [blame^] | 1155 | w->power_check = dapm_generic_check_power; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1156 | dapm_new_pga(codec, w); |
| 1157 | break; |
| 1158 | case snd_soc_dapm_input: |
| 1159 | case snd_soc_dapm_output: |
| 1160 | case snd_soc_dapm_micbias: |
| 1161 | case snd_soc_dapm_spk: |
| 1162 | case snd_soc_dapm_hp: |
| 1163 | case snd_soc_dapm_mic: |
| 1164 | case snd_soc_dapm_line: |
Mark Brown | b75576d | 2009-04-20 17:56:13 +0100 | [diff] [blame^] | 1165 | w->power_check = dapm_generic_check_power; |
| 1166 | break; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1167 | case snd_soc_dapm_vmid: |
| 1168 | case snd_soc_dapm_pre: |
| 1169 | case snd_soc_dapm_post: |
| 1170 | break; |
| 1171 | } |
| 1172 | w->new = 1; |
| 1173 | } |
| 1174 | |
| 1175 | dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1176 | return 0; |
| 1177 | } |
| 1178 | EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets); |
| 1179 | |
| 1180 | /** |
| 1181 | * snd_soc_dapm_get_volsw - dapm mixer get callback |
| 1182 | * @kcontrol: mixer control |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 1183 | * @ucontrol: control element information |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1184 | * |
| 1185 | * Callback to get the value of a dapm mixer control. |
| 1186 | * |
| 1187 | * Returns 0 for success. |
| 1188 | */ |
| 1189 | int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol, |
| 1190 | struct snd_ctl_elem_value *ucontrol) |
| 1191 | { |
| 1192 | struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol); |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 1193 | struct soc_mixer_control *mc = |
| 1194 | (struct soc_mixer_control *)kcontrol->private_value; |
Jon Smirl | 815ecf8 | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 1195 | unsigned int reg = mc->reg; |
| 1196 | unsigned int shift = mc->shift; |
| 1197 | unsigned int rshift = mc->rshift; |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 1198 | int max = mc->max; |
Jon Smirl | 815ecf8 | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 1199 | unsigned int invert = mc->invert; |
| 1200 | unsigned int mask = (1 << fls(max)) - 1; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1201 | |
| 1202 | /* return the saved value if we are powered down */ |
| 1203 | if (widget->id == snd_soc_dapm_pga && !widget->power) { |
| 1204 | ucontrol->value.integer.value[0] = widget->saved_value; |
| 1205 | return 0; |
| 1206 | } |
| 1207 | |
| 1208 | ucontrol->value.integer.value[0] = |
| 1209 | (snd_soc_read(widget->codec, reg) >> shift) & mask; |
| 1210 | if (shift != rshift) |
| 1211 | ucontrol->value.integer.value[1] = |
| 1212 | (snd_soc_read(widget->codec, reg) >> rshift) & mask; |
| 1213 | if (invert) { |
| 1214 | ucontrol->value.integer.value[0] = |
Philipp Zabel | a7a4ac8 | 2008-01-10 14:37:42 +0100 | [diff] [blame] | 1215 | max - ucontrol->value.integer.value[0]; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1216 | if (shift != rshift) |
| 1217 | ucontrol->value.integer.value[1] = |
Philipp Zabel | a7a4ac8 | 2008-01-10 14:37:42 +0100 | [diff] [blame] | 1218 | max - ucontrol->value.integer.value[1]; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | return 0; |
| 1222 | } |
| 1223 | EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw); |
| 1224 | |
| 1225 | /** |
| 1226 | * snd_soc_dapm_put_volsw - dapm mixer set callback |
| 1227 | * @kcontrol: mixer control |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 1228 | * @ucontrol: control element information |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1229 | * |
| 1230 | * Callback to set the value of a dapm mixer control. |
| 1231 | * |
| 1232 | * Returns 0 for success. |
| 1233 | */ |
| 1234 | int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol, |
| 1235 | struct snd_ctl_elem_value *ucontrol) |
| 1236 | { |
| 1237 | struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol); |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 1238 | struct soc_mixer_control *mc = |
| 1239 | (struct soc_mixer_control *)kcontrol->private_value; |
Jon Smirl | 815ecf8 | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 1240 | unsigned int reg = mc->reg; |
| 1241 | unsigned int shift = mc->shift; |
| 1242 | unsigned int rshift = mc->rshift; |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 1243 | int max = mc->max; |
Jon Smirl | 815ecf8 | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 1244 | unsigned int mask = (1 << fls(max)) - 1; |
| 1245 | unsigned int invert = mc->invert; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1246 | unsigned short val, val2, val_mask; |
| 1247 | int ret; |
| 1248 | |
| 1249 | val = (ucontrol->value.integer.value[0] & mask); |
| 1250 | |
| 1251 | if (invert) |
Philipp Zabel | a7a4ac8 | 2008-01-10 14:37:42 +0100 | [diff] [blame] | 1252 | val = max - val; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1253 | val_mask = mask << shift; |
| 1254 | val = val << shift; |
| 1255 | if (shift != rshift) { |
| 1256 | val2 = (ucontrol->value.integer.value[1] & mask); |
| 1257 | if (invert) |
Philipp Zabel | a7a4ac8 | 2008-01-10 14:37:42 +0100 | [diff] [blame] | 1258 | val2 = max - val2; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1259 | val_mask |= mask << rshift; |
| 1260 | val |= val2 << rshift; |
| 1261 | } |
| 1262 | |
| 1263 | mutex_lock(&widget->codec->mutex); |
| 1264 | widget->value = val; |
| 1265 | |
| 1266 | /* save volume value if the widget is powered down */ |
| 1267 | if (widget->id == snd_soc_dapm_pga && !widget->power) { |
| 1268 | widget->saved_value = val; |
| 1269 | mutex_unlock(&widget->codec->mutex); |
| 1270 | return 1; |
| 1271 | } |
| 1272 | |
| 1273 | dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert); |
| 1274 | if (widget->event) { |
| 1275 | if (widget->event_flags & SND_SOC_DAPM_PRE_REG) { |
Laim Girdwood | 9af6d95 | 2008-01-10 14:41:02 +0100 | [diff] [blame] | 1276 | ret = widget->event(widget, kcontrol, |
| 1277 | SND_SOC_DAPM_PRE_REG); |
| 1278 | if (ret < 0) { |
| 1279 | ret = 1; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1280 | goto out; |
Laim Girdwood | 9af6d95 | 2008-01-10 14:41:02 +0100 | [diff] [blame] | 1281 | } |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1282 | } |
| 1283 | ret = snd_soc_update_bits(widget->codec, reg, val_mask, val); |
| 1284 | if (widget->event_flags & SND_SOC_DAPM_POST_REG) |
Laim Girdwood | 9af6d95 | 2008-01-10 14:41:02 +0100 | [diff] [blame] | 1285 | ret = widget->event(widget, kcontrol, |
| 1286 | SND_SOC_DAPM_POST_REG); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1287 | } else |
| 1288 | ret = snd_soc_update_bits(widget->codec, reg, val_mask, val); |
| 1289 | |
| 1290 | out: |
| 1291 | mutex_unlock(&widget->codec->mutex); |
| 1292 | return ret; |
| 1293 | } |
| 1294 | EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw); |
| 1295 | |
| 1296 | /** |
| 1297 | * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback |
| 1298 | * @kcontrol: mixer control |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 1299 | * @ucontrol: control element information |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1300 | * |
| 1301 | * Callback to get the value of a dapm enumerated double mixer control. |
| 1302 | * |
| 1303 | * Returns 0 for success. |
| 1304 | */ |
| 1305 | int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol, |
| 1306 | struct snd_ctl_elem_value *ucontrol) |
| 1307 | { |
| 1308 | struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol); |
| 1309 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
| 1310 | unsigned short val, bitmask; |
| 1311 | |
Jon Smirl | f8ba0b7 | 2008-07-29 11:42:27 +0100 | [diff] [blame] | 1312 | for (bitmask = 1; bitmask < e->max; bitmask <<= 1) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1313 | ; |
| 1314 | val = snd_soc_read(widget->codec, e->reg); |
| 1315 | ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1); |
| 1316 | if (e->shift_l != e->shift_r) |
| 1317 | ucontrol->value.enumerated.item[1] = |
| 1318 | (val >> e->shift_r) & (bitmask - 1); |
| 1319 | |
| 1320 | return 0; |
| 1321 | } |
| 1322 | EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double); |
| 1323 | |
| 1324 | /** |
| 1325 | * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback |
| 1326 | * @kcontrol: mixer control |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 1327 | * @ucontrol: control element information |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1328 | * |
| 1329 | * Callback to set the value of a dapm enumerated double mixer control. |
| 1330 | * |
| 1331 | * Returns 0 for success. |
| 1332 | */ |
| 1333 | int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol, |
| 1334 | struct snd_ctl_elem_value *ucontrol) |
| 1335 | { |
| 1336 | struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol); |
| 1337 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
| 1338 | unsigned short val, mux; |
| 1339 | unsigned short mask, bitmask; |
| 1340 | int ret = 0; |
| 1341 | |
Jon Smirl | f8ba0b7 | 2008-07-29 11:42:27 +0100 | [diff] [blame] | 1342 | for (bitmask = 1; bitmask < e->max; bitmask <<= 1) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1343 | ; |
Jon Smirl | f8ba0b7 | 2008-07-29 11:42:27 +0100 | [diff] [blame] | 1344 | if (ucontrol->value.enumerated.item[0] > e->max - 1) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1345 | return -EINVAL; |
| 1346 | mux = ucontrol->value.enumerated.item[0]; |
| 1347 | val = mux << e->shift_l; |
| 1348 | mask = (bitmask - 1) << e->shift_l; |
| 1349 | if (e->shift_l != e->shift_r) { |
Jon Smirl | f8ba0b7 | 2008-07-29 11:42:27 +0100 | [diff] [blame] | 1350 | if (ucontrol->value.enumerated.item[1] > e->max - 1) |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1351 | return -EINVAL; |
| 1352 | val |= ucontrol->value.enumerated.item[1] << e->shift_r; |
| 1353 | mask |= (bitmask - 1) << e->shift_r; |
| 1354 | } |
| 1355 | |
| 1356 | mutex_lock(&widget->codec->mutex); |
| 1357 | widget->value = val; |
Richard Zhao | cb01e2b | 2008-10-07 08:05:20 +0800 | [diff] [blame] | 1358 | dapm_mux_update_power(widget, kcontrol, mask, mux, val, e); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1359 | if (widget->event) { |
| 1360 | if (widget->event_flags & SND_SOC_DAPM_PRE_REG) { |
Laim Girdwood | 9af6d95 | 2008-01-10 14:41:02 +0100 | [diff] [blame] | 1361 | ret = widget->event(widget, |
| 1362 | kcontrol, SND_SOC_DAPM_PRE_REG); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1363 | if (ret < 0) |
| 1364 | goto out; |
| 1365 | } |
| 1366 | ret = snd_soc_update_bits(widget->codec, e->reg, mask, val); |
| 1367 | if (widget->event_flags & SND_SOC_DAPM_POST_REG) |
Laim Girdwood | 9af6d95 | 2008-01-10 14:41:02 +0100 | [diff] [blame] | 1368 | ret = widget->event(widget, |
| 1369 | kcontrol, SND_SOC_DAPM_POST_REG); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1370 | } else |
| 1371 | ret = snd_soc_update_bits(widget->codec, e->reg, mask, val); |
| 1372 | |
| 1373 | out: |
| 1374 | mutex_unlock(&widget->codec->mutex); |
| 1375 | return ret; |
| 1376 | } |
| 1377 | EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double); |
| 1378 | |
| 1379 | /** |
Peter Ujfalusi | 2e72f8e | 2009-01-05 09:54:57 +0200 | [diff] [blame] | 1380 | * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get |
| 1381 | * callback |
| 1382 | * @kcontrol: mixer control |
| 1383 | * @ucontrol: control element information |
| 1384 | * |
| 1385 | * Callback to get the value of a dapm semi enumerated double mixer control. |
| 1386 | * |
| 1387 | * Semi enumerated mixer: the enumerated items are referred as values. Can be |
| 1388 | * used for handling bitfield coded enumeration for example. |
| 1389 | * |
| 1390 | * Returns 0 for success. |
| 1391 | */ |
| 1392 | int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol, |
| 1393 | struct snd_ctl_elem_value *ucontrol) |
| 1394 | { |
| 1395 | struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol); |
Peter Ujfalusi | 7415555 | 2009-01-08 13:34:29 +0200 | [diff] [blame] | 1396 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
Peter Ujfalusi | 2e72f8e | 2009-01-05 09:54:57 +0200 | [diff] [blame] | 1397 | unsigned short reg_val, val, mux; |
| 1398 | |
| 1399 | reg_val = snd_soc_read(widget->codec, e->reg); |
| 1400 | val = (reg_val >> e->shift_l) & e->mask; |
| 1401 | for (mux = 0; mux < e->max; mux++) { |
| 1402 | if (val == e->values[mux]) |
| 1403 | break; |
| 1404 | } |
| 1405 | ucontrol->value.enumerated.item[0] = mux; |
| 1406 | if (e->shift_l != e->shift_r) { |
| 1407 | val = (reg_val >> e->shift_r) & e->mask; |
| 1408 | for (mux = 0; mux < e->max; mux++) { |
| 1409 | if (val == e->values[mux]) |
| 1410 | break; |
| 1411 | } |
| 1412 | ucontrol->value.enumerated.item[1] = mux; |
| 1413 | } |
| 1414 | |
| 1415 | return 0; |
| 1416 | } |
| 1417 | EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double); |
| 1418 | |
| 1419 | /** |
| 1420 | * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set |
| 1421 | * callback |
| 1422 | * @kcontrol: mixer control |
| 1423 | * @ucontrol: control element information |
| 1424 | * |
| 1425 | * Callback to set the value of a dapm semi enumerated double mixer control. |
| 1426 | * |
| 1427 | * Semi enumerated mixer: the enumerated items are referred as values. Can be |
| 1428 | * used for handling bitfield coded enumeration for example. |
| 1429 | * |
| 1430 | * Returns 0 for success. |
| 1431 | */ |
| 1432 | int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol, |
| 1433 | struct snd_ctl_elem_value *ucontrol) |
| 1434 | { |
| 1435 | struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol); |
Peter Ujfalusi | 7415555 | 2009-01-08 13:34:29 +0200 | [diff] [blame] | 1436 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
Peter Ujfalusi | 2e72f8e | 2009-01-05 09:54:57 +0200 | [diff] [blame] | 1437 | unsigned short val, mux; |
| 1438 | unsigned short mask; |
| 1439 | int ret = 0; |
| 1440 | |
| 1441 | if (ucontrol->value.enumerated.item[0] > e->max - 1) |
| 1442 | return -EINVAL; |
| 1443 | mux = ucontrol->value.enumerated.item[0]; |
| 1444 | val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l; |
| 1445 | mask = e->mask << e->shift_l; |
| 1446 | if (e->shift_l != e->shift_r) { |
| 1447 | if (ucontrol->value.enumerated.item[1] > e->max - 1) |
| 1448 | return -EINVAL; |
| 1449 | val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r; |
| 1450 | mask |= e->mask << e->shift_r; |
| 1451 | } |
| 1452 | |
| 1453 | mutex_lock(&widget->codec->mutex); |
| 1454 | widget->value = val; |
Peter Ujfalusi | 7415555 | 2009-01-08 13:34:29 +0200 | [diff] [blame] | 1455 | dapm_mux_update_power(widget, kcontrol, mask, mux, val, e); |
Peter Ujfalusi | 2e72f8e | 2009-01-05 09:54:57 +0200 | [diff] [blame] | 1456 | if (widget->event) { |
| 1457 | if (widget->event_flags & SND_SOC_DAPM_PRE_REG) { |
| 1458 | ret = widget->event(widget, |
| 1459 | kcontrol, SND_SOC_DAPM_PRE_REG); |
| 1460 | if (ret < 0) |
| 1461 | goto out; |
| 1462 | } |
| 1463 | ret = snd_soc_update_bits(widget->codec, e->reg, mask, val); |
| 1464 | if (widget->event_flags & SND_SOC_DAPM_POST_REG) |
| 1465 | ret = widget->event(widget, |
| 1466 | kcontrol, SND_SOC_DAPM_POST_REG); |
| 1467 | } else |
| 1468 | ret = snd_soc_update_bits(widget->codec, e->reg, mask, val); |
| 1469 | |
| 1470 | out: |
| 1471 | mutex_unlock(&widget->codec->mutex); |
| 1472 | return ret; |
| 1473 | } |
| 1474 | EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double); |
| 1475 | |
| 1476 | /** |
Mark Brown | 8b37dbd | 2009-02-28 21:14:20 +0000 | [diff] [blame] | 1477 | * snd_soc_dapm_info_pin_switch - Info for a pin switch |
| 1478 | * |
| 1479 | * @kcontrol: mixer control |
| 1480 | * @uinfo: control element information |
| 1481 | * |
| 1482 | * Callback to provide information about a pin switch control. |
| 1483 | */ |
| 1484 | int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol, |
| 1485 | struct snd_ctl_elem_info *uinfo) |
| 1486 | { |
| 1487 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 1488 | uinfo->count = 1; |
| 1489 | uinfo->value.integer.min = 0; |
| 1490 | uinfo->value.integer.max = 1; |
| 1491 | |
| 1492 | return 0; |
| 1493 | } |
| 1494 | EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch); |
| 1495 | |
| 1496 | /** |
| 1497 | * snd_soc_dapm_get_pin_switch - Get information for a pin switch |
| 1498 | * |
| 1499 | * @kcontrol: mixer control |
| 1500 | * @ucontrol: Value |
| 1501 | */ |
| 1502 | int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol, |
| 1503 | struct snd_ctl_elem_value *ucontrol) |
| 1504 | { |
| 1505 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1506 | const char *pin = (const char *)kcontrol->private_value; |
| 1507 | |
| 1508 | mutex_lock(&codec->mutex); |
| 1509 | |
| 1510 | ucontrol->value.integer.value[0] = |
| 1511 | snd_soc_dapm_get_pin_status(codec, pin); |
| 1512 | |
| 1513 | mutex_unlock(&codec->mutex); |
| 1514 | |
| 1515 | return 0; |
| 1516 | } |
| 1517 | EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch); |
| 1518 | |
| 1519 | /** |
| 1520 | * snd_soc_dapm_put_pin_switch - Set information for a pin switch |
| 1521 | * |
| 1522 | * @kcontrol: mixer control |
| 1523 | * @ucontrol: Value |
| 1524 | */ |
| 1525 | int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol, |
| 1526 | struct snd_ctl_elem_value *ucontrol) |
| 1527 | { |
| 1528 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1529 | const char *pin = (const char *)kcontrol->private_value; |
| 1530 | |
| 1531 | mutex_lock(&codec->mutex); |
| 1532 | |
| 1533 | if (ucontrol->value.integer.value[0]) |
| 1534 | snd_soc_dapm_enable_pin(codec, pin); |
| 1535 | else |
| 1536 | snd_soc_dapm_disable_pin(codec, pin); |
| 1537 | |
| 1538 | snd_soc_dapm_sync(codec); |
| 1539 | |
| 1540 | mutex_unlock(&codec->mutex); |
| 1541 | |
| 1542 | return 0; |
| 1543 | } |
| 1544 | EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch); |
| 1545 | |
| 1546 | /** |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1547 | * snd_soc_dapm_new_control - create new dapm control |
| 1548 | * @codec: audio codec |
| 1549 | * @widget: widget template |
| 1550 | * |
| 1551 | * Creates a new dapm control based upon the template. |
| 1552 | * |
| 1553 | * Returns 0 for success else error. |
| 1554 | */ |
| 1555 | int snd_soc_dapm_new_control(struct snd_soc_codec *codec, |
| 1556 | const struct snd_soc_dapm_widget *widget) |
| 1557 | { |
| 1558 | struct snd_soc_dapm_widget *w; |
| 1559 | |
| 1560 | if ((w = dapm_cnew_widget(widget)) == NULL) |
| 1561 | return -ENOMEM; |
| 1562 | |
| 1563 | w->codec = codec; |
| 1564 | INIT_LIST_HEAD(&w->sources); |
| 1565 | INIT_LIST_HEAD(&w->sinks); |
| 1566 | INIT_LIST_HEAD(&w->list); |
| 1567 | list_add(&w->list, &codec->dapm_widgets); |
| 1568 | |
| 1569 | /* machine layer set ups unconnected pins and insertions */ |
| 1570 | w->connected = 1; |
| 1571 | return 0; |
| 1572 | } |
| 1573 | EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control); |
| 1574 | |
| 1575 | /** |
Mark Brown | 4ba1327 | 2008-05-13 14:51:19 +0200 | [diff] [blame] | 1576 | * snd_soc_dapm_new_controls - create new dapm controls |
| 1577 | * @codec: audio codec |
| 1578 | * @widget: widget array |
| 1579 | * @num: number of widgets |
| 1580 | * |
| 1581 | * Creates new DAPM controls based upon the templates. |
| 1582 | * |
| 1583 | * Returns 0 for success else error. |
| 1584 | */ |
| 1585 | int snd_soc_dapm_new_controls(struct snd_soc_codec *codec, |
| 1586 | const struct snd_soc_dapm_widget *widget, |
| 1587 | int num) |
| 1588 | { |
| 1589 | int i, ret; |
| 1590 | |
| 1591 | for (i = 0; i < num; i++) { |
| 1592 | ret = snd_soc_dapm_new_control(codec, widget); |
Mark Brown | b8b33cb | 2008-12-18 11:19:30 +0000 | [diff] [blame] | 1593 | if (ret < 0) { |
| 1594 | printk(KERN_ERR |
| 1595 | "ASoC: Failed to create DAPM control %s: %d\n", |
| 1596 | widget->name, ret); |
Mark Brown | 4ba1327 | 2008-05-13 14:51:19 +0200 | [diff] [blame] | 1597 | return ret; |
Mark Brown | b8b33cb | 2008-12-18 11:19:30 +0000 | [diff] [blame] | 1598 | } |
Mark Brown | 4ba1327 | 2008-05-13 14:51:19 +0200 | [diff] [blame] | 1599 | widget++; |
| 1600 | } |
| 1601 | return 0; |
| 1602 | } |
| 1603 | EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls); |
| 1604 | |
| 1605 | |
| 1606 | /** |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1607 | * snd_soc_dapm_stream_event - send a stream event to the dapm core |
| 1608 | * @codec: audio codec |
| 1609 | * @stream: stream name |
| 1610 | * @event: stream event |
| 1611 | * |
| 1612 | * Sends a stream event to the dapm core. The core then makes any |
| 1613 | * necessary widget power changes. |
| 1614 | * |
| 1615 | * Returns 0 for success else error. |
| 1616 | */ |
| 1617 | int snd_soc_dapm_stream_event(struct snd_soc_codec *codec, |
| 1618 | char *stream, int event) |
| 1619 | { |
| 1620 | struct snd_soc_dapm_widget *w; |
| 1621 | |
Seth Forshee | 11da21a | 2007-02-02 17:14:19 +0100 | [diff] [blame] | 1622 | if (stream == NULL) |
| 1623 | return 0; |
| 1624 | |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1625 | mutex_lock(&codec->mutex); |
| 1626 | list_for_each_entry(w, &codec->dapm_widgets, list) |
| 1627 | { |
| 1628 | if (!w->sname) |
| 1629 | continue; |
Mark Brown | c1286b8 | 2008-07-07 19:26:03 +0100 | [diff] [blame] | 1630 | pr_debug("widget %s\n %s stream %s event %d\n", |
| 1631 | w->name, w->sname, stream, event); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1632 | if (strstr(w->sname, stream)) { |
| 1633 | switch(event) { |
| 1634 | case SND_SOC_DAPM_STREAM_START: |
| 1635 | w->active = 1; |
| 1636 | break; |
| 1637 | case SND_SOC_DAPM_STREAM_STOP: |
| 1638 | w->active = 0; |
| 1639 | break; |
| 1640 | case SND_SOC_DAPM_STREAM_SUSPEND: |
| 1641 | if (w->active) |
| 1642 | w->suspend = 1; |
| 1643 | w->active = 0; |
| 1644 | break; |
| 1645 | case SND_SOC_DAPM_STREAM_RESUME: |
| 1646 | if (w->suspend) { |
| 1647 | w->active = 1; |
| 1648 | w->suspend = 0; |
| 1649 | } |
| 1650 | break; |
| 1651 | case SND_SOC_DAPM_STREAM_PAUSE_PUSH: |
| 1652 | break; |
| 1653 | case SND_SOC_DAPM_STREAM_PAUSE_RELEASE: |
| 1654 | break; |
| 1655 | } |
| 1656 | } |
| 1657 | } |
| 1658 | mutex_unlock(&codec->mutex); |
| 1659 | |
| 1660 | dapm_power_widgets(codec, event); |
Harvey Harrison | 9bf8e7d | 2008-03-03 15:32:18 -0800 | [diff] [blame] | 1661 | dump_dapm(codec, __func__); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1662 | return 0; |
| 1663 | } |
| 1664 | EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event); |
| 1665 | |
| 1666 | /** |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 1667 | * snd_soc_dapm_set_bias_level - set the bias level for the system |
Liam Girdwood | 0b4d221 | 2008-01-10 14:36:20 +0100 | [diff] [blame] | 1668 | * @socdev: audio device |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 1669 | * @level: level to configure |
Liam Girdwood | 0b4d221 | 2008-01-10 14:36:20 +0100 | [diff] [blame] | 1670 | * |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 1671 | * Configure the bias (power) levels for the SoC audio device. |
Liam Girdwood | 0b4d221 | 2008-01-10 14:36:20 +0100 | [diff] [blame] | 1672 | * |
| 1673 | * Returns 0 for success else error. |
| 1674 | */ |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 1675 | int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev, |
| 1676 | enum snd_soc_bias_level level) |
Liam Girdwood | 0b4d221 | 2008-01-10 14:36:20 +0100 | [diff] [blame] | 1677 | { |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 1678 | struct snd_soc_card *card = socdev->card; |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 1679 | struct snd_soc_codec *codec = socdev->card->codec; |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 1680 | int ret = 0; |
Liam Girdwood | 0b4d221 | 2008-01-10 14:36:20 +0100 | [diff] [blame] | 1681 | |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 1682 | if (card->set_bias_level) |
| 1683 | ret = card->set_bias_level(card, level); |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 1684 | if (ret == 0 && codec->set_bias_level) |
| 1685 | ret = codec->set_bias_level(codec, level); |
| 1686 | |
| 1687 | return ret; |
Liam Girdwood | 0b4d221 | 2008-01-10 14:36:20 +0100 | [diff] [blame] | 1688 | } |
Liam Girdwood | 0b4d221 | 2008-01-10 14:36:20 +0100 | [diff] [blame] | 1689 | |
| 1690 | /** |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 1691 | * snd_soc_dapm_enable_pin - enable pin. |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 1692 | * @codec: SoC codec |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 1693 | * @pin: pin name |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1694 | * |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 1695 | * Enables input/output pin and it's parents or children widgets iff there is |
| 1696 | * a valid audio route and active audio stream. |
| 1697 | * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to |
| 1698 | * do any widget power switching. |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1699 | */ |
Mark Brown | 1649923 | 2009-01-07 18:25:13 +0000 | [diff] [blame] | 1700 | 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] | 1701 | { |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 1702 | return snd_soc_dapm_set_pin(codec, pin, 1); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1703 | } |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 1704 | EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1705 | |
| 1706 | /** |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 1707 | * snd_soc_dapm_disable_pin - disable pin. |
| 1708 | * @codec: SoC codec |
| 1709 | * @pin: pin name |
Graeme Gregory | eeec12b | 2008-04-30 19:27:40 +0200 | [diff] [blame] | 1710 | * |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 1711 | * Disables input/output pin and it's parents or children widgets. |
| 1712 | * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to |
| 1713 | * do any widget power switching. |
Graeme Gregory | eeec12b | 2008-04-30 19:27:40 +0200 | [diff] [blame] | 1714 | */ |
Mark Brown | 1649923 | 2009-01-07 18:25:13 +0000 | [diff] [blame] | 1715 | 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] | 1716 | { |
| 1717 | return snd_soc_dapm_set_pin(codec, pin, 0); |
| 1718 | } |
| 1719 | EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin); |
| 1720 | |
| 1721 | /** |
Mark Brown | 5817b52 | 2008-09-24 11:23:11 +0100 | [diff] [blame] | 1722 | * snd_soc_dapm_nc_pin - permanently disable pin. |
| 1723 | * @codec: SoC codec |
| 1724 | * @pin: pin name |
| 1725 | * |
| 1726 | * Marks the specified pin as being not connected, disabling it along |
| 1727 | * any parent or child widgets. At present this is identical to |
| 1728 | * snd_soc_dapm_disable_pin() but in future it will be extended to do |
| 1729 | * additional things such as disabling controls which only affect |
| 1730 | * paths through the pin. |
| 1731 | * |
| 1732 | * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to |
| 1733 | * do any widget power switching. |
| 1734 | */ |
Mark Brown | 1649923 | 2009-01-07 18:25:13 +0000 | [diff] [blame] | 1735 | 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] | 1736 | { |
| 1737 | return snd_soc_dapm_set_pin(codec, pin, 0); |
| 1738 | } |
| 1739 | EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin); |
| 1740 | |
| 1741 | /** |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 1742 | * snd_soc_dapm_get_pin_status - get audio pin status |
| 1743 | * @codec: audio codec |
| 1744 | * @pin: audio signal pin endpoint (or start point) |
| 1745 | * |
| 1746 | * Get audio pin status - connected or disconnected. |
| 1747 | * |
| 1748 | * Returns 1 for connected otherwise 0. |
| 1749 | */ |
Mark Brown | 1649923 | 2009-01-07 18:25:13 +0000 | [diff] [blame] | 1750 | 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] | 1751 | { |
| 1752 | struct snd_soc_dapm_widget *w; |
| 1753 | |
| 1754 | list_for_each_entry(w, &codec->dapm_widgets, list) { |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 1755 | if (!strcmp(w->name, pin)) |
Graeme Gregory | eeec12b | 2008-04-30 19:27:40 +0200 | [diff] [blame] | 1756 | return w->connected; |
| 1757 | } |
| 1758 | |
| 1759 | return 0; |
| 1760 | } |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 1761 | EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status); |
Graeme Gregory | eeec12b | 2008-04-30 19:27:40 +0200 | [diff] [blame] | 1762 | |
| 1763 | /** |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1764 | * snd_soc_dapm_free - free dapm resources |
| 1765 | * @socdev: SoC device |
| 1766 | * |
| 1767 | * Free all dapm widgets and resources. |
| 1768 | */ |
| 1769 | void snd_soc_dapm_free(struct snd_soc_device *socdev) |
| 1770 | { |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 1771 | struct snd_soc_codec *codec = socdev->card->codec; |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1772 | |
| 1773 | snd_soc_dapm_sys_remove(socdev->dev); |
| 1774 | dapm_free_widgets(codec); |
| 1775 | } |
| 1776 | EXPORT_SYMBOL_GPL(snd_soc_dapm_free); |
| 1777 | |
| 1778 | /* Module information */ |
Liam Girdwood | d331124 | 2008-10-12 13:17:36 +0100 | [diff] [blame] | 1779 | MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); |
Richard Purdie | 2b97eab | 2006-10-06 18:32:18 +0200 | [diff] [blame] | 1780 | MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC"); |
| 1781 | MODULE_LICENSE("GPL"); |