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