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