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