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