blob: 5c01ac1cfc0a2c898d6b2ff291a93b0c2e649f2b [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,
73 [snd_soc_dapm_virt_mux] = 6,
74 [snd_soc_dapm_value_mux] = 6,
75 [snd_soc_dapm_dac] = 7,
76 [snd_soc_dapm_switch] = 8,
77 [snd_soc_dapm_mixer] = 8,
78 [snd_soc_dapm_mixer_named_ctl] = 8,
79 [snd_soc_dapm_pga] = 9,
80 [snd_soc_dapm_adc] = 10,
81 [snd_soc_dapm_out_drv] = 11,
82 [snd_soc_dapm_hp] = 11,
83 [snd_soc_dapm_spk] = 11,
84 [snd_soc_dapm_line] = 11,
85 [snd_soc_dapm_kcontrol] = 12,
86 [snd_soc_dapm_post] = 13,
Richard Purdie2b97eab2006-10-06 18:32:18 +020087};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000088
Richard Purdie2b97eab2006-10-06 18:32:18 +020089static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010090 [snd_soc_dapm_pre] = 0,
Lars-Peter Clausen57295072013-08-05 11:27:31 +020091 [snd_soc_dapm_kcontrol] = 1,
92 [snd_soc_dapm_adc] = 2,
93 [snd_soc_dapm_hp] = 3,
94 [snd_soc_dapm_spk] = 3,
95 [snd_soc_dapm_line] = 3,
96 [snd_soc_dapm_out_drv] = 3,
Mark Brown38357ab2009-06-06 19:03:23 +010097 [snd_soc_dapm_pga] = 4,
Lars-Peter Clausenefc77e32013-06-14 13:16:50 +020098 [snd_soc_dapm_switch] = 5,
Mark Brown38357ab2009-06-06 19:03:23 +010099 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +0100100 [snd_soc_dapm_mixer] = 5,
101 [snd_soc_dapm_dac] = 6,
102 [snd_soc_dapm_mic] = 7,
103 [snd_soc_dapm_micbias] = 8,
104 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000105 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +0100106 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +0100107 [snd_soc_dapm_aif_in] = 10,
108 [snd_soc_dapm_aif_out] = 10,
Mark Brown46162742013-06-05 19:36:11 +0100109 [snd_soc_dapm_dai_in] = 10,
110 [snd_soc_dapm_dai_out] = 10,
Mark Brownc74184e2012-04-04 22:12:09 +0100111 [snd_soc_dapm_dai_link] = 11,
Mark Brownc74184e2012-04-04 22:12:09 +0100112 [snd_soc_dapm_supply] = 12,
Mark Brown1dd275b2013-10-09 13:56:37 +0100113 [snd_soc_dapm_clock_supply] = 13,
114 [snd_soc_dapm_regulator_supply] = 13,
115 [snd_soc_dapm_post] = 14,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200116};
117
Mark Brownf9fa2b12014-03-06 16:49:11 +0800118static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
119{
120 if (dapm->card && dapm->card->instantiated)
121 lockdep_assert_held(&dapm->card->dapm_mutex);
122}
123
Troy Kisky12ef1932008-10-13 17:42:14 -0700124static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +0100125{
126 if (pop_time)
127 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
128}
129
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200130static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100131{
132 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200133 char *buf;
134
135 if (!pop_time)
136 return;
137
138 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
139 if (buf == NULL)
140 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100141
142 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200143 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100144 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100145 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200146
147 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100148}
149
Mark Browndb432b42011-10-03 21:06:40 +0100150static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
151{
152 return !list_empty(&w->dirty);
153}
154
Mark Brown492c0a12014-03-06 16:15:48 +0800155static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100156{
Mark Brownf9fa2b12014-03-06 16:49:11 +0800157 dapm_assert_locked(w->dapm);
158
Mark Brown75c1f892011-10-04 22:28:08 +0100159 if (!dapm_dirty_widget(w)) {
160 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
161 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100162 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100163 }
Mark Browndb432b42011-10-03 21:06:40 +0100164}
165
Mark Browne2d32ff2012-08-31 17:38:32 -0700166void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
167{
168 struct snd_soc_card *card = dapm->card;
169 struct snd_soc_dapm_widget *w;
170
171 mutex_lock(&card->dapm_mutex);
172
173 list_for_each_entry(w, &card->widgets, list) {
174 switch (w->id) {
175 case snd_soc_dapm_input:
176 case snd_soc_dapm_output:
177 dapm_mark_dirty(w, "Rechecking inputs and outputs");
178 break;
179 default:
180 break;
181 }
182 }
183
184 mutex_unlock(&card->dapm_mutex);
185}
186EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
187
Richard Purdie2b97eab2006-10-06 18:32:18 +0200188/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100189static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200190 const struct snd_soc_dapm_widget *_widget)
191{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100192 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200193}
194
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200195struct dapm_kcontrol_data {
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200196 unsigned int value;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200197 struct snd_soc_dapm_widget *widget;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200198 struct list_head paths;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200199 struct snd_soc_dapm_widget_list *wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200200};
201
202static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
203 struct snd_kcontrol *kcontrol)
204{
205 struct dapm_kcontrol_data *data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200206 struct soc_mixer_control *mc;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200207
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200208 data = kzalloc(sizeof(*data), GFP_KERNEL);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200209 if (!data) {
210 dev_err(widget->dapm->dev,
211 "ASoC: can't allocate kcontrol data for %s\n",
212 widget->name);
213 return -ENOMEM;
214 }
215
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200216 INIT_LIST_HEAD(&data->paths);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200217
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200218 switch (widget->id) {
219 case snd_soc_dapm_switch:
220 case snd_soc_dapm_mixer:
221 case snd_soc_dapm_mixer_named_ctl:
222 mc = (struct soc_mixer_control *)kcontrol->private_value;
223
224 if (mc->autodisable) {
225 struct snd_soc_dapm_widget template;
226
227 memset(&template, 0, sizeof(template));
228 template.reg = mc->reg;
229 template.mask = (1 << fls(mc->max)) - 1;
230 template.shift = mc->shift;
231 if (mc->invert)
232 template.off_val = mc->max;
233 else
234 template.off_val = 0;
235 template.on_val = template.off_val;
236 template.id = snd_soc_dapm_kcontrol;
237 template.name = kcontrol->id.name;
238
Lars-Peter Clausen2daabd72013-08-30 17:39:33 +0200239 data->value = template.on_val;
240
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200241 data->widget = snd_soc_dapm_new_control(widget->dapm,
242 &template);
243 if (!data->widget) {
244 kfree(data);
245 return -ENOMEM;
246 }
247 }
248 break;
249 default:
250 break;
251 }
252
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200253 kcontrol->private_data = data;
254
255 return 0;
256}
257
258static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
259{
260 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200261 kfree(data->widget);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200262 kfree(data->wlist);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200263 kfree(data);
264}
265
266static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
267 const struct snd_kcontrol *kcontrol)
268{
269 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
270
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200271 return data->wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200272}
273
274static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
275 struct snd_soc_dapm_widget *widget)
276{
277 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200278 struct snd_soc_dapm_widget_list *new_wlist;
279 unsigned int n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200280
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200281 if (data->wlist)
282 n = data->wlist->num_widgets + 1;
283 else
284 n = 1;
285
286 new_wlist = krealloc(data->wlist,
287 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
288 if (!new_wlist)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200289 return -ENOMEM;
290
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200291 new_wlist->widgets[n - 1] = widget;
292 new_wlist->num_widgets = n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200293
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200294 data->wlist = new_wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200295
296 return 0;
297}
298
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200299static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
300 struct snd_soc_dapm_path *path)
301{
302 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
303
304 list_add_tail(&path->list_kcontrol, &data->paths);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200305
306 if (data->widget) {
307 snd_soc_dapm_add_path(data->widget->dapm, data->widget,
308 path->source, NULL, NULL);
309 }
310}
311
312static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
313{
314 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
315
316 if (!data->widget)
317 return true;
318
319 return data->widget->power;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200320}
321
322static struct list_head *dapm_kcontrol_get_path_list(
323 const struct snd_kcontrol *kcontrol)
324{
325 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
326
327 return &data->paths;
328}
329
330#define dapm_kcontrol_for_each_path(path, kcontrol) \
331 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
332 list_kcontrol)
333
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200334static unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
335{
336 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
337
338 return data->value;
339}
340
341static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
342 unsigned int value)
343{
344 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
345
346 if (data->value == value)
347 return false;
348
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200349 if (data->widget)
350 data->widget->on_val = value;
351
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200352 data->value = value;
353
354 return true;
355}
356
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200357/**
358 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
359 * @kcontrol: The kcontrol
360 */
361struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
362{
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200363 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->codec;
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200364}
365EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
366
Liam Girdwood6c120e12012-02-15 15:15:34 +0000367static void dapm_reset(struct snd_soc_card *card)
368{
369 struct snd_soc_dapm_widget *w;
370
Mark Brownf9fa2b12014-03-06 16:49:11 +0800371 lockdep_assert_held(&card->dapm_mutex);
372
Liam Girdwood6c120e12012-02-15 15:15:34 +0000373 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
374
375 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200376 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000377 w->power_checked = false;
378 w->inputs = -1;
379 w->outputs = -1;
380 }
381}
382
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800383static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg,
384 unsigned int *value)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100385{
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800386 if (w->codec) {
387 *value = snd_soc_read(w->codec, reg);
388 return 0;
389 } else if (w->platform) {
390 *value = snd_soc_platform_read(w->platform, reg);
391 return 0;
392 }
Liam Girdwoodb7950642011-07-04 22:10:52 +0100393
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000394 dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100395 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100396}
397
Lars-Peter Clausen30ac6b62014-02-13 14:14:21 +0100398static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg,
399 unsigned int val)
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100400{
401 if (w->codec)
402 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100403 else if (w->platform)
404 return snd_soc_platform_write(w->platform, reg, val);
405
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000406 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100407 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100408}
409
Liam Girdwood49575fb52012-03-06 18:16:19 +0000410static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
411{
Mark Browne06ab3b2012-03-06 23:58:22 +0000412 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000413 mutex_lock(&w->codec->mutex);
414 else if (w->platform)
415 mutex_lock(&w->platform->mutex);
416}
417
418static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
419{
Mark Browne06ab3b2012-03-06 23:58:22 +0000420 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000421 mutex_unlock(&w->codec->mutex);
422 else if (w->platform)
423 mutex_unlock(&w->platform->mutex);
424}
425
Mark Browneb270e92013-10-09 13:52:52 +0100426static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
427{
428 if (dapm->codec && dapm->codec->using_regmap)
429 regmap_async_complete(dapm->codec->control_data);
430}
431
Liam Girdwood49575fb52012-03-06 18:16:19 +0000432static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100433 unsigned short reg, unsigned int mask, unsigned int value)
434{
Mark Brown8a713da2011-12-03 12:33:55 +0000435 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100436 unsigned int old, new;
437 int ret;
438
Mark Brown8a713da2011-12-03 12:33:55 +0000439 if (w->codec && w->codec->using_regmap) {
Mark Browneb270e92013-10-09 13:52:52 +0100440 ret = regmap_update_bits_check_async(w->codec->control_data,
441 reg, mask, value,
442 &change);
Mark Brown8a713da2011-12-03 12:33:55 +0000443 if (ret != 0)
444 return ret;
445 } else {
Liam Girdwood49575fb52012-03-06 18:16:19 +0000446 soc_widget_lock(w);
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800447 ret = soc_widget_read(w, reg, &old);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000448 if (ret < 0) {
449 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100450 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000451 }
Mark Brown8a713da2011-12-03 12:33:55 +0000452
Mark Brown8a713da2011-12-03 12:33:55 +0000453 new = (old & ~mask) | (value & mask);
454 change = old != new;
455 if (change) {
456 ret = soc_widget_write(w, reg, new);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000457 if (ret < 0) {
458 soc_widget_unlock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000459 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000460 }
Mark Brown8a713da2011-12-03 12:33:55 +0000461 }
Liam Girdwood49575fb52012-03-06 18:16:19 +0000462 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100463 }
464
465 return change;
466}
467
Mark Brown452c5ea2009-05-17 21:41:23 +0100468/**
469 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800470 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100471 * @level: level to configure
472 *
473 * Configure the bias (power) levels for the SoC audio device.
474 *
475 * Returns 0 for success else error.
476 */
Mark Browned5a4c42011-02-18 11:12:42 -0800477static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200478 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100479{
Mark Browned5a4c42011-02-18 11:12:42 -0800480 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100481 int ret = 0;
482
Mark Brown84e90932010-11-04 00:07:02 -0400483 trace_snd_soc_bias_level_start(card, level);
484
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000485 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100486 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100487 if (ret != 0)
488 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100489
Mark Browncc4c6702011-06-06 19:03:34 +0100490 if (dapm->codec) {
491 if (dapm->codec->driver->set_bias_level)
492 ret = dapm->codec->driver->set_bias_level(dapm->codec,
493 level);
Mark Brownd8c3bb92012-08-23 18:10:42 +0100494 else
495 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100496 } else if (!card || dapm != &card->dapm) {
Liam Girdwood41231282012-07-06 16:56:16 +0100497 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100498 }
Liam Girdwood41231282012-07-06 16:56:16 +0100499
Mark Brown171ec6b2011-06-06 18:15:19 +0100500 if (ret != 0)
501 goto out;
502
503 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100504 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100505out:
Mark Brown84e90932010-11-04 00:07:02 -0400506 trace_snd_soc_bias_level_done(card, level);
507
Mark Brown452c5ea2009-05-17 21:41:23 +0100508 return ret;
509}
510
Richard Purdie2b97eab2006-10-06 18:32:18 +0200511/* set up initial codec paths */
512static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
513 struct snd_soc_dapm_path *p, int i)
514{
515 switch (w->id) {
516 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000517 case snd_soc_dapm_mixer:
518 case snd_soc_dapm_mixer_named_ctl: {
Lars-Peter Clausen30ac6b62014-02-13 14:14:21 +0100519 unsigned int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100520 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600521 w->kcontrol_news[i].private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +0200522 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400523 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100524 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400525 unsigned int mask = (1 << fls(max)) - 1;
526 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200527
Lars-Peter Clausen249ce132013-10-06 13:43:49 +0200528 if (reg != SND_SOC_NOPM) {
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800529 soc_widget_read(w, reg, &val);
Lars-Peter Clausen249ce132013-10-06 13:43:49 +0200530 val = (val >> shift) & mask;
531 if (invert)
532 val = max - val;
533 p->connect = !!val;
534 } else {
535 p->connect = 0;
536 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200537
Richard Purdie2b97eab2006-10-06 18:32:18 +0200538 }
539 break;
540 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600541 struct soc_enum *e = (struct soc_enum *)
542 w->kcontrol_news[i].private_value;
Lars-Peter Clausen30ac6b62014-02-13 14:14:21 +0100543 unsigned int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200544
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800545 soc_widget_read(w, e->reg, &val);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200546 item = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200547
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200548 if (item < e->max && !strcmp(p->name, e->texts[item]))
549 p->connect = 1;
550 else
551 p->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200552 }
553 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000554 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600555 struct soc_enum *e = (struct soc_enum *)
556 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000557
558 p->connect = 0;
559 /* since a virtual mux has no backing registers to
560 * decide which path to connect, it will try to match
561 * with the first enumeration. This is to ensure
562 * that the default mux choice (the first) will be
563 * correctly powered up during initialization.
564 */
565 if (!strcmp(p->name, e->texts[0]))
566 p->connect = 1;
567 }
568 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200569 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200570 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600571 w->kcontrol_news[i].private_value;
Lars-Peter Clausen30ac6b62014-02-13 14:14:21 +0100572 unsigned int val, item;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200573
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -0800574 soc_widget_read(w, e->reg, &val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200575 val = (val >> e->shift_l) & e->mask;
576 for (item = 0; item < e->max; item++) {
577 if (val == e->values[item])
578 break;
579 }
580
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200581 if (item < e->max && !strcmp(p->name, e->texts[item]))
582 p->connect = 1;
583 else
584 p->connect = 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200585 }
586 break;
Mark Brown56563102011-10-03 22:41:09 +0100587 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200588 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600589 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200590 case snd_soc_dapm_output:
591 case snd_soc_dapm_adc:
592 case snd_soc_dapm_input:
Mark Brown1ab97c82011-11-27 16:21:51 +0000593 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200594 case snd_soc_dapm_dac:
595 case snd_soc_dapm_micbias:
596 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100597 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +0000598 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200599 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100600 case snd_soc_dapm_aif_in:
601 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100602 case snd_soc_dapm_dai_in:
603 case snd_soc_dapm_dai_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200604 case snd_soc_dapm_hp:
605 case snd_soc_dapm_mic:
606 case snd_soc_dapm_spk:
607 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +0100608 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200609 case snd_soc_dapm_kcontrol:
Mark Brown56563102011-10-03 22:41:09 +0100610 p->connect = 1;
611 break;
612 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200613 case snd_soc_dapm_pre:
614 case snd_soc_dapm_post:
615 p->connect = 0;
616 break;
617 }
618}
619
Mark Brown74b8f952009-06-06 11:26:15 +0100620/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200621static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200622 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
623 struct snd_soc_dapm_path *path, const char *control_name,
624 const struct snd_kcontrol_new *kcontrol)
625{
626 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
627 int i;
628
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100629 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200630 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200631 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200632 list_add(&path->list_sink, &dest->sources);
633 list_add(&path->list_source, &src->sinks);
634 path->name = (char*)e->texts[i];
635 dapm_set_path_status(dest, path, 0);
636 return 0;
637 }
638 }
639
640 return -ENODEV;
641}
642
Mark Brown74b8f952009-06-06 11:26:15 +0100643/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200644static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200645 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
646 struct snd_soc_dapm_path *path, const char *control_name)
647{
648 int i;
649
650 /* search for mixer kcontrol */
651 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600652 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200653 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200654 list_add(&path->list_sink, &dest->sources);
655 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600656 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200657 dapm_set_path_status(dest, path, i);
658 return 0;
659 }
660 }
661 return -ENODEV;
662}
663
Stephen Warrenaf468002011-04-28 17:38:01 -0600664static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600665 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600666 const struct snd_kcontrol_new *kcontrol_new,
667 struct snd_kcontrol **kcontrol)
668{
669 struct snd_soc_dapm_widget *w;
670 int i;
671
672 *kcontrol = NULL;
673
674 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600675 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
676 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600677 for (i = 0; i < w->num_kcontrols; i++) {
678 if (&w->kcontrol_news[i] == kcontrol_new) {
679 if (w->kcontrols)
680 *kcontrol = w->kcontrols[i];
681 return 1;
682 }
683 }
684 }
685
686 return 0;
687}
688
Stephen Warren85762e72013-03-29 15:40:10 -0600689/*
690 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
691 * create it. Either way, add the widget into the control's widget list
692 */
693static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100694 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200695{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200696 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000697 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000698 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600699 size_t prefix_len;
700 int shared;
701 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600702 bool wname_in_long_name, kcname_in_long_name;
Stephen Warren85762e72013-03-29 15:40:10 -0600703 char *long_name;
704 const char *name;
705 int ret;
Mark Brownefb7ac32011-03-08 17:23:24 +0000706
707 if (dapm->codec)
708 prefix = dapm->codec->name_prefix;
709 else
710 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200711
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000712 if (prefix)
713 prefix_len = strlen(prefix) + 1;
714 else
715 prefix_len = 0;
716
Stephen Warren85762e72013-03-29 15:40:10 -0600717 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
718 &kcontrol);
719
Stephen Warren85762e72013-03-29 15:40:10 -0600720 if (!kcontrol) {
721 if (shared) {
722 wname_in_long_name = false;
723 kcname_in_long_name = true;
724 } else {
725 switch (w->id) {
726 case snd_soc_dapm_switch:
727 case snd_soc_dapm_mixer:
728 wname_in_long_name = true;
729 kcname_in_long_name = true;
730 break;
731 case snd_soc_dapm_mixer_named_ctl:
732 wname_in_long_name = false;
733 kcname_in_long_name = true;
734 break;
735 case snd_soc_dapm_mux:
736 case snd_soc_dapm_virt_mux:
737 case snd_soc_dapm_value_mux:
738 wname_in_long_name = true;
739 kcname_in_long_name = false;
740 break;
741 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600742 return -EINVAL;
743 }
744 }
745
746 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600747 /*
748 * The control will get a prefix from the control
749 * creation process but we're also using the same
750 * prefix for widgets so cut the prefix off the
751 * front of the widget name.
752 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200753 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600754 w->name + prefix_len,
755 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200756 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200757 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600758
759 name = long_name;
760 } else if (wname_in_long_name) {
761 long_name = NULL;
762 name = w->name + prefix_len;
763 } else {
764 long_name = NULL;
765 name = w->kcontrol_news[kci].name;
766 }
767
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200768 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600769 prefix);
Lars-Peter Clausen656ca9d2013-06-14 13:16:54 +0200770 kfree(long_name);
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200771 if (!kcontrol)
772 return -ENOMEM;
773 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200774
775 ret = dapm_kcontrol_data_alloc(w, kcontrol);
776 if (ret) {
777 snd_ctl_free_one(kcontrol);
778 return ret;
779 }
780
Stephen Warren85762e72013-03-29 15:40:10 -0600781 ret = snd_ctl_add(card, kcontrol);
782 if (ret < 0) {
783 dev_err(dapm->dev,
784 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
785 w->name, name, ret);
Stephen Warren85762e72013-03-29 15:40:10 -0600786 return ret;
787 }
Stephen Warren85762e72013-03-29 15:40:10 -0600788 }
789
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200790 ret = dapm_kcontrol_add_widget(kcontrol, w);
791 if (ret)
792 return ret;
793
Stephen Warren85762e72013-03-29 15:40:10 -0600794 w->kcontrols[kci] = kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600795
796 return 0;
797}
798
799/* create new dapm mixer control */
800static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
801{
802 int i, ret;
803 struct snd_soc_dapm_path *path;
804
Richard Purdie2b97eab2006-10-06 18:32:18 +0200805 /* add kcontrol */
806 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200807 /* match name */
808 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200809 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600810 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200811 continue;
812
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200813 if (w->kcontrols[i]) {
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200814 dapm_kcontrol_add_path(w->kcontrols[i], path);
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200815 continue;
816 }
817
Mark Brown946d92a2013-08-12 23:28:42 +0100818 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
Stephen Warren85762e72013-03-29 15:40:10 -0600819 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200820 return ret;
Mark Brown946d92a2013-08-12 23:28:42 +0100821
822 dapm_kcontrol_add_path(w->kcontrols[i], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200823 }
824 }
Stephen Warren85762e72013-03-29 15:40:10 -0600825
826 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200827}
828
829/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200830static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200831{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200832 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600833 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600834 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200835
Stephen Warrenaf468002011-04-28 17:38:01 -0600836 if (w->num_kcontrols != 1) {
837 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000838 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600839 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200840 return -EINVAL;
841 }
842
Lars-Peter Clausenfe5813912013-08-01 18:30:38 +0200843 if (list_empty(&w->sources)) {
Stephen Warren85762e72013-03-29 15:40:10 -0600844 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
845 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600846 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200847
Mark Brown946d92a2013-08-12 23:28:42 +0100848 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600849 if (ret < 0)
850 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600851
Richard Purdie2b97eab2006-10-06 18:32:18 +0200852 list_for_each_entry(path, &w->sources, list_sink)
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200853 dapm_kcontrol_add_path(w->kcontrols[0], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200854
Stephen Warrenaf468002011-04-28 17:38:01 -0600855 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200856}
857
858/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200859static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200860{
Mark Browna6c65732010-03-03 17:45:21 +0000861 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200862 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000863 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200864
Mark Browna6c65732010-03-03 17:45:21 +0000865 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200866}
867
868/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100869static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
870 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200871{
872 struct snd_soc_dapm_path *p;
873
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100874 list_for_each_entry(p, sink, list_source) {
875 if (p->walked) {
876 p->walked = 0;
877 dapm_clear_walk_output(dapm, &p->sink->sinks);
878 }
879 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200880}
881
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100882static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
883 struct list_head *source)
884{
885 struct snd_soc_dapm_path *p;
886
887 list_for_each_entry(p, source, list_sink) {
888 if (p->walked) {
889 p->walked = 0;
890 dapm_clear_walk_input(dapm, &p->source->sources);
891 }
892 }
893}
894
895
Mark Brown99497882010-05-07 20:24:05 +0100896/* We implement power down on suspend by checking the power state of
897 * the ALSA card - when we are suspending the ALSA state for the card
898 * is set to D3.
899 */
900static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
901{
Mark Brown12ea2c72011-03-02 18:17:32 +0000902 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100903
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000904 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100905 case SNDRV_CTL_POWER_D3hot:
906 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100907 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000908 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200909 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100910 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100911 default:
912 return 1;
913 }
914}
915
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100916/* add widget to list if it's not already in the list */
917static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
918 struct snd_soc_dapm_widget *w)
919{
920 struct snd_soc_dapm_widget_list *wlist;
921 int wlistsize, wlistentries, i;
922
923 if (*list == NULL)
924 return -EINVAL;
925
926 wlist = *list;
927
928 /* is this widget already in the list */
929 for (i = 0; i < wlist->num_widgets; i++) {
930 if (wlist->widgets[i] == w)
931 return 0;
932 }
933
934 /* allocate some new space */
935 wlistentries = wlist->num_widgets + 1;
936 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
937 wlistentries * sizeof(struct snd_soc_dapm_widget *);
938 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
939 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000940 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100941 w->name);
942 return -ENOMEM;
943 }
944 wlist = *list;
945
946 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000947 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100948 w->name, wlist->num_widgets);
949
950 wlist->widgets[wlist->num_widgets] = w;
951 wlist->num_widgets++;
952 return 1;
953}
954
Richard Purdie2b97eab2006-10-06 18:32:18 +0200955/*
956 * Recursively check for a completed path to an active or physically connected
957 * output widget. Returns number of complete paths.
958 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100959static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
960 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200961{
962 struct snd_soc_dapm_path *path;
963 int con = 0;
964
Mark Brown024dc072011-10-09 11:52:05 +0100965 if (widget->outputs >= 0)
966 return widget->outputs;
967
Mark Brownde02d072011-09-20 21:43:24 +0100968 DAPM_UPDATE_STAT(widget, path_checks);
969
Mark Brown62ea8742012-01-21 21:14:48 +0000970 switch (widget->id) {
971 case snd_soc_dapm_supply:
972 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200973 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200974 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100975 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000976 default:
977 break;
978 }
Mark Brown246d0a12009-04-22 18:24:55 +0100979
Mark Brown010ff262009-08-17 17:39:22 +0100980 switch (widget->id) {
981 case snd_soc_dapm_adc:
982 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100983 case snd_soc_dapm_dai_out:
Mark Brown024dc072011-10-09 11:52:05 +0100984 if (widget->active) {
985 widget->outputs = snd_soc_dapm_suspend_check(widget);
986 return widget->outputs;
987 }
Mark Brown010ff262009-08-17 17:39:22 +0100988 default:
989 break;
990 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200991
992 if (widget->connected) {
993 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100994 if (widget->id == snd_soc_dapm_output && !widget->ext) {
995 widget->outputs = snd_soc_dapm_suspend_check(widget);
996 return widget->outputs;
997 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200998
999 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +01001000 if (widget->id == snd_soc_dapm_hp ||
1001 widget->id == snd_soc_dapm_spk ||
1002 (widget->id == snd_soc_dapm_line &&
1003 !list_empty(&widget->sources))) {
1004 widget->outputs = snd_soc_dapm_suspend_check(widget);
1005 return widget->outputs;
1006 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001007 }
1008
1009 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e02011-09-21 18:19:14 +01001010 DAPM_UPDATE_STAT(widget, neighbour_checks);
1011
Mark Brownbf3a9e12011-06-13 16:42:29 +01001012 if (path->weak)
1013 continue;
1014
Mark Brown8af294b2013-02-22 17:48:15 +00001015 if (path->walking)
1016 return 1;
1017
Richard Purdie2b97eab2006-10-06 18:32:18 +02001018 if (path->walked)
1019 continue;
1020
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001021 trace_snd_soc_dapm_output_path(widget, path);
1022
Richard Purdie2b97eab2006-10-06 18:32:18 +02001023 if (path->sink && path->connect) {
1024 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +00001025 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001026
1027 /* do we need to add this widget to the list ? */
1028 if (list) {
1029 int err;
1030 err = dapm_list_add_widget(list, path->sink);
1031 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001032 dev_err(widget->dapm->dev,
1033 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001034 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001035 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001036 return con;
1037 }
1038 }
1039
1040 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001041
1042 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001043 }
1044 }
1045
Mark Brown024dc072011-10-09 11:52:05 +01001046 widget->outputs = con;
1047
Richard Purdie2b97eab2006-10-06 18:32:18 +02001048 return con;
1049}
1050
1051/*
1052 * Recursively check for a completed path to an active or physically connected
1053 * input widget. Returns number of complete paths.
1054 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001055static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1056 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001057{
1058 struct snd_soc_dapm_path *path;
1059 int con = 0;
1060
Mark Brown024dc072011-10-09 11:52:05 +01001061 if (widget->inputs >= 0)
1062 return widget->inputs;
1063
Mark Brownde02d072011-09-20 21:43:24 +01001064 DAPM_UPDATE_STAT(widget, path_checks);
1065
Mark Brown62ea8742012-01-21 21:14:48 +00001066 switch (widget->id) {
1067 case snd_soc_dapm_supply:
1068 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001069 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001070 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +01001071 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +00001072 default:
1073 break;
1074 }
Mark Brown246d0a12009-04-22 18:24:55 +01001075
Richard Purdie2b97eab2006-10-06 18:32:18 +02001076 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +01001077 switch (widget->id) {
1078 case snd_soc_dapm_dac:
1079 case snd_soc_dapm_aif_in:
Mark Brown46162742013-06-05 19:36:11 +01001080 case snd_soc_dapm_dai_in:
Mark Brown024dc072011-10-09 11:52:05 +01001081 if (widget->active) {
1082 widget->inputs = snd_soc_dapm_suspend_check(widget);
1083 return widget->inputs;
1084 }
Mark Brown010ff262009-08-17 17:39:22 +01001085 default:
1086 break;
1087 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001088
1089 if (widget->connected) {
1090 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +01001091 if (widget->id == snd_soc_dapm_input && !widget->ext) {
1092 widget->inputs = snd_soc_dapm_suspend_check(widget);
1093 return widget->inputs;
1094 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001095
1096 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +01001097 if (widget->id == snd_soc_dapm_vmid) {
1098 widget->inputs = snd_soc_dapm_suspend_check(widget);
1099 return widget->inputs;
1100 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001101
1102 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +03001103 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +01001104 (widget->id == snd_soc_dapm_line &&
1105 !list_empty(&widget->sinks))) {
1106 widget->inputs = snd_soc_dapm_suspend_check(widget);
1107 return widget->inputs;
1108 }
1109
Mark Brown1ab97c82011-11-27 16:21:51 +00001110 /* signal generator */
1111 if (widget->id == snd_soc_dapm_siggen) {
1112 widget->inputs = snd_soc_dapm_suspend_check(widget);
1113 return widget->inputs;
1114 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001115 }
1116
1117 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e02011-09-21 18:19:14 +01001118 DAPM_UPDATE_STAT(widget, neighbour_checks);
1119
Mark Brownbf3a9e12011-06-13 16:42:29 +01001120 if (path->weak)
1121 continue;
1122
Mark Brown8af294b2013-02-22 17:48:15 +00001123 if (path->walking)
1124 return 1;
1125
Richard Purdie2b97eab2006-10-06 18:32:18 +02001126 if (path->walked)
1127 continue;
1128
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001129 trace_snd_soc_dapm_input_path(widget, path);
1130
Richard Purdie2b97eab2006-10-06 18:32:18 +02001131 if (path->source && path->connect) {
1132 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +00001133 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001134
1135 /* do we need to add this widget to the list ? */
1136 if (list) {
1137 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +01001138 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001139 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001140 dev_err(widget->dapm->dev,
1141 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001142 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001143 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001144 return con;
1145 }
1146 }
1147
1148 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001149
1150 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001151 }
1152 }
1153
Mark Brown024dc072011-10-09 11:52:05 +01001154 widget->inputs = con;
1155
Richard Purdie2b97eab2006-10-06 18:32:18 +02001156 return con;
1157}
1158
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001159/**
1160 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1161 * @dai: the soc DAI.
1162 * @stream: stream direction.
1163 * @list: list of active widgets for this stream.
1164 *
1165 * Queries DAPM graph as to whether an valid audio stream path exists for
1166 * the initial stream specified by name. This takes into account
1167 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1168 *
1169 * Returns the number of valid paths or negative error.
1170 */
1171int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1172 struct snd_soc_dapm_widget_list **list)
1173{
1174 struct snd_soc_card *card = dai->card;
1175 int paths;
1176
1177 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1178 dapm_reset(card);
1179
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001180 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001181 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001182 dapm_clear_walk_output(&card->dapm,
1183 &dai->playback_widget->sinks);
1184 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001185 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001186 dapm_clear_walk_input(&card->dapm,
1187 &dai->capture_widget->sources);
1188 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001189
1190 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001191 mutex_unlock(&card->dapm_mutex);
1192
1193 return paths;
1194}
1195
Richard Purdie2b97eab2006-10-06 18:32:18 +02001196/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001197 * Handler for generic register modifier widget.
1198 */
1199int dapm_reg_event(struct snd_soc_dapm_widget *w,
1200 struct snd_kcontrol *kcontrol, int event)
1201{
1202 unsigned int val;
1203
1204 if (SND_SOC_DAPM_EVENT_ON(event))
1205 val = w->on_val;
1206 else
1207 val = w->off_val;
1208
Liam Girdwood49575fb52012-03-06 18:16:19 +00001209 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001210 w->mask << w->shift, val << w->shift);
1211
1212 return 0;
1213}
Mark Brown11589412008-07-29 11:42:23 +01001214EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001215
Mark Brown62ea8742012-01-21 21:14:48 +00001216/*
1217 * Handler for regulator supply widget.
1218 */
1219int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1220 struct snd_kcontrol *kcontrol, int event)
1221{
Mark Brownc05b84d2012-09-07 12:57:11 +08001222 int ret;
1223
Mark Browneb270e92013-10-09 13:52:52 +01001224 soc_dapm_async_complete(w->dapm);
1225
Mark Brownc05b84d2012-09-07 12:57:11 +08001226 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001227 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001228 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001229 if (ret != 0)
1230 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001231 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001232 w->name, ret);
1233 }
1234
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001235 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001236 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001237 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001238 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001239 if (ret != 0)
1240 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00001241 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001242 w->name, ret);
1243 }
1244
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001245 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001246 }
Mark Brown62ea8742012-01-21 21:14:48 +00001247}
1248EXPORT_SYMBOL_GPL(dapm_regulator_event);
1249
Ola Liljad7e7eb92012-05-24 15:26:25 +02001250/*
1251 * Handler for clock supply widget.
1252 */
1253int dapm_clock_event(struct snd_soc_dapm_widget *w,
1254 struct snd_kcontrol *kcontrol, int event)
1255{
1256 if (!w->clk)
1257 return -EIO;
1258
Mark Browneb270e92013-10-09 13:52:52 +01001259 soc_dapm_async_complete(w->dapm);
1260
Mark Brownec029952012-06-04 08:16:20 +01001261#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001262 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001263 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001264 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001265 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001266 return 0;
1267 }
Mark Brownec029952012-06-04 08:16:20 +01001268#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001269 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001270}
1271EXPORT_SYMBOL_GPL(dapm_clock_event);
1272
Mark Brownd8050022011-09-28 18:28:23 +01001273static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1274{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001275 if (w->power_checked)
1276 return w->new_power;
1277
Mark Brownd8050022011-09-28 18:28:23 +01001278 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001279 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001280 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001281 w->new_power = w->power_check(w);
1282
1283 w->power_checked = true;
1284
1285 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001286}
1287
Mark Browncd0f2d42009-04-20 16:56:59 +01001288/* Generic check to see if a widget should be powered.
1289 */
1290static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1291{
1292 int in, out;
1293
Mark Brownde02d072011-09-20 21:43:24 +01001294 DAPM_UPDATE_STAT(w, power_checks);
1295
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001296 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001297 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001298 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001299 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001300 return out != 0 && in != 0;
1301}
1302
Mark Brown6ea31b92009-04-20 17:15:41 +01001303/* Check to see if an ADC has power */
1304static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1305{
1306 int in;
1307
Mark Brownde02d072011-09-20 21:43:24 +01001308 DAPM_UPDATE_STAT(w, power_checks);
1309
Mark Brown6ea31b92009-04-20 17:15:41 +01001310 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001311 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001312 dapm_clear_walk_input(w->dapm, &w->sources);
Mark Brown6ea31b92009-04-20 17:15:41 +01001313 return in != 0;
1314 } else {
1315 return dapm_generic_check_power(w);
1316 }
1317}
1318
1319/* Check to see if a DAC has power */
1320static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1321{
1322 int out;
1323
Mark Brownde02d072011-09-20 21:43:24 +01001324 DAPM_UPDATE_STAT(w, power_checks);
1325
Mark Brown6ea31b92009-04-20 17:15:41 +01001326 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001327 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001328 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001329 return out != 0;
1330 } else {
1331 return dapm_generic_check_power(w);
1332 }
1333}
1334
Mark Brown246d0a12009-04-22 18:24:55 +01001335/* Check to see if a power supply is needed */
1336static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1337{
1338 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001339
Mark Brownde02d072011-09-20 21:43:24 +01001340 DAPM_UPDATE_STAT(w, power_checks);
1341
Mark Brown246d0a12009-04-22 18:24:55 +01001342 /* Check if one of our outputs is connected */
1343 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001344 DAPM_UPDATE_STAT(w, neighbour_checks);
1345
Mark Brownbf3a9e12011-06-13 16:42:29 +01001346 if (path->weak)
1347 continue;
1348
Mark Brown215edda2009-09-08 18:59:05 +01001349 if (path->connected &&
1350 !path->connected(path->source, path->sink))
1351 continue;
1352
Mark Brown30173582011-02-11 11:42:19 +00001353 if (!path->sink)
1354 continue;
1355
Mark Brownf68d7e12011-10-04 22:57:50 +01001356 if (dapm_widget_power_check(path->sink))
1357 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001358 }
1359
Mark Brownf68d7e12011-10-04 22:57:50 +01001360 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001361}
1362
Mark Brown35c64bc2011-09-28 18:23:53 +01001363static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1364{
1365 return 1;
1366}
1367
Mark Brown38357ab2009-06-06 19:03:23 +01001368static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1369 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001370 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001371{
Mark Brown828a8422011-01-15 13:14:30 +00001372 int *sort;
1373
1374 if (power_up)
1375 sort = dapm_up_seq;
1376 else
1377 sort = dapm_down_seq;
1378
Mark Brown38357ab2009-06-06 19:03:23 +01001379 if (sort[a->id] != sort[b->id])
1380 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001381 if (a->subseq != b->subseq) {
1382 if (power_up)
1383 return a->subseq - b->subseq;
1384 else
1385 return b->subseq - a->subseq;
1386 }
Mark Brownb22ead22009-06-07 12:51:26 +01001387 if (a->reg != b->reg)
1388 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001389 if (a->dapm != b->dapm)
1390 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001391
Mark Brown38357ab2009-06-06 19:03:23 +01001392 return 0;
1393}
Mark Brown42aa3412009-03-01 19:21:10 +00001394
Mark Brown38357ab2009-06-06 19:03:23 +01001395/* Insert a widget in order into a DAPM power sequence. */
1396static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1397 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001398 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001399{
1400 struct snd_soc_dapm_widget *w;
1401
1402 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001403 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001404 list_add_tail(&new_widget->power_list, &w->power_list);
1405 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001406 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001407
Mark Brown38357ab2009-06-06 19:03:23 +01001408 list_add_tail(&new_widget->power_list, list);
1409}
Mark Brown42aa3412009-03-01 19:21:10 +00001410
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001411static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001412 struct snd_soc_dapm_widget *w, int event)
1413{
Mark Brown68f89ad2010-11-03 23:51:49 -04001414 const char *ev_name;
1415 int power, ret;
1416
1417 switch (event) {
1418 case SND_SOC_DAPM_PRE_PMU:
1419 ev_name = "PRE_PMU";
1420 power = 1;
1421 break;
1422 case SND_SOC_DAPM_POST_PMU:
1423 ev_name = "POST_PMU";
1424 power = 1;
1425 break;
1426 case SND_SOC_DAPM_PRE_PMD:
1427 ev_name = "PRE_PMD";
1428 power = 0;
1429 break;
1430 case SND_SOC_DAPM_POST_PMD:
1431 ev_name = "POST_PMD";
1432 power = 0;
1433 break;
Mark Brown80114122013-02-25 15:14:19 +00001434 case SND_SOC_DAPM_WILL_PMU:
1435 ev_name = "WILL_PMU";
1436 power = 1;
1437 break;
1438 case SND_SOC_DAPM_WILL_PMD:
1439 ev_name = "WILL_PMD";
1440 power = 0;
1441 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001442 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001443 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001444 return;
1445 }
1446
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001447 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001448 return;
1449
1450 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001451 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001452 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001453 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001454 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001455 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001456 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001457 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001458 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001459 ev_name, w->name, ret);
1460 }
1461}
1462
Mark Brownb22ead22009-06-07 12:51:26 +01001463/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001464static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001465 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001466{
Mark Brown68f89ad2010-11-03 23:51:49 -04001467 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001468 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001469 unsigned int value = 0;
1470 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001471
1472 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1473 power_list)->reg;
1474
1475 list_for_each_entry(w, pending, power_list) {
Takashi Iwaibf4edea2013-11-07 18:38:47 +01001476 WARN_ON(reg != w->reg);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001477 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001478
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001479 mask |= w->mask << w->shift;
1480 if (w->power)
1481 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001482 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001483 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001484
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001485 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001486 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1487 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001488
Mark Brown68f89ad2010-11-03 23:51:49 -04001489 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001490 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1491 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001492 }
1493
Mark Brown81628102009-06-07 13:21:24 +01001494 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001495 /* Any widget will do, they should all be updating the
1496 * same register.
1497 */
1498 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1499 power_list);
1500
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001501 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001502 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001503 value, mask, reg, card->pop_time);
1504 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001505 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001506 }
1507
1508 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001509 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1510 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001511 }
Mark Brown42aa3412009-03-01 19:21:10 +00001512}
1513
Mark Brownb22ead22009-06-07 12:51:26 +01001514/* Apply a DAPM power sequence.
1515 *
1516 * We walk over a pre-sorted list of widgets to apply power to. In
1517 * order to minimise the number of writes to the device required
1518 * multiple widgets will be updated in a single write where possible.
1519 * Currently anything that requires more than a single write is not
1520 * handled.
1521 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001522static void dapm_seq_run(struct snd_soc_card *card,
1523 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001524{
1525 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001526 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001527 LIST_HEAD(pending);
1528 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001529 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001530 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001531 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001532 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001533 int *sort;
1534
1535 if (power_up)
1536 sort = dapm_up_seq;
1537 else
1538 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001539
Mark Brownb22ead22009-06-07 12:51:26 +01001540 list_for_each_entry_safe(w, n, list, power_list) {
1541 ret = 0;
1542
1543 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001544 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001545 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001546 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001547 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001548
Mark Brown474b62d2011-01-18 16:14:44 +00001549 if (cur_dapm && cur_dapm->seq_notifier) {
1550 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1551 if (sort[i] == cur_sort)
1552 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001553 i,
1554 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001555 }
1556
Mark Browneb270e92013-10-09 13:52:52 +01001557 if (cur_dapm && w->dapm != cur_dapm)
1558 soc_dapm_async_complete(cur_dapm);
1559
Mark Brownb22ead22009-06-07 12:51:26 +01001560 INIT_LIST_HEAD(&pending);
1561 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001562 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001563 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001564 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001565 }
1566
Mark Brown163cac02009-06-07 10:12:52 +01001567 switch (w->id) {
1568 case snd_soc_dapm_pre:
1569 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001570 list_for_each_entry_safe_continue(w, n, list,
1571 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001572
Mark Brownb22ead22009-06-07 12:51:26 +01001573 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001574 ret = w->event(w,
1575 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001576 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001577 ret = w->event(w,
1578 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001579 break;
1580
1581 case snd_soc_dapm_post:
1582 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001583 list_for_each_entry_safe_continue(w, n, list,
1584 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001585
Mark Brownb22ead22009-06-07 12:51:26 +01001586 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001587 ret = w->event(w,
1588 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001589 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001590 ret = w->event(w,
1591 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001592 break;
1593
Mark Brown163cac02009-06-07 10:12:52 +01001594 default:
Mark Brown81628102009-06-07 13:21:24 +01001595 /* Queue it up for application */
1596 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001597 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001598 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001599 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001600 list_move(&w->power_list, &pending);
1601 break;
Mark Brown163cac02009-06-07 10:12:52 +01001602 }
Mark Brownb22ead22009-06-07 12:51:26 +01001603
1604 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001605 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001606 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001607 }
Mark Brownb22ead22009-06-07 12:51:26 +01001608
1609 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001610 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001611
1612 if (cur_dapm && cur_dapm->seq_notifier) {
1613 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1614 if (sort[i] == cur_sort)
1615 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001616 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001617 }
Mark Browneb270e92013-10-09 13:52:52 +01001618
1619 list_for_each_entry(d, &card->dapm_list, list) {
1620 soc_dapm_async_complete(d);
1621 }
Mark Brown163cac02009-06-07 10:12:52 +01001622}
1623
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001624static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001625{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001626 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001627 struct snd_soc_dapm_widget_list *wlist;
1628 struct snd_soc_dapm_widget *w = NULL;
1629 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001630 int ret;
1631
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001632 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001633 return;
1634
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001635 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001636
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001637 for (wi = 0; wi < wlist->num_widgets; wi++) {
1638 w = wlist->widgets[wi];
1639
1640 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1641 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1642 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001643 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001644 w->name, ret);
1645 }
Mark Brown97404f22010-12-14 16:13:57 +00001646 }
1647
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001648 if (!w)
1649 return;
1650
Liam Girdwood49575fb52012-03-06 18:16:19 +00001651 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001652 update->val);
1653 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001654 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001655 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001656
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001657 for (wi = 0; wi < wlist->num_widgets; wi++) {
1658 w = wlist->widgets[wi];
1659
1660 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1661 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1662 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001663 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001664 w->name, ret);
1665 }
Mark Brown97404f22010-12-14 16:13:57 +00001666 }
1667}
1668
Mark Brown9d0624a2011-02-18 11:49:43 -08001669/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1670 * they're changing state.
1671 */
1672static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1673{
1674 struct snd_soc_dapm_context *d = data;
1675 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001676
Mark Brown56fba412011-06-04 11:25:10 +01001677 /* If we're off and we're not supposed to be go into STANDBY */
1678 if (d->bias_level == SND_SOC_BIAS_OFF &&
1679 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001680 if (d->dev)
1681 pm_runtime_get_sync(d->dev);
1682
Mark Brown9d0624a2011-02-18 11:49:43 -08001683 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1684 if (ret != 0)
1685 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001686 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001687 }
1688
Mark Brown56fba412011-06-04 11:25:10 +01001689 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1690 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001691 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1692 if (ret != 0)
1693 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001694 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001695 }
1696}
1697
1698/* Async callback run prior to DAPM sequences - brings to their final
1699 * state.
1700 */
1701static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1702{
1703 struct snd_soc_dapm_context *d = data;
1704 int ret;
1705
1706 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001707 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1708 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1709 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001710 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1711 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001712 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001713 ret);
1714 }
1715
1716 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001717 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1718 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001719 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1720 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001721 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1722 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001723
1724 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001725 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001726 }
1727
1728 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001729 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1730 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001731 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1732 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001733 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001734 ret);
1735 }
1736}
Mark Brown97404f22010-12-14 16:13:57 +00001737
Mark Brownfe4fda52011-10-03 22:36:57 +01001738static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1739 bool power, bool connect)
1740{
1741 /* If a connection is being made or broken then that update
1742 * will have marked the peer dirty, otherwise the widgets are
1743 * not connected and this update has no impact. */
1744 if (!connect)
1745 return;
1746
1747 /* If the peer is already in the state we're moving to then we
1748 * won't have an impact on it. */
1749 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001750 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001751}
1752
Mark Brown05623c42011-09-28 17:02:31 +01001753static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1754 struct list_head *up_list,
1755 struct list_head *down_list)
1756{
Mark Browndb432b42011-10-03 21:06:40 +01001757 struct snd_soc_dapm_path *path;
1758
Mark Brown05623c42011-09-28 17:02:31 +01001759 if (w->power == power)
1760 return;
1761
1762 trace_snd_soc_dapm_widget_power(w, power);
1763
Mark Browndb432b42011-10-03 21:06:40 +01001764 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001765 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001766 */
1767 list_for_each_entry(path, &w->sources, list_sink) {
1768 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001769 dapm_widget_set_peer_power(path->source, power,
1770 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001771 }
1772 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001773 switch (w->id) {
1774 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001775 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001776 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001777 case snd_soc_dapm_kcontrol:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001778 /* Supplies can't affect their outputs, only their inputs */
1779 break;
1780 default:
1781 list_for_each_entry(path, &w->sinks, list_source) {
1782 if (path->sink) {
1783 dapm_widget_set_peer_power(path->sink, power,
1784 path->connect);
1785 }
Mark Browndb432b42011-10-03 21:06:40 +01001786 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001787 break;
Mark Browndb432b42011-10-03 21:06:40 +01001788 }
1789
Mark Brown05623c42011-09-28 17:02:31 +01001790 if (power)
1791 dapm_seq_insert(w, up_list, true);
1792 else
1793 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001794}
1795
Mark Brown7c81beb2011-09-20 22:22:32 +01001796static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1797 struct list_head *up_list,
1798 struct list_head *down_list)
1799{
Mark Brown7c81beb2011-09-20 22:22:32 +01001800 int power;
1801
1802 switch (w->id) {
1803 case snd_soc_dapm_pre:
1804 dapm_seq_insert(w, down_list, false);
1805 break;
1806 case snd_soc_dapm_post:
1807 dapm_seq_insert(w, up_list, true);
1808 break;
1809
1810 default:
Mark Brownd8050022011-09-28 18:28:23 +01001811 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001812
Mark Brown05623c42011-09-28 17:02:31 +01001813 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001814 break;
1815 }
1816}
1817
Mark Brown42aa3412009-03-01 19:21:10 +00001818/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001819 * Scan each dapm widget for complete audio path.
1820 * A complete path is a route that has valid endpoints i.e.:-
1821 *
1822 * o DAC to output pin.
1823 * o Input Pin to ADC.
1824 * o Input pin to Output pin (bypass, sidetone)
1825 * o DAC to ADC (loopback).
1826 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001827static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001828{
1829 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001830 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001831 LIST_HEAD(up_list);
1832 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001833 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001834 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001835
Mark Brownf9fa2b12014-03-06 16:49:11 +08001836 lockdep_assert_held(&card->dapm_mutex);
1837
Mark Brown84e90932010-11-04 00:07:02 -04001838 trace_snd_soc_dapm_start(card);
1839
Mark Brown56fba412011-06-04 11:25:10 +01001840 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001841 if (d->idle_bias_off)
1842 d->target_bias_level = SND_SOC_BIAS_OFF;
1843 else
1844 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001845 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001846
Liam Girdwood6c120e12012-02-15 15:15:34 +00001847 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001848
Mark Brown6d3ddc82009-05-16 17:47:29 +01001849 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001850 * lists indicating if they should be powered up or down. We
1851 * only check widgets that have been flagged as dirty but note
1852 * that new widgets may be added to the dirty list while we
1853 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001854 */
Mark Browndb432b42011-10-03 21:06:40 +01001855 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001856 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001857 }
1858
Mark Brownf9de6d72011-09-28 17:19:47 +01001859 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001860 switch (w->id) {
1861 case snd_soc_dapm_pre:
1862 case snd_soc_dapm_post:
1863 /* These widgets always need to be powered */
1864 break;
1865 default:
1866 list_del_init(&w->dirty);
1867 break;
1868 }
Mark Browndb432b42011-10-03 21:06:40 +01001869
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001870 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001871 d = w->dapm;
1872
1873 /* Supplies and micbiases only bring the
1874 * context up to STANDBY as unless something
1875 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001876 * generally don't require full power. Signal
1877 * generators are virtual pins and have no
1878 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001879 */
1880 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001881 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001882 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001883 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001884 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001885 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001886 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001887 case snd_soc_dapm_micbias:
1888 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1889 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1890 break;
1891 default:
1892 d->target_bias_level = SND_SOC_BIAS_ON;
1893 break;
1894 }
1895 }
1896
1897 }
1898
Mark Brown85a843c2011-09-21 21:29:47 +01001899 /* Force all contexts in the card to the same bias state if
1900 * they're not ground referenced.
1901 */
Mark Brown56fba412011-06-04 11:25:10 +01001902 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001903 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001904 if (d->target_bias_level > bias)
1905 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001906 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001907 if (!d->idle_bias_off)
1908 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001909
Mark Brownde02d072011-09-20 21:43:24 +01001910 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001911
Xiang Xiao17282ba2014-03-02 00:04:03 +08001912 /* Run card bias changes at first */
1913 dapm_pre_sequence_async(&card->dapm, 0);
1914 /* Run other bias changes in parallel */
1915 list_for_each_entry(d, &card->dapm_list, list) {
1916 if (d != &card->dapm)
1917 async_schedule_domain(dapm_pre_sequence_async, d,
1918 &async_domain);
1919 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001920 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001921
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001922 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001923 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001924 }
1925
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001926 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001927 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001928 }
1929
Mark Brown6d3ddc82009-05-16 17:47:29 +01001930 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001931 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001932
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001933 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001934
Mark Brown6d3ddc82009-05-16 17:47:29 +01001935 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001936 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001937
Mark Brown9d0624a2011-02-18 11:49:43 -08001938 /* Run all the bias changes in parallel */
Xiang Xiao17282ba2014-03-02 00:04:03 +08001939 list_for_each_entry(d, &card->dapm_list, list) {
1940 if (d != &card->dapm)
1941 async_schedule_domain(dapm_post_sequence_async, d,
1942 &async_domain);
1943 }
Mark Brown9d0624a2011-02-18 11:49:43 -08001944 async_synchronize_full_domain(&async_domain);
Xiang Xiao17282ba2014-03-02 00:04:03 +08001945 /* Run card bias changes at last */
1946 dapm_post_sequence_async(&card->dapm, 0);
Mark Brown452c5ea2009-05-17 21:41:23 +01001947
Liam Girdwood8078d872012-02-15 15:15:35 +00001948 /* do we need to notify any clients that DAPM event is complete */
1949 list_for_each_entry(d, &card->dapm_list, list) {
1950 if (d->stream_event)
1951 d->stream_event(d, event);
1952 }
1953
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001954 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001955 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001956 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001957
Mark Brown84e90932010-11-04 00:07:02 -04001958 trace_snd_soc_dapm_done(card);
1959
Mark Brown42aa3412009-03-01 19:21:10 +00001960 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001961}
1962
Mark Brown79fb9382009-08-21 16:38:13 +01001963#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001964static ssize_t dapm_widget_power_read_file(struct file *file,
1965 char __user *user_buf,
1966 size_t count, loff_t *ppos)
1967{
1968 struct snd_soc_dapm_widget *w = file->private_data;
1969 char *buf;
1970 int in, out;
1971 ssize_t ret;
1972 struct snd_soc_dapm_path *p = NULL;
1973
1974 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1975 if (!buf)
1976 return -ENOMEM;
1977
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001978 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001979 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001980 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001981 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001982
Mark Brownf13ebad2012-03-03 18:01:01 +00001983 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1984 w->name, w->power ? "On" : "Off",
1985 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001986
Mark Brownd033c362009-12-04 15:25:56 +00001987 if (w->reg >= 0)
1988 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001989 " - R%d(0x%x) mask 0x%x",
1990 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001991
1992 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1993
Mark Brown3eef08b2009-09-14 16:49:00 +01001994 if (w->sname)
1995 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1996 w->sname,
1997 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001998
1999 list_for_each_entry(p, &w->sources, list_sink) {
Takashi Iwaiff186202013-10-28 14:21:49 +01002000 if (p->connected && !p->connected(w, p->source))
Mark Brown215edda2009-09-08 18:59:05 +01002001 continue;
2002
Mark Brown79fb9382009-08-21 16:38:13 +01002003 if (p->connect)
2004 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00002005 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01002006 p->name ? p->name : "static",
2007 p->source->name);
2008 }
2009 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01002010 if (p->connected && !p->connected(w, p->sink))
2011 continue;
2012
Mark Brown79fb9382009-08-21 16:38:13 +01002013 if (p->connect)
2014 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00002015 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01002016 p->name ? p->name : "static",
2017 p->sink->name);
2018 }
2019
2020 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2021
2022 kfree(buf);
2023 return ret;
2024}
2025
2026static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002027 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01002028 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002029 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01002030};
2031
Mark Brownef49e4f2011-04-04 20:48:13 +09002032static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2033 size_t count, loff_t *ppos)
2034{
2035 struct snd_soc_dapm_context *dapm = file->private_data;
2036 char *level;
2037
2038 switch (dapm->bias_level) {
2039 case SND_SOC_BIAS_ON:
2040 level = "On\n";
2041 break;
2042 case SND_SOC_BIAS_PREPARE:
2043 level = "Prepare\n";
2044 break;
2045 case SND_SOC_BIAS_STANDBY:
2046 level = "Standby\n";
2047 break;
2048 case SND_SOC_BIAS_OFF:
2049 level = "Off\n";
2050 break;
2051 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01002052 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09002053 level = "Unknown\n";
2054 break;
2055 }
2056
2057 return simple_read_from_buffer(user_buf, count, ppos, level,
2058 strlen(level));
2059}
2060
2061static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002062 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09002063 .read = dapm_bias_read_file,
2064 .llseek = default_llseek,
2065};
2066
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002067void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2068 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002069{
Mark Brown79fb9382009-08-21 16:38:13 +01002070 struct dentry *d;
2071
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002072 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2073
2074 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00002075 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002076 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002077 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002078 }
Mark Brown79fb9382009-08-21 16:38:13 +01002079
Mark Brownef49e4f2011-04-04 20:48:13 +09002080 d = debugfs_create_file("bias_level", 0444,
2081 dapm->debugfs_dapm, dapm,
2082 &dapm_bias_fops);
2083 if (!d)
2084 dev_warn(dapm->dev,
2085 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002086}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002087
2088static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2089{
2090 struct snd_soc_dapm_context *dapm = w->dapm;
2091 struct dentry *d;
2092
2093 if (!dapm->debugfs_dapm || !w->name)
2094 return;
2095
2096 d = debugfs_create_file(w->name, 0444,
2097 dapm->debugfs_dapm, w,
2098 &dapm_widget_power_fops);
2099 if (!d)
2100 dev_warn(w->dapm->dev,
2101 "ASoC: Failed to create %s debugfs file\n",
2102 w->name);
2103}
2104
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002105static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2106{
2107 debugfs_remove_recursive(dapm->debugfs_dapm);
2108}
2109
Mark Brown79fb9382009-08-21 16:38:13 +01002110#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002111void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2112 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002113{
2114}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002115
2116static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2117{
2118}
2119
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002120static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2121{
2122}
2123
Mark Brown79fb9382009-08-21 16:38:13 +01002124#endif
2125
Richard Purdie2b97eab2006-10-06 18:32:18 +02002126/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002127static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002128 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002129{
2130 struct snd_soc_dapm_path *path;
2131 int found = 0;
2132
Mark Brownf9fa2b12014-03-06 16:49:11 +08002133 lockdep_assert_held(&card->dapm_mutex);
2134
Richard Purdie2b97eab2006-10-06 18:32:18 +02002135 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002136 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Zhaocb01e2b2008-10-07 08:05:20 +08002137 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02002138 continue;
2139
2140 found = 1;
2141 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01002142 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002143 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01002144 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01002145 } else {
2146 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01002147 dapm_mark_dirty(path->source,
2148 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002149 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01002150 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002151 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002152 }
2153
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002154 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002155 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002156
Liam Girdwood618dae12012-04-25 12:12:51 +01002157 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002158}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002159
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002160int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002161 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2162 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002163{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002164 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002165 int ret;
2166
Liam Girdwood3cd04342012-03-09 12:02:08 +00002167 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002168 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002169 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002170 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002171 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002172 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002173 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002174 return ret;
2175}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002176EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002177
Milan plzik1b075e32008-01-10 14:39:46 +01002178/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002179static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002180 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002181{
2182 struct snd_soc_dapm_path *path;
2183 int found = 0;
2184
Mark Brownf9fa2b12014-03-06 16:49:11 +08002185 lockdep_assert_held(&card->dapm_mutex);
2186
Richard Purdie2b97eab2006-10-06 18:32:18 +02002187 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002188 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002189 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002190 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002191 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002192 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002193 }
2194
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002195 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002196 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002197
Liam Girdwood618dae12012-04-25 12:12:51 +01002198 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002199}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002200
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002201int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002202 struct snd_kcontrol *kcontrol, int connect,
2203 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002204{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002205 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002206 int ret;
2207
Liam Girdwood3cd04342012-03-09 12:02:08 +00002208 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002209 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002210 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002211 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002212 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002213 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002214 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002215 return ret;
2216}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002217EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002218
2219/* show dapm widget status in sys fs */
2220static ssize_t dapm_widget_show(struct device *dev,
2221 struct device_attribute *attr, char *buf)
2222{
Mark Brown36ae1a92012-01-06 17:12:45 -08002223 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002224 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002225 struct snd_soc_dapm_widget *w;
2226 int count = 0;
2227 char *state = "not set";
2228
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002229 list_for_each_entry(w, &codec->card->widgets, list) {
2230 if (w->dapm != &codec->dapm)
2231 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002232
2233 /* only display widgets that burnm power */
2234 switch (w->id) {
2235 case snd_soc_dapm_hp:
2236 case snd_soc_dapm_mic:
2237 case snd_soc_dapm_spk:
2238 case snd_soc_dapm_line:
2239 case snd_soc_dapm_micbias:
2240 case snd_soc_dapm_dac:
2241 case snd_soc_dapm_adc:
2242 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002243 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002244 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002245 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002246 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002247 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002248 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002249 if (w->name)
2250 count += sprintf(buf + count, "%s: %s\n",
2251 w->name, w->power ? "On":"Off");
2252 break;
2253 default:
2254 break;
2255 }
2256 }
2257
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002258 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002259 case SND_SOC_BIAS_ON:
2260 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002261 break;
Mark Brown0be98982008-05-19 12:31:28 +02002262 case SND_SOC_BIAS_PREPARE:
2263 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002264 break;
Mark Brown0be98982008-05-19 12:31:28 +02002265 case SND_SOC_BIAS_STANDBY:
2266 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002267 break;
Mark Brown0be98982008-05-19 12:31:28 +02002268 case SND_SOC_BIAS_OFF:
2269 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002270 break;
2271 }
2272 count += sprintf(buf + count, "PM State: %s\n", state);
2273
2274 return count;
2275}
2276
2277static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2278
2279int snd_soc_dapm_sys_add(struct device *dev)
2280{
Troy Kisky12ef1932008-10-13 17:42:14 -07002281 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002282}
2283
2284static void snd_soc_dapm_sys_remove(struct device *dev)
2285{
Mark Brownaef90842009-05-16 17:53:16 +01002286 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002287}
2288
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002289static void dapm_free_path(struct snd_soc_dapm_path *path)
2290{
2291 list_del(&path->list_sink);
2292 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002293 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002294 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002295 kfree(path);
2296}
2297
Richard Purdie2b97eab2006-10-06 18:32:18 +02002298/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002299static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002300{
2301 struct snd_soc_dapm_widget *w, *next_w;
2302 struct snd_soc_dapm_path *p, *next_p;
2303
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002304 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2305 if (w->dapm != dapm)
2306 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002307 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002308 /*
2309 * remove source and sink paths associated to this widget.
2310 * While removing the path, remove reference to it from both
2311 * source and sink widgets so that path is removed only once.
2312 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002313 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2314 dapm_free_path(p);
2315
2316 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2317 dapm_free_path(p);
2318
Stephen Warrenfad59882011-04-28 17:37:59 -06002319 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002320 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002321 kfree(w);
2322 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002323}
2324
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002325static struct snd_soc_dapm_widget *dapm_find_widget(
2326 struct snd_soc_dapm_context *dapm, const char *pin,
2327 bool search_other_contexts)
2328{
2329 struct snd_soc_dapm_widget *w;
2330 struct snd_soc_dapm_widget *fallback = NULL;
2331
2332 list_for_each_entry(w, &dapm->card->widgets, list) {
2333 if (!strcmp(w->name, pin)) {
2334 if (w->dapm == dapm)
2335 return w;
2336 else
2337 fallback = w;
2338 }
2339 }
2340
2341 if (search_other_contexts)
2342 return fallback;
2343
2344 return NULL;
2345}
2346
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002347static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002348 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002349{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002350 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002351
Mark Brownf9fa2b12014-03-06 16:49:11 +08002352 dapm_assert_locked(dapm);
2353
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002354 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002355 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002356 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002357 }
2358
Mark Brown1a8b2d92012-02-16 11:50:07 -08002359 if (w->connected != status)
2360 dapm_mark_dirty(w, "pin configuration");
2361
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002362 w->connected = status;
2363 if (status == 0)
2364 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002365
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002366 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002367}
2368
Richard Purdie2b97eab2006-10-06 18:32:18 +02002369/**
Charles Keepax3eb29df2014-02-18 15:22:15 +00002370 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2371 * @dapm: DAPM context
2372 *
2373 * Walks all dapm audio paths and powers widgets according to their
2374 * stream or path usage.
2375 *
2376 * Requires external locking.
2377 *
2378 * Returns 0 for success.
2379 */
2380int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2381{
2382 /*
2383 * Suppress early reports (eg, jacks syncing their state) to avoid
2384 * silly DAPM runs during card startup.
2385 */
2386 if (!dapm->card || !dapm->card->instantiated)
2387 return 0;
2388
2389 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2390}
2391EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2392
2393/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002394 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002395 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002396 *
2397 * Walks all dapm audio paths and powers widgets according to their
2398 * stream or path usage.
2399 *
2400 * Returns 0 for success.
2401 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002402int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002403{
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002404 int ret;
2405
Liam Girdwood3cd04342012-03-09 12:02:08 +00002406 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Charles Keepax3eb29df2014-02-18 15:22:15 +00002407 ret = snd_soc_dapm_sync_unlocked(dapm);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002408 mutex_unlock(&dapm->card->dapm_mutex);
2409 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002410}
Liam Girdwooda5302182008-07-07 13:35:17 +01002411EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002412
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002413static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2414 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2415 const char *control,
2416 int (*connected)(struct snd_soc_dapm_widget *source,
2417 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002418{
2419 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002420 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002421
2422 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2423 if (!path)
2424 return -ENOMEM;
2425
2426 path->source = wsource;
2427 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002428 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002429 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002430 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002431 INIT_LIST_HEAD(&path->list_source);
2432 INIT_LIST_HEAD(&path->list_sink);
2433
2434 /* check for external widgets */
2435 if (wsink->id == snd_soc_dapm_input) {
2436 if (wsource->id == snd_soc_dapm_micbias ||
2437 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002438 wsource->id == snd_soc_dapm_line ||
2439 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002440 wsink->ext = 1;
2441 }
2442 if (wsource->id == snd_soc_dapm_output) {
2443 if (wsink->id == snd_soc_dapm_spk ||
2444 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002445 wsink->id == snd_soc_dapm_line ||
2446 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002447 wsource->ext = 1;
2448 }
2449
Lars-Peter Clausen34742cb2013-08-27 15:50:54 +02002450 dapm_mark_dirty(wsource, "Route added");
2451 dapm_mark_dirty(wsink, "Route added");
2452
Richard Purdie2b97eab2006-10-06 18:32:18 +02002453 /* connect static paths */
2454 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002455 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002456 list_add(&path->list_sink, &wsink->sources);
2457 list_add(&path->list_source, &wsource->sinks);
2458 path->connect = 1;
2459 return 0;
2460 }
2461
2462 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002463 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002464 case snd_soc_dapm_adc:
2465 case snd_soc_dapm_dac:
2466 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002467 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002468 case snd_soc_dapm_input:
2469 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002470 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002471 case snd_soc_dapm_micbias:
2472 case snd_soc_dapm_vmid:
2473 case snd_soc_dapm_pre:
2474 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002475 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002476 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002477 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002478 case snd_soc_dapm_aif_in:
2479 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002480 case snd_soc_dapm_dai_in:
2481 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002482 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002483 case snd_soc_dapm_kcontrol:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002484 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002485 list_add(&path->list_sink, &wsink->sources);
2486 list_add(&path->list_source, &wsource->sinks);
2487 path->connect = 1;
2488 return 0;
2489 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002490 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002491 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002492 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002493 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002494 if (ret != 0)
2495 goto err;
2496 break;
2497 case snd_soc_dapm_switch:
2498 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002499 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002500 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002501 if (ret != 0)
2502 goto err;
2503 break;
2504 case snd_soc_dapm_hp:
2505 case snd_soc_dapm_mic:
2506 case snd_soc_dapm_line:
2507 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002508 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002509 list_add(&path->list_sink, &wsink->sources);
2510 list_add(&path->list_source, &wsource->sinks);
2511 path->connect = 0;
2512 return 0;
2513 }
Mark Brownfabd0382012-07-05 17:20:06 +01002514
Richard Purdie2b97eab2006-10-06 18:32:18 +02002515 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002516err:
2517 kfree(path);
2518 return ret;
2519}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002520
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002521static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002522 const struct snd_soc_dapm_route *route,
2523 unsigned int is_prefixed)
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002524{
2525 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2526 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2527 const char *sink;
2528 const char *source;
2529 char prefixed_sink[80];
2530 char prefixed_source[80];
2531 int ret;
2532
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002533 if (dapm->codec && dapm->codec->name_prefix && !is_prefixed) {
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002534 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2535 dapm->codec->name_prefix, route->sink);
2536 sink = prefixed_sink;
2537 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2538 dapm->codec->name_prefix, route->source);
2539 source = prefixed_source;
2540 } else {
2541 sink = route->sink;
2542 source = route->source;
2543 }
2544
2545 /*
2546 * find src and dest widgets over all widgets but favor a widget from
2547 * current DAPM context
2548 */
2549 list_for_each_entry(w, &dapm->card->widgets, list) {
2550 if (!wsink && !(strcmp(w->name, sink))) {
2551 wtsink = w;
2552 if (w->dapm == dapm)
2553 wsink = w;
2554 continue;
2555 }
2556 if (!wsource && !(strcmp(w->name, source))) {
2557 wtsource = w;
2558 if (w->dapm == dapm)
2559 wsource = w;
2560 }
2561 }
2562 /* use widget from another DAPM context if not found from this */
2563 if (!wsink)
2564 wsink = wtsink;
2565 if (!wsource)
2566 wsource = wtsource;
2567
2568 if (wsource == NULL) {
2569 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2570 route->source);
2571 return -ENODEV;
2572 }
2573 if (wsink == NULL) {
2574 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2575 route->sink);
2576 return -ENODEV;
2577 }
2578
2579 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2580 route->connected);
2581 if (ret)
2582 goto err;
2583
2584 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002585err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002586 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002587 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002588 return ret;
2589}
Mark Brown105f1c22008-05-13 14:52:19 +02002590
Mark Brownefcc3c62012-07-05 17:24:19 +01002591static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2592 const struct snd_soc_dapm_route *route)
2593{
2594 struct snd_soc_dapm_path *path, *p;
2595 const char *sink;
2596 const char *source;
2597 char prefixed_sink[80];
2598 char prefixed_source[80];
2599
2600 if (route->control) {
2601 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002602 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002603 return -EINVAL;
2604 }
2605
2606 if (dapm->codec && dapm->codec->name_prefix) {
2607 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2608 dapm->codec->name_prefix, route->sink);
2609 sink = prefixed_sink;
2610 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2611 dapm->codec->name_prefix, route->source);
2612 source = prefixed_source;
2613 } else {
2614 sink = route->sink;
2615 source = route->source;
2616 }
2617
2618 path = NULL;
2619 list_for_each_entry(p, &dapm->card->paths, list) {
2620 if (strcmp(p->source->name, source) != 0)
2621 continue;
2622 if (strcmp(p->sink->name, sink) != 0)
2623 continue;
2624 path = p;
2625 break;
2626 }
2627
2628 if (path) {
2629 dapm_mark_dirty(path->source, "Route removed");
2630 dapm_mark_dirty(path->sink, "Route removed");
2631
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002632 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002633 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002634 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002635 source, sink);
2636 }
2637
2638 return 0;
2639}
2640
Mark Brown105f1c22008-05-13 14:52:19 +02002641/**
Mark Brown105f1c22008-05-13 14:52:19 +02002642 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002643 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002644 * @route: audio routes
2645 * @num: number of routes
2646 *
2647 * Connects 2 dapm widgets together via a named audio path. The sink is
2648 * the widget receiving the audio signal, whilst the source is the sender
2649 * of the audio signal.
2650 *
2651 * Returns 0 for success else error. On error all resources can be freed
2652 * with a call to snd_soc_card_free().
2653 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002654int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002655 const struct snd_soc_dapm_route *route, int num)
2656{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002657 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002658
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002659 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002660 for (i = 0; i < num; i++) {
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08002661 r = snd_soc_dapm_add_route(dapm, route, false);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002662 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002663 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2664 route->source,
2665 route->control ? route->control : "direct",
2666 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002667 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002668 }
2669 route++;
2670 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002671 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002672
Dan Carpenter60884c22012-04-13 22:25:43 +03002673 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002674}
2675EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2676
Mark Brownefcc3c62012-07-05 17:24:19 +01002677/**
2678 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2679 * @dapm: DAPM context
2680 * @route: audio routes
2681 * @num: number of routes
2682 *
2683 * Removes routes from the DAPM context.
2684 */
2685int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2686 const struct snd_soc_dapm_route *route, int num)
2687{
2688 int i, ret = 0;
2689
2690 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2691 for (i = 0; i < num; i++) {
2692 snd_soc_dapm_del_route(dapm, route);
2693 route++;
2694 }
2695 mutex_unlock(&dapm->card->dapm_mutex);
2696
2697 return ret;
2698}
2699EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2700
Mark Brownbf3a9e12011-06-13 16:42:29 +01002701static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2702 const struct snd_soc_dapm_route *route)
2703{
2704 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2705 route->source,
2706 true);
2707 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2708 route->sink,
2709 true);
2710 struct snd_soc_dapm_path *path;
2711 int count = 0;
2712
2713 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002714 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002715 route->source);
2716 return -ENODEV;
2717 }
2718
2719 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002720 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002721 route->sink);
2722 return -ENODEV;
2723 }
2724
2725 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002726 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002727 route->source, route->sink);
2728
2729 list_for_each_entry(path, &source->sinks, list_source) {
2730 if (path->sink == sink) {
2731 path->weak = 1;
2732 count++;
2733 }
2734 }
2735
2736 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002737 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002738 route->source, route->sink);
2739 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002740 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002741 count, route->source, route->sink);
2742
2743 return 0;
2744}
2745
2746/**
2747 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2748 * @dapm: DAPM context
2749 * @route: audio routes
2750 * @num: number of routes
2751 *
2752 * Mark existing routes matching those specified in the passed array
2753 * as being weak, meaning that they are ignored for the purpose of
2754 * power decisions. The main intended use case is for sidetone paths
2755 * which couple audio between other independent paths if they are both
2756 * active in order to make the combination work better at the user
2757 * level but which aren't intended to be "used".
2758 *
2759 * Note that CODEC drivers should not use this as sidetone type paths
2760 * can frequently also be used as bypass paths.
2761 */
2762int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2763 const struct snd_soc_dapm_route *route, int num)
2764{
2765 int i, err;
2766 int ret = 0;
2767
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002768 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002769 for (i = 0; i < num; i++) {
2770 err = snd_soc_dapm_weak_route(dapm, route);
2771 if (err)
2772 ret = err;
2773 route++;
2774 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002775 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002776
2777 return ret;
2778}
2779EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2780
Mark Brown105f1c22008-05-13 14:52:19 +02002781/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002782 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002783 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002784 *
2785 * Checks the codec for any new dapm widgets and creates them if found.
2786 *
2787 * Returns 0 for success.
2788 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002789int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002790{
2791 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002792 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002793
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002794 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002795
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002796 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002797 {
2798 if (w->new)
2799 continue;
2800
Stephen Warrenfad59882011-04-28 17:37:59 -06002801 if (w->num_kcontrols) {
2802 w->kcontrols = kzalloc(w->num_kcontrols *
2803 sizeof(struct snd_kcontrol *),
2804 GFP_KERNEL);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002805 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002806 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002807 return -ENOMEM;
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002808 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002809 }
2810
Richard Purdie2b97eab2006-10-06 18:32:18 +02002811 switch(w->id) {
2812 case snd_soc_dapm_switch:
2813 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002814 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002815 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002816 break;
2817 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002818 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002819 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002820 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002821 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002822 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002823 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002824 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002825 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002826 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002827 break;
2828 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002829
2830 /* Read the initial power state from the device */
2831 if (w->reg >= 0) {
Arun Shamanna Lakshmif7d3c172014-01-14 15:31:54 -08002832 soc_widget_read(w, w->reg, &val);
2833 val = val >> w->shift;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002834 val &= w->mask;
2835 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002836 w->power = 1;
2837 }
2838
Richard Purdie2b97eab2006-10-06 18:32:18 +02002839 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002840
Mark Brown7508b122011-10-05 12:09:12 +01002841 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002842 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002843 }
2844
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002845 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2846 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002847 return 0;
2848}
2849EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2850
2851/**
2852 * snd_soc_dapm_get_volsw - dapm mixer get callback
2853 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002854 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002855 *
2856 * Callback to get the value of a dapm mixer control.
2857 *
2858 * Returns 0 for success.
2859 */
2860int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2861 struct snd_ctl_elem_value *ucontrol)
2862{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002863 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002864 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002865 struct soc_mixer_control *mc =
2866 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002867 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002868 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002869 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002870 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002871 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002872 unsigned int val;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002873
2874 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002875 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002876 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002877 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002878
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002879 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002880 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002881 val = (snd_soc_read(codec, reg) >> shift) & mask;
2882 else
2883 val = dapm_kcontrol_get_value(kcontrol);
2884 mutex_unlock(&card->dapm_mutex);
2885
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002886 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002887 ucontrol->value.integer.value[0] = max - val;
2888 else
2889 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002890
2891 return 0;
2892}
2893EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2894
2895/**
2896 * snd_soc_dapm_put_volsw - dapm mixer set callback
2897 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002898 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002899 *
2900 * Callback to set the value of a dapm mixer control.
2901 *
2902 * Returns 0 for success.
2903 */
2904int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2905 struct snd_ctl_elem_value *ucontrol)
2906{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002907 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002908 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002909 struct soc_mixer_control *mc =
2910 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002911 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002912 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002913 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002914 unsigned int mask = (1 << fls(max)) - 1;
2915 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002916 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002917 int connect, change;
2918 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08002919 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002920
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002921 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002922 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002923 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002924 kcontrol->id.name);
2925
Richard Purdie2b97eab2006-10-06 18:32:18 +02002926 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002927 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002928
2929 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002930 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002931
Liam Girdwood3cd04342012-03-09 12:02:08 +00002932 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002933
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002934 change = dapm_kcontrol_set_value(kcontrol, val);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002935
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002936 if (reg != SND_SOC_NOPM) {
2937 mask = mask << shift;
2938 val = val << shift;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002939
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002940 change = snd_soc_test_bits(codec, reg, mask, val);
2941 }
2942
Mark Brown97404f22010-12-14 16:13:57 +00002943 if (change) {
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002944 if (reg != SND_SOC_NOPM) {
2945 update.kcontrol = kcontrol;
2946 update.reg = reg;
2947 update.mask = mask;
2948 update.val = val;
Mark Brown283375c2009-12-07 18:09:03 +00002949
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002950 card->update = &update;
2951 }
Mark Brown97404f22010-12-14 16:13:57 +00002952
Nenghua Cao52765972013-12-13 20:13:49 +08002953 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002954
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002955 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002956 }
2957
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002958 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08002959
2960 if (ret > 0)
2961 soc_dpcm_runtime_update(card);
2962
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002963 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002964}
2965EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2966
2967/**
2968 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2969 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002970 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002971 *
2972 * Callback to get the value of a dapm enumerated double mixer control.
2973 *
2974 * Returns 0 for success.
2975 */
2976int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2977 struct snd_ctl_elem_value *ucontrol)
2978{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002979 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002980 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002981 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002982
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002983 val = snd_soc_read(codec, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002984 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002985 if (e->shift_l != e->shift_r)
2986 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002987 (val >> e->shift_r) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002988
2989 return 0;
2990}
2991EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2992
2993/**
2994 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2995 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002996 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002997 *
2998 * Callback to set the value of a dapm enumerated double mixer control.
2999 *
3000 * Returns 0 for success.
3001 */
3002int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3003 struct snd_ctl_elem_value *ucontrol)
3004{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003005 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003006 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003007 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01003008 unsigned int val, mux, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003009 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00003010 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08003011 int ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003012
Jon Smirlf8ba0b72008-07-29 11:42:27 +01003013 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003014 return -EINVAL;
3015 mux = ucontrol->value.enumerated.item[0];
3016 val = mux << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003017 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003018 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01003019 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003020 return -EINVAL;
3021 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02003022 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003023 }
3024
Liam Girdwood3cd04342012-03-09 12:02:08 +00003025 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003026
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003027 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003028 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02003029 update.kcontrol = kcontrol;
3030 update.reg = e->reg;
3031 update.mask = mask;
3032 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003033 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00003034
Nenghua Cao52765972013-12-13 20:13:49 +08003035 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003036
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003037 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003038 }
3039
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003040 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003041
3042 if (ret > 0)
3043 soc_dpcm_runtime_update(card);
3044
Mark Brown97404f22010-12-14 16:13:57 +00003045 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003046}
3047EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3048
3049/**
Mark Brownd2b247a2009-10-06 15:21:04 +01003050 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
3051 * @kcontrol: mixer control
3052 * @ucontrol: control element information
3053 *
3054 * Returns 0 for success.
3055 */
3056int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
3057 struct snd_ctl_elem_value *ucontrol)
3058{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003059 ucontrol->value.enumerated.item[0] = dapm_kcontrol_get_value(kcontrol);
Mark Brownd2b247a2009-10-06 15:21:04 +01003060 return 0;
3061}
3062EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
3063
3064/**
3065 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
3066 * @kcontrol: mixer control
3067 * @ucontrol: control element information
3068 *
3069 * Returns 0 for success.
3070 */
3071int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
3072 struct snd_ctl_elem_value *ucontrol)
3073{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003074 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003075 struct snd_soc_card *card = codec->card;
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003076 unsigned int value;
Mark Brownd2b247a2009-10-06 15:21:04 +01003077 struct soc_enum *e =
3078 (struct soc_enum *)kcontrol->private_value;
3079 int change;
Nenghua Cao52765972013-12-13 20:13:49 +08003080 int ret = 0;
Mark Brownd2b247a2009-10-06 15:21:04 +01003081
3082 if (ucontrol->value.enumerated.item[0] >= e->max)
3083 return -EINVAL;
3084
Liam Girdwood3cd04342012-03-09 12:02:08 +00003085 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01003086
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003087 value = ucontrol->value.enumerated.item[0];
3088 change = dapm_kcontrol_set_value(kcontrol, value);
3089 if (change)
Nenghua Cao52765972013-12-13 20:13:49 +08003090 ret = soc_dapm_mux_update_power(card, kcontrol, value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003091
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003092 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003093
3094 if (ret > 0)
3095 soc_dpcm_runtime_update(card);
3096
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02003097 return change;
Mark Brownd2b247a2009-10-06 15:21:04 +01003098}
3099EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
3100
3101/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003102 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
3103 * callback
3104 * @kcontrol: mixer control
3105 * @ucontrol: control element information
3106 *
3107 * Callback to get the value of a dapm semi enumerated double mixer control.
3108 *
3109 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3110 * used for handling bitfield coded enumeration for example.
3111 *
3112 * Returns 0 for success.
3113 */
3114int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
3115 struct snd_ctl_elem_value *ucontrol)
3116{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003117 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02003118 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03003119 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003120
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003121 reg_val = snd_soc_read(codec, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003122 val = (reg_val >> e->shift_l) & e->mask;
3123 for (mux = 0; mux < e->max; mux++) {
3124 if (val == e->values[mux])
3125 break;
3126 }
3127 ucontrol->value.enumerated.item[0] = mux;
3128 if (e->shift_l != e->shift_r) {
3129 val = (reg_val >> e->shift_r) & e->mask;
3130 for (mux = 0; mux < e->max; mux++) {
3131 if (val == e->values[mux])
3132 break;
3133 }
3134 ucontrol->value.enumerated.item[1] = mux;
3135 }
3136
3137 return 0;
3138}
3139EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
3140
3141/**
3142 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
3143 * callback
3144 * @kcontrol: mixer control
3145 * @ucontrol: control element information
3146 *
3147 * Callback to set the value of a dapm semi enumerated double mixer control.
3148 *
3149 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3150 * used for handling bitfield coded enumeration for example.
3151 *
3152 * Returns 0 for success.
3153 */
3154int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
3155 struct snd_ctl_elem_value *ucontrol)
3156{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003157 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003158 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02003159 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01003160 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03003161 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00003162 struct snd_soc_dapm_update update;
Nenghua Cao52765972013-12-13 20:13:49 +08003163 int ret = 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003164
3165 if (ucontrol->value.enumerated.item[0] > e->max - 1)
3166 return -EINVAL;
3167 mux = ucontrol->value.enumerated.item[0];
3168 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
3169 mask = e->mask << e->shift_l;
3170 if (e->shift_l != e->shift_r) {
3171 if (ucontrol->value.enumerated.item[1] > e->max - 1)
3172 return -EINVAL;
3173 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
3174 mask |= e->mask << e->shift_r;
3175 }
3176
Liam Girdwood3cd04342012-03-09 12:02:08 +00003177 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003178
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003179 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003180 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02003181 update.kcontrol = kcontrol;
3182 update.reg = e->reg;
3183 update.mask = mask;
3184 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003185 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00003186
Nenghua Cao52765972013-12-13 20:13:49 +08003187 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003188
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003189 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003190 }
3191
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003192 mutex_unlock(&card->dapm_mutex);
Nenghua Cao52765972013-12-13 20:13:49 +08003193
3194 if (ret > 0)
3195 soc_dpcm_runtime_update(card);
3196
Mark Brown97404f22010-12-14 16:13:57 +00003197 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003198}
3199EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
3200
3201/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003202 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3203 *
3204 * @kcontrol: mixer control
3205 * @uinfo: control element information
3206 *
3207 * Callback to provide information about a pin switch control.
3208 */
3209int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3210 struct snd_ctl_elem_info *uinfo)
3211{
3212 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3213 uinfo->count = 1;
3214 uinfo->value.integer.min = 0;
3215 uinfo->value.integer.max = 1;
3216
3217 return 0;
3218}
3219EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3220
3221/**
3222 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3223 *
3224 * @kcontrol: mixer control
3225 * @ucontrol: Value
3226 */
3227int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3228 struct snd_ctl_elem_value *ucontrol)
3229{
Mark Brown48a8c392012-02-14 17:11:15 -08003230 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003231 const char *pin = (const char *)kcontrol->private_value;
3232
Liam Girdwood3cd04342012-03-09 12:02:08 +00003233 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003234
3235 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003236 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003237
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003238 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003239
3240 return 0;
3241}
3242EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3243
3244/**
3245 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3246 *
3247 * @kcontrol: mixer control
3248 * @ucontrol: Value
3249 */
3250int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3251 struct snd_ctl_elem_value *ucontrol)
3252{
Mark Brown48a8c392012-02-14 17:11:15 -08003253 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003254 const char *pin = (const char *)kcontrol->private_value;
3255
Mark Brown8b37dbd2009-02-28 21:14:20 +00003256 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003257 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003258 else
Mark Brown48a8c392012-02-14 17:11:15 -08003259 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003260
Mark Brown48a8c392012-02-14 17:11:15 -08003261 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003262 return 0;
3263}
3264EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3265
Mark Brown5ba06fc2012-02-16 11:07:13 -08003266static struct snd_soc_dapm_widget *
3267snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3268 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003269{
3270 struct snd_soc_dapm_widget *w;
Mark Brown62ea8742012-01-21 21:14:48 +00003271 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003272
3273 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003274 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003275
Mark Brown62ea8742012-01-21 21:14:48 +00003276 switch (w->id) {
3277 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003278 w->regulator = devm_regulator_get(dapm->dev, w->name);
3279 if (IS_ERR(w->regulator)) {
3280 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003281 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003282 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003283 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003284 }
Mark Brown8784c772013-01-10 19:33:47 +00003285
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003286 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003287 ret = regulator_allow_bypass(w->regulator, true);
3288 if (ret != 0)
3289 dev_warn(w->dapm->dev,
Charles Keepax30686c32014-02-18 16:05:27 +00003290 "ASoC: Failed to bypass %s: %d\n",
Mark Brown8784c772013-01-10 19:33:47 +00003291 w->name, ret);
3292 }
Mark Brown62ea8742012-01-21 21:14:48 +00003293 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003294 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003295#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003296 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003297 if (IS_ERR(w->clk)) {
3298 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003299 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003300 w->name, ret);
3301 return NULL;
3302 }
Mark Brownec029952012-06-04 08:16:20 +01003303#else
3304 return NULL;
3305#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003306 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003307 default:
3308 break;
3309 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003310
Mark Brown88e8b9a2011-03-02 18:18:24 +00003311 if (dapm->codec && dapm->codec->name_prefix)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003312 w->name = kasprintf(GFP_KERNEL, "%s %s",
3313 dapm->codec->name_prefix, widget->name);
3314 else
3315 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3316
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003317 if (w->name == NULL) {
3318 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003319 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003320 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003321
Mark Brown7ca3a182011-10-08 14:04:50 +01003322 switch (w->id) {
3323 case snd_soc_dapm_switch:
3324 case snd_soc_dapm_mixer:
3325 case snd_soc_dapm_mixer_named_ctl:
3326 w->power_check = dapm_generic_check_power;
3327 break;
3328 case snd_soc_dapm_mux:
3329 case snd_soc_dapm_virt_mux:
3330 case snd_soc_dapm_value_mux:
3331 w->power_check = dapm_generic_check_power;
3332 break;
Mark Brown46162742013-06-05 19:36:11 +01003333 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003334 w->power_check = dapm_adc_check_power;
3335 break;
Mark Brown46162742013-06-05 19:36:11 +01003336 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003337 w->power_check = dapm_dac_check_power;
3338 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003339 case snd_soc_dapm_adc:
3340 case snd_soc_dapm_aif_out:
3341 case snd_soc_dapm_dac:
3342 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003343 case snd_soc_dapm_pga:
3344 case snd_soc_dapm_out_drv:
3345 case snd_soc_dapm_input:
3346 case snd_soc_dapm_output:
3347 case snd_soc_dapm_micbias:
3348 case snd_soc_dapm_spk:
3349 case snd_soc_dapm_hp:
3350 case snd_soc_dapm_mic:
3351 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003352 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003353 w->power_check = dapm_generic_check_power;
3354 break;
3355 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003356 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003357 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003358 case snd_soc_dapm_kcontrol:
Mark Brown7ca3a182011-10-08 14:04:50 +01003359 w->power_check = dapm_supply_check_power;
3360 break;
3361 default:
3362 w->power_check = dapm_always_on_check_power;
3363 break;
3364 }
3365
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003366 w->dapm = dapm;
3367 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003368 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003369 INIT_LIST_HEAD(&w->sources);
3370 INIT_LIST_HEAD(&w->sinks);
3371 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003372 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003373 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003374
3375 /* machine layer set ups unconnected pins and insertions */
3376 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003377 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003378}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003379
3380/**
Mark Brown4ba13272008-05-13 14:51:19 +02003381 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003382 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003383 * @widget: widget array
3384 * @num: number of widgets
3385 *
3386 * Creates new DAPM controls based upon the templates.
3387 *
3388 * Returns 0 for success else error.
3389 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003390int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003391 const struct snd_soc_dapm_widget *widget,
3392 int num)
3393{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003394 struct snd_soc_dapm_widget *w;
3395 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003396 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003397
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003398 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003399 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003400 w = snd_soc_dapm_new_control(dapm, widget);
3401 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003402 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003403 "ASoC: Failed to create DAPM control %s\n",
3404 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003405 ret = -ENOMEM;
3406 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003407 }
Mark Brown4ba13272008-05-13 14:51:19 +02003408 widget++;
3409 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003410 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003411 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003412}
3413EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3414
Mark Brownc74184e2012-04-04 22:12:09 +01003415static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3416 struct snd_kcontrol *kcontrol, int event)
3417{
3418 struct snd_soc_dapm_path *source_p, *sink_p;
3419 struct snd_soc_dai *source, *sink;
3420 const struct snd_soc_pcm_stream *config = w->params;
3421 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003422 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003423 u64 fmt;
3424 int ret;
3425
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003426 if (WARN_ON(!config) ||
3427 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3428 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003429
3430 /* We only support a single source and sink, pick the first */
3431 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3432 list_sink);
3433 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3434 list_source);
3435
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003436 if (WARN_ON(!source_p || !sink_p) ||
3437 WARN_ON(!sink_p->source || !source_p->sink) ||
3438 WARN_ON(!source_p->source || !sink_p->sink))
3439 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003440
3441 source = source_p->source->priv;
3442 sink = sink_p->sink->priv;
3443
3444 /* Be a little careful as we don't want to overflow the mask array */
3445 if (config->formats) {
3446 fmt = ffs(config->formats) - 1;
3447 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003448 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003449 config->formats);
3450 fmt = 0;
3451 }
3452
3453 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003454 params = kzalloc(sizeof(*params), GFP_KERNEL);
3455 if (!params) {
3456 ret = -ENOMEM;
3457 goto out;
3458 }
3459 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003460
Mark Brown9747cec2012-04-26 19:12:21 +01003461 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003462 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003463 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003464 config->rate_max;
3465
Mark Brown9747cec2012-04-26 19:12:21 +01003466 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003467 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003468 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003469 = config->channels_max;
3470
3471 memset(&substream, 0, sizeof(substream));
3472
3473 switch (event) {
3474 case SND_SOC_DAPM_PRE_PMU:
3475 if (source->driver->ops && source->driver->ops->hw_params) {
3476 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3477 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003478 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003479 if (ret != 0) {
3480 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003481 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003482 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003483 }
3484 }
3485
3486 if (sink->driver->ops && sink->driver->ops->hw_params) {
3487 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003488 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003489 sink);
3490 if (ret != 0) {
3491 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003492 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003493 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003494 }
3495 }
3496 break;
3497
3498 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003499 ret = snd_soc_dai_digital_mute(sink, 0,
3500 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003501 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003502 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003503 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003504 break;
3505
3506 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003507 ret = snd_soc_dai_digital_mute(sink, 1,
3508 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003509 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003510 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003511 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003512 break;
3513
3514 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003515 WARN(1, "Unknown event %d\n", event);
Mark Brownc74184e2012-04-04 22:12:09 +01003516 return -EINVAL;
3517 }
3518
Mark Brown9747cec2012-04-26 19:12:21 +01003519out:
3520 kfree(params);
3521 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003522}
3523
3524int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3525 const struct snd_soc_pcm_stream *params,
3526 struct snd_soc_dapm_widget *source,
3527 struct snd_soc_dapm_widget *sink)
3528{
3529 struct snd_soc_dapm_route routes[2];
3530 struct snd_soc_dapm_widget template;
3531 struct snd_soc_dapm_widget *w;
3532 size_t len;
3533 char *link_name;
3534
3535 len = strlen(source->name) + strlen(sink->name) + 2;
3536 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3537 if (!link_name)
3538 return -ENOMEM;
3539 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3540
3541 memset(&template, 0, sizeof(template));
3542 template.reg = SND_SOC_NOPM;
3543 template.id = snd_soc_dapm_dai_link;
3544 template.name = link_name;
3545 template.event = snd_soc_dai_link_event;
3546 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3547 SND_SOC_DAPM_PRE_PMD;
3548
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003549 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003550
3551 w = snd_soc_dapm_new_control(&card->dapm, &template);
3552 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003553 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003554 link_name);
3555 return -ENOMEM;
3556 }
3557
3558 w->params = params;
3559
3560 memset(&routes, 0, sizeof(routes));
3561
3562 routes[0].source = source->name;
3563 routes[0].sink = link_name;
3564 routes[1].source = link_name;
3565 routes[1].sink = sink->name;
3566
3567 return snd_soc_dapm_add_routes(&card->dapm, routes,
3568 ARRAY_SIZE(routes));
3569}
3570
Mark Brown888df392012-02-16 19:37:51 -08003571int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3572 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003573{
Mark Brown888df392012-02-16 19:37:51 -08003574 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003575 struct snd_soc_dapm_widget *w;
3576
Mark Brown888df392012-02-16 19:37:51 -08003577 WARN_ON(dapm->dev != dai->dev);
3578
3579 memset(&template, 0, sizeof(template));
3580 template.reg = SND_SOC_NOPM;
3581
3582 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003583 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003584 template.name = dai->driver->playback.stream_name;
3585 template.sname = dai->driver->playback.stream_name;
3586
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003587 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003588 template.name);
3589
3590 w = snd_soc_dapm_new_control(dapm, &template);
3591 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003592 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003593 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003594 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003595 }
3596
3597 w->priv = dai;
3598 dai->playback_widget = w;
3599 }
3600
3601 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003602 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003603 template.name = dai->driver->capture.stream_name;
3604 template.sname = dai->driver->capture.stream_name;
3605
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003606 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003607 template.name);
3608
3609 w = snd_soc_dapm_new_control(dapm, &template);
3610 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003611 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003612 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003613 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003614 }
3615
3616 w->priv = dai;
3617 dai->capture_widget = w;
3618 }
3619
3620 return 0;
3621}
3622
3623int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3624{
3625 struct snd_soc_dapm_widget *dai_w, *w;
3626 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003627
3628 /* For each DAI widget... */
3629 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003630 switch (dai_w->id) {
3631 case snd_soc_dapm_dai_in:
3632 case snd_soc_dapm_dai_out:
3633 break;
3634 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003635 continue;
Mark Brown46162742013-06-05 19:36:11 +01003636 }
Mark Brown888df392012-02-16 19:37:51 -08003637
3638 dai = dai_w->priv;
3639
3640 /* ...find all widgets with the same stream and link them */
3641 list_for_each_entry(w, &card->widgets, list) {
3642 if (w->dapm != dai_w->dapm)
3643 continue;
3644
Mark Brown46162742013-06-05 19:36:11 +01003645 switch (w->id) {
3646 case snd_soc_dapm_dai_in:
3647 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003648 continue;
Mark Brown46162742013-06-05 19:36:11 +01003649 default:
3650 break;
3651 }
Mark Brown888df392012-02-16 19:37:51 -08003652
Russell King19c2c5f2013-08-04 20:24:03 +01003653 if (!w->sname || !strstr(w->sname, dai_w->name))
Mark Brown888df392012-02-16 19:37:51 -08003654 continue;
3655
3656 if (dai->driver->playback.stream_name &&
3657 strstr(w->sname,
3658 dai->driver->playback.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003659 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003660 dai->playback_widget->name, w->name);
Mark Brown888df392012-02-16 19:37:51 -08003661
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003662 snd_soc_dapm_add_path(w->dapm,
3663 dai->playback_widget, w, NULL, NULL);
Mark Brown888df392012-02-16 19:37:51 -08003664 }
3665
3666 if (dai->driver->capture.stream_name &&
3667 strstr(w->sname,
3668 dai->driver->capture.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003669 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003670 w->name, dai->capture_widget->name);
Mark Brown888df392012-02-16 19:37:51 -08003671
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003672 snd_soc_dapm_add_path(w->dapm, w,
3673 dai->capture_widget, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003674 }
3675 }
3676 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003677
Mark Brown888df392012-02-16 19:37:51 -08003678 return 0;
3679}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003680
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003681void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3682{
3683 struct snd_soc_pcm_runtime *rtd = card->rtd;
3684 struct snd_soc_dai *cpu_dai, *codec_dai;
3685 struct snd_soc_dapm_route r;
3686 int i;
3687
3688 memset(&r, 0, sizeof(r));
3689
3690 /* for each BE DAI link... */
3691 for (i = 0; i < card->num_rtd; i++) {
3692 rtd = &card->rtd[i];
3693 cpu_dai = rtd->cpu_dai;
3694 codec_dai = rtd->codec_dai;
3695
3696 /* dynamic FE links have no fixed DAI mapping */
3697 if (rtd->dai_link->dynamic)
3698 continue;
3699
3700 /* there is no point in connecting BE DAI links with dummies */
3701 if (snd_soc_dai_is_dummy(codec_dai) ||
3702 snd_soc_dai_is_dummy(cpu_dai))
3703 continue;
3704
3705 /* connect BE DAI playback if widgets are valid */
3706 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
3707 r.source = cpu_dai->playback_widget->name;
3708 r.sink = codec_dai->playback_widget->name;
3709 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3710 cpu_dai->codec->name, r.source,
3711 codec_dai->platform->name, r.sink);
3712
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08003713 snd_soc_dapm_add_route(&card->dapm, &r, true);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003714 }
3715
3716 /* connect BE DAI capture if widgets are valid */
3717 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
3718 r.source = codec_dai->capture_widget->name;
3719 r.sink = cpu_dai->capture_widget->name;
3720 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3721 codec_dai->codec->name, r.source,
3722 cpu_dai->platform->name, r.sink);
3723
Arun Shamanna Lakshmibd23c5b2014-01-15 13:03:16 -08003724 snd_soc_dapm_add_route(&card->dapm, &r, true);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00003725 }
3726
3727 }
3728}
3729
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003730static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3731 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003732{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003733
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003734 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3735 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3736 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003737
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003738 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3739 w_cpu = cpu_dai->playback_widget;
3740 w_codec = codec_dai->playback_widget;
3741 } else {
3742 w_cpu = cpu_dai->capture_widget;
3743 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003744 }
3745
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003746 if (w_cpu) {
3747
3748 dapm_mark_dirty(w_cpu, "stream event");
3749
3750 switch (event) {
3751 case SND_SOC_DAPM_STREAM_START:
3752 w_cpu->active = 1;
3753 break;
3754 case SND_SOC_DAPM_STREAM_STOP:
3755 w_cpu->active = 0;
3756 break;
3757 case SND_SOC_DAPM_STREAM_SUSPEND:
3758 case SND_SOC_DAPM_STREAM_RESUME:
3759 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3760 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3761 break;
3762 }
3763 }
3764
3765 if (w_codec) {
3766
3767 dapm_mark_dirty(w_codec, "stream event");
3768
3769 switch (event) {
3770 case SND_SOC_DAPM_STREAM_START:
3771 w_codec->active = 1;
3772 break;
3773 case SND_SOC_DAPM_STREAM_STOP:
3774 w_codec->active = 0;
3775 break;
3776 case SND_SOC_DAPM_STREAM_SUSPEND:
3777 case SND_SOC_DAPM_STREAM_RESUME:
3778 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3779 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3780 break;
3781 }
3782 }
3783
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003784 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003785}
3786
3787/**
3788 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3789 * @rtd: PCM runtime data
3790 * @stream: stream name
3791 * @event: stream event
3792 *
3793 * Sends a stream event to the dapm core. The core then makes any
3794 * necessary widget power changes.
3795 *
3796 * Returns 0 for success else error.
3797 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003798void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3799 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003800{
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003801 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003802
Liam Girdwood3cd04342012-03-09 12:02:08 +00003803 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003804 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003805 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003806}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003807
3808/**
Charles Keepax11391102014-02-18 15:22:14 +00003809 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3810 * @dapm: DAPM context
3811 * @pin: pin name
3812 *
3813 * Enables input/output pin and its parents or children widgets iff there is
3814 * a valid audio route and active audio stream.
3815 *
3816 * Requires external locking.
3817 *
3818 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3819 * do any widget power switching.
3820 */
3821int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3822 const char *pin)
3823{
3824 return snd_soc_dapm_set_pin(dapm, pin, 1);
3825}
3826EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3827
3828/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003829 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003830 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003831 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003832 *
Mark Brown74b8f952009-06-06 11:26:15 +01003833 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003834 * a valid audio route and active audio stream.
Charles Keepax11391102014-02-18 15:22:14 +00003835 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003836 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3837 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003838 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003839int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003840{
Charles Keepax11391102014-02-18 15:22:14 +00003841 int ret;
3842
3843 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3844
3845 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3846
3847 mutex_unlock(&dapm->card->dapm_mutex);
3848
3849 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003850}
Liam Girdwooda5302182008-07-07 13:35:17 +01003851EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003852
3853/**
Charles Keepax11391102014-02-18 15:22:14 +00003854 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3855 * @dapm: DAPM context
3856 * @pin: pin name
3857 *
3858 * Enables input/output pin regardless of any other state. This is
3859 * intended for use with microphone bias supplies used in microphone
3860 * jack detection.
3861 *
3862 * Requires external locking.
3863 *
3864 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3865 * do any widget power switching.
3866 */
3867int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3868 const char *pin)
3869{
3870 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3871
3872 if (!w) {
3873 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3874 return -EINVAL;
3875 }
3876
3877 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3878 w->connected = 1;
3879 w->force = 1;
3880 dapm_mark_dirty(w, "force enable");
3881
3882 return 0;
3883}
3884EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3885
3886/**
Mark Brownda341832010-03-15 19:23:37 +00003887 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003888 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003889 * @pin: pin name
3890 *
3891 * Enables input/output pin regardless of any other state. This is
3892 * intended for use with microphone bias supplies used in microphone
3893 * jack detection.
3894 *
3895 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3896 * do any widget power switching.
3897 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003898int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3899 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003900{
Charles Keepax11391102014-02-18 15:22:14 +00003901 int ret;
Mark Brownda341832010-03-15 19:23:37 +00003902
Charles Keepax11391102014-02-18 15:22:14 +00003903 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownda341832010-03-15 19:23:37 +00003904
Charles Keepax11391102014-02-18 15:22:14 +00003905 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Mark Brown0d867332011-04-06 11:38:14 +09003906
Charles Keepax11391102014-02-18 15:22:14 +00003907 mutex_unlock(&dapm->card->dapm_mutex);
3908
3909 return ret;
Mark Brownda341832010-03-15 19:23:37 +00003910}
3911EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3912
3913/**
Charles Keepax11391102014-02-18 15:22:14 +00003914 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3915 * @dapm: DAPM context
3916 * @pin: pin name
3917 *
3918 * Disables input/output pin and its parents or children widgets.
3919 *
3920 * Requires external locking.
3921 *
3922 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3923 * do any widget power switching.
3924 */
3925int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3926 const char *pin)
3927{
3928 return snd_soc_dapm_set_pin(dapm, pin, 0);
3929}
3930EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3931
3932/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003933 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003934 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003935 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003936 *
Mark Brown74b8f952009-06-06 11:26:15 +01003937 * Disables input/output pin and its parents or children widgets.
Charles Keepax11391102014-02-18 15:22:14 +00003938 *
Liam Girdwooda5302182008-07-07 13:35:17 +01003939 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3940 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003941 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003942int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3943 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003944{
Charles Keepax11391102014-02-18 15:22:14 +00003945 int ret;
3946
3947 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3948
3949 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3950
3951 mutex_unlock(&dapm->card->dapm_mutex);
3952
3953 return ret;
Liam Girdwooda5302182008-07-07 13:35:17 +01003954}
3955EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3956
3957/**
Charles Keepax11391102014-02-18 15:22:14 +00003958 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3959 * @dapm: DAPM context
3960 * @pin: pin name
3961 *
3962 * Marks the specified pin as being not connected, disabling it along
3963 * any parent or child widgets. At present this is identical to
3964 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3965 * additional things such as disabling controls which only affect
3966 * paths through the pin.
3967 *
3968 * Requires external locking.
3969 *
3970 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3971 * do any widget power switching.
3972 */
3973int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3974 const char *pin)
3975{
3976 return snd_soc_dapm_set_pin(dapm, pin, 0);
3977}
3978EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3979
3980/**
Mark Brown5817b522008-09-24 11:23:11 +01003981 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003982 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003983 * @pin: pin name
3984 *
3985 * Marks the specified pin as being not connected, disabling it along
3986 * any parent or child widgets. At present this is identical to
3987 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3988 * additional things such as disabling controls which only affect
3989 * paths through the pin.
3990 *
3991 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3992 * do any widget power switching.
3993 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003994int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003995{
Charles Keepax11391102014-02-18 15:22:14 +00003996 int ret;
3997
3998 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3999
4000 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4001
4002 mutex_unlock(&dapm->card->dapm_mutex);
4003
4004 return ret;
Mark Brown5817b522008-09-24 11:23:11 +01004005}
4006EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4007
4008/**
Liam Girdwooda5302182008-07-07 13:35:17 +01004009 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004010 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01004011 * @pin: audio signal pin endpoint (or start point)
4012 *
4013 * Get audio pin status - connected or disconnected.
4014 *
4015 * Returns 1 for connected otherwise 0.
4016 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004017int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4018 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004019{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004020 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004021
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004022 if (w)
4023 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06004024
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004025 return 0;
4026}
Liam Girdwooda5302182008-07-07 13:35:17 +01004027EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02004028
4029/**
Mark Brown1547aba2010-05-07 21:11:40 +01004030 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004031 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01004032 * @pin: audio signal pin endpoint (or start point)
4033 *
4034 * Mark the given endpoint or pin as ignoring suspend. When the
4035 * system is disabled a path between two endpoints flagged as ignoring
4036 * suspend will not be disabled. The path must already be enabled via
4037 * normal means at suspend time, it will not be turned on if it was not
4038 * already enabled.
4039 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004040int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4041 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01004042{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004043 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01004044
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004045 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004046 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004047 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01004048 }
4049
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02004050 w->ignore_suspend = 1;
4051
4052 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01004053}
4054EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4055
Stephen Warren16332812011-11-23 12:42:04 -07004056static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
4057 struct snd_soc_dapm_widget *w)
4058{
4059 struct snd_soc_dapm_path *p;
4060
4061 list_for_each_entry(p, &card->paths, list) {
4062 if ((p->source == w) || (p->sink == w)) {
4063 dev_dbg(card->dev,
4064 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
4065 p->source->name, p->source->id, p->source->dapm,
4066 p->sink->name, p->sink->id, p->sink->dapm);
4067
4068 /* Connected to something other than the codec */
4069 if (p->source->dapm != p->sink->dapm)
4070 return true;
4071 /*
4072 * Loopback connection from codec external pin to
4073 * codec external pin
4074 */
4075 if (p->sink->id == snd_soc_dapm_input) {
4076 switch (p->source->id) {
4077 case snd_soc_dapm_output:
4078 case snd_soc_dapm_micbias:
4079 return true;
4080 default:
4081 break;
4082 }
4083 }
4084 }
4085 }
4086
4087 return false;
4088}
4089
4090/**
4091 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
4092 * @codec: The codec whose pins should be processed
4093 *
4094 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
4095 * which are unused. Pins are used if they are connected externally to the
4096 * codec, whether that be to some other device, or a loop-back connection to
4097 * the codec itself.
4098 */
4099void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
4100{
4101 struct snd_soc_card *card = codec->card;
4102 struct snd_soc_dapm_context *dapm = &codec->dapm;
4103 struct snd_soc_dapm_widget *w;
4104
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004105 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07004106 &card->dapm, &codec->dapm);
4107
4108 list_for_each_entry(w, &card->widgets, list) {
4109 if (w->dapm != dapm)
4110 continue;
4111 switch (w->id) {
4112 case snd_soc_dapm_input:
4113 case snd_soc_dapm_output:
4114 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00004115 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07004116 w->name);
4117 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00004118 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07004119 "... Not in map; disabling\n");
4120 snd_soc_dapm_nc_pin(dapm, w->name);
4121 }
4122 break;
4123 default:
4124 break;
4125 }
4126 }
4127}
4128
Mark Brown1547aba2010-05-07 21:11:40 +01004129/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02004130 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03004131 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02004132 *
4133 * Free all dapm widgets and resources.
4134 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004135void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02004136{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004137 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02004138 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004139 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02004140 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02004141}
4142EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4143
Xiang Xiao57996352014-03-02 00:04:02 +08004144static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01004145{
Liam Girdwood01005a72012-07-06 16:57:05 +01004146 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01004147 struct snd_soc_dapm_widget *w;
4148 LIST_HEAD(down_list);
4149 int powerdown = 0;
4150
Liam Girdwood01005a72012-07-06 16:57:05 +01004151 mutex_lock(&card->dapm_mutex);
4152
Jarkko Nikula97c866d2010-12-14 12:18:31 +02004153 list_for_each_entry(w, &dapm->card->widgets, list) {
4154 if (w->dapm != dapm)
4155 continue;
Mark Brown51737472009-06-22 13:16:51 +01004156 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00004157 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01004158 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01004159 powerdown = 1;
4160 }
4161 }
4162
4163 /* If there were no widgets to power down we're already in
4164 * standby.
4165 */
4166 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00004167 if (dapm->bias_level == SND_SOC_BIAS_ON)
4168 snd_soc_dapm_set_bias_level(dapm,
4169 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02004170 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00004171 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4172 snd_soc_dapm_set_bias_level(dapm,
4173 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01004174 }
Liam Girdwood01005a72012-07-06 16:57:05 +01004175
4176 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004177}
Mark Brown51737472009-06-22 13:16:51 +01004178
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004179/*
4180 * snd_soc_dapm_shutdown - callback for system shutdown
4181 */
4182void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4183{
Xiang Xiao57996352014-03-02 00:04:02 +08004184 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004185
Xiang Xiao57996352014-03-02 00:04:02 +08004186 list_for_each_entry(dapm, &card->dapm_list, list) {
Xiang Xiao17282ba2014-03-02 00:04:03 +08004187 if (dapm != &card->dapm) {
4188 soc_dapm_shutdown_dapm(dapm);
4189 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4190 snd_soc_dapm_set_bias_level(dapm,
4191 SND_SOC_BIAS_OFF);
4192 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004193 }
Xiang Xiao17282ba2014-03-02 00:04:03 +08004194
4195 soc_dapm_shutdown_dapm(&card->dapm);
4196 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4197 snd_soc_dapm_set_bias_level(&card->dapm,
4198 SND_SOC_BIAS_OFF);
Mark Brown51737472009-06-22 13:16:51 +01004199}
4200
Richard Purdie2b97eab2006-10-06 18:32:18 +02004201/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004202MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02004203MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4204MODULE_LICENSE("GPL");