blob: 2f9e31913b936b029f9da32ba048e60e12f1e8ae [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
17 * mic/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +020021 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
24 * Todo:
25 * o DAPM power change sequencing - allow for configurable per
26 * codec sequences.
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
30 */
31
32#include <linux/module.h>
33#include <linux/moduleparam.h>
34#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080035#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020036#include <linux/delay.h>
37#include <linux/pm.h>
38#include <linux/bitops.h>
39#include <linux/platform_device.h>
40#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020041#include <linux/debugfs.h>
Mark Brownf1aac482011-12-05 15:17:06 +000042#include <linux/pm_runtime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020044#include <sound/core.h>
45#include <sound/pcm.h>
46#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020047#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020048#include <sound/initval.h>
49
Mark Brown84e90932010-11-04 00:07:02 -040050#include <trace/events/asoc.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070051int soc_dpcm_runtime_update(struct snd_soc_dapm_widget *);
Mark Brown84e90932010-11-04 00:07:02 -040052
Mark Brownde02d072011-09-20 21:43:24 +010053#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
54
Richard Purdie2b97eab2006-10-06 18:32:18 +020055/* dapm power sequences - make this per codec in the future */
56static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010057 [snd_soc_dapm_pre] = 0,
58 [snd_soc_dapm_supply] = 1,
59 [snd_soc_dapm_micbias] = 2,
Kiran Kandid8cf5212012-03-02 15:34:53 -080060 [snd_soc_dapm_adc] = 3,
Mark Brown010ff262009-08-17 17:39:22 +010061 [snd_soc_dapm_mic] = 4,
62 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000063 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010064 [snd_soc_dapm_value_mux] = 5,
65 [snd_soc_dapm_dac] = 6,
66 [snd_soc_dapm_mixer] = 7,
67 [snd_soc_dapm_mixer_named_ctl] = 7,
68 [snd_soc_dapm_pga] = 8,
Bharath Ramachandramurthy9c79f132011-11-28 11:18:57 -080069 [snd_soc_dapm_aif_in] = 8,
70 [snd_soc_dapm_aif_out] = 8,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060071 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010072 [snd_soc_dapm_hp] = 10,
73 [snd_soc_dapm_spk] = 10,
74 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020075};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000076
Richard Purdie2b97eab2006-10-06 18:32:18 +020077static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010078 [snd_soc_dapm_pre] = 0,
Bharath Ramachandramurthy9c79f132011-11-28 11:18:57 -080079 [snd_soc_dapm_aif_in] = 1,
80 [snd_soc_dapm_aif_out] = 1,
Damir Didjustoc6f83cb2012-12-03 00:54:14 -080081 [snd_soc_dapm_adc] = 5,
Mark Brown38357ab2009-06-06 19:03:23 +010082 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010083 [snd_soc_dapm_spk] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060084 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010085 [snd_soc_dapm_pga] = 4,
86 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010087 [snd_soc_dapm_mixer] = 5,
88 [snd_soc_dapm_dac] = 6,
89 [snd_soc_dapm_mic] = 7,
90 [snd_soc_dapm_micbias] = 8,
91 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000092 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010093 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010094 [snd_soc_dapm_supply] = 11,
95 [snd_soc_dapm_post] = 12,
Richard Purdie2b97eab2006-10-06 18:32:18 +020096};
97
Troy Kisky12ef1932008-10-13 17:42:14 -070098static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +010099{
100 if (pop_time)
101 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
102}
103
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200104static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100105{
106 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200107 char *buf;
108
109 if (!pop_time)
110 return;
111
112 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
113 if (buf == NULL)
114 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100115
116 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200117 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100118 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100119 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200120
121 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100122}
123
Mark Browndb432b42011-10-03 21:06:40 +0100124static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
125{
126 return !list_empty(&w->dirty);
127}
128
Mark Brown25c77c52011-10-08 13:36:03 +0100129void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100130{
Mark Brown75c1f892011-10-04 22:28:08 +0100131 if (!dapm_dirty_widget(w)) {
132 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
133 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100134 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100135 }
Mark Browndb432b42011-10-03 21:06:40 +0100136}
Mark Brown25c77c52011-10-08 13:36:03 +0100137EXPORT_SYMBOL_GPL(dapm_mark_dirty);
Mark Browndb432b42011-10-03 21:06:40 +0100138
Richard Purdie2b97eab2006-10-06 18:32:18 +0200139/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100140static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200141 const struct snd_soc_dapm_widget *_widget)
142{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100143 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200144}
145
Liam Girdwood48056082011-07-20 12:23:33 +0100146/* get snd_card from DAPM context */
147static inline struct snd_card *dapm_get_snd_card(
148 struct snd_soc_dapm_context *dapm)
149{
150 if (dapm->codec)
151 return dapm->codec->card->snd_card;
152 else if (dapm->platform)
153 return dapm->platform->card->snd_card;
154 else
155 BUG();
156
157 /* unreachable */
158 return NULL;
159}
160
161/* get soc_card from DAPM context */
162static inline struct snd_soc_card *dapm_get_soc_card(
163 struct snd_soc_dapm_context *dapm)
164{
165 if (dapm->codec)
166 return dapm->codec->card;
167 else if (dapm->platform)
168 return dapm->platform->card;
169 else
170 BUG();
171
172 /* unreachable */
173 return NULL;
174}
175
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100176static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
177{
178 if (w->codec)
179 return snd_soc_read(w->codec, reg);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100180 else if (w->platform)
181 return snd_soc_platform_read(w->platform, reg);
182
183 dev_err(w->dapm->dev, "no valid widget read method\n");
184 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100185}
186
187static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
188{
189 if (w->codec)
190 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100191 else if (w->platform)
192 return snd_soc_platform_write(w->platform, reg, val);
193
194 dev_err(w->dapm->dev, "no valid widget write method\n");
195 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100196}
197
198static int soc_widget_update_bits(struct snd_soc_dapm_widget *w,
199 unsigned short reg, unsigned int mask, unsigned int value)
200{
Mark Brown8a713da2011-12-03 12:33:55 +0000201 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100202 unsigned int old, new;
203 int ret;
204
Mark Brown8a713da2011-12-03 12:33:55 +0000205 if (w->codec && w->codec->using_regmap) {
206 ret = regmap_update_bits_check(w->codec->control_data,
207 reg, mask, value, &change);
208 if (ret != 0)
209 return ret;
210 } else {
211 ret = soc_widget_read(w, reg);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100212 if (ret < 0)
213 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +0000214
215 old = ret;
216 new = (old & ~mask) | (value & mask);
217 change = old != new;
218 if (change) {
219 ret = soc_widget_write(w, reg, new);
Mark Brown80f48142012-03-12 10:33:34 +0000220 if (ret < 0)
Mark Brown8a713da2011-12-03 12:33:55 +0000221 return ret;
222 }
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100223 }
224
225 return change;
226}
227
Mark Brown452c5ea2009-05-17 21:41:23 +0100228/**
229 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800230 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100231 * @level: level to configure
232 *
233 * Configure the bias (power) levels for the SoC audio device.
234 *
235 * Returns 0 for success else error.
236 */
Mark Browned5a4c42011-02-18 11:12:42 -0800237static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200238 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100239{
Mark Browned5a4c42011-02-18 11:12:42 -0800240 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100241 int ret = 0;
242
Mark Brown84e90932010-11-04 00:07:02 -0400243 trace_snd_soc_bias_level_start(card, level);
244
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000245 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100246 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100247 if (ret != 0)
248 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100249
Mark Browncc4c6702011-06-06 19:03:34 +0100250 if (dapm->codec) {
251 if (dapm->codec->driver->set_bias_level)
252 ret = dapm->codec->driver->set_bias_level(dapm->codec,
253 level);
254 else
255 dapm->bias_level = level;
256 }
Mark Brown171ec6b2011-06-06 18:15:19 +0100257 if (ret != 0)
258 goto out;
259
260 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100261 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100262out:
Mark Brown84e90932010-11-04 00:07:02 -0400263 trace_snd_soc_bias_level_done(card, level);
264
Mark Brown452c5ea2009-05-17 21:41:23 +0100265 return ret;
266}
267
Richard Purdie2b97eab2006-10-06 18:32:18 +0200268/* set up initial codec paths */
269static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
270 struct snd_soc_dapm_path *p, int i)
271{
272 switch (w->id) {
273 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000274 case snd_soc_dapm_mixer:
275 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200276 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100277 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600278 w->kcontrol_news[i].private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -0400279 unsigned int reg = mc->reg;
280 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100281 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -0400282 unsigned int mask = (1 << fls(max)) - 1;
283 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200284
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100285 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200286 val = (val >> shift) & mask;
287
288 if ((invert && !val) || (!invert && val))
289 p->connect = 1;
290 else
291 p->connect = 0;
292 }
293 break;
294 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600295 struct soc_enum *e = (struct soc_enum *)
296 w->kcontrol_news[i].private_value;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200297 int val, item, bitmask;
298
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100299 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Mark Brown88d96082011-06-06 16:16:34 +0100300 ;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100301 val = soc_widget_read(w, e->reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200302 item = (val >> e->shift_l) & (bitmask - 1);
303
304 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100305 for (i = 0; i < e->max; i++) {
Liam Girdwooda8f13d82011-05-18 18:39:21 +0100306 if (!(strcmp(p->name, snd_soc_get_enum_text(e, i))) && item == i)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200307 p->connect = 1;
308 }
309 }
310 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000311 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600312 struct soc_enum *e = (struct soc_enum *)
313 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000314
315 p->connect = 0;
316 /* since a virtual mux has no backing registers to
317 * decide which path to connect, it will try to match
318 * with the first enumeration. This is to ensure
319 * that the default mux choice (the first) will be
320 * correctly powered up during initialization.
321 */
Liam Girdwooda8f13d82011-05-18 18:39:21 +0100322 if (!strcmp(p->name, snd_soc_get_enum_text(e, 0)))
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000323 p->connect = 1;
324 }
325 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200326 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200327 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600328 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200329 int val, item;
330
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100331 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200332 val = (val >> e->shift_l) & e->mask;
333 for (item = 0; item < e->max; item++) {
334 if (val == e->values[item])
335 break;
336 }
337
338 p->connect = 0;
339 for (i = 0; i < e->max; i++) {
Liam Girdwooda8f13d82011-05-18 18:39:21 +0100340 if (!(strcmp(p->name, snd_soc_get_enum_text(e, i))) && item == i)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200341 p->connect = 1;
342 }
343 }
344 break;
Mark Brown56563102011-10-03 22:41:09 +0100345 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200346 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600347 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200348 case snd_soc_dapm_output:
349 case snd_soc_dapm_adc:
350 case snd_soc_dapm_input:
Mark Brown1ab97c82011-11-27 16:21:51 +0000351 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200352 case snd_soc_dapm_dac:
353 case snd_soc_dapm_micbias:
354 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100355 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100356 case snd_soc_dapm_aif_in:
357 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200358 case snd_soc_dapm_hp:
359 case snd_soc_dapm_mic:
360 case snd_soc_dapm_spk:
361 case snd_soc_dapm_line:
Mark Brown56563102011-10-03 22:41:09 +0100362 p->connect = 1;
363 break;
364 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200365 case snd_soc_dapm_pre:
366 case snd_soc_dapm_post:
367 p->connect = 0;
368 break;
369 }
370}
371
Mark Brown74b8f952009-06-06 11:26:15 +0100372/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200373static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200374 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
375 struct snd_soc_dapm_path *path, const char *control_name,
376 const struct snd_kcontrol_new *kcontrol)
377{
378 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
379 int i;
380
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100381 for (i = 0; i < e->max; i++) {
Liam Girdwooda8f13d82011-05-18 18:39:21 +0100382 if (!(strcmp(control_name, snd_soc_get_enum_text(e, i)))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200383 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200384 list_add(&path->list_sink, &dest->sources);
385 list_add(&path->list_source, &src->sinks);
Liam Girdwooda8f13d82011-05-18 18:39:21 +0100386 path->name = (char*)snd_soc_get_enum_text(e, i);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200387 dapm_set_path_status(dest, path, 0);
388 return 0;
389 }
390 }
391
392 return -ENODEV;
393}
394
Mark Brown74b8f952009-06-06 11:26:15 +0100395/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200396static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200397 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
398 struct snd_soc_dapm_path *path, const char *control_name)
399{
400 int i;
401
402 /* search for mixer kcontrol */
403 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600404 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200405 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200406 list_add(&path->list_sink, &dest->sources);
407 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600408 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200409 dapm_set_path_status(dest, path, i);
410 return 0;
411 }
412 }
413 return -ENODEV;
414}
415
Stephen Warrenaf468002011-04-28 17:38:01 -0600416static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600417 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600418 const struct snd_kcontrol_new *kcontrol_new,
419 struct snd_kcontrol **kcontrol)
420{
421 struct snd_soc_dapm_widget *w;
422 int i;
423
424 *kcontrol = NULL;
425
426 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600427 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
428 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600429 for (i = 0; i < w->num_kcontrols; i++) {
430 if (&w->kcontrol_news[i] == kcontrol_new) {
431 if (w->kcontrols)
432 *kcontrol = w->kcontrols[i];
433 return 1;
434 }
435 }
436 }
437
438 return 0;
439}
440
Richard Purdie2b97eab2006-10-06 18:32:18 +0200441/* create new dapm mixer control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200442static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200443{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200444 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200445 int i, ret = 0;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000446 size_t name_len, prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200447 struct snd_soc_dapm_path *path;
Mark Brown12ea2c72011-03-02 18:17:32 +0000448 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000449 const char *prefix;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600450 struct snd_soc_dapm_widget_list *wlist;
451 size_t wlistsize;
Mark Brownefb7ac32011-03-08 17:23:24 +0000452
453 if (dapm->codec)
454 prefix = dapm->codec->name_prefix;
455 else
456 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200457
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000458 if (prefix)
459 prefix_len = strlen(prefix) + 1;
460 else
461 prefix_len = 0;
462
Richard Purdie2b97eab2006-10-06 18:32:18 +0200463 /* add kcontrol */
464 for (i = 0; i < w->num_kcontrols; i++) {
465
466 /* match name */
467 list_for_each_entry(path, &w->sources, list_sink) {
468
469 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600470 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200471 continue;
472
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200473 if (w->kcontrols[i]) {
474 path->kcontrol = w->kcontrols[i];
475 continue;
476 }
477
Stephen Warrenfafd2172011-04-28 17:38:00 -0600478 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
479 sizeof(struct snd_soc_dapm_widget *),
480 wlist = kzalloc(wlistsize, GFP_KERNEL);
481 if (wlist == NULL) {
482 dev_err(dapm->dev,
483 "asoc: can't allocate widget list for %s\n",
484 w->name);
485 return -ENOMEM;
486 }
487 wlist->num_widgets = 1;
488 wlist->widgets[0] = w;
489
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000490 /* add dapm control with long name.
491 * for dapm_mixer this is the concatenation of the
492 * mixer and kcontrol name.
493 * for dapm_mixer_named_ctl this is simply the
494 * kcontrol name.
495 */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600496 name_len = strlen(w->kcontrol_news[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000497 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000498 name_len += 1 + strlen(w->name);
499
Mark Brown219b93f2008-10-28 13:02:31 +0000500 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000501
Stephen Warrenfafd2172011-04-28 17:38:00 -0600502 if (path->long_name == NULL) {
503 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200504 return -ENOMEM;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600505 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200506
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000507 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000508 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000509 /* The control will get a prefix from
510 * the control creation process but
511 * we're also using the same prefix
512 * for widgets so cut the prefix off
513 * the front of the widget name.
514 */
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000515 snprintf(path->long_name, name_len, "%s %s",
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000516 w->name + prefix_len,
Stephen Warren82cfecd2011-04-28 17:37:58 -0600517 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000518 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000519 case snd_soc_dapm_mixer_named_ctl:
520 snprintf(path->long_name, name_len, "%s",
Stephen Warren82cfecd2011-04-28 17:37:58 -0600521 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000522 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000523 }
524
Mark Brown219b93f2008-10-28 13:02:31 +0000525 path->long_name[name_len - 1] = '\0';
526
Stephen Warrenfafd2172011-04-28 17:38:00 -0600527 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
528 wlist, path->long_name,
529 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200530 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200531 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200532 dev_err(dapm->dev,
533 "asoc: failed to add dapm kcontrol %s: %d\n",
534 path->long_name, ret);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600535 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200536 kfree(path->long_name);
537 path->long_name = NULL;
538 return ret;
539 }
Stephen Warrenfad59882011-04-28 17:37:59 -0600540 w->kcontrols[i] = path->kcontrol;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200541 }
542 }
543 return ret;
544}
545
546/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200547static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200548{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200549 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200550 struct snd_soc_dapm_path *path = NULL;
551 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000552 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000553 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000554 size_t prefix_len;
Stephen Warrenaf468002011-04-28 17:38:01 -0600555 int ret;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600556 struct snd_soc_dapm_widget_list *wlist;
Stephen Warrenaf468002011-04-28 17:38:01 -0600557 int shared, wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600558 size_t wlistsize;
Stephen Warrenaf468002011-04-28 17:38:01 -0600559 char *name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200560
Stephen Warrenaf468002011-04-28 17:38:01 -0600561 if (w->num_kcontrols != 1) {
562 dev_err(dapm->dev,
563 "asoc: mux %s has incorrect number of controls\n",
564 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200565 return -EINVAL;
566 }
567
Stephen Warren1007da02011-05-26 09:57:33 -0600568 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
Stephen Warrenaf468002011-04-28 17:38:01 -0600569 &kcontrol);
570 if (kcontrol) {
571 wlist = kcontrol->private_data;
572 wlistentries = wlist->num_widgets + 1;
573 } else {
574 wlist = NULL;
575 wlistentries = 1;
576 }
Stephen Warrenfafd2172011-04-28 17:38:00 -0600577 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
Stephen Warrenaf468002011-04-28 17:38:01 -0600578 wlistentries * sizeof(struct snd_soc_dapm_widget *),
579 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600580 if (wlist == NULL) {
581 dev_err(dapm->dev,
582 "asoc: can't allocate widget list for %s\n", w->name);
583 return -ENOMEM;
584 }
Stephen Warrenaf468002011-04-28 17:38:01 -0600585 wlist->num_widgets = wlistentries;
586 wlist->widgets[wlistentries - 1] = w;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600587
Stephen Warrenaf468002011-04-28 17:38:01 -0600588 if (!kcontrol) {
589 if (dapm->codec)
590 prefix = dapm->codec->name_prefix;
591 else
592 prefix = NULL;
Mark Brownefb7ac32011-03-08 17:23:24 +0000593
Stephen Warrenaf468002011-04-28 17:38:01 -0600594 if (shared) {
595 name = w->kcontrol_news[0].name;
596 prefix_len = 0;
597 } else {
598 name = w->name;
599 if (prefix)
600 prefix_len = strlen(prefix) + 1;
601 else
602 prefix_len = 0;
603 }
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000604
Stephen Warrenaf468002011-04-28 17:38:01 -0600605 /*
606 * The control will get a prefix from the control creation
607 * process but we're also using the same prefix for widgets so
608 * cut the prefix off the front of the widget name.
609 */
610 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
611 name + prefix_len, prefix);
612 ret = snd_ctl_add(card, kcontrol);
613 if (ret < 0) {
Mark Brown53daf202011-09-05 10:51:05 -0700614 dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
615 w->name, ret);
Stephen Warrenaf468002011-04-28 17:38:01 -0600616 kfree(wlist);
617 return ret;
618 }
619 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200620
Stephen Warrenaf468002011-04-28 17:38:01 -0600621 kcontrol->private_data = wlist;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200622
Stephen Warrenfad59882011-04-28 17:37:59 -0600623 w->kcontrols[0] = kcontrol;
624
Richard Purdie2b97eab2006-10-06 18:32:18 +0200625 list_for_each_entry(path, &w->sources, list_sink)
626 path->kcontrol = kcontrol;
627
Stephen Warrenaf468002011-04-28 17:38:01 -0600628 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200629}
630
631/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200632static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200633{
Mark Browna6c65732010-03-03 17:45:21 +0000634 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200635 dev_err(w->dapm->dev,
636 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200637
Mark Browna6c65732010-03-03 17:45:21 +0000638 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200639}
640
Mark Brown99497882010-05-07 20:24:05 +0100641/* We implement power down on suspend by checking the power state of
642 * the ALSA card - when we are suspending the ALSA state for the card
643 * is set to D3.
644 */
645static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
646{
Mark Brown12ea2c72011-03-02 18:17:32 +0000647 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100648
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000649 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100650 case SNDRV_CTL_POWER_D3hot:
651 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100652 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200653 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
654 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100655 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100656 default:
657 return 1;
658 }
659}
660
Steve Mucklef132c6c2012-06-06 18:30:57 -0700661/* reset 'walked' bit for each dapm path */
662static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
663{
664 struct snd_soc_dapm_path *p;
665
666 list_for_each_entry(p, &dapm->card->paths, list)
667 p->walked = 0;
668}
669
670/* add widget to list if it's not already in the list */
671static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
672 struct snd_soc_dapm_widget *w)
673{
674 struct snd_soc_dapm_widget_list *wlist;
675 int wlistsize, wlistentries, i;
676
677 if (*list == NULL)
678 return -EINVAL;
679
680 wlist = *list;
681
682 /* is this widget already in the list */
683 for (i = 0; i < wlist->num_widgets; i++) {
684 if (wlist->widgets[i] == w)
685 return 0;
686 }
687
688 /* allocate some new space */
689 wlistentries = wlist->num_widgets + 1;
690 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
691 wlistentries * sizeof(struct snd_soc_dapm_widget *);
692 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
693 if (*list == NULL) {
694 dev_err(w->dapm->dev, "can't allocate widget list for %s\n",
695 w->name);
696 return -ENOMEM;
697 }
698 wlist = *list;
699
700 /* insert the widget */
701 dev_dbg(w->dapm->dev, "added %s in widget list pos %d\n",
702 w->name, wlist->num_widgets);
703
704 wlist->widgets[wlist->num_widgets] = w;
705 wlist->num_widgets++;
706 return 1;
707}
708
Richard Purdie2b97eab2006-10-06 18:32:18 +0200709/*
710 * Recursively check for a completed path to an active or physically connected
711 * output widget. Returns number of complete paths.
712 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700713static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
714 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200715{
716 struct snd_soc_dapm_path *path;
717 int con = 0;
718
Mark Brown024dc072011-10-09 11:52:05 +0100719 if (widget->outputs >= 0)
720 return widget->outputs;
721
Mark Brownde02d072011-09-20 21:43:24 +0100722 DAPM_UPDATE_STAT(widget, path_checks);
723
Mark Brown246d0a12009-04-22 18:24:55 +0100724 if (widget->id == snd_soc_dapm_supply)
725 return 0;
726
Mark Brown010ff262009-08-17 17:39:22 +0100727 switch (widget->id) {
728 case snd_soc_dapm_adc:
729 case snd_soc_dapm_aif_out:
Mark Brown024dc072011-10-09 11:52:05 +0100730 if (widget->active) {
731 widget->outputs = snd_soc_dapm_suspend_check(widget);
732 return widget->outputs;
733 }
Mark Brown010ff262009-08-17 17:39:22 +0100734 default:
735 break;
736 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200737
738 if (widget->connected) {
739 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100740 if (widget->id == snd_soc_dapm_output && !widget->ext) {
741 widget->outputs = snd_soc_dapm_suspend_check(widget);
742 return widget->outputs;
743 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200744
745 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100746 if (widget->id == snd_soc_dapm_hp ||
747 widget->id == snd_soc_dapm_spk ||
748 (widget->id == snd_soc_dapm_line &&
749 !list_empty(&widget->sources))) {
750 widget->outputs = snd_soc_dapm_suspend_check(widget);
751 return widget->outputs;
752 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200753 }
754
755 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e2011-09-21 18:19:14 +0100756 DAPM_UPDATE_STAT(widget, neighbour_checks);
757
Mark Brownbf3a9e12011-06-13 16:42:29 +0100758 if (path->weak)
759 continue;
760
Richard Purdie2b97eab2006-10-06 18:32:18 +0200761 if (path->walked)
762 continue;
763
Steve Mucklef132c6c2012-06-06 18:30:57 -0700764 dev_vdbg(widget->dapm->dev," %c : %s -> %s -> %s : %c\n",
765 path->sink && path->connect ? '*' : ' ',
766 widget->name, path->name, path->sink->name,
767 path->weak ? 'w': ' ');
768
Richard Purdie2b97eab2006-10-06 18:32:18 +0200769 if (path->sink && path->connect) {
770 path->walked = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700771
772 /* do we need to add this widget to the list ? */
773 if (list) {
774 int err;
775 err = dapm_list_add_widget(list, path->sink);
776 if (err < 0) {
777 dev_err(widget->dapm->dev, "could not add widget %s\n",
778 widget->name);
779 return con;
780 }
781 }
782
783 con += is_connected_output_ep(path->sink, list);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200784 }
785 }
786
Mark Brown024dc072011-10-09 11:52:05 +0100787 widget->outputs = con;
788
Richard Purdie2b97eab2006-10-06 18:32:18 +0200789 return con;
790}
791
792/*
793 * Recursively check for a completed path to an active or physically connected
794 * input widget. Returns number of complete paths.
795 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700796static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
797 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200798{
799 struct snd_soc_dapm_path *path;
800 int con = 0;
801
Mark Brown024dc072011-10-09 11:52:05 +0100802 if (widget->inputs >= 0)
803 return widget->inputs;
804
Mark Brownde02d072011-09-20 21:43:24 +0100805 DAPM_UPDATE_STAT(widget, path_checks);
806
Mark Brown246d0a12009-04-22 18:24:55 +0100807 if (widget->id == snd_soc_dapm_supply)
808 return 0;
809
Richard Purdie2b97eab2006-10-06 18:32:18 +0200810 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100811 switch (widget->id) {
812 case snd_soc_dapm_dac:
813 case snd_soc_dapm_aif_in:
Mark Brown024dc072011-10-09 11:52:05 +0100814 if (widget->active) {
815 widget->inputs = snd_soc_dapm_suspend_check(widget);
816 return widget->inputs;
817 }
Mark Brown010ff262009-08-17 17:39:22 +0100818 default:
819 break;
820 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200821
822 if (widget->connected) {
823 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100824 if (widget->id == snd_soc_dapm_input && !widget->ext) {
825 widget->inputs = snd_soc_dapm_suspend_check(widget);
826 return widget->inputs;
827 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200828
829 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100830 if (widget->id == snd_soc_dapm_vmid) {
831 widget->inputs = snd_soc_dapm_suspend_check(widget);
832 return widget->inputs;
833 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200834
835 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300836 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100837 (widget->id == snd_soc_dapm_line &&
838 !list_empty(&widget->sinks))) {
839 widget->inputs = snd_soc_dapm_suspend_check(widget);
840 return widget->inputs;
841 }
842
Mark Brown1ab97c82011-11-27 16:21:51 +0000843 /* signal generator */
844 if (widget->id == snd_soc_dapm_siggen) {
845 widget->inputs = snd_soc_dapm_suspend_check(widget);
846 return widget->inputs;
847 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200848 }
849
850 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e2011-09-21 18:19:14 +0100851 DAPM_UPDATE_STAT(widget, neighbour_checks);
852
Steve Mucklef132c6c2012-06-06 18:30:57 -0700853 if (path->source)
854 dev_vdbg(widget->dapm->dev," %c : %s <- %s <- %s : %c\n",
855 path->source && path->connect ? '*' : ' ',
856 widget->name, path->name, path->source->name,
857 path->weak ? 'w': ' ');
858
Mark Brownbf3a9e12011-06-13 16:42:29 +0100859 if (path->weak)
860 continue;
861
Richard Purdie2b97eab2006-10-06 18:32:18 +0200862 if (path->walked)
863 continue;
864
865 if (path->source && path->connect) {
866 path->walked = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700867
868 /* do we need to add this widget to the list ? */
869 if (list) {
870 int err;
871 err = dapm_list_add_widget(list, path->sink);
872 if (err < 0) {
873 dev_err(widget->dapm->dev, "could not add widget %s\n",
874 widget->name);
875 return con;
876 }
877 }
878
879 con += is_connected_input_ep(path->source, list);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200880 }
881 }
882
Mark Brown024dc072011-10-09 11:52:05 +0100883 widget->inputs = con;
884
Richard Purdie2b97eab2006-10-06 18:32:18 +0200885 return con;
886}
887
Steve Mucklef132c6c2012-06-06 18:30:57 -0700888/* Get the DAPM AIF widget for thie DAI stream */
889struct snd_soc_dapm_widget *snd_soc_get_codec_widget(struct snd_soc_card *card,
890 struct snd_soc_codec *codec, const char *name)
891{
892 struct snd_soc_dapm_widget *w;
893
894 /* get stream root widget AIF from stream string and direction */
895 list_for_each_entry(w, &card->widgets, list) {
896
897 /* make sure the widget belongs the DAI codec or platform */
898 if (w->codec && w->codec != codec)
899 continue;
900
901 if (!strcmp(w->name, name))
902 return w;
903
904 }
905 dev_err(card->dapm.dev, "DAI AIF widget for %s not found\n", name);
906 return NULL;
907}
908EXPORT_SYMBOL_GPL(snd_soc_get_codec_widget);
909
910struct snd_soc_dapm_widget *snd_soc_get_platform_widget(struct snd_soc_card *card,
911 struct snd_soc_platform *platform, const char *name)
912{
913 struct snd_soc_dapm_widget *w;
914
915 /* get stream root widget AIF from stream string and direction */
916 list_for_each_entry(w, &card->widgets, list) {
917
918 /* make sure the widget belongs the DAI codec or platform */
919 if (w->platform && w->platform != platform)
920 continue;
921
922 if (!strcmp(w->name, name))
923 return w;
924 }
925 dev_err(card->dapm.dev, "DAI AIF widget for %s not found\n", name);
926 return NULL;
927}
928EXPORT_SYMBOL_GPL(snd_soc_get_platform_widget);
929
930static int dapm_get_playback_paths(struct snd_soc_dapm_context *dapm,
931 struct snd_soc_dapm_widget *root,
932 struct snd_soc_dapm_widget_list **list)
933{
934 int paths;
935
936 if (!root) {
937 dev_err(dapm->dev, "no root widget for path discovery\n");
938 return 0;
939 }
940
941 dev_dbg(dapm->dev, "Playback: checking paths from %s\n",root->name);
942 paths = is_connected_output_ep(root, list);
943 dev_dbg(dapm->dev, "Playback: found %d paths from %s\n", paths, root->name);
944
945 return paths;
946}
947
948static int dapm_get_capture_paths(struct snd_soc_dapm_context *dapm,
949 struct snd_soc_dapm_widget *root,
950 struct snd_soc_dapm_widget_list **list)
951{
952 int paths;
953
954 if (!root) {
955 dev_err(dapm->dev, "no root widget for path discovery\n");
956 return 0;
957 }
958
959 dev_dbg(dapm->dev, "Capture: checking paths from %s\n",root->name);
960 paths = is_connected_input_ep(root, list);
961 dev_dbg(dapm->dev, "Capture: found %d paths from %s\n", paths, root->name);
962
963 return paths;
964}
965
966/**
967 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
968 * @dai: the soc DAI.
969 * @stream: stream direction.
970 * @list: list of active widgets for this stream.
971 *
972 * Queries DAPM graph as to whether an valid audio stream path exists for
973 * the initial stream specified by name. This takes into account
974 * current mixer and mux kcontrol settings. Creates list of valid widgets.
975 *
976 * Returns the number of valid paths or negative error.
977 */
978int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
979 struct snd_soc_dapm_widget_list **list)
980{
981 struct snd_soc_card *card = dai->card;
982 struct snd_soc_dapm_widget *w;
983 int paths;
984
985 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
986
987 list_for_each_entry(w, &card->widgets, list) {
988 w->power_checked = false;
989 w->inputs = -1;
990 w->outputs = -1;
991 }
992
993 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
994 paths = dapm_get_playback_paths(&card->dapm, dai->playback_aif, list);
995 else
996 paths = dapm_get_capture_paths(&card->dapm, dai->capture_aif, list);
997
998 dapm_clear_walk(&card->dapm);
999 return paths;
1000}
1001
Richard Purdie2b97eab2006-10-06 18:32:18 +02001002/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001003 * Handler for generic register modifier widget.
1004 */
1005int dapm_reg_event(struct snd_soc_dapm_widget *w,
1006 struct snd_kcontrol *kcontrol, int event)
1007{
1008 unsigned int val;
1009
1010 if (SND_SOC_DAPM_EVENT_ON(event))
1011 val = w->on_val;
1012 else
1013 val = w->off_val;
1014
Liam Girdwood0445bdf2011-06-13 19:37:36 +01001015 soc_widget_update_bits(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001016 w->mask << w->shift, val << w->shift);
1017
1018 return 0;
1019}
Mark Brown11589412008-07-29 11:42:23 +01001020EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001021
Mark Brownd8050022011-09-28 18:28:23 +01001022static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1023{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001024 if (w->power_checked)
1025 return w->new_power;
1026
Mark Brownd8050022011-09-28 18:28:23 +01001027 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001028 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001029 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001030 w->new_power = w->power_check(w);
1031
1032 w->power_checked = true;
1033
1034 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001035}
1036
Mark Browncd0f2d42009-04-20 16:56:59 +01001037/* Generic check to see if a widget should be powered.
1038 */
1039static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1040{
1041 int in, out;
1042
Mark Brownde02d072011-09-20 21:43:24 +01001043 DAPM_UPDATE_STAT(w, power_checks);
1044
Steve Mucklef132c6c2012-06-06 18:30:57 -07001045 in = is_connected_input_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001046 dapm_clear_walk(w->dapm);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001047 out = is_connected_output_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001048 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +01001049 return out != 0 && in != 0;
1050}
1051
Mark Brown6ea31b92009-04-20 17:15:41 +01001052/* Check to see if an ADC has power */
1053static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1054{
1055 int in;
1056
Mark Brownde02d072011-09-20 21:43:24 +01001057 DAPM_UPDATE_STAT(w, power_checks);
1058
Mark Brown6ea31b92009-04-20 17:15:41 +01001059 if (w->active) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001060 in = is_connected_input_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001061 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +01001062 return in != 0;
1063 } else {
1064 return dapm_generic_check_power(w);
1065 }
1066}
1067
1068/* Check to see if a DAC has power */
1069static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1070{
1071 int out;
1072
Mark Brownde02d072011-09-20 21:43:24 +01001073 DAPM_UPDATE_STAT(w, power_checks);
1074
Mark Brown6ea31b92009-04-20 17:15:41 +01001075 if (w->active) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001076 out = is_connected_output_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001077 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +01001078 return out != 0;
1079 } else {
1080 return dapm_generic_check_power(w);
1081 }
1082}
1083
Mark Brown246d0a12009-04-22 18:24:55 +01001084/* Check to see if a power supply is needed */
1085static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1086{
1087 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001088
Mark Brownde02d072011-09-20 21:43:24 +01001089 DAPM_UPDATE_STAT(w, power_checks);
1090
Mark Brown246d0a12009-04-22 18:24:55 +01001091 /* Check if one of our outputs is connected */
1092 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001093 DAPM_UPDATE_STAT(w, neighbour_checks);
1094
Mark Brownbf3a9e12011-06-13 16:42:29 +01001095 if (path->weak)
1096 continue;
1097
Mark Brown215edda2009-09-08 18:59:05 +01001098 if (path->connected &&
1099 !path->connected(path->source, path->sink))
1100 continue;
1101
Mark Brown30173582011-02-11 11:42:19 +00001102 if (!path->sink)
1103 continue;
1104
Mark Brownf68d7e12011-10-04 22:57:50 +01001105 if (dapm_widget_power_check(path->sink))
1106 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001107 }
1108
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001109 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +01001110
Mark Brownf68d7e12011-10-04 22:57:50 +01001111 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001112}
1113
Mark Brown35c64bc2011-09-28 18:23:53 +01001114static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1115{
1116 return 1;
1117}
1118
Mark Brown38357ab2009-06-06 19:03:23 +01001119static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1120 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001121 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001122{
Mark Brown828a8422011-01-15 13:14:30 +00001123 int *sort;
1124
1125 if (power_up)
1126 sort = dapm_up_seq;
1127 else
1128 sort = dapm_down_seq;
1129
Mark Brown38357ab2009-06-06 19:03:23 +01001130 if (sort[a->id] != sort[b->id])
1131 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001132 if (a->subseq != b->subseq) {
1133 if (power_up)
1134 return a->subseq - b->subseq;
1135 else
1136 return b->subseq - a->subseq;
1137 }
Mark Brownb22ead22009-06-07 12:51:26 +01001138 if (a->reg != b->reg)
1139 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001140 if (a->dapm != b->dapm)
1141 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001142
Mark Brown38357ab2009-06-06 19:03:23 +01001143 return 0;
1144}
Mark Brown42aa3412009-03-01 19:21:10 +00001145
Mark Brown38357ab2009-06-06 19:03:23 +01001146/* Insert a widget in order into a DAPM power sequence. */
1147static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1148 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001149 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001150{
1151 struct snd_soc_dapm_widget *w;
1152
1153 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001154 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001155 list_add_tail(&new_widget->power_list, &w->power_list);
1156 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001157 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001158
Mark Brown38357ab2009-06-06 19:03:23 +01001159 list_add_tail(&new_widget->power_list, list);
1160}
Mark Brown42aa3412009-03-01 19:21:10 +00001161
Mark Brown68f89ad2010-11-03 23:51:49 -04001162static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1163 struct snd_soc_dapm_widget *w, int event)
1164{
1165 struct snd_soc_card *card = dapm->card;
1166 const char *ev_name;
1167 int power, ret;
1168
1169 switch (event) {
1170 case SND_SOC_DAPM_PRE_PMU:
1171 ev_name = "PRE_PMU";
1172 power = 1;
1173 break;
1174 case SND_SOC_DAPM_POST_PMU:
1175 ev_name = "POST_PMU";
1176 power = 1;
1177 break;
1178 case SND_SOC_DAPM_PRE_PMD:
1179 ev_name = "PRE_PMD";
1180 power = 0;
1181 break;
1182 case SND_SOC_DAPM_POST_PMD:
1183 ev_name = "POST_PMD";
1184 power = 0;
1185 break;
1186 default:
1187 BUG();
1188 return;
1189 }
1190
1191 if (w->power != power)
1192 return;
1193
1194 if (w->event && (w->event_flags & event)) {
1195 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1196 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001197 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001198 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001199 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001200 if (ret < 0)
1201 pr_err("%s: %s event failed: %d\n",
1202 ev_name, w->name, ret);
1203 }
1204}
1205
Mark Brownb22ead22009-06-07 12:51:26 +01001206/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001207static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +01001208 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001209{
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001210 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -04001211 struct snd_soc_dapm_widget *w;
1212 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +01001213 unsigned int value = 0;
1214 unsigned int mask = 0;
1215 unsigned int cur_mask;
1216
1217 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1218 power_list)->reg;
1219
1220 list_for_each_entry(w, pending, power_list) {
1221 cur_mask = 1 << w->shift;
1222 BUG_ON(reg != w->reg);
1223
1224 if (w->invert)
1225 power = !w->power;
1226 else
1227 power = w->power;
1228
1229 mask |= cur_mask;
1230 if (power)
1231 value |= cur_mask;
1232
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001233 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001234 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1235 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001236
Mark Brown68f89ad2010-11-03 23:51:49 -04001237 /* Check for events */
1238 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1239 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001240 }
1241
Mark Brown81628102009-06-07 13:21:24 +01001242 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001243 /* Any widget will do, they should all be updating the
1244 * same register.
1245 */
1246 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1247 power_list);
1248
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001249 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001250 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001251 value, mask, reg, card->pop_time);
1252 pop_wait(card->pop_time);
Liam Girdwood0445bdf2011-06-13 19:37:36 +01001253 soc_widget_update_bits(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001254 }
1255
1256 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -04001257 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1258 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001259 }
Mark Brown42aa3412009-03-01 19:21:10 +00001260}
1261
Mark Brownb22ead22009-06-07 12:51:26 +01001262/* Apply a DAPM power sequence.
1263 *
1264 * We walk over a pre-sorted list of widgets to apply power to. In
1265 * order to minimise the number of writes to the device required
1266 * multiple widgets will be updated in a single write where possible.
1267 * Currently anything that requires more than a single write is not
1268 * handled.
1269 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001270static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +00001271 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001272{
1273 struct snd_soc_dapm_widget *w, *n;
1274 LIST_HEAD(pending);
1275 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001276 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001277 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001278 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001279 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001280 int *sort;
1281
1282 if (power_up)
1283 sort = dapm_up_seq;
1284 else
1285 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001286
Mark Brownb22ead22009-06-07 12:51:26 +01001287 list_for_each_entry_safe(w, n, list, power_list) {
1288 ret = 0;
1289
1290 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001291 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001292 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Sriranjan Srikantamfd7f1ad2012-08-14 17:42:06 -07001293 if (cur_dapm && !list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001294 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001295
Mark Brown474b62d2011-01-18 16:14:44 +00001296 if (cur_dapm && cur_dapm->seq_notifier) {
1297 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1298 if (sort[i] == cur_sort)
1299 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001300 i,
1301 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001302 }
1303
Mark Brownb22ead22009-06-07 12:51:26 +01001304 INIT_LIST_HEAD(&pending);
1305 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001306 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001307 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001308 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001309 }
1310
Mark Brown163cac02009-06-07 10:12:52 +01001311 switch (w->id) {
1312 case snd_soc_dapm_pre:
1313 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001314 list_for_each_entry_safe_continue(w, n, list,
1315 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001316
Mark Brownb22ead22009-06-07 12:51:26 +01001317 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001318 ret = w->event(w,
1319 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001320 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001321 ret = w->event(w,
1322 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001323 break;
1324
1325 case snd_soc_dapm_post:
1326 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001327 list_for_each_entry_safe_continue(w, n, list,
1328 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001329
Mark Brownb22ead22009-06-07 12:51:26 +01001330 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001331 ret = w->event(w,
1332 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001333 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001334 ret = w->event(w,
1335 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001336 break;
1337
Mark Brown163cac02009-06-07 10:12:52 +01001338 default:
Mark Brown81628102009-06-07 13:21:24 +01001339 /* Queue it up for application */
1340 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001341 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001342 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001343 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001344 list_move(&w->power_list, &pending);
1345 break;
Mark Brown163cac02009-06-07 10:12:52 +01001346 }
Mark Brownb22ead22009-06-07 12:51:26 +01001347
1348 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001349 dev_err(w->dapm->dev,
1350 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001351 }
Mark Brownb22ead22009-06-07 12:51:26 +01001352
Sriranjan Srikantamfd7f1ad2012-08-14 17:42:06 -07001353 if (cur_dapm && !list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001354 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001355
1356 if (cur_dapm && cur_dapm->seq_notifier) {
1357 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1358 if (sort[i] == cur_sort)
1359 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001360 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001361 }
Mark Brown163cac02009-06-07 10:12:52 +01001362}
1363
Mark Brown97404f22010-12-14 16:13:57 +00001364static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1365{
1366 struct snd_soc_dapm_update *update = dapm->update;
1367 struct snd_soc_dapm_widget *w;
1368 int ret;
1369
1370 if (!update)
1371 return;
1372
1373 w = update->widget;
1374
1375 if (w->event &&
1376 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1377 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1378 if (ret != 0)
1379 pr_err("%s DAPM pre-event failed: %d\n",
1380 w->name, ret);
1381 }
1382
1383 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1384 update->val);
1385 if (ret < 0)
1386 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1387
1388 if (w->event &&
1389 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1390 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1391 if (ret != 0)
1392 pr_err("%s DAPM post-event failed: %d\n",
1393 w->name, ret);
1394 }
1395}
1396
Mark Brown9d0624a2011-02-18 11:49:43 -08001397/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1398 * they're changing state.
1399 */
1400static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1401{
1402 struct snd_soc_dapm_context *d = data;
1403 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001404
Mark Brown56fba412011-06-04 11:25:10 +01001405 /* If we're off and we're not supposed to be go into STANDBY */
1406 if (d->bias_level == SND_SOC_BIAS_OFF &&
1407 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001408 if (d->dev)
1409 pm_runtime_get_sync(d->dev);
1410
Mark Brown9d0624a2011-02-18 11:49:43 -08001411 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1412 if (ret != 0)
1413 dev_err(d->dev,
1414 "Failed to turn on bias: %d\n", ret);
1415 }
1416
Mark Brown56fba412011-06-04 11:25:10 +01001417 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1418 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001419 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1420 if (ret != 0)
1421 dev_err(d->dev,
1422 "Failed to prepare bias: %d\n", ret);
1423 }
1424}
1425
1426/* Async callback run prior to DAPM sequences - brings to their final
1427 * state.
1428 */
1429static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1430{
1431 struct snd_soc_dapm_context *d = data;
1432 int ret;
1433
1434 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001435 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1436 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1437 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001438 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1439 if (ret != 0)
1440 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1441 ret);
1442 }
1443
1444 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001445 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1446 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001447 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1448 if (ret != 0)
1449 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001450
1451 if (d->dev)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001452 pm_runtime_put_sync(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001453 }
1454
1455 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001456 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1457 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001458 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1459 if (ret != 0)
1460 dev_err(d->dev, "Failed to apply active bias: %d\n",
1461 ret);
1462 }
1463}
Mark Brown97404f22010-12-14 16:13:57 +00001464
Mark Brownfe4fda52011-10-03 22:36:57 +01001465static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1466 bool power, bool connect)
1467{
1468 /* If a connection is being made or broken then that update
1469 * will have marked the peer dirty, otherwise the widgets are
1470 * not connected and this update has no impact. */
1471 if (!connect)
1472 return;
1473
1474 /* If the peer is already in the state we're moving to then we
1475 * won't have an impact on it. */
1476 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001477 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001478}
1479
Mark Brown05623c42011-09-28 17:02:31 +01001480static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1481 struct list_head *up_list,
1482 struct list_head *down_list)
1483{
Mark Browndb432b42011-10-03 21:06:40 +01001484 struct snd_soc_dapm_path *path;
1485
Mark Brown05623c42011-09-28 17:02:31 +01001486 if (w->power == power)
1487 return;
1488
1489 trace_snd_soc_dapm_widget_power(w, power);
1490
Mark Browndb432b42011-10-03 21:06:40 +01001491 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001492 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001493 */
1494 list_for_each_entry(path, &w->sources, list_sink) {
1495 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001496 dapm_widget_set_peer_power(path->source, power,
1497 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001498 }
1499 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001500 switch (w->id) {
1501 case snd_soc_dapm_supply:
1502 /* Supplies can't affect their outputs, only their inputs */
1503 break;
1504 default:
1505 list_for_each_entry(path, &w->sinks, list_source) {
1506 if (path->sink) {
1507 dapm_widget_set_peer_power(path->sink, power,
1508 path->connect);
1509 }
Mark Browndb432b42011-10-03 21:06:40 +01001510 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001511 break;
Mark Browndb432b42011-10-03 21:06:40 +01001512 }
1513
Steve Mucklef132c6c2012-06-06 18:30:57 -07001514 if (power) {
Mark Brown05623c42011-09-28 17:02:31 +01001515 dapm_seq_insert(w, up_list, true);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001516 dev_dbg(w->dapm->dev,
1517 "dapm: power up widget %s\n", w->name);
1518 } else {
Mark Brown05623c42011-09-28 17:02:31 +01001519 dapm_seq_insert(w, down_list, false);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001520 dev_dbg(w->dapm->dev,
1521 "dapm: power down widget %s\n", w->name);
1522 }
Mark Brown05623c42011-09-28 17:02:31 +01001523
1524 w->power = power;
1525}
1526
Mark Brown7c81beb2011-09-20 22:22:32 +01001527static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1528 struct list_head *up_list,
1529 struct list_head *down_list)
1530{
Mark Brown7c81beb2011-09-20 22:22:32 +01001531 int power;
1532
1533 switch (w->id) {
1534 case snd_soc_dapm_pre:
1535 dapm_seq_insert(w, down_list, false);
1536 break;
1537 case snd_soc_dapm_post:
1538 dapm_seq_insert(w, up_list, true);
1539 break;
1540
1541 default:
Mark Brownd8050022011-09-28 18:28:23 +01001542 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001543
Mark Brown05623c42011-09-28 17:02:31 +01001544 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001545 break;
1546 }
1547}
1548
Mark Brown42aa3412009-03-01 19:21:10 +00001549/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001550 * Scan each dapm widget for complete audio path.
1551 * A complete path is a route that has valid endpoints i.e.:-
1552 *
1553 * o DAC to output pin.
1554 * o Input Pin to ADC.
1555 * o Input pin to Output pin (bypass, sidetone)
1556 * o DAC to ADC (loopback).
1557 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001558static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001559{
Mark Brown12ea2c72011-03-02 18:17:32 +00001560 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001561 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001562 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001563 LIST_HEAD(up_list);
1564 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001565 LIST_HEAD(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001566 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001567
Mark Brown84e90932010-11-04 00:07:02 -04001568 trace_snd_soc_dapm_start(card);
1569
Sriranjan Srikantamfd7f1ad2012-08-14 17:42:06 -07001570 mutex_lock(&card->dapm_power_mutex);
1571
Mark Brown56fba412011-06-04 11:25:10 +01001572 list_for_each_entry(d, &card->dapm_list, list) {
1573 if (d->n_widgets || d->codec == NULL) {
1574 if (d->idle_bias_off)
1575 d->target_bias_level = SND_SOC_BIAS_OFF;
1576 else
1577 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1578 }
1579 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001580
Steve Mucklef132c6c2012-06-06 18:30:57 -07001581 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
1582
1583 list_for_each_entry(w, &card->widgets, list) {
1584 w->power_checked = false;
1585 w->inputs = -1;
1586 w->outputs = -1;
1587 }
Mark Brown9b8a83b2011-10-04 22:15:59 +01001588
Mark Brown6d3ddc82009-05-16 17:47:29 +01001589 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001590 * lists indicating if they should be powered up or down. We
1591 * only check widgets that have been flagged as dirty but note
1592 * that new widgets may be added to the dirty list while we
1593 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001594 */
Mark Browndb432b42011-10-03 21:06:40 +01001595 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001596 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001597 }
1598
Mark Brownf9de6d72011-09-28 17:19:47 +01001599 list_for_each_entry(w, &card->widgets, list) {
Mark Browndb432b42011-10-03 21:06:40 +01001600 list_del_init(&w->dirty);
1601
Mark Brownf9de6d72011-09-28 17:19:47 +01001602 if (w->power) {
1603 d = w->dapm;
1604
1605 /* Supplies and micbiases only bring the
1606 * context up to STANDBY as unless something
1607 * else is active and passing audio they
Steve Mucklef132c6c2012-06-06 18:30:57 -07001608 * generally don't require full power.
Mark Brownf9de6d72011-09-28 17:19:47 +01001609 */
1610 switch (w->id) {
1611 case snd_soc_dapm_supply:
1612 case snd_soc_dapm_micbias:
1613 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1614 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1615 break;
1616 default:
1617 d->target_bias_level = SND_SOC_BIAS_ON;
1618 break;
1619 }
1620 }
1621
1622 }
1623
Mark Brownb14b76a2009-08-17 11:55:38 +01001624 /* If there are no DAPM widgets then try to figure out power from the
1625 * event type.
1626 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001627 if (!dapm->n_widgets) {
Mark Brownb14b76a2009-08-17 11:55:38 +01001628 switch (event) {
1629 case SND_SOC_DAPM_STREAM_START:
1630 case SND_SOC_DAPM_STREAM_RESUME:
Mark Brown56fba412011-06-04 11:25:10 +01001631 dapm->target_bias_level = SND_SOC_BIAS_ON;
Mark Brownb14b76a2009-08-17 11:55:38 +01001632 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001633 case SND_SOC_DAPM_STREAM_STOP:
Liam Girdwoode7c80e22012-01-16 15:23:31 +00001634 if (dapm->codec && dapm->codec->active)
Mark Brown56fba412011-06-04 11:25:10 +01001635 dapm->target_bias_level = SND_SOC_BIAS_ON;
1636 else
1637 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001638 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001639 case SND_SOC_DAPM_STREAM_SUSPEND:
Mark Brown56fba412011-06-04 11:25:10 +01001640 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown50b6bce2009-11-23 13:11:53 +00001641 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001642 case SND_SOC_DAPM_STREAM_NOP:
Mark Brown56fba412011-06-04 11:25:10 +01001643 dapm->target_bias_level = dapm->bias_level;
Mark Brown50b6bce2009-11-23 13:11:53 +00001644 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001645 default:
1646 break;
1647 }
1648 }
1649
Mark Brown85a843c2011-09-21 21:29:47 +01001650 /* Force all contexts in the card to the same bias state if
1651 * they're not ground referenced.
1652 */
Mark Brown56fba412011-06-04 11:25:10 +01001653 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001654 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001655 if (d->target_bias_level > bias)
1656 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001657 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001658 if (!d->idle_bias_off)
1659 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001660
Mark Brownde02d072011-09-20 21:43:24 +01001661 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001662
Mark Brown9d0624a2011-02-18 11:49:43 -08001663 /* Run all the bias changes in parallel */
1664 list_for_each_entry(d, &dapm->card->dapm_list, list)
1665 async_schedule_domain(dapm_pre_sequence_async, d,
1666 &async_domain);
1667 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001668
Mark Brown6d3ddc82009-05-16 17:47:29 +01001669 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001670 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001671
Mark Brown97404f22010-12-14 16:13:57 +00001672 dapm_widget_update(dapm);
1673
Mark Brown6d3ddc82009-05-16 17:47:29 +01001674 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001675 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001676
Mark Brown9d0624a2011-02-18 11:49:43 -08001677 /* Run all the bias changes in parallel */
1678 list_for_each_entry(d, &dapm->card->dapm_list, list)
1679 async_schedule_domain(dapm_post_sequence_async, d,
1680 &async_domain);
1681 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001682
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001683 pop_dbg(dapm->dev, card->pop_time,
1684 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001685 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001686
Sriranjan Srikantamfd7f1ad2012-08-14 17:42:06 -07001687 mutex_unlock(&card->dapm_power_mutex);
1688
Mark Brown84e90932010-11-04 00:07:02 -04001689 trace_snd_soc_dapm_done(card);
1690
Mark Brown42aa3412009-03-01 19:21:10 +00001691 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001692}
1693
Mark Brown79fb9382009-08-21 16:38:13 +01001694#ifdef CONFIG_DEBUG_FS
1695static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1696{
1697 file->private_data = inode->i_private;
1698 return 0;
1699}
1700
1701static ssize_t dapm_widget_power_read_file(struct file *file,
1702 char __user *user_buf,
1703 size_t count, loff_t *ppos)
1704{
1705 struct snd_soc_dapm_widget *w = file->private_data;
1706 char *buf;
1707 int in, out;
1708 ssize_t ret;
1709 struct snd_soc_dapm_path *p = NULL;
1710
1711 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1712 if (!buf)
1713 return -ENOMEM;
1714
Steve Mucklef132c6c2012-06-06 18:30:57 -07001715 in = is_connected_input_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001716 dapm_clear_walk(w->dapm);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001717 out = is_connected_output_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001718 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001719
Mark Brownd033c362009-12-04 15:25:56 +00001720 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001721 w->name, w->power ? "On" : "Off", in, out);
1722
Mark Brownd033c362009-12-04 15:25:56 +00001723 if (w->reg >= 0)
1724 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1725 " - R%d(0x%x) bit %d",
1726 w->reg, w->reg, w->shift);
1727
1728 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1729
Mark Brown3eef08b2009-09-14 16:49:00 +01001730 if (w->sname)
1731 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1732 w->sname,
1733 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001734
1735 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001736 if (p->connected && !p->connected(w, p->sink))
1737 continue;
1738
Mark Brown79fb9382009-08-21 16:38:13 +01001739 if (p->connect)
1740 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001741 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001742 p->name ? p->name : "static",
1743 p->source->name);
1744 }
1745 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001746 if (p->connected && !p->connected(w, p->sink))
1747 continue;
1748
Mark Brown79fb9382009-08-21 16:38:13 +01001749 if (p->connect)
1750 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001751 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001752 p->name ? p->name : "static",
1753 p->sink->name);
1754 }
1755
1756 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1757
1758 kfree(buf);
1759 return ret;
1760}
1761
1762static const struct file_operations dapm_widget_power_fops = {
1763 .open = dapm_widget_power_open_file,
1764 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001765 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001766};
1767
Mark Brownef49e4f2011-04-04 20:48:13 +09001768static int dapm_bias_open_file(struct inode *inode, struct file *file)
1769{
1770 file->private_data = inode->i_private;
1771 return 0;
1772}
1773
1774static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1775 size_t count, loff_t *ppos)
1776{
1777 struct snd_soc_dapm_context *dapm = file->private_data;
1778 char *level;
1779
1780 switch (dapm->bias_level) {
1781 case SND_SOC_BIAS_ON:
1782 level = "On\n";
1783 break;
1784 case SND_SOC_BIAS_PREPARE:
1785 level = "Prepare\n";
1786 break;
1787 case SND_SOC_BIAS_STANDBY:
1788 level = "Standby\n";
1789 break;
1790 case SND_SOC_BIAS_OFF:
1791 level = "Off\n";
1792 break;
1793 default:
1794 BUG();
1795 level = "Unknown\n";
1796 break;
1797 }
1798
1799 return simple_read_from_buffer(user_buf, count, ppos, level,
1800 strlen(level));
1801}
1802
1803static const struct file_operations dapm_bias_fops = {
1804 .open = dapm_bias_open_file,
1805 .read = dapm_bias_read_file,
1806 .llseek = default_llseek,
1807};
1808
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001809void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1810 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001811{
Mark Brown79fb9382009-08-21 16:38:13 +01001812 struct dentry *d;
1813
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001814 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1815
1816 if (!dapm->debugfs_dapm) {
1817 printk(KERN_WARNING
1818 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001819 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001820 }
Mark Brown79fb9382009-08-21 16:38:13 +01001821
Mark Brownef49e4f2011-04-04 20:48:13 +09001822 d = debugfs_create_file("bias_level", 0444,
1823 dapm->debugfs_dapm, dapm,
1824 &dapm_bias_fops);
1825 if (!d)
1826 dev_warn(dapm->dev,
1827 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001828}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001829
1830static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1831{
1832 struct snd_soc_dapm_context *dapm = w->dapm;
1833 struct dentry *d;
1834
1835 if (!dapm->debugfs_dapm || !w->name)
1836 return;
1837
1838 d = debugfs_create_file(w->name, 0444,
1839 dapm->debugfs_dapm, w,
1840 &dapm_widget_power_fops);
1841 if (!d)
1842 dev_warn(w->dapm->dev,
1843 "ASoC: Failed to create %s debugfs file\n",
1844 w->name);
1845}
1846
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001847static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1848{
1849 debugfs_remove_recursive(dapm->debugfs_dapm);
1850}
1851
Mark Brown79fb9382009-08-21 16:38:13 +01001852#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001853void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1854 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001855{
1856}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001857
1858static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1859{
1860}
1861
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001862static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1863{
1864}
1865
Mark Brown79fb9382009-08-21 16:38:13 +01001866#endif
1867
Richard Purdie2b97eab2006-10-06 18:32:18 +02001868/* test and update the power status of a mux widget */
Liam Girdwood55c42612012-03-07 10:38:27 +00001869static int soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001870 struct snd_kcontrol *kcontrol, int change,
1871 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001872{
1873 struct snd_soc_dapm_path *path;
1874 int found = 0;
1875
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001876 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001877 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001878 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001879 return -ENODEV;
1880
Mark Brown3a655772009-10-05 17:23:30 +01001881 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001882 return 0;
1883
1884 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001885 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Liam Girdwoode002c982011-02-08 11:45:20 +00001886
Richard Purdie2b97eab2006-10-06 18:32:18 +02001887 if (path->kcontrol != kcontrol)
1888 continue;
1889
Liam Girdwooda8f13d82011-05-18 18:39:21 +01001890 if (!path->name || !snd_soc_get_enum_text(e, mux))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001891 continue;
1892
1893 found = 1;
1894 /* we now need to match the string in the enum to the path */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001895 if (!(strcmp(path->name, snd_soc_get_enum_text(e, mux)))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001896 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01001897 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01001898 } else {
1899 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01001900 dapm_mark_dirty(path->source,
1901 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001902 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01001903 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001904 }
1905
Mark Browndb432b42011-10-03 21:06:40 +01001906 if (found) {
Gopikrishnaiah Anandan8848f672013-12-23 16:49:04 -08001907 dapm_mark_dirty(widget, "mux change");
1908 dapm_power_widgets(widget->dapm,
1909 SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001910 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001911
Gopikrishnaiah Anandan8848f672013-12-23 16:49:04 -08001912 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001913}
Liam Girdwood55c42612012-03-07 10:38:27 +00001914
1915int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1916 struct snd_kcontrol *kcontrol, int change,
1917 int mux, struct soc_enum *e)
1918{
1919 struct snd_soc_card *card = widget->dapm->card;
1920 int ret;
Gopikrishnaiah Anandan07d4b262013-11-13 11:06:13 -08001921 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_PCM);
Liam Girdwood55c42612012-03-07 10:38:27 +00001922 ret = soc_dapm_mux_update_power(widget, kcontrol, change, mux, e);
1923 mutex_unlock(&card->dapm_mutex);
Gopikrishnaiah Anandan8848f672013-12-23 16:49:04 -08001924 if (ret > 0)
1925 soc_dpcm_runtime_update(widget);
Liam Girdwood55c42612012-03-07 10:38:27 +00001926 return ret;
1927}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001928EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001929
Milan plzik1b075e32008-01-10 14:39:46 +01001930/* test and update the power status of a mixer or switch widget */
Liam Girdwood55c42612012-03-07 10:38:27 +00001931static int soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001932 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001933{
1934 struct snd_soc_dapm_path *path;
1935 int found = 0;
1936
Milan plzik1b075e32008-01-10 14:39:46 +01001937 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001938 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001939 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001940 return -ENODEV;
1941
Richard Purdie2b97eab2006-10-06 18:32:18 +02001942 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001943 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001944 if (path->kcontrol != kcontrol)
1945 continue;
1946
1947 /* found, now check type */
1948 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001949 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01001950 dapm_mark_dirty(path->source, "mixer connection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001951 }
1952
Mark Browndb432b42011-10-03 21:06:40 +01001953 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001954 dapm_mark_dirty(widget, "mixer update");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001955 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001956 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001957
Gopikrishnaiah Anandan8848f672013-12-23 16:49:04 -08001958 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001959}
Liam Girdwood55c42612012-03-07 10:38:27 +00001960
1961int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1962 struct snd_kcontrol *kcontrol, int connect)
1963{
1964 struct snd_soc_card *card = widget->dapm->card;
1965 int ret;
Gopikrishnaiah Anandan07d4b262013-11-13 11:06:13 -08001966 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_PCM);
Liam Girdwood55c42612012-03-07 10:38:27 +00001967 ret = soc_dapm_mixer_update_power(widget, kcontrol, connect);
1968 mutex_unlock(&card->dapm_mutex);
Gopikrishnaiah Anandan8848f672013-12-23 16:49:04 -08001969 if (ret > 0)
1970 soc_dpcm_runtime_update(widget);
Liam Girdwood55c42612012-03-07 10:38:27 +00001971 return ret;
1972}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001973EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001974
1975/* show dapm widget status in sys fs */
1976static ssize_t dapm_widget_show(struct device *dev,
1977 struct device_attribute *attr, char *buf)
1978{
Mark Brown36ae1a92012-01-06 17:12:45 -08001979 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001980 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001981 struct snd_soc_dapm_widget *w;
1982 int count = 0;
1983 char *state = "not set";
1984
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001985 list_for_each_entry(w, &codec->card->widgets, list) {
1986 if (w->dapm != &codec->dapm)
1987 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001988
1989 /* only display widgets that burnm power */
1990 switch (w->id) {
1991 case snd_soc_dapm_hp:
1992 case snd_soc_dapm_mic:
1993 case snd_soc_dapm_spk:
1994 case snd_soc_dapm_line:
1995 case snd_soc_dapm_micbias:
1996 case snd_soc_dapm_dac:
1997 case snd_soc_dapm_adc:
1998 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001999 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002000 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002001 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002002 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002003 if (w->name)
2004 count += sprintf(buf + count, "%s: %s\n",
2005 w->name, w->power ? "On":"Off");
2006 break;
2007 default:
2008 break;
2009 }
2010 }
2011
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002012 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002013 case SND_SOC_BIAS_ON:
2014 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002015 break;
Mark Brown0be98982008-05-19 12:31:28 +02002016 case SND_SOC_BIAS_PREPARE:
2017 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002018 break;
Mark Brown0be98982008-05-19 12:31:28 +02002019 case SND_SOC_BIAS_STANDBY:
2020 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002021 break;
Mark Brown0be98982008-05-19 12:31:28 +02002022 case SND_SOC_BIAS_OFF:
2023 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002024 break;
2025 }
2026 count += sprintf(buf + count, "PM State: %s\n", state);
2027
2028 return count;
2029}
2030
2031static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2032
2033int snd_soc_dapm_sys_add(struct device *dev)
2034{
Troy Kisky12ef1932008-10-13 17:42:14 -07002035 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002036}
2037
2038static void snd_soc_dapm_sys_remove(struct device *dev)
2039{
Mark Brownaef90842009-05-16 17:53:16 +01002040 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002041}
2042
2043/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002044static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002045{
2046 struct snd_soc_dapm_widget *w, *next_w;
2047 struct snd_soc_dapm_path *p, *next_p;
2048
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002049 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2050 if (w->dapm != dapm)
2051 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002052 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002053 /*
2054 * remove source and sink paths associated to this widget.
2055 * While removing the path, remove reference to it from both
2056 * source and sink widgets so that path is removed only once.
2057 */
2058 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
2059 list_del(&p->list_sink);
2060 list_del(&p->list_source);
2061 list_del(&p->list);
2062 kfree(p->long_name);
2063 kfree(p);
2064 }
2065 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
2066 list_del(&p->list_sink);
2067 list_del(&p->list_source);
2068 list_del(&p->list);
2069 kfree(p->long_name);
2070 kfree(p);
2071 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002072 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002073 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002074 kfree(w);
2075 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002076}
2077
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002078static struct snd_soc_dapm_widget *dapm_find_widget(
2079 struct snd_soc_dapm_context *dapm, const char *pin,
2080 bool search_other_contexts)
2081{
2082 struct snd_soc_dapm_widget *w;
2083 struct snd_soc_dapm_widget *fallback = NULL;
2084
2085 list_for_each_entry(w, &dapm->card->widgets, list) {
2086 if (!strcmp(w->name, pin)) {
2087 if (w->dapm == dapm)
2088 return w;
2089 else
2090 fallback = w;
2091 }
2092 }
2093
2094 if (search_other_contexts)
2095 return fallback;
2096
2097 return NULL;
2098}
2099
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002100static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002101 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002102{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002103 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002104
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002105 if (!w) {
2106 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2107 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002108 }
2109
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002110 w->connected = status;
2111 if (status == 0)
2112 w->force = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002113 dapm_mark_dirty(w, "pin configuration");
Mark Brown0d867332011-04-06 11:38:14 +09002114
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002115 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002116}
2117
Richard Purdie2b97eab2006-10-06 18:32:18 +02002118/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002119 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002120 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002121 *
2122 * Walks all dapm audio paths and powers widgets according to their
2123 * stream or path usage.
2124 *
2125 * Returns 0 for success.
2126 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002127int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002128{
Liam Girdwood6acab8c2012-03-07 10:38:26 +00002129 int ret;
2130
Mark Brown4f4c0072011-10-07 14:29:19 +01002131 /*
2132 * Suppress early reports (eg, jacks syncing their state) to avoid
2133 * silly DAPM runs during card startup.
2134 */
2135 if (!dapm->card || !dapm->card->instantiated)
2136 return 0;
2137
Liam Girdwood6acab8c2012-03-07 10:38:26 +00002138 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_PCM);
2139 ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2140 mutex_unlock(&dapm->card->dapm_mutex);
2141 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002142}
Liam Girdwooda5302182008-07-07 13:35:17 +01002143EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002144
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002145static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01002146 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002147{
2148 struct snd_soc_dapm_path *path;
2149 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002150 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002151 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01002152 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002153 const char *source;
2154 char prefixed_sink[80];
2155 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002156 int ret = 0;
2157
Mark Brown88e8b9a2011-03-02 18:18:24 +00002158 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002159 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2160 dapm->codec->name_prefix, route->sink);
2161 sink = prefixed_sink;
2162 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2163 dapm->codec->name_prefix, route->source);
2164 source = prefixed_source;
2165 } else {
2166 sink = route->sink;
2167 source = route->source;
2168 }
2169
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002170 /*
2171 * find src and dest widgets over all widgets but favor a widget from
2172 * current DAPM context
2173 */
2174 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002175 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002176 wtsink = w;
2177 if (w->dapm == dapm)
2178 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002179 continue;
2180 }
2181 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002182 wtsource = w;
2183 if (w->dapm == dapm)
2184 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002185 }
2186 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002187 /* use widget from another DAPM context if not found from this */
2188 if (!wsink)
2189 wsink = wtsink;
2190 if (!wsource)
2191 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002192
2193 if (wsource == NULL || wsink == NULL)
2194 return -ENODEV;
2195
2196 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2197 if (!path)
2198 return -ENOMEM;
2199
2200 path->source = wsource;
2201 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01002202 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002203 INIT_LIST_HEAD(&path->list);
2204 INIT_LIST_HEAD(&path->list_source);
2205 INIT_LIST_HEAD(&path->list_sink);
2206
2207 /* check for external widgets */
2208 if (wsink->id == snd_soc_dapm_input) {
2209 if (wsource->id == snd_soc_dapm_micbias ||
2210 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002211 wsource->id == snd_soc_dapm_line ||
2212 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002213 wsink->ext = 1;
2214 }
2215 if (wsource->id == snd_soc_dapm_output) {
2216 if (wsink->id == snd_soc_dapm_spk ||
2217 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002218 wsink->id == snd_soc_dapm_line ||
2219 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002220 wsource->ext = 1;
2221 }
2222
2223 /* connect static paths */
2224 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002225 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002226 list_add(&path->list_sink, &wsink->sources);
2227 list_add(&path->list_source, &wsource->sinks);
2228 path->connect = 1;
2229 return 0;
2230 }
2231
2232 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002233 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002234 case snd_soc_dapm_adc:
2235 case snd_soc_dapm_dac:
2236 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002237 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002238 case snd_soc_dapm_input:
2239 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002240 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002241 case snd_soc_dapm_micbias:
2242 case snd_soc_dapm_vmid:
2243 case snd_soc_dapm_pre:
2244 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002245 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002246 case snd_soc_dapm_aif_in:
2247 case snd_soc_dapm_aif_out:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002248 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002249 list_add(&path->list_sink, &wsink->sources);
2250 list_add(&path->list_source, &wsource->sinks);
2251 path->connect = 1;
2252 return 0;
2253 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002254 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002255 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002256 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002257 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002258 if (ret != 0)
2259 goto err;
2260 break;
2261 case snd_soc_dapm_switch:
2262 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002263 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002264 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002265 if (ret != 0)
2266 goto err;
2267 break;
2268 case snd_soc_dapm_hp:
2269 case snd_soc_dapm_mic:
2270 case snd_soc_dapm_line:
2271 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002272 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002273 list_add(&path->list_sink, &wsink->sources);
2274 list_add(&path->list_source, &wsource->sinks);
2275 path->connect = 0;
2276 return 0;
2277 }
2278 return 0;
2279
2280err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002281 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2282 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002283 kfree(path);
2284 return ret;
2285}
Mark Brown105f1c22008-05-13 14:52:19 +02002286
2287/**
Mark Brown105f1c22008-05-13 14:52:19 +02002288 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002289 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002290 * @route: audio routes
2291 * @num: number of routes
2292 *
2293 * Connects 2 dapm widgets together via a named audio path. The sink is
2294 * the widget receiving the audio signal, whilst the source is the sender
2295 * of the audio signal.
2296 *
2297 * Returns 0 for success else error. On error all resources can be freed
2298 * with a call to snd_soc_card_free().
2299 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002300int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002301 const struct snd_soc_dapm_route *route, int num)
2302{
2303 int i, ret;
2304
Liam Girdwood6acab8c2012-03-07 10:38:26 +00002305 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002306 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002307 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02002308 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002309 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2310 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02002311 return ret;
2312 }
2313 route++;
2314 }
Liam Girdwood6acab8c2012-03-07 10:38:26 +00002315 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002316
2317 return 0;
2318}
2319EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2320
Mark Brownbf3a9e12011-06-13 16:42:29 +01002321static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2322 const struct snd_soc_dapm_route *route)
2323{
2324 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2325 route->source,
2326 true);
2327 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2328 route->sink,
2329 true);
2330 struct snd_soc_dapm_path *path;
2331 int count = 0;
2332
2333 if (!source) {
2334 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2335 route->source);
2336 return -ENODEV;
2337 }
2338
2339 if (!sink) {
2340 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2341 route->sink);
2342 return -ENODEV;
2343 }
2344
2345 if (route->control || route->connected)
2346 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2347 route->source, route->sink);
2348
2349 list_for_each_entry(path, &source->sinks, list_source) {
2350 if (path->sink == sink) {
2351 path->weak = 1;
2352 count++;
2353 }
2354 }
2355
2356 if (count == 0)
2357 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2358 route->source, route->sink);
2359 if (count > 1)
2360 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2361 count, route->source, route->sink);
2362
2363 return 0;
2364}
2365
2366/**
2367 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2368 * @dapm: DAPM context
2369 * @route: audio routes
2370 * @num: number of routes
2371 *
2372 * Mark existing routes matching those specified in the passed array
2373 * as being weak, meaning that they are ignored for the purpose of
2374 * power decisions. The main intended use case is for sidetone paths
2375 * which couple audio between other independent paths if they are both
2376 * active in order to make the combination work better at the user
2377 * level but which aren't intended to be "used".
2378 *
2379 * Note that CODEC drivers should not use this as sidetone type paths
2380 * can frequently also be used as bypass paths.
2381 */
2382int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2383 const struct snd_soc_dapm_route *route, int num)
2384{
2385 int i, err;
2386 int ret = 0;
2387
Liam Girdwood6acab8c2012-03-07 10:38:26 +00002388 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002389 for (i = 0; i < num; i++) {
2390 err = snd_soc_dapm_weak_route(dapm, route);
2391 if (err)
2392 ret = err;
2393 route++;
2394 }
Liam Girdwood6acab8c2012-03-07 10:38:26 +00002395 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002396
2397 return ret;
2398}
2399EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2400
Mark Brown105f1c22008-05-13 14:52:19 +02002401/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002402 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002403 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002404 *
2405 * Checks the codec for any new dapm widgets and creates them if found.
2406 *
2407 * Returns 0 for success.
2408 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002409int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002410{
2411 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002412 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002413
Liam Girdwood6acab8c2012-03-07 10:38:26 +00002414 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2415
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002416 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002417 {
2418 if (w->new)
2419 continue;
2420
Stephen Warrenfad59882011-04-28 17:37:59 -06002421 if (w->num_kcontrols) {
2422 w->kcontrols = kzalloc(w->num_kcontrols *
2423 sizeof(struct snd_kcontrol *),
2424 GFP_KERNEL);
Liam Girdwood6acab8c2012-03-07 10:38:26 +00002425 if (!w->kcontrols) {
2426 mutex_unlock(&dapm->card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002427 return -ENOMEM;
Liam Girdwood6acab8c2012-03-07 10:38:26 +00002428 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002429 }
2430
Richard Purdie2b97eab2006-10-06 18:32:18 +02002431 switch(w->id) {
2432 case snd_soc_dapm_switch:
2433 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002434 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002435 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002436 break;
2437 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002438 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002439 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002440 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002441 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002442 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002443 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002444 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002445 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002446 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002447 break;
2448 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002449
2450 /* Read the initial power state from the device */
2451 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002452 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002453 val &= 1 << w->shift;
2454 if (w->invert)
2455 val = !val;
2456
2457 if (val)
2458 w->power = 1;
2459 }
2460
Richard Purdie2b97eab2006-10-06 18:32:18 +02002461 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002462
Mark Brown7508b122011-10-05 12:09:12 +01002463 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002464 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002465 }
2466
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002467 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwood6acab8c2012-03-07 10:38:26 +00002468 mutex_unlock(&dapm->card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002469 return 0;
2470}
2471EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2472
2473/**
2474 * snd_soc_dapm_get_volsw - dapm mixer get callback
2475 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002476 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002477 *
2478 * Callback to get the value of a dapm mixer control.
2479 *
2480 * Returns 0 for success.
2481 */
2482int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2483 struct snd_ctl_elem_value *ucontrol)
2484{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002485 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2486 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002487 struct soc_mixer_control *mc =
2488 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04002489 unsigned int reg = mc->reg;
2490 unsigned int shift = mc->shift;
2491 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002492 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002493 unsigned int invert = mc->invert;
2494 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002495
Richard Purdie2b97eab2006-10-06 18:32:18 +02002496 ucontrol->value.integer.value[0] =
2497 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2498 if (shift != rshift)
2499 ucontrol->value.integer.value[1] =
2500 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2501 if (invert) {
2502 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002503 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002504 if (shift != rshift)
2505 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002506 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002507 }
2508
2509 return 0;
2510}
2511EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2512
2513/**
2514 * snd_soc_dapm_put_volsw - dapm mixer set callback
2515 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002516 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002517 *
2518 * Callback to set the value of a dapm mixer control.
2519 *
2520 * Returns 0 for success.
2521 */
2522int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2523 struct snd_ctl_elem_value *ucontrol)
2524{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002525 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2526 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2527 struct snd_soc_codec *codec = widget->codec;
Liam Girdwood6acab8c2012-03-07 10:38:26 +00002528 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002529 struct soc_mixer_control *mc =
2530 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04002531 unsigned int reg = mc->reg;
2532 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002533 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002534 unsigned int mask = (1 << fls(max)) - 1;
2535 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002536 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002537 int connect, change;
2538 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002539 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002540
2541 val = (ucontrol->value.integer.value[0] & mask);
2542
2543 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002544 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002545 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002546 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002547
Stephen Warrenfafd2172011-04-28 17:38:00 -06002548 if (val)
2549 /* new connection */
2550 connect = invert ? 0 : 1;
2551 else
2552 /* old connection must be powered down */
2553 connect = invert ? 1 : 0;
2554
Liam Girdwood6acab8c2012-03-07 10:38:26 +00002555 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_PCM);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002556
Stephen Warrene9cf7042011-01-27 14:54:05 -07002557 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002558 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002559 for (wi = 0; wi < wlist->num_widgets; wi++) {
2560 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002561
Stephen Warrenfafd2172011-04-28 17:38:00 -06002562 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002563
Stephen Warrenfafd2172011-04-28 17:38:00 -06002564 update.kcontrol = kcontrol;
2565 update.widget = widget;
2566 update.reg = reg;
2567 update.mask = mask;
2568 update.val = val;
2569 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002570
Liam Girdwood55c42612012-03-07 10:38:27 +00002571 soc_dapm_mixer_update_power(widget, kcontrol, connect);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002572
2573 widget->dapm->update = NULL;
2574 }
Mark Brown283375c2009-12-07 18:09:03 +00002575 }
2576
Liam Girdwood6acab8c2012-03-07 10:38:26 +00002577 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002578 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002579}
2580EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2581
2582/**
2583 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2584 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002585 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002586 *
2587 * Callback to get the value of a dapm enumerated double mixer control.
2588 *
2589 * Returns 0 for success.
2590 */
2591int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2592 struct snd_ctl_elem_value *ucontrol)
2593{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002594 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2595 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002596 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002597 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002598
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002599 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002600 ;
2601 val = snd_soc_read(widget->codec, e->reg);
2602 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2603 if (e->shift_l != e->shift_r)
2604 ucontrol->value.enumerated.item[1] =
2605 (val >> e->shift_r) & (bitmask - 1);
2606
2607 return 0;
2608}
2609EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2610
2611/**
2612 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2613 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002614 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002615 *
2616 * Callback to set the value of a dapm enumerated double mixer control.
2617 *
2618 * Returns 0 for success.
2619 */
2620int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2621 struct snd_ctl_elem_value *ucontrol)
2622{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002623 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2624 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2625 struct snd_soc_codec *codec = widget->codec;
Liam Girdwood6acab8c2012-03-07 10:38:26 +00002626 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002627 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002628 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002629 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00002630 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002631 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002632
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002633 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002634 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002635 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002636 return -EINVAL;
2637 mux = ucontrol->value.enumerated.item[0];
2638 val = mux << e->shift_l;
2639 mask = (bitmask - 1) << e->shift_l;
2640 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002641 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002642 return -EINVAL;
2643 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2644 mask |= (bitmask - 1) << e->shift_r;
2645 }
2646
Liam Girdwood6acab8c2012-03-07 10:38:26 +00002647 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_PCM);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002648
Mark Brown3a655772009-10-05 17:23:30 +01002649 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002650 if (change) {
2651 for (wi = 0; wi < wlist->num_widgets; wi++) {
2652 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002653
Stephen Warrenfafd2172011-04-28 17:38:00 -06002654 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002655
Stephen Warrenfafd2172011-04-28 17:38:00 -06002656 update.kcontrol = kcontrol;
2657 update.widget = widget;
2658 update.reg = e->reg;
2659 update.mask = mask;
2660 update.val = val;
2661 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002662
Liam Girdwood55c42612012-03-07 10:38:27 +00002663 soc_dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002664
Stephen Warrenfafd2172011-04-28 17:38:00 -06002665 widget->dapm->update = NULL;
2666 }
2667 }
2668
Liam Girdwood6acab8c2012-03-07 10:38:26 +00002669 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002670 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002671}
2672EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2673
2674/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002675 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2676 * @kcontrol: mixer control
2677 * @ucontrol: control element information
2678 *
2679 * Returns 0 for success.
2680 */
2681int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2682 struct snd_ctl_elem_value *ucontrol)
2683{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002684 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2685 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002686
2687 ucontrol->value.enumerated.item[0] = widget->value;
2688
2689 return 0;
2690}
2691EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2692
2693/**
2694 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2695 * @kcontrol: mixer control
2696 * @ucontrol: control element information
2697 *
2698 * Returns 0 for success.
2699 */
2700int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2701 struct snd_ctl_elem_value *ucontrol)
2702{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002703 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2704 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2705 struct snd_soc_codec *codec = widget->codec;
Liam Girdwood6acab8c2012-03-07 10:38:26 +00002706 struct snd_soc_card *card = codec->card;
Mark Brownd2b247a2009-10-06 15:21:04 +01002707 struct soc_enum *e =
2708 (struct soc_enum *)kcontrol->private_value;
2709 int change;
2710 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002711 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002712
2713 if (ucontrol->value.enumerated.item[0] >= e->max)
2714 return -EINVAL;
2715
Liam Girdwood6acab8c2012-03-07 10:38:26 +00002716 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_PCM);
Mark Brownd2b247a2009-10-06 15:21:04 +01002717
2718 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002719 if (change) {
2720 for (wi = 0; wi < wlist->num_widgets; wi++) {
2721 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002722
Stephen Warrenfafd2172011-04-28 17:38:00 -06002723 widget->value = ucontrol->value.enumerated.item[0];
2724
Liam Girdwood55c42612012-03-07 10:38:27 +00002725 soc_dapm_mux_update_power(widget, kcontrol, change,
Liam Girdwoode002c982011-02-08 11:45:20 +00002726 widget->value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002727 }
2728 }
2729
Liam Girdwood6acab8c2012-03-07 10:38:26 +00002730 mutex_unlock(&card->dapm_mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002731 return ret;
2732}
2733EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2734
2735/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002736 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2737 * callback
2738 * @kcontrol: mixer control
2739 * @ucontrol: control element information
2740 *
2741 * Callback to get the value of a dapm semi enumerated double mixer control.
2742 *
2743 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2744 * used for handling bitfield coded enumeration for example.
2745 *
2746 * Returns 0 for success.
2747 */
2748int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2749 struct snd_ctl_elem_value *ucontrol)
2750{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002751 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2752 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002753 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002754 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002755
2756 reg_val = snd_soc_read(widget->codec, e->reg);
2757 val = (reg_val >> e->shift_l) & e->mask;
2758 for (mux = 0; mux < e->max; mux++) {
2759 if (val == e->values[mux])
2760 break;
2761 }
2762 ucontrol->value.enumerated.item[0] = mux;
2763 if (e->shift_l != e->shift_r) {
2764 val = (reg_val >> e->shift_r) & e->mask;
2765 for (mux = 0; mux < e->max; mux++) {
2766 if (val == e->values[mux])
2767 break;
2768 }
2769 ucontrol->value.enumerated.item[1] = mux;
2770 }
2771
2772 return 0;
2773}
2774EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2775
2776/**
2777 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2778 * callback
2779 * @kcontrol: mixer control
2780 * @ucontrol: control element information
2781 *
2782 * Callback to set the value of a dapm semi enumerated double mixer control.
2783 *
2784 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2785 * used for handling bitfield coded enumeration for example.
2786 *
2787 * Returns 0 for success.
2788 */
2789int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2790 struct snd_ctl_elem_value *ucontrol)
2791{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002792 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2793 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2794 struct snd_soc_codec *codec = widget->codec;
Liam Girdwood6acab8c2012-03-07 10:38:26 +00002795 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002796 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002797 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002798 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002799 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002800 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002801
2802 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2803 return -EINVAL;
2804 mux = ucontrol->value.enumerated.item[0];
2805 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2806 mask = e->mask << e->shift_l;
2807 if (e->shift_l != e->shift_r) {
2808 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2809 return -EINVAL;
2810 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2811 mask |= e->mask << e->shift_r;
2812 }
2813
Liam Girdwood6acab8c2012-03-07 10:38:26 +00002814 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_PCM);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002815
Mark Brown3a655772009-10-05 17:23:30 +01002816 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002817 if (change) {
2818 for (wi = 0; wi < wlist->num_widgets; wi++) {
2819 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002820
Stephen Warrenfafd2172011-04-28 17:38:00 -06002821 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002822
Stephen Warrenfafd2172011-04-28 17:38:00 -06002823 update.kcontrol = kcontrol;
2824 update.widget = widget;
2825 update.reg = e->reg;
2826 update.mask = mask;
2827 update.val = val;
2828 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002829
Liam Girdwood55c42612012-03-07 10:38:27 +00002830 soc_dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002831
Stephen Warrenfafd2172011-04-28 17:38:00 -06002832 widget->dapm->update = NULL;
2833 }
2834 }
2835
Liam Girdwood6acab8c2012-03-07 10:38:26 +00002836 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002837 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002838}
2839EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2840
2841/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002842 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2843 *
2844 * @kcontrol: mixer control
2845 * @uinfo: control element information
2846 *
2847 * Callback to provide information about a pin switch control.
2848 */
2849int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2850 struct snd_ctl_elem_info *uinfo)
2851{
2852 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2853 uinfo->count = 1;
2854 uinfo->value.integer.min = 0;
2855 uinfo->value.integer.max = 1;
2856
2857 return 0;
2858}
2859EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2860
2861/**
2862 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2863 *
2864 * @kcontrol: mixer control
2865 * @ucontrol: Value
2866 */
2867int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2868 struct snd_ctl_elem_value *ucontrol)
2869{
Mark Brown29d829d2012-02-14 17:11:15 -08002870 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002871 const char *pin = (const char *)kcontrol->private_value;
2872
Liam Girdwood6acab8c2012-03-07 10:38:26 +00002873 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_PCM);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002874
2875 ucontrol->value.integer.value[0] =
Mark Brown29d829d2012-02-14 17:11:15 -08002876 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002877
Liam Girdwood6acab8c2012-03-07 10:38:26 +00002878 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002879
2880 return 0;
2881}
2882EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2883
2884/**
2885 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2886 *
2887 * @kcontrol: mixer control
2888 * @ucontrol: Value
2889 */
2890int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2891 struct snd_ctl_elem_value *ucontrol)
2892{
Mark Brown29d829d2012-02-14 17:11:15 -08002893 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002894 const char *pin = (const char *)kcontrol->private_value;
2895
Liam Girdwood6acab8c2012-03-07 10:38:26 +00002896 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_PCM);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002897
2898 if (ucontrol->value.integer.value[0])
Mark Brown29d829d2012-02-14 17:11:15 -08002899 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002900 else
Mark Brown29d829d2012-02-14 17:11:15 -08002901 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002902
Liam Girdwood6acab8c2012-03-07 10:38:26 +00002903 mutex_unlock(&card->dapm_mutex);
2904
Mark Brown29d829d2012-02-14 17:11:15 -08002905 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002906 return 0;
2907}
2908EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2909
2910/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002911 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002912 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002913 * @widget: widget template
2914 *
2915 * Creates a new dapm control based upon the template.
2916 *
2917 * Returns 0 for success else error.
2918 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002919int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002920 const struct snd_soc_dapm_widget *widget)
2921{
2922 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002923 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002924
2925 if ((w = dapm_cnew_widget(widget)) == NULL)
2926 return -ENOMEM;
2927
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002928 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002929 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002930 name_len += 1 + strlen(dapm->codec->name_prefix);
2931 w->name = kmalloc(name_len, GFP_KERNEL);
2932 if (w->name == NULL) {
2933 kfree(w);
2934 return -ENOMEM;
2935 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002936 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002937 snprintf(w->name, name_len, "%s %s",
2938 dapm->codec->name_prefix, widget->name);
2939 else
2940 snprintf(w->name, name_len, "%s", widget->name);
2941
Mark Brown7ca3a182011-10-08 14:04:50 +01002942 switch (w->id) {
2943 case snd_soc_dapm_switch:
2944 case snd_soc_dapm_mixer:
2945 case snd_soc_dapm_mixer_named_ctl:
2946 w->power_check = dapm_generic_check_power;
2947 break;
2948 case snd_soc_dapm_mux:
2949 case snd_soc_dapm_virt_mux:
2950 case snd_soc_dapm_value_mux:
2951 w->power_check = dapm_generic_check_power;
2952 break;
2953 case snd_soc_dapm_adc:
2954 case snd_soc_dapm_aif_out:
2955 w->power_check = dapm_adc_check_power;
2956 break;
2957 case snd_soc_dapm_dac:
2958 case snd_soc_dapm_aif_in:
2959 w->power_check = dapm_dac_check_power;
2960 break;
2961 case snd_soc_dapm_pga:
2962 case snd_soc_dapm_out_drv:
2963 case snd_soc_dapm_input:
2964 case snd_soc_dapm_output:
2965 case snd_soc_dapm_micbias:
2966 case snd_soc_dapm_spk:
2967 case snd_soc_dapm_hp:
2968 case snd_soc_dapm_mic:
2969 case snd_soc_dapm_line:
2970 w->power_check = dapm_generic_check_power;
2971 break;
2972 case snd_soc_dapm_supply:
2973 w->power_check = dapm_supply_check_power;
2974 break;
2975 default:
2976 w->power_check = dapm_always_on_check_power;
2977 break;
2978 }
2979
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002980 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002981 w->dapm = dapm;
2982 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01002983 w->platform = dapm->platform;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002984 w->dai = dapm->dai;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002985 INIT_LIST_HEAD(&w->sources);
2986 INIT_LIST_HEAD(&w->sinks);
2987 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01002988 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002989 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002990
2991 /* machine layer set ups unconnected pins and insertions */
2992 w->connected = 1;
2993 return 0;
2994}
2995EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2996
2997/**
Mark Brown4ba13272008-05-13 14:51:19 +02002998 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002999 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003000 * @widget: widget array
3001 * @num: number of widgets
3002 *
3003 * Creates new DAPM controls based upon the templates.
3004 *
3005 * Returns 0 for success else error.
3006 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003007int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003008 const struct snd_soc_dapm_widget *widget,
3009 int num)
3010{
3011 int i, ret;
3012
Liam Girdwood6acab8c2012-03-07 10:38:26 +00003013 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003014 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003015 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00003016 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003017 dev_err(dapm->dev,
3018 "ASoC: Failed to create DAPM control %s: %d\n",
3019 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02003020 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003021 }
Mark Brown4ba13272008-05-13 14:51:19 +02003022 widget++;
3023 }
Liam Girdwood6acab8c2012-03-07 10:38:26 +00003024 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown4ba13272008-05-13 14:51:19 +02003025 return 0;
3026}
3027EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3028
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003029static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003030 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003031{
3032 struct snd_soc_dapm_widget *w;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003033
Liam Girdwood13e13a32011-01-31 21:30:52 +00003034 if (!dapm)
3035 return;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003036
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003037 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003038 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003039 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003040 continue;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003041 dev_vdbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003042 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003043 if (strstr(w->sname, stream)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003044 dapm_mark_dirty(w, "stream event");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003045 switch(event) {
3046 case SND_SOC_DAPM_STREAM_START:
3047 w->active = 1;
3048 break;
3049 case SND_SOC_DAPM_STREAM_STOP:
3050 w->active = 0;
3051 break;
3052 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003053 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003054 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003055 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3056 break;
3057 }
3058 }
3059 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003060
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003061 dapm_power_widgets(dapm, event);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003062
Liam Girdwood670ff442011-03-30 23:27:27 +01003063 /* do we need to notify any clients that DAPM stream is complete */
3064 if (dapm->stream_event)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003065 dapm->stream_event(dapm, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003066}
3067
Steve Mucklef132c6c2012-06-06 18:30:57 -07003068static void widget_stream_event(struct snd_soc_dapm_context *dapm,
3069 struct snd_soc_dapm_widget *w, int event)
3070{
Mark Brownfe360682012-02-16 19:43:20 -08003071 if (!w)
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003072 return;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003073
Mark Brownfe360682012-02-16 19:43:20 -08003074 dapm_mark_dirty(w, "stream event");
3075
Steve Mucklef132c6c2012-06-06 18:30:57 -07003076 switch(event) {
Mark Brownfe360682012-02-16 19:43:20 -08003077 case SND_SOC_DAPM_STREAM_START:
3078 w->active = 1;
3079 break;
3080 case SND_SOC_DAPM_STREAM_STOP:
3081 w->active = 0;
3082 break;
3083 case SND_SOC_DAPM_STREAM_SUSPEND:
3084 case SND_SOC_DAPM_STREAM_RESUME:
3085 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3086 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3087 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003088 }
3089
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003090 dapm_power_widgets(dapm, event);
3091}
3092
Steve Mucklef132c6c2012-06-06 18:30:57 -07003093void snd_soc_dapm_rtd_stream_event(struct snd_soc_pcm_runtime *rtd,
3094 int stream, int event)
3095{
3096 struct snd_soc_dapm_context *pdapm = &rtd->platform->dapm;
3097 struct snd_soc_dapm_context *cdapm = &rtd->codec->dapm;
Gopikrishnaiah Anandan8848f672013-12-23 16:49:04 -08003098 struct snd_soc_card *card = rtd->card;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003099
3100 dev_dbg(rtd->dev, "rtd stream %d event %d\n", stream, event);
3101
Gopikrishnaiah Anandan8848f672013-12-23 16:49:04 -08003102 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003103 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3104 widget_stream_event(pdapm, rtd->cpu_dai->playback_aif, event);
3105 widget_stream_event(cdapm, rtd->codec_dai->playback_aif, event);
3106 } else {
3107 widget_stream_event(pdapm, rtd->cpu_dai->capture_aif, event);
3108 widget_stream_event(cdapm, rtd->codec_dai->capture_aif, event);
3109 }
Gopikrishnaiah Anandan8848f672013-12-23 16:49:04 -08003110 mutex_unlock(&card->dapm_mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003111
3112 /* do we need to notify any clients that DAPM stream is complete */
3113 if (pdapm->stream_event)
3114 pdapm->stream_event(pdapm, event);
3115 if (cdapm->stream_event)
3116 cdapm->stream_event(cdapm, event);
3117}
3118EXPORT_SYMBOL_GPL(snd_soc_dapm_rtd_stream_event);
3119
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003120/**
3121 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3122 * @rtd: PCM runtime data
3123 * @stream: stream name
3124 * @event: stream event
3125 *
3126 * Sends a stream event to the dapm core. The core then makes any
3127 * necessary widget power changes.
3128 *
3129 * Returns 0 for success else error.
3130 */
3131int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
3132 const char *stream, int event)
3133{
Gopikrishnaiah Anandan8848f672013-12-23 16:49:04 -08003134 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003135 struct snd_soc_codec *codec = rtd->codec;
3136
3137 if (stream == NULL)
3138 return 0;
Gopikrishnaiah Anandan8848f672013-12-23 16:49:04 -08003139 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_PCM);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003140 soc_dapm_stream_event(&codec->dapm, stream, event);
Gopikrishnaiah Anandan8848f672013-12-23 16:49:04 -08003141 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003142 return 0;
3143}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003144
Steve Mucklef132c6c2012-06-06 18:30:57 -07003145void snd_soc_dapm_codec_stream_event(struct snd_soc_codec *codec,
3146 const char *stream, int event)
3147{
3148// mutex_lock(&codec->card->dapm_mutex);
3149 soc_dapm_stream_event(&codec->dapm, stream, event);
3150// mutex_unlock(&codec->card->dapm_mutex);
3151}
3152EXPORT_SYMBOL(snd_soc_dapm_codec_stream_event);
3153
Richard Purdie2b97eab2006-10-06 18:32:18 +02003154/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003155 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003156 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003157 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003158 *
Mark Brown74b8f952009-06-06 11:26:15 +01003159 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003160 * a valid audio route and active audio stream.
3161 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3162 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003163 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003164int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003165{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003166 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003167}
Liam Girdwooda5302182008-07-07 13:35:17 +01003168EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003169
3170/**
Mark Brownda341832010-03-15 19:23:37 +00003171 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003172 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003173 * @pin: pin name
3174 *
3175 * Enables input/output pin regardless of any other state. This is
3176 * intended for use with microphone bias supplies used in microphone
3177 * jack detection.
3178 *
3179 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3180 * do any widget power switching.
3181 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003182int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3183 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003184{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003185 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003186
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003187 if (!w) {
3188 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3189 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003190 }
3191
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003192 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
3193 w->connected = 1;
3194 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003195 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003196
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003197 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003198}
3199EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3200
3201/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003202 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003203 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003204 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003205 *
Mark Brown74b8f952009-06-06 11:26:15 +01003206 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003207 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3208 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003209 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003210int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3211 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003212{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003213 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003214}
3215EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3216
3217/**
Mark Brown5817b522008-09-24 11:23:11 +01003218 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003219 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003220 * @pin: pin name
3221 *
3222 * Marks the specified pin as being not connected, disabling it along
3223 * any parent or child widgets. At present this is identical to
3224 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3225 * additional things such as disabling controls which only affect
3226 * paths through the pin.
3227 *
3228 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3229 * do any widget power switching.
3230 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003231int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003232{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003233 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003234}
3235EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3236
3237/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003238 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003239 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003240 * @pin: audio signal pin endpoint (or start point)
3241 *
3242 * Get audio pin status - connected or disconnected.
3243 *
3244 * Returns 1 for connected otherwise 0.
3245 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003246int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3247 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003248{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003249 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003250
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003251 if (w)
3252 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003253
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003254 return 0;
3255}
Liam Girdwooda5302182008-07-07 13:35:17 +01003256EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003257
3258/**
Mark Brown1547aba2010-05-07 21:11:40 +01003259 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003260 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003261 * @pin: audio signal pin endpoint (or start point)
3262 *
3263 * Mark the given endpoint or pin as ignoring suspend. When the
3264 * system is disabled a path between two endpoints flagged as ignoring
3265 * suspend will not be disabled. The path must already be enabled via
3266 * normal means at suspend time, it will not be turned on if it was not
3267 * already enabled.
3268 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003269int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3270 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003271{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003272 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003273
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003274 if (!w) {
3275 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3276 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003277 }
3278
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003279 w->ignore_suspend = 1;
3280
3281 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003282}
3283EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3284
Stephen Warren16332812011-11-23 12:42:04 -07003285static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3286 struct snd_soc_dapm_widget *w)
3287{
3288 struct snd_soc_dapm_path *p;
3289
3290 list_for_each_entry(p, &card->paths, list) {
3291 if ((p->source == w) || (p->sink == w)) {
3292 dev_dbg(card->dev,
3293 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3294 p->source->name, p->source->id, p->source->dapm,
3295 p->sink->name, p->sink->id, p->sink->dapm);
3296
3297 /* Connected to something other than the codec */
3298 if (p->source->dapm != p->sink->dapm)
3299 return true;
3300 /*
3301 * Loopback connection from codec external pin to
3302 * codec external pin
3303 */
3304 if (p->sink->id == snd_soc_dapm_input) {
3305 switch (p->source->id) {
3306 case snd_soc_dapm_output:
3307 case snd_soc_dapm_micbias:
3308 return true;
3309 default:
3310 break;
3311 }
3312 }
3313 }
3314 }
3315
3316 return false;
3317}
3318
3319/**
3320 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3321 * @codec: The codec whose pins should be processed
3322 *
3323 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3324 * which are unused. Pins are used if they are connected externally to the
3325 * codec, whether that be to some other device, or a loop-back connection to
3326 * the codec itself.
3327 */
3328void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3329{
3330 struct snd_soc_card *card = codec->card;
3331 struct snd_soc_dapm_context *dapm = &codec->dapm;
3332 struct snd_soc_dapm_widget *w;
3333
Mark Browna094b802011-11-27 19:42:20 +00003334 dev_dbg(codec->dev, "Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003335 &card->dapm, &codec->dapm);
3336
3337 list_for_each_entry(w, &card->widgets, list) {
3338 if (w->dapm != dapm)
3339 continue;
3340 switch (w->id) {
3341 case snd_soc_dapm_input:
3342 case snd_soc_dapm_output:
3343 case snd_soc_dapm_micbias:
Mark Browna094b802011-11-27 19:42:20 +00003344 dev_dbg(codec->dev, "Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003345 w->name);
3346 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003347 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003348 "... Not in map; disabling\n");
3349 snd_soc_dapm_nc_pin(dapm, w->name);
3350 }
3351 break;
3352 default:
3353 break;
3354 }
3355 }
3356}
3357
Mark Brown1547aba2010-05-07 21:11:40 +01003358/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003359 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003360 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003361 *
3362 * Free all dapm widgets and resources.
3363 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003364void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003365{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003366 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003367 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003368 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003369 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003370}
3371EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3372
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003373static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003374{
Mark Brown51737472009-06-22 13:16:51 +01003375 struct snd_soc_dapm_widget *w;
3376 LIST_HEAD(down_list);
3377 int powerdown = 0;
3378
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003379 list_for_each_entry(w, &dapm->card->widgets, list) {
3380 if (w->dapm != dapm)
3381 continue;
Mark Brown51737472009-06-22 13:16:51 +01003382 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003383 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003384 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003385 powerdown = 1;
3386 }
3387 }
3388
3389 /* If there were no widgets to power down we're already in
3390 * standby.
3391 */
3392 if (powerdown) {
Mark Browned5a4c42011-02-18 11:12:42 -08003393 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00003394 dapm_seq_run(dapm, &down_list, 0, false);
Mark Browned5a4c42011-02-18 11:12:42 -08003395 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003396 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003397}
Mark Brown51737472009-06-22 13:16:51 +01003398
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003399/*
3400 * snd_soc_dapm_shutdown - callback for system shutdown
3401 */
3402void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3403{
3404 struct snd_soc_codec *codec;
3405
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003406 list_for_each_entry(codec, &card->codec_dev_list, list) {
3407 soc_dapm_shutdown_codec(&codec->dapm);
Mark Browned5a4c42011-02-18 11:12:42 -08003408 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003409 }
Mark Brown51737472009-06-22 13:16:51 +01003410}
3411
Richard Purdie2b97eab2006-10-06 18:32:18 +02003412/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003413MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003414MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3415MODULE_LICENSE("GPL");