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