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