Merge tag 'topic/devm' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/regul...
[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/meadphone 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 powerdown of audio susbsystem to reduce pops between a quick
22 * device reopen.
23 *
24 * Todo:
25 * o DAPM power change sequencing - allow for configurable per
26 * codec sequences.
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
30 */
31
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/init.h>
35 #include <linux/async.h>
36 #include <linux/delay.h>
37 #include <linux/pm.h>
38 #include <linux/bitops.h>
39 #include <linux/platform_device.h>
40 #include <linux/jiffies.h>
41 #include <linux/debugfs.h>
42 #include <linux/pm_runtime.h>
43 #include <linux/slab.h>
44 #include <sound/core.h>
45 #include <sound/pcm.h>
46 #include <sound/pcm_params.h>
47 #include <sound/soc.h>
48 #include <sound/initval.h>
49
50 #include <trace/events/asoc.h>
51
52 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
53
54 /* dapm power sequences - make this per codec in the future */
55 static int dapm_up_seq[] = {
56 [snd_soc_dapm_pre] = 0,
57 [snd_soc_dapm_supply] = 1,
58 [snd_soc_dapm_micbias] = 2,
59 [snd_soc_dapm_aif_in] = 3,
60 [snd_soc_dapm_aif_out] = 3,
61 [snd_soc_dapm_mic] = 4,
62 [snd_soc_dapm_mux] = 5,
63 [snd_soc_dapm_virt_mux] = 5,
64 [snd_soc_dapm_value_mux] = 5,
65 [snd_soc_dapm_dac] = 6,
66 [snd_soc_dapm_mixer] = 7,
67 [snd_soc_dapm_mixer_named_ctl] = 7,
68 [snd_soc_dapm_pga] = 8,
69 [snd_soc_dapm_adc] = 9,
70 [snd_soc_dapm_out_drv] = 10,
71 [snd_soc_dapm_hp] = 10,
72 [snd_soc_dapm_spk] = 10,
73 [snd_soc_dapm_post] = 11,
74 };
75
76 static int dapm_down_seq[] = {
77 [snd_soc_dapm_pre] = 0,
78 [snd_soc_dapm_adc] = 1,
79 [snd_soc_dapm_hp] = 2,
80 [snd_soc_dapm_spk] = 2,
81 [snd_soc_dapm_out_drv] = 2,
82 [snd_soc_dapm_pga] = 4,
83 [snd_soc_dapm_mixer_named_ctl] = 5,
84 [snd_soc_dapm_mixer] = 5,
85 [snd_soc_dapm_dac] = 6,
86 [snd_soc_dapm_mic] = 7,
87 [snd_soc_dapm_micbias] = 8,
88 [snd_soc_dapm_mux] = 9,
89 [snd_soc_dapm_virt_mux] = 9,
90 [snd_soc_dapm_value_mux] = 9,
91 [snd_soc_dapm_aif_in] = 10,
92 [snd_soc_dapm_aif_out] = 10,
93 [snd_soc_dapm_supply] = 11,
94 [snd_soc_dapm_post] = 12,
95 };
96
97 static void pop_wait(u32 pop_time)
98 {
99 if (pop_time)
100 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
101 }
102
103 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
104 {
105 va_list args;
106 char *buf;
107
108 if (!pop_time)
109 return;
110
111 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
112 if (buf == NULL)
113 return;
114
115 va_start(args, fmt);
116 vsnprintf(buf, PAGE_SIZE, fmt, args);
117 dev_info(dev, "%s", buf);
118 va_end(args);
119
120 kfree(buf);
121 }
122
123 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
124 {
125 return !list_empty(&w->dirty);
126 }
127
128 void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
129 {
130 if (!dapm_dirty_widget(w)) {
131 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
132 w->name, reason);
133 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
134 }
135 }
136 EXPORT_SYMBOL_GPL(dapm_mark_dirty);
137
138 /* create a new dapm widget */
139 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
140 const struct snd_soc_dapm_widget *_widget)
141 {
142 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
143 }
144
145 /* get snd_card from DAPM context */
146 static inline struct snd_card *dapm_get_snd_card(
147 struct snd_soc_dapm_context *dapm)
148 {
149 if (dapm->codec)
150 return dapm->codec->card->snd_card;
151 else if (dapm->platform)
152 return dapm->platform->card->snd_card;
153 else
154 BUG();
155
156 /* unreachable */
157 return NULL;
158 }
159
160 /* get soc_card from DAPM context */
161 static inline struct snd_soc_card *dapm_get_soc_card(
162 struct snd_soc_dapm_context *dapm)
163 {
164 if (dapm->codec)
165 return dapm->codec->card;
166 else if (dapm->platform)
167 return dapm->platform->card;
168 else
169 BUG();
170
171 /* unreachable */
172 return NULL;
173 }
174
175 static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
176 {
177 if (w->codec)
178 return snd_soc_read(w->codec, reg);
179 else if (w->platform)
180 return snd_soc_platform_read(w->platform, reg);
181
182 dev_err(w->dapm->dev, "no valid widget read method\n");
183 return -1;
184 }
185
186 static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
187 {
188 if (w->codec)
189 return snd_soc_write(w->codec, reg, val);
190 else if (w->platform)
191 return snd_soc_platform_write(w->platform, reg, val);
192
193 dev_err(w->dapm->dev, "no valid widget write method\n");
194 return -1;
195 }
196
197 static int soc_widget_update_bits(struct snd_soc_dapm_widget *w,
198 unsigned short reg, unsigned int mask, unsigned int value)
199 {
200 bool change;
201 unsigned int old, new;
202 int ret;
203
204 if (w->codec && w->codec->using_regmap) {
205 ret = regmap_update_bits_check(w->codec->control_data,
206 reg, mask, value, &change);
207 if (ret != 0)
208 return ret;
209 } else {
210 ret = soc_widget_read(w, reg);
211 if (ret < 0)
212 return ret;
213
214 old = ret;
215 new = (old & ~mask) | (value & mask);
216 change = old != new;
217 if (change) {
218 ret = soc_widget_write(w, reg, new);
219 if (ret < 0)
220 return ret;
221 }
222 }
223
224 return change;
225 }
226
227 /**
228 * snd_soc_dapm_set_bias_level - set the bias level for the system
229 * @dapm: DAPM context
230 * @level: level to configure
231 *
232 * Configure the bias (power) levels for the SoC audio device.
233 *
234 * Returns 0 for success else error.
235 */
236 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
237 enum snd_soc_bias_level level)
238 {
239 struct snd_soc_card *card = dapm->card;
240 int ret = 0;
241
242 trace_snd_soc_bias_level_start(card, level);
243
244 if (card && card->set_bias_level)
245 ret = card->set_bias_level(card, dapm, level);
246 if (ret != 0)
247 goto out;
248
249 if (dapm->codec) {
250 if (dapm->codec->driver->set_bias_level)
251 ret = dapm->codec->driver->set_bias_level(dapm->codec,
252 level);
253 else
254 dapm->bias_level = level;
255 }
256 if (ret != 0)
257 goto out;
258
259 if (card && card->set_bias_level_post)
260 ret = card->set_bias_level_post(card, dapm, level);
261 out:
262 trace_snd_soc_bias_level_done(card, level);
263
264 return ret;
265 }
266
267 /* set up initial codec paths */
268 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
269 struct snd_soc_dapm_path *p, int i)
270 {
271 switch (w->id) {
272 case snd_soc_dapm_switch:
273 case snd_soc_dapm_mixer:
274 case snd_soc_dapm_mixer_named_ctl: {
275 int val;
276 struct soc_mixer_control *mc = (struct soc_mixer_control *)
277 w->kcontrol_news[i].private_value;
278 unsigned int reg = mc->reg;
279 unsigned int shift = mc->shift;
280 int max = mc->max;
281 unsigned int mask = (1 << fls(max)) - 1;
282 unsigned int invert = mc->invert;
283
284 val = soc_widget_read(w, reg);
285 val = (val >> shift) & mask;
286
287 if ((invert && !val) || (!invert && val))
288 p->connect = 1;
289 else
290 p->connect = 0;
291 }
292 break;
293 case snd_soc_dapm_mux: {
294 struct soc_enum *e = (struct soc_enum *)
295 w->kcontrol_news[i].private_value;
296 int val, item, bitmask;
297
298 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
299 ;
300 val = soc_widget_read(w, e->reg);
301 item = (val >> e->shift_l) & (bitmask - 1);
302
303 p->connect = 0;
304 for (i = 0; i < e->max; i++) {
305 if (!(strcmp(p->name, e->texts[i])) && item == i)
306 p->connect = 1;
307 }
308 }
309 break;
310 case snd_soc_dapm_virt_mux: {
311 struct soc_enum *e = (struct soc_enum *)
312 w->kcontrol_news[i].private_value;
313
314 p->connect = 0;
315 /* since a virtual mux has no backing registers to
316 * decide which path to connect, it will try to match
317 * with the first enumeration. This is to ensure
318 * that the default mux choice (the first) will be
319 * correctly powered up during initialization.
320 */
321 if (!strcmp(p->name, e->texts[0]))
322 p->connect = 1;
323 }
324 break;
325 case snd_soc_dapm_value_mux: {
326 struct soc_enum *e = (struct soc_enum *)
327 w->kcontrol_news[i].private_value;
328 int val, item;
329
330 val = soc_widget_read(w, e->reg);
331 val = (val >> e->shift_l) & e->mask;
332 for (item = 0; item < e->max; item++) {
333 if (val == e->values[item])
334 break;
335 }
336
337 p->connect = 0;
338 for (i = 0; i < e->max; i++) {
339 if (!(strcmp(p->name, e->texts[i])) && item == i)
340 p->connect = 1;
341 }
342 }
343 break;
344 /* does not affect routing - always connected */
345 case snd_soc_dapm_pga:
346 case snd_soc_dapm_out_drv:
347 case snd_soc_dapm_output:
348 case snd_soc_dapm_adc:
349 case snd_soc_dapm_input:
350 case snd_soc_dapm_siggen:
351 case snd_soc_dapm_dac:
352 case snd_soc_dapm_micbias:
353 case snd_soc_dapm_vmid:
354 case snd_soc_dapm_supply:
355 case snd_soc_dapm_aif_in:
356 case snd_soc_dapm_aif_out:
357 case snd_soc_dapm_hp:
358 case snd_soc_dapm_mic:
359 case snd_soc_dapm_spk:
360 case snd_soc_dapm_line:
361 p->connect = 1;
362 break;
363 /* does affect routing - dynamically connected */
364 case snd_soc_dapm_pre:
365 case snd_soc_dapm_post:
366 p->connect = 0;
367 break;
368 }
369 }
370
371 /* connect mux widget to its interconnecting audio paths */
372 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
373 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
374 struct snd_soc_dapm_path *path, const char *control_name,
375 const struct snd_kcontrol_new *kcontrol)
376 {
377 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
378 int i;
379
380 for (i = 0; i < e->max; i++) {
381 if (!(strcmp(control_name, e->texts[i]))) {
382 list_add(&path->list, &dapm->card->paths);
383 list_add(&path->list_sink, &dest->sources);
384 list_add(&path->list_source, &src->sinks);
385 path->name = (char*)e->texts[i];
386 dapm_set_path_status(dest, path, 0);
387 return 0;
388 }
389 }
390
391 return -ENODEV;
392 }
393
394 /* connect mixer widget to its interconnecting audio paths */
395 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
396 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
397 struct snd_soc_dapm_path *path, const char *control_name)
398 {
399 int i;
400
401 /* search for mixer kcontrol */
402 for (i = 0; i < dest->num_kcontrols; i++) {
403 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
404 list_add(&path->list, &dapm->card->paths);
405 list_add(&path->list_sink, &dest->sources);
406 list_add(&path->list_source, &src->sinks);
407 path->name = dest->kcontrol_news[i].name;
408 dapm_set_path_status(dest, path, i);
409 return 0;
410 }
411 }
412 return -ENODEV;
413 }
414
415 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
416 struct snd_soc_dapm_widget *kcontrolw,
417 const struct snd_kcontrol_new *kcontrol_new,
418 struct snd_kcontrol **kcontrol)
419 {
420 struct snd_soc_dapm_widget *w;
421 int i;
422
423 *kcontrol = NULL;
424
425 list_for_each_entry(w, &dapm->card->widgets, list) {
426 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
427 continue;
428 for (i = 0; i < w->num_kcontrols; i++) {
429 if (&w->kcontrol_news[i] == kcontrol_new) {
430 if (w->kcontrols)
431 *kcontrol = w->kcontrols[i];
432 return 1;
433 }
434 }
435 }
436
437 return 0;
438 }
439
440 /* create new dapm mixer control */
441 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
442 {
443 struct snd_soc_dapm_context *dapm = w->dapm;
444 int i, ret = 0;
445 size_t name_len, prefix_len;
446 struct snd_soc_dapm_path *path;
447 struct snd_card *card = dapm->card->snd_card;
448 const char *prefix;
449 struct snd_soc_dapm_widget_list *wlist;
450 size_t wlistsize;
451
452 if (dapm->codec)
453 prefix = dapm->codec->name_prefix;
454 else
455 prefix = NULL;
456
457 if (prefix)
458 prefix_len = strlen(prefix) + 1;
459 else
460 prefix_len = 0;
461
462 /* add kcontrol */
463 for (i = 0; i < w->num_kcontrols; i++) {
464
465 /* match name */
466 list_for_each_entry(path, &w->sources, list_sink) {
467
468 /* mixer/mux paths name must match control name */
469 if (path->name != (char *)w->kcontrol_news[i].name)
470 continue;
471
472 if (w->kcontrols[i]) {
473 path->kcontrol = w->kcontrols[i];
474 continue;
475 }
476
477 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
478 sizeof(struct snd_soc_dapm_widget *),
479 wlist = kzalloc(wlistsize, GFP_KERNEL);
480 if (wlist == NULL) {
481 dev_err(dapm->dev,
482 "asoc: can't allocate widget list for %s\n",
483 w->name);
484 return -ENOMEM;
485 }
486 wlist->num_widgets = 1;
487 wlist->widgets[0] = w;
488
489 /* add dapm control with long name.
490 * for dapm_mixer this is the concatenation of the
491 * mixer and kcontrol name.
492 * for dapm_mixer_named_ctl this is simply the
493 * kcontrol name.
494 */
495 name_len = strlen(w->kcontrol_news[i].name) + 1;
496 if (w->id != snd_soc_dapm_mixer_named_ctl)
497 name_len += 1 + strlen(w->name);
498
499 path->long_name = kmalloc(name_len, GFP_KERNEL);
500
501 if (path->long_name == NULL) {
502 kfree(wlist);
503 return -ENOMEM;
504 }
505
506 switch (w->id) {
507 default:
508 /* The control will get a prefix from
509 * the control creation process but
510 * we're also using the same prefix
511 * for widgets so cut the prefix off
512 * the front of the widget name.
513 */
514 snprintf(path->long_name, name_len, "%s %s",
515 w->name + prefix_len,
516 w->kcontrol_news[i].name);
517 break;
518 case snd_soc_dapm_mixer_named_ctl:
519 snprintf(path->long_name, name_len, "%s",
520 w->kcontrol_news[i].name);
521 break;
522 }
523
524 path->long_name[name_len - 1] = '\0';
525
526 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
527 wlist, path->long_name,
528 prefix);
529 ret = snd_ctl_add(card, path->kcontrol);
530 if (ret < 0) {
531 dev_err(dapm->dev,
532 "asoc: failed to add dapm kcontrol %s: %d\n",
533 path->long_name, ret);
534 kfree(wlist);
535 kfree(path->long_name);
536 path->long_name = NULL;
537 return ret;
538 }
539 w->kcontrols[i] = path->kcontrol;
540 }
541 }
542 return ret;
543 }
544
545 /* create new dapm mux control */
546 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
547 {
548 struct snd_soc_dapm_context *dapm = w->dapm;
549 struct snd_soc_dapm_path *path = NULL;
550 struct snd_kcontrol *kcontrol;
551 struct snd_card *card = dapm->card->snd_card;
552 const char *prefix;
553 size_t prefix_len;
554 int ret;
555 struct snd_soc_dapm_widget_list *wlist;
556 int shared, wlistentries;
557 size_t wlistsize;
558 char *name;
559
560 if (w->num_kcontrols != 1) {
561 dev_err(dapm->dev,
562 "asoc: mux %s has incorrect number of controls\n",
563 w->name);
564 return -EINVAL;
565 }
566
567 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
568 &kcontrol);
569 if (kcontrol) {
570 wlist = kcontrol->private_data;
571 wlistentries = wlist->num_widgets + 1;
572 } else {
573 wlist = NULL;
574 wlistentries = 1;
575 }
576 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
577 wlistentries * sizeof(struct snd_soc_dapm_widget *),
578 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
579 if (wlist == NULL) {
580 dev_err(dapm->dev,
581 "asoc: can't allocate widget list for %s\n", w->name);
582 return -ENOMEM;
583 }
584 wlist->num_widgets = wlistentries;
585 wlist->widgets[wlistentries - 1] = w;
586
587 if (!kcontrol) {
588 if (dapm->codec)
589 prefix = dapm->codec->name_prefix;
590 else
591 prefix = NULL;
592
593 if (shared) {
594 name = w->kcontrol_news[0].name;
595 prefix_len = 0;
596 } else {
597 name = w->name;
598 if (prefix)
599 prefix_len = strlen(prefix) + 1;
600 else
601 prefix_len = 0;
602 }
603
604 /*
605 * The control will get a prefix from the control creation
606 * process but we're also using the same prefix for widgets so
607 * cut the prefix off the front of the widget name.
608 */
609 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
610 name + prefix_len, prefix);
611 ret = snd_ctl_add(card, kcontrol);
612 if (ret < 0) {
613 dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
614 w->name, ret);
615 kfree(wlist);
616 return ret;
617 }
618 }
619
620 kcontrol->private_data = wlist;
621
622 w->kcontrols[0] = kcontrol;
623
624 list_for_each_entry(path, &w->sources, list_sink)
625 path->kcontrol = kcontrol;
626
627 return 0;
628 }
629
630 /* create new dapm volume control */
631 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
632 {
633 if (w->num_kcontrols)
634 dev_err(w->dapm->dev,
635 "asoc: PGA controls not supported: '%s'\n", w->name);
636
637 return 0;
638 }
639
640 /* reset 'walked' bit for each dapm path */
641 static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
642 {
643 struct snd_soc_dapm_path *p;
644
645 list_for_each_entry(p, &dapm->card->paths, list)
646 p->walked = 0;
647 }
648
649 /* We implement power down on suspend by checking the power state of
650 * the ALSA card - when we are suspending the ALSA state for the card
651 * is set to D3.
652 */
653 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
654 {
655 int level = snd_power_get_state(widget->dapm->card->snd_card);
656
657 switch (level) {
658 case SNDRV_CTL_POWER_D3hot:
659 case SNDRV_CTL_POWER_D3cold:
660 if (widget->ignore_suspend)
661 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
662 widget->name);
663 return widget->ignore_suspend;
664 default:
665 return 1;
666 }
667 }
668
669 /*
670 * Recursively check for a completed path to an active or physically connected
671 * output widget. Returns number of complete paths.
672 */
673 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
674 {
675 struct snd_soc_dapm_path *path;
676 int con = 0;
677
678 if (widget->outputs >= 0)
679 return widget->outputs;
680
681 DAPM_UPDATE_STAT(widget, path_checks);
682
683 if (widget->id == snd_soc_dapm_supply)
684 return 0;
685
686 switch (widget->id) {
687 case snd_soc_dapm_adc:
688 case snd_soc_dapm_aif_out:
689 if (widget->active) {
690 widget->outputs = snd_soc_dapm_suspend_check(widget);
691 return widget->outputs;
692 }
693 default:
694 break;
695 }
696
697 if (widget->connected) {
698 /* connected pin ? */
699 if (widget->id == snd_soc_dapm_output && !widget->ext) {
700 widget->outputs = snd_soc_dapm_suspend_check(widget);
701 return widget->outputs;
702 }
703
704 /* connected jack or spk ? */
705 if (widget->id == snd_soc_dapm_hp ||
706 widget->id == snd_soc_dapm_spk ||
707 (widget->id == snd_soc_dapm_line &&
708 !list_empty(&widget->sources))) {
709 widget->outputs = snd_soc_dapm_suspend_check(widget);
710 return widget->outputs;
711 }
712 }
713
714 list_for_each_entry(path, &widget->sinks, list_source) {
715 DAPM_UPDATE_STAT(widget, neighbour_checks);
716
717 if (path->weak)
718 continue;
719
720 if (path->walked)
721 continue;
722
723 if (path->sink && path->connect) {
724 path->walked = 1;
725 con += is_connected_output_ep(path->sink);
726 }
727 }
728
729 widget->outputs = con;
730
731 return con;
732 }
733
734 /*
735 * Recursively check for a completed path to an active or physically connected
736 * input widget. Returns number of complete paths.
737 */
738 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
739 {
740 struct snd_soc_dapm_path *path;
741 int con = 0;
742
743 if (widget->inputs >= 0)
744 return widget->inputs;
745
746 DAPM_UPDATE_STAT(widget, path_checks);
747
748 if (widget->id == snd_soc_dapm_supply)
749 return 0;
750
751 /* active stream ? */
752 switch (widget->id) {
753 case snd_soc_dapm_dac:
754 case snd_soc_dapm_aif_in:
755 if (widget->active) {
756 widget->inputs = snd_soc_dapm_suspend_check(widget);
757 return widget->inputs;
758 }
759 default:
760 break;
761 }
762
763 if (widget->connected) {
764 /* connected pin ? */
765 if (widget->id == snd_soc_dapm_input && !widget->ext) {
766 widget->inputs = snd_soc_dapm_suspend_check(widget);
767 return widget->inputs;
768 }
769
770 /* connected VMID/Bias for lower pops */
771 if (widget->id == snd_soc_dapm_vmid) {
772 widget->inputs = snd_soc_dapm_suspend_check(widget);
773 return widget->inputs;
774 }
775
776 /* connected jack ? */
777 if (widget->id == snd_soc_dapm_mic ||
778 (widget->id == snd_soc_dapm_line &&
779 !list_empty(&widget->sinks))) {
780 widget->inputs = snd_soc_dapm_suspend_check(widget);
781 return widget->inputs;
782 }
783
784 /* signal generator */
785 if (widget->id == snd_soc_dapm_siggen) {
786 widget->inputs = snd_soc_dapm_suspend_check(widget);
787 return widget->inputs;
788 }
789 }
790
791 list_for_each_entry(path, &widget->sources, list_sink) {
792 DAPM_UPDATE_STAT(widget, neighbour_checks);
793
794 if (path->weak)
795 continue;
796
797 if (path->walked)
798 continue;
799
800 if (path->source && path->connect) {
801 path->walked = 1;
802 con += is_connected_input_ep(path->source);
803 }
804 }
805
806 widget->inputs = con;
807
808 return con;
809 }
810
811 /*
812 * Handler for generic register modifier widget.
813 */
814 int dapm_reg_event(struct snd_soc_dapm_widget *w,
815 struct snd_kcontrol *kcontrol, int event)
816 {
817 unsigned int val;
818
819 if (SND_SOC_DAPM_EVENT_ON(event))
820 val = w->on_val;
821 else
822 val = w->off_val;
823
824 soc_widget_update_bits(w, -(w->reg + 1),
825 w->mask << w->shift, val << w->shift);
826
827 return 0;
828 }
829 EXPORT_SYMBOL_GPL(dapm_reg_event);
830
831 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
832 {
833 if (w->power_checked)
834 return w->new_power;
835
836 if (w->force)
837 w->new_power = 1;
838 else
839 w->new_power = w->power_check(w);
840
841 w->power_checked = true;
842
843 return w->new_power;
844 }
845
846 /* Generic check to see if a widget should be powered.
847 */
848 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
849 {
850 int in, out;
851
852 DAPM_UPDATE_STAT(w, power_checks);
853
854 in = is_connected_input_ep(w);
855 dapm_clear_walk(w->dapm);
856 out = is_connected_output_ep(w);
857 dapm_clear_walk(w->dapm);
858 return out != 0 && in != 0;
859 }
860
861 /* Check to see if an ADC has power */
862 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
863 {
864 int in;
865
866 DAPM_UPDATE_STAT(w, power_checks);
867
868 if (w->active) {
869 in = is_connected_input_ep(w);
870 dapm_clear_walk(w->dapm);
871 return in != 0;
872 } else {
873 return dapm_generic_check_power(w);
874 }
875 }
876
877 /* Check to see if a DAC has power */
878 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
879 {
880 int out;
881
882 DAPM_UPDATE_STAT(w, power_checks);
883
884 if (w->active) {
885 out = is_connected_output_ep(w);
886 dapm_clear_walk(w->dapm);
887 return out != 0;
888 } else {
889 return dapm_generic_check_power(w);
890 }
891 }
892
893 /* Check to see if a power supply is needed */
894 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
895 {
896 struct snd_soc_dapm_path *path;
897
898 DAPM_UPDATE_STAT(w, power_checks);
899
900 /* Check if one of our outputs is connected */
901 list_for_each_entry(path, &w->sinks, list_source) {
902 DAPM_UPDATE_STAT(w, neighbour_checks);
903
904 if (path->weak)
905 continue;
906
907 if (path->connected &&
908 !path->connected(path->source, path->sink))
909 continue;
910
911 if (!path->sink)
912 continue;
913
914 if (dapm_widget_power_check(path->sink))
915 return 1;
916 }
917
918 dapm_clear_walk(w->dapm);
919
920 return 0;
921 }
922
923 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
924 {
925 return 1;
926 }
927
928 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
929 struct snd_soc_dapm_widget *b,
930 bool power_up)
931 {
932 int *sort;
933
934 if (power_up)
935 sort = dapm_up_seq;
936 else
937 sort = dapm_down_seq;
938
939 if (sort[a->id] != sort[b->id])
940 return sort[a->id] - sort[b->id];
941 if (a->subseq != b->subseq) {
942 if (power_up)
943 return a->subseq - b->subseq;
944 else
945 return b->subseq - a->subseq;
946 }
947 if (a->reg != b->reg)
948 return a->reg - b->reg;
949 if (a->dapm != b->dapm)
950 return (unsigned long)a->dapm - (unsigned long)b->dapm;
951
952 return 0;
953 }
954
955 /* Insert a widget in order into a DAPM power sequence. */
956 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
957 struct list_head *list,
958 bool power_up)
959 {
960 struct snd_soc_dapm_widget *w;
961
962 list_for_each_entry(w, list, power_list)
963 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
964 list_add_tail(&new_widget->power_list, &w->power_list);
965 return;
966 }
967
968 list_add_tail(&new_widget->power_list, list);
969 }
970
971 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
972 struct snd_soc_dapm_widget *w, int event)
973 {
974 struct snd_soc_card *card = dapm->card;
975 const char *ev_name;
976 int power, ret;
977
978 switch (event) {
979 case SND_SOC_DAPM_PRE_PMU:
980 ev_name = "PRE_PMU";
981 power = 1;
982 break;
983 case SND_SOC_DAPM_POST_PMU:
984 ev_name = "POST_PMU";
985 power = 1;
986 break;
987 case SND_SOC_DAPM_PRE_PMD:
988 ev_name = "PRE_PMD";
989 power = 0;
990 break;
991 case SND_SOC_DAPM_POST_PMD:
992 ev_name = "POST_PMD";
993 power = 0;
994 break;
995 default:
996 BUG();
997 return;
998 }
999
1000 if (w->power != power)
1001 return;
1002
1003 if (w->event && (w->event_flags & event)) {
1004 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1005 w->name, ev_name);
1006 trace_snd_soc_dapm_widget_event_start(w, event);
1007 ret = w->event(w, NULL, event);
1008 trace_snd_soc_dapm_widget_event_done(w, event);
1009 if (ret < 0)
1010 pr_err("%s: %s event failed: %d\n",
1011 ev_name, w->name, ret);
1012 }
1013 }
1014
1015 /* Apply the coalesced changes from a DAPM sequence */
1016 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
1017 struct list_head *pending)
1018 {
1019 struct snd_soc_card *card = dapm->card;
1020 struct snd_soc_dapm_widget *w;
1021 int reg, power;
1022 unsigned int value = 0;
1023 unsigned int mask = 0;
1024 unsigned int cur_mask;
1025
1026 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1027 power_list)->reg;
1028
1029 list_for_each_entry(w, pending, power_list) {
1030 cur_mask = 1 << w->shift;
1031 BUG_ON(reg != w->reg);
1032
1033 if (w->invert)
1034 power = !w->power;
1035 else
1036 power = w->power;
1037
1038 mask |= cur_mask;
1039 if (power)
1040 value |= cur_mask;
1041
1042 pop_dbg(dapm->dev, card->pop_time,
1043 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1044 w->name, reg, value, mask);
1045
1046 /* Check for events */
1047 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1048 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
1049 }
1050
1051 if (reg >= 0) {
1052 /* Any widget will do, they should all be updating the
1053 * same register.
1054 */
1055 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1056 power_list);
1057
1058 pop_dbg(dapm->dev, card->pop_time,
1059 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1060 value, mask, reg, card->pop_time);
1061 pop_wait(card->pop_time);
1062 soc_widget_update_bits(w, reg, mask, value);
1063 }
1064
1065 list_for_each_entry(w, pending, power_list) {
1066 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1067 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
1068 }
1069 }
1070
1071 /* Apply a DAPM power sequence.
1072 *
1073 * We walk over a pre-sorted list of widgets to apply power to. In
1074 * order to minimise the number of writes to the device required
1075 * multiple widgets will be updated in a single write where possible.
1076 * Currently anything that requires more than a single write is not
1077 * handled.
1078 */
1079 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
1080 struct list_head *list, int event, bool power_up)
1081 {
1082 struct snd_soc_dapm_widget *w, *n;
1083 LIST_HEAD(pending);
1084 int cur_sort = -1;
1085 int cur_subseq = -1;
1086 int cur_reg = SND_SOC_NOPM;
1087 struct snd_soc_dapm_context *cur_dapm = NULL;
1088 int ret, i;
1089 int *sort;
1090
1091 if (power_up)
1092 sort = dapm_up_seq;
1093 else
1094 sort = dapm_down_seq;
1095
1096 list_for_each_entry_safe(w, n, list, power_list) {
1097 ret = 0;
1098
1099 /* Do we need to apply any queued changes? */
1100 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1101 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1102 if (!list_empty(&pending))
1103 dapm_seq_run_coalesced(cur_dapm, &pending);
1104
1105 if (cur_dapm && cur_dapm->seq_notifier) {
1106 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1107 if (sort[i] == cur_sort)
1108 cur_dapm->seq_notifier(cur_dapm,
1109 i,
1110 cur_subseq);
1111 }
1112
1113 INIT_LIST_HEAD(&pending);
1114 cur_sort = -1;
1115 cur_subseq = INT_MIN;
1116 cur_reg = SND_SOC_NOPM;
1117 cur_dapm = NULL;
1118 }
1119
1120 switch (w->id) {
1121 case snd_soc_dapm_pre:
1122 if (!w->event)
1123 list_for_each_entry_safe_continue(w, n, list,
1124 power_list);
1125
1126 if (event == SND_SOC_DAPM_STREAM_START)
1127 ret = w->event(w,
1128 NULL, SND_SOC_DAPM_PRE_PMU);
1129 else if (event == SND_SOC_DAPM_STREAM_STOP)
1130 ret = w->event(w,
1131 NULL, SND_SOC_DAPM_PRE_PMD);
1132 break;
1133
1134 case snd_soc_dapm_post:
1135 if (!w->event)
1136 list_for_each_entry_safe_continue(w, n, list,
1137 power_list);
1138
1139 if (event == SND_SOC_DAPM_STREAM_START)
1140 ret = w->event(w,
1141 NULL, SND_SOC_DAPM_POST_PMU);
1142 else if (event == SND_SOC_DAPM_STREAM_STOP)
1143 ret = w->event(w,
1144 NULL, SND_SOC_DAPM_POST_PMD);
1145 break;
1146
1147 default:
1148 /* Queue it up for application */
1149 cur_sort = sort[w->id];
1150 cur_subseq = w->subseq;
1151 cur_reg = w->reg;
1152 cur_dapm = w->dapm;
1153 list_move(&w->power_list, &pending);
1154 break;
1155 }
1156
1157 if (ret < 0)
1158 dev_err(w->dapm->dev,
1159 "Failed to apply widget power: %d\n", ret);
1160 }
1161
1162 if (!list_empty(&pending))
1163 dapm_seq_run_coalesced(cur_dapm, &pending);
1164
1165 if (cur_dapm && cur_dapm->seq_notifier) {
1166 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1167 if (sort[i] == cur_sort)
1168 cur_dapm->seq_notifier(cur_dapm,
1169 i, cur_subseq);
1170 }
1171 }
1172
1173 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1174 {
1175 struct snd_soc_dapm_update *update = dapm->update;
1176 struct snd_soc_dapm_widget *w;
1177 int ret;
1178
1179 if (!update)
1180 return;
1181
1182 w = update->widget;
1183
1184 if (w->event &&
1185 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1186 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1187 if (ret != 0)
1188 pr_err("%s DAPM pre-event failed: %d\n",
1189 w->name, ret);
1190 }
1191
1192 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1193 update->val);
1194 if (ret < 0)
1195 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1196
1197 if (w->event &&
1198 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1199 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1200 if (ret != 0)
1201 pr_err("%s DAPM post-event failed: %d\n",
1202 w->name, ret);
1203 }
1204 }
1205
1206 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1207 * they're changing state.
1208 */
1209 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1210 {
1211 struct snd_soc_dapm_context *d = data;
1212 int ret;
1213
1214 /* If we're off and we're not supposed to be go into STANDBY */
1215 if (d->bias_level == SND_SOC_BIAS_OFF &&
1216 d->target_bias_level != SND_SOC_BIAS_OFF) {
1217 if (d->dev)
1218 pm_runtime_get_sync(d->dev);
1219
1220 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1221 if (ret != 0)
1222 dev_err(d->dev,
1223 "Failed to turn on bias: %d\n", ret);
1224 }
1225
1226 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1227 if (d->bias_level != d->target_bias_level) {
1228 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1229 if (ret != 0)
1230 dev_err(d->dev,
1231 "Failed to prepare bias: %d\n", ret);
1232 }
1233 }
1234
1235 /* Async callback run prior to DAPM sequences - brings to their final
1236 * state.
1237 */
1238 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1239 {
1240 struct snd_soc_dapm_context *d = data;
1241 int ret;
1242
1243 /* If we just powered the last thing off drop to standby bias */
1244 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1245 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1246 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1247 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1248 if (ret != 0)
1249 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1250 ret);
1251 }
1252
1253 /* If we're in standby and can support bias off then do that */
1254 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1255 d->target_bias_level == SND_SOC_BIAS_OFF) {
1256 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1257 if (ret != 0)
1258 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1259
1260 if (d->dev)
1261 pm_runtime_put_sync(d->dev);
1262 }
1263
1264 /* If we just powered up then move to active bias */
1265 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1266 d->target_bias_level == SND_SOC_BIAS_ON) {
1267 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1268 if (ret != 0)
1269 dev_err(d->dev, "Failed to apply active bias: %d\n",
1270 ret);
1271 }
1272 }
1273
1274 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1275 bool power, bool connect)
1276 {
1277 /* If a connection is being made or broken then that update
1278 * will have marked the peer dirty, otherwise the widgets are
1279 * not connected and this update has no impact. */
1280 if (!connect)
1281 return;
1282
1283 /* If the peer is already in the state we're moving to then we
1284 * won't have an impact on it. */
1285 if (power != peer->power)
1286 dapm_mark_dirty(peer, "peer state change");
1287 }
1288
1289 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1290 struct list_head *up_list,
1291 struct list_head *down_list)
1292 {
1293 struct snd_soc_dapm_path *path;
1294
1295 if (w->power == power)
1296 return;
1297
1298 trace_snd_soc_dapm_widget_power(w, power);
1299
1300 /* If we changed our power state perhaps our neigbours changed
1301 * also.
1302 */
1303 list_for_each_entry(path, &w->sources, list_sink) {
1304 if (path->source) {
1305 dapm_widget_set_peer_power(path->source, power,
1306 path->connect);
1307 }
1308 }
1309 switch (w->id) {
1310 case snd_soc_dapm_supply:
1311 /* Supplies can't affect their outputs, only their inputs */
1312 break;
1313 default:
1314 list_for_each_entry(path, &w->sinks, list_source) {
1315 if (path->sink) {
1316 dapm_widget_set_peer_power(path->sink, power,
1317 path->connect);
1318 }
1319 }
1320 break;
1321 }
1322
1323 if (power)
1324 dapm_seq_insert(w, up_list, true);
1325 else
1326 dapm_seq_insert(w, down_list, false);
1327
1328 w->power = power;
1329 }
1330
1331 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1332 struct list_head *up_list,
1333 struct list_head *down_list)
1334 {
1335 int power;
1336
1337 switch (w->id) {
1338 case snd_soc_dapm_pre:
1339 dapm_seq_insert(w, down_list, false);
1340 break;
1341 case snd_soc_dapm_post:
1342 dapm_seq_insert(w, up_list, true);
1343 break;
1344
1345 default:
1346 power = dapm_widget_power_check(w);
1347
1348 dapm_widget_set_power(w, power, up_list, down_list);
1349 break;
1350 }
1351 }
1352
1353 /*
1354 * Scan each dapm widget for complete audio path.
1355 * A complete path is a route that has valid endpoints i.e.:-
1356 *
1357 * o DAC to output pin.
1358 * o Input Pin to ADC.
1359 * o Input pin to Output pin (bypass, sidetone)
1360 * o DAC to ADC (loopback).
1361 */
1362 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1363 {
1364 struct snd_soc_card *card = dapm->card;
1365 struct snd_soc_dapm_widget *w;
1366 struct snd_soc_dapm_context *d;
1367 LIST_HEAD(up_list);
1368 LIST_HEAD(down_list);
1369 LIST_HEAD(async_domain);
1370 enum snd_soc_bias_level bias;
1371
1372 trace_snd_soc_dapm_start(card);
1373
1374 list_for_each_entry(d, &card->dapm_list, list) {
1375 if (d->n_widgets || d->codec == NULL) {
1376 if (d->idle_bias_off)
1377 d->target_bias_level = SND_SOC_BIAS_OFF;
1378 else
1379 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1380 }
1381 }
1382
1383 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
1384
1385 list_for_each_entry(w, &card->widgets, list) {
1386 w->power_checked = false;
1387 w->inputs = -1;
1388 w->outputs = -1;
1389 }
1390
1391 /* Check which widgets we need to power and store them in
1392 * lists indicating if they should be powered up or down. We
1393 * only check widgets that have been flagged as dirty but note
1394 * that new widgets may be added to the dirty list while we
1395 * iterate.
1396 */
1397 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1398 dapm_power_one_widget(w, &up_list, &down_list);
1399 }
1400
1401 list_for_each_entry(w, &card->widgets, list) {
1402 list_del_init(&w->dirty);
1403
1404 if (w->power) {
1405 d = w->dapm;
1406
1407 /* Supplies and micbiases only bring the
1408 * context up to STANDBY as unless something
1409 * else is active and passing audio they
1410 * generally don't require full power.
1411 */
1412 switch (w->id) {
1413 case snd_soc_dapm_supply:
1414 case snd_soc_dapm_micbias:
1415 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1416 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1417 break;
1418 default:
1419 d->target_bias_level = SND_SOC_BIAS_ON;
1420 break;
1421 }
1422 }
1423
1424 }
1425
1426 /* If there are no DAPM widgets then try to figure out power from the
1427 * event type.
1428 */
1429 if (!dapm->n_widgets) {
1430 switch (event) {
1431 case SND_SOC_DAPM_STREAM_START:
1432 case SND_SOC_DAPM_STREAM_RESUME:
1433 dapm->target_bias_level = SND_SOC_BIAS_ON;
1434 break;
1435 case SND_SOC_DAPM_STREAM_STOP:
1436 if (dapm->codec && dapm->codec->active)
1437 dapm->target_bias_level = SND_SOC_BIAS_ON;
1438 else
1439 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1440 break;
1441 case SND_SOC_DAPM_STREAM_SUSPEND:
1442 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1443 break;
1444 case SND_SOC_DAPM_STREAM_NOP:
1445 dapm->target_bias_level = dapm->bias_level;
1446 break;
1447 default:
1448 break;
1449 }
1450 }
1451
1452 /* Force all contexts in the card to the same bias state if
1453 * they're not ground referenced.
1454 */
1455 bias = SND_SOC_BIAS_OFF;
1456 list_for_each_entry(d, &card->dapm_list, list)
1457 if (d->target_bias_level > bias)
1458 bias = d->target_bias_level;
1459 list_for_each_entry(d, &card->dapm_list, list)
1460 if (!d->idle_bias_off)
1461 d->target_bias_level = bias;
1462
1463 trace_snd_soc_dapm_walk_done(card);
1464
1465 /* Run all the bias changes in parallel */
1466 list_for_each_entry(d, &dapm->card->dapm_list, list)
1467 async_schedule_domain(dapm_pre_sequence_async, d,
1468 &async_domain);
1469 async_synchronize_full_domain(&async_domain);
1470
1471 /* Power down widgets first; try to avoid amplifying pops. */
1472 dapm_seq_run(dapm, &down_list, event, false);
1473
1474 dapm_widget_update(dapm);
1475
1476 /* Now power up. */
1477 dapm_seq_run(dapm, &up_list, event, true);
1478
1479 /* Run all the bias changes in parallel */
1480 list_for_each_entry(d, &dapm->card->dapm_list, list)
1481 async_schedule_domain(dapm_post_sequence_async, d,
1482 &async_domain);
1483 async_synchronize_full_domain(&async_domain);
1484
1485 pop_dbg(dapm->dev, card->pop_time,
1486 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1487 pop_wait(card->pop_time);
1488
1489 trace_snd_soc_dapm_done(card);
1490
1491 return 0;
1492 }
1493
1494 #ifdef CONFIG_DEBUG_FS
1495 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1496 {
1497 file->private_data = inode->i_private;
1498 return 0;
1499 }
1500
1501 static ssize_t dapm_widget_power_read_file(struct file *file,
1502 char __user *user_buf,
1503 size_t count, loff_t *ppos)
1504 {
1505 struct snd_soc_dapm_widget *w = file->private_data;
1506 char *buf;
1507 int in, out;
1508 ssize_t ret;
1509 struct snd_soc_dapm_path *p = NULL;
1510
1511 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1512 if (!buf)
1513 return -ENOMEM;
1514
1515 in = is_connected_input_ep(w);
1516 dapm_clear_walk(w->dapm);
1517 out = is_connected_output_ep(w);
1518 dapm_clear_walk(w->dapm);
1519
1520 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
1521 w->name, w->power ? "On" : "Off", in, out);
1522
1523 if (w->reg >= 0)
1524 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1525 " - R%d(0x%x) bit %d",
1526 w->reg, w->reg, w->shift);
1527
1528 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1529
1530 if (w->sname)
1531 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1532 w->sname,
1533 w->active ? "active" : "inactive");
1534
1535 list_for_each_entry(p, &w->sources, list_sink) {
1536 if (p->connected && !p->connected(w, p->sink))
1537 continue;
1538
1539 if (p->connect)
1540 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1541 " in \"%s\" \"%s\"\n",
1542 p->name ? p->name : "static",
1543 p->source->name);
1544 }
1545 list_for_each_entry(p, &w->sinks, list_source) {
1546 if (p->connected && !p->connected(w, p->sink))
1547 continue;
1548
1549 if (p->connect)
1550 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1551 " out \"%s\" \"%s\"\n",
1552 p->name ? p->name : "static",
1553 p->sink->name);
1554 }
1555
1556 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1557
1558 kfree(buf);
1559 return ret;
1560 }
1561
1562 static const struct file_operations dapm_widget_power_fops = {
1563 .open = dapm_widget_power_open_file,
1564 .read = dapm_widget_power_read_file,
1565 .llseek = default_llseek,
1566 };
1567
1568 static int dapm_bias_open_file(struct inode *inode, struct file *file)
1569 {
1570 file->private_data = inode->i_private;
1571 return 0;
1572 }
1573
1574 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1575 size_t count, loff_t *ppos)
1576 {
1577 struct snd_soc_dapm_context *dapm = file->private_data;
1578 char *level;
1579
1580 switch (dapm->bias_level) {
1581 case SND_SOC_BIAS_ON:
1582 level = "On\n";
1583 break;
1584 case SND_SOC_BIAS_PREPARE:
1585 level = "Prepare\n";
1586 break;
1587 case SND_SOC_BIAS_STANDBY:
1588 level = "Standby\n";
1589 break;
1590 case SND_SOC_BIAS_OFF:
1591 level = "Off\n";
1592 break;
1593 default:
1594 BUG();
1595 level = "Unknown\n";
1596 break;
1597 }
1598
1599 return simple_read_from_buffer(user_buf, count, ppos, level,
1600 strlen(level));
1601 }
1602
1603 static const struct file_operations dapm_bias_fops = {
1604 .open = dapm_bias_open_file,
1605 .read = dapm_bias_read_file,
1606 .llseek = default_llseek,
1607 };
1608
1609 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1610 struct dentry *parent)
1611 {
1612 struct dentry *d;
1613
1614 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1615
1616 if (!dapm->debugfs_dapm) {
1617 printk(KERN_WARNING
1618 "Failed to create DAPM debugfs directory\n");
1619 return;
1620 }
1621
1622 d = debugfs_create_file("bias_level", 0444,
1623 dapm->debugfs_dapm, dapm,
1624 &dapm_bias_fops);
1625 if (!d)
1626 dev_warn(dapm->dev,
1627 "ASoC: Failed to create bias level debugfs file\n");
1628 }
1629
1630 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1631 {
1632 struct snd_soc_dapm_context *dapm = w->dapm;
1633 struct dentry *d;
1634
1635 if (!dapm->debugfs_dapm || !w->name)
1636 return;
1637
1638 d = debugfs_create_file(w->name, 0444,
1639 dapm->debugfs_dapm, w,
1640 &dapm_widget_power_fops);
1641 if (!d)
1642 dev_warn(w->dapm->dev,
1643 "ASoC: Failed to create %s debugfs file\n",
1644 w->name);
1645 }
1646
1647 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1648 {
1649 debugfs_remove_recursive(dapm->debugfs_dapm);
1650 }
1651
1652 #else
1653 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1654 struct dentry *parent)
1655 {
1656 }
1657
1658 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1659 {
1660 }
1661
1662 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1663 {
1664 }
1665
1666 #endif
1667
1668 /* test and update the power status of a mux widget */
1669 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1670 struct snd_kcontrol *kcontrol, int change,
1671 int mux, struct soc_enum *e)
1672 {
1673 struct snd_soc_dapm_path *path;
1674 int found = 0;
1675
1676 if (widget->id != snd_soc_dapm_mux &&
1677 widget->id != snd_soc_dapm_virt_mux &&
1678 widget->id != snd_soc_dapm_value_mux)
1679 return -ENODEV;
1680
1681 if (!change)
1682 return 0;
1683
1684 /* find dapm widget path assoc with kcontrol */
1685 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1686 if (path->kcontrol != kcontrol)
1687 continue;
1688
1689 if (!path->name || !e->texts[mux])
1690 continue;
1691
1692 found = 1;
1693 /* we now need to match the string in the enum to the path */
1694 if (!(strcmp(path->name, e->texts[mux]))) {
1695 path->connect = 1; /* new connection */
1696 dapm_mark_dirty(path->source, "mux connection");
1697 } else {
1698 if (path->connect)
1699 dapm_mark_dirty(path->source,
1700 "mux disconnection");
1701 path->connect = 0; /* old connection must be powered down */
1702 }
1703 }
1704
1705 if (found) {
1706 dapm_mark_dirty(widget, "mux change");
1707 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1708 }
1709
1710 return 0;
1711 }
1712
1713 /* test and update the power status of a mixer or switch widget */
1714 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1715 struct snd_kcontrol *kcontrol, int connect)
1716 {
1717 struct snd_soc_dapm_path *path;
1718 int found = 0;
1719
1720 if (widget->id != snd_soc_dapm_mixer &&
1721 widget->id != snd_soc_dapm_mixer_named_ctl &&
1722 widget->id != snd_soc_dapm_switch)
1723 return -ENODEV;
1724
1725 /* find dapm widget path assoc with kcontrol */
1726 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1727 if (path->kcontrol != kcontrol)
1728 continue;
1729
1730 /* found, now check type */
1731 found = 1;
1732 path->connect = connect;
1733 dapm_mark_dirty(path->source, "mixer connection");
1734 }
1735
1736 if (found) {
1737 dapm_mark_dirty(widget, "mixer update");
1738 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1739 }
1740
1741 return 0;
1742 }
1743
1744 /* show dapm widget status in sys fs */
1745 static ssize_t dapm_widget_show(struct device *dev,
1746 struct device_attribute *attr, char *buf)
1747 {
1748 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
1749 struct snd_soc_codec *codec =rtd->codec;
1750 struct snd_soc_dapm_widget *w;
1751 int count = 0;
1752 char *state = "not set";
1753
1754 list_for_each_entry(w, &codec->card->widgets, list) {
1755 if (w->dapm != &codec->dapm)
1756 continue;
1757
1758 /* only display widgets that burnm power */
1759 switch (w->id) {
1760 case snd_soc_dapm_hp:
1761 case snd_soc_dapm_mic:
1762 case snd_soc_dapm_spk:
1763 case snd_soc_dapm_line:
1764 case snd_soc_dapm_micbias:
1765 case snd_soc_dapm_dac:
1766 case snd_soc_dapm_adc:
1767 case snd_soc_dapm_pga:
1768 case snd_soc_dapm_out_drv:
1769 case snd_soc_dapm_mixer:
1770 case snd_soc_dapm_mixer_named_ctl:
1771 case snd_soc_dapm_supply:
1772 if (w->name)
1773 count += sprintf(buf + count, "%s: %s\n",
1774 w->name, w->power ? "On":"Off");
1775 break;
1776 default:
1777 break;
1778 }
1779 }
1780
1781 switch (codec->dapm.bias_level) {
1782 case SND_SOC_BIAS_ON:
1783 state = "On";
1784 break;
1785 case SND_SOC_BIAS_PREPARE:
1786 state = "Prepare";
1787 break;
1788 case SND_SOC_BIAS_STANDBY:
1789 state = "Standby";
1790 break;
1791 case SND_SOC_BIAS_OFF:
1792 state = "Off";
1793 break;
1794 }
1795 count += sprintf(buf + count, "PM State: %s\n", state);
1796
1797 return count;
1798 }
1799
1800 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1801
1802 int snd_soc_dapm_sys_add(struct device *dev)
1803 {
1804 return device_create_file(dev, &dev_attr_dapm_widget);
1805 }
1806
1807 static void snd_soc_dapm_sys_remove(struct device *dev)
1808 {
1809 device_remove_file(dev, &dev_attr_dapm_widget);
1810 }
1811
1812 /* free all dapm widgets and resources */
1813 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1814 {
1815 struct snd_soc_dapm_widget *w, *next_w;
1816 struct snd_soc_dapm_path *p, *next_p;
1817
1818 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1819 if (w->dapm != dapm)
1820 continue;
1821 list_del(&w->list);
1822 /*
1823 * remove source and sink paths associated to this widget.
1824 * While removing the path, remove reference to it from both
1825 * source and sink widgets so that path is removed only once.
1826 */
1827 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1828 list_del(&p->list_sink);
1829 list_del(&p->list_source);
1830 list_del(&p->list);
1831 kfree(p->long_name);
1832 kfree(p);
1833 }
1834 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1835 list_del(&p->list_sink);
1836 list_del(&p->list_source);
1837 list_del(&p->list);
1838 kfree(p->long_name);
1839 kfree(p);
1840 }
1841 kfree(w->kcontrols);
1842 kfree(w->name);
1843 kfree(w);
1844 }
1845 }
1846
1847 static struct snd_soc_dapm_widget *dapm_find_widget(
1848 struct snd_soc_dapm_context *dapm, const char *pin,
1849 bool search_other_contexts)
1850 {
1851 struct snd_soc_dapm_widget *w;
1852 struct snd_soc_dapm_widget *fallback = NULL;
1853
1854 list_for_each_entry(w, &dapm->card->widgets, list) {
1855 if (!strcmp(w->name, pin)) {
1856 if (w->dapm == dapm)
1857 return w;
1858 else
1859 fallback = w;
1860 }
1861 }
1862
1863 if (search_other_contexts)
1864 return fallback;
1865
1866 return NULL;
1867 }
1868
1869 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
1870 const char *pin, int status)
1871 {
1872 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
1873
1874 if (!w) {
1875 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1876 return -EINVAL;
1877 }
1878
1879 w->connected = status;
1880 if (status == 0)
1881 w->force = 0;
1882 dapm_mark_dirty(w, "pin configuration");
1883
1884 return 0;
1885 }
1886
1887 /**
1888 * snd_soc_dapm_sync - scan and power dapm paths
1889 * @dapm: DAPM context
1890 *
1891 * Walks all dapm audio paths and powers widgets according to their
1892 * stream or path usage.
1893 *
1894 * Returns 0 for success.
1895 */
1896 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
1897 {
1898 /*
1899 * Suppress early reports (eg, jacks syncing their state) to avoid
1900 * silly DAPM runs during card startup.
1901 */
1902 if (!dapm->card || !dapm->card->instantiated)
1903 return 0;
1904
1905 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1906 }
1907 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1908
1909 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
1910 const struct snd_soc_dapm_route *route)
1911 {
1912 struct snd_soc_dapm_path *path;
1913 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1914 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
1915 const char *sink;
1916 const char *control = route->control;
1917 const char *source;
1918 char prefixed_sink[80];
1919 char prefixed_source[80];
1920 int ret = 0;
1921
1922 if (dapm->codec && dapm->codec->name_prefix) {
1923 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1924 dapm->codec->name_prefix, route->sink);
1925 sink = prefixed_sink;
1926 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1927 dapm->codec->name_prefix, route->source);
1928 source = prefixed_source;
1929 } else {
1930 sink = route->sink;
1931 source = route->source;
1932 }
1933
1934 /*
1935 * find src and dest widgets over all widgets but favor a widget from
1936 * current DAPM context
1937 */
1938 list_for_each_entry(w, &dapm->card->widgets, list) {
1939 if (!wsink && !(strcmp(w->name, sink))) {
1940 wtsink = w;
1941 if (w->dapm == dapm)
1942 wsink = w;
1943 continue;
1944 }
1945 if (!wsource && !(strcmp(w->name, source))) {
1946 wtsource = w;
1947 if (w->dapm == dapm)
1948 wsource = w;
1949 }
1950 }
1951 /* use widget from another DAPM context if not found from this */
1952 if (!wsink)
1953 wsink = wtsink;
1954 if (!wsource)
1955 wsource = wtsource;
1956
1957 if (wsource == NULL || wsink == NULL)
1958 return -ENODEV;
1959
1960 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1961 if (!path)
1962 return -ENOMEM;
1963
1964 path->source = wsource;
1965 path->sink = wsink;
1966 path->connected = route->connected;
1967 INIT_LIST_HEAD(&path->list);
1968 INIT_LIST_HEAD(&path->list_source);
1969 INIT_LIST_HEAD(&path->list_sink);
1970
1971 /* check for external widgets */
1972 if (wsink->id == snd_soc_dapm_input) {
1973 if (wsource->id == snd_soc_dapm_micbias ||
1974 wsource->id == snd_soc_dapm_mic ||
1975 wsource->id == snd_soc_dapm_line ||
1976 wsource->id == snd_soc_dapm_output)
1977 wsink->ext = 1;
1978 }
1979 if (wsource->id == snd_soc_dapm_output) {
1980 if (wsink->id == snd_soc_dapm_spk ||
1981 wsink->id == snd_soc_dapm_hp ||
1982 wsink->id == snd_soc_dapm_line ||
1983 wsink->id == snd_soc_dapm_input)
1984 wsource->ext = 1;
1985 }
1986
1987 /* connect static paths */
1988 if (control == NULL) {
1989 list_add(&path->list, &dapm->card->paths);
1990 list_add(&path->list_sink, &wsink->sources);
1991 list_add(&path->list_source, &wsource->sinks);
1992 path->connect = 1;
1993 return 0;
1994 }
1995
1996 /* connect dynamic paths */
1997 switch (wsink->id) {
1998 case snd_soc_dapm_adc:
1999 case snd_soc_dapm_dac:
2000 case snd_soc_dapm_pga:
2001 case snd_soc_dapm_out_drv:
2002 case snd_soc_dapm_input:
2003 case snd_soc_dapm_output:
2004 case snd_soc_dapm_siggen:
2005 case snd_soc_dapm_micbias:
2006 case snd_soc_dapm_vmid:
2007 case snd_soc_dapm_pre:
2008 case snd_soc_dapm_post:
2009 case snd_soc_dapm_supply:
2010 case snd_soc_dapm_aif_in:
2011 case snd_soc_dapm_aif_out:
2012 list_add(&path->list, &dapm->card->paths);
2013 list_add(&path->list_sink, &wsink->sources);
2014 list_add(&path->list_source, &wsource->sinks);
2015 path->connect = 1;
2016 return 0;
2017 case snd_soc_dapm_mux:
2018 case snd_soc_dapm_virt_mux:
2019 case snd_soc_dapm_value_mux:
2020 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2021 &wsink->kcontrol_news[0]);
2022 if (ret != 0)
2023 goto err;
2024 break;
2025 case snd_soc_dapm_switch:
2026 case snd_soc_dapm_mixer:
2027 case snd_soc_dapm_mixer_named_ctl:
2028 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2029 if (ret != 0)
2030 goto err;
2031 break;
2032 case snd_soc_dapm_hp:
2033 case snd_soc_dapm_mic:
2034 case snd_soc_dapm_line:
2035 case snd_soc_dapm_spk:
2036 list_add(&path->list, &dapm->card->paths);
2037 list_add(&path->list_sink, &wsink->sources);
2038 list_add(&path->list_source, &wsource->sinks);
2039 path->connect = 0;
2040 return 0;
2041 }
2042 return 0;
2043
2044 err:
2045 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2046 source, control, sink);
2047 kfree(path);
2048 return ret;
2049 }
2050
2051 /**
2052 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2053 * @dapm: DAPM context
2054 * @route: audio routes
2055 * @num: number of routes
2056 *
2057 * Connects 2 dapm widgets together via a named audio path. The sink is
2058 * the widget receiving the audio signal, whilst the source is the sender
2059 * of the audio signal.
2060 *
2061 * Returns 0 for success else error. On error all resources can be freed
2062 * with a call to snd_soc_card_free().
2063 */
2064 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2065 const struct snd_soc_dapm_route *route, int num)
2066 {
2067 int i, ret;
2068
2069 for (i = 0; i < num; i++) {
2070 ret = snd_soc_dapm_add_route(dapm, route);
2071 if (ret < 0) {
2072 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2073 route->source, route->sink);
2074 return ret;
2075 }
2076 route++;
2077 }
2078
2079 return 0;
2080 }
2081 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2082
2083 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2084 const struct snd_soc_dapm_route *route)
2085 {
2086 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2087 route->source,
2088 true);
2089 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2090 route->sink,
2091 true);
2092 struct snd_soc_dapm_path *path;
2093 int count = 0;
2094
2095 if (!source) {
2096 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2097 route->source);
2098 return -ENODEV;
2099 }
2100
2101 if (!sink) {
2102 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2103 route->sink);
2104 return -ENODEV;
2105 }
2106
2107 if (route->control || route->connected)
2108 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2109 route->source, route->sink);
2110
2111 list_for_each_entry(path, &source->sinks, list_source) {
2112 if (path->sink == sink) {
2113 path->weak = 1;
2114 count++;
2115 }
2116 }
2117
2118 if (count == 0)
2119 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2120 route->source, route->sink);
2121 if (count > 1)
2122 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2123 count, route->source, route->sink);
2124
2125 return 0;
2126 }
2127
2128 /**
2129 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2130 * @dapm: DAPM context
2131 * @route: audio routes
2132 * @num: number of routes
2133 *
2134 * Mark existing routes matching those specified in the passed array
2135 * as being weak, meaning that they are ignored for the purpose of
2136 * power decisions. The main intended use case is for sidetone paths
2137 * which couple audio between other independent paths if they are both
2138 * active in order to make the combination work better at the user
2139 * level but which aren't intended to be "used".
2140 *
2141 * Note that CODEC drivers should not use this as sidetone type paths
2142 * can frequently also be used as bypass paths.
2143 */
2144 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2145 const struct snd_soc_dapm_route *route, int num)
2146 {
2147 int i, err;
2148 int ret = 0;
2149
2150 for (i = 0; i < num; i++) {
2151 err = snd_soc_dapm_weak_route(dapm, route);
2152 if (err)
2153 ret = err;
2154 route++;
2155 }
2156
2157 return ret;
2158 }
2159 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2160
2161 /**
2162 * snd_soc_dapm_new_widgets - add new dapm widgets
2163 * @dapm: DAPM context
2164 *
2165 * Checks the codec for any new dapm widgets and creates them if found.
2166 *
2167 * Returns 0 for success.
2168 */
2169 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
2170 {
2171 struct snd_soc_dapm_widget *w;
2172 unsigned int val;
2173
2174 list_for_each_entry(w, &dapm->card->widgets, list)
2175 {
2176 if (w->new)
2177 continue;
2178
2179 if (w->num_kcontrols) {
2180 w->kcontrols = kzalloc(w->num_kcontrols *
2181 sizeof(struct snd_kcontrol *),
2182 GFP_KERNEL);
2183 if (!w->kcontrols)
2184 return -ENOMEM;
2185 }
2186
2187 switch(w->id) {
2188 case snd_soc_dapm_switch:
2189 case snd_soc_dapm_mixer:
2190 case snd_soc_dapm_mixer_named_ctl:
2191 dapm_new_mixer(w);
2192 break;
2193 case snd_soc_dapm_mux:
2194 case snd_soc_dapm_virt_mux:
2195 case snd_soc_dapm_value_mux:
2196 dapm_new_mux(w);
2197 break;
2198 case snd_soc_dapm_pga:
2199 case snd_soc_dapm_out_drv:
2200 dapm_new_pga(w);
2201 break;
2202 default:
2203 break;
2204 }
2205
2206 /* Read the initial power state from the device */
2207 if (w->reg >= 0) {
2208 val = soc_widget_read(w, w->reg);
2209 val &= 1 << w->shift;
2210 if (w->invert)
2211 val = !val;
2212
2213 if (val)
2214 w->power = 1;
2215 }
2216
2217 w->new = 1;
2218
2219 dapm_mark_dirty(w, "new widget");
2220 dapm_debugfs_add_widget(w);
2221 }
2222
2223 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2224 return 0;
2225 }
2226 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2227
2228 /**
2229 * snd_soc_dapm_get_volsw - dapm mixer get callback
2230 * @kcontrol: mixer control
2231 * @ucontrol: control element information
2232 *
2233 * Callback to get the value of a dapm mixer control.
2234 *
2235 * Returns 0 for success.
2236 */
2237 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2238 struct snd_ctl_elem_value *ucontrol)
2239 {
2240 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2241 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2242 struct soc_mixer_control *mc =
2243 (struct soc_mixer_control *)kcontrol->private_value;
2244 unsigned int reg = mc->reg;
2245 unsigned int shift = mc->shift;
2246 unsigned int rshift = mc->rshift;
2247 int max = mc->max;
2248 unsigned int invert = mc->invert;
2249 unsigned int mask = (1 << fls(max)) - 1;
2250
2251 ucontrol->value.integer.value[0] =
2252 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2253 if (shift != rshift)
2254 ucontrol->value.integer.value[1] =
2255 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2256 if (invert) {
2257 ucontrol->value.integer.value[0] =
2258 max - ucontrol->value.integer.value[0];
2259 if (shift != rshift)
2260 ucontrol->value.integer.value[1] =
2261 max - ucontrol->value.integer.value[1];
2262 }
2263
2264 return 0;
2265 }
2266 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2267
2268 /**
2269 * snd_soc_dapm_put_volsw - dapm mixer set callback
2270 * @kcontrol: mixer control
2271 * @ucontrol: control element information
2272 *
2273 * Callback to set the value of a dapm mixer control.
2274 *
2275 * Returns 0 for success.
2276 */
2277 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2278 struct snd_ctl_elem_value *ucontrol)
2279 {
2280 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2281 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2282 struct snd_soc_codec *codec = widget->codec;
2283 struct soc_mixer_control *mc =
2284 (struct soc_mixer_control *)kcontrol->private_value;
2285 unsigned int reg = mc->reg;
2286 unsigned int shift = mc->shift;
2287 int max = mc->max;
2288 unsigned int mask = (1 << fls(max)) - 1;
2289 unsigned int invert = mc->invert;
2290 unsigned int val;
2291 int connect, change;
2292 struct snd_soc_dapm_update update;
2293 int wi;
2294
2295 val = (ucontrol->value.integer.value[0] & mask);
2296
2297 if (invert)
2298 val = max - val;
2299 mask = mask << shift;
2300 val = val << shift;
2301
2302 if (val)
2303 /* new connection */
2304 connect = invert ? 0 : 1;
2305 else
2306 /* old connection must be powered down */
2307 connect = invert ? 1 : 0;
2308
2309 mutex_lock(&codec->mutex);
2310
2311 change = snd_soc_test_bits(widget->codec, reg, mask, val);
2312 if (change) {
2313 for (wi = 0; wi < wlist->num_widgets; wi++) {
2314 widget = wlist->widgets[wi];
2315
2316 widget->value = val;
2317
2318 update.kcontrol = kcontrol;
2319 update.widget = widget;
2320 update.reg = reg;
2321 update.mask = mask;
2322 update.val = val;
2323 widget->dapm->update = &update;
2324
2325 dapm_mixer_update_power(widget, kcontrol, connect);
2326
2327 widget->dapm->update = NULL;
2328 }
2329 }
2330
2331 mutex_unlock(&codec->mutex);
2332 return 0;
2333 }
2334 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2335
2336 /**
2337 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2338 * @kcontrol: mixer control
2339 * @ucontrol: control element information
2340 *
2341 * Callback to get the value of a dapm enumerated double mixer control.
2342 *
2343 * Returns 0 for success.
2344 */
2345 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2346 struct snd_ctl_elem_value *ucontrol)
2347 {
2348 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2349 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2350 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2351 unsigned int val, bitmask;
2352
2353 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2354 ;
2355 val = snd_soc_read(widget->codec, e->reg);
2356 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2357 if (e->shift_l != e->shift_r)
2358 ucontrol->value.enumerated.item[1] =
2359 (val >> e->shift_r) & (bitmask - 1);
2360
2361 return 0;
2362 }
2363 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2364
2365 /**
2366 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2367 * @kcontrol: mixer control
2368 * @ucontrol: control element information
2369 *
2370 * Callback to set the value of a dapm enumerated double mixer control.
2371 *
2372 * Returns 0 for success.
2373 */
2374 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2375 struct snd_ctl_elem_value *ucontrol)
2376 {
2377 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2378 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2379 struct snd_soc_codec *codec = widget->codec;
2380 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2381 unsigned int val, mux, change;
2382 unsigned int mask, bitmask;
2383 struct snd_soc_dapm_update update;
2384 int wi;
2385
2386 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2387 ;
2388 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2389 return -EINVAL;
2390 mux = ucontrol->value.enumerated.item[0];
2391 val = mux << e->shift_l;
2392 mask = (bitmask - 1) << e->shift_l;
2393 if (e->shift_l != e->shift_r) {
2394 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2395 return -EINVAL;
2396 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2397 mask |= (bitmask - 1) << e->shift_r;
2398 }
2399
2400 mutex_lock(&codec->mutex);
2401
2402 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2403 if (change) {
2404 for (wi = 0; wi < wlist->num_widgets; wi++) {
2405 widget = wlist->widgets[wi];
2406
2407 widget->value = val;
2408
2409 update.kcontrol = kcontrol;
2410 update.widget = widget;
2411 update.reg = e->reg;
2412 update.mask = mask;
2413 update.val = val;
2414 widget->dapm->update = &update;
2415
2416 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2417
2418 widget->dapm->update = NULL;
2419 }
2420 }
2421
2422 mutex_unlock(&codec->mutex);
2423 return change;
2424 }
2425 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2426
2427 /**
2428 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2429 * @kcontrol: mixer control
2430 * @ucontrol: control element information
2431 *
2432 * Returns 0 for success.
2433 */
2434 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2435 struct snd_ctl_elem_value *ucontrol)
2436 {
2437 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2438 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2439
2440 ucontrol->value.enumerated.item[0] = widget->value;
2441
2442 return 0;
2443 }
2444 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2445
2446 /**
2447 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2448 * @kcontrol: mixer control
2449 * @ucontrol: control element information
2450 *
2451 * Returns 0 for success.
2452 */
2453 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2454 struct snd_ctl_elem_value *ucontrol)
2455 {
2456 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2457 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2458 struct snd_soc_codec *codec = widget->codec;
2459 struct soc_enum *e =
2460 (struct soc_enum *)kcontrol->private_value;
2461 int change;
2462 int ret = 0;
2463 int wi;
2464
2465 if (ucontrol->value.enumerated.item[0] >= e->max)
2466 return -EINVAL;
2467
2468 mutex_lock(&codec->mutex);
2469
2470 change = widget->value != ucontrol->value.enumerated.item[0];
2471 if (change) {
2472 for (wi = 0; wi < wlist->num_widgets; wi++) {
2473 widget = wlist->widgets[wi];
2474
2475 widget->value = ucontrol->value.enumerated.item[0];
2476
2477 dapm_mux_update_power(widget, kcontrol, change,
2478 widget->value, e);
2479 }
2480 }
2481
2482 mutex_unlock(&codec->mutex);
2483 return ret;
2484 }
2485 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2486
2487 /**
2488 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2489 * callback
2490 * @kcontrol: mixer control
2491 * @ucontrol: control element information
2492 *
2493 * Callback to get the value of a dapm semi enumerated double mixer control.
2494 *
2495 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2496 * used for handling bitfield coded enumeration for example.
2497 *
2498 * Returns 0 for success.
2499 */
2500 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2501 struct snd_ctl_elem_value *ucontrol)
2502 {
2503 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2504 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2505 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2506 unsigned int reg_val, val, mux;
2507
2508 reg_val = snd_soc_read(widget->codec, e->reg);
2509 val = (reg_val >> e->shift_l) & e->mask;
2510 for (mux = 0; mux < e->max; mux++) {
2511 if (val == e->values[mux])
2512 break;
2513 }
2514 ucontrol->value.enumerated.item[0] = mux;
2515 if (e->shift_l != e->shift_r) {
2516 val = (reg_val >> e->shift_r) & e->mask;
2517 for (mux = 0; mux < e->max; mux++) {
2518 if (val == e->values[mux])
2519 break;
2520 }
2521 ucontrol->value.enumerated.item[1] = mux;
2522 }
2523
2524 return 0;
2525 }
2526 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2527
2528 /**
2529 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2530 * callback
2531 * @kcontrol: mixer control
2532 * @ucontrol: control element information
2533 *
2534 * Callback to set the value of a dapm semi enumerated double mixer control.
2535 *
2536 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2537 * used for handling bitfield coded enumeration for example.
2538 *
2539 * Returns 0 for success.
2540 */
2541 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2542 struct snd_ctl_elem_value *ucontrol)
2543 {
2544 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2545 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2546 struct snd_soc_codec *codec = widget->codec;
2547 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2548 unsigned int val, mux, change;
2549 unsigned int mask;
2550 struct snd_soc_dapm_update update;
2551 int wi;
2552
2553 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2554 return -EINVAL;
2555 mux = ucontrol->value.enumerated.item[0];
2556 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2557 mask = e->mask << e->shift_l;
2558 if (e->shift_l != e->shift_r) {
2559 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2560 return -EINVAL;
2561 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2562 mask |= e->mask << e->shift_r;
2563 }
2564
2565 mutex_lock(&codec->mutex);
2566
2567 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2568 if (change) {
2569 for (wi = 0; wi < wlist->num_widgets; wi++) {
2570 widget = wlist->widgets[wi];
2571
2572 widget->value = val;
2573
2574 update.kcontrol = kcontrol;
2575 update.widget = widget;
2576 update.reg = e->reg;
2577 update.mask = mask;
2578 update.val = val;
2579 widget->dapm->update = &update;
2580
2581 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2582
2583 widget->dapm->update = NULL;
2584 }
2585 }
2586
2587 mutex_unlock(&codec->mutex);
2588 return change;
2589 }
2590 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2591
2592 /**
2593 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2594 *
2595 * @kcontrol: mixer control
2596 * @uinfo: control element information
2597 *
2598 * Callback to provide information about a pin switch control.
2599 */
2600 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2601 struct snd_ctl_elem_info *uinfo)
2602 {
2603 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2604 uinfo->count = 1;
2605 uinfo->value.integer.min = 0;
2606 uinfo->value.integer.max = 1;
2607
2608 return 0;
2609 }
2610 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2611
2612 /**
2613 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2614 *
2615 * @kcontrol: mixer control
2616 * @ucontrol: Value
2617 */
2618 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2619 struct snd_ctl_elem_value *ucontrol)
2620 {
2621 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2622 const char *pin = (const char *)kcontrol->private_value;
2623
2624 mutex_lock(&codec->mutex);
2625
2626 ucontrol->value.integer.value[0] =
2627 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
2628
2629 mutex_unlock(&codec->mutex);
2630
2631 return 0;
2632 }
2633 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2634
2635 /**
2636 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2637 *
2638 * @kcontrol: mixer control
2639 * @ucontrol: Value
2640 */
2641 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2642 struct snd_ctl_elem_value *ucontrol)
2643 {
2644 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2645 const char *pin = (const char *)kcontrol->private_value;
2646
2647 mutex_lock(&codec->mutex);
2648
2649 if (ucontrol->value.integer.value[0])
2650 snd_soc_dapm_enable_pin(&codec->dapm, pin);
2651 else
2652 snd_soc_dapm_disable_pin(&codec->dapm, pin);
2653
2654 snd_soc_dapm_sync(&codec->dapm);
2655
2656 mutex_unlock(&codec->mutex);
2657
2658 return 0;
2659 }
2660 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2661
2662 /**
2663 * snd_soc_dapm_new_control - create new dapm control
2664 * @dapm: DAPM context
2665 * @widget: widget template
2666 *
2667 * Creates a new dapm control based upon the template.
2668 *
2669 * Returns 0 for success else error.
2670 */
2671 int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2672 const struct snd_soc_dapm_widget *widget)
2673 {
2674 struct snd_soc_dapm_widget *w;
2675 size_t name_len;
2676
2677 if ((w = dapm_cnew_widget(widget)) == NULL)
2678 return -ENOMEM;
2679
2680 name_len = strlen(widget->name) + 1;
2681 if (dapm->codec && dapm->codec->name_prefix)
2682 name_len += 1 + strlen(dapm->codec->name_prefix);
2683 w->name = kmalloc(name_len, GFP_KERNEL);
2684 if (w->name == NULL) {
2685 kfree(w);
2686 return -ENOMEM;
2687 }
2688 if (dapm->codec && dapm->codec->name_prefix)
2689 snprintf(w->name, name_len, "%s %s",
2690 dapm->codec->name_prefix, widget->name);
2691 else
2692 snprintf(w->name, name_len, "%s", widget->name);
2693
2694 switch (w->id) {
2695 case snd_soc_dapm_switch:
2696 case snd_soc_dapm_mixer:
2697 case snd_soc_dapm_mixer_named_ctl:
2698 w->power_check = dapm_generic_check_power;
2699 break;
2700 case snd_soc_dapm_mux:
2701 case snd_soc_dapm_virt_mux:
2702 case snd_soc_dapm_value_mux:
2703 w->power_check = dapm_generic_check_power;
2704 break;
2705 case snd_soc_dapm_adc:
2706 case snd_soc_dapm_aif_out:
2707 w->power_check = dapm_adc_check_power;
2708 break;
2709 case snd_soc_dapm_dac:
2710 case snd_soc_dapm_aif_in:
2711 w->power_check = dapm_dac_check_power;
2712 break;
2713 case snd_soc_dapm_pga:
2714 case snd_soc_dapm_out_drv:
2715 case snd_soc_dapm_input:
2716 case snd_soc_dapm_output:
2717 case snd_soc_dapm_micbias:
2718 case snd_soc_dapm_spk:
2719 case snd_soc_dapm_hp:
2720 case snd_soc_dapm_mic:
2721 case snd_soc_dapm_line:
2722 w->power_check = dapm_generic_check_power;
2723 break;
2724 case snd_soc_dapm_supply:
2725 w->power_check = dapm_supply_check_power;
2726 break;
2727 default:
2728 w->power_check = dapm_always_on_check_power;
2729 break;
2730 }
2731
2732 dapm->n_widgets++;
2733 w->dapm = dapm;
2734 w->codec = dapm->codec;
2735 w->platform = dapm->platform;
2736 INIT_LIST_HEAD(&w->sources);
2737 INIT_LIST_HEAD(&w->sinks);
2738 INIT_LIST_HEAD(&w->list);
2739 INIT_LIST_HEAD(&w->dirty);
2740 list_add(&w->list, &dapm->card->widgets);
2741
2742 /* machine layer set ups unconnected pins and insertions */
2743 w->connected = 1;
2744 return 0;
2745 }
2746 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2747
2748 /**
2749 * snd_soc_dapm_new_controls - create new dapm controls
2750 * @dapm: DAPM context
2751 * @widget: widget array
2752 * @num: number of widgets
2753 *
2754 * Creates new DAPM controls based upon the templates.
2755 *
2756 * Returns 0 for success else error.
2757 */
2758 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2759 const struct snd_soc_dapm_widget *widget,
2760 int num)
2761 {
2762 int i, ret;
2763
2764 for (i = 0; i < num; i++) {
2765 ret = snd_soc_dapm_new_control(dapm, widget);
2766 if (ret < 0) {
2767 dev_err(dapm->dev,
2768 "ASoC: Failed to create DAPM control %s: %d\n",
2769 widget->name, ret);
2770 return ret;
2771 }
2772 widget++;
2773 }
2774 return 0;
2775 }
2776 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2777
2778 static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
2779 const char *stream, int event)
2780 {
2781 struct snd_soc_dapm_widget *w;
2782
2783 list_for_each_entry(w, &dapm->card->widgets, list)
2784 {
2785 if (!w->sname || w->dapm != dapm)
2786 continue;
2787 dev_vdbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2788 w->name, w->sname, stream, event);
2789 if (strstr(w->sname, stream)) {
2790 dapm_mark_dirty(w, "stream event");
2791 switch(event) {
2792 case SND_SOC_DAPM_STREAM_START:
2793 w->active = 1;
2794 break;
2795 case SND_SOC_DAPM_STREAM_STOP:
2796 w->active = 0;
2797 break;
2798 case SND_SOC_DAPM_STREAM_SUSPEND:
2799 case SND_SOC_DAPM_STREAM_RESUME:
2800 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2801 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2802 break;
2803 }
2804 }
2805 }
2806
2807 dapm_power_widgets(dapm, event);
2808
2809 /* do we need to notify any clients that DAPM stream is complete */
2810 if (dapm->stream_event)
2811 dapm->stream_event(dapm, event);
2812 }
2813
2814 /**
2815 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2816 * @rtd: PCM runtime data
2817 * @stream: stream name
2818 * @event: stream event
2819 *
2820 * Sends a stream event to the dapm core. The core then makes any
2821 * necessary widget power changes.
2822 *
2823 * Returns 0 for success else error.
2824 */
2825 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2826 const char *stream, int event)
2827 {
2828 struct snd_soc_codec *codec = rtd->codec;
2829
2830 if (stream == NULL)
2831 return 0;
2832
2833 mutex_lock(&codec->mutex);
2834 soc_dapm_stream_event(&codec->dapm, stream, event);
2835 mutex_unlock(&codec->mutex);
2836 return 0;
2837 }
2838
2839 /**
2840 * snd_soc_dapm_enable_pin - enable pin.
2841 * @dapm: DAPM context
2842 * @pin: pin name
2843 *
2844 * Enables input/output pin and its parents or children widgets iff there is
2845 * a valid audio route and active audio stream.
2846 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2847 * do any widget power switching.
2848 */
2849 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2850 {
2851 return snd_soc_dapm_set_pin(dapm, pin, 1);
2852 }
2853 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2854
2855 /**
2856 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2857 * @dapm: DAPM context
2858 * @pin: pin name
2859 *
2860 * Enables input/output pin regardless of any other state. This is
2861 * intended for use with microphone bias supplies used in microphone
2862 * jack detection.
2863 *
2864 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2865 * do any widget power switching.
2866 */
2867 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2868 const char *pin)
2869 {
2870 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2871
2872 if (!w) {
2873 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2874 return -EINVAL;
2875 }
2876
2877 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2878 w->connected = 1;
2879 w->force = 1;
2880 dapm_mark_dirty(w, "force enable");
2881
2882 return 0;
2883 }
2884 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2885
2886 /**
2887 * snd_soc_dapm_disable_pin - disable pin.
2888 * @dapm: DAPM context
2889 * @pin: pin name
2890 *
2891 * Disables input/output pin and its parents or children widgets.
2892 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2893 * do any widget power switching.
2894 */
2895 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2896 const char *pin)
2897 {
2898 return snd_soc_dapm_set_pin(dapm, pin, 0);
2899 }
2900 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2901
2902 /**
2903 * snd_soc_dapm_nc_pin - permanently disable pin.
2904 * @dapm: DAPM context
2905 * @pin: pin name
2906 *
2907 * Marks the specified pin as being not connected, disabling it along
2908 * any parent or child widgets. At present this is identical to
2909 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2910 * additional things such as disabling controls which only affect
2911 * paths through the pin.
2912 *
2913 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2914 * do any widget power switching.
2915 */
2916 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2917 {
2918 return snd_soc_dapm_set_pin(dapm, pin, 0);
2919 }
2920 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2921
2922 /**
2923 * snd_soc_dapm_get_pin_status - get audio pin status
2924 * @dapm: DAPM context
2925 * @pin: audio signal pin endpoint (or start point)
2926 *
2927 * Get audio pin status - connected or disconnected.
2928 *
2929 * Returns 1 for connected otherwise 0.
2930 */
2931 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2932 const char *pin)
2933 {
2934 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2935
2936 if (w)
2937 return w->connected;
2938
2939 return 0;
2940 }
2941 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
2942
2943 /**
2944 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2945 * @dapm: DAPM context
2946 * @pin: audio signal pin endpoint (or start point)
2947 *
2948 * Mark the given endpoint or pin as ignoring suspend. When the
2949 * system is disabled a path between two endpoints flagged as ignoring
2950 * suspend will not be disabled. The path must already be enabled via
2951 * normal means at suspend time, it will not be turned on if it was not
2952 * already enabled.
2953 */
2954 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2955 const char *pin)
2956 {
2957 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
2958
2959 if (!w) {
2960 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2961 return -EINVAL;
2962 }
2963
2964 w->ignore_suspend = 1;
2965
2966 return 0;
2967 }
2968 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2969
2970 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
2971 struct snd_soc_dapm_widget *w)
2972 {
2973 struct snd_soc_dapm_path *p;
2974
2975 list_for_each_entry(p, &card->paths, list) {
2976 if ((p->source == w) || (p->sink == w)) {
2977 dev_dbg(card->dev,
2978 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
2979 p->source->name, p->source->id, p->source->dapm,
2980 p->sink->name, p->sink->id, p->sink->dapm);
2981
2982 /* Connected to something other than the codec */
2983 if (p->source->dapm != p->sink->dapm)
2984 return true;
2985 /*
2986 * Loopback connection from codec external pin to
2987 * codec external pin
2988 */
2989 if (p->sink->id == snd_soc_dapm_input) {
2990 switch (p->source->id) {
2991 case snd_soc_dapm_output:
2992 case snd_soc_dapm_micbias:
2993 return true;
2994 default:
2995 break;
2996 }
2997 }
2998 }
2999 }
3000
3001 return false;
3002 }
3003
3004 /**
3005 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3006 * @codec: The codec whose pins should be processed
3007 *
3008 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3009 * which are unused. Pins are used if they are connected externally to the
3010 * codec, whether that be to some other device, or a loop-back connection to
3011 * the codec itself.
3012 */
3013 void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3014 {
3015 struct snd_soc_card *card = codec->card;
3016 struct snd_soc_dapm_context *dapm = &codec->dapm;
3017 struct snd_soc_dapm_widget *w;
3018
3019 dev_dbg(codec->dev, "Auto NC: DAPMs: card:%p codec:%p\n",
3020 &card->dapm, &codec->dapm);
3021
3022 list_for_each_entry(w, &card->widgets, list) {
3023 if (w->dapm != dapm)
3024 continue;
3025 switch (w->id) {
3026 case snd_soc_dapm_input:
3027 case snd_soc_dapm_output:
3028 case snd_soc_dapm_micbias:
3029 dev_dbg(codec->dev, "Auto NC: Checking widget %s\n",
3030 w->name);
3031 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
3032 dev_dbg(codec->dev,
3033 "... Not in map; disabling\n");
3034 snd_soc_dapm_nc_pin(dapm, w->name);
3035 }
3036 break;
3037 default:
3038 break;
3039 }
3040 }
3041 }
3042
3043 /**
3044 * snd_soc_dapm_free - free dapm resources
3045 * @dapm: DAPM context
3046 *
3047 * Free all dapm widgets and resources.
3048 */
3049 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
3050 {
3051 snd_soc_dapm_sys_remove(dapm->dev);
3052 dapm_debugfs_cleanup(dapm);
3053 dapm_free_widgets(dapm);
3054 list_del(&dapm->list);
3055 }
3056 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3057
3058 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
3059 {
3060 struct snd_soc_dapm_widget *w;
3061 LIST_HEAD(down_list);
3062 int powerdown = 0;
3063
3064 list_for_each_entry(w, &dapm->card->widgets, list) {
3065 if (w->dapm != dapm)
3066 continue;
3067 if (w->power) {
3068 dapm_seq_insert(w, &down_list, false);
3069 w->power = 0;
3070 powerdown = 1;
3071 }
3072 }
3073
3074 /* If there were no widgets to power down we're already in
3075 * standby.
3076 */
3077 if (powerdown) {
3078 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
3079 dapm_seq_run(dapm, &down_list, 0, false);
3080 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
3081 }
3082 }
3083
3084 /*
3085 * snd_soc_dapm_shutdown - callback for system shutdown
3086 */
3087 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3088 {
3089 struct snd_soc_codec *codec;
3090
3091 list_for_each_entry(codec, &card->codec_dev_list, list) {
3092 soc_dapm_shutdown_codec(&codec->dapm);
3093 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
3094 }
3095 }
3096
3097 /* Module information */
3098 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3099 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3100 MODULE_LICENSE("GPL");
This page took 0.178027 seconds and 6 git commands to generate.