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