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