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