blob: 5ac6434163041ce46a4f092329ddfeb32da1b075 [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,
Mark Brown38357ab2009-06-06 19:03:23 +010081 [snd_soc_dapm_adc] = 1,
82 [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 Girdwood40f02cd2012-02-06 16:05:14 +00001869int snd_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) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001907 if (widget->platform) {
1908 soc_dpcm_runtime_update(widget);
1909 } else {
1910 dapm_mark_dirty(widget, "mux change");
Patrick Laia8d41ee2011-09-12 12:42:04 -07001911 dapm_power_widgets(widget->dapm,
Steve Mucklef132c6c2012-06-06 18:30:57 -07001912 SND_SOC_DAPM_STREAM_NOP);
1913 }
Mark Browndb432b42011-10-03 21:06:40 +01001914 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001915
1916 return 0;
1917}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001918EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001919
Milan plzik1b075e32008-01-10 14:39:46 +01001920/* test and update the power status of a mixer or switch widget */
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001921int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001922 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001923{
1924 struct snd_soc_dapm_path *path;
1925 int found = 0;
1926
Milan plzik1b075e32008-01-10 14:39:46 +01001927 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001928 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001929 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001930 return -ENODEV;
1931
Richard Purdie2b97eab2006-10-06 18:32:18 +02001932 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001933 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001934 if (path->kcontrol != kcontrol)
1935 continue;
1936
1937 /* found, now check type */
1938 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001939 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01001940 dapm_mark_dirty(path->source, "mixer connection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001941 }
1942
Mark Browndb432b42011-10-03 21:06:40 +01001943 if (found) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001944 if (widget->platform) {
1945 soc_dpcm_runtime_update(widget);
1946 } else {
Mark Brown75c1f892011-10-04 22:28:08 +01001947 dapm_mark_dirty(widget, "mixer update");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001948 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001949 }
1950
Mark Browndb432b42011-10-03 21:06:40 +01001951 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001952
1953 return 0;
1954}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001955EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001956
1957/* show dapm widget status in sys fs */
1958static ssize_t dapm_widget_show(struct device *dev,
1959 struct device_attribute *attr, char *buf)
1960{
Mark Brown36ae1a92012-01-06 17:12:45 -08001961 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001962 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001963 struct snd_soc_dapm_widget *w;
1964 int count = 0;
1965 char *state = "not set";
1966
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001967 list_for_each_entry(w, &codec->card->widgets, list) {
1968 if (w->dapm != &codec->dapm)
1969 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001970
1971 /* only display widgets that burnm power */
1972 switch (w->id) {
1973 case snd_soc_dapm_hp:
1974 case snd_soc_dapm_mic:
1975 case snd_soc_dapm_spk:
1976 case snd_soc_dapm_line:
1977 case snd_soc_dapm_micbias:
1978 case snd_soc_dapm_dac:
1979 case snd_soc_dapm_adc:
1980 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001981 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001982 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001983 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001984 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001985 if (w->name)
1986 count += sprintf(buf + count, "%s: %s\n",
1987 w->name, w->power ? "On":"Off");
1988 break;
1989 default:
1990 break;
1991 }
1992 }
1993
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001994 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001995 case SND_SOC_BIAS_ON:
1996 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001997 break;
Mark Brown0be98982008-05-19 12:31:28 +02001998 case SND_SOC_BIAS_PREPARE:
1999 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002000 break;
Mark Brown0be98982008-05-19 12:31:28 +02002001 case SND_SOC_BIAS_STANDBY:
2002 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002003 break;
Mark Brown0be98982008-05-19 12:31:28 +02002004 case SND_SOC_BIAS_OFF:
2005 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002006 break;
2007 }
2008 count += sprintf(buf + count, "PM State: %s\n", state);
2009
2010 return count;
2011}
2012
2013static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2014
2015int snd_soc_dapm_sys_add(struct device *dev)
2016{
Troy Kisky12ef1932008-10-13 17:42:14 -07002017 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002018}
2019
2020static void snd_soc_dapm_sys_remove(struct device *dev)
2021{
Mark Brownaef90842009-05-16 17:53:16 +01002022 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002023}
2024
2025/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002026static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002027{
2028 struct snd_soc_dapm_widget *w, *next_w;
2029 struct snd_soc_dapm_path *p, *next_p;
2030
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002031 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2032 if (w->dapm != dapm)
2033 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002034 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002035 /*
2036 * remove source and sink paths associated to this widget.
2037 * While removing the path, remove reference to it from both
2038 * source and sink widgets so that path is removed only once.
2039 */
2040 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
2041 list_del(&p->list_sink);
2042 list_del(&p->list_source);
2043 list_del(&p->list);
2044 kfree(p->long_name);
2045 kfree(p);
2046 }
2047 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
2048 list_del(&p->list_sink);
2049 list_del(&p->list_source);
2050 list_del(&p->list);
2051 kfree(p->long_name);
2052 kfree(p);
2053 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002054 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002055 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002056 kfree(w);
2057 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002058}
2059
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002060static struct snd_soc_dapm_widget *dapm_find_widget(
2061 struct snd_soc_dapm_context *dapm, const char *pin,
2062 bool search_other_contexts)
2063{
2064 struct snd_soc_dapm_widget *w;
2065 struct snd_soc_dapm_widget *fallback = NULL;
2066
2067 list_for_each_entry(w, &dapm->card->widgets, list) {
2068 if (!strcmp(w->name, pin)) {
2069 if (w->dapm == dapm)
2070 return w;
2071 else
2072 fallback = w;
2073 }
2074 }
2075
2076 if (search_other_contexts)
2077 return fallback;
2078
2079 return NULL;
2080}
2081
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002082static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002083 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002084{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002085 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002086
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002087 if (!w) {
2088 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2089 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002090 }
2091
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002092 w->connected = status;
2093 if (status == 0)
2094 w->force = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002095 dapm_mark_dirty(w, "pin configuration");
Mark Brown0d867332011-04-06 11:38:14 +09002096
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002097 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002098}
2099
Richard Purdie2b97eab2006-10-06 18:32:18 +02002100/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002101 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002102 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002103 *
2104 * Walks all dapm audio paths and powers widgets according to their
2105 * stream or path usage.
2106 *
2107 * Returns 0 for success.
2108 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002109int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002110{
Mark Brown4f4c0072011-10-07 14:29:19 +01002111 /*
2112 * Suppress early reports (eg, jacks syncing their state) to avoid
2113 * silly DAPM runs during card startup.
2114 */
2115 if (!dapm->card || !dapm->card->instantiated)
2116 return 0;
2117
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002118 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002119}
Liam Girdwooda5302182008-07-07 13:35:17 +01002120EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002121
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002122static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01002123 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002124{
2125 struct snd_soc_dapm_path *path;
2126 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002127 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002128 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01002129 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002130 const char *source;
2131 char prefixed_sink[80];
2132 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002133 int ret = 0;
2134
Mark Brown88e8b9a2011-03-02 18:18:24 +00002135 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002136 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2137 dapm->codec->name_prefix, route->sink);
2138 sink = prefixed_sink;
2139 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2140 dapm->codec->name_prefix, route->source);
2141 source = prefixed_source;
2142 } else {
2143 sink = route->sink;
2144 source = route->source;
2145 }
2146
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002147 /*
2148 * find src and dest widgets over all widgets but favor a widget from
2149 * current DAPM context
2150 */
2151 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002152 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002153 wtsink = w;
2154 if (w->dapm == dapm)
2155 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002156 continue;
2157 }
2158 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002159 wtsource = w;
2160 if (w->dapm == dapm)
2161 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002162 }
2163 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002164 /* use widget from another DAPM context if not found from this */
2165 if (!wsink)
2166 wsink = wtsink;
2167 if (!wsource)
2168 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002169
2170 if (wsource == NULL || wsink == NULL)
2171 return -ENODEV;
2172
2173 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2174 if (!path)
2175 return -ENOMEM;
2176
2177 path->source = wsource;
2178 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01002179 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002180 INIT_LIST_HEAD(&path->list);
2181 INIT_LIST_HEAD(&path->list_source);
2182 INIT_LIST_HEAD(&path->list_sink);
2183
2184 /* check for external widgets */
2185 if (wsink->id == snd_soc_dapm_input) {
2186 if (wsource->id == snd_soc_dapm_micbias ||
2187 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002188 wsource->id == snd_soc_dapm_line ||
2189 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002190 wsink->ext = 1;
2191 }
2192 if (wsource->id == snd_soc_dapm_output) {
2193 if (wsink->id == snd_soc_dapm_spk ||
2194 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002195 wsink->id == snd_soc_dapm_line ||
2196 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002197 wsource->ext = 1;
2198 }
2199
2200 /* connect static paths */
2201 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002202 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002203 list_add(&path->list_sink, &wsink->sources);
2204 list_add(&path->list_source, &wsource->sinks);
2205 path->connect = 1;
2206 return 0;
2207 }
2208
2209 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002210 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002211 case snd_soc_dapm_adc:
2212 case snd_soc_dapm_dac:
2213 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002214 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002215 case snd_soc_dapm_input:
2216 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002217 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002218 case snd_soc_dapm_micbias:
2219 case snd_soc_dapm_vmid:
2220 case snd_soc_dapm_pre:
2221 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002222 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002223 case snd_soc_dapm_aif_in:
2224 case snd_soc_dapm_aif_out:
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 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002231 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002232 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002233 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002234 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002235 if (ret != 0)
2236 goto err;
2237 break;
2238 case snd_soc_dapm_switch:
2239 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002240 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002241 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002242 if (ret != 0)
2243 goto err;
2244 break;
2245 case snd_soc_dapm_hp:
2246 case snd_soc_dapm_mic:
2247 case snd_soc_dapm_line:
2248 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002249 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002250 list_add(&path->list_sink, &wsink->sources);
2251 list_add(&path->list_source, &wsource->sinks);
2252 path->connect = 0;
2253 return 0;
2254 }
2255 return 0;
2256
2257err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002258 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2259 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002260 kfree(path);
2261 return ret;
2262}
Mark Brown105f1c22008-05-13 14:52:19 +02002263
2264/**
Mark Brown105f1c22008-05-13 14:52:19 +02002265 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002266 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002267 * @route: audio routes
2268 * @num: number of routes
2269 *
2270 * Connects 2 dapm widgets together via a named audio path. The sink is
2271 * the widget receiving the audio signal, whilst the source is the sender
2272 * of the audio signal.
2273 *
2274 * Returns 0 for success else error. On error all resources can be freed
2275 * with a call to snd_soc_card_free().
2276 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002277int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002278 const struct snd_soc_dapm_route *route, int num)
2279{
2280 int i, ret;
2281
2282 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002283 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02002284 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002285 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2286 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02002287 return ret;
2288 }
2289 route++;
2290 }
2291
2292 return 0;
2293}
2294EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2295
Mark Brownbf3a9e12011-06-13 16:42:29 +01002296static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2297 const struct snd_soc_dapm_route *route)
2298{
2299 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2300 route->source,
2301 true);
2302 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2303 route->sink,
2304 true);
2305 struct snd_soc_dapm_path *path;
2306 int count = 0;
2307
2308 if (!source) {
2309 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2310 route->source);
2311 return -ENODEV;
2312 }
2313
2314 if (!sink) {
2315 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2316 route->sink);
2317 return -ENODEV;
2318 }
2319
2320 if (route->control || route->connected)
2321 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2322 route->source, route->sink);
2323
2324 list_for_each_entry(path, &source->sinks, list_source) {
2325 if (path->sink == sink) {
2326 path->weak = 1;
2327 count++;
2328 }
2329 }
2330
2331 if (count == 0)
2332 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2333 route->source, route->sink);
2334 if (count > 1)
2335 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2336 count, route->source, route->sink);
2337
2338 return 0;
2339}
2340
2341/**
2342 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2343 * @dapm: DAPM context
2344 * @route: audio routes
2345 * @num: number of routes
2346 *
2347 * Mark existing routes matching those specified in the passed array
2348 * as being weak, meaning that they are ignored for the purpose of
2349 * power decisions. The main intended use case is for sidetone paths
2350 * which couple audio between other independent paths if they are both
2351 * active in order to make the combination work better at the user
2352 * level but which aren't intended to be "used".
2353 *
2354 * Note that CODEC drivers should not use this as sidetone type paths
2355 * can frequently also be used as bypass paths.
2356 */
2357int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2358 const struct snd_soc_dapm_route *route, int num)
2359{
2360 int i, err;
2361 int ret = 0;
2362
2363 for (i = 0; i < num; i++) {
2364 err = snd_soc_dapm_weak_route(dapm, route);
2365 if (err)
2366 ret = err;
2367 route++;
2368 }
2369
2370 return ret;
2371}
2372EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2373
Mark Brown105f1c22008-05-13 14:52:19 +02002374/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002375 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002376 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002377 *
2378 * Checks the codec for any new dapm widgets and creates them if found.
2379 *
2380 * Returns 0 for success.
2381 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002382int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002383{
2384 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002385 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002386
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002387 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002388 {
2389 if (w->new)
2390 continue;
2391
Stephen Warrenfad59882011-04-28 17:37:59 -06002392 if (w->num_kcontrols) {
2393 w->kcontrols = kzalloc(w->num_kcontrols *
2394 sizeof(struct snd_kcontrol *),
2395 GFP_KERNEL);
2396 if (!w->kcontrols)
2397 return -ENOMEM;
2398 }
2399
Richard Purdie2b97eab2006-10-06 18:32:18 +02002400 switch(w->id) {
2401 case snd_soc_dapm_switch:
2402 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002403 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002404 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002405 break;
2406 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002407 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002408 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002409 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002410 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002411 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002412 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002413 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002414 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002415 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002416 break;
2417 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002418
2419 /* Read the initial power state from the device */
2420 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002421 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002422 val &= 1 << w->shift;
2423 if (w->invert)
2424 val = !val;
2425
2426 if (val)
2427 w->power = 1;
2428 }
2429
Richard Purdie2b97eab2006-10-06 18:32:18 +02002430 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002431
Mark Brown7508b122011-10-05 12:09:12 +01002432 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002433 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002434 }
2435
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002436 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002437 return 0;
2438}
2439EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2440
2441/**
2442 * snd_soc_dapm_get_volsw - dapm mixer get callback
2443 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002444 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002445 *
2446 * Callback to get the value of a dapm mixer control.
2447 *
2448 * Returns 0 for success.
2449 */
2450int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2451 struct snd_ctl_elem_value *ucontrol)
2452{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002453 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2454 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002455 struct soc_mixer_control *mc =
2456 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04002457 unsigned int reg = mc->reg;
2458 unsigned int shift = mc->shift;
2459 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002460 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002461 unsigned int invert = mc->invert;
2462 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002463
Richard Purdie2b97eab2006-10-06 18:32:18 +02002464 ucontrol->value.integer.value[0] =
2465 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2466 if (shift != rshift)
2467 ucontrol->value.integer.value[1] =
2468 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2469 if (invert) {
2470 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002471 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002472 if (shift != rshift)
2473 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002474 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002475 }
2476
2477 return 0;
2478}
2479EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2480
2481/**
2482 * snd_soc_dapm_put_volsw - dapm mixer set callback
2483 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002484 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002485 *
2486 * Callback to set the value of a dapm mixer control.
2487 *
2488 * Returns 0 for success.
2489 */
2490int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2491 struct snd_ctl_elem_value *ucontrol)
2492{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002493 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2494 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2495 struct snd_soc_codec *codec = widget->codec;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002496 struct soc_mixer_control *mc =
2497 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04002498 unsigned int reg = mc->reg;
2499 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002500 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002501 unsigned int mask = (1 << fls(max)) - 1;
2502 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002503 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002504 int connect, change;
2505 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002506 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002507
2508 val = (ucontrol->value.integer.value[0] & mask);
2509
2510 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002511 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002512 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002513 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002514
Stephen Warrenfafd2172011-04-28 17:38:00 -06002515 if (val)
2516 /* new connection */
2517 connect = invert ? 0 : 1;
2518 else
2519 /* old connection must be powered down */
2520 connect = invert ? 1 : 0;
2521
2522 mutex_lock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002523
Stephen Warrene9cf7042011-01-27 14:54:05 -07002524 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002525 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002526 for (wi = 0; wi < wlist->num_widgets; wi++) {
2527 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002528
Stephen Warrenfafd2172011-04-28 17:38:00 -06002529 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002530
Stephen Warrenfafd2172011-04-28 17:38:00 -06002531 update.kcontrol = kcontrol;
2532 update.widget = widget;
2533 update.reg = reg;
2534 update.mask = mask;
2535 update.val = val;
2536 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002537
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002538 snd_soc_dapm_mixer_update_power(widget, kcontrol, connect);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002539
2540 widget->dapm->update = NULL;
2541 }
Mark Brown283375c2009-12-07 18:09:03 +00002542 }
2543
Stephen Warrenfafd2172011-04-28 17:38:00 -06002544 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002545 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002546}
2547EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2548
2549/**
2550 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2551 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002552 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002553 *
2554 * Callback to get the value of a dapm enumerated double mixer control.
2555 *
2556 * Returns 0 for success.
2557 */
2558int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2559 struct snd_ctl_elem_value *ucontrol)
2560{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002561 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2562 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002563 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002564 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002565
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002566 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002567 ;
2568 val = snd_soc_read(widget->codec, e->reg);
2569 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2570 if (e->shift_l != e->shift_r)
2571 ucontrol->value.enumerated.item[1] =
2572 (val >> e->shift_r) & (bitmask - 1);
2573
2574 return 0;
2575}
2576EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2577
2578/**
2579 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2580 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002581 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002582 *
2583 * Callback to set the value of a dapm enumerated double mixer control.
2584 *
2585 * Returns 0 for success.
2586 */
2587int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2588 struct snd_ctl_elem_value *ucontrol)
2589{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002590 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2591 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2592 struct snd_soc_codec *codec = widget->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002593 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002594 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002595 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00002596 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002597 int wi;
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 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002601 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002602 return -EINVAL;
2603 mux = ucontrol->value.enumerated.item[0];
2604 val = mux << e->shift_l;
2605 mask = (bitmask - 1) << e->shift_l;
2606 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002607 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002608 return -EINVAL;
2609 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2610 mask |= (bitmask - 1) << e->shift_r;
2611 }
2612
Stephen Warrenfafd2172011-04-28 17:38:00 -06002613 mutex_lock(&codec->mutex);
2614
Mark Brown3a655772009-10-05 17:23:30 +01002615 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002616 if (change) {
2617 for (wi = 0; wi < wlist->num_widgets; wi++) {
2618 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002619
Stephen Warrenfafd2172011-04-28 17:38:00 -06002620 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002621
Stephen Warrenfafd2172011-04-28 17:38:00 -06002622 update.kcontrol = kcontrol;
2623 update.widget = widget;
2624 update.reg = e->reg;
2625 update.mask = mask;
2626 update.val = val;
2627 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002628
Liam Girdwoode002c982011-02-08 11:45:20 +00002629 snd_soc_dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002630
Stephen Warrenfafd2172011-04-28 17:38:00 -06002631 widget->dapm->update = NULL;
2632 }
2633 }
2634
2635 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002636 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002637}
2638EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2639
2640/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002641 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2642 * @kcontrol: mixer control
2643 * @ucontrol: control element information
2644 *
2645 * Returns 0 for success.
2646 */
2647int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2648 struct snd_ctl_elem_value *ucontrol)
2649{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002650 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2651 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002652
2653 ucontrol->value.enumerated.item[0] = widget->value;
2654
2655 return 0;
2656}
2657EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2658
2659/**
2660 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2661 * @kcontrol: mixer control
2662 * @ucontrol: control element information
2663 *
2664 * Returns 0 for success.
2665 */
2666int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2667 struct snd_ctl_elem_value *ucontrol)
2668{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002669 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2670 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2671 struct snd_soc_codec *codec = widget->codec;
Mark Brownd2b247a2009-10-06 15:21:04 +01002672 struct soc_enum *e =
2673 (struct soc_enum *)kcontrol->private_value;
2674 int change;
2675 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002676 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002677
2678 if (ucontrol->value.enumerated.item[0] >= e->max)
2679 return -EINVAL;
2680
Stephen Warrenfafd2172011-04-28 17:38:00 -06002681 mutex_lock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002682
2683 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002684 if (change) {
2685 for (wi = 0; wi < wlist->num_widgets; wi++) {
2686 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002687
Stephen Warrenfafd2172011-04-28 17:38:00 -06002688 widget->value = ucontrol->value.enumerated.item[0];
2689
Liam Girdwoode002c982011-02-08 11:45:20 +00002690 snd_soc_dapm_mux_update_power(widget, kcontrol, change,
2691 widget->value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002692 }
2693 }
2694
2695 mutex_unlock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002696 return ret;
2697}
2698EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2699
2700/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002701 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2702 * callback
2703 * @kcontrol: mixer control
2704 * @ucontrol: control element information
2705 *
2706 * Callback to get the value of a dapm semi enumerated double mixer control.
2707 *
2708 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2709 * used for handling bitfield coded enumeration for example.
2710 *
2711 * Returns 0 for success.
2712 */
2713int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2714 struct snd_ctl_elem_value *ucontrol)
2715{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002716 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2717 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002718 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002719 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002720
2721 reg_val = snd_soc_read(widget->codec, e->reg);
2722 val = (reg_val >> e->shift_l) & e->mask;
2723 for (mux = 0; mux < e->max; mux++) {
2724 if (val == e->values[mux])
2725 break;
2726 }
2727 ucontrol->value.enumerated.item[0] = mux;
2728 if (e->shift_l != e->shift_r) {
2729 val = (reg_val >> e->shift_r) & e->mask;
2730 for (mux = 0; mux < e->max; mux++) {
2731 if (val == e->values[mux])
2732 break;
2733 }
2734 ucontrol->value.enumerated.item[1] = mux;
2735 }
2736
2737 return 0;
2738}
2739EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2740
2741/**
2742 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2743 * callback
2744 * @kcontrol: mixer control
2745 * @ucontrol: control element information
2746 *
2747 * Callback to set the value of a dapm semi enumerated double mixer control.
2748 *
2749 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2750 * used for handling bitfield coded enumeration for example.
2751 *
2752 * Returns 0 for success.
2753 */
2754int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2755 struct snd_ctl_elem_value *ucontrol)
2756{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002757 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2758 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2759 struct snd_soc_codec *codec = widget->codec;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002760 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002761 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002762 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002763 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002764 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002765
2766 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2767 return -EINVAL;
2768 mux = ucontrol->value.enumerated.item[0];
2769 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2770 mask = e->mask << e->shift_l;
2771 if (e->shift_l != e->shift_r) {
2772 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2773 return -EINVAL;
2774 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2775 mask |= e->mask << e->shift_r;
2776 }
2777
Stephen Warrenfafd2172011-04-28 17:38:00 -06002778 mutex_lock(&codec->mutex);
2779
Mark Brown3a655772009-10-05 17:23:30 +01002780 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002781 if (change) {
2782 for (wi = 0; wi < wlist->num_widgets; wi++) {
2783 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002784
Stephen Warrenfafd2172011-04-28 17:38:00 -06002785 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002786
Stephen Warrenfafd2172011-04-28 17:38:00 -06002787 update.kcontrol = kcontrol;
2788 update.widget = widget;
2789 update.reg = e->reg;
2790 update.mask = mask;
2791 update.val = val;
2792 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002793
Liam Girdwoode002c982011-02-08 11:45:20 +00002794 snd_soc_dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002795
Stephen Warrenfafd2172011-04-28 17:38:00 -06002796 widget->dapm->update = NULL;
2797 }
2798 }
2799
2800 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002801 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002802}
2803EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2804
2805/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002806 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2807 *
2808 * @kcontrol: mixer control
2809 * @uinfo: control element information
2810 *
2811 * Callback to provide information about a pin switch control.
2812 */
2813int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2814 struct snd_ctl_elem_info *uinfo)
2815{
2816 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2817 uinfo->count = 1;
2818 uinfo->value.integer.min = 0;
2819 uinfo->value.integer.max = 1;
2820
2821 return 0;
2822}
2823EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2824
2825/**
2826 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2827 *
2828 * @kcontrol: mixer control
2829 * @ucontrol: Value
2830 */
2831int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2832 struct snd_ctl_elem_value *ucontrol)
2833{
2834 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2835 const char *pin = (const char *)kcontrol->private_value;
2836
2837 mutex_lock(&codec->mutex);
2838
2839 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002840 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002841
2842 mutex_unlock(&codec->mutex);
2843
2844 return 0;
2845}
2846EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2847
2848/**
2849 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2850 *
2851 * @kcontrol: mixer control
2852 * @ucontrol: Value
2853 */
2854int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2855 struct snd_ctl_elem_value *ucontrol)
2856{
2857 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2858 const char *pin = (const char *)kcontrol->private_value;
2859
2860 mutex_lock(&codec->mutex);
2861
2862 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002863 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002864 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002865 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002866
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002867 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002868
2869 mutex_unlock(&codec->mutex);
2870
2871 return 0;
2872}
2873EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2874
2875/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002876 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002877 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002878 * @widget: widget template
2879 *
2880 * Creates a new dapm control based upon the template.
2881 *
2882 * Returns 0 for success else error.
2883 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002884int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002885 const struct snd_soc_dapm_widget *widget)
2886{
2887 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002888 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002889
2890 if ((w = dapm_cnew_widget(widget)) == NULL)
2891 return -ENOMEM;
2892
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002893 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002894 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002895 name_len += 1 + strlen(dapm->codec->name_prefix);
2896 w->name = kmalloc(name_len, GFP_KERNEL);
2897 if (w->name == NULL) {
2898 kfree(w);
2899 return -ENOMEM;
2900 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002901 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002902 snprintf(w->name, name_len, "%s %s",
2903 dapm->codec->name_prefix, widget->name);
2904 else
2905 snprintf(w->name, name_len, "%s", widget->name);
2906
Mark Brown7ca3a182011-10-08 14:04:50 +01002907 switch (w->id) {
2908 case snd_soc_dapm_switch:
2909 case snd_soc_dapm_mixer:
2910 case snd_soc_dapm_mixer_named_ctl:
2911 w->power_check = dapm_generic_check_power;
2912 break;
2913 case snd_soc_dapm_mux:
2914 case snd_soc_dapm_virt_mux:
2915 case snd_soc_dapm_value_mux:
2916 w->power_check = dapm_generic_check_power;
2917 break;
2918 case snd_soc_dapm_adc:
2919 case snd_soc_dapm_aif_out:
2920 w->power_check = dapm_adc_check_power;
2921 break;
2922 case snd_soc_dapm_dac:
2923 case snd_soc_dapm_aif_in:
2924 w->power_check = dapm_dac_check_power;
2925 break;
2926 case snd_soc_dapm_pga:
2927 case snd_soc_dapm_out_drv:
2928 case snd_soc_dapm_input:
2929 case snd_soc_dapm_output:
2930 case snd_soc_dapm_micbias:
2931 case snd_soc_dapm_spk:
2932 case snd_soc_dapm_hp:
2933 case snd_soc_dapm_mic:
2934 case snd_soc_dapm_line:
2935 w->power_check = dapm_generic_check_power;
2936 break;
2937 case snd_soc_dapm_supply:
2938 w->power_check = dapm_supply_check_power;
2939 break;
2940 default:
2941 w->power_check = dapm_always_on_check_power;
2942 break;
2943 }
2944
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002945 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002946 w->dapm = dapm;
2947 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01002948 w->platform = dapm->platform;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002949 w->dai = dapm->dai;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002950 INIT_LIST_HEAD(&w->sources);
2951 INIT_LIST_HEAD(&w->sinks);
2952 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01002953 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002954 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002955
2956 /* machine layer set ups unconnected pins and insertions */
2957 w->connected = 1;
2958 return 0;
2959}
2960EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2961
2962/**
Mark Brown4ba13272008-05-13 14:51:19 +02002963 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002964 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002965 * @widget: widget array
2966 * @num: number of widgets
2967 *
2968 * Creates new DAPM controls based upon the templates.
2969 *
2970 * Returns 0 for success else error.
2971 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002972int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002973 const struct snd_soc_dapm_widget *widget,
2974 int num)
2975{
2976 int i, ret;
2977
2978 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002979 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002980 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002981 dev_err(dapm->dev,
2982 "ASoC: Failed to create DAPM control %s: %d\n",
2983 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002984 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002985 }
Mark Brown4ba13272008-05-13 14:51:19 +02002986 widget++;
2987 }
2988 return 0;
2989}
2990EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2991
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002992static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002993 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002994{
2995 struct snd_soc_dapm_widget *w;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08002996
Liam Girdwood13e13a32011-01-31 21:30:52 +00002997 if (!dapm)
2998 return;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08002999
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003000 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003001 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003002 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003003 continue;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003004 dev_vdbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003005 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003006 if (strstr(w->sname, stream)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003007 dapm_mark_dirty(w, "stream event");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003008 switch(event) {
3009 case SND_SOC_DAPM_STREAM_START:
3010 w->active = 1;
3011 break;
3012 case SND_SOC_DAPM_STREAM_STOP:
3013 w->active = 0;
3014 break;
3015 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003016 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003017 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02003018 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3019 break;
3020 }
3021 }
3022 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003023
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003024 dapm_power_widgets(dapm, event);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003025
Liam Girdwood670ff442011-03-30 23:27:27 +01003026 /* do we need to notify any clients that DAPM stream is complete */
3027 if (dapm->stream_event)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003028 dapm->stream_event(dapm, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003029}
3030
Steve Mucklef132c6c2012-06-06 18:30:57 -07003031static void widget_stream_event(struct snd_soc_dapm_context *dapm,
3032 struct snd_soc_dapm_widget *w, int event)
3033{
Mark Brownfe360682012-02-16 19:43:20 -08003034 if (!w)
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003035 return;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003036
Mark Brownfe360682012-02-16 19:43:20 -08003037 dapm_mark_dirty(w, "stream event");
3038
Steve Mucklef132c6c2012-06-06 18:30:57 -07003039 switch(event) {
Mark Brownfe360682012-02-16 19:43:20 -08003040 case SND_SOC_DAPM_STREAM_START:
3041 w->active = 1;
3042 break;
3043 case SND_SOC_DAPM_STREAM_STOP:
3044 w->active = 0;
3045 break;
3046 case SND_SOC_DAPM_STREAM_SUSPEND:
3047 case SND_SOC_DAPM_STREAM_RESUME:
3048 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3049 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3050 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003051 }
3052
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003053 dapm_power_widgets(dapm, event);
3054}
3055
Steve Mucklef132c6c2012-06-06 18:30:57 -07003056void snd_soc_dapm_rtd_stream_event(struct snd_soc_pcm_runtime *rtd,
3057 int stream, int event)
3058{
3059 struct snd_soc_dapm_context *pdapm = &rtd->platform->dapm;
3060 struct snd_soc_dapm_context *cdapm = &rtd->codec->dapm;
3061
3062 dev_dbg(rtd->dev, "rtd stream %d event %d\n", stream, event);
3063
3064 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3065 widget_stream_event(pdapm, rtd->cpu_dai->playback_aif, event);
3066 widget_stream_event(cdapm, rtd->codec_dai->playback_aif, event);
3067 } else {
3068 widget_stream_event(pdapm, rtd->cpu_dai->capture_aif, event);
3069 widget_stream_event(cdapm, rtd->codec_dai->capture_aif, event);
3070 }
3071
3072 /* do we need to notify any clients that DAPM stream is complete */
3073 if (pdapm->stream_event)
3074 pdapm->stream_event(pdapm, event);
3075 if (cdapm->stream_event)
3076 cdapm->stream_event(cdapm, event);
3077}
3078EXPORT_SYMBOL_GPL(snd_soc_dapm_rtd_stream_event);
3079
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003080/**
3081 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3082 * @rtd: PCM runtime data
3083 * @stream: stream name
3084 * @event: stream event
3085 *
3086 * Sends a stream event to the dapm core. The core then makes any
3087 * necessary widget power changes.
3088 *
3089 * Returns 0 for success else error.
3090 */
3091int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
3092 const char *stream, int event)
3093{
3094 struct snd_soc_codec *codec = rtd->codec;
3095
3096 if (stream == NULL)
3097 return 0;
Liam Girdwoodecc1a0d2011-02-16 16:24:17 +00003098
Sriranjan Srikantamfd7f1ad2012-08-14 17:42:06 -07003099 mutex_lock(&codec->mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003100 soc_dapm_stream_event(&codec->dapm, stream, event);
Sriranjan Srikantamfd7f1ad2012-08-14 17:42:06 -07003101 mutex_unlock(&codec->mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003102
Richard Purdie2b97eab2006-10-06 18:32:18 +02003103 return 0;
3104}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003105
Steve Mucklef132c6c2012-06-06 18:30:57 -07003106void snd_soc_dapm_codec_stream_event(struct snd_soc_codec *codec,
3107 const char *stream, int event)
3108{
3109// mutex_lock(&codec->card->dapm_mutex);
3110 soc_dapm_stream_event(&codec->dapm, stream, event);
3111// mutex_unlock(&codec->card->dapm_mutex);
3112}
3113EXPORT_SYMBOL(snd_soc_dapm_codec_stream_event);
3114
Richard Purdie2b97eab2006-10-06 18:32:18 +02003115/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003116 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003117 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003118 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003119 *
Mark Brown74b8f952009-06-06 11:26:15 +01003120 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003121 * a valid audio route and active audio stream.
3122 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3123 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003124 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003125int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003126{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003127 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003128}
Liam Girdwooda5302182008-07-07 13:35:17 +01003129EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003130
3131/**
Mark Brownda341832010-03-15 19:23:37 +00003132 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003133 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003134 * @pin: pin name
3135 *
3136 * Enables input/output pin regardless of any other state. This is
3137 * intended for use with microphone bias supplies used in microphone
3138 * jack detection.
3139 *
3140 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3141 * do any widget power switching.
3142 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003143int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3144 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003145{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003146 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003147
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003148 if (!w) {
3149 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3150 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003151 }
3152
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003153 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
3154 w->connected = 1;
3155 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003156 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003157
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003158 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003159}
3160EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3161
3162/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003163 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003164 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003165 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003166 *
Mark Brown74b8f952009-06-06 11:26:15 +01003167 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003168 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3169 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003170 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003171int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3172 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003173{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003174 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003175}
3176EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3177
3178/**
Mark Brown5817b522008-09-24 11:23:11 +01003179 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003180 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003181 * @pin: pin name
3182 *
3183 * Marks the specified pin as being not connected, disabling it along
3184 * any parent or child widgets. At present this is identical to
3185 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3186 * additional things such as disabling controls which only affect
3187 * paths through the pin.
3188 *
3189 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3190 * do any widget power switching.
3191 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003192int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003193{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003194 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003195}
3196EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3197
3198/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003199 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003200 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003201 * @pin: audio signal pin endpoint (or start point)
3202 *
3203 * Get audio pin status - connected or disconnected.
3204 *
3205 * Returns 1 for connected otherwise 0.
3206 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003207int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3208 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003209{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003210 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003211
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003212 if (w)
3213 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003214
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003215 return 0;
3216}
Liam Girdwooda5302182008-07-07 13:35:17 +01003217EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003218
3219/**
Mark Brown1547aba2010-05-07 21:11:40 +01003220 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003221 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003222 * @pin: audio signal pin endpoint (or start point)
3223 *
3224 * Mark the given endpoint or pin as ignoring suspend. When the
3225 * system is disabled a path between two endpoints flagged as ignoring
3226 * suspend will not be disabled. The path must already be enabled via
3227 * normal means at suspend time, it will not be turned on if it was not
3228 * already enabled.
3229 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003230int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3231 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003232{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003233 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003234
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003235 if (!w) {
3236 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3237 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003238 }
3239
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003240 w->ignore_suspend = 1;
3241
3242 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003243}
3244EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3245
Stephen Warren16332812011-11-23 12:42:04 -07003246static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3247 struct snd_soc_dapm_widget *w)
3248{
3249 struct snd_soc_dapm_path *p;
3250
3251 list_for_each_entry(p, &card->paths, list) {
3252 if ((p->source == w) || (p->sink == w)) {
3253 dev_dbg(card->dev,
3254 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3255 p->source->name, p->source->id, p->source->dapm,
3256 p->sink->name, p->sink->id, p->sink->dapm);
3257
3258 /* Connected to something other than the codec */
3259 if (p->source->dapm != p->sink->dapm)
3260 return true;
3261 /*
3262 * Loopback connection from codec external pin to
3263 * codec external pin
3264 */
3265 if (p->sink->id == snd_soc_dapm_input) {
3266 switch (p->source->id) {
3267 case snd_soc_dapm_output:
3268 case snd_soc_dapm_micbias:
3269 return true;
3270 default:
3271 break;
3272 }
3273 }
3274 }
3275 }
3276
3277 return false;
3278}
3279
3280/**
3281 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3282 * @codec: The codec whose pins should be processed
3283 *
3284 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3285 * which are unused. Pins are used if they are connected externally to the
3286 * codec, whether that be to some other device, or a loop-back connection to
3287 * the codec itself.
3288 */
3289void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3290{
3291 struct snd_soc_card *card = codec->card;
3292 struct snd_soc_dapm_context *dapm = &codec->dapm;
3293 struct snd_soc_dapm_widget *w;
3294
Mark Browna094b802011-11-27 19:42:20 +00003295 dev_dbg(codec->dev, "Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003296 &card->dapm, &codec->dapm);
3297
3298 list_for_each_entry(w, &card->widgets, list) {
3299 if (w->dapm != dapm)
3300 continue;
3301 switch (w->id) {
3302 case snd_soc_dapm_input:
3303 case snd_soc_dapm_output:
3304 case snd_soc_dapm_micbias:
Mark Browna094b802011-11-27 19:42:20 +00003305 dev_dbg(codec->dev, "Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003306 w->name);
3307 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003308 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003309 "... Not in map; disabling\n");
3310 snd_soc_dapm_nc_pin(dapm, w->name);
3311 }
3312 break;
3313 default:
3314 break;
3315 }
3316 }
3317}
3318
Mark Brown1547aba2010-05-07 21:11:40 +01003319/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003320 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003321 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003322 *
3323 * Free all dapm widgets and resources.
3324 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003325void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003326{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003327 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003328 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003329 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003330 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003331}
3332EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3333
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003334static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003335{
Mark Brown51737472009-06-22 13:16:51 +01003336 struct snd_soc_dapm_widget *w;
3337 LIST_HEAD(down_list);
3338 int powerdown = 0;
3339
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003340 list_for_each_entry(w, &dapm->card->widgets, list) {
3341 if (w->dapm != dapm)
3342 continue;
Mark Brown51737472009-06-22 13:16:51 +01003343 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003344 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003345 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003346 powerdown = 1;
3347 }
3348 }
3349
3350 /* If there were no widgets to power down we're already in
3351 * standby.
3352 */
3353 if (powerdown) {
Mark Browned5a4c42011-02-18 11:12:42 -08003354 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00003355 dapm_seq_run(dapm, &down_list, 0, false);
Mark Browned5a4c42011-02-18 11:12:42 -08003356 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003357 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003358}
Mark Brown51737472009-06-22 13:16:51 +01003359
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003360/*
3361 * snd_soc_dapm_shutdown - callback for system shutdown
3362 */
3363void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3364{
3365 struct snd_soc_codec *codec;
3366
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003367 list_for_each_entry(codec, &card->codec_dev_list, list) {
3368 soc_dapm_shutdown_codec(&codec->dapm);
Mark Browned5a4c42011-02-18 11:12:42 -08003369 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003370 }
Mark Brown51737472009-06-22 13:16:51 +01003371}
3372
Richard Purdie2b97eab2006-10-06 18:32:18 +02003373/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003374MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003375MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3376MODULE_LICENSE("GPL");