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