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