1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/module.h>
21#include <linux/init.h>
22#include <linux/async.h>
23#include <linux/delay.h>
24#include <linux/pm.h>
25#include <linux/bitops.h>
26#include <linux/platform_device.h>
27#include <linux/jiffies.h>
28#include <linux/debugfs.h>
29#include <linux/pm_runtime.h>
30#include <linux/regulator/consumer.h>
31#include <linux/pinctrl/consumer.h>
32#include <linux/clk.h>
33#include <linux/slab.h>
34#include <sound/core.h>
35#include <sound/pcm.h>
36#include <sound/pcm_params.h>
37#include <sound/soc.h>
38#include <sound/initval.h>
39
40#include <trace/events/asoc.h>
41
42#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
43
44#define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
45 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
46
47#define snd_soc_dapm_for_each_direction(dir) \
48 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
49 (dir)++)
50
51static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
52 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
53 const char *control,
54 int (*connected)(struct snd_soc_dapm_widget *source,
55 struct snd_soc_dapm_widget *sink));
56
57struct snd_soc_dapm_widget *
58snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
59 const struct snd_soc_dapm_widget *widget);
60
61struct snd_soc_dapm_widget *
62snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
63 const struct snd_soc_dapm_widget *widget);
64
65
66static int dapm_up_seq[] = {
67 [snd_soc_dapm_pre] = 1,
68 [snd_soc_dapm_regulator_supply] = 2,
69 [snd_soc_dapm_pinctrl] = 2,
70 [snd_soc_dapm_clock_supply] = 2,
71 [snd_soc_dapm_supply] = 3,
72 [snd_soc_dapm_micbias] = 4,
73 [snd_soc_dapm_vmid] = 4,
74 [snd_soc_dapm_dai_link] = 3,
75 [snd_soc_dapm_dai_in] = 5,
76 [snd_soc_dapm_dai_out] = 5,
77 [snd_soc_dapm_aif_in] = 5,
78 [snd_soc_dapm_aif_out] = 5,
79 [snd_soc_dapm_mic] = 6,
80 [snd_soc_dapm_siggen] = 6,
81 [snd_soc_dapm_input] = 6,
82 [snd_soc_dapm_output] = 6,
83 [snd_soc_dapm_mux] = 7,
84 [snd_soc_dapm_demux] = 7,
85 [snd_soc_dapm_dac] = 8,
86 [snd_soc_dapm_switch] = 9,
87 [snd_soc_dapm_mixer] = 9,
88 [snd_soc_dapm_mixer_named_ctl] = 9,
89 [snd_soc_dapm_pga] = 10,
90 [snd_soc_dapm_buffer] = 10,
91 [snd_soc_dapm_scheduler] = 10,
92 [snd_soc_dapm_effect] = 10,
93 [snd_soc_dapm_src] = 10,
94 [snd_soc_dapm_asrc] = 10,
95 [snd_soc_dapm_encoder] = 10,
96 [snd_soc_dapm_decoder] = 10,
97 [snd_soc_dapm_adc] = 11,
98 [snd_soc_dapm_out_drv] = 12,
99 [snd_soc_dapm_hp] = 12,
100 [snd_soc_dapm_spk] = 12,
101 [snd_soc_dapm_line] = 12,
102 [snd_soc_dapm_sink] = 12,
103 [snd_soc_dapm_kcontrol] = 13,
104 [snd_soc_dapm_post] = 14,
105};
106
107static int dapm_down_seq[] = {
108 [snd_soc_dapm_pre] = 1,
109 [snd_soc_dapm_kcontrol] = 2,
110 [snd_soc_dapm_adc] = 3,
111 [snd_soc_dapm_hp] = 4,
112 [snd_soc_dapm_spk] = 4,
113 [snd_soc_dapm_line] = 4,
114 [snd_soc_dapm_out_drv] = 4,
115 [snd_soc_dapm_sink] = 4,
116 [snd_soc_dapm_pga] = 5,
117 [snd_soc_dapm_buffer] = 5,
118 [snd_soc_dapm_scheduler] = 5,
119 [snd_soc_dapm_effect] = 5,
120 [snd_soc_dapm_src] = 5,
121 [snd_soc_dapm_asrc] = 5,
122 [snd_soc_dapm_encoder] = 5,
123 [snd_soc_dapm_decoder] = 5,
124 [snd_soc_dapm_switch] = 6,
125 [snd_soc_dapm_mixer_named_ctl] = 6,
126 [snd_soc_dapm_mixer] = 6,
127 [snd_soc_dapm_dac] = 7,
128 [snd_soc_dapm_mic] = 8,
129 [snd_soc_dapm_siggen] = 8,
130 [snd_soc_dapm_input] = 8,
131 [snd_soc_dapm_output] = 8,
132 [snd_soc_dapm_micbias] = 9,
133 [snd_soc_dapm_vmid] = 9,
134 [snd_soc_dapm_mux] = 10,
135 [snd_soc_dapm_demux] = 10,
136 [snd_soc_dapm_aif_in] = 11,
137 [snd_soc_dapm_aif_out] = 11,
138 [snd_soc_dapm_dai_in] = 11,
139 [snd_soc_dapm_dai_out] = 11,
140 [snd_soc_dapm_dai_link] = 12,
141 [snd_soc_dapm_supply] = 13,
142 [snd_soc_dapm_clock_supply] = 14,
143 [snd_soc_dapm_pinctrl] = 14,
144 [snd_soc_dapm_regulator_supply] = 14,
145 [snd_soc_dapm_post] = 15,
146};
147
148static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
149{
150 if (dapm->card && dapm->card->instantiated)
151 lockdep_assert_held(&dapm->card->dapm_mutex);
152}
153
154static void pop_wait(u32 pop_time)
155{
156 if (pop_time)
157 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
158}
159
160__printf(3, 4)
161static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
162{
163 va_list args;
164 char *buf;
165
166 if (!pop_time)
167 return;
168
169 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
170 if (buf == NULL)
171 return;
172
173 va_start(args, fmt);
174 vsnprintf(buf, PAGE_SIZE, fmt, args);
175 dev_info(dev, "%s", buf);
176 va_end(args);
177
178 kfree(buf);
179}
180
181static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
182{
183 return !list_empty(&w->dirty);
184}
185
186static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
187{
188 dapm_assert_locked(w->dapm);
189
190 if (!dapm_dirty_widget(w)) {
191 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
192 w->name, reason);
193 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
194 }
195}
196
197
198
199
200
201
202
203
204static __always_inline void dapm_widget_invalidate_paths(
205 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
206{
207 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
208 struct snd_soc_dapm_widget *node;
209 struct snd_soc_dapm_path *p;
210 LIST_HEAD(list);
211
212 dapm_assert_locked(w->dapm);
213
214 if (w->endpoints[dir] == -1)
215 return;
216
217 list_add_tail(&w->work_list, &list);
218 w->endpoints[dir] = -1;
219
220 list_for_each_entry(w, &list, work_list) {
221 snd_soc_dapm_widget_for_each_path(w, dir, p) {
222 if (p->is_supply || p->weak || !p->connect)
223 continue;
224 node = p->node[rdir];
225 if (node->endpoints[dir] != -1) {
226 node->endpoints[dir] = -1;
227 list_add_tail(&node->work_list, &list);
228 }
229 }
230 }
231}
232
233
234
235
236
237
238
239
240
241
242
243
244
245static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
246{
247 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
248}
249
250
251
252
253
254
255
256
257
258
259
260
261
262static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
263{
264 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
265}
266
267
268
269
270
271
272
273
274
275
276
277
278static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
279{
280
281
282
283
284 if (p->weak || p->is_supply)
285 return;
286
287
288
289
290
291
292
293 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
294 dapm_widget_invalidate_input_paths(p->sink);
295 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
296 dapm_widget_invalidate_output_paths(p->source);
297}
298
299void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
300{
301 struct snd_soc_dapm_widget *w;
302
303 mutex_lock(&card->dapm_mutex);
304
305 list_for_each_entry(w, &card->widgets, list) {
306 if (w->is_ep) {
307 dapm_mark_dirty(w, "Rechecking endpoints");
308 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
309 dapm_widget_invalidate_output_paths(w);
310 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
311 dapm_widget_invalidate_input_paths(w);
312 }
313 }
314
315 mutex_unlock(&card->dapm_mutex);
316}
317EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
318
319
320static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
321 const struct snd_soc_dapm_widget *_widget)
322{
323 struct snd_soc_dapm_widget *w;
324
325 w = kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
326 if (!w)
327 return NULL;
328
329
330
331
332
333 if (_widget->sname) {
334 w->sname = kstrdup_const(_widget->sname, GFP_KERNEL);
335 if (!w->sname) {
336 kfree(w);
337 return NULL;
338 }
339 }
340 return w;
341}
342
343struct dapm_kcontrol_data {
344 unsigned int value;
345 struct snd_soc_dapm_widget *widget;
346 struct list_head paths;
347 struct snd_soc_dapm_widget_list *wlist;
348};
349
350static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
351 struct snd_kcontrol *kcontrol, const char *ctrl_name)
352{
353 struct dapm_kcontrol_data *data;
354 struct soc_mixer_control *mc;
355 struct soc_enum *e;
356 const char *name;
357 int ret;
358
359 data = kzalloc(sizeof(*data), GFP_KERNEL);
360 if (!data)
361 return -ENOMEM;
362
363 INIT_LIST_HEAD(&data->paths);
364
365 switch (widget->id) {
366 case snd_soc_dapm_switch:
367 case snd_soc_dapm_mixer:
368 case snd_soc_dapm_mixer_named_ctl:
369 mc = (struct soc_mixer_control *)kcontrol->private_value;
370
371 if (mc->autodisable && snd_soc_volsw_is_stereo(mc))
372 dev_warn(widget->dapm->dev,
373 "ASoC: Unsupported stereo autodisable control '%s'\n",
374 ctrl_name);
375
376 if (mc->autodisable) {
377 struct snd_soc_dapm_widget template;
378
379 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
380 "Autodisable");
381 if (!name) {
382 ret = -ENOMEM;
383 goto err_data;
384 }
385
386 memset(&template, 0, sizeof(template));
387 template.reg = mc->reg;
388 template.mask = (1 << fls(mc->max)) - 1;
389 template.shift = mc->shift;
390 if (mc->invert)
391 template.off_val = mc->max;
392 else
393 template.off_val = 0;
394 template.on_val = template.off_val;
395 template.id = snd_soc_dapm_kcontrol;
396 template.name = name;
397
398 data->value = template.on_val;
399
400 data->widget =
401 snd_soc_dapm_new_control_unlocked(widget->dapm,
402 &template);
403 kfree(name);
404 if (IS_ERR(data->widget)) {
405 ret = PTR_ERR(data->widget);
406 goto err_data;
407 }
408 }
409 break;
410 case snd_soc_dapm_demux:
411 case snd_soc_dapm_mux:
412 e = (struct soc_enum *)kcontrol->private_value;
413
414 if (e->autodisable) {
415 struct snd_soc_dapm_widget template;
416
417 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
418 "Autodisable");
419 if (!name) {
420 ret = -ENOMEM;
421 goto err_data;
422 }
423
424 memset(&template, 0, sizeof(template));
425 template.reg = e->reg;
426 template.mask = e->mask << e->shift_l;
427 template.shift = e->shift_l;
428 template.off_val = snd_soc_enum_item_to_val(e, 0);
429 template.on_val = template.off_val;
430 template.id = snd_soc_dapm_kcontrol;
431 template.name = name;
432
433 data->value = template.on_val;
434
435 data->widget = snd_soc_dapm_new_control_unlocked(
436 widget->dapm, &template);
437 kfree(name);
438 if (IS_ERR(data->widget)) {
439 ret = PTR_ERR(data->widget);
440 goto err_data;
441 }
442
443 snd_soc_dapm_add_path(widget->dapm, data->widget,
444 widget, NULL, NULL);
445 }
446 break;
447 default:
448 break;
449 }
450
451 kcontrol->private_data = data;
452
453 return 0;
454
455err_data:
456 kfree(data);
457 return ret;
458}
459
460static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
461{
462 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
463
464 list_del(&data->paths);
465 kfree(data->wlist);
466 kfree(data);
467}
468
469static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
470 const struct snd_kcontrol *kcontrol)
471{
472 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
473
474 return data->wlist;
475}
476
477static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
478 struct snd_soc_dapm_widget *widget)
479{
480 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
481 struct snd_soc_dapm_widget_list *new_wlist;
482 unsigned int n;
483
484 if (data->wlist)
485 n = data->wlist->num_widgets + 1;
486 else
487 n = 1;
488
489 new_wlist = krealloc(data->wlist,
490 struct_size(new_wlist, widgets, n),
491 GFP_KERNEL);
492 if (!new_wlist)
493 return -ENOMEM;
494
495 new_wlist->widgets[n - 1] = widget;
496 new_wlist->num_widgets = n;
497
498 data->wlist = new_wlist;
499
500 return 0;
501}
502
503static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
504 struct snd_soc_dapm_path *path)
505{
506 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
507
508 list_add_tail(&path->list_kcontrol, &data->paths);
509}
510
511static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
512{
513 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
514
515 if (!data->widget)
516 return true;
517
518 return data->widget->power;
519}
520
521static struct list_head *dapm_kcontrol_get_path_list(
522 const struct snd_kcontrol *kcontrol)
523{
524 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
525
526 return &data->paths;
527}
528
529#define dapm_kcontrol_for_each_path(path, kcontrol) \
530 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
531 list_kcontrol)
532
533unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
534{
535 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
536
537 return data->value;
538}
539EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
540
541static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
542 unsigned int value)
543{
544 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
545
546 if (data->value == value)
547 return false;
548
549 if (data->widget)
550 data->widget->on_val = value;
551
552 data->value = value;
553
554 return true;
555}
556
557
558
559
560
561
562struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
563 struct snd_kcontrol *kcontrol)
564{
565 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
566}
567EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
568
569
570
571
572
573
574
575
576
577struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
578 struct snd_kcontrol *kcontrol)
579{
580 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
581}
582EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
583
584static void dapm_reset(struct snd_soc_card *card)
585{
586 struct snd_soc_dapm_widget *w;
587
588 lockdep_assert_held(&card->dapm_mutex);
589
590 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
591
592 list_for_each_entry(w, &card->widgets, list) {
593 w->new_power = w->power;
594 w->power_checked = false;
595 }
596}
597
598static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
599{
600 if (!dapm->component)
601 return NULL;
602 return dapm->component->name_prefix;
603}
604
605static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
606 unsigned int *value)
607{
608 if (!dapm->component)
609 return -EIO;
610 return snd_soc_component_read(dapm->component, reg, value);
611}
612
613static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
614 int reg, unsigned int mask, unsigned int value)
615{
616 if (!dapm->component)
617 return -EIO;
618 return snd_soc_component_update_bits(dapm->component, reg,
619 mask, value);
620}
621
622static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
623 int reg, unsigned int mask, unsigned int value)
624{
625 if (!dapm->component)
626 return -EIO;
627 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
628}
629
630static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
631{
632 if (dapm->component)
633 snd_soc_component_async_complete(dapm->component);
634}
635
636static struct snd_soc_dapm_widget *
637dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
638{
639 struct snd_soc_dapm_widget *w = wcache->widget;
640 struct list_head *wlist;
641 const int depth = 2;
642 int i = 0;
643
644 if (w) {
645 wlist = &w->dapm->card->widgets;
646
647 list_for_each_entry_from(w, wlist, list) {
648 if (!strcmp(name, w->name))
649 return w;
650
651 if (++i == depth)
652 break;
653 }
654 }
655
656 return NULL;
657}
658
659static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
660 struct snd_soc_dapm_widget *w)
661{
662 wcache->widget = w;
663}
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
683 enum snd_soc_bias_level level)
684{
685 int ret = 0;
686
687 if (dapm->set_bias_level)
688 ret = dapm->set_bias_level(dapm, level);
689
690 if (ret == 0)
691 dapm->bias_level = level;
692
693 return ret;
694}
695EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
696
697
698
699
700
701
702
703
704
705
706static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
707 enum snd_soc_bias_level level)
708{
709 struct snd_soc_card *card = dapm->card;
710 int ret = 0;
711
712 trace_snd_soc_bias_level_start(card, level);
713
714 if (card && card->set_bias_level)
715 ret = card->set_bias_level(card, dapm, level);
716 if (ret != 0)
717 goto out;
718
719 if (!card || dapm != &card->dapm)
720 ret = snd_soc_dapm_force_bias_level(dapm, level);
721
722 if (ret != 0)
723 goto out;
724
725 if (card && card->set_bias_level_post)
726 ret = card->set_bias_level_post(card, dapm, level);
727out:
728 trace_snd_soc_bias_level_done(card, level);
729
730 return ret;
731}
732
733
734static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
735 struct snd_soc_dapm_path *path, const char *control_name,
736 struct snd_soc_dapm_widget *w)
737{
738 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
739 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
740 unsigned int val, item;
741 int i;
742
743 if (e->reg != SND_SOC_NOPM) {
744 soc_dapm_read(dapm, e->reg, &val);
745 val = (val >> e->shift_l) & e->mask;
746 item = snd_soc_enum_val_to_item(e, val);
747 } else {
748
749
750
751
752
753
754 item = 0;
755 }
756
757 i = match_string(e->texts, e->items, control_name);
758 if (i < 0)
759 return -ENODEV;
760
761 path->name = e->texts[i];
762 path->connect = (i == item);
763 return 0;
764
765}
766
767
768static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
769 int nth_path)
770{
771 struct soc_mixer_control *mc = (struct soc_mixer_control *)
772 p->sink->kcontrol_news[i].private_value;
773 unsigned int reg = mc->reg;
774 unsigned int shift = mc->shift;
775 unsigned int max = mc->max;
776 unsigned int mask = (1 << fls(max)) - 1;
777 unsigned int invert = mc->invert;
778 unsigned int val;
779
780 if (reg != SND_SOC_NOPM) {
781 soc_dapm_read(p->sink->dapm, reg, &val);
782
783
784
785
786
787
788
789
790
791
792
793
794 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
795 if (reg != mc->rreg)
796 soc_dapm_read(p->sink->dapm, mc->rreg, &val);
797 val = (val >> mc->rshift) & mask;
798 } else {
799 val = (val >> shift) & mask;
800 }
801 if (invert)
802 val = max - val;
803 p->connect = !!val;
804 } else {
805 p->connect = 0;
806 }
807}
808
809
810static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
811 struct snd_soc_dapm_path *path, const char *control_name)
812{
813 int i, nth_path = 0;
814
815
816 for (i = 0; i < path->sink->num_kcontrols; i++) {
817 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
818 path->name = path->sink->kcontrol_news[i].name;
819 dapm_set_mixer_path_status(path, i, nth_path++);
820 return 0;
821 }
822 }
823 return -ENODEV;
824}
825
826static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
827 struct snd_soc_dapm_widget *kcontrolw,
828 const struct snd_kcontrol_new *kcontrol_new,
829 struct snd_kcontrol **kcontrol)
830{
831 struct snd_soc_dapm_widget *w;
832 int i;
833
834 *kcontrol = NULL;
835
836 list_for_each_entry(w, &dapm->card->widgets, list) {
837 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
838 continue;
839 for (i = 0; i < w->num_kcontrols; i++) {
840 if (&w->kcontrol_news[i] == kcontrol_new) {
841 if (w->kcontrols)
842 *kcontrol = w->kcontrols[i];
843 return 1;
844 }
845 }
846 }
847
848 return 0;
849}
850
851
852
853
854
855static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
856 int kci)
857{
858 struct snd_soc_dapm_context *dapm = w->dapm;
859 struct snd_card *card = dapm->card->snd_card;
860 const char *prefix;
861 size_t prefix_len;
862 int shared;
863 struct snd_kcontrol *kcontrol;
864 bool wname_in_long_name, kcname_in_long_name;
865 char *long_name = NULL;
866 const char *name;
867 int ret = 0;
868
869 prefix = soc_dapm_prefix(dapm);
870 if (prefix)
871 prefix_len = strlen(prefix) + 1;
872 else
873 prefix_len = 0;
874
875 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
876 &kcontrol);
877
878 if (!kcontrol) {
879 if (shared) {
880 wname_in_long_name = false;
881 kcname_in_long_name = true;
882 } else {
883 switch (w->id) {
884 case snd_soc_dapm_switch:
885 case snd_soc_dapm_mixer:
886 case snd_soc_dapm_pga:
887 case snd_soc_dapm_effect:
888 case snd_soc_dapm_out_drv:
889 wname_in_long_name = true;
890 kcname_in_long_name = true;
891 break;
892 case snd_soc_dapm_mixer_named_ctl:
893 wname_in_long_name = false;
894 kcname_in_long_name = true;
895 break;
896 case snd_soc_dapm_demux:
897 case snd_soc_dapm_mux:
898 wname_in_long_name = true;
899 kcname_in_long_name = false;
900 break;
901 default:
902 return -EINVAL;
903 }
904 }
905
906 if (wname_in_long_name && kcname_in_long_name) {
907
908
909
910
911
912
913 long_name = kasprintf(GFP_KERNEL, "%s %s",
914 w->name + prefix_len,
915 w->kcontrol_news[kci].name);
916 if (long_name == NULL)
917 return -ENOMEM;
918
919 name = long_name;
920 } else if (wname_in_long_name) {
921 long_name = NULL;
922 name = w->name + prefix_len;
923 } else {
924 long_name = NULL;
925 name = w->kcontrol_news[kci].name;
926 }
927
928 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
929 prefix);
930 if (!kcontrol) {
931 ret = -ENOMEM;
932 goto exit_free;
933 }
934
935 kcontrol->private_free = dapm_kcontrol_free;
936
937 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
938 if (ret) {
939 snd_ctl_free_one(kcontrol);
940 goto exit_free;
941 }
942
943 ret = snd_ctl_add(card, kcontrol);
944 if (ret < 0) {
945 dev_err(dapm->dev,
946 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
947 w->name, name, ret);
948 goto exit_free;
949 }
950 }
951
952 ret = dapm_kcontrol_add_widget(kcontrol, w);
953 if (ret == 0)
954 w->kcontrols[kci] = kcontrol;
955
956exit_free:
957 kfree(long_name);
958
959 return ret;
960}
961
962
963static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
964{
965 int i, ret;
966 struct snd_soc_dapm_path *path;
967 struct dapm_kcontrol_data *data;
968
969
970 for (i = 0; i < w->num_kcontrols; i++) {
971
972 snd_soc_dapm_widget_for_each_source_path(w, path) {
973
974 if (path->name != (char *)w->kcontrol_news[i].name)
975 continue;
976
977 if (!w->kcontrols[i]) {
978 ret = dapm_create_or_share_kcontrol(w, i);
979 if (ret < 0)
980 return ret;
981 }
982
983 dapm_kcontrol_add_path(w->kcontrols[i], path);
984
985 data = snd_kcontrol_chip(w->kcontrols[i]);
986 if (data->widget)
987 snd_soc_dapm_add_path(data->widget->dapm,
988 data->widget,
989 path->source,
990 NULL, NULL);
991 }
992 }
993
994 return 0;
995}
996
997
998static int dapm_new_mux(struct snd_soc_dapm_widget *w)
999{
1000 struct snd_soc_dapm_context *dapm = w->dapm;
1001 enum snd_soc_dapm_direction dir;
1002 struct snd_soc_dapm_path *path;
1003 const char *type;
1004 int ret;
1005
1006 switch (w->id) {
1007 case snd_soc_dapm_mux:
1008 dir = SND_SOC_DAPM_DIR_OUT;
1009 type = "mux";
1010 break;
1011 case snd_soc_dapm_demux:
1012 dir = SND_SOC_DAPM_DIR_IN;
1013 type = "demux";
1014 break;
1015 default:
1016 return -EINVAL;
1017 }
1018
1019 if (w->num_kcontrols != 1) {
1020 dev_err(dapm->dev,
1021 "ASoC: %s %s has incorrect number of controls\n", type,
1022 w->name);
1023 return -EINVAL;
1024 }
1025
1026 if (list_empty(&w->edges[dir])) {
1027 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
1028 return -EINVAL;
1029 }
1030
1031 ret = dapm_create_or_share_kcontrol(w, 0);
1032 if (ret < 0)
1033 return ret;
1034
1035 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1036 if (path->name)
1037 dapm_kcontrol_add_path(w->kcontrols[0], path);
1038 }
1039
1040 return 0;
1041}
1042
1043
1044static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1045{
1046 int i, ret;
1047
1048 for (i = 0; i < w->num_kcontrols; i++) {
1049 ret = dapm_create_or_share_kcontrol(w, i);
1050 if (ret < 0)
1051 return ret;
1052 }
1053
1054 return 0;
1055}
1056
1057
1058static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1059{
1060 int i, ret;
1061 struct snd_kcontrol *kcontrol;
1062 struct snd_soc_dapm_context *dapm = w->dapm;
1063 struct snd_card *card = dapm->card->snd_card;
1064 struct snd_soc_pcm_runtime *rtd = w->priv;
1065
1066
1067 if (rtd->dai_link->num_params <= 1)
1068 return 0;
1069
1070
1071 for (i = 0; i < w->num_kcontrols; i++) {
1072 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1073 w->name, NULL);
1074 ret = snd_ctl_add(card, kcontrol);
1075 if (ret < 0) {
1076 dev_err(dapm->dev,
1077 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1078 w->name, w->kcontrol_news[i].name, ret);
1079 return ret;
1080 }
1081 kcontrol->private_data = w;
1082 w->kcontrols[i] = kcontrol;
1083 }
1084
1085 return 0;
1086}
1087
1088
1089
1090
1091
1092static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1093{
1094 int level = snd_power_get_state(widget->dapm->card->snd_card);
1095
1096 switch (level) {
1097 case SNDRV_CTL_POWER_D3hot:
1098 case SNDRV_CTL_POWER_D3cold:
1099 if (widget->ignore_suspend)
1100 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1101 widget->name);
1102 return widget->ignore_suspend;
1103 default:
1104 return 1;
1105 }
1106}
1107
1108static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1109 struct list_head *widgets)
1110{
1111 struct snd_soc_dapm_widget *w;
1112 struct list_head *it;
1113 unsigned int size = 0;
1114 unsigned int i = 0;
1115
1116 list_for_each(it, widgets)
1117 size++;
1118
1119 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
1120 if (*list == NULL)
1121 return -ENOMEM;
1122
1123 list_for_each_entry(w, widgets, work_list)
1124 (*list)->widgets[i++] = w;
1125
1126 (*list)->num_widgets = i;
1127
1128 return 0;
1129}
1130
1131
1132
1133
1134
1135
1136
1137
1138static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1139 struct list_head *list, enum snd_soc_dapm_direction dir,
1140 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1141 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1142 enum snd_soc_dapm_direction)),
1143 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1144 enum snd_soc_dapm_direction))
1145{
1146 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1147 struct snd_soc_dapm_path *path;
1148 int con = 0;
1149
1150 if (widget->endpoints[dir] >= 0)
1151 return widget->endpoints[dir];
1152
1153 DAPM_UPDATE_STAT(widget, path_checks);
1154
1155
1156 if (list)
1157 list_add_tail(&widget->work_list, list);
1158
1159 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1160 list = NULL;
1161 custom_stop_condition = NULL;
1162 }
1163
1164 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1165 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1166 return widget->endpoints[dir];
1167 }
1168
1169 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1170 DAPM_UPDATE_STAT(widget, neighbour_checks);
1171
1172 if (path->weak || path->is_supply)
1173 continue;
1174
1175 if (path->walking)
1176 return 1;
1177
1178 trace_snd_soc_dapm_path(widget, dir, path);
1179
1180 if (path->connect) {
1181 path->walking = 1;
1182 con += fn(path->node[dir], list, custom_stop_condition);
1183 path->walking = 0;
1184 }
1185 }
1186
1187 widget->endpoints[dir] = con;
1188
1189 return con;
1190}
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1202 struct list_head *list,
1203 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1204 enum snd_soc_dapm_direction))
1205{
1206 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1207 is_connected_output_ep, custom_stop_condition);
1208}
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1220 struct list_head *list,
1221 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1222 enum snd_soc_dapm_direction))
1223{
1224 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1225 is_connected_input_ep, custom_stop_condition);
1226}
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1248 struct snd_soc_dapm_widget_list **list,
1249 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1250 enum snd_soc_dapm_direction))
1251{
1252 struct snd_soc_card *card = dai->component->card;
1253 struct snd_soc_dapm_widget *w;
1254 LIST_HEAD(widgets);
1255 int paths;
1256 int ret;
1257
1258 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1259
1260
1261
1262
1263
1264 list_for_each_entry(w, &card->widgets, list) {
1265 w->endpoints[SND_SOC_DAPM_DIR_IN] = -1;
1266 w->endpoints[SND_SOC_DAPM_DIR_OUT] = -1;
1267 }
1268
1269 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1270 paths = is_connected_output_ep(dai->playback_widget, &widgets,
1271 custom_stop_condition);
1272 else
1273 paths = is_connected_input_ep(dai->capture_widget, &widgets,
1274 custom_stop_condition);
1275
1276
1277 list_del(widgets.next);
1278
1279 ret = dapm_widget_list_create(list, &widgets);
1280 if (ret)
1281 paths = ret;
1282
1283 trace_snd_soc_dapm_connected(paths, stream);
1284 mutex_unlock(&card->dapm_mutex);
1285
1286 return paths;
1287}
1288
1289
1290
1291
1292int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1293 struct snd_kcontrol *kcontrol, int event)
1294{
1295 int ret;
1296
1297 soc_dapm_async_complete(w->dapm);
1298
1299 if (SND_SOC_DAPM_EVENT_ON(event)) {
1300 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1301 ret = regulator_allow_bypass(w->regulator, false);
1302 if (ret != 0)
1303 dev_warn(w->dapm->dev,
1304 "ASoC: Failed to unbypass %s: %d\n",
1305 w->name, ret);
1306 }
1307
1308 return regulator_enable(w->regulator);
1309 } else {
1310 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1311 ret = regulator_allow_bypass(w->regulator, true);
1312 if (ret != 0)
1313 dev_warn(w->dapm->dev,
1314 "ASoC: Failed to bypass %s: %d\n",
1315 w->name, ret);
1316 }
1317
1318 return regulator_disable_deferred(w->regulator, w->shift);
1319 }
1320}
1321EXPORT_SYMBOL_GPL(dapm_regulator_event);
1322
1323
1324
1325
1326int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1327 struct snd_kcontrol *kcontrol, int event)
1328{
1329 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1330 struct pinctrl *p = w->pinctrl;
1331 struct pinctrl_state *s;
1332
1333 if (!p || !priv)
1334 return -EIO;
1335
1336 if (SND_SOC_DAPM_EVENT_ON(event))
1337 s = pinctrl_lookup_state(p, priv->active_state);
1338 else
1339 s = pinctrl_lookup_state(p, priv->sleep_state);
1340
1341 if (IS_ERR(s))
1342 return PTR_ERR(s);
1343
1344 return pinctrl_select_state(p, s);
1345}
1346EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1347
1348
1349
1350
1351int dapm_clock_event(struct snd_soc_dapm_widget *w,
1352 struct snd_kcontrol *kcontrol, int event)
1353{
1354 if (!w->clk)
1355 return -EIO;
1356
1357 soc_dapm_async_complete(w->dapm);
1358
1359 if (SND_SOC_DAPM_EVENT_ON(event)) {
1360 return clk_prepare_enable(w->clk);
1361 } else {
1362 clk_disable_unprepare(w->clk);
1363 return 0;
1364 }
1365
1366 return 0;
1367}
1368EXPORT_SYMBOL_GPL(dapm_clock_event);
1369
1370static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1371{
1372 if (w->power_checked)
1373 return w->new_power;
1374
1375 if (w->force)
1376 w->new_power = 1;
1377 else
1378 w->new_power = w->power_check(w);
1379
1380 w->power_checked = true;
1381
1382 return w->new_power;
1383}
1384
1385
1386static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1387{
1388 int in, out;
1389
1390 DAPM_UPDATE_STAT(w, power_checks);
1391
1392 in = is_connected_input_ep(w, NULL, NULL);
1393 out = is_connected_output_ep(w, NULL, NULL);
1394 return out != 0 && in != 0;
1395}
1396
1397
1398static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1399{
1400 struct snd_soc_dapm_path *path;
1401
1402 DAPM_UPDATE_STAT(w, power_checks);
1403
1404
1405 snd_soc_dapm_widget_for_each_sink_path(w, path) {
1406 DAPM_UPDATE_STAT(w, neighbour_checks);
1407
1408 if (path->weak)
1409 continue;
1410
1411 if (path->connected &&
1412 !path->connected(path->source, path->sink))
1413 continue;
1414
1415 if (dapm_widget_power_check(path->sink))
1416 return 1;
1417 }
1418
1419 return 0;
1420}
1421
1422static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1423{
1424 return w->connected;
1425}
1426
1427static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1428 struct snd_soc_dapm_widget *b,
1429 bool power_up)
1430{
1431 int *sort;
1432
1433 BUILD_BUG_ON(ARRAY_SIZE(dapm_up_seq) != SND_SOC_DAPM_TYPE_COUNT);
1434 BUILD_BUG_ON(ARRAY_SIZE(dapm_down_seq) != SND_SOC_DAPM_TYPE_COUNT);
1435
1436 if (power_up)
1437 sort = dapm_up_seq;
1438 else
1439 sort = dapm_down_seq;
1440
1441 WARN_ONCE(sort[a->id] == 0, "offset a->id %d not initialized\n", a->id);
1442 WARN_ONCE(sort[b->id] == 0, "offset b->id %d not initialized\n", b->id);
1443
1444 if (sort[a->id] != sort[b->id])
1445 return sort[a->id] - sort[b->id];
1446 if (a->subseq != b->subseq) {
1447 if (power_up)
1448 return a->subseq - b->subseq;
1449 else
1450 return b->subseq - a->subseq;
1451 }
1452 if (a->reg != b->reg)
1453 return a->reg - b->reg;
1454 if (a->dapm != b->dapm)
1455 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1456
1457 return 0;
1458}
1459
1460
1461static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1462 struct list_head *list,
1463 bool power_up)
1464{
1465 struct snd_soc_dapm_widget *w;
1466
1467 list_for_each_entry(w, list, power_list)
1468 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1469 list_add_tail(&new_widget->power_list, &w->power_list);
1470 return;
1471 }
1472
1473 list_add_tail(&new_widget->power_list, list);
1474}
1475
1476static void dapm_seq_check_event(struct snd_soc_card *card,
1477 struct snd_soc_dapm_widget *w, int event)
1478{
1479 const char *ev_name;
1480 int power, ret;
1481
1482 switch (event) {
1483 case SND_SOC_DAPM_PRE_PMU:
1484 ev_name = "PRE_PMU";
1485 power = 1;
1486 break;
1487 case SND_SOC_DAPM_POST_PMU:
1488 ev_name = "POST_PMU";
1489 power = 1;
1490 break;
1491 case SND_SOC_DAPM_PRE_PMD:
1492 ev_name = "PRE_PMD";
1493 power = 0;
1494 break;
1495 case SND_SOC_DAPM_POST_PMD:
1496 ev_name = "POST_PMD";
1497 power = 0;
1498 break;
1499 case SND_SOC_DAPM_WILL_PMU:
1500 ev_name = "WILL_PMU";
1501 power = 1;
1502 break;
1503 case SND_SOC_DAPM_WILL_PMD:
1504 ev_name = "WILL_PMD";
1505 power = 0;
1506 break;
1507 default:
1508 WARN(1, "Unknown event %d\n", event);
1509 return;
1510 }
1511
1512 if (w->new_power != power)
1513 return;
1514
1515 if (w->event && (w->event_flags & event)) {
1516 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1517 w->name, ev_name);
1518 soc_dapm_async_complete(w->dapm);
1519 trace_snd_soc_dapm_widget_event_start(w, event);
1520 ret = w->event(w, NULL, event);
1521 trace_snd_soc_dapm_widget_event_done(w, event);
1522 if (ret < 0)
1523 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1524 ev_name, w->name, ret);
1525 }
1526}
1527
1528
1529static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1530 struct list_head *pending)
1531{
1532 struct snd_soc_dapm_context *dapm;
1533 struct snd_soc_dapm_widget *w;
1534 int reg;
1535 unsigned int value = 0;
1536 unsigned int mask = 0;
1537
1538 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1539 reg = w->reg;
1540 dapm = w->dapm;
1541
1542 list_for_each_entry(w, pending, power_list) {
1543 WARN_ON(reg != w->reg || dapm != w->dapm);
1544 w->power = w->new_power;
1545
1546 mask |= w->mask << w->shift;
1547 if (w->power)
1548 value |= w->on_val << w->shift;
1549 else
1550 value |= w->off_val << w->shift;
1551
1552 pop_dbg(dapm->dev, card->pop_time,
1553 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1554 w->name, reg, value, mask);
1555
1556
1557 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1558 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1559 }
1560
1561 if (reg >= 0) {
1562
1563
1564
1565
1566 pop_dbg(dapm->dev, card->pop_time,
1567 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1568 value, mask, reg, card->pop_time);
1569 pop_wait(card->pop_time);
1570 soc_dapm_update_bits(dapm, reg, mask, value);
1571 }
1572
1573 list_for_each_entry(w, pending, power_list) {
1574 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1575 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1576 }
1577}
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587static void dapm_seq_run(struct snd_soc_card *card,
1588 struct list_head *list, int event, bool power_up)
1589{
1590 struct snd_soc_dapm_widget *w, *n;
1591 struct snd_soc_dapm_context *d;
1592 LIST_HEAD(pending);
1593 int cur_sort = -1;
1594 int cur_subseq = -1;
1595 int cur_reg = SND_SOC_NOPM;
1596 struct snd_soc_dapm_context *cur_dapm = NULL;
1597 int ret, i;
1598 int *sort;
1599
1600 if (power_up)
1601 sort = dapm_up_seq;
1602 else
1603 sort = dapm_down_seq;
1604
1605 list_for_each_entry_safe(w, n, list, power_list) {
1606 ret = 0;
1607
1608
1609 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1610 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1611 if (!list_empty(&pending))
1612 dapm_seq_run_coalesced(card, &pending);
1613
1614 if (cur_dapm && cur_dapm->seq_notifier) {
1615 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1616 if (sort[i] == cur_sort)
1617 cur_dapm->seq_notifier(cur_dapm,
1618 i,
1619 cur_subseq);
1620 }
1621
1622 if (cur_dapm && w->dapm != cur_dapm)
1623 soc_dapm_async_complete(cur_dapm);
1624
1625 INIT_LIST_HEAD(&pending);
1626 cur_sort = -1;
1627 cur_subseq = INT_MIN;
1628 cur_reg = SND_SOC_NOPM;
1629 cur_dapm = NULL;
1630 }
1631
1632 switch (w->id) {
1633 case snd_soc_dapm_pre:
1634 if (!w->event)
1635 list_for_each_entry_safe_continue(w, n, list,
1636 power_list);
1637
1638 if (event == SND_SOC_DAPM_STREAM_START)
1639 ret = w->event(w,
1640 NULL, SND_SOC_DAPM_PRE_PMU);
1641 else if (event == SND_SOC_DAPM_STREAM_STOP)
1642 ret = w->event(w,
1643 NULL, SND_SOC_DAPM_PRE_PMD);
1644 break;
1645
1646 case snd_soc_dapm_post:
1647 if (!w->event)
1648 list_for_each_entry_safe_continue(w, n, list,
1649 power_list);
1650
1651 if (event == SND_SOC_DAPM_STREAM_START)
1652 ret = w->event(w,
1653 NULL, SND_SOC_DAPM_POST_PMU);
1654 else if (event == SND_SOC_DAPM_STREAM_STOP)
1655 ret = w->event(w,
1656 NULL, SND_SOC_DAPM_POST_PMD);
1657 break;
1658
1659 default:
1660
1661 cur_sort = sort[w->id];
1662 cur_subseq = w->subseq;
1663 cur_reg = w->reg;
1664 cur_dapm = w->dapm;
1665 list_move(&w->power_list, &pending);
1666 break;
1667 }
1668
1669 if (ret < 0)
1670 dev_err(w->dapm->dev,
1671 "ASoC: Failed to apply widget power: %d\n", ret);
1672 }
1673
1674 if (!list_empty(&pending))
1675 dapm_seq_run_coalesced(card, &pending);
1676
1677 if (cur_dapm && cur_dapm->seq_notifier) {
1678 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1679 if (sort[i] == cur_sort)
1680 cur_dapm->seq_notifier(cur_dapm,
1681 i, cur_subseq);
1682 }
1683
1684 list_for_each_entry(d, &card->dapm_list, list) {
1685 soc_dapm_async_complete(d);
1686 }
1687}
1688
1689static void dapm_widget_update(struct snd_soc_card *card)
1690{
1691 struct snd_soc_dapm_update *update = card->update;
1692 struct snd_soc_dapm_widget_list *wlist;
1693 struct snd_soc_dapm_widget *w = NULL;
1694 unsigned int wi;
1695 int ret;
1696
1697 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1698 return;
1699
1700 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1701
1702 for (wi = 0; wi < wlist->num_widgets; wi++) {
1703 w = wlist->widgets[wi];
1704
1705 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1706 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1707 if (ret != 0)
1708 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1709 w->name, ret);
1710 }
1711 }
1712
1713 if (!w)
1714 return;
1715
1716 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1717 update->val);
1718 if (ret < 0)
1719 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1720 w->name, ret);
1721
1722 if (update->has_second_set) {
1723 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1724 update->mask2, update->val2);
1725 if (ret < 0)
1726 dev_err(w->dapm->dev,
1727 "ASoC: %s DAPM update failed: %d\n",
1728 w->name, ret);
1729 }
1730
1731 for (wi = 0; wi < wlist->num_widgets; wi++) {
1732 w = wlist->widgets[wi];
1733
1734 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1735 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1736 if (ret != 0)
1737 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1738 w->name, ret);
1739 }
1740 }
1741}
1742
1743
1744
1745
1746static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1747{
1748 struct snd_soc_dapm_context *d = data;
1749 int ret;
1750
1751
1752 if (d->bias_level == SND_SOC_BIAS_OFF &&
1753 d->target_bias_level != SND_SOC_BIAS_OFF) {
1754 if (d->dev)
1755 pm_runtime_get_sync(d->dev);
1756
1757 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1758 if (ret != 0)
1759 dev_err(d->dev,
1760 "ASoC: Failed to turn on bias: %d\n", ret);
1761 }
1762
1763
1764 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1765 d->bias_level != SND_SOC_BIAS_ON) ||
1766 (d->target_bias_level != SND_SOC_BIAS_ON &&
1767 d->bias_level == SND_SOC_BIAS_ON)) {
1768 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1769 if (ret != 0)
1770 dev_err(d->dev,
1771 "ASoC: Failed to prepare bias: %d\n", ret);
1772 }
1773}
1774
1775
1776
1777
1778static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1779{
1780 struct snd_soc_dapm_context *d = data;
1781 int ret;
1782
1783
1784 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1785 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1786 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1787 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1788 if (ret != 0)
1789 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1790 ret);
1791 }
1792
1793
1794 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1795 d->target_bias_level == SND_SOC_BIAS_OFF) {
1796 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1797 if (ret != 0)
1798 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1799 ret);
1800
1801 if (d->dev)
1802 pm_runtime_put(d->dev);
1803 }
1804
1805
1806 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1807 d->target_bias_level == SND_SOC_BIAS_ON) {
1808 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1809 if (ret != 0)
1810 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1811 ret);
1812 }
1813}
1814
1815static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1816 bool power, bool connect)
1817{
1818
1819
1820
1821 if (!connect)
1822 return;
1823
1824
1825
1826 if (power != peer->power)
1827 dapm_mark_dirty(peer, "peer state change");
1828}
1829
1830static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1831 struct list_head *up_list,
1832 struct list_head *down_list)
1833{
1834 struct snd_soc_dapm_path *path;
1835
1836 if (w->power == power)
1837 return;
1838
1839 trace_snd_soc_dapm_widget_power(w, power);
1840
1841
1842
1843
1844 snd_soc_dapm_widget_for_each_source_path(w, path)
1845 dapm_widget_set_peer_power(path->source, power, path->connect);
1846
1847
1848 if (!w->is_supply) {
1849 snd_soc_dapm_widget_for_each_sink_path(w, path)
1850 dapm_widget_set_peer_power(path->sink, power,
1851 path->connect);
1852 }
1853
1854 if (power)
1855 dapm_seq_insert(w, up_list, true);
1856 else
1857 dapm_seq_insert(w, down_list, false);
1858}
1859
1860static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1861 struct list_head *up_list,
1862 struct list_head *down_list)
1863{
1864 int power;
1865
1866 switch (w->id) {
1867 case snd_soc_dapm_pre:
1868 dapm_seq_insert(w, down_list, false);
1869 break;
1870 case snd_soc_dapm_post:
1871 dapm_seq_insert(w, up_list, true);
1872 break;
1873
1874 default:
1875 power = dapm_widget_power_check(w);
1876
1877 dapm_widget_set_power(w, power, up_list, down_list);
1878 break;
1879 }
1880}
1881
1882static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1883{
1884 if (dapm->idle_bias_off)
1885 return true;
1886
1887 switch (snd_power_get_state(dapm->card->snd_card)) {
1888 case SNDRV_CTL_POWER_D3hot:
1889 case SNDRV_CTL_POWER_D3cold:
1890 return dapm->suspend_bias_off;
1891 default:
1892 break;
1893 }
1894
1895 return false;
1896}
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907static int dapm_power_widgets(struct snd_soc_card *card, int event)
1908{
1909 struct snd_soc_dapm_widget *w;
1910 struct snd_soc_dapm_context *d;
1911 LIST_HEAD(up_list);
1912 LIST_HEAD(down_list);
1913 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1914 enum snd_soc_bias_level bias;
1915
1916 lockdep_assert_held(&card->dapm_mutex);
1917
1918 trace_snd_soc_dapm_start(card);
1919
1920 list_for_each_entry(d, &card->dapm_list, list) {
1921 if (dapm_idle_bias_off(d))
1922 d->target_bias_level = SND_SOC_BIAS_OFF;
1923 else
1924 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1925 }
1926
1927 dapm_reset(card);
1928
1929
1930
1931
1932
1933
1934
1935 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1936 dapm_power_one_widget(w, &up_list, &down_list);
1937 }
1938
1939 list_for_each_entry(w, &card->widgets, list) {
1940 switch (w->id) {
1941 case snd_soc_dapm_pre:
1942 case snd_soc_dapm_post:
1943
1944 break;
1945 default:
1946 list_del_init(&w->dirty);
1947 break;
1948 }
1949
1950 if (w->new_power) {
1951 d = w->dapm;
1952
1953
1954
1955
1956
1957
1958
1959
1960 switch (w->id) {
1961 case snd_soc_dapm_siggen:
1962 case snd_soc_dapm_vmid:
1963 break;
1964 case snd_soc_dapm_supply:
1965 case snd_soc_dapm_regulator_supply:
1966 case snd_soc_dapm_pinctrl:
1967 case snd_soc_dapm_clock_supply:
1968 case snd_soc_dapm_micbias:
1969 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1970 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1971 break;
1972 default:
1973 d->target_bias_level = SND_SOC_BIAS_ON;
1974 break;
1975 }
1976 }
1977
1978 }
1979
1980
1981
1982
1983 bias = SND_SOC_BIAS_OFF;
1984 list_for_each_entry(d, &card->dapm_list, list)
1985 if (d->target_bias_level > bias)
1986 bias = d->target_bias_level;
1987 list_for_each_entry(d, &card->dapm_list, list)
1988 if (!dapm_idle_bias_off(d))
1989 d->target_bias_level = bias;
1990
1991 trace_snd_soc_dapm_walk_done(card);
1992
1993
1994 dapm_pre_sequence_async(&card->dapm, 0);
1995
1996 list_for_each_entry(d, &card->dapm_list, list) {
1997 if (d != &card->dapm && d->bias_level != d->target_bias_level)
1998 async_schedule_domain(dapm_pre_sequence_async, d,
1999 &async_domain);
2000 }
2001 async_synchronize_full_domain(&async_domain);
2002
2003 list_for_each_entry(w, &down_list, power_list) {
2004 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
2005 }
2006
2007 list_for_each_entry(w, &up_list, power_list) {
2008 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
2009 }
2010
2011
2012 dapm_seq_run(card, &down_list, event, false);
2013
2014 dapm_widget_update(card);
2015
2016
2017 dapm_seq_run(card, &up_list, event, true);
2018
2019
2020 list_for_each_entry(d, &card->dapm_list, list) {
2021 if (d != &card->dapm && d->bias_level != d->target_bias_level)
2022 async_schedule_domain(dapm_post_sequence_async, d,
2023 &async_domain);
2024 }
2025 async_synchronize_full_domain(&async_domain);
2026
2027 dapm_post_sequence_async(&card->dapm, 0);
2028
2029
2030 list_for_each_entry(d, &card->dapm_list, list) {
2031 if (d->stream_event)
2032 d->stream_event(d, event);
2033 }
2034
2035 pop_dbg(card->dev, card->pop_time,
2036 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
2037 pop_wait(card->pop_time);
2038
2039 trace_snd_soc_dapm_done(card);
2040
2041 return 0;
2042}
2043
2044#ifdef CONFIG_DEBUG_FS
2045static ssize_t dapm_widget_power_read_file(struct file *file,
2046 char __user *user_buf,
2047 size_t count, loff_t *ppos)
2048{
2049 struct snd_soc_dapm_widget *w = file->private_data;
2050 struct snd_soc_card *card = w->dapm->card;
2051 enum snd_soc_dapm_direction dir, rdir;
2052 char *buf;
2053 int in, out;
2054 ssize_t ret;
2055 struct snd_soc_dapm_path *p = NULL;
2056
2057 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2058 if (!buf)
2059 return -ENOMEM;
2060
2061 mutex_lock(&card->dapm_mutex);
2062
2063
2064 if (w->is_supply) {
2065 in = 0;
2066 out = 0;
2067 } else {
2068 in = is_connected_input_ep(w, NULL, NULL);
2069 out = is_connected_output_ep(w, NULL, NULL);
2070 }
2071
2072 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2073 w->name, w->power ? "On" : "Off",
2074 w->force ? " (forced)" : "", in, out);
2075
2076 if (w->reg >= 0)
2077 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2078 " - R%d(0x%x) mask 0x%x",
2079 w->reg, w->reg, w->mask << w->shift);
2080
2081 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
2082
2083 if (w->sname)
2084 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2085 w->sname,
2086 w->active ? "active" : "inactive");
2087
2088 snd_soc_dapm_for_each_direction(dir) {
2089 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2090 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2091 if (p->connected && !p->connected(p->source, p->sink))
2092 continue;
2093
2094 if (!p->connect)
2095 continue;
2096
2097 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2098 " %s \"%s\" \"%s\"\n",
2099 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2100 p->name ? p->name : "static",
2101 p->node[rdir]->name);
2102 }
2103 }
2104
2105 mutex_unlock(&card->dapm_mutex);
2106
2107 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2108
2109 kfree(buf);
2110 return ret;
2111}
2112
2113static const struct file_operations dapm_widget_power_fops = {
2114 .open = simple_open,
2115 .read = dapm_widget_power_read_file,
2116 .llseek = default_llseek,
2117};
2118
2119static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2120 size_t count, loff_t *ppos)
2121{
2122 struct snd_soc_dapm_context *dapm = file->private_data;
2123 char *level;
2124
2125 switch (dapm->bias_level) {
2126 case SND_SOC_BIAS_ON:
2127 level = "On\n";
2128 break;
2129 case SND_SOC_BIAS_PREPARE:
2130 level = "Prepare\n";
2131 break;
2132 case SND_SOC_BIAS_STANDBY:
2133 level = "Standby\n";
2134 break;
2135 case SND_SOC_BIAS_OFF:
2136 level = "Off\n";
2137 break;
2138 default:
2139 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2140 level = "Unknown\n";
2141 break;
2142 }
2143
2144 return simple_read_from_buffer(user_buf, count, ppos, level,
2145 strlen(level));
2146}
2147
2148static const struct file_operations dapm_bias_fops = {
2149 .open = simple_open,
2150 .read = dapm_bias_read_file,
2151 .llseek = default_llseek,
2152};
2153
2154void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2155 struct dentry *parent)
2156{
2157 struct dentry *d;
2158
2159 if (!parent || IS_ERR(parent))
2160 return;
2161
2162 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2163
2164 if (IS_ERR(dapm->debugfs_dapm)) {
2165 dev_warn(dapm->dev,
2166 "ASoC: Failed to create DAPM debugfs directory %ld\n",
2167 PTR_ERR(dapm->debugfs_dapm));
2168 return;
2169 }
2170
2171 d = debugfs_create_file("bias_level", 0444,
2172 dapm->debugfs_dapm, dapm,
2173 &dapm_bias_fops);
2174 if (IS_ERR(d))
2175 dev_warn(dapm->dev,
2176 "ASoC: Failed to create bias level debugfs file: %ld\n",
2177 PTR_ERR(d));
2178}
2179
2180static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2181{
2182 struct snd_soc_dapm_context *dapm = w->dapm;
2183 struct dentry *d;
2184
2185 if (!dapm->debugfs_dapm || !w->name)
2186 return;
2187
2188 d = debugfs_create_file(w->name, 0444,
2189 dapm->debugfs_dapm, w,
2190 &dapm_widget_power_fops);
2191 if (IS_ERR(d))
2192 dev_warn(w->dapm->dev,
2193 "ASoC: Failed to create %s debugfs file: %ld\n",
2194 w->name, PTR_ERR(d));
2195}
2196
2197static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2198{
2199 if (!dapm->debugfs_dapm)
2200 return;
2201 debugfs_remove_recursive(dapm->debugfs_dapm);
2202 dapm->debugfs_dapm = NULL;
2203}
2204
2205#else
2206void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2207 struct dentry *parent)
2208{
2209}
2210
2211static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2212{
2213}
2214
2215static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2216{
2217}
2218
2219#endif
2220
2221
2222
2223
2224
2225
2226
2227
2228static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2229 bool connect, const char *reason)
2230{
2231 if (path->connect == connect)
2232 return;
2233
2234 path->connect = connect;
2235 dapm_mark_dirty(path->source, reason);
2236 dapm_mark_dirty(path->sink, reason);
2237 dapm_path_invalidate(path);
2238}
2239
2240
2241static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2242 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2243{
2244 struct snd_soc_dapm_path *path;
2245 int found = 0;
2246 bool connect;
2247
2248 lockdep_assert_held(&card->dapm_mutex);
2249
2250
2251 dapm_kcontrol_for_each_path(path, kcontrol) {
2252 found = 1;
2253
2254 if (e && !(strcmp(path->name, e->texts[mux])))
2255 connect = true;
2256 else
2257 connect = false;
2258
2259 soc_dapm_connect_path(path, connect, "mux update");
2260 }
2261
2262 if (found)
2263 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2264
2265 return found;
2266}
2267
2268int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2269 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2270 struct snd_soc_dapm_update *update)
2271{
2272 struct snd_soc_card *card = dapm->card;
2273 int ret;
2274
2275 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2276 card->update = update;
2277 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2278 card->update = NULL;
2279 mutex_unlock(&card->dapm_mutex);
2280 if (ret > 0)
2281 soc_dpcm_runtime_update(card);
2282 return ret;
2283}
2284EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2285
2286
2287static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2288 struct snd_kcontrol *kcontrol,
2289 int connect, int rconnect)
2290{
2291 struct snd_soc_dapm_path *path;
2292 int found = 0;
2293
2294 lockdep_assert_held(&card->dapm_mutex);
2295
2296
2297 dapm_kcontrol_for_each_path(path, kcontrol) {
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320 if (found && rconnect >= 0)
2321 soc_dapm_connect_path(path, rconnect, "mixer update");
2322 else
2323 soc_dapm_connect_path(path, connect, "mixer update");
2324 found = 1;
2325 }
2326
2327 if (found)
2328 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2329
2330 return found;
2331}
2332
2333int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2334 struct snd_kcontrol *kcontrol, int connect,
2335 struct snd_soc_dapm_update *update)
2336{
2337 struct snd_soc_card *card = dapm->card;
2338 int ret;
2339
2340 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2341 card->update = update;
2342 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2343 card->update = NULL;
2344 mutex_unlock(&card->dapm_mutex);
2345 if (ret > 0)
2346 soc_dpcm_runtime_update(card);
2347 return ret;
2348}
2349EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2350
2351static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2352 char *buf)
2353{
2354 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2355 struct snd_soc_dapm_widget *w;
2356 int count = 0;
2357 char *state = "not set";
2358
2359
2360
2361
2362
2363 if (!cmpnt->card)
2364 return 0;
2365
2366 list_for_each_entry(w, &cmpnt->card->widgets, list) {
2367 if (w->dapm != dapm)
2368 continue;
2369
2370
2371 switch (w->id) {
2372 case snd_soc_dapm_hp:
2373 case snd_soc_dapm_mic:
2374 case snd_soc_dapm_spk:
2375 case snd_soc_dapm_line:
2376 case snd_soc_dapm_micbias:
2377 case snd_soc_dapm_dac:
2378 case snd_soc_dapm_adc:
2379 case snd_soc_dapm_pga:
2380 case snd_soc_dapm_effect:
2381 case snd_soc_dapm_out_drv:
2382 case snd_soc_dapm_mixer:
2383 case snd_soc_dapm_mixer_named_ctl:
2384 case snd_soc_dapm_supply:
2385 case snd_soc_dapm_regulator_supply:
2386 case snd_soc_dapm_pinctrl:
2387 case snd_soc_dapm_clock_supply:
2388 if (w->name)
2389 count += sprintf(buf + count, "%s: %s\n",
2390 w->name, w->power ? "On":"Off");
2391 break;
2392 default:
2393 break;
2394 }
2395 }
2396
2397 switch (snd_soc_dapm_get_bias_level(dapm)) {
2398 case SND_SOC_BIAS_ON:
2399 state = "On";
2400 break;
2401 case SND_SOC_BIAS_PREPARE:
2402 state = "Prepare";
2403 break;
2404 case SND_SOC_BIAS_STANDBY:
2405 state = "Standby";
2406 break;
2407 case SND_SOC_BIAS_OFF:
2408 state = "Off";
2409 break;
2410 }
2411 count += sprintf(buf + count, "PM State: %s\n", state);
2412
2413 return count;
2414}
2415
2416
2417static ssize_t dapm_widget_show(struct device *dev,
2418 struct device_attribute *attr, char *buf)
2419{
2420 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2421 struct snd_soc_dai *codec_dai;
2422 int i, count = 0;
2423
2424 mutex_lock(&rtd->card->dapm_mutex);
2425
2426 for_each_rtd_codec_dai(rtd, i, codec_dai) {
2427 struct snd_soc_component *cmpnt = codec_dai->component;
2428
2429 count += dapm_widget_show_component(cmpnt, buf + count);
2430 }
2431
2432 mutex_unlock(&rtd->card->dapm_mutex);
2433
2434 return count;
2435}
2436
2437static DEVICE_ATTR_RO(dapm_widget);
2438
2439struct attribute *soc_dapm_dev_attrs[] = {
2440 &dev_attr_dapm_widget.attr,
2441 NULL
2442};
2443
2444static void dapm_free_path(struct snd_soc_dapm_path *path)
2445{
2446 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2447 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2448 list_del(&path->list_kcontrol);
2449 list_del(&path->list);
2450 kfree(path);
2451}
2452
2453void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2454{
2455 struct snd_soc_dapm_path *p, *next_p;
2456 enum snd_soc_dapm_direction dir;
2457
2458 list_del(&w->list);
2459
2460
2461
2462
2463
2464 snd_soc_dapm_for_each_direction(dir) {
2465 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2466 dapm_free_path(p);
2467 }
2468
2469 kfree(w->kcontrols);
2470 kfree_const(w->name);
2471 kfree_const(w->sname);
2472 kfree(w);
2473}
2474
2475void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2476{
2477 dapm->path_sink_cache.widget = NULL;
2478 dapm->path_source_cache.widget = NULL;
2479}
2480
2481
2482static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2483{
2484 struct snd_soc_dapm_widget *w, *next_w;
2485
2486 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2487 if (w->dapm != dapm)
2488 continue;
2489 snd_soc_dapm_free_widget(w);
2490 }
2491 snd_soc_dapm_reset_cache(dapm);
2492}
2493
2494static struct snd_soc_dapm_widget *dapm_find_widget(
2495 struct snd_soc_dapm_context *dapm, const char *pin,
2496 bool search_other_contexts)
2497{
2498 struct snd_soc_dapm_widget *w;
2499 struct snd_soc_dapm_widget *fallback = NULL;
2500
2501 list_for_each_entry(w, &dapm->card->widgets, list) {
2502 if (!strcmp(w->name, pin)) {
2503 if (w->dapm == dapm)
2504 return w;
2505 else
2506 fallback = w;
2507 }
2508 }
2509
2510 if (search_other_contexts)
2511 return fallback;
2512
2513 return NULL;
2514}
2515
2516static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2517 const char *pin, int status)
2518{
2519 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2520
2521 dapm_assert_locked(dapm);
2522
2523 if (!w) {
2524 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2525 return -EINVAL;
2526 }
2527
2528 if (w->connected != status) {
2529 dapm_mark_dirty(w, "pin configuration");
2530 dapm_widget_invalidate_input_paths(w);
2531 dapm_widget_invalidate_output_paths(w);
2532 }
2533
2534 w->connected = status;
2535 if (status == 0)
2536 w->force = 0;
2537
2538 return 0;
2539}
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2553{
2554
2555
2556
2557
2558 if (!dapm->card || !dapm->card->instantiated)
2559 return 0;
2560
2561 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2562}
2563EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2575{
2576 int ret;
2577
2578 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2579 ret = snd_soc_dapm_sync_unlocked(dapm);
2580 mutex_unlock(&dapm->card->dapm_mutex);
2581 return ret;
2582}
2583EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2584
2585static int dapm_update_dai_chan(struct snd_soc_dapm_path *p,
2586 struct snd_soc_dapm_widget *w,
2587 int channels)
2588{
2589 switch (w->id) {
2590 case snd_soc_dapm_aif_out:
2591 case snd_soc_dapm_aif_in:
2592 break;
2593 default:
2594 return 0;
2595 }
2596
2597 dev_dbg(w->dapm->dev, "%s DAI route %s -> %s\n",
2598 w->channel < channels ? "Connecting" : "Disconnecting",
2599 p->source->name, p->sink->name);
2600
2601 if (w->channel < channels)
2602 soc_dapm_connect_path(p, true, "dai update");
2603 else
2604 soc_dapm_connect_path(p, false, "dai update");
2605
2606 return 0;
2607}
2608
2609static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream,
2610 struct snd_pcm_hw_params *params,
2611 struct snd_soc_dai *dai)
2612{
2613 int dir = substream->stream;
2614 int channels = params_channels(params);
2615 struct snd_soc_dapm_path *p;
2616 struct snd_soc_dapm_widget *w;
2617 int ret;
2618
2619 if (dir == SNDRV_PCM_STREAM_PLAYBACK)
2620 w = dai->playback_widget;
2621 else
2622 w = dai->capture_widget;
2623
2624 if (!w)
2625 return 0;
2626
2627 dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name,
2628 dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
2629
2630 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2631 ret = dapm_update_dai_chan(p, p->sink, channels);
2632 if (ret < 0)
2633 return ret;
2634 }
2635
2636 snd_soc_dapm_widget_for_each_source_path(w, p) {
2637 ret = dapm_update_dai_chan(p, p->source, channels);
2638 if (ret < 0)
2639 return ret;
2640 }
2641
2642 return 0;
2643}
2644
2645int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream,
2646 struct snd_pcm_hw_params *params,
2647 struct snd_soc_dai *dai)
2648{
2649 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2650 int ret;
2651
2652 mutex_lock_nested(&rtd->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2653 ret = dapm_update_dai_unlocked(substream, params, dai);
2654 mutex_unlock(&rtd->card->dapm_mutex);
2655
2656 return ret;
2657}
2658EXPORT_SYMBOL_GPL(snd_soc_dapm_update_dai);
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2670{
2671 enum snd_soc_dapm_direction dir;
2672 struct snd_soc_dapm_path *p;
2673 unsigned int ep;
2674
2675 switch (w->id) {
2676 case snd_soc_dapm_input:
2677
2678 if (w->dapm->card->fully_routed)
2679 return;
2680 ep = SND_SOC_DAPM_EP_SOURCE;
2681 snd_soc_dapm_widget_for_each_source_path(w, p) {
2682 if (p->source->id == snd_soc_dapm_micbias ||
2683 p->source->id == snd_soc_dapm_mic ||
2684 p->source->id == snd_soc_dapm_line ||
2685 p->source->id == snd_soc_dapm_output) {
2686 ep = 0;
2687 break;
2688 }
2689 }
2690 break;
2691 case snd_soc_dapm_output:
2692
2693 if (w->dapm->card->fully_routed)
2694 return;
2695 ep = SND_SOC_DAPM_EP_SINK;
2696 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2697 if (p->sink->id == snd_soc_dapm_spk ||
2698 p->sink->id == snd_soc_dapm_hp ||
2699 p->sink->id == snd_soc_dapm_line ||
2700 p->sink->id == snd_soc_dapm_input) {
2701 ep = 0;
2702 break;
2703 }
2704 }
2705 break;
2706 case snd_soc_dapm_line:
2707 ep = 0;
2708 snd_soc_dapm_for_each_direction(dir) {
2709 if (!list_empty(&w->edges[dir]))
2710 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2711 }
2712 break;
2713 default:
2714 return;
2715 }
2716
2717 w->is_ep = ep;
2718}
2719
2720static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2721 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2722 const char *control)
2723{
2724 bool dynamic_source = false;
2725 bool dynamic_sink = false;
2726
2727 if (!control)
2728 return 0;
2729
2730 switch (source->id) {
2731 case snd_soc_dapm_demux:
2732 dynamic_source = true;
2733 break;
2734 default:
2735 break;
2736 }
2737
2738 switch (sink->id) {
2739 case snd_soc_dapm_mux:
2740 case snd_soc_dapm_switch:
2741 case snd_soc_dapm_mixer:
2742 case snd_soc_dapm_mixer_named_ctl:
2743 dynamic_sink = true;
2744 break;
2745 default:
2746 break;
2747 }
2748
2749 if (dynamic_source && dynamic_sink) {
2750 dev_err(dapm->dev,
2751 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2752 source->name, control, sink->name);
2753 return -EINVAL;
2754 } else if (!dynamic_source && !dynamic_sink) {
2755 dev_err(dapm->dev,
2756 "Control not supported for path %s -> [%s] -> %s\n",
2757 source->name, control, sink->name);
2758 return -EINVAL;
2759 }
2760
2761 return 0;
2762}
2763
2764static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2765 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2766 const char *control,
2767 int (*connected)(struct snd_soc_dapm_widget *source,
2768 struct snd_soc_dapm_widget *sink))
2769{
2770 struct snd_soc_dapm_widget *widgets[2];
2771 enum snd_soc_dapm_direction dir;
2772 struct snd_soc_dapm_path *path;
2773 int ret;
2774
2775 if (wsink->is_supply && !wsource->is_supply) {
2776 dev_err(dapm->dev,
2777 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2778 wsource->name, wsink->name);
2779 return -EINVAL;
2780 }
2781
2782 if (connected && !wsource->is_supply) {
2783 dev_err(dapm->dev,
2784 "connected() callback only supported for supply widgets (%s -> %s)\n",
2785 wsource->name, wsink->name);
2786 return -EINVAL;
2787 }
2788
2789 if (wsource->is_supply && control) {
2790 dev_err(dapm->dev,
2791 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2792 wsource->name, control, wsink->name);
2793 return -EINVAL;
2794 }
2795
2796 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2797 if (ret)
2798 return ret;
2799
2800 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2801 if (!path)
2802 return -ENOMEM;
2803
2804 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2805 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2806 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2807 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2808
2809 path->connected = connected;
2810 INIT_LIST_HEAD(&path->list);
2811 INIT_LIST_HEAD(&path->list_kcontrol);
2812
2813 if (wsource->is_supply || wsink->is_supply)
2814 path->is_supply = 1;
2815
2816
2817 if (control == NULL) {
2818 path->connect = 1;
2819 } else {
2820 switch (wsource->id) {
2821 case snd_soc_dapm_demux:
2822 ret = dapm_connect_mux(dapm, path, control, wsource);
2823 if (ret)
2824 goto err;
2825 break;
2826 default:
2827 break;
2828 }
2829
2830 switch (wsink->id) {
2831 case snd_soc_dapm_mux:
2832 ret = dapm_connect_mux(dapm, path, control, wsink);
2833 if (ret != 0)
2834 goto err;
2835 break;
2836 case snd_soc_dapm_switch:
2837 case snd_soc_dapm_mixer:
2838 case snd_soc_dapm_mixer_named_ctl:
2839 ret = dapm_connect_mixer(dapm, path, control);
2840 if (ret != 0)
2841 goto err;
2842 break;
2843 default:
2844 break;
2845 }
2846 }
2847
2848 list_add(&path->list, &dapm->card->paths);
2849 snd_soc_dapm_for_each_direction(dir)
2850 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
2851
2852 snd_soc_dapm_for_each_direction(dir) {
2853 dapm_update_widget_flags(widgets[dir]);
2854 dapm_mark_dirty(widgets[dir], "Route added");
2855 }
2856
2857 if (dapm->card->instantiated && path->connect)
2858 dapm_path_invalidate(path);
2859
2860 return 0;
2861err:
2862 kfree(path);
2863 return ret;
2864}
2865
2866static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2867 const struct snd_soc_dapm_route *route)
2868{
2869 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2870 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2871 const char *sink;
2872 const char *source;
2873 char prefixed_sink[80];
2874 char prefixed_source[80];
2875 const char *prefix;
2876 unsigned int sink_ref = 0;
2877 unsigned int source_ref = 0;
2878 int ret;
2879
2880 prefix = soc_dapm_prefix(dapm);
2881 if (prefix) {
2882 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2883 prefix, route->sink);
2884 sink = prefixed_sink;
2885 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2886 prefix, route->source);
2887 source = prefixed_source;
2888 } else {
2889 sink = route->sink;
2890 source = route->source;
2891 }
2892
2893 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2894 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2895
2896 if (wsink && wsource)
2897 goto skip_search;
2898
2899
2900
2901
2902
2903 list_for_each_entry(w, &dapm->card->widgets, list) {
2904 if (!wsink && !(strcmp(w->name, sink))) {
2905 wtsink = w;
2906 if (w->dapm == dapm) {
2907 wsink = w;
2908 if (wsource)
2909 break;
2910 }
2911 sink_ref++;
2912 if (sink_ref > 1)
2913 dev_warn(dapm->dev,
2914 "ASoC: sink widget %s overwritten\n",
2915 w->name);
2916 continue;
2917 }
2918 if (!wsource && !(strcmp(w->name, source))) {
2919 wtsource = w;
2920 if (w->dapm == dapm) {
2921 wsource = w;
2922 if (wsink)
2923 break;
2924 }
2925 source_ref++;
2926 if (source_ref > 1)
2927 dev_warn(dapm->dev,
2928 "ASoC: source widget %s overwritten\n",
2929 w->name);
2930 }
2931 }
2932
2933 if (!wsink)
2934 wsink = wtsink;
2935 if (!wsource)
2936 wsource = wtsource;
2937
2938 if (wsource == NULL) {
2939 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2940 route->source);
2941 return -ENODEV;
2942 }
2943 if (wsink == NULL) {
2944 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2945 route->sink);
2946 return -ENODEV;
2947 }
2948
2949skip_search:
2950 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2951 dapm_wcache_update(&dapm->path_source_cache, wsource);
2952
2953 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2954 route->connected);
2955 if (ret)
2956 goto err;
2957
2958 return 0;
2959err:
2960 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2961 source, route->control, sink);
2962 return ret;
2963}
2964
2965static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2966 const struct snd_soc_dapm_route *route)
2967{
2968 struct snd_soc_dapm_widget *wsource, *wsink;
2969 struct snd_soc_dapm_path *path, *p;
2970 const char *sink;
2971 const char *source;
2972 char prefixed_sink[80];
2973 char prefixed_source[80];
2974 const char *prefix;
2975
2976 if (route->control) {
2977 dev_err(dapm->dev,
2978 "ASoC: Removal of routes with controls not supported\n");
2979 return -EINVAL;
2980 }
2981
2982 prefix = soc_dapm_prefix(dapm);
2983 if (prefix) {
2984 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2985 prefix, route->sink);
2986 sink = prefixed_sink;
2987 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2988 prefix, route->source);
2989 source = prefixed_source;
2990 } else {
2991 sink = route->sink;
2992 source = route->source;
2993 }
2994
2995 path = NULL;
2996 list_for_each_entry(p, &dapm->card->paths, list) {
2997 if (strcmp(p->source->name, source) != 0)
2998 continue;
2999 if (strcmp(p->sink->name, sink) != 0)
3000 continue;
3001 path = p;
3002 break;
3003 }
3004
3005 if (path) {
3006 wsource = path->source;
3007 wsink = path->sink;
3008
3009 dapm_mark_dirty(wsource, "Route removed");
3010 dapm_mark_dirty(wsink, "Route removed");
3011 if (path->connect)
3012 dapm_path_invalidate(path);
3013
3014 dapm_free_path(path);
3015
3016
3017 dapm_update_widget_flags(wsource);
3018 dapm_update_widget_flags(wsink);
3019 } else {
3020 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
3021 source, sink);
3022 }
3023
3024 return 0;
3025}
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
3041 const struct snd_soc_dapm_route *route, int num)
3042{
3043 int i, r, ret = 0;
3044
3045 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3046 for (i = 0; i < num; i++) {
3047 r = snd_soc_dapm_add_route(dapm, route);
3048 if (r < 0) {
3049 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
3050 route->source,
3051 route->control ? route->control : "direct",
3052 route->sink);
3053 ret = r;
3054 }
3055 route++;
3056 }
3057 mutex_unlock(&dapm->card->dapm_mutex);
3058
3059 return ret;
3060}
3061EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
3072 const struct snd_soc_dapm_route *route, int num)
3073{
3074 int i;
3075
3076 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3077 for (i = 0; i < num; i++) {
3078 snd_soc_dapm_del_route(dapm, route);
3079 route++;
3080 }
3081 mutex_unlock(&dapm->card->dapm_mutex);
3082
3083 return 0;
3084}
3085EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
3086
3087static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
3088 const struct snd_soc_dapm_route *route)
3089{
3090 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3091 route->source,
3092 true);
3093 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3094 route->sink,
3095 true);
3096 struct snd_soc_dapm_path *path;
3097 int count = 0;
3098
3099 if (!source) {
3100 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
3101 route->source);
3102 return -ENODEV;
3103 }
3104
3105 if (!sink) {
3106 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
3107 route->sink);
3108 return -ENODEV;
3109 }
3110
3111 if (route->control || route->connected)
3112 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
3113 route->source, route->sink);
3114
3115 snd_soc_dapm_widget_for_each_sink_path(source, path) {
3116 if (path->sink == sink) {
3117 path->weak = 1;
3118 count++;
3119 }
3120 }
3121
3122 if (count == 0)
3123 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
3124 route->source, route->sink);
3125 if (count > 1)
3126 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
3127 count, route->source, route->sink);
3128
3129 return 0;
3130}
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3149 const struct snd_soc_dapm_route *route, int num)
3150{
3151 int i, err;
3152 int ret = 0;
3153
3154 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3155 for (i = 0; i < num; i++) {
3156 err = snd_soc_dapm_weak_route(dapm, route);
3157 if (err)
3158 ret = err;
3159 route++;
3160 }
3161 mutex_unlock(&dapm->card->dapm_mutex);
3162
3163 return ret;
3164}
3165EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3176{
3177 struct snd_soc_dapm_widget *w;
3178 unsigned int val;
3179
3180 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3181
3182 list_for_each_entry(w, &card->widgets, list)
3183 {
3184 if (w->new)
3185 continue;
3186
3187 if (w->num_kcontrols) {
3188 w->kcontrols = kcalloc(w->num_kcontrols,
3189 sizeof(struct snd_kcontrol *),
3190 GFP_KERNEL);
3191 if (!w->kcontrols) {
3192 mutex_unlock(&card->dapm_mutex);
3193 return -ENOMEM;
3194 }
3195 }
3196
3197 switch(w->id) {
3198 case snd_soc_dapm_switch:
3199 case snd_soc_dapm_mixer:
3200 case snd_soc_dapm_mixer_named_ctl:
3201 dapm_new_mixer(w);
3202 break;
3203 case snd_soc_dapm_mux:
3204 case snd_soc_dapm_demux:
3205 dapm_new_mux(w);
3206 break;
3207 case snd_soc_dapm_pga:
3208 case snd_soc_dapm_effect:
3209 case snd_soc_dapm_out_drv:
3210 dapm_new_pga(w);
3211 break;
3212 case snd_soc_dapm_dai_link:
3213 dapm_new_dai_link(w);
3214 break;
3215 default:
3216 break;
3217 }
3218
3219
3220 if (w->reg >= 0) {
3221 soc_dapm_read(w->dapm, w->reg, &val);
3222 val = val >> w->shift;
3223 val &= w->mask;
3224 if (val == w->on_val)
3225 w->power = 1;
3226 }
3227
3228 w->new = 1;
3229
3230 dapm_mark_dirty(w, "new widget");
3231 dapm_debugfs_add_widget(w);
3232 }
3233
3234 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3235 mutex_unlock(&card->dapm_mutex);
3236 return 0;
3237}
3238EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3250 struct snd_ctl_elem_value *ucontrol)
3251{
3252 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3253 struct snd_soc_card *card = dapm->card;
3254 struct soc_mixer_control *mc =
3255 (struct soc_mixer_control *)kcontrol->private_value;
3256 int reg = mc->reg;
3257 unsigned int shift = mc->shift;
3258 int max = mc->max;
3259 unsigned int width = fls(max);
3260 unsigned int mask = (1 << fls(max)) - 1;
3261 unsigned int invert = mc->invert;
3262 unsigned int reg_val, val, rval = 0;
3263 int ret = 0;
3264
3265 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3266 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3267 ret = soc_dapm_read(dapm, reg, ®_val);
3268 val = (reg_val >> shift) & mask;
3269
3270 if (ret == 0 && reg != mc->rreg)
3271 ret = soc_dapm_read(dapm, mc->rreg, ®_val);
3272
3273 if (snd_soc_volsw_is_stereo(mc))
3274 rval = (reg_val >> mc->rshift) & mask;
3275 } else {
3276 reg_val = dapm_kcontrol_get_value(kcontrol);
3277 val = reg_val & mask;
3278
3279 if (snd_soc_volsw_is_stereo(mc))
3280 rval = (reg_val >> width) & mask;
3281 }
3282 mutex_unlock(&card->dapm_mutex);
3283
3284 if (ret)
3285 return ret;
3286
3287 if (invert)
3288 ucontrol->value.integer.value[0] = max - val;
3289 else
3290 ucontrol->value.integer.value[0] = val;
3291
3292 if (snd_soc_volsw_is_stereo(mc)) {
3293 if (invert)
3294 ucontrol->value.integer.value[1] = max - rval;
3295 else
3296 ucontrol->value.integer.value[1] = rval;
3297 }
3298
3299 return ret;
3300}
3301EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3313 struct snd_ctl_elem_value *ucontrol)
3314{
3315 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3316 struct snd_soc_card *card = dapm->card;
3317 struct soc_mixer_control *mc =
3318 (struct soc_mixer_control *)kcontrol->private_value;
3319 int reg = mc->reg;
3320 unsigned int shift = mc->shift;
3321 int max = mc->max;
3322 unsigned int width = fls(max);
3323 unsigned int mask = (1 << width) - 1;
3324 unsigned int invert = mc->invert;
3325 unsigned int val, rval = 0;
3326 int connect, rconnect = -1, change, reg_change = 0;
3327 struct snd_soc_dapm_update update = {};
3328 int ret = 0;
3329
3330 val = (ucontrol->value.integer.value[0] & mask);
3331 connect = !!val;
3332
3333 if (invert)
3334 val = max - val;
3335
3336 if (snd_soc_volsw_is_stereo(mc)) {
3337 rval = (ucontrol->value.integer.value[1] & mask);
3338 rconnect = !!rval;
3339 if (invert)
3340 rval = max - rval;
3341 }
3342
3343 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3344
3345
3346 if (width > sizeof(unsigned int) * 8 / 2)
3347 dev_warn(dapm->dev,
3348 "ASoC: control %s field width limit exceeded\n",
3349 kcontrol->id.name);
3350 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3351
3352 if (reg != SND_SOC_NOPM) {
3353 val = val << shift;
3354 rval = rval << mc->rshift;
3355
3356 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3357
3358 if (snd_soc_volsw_is_stereo(mc))
3359 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3360 mask << mc->rshift,
3361 rval);
3362 }
3363
3364 if (change || reg_change) {
3365 if (reg_change) {
3366 if (snd_soc_volsw_is_stereo(mc)) {
3367 update.has_second_set = true;
3368 update.reg2 = mc->rreg;
3369 update.mask2 = mask << mc->rshift;
3370 update.val2 = rval;
3371 }
3372 update.kcontrol = kcontrol;
3373 update.reg = reg;
3374 update.mask = mask << shift;
3375 update.val = val;
3376 card->update = &update;
3377 }
3378 change |= reg_change;
3379
3380 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3381 rconnect);
3382
3383 card->update = NULL;
3384 }
3385
3386 mutex_unlock(&card->dapm_mutex);
3387
3388 if (ret > 0)
3389 soc_dpcm_runtime_update(card);
3390
3391 return change;
3392}
3393EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3405 struct snd_ctl_elem_value *ucontrol)
3406{
3407 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3408 struct snd_soc_card *card = dapm->card;
3409 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3410 unsigned int reg_val, val;
3411
3412 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3413 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3414 int ret = soc_dapm_read(dapm, e->reg, ®_val);
3415 if (ret) {
3416 mutex_unlock(&card->dapm_mutex);
3417 return ret;
3418 }
3419 } else {
3420 reg_val = dapm_kcontrol_get_value(kcontrol);
3421 }
3422 mutex_unlock(&card->dapm_mutex);
3423
3424 val = (reg_val >> e->shift_l) & e->mask;
3425 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3426 if (e->shift_l != e->shift_r) {
3427 val = (reg_val >> e->shift_r) & e->mask;
3428 val = snd_soc_enum_val_to_item(e, val);
3429 ucontrol->value.enumerated.item[1] = val;
3430 }
3431
3432 return 0;
3433}
3434EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3446 struct snd_ctl_elem_value *ucontrol)
3447{
3448 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3449 struct snd_soc_card *card = dapm->card;
3450 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3451 unsigned int *item = ucontrol->value.enumerated.item;
3452 unsigned int val, change, reg_change = 0;
3453 unsigned int mask;
3454 struct snd_soc_dapm_update update = {};
3455 int ret = 0;
3456
3457 if (item[0] >= e->items)
3458 return -EINVAL;
3459
3460 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3461 mask = e->mask << e->shift_l;
3462 if (e->shift_l != e->shift_r) {
3463 if (item[1] > e->items)
3464 return -EINVAL;
3465 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3466 mask |= e->mask << e->shift_r;
3467 }
3468
3469 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3470
3471 change = dapm_kcontrol_set_value(kcontrol, val);
3472
3473 if (e->reg != SND_SOC_NOPM)
3474 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3475
3476 if (change || reg_change) {
3477 if (reg_change) {
3478 update.kcontrol = kcontrol;
3479 update.reg = e->reg;
3480 update.mask = mask;
3481 update.val = val;
3482 card->update = &update;
3483 }
3484 change |= reg_change;
3485
3486 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3487
3488 card->update = NULL;
3489 }
3490
3491 mutex_unlock(&card->dapm_mutex);
3492
3493 if (ret > 0)
3494 soc_dpcm_runtime_update(card);
3495
3496 return change;
3497}
3498EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3509 struct snd_ctl_elem_info *uinfo)
3510{
3511 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3512 uinfo->count = 1;
3513 uinfo->value.integer.min = 0;
3514 uinfo->value.integer.max = 1;
3515
3516 return 0;
3517}
3518EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3519
3520
3521
3522
3523
3524
3525
3526int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3527 struct snd_ctl_elem_value *ucontrol)
3528{
3529 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3530 const char *pin = (const char *)kcontrol->private_value;
3531
3532 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3533
3534 ucontrol->value.integer.value[0] =
3535 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3536
3537 mutex_unlock(&card->dapm_mutex);
3538
3539 return 0;
3540}
3541EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3542
3543
3544
3545
3546
3547
3548
3549int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3550 struct snd_ctl_elem_value *ucontrol)
3551{
3552 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3553 const char *pin = (const char *)kcontrol->private_value;
3554
3555 if (ucontrol->value.integer.value[0])
3556 snd_soc_dapm_enable_pin(&card->dapm, pin);
3557 else
3558 snd_soc_dapm_disable_pin(&card->dapm, pin);
3559
3560 snd_soc_dapm_sync(&card->dapm);
3561 return 0;
3562}
3563EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3564
3565struct snd_soc_dapm_widget *
3566snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3567 const struct snd_soc_dapm_widget *widget)
3568{
3569 enum snd_soc_dapm_direction dir;
3570 struct snd_soc_dapm_widget *w;
3571 const char *prefix;
3572 int ret;
3573
3574 if ((w = dapm_cnew_widget(widget)) == NULL)
3575 return ERR_PTR(-ENOMEM);
3576
3577 switch (w->id) {
3578 case snd_soc_dapm_regulator_supply:
3579 w->regulator = devm_regulator_get(dapm->dev, w->name);
3580 if (IS_ERR(w->regulator)) {
3581 ret = PTR_ERR(w->regulator);
3582 goto request_failed;
3583 }
3584
3585 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3586 ret = regulator_allow_bypass(w->regulator, true);
3587 if (ret != 0)
3588 dev_warn(dapm->dev,
3589 "ASoC: Failed to bypass %s: %d\n",
3590 w->name, ret);
3591 }
3592 break;
3593 case snd_soc_dapm_pinctrl:
3594 w->pinctrl = devm_pinctrl_get(dapm->dev);
3595 if (IS_ERR(w->pinctrl)) {
3596 ret = PTR_ERR(w->pinctrl);
3597 goto request_failed;
3598 }
3599 break;
3600 case snd_soc_dapm_clock_supply:
3601 w->clk = devm_clk_get(dapm->dev, w->name);
3602 if (IS_ERR(w->clk)) {
3603 ret = PTR_ERR(w->clk);
3604 goto request_failed;
3605 }
3606 break;
3607 default:
3608 break;
3609 }
3610
3611 prefix = soc_dapm_prefix(dapm);
3612 if (prefix)
3613 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3614 else
3615 w->name = kstrdup_const(widget->name, GFP_KERNEL);
3616 if (w->name == NULL) {
3617 kfree_const(w->sname);
3618 kfree(w);
3619 return ERR_PTR(-ENOMEM);
3620 }
3621
3622 switch (w->id) {
3623 case snd_soc_dapm_mic:
3624 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3625 w->power_check = dapm_generic_check_power;
3626 break;
3627 case snd_soc_dapm_input:
3628 if (!dapm->card->fully_routed)
3629 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3630 w->power_check = dapm_generic_check_power;
3631 break;
3632 case snd_soc_dapm_spk:
3633 case snd_soc_dapm_hp:
3634 w->is_ep = SND_SOC_DAPM_EP_SINK;
3635 w->power_check = dapm_generic_check_power;
3636 break;
3637 case snd_soc_dapm_output:
3638 if (!dapm->card->fully_routed)
3639 w->is_ep = SND_SOC_DAPM_EP_SINK;
3640 w->power_check = dapm_generic_check_power;
3641 break;
3642 case snd_soc_dapm_vmid:
3643 case snd_soc_dapm_siggen:
3644 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3645 w->power_check = dapm_always_on_check_power;
3646 break;
3647 case snd_soc_dapm_sink:
3648 w->is_ep = SND_SOC_DAPM_EP_SINK;
3649 w->power_check = dapm_always_on_check_power;
3650 break;
3651
3652 case snd_soc_dapm_mux:
3653 case snd_soc_dapm_demux:
3654 case snd_soc_dapm_switch:
3655 case snd_soc_dapm_mixer:
3656 case snd_soc_dapm_mixer_named_ctl:
3657 case snd_soc_dapm_adc:
3658 case snd_soc_dapm_aif_out:
3659 case snd_soc_dapm_dac:
3660 case snd_soc_dapm_aif_in:
3661 case snd_soc_dapm_pga:
3662 case snd_soc_dapm_buffer:
3663 case snd_soc_dapm_scheduler:
3664 case snd_soc_dapm_effect:
3665 case snd_soc_dapm_src:
3666 case snd_soc_dapm_asrc:
3667 case snd_soc_dapm_encoder:
3668 case snd_soc_dapm_decoder:
3669 case snd_soc_dapm_out_drv:
3670 case snd_soc_dapm_micbias:
3671 case snd_soc_dapm_line:
3672 case snd_soc_dapm_dai_link:
3673 case snd_soc_dapm_dai_out:
3674 case snd_soc_dapm_dai_in:
3675 w->power_check = dapm_generic_check_power;
3676 break;
3677 case snd_soc_dapm_supply:
3678 case snd_soc_dapm_regulator_supply:
3679 case snd_soc_dapm_pinctrl:
3680 case snd_soc_dapm_clock_supply:
3681 case snd_soc_dapm_kcontrol:
3682 w->is_supply = 1;
3683 w->power_check = dapm_supply_check_power;
3684 break;
3685 default:
3686 w->power_check = dapm_always_on_check_power;
3687 break;
3688 }
3689
3690 w->dapm = dapm;
3691 INIT_LIST_HEAD(&w->list);
3692 INIT_LIST_HEAD(&w->dirty);
3693 list_add_tail(&w->list, &dapm->card->widgets);
3694
3695 snd_soc_dapm_for_each_direction(dir) {
3696 INIT_LIST_HEAD(&w->edges[dir]);
3697 w->endpoints[dir] = -1;
3698 }
3699
3700
3701 w->connected = 1;
3702 return w;
3703
3704request_failed:
3705 if (ret != -EPROBE_DEFER)
3706 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3707 w->name, ret);
3708
3709 kfree_const(w->sname);
3710 kfree(w);
3711 return ERR_PTR(ret);
3712}
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723struct snd_soc_dapm_widget *
3724snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3725 const struct snd_soc_dapm_widget *widget)
3726{
3727 struct snd_soc_dapm_widget *w;
3728
3729 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3730 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3731 mutex_unlock(&dapm->card->dapm_mutex);
3732
3733 return w;
3734}
3735EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3748 const struct snd_soc_dapm_widget *widget,
3749 int num)
3750{
3751 struct snd_soc_dapm_widget *w;
3752 int i;
3753 int ret = 0;
3754
3755 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3756 for (i = 0; i < num; i++) {
3757 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3758 if (IS_ERR(w)) {
3759 ret = PTR_ERR(w);
3760 break;
3761 }
3762 widget++;
3763 }
3764 mutex_unlock(&dapm->card->dapm_mutex);
3765 return ret;
3766}
3767EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3768
3769static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3770 struct snd_kcontrol *kcontrol, int event)
3771{
3772 struct snd_soc_dapm_path *path;
3773 struct snd_soc_dai *source, *sink;
3774 struct snd_soc_pcm_runtime *rtd = w->priv;
3775 const struct snd_soc_pcm_stream *config;
3776 struct snd_pcm_substream substream;
3777 struct snd_pcm_hw_params *params = NULL;
3778 struct snd_pcm_runtime *runtime = NULL;
3779 unsigned int fmt;
3780 int ret = 0;
3781
3782 config = rtd->dai_link->params + rtd->params_select;
3783
3784 if (WARN_ON(!config) ||
3785 WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3786 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3787 return -EINVAL;
3788
3789
3790 if (config->formats) {
3791 fmt = ffs(config->formats) - 1;
3792 } else {
3793 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3794 config->formats);
3795 fmt = 0;
3796 }
3797
3798
3799 params = kzalloc(sizeof(*params), GFP_KERNEL);
3800 if (!params) {
3801 ret = -ENOMEM;
3802 goto out;
3803 }
3804 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3805
3806 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3807 config->rate_min;
3808 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3809 config->rate_max;
3810
3811 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3812 = config->channels_min;
3813 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3814 = config->channels_max;
3815
3816 memset(&substream, 0, sizeof(substream));
3817
3818
3819 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3820 if (!runtime) {
3821 ret = -ENOMEM;
3822 goto out;
3823 }
3824 substream.runtime = runtime;
3825 substream.private_data = rtd;
3826
3827 switch (event) {
3828 case SND_SOC_DAPM_PRE_PMU:
3829 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3830 snd_soc_dapm_widget_for_each_source_path(w, path) {
3831 source = path->source->priv;
3832
3833 if (source->driver->ops->startup) {
3834 ret = source->driver->ops->startup(&substream,
3835 source);
3836 if (ret < 0) {
3837 dev_err(source->dev,
3838 "ASoC: startup() failed: %d\n",
3839 ret);
3840 goto out;
3841 }
3842 }
3843 source->active++;
3844 ret = soc_dai_hw_params(&substream, params, source);
3845 if (ret < 0)
3846 goto out;
3847
3848 dapm_update_dai_unlocked(&substream, params, source);
3849 }
3850
3851 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3852 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3853 sink = path->sink->priv;
3854
3855 if (sink->driver->ops->startup) {
3856 ret = sink->driver->ops->startup(&substream,
3857 sink);
3858 if (ret < 0) {
3859 dev_err(sink->dev,
3860 "ASoC: startup() failed: %d\n",
3861 ret);
3862 goto out;
3863 }
3864 }
3865 sink->active++;
3866 ret = soc_dai_hw_params(&substream, params, sink);
3867 if (ret < 0)
3868 goto out;
3869
3870 dapm_update_dai_unlocked(&substream, params, sink);
3871 }
3872 break;
3873
3874 case SND_SOC_DAPM_POST_PMU:
3875 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3876 sink = path->sink->priv;
3877
3878 ret = snd_soc_dai_digital_mute(sink, 0,
3879 SNDRV_PCM_STREAM_PLAYBACK);
3880 if (ret != 0 && ret != -ENOTSUPP)
3881 dev_warn(sink->dev,
3882 "ASoC: Failed to unmute: %d\n", ret);
3883 ret = 0;
3884 }
3885 break;
3886
3887 case SND_SOC_DAPM_PRE_PMD:
3888 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3889 sink = path->sink->priv;
3890
3891 ret = snd_soc_dai_digital_mute(sink, 1,
3892 SNDRV_PCM_STREAM_PLAYBACK);
3893 if (ret != 0 && ret != -ENOTSUPP)
3894 dev_warn(sink->dev,
3895 "ASoC: Failed to mute: %d\n", ret);
3896 ret = 0;
3897 }
3898
3899 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3900 snd_soc_dapm_widget_for_each_source_path(w, path) {
3901 source = path->source->priv;
3902
3903 if (source->driver->ops->hw_free)
3904 source->driver->ops->hw_free(&substream,
3905 source);
3906
3907 source->active--;
3908 if (source->driver->ops->shutdown)
3909 source->driver->ops->shutdown(&substream,
3910 source);
3911 }
3912
3913 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3914 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3915 sink = path->sink->priv;
3916
3917 if (sink->driver->ops->hw_free)
3918 sink->driver->ops->hw_free(&substream, sink);
3919
3920 sink->active--;
3921 if (sink->driver->ops->shutdown)
3922 sink->driver->ops->shutdown(&substream, sink);
3923 }
3924 break;
3925
3926 default:
3927 WARN(1, "Unknown event %d\n", event);
3928 ret = -EINVAL;
3929 }
3930
3931out:
3932 kfree(runtime);
3933 kfree(params);
3934 return ret;
3935}
3936
3937static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3938 struct snd_ctl_elem_value *ucontrol)
3939{
3940 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3941 struct snd_soc_pcm_runtime *rtd = w->priv;
3942
3943 ucontrol->value.enumerated.item[0] = rtd->params_select;
3944
3945 return 0;
3946}
3947
3948static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3949 struct snd_ctl_elem_value *ucontrol)
3950{
3951 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3952 struct snd_soc_pcm_runtime *rtd = w->priv;
3953
3954
3955 if (w->power)
3956 return -EBUSY;
3957
3958 if (ucontrol->value.enumerated.item[0] == rtd->params_select)
3959 return 0;
3960
3961 if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_params)
3962 return -EINVAL;
3963
3964 rtd->params_select = ucontrol->value.enumerated.item[0];
3965
3966 return 0;
3967}
3968
3969static void
3970snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
3971 unsigned long *private_value,
3972 int num_params,
3973 const char **w_param_text)
3974{
3975 int count;
3976
3977 devm_kfree(card->dev, (void *)*private_value);
3978
3979 if (!w_param_text)
3980 return;
3981
3982 for (count = 0 ; count < num_params; count++)
3983 devm_kfree(card->dev, (void *)w_param_text[count]);
3984 devm_kfree(card->dev, w_param_text);
3985}
3986
3987static struct snd_kcontrol_new *
3988snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
3989 char *link_name,
3990 const struct snd_soc_pcm_stream *params,
3991 int num_params, const char **w_param_text,
3992 unsigned long *private_value)
3993{
3994 struct soc_enum w_param_enum[] = {
3995 SOC_ENUM_SINGLE(0, 0, 0, NULL),
3996 };
3997 struct snd_kcontrol_new kcontrol_dai_link[] = {
3998 SOC_ENUM_EXT(NULL, w_param_enum[0],
3999 snd_soc_dapm_dai_link_get,
4000 snd_soc_dapm_dai_link_put),
4001 };
4002 struct snd_kcontrol_new *kcontrol_news;
4003 const struct snd_soc_pcm_stream *config = params;
4004 int count;
4005
4006 for (count = 0 ; count < num_params; count++) {
4007 if (!config->stream_name) {
4008 dev_warn(card->dapm.dev,
4009 "ASoC: anonymous config %d for dai link %s\n",
4010 count, link_name);
4011 w_param_text[count] =
4012 devm_kasprintf(card->dev, GFP_KERNEL,
4013 "Anonymous Configuration %d",
4014 count);
4015 } else {
4016 w_param_text[count] = devm_kmemdup(card->dev,
4017 config->stream_name,
4018 strlen(config->stream_name) + 1,
4019 GFP_KERNEL);
4020 }
4021 if (!w_param_text[count])
4022 goto outfree_w_param;
4023 config++;
4024 }
4025
4026 w_param_enum[0].items = num_params;
4027 w_param_enum[0].texts = w_param_text;
4028
4029 *private_value =
4030 (unsigned long) devm_kmemdup(card->dev,
4031 (void *)(kcontrol_dai_link[0].private_value),
4032 sizeof(struct soc_enum), GFP_KERNEL);
4033 if (!*private_value) {
4034 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4035 link_name);
4036 goto outfree_w_param;
4037 }
4038 kcontrol_dai_link[0].private_value = *private_value;
4039
4040 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
4041 sizeof(struct snd_kcontrol_new),
4042 GFP_KERNEL);
4043 if (!kcontrol_news) {
4044 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4045 link_name);
4046 goto outfree_w_param;
4047 }
4048 return kcontrol_news;
4049
4050outfree_w_param:
4051 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
4052 return NULL;
4053}
4054
4055static struct snd_soc_dapm_widget *
4056snd_soc_dapm_new_dai(struct snd_soc_card *card, struct snd_soc_pcm_runtime *rtd,
4057 struct snd_soc_dapm_widget *source,
4058 struct snd_soc_dapm_widget *sink)
4059{
4060 struct snd_soc_dapm_widget template;
4061 struct snd_soc_dapm_widget *w;
4062 const char **w_param_text;
4063 unsigned long private_value = 0;
4064 char *link_name;
4065 int ret;
4066
4067 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
4068 source->name, sink->name);
4069 if (!link_name)
4070 return ERR_PTR(-ENOMEM);
4071
4072 memset(&template, 0, sizeof(template));
4073 template.reg = SND_SOC_NOPM;
4074 template.id = snd_soc_dapm_dai_link;
4075 template.name = link_name;
4076 template.event = snd_soc_dai_link_event;
4077 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
4078 SND_SOC_DAPM_PRE_PMD;
4079 template.kcontrol_news = NULL;
4080
4081
4082 if (rtd->dai_link->num_params > 1) {
4083 w_param_text = devm_kcalloc(card->dev,
4084 rtd->dai_link->num_params,
4085 sizeof(char *), GFP_KERNEL);
4086 if (!w_param_text) {
4087 ret = -ENOMEM;
4088 goto param_fail;
4089 }
4090
4091 template.num_kcontrols = 1;
4092 template.kcontrol_news =
4093 snd_soc_dapm_alloc_kcontrol(card,
4094 link_name,
4095 rtd->dai_link->params,
4096 rtd->dai_link->num_params,
4097 w_param_text, &private_value);
4098 if (!template.kcontrol_news) {
4099 ret = -ENOMEM;
4100 goto param_fail;
4101 }
4102 } else {
4103 w_param_text = NULL;
4104 }
4105 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
4106
4107 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
4108 if (IS_ERR(w)) {
4109 ret = PTR_ERR(w);
4110 goto outfree_kcontrol_news;
4111 }
4112
4113 w->priv = rtd;
4114
4115 return w;
4116
4117outfree_kcontrol_news:
4118 devm_kfree(card->dev, (void *)template.kcontrol_news);
4119 snd_soc_dapm_free_kcontrol(card, &private_value,
4120 rtd->dai_link->num_params, w_param_text);
4121param_fail:
4122 devm_kfree(card->dev, link_name);
4123 return ERR_PTR(ret);
4124}
4125
4126int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
4127 struct snd_soc_dai *dai)
4128{
4129 struct snd_soc_dapm_widget template;
4130 struct snd_soc_dapm_widget *w;
4131
4132 WARN_ON(dapm->dev != dai->dev);
4133
4134 memset(&template, 0, sizeof(template));
4135 template.reg = SND_SOC_NOPM;
4136
4137 if (dai->driver->playback.stream_name) {
4138 template.id = snd_soc_dapm_dai_in;
4139 template.name = dai->driver->playback.stream_name;
4140 template.sname = dai->driver->playback.stream_name;
4141
4142 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4143 template.name);
4144
4145 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4146 if (IS_ERR(w))
4147 return PTR_ERR(w);
4148
4149 w->priv = dai;
4150 dai->playback_widget = w;
4151 }
4152
4153 if (dai->driver->capture.stream_name) {
4154 template.id = snd_soc_dapm_dai_out;
4155 template.name = dai->driver->capture.stream_name;
4156 template.sname = dai->driver->capture.stream_name;
4157
4158 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4159 template.name);
4160
4161 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4162 if (IS_ERR(w))
4163 return PTR_ERR(w);
4164
4165 w->priv = dai;
4166 dai->capture_widget = w;
4167 }
4168
4169 return 0;
4170}
4171
4172int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4173{
4174 struct snd_soc_dapm_widget *dai_w, *w;
4175 struct snd_soc_dapm_widget *src, *sink;
4176 struct snd_soc_dai *dai;
4177
4178
4179 list_for_each_entry(dai_w, &card->widgets, list) {
4180 switch (dai_w->id) {
4181 case snd_soc_dapm_dai_in:
4182 case snd_soc_dapm_dai_out:
4183 break;
4184 default:
4185 continue;
4186 }
4187
4188
4189 if (!dai_w->priv) {
4190 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4191 dai_w->name);
4192 continue;
4193 }
4194
4195 dai = dai_w->priv;
4196
4197
4198 list_for_each_entry(w, &card->widgets, list) {
4199 if (w->dapm != dai_w->dapm)
4200 continue;
4201
4202 switch (w->id) {
4203 case snd_soc_dapm_dai_in:
4204 case snd_soc_dapm_dai_out:
4205 continue;
4206 default:
4207 break;
4208 }
4209
4210 if (!w->sname || !strstr(w->sname, dai_w->sname))
4211 continue;
4212
4213 if (dai_w->id == snd_soc_dapm_dai_in) {
4214 src = dai_w;
4215 sink = w;
4216 } else {
4217 src = w;
4218 sink = dai_w;
4219 }
4220 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4221 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4222 }
4223 }
4224
4225 return 0;
4226}
4227
4228static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
4229 struct snd_soc_pcm_runtime *rtd)
4230{
4231 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
4232 struct snd_soc_dai *codec_dai;
4233 struct snd_soc_dapm_widget *playback = NULL, *capture = NULL;
4234 struct snd_soc_dapm_widget *codec, *playback_cpu, *capture_cpu;
4235 int i;
4236
4237 if (rtd->dai_link->params) {
4238 playback_cpu = cpu_dai->capture_widget;
4239 capture_cpu = cpu_dai->playback_widget;
4240 } else {
4241 playback = cpu_dai->playback_widget;
4242 capture = cpu_dai->capture_widget;
4243 playback_cpu = playback;
4244 capture_cpu = capture;
4245 }
4246
4247 for_each_rtd_codec_dai(rtd, i, codec_dai) {
4248
4249
4250 codec = codec_dai->playback_widget;
4251
4252 if (playback_cpu && codec) {
4253 if (!playback) {
4254 playback = snd_soc_dapm_new_dai(card, rtd,
4255 playback_cpu,
4256 codec);
4257 if (IS_ERR(playback)) {
4258 dev_err(rtd->dev,
4259 "ASoC: Failed to create DAI %s: %ld\n",
4260 codec_dai->name,
4261 PTR_ERR(playback));
4262 continue;
4263 }
4264
4265 snd_soc_dapm_add_path(&card->dapm, playback_cpu,
4266 playback, NULL, NULL);
4267 }
4268
4269 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4270 cpu_dai->component->name, playback_cpu->name,
4271 codec_dai->component->name, codec->name);
4272
4273 snd_soc_dapm_add_path(&card->dapm, playback, codec,
4274 NULL, NULL);
4275 }
4276 }
4277
4278 for_each_rtd_codec_dai(rtd, i, codec_dai) {
4279
4280 codec = codec_dai->capture_widget;
4281
4282 if (codec && capture_cpu) {
4283 if (!capture) {
4284 capture = snd_soc_dapm_new_dai(card, rtd,
4285 codec,
4286 capture_cpu);
4287 if (IS_ERR(capture)) {
4288 dev_err(rtd->dev,
4289 "ASoC: Failed to create DAI %s: %ld\n",
4290 codec_dai->name,
4291 PTR_ERR(capture));
4292 continue;
4293 }
4294
4295 snd_soc_dapm_add_path(&card->dapm, capture,
4296 capture_cpu, NULL, NULL);
4297 }
4298
4299 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4300 codec_dai->component->name, codec->name,
4301 cpu_dai->component->name, capture_cpu->name);
4302
4303 snd_soc_dapm_add_path(&card->dapm, codec, capture,
4304 NULL, NULL);
4305 }
4306 }
4307}
4308
4309static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4310 int event)
4311{
4312 struct snd_soc_dapm_widget *w;
4313 unsigned int ep;
4314
4315 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
4316 w = dai->playback_widget;
4317 else
4318 w = dai->capture_widget;
4319
4320 if (w) {
4321 dapm_mark_dirty(w, "stream event");
4322
4323 if (w->id == snd_soc_dapm_dai_in) {
4324 ep = SND_SOC_DAPM_EP_SOURCE;
4325 dapm_widget_invalidate_input_paths(w);
4326 } else {
4327 ep = SND_SOC_DAPM_EP_SINK;
4328 dapm_widget_invalidate_output_paths(w);
4329 }
4330
4331 switch (event) {
4332 case SND_SOC_DAPM_STREAM_START:
4333 w->active = 1;
4334 w->is_ep = ep;
4335 break;
4336 case SND_SOC_DAPM_STREAM_STOP:
4337 w->active = 0;
4338 w->is_ep = 0;
4339 break;
4340 case SND_SOC_DAPM_STREAM_SUSPEND:
4341 case SND_SOC_DAPM_STREAM_RESUME:
4342 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4343 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4344 break;
4345 }
4346 }
4347}
4348
4349void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4350{
4351 struct snd_soc_pcm_runtime *rtd;
4352
4353
4354 for_each_card_rtds(card, rtd) {
4355
4356
4357
4358
4359 if (rtd->dai_link->dynamic)
4360 continue;
4361
4362 dapm_connect_dai_link_widgets(card, rtd);
4363 }
4364}
4365
4366static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4367 int event)
4368{
4369 struct snd_soc_dai *codec_dai;
4370 int i;
4371
4372 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
4373 for_each_rtd_codec_dai(rtd, i, codec_dai)
4374 soc_dapm_dai_stream_event(codec_dai, stream, event);
4375
4376 dapm_power_widgets(rtd->card, event);
4377}
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4391 int event)
4392{
4393 struct snd_soc_card *card = rtd->card;
4394
4395 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4396 soc_dapm_stream_event(rtd, stream, event);
4397 mutex_unlock(&card->dapm_mutex);
4398}
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4414 const char *pin)
4415{
4416 return snd_soc_dapm_set_pin(dapm, pin, 1);
4417}
4418EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4432{
4433 int ret;
4434
4435 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4436
4437 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4438
4439 mutex_unlock(&dapm->card->dapm_mutex);
4440
4441 return ret;
4442}
4443EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4460 const char *pin)
4461{
4462 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4463
4464 if (!w) {
4465 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4466 return -EINVAL;
4467 }
4468
4469 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4470 if (!w->connected) {
4471
4472
4473
4474
4475 dapm_widget_invalidate_input_paths(w);
4476 dapm_widget_invalidate_output_paths(w);
4477 w->connected = 1;
4478 }
4479 w->force = 1;
4480 dapm_mark_dirty(w, "force enable");
4481
4482 return 0;
4483}
4484EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4499 const char *pin)
4500{
4501 int ret;
4502
4503 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4504
4505 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4506
4507 mutex_unlock(&dapm->card->dapm_mutex);
4508
4509 return ret;
4510}
4511EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4526 const char *pin)
4527{
4528 return snd_soc_dapm_set_pin(dapm, pin, 0);
4529}
4530EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4543 const char *pin)
4544{
4545 int ret;
4546
4547 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4548
4549 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4550
4551 mutex_unlock(&dapm->card->dapm_mutex);
4552
4553 return ret;
4554}
4555EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4574 const char *pin)
4575{
4576 return snd_soc_dapm_set_pin(dapm, pin, 0);
4577}
4578EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4595{
4596 int ret;
4597
4598 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4599
4600 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4601
4602 mutex_unlock(&dapm->card->dapm_mutex);
4603
4604 return ret;
4605}
4606EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4618 const char *pin)
4619{
4620 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4621
4622 if (w)
4623 return w->connected;
4624
4625 return 0;
4626}
4627EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4641 const char *pin)
4642{
4643 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4644
4645 if (!w) {
4646 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4647 return -EINVAL;
4648 }
4649
4650 w->ignore_suspend = 1;
4651
4652 return 0;
4653}
4654EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4655
4656
4657
4658
4659
4660
4661
4662void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4663{
4664 dapm_debugfs_cleanup(dapm);
4665 dapm_free_widgets(dapm);
4666 list_del(&dapm->list);
4667}
4668EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4669
4670static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4671{
4672 struct snd_soc_card *card = dapm->card;
4673 struct snd_soc_dapm_widget *w;
4674 LIST_HEAD(down_list);
4675 int powerdown = 0;
4676
4677 mutex_lock(&card->dapm_mutex);
4678
4679 list_for_each_entry(w, &dapm->card->widgets, list) {
4680 if (w->dapm != dapm)
4681 continue;
4682 if (w->power) {
4683 dapm_seq_insert(w, &down_list, false);
4684 w->power = 0;
4685 powerdown = 1;
4686 }
4687 }
4688
4689
4690
4691
4692 if (powerdown) {
4693 if (dapm->bias_level == SND_SOC_BIAS_ON)
4694 snd_soc_dapm_set_bias_level(dapm,
4695 SND_SOC_BIAS_PREPARE);
4696 dapm_seq_run(card, &down_list, 0, false);
4697 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4698 snd_soc_dapm_set_bias_level(dapm,
4699 SND_SOC_BIAS_STANDBY);
4700 }
4701
4702 mutex_unlock(&card->dapm_mutex);
4703}
4704
4705
4706
4707
4708void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4709{
4710 struct snd_soc_dapm_context *dapm;
4711
4712 list_for_each_entry(dapm, &card->dapm_list, list) {
4713 if (dapm != &card->dapm) {
4714 soc_dapm_shutdown_dapm(dapm);
4715 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4716 snd_soc_dapm_set_bias_level(dapm,
4717 SND_SOC_BIAS_OFF);
4718 }
4719 }
4720
4721 soc_dapm_shutdown_dapm(&card->dapm);
4722 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4723 snd_soc_dapm_set_bias_level(&card->dapm,
4724 SND_SOC_BIAS_OFF);
4725}
4726
4727
4728MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4729MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4730MODULE_LICENSE("GPL");
4731