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