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