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