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