~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/soc/soc-dapm.c

Version: ~ [ linux-5.4-rc3 ] ~ [ linux-5.3.6 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.79 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.149 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.196 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.196 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.75 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp