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