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