ASoC: dapm: Drop runtime PM references asynchronously
[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/async.h>
36 #include <linux/delay.h>
37 #include <linux/pm.h>
38 #include <linux/bitops.h>
39 #include <linux/platform_device.h>
40 #include <linux/jiffies.h>
41 #include <linux/debugfs.h>
42 #include <linux/pm_runtime.h>
43 #include <linux/regulator/consumer.h>
44 #include <linux/slab.h>
45 #include <sound/core.h>
46 #include <sound/pcm.h>
47 #include <sound/pcm_params.h>
48 #include <sound/soc.h>
49 #include <sound/initval.h>
50
51 #include <trace/events/asoc.h>
52
53 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
54
55 /* dapm power sequences - make this per codec in the future */
56 static int dapm_up_seq[] = {
57 [snd_soc_dapm_pre] = 0,
58 [snd_soc_dapm_supply] = 1,
59 [snd_soc_dapm_regulator_supply] = 1,
60 [snd_soc_dapm_micbias] = 2,
61 [snd_soc_dapm_aif_in] = 3,
62 [snd_soc_dapm_aif_out] = 3,
63 [snd_soc_dapm_mic] = 4,
64 [snd_soc_dapm_mux] = 5,
65 [snd_soc_dapm_virt_mux] = 5,
66 [snd_soc_dapm_value_mux] = 5,
67 [snd_soc_dapm_dac] = 6,
68 [snd_soc_dapm_mixer] = 7,
69 [snd_soc_dapm_mixer_named_ctl] = 7,
70 [snd_soc_dapm_pga] = 8,
71 [snd_soc_dapm_adc] = 9,
72 [snd_soc_dapm_out_drv] = 10,
73 [snd_soc_dapm_hp] = 10,
74 [snd_soc_dapm_spk] = 10,
75 [snd_soc_dapm_post] = 11,
76 };
77
78 static int dapm_down_seq[] = {
79 [snd_soc_dapm_pre] = 0,
80 [snd_soc_dapm_adc] = 1,
81 [snd_soc_dapm_hp] = 2,
82 [snd_soc_dapm_spk] = 2,
83 [snd_soc_dapm_out_drv] = 2,
84 [snd_soc_dapm_pga] = 4,
85 [snd_soc_dapm_mixer_named_ctl] = 5,
86 [snd_soc_dapm_mixer] = 5,
87 [snd_soc_dapm_dac] = 6,
88 [snd_soc_dapm_mic] = 7,
89 [snd_soc_dapm_micbias] = 8,
90 [snd_soc_dapm_mux] = 9,
91 [snd_soc_dapm_virt_mux] = 9,
92 [snd_soc_dapm_value_mux] = 9,
93 [snd_soc_dapm_aif_in] = 10,
94 [snd_soc_dapm_aif_out] = 10,
95 [snd_soc_dapm_regulator_supply] = 11,
96 [snd_soc_dapm_supply] = 11,
97 [snd_soc_dapm_post] = 12,
98 };
99
100 static void pop_wait(u32 pop_time)
101 {
102 if (pop_time)
103 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
104 }
105
106 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
107 {
108 va_list args;
109 char *buf;
110
111 if (!pop_time)
112 return;
113
114 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
115 if (buf == NULL)
116 return;
117
118 va_start(args, fmt);
119 vsnprintf(buf, PAGE_SIZE, fmt, args);
120 dev_info(dev, "%s", buf);
121 va_end(args);
122
123 kfree(buf);
124 }
125
126 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
127 {
128 return !list_empty(&w->dirty);
129 }
130
131 void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
132 {
133 if (!dapm_dirty_widget(w)) {
134 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
135 w->name, reason);
136 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
137 }
138 }
139 EXPORT_SYMBOL_GPL(dapm_mark_dirty);
140
141 /* create a new dapm widget */
142 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
143 const struct snd_soc_dapm_widget *_widget)
144 {
145 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
146 }
147
148 /* get snd_card from DAPM context */
149 static inline struct snd_card *dapm_get_snd_card(
150 struct snd_soc_dapm_context *dapm)
151 {
152 if (dapm->codec)
153 return dapm->codec->card->snd_card;
154 else if (dapm->platform)
155 return dapm->platform->card->snd_card;
156 else
157 BUG();
158
159 /* unreachable */
160 return NULL;
161 }
162
163 /* get soc_card from DAPM context */
164 static inline struct snd_soc_card *dapm_get_soc_card(
165 struct snd_soc_dapm_context *dapm)
166 {
167 if (dapm->codec)
168 return dapm->codec->card;
169 else if (dapm->platform)
170 return dapm->platform->card;
171 else
172 BUG();
173
174 /* unreachable */
175 return NULL;
176 }
177
178 static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
179 {
180 if (w->codec)
181 return snd_soc_read(w->codec, reg);
182 else if (w->platform)
183 return snd_soc_platform_read(w->platform, reg);
184
185 dev_err(w->dapm->dev, "no valid widget read method\n");
186 return -1;
187 }
188
189 static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
190 {
191 if (w->codec)
192 return snd_soc_write(w->codec, reg, val);
193 else if (w->platform)
194 return snd_soc_platform_write(w->platform, reg, val);
195
196 dev_err(w->dapm->dev, "no valid widget write method\n");
197 return -1;
198 }
199
200 static int soc_widget_update_bits(struct snd_soc_dapm_widget *w,
201 unsigned short reg, unsigned int mask, unsigned int value)
202 {
203 bool change;
204 unsigned int old, new;
205 int ret;
206
207 if (w->codec && w->codec->using_regmap) {
208 ret = regmap_update_bits_check(w->codec->control_data,
209 reg, mask, value, &change);
210 if (ret != 0)
211 return ret;
212 } else {
213 ret = soc_widget_read(w, reg);
214 if (ret < 0)
215 return ret;
216
217 old = ret;
218 new = (old & ~mask) | (value & mask);
219 change = old != new;
220 if (change) {
221 ret = soc_widget_write(w, reg, new);
222 if (ret < 0)
223 return ret;
224 }
225 }
226
227 return change;
228 }
229
230 /**
231 * snd_soc_dapm_set_bias_level - set the bias level for the system
232 * @dapm: DAPM context
233 * @level: level to configure
234 *
235 * Configure the bias (power) levels for the SoC audio device.
236 *
237 * Returns 0 for success else error.
238 */
239 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
240 enum snd_soc_bias_level level)
241 {
242 struct snd_soc_card *card = dapm->card;
243 int ret = 0;
244
245 trace_snd_soc_bias_level_start(card, level);
246
247 if (card && card->set_bias_level)
248 ret = card->set_bias_level(card, dapm, level);
249 if (ret != 0)
250 goto out;
251
252 if (dapm->codec) {
253 if (dapm->codec->driver->set_bias_level)
254 ret = dapm->codec->driver->set_bias_level(dapm->codec,
255 level);
256 else
257 dapm->bias_level = level;
258 }
259 if (ret != 0)
260 goto out;
261
262 if (card && card->set_bias_level_post)
263 ret = card->set_bias_level_post(card, dapm, level);
264 out:
265 trace_snd_soc_bias_level_done(card, level);
266
267 return ret;
268 }
269
270 /* set up initial codec paths */
271 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
272 struct snd_soc_dapm_path *p, int i)
273 {
274 switch (w->id) {
275 case snd_soc_dapm_switch:
276 case snd_soc_dapm_mixer:
277 case snd_soc_dapm_mixer_named_ctl: {
278 int val;
279 struct soc_mixer_control *mc = (struct soc_mixer_control *)
280 w->kcontrol_news[i].private_value;
281 unsigned int reg = mc->reg;
282 unsigned int shift = mc->shift;
283 int max = mc->max;
284 unsigned int mask = (1 << fls(max)) - 1;
285 unsigned int invert = mc->invert;
286
287 val = soc_widget_read(w, reg);
288 val = (val >> shift) & mask;
289
290 if ((invert && !val) || (!invert && val))
291 p->connect = 1;
292 else
293 p->connect = 0;
294 }
295 break;
296 case snd_soc_dapm_mux: {
297 struct soc_enum *e = (struct soc_enum *)
298 w->kcontrol_news[i].private_value;
299 int val, item, bitmask;
300
301 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
302 ;
303 val = soc_widget_read(w, e->reg);
304 item = (val >> e->shift_l) & (bitmask - 1);
305
306 p->connect = 0;
307 for (i = 0; i < e->max; i++) {
308 if (!(strcmp(p->name, e->texts[i])) && item == i)
309 p->connect = 1;
310 }
311 }
312 break;
313 case snd_soc_dapm_virt_mux: {
314 struct soc_enum *e = (struct soc_enum *)
315 w->kcontrol_news[i].private_value;
316
317 p->connect = 0;
318 /* since a virtual mux has no backing registers to
319 * decide which path to connect, it will try to match
320 * with the first enumeration. This is to ensure
321 * that the default mux choice (the first) will be
322 * correctly powered up during initialization.
323 */
324 if (!strcmp(p->name, e->texts[0]))
325 p->connect = 1;
326 }
327 break;
328 case snd_soc_dapm_value_mux: {
329 struct soc_enum *e = (struct soc_enum *)
330 w->kcontrol_news[i].private_value;
331 int val, item;
332
333 val = soc_widget_read(w, e->reg);
334 val = (val >> e->shift_l) & e->mask;
335 for (item = 0; item < e->max; item++) {
336 if (val == e->values[item])
337 break;
338 }
339
340 p->connect = 0;
341 for (i = 0; i < e->max; i++) {
342 if (!(strcmp(p->name, e->texts[i])) && item == i)
343 p->connect = 1;
344 }
345 }
346 break;
347 /* does not affect routing - always connected */
348 case snd_soc_dapm_pga:
349 case snd_soc_dapm_out_drv:
350 case snd_soc_dapm_output:
351 case snd_soc_dapm_adc:
352 case snd_soc_dapm_input:
353 case snd_soc_dapm_siggen:
354 case snd_soc_dapm_dac:
355 case snd_soc_dapm_micbias:
356 case snd_soc_dapm_vmid:
357 case snd_soc_dapm_supply:
358 case snd_soc_dapm_regulator_supply:
359 case snd_soc_dapm_aif_in:
360 case snd_soc_dapm_aif_out:
361 case snd_soc_dapm_hp:
362 case snd_soc_dapm_mic:
363 case snd_soc_dapm_spk:
364 case snd_soc_dapm_line:
365 p->connect = 1;
366 break;
367 /* does affect routing - dynamically connected */
368 case snd_soc_dapm_pre:
369 case snd_soc_dapm_post:
370 p->connect = 0;
371 break;
372 }
373 }
374
375 /* connect mux widget to its interconnecting audio paths */
376 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
377 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
378 struct snd_soc_dapm_path *path, const char *control_name,
379 const struct snd_kcontrol_new *kcontrol)
380 {
381 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
382 int i;
383
384 for (i = 0; i < e->max; i++) {
385 if (!(strcmp(control_name, e->texts[i]))) {
386 list_add(&path->list, &dapm->card->paths);
387 list_add(&path->list_sink, &dest->sources);
388 list_add(&path->list_source, &src->sinks);
389 path->name = (char*)e->texts[i];
390 dapm_set_path_status(dest, path, 0);
391 return 0;
392 }
393 }
394
395 return -ENODEV;
396 }
397
398 /* connect mixer widget to its interconnecting audio paths */
399 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
400 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
401 struct snd_soc_dapm_path *path, const char *control_name)
402 {
403 int i;
404
405 /* search for mixer kcontrol */
406 for (i = 0; i < dest->num_kcontrols; i++) {
407 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
408 list_add(&path->list, &dapm->card->paths);
409 list_add(&path->list_sink, &dest->sources);
410 list_add(&path->list_source, &src->sinks);
411 path->name = dest->kcontrol_news[i].name;
412 dapm_set_path_status(dest, path, i);
413 return 0;
414 }
415 }
416 return -ENODEV;
417 }
418
419 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
420 struct snd_soc_dapm_widget *kcontrolw,
421 const struct snd_kcontrol_new *kcontrol_new,
422 struct snd_kcontrol **kcontrol)
423 {
424 struct snd_soc_dapm_widget *w;
425 int i;
426
427 *kcontrol = NULL;
428
429 list_for_each_entry(w, &dapm->card->widgets, list) {
430 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
431 continue;
432 for (i = 0; i < w->num_kcontrols; i++) {
433 if (&w->kcontrol_news[i] == kcontrol_new) {
434 if (w->kcontrols)
435 *kcontrol = w->kcontrols[i];
436 return 1;
437 }
438 }
439 }
440
441 return 0;
442 }
443
444 /* create new dapm mixer control */
445 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
446 {
447 struct snd_soc_dapm_context *dapm = w->dapm;
448 int i, ret = 0;
449 size_t name_len, prefix_len;
450 struct snd_soc_dapm_path *path;
451 struct snd_card *card = dapm->card->snd_card;
452 const char *prefix;
453 struct snd_soc_dapm_widget_list *wlist;
454 size_t wlistsize;
455
456 if (dapm->codec)
457 prefix = dapm->codec->name_prefix;
458 else
459 prefix = NULL;
460
461 if (prefix)
462 prefix_len = strlen(prefix) + 1;
463 else
464 prefix_len = 0;
465
466 /* add kcontrol */
467 for (i = 0; i < w->num_kcontrols; i++) {
468
469 /* match name */
470 list_for_each_entry(path, &w->sources, list_sink) {
471
472 /* mixer/mux paths name must match control name */
473 if (path->name != (char *)w->kcontrol_news[i].name)
474 continue;
475
476 if (w->kcontrols[i]) {
477 path->kcontrol = w->kcontrols[i];
478 continue;
479 }
480
481 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
482 sizeof(struct snd_soc_dapm_widget *),
483 wlist = kzalloc(wlistsize, GFP_KERNEL);
484 if (wlist == NULL) {
485 dev_err(dapm->dev,
486 "asoc: can't allocate widget list for %s\n",
487 w->name);
488 return -ENOMEM;
489 }
490 wlist->num_widgets = 1;
491 wlist->widgets[0] = w;
492
493 /* add dapm control with long name.
494 * for dapm_mixer this is the concatenation of the
495 * mixer and kcontrol name.
496 * for dapm_mixer_named_ctl this is simply the
497 * kcontrol name.
498 */
499 name_len = strlen(w->kcontrol_news[i].name) + 1;
500 if (w->id != snd_soc_dapm_mixer_named_ctl)
501 name_len += 1 + strlen(w->name);
502
503 path->long_name = kmalloc(name_len, GFP_KERNEL);
504
505 if (path->long_name == NULL) {
506 kfree(wlist);
507 return -ENOMEM;
508 }
509
510 switch (w->id) {
511 default:
512 /* The control will get a prefix from
513 * the control creation process but
514 * we're also using the same prefix
515 * for widgets so cut the prefix off
516 * the front of the widget name.
517 */
518 snprintf(path->long_name, name_len, "%s %s",
519 w->name + prefix_len,
520 w->kcontrol_news[i].name);
521 break;
522 case snd_soc_dapm_mixer_named_ctl:
523 snprintf(path->long_name, name_len, "%s",
524 w->kcontrol_news[i].name);
525 break;
526 }
527
528 path->long_name[name_len - 1] = '\0';
529
530 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
531 wlist, path->long_name,
532 prefix);
533 ret = snd_ctl_add(card, path->kcontrol);
534 if (ret < 0) {
535 dev_err(dapm->dev,
536 "asoc: failed to add dapm kcontrol %s: %d\n",
537 path->long_name, ret);
538 kfree(wlist);
539 kfree(path->long_name);
540 path->long_name = NULL;
541 return ret;
542 }
543 w->kcontrols[i] = path->kcontrol;
544 }
545 }
546 return ret;
547 }
548
549 /* create new dapm mux control */
550 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
551 {
552 struct snd_soc_dapm_context *dapm = w->dapm;
553 struct snd_soc_dapm_path *path = NULL;
554 struct snd_kcontrol *kcontrol;
555 struct snd_card *card = dapm->card->snd_card;
556 const char *prefix;
557 size_t prefix_len;
558 int ret;
559 struct snd_soc_dapm_widget_list *wlist;
560 int shared, wlistentries;
561 size_t wlistsize;
562 char *name;
563
564 if (w->num_kcontrols != 1) {
565 dev_err(dapm->dev,
566 "asoc: mux %s has incorrect number of controls\n",
567 w->name);
568 return -EINVAL;
569 }
570
571 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
572 &kcontrol);
573 if (kcontrol) {
574 wlist = kcontrol->private_data;
575 wlistentries = wlist->num_widgets + 1;
576 } else {
577 wlist = NULL;
578 wlistentries = 1;
579 }
580 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
581 wlistentries * sizeof(struct snd_soc_dapm_widget *),
582 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
583 if (wlist == NULL) {
584 dev_err(dapm->dev,
585 "asoc: can't allocate widget list for %s\n", w->name);
586 return -ENOMEM;
587 }
588 wlist->num_widgets = wlistentries;
589 wlist->widgets[wlistentries - 1] = w;
590
591 if (!kcontrol) {
592 if (dapm->codec)
593 prefix = dapm->codec->name_prefix;
594 else
595 prefix = NULL;
596
597 if (shared) {
598 name = w->kcontrol_news[0].name;
599 prefix_len = 0;
600 } else {
601 name = w->name;
602 if (prefix)
603 prefix_len = strlen(prefix) + 1;
604 else
605 prefix_len = 0;
606 }
607
608 /*
609 * The control will get a prefix from the control creation
610 * process but we're also using the same prefix for widgets so
611 * cut the prefix off the front of the widget name.
612 */
613 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
614 name + prefix_len, prefix);
615 ret = snd_ctl_add(card, kcontrol);
616 if (ret < 0) {
617 dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
618 w->name, ret);
619 kfree(wlist);
620 return ret;
621 }
622 }
623
624 kcontrol->private_data = wlist;
625
626 w->kcontrols[0] = kcontrol;
627
628 list_for_each_entry(path, &w->sources, list_sink)
629 path->kcontrol = kcontrol;
630
631 return 0;
632 }
633
634 /* create new dapm volume control */
635 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
636 {
637 if (w->num_kcontrols)
638 dev_err(w->dapm->dev,
639 "asoc: PGA controls not supported: '%s'\n", w->name);
640
641 return 0;
642 }
643
644 /* reset 'walked' bit for each dapm path */
645 static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
646 {
647 struct snd_soc_dapm_path *p;
648
649 list_for_each_entry(p, &dapm->card->paths, list)
650 p->walked = 0;
651 }
652
653 /* We implement power down on suspend by checking the power state of
654 * the ALSA card - when we are suspending the ALSA state for the card
655 * is set to D3.
656 */
657 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
658 {
659 int level = snd_power_get_state(widget->dapm->card->snd_card);
660
661 switch (level) {
662 case SNDRV_CTL_POWER_D3hot:
663 case SNDRV_CTL_POWER_D3cold:
664 if (widget->ignore_suspend)
665 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
666 widget->name);
667 return widget->ignore_suspend;
668 default:
669 return 1;
670 }
671 }
672
673 /*
674 * Recursively check for a completed path to an active or physically connected
675 * output widget. Returns number of complete paths.
676 */
677 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
678 {
679 struct snd_soc_dapm_path *path;
680 int con = 0;
681
682 if (widget->outputs >= 0)
683 return widget->outputs;
684
685 DAPM_UPDATE_STAT(widget, path_checks);
686
687 switch (widget->id) {
688 case snd_soc_dapm_supply:
689 case snd_soc_dapm_regulator_supply:
690 return 0;
691 default:
692 break;
693 }
694
695 switch (widget->id) {
696 case snd_soc_dapm_adc:
697 case snd_soc_dapm_aif_out:
698 if (widget->active) {
699 widget->outputs = snd_soc_dapm_suspend_check(widget);
700 return widget->outputs;
701 }
702 default:
703 break;
704 }
705
706 if (widget->connected) {
707 /* connected pin ? */
708 if (widget->id == snd_soc_dapm_output && !widget->ext) {
709 widget->outputs = snd_soc_dapm_suspend_check(widget);
710 return widget->outputs;
711 }
712
713 /* connected jack or spk ? */
714 if (widget->id == snd_soc_dapm_hp ||
715 widget->id == snd_soc_dapm_spk ||
716 (widget->id == snd_soc_dapm_line &&
717 !list_empty(&widget->sources))) {
718 widget->outputs = snd_soc_dapm_suspend_check(widget);
719 return widget->outputs;
720 }
721 }
722
723 list_for_each_entry(path, &widget->sinks, list_source) {
724 DAPM_UPDATE_STAT(widget, neighbour_checks);
725
726 if (path->weak)
727 continue;
728
729 if (path->walked)
730 continue;
731
732 if (path->sink && path->connect) {
733 path->walked = 1;
734 con += is_connected_output_ep(path->sink);
735 }
736 }
737
738 widget->outputs = con;
739
740 return con;
741 }
742
743 /*
744 * Recursively check for a completed path to an active or physically connected
745 * input widget. Returns number of complete paths.
746 */
747 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
748 {
749 struct snd_soc_dapm_path *path;
750 int con = 0;
751
752 if (widget->inputs >= 0)
753 return widget->inputs;
754
755 DAPM_UPDATE_STAT(widget, path_checks);
756
757 switch (widget->id) {
758 case snd_soc_dapm_supply:
759 case snd_soc_dapm_regulator_supply:
760 return 0;
761 default:
762 break;
763 }
764
765 /* active stream ? */
766 switch (widget->id) {
767 case snd_soc_dapm_dac:
768 case snd_soc_dapm_aif_in:
769 if (widget->active) {
770 widget->inputs = snd_soc_dapm_suspend_check(widget);
771 return widget->inputs;
772 }
773 default:
774 break;
775 }
776
777 if (widget->connected) {
778 /* connected pin ? */
779 if (widget->id == snd_soc_dapm_input && !widget->ext) {
780 widget->inputs = snd_soc_dapm_suspend_check(widget);
781 return widget->inputs;
782 }
783
784 /* connected VMID/Bias for lower pops */
785 if (widget->id == snd_soc_dapm_vmid) {
786 widget->inputs = snd_soc_dapm_suspend_check(widget);
787 return widget->inputs;
788 }
789
790 /* connected jack ? */
791 if (widget->id == snd_soc_dapm_mic ||
792 (widget->id == snd_soc_dapm_line &&
793 !list_empty(&widget->sinks))) {
794 widget->inputs = snd_soc_dapm_suspend_check(widget);
795 return widget->inputs;
796 }
797
798 /* signal generator */
799 if (widget->id == snd_soc_dapm_siggen) {
800 widget->inputs = snd_soc_dapm_suspend_check(widget);
801 return widget->inputs;
802 }
803 }
804
805 list_for_each_entry(path, &widget->sources, list_sink) {
806 DAPM_UPDATE_STAT(widget, neighbour_checks);
807
808 if (path->weak)
809 continue;
810
811 if (path->walked)
812 continue;
813
814 if (path->source && path->connect) {
815 path->walked = 1;
816 con += is_connected_input_ep(path->source);
817 }
818 }
819
820 widget->inputs = con;
821
822 return con;
823 }
824
825 /*
826 * Handler for generic register modifier widget.
827 */
828 int dapm_reg_event(struct snd_soc_dapm_widget *w,
829 struct snd_kcontrol *kcontrol, int event)
830 {
831 unsigned int val;
832
833 if (SND_SOC_DAPM_EVENT_ON(event))
834 val = w->on_val;
835 else
836 val = w->off_val;
837
838 soc_widget_update_bits(w, -(w->reg + 1),
839 w->mask << w->shift, val << w->shift);
840
841 return 0;
842 }
843 EXPORT_SYMBOL_GPL(dapm_reg_event);
844
845 /*
846 * Handler for regulator supply widget.
847 */
848 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
849 struct snd_kcontrol *kcontrol, int event)
850 {
851 if (SND_SOC_DAPM_EVENT_ON(event))
852 return regulator_enable(w->priv);
853 else
854 return regulator_disable_deferred(w->priv, w->shift);
855 }
856 EXPORT_SYMBOL_GPL(dapm_regulator_event);
857
858 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
859 {
860 if (w->power_checked)
861 return w->new_power;
862
863 if (w->force)
864 w->new_power = 1;
865 else
866 w->new_power = w->power_check(w);
867
868 w->power_checked = true;
869
870 return w->new_power;
871 }
872
873 /* Generic check to see if a widget should be powered.
874 */
875 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
876 {
877 int in, out;
878
879 DAPM_UPDATE_STAT(w, power_checks);
880
881 in = is_connected_input_ep(w);
882 dapm_clear_walk(w->dapm);
883 out = is_connected_output_ep(w);
884 dapm_clear_walk(w->dapm);
885 return out != 0 && in != 0;
886 }
887
888 /* Check to see if an ADC has power */
889 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
890 {
891 int in;
892
893 DAPM_UPDATE_STAT(w, power_checks);
894
895 if (w->active) {
896 in = is_connected_input_ep(w);
897 dapm_clear_walk(w->dapm);
898 return in != 0;
899 } else {
900 return dapm_generic_check_power(w);
901 }
902 }
903
904 /* Check to see if a DAC has power */
905 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
906 {
907 int out;
908
909 DAPM_UPDATE_STAT(w, power_checks);
910
911 if (w->active) {
912 out = is_connected_output_ep(w);
913 dapm_clear_walk(w->dapm);
914 return out != 0;
915 } else {
916 return dapm_generic_check_power(w);
917 }
918 }
919
920 /* Check to see if a power supply is needed */
921 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
922 {
923 struct snd_soc_dapm_path *path;
924
925 DAPM_UPDATE_STAT(w, power_checks);
926
927 /* Check if one of our outputs is connected */
928 list_for_each_entry(path, &w->sinks, list_source) {
929 DAPM_UPDATE_STAT(w, neighbour_checks);
930
931 if (path->weak)
932 continue;
933
934 if (path->connected &&
935 !path->connected(path->source, path->sink))
936 continue;
937
938 if (!path->sink)
939 continue;
940
941 if (dapm_widget_power_check(path->sink))
942 return 1;
943 }
944
945 dapm_clear_walk(w->dapm);
946
947 return 0;
948 }
949
950 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
951 {
952 return 1;
953 }
954
955 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
956 struct snd_soc_dapm_widget *b,
957 bool power_up)
958 {
959 int *sort;
960
961 if (power_up)
962 sort = dapm_up_seq;
963 else
964 sort = dapm_down_seq;
965
966 if (sort[a->id] != sort[b->id])
967 return sort[a->id] - sort[b->id];
968 if (a->subseq != b->subseq) {
969 if (power_up)
970 return a->subseq - b->subseq;
971 else
972 return b->subseq - a->subseq;
973 }
974 if (a->reg != b->reg)
975 return a->reg - b->reg;
976 if (a->dapm != b->dapm)
977 return (unsigned long)a->dapm - (unsigned long)b->dapm;
978
979 return 0;
980 }
981
982 /* Insert a widget in order into a DAPM power sequence. */
983 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
984 struct list_head *list,
985 bool power_up)
986 {
987 struct snd_soc_dapm_widget *w;
988
989 list_for_each_entry(w, list, power_list)
990 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
991 list_add_tail(&new_widget->power_list, &w->power_list);
992 return;
993 }
994
995 list_add_tail(&new_widget->power_list, list);
996 }
997
998 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
999 struct snd_soc_dapm_widget *w, int event)
1000 {
1001 struct snd_soc_card *card = dapm->card;
1002 const char *ev_name;
1003 int power, ret;
1004
1005 switch (event) {
1006 case SND_SOC_DAPM_PRE_PMU:
1007 ev_name = "PRE_PMU";
1008 power = 1;
1009 break;
1010 case SND_SOC_DAPM_POST_PMU:
1011 ev_name = "POST_PMU";
1012 power = 1;
1013 break;
1014 case SND_SOC_DAPM_PRE_PMD:
1015 ev_name = "PRE_PMD";
1016 power = 0;
1017 break;
1018 case SND_SOC_DAPM_POST_PMD:
1019 ev_name = "POST_PMD";
1020 power = 0;
1021 break;
1022 default:
1023 BUG();
1024 return;
1025 }
1026
1027 if (w->power != power)
1028 return;
1029
1030 if (w->event && (w->event_flags & event)) {
1031 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1032 w->name, ev_name);
1033 trace_snd_soc_dapm_widget_event_start(w, event);
1034 ret = w->event(w, NULL, event);
1035 trace_snd_soc_dapm_widget_event_done(w, event);
1036 if (ret < 0)
1037 pr_err("%s: %s event failed: %d\n",
1038 ev_name, w->name, ret);
1039 }
1040 }
1041
1042 /* Apply the coalesced changes from a DAPM sequence */
1043 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
1044 struct list_head *pending)
1045 {
1046 struct snd_soc_card *card = dapm->card;
1047 struct snd_soc_dapm_widget *w;
1048 int reg, power;
1049 unsigned int value = 0;
1050 unsigned int mask = 0;
1051 unsigned int cur_mask;
1052
1053 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1054 power_list)->reg;
1055
1056 list_for_each_entry(w, pending, power_list) {
1057 cur_mask = 1 << w->shift;
1058 BUG_ON(reg != w->reg);
1059
1060 if (w->invert)
1061 power = !w->power;
1062 else
1063 power = w->power;
1064
1065 mask |= cur_mask;
1066 if (power)
1067 value |= cur_mask;
1068
1069 pop_dbg(dapm->dev, card->pop_time,
1070 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1071 w->name, reg, value, mask);
1072
1073 /* Check for events */
1074 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1075 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
1076 }
1077
1078 if (reg >= 0) {
1079 /* Any widget will do, they should all be updating the
1080 * same register.
1081 */
1082 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1083 power_list);
1084
1085 pop_dbg(dapm->dev, card->pop_time,
1086 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1087 value, mask, reg, card->pop_time);
1088 pop_wait(card->pop_time);
1089 soc_widget_update_bits(w, reg, mask, value);
1090 }
1091
1092 list_for_each_entry(w, pending, power_list) {
1093 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1094 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
1095 }
1096 }
1097
1098 /* Apply a DAPM power sequence.
1099 *
1100 * We walk over a pre-sorted list of widgets to apply power to. In
1101 * order to minimise the number of writes to the device required
1102 * multiple widgets will be updated in a single write where possible.
1103 * Currently anything that requires more than a single write is not
1104 * handled.
1105 */
1106 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
1107 struct list_head *list, int event, bool power_up)
1108 {
1109 struct snd_soc_dapm_widget *w, *n;
1110 LIST_HEAD(pending);
1111 int cur_sort = -1;
1112 int cur_subseq = -1;
1113 int cur_reg = SND_SOC_NOPM;
1114 struct snd_soc_dapm_context *cur_dapm = NULL;
1115 int ret, i;
1116 int *sort;
1117
1118 if (power_up)
1119 sort = dapm_up_seq;
1120 else
1121 sort = dapm_down_seq;
1122
1123 list_for_each_entry_safe(w, n, list, power_list) {
1124 ret = 0;
1125
1126 /* Do we need to apply any queued changes? */
1127 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1128 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1129 if (!list_empty(&pending))
1130 dapm_seq_run_coalesced(cur_dapm, &pending);
1131
1132 if (cur_dapm && cur_dapm->seq_notifier) {
1133 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1134 if (sort[i] == cur_sort)
1135 cur_dapm->seq_notifier(cur_dapm,
1136 i,
1137 cur_subseq);
1138 }
1139
1140 INIT_LIST_HEAD(&pending);
1141 cur_sort = -1;
1142 cur_subseq = INT_MIN;
1143 cur_reg = SND_SOC_NOPM;
1144 cur_dapm = NULL;
1145 }
1146
1147 switch (w->id) {
1148 case snd_soc_dapm_pre:
1149 if (!w->event)
1150 list_for_each_entry_safe_continue(w, n, list,
1151 power_list);
1152
1153 if (event == SND_SOC_DAPM_STREAM_START)
1154 ret = w->event(w,
1155 NULL, SND_SOC_DAPM_PRE_PMU);
1156 else if (event == SND_SOC_DAPM_STREAM_STOP)
1157 ret = w->event(w,
1158 NULL, SND_SOC_DAPM_PRE_PMD);
1159 break;
1160
1161 case snd_soc_dapm_post:
1162 if (!w->event)
1163 list_for_each_entry_safe_continue(w, n, list,
1164 power_list);
1165
1166 if (event == SND_SOC_DAPM_STREAM_START)
1167 ret = w->event(w,
1168 NULL, SND_SOC_DAPM_POST_PMU);
1169 else if (event == SND_SOC_DAPM_STREAM_STOP)
1170 ret = w->event(w,
1171 NULL, SND_SOC_DAPM_POST_PMD);
1172 break;
1173
1174 default:
1175 /* Queue it up for application */
1176 cur_sort = sort[w->id];
1177 cur_subseq = w->subseq;
1178 cur_reg = w->reg;
1179 cur_dapm = w->dapm;
1180 list_move(&w->power_list, &pending);
1181 break;
1182 }
1183
1184 if (ret < 0)
1185 dev_err(w->dapm->dev,
1186 "Failed to apply widget power: %d\n", ret);
1187 }
1188
1189 if (!list_empty(&pending))
1190 dapm_seq_run_coalesced(cur_dapm, &pending);
1191
1192 if (cur_dapm && cur_dapm->seq_notifier) {
1193 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1194 if (sort[i] == cur_sort)
1195 cur_dapm->seq_notifier(cur_dapm,
1196 i, cur_subseq);
1197 }
1198 }
1199
1200 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1201 {
1202 struct snd_soc_dapm_update *update = dapm->update;
1203 struct snd_soc_dapm_widget *w;
1204 int ret;
1205
1206 if (!update)
1207 return;
1208
1209 w = update->widget;
1210
1211 if (w->event &&
1212 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1213 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1214 if (ret != 0)
1215 pr_err("%s DAPM pre-event failed: %d\n",
1216 w->name, ret);
1217 }
1218
1219 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1220 update->val);
1221 if (ret < 0)
1222 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1223
1224 if (w->event &&
1225 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1226 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1227 if (ret != 0)
1228 pr_err("%s DAPM post-event failed: %d\n",
1229 w->name, ret);
1230 }
1231 }
1232
1233 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1234 * they're changing state.
1235 */
1236 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1237 {
1238 struct snd_soc_dapm_context *d = data;
1239 int ret;
1240
1241 /* If we're off and we're not supposed to be go into STANDBY */
1242 if (d->bias_level == SND_SOC_BIAS_OFF &&
1243 d->target_bias_level != SND_SOC_BIAS_OFF) {
1244 if (d->dev)
1245 pm_runtime_get_sync(d->dev);
1246
1247 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1248 if (ret != 0)
1249 dev_err(d->dev,
1250 "Failed to turn on bias: %d\n", ret);
1251 }
1252
1253 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1254 if (d->bias_level != d->target_bias_level) {
1255 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1256 if (ret != 0)
1257 dev_err(d->dev,
1258 "Failed to prepare bias: %d\n", ret);
1259 }
1260 }
1261
1262 /* Async callback run prior to DAPM sequences - brings to their final
1263 * state.
1264 */
1265 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1266 {
1267 struct snd_soc_dapm_context *d = data;
1268 int ret;
1269
1270 /* If we just powered the last thing off drop to standby bias */
1271 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1272 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1273 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1274 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1275 if (ret != 0)
1276 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1277 ret);
1278 }
1279
1280 /* If we're in standby and can support bias off then do that */
1281 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1282 d->target_bias_level == SND_SOC_BIAS_OFF) {
1283 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1284 if (ret != 0)
1285 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1286
1287 if (d->dev)
1288 pm_runtime_put(d->dev);
1289 }
1290
1291 /* If we just powered up then move to active bias */
1292 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1293 d->target_bias_level == SND_SOC_BIAS_ON) {
1294 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1295 if (ret != 0)
1296 dev_err(d->dev, "Failed to apply active bias: %d\n",
1297 ret);
1298 }
1299 }
1300
1301 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1302 bool power, bool connect)
1303 {
1304 /* If a connection is being made or broken then that update
1305 * will have marked the peer dirty, otherwise the widgets are
1306 * not connected and this update has no impact. */
1307 if (!connect)
1308 return;
1309
1310 /* If the peer is already in the state we're moving to then we
1311 * won't have an impact on it. */
1312 if (power != peer->power)
1313 dapm_mark_dirty(peer, "peer state change");
1314 }
1315
1316 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1317 struct list_head *up_list,
1318 struct list_head *down_list)
1319 {
1320 struct snd_soc_dapm_path *path;
1321
1322 if (w->power == power)
1323 return;
1324
1325 trace_snd_soc_dapm_widget_power(w, power);
1326
1327 /* If we changed our power state perhaps our neigbours changed
1328 * also.
1329 */
1330 list_for_each_entry(path, &w->sources, list_sink) {
1331 if (path->source) {
1332 dapm_widget_set_peer_power(path->source, power,
1333 path->connect);
1334 }
1335 }
1336 switch (w->id) {
1337 case snd_soc_dapm_supply:
1338 case snd_soc_dapm_regulator_supply:
1339 /* Supplies can't affect their outputs, only their inputs */
1340 break;
1341 default:
1342 list_for_each_entry(path, &w->sinks, list_source) {
1343 if (path->sink) {
1344 dapm_widget_set_peer_power(path->sink, power,
1345 path->connect);
1346 }
1347 }
1348 break;
1349 }
1350
1351 if (power)
1352 dapm_seq_insert(w, up_list, true);
1353 else
1354 dapm_seq_insert(w, down_list, false);
1355
1356 w->power = power;
1357 }
1358
1359 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1360 struct list_head *up_list,
1361 struct list_head *down_list)
1362 {
1363 int power;
1364
1365 switch (w->id) {
1366 case snd_soc_dapm_pre:
1367 dapm_seq_insert(w, down_list, false);
1368 break;
1369 case snd_soc_dapm_post:
1370 dapm_seq_insert(w, up_list, true);
1371 break;
1372
1373 default:
1374 power = dapm_widget_power_check(w);
1375
1376 dapm_widget_set_power(w, power, up_list, down_list);
1377 break;
1378 }
1379 }
1380
1381 /*
1382 * Scan each dapm widget for complete audio path.
1383 * A complete path is a route that has valid endpoints i.e.:-
1384 *
1385 * o DAC to output pin.
1386 * o Input Pin to ADC.
1387 * o Input pin to Output pin (bypass, sidetone)
1388 * o DAC to ADC (loopback).
1389 */
1390 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1391 {
1392 struct snd_soc_card *card = dapm->card;
1393 struct snd_soc_dapm_widget *w;
1394 struct snd_soc_dapm_context *d;
1395 LIST_HEAD(up_list);
1396 LIST_HEAD(down_list);
1397 LIST_HEAD(async_domain);
1398 enum snd_soc_bias_level bias;
1399
1400 trace_snd_soc_dapm_start(card);
1401
1402 list_for_each_entry(d, &card->dapm_list, list) {
1403 if (d->n_widgets || d->codec == NULL) {
1404 if (d->idle_bias_off)
1405 d->target_bias_level = SND_SOC_BIAS_OFF;
1406 else
1407 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1408 }
1409 }
1410
1411 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
1412
1413 list_for_each_entry(w, &card->widgets, list) {
1414 w->power_checked = false;
1415 w->inputs = -1;
1416 w->outputs = -1;
1417 }
1418
1419 /* Check which widgets we need to power and store them in
1420 * lists indicating if they should be powered up or down. We
1421 * only check widgets that have been flagged as dirty but note
1422 * that new widgets may be added to the dirty list while we
1423 * iterate.
1424 */
1425 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1426 dapm_power_one_widget(w, &up_list, &down_list);
1427 }
1428
1429 list_for_each_entry(w, &card->widgets, list) {
1430 list_del_init(&w->dirty);
1431
1432 if (w->power) {
1433 d = w->dapm;
1434
1435 /* Supplies and micbiases only bring the
1436 * context up to STANDBY as unless something
1437 * else is active and passing audio they
1438 * generally don't require full power.
1439 */
1440 switch (w->id) {
1441 case snd_soc_dapm_supply:
1442 case snd_soc_dapm_regulator_supply:
1443 case snd_soc_dapm_micbias:
1444 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1445 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1446 break;
1447 default:
1448 d->target_bias_level = SND_SOC_BIAS_ON;
1449 break;
1450 }
1451 }
1452
1453 }
1454
1455 /* If there are no DAPM widgets then try to figure out power from the
1456 * event type.
1457 */
1458 if (!dapm->n_widgets) {
1459 switch (event) {
1460 case SND_SOC_DAPM_STREAM_START:
1461 case SND_SOC_DAPM_STREAM_RESUME:
1462 dapm->target_bias_level = SND_SOC_BIAS_ON;
1463 break;
1464 case SND_SOC_DAPM_STREAM_STOP:
1465 if (dapm->codec && dapm->codec->active)
1466 dapm->target_bias_level = SND_SOC_BIAS_ON;
1467 else
1468 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1469 break;
1470 case SND_SOC_DAPM_STREAM_SUSPEND:
1471 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1472 break;
1473 case SND_SOC_DAPM_STREAM_NOP:
1474 dapm->target_bias_level = dapm->bias_level;
1475 break;
1476 default:
1477 break;
1478 }
1479 }
1480
1481 /* Force all contexts in the card to the same bias state if
1482 * they're not ground referenced.
1483 */
1484 bias = SND_SOC_BIAS_OFF;
1485 list_for_each_entry(d, &card->dapm_list, list)
1486 if (d->target_bias_level > bias)
1487 bias = d->target_bias_level;
1488 list_for_each_entry(d, &card->dapm_list, list)
1489 if (!d->idle_bias_off)
1490 d->target_bias_level = bias;
1491
1492 trace_snd_soc_dapm_walk_done(card);
1493
1494 /* Run all the bias changes in parallel */
1495 list_for_each_entry(d, &dapm->card->dapm_list, list)
1496 async_schedule_domain(dapm_pre_sequence_async, d,
1497 &async_domain);
1498 async_synchronize_full_domain(&async_domain);
1499
1500 /* Power down widgets first; try to avoid amplifying pops. */
1501 dapm_seq_run(dapm, &down_list, event, false);
1502
1503 dapm_widget_update(dapm);
1504
1505 /* Now power up. */
1506 dapm_seq_run(dapm, &up_list, event, true);
1507
1508 /* Run all the bias changes in parallel */
1509 list_for_each_entry(d, &dapm->card->dapm_list, list)
1510 async_schedule_domain(dapm_post_sequence_async, d,
1511 &async_domain);
1512 async_synchronize_full_domain(&async_domain);
1513
1514 pop_dbg(dapm->dev, card->pop_time,
1515 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1516 pop_wait(card->pop_time);
1517
1518 trace_snd_soc_dapm_done(card);
1519
1520 return 0;
1521 }
1522
1523 #ifdef CONFIG_DEBUG_FS
1524 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1525 {
1526 file->private_data = inode->i_private;
1527 return 0;
1528 }
1529
1530 static ssize_t dapm_widget_power_read_file(struct file *file,
1531 char __user *user_buf,
1532 size_t count, loff_t *ppos)
1533 {
1534 struct snd_soc_dapm_widget *w = file->private_data;
1535 char *buf;
1536 int in, out;
1537 ssize_t ret;
1538 struct snd_soc_dapm_path *p = NULL;
1539
1540 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1541 if (!buf)
1542 return -ENOMEM;
1543
1544 in = is_connected_input_ep(w);
1545 dapm_clear_walk(w->dapm);
1546 out = is_connected_output_ep(w);
1547 dapm_clear_walk(w->dapm);
1548
1549 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
1550 w->name, w->power ? "On" : "Off", in, out);
1551
1552 if (w->reg >= 0)
1553 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1554 " - R%d(0x%x) bit %d",
1555 w->reg, w->reg, w->shift);
1556
1557 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1558
1559 if (w->sname)
1560 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1561 w->sname,
1562 w->active ? "active" : "inactive");
1563
1564 list_for_each_entry(p, &w->sources, list_sink) {
1565 if (p->connected && !p->connected(w, p->sink))
1566 continue;
1567
1568 if (p->connect)
1569 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1570 " in \"%s\" \"%s\"\n",
1571 p->name ? p->name : "static",
1572 p->source->name);
1573 }
1574 list_for_each_entry(p, &w->sinks, list_source) {
1575 if (p->connected && !p->connected(w, p->sink))
1576 continue;
1577
1578 if (p->connect)
1579 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1580 " out \"%s\" \"%s\"\n",
1581 p->name ? p->name : "static",
1582 p->sink->name);
1583 }
1584
1585 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1586
1587 kfree(buf);
1588 return ret;
1589 }
1590
1591 static const struct file_operations dapm_widget_power_fops = {
1592 .open = dapm_widget_power_open_file,
1593 .read = dapm_widget_power_read_file,
1594 .llseek = default_llseek,
1595 };
1596
1597 static int dapm_bias_open_file(struct inode *inode, struct file *file)
1598 {
1599 file->private_data = inode->i_private;
1600 return 0;
1601 }
1602
1603 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1604 size_t count, loff_t *ppos)
1605 {
1606 struct snd_soc_dapm_context *dapm = file->private_data;
1607 char *level;
1608
1609 switch (dapm->bias_level) {
1610 case SND_SOC_BIAS_ON:
1611 level = "On\n";
1612 break;
1613 case SND_SOC_BIAS_PREPARE:
1614 level = "Prepare\n";
1615 break;
1616 case SND_SOC_BIAS_STANDBY:
1617 level = "Standby\n";
1618 break;
1619 case SND_SOC_BIAS_OFF:
1620 level = "Off\n";
1621 break;
1622 default:
1623 BUG();
1624 level = "Unknown\n";
1625 break;
1626 }
1627
1628 return simple_read_from_buffer(user_buf, count, ppos, level,
1629 strlen(level));
1630 }
1631
1632 static const struct file_operations dapm_bias_fops = {
1633 .open = dapm_bias_open_file,
1634 .read = dapm_bias_read_file,
1635 .llseek = default_llseek,
1636 };
1637
1638 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1639 struct dentry *parent)
1640 {
1641 struct dentry *d;
1642
1643 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1644
1645 if (!dapm->debugfs_dapm) {
1646 printk(KERN_WARNING
1647 "Failed to create DAPM debugfs directory\n");
1648 return;
1649 }
1650
1651 d = debugfs_create_file("bias_level", 0444,
1652 dapm->debugfs_dapm, dapm,
1653 &dapm_bias_fops);
1654 if (!d)
1655 dev_warn(dapm->dev,
1656 "ASoC: Failed to create bias level debugfs file\n");
1657 }
1658
1659 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1660 {
1661 struct snd_soc_dapm_context *dapm = w->dapm;
1662 struct dentry *d;
1663
1664 if (!dapm->debugfs_dapm || !w->name)
1665 return;
1666
1667 d = debugfs_create_file(w->name, 0444,
1668 dapm->debugfs_dapm, w,
1669 &dapm_widget_power_fops);
1670 if (!d)
1671 dev_warn(w->dapm->dev,
1672 "ASoC: Failed to create %s debugfs file\n",
1673 w->name);
1674 }
1675
1676 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1677 {
1678 debugfs_remove_recursive(dapm->debugfs_dapm);
1679 }
1680
1681 #else
1682 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1683 struct dentry *parent)
1684 {
1685 }
1686
1687 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1688 {
1689 }
1690
1691 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1692 {
1693 }
1694
1695 #endif
1696
1697 /* test and update the power status of a mux widget */
1698 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1699 struct snd_kcontrol *kcontrol, int change,
1700 int mux, struct soc_enum *e)
1701 {
1702 struct snd_soc_dapm_path *path;
1703 int found = 0;
1704
1705 if (widget->id != snd_soc_dapm_mux &&
1706 widget->id != snd_soc_dapm_virt_mux &&
1707 widget->id != snd_soc_dapm_value_mux)
1708 return -ENODEV;
1709
1710 if (!change)
1711 return 0;
1712
1713 /* find dapm widget path assoc with kcontrol */
1714 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1715 if (path->kcontrol != kcontrol)
1716 continue;
1717
1718 if (!path->name || !e->texts[mux])
1719 continue;
1720
1721 found = 1;
1722 /* we now need to match the string in the enum to the path */
1723 if (!(strcmp(path->name, e->texts[mux]))) {
1724 path->connect = 1; /* new connection */
1725 dapm_mark_dirty(path->source, "mux connection");
1726 } else {
1727 if (path->connect)
1728 dapm_mark_dirty(path->source,
1729 "mux disconnection");
1730 path->connect = 0; /* old connection must be powered down */
1731 }
1732 }
1733
1734 if (found) {
1735 dapm_mark_dirty(widget, "mux change");
1736 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1737 }
1738
1739 return 0;
1740 }
1741
1742 /* test and update the power status of a mixer or switch widget */
1743 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1744 struct snd_kcontrol *kcontrol, int connect)
1745 {
1746 struct snd_soc_dapm_path *path;
1747 int found = 0;
1748
1749 if (widget->id != snd_soc_dapm_mixer &&
1750 widget->id != snd_soc_dapm_mixer_named_ctl &&
1751 widget->id != snd_soc_dapm_switch)
1752 return -ENODEV;
1753
1754 /* find dapm widget path assoc with kcontrol */
1755 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1756 if (path->kcontrol != kcontrol)
1757 continue;
1758
1759 /* found, now check type */
1760 found = 1;
1761 path->connect = connect;
1762 dapm_mark_dirty(path->source, "mixer connection");
1763 }
1764
1765 if (found) {
1766 dapm_mark_dirty(widget, "mixer update");
1767 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1768 }
1769
1770 return 0;
1771 }
1772
1773 /* show dapm widget status in sys fs */
1774 static ssize_t dapm_widget_show(struct device *dev,
1775 struct device_attribute *attr, char *buf)
1776 {
1777 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
1778 struct snd_soc_codec *codec =rtd->codec;
1779 struct snd_soc_dapm_widget *w;
1780 int count = 0;
1781 char *state = "not set";
1782
1783 list_for_each_entry(w, &codec->card->widgets, list) {
1784 if (w->dapm != &codec->dapm)
1785 continue;
1786
1787 /* only display widgets that burnm power */
1788 switch (w->id) {
1789 case snd_soc_dapm_hp:
1790 case snd_soc_dapm_mic:
1791 case snd_soc_dapm_spk:
1792 case snd_soc_dapm_line:
1793 case snd_soc_dapm_micbias:
1794 case snd_soc_dapm_dac:
1795 case snd_soc_dapm_adc:
1796 case snd_soc_dapm_pga:
1797 case snd_soc_dapm_out_drv:
1798 case snd_soc_dapm_mixer:
1799 case snd_soc_dapm_mixer_named_ctl:
1800 case snd_soc_dapm_supply:
1801 case snd_soc_dapm_regulator_supply:
1802 if (w->name)
1803 count += sprintf(buf + count, "%s: %s\n",
1804 w->name, w->power ? "On":"Off");
1805 break;
1806 default:
1807 break;
1808 }
1809 }
1810
1811 switch (codec->dapm.bias_level) {
1812 case SND_SOC_BIAS_ON:
1813 state = "On";
1814 break;
1815 case SND_SOC_BIAS_PREPARE:
1816 state = "Prepare";
1817 break;
1818 case SND_SOC_BIAS_STANDBY:
1819 state = "Standby";
1820 break;
1821 case SND_SOC_BIAS_OFF:
1822 state = "Off";
1823 break;
1824 }
1825 count += sprintf(buf + count, "PM State: %s\n", state);
1826
1827 return count;
1828 }
1829
1830 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1831
1832 int snd_soc_dapm_sys_add(struct device *dev)
1833 {
1834 return device_create_file(dev, &dev_attr_dapm_widget);
1835 }
1836
1837 static void snd_soc_dapm_sys_remove(struct device *dev)
1838 {
1839 device_remove_file(dev, &dev_attr_dapm_widget);
1840 }
1841
1842 /* free all dapm widgets and resources */
1843 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1844 {
1845 struct snd_soc_dapm_widget *w, *next_w;
1846 struct snd_soc_dapm_path *p, *next_p;
1847
1848 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1849 if (w->dapm != dapm)
1850 continue;
1851 list_del(&w->list);
1852 /*
1853 * remove source and sink paths associated to this widget.
1854 * While removing the path, remove reference to it from both
1855 * source and sink widgets so that path is removed only once.
1856 */
1857 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1858 list_del(&p->list_sink);
1859 list_del(&p->list_source);
1860 list_del(&p->list);
1861 kfree(p->long_name);
1862 kfree(p);
1863 }
1864 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1865 list_del(&p->list_sink);
1866 list_del(&p->list_source);
1867 list_del(&p->list);
1868 kfree(p->long_name);
1869 kfree(p);
1870 }
1871 kfree(w->kcontrols);
1872 kfree(w->name);
1873 kfree(w);
1874 }
1875 }
1876
1877 static struct snd_soc_dapm_widget *dapm_find_widget(
1878 struct snd_soc_dapm_context *dapm, const char *pin,
1879 bool search_other_contexts)
1880 {
1881 struct snd_soc_dapm_widget *w;
1882 struct snd_soc_dapm_widget *fallback = NULL;
1883
1884 list_for_each_entry(w, &dapm->card->widgets, list) {
1885 if (!strcmp(w->name, pin)) {
1886 if (w->dapm == dapm)
1887 return w;
1888 else
1889 fallback = w;
1890 }
1891 }
1892
1893 if (search_other_contexts)
1894 return fallback;
1895
1896 return NULL;
1897 }
1898
1899 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
1900 const char *pin, int status)
1901 {
1902 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
1903
1904 if (!w) {
1905 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1906 return -EINVAL;
1907 }
1908
1909 w->connected = status;
1910 if (status == 0)
1911 w->force = 0;
1912 dapm_mark_dirty(w, "pin configuration");
1913
1914 return 0;
1915 }
1916
1917 /**
1918 * snd_soc_dapm_sync - scan and power dapm paths
1919 * @dapm: DAPM context
1920 *
1921 * Walks all dapm audio paths and powers widgets according to their
1922 * stream or path usage.
1923 *
1924 * Returns 0 for success.
1925 */
1926 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
1927 {
1928 /*
1929 * Suppress early reports (eg, jacks syncing their state) to avoid
1930 * silly DAPM runs during card startup.
1931 */
1932 if (!dapm->card || !dapm->card->instantiated)
1933 return 0;
1934
1935 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1936 }
1937 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1938
1939 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
1940 const struct snd_soc_dapm_route *route)
1941 {
1942 struct snd_soc_dapm_path *path;
1943 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1944 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
1945 const char *sink;
1946 const char *control = route->control;
1947 const char *source;
1948 char prefixed_sink[80];
1949 char prefixed_source[80];
1950 int ret = 0;
1951
1952 if (dapm->codec && dapm->codec->name_prefix) {
1953 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1954 dapm->codec->name_prefix, route->sink);
1955 sink = prefixed_sink;
1956 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1957 dapm->codec->name_prefix, route->source);
1958 source = prefixed_source;
1959 } else {
1960 sink = route->sink;
1961 source = route->source;
1962 }
1963
1964 /*
1965 * find src and dest widgets over all widgets but favor a widget from
1966 * current DAPM context
1967 */
1968 list_for_each_entry(w, &dapm->card->widgets, list) {
1969 if (!wsink && !(strcmp(w->name, sink))) {
1970 wtsink = w;
1971 if (w->dapm == dapm)
1972 wsink = w;
1973 continue;
1974 }
1975 if (!wsource && !(strcmp(w->name, source))) {
1976 wtsource = w;
1977 if (w->dapm == dapm)
1978 wsource = w;
1979 }
1980 }
1981 /* use widget from another DAPM context if not found from this */
1982 if (!wsink)
1983 wsink = wtsink;
1984 if (!wsource)
1985 wsource = wtsource;
1986
1987 if (wsource == NULL || wsink == NULL)
1988 return -ENODEV;
1989
1990 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1991 if (!path)
1992 return -ENOMEM;
1993
1994 path->source = wsource;
1995 path->sink = wsink;
1996 path->connected = route->connected;
1997 INIT_LIST_HEAD(&path->list);
1998 INIT_LIST_HEAD(&path->list_source);
1999 INIT_LIST_HEAD(&path->list_sink);
2000
2001 /* check for external widgets */
2002 if (wsink->id == snd_soc_dapm_input) {
2003 if (wsource->id == snd_soc_dapm_micbias ||
2004 wsource->id == snd_soc_dapm_mic ||
2005 wsource->id == snd_soc_dapm_line ||
2006 wsource->id == snd_soc_dapm_output)
2007 wsink->ext = 1;
2008 }
2009 if (wsource->id == snd_soc_dapm_output) {
2010 if (wsink->id == snd_soc_dapm_spk ||
2011 wsink->id == snd_soc_dapm_hp ||
2012 wsink->id == snd_soc_dapm_line ||
2013 wsink->id == snd_soc_dapm_input)
2014 wsource->ext = 1;
2015 }
2016
2017 /* connect static paths */
2018 if (control == NULL) {
2019 list_add(&path->list, &dapm->card->paths);
2020 list_add(&path->list_sink, &wsink->sources);
2021 list_add(&path->list_source, &wsource->sinks);
2022 path->connect = 1;
2023 return 0;
2024 }
2025
2026 /* connect dynamic paths */
2027 switch (wsink->id) {
2028 case snd_soc_dapm_adc:
2029 case snd_soc_dapm_dac:
2030 case snd_soc_dapm_pga:
2031 case snd_soc_dapm_out_drv:
2032 case snd_soc_dapm_input:
2033 case snd_soc_dapm_output:
2034 case snd_soc_dapm_siggen:
2035 case snd_soc_dapm_micbias:
2036 case snd_soc_dapm_vmid:
2037 case snd_soc_dapm_pre:
2038 case snd_soc_dapm_post:
2039 case snd_soc_dapm_supply:
2040 case snd_soc_dapm_regulator_supply:
2041 case snd_soc_dapm_aif_in:
2042 case snd_soc_dapm_aif_out:
2043 list_add(&path->list, &dapm->card->paths);
2044 list_add(&path->list_sink, &wsink->sources);
2045 list_add(&path->list_source, &wsource->sinks);
2046 path->connect = 1;
2047 return 0;
2048 case snd_soc_dapm_mux:
2049 case snd_soc_dapm_virt_mux:
2050 case snd_soc_dapm_value_mux:
2051 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2052 &wsink->kcontrol_news[0]);
2053 if (ret != 0)
2054 goto err;
2055 break;
2056 case snd_soc_dapm_switch:
2057 case snd_soc_dapm_mixer:
2058 case snd_soc_dapm_mixer_named_ctl:
2059 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2060 if (ret != 0)
2061 goto err;
2062 break;
2063 case snd_soc_dapm_hp:
2064 case snd_soc_dapm_mic:
2065 case snd_soc_dapm_line:
2066 case snd_soc_dapm_spk:
2067 list_add(&path->list, &dapm->card->paths);
2068 list_add(&path->list_sink, &wsink->sources);
2069 list_add(&path->list_source, &wsource->sinks);
2070 path->connect = 0;
2071 return 0;
2072 }
2073 return 0;
2074
2075 err:
2076 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2077 source, control, sink);
2078 kfree(path);
2079 return ret;
2080 }
2081
2082 /**
2083 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2084 * @dapm: DAPM context
2085 * @route: audio routes
2086 * @num: number of routes
2087 *
2088 * Connects 2 dapm widgets together via a named audio path. The sink is
2089 * the widget receiving the audio signal, whilst the source is the sender
2090 * of the audio signal.
2091 *
2092 * Returns 0 for success else error. On error all resources can be freed
2093 * with a call to snd_soc_card_free().
2094 */
2095 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2096 const struct snd_soc_dapm_route *route, int num)
2097 {
2098 int i, ret;
2099
2100 for (i = 0; i < num; i++) {
2101 ret = snd_soc_dapm_add_route(dapm, route);
2102 if (ret < 0) {
2103 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2104 route->source, route->sink);
2105 return ret;
2106 }
2107 route++;
2108 }
2109
2110 return 0;
2111 }
2112 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2113
2114 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2115 const struct snd_soc_dapm_route *route)
2116 {
2117 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2118 route->source,
2119 true);
2120 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2121 route->sink,
2122 true);
2123 struct snd_soc_dapm_path *path;
2124 int count = 0;
2125
2126 if (!source) {
2127 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2128 route->source);
2129 return -ENODEV;
2130 }
2131
2132 if (!sink) {
2133 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2134 route->sink);
2135 return -ENODEV;
2136 }
2137
2138 if (route->control || route->connected)
2139 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2140 route->source, route->sink);
2141
2142 list_for_each_entry(path, &source->sinks, list_source) {
2143 if (path->sink == sink) {
2144 path->weak = 1;
2145 count++;
2146 }
2147 }
2148
2149 if (count == 0)
2150 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2151 route->source, route->sink);
2152 if (count > 1)
2153 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2154 count, route->source, route->sink);
2155
2156 return 0;
2157 }
2158
2159 /**
2160 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2161 * @dapm: DAPM context
2162 * @route: audio routes
2163 * @num: number of routes
2164 *
2165 * Mark existing routes matching those specified in the passed array
2166 * as being weak, meaning that they are ignored for the purpose of
2167 * power decisions. The main intended use case is for sidetone paths
2168 * which couple audio between other independent paths if they are both
2169 * active in order to make the combination work better at the user
2170 * level but which aren't intended to be "used".
2171 *
2172 * Note that CODEC drivers should not use this as sidetone type paths
2173 * can frequently also be used as bypass paths.
2174 */
2175 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2176 const struct snd_soc_dapm_route *route, int num)
2177 {
2178 int i, err;
2179 int ret = 0;
2180
2181 for (i = 0; i < num; i++) {
2182 err = snd_soc_dapm_weak_route(dapm, route);
2183 if (err)
2184 ret = err;
2185 route++;
2186 }
2187
2188 return ret;
2189 }
2190 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2191
2192 /**
2193 * snd_soc_dapm_new_widgets - add new dapm widgets
2194 * @dapm: DAPM context
2195 *
2196 * Checks the codec for any new dapm widgets and creates them if found.
2197 *
2198 * Returns 0 for success.
2199 */
2200 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
2201 {
2202 struct snd_soc_dapm_widget *w;
2203 unsigned int val;
2204
2205 list_for_each_entry(w, &dapm->card->widgets, list)
2206 {
2207 if (w->new)
2208 continue;
2209
2210 if (w->num_kcontrols) {
2211 w->kcontrols = kzalloc(w->num_kcontrols *
2212 sizeof(struct snd_kcontrol *),
2213 GFP_KERNEL);
2214 if (!w->kcontrols)
2215 return -ENOMEM;
2216 }
2217
2218 switch(w->id) {
2219 case snd_soc_dapm_switch:
2220 case snd_soc_dapm_mixer:
2221 case snd_soc_dapm_mixer_named_ctl:
2222 dapm_new_mixer(w);
2223 break;
2224 case snd_soc_dapm_mux:
2225 case snd_soc_dapm_virt_mux:
2226 case snd_soc_dapm_value_mux:
2227 dapm_new_mux(w);
2228 break;
2229 case snd_soc_dapm_pga:
2230 case snd_soc_dapm_out_drv:
2231 dapm_new_pga(w);
2232 break;
2233 default:
2234 break;
2235 }
2236
2237 /* Read the initial power state from the device */
2238 if (w->reg >= 0) {
2239 val = soc_widget_read(w, w->reg);
2240 val &= 1 << w->shift;
2241 if (w->invert)
2242 val = !val;
2243
2244 if (val)
2245 w->power = 1;
2246 }
2247
2248 w->new = 1;
2249
2250 dapm_mark_dirty(w, "new widget");
2251 dapm_debugfs_add_widget(w);
2252 }
2253
2254 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2255 return 0;
2256 }
2257 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2258
2259 /**
2260 * snd_soc_dapm_get_volsw - dapm mixer get callback
2261 * @kcontrol: mixer control
2262 * @ucontrol: control element information
2263 *
2264 * Callback to get the value of a dapm mixer control.
2265 *
2266 * Returns 0 for success.
2267 */
2268 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2269 struct snd_ctl_elem_value *ucontrol)
2270 {
2271 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2272 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2273 struct soc_mixer_control *mc =
2274 (struct soc_mixer_control *)kcontrol->private_value;
2275 unsigned int reg = mc->reg;
2276 unsigned int shift = mc->shift;
2277 unsigned int rshift = mc->rshift;
2278 int max = mc->max;
2279 unsigned int invert = mc->invert;
2280 unsigned int mask = (1 << fls(max)) - 1;
2281
2282 ucontrol->value.integer.value[0] =
2283 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2284 if (shift != rshift)
2285 ucontrol->value.integer.value[1] =
2286 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2287 if (invert) {
2288 ucontrol->value.integer.value[0] =
2289 max - ucontrol->value.integer.value[0];
2290 if (shift != rshift)
2291 ucontrol->value.integer.value[1] =
2292 max - ucontrol->value.integer.value[1];
2293 }
2294
2295 return 0;
2296 }
2297 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2298
2299 /**
2300 * snd_soc_dapm_put_volsw - dapm mixer set callback
2301 * @kcontrol: mixer control
2302 * @ucontrol: control element information
2303 *
2304 * Callback to set the value of a dapm mixer control.
2305 *
2306 * Returns 0 for success.
2307 */
2308 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2309 struct snd_ctl_elem_value *ucontrol)
2310 {
2311 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2312 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2313 struct snd_soc_codec *codec = widget->codec;
2314 struct soc_mixer_control *mc =
2315 (struct soc_mixer_control *)kcontrol->private_value;
2316 unsigned int reg = mc->reg;
2317 unsigned int shift = mc->shift;
2318 int max = mc->max;
2319 unsigned int mask = (1 << fls(max)) - 1;
2320 unsigned int invert = mc->invert;
2321 unsigned int val;
2322 int connect, change;
2323 struct snd_soc_dapm_update update;
2324 int wi;
2325
2326 val = (ucontrol->value.integer.value[0] & mask);
2327
2328 if (invert)
2329 val = max - val;
2330 mask = mask << shift;
2331 val = val << shift;
2332
2333 if (val)
2334 /* new connection */
2335 connect = invert ? 0 : 1;
2336 else
2337 /* old connection must be powered down */
2338 connect = invert ? 1 : 0;
2339
2340 mutex_lock(&codec->mutex);
2341
2342 change = snd_soc_test_bits(widget->codec, reg, mask, val);
2343 if (change) {
2344 for (wi = 0; wi < wlist->num_widgets; wi++) {
2345 widget = wlist->widgets[wi];
2346
2347 widget->value = val;
2348
2349 update.kcontrol = kcontrol;
2350 update.widget = widget;
2351 update.reg = reg;
2352 update.mask = mask;
2353 update.val = val;
2354 widget->dapm->update = &update;
2355
2356 dapm_mixer_update_power(widget, kcontrol, connect);
2357
2358 widget->dapm->update = NULL;
2359 }
2360 }
2361
2362 mutex_unlock(&codec->mutex);
2363 return 0;
2364 }
2365 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2366
2367 /**
2368 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2369 * @kcontrol: mixer control
2370 * @ucontrol: control element information
2371 *
2372 * Callback to get the value of a dapm enumerated double mixer control.
2373 *
2374 * Returns 0 for success.
2375 */
2376 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2377 struct snd_ctl_elem_value *ucontrol)
2378 {
2379 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2380 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2381 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2382 unsigned int val, bitmask;
2383
2384 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2385 ;
2386 val = snd_soc_read(widget->codec, e->reg);
2387 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2388 if (e->shift_l != e->shift_r)
2389 ucontrol->value.enumerated.item[1] =
2390 (val >> e->shift_r) & (bitmask - 1);
2391
2392 return 0;
2393 }
2394 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2395
2396 /**
2397 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2398 * @kcontrol: mixer control
2399 * @ucontrol: control element information
2400 *
2401 * Callback to set the value of a dapm enumerated double mixer control.
2402 *
2403 * Returns 0 for success.
2404 */
2405 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2406 struct snd_ctl_elem_value *ucontrol)
2407 {
2408 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2409 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2410 struct snd_soc_codec *codec = widget->codec;
2411 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2412 unsigned int val, mux, change;
2413 unsigned int mask, bitmask;
2414 struct snd_soc_dapm_update update;
2415 int wi;
2416
2417 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2418 ;
2419 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2420 return -EINVAL;
2421 mux = ucontrol->value.enumerated.item[0];
2422 val = mux << e->shift_l;
2423 mask = (bitmask - 1) << e->shift_l;
2424 if (e->shift_l != e->shift_r) {
2425 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2426 return -EINVAL;
2427 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2428 mask |= (bitmask - 1) << e->shift_r;
2429 }
2430
2431 mutex_lock(&codec->mutex);
2432
2433 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2434 if (change) {
2435 for (wi = 0; wi < wlist->num_widgets; wi++) {
2436 widget = wlist->widgets[wi];
2437
2438 widget->value = val;
2439
2440 update.kcontrol = kcontrol;
2441 update.widget = widget;
2442 update.reg = e->reg;
2443 update.mask = mask;
2444 update.val = val;
2445 widget->dapm->update = &update;
2446
2447 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2448
2449 widget->dapm->update = NULL;
2450 }
2451 }
2452
2453 mutex_unlock(&codec->mutex);
2454 return change;
2455 }
2456 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2457
2458 /**
2459 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2460 * @kcontrol: mixer control
2461 * @ucontrol: control element information
2462 *
2463 * Returns 0 for success.
2464 */
2465 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2466 struct snd_ctl_elem_value *ucontrol)
2467 {
2468 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2469 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2470
2471 ucontrol->value.enumerated.item[0] = widget->value;
2472
2473 return 0;
2474 }
2475 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2476
2477 /**
2478 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2479 * @kcontrol: mixer control
2480 * @ucontrol: control element information
2481 *
2482 * Returns 0 for success.
2483 */
2484 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2485 struct snd_ctl_elem_value *ucontrol)
2486 {
2487 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2488 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2489 struct snd_soc_codec *codec = widget->codec;
2490 struct soc_enum *e =
2491 (struct soc_enum *)kcontrol->private_value;
2492 int change;
2493 int ret = 0;
2494 int wi;
2495
2496 if (ucontrol->value.enumerated.item[0] >= e->max)
2497 return -EINVAL;
2498
2499 mutex_lock(&codec->mutex);
2500
2501 change = widget->value != ucontrol->value.enumerated.item[0];
2502 if (change) {
2503 for (wi = 0; wi < wlist->num_widgets; wi++) {
2504 widget = wlist->widgets[wi];
2505
2506 widget->value = ucontrol->value.enumerated.item[0];
2507
2508 dapm_mux_update_power(widget, kcontrol, change,
2509 widget->value, e);
2510 }
2511 }
2512
2513 mutex_unlock(&codec->mutex);
2514 return ret;
2515 }
2516 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2517
2518 /**
2519 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2520 * callback
2521 * @kcontrol: mixer control
2522 * @ucontrol: control element information
2523 *
2524 * Callback to get the value of a dapm semi enumerated double mixer control.
2525 *
2526 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2527 * used for handling bitfield coded enumeration for example.
2528 *
2529 * Returns 0 for success.
2530 */
2531 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2532 struct snd_ctl_elem_value *ucontrol)
2533 {
2534 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2535 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2536 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2537 unsigned int reg_val, val, mux;
2538
2539 reg_val = snd_soc_read(widget->codec, e->reg);
2540 val = (reg_val >> e->shift_l) & e->mask;
2541 for (mux = 0; mux < e->max; mux++) {
2542 if (val == e->values[mux])
2543 break;
2544 }
2545 ucontrol->value.enumerated.item[0] = mux;
2546 if (e->shift_l != e->shift_r) {
2547 val = (reg_val >> e->shift_r) & e->mask;
2548 for (mux = 0; mux < e->max; mux++) {
2549 if (val == e->values[mux])
2550 break;
2551 }
2552 ucontrol->value.enumerated.item[1] = mux;
2553 }
2554
2555 return 0;
2556 }
2557 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2558
2559 /**
2560 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2561 * callback
2562 * @kcontrol: mixer control
2563 * @ucontrol: control element information
2564 *
2565 * Callback to set the value of a dapm semi enumerated double mixer control.
2566 *
2567 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2568 * used for handling bitfield coded enumeration for example.
2569 *
2570 * Returns 0 for success.
2571 */
2572 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2573 struct snd_ctl_elem_value *ucontrol)
2574 {
2575 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2576 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2577 struct snd_soc_codec *codec = widget->codec;
2578 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2579 unsigned int val, mux, change;
2580 unsigned int mask;
2581 struct snd_soc_dapm_update update;
2582 int wi;
2583
2584 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2585 return -EINVAL;
2586 mux = ucontrol->value.enumerated.item[0];
2587 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2588 mask = e->mask << e->shift_l;
2589 if (e->shift_l != e->shift_r) {
2590 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2591 return -EINVAL;
2592 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2593 mask |= e->mask << e->shift_r;
2594 }
2595
2596 mutex_lock(&codec->mutex);
2597
2598 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2599 if (change) {
2600 for (wi = 0; wi < wlist->num_widgets; wi++) {
2601 widget = wlist->widgets[wi];
2602
2603 widget->value = val;
2604
2605 update.kcontrol = kcontrol;
2606 update.widget = widget;
2607 update.reg = e->reg;
2608 update.mask = mask;
2609 update.val = val;
2610 widget->dapm->update = &update;
2611
2612 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2613
2614 widget->dapm->update = NULL;
2615 }
2616 }
2617
2618 mutex_unlock(&codec->mutex);
2619 return change;
2620 }
2621 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2622
2623 /**
2624 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2625 *
2626 * @kcontrol: mixer control
2627 * @uinfo: control element information
2628 *
2629 * Callback to provide information about a pin switch control.
2630 */
2631 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2632 struct snd_ctl_elem_info *uinfo)
2633 {
2634 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2635 uinfo->count = 1;
2636 uinfo->value.integer.min = 0;
2637 uinfo->value.integer.max = 1;
2638
2639 return 0;
2640 }
2641 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2642
2643 /**
2644 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2645 *
2646 * @kcontrol: mixer control
2647 * @ucontrol: Value
2648 */
2649 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2650 struct snd_ctl_elem_value *ucontrol)
2651 {
2652 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2653 const char *pin = (const char *)kcontrol->private_value;
2654
2655 mutex_lock(&codec->mutex);
2656
2657 ucontrol->value.integer.value[0] =
2658 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
2659
2660 mutex_unlock(&codec->mutex);
2661
2662 return 0;
2663 }
2664 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2665
2666 /**
2667 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2668 *
2669 * @kcontrol: mixer control
2670 * @ucontrol: Value
2671 */
2672 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2673 struct snd_ctl_elem_value *ucontrol)
2674 {
2675 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2676 const char *pin = (const char *)kcontrol->private_value;
2677
2678 mutex_lock(&codec->mutex);
2679
2680 if (ucontrol->value.integer.value[0])
2681 snd_soc_dapm_enable_pin(&codec->dapm, pin);
2682 else
2683 snd_soc_dapm_disable_pin(&codec->dapm, pin);
2684
2685 snd_soc_dapm_sync(&codec->dapm);
2686
2687 mutex_unlock(&codec->mutex);
2688
2689 return 0;
2690 }
2691 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2692
2693 /**
2694 * snd_soc_dapm_new_control - create new dapm control
2695 * @dapm: DAPM context
2696 * @widget: widget template
2697 *
2698 * Creates a new dapm control based upon the template.
2699 *
2700 * Returns 0 for success else error.
2701 */
2702 int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2703 const struct snd_soc_dapm_widget *widget)
2704 {
2705 struct snd_soc_dapm_widget *w;
2706 size_t name_len;
2707 int ret;
2708
2709 if ((w = dapm_cnew_widget(widget)) == NULL)
2710 return -ENOMEM;
2711
2712 switch (w->id) {
2713 case snd_soc_dapm_regulator_supply:
2714 w->priv = devm_regulator_get(dapm->dev, w->name);
2715 if (IS_ERR(w->priv)) {
2716 ret = PTR_ERR(w->priv);
2717 dev_err(dapm->dev, "Failed to request %s: %d\n",
2718 w->name, ret);
2719 return ret;
2720 }
2721 break;
2722 default:
2723 break;
2724 }
2725
2726 name_len = strlen(widget->name) + 1;
2727 if (dapm->codec && dapm->codec->name_prefix)
2728 name_len += 1 + strlen(dapm->codec->name_prefix);
2729 w->name = kmalloc(name_len, GFP_KERNEL);
2730 if (w->name == NULL) {
2731 kfree(w);
2732 return -ENOMEM;
2733 }
2734 if (dapm->codec && dapm->codec->name_prefix)
2735 snprintf(w->name, name_len, "%s %s",
2736 dapm->codec->name_prefix, widget->name);
2737 else
2738 snprintf(w->name, name_len, "%s", widget->name);
2739
2740 switch (w->id) {
2741 case snd_soc_dapm_switch:
2742 case snd_soc_dapm_mixer:
2743 case snd_soc_dapm_mixer_named_ctl:
2744 w->power_check = dapm_generic_check_power;
2745 break;
2746 case snd_soc_dapm_mux:
2747 case snd_soc_dapm_virt_mux:
2748 case snd_soc_dapm_value_mux:
2749 w->power_check = dapm_generic_check_power;
2750 break;
2751 case snd_soc_dapm_adc:
2752 case snd_soc_dapm_aif_out:
2753 w->power_check = dapm_adc_check_power;
2754 break;
2755 case snd_soc_dapm_dac:
2756 case snd_soc_dapm_aif_in:
2757 w->power_check = dapm_dac_check_power;
2758 break;
2759 case snd_soc_dapm_pga:
2760 case snd_soc_dapm_out_drv:
2761 case snd_soc_dapm_input:
2762 case snd_soc_dapm_output:
2763 case snd_soc_dapm_micbias:
2764 case snd_soc_dapm_spk:
2765 case snd_soc_dapm_hp:
2766 case snd_soc_dapm_mic:
2767 case snd_soc_dapm_line:
2768 w->power_check = dapm_generic_check_power;
2769 break;
2770 case snd_soc_dapm_supply:
2771 case snd_soc_dapm_regulator_supply:
2772 w->power_check = dapm_supply_check_power;
2773 break;
2774 default:
2775 w->power_check = dapm_always_on_check_power;
2776 break;
2777 }
2778
2779 dapm->n_widgets++;
2780 w->dapm = dapm;
2781 w->codec = dapm->codec;
2782 w->platform = dapm->platform;
2783 INIT_LIST_HEAD(&w->sources);
2784 INIT_LIST_HEAD(&w->sinks);
2785 INIT_LIST_HEAD(&w->list);
2786 INIT_LIST_HEAD(&w->dirty);
2787 list_add(&w->list, &dapm->card->widgets);
2788
2789 /* machine layer set ups unconnected pins and insertions */
2790 w->connected = 1;
2791 return 0;
2792 }
2793 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2794
2795 /**
2796 * snd_soc_dapm_new_controls - create new dapm controls
2797 * @dapm: DAPM context
2798 * @widget: widget array
2799 * @num: number of widgets
2800 *
2801 * Creates new DAPM controls based upon the templates.
2802 *
2803 * Returns 0 for success else error.
2804 */
2805 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2806 const struct snd_soc_dapm_widget *widget,
2807 int num)
2808 {
2809 int i, ret;
2810
2811 for (i = 0; i < num; i++) {
2812 ret = snd_soc_dapm_new_control(dapm, widget);
2813 if (ret < 0) {
2814 dev_err(dapm->dev,
2815 "ASoC: Failed to create DAPM control %s: %d\n",
2816 widget->name, ret);
2817 return ret;
2818 }
2819 widget++;
2820 }
2821 return 0;
2822 }
2823 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2824
2825 static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
2826 const char *stream, int event)
2827 {
2828 struct snd_soc_dapm_widget *w;
2829
2830 list_for_each_entry(w, &dapm->card->widgets, list)
2831 {
2832 if (!w->sname || w->dapm != dapm)
2833 continue;
2834 dev_vdbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2835 w->name, w->sname, stream, event);
2836 if (strstr(w->sname, stream)) {
2837 dapm_mark_dirty(w, "stream event");
2838 switch(event) {
2839 case SND_SOC_DAPM_STREAM_START:
2840 w->active = 1;
2841 break;
2842 case SND_SOC_DAPM_STREAM_STOP:
2843 w->active = 0;
2844 break;
2845 case SND_SOC_DAPM_STREAM_SUSPEND:
2846 case SND_SOC_DAPM_STREAM_RESUME:
2847 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2848 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2849 break;
2850 }
2851 }
2852 }
2853
2854 dapm_power_widgets(dapm, event);
2855
2856 /* do we need to notify any clients that DAPM stream is complete */
2857 if (dapm->stream_event)
2858 dapm->stream_event(dapm, event);
2859 }
2860
2861 /**
2862 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2863 * @rtd: PCM runtime data
2864 * @stream: stream name
2865 * @event: stream event
2866 *
2867 * Sends a stream event to the dapm core. The core then makes any
2868 * necessary widget power changes.
2869 *
2870 * Returns 0 for success else error.
2871 */
2872 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2873 const char *stream, int event)
2874 {
2875 struct snd_soc_codec *codec = rtd->codec;
2876
2877 if (stream == NULL)
2878 return 0;
2879
2880 mutex_lock(&codec->mutex);
2881 soc_dapm_stream_event(&codec->dapm, stream, event);
2882 mutex_unlock(&codec->mutex);
2883 return 0;
2884 }
2885
2886 /**
2887 * snd_soc_dapm_enable_pin - enable pin.
2888 * @dapm: DAPM context
2889 * @pin: pin name
2890 *
2891 * Enables input/output pin and its parents or children widgets iff there is
2892 * a valid audio route and active audio stream.
2893 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2894 * do any widget power switching.
2895 */
2896 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2897 {
2898 return snd_soc_dapm_set_pin(dapm, pin, 1);
2899 }
2900 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2901
2902 /**
2903 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2904 * @dapm: DAPM context
2905 * @pin: pin name
2906 *
2907 * Enables input/output pin regardless of any other state. This is
2908 * intended for use with microphone bias supplies used in microphone
2909 * jack detection.
2910 *
2911 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2912 * do any widget power switching.
2913 */
2914 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2915 const char *pin)
2916 {
2917 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2918
2919 if (!w) {
2920 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2921 return -EINVAL;
2922 }
2923
2924 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2925 w->connected = 1;
2926 w->force = 1;
2927 dapm_mark_dirty(w, "force enable");
2928
2929 return 0;
2930 }
2931 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2932
2933 /**
2934 * snd_soc_dapm_disable_pin - disable pin.
2935 * @dapm: DAPM context
2936 * @pin: pin name
2937 *
2938 * Disables input/output pin and its parents or children widgets.
2939 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2940 * do any widget power switching.
2941 */
2942 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2943 const char *pin)
2944 {
2945 return snd_soc_dapm_set_pin(dapm, pin, 0);
2946 }
2947 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2948
2949 /**
2950 * snd_soc_dapm_nc_pin - permanently disable pin.
2951 * @dapm: DAPM context
2952 * @pin: pin name
2953 *
2954 * Marks the specified pin as being not connected, disabling it along
2955 * any parent or child widgets. At present this is identical to
2956 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2957 * additional things such as disabling controls which only affect
2958 * paths through the pin.
2959 *
2960 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2961 * do any widget power switching.
2962 */
2963 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2964 {
2965 return snd_soc_dapm_set_pin(dapm, pin, 0);
2966 }
2967 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2968
2969 /**
2970 * snd_soc_dapm_get_pin_status - get audio pin status
2971 * @dapm: DAPM context
2972 * @pin: audio signal pin endpoint (or start point)
2973 *
2974 * Get audio pin status - connected or disconnected.
2975 *
2976 * Returns 1 for connected otherwise 0.
2977 */
2978 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2979 const char *pin)
2980 {
2981 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2982
2983 if (w)
2984 return w->connected;
2985
2986 return 0;
2987 }
2988 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
2989
2990 /**
2991 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2992 * @dapm: DAPM context
2993 * @pin: audio signal pin endpoint (or start point)
2994 *
2995 * Mark the given endpoint or pin as ignoring suspend. When the
2996 * system is disabled a path between two endpoints flagged as ignoring
2997 * suspend will not be disabled. The path must already be enabled via
2998 * normal means at suspend time, it will not be turned on if it was not
2999 * already enabled.
3000 */
3001 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3002 const char *pin)
3003 {
3004 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
3005
3006 if (!w) {
3007 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3008 return -EINVAL;
3009 }
3010
3011 w->ignore_suspend = 1;
3012
3013 return 0;
3014 }
3015 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3016
3017 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3018 struct snd_soc_dapm_widget *w)
3019 {
3020 struct snd_soc_dapm_path *p;
3021
3022 list_for_each_entry(p, &card->paths, list) {
3023 if ((p->source == w) || (p->sink == w)) {
3024 dev_dbg(card->dev,
3025 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3026 p->source->name, p->source->id, p->source->dapm,
3027 p->sink->name, p->sink->id, p->sink->dapm);
3028
3029 /* Connected to something other than the codec */
3030 if (p->source->dapm != p->sink->dapm)
3031 return true;
3032 /*
3033 * Loopback connection from codec external pin to
3034 * codec external pin
3035 */
3036 if (p->sink->id == snd_soc_dapm_input) {
3037 switch (p->source->id) {
3038 case snd_soc_dapm_output:
3039 case snd_soc_dapm_micbias:
3040 return true;
3041 default:
3042 break;
3043 }
3044 }
3045 }
3046 }
3047
3048 return false;
3049 }
3050
3051 /**
3052 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3053 * @codec: The codec whose pins should be processed
3054 *
3055 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3056 * which are unused. Pins are used if they are connected externally to the
3057 * codec, whether that be to some other device, or a loop-back connection to
3058 * the codec itself.
3059 */
3060 void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3061 {
3062 struct snd_soc_card *card = codec->card;
3063 struct snd_soc_dapm_context *dapm = &codec->dapm;
3064 struct snd_soc_dapm_widget *w;
3065
3066 dev_dbg(codec->dev, "Auto NC: DAPMs: card:%p codec:%p\n",
3067 &card->dapm, &codec->dapm);
3068
3069 list_for_each_entry(w, &card->widgets, list) {
3070 if (w->dapm != dapm)
3071 continue;
3072 switch (w->id) {
3073 case snd_soc_dapm_input:
3074 case snd_soc_dapm_output:
3075 case snd_soc_dapm_micbias:
3076 dev_dbg(codec->dev, "Auto NC: Checking widget %s\n",
3077 w->name);
3078 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
3079 dev_dbg(codec->dev,
3080 "... Not in map; disabling\n");
3081 snd_soc_dapm_nc_pin(dapm, w->name);
3082 }
3083 break;
3084 default:
3085 break;
3086 }
3087 }
3088 }
3089
3090 /**
3091 * snd_soc_dapm_free - free dapm resources
3092 * @dapm: DAPM context
3093 *
3094 * Free all dapm widgets and resources.
3095 */
3096 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
3097 {
3098 snd_soc_dapm_sys_remove(dapm->dev);
3099 dapm_debugfs_cleanup(dapm);
3100 dapm_free_widgets(dapm);
3101 list_del(&dapm->list);
3102 }
3103 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3104
3105 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
3106 {
3107 struct snd_soc_dapm_widget *w;
3108 LIST_HEAD(down_list);
3109 int powerdown = 0;
3110
3111 list_for_each_entry(w, &dapm->card->widgets, list) {
3112 if (w->dapm != dapm)
3113 continue;
3114 if (w->power) {
3115 dapm_seq_insert(w, &down_list, false);
3116 w->power = 0;
3117 powerdown = 1;
3118 }
3119 }
3120
3121 /* If there were no widgets to power down we're already in
3122 * standby.
3123 */
3124 if (powerdown) {
3125 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
3126 dapm_seq_run(dapm, &down_list, 0, false);
3127 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
3128 }
3129 }
3130
3131 /*
3132 * snd_soc_dapm_shutdown - callback for system shutdown
3133 */
3134 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3135 {
3136 struct snd_soc_codec *codec;
3137
3138 list_for_each_entry(codec, &card->codec_dev_list, list) {
3139 soc_dapm_shutdown_codec(&codec->dapm);
3140 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
3141 }
3142 }
3143
3144 /* Module information */
3145 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3146 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3147 MODULE_LICENSE("GPL");
This page took 0.091722 seconds and 6 git commands to generate.