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