blob: f4ba7b40a6abf204f8e06a8d32177a480520fa06 [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 Clausen57295072013-08-05 11:27:31 +0200257 kfree(data->widget);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200258 kfree(data->wlist);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200259 kfree(data);
260}
261
262static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
263 const struct snd_kcontrol *kcontrol)
264{
265 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
266
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200267 return data->wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200268}
269
270static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
271 struct snd_soc_dapm_widget *widget)
272{
273 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200274 struct snd_soc_dapm_widget_list *new_wlist;
275 unsigned int n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200276
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200277 if (data->wlist)
278 n = data->wlist->num_widgets + 1;
279 else
280 n = 1;
281
282 new_wlist = krealloc(data->wlist,
283 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
284 if (!new_wlist)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200285 return -ENOMEM;
286
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200287 new_wlist->widgets[n - 1] = widget;
288 new_wlist->num_widgets = n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200289
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200290 data->wlist = new_wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200291
292 return 0;
293}
294
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200295static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
296 struct snd_soc_dapm_path *path)
297{
298 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
299
300 list_add_tail(&path->list_kcontrol, &data->paths);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200301
302 if (data->widget) {
303 snd_soc_dapm_add_path(data->widget->dapm, data->widget,
304 path->source, NULL, NULL);
305 }
306}
307
308static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
309{
310 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
311
312 if (!data->widget)
313 return true;
314
315 return data->widget->power;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200316}
317
318static struct list_head *dapm_kcontrol_get_path_list(
319 const struct snd_kcontrol *kcontrol)
320{
321 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
322
323 return &data->paths;
324}
325
326#define dapm_kcontrol_for_each_path(path, kcontrol) \
327 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
328 list_kcontrol)
329
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200330static unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
331{
332 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
333
334 return data->value;
335}
336
337static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
338 unsigned int value)
339{
340 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
341
342 if (data->value == value)
343 return false;
344
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200345 if (data->widget)
346 data->widget->on_val = value;
347
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200348 data->value = value;
349
350 return true;
351}
352
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200353/**
354 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
355 * @kcontrol: The kcontrol
356 */
357struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
358{
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200359 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->codec;
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200360}
361EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
362
Liam Girdwood6c120e12012-02-15 15:15:34 +0000363static void dapm_reset(struct snd_soc_card *card)
364{
365 struct snd_soc_dapm_widget *w;
366
Mark Brownf9fa2b12014-03-06 16:49:11 +0800367 lockdep_assert_held(&card->dapm_mutex);
368
Liam Girdwood6c120e12012-02-15 15:15:34 +0000369 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
370
371 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200372 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000373 w->power_checked = false;
374 w->inputs = -1;
375 w->outputs = -1;
376 }
377}
378
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800379static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg,
380 unsigned int *value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100381{
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800382 if (w->codec) {
383 *value = snd_soc_read(w->codec, reg);
384 return 0;
385 } else if (w->platform) {
386 *value = snd_soc_platform_read(w->platform, reg);
387 return 0;
388 }
Liam Girdwoodb7950642011-07-04 22:10:52 +0100389
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000390 dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100391 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100392}
393
Lars-Peter Clausen30ac6b62014-02-13 14:14:21 +0100394static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg,
395 unsigned int val)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100396{
397 if (w->codec)
398 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100399 else if (w->platform)
400 return snd_soc_platform_write(w->platform, reg, val);
401
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000402 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100403 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100404}
405
Liam Girdwood49575fb52012-03-06 18:16:19 +0000406static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
407{
Mark Browne06ab3b2012-03-06 23:58:22 +0000408 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000409 mutex_lock(&w->codec->mutex);
410 else if (w->platform)
411 mutex_lock(&w->platform->mutex);
412}
413
414static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
415{
Mark Browne06ab3b2012-03-06 23:58:22 +0000416 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000417 mutex_unlock(&w->codec->mutex);
418 else if (w->platform)
419 mutex_unlock(&w->platform->mutex);
420}
421
Mark Browneb270e92013-10-09 13:52:52 +0100422static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
423{
424 if (dapm->codec && dapm->codec->using_regmap)
425 regmap_async_complete(dapm->codec->control_data);
426}
427
Liam Girdwood49575fb52012-03-06 18:16:19 +0000428static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
Bard Liao34775012014-04-17 20:12:56 +0800429 int reg, unsigned int mask, unsigned int value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100430{
Mark Brown8a713da2011-12-03 12:33:55 +0000431 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100432 unsigned int old, new;
433 int ret;
434
Mark Brown8a713da2011-12-03 12:33:55 +0000435 if (w->codec && w->codec->using_regmap) {
Mark Browneb270e92013-10-09 13:52:52 +0100436 ret = regmap_update_bits_check_async(w->codec->control_data,
437 reg, mask, value,
438 &change);
Mark Brown8a713da2011-12-03 12:33:55 +0000439 if (ret != 0)
440 return ret;
441 } else {
Liam Girdwood49575fb52012-03-06 18:16:19 +0000442 soc_widget_lock(w);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800443 ret = soc_widget_read(w, reg, &old);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000444 if (ret < 0) {
445 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100446 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000447 }
Mark Brown8a713da2011-12-03 12:33:55 +0000448
Mark Brown8a713da2011-12-03 12:33:55 +0000449 new = (old & ~mask) | (value & mask);
450 change = old != new;
451 if (change) {
452 ret = soc_widget_write(w, reg, new);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000453 if (ret < 0) {
454 soc_widget_unlock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000455 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000456 }
Mark Brown8a713da2011-12-03 12:33:55 +0000457 }
Liam Girdwood49575fb52012-03-06 18:16:19 +0000458 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100459 }
460
461 return change;
462}
463
Mark Brown452c5ea2009-05-17 21:41:23 +0100464/**
465 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800466 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100467 * @level: level to configure
468 *
469 * Configure the bias (power) levels for the SoC audio device.
470 *
471 * Returns 0 for success else error.
472 */
Mark Browned5a4c42011-02-18 11:12:42 -0800473static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200474 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100475{
Mark Browned5a4c42011-02-18 11:12:42 -0800476 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100477 int ret = 0;
478
Mark Brown84e90932010-11-04 00:07:02 -0400479 trace_snd_soc_bias_level_start(card, level);
480
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000481 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100482 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100483 if (ret != 0)
484 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100485
Mark Browncc4c6702011-06-06 19:03:34 +0100486 if (dapm->codec) {
487 if (dapm->codec->driver->set_bias_level)
488 ret = dapm->codec->driver->set_bias_level(dapm->codec,
489 level);
Mark Brownd8c3bb92012-08-23 18:10:42 +0100490 else
491 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100492 } else if (!card || dapm != &card->dapm) {
Liam Girdwood41231282012-07-06 16:56:16 +0100493 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100494 }
Liam Girdwood41231282012-07-06 16:56:16 +0100495
Mark Brown171ec6b2011-06-06 18:15:19 +0100496 if (ret != 0)
497 goto out;
498
499 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100500 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100501out:
Mark Brown84e90932010-11-04 00:07:02 -0400502 trace_snd_soc_bias_level_done(card, level);
503
Mark Brown452c5ea2009-05-17 21:41:23 +0100504 return ret;
505}
506
Mark Brown74b8f952009-06-06 11:26:15 +0100507/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200508static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200509 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
510 struct snd_soc_dapm_path *path, const char *control_name,
511 const struct snd_kcontrol_new *kcontrol)
512{
513 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100514 unsigned int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200515 int i;
516
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100517 if (e->reg != SND_SOC_NOPM) {
518 soc_widget_read(dest, e->reg, &val);
519 val = (val >> e->shift_l) & e->mask;
520 item = snd_soc_enum_val_to_item(e, val);
521 } else {
522 /* since a virtual mux has no backing registers to
523 * decide which path to connect, it will try to match
524 * with the first enumeration. This is to ensure
525 * that the default mux choice (the first) will be
526 * correctly powered up during initialization.
527 */
528 item = 0;
529 }
530
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100531 for (i = 0; i < e->items; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200532 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200533 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200534 list_add(&path->list_sink, &dest->sources);
535 list_add(&path->list_source, &src->sinks);
536 path->name = (char*)e->texts[i];
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100537 if (i == item)
538 path->connect = 1;
539 else
540 path->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200541 return 0;
542 }
543 }
544
545 return -ENODEV;
546}
547
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100548/* set up initial codec paths */
549static void dapm_set_mixer_path_status(struct snd_soc_dapm_widget *w,
550 struct snd_soc_dapm_path *p, int i)
551{
552 struct soc_mixer_control *mc = (struct soc_mixer_control *)
553 w->kcontrol_news[i].private_value;
554 unsigned int reg = mc->reg;
555 unsigned int shift = mc->shift;
556 unsigned int max = mc->max;
557 unsigned int mask = (1 << fls(max)) - 1;
558 unsigned int invert = mc->invert;
559 unsigned int val;
560
561 if (reg != SND_SOC_NOPM) {
562 soc_widget_read(w, reg, &val);
563 val = (val >> shift) & mask;
564 if (invert)
565 val = max - val;
566 p->connect = !!val;
567 } else {
568 p->connect = 0;
569 }
570}
571
Mark Brown74b8f952009-06-06 11:26:15 +0100572/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200573static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200574 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
575 struct snd_soc_dapm_path *path, const char *control_name)
576{
577 int i;
578
579 /* search for mixer kcontrol */
580 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600581 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200582 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200583 list_add(&path->list_sink, &dest->sources);
584 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600585 path->name = dest->kcontrol_news[i].name;
Lars-Peter Clausen234c0b82014-02-28 08:31:12 +0100586 dapm_set_mixer_path_status(dest, path, i);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200587 return 0;
588 }
589 }
590 return -ENODEV;
591}
592
Stephen Warrenaf468002011-04-28 17:38:01 -0600593static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600594 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600595 const struct snd_kcontrol_new *kcontrol_new,
596 struct snd_kcontrol **kcontrol)
597{
598 struct snd_soc_dapm_widget *w;
599 int i;
600
601 *kcontrol = NULL;
602
603 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600604 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
605 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600606 for (i = 0; i < w->num_kcontrols; i++) {
607 if (&w->kcontrol_news[i] == kcontrol_new) {
608 if (w->kcontrols)
609 *kcontrol = w->kcontrols[i];
610 return 1;
611 }
612 }
613 }
614
615 return 0;
616}
617
Stephen Warren85762e72013-03-29 15:40:10 -0600618/*
619 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
620 * create it. Either way, add the widget into the control's widget list
621 */
622static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100623 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200624{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200625 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000626 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000627 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600628 size_t prefix_len;
629 int shared;
630 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600631 bool wname_in_long_name, kcname_in_long_name;
Stephen Warren85762e72013-03-29 15:40:10 -0600632 char *long_name;
633 const char *name;
634 int ret;
Mark Brownefb7ac32011-03-08 17:23:24 +0000635
636 if (dapm->codec)
637 prefix = dapm->codec->name_prefix;
638 else
639 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200640
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000641 if (prefix)
642 prefix_len = strlen(prefix) + 1;
643 else
644 prefix_len = 0;
645
Stephen Warren85762e72013-03-29 15:40:10 -0600646 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
647 &kcontrol);
648
Stephen Warren85762e72013-03-29 15:40:10 -0600649 if (!kcontrol) {
650 if (shared) {
651 wname_in_long_name = false;
652 kcname_in_long_name = true;
653 } else {
654 switch (w->id) {
655 case snd_soc_dapm_switch:
656 case snd_soc_dapm_mixer:
657 wname_in_long_name = true;
658 kcname_in_long_name = true;
659 break;
660 case snd_soc_dapm_mixer_named_ctl:
661 wname_in_long_name = false;
662 kcname_in_long_name = true;
663 break;
664 case snd_soc_dapm_mux:
Stephen Warren85762e72013-03-29 15:40:10 -0600665 wname_in_long_name = true;
666 kcname_in_long_name = false;
667 break;
668 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600669 return -EINVAL;
670 }
671 }
672
673 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600674 /*
675 * The control will get a prefix from the control
676 * creation process but we're also using the same
677 * prefix for widgets so cut the prefix off the
678 * front of the widget name.
679 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200680 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600681 w->name + prefix_len,
682 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200683 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200684 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600685
686 name = long_name;
687 } else if (wname_in_long_name) {
688 long_name = NULL;
689 name = w->name + prefix_len;
690 } else {
691 long_name = NULL;
692 name = w->kcontrol_news[kci].name;
693 }
694
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200695 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600696 prefix);
Lars-Peter Clausen656ca9d2013-06-14 13:16:54 +0200697 kfree(long_name);
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200698 if (!kcontrol)
699 return -ENOMEM;
700 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200701
702 ret = dapm_kcontrol_data_alloc(w, kcontrol);
703 if (ret) {
704 snd_ctl_free_one(kcontrol);
705 return ret;
706 }
707
Stephen Warren85762e72013-03-29 15:40:10 -0600708 ret = snd_ctl_add(card, kcontrol);
709 if (ret < 0) {
710 dev_err(dapm->dev,
711 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
712 w->name, name, ret);
Stephen Warren85762e72013-03-29 15:40:10 -0600713 return ret;
714 }
Stephen Warren85762e72013-03-29 15:40:10 -0600715 }
716
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200717 ret = dapm_kcontrol_add_widget(kcontrol, w);
718 if (ret)
719 return ret;
720
Stephen Warren85762e72013-03-29 15:40:10 -0600721 w->kcontrols[kci] = kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600722
723 return 0;
724}
725
726/* create new dapm mixer control */
727static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
728{
729 int i, ret;
730 struct snd_soc_dapm_path *path;
731
Richard Purdie2b97eab2006-10-06 18:32:18 +0200732 /* add kcontrol */
733 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200734 /* match name */
735 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200736 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600737 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200738 continue;
739
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200740 if (w->kcontrols[i]) {
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200741 dapm_kcontrol_add_path(w->kcontrols[i], path);
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200742 continue;
743 }
744
Mark Brown946d92a2013-08-12 23:28:42 +0100745 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
Stephen Warren85762e72013-03-29 15:40:10 -0600746 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200747 return ret;
Mark Brown946d92a2013-08-12 23:28:42 +0100748
749 dapm_kcontrol_add_path(w->kcontrols[i], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200750 }
751 }
Stephen Warren85762e72013-03-29 15:40:10 -0600752
753 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200754}
755
756/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200757static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200758{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200759 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600760 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600761 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200762
Stephen Warrenaf468002011-04-28 17:38:01 -0600763 if (w->num_kcontrols != 1) {
764 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000765 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600766 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200767 return -EINVAL;
768 }
769
Lars-Peter Clausenfe581392013-08-01 18:30:38 +0200770 if (list_empty(&w->sources)) {
Stephen Warren85762e72013-03-29 15:40:10 -0600771 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
772 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600773 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200774
Mark Brown946d92a2013-08-12 23:28:42 +0100775 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600776 if (ret < 0)
777 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600778
Richard Purdie2b97eab2006-10-06 18:32:18 +0200779 list_for_each_entry(path, &w->sources, list_sink)
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200780 dapm_kcontrol_add_path(w->kcontrols[0], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200781
Stephen Warrenaf468002011-04-28 17:38:01 -0600782 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200783}
784
785/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200786static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200787{
Mark Browna6c65732010-03-03 17:45:21 +0000788 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200789 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000790 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200791
Mark Browna6c65732010-03-03 17:45:21 +0000792 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200793}
794
795/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100796static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
797 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200798{
799 struct snd_soc_dapm_path *p;
800
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100801 list_for_each_entry(p, sink, list_source) {
802 if (p->walked) {
803 p->walked = 0;
804 dapm_clear_walk_output(dapm, &p->sink->sinks);
805 }
806 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200807}
808
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100809static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
810 struct list_head *source)
811{
812 struct snd_soc_dapm_path *p;
813
814 list_for_each_entry(p, source, list_sink) {
815 if (p->walked) {
816 p->walked = 0;
817 dapm_clear_walk_input(dapm, &p->source->sources);
818 }
819 }
820}
821
822
Mark Brown99497882010-05-07 20:24:05 +0100823/* We implement power down on suspend by checking the power state of
824 * the ALSA card - when we are suspending the ALSA state for the card
825 * is set to D3.
826 */
827static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
828{
Mark Brown12ea2c72011-03-02 18:17:32 +0000829 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100830
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000831 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100832 case SNDRV_CTL_POWER_D3hot:
833 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100834 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000835 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200836 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100837 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100838 default:
839 return 1;
840 }
841}
842
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100843/* add widget to list if it's not already in the list */
844static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
845 struct snd_soc_dapm_widget *w)
846{
847 struct snd_soc_dapm_widget_list *wlist;
848 int wlistsize, wlistentries, i;
849
850 if (*list == NULL)
851 return -EINVAL;
852
853 wlist = *list;
854
855 /* is this widget already in the list */
856 for (i = 0; i < wlist->num_widgets; i++) {
857 if (wlist->widgets[i] == w)
858 return 0;
859 }
860
861 /* allocate some new space */
862 wlistentries = wlist->num_widgets + 1;
863 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
864 wlistentries * sizeof(struct snd_soc_dapm_widget *);
865 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
866 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000867 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100868 w->name);
869 return -ENOMEM;
870 }
871 wlist = *list;
872
873 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000874 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100875 w->name, wlist->num_widgets);
876
877 wlist->widgets[wlist->num_widgets] = w;
878 wlist->num_widgets++;
879 return 1;
880}
881
Richard Purdie2b97eab2006-10-06 18:32:18 +0200882/*
883 * Recursively check for a completed path to an active or physically connected
884 * output widget. Returns number of complete paths.
885 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100886static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
887 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200888{
889 struct snd_soc_dapm_path *path;
890 int con = 0;
891
Mark Brown024dc072011-10-09 11:52:05 +0100892 if (widget->outputs >= 0)
893 return widget->outputs;
894
Mark Brownde02d072011-09-20 21:43:24 +0100895 DAPM_UPDATE_STAT(widget, path_checks);
896
Mark Brown62ea8742012-01-21 21:14:48 +0000897 switch (widget->id) {
898 case snd_soc_dapm_supply:
899 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200900 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200901 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100902 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000903 default:
904 break;
905 }
Mark Brown246d0a12009-04-22 18:24:55 +0100906
Mark Brown010ff262009-08-17 17:39:22 +0100907 switch (widget->id) {
908 case snd_soc_dapm_adc:
909 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100910 case snd_soc_dapm_dai_out:
Mark Brown024dc072011-10-09 11:52:05 +0100911 if (widget->active) {
912 widget->outputs = snd_soc_dapm_suspend_check(widget);
913 return widget->outputs;
914 }
Mark Brown010ff262009-08-17 17:39:22 +0100915 default:
916 break;
917 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200918
919 if (widget->connected) {
920 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100921 if (widget->id == snd_soc_dapm_output && !widget->ext) {
922 widget->outputs = snd_soc_dapm_suspend_check(widget);
923 return widget->outputs;
924 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200925
926 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100927 if (widget->id == snd_soc_dapm_hp ||
928 widget->id == snd_soc_dapm_spk ||
929 (widget->id == snd_soc_dapm_line &&
930 !list_empty(&widget->sources))) {
931 widget->outputs = snd_soc_dapm_suspend_check(widget);
932 return widget->outputs;
933 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200934 }
935
936 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e2011-09-21 18:19:14 +0100937 DAPM_UPDATE_STAT(widget, neighbour_checks);
938
Mark Brownbf3a9e12011-06-13 16:42:29 +0100939 if (path->weak)
940 continue;
941
Mark Brown8af294b2013-02-22 17:48:15 +0000942 if (path->walking)
943 return 1;
944
Richard Purdie2b97eab2006-10-06 18:32:18 +0200945 if (path->walked)
946 continue;
947
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100948 trace_snd_soc_dapm_output_path(widget, path);
949
Richard Purdie2b97eab2006-10-06 18:32:18 +0200950 if (path->sink && path->connect) {
951 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +0000952 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100953
954 /* do we need to add this widget to the list ? */
955 if (list) {
956 int err;
957 err = dapm_list_add_widget(list, path->sink);
958 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000959 dev_err(widget->dapm->dev,
960 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100961 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +0000962 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100963 return con;
964 }
965 }
966
967 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +0000968
969 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200970 }
971 }
972
Mark Brown024dc072011-10-09 11:52:05 +0100973 widget->outputs = con;
974
Richard Purdie2b97eab2006-10-06 18:32:18 +0200975 return con;
976}
977
978/*
979 * Recursively check for a completed path to an active or physically connected
980 * input widget. Returns number of complete paths.
981 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100982static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
983 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200984{
985 struct snd_soc_dapm_path *path;
986 int con = 0;
987
Mark Brown024dc072011-10-09 11:52:05 +0100988 if (widget->inputs >= 0)
989 return widget->inputs;
990
Mark Brownde02d072011-09-20 21:43:24 +0100991 DAPM_UPDATE_STAT(widget, path_checks);
992
Mark Brown62ea8742012-01-21 21:14:48 +0000993 switch (widget->id) {
994 case snd_soc_dapm_supply:
995 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200996 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200997 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100998 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000999 default:
1000 break;
1001 }
Mark Brown246d0a12009-04-22 18:24:55 +01001002
Richard Purdie2b97eab2006-10-06 18:32:18 +02001003 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +01001004 switch (widget->id) {
1005 case snd_soc_dapm_dac:
1006 case snd_soc_dapm_aif_in:
Mark Brown46162742013-06-05 19:36:11 +01001007 case snd_soc_dapm_dai_in:
Mark Brown024dc072011-10-09 11:52:05 +01001008 if (widget->active) {
1009 widget->inputs = snd_soc_dapm_suspend_check(widget);
1010 return widget->inputs;
1011 }
Mark Brown010ff262009-08-17 17:39:22 +01001012 default:
1013 break;
1014 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001015
1016 if (widget->connected) {
1017 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +01001018 if (widget->id == snd_soc_dapm_input && !widget->ext) {
1019 widget->inputs = snd_soc_dapm_suspend_check(widget);
1020 return widget->inputs;
1021 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001022
1023 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +01001024 if (widget->id == snd_soc_dapm_vmid) {
1025 widget->inputs = snd_soc_dapm_suspend_check(widget);
1026 return widget->inputs;
1027 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001028
1029 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +03001030 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +01001031 (widget->id == snd_soc_dapm_line &&
1032 !list_empty(&widget->sinks))) {
1033 widget->inputs = snd_soc_dapm_suspend_check(widget);
1034 return widget->inputs;
1035 }
1036
Mark Brown1ab97c82011-11-27 16:21:51 +00001037 /* signal generator */
1038 if (widget->id == snd_soc_dapm_siggen) {
1039 widget->inputs = snd_soc_dapm_suspend_check(widget);
1040 return widget->inputs;
1041 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001042 }
1043
1044 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e2011-09-21 18:19:14 +01001045 DAPM_UPDATE_STAT(widget, neighbour_checks);
1046
Mark Brownbf3a9e12011-06-13 16:42:29 +01001047 if (path->weak)
1048 continue;
1049
Mark Brown8af294b2013-02-22 17:48:15 +00001050 if (path->walking)
1051 return 1;
1052
Richard Purdie2b97eab2006-10-06 18:32:18 +02001053 if (path->walked)
1054 continue;
1055
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001056 trace_snd_soc_dapm_input_path(widget, path);
1057
Richard Purdie2b97eab2006-10-06 18:32:18 +02001058 if (path->source && path->connect) {
1059 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +00001060 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001061
1062 /* do we need to add this widget to the list ? */
1063 if (list) {
1064 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +01001065 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001066 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001067 dev_err(widget->dapm->dev,
1068 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001069 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001070 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001071 return con;
1072 }
1073 }
1074
1075 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001076
1077 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001078 }
1079 }
1080
Mark Brown024dc072011-10-09 11:52:05 +01001081 widget->inputs = con;
1082
Richard Purdie2b97eab2006-10-06 18:32:18 +02001083 return con;
1084}
1085
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001086/**
1087 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1088 * @dai: the soc DAI.
1089 * @stream: stream direction.
1090 * @list: list of active widgets for this stream.
1091 *
1092 * Queries DAPM graph as to whether an valid audio stream path exists for
1093 * the initial stream specified by name. This takes into account
1094 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1095 *
1096 * Returns the number of valid paths or negative error.
1097 */
1098int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1099 struct snd_soc_dapm_widget_list **list)
1100{
1101 struct snd_soc_card *card = dai->card;
1102 int paths;
1103
1104 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1105 dapm_reset(card);
1106
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001107 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001108 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001109 dapm_clear_walk_output(&card->dapm,
1110 &dai->playback_widget->sinks);
1111 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001112 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001113 dapm_clear_walk_input(&card->dapm,
1114 &dai->capture_widget->sources);
1115 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001116
1117 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001118 mutex_unlock(&card->dapm_mutex);
1119
1120 return paths;
1121}
1122
Richard Purdie2b97eab2006-10-06 18:32:18 +02001123/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001124 * Handler for generic register modifier widget.
1125 */
1126int dapm_reg_event(struct snd_soc_dapm_widget *w,
1127 struct snd_kcontrol *kcontrol, int event)
1128{
1129 unsigned int val;
1130
1131 if (SND_SOC_DAPM_EVENT_ON(event))
1132 val = w->on_val;
1133 else
1134 val = w->off_val;
1135
Liam Girdwood49575fb52012-03-06 18:16:19 +00001136 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001137 w->mask << w->shift, val << w->shift);
1138
1139 return 0;
1140}
Mark Brown11589412008-07-29 11:42:23 +01001141EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001142
Mark Brown62ea8742012-01-21 21:14:48 +00001143/*
1144 * Handler for regulator supply widget.
1145 */
1146int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1147 struct snd_kcontrol *kcontrol, int event)
1148{
Mark Brownc05b84d2012-09-07 12:57:11 +08001149 int ret;
1150
Mark Browneb270e92013-10-09 13:52:52 +01001151 soc_dapm_async_complete(w->dapm);
1152
Mark Brownc05b84d2012-09-07 12:57:11 +08001153 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001154 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001155 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001156 if (ret != 0)
1157 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001158 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001159 w->name, ret);
1160 }
1161
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001162 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001163 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001164 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001165 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001166 if (ret != 0)
1167 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001168 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001169 w->name, ret);
1170 }
1171
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001172 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001173 }
Mark Brown62ea8742012-01-21 21:14:48 +00001174}
1175EXPORT_SYMBOL_GPL(dapm_regulator_event);
1176
Ola Liljad7e7eb92012-05-24 15:26:25 +02001177/*
1178 * Handler for clock supply widget.
1179 */
1180int dapm_clock_event(struct snd_soc_dapm_widget *w,
1181 struct snd_kcontrol *kcontrol, int event)
1182{
1183 if (!w->clk)
1184 return -EIO;
1185
Mark Browneb270e92013-10-09 13:52:52 +01001186 soc_dapm_async_complete(w->dapm);
1187
Mark Brownec029952012-06-04 08:16:20 +01001188#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001189 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001190 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001191 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001192 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001193 return 0;
1194 }
Mark Brownec029952012-06-04 08:16:20 +01001195#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001196 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001197}
1198EXPORT_SYMBOL_GPL(dapm_clock_event);
1199
Mark Brownd8050022011-09-28 18:28:23 +01001200static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1201{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001202 if (w->power_checked)
1203 return w->new_power;
1204
Mark Brownd8050022011-09-28 18:28:23 +01001205 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001206 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001207 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001208 w->new_power = w->power_check(w);
1209
1210 w->power_checked = true;
1211
1212 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001213}
1214
Mark Browncd0f2d42009-04-20 16:56:59 +01001215/* Generic check to see if a widget should be powered.
1216 */
1217static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1218{
1219 int in, out;
1220
Mark Brownde02d072011-09-20 21:43:24 +01001221 DAPM_UPDATE_STAT(w, power_checks);
1222
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001223 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001224 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001225 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001226 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001227 return out != 0 && in != 0;
1228}
1229
Mark Brown6ea31b92009-04-20 17:15:41 +01001230/* Check to see if an ADC has power */
1231static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1232{
1233 int in;
1234
Mark Brownde02d072011-09-20 21:43:24 +01001235 DAPM_UPDATE_STAT(w, power_checks);
1236
Mark Brown6ea31b92009-04-20 17:15:41 +01001237 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001238 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001239 dapm_clear_walk_input(w->dapm, &w->sources);
Mark Brown6ea31b92009-04-20 17:15:41 +01001240 return in != 0;
1241 } else {
1242 return dapm_generic_check_power(w);
1243 }
1244}
1245
1246/* Check to see if a DAC has power */
1247static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1248{
1249 int out;
1250
Mark Brownde02d072011-09-20 21:43:24 +01001251 DAPM_UPDATE_STAT(w, power_checks);
1252
Mark Brown6ea31b92009-04-20 17:15:41 +01001253 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001254 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001255 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001256 return out != 0;
1257 } else {
1258 return dapm_generic_check_power(w);
1259 }
1260}
1261
Mark Brown246d0a12009-04-22 18:24:55 +01001262/* Check to see if a power supply is needed */
1263static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1264{
1265 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001266
Mark Brownde02d072011-09-20 21:43:24 +01001267 DAPM_UPDATE_STAT(w, power_checks);
1268
Mark Brown246d0a12009-04-22 18:24:55 +01001269 /* Check if one of our outputs is connected */
1270 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001271 DAPM_UPDATE_STAT(w, neighbour_checks);
1272
Mark Brownbf3a9e12011-06-13 16:42:29 +01001273 if (path->weak)
1274 continue;
1275
Mark Brown215edda2009-09-08 18:59:05 +01001276 if (path->connected &&
1277 !path->connected(path->source, path->sink))
1278 continue;
1279
Mark Brown30173582011-02-11 11:42:19 +00001280 if (!path->sink)
1281 continue;
1282
Mark Brownf68d7e12011-10-04 22:57:50 +01001283 if (dapm_widget_power_check(path->sink))
1284 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001285 }
1286
Mark Brownf68d7e12011-10-04 22:57:50 +01001287 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001288}
1289
Mark Brown35c64bc2011-09-28 18:23:53 +01001290static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1291{
1292 return 1;
1293}
1294
Mark Brown38357ab2009-06-06 19:03:23 +01001295static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1296 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001297 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001298{
Mark Brown828a8422011-01-15 13:14:30 +00001299 int *sort;
1300
1301 if (power_up)
1302 sort = dapm_up_seq;
1303 else
1304 sort = dapm_down_seq;
1305
Mark Brown38357ab2009-06-06 19:03:23 +01001306 if (sort[a->id] != sort[b->id])
1307 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001308 if (a->subseq != b->subseq) {
1309 if (power_up)
1310 return a->subseq - b->subseq;
1311 else
1312 return b->subseq - a->subseq;
1313 }
Mark Brownb22ead22009-06-07 12:51:26 +01001314 if (a->reg != b->reg)
1315 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001316 if (a->dapm != b->dapm)
1317 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001318
Mark Brown38357ab2009-06-06 19:03:23 +01001319 return 0;
1320}
Mark Brown42aa3412009-03-01 19:21:10 +00001321
Mark Brown38357ab2009-06-06 19:03:23 +01001322/* Insert a widget in order into a DAPM power sequence. */
1323static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1324 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001325 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001326{
1327 struct snd_soc_dapm_widget *w;
1328
1329 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001330 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001331 list_add_tail(&new_widget->power_list, &w->power_list);
1332 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001333 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001334
Mark Brown38357ab2009-06-06 19:03:23 +01001335 list_add_tail(&new_widget->power_list, list);
1336}
Mark Brown42aa3412009-03-01 19:21:10 +00001337
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001338static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001339 struct snd_soc_dapm_widget *w, int event)
1340{
Mark Brown68f89ad2010-11-03 23:51:49 -04001341 const char *ev_name;
1342 int power, ret;
1343
1344 switch (event) {
1345 case SND_SOC_DAPM_PRE_PMU:
1346 ev_name = "PRE_PMU";
1347 power = 1;
1348 break;
1349 case SND_SOC_DAPM_POST_PMU:
1350 ev_name = "POST_PMU";
1351 power = 1;
1352 break;
1353 case SND_SOC_DAPM_PRE_PMD:
1354 ev_name = "PRE_PMD";
1355 power = 0;
1356 break;
1357 case SND_SOC_DAPM_POST_PMD:
1358 ev_name = "POST_PMD";
1359 power = 0;
1360 break;
Mark Brown80114122013-02-25 15:14:19 +00001361 case SND_SOC_DAPM_WILL_PMU:
1362 ev_name = "WILL_PMU";
1363 power = 1;
1364 break;
1365 case SND_SOC_DAPM_WILL_PMD:
1366 ev_name = "WILL_PMD";
1367 power = 0;
1368 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001369 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001370 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001371 return;
1372 }
1373
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001374 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001375 return;
1376
1377 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001378 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001379 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001380 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001381 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001382 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001383 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001384 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001385 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001386 ev_name, w->name, ret);
1387 }
1388}
1389
Mark Brownb22ead22009-06-07 12:51:26 +01001390/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001391static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001392 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001393{
Mark Brown68f89ad2010-11-03 23:51:49 -04001394 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001395 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001396 unsigned int value = 0;
1397 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001398
1399 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1400 power_list)->reg;
1401
1402 list_for_each_entry(w, pending, power_list) {
Takashi Iwaibf4edea2013-11-07 18:38:47 +01001403 WARN_ON(reg != w->reg);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001404 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001405
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001406 mask |= w->mask << w->shift;
1407 if (w->power)
1408 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001409 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001410 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001411
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001412 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001413 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1414 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001415
Mark Brown68f89ad2010-11-03 23:51:49 -04001416 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001417 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1418 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001419 }
1420
Mark Brown81628102009-06-07 13:21:24 +01001421 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001422 /* Any widget will do, they should all be updating the
1423 * same register.
1424 */
1425 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1426 power_list);
1427
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001428 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001429 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001430 value, mask, reg, card->pop_time);
1431 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001432 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001433 }
1434
1435 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001436 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1437 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001438 }
Mark Brown42aa3412009-03-01 19:21:10 +00001439}
1440
Mark Brownb22ead22009-06-07 12:51:26 +01001441/* Apply a DAPM power sequence.
1442 *
1443 * We walk over a pre-sorted list of widgets to apply power to. In
1444 * order to minimise the number of writes to the device required
1445 * multiple widgets will be updated in a single write where possible.
1446 * Currently anything that requires more than a single write is not
1447 * handled.
1448 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001449static void dapm_seq_run(struct snd_soc_card *card,
1450 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001451{
1452 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001453 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001454 LIST_HEAD(pending);
1455 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001456 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001457 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001458 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001459 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001460 int *sort;
1461
1462 if (power_up)
1463 sort = dapm_up_seq;
1464 else
1465 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001466
Mark Brownb22ead22009-06-07 12:51:26 +01001467 list_for_each_entry_safe(w, n, list, power_list) {
1468 ret = 0;
1469
1470 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001471 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001472 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001473 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001474 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001475
Mark Brown474b62d2011-01-18 16:14:44 +00001476 if (cur_dapm && cur_dapm->seq_notifier) {
1477 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1478 if (sort[i] == cur_sort)
1479 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001480 i,
1481 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001482 }
1483
Mark Browneb270e92013-10-09 13:52:52 +01001484 if (cur_dapm && w->dapm != cur_dapm)
1485 soc_dapm_async_complete(cur_dapm);
1486
Mark Brownb22ead22009-06-07 12:51:26 +01001487 INIT_LIST_HEAD(&pending);
1488 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001489 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001490 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001491 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001492 }
1493
Mark Brown163cac02009-06-07 10:12:52 +01001494 switch (w->id) {
1495 case snd_soc_dapm_pre:
1496 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001497 list_for_each_entry_safe_continue(w, n, list,
1498 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001499
Mark Brownb22ead22009-06-07 12:51:26 +01001500 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001501 ret = w->event(w,
1502 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001503 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001504 ret = w->event(w,
1505 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001506 break;
1507
1508 case snd_soc_dapm_post:
1509 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001510 list_for_each_entry_safe_continue(w, n, list,
1511 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001512
Mark Brownb22ead22009-06-07 12:51:26 +01001513 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001514 ret = w->event(w,
1515 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001516 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001517 ret = w->event(w,
1518 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001519 break;
1520
Mark Brown163cac02009-06-07 10:12:52 +01001521 default:
Mark Brown81628102009-06-07 13:21:24 +01001522 /* Queue it up for application */
1523 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001524 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001525 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001526 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001527 list_move(&w->power_list, &pending);
1528 break;
Mark Brown163cac02009-06-07 10:12:52 +01001529 }
Mark Brownb22ead22009-06-07 12:51:26 +01001530
1531 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001532 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001533 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001534 }
Mark Brownb22ead22009-06-07 12:51:26 +01001535
1536 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001537 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001538
1539 if (cur_dapm && cur_dapm->seq_notifier) {
1540 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1541 if (sort[i] == cur_sort)
1542 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001543 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001544 }
Mark Browneb270e92013-10-09 13:52:52 +01001545
1546 list_for_each_entry(d, &card->dapm_list, list) {
1547 soc_dapm_async_complete(d);
1548 }
Mark Brown163cac02009-06-07 10:12:52 +01001549}
1550
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001551static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001552{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001553 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001554 struct snd_soc_dapm_widget_list *wlist;
1555 struct snd_soc_dapm_widget *w = NULL;
1556 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001557 int ret;
1558
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001559 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001560 return;
1561
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001562 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001563
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001564 for (wi = 0; wi < wlist->num_widgets; wi++) {
1565 w = wlist->widgets[wi];
1566
1567 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1568 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1569 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001570 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001571 w->name, ret);
1572 }
Mark Brown97404f22010-12-14 16:13:57 +00001573 }
1574
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001575 if (!w)
1576 return;
1577
Liam Girdwood49575fb52012-03-06 18:16:19 +00001578 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001579 update->val);
1580 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001581 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001582 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001583
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001584 for (wi = 0; wi < wlist->num_widgets; wi++) {
1585 w = wlist->widgets[wi];
1586
1587 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1588 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1589 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001590 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001591 w->name, ret);
1592 }
Mark Brown97404f22010-12-14 16:13:57 +00001593 }
1594}
1595
Mark Brown9d0624a2011-02-18 11:49:43 -08001596/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1597 * they're changing state.
1598 */
1599static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1600{
1601 struct snd_soc_dapm_context *d = data;
1602 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001603
Mark Brown56fba412011-06-04 11:25:10 +01001604 /* If we're off and we're not supposed to be go into STANDBY */
1605 if (d->bias_level == SND_SOC_BIAS_OFF &&
1606 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001607 if (d->dev)
1608 pm_runtime_get_sync(d->dev);
1609
Mark Brown9d0624a2011-02-18 11:49:43 -08001610 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1611 if (ret != 0)
1612 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001613 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001614 }
1615
Mark Brown56fba412011-06-04 11:25:10 +01001616 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1617 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001618 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1619 if (ret != 0)
1620 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001621 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001622 }
1623}
1624
1625/* Async callback run prior to DAPM sequences - brings to their final
1626 * state.
1627 */
1628static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1629{
1630 struct snd_soc_dapm_context *d = data;
1631 int ret;
1632
1633 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001634 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1635 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1636 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001637 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1638 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001639 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001640 ret);
1641 }
1642
1643 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001644 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1645 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001646 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1647 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001648 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1649 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001650
1651 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001652 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001653 }
1654
1655 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001656 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1657 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001658 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1659 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001660 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001661 ret);
1662 }
1663}
Mark Brown97404f22010-12-14 16:13:57 +00001664
Mark Brownfe4fda52011-10-03 22:36:57 +01001665static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1666 bool power, bool connect)
1667{
1668 /* If a connection is being made or broken then that update
1669 * will have marked the peer dirty, otherwise the widgets are
1670 * not connected and this update has no impact. */
1671 if (!connect)
1672 return;
1673
1674 /* If the peer is already in the state we're moving to then we
1675 * won't have an impact on it. */
1676 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001677 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001678}
1679
Mark Brown05623c42011-09-28 17:02:31 +01001680static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1681 struct list_head *up_list,
1682 struct list_head *down_list)
1683{
Mark Browndb432b42011-10-03 21:06:40 +01001684 struct snd_soc_dapm_path *path;
1685
Mark Brown05623c42011-09-28 17:02:31 +01001686 if (w->power == power)
1687 return;
1688
1689 trace_snd_soc_dapm_widget_power(w, power);
1690
Mark Browndb432b42011-10-03 21:06:40 +01001691 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001692 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001693 */
1694 list_for_each_entry(path, &w->sources, list_sink) {
1695 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001696 dapm_widget_set_peer_power(path->source, power,
1697 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001698 }
1699 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001700 switch (w->id) {
1701 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001702 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001703 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001704 case snd_soc_dapm_kcontrol:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001705 /* Supplies can't affect their outputs, only their inputs */
1706 break;
1707 default:
1708 list_for_each_entry(path, &w->sinks, list_source) {
1709 if (path->sink) {
1710 dapm_widget_set_peer_power(path->sink, power,
1711 path->connect);
1712 }
Mark Browndb432b42011-10-03 21:06:40 +01001713 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001714 break;
Mark Browndb432b42011-10-03 21:06:40 +01001715 }
1716
Mark Brown05623c42011-09-28 17:02:31 +01001717 if (power)
1718 dapm_seq_insert(w, up_list, true);
1719 else
1720 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001721}
1722
Mark Brown7c81beb2011-09-20 22:22:32 +01001723static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1724 struct list_head *up_list,
1725 struct list_head *down_list)
1726{
Mark Brown7c81beb2011-09-20 22:22:32 +01001727 int power;
1728
1729 switch (w->id) {
1730 case snd_soc_dapm_pre:
1731 dapm_seq_insert(w, down_list, false);
1732 break;
1733 case snd_soc_dapm_post:
1734 dapm_seq_insert(w, up_list, true);
1735 break;
1736
1737 default:
Mark Brownd8050022011-09-28 18:28:23 +01001738 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001739
Mark Brown05623c42011-09-28 17:02:31 +01001740 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001741 break;
1742 }
1743}
1744
Mark Brown42aa3412009-03-01 19:21:10 +00001745/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001746 * Scan each dapm widget for complete audio path.
1747 * A complete path is a route that has valid endpoints i.e.:-
1748 *
1749 * o DAC to output pin.
1750 * o Input Pin to ADC.
1751 * o Input pin to Output pin (bypass, sidetone)
1752 * o DAC to ADC (loopback).
1753 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001754static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001755{
1756 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001757 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001758 LIST_HEAD(up_list);
1759 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001760 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001761 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001762
Mark Brownf9fa2b12014-03-06 16:49:11 +08001763 lockdep_assert_held(&card->dapm_mutex);
1764
Mark Brown84e90932010-11-04 00:07:02 -04001765 trace_snd_soc_dapm_start(card);
1766
Mark Brown56fba412011-06-04 11:25:10 +01001767 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001768 if (d->idle_bias_off)
1769 d->target_bias_level = SND_SOC_BIAS_OFF;
1770 else
1771 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001772 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001773
Liam Girdwood6c120e12012-02-15 15:15:34 +00001774 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001775
Mark Brown6d3ddc82009-05-16 17:47:29 +01001776 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001777 * lists indicating if they should be powered up or down. We
1778 * only check widgets that have been flagged as dirty but note
1779 * that new widgets may be added to the dirty list while we
1780 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001781 */
Mark Browndb432b42011-10-03 21:06:40 +01001782 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001783 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001784 }
1785
Mark Brownf9de6d72011-09-28 17:19:47 +01001786 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001787 switch (w->id) {
1788 case snd_soc_dapm_pre:
1789 case snd_soc_dapm_post:
1790 /* These widgets always need to be powered */
1791 break;
1792 default:
1793 list_del_init(&w->dirty);
1794 break;
1795 }
Mark Browndb432b42011-10-03 21:06:40 +01001796
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001797 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001798 d = w->dapm;
1799
1800 /* Supplies and micbiases only bring the
1801 * context up to STANDBY as unless something
1802 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001803 * generally don't require full power. Signal
1804 * generators are virtual pins and have no
1805 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001806 */
1807 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001808 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001809 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001810 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001811 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001812 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001813 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001814 case snd_soc_dapm_micbias:
1815 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1816 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1817 break;
1818 default:
1819 d->target_bias_level = SND_SOC_BIAS_ON;
1820 break;
1821 }
1822 }
1823
1824 }
1825
Mark Brown85a843c2011-09-21 21:29:47 +01001826 /* Force all contexts in the card to the same bias state if
1827 * they're not ground referenced.
1828 */
Mark Brown56fba412011-06-04 11:25:10 +01001829 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001830 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001831 if (d->target_bias_level > bias)
1832 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001833 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001834 if (!d->idle_bias_off)
1835 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001836
Mark Brownde02d072011-09-20 21:43:24 +01001837 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001838
Xiang Xiao17282ba2014-03-02 00:04:03 +08001839 /* Run card bias changes at first */
1840 dapm_pre_sequence_async(&card->dapm, 0);
1841 /* Run other bias changes in parallel */
1842 list_for_each_entry(d, &card->dapm_list, list) {
1843 if (d != &card->dapm)
1844 async_schedule_domain(dapm_pre_sequence_async, d,
1845 &async_domain);
1846 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001847 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001848
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001849 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001850 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001851 }
1852
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001853 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001854 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001855 }
1856
Mark Brown6d3ddc82009-05-16 17:47:29 +01001857 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001858 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001859
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001860 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001861
Mark Brown6d3ddc82009-05-16 17:47:29 +01001862 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001863 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001864
Mark Brown9d0624a2011-02-18 11:49:43 -08001865 /* Run all the bias changes in parallel */
Xiang Xiao17282ba2014-03-02 00:04:03 +08001866 list_for_each_entry(d, &card->dapm_list, list) {
1867 if (d != &card->dapm)
1868 async_schedule_domain(dapm_post_sequence_async, d,
1869 &async_domain);
1870 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001871 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08001872 /* Run card bias changes at last */
1873 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01001874
Liam Girdwood8078d872012-02-15 15:15:35 +00001875 /* do we need to notify any clients that DAPM event is complete */
1876 list_for_each_entry(d, &card->dapm_list, list) {
1877 if (d->stream_event)
1878 d->stream_event(d, event);
1879 }
1880
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001881 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001882 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001883 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001884
Mark Brown84e90932010-11-04 00:07:02 -04001885 trace_snd_soc_dapm_done(card);
1886
Mark Brown42aa3412009-03-01 19:21:10 +00001887 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001888}
1889
Mark Brown79fb9382009-08-21 16:38:13 +01001890#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001891static ssize_t dapm_widget_power_read_file(struct file *file,
1892 char __user *user_buf,
1893 size_t count, loff_t *ppos)
1894{
1895 struct snd_soc_dapm_widget *w = file->private_data;
1896 char *buf;
1897 int in, out;
1898 ssize_t ret;
1899 struct snd_soc_dapm_path *p = NULL;
1900
1901 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1902 if (!buf)
1903 return -ENOMEM;
1904
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001905 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001906 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001907 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001908 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001909
Mark Brownf13ebad2012-03-03 18:01:01 +00001910 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1911 w->name, w->power ? "On" : "Off",
1912 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001913
Mark Brownd033c362009-12-04 15:25:56 +00001914 if (w->reg >= 0)
1915 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001916 " - R%d(0x%x) mask 0x%x",
1917 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001918
1919 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1920
Mark Brown3eef08b2009-09-14 16:49:00 +01001921 if (w->sname)
1922 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1923 w->sname,
1924 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001925
1926 list_for_each_entry(p, &w->sources, list_sink) {
Takashi Iwaiff186202013-10-28 14:21:49 +01001927 if (p->connected && !p->connected(w, p->source))
Mark Brown215edda2009-09-08 18:59:05 +01001928 continue;
1929
Mark Brown79fb9382009-08-21 16:38:13 +01001930 if (p->connect)
1931 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001932 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001933 p->name ? p->name : "static",
1934 p->source->name);
1935 }
1936 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001937 if (p->connected && !p->connected(w, p->sink))
1938 continue;
1939
Mark Brown79fb9382009-08-21 16:38:13 +01001940 if (p->connect)
1941 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001942 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001943 p->name ? p->name : "static",
1944 p->sink->name);
1945 }
1946
1947 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1948
1949 kfree(buf);
1950 return ret;
1951}
1952
1953static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001954 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001955 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001956 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001957};
1958
Mark Brownef49e4f2011-04-04 20:48:13 +09001959static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1960 size_t count, loff_t *ppos)
1961{
1962 struct snd_soc_dapm_context *dapm = file->private_data;
1963 char *level;
1964
1965 switch (dapm->bias_level) {
1966 case SND_SOC_BIAS_ON:
1967 level = "On\n";
1968 break;
1969 case SND_SOC_BIAS_PREPARE:
1970 level = "Prepare\n";
1971 break;
1972 case SND_SOC_BIAS_STANDBY:
1973 level = "Standby\n";
1974 break;
1975 case SND_SOC_BIAS_OFF:
1976 level = "Off\n";
1977 break;
1978 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001979 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09001980 level = "Unknown\n";
1981 break;
1982 }
1983
1984 return simple_read_from_buffer(user_buf, count, ppos, level,
1985 strlen(level));
1986}
1987
1988static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001989 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001990 .read = dapm_bias_read_file,
1991 .llseek = default_llseek,
1992};
1993
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001994void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1995 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001996{
Mark Brown79fb9382009-08-21 16:38:13 +01001997 struct dentry *d;
1998
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001999 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2000
2001 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00002002 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002003 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002004 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002005 }
Mark Brown79fb9382009-08-21 16:38:13 +01002006
Mark Brownef49e4f2011-04-04 20:48:13 +09002007 d = debugfs_create_file("bias_level", 0444,
2008 dapm->debugfs_dapm, dapm,
2009 &dapm_bias_fops);
2010 if (!d)
2011 dev_warn(dapm->dev,
2012 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002013}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002014
2015static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2016{
2017 struct snd_soc_dapm_context *dapm = w->dapm;
2018 struct dentry *d;
2019
2020 if (!dapm->debugfs_dapm || !w->name)
2021 return;
2022
2023 d = debugfs_create_file(w->name, 0444,
2024 dapm->debugfs_dapm, w,
2025 &dapm_widget_power_fops);
2026 if (!d)
2027 dev_warn(w->dapm->dev,
2028 "ASoC: Failed to create %s debugfs file\n",
2029 w->name);
2030}
2031
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002032static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2033{
2034 debugfs_remove_recursive(dapm->debugfs_dapm);
2035}
2036
Mark Brown79fb9382009-08-21 16:38:13 +01002037#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002038void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2039 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002040{
2041}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002042
2043static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2044{
2045}
2046
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002047static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2048{
2049}
2050
Mark Brown79fb9382009-08-21 16:38:13 +01002051#endif
2052
Richard Purdie2b97eab2006-10-06 18:32:18 +02002053/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002054static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002055 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002056{
2057 struct snd_soc_dapm_path *path;
2058 int found = 0;
2059
Mark Brownf9fa2b12014-03-06 16:49:11 +08002060 lockdep_assert_held(&card->dapm_mutex);
2061
Richard Purdie2b97eab2006-10-06 18:32:18 +02002062 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002063 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Zhaocb01e2b2008-10-07 08:05:20 +08002064 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02002065 continue;
2066
2067 found = 1;
2068 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01002069 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002070 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01002071 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01002072 } else {
2073 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01002074 dapm_mark_dirty(path->source,
2075 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002076 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01002077 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002078 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002079 }
2080
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002081 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002082 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002083
Liam Girdwood618dae12012-04-25 12:12:51 +01002084 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002085}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002086
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002087int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002088 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2089 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002090{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002091 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002092 int ret;
2093
Liam Girdwood3cd04342012-03-09 12:02:08 +00002094 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002095 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002096 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002097 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002098 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002099 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002100 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002101 return ret;
2102}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002103EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002104
Milan plzik1b075e32008-01-10 14:39:46 +01002105/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002106static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002107 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002108{
2109 struct snd_soc_dapm_path *path;
2110 int found = 0;
2111
Mark Brownf9fa2b12014-03-06 16:49:11 +08002112 lockdep_assert_held(&card->dapm_mutex);
2113
Richard Purdie2b97eab2006-10-06 18:32:18 +02002114 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002115 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002116 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002117 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002118 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002119 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002120 }
2121
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002122 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002123 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002124
Liam Girdwood618dae12012-04-25 12:12:51 +01002125 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002126}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002127
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002128int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002129 struct snd_kcontrol *kcontrol, int connect,
2130 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002131{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002132 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002133 int ret;
2134
Liam Girdwood3cd04342012-03-09 12:02:08 +00002135 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002136 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002137 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002138 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002139 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002140 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002141 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002142 return ret;
2143}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002144EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002145
2146/* show dapm widget status in sys fs */
2147static ssize_t dapm_widget_show(struct device *dev,
2148 struct device_attribute *attr, char *buf)
2149{
Mark Brown36ae1a92012-01-06 17:12:45 -08002150 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002151 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002152 struct snd_soc_dapm_widget *w;
2153 int count = 0;
2154 char *state = "not set";
2155
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002156 list_for_each_entry(w, &codec->card->widgets, list) {
2157 if (w->dapm != &codec->dapm)
2158 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002159
2160 /* only display widgets that burnm power */
2161 switch (w->id) {
2162 case snd_soc_dapm_hp:
2163 case snd_soc_dapm_mic:
2164 case snd_soc_dapm_spk:
2165 case snd_soc_dapm_line:
2166 case snd_soc_dapm_micbias:
2167 case snd_soc_dapm_dac:
2168 case snd_soc_dapm_adc:
2169 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002170 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002171 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002172 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002173 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002174 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002175 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002176 if (w->name)
2177 count += sprintf(buf + count, "%s: %s\n",
2178 w->name, w->power ? "On":"Off");
2179 break;
2180 default:
2181 break;
2182 }
2183 }
2184
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002185 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002186 case SND_SOC_BIAS_ON:
2187 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002188 break;
Mark Brown0be98982008-05-19 12:31:28 +02002189 case SND_SOC_BIAS_PREPARE:
2190 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002191 break;
Mark Brown0be98982008-05-19 12:31:28 +02002192 case SND_SOC_BIAS_STANDBY:
2193 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002194 break;
Mark Brown0be98982008-05-19 12:31:28 +02002195 case SND_SOC_BIAS_OFF:
2196 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002197 break;
2198 }
2199 count += sprintf(buf + count, "PM State: %s\n", state);
2200
2201 return count;
2202}
2203
2204static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2205
2206int snd_soc_dapm_sys_add(struct device *dev)
2207{
Troy Kisky12ef1932008-10-13 17:42:14 -07002208 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002209}
2210
2211static void snd_soc_dapm_sys_remove(struct device *dev)
2212{
Mark Brownaef90842009-05-16 17:53:16 +01002213 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002214}
2215
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002216static void dapm_free_path(struct snd_soc_dapm_path *path)
2217{
2218 list_del(&path->list_sink);
2219 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002220 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002221 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002222 kfree(path);
2223}
2224
Richard Purdie2b97eab2006-10-06 18:32:18 +02002225/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002226static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002227{
2228 struct snd_soc_dapm_widget *w, *next_w;
2229 struct snd_soc_dapm_path *p, *next_p;
2230
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002231 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2232 if (w->dapm != dapm)
2233 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002234 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002235 /*
2236 * remove source and sink paths associated to this widget.
2237 * While removing the path, remove reference to it from both
2238 * source and sink widgets so that path is removed only once.
2239 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002240 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2241 dapm_free_path(p);
2242
2243 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2244 dapm_free_path(p);
2245
Stephen Warrenfad59882011-04-28 17:37:59 -06002246 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002247 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002248 kfree(w);
2249 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002250}
2251
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002252static struct snd_soc_dapm_widget *dapm_find_widget(
2253 struct snd_soc_dapm_context *dapm, const char *pin,
2254 bool search_other_contexts)
2255{
2256 struct snd_soc_dapm_widget *w;
2257 struct snd_soc_dapm_widget *fallback = NULL;
2258
2259 list_for_each_entry(w, &dapm->card->widgets, list) {
2260 if (!strcmp(w->name, pin)) {
2261 if (w->dapm == dapm)
2262 return w;
2263 else
2264 fallback = w;
2265 }
2266 }
2267
2268 if (search_other_contexts)
2269 return fallback;
2270
2271 return NULL;
2272}
2273
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002274static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002275 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002276{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002277 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002278
Mark Brownf9fa2b12014-03-06 16:49:11 +08002279 dapm_assert_locked(dapm);
2280
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002281 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002282 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002283 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002284 }
2285
Mark Brown1a8b2d92012-02-16 11:50:07 -08002286 if (w->connected != status)
2287 dapm_mark_dirty(w, "pin configuration");
2288
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002289 w->connected = status;
2290 if (status == 0)
2291 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002292
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002293 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002294}
2295
Richard Purdie2b97eab2006-10-06 18:32:18 +02002296/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002297 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2298 * @dapm: DAPM context
2299 *
2300 * Walks all dapm audio paths and powers widgets according to their
2301 * stream or path usage.
2302 *
2303 * Requires external locking.
2304 *
2305 * Returns 0 for success.
2306 */
2307int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2308{
2309 /*
2310 * Suppress early reports (eg, jacks syncing their state) to avoid
2311 * silly DAPM runs during card startup.
2312 */
2313 if (!dapm->card || !dapm->card->instantiated)
2314 return 0;
2315
2316 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2317}
2318EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2319
2320/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002321 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002322 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002323 *
2324 * Walks all dapm audio paths and powers widgets according to their
2325 * stream or path usage.
2326 *
2327 * Returns 0 for success.
2328 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002329int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002330{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002331 int ret;
2332
Liam Girdwood3cd04342012-03-09 12:02:08 +00002333 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002334 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002335 mutex_unlock(&dapm->card->dapm_mutex);
2336 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002337}
Liam Girdwooda5302182008-07-07 13:35:17 +01002338EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002339
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002340static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2341 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2342 const char *control,
2343 int (*connected)(struct snd_soc_dapm_widget *source,
2344 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002345{
2346 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002347 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002348
2349 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2350 if (!path)
2351 return -ENOMEM;
2352
2353 path->source = wsource;
2354 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002355 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002356 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002357 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002358 INIT_LIST_HEAD(&path->list_source);
2359 INIT_LIST_HEAD(&path->list_sink);
2360
2361 /* check for external widgets */
2362 if (wsink->id == snd_soc_dapm_input) {
2363 if (wsource->id == snd_soc_dapm_micbias ||
2364 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002365 wsource->id == snd_soc_dapm_line ||
2366 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002367 wsink->ext = 1;
2368 }
2369 if (wsource->id == snd_soc_dapm_output) {
2370 if (wsink->id == snd_soc_dapm_spk ||
2371 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002372 wsink->id == snd_soc_dapm_line ||
2373 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002374 wsource->ext = 1;
2375 }
2376
Lars-Peter Clausen34742cb2013-08-27 15:50:54 +02002377 dapm_mark_dirty(wsource, "Route added");
2378 dapm_mark_dirty(wsink, "Route added");
2379
Richard Purdie2b97eab2006-10-06 18:32:18 +02002380 /* connect static paths */
2381 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002382 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002383 list_add(&path->list_sink, &wsink->sources);
2384 list_add(&path->list_source, &wsource->sinks);
2385 path->connect = 1;
2386 return 0;
2387 }
2388
2389 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002390 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002391 case snd_soc_dapm_adc:
2392 case snd_soc_dapm_dac:
2393 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002394 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002395 case snd_soc_dapm_input:
2396 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002397 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002398 case snd_soc_dapm_micbias:
2399 case snd_soc_dapm_vmid:
2400 case snd_soc_dapm_pre:
2401 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002402 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002403 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002404 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002405 case snd_soc_dapm_aif_in:
2406 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002407 case snd_soc_dapm_dai_in:
2408 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002409 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002410 case snd_soc_dapm_kcontrol:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002411 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002412 list_add(&path->list_sink, &wsink->sources);
2413 list_add(&path->list_source, &wsource->sinks);
2414 path->connect = 1;
2415 return 0;
2416 case snd_soc_dapm_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002417 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002418 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002419 if (ret != 0)
2420 goto err;
2421 break;
2422 case snd_soc_dapm_switch:
2423 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002424 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002425 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002426 if (ret != 0)
2427 goto err;
2428 break;
2429 case snd_soc_dapm_hp:
2430 case snd_soc_dapm_mic:
2431 case snd_soc_dapm_line:
2432 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002433 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002434 list_add(&path->list_sink, &wsink->sources);
2435 list_add(&path->list_source, &wsource->sinks);
2436 path->connect = 0;
2437 return 0;
2438 }
Mark Brownfabd0382012-07-05 17:20:06 +01002439
Richard Purdie2b97eab2006-10-06 18:32:18 +02002440 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002441err:
2442 kfree(path);
2443 return ret;
2444}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002445
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002446static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002447 const struct snd_soc_dapm_route *route,
2448 unsigned int is_prefixed)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002449{
2450 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2451 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2452 const char *sink;
2453 const char *source;
2454 char prefixed_sink[80];
2455 char prefixed_source[80];
2456 int ret;
2457
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002458 if (dapm->codec && dapm->codec->name_prefix && !is_prefixed) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002459 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2460 dapm->codec->name_prefix, route->sink);
2461 sink = prefixed_sink;
2462 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2463 dapm->codec->name_prefix, route->source);
2464 source = prefixed_source;
2465 } else {
2466 sink = route->sink;
2467 source = route->source;
2468 }
2469
2470 /*
2471 * find src and dest widgets over all widgets but favor a widget from
2472 * current DAPM context
2473 */
2474 list_for_each_entry(w, &dapm->card->widgets, list) {
2475 if (!wsink && !(strcmp(w->name, sink))) {
2476 wtsink = w;
2477 if (w->dapm == dapm)
2478 wsink = w;
2479 continue;
2480 }
2481 if (!wsource && !(strcmp(w->name, source))) {
2482 wtsource = w;
2483 if (w->dapm == dapm)
2484 wsource = w;
2485 }
2486 }
2487 /* use widget from another DAPM context if not found from this */
2488 if (!wsink)
2489 wsink = wtsink;
2490 if (!wsource)
2491 wsource = wtsource;
2492
2493 if (wsource == NULL) {
2494 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2495 route->source);
2496 return -ENODEV;
2497 }
2498 if (wsink == NULL) {
2499 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2500 route->sink);
2501 return -ENODEV;
2502 }
2503
2504 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2505 route->connected);
2506 if (ret)
2507 goto err;
2508
2509 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002510err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002511 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002512 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002513 return ret;
2514}
Mark Brown105f1c22008-05-13 14:52:19 +02002515
Mark Brownefcc3c62012-07-05 17:24:19 +01002516static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2517 const struct snd_soc_dapm_route *route)
2518{
2519 struct snd_soc_dapm_path *path, *p;
2520 const char *sink;
2521 const char *source;
2522 char prefixed_sink[80];
2523 char prefixed_source[80];
2524
2525 if (route->control) {
2526 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002527 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002528 return -EINVAL;
2529 }
2530
2531 if (dapm->codec && dapm->codec->name_prefix) {
2532 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2533 dapm->codec->name_prefix, route->sink);
2534 sink = prefixed_sink;
2535 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2536 dapm->codec->name_prefix, route->source);
2537 source = prefixed_source;
2538 } else {
2539 sink = route->sink;
2540 source = route->source;
2541 }
2542
2543 path = NULL;
2544 list_for_each_entry(p, &dapm->card->paths, list) {
2545 if (strcmp(p->source->name, source) != 0)
2546 continue;
2547 if (strcmp(p->sink->name, sink) != 0)
2548 continue;
2549 path = p;
2550 break;
2551 }
2552
2553 if (path) {
2554 dapm_mark_dirty(path->source, "Route removed");
2555 dapm_mark_dirty(path->sink, "Route removed");
2556
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002557 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002558 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002559 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002560 source, sink);
2561 }
2562
2563 return 0;
2564}
2565
Mark Brown105f1c22008-05-13 14:52:19 +02002566/**
Mark Brown105f1c22008-05-13 14:52:19 +02002567 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002568 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002569 * @route: audio routes
2570 * @num: number of routes
2571 *
2572 * Connects 2 dapm widgets together via a named audio path. The sink is
2573 * the widget receiving the audio signal, whilst the source is the sender
2574 * of the audio signal.
2575 *
2576 * Returns 0 for success else error. On error all resources can be freed
2577 * with a call to snd_soc_card_free().
2578 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002579int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002580 const struct snd_soc_dapm_route *route, int num)
2581{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002582 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002583
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002584 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002585 for (i = 0; i < num; i++) {
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002586 r = snd_soc_dapm_add_route(dapm, route, false);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002587 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002588 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2589 route->source,
2590 route->control ? route->control : "direct",
2591 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002592 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002593 }
2594 route++;
2595 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002596 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002597
Dan Carpenter60884c22012-04-13 22:25:43 +03002598 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002599}
2600EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2601
Mark Brownefcc3c62012-07-05 17:24:19 +01002602/**
2603 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2604 * @dapm: DAPM context
2605 * @route: audio routes
2606 * @num: number of routes
2607 *
2608 * Removes routes from the DAPM context.
2609 */
2610int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2611 const struct snd_soc_dapm_route *route, int num)
2612{
2613 int i, ret = 0;
2614
2615 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2616 for (i = 0; i < num; i++) {
2617 snd_soc_dapm_del_route(dapm, route);
2618 route++;
2619 }
2620 mutex_unlock(&dapm->card->dapm_mutex);
2621
2622 return ret;
2623}
2624EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2625
Mark Brownbf3a9e12011-06-13 16:42:29 +01002626static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2627 const struct snd_soc_dapm_route *route)
2628{
2629 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2630 route->source,
2631 true);
2632 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2633 route->sink,
2634 true);
2635 struct snd_soc_dapm_path *path;
2636 int count = 0;
2637
2638 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002639 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002640 route->source);
2641 return -ENODEV;
2642 }
2643
2644 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002645 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002646 route->sink);
2647 return -ENODEV;
2648 }
2649
2650 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002651 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002652 route->source, route->sink);
2653
2654 list_for_each_entry(path, &source->sinks, list_source) {
2655 if (path->sink == sink) {
2656 path->weak = 1;
2657 count++;
2658 }
2659 }
2660
2661 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002662 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002663 route->source, route->sink);
2664 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002665 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002666 count, route->source, route->sink);
2667
2668 return 0;
2669}
2670
2671/**
2672 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2673 * @dapm: DAPM context
2674 * @route: audio routes
2675 * @num: number of routes
2676 *
2677 * Mark existing routes matching those specified in the passed array
2678 * as being weak, meaning that they are ignored for the purpose of
2679 * power decisions. The main intended use case is for sidetone paths
2680 * which couple audio between other independent paths if they are both
2681 * active in order to make the combination work better at the user
2682 * level but which aren't intended to be "used".
2683 *
2684 * Note that CODEC drivers should not use this as sidetone type paths
2685 * can frequently also be used as bypass paths.
2686 */
2687int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2688 const struct snd_soc_dapm_route *route, int num)
2689{
2690 int i, err;
2691 int ret = 0;
2692
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002693 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002694 for (i = 0; i < num; i++) {
2695 err = snd_soc_dapm_weak_route(dapm, route);
2696 if (err)
2697 ret = err;
2698 route++;
2699 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002700 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002701
2702 return ret;
2703}
2704EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2705
Mark Brown105f1c22008-05-13 14:52:19 +02002706/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002707 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002708 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002709 *
2710 * Checks the codec for any new dapm widgets and creates them if found.
2711 *
2712 * Returns 0 for success.
2713 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002714int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002715{
2716 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002717 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002718
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002719 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002720
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002721 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002722 {
2723 if (w->new)
2724 continue;
2725
Stephen Warrenfad59882011-04-28 17:37:59 -06002726 if (w->num_kcontrols) {
2727 w->kcontrols = kzalloc(w->num_kcontrols *
2728 sizeof(struct snd_kcontrol *),
2729 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002730 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002731 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002732 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002733 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002734 }
2735
Richard Purdie2b97eab2006-10-06 18:32:18 +02002736 switch(w->id) {
2737 case snd_soc_dapm_switch:
2738 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002739 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002740 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002741 break;
2742 case snd_soc_dapm_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002743 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002744 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002745 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002746 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002747 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002748 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002749 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002750 break;
2751 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002752
2753 /* Read the initial power state from the device */
2754 if (w->reg >= 0) {
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08002755 soc_widget_read(w, w->reg, &val);
2756 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002757 val &= w->mask;
2758 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002759 w->power = 1;
2760 }
2761
Richard Purdie2b97eab2006-10-06 18:32:18 +02002762 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002763
Mark Brown7508b122011-10-05 12:09:12 +01002764 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002765 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002766 }
2767
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002768 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2769 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002770 return 0;
2771}
2772EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2773
2774/**
2775 * snd_soc_dapm_get_volsw - dapm mixer get callback
2776 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002777 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002778 *
2779 * Callback to get the value of a dapm mixer control.
2780 *
2781 * Returns 0 for success.
2782 */
2783int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2784 struct snd_ctl_elem_value *ucontrol)
2785{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002786 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002787 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002788 struct soc_mixer_control *mc =
2789 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002790 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002791 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002792 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002793 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002794 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002795 unsigned int val;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002796
2797 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002798 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002799 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002800 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002801
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002802 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002803 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002804 val = (snd_soc_read(codec, reg) >> shift) & mask;
2805 else
2806 val = dapm_kcontrol_get_value(kcontrol);
2807 mutex_unlock(&card->dapm_mutex);
2808
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002809 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002810 ucontrol->value.integer.value[0] = max - val;
2811 else
2812 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002813
2814 return 0;
2815}
2816EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2817
2818/**
2819 * snd_soc_dapm_put_volsw - dapm mixer set callback
2820 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002821 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002822 *
2823 * Callback to set the value of a dapm mixer control.
2824 *
2825 * Returns 0 for success.
2826 */
2827int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2828 struct snd_ctl_elem_value *ucontrol)
2829{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002830 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002831 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002832 struct soc_mixer_control *mc =
2833 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002834 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002835 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002836 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002837 unsigned int mask = (1 << fls(max)) - 1;
2838 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002839 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002840 int connect, change;
2841 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002842 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002843
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002844 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002845 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002846 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002847 kcontrol->id.name);
2848
Richard Purdie2b97eab2006-10-06 18:32:18 +02002849 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002850 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002851
2852 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002853 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002854
Liam Girdwood3cd04342012-03-09 12:02:08 +00002855 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002856
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002857 change = dapm_kcontrol_set_value(kcontrol, val);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002858
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002859 if (reg != SND_SOC_NOPM) {
2860 mask = mask << shift;
2861 val = val << shift;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002862
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002863 change = snd_soc_test_bits(codec, reg, mask, val);
2864 }
2865
Mark Brown97404f22010-12-14 16:13:57 +00002866 if (change) {
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002867 if (reg != SND_SOC_NOPM) {
2868 update.kcontrol = kcontrol;
2869 update.reg = reg;
2870 update.mask = mask;
2871 update.val = val;
Mark Brown283375c2009-12-07 18:09:03 +00002872
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002873 card->update = &update;
2874 }
Mark Brown97404f22010-12-14 16:13:57 +00002875
Nenghua Cao52765972013-12-13 20:13:49 +08002876 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002877
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002878 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002879 }
2880
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002881 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002882
2883 if (ret > 0)
2884 soc_dpcm_runtime_update(card);
2885
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002886 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002887}
2888EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2889
2890/**
2891 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2892 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002893 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002894 *
2895 * Callback to get the value of a dapm enumerated double mixer control.
2896 *
2897 * Returns 0 for success.
2898 */
2899int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2900 struct snd_ctl_elem_value *ucontrol)
2901{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002902 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002903 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002904 unsigned int reg_val, val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002905
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002906 if (e->reg != SND_SOC_NOPM)
2907 reg_val = snd_soc_read(codec, e->reg);
2908 else
2909 reg_val = dapm_kcontrol_get_value(kcontrol);
2910
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002911 val = (reg_val >> e->shift_l) & e->mask;
2912 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
2913 if (e->shift_l != e->shift_r) {
2914 val = (reg_val >> e->shift_r) & e->mask;
2915 val = snd_soc_enum_val_to_item(e, val);
2916 ucontrol->value.enumerated.item[1] = val;
2917 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002918
2919 return 0;
2920}
2921EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2922
2923/**
2924 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2925 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002926 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002927 *
2928 * Callback to set the value of a dapm enumerated double mixer control.
2929 *
2930 * Returns 0 for success.
2931 */
2932int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2933 struct snd_ctl_elem_value *ucontrol)
2934{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002935 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002936 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002937 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002938 unsigned int *item = ucontrol->value.enumerated.item;
2939 unsigned int val, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002940 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002941 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002942 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002943
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002944 if (item[0] >= e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002945 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002946
2947 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002948 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002949 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002950 if (item[1] > e->items)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002951 return -EINVAL;
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002952 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002953 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002954 }
2955
Liam Girdwood3cd04342012-03-09 12:02:08 +00002956 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002957
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002958 if (e->reg != SND_SOC_NOPM)
2959 change = snd_soc_test_bits(codec, e->reg, mask, val);
2960 else
2961 change = dapm_kcontrol_set_value(kcontrol, val);
Mark Brown97404f22010-12-14 16:13:57 +00002962
Richard Purdie2b97eab2006-10-06 18:32:18 +02002963 if (change) {
Lars-Peter Clausen236aaa62014-02-28 08:31:11 +01002964 if (e->reg != SND_SOC_NOPM) {
2965 update.kcontrol = kcontrol;
2966 update.reg = e->reg;
2967 update.mask = mask;
2968 update.val = val;
2969 card->update = &update;
2970 }
Mark Brown3a655772009-10-05 17:23:30 +01002971
Lars-Peter Clausen3727b492014-02-28 08:31:04 +01002972 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002973
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002974 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002975 }
2976
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002977 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002978
2979 if (ret > 0)
2980 soc_dpcm_runtime_update(card);
2981
Mark Brown97404f22010-12-14 16:13:57 +00002982 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002983}
2984EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2985
2986/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002987 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2988 *
2989 * @kcontrol: mixer control
2990 * @uinfo: control element information
2991 *
2992 * Callback to provide information about a pin switch control.
2993 */
2994int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2995 struct snd_ctl_elem_info *uinfo)
2996{
2997 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2998 uinfo->count = 1;
2999 uinfo->value.integer.min = 0;
3000 uinfo->value.integer.max = 1;
3001
3002 return 0;
3003}
3004EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3005
3006/**
3007 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3008 *
3009 * @kcontrol: mixer control
3010 * @ucontrol: Value
3011 */
3012int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3013 struct snd_ctl_elem_value *ucontrol)
3014{
Mark Brown48a8c392012-02-14 17:11:15 -08003015 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003016 const char *pin = (const char *)kcontrol->private_value;
3017
Liam Girdwood3cd04342012-03-09 12:02:08 +00003018 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003019
3020 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003021 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003022
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003023 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003024
3025 return 0;
3026}
3027EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3028
3029/**
3030 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3031 *
3032 * @kcontrol: mixer control
3033 * @ucontrol: Value
3034 */
3035int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3036 struct snd_ctl_elem_value *ucontrol)
3037{
Mark Brown48a8c392012-02-14 17:11:15 -08003038 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003039 const char *pin = (const char *)kcontrol->private_value;
3040
Mark Brown8b37dbd2009-02-28 21:14:20 +00003041 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003042 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003043 else
Mark Brown48a8c392012-02-14 17:11:15 -08003044 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003045
Mark Brown48a8c392012-02-14 17:11:15 -08003046 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003047 return 0;
3048}
3049EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3050
Mark Brown5ba06fc2012-02-16 11:07:13 -08003051static struct snd_soc_dapm_widget *
3052snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3053 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003054{
3055 struct snd_soc_dapm_widget *w;
Mark Brown62ea8742012-01-21 21:14:48 +00003056 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003057
3058 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003059 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003060
Mark Brown62ea8742012-01-21 21:14:48 +00003061 switch (w->id) {
3062 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003063 w->regulator = devm_regulator_get(dapm->dev, w->name);
3064 if (IS_ERR(w->regulator)) {
3065 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003066 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003067 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003068 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003069 }
Mark Brown8784c772013-01-10 19:33:47 +00003070
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003071 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003072 ret = regulator_allow_bypass(w->regulator, true);
3073 if (ret != 0)
3074 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003075 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003076 w->name, ret);
3077 }
Mark Brown62ea8742012-01-21 21:14:48 +00003078 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003079 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003080#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003081 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003082 if (IS_ERR(w->clk)) {
3083 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003084 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003085 w->name, ret);
3086 return NULL;
3087 }
Mark Brownec029952012-06-04 08:16:20 +01003088#else
3089 return NULL;
3090#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003091 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003092 default:
3093 break;
3094 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003095
Mark Brown88e8b9a2011-03-02 18:18:24 +00003096 if (dapm->codec && dapm->codec->name_prefix)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003097 w->name = kasprintf(GFP_KERNEL, "%s %s",
3098 dapm->codec->name_prefix, widget->name);
3099 else
3100 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3101
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003102 if (w->name == NULL) {
3103 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003104 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003105 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003106
Mark Brown7ca3a182011-10-08 14:04:50 +01003107 switch (w->id) {
3108 case snd_soc_dapm_switch:
3109 case snd_soc_dapm_mixer:
3110 case snd_soc_dapm_mixer_named_ctl:
3111 w->power_check = dapm_generic_check_power;
3112 break;
3113 case snd_soc_dapm_mux:
Mark Brown7ca3a182011-10-08 14:04:50 +01003114 w->power_check = dapm_generic_check_power;
3115 break;
Mark Brown46162742013-06-05 19:36:11 +01003116 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003117 w->power_check = dapm_adc_check_power;
3118 break;
Mark Brown46162742013-06-05 19:36:11 +01003119 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003120 w->power_check = dapm_dac_check_power;
3121 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003122 case snd_soc_dapm_adc:
3123 case snd_soc_dapm_aif_out:
3124 case snd_soc_dapm_dac:
3125 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003126 case snd_soc_dapm_pga:
3127 case snd_soc_dapm_out_drv:
3128 case snd_soc_dapm_input:
3129 case snd_soc_dapm_output:
3130 case snd_soc_dapm_micbias:
3131 case snd_soc_dapm_spk:
3132 case snd_soc_dapm_hp:
3133 case snd_soc_dapm_mic:
3134 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003135 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003136 w->power_check = dapm_generic_check_power;
3137 break;
3138 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003139 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003140 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003141 case snd_soc_dapm_kcontrol:
Mark Brown7ca3a182011-10-08 14:04:50 +01003142 w->power_check = dapm_supply_check_power;
3143 break;
3144 default:
3145 w->power_check = dapm_always_on_check_power;
3146 break;
3147 }
3148
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003149 w->dapm = dapm;
3150 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003151 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003152 INIT_LIST_HEAD(&w->sources);
3153 INIT_LIST_HEAD(&w->sinks);
3154 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003155 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003156 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003157
3158 /* machine layer set ups unconnected pins and insertions */
3159 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003160 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003161}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003162
3163/**
Mark Brown4ba13272008-05-13 14:51:19 +02003164 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003165 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003166 * @widget: widget array
3167 * @num: number of widgets
3168 *
3169 * Creates new DAPM controls based upon the templates.
3170 *
3171 * Returns 0 for success else error.
3172 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003173int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003174 const struct snd_soc_dapm_widget *widget,
3175 int num)
3176{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003177 struct snd_soc_dapm_widget *w;
3178 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003179 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003180
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003181 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003182 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003183 w = snd_soc_dapm_new_control(dapm, widget);
3184 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003185 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003186 "ASoC: Failed to create DAPM control %s\n",
3187 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003188 ret = -ENOMEM;
3189 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003190 }
Mark Brown4ba13272008-05-13 14:51:19 +02003191 widget++;
3192 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003193 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003194 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003195}
3196EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3197
Mark Brownc74184e2012-04-04 22:12:09 +01003198static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3199 struct snd_kcontrol *kcontrol, int event)
3200{
3201 struct snd_soc_dapm_path *source_p, *sink_p;
3202 struct snd_soc_dai *source, *sink;
3203 const struct snd_soc_pcm_stream *config = w->params;
3204 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003205 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003206 u64 fmt;
3207 int ret;
3208
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003209 if (WARN_ON(!config) ||
3210 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3211 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003212
3213 /* We only support a single source and sink, pick the first */
3214 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3215 list_sink);
3216 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3217 list_source);
3218
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003219 if (WARN_ON(!source_p || !sink_p) ||
3220 WARN_ON(!sink_p->source || !source_p->sink) ||
3221 WARN_ON(!source_p->source || !sink_p->sink))
3222 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003223
3224 source = source_p->source->priv;
3225 sink = sink_p->sink->priv;
3226
3227 /* Be a little careful as we don't want to overflow the mask array */
3228 if (config->formats) {
3229 fmt = ffs(config->formats) - 1;
3230 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003231 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003232 config->formats);
3233 fmt = 0;
3234 }
3235
3236 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003237 params = kzalloc(sizeof(*params), GFP_KERNEL);
3238 if (!params) {
3239 ret = -ENOMEM;
3240 goto out;
3241 }
3242 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003243
Mark Brown9747cec2012-04-26 19:12:21 +01003244 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003245 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003246 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003247 config->rate_max;
3248
Mark Brown9747cec2012-04-26 19:12:21 +01003249 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003250 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003251 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003252 = config->channels_max;
3253
3254 memset(&substream, 0, sizeof(substream));
3255
3256 switch (event) {
3257 case SND_SOC_DAPM_PRE_PMU:
3258 if (source->driver->ops && source->driver->ops->hw_params) {
3259 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3260 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003261 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003262 if (ret != 0) {
3263 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003264 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003265 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003266 }
3267 }
3268
3269 if (sink->driver->ops && sink->driver->ops->hw_params) {
3270 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003271 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003272 sink);
3273 if (ret != 0) {
3274 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003275 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003276 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003277 }
3278 }
3279 break;
3280
3281 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003282 ret = snd_soc_dai_digital_mute(sink, 0,
3283 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003284 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003285 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003286 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003287 break;
3288
3289 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003290 ret = snd_soc_dai_digital_mute(sink, 1,
3291 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003292 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003293 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003294 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003295 break;
3296
3297 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003298 WARN(1, "Unknown event %d\n", event);
Mark Brownc74184e2012-04-04 22:12:09 +01003299 return -EINVAL;
3300 }
3301
Mark Brown9747cec2012-04-26 19:12:21 +01003302out:
3303 kfree(params);
3304 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003305}
3306
3307int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3308 const struct snd_soc_pcm_stream *params,
3309 struct snd_soc_dapm_widget *source,
3310 struct snd_soc_dapm_widget *sink)
3311{
3312 struct snd_soc_dapm_route routes[2];
3313 struct snd_soc_dapm_widget template;
3314 struct snd_soc_dapm_widget *w;
3315 size_t len;
3316 char *link_name;
3317
3318 len = strlen(source->name) + strlen(sink->name) + 2;
3319 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3320 if (!link_name)
3321 return -ENOMEM;
3322 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3323
3324 memset(&template, 0, sizeof(template));
3325 template.reg = SND_SOC_NOPM;
3326 template.id = snd_soc_dapm_dai_link;
3327 template.name = link_name;
3328 template.event = snd_soc_dai_link_event;
3329 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3330 SND_SOC_DAPM_PRE_PMD;
3331
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003332 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003333
3334 w = snd_soc_dapm_new_control(&card->dapm, &template);
3335 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003336 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003337 link_name);
3338 return -ENOMEM;
3339 }
3340
3341 w->params = params;
3342
3343 memset(&routes, 0, sizeof(routes));
3344
3345 routes[0].source = source->name;
3346 routes[0].sink = link_name;
3347 routes[1].source = link_name;
3348 routes[1].sink = sink->name;
3349
3350 return snd_soc_dapm_add_routes(&card->dapm, routes,
3351 ARRAY_SIZE(routes));
3352}
3353
Mark Brown888df392012-02-16 19:37:51 -08003354int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3355 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003356{
Mark Brown888df392012-02-16 19:37:51 -08003357 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003358 struct snd_soc_dapm_widget *w;
3359
Mark Brown888df392012-02-16 19:37:51 -08003360 WARN_ON(dapm->dev != dai->dev);
3361
3362 memset(&template, 0, sizeof(template));
3363 template.reg = SND_SOC_NOPM;
3364
3365 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003366 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003367 template.name = dai->driver->playback.stream_name;
3368 template.sname = dai->driver->playback.stream_name;
3369
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003370 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003371 template.name);
3372
3373 w = snd_soc_dapm_new_control(dapm, &template);
3374 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003375 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003376 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003377 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003378 }
3379
3380 w->priv = dai;
3381 dai->playback_widget = w;
3382 }
3383
3384 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003385 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003386 template.name = dai->driver->capture.stream_name;
3387 template.sname = dai->driver->capture.stream_name;
3388
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003389 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003390 template.name);
3391
3392 w = snd_soc_dapm_new_control(dapm, &template);
3393 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003394 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003395 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003396 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003397 }
3398
3399 w->priv = dai;
3400 dai->capture_widget = w;
3401 }
3402
3403 return 0;
3404}
3405
3406int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3407{
3408 struct snd_soc_dapm_widget *dai_w, *w;
3409 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003410
3411 /* For each DAI widget... */
3412 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003413 switch (dai_w->id) {
3414 case snd_soc_dapm_dai_in:
3415 case snd_soc_dapm_dai_out:
3416 break;
3417 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003418 continue;
Mark Brown46162742013-06-05 19:36:11 +01003419 }
Mark Brown888df392012-02-16 19:37:51 -08003420
3421 dai = dai_w->priv;
3422
3423 /* ...find all widgets with the same stream and link them */
3424 list_for_each_entry(w, &card->widgets, list) {
3425 if (w->dapm != dai_w->dapm)
3426 continue;
3427
Mark Brown46162742013-06-05 19:36:11 +01003428 switch (w->id) {
3429 case snd_soc_dapm_dai_in:
3430 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003431 continue;
Mark Brown46162742013-06-05 19:36:11 +01003432 default:
3433 break;
3434 }
Mark Brown888df392012-02-16 19:37:51 -08003435
Russell King19c2c5f2013-08-04 20:24:03 +01003436 if (!w->sname || !strstr(w->sname, dai_w->name))
Mark Brown888df392012-02-16 19:37:51 -08003437 continue;
3438
3439 if (dai->driver->playback.stream_name &&
3440 strstr(w->sname,
3441 dai->driver->playback.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003442 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003443 dai->playback_widget->name, w->name);
Mark Brown888df392012-02-16 19:37:51 -08003444
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003445 snd_soc_dapm_add_path(w->dapm,
3446 dai->playback_widget, w, NULL, NULL);
Mark Brown888df392012-02-16 19:37:51 -08003447 }
3448
3449 if (dai->driver->capture.stream_name &&
3450 strstr(w->sname,
3451 dai->driver->capture.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003452 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003453 w->name, dai->capture_widget->name);
Mark Brown888df392012-02-16 19:37:51 -08003454
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003455 snd_soc_dapm_add_path(w->dapm, w,
3456 dai->capture_widget, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003457 }
3458 }
3459 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003460
Mark Brown888df392012-02-16 19:37:51 -08003461 return 0;
3462}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003463
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003464void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3465{
3466 struct snd_soc_pcm_runtime *rtd = card->rtd;
3467 struct snd_soc_dai *cpu_dai, *codec_dai;
3468 struct snd_soc_dapm_route r;
3469 int i;
3470
3471 memset(&r, 0, sizeof(r));
3472
3473 /* for each BE DAI link... */
3474 for (i = 0; i < card->num_rtd; i++) {
3475 rtd = &card->rtd[i];
3476 cpu_dai = rtd->cpu_dai;
3477 codec_dai = rtd->codec_dai;
3478
3479 /* dynamic FE links have no fixed DAI mapping */
3480 if (rtd->dai_link->dynamic)
3481 continue;
3482
3483 /* there is no point in connecting BE DAI links with dummies */
3484 if (snd_soc_dai_is_dummy(codec_dai) ||
3485 snd_soc_dai_is_dummy(cpu_dai))
3486 continue;
3487
3488 /* connect BE DAI playback if widgets are valid */
3489 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
3490 r.source = cpu_dai->playback_widget->name;
3491 r.sink = codec_dai->playback_widget->name;
3492 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3493 cpu_dai->codec->name, r.source,
3494 codec_dai->platform->name, r.sink);
3495
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08003496 snd_soc_dapm_add_route(&card->dapm, &r, true);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003497 }
3498
3499 /* connect BE DAI capture if widgets are valid */
3500 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
3501 r.source = codec_dai->capture_widget->name;
3502 r.sink = cpu_dai->capture_widget->name;
3503 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3504 codec_dai->codec->name, r.source,
3505 cpu_dai->platform->name, r.sink);
3506
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08003507 snd_soc_dapm_add_route(&card->dapm, &r, true);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003508 }
3509
3510 }
3511}
3512
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003513static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3514 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003515{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003516
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003517 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3518 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3519 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003520
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003521 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3522 w_cpu = cpu_dai->playback_widget;
3523 w_codec = codec_dai->playback_widget;
3524 } else {
3525 w_cpu = cpu_dai->capture_widget;
3526 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003527 }
3528
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003529 if (w_cpu) {
3530
3531 dapm_mark_dirty(w_cpu, "stream event");
3532
3533 switch (event) {
3534 case SND_SOC_DAPM_STREAM_START:
3535 w_cpu->active = 1;
3536 break;
3537 case SND_SOC_DAPM_STREAM_STOP:
3538 w_cpu->active = 0;
3539 break;
3540 case SND_SOC_DAPM_STREAM_SUSPEND:
3541 case SND_SOC_DAPM_STREAM_RESUME:
3542 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3543 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3544 break;
3545 }
3546 }
3547
3548 if (w_codec) {
3549
3550 dapm_mark_dirty(w_codec, "stream event");
3551
3552 switch (event) {
3553 case SND_SOC_DAPM_STREAM_START:
3554 w_codec->active = 1;
3555 break;
3556 case SND_SOC_DAPM_STREAM_STOP:
3557 w_codec->active = 0;
3558 break;
3559 case SND_SOC_DAPM_STREAM_SUSPEND:
3560 case SND_SOC_DAPM_STREAM_RESUME:
3561 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3562 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3563 break;
3564 }
3565 }
3566
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003567 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003568}
3569
3570/**
3571 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3572 * @rtd: PCM runtime data
3573 * @stream: stream name
3574 * @event: stream event
3575 *
3576 * Sends a stream event to the dapm core. The core then makes any
3577 * necessary widget power changes.
3578 *
3579 * Returns 0 for success else error.
3580 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003581void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3582 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003583{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003584 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003585
Liam Girdwood3cd04342012-03-09 12:02:08 +00003586 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003587 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003588 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003589}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003590
3591/**
Charles Keepax11391102014-02-18 15:22:14 +00003592 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3593 * @dapm: DAPM context
3594 * @pin: pin name
3595 *
3596 * Enables input/output pin and its parents or children widgets iff there is
3597 * a valid audio route and active audio stream.
3598 *
3599 * Requires external locking.
3600 *
3601 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3602 * do any widget power switching.
3603 */
3604int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3605 const char *pin)
3606{
3607 return snd_soc_dapm_set_pin(dapm, pin, 1);
3608}
3609EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3610
3611/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003612 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003613 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003614 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003615 *
Mark Brown74b8f952009-06-06 11:26:15 +01003616 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003617 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00003618 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003619 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3620 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003621 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003622int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003623{
Charles Keepax11391102014-02-18 15:22:14 +00003624 int ret;
3625
3626 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3627
3628 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3629
3630 mutex_unlock(&dapm->card->dapm_mutex);
3631
3632 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003633}
Liam Girdwooda5302182008-07-07 13:35:17 +01003634EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003635
3636/**
Charles Keepax11391102014-02-18 15:22:14 +00003637 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3638 * @dapm: DAPM context
3639 * @pin: pin name
3640 *
3641 * Enables input/output pin regardless of any other state. This is
3642 * intended for use with microphone bias supplies used in microphone
3643 * jack detection.
3644 *
3645 * Requires external locking.
3646 *
3647 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3648 * do any widget power switching.
3649 */
3650int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3651 const char *pin)
3652{
3653 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3654
3655 if (!w) {
3656 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3657 return -EINVAL;
3658 }
3659
3660 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3661 w->connected = 1;
3662 w->force = 1;
3663 dapm_mark_dirty(w, "force enable");
3664
3665 return 0;
3666}
3667EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3668
3669/**
Mark Brownda341832010-03-15 19:23:37 +00003670 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003671 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003672 * @pin: pin name
3673 *
3674 * Enables input/output pin regardless of any other state. This is
3675 * intended for use with microphone bias supplies used in microphone
3676 * jack detection.
3677 *
3678 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3679 * do any widget power switching.
3680 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003681int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3682 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003683{
Charles Keepax11391102014-02-18 15:22:14 +00003684 int ret;
Mark Brownda341832010-03-15 19:23:37 +00003685
Charles Keepax11391102014-02-18 15:22:14 +00003686 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00003687
Charles Keepax11391102014-02-18 15:22:14 +00003688 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09003689
Charles Keepax11391102014-02-18 15:22:14 +00003690 mutex_unlock(&dapm->card->dapm_mutex);
3691
3692 return ret;
Mark Brownda341832010-03-15 19:23:37 +00003693}
3694EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3695
3696/**
Charles Keepax11391102014-02-18 15:22:14 +00003697 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3698 * @dapm: DAPM context
3699 * @pin: pin name
3700 *
3701 * Disables input/output pin and its parents or children widgets.
3702 *
3703 * Requires external locking.
3704 *
3705 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3706 * do any widget power switching.
3707 */
3708int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3709 const char *pin)
3710{
3711 return snd_soc_dapm_set_pin(dapm, pin, 0);
3712}
3713EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3714
3715/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003716 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003717 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003718 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003719 *
Mark Brown74b8f952009-06-06 11:26:15 +01003720 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00003721 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003722 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3723 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003724 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003725int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3726 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003727{
Charles Keepax11391102014-02-18 15:22:14 +00003728 int ret;
3729
3730 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3731
3732 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3733
3734 mutex_unlock(&dapm->card->dapm_mutex);
3735
3736 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01003737}
3738EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3739
3740/**
Charles Keepax11391102014-02-18 15:22:14 +00003741 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3742 * @dapm: DAPM context
3743 * @pin: pin name
3744 *
3745 * Marks the specified pin as being not connected, disabling it along
3746 * any parent or child widgets. At present this is identical to
3747 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3748 * additional things such as disabling controls which only affect
3749 * paths through the pin.
3750 *
3751 * Requires external locking.
3752 *
3753 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3754 * do any widget power switching.
3755 */
3756int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3757 const char *pin)
3758{
3759 return snd_soc_dapm_set_pin(dapm, pin, 0);
3760}
3761EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3762
3763/**
Mark Brown5817b522008-09-24 11:23:11 +01003764 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003765 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003766 * @pin: pin name
3767 *
3768 * Marks the specified pin as being not connected, disabling it along
3769 * any parent or child widgets. At present this is identical to
3770 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3771 * additional things such as disabling controls which only affect
3772 * paths through the pin.
3773 *
3774 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3775 * do any widget power switching.
3776 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003777int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003778{
Charles Keepax11391102014-02-18 15:22:14 +00003779 int ret;
3780
3781 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3782
3783 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3784
3785 mutex_unlock(&dapm->card->dapm_mutex);
3786
3787 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01003788}
3789EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3790
3791/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003792 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003793 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003794 * @pin: audio signal pin endpoint (or start point)
3795 *
3796 * Get audio pin status - connected or disconnected.
3797 *
3798 * Returns 1 for connected otherwise 0.
3799 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003800int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3801 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003802{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003803 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003804
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003805 if (w)
3806 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003807
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003808 return 0;
3809}
Liam Girdwooda5302182008-07-07 13:35:17 +01003810EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003811
3812/**
Mark Brown1547aba2010-05-07 21:11:40 +01003813 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003814 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003815 * @pin: audio signal pin endpoint (or start point)
3816 *
3817 * Mark the given endpoint or pin as ignoring suspend. When the
3818 * system is disabled a path between two endpoints flagged as ignoring
3819 * suspend will not be disabled. The path must already be enabled via
3820 * normal means at suspend time, it will not be turned on if it was not
3821 * already enabled.
3822 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003823int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3824 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003825{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003826 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003827
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003828 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003829 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003830 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003831 }
3832
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003833 w->ignore_suspend = 1;
3834
3835 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003836}
3837EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3838
Stephen Warren16332812011-11-23 12:42:04 -07003839static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3840 struct snd_soc_dapm_widget *w)
3841{
3842 struct snd_soc_dapm_path *p;
3843
3844 list_for_each_entry(p, &card->paths, list) {
3845 if ((p->source == w) || (p->sink == w)) {
3846 dev_dbg(card->dev,
3847 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3848 p->source->name, p->source->id, p->source->dapm,
3849 p->sink->name, p->sink->id, p->sink->dapm);
3850
3851 /* Connected to something other than the codec */
3852 if (p->source->dapm != p->sink->dapm)
3853 return true;
3854 /*
3855 * Loopback connection from codec external pin to
3856 * codec external pin
3857 */
3858 if (p->sink->id == snd_soc_dapm_input) {
3859 switch (p->source->id) {
3860 case snd_soc_dapm_output:
3861 case snd_soc_dapm_micbias:
3862 return true;
3863 default:
3864 break;
3865 }
3866 }
3867 }
3868 }
3869
3870 return false;
3871}
3872
3873/**
3874 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3875 * @codec: The codec whose pins should be processed
3876 *
3877 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3878 * which are unused. Pins are used if they are connected externally to the
3879 * codec, whether that be to some other device, or a loop-back connection to
3880 * the codec itself.
3881 */
3882void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3883{
3884 struct snd_soc_card *card = codec->card;
3885 struct snd_soc_dapm_context *dapm = &codec->dapm;
3886 struct snd_soc_dapm_widget *w;
3887
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003888 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003889 &card->dapm, &codec->dapm);
3890
3891 list_for_each_entry(w, &card->widgets, list) {
3892 if (w->dapm != dapm)
3893 continue;
3894 switch (w->id) {
3895 case snd_soc_dapm_input:
3896 case snd_soc_dapm_output:
3897 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003898 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003899 w->name);
3900 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003901 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003902 "... Not in map; disabling\n");
3903 snd_soc_dapm_nc_pin(dapm, w->name);
3904 }
3905 break;
3906 default:
3907 break;
3908 }
3909 }
3910}
3911
Mark Brown1547aba2010-05-07 21:11:40 +01003912/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003913 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003914 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003915 *
3916 * Free all dapm widgets and resources.
3917 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003918void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003919{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003920 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003921 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003922 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003923 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003924}
3925EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3926
Xiang Xiao57996352014-03-02 00:04:02 +08003927static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003928{
Liam Girdwood01005a72012-07-06 16:57:05 +01003929 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003930 struct snd_soc_dapm_widget *w;
3931 LIST_HEAD(down_list);
3932 int powerdown = 0;
3933
Liam Girdwood01005a72012-07-06 16:57:05 +01003934 mutex_lock(&card->dapm_mutex);
3935
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003936 list_for_each_entry(w, &dapm->card->widgets, list) {
3937 if (w->dapm != dapm)
3938 continue;
Mark Brown51737472009-06-22 13:16:51 +01003939 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003940 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003941 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003942 powerdown = 1;
3943 }
3944 }
3945
3946 /* If there were no widgets to power down we're already in
3947 * standby.
3948 */
3949 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003950 if (dapm->bias_level == SND_SOC_BIAS_ON)
3951 snd_soc_dapm_set_bias_level(dapm,
3952 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003953 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003954 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3955 snd_soc_dapm_set_bias_level(dapm,
3956 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003957 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003958
3959 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003960}
Mark Brown51737472009-06-22 13:16:51 +01003961
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003962/*
3963 * snd_soc_dapm_shutdown - callback for system shutdown
3964 */
3965void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3966{
Xiang Xiao57996352014-03-02 00:04:02 +08003967 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003968
Xiang Xiao57996352014-03-02 00:04:02 +08003969 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08003970 if (dapm != &card->dapm) {
3971 soc_dapm_shutdown_dapm(dapm);
3972 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
3973 snd_soc_dapm_set_bias_level(dapm,
3974 SND_SOC_BIAS_OFF);
3975 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003976 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08003977
3978 soc_dapm_shutdown_dapm(&card->dapm);
3979 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3980 snd_soc_dapm_set_bias_level(&card->dapm,
3981 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01003982}
3983
Richard Purdie2b97eab2006-10-06 18:32:18 +02003984/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003985MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003986MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3987MODULE_LICENSE("GPL");