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