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