blob: 074e9ce136f806e0cebf48d69bbc1b4504dd9735 [file] [log] [blame]
Richard Purdie2b97eab2006-10-06 18:32:18 +02001/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020012 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
Mark Brown74b8f952009-06-06 11:26:15 +010015 * DACs/ADCs.
Richard Purdie2b97eab2006-10-06 18:32:18 +020016 * o Platform power domain - can support external components i.e. amps and
Liam Girdwood612a3fe2012-02-06 16:05:29 +000017 * mic/headphone insertion events.
Richard Purdie2b97eab2006-10-06 18:32:18 +020018 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Liam Girdwood612a3fe2012-02-06 16:05:29 +000021 * o Delayed power down of audio subsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020024 */
25
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080029#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020030#include <linux/delay.h>
31#include <linux/pm.h>
32#include <linux/bitops.h>
33#include <linux/platform_device.h>
34#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020035#include <linux/debugfs.h>
Mark Brownf1aac482011-12-05 15:17:06 +000036#include <linux/pm_runtime.h>
Mark Brown62ea8742012-01-21 21:14:48 +000037#include <linux/regulator/consumer.h>
Ola Liljad7e7eb92012-05-24 15:26:25 +020038#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020040#include <sound/core.h>
41#include <sound/pcm.h>
42#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020043#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020044#include <sound/initval.h>
45
Mark Brown84e90932010-11-04 00:07:02 -040046#include <trace/events/asoc.h>
47
Mark Brownde02d072011-09-20 21:43:24 +010048#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
49
Lars-Peter Clausen57295072013-08-05 11:27:31 +020050static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
51 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
52 const char *control,
53 int (*connected)(struct snd_soc_dapm_widget *source,
54 struct snd_soc_dapm_widget *sink));
55static struct snd_soc_dapm_widget *
56snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
57 const struct snd_soc_dapm_widget *widget);
58
Richard Purdie2b97eab2006-10-06 18:32:18 +020059/* dapm power sequences - make this per codec in the future */
60static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010061 [snd_soc_dapm_pre] = 0,
Mark Brown62ea8742012-01-21 21:14:48 +000062 [snd_soc_dapm_regulator_supply] = 1,
Ola Liljad7e7eb92012-05-24 15:26:25 +020063 [snd_soc_dapm_clock_supply] = 1,
Mark Brown1dd275b2013-10-09 13:56:37 +010064 [snd_soc_dapm_supply] = 2,
65 [snd_soc_dapm_micbias] = 3,
Mark Brownc74184e2012-04-04 22:12:09 +010066 [snd_soc_dapm_dai_link] = 2,
Mark Brown1dd275b2013-10-09 13:56:37 +010067 [snd_soc_dapm_dai_in] = 4,
68 [snd_soc_dapm_dai_out] = 4,
69 [snd_soc_dapm_aif_in] = 4,
70 [snd_soc_dapm_aif_out] = 4,
71 [snd_soc_dapm_mic] = 5,
72 [snd_soc_dapm_mux] = 6,
Mark Brown1dd275b2013-10-09 13:56:37 +010073 [snd_soc_dapm_dac] = 7,
74 [snd_soc_dapm_switch] = 8,
75 [snd_soc_dapm_mixer] = 8,
76 [snd_soc_dapm_mixer_named_ctl] = 8,
77 [snd_soc_dapm_pga] = 9,
78 [snd_soc_dapm_adc] = 10,
79 [snd_soc_dapm_out_drv] = 11,
80 [snd_soc_dapm_hp] = 11,
81 [snd_soc_dapm_spk] = 11,
82 [snd_soc_dapm_line] = 11,
83 [snd_soc_dapm_kcontrol] = 12,
84 [snd_soc_dapm_post] = 13,
Richard Purdie2b97eab2006-10-06 18:32:18 +020085};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000086
Richard Purdie2b97eab2006-10-06 18:32:18 +020087static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010088 [snd_soc_dapm_pre] = 0,
Lars-Peter Clausen57295072013-08-05 11:27:31 +020089 [snd_soc_dapm_kcontrol] = 1,
90 [snd_soc_dapm_adc] = 2,
91 [snd_soc_dapm_hp] = 3,
92 [snd_soc_dapm_spk] = 3,
93 [snd_soc_dapm_line] = 3,
94 [snd_soc_dapm_out_drv] = 3,
Mark Brown38357ab2009-06-06 19:03:23 +010095 [snd_soc_dapm_pga] = 4,
Lars-Peter Clausenefc77e32013-06-14 13:16:50 +020096 [snd_soc_dapm_switch] = 5,
Mark Brown38357ab2009-06-06 19:03:23 +010097 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010098 [snd_soc_dapm_mixer] = 5,
99 [snd_soc_dapm_dac] = 6,
100 [snd_soc_dapm_mic] = 7,
101 [snd_soc_dapm_micbias] = 8,
102 [snd_soc_dapm_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +0100103 [snd_soc_dapm_aif_in] = 10,
104 [snd_soc_dapm_aif_out] = 10,
Mark Brown46162742013-06-05 19:36:11 +0100105 [snd_soc_dapm_dai_in] = 10,
106 [snd_soc_dapm_dai_out] = 10,
Mark Brownc74184e2012-04-04 22:12:09 +0100107 [snd_soc_dapm_dai_link] = 11,
Mark Brownc74184e2012-04-04 22:12:09 +0100108 [snd_soc_dapm_supply] = 12,
Mark Brown1dd275b2013-10-09 13:56:37 +0100109 [snd_soc_dapm_clock_supply] = 13,
110 [snd_soc_dapm_regulator_supply] = 13,
111 [snd_soc_dapm_post] = 14,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200112};
113
Mark Brownf9fa2b12014-03-06 16:49:11 +0800114static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
115{
116 if (dapm->card && dapm->card->instantiated)
117 lockdep_assert_held(&dapm->card->dapm_mutex);
118}
119
Troy Kisky12ef1932008-10-13 17:42:14 -0700120static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +0100121{
122 if (pop_time)
123 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
124}
125
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200126static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100127{
128 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200129 char *buf;
130
131 if (!pop_time)
132 return;
133
134 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
135 if (buf == NULL)
136 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100137
138 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200139 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100140 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100141 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200142
143 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100144}
145
Mark Browndb432b42011-10-03 21:06:40 +0100146static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
147{
148 return !list_empty(&w->dirty);
149}
150
Mark Brown492c0a12014-03-06 16:15:48 +0800151static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100152{
Mark Brownf9fa2b12014-03-06 16:49:11 +0800153 dapm_assert_locked(w->dapm);
154
Mark Brown75c1f892011-10-04 22:28:08 +0100155 if (!dapm_dirty_widget(w)) {
156 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
157 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100158 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100159 }
Mark Browndb432b42011-10-03 21:06:40 +0100160}
161
Mark Browne2d32ff2012-08-31 17:38:32 -0700162void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
163{
164 struct snd_soc_card *card = dapm->card;
165 struct snd_soc_dapm_widget *w;
166
167 mutex_lock(&card->dapm_mutex);
168
169 list_for_each_entry(w, &card->widgets, list) {
170 switch (w->id) {
171 case snd_soc_dapm_input:
172 case snd_soc_dapm_output:
173 dapm_mark_dirty(w, "Rechecking inputs and outputs");
174 break;
175 default:
176 break;
177 }
178 }
179
180 mutex_unlock(&card->dapm_mutex);
181}
182EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
183
Richard Purdie2b97eab2006-10-06 18:32:18 +0200184/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100185static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200186 const struct snd_soc_dapm_widget *_widget)
187{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100188 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200189}
190
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200191struct dapm_kcontrol_data {
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200192 unsigned int value;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200193 struct snd_soc_dapm_widget *widget;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200194 struct list_head paths;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200195 struct snd_soc_dapm_widget_list *wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200196};
197
198static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
199 struct snd_kcontrol *kcontrol)
200{
201 struct dapm_kcontrol_data *data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200202 struct soc_mixer_control *mc;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200203
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200204 data = kzalloc(sizeof(*data), GFP_KERNEL);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200205 if (!data) {
206 dev_err(widget->dapm->dev,
207 "ASoC: can't allocate kcontrol data for %s\n",
208 widget->name);
209 return -ENOMEM;
210 }
211
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200212 INIT_LIST_HEAD(&data->paths);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200213
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200214 switch (widget->id) {
215 case snd_soc_dapm_switch:
216 case snd_soc_dapm_mixer:
217 case snd_soc_dapm_mixer_named_ctl:
218 mc = (struct soc_mixer_control *)kcontrol->private_value;
219
220 if (mc->autodisable) {
221 struct snd_soc_dapm_widget template;
222
223 memset(&template, 0, sizeof(template));
224 template.reg = mc->reg;
225 template.mask = (1 << fls(mc->max)) - 1;
226 template.shift = mc->shift;
227 if (mc->invert)
228 template.off_val = mc->max;
229 else
230 template.off_val = 0;
231 template.on_val = template.off_val;
232 template.id = snd_soc_dapm_kcontrol;
233 template.name = kcontrol->id.name;
234
Lars-Peter Clausen2daabd72013-08-30 17:39:33 +0200235 data->value = template.on_val;
236
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200237 data->widget = snd_soc_dapm_new_control(widget->dapm,
238 &template);
239 if (!data->widget) {
240 kfree(data);
241 return -ENOMEM;
242 }
243 }
244 break;
245 default:
246 break;
247 }
248
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200249 kcontrol->private_data = data;
250
251 return 0;
252}
253
254static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
255{
256 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200257 kfree(data->wlist);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200258 kfree(data);
259}
260
261static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
262 const struct snd_kcontrol *kcontrol)
263{
264 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
265
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200266 return data->wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200267}
268
269static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
270 struct snd_soc_dapm_widget *widget)
271{
272 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200273 struct snd_soc_dapm_widget_list *new_wlist;
274 unsigned int n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200275
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200276 if (data->wlist)
277 n = data->wlist->num_widgets + 1;
278 else
279 n = 1;
280
281 new_wlist = krealloc(data->wlist,
282 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
283 if (!new_wlist)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200284 return -ENOMEM;
285
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200286 new_wlist->widgets[n - 1] = widget;
287 new_wlist->num_widgets = n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200288
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200289 data->wlist = new_wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200290
291 return 0;
292}
293
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200294static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
295 struct snd_soc_dapm_path *path)
296{
297 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
298
299 list_add_tail(&path->list_kcontrol, &data->paths);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200300
301 if (data->widget) {
302 snd_soc_dapm_add_path(data->widget->dapm, data->widget,
303 path->source, NULL, NULL);
304 }
305}
306
307static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
308{
309 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
310
311 if (!data->widget)
312 return true;
313
314 return data->widget->power;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200315}
316
317static struct list_head *dapm_kcontrol_get_path_list(
318 const struct snd_kcontrol *kcontrol)
319{
320 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
321
322 return &data->paths;
323}
324
325#define dapm_kcontrol_for_each_path(path, kcontrol) \
326 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
327 list_kcontrol)
328
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200329static unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
330{
331 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
332
333 return data->value;
334}
335
336static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
337 unsigned int value)
338{
339 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
340
341 if (data->value == value)
342 return false;
343
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200344 if (data->widget)
345 data->widget->on_val = value;
346
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200347 data->value = value;
348
349 return true;
350}
351
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200352/**
353 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
354 * @kcontrol: The kcontrol
355 */
356struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
357{
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200358 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->codec;
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200359}
360EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
361
Liam Girdwood6c120e12012-02-15 15:15:34 +0000362static void dapm_reset(struct snd_soc_card *card)
363{
364 struct snd_soc_dapm_widget *w;
365
Mark Brownf9fa2b12014-03-06 16:49:11 +0800366 lockdep_assert_held(&card->dapm_mutex);
367
Liam Girdwood6c120e12012-02-15 15:15:34 +0000368 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
369
370 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200371 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000372 w->power_checked = false;
373 w->inputs = -1;
374 w->outputs = -1;
375 }
376}
377
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200378static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
379{
380 if (!dapm->component)
381 return NULL;
382 return dapm->component->name_prefix;
383}
384
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800385static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg,
386 unsigned int *value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100387{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200388 if (!w->dapm->component)
389 return -EIO;
390 return snd_soc_component_read(w->dapm->component, reg, value);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100391}
392
Lars-Peter Clausen23d54422014-04-22 13:23:16 +0200393static int soc_widget_update_bits(struct snd_soc_dapm_widget *w,
Bard Liao34775012014-04-17 20:12:56 +0800394 int reg, unsigned int mask, unsigned int value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100395{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200396 if (!w->dapm->component)
397 return -EIO;
398 return snd_soc_component_update_bits_async(w->dapm->component, reg,
399 mask, value);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000400}
401
Mark Browneb270e92013-10-09 13:52:52 +0100402static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
403{
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200404 if (dapm->component)
405 snd_soc_component_async_complete(dapm->component);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100406}
407
Mark Brown452c5ea2009-05-17 21:41:23 +0100408/**
409 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800410 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100411 * @level: level to configure
412 *
413 * Configure the bias (power) levels for the SoC audio device.
414 *
415 * Returns 0 for success else error.
416 */
Mark Browned5a4c42011-02-18 11:12:42 -0800417static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200418 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100419{
Mark Browned5a4c42011-02-18 11:12:42 -0800420 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100421 int ret = 0;
422
Mark Brown84e90932010-11-04 00:07:02 -0400423 trace_snd_soc_bias_level_start(card, level);
424
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000425 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100426 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100427 if (ret != 0)
428 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100429
Mark Browncc4c6702011-06-06 19:03:34 +0100430 if (dapm->codec) {
431 if (dapm->codec->driver->set_bias_level)
432 ret = dapm->codec->driver->set_bias_level(dapm->codec,
433 level);
Mark Brownd8c3bb92012-08-23 18:10:42 +0100434 else
435 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100436 } else if (!card || dapm != &card->dapm) {
Liam Girdwood41231282012-07-06 16:56:16 +0100437 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100438 }
Liam Girdwood41231282012-07-06 16:56:16 +0100439
Mark Brown171ec6b2011-06-06 18:15:19 +0100440 if (ret != 0)
441 goto out;
442
443 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100444 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100445out:
Mark Brown84e90932010-11-04 00:07:02 -0400446 trace_snd_soc_bias_level_done(card, level);
447
Mark Brown452c5ea2009-05-17 21:41:23 +0100448 return ret;
449}
450
Mark Brown74b8f952009-06-06 11:26:15 +0100451/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200452static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200453 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
454 struct snd_soc_dapm_path *path, const char *control_name,
455 const struct snd_kcontrol_new *kcontrol)
456{
457 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100458 unsigned int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200459 int i;
460
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100461 if (e->reg != SND_SOC_NOPM) {
462 soc_widget_read(dest, e->reg, &val);
463 val = (val >> e->shift_l) & e->mask;
464 item = snd_soc_enum_val_to_item(e, val);
465 } else {
466 /* since a virtual mux has no backing registers to
467 * decide which path to connect, it will try to match
468 * with the first enumeration. This is to ensure
469 * that the default mux choice (the first) will be
470 * correctly powered up during initialization.
471 */
472 item = 0;
473 }
474
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100475 for (i = 0; i < e->items; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200476 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200477 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200478 list_add(&path->list_sink, &dest->sources);
479 list_add(&path->list_source, &src->sinks);
480 path->name = (char*)e->texts[i];
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100481 if (i == item)
482 path->connect = 1;
483 else
484 path->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200485 return 0;
486 }
487 }
488
489 return -ENODEV;
490}
491
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100492/* set up initial codec paths */
493static void dapm_set_mixer_path_status(struct snd_soc_dapm_widget *w,
494 struct snd_soc_dapm_path *p, int i)
495{
496 struct soc_mixer_control *mc = (struct soc_mixer_control *)
497 w->kcontrol_news[i].private_value;
498 unsigned int reg = mc->reg;
499 unsigned int shift = mc->shift;
500 unsigned int max = mc->max;
501 unsigned int mask = (1 << fls(max)) - 1;
502 unsigned int invert = mc->invert;
503 unsigned int val;
504
505 if (reg != SND_SOC_NOPM) {
506 soc_widget_read(w, reg, &val);
507 val = (val >> shift) & mask;
508 if (invert)
509 val = max - val;
510 p->connect = !!val;
511 } else {
512 p->connect = 0;
513 }
514}
515
Mark Brown74b8f952009-06-06 11:26:15 +0100516/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200517static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200518 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
519 struct snd_soc_dapm_path *path, const char *control_name)
520{
521 int i;
522
523 /* search for mixer kcontrol */
524 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600525 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200526 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200527 list_add(&path->list_sink, &dest->sources);
528 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600529 path->name = dest->kcontrol_news[i].name;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100530 dapm_set_mixer_path_status(dest, path, i);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200531 return 0;
532 }
533 }
534 return -ENODEV;
535}
536
Stephen Warrenaf468002011-04-28 17:38:01 -0600537static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600538 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600539 const struct snd_kcontrol_new *kcontrol_new,
540 struct snd_kcontrol **kcontrol)
541{
542 struct snd_soc_dapm_widget *w;
543 int i;
544
545 *kcontrol = NULL;
546
547 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600548 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
549 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600550 for (i = 0; i < w->num_kcontrols; i++) {
551 if (&w->kcontrol_news[i] == kcontrol_new) {
552 if (w->kcontrols)
553 *kcontrol = w->kcontrols[i];
554 return 1;
555 }
556 }
557 }
558
559 return 0;
560}
561
Stephen Warren85762e72013-03-29 15:40:10 -0600562/*
563 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
564 * create it. Either way, add the widget into the control's widget list
565 */
566static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100567 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200568{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200569 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000570 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000571 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600572 size_t prefix_len;
573 int shared;
574 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600575 bool wname_in_long_name, kcname_in_long_name;
Stephen Warren85762e72013-03-29 15:40:10 -0600576 char *long_name;
577 const char *name;
578 int ret;
Mark Brownefb7ac32011-03-08 17:23:24 +0000579
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +0200580 prefix = soc_dapm_prefix(dapm);
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000581 if (prefix)
582 prefix_len = strlen(prefix) + 1;
583 else
584 prefix_len = 0;
585
Stephen Warren85762e72013-03-29 15:40:10 -0600586 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
587 &kcontrol);
588
Stephen Warren85762e72013-03-29 15:40:10 -0600589 if (!kcontrol) {
590 if (shared) {
591 wname_in_long_name = false;
592 kcname_in_long_name = true;
593 } else {
594 switch (w->id) {
595 case snd_soc_dapm_switch:
596 case snd_soc_dapm_mixer:
597 wname_in_long_name = true;
598 kcname_in_long_name = true;
599 break;
600 case snd_soc_dapm_mixer_named_ctl:
601 wname_in_long_name = false;
602 kcname_in_long_name = true;
603 break;
604 case snd_soc_dapm_mux:
Stephen Warren85762e72013-03-29 15:40:10 -0600605 wname_in_long_name = true;
606 kcname_in_long_name = false;
607 break;
608 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600609 return -EINVAL;
610 }
611 }
612
613 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600614 /*
615 * The control will get a prefix from the control
616 * creation process but we're also using the same
617 * prefix for widgets so cut the prefix off the
618 * front of the widget name.
619 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200620 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600621 w->name + prefix_len,
622 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200623 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200624 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600625
626 name = long_name;
627 } else if (wname_in_long_name) {
628 long_name = NULL;
629 name = w->name + prefix_len;
630 } else {
631 long_name = NULL;
632 name = w->kcontrol_news[kci].name;
633 }
634
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200635 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600636 prefix);
Lars-Peter Clausen656ca9d2013-06-14 13:16:54 +0200637 kfree(long_name);
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200638 if (!kcontrol)
639 return -ENOMEM;
640 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200641
642 ret = dapm_kcontrol_data_alloc(w, kcontrol);
643 if (ret) {
644 snd_ctl_free_one(kcontrol);
645 return ret;
646 }
647
Stephen Warren85762e72013-03-29 15:40:10 -0600648 ret = snd_ctl_add(card, kcontrol);
649 if (ret < 0) {
650 dev_err(dapm->dev,
651 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
652 w->name, name, ret);
Stephen Warren85762e72013-03-29 15:40:10 -0600653 return ret;
654 }
Stephen Warren85762e72013-03-29 15:40:10 -0600655 }
656
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200657 ret = dapm_kcontrol_add_widget(kcontrol, w);
658 if (ret)
659 return ret;
660
Stephen Warren85762e72013-03-29 15:40:10 -0600661 w->kcontrols[kci] = kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600662
663 return 0;
664}
665
666/* create new dapm mixer control */
667static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
668{
669 int i, ret;
670 struct snd_soc_dapm_path *path;
671
Richard Purdie2b97eab2006-10-06 18:32:18 +0200672 /* add kcontrol */
673 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200674 /* match name */
675 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200676 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600677 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200678 continue;
679
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200680 if (w->kcontrols[i]) {
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200681 dapm_kcontrol_add_path(w->kcontrols[i], path);
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200682 continue;
683 }
684
Mark Brown946d92a2013-08-12 23:28:42 +0100685 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
Stephen Warren85762e72013-03-29 15:40:10 -0600686 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200687 return ret;
Mark Brown946d92a2013-08-12 23:28:42 +0100688
689 dapm_kcontrol_add_path(w->kcontrols[i], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200690 }
691 }
Stephen Warren85762e72013-03-29 15:40:10 -0600692
693 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200694}
695
696/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200697static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200698{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200699 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600700 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600701 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200702
Stephen Warrenaf468002011-04-28 17:38:01 -0600703 if (w->num_kcontrols != 1) {
704 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000705 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600706 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200707 return -EINVAL;
708 }
709
Lars-Peter Clausenfe5813912013-08-01 18:30:38 +0200710 if (list_empty(&w->sources)) {
Stephen Warren85762e72013-03-29 15:40:10 -0600711 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
712 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600713 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200714
Mark Brown946d92a2013-08-12 23:28:42 +0100715 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600716 if (ret < 0)
717 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600718
Richard Purdie2b97eab2006-10-06 18:32:18 +0200719 list_for_each_entry(path, &w->sources, list_sink)
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200720 dapm_kcontrol_add_path(w->kcontrols[0], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200721
Stephen Warrenaf468002011-04-28 17:38:01 -0600722 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200723}
724
725/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200726static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200727{
Mark Browna6c65732010-03-03 17:45:21 +0000728 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200729 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000730 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200731
Mark Browna6c65732010-03-03 17:45:21 +0000732 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200733}
734
735/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100736static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
737 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200738{
739 struct snd_soc_dapm_path *p;
740
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100741 list_for_each_entry(p, sink, list_source) {
742 if (p->walked) {
743 p->walked = 0;
744 dapm_clear_walk_output(dapm, &p->sink->sinks);
745 }
746 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200747}
748
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100749static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
750 struct list_head *source)
751{
752 struct snd_soc_dapm_path *p;
753
754 list_for_each_entry(p, source, list_sink) {
755 if (p->walked) {
756 p->walked = 0;
757 dapm_clear_walk_input(dapm, &p->source->sources);
758 }
759 }
760}
761
762
Mark Brown99497882010-05-07 20:24:05 +0100763/* We implement power down on suspend by checking the power state of
764 * the ALSA card - when we are suspending the ALSA state for the card
765 * is set to D3.
766 */
767static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
768{
Mark Brown12ea2c72011-03-02 18:17:32 +0000769 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100770
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000771 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100772 case SNDRV_CTL_POWER_D3hot:
773 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100774 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000775 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200776 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100777 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100778 default:
779 return 1;
780 }
781}
782
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100783/* add widget to list if it's not already in the list */
784static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
785 struct snd_soc_dapm_widget *w)
786{
787 struct snd_soc_dapm_widget_list *wlist;
788 int wlistsize, wlistentries, i;
789
790 if (*list == NULL)
791 return -EINVAL;
792
793 wlist = *list;
794
795 /* is this widget already in the list */
796 for (i = 0; i < wlist->num_widgets; i++) {
797 if (wlist->widgets[i] == w)
798 return 0;
799 }
800
801 /* allocate some new space */
802 wlistentries = wlist->num_widgets + 1;
803 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
804 wlistentries * sizeof(struct snd_soc_dapm_widget *);
805 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
806 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000807 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100808 w->name);
809 return -ENOMEM;
810 }
811 wlist = *list;
812
813 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000814 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100815 w->name, wlist->num_widgets);
816
817 wlist->widgets[wlist->num_widgets] = w;
818 wlist->num_widgets++;
819 return 1;
820}
821
Richard Purdie2b97eab2006-10-06 18:32:18 +0200822/*
823 * Recursively check for a completed path to an active or physically connected
824 * output widget. Returns number of complete paths.
825 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100826static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
827 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200828{
829 struct snd_soc_dapm_path *path;
830 int con = 0;
831
Mark Brown024dc072011-10-09 11:52:05 +0100832 if (widget->outputs >= 0)
833 return widget->outputs;
834
Mark Brownde02d072011-09-20 21:43:24 +0100835 DAPM_UPDATE_STAT(widget, path_checks);
836
Mark Brown62ea8742012-01-21 21:14:48 +0000837 switch (widget->id) {
838 case snd_soc_dapm_supply:
839 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200840 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200841 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100842 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000843 default:
844 break;
845 }
Mark Brown246d0a12009-04-22 18:24:55 +0100846
Mark Brown010ff262009-08-17 17:39:22 +0100847 switch (widget->id) {
848 case snd_soc_dapm_adc:
849 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100850 case snd_soc_dapm_dai_out:
Mark Brown024dc072011-10-09 11:52:05 +0100851 if (widget->active) {
852 widget->outputs = snd_soc_dapm_suspend_check(widget);
853 return widget->outputs;
854 }
Mark Brown010ff262009-08-17 17:39:22 +0100855 default:
856 break;
857 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200858
859 if (widget->connected) {
860 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100861 if (widget->id == snd_soc_dapm_output && !widget->ext) {
862 widget->outputs = snd_soc_dapm_suspend_check(widget);
863 return widget->outputs;
864 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200865
866 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100867 if (widget->id == snd_soc_dapm_hp ||
868 widget->id == snd_soc_dapm_spk ||
869 (widget->id == snd_soc_dapm_line &&
870 !list_empty(&widget->sources))) {
871 widget->outputs = snd_soc_dapm_suspend_check(widget);
872 return widget->outputs;
873 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200874 }
875
876 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e02011-09-21 18:19:14 +0100877 DAPM_UPDATE_STAT(widget, neighbour_checks);
878
Mark Brownbf3a9e12011-06-13 16:42:29 +0100879 if (path->weak)
880 continue;
881
Mark Brown8af294b2013-02-22 17:48:15 +0000882 if (path->walking)
883 return 1;
884
Richard Purdie2b97eab2006-10-06 18:32:18 +0200885 if (path->walked)
886 continue;
887
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100888 trace_snd_soc_dapm_output_path(widget, path);
889
Richard Purdie2b97eab2006-10-06 18:32:18 +0200890 if (path->sink && path->connect) {
891 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000892 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100893
894 /* do we need to add this widget to the list ? */
895 if (list) {
896 int err;
897 err = dapm_list_add_widget(list, path->sink);
898 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000899 dev_err(widget->dapm->dev,
900 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100901 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000902 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100903 return con;
904 }
905 }
906
907 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000908
909 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200910 }
911 }
912
Mark Brown024dc072011-10-09 11:52:05 +0100913 widget->outputs = con;
914
Richard Purdie2b97eab2006-10-06 18:32:18 +0200915 return con;
916}
917
918/*
919 * Recursively check for a completed path to an active or physically connected
920 * input widget. Returns number of complete paths.
921 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100922static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
923 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200924{
925 struct snd_soc_dapm_path *path;
926 int con = 0;
927
Mark Brown024dc072011-10-09 11:52:05 +0100928 if (widget->inputs >= 0)
929 return widget->inputs;
930
Mark Brownde02d072011-09-20 21:43:24 +0100931 DAPM_UPDATE_STAT(widget, path_checks);
932
Mark Brown62ea8742012-01-21 21:14:48 +0000933 switch (widget->id) {
934 case snd_soc_dapm_supply:
935 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200936 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200937 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100938 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000939 default:
940 break;
941 }
Mark Brown246d0a12009-04-22 18:24:55 +0100942
Richard Purdie2b97eab2006-10-06 18:32:18 +0200943 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100944 switch (widget->id) {
945 case snd_soc_dapm_dac:
946 case snd_soc_dapm_aif_in:
Mark Brown46162742013-06-05 19:36:11 +0100947 case snd_soc_dapm_dai_in:
Mark Brown024dc072011-10-09 11:52:05 +0100948 if (widget->active) {
949 widget->inputs = snd_soc_dapm_suspend_check(widget);
950 return widget->inputs;
951 }
Mark Brown010ff262009-08-17 17:39:22 +0100952 default:
953 break;
954 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200955
956 if (widget->connected) {
957 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100958 if (widget->id == snd_soc_dapm_input && !widget->ext) {
959 widget->inputs = snd_soc_dapm_suspend_check(widget);
960 return widget->inputs;
961 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200962
963 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100964 if (widget->id == snd_soc_dapm_vmid) {
965 widget->inputs = snd_soc_dapm_suspend_check(widget);
966 return widget->inputs;
967 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200968
969 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300970 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100971 (widget->id == snd_soc_dapm_line &&
972 !list_empty(&widget->sinks))) {
973 widget->inputs = snd_soc_dapm_suspend_check(widget);
974 return widget->inputs;
975 }
976
Mark Brown1ab97c82011-11-27 16:21:51 +0000977 /* signal generator */
978 if (widget->id == snd_soc_dapm_siggen) {
979 widget->inputs = snd_soc_dapm_suspend_check(widget);
980 return widget->inputs;
981 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200982 }
983
984 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e02011-09-21 18:19:14 +0100985 DAPM_UPDATE_STAT(widget, neighbour_checks);
986
Mark Brownbf3a9e12011-06-13 16:42:29 +0100987 if (path->weak)
988 continue;
989
Mark Brown8af294b2013-02-22 17:48:15 +0000990 if (path->walking)
991 return 1;
992
Richard Purdie2b97eab2006-10-06 18:32:18 +0200993 if (path->walked)
994 continue;
995
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100996 trace_snd_soc_dapm_input_path(widget, path);
997
Richard Purdie2b97eab2006-10-06 18:32:18 +0200998 if (path->source && path->connect) {
999 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +00001000 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001001
1002 /* do we need to add this widget to the list ? */
1003 if (list) {
1004 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +01001005 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001006 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001007 dev_err(widget->dapm->dev,
1008 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001009 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001010 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001011 return con;
1012 }
1013 }
1014
1015 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001016
1017 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001018 }
1019 }
1020
Mark Brown024dc072011-10-09 11:52:05 +01001021 widget->inputs = con;
1022
Richard Purdie2b97eab2006-10-06 18:32:18 +02001023 return con;
1024}
1025
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001026/**
1027 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1028 * @dai: the soc DAI.
1029 * @stream: stream direction.
1030 * @list: list of active widgets for this stream.
1031 *
1032 * Queries DAPM graph as to whether an valid audio stream path exists for
1033 * the initial stream specified by name. This takes into account
1034 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1035 *
1036 * Returns the number of valid paths or negative error.
1037 */
1038int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1039 struct snd_soc_dapm_widget_list **list)
1040{
1041 struct snd_soc_card *card = dai->card;
1042 int paths;
1043
1044 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1045 dapm_reset(card);
1046
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001047 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001048 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001049 dapm_clear_walk_output(&card->dapm,
1050 &dai->playback_widget->sinks);
1051 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001052 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001053 dapm_clear_walk_input(&card->dapm,
1054 &dai->capture_widget->sources);
1055 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001056
1057 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001058 mutex_unlock(&card->dapm_mutex);
1059
1060 return paths;
1061}
1062
Richard Purdie2b97eab2006-10-06 18:32:18 +02001063/*
Mark Brown62ea8742012-01-21 21:14:48 +00001064 * Handler for regulator supply widget.
1065 */
1066int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1067 struct snd_kcontrol *kcontrol, int event)
1068{
Mark Brownc05b84d2012-09-07 12:57:11 +08001069 int ret;
1070
Mark Browneb270e92013-10-09 13:52:52 +01001071 soc_dapm_async_complete(w->dapm);
1072
Mark Brownc05b84d2012-09-07 12:57:11 +08001073 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001074 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001075 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001076 if (ret != 0)
1077 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001078 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001079 w->name, ret);
1080 }
1081
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001082 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001083 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001084 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001085 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001086 if (ret != 0)
1087 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001088 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001089 w->name, ret);
1090 }
1091
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001092 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001093 }
Mark Brown62ea8742012-01-21 21:14:48 +00001094}
1095EXPORT_SYMBOL_GPL(dapm_regulator_event);
1096
Ola Liljad7e7eb92012-05-24 15:26:25 +02001097/*
1098 * Handler for clock supply widget.
1099 */
1100int dapm_clock_event(struct snd_soc_dapm_widget *w,
1101 struct snd_kcontrol *kcontrol, int event)
1102{
1103 if (!w->clk)
1104 return -EIO;
1105
Mark Browneb270e92013-10-09 13:52:52 +01001106 soc_dapm_async_complete(w->dapm);
1107
Mark Brownec029952012-06-04 08:16:20 +01001108#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001109 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001110 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001111 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001112 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001113 return 0;
1114 }
Mark Brownec029952012-06-04 08:16:20 +01001115#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001116 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001117}
1118EXPORT_SYMBOL_GPL(dapm_clock_event);
1119
Mark Brownd8050022011-09-28 18:28:23 +01001120static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1121{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001122 if (w->power_checked)
1123 return w->new_power;
1124
Mark Brownd8050022011-09-28 18:28:23 +01001125 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001126 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001127 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001128 w->new_power = w->power_check(w);
1129
1130 w->power_checked = true;
1131
1132 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001133}
1134
Mark Browncd0f2d42009-04-20 16:56:59 +01001135/* Generic check to see if a widget should be powered.
1136 */
1137static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1138{
1139 int in, out;
1140
Mark Brownde02d072011-09-20 21:43:24 +01001141 DAPM_UPDATE_STAT(w, power_checks);
1142
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001143 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001144 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001145 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001146 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001147 return out != 0 && in != 0;
1148}
1149
Mark Brown6ea31b92009-04-20 17:15:41 +01001150/* Check to see if an ADC has power */
1151static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1152{
1153 int in;
1154
Mark Brownde02d072011-09-20 21:43:24 +01001155 DAPM_UPDATE_STAT(w, power_checks);
1156
Mark Brown6ea31b92009-04-20 17:15:41 +01001157 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001158 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001159 dapm_clear_walk_input(w->dapm, &w->sources);
Mark Brown6ea31b92009-04-20 17:15:41 +01001160 return in != 0;
1161 } else {
1162 return dapm_generic_check_power(w);
1163 }
1164}
1165
1166/* Check to see if a DAC has power */
1167static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1168{
1169 int out;
1170
Mark Brownde02d072011-09-20 21:43:24 +01001171 DAPM_UPDATE_STAT(w, power_checks);
1172
Mark Brown6ea31b92009-04-20 17:15:41 +01001173 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001174 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001175 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001176 return out != 0;
1177 } else {
1178 return dapm_generic_check_power(w);
1179 }
1180}
1181
Mark Brown246d0a12009-04-22 18:24:55 +01001182/* Check to see if a power supply is needed */
1183static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1184{
1185 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001186
Mark Brownde02d072011-09-20 21:43:24 +01001187 DAPM_UPDATE_STAT(w, power_checks);
1188
Mark Brown246d0a12009-04-22 18:24:55 +01001189 /* Check if one of our outputs is connected */
1190 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001191 DAPM_UPDATE_STAT(w, neighbour_checks);
1192
Mark Brownbf3a9e12011-06-13 16:42:29 +01001193 if (path->weak)
1194 continue;
1195
Mark Brown215edda2009-09-08 18:59:05 +01001196 if (path->connected &&
1197 !path->connected(path->source, path->sink))
1198 continue;
1199
Mark Brown30173582011-02-11 11:42:19 +00001200 if (!path->sink)
1201 continue;
1202
Mark Brownf68d7e12011-10-04 22:57:50 +01001203 if (dapm_widget_power_check(path->sink))
1204 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001205 }
1206
Mark Brownf68d7e12011-10-04 22:57:50 +01001207 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001208}
1209
Mark Brown35c64bc2011-09-28 18:23:53 +01001210static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1211{
1212 return 1;
1213}
1214
Mark Brown38357ab2009-06-06 19:03:23 +01001215static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1216 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001217 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001218{
Mark Brown828a8422011-01-15 13:14:30 +00001219 int *sort;
1220
1221 if (power_up)
1222 sort = dapm_up_seq;
1223 else
1224 sort = dapm_down_seq;
1225
Mark Brown38357ab2009-06-06 19:03:23 +01001226 if (sort[a->id] != sort[b->id])
1227 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001228 if (a->subseq != b->subseq) {
1229 if (power_up)
1230 return a->subseq - b->subseq;
1231 else
1232 return b->subseq - a->subseq;
1233 }
Mark Brownb22ead22009-06-07 12:51:26 +01001234 if (a->reg != b->reg)
1235 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001236 if (a->dapm != b->dapm)
1237 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001238
Mark Brown38357ab2009-06-06 19:03:23 +01001239 return 0;
1240}
Mark Brown42aa3412009-03-01 19:21:10 +00001241
Mark Brown38357ab2009-06-06 19:03:23 +01001242/* Insert a widget in order into a DAPM power sequence. */
1243static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1244 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001245 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001246{
1247 struct snd_soc_dapm_widget *w;
1248
1249 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001250 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001251 list_add_tail(&new_widget->power_list, &w->power_list);
1252 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001253 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001254
Mark Brown38357ab2009-06-06 19:03:23 +01001255 list_add_tail(&new_widget->power_list, list);
1256}
Mark Brown42aa3412009-03-01 19:21:10 +00001257
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001258static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001259 struct snd_soc_dapm_widget *w, int event)
1260{
Mark Brown68f89ad2010-11-03 23:51:49 -04001261 const char *ev_name;
1262 int power, ret;
1263
1264 switch (event) {
1265 case SND_SOC_DAPM_PRE_PMU:
1266 ev_name = "PRE_PMU";
1267 power = 1;
1268 break;
1269 case SND_SOC_DAPM_POST_PMU:
1270 ev_name = "POST_PMU";
1271 power = 1;
1272 break;
1273 case SND_SOC_DAPM_PRE_PMD:
1274 ev_name = "PRE_PMD";
1275 power = 0;
1276 break;
1277 case SND_SOC_DAPM_POST_PMD:
1278 ev_name = "POST_PMD";
1279 power = 0;
1280 break;
Mark Brown80114122013-02-25 15:14:19 +00001281 case SND_SOC_DAPM_WILL_PMU:
1282 ev_name = "WILL_PMU";
1283 power = 1;
1284 break;
1285 case SND_SOC_DAPM_WILL_PMD:
1286 ev_name = "WILL_PMD";
1287 power = 0;
1288 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001289 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001290 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001291 return;
1292 }
1293
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001294 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001295 return;
1296
1297 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001298 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001299 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001300 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001301 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001302 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001303 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001304 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001305 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001306 ev_name, w->name, ret);
1307 }
1308}
1309
Mark Brownb22ead22009-06-07 12:51:26 +01001310/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001311static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001312 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001313{
Mark Brown68f89ad2010-11-03 23:51:49 -04001314 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001315 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001316 unsigned int value = 0;
1317 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001318
1319 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1320 power_list)->reg;
1321
1322 list_for_each_entry(w, pending, power_list) {
Takashi Iwaibf4edea2013-11-07 18:38:47 +01001323 WARN_ON(reg != w->reg);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001324 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001325
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001326 mask |= w->mask << w->shift;
1327 if (w->power)
1328 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001329 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001330 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001331
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001332 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001333 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1334 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001335
Mark Brown68f89ad2010-11-03 23:51:49 -04001336 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001337 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1338 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001339 }
1340
Mark Brown81628102009-06-07 13:21:24 +01001341 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001342 /* Any widget will do, they should all be updating the
1343 * same register.
1344 */
1345 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1346 power_list);
1347
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001348 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001349 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001350 value, mask, reg, card->pop_time);
1351 pop_wait(card->pop_time);
Lars-Peter Clausen23d54422014-04-22 13:23:16 +02001352 soc_widget_update_bits(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001353 }
1354
1355 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001356 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1357 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001358 }
Mark Brown42aa3412009-03-01 19:21:10 +00001359}
1360
Mark Brownb22ead22009-06-07 12:51:26 +01001361/* Apply a DAPM power sequence.
1362 *
1363 * We walk over a pre-sorted list of widgets to apply power to. In
1364 * order to minimise the number of writes to the device required
1365 * multiple widgets will be updated in a single write where possible.
1366 * Currently anything that requires more than a single write is not
1367 * handled.
1368 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001369static void dapm_seq_run(struct snd_soc_card *card,
1370 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001371{
1372 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001373 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001374 LIST_HEAD(pending);
1375 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001376 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001377 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001378 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001379 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001380 int *sort;
1381
1382 if (power_up)
1383 sort = dapm_up_seq;
1384 else
1385 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001386
Mark Brownb22ead22009-06-07 12:51:26 +01001387 list_for_each_entry_safe(w, n, list, power_list) {
1388 ret = 0;
1389
1390 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001391 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001392 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001393 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001394 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001395
Mark Brown474b62d2011-01-18 16:14:44 +00001396 if (cur_dapm && cur_dapm->seq_notifier) {
1397 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1398 if (sort[i] == cur_sort)
1399 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001400 i,
1401 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001402 }
1403
Mark Browneb270e92013-10-09 13:52:52 +01001404 if (cur_dapm && w->dapm != cur_dapm)
1405 soc_dapm_async_complete(cur_dapm);
1406
Mark Brownb22ead22009-06-07 12:51:26 +01001407 INIT_LIST_HEAD(&pending);
1408 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001409 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001410 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001411 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001412 }
1413
Mark Brown163cac02009-06-07 10:12:52 +01001414 switch (w->id) {
1415 case snd_soc_dapm_pre:
1416 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001417 list_for_each_entry_safe_continue(w, n, list,
1418 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001419
Mark Brownb22ead22009-06-07 12:51:26 +01001420 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001421 ret = w->event(w,
1422 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001423 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001424 ret = w->event(w,
1425 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001426 break;
1427
1428 case snd_soc_dapm_post:
1429 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001430 list_for_each_entry_safe_continue(w, n, list,
1431 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001432
Mark Brownb22ead22009-06-07 12:51:26 +01001433 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001434 ret = w->event(w,
1435 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001436 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001437 ret = w->event(w,
1438 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001439 break;
1440
Mark Brown163cac02009-06-07 10:12:52 +01001441 default:
Mark Brown81628102009-06-07 13:21:24 +01001442 /* Queue it up for application */
1443 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001444 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001445 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001446 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001447 list_move(&w->power_list, &pending);
1448 break;
Mark Brown163cac02009-06-07 10:12:52 +01001449 }
Mark Brownb22ead22009-06-07 12:51:26 +01001450
1451 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001452 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001453 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001454 }
Mark Brownb22ead22009-06-07 12:51:26 +01001455
1456 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001457 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001458
1459 if (cur_dapm && cur_dapm->seq_notifier) {
1460 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1461 if (sort[i] == cur_sort)
1462 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001463 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001464 }
Mark Browneb270e92013-10-09 13:52:52 +01001465
1466 list_for_each_entry(d, &card->dapm_list, list) {
1467 soc_dapm_async_complete(d);
1468 }
Mark Brown163cac02009-06-07 10:12:52 +01001469}
1470
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001471static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001472{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001473 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001474 struct snd_soc_dapm_widget_list *wlist;
1475 struct snd_soc_dapm_widget *w = NULL;
1476 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001477 int ret;
1478
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001479 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001480 return;
1481
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001482 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001483
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001484 for (wi = 0; wi < wlist->num_widgets; wi++) {
1485 w = wlist->widgets[wi];
1486
1487 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1488 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1489 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001490 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001491 w->name, ret);
1492 }
Mark Brown97404f22010-12-14 16:13:57 +00001493 }
1494
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001495 if (!w)
1496 return;
1497
Lars-Peter Clausen23d54422014-04-22 13:23:16 +02001498 ret = soc_widget_update_bits(w, update->reg, update->mask, update->val);
Mark Brown97404f22010-12-14 16:13:57 +00001499 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001500 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001501 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001502
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001503 for (wi = 0; wi < wlist->num_widgets; wi++) {
1504 w = wlist->widgets[wi];
1505
1506 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1507 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1508 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001509 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001510 w->name, ret);
1511 }
Mark Brown97404f22010-12-14 16:13:57 +00001512 }
1513}
1514
Mark Brown9d0624a2011-02-18 11:49:43 -08001515/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1516 * they're changing state.
1517 */
1518static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1519{
1520 struct snd_soc_dapm_context *d = data;
1521 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001522
Mark Brown56fba412011-06-04 11:25:10 +01001523 /* If we're off and we're not supposed to be go into STANDBY */
1524 if (d->bias_level == SND_SOC_BIAS_OFF &&
1525 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001526 if (d->dev)
1527 pm_runtime_get_sync(d->dev);
1528
Mark Brown9d0624a2011-02-18 11:49:43 -08001529 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1530 if (ret != 0)
1531 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001532 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001533 }
1534
Lars-Peter Clausence85a4d2014-05-06 10:32:15 +02001535 /* Prepare for a transition to ON or away from ON */
1536 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1537 d->bias_level != SND_SOC_BIAS_ON) ||
1538 (d->target_bias_level != SND_SOC_BIAS_ON &&
1539 d->bias_level == SND_SOC_BIAS_ON)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001540 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1541 if (ret != 0)
1542 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001543 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001544 }
1545}
1546
1547/* Async callback run prior to DAPM sequences - brings to their final
1548 * state.
1549 */
1550static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1551{
1552 struct snd_soc_dapm_context *d = data;
1553 int ret;
1554
1555 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001556 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1557 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1558 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001559 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1560 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001561 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001562 ret);
1563 }
1564
1565 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001566 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1567 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001568 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1569 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001570 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1571 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001572
1573 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001574 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001575 }
1576
1577 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001578 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1579 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001580 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1581 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001582 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001583 ret);
1584 }
1585}
Mark Brown97404f22010-12-14 16:13:57 +00001586
Mark Brownfe4fda52011-10-03 22:36:57 +01001587static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1588 bool power, bool connect)
1589{
1590 /* If a connection is being made or broken then that update
1591 * will have marked the peer dirty, otherwise the widgets are
1592 * not connected and this update has no impact. */
1593 if (!connect)
1594 return;
1595
1596 /* If the peer is already in the state we're moving to then we
1597 * won't have an impact on it. */
1598 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001599 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001600}
1601
Mark Brown05623c42011-09-28 17:02:31 +01001602static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1603 struct list_head *up_list,
1604 struct list_head *down_list)
1605{
Mark Browndb432b42011-10-03 21:06:40 +01001606 struct snd_soc_dapm_path *path;
1607
Mark Brown05623c42011-09-28 17:02:31 +01001608 if (w->power == power)
1609 return;
1610
1611 trace_snd_soc_dapm_widget_power(w, power);
1612
Mark Browndb432b42011-10-03 21:06:40 +01001613 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001614 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001615 */
1616 list_for_each_entry(path, &w->sources, list_sink) {
1617 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001618 dapm_widget_set_peer_power(path->source, power,
1619 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001620 }
1621 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001622 switch (w->id) {
1623 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001624 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001625 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001626 case snd_soc_dapm_kcontrol:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001627 /* Supplies can't affect their outputs, only their inputs */
1628 break;
1629 default:
1630 list_for_each_entry(path, &w->sinks, list_source) {
1631 if (path->sink) {
1632 dapm_widget_set_peer_power(path->sink, power,
1633 path->connect);
1634 }
Mark Browndb432b42011-10-03 21:06:40 +01001635 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001636 break;
Mark Browndb432b42011-10-03 21:06:40 +01001637 }
1638
Mark Brown05623c42011-09-28 17:02:31 +01001639 if (power)
1640 dapm_seq_insert(w, up_list, true);
1641 else
1642 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001643}
1644
Mark Brown7c81beb2011-09-20 22:22:32 +01001645static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1646 struct list_head *up_list,
1647 struct list_head *down_list)
1648{
Mark Brown7c81beb2011-09-20 22:22:32 +01001649 int power;
1650
1651 switch (w->id) {
1652 case snd_soc_dapm_pre:
1653 dapm_seq_insert(w, down_list, false);
1654 break;
1655 case snd_soc_dapm_post:
1656 dapm_seq_insert(w, up_list, true);
1657 break;
1658
1659 default:
Mark Brownd8050022011-09-28 18:28:23 +01001660 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001661
Mark Brown05623c42011-09-28 17:02:31 +01001662 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001663 break;
1664 }
1665}
1666
Mark Brown42aa3412009-03-01 19:21:10 +00001667/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001668 * Scan each dapm widget for complete audio path.
1669 * A complete path is a route that has valid endpoints i.e.:-
1670 *
1671 * o DAC to output pin.
1672 * o Input Pin to ADC.
1673 * o Input pin to Output pin (bypass, sidetone)
1674 * o DAC to ADC (loopback).
1675 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001676static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001677{
1678 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001679 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001680 LIST_HEAD(up_list);
1681 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001682 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001683 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001684
Mark Brownf9fa2b12014-03-06 16:49:11 +08001685 lockdep_assert_held(&card->dapm_mutex);
1686
Mark Brown84e90932010-11-04 00:07:02 -04001687 trace_snd_soc_dapm_start(card);
1688
Mark Brown56fba412011-06-04 11:25:10 +01001689 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001690 if (d->idle_bias_off)
1691 d->target_bias_level = SND_SOC_BIAS_OFF;
1692 else
1693 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001694 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001695
Liam Girdwood6c120e12012-02-15 15:15:34 +00001696 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001697
Mark Brown6d3ddc82009-05-16 17:47:29 +01001698 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001699 * lists indicating if they should be powered up or down. We
1700 * only check widgets that have been flagged as dirty but note
1701 * that new widgets may be added to the dirty list while we
1702 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001703 */
Mark Browndb432b42011-10-03 21:06:40 +01001704 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001705 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001706 }
1707
Mark Brownf9de6d72011-09-28 17:19:47 +01001708 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001709 switch (w->id) {
1710 case snd_soc_dapm_pre:
1711 case snd_soc_dapm_post:
1712 /* These widgets always need to be powered */
1713 break;
1714 default:
1715 list_del_init(&w->dirty);
1716 break;
1717 }
Mark Browndb432b42011-10-03 21:06:40 +01001718
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001719 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001720 d = w->dapm;
1721
1722 /* Supplies and micbiases only bring the
1723 * context up to STANDBY as unless something
1724 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001725 * generally don't require full power. Signal
1726 * generators are virtual pins and have no
1727 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001728 */
1729 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001730 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001731 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001732 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001733 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001734 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001735 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001736 case snd_soc_dapm_micbias:
1737 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1738 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1739 break;
1740 default:
1741 d->target_bias_level = SND_SOC_BIAS_ON;
1742 break;
1743 }
1744 }
1745
1746 }
1747
Mark Brown85a843c2011-09-21 21:29:47 +01001748 /* Force all contexts in the card to the same bias state if
1749 * they're not ground referenced.
1750 */
Mark Brown56fba412011-06-04 11:25:10 +01001751 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001752 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001753 if (d->target_bias_level > bias)
1754 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001755 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001756 if (!d->idle_bias_off)
1757 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001758
Mark Brownde02d072011-09-20 21:43:24 +01001759 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001760
Xiang Xiao17282ba2014-03-02 00:04:03 +08001761 /* Run card bias changes at first */
1762 dapm_pre_sequence_async(&card->dapm, 0);
1763 /* Run other bias changes in parallel */
1764 list_for_each_entry(d, &card->dapm_list, list) {
1765 if (d != &card->dapm)
1766 async_schedule_domain(dapm_pre_sequence_async, d,
1767 &async_domain);
1768 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001769 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001770
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001771 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001772 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001773 }
1774
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001775 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001776 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001777 }
1778
Mark Brown6d3ddc82009-05-16 17:47:29 +01001779 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001780 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001781
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001782 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001783
Mark Brown6d3ddc82009-05-16 17:47:29 +01001784 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001785 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001786
Mark Brown9d0624a2011-02-18 11:49:43 -08001787 /* Run all the bias changes in parallel */
Xiang Xiao17282ba2014-03-02 00:04:03 +08001788 list_for_each_entry(d, &card->dapm_list, list) {
1789 if (d != &card->dapm)
1790 async_schedule_domain(dapm_post_sequence_async, d,
1791 &async_domain);
1792 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001793 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08001794 /* Run card bias changes at last */
1795 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01001796
Liam Girdwood8078d872012-02-15 15:15:35 +00001797 /* do we need to notify any clients that DAPM event is complete */
1798 list_for_each_entry(d, &card->dapm_list, list) {
1799 if (d->stream_event)
1800 d->stream_event(d, event);
1801 }
1802
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001803 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001804 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001805 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001806
Mark Brown84e90932010-11-04 00:07:02 -04001807 trace_snd_soc_dapm_done(card);
1808
Mark Brown42aa3412009-03-01 19:21:10 +00001809 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001810}
1811
Mark Brown79fb9382009-08-21 16:38:13 +01001812#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001813static ssize_t dapm_widget_power_read_file(struct file *file,
1814 char __user *user_buf,
1815 size_t count, loff_t *ppos)
1816{
1817 struct snd_soc_dapm_widget *w = file->private_data;
1818 char *buf;
1819 int in, out;
1820 ssize_t ret;
1821 struct snd_soc_dapm_path *p = NULL;
1822
1823 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1824 if (!buf)
1825 return -ENOMEM;
1826
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001827 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001828 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001829 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001830 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001831
Mark Brownf13ebad2012-03-03 18:01:01 +00001832 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1833 w->name, w->power ? "On" : "Off",
1834 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001835
Mark Brownd033c362009-12-04 15:25:56 +00001836 if (w->reg >= 0)
1837 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001838 " - R%d(0x%x) mask 0x%x",
1839 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001840
1841 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1842
Mark Brown3eef08b2009-09-14 16:49:00 +01001843 if (w->sname)
1844 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1845 w->sname,
1846 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001847
1848 list_for_each_entry(p, &w->sources, list_sink) {
Takashi Iwaiff186202013-10-28 14:21:49 +01001849 if (p->connected && !p->connected(w, p->source))
Mark Brown215edda2009-09-08 18:59:05 +01001850 continue;
1851
Mark Brown79fb9382009-08-21 16:38:13 +01001852 if (p->connect)
1853 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001854 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001855 p->name ? p->name : "static",
1856 p->source->name);
1857 }
1858 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001859 if (p->connected && !p->connected(w, p->sink))
1860 continue;
1861
Mark Brown79fb9382009-08-21 16:38:13 +01001862 if (p->connect)
1863 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001864 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001865 p->name ? p->name : "static",
1866 p->sink->name);
1867 }
1868
1869 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1870
1871 kfree(buf);
1872 return ret;
1873}
1874
1875static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001876 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001877 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001878 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001879};
1880
Mark Brownef49e4f2011-04-04 20:48:13 +09001881static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1882 size_t count, loff_t *ppos)
1883{
1884 struct snd_soc_dapm_context *dapm = file->private_data;
1885 char *level;
1886
1887 switch (dapm->bias_level) {
1888 case SND_SOC_BIAS_ON:
1889 level = "On\n";
1890 break;
1891 case SND_SOC_BIAS_PREPARE:
1892 level = "Prepare\n";
1893 break;
1894 case SND_SOC_BIAS_STANDBY:
1895 level = "Standby\n";
1896 break;
1897 case SND_SOC_BIAS_OFF:
1898 level = "Off\n";
1899 break;
1900 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001901 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09001902 level = "Unknown\n";
1903 break;
1904 }
1905
1906 return simple_read_from_buffer(user_buf, count, ppos, level,
1907 strlen(level));
1908}
1909
1910static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001911 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001912 .read = dapm_bias_read_file,
1913 .llseek = default_llseek,
1914};
1915
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001916void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1917 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001918{
Mark Brown79fb9382009-08-21 16:38:13 +01001919 struct dentry *d;
1920
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001921 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1922
1923 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001924 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001925 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001926 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001927 }
Mark Brown79fb9382009-08-21 16:38:13 +01001928
Mark Brownef49e4f2011-04-04 20:48:13 +09001929 d = debugfs_create_file("bias_level", 0444,
1930 dapm->debugfs_dapm, dapm,
1931 &dapm_bias_fops);
1932 if (!d)
1933 dev_warn(dapm->dev,
1934 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001935}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001936
1937static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1938{
1939 struct snd_soc_dapm_context *dapm = w->dapm;
1940 struct dentry *d;
1941
1942 if (!dapm->debugfs_dapm || !w->name)
1943 return;
1944
1945 d = debugfs_create_file(w->name, 0444,
1946 dapm->debugfs_dapm, w,
1947 &dapm_widget_power_fops);
1948 if (!d)
1949 dev_warn(w->dapm->dev,
1950 "ASoC: Failed to create %s debugfs file\n",
1951 w->name);
1952}
1953
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001954static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1955{
1956 debugfs_remove_recursive(dapm->debugfs_dapm);
1957}
1958
Mark Brown79fb9382009-08-21 16:38:13 +01001959#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001960void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1961 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001962{
1963}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001964
1965static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1966{
1967}
1968
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001969static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1970{
1971}
1972
Mark Brown79fb9382009-08-21 16:38:13 +01001973#endif
1974
Richard Purdie2b97eab2006-10-06 18:32:18 +02001975/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001976static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001977 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001978{
1979 struct snd_soc_dapm_path *path;
1980 int found = 0;
1981
Mark Brownf9fa2b12014-03-06 16:49:11 +08001982 lockdep_assert_held(&card->dapm_mutex);
1983
Richard Purdie2b97eab2006-10-06 18:32:18 +02001984 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02001985 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001986 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001987 continue;
1988
1989 found = 1;
1990 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01001991 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001992 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01001993 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01001994 } else {
1995 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01001996 dapm_mark_dirty(path->source,
1997 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001998 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01001999 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002000 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002001 }
2002
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002003 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002004 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002005
Liam Girdwood618dae12012-04-25 12:12:51 +01002006 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002007}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002008
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002009int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002010 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2011 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002012{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002013 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002014 int ret;
2015
Liam Girdwood3cd04342012-03-09 12:02:08 +00002016 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002017 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002018 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002019 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002020 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002021 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002022 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002023 return ret;
2024}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002025EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002026
Milan plzik1b075e32008-01-10 14:39:46 +01002027/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002028static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002029 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002030{
2031 struct snd_soc_dapm_path *path;
2032 int found = 0;
2033
Mark Brownf9fa2b12014-03-06 16:49:11 +08002034 lockdep_assert_held(&card->dapm_mutex);
2035
Richard Purdie2b97eab2006-10-06 18:32:18 +02002036 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002037 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002038 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002039 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002040 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002041 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002042 }
2043
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002044 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002045 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002046
Liam Girdwood618dae12012-04-25 12:12:51 +01002047 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002048}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002049
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002050int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002051 struct snd_kcontrol *kcontrol, int connect,
2052 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002053{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002054 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002055 int ret;
2056
Liam Girdwood3cd04342012-03-09 12:02:08 +00002057 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002058 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002059 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002060 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002061 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002062 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002063 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002064 return ret;
2065}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002066EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002067
2068/* show dapm widget status in sys fs */
2069static ssize_t dapm_widget_show(struct device *dev,
2070 struct device_attribute *attr, char *buf)
2071{
Mark Brown36ae1a92012-01-06 17:12:45 -08002072 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002073 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002074 struct snd_soc_dapm_widget *w;
2075 int count = 0;
2076 char *state = "not set";
2077
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002078 list_for_each_entry(w, &codec->card->widgets, list) {
2079 if (w->dapm != &codec->dapm)
2080 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002081
2082 /* only display widgets that burnm power */
2083 switch (w->id) {
2084 case snd_soc_dapm_hp:
2085 case snd_soc_dapm_mic:
2086 case snd_soc_dapm_spk:
2087 case snd_soc_dapm_line:
2088 case snd_soc_dapm_micbias:
2089 case snd_soc_dapm_dac:
2090 case snd_soc_dapm_adc:
2091 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002092 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002093 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002094 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002095 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002096 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002097 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002098 if (w->name)
2099 count += sprintf(buf + count, "%s: %s\n",
2100 w->name, w->power ? "On":"Off");
2101 break;
2102 default:
2103 break;
2104 }
2105 }
2106
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002107 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002108 case SND_SOC_BIAS_ON:
2109 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002110 break;
Mark Brown0be98982008-05-19 12:31:28 +02002111 case SND_SOC_BIAS_PREPARE:
2112 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002113 break;
Mark Brown0be98982008-05-19 12:31:28 +02002114 case SND_SOC_BIAS_STANDBY:
2115 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002116 break;
Mark Brown0be98982008-05-19 12:31:28 +02002117 case SND_SOC_BIAS_OFF:
2118 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002119 break;
2120 }
2121 count += sprintf(buf + count, "PM State: %s\n", state);
2122
2123 return count;
2124}
2125
2126static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2127
2128int snd_soc_dapm_sys_add(struct device *dev)
2129{
Troy Kisky12ef1932008-10-13 17:42:14 -07002130 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002131}
2132
2133static void snd_soc_dapm_sys_remove(struct device *dev)
2134{
Mark Brownaef90842009-05-16 17:53:16 +01002135 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002136}
2137
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002138static void dapm_free_path(struct snd_soc_dapm_path *path)
2139{
2140 list_del(&path->list_sink);
2141 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002142 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002143 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002144 kfree(path);
2145}
2146
Richard Purdie2b97eab2006-10-06 18:32:18 +02002147/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002148static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002149{
2150 struct snd_soc_dapm_widget *w, *next_w;
2151 struct snd_soc_dapm_path *p, *next_p;
2152
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002153 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2154 if (w->dapm != dapm)
2155 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002156 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002157 /*
2158 * remove source and sink paths associated to this widget.
2159 * While removing the path, remove reference to it from both
2160 * source and sink widgets so that path is removed only once.
2161 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002162 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2163 dapm_free_path(p);
2164
2165 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2166 dapm_free_path(p);
2167
Stephen Warrenfad59882011-04-28 17:37:59 -06002168 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002169 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002170 kfree(w);
2171 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002172}
2173
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002174static struct snd_soc_dapm_widget *dapm_find_widget(
2175 struct snd_soc_dapm_context *dapm, const char *pin,
2176 bool search_other_contexts)
2177{
2178 struct snd_soc_dapm_widget *w;
2179 struct snd_soc_dapm_widget *fallback = NULL;
2180
2181 list_for_each_entry(w, &dapm->card->widgets, list) {
2182 if (!strcmp(w->name, pin)) {
2183 if (w->dapm == dapm)
2184 return w;
2185 else
2186 fallback = w;
2187 }
2188 }
2189
2190 if (search_other_contexts)
2191 return fallback;
2192
2193 return NULL;
2194}
2195
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002196static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002197 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002198{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002199 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002200
Mark Brownf9fa2b12014-03-06 16:49:11 +08002201 dapm_assert_locked(dapm);
2202
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002203 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002204 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002205 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002206 }
2207
Mark Brown1a8b2d92012-02-16 11:50:07 -08002208 if (w->connected != status)
2209 dapm_mark_dirty(w, "pin configuration");
2210
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002211 w->connected = status;
2212 if (status == 0)
2213 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002214
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002215 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002216}
2217
Richard Purdie2b97eab2006-10-06 18:32:18 +02002218/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002219 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2220 * @dapm: DAPM context
2221 *
2222 * Walks all dapm audio paths and powers widgets according to their
2223 * stream or path usage.
2224 *
2225 * Requires external locking.
2226 *
2227 * Returns 0 for success.
2228 */
2229int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2230{
2231 /*
2232 * Suppress early reports (eg, jacks syncing their state) to avoid
2233 * silly DAPM runs during card startup.
2234 */
2235 if (!dapm->card || !dapm->card->instantiated)
2236 return 0;
2237
2238 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2239}
2240EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2241
2242/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002243 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002244 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002245 *
2246 * Walks all dapm audio paths and powers widgets according to their
2247 * stream or path usage.
2248 *
2249 * Returns 0 for success.
2250 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002251int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002252{
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002253 int ret;
2254
Liam Girdwood3cd04342012-03-09 12:02:08 +00002255 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002256 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002257 mutex_unlock(&dapm->card->dapm_mutex);
2258 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002259}
Liam Girdwooda5302182008-07-07 13:35:17 +01002260EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002261
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002262static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2263 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2264 const char *control,
2265 int (*connected)(struct snd_soc_dapm_widget *source,
2266 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002267{
2268 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002269 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002270
2271 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2272 if (!path)
2273 return -ENOMEM;
2274
2275 path->source = wsource;
2276 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002277 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002278 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002279 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002280 INIT_LIST_HEAD(&path->list_source);
2281 INIT_LIST_HEAD(&path->list_sink);
2282
2283 /* check for external widgets */
2284 if (wsink->id == snd_soc_dapm_input) {
2285 if (wsource->id == snd_soc_dapm_micbias ||
2286 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002287 wsource->id == snd_soc_dapm_line ||
2288 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002289 wsink->ext = 1;
2290 }
2291 if (wsource->id == snd_soc_dapm_output) {
2292 if (wsink->id == snd_soc_dapm_spk ||
2293 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002294 wsink->id == snd_soc_dapm_line ||
2295 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002296 wsource->ext = 1;
2297 }
2298
Lars-Peter Clausen34742cb2013-08-27 15:50:54 +02002299 dapm_mark_dirty(wsource, "Route added");
2300 dapm_mark_dirty(wsink, "Route added");
2301
Richard Purdie2b97eab2006-10-06 18:32:18 +02002302 /* connect static paths */
2303 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002304 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002305 list_add(&path->list_sink, &wsink->sources);
2306 list_add(&path->list_source, &wsource->sinks);
2307 path->connect = 1;
2308 return 0;
2309 }
2310
2311 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002312 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002313 case snd_soc_dapm_adc:
2314 case snd_soc_dapm_dac:
2315 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002316 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002317 case snd_soc_dapm_input:
2318 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002319 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002320 case snd_soc_dapm_micbias:
2321 case snd_soc_dapm_vmid:
2322 case snd_soc_dapm_pre:
2323 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002324 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002325 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002326 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002327 case snd_soc_dapm_aif_in:
2328 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002329 case snd_soc_dapm_dai_in:
2330 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002331 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002332 case snd_soc_dapm_kcontrol:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002333 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002334 list_add(&path->list_sink, &wsink->sources);
2335 list_add(&path->list_source, &wsource->sinks);
2336 path->connect = 1;
2337 return 0;
2338 case snd_soc_dapm_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002339 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002340 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002341 if (ret != 0)
2342 goto err;
2343 break;
2344 case snd_soc_dapm_switch:
2345 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002346 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002347 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002348 if (ret != 0)
2349 goto err;
2350 break;
2351 case snd_soc_dapm_hp:
2352 case snd_soc_dapm_mic:
2353 case snd_soc_dapm_line:
2354 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002355 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002356 list_add(&path->list_sink, &wsink->sources);
2357 list_add(&path->list_source, &wsource->sinks);
2358 path->connect = 0;
2359 return 0;
2360 }
Mark Brownfabd0382012-07-05 17:20:06 +01002361
Richard Purdie2b97eab2006-10-06 18:32:18 +02002362 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002363err:
2364 kfree(path);
2365 return ret;
2366}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002367
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002368static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002369 const struct snd_soc_dapm_route *route)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002370{
2371 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2372 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2373 const char *sink;
2374 const char *source;
2375 char prefixed_sink[80];
2376 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002377 const char *prefix;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002378 int ret;
2379
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002380 prefix = soc_dapm_prefix(dapm);
2381 if (prefix) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002382 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002383 prefix, route->sink);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002384 sink = prefixed_sink;
2385 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002386 prefix, route->source);
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002387 source = prefixed_source;
2388 } else {
2389 sink = route->sink;
2390 source = route->source;
2391 }
2392
2393 /*
2394 * find src and dest widgets over all widgets but favor a widget from
2395 * current DAPM context
2396 */
2397 list_for_each_entry(w, &dapm->card->widgets, list) {
2398 if (!wsink && !(strcmp(w->name, sink))) {
2399 wtsink = w;
2400 if (w->dapm == dapm)
2401 wsink = w;
2402 continue;
2403 }
2404 if (!wsource && !(strcmp(w->name, source))) {
2405 wtsource = w;
2406 if (w->dapm == dapm)
2407 wsource = w;
2408 }
2409 }
2410 /* use widget from another DAPM context if not found from this */
2411 if (!wsink)
2412 wsink = wtsink;
2413 if (!wsource)
2414 wsource = wtsource;
2415
2416 if (wsource == NULL) {
2417 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2418 route->source);
2419 return -ENODEV;
2420 }
2421 if (wsink == NULL) {
2422 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2423 route->sink);
2424 return -ENODEV;
2425 }
2426
2427 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2428 route->connected);
2429 if (ret)
2430 goto err;
2431
2432 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002433err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002434 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002435 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002436 return ret;
2437}
Mark Brown105f1c22008-05-13 14:52:19 +02002438
Mark Brownefcc3c62012-07-05 17:24:19 +01002439static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2440 const struct snd_soc_dapm_route *route)
2441{
2442 struct snd_soc_dapm_path *path, *p;
2443 const char *sink;
2444 const char *source;
2445 char prefixed_sink[80];
2446 char prefixed_source[80];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002447 const char *prefix;
Mark Brownefcc3c62012-07-05 17:24:19 +01002448
2449 if (route->control) {
2450 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002451 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002452 return -EINVAL;
2453 }
2454
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002455 prefix = soc_dapm_prefix(dapm);
2456 if (prefix) {
Mark Brownefcc3c62012-07-05 17:24:19 +01002457 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002458 prefix, route->sink);
Mark Brownefcc3c62012-07-05 17:24:19 +01002459 sink = prefixed_sink;
2460 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002461 prefix, route->source);
Mark Brownefcc3c62012-07-05 17:24:19 +01002462 source = prefixed_source;
2463 } else {
2464 sink = route->sink;
2465 source = route->source;
2466 }
2467
2468 path = NULL;
2469 list_for_each_entry(p, &dapm->card->paths, list) {
2470 if (strcmp(p->source->name, source) != 0)
2471 continue;
2472 if (strcmp(p->sink->name, sink) != 0)
2473 continue;
2474 path = p;
2475 break;
2476 }
2477
2478 if (path) {
2479 dapm_mark_dirty(path->source, "Route removed");
2480 dapm_mark_dirty(path->sink, "Route removed");
2481
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002482 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002483 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002484 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002485 source, sink);
2486 }
2487
2488 return 0;
2489}
2490
Mark Brown105f1c22008-05-13 14:52:19 +02002491/**
Mark Brown105f1c22008-05-13 14:52:19 +02002492 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002493 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002494 * @route: audio routes
2495 * @num: number of routes
2496 *
2497 * Connects 2 dapm widgets together via a named audio path. The sink is
2498 * the widget receiving the audio signal, whilst the source is the sender
2499 * of the audio signal.
2500 *
2501 * Returns 0 for success else error. On error all resources can be freed
2502 * with a call to snd_soc_card_free().
2503 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002504int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002505 const struct snd_soc_dapm_route *route, int num)
2506{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002507 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002508
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002509 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002510 for (i = 0; i < num; i++) {
Lars-Peter Clausena4e91542014-05-07 16:20:25 +02002511 r = snd_soc_dapm_add_route(dapm, route);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002512 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002513 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2514 route->source,
2515 route->control ? route->control : "direct",
2516 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002517 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002518 }
2519 route++;
2520 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002521 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002522
Dan Carpenter60884c22012-04-13 22:25:43 +03002523 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002524}
2525EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2526
Mark Brownefcc3c62012-07-05 17:24:19 +01002527/**
2528 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2529 * @dapm: DAPM context
2530 * @route: audio routes
2531 * @num: number of routes
2532 *
2533 * Removes routes from the DAPM context.
2534 */
2535int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2536 const struct snd_soc_dapm_route *route, int num)
2537{
2538 int i, ret = 0;
2539
2540 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2541 for (i = 0; i < num; i++) {
2542 snd_soc_dapm_del_route(dapm, route);
2543 route++;
2544 }
2545 mutex_unlock(&dapm->card->dapm_mutex);
2546
2547 return ret;
2548}
2549EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2550
Mark Brownbf3a9e12011-06-13 16:42:29 +01002551static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2552 const struct snd_soc_dapm_route *route)
2553{
2554 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2555 route->source,
2556 true);
2557 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2558 route->sink,
2559 true);
2560 struct snd_soc_dapm_path *path;
2561 int count = 0;
2562
2563 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002564 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002565 route->source);
2566 return -ENODEV;
2567 }
2568
2569 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002570 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002571 route->sink);
2572 return -ENODEV;
2573 }
2574
2575 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002576 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002577 route->source, route->sink);
2578
2579 list_for_each_entry(path, &source->sinks, list_source) {
2580 if (path->sink == sink) {
2581 path->weak = 1;
2582 count++;
2583 }
2584 }
2585
2586 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002587 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002588 route->source, route->sink);
2589 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002590 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002591 count, route->source, route->sink);
2592
2593 return 0;
2594}
2595
2596/**
2597 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2598 * @dapm: DAPM context
2599 * @route: audio routes
2600 * @num: number of routes
2601 *
2602 * Mark existing routes matching those specified in the passed array
2603 * as being weak, meaning that they are ignored for the purpose of
2604 * power decisions. The main intended use case is for sidetone paths
2605 * which couple audio between other independent paths if they are both
2606 * active in order to make the combination work better at the user
2607 * level but which aren't intended to be "used".
2608 *
2609 * Note that CODEC drivers should not use this as sidetone type paths
2610 * can frequently also be used as bypass paths.
2611 */
2612int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2613 const struct snd_soc_dapm_route *route, int num)
2614{
2615 int i, err;
2616 int ret = 0;
2617
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002618 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002619 for (i = 0; i < num; i++) {
2620 err = snd_soc_dapm_weak_route(dapm, route);
2621 if (err)
2622 ret = err;
2623 route++;
2624 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002625 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002626
2627 return ret;
2628}
2629EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2630
Mark Brown105f1c22008-05-13 14:52:19 +02002631/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002632 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002633 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002634 *
2635 * Checks the codec for any new dapm widgets and creates them if found.
2636 *
2637 * Returns 0 for success.
2638 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002639int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002640{
2641 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002642 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002643
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002644 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002645
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002646 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002647 {
2648 if (w->new)
2649 continue;
2650
Stephen Warrenfad59882011-04-28 17:37:59 -06002651 if (w->num_kcontrols) {
2652 w->kcontrols = kzalloc(w->num_kcontrols *
2653 sizeof(struct snd_kcontrol *),
2654 GFP_KERNEL);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002655 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002656 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002657 return -ENOMEM;
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002658 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002659 }
2660
Richard Purdie2b97eab2006-10-06 18:32:18 +02002661 switch(w->id) {
2662 case snd_soc_dapm_switch:
2663 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002664 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002665 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002666 break;
2667 case snd_soc_dapm_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002668 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002669 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002670 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002671 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002672 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002673 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002674 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002675 break;
2676 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002677
2678 /* Read the initial power state from the device */
2679 if (w->reg >= 0) {
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08002680 soc_widget_read(w, w->reg, &val);
2681 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002682 val &= w->mask;
2683 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002684 w->power = 1;
2685 }
2686
Richard Purdie2b97eab2006-10-06 18:32:18 +02002687 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002688
Mark Brown7508b122011-10-05 12:09:12 +01002689 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002690 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002691 }
2692
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002693 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2694 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002695 return 0;
2696}
2697EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2698
2699/**
2700 * snd_soc_dapm_get_volsw - dapm mixer get callback
2701 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002702 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002703 *
2704 * Callback to get the value of a dapm mixer control.
2705 *
2706 * Returns 0 for success.
2707 */
2708int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2709 struct snd_ctl_elem_value *ucontrol)
2710{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002711 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002712 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002713 struct soc_mixer_control *mc =
2714 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002715 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002716 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002717 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002718 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002719 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002720 unsigned int val;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002721
2722 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002723 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002724 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002725 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002726
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002727 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002728 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002729 val = (snd_soc_read(codec, reg) >> shift) & mask;
2730 else
2731 val = dapm_kcontrol_get_value(kcontrol);
2732 mutex_unlock(&card->dapm_mutex);
2733
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002734 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002735 ucontrol->value.integer.value[0] = max - val;
2736 else
2737 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002738
2739 return 0;
2740}
2741EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2742
2743/**
2744 * snd_soc_dapm_put_volsw - dapm mixer set callback
2745 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002746 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002747 *
2748 * Callback to set the value of a dapm mixer control.
2749 *
2750 * Returns 0 for success.
2751 */
2752int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2753 struct snd_ctl_elem_value *ucontrol)
2754{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002755 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002756 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002757 struct soc_mixer_control *mc =
2758 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002759 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002760 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002761 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002762 unsigned int mask = (1 << fls(max)) - 1;
2763 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002764 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002765 int connect, change;
2766 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002767 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002768
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002769 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002770 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002771 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002772 kcontrol->id.name);
2773
Richard Purdie2b97eab2006-10-06 18:32:18 +02002774 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002775 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002776
2777 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002778 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002779
Liam Girdwood3cd04342012-03-09 12:02:08 +00002780 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002781
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002782 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00002783 if (change) {
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002784 if (reg != SND_SOC_NOPM) {
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02002785 mask = mask << shift;
2786 val = val << shift;
Mark Brown283375c2009-12-07 18:09:03 +00002787
Lars-Peter Clausenc9e065c2014-05-04 19:17:05 +02002788 if (snd_soc_test_bits(codec, reg, mask, val)) {
2789 update.kcontrol = kcontrol;
2790 update.reg = reg;
2791 update.mask = mask;
2792 update.val = val;
2793 card->update = &update;
2794 }
2795
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002796 }
Mark Brown97404f22010-12-14 16:13:57 +00002797
Nenghua Cao52765972013-12-13 20:13:49 +08002798 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002799
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002800 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002801 }
2802
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002803 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002804
2805 if (ret > 0)
2806 soc_dpcm_runtime_update(card);
2807
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002808 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002809}
2810EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2811
2812/**
2813 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2814 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002815 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002816 *
2817 * Callback to get the value of a dapm enumerated double mixer control.
2818 *
2819 * Returns 0 for success.
2820 */
2821int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2822 struct snd_ctl_elem_value *ucontrol)
2823{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002824 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002825 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002826 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002827
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002828 if (e->reg != SND_SOC_NOPM)
2829 reg_val = snd_soc_read(codec, e->reg);
2830 else
2831 reg_val = dapm_kcontrol_get_value(kcontrol);
2832
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002833 val = (reg_val >> e->shift_l) & e->mask;
2834 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
2835 if (e->shift_l != e->shift_r) {
2836 val = (reg_val >> e->shift_r) & e->mask;
2837 val = snd_soc_enum_val_to_item(e, val);
2838 ucontrol->value.enumerated.item[1] = val;
2839 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002840
2841 return 0;
2842}
2843EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2844
2845/**
2846 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2847 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002848 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002849 *
2850 * Callback to set the value of a dapm enumerated double mixer control.
2851 *
2852 * Returns 0 for success.
2853 */
2854int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2855 struct snd_ctl_elem_value *ucontrol)
2856{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002857 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002858 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002859 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002860 unsigned int *item = ucontrol->value.enumerated.item;
2861 unsigned int val, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002862 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002863 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002864 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002865
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002866 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002867 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002868
2869 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002870 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002871 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002872 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002873 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002874 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002875 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002876 }
2877
Liam Girdwood3cd04342012-03-09 12:02:08 +00002878 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002879
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002880 if (e->reg != SND_SOC_NOPM)
2881 change = snd_soc_test_bits(codec, e->reg, mask, val);
2882 else
2883 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00002884
Richard Purdie2b97eab2006-10-06 18:32:18 +02002885 if (change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002886 if (e->reg != SND_SOC_NOPM) {
2887 update.kcontrol = kcontrol;
2888 update.reg = e->reg;
2889 update.mask = mask;
2890 update.val = val;
2891 card->update = &update;
2892 }
Mark Brown3a655772009-10-05 17:23:30 +01002893
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002894 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002895
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002896 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002897 }
2898
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002899 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002900
2901 if (ret > 0)
2902 soc_dpcm_runtime_update(card);
2903
Mark Brown97404f22010-12-14 16:13:57 +00002904 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002905}
2906EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2907
2908/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002909 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2910 *
2911 * @kcontrol: mixer control
2912 * @uinfo: control element information
2913 *
2914 * Callback to provide information about a pin switch control.
2915 */
2916int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2917 struct snd_ctl_elem_info *uinfo)
2918{
2919 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2920 uinfo->count = 1;
2921 uinfo->value.integer.min = 0;
2922 uinfo->value.integer.max = 1;
2923
2924 return 0;
2925}
2926EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2927
2928/**
2929 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2930 *
2931 * @kcontrol: mixer control
2932 * @ucontrol: Value
2933 */
2934int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2935 struct snd_ctl_elem_value *ucontrol)
2936{
Mark Brown48a8c392012-02-14 17:11:15 -08002937 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002938 const char *pin = (const char *)kcontrol->private_value;
2939
Liam Girdwood3cd04342012-03-09 12:02:08 +00002940 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002941
2942 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08002943 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002944
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002945 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002946
2947 return 0;
2948}
2949EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2950
2951/**
2952 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2953 *
2954 * @kcontrol: mixer control
2955 * @ucontrol: Value
2956 */
2957int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2958 struct snd_ctl_elem_value *ucontrol)
2959{
Mark Brown48a8c392012-02-14 17:11:15 -08002960 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002961 const char *pin = (const char *)kcontrol->private_value;
2962
Mark Brown8b37dbd2009-02-28 21:14:20 +00002963 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08002964 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002965 else
Mark Brown48a8c392012-02-14 17:11:15 -08002966 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002967
Mark Brown48a8c392012-02-14 17:11:15 -08002968 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002969 return 0;
2970}
2971EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2972
Mark Brown5ba06fc2012-02-16 11:07:13 -08002973static struct snd_soc_dapm_widget *
2974snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2975 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002976{
2977 struct snd_soc_dapm_widget *w;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002978 const char *prefix;
Mark Brown62ea8742012-01-21 21:14:48 +00002979 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002980
2981 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08002982 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002983
Mark Brown62ea8742012-01-21 21:14:48 +00002984 switch (w->id) {
2985 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00002986 w->regulator = devm_regulator_get(dapm->dev, w->name);
2987 if (IS_ERR(w->regulator)) {
2988 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002989 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00002990 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08002991 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00002992 }
Mark Brown8784c772013-01-10 19:33:47 +00002993
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002994 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00002995 ret = regulator_allow_bypass(w->regulator, true);
2996 if (ret != 0)
2997 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00002998 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00002999 w->name, ret);
3000 }
Mark Brown62ea8742012-01-21 21:14:48 +00003001 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003002 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003003#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003004 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003005 if (IS_ERR(w->clk)) {
3006 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003007 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003008 w->name, ret);
3009 return NULL;
3010 }
Mark Brownec029952012-06-04 08:16:20 +01003011#else
3012 return NULL;
3013#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003014 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003015 default:
3016 break;
3017 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003018
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02003019 prefix = soc_dapm_prefix(dapm);
3020 if (prefix)
3021 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003022 else
3023 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3024
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003025 if (w->name == NULL) {
3026 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003027 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003028 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003029
Mark Brown7ca3a182011-10-08 14:04:50 +01003030 switch (w->id) {
3031 case snd_soc_dapm_switch:
3032 case snd_soc_dapm_mixer:
3033 case snd_soc_dapm_mixer_named_ctl:
3034 w->power_check = dapm_generic_check_power;
3035 break;
3036 case snd_soc_dapm_mux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003037 w->power_check = dapm_generic_check_power;
3038 break;
Mark Brown46162742013-06-05 19:36:11 +01003039 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003040 w->power_check = dapm_adc_check_power;
3041 break;
Mark Brown46162742013-06-05 19:36:11 +01003042 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003043 w->power_check = dapm_dac_check_power;
3044 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003045 case snd_soc_dapm_adc:
3046 case snd_soc_dapm_aif_out:
3047 case snd_soc_dapm_dac:
3048 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003049 case snd_soc_dapm_pga:
3050 case snd_soc_dapm_out_drv:
3051 case snd_soc_dapm_input:
3052 case snd_soc_dapm_output:
3053 case snd_soc_dapm_micbias:
3054 case snd_soc_dapm_spk:
3055 case snd_soc_dapm_hp:
3056 case snd_soc_dapm_mic:
3057 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003058 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003059 w->power_check = dapm_generic_check_power;
3060 break;
3061 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003062 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003063 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003064 case snd_soc_dapm_kcontrol:
Mark Brown7ca3a182011-10-08 14:04:50 +01003065 w->power_check = dapm_supply_check_power;
3066 break;
3067 default:
3068 w->power_check = dapm_always_on_check_power;
3069 break;
3070 }
3071
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003072 w->dapm = dapm;
3073 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003074 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003075 INIT_LIST_HEAD(&w->sources);
3076 INIT_LIST_HEAD(&w->sinks);
3077 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003078 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003079 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003080
3081 /* machine layer set ups unconnected pins and insertions */
3082 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003083 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003084}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003085
3086/**
Mark Brown4ba13272008-05-13 14:51:19 +02003087 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003088 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003089 * @widget: widget array
3090 * @num: number of widgets
3091 *
3092 * Creates new DAPM controls based upon the templates.
3093 *
3094 * Returns 0 for success else error.
3095 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003096int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003097 const struct snd_soc_dapm_widget *widget,
3098 int num)
3099{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003100 struct snd_soc_dapm_widget *w;
3101 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003102 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003103
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003104 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003105 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003106 w = snd_soc_dapm_new_control(dapm, widget);
3107 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003108 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003109 "ASoC: Failed to create DAPM control %s\n",
3110 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003111 ret = -ENOMEM;
3112 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003113 }
Mark Brown4ba13272008-05-13 14:51:19 +02003114 widget++;
3115 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003116 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003117 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003118}
3119EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3120
Mark Brownc74184e2012-04-04 22:12:09 +01003121static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3122 struct snd_kcontrol *kcontrol, int event)
3123{
3124 struct snd_soc_dapm_path *source_p, *sink_p;
3125 struct snd_soc_dai *source, *sink;
3126 const struct snd_soc_pcm_stream *config = w->params;
3127 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003128 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003129 u64 fmt;
3130 int ret;
3131
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003132 if (WARN_ON(!config) ||
3133 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3134 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003135
3136 /* We only support a single source and sink, pick the first */
3137 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3138 list_sink);
3139 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3140 list_source);
3141
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003142 if (WARN_ON(!source_p || !sink_p) ||
3143 WARN_ON(!sink_p->source || !source_p->sink) ||
3144 WARN_ON(!source_p->source || !sink_p->sink))
3145 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003146
3147 source = source_p->source->priv;
3148 sink = sink_p->sink->priv;
3149
3150 /* Be a little careful as we don't want to overflow the mask array */
3151 if (config->formats) {
3152 fmt = ffs(config->formats) - 1;
3153 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003154 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003155 config->formats);
3156 fmt = 0;
3157 }
3158
3159 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003160 params = kzalloc(sizeof(*params), GFP_KERNEL);
3161 if (!params) {
3162 ret = -ENOMEM;
3163 goto out;
3164 }
3165 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003166
Mark Brown9747cec2012-04-26 19:12:21 +01003167 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003168 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003169 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003170 config->rate_max;
3171
Mark Brown9747cec2012-04-26 19:12:21 +01003172 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003173 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003174 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003175 = config->channels_max;
3176
3177 memset(&substream, 0, sizeof(substream));
3178
3179 switch (event) {
3180 case SND_SOC_DAPM_PRE_PMU:
3181 if (source->driver->ops && source->driver->ops->hw_params) {
3182 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3183 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003184 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003185 if (ret != 0) {
3186 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003187 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003188 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003189 }
3190 }
3191
3192 if (sink->driver->ops && sink->driver->ops->hw_params) {
3193 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003194 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003195 sink);
3196 if (ret != 0) {
3197 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003198 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003199 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003200 }
3201 }
3202 break;
3203
3204 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003205 ret = snd_soc_dai_digital_mute(sink, 0,
3206 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003207 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003208 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003209 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003210 break;
3211
3212 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003213 ret = snd_soc_dai_digital_mute(sink, 1,
3214 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003215 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003216 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003217 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003218 break;
3219
3220 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003221 WARN(1, "Unknown event %d\n", event);
Mark Brownc74184e2012-04-04 22:12:09 +01003222 return -EINVAL;
3223 }
3224
Mark Brown9747cec2012-04-26 19:12:21 +01003225out:
3226 kfree(params);
3227 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003228}
3229
3230int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3231 const struct snd_soc_pcm_stream *params,
3232 struct snd_soc_dapm_widget *source,
3233 struct snd_soc_dapm_widget *sink)
3234{
Mark Brownc74184e2012-04-04 22:12:09 +01003235 struct snd_soc_dapm_widget template;
3236 struct snd_soc_dapm_widget *w;
3237 size_t len;
3238 char *link_name;
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003239 int ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003240
3241 len = strlen(source->name) + strlen(sink->name) + 2;
3242 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3243 if (!link_name)
3244 return -ENOMEM;
3245 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3246
3247 memset(&template, 0, sizeof(template));
3248 template.reg = SND_SOC_NOPM;
3249 template.id = snd_soc_dapm_dai_link;
3250 template.name = link_name;
3251 template.event = snd_soc_dai_link_event;
3252 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3253 SND_SOC_DAPM_PRE_PMD;
3254
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003255 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003256
3257 w = snd_soc_dapm_new_control(&card->dapm, &template);
3258 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003259 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003260 link_name);
3261 return -ENOMEM;
3262 }
3263
3264 w->params = params;
3265
Lars-Peter Clausenfe838972014-05-07 16:20:27 +02003266 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3267 if (ret)
3268 return ret;
3269 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
Mark Brownc74184e2012-04-04 22:12:09 +01003270}
3271
Mark Brown888df392012-02-16 19:37:51 -08003272int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3273 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003274{
Mark Brown888df392012-02-16 19:37:51 -08003275 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003276 struct snd_soc_dapm_widget *w;
3277
Mark Brown888df392012-02-16 19:37:51 -08003278 WARN_ON(dapm->dev != dai->dev);
3279
3280 memset(&template, 0, sizeof(template));
3281 template.reg = SND_SOC_NOPM;
3282
3283 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003284 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003285 template.name = dai->driver->playback.stream_name;
3286 template.sname = dai->driver->playback.stream_name;
3287
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003288 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003289 template.name);
3290
3291 w = snd_soc_dapm_new_control(dapm, &template);
3292 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003293 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003294 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003295 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003296 }
3297
3298 w->priv = dai;
3299 dai->playback_widget = w;
3300 }
3301
3302 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003303 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003304 template.name = dai->driver->capture.stream_name;
3305 template.sname = dai->driver->capture.stream_name;
3306
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003307 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003308 template.name);
3309
3310 w = snd_soc_dapm_new_control(dapm, &template);
3311 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003312 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003313 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003314 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003315 }
3316
3317 w->priv = dai;
3318 dai->capture_widget = w;
3319 }
3320
3321 return 0;
3322}
3323
3324int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3325{
3326 struct snd_soc_dapm_widget *dai_w, *w;
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003327 struct snd_soc_dapm_widget *src, *sink;
Mark Brown888df392012-02-16 19:37:51 -08003328 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003329
3330 /* For each DAI widget... */
3331 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003332 switch (dai_w->id) {
3333 case snd_soc_dapm_dai_in:
3334 case snd_soc_dapm_dai_out:
3335 break;
3336 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003337 continue;
Mark Brown46162742013-06-05 19:36:11 +01003338 }
Mark Brown888df392012-02-16 19:37:51 -08003339
3340 dai = dai_w->priv;
3341
3342 /* ...find all widgets with the same stream and link them */
3343 list_for_each_entry(w, &card->widgets, list) {
3344 if (w->dapm != dai_w->dapm)
3345 continue;
3346
Mark Brown46162742013-06-05 19:36:11 +01003347 switch (w->id) {
3348 case snd_soc_dapm_dai_in:
3349 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003350 continue;
Mark Brown46162742013-06-05 19:36:11 +01003351 default:
3352 break;
3353 }
Mark Brown888df392012-02-16 19:37:51 -08003354
Russell King19c2c5f2013-08-04 20:24:03 +01003355 if (!w->sname || !strstr(w->sname, dai_w->name))
Mark Brown888df392012-02-16 19:37:51 -08003356 continue;
3357
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003358 if (dai_w->id == snd_soc_dapm_dai_in) {
3359 src = dai_w;
3360 sink = w;
3361 } else {
3362 src = w;
3363 sink = dai_w;
Mark Brown888df392012-02-16 19:37:51 -08003364 }
Lars-Peter Clausen0f9bd7b2014-05-07 16:20:28 +02003365 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
3366 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003367 }
3368 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003369
Mark Brown888df392012-02-16 19:37:51 -08003370 return 0;
3371}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003372
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003373void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3374{
3375 struct snd_soc_pcm_runtime *rtd = card->rtd;
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003376 struct snd_soc_dapm_widget *sink, *source;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003377 struct snd_soc_dai *cpu_dai, *codec_dai;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003378 int i;
3379
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003380 /* for each BE DAI link... */
3381 for (i = 0; i < card->num_rtd; i++) {
3382 rtd = &card->rtd[i];
3383 cpu_dai = rtd->cpu_dai;
3384 codec_dai = rtd->codec_dai;
3385
Lars-Peter Clausenca5106a2014-05-07 16:20:24 +02003386 /*
3387 * dynamic FE links have no fixed DAI mapping.
3388 * CODEC<->CODEC links have no direct connection.
3389 */
3390 if (rtd->dai_link->dynamic || rtd->dai_link->params)
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003391 continue;
3392
3393 /* there is no point in connecting BE DAI links with dummies */
3394 if (snd_soc_dai_is_dummy(codec_dai) ||
3395 snd_soc_dai_is_dummy(cpu_dai))
3396 continue;
3397
3398 /* connect BE DAI playback if widgets are valid */
3399 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003400 source = cpu_dai->playback_widget;
3401 sink = codec_dai->playback_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003402 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003403 cpu_dai->component->name, source->name,
3404 codec_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003405
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003406 snd_soc_dapm_add_path(&card->dapm, source, sink,
3407 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003408 }
3409
3410 /* connect BE DAI capture if widgets are valid */
3411 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003412 source = codec_dai->capture_widget;
3413 sink = cpu_dai->capture_widget;
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003414 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003415 codec_dai->component->name, source->name,
3416 cpu_dai->component->name, sink->name);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003417
Lars-Peter Clausen9887c202014-05-07 16:20:26 +02003418 snd_soc_dapm_add_path(&card->dapm, source, sink,
3419 NULL, NULL);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003420 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003421 }
3422}
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003423
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003424static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
3425 int event)
3426{
3427 struct snd_soc_dapm_widget *w;
3428
3429 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3430 w = dai->playback_widget;
3431 else
3432 w = dai->capture_widget;
3433
3434 if (w) {
3435 dapm_mark_dirty(w, "stream event");
3436
3437 switch (event) {
3438 case SND_SOC_DAPM_STREAM_START:
3439 w->active = 1;
3440 break;
3441 case SND_SOC_DAPM_STREAM_STOP:
3442 w->active = 0;
3443 break;
3444 case SND_SOC_DAPM_STREAM_SUSPEND:
3445 case SND_SOC_DAPM_STREAM_RESUME:
3446 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3447 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3448 break;
3449 }
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003450 }
3451}
3452
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003453static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3454 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003455{
Lars-Peter Clausenc471fdd2014-04-29 14:51:22 +02003456 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
3457 soc_dapm_dai_stream_event(rtd->codec_dai, stream, event);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003458
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003459 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003460}
3461
3462/**
3463 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3464 * @rtd: PCM runtime data
3465 * @stream: stream name
3466 * @event: stream event
3467 *
3468 * Sends a stream event to the dapm core. The core then makes any
3469 * necessary widget power changes.
3470 *
3471 * Returns 0 for success else error.
3472 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003473void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3474 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003475{
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003476 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003477
Liam Girdwood3cd04342012-03-09 12:02:08 +00003478 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003479 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003480 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003481}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003482
3483/**
Charles Keepax11391102014-02-18 15:22:14 +00003484 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3485 * @dapm: DAPM context
3486 * @pin: pin name
3487 *
3488 * Enables input/output pin and its parents or children widgets iff there is
3489 * a valid audio route and active audio stream.
3490 *
3491 * Requires external locking.
3492 *
3493 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3494 * do any widget power switching.
3495 */
3496int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3497 const char *pin)
3498{
3499 return snd_soc_dapm_set_pin(dapm, pin, 1);
3500}
3501EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3502
3503/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003504 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003505 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003506 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003507 *
Mark Brown74b8f952009-06-06 11:26:15 +01003508 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003509 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00003510 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003511 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3512 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003513 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003514int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003515{
Charles Keepax11391102014-02-18 15:22:14 +00003516 int ret;
3517
3518 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3519
3520 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3521
3522 mutex_unlock(&dapm->card->dapm_mutex);
3523
3524 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003525}
Liam Girdwooda5302182008-07-07 13:35:17 +01003526EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003527
3528/**
Charles Keepax11391102014-02-18 15:22:14 +00003529 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3530 * @dapm: DAPM context
3531 * @pin: pin name
3532 *
3533 * Enables input/output pin regardless of any other state. This is
3534 * intended for use with microphone bias supplies used in microphone
3535 * jack detection.
3536 *
3537 * Requires external locking.
3538 *
3539 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3540 * do any widget power switching.
3541 */
3542int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3543 const char *pin)
3544{
3545 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3546
3547 if (!w) {
3548 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3549 return -EINVAL;
3550 }
3551
3552 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3553 w->connected = 1;
3554 w->force = 1;
3555 dapm_mark_dirty(w, "force enable");
3556
3557 return 0;
3558}
3559EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3560
3561/**
Mark Brownda341832010-03-15 19:23:37 +00003562 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003563 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003564 * @pin: pin name
3565 *
3566 * Enables input/output pin regardless of any other state. This is
3567 * intended for use with microphone bias supplies used in microphone
3568 * jack detection.
3569 *
3570 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3571 * do any widget power switching.
3572 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003573int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3574 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003575{
Charles Keepax11391102014-02-18 15:22:14 +00003576 int ret;
Mark Brownda341832010-03-15 19:23:37 +00003577
Charles Keepax11391102014-02-18 15:22:14 +00003578 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00003579
Charles Keepax11391102014-02-18 15:22:14 +00003580 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09003581
Charles Keepax11391102014-02-18 15:22:14 +00003582 mutex_unlock(&dapm->card->dapm_mutex);
3583
3584 return ret;
Mark Brownda341832010-03-15 19:23:37 +00003585}
3586EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3587
3588/**
Charles Keepax11391102014-02-18 15:22:14 +00003589 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3590 * @dapm: DAPM context
3591 * @pin: pin name
3592 *
3593 * Disables input/output pin and its parents or children widgets.
3594 *
3595 * Requires external locking.
3596 *
3597 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3598 * do any widget power switching.
3599 */
3600int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3601 const char *pin)
3602{
3603 return snd_soc_dapm_set_pin(dapm, pin, 0);
3604}
3605EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3606
3607/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003608 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003609 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003610 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003611 *
Mark Brown74b8f952009-06-06 11:26:15 +01003612 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00003613 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003614 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3615 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003616 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003617int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3618 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003619{
Charles Keepax11391102014-02-18 15:22:14 +00003620 int ret;
3621
3622 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3623
3624 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3625
3626 mutex_unlock(&dapm->card->dapm_mutex);
3627
3628 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01003629}
3630EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3631
3632/**
Charles Keepax11391102014-02-18 15:22:14 +00003633 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3634 * @dapm: DAPM context
3635 * @pin: pin name
3636 *
3637 * Marks the specified pin as being not connected, disabling it along
3638 * any parent or child widgets. At present this is identical to
3639 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3640 * additional things such as disabling controls which only affect
3641 * paths through the pin.
3642 *
3643 * Requires external locking.
3644 *
3645 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3646 * do any widget power switching.
3647 */
3648int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3649 const char *pin)
3650{
3651 return snd_soc_dapm_set_pin(dapm, pin, 0);
3652}
3653EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3654
3655/**
Mark Brown5817b522008-09-24 11:23:11 +01003656 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003657 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003658 * @pin: pin name
3659 *
3660 * Marks the specified pin as being not connected, disabling it along
3661 * any parent or child widgets. At present this is identical to
3662 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3663 * additional things such as disabling controls which only affect
3664 * paths through the pin.
3665 *
3666 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3667 * do any widget power switching.
3668 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003669int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003670{
Charles Keepax11391102014-02-18 15:22:14 +00003671 int ret;
3672
3673 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3674
3675 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3676
3677 mutex_unlock(&dapm->card->dapm_mutex);
3678
3679 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01003680}
3681EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3682
3683/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003684 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003685 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003686 * @pin: audio signal pin endpoint (or start point)
3687 *
3688 * Get audio pin status - connected or disconnected.
3689 *
3690 * Returns 1 for connected otherwise 0.
3691 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003692int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3693 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003694{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003695 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003696
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003697 if (w)
3698 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003699
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003700 return 0;
3701}
Liam Girdwooda5302182008-07-07 13:35:17 +01003702EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003703
3704/**
Mark Brown1547aba2010-05-07 21:11:40 +01003705 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003706 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003707 * @pin: audio signal pin endpoint (or start point)
3708 *
3709 * Mark the given endpoint or pin as ignoring suspend. When the
3710 * system is disabled a path between two endpoints flagged as ignoring
3711 * suspend will not be disabled. The path must already be enabled via
3712 * normal means at suspend time, it will not be turned on if it was not
3713 * already enabled.
3714 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003715int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3716 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003717{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003718 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003719
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003720 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003721 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003722 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003723 }
3724
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003725 w->ignore_suspend = 1;
3726
3727 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003728}
3729EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3730
Stephen Warren16332812011-11-23 12:42:04 -07003731static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3732 struct snd_soc_dapm_widget *w)
3733{
3734 struct snd_soc_dapm_path *p;
3735
3736 list_for_each_entry(p, &card->paths, list) {
3737 if ((p->source == w) || (p->sink == w)) {
3738 dev_dbg(card->dev,
3739 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3740 p->source->name, p->source->id, p->source->dapm,
3741 p->sink->name, p->sink->id, p->sink->dapm);
3742
3743 /* Connected to something other than the codec */
3744 if (p->source->dapm != p->sink->dapm)
3745 return true;
3746 /*
3747 * Loopback connection from codec external pin to
3748 * codec external pin
3749 */
3750 if (p->sink->id == snd_soc_dapm_input) {
3751 switch (p->source->id) {
3752 case snd_soc_dapm_output:
3753 case snd_soc_dapm_micbias:
3754 return true;
3755 default:
3756 break;
3757 }
3758 }
3759 }
3760 }
3761
3762 return false;
3763}
3764
3765/**
3766 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3767 * @codec: The codec whose pins should be processed
3768 *
3769 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3770 * which are unused. Pins are used if they are connected externally to the
3771 * codec, whether that be to some other device, or a loop-back connection to
3772 * the codec itself.
3773 */
3774void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3775{
3776 struct snd_soc_card *card = codec->card;
3777 struct snd_soc_dapm_context *dapm = &codec->dapm;
3778 struct snd_soc_dapm_widget *w;
3779
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003780 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003781 &card->dapm, &codec->dapm);
3782
3783 list_for_each_entry(w, &card->widgets, list) {
3784 if (w->dapm != dapm)
3785 continue;
3786 switch (w->id) {
3787 case snd_soc_dapm_input:
3788 case snd_soc_dapm_output:
3789 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003790 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003791 w->name);
3792 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003793 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003794 "... Not in map; disabling\n");
3795 snd_soc_dapm_nc_pin(dapm, w->name);
3796 }
3797 break;
3798 default:
3799 break;
3800 }
3801 }
3802}
3803
Mark Brown1547aba2010-05-07 21:11:40 +01003804/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003805 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003806 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003807 *
3808 * Free all dapm widgets and resources.
3809 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003810void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003811{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003812 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003813 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003814 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003815 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003816}
3817EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3818
Xiang Xiao57996352014-03-02 00:04:02 +08003819static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003820{
Liam Girdwood01005a72012-07-06 16:57:05 +01003821 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003822 struct snd_soc_dapm_widget *w;
3823 LIST_HEAD(down_list);
3824 int powerdown = 0;
3825
Liam Girdwood01005a72012-07-06 16:57:05 +01003826 mutex_lock(&card->dapm_mutex);
3827
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003828 list_for_each_entry(w, &dapm->card->widgets, list) {
3829 if (w->dapm != dapm)
3830 continue;
Mark Brown51737472009-06-22 13:16:51 +01003831 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003832 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003833 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003834 powerdown = 1;
3835 }
3836 }
3837
3838 /* If there were no widgets to power down we're already in
3839 * standby.
3840 */
3841 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003842 if (dapm->bias_level == SND_SOC_BIAS_ON)
3843 snd_soc_dapm_set_bias_level(dapm,
3844 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003845 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003846 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3847 snd_soc_dapm_set_bias_level(dapm,
3848 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003849 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003850
3851 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003852}
Mark Brown51737472009-06-22 13:16:51 +01003853
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003854/*
3855 * snd_soc_dapm_shutdown - callback for system shutdown
3856 */
3857void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3858{
Xiang Xiao57996352014-03-02 00:04:02 +08003859 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003860
Xiang Xiao57996352014-03-02 00:04:02 +08003861 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08003862 if (dapm != &card->dapm) {
3863 soc_dapm_shutdown_dapm(dapm);
3864 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
3865 snd_soc_dapm_set_bias_level(dapm,
3866 SND_SOC_BIAS_OFF);
3867 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003868 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08003869
3870 soc_dapm_shutdown_dapm(&card->dapm);
3871 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3872 snd_soc_dapm_set_bias_level(&card->dapm,
3873 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01003874}
3875
Richard Purdie2b97eab2006-10-06 18:32:18 +02003876/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003877MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003878MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3879MODULE_LICENSE("GPL");