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