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