ASoC: Factor out application of power for generic widgets
[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  *      DAC's/ADC's.
16  *    o Platform power domain - can support external components i.e. amps and
17  *      mic/meadphone insertion events.
18  *    o Automatic Mic Bias support
19  *    o Jack insertion power event initiation - e.g. hp insertion will enable
20  *      sinks, dacs, etc
21  *    o Delayed powerdown of audio susbsystem to reduce pops between a quick
22  *      device reopen.
23  *
24  *  Todo:
25  *    o DAPM power change sequencing - allow for configurable per
26  *      codec sequences.
27  *    o Support for analogue bias optimisation.
28  *    o Support for reduced codec oversampling rates.
29  *    o Support for reduced codec bias currents.
30  */
31
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/init.h>
35 #include <linux/delay.h>
36 #include <linux/pm.h>
37 #include <linux/bitops.h>
38 #include <linux/platform_device.h>
39 #include <linux/jiffies.h>
40 #include <sound/core.h>
41 #include <sound/pcm.h>
42 #include <sound/pcm_params.h>
43 #include <sound/soc-dapm.h>
44 #include <sound/initval.h>
45
46 /* debug */
47 #ifdef DEBUG
48 #define dump_dapm(codec, action) dbg_dump_dapm(codec, action)
49 #else
50 #define dump_dapm(codec, action)
51 #endif
52
53 /* dapm power sequences - make this per codec in the future */
54 static int dapm_up_seq[] = {
55         snd_soc_dapm_pre, snd_soc_dapm_micbias, snd_soc_dapm_mic,
56         snd_soc_dapm_mux, snd_soc_dapm_value_mux, snd_soc_dapm_dac,
57         snd_soc_dapm_mixer, snd_soc_dapm_mixer_named_ctl, snd_soc_dapm_pga,
58         snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk, snd_soc_dapm_post
59 };
60
61 static int dapm_down_seq[] = {
62         snd_soc_dapm_pre, snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk,
63         snd_soc_dapm_pga, snd_soc_dapm_mixer_named_ctl, snd_soc_dapm_mixer,
64         snd_soc_dapm_dac, snd_soc_dapm_mic, snd_soc_dapm_micbias,
65         snd_soc_dapm_mux, snd_soc_dapm_value_mux, snd_soc_dapm_post
66 };
67
68 static int dapm_status = 1;
69 module_param(dapm_status, int, 0);
70 MODULE_PARM_DESC(dapm_status, "enable DPM sysfs entries");
71
72 static void pop_wait(u32 pop_time)
73 {
74         if (pop_time)
75                 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
76 }
77
78 static void pop_dbg(u32 pop_time, const char *fmt, ...)
79 {
80         va_list args;
81
82         va_start(args, fmt);
83
84         if (pop_time) {
85                 vprintk(fmt, args);
86                 pop_wait(pop_time);
87         }
88
89         va_end(args);
90 }
91
92 /* create a new dapm widget */
93 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
94         const struct snd_soc_dapm_widget *_widget)
95 {
96         return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
97 }
98
99 /* set up initial codec paths */
100 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
101         struct snd_soc_dapm_path *p, int i)
102 {
103         switch (w->id) {
104         case snd_soc_dapm_switch:
105         case snd_soc_dapm_mixer:
106         case snd_soc_dapm_mixer_named_ctl: {
107                 int val;
108                 struct soc_mixer_control *mc = (struct soc_mixer_control *)
109                         w->kcontrols[i].private_value;
110                 unsigned int reg = mc->reg;
111                 unsigned int shift = mc->shift;
112                 int max = mc->max;
113                 unsigned int mask = (1 << fls(max)) - 1;
114                 unsigned int invert = mc->invert;
115
116                 val = snd_soc_read(w->codec, reg);
117                 val = (val >> shift) & mask;
118
119                 if ((invert && !val) || (!invert && val))
120                         p->connect = 1;
121                 else
122                         p->connect = 0;
123         }
124         break;
125         case snd_soc_dapm_mux: {
126                 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
127                 int val, item, bitmask;
128
129                 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
130                 ;
131                 val = snd_soc_read(w->codec, e->reg);
132                 item = (val >> e->shift_l) & (bitmask - 1);
133
134                 p->connect = 0;
135                 for (i = 0; i < e->max; i++) {
136                         if (!(strcmp(p->name, e->texts[i])) && item == i)
137                                 p->connect = 1;
138                 }
139         }
140         break;
141         case snd_soc_dapm_value_mux: {
142                 struct soc_enum *e = (struct soc_enum *)
143                         w->kcontrols[i].private_value;
144                 int val, item;
145
146                 val = snd_soc_read(w->codec, e->reg);
147                 val = (val >> e->shift_l) & e->mask;
148                 for (item = 0; item < e->max; item++) {
149                         if (val == e->values[item])
150                                 break;
151                 }
152
153                 p->connect = 0;
154                 for (i = 0; i < e->max; i++) {
155                         if (!(strcmp(p->name, e->texts[i])) && item == i)
156                                 p->connect = 1;
157                 }
158         }
159         break;
160         /* does not effect routing - always connected */
161         case snd_soc_dapm_pga:
162         case snd_soc_dapm_output:
163         case snd_soc_dapm_adc:
164         case snd_soc_dapm_input:
165         case snd_soc_dapm_dac:
166         case snd_soc_dapm_micbias:
167         case snd_soc_dapm_vmid:
168                 p->connect = 1;
169         break;
170         /* does effect routing - dynamically connected */
171         case snd_soc_dapm_hp:
172         case snd_soc_dapm_mic:
173         case snd_soc_dapm_spk:
174         case snd_soc_dapm_line:
175         case snd_soc_dapm_pre:
176         case snd_soc_dapm_post:
177                 p->connect = 0;
178         break;
179         }
180 }
181
182 /* connect mux widget to it's interconnecting audio paths */
183 static int dapm_connect_mux(struct snd_soc_codec *codec,
184         struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
185         struct snd_soc_dapm_path *path, const char *control_name,
186         const struct snd_kcontrol_new *kcontrol)
187 {
188         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
189         int i;
190
191         for (i = 0; i < e->max; i++) {
192                 if (!(strcmp(control_name, e->texts[i]))) {
193                         list_add(&path->list, &codec->dapm_paths);
194                         list_add(&path->list_sink, &dest->sources);
195                         list_add(&path->list_source, &src->sinks);
196                         path->name = (char*)e->texts[i];
197                         dapm_set_path_status(dest, path, 0);
198                         return 0;
199                 }
200         }
201
202         return -ENODEV;
203 }
204
205 /* connect mixer widget to it's interconnecting audio paths */
206 static int dapm_connect_mixer(struct snd_soc_codec *codec,
207         struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
208         struct snd_soc_dapm_path *path, const char *control_name)
209 {
210         int i;
211
212         /* search for mixer kcontrol */
213         for (i = 0; i < dest->num_kcontrols; i++) {
214                 if (!strcmp(control_name, dest->kcontrols[i].name)) {
215                         list_add(&path->list, &codec->dapm_paths);
216                         list_add(&path->list_sink, &dest->sources);
217                         list_add(&path->list_source, &src->sinks);
218                         path->name = dest->kcontrols[i].name;
219                         dapm_set_path_status(dest, path, i);
220                         return 0;
221                 }
222         }
223         return -ENODEV;
224 }
225
226 /* update dapm codec register bits */
227 static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
228 {
229         int change, power;
230         unsigned short old, new;
231         struct snd_soc_codec *codec = widget->codec;
232
233         /* check for valid widgets */
234         if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
235                 widget->id == snd_soc_dapm_output ||
236                 widget->id == snd_soc_dapm_hp ||
237                 widget->id == snd_soc_dapm_mic ||
238                 widget->id == snd_soc_dapm_line ||
239                 widget->id == snd_soc_dapm_spk)
240                 return 0;
241
242         power = widget->power;
243         if (widget->invert)
244                 power = (power ? 0:1);
245
246         old = snd_soc_read(codec, widget->reg);
247         new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
248
249         change = old != new;
250         if (change) {
251                 pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n",
252                         widget->name, widget->power ? "on" : "off",
253                         codec->pop_time);
254                 snd_soc_write(codec, widget->reg, new);
255                 pop_wait(codec->pop_time);
256         }
257         pr_debug("reg %x old %x new %x change %d\n", widget->reg,
258                  old, new, change);
259         return change;
260 }
261
262 /* ramps the volume up or down to minimise pops before or after a
263  * DAPM power event */
264 static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power)
265 {
266         const struct snd_kcontrol_new *k = widget->kcontrols;
267
268         if (widget->muted && !power)
269                 return 0;
270         if (!widget->muted && power)
271                 return 0;
272
273         if (widget->num_kcontrols && k) {
274                 struct soc_mixer_control *mc =
275                         (struct soc_mixer_control *)k->private_value;
276                 unsigned int reg = mc->reg;
277                 unsigned int shift = mc->shift;
278                 int max = mc->max;
279                 unsigned int mask = (1 << fls(max)) - 1;
280                 unsigned int invert = mc->invert;
281
282                 if (power) {
283                         int i;
284                         /* power up has happended, increase volume to last level */
285                         if (invert) {
286                                 for (i = max; i > widget->saved_value; i--)
287                                         snd_soc_update_bits(widget->codec, reg, mask, i);
288                         } else {
289                                 for (i = 0; i < widget->saved_value; i++)
290                                         snd_soc_update_bits(widget->codec, reg, mask, i);
291                         }
292                         widget->muted = 0;
293                 } else {
294                         /* power down is about to occur, decrease volume to mute */
295                         int val = snd_soc_read(widget->codec, reg);
296                         int i = widget->saved_value = (val >> shift) & mask;
297                         if (invert) {
298                                 for (; i < mask; i++)
299                                         snd_soc_update_bits(widget->codec, reg, mask, i);
300                         } else {
301                                 for (; i > 0; i--)
302                                         snd_soc_update_bits(widget->codec, reg, mask, i);
303                         }
304                         widget->muted = 1;
305                 }
306         }
307         return 0;
308 }
309
310 /* create new dapm mixer control */
311 static int dapm_new_mixer(struct snd_soc_codec *codec,
312         struct snd_soc_dapm_widget *w)
313 {
314         int i, ret = 0;
315         size_t name_len;
316         struct snd_soc_dapm_path *path;
317
318         /* add kcontrol */
319         for (i = 0; i < w->num_kcontrols; i++) {
320
321                 /* match name */
322                 list_for_each_entry(path, &w->sources, list_sink) {
323
324                         /* mixer/mux paths name must match control name */
325                         if (path->name != (char*)w->kcontrols[i].name)
326                                 continue;
327
328                         /* add dapm control with long name.
329                          * for dapm_mixer this is the concatenation of the
330                          * mixer and kcontrol name.
331                          * for dapm_mixer_named_ctl this is simply the
332                          * kcontrol name.
333                          */
334                         name_len = strlen(w->kcontrols[i].name) + 1;
335                         if (w->id != snd_soc_dapm_mixer_named_ctl)
336                                 name_len += 1 + strlen(w->name);
337
338                         path->long_name = kmalloc(name_len, GFP_KERNEL);
339
340                         if (path->long_name == NULL)
341                                 return -ENOMEM;
342
343                         switch (w->id) {
344                         default:
345                                 snprintf(path->long_name, name_len, "%s %s",
346                                          w->name, w->kcontrols[i].name);
347                                 break;
348                         case snd_soc_dapm_mixer_named_ctl:
349                                 snprintf(path->long_name, name_len, "%s",
350                                          w->kcontrols[i].name);
351                                 break;
352                         }
353
354                         path->long_name[name_len - 1] = '\0';
355
356                         path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
357                                 path->long_name);
358                         ret = snd_ctl_add(codec->card, path->kcontrol);
359                         if (ret < 0) {
360                                 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s: %d\n",
361                                        path->long_name,
362                                        ret);
363                                 kfree(path->long_name);
364                                 path->long_name = NULL;
365                                 return ret;
366                         }
367                 }
368         }
369         return ret;
370 }
371
372 /* create new dapm mux control */
373 static int dapm_new_mux(struct snd_soc_codec *codec,
374         struct snd_soc_dapm_widget *w)
375 {
376         struct snd_soc_dapm_path *path = NULL;
377         struct snd_kcontrol *kcontrol;
378         int ret = 0;
379
380         if (!w->num_kcontrols) {
381                 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
382                 return -EINVAL;
383         }
384
385         kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
386         ret = snd_ctl_add(codec->card, kcontrol);
387         if (ret < 0)
388                 goto err;
389
390         list_for_each_entry(path, &w->sources, list_sink)
391                 path->kcontrol = kcontrol;
392
393         return ret;
394
395 err:
396         printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
397         return ret;
398 }
399
400 /* create new dapm volume control */
401 static int dapm_new_pga(struct snd_soc_codec *codec,
402         struct snd_soc_dapm_widget *w)
403 {
404         struct snd_kcontrol *kcontrol;
405         int ret = 0;
406
407         if (!w->num_kcontrols)
408                 return -EINVAL;
409
410         kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
411         ret = snd_ctl_add(codec->card, kcontrol);
412         if (ret < 0) {
413                 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
414                 return ret;
415         }
416
417         return ret;
418 }
419
420 /* reset 'walked' bit for each dapm path */
421 static inline void dapm_clear_walk(struct snd_soc_codec *codec)
422 {
423         struct snd_soc_dapm_path *p;
424
425         list_for_each_entry(p, &codec->dapm_paths, list)
426                 p->walked = 0;
427 }
428
429 /*
430  * Recursively check for a completed path to an active or physically connected
431  * output widget. Returns number of complete paths.
432  */
433 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
434 {
435         struct snd_soc_dapm_path *path;
436         int con = 0;
437
438         if (widget->id == snd_soc_dapm_adc && widget->active)
439                 return 1;
440
441         if (widget->connected) {
442                 /* connected pin ? */
443                 if (widget->id == snd_soc_dapm_output && !widget->ext)
444                         return 1;
445
446                 /* connected jack or spk ? */
447                 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
448                         widget->id == snd_soc_dapm_line)
449                         return 1;
450         }
451
452         list_for_each_entry(path, &widget->sinks, list_source) {
453                 if (path->walked)
454                         continue;
455
456                 if (path->sink && path->connect) {
457                         path->walked = 1;
458                         con += is_connected_output_ep(path->sink);
459                 }
460         }
461
462         return con;
463 }
464
465 /*
466  * Recursively check for a completed path to an active or physically connected
467  * input widget. Returns number of complete paths.
468  */
469 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
470 {
471         struct snd_soc_dapm_path *path;
472         int con = 0;
473
474         /* active stream ? */
475         if (widget->id == snd_soc_dapm_dac && widget->active)
476                 return 1;
477
478         if (widget->connected) {
479                 /* connected pin ? */
480                 if (widget->id == snd_soc_dapm_input && !widget->ext)
481                         return 1;
482
483                 /* connected VMID/Bias for lower pops */
484                 if (widget->id == snd_soc_dapm_vmid)
485                         return 1;
486
487                 /* connected jack ? */
488                 if (widget->id == snd_soc_dapm_mic || widget->id == snd_soc_dapm_line)
489                         return 1;
490         }
491
492         list_for_each_entry(path, &widget->sources, list_sink) {
493                 if (path->walked)
494                         continue;
495
496                 if (path->source && path->connect) {
497                         path->walked = 1;
498                         con += is_connected_input_ep(path->source);
499                 }
500         }
501
502         return con;
503 }
504
505 /*
506  * Handler for generic register modifier widget.
507  */
508 int dapm_reg_event(struct snd_soc_dapm_widget *w,
509                    struct snd_kcontrol *kcontrol, int event)
510 {
511         unsigned int val;
512
513         if (SND_SOC_DAPM_EVENT_ON(event))
514                 val = w->on_val;
515         else
516                 val = w->off_val;
517
518         snd_soc_update_bits(w->codec, -(w->reg + 1),
519                             w->mask << w->shift, val << w->shift);
520
521         return 0;
522 }
523 EXPORT_SYMBOL_GPL(dapm_reg_event);
524
525 /* Standard power change method, used to apply power changes to most
526  * widgets.
527  */
528 static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
529 {
530         int ret;
531
532         /* call any power change event handlers */
533         if (w->event)
534                 pr_debug("power %s event for %s flags %x\n",
535                          w->power ? "on" : "off",
536                          w->name, w->event_flags);
537
538         /* power up pre event */
539         if (w->power && w->event &&
540             (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
541                 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
542                 if (ret < 0)
543                         return ret;
544         }
545
546         /* power down pre event */
547         if (!w->power && w->event &&
548             (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
549                 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
550                 if (ret < 0)
551                         return ret;
552         }
553
554         /* Lower PGA volume to reduce pops */
555         if (w->id == snd_soc_dapm_pga && !w->power)
556                 dapm_set_pga(w, w->power);
557
558         dapm_update_bits(w);
559
560         /* Raise PGA volume to reduce pops */
561         if (w->id == snd_soc_dapm_pga && w->power)
562                 dapm_set_pga(w, w->power);
563
564         /* power up post event */
565         if (w->power && w->event &&
566             (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
567                 ret = w->event(w,
568                                NULL, SND_SOC_DAPM_POST_PMU);
569                 if (ret < 0)
570                         return ret;
571         }
572
573         /* power down post event */
574         if (!w->power && w->event &&
575             (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
576                 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
577                 if (ret < 0)
578                         return ret;
579         }
580
581         return 0;
582 }
583
584 /*
585  * Scan a single DAPM widget for a complete audio path and update the
586  * power status appropriately.
587  */
588 static int dapm_power_widget(struct snd_soc_codec *codec, int event,
589                              struct snd_soc_dapm_widget *w)
590 {
591         int in, out, power_change, power, ret;
592
593         /* vmid - no action */
594         if (w->id == snd_soc_dapm_vmid)
595                 return 0;
596
597         /* active ADC */
598         if (w->id == snd_soc_dapm_adc && w->active) {
599                 in = is_connected_input_ep(w);
600                 dapm_clear_walk(w->codec);
601                 w->power = (in != 0) ? 1 : 0;
602                 dapm_update_bits(w);
603                 return 0;
604         }
605
606         /* active DAC */
607         if (w->id == snd_soc_dapm_dac && w->active) {
608                 out = is_connected_output_ep(w);
609                 dapm_clear_walk(w->codec);
610                 w->power = (out != 0) ? 1 : 0;
611                 dapm_update_bits(w);
612                 return 0;
613         }
614
615         /* pre and post event widgets */
616         if (w->id == snd_soc_dapm_pre) {
617                 if (!w->event)
618                         return 0;
619
620                 if (event == SND_SOC_DAPM_STREAM_START) {
621                         ret = w->event(w,
622                                        NULL, SND_SOC_DAPM_PRE_PMU);
623                         if (ret < 0)
624                                 return ret;
625                 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
626                         ret = w->event(w,
627                                        NULL, SND_SOC_DAPM_PRE_PMD);
628                         if (ret < 0)
629                                 return ret;
630                 }
631                 return 0;
632         }
633         if (w->id == snd_soc_dapm_post) {
634                 if (!w->event)
635                         return 0;
636
637                 if (event == SND_SOC_DAPM_STREAM_START) {
638                         ret = w->event(w,
639                                        NULL, SND_SOC_DAPM_POST_PMU);
640                         if (ret < 0)
641                                 return ret;
642                 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
643                         ret = w->event(w,
644                                        NULL, SND_SOC_DAPM_POST_PMD);
645                         if (ret < 0)
646                                 return ret;
647                 }
648                 return 0;
649         }
650
651         /* all other widgets */
652         in = is_connected_input_ep(w);
653         dapm_clear_walk(w->codec);
654         out = is_connected_output_ep(w);
655         dapm_clear_walk(w->codec);
656         power = (out != 0 && in != 0) ? 1 : 0;
657         power_change = (w->power == power) ? 0 : 1;
658         w->power = power;
659
660         if (!power_change)
661                 return 0;
662
663         return dapm_generic_apply_power(w);
664 }
665
666 /*
667  * Scan each dapm widget for complete audio path.
668  * A complete path is a route that has valid endpoints i.e.:-
669  *
670  *  o DAC to output pin.
671  *  o Input Pin to ADC.
672  *  o Input pin to Output pin (bypass, sidetone)
673  *  o DAC to ADC (loopback).
674  */
675 static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
676 {
677         struct snd_soc_dapm_widget *w;
678         int i, c = 1, *seq = NULL, ret = 0;
679
680         /* do we have a sequenced stream event */
681         if (event == SND_SOC_DAPM_STREAM_START) {
682                 c = ARRAY_SIZE(dapm_up_seq);
683                 seq = dapm_up_seq;
684         } else if (event == SND_SOC_DAPM_STREAM_STOP) {
685                 c = ARRAY_SIZE(dapm_down_seq);
686                 seq = dapm_down_seq;
687         }
688
689         for (i = 0; i < c; i++) {
690                 list_for_each_entry(w, &codec->dapm_widgets, list) {
691
692                         /* is widget in stream order */
693                         if (seq && seq[i] && w->id != seq[i])
694                                 continue;
695
696                         ret = dapm_power_widget(codec, event, w);
697                         if (ret != 0)
698                                 return ret;
699                 }
700         }
701
702         return 0;
703 }
704
705 #ifdef DEBUG
706 static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
707 {
708         struct snd_soc_dapm_widget *w;
709         struct snd_soc_dapm_path *p = NULL;
710         int in, out;
711
712         printk("DAPM %s %s\n", codec->name, action);
713
714         list_for_each_entry(w, &codec->dapm_widgets, list) {
715
716                 /* only display widgets that effect routing */
717                 switch (w->id) {
718                 case snd_soc_dapm_pre:
719                 case snd_soc_dapm_post:
720                 case snd_soc_dapm_vmid:
721                         continue;
722                 case snd_soc_dapm_mux:
723                 case snd_soc_dapm_value_mux:
724                 case snd_soc_dapm_output:
725                 case snd_soc_dapm_input:
726                 case snd_soc_dapm_switch:
727                 case snd_soc_dapm_hp:
728                 case snd_soc_dapm_mic:
729                 case snd_soc_dapm_spk:
730                 case snd_soc_dapm_line:
731                 case snd_soc_dapm_micbias:
732                 case snd_soc_dapm_dac:
733                 case snd_soc_dapm_adc:
734                 case snd_soc_dapm_pga:
735                 case snd_soc_dapm_mixer:
736                 case snd_soc_dapm_mixer_named_ctl:
737                         if (w->name) {
738                                 in = is_connected_input_ep(w);
739                                 dapm_clear_walk(w->codec);
740                                 out = is_connected_output_ep(w);
741                                 dapm_clear_walk(w->codec);
742                                 printk("%s: %s  in %d out %d\n", w->name,
743                                         w->power ? "On":"Off",in, out);
744
745                                 list_for_each_entry(p, &w->sources, list_sink) {
746                                         if (p->connect)
747                                                 printk(" in  %s %s\n", p->name ? p->name : "static",
748                                                         p->source->name);
749                                 }
750                                 list_for_each_entry(p, &w->sinks, list_source) {
751                                         if (p->connect)
752                                                 printk(" out %s %s\n", p->name ? p->name : "static",
753                                                         p->sink->name);
754                                 }
755                         }
756                 break;
757                 }
758         }
759 }
760 #endif
761
762 /* test and update the power status of a mux widget */
763 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
764                                  struct snd_kcontrol *kcontrol, int mask,
765                                  int mux, int val, struct soc_enum *e)
766 {
767         struct snd_soc_dapm_path *path;
768         int found = 0;
769
770         if (widget->id != snd_soc_dapm_mux &&
771             widget->id != snd_soc_dapm_value_mux)
772                 return -ENODEV;
773
774         if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
775                 return 0;
776
777         /* find dapm widget path assoc with kcontrol */
778         list_for_each_entry(path, &widget->codec->dapm_paths, list) {
779                 if (path->kcontrol != kcontrol)
780                         continue;
781
782                 if (!path->name || !e->texts[mux])
783                         continue;
784
785                 found = 1;
786                 /* we now need to match the string in the enum to the path */
787                 if (!(strcmp(path->name, e->texts[mux])))
788                         path->connect = 1; /* new connection */
789                 else
790                         path->connect = 0; /* old connection must be powered down */
791         }
792
793         if (found) {
794                 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
795                 dump_dapm(widget->codec, "mux power update");
796         }
797
798         return 0;
799 }
800
801 /* test and update the power status of a mixer or switch widget */
802 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
803                                    struct snd_kcontrol *kcontrol, int reg,
804                                    int val_mask, int val, int invert)
805 {
806         struct snd_soc_dapm_path *path;
807         int found = 0;
808
809         if (widget->id != snd_soc_dapm_mixer &&
810             widget->id != snd_soc_dapm_mixer_named_ctl &&
811             widget->id != snd_soc_dapm_switch)
812                 return -ENODEV;
813
814         if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
815                 return 0;
816
817         /* find dapm widget path assoc with kcontrol */
818         list_for_each_entry(path, &widget->codec->dapm_paths, list) {
819                 if (path->kcontrol != kcontrol)
820                         continue;
821
822                 /* found, now check type */
823                 found = 1;
824                 if (val)
825                         /* new connection */
826                         path->connect = invert ? 0:1;
827                 else
828                         /* old connection must be powered down */
829                         path->connect = invert ? 1:0;
830                 break;
831         }
832
833         if (found) {
834                 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
835                 dump_dapm(widget->codec, "mixer power update");
836         }
837
838         return 0;
839 }
840
841 /* show dapm widget status in sys fs */
842 static ssize_t dapm_widget_show(struct device *dev,
843         struct device_attribute *attr, char *buf)
844 {
845         struct snd_soc_device *devdata = dev_get_drvdata(dev);
846         struct snd_soc_codec *codec = devdata->card->codec;
847         struct snd_soc_dapm_widget *w;
848         int count = 0;
849         char *state = "not set";
850
851         list_for_each_entry(w, &codec->dapm_widgets, list) {
852
853                 /* only display widgets that burnm power */
854                 switch (w->id) {
855                 case snd_soc_dapm_hp:
856                 case snd_soc_dapm_mic:
857                 case snd_soc_dapm_spk:
858                 case snd_soc_dapm_line:
859                 case snd_soc_dapm_micbias:
860                 case snd_soc_dapm_dac:
861                 case snd_soc_dapm_adc:
862                 case snd_soc_dapm_pga:
863                 case snd_soc_dapm_mixer:
864                 case snd_soc_dapm_mixer_named_ctl:
865                         if (w->name)
866                                 count += sprintf(buf + count, "%s: %s\n",
867                                         w->name, w->power ? "On":"Off");
868                 break;
869                 default:
870                 break;
871                 }
872         }
873
874         switch (codec->bias_level) {
875         case SND_SOC_BIAS_ON:
876                 state = "On";
877                 break;
878         case SND_SOC_BIAS_PREPARE:
879                 state = "Prepare";
880                 break;
881         case SND_SOC_BIAS_STANDBY:
882                 state = "Standby";
883                 break;
884         case SND_SOC_BIAS_OFF:
885                 state = "Off";
886                 break;
887         }
888         count += sprintf(buf + count, "PM State: %s\n", state);
889
890         return count;
891 }
892
893 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
894
895 int snd_soc_dapm_sys_add(struct device *dev)
896 {
897         if (!dapm_status)
898                 return 0;
899         return device_create_file(dev, &dev_attr_dapm_widget);
900 }
901
902 static void snd_soc_dapm_sys_remove(struct device *dev)
903 {
904         if (dapm_status) {
905                 device_remove_file(dev, &dev_attr_dapm_widget);
906         }
907 }
908
909 /* free all dapm widgets and resources */
910 static void dapm_free_widgets(struct snd_soc_codec *codec)
911 {
912         struct snd_soc_dapm_widget *w, *next_w;
913         struct snd_soc_dapm_path *p, *next_p;
914
915         list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
916                 list_del(&w->list);
917                 kfree(w);
918         }
919
920         list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
921                 list_del(&p->list);
922                 kfree(p->long_name);
923                 kfree(p);
924         }
925 }
926
927 static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
928                                 const char *pin, int status)
929 {
930         struct snd_soc_dapm_widget *w;
931
932         list_for_each_entry(w, &codec->dapm_widgets, list) {
933                 if (!strcmp(w->name, pin)) {
934                         pr_debug("dapm: %s: pin %s\n", codec->name, pin);
935                         w->connected = status;
936                         return 0;
937                 }
938         }
939
940         pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
941         return -EINVAL;
942 }
943
944 /**
945  * snd_soc_dapm_sync - scan and power dapm paths
946  * @codec: audio codec
947  *
948  * Walks all dapm audio paths and powers widgets according to their
949  * stream or path usage.
950  *
951  * Returns 0 for success.
952  */
953 int snd_soc_dapm_sync(struct snd_soc_codec *codec)
954 {
955         int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
956         dump_dapm(codec, "sync");
957         return ret;
958 }
959 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
960
961 static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
962         const char *sink, const char *control, const char *source)
963 {
964         struct snd_soc_dapm_path *path;
965         struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
966         int ret = 0;
967
968         /* find src and dest widgets */
969         list_for_each_entry(w, &codec->dapm_widgets, list) {
970
971                 if (!wsink && !(strcmp(w->name, sink))) {
972                         wsink = w;
973                         continue;
974                 }
975                 if (!wsource && !(strcmp(w->name, source))) {
976                         wsource = w;
977                 }
978         }
979
980         if (wsource == NULL || wsink == NULL)
981                 return -ENODEV;
982
983         path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
984         if (!path)
985                 return -ENOMEM;
986
987         path->source = wsource;
988         path->sink = wsink;
989         INIT_LIST_HEAD(&path->list);
990         INIT_LIST_HEAD(&path->list_source);
991         INIT_LIST_HEAD(&path->list_sink);
992
993         /* check for external widgets */
994         if (wsink->id == snd_soc_dapm_input) {
995                 if (wsource->id == snd_soc_dapm_micbias ||
996                         wsource->id == snd_soc_dapm_mic ||
997                         wsink->id == snd_soc_dapm_line ||
998                         wsink->id == snd_soc_dapm_output)
999                         wsink->ext = 1;
1000         }
1001         if (wsource->id == snd_soc_dapm_output) {
1002                 if (wsink->id == snd_soc_dapm_spk ||
1003                         wsink->id == snd_soc_dapm_hp ||
1004                         wsink->id == snd_soc_dapm_line ||
1005                         wsink->id == snd_soc_dapm_input)
1006                         wsource->ext = 1;
1007         }
1008
1009         /* connect static paths */
1010         if (control == NULL) {
1011                 list_add(&path->list, &codec->dapm_paths);
1012                 list_add(&path->list_sink, &wsink->sources);
1013                 list_add(&path->list_source, &wsource->sinks);
1014                 path->connect = 1;
1015                 return 0;
1016         }
1017
1018         /* connect dynamic paths */
1019         switch(wsink->id) {
1020         case snd_soc_dapm_adc:
1021         case snd_soc_dapm_dac:
1022         case snd_soc_dapm_pga:
1023         case snd_soc_dapm_input:
1024         case snd_soc_dapm_output:
1025         case snd_soc_dapm_micbias:
1026         case snd_soc_dapm_vmid:
1027         case snd_soc_dapm_pre:
1028         case snd_soc_dapm_post:
1029                 list_add(&path->list, &codec->dapm_paths);
1030                 list_add(&path->list_sink, &wsink->sources);
1031                 list_add(&path->list_source, &wsource->sinks);
1032                 path->connect = 1;
1033                 return 0;
1034         case snd_soc_dapm_mux:
1035         case snd_soc_dapm_value_mux:
1036                 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
1037                         &wsink->kcontrols[0]);
1038                 if (ret != 0)
1039                         goto err;
1040                 break;
1041         case snd_soc_dapm_switch:
1042         case snd_soc_dapm_mixer:
1043         case snd_soc_dapm_mixer_named_ctl:
1044                 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
1045                 if (ret != 0)
1046                         goto err;
1047                 break;
1048         case snd_soc_dapm_hp:
1049         case snd_soc_dapm_mic:
1050         case snd_soc_dapm_line:
1051         case snd_soc_dapm_spk:
1052                 list_add(&path->list, &codec->dapm_paths);
1053                 list_add(&path->list_sink, &wsink->sources);
1054                 list_add(&path->list_source, &wsource->sinks);
1055                 path->connect = 0;
1056                 return 0;
1057         }
1058         return 0;
1059
1060 err:
1061         printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1062                 control, sink);
1063         kfree(path);
1064         return ret;
1065 }
1066
1067 /**
1068  * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1069  * @codec: codec
1070  * @route: audio routes
1071  * @num: number of routes
1072  *
1073  * Connects 2 dapm widgets together via a named audio path. The sink is
1074  * the widget receiving the audio signal, whilst the source is the sender
1075  * of the audio signal.
1076  *
1077  * Returns 0 for success else error. On error all resources can be freed
1078  * with a call to snd_soc_card_free().
1079  */
1080 int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1081                             const struct snd_soc_dapm_route *route, int num)
1082 {
1083         int i, ret;
1084
1085         for (i = 0; i < num; i++) {
1086                 ret = snd_soc_dapm_add_route(codec, route->sink,
1087                                              route->control, route->source);
1088                 if (ret < 0) {
1089                         printk(KERN_ERR "Failed to add route %s->%s\n",
1090                                route->source,
1091                                route->sink);
1092                         return ret;
1093                 }
1094                 route++;
1095         }
1096
1097         return 0;
1098 }
1099 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1100
1101 /**
1102  * snd_soc_dapm_new_widgets - add new dapm widgets
1103  * @codec: audio codec
1104  *
1105  * Checks the codec for any new dapm widgets and creates them if found.
1106  *
1107  * Returns 0 for success.
1108  */
1109 int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1110 {
1111         struct snd_soc_dapm_widget *w;
1112
1113         list_for_each_entry(w, &codec->dapm_widgets, list)
1114         {
1115                 if (w->new)
1116                         continue;
1117
1118                 switch(w->id) {
1119                 case snd_soc_dapm_switch:
1120                 case snd_soc_dapm_mixer:
1121                 case snd_soc_dapm_mixer_named_ctl:
1122                         dapm_new_mixer(codec, w);
1123                         break;
1124                 case snd_soc_dapm_mux:
1125                 case snd_soc_dapm_value_mux:
1126                         dapm_new_mux(codec, w);
1127                         break;
1128                 case snd_soc_dapm_adc:
1129                 case snd_soc_dapm_dac:
1130                 case snd_soc_dapm_pga:
1131                         dapm_new_pga(codec, w);
1132                         break;
1133                 case snd_soc_dapm_input:
1134                 case snd_soc_dapm_output:
1135                 case snd_soc_dapm_micbias:
1136                 case snd_soc_dapm_spk:
1137                 case snd_soc_dapm_hp:
1138                 case snd_soc_dapm_mic:
1139                 case snd_soc_dapm_line:
1140                 case snd_soc_dapm_vmid:
1141                 case snd_soc_dapm_pre:
1142                 case snd_soc_dapm_post:
1143                         break;
1144                 }
1145                 w->new = 1;
1146         }
1147
1148         dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1149         return 0;
1150 }
1151 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1152
1153 /**
1154  * snd_soc_dapm_get_volsw - dapm mixer get callback
1155  * @kcontrol: mixer control
1156  * @ucontrol: control element information
1157  *
1158  * Callback to get the value of a dapm mixer control.
1159  *
1160  * Returns 0 for success.
1161  */
1162 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1163         struct snd_ctl_elem_value *ucontrol)
1164 {
1165         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1166         struct soc_mixer_control *mc =
1167                 (struct soc_mixer_control *)kcontrol->private_value;
1168         unsigned int reg = mc->reg;
1169         unsigned int shift = mc->shift;
1170         unsigned int rshift = mc->rshift;
1171         int max = mc->max;
1172         unsigned int invert = mc->invert;
1173         unsigned int mask = (1 << fls(max)) - 1;
1174
1175         /* return the saved value if we are powered down */
1176         if (widget->id == snd_soc_dapm_pga && !widget->power) {
1177                 ucontrol->value.integer.value[0] = widget->saved_value;
1178                 return 0;
1179         }
1180
1181         ucontrol->value.integer.value[0] =
1182                 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1183         if (shift != rshift)
1184                 ucontrol->value.integer.value[1] =
1185                         (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1186         if (invert) {
1187                 ucontrol->value.integer.value[0] =
1188                         max - ucontrol->value.integer.value[0];
1189                 if (shift != rshift)
1190                         ucontrol->value.integer.value[1] =
1191                                 max - ucontrol->value.integer.value[1];
1192         }
1193
1194         return 0;
1195 }
1196 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1197
1198 /**
1199  * snd_soc_dapm_put_volsw - dapm mixer set callback
1200  * @kcontrol: mixer control
1201  * @ucontrol: control element information
1202  *
1203  * Callback to set the value of a dapm mixer control.
1204  *
1205  * Returns 0 for success.
1206  */
1207 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1208         struct snd_ctl_elem_value *ucontrol)
1209 {
1210         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1211         struct soc_mixer_control *mc =
1212                 (struct soc_mixer_control *)kcontrol->private_value;
1213         unsigned int reg = mc->reg;
1214         unsigned int shift = mc->shift;
1215         unsigned int rshift = mc->rshift;
1216         int max = mc->max;
1217         unsigned int mask = (1 << fls(max)) - 1;
1218         unsigned int invert = mc->invert;
1219         unsigned short val, val2, val_mask;
1220         int ret;
1221
1222         val = (ucontrol->value.integer.value[0] & mask);
1223
1224         if (invert)
1225                 val = max - val;
1226         val_mask = mask << shift;
1227         val = val << shift;
1228         if (shift != rshift) {
1229                 val2 = (ucontrol->value.integer.value[1] & mask);
1230                 if (invert)
1231                         val2 = max - val2;
1232                 val_mask |= mask << rshift;
1233                 val |= val2 << rshift;
1234         }
1235
1236         mutex_lock(&widget->codec->mutex);
1237         widget->value = val;
1238
1239         /* save volume value if the widget is powered down */
1240         if (widget->id == snd_soc_dapm_pga && !widget->power) {
1241                 widget->saved_value = val;
1242                 mutex_unlock(&widget->codec->mutex);
1243                 return 1;
1244         }
1245
1246         dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
1247         if (widget->event) {
1248                 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1249                         ret = widget->event(widget, kcontrol,
1250                                                 SND_SOC_DAPM_PRE_REG);
1251                         if (ret < 0) {
1252                                 ret = 1;
1253                                 goto out;
1254                         }
1255                 }
1256                 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1257                 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1258                         ret = widget->event(widget, kcontrol,
1259                                                 SND_SOC_DAPM_POST_REG);
1260         } else
1261                 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1262
1263 out:
1264         mutex_unlock(&widget->codec->mutex);
1265         return ret;
1266 }
1267 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1268
1269 /**
1270  * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1271  * @kcontrol: mixer control
1272  * @ucontrol: control element information
1273  *
1274  * Callback to get the value of a dapm enumerated double mixer control.
1275  *
1276  * Returns 0 for success.
1277  */
1278 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1279         struct snd_ctl_elem_value *ucontrol)
1280 {
1281         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1282         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1283         unsigned short val, bitmask;
1284
1285         for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1286                 ;
1287         val = snd_soc_read(widget->codec, e->reg);
1288         ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1289         if (e->shift_l != e->shift_r)
1290                 ucontrol->value.enumerated.item[1] =
1291                         (val >> e->shift_r) & (bitmask - 1);
1292
1293         return 0;
1294 }
1295 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1296
1297 /**
1298  * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1299  * @kcontrol: mixer control
1300  * @ucontrol: control element information
1301  *
1302  * Callback to set the value of a dapm enumerated double mixer control.
1303  *
1304  * Returns 0 for success.
1305  */
1306 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1307         struct snd_ctl_elem_value *ucontrol)
1308 {
1309         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1310         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1311         unsigned short val, mux;
1312         unsigned short mask, bitmask;
1313         int ret = 0;
1314
1315         for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1316                 ;
1317         if (ucontrol->value.enumerated.item[0] > e->max - 1)
1318                 return -EINVAL;
1319         mux = ucontrol->value.enumerated.item[0];
1320         val = mux << e->shift_l;
1321         mask = (bitmask - 1) << e->shift_l;
1322         if (e->shift_l != e->shift_r) {
1323                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1324                         return -EINVAL;
1325                 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1326                 mask |= (bitmask - 1) << e->shift_r;
1327         }
1328
1329         mutex_lock(&widget->codec->mutex);
1330         widget->value = val;
1331         dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
1332         if (widget->event) {
1333                 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1334                         ret = widget->event(widget,
1335                                 kcontrol, SND_SOC_DAPM_PRE_REG);
1336                         if (ret < 0)
1337                                 goto out;
1338                 }
1339                 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1340                 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1341                         ret = widget->event(widget,
1342                                 kcontrol, SND_SOC_DAPM_POST_REG);
1343         } else
1344                 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1345
1346 out:
1347         mutex_unlock(&widget->codec->mutex);
1348         return ret;
1349 }
1350 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1351
1352 /**
1353  * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1354  *                                      callback
1355  * @kcontrol: mixer control
1356  * @ucontrol: control element information
1357  *
1358  * Callback to get the value of a dapm semi enumerated double mixer control.
1359  *
1360  * Semi enumerated mixer: the enumerated items are referred as values. Can be
1361  * used for handling bitfield coded enumeration for example.
1362  *
1363  * Returns 0 for success.
1364  */
1365 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1366         struct snd_ctl_elem_value *ucontrol)
1367 {
1368         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1369         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1370         unsigned short reg_val, val, mux;
1371
1372         reg_val = snd_soc_read(widget->codec, e->reg);
1373         val = (reg_val >> e->shift_l) & e->mask;
1374         for (mux = 0; mux < e->max; mux++) {
1375                 if (val == e->values[mux])
1376                         break;
1377         }
1378         ucontrol->value.enumerated.item[0] = mux;
1379         if (e->shift_l != e->shift_r) {
1380                 val = (reg_val >> e->shift_r) & e->mask;
1381                 for (mux = 0; mux < e->max; mux++) {
1382                         if (val == e->values[mux])
1383                                 break;
1384                 }
1385                 ucontrol->value.enumerated.item[1] = mux;
1386         }
1387
1388         return 0;
1389 }
1390 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1391
1392 /**
1393  * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1394  *                                      callback
1395  * @kcontrol: mixer control
1396  * @ucontrol: control element information
1397  *
1398  * Callback to set the value of a dapm semi enumerated double mixer control.
1399  *
1400  * Semi enumerated mixer: the enumerated items are referred as values. Can be
1401  * used for handling bitfield coded enumeration for example.
1402  *
1403  * Returns 0 for success.
1404  */
1405 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1406         struct snd_ctl_elem_value *ucontrol)
1407 {
1408         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1409         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1410         unsigned short val, mux;
1411         unsigned short mask;
1412         int ret = 0;
1413
1414         if (ucontrol->value.enumerated.item[0] > e->max - 1)
1415                 return -EINVAL;
1416         mux = ucontrol->value.enumerated.item[0];
1417         val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1418         mask = e->mask << e->shift_l;
1419         if (e->shift_l != e->shift_r) {
1420                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1421                         return -EINVAL;
1422                 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1423                 mask |= e->mask << e->shift_r;
1424         }
1425
1426         mutex_lock(&widget->codec->mutex);
1427         widget->value = val;
1428         dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
1429         if (widget->event) {
1430                 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1431                         ret = widget->event(widget,
1432                                 kcontrol, SND_SOC_DAPM_PRE_REG);
1433                         if (ret < 0)
1434                                 goto out;
1435                 }
1436                 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1437                 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1438                         ret = widget->event(widget,
1439                                 kcontrol, SND_SOC_DAPM_POST_REG);
1440         } else
1441                 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1442
1443 out:
1444         mutex_unlock(&widget->codec->mutex);
1445         return ret;
1446 }
1447 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1448
1449 /**
1450  * snd_soc_dapm_info_pin_switch - Info for a pin switch
1451  *
1452  * @kcontrol: mixer control
1453  * @uinfo: control element information
1454  *
1455  * Callback to provide information about a pin switch control.
1456  */
1457 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1458                                  struct snd_ctl_elem_info *uinfo)
1459 {
1460         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1461         uinfo->count = 1;
1462         uinfo->value.integer.min = 0;
1463         uinfo->value.integer.max = 1;
1464
1465         return 0;
1466 }
1467 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1468
1469 /**
1470  * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1471  *
1472  * @kcontrol: mixer control
1473  * @ucontrol: Value
1474  */
1475 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
1476                                 struct snd_ctl_elem_value *ucontrol)
1477 {
1478         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1479         const char *pin = (const char *)kcontrol->private_value;
1480
1481         mutex_lock(&codec->mutex);
1482
1483         ucontrol->value.integer.value[0] =
1484                 snd_soc_dapm_get_pin_status(codec, pin);
1485
1486         mutex_unlock(&codec->mutex);
1487
1488         return 0;
1489 }
1490 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
1491
1492 /**
1493  * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1494  *
1495  * @kcontrol: mixer control
1496  * @ucontrol: Value
1497  */
1498 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
1499                                 struct snd_ctl_elem_value *ucontrol)
1500 {
1501         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1502         const char *pin = (const char *)kcontrol->private_value;
1503
1504         mutex_lock(&codec->mutex);
1505
1506         if (ucontrol->value.integer.value[0])
1507                 snd_soc_dapm_enable_pin(codec, pin);
1508         else
1509                 snd_soc_dapm_disable_pin(codec, pin);
1510
1511         snd_soc_dapm_sync(codec);
1512
1513         mutex_unlock(&codec->mutex);
1514
1515         return 0;
1516 }
1517 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
1518
1519 /**
1520  * snd_soc_dapm_new_control - create new dapm control
1521  * @codec: audio codec
1522  * @widget: widget template
1523  *
1524  * Creates a new dapm control based upon the template.
1525  *
1526  * Returns 0 for success else error.
1527  */
1528 int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1529         const struct snd_soc_dapm_widget *widget)
1530 {
1531         struct snd_soc_dapm_widget *w;
1532
1533         if ((w = dapm_cnew_widget(widget)) == NULL)
1534                 return -ENOMEM;
1535
1536         w->codec = codec;
1537         INIT_LIST_HEAD(&w->sources);
1538         INIT_LIST_HEAD(&w->sinks);
1539         INIT_LIST_HEAD(&w->list);
1540         list_add(&w->list, &codec->dapm_widgets);
1541
1542         /* machine layer set ups unconnected pins and insertions */
1543         w->connected = 1;
1544         return 0;
1545 }
1546 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1547
1548 /**
1549  * snd_soc_dapm_new_controls - create new dapm controls
1550  * @codec: audio codec
1551  * @widget: widget array
1552  * @num: number of widgets
1553  *
1554  * Creates new DAPM controls based upon the templates.
1555  *
1556  * Returns 0 for success else error.
1557  */
1558 int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1559         const struct snd_soc_dapm_widget *widget,
1560         int num)
1561 {
1562         int i, ret;
1563
1564         for (i = 0; i < num; i++) {
1565                 ret = snd_soc_dapm_new_control(codec, widget);
1566                 if (ret < 0) {
1567                         printk(KERN_ERR
1568                                "ASoC: Failed to create DAPM control %s: %d\n",
1569                                widget->name, ret);
1570                         return ret;
1571                 }
1572                 widget++;
1573         }
1574         return 0;
1575 }
1576 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1577
1578
1579 /**
1580  * snd_soc_dapm_stream_event - send a stream event to the dapm core
1581  * @codec: audio codec
1582  * @stream: stream name
1583  * @event: stream event
1584  *
1585  * Sends a stream event to the dapm core. The core then makes any
1586  * necessary widget power changes.
1587  *
1588  * Returns 0 for success else error.
1589  */
1590 int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
1591         char *stream, int event)
1592 {
1593         struct snd_soc_dapm_widget *w;
1594
1595         if (stream == NULL)
1596                 return 0;
1597
1598         mutex_lock(&codec->mutex);
1599         list_for_each_entry(w, &codec->dapm_widgets, list)
1600         {
1601                 if (!w->sname)
1602                         continue;
1603                 pr_debug("widget %s\n %s stream %s event %d\n",
1604                          w->name, w->sname, stream, event);
1605                 if (strstr(w->sname, stream)) {
1606                         switch(event) {
1607                         case SND_SOC_DAPM_STREAM_START:
1608                                 w->active = 1;
1609                                 break;
1610                         case SND_SOC_DAPM_STREAM_STOP:
1611                                 w->active = 0;
1612                                 break;
1613                         case SND_SOC_DAPM_STREAM_SUSPEND:
1614                                 if (w->active)
1615                                         w->suspend = 1;
1616                                 w->active = 0;
1617                                 break;
1618                         case SND_SOC_DAPM_STREAM_RESUME:
1619                                 if (w->suspend) {
1620                                         w->active = 1;
1621                                         w->suspend = 0;
1622                                 }
1623                                 break;
1624                         case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
1625                                 break;
1626                         case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
1627                                 break;
1628                         }
1629                 }
1630         }
1631         mutex_unlock(&codec->mutex);
1632
1633         dapm_power_widgets(codec, event);
1634         dump_dapm(codec, __func__);
1635         return 0;
1636 }
1637 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
1638
1639 /**
1640  * snd_soc_dapm_set_bias_level - set the bias level for the system
1641  * @socdev: audio device
1642  * @level: level to configure
1643  *
1644  * Configure the bias (power) levels for the SoC audio device.
1645  *
1646  * Returns 0 for success else error.
1647  */
1648 int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
1649                                 enum snd_soc_bias_level level)
1650 {
1651         struct snd_soc_card *card = socdev->card;
1652         struct snd_soc_codec *codec = socdev->card->codec;
1653         int ret = 0;
1654
1655         if (card->set_bias_level)
1656                 ret = card->set_bias_level(card, level);
1657         if (ret == 0 && codec->set_bias_level)
1658                 ret = codec->set_bias_level(codec, level);
1659
1660         return ret;
1661 }
1662
1663 /**
1664  * snd_soc_dapm_enable_pin - enable pin.
1665  * @codec: SoC codec
1666  * @pin: pin name
1667  *
1668  * Enables input/output pin and it's parents or children widgets iff there is
1669  * a valid audio route and active audio stream.
1670  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1671  * do any widget power switching.
1672  */
1673 int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
1674 {
1675         return snd_soc_dapm_set_pin(codec, pin, 1);
1676 }
1677 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
1678
1679 /**
1680  * snd_soc_dapm_disable_pin - disable pin.
1681  * @codec: SoC codec
1682  * @pin: pin name
1683  *
1684  * Disables input/output pin and it's parents or children widgets.
1685  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1686  * do any widget power switching.
1687  */
1688 int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
1689 {
1690         return snd_soc_dapm_set_pin(codec, pin, 0);
1691 }
1692 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
1693
1694 /**
1695  * snd_soc_dapm_nc_pin - permanently disable pin.
1696  * @codec: SoC codec
1697  * @pin: pin name
1698  *
1699  * Marks the specified pin as being not connected, disabling it along
1700  * any parent or child widgets.  At present this is identical to
1701  * snd_soc_dapm_disable_pin() but in future it will be extended to do
1702  * additional things such as disabling controls which only affect
1703  * paths through the pin.
1704  *
1705  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1706  * do any widget power switching.
1707  */
1708 int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
1709 {
1710         return snd_soc_dapm_set_pin(codec, pin, 0);
1711 }
1712 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
1713
1714 /**
1715  * snd_soc_dapm_get_pin_status - get audio pin status
1716  * @codec: audio codec
1717  * @pin: audio signal pin endpoint (or start point)
1718  *
1719  * Get audio pin status - connected or disconnected.
1720  *
1721  * Returns 1 for connected otherwise 0.
1722  */
1723 int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
1724 {
1725         struct snd_soc_dapm_widget *w;
1726
1727         list_for_each_entry(w, &codec->dapm_widgets, list) {
1728                 if (!strcmp(w->name, pin))
1729                         return w->connected;
1730         }
1731
1732         return 0;
1733 }
1734 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
1735
1736 /**
1737  * snd_soc_dapm_free - free dapm resources
1738  * @socdev: SoC device
1739  *
1740  * Free all dapm widgets and resources.
1741  */
1742 void snd_soc_dapm_free(struct snd_soc_device *socdev)
1743 {
1744         struct snd_soc_codec *codec = socdev->card->codec;
1745
1746         snd_soc_dapm_sys_remove(socdev->dev);
1747         dapm_free_widgets(codec);
1748 }
1749 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
1750
1751 /* Module information */
1752 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
1753 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
1754 MODULE_LICENSE("GPL");