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