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