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