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