blob: 04ef84106d7c9d7172324c5ec012cfba94f98fa4 [file] [log] [blame]
Richard Purdie2b97eab2006-10-06 18:32:18 +02001/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02006 *
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 Purdie2b97eab2006-10-06 18:32:18 +020012 * 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. Day3a4fa0a2007-10-19 23:10:43 +020021 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * 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 Purdie2b97eab2006-10-06 18:32:18 +020040#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 Brownc1286b82008-07-07 19:26:03 +010047#ifdef DEBUG
Richard Purdie2b97eab2006-10-06 18:32:18 +020048#define dump_dapm(codec, action) dbg_dump_dapm(codec, action)
Richard Purdie2b97eab2006-10-06 18:32:18 +020049#else
50#define dump_dapm(codec, action)
Richard Purdie2b97eab2006-10-06 18:32:18 +020051#endif
52
Richard Purdie2b97eab2006-10-06 18:32:18 +020053/* dapm power sequences - make this per codec in the future */
54static int dapm_up_seq[] = {
Mark Brown246d0a12009-04-22 18:24:55 +010055 snd_soc_dapm_pre, snd_soc_dapm_supply, snd_soc_dapm_micbias,
56 snd_soc_dapm_mic, snd_soc_dapm_mux, snd_soc_dapm_value_mux,
57 snd_soc_dapm_dac, snd_soc_dapm_mixer, snd_soc_dapm_mixer_named_ctl,
58 snd_soc_dapm_pga, snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk,
59 snd_soc_dapm_post
Richard Purdie2b97eab2006-10-06 18:32:18 +020060};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000061
Richard Purdie2b97eab2006-10-06 18:32:18 +020062static int dapm_down_seq[] = {
63 snd_soc_dapm_pre, snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk,
Ian Moltonca9c1aa2009-01-06 20:11:51 +000064 snd_soc_dapm_pga, snd_soc_dapm_mixer_named_ctl, snd_soc_dapm_mixer,
65 snd_soc_dapm_dac, snd_soc_dapm_mic, snd_soc_dapm_micbias,
Mark Brown246d0a12009-04-22 18:24:55 +010066 snd_soc_dapm_mux, snd_soc_dapm_value_mux, snd_soc_dapm_supply,
67 snd_soc_dapm_post
Richard Purdie2b97eab2006-10-06 18:32:18 +020068};
69
70static int dapm_status = 1;
71module_param(dapm_status, int, 0);
72MODULE_PARM_DESC(dapm_status, "enable DPM sysfs entries");
73
Troy Kisky12ef1932008-10-13 17:42:14 -070074static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +010075{
76 if (pop_time)
77 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
78}
79
Troy Kisky12ef1932008-10-13 17:42:14 -070080static void pop_dbg(u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +010081{
82 va_list args;
83
84 va_start(args, fmt);
85
86 if (pop_time) {
87 vprintk(fmt, args);
Troy Kisky12ef1932008-10-13 17:42:14 -070088 pop_wait(pop_time);
Mark Brown15e4c722008-07-02 11:51:20 +010089 }
90
91 va_end(args);
92}
93
Richard Purdie2b97eab2006-10-06 18:32:18 +020094/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +010095static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +020096 const struct snd_soc_dapm_widget *_widget)
97{
Takashi Iwai88cb4292007-02-05 14:56:20 +010098 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +020099}
100
101/* set up initial codec paths */
102static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
103 struct snd_soc_dapm_path *p, int i)
104{
105 switch (w->id) {
106 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000107 case snd_soc_dapm_mixer:
108 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200109 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100110 struct soc_mixer_control *mc = (struct soc_mixer_control *)
111 w->kcontrols[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400112 unsigned int reg = mc->reg;
113 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100114 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400115 unsigned int mask = (1 << fls(max)) - 1;
116 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200117
118 val = snd_soc_read(w->codec, reg);
119 val = (val >> shift) & mask;
120
121 if ((invert && !val) || (!invert && val))
122 p->connect = 1;
123 else
124 p->connect = 0;
125 }
126 break;
127 case snd_soc_dapm_mux: {
128 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
129 int val, item, bitmask;
130
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100131 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200132 ;
133 val = snd_soc_read(w->codec, e->reg);
134 item = (val >> e->shift_l) & (bitmask - 1);
135
136 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100137 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200138 if (!(strcmp(p->name, e->texts[i])) && item == i)
139 p->connect = 1;
140 }
141 }
142 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200143 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200144 struct soc_enum *e = (struct soc_enum *)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200145 w->kcontrols[i].private_value;
146 int val, item;
147
148 val = snd_soc_read(w->codec, e->reg);
149 val = (val >> e->shift_l) & e->mask;
150 for (item = 0; item < e->max; item++) {
151 if (val == e->values[item])
152 break;
153 }
154
155 p->connect = 0;
156 for (i = 0; i < e->max; i++) {
157 if (!(strcmp(p->name, e->texts[i])) && item == i)
158 p->connect = 1;
159 }
160 }
161 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200162 /* does not effect routing - always connected */
163 case snd_soc_dapm_pga:
164 case snd_soc_dapm_output:
165 case snd_soc_dapm_adc:
166 case snd_soc_dapm_input:
167 case snd_soc_dapm_dac:
168 case snd_soc_dapm_micbias:
169 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100170 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200171 p->connect = 1;
172 break;
173 /* does effect routing - dynamically connected */
174 case snd_soc_dapm_hp:
175 case snd_soc_dapm_mic:
176 case snd_soc_dapm_spk:
177 case snd_soc_dapm_line:
178 case snd_soc_dapm_pre:
179 case snd_soc_dapm_post:
180 p->connect = 0;
181 break;
182 }
183}
184
185/* connect mux widget to it's interconnecting audio paths */
186static int dapm_connect_mux(struct snd_soc_codec *codec,
187 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
188 struct snd_soc_dapm_path *path, const char *control_name,
189 const struct snd_kcontrol_new *kcontrol)
190{
191 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
192 int i;
193
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100194 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200195 if (!(strcmp(control_name, e->texts[i]))) {
196 list_add(&path->list, &codec->dapm_paths);
197 list_add(&path->list_sink, &dest->sources);
198 list_add(&path->list_source, &src->sinks);
199 path->name = (char*)e->texts[i];
200 dapm_set_path_status(dest, path, 0);
201 return 0;
202 }
203 }
204
205 return -ENODEV;
206}
207
208/* connect mixer widget to it's interconnecting audio paths */
209static int dapm_connect_mixer(struct snd_soc_codec *codec,
210 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
211 struct snd_soc_dapm_path *path, const char *control_name)
212{
213 int i;
214
215 /* search for mixer kcontrol */
216 for (i = 0; i < dest->num_kcontrols; i++) {
217 if (!strcmp(control_name, dest->kcontrols[i].name)) {
218 list_add(&path->list, &codec->dapm_paths);
219 list_add(&path->list_sink, &dest->sources);
220 list_add(&path->list_source, &src->sinks);
221 path->name = dest->kcontrols[i].name;
222 dapm_set_path_status(dest, path, i);
223 return 0;
224 }
225 }
226 return -ENODEV;
227}
228
229/* update dapm codec register bits */
230static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
231{
232 int change, power;
233 unsigned short old, new;
234 struct snd_soc_codec *codec = widget->codec;
235
236 /* check for valid widgets */
237 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
238 widget->id == snd_soc_dapm_output ||
239 widget->id == snd_soc_dapm_hp ||
240 widget->id == snd_soc_dapm_mic ||
241 widget->id == snd_soc_dapm_line ||
242 widget->id == snd_soc_dapm_spk)
243 return 0;
244
245 power = widget->power;
246 if (widget->invert)
247 power = (power ? 0:1);
248
249 old = snd_soc_read(codec, widget->reg);
250 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
251
252 change = old != new;
253 if (change) {
Troy Kisky12ef1932008-10-13 17:42:14 -0700254 pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n",
255 widget->name, widget->power ? "on" : "off",
256 codec->pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200257 snd_soc_write(codec, widget->reg, new);
Troy Kisky12ef1932008-10-13 17:42:14 -0700258 pop_wait(codec->pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200259 }
Mark Brownc1286b82008-07-07 19:26:03 +0100260 pr_debug("reg %x old %x new %x change %d\n", widget->reg,
261 old, new, change);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200262 return change;
263}
264
265/* ramps the volume up or down to minimise pops before or after a
266 * DAPM power event */
267static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power)
268{
269 const struct snd_kcontrol_new *k = widget->kcontrols;
270
271 if (widget->muted && !power)
272 return 0;
273 if (!widget->muted && power)
274 return 0;
275
276 if (widget->num_kcontrols && k) {
Jon Smirl4eaa9812008-07-29 11:42:26 +0100277 struct soc_mixer_control *mc =
278 (struct soc_mixer_control *)k->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400279 unsigned int reg = mc->reg;
280 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100281 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400282 unsigned int mask = (1 << fls(max)) - 1;
283 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200284
285 if (power) {
286 int i;
287 /* power up has happended, increase volume to last level */
288 if (invert) {
Jon Smirl4eaa9812008-07-29 11:42:26 +0100289 for (i = max; i > widget->saved_value; i--)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200290 snd_soc_update_bits(widget->codec, reg, mask, i);
291 } else {
292 for (i = 0; i < widget->saved_value; i++)
293 snd_soc_update_bits(widget->codec, reg, mask, i);
294 }
295 widget->muted = 0;
296 } else {
297 /* power down is about to occur, decrease volume to mute */
298 int val = snd_soc_read(widget->codec, reg);
299 int i = widget->saved_value = (val >> shift) & mask;
300 if (invert) {
301 for (; i < mask; i++)
302 snd_soc_update_bits(widget->codec, reg, mask, i);
303 } else {
304 for (; i > 0; i--)
305 snd_soc_update_bits(widget->codec, reg, mask, i);
306 }
307 widget->muted = 1;
308 }
309 }
310 return 0;
311}
312
313/* create new dapm mixer control */
314static int dapm_new_mixer(struct snd_soc_codec *codec,
315 struct snd_soc_dapm_widget *w)
316{
317 int i, ret = 0;
Mark Brown219b93f2008-10-28 13:02:31 +0000318 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200319 struct snd_soc_dapm_path *path;
320
321 /* add kcontrol */
322 for (i = 0; i < w->num_kcontrols; i++) {
323
324 /* match name */
325 list_for_each_entry(path, &w->sources, list_sink) {
326
327 /* mixer/mux paths name must match control name */
328 if (path->name != (char*)w->kcontrols[i].name)
329 continue;
330
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000331 /* add dapm control with long name.
332 * for dapm_mixer this is the concatenation of the
333 * mixer and kcontrol name.
334 * for dapm_mixer_named_ctl this is simply the
335 * kcontrol name.
336 */
337 name_len = strlen(w->kcontrols[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000338 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000339 name_len += 1 + strlen(w->name);
340
Mark Brown219b93f2008-10-28 13:02:31 +0000341 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000342
Richard Purdie2b97eab2006-10-06 18:32:18 +0200343 if (path->long_name == NULL)
344 return -ENOMEM;
345
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000346 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000347 default:
348 snprintf(path->long_name, name_len, "%s %s",
349 w->name, w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000350 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000351 case snd_soc_dapm_mixer_named_ctl:
352 snprintf(path->long_name, name_len, "%s",
353 w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000354 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000355 }
356
Mark Brown219b93f2008-10-28 13:02:31 +0000357 path->long_name[name_len - 1] = '\0';
358
Richard Purdie2b97eab2006-10-06 18:32:18 +0200359 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
360 path->long_name);
361 ret = snd_ctl_add(codec->card, path->kcontrol);
362 if (ret < 0) {
Mark Brown6553e192009-04-06 16:59:32 +0100363 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s: %d\n",
364 path->long_name,
365 ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200366 kfree(path->long_name);
367 path->long_name = NULL;
368 return ret;
369 }
370 }
371 }
372 return ret;
373}
374
375/* create new dapm mux control */
376static int dapm_new_mux(struct snd_soc_codec *codec,
377 struct snd_soc_dapm_widget *w)
378{
379 struct snd_soc_dapm_path *path = NULL;
380 struct snd_kcontrol *kcontrol;
381 int ret = 0;
382
383 if (!w->num_kcontrols) {
384 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
385 return -EINVAL;
386 }
387
388 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
389 ret = snd_ctl_add(codec->card, kcontrol);
390 if (ret < 0)
391 goto err;
392
393 list_for_each_entry(path, &w->sources, list_sink)
394 path->kcontrol = kcontrol;
395
396 return ret;
397
398err:
399 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
400 return ret;
401}
402
403/* create new dapm volume control */
404static int dapm_new_pga(struct snd_soc_codec *codec,
405 struct snd_soc_dapm_widget *w)
406{
407 struct snd_kcontrol *kcontrol;
408 int ret = 0;
409
410 if (!w->num_kcontrols)
411 return -EINVAL;
412
413 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
414 ret = snd_ctl_add(codec->card, kcontrol);
415 if (ret < 0) {
416 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
417 return ret;
418 }
419
420 return ret;
421}
422
423/* reset 'walked' bit for each dapm path */
424static inline void dapm_clear_walk(struct snd_soc_codec *codec)
425{
426 struct snd_soc_dapm_path *p;
427
428 list_for_each_entry(p, &codec->dapm_paths, list)
429 p->walked = 0;
430}
431
432/*
433 * Recursively check for a completed path to an active or physically connected
434 * output widget. Returns number of complete paths.
435 */
436static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
437{
438 struct snd_soc_dapm_path *path;
439 int con = 0;
440
Mark Brown246d0a12009-04-22 18:24:55 +0100441 if (widget->id == snd_soc_dapm_supply)
442 return 0;
443
Richard Purdie2b97eab2006-10-06 18:32:18 +0200444 if (widget->id == snd_soc_dapm_adc && widget->active)
445 return 1;
446
447 if (widget->connected) {
448 /* connected pin ? */
449 if (widget->id == snd_soc_dapm_output && !widget->ext)
450 return 1;
451
452 /* connected jack or spk ? */
453 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
454 widget->id == snd_soc_dapm_line)
455 return 1;
456 }
457
458 list_for_each_entry(path, &widget->sinks, list_source) {
459 if (path->walked)
460 continue;
461
462 if (path->sink && path->connect) {
463 path->walked = 1;
464 con += is_connected_output_ep(path->sink);
465 }
466 }
467
468 return con;
469}
470
471/*
472 * Recursively check for a completed path to an active or physically connected
473 * input widget. Returns number of complete paths.
474 */
475static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
476{
477 struct snd_soc_dapm_path *path;
478 int con = 0;
479
Mark Brown246d0a12009-04-22 18:24:55 +0100480 if (widget->id == snd_soc_dapm_supply)
481 return 0;
482
Richard Purdie2b97eab2006-10-06 18:32:18 +0200483 /* active stream ? */
484 if (widget->id == snd_soc_dapm_dac && widget->active)
485 return 1;
486
487 if (widget->connected) {
488 /* connected pin ? */
489 if (widget->id == snd_soc_dapm_input && !widget->ext)
490 return 1;
491
492 /* connected VMID/Bias for lower pops */
493 if (widget->id == snd_soc_dapm_vmid)
494 return 1;
495
496 /* connected jack ? */
497 if (widget->id == snd_soc_dapm_mic || widget->id == snd_soc_dapm_line)
498 return 1;
499 }
500
501 list_for_each_entry(path, &widget->sources, list_sink) {
502 if (path->walked)
503 continue;
504
505 if (path->source && path->connect) {
506 path->walked = 1;
507 con += is_connected_input_ep(path->source);
508 }
509 }
510
511 return con;
512}
513
514/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300515 * Handler for generic register modifier widget.
516 */
517int dapm_reg_event(struct snd_soc_dapm_widget *w,
518 struct snd_kcontrol *kcontrol, int event)
519{
520 unsigned int val;
521
522 if (SND_SOC_DAPM_EVENT_ON(event))
523 val = w->on_val;
524 else
525 val = w->off_val;
526
527 snd_soc_update_bits(w->codec, -(w->reg + 1),
528 w->mask << w->shift, val << w->shift);
529
530 return 0;
531}
Mark Brown11589412008-07-29 11:42:23 +0100532EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300533
Mark Brown025756e2009-04-13 11:09:18 +0100534/* Standard power change method, used to apply power changes to most
535 * widgets.
536 */
537static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
538{
539 int ret;
540
541 /* call any power change event handlers */
542 if (w->event)
543 pr_debug("power %s event for %s flags %x\n",
544 w->power ? "on" : "off",
545 w->name, w->event_flags);
546
547 /* power up pre event */
548 if (w->power && w->event &&
549 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
550 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
551 if (ret < 0)
552 return ret;
553 }
554
555 /* power down pre event */
556 if (!w->power && w->event &&
557 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
558 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
559 if (ret < 0)
560 return ret;
561 }
562
563 /* Lower PGA volume to reduce pops */
564 if (w->id == snd_soc_dapm_pga && !w->power)
565 dapm_set_pga(w, w->power);
566
567 dapm_update_bits(w);
568
569 /* Raise PGA volume to reduce pops */
570 if (w->id == snd_soc_dapm_pga && w->power)
571 dapm_set_pga(w, w->power);
572
573 /* power up post event */
574 if (w->power && w->event &&
575 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
576 ret = w->event(w,
577 NULL, SND_SOC_DAPM_POST_PMU);
578 if (ret < 0)
579 return ret;
580 }
581
582 /* power down post event */
583 if (!w->power && w->event &&
584 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
585 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
586 if (ret < 0)
587 return ret;
588 }
589
590 return 0;
591}
592
Mark Browncd0f2d42009-04-20 16:56:59 +0100593/* Generic check to see if a widget should be powered.
594 */
595static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
596{
597 int in, out;
598
599 in = is_connected_input_ep(w);
600 dapm_clear_walk(w->codec);
601 out = is_connected_output_ep(w);
602 dapm_clear_walk(w->codec);
603 return out != 0 && in != 0;
604}
605
Mark Brown6ea31b92009-04-20 17:15:41 +0100606/* Check to see if an ADC has power */
607static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
608{
609 int in;
610
611 if (w->active) {
612 in = is_connected_input_ep(w);
613 dapm_clear_walk(w->codec);
614 return in != 0;
615 } else {
616 return dapm_generic_check_power(w);
617 }
618}
619
620/* Check to see if a DAC has power */
621static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
622{
623 int out;
624
625 if (w->active) {
626 out = is_connected_output_ep(w);
627 dapm_clear_walk(w->codec);
628 return out != 0;
629 } else {
630 return dapm_generic_check_power(w);
631 }
632}
633
Mark Brown246d0a12009-04-22 18:24:55 +0100634/* Check to see if a power supply is needed */
635static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
636{
637 struct snd_soc_dapm_path *path;
638 int power = 0;
639
640 /* Check if one of our outputs is connected */
641 list_for_each_entry(path, &w->sinks, list_source) {
642 if (path->sink && path->sink->power_check &&
643 path->sink->power_check(path->sink)) {
644 power = 1;
645 break;
646 }
647 }
648
649 dapm_clear_walk(w->codec);
650
651 return power;
652}
653
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300654/*
Mark Brown42aa3412009-03-01 19:21:10 +0000655 * Scan a single DAPM widget for a complete audio path and update the
656 * power status appropriately.
657 */
658static int dapm_power_widget(struct snd_soc_codec *codec, int event,
659 struct snd_soc_dapm_widget *w)
660{
Mark Brown6d3ddc82009-05-16 17:47:29 +0100661 int ret;
Mark Brown42aa3412009-03-01 19:21:10 +0000662
Mark Brown6ea31b92009-04-20 17:15:41 +0100663 switch (w->id) {
Mark Brown6ea31b92009-04-20 17:15:41 +0100664 case snd_soc_dapm_pre:
Mark Brown42aa3412009-03-01 19:21:10 +0000665 if (!w->event)
666 return 0;
667
668 if (event == SND_SOC_DAPM_STREAM_START) {
669 ret = w->event(w,
670 NULL, SND_SOC_DAPM_PRE_PMU);
671 if (ret < 0)
672 return ret;
673 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
674 ret = w->event(w,
675 NULL, SND_SOC_DAPM_PRE_PMD);
676 if (ret < 0)
677 return ret;
678 }
679 return 0;
Mark Brown6ea31b92009-04-20 17:15:41 +0100680
681 case snd_soc_dapm_post:
Mark Brown42aa3412009-03-01 19:21:10 +0000682 if (!w->event)
683 return 0;
684
685 if (event == SND_SOC_DAPM_STREAM_START) {
686 ret = w->event(w,
687 NULL, SND_SOC_DAPM_POST_PMU);
688 if (ret < 0)
689 return ret;
690 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
691 ret = w->event(w,
692 NULL, SND_SOC_DAPM_POST_PMD);
693 if (ret < 0)
694 return ret;
695 }
696 return 0;
Mark Brown6ea31b92009-04-20 17:15:41 +0100697
698 default:
Mark Brown6d3ddc82009-05-16 17:47:29 +0100699 return dapm_generic_apply_power(w);
Mark Brown42aa3412009-03-01 19:21:10 +0000700 }
Mark Brown42aa3412009-03-01 19:21:10 +0000701}
702
703/*
Richard Purdie2b97eab2006-10-06 18:32:18 +0200704 * Scan each dapm widget for complete audio path.
705 * A complete path is a route that has valid endpoints i.e.:-
706 *
707 * o DAC to output pin.
708 * o Input Pin to ADC.
709 * o Input pin to Output pin (bypass, sidetone)
710 * o DAC to ADC (loopback).
711 */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100712static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200713{
714 struct snd_soc_dapm_widget *w;
Mark Brown6d3ddc82009-05-16 17:47:29 +0100715 int ret = 0;
716 int i, power;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200717
Mark Brown6d3ddc82009-05-16 17:47:29 +0100718 INIT_LIST_HEAD(&codec->up_list);
719 INIT_LIST_HEAD(&codec->down_list);
720
721 /* Check which widgets we need to power and store them in
722 * lists indicating if they should be powered up or down.
723 */
724 list_for_each_entry(w, &codec->dapm_widgets, list) {
725 switch (w->id) {
726 case snd_soc_dapm_pre:
727 list_add_tail(&codec->down_list, &w->power_list);
728 break;
729 case snd_soc_dapm_post:
730 list_add_tail(&codec->up_list, &w->power_list);
731 break;
732
733 default:
734 if (!w->power_check)
735 continue;
736
737 power = w->power_check(w);
738 if (w->power == power)
739 continue;
740
741 if (power)
742 list_add_tail(&w->power_list, &codec->up_list);
743 else
744 list_add_tail(&w->power_list,
745 &codec->down_list);
746
747 w->power = power;
748 break;
749 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200750 }
751
Mark Brown6d3ddc82009-05-16 17:47:29 +0100752 /* Power down widgets first; try to avoid amplifying pops. */
753 for (i = 0; i < ARRAY_SIZE(dapm_down_seq); i++) {
754 list_for_each_entry(w, &codec->down_list, power_list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200755 /* is widget in stream order */
Mark Brown6d3ddc82009-05-16 17:47:29 +0100756 if (w->id != dapm_down_seq[i])
Richard Purdie2b97eab2006-10-06 18:32:18 +0200757 continue;
758
Mark Brown42aa3412009-03-01 19:21:10 +0000759 ret = dapm_power_widget(codec, event, w);
760 if (ret != 0)
Mark Brown6d3ddc82009-05-16 17:47:29 +0100761 pr_err("Failed to power down %s: %d\n",
762 w->name, ret);
763 }
764 }
765
766 /* Now power up. */
767 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++) {
768 list_for_each_entry(w, &codec->up_list, power_list) {
769 /* is widget in stream order */
770 if (w->id != dapm_up_seq[i])
771 continue;
772
773 ret = dapm_power_widget(codec, event, w);
774 if (ret != 0)
775 pr_err("Failed to power up %s: %d\n",
776 w->name, ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200777 }
778 }
779
Mark Brown42aa3412009-03-01 19:21:10 +0000780 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200781}
782
Mark Brownc1286b82008-07-07 19:26:03 +0100783#ifdef DEBUG
Richard Purdie2b97eab2006-10-06 18:32:18 +0200784static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
785{
786 struct snd_soc_dapm_widget *w;
787 struct snd_soc_dapm_path *p = NULL;
788 int in, out;
789
790 printk("DAPM %s %s\n", codec->name, action);
791
792 list_for_each_entry(w, &codec->dapm_widgets, list) {
793
794 /* only display widgets that effect routing */
795 switch (w->id) {
796 case snd_soc_dapm_pre:
797 case snd_soc_dapm_post:
798 case snd_soc_dapm_vmid:
799 continue;
800 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200801 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200802 case snd_soc_dapm_output:
803 case snd_soc_dapm_input:
804 case snd_soc_dapm_switch:
805 case snd_soc_dapm_hp:
806 case snd_soc_dapm_mic:
807 case snd_soc_dapm_spk:
808 case snd_soc_dapm_line:
809 case snd_soc_dapm_micbias:
810 case snd_soc_dapm_dac:
811 case snd_soc_dapm_adc:
812 case snd_soc_dapm_pga:
813 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000814 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +0100815 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200816 if (w->name) {
817 in = is_connected_input_ep(w);
818 dapm_clear_walk(w->codec);
819 out = is_connected_output_ep(w);
820 dapm_clear_walk(w->codec);
821 printk("%s: %s in %d out %d\n", w->name,
822 w->power ? "On":"Off",in, out);
823
824 list_for_each_entry(p, &w->sources, list_sink) {
825 if (p->connect)
826 printk(" in %s %s\n", p->name ? p->name : "static",
827 p->source->name);
828 }
829 list_for_each_entry(p, &w->sinks, list_source) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200830 if (p->connect)
831 printk(" out %s %s\n", p->name ? p->name : "static",
832 p->sink->name);
833 }
834 }
835 break;
836 }
837 }
838}
839#endif
840
841/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100842static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
843 struct snd_kcontrol *kcontrol, int mask,
Richard Zhaocb01e2b2008-10-07 08:05:20 +0800844 int mux, int val, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200845{
846 struct snd_soc_dapm_path *path;
847 int found = 0;
848
Peter Ujfalusieff317d2009-01-15 14:40:47 +0200849 if (widget->id != snd_soc_dapm_mux &&
850 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200851 return -ENODEV;
852
853 if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
854 return 0;
855
856 /* find dapm widget path assoc with kcontrol */
857 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
858 if (path->kcontrol != kcontrol)
859 continue;
860
Richard Zhaocb01e2b2008-10-07 08:05:20 +0800861 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +0200862 continue;
863
864 found = 1;
865 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +0800866 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +0200867 path->connect = 1; /* new connection */
868 else
869 path->connect = 0; /* old connection must be powered down */
870 }
871
Mark Brown3fccd8b2008-07-07 19:26:04 +0100872 if (found) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200873 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
Mark Brown3fccd8b2008-07-07 19:26:04 +0100874 dump_dapm(widget->codec, "mux power update");
875 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200876
877 return 0;
878}
Richard Purdie2b97eab2006-10-06 18:32:18 +0200879
Milan plzik1b075e32008-01-10 14:39:46 +0100880/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100881static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
882 struct snd_kcontrol *kcontrol, int reg,
883 int val_mask, int val, int invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200884{
885 struct snd_soc_dapm_path *path;
886 int found = 0;
887
Milan plzik1b075e32008-01-10 14:39:46 +0100888 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000889 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +0100890 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200891 return -ENODEV;
892
893 if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
894 return 0;
895
896 /* find dapm widget path assoc with kcontrol */
897 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
898 if (path->kcontrol != kcontrol)
899 continue;
900
901 /* found, now check type */
902 found = 1;
903 if (val)
904 /* new connection */
905 path->connect = invert ? 0:1;
906 else
907 /* old connection must be powered down */
908 path->connect = invert ? 1:0;
909 break;
910 }
911
Mark Brown3fccd8b2008-07-07 19:26:04 +0100912 if (found) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200913 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
Mark Brown3fccd8b2008-07-07 19:26:04 +0100914 dump_dapm(widget->codec, "mixer power update");
915 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200916
917 return 0;
918}
Richard Purdie2b97eab2006-10-06 18:32:18 +0200919
920/* show dapm widget status in sys fs */
921static ssize_t dapm_widget_show(struct device *dev,
922 struct device_attribute *attr, char *buf)
923{
924 struct snd_soc_device *devdata = dev_get_drvdata(dev);
Mark Brown6627a652009-01-23 22:55:23 +0000925 struct snd_soc_codec *codec = devdata->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200926 struct snd_soc_dapm_widget *w;
927 int count = 0;
928 char *state = "not set";
929
930 list_for_each_entry(w, &codec->dapm_widgets, list) {
931
932 /* only display widgets that burnm power */
933 switch (w->id) {
934 case snd_soc_dapm_hp:
935 case snd_soc_dapm_mic:
936 case snd_soc_dapm_spk:
937 case snd_soc_dapm_line:
938 case snd_soc_dapm_micbias:
939 case snd_soc_dapm_dac:
940 case snd_soc_dapm_adc:
941 case snd_soc_dapm_pga:
942 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000943 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +0100944 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200945 if (w->name)
946 count += sprintf(buf + count, "%s: %s\n",
947 w->name, w->power ? "On":"Off");
948 break;
949 default:
950 break;
951 }
952 }
953
Mark Brown0be98982008-05-19 12:31:28 +0200954 switch (codec->bias_level) {
955 case SND_SOC_BIAS_ON:
956 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200957 break;
Mark Brown0be98982008-05-19 12:31:28 +0200958 case SND_SOC_BIAS_PREPARE:
959 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200960 break;
Mark Brown0be98982008-05-19 12:31:28 +0200961 case SND_SOC_BIAS_STANDBY:
962 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200963 break;
Mark Brown0be98982008-05-19 12:31:28 +0200964 case SND_SOC_BIAS_OFF:
965 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200966 break;
967 }
968 count += sprintf(buf + count, "PM State: %s\n", state);
969
970 return count;
971}
972
973static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
974
975int snd_soc_dapm_sys_add(struct device *dev)
976{
Mark Brownf0062a92008-08-28 12:46:24 +0100977 if (!dapm_status)
978 return 0;
Troy Kisky12ef1932008-10-13 17:42:14 -0700979 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200980}
981
982static void snd_soc_dapm_sys_remove(struct device *dev)
983{
Mark Brown15e4c722008-07-02 11:51:20 +0100984 if (dapm_status) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200985 device_remove_file(dev, &dev_attr_dapm_widget);
Mark Brown15e4c722008-07-02 11:51:20 +0100986 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200987}
988
989/* free all dapm widgets and resources */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100990static void dapm_free_widgets(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200991{
992 struct snd_soc_dapm_widget *w, *next_w;
993 struct snd_soc_dapm_path *p, *next_p;
994
995 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
996 list_del(&w->list);
997 kfree(w);
998 }
999
1000 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
1001 list_del(&p->list);
1002 kfree(p->long_name);
1003 kfree(p);
1004 }
1005}
1006
Liam Girdwooda5302182008-07-07 13:35:17 +01001007static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
Mark Brown16499232009-01-07 18:25:13 +00001008 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001009{
1010 struct snd_soc_dapm_widget *w;
1011
1012 list_for_each_entry(w, &codec->dapm_widgets, list) {
1013 if (!strcmp(w->name, pin)) {
Mark Brownc1286b82008-07-07 19:26:03 +01001014 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001015 w->connected = status;
1016 return 0;
1017 }
1018 }
1019
Mark Brownc1286b82008-07-07 19:26:03 +01001020 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001021 return -EINVAL;
1022}
1023
Richard Purdie2b97eab2006-10-06 18:32:18 +02001024/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001025 * snd_soc_dapm_sync - scan and power dapm paths
Richard Purdie2b97eab2006-10-06 18:32:18 +02001026 * @codec: audio codec
1027 *
1028 * Walks all dapm audio paths and powers widgets according to their
1029 * stream or path usage.
1030 *
1031 * Returns 0 for success.
1032 */
Liam Girdwooda5302182008-07-07 13:35:17 +01001033int snd_soc_dapm_sync(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001034{
Mark Brown3fccd8b2008-07-07 19:26:04 +01001035 int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1036 dump_dapm(codec, "sync");
1037 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001038}
Liam Girdwooda5302182008-07-07 13:35:17 +01001039EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001040
Mark Brown105f1c22008-05-13 14:52:19 +02001041static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
1042 const char *sink, const char *control, const char *source)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001043{
1044 struct snd_soc_dapm_path *path;
1045 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1046 int ret = 0;
1047
1048 /* find src and dest widgets */
1049 list_for_each_entry(w, &codec->dapm_widgets, list) {
1050
1051 if (!wsink && !(strcmp(w->name, sink))) {
1052 wsink = w;
1053 continue;
1054 }
1055 if (!wsource && !(strcmp(w->name, source))) {
1056 wsource = w;
1057 }
1058 }
1059
1060 if (wsource == NULL || wsink == NULL)
1061 return -ENODEV;
1062
1063 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1064 if (!path)
1065 return -ENOMEM;
1066
1067 path->source = wsource;
1068 path->sink = wsink;
1069 INIT_LIST_HEAD(&path->list);
1070 INIT_LIST_HEAD(&path->list_source);
1071 INIT_LIST_HEAD(&path->list_sink);
1072
1073 /* check for external widgets */
1074 if (wsink->id == snd_soc_dapm_input) {
1075 if (wsource->id == snd_soc_dapm_micbias ||
1076 wsource->id == snd_soc_dapm_mic ||
Seth Forshee1e392212007-04-16 15:36:42 +02001077 wsink->id == snd_soc_dapm_line ||
1078 wsink->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001079 wsink->ext = 1;
1080 }
1081 if (wsource->id == snd_soc_dapm_output) {
1082 if (wsink->id == snd_soc_dapm_spk ||
1083 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001084 wsink->id == snd_soc_dapm_line ||
1085 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001086 wsource->ext = 1;
1087 }
1088
1089 /* connect static paths */
1090 if (control == NULL) {
1091 list_add(&path->list, &codec->dapm_paths);
1092 list_add(&path->list_sink, &wsink->sources);
1093 list_add(&path->list_source, &wsource->sinks);
1094 path->connect = 1;
1095 return 0;
1096 }
1097
1098 /* connect dynamic paths */
1099 switch(wsink->id) {
1100 case snd_soc_dapm_adc:
1101 case snd_soc_dapm_dac:
1102 case snd_soc_dapm_pga:
1103 case snd_soc_dapm_input:
1104 case snd_soc_dapm_output:
1105 case snd_soc_dapm_micbias:
1106 case snd_soc_dapm_vmid:
1107 case snd_soc_dapm_pre:
1108 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001109 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001110 list_add(&path->list, &codec->dapm_paths);
1111 list_add(&path->list_sink, &wsink->sources);
1112 list_add(&path->list_source, &wsource->sinks);
1113 path->connect = 1;
1114 return 0;
1115 case snd_soc_dapm_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001116 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001117 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
1118 &wsink->kcontrols[0]);
1119 if (ret != 0)
1120 goto err;
1121 break;
1122 case snd_soc_dapm_switch:
1123 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001124 case snd_soc_dapm_mixer_named_ctl:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001125 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
1126 if (ret != 0)
1127 goto err;
1128 break;
1129 case snd_soc_dapm_hp:
1130 case snd_soc_dapm_mic:
1131 case snd_soc_dapm_line:
1132 case snd_soc_dapm_spk:
1133 list_add(&path->list, &codec->dapm_paths);
1134 list_add(&path->list_sink, &wsink->sources);
1135 list_add(&path->list_source, &wsource->sinks);
1136 path->connect = 0;
1137 return 0;
1138 }
1139 return 0;
1140
1141err:
1142 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1143 control, sink);
1144 kfree(path);
1145 return ret;
1146}
Mark Brown105f1c22008-05-13 14:52:19 +02001147
1148/**
Mark Brown105f1c22008-05-13 14:52:19 +02001149 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1150 * @codec: codec
1151 * @route: audio routes
1152 * @num: number of routes
1153 *
1154 * Connects 2 dapm widgets together via a named audio path. The sink is
1155 * the widget receiving the audio signal, whilst the source is the sender
1156 * of the audio signal.
1157 *
1158 * Returns 0 for success else error. On error all resources can be freed
1159 * with a call to snd_soc_card_free().
1160 */
1161int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1162 const struct snd_soc_dapm_route *route, int num)
1163{
1164 int i, ret;
1165
1166 for (i = 0; i < num; i++) {
1167 ret = snd_soc_dapm_add_route(codec, route->sink,
1168 route->control, route->source);
1169 if (ret < 0) {
1170 printk(KERN_ERR "Failed to add route %s->%s\n",
1171 route->source,
1172 route->sink);
1173 return ret;
1174 }
1175 route++;
1176 }
1177
1178 return 0;
1179}
1180EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1181
1182/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001183 * snd_soc_dapm_new_widgets - add new dapm widgets
1184 * @codec: audio codec
1185 *
1186 * Checks the codec for any new dapm widgets and creates them if found.
1187 *
1188 * Returns 0 for success.
1189 */
1190int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1191{
1192 struct snd_soc_dapm_widget *w;
1193
Richard Purdie2b97eab2006-10-06 18:32:18 +02001194 list_for_each_entry(w, &codec->dapm_widgets, list)
1195 {
1196 if (w->new)
1197 continue;
1198
1199 switch(w->id) {
1200 case snd_soc_dapm_switch:
1201 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001202 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001203 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001204 dapm_new_mixer(codec, w);
1205 break;
1206 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001207 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001208 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001209 dapm_new_mux(codec, w);
1210 break;
1211 case snd_soc_dapm_adc:
Mark Brownb75576d2009-04-20 17:56:13 +01001212 w->power_check = dapm_adc_check_power;
1213 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001214 case snd_soc_dapm_dac:
Mark Brownb75576d2009-04-20 17:56:13 +01001215 w->power_check = dapm_dac_check_power;
1216 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001217 case snd_soc_dapm_pga:
Mark Brownb75576d2009-04-20 17:56:13 +01001218 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001219 dapm_new_pga(codec, w);
1220 break;
1221 case snd_soc_dapm_input:
1222 case snd_soc_dapm_output:
1223 case snd_soc_dapm_micbias:
1224 case snd_soc_dapm_spk:
1225 case snd_soc_dapm_hp:
1226 case snd_soc_dapm_mic:
1227 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001228 w->power_check = dapm_generic_check_power;
1229 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001230 case snd_soc_dapm_supply:
1231 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001232 case snd_soc_dapm_vmid:
1233 case snd_soc_dapm_pre:
1234 case snd_soc_dapm_post:
1235 break;
1236 }
1237 w->new = 1;
1238 }
1239
1240 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001241 return 0;
1242}
1243EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1244
1245/**
1246 * snd_soc_dapm_get_volsw - dapm mixer get callback
1247 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001248 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001249 *
1250 * Callback to get the value of a dapm mixer control.
1251 *
1252 * Returns 0 for success.
1253 */
1254int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1255 struct snd_ctl_elem_value *ucontrol)
1256{
1257 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001258 struct soc_mixer_control *mc =
1259 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001260 unsigned int reg = mc->reg;
1261 unsigned int shift = mc->shift;
1262 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001263 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001264 unsigned int invert = mc->invert;
1265 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001266
1267 /* return the saved value if we are powered down */
1268 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1269 ucontrol->value.integer.value[0] = widget->saved_value;
1270 return 0;
1271 }
1272
1273 ucontrol->value.integer.value[0] =
1274 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1275 if (shift != rshift)
1276 ucontrol->value.integer.value[1] =
1277 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1278 if (invert) {
1279 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001280 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001281 if (shift != rshift)
1282 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001283 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001284 }
1285
1286 return 0;
1287}
1288EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1289
1290/**
1291 * snd_soc_dapm_put_volsw - dapm mixer set callback
1292 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001293 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001294 *
1295 * Callback to set the value of a dapm mixer control.
1296 *
1297 * Returns 0 for success.
1298 */
1299int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1300 struct snd_ctl_elem_value *ucontrol)
1301{
1302 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001303 struct soc_mixer_control *mc =
1304 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001305 unsigned int reg = mc->reg;
1306 unsigned int shift = mc->shift;
1307 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001308 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001309 unsigned int mask = (1 << fls(max)) - 1;
1310 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001311 unsigned short val, val2, val_mask;
1312 int ret;
1313
1314 val = (ucontrol->value.integer.value[0] & mask);
1315
1316 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001317 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001318 val_mask = mask << shift;
1319 val = val << shift;
1320 if (shift != rshift) {
1321 val2 = (ucontrol->value.integer.value[1] & mask);
1322 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001323 val2 = max - val2;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001324 val_mask |= mask << rshift;
1325 val |= val2 << rshift;
1326 }
1327
1328 mutex_lock(&widget->codec->mutex);
1329 widget->value = val;
1330
1331 /* save volume value if the widget is powered down */
1332 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1333 widget->saved_value = val;
1334 mutex_unlock(&widget->codec->mutex);
1335 return 1;
1336 }
1337
1338 dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
1339 if (widget->event) {
1340 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001341 ret = widget->event(widget, kcontrol,
1342 SND_SOC_DAPM_PRE_REG);
1343 if (ret < 0) {
1344 ret = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001345 goto out;
Laim Girdwood9af6d952008-01-10 14:41:02 +01001346 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001347 }
1348 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1349 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001350 ret = widget->event(widget, kcontrol,
1351 SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001352 } else
1353 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1354
1355out:
1356 mutex_unlock(&widget->codec->mutex);
1357 return ret;
1358}
1359EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1360
1361/**
1362 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1363 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001364 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001365 *
1366 * Callback to get the value of a dapm enumerated double mixer control.
1367 *
1368 * Returns 0 for success.
1369 */
1370int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1371 struct snd_ctl_elem_value *ucontrol)
1372{
1373 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1374 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1375 unsigned short val, bitmask;
1376
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001377 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001378 ;
1379 val = snd_soc_read(widget->codec, e->reg);
1380 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1381 if (e->shift_l != e->shift_r)
1382 ucontrol->value.enumerated.item[1] =
1383 (val >> e->shift_r) & (bitmask - 1);
1384
1385 return 0;
1386}
1387EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1388
1389/**
1390 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1391 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001392 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001393 *
1394 * Callback to set the value of a dapm enumerated double mixer control.
1395 *
1396 * Returns 0 for success.
1397 */
1398int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1399 struct snd_ctl_elem_value *ucontrol)
1400{
1401 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1402 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1403 unsigned short val, mux;
1404 unsigned short mask, bitmask;
1405 int ret = 0;
1406
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001407 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001408 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001409 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001410 return -EINVAL;
1411 mux = ucontrol->value.enumerated.item[0];
1412 val = mux << e->shift_l;
1413 mask = (bitmask - 1) << e->shift_l;
1414 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001415 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001416 return -EINVAL;
1417 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1418 mask |= (bitmask - 1) << e->shift_r;
1419 }
1420
1421 mutex_lock(&widget->codec->mutex);
1422 widget->value = val;
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001423 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001424 if (widget->event) {
1425 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001426 ret = widget->event(widget,
1427 kcontrol, SND_SOC_DAPM_PRE_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001428 if (ret < 0)
1429 goto out;
1430 }
1431 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1432 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001433 ret = widget->event(widget,
1434 kcontrol, SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001435 } else
1436 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1437
1438out:
1439 mutex_unlock(&widget->codec->mutex);
1440 return ret;
1441}
1442EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1443
1444/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001445 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1446 * callback
1447 * @kcontrol: mixer control
1448 * @ucontrol: control element information
1449 *
1450 * Callback to get the value of a dapm semi enumerated double mixer control.
1451 *
1452 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1453 * used for handling bitfield coded enumeration for example.
1454 *
1455 * Returns 0 for success.
1456 */
1457int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1458 struct snd_ctl_elem_value *ucontrol)
1459{
1460 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001461 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001462 unsigned short reg_val, val, mux;
1463
1464 reg_val = snd_soc_read(widget->codec, e->reg);
1465 val = (reg_val >> e->shift_l) & e->mask;
1466 for (mux = 0; mux < e->max; mux++) {
1467 if (val == e->values[mux])
1468 break;
1469 }
1470 ucontrol->value.enumerated.item[0] = mux;
1471 if (e->shift_l != e->shift_r) {
1472 val = (reg_val >> e->shift_r) & e->mask;
1473 for (mux = 0; mux < e->max; mux++) {
1474 if (val == e->values[mux])
1475 break;
1476 }
1477 ucontrol->value.enumerated.item[1] = mux;
1478 }
1479
1480 return 0;
1481}
1482EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1483
1484/**
1485 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1486 * callback
1487 * @kcontrol: mixer control
1488 * @ucontrol: control element information
1489 *
1490 * Callback to set the value of a dapm semi enumerated double mixer control.
1491 *
1492 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1493 * used for handling bitfield coded enumeration for example.
1494 *
1495 * Returns 0 for success.
1496 */
1497int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1498 struct snd_ctl_elem_value *ucontrol)
1499{
1500 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001501 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001502 unsigned short val, mux;
1503 unsigned short mask;
1504 int ret = 0;
1505
1506 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1507 return -EINVAL;
1508 mux = ucontrol->value.enumerated.item[0];
1509 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1510 mask = e->mask << e->shift_l;
1511 if (e->shift_l != e->shift_r) {
1512 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1513 return -EINVAL;
1514 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1515 mask |= e->mask << e->shift_r;
1516 }
1517
1518 mutex_lock(&widget->codec->mutex);
1519 widget->value = val;
Peter Ujfalusi74155552009-01-08 13:34:29 +02001520 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001521 if (widget->event) {
1522 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1523 ret = widget->event(widget,
1524 kcontrol, SND_SOC_DAPM_PRE_REG);
1525 if (ret < 0)
1526 goto out;
1527 }
1528 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1529 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1530 ret = widget->event(widget,
1531 kcontrol, SND_SOC_DAPM_POST_REG);
1532 } else
1533 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1534
1535out:
1536 mutex_unlock(&widget->codec->mutex);
1537 return ret;
1538}
1539EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1540
1541/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00001542 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1543 *
1544 * @kcontrol: mixer control
1545 * @uinfo: control element information
1546 *
1547 * Callback to provide information about a pin switch control.
1548 */
1549int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1550 struct snd_ctl_elem_info *uinfo)
1551{
1552 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1553 uinfo->count = 1;
1554 uinfo->value.integer.min = 0;
1555 uinfo->value.integer.max = 1;
1556
1557 return 0;
1558}
1559EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1560
1561/**
1562 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1563 *
1564 * @kcontrol: mixer control
1565 * @ucontrol: Value
1566 */
1567int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
1568 struct snd_ctl_elem_value *ucontrol)
1569{
1570 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1571 const char *pin = (const char *)kcontrol->private_value;
1572
1573 mutex_lock(&codec->mutex);
1574
1575 ucontrol->value.integer.value[0] =
1576 snd_soc_dapm_get_pin_status(codec, pin);
1577
1578 mutex_unlock(&codec->mutex);
1579
1580 return 0;
1581}
1582EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
1583
1584/**
1585 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1586 *
1587 * @kcontrol: mixer control
1588 * @ucontrol: Value
1589 */
1590int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
1591 struct snd_ctl_elem_value *ucontrol)
1592{
1593 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1594 const char *pin = (const char *)kcontrol->private_value;
1595
1596 mutex_lock(&codec->mutex);
1597
1598 if (ucontrol->value.integer.value[0])
1599 snd_soc_dapm_enable_pin(codec, pin);
1600 else
1601 snd_soc_dapm_disable_pin(codec, pin);
1602
1603 snd_soc_dapm_sync(codec);
1604
1605 mutex_unlock(&codec->mutex);
1606
1607 return 0;
1608}
1609EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
1610
1611/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001612 * snd_soc_dapm_new_control - create new dapm control
1613 * @codec: audio codec
1614 * @widget: widget template
1615 *
1616 * Creates a new dapm control based upon the template.
1617 *
1618 * Returns 0 for success else error.
1619 */
1620int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1621 const struct snd_soc_dapm_widget *widget)
1622{
1623 struct snd_soc_dapm_widget *w;
1624
1625 if ((w = dapm_cnew_widget(widget)) == NULL)
1626 return -ENOMEM;
1627
1628 w->codec = codec;
1629 INIT_LIST_HEAD(&w->sources);
1630 INIT_LIST_HEAD(&w->sinks);
1631 INIT_LIST_HEAD(&w->list);
1632 list_add(&w->list, &codec->dapm_widgets);
1633
1634 /* machine layer set ups unconnected pins and insertions */
1635 w->connected = 1;
1636 return 0;
1637}
1638EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1639
1640/**
Mark Brown4ba13272008-05-13 14:51:19 +02001641 * snd_soc_dapm_new_controls - create new dapm controls
1642 * @codec: audio codec
1643 * @widget: widget array
1644 * @num: number of widgets
1645 *
1646 * Creates new DAPM controls based upon the templates.
1647 *
1648 * Returns 0 for success else error.
1649 */
1650int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1651 const struct snd_soc_dapm_widget *widget,
1652 int num)
1653{
1654 int i, ret;
1655
1656 for (i = 0; i < num; i++) {
1657 ret = snd_soc_dapm_new_control(codec, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00001658 if (ret < 0) {
1659 printk(KERN_ERR
1660 "ASoC: Failed to create DAPM control %s: %d\n",
1661 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02001662 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00001663 }
Mark Brown4ba13272008-05-13 14:51:19 +02001664 widget++;
1665 }
1666 return 0;
1667}
1668EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1669
1670
1671/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001672 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1673 * @codec: audio codec
1674 * @stream: stream name
1675 * @event: stream event
1676 *
1677 * Sends a stream event to the dapm core. The core then makes any
1678 * necessary widget power changes.
1679 *
1680 * Returns 0 for success else error.
1681 */
1682int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
1683 char *stream, int event)
1684{
1685 struct snd_soc_dapm_widget *w;
1686
Seth Forshee11da21a2007-02-02 17:14:19 +01001687 if (stream == NULL)
1688 return 0;
1689
Richard Purdie2b97eab2006-10-06 18:32:18 +02001690 mutex_lock(&codec->mutex);
1691 list_for_each_entry(w, &codec->dapm_widgets, list)
1692 {
1693 if (!w->sname)
1694 continue;
Mark Brownc1286b82008-07-07 19:26:03 +01001695 pr_debug("widget %s\n %s stream %s event %d\n",
1696 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001697 if (strstr(w->sname, stream)) {
1698 switch(event) {
1699 case SND_SOC_DAPM_STREAM_START:
1700 w->active = 1;
1701 break;
1702 case SND_SOC_DAPM_STREAM_STOP:
1703 w->active = 0;
1704 break;
1705 case SND_SOC_DAPM_STREAM_SUSPEND:
1706 if (w->active)
1707 w->suspend = 1;
1708 w->active = 0;
1709 break;
1710 case SND_SOC_DAPM_STREAM_RESUME:
1711 if (w->suspend) {
1712 w->active = 1;
1713 w->suspend = 0;
1714 }
1715 break;
1716 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
1717 break;
1718 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
1719 break;
1720 }
1721 }
1722 }
1723 mutex_unlock(&codec->mutex);
1724
1725 dapm_power_widgets(codec, event);
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -08001726 dump_dapm(codec, __func__);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001727 return 0;
1728}
1729EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
1730
1731/**
Mark Brown0be98982008-05-19 12:31:28 +02001732 * snd_soc_dapm_set_bias_level - set the bias level for the system
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001733 * @socdev: audio device
Mark Brown0be98982008-05-19 12:31:28 +02001734 * @level: level to configure
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001735 *
Mark Brown0be98982008-05-19 12:31:28 +02001736 * Configure the bias (power) levels for the SoC audio device.
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001737 *
1738 * Returns 0 for success else error.
1739 */
Mark Brown0be98982008-05-19 12:31:28 +02001740int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
1741 enum snd_soc_bias_level level)
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001742{
Mark Brown87506542008-11-18 20:50:34 +00001743 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +00001744 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brown0be98982008-05-19 12:31:28 +02001745 int ret = 0;
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001746
Mark Brown87506542008-11-18 20:50:34 +00001747 if (card->set_bias_level)
1748 ret = card->set_bias_level(card, level);
Mark Brown0be98982008-05-19 12:31:28 +02001749 if (ret == 0 && codec->set_bias_level)
1750 ret = codec->set_bias_level(codec, level);
1751
1752 return ret;
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001753}
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001754
1755/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001756 * snd_soc_dapm_enable_pin - enable pin.
Mark Brownac11a2b2009-01-01 12:18:17 +00001757 * @codec: SoC codec
Liam Girdwooda5302182008-07-07 13:35:17 +01001758 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02001759 *
Liam Girdwooda5302182008-07-07 13:35:17 +01001760 * Enables input/output pin and it's parents or children widgets iff there is
1761 * a valid audio route and active audio stream.
1762 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1763 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001764 */
Mark Brown16499232009-01-07 18:25:13 +00001765int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001766{
Liam Girdwooda5302182008-07-07 13:35:17 +01001767 return snd_soc_dapm_set_pin(codec, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001768}
Liam Girdwooda5302182008-07-07 13:35:17 +01001769EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001770
1771/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001772 * snd_soc_dapm_disable_pin - disable pin.
1773 * @codec: SoC codec
1774 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001775 *
Liam Girdwooda5302182008-07-07 13:35:17 +01001776 * Disables input/output pin and it's parents or children widgets.
1777 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1778 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001779 */
Mark Brown16499232009-01-07 18:25:13 +00001780int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01001781{
1782 return snd_soc_dapm_set_pin(codec, pin, 0);
1783}
1784EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
1785
1786/**
Mark Brown5817b522008-09-24 11:23:11 +01001787 * snd_soc_dapm_nc_pin - permanently disable pin.
1788 * @codec: SoC codec
1789 * @pin: pin name
1790 *
1791 * Marks the specified pin as being not connected, disabling it along
1792 * any parent or child widgets. At present this is identical to
1793 * snd_soc_dapm_disable_pin() but in future it will be extended to do
1794 * additional things such as disabling controls which only affect
1795 * paths through the pin.
1796 *
1797 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1798 * do any widget power switching.
1799 */
Mark Brown16499232009-01-07 18:25:13 +00001800int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01001801{
1802 return snd_soc_dapm_set_pin(codec, pin, 0);
1803}
1804EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
1805
1806/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001807 * snd_soc_dapm_get_pin_status - get audio pin status
1808 * @codec: audio codec
1809 * @pin: audio signal pin endpoint (or start point)
1810 *
1811 * Get audio pin status - connected or disconnected.
1812 *
1813 * Returns 1 for connected otherwise 0.
1814 */
Mark Brown16499232009-01-07 18:25:13 +00001815int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001816{
1817 struct snd_soc_dapm_widget *w;
1818
1819 list_for_each_entry(w, &codec->dapm_widgets, list) {
Liam Girdwooda5302182008-07-07 13:35:17 +01001820 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001821 return w->connected;
1822 }
1823
1824 return 0;
1825}
Liam Girdwooda5302182008-07-07 13:35:17 +01001826EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001827
1828/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001829 * snd_soc_dapm_free - free dapm resources
1830 * @socdev: SoC device
1831 *
1832 * Free all dapm widgets and resources.
1833 */
1834void snd_soc_dapm_free(struct snd_soc_device *socdev)
1835{
Mark Brown6627a652009-01-23 22:55:23 +00001836 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001837
1838 snd_soc_dapm_sys_remove(socdev->dev);
1839 dapm_free_widgets(codec);
1840}
1841EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
1842
1843/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01001844MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001845MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
1846MODULE_LICENSE("GPL");