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