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