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