config: add configuration for x86_64
[profile/ivi/kernel-x86-ivi.git] / sound / soc / soc-dapm.c
1 /*
2  * soc-dapm.c  --  ALSA SoC Dynamic Audio Power Management
3  *
4  * Copyright 2005 Wolfson Microelectronics PLC.
5  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
6  *
7  *  This program is free software; you can redistribute  it and/or modify it
8  *  under  the terms of  the GNU General  Public License as published by the
9  *  Free Software Foundation;  either version 2 of the  License, or (at your
10  *  option) any later version.
11  *
12  *  Features:
13  *    o Changes power status of internal codec blocks depending on the
14  *      dynamic configuration of codec internal audio paths and active
15  *      DACs/ADCs.
16  *    o Platform power domain - can support external components i.e. amps and
17  *      mic/headphone 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 power down of audio subsystem to reduce pops between a quick
22  *      device reopen.
23  *
24  */
25
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/init.h>
29 #include <linux/async.h>
30 #include <linux/delay.h>
31 #include <linux/pm.h>
32 #include <linux/bitops.h>
33 #include <linux/platform_device.h>
34 #include <linux/jiffies.h>
35 #include <linux/debugfs.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/regulator/consumer.h>
38 #include <linux/clk.h>
39 #include <linux/slab.h>
40 #include <sound/core.h>
41 #include <sound/pcm.h>
42 #include <sound/pcm_params.h>
43 #include <sound/soc.h>
44 #include <sound/initval.h>
45
46 #include <trace/events/asoc.h>
47
48 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
49
50 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
51         struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
52         const char *control,
53         int (*connected)(struct snd_soc_dapm_widget *source,
54                          struct snd_soc_dapm_widget *sink));
55 static struct snd_soc_dapm_widget *
56 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
57                          const struct snd_soc_dapm_widget *widget);
58
59 /* dapm power sequences - make this per codec in the future */
60 static int dapm_up_seq[] = {
61         [snd_soc_dapm_pre] = 0,
62         [snd_soc_dapm_regulator_supply] = 1,
63         [snd_soc_dapm_clock_supply] = 1,
64         [snd_soc_dapm_supply] = 2,
65         [snd_soc_dapm_micbias] = 3,
66         [snd_soc_dapm_dai_link] = 2,
67         [snd_soc_dapm_dai_in] = 4,
68         [snd_soc_dapm_dai_out] = 4,
69         [snd_soc_dapm_aif_in] = 4,
70         [snd_soc_dapm_aif_out] = 4,
71         [snd_soc_dapm_mic] = 5,
72         [snd_soc_dapm_mux] = 6,
73         [snd_soc_dapm_virt_mux] = 6,
74         [snd_soc_dapm_value_mux] = 6,
75         [snd_soc_dapm_dac] = 7,
76         [snd_soc_dapm_switch] = 8,
77         [snd_soc_dapm_mixer] = 8,
78         [snd_soc_dapm_mixer_named_ctl] = 8,
79         [snd_soc_dapm_pga] = 9,
80         [snd_soc_dapm_adc] = 10,
81         [snd_soc_dapm_out_drv] = 11,
82         [snd_soc_dapm_hp] = 11,
83         [snd_soc_dapm_spk] = 11,
84         [snd_soc_dapm_line] = 11,
85         [snd_soc_dapm_kcontrol] = 12,
86         [snd_soc_dapm_post] = 13,
87 };
88
89 static int dapm_down_seq[] = {
90         [snd_soc_dapm_pre] = 0,
91         [snd_soc_dapm_kcontrol] = 1,
92         [snd_soc_dapm_adc] = 2,
93         [snd_soc_dapm_hp] = 3,
94         [snd_soc_dapm_spk] = 3,
95         [snd_soc_dapm_line] = 3,
96         [snd_soc_dapm_out_drv] = 3,
97         [snd_soc_dapm_pga] = 4,
98         [snd_soc_dapm_switch] = 5,
99         [snd_soc_dapm_mixer_named_ctl] = 5,
100         [snd_soc_dapm_mixer] = 5,
101         [snd_soc_dapm_dac] = 6,
102         [snd_soc_dapm_mic] = 7,
103         [snd_soc_dapm_micbias] = 8,
104         [snd_soc_dapm_mux] = 9,
105         [snd_soc_dapm_virt_mux] = 9,
106         [snd_soc_dapm_value_mux] = 9,
107         [snd_soc_dapm_aif_in] = 10,
108         [snd_soc_dapm_aif_out] = 10,
109         [snd_soc_dapm_dai_in] = 10,
110         [snd_soc_dapm_dai_out] = 10,
111         [snd_soc_dapm_dai_link] = 11,
112         [snd_soc_dapm_supply] = 12,
113         [snd_soc_dapm_clock_supply] = 13,
114         [snd_soc_dapm_regulator_supply] = 13,
115         [snd_soc_dapm_post] = 14,
116 };
117
118 static void pop_wait(u32 pop_time)
119 {
120         if (pop_time)
121                 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
122 }
123
124 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
125 {
126         va_list args;
127         char *buf;
128
129         if (!pop_time)
130                 return;
131
132         buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
133         if (buf == NULL)
134                 return;
135
136         va_start(args, fmt);
137         vsnprintf(buf, PAGE_SIZE, fmt, args);
138         dev_info(dev, "%s", buf);
139         va_end(args);
140
141         kfree(buf);
142 }
143
144 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
145 {
146         return !list_empty(&w->dirty);
147 }
148
149 void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
150 {
151         if (!dapm_dirty_widget(w)) {
152                 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
153                          w->name, reason);
154                 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
155         }
156 }
157 EXPORT_SYMBOL_GPL(dapm_mark_dirty);
158
159 void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
160 {
161         struct snd_soc_card *card = dapm->card;
162         struct snd_soc_dapm_widget *w;
163
164         mutex_lock(&card->dapm_mutex);
165
166         list_for_each_entry(w, &card->widgets, list) {
167                 switch (w->id) {
168                 case snd_soc_dapm_input:
169                 case snd_soc_dapm_output:
170                         dapm_mark_dirty(w, "Rechecking inputs and outputs");
171                         break;
172                 default:
173                         break;
174                 }
175         }
176
177         mutex_unlock(&card->dapm_mutex);
178 }
179 EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
180
181 /* create a new dapm widget */
182 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
183         const struct snd_soc_dapm_widget *_widget)
184 {
185         return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
186 }
187
188 struct dapm_kcontrol_data {
189         unsigned int value;
190         struct snd_soc_dapm_widget *widget;
191         struct list_head paths;
192         struct snd_soc_dapm_widget_list *wlist;
193 };
194
195 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
196         struct snd_kcontrol *kcontrol)
197 {
198         struct dapm_kcontrol_data *data;
199         struct soc_mixer_control *mc;
200
201         data = kzalloc(sizeof(*data), GFP_KERNEL);
202         if (!data) {
203                 dev_err(widget->dapm->dev,
204                                 "ASoC: can't allocate kcontrol data for %s\n",
205                                 widget->name);
206                 return -ENOMEM;
207         }
208
209         INIT_LIST_HEAD(&data->paths);
210
211         switch (widget->id) {
212         case snd_soc_dapm_switch:
213         case snd_soc_dapm_mixer:
214         case snd_soc_dapm_mixer_named_ctl:
215                 mc = (struct soc_mixer_control *)kcontrol->private_value;
216
217                 if (mc->autodisable) {
218                         struct snd_soc_dapm_widget template;
219
220                         memset(&template, 0, sizeof(template));
221                         template.reg = mc->reg;
222                         template.mask = (1 << fls(mc->max)) - 1;
223                         template.shift = mc->shift;
224                         if (mc->invert)
225                                 template.off_val = mc->max;
226                         else
227                                 template.off_val = 0;
228                         template.on_val = template.off_val;
229                         template.id = snd_soc_dapm_kcontrol;
230                         template.name = kcontrol->id.name;
231
232                         data->value = template.on_val;
233
234                         data->widget = snd_soc_dapm_new_control(widget->dapm,
235                                 &template);
236                         if (!data->widget) {
237                                 kfree(data);
238                                 return -ENOMEM;
239                         }
240                 }
241                 break;
242         default:
243                 break;
244         }
245
246         kcontrol->private_data = data;
247
248         return 0;
249 }
250
251 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
252 {
253         struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
254         kfree(data->wlist);
255         kfree(data);
256 }
257
258 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
259         const struct snd_kcontrol *kcontrol)
260 {
261         struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
262
263         return data->wlist;
264 }
265
266 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
267         struct snd_soc_dapm_widget *widget)
268 {
269         struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
270         struct snd_soc_dapm_widget_list *new_wlist;
271         unsigned int n;
272
273         if (data->wlist)
274                 n = data->wlist->num_widgets + 1;
275         else
276                 n = 1;
277
278         new_wlist = krealloc(data->wlist,
279                         sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
280         if (!new_wlist)
281                 return -ENOMEM;
282
283         new_wlist->widgets[n - 1] = widget;
284         new_wlist->num_widgets = n;
285
286         data->wlist = new_wlist;
287
288         return 0;
289 }
290
291 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
292         struct snd_soc_dapm_path *path)
293 {
294         struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
295
296         list_add_tail(&path->list_kcontrol, &data->paths);
297
298         if (data->widget) {
299                 snd_soc_dapm_add_path(data->widget->dapm, data->widget,
300                     path->source, NULL, NULL);
301         }
302 }
303
304 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
305 {
306         struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
307
308         if (!data->widget)
309                 return true;
310
311         return data->widget->power;
312 }
313
314 static struct list_head *dapm_kcontrol_get_path_list(
315         const struct snd_kcontrol *kcontrol)
316 {
317         struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
318
319         return &data->paths;
320 }
321
322 #define dapm_kcontrol_for_each_path(path, kcontrol) \
323         list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
324                 list_kcontrol)
325
326 static unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
327 {
328         struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
329
330         return data->value;
331 }
332
333 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
334         unsigned int value)
335 {
336         struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
337
338         if (data->value == value)
339                 return false;
340
341         if (data->widget)
342                 data->widget->on_val = value;
343
344         data->value = value;
345
346         return true;
347 }
348
349 /**
350  * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
351  * @kcontrol: The kcontrol
352  */
353 struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
354 {
355         return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->codec;
356 }
357 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
358
359 static void dapm_reset(struct snd_soc_card *card)
360 {
361         struct snd_soc_dapm_widget *w;
362
363         memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
364
365         list_for_each_entry(w, &card->widgets, list) {
366                 w->new_power = w->power;
367                 w->power_checked = false;
368                 w->inputs = -1;
369                 w->outputs = -1;
370         }
371 }
372
373 static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg,
374         unsigned int *value)
375 {
376         if (w->codec) {
377                 *value = snd_soc_read(w->codec, reg);
378                 return 0;
379         } else if (w->platform) {
380                 *value = snd_soc_platform_read(w->platform, reg);
381                 return 0;
382         }
383
384         dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
385         return -1;
386 }
387
388 static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
389 {
390         if (w->codec)
391                 return snd_soc_write(w->codec, reg, val);
392         else if (w->platform)
393                 return snd_soc_platform_write(w->platform, reg, val);
394
395         dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
396         return -1;
397 }
398
399 static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
400 {
401         if (w->codec && !w->codec->using_regmap)
402                 mutex_lock(&w->codec->mutex);
403         else if (w->platform)
404                 mutex_lock(&w->platform->mutex);
405 }
406
407 static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
408 {
409         if (w->codec && !w->codec->using_regmap)
410                 mutex_unlock(&w->codec->mutex);
411         else if (w->platform)
412                 mutex_unlock(&w->platform->mutex);
413 }
414
415 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
416 {
417         if (dapm->codec && dapm->codec->using_regmap)
418                 regmap_async_complete(dapm->codec->control_data);
419 }
420
421 static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
422         unsigned short reg, unsigned int mask, unsigned int value)
423 {
424         bool change;
425         unsigned int old, new;
426         int ret;
427
428         if (w->codec && w->codec->using_regmap) {
429                 ret = regmap_update_bits_check_async(w->codec->control_data,
430                                                      reg, mask, value,
431                                                      &change);
432                 if (ret != 0)
433                         return ret;
434         } else {
435                 soc_widget_lock(w);
436                 ret = soc_widget_read(w, reg, &old);
437                 if (ret < 0) {
438                         soc_widget_unlock(w);
439                         return ret;
440                 }
441
442                 new = (old & ~mask) | (value & mask);
443                 change = old != new;
444                 if (change) {
445                         ret = soc_widget_write(w, reg, new);
446                         if (ret < 0) {
447                                 soc_widget_unlock(w);
448                                 return ret;
449                         }
450                 }
451                 soc_widget_unlock(w);
452         }
453
454         return change;
455 }
456
457 /**
458  * snd_soc_dapm_set_bias_level - set the bias level for the system
459  * @dapm: DAPM context
460  * @level: level to configure
461  *
462  * Configure the bias (power) levels for the SoC audio device.
463  *
464  * Returns 0 for success else error.
465  */
466 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
467                                        enum snd_soc_bias_level level)
468 {
469         struct snd_soc_card *card = dapm->card;
470         int ret = 0;
471
472         trace_snd_soc_bias_level_start(card, level);
473
474         if (card && card->set_bias_level)
475                 ret = card->set_bias_level(card, dapm, level);
476         if (ret != 0)
477                 goto out;
478
479         if (dapm->codec) {
480                 if (dapm->codec->driver->set_bias_level)
481                         ret = dapm->codec->driver->set_bias_level(dapm->codec,
482                                                                   level);
483                 else
484                         dapm->bias_level = level;
485         } else if (!card || dapm != &card->dapm) {
486                 dapm->bias_level = level;
487         }
488
489         if (ret != 0)
490                 goto out;
491
492         if (card && card->set_bias_level_post)
493                 ret = card->set_bias_level_post(card, dapm, level);
494 out:
495         trace_snd_soc_bias_level_done(card, level);
496
497         return ret;
498 }
499
500 /* set up initial codec paths */
501 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
502         struct snd_soc_dapm_path *p, int i)
503 {
504         switch (w->id) {
505         case snd_soc_dapm_switch:
506         case snd_soc_dapm_mixer:
507         case snd_soc_dapm_mixer_named_ctl: {
508                 int val;
509                 struct soc_mixer_control *mc = (struct soc_mixer_control *)
510                         w->kcontrol_news[i].private_value;
511                 int reg = mc->reg;
512                 unsigned int shift = mc->shift;
513                 int max = mc->max;
514                 unsigned int mask = (1 << fls(max)) - 1;
515                 unsigned int invert = mc->invert;
516
517                 if (reg != SND_SOC_NOPM) {
518                         soc_widget_read(w, reg, &val);
519                         val = (val >> shift) & mask;
520                         if (invert)
521                                 val = max - val;
522                         p->connect = !!val;
523                 } else {
524                         p->connect = 0;
525                 }
526
527         }
528         break;
529         case snd_soc_dapm_mux: {
530                 struct soc_enum *e = (struct soc_enum *)
531                         w->kcontrol_news[i].private_value;
532                 int val, item;
533
534                 soc_widget_read(w, e->reg, &val);
535                 item = (val >> e->shift_l) & e->mask;
536
537                 if (item < e->max && !strcmp(p->name, e->texts[item]))
538                         p->connect = 1;
539                 else
540                         p->connect = 0;
541         }
542         break;
543         case snd_soc_dapm_virt_mux: {
544                 struct soc_enum *e = (struct soc_enum *)
545                         w->kcontrol_news[i].private_value;
546
547                 p->connect = 0;
548                 /* since a virtual mux has no backing registers to
549                  * decide which path to connect, it will try to match
550                  * with the first enumeration.  This is to ensure
551                  * that the default mux choice (the first) will be
552                  * correctly powered up during initialization.
553                  */
554                 if (!strcmp(p->name, e->texts[0]))
555                         p->connect = 1;
556         }
557         break;
558         case snd_soc_dapm_value_mux: {
559                 struct soc_enum *e = (struct soc_enum *)
560                         w->kcontrol_news[i].private_value;
561                 int val, item;
562
563                 soc_widget_read(w, e->reg, &val);
564                 val = (val >> e->shift_l) & e->mask;
565                 for (item = 0; item < e->max; item++) {
566                         if (val == e->values[item])
567                                 break;
568                 }
569
570                 if (item < e->max && !strcmp(p->name, e->texts[item]))
571                         p->connect = 1;
572                 else
573                         p->connect = 0;
574         }
575         break;
576         /* does not affect routing - always connected */
577         case snd_soc_dapm_pga:
578         case snd_soc_dapm_out_drv:
579         case snd_soc_dapm_output:
580         case snd_soc_dapm_adc:
581         case snd_soc_dapm_input:
582         case snd_soc_dapm_siggen:
583         case snd_soc_dapm_dac:
584         case snd_soc_dapm_micbias:
585         case snd_soc_dapm_vmid:
586         case snd_soc_dapm_supply:
587         case snd_soc_dapm_regulator_supply:
588         case snd_soc_dapm_clock_supply:
589         case snd_soc_dapm_aif_in:
590         case snd_soc_dapm_aif_out:
591         case snd_soc_dapm_dai_in:
592         case snd_soc_dapm_dai_out:
593         case snd_soc_dapm_hp:
594         case snd_soc_dapm_mic:
595         case snd_soc_dapm_spk:
596         case snd_soc_dapm_line:
597         case snd_soc_dapm_dai_link:
598         case snd_soc_dapm_kcontrol:
599                 p->connect = 1;
600         break;
601         /* does affect routing - dynamically connected */
602         case snd_soc_dapm_pre:
603         case snd_soc_dapm_post:
604                 p->connect = 0;
605         break;
606         }
607 }
608
609 /* connect mux widget to its interconnecting audio paths */
610 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
611         struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
612         struct snd_soc_dapm_path *path, const char *control_name,
613         const struct snd_kcontrol_new *kcontrol)
614 {
615         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
616         int i;
617
618         for (i = 0; i < e->max; i++) {
619                 if (!(strcmp(control_name, e->texts[i]))) {
620                         list_add(&path->list, &dapm->card->paths);
621                         list_add(&path->list_sink, &dest->sources);
622                         list_add(&path->list_source, &src->sinks);
623                         path->name = (char*)e->texts[i];
624                         dapm_set_path_status(dest, path, 0);
625                         return 0;
626                 }
627         }
628
629         return -ENODEV;
630 }
631
632 /* connect mixer widget to its interconnecting audio paths */
633 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
634         struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
635         struct snd_soc_dapm_path *path, const char *control_name)
636 {
637         int i;
638
639         /* search for mixer kcontrol */
640         for (i = 0; i < dest->num_kcontrols; i++) {
641                 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
642                         list_add(&path->list, &dapm->card->paths);
643                         list_add(&path->list_sink, &dest->sources);
644                         list_add(&path->list_source, &src->sinks);
645                         path->name = dest->kcontrol_news[i].name;
646                         dapm_set_path_status(dest, path, i);
647                         return 0;
648                 }
649         }
650         return -ENODEV;
651 }
652
653 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
654         struct snd_soc_dapm_widget *kcontrolw,
655         const struct snd_kcontrol_new *kcontrol_new,
656         struct snd_kcontrol **kcontrol)
657 {
658         struct snd_soc_dapm_widget *w;
659         int i;
660
661         *kcontrol = NULL;
662
663         list_for_each_entry(w, &dapm->card->widgets, list) {
664                 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
665                         continue;
666                 for (i = 0; i < w->num_kcontrols; i++) {
667                         if (&w->kcontrol_news[i] == kcontrol_new) {
668                                 if (w->kcontrols)
669                                         *kcontrol = w->kcontrols[i];
670                                 return 1;
671                         }
672                 }
673         }
674
675         return 0;
676 }
677
678 /*
679  * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
680  * create it. Either way, add the widget into the control's widget list
681  */
682 static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
683         int kci)
684 {
685         struct snd_soc_dapm_context *dapm = w->dapm;
686         struct snd_card *card = dapm->card->snd_card;
687         const char *prefix;
688         size_t prefix_len;
689         int shared;
690         struct snd_kcontrol *kcontrol;
691         bool wname_in_long_name, kcname_in_long_name;
692         char *long_name;
693         const char *name;
694         int ret;
695
696         if (dapm->codec)
697                 prefix = dapm->codec->name_prefix;
698         else
699                 prefix = NULL;
700
701         if (prefix)
702                 prefix_len = strlen(prefix) + 1;
703         else
704                 prefix_len = 0;
705
706         shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
707                                          &kcontrol);
708
709         if (!kcontrol) {
710                 if (shared) {
711                         wname_in_long_name = false;
712                         kcname_in_long_name = true;
713                 } else {
714                         switch (w->id) {
715                         case snd_soc_dapm_switch:
716                         case snd_soc_dapm_mixer:
717                                 wname_in_long_name = true;
718                                 kcname_in_long_name = true;
719                                 break;
720                         case snd_soc_dapm_mixer_named_ctl:
721                                 wname_in_long_name = false;
722                                 kcname_in_long_name = true;
723                                 break;
724                         case snd_soc_dapm_mux:
725                         case snd_soc_dapm_virt_mux:
726                         case snd_soc_dapm_value_mux:
727                                 wname_in_long_name = true;
728                                 kcname_in_long_name = false;
729                                 break;
730                         default:
731                                 return -EINVAL;
732                         }
733                 }
734
735                 if (wname_in_long_name && kcname_in_long_name) {
736                         /*
737                          * The control will get a prefix from the control
738                          * creation process but we're also using the same
739                          * prefix for widgets so cut the prefix off the
740                          * front of the widget name.
741                          */
742                         long_name = kasprintf(GFP_KERNEL, "%s %s",
743                                  w->name + prefix_len,
744                                  w->kcontrol_news[kci].name);
745                         if (long_name == NULL)
746                                 return -ENOMEM;
747
748                         name = long_name;
749                 } else if (wname_in_long_name) {
750                         long_name = NULL;
751                         name = w->name + prefix_len;
752                 } else {
753                         long_name = NULL;
754                         name = w->kcontrol_news[kci].name;
755                 }
756
757                 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
758                                         prefix);
759                 kfree(long_name);
760                 if (!kcontrol)
761                         return -ENOMEM;
762                 kcontrol->private_free = dapm_kcontrol_free;
763
764                 ret = dapm_kcontrol_data_alloc(w, kcontrol);
765                 if (ret) {
766                         snd_ctl_free_one(kcontrol);
767                         return ret;
768                 }
769
770                 ret = snd_ctl_add(card, kcontrol);
771                 if (ret < 0) {
772                         dev_err(dapm->dev,
773                                 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
774                                 w->name, name, ret);
775                         return ret;
776                 }
777         }
778
779         ret = dapm_kcontrol_add_widget(kcontrol, w);
780         if (ret)
781                 return ret;
782
783         w->kcontrols[kci] = kcontrol;
784
785         return 0;
786 }
787
788 /* create new dapm mixer control */
789 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
790 {
791         int i, ret;
792         struct snd_soc_dapm_path *path;
793
794         /* add kcontrol */
795         for (i = 0; i < w->num_kcontrols; i++) {
796                 /* match name */
797                 list_for_each_entry(path, &w->sources, list_sink) {
798                         /* mixer/mux paths name must match control name */
799                         if (path->name != (char *)w->kcontrol_news[i].name)
800                                 continue;
801
802                         if (w->kcontrols[i]) {
803                                 dapm_kcontrol_add_path(w->kcontrols[i], path);
804                                 continue;
805                         }
806
807                         ret = dapm_create_or_share_mixmux_kcontrol(w, i);
808                         if (ret < 0)
809                                 return ret;
810
811                         dapm_kcontrol_add_path(w->kcontrols[i], path);
812                 }
813         }
814
815         return 0;
816 }
817
818 /* create new dapm mux control */
819 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
820 {
821         struct snd_soc_dapm_context *dapm = w->dapm;
822         struct snd_soc_dapm_path *path;
823         int ret;
824
825         if (w->num_kcontrols != 1) {
826                 dev_err(dapm->dev,
827                         "ASoC: mux %s has incorrect number of controls\n",
828                         w->name);
829                 return -EINVAL;
830         }
831
832         if (list_empty(&w->sources)) {
833                 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
834                 return -EINVAL;
835         }
836
837         ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
838         if (ret < 0)
839                 return ret;
840
841         list_for_each_entry(path, &w->sources, list_sink)
842                 dapm_kcontrol_add_path(w->kcontrols[0], path);
843
844         return 0;
845 }
846
847 /* create new dapm volume control */
848 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
849 {
850         if (w->num_kcontrols)
851                 dev_err(w->dapm->dev,
852                         "ASoC: PGA controls not supported: '%s'\n", w->name);
853
854         return 0;
855 }
856
857 /* reset 'walked' bit for each dapm path */
858 static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
859                                    struct list_head *sink)
860 {
861         struct snd_soc_dapm_path *p;
862
863         list_for_each_entry(p, sink, list_source) {
864                 if (p->walked) {
865                         p->walked = 0;
866                         dapm_clear_walk_output(dapm, &p->sink->sinks);
867                 }
868         }
869 }
870
871 static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
872                                   struct list_head *source)
873 {
874         struct snd_soc_dapm_path *p;
875
876         list_for_each_entry(p, source, list_sink) {
877                 if (p->walked) {
878                         p->walked = 0;
879                         dapm_clear_walk_input(dapm, &p->source->sources);
880                 }
881         }
882 }
883
884
885 /* We implement power down on suspend by checking the power state of
886  * the ALSA card - when we are suspending the ALSA state for the card
887  * is set to D3.
888  */
889 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
890 {
891         int level = snd_power_get_state(widget->dapm->card->snd_card);
892
893         switch (level) {
894         case SNDRV_CTL_POWER_D3hot:
895         case SNDRV_CTL_POWER_D3cold:
896                 if (widget->ignore_suspend)
897                         dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
898                                 widget->name);
899                 return widget->ignore_suspend;
900         default:
901                 return 1;
902         }
903 }
904
905 /* add widget to list if it's not already in the list */
906 static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
907         struct snd_soc_dapm_widget *w)
908 {
909         struct snd_soc_dapm_widget_list *wlist;
910         int wlistsize, wlistentries, i;
911
912         if (*list == NULL)
913                 return -EINVAL;
914
915         wlist = *list;
916
917         /* is this widget already in the list */
918         for (i = 0; i < wlist->num_widgets; i++) {
919                 if (wlist->widgets[i] == w)
920                         return 0;
921         }
922
923         /* allocate some new space */
924         wlistentries = wlist->num_widgets + 1;
925         wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
926                         wlistentries * sizeof(struct snd_soc_dapm_widget *);
927         *list = krealloc(wlist, wlistsize, GFP_KERNEL);
928         if (*list == NULL) {
929                 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
930                         w->name);
931                 return -ENOMEM;
932         }
933         wlist = *list;
934
935         /* insert the widget */
936         dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
937                         w->name, wlist->num_widgets);
938
939         wlist->widgets[wlist->num_widgets] = w;
940         wlist->num_widgets++;
941         return 1;
942 }
943
944 /*
945  * Recursively check for a completed path to an active or physically connected
946  * output widget. Returns number of complete paths.
947  */
948 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
949         struct snd_soc_dapm_widget_list **list)
950 {
951         struct snd_soc_dapm_path *path;
952         int con = 0;
953
954         if (widget->outputs >= 0)
955                 return widget->outputs;
956
957         DAPM_UPDATE_STAT(widget, path_checks);
958
959         switch (widget->id) {
960         case snd_soc_dapm_supply:
961         case snd_soc_dapm_regulator_supply:
962         case snd_soc_dapm_clock_supply:
963         case snd_soc_dapm_kcontrol:
964                 return 0;
965         default:
966                 break;
967         }
968
969         switch (widget->id) {
970         case snd_soc_dapm_adc:
971         case snd_soc_dapm_aif_out:
972         case snd_soc_dapm_dai_out:
973                 if (widget->active) {
974                         widget->outputs = snd_soc_dapm_suspend_check(widget);
975                         return widget->outputs;
976                 }
977         default:
978                 break;
979         }
980
981         if (widget->connected) {
982                 /* connected pin ? */
983                 if (widget->id == snd_soc_dapm_output && !widget->ext) {
984                         widget->outputs = snd_soc_dapm_suspend_check(widget);
985                         return widget->outputs;
986                 }
987
988                 /* connected jack or spk ? */
989                 if (widget->id == snd_soc_dapm_hp ||
990                     widget->id == snd_soc_dapm_spk ||
991                     (widget->id == snd_soc_dapm_line &&
992                      !list_empty(&widget->sources))) {
993                         widget->outputs = snd_soc_dapm_suspend_check(widget);
994                         return widget->outputs;
995                 }
996         }
997
998         list_for_each_entry(path, &widget->sinks, list_source) {
999                 DAPM_UPDATE_STAT(widget, neighbour_checks);
1000
1001                 if (path->weak)
1002                         continue;
1003
1004                 if (path->walking)
1005                         return 1;
1006
1007                 if (path->walked)
1008                         continue;
1009
1010                 trace_snd_soc_dapm_output_path(widget, path);
1011
1012                 if (path->sink && path->connect) {
1013                         path->walked = 1;
1014                         path->walking = 1;
1015
1016                         /* do we need to add this widget to the list ? */
1017                         if (list) {
1018                                 int err;
1019                                 err = dapm_list_add_widget(list, path->sink);
1020                                 if (err < 0) {
1021                                         dev_err(widget->dapm->dev,
1022                                                 "ASoC: could not add widget %s\n",
1023                                                 widget->name);
1024                                         path->walking = 0;
1025                                         return con;
1026                                 }
1027                         }
1028
1029                         con += is_connected_output_ep(path->sink, list);
1030
1031                         path->walking = 0;
1032                 }
1033         }
1034
1035         widget->outputs = con;
1036
1037         return con;
1038 }
1039
1040 /*
1041  * Recursively check for a completed path to an active or physically connected
1042  * input widget. Returns number of complete paths.
1043  */
1044 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1045         struct snd_soc_dapm_widget_list **list)
1046 {
1047         struct snd_soc_dapm_path *path;
1048         int con = 0;
1049
1050         if (widget->inputs >= 0)
1051                 return widget->inputs;
1052
1053         DAPM_UPDATE_STAT(widget, path_checks);
1054
1055         switch (widget->id) {
1056         case snd_soc_dapm_supply:
1057         case snd_soc_dapm_regulator_supply:
1058         case snd_soc_dapm_clock_supply:
1059         case snd_soc_dapm_kcontrol:
1060                 return 0;
1061         default:
1062                 break;
1063         }
1064
1065         /* active stream ? */
1066         switch (widget->id) {
1067         case snd_soc_dapm_dac:
1068         case snd_soc_dapm_aif_in:
1069         case snd_soc_dapm_dai_in:
1070                 if (widget->active) {
1071                         widget->inputs = snd_soc_dapm_suspend_check(widget);
1072                         return widget->inputs;
1073                 }
1074         default:
1075                 break;
1076         }
1077
1078         if (widget->connected) {
1079                 /* connected pin ? */
1080                 if (widget->id == snd_soc_dapm_input && !widget->ext) {
1081                         widget->inputs = snd_soc_dapm_suspend_check(widget);
1082                         return widget->inputs;
1083                 }
1084
1085                 /* connected VMID/Bias for lower pops */
1086                 if (widget->id == snd_soc_dapm_vmid) {
1087                         widget->inputs = snd_soc_dapm_suspend_check(widget);
1088                         return widget->inputs;
1089                 }
1090
1091                 /* connected jack ? */
1092                 if (widget->id == snd_soc_dapm_mic ||
1093                     (widget->id == snd_soc_dapm_line &&
1094                      !list_empty(&widget->sinks))) {
1095                         widget->inputs = snd_soc_dapm_suspend_check(widget);
1096                         return widget->inputs;
1097                 }
1098
1099                 /* signal generator */
1100                 if (widget->id == snd_soc_dapm_siggen) {
1101                         widget->inputs = snd_soc_dapm_suspend_check(widget);
1102                         return widget->inputs;
1103                 }
1104         }
1105
1106         list_for_each_entry(path, &widget->sources, list_sink) {
1107                 DAPM_UPDATE_STAT(widget, neighbour_checks);
1108
1109                 if (path->weak)
1110                         continue;
1111
1112                 if (path->walking)
1113                         return 1;
1114
1115                 if (path->walked)
1116                         continue;
1117
1118                 trace_snd_soc_dapm_input_path(widget, path);
1119
1120                 if (path->source && path->connect) {
1121                         path->walked = 1;
1122                         path->walking = 1;
1123
1124                         /* do we need to add this widget to the list ? */
1125                         if (list) {
1126                                 int err;
1127                                 err = dapm_list_add_widget(list, path->source);
1128                                 if (err < 0) {
1129                                         dev_err(widget->dapm->dev,
1130                                                 "ASoC: could not add widget %s\n",
1131                                                 widget->name);
1132                                         path->walking = 0;
1133                                         return con;
1134                                 }
1135                         }
1136
1137                         con += is_connected_input_ep(path->source, list);
1138
1139                         path->walking = 0;
1140                 }
1141         }
1142
1143         widget->inputs = con;
1144
1145         return con;
1146 }
1147
1148 /**
1149  * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1150  * @dai: the soc DAI.
1151  * @stream: stream direction.
1152  * @list: list of active widgets for this stream.
1153  *
1154  * Queries DAPM graph as to whether an valid audio stream path exists for
1155  * the initial stream specified by name. This takes into account
1156  * current mixer and mux kcontrol settings. Creates list of valid widgets.
1157  *
1158  * Returns the number of valid paths or negative error.
1159  */
1160 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1161         struct snd_soc_dapm_widget_list **list)
1162 {
1163         struct snd_soc_card *card = dai->card;
1164         int paths;
1165
1166         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1167         dapm_reset(card);
1168
1169         if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1170                 paths = is_connected_output_ep(dai->playback_widget, list);
1171                 dapm_clear_walk_output(&card->dapm,
1172                                        &dai->playback_widget->sinks);
1173         } else {
1174                 paths = is_connected_input_ep(dai->capture_widget, list);
1175                 dapm_clear_walk_input(&card->dapm,
1176                                       &dai->capture_widget->sources);
1177         }
1178
1179         trace_snd_soc_dapm_connected(paths, stream);
1180         mutex_unlock(&card->dapm_mutex);
1181
1182         return paths;
1183 }
1184
1185 /*
1186  * Handler for generic register modifier widget.
1187  */
1188 int dapm_reg_event(struct snd_soc_dapm_widget *w,
1189                    struct snd_kcontrol *kcontrol, int event)
1190 {
1191         unsigned int val;
1192
1193         if (SND_SOC_DAPM_EVENT_ON(event))
1194                 val = w->on_val;
1195         else
1196                 val = w->off_val;
1197
1198         soc_widget_update_bits_locked(w, -(w->reg + 1),
1199                             w->mask << w->shift, val << w->shift);
1200
1201         return 0;
1202 }
1203 EXPORT_SYMBOL_GPL(dapm_reg_event);
1204
1205 /*
1206  * Handler for regulator supply widget.
1207  */
1208 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1209                    struct snd_kcontrol *kcontrol, int event)
1210 {
1211         int ret;
1212
1213         soc_dapm_async_complete(w->dapm);
1214
1215         if (SND_SOC_DAPM_EVENT_ON(event)) {
1216                 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1217                         ret = regulator_allow_bypass(w->regulator, false);
1218                         if (ret != 0)
1219                                 dev_warn(w->dapm->dev,
1220                                          "ASoC: Failed to unbypass %s: %d\n",
1221                                          w->name, ret);
1222                 }
1223
1224                 return regulator_enable(w->regulator);
1225         } else {
1226                 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1227                         ret = regulator_allow_bypass(w->regulator, true);
1228                         if (ret != 0)
1229                                 dev_warn(w->dapm->dev,
1230                                          "ASoC: Failed to bypass %s: %d\n",
1231                                          w->name, ret);
1232                 }
1233
1234                 return regulator_disable_deferred(w->regulator, w->shift);
1235         }
1236 }
1237 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1238
1239 /*
1240  * Handler for clock supply widget.
1241  */
1242 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1243                    struct snd_kcontrol *kcontrol, int event)
1244 {
1245         if (!w->clk)
1246                 return -EIO;
1247
1248         soc_dapm_async_complete(w->dapm);
1249
1250 #ifdef CONFIG_HAVE_CLK
1251         if (SND_SOC_DAPM_EVENT_ON(event)) {
1252                 return clk_prepare_enable(w->clk);
1253         } else {
1254                 clk_disable_unprepare(w->clk);
1255                 return 0;
1256         }
1257 #endif
1258         return 0;
1259 }
1260 EXPORT_SYMBOL_GPL(dapm_clock_event);
1261
1262 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1263 {
1264         if (w->power_checked)
1265                 return w->new_power;
1266
1267         if (w->force)
1268                 w->new_power = 1;
1269         else
1270                 w->new_power = w->power_check(w);
1271
1272         w->power_checked = true;
1273
1274         return w->new_power;
1275 }
1276
1277 /* Generic check to see if a widget should be powered.
1278  */
1279 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1280 {
1281         int in, out;
1282
1283         DAPM_UPDATE_STAT(w, power_checks);
1284
1285         in = is_connected_input_ep(w, NULL);
1286         dapm_clear_walk_input(w->dapm, &w->sources);
1287         out = is_connected_output_ep(w, NULL);
1288         dapm_clear_walk_output(w->dapm, &w->sinks);
1289         return out != 0 && in != 0;
1290 }
1291
1292 /* Check to see if an ADC has power */
1293 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1294 {
1295         int in;
1296
1297         DAPM_UPDATE_STAT(w, power_checks);
1298
1299         if (w->active) {
1300                 in = is_connected_input_ep(w, NULL);
1301                 dapm_clear_walk_input(w->dapm, &w->sources);
1302                 return in != 0;
1303         } else {
1304                 return dapm_generic_check_power(w);
1305         }
1306 }
1307
1308 /* Check to see if a DAC has power */
1309 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1310 {
1311         int out;
1312
1313         DAPM_UPDATE_STAT(w, power_checks);
1314
1315         if (w->active) {
1316                 out = is_connected_output_ep(w, NULL);
1317                 dapm_clear_walk_output(w->dapm, &w->sinks);
1318                 return out != 0;
1319         } else {
1320                 return dapm_generic_check_power(w);
1321         }
1322 }
1323
1324 /* Check to see if a power supply is needed */
1325 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1326 {
1327         struct snd_soc_dapm_path *path;
1328
1329         DAPM_UPDATE_STAT(w, power_checks);
1330
1331         /* Check if one of our outputs is connected */
1332         list_for_each_entry(path, &w->sinks, list_source) {
1333                 DAPM_UPDATE_STAT(w, neighbour_checks);
1334
1335                 if (path->weak)
1336                         continue;
1337
1338                 if (path->connected &&
1339                     !path->connected(path->source, path->sink))
1340                         continue;
1341
1342                 if (!path->sink)
1343                         continue;
1344
1345                 if (dapm_widget_power_check(path->sink))
1346                         return 1;
1347         }
1348
1349         return 0;
1350 }
1351
1352 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1353 {
1354         return 1;
1355 }
1356
1357 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1358                             struct snd_soc_dapm_widget *b,
1359                             bool power_up)
1360 {
1361         int *sort;
1362
1363         if (power_up)
1364                 sort = dapm_up_seq;
1365         else
1366                 sort = dapm_down_seq;
1367
1368         if (sort[a->id] != sort[b->id])
1369                 return sort[a->id] - sort[b->id];
1370         if (a->subseq != b->subseq) {
1371                 if (power_up)
1372                         return a->subseq - b->subseq;
1373                 else
1374                         return b->subseq - a->subseq;
1375         }
1376         if (a->reg != b->reg)
1377                 return a->reg - b->reg;
1378         if (a->dapm != b->dapm)
1379                 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1380
1381         return 0;
1382 }
1383
1384 /* Insert a widget in order into a DAPM power sequence. */
1385 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1386                             struct list_head *list,
1387                             bool power_up)
1388 {
1389         struct snd_soc_dapm_widget *w;
1390
1391         list_for_each_entry(w, list, power_list)
1392                 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1393                         list_add_tail(&new_widget->power_list, &w->power_list);
1394                         return;
1395                 }
1396
1397         list_add_tail(&new_widget->power_list, list);
1398 }
1399
1400 static void dapm_seq_check_event(struct snd_soc_card *card,
1401                                  struct snd_soc_dapm_widget *w, int event)
1402 {
1403         const char *ev_name;
1404         int power, ret;
1405
1406         switch (event) {
1407         case SND_SOC_DAPM_PRE_PMU:
1408                 ev_name = "PRE_PMU";
1409                 power = 1;
1410                 break;
1411         case SND_SOC_DAPM_POST_PMU:
1412                 ev_name = "POST_PMU";
1413                 power = 1;
1414                 break;
1415         case SND_SOC_DAPM_PRE_PMD:
1416                 ev_name = "PRE_PMD";
1417                 power = 0;
1418                 break;
1419         case SND_SOC_DAPM_POST_PMD:
1420                 ev_name = "POST_PMD";
1421                 power = 0;
1422                 break;
1423         case SND_SOC_DAPM_WILL_PMU:
1424                 ev_name = "WILL_PMU";
1425                 power = 1;
1426                 break;
1427         case SND_SOC_DAPM_WILL_PMD:
1428                 ev_name = "WILL_PMD";
1429                 power = 0;
1430                 break;
1431         default:
1432                 WARN(1, "Unknown event %d\n", event);
1433                 return;
1434         }
1435
1436         if (w->new_power != power)
1437                 return;
1438
1439         if (w->event && (w->event_flags & event)) {
1440                 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1441                         w->name, ev_name);
1442                 soc_dapm_async_complete(w->dapm);
1443                 trace_snd_soc_dapm_widget_event_start(w, event);
1444                 ret = w->event(w, NULL, event);
1445                 trace_snd_soc_dapm_widget_event_done(w, event);
1446                 if (ret < 0)
1447                         dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1448                                ev_name, w->name, ret);
1449         }
1450 }
1451
1452 /* Apply the coalesced changes from a DAPM sequence */
1453 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1454                                    struct list_head *pending)
1455 {
1456         struct snd_soc_dapm_widget *w;
1457         int reg;
1458         unsigned int value = 0;
1459         unsigned int mask = 0;
1460
1461         reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1462                                power_list)->reg;
1463
1464         list_for_each_entry(w, pending, power_list) {
1465                 WARN_ON(reg != w->reg);
1466                 w->power = w->new_power;
1467
1468                 mask |= w->mask << w->shift;
1469                 if (w->power)
1470                         value |= w->on_val << w->shift;
1471                 else
1472                         value |= w->off_val << w->shift;
1473
1474                 pop_dbg(w->dapm->dev, card->pop_time,
1475                         "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1476                         w->name, reg, value, mask);
1477
1478                 /* Check for events */
1479                 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1480                 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1481         }
1482
1483         if (reg >= 0) {
1484                 /* Any widget will do, they should all be updating the
1485                  * same register.
1486                  */
1487                 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1488                                      power_list);
1489
1490                 pop_dbg(w->dapm->dev, card->pop_time,
1491                         "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1492                         value, mask, reg, card->pop_time);
1493                 pop_wait(card->pop_time);
1494                 soc_widget_update_bits_locked(w, reg, mask, value);
1495         }
1496
1497         list_for_each_entry(w, pending, power_list) {
1498                 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1499                 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1500         }
1501 }
1502
1503 /* Apply a DAPM power sequence.
1504  *
1505  * We walk over a pre-sorted list of widgets to apply power to.  In
1506  * order to minimise the number of writes to the device required
1507  * multiple widgets will be updated in a single write where possible.
1508  * Currently anything that requires more than a single write is not
1509  * handled.
1510  */
1511 static void dapm_seq_run(struct snd_soc_card *card,
1512         struct list_head *list, int event, bool power_up)
1513 {
1514         struct snd_soc_dapm_widget *w, *n;
1515         struct snd_soc_dapm_context *d;
1516         LIST_HEAD(pending);
1517         int cur_sort = -1;
1518         int cur_subseq = -1;
1519         int cur_reg = SND_SOC_NOPM;
1520         struct snd_soc_dapm_context *cur_dapm = NULL;
1521         int ret, i;
1522         int *sort;
1523
1524         if (power_up)
1525                 sort = dapm_up_seq;
1526         else
1527                 sort = dapm_down_seq;
1528
1529         list_for_each_entry_safe(w, n, list, power_list) {
1530                 ret = 0;
1531
1532                 /* Do we need to apply any queued changes? */
1533                 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1534                     w->dapm != cur_dapm || w->subseq != cur_subseq) {
1535                         if (!list_empty(&pending))
1536                                 dapm_seq_run_coalesced(card, &pending);
1537
1538                         if (cur_dapm && cur_dapm->seq_notifier) {
1539                                 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1540                                         if (sort[i] == cur_sort)
1541                                                 cur_dapm->seq_notifier(cur_dapm,
1542                                                                        i,
1543                                                                        cur_subseq);
1544                         }
1545
1546                         if (cur_dapm && w->dapm != cur_dapm)
1547                                 soc_dapm_async_complete(cur_dapm);
1548
1549                         INIT_LIST_HEAD(&pending);
1550                         cur_sort = -1;
1551                         cur_subseq = INT_MIN;
1552                         cur_reg = SND_SOC_NOPM;
1553                         cur_dapm = NULL;
1554                 }
1555
1556                 switch (w->id) {
1557                 case snd_soc_dapm_pre:
1558                         if (!w->event)
1559                                 list_for_each_entry_safe_continue(w, n, list,
1560                                                                   power_list);
1561
1562                         if (event == SND_SOC_DAPM_STREAM_START)
1563                                 ret = w->event(w,
1564                                                NULL, SND_SOC_DAPM_PRE_PMU);
1565                         else if (event == SND_SOC_DAPM_STREAM_STOP)
1566                                 ret = w->event(w,
1567                                                NULL, SND_SOC_DAPM_PRE_PMD);
1568                         break;
1569
1570                 case snd_soc_dapm_post:
1571                         if (!w->event)
1572                                 list_for_each_entry_safe_continue(w, n, list,
1573                                                                   power_list);
1574
1575                         if (event == SND_SOC_DAPM_STREAM_START)
1576                                 ret = w->event(w,
1577                                                NULL, SND_SOC_DAPM_POST_PMU);
1578                         else if (event == SND_SOC_DAPM_STREAM_STOP)
1579                                 ret = w->event(w,
1580                                                NULL, SND_SOC_DAPM_POST_PMD);
1581                         break;
1582
1583                 default:
1584                         /* Queue it up for application */
1585                         cur_sort = sort[w->id];
1586                         cur_subseq = w->subseq;
1587                         cur_reg = w->reg;
1588                         cur_dapm = w->dapm;
1589                         list_move(&w->power_list, &pending);
1590                         break;
1591                 }
1592
1593                 if (ret < 0)
1594                         dev_err(w->dapm->dev,
1595                                 "ASoC: Failed to apply widget power: %d\n", ret);
1596         }
1597
1598         if (!list_empty(&pending))
1599                 dapm_seq_run_coalesced(card, &pending);
1600
1601         if (cur_dapm && cur_dapm->seq_notifier) {
1602                 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1603                         if (sort[i] == cur_sort)
1604                                 cur_dapm->seq_notifier(cur_dapm,
1605                                                        i, cur_subseq);
1606         }
1607
1608         list_for_each_entry(d, &card->dapm_list, list) {
1609                 soc_dapm_async_complete(d);
1610         }
1611 }
1612
1613 static void dapm_widget_update(struct snd_soc_card *card)
1614 {
1615         struct snd_soc_dapm_update *update = card->update;
1616         struct snd_soc_dapm_widget_list *wlist;
1617         struct snd_soc_dapm_widget *w = NULL;
1618         unsigned int wi;
1619         int ret;
1620
1621         if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1622                 return;
1623
1624         wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1625
1626         for (wi = 0; wi < wlist->num_widgets; wi++) {
1627                 w = wlist->widgets[wi];
1628
1629                 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1630                         ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1631                         if (ret != 0)
1632                                 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1633                                            w->name, ret);
1634                 }
1635         }
1636
1637         if (!w)
1638                 return;
1639
1640         ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
1641                                   update->val);
1642         if (ret < 0)
1643                 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1644                         w->name, ret);
1645
1646         for (wi = 0; wi < wlist->num_widgets; wi++) {
1647                 w = wlist->widgets[wi];
1648
1649                 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1650                         ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1651                         if (ret != 0)
1652                                 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1653                                            w->name, ret);
1654                 }
1655         }
1656 }
1657
1658 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1659  * they're changing state.
1660  */
1661 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1662 {
1663         struct snd_soc_dapm_context *d = data;
1664         int ret;
1665
1666         /* If we're off and we're not supposed to be go into STANDBY */
1667         if (d->bias_level == SND_SOC_BIAS_OFF &&
1668             d->target_bias_level != SND_SOC_BIAS_OFF) {
1669                 if (d->dev)
1670                         pm_runtime_get_sync(d->dev);
1671
1672                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1673                 if (ret != 0)
1674                         dev_err(d->dev,
1675                                 "ASoC: Failed to turn on bias: %d\n", ret);
1676         }
1677
1678         /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1679         if (d->bias_level != d->target_bias_level) {
1680                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1681                 if (ret != 0)
1682                         dev_err(d->dev,
1683                                 "ASoC: Failed to prepare bias: %d\n", ret);
1684         }
1685 }
1686
1687 /* Async callback run prior to DAPM sequences - brings to their final
1688  * state.
1689  */
1690 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1691 {
1692         struct snd_soc_dapm_context *d = data;
1693         int ret;
1694
1695         /* If we just powered the last thing off drop to standby bias */
1696         if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1697             (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1698              d->target_bias_level == SND_SOC_BIAS_OFF)) {
1699                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1700                 if (ret != 0)
1701                         dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1702                                 ret);
1703         }
1704
1705         /* If we're in standby and can support bias off then do that */
1706         if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1707             d->target_bias_level == SND_SOC_BIAS_OFF) {
1708                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1709                 if (ret != 0)
1710                         dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1711                                 ret);
1712
1713                 if (d->dev)
1714                         pm_runtime_put(d->dev);
1715         }
1716
1717         /* If we just powered up then move to active bias */
1718         if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1719             d->target_bias_level == SND_SOC_BIAS_ON) {
1720                 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1721                 if (ret != 0)
1722                         dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1723                                 ret);
1724         }
1725 }
1726
1727 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1728                                        bool power, bool connect)
1729 {
1730         /* If a connection is being made or broken then that update
1731          * will have marked the peer dirty, otherwise the widgets are
1732          * not connected and this update has no impact. */
1733         if (!connect)
1734                 return;
1735
1736         /* If the peer is already in the state we're moving to then we
1737          * won't have an impact on it. */
1738         if (power != peer->power)
1739                 dapm_mark_dirty(peer, "peer state change");
1740 }
1741
1742 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1743                                   struct list_head *up_list,
1744                                   struct list_head *down_list)
1745 {
1746         struct snd_soc_dapm_path *path;
1747
1748         if (w->power == power)
1749                 return;
1750
1751         trace_snd_soc_dapm_widget_power(w, power);
1752
1753         /* If we changed our power state perhaps our neigbours changed
1754          * also.
1755          */
1756         list_for_each_entry(path, &w->sources, list_sink) {
1757                 if (path->source) {
1758                         dapm_widget_set_peer_power(path->source, power,
1759                                                    path->connect);
1760                 }
1761         }
1762         switch (w->id) {
1763         case snd_soc_dapm_supply:
1764         case snd_soc_dapm_regulator_supply:
1765         case snd_soc_dapm_clock_supply:
1766         case snd_soc_dapm_kcontrol:
1767                 /* Supplies can't affect their outputs, only their inputs */
1768                 break;
1769         default:
1770                 list_for_each_entry(path, &w->sinks, list_source) {
1771                         if (path->sink) {
1772                                 dapm_widget_set_peer_power(path->sink, power,
1773                                                            path->connect);
1774                         }
1775                 }
1776                 break;
1777         }
1778
1779         if (power)
1780                 dapm_seq_insert(w, up_list, true);
1781         else
1782                 dapm_seq_insert(w, down_list, false);
1783 }
1784
1785 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1786                                   struct list_head *up_list,
1787                                   struct list_head *down_list)
1788 {
1789         int power;
1790
1791         switch (w->id) {
1792         case snd_soc_dapm_pre:
1793                 dapm_seq_insert(w, down_list, false);
1794                 break;
1795         case snd_soc_dapm_post:
1796                 dapm_seq_insert(w, up_list, true);
1797                 break;
1798
1799         default:
1800                 power = dapm_widget_power_check(w);
1801
1802                 dapm_widget_set_power(w, power, up_list, down_list);
1803                 break;
1804         }
1805 }
1806
1807 /*
1808  * Scan each dapm widget for complete audio path.
1809  * A complete path is a route that has valid endpoints i.e.:-
1810  *
1811  *  o DAC to output pin.
1812  *  o Input Pin to ADC.
1813  *  o Input pin to Output pin (bypass, sidetone)
1814  *  o DAC to ADC (loopback).
1815  */
1816 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1817 {
1818         struct snd_soc_dapm_widget *w;
1819         struct snd_soc_dapm_context *d;
1820         LIST_HEAD(up_list);
1821         LIST_HEAD(down_list);
1822         ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1823         enum snd_soc_bias_level bias;
1824
1825         trace_snd_soc_dapm_start(card);
1826
1827         list_for_each_entry(d, &card->dapm_list, list) {
1828                 if (d->idle_bias_off)
1829                         d->target_bias_level = SND_SOC_BIAS_OFF;
1830                 else
1831                         d->target_bias_level = SND_SOC_BIAS_STANDBY;
1832         }
1833
1834         dapm_reset(card);
1835
1836         /* Check which widgets we need to power and store them in
1837          * lists indicating if they should be powered up or down.  We
1838          * only check widgets that have been flagged as dirty but note
1839          * that new widgets may be added to the dirty list while we
1840          * iterate.
1841          */
1842         list_for_each_entry(w, &card->dapm_dirty, dirty) {
1843                 dapm_power_one_widget(w, &up_list, &down_list);
1844         }
1845
1846         list_for_each_entry(w, &card->widgets, list) {
1847                 switch (w->id) {
1848                 case snd_soc_dapm_pre:
1849                 case snd_soc_dapm_post:
1850                         /* These widgets always need to be powered */
1851                         break;
1852                 default:
1853                         list_del_init(&w->dirty);
1854                         break;
1855                 }
1856
1857                 if (w->new_power) {
1858                         d = w->dapm;
1859
1860                         /* Supplies and micbiases only bring the
1861                          * context up to STANDBY as unless something
1862                          * else is active and passing audio they
1863                          * generally don't require full power.  Signal
1864                          * generators are virtual pins and have no
1865                          * power impact themselves.
1866                          */
1867                         switch (w->id) {
1868                         case snd_soc_dapm_siggen:
1869                         case snd_soc_dapm_vmid:
1870                                 break;
1871                         case snd_soc_dapm_supply:
1872                         case snd_soc_dapm_regulator_supply:
1873                         case snd_soc_dapm_clock_supply:
1874                         case snd_soc_dapm_micbias:
1875                                 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1876                                         d->target_bias_level = SND_SOC_BIAS_STANDBY;
1877                                 break;
1878                         default:
1879                                 d->target_bias_level = SND_SOC_BIAS_ON;
1880                                 break;
1881                         }
1882                 }
1883
1884         }
1885
1886         /* Force all contexts in the card to the same bias state if
1887          * they're not ground referenced.
1888          */
1889         bias = SND_SOC_BIAS_OFF;
1890         list_for_each_entry(d, &card->dapm_list, list)
1891                 if (d->target_bias_level > bias)
1892                         bias = d->target_bias_level;
1893         list_for_each_entry(d, &card->dapm_list, list)
1894                 if (!d->idle_bias_off)
1895                         d->target_bias_level = bias;
1896
1897         trace_snd_soc_dapm_walk_done(card);
1898
1899         /* Run all the bias changes in parallel */
1900         list_for_each_entry(d, &card->dapm_list, list)
1901                 async_schedule_domain(dapm_pre_sequence_async, d,
1902                                         &async_domain);
1903         async_synchronize_full_domain(&async_domain);
1904
1905         list_for_each_entry(w, &down_list, power_list) {
1906                 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
1907         }
1908
1909         list_for_each_entry(w, &up_list, power_list) {
1910                 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
1911         }
1912
1913         /* Power down widgets first; try to avoid amplifying pops. */
1914         dapm_seq_run(card, &down_list, event, false);
1915
1916         dapm_widget_update(card);
1917
1918         /* Now power up. */
1919         dapm_seq_run(card, &up_list, event, true);
1920
1921         /* Run all the bias changes in parallel */
1922         list_for_each_entry(d, &card->dapm_list, list)
1923                 async_schedule_domain(dapm_post_sequence_async, d,
1924                                         &async_domain);
1925         async_synchronize_full_domain(&async_domain);
1926
1927         /* do we need to notify any clients that DAPM event is complete */
1928         list_for_each_entry(d, &card->dapm_list, list) {
1929                 if (d->stream_event)
1930                         d->stream_event(d, event);
1931         }
1932
1933         pop_dbg(card->dev, card->pop_time,
1934                 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1935         pop_wait(card->pop_time);
1936
1937         trace_snd_soc_dapm_done(card);
1938
1939         return 0;
1940 }
1941
1942 #ifdef CONFIG_DEBUG_FS
1943 static ssize_t dapm_widget_power_read_file(struct file *file,
1944                                            char __user *user_buf,
1945                                            size_t count, loff_t *ppos)
1946 {
1947         struct snd_soc_dapm_widget *w = file->private_data;
1948         char *buf;
1949         int in, out;
1950         ssize_t ret;
1951         struct snd_soc_dapm_path *p = NULL;
1952
1953         buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1954         if (!buf)
1955                 return -ENOMEM;
1956
1957         in = is_connected_input_ep(w, NULL);
1958         dapm_clear_walk_input(w->dapm, &w->sources);
1959         out = is_connected_output_ep(w, NULL);
1960         dapm_clear_walk_output(w->dapm, &w->sinks);
1961
1962         ret = snprintf(buf, PAGE_SIZE, "%s: %s%s  in %d out %d",
1963                        w->name, w->power ? "On" : "Off",
1964                        w->force ? " (forced)" : "", in, out);
1965
1966         if (w->reg >= 0)
1967                 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1968                                 " - R%d(0x%x) mask 0x%x",
1969                                 w->reg, w->reg, w->mask << w->shift);
1970
1971         ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1972
1973         if (w->sname)
1974                 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1975                                 w->sname,
1976                                 w->active ? "active" : "inactive");
1977
1978         list_for_each_entry(p, &w->sources, list_sink) {
1979                 if (p->connected && !p->connected(w, p->source))
1980                         continue;
1981
1982                 if (p->connect)
1983                         ret += snprintf(buf + ret, PAGE_SIZE - ret,
1984                                         " in  \"%s\" \"%s\"\n",
1985                                         p->name ? p->name : "static",
1986                                         p->source->name);
1987         }
1988         list_for_each_entry(p, &w->sinks, list_source) {
1989                 if (p->connected && !p->connected(w, p->sink))
1990                         continue;
1991
1992                 if (p->connect)
1993                         ret += snprintf(buf + ret, PAGE_SIZE - ret,
1994                                         " out \"%s\" \"%s\"\n",
1995                                         p->name ? p->name : "static",
1996                                         p->sink->name);
1997         }
1998
1999         ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2000
2001         kfree(buf);
2002         return ret;
2003 }
2004
2005 static const struct file_operations dapm_widget_power_fops = {
2006         .open = simple_open,
2007         .read = dapm_widget_power_read_file,
2008         .llseek = default_llseek,
2009 };
2010
2011 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2012                                    size_t count, loff_t *ppos)
2013 {
2014         struct snd_soc_dapm_context *dapm = file->private_data;
2015         char *level;
2016
2017         switch (dapm->bias_level) {
2018         case SND_SOC_BIAS_ON:
2019                 level = "On\n";
2020                 break;
2021         case SND_SOC_BIAS_PREPARE:
2022                 level = "Prepare\n";
2023                 break;
2024         case SND_SOC_BIAS_STANDBY:
2025                 level = "Standby\n";
2026                 break;
2027         case SND_SOC_BIAS_OFF:
2028                 level = "Off\n";
2029                 break;
2030         default:
2031                 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2032                 level = "Unknown\n";
2033                 break;
2034         }
2035
2036         return simple_read_from_buffer(user_buf, count, ppos, level,
2037                                        strlen(level));
2038 }
2039
2040 static const struct file_operations dapm_bias_fops = {
2041         .open = simple_open,
2042         .read = dapm_bias_read_file,
2043         .llseek = default_llseek,
2044 };
2045
2046 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2047         struct dentry *parent)
2048 {
2049         struct dentry *d;
2050
2051         dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2052
2053         if (!dapm->debugfs_dapm) {
2054                 dev_warn(dapm->dev,
2055                        "ASoC: Failed to create DAPM debugfs directory\n");
2056                 return;
2057         }
2058
2059         d = debugfs_create_file("bias_level", 0444,
2060                                 dapm->debugfs_dapm, dapm,
2061                                 &dapm_bias_fops);
2062         if (!d)
2063                 dev_warn(dapm->dev,
2064                          "ASoC: Failed to create bias level debugfs file\n");
2065 }
2066
2067 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2068 {
2069         struct snd_soc_dapm_context *dapm = w->dapm;
2070         struct dentry *d;
2071
2072         if (!dapm->debugfs_dapm || !w->name)
2073                 return;
2074
2075         d = debugfs_create_file(w->name, 0444,
2076                                 dapm->debugfs_dapm, w,
2077                                 &dapm_widget_power_fops);
2078         if (!d)
2079                 dev_warn(w->dapm->dev,
2080                         "ASoC: Failed to create %s debugfs file\n",
2081                         w->name);
2082 }
2083
2084 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2085 {
2086         debugfs_remove_recursive(dapm->debugfs_dapm);
2087 }
2088
2089 #else
2090 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2091         struct dentry *parent)
2092 {
2093 }
2094
2095 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2096 {
2097 }
2098
2099 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2100 {
2101 }
2102
2103 #endif
2104
2105 /* test and update the power status of a mux widget */
2106 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2107                                  struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2108 {
2109         struct snd_soc_dapm_path *path;
2110         int found = 0;
2111
2112         /* find dapm widget path assoc with kcontrol */
2113         dapm_kcontrol_for_each_path(path, kcontrol) {
2114                 if (!path->name || !e->texts[mux])
2115                         continue;
2116
2117                 found = 1;
2118                 /* we now need to match the string in the enum to the path */
2119                 if (!(strcmp(path->name, e->texts[mux]))) {
2120                         path->connect = 1; /* new connection */
2121                         dapm_mark_dirty(path->source, "mux connection");
2122                 } else {
2123                         if (path->connect)
2124                                 dapm_mark_dirty(path->source,
2125                                                 "mux disconnection");
2126                         path->connect = 0; /* old connection must be powered down */
2127                 }
2128                 dapm_mark_dirty(path->sink, "mux change");
2129         }
2130
2131         if (found)
2132                 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2133
2134         return found;
2135 }
2136
2137 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2138         struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2139         struct snd_soc_dapm_update *update)
2140 {
2141         struct snd_soc_card *card = dapm->card;
2142         int ret;
2143
2144         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2145         card->update = update;
2146         ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2147         card->update = NULL;
2148         mutex_unlock(&card->dapm_mutex);
2149         if (ret > 0)
2150                 soc_dpcm_runtime_update(card);
2151         return ret;
2152 }
2153 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2154
2155 /* test and update the power status of a mixer or switch widget */
2156 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2157                                    struct snd_kcontrol *kcontrol, int connect)
2158 {
2159         struct snd_soc_dapm_path *path;
2160         int found = 0;
2161
2162         /* find dapm widget path assoc with kcontrol */
2163         dapm_kcontrol_for_each_path(path, kcontrol) {
2164                 found = 1;
2165                 path->connect = connect;
2166                 dapm_mark_dirty(path->source, "mixer connection");
2167                 dapm_mark_dirty(path->sink, "mixer update");
2168         }
2169
2170         if (found)
2171                 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2172
2173         return found;
2174 }
2175
2176 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2177         struct snd_kcontrol *kcontrol, int connect,
2178         struct snd_soc_dapm_update *update)
2179 {
2180         struct snd_soc_card *card = dapm->card;
2181         int ret;
2182
2183         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2184         card->update = update;
2185         ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
2186         card->update = NULL;
2187         mutex_unlock(&card->dapm_mutex);
2188         if (ret > 0)
2189                 soc_dpcm_runtime_update(card);
2190         return ret;
2191 }
2192 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2193
2194 /* show dapm widget status in sys fs */
2195 static ssize_t dapm_widget_show(struct device *dev,
2196         struct device_attribute *attr, char *buf)
2197 {
2198         struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2199         struct snd_soc_codec *codec =rtd->codec;
2200         struct snd_soc_dapm_widget *w;
2201         int count = 0;
2202         char *state = "not set";
2203
2204         list_for_each_entry(w, &codec->card->widgets, list) {
2205                 if (w->dapm != &codec->dapm)
2206                         continue;
2207
2208                 /* only display widgets that burnm power */
2209                 switch (w->id) {
2210                 case snd_soc_dapm_hp:
2211                 case snd_soc_dapm_mic:
2212                 case snd_soc_dapm_spk:
2213                 case snd_soc_dapm_line:
2214                 case snd_soc_dapm_micbias:
2215                 case snd_soc_dapm_dac:
2216                 case snd_soc_dapm_adc:
2217                 case snd_soc_dapm_pga:
2218                 case snd_soc_dapm_out_drv:
2219                 case snd_soc_dapm_mixer:
2220                 case snd_soc_dapm_mixer_named_ctl:
2221                 case snd_soc_dapm_supply:
2222                 case snd_soc_dapm_regulator_supply:
2223                 case snd_soc_dapm_clock_supply:
2224                         if (w->name)
2225                                 count += sprintf(buf + count, "%s: %s\n",
2226                                         w->name, w->power ? "On":"Off");
2227                 break;
2228                 default:
2229                 break;
2230                 }
2231         }
2232
2233         switch (codec->dapm.bias_level) {
2234         case SND_SOC_BIAS_ON:
2235                 state = "On";
2236                 break;
2237         case SND_SOC_BIAS_PREPARE:
2238                 state = "Prepare";
2239                 break;
2240         case SND_SOC_BIAS_STANDBY:
2241                 state = "Standby";
2242                 break;
2243         case SND_SOC_BIAS_OFF:
2244                 state = "Off";
2245                 break;
2246         }
2247         count += sprintf(buf + count, "PM State: %s\n", state);
2248
2249         return count;
2250 }
2251
2252 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2253
2254 int snd_soc_dapm_sys_add(struct device *dev)
2255 {
2256         return device_create_file(dev, &dev_attr_dapm_widget);
2257 }
2258
2259 static void snd_soc_dapm_sys_remove(struct device *dev)
2260 {
2261         device_remove_file(dev, &dev_attr_dapm_widget);
2262 }
2263
2264 static void dapm_free_path(struct snd_soc_dapm_path *path)
2265 {
2266         list_del(&path->list_sink);
2267         list_del(&path->list_source);
2268         list_del(&path->list_kcontrol);
2269         list_del(&path->list);
2270         kfree(path);
2271 }
2272
2273 /* free all dapm widgets and resources */
2274 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2275 {
2276         struct snd_soc_dapm_widget *w, *next_w;
2277         struct snd_soc_dapm_path *p, *next_p;
2278
2279         list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2280                 if (w->dapm != dapm)
2281                         continue;
2282                 list_del(&w->list);
2283                 /*
2284                  * remove source and sink paths associated to this widget.
2285                  * While removing the path, remove reference to it from both
2286                  * source and sink widgets so that path is removed only once.
2287                  */
2288                 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2289                         dapm_free_path(p);
2290
2291                 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2292                         dapm_free_path(p);
2293
2294                 kfree(w->kcontrols);
2295                 kfree(w->name);
2296                 kfree(w);
2297         }
2298 }
2299
2300 static struct snd_soc_dapm_widget *dapm_find_widget(
2301                         struct snd_soc_dapm_context *dapm, const char *pin,
2302                         bool search_other_contexts)
2303 {
2304         struct snd_soc_dapm_widget *w;
2305         struct snd_soc_dapm_widget *fallback = NULL;
2306
2307         list_for_each_entry(w, &dapm->card->widgets, list) {
2308                 if (!strcmp(w->name, pin)) {
2309                         if (w->dapm == dapm)
2310                                 return w;
2311                         else
2312                                 fallback = w;
2313                 }
2314         }
2315
2316         if (search_other_contexts)
2317                 return fallback;
2318
2319         return NULL;
2320 }
2321
2322 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2323                                 const char *pin, int status)
2324 {
2325         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2326
2327         if (!w) {
2328                 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2329                 return -EINVAL;
2330         }
2331
2332         if (w->connected != status)
2333                 dapm_mark_dirty(w, "pin configuration");
2334
2335         w->connected = status;
2336         if (status == 0)
2337                 w->force = 0;
2338
2339         return 0;
2340 }
2341
2342 /**
2343  * snd_soc_dapm_sync - scan and power dapm paths
2344  * @dapm: DAPM context
2345  *
2346  * Walks all dapm audio paths and powers widgets according to their
2347  * stream or path usage.
2348  *
2349  * Returns 0 for success.
2350  */
2351 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2352 {
2353         int ret;
2354
2355         /*
2356          * Suppress early reports (eg, jacks syncing their state) to avoid
2357          * silly DAPM runs during card startup.
2358          */
2359         if (!dapm->card || !dapm->card->instantiated)
2360                 return 0;
2361
2362         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2363         ret = dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2364         mutex_unlock(&dapm->card->dapm_mutex);
2365         return ret;
2366 }
2367 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2368
2369 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2370         struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2371         const char *control,
2372         int (*connected)(struct snd_soc_dapm_widget *source,
2373                          struct snd_soc_dapm_widget *sink))
2374 {
2375         struct snd_soc_dapm_path *path;
2376         int ret;
2377
2378         path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2379         if (!path)
2380                 return -ENOMEM;
2381
2382         path->source = wsource;
2383         path->sink = wsink;
2384         path->connected = connected;
2385         INIT_LIST_HEAD(&path->list);
2386         INIT_LIST_HEAD(&path->list_kcontrol);
2387         INIT_LIST_HEAD(&path->list_source);
2388         INIT_LIST_HEAD(&path->list_sink);
2389
2390         /* check for external widgets */
2391         if (wsink->id == snd_soc_dapm_input) {
2392                 if (wsource->id == snd_soc_dapm_micbias ||
2393                         wsource->id == snd_soc_dapm_mic ||
2394                         wsource->id == snd_soc_dapm_line ||
2395                         wsource->id == snd_soc_dapm_output)
2396                         wsink->ext = 1;
2397         }
2398         if (wsource->id == snd_soc_dapm_output) {
2399                 if (wsink->id == snd_soc_dapm_spk ||
2400                         wsink->id == snd_soc_dapm_hp ||
2401                         wsink->id == snd_soc_dapm_line ||
2402                         wsink->id == snd_soc_dapm_input)
2403                         wsource->ext = 1;
2404         }
2405
2406         dapm_mark_dirty(wsource, "Route added");
2407         dapm_mark_dirty(wsink, "Route added");
2408
2409         /* connect static paths */
2410         if (control == NULL) {
2411                 list_add(&path->list, &dapm->card->paths);
2412                 list_add(&path->list_sink, &wsink->sources);
2413                 list_add(&path->list_source, &wsource->sinks);
2414                 path->connect = 1;
2415                 return 0;
2416         }
2417
2418         /* connect dynamic paths */
2419         switch (wsink->id) {
2420         case snd_soc_dapm_adc:
2421         case snd_soc_dapm_dac:
2422         case snd_soc_dapm_pga:
2423         case snd_soc_dapm_out_drv:
2424         case snd_soc_dapm_input:
2425         case snd_soc_dapm_output:
2426         case snd_soc_dapm_siggen:
2427         case snd_soc_dapm_micbias:
2428         case snd_soc_dapm_vmid:
2429         case snd_soc_dapm_pre:
2430         case snd_soc_dapm_post:
2431         case snd_soc_dapm_supply:
2432         case snd_soc_dapm_regulator_supply:
2433         case snd_soc_dapm_clock_supply:
2434         case snd_soc_dapm_aif_in:
2435         case snd_soc_dapm_aif_out:
2436         case snd_soc_dapm_dai_in:
2437         case snd_soc_dapm_dai_out:
2438         case snd_soc_dapm_dai_link:
2439         case snd_soc_dapm_kcontrol:
2440                 list_add(&path->list, &dapm->card->paths);
2441                 list_add(&path->list_sink, &wsink->sources);
2442                 list_add(&path->list_source, &wsource->sinks);
2443                 path->connect = 1;
2444                 return 0;
2445         case snd_soc_dapm_mux:
2446         case snd_soc_dapm_virt_mux:
2447         case snd_soc_dapm_value_mux:
2448                 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2449                         &wsink->kcontrol_news[0]);
2450                 if (ret != 0)
2451                         goto err;
2452                 break;
2453         case snd_soc_dapm_switch:
2454         case snd_soc_dapm_mixer:
2455         case snd_soc_dapm_mixer_named_ctl:
2456                 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2457                 if (ret != 0)
2458                         goto err;
2459                 break;
2460         case snd_soc_dapm_hp:
2461         case snd_soc_dapm_mic:
2462         case snd_soc_dapm_line:
2463         case snd_soc_dapm_spk:
2464                 list_add(&path->list, &dapm->card->paths);
2465                 list_add(&path->list_sink, &wsink->sources);
2466                 list_add(&path->list_source, &wsource->sinks);
2467                 path->connect = 0;
2468                 return 0;
2469         }
2470
2471         return 0;
2472 err:
2473         kfree(path);
2474         return ret;
2475 }
2476
2477 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2478                                   const struct snd_soc_dapm_route *route,
2479                                   unsigned int is_prefixed)
2480 {
2481         struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2482         struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2483         const char *sink;
2484         const char *source;
2485         char prefixed_sink[80];
2486         char prefixed_source[80];
2487         int ret;
2488
2489         if (dapm->codec && dapm->codec->name_prefix && !is_prefixed) {
2490                 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2491                          dapm->codec->name_prefix, route->sink);
2492                 sink = prefixed_sink;
2493                 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2494                          dapm->codec->name_prefix, route->source);
2495                 source = prefixed_source;
2496         } else {
2497                 sink = route->sink;
2498                 source = route->source;
2499         }
2500
2501         /*
2502          * find src and dest widgets over all widgets but favor a widget from
2503          * current DAPM context
2504          */
2505         list_for_each_entry(w, &dapm->card->widgets, list) {
2506                 if (!wsink && !(strcmp(w->name, sink))) {
2507                         wtsink = w;
2508                         if (w->dapm == dapm)
2509                                 wsink = w;
2510                         continue;
2511                 }
2512                 if (!wsource && !(strcmp(w->name, source))) {
2513                         wtsource = w;
2514                         if (w->dapm == dapm)
2515                                 wsource = w;
2516                 }
2517         }
2518         /* use widget from another DAPM context if not found from this */
2519         if (!wsink)
2520                 wsink = wtsink;
2521         if (!wsource)
2522                 wsource = wtsource;
2523
2524         if (wsource == NULL) {
2525                 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2526                         route->source);
2527                 return -ENODEV;
2528         }
2529         if (wsink == NULL) {
2530                 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2531                         route->sink);
2532                 return -ENODEV;
2533         }
2534
2535         ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2536                 route->connected);
2537         if (ret)
2538                 goto err;
2539
2540         return 0;
2541 err:
2542         dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2543                  source, route->control, sink);
2544         return ret;
2545 }
2546
2547 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2548                                   const struct snd_soc_dapm_route *route)
2549 {
2550         struct snd_soc_dapm_path *path, *p;
2551         const char *sink;
2552         const char *source;
2553         char prefixed_sink[80];
2554         char prefixed_source[80];
2555
2556         if (route->control) {
2557                 dev_err(dapm->dev,
2558                         "ASoC: Removal of routes with controls not supported\n");
2559                 return -EINVAL;
2560         }
2561
2562         if (dapm->codec && dapm->codec->name_prefix) {
2563                 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2564                          dapm->codec->name_prefix, route->sink);
2565                 sink = prefixed_sink;
2566                 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2567                          dapm->codec->name_prefix, route->source);
2568                 source = prefixed_source;
2569         } else {
2570                 sink = route->sink;
2571                 source = route->source;
2572         }
2573
2574         path = NULL;
2575         list_for_each_entry(p, &dapm->card->paths, list) {
2576                 if (strcmp(p->source->name, source) != 0)
2577                         continue;
2578                 if (strcmp(p->sink->name, sink) != 0)
2579                         continue;
2580                 path = p;
2581                 break;
2582         }
2583
2584         if (path) {
2585                 dapm_mark_dirty(path->source, "Route removed");
2586                 dapm_mark_dirty(path->sink, "Route removed");
2587
2588                 dapm_free_path(path);
2589         } else {
2590                 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2591                          source, sink);
2592         }
2593
2594         return 0;
2595 }
2596
2597 /**
2598  * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2599  * @dapm: DAPM context
2600  * @route: audio routes
2601  * @num: number of routes
2602  *
2603  * Connects 2 dapm widgets together via a named audio path. The sink is
2604  * the widget receiving the audio signal, whilst the source is the sender
2605  * of the audio signal.
2606  *
2607  * Returns 0 for success else error. On error all resources can be freed
2608  * with a call to snd_soc_card_free().
2609  */
2610 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2611                             const struct snd_soc_dapm_route *route, int num)
2612 {
2613         int i, r, ret = 0;
2614
2615         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2616         for (i = 0; i < num; i++) {
2617                 r = snd_soc_dapm_add_route(dapm, route, false);
2618                 if (r < 0) {
2619                         dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2620                                 route->source,
2621                                 route->control ? route->control : "direct",
2622                                 route->sink);
2623                         ret = r;
2624                 }
2625                 route++;
2626         }
2627         mutex_unlock(&dapm->card->dapm_mutex);
2628
2629         return ret;
2630 }
2631 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2632
2633 /**
2634  * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2635  * @dapm: DAPM context
2636  * @route: audio routes
2637  * @num: number of routes
2638  *
2639  * Removes routes from the DAPM context.
2640  */
2641 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2642                             const struct snd_soc_dapm_route *route, int num)
2643 {
2644         int i, ret = 0;
2645
2646         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2647         for (i = 0; i < num; i++) {
2648                 snd_soc_dapm_del_route(dapm, route);
2649                 route++;
2650         }
2651         mutex_unlock(&dapm->card->dapm_mutex);
2652
2653         return ret;
2654 }
2655 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2656
2657 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2658                                    const struct snd_soc_dapm_route *route)
2659 {
2660         struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2661                                                               route->source,
2662                                                               true);
2663         struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2664                                                             route->sink,
2665                                                             true);
2666         struct snd_soc_dapm_path *path;
2667         int count = 0;
2668
2669         if (!source) {
2670                 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
2671                         route->source);
2672                 return -ENODEV;
2673         }
2674
2675         if (!sink) {
2676                 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
2677                         route->sink);
2678                 return -ENODEV;
2679         }
2680
2681         if (route->control || route->connected)
2682                 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
2683                          route->source, route->sink);
2684
2685         list_for_each_entry(path, &source->sinks, list_source) {
2686                 if (path->sink == sink) {
2687                         path->weak = 1;
2688                         count++;
2689                 }
2690         }
2691
2692         if (count == 0)
2693                 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
2694                         route->source, route->sink);
2695         if (count > 1)
2696                 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
2697                          count, route->source, route->sink);
2698
2699         return 0;
2700 }
2701
2702 /**
2703  * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2704  * @dapm: DAPM context
2705  * @route: audio routes
2706  * @num: number of routes
2707  *
2708  * Mark existing routes matching those specified in the passed array
2709  * as being weak, meaning that they are ignored for the purpose of
2710  * power decisions.  The main intended use case is for sidetone paths
2711  * which couple audio between other independent paths if they are both
2712  * active in order to make the combination work better at the user
2713  * level but which aren't intended to be "used".
2714  *
2715  * Note that CODEC drivers should not use this as sidetone type paths
2716  * can frequently also be used as bypass paths.
2717  */
2718 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2719                              const struct snd_soc_dapm_route *route, int num)
2720 {
2721         int i, err;
2722         int ret = 0;
2723
2724         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2725         for (i = 0; i < num; i++) {
2726                 err = snd_soc_dapm_weak_route(dapm, route);
2727                 if (err)
2728                         ret = err;
2729                 route++;
2730         }
2731         mutex_unlock(&dapm->card->dapm_mutex);
2732
2733         return ret;
2734 }
2735 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2736
2737 /**
2738  * snd_soc_dapm_new_widgets - add new dapm widgets
2739  * @dapm: DAPM context
2740  *
2741  * Checks the codec for any new dapm widgets and creates them if found.
2742  *
2743  * Returns 0 for success.
2744  */
2745 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
2746 {
2747         struct snd_soc_dapm_widget *w;
2748         unsigned int val;
2749
2750         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2751
2752         list_for_each_entry(w, &card->widgets, list)
2753         {
2754                 if (w->new)
2755                         continue;
2756
2757                 if (w->num_kcontrols) {
2758                         w->kcontrols = kzalloc(w->num_kcontrols *
2759                                                 sizeof(struct snd_kcontrol *),
2760                                                 GFP_KERNEL);
2761                         if (!w->kcontrols) {
2762                                 mutex_unlock(&card->dapm_mutex);
2763                                 return -ENOMEM;
2764                         }
2765                 }
2766
2767                 switch(w->id) {
2768                 case snd_soc_dapm_switch:
2769                 case snd_soc_dapm_mixer:
2770                 case snd_soc_dapm_mixer_named_ctl:
2771                         dapm_new_mixer(w);
2772                         break;
2773                 case snd_soc_dapm_mux:
2774                 case snd_soc_dapm_virt_mux:
2775                 case snd_soc_dapm_value_mux:
2776                         dapm_new_mux(w);
2777                         break;
2778                 case snd_soc_dapm_pga:
2779                 case snd_soc_dapm_out_drv:
2780                         dapm_new_pga(w);
2781                         break;
2782                 default:
2783                         break;
2784                 }
2785
2786                 /* Read the initial power state from the device */
2787                 if (w->reg >= 0) {
2788                         soc_widget_read(w, w->reg, &val);
2789                         val = val >> w->shift;
2790                         val &= w->mask;
2791                         if (val == w->on_val)
2792                                 w->power = 1;
2793                 }
2794
2795                 w->new = 1;
2796
2797                 dapm_mark_dirty(w, "new widget");
2798                 dapm_debugfs_add_widget(w);
2799         }
2800
2801         dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2802         mutex_unlock(&card->dapm_mutex);
2803         return 0;
2804 }
2805 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2806
2807 /**
2808  * snd_soc_dapm_get_volsw - dapm mixer get callback
2809  * @kcontrol: mixer control
2810  * @ucontrol: control element information
2811  *
2812  * Callback to get the value of a dapm mixer control.
2813  *
2814  * Returns 0 for success.
2815  */
2816 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2817         struct snd_ctl_elem_value *ucontrol)
2818 {
2819         struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2820         struct snd_soc_card *card = codec->card;
2821         struct soc_mixer_control *mc =
2822                 (struct soc_mixer_control *)kcontrol->private_value;
2823         int reg = mc->reg;
2824         unsigned int shift = mc->shift;
2825         int max = mc->max;
2826         unsigned int mask = (1 << fls(max)) - 1;
2827         unsigned int invert = mc->invert;
2828         unsigned int val;
2829
2830         if (snd_soc_volsw_is_stereo(mc))
2831                 dev_warn(codec->dapm.dev,
2832                          "ASoC: Control '%s' is stereo, which is not supported\n",
2833                          kcontrol->id.name);
2834
2835         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2836         if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM)
2837                 val = (snd_soc_read(codec, reg) >> shift) & mask;
2838         else
2839                 val = dapm_kcontrol_get_value(kcontrol);
2840         mutex_unlock(&card->dapm_mutex);
2841
2842         if (invert)
2843                 ucontrol->value.integer.value[0] = max - val;
2844         else
2845                 ucontrol->value.integer.value[0] = val;
2846
2847         return 0;
2848 }
2849 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2850
2851 /**
2852  * snd_soc_dapm_put_volsw - dapm mixer set callback
2853  * @kcontrol: mixer control
2854  * @ucontrol: control element information
2855  *
2856  * Callback to set the value of a dapm mixer control.
2857  *
2858  * Returns 0 for success.
2859  */
2860 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2861         struct snd_ctl_elem_value *ucontrol)
2862 {
2863         struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2864         struct snd_soc_card *card = codec->card;
2865         struct soc_mixer_control *mc =
2866                 (struct soc_mixer_control *)kcontrol->private_value;
2867         int reg = mc->reg;
2868         unsigned int shift = mc->shift;
2869         int max = mc->max;
2870         unsigned int mask = (1 << fls(max)) - 1;
2871         unsigned int invert = mc->invert;
2872         unsigned int val;
2873         int connect, change;
2874         struct snd_soc_dapm_update update;
2875         int ret = 0;
2876
2877         if (snd_soc_volsw_is_stereo(mc))
2878                 dev_warn(codec->dapm.dev,
2879                          "ASoC: Control '%s' is stereo, which is not supported\n",
2880                          kcontrol->id.name);
2881
2882         val = (ucontrol->value.integer.value[0] & mask);
2883         connect = !!val;
2884
2885         if (invert)
2886                 val = max - val;
2887
2888         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2889
2890         change = dapm_kcontrol_set_value(kcontrol, val);
2891         if (change) {
2892                 if (reg != SND_SOC_NOPM) {
2893                         mask = mask << shift;
2894                         val = val << shift;
2895
2896                         if (snd_soc_test_bits(codec, reg, mask, val)) {
2897                                 update.kcontrol = kcontrol;
2898                                 update.reg = reg;
2899                                 update.mask = mask;
2900                                 update.val = val;
2901                                 card->update = &update;
2902                         }
2903
2904                 }
2905
2906                 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
2907
2908                 card->update = NULL;
2909         }
2910
2911         mutex_unlock(&card->dapm_mutex);
2912
2913         if (ret > 0)
2914                 soc_dpcm_runtime_update(card);
2915
2916         return change;
2917 }
2918 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2919
2920 /**
2921  * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2922  * @kcontrol: mixer control
2923  * @ucontrol: control element information
2924  *
2925  * Callback to get the value of a dapm enumerated double mixer control.
2926  *
2927  * Returns 0 for success.
2928  */
2929 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2930         struct snd_ctl_elem_value *ucontrol)
2931 {
2932         struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2933         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2934         unsigned int val;
2935
2936         val = snd_soc_read(codec, e->reg);
2937         ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
2938         if (e->shift_l != e->shift_r)
2939                 ucontrol->value.enumerated.item[1] =
2940                         (val >> e->shift_r) & e->mask;
2941
2942         return 0;
2943 }
2944 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2945
2946 /**
2947  * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2948  * @kcontrol: mixer control
2949  * @ucontrol: control element information
2950  *
2951  * Callback to set the value of a dapm enumerated double mixer control.
2952  *
2953  * Returns 0 for success.
2954  */
2955 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2956         struct snd_ctl_elem_value *ucontrol)
2957 {
2958         struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2959         struct snd_soc_card *card = codec->card;
2960         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2961         unsigned int val, mux, change;
2962         unsigned int mask;
2963         struct snd_soc_dapm_update update;
2964         int ret = 0;
2965
2966         if (ucontrol->value.enumerated.item[0] > e->max - 1)
2967                 return -EINVAL;
2968         mux = ucontrol->value.enumerated.item[0];
2969         val = mux << e->shift_l;
2970         mask = e->mask << e->shift_l;
2971         if (e->shift_l != e->shift_r) {
2972                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2973                         return -EINVAL;
2974                 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2975                 mask |= e->mask << e->shift_r;
2976         }
2977
2978         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2979
2980         change = snd_soc_test_bits(codec, e->reg, mask, val);
2981         if (change) {
2982                 update.kcontrol = kcontrol;
2983                 update.reg = e->reg;
2984                 update.mask = mask;
2985                 update.val = val;
2986                 card->update = &update;
2987
2988                 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2989
2990                 card->update = NULL;
2991         }
2992
2993         mutex_unlock(&card->dapm_mutex);
2994
2995         if (ret > 0)
2996                 soc_dpcm_runtime_update(card);
2997
2998         return change;
2999 }
3000 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3001
3002 /**
3003  * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
3004  * @kcontrol: mixer control
3005  * @ucontrol: control element information
3006  *
3007  * Returns 0 for success.
3008  */
3009 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
3010                                struct snd_ctl_elem_value *ucontrol)
3011 {
3012         ucontrol->value.enumerated.item[0] = dapm_kcontrol_get_value(kcontrol);
3013         return 0;
3014 }
3015 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
3016
3017 /**
3018  * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
3019  * @kcontrol: mixer control
3020  * @ucontrol: control element information
3021  *
3022  * Returns 0 for success.
3023  */
3024 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
3025                                struct snd_ctl_elem_value *ucontrol)
3026 {
3027         struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
3028         struct snd_soc_card *card = codec->card;
3029         unsigned int value;
3030         struct soc_enum *e =
3031                 (struct soc_enum *)kcontrol->private_value;
3032         int change;
3033         int ret = 0;
3034
3035         if (ucontrol->value.enumerated.item[0] >= e->max)
3036                 return -EINVAL;
3037
3038         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3039
3040         value = ucontrol->value.enumerated.item[0];
3041         change = dapm_kcontrol_set_value(kcontrol, value);
3042         if (change)
3043                 ret = soc_dapm_mux_update_power(card, kcontrol, value, e);
3044
3045         mutex_unlock(&card->dapm_mutex);
3046
3047         if (ret > 0)
3048                 soc_dpcm_runtime_update(card);
3049
3050         return change;
3051 }
3052 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
3053
3054 /**
3055  * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
3056  *                                      callback
3057  * @kcontrol: mixer control
3058  * @ucontrol: control element information
3059  *
3060  * Callback to get the value of a dapm semi enumerated double mixer control.
3061  *
3062  * Semi enumerated mixer: the enumerated items are referred as values. Can be
3063  * used for handling bitfield coded enumeration for example.
3064  *
3065  * Returns 0 for success.
3066  */
3067 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
3068         struct snd_ctl_elem_value *ucontrol)
3069 {
3070         struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
3071         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3072         unsigned int reg_val, val, mux;
3073
3074         reg_val = snd_soc_read(codec, e->reg);
3075         val = (reg_val >> e->shift_l) & e->mask;
3076         for (mux = 0; mux < e->max; mux++) {
3077                 if (val == e->values[mux])
3078                         break;
3079         }
3080         ucontrol->value.enumerated.item[0] = mux;
3081         if (e->shift_l != e->shift_r) {
3082                 val = (reg_val >> e->shift_r) & e->mask;
3083                 for (mux = 0; mux < e->max; mux++) {
3084                         if (val == e->values[mux])
3085                                 break;
3086                 }
3087                 ucontrol->value.enumerated.item[1] = mux;
3088         }
3089
3090         return 0;
3091 }
3092 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
3093
3094 /**
3095  * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
3096  *                                      callback
3097  * @kcontrol: mixer control
3098  * @ucontrol: control element information
3099  *
3100  * Callback to set the value of a dapm semi enumerated double mixer control.
3101  *
3102  * Semi enumerated mixer: the enumerated items are referred as values. Can be
3103  * used for handling bitfield coded enumeration for example.
3104  *
3105  * Returns 0 for success.
3106  */
3107 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
3108         struct snd_ctl_elem_value *ucontrol)
3109 {
3110         struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
3111         struct snd_soc_card *card = codec->card;
3112         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3113         unsigned int val, mux, change;
3114         unsigned int mask;
3115         struct snd_soc_dapm_update update;
3116         int ret = 0;
3117
3118         if (ucontrol->value.enumerated.item[0] > e->max - 1)
3119                 return -EINVAL;
3120         mux = ucontrol->value.enumerated.item[0];
3121         val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
3122         mask = e->mask << e->shift_l;
3123         if (e->shift_l != e->shift_r) {
3124                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
3125                         return -EINVAL;
3126                 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
3127                 mask |= e->mask << e->shift_r;
3128         }
3129
3130         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3131
3132         change = snd_soc_test_bits(codec, e->reg, mask, val);
3133         if (change) {
3134                 update.kcontrol = kcontrol;
3135                 update.reg = e->reg;
3136                 update.mask = mask;
3137                 update.val = val;
3138                 card->update = &update;
3139
3140                 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
3141
3142                 card->update = NULL;
3143         }
3144
3145         mutex_unlock(&card->dapm_mutex);
3146
3147         if (ret > 0)
3148                 soc_dpcm_runtime_update(card);
3149
3150         return change;
3151 }
3152 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
3153
3154 /**
3155  * snd_soc_dapm_info_pin_switch - Info for a pin switch
3156  *
3157  * @kcontrol: mixer control
3158  * @uinfo: control element information
3159  *
3160  * Callback to provide information about a pin switch control.
3161  */
3162 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3163                                  struct snd_ctl_elem_info *uinfo)
3164 {
3165         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3166         uinfo->count = 1;
3167         uinfo->value.integer.min = 0;
3168         uinfo->value.integer.max = 1;
3169
3170         return 0;
3171 }
3172 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3173
3174 /**
3175  * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3176  *
3177  * @kcontrol: mixer control
3178  * @ucontrol: Value
3179  */
3180 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3181                                 struct snd_ctl_elem_value *ucontrol)
3182 {
3183         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3184         const char *pin = (const char *)kcontrol->private_value;
3185
3186         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3187
3188         ucontrol->value.integer.value[0] =
3189                 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3190
3191         mutex_unlock(&card->dapm_mutex);
3192
3193         return 0;
3194 }
3195 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3196
3197 /**
3198  * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3199  *
3200  * @kcontrol: mixer control
3201  * @ucontrol: Value
3202  */
3203 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3204                                 struct snd_ctl_elem_value *ucontrol)
3205 {
3206         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3207         const char *pin = (const char *)kcontrol->private_value;
3208
3209         if (ucontrol->value.integer.value[0])
3210                 snd_soc_dapm_enable_pin(&card->dapm, pin);
3211         else
3212                 snd_soc_dapm_disable_pin(&card->dapm, pin);
3213
3214         snd_soc_dapm_sync(&card->dapm);
3215         return 0;
3216 }
3217 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3218
3219 static struct snd_soc_dapm_widget *
3220 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3221                          const struct snd_soc_dapm_widget *widget)
3222 {
3223         struct snd_soc_dapm_widget *w;
3224         int ret;
3225
3226         if ((w = dapm_cnew_widget(widget)) == NULL)
3227                 return NULL;
3228
3229         switch (w->id) {
3230         case snd_soc_dapm_regulator_supply:
3231                 w->regulator = devm_regulator_get(dapm->dev, w->name);
3232                 if (IS_ERR(w->regulator)) {
3233                         ret = PTR_ERR(w->regulator);
3234                         dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3235                                 w->name, ret);
3236                         return NULL;
3237                 }
3238
3239                 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3240                         ret = regulator_allow_bypass(w->regulator, true);
3241                         if (ret != 0)
3242                                 dev_warn(w->dapm->dev,
3243                                          "ASoC: Failed to bypass %s: %d\n",
3244                                          w->name, ret);
3245                 }
3246                 break;
3247         case snd_soc_dapm_clock_supply:
3248 #ifdef CONFIG_CLKDEV_LOOKUP
3249                 w->clk = devm_clk_get(dapm->dev, w->name);
3250                 if (IS_ERR(w->clk)) {
3251                         ret = PTR_ERR(w->clk);
3252                         dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3253                                 w->name, ret);
3254                         return NULL;
3255                 }
3256 #else
3257                 return NULL;
3258 #endif
3259                 break;
3260         default:
3261                 break;
3262         }
3263
3264         if (dapm->codec && dapm->codec->name_prefix)
3265                 w->name = kasprintf(GFP_KERNEL, "%s %s",
3266                         dapm->codec->name_prefix, widget->name);
3267         else
3268                 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3269
3270         if (w->name == NULL) {
3271                 kfree(w);
3272                 return NULL;
3273         }
3274
3275         switch (w->id) {
3276         case snd_soc_dapm_switch:
3277         case snd_soc_dapm_mixer:
3278         case snd_soc_dapm_mixer_named_ctl:
3279                 w->power_check = dapm_generic_check_power;
3280                 break;
3281         case snd_soc_dapm_mux:
3282         case snd_soc_dapm_virt_mux:
3283         case snd_soc_dapm_value_mux:
3284                 w->power_check = dapm_generic_check_power;
3285                 break;
3286         case snd_soc_dapm_dai_out:
3287                 w->power_check = dapm_adc_check_power;
3288                 break;
3289         case snd_soc_dapm_dai_in:
3290                 w->power_check = dapm_dac_check_power;
3291                 break;
3292         case snd_soc_dapm_adc:
3293         case snd_soc_dapm_aif_out:
3294         case snd_soc_dapm_dac:
3295         case snd_soc_dapm_aif_in:
3296         case snd_soc_dapm_pga:
3297         case snd_soc_dapm_out_drv:
3298         case snd_soc_dapm_input:
3299         case snd_soc_dapm_output:
3300         case snd_soc_dapm_micbias:
3301         case snd_soc_dapm_spk:
3302         case snd_soc_dapm_hp:
3303         case snd_soc_dapm_mic:
3304         case snd_soc_dapm_line:
3305         case snd_soc_dapm_dai_link:
3306                 w->power_check = dapm_generic_check_power;
3307                 break;
3308         case snd_soc_dapm_supply:
3309         case snd_soc_dapm_regulator_supply:
3310         case snd_soc_dapm_clock_supply:
3311         case snd_soc_dapm_kcontrol:
3312                 w->power_check = dapm_supply_check_power;
3313                 break;
3314         default:
3315                 w->power_check = dapm_always_on_check_power;
3316                 break;
3317         }
3318
3319         w->dapm = dapm;
3320         w->codec = dapm->codec;
3321         w->platform = dapm->platform;
3322         INIT_LIST_HEAD(&w->sources);
3323         INIT_LIST_HEAD(&w->sinks);
3324         INIT_LIST_HEAD(&w->list);
3325         INIT_LIST_HEAD(&w->dirty);
3326         list_add(&w->list, &dapm->card->widgets);
3327
3328         /* machine layer set ups unconnected pins and insertions */
3329         w->connected = 1;
3330         return w;
3331 }
3332
3333 /**
3334  * snd_soc_dapm_new_controls - create new dapm controls
3335  * @dapm: DAPM context
3336  * @widget: widget array
3337  * @num: number of widgets
3338  *
3339  * Creates new DAPM controls based upon the templates.
3340  *
3341  * Returns 0 for success else error.
3342  */
3343 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3344         const struct snd_soc_dapm_widget *widget,
3345         int num)
3346 {
3347         struct snd_soc_dapm_widget *w;
3348         int i;
3349         int ret = 0;
3350
3351         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3352         for (i = 0; i < num; i++) {
3353                 w = snd_soc_dapm_new_control(dapm, widget);
3354                 if (!w) {
3355                         dev_err(dapm->dev,
3356                                 "ASoC: Failed to create DAPM control %s\n",
3357                                 widget->name);
3358                         ret = -ENOMEM;
3359                         break;
3360                 }
3361                 widget++;
3362         }
3363         mutex_unlock(&dapm->card->dapm_mutex);
3364         return ret;
3365 }
3366 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3367
3368 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3369                                   struct snd_kcontrol *kcontrol, int event)
3370 {
3371         struct snd_soc_dapm_path *source_p, *sink_p;
3372         struct snd_soc_dai *source, *sink;
3373         const struct snd_soc_pcm_stream *config = w->params;
3374         struct snd_pcm_substream substream;
3375         struct snd_pcm_hw_params *params = NULL;
3376         u64 fmt;
3377         int ret;
3378
3379         if (WARN_ON(!config) ||
3380             WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3381                 return -EINVAL;
3382
3383         /* We only support a single source and sink, pick the first */
3384         source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3385                                     list_sink);
3386         sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3387                                   list_source);
3388
3389         if (WARN_ON(!source_p || !sink_p) ||
3390             WARN_ON(!sink_p->source || !source_p->sink) ||
3391             WARN_ON(!source_p->source || !sink_p->sink))
3392                 return -EINVAL;
3393
3394         source = source_p->source->priv;
3395         sink = sink_p->sink->priv;
3396
3397         /* Be a little careful as we don't want to overflow the mask array */
3398         if (config->formats) {
3399                 fmt = ffs(config->formats) - 1;
3400         } else {
3401                 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3402                          config->formats);
3403                 fmt = 0;
3404         }
3405
3406         /* Currently very limited parameter selection */
3407         params = kzalloc(sizeof(*params), GFP_KERNEL);
3408         if (!params) {
3409                 ret = -ENOMEM;
3410                 goto out;
3411         }
3412         snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3413
3414         hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3415                 config->rate_min;
3416         hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3417                 config->rate_max;
3418
3419         hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3420                 = config->channels_min;
3421         hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3422                 = config->channels_max;
3423
3424         memset(&substream, 0, sizeof(substream));
3425
3426         switch (event) {
3427         case SND_SOC_DAPM_PRE_PMU:
3428                 if (source->driver->ops && source->driver->ops->hw_params) {
3429                         substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3430                         ret = source->driver->ops->hw_params(&substream,
3431                                                              params, source);
3432                         if (ret != 0) {
3433                                 dev_err(source->dev,
3434                                         "ASoC: hw_params() failed: %d\n", ret);
3435                                 goto out;
3436                         }
3437                 }
3438
3439                 if (sink->driver->ops && sink->driver->ops->hw_params) {
3440                         substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3441                         ret = sink->driver->ops->hw_params(&substream, params,
3442                                                            sink);
3443                         if (ret != 0) {
3444                                 dev_err(sink->dev,
3445                                         "ASoC: hw_params() failed: %d\n", ret);
3446                                 goto out;
3447                         }
3448                 }
3449                 break;
3450
3451         case SND_SOC_DAPM_POST_PMU:
3452                 ret = snd_soc_dai_digital_mute(sink, 0,
3453                                                SNDRV_PCM_STREAM_PLAYBACK);
3454                 if (ret != 0 && ret != -ENOTSUPP)
3455                         dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
3456                 ret = 0;
3457                 break;
3458
3459         case SND_SOC_DAPM_PRE_PMD:
3460                 ret = snd_soc_dai_digital_mute(sink, 1,
3461                                                SNDRV_PCM_STREAM_PLAYBACK);
3462                 if (ret != 0 && ret != -ENOTSUPP)
3463                         dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
3464                 ret = 0;
3465                 break;
3466
3467         default:
3468                 WARN(1, "Unknown event %d\n", event);
3469                 return -EINVAL;
3470         }
3471
3472 out:
3473         kfree(params);
3474         return ret;
3475 }
3476
3477 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3478                          const struct snd_soc_pcm_stream *params,
3479                          struct snd_soc_dapm_widget *source,
3480                          struct snd_soc_dapm_widget *sink)
3481 {
3482         struct snd_soc_dapm_route routes[2];
3483         struct snd_soc_dapm_widget template;
3484         struct snd_soc_dapm_widget *w;
3485         size_t len;
3486         char *link_name;
3487
3488         len = strlen(source->name) + strlen(sink->name) + 2;
3489         link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3490         if (!link_name)
3491                 return -ENOMEM;
3492         snprintf(link_name, len, "%s-%s", source->name, sink->name);
3493
3494         memset(&template, 0, sizeof(template));
3495         template.reg = SND_SOC_NOPM;
3496         template.id = snd_soc_dapm_dai_link;
3497         template.name = link_name;
3498         template.event = snd_soc_dai_link_event;
3499         template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3500                 SND_SOC_DAPM_PRE_PMD;
3501
3502         dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3503
3504         w = snd_soc_dapm_new_control(&card->dapm, &template);
3505         if (!w) {
3506                 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
3507                         link_name);
3508                 return -ENOMEM;
3509         }
3510
3511         w->params = params;
3512
3513         memset(&routes, 0, sizeof(routes));
3514
3515         routes[0].source = source->name;
3516         routes[0].sink = link_name;
3517         routes[1].source = link_name;
3518         routes[1].sink = sink->name;
3519
3520         return snd_soc_dapm_add_routes(&card->dapm, routes,
3521                                        ARRAY_SIZE(routes));
3522 }
3523
3524 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3525                                  struct snd_soc_dai *dai)
3526 {
3527         struct snd_soc_dapm_widget template;
3528         struct snd_soc_dapm_widget *w;
3529
3530         WARN_ON(dapm->dev != dai->dev);
3531
3532         memset(&template, 0, sizeof(template));
3533         template.reg = SND_SOC_NOPM;
3534
3535         if (dai->driver->playback.stream_name) {
3536                 template.id = snd_soc_dapm_dai_in;
3537                 template.name = dai->driver->playback.stream_name;
3538                 template.sname = dai->driver->playback.stream_name;
3539
3540                 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3541                         template.name);
3542
3543                 w = snd_soc_dapm_new_control(dapm, &template);
3544                 if (!w) {
3545                         dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3546                                 dai->driver->playback.stream_name);
3547                         return -ENOMEM;
3548                 }
3549
3550                 w->priv = dai;
3551                 dai->playback_widget = w;
3552         }
3553
3554         if (dai->driver->capture.stream_name) {
3555                 template.id = snd_soc_dapm_dai_out;
3556                 template.name = dai->driver->capture.stream_name;
3557                 template.sname = dai->driver->capture.stream_name;
3558
3559                 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3560                         template.name);
3561
3562                 w = snd_soc_dapm_new_control(dapm, &template);
3563                 if (!w) {
3564                         dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3565                                 dai->driver->capture.stream_name);
3566                         return -ENOMEM;
3567                 }
3568
3569                 w->priv = dai;
3570                 dai->capture_widget = w;
3571         }
3572
3573         return 0;
3574 }
3575
3576 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3577 {
3578         struct snd_soc_dapm_widget *dai_w, *w;
3579         struct snd_soc_dai *dai;
3580
3581         /* For each DAI widget... */
3582         list_for_each_entry(dai_w, &card->widgets, list) {
3583                 switch (dai_w->id) {
3584                 case snd_soc_dapm_dai_in:
3585                 case snd_soc_dapm_dai_out:
3586                         break;
3587                 default:
3588                         continue;
3589                 }
3590
3591                 dai = dai_w->priv;
3592
3593                 /* ...find all widgets with the same stream and link them */
3594                 list_for_each_entry(w, &card->widgets, list) {
3595                         if (w->dapm != dai_w->dapm)
3596                                 continue;
3597
3598                         switch (w->id) {
3599                         case snd_soc_dapm_dai_in:
3600                         case snd_soc_dapm_dai_out:
3601                                 continue;
3602                         default:
3603                                 break;
3604                         }
3605
3606                         if (!w->sname || !strstr(w->sname, dai_w->name))
3607                                 continue;
3608
3609                         if (dai->driver->playback.stream_name &&
3610                             strstr(w->sname,
3611                                    dai->driver->playback.stream_name)) {
3612                                 dev_dbg(dai->dev, "%s -> %s\n",
3613                                          dai->playback_widget->name, w->name);
3614
3615                                 snd_soc_dapm_add_path(w->dapm,
3616                                         dai->playback_widget, w, NULL, NULL);
3617                         }
3618
3619                         if (dai->driver->capture.stream_name &&
3620                             strstr(w->sname,
3621                                    dai->driver->capture.stream_name)) {
3622                                 dev_dbg(dai->dev, "%s -> %s\n",
3623                                         w->name, dai->capture_widget->name);
3624
3625                                 snd_soc_dapm_add_path(w->dapm, w,
3626                                         dai->capture_widget, NULL, NULL);
3627                         }
3628                 }
3629         }
3630
3631         return 0;
3632 }
3633
3634 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3635 {
3636         struct snd_soc_pcm_runtime *rtd = card->rtd;
3637         struct snd_soc_dai *cpu_dai, *codec_dai;
3638         struct snd_soc_dapm_route r;
3639         int i;
3640
3641         memset(&r, 0, sizeof(r));
3642
3643         /* for each BE DAI link... */
3644         for (i = 0; i < card->num_rtd; i++) {
3645                 rtd = &card->rtd[i];
3646                 cpu_dai = rtd->cpu_dai;
3647                 codec_dai = rtd->codec_dai;
3648
3649                 /*
3650                  * dynamic FE links have no fixed DAI mapping.
3651                  * CODEC<->CODEC links have no direct connection.
3652                  */
3653                 if (rtd->dai_link->dynamic || rtd->dai_link->params)
3654                         continue;
3655
3656                 /* there is no point in connecting BE DAI links with dummies */
3657                 if (snd_soc_dai_is_dummy(codec_dai) ||
3658                         snd_soc_dai_is_dummy(cpu_dai))
3659                         continue;
3660
3661                 /* connect BE DAI playback if widgets are valid */
3662                 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
3663                         r.source = cpu_dai->playback_widget->name;
3664                         r.sink = codec_dai->playback_widget->name;
3665                         dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3666                                 cpu_dai->codec->name, r.source,
3667                                 codec_dai->platform->name, r.sink);
3668
3669                         snd_soc_dapm_add_route(&card->dapm, &r, true);
3670                 }
3671
3672                 /* connect BE DAI capture if widgets are valid */
3673                 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
3674                         r.source = codec_dai->capture_widget->name;
3675                         r.sink = cpu_dai->capture_widget->name;
3676                         dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3677                                 codec_dai->codec->name, r.source,
3678                                 cpu_dai->platform->name, r.sink);
3679
3680                         snd_soc_dapm_add_route(&card->dapm, &r, true);
3681                 }
3682
3683         }
3684 }
3685
3686 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3687         int event)
3688 {
3689
3690         struct snd_soc_dapm_widget *w_cpu, *w_codec;
3691         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3692         struct snd_soc_dai *codec_dai = rtd->codec_dai;
3693
3694         if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3695                 w_cpu = cpu_dai->playback_widget;
3696                 w_codec = codec_dai->playback_widget;
3697         } else {
3698                 w_cpu = cpu_dai->capture_widget;
3699                 w_codec = codec_dai->capture_widget;
3700         }
3701
3702         if (w_cpu) {
3703
3704                 dapm_mark_dirty(w_cpu, "stream event");
3705
3706                 switch (event) {
3707                 case SND_SOC_DAPM_STREAM_START:
3708                         w_cpu->active = 1;
3709                         break;
3710                 case SND_SOC_DAPM_STREAM_STOP:
3711                         w_cpu->active = 0;
3712                         break;
3713                 case SND_SOC_DAPM_STREAM_SUSPEND:
3714                 case SND_SOC_DAPM_STREAM_RESUME:
3715                 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3716                 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3717                         break;
3718                 }
3719         }
3720
3721         if (w_codec) {
3722
3723                 dapm_mark_dirty(w_codec, "stream event");
3724
3725                 switch (event) {
3726                 case SND_SOC_DAPM_STREAM_START:
3727                         w_codec->active = 1;
3728                         break;
3729                 case SND_SOC_DAPM_STREAM_STOP:
3730                         w_codec->active = 0;
3731                         break;
3732                 case SND_SOC_DAPM_STREAM_SUSPEND:
3733                 case SND_SOC_DAPM_STREAM_RESUME:
3734                 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3735                 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3736                         break;
3737                 }
3738         }
3739
3740         dapm_power_widgets(rtd->card, event);
3741 }
3742
3743 /**
3744  * snd_soc_dapm_stream_event - send a stream event to the dapm core
3745  * @rtd: PCM runtime data
3746  * @stream: stream name
3747  * @event: stream event
3748  *
3749  * Sends a stream event to the dapm core. The core then makes any
3750  * necessary widget power changes.
3751  *
3752  * Returns 0 for success else error.
3753  */
3754 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3755                               int event)
3756 {
3757         struct snd_soc_card *card = rtd->card;
3758
3759         mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3760         soc_dapm_stream_event(rtd, stream, event);
3761         mutex_unlock(&card->dapm_mutex);
3762 }
3763
3764 /**
3765  * snd_soc_dapm_enable_pin_unlocked - enable pin.
3766  * @dapm: DAPM context
3767  * @pin: pin name
3768  *
3769  * Enables input/output pin and its parents or children widgets iff there is
3770  * a valid audio route and active audio stream.
3771  *
3772  * Requires external locking.
3773  *
3774  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3775  * do any widget power switching.
3776  */
3777 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3778                                    const char *pin)
3779 {
3780         return snd_soc_dapm_set_pin(dapm, pin, 1);
3781 }
3782 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3783
3784 /**
3785  * snd_soc_dapm_enable_pin - enable pin.
3786  * @dapm: DAPM context
3787  * @pin: pin name
3788  *
3789  * Enables input/output pin and its parents or children widgets iff there is
3790  * a valid audio route and active audio stream.
3791  *
3792  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3793  * do any widget power switching.
3794  */
3795 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3796 {
3797         int ret;
3798
3799         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3800
3801         ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3802
3803         mutex_unlock(&dapm->card->dapm_mutex);
3804
3805         return ret;
3806 }
3807 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
3808
3809 /**
3810  * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3811  * @dapm: DAPM context
3812  * @pin: pin name
3813  *
3814  * Enables input/output pin regardless of any other state.  This is
3815  * intended for use with microphone bias supplies used in microphone
3816  * jack detection.
3817  *
3818  * Requires external locking.
3819  *
3820  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3821  * do any widget power switching.
3822  */
3823 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3824                                          const char *pin)
3825 {
3826         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3827
3828         if (!w) {
3829                 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3830                 return -EINVAL;
3831         }
3832
3833         dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3834         w->connected = 1;
3835         w->force = 1;
3836         dapm_mark_dirty(w, "force enable");
3837
3838         return 0;
3839 }
3840 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3841
3842 /**
3843  * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3844  * @dapm: DAPM context
3845  * @pin: pin name
3846  *
3847  * Enables input/output pin regardless of any other state.  This is
3848  * intended for use with microphone bias supplies used in microphone
3849  * jack detection.
3850  *
3851  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3852  * do any widget power switching.
3853  */
3854 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3855                                   const char *pin)
3856 {
3857         int ret;
3858
3859         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3860
3861         ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
3862
3863         mutex_unlock(&dapm->card->dapm_mutex);
3864
3865         return ret;
3866 }
3867 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3868
3869 /**
3870  * snd_soc_dapm_disable_pin_unlocked - disable pin.
3871  * @dapm: DAPM context
3872  * @pin: pin name
3873  *
3874  * Disables input/output pin and its parents or children widgets.
3875  *
3876  * Requires external locking.
3877  *
3878  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3879  * do any widget power switching.
3880  */
3881 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3882                                     const char *pin)
3883 {
3884         return snd_soc_dapm_set_pin(dapm, pin, 0);
3885 }
3886 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3887
3888 /**
3889  * snd_soc_dapm_disable_pin - disable pin.
3890  * @dapm: DAPM context
3891  * @pin: pin name
3892  *
3893  * Disables input/output pin and its parents or children widgets.
3894  *
3895  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3896  * do any widget power switching.
3897  */
3898 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3899                              const char *pin)
3900 {
3901         int ret;
3902
3903         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3904
3905         ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3906
3907         mutex_unlock(&dapm->card->dapm_mutex);
3908
3909         return ret;
3910 }
3911 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3912
3913 /**
3914  * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3915  * @dapm: DAPM context
3916  * @pin: pin name
3917  *
3918  * Marks the specified pin as being not connected, disabling it along
3919  * any parent or child widgets.  At present this is identical to
3920  * snd_soc_dapm_disable_pin() but in future it will be extended to do
3921  * additional things such as disabling controls which only affect
3922  * paths through the pin.
3923  *
3924  * Requires external locking.
3925  *
3926  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3927  * do any widget power switching.
3928  */
3929 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3930                                const char *pin)
3931 {
3932         return snd_soc_dapm_set_pin(dapm, pin, 0);
3933 }
3934 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3935
3936 /**
3937  * snd_soc_dapm_nc_pin - permanently disable pin.
3938  * @dapm: DAPM context
3939  * @pin: pin name
3940  *
3941  * Marks the specified pin as being not connected, disabling it along
3942  * any parent or child widgets.  At present this is identical to
3943  * snd_soc_dapm_disable_pin() but in future it will be extended to do
3944  * additional things such as disabling controls which only affect
3945  * paths through the pin.
3946  *
3947  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3948  * do any widget power switching.
3949  */
3950 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3951 {
3952         int ret;
3953
3954         mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3955
3956         ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3957
3958         mutex_unlock(&dapm->card->dapm_mutex);
3959
3960         return ret;
3961 }
3962 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3963
3964 /**
3965  * snd_soc_dapm_get_pin_status - get audio pin status
3966  * @dapm: DAPM context
3967  * @pin: audio signal pin endpoint (or start point)
3968  *
3969  * Get audio pin status - connected or disconnected.
3970  *
3971  * Returns 1 for connected otherwise 0.
3972  */
3973 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3974                                 const char *pin)
3975 {
3976         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3977
3978         if (w)
3979                 return w->connected;
3980
3981         return 0;
3982 }
3983 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
3984
3985 /**
3986  * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3987  * @dapm: DAPM context
3988  * @pin: audio signal pin endpoint (or start point)
3989  *
3990  * Mark the given endpoint or pin as ignoring suspend.  When the
3991  * system is disabled a path between two endpoints flagged as ignoring
3992  * suspend will not be disabled.  The path must already be enabled via
3993  * normal means at suspend time, it will not be turned on if it was not
3994  * already enabled.
3995  */
3996 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3997                                 const char *pin)
3998 {
3999         struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4000
4001         if (!w) {
4002                 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4003                 return -EINVAL;
4004         }
4005
4006         w->ignore_suspend = 1;
4007
4008         return 0;
4009 }
4010 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4011
4012 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
4013                                               struct snd_soc_dapm_widget *w)
4014 {
4015         struct snd_soc_dapm_path *p;
4016
4017         list_for_each_entry(p, &card->paths, list) {
4018                 if ((p->source == w) || (p->sink == w)) {
4019                         dev_dbg(card->dev,
4020                             "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
4021                             p->source->name, p->source->id, p->source->dapm,
4022                             p->sink->name, p->sink->id, p->sink->dapm);
4023
4024                         /* Connected to something other than the codec */
4025                         if (p->source->dapm != p->sink->dapm)
4026                                 return true;
4027                         /*
4028                          * Loopback connection from codec external pin to
4029                          * codec external pin
4030                          */
4031                         if (p->sink->id == snd_soc_dapm_input) {
4032                                 switch (p->source->id) {
4033                                 case snd_soc_dapm_output:
4034                                 case snd_soc_dapm_micbias:
4035                                         return true;
4036                                 default:
4037                                         break;
4038                                 }
4039                         }
4040                 }
4041         }
4042
4043         return false;
4044 }
4045
4046 /**
4047  * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
4048  * @codec: The codec whose pins should be processed
4049  *
4050  * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
4051  * which are unused. Pins are used if they are connected externally to the
4052  * codec, whether that be to some other device, or a loop-back connection to
4053  * the codec itself.
4054  */
4055 void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
4056 {
4057         struct snd_soc_card *card = codec->card;
4058         struct snd_soc_dapm_context *dapm = &codec->dapm;
4059         struct snd_soc_dapm_widget *w;
4060
4061         dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
4062                 &card->dapm, &codec->dapm);
4063
4064         list_for_each_entry(w, &card->widgets, list) {
4065                 if (w->dapm != dapm)
4066                         continue;
4067                 switch (w->id) {
4068                 case snd_soc_dapm_input:
4069                 case snd_soc_dapm_output:
4070                 case snd_soc_dapm_micbias:
4071                         dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
4072                                 w->name);
4073                         if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
4074                                 dev_dbg(codec->dev,
4075                                         "... Not in map; disabling\n");
4076                                 snd_soc_dapm_nc_pin(dapm, w->name);
4077                         }
4078                         break;
4079                 default:
4080                         break;
4081                 }
4082         }
4083 }
4084
4085 /**
4086  * snd_soc_dapm_free - free dapm resources
4087  * @dapm: DAPM context
4088  *
4089  * Free all dapm widgets and resources.
4090  */
4091 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4092 {
4093         snd_soc_dapm_sys_remove(dapm->dev);
4094         dapm_debugfs_cleanup(dapm);
4095         dapm_free_widgets(dapm);
4096         list_del(&dapm->list);
4097 }
4098 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4099
4100 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
4101 {
4102         struct snd_soc_card *card = dapm->card;
4103         struct snd_soc_dapm_widget *w;
4104         LIST_HEAD(down_list);
4105         int powerdown = 0;
4106
4107         mutex_lock(&card->dapm_mutex);
4108
4109         list_for_each_entry(w, &dapm->card->widgets, list) {
4110                 if (w->dapm != dapm)
4111                         continue;
4112                 if (w->power) {
4113                         dapm_seq_insert(w, &down_list, false);
4114                         w->power = 0;
4115                         powerdown = 1;
4116                 }
4117         }
4118
4119         /* If there were no widgets to power down we're already in
4120          * standby.
4121          */
4122         if (powerdown) {
4123                 if (dapm->bias_level == SND_SOC_BIAS_ON)
4124                         snd_soc_dapm_set_bias_level(dapm,
4125                                                     SND_SOC_BIAS_PREPARE);
4126                 dapm_seq_run(card, &down_list, 0, false);
4127                 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4128                         snd_soc_dapm_set_bias_level(dapm,
4129                                                     SND_SOC_BIAS_STANDBY);
4130         }
4131
4132         mutex_unlock(&card->dapm_mutex);
4133 }
4134
4135 /*
4136  * snd_soc_dapm_shutdown - callback for system shutdown
4137  */
4138 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4139 {
4140         struct snd_soc_codec *codec;
4141
4142         list_for_each_entry(codec, &card->codec_dev_list, card_list) {
4143                 soc_dapm_shutdown_codec(&codec->dapm);
4144                 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4145                         snd_soc_dapm_set_bias_level(&codec->dapm,
4146                                                     SND_SOC_BIAS_OFF);
4147         }
4148 }
4149
4150 /* Module information */
4151 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4152 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4153 MODULE_LICENSE("GPL");