blob: 13fcb61a922f5580d9eeb1d57fb0e16d0252ee79 [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,
62 [snd_soc_dapm_supply] = 1,
Mark Brown62ea8742012-01-21 21:14:48 +000063 [snd_soc_dapm_regulator_supply] = 1,
Ola Liljad7e7eb92012-05-24 15:26:25 +020064 [snd_soc_dapm_clock_supply] = 1,
Mark Brown38357ab2009-06-06 19:03:23 +010065 [snd_soc_dapm_micbias] = 2,
Mark Brownc74184e2012-04-04 22:12:09 +010066 [snd_soc_dapm_dai_link] = 2,
Mark Brown46162742013-06-05 19:36:11 +010067 [snd_soc_dapm_dai_in] = 3,
68 [snd_soc_dapm_dai_out] = 3,
Mark Brown010ff262009-08-17 17:39:22 +010069 [snd_soc_dapm_aif_in] = 3,
70 [snd_soc_dapm_aif_out] = 3,
71 [snd_soc_dapm_mic] = 4,
72 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000073 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010074 [snd_soc_dapm_value_mux] = 5,
75 [snd_soc_dapm_dac] = 6,
Lars-Peter Clausenefc77e32013-06-14 13:16:50 +020076 [snd_soc_dapm_switch] = 7,
Mark Brown010ff262009-08-17 17:39:22 +010077 [snd_soc_dapm_mixer] = 7,
78 [snd_soc_dapm_mixer_named_ctl] = 7,
79 [snd_soc_dapm_pga] = 8,
80 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060081 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010082 [snd_soc_dapm_hp] = 10,
83 [snd_soc_dapm_spk] = 10,
Mark Brown7e1f7c82012-04-12 17:29:36 +010084 [snd_soc_dapm_line] = 10,
Lars-Peter Clausen57295072013-08-05 11:27:31 +020085 [snd_soc_dapm_kcontrol] = 11,
86 [snd_soc_dapm_post] = 12,
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,
Ola Liljad7e7eb92012-05-24 15:26:25 +0200112 [snd_soc_dapm_clock_supply] = 12,
Mark Brownc74184e2012-04-04 22:12:09 +0100113 [snd_soc_dapm_regulator_supply] = 12,
114 [snd_soc_dapm_supply] = 12,
115 [snd_soc_dapm_post] = 13,
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
412static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100413 unsigned short reg, unsigned int mask, unsigned int value)
414{
Mark Brown8a713da2011-12-03 12:33:55 +0000415 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100416 unsigned int old, new;
417 int ret;
418
Mark Brown8a713da2011-12-03 12:33:55 +0000419 if (w->codec && w->codec->using_regmap) {
420 ret = regmap_update_bits_check(w->codec->control_data,
421 reg, mask, value, &change);
422 if (ret != 0)
423 return ret;
424 } else {
Liam Girdwood49575fb52012-03-06 18:16:19 +0000425 soc_widget_lock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000426 ret = soc_widget_read(w, reg);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000427 if (ret < 0) {
428 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100429 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000430 }
Mark Brown8a713da2011-12-03 12:33:55 +0000431
432 old = ret;
433 new = (old & ~mask) | (value & mask);
434 change = old != new;
435 if (change) {
436 ret = soc_widget_write(w, reg, new);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000437 if (ret < 0) {
438 soc_widget_unlock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000439 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000440 }
Mark Brown8a713da2011-12-03 12:33:55 +0000441 }
Liam Girdwood49575fb52012-03-06 18:16:19 +0000442 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100443 }
444
445 return change;
446}
447
Mark Brown452c5ea2009-05-17 21:41:23 +0100448/**
449 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800450 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100451 * @level: level to configure
452 *
453 * Configure the bias (power) levels for the SoC audio device.
454 *
455 * Returns 0 for success else error.
456 */
Mark Browned5a4c42011-02-18 11:12:42 -0800457static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200458 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100459{
Mark Browned5a4c42011-02-18 11:12:42 -0800460 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100461 int ret = 0;
462
Mark Brown84e90932010-11-04 00:07:02 -0400463 trace_snd_soc_bias_level_start(card, level);
464
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000465 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100466 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100467 if (ret != 0)
468 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100469
Mark Browncc4c6702011-06-06 19:03:34 +0100470 if (dapm->codec) {
471 if (dapm->codec->driver->set_bias_level)
472 ret = dapm->codec->driver->set_bias_level(dapm->codec,
473 level);
Mark Brownd8c3bb92012-08-23 18:10:42 +0100474 else
475 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100476 } else if (!card || dapm != &card->dapm) {
Liam Girdwood41231282012-07-06 16:56:16 +0100477 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100478 }
Liam Girdwood41231282012-07-06 16:56:16 +0100479
Mark Brown171ec6b2011-06-06 18:15:19 +0100480 if (ret != 0)
481 goto out;
482
483 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100484 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100485out:
Mark Brown84e90932010-11-04 00:07:02 -0400486 trace_snd_soc_bias_level_done(card, level);
487
Mark Brown452c5ea2009-05-17 21:41:23 +0100488 return ret;
489}
490
Richard Purdie2b97eab2006-10-06 18:32:18 +0200491/* set up initial codec paths */
492static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
493 struct snd_soc_dapm_path *p, int i)
494{
495 switch (w->id) {
496 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000497 case snd_soc_dapm_mixer:
498 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200499 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100500 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600501 w->kcontrol_news[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400502 unsigned int reg = mc->reg;
503 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100504 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400505 unsigned int mask = (1 << fls(max)) - 1;
506 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200507
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100508 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200509 val = (val >> shift) & mask;
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200510 if (invert)
511 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200512
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200513 p->connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200514 }
515 break;
516 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600517 struct soc_enum *e = (struct soc_enum *)
518 w->kcontrol_news[i].private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200519 int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200520
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100521 val = soc_widget_read(w, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200522 item = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200523
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200524 if (item < e->max && !strcmp(p->name, e->texts[item]))
525 p->connect = 1;
526 else
527 p->connect = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200528 }
529 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000530 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600531 struct soc_enum *e = (struct soc_enum *)
532 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000533
534 p->connect = 0;
535 /* since a virtual mux has no backing registers to
536 * decide which path to connect, it will try to match
537 * with the first enumeration. This is to ensure
538 * that the default mux choice (the first) will be
539 * correctly powered up during initialization.
540 */
541 if (!strcmp(p->name, e->texts[0]))
542 p->connect = 1;
543 }
544 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200545 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200546 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600547 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200548 int val, item;
549
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100550 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200551 val = (val >> e->shift_l) & e->mask;
552 for (item = 0; item < e->max; item++) {
553 if (val == e->values[item])
554 break;
555 }
556
Lars-Peter Clausen58fee772013-06-14 13:16:52 +0200557 if (item < e->max && !strcmp(p->name, e->texts[item]))
558 p->connect = 1;
559 else
560 p->connect = 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200561 }
562 break;
Mark Brown56563102011-10-03 22:41:09 +0100563 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200564 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600565 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200566 case snd_soc_dapm_output:
567 case snd_soc_dapm_adc:
568 case snd_soc_dapm_input:
Mark Brown1ab97c82011-11-27 16:21:51 +0000569 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200570 case snd_soc_dapm_dac:
571 case snd_soc_dapm_micbias:
572 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100573 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +0000574 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200575 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100576 case snd_soc_dapm_aif_in:
577 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100578 case snd_soc_dapm_dai_in:
579 case snd_soc_dapm_dai_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200580 case snd_soc_dapm_hp:
581 case snd_soc_dapm_mic:
582 case snd_soc_dapm_spk:
583 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +0100584 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200585 case snd_soc_dapm_kcontrol:
Mark Brown56563102011-10-03 22:41:09 +0100586 p->connect = 1;
587 break;
588 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200589 case snd_soc_dapm_pre:
590 case snd_soc_dapm_post:
591 p->connect = 0;
592 break;
593 }
594}
595
Mark Brown74b8f952009-06-06 11:26:15 +0100596/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200597static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200598 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
599 struct snd_soc_dapm_path *path, const char *control_name,
600 const struct snd_kcontrol_new *kcontrol)
601{
602 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
603 int i;
604
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100605 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200606 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200607 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200608 list_add(&path->list_sink, &dest->sources);
609 list_add(&path->list_source, &src->sinks);
610 path->name = (char*)e->texts[i];
611 dapm_set_path_status(dest, path, 0);
612 return 0;
613 }
614 }
615
616 return -ENODEV;
617}
618
Mark Brown74b8f952009-06-06 11:26:15 +0100619/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200620static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200621 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
622 struct snd_soc_dapm_path *path, const char *control_name)
623{
624 int i;
625
626 /* search for mixer kcontrol */
627 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600628 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200629 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200630 list_add(&path->list_sink, &dest->sources);
631 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600632 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200633 dapm_set_path_status(dest, path, i);
634 return 0;
635 }
636 }
637 return -ENODEV;
638}
639
Stephen Warrenaf468002011-04-28 17:38:01 -0600640static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600641 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600642 const struct snd_kcontrol_new *kcontrol_new,
643 struct snd_kcontrol **kcontrol)
644{
645 struct snd_soc_dapm_widget *w;
646 int i;
647
648 *kcontrol = NULL;
649
650 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600651 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
652 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600653 for (i = 0; i < w->num_kcontrols; i++) {
654 if (&w->kcontrol_news[i] == kcontrol_new) {
655 if (w->kcontrols)
656 *kcontrol = w->kcontrols[i];
657 return 1;
658 }
659 }
660 }
661
662 return 0;
663}
664
Stephen Warren85762e72013-03-29 15:40:10 -0600665/*
666 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
667 * create it. Either way, add the widget into the control's widget list
668 */
669static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
Mark Brown946d92a2013-08-12 23:28:42 +0100670 int kci)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200671{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200672 struct snd_soc_dapm_context *dapm = w->dapm;
Mark Brown12ea2c72011-03-02 18:17:32 +0000673 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000674 const char *prefix;
Stephen Warren85762e72013-03-29 15:40:10 -0600675 size_t prefix_len;
676 int shared;
677 struct snd_kcontrol *kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600678 bool wname_in_long_name, kcname_in_long_name;
Stephen Warren85762e72013-03-29 15:40:10 -0600679 char *long_name;
680 const char *name;
681 int ret;
Mark Brownefb7ac32011-03-08 17:23:24 +0000682
683 if (dapm->codec)
684 prefix = dapm->codec->name_prefix;
685 else
686 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200687
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000688 if (prefix)
689 prefix_len = strlen(prefix) + 1;
690 else
691 prefix_len = 0;
692
Stephen Warren85762e72013-03-29 15:40:10 -0600693 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
694 &kcontrol);
695
Stephen Warren85762e72013-03-29 15:40:10 -0600696 if (!kcontrol) {
697 if (shared) {
698 wname_in_long_name = false;
699 kcname_in_long_name = true;
700 } else {
701 switch (w->id) {
702 case snd_soc_dapm_switch:
703 case snd_soc_dapm_mixer:
704 wname_in_long_name = true;
705 kcname_in_long_name = true;
706 break;
707 case snd_soc_dapm_mixer_named_ctl:
708 wname_in_long_name = false;
709 kcname_in_long_name = true;
710 break;
711 case snd_soc_dapm_mux:
712 case snd_soc_dapm_virt_mux:
713 case snd_soc_dapm_value_mux:
714 wname_in_long_name = true;
715 kcname_in_long_name = false;
716 break;
717 default:
Stephen Warren85762e72013-03-29 15:40:10 -0600718 return -EINVAL;
719 }
720 }
721
722 if (wname_in_long_name && kcname_in_long_name) {
Stephen Warren85762e72013-03-29 15:40:10 -0600723 /*
724 * The control will get a prefix from the control
725 * creation process but we're also using the same
726 * prefix for widgets so cut the prefix off the
727 * front of the widget name.
728 */
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200729 long_name = kasprintf(GFP_KERNEL, "%s %s",
Stephen Warren85762e72013-03-29 15:40:10 -0600730 w->name + prefix_len,
731 w->kcontrol_news[kci].name);
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200732 if (long_name == NULL)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +0200733 return -ENOMEM;
Stephen Warren85762e72013-03-29 15:40:10 -0600734
735 name = long_name;
736 } else if (wname_in_long_name) {
737 long_name = NULL;
738 name = w->name + prefix_len;
739 } else {
740 long_name = NULL;
741 name = w->kcontrol_news[kci].name;
742 }
743
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200744 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
Stephen Warren85762e72013-03-29 15:40:10 -0600745 prefix);
Lars-Peter Clausen656ca9d2013-06-14 13:16:54 +0200746 kfree(long_name);
Lars-Peter Clausen9356e9d2013-08-01 14:08:06 +0200747 if (!kcontrol)
748 return -ENOMEM;
749 kcontrol->private_free = dapm_kcontrol_free;
Lars-Peter Clausene84357f2013-07-29 17:13:58 +0200750
751 ret = dapm_kcontrol_data_alloc(w, kcontrol);
752 if (ret) {
753 snd_ctl_free_one(kcontrol);
754 return ret;
755 }
756
Stephen Warren85762e72013-03-29 15:40:10 -0600757 ret = snd_ctl_add(card, kcontrol);
758 if (ret < 0) {
759 dev_err(dapm->dev,
760 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
761 w->name, name, ret);
Stephen Warren85762e72013-03-29 15:40:10 -0600762 return ret;
763 }
Stephen Warren85762e72013-03-29 15:40:10 -0600764 }
765
Lars-Peter Clausen2c75bdf2013-08-01 14:08:07 +0200766 ret = dapm_kcontrol_add_widget(kcontrol, w);
767 if (ret)
768 return ret;
769
Stephen Warren85762e72013-03-29 15:40:10 -0600770 w->kcontrols[kci] = kcontrol;
Stephen Warren85762e72013-03-29 15:40:10 -0600771
772 return 0;
773}
774
775/* create new dapm mixer control */
776static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
777{
778 int i, ret;
779 struct snd_soc_dapm_path *path;
780
Richard Purdie2b97eab2006-10-06 18:32:18 +0200781 /* add kcontrol */
782 for (i = 0; i < w->num_kcontrols; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200783 /* match name */
784 list_for_each_entry(path, &w->sources, list_sink) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200785 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600786 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200787 continue;
788
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200789 if (w->kcontrols[i]) {
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200790 dapm_kcontrol_add_path(w->kcontrols[i], path);
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200791 continue;
792 }
793
Mark Brown946d92a2013-08-12 23:28:42 +0100794 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
Stephen Warren85762e72013-03-29 15:40:10 -0600795 if (ret < 0)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200796 return ret;
Mark Brown946d92a2013-08-12 23:28:42 +0100797
798 dapm_kcontrol_add_path(w->kcontrols[i], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200799 }
800 }
Stephen Warren85762e72013-03-29 15:40:10 -0600801
802 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200803}
804
805/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200806static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200807{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200808 struct snd_soc_dapm_context *dapm = w->dapm;
Stephen Warren85762e72013-03-29 15:40:10 -0600809 struct snd_soc_dapm_path *path;
Stephen Warrenaf468002011-04-28 17:38:01 -0600810 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200811
Stephen Warrenaf468002011-04-28 17:38:01 -0600812 if (w->num_kcontrols != 1) {
813 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000814 "ASoC: mux %s has incorrect number of controls\n",
Stephen Warrenaf468002011-04-28 17:38:01 -0600815 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200816 return -EINVAL;
817 }
818
Lars-Peter Clausenfe581392013-08-01 18:30:38 +0200819 if (list_empty(&w->sources)) {
Stephen Warren85762e72013-03-29 15:40:10 -0600820 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
821 return -EINVAL;
Stephen Warrenaf468002011-04-28 17:38:01 -0600822 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200823
Mark Brown946d92a2013-08-12 23:28:42 +0100824 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
Stephen Warren85762e72013-03-29 15:40:10 -0600825 if (ret < 0)
826 return ret;
Stephen Warrenfad59882011-04-28 17:37:59 -0600827
Richard Purdie2b97eab2006-10-06 18:32:18 +0200828 list_for_each_entry(path, &w->sources, list_sink)
Lars-Peter Clausen5106b922013-07-29 17:14:00 +0200829 dapm_kcontrol_add_path(w->kcontrols[0], path);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200830
Stephen Warrenaf468002011-04-28 17:38:01 -0600831 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200832}
833
834/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200835static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200836{
Mark Browna6c65732010-03-03 17:45:21 +0000837 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200838 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000839 "ASoC: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200840
Mark Browna6c65732010-03-03 17:45:21 +0000841 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200842}
843
844/* reset 'walked' bit for each dapm path */
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100845static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
846 struct list_head *sink)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200847{
848 struct snd_soc_dapm_path *p;
849
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100850 list_for_each_entry(p, sink, list_source) {
851 if (p->walked) {
852 p->walked = 0;
853 dapm_clear_walk_output(dapm, &p->sink->sinks);
854 }
855 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200856}
857
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +0100858static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
859 struct list_head *source)
860{
861 struct snd_soc_dapm_path *p;
862
863 list_for_each_entry(p, source, list_sink) {
864 if (p->walked) {
865 p->walked = 0;
866 dapm_clear_walk_input(dapm, &p->source->sources);
867 }
868 }
869}
870
871
Mark Brown9949788b2010-05-07 20:24:05 +0100872/* We implement power down on suspend by checking the power state of
873 * the ALSA card - when we are suspending the ALSA state for the card
874 * is set to D3.
875 */
876static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
877{
Mark Brown12ea2c72011-03-02 18:17:32 +0000878 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown9949788b2010-05-07 20:24:05 +0100879
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000880 switch (level) {
Mark Brown9949788b2010-05-07 20:24:05 +0100881 case SNDRV_CTL_POWER_D3hot:
882 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100883 if (widget->ignore_suspend)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000884 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200885 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100886 return widget->ignore_suspend;
Mark Brown9949788b2010-05-07 20:24:05 +0100887 default:
888 return 1;
889 }
890}
891
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100892/* add widget to list if it's not already in the list */
893static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
894 struct snd_soc_dapm_widget *w)
895{
896 struct snd_soc_dapm_widget_list *wlist;
897 int wlistsize, wlistentries, i;
898
899 if (*list == NULL)
900 return -EINVAL;
901
902 wlist = *list;
903
904 /* is this widget already in the list */
905 for (i = 0; i < wlist->num_widgets; i++) {
906 if (wlist->widgets[i] == w)
907 return 0;
908 }
909
910 /* allocate some new space */
911 wlistentries = wlist->num_widgets + 1;
912 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
913 wlistentries * sizeof(struct snd_soc_dapm_widget *);
914 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
915 if (*list == NULL) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000916 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100917 w->name);
918 return -ENOMEM;
919 }
920 wlist = *list;
921
922 /* insert the widget */
Liam Girdwood30a6a1a2012-11-19 14:39:12 +0000923 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100924 w->name, wlist->num_widgets);
925
926 wlist->widgets[wlist->num_widgets] = w;
927 wlist->num_widgets++;
928 return 1;
929}
930
Richard Purdie2b97eab2006-10-06 18:32:18 +0200931/*
932 * Recursively check for a completed path to an active or physically connected
933 * output widget. Returns number of complete paths.
934 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100935static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
936 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200937{
938 struct snd_soc_dapm_path *path;
939 int con = 0;
940
Mark Brown024dc072011-10-09 11:52:05 +0100941 if (widget->outputs >= 0)
942 return widget->outputs;
943
Mark Brownde02d072011-09-20 21:43:24 +0100944 DAPM_UPDATE_STAT(widget, path_checks);
945
Mark Brown62ea8742012-01-21 21:14:48 +0000946 switch (widget->id) {
947 case snd_soc_dapm_supply:
948 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200949 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +0200950 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +0100951 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000952 default:
953 break;
954 }
Mark Brown246d0a12009-04-22 18:24:55 +0100955
Mark Brown010ff262009-08-17 17:39:22 +0100956 switch (widget->id) {
957 case snd_soc_dapm_adc:
958 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +0100959 case snd_soc_dapm_dai_out:
Mark Brown024dc072011-10-09 11:52:05 +0100960 if (widget->active) {
961 widget->outputs = snd_soc_dapm_suspend_check(widget);
962 return widget->outputs;
963 }
Mark Brown010ff262009-08-17 17:39:22 +0100964 default:
965 break;
966 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200967
968 if (widget->connected) {
969 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100970 if (widget->id == snd_soc_dapm_output && !widget->ext) {
971 widget->outputs = snd_soc_dapm_suspend_check(widget);
972 return widget->outputs;
973 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200974
975 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100976 if (widget->id == snd_soc_dapm_hp ||
977 widget->id == snd_soc_dapm_spk ||
978 (widget->id == snd_soc_dapm_line &&
979 !list_empty(&widget->sources))) {
980 widget->outputs = snd_soc_dapm_suspend_check(widget);
981 return widget->outputs;
982 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200983 }
984
985 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e02011-09-21 18:19:14 +0100986 DAPM_UPDATE_STAT(widget, neighbour_checks);
987
Mark Brownbf3a9e12011-06-13 16:42:29 +0100988 if (path->weak)
989 continue;
990
Mark Brown8af294b2013-02-22 17:48:15 +0000991 if (path->walking)
992 return 1;
993
Richard Purdie2b97eab2006-10-06 18:32:18 +0200994 if (path->walked)
995 continue;
996
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100997 trace_snd_soc_dapm_output_path(widget, path);
998
Richard Purdie2b97eab2006-10-06 18:32:18 +0200999 if (path->sink && path->connect) {
1000 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +00001001 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001002
1003 /* do we need to add this widget to the list ? */
1004 if (list) {
1005 int err;
1006 err = dapm_list_add_widget(list, path->sink);
1007 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001008 dev_err(widget->dapm->dev,
1009 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001010 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001011 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001012 return con;
1013 }
1014 }
1015
1016 con += is_connected_output_ep(path->sink, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001017
1018 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001019 }
1020 }
1021
Mark Brown024dc072011-10-09 11:52:05 +01001022 widget->outputs = con;
1023
Richard Purdie2b97eab2006-10-06 18:32:18 +02001024 return con;
1025}
1026
1027/*
1028 * Recursively check for a completed path to an active or physically connected
1029 * input widget. Returns number of complete paths.
1030 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001031static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1032 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001033{
1034 struct snd_soc_dapm_path *path;
1035 int con = 0;
1036
Mark Brown024dc072011-10-09 11:52:05 +01001037 if (widget->inputs >= 0)
1038 return widget->inputs;
1039
Mark Brownde02d072011-09-20 21:43:24 +01001040 DAPM_UPDATE_STAT(widget, path_checks);
1041
Mark Brown62ea8742012-01-21 21:14:48 +00001042 switch (widget->id) {
1043 case snd_soc_dapm_supply:
1044 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001045 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001046 case snd_soc_dapm_kcontrol:
Mark Brown246d0a12009-04-22 18:24:55 +01001047 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +00001048 default:
1049 break;
1050 }
Mark Brown246d0a12009-04-22 18:24:55 +01001051
Richard Purdie2b97eab2006-10-06 18:32:18 +02001052 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +01001053 switch (widget->id) {
1054 case snd_soc_dapm_dac:
1055 case snd_soc_dapm_aif_in:
Mark Brown46162742013-06-05 19:36:11 +01001056 case snd_soc_dapm_dai_in:
Mark Brown024dc072011-10-09 11:52:05 +01001057 if (widget->active) {
1058 widget->inputs = snd_soc_dapm_suspend_check(widget);
1059 return widget->inputs;
1060 }
Mark Brown010ff262009-08-17 17:39:22 +01001061 default:
1062 break;
1063 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001064
1065 if (widget->connected) {
1066 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +01001067 if (widget->id == snd_soc_dapm_input && !widget->ext) {
1068 widget->inputs = snd_soc_dapm_suspend_check(widget);
1069 return widget->inputs;
1070 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001071
1072 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +01001073 if (widget->id == snd_soc_dapm_vmid) {
1074 widget->inputs = snd_soc_dapm_suspend_check(widget);
1075 return widget->inputs;
1076 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001077
1078 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +03001079 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +01001080 (widget->id == snd_soc_dapm_line &&
1081 !list_empty(&widget->sinks))) {
1082 widget->inputs = snd_soc_dapm_suspend_check(widget);
1083 return widget->inputs;
1084 }
1085
Mark Brown1ab97c82011-11-27 16:21:51 +00001086 /* signal generator */
1087 if (widget->id == snd_soc_dapm_siggen) {
1088 widget->inputs = snd_soc_dapm_suspend_check(widget);
1089 return widget->inputs;
1090 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001091 }
1092
1093 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e02011-09-21 18:19:14 +01001094 DAPM_UPDATE_STAT(widget, neighbour_checks);
1095
Mark Brownbf3a9e12011-06-13 16:42:29 +01001096 if (path->weak)
1097 continue;
1098
Mark Brown8af294b2013-02-22 17:48:15 +00001099 if (path->walking)
1100 return 1;
1101
Richard Purdie2b97eab2006-10-06 18:32:18 +02001102 if (path->walked)
1103 continue;
1104
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001105 trace_snd_soc_dapm_input_path(widget, path);
1106
Richard Purdie2b97eab2006-10-06 18:32:18 +02001107 if (path->source && path->connect) {
1108 path->walked = 1;
Mark Brown8af294b2013-02-22 17:48:15 +00001109 path->walking = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001110
1111 /* do we need to add this widget to the list ? */
1112 if (list) {
1113 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +01001114 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001115 if (err < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001116 dev_err(widget->dapm->dev,
1117 "ASoC: could not add widget %s\n",
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001118 widget->name);
Mark Brown8af294b2013-02-22 17:48:15 +00001119 path->walking = 0;
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001120 return con;
1121 }
1122 }
1123
1124 con += is_connected_input_ep(path->source, list);
Mark Brown8af294b2013-02-22 17:48:15 +00001125
1126 path->walking = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001127 }
1128 }
1129
Mark Brown024dc072011-10-09 11:52:05 +01001130 widget->inputs = con;
1131
Richard Purdie2b97eab2006-10-06 18:32:18 +02001132 return con;
1133}
1134
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001135/**
1136 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1137 * @dai: the soc DAI.
1138 * @stream: stream direction.
1139 * @list: list of active widgets for this stream.
1140 *
1141 * Queries DAPM graph as to whether an valid audio stream path exists for
1142 * the initial stream specified by name. This takes into account
1143 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1144 *
1145 * Returns the number of valid paths or negative error.
1146 */
1147int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1148 struct snd_soc_dapm_widget_list **list)
1149{
1150 struct snd_soc_card *card = dai->card;
1151 int paths;
1152
1153 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1154 dapm_reset(card);
1155
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001156 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001157 paths = is_connected_output_ep(dai->playback_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001158 dapm_clear_walk_output(&card->dapm,
1159 &dai->playback_widget->sinks);
1160 } else {
Liam Girdwoodd298caa2012-06-01 18:03:00 +01001161 paths = is_connected_input_ep(dai->capture_widget, list);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001162 dapm_clear_walk_input(&card->dapm,
1163 &dai->capture_widget->sources);
1164 }
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001165
1166 trace_snd_soc_dapm_connected(paths, stream);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001167 mutex_unlock(&card->dapm_mutex);
1168
1169 return paths;
1170}
1171
Richard Purdie2b97eab2006-10-06 18:32:18 +02001172/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001173 * Handler for generic register modifier widget.
1174 */
1175int dapm_reg_event(struct snd_soc_dapm_widget *w,
1176 struct snd_kcontrol *kcontrol, int event)
1177{
1178 unsigned int val;
1179
1180 if (SND_SOC_DAPM_EVENT_ON(event))
1181 val = w->on_val;
1182 else
1183 val = w->off_val;
1184
Liam Girdwood49575fb52012-03-06 18:16:19 +00001185 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001186 w->mask << w->shift, val << w->shift);
1187
1188 return 0;
1189}
Mark Brown11589412008-07-29 11:42:23 +01001190EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001191
Mark Brown62ea8742012-01-21 21:14:48 +00001192/*
1193 * Handler for regulator supply widget.
1194 */
1195int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1196 struct snd_kcontrol *kcontrol, int event)
1197{
Mark Brownc05b84d2012-09-07 12:57:11 +08001198 int ret;
1199
1200 if (SND_SOC_DAPM_EVENT_ON(event)) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001201 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001202 ret = regulator_allow_bypass(w->regulator, false);
Mark Brownc05b84d2012-09-07 12:57:11 +08001203 if (ret != 0)
1204 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001205 "ASoC: Failed to bypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001206 w->name, ret);
1207 }
1208
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001209 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001210 } else {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001211 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00001212 ret = regulator_allow_bypass(w->regulator, true);
Mark Brownc05b84d2012-09-07 12:57:11 +08001213 if (ret != 0)
1214 dev_warn(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001215 "ASoC: Failed to unbypass %s: %d\n",
Mark Brownc05b84d2012-09-07 12:57:11 +08001216 w->name, ret);
1217 }
1218
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001219 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001220 }
Mark Brown62ea8742012-01-21 21:14:48 +00001221}
1222EXPORT_SYMBOL_GPL(dapm_regulator_event);
1223
Ola Liljad7e7eb92012-05-24 15:26:25 +02001224/*
1225 * Handler for clock supply widget.
1226 */
1227int dapm_clock_event(struct snd_soc_dapm_widget *w,
1228 struct snd_kcontrol *kcontrol, int event)
1229{
1230 if (!w->clk)
1231 return -EIO;
1232
Mark Brownec029952012-06-04 08:16:20 +01001233#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001234 if (SND_SOC_DAPM_EVENT_ON(event)) {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001235 return clk_prepare_enable(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001236 } else {
Fabio Baltieri37c1b922013-04-30 16:09:52 +02001237 clk_disable_unprepare(w->clk);
Ola Liljad7e7eb92012-05-24 15:26:25 +02001238 return 0;
1239 }
Mark Brownec029952012-06-04 08:16:20 +01001240#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001241 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001242}
1243EXPORT_SYMBOL_GPL(dapm_clock_event);
1244
Mark Brownd8050022011-09-28 18:28:23 +01001245static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1246{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001247 if (w->power_checked)
1248 return w->new_power;
1249
Mark Brownd8050022011-09-28 18:28:23 +01001250 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001251 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001252 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001253 w->new_power = w->power_check(w);
1254
1255 w->power_checked = true;
1256
1257 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001258}
1259
Mark Browncd0f2d42009-04-20 16:56:59 +01001260/* Generic check to see if a widget should be powered.
1261 */
1262static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1263{
1264 int in, out;
1265
Mark Brownde02d072011-09-20 21:43:24 +01001266 DAPM_UPDATE_STAT(w, power_checks);
1267
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001268 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001269 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001270 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001271 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Browncd0f2d42009-04-20 16:56:59 +01001272 return out != 0 && in != 0;
1273}
1274
Mark Brown6ea31b92009-04-20 17:15:41 +01001275/* Check to see if an ADC has power */
1276static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1277{
1278 int in;
1279
Mark Brownde02d072011-09-20 21:43:24 +01001280 DAPM_UPDATE_STAT(w, power_checks);
1281
Mark Brown6ea31b92009-04-20 17:15:41 +01001282 if (w->active) {
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);
Mark Brown6ea31b92009-04-20 17:15:41 +01001285 return in != 0;
1286 } else {
1287 return dapm_generic_check_power(w);
1288 }
1289}
1290
1291/* Check to see if a DAC has power */
1292static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1293{
1294 int out;
1295
Mark Brownde02d072011-09-20 21:43:24 +01001296 DAPM_UPDATE_STAT(w, power_checks);
1297
Mark Brown6ea31b92009-04-20 17:15:41 +01001298 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001299 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001300 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown6ea31b92009-04-20 17:15:41 +01001301 return out != 0;
1302 } else {
1303 return dapm_generic_check_power(w);
1304 }
1305}
1306
Mark Brown246d0a12009-04-22 18:24:55 +01001307/* Check to see if a power supply is needed */
1308static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1309{
1310 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001311
Mark Brownde02d072011-09-20 21:43:24 +01001312 DAPM_UPDATE_STAT(w, power_checks);
1313
Mark Brown246d0a12009-04-22 18:24:55 +01001314 /* Check if one of our outputs is connected */
1315 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001316 DAPM_UPDATE_STAT(w, neighbour_checks);
1317
Mark Brownbf3a9e12011-06-13 16:42:29 +01001318 if (path->weak)
1319 continue;
1320
Mark Brown215edda2009-09-08 18:59:05 +01001321 if (path->connected &&
1322 !path->connected(path->source, path->sink))
1323 continue;
1324
Mark Brown30173582011-02-11 11:42:19 +00001325 if (!path->sink)
1326 continue;
1327
Mark Brownf68d7e12011-10-04 22:57:50 +01001328 if (dapm_widget_power_check(path->sink))
1329 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001330 }
1331
Mark Brownf68d7e12011-10-04 22:57:50 +01001332 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001333}
1334
Mark Brown35c64bc2011-09-28 18:23:53 +01001335static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1336{
1337 return 1;
1338}
1339
Mark Brown38357ab2009-06-06 19:03:23 +01001340static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1341 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001342 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001343{
Mark Brown828a8422011-01-15 13:14:30 +00001344 int *sort;
1345
1346 if (power_up)
1347 sort = dapm_up_seq;
1348 else
1349 sort = dapm_down_seq;
1350
Mark Brown38357ab2009-06-06 19:03:23 +01001351 if (sort[a->id] != sort[b->id])
1352 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001353 if (a->subseq != b->subseq) {
1354 if (power_up)
1355 return a->subseq - b->subseq;
1356 else
1357 return b->subseq - a->subseq;
1358 }
Mark Brownb22ead22009-06-07 12:51:26 +01001359 if (a->reg != b->reg)
1360 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001361 if (a->dapm != b->dapm)
1362 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001363
Mark Brown38357ab2009-06-06 19:03:23 +01001364 return 0;
1365}
Mark Brown42aa3412009-03-01 19:21:10 +00001366
Mark Brown38357ab2009-06-06 19:03:23 +01001367/* Insert a widget in order into a DAPM power sequence. */
1368static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1369 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001370 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001371{
1372 struct snd_soc_dapm_widget *w;
1373
1374 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001375 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001376 list_add_tail(&new_widget->power_list, &w->power_list);
1377 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001378 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001379
Mark Brown38357ab2009-06-06 19:03:23 +01001380 list_add_tail(&new_widget->power_list, list);
1381}
Mark Brown42aa3412009-03-01 19:21:10 +00001382
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001383static void dapm_seq_check_event(struct snd_soc_card *card,
Mark Brown68f89ad2010-11-03 23:51:49 -04001384 struct snd_soc_dapm_widget *w, int event)
1385{
Mark Brown68f89ad2010-11-03 23:51:49 -04001386 const char *ev_name;
1387 int power, ret;
1388
1389 switch (event) {
1390 case SND_SOC_DAPM_PRE_PMU:
1391 ev_name = "PRE_PMU";
1392 power = 1;
1393 break;
1394 case SND_SOC_DAPM_POST_PMU:
1395 ev_name = "POST_PMU";
1396 power = 1;
1397 break;
1398 case SND_SOC_DAPM_PRE_PMD:
1399 ev_name = "PRE_PMD";
1400 power = 0;
1401 break;
1402 case SND_SOC_DAPM_POST_PMD:
1403 ev_name = "POST_PMD";
1404 power = 0;
1405 break;
Mark Brown80114122013-02-25 15:14:19 +00001406 case SND_SOC_DAPM_WILL_PMU:
1407 ev_name = "WILL_PMU";
1408 power = 1;
1409 break;
1410 case SND_SOC_DAPM_WILL_PMD:
1411 ev_name = "WILL_PMD";
1412 power = 0;
1413 break;
Mark Brown68f89ad2010-11-03 23:51:49 -04001414 default:
1415 BUG();
1416 return;
1417 }
1418
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001419 if (w->new_power != power)
Mark Brown68f89ad2010-11-03 23:51:49 -04001420 return;
1421
1422 if (w->event && (w->event_flags & event)) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001423 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001424 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001425 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001426 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001427 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001428 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001429 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
Mark Brown68f89ad2010-11-03 23:51:49 -04001430 ev_name, w->name, ret);
1431 }
1432}
1433
Mark Brownb22ead22009-06-07 12:51:26 +01001434/* Apply the coalesced changes from a DAPM sequence */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001435static void dapm_seq_run_coalesced(struct snd_soc_card *card,
Mark Brownb22ead22009-06-07 12:51:26 +01001436 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001437{
Mark Brown68f89ad2010-11-03 23:51:49 -04001438 struct snd_soc_dapm_widget *w;
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001439 int reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001440 unsigned int value = 0;
1441 unsigned int mask = 0;
Mark Brownb22ead22009-06-07 12:51:26 +01001442
1443 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1444 power_list)->reg;
1445
1446 list_for_each_entry(w, pending, power_list) {
Mark Brownb22ead22009-06-07 12:51:26 +01001447 BUG_ON(reg != w->reg);
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001448 w->power = w->new_power;
Mark Brownb22ead22009-06-07 12:51:26 +01001449
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001450 mask |= w->mask << w->shift;
1451 if (w->power)
1452 value |= w->on_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001453 else
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001454 value |= w->off_val << w->shift;
Mark Brownb22ead22009-06-07 12:51:26 +01001455
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001456 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001457 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1458 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001459
Mark Brown68f89ad2010-11-03 23:51:49 -04001460 /* Check for events */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001461 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1462 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001463 }
1464
Mark Brown81628102009-06-07 13:21:24 +01001465 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001466 /* Any widget will do, they should all be updating the
1467 * same register.
1468 */
1469 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1470 power_list);
1471
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001472 pop_dbg(w->dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001473 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001474 value, mask, reg, card->pop_time);
1475 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001476 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001477 }
1478
1479 list_for_each_entry(w, pending, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001480 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1481 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001482 }
Mark Brown42aa3412009-03-01 19:21:10 +00001483}
1484
Mark Brownb22ead22009-06-07 12:51:26 +01001485/* Apply a DAPM power sequence.
1486 *
1487 * We walk over a pre-sorted list of widgets to apply power to. In
1488 * order to minimise the number of writes to the device required
1489 * multiple widgets will be updated in a single write where possible.
1490 * Currently anything that requires more than a single write is not
1491 * handled.
1492 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001493static void dapm_seq_run(struct snd_soc_card *card,
1494 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001495{
1496 struct snd_soc_dapm_widget *w, *n;
1497 LIST_HEAD(pending);
1498 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001499 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001500 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001501 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001502 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001503 int *sort;
1504
1505 if (power_up)
1506 sort = dapm_up_seq;
1507 else
1508 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001509
Mark Brownb22ead22009-06-07 12:51:26 +01001510 list_for_each_entry_safe(w, n, list, power_list) {
1511 ret = 0;
1512
1513 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001514 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001515 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001516 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001517 dapm_seq_run_coalesced(card, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001518
Mark Brown474b62d2011-01-18 16:14:44 +00001519 if (cur_dapm && cur_dapm->seq_notifier) {
1520 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1521 if (sort[i] == cur_sort)
1522 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001523 i,
1524 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001525 }
1526
Mark Brownb22ead22009-06-07 12:51:26 +01001527 INIT_LIST_HEAD(&pending);
1528 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001529 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001530 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001531 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001532 }
1533
Mark Brown163cac02009-06-07 10:12:52 +01001534 switch (w->id) {
1535 case snd_soc_dapm_pre:
1536 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001537 list_for_each_entry_safe_continue(w, n, list,
1538 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001539
Mark Brownb22ead22009-06-07 12:51:26 +01001540 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001541 ret = w->event(w,
1542 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001543 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001544 ret = w->event(w,
1545 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001546 break;
1547
1548 case snd_soc_dapm_post:
1549 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001550 list_for_each_entry_safe_continue(w, n, list,
1551 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001552
Mark Brownb22ead22009-06-07 12:51:26 +01001553 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001554 ret = w->event(w,
1555 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001556 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001557 ret = w->event(w,
1558 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001559 break;
1560
Mark Brown163cac02009-06-07 10:12:52 +01001561 default:
Mark Brown81628102009-06-07 13:21:24 +01001562 /* Queue it up for application */
1563 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001564 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001565 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001566 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001567 list_move(&w->power_list, &pending);
1568 break;
Mark Brown163cac02009-06-07 10:12:52 +01001569 }
Mark Brownb22ead22009-06-07 12:51:26 +01001570
1571 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001572 dev_err(w->dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001573 "ASoC: Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001574 }
Mark Brownb22ead22009-06-07 12:51:26 +01001575
1576 if (!list_empty(&pending))
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001577 dapm_seq_run_coalesced(card, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001578
1579 if (cur_dapm && cur_dapm->seq_notifier) {
1580 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1581 if (sort[i] == cur_sort)
1582 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001583 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001584 }
Mark Brown163cac02009-06-07 10:12:52 +01001585}
1586
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001587static void dapm_widget_update(struct snd_soc_card *card)
Mark Brown97404f22010-12-14 16:13:57 +00001588{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001589 struct snd_soc_dapm_update *update = card->update;
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001590 struct snd_soc_dapm_widget_list *wlist;
1591 struct snd_soc_dapm_widget *w = NULL;
1592 unsigned int wi;
Mark Brown97404f22010-12-14 16:13:57 +00001593 int ret;
1594
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001595 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
Mark Brown97404f22010-12-14 16:13:57 +00001596 return;
1597
Lars-Peter Clausene84357f2013-07-29 17:13:58 +02001598 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
Mark Brown97404f22010-12-14 16:13:57 +00001599
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001600 for (wi = 0; wi < wlist->num_widgets; wi++) {
1601 w = wlist->widgets[wi];
1602
1603 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1604 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1605 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001606 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001607 w->name, ret);
1608 }
Mark Brown97404f22010-12-14 16:13:57 +00001609 }
1610
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001611 if (!w)
1612 return;
1613
Liam Girdwood49575fb52012-03-06 18:16:19 +00001614 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001615 update->val);
1616 if (ret < 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001617 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001618 w->name, ret);
Mark Brown97404f22010-12-14 16:13:57 +00001619
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001620 for (wi = 0; wi < wlist->num_widgets; wi++) {
1621 w = wlist->widgets[wi];
1622
1623 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1624 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1625 if (ret != 0)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001626 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02001627 w->name, ret);
1628 }
Mark Brown97404f22010-12-14 16:13:57 +00001629 }
1630}
1631
Mark Brown9d0624a2011-02-18 11:49:43 -08001632/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1633 * they're changing state.
1634 */
1635static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1636{
1637 struct snd_soc_dapm_context *d = data;
1638 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001639
Mark Brown56fba412011-06-04 11:25:10 +01001640 /* If we're off and we're not supposed to be go into STANDBY */
1641 if (d->bias_level == SND_SOC_BIAS_OFF &&
1642 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001643 if (d->dev)
1644 pm_runtime_get_sync(d->dev);
1645
Mark Brown9d0624a2011-02-18 11:49:43 -08001646 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1647 if (ret != 0)
1648 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001649 "ASoC: Failed to turn on bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001650 }
1651
Mark Brown56fba412011-06-04 11:25:10 +01001652 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1653 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001654 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1655 if (ret != 0)
1656 dev_err(d->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001657 "ASoC: Failed to prepare bias: %d\n", ret);
Mark Brown9d0624a2011-02-18 11:49:43 -08001658 }
1659}
1660
1661/* Async callback run prior to DAPM sequences - brings to their final
1662 * state.
1663 */
1664static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1665{
1666 struct snd_soc_dapm_context *d = data;
1667 int ret;
1668
1669 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001670 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1671 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1672 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001673 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1674 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001675 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001676 ret);
1677 }
1678
1679 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001680 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1681 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001682 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1683 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001684 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1685 ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001686
1687 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001688 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001689 }
1690
1691 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001692 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1693 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001694 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1695 if (ret != 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00001696 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
Mark Brown9d0624a2011-02-18 11:49:43 -08001697 ret);
1698 }
1699}
Mark Brown97404f22010-12-14 16:13:57 +00001700
Mark Brownfe4fda52011-10-03 22:36:57 +01001701static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1702 bool power, bool connect)
1703{
1704 /* If a connection is being made or broken then that update
1705 * will have marked the peer dirty, otherwise the widgets are
1706 * not connected and this update has no impact. */
1707 if (!connect)
1708 return;
1709
1710 /* If the peer is already in the state we're moving to then we
1711 * won't have an impact on it. */
1712 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001713 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001714}
1715
Mark Brown05623c42011-09-28 17:02:31 +01001716static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1717 struct list_head *up_list,
1718 struct list_head *down_list)
1719{
Mark Browndb432b42011-10-03 21:06:40 +01001720 struct snd_soc_dapm_path *path;
1721
Mark Brown05623c42011-09-28 17:02:31 +01001722 if (w->power == power)
1723 return;
1724
1725 trace_snd_soc_dapm_widget_power(w, power);
1726
Mark Browndb432b42011-10-03 21:06:40 +01001727 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001728 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001729 */
1730 list_for_each_entry(path, &w->sources, list_sink) {
1731 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001732 dapm_widget_set_peer_power(path->source, power,
1733 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001734 }
1735 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001736 switch (w->id) {
1737 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001738 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001739 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02001740 case snd_soc_dapm_kcontrol:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001741 /* Supplies can't affect their outputs, only their inputs */
1742 break;
1743 default:
1744 list_for_each_entry(path, &w->sinks, list_source) {
1745 if (path->sink) {
1746 dapm_widget_set_peer_power(path->sink, power,
1747 path->connect);
1748 }
Mark Browndb432b42011-10-03 21:06:40 +01001749 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001750 break;
Mark Browndb432b42011-10-03 21:06:40 +01001751 }
1752
Mark Brown05623c42011-09-28 17:02:31 +01001753 if (power)
1754 dapm_seq_insert(w, up_list, true);
1755 else
1756 dapm_seq_insert(w, down_list, false);
Mark Brown05623c42011-09-28 17:02:31 +01001757}
1758
Mark Brown7c81beb2011-09-20 22:22:32 +01001759static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1760 struct list_head *up_list,
1761 struct list_head *down_list)
1762{
Mark Brown7c81beb2011-09-20 22:22:32 +01001763 int power;
1764
1765 switch (w->id) {
1766 case snd_soc_dapm_pre:
1767 dapm_seq_insert(w, down_list, false);
1768 break;
1769 case snd_soc_dapm_post:
1770 dapm_seq_insert(w, up_list, true);
1771 break;
1772
1773 default:
Mark Brownd8050022011-09-28 18:28:23 +01001774 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001775
Mark Brown05623c42011-09-28 17:02:31 +01001776 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001777 break;
1778 }
1779}
1780
Mark Brown42aa3412009-03-01 19:21:10 +00001781/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001782 * Scan each dapm widget for complete audio path.
1783 * A complete path is a route that has valid endpoints i.e.:-
1784 *
1785 * o DAC to output pin.
1786 * o Input Pin to ADC.
1787 * o Input pin to Output pin (bypass, sidetone)
1788 * o DAC to ADC (loopback).
1789 */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001790static int dapm_power_widgets(struct snd_soc_card *card, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001791{
1792 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001793 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001794 LIST_HEAD(up_list);
1795 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001796 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001797 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001798
Mark Brown84e90932010-11-04 00:07:02 -04001799 trace_snd_soc_dapm_start(card);
1800
Mark Brown56fba412011-06-04 11:25:10 +01001801 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001802 if (d->idle_bias_off)
1803 d->target_bias_level = SND_SOC_BIAS_OFF;
1804 else
1805 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001806 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001807
Liam Girdwood6c120e12012-02-15 15:15:34 +00001808 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001809
Mark Brown6d3ddc82009-05-16 17:47:29 +01001810 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001811 * lists indicating if they should be powered up or down. We
1812 * only check widgets that have been flagged as dirty but note
1813 * that new widgets may be added to the dirty list while we
1814 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001815 */
Mark Browndb432b42011-10-03 21:06:40 +01001816 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001817 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001818 }
1819
Mark Brownf9de6d72011-09-28 17:19:47 +01001820 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001821 switch (w->id) {
1822 case snd_soc_dapm_pre:
1823 case snd_soc_dapm_post:
1824 /* These widgets always need to be powered */
1825 break;
1826 default:
1827 list_del_init(&w->dirty);
1828 break;
1829 }
Mark Browndb432b42011-10-03 21:06:40 +01001830
Lars-Peter Clausen39eb5fd2013-07-29 17:14:03 +02001831 if (w->new_power) {
Mark Brownf9de6d72011-09-28 17:19:47 +01001832 d = w->dapm;
1833
1834 /* Supplies and micbiases only bring the
1835 * context up to STANDBY as unless something
1836 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001837 * generally don't require full power. Signal
1838 * generators are virtual pins and have no
1839 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001840 */
1841 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001842 case snd_soc_dapm_siggen:
1843 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001844 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001845 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001846 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001847 case snd_soc_dapm_micbias:
1848 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1849 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1850 break;
1851 default:
1852 d->target_bias_level = SND_SOC_BIAS_ON;
1853 break;
1854 }
1855 }
1856
1857 }
1858
Mark Brown85a843c2011-09-21 21:29:47 +01001859 /* Force all contexts in the card to the same bias state if
1860 * they're not ground referenced.
1861 */
Mark Brown56fba412011-06-04 11:25:10 +01001862 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001863 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001864 if (d->target_bias_level > bias)
1865 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001866 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001867 if (!d->idle_bias_off)
1868 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001869
Mark Brownde02d072011-09-20 21:43:24 +01001870 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001871
Mark Brown9d0624a2011-02-18 11:49:43 -08001872 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001873 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001874 async_schedule_domain(dapm_pre_sequence_async, d,
1875 &async_domain);
1876 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001877
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001878 list_for_each_entry(w, &down_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001879 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
Mark Brown80114122013-02-25 15:14:19 +00001880 }
1881
Lars-Peter Clausencf1f7c62013-05-23 00:12:53 +02001882 list_for_each_entry(w, &up_list, power_list) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001883 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
Mark Brown80114122013-02-25 15:14:19 +00001884 }
1885
Mark Brown6d3ddc82009-05-16 17:47:29 +01001886 /* Power down widgets first; try to avoid amplifying pops. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001887 dapm_seq_run(card, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001888
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001889 dapm_widget_update(card);
Mark Brown97404f22010-12-14 16:13:57 +00001890
Mark Brown6d3ddc82009-05-16 17:47:29 +01001891 /* Now power up. */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001892 dapm_seq_run(card, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001893
Mark Brown9d0624a2011-02-18 11:49:43 -08001894 /* Run all the bias changes in parallel */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001895 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown9d0624a2011-02-18 11:49:43 -08001896 async_schedule_domain(dapm_post_sequence_async, d,
1897 &async_domain);
1898 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001899
Liam Girdwood8078d872012-02-15 15:15:35 +00001900 /* do we need to notify any clients that DAPM event is complete */
1901 list_for_each_entry(d, &card->dapm_list, list) {
1902 if (d->stream_event)
1903 d->stream_event(d, event);
1904 }
1905
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02001906 pop_dbg(card->dev, card->pop_time,
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001907 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001908 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001909
Mark Brown84e90932010-11-04 00:07:02 -04001910 trace_snd_soc_dapm_done(card);
1911
Mark Brown42aa3412009-03-01 19:21:10 +00001912 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001913}
1914
Mark Brown79fb9382009-08-21 16:38:13 +01001915#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001916static ssize_t dapm_widget_power_read_file(struct file *file,
1917 char __user *user_buf,
1918 size_t count, loff_t *ppos)
1919{
1920 struct snd_soc_dapm_widget *w = file->private_data;
1921 char *buf;
1922 int in, out;
1923 ssize_t ret;
1924 struct snd_soc_dapm_path *p = NULL;
1925
1926 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1927 if (!buf)
1928 return -ENOMEM;
1929
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001930 in = is_connected_input_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001931 dapm_clear_walk_input(w->dapm, &w->sources);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001932 out = is_connected_output_ep(w, NULL);
Ryo Tsutsui1059ecf2013-04-01 12:50:01 +01001933 dapm_clear_walk_output(w->dapm, &w->sinks);
Mark Brown79fb9382009-08-21 16:38:13 +01001934
Mark Brownf13ebad2012-03-03 18:01:01 +00001935 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1936 w->name, w->power ? "On" : "Off",
1937 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001938
Mark Brownd033c362009-12-04 15:25:56 +00001939 if (w->reg >= 0)
1940 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02001941 " - R%d(0x%x) mask 0x%x",
1942 w->reg, w->reg, w->mask << w->shift);
Mark Brownd033c362009-12-04 15:25:56 +00001943
1944 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1945
Mark Brown3eef08b2009-09-14 16:49:00 +01001946 if (w->sname)
1947 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1948 w->sname,
1949 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001950
1951 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001952 if (p->connected && !p->connected(w, p->sink))
1953 continue;
1954
Mark Brown79fb9382009-08-21 16:38:13 +01001955 if (p->connect)
1956 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001957 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001958 p->name ? p->name : "static",
1959 p->source->name);
1960 }
1961 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001962 if (p->connected && !p->connected(w, p->sink))
1963 continue;
1964
Mark Brown79fb9382009-08-21 16:38:13 +01001965 if (p->connect)
1966 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001967 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001968 p->name ? p->name : "static",
1969 p->sink->name);
1970 }
1971
1972 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1973
1974 kfree(buf);
1975 return ret;
1976}
1977
1978static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001979 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001980 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001981 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001982};
1983
Mark Brownef49e4f2011-04-04 20:48:13 +09001984static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1985 size_t count, loff_t *ppos)
1986{
1987 struct snd_soc_dapm_context *dapm = file->private_data;
1988 char *level;
1989
1990 switch (dapm->bias_level) {
1991 case SND_SOC_BIAS_ON:
1992 level = "On\n";
1993 break;
1994 case SND_SOC_BIAS_PREPARE:
1995 level = "Prepare\n";
1996 break;
1997 case SND_SOC_BIAS_STANDBY:
1998 level = "Standby\n";
1999 break;
2000 case SND_SOC_BIAS_OFF:
2001 level = "Off\n";
2002 break;
2003 default:
2004 BUG();
2005 level = "Unknown\n";
2006 break;
2007 }
2008
2009 return simple_read_from_buffer(user_buf, count, ppos, level,
2010 strlen(level));
2011}
2012
2013static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07002014 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09002015 .read = dapm_bias_read_file,
2016 .llseek = default_llseek,
2017};
2018
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002019void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2020 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002021{
Mark Brown79fb9382009-08-21 16:38:13 +01002022 struct dentry *d;
2023
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002024 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2025
2026 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00002027 dev_warn(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002028 "ASoC: Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002029 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002030 }
Mark Brown79fb9382009-08-21 16:38:13 +01002031
Mark Brownef49e4f2011-04-04 20:48:13 +09002032 d = debugfs_create_file("bias_level", 0444,
2033 dapm->debugfs_dapm, dapm,
2034 &dapm_bias_fops);
2035 if (!d)
2036 dev_warn(dapm->dev,
2037 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01002038}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002039
2040static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2041{
2042 struct snd_soc_dapm_context *dapm = w->dapm;
2043 struct dentry *d;
2044
2045 if (!dapm->debugfs_dapm || !w->name)
2046 return;
2047
2048 d = debugfs_create_file(w->name, 0444,
2049 dapm->debugfs_dapm, w,
2050 &dapm_widget_power_fops);
2051 if (!d)
2052 dev_warn(w->dapm->dev,
2053 "ASoC: Failed to create %s debugfs file\n",
2054 w->name);
2055}
2056
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002057static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2058{
2059 debugfs_remove_recursive(dapm->debugfs_dapm);
2060}
2061
Mark Brown79fb9382009-08-21 16:38:13 +01002062#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02002063void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2064 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01002065{
2066}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002067
2068static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2069{
2070}
2071
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002072static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2073{
2074}
2075
Mark Brown79fb9382009-08-21 16:38:13 +01002076#endif
2077
Richard Purdie2b97eab2006-10-06 18:32:18 +02002078/* test and update the power status of a mux widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002079static int soc_dapm_mux_update_power(struct snd_soc_card *card,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002080 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002081{
2082 struct snd_soc_dapm_path *path;
2083 int found = 0;
2084
Richard Purdie2b97eab2006-10-06 18:32:18 +02002085 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002086 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Zhaocb01e2b2008-10-07 08:05:20 +08002087 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02002088 continue;
2089
2090 found = 1;
2091 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01002092 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002093 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01002094 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01002095 } else {
2096 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01002097 dapm_mark_dirty(path->source,
2098 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002099 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01002100 }
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002101 dapm_mark_dirty(path->sink, "mux change");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002102 }
2103
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002104 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002105 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002106
Liam Girdwood618dae12012-04-25 12:12:51 +01002107 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002108}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002109
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002110int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002111 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2112 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002113{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002114 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002115 int ret;
2116
Liam Girdwood3cd04342012-03-09 12:02:08 +00002117 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002118 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002119 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002120 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002121 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002122 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002123 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002124 return ret;
2125}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002126EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002127
Milan plzik1b075e32008-01-10 14:39:46 +01002128/* test and update the power status of a mixer or switch widget */
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002129static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
Mark Brown283375c2009-12-07 18:09:03 +00002130 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002131{
2132 struct snd_soc_dapm_path *path;
2133 int found = 0;
2134
Richard Purdie2b97eab2006-10-06 18:32:18 +02002135 /* find dapm widget path assoc with kcontrol */
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002136 dapm_kcontrol_for_each_path(path, kcontrol) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002137 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00002138 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01002139 dapm_mark_dirty(path->source, "mixer connection");
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002140 dapm_mark_dirty(path->sink, "mixer update");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002141 }
2142
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002143 if (found)
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002144 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002145
Liam Girdwood618dae12012-04-25 12:12:51 +01002146 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002147}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002148
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002149int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
Lars-Peter Clausen6b3fc032013-07-24 15:27:38 +02002150 struct snd_kcontrol *kcontrol, int connect,
2151 struct snd_soc_dapm_update *update)
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002152{
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002153 struct snd_soc_card *card = dapm->card;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002154 int ret;
2155
Liam Girdwood3cd04342012-03-09 12:02:08 +00002156 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002157 card->update = update;
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002158 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002159 card->update = NULL;
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002160 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01002161 if (ret > 0)
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002162 soc_dpcm_runtime_update(card);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002163 return ret;
2164}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002165EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002166
2167/* show dapm widget status in sys fs */
2168static ssize_t dapm_widget_show(struct device *dev,
2169 struct device_attribute *attr, char *buf)
2170{
Mark Brown36ae1a92012-01-06 17:12:45 -08002171 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002172 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002173 struct snd_soc_dapm_widget *w;
2174 int count = 0;
2175 char *state = "not set";
2176
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002177 list_for_each_entry(w, &codec->card->widgets, list) {
2178 if (w->dapm != &codec->dapm)
2179 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002180
2181 /* only display widgets that burnm power */
2182 switch (w->id) {
2183 case snd_soc_dapm_hp:
2184 case snd_soc_dapm_mic:
2185 case snd_soc_dapm_spk:
2186 case snd_soc_dapm_line:
2187 case snd_soc_dapm_micbias:
2188 case snd_soc_dapm_dac:
2189 case snd_soc_dapm_adc:
2190 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002191 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002192 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002193 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002194 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002195 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002196 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002197 if (w->name)
2198 count += sprintf(buf + count, "%s: %s\n",
2199 w->name, w->power ? "On":"Off");
2200 break;
2201 default:
2202 break;
2203 }
2204 }
2205
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002206 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002207 case SND_SOC_BIAS_ON:
2208 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002209 break;
Mark Brown0be98982008-05-19 12:31:28 +02002210 case SND_SOC_BIAS_PREPARE:
2211 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002212 break;
Mark Brown0be98982008-05-19 12:31:28 +02002213 case SND_SOC_BIAS_STANDBY:
2214 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002215 break;
Mark Brown0be98982008-05-19 12:31:28 +02002216 case SND_SOC_BIAS_OFF:
2217 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002218 break;
2219 }
2220 count += sprintf(buf + count, "PM State: %s\n", state);
2221
2222 return count;
2223}
2224
2225static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2226
2227int snd_soc_dapm_sys_add(struct device *dev)
2228{
Troy Kisky12ef1932008-10-13 17:42:14 -07002229 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002230}
2231
2232static void snd_soc_dapm_sys_remove(struct device *dev)
2233{
Mark Brownaef90842009-05-16 17:53:16 +01002234 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002235}
2236
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002237static void dapm_free_path(struct snd_soc_dapm_path *path)
2238{
2239 list_del(&path->list_sink);
2240 list_del(&path->list_source);
Lars-Peter Clausen5106b922013-07-29 17:14:00 +02002241 list_del(&path->list_kcontrol);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002242 list_del(&path->list);
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002243 kfree(path);
2244}
2245
Richard Purdie2b97eab2006-10-06 18:32:18 +02002246/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002247static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002248{
2249 struct snd_soc_dapm_widget *w, *next_w;
2250 struct snd_soc_dapm_path *p, *next_p;
2251
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002252 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2253 if (w->dapm != dapm)
2254 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002255 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002256 /*
2257 * remove source and sink paths associated to this widget.
2258 * While removing the path, remove reference to it from both
2259 * source and sink widgets so that path is removed only once.
2260 */
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002261 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2262 dapm_free_path(p);
2263
2264 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2265 dapm_free_path(p);
2266
Stephen Warrenfad59882011-04-28 17:37:59 -06002267 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002268 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002269 kfree(w);
2270 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002271}
2272
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002273static struct snd_soc_dapm_widget *dapm_find_widget(
2274 struct snd_soc_dapm_context *dapm, const char *pin,
2275 bool search_other_contexts)
2276{
2277 struct snd_soc_dapm_widget *w;
2278 struct snd_soc_dapm_widget *fallback = NULL;
2279
2280 list_for_each_entry(w, &dapm->card->widgets, list) {
2281 if (!strcmp(w->name, pin)) {
2282 if (w->dapm == dapm)
2283 return w;
2284 else
2285 fallback = w;
2286 }
2287 }
2288
2289 if (search_other_contexts)
2290 return fallback;
2291
2292 return NULL;
2293}
2294
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002295static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002296 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002297{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002298 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002299
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002300 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002301 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002302 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002303 }
2304
Mark Brown1a8b2d92012-02-16 11:50:07 -08002305 if (w->connected != status)
2306 dapm_mark_dirty(w, "pin configuration");
2307
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002308 w->connected = status;
2309 if (status == 0)
2310 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002311
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002312 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002313}
2314
Richard Purdie2b97eab2006-10-06 18:32:18 +02002315/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002316 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002317 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002318 *
2319 * Walks all dapm audio paths and powers widgets according to their
2320 * stream or path usage.
2321 *
2322 * Returns 0 for success.
2323 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002324int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002325{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002326 int ret;
2327
Mark Brown4f4c0072011-10-07 14:29:19 +01002328 /*
2329 * Suppress early reports (eg, jacks syncing their state) to avoid
2330 * silly DAPM runs during card startup.
2331 */
2332 if (!dapm->card || !dapm->card->instantiated)
2333 return 0;
2334
Liam Girdwood3cd04342012-03-09 12:02:08 +00002335 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002336 ret = dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002337 mutex_unlock(&dapm->card->dapm_mutex);
2338 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002339}
Liam Girdwooda5302182008-07-07 13:35:17 +01002340EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002341
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002342static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2343 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2344 const char *control,
2345 int (*connected)(struct snd_soc_dapm_widget *source,
2346 struct snd_soc_dapm_widget *sink))
Richard Purdie2b97eab2006-10-06 18:32:18 +02002347{
2348 struct snd_soc_dapm_path *path;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002349 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002350
2351 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2352 if (!path)
2353 return -ENOMEM;
2354
2355 path->source = wsource;
2356 path->sink = wsink;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002357 path->connected = connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002358 INIT_LIST_HEAD(&path->list);
Mark Brown69c2d342013-08-13 00:20:36 +01002359 INIT_LIST_HEAD(&path->list_kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002360 INIT_LIST_HEAD(&path->list_source);
2361 INIT_LIST_HEAD(&path->list_sink);
2362
2363 /* check for external widgets */
2364 if (wsink->id == snd_soc_dapm_input) {
2365 if (wsource->id == snd_soc_dapm_micbias ||
2366 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002367 wsource->id == snd_soc_dapm_line ||
2368 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002369 wsink->ext = 1;
2370 }
2371 if (wsource->id == snd_soc_dapm_output) {
2372 if (wsink->id == snd_soc_dapm_spk ||
2373 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002374 wsink->id == snd_soc_dapm_line ||
2375 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002376 wsource->ext = 1;
2377 }
2378
Lars-Peter Clausen34742cb2013-08-27 15:50:54 +02002379 dapm_mark_dirty(wsource, "Route added");
2380 dapm_mark_dirty(wsink, "Route added");
2381
Richard Purdie2b97eab2006-10-06 18:32:18 +02002382 /* connect static paths */
2383 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002384 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002385 list_add(&path->list_sink, &wsink->sources);
2386 list_add(&path->list_source, &wsource->sinks);
2387 path->connect = 1;
2388 return 0;
2389 }
2390
2391 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002392 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002393 case snd_soc_dapm_adc:
2394 case snd_soc_dapm_dac:
2395 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002396 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002397 case snd_soc_dapm_input:
2398 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002399 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002400 case snd_soc_dapm_micbias:
2401 case snd_soc_dapm_vmid:
2402 case snd_soc_dapm_pre:
2403 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002404 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002405 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002406 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002407 case snd_soc_dapm_aif_in:
2408 case snd_soc_dapm_aif_out:
Mark Brown46162742013-06-05 19:36:11 +01002409 case snd_soc_dapm_dai_in:
2410 case snd_soc_dapm_dai_out:
Mark Brownc74184e2012-04-04 22:12:09 +01002411 case snd_soc_dapm_dai_link:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002412 case snd_soc_dapm_kcontrol:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002413 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002414 list_add(&path->list_sink, &wsink->sources);
2415 list_add(&path->list_source, &wsource->sinks);
2416 path->connect = 1;
2417 return 0;
2418 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002419 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002420 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002421 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002422 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002423 if (ret != 0)
2424 goto err;
2425 break;
2426 case snd_soc_dapm_switch:
2427 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002428 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002429 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002430 if (ret != 0)
2431 goto err;
2432 break;
2433 case snd_soc_dapm_hp:
2434 case snd_soc_dapm_mic:
2435 case snd_soc_dapm_line:
2436 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002437 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002438 list_add(&path->list_sink, &wsink->sources);
2439 list_add(&path->list_source, &wsource->sinks);
2440 path->connect = 0;
2441 return 0;
2442 }
Mark Brownfabd0382012-07-05 17:20:06 +01002443
Richard Purdie2b97eab2006-10-06 18:32:18 +02002444 return 0;
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002445err:
2446 kfree(path);
2447 return ret;
2448}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002449
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002450static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2451 const struct snd_soc_dapm_route *route)
2452{
2453 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2454 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2455 const char *sink;
2456 const char *source;
2457 char prefixed_sink[80];
2458 char prefixed_source[80];
2459 int ret;
2460
2461 if (dapm->codec && dapm->codec->name_prefix) {
2462 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2463 dapm->codec->name_prefix, route->sink);
2464 sink = prefixed_sink;
2465 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2466 dapm->codec->name_prefix, route->source);
2467 source = prefixed_source;
2468 } else {
2469 sink = route->sink;
2470 source = route->source;
2471 }
2472
2473 /*
2474 * find src and dest widgets over all widgets but favor a widget from
2475 * current DAPM context
2476 */
2477 list_for_each_entry(w, &dapm->card->widgets, list) {
2478 if (!wsink && !(strcmp(w->name, sink))) {
2479 wtsink = w;
2480 if (w->dapm == dapm)
2481 wsink = w;
2482 continue;
2483 }
2484 if (!wsource && !(strcmp(w->name, source))) {
2485 wtsource = w;
2486 if (w->dapm == dapm)
2487 wsource = w;
2488 }
2489 }
2490 /* use widget from another DAPM context if not found from this */
2491 if (!wsink)
2492 wsink = wtsink;
2493 if (!wsource)
2494 wsource = wtsource;
2495
2496 if (wsource == NULL) {
2497 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2498 route->source);
2499 return -ENODEV;
2500 }
2501 if (wsink == NULL) {
2502 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2503 route->sink);
2504 return -ENODEV;
2505 }
2506
2507 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2508 route->connected);
2509 if (ret)
2510 goto err;
2511
2512 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002513err:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002514 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02002515 source, route->control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002516 return ret;
2517}
Mark Brown105f1c22008-05-13 14:52:19 +02002518
Mark Brownefcc3c62012-07-05 17:24:19 +01002519static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2520 const struct snd_soc_dapm_route *route)
2521{
2522 struct snd_soc_dapm_path *path, *p;
2523 const char *sink;
2524 const char *source;
2525 char prefixed_sink[80];
2526 char prefixed_source[80];
2527
2528 if (route->control) {
2529 dev_err(dapm->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002530 "ASoC: Removal of routes with controls not supported\n");
Mark Brownefcc3c62012-07-05 17:24:19 +01002531 return -EINVAL;
2532 }
2533
2534 if (dapm->codec && dapm->codec->name_prefix) {
2535 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2536 dapm->codec->name_prefix, route->sink);
2537 sink = prefixed_sink;
2538 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2539 dapm->codec->name_prefix, route->source);
2540 source = prefixed_source;
2541 } else {
2542 sink = route->sink;
2543 source = route->source;
2544 }
2545
2546 path = NULL;
2547 list_for_each_entry(p, &dapm->card->paths, list) {
2548 if (strcmp(p->source->name, source) != 0)
2549 continue;
2550 if (strcmp(p->sink->name, sink) != 0)
2551 continue;
2552 path = p;
2553 break;
2554 }
2555
2556 if (path) {
2557 dapm_mark_dirty(path->source, "Route removed");
2558 dapm_mark_dirty(path->sink, "Route removed");
2559
Lars-Peter Clausen88722932013-06-14 13:16:53 +02002560 dapm_free_path(path);
Mark Brownefcc3c62012-07-05 17:24:19 +01002561 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002562 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
Mark Brownefcc3c62012-07-05 17:24:19 +01002563 source, sink);
2564 }
2565
2566 return 0;
2567}
2568
Mark Brown105f1c22008-05-13 14:52:19 +02002569/**
Mark Brown105f1c22008-05-13 14:52:19 +02002570 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002571 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002572 * @route: audio routes
2573 * @num: number of routes
2574 *
2575 * Connects 2 dapm widgets together via a named audio path. The sink is
2576 * the widget receiving the audio signal, whilst the source is the sender
2577 * of the audio signal.
2578 *
2579 * Returns 0 for success else error. On error all resources can be freed
2580 * with a call to snd_soc_card_free().
2581 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002582int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002583 const struct snd_soc_dapm_route *route, int num)
2584{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002585 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002586
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002587 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002588 for (i = 0; i < num; i++) {
Mark Brown62d4a4b2012-06-22 12:21:49 +01002589 r = snd_soc_dapm_add_route(dapm, route);
2590 if (r < 0) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002591 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2592 route->source,
2593 route->control ? route->control : "direct",
2594 route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002595 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002596 }
2597 route++;
2598 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002599 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002600
Dan Carpenter60884c22012-04-13 22:25:43 +03002601 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002602}
2603EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2604
Mark Brownefcc3c62012-07-05 17:24:19 +01002605/**
2606 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2607 * @dapm: DAPM context
2608 * @route: audio routes
2609 * @num: number of routes
2610 *
2611 * Removes routes from the DAPM context.
2612 */
2613int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2614 const struct snd_soc_dapm_route *route, int num)
2615{
2616 int i, ret = 0;
2617
2618 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2619 for (i = 0; i < num; i++) {
2620 snd_soc_dapm_del_route(dapm, route);
2621 route++;
2622 }
2623 mutex_unlock(&dapm->card->dapm_mutex);
2624
2625 return ret;
2626}
2627EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2628
Mark Brownbf3a9e12011-06-13 16:42:29 +01002629static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2630 const struct snd_soc_dapm_route *route)
2631{
2632 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2633 route->source,
2634 true);
2635 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2636 route->sink,
2637 true);
2638 struct snd_soc_dapm_path *path;
2639 int count = 0;
2640
2641 if (!source) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002642 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002643 route->source);
2644 return -ENODEV;
2645 }
2646
2647 if (!sink) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002648 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002649 route->sink);
2650 return -ENODEV;
2651 }
2652
2653 if (route->control || route->connected)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002654 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002655 route->source, route->sink);
2656
2657 list_for_each_entry(path, &source->sinks, list_source) {
2658 if (path->sink == sink) {
2659 path->weak = 1;
2660 count++;
2661 }
2662 }
2663
2664 if (count == 0)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002665 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002666 route->source, route->sink);
2667 if (count > 1)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002668 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
Mark Brownbf3a9e12011-06-13 16:42:29 +01002669 count, route->source, route->sink);
2670
2671 return 0;
2672}
2673
2674/**
2675 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2676 * @dapm: DAPM context
2677 * @route: audio routes
2678 * @num: number of routes
2679 *
2680 * Mark existing routes matching those specified in the passed array
2681 * as being weak, meaning that they are ignored for the purpose of
2682 * power decisions. The main intended use case is for sidetone paths
2683 * which couple audio between other independent paths if they are both
2684 * active in order to make the combination work better at the user
2685 * level but which aren't intended to be "used".
2686 *
2687 * Note that CODEC drivers should not use this as sidetone type paths
2688 * can frequently also be used as bypass paths.
2689 */
2690int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2691 const struct snd_soc_dapm_route *route, int num)
2692{
2693 int i, err;
2694 int ret = 0;
2695
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002696 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002697 for (i = 0; i < num; i++) {
2698 err = snd_soc_dapm_weak_route(dapm, route);
2699 if (err)
2700 ret = err;
2701 route++;
2702 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002703 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002704
2705 return ret;
2706}
2707EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2708
Mark Brown105f1c22008-05-13 14:52:19 +02002709/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002710 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002711 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002712 *
2713 * Checks the codec for any new dapm widgets and creates them if found.
2714 *
2715 * Returns 0 for success.
2716 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002717int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002718{
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002719 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002720 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002721 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002722
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002723 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002724
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002725 list_for_each_entry(w, &card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002726 {
2727 if (w->new)
2728 continue;
2729
Stephen Warrenfad59882011-04-28 17:37:59 -06002730 if (w->num_kcontrols) {
2731 w->kcontrols = kzalloc(w->num_kcontrols *
2732 sizeof(struct snd_kcontrol *),
2733 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002734 if (!w->kcontrols) {
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002735 mutex_unlock(&card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002736 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002737 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002738 }
2739
Richard Purdie2b97eab2006-10-06 18:32:18 +02002740 switch(w->id) {
2741 case snd_soc_dapm_switch:
2742 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002743 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002744 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002745 break;
2746 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002747 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002748 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002749 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002750 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002751 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002752 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002753 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002754 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002755 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002756 break;
2757 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002758
2759 /* Read the initial power state from the device */
2760 if (w->reg >= 0) {
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02002761 val = soc_widget_read(w, w->reg) >> w->shift;
2762 val &= w->mask;
2763 if (val == w->on_val)
Mark Brownb66a70d2011-02-09 18:04:11 +00002764 w->power = 1;
2765 }
2766
Richard Purdie2b97eab2006-10-06 18:32:18 +02002767 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002768
Mark Brown7508b122011-10-05 12:09:12 +01002769 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002770 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002771 }
2772
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002773 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2774 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002775 return 0;
2776}
2777EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2778
2779/**
2780 * snd_soc_dapm_get_volsw - dapm mixer get callback
2781 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002782 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002783 *
2784 * Callback to get the value of a dapm mixer control.
2785 *
2786 * Returns 0 for success.
2787 */
2788int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2789 struct snd_ctl_elem_value *ucontrol)
2790{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002791 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002792 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002793 struct soc_mixer_control *mc =
2794 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002795 unsigned int reg = mc->reg;
2796 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002797 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002798 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002799 unsigned int invert = mc->invert;
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002800 unsigned int val;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002801
2802 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002803 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002804 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002805 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002806
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002807 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2808 if (dapm_kcontrol_is_powered(kcontrol))
2809 val = (snd_soc_read(codec, reg) >> shift) & mask;
2810 else
2811 val = dapm_kcontrol_get_value(kcontrol);
2812 mutex_unlock(&card->dapm_mutex);
2813
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002814 if (invert)
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002815 ucontrol->value.integer.value[0] = max - val;
2816 else
2817 ucontrol->value.integer.value[0] = val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002818
2819 return 0;
2820}
2821EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2822
2823/**
2824 * snd_soc_dapm_put_volsw - dapm mixer set callback
2825 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002826 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002827 *
2828 * Callback to set the value of a dapm mixer control.
2829 *
2830 * Returns 0 for success.
2831 */
2832int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2833 struct snd_ctl_elem_value *ucontrol)
2834{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002835 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002836 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002837 struct soc_mixer_control *mc =
2838 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002839 unsigned int reg = mc->reg;
2840 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002841 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002842 unsigned int mask = (1 << fls(max)) - 1;
2843 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002844 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002845 int connect, change;
2846 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002847
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002848 if (snd_soc_volsw_is_stereo(mc))
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002849 dev_warn(codec->dapm.dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00002850 "ASoC: Control '%s' is stereo, which is not supported\n",
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002851 kcontrol->id.name);
2852
Richard Purdie2b97eab2006-10-06 18:32:18 +02002853 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002854 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002855
2856 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002857 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002858
Liam Girdwood3cd04342012-03-09 12:02:08 +00002859 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002860
Lars-Peter Clausen57295072013-08-05 11:27:31 +02002861 dapm_kcontrol_set_value(kcontrol, val);
2862
2863 mask = mask << shift;
2864 val = val << shift;
2865
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002866 change = snd_soc_test_bits(codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002867 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002868 update.kcontrol = kcontrol;
2869 update.reg = reg;
2870 update.mask = mask;
2871 update.val = val;
Mark Brown283375c2009-12-07 18:09:03 +00002872
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002873 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002874
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002875 soc_dapm_mixer_update_power(card, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00002876
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002877 card->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00002878 }
2879
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002880 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02002881 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002882}
2883EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2884
2885/**
2886 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2887 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002888 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002889 *
2890 * Callback to get the value of a dapm enumerated double mixer control.
2891 *
2892 * Returns 0 for success.
2893 */
2894int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2895 struct snd_ctl_elem_value *ucontrol)
2896{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002897 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002898 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002899 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002900
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002901 val = snd_soc_read(codec, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002902 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002903 if (e->shift_l != e->shift_r)
2904 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002905 (val >> e->shift_r) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002906
2907 return 0;
2908}
2909EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2910
2911/**
2912 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2913 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002914 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002915 *
2916 * Callback to set the value of a dapm enumerated double mixer control.
2917 *
2918 * Returns 0 for success.
2919 */
2920int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2921 struct snd_ctl_elem_value *ucontrol)
2922{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002923 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002924 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002925 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002926 unsigned int val, mux, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002927 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002928 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002929
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002930 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002931 return -EINVAL;
2932 mux = ucontrol->value.enumerated.item[0];
2933 val = mux << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002934 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002935 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002936 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002937 return -EINVAL;
2938 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002939 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002940 }
2941
Liam Girdwood3cd04342012-03-09 12:02:08 +00002942 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002943
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02002944 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002945 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02002946 update.kcontrol = kcontrol;
2947 update.reg = e->reg;
2948 update.mask = mask;
2949 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002950 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002951
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02002952 soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002953
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02002954 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002955 }
2956
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002957 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002958 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002959}
2960EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2961
2962/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002963 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2964 * @kcontrol: mixer control
2965 * @ucontrol: control element information
2966 *
2967 * Returns 0 for success.
2968 */
2969int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2970 struct snd_ctl_elem_value *ucontrol)
2971{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002972 ucontrol->value.enumerated.item[0] = dapm_kcontrol_get_value(kcontrol);
Mark Brownd2b247a2009-10-06 15:21:04 +01002973 return 0;
2974}
2975EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2976
2977/**
2978 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2979 * @kcontrol: mixer control
2980 * @ucontrol: control element information
2981 *
2982 * Returns 0 for success.
2983 */
2984int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2985 struct snd_ctl_elem_value *ucontrol)
2986{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002987 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002988 struct snd_soc_card *card = codec->card;
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002989 unsigned int value;
Mark Brownd2b247a2009-10-06 15:21:04 +01002990 struct soc_enum *e =
2991 (struct soc_enum *)kcontrol->private_value;
2992 int change;
Mark Brownd2b247a2009-10-06 15:21:04 +01002993
2994 if (ucontrol->value.enumerated.item[0] >= e->max)
2995 return -EINVAL;
2996
Liam Girdwood3cd04342012-03-09 12:02:08 +00002997 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01002998
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02002999 value = ucontrol->value.enumerated.item[0];
3000 change = dapm_kcontrol_set_value(kcontrol, value);
3001 if (change)
3002 soc_dapm_mux_update_power(card, kcontrol, value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003003
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003004 mutex_unlock(&card->dapm_mutex);
Lars-Peter Clausen56a67832013-07-24 15:27:35 +02003005 return change;
Mark Brownd2b247a2009-10-06 15:21:04 +01003006}
3007EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
3008
3009/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003010 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
3011 * callback
3012 * @kcontrol: mixer control
3013 * @ucontrol: control element information
3014 *
3015 * Callback to get the value of a dapm semi enumerated double mixer control.
3016 *
3017 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3018 * used for handling bitfield coded enumeration for example.
3019 *
3020 * Returns 0 for success.
3021 */
3022int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
3023 struct snd_ctl_elem_value *ucontrol)
3024{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003025 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02003026 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03003027 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003028
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003029 reg_val = snd_soc_read(codec, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003030 val = (reg_val >> e->shift_l) & e->mask;
3031 for (mux = 0; mux < e->max; mux++) {
3032 if (val == e->values[mux])
3033 break;
3034 }
3035 ucontrol->value.enumerated.item[0] = mux;
3036 if (e->shift_l != e->shift_r) {
3037 val = (reg_val >> e->shift_r) & e->mask;
3038 for (mux = 0; mux < e->max; mux++) {
3039 if (val == e->values[mux])
3040 break;
3041 }
3042 ucontrol->value.enumerated.item[1] = mux;
3043 }
3044
3045 return 0;
3046}
3047EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
3048
3049/**
3050 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
3051 * callback
3052 * @kcontrol: mixer control
3053 * @ucontrol: control element information
3054 *
3055 * Callback to set the value of a dapm semi enumerated double mixer control.
3056 *
3057 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3058 * used for handling bitfield coded enumeration for example.
3059 *
3060 * Returns 0 for success.
3061 */
3062int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
3063 struct snd_ctl_elem_value *ucontrol)
3064{
Lars-Peter Clausencf7c1de2013-07-29 17:13:59 +02003065 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003066 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02003067 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01003068 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03003069 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00003070 struct snd_soc_dapm_update update;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003071
3072 if (ucontrol->value.enumerated.item[0] > e->max - 1)
3073 return -EINVAL;
3074 mux = ucontrol->value.enumerated.item[0];
3075 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
3076 mask = e->mask << e->shift_l;
3077 if (e->shift_l != e->shift_r) {
3078 if (ucontrol->value.enumerated.item[1] > e->max - 1)
3079 return -EINVAL;
3080 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
3081 mask |= e->mask << e->shift_r;
3082 }
3083
Liam Girdwood3cd04342012-03-09 12:02:08 +00003084 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003085
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02003086 change = snd_soc_test_bits(codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06003087 if (change) {
Lars-Peter Clausence6cfaf2013-07-24 15:27:37 +02003088 update.kcontrol = kcontrol;
3089 update.reg = e->reg;
3090 update.mask = mask;
3091 update.val = val;
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003092 card->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00003093
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003094 soc_dapm_mux_update_power(card, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01003095
Lars-Peter Clausen564c65042013-07-29 17:13:55 +02003096 card->update = NULL;
Stephen Warrenfafd2172011-04-28 17:38:00 -06003097 }
3098
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003099 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00003100 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02003101}
3102EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
3103
3104/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00003105 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3106 *
3107 * @kcontrol: mixer control
3108 * @uinfo: control element information
3109 *
3110 * Callback to provide information about a pin switch control.
3111 */
3112int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3113 struct snd_ctl_elem_info *uinfo)
3114{
3115 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3116 uinfo->count = 1;
3117 uinfo->value.integer.min = 0;
3118 uinfo->value.integer.max = 1;
3119
3120 return 0;
3121}
3122EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3123
3124/**
3125 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3126 *
3127 * @kcontrol: mixer control
3128 * @ucontrol: Value
3129 */
3130int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3131 struct snd_ctl_elem_value *ucontrol)
3132{
Mark Brown48a8c392012-02-14 17:11:15 -08003133 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003134 const char *pin = (const char *)kcontrol->private_value;
3135
Liam Girdwood3cd04342012-03-09 12:02:08 +00003136 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003137
3138 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08003139 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003140
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003141 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003142
3143 return 0;
3144}
3145EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3146
3147/**
3148 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3149 *
3150 * @kcontrol: mixer control
3151 * @ucontrol: Value
3152 */
3153int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3154 struct snd_ctl_elem_value *ucontrol)
3155{
Mark Brown48a8c392012-02-14 17:11:15 -08003156 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003157 const char *pin = (const char *)kcontrol->private_value;
3158
Liam Girdwood3cd04342012-03-09 12:02:08 +00003159 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003160
3161 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08003162 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003163 else
Mark Brown48a8c392012-02-14 17:11:15 -08003164 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003165
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003166 mutex_unlock(&card->dapm_mutex);
3167
Mark Brown48a8c392012-02-14 17:11:15 -08003168 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003169 return 0;
3170}
3171EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3172
Mark Brown5ba06fc2012-02-16 11:07:13 -08003173static struct snd_soc_dapm_widget *
3174snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3175 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003176{
3177 struct snd_soc_dapm_widget *w;
Mark Brown62ea8742012-01-21 21:14:48 +00003178 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003179
3180 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003181 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003182
Mark Brown62ea8742012-01-21 21:14:48 +00003183 switch (w->id) {
3184 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003185 w->regulator = devm_regulator_get(dapm->dev, w->name);
3186 if (IS_ERR(w->regulator)) {
3187 ret = PTR_ERR(w->regulator);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003188 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Mark Brown62ea8742012-01-21 21:14:48 +00003189 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003190 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003191 }
Mark Brown8784c772013-01-10 19:33:47 +00003192
Lars-Peter Clausende9ba982013-07-29 17:14:01 +02003193 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
Mark Brown8784c772013-01-10 19:33:47 +00003194 ret = regulator_allow_bypass(w->regulator, true);
3195 if (ret != 0)
3196 dev_warn(w->dapm->dev,
3197 "ASoC: Failed to unbypass %s: %d\n",
3198 w->name, ret);
3199 }
Mark Brown62ea8742012-01-21 21:14:48 +00003200 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003201 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003202#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003203 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003204 if (IS_ERR(w->clk)) {
3205 ret = PTR_ERR(w->clk);
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003206 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
Ola Liljad7e7eb92012-05-24 15:26:25 +02003207 w->name, ret);
3208 return NULL;
3209 }
Mark Brownec029952012-06-04 08:16:20 +01003210#else
3211 return NULL;
3212#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003213 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003214 default:
3215 break;
3216 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003217
Mark Brown88e8b9a2011-03-02 18:18:24 +00003218 if (dapm->codec && dapm->codec->name_prefix)
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02003219 w->name = kasprintf(GFP_KERNEL, "%s %s",
3220 dapm->codec->name_prefix, widget->name);
3221 else
3222 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3223
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003224 if (w->name == NULL) {
3225 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003226 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003227 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003228
Mark Brown7ca3a182011-10-08 14:04:50 +01003229 switch (w->id) {
3230 case snd_soc_dapm_switch:
3231 case snd_soc_dapm_mixer:
3232 case snd_soc_dapm_mixer_named_ctl:
3233 w->power_check = dapm_generic_check_power;
3234 break;
3235 case snd_soc_dapm_mux:
3236 case snd_soc_dapm_virt_mux:
3237 case snd_soc_dapm_value_mux:
3238 w->power_check = dapm_generic_check_power;
3239 break;
Mark Brown46162742013-06-05 19:36:11 +01003240 case snd_soc_dapm_dai_out:
Mark Brown7ca3a182011-10-08 14:04:50 +01003241 w->power_check = dapm_adc_check_power;
3242 break;
Mark Brown46162742013-06-05 19:36:11 +01003243 case snd_soc_dapm_dai_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003244 w->power_check = dapm_dac_check_power;
3245 break;
Mark Brown63c69a62013-07-18 22:03:01 +01003246 case snd_soc_dapm_adc:
3247 case snd_soc_dapm_aif_out:
3248 case snd_soc_dapm_dac:
3249 case snd_soc_dapm_aif_in:
Mark Brown7ca3a182011-10-08 14:04:50 +01003250 case snd_soc_dapm_pga:
3251 case snd_soc_dapm_out_drv:
3252 case snd_soc_dapm_input:
3253 case snd_soc_dapm_output:
3254 case snd_soc_dapm_micbias:
3255 case snd_soc_dapm_spk:
3256 case snd_soc_dapm_hp:
3257 case snd_soc_dapm_mic:
3258 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003259 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003260 w->power_check = dapm_generic_check_power;
3261 break;
3262 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003263 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003264 case snd_soc_dapm_clock_supply:
Lars-Peter Clausen57295072013-08-05 11:27:31 +02003265 case snd_soc_dapm_kcontrol:
Mark Brown7ca3a182011-10-08 14:04:50 +01003266 w->power_check = dapm_supply_check_power;
3267 break;
3268 default:
3269 w->power_check = dapm_always_on_check_power;
3270 break;
3271 }
3272
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003273 w->dapm = dapm;
3274 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003275 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003276 INIT_LIST_HEAD(&w->sources);
3277 INIT_LIST_HEAD(&w->sinks);
3278 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003279 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003280 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003281
3282 /* machine layer set ups unconnected pins and insertions */
3283 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003284 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003285}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003286
3287/**
Mark Brown4ba13272008-05-13 14:51:19 +02003288 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003289 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003290 * @widget: widget array
3291 * @num: number of widgets
3292 *
3293 * Creates new DAPM controls based upon the templates.
3294 *
3295 * Returns 0 for success else error.
3296 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003297int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003298 const struct snd_soc_dapm_widget *widget,
3299 int num)
3300{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003301 struct snd_soc_dapm_widget *w;
3302 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003303 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003304
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003305 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003306 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003307 w = snd_soc_dapm_new_control(dapm, widget);
3308 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003309 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003310 "ASoC: Failed to create DAPM control %s\n",
3311 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003312 ret = -ENOMEM;
3313 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003314 }
Mark Brown4ba13272008-05-13 14:51:19 +02003315 widget++;
3316 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003317 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003318 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003319}
3320EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3321
Mark Brownc74184e2012-04-04 22:12:09 +01003322static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3323 struct snd_kcontrol *kcontrol, int event)
3324{
3325 struct snd_soc_dapm_path *source_p, *sink_p;
3326 struct snd_soc_dai *source, *sink;
3327 const struct snd_soc_pcm_stream *config = w->params;
3328 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003329 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003330 u64 fmt;
3331 int ret;
3332
3333 BUG_ON(!config);
3334 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3335
3336 /* We only support a single source and sink, pick the first */
3337 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3338 list_sink);
3339 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3340 list_source);
3341
3342 BUG_ON(!source_p || !sink_p);
3343 BUG_ON(!sink_p->source || !source_p->sink);
3344 BUG_ON(!source_p->source || !sink_p->sink);
3345
3346 source = source_p->source->priv;
3347 sink = sink_p->sink->priv;
3348
3349 /* Be a little careful as we don't want to overflow the mask array */
3350 if (config->formats) {
3351 fmt = ffs(config->formats) - 1;
3352 } else {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003353 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003354 config->formats);
3355 fmt = 0;
3356 }
3357
3358 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003359 params = kzalloc(sizeof(*params), GFP_KERNEL);
3360 if (!params) {
3361 ret = -ENOMEM;
3362 goto out;
3363 }
3364 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003365
Mark Brown9747cec2012-04-26 19:12:21 +01003366 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003367 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003368 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003369 config->rate_max;
3370
Mark Brown9747cec2012-04-26 19:12:21 +01003371 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003372 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003373 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003374 = config->channels_max;
3375
3376 memset(&substream, 0, sizeof(substream));
3377
3378 switch (event) {
3379 case SND_SOC_DAPM_PRE_PMU:
3380 if (source->driver->ops && source->driver->ops->hw_params) {
3381 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3382 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003383 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003384 if (ret != 0) {
3385 dev_err(source->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003386 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003387 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003388 }
3389 }
3390
3391 if (sink->driver->ops && sink->driver->ops->hw_params) {
3392 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003393 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003394 sink);
3395 if (ret != 0) {
3396 dev_err(sink->dev,
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003397 "ASoC: hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003398 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003399 }
3400 }
3401 break;
3402
3403 case SND_SOC_DAPM_POST_PMU:
Mark Brownda183962013-02-06 15:44:07 +00003404 ret = snd_soc_dai_digital_mute(sink, 0,
3405 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003406 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003407 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003408 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003409 break;
3410
3411 case SND_SOC_DAPM_PRE_PMD:
Mark Brownda183962013-02-06 15:44:07 +00003412 ret = snd_soc_dai_digital_mute(sink, 1,
3413 SNDRV_PCM_STREAM_PLAYBACK);
Mark Brownc74184e2012-04-04 22:12:09 +01003414 if (ret != 0 && ret != -ENOTSUPP)
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003415 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003416 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003417 break;
3418
3419 default:
3420 BUG();
3421 return -EINVAL;
3422 }
3423
Mark Brown9747cec2012-04-26 19:12:21 +01003424out:
3425 kfree(params);
3426 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003427}
3428
3429int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3430 const struct snd_soc_pcm_stream *params,
3431 struct snd_soc_dapm_widget *source,
3432 struct snd_soc_dapm_widget *sink)
3433{
3434 struct snd_soc_dapm_route routes[2];
3435 struct snd_soc_dapm_widget template;
3436 struct snd_soc_dapm_widget *w;
3437 size_t len;
3438 char *link_name;
3439
3440 len = strlen(source->name) + strlen(sink->name) + 2;
3441 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3442 if (!link_name)
3443 return -ENOMEM;
3444 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3445
3446 memset(&template, 0, sizeof(template));
3447 template.reg = SND_SOC_NOPM;
3448 template.id = snd_soc_dapm_dai_link;
3449 template.name = link_name;
3450 template.event = snd_soc_dai_link_event;
3451 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3452 SND_SOC_DAPM_PRE_PMD;
3453
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003454 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
Mark Brownc74184e2012-04-04 22:12:09 +01003455
3456 w = snd_soc_dapm_new_control(&card->dapm, &template);
3457 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003458 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003459 link_name);
3460 return -ENOMEM;
3461 }
3462
3463 w->params = params;
3464
3465 memset(&routes, 0, sizeof(routes));
3466
3467 routes[0].source = source->name;
3468 routes[0].sink = link_name;
3469 routes[1].source = link_name;
3470 routes[1].sink = sink->name;
3471
3472 return snd_soc_dapm_add_routes(&card->dapm, routes,
3473 ARRAY_SIZE(routes));
3474}
3475
Mark Brown888df392012-02-16 19:37:51 -08003476int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3477 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003478{
Mark Brown888df392012-02-16 19:37:51 -08003479 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003480 struct snd_soc_dapm_widget *w;
3481
Mark Brown888df392012-02-16 19:37:51 -08003482 WARN_ON(dapm->dev != dai->dev);
3483
3484 memset(&template, 0, sizeof(template));
3485 template.reg = SND_SOC_NOPM;
3486
3487 if (dai->driver->playback.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003488 template.id = snd_soc_dapm_dai_in;
Mark Brown888df392012-02-16 19:37:51 -08003489 template.name = dai->driver->playback.stream_name;
3490 template.sname = dai->driver->playback.stream_name;
3491
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003492 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003493 template.name);
3494
3495 w = snd_soc_dapm_new_control(dapm, &template);
3496 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003497 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003498 dai->driver->playback.stream_name);
3499 }
3500
3501 w->priv = dai;
3502 dai->playback_widget = w;
3503 }
3504
3505 if (dai->driver->capture.stream_name) {
Mark Brown46162742013-06-05 19:36:11 +01003506 template.id = snd_soc_dapm_dai_out;
Mark Brown888df392012-02-16 19:37:51 -08003507 template.name = dai->driver->capture.stream_name;
3508 template.sname = dai->driver->capture.stream_name;
3509
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003510 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003511 template.name);
3512
3513 w = snd_soc_dapm_new_control(dapm, &template);
3514 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003515 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
Mark Brown888df392012-02-16 19:37:51 -08003516 dai->driver->capture.stream_name);
3517 }
3518
3519 w->priv = dai;
3520 dai->capture_widget = w;
3521 }
3522
3523 return 0;
3524}
3525
3526int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3527{
3528 struct snd_soc_dapm_widget *dai_w, *w;
3529 struct snd_soc_dai *dai;
Mark Brown888df392012-02-16 19:37:51 -08003530
3531 /* For each DAI widget... */
3532 list_for_each_entry(dai_w, &card->widgets, list) {
Mark Brown46162742013-06-05 19:36:11 +01003533 switch (dai_w->id) {
3534 case snd_soc_dapm_dai_in:
3535 case snd_soc_dapm_dai_out:
3536 break;
3537 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003538 continue;
Mark Brown46162742013-06-05 19:36:11 +01003539 }
Mark Brown888df392012-02-16 19:37:51 -08003540
3541 dai = dai_w->priv;
3542
3543 /* ...find all widgets with the same stream and link them */
3544 list_for_each_entry(w, &card->widgets, list) {
3545 if (w->dapm != dai_w->dapm)
3546 continue;
3547
Mark Brown46162742013-06-05 19:36:11 +01003548 switch (w->id) {
3549 case snd_soc_dapm_dai_in:
3550 case snd_soc_dapm_dai_out:
Mark Brown888df392012-02-16 19:37:51 -08003551 continue;
Mark Brown46162742013-06-05 19:36:11 +01003552 default:
3553 break;
3554 }
Mark Brown888df392012-02-16 19:37:51 -08003555
Russell King19c2c5f2013-08-04 20:24:03 +01003556 if (!w->sname || !strstr(w->sname, dai_w->name))
Mark Brown888df392012-02-16 19:37:51 -08003557 continue;
3558
3559 if (dai->driver->playback.stream_name &&
3560 strstr(w->sname,
3561 dai->driver->playback.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003562 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003563 dai->playback_widget->name, w->name);
Mark Brown888df392012-02-16 19:37:51 -08003564
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003565 snd_soc_dapm_add_path(w->dapm,
3566 dai->playback_widget, w, NULL, NULL);
Mark Brown888df392012-02-16 19:37:51 -08003567 }
3568
3569 if (dai->driver->capture.stream_name &&
3570 strstr(w->sname,
3571 dai->driver->capture.stream_name)) {
Mark Brown888df392012-02-16 19:37:51 -08003572 dev_dbg(dai->dev, "%s -> %s\n",
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003573 w->name, dai->capture_widget->name);
Mark Brown888df392012-02-16 19:37:51 -08003574
Lars-Peter Clausen25536282013-07-29 17:14:02 +02003575 snd_soc_dapm_add_path(w->dapm, w,
3576 dai->capture_widget, NULL, NULL);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003577 }
3578 }
3579 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003580
Mark Brown888df392012-02-16 19:37:51 -08003581 return 0;
3582}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003583
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003584static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3585 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003586{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003587
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003588 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3589 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3590 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003591
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003592 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3593 w_cpu = cpu_dai->playback_widget;
3594 w_codec = codec_dai->playback_widget;
3595 } else {
3596 w_cpu = cpu_dai->capture_widget;
3597 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003598 }
3599
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003600 if (w_cpu) {
3601
3602 dapm_mark_dirty(w_cpu, "stream event");
3603
3604 switch (event) {
3605 case SND_SOC_DAPM_STREAM_START:
3606 w_cpu->active = 1;
3607 break;
3608 case SND_SOC_DAPM_STREAM_STOP:
3609 w_cpu->active = 0;
3610 break;
3611 case SND_SOC_DAPM_STREAM_SUSPEND:
3612 case SND_SOC_DAPM_STREAM_RESUME:
3613 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3614 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3615 break;
3616 }
3617 }
3618
3619 if (w_codec) {
3620
3621 dapm_mark_dirty(w_codec, "stream event");
3622
3623 switch (event) {
3624 case SND_SOC_DAPM_STREAM_START:
3625 w_codec->active = 1;
3626 break;
3627 case SND_SOC_DAPM_STREAM_STOP:
3628 w_codec->active = 0;
3629 break;
3630 case SND_SOC_DAPM_STREAM_SUSPEND:
3631 case SND_SOC_DAPM_STREAM_RESUME:
3632 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3633 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3634 break;
3635 }
3636 }
3637
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003638 dapm_power_widgets(rtd->card, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003639}
3640
3641/**
3642 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3643 * @rtd: PCM runtime data
3644 * @stream: stream name
3645 * @event: stream event
3646 *
3647 * Sends a stream event to the dapm core. The core then makes any
3648 * necessary widget power changes.
3649 *
3650 * Returns 0 for success else error.
3651 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003652void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3653 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003654{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003655 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003656
Liam Girdwood3cd04342012-03-09 12:02:08 +00003657 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003658 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003659 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003660}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003661
3662/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003663 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003664 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003665 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003666 *
Mark Brown74b8f952009-06-06 11:26:15 +01003667 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003668 * a valid audio route and active audio stream.
3669 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3670 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003671 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003672int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003673{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003674 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003675}
Liam Girdwooda5302182008-07-07 13:35:17 +01003676EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003677
3678/**
Mark Brownda341832010-03-15 19:23:37 +00003679 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003680 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003681 * @pin: pin name
3682 *
3683 * Enables input/output pin regardless of any other state. This is
3684 * intended for use with microphone bias supplies used in microphone
3685 * jack detection.
3686 *
3687 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3688 * do any widget power switching.
3689 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003690int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3691 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003692{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003693 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003694
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003695 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003696 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003697 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003698 }
3699
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003700 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003701 w->connected = 1;
3702 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003703 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003704
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003705 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003706}
3707EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3708
3709/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003710 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003711 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003712 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003713 *
Mark Brown74b8f952009-06-06 11:26:15 +01003714 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003715 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3716 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003717 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003718int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3719 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003720{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003721 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003722}
3723EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3724
3725/**
Mark Brown5817b522008-09-24 11:23:11 +01003726 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003727 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003728 * @pin: pin name
3729 *
3730 * Marks the specified pin as being not connected, disabling it along
3731 * any parent or child widgets. At present this is identical to
3732 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3733 * additional things such as disabling controls which only affect
3734 * paths through the pin.
3735 *
3736 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3737 * do any widget power switching.
3738 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003739int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003740{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003741 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003742}
3743EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3744
3745/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003746 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003747 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003748 * @pin: audio signal pin endpoint (or start point)
3749 *
3750 * Get audio pin status - connected or disconnected.
3751 *
3752 * Returns 1 for connected otherwise 0.
3753 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003754int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3755 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003756{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003757 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003758
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003759 if (w)
3760 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003761
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003762 return 0;
3763}
Liam Girdwooda5302182008-07-07 13:35:17 +01003764EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003765
3766/**
Mark Brown1547aba2010-05-07 21:11:40 +01003767 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003768 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003769 * @pin: audio signal pin endpoint (or start point)
3770 *
3771 * Mark the given endpoint or pin as ignoring suspend. When the
3772 * system is disabled a path between two endpoints flagged as ignoring
3773 * suspend will not be disabled. The path must already be enabled via
3774 * normal means at suspend time, it will not be turned on if it was not
3775 * already enabled.
3776 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003777int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3778 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003779{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003780 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003781
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003782 if (!w) {
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003783 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003784 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003785 }
3786
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003787 w->ignore_suspend = 1;
3788
3789 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003790}
3791EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3792
Stephen Warren16332812011-11-23 12:42:04 -07003793static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3794 struct snd_soc_dapm_widget *w)
3795{
3796 struct snd_soc_dapm_path *p;
3797
3798 list_for_each_entry(p, &card->paths, list) {
3799 if ((p->source == w) || (p->sink == w)) {
3800 dev_dbg(card->dev,
3801 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3802 p->source->name, p->source->id, p->source->dapm,
3803 p->sink->name, p->sink->id, p->sink->dapm);
3804
3805 /* Connected to something other than the codec */
3806 if (p->source->dapm != p->sink->dapm)
3807 return true;
3808 /*
3809 * Loopback connection from codec external pin to
3810 * codec external pin
3811 */
3812 if (p->sink->id == snd_soc_dapm_input) {
3813 switch (p->source->id) {
3814 case snd_soc_dapm_output:
3815 case snd_soc_dapm_micbias:
3816 return true;
3817 default:
3818 break;
3819 }
3820 }
3821 }
3822 }
3823
3824 return false;
3825}
3826
3827/**
3828 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3829 * @codec: The codec whose pins should be processed
3830 *
3831 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3832 * which are unused. Pins are used if they are connected externally to the
3833 * codec, whether that be to some other device, or a loop-back connection to
3834 * the codec itself.
3835 */
3836void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3837{
3838 struct snd_soc_card *card = codec->card;
3839 struct snd_soc_dapm_context *dapm = &codec->dapm;
3840 struct snd_soc_dapm_widget *w;
3841
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003842 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003843 &card->dapm, &codec->dapm);
3844
3845 list_for_each_entry(w, &card->widgets, list) {
3846 if (w->dapm != dapm)
3847 continue;
3848 switch (w->id) {
3849 case snd_soc_dapm_input:
3850 case snd_soc_dapm_output:
3851 case snd_soc_dapm_micbias:
Liam Girdwood30a6a1a2012-11-19 14:39:12 +00003852 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003853 w->name);
3854 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003855 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003856 "... Not in map; disabling\n");
3857 snd_soc_dapm_nc_pin(dapm, w->name);
3858 }
3859 break;
3860 default:
3861 break;
3862 }
3863 }
3864}
3865
Mark Brown1547aba2010-05-07 21:11:40 +01003866/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003867 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003868 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003869 *
3870 * Free all dapm widgets and resources.
3871 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003872void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003873{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003874 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003875 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003876 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003877 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003878}
3879EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3880
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003881static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003882{
Liam Girdwood01005a72012-07-06 16:57:05 +01003883 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003884 struct snd_soc_dapm_widget *w;
3885 LIST_HEAD(down_list);
3886 int powerdown = 0;
3887
Liam Girdwood01005a72012-07-06 16:57:05 +01003888 mutex_lock(&card->dapm_mutex);
3889
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003890 list_for_each_entry(w, &dapm->card->widgets, list) {
3891 if (w->dapm != dapm)
3892 continue;
Mark Brown51737472009-06-22 13:16:51 +01003893 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003894 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003895 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003896 powerdown = 1;
3897 }
3898 }
3899
3900 /* If there were no widgets to power down we're already in
3901 * standby.
3902 */
3903 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003904 if (dapm->bias_level == SND_SOC_BIAS_ON)
3905 snd_soc_dapm_set_bias_level(dapm,
3906 SND_SOC_BIAS_PREPARE);
Lars-Peter Clausen95dd5cd2013-07-29 17:13:56 +02003907 dapm_seq_run(card, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003908 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3909 snd_soc_dapm_set_bias_level(dapm,
3910 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003911 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003912
3913 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003914}
Mark Brown51737472009-06-22 13:16:51 +01003915
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003916/*
3917 * snd_soc_dapm_shutdown - callback for system shutdown
3918 */
3919void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3920{
3921 struct snd_soc_codec *codec;
3922
Misael Lopez Cruz445632a2012-11-08 12:03:12 -06003923 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003924 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003925 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3926 snd_soc_dapm_set_bias_level(&codec->dapm,
3927 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003928 }
Mark Brown51737472009-06-22 13:16:51 +01003929}
3930
Richard Purdie2b97eab2006-10-06 18:32:18 +02003931/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003932MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003933MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3934MODULE_LICENSE("GPL");