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