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