blob: dcade130157f3433d88ddbf7db7c311c43ea8042 [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
Troy Kisky12ef1932008-10-13 17:42:14 -0700118static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +0100119{
120 if (pop_time)
121 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
122}
123
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200124static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100125{
126 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200127 char *buf;
128
129 if (!pop_time)
130 return;
131
132 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
133 if (buf == NULL)
134 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100135
136 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200137 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100138 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100139 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200140
141 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100142}
143
Mark Browndb432b42011-10-03 21:06:40 +0100144static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
145{
146 return !list_empty(&w->dirty);
147}
148
Mark Brown25c77c52011-10-08 13:36:03 +0100149void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100150{
Mark Brown75c1f892011-10-04 22:28:08 +0100151 if (!dapm_dirty_widget(w)) {
152 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
153 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100154 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100155 }
Mark Browndb432b42011-10-03 21:06:40 +0100156}
Mark Brown25c77c52011-10-08 13:36:03 +0100157EXPORT_SYMBOL_GPL(dapm_mark_dirty);
Mark Browndb432b42011-10-03 21:06:40 +0100158
Mark Browne2d32ff2012-08-31 17:38:32 -0700159void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
160{
161 struct snd_soc_card *card = dapm->card;
162 struct snd_soc_dapm_widget *w;
163
164 mutex_lock(&card->dapm_mutex);
165
166 list_for_each_entry(w, &card->widgets, list) {
167 switch (w->id) {
168 case snd_soc_dapm_input:
169 case snd_soc_dapm_output:
170 dapm_mark_dirty(w, "Rechecking inputs and outputs");
171 break;
172 default:
173 break;
174 }
175 }
176
177 mutex_unlock(&card->dapm_mutex);
178}
179EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
180
Richard Purdie2b97eab2006-10-06 18:32:18 +0200181/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100182static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200183 const struct snd_soc_dapm_widget *_widget)
184{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100185 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200186}
187
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200188struct dapm_kcontrol_data {
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200189 unsigned int value;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200190 struct snd_soc_dapm_widget *widget;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200191 struct list_head paths;
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200192 struct snd_soc_dapm_widget_list *wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200193};
194
195static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
196 struct snd_kcontrol *kcontrol)
197{
198 struct dapm_kcontrol_data *data;
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200199 struct soc_mixer_control *mc;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200200
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200201 data = kzalloc(sizeof(*data), GFP_KERNEL);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200202 if (!data) {
203 dev_err(widget->dapm->dev,
204 "ASoC: can't allocate kcontrol data for %s\n",
205 widget->name);
206 return -ENOMEM;
207 }
208
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200209 INIT_LIST_HEAD(&data->paths);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200210
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200211 switch (widget->id) {
212 case snd_soc_dapm_switch:
213 case snd_soc_dapm_mixer:
214 case snd_soc_dapm_mixer_named_ctl:
215 mc = (struct soc_mixer_control *)kcontrol->private_value;
216
217 if (mc->autodisable) {
218 struct snd_soc_dapm_widget template;
219
220 memset(&template, 0, sizeof(template));
221 template.reg = mc->reg;
222 template.mask = (1 << fls(mc->max)) - 1;
223 template.shift = mc->shift;
224 if (mc->invert)
225 template.off_val = mc->max;
226 else
227 template.off_val = 0;
228 template.on_val = template.off_val;
229 template.id = snd_soc_dapm_kcontrol;
230 template.name = kcontrol->id.name;
231
Lars-Peter Clausen2daabd72013-08-30 17:39:33 +0200232 data->value = template.on_val;
233
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200234 data->widget = snd_soc_dapm_new_control(widget->dapm,
235 &template);
236 if (!data->widget) {
237 kfree(data);
238 return -ENOMEM;
239 }
240 }
241 break;
242 default:
243 break;
244 }
245
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200246 kcontrol->private_data = data;
247
248 return 0;
249}
250
251static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
252{
253 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200254 kfree(data->widget);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200255 kfree(data->wlist);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200256 kfree(data);
257}
258
259static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
260 const struct snd_kcontrol *kcontrol)
261{
262 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
263
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200264 return data->wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200265}
266
267static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
268 struct snd_soc_dapm_widget *widget)
269{
270 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200271 struct snd_soc_dapm_widget_list *new_wlist;
272 unsigned int n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200273
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200274 if (data->wlist)
275 n = data->wlist->num_widgets + 1;
276 else
277 n = 1;
278
279 new_wlist = krealloc(data->wlist,
280 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
281 if (!new_wlist)
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200282 return -ENOMEM;
283
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200284 new_wlist->widgets[n - 1] = widget;
285 new_wlist->num_widgets = n;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200286
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200287 data->wlist = new_wlist;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200288
289 return 0;
290}
291
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200292static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
293 struct snd_soc_dapm_path *path)
294{
295 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
296
297 list_add_tail(&path->list_kcontrol, &data->paths);
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200298
299 if (data->widget) {
300 snd_soc_dapm_add_path(data->widget->dapm, data->widget,
301 path->source, NULL, NULL);
302 }
303}
304
305static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
306{
307 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
308
309 if (!data->widget)
310 return true;
311
312 return data->widget->power;
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200313}
314
315static struct list_head *dapm_kcontrol_get_path_list(
316 const struct snd_kcontrol *kcontrol)
317{
318 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
319
320 return &data->paths;
321}
322
323#define dapm_kcontrol_for_each_path(path, kcontrol) \
324 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
325 list_kcontrol)
326
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200327static unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
328{
329 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
330
331 return data->value;
332}
333
334static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
335 unsigned int value)
336{
337 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
338
339 if (data->value == value)
340 return false;
341
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200342 if (data->widget)
343 data->widget->on_val = value;
344
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +0200345 data->value = value;
346
347 return true;
348}
349
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200350/**
351 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
352 * @kcontrol: The kcontrol
353 */
354struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
355{
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200356 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->codec;
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +0200357}
358EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
359
Liam Girdwood6c120e12012-02-15 15:15:34 +0000360static void dapm_reset(struct snd_soc_card *card)
361{
362 struct snd_soc_dapm_widget *w;
363
364 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
365
366 list_for_each_entry(w, &card->widgets, list) {
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +0200367 w->new_power = w->power;
Liam Girdwood6c120e12012-02-15 15:15:34 +0000368 w->power_checked = false;
369 w->inputs = -1;
370 w->outputs = -1;
371 }
372}
373
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100374static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
375{
376 if (w->codec)
377 return snd_soc_read(w->codec, reg);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100378 else if (w->platform)
379 return snd_soc_platform_read(w->platform, reg);
380
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000381 dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100382 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100383}
384
385static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
386{
387 if (w->codec)
388 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100389 else if (w->platform)
390 return snd_soc_platform_write(w->platform, reg, val);
391
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000392 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
Liam Girdwoodb7950642011-07-04 22:10:52 +0100393 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100394}
395
Liam Girdwood49575fb52012-03-06 18:16:19 +0000396static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
397{
Mark Browne06ab3b2012-03-06 23:58:22 +0000398 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000399 mutex_lock(&w->codec->mutex);
400 else if (w->platform)
401 mutex_lock(&w->platform->mutex);
402}
403
404static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
405{
Mark Browne06ab3b2012-03-06 23:58:22 +0000406 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000407 mutex_unlock(&w->codec->mutex);
408 else if (w->platform)
409 mutex_unlock(&w->platform->mutex);
410}
411
Mark Browneb270e92013-10-09 13:52:52 +0100412static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
413{
414 if (dapm->codec && dapm->codec->using_regmap)
415 regmap_async_complete(dapm->codec->control_data);
416}
417
Liam Girdwood49575fb52012-03-06 18:16:19 +0000418static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100419 unsigned short reg, unsigned int mask, unsigned int value)
420{
Mark Brown8a713da2011-12-03 12:33:55 +0000421 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100422 unsigned int old, new;
423 int ret;
424
Mark Brown8a713da2011-12-03 12:33:55 +0000425 if (w->codec && w->codec->using_regmap) {
Mark Browneb270e92013-10-09 13:52:52 +0100426 ret = regmap_update_bits_check_async(w->codec->control_data,
427 reg, mask, value,
428 &change);
Mark Brown8a713da2011-12-03 12:33:55 +0000429 if (ret != 0)
430 return ret;
431 } else {
Liam Girdwood49575fb52012-03-06 18:16:19 +0000432 soc_widget_lock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000433 ret = soc_widget_read(w, reg);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000434 if (ret < 0) {
435 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100436 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000437 }
Mark Brown8a713da2011-12-03 12:33:55 +0000438
439 old = ret;
440 new = (old & ~mask) | (value & mask);
441 change = old != new;
442 if (change) {
443 ret = soc_widget_write(w, reg, new);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000444 if (ret < 0) {
445 soc_widget_unlock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000446 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000447 }
Mark Brown8a713da2011-12-03 12:33:55 +0000448 }
Liam Girdwood49575fb52012-03-06 18:16:19 +0000449 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100450 }
451
452 return change;
453}
454
Mark Brown452c5ea2009-05-17 21:41:23 +0100455/**
456 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800457 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100458 * @level: level to configure
459 *
460 * Configure the bias (power) levels for the SoC audio device.
461 *
462 * Returns 0 for success else error.
463 */
Mark Browned5a4c42011-02-18 11:12:42 -0800464static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200465 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100466{
Mark Browned5a4c42011-02-18 11:12:42 -0800467 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100468 int ret = 0;
469
Mark Brown84e90932010-11-04 00:07:02 -0400470 trace_snd_soc_bias_level_start(card, level);
471
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000472 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100473 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100474 if (ret != 0)
475 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100476
Mark Browncc4c6702011-06-06 19:03:34 +0100477 if (dapm->codec) {
478 if (dapm->codec->driver->set_bias_level)
479 ret = dapm->codec->driver->set_bias_level(dapm->codec,
480 level);
Mark Brownd8c3bb92012-08-23 18:10:42 +0100481 else
482 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100483 } else if (!card || dapm != &card->dapm) {
Liam Girdwood41231282012-07-06 16:56:16 +0100484 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100485 }
Liam Girdwood41231282012-07-06 16:56:16 +0100486
Mark Brown171ec6b2011-06-06 18:15:19 +0100487 if (ret != 0)
488 goto out;
489
490 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100491 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100492out:
Mark Brown84e90932010-11-04 00:07:02 -0400493 trace_snd_soc_bias_level_done(card, level);
494
Mark Brown452c5ea2009-05-17 21:41:23 +0100495 return ret;
496}
497
Richard Purdie2b97eab2006-10-06 18:32:18 +0200498/* set up initial codec paths */
499static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
500 struct snd_soc_dapm_path *p, int i)
501{
502 switch (w->id) {
503 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000504 case snd_soc_dapm_mixer:
505 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200506 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100507 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600508 w->kcontrol_news[i].private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +0200509 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400510 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100511 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400512 unsigned int mask = (1 << fls(max)) - 1;
513 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200514
Lars-Peter Clausen249ce132013-10-06 13:43:49 +0200515 if (reg != SND_SOC_NOPM) {
516 val = soc_widget_read(w, reg);
517 val = (val >> shift) & mask;
518 if (invert)
519 val = max - val;
520 p->connect = !!val;
521 } else {
522 p->connect = 0;
523 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200524
Richard Purdie2b97eab2006-10-06 18:32:18 +0200525 }
526 break;
527 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600528 struct soc_enum *e = (struct soc_enum *)
529 w->kcontrol_news[i].private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200530 int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200531
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100532 val = soc_widget_read(w, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200533 item = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200534
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200535 if (item < e->max && !strcmp(p->name, e->texts[item]))
536 p->connect = 1;
537 else
538 p->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200539 }
540 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000541 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600542 struct soc_enum *e = (struct soc_enum *)
543 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000544
545 p->connect = 0;
546 /* since a virtual mux has no backing registers to
547 * decide which path to connect, it will try to match
548 * with the first enumeration. This is to ensure
549 * that the default mux choice (the first) will be
550 * correctly powered up during initialization.
551 */
552 if (!strcmp(p->name, e->texts[0]))
553 p->connect = 1;
554 }
555 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200556 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200557 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600558 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200559 int val, item;
560
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100561 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200562 val = (val >> e->shift_l) & e->mask;
563 for (item = 0; item < e->max; item++) {
564 if (val == e->values[item])
565 break;
566 }
567
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200568 if (item < e->max && !strcmp(p->name, e->texts[item]))
569 p->connect = 1;
570 else
571 p->connect = 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200572 }
573 break;
Mark Brown56563102011-10-03 22:41:09 +0100574 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200575 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600576 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200577 case snd_soc_dapm_output:
578 case snd_soc_dapm_adc:
579 case snd_soc_dapm_input:
Mark Brown1ab97c82011-11-27 16:21:51 +0000580 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200581 case snd_soc_dapm_dac:
582 case snd_soc_dapm_micbias:
583 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100584 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +0000585 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200586 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100587 case snd_soc_dapm_aif_in:
588 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100589 case snd_soc_dapm_dai_in:
590 case snd_soc_dapm_dai_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200591 case snd_soc_dapm_hp:
592 case snd_soc_dapm_mic:
593 case snd_soc_dapm_spk:
594 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +0100595 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200596 case snd_soc_dapm_kcontrol:
Mark Brown56563102011-10-03 22:41:09 +0100597 p->connect = 1;
598 break;
599 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200600 case snd_soc_dapm_pre:
601 case snd_soc_dapm_post:
602 p->connect = 0;
603 break;
604 }
605}
606
Mark Brown74b8f952009-06-06 11:26:15 +0100607/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200608static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200609 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
610 struct snd_soc_dapm_path *path, const char *control_name,
611 const struct snd_kcontrol_new *kcontrol)
612{
613 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
614 int i;
615
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100616 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200617 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200618 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200619 list_add(&path->list_sink, &dest->sources);
620 list_add(&path->list_source, &src->sinks);
621 path->name = (char*)e->texts[i];
622 dapm_set_path_status(dest, path, 0);
623 return 0;
624 }
625 }
626
627 return -ENODEV;
628}
629
Mark Brown74b8f952009-06-06 11:26:15 +0100630/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200631static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200632 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
633 struct snd_soc_dapm_path *path, const char *control_name)
634{
635 int i;
636
637 /* search for mixer kcontrol */
638 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600639 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200640 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200641 list_add(&path->list_sink, &dest->sources);
642 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600643 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200644 dapm_set_path_status(dest, path, i);
645 return 0;
646 }
647 }
648 return -ENODEV;
649}
650
Stephen Warrenaf468002011-04-28 17:38:01 -0600651static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600652 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600653 const struct snd_kcontrol_new *kcontrol_new,
654 struct snd_kcontrol **kcontrol)
655{
656 struct snd_soc_dapm_widget *w;
657 int i;
658
659 *kcontrol = NULL;
660
661 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600662 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
663 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600664 for (i = 0; i < w->num_kcontrols; i++) {
665 if (&w->kcontrol_news[i] == kcontrol_new) {
666 if (w->kcontrols)
667 *kcontrol = w->kcontrols[i];
668 return 1;
669 }
670 }
671 }
672
673 return 0;
674}
675
Stephen Warren85762e72013-03-29 15:40:10 -0600676/*
677 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
678 * create it. Either way, add the widget into the control's widget list
679 */
680static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100681 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200682{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200683 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000684 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000685 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600686 size_t prefix_len;
687 int shared;
688 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600689 bool wname_in_long_name, kcname_in_long_name;
Stephen Warren85762e72013-03-29 15:40:10 -0600690 char *long_name;
691 const char *name;
692 int ret;
Mark Brownefb7ac32011-03-08 17:23:24 +0000693
694 if (dapm->codec)
695 prefix = dapm->codec->name_prefix;
696 else
697 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200698
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000699 if (prefix)
700 prefix_len = strlen(prefix) + 1;
701 else
702 prefix_len = 0;
703
Stephen Warren85762e72013-03-29 15:40:10 -0600704 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
705 &kcontrol);
706
Stephen Warren85762e72013-03-29 15:40:10 -0600707 if (!kcontrol) {
708 if (shared) {
709 wname_in_long_name = false;
710 kcname_in_long_name = true;
711 } else {
712 switch (w->id) {
713 case snd_soc_dapm_switch:
714 case snd_soc_dapm_mixer:
715 wname_in_long_name = true;
716 kcname_in_long_name = true;
717 break;
718 case snd_soc_dapm_mixer_named_ctl:
719 wname_in_long_name = false;
720 kcname_in_long_name = true;
721 break;
722 case snd_soc_dapm_mux:
723 case snd_soc_dapm_virt_mux:
724 case snd_soc_dapm_value_mux:
725 wname_in_long_name = true;
726 kcname_in_long_name = false;
727 break;
728 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600729 return -EINVAL;
730 }
731 }
732
733 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600734 /*
735 * The control will get a prefix from the control
736 * creation process but we're also using the same
737 * prefix for widgets so cut the prefix off the
738 * front of the widget name.
739 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200740 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600741 w->name + prefix_len,
742 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200743 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200744 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600745
746 name = long_name;
747 } else if (wname_in_long_name) {
748 long_name = NULL;
749 name = w->name + prefix_len;
750 } else {
751 long_name = NULL;
752 name = w->kcontrol_news[kci].name;
753 }
754
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200755 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600756 prefix);
Lars-Peter Clausen656ca9d2013-06-14 13:16:54 +0200757 kfree(long_name);
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200758 if (!kcontrol)
759 return -ENOMEM;
760 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200761
762 ret = dapm_kcontrol_data_alloc(w, kcontrol);
763 if (ret) {
764 snd_ctl_free_one(kcontrol);
765 return ret;
766 }
767
Stephen Warren85762e72013-03-29 15:40:10 -0600768 ret = snd_ctl_add(card, kcontrol);
769 if (ret < 0) {
770 dev_err(dapm->dev,
771 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
772 w->name, name, ret);
Stephen Warren85762e72013-03-29 15:40:10 -0600773 return ret;
774 }
Stephen Warren85762e72013-03-29 15:40:10 -0600775 }
776
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200777 ret = dapm_kcontrol_add_widget(kcontrol, w);
778 if (ret)
779 return ret;
780
Stephen Warren85762e72013-03-29 15:40:10 -0600781 w->kcontrols[kci] = kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600782
783 return 0;
784}
785
786/* create new dapm mixer control */
787static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
788{
789 int i, ret;
790 struct snd_soc_dapm_path *path;
791
Richard Purdie2b97eab2006-10-06 18:32:18 +0200792 /* add kcontrol */
793 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200794 /* match name */
795 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200796 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600797 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200798 continue;
799
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200800 if (w->kcontrols[i]) {
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200801 dapm_kcontrol_add_path(w->kcontrols[i], path);
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200802 continue;
803 }
804
Mark Brown946d92a2013-08-12 23:28:42 +0100805 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
Stephen Warren85762e72013-03-29 15:40:10 -0600806 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200807 return ret;
Mark Brown946d92a2013-08-12 23:28:42 +0100808
809 dapm_kcontrol_add_path(w->kcontrols[i], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200810 }
811 }
Stephen Warren85762e72013-03-29 15:40:10 -0600812
813 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200814}
815
816/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200817static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200818{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200819 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600820 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600821 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200822
Stephen Warrenaf468002011-04-28 17:38:01 -0600823 if (w->num_kcontrols != 1) {
824 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000825 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600826 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200827 return -EINVAL;
828 }
829
Lars-Peter Clausenfe5813912013-08-01 18:30:38 +0200830 if (list_empty(&w->sources)) {
Stephen Warren85762e72013-03-29 15:40:10 -0600831 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
832 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600833 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200834
Mark Brown946d92a2013-08-12 23:28:42 +0100835 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600836 if (ret < 0)
837 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600838
Richard Purdie2b97eab2006-10-06 18:32:18 +0200839 list_for_each_entry(path, &w->sources, list_sink)
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200840 dapm_kcontrol_add_path(w->kcontrols[0], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200841
Stephen Warrenaf468002011-04-28 17:38:01 -0600842 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200843}
844
845/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200846static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200847{
Mark Browna6c65732010-03-03 17:45:21 +0000848 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200849 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000850 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200851
Mark Browna6c65732010-03-03 17:45:21 +0000852 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200853}
854
855/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100856static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
857 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200858{
859 struct snd_soc_dapm_path *p;
860
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100861 list_for_each_entry(p, sink, list_source) {
862 if (p->walked) {
863 p->walked = 0;
864 dapm_clear_walk_output(dapm, &p->sink->sinks);
865 }
866 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200867}
868
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100869static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
870 struct list_head *source)
871{
872 struct snd_soc_dapm_path *p;
873
874 list_for_each_entry(p, source, list_sink) {
875 if (p->walked) {
876 p->walked = 0;
877 dapm_clear_walk_input(dapm, &p->source->sources);
878 }
879 }
880}
881
882
Mark Brown99497882010-05-07 20:24:05 +0100883/* We implement power down on suspend by checking the power state of
884 * the ALSA card - when we are suspending the ALSA state for the card
885 * is set to D3.
886 */
887static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
888{
Mark Brown12ea2c72011-03-02 18:17:32 +0000889 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100890
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000891 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100892 case SNDRV_CTL_POWER_D3hot:
893 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100894 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000895 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200896 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100897 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100898 default:
899 return 1;
900 }
901}
902
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100903/* add widget to list if it's not already in the list */
904static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
905 struct snd_soc_dapm_widget *w)
906{
907 struct snd_soc_dapm_widget_list *wlist;
908 int wlistsize, wlistentries, i;
909
910 if (*list == NULL)
911 return -EINVAL;
912
913 wlist = *list;
914
915 /* is this widget already in the list */
916 for (i = 0; i < wlist->num_widgets; i++) {
917 if (wlist->widgets[i] == w)
918 return 0;
919 }
920
921 /* allocate some new space */
922 wlistentries = wlist->num_widgets + 1;
923 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
924 wlistentries * sizeof(struct snd_soc_dapm_widget *);
925 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
926 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000927 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100928 w->name);
929 return -ENOMEM;
930 }
931 wlist = *list;
932
933 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000934 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100935 w->name, wlist->num_widgets);
936
937 wlist->widgets[wlist->num_widgets] = w;
938 wlist->num_widgets++;
939 return 1;
940}
941
Richard Purdie2b97eab2006-10-06 18:32:18 +0200942/*
943 * Recursively check for a completed path to an active or physically connected
944 * output widget. Returns number of complete paths.
945 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100946static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
947 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200948{
949 struct snd_soc_dapm_path *path;
950 int con = 0;
951
Mark Brown024dc072011-10-09 11:52:05 +0100952 if (widget->outputs >= 0)
953 return widget->outputs;
954
Mark Brownde02d072011-09-20 21:43:24 +0100955 DAPM_UPDATE_STAT(widget, path_checks);
956
Mark Brown62ea8742012-01-21 21:14:48 +0000957 switch (widget->id) {
958 case snd_soc_dapm_supply:
959 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200960 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200961 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100962 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000963 default:
964 break;
965 }
Mark Brown246d0a12009-04-22 18:24:55 +0100966
Mark Brown010ff262009-08-17 17:39:22 +0100967 switch (widget->id) {
968 case snd_soc_dapm_adc:
969 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100970 case snd_soc_dapm_dai_out:
Mark Brown024dc072011-10-09 11:52:05 +0100971 if (widget->active) {
972 widget->outputs = snd_soc_dapm_suspend_check(widget);
973 return widget->outputs;
974 }
Mark Brown010ff262009-08-17 17:39:22 +0100975 default:
976 break;
977 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200978
979 if (widget->connected) {
980 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100981 if (widget->id == snd_soc_dapm_output && !widget->ext) {
982 widget->outputs = snd_soc_dapm_suspend_check(widget);
983 return widget->outputs;
984 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200985
986 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100987 if (widget->id == snd_soc_dapm_hp ||
988 widget->id == snd_soc_dapm_spk ||
989 (widget->id == snd_soc_dapm_line &&
990 !list_empty(&widget->sources))) {
991 widget->outputs = snd_soc_dapm_suspend_check(widget);
992 return widget->outputs;
993 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200994 }
995
996 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e02011-09-21 18:19:14 +0100997 DAPM_UPDATE_STAT(widget, neighbour_checks);
998
Mark Brownbf3a9e12011-06-13 16:42:29 +0100999 if (path->weak)
1000 continue;
1001
Mark Brown8af294b2013-02-22 17:48:15 +00001002 if (path->walking)
1003 return 1;
1004
Richard Purdie2b97eab2006-10-06 18:32:18 +02001005 if (path->walked)
1006 continue;
1007
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001008 trace_snd_soc_dapm_output_path(widget, path);
1009
Richard Purdie2b97eab2006-10-06 18:32:18 +02001010 if (path->sink && path->connect) {
1011 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +00001012 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001013
1014 /* do we need to add this widget to the list ? */
1015 if (list) {
1016 int err;
1017 err = dapm_list_add_widget(list, path->sink);
1018 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001019 dev_err(widget->dapm->dev,
1020 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001021 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001022 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001023 return con;
1024 }
1025 }
1026
1027 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001028
1029 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001030 }
1031 }
1032
Mark Brown024dc072011-10-09 11:52:05 +01001033 widget->outputs = con;
1034
Richard Purdie2b97eab2006-10-06 18:32:18 +02001035 return con;
1036}
1037
1038/*
1039 * Recursively check for a completed path to an active or physically connected
1040 * input widget. Returns number of complete paths.
1041 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001042static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1043 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001044{
1045 struct snd_soc_dapm_path *path;
1046 int con = 0;
1047
Mark Brown024dc072011-10-09 11:52:05 +01001048 if (widget->inputs >= 0)
1049 return widget->inputs;
1050
Mark Brownde02d072011-09-20 21:43:24 +01001051 DAPM_UPDATE_STAT(widget, path_checks);
1052
Mark Brown62ea8742012-01-21 21:14:48 +00001053 switch (widget->id) {
1054 case snd_soc_dapm_supply:
1055 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001056 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001057 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +01001058 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +00001059 default:
1060 break;
1061 }
Mark Brown246d0a12009-04-22 18:24:55 +01001062
Richard Purdie2b97eab2006-10-06 18:32:18 +02001063 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +01001064 switch (widget->id) {
1065 case snd_soc_dapm_dac:
1066 case snd_soc_dapm_aif_in:
Mark Brown46162742013-06-05 19:36:11 +01001067 case snd_soc_dapm_dai_in:
Mark Brown024dc072011-10-09 11:52:05 +01001068 if (widget->active) {
1069 widget->inputs = snd_soc_dapm_suspend_check(widget);
1070 return widget->inputs;
1071 }
Mark Brown010ff262009-08-17 17:39:22 +01001072 default:
1073 break;
1074 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001075
1076 if (widget->connected) {
1077 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +01001078 if (widget->id == snd_soc_dapm_input && !widget->ext) {
1079 widget->inputs = snd_soc_dapm_suspend_check(widget);
1080 return widget->inputs;
1081 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001082
1083 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +01001084 if (widget->id == snd_soc_dapm_vmid) {
1085 widget->inputs = snd_soc_dapm_suspend_check(widget);
1086 return widget->inputs;
1087 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001088
1089 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +03001090 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +01001091 (widget->id == snd_soc_dapm_line &&
1092 !list_empty(&widget->sinks))) {
1093 widget->inputs = snd_soc_dapm_suspend_check(widget);
1094 return widget->inputs;
1095 }
1096
Mark Brown1ab97c82011-11-27 16:21:51 +00001097 /* signal generator */
1098 if (widget->id == snd_soc_dapm_siggen) {
1099 widget->inputs = snd_soc_dapm_suspend_check(widget);
1100 return widget->inputs;
1101 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001102 }
1103
1104 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e02011-09-21 18:19:14 +01001105 DAPM_UPDATE_STAT(widget, neighbour_checks);
1106
Mark Brownbf3a9e12011-06-13 16:42:29 +01001107 if (path->weak)
1108 continue;
1109
Mark Brown8af294b2013-02-22 17:48:15 +00001110 if (path->walking)
1111 return 1;
1112
Richard Purdie2b97eab2006-10-06 18:32:18 +02001113 if (path->walked)
1114 continue;
1115
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001116 trace_snd_soc_dapm_input_path(widget, path);
1117
Richard Purdie2b97eab2006-10-06 18:32:18 +02001118 if (path->source && path->connect) {
1119 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +00001120 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001121
1122 /* do we need to add this widget to the list ? */
1123 if (list) {
1124 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +01001125 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001126 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001127 dev_err(widget->dapm->dev,
1128 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001129 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001130 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001131 return con;
1132 }
1133 }
1134
1135 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001136
1137 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001138 }
1139 }
1140
Mark Brown024dc072011-10-09 11:52:05 +01001141 widget->inputs = con;
1142
Richard Purdie2b97eab2006-10-06 18:32:18 +02001143 return con;
1144}
1145
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001146/**
1147 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1148 * @dai: the soc DAI.
1149 * @stream: stream direction.
1150 * @list: list of active widgets for this stream.
1151 *
1152 * Queries DAPM graph as to whether an valid audio stream path exists for
1153 * the initial stream specified by name. This takes into account
1154 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1155 *
1156 * Returns the number of valid paths or negative error.
1157 */
1158int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1159 struct snd_soc_dapm_widget_list **list)
1160{
1161 struct snd_soc_card *card = dai->card;
1162 int paths;
1163
1164 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1165 dapm_reset(card);
1166
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001167 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001168 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001169 dapm_clear_walk_output(&card->dapm,
1170 &dai->playback_widget->sinks);
1171 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001172 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001173 dapm_clear_walk_input(&card->dapm,
1174 &dai->capture_widget->sources);
1175 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001176
1177 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001178 mutex_unlock(&card->dapm_mutex);
1179
1180 return paths;
1181}
1182
Richard Purdie2b97eab2006-10-06 18:32:18 +02001183/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001184 * Handler for generic register modifier widget.
1185 */
1186int dapm_reg_event(struct snd_soc_dapm_widget *w,
1187 struct snd_kcontrol *kcontrol, int event)
1188{
1189 unsigned int val;
1190
1191 if (SND_SOC_DAPM_EVENT_ON(event))
1192 val = w->on_val;
1193 else
1194 val = w->off_val;
1195
Liam Girdwood49575fb52012-03-06 18:16:19 +00001196 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001197 w->mask << w->shift, val << w->shift);
1198
1199 return 0;
1200}
Mark Brown11589412008-07-29 11:42:23 +01001201EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001202
Mark Brown62ea8742012-01-21 21:14:48 +00001203/*
1204 * Handler for regulator supply widget.
1205 */
1206int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1207 struct snd_kcontrol *kcontrol, int event)
1208{
Mark Brownc05b84d2012-09-07 12:57:11 +08001209 int ret;
1210
Mark Browneb270e92013-10-09 13:52:52 +01001211 soc_dapm_async_complete(w->dapm);
1212
Mark Brownc05b84d2012-09-07 12:57:11 +08001213 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001214 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001215 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001216 if (ret != 0)
1217 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001218 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001219 w->name, ret);
1220 }
1221
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001222 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001223 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001224 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001225 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001226 if (ret != 0)
1227 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001228 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001229 w->name, ret);
1230 }
1231
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001232 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001233 }
Mark Brown62ea8742012-01-21 21:14:48 +00001234}
1235EXPORT_SYMBOL_GPL(dapm_regulator_event);
1236
Ola Liljad7e7eb92012-05-24 15:26:25 +02001237/*
1238 * Handler for clock supply widget.
1239 */
1240int dapm_clock_event(struct snd_soc_dapm_widget *w,
1241 struct snd_kcontrol *kcontrol, int event)
1242{
1243 if (!w->clk)
1244 return -EIO;
1245
Mark Browneb270e92013-10-09 13:52:52 +01001246 soc_dapm_async_complete(w->dapm);
1247
Mark Brownec029952012-06-04 08:16:20 +01001248#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001249 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001250 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001251 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001252 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001253 return 0;
1254 }
Mark Brownec029952012-06-04 08:16:20 +01001255#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001256 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001257}
1258EXPORT_SYMBOL_GPL(dapm_clock_event);
1259
Mark Brownd8050022011-09-28 18:28:23 +01001260static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1261{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001262 if (w->power_checked)
1263 return w->new_power;
1264
Mark Brownd8050022011-09-28 18:28:23 +01001265 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001266 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001267 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001268 w->new_power = w->power_check(w);
1269
1270 w->power_checked = true;
1271
1272 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001273}
1274
Mark Browncd0f2d42009-04-20 16:56:59 +01001275/* Generic check to see if a widget should be powered.
1276 */
1277static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1278{
1279 int in, out;
1280
Mark Brownde02d072011-09-20 21:43:24 +01001281 DAPM_UPDATE_STAT(w, power_checks);
1282
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001283 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001284 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001285 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001286 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001287 return out != 0 && in != 0;
1288}
1289
Mark Brown6ea31b92009-04-20 17:15:41 +01001290/* Check to see if an ADC has power */
1291static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1292{
1293 int in;
1294
Mark Brownde02d072011-09-20 21:43:24 +01001295 DAPM_UPDATE_STAT(w, power_checks);
1296
Mark Brown6ea31b92009-04-20 17:15:41 +01001297 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001298 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001299 dapm_clear_walk_input(w->dapm, &w->sources);
Mark Brown6ea31b92009-04-20 17:15:41 +01001300 return in != 0;
1301 } else {
1302 return dapm_generic_check_power(w);
1303 }
1304}
1305
1306/* Check to see if a DAC has power */
1307static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1308{
1309 int out;
1310
Mark Brownde02d072011-09-20 21:43:24 +01001311 DAPM_UPDATE_STAT(w, power_checks);
1312
Mark Brown6ea31b92009-04-20 17:15:41 +01001313 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001314 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001315 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001316 return out != 0;
1317 } else {
1318 return dapm_generic_check_power(w);
1319 }
1320}
1321
Mark Brown246d0a12009-04-22 18:24:55 +01001322/* Check to see if a power supply is needed */
1323static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1324{
1325 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001326
Mark Brownde02d072011-09-20 21:43:24 +01001327 DAPM_UPDATE_STAT(w, power_checks);
1328
Mark Brown246d0a12009-04-22 18:24:55 +01001329 /* Check if one of our outputs is connected */
1330 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001331 DAPM_UPDATE_STAT(w, neighbour_checks);
1332
Mark Brownbf3a9e12011-06-13 16:42:29 +01001333 if (path->weak)
1334 continue;
1335
Mark Brown215edda2009-09-08 18:59:05 +01001336 if (path->connected &&
1337 !path->connected(path->source, path->sink))
1338 continue;
1339
Mark Brown30173582011-02-11 11:42:19 +00001340 if (!path->sink)
1341 continue;
1342
Mark Brownf68d7e12011-10-04 22:57:50 +01001343 if (dapm_widget_power_check(path->sink))
1344 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001345 }
1346
Mark Brownf68d7e12011-10-04 22:57:50 +01001347 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001348}
1349
Mark Brown35c64bc2011-09-28 18:23:53 +01001350static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1351{
1352 return 1;
1353}
1354
Mark Brown38357ab2009-06-06 19:03:23 +01001355static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1356 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001357 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001358{
Mark Brown828a8422011-01-15 13:14:30 +00001359 int *sort;
1360
1361 if (power_up)
1362 sort = dapm_up_seq;
1363 else
1364 sort = dapm_down_seq;
1365
Mark Brown38357ab2009-06-06 19:03:23 +01001366 if (sort[a->id] != sort[b->id])
1367 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001368 if (a->subseq != b->subseq) {
1369 if (power_up)
1370 return a->subseq - b->subseq;
1371 else
1372 return b->subseq - a->subseq;
1373 }
Mark Brownb22ead22009-06-07 12:51:26 +01001374 if (a->reg != b->reg)
1375 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001376 if (a->dapm != b->dapm)
1377 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001378
Mark Brown38357ab2009-06-06 19:03:23 +01001379 return 0;
1380}
Mark Brown42aa3412009-03-01 19:21:10 +00001381
Mark Brown38357ab2009-06-06 19:03:23 +01001382/* Insert a widget in order into a DAPM power sequence. */
1383static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1384 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001385 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001386{
1387 struct snd_soc_dapm_widget *w;
1388
1389 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001390 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001391 list_add_tail(&new_widget->power_list, &w->power_list);
1392 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001393 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001394
Mark Brown38357ab2009-06-06 19:03:23 +01001395 list_add_tail(&new_widget->power_list, list);
1396}
Mark Brown42aa3412009-03-01 19:21:10 +00001397
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001398static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001399 struct snd_soc_dapm_widget *w, int event)
1400{
Mark Brown68f89ad2010-11-03 23:51:49 -04001401 const char *ev_name;
1402 int power, ret;
1403
1404 switch (event) {
1405 case SND_SOC_DAPM_PRE_PMU:
1406 ev_name = "PRE_PMU";
1407 power = 1;
1408 break;
1409 case SND_SOC_DAPM_POST_PMU:
1410 ev_name = "POST_PMU";
1411 power = 1;
1412 break;
1413 case SND_SOC_DAPM_PRE_PMD:
1414 ev_name = "PRE_PMD";
1415 power = 0;
1416 break;
1417 case SND_SOC_DAPM_POST_PMD:
1418 ev_name = "POST_PMD";
1419 power = 0;
1420 break;
Mark Brown80114122013-02-25 15:14:19 +00001421 case SND_SOC_DAPM_WILL_PMU:
1422 ev_name = "WILL_PMU";
1423 power = 1;
1424 break;
1425 case SND_SOC_DAPM_WILL_PMD:
1426 ev_name = "WILL_PMD";
1427 power = 0;
1428 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001429 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01001430 WARN(1, "Unknown event %d\n", event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001431 return;
1432 }
1433
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001434 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001435 return;
1436
1437 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001438 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001439 w->name, ev_name);
Mark Browneb270e92013-10-09 13:52:52 +01001440 soc_dapm_async_complete(w->dapm);
Mark Brown84e90932010-11-04 00:07:02 -04001441 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001442 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001443 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001444 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001445 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001446 ev_name, w->name, ret);
1447 }
1448}
1449
Mark Brownb22ead22009-06-07 12:51:26 +01001450/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001451static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001452 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001453{
Mark Brown68f89ad2010-11-03 23:51:49 -04001454 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001455 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001456 unsigned int value = 0;
1457 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001458
1459 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1460 power_list)->reg;
1461
1462 list_for_each_entry(w, pending, power_list) {
Takashi Iwaibf4edea2013-11-07 18:38:47 +01001463 WARN_ON(reg != w->reg);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001464 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001465
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001466 mask |= w->mask << w->shift;
1467 if (w->power)
1468 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001469 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001470 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001471
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001472 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001473 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1474 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001475
Mark Brown68f89ad2010-11-03 23:51:49 -04001476 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001477 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1478 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001479 }
1480
Mark Brown81628102009-06-07 13:21:24 +01001481 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001482 /* Any widget will do, they should all be updating the
1483 * same register.
1484 */
1485 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1486 power_list);
1487
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001488 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001489 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001490 value, mask, reg, card->pop_time);
1491 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001492 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001493 }
1494
1495 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001496 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1497 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001498 }
Mark Brown42aa3412009-03-01 19:21:10 +00001499}
1500
Mark Brownb22ead22009-06-07 12:51:26 +01001501/* Apply a DAPM power sequence.
1502 *
1503 * We walk over a pre-sorted list of widgets to apply power to. In
1504 * order to minimise the number of writes to the device required
1505 * multiple widgets will be updated in a single write where possible.
1506 * Currently anything that requires more than a single write is not
1507 * handled.
1508 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001509static void dapm_seq_run(struct snd_soc_card *card,
1510 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001511{
1512 struct snd_soc_dapm_widget *w, *n;
Mark Browneb270e92013-10-09 13:52:52 +01001513 struct snd_soc_dapm_context *d;
Mark Brownb22ead22009-06-07 12:51:26 +01001514 LIST_HEAD(pending);
1515 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001516 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001517 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001518 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001519 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001520 int *sort;
1521
1522 if (power_up)
1523 sort = dapm_up_seq;
1524 else
1525 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001526
Mark Brownb22ead22009-06-07 12:51:26 +01001527 list_for_each_entry_safe(w, n, list, power_list) {
1528 ret = 0;
1529
1530 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001531 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001532 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001533 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001534 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001535
Mark Brown474b62d2011-01-18 16:14:44 +00001536 if (cur_dapm && cur_dapm->seq_notifier) {
1537 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1538 if (sort[i] == cur_sort)
1539 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001540 i,
1541 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001542 }
1543
Mark Browneb270e92013-10-09 13:52:52 +01001544 if (cur_dapm && w->dapm != cur_dapm)
1545 soc_dapm_async_complete(cur_dapm);
1546
Mark Brownb22ead22009-06-07 12:51:26 +01001547 INIT_LIST_HEAD(&pending);
1548 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001549 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001550 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001551 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001552 }
1553
Mark Brown163cac02009-06-07 10:12:52 +01001554 switch (w->id) {
1555 case snd_soc_dapm_pre:
1556 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001557 list_for_each_entry_safe_continue(w, n, list,
1558 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001559
Mark Brownb22ead22009-06-07 12:51:26 +01001560 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001561 ret = w->event(w,
1562 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001563 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001564 ret = w->event(w,
1565 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001566 break;
1567
1568 case snd_soc_dapm_post:
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_POST_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_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001579 break;
1580
Mark Brown163cac02009-06-07 10:12:52 +01001581 default:
Mark Brown81628102009-06-07 13:21:24 +01001582 /* Queue it up for application */
1583 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001584 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001585 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001586 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001587 list_move(&w->power_list, &pending);
1588 break;
Mark Brown163cac02009-06-07 10:12:52 +01001589 }
Mark Brownb22ead22009-06-07 12:51:26 +01001590
1591 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001592 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001593 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001594 }
Mark Brownb22ead22009-06-07 12:51:26 +01001595
1596 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001597 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001598
1599 if (cur_dapm && cur_dapm->seq_notifier) {
1600 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1601 if (sort[i] == cur_sort)
1602 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001603 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001604 }
Mark Browneb270e92013-10-09 13:52:52 +01001605
1606 list_for_each_entry(d, &card->dapm_list, list) {
1607 soc_dapm_async_complete(d);
1608 }
Mark Brown163cac02009-06-07 10:12:52 +01001609}
1610
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001611static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001612{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001613 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001614 struct snd_soc_dapm_widget_list *wlist;
1615 struct snd_soc_dapm_widget *w = NULL;
1616 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001617 int ret;
1618
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001619 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001620 return;
1621
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001622 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001623
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001624 for (wi = 0; wi < wlist->num_widgets; wi++) {
1625 w = wlist->widgets[wi];
1626
1627 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1628 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1629 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001630 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001631 w->name, ret);
1632 }
Mark Brown97404f22010-12-14 16:13:57 +00001633 }
1634
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001635 if (!w)
1636 return;
1637
Liam Girdwood49575fb52012-03-06 18:16:19 +00001638 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001639 update->val);
1640 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001641 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001642 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001643
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001644 for (wi = 0; wi < wlist->num_widgets; wi++) {
1645 w = wlist->widgets[wi];
1646
1647 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1648 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1649 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001650 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001651 w->name, ret);
1652 }
Mark Brown97404f22010-12-14 16:13:57 +00001653 }
1654}
1655
Mark Brown9d0624a2011-02-18 11:49:43 -08001656/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1657 * they're changing state.
1658 */
1659static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1660{
1661 struct snd_soc_dapm_context *d = data;
1662 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001663
Mark Brown56fba412011-06-04 11:25:10 +01001664 /* If we're off and we're not supposed to be go into STANDBY */
1665 if (d->bias_level == SND_SOC_BIAS_OFF &&
1666 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001667 if (d->dev)
1668 pm_runtime_get_sync(d->dev);
1669
Mark Brown9d0624a2011-02-18 11:49:43 -08001670 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1671 if (ret != 0)
1672 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001673 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001674 }
1675
Mark Brown56fba412011-06-04 11:25:10 +01001676 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1677 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001678 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1679 if (ret != 0)
1680 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001681 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001682 }
1683}
1684
1685/* Async callback run prior to DAPM sequences - brings to their final
1686 * state.
1687 */
1688static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1689{
1690 struct snd_soc_dapm_context *d = data;
1691 int ret;
1692
1693 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001694 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1695 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1696 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001697 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1698 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001699 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001700 ret);
1701 }
1702
1703 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001704 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1705 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001706 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1707 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001708 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1709 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001710
1711 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001712 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001713 }
1714
1715 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001716 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1717 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001718 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1719 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001720 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001721 ret);
1722 }
1723}
Mark Brown97404f22010-12-14 16:13:57 +00001724
Mark Brownfe4fda52011-10-03 22:36:57 +01001725static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1726 bool power, bool connect)
1727{
1728 /* If a connection is being made or broken then that update
1729 * will have marked the peer dirty, otherwise the widgets are
1730 * not connected and this update has no impact. */
1731 if (!connect)
1732 return;
1733
1734 /* If the peer is already in the state we're moving to then we
1735 * won't have an impact on it. */
1736 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001737 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001738}
1739
Mark Brown05623c42011-09-28 17:02:31 +01001740static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1741 struct list_head *up_list,
1742 struct list_head *down_list)
1743{
Mark Browndb432b42011-10-03 21:06:40 +01001744 struct snd_soc_dapm_path *path;
1745
Mark Brown05623c42011-09-28 17:02:31 +01001746 if (w->power == power)
1747 return;
1748
1749 trace_snd_soc_dapm_widget_power(w, power);
1750
Mark Browndb432b42011-10-03 21:06:40 +01001751 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001752 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001753 */
1754 list_for_each_entry(path, &w->sources, list_sink) {
1755 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001756 dapm_widget_set_peer_power(path->source, power,
1757 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001758 }
1759 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001760 switch (w->id) {
1761 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001762 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001763 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001764 case snd_soc_dapm_kcontrol:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001765 /* Supplies can't affect their outputs, only their inputs */
1766 break;
1767 default:
1768 list_for_each_entry(path, &w->sinks, list_source) {
1769 if (path->sink) {
1770 dapm_widget_set_peer_power(path->sink, power,
1771 path->connect);
1772 }
Mark Browndb432b42011-10-03 21:06:40 +01001773 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001774 break;
Mark Browndb432b42011-10-03 21:06:40 +01001775 }
1776
Mark Brown05623c42011-09-28 17:02:31 +01001777 if (power)
1778 dapm_seq_insert(w, up_list, true);
1779 else
1780 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001781}
1782
Mark Brown7c81beb2011-09-20 22:22:32 +01001783static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1784 struct list_head *up_list,
1785 struct list_head *down_list)
1786{
Mark Brown7c81beb2011-09-20 22:22:32 +01001787 int power;
1788
1789 switch (w->id) {
1790 case snd_soc_dapm_pre:
1791 dapm_seq_insert(w, down_list, false);
1792 break;
1793 case snd_soc_dapm_post:
1794 dapm_seq_insert(w, up_list, true);
1795 break;
1796
1797 default:
Mark Brownd8050022011-09-28 18:28:23 +01001798 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001799
Mark Brown05623c42011-09-28 17:02:31 +01001800 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001801 break;
1802 }
1803}
1804
Mark Brown42aa3412009-03-01 19:21:10 +00001805/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001806 * Scan each dapm widget for complete audio path.
1807 * A complete path is a route that has valid endpoints i.e.:-
1808 *
1809 * o DAC to output pin.
1810 * o Input Pin to ADC.
1811 * o Input pin to Output pin (bypass, sidetone)
1812 * o DAC to ADC (loopback).
1813 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001814static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001815{
1816 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001817 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001818 LIST_HEAD(up_list);
1819 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001820 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001821 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001822
Mark Brown84e90932010-11-04 00:07:02 -04001823 trace_snd_soc_dapm_start(card);
1824
Mark Brown56fba412011-06-04 11:25:10 +01001825 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001826 if (d->idle_bias_off)
1827 d->target_bias_level = SND_SOC_BIAS_OFF;
1828 else
1829 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001830 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001831
Liam Girdwood6c120e12012-02-15 15:15:34 +00001832 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001833
Mark Brown6d3ddc82009-05-16 17:47:29 +01001834 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001835 * lists indicating if they should be powered up or down. We
1836 * only check widgets that have been flagged as dirty but note
1837 * that new widgets may be added to the dirty list while we
1838 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001839 */
Mark Browndb432b42011-10-03 21:06:40 +01001840 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001841 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001842 }
1843
Mark Brownf9de6d72011-09-28 17:19:47 +01001844 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001845 switch (w->id) {
1846 case snd_soc_dapm_pre:
1847 case snd_soc_dapm_post:
1848 /* These widgets always need to be powered */
1849 break;
1850 default:
1851 list_del_init(&w->dirty);
1852 break;
1853 }
Mark Browndb432b42011-10-03 21:06:40 +01001854
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001855 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001856 d = w->dapm;
1857
1858 /* Supplies and micbiases only bring the
1859 * context up to STANDBY as unless something
1860 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001861 * generally don't require full power. Signal
1862 * generators are virtual pins and have no
1863 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001864 */
1865 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001866 case snd_soc_dapm_siggen:
Lars-Peter Clausenda83fea2013-10-05 19:26:17 +02001867 case snd_soc_dapm_vmid:
Mark Brownafe62362012-01-25 19:55:22 +00001868 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001869 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001870 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001871 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001872 case snd_soc_dapm_micbias:
1873 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1874 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1875 break;
1876 default:
1877 d->target_bias_level = SND_SOC_BIAS_ON;
1878 break;
1879 }
1880 }
1881
1882 }
1883
Mark Brown85a843c2011-09-21 21:29:47 +01001884 /* Force all contexts in the card to the same bias state if
1885 * they're not ground referenced.
1886 */
Mark Brown56fba412011-06-04 11:25:10 +01001887 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001888 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001889 if (d->target_bias_level > bias)
1890 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001891 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001892 if (!d->idle_bias_off)
1893 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001894
Mark Brownde02d072011-09-20 21:43:24 +01001895 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001896
Mark Brown9d0624a2011-02-18 11:49:43 -08001897 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001898 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001899 async_schedule_domain(dapm_pre_sequence_async, d,
1900 &async_domain);
1901 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001902
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001903 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001904 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001905 }
1906
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001907 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001908 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001909 }
1910
Mark Brown6d3ddc82009-05-16 17:47:29 +01001911 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001912 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001913
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001914 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001915
Mark Brown6d3ddc82009-05-16 17:47:29 +01001916 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001917 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001918
Mark Brown9d0624a2011-02-18 11:49:43 -08001919 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001920 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001921 async_schedule_domain(dapm_post_sequence_async, d,
1922 &async_domain);
1923 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001924
Liam Girdwood8078d872012-02-15 15:15:35 +00001925 /* do we need to notify any clients that DAPM event is complete */
1926 list_for_each_entry(d, &card->dapm_list, list) {
1927 if (d->stream_event)
1928 d->stream_event(d, event);
1929 }
1930
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001931 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001932 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001933 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001934
Mark Brown84e90932010-11-04 00:07:02 -04001935 trace_snd_soc_dapm_done(card);
1936
Mark Brown42aa3412009-03-01 19:21:10 +00001937 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001938}
1939
Mark Brown79fb9382009-08-21 16:38:13 +01001940#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001941static ssize_t dapm_widget_power_read_file(struct file *file,
1942 char __user *user_buf,
1943 size_t count, loff_t *ppos)
1944{
1945 struct snd_soc_dapm_widget *w = file->private_data;
1946 char *buf;
1947 int in, out;
1948 ssize_t ret;
1949 struct snd_soc_dapm_path *p = NULL;
1950
1951 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1952 if (!buf)
1953 return -ENOMEM;
1954
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001955 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001956 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001957 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001958 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001959
Mark Brownf13ebad2012-03-03 18:01:01 +00001960 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1961 w->name, w->power ? "On" : "Off",
1962 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001963
Mark Brownd033c362009-12-04 15:25:56 +00001964 if (w->reg >= 0)
1965 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001966 " - R%d(0x%x) mask 0x%x",
1967 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001968
1969 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1970
Mark Brown3eef08b2009-09-14 16:49:00 +01001971 if (w->sname)
1972 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1973 w->sname,
1974 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001975
1976 list_for_each_entry(p, &w->sources, list_sink) {
Takashi Iwaiff186202013-10-28 14:21:49 +01001977 if (p->connected && !p->connected(w, p->source))
Mark Brown215edda2009-09-08 18:59:05 +01001978 continue;
1979
Mark Brown79fb9382009-08-21 16:38:13 +01001980 if (p->connect)
1981 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001982 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001983 p->name ? p->name : "static",
1984 p->source->name);
1985 }
1986 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001987 if (p->connected && !p->connected(w, p->sink))
1988 continue;
1989
Mark Brown79fb9382009-08-21 16:38:13 +01001990 if (p->connect)
1991 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001992 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001993 p->name ? p->name : "static",
1994 p->sink->name);
1995 }
1996
1997 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1998
1999 kfree(buf);
2000 return ret;
2001}
2002
2003static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002004 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01002005 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002006 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01002007};
2008
Mark Brownef49e4f2011-04-04 20:48:13 +09002009static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2010 size_t count, loff_t *ppos)
2011{
2012 struct snd_soc_dapm_context *dapm = file->private_data;
2013 char *level;
2014
2015 switch (dapm->bias_level) {
2016 case SND_SOC_BIAS_ON:
2017 level = "On\n";
2018 break;
2019 case SND_SOC_BIAS_PREPARE:
2020 level = "Prepare\n";
2021 break;
2022 case SND_SOC_BIAS_STANDBY:
2023 level = "Standby\n";
2024 break;
2025 case SND_SOC_BIAS_OFF:
2026 level = "Off\n";
2027 break;
2028 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01002029 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
Mark Brownef49e4f2011-04-04 20:48:13 +09002030 level = "Unknown\n";
2031 break;
2032 }
2033
2034 return simple_read_from_buffer(user_buf, count, ppos, level,
2035 strlen(level));
2036}
2037
2038static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002039 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09002040 .read = dapm_bias_read_file,
2041 .llseek = default_llseek,
2042};
2043
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002044void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2045 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002046{
Mark Brown79fb9382009-08-21 16:38:13 +01002047 struct dentry *d;
2048
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002049 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2050
2051 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00002052 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002053 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002054 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002055 }
Mark Brown79fb9382009-08-21 16:38:13 +01002056
Mark Brownef49e4f2011-04-04 20:48:13 +09002057 d = debugfs_create_file("bias_level", 0444,
2058 dapm->debugfs_dapm, dapm,
2059 &dapm_bias_fops);
2060 if (!d)
2061 dev_warn(dapm->dev,
2062 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002063}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002064
2065static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2066{
2067 struct snd_soc_dapm_context *dapm = w->dapm;
2068 struct dentry *d;
2069
2070 if (!dapm->debugfs_dapm || !w->name)
2071 return;
2072
2073 d = debugfs_create_file(w->name, 0444,
2074 dapm->debugfs_dapm, w,
2075 &dapm_widget_power_fops);
2076 if (!d)
2077 dev_warn(w->dapm->dev,
2078 "ASoC: Failed to create %s debugfs file\n",
2079 w->name);
2080}
2081
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002082static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2083{
2084 debugfs_remove_recursive(dapm->debugfs_dapm);
2085}
2086
Mark Brown79fb9382009-08-21 16:38:13 +01002087#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002088void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2089 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002090{
2091}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002092
2093static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2094{
2095}
2096
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002097static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2098{
2099}
2100
Mark Brown79fb9382009-08-21 16:38:13 +01002101#endif
2102
Richard Purdie2b97eab2006-10-06 18:32:18 +02002103/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002104static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002105 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002106{
2107 struct snd_soc_dapm_path *path;
2108 int found = 0;
2109
Richard Purdie2b97eab2006-10-06 18:32:18 +02002110 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002111 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Zhaocb01e2b2008-10-07 08:05:20 +08002112 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02002113 continue;
2114
2115 found = 1;
2116 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01002117 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002118 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01002119 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01002120 } else {
2121 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01002122 dapm_mark_dirty(path->source,
2123 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002124 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01002125 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002126 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002127 }
2128
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002129 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002130 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002131
Liam Girdwood618dae12012-04-25 12:12:51 +01002132 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002133}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002134
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002135int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002136 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2137 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002138{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002139 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002140 int ret;
2141
Liam Girdwood3cd04342012-03-09 12:02:08 +00002142 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002143 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002144 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002145 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002146 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002147 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002148 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002149 return ret;
2150}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002151EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002152
Milan plzik1b075e32008-01-10 14:39:46 +01002153/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002154static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002155 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002156{
2157 struct snd_soc_dapm_path *path;
2158 int found = 0;
2159
Richard Purdie2b97eab2006-10-06 18:32:18 +02002160 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002161 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002162 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002163 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002164 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002165 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002166 }
2167
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002168 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002169 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002170
Liam Girdwood618dae12012-04-25 12:12:51 +01002171 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002172}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002173
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002174int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002175 struct snd_kcontrol *kcontrol, int connect,
2176 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002177{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002178 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002179 int ret;
2180
Liam Girdwood3cd04342012-03-09 12:02:08 +00002181 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002182 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002183 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002184 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002185 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002186 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002187 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002188 return ret;
2189}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002190EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002191
2192/* show dapm widget status in sys fs */
2193static ssize_t dapm_widget_show(struct device *dev,
2194 struct device_attribute *attr, char *buf)
2195{
Mark Brown36ae1a92012-01-06 17:12:45 -08002196 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002197 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002198 struct snd_soc_dapm_widget *w;
2199 int count = 0;
2200 char *state = "not set";
2201
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002202 list_for_each_entry(w, &codec->card->widgets, list) {
2203 if (w->dapm != &codec->dapm)
2204 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002205
2206 /* only display widgets that burnm power */
2207 switch (w->id) {
2208 case snd_soc_dapm_hp:
2209 case snd_soc_dapm_mic:
2210 case snd_soc_dapm_spk:
2211 case snd_soc_dapm_line:
2212 case snd_soc_dapm_micbias:
2213 case snd_soc_dapm_dac:
2214 case snd_soc_dapm_adc:
2215 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002216 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002217 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002218 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002219 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002220 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002221 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002222 if (w->name)
2223 count += sprintf(buf + count, "%s: %s\n",
2224 w->name, w->power ? "On":"Off");
2225 break;
2226 default:
2227 break;
2228 }
2229 }
2230
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002231 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002232 case SND_SOC_BIAS_ON:
2233 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002234 break;
Mark Brown0be98982008-05-19 12:31:28 +02002235 case SND_SOC_BIAS_PREPARE:
2236 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002237 break;
Mark Brown0be98982008-05-19 12:31:28 +02002238 case SND_SOC_BIAS_STANDBY:
2239 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002240 break;
Mark Brown0be98982008-05-19 12:31:28 +02002241 case SND_SOC_BIAS_OFF:
2242 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002243 break;
2244 }
2245 count += sprintf(buf + count, "PM State: %s\n", state);
2246
2247 return count;
2248}
2249
2250static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2251
2252int snd_soc_dapm_sys_add(struct device *dev)
2253{
Troy Kisky12ef1932008-10-13 17:42:14 -07002254 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002255}
2256
2257static void snd_soc_dapm_sys_remove(struct device *dev)
2258{
Mark Brownaef90842009-05-16 17:53:16 +01002259 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002260}
2261
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002262static void dapm_free_path(struct snd_soc_dapm_path *path)
2263{
2264 list_del(&path->list_sink);
2265 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002266 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002267 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002268 kfree(path);
2269}
2270
Richard Purdie2b97eab2006-10-06 18:32:18 +02002271/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002272static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002273{
2274 struct snd_soc_dapm_widget *w, *next_w;
2275 struct snd_soc_dapm_path *p, *next_p;
2276
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002277 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2278 if (w->dapm != dapm)
2279 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002280 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002281 /*
2282 * remove source and sink paths associated to this widget.
2283 * While removing the path, remove reference to it from both
2284 * source and sink widgets so that path is removed only once.
2285 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002286 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2287 dapm_free_path(p);
2288
2289 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2290 dapm_free_path(p);
2291
Stephen Warrenfad59882011-04-28 17:37:59 -06002292 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002293 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002294 kfree(w);
2295 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002296}
2297
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002298static struct snd_soc_dapm_widget *dapm_find_widget(
2299 struct snd_soc_dapm_context *dapm, const char *pin,
2300 bool search_other_contexts)
2301{
2302 struct snd_soc_dapm_widget *w;
2303 struct snd_soc_dapm_widget *fallback = NULL;
2304
2305 list_for_each_entry(w, &dapm->card->widgets, list) {
2306 if (!strcmp(w->name, pin)) {
2307 if (w->dapm == dapm)
2308 return w;
2309 else
2310 fallback = w;
2311 }
2312 }
2313
2314 if (search_other_contexts)
2315 return fallback;
2316
2317 return NULL;
2318}
2319
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002320static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002321 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002322{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002323 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002324
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002325 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002326 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002327 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002328 }
2329
Mark Brown1a8b2d92012-02-16 11:50:07 -08002330 if (w->connected != status)
2331 dapm_mark_dirty(w, "pin configuration");
2332
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002333 w->connected = status;
2334 if (status == 0)
2335 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002336
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002337 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002338}
2339
Richard Purdie2b97eab2006-10-06 18:32:18 +02002340/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002341 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002342 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002343 *
2344 * Walks all dapm audio paths and powers widgets according to their
2345 * stream or path usage.
2346 *
2347 * Returns 0 for success.
2348 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002349int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002350{
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002351 int ret;
2352
Mark Brown4f4c0072011-10-07 14:29:19 +01002353 /*
2354 * Suppress early reports (eg, jacks syncing their state) to avoid
2355 * silly DAPM runs during card startup.
2356 */
2357 if (!dapm->card || !dapm->card->instantiated)
2358 return 0;
2359
Liam Girdwood3cd04342012-03-09 12:02:08 +00002360 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002361 ret = dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002362 mutex_unlock(&dapm->card->dapm_mutex);
2363 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002364}
Liam Girdwooda5302182008-07-07 13:35:17 +01002365EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002366
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002367static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2368 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2369 const char *control,
2370 int (*connected)(struct snd_soc_dapm_widget *source,
2371 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002372{
2373 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002374 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002375
2376 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2377 if (!path)
2378 return -ENOMEM;
2379
2380 path->source = wsource;
2381 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002382 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002383 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002384 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002385 INIT_LIST_HEAD(&path->list_source);
2386 INIT_LIST_HEAD(&path->list_sink);
2387
2388 /* check for external widgets */
2389 if (wsink->id == snd_soc_dapm_input) {
2390 if (wsource->id == snd_soc_dapm_micbias ||
2391 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002392 wsource->id == snd_soc_dapm_line ||
2393 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002394 wsink->ext = 1;
2395 }
2396 if (wsource->id == snd_soc_dapm_output) {
2397 if (wsink->id == snd_soc_dapm_spk ||
2398 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002399 wsink->id == snd_soc_dapm_line ||
2400 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002401 wsource->ext = 1;
2402 }
2403
Lars-Peter Clausen34742cb2013-08-27 15:50:54 +02002404 dapm_mark_dirty(wsource, "Route added");
2405 dapm_mark_dirty(wsink, "Route added");
2406
Richard Purdie2b97eab2006-10-06 18:32:18 +02002407 /* connect static paths */
2408 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002409 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002410 list_add(&path->list_sink, &wsink->sources);
2411 list_add(&path->list_source, &wsource->sinks);
2412 path->connect = 1;
2413 return 0;
2414 }
2415
2416 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002417 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002418 case snd_soc_dapm_adc:
2419 case snd_soc_dapm_dac:
2420 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002421 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002422 case snd_soc_dapm_input:
2423 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002424 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002425 case snd_soc_dapm_micbias:
2426 case snd_soc_dapm_vmid:
2427 case snd_soc_dapm_pre:
2428 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002429 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002430 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002431 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002432 case snd_soc_dapm_aif_in:
2433 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002434 case snd_soc_dapm_dai_in:
2435 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002436 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002437 case snd_soc_dapm_kcontrol:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002438 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002439 list_add(&path->list_sink, &wsink->sources);
2440 list_add(&path->list_source, &wsource->sinks);
2441 path->connect = 1;
2442 return 0;
2443 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002444 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002445 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002446 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002447 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002448 if (ret != 0)
2449 goto err;
2450 break;
2451 case snd_soc_dapm_switch:
2452 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002453 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002454 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002455 if (ret != 0)
2456 goto err;
2457 break;
2458 case snd_soc_dapm_hp:
2459 case snd_soc_dapm_mic:
2460 case snd_soc_dapm_line:
2461 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002462 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002463 list_add(&path->list_sink, &wsink->sources);
2464 list_add(&path->list_source, &wsource->sinks);
2465 path->connect = 0;
2466 return 0;
2467 }
Mark Brownfabd0382012-07-05 17:20:06 +01002468
Richard Purdie2b97eab2006-10-06 18:32:18 +02002469 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002470err:
2471 kfree(path);
2472 return ret;
2473}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002474
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002475static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2476 const struct snd_soc_dapm_route *route)
2477{
2478 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2479 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2480 const char *sink;
2481 const char *source;
2482 char prefixed_sink[80];
2483 char prefixed_source[80];
2484 int ret;
2485
2486 if (dapm->codec && dapm->codec->name_prefix) {
2487 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2488 dapm->codec->name_prefix, route->sink);
2489 sink = prefixed_sink;
2490 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2491 dapm->codec->name_prefix, route->source);
2492 source = prefixed_source;
2493 } else {
2494 sink = route->sink;
2495 source = route->source;
2496 }
2497
2498 /*
2499 * find src and dest widgets over all widgets but favor a widget from
2500 * current DAPM context
2501 */
2502 list_for_each_entry(w, &dapm->card->widgets, list) {
2503 if (!wsink && !(strcmp(w->name, sink))) {
2504 wtsink = w;
2505 if (w->dapm == dapm)
2506 wsink = w;
2507 continue;
2508 }
2509 if (!wsource && !(strcmp(w->name, source))) {
2510 wtsource = w;
2511 if (w->dapm == dapm)
2512 wsource = w;
2513 }
2514 }
2515 /* use widget from another DAPM context if not found from this */
2516 if (!wsink)
2517 wsink = wtsink;
2518 if (!wsource)
2519 wsource = wtsource;
2520
2521 if (wsource == NULL) {
2522 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2523 route->source);
2524 return -ENODEV;
2525 }
2526 if (wsink == NULL) {
2527 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2528 route->sink);
2529 return -ENODEV;
2530 }
2531
2532 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2533 route->connected);
2534 if (ret)
2535 goto err;
2536
2537 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002538err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002539 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002540 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002541 return ret;
2542}
Mark Brown105f1c22008-05-13 14:52:19 +02002543
Mark Brownefcc3c62012-07-05 17:24:19 +01002544static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2545 const struct snd_soc_dapm_route *route)
2546{
2547 struct snd_soc_dapm_path *path, *p;
2548 const char *sink;
2549 const char *source;
2550 char prefixed_sink[80];
2551 char prefixed_source[80];
2552
2553 if (route->control) {
2554 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002555 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002556 return -EINVAL;
2557 }
2558
2559 if (dapm->codec && dapm->codec->name_prefix) {
2560 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2561 dapm->codec->name_prefix, route->sink);
2562 sink = prefixed_sink;
2563 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2564 dapm->codec->name_prefix, route->source);
2565 source = prefixed_source;
2566 } else {
2567 sink = route->sink;
2568 source = route->source;
2569 }
2570
2571 path = NULL;
2572 list_for_each_entry(p, &dapm->card->paths, list) {
2573 if (strcmp(p->source->name, source) != 0)
2574 continue;
2575 if (strcmp(p->sink->name, sink) != 0)
2576 continue;
2577 path = p;
2578 break;
2579 }
2580
2581 if (path) {
2582 dapm_mark_dirty(path->source, "Route removed");
2583 dapm_mark_dirty(path->sink, "Route removed");
2584
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002585 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002586 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002587 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002588 source, sink);
2589 }
2590
2591 return 0;
2592}
2593
Mark Brown105f1c22008-05-13 14:52:19 +02002594/**
Mark Brown105f1c22008-05-13 14:52:19 +02002595 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002596 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002597 * @route: audio routes
2598 * @num: number of routes
2599 *
2600 * Connects 2 dapm widgets together via a named audio path. The sink is
2601 * the widget receiving the audio signal, whilst the source is the sender
2602 * of the audio signal.
2603 *
2604 * Returns 0 for success else error. On error all resources can be freed
2605 * with a call to snd_soc_card_free().
2606 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002607int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002608 const struct snd_soc_dapm_route *route, int num)
2609{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002610 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002611
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002612 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002613 for (i = 0; i < num; i++) {
Mark Brown62d4a4b2012-06-22 12:21:49 +01002614 r = snd_soc_dapm_add_route(dapm, route);
2615 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002616 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2617 route->source,
2618 route->control ? route->control : "direct",
2619 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002620 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002621 }
2622 route++;
2623 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002624 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002625
Dan Carpenter60884c22012-04-13 22:25:43 +03002626 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002627}
2628EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2629
Mark Brownefcc3c62012-07-05 17:24:19 +01002630/**
2631 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2632 * @dapm: DAPM context
2633 * @route: audio routes
2634 * @num: number of routes
2635 *
2636 * Removes routes from the DAPM context.
2637 */
2638int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2639 const struct snd_soc_dapm_route *route, int num)
2640{
2641 int i, ret = 0;
2642
2643 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2644 for (i = 0; i < num; i++) {
2645 snd_soc_dapm_del_route(dapm, route);
2646 route++;
2647 }
2648 mutex_unlock(&dapm->card->dapm_mutex);
2649
2650 return ret;
2651}
2652EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2653
Mark Brownbf3a9e12011-06-13 16:42:29 +01002654static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2655 const struct snd_soc_dapm_route *route)
2656{
2657 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2658 route->source,
2659 true);
2660 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2661 route->sink,
2662 true);
2663 struct snd_soc_dapm_path *path;
2664 int count = 0;
2665
2666 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002667 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002668 route->source);
2669 return -ENODEV;
2670 }
2671
2672 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002673 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002674 route->sink);
2675 return -ENODEV;
2676 }
2677
2678 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002679 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002680 route->source, route->sink);
2681
2682 list_for_each_entry(path, &source->sinks, list_source) {
2683 if (path->sink == sink) {
2684 path->weak = 1;
2685 count++;
2686 }
2687 }
2688
2689 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002690 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002691 route->source, route->sink);
2692 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002693 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002694 count, route->source, route->sink);
2695
2696 return 0;
2697}
2698
2699/**
2700 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2701 * @dapm: DAPM context
2702 * @route: audio routes
2703 * @num: number of routes
2704 *
2705 * Mark existing routes matching those specified in the passed array
2706 * as being weak, meaning that they are ignored for the purpose of
2707 * power decisions. The main intended use case is for sidetone paths
2708 * which couple audio between other independent paths if they are both
2709 * active in order to make the combination work better at the user
2710 * level but which aren't intended to be "used".
2711 *
2712 * Note that CODEC drivers should not use this as sidetone type paths
2713 * can frequently also be used as bypass paths.
2714 */
2715int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2716 const struct snd_soc_dapm_route *route, int num)
2717{
2718 int i, err;
2719 int ret = 0;
2720
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002721 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002722 for (i = 0; i < num; i++) {
2723 err = snd_soc_dapm_weak_route(dapm, route);
2724 if (err)
2725 ret = err;
2726 route++;
2727 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002728 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002729
2730 return ret;
2731}
2732EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2733
Mark Brown105f1c22008-05-13 14:52:19 +02002734/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002735 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002736 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002737 *
2738 * Checks the codec for any new dapm widgets and creates them if found.
2739 *
2740 * Returns 0 for success.
2741 */
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002742int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002743{
2744 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002745 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002746
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002747 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002748
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002749 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002750 {
2751 if (w->new)
2752 continue;
2753
Stephen Warrenfad59882011-04-28 17:37:59 -06002754 if (w->num_kcontrols) {
2755 w->kcontrols = kzalloc(w->num_kcontrols *
2756 sizeof(struct snd_kcontrol *),
2757 GFP_KERNEL);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002758 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002759 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002760 return -ENOMEM;
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002761 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002762 }
2763
Richard Purdie2b97eab2006-10-06 18:32:18 +02002764 switch(w->id) {
2765 case snd_soc_dapm_switch:
2766 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002767 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002768 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002769 break;
2770 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002771 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002772 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002773 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002774 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002775 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002776 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002777 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002778 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002779 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002780 break;
2781 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002782
2783 /* Read the initial power state from the device */
2784 if (w->reg >= 0) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002785 val = soc_widget_read(w, w->reg) >> w->shift;
2786 val &= w->mask;
2787 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002788 w->power = 1;
2789 }
2790
Richard Purdie2b97eab2006-10-06 18:32:18 +02002791 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002792
Mark Brown7508b122011-10-05 12:09:12 +01002793 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002794 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002795 }
2796
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002797 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2798 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002799 return 0;
2800}
2801EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2802
2803/**
2804 * snd_soc_dapm_get_volsw - dapm mixer get callback
2805 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002806 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002807 *
2808 * Callback to get the value of a dapm mixer control.
2809 *
2810 * Returns 0 for success.
2811 */
2812int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2813 struct snd_ctl_elem_value *ucontrol)
2814{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002815 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002816 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002817 struct soc_mixer_control *mc =
2818 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002819 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002820 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002821 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002822 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002823 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002824 unsigned int val;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002825
2826 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002827 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002828 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002829 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002830
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002831 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002832 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002833 val = (snd_soc_read(codec, reg) >> shift) & mask;
2834 else
2835 val = dapm_kcontrol_get_value(kcontrol);
2836 mutex_unlock(&card->dapm_mutex);
2837
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002838 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002839 ucontrol->value.integer.value[0] = max - val;
2840 else
2841 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002842
2843 return 0;
2844}
2845EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2846
2847/**
2848 * snd_soc_dapm_put_volsw - dapm mixer set callback
2849 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002850 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002851 *
2852 * Callback to set the value of a dapm mixer control.
2853 *
2854 * Returns 0 for success.
2855 */
2856int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2857 struct snd_ctl_elem_value *ucontrol)
2858{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002859 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002860 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002861 struct soc_mixer_control *mc =
2862 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002863 int reg = mc->reg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002864 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002865 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002866 unsigned int mask = (1 << fls(max)) - 1;
2867 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002868 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002869 int connect, change;
2870 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002871
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002872 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002873 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002874 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002875 kcontrol->id.name);
2876
Richard Purdie2b97eab2006-10-06 18:32:18 +02002877 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002878 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002879
2880 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002881 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002882
Liam Girdwood3cd04342012-03-09 12:02:08 +00002883 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002884
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002885 change = dapm_kcontrol_set_value(kcontrol, val);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002886
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002887 if (reg != SND_SOC_NOPM) {
2888 mask = mask << shift;
2889 val = val << shift;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002890
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002891 change = snd_soc_test_bits(codec, reg, mask, val);
2892 }
2893
Mark Brown97404f22010-12-14 16:13:57 +00002894 if (change) {
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002895 if (reg != SND_SOC_NOPM) {
2896 update.kcontrol = kcontrol;
2897 update.reg = reg;
2898 update.mask = mask;
2899 update.val = val;
Mark Brown283375c2009-12-07 18:09:03 +00002900
Lars-Peter Clausen249ce132013-10-06 13:43:49 +02002901 card->update = &update;
2902 }
Mark Brown97404f22010-12-14 16:13:57 +00002903
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002904 soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002905
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002906 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002907 }
2908
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002909 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002910 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002911}
2912EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2913
2914/**
2915 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2916 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002917 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002918 *
2919 * Callback to get the value of a dapm enumerated double mixer control.
2920 *
2921 * Returns 0 for success.
2922 */
2923int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2924 struct snd_ctl_elem_value *ucontrol)
2925{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002926 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002927 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002928 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002929
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002930 val = snd_soc_read(codec, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002931 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002932 if (e->shift_l != e->shift_r)
2933 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002934 (val >> e->shift_r) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002935
2936 return 0;
2937}
2938EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2939
2940/**
2941 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2942 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002943 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002944 *
2945 * Callback to set the value of a dapm enumerated double mixer control.
2946 *
2947 * Returns 0 for success.
2948 */
2949int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2950 struct snd_ctl_elem_value *ucontrol)
2951{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002952 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002953 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002954 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002955 unsigned int val, mux, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002956 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002957 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002958
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002959 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002960 return -EINVAL;
2961 mux = ucontrol->value.enumerated.item[0];
2962 val = mux << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002963 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002964 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002965 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002966 return -EINVAL;
2967 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002968 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002969 }
2970
Liam Girdwood3cd04342012-03-09 12:02:08 +00002971 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002972
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002973 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002974 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002975 update.kcontrol = kcontrol;
2976 update.reg = e->reg;
2977 update.mask = mask;
2978 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002979 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002980
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002981 soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002982
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002983 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002984 }
2985
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00002986 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002987 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002988}
2989EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2990
2991/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002992 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2993 * @kcontrol: mixer control
2994 * @ucontrol: control element information
2995 *
2996 * Returns 0 for success.
2997 */
2998int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2999 struct snd_ctl_elem_value *ucontrol)
3000{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003001 ucontrol->value.enumerated.item[0] = dapm_kcontrol_get_value(kcontrol);
Mark Brownd2b247a2009-10-06 15:21:04 +01003002 return 0;
3003}
3004EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
3005
3006/**
3007 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
3008 * @kcontrol: mixer control
3009 * @ucontrol: control element information
3010 *
3011 * Returns 0 for success.
3012 */
3013int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
3014 struct snd_ctl_elem_value *ucontrol)
3015{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003016 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003017 struct snd_soc_card *card = codec->card;
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003018 unsigned int value;
Mark Brownd2b247a2009-10-06 15:21:04 +01003019 struct soc_enum *e =
3020 (struct soc_enum *)kcontrol->private_value;
3021 int change;
Mark Brownd2b247a2009-10-06 15:21:04 +01003022
3023 if (ucontrol->value.enumerated.item[0] >= e->max)
3024 return -EINVAL;
3025
Liam Girdwood3cd04342012-03-09 12:02:08 +00003026 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01003027
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003028 value = ucontrol->value.enumerated.item[0];
3029 change = dapm_kcontrol_set_value(kcontrol, value);
3030 if (change)
3031 soc_dapm_mux_update_power(card, kcontrol, value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003032
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003033 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02003034 return change;
Mark Brownd2b247a2009-10-06 15:21:04 +01003035}
3036EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
3037
3038/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003039 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
3040 * callback
3041 * @kcontrol: mixer control
3042 * @ucontrol: control element information
3043 *
3044 * Callback to get the value of a dapm semi enumerated double mixer control.
3045 *
3046 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3047 * used for handling bitfield coded enumeration for example.
3048 *
3049 * Returns 0 for success.
3050 */
3051int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
3052 struct snd_ctl_elem_value *ucontrol)
3053{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003054 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02003055 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03003056 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003057
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003058 reg_val = snd_soc_read(codec, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003059 val = (reg_val >> e->shift_l) & e->mask;
3060 for (mux = 0; mux < e->max; mux++) {
3061 if (val == e->values[mux])
3062 break;
3063 }
3064 ucontrol->value.enumerated.item[0] = mux;
3065 if (e->shift_l != e->shift_r) {
3066 val = (reg_val >> e->shift_r) & e->mask;
3067 for (mux = 0; mux < e->max; mux++) {
3068 if (val == e->values[mux])
3069 break;
3070 }
3071 ucontrol->value.enumerated.item[1] = mux;
3072 }
3073
3074 return 0;
3075}
3076EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
3077
3078/**
3079 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
3080 * callback
3081 * @kcontrol: mixer control
3082 * @ucontrol: control element information
3083 *
3084 * Callback to set the value of a dapm semi enumerated double mixer control.
3085 *
3086 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3087 * used for handling bitfield coded enumeration for example.
3088 *
3089 * Returns 0 for success.
3090 */
3091int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
3092 struct snd_ctl_elem_value *ucontrol)
3093{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003094 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003095 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02003096 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01003097 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03003098 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00003099 struct snd_soc_dapm_update update;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003100
3101 if (ucontrol->value.enumerated.item[0] > e->max - 1)
3102 return -EINVAL;
3103 mux = ucontrol->value.enumerated.item[0];
3104 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
3105 mask = e->mask << e->shift_l;
3106 if (e->shift_l != e->shift_r) {
3107 if (ucontrol->value.enumerated.item[1] > e->max - 1)
3108 return -EINVAL;
3109 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
3110 mask |= e->mask << e->shift_r;
3111 }
3112
Liam Girdwood3cd04342012-03-09 12:02:08 +00003113 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003114
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003115 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003116 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02003117 update.kcontrol = kcontrol;
3118 update.reg = e->reg;
3119 update.mask = mask;
3120 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003121 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00003122
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003123 soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003124
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003125 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003126 }
3127
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003128 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00003129 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003130}
3131EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
3132
3133/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003134 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3135 *
3136 * @kcontrol: mixer control
3137 * @uinfo: control element information
3138 *
3139 * Callback to provide information about a pin switch control.
3140 */
3141int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3142 struct snd_ctl_elem_info *uinfo)
3143{
3144 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3145 uinfo->count = 1;
3146 uinfo->value.integer.min = 0;
3147 uinfo->value.integer.max = 1;
3148
3149 return 0;
3150}
3151EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3152
3153/**
3154 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3155 *
3156 * @kcontrol: mixer control
3157 * @ucontrol: Value
3158 */
3159int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3160 struct snd_ctl_elem_value *ucontrol)
3161{
Mark Brown48a8c392012-02-14 17:11:15 -08003162 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003163 const char *pin = (const char *)kcontrol->private_value;
3164
Liam Girdwood3cd04342012-03-09 12:02:08 +00003165 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003166
3167 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003168 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003169
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003170 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003171
3172 return 0;
3173}
3174EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3175
3176/**
3177 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3178 *
3179 * @kcontrol: mixer control
3180 * @ucontrol: Value
3181 */
3182int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3183 struct snd_ctl_elem_value *ucontrol)
3184{
Mark Brown48a8c392012-02-14 17:11:15 -08003185 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003186 const char *pin = (const char *)kcontrol->private_value;
3187
Liam Girdwood3cd04342012-03-09 12:02:08 +00003188 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003189
3190 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003191 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003192 else
Mark Brown48a8c392012-02-14 17:11:15 -08003193 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003194
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003195 mutex_unlock(&card->dapm_mutex);
3196
Mark Brown48a8c392012-02-14 17:11:15 -08003197 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003198 return 0;
3199}
3200EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3201
Mark Brown5ba06fc2012-02-16 11:07:13 -08003202static struct snd_soc_dapm_widget *
3203snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3204 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003205{
3206 struct snd_soc_dapm_widget *w;
Mark Brown62ea8742012-01-21 21:14:48 +00003207 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003208
3209 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003210 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003211
Mark Brown62ea8742012-01-21 21:14:48 +00003212 switch (w->id) {
3213 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003214 w->regulator = devm_regulator_get(dapm->dev, w->name);
3215 if (IS_ERR(w->regulator)) {
3216 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003217 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003218 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003219 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003220 }
Mark Brown8784c772013-01-10 19:33:47 +00003221
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003222 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003223 ret = regulator_allow_bypass(w->regulator, true);
3224 if (ret != 0)
3225 dev_warn(w->dapm->dev,
3226 "ASoC: Failed to unbypass %s: %d\n",
3227 w->name, ret);
3228 }
Mark Brown62ea8742012-01-21 21:14:48 +00003229 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003230 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003231#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003232 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003233 if (IS_ERR(w->clk)) {
3234 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003235 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003236 w->name, ret);
3237 return NULL;
3238 }
Mark Brownec029952012-06-04 08:16:20 +01003239#else
3240 return NULL;
3241#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003242 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003243 default:
3244 break;
3245 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003246
Mark Brown88e8b9a2011-03-02 18:18:24 +00003247 if (dapm->codec && dapm->codec->name_prefix)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003248 w->name = kasprintf(GFP_KERNEL, "%s %s",
3249 dapm->codec->name_prefix, widget->name);
3250 else
3251 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3252
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003253 if (w->name == NULL) {
3254 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003255 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003256 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003257
Mark Brown7ca3a182011-10-08 14:04:50 +01003258 switch (w->id) {
3259 case snd_soc_dapm_switch:
3260 case snd_soc_dapm_mixer:
3261 case snd_soc_dapm_mixer_named_ctl:
3262 w->power_check = dapm_generic_check_power;
3263 break;
3264 case snd_soc_dapm_mux:
3265 case snd_soc_dapm_virt_mux:
3266 case snd_soc_dapm_value_mux:
3267 w->power_check = dapm_generic_check_power;
3268 break;
Mark Brown46162742013-06-05 19:36:11 +01003269 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003270 w->power_check = dapm_adc_check_power;
3271 break;
Mark Brown46162742013-06-05 19:36:11 +01003272 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003273 w->power_check = dapm_dac_check_power;
3274 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003275 case snd_soc_dapm_adc:
3276 case snd_soc_dapm_aif_out:
3277 case snd_soc_dapm_dac:
3278 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003279 case snd_soc_dapm_pga:
3280 case snd_soc_dapm_out_drv:
3281 case snd_soc_dapm_input:
3282 case snd_soc_dapm_output:
3283 case snd_soc_dapm_micbias:
3284 case snd_soc_dapm_spk:
3285 case snd_soc_dapm_hp:
3286 case snd_soc_dapm_mic:
3287 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003288 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003289 w->power_check = dapm_generic_check_power;
3290 break;
3291 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003292 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003293 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003294 case snd_soc_dapm_kcontrol:
Mark Brown7ca3a182011-10-08 14:04:50 +01003295 w->power_check = dapm_supply_check_power;
3296 break;
3297 default:
3298 w->power_check = dapm_always_on_check_power;
3299 break;
3300 }
3301
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003302 w->dapm = dapm;
3303 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003304 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003305 INIT_LIST_HEAD(&w->sources);
3306 INIT_LIST_HEAD(&w->sinks);
3307 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003308 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003309 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003310
3311 /* machine layer set ups unconnected pins and insertions */
3312 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003313 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003314}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003315
3316/**
Mark Brown4ba13272008-05-13 14:51:19 +02003317 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003318 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003319 * @widget: widget array
3320 * @num: number of widgets
3321 *
3322 * Creates new DAPM controls based upon the templates.
3323 *
3324 * Returns 0 for success else error.
3325 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003326int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003327 const struct snd_soc_dapm_widget *widget,
3328 int num)
3329{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003330 struct snd_soc_dapm_widget *w;
3331 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003332 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003333
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003334 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003335 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003336 w = snd_soc_dapm_new_control(dapm, widget);
3337 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003338 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003339 "ASoC: Failed to create DAPM control %s\n",
3340 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003341 ret = -ENOMEM;
3342 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003343 }
Mark Brown4ba13272008-05-13 14:51:19 +02003344 widget++;
3345 }
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003346 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003347 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003348}
3349EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3350
Mark Brownc74184e2012-04-04 22:12:09 +01003351static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3352 struct snd_kcontrol *kcontrol, int event)
3353{
3354 struct snd_soc_dapm_path *source_p, *sink_p;
3355 struct snd_soc_dai *source, *sink;
3356 const struct snd_soc_pcm_stream *config = w->params;
3357 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003358 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003359 u64 fmt;
3360 int ret;
3361
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003362 if (WARN_ON(!config) ||
3363 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3364 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003365
3366 /* We only support a single source and sink, pick the first */
3367 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3368 list_sink);
3369 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3370 list_source);
3371
Takashi Iwaibf4edea2013-11-07 18:38:47 +01003372 if (WARN_ON(!source_p || !sink_p) ||
3373 WARN_ON(!sink_p->source || !source_p->sink) ||
3374 WARN_ON(!source_p->source || !sink_p->sink))
3375 return -EINVAL;
Mark Brownc74184e2012-04-04 22:12:09 +01003376
3377 source = source_p->source->priv;
3378 sink = sink_p->sink->priv;
3379
3380 /* Be a little careful as we don't want to overflow the mask array */
3381 if (config->formats) {
3382 fmt = ffs(config->formats) - 1;
3383 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003384 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003385 config->formats);
3386 fmt = 0;
3387 }
3388
3389 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003390 params = kzalloc(sizeof(*params), GFP_KERNEL);
3391 if (!params) {
3392 ret = -ENOMEM;
3393 goto out;
3394 }
3395 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003396
Mark Brown9747cec2012-04-26 19:12:21 +01003397 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003398 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003399 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003400 config->rate_max;
3401
Mark Brown9747cec2012-04-26 19:12:21 +01003402 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003403 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003404 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003405 = config->channels_max;
3406
3407 memset(&substream, 0, sizeof(substream));
3408
3409 switch (event) {
3410 case SND_SOC_DAPM_PRE_PMU:
3411 if (source->driver->ops && source->driver->ops->hw_params) {
3412 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3413 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003414 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003415 if (ret != 0) {
3416 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003417 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003418 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003419 }
3420 }
3421
3422 if (sink->driver->ops && sink->driver->ops->hw_params) {
3423 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003424 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003425 sink);
3426 if (ret != 0) {
3427 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003428 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003429 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003430 }
3431 }
3432 break;
3433
3434 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003435 ret = snd_soc_dai_digital_mute(sink, 0,
3436 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003437 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003438 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003439 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003440 break;
3441
3442 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003443 ret = snd_soc_dai_digital_mute(sink, 1,
3444 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003445 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003446 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003447 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003448 break;
3449
3450 default:
Takashi Iwaia6ed0602013-11-06 11:07:19 +01003451 WARN(1, "Unknown event %d\n", event);
Mark Brownc74184e2012-04-04 22:12:09 +01003452 return -EINVAL;
3453 }
3454
Mark Brown9747cec2012-04-26 19:12:21 +01003455out:
3456 kfree(params);
3457 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003458}
3459
3460int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3461 const struct snd_soc_pcm_stream *params,
3462 struct snd_soc_dapm_widget *source,
3463 struct snd_soc_dapm_widget *sink)
3464{
3465 struct snd_soc_dapm_route routes[2];
3466 struct snd_soc_dapm_widget template;
3467 struct snd_soc_dapm_widget *w;
3468 size_t len;
3469 char *link_name;
3470
3471 len = strlen(source->name) + strlen(sink->name) + 2;
3472 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3473 if (!link_name)
3474 return -ENOMEM;
3475 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3476
3477 memset(&template, 0, sizeof(template));
3478 template.reg = SND_SOC_NOPM;
3479 template.id = snd_soc_dapm_dai_link;
3480 template.name = link_name;
3481 template.event = snd_soc_dai_link_event;
3482 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3483 SND_SOC_DAPM_PRE_PMD;
3484
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003485 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003486
3487 w = snd_soc_dapm_new_control(&card->dapm, &template);
3488 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003489 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003490 link_name);
3491 return -ENOMEM;
3492 }
3493
3494 w->params = params;
3495
3496 memset(&routes, 0, sizeof(routes));
3497
3498 routes[0].source = source->name;
3499 routes[0].sink = link_name;
3500 routes[1].source = link_name;
3501 routes[1].sink = sink->name;
3502
3503 return snd_soc_dapm_add_routes(&card->dapm, routes,
3504 ARRAY_SIZE(routes));
3505}
3506
Mark Brown888df392012-02-16 19:37:51 -08003507int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3508 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003509{
Mark Brown888df392012-02-16 19:37:51 -08003510 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003511 struct snd_soc_dapm_widget *w;
3512
Mark Brown888df392012-02-16 19:37:51 -08003513 WARN_ON(dapm->dev != dai->dev);
3514
3515 memset(&template, 0, sizeof(template));
3516 template.reg = SND_SOC_NOPM;
3517
3518 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003519 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003520 template.name = dai->driver->playback.stream_name;
3521 template.sname = dai->driver->playback.stream_name;
3522
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003523 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003524 template.name);
3525
3526 w = snd_soc_dapm_new_control(dapm, &template);
3527 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003528 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003529 dai->driver->playback.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003530 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003531 }
3532
3533 w->priv = dai;
3534 dai->playback_widget = w;
3535 }
3536
3537 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003538 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003539 template.name = dai->driver->capture.stream_name;
3540 template.sname = dai->driver->capture.stream_name;
3541
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003542 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003543 template.name);
3544
3545 w = snd_soc_dapm_new_control(dapm, &template);
3546 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003547 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003548 dai->driver->capture.stream_name);
Takashi Iwai298402a2013-10-28 14:21:50 +01003549 return -ENOMEM;
Mark Brown888df392012-02-16 19:37:51 -08003550 }
3551
3552 w->priv = dai;
3553 dai->capture_widget = w;
3554 }
3555
3556 return 0;
3557}
3558
3559int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3560{
3561 struct snd_soc_dapm_widget *dai_w, *w;
3562 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003563
3564 /* For each DAI widget... */
3565 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003566 switch (dai_w->id) {
3567 case snd_soc_dapm_dai_in:
3568 case snd_soc_dapm_dai_out:
3569 break;
3570 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003571 continue;
Mark Brown46162742013-06-05 19:36:11 +01003572 }
Mark Brown888df392012-02-16 19:37:51 -08003573
3574 dai = dai_w->priv;
3575
3576 /* ...find all widgets with the same stream and link them */
3577 list_for_each_entry(w, &card->widgets, list) {
3578 if (w->dapm != dai_w->dapm)
3579 continue;
3580
Mark Brown46162742013-06-05 19:36:11 +01003581 switch (w->id) {
3582 case snd_soc_dapm_dai_in:
3583 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003584 continue;
Mark Brown46162742013-06-05 19:36:11 +01003585 default:
3586 break;
3587 }
Mark Brown888df392012-02-16 19:37:51 -08003588
Russell King19c2c5f2013-08-04 20:24:03 +01003589 if (!w->sname || !strstr(w->sname, dai_w->name))
Mark Brown888df392012-02-16 19:37:51 -08003590 continue;
3591
3592 if (dai->driver->playback.stream_name &&
3593 strstr(w->sname,
3594 dai->driver->playback.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003595 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003596 dai->playback_widget->name, w->name);
Mark Brown888df392012-02-16 19:37:51 -08003597
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003598 snd_soc_dapm_add_path(w->dapm,
3599 dai->playback_widget, w, NULL, NULL);
Mark Brown888df392012-02-16 19:37:51 -08003600 }
3601
3602 if (dai->driver->capture.stream_name &&
3603 strstr(w->sname,
3604 dai->driver->capture.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003605 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003606 w->name, dai->capture_widget->name);
Mark Brown888df392012-02-16 19:37:51 -08003607
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003608 snd_soc_dapm_add_path(w->dapm, w,
3609 dai->capture_widget, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003610 }
3611 }
3612 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003613
Mark Brown888df392012-02-16 19:37:51 -08003614 return 0;
3615}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003616
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003617static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3618 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003619{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003620
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003621 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3622 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3623 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003624
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003625 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3626 w_cpu = cpu_dai->playback_widget;
3627 w_codec = codec_dai->playback_widget;
3628 } else {
3629 w_cpu = cpu_dai->capture_widget;
3630 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003631 }
3632
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003633 if (w_cpu) {
3634
3635 dapm_mark_dirty(w_cpu, "stream event");
3636
3637 switch (event) {
3638 case SND_SOC_DAPM_STREAM_START:
3639 w_cpu->active = 1;
3640 break;
3641 case SND_SOC_DAPM_STREAM_STOP:
3642 w_cpu->active = 0;
3643 break;
3644 case SND_SOC_DAPM_STREAM_SUSPEND:
3645 case SND_SOC_DAPM_STREAM_RESUME:
3646 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3647 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3648 break;
3649 }
3650 }
3651
3652 if (w_codec) {
3653
3654 dapm_mark_dirty(w_codec, "stream event");
3655
3656 switch (event) {
3657 case SND_SOC_DAPM_STREAM_START:
3658 w_codec->active = 1;
3659 break;
3660 case SND_SOC_DAPM_STREAM_STOP:
3661 w_codec->active = 0;
3662 break;
3663 case SND_SOC_DAPM_STREAM_SUSPEND:
3664 case SND_SOC_DAPM_STREAM_RESUME:
3665 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3666 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3667 break;
3668 }
3669 }
3670
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003671 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003672}
3673
3674/**
3675 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3676 * @rtd: PCM runtime data
3677 * @stream: stream name
3678 * @event: stream event
3679 *
3680 * Sends a stream event to the dapm core. The core then makes any
3681 * necessary widget power changes.
3682 *
3683 * Returns 0 for success else error.
3684 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003685void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3686 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003687{
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003688 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003689
Liam Girdwood3cd04342012-03-09 12:02:08 +00003690 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003691 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003692 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003693}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003694
3695/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003696 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003697 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003698 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003699 *
Mark Brown74b8f952009-06-06 11:26:15 +01003700 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003701 * a valid audio route and active audio stream.
3702 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3703 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003704 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003705int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003706{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003707 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003708}
Liam Girdwooda5302182008-07-07 13:35:17 +01003709EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003710
3711/**
Mark Brownda341832010-03-15 19:23:37 +00003712 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003713 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003714 * @pin: pin name
3715 *
3716 * Enables input/output pin regardless of any other state. This is
3717 * intended for use with microphone bias supplies used in microphone
3718 * jack detection.
3719 *
3720 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3721 * do any widget power switching.
3722 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003723int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3724 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003725{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003726 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003727
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003728 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003729 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003730 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003731 }
3732
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003733 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003734 w->connected = 1;
3735 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003736 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003737
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003738 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003739}
3740EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3741
3742/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003743 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003744 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003745 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003746 *
Mark Brown74b8f952009-06-06 11:26:15 +01003747 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003748 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3749 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003750 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003751int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3752 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003753{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003754 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003755}
3756EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3757
3758/**
Mark Brown5817b522008-09-24 11:23:11 +01003759 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003760 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003761 * @pin: pin name
3762 *
3763 * Marks the specified pin as being not connected, disabling it along
3764 * any parent or child widgets. At present this is identical to
3765 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3766 * additional things such as disabling controls which only affect
3767 * paths through the pin.
3768 *
3769 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3770 * do any widget power switching.
3771 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003772int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003773{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003774 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003775}
3776EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3777
3778/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003779 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003780 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003781 * @pin: audio signal pin endpoint (or start point)
3782 *
3783 * Get audio pin status - connected or disconnected.
3784 *
3785 * Returns 1 for connected otherwise 0.
3786 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003787int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3788 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003789{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003790 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003791
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003792 if (w)
3793 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003794
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003795 return 0;
3796}
Liam Girdwooda5302182008-07-07 13:35:17 +01003797EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003798
3799/**
Mark Brown1547aba2010-05-07 21:11:40 +01003800 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003801 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003802 * @pin: audio signal pin endpoint (or start point)
3803 *
3804 * Mark the given endpoint or pin as ignoring suspend. When the
3805 * system is disabled a path between two endpoints flagged as ignoring
3806 * suspend will not be disabled. The path must already be enabled via
3807 * normal means at suspend time, it will not be turned on if it was not
3808 * already enabled.
3809 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003810int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3811 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003812{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003813 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003814
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003815 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003816 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003817 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003818 }
3819
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003820 w->ignore_suspend = 1;
3821
3822 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003823}
3824EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3825
Stephen Warren16332812011-11-23 12:42:04 -07003826static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3827 struct snd_soc_dapm_widget *w)
3828{
3829 struct snd_soc_dapm_path *p;
3830
3831 list_for_each_entry(p, &card->paths, list) {
3832 if ((p->source == w) || (p->sink == w)) {
3833 dev_dbg(card->dev,
3834 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3835 p->source->name, p->source->id, p->source->dapm,
3836 p->sink->name, p->sink->id, p->sink->dapm);
3837
3838 /* Connected to something other than the codec */
3839 if (p->source->dapm != p->sink->dapm)
3840 return true;
3841 /*
3842 * Loopback connection from codec external pin to
3843 * codec external pin
3844 */
3845 if (p->sink->id == snd_soc_dapm_input) {
3846 switch (p->source->id) {
3847 case snd_soc_dapm_output:
3848 case snd_soc_dapm_micbias:
3849 return true;
3850 default:
3851 break;
3852 }
3853 }
3854 }
3855 }
3856
3857 return false;
3858}
3859
3860/**
3861 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3862 * @codec: The codec whose pins should be processed
3863 *
3864 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3865 * which are unused. Pins are used if they are connected externally to the
3866 * codec, whether that be to some other device, or a loop-back connection to
3867 * the codec itself.
3868 */
3869void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3870{
3871 struct snd_soc_card *card = codec->card;
3872 struct snd_soc_dapm_context *dapm = &codec->dapm;
3873 struct snd_soc_dapm_widget *w;
3874
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003875 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003876 &card->dapm, &codec->dapm);
3877
3878 list_for_each_entry(w, &card->widgets, list) {
3879 if (w->dapm != dapm)
3880 continue;
3881 switch (w->id) {
3882 case snd_soc_dapm_input:
3883 case snd_soc_dapm_output:
3884 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003885 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003886 w->name);
3887 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003888 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003889 "... Not in map; disabling\n");
3890 snd_soc_dapm_nc_pin(dapm, w->name);
3891 }
3892 break;
3893 default:
3894 break;
3895 }
3896 }
3897}
3898
Mark Brown1547aba2010-05-07 21:11:40 +01003899/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003900 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003901 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003902 *
3903 * Free all dapm widgets and resources.
3904 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003905void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003906{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003907 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003908 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003909 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003910 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003911}
3912EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3913
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003914static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003915{
Liam Girdwood01005a72012-07-06 16:57:05 +01003916 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003917 struct snd_soc_dapm_widget *w;
3918 LIST_HEAD(down_list);
3919 int powerdown = 0;
3920
Liam Girdwood01005a72012-07-06 16:57:05 +01003921 mutex_lock(&card->dapm_mutex);
3922
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003923 list_for_each_entry(w, &dapm->card->widgets, list) {
3924 if (w->dapm != dapm)
3925 continue;
Mark Brown51737472009-06-22 13:16:51 +01003926 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003927 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003928 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003929 powerdown = 1;
3930 }
3931 }
3932
3933 /* If there were no widgets to power down we're already in
3934 * standby.
3935 */
3936 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003937 if (dapm->bias_level == SND_SOC_BIAS_ON)
3938 snd_soc_dapm_set_bias_level(dapm,
3939 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003940 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003941 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3942 snd_soc_dapm_set_bias_level(dapm,
3943 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003944 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003945
3946 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003947}
Mark Brown51737472009-06-22 13:16:51 +01003948
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003949/*
3950 * snd_soc_dapm_shutdown - callback for system shutdown
3951 */
3952void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3953{
3954 struct snd_soc_codec *codec;
3955
Misael Lopez Cruz445632a2012-11-08 12:03:12 -06003956 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003957 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003958 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3959 snd_soc_dapm_set_bias_level(&codec->dapm,
3960 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003961 }
Mark Brown51737472009-06-22 13:16:51 +01003962}
3963
Richard Purdie2b97eab2006-10-06 18:32:18 +02003964/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003965MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003966MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3967MODULE_LICENSE("GPL");