blob: 170c4ffa609ff2f9df87da7af186f47d12cee977 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020043#include <sound/core.h>
44#include <sound/pcm.h>
45#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020046#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020047#include <sound/initval.h>
48
Mark Brown84e90932010-11-04 00:07:02 -040049#include <trace/events/asoc.h>
50
Richard Purdie2b97eab2006-10-06 18:32:18 +020051/* dapm power sequences - make this per codec in the future */
52static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010053 [snd_soc_dapm_pre] = 0,
54 [snd_soc_dapm_supply] = 1,
55 [snd_soc_dapm_micbias] = 2,
Mark Brown010ff262009-08-17 17:39:22 +010056 [snd_soc_dapm_aif_in] = 3,
57 [snd_soc_dapm_aif_out] = 3,
58 [snd_soc_dapm_mic] = 4,
59 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000060 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010061 [snd_soc_dapm_value_mux] = 5,
62 [snd_soc_dapm_dac] = 6,
63 [snd_soc_dapm_mixer] = 7,
64 [snd_soc_dapm_mixer_named_ctl] = 7,
65 [snd_soc_dapm_pga] = 8,
66 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060067 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010068 [snd_soc_dapm_hp] = 10,
69 [snd_soc_dapm_spk] = 10,
70 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020071};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000072
Richard Purdie2b97eab2006-10-06 18:32:18 +020073static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010074 [snd_soc_dapm_pre] = 0,
75 [snd_soc_dapm_adc] = 1,
76 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010077 [snd_soc_dapm_spk] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060078 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010079 [snd_soc_dapm_pga] = 4,
80 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010081 [snd_soc_dapm_mixer] = 5,
82 [snd_soc_dapm_dac] = 6,
83 [snd_soc_dapm_mic] = 7,
84 [snd_soc_dapm_micbias] = 8,
85 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000086 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010087 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010088 [snd_soc_dapm_aif_in] = 10,
89 [snd_soc_dapm_aif_out] = 10,
90 [snd_soc_dapm_supply] = 11,
91 [snd_soc_dapm_post] = 12,
Richard Purdie2b97eab2006-10-06 18:32:18 +020092};
93
Troy Kisky12ef1932008-10-13 17:42:14 -070094static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +010095{
96 if (pop_time)
97 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
98}
99
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200100static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100101{
102 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200103 char *buf;
104
105 if (!pop_time)
106 return;
107
108 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
109 if (buf == NULL)
110 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100111
112 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200113 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100114 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100115 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200116
117 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100118}
119
Richard Purdie2b97eab2006-10-06 18:32:18 +0200120/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100121static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200122 const struct snd_soc_dapm_widget *_widget)
123{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100124 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200125}
126
Liam Girdwood48056082011-07-20 12:23:33 +0100127/* get snd_card from DAPM context */
128static inline struct snd_card *dapm_get_snd_card(
129 struct snd_soc_dapm_context *dapm)
130{
131 if (dapm->codec)
132 return dapm->codec->card->snd_card;
133 else if (dapm->platform)
134 return dapm->platform->card->snd_card;
135 else
136 BUG();
137
138 /* unreachable */
139 return NULL;
140}
141
142/* get soc_card from DAPM context */
143static inline struct snd_soc_card *dapm_get_soc_card(
144 struct snd_soc_dapm_context *dapm)
145{
146 if (dapm->codec)
147 return dapm->codec->card;
148 else if (dapm->platform)
149 return dapm->platform->card;
150 else
151 BUG();
152
153 /* unreachable */
154 return NULL;
155}
156
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100157static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
158{
159 if (w->codec)
160 return snd_soc_read(w->codec, reg);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100161 else if (w->platform)
162 return snd_soc_platform_read(w->platform, reg);
163
164 dev_err(w->dapm->dev, "no valid widget read method\n");
165 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100166}
167
168static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
169{
170 if (w->codec)
171 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100172 else if (w->platform)
173 return snd_soc_platform_write(w->platform, reg, val);
174
175 dev_err(w->dapm->dev, "no valid widget write method\n");
176 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100177}
178
179static int soc_widget_update_bits(struct snd_soc_dapm_widget *w,
180 unsigned short reg, unsigned int mask, unsigned int value)
181{
182 int change;
183 unsigned int old, new;
184 int ret;
185
186 ret = soc_widget_read(w, reg);
187 if (ret < 0)
188 return ret;
189
190 old = ret;
191 new = (old & ~mask) | (value & mask);
192 change = old != new;
193 if (change) {
194 ret = soc_widget_write(w, reg, new);
195 if (ret < 0)
196 return ret;
197 }
198
199 return change;
200}
201
Mark Brown452c5ea2009-05-17 21:41:23 +0100202/**
203 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800204 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100205 * @level: level to configure
206 *
207 * Configure the bias (power) levels for the SoC audio device.
208 *
209 * Returns 0 for success else error.
210 */
Mark Browned5a4c42011-02-18 11:12:42 -0800211static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200212 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100213{
Mark Browned5a4c42011-02-18 11:12:42 -0800214 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100215 int ret = 0;
216
Mark Brown84e90932010-11-04 00:07:02 -0400217 trace_snd_soc_bias_level_start(card, level);
218
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000219 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100220 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100221 if (ret != 0)
222 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100223
Mark Browncc4c6702011-06-06 19:03:34 +0100224 if (dapm->codec) {
225 if (dapm->codec->driver->set_bias_level)
226 ret = dapm->codec->driver->set_bias_level(dapm->codec,
227 level);
228 else
229 dapm->bias_level = level;
230 }
Mark Brown171ec6b2011-06-06 18:15:19 +0100231 if (ret != 0)
232 goto out;
233
234 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100235 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100236out:
Mark Brown84e90932010-11-04 00:07:02 -0400237 trace_snd_soc_bias_level_done(card, level);
238
Mark Brown452c5ea2009-05-17 21:41:23 +0100239 return ret;
240}
241
Richard Purdie2b97eab2006-10-06 18:32:18 +0200242/* set up initial codec paths */
243static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
244 struct snd_soc_dapm_path *p, int i)
245{
246 switch (w->id) {
247 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000248 case snd_soc_dapm_mixer:
249 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200250 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100251 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600252 w->kcontrol_news[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400253 unsigned int reg = mc->reg;
254 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100255 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400256 unsigned int mask = (1 << fls(max)) - 1;
257 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200258
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100259 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200260 val = (val >> shift) & mask;
261
262 if ((invert && !val) || (!invert && val))
263 p->connect = 1;
264 else
265 p->connect = 0;
266 }
267 break;
268 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600269 struct soc_enum *e = (struct soc_enum *)
270 w->kcontrol_news[i].private_value;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200271 int val, item, bitmask;
272
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100273 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Mark Brown88d96082011-06-06 16:16:34 +0100274 ;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100275 val = soc_widget_read(w, e->reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200276 item = (val >> e->shift_l) & (bitmask - 1);
277
278 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100279 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200280 if (!(strcmp(p->name, e->texts[i])) && item == i)
281 p->connect = 1;
282 }
283 }
284 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000285 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600286 struct soc_enum *e = (struct soc_enum *)
287 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000288
289 p->connect = 0;
290 /* since a virtual mux has no backing registers to
291 * decide which path to connect, it will try to match
292 * with the first enumeration. This is to ensure
293 * that the default mux choice (the first) will be
294 * correctly powered up during initialization.
295 */
296 if (!strcmp(p->name, e->texts[0]))
297 p->connect = 1;
298 }
299 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200300 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200301 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600302 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200303 int val, item;
304
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100305 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200306 val = (val >> e->shift_l) & e->mask;
307 for (item = 0; item < e->max; item++) {
308 if (val == e->values[item])
309 break;
310 }
311
312 p->connect = 0;
313 for (i = 0; i < e->max; i++) {
314 if (!(strcmp(p->name, e->texts[i])) && item == i)
315 p->connect = 1;
316 }
317 }
318 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200319 /* does not effect routing - always connected */
320 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600321 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200322 case snd_soc_dapm_output:
323 case snd_soc_dapm_adc:
324 case snd_soc_dapm_input:
325 case snd_soc_dapm_dac:
326 case snd_soc_dapm_micbias:
327 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100328 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100329 case snd_soc_dapm_aif_in:
330 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200331 p->connect = 1;
332 break;
333 /* does effect routing - dynamically connected */
334 case snd_soc_dapm_hp:
335 case snd_soc_dapm_mic:
336 case snd_soc_dapm_spk:
337 case snd_soc_dapm_line:
338 case snd_soc_dapm_pre:
339 case snd_soc_dapm_post:
340 p->connect = 0;
341 break;
342 }
343}
344
Mark Brown74b8f952009-06-06 11:26:15 +0100345/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200346static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200347 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
348 struct snd_soc_dapm_path *path, const char *control_name,
349 const struct snd_kcontrol_new *kcontrol)
350{
351 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
352 int i;
353
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100354 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200355 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200356 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200357 list_add(&path->list_sink, &dest->sources);
358 list_add(&path->list_source, &src->sinks);
359 path->name = (char*)e->texts[i];
360 dapm_set_path_status(dest, path, 0);
361 return 0;
362 }
363 }
364
365 return -ENODEV;
366}
367
Mark Brown74b8f952009-06-06 11:26:15 +0100368/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200369static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200370 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
371 struct snd_soc_dapm_path *path, const char *control_name)
372{
373 int i;
374
375 /* search for mixer kcontrol */
376 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600377 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200378 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200379 list_add(&path->list_sink, &dest->sources);
380 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600381 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200382 dapm_set_path_status(dest, path, i);
383 return 0;
384 }
385 }
386 return -ENODEV;
387}
388
Stephen Warrenaf468002011-04-28 17:38:01 -0600389static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600390 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600391 const struct snd_kcontrol_new *kcontrol_new,
392 struct snd_kcontrol **kcontrol)
393{
394 struct snd_soc_dapm_widget *w;
395 int i;
396
397 *kcontrol = NULL;
398
399 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600400 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
401 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600402 for (i = 0; i < w->num_kcontrols; i++) {
403 if (&w->kcontrol_news[i] == kcontrol_new) {
404 if (w->kcontrols)
405 *kcontrol = w->kcontrols[i];
406 return 1;
407 }
408 }
409 }
410
411 return 0;
412}
413
Richard Purdie2b97eab2006-10-06 18:32:18 +0200414/* create new dapm mixer control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200415static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200416{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200417 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200418 int i, ret = 0;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000419 size_t name_len, prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200420 struct snd_soc_dapm_path *path;
Mark Brown12ea2c72011-03-02 18:17:32 +0000421 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000422 const char *prefix;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600423 struct snd_soc_dapm_widget_list *wlist;
424 size_t wlistsize;
Mark Brownefb7ac32011-03-08 17:23:24 +0000425
426 if (dapm->codec)
427 prefix = dapm->codec->name_prefix;
428 else
429 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200430
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000431 if (prefix)
432 prefix_len = strlen(prefix) + 1;
433 else
434 prefix_len = 0;
435
Richard Purdie2b97eab2006-10-06 18:32:18 +0200436 /* add kcontrol */
437 for (i = 0; i < w->num_kcontrols; i++) {
438
439 /* match name */
440 list_for_each_entry(path, &w->sources, list_sink) {
441
442 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600443 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200444 continue;
445
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200446 if (w->kcontrols[i]) {
447 path->kcontrol = w->kcontrols[i];
448 continue;
449 }
450
Stephen Warrenfafd2172011-04-28 17:38:00 -0600451 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
452 sizeof(struct snd_soc_dapm_widget *),
453 wlist = kzalloc(wlistsize, GFP_KERNEL);
454 if (wlist == NULL) {
455 dev_err(dapm->dev,
456 "asoc: can't allocate widget list for %s\n",
457 w->name);
458 return -ENOMEM;
459 }
460 wlist->num_widgets = 1;
461 wlist->widgets[0] = w;
462
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000463 /* add dapm control with long name.
464 * for dapm_mixer this is the concatenation of the
465 * mixer and kcontrol name.
466 * for dapm_mixer_named_ctl this is simply the
467 * kcontrol name.
468 */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600469 name_len = strlen(w->kcontrol_news[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000470 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000471 name_len += 1 + strlen(w->name);
472
Mark Brown219b93f2008-10-28 13:02:31 +0000473 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000474
Stephen Warrenfafd2172011-04-28 17:38:00 -0600475 if (path->long_name == NULL) {
476 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200477 return -ENOMEM;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600478 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200479
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000480 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000481 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000482 /* The control will get a prefix from
483 * the control creation process but
484 * we're also using the same prefix
485 * for widgets so cut the prefix off
486 * the front of the widget name.
487 */
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000488 snprintf(path->long_name, name_len, "%s %s",
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000489 w->name + prefix_len,
Stephen Warren82cfecd2011-04-28 17:37:58 -0600490 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000491 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000492 case snd_soc_dapm_mixer_named_ctl:
493 snprintf(path->long_name, name_len, "%s",
Stephen Warren82cfecd2011-04-28 17:37:58 -0600494 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000495 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000496 }
497
Mark Brown219b93f2008-10-28 13:02:31 +0000498 path->long_name[name_len - 1] = '\0';
499
Stephen Warrenfafd2172011-04-28 17:38:00 -0600500 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
501 wlist, path->long_name,
502 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200503 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200504 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200505 dev_err(dapm->dev,
506 "asoc: failed to add dapm kcontrol %s: %d\n",
507 path->long_name, ret);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600508 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200509 kfree(path->long_name);
510 path->long_name = NULL;
511 return ret;
512 }
Stephen Warrenfad59882011-04-28 17:37:59 -0600513 w->kcontrols[i] = path->kcontrol;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200514 }
515 }
516 return ret;
517}
518
519/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200520static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200521{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200522 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200523 struct snd_soc_dapm_path *path = NULL;
524 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000525 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000526 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000527 size_t prefix_len;
Stephen Warrenaf468002011-04-28 17:38:01 -0600528 int ret;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600529 struct snd_soc_dapm_widget_list *wlist;
Stephen Warrenaf468002011-04-28 17:38:01 -0600530 int shared, wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600531 size_t wlistsize;
Stephen Warrenaf468002011-04-28 17:38:01 -0600532 char *name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200533
Stephen Warrenaf468002011-04-28 17:38:01 -0600534 if (w->num_kcontrols != 1) {
535 dev_err(dapm->dev,
536 "asoc: mux %s has incorrect number of controls\n",
537 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200538 return -EINVAL;
539 }
540
Stephen Warren1007da02011-05-26 09:57:33 -0600541 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
Stephen Warrenaf468002011-04-28 17:38:01 -0600542 &kcontrol);
543 if (kcontrol) {
544 wlist = kcontrol->private_data;
545 wlistentries = wlist->num_widgets + 1;
546 } else {
547 wlist = NULL;
548 wlistentries = 1;
549 }
Stephen Warrenfafd2172011-04-28 17:38:00 -0600550 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
Stephen Warrenaf468002011-04-28 17:38:01 -0600551 wlistentries * sizeof(struct snd_soc_dapm_widget *),
552 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600553 if (wlist == NULL) {
554 dev_err(dapm->dev,
555 "asoc: can't allocate widget list for %s\n", w->name);
556 return -ENOMEM;
557 }
Stephen Warrenaf468002011-04-28 17:38:01 -0600558 wlist->num_widgets = wlistentries;
559 wlist->widgets[wlistentries - 1] = w;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600560
Stephen Warrenaf468002011-04-28 17:38:01 -0600561 if (!kcontrol) {
562 if (dapm->codec)
563 prefix = dapm->codec->name_prefix;
564 else
565 prefix = NULL;
Mark Brownefb7ac32011-03-08 17:23:24 +0000566
Stephen Warrenaf468002011-04-28 17:38:01 -0600567 if (shared) {
568 name = w->kcontrol_news[0].name;
569 prefix_len = 0;
570 } else {
571 name = w->name;
572 if (prefix)
573 prefix_len = strlen(prefix) + 1;
574 else
575 prefix_len = 0;
576 }
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000577
Stephen Warrenaf468002011-04-28 17:38:01 -0600578 /*
579 * The control will get a prefix from the control creation
580 * process but we're also using the same prefix for widgets so
581 * cut the prefix off the front of the widget name.
582 */
583 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
584 name + prefix_len, prefix);
585 ret = snd_ctl_add(card, kcontrol);
586 if (ret < 0) {
587 dev_err(dapm->dev,
588 "asoc: failed to add kcontrol %s\n", w->name);
589 kfree(wlist);
590 return ret;
591 }
592 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200593
Stephen Warrenaf468002011-04-28 17:38:01 -0600594 kcontrol->private_data = wlist;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200595
Stephen Warrenfad59882011-04-28 17:37:59 -0600596 w->kcontrols[0] = kcontrol;
597
Richard Purdie2b97eab2006-10-06 18:32:18 +0200598 list_for_each_entry(path, &w->sources, list_sink)
599 path->kcontrol = kcontrol;
600
Stephen Warrenaf468002011-04-28 17:38:01 -0600601 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200602}
603
604/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200605static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200606{
Mark Browna6c65732010-03-03 17:45:21 +0000607 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200608 dev_err(w->dapm->dev,
609 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200610
Mark Browna6c65732010-03-03 17:45:21 +0000611 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200612}
613
614/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200615static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200616{
617 struct snd_soc_dapm_path *p;
618
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200619 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200620 p->walked = 0;
621}
622
Mark Brown99497882010-05-07 20:24:05 +0100623/* We implement power down on suspend by checking the power state of
624 * the ALSA card - when we are suspending the ALSA state for the card
625 * is set to D3.
626 */
627static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
628{
Mark Brown12ea2c72011-03-02 18:17:32 +0000629 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100630
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000631 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100632 case SNDRV_CTL_POWER_D3hot:
633 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100634 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200635 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
636 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100637 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100638 default:
639 return 1;
640 }
641}
642
Richard Purdie2b97eab2006-10-06 18:32:18 +0200643/*
644 * Recursively check for a completed path to an active or physically connected
645 * output widget. Returns number of complete paths.
646 */
647static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
648{
649 struct snd_soc_dapm_path *path;
650 int con = 0;
651
Mark Brown246d0a12009-04-22 18:24:55 +0100652 if (widget->id == snd_soc_dapm_supply)
653 return 0;
654
Mark Brown010ff262009-08-17 17:39:22 +0100655 switch (widget->id) {
656 case snd_soc_dapm_adc:
657 case snd_soc_dapm_aif_out:
658 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100659 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100660 default:
661 break;
662 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200663
664 if (widget->connected) {
665 /* connected pin ? */
666 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100667 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200668
669 /* connected jack or spk ? */
670 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300671 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown99497882010-05-07 20:24:05 +0100672 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200673 }
674
675 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Brownbf3a9e12011-06-13 16:42:29 +0100676 if (path->weak)
677 continue;
678
Richard Purdie2b97eab2006-10-06 18:32:18 +0200679 if (path->walked)
680 continue;
681
682 if (path->sink && path->connect) {
683 path->walked = 1;
684 con += is_connected_output_ep(path->sink);
685 }
686 }
687
688 return con;
689}
690
691/*
692 * Recursively check for a completed path to an active or physically connected
693 * input widget. Returns number of complete paths.
694 */
695static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
696{
697 struct snd_soc_dapm_path *path;
698 int con = 0;
699
Mark Brown246d0a12009-04-22 18:24:55 +0100700 if (widget->id == snd_soc_dapm_supply)
701 return 0;
702
Richard Purdie2b97eab2006-10-06 18:32:18 +0200703 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100704 switch (widget->id) {
705 case snd_soc_dapm_dac:
706 case snd_soc_dapm_aif_in:
707 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100708 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100709 default:
710 break;
711 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200712
713 if (widget->connected) {
714 /* connected pin ? */
715 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100716 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200717
718 /* connected VMID/Bias for lower pops */
719 if (widget->id == snd_soc_dapm_vmid)
Mark Brown99497882010-05-07 20:24:05 +0100720 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200721
722 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300723 if (widget->id == snd_soc_dapm_mic ||
724 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown99497882010-05-07 20:24:05 +0100725 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200726 }
727
728 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Brownbf3a9e12011-06-13 16:42:29 +0100729 if (path->weak)
730 continue;
731
Richard Purdie2b97eab2006-10-06 18:32:18 +0200732 if (path->walked)
733 continue;
734
735 if (path->source && path->connect) {
736 path->walked = 1;
737 con += is_connected_input_ep(path->source);
738 }
739 }
740
741 return con;
742}
743
744/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300745 * Handler for generic register modifier widget.
746 */
747int dapm_reg_event(struct snd_soc_dapm_widget *w,
748 struct snd_kcontrol *kcontrol, int event)
749{
750 unsigned int val;
751
752 if (SND_SOC_DAPM_EVENT_ON(event))
753 val = w->on_val;
754 else
755 val = w->off_val;
756
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100757 soc_widget_update_bits(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300758 w->mask << w->shift, val << w->shift);
759
760 return 0;
761}
Mark Brown11589412008-07-29 11:42:23 +0100762EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300763
Mark Browncd0f2d42009-04-20 16:56:59 +0100764/* Generic check to see if a widget should be powered.
765 */
766static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
767{
768 int in, out;
769
770 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200771 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100772 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200773 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100774 return out != 0 && in != 0;
775}
776
Mark Brown6ea31b92009-04-20 17:15:41 +0100777/* Check to see if an ADC has power */
778static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
779{
780 int in;
781
782 if (w->active) {
783 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200784 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100785 return in != 0;
786 } else {
787 return dapm_generic_check_power(w);
788 }
789}
790
791/* Check to see if a DAC has power */
792static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
793{
794 int out;
795
796 if (w->active) {
797 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200798 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100799 return out != 0;
800 } else {
801 return dapm_generic_check_power(w);
802 }
803}
804
Mark Brown246d0a12009-04-22 18:24:55 +0100805/* Check to see if a power supply is needed */
806static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
807{
808 struct snd_soc_dapm_path *path;
809 int power = 0;
810
811 /* Check if one of our outputs is connected */
812 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brownbf3a9e12011-06-13 16:42:29 +0100813 if (path->weak)
814 continue;
815
Mark Brown215edda2009-09-08 18:59:05 +0100816 if (path->connected &&
817 !path->connected(path->source, path->sink))
818 continue;
819
Mark Brown30173582011-02-11 11:42:19 +0000820 if (!path->sink)
821 continue;
822
823 if (path->sink->force) {
824 power = 1;
825 break;
826 }
827
828 if (path->sink->power_check &&
Mark Brown246d0a12009-04-22 18:24:55 +0100829 path->sink->power_check(path->sink)) {
830 power = 1;
831 break;
832 }
833 }
834
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200835 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100836
837 return power;
838}
839
Mark Brown38357ab2009-06-06 19:03:23 +0100840static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
841 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +0000842 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +0000843{
Mark Brown828a8422011-01-15 13:14:30 +0000844 int *sort;
845
846 if (power_up)
847 sort = dapm_up_seq;
848 else
849 sort = dapm_down_seq;
850
Mark Brown38357ab2009-06-06 19:03:23 +0100851 if (sort[a->id] != sort[b->id])
852 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +0000853 if (a->subseq != b->subseq) {
854 if (power_up)
855 return a->subseq - b->subseq;
856 else
857 return b->subseq - a->subseq;
858 }
Mark Brownb22ead22009-06-07 12:51:26 +0100859 if (a->reg != b->reg)
860 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +0000861 if (a->dapm != b->dapm)
862 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +0000863
Mark Brown38357ab2009-06-06 19:03:23 +0100864 return 0;
865}
Mark Brown42aa3412009-03-01 19:21:10 +0000866
Mark Brown38357ab2009-06-06 19:03:23 +0100867/* Insert a widget in order into a DAPM power sequence. */
868static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
869 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +0000870 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +0100871{
872 struct snd_soc_dapm_widget *w;
873
874 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +0000875 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +0100876 list_add_tail(&new_widget->power_list, &w->power_list);
877 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000878 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100879
Mark Brown38357ab2009-06-06 19:03:23 +0100880 list_add_tail(&new_widget->power_list, list);
881}
Mark Brown42aa3412009-03-01 19:21:10 +0000882
Mark Brown68f89ad2010-11-03 23:51:49 -0400883static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
884 struct snd_soc_dapm_widget *w, int event)
885{
886 struct snd_soc_card *card = dapm->card;
887 const char *ev_name;
888 int power, ret;
889
890 switch (event) {
891 case SND_SOC_DAPM_PRE_PMU:
892 ev_name = "PRE_PMU";
893 power = 1;
894 break;
895 case SND_SOC_DAPM_POST_PMU:
896 ev_name = "POST_PMU";
897 power = 1;
898 break;
899 case SND_SOC_DAPM_PRE_PMD:
900 ev_name = "PRE_PMD";
901 power = 0;
902 break;
903 case SND_SOC_DAPM_POST_PMD:
904 ev_name = "POST_PMD";
905 power = 0;
906 break;
907 default:
908 BUG();
909 return;
910 }
911
912 if (w->power != power)
913 return;
914
915 if (w->event && (w->event_flags & event)) {
916 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
917 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -0400918 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400919 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -0400920 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400921 if (ret < 0)
922 pr_err("%s: %s event failed: %d\n",
923 ev_name, w->name, ret);
924 }
925}
926
Mark Brownb22ead22009-06-07 12:51:26 +0100927/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200928static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +0100929 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100930{
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200931 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -0400932 struct snd_soc_dapm_widget *w;
933 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +0100934 unsigned int value = 0;
935 unsigned int mask = 0;
936 unsigned int cur_mask;
937
938 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
939 power_list)->reg;
940
941 list_for_each_entry(w, pending, power_list) {
942 cur_mask = 1 << w->shift;
943 BUG_ON(reg != w->reg);
944
945 if (w->invert)
946 power = !w->power;
947 else
948 power = w->power;
949
950 mask |= cur_mask;
951 if (power)
952 value |= cur_mask;
953
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200954 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +0100955 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
956 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100957
Mark Brown68f89ad2010-11-03 23:51:49 -0400958 /* Check for events */
959 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
960 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100961 }
962
Mark Brown81628102009-06-07 13:21:24 +0100963 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +0100964 /* Any widget will do, they should all be updating the
965 * same register.
966 */
967 w = list_first_entry(pending, struct snd_soc_dapm_widget,
968 power_list);
969
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200970 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +0100971 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200972 value, mask, reg, card->pop_time);
973 pop_wait(card->pop_time);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100974 soc_widget_update_bits(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +0100975 }
976
977 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -0400978 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
979 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +0000980 }
Mark Brown42aa3412009-03-01 19:21:10 +0000981}
982
Mark Brownb22ead22009-06-07 12:51:26 +0100983/* Apply a DAPM power sequence.
984 *
985 * We walk over a pre-sorted list of widgets to apply power to. In
986 * order to minimise the number of writes to the device required
987 * multiple widgets will be updated in a single write where possible.
988 * Currently anything that requires more than a single write is not
989 * handled.
990 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200991static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +0000992 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +0100993{
994 struct snd_soc_dapm_widget *w, *n;
995 LIST_HEAD(pending);
996 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +0000997 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +0100998 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200999 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001000 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001001 int *sort;
1002
1003 if (power_up)
1004 sort = dapm_up_seq;
1005 else
1006 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001007
Mark Brownb22ead22009-06-07 12:51:26 +01001008 list_for_each_entry_safe(w, n, list, power_list) {
1009 ret = 0;
1010
1011 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001012 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001013 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001014 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001015 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001016
Mark Brown474b62d2011-01-18 16:14:44 +00001017 if (cur_dapm && cur_dapm->seq_notifier) {
1018 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1019 if (sort[i] == cur_sort)
1020 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001021 i,
1022 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001023 }
1024
Mark Brownb22ead22009-06-07 12:51:26 +01001025 INIT_LIST_HEAD(&pending);
1026 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001027 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001028 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001029 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001030 }
1031
Mark Brown163cac02009-06-07 10:12:52 +01001032 switch (w->id) {
1033 case snd_soc_dapm_pre:
1034 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001035 list_for_each_entry_safe_continue(w, n, list,
1036 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001037
Mark Brownb22ead22009-06-07 12:51:26 +01001038 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001039 ret = w->event(w,
1040 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001041 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001042 ret = w->event(w,
1043 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001044 break;
1045
1046 case snd_soc_dapm_post:
1047 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001048 list_for_each_entry_safe_continue(w, n, list,
1049 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001050
Mark Brownb22ead22009-06-07 12:51:26 +01001051 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001052 ret = w->event(w,
1053 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001054 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001055 ret = w->event(w,
1056 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001057 break;
1058
Mark Brown163cac02009-06-07 10:12:52 +01001059 default:
Mark Brown81628102009-06-07 13:21:24 +01001060 /* Queue it up for application */
1061 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001062 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001063 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001064 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001065 list_move(&w->power_list, &pending);
1066 break;
Mark Brown163cac02009-06-07 10:12:52 +01001067 }
Mark Brownb22ead22009-06-07 12:51:26 +01001068
1069 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001070 dev_err(w->dapm->dev,
1071 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001072 }
Mark Brownb22ead22009-06-07 12:51:26 +01001073
1074 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001075 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001076
1077 if (cur_dapm && cur_dapm->seq_notifier) {
1078 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1079 if (sort[i] == cur_sort)
1080 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001081 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001082 }
Mark Brown163cac02009-06-07 10:12:52 +01001083}
1084
Mark Brown97404f22010-12-14 16:13:57 +00001085static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1086{
1087 struct snd_soc_dapm_update *update = dapm->update;
1088 struct snd_soc_dapm_widget *w;
1089 int ret;
1090
1091 if (!update)
1092 return;
1093
1094 w = update->widget;
1095
1096 if (w->event &&
1097 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1098 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1099 if (ret != 0)
1100 pr_err("%s DAPM pre-event failed: %d\n",
1101 w->name, ret);
1102 }
1103
1104 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1105 update->val);
1106 if (ret < 0)
1107 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1108
1109 if (w->event &&
1110 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1111 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1112 if (ret != 0)
1113 pr_err("%s DAPM post-event failed: %d\n",
1114 w->name, ret);
1115 }
1116}
1117
Mark Brown9d0624a2011-02-18 11:49:43 -08001118/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1119 * they're changing state.
1120 */
1121static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1122{
1123 struct snd_soc_dapm_context *d = data;
1124 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001125
Mark Brown56fba412011-06-04 11:25:10 +01001126 /* If we're off and we're not supposed to be go into STANDBY */
1127 if (d->bias_level == SND_SOC_BIAS_OFF &&
1128 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001129 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1130 if (ret != 0)
1131 dev_err(d->dev,
1132 "Failed to turn on bias: %d\n", ret);
1133 }
1134
Mark Brown56fba412011-06-04 11:25:10 +01001135 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1136 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001137 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1138 if (ret != 0)
1139 dev_err(d->dev,
1140 "Failed to prepare bias: %d\n", ret);
1141 }
1142}
1143
1144/* Async callback run prior to DAPM sequences - brings to their final
1145 * state.
1146 */
1147static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1148{
1149 struct snd_soc_dapm_context *d = data;
1150 int ret;
1151
1152 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001153 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1154 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1155 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001156 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1157 if (ret != 0)
1158 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1159 ret);
1160 }
1161
1162 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001163 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1164 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001165 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1166 if (ret != 0)
1167 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1168 }
1169
1170 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001171 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1172 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001173 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1174 if (ret != 0)
1175 dev_err(d->dev, "Failed to apply active bias: %d\n",
1176 ret);
1177 }
1178}
Mark Brown97404f22010-12-14 16:13:57 +00001179
Mark Brown42aa3412009-03-01 19:21:10 +00001180/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001181 * Scan each dapm widget for complete audio path.
1182 * A complete path is a route that has valid endpoints i.e.:-
1183 *
1184 * o DAC to output pin.
1185 * o Input Pin to ADC.
1186 * o Input pin to Output pin (bypass, sidetone)
1187 * o DAC to ADC (loopback).
1188 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001189static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001190{
Mark Brown12ea2c72011-03-02 18:17:32 +00001191 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001192 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001193 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001194 LIST_HEAD(up_list);
1195 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001196 LIST_HEAD(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001197 enum snd_soc_bias_level bias;
Mark Brown38357ab2009-06-06 19:03:23 +01001198 int power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001199
Mark Brown84e90932010-11-04 00:07:02 -04001200 trace_snd_soc_dapm_start(card);
1201
Mark Brown56fba412011-06-04 11:25:10 +01001202 list_for_each_entry(d, &card->dapm_list, list) {
1203 if (d->n_widgets || d->codec == NULL) {
1204 if (d->idle_bias_off)
1205 d->target_bias_level = SND_SOC_BIAS_OFF;
1206 else
1207 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1208 }
1209 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001210
Mark Brown6d3ddc82009-05-16 17:47:29 +01001211 /* Check which widgets we need to power and store them in
1212 * lists indicating if they should be powered up or down.
1213 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001214 list_for_each_entry(w, &card->widgets, list) {
Mark Brown6d3ddc82009-05-16 17:47:29 +01001215 switch (w->id) {
1216 case snd_soc_dapm_pre:
Mark Brown828a8422011-01-15 13:14:30 +00001217 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001218 break;
1219 case snd_soc_dapm_post:
Mark Brown828a8422011-01-15 13:14:30 +00001220 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001221 break;
1222
1223 default:
1224 if (!w->power_check)
1225 continue;
1226
Mark Brown99497882010-05-07 20:24:05 +01001227 if (!w->force)
Mark Brown50b6bce2009-11-23 13:11:53 +00001228 power = w->power_check(w);
Mark Brown99497882010-05-07 20:24:05 +01001229 else
1230 power = 1;
Mark Browndfcc9042011-06-04 11:34:43 +01001231
1232 if (power) {
1233 d = w->dapm;
1234
1235 /* Supplies and micbiases only bring
1236 * the context up to STANDBY as unless
1237 * something else is active and
1238 * passing audio they generally don't
1239 * require full power.
1240 */
1241 switch (w->id) {
1242 case snd_soc_dapm_supply:
1243 case snd_soc_dapm_micbias:
1244 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1245 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1246 break;
1247 default:
1248 d->target_bias_level = SND_SOC_BIAS_ON;
1249 break;
1250 }
1251 }
Mark Brown452c5ea2009-05-17 21:41:23 +01001252
Mark Brown6d3ddc82009-05-16 17:47:29 +01001253 if (w->power == power)
1254 continue;
1255
Mark Brown84e90932010-11-04 00:07:02 -04001256 trace_snd_soc_dapm_widget_power(w, power);
1257
Mark Brown6d3ddc82009-05-16 17:47:29 +01001258 if (power)
Mark Brown828a8422011-01-15 13:14:30 +00001259 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001260 else
Mark Brown828a8422011-01-15 13:14:30 +00001261 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001262
1263 w->power = power;
1264 break;
1265 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001266 }
1267
Mark Brownb14b76a2009-08-17 11:55:38 +01001268 /* If there are no DAPM widgets then try to figure out power from the
1269 * event type.
1270 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001271 if (!dapm->n_widgets) {
Mark Brownb14b76a2009-08-17 11:55:38 +01001272 switch (event) {
1273 case SND_SOC_DAPM_STREAM_START:
1274 case SND_SOC_DAPM_STREAM_RESUME:
Mark Brown56fba412011-06-04 11:25:10 +01001275 dapm->target_bias_level = SND_SOC_BIAS_ON;
Mark Brownb14b76a2009-08-17 11:55:38 +01001276 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001277 case SND_SOC_DAPM_STREAM_STOP:
Mark Brown56fba412011-06-04 11:25:10 +01001278 if (dapm->codec->active)
1279 dapm->target_bias_level = SND_SOC_BIAS_ON;
1280 else
1281 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001282 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001283 case SND_SOC_DAPM_STREAM_SUSPEND:
Mark Brown56fba412011-06-04 11:25:10 +01001284 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown50b6bce2009-11-23 13:11:53 +00001285 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001286 case SND_SOC_DAPM_STREAM_NOP:
Mark Brown56fba412011-06-04 11:25:10 +01001287 dapm->target_bias_level = dapm->bias_level;
Mark Brown50b6bce2009-11-23 13:11:53 +00001288 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001289 default:
1290 break;
1291 }
1292 }
1293
Mark Brown52ba67b2011-04-04 21:05:11 +09001294 /* Force all contexts in the card to the same bias state */
Mark Brown56fba412011-06-04 11:25:10 +01001295 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001296 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001297 if (d->target_bias_level > bias)
1298 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001299 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001300 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001301
1302
Mark Brown9d0624a2011-02-18 11:49:43 -08001303 /* Run all the bias changes in parallel */
1304 list_for_each_entry(d, &dapm->card->dapm_list, list)
1305 async_schedule_domain(dapm_pre_sequence_async, d,
1306 &async_domain);
1307 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001308
Mark Brown6d3ddc82009-05-16 17:47:29 +01001309 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001310 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001311
Mark Brown97404f22010-12-14 16:13:57 +00001312 dapm_widget_update(dapm);
1313
Mark Brown6d3ddc82009-05-16 17:47:29 +01001314 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001315 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001316
Mark Brown9d0624a2011-02-18 11:49:43 -08001317 /* Run all the bias changes in parallel */
1318 list_for_each_entry(d, &dapm->card->dapm_list, list)
1319 async_schedule_domain(dapm_post_sequence_async, d,
1320 &async_domain);
1321 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001322
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001323 pop_dbg(dapm->dev, card->pop_time,
1324 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001325 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001326
Mark Brown84e90932010-11-04 00:07:02 -04001327 trace_snd_soc_dapm_done(card);
1328
Mark Brown42aa3412009-03-01 19:21:10 +00001329 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001330}
1331
Mark Brown79fb9382009-08-21 16:38:13 +01001332#ifdef CONFIG_DEBUG_FS
1333static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1334{
1335 file->private_data = inode->i_private;
1336 return 0;
1337}
1338
1339static ssize_t dapm_widget_power_read_file(struct file *file,
1340 char __user *user_buf,
1341 size_t count, loff_t *ppos)
1342{
1343 struct snd_soc_dapm_widget *w = file->private_data;
1344 char *buf;
1345 int in, out;
1346 ssize_t ret;
1347 struct snd_soc_dapm_path *p = NULL;
1348
1349 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1350 if (!buf)
1351 return -ENOMEM;
1352
1353 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001354 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001355 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001356 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001357
Mark Brownd033c362009-12-04 15:25:56 +00001358 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001359 w->name, w->power ? "On" : "Off", in, out);
1360
Mark Brownd033c362009-12-04 15:25:56 +00001361 if (w->reg >= 0)
1362 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1363 " - R%d(0x%x) bit %d",
1364 w->reg, w->reg, w->shift);
1365
1366 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1367
Mark Brown3eef08b2009-09-14 16:49:00 +01001368 if (w->sname)
1369 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1370 w->sname,
1371 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001372
1373 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001374 if (p->connected && !p->connected(w, p->sink))
1375 continue;
1376
Mark Brown79fb9382009-08-21 16:38:13 +01001377 if (p->connect)
1378 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001379 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001380 p->name ? p->name : "static",
1381 p->source->name);
1382 }
1383 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001384 if (p->connected && !p->connected(w, p->sink))
1385 continue;
1386
Mark Brown79fb9382009-08-21 16:38:13 +01001387 if (p->connect)
1388 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001389 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001390 p->name ? p->name : "static",
1391 p->sink->name);
1392 }
1393
1394 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1395
1396 kfree(buf);
1397 return ret;
1398}
1399
1400static const struct file_operations dapm_widget_power_fops = {
1401 .open = dapm_widget_power_open_file,
1402 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001403 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001404};
1405
Mark Brownef49e4f2011-04-04 20:48:13 +09001406static int dapm_bias_open_file(struct inode *inode, struct file *file)
1407{
1408 file->private_data = inode->i_private;
1409 return 0;
1410}
1411
1412static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1413 size_t count, loff_t *ppos)
1414{
1415 struct snd_soc_dapm_context *dapm = file->private_data;
1416 char *level;
1417
1418 switch (dapm->bias_level) {
1419 case SND_SOC_BIAS_ON:
1420 level = "On\n";
1421 break;
1422 case SND_SOC_BIAS_PREPARE:
1423 level = "Prepare\n";
1424 break;
1425 case SND_SOC_BIAS_STANDBY:
1426 level = "Standby\n";
1427 break;
1428 case SND_SOC_BIAS_OFF:
1429 level = "Off\n";
1430 break;
1431 default:
1432 BUG();
1433 level = "Unknown\n";
1434 break;
1435 }
1436
1437 return simple_read_from_buffer(user_buf, count, ppos, level,
1438 strlen(level));
1439}
1440
1441static const struct file_operations dapm_bias_fops = {
1442 .open = dapm_bias_open_file,
1443 .read = dapm_bias_read_file,
1444 .llseek = default_llseek,
1445};
1446
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001447void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1448 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001449{
Mark Brown79fb9382009-08-21 16:38:13 +01001450 struct dentry *d;
1451
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001452 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1453
1454 if (!dapm->debugfs_dapm) {
1455 printk(KERN_WARNING
1456 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001457 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001458 }
Mark Brown79fb9382009-08-21 16:38:13 +01001459
Mark Brownef49e4f2011-04-04 20:48:13 +09001460 d = debugfs_create_file("bias_level", 0444,
1461 dapm->debugfs_dapm, dapm,
1462 &dapm_bias_fops);
1463 if (!d)
1464 dev_warn(dapm->dev,
1465 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001466}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001467
1468static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1469{
1470 struct snd_soc_dapm_context *dapm = w->dapm;
1471 struct dentry *d;
1472
1473 if (!dapm->debugfs_dapm || !w->name)
1474 return;
1475
1476 d = debugfs_create_file(w->name, 0444,
1477 dapm->debugfs_dapm, w,
1478 &dapm_widget_power_fops);
1479 if (!d)
1480 dev_warn(w->dapm->dev,
1481 "ASoC: Failed to create %s debugfs file\n",
1482 w->name);
1483}
1484
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001485static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1486{
1487 debugfs_remove_recursive(dapm->debugfs_dapm);
1488}
1489
Mark Brown79fb9382009-08-21 16:38:13 +01001490#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001491void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1492 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001493{
1494}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001495
1496static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1497{
1498}
1499
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001500static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1501{
1502}
1503
Mark Brown79fb9382009-08-21 16:38:13 +01001504#endif
1505
Richard Purdie2b97eab2006-10-06 18:32:18 +02001506/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001507static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001508 struct snd_kcontrol *kcontrol, int change,
1509 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001510{
1511 struct snd_soc_dapm_path *path;
1512 int found = 0;
1513
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001514 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001515 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001516 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001517 return -ENODEV;
1518
Mark Brown3a655772009-10-05 17:23:30 +01001519 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001520 return 0;
1521
1522 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001523 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001524 if (path->kcontrol != kcontrol)
1525 continue;
1526
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001527 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001528 continue;
1529
1530 found = 1;
1531 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001532 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001533 path->connect = 1; /* new connection */
1534 else
1535 path->connect = 0; /* old connection must be powered down */
1536 }
1537
Mark Brownb91b8fa2010-01-20 18:18:35 +00001538 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001539 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001540
1541 return 0;
1542}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001543
Milan plzik1b075e32008-01-10 14:39:46 +01001544/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001545static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001546 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001547{
1548 struct snd_soc_dapm_path *path;
1549 int found = 0;
1550
Milan plzik1b075e32008-01-10 14:39:46 +01001551 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001552 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001553 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001554 return -ENODEV;
1555
Richard Purdie2b97eab2006-10-06 18:32:18 +02001556 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001557 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001558 if (path->kcontrol != kcontrol)
1559 continue;
1560
1561 /* found, now check type */
1562 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001563 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001564 }
1565
Mark Brownb91b8fa2010-01-20 18:18:35 +00001566 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001567 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001568
1569 return 0;
1570}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001571
1572/* show dapm widget status in sys fs */
1573static ssize_t dapm_widget_show(struct device *dev,
1574 struct device_attribute *attr, char *buf)
1575{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001576 struct snd_soc_pcm_runtime *rtd =
1577 container_of(dev, struct snd_soc_pcm_runtime, dev);
1578 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001579 struct snd_soc_dapm_widget *w;
1580 int count = 0;
1581 char *state = "not set";
1582
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001583 list_for_each_entry(w, &codec->card->widgets, list) {
1584 if (w->dapm != &codec->dapm)
1585 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001586
1587 /* only display widgets that burnm power */
1588 switch (w->id) {
1589 case snd_soc_dapm_hp:
1590 case snd_soc_dapm_mic:
1591 case snd_soc_dapm_spk:
1592 case snd_soc_dapm_line:
1593 case snd_soc_dapm_micbias:
1594 case snd_soc_dapm_dac:
1595 case snd_soc_dapm_adc:
1596 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001597 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001598 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001599 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001600 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001601 if (w->name)
1602 count += sprintf(buf + count, "%s: %s\n",
1603 w->name, w->power ? "On":"Off");
1604 break;
1605 default:
1606 break;
1607 }
1608 }
1609
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001610 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001611 case SND_SOC_BIAS_ON:
1612 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001613 break;
Mark Brown0be98982008-05-19 12:31:28 +02001614 case SND_SOC_BIAS_PREPARE:
1615 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001616 break;
Mark Brown0be98982008-05-19 12:31:28 +02001617 case SND_SOC_BIAS_STANDBY:
1618 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001619 break;
Mark Brown0be98982008-05-19 12:31:28 +02001620 case SND_SOC_BIAS_OFF:
1621 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001622 break;
1623 }
1624 count += sprintf(buf + count, "PM State: %s\n", state);
1625
1626 return count;
1627}
1628
1629static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1630
1631int snd_soc_dapm_sys_add(struct device *dev)
1632{
Troy Kisky12ef1932008-10-13 17:42:14 -07001633 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001634}
1635
1636static void snd_soc_dapm_sys_remove(struct device *dev)
1637{
Mark Brownaef90842009-05-16 17:53:16 +01001638 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001639}
1640
1641/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001642static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001643{
1644 struct snd_soc_dapm_widget *w, *next_w;
1645 struct snd_soc_dapm_path *p, *next_p;
1646
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001647 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1648 if (w->dapm != dapm)
1649 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001650 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001651 /*
1652 * remove source and sink paths associated to this widget.
1653 * While removing the path, remove reference to it from both
1654 * source and sink widgets so that path is removed only once.
1655 */
1656 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1657 list_del(&p->list_sink);
1658 list_del(&p->list_source);
1659 list_del(&p->list);
1660 kfree(p->long_name);
1661 kfree(p);
1662 }
1663 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1664 list_del(&p->list_sink);
1665 list_del(&p->list_source);
1666 list_del(&p->list);
1667 kfree(p->long_name);
1668 kfree(p);
1669 }
Stephen Warrenfad59882011-04-28 17:37:59 -06001670 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001671 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001672 kfree(w);
1673 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001674}
1675
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001676static struct snd_soc_dapm_widget *dapm_find_widget(
1677 struct snd_soc_dapm_context *dapm, const char *pin,
1678 bool search_other_contexts)
1679{
1680 struct snd_soc_dapm_widget *w;
1681 struct snd_soc_dapm_widget *fallback = NULL;
1682
1683 list_for_each_entry(w, &dapm->card->widgets, list) {
1684 if (!strcmp(w->name, pin)) {
1685 if (w->dapm == dapm)
1686 return w;
1687 else
1688 fallback = w;
1689 }
1690 }
1691
1692 if (search_other_contexts)
1693 return fallback;
1694
1695 return NULL;
1696}
1697
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001698static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001699 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001700{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001701 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01001702
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001703 if (!w) {
1704 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1705 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01001706 }
1707
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001708 w->connected = status;
1709 if (status == 0)
1710 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09001711
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001712 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001713}
1714
Richard Purdie2b97eab2006-10-06 18:32:18 +02001715/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001716 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001717 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001718 *
1719 * Walks all dapm audio paths and powers widgets according to their
1720 * stream or path usage.
1721 *
1722 * Returns 0 for success.
1723 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001724int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001725{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001726 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001727}
Liam Girdwooda5302182008-07-07 13:35:17 +01001728EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001729
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001730static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001731 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001732{
1733 struct snd_soc_dapm_path *path;
1734 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001735 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001736 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001737 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001738 const char *source;
1739 char prefixed_sink[80];
1740 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001741 int ret = 0;
1742
Mark Brown88e8b9a2011-03-02 18:18:24 +00001743 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001744 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1745 dapm->codec->name_prefix, route->sink);
1746 sink = prefixed_sink;
1747 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1748 dapm->codec->name_prefix, route->source);
1749 source = prefixed_source;
1750 } else {
1751 sink = route->sink;
1752 source = route->source;
1753 }
1754
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001755 /*
1756 * find src and dest widgets over all widgets but favor a widget from
1757 * current DAPM context
1758 */
1759 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001760 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001761 wtsink = w;
1762 if (w->dapm == dapm)
1763 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001764 continue;
1765 }
1766 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001767 wtsource = w;
1768 if (w->dapm == dapm)
1769 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001770 }
1771 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001772 /* use widget from another DAPM context if not found from this */
1773 if (!wsink)
1774 wsink = wtsink;
1775 if (!wsource)
1776 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001777
1778 if (wsource == NULL || wsink == NULL)
1779 return -ENODEV;
1780
1781 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1782 if (!path)
1783 return -ENOMEM;
1784
1785 path->source = wsource;
1786 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001787 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001788 INIT_LIST_HEAD(&path->list);
1789 INIT_LIST_HEAD(&path->list_source);
1790 INIT_LIST_HEAD(&path->list_sink);
1791
1792 /* check for external widgets */
1793 if (wsink->id == snd_soc_dapm_input) {
1794 if (wsource->id == snd_soc_dapm_micbias ||
1795 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001796 wsource->id == snd_soc_dapm_line ||
1797 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001798 wsink->ext = 1;
1799 }
1800 if (wsource->id == snd_soc_dapm_output) {
1801 if (wsink->id == snd_soc_dapm_spk ||
1802 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001803 wsink->id == snd_soc_dapm_line ||
1804 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001805 wsource->ext = 1;
1806 }
1807
1808 /* connect static paths */
1809 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001810 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001811 list_add(&path->list_sink, &wsink->sources);
1812 list_add(&path->list_source, &wsource->sinks);
1813 path->connect = 1;
1814 return 0;
1815 }
1816
1817 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08001818 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001819 case snd_soc_dapm_adc:
1820 case snd_soc_dapm_dac:
1821 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001822 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001823 case snd_soc_dapm_input:
1824 case snd_soc_dapm_output:
1825 case snd_soc_dapm_micbias:
1826 case snd_soc_dapm_vmid:
1827 case snd_soc_dapm_pre:
1828 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001829 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001830 case snd_soc_dapm_aif_in:
1831 case snd_soc_dapm_aif_out:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001832 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001833 list_add(&path->list_sink, &wsink->sources);
1834 list_add(&path->list_source, &wsource->sinks);
1835 path->connect = 1;
1836 return 0;
1837 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001838 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001839 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001840 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06001841 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001842 if (ret != 0)
1843 goto err;
1844 break;
1845 case snd_soc_dapm_switch:
1846 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001847 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001848 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001849 if (ret != 0)
1850 goto err;
1851 break;
1852 case snd_soc_dapm_hp:
1853 case snd_soc_dapm_mic:
1854 case snd_soc_dapm_line:
1855 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001856 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001857 list_add(&path->list_sink, &wsink->sources);
1858 list_add(&path->list_source, &wsource->sinks);
1859 path->connect = 0;
1860 return 0;
1861 }
1862 return 0;
1863
1864err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001865 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1866 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001867 kfree(path);
1868 return ret;
1869}
Mark Brown105f1c22008-05-13 14:52:19 +02001870
1871/**
Mark Brown105f1c22008-05-13 14:52:19 +02001872 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001873 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02001874 * @route: audio routes
1875 * @num: number of routes
1876 *
1877 * Connects 2 dapm widgets together via a named audio path. The sink is
1878 * the widget receiving the audio signal, whilst the source is the sender
1879 * of the audio signal.
1880 *
1881 * Returns 0 for success else error. On error all resources can be freed
1882 * with a call to snd_soc_card_free().
1883 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001884int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02001885 const struct snd_soc_dapm_route *route, int num)
1886{
1887 int i, ret;
1888
1889 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001890 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001891 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001892 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1893 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02001894 return ret;
1895 }
1896 route++;
1897 }
1898
1899 return 0;
1900}
1901EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1902
Mark Brownbf3a9e12011-06-13 16:42:29 +01001903static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
1904 const struct snd_soc_dapm_route *route)
1905{
1906 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
1907 route->source,
1908 true);
1909 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
1910 route->sink,
1911 true);
1912 struct snd_soc_dapm_path *path;
1913 int count = 0;
1914
1915 if (!source) {
1916 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
1917 route->source);
1918 return -ENODEV;
1919 }
1920
1921 if (!sink) {
1922 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
1923 route->sink);
1924 return -ENODEV;
1925 }
1926
1927 if (route->control || route->connected)
1928 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
1929 route->source, route->sink);
1930
1931 list_for_each_entry(path, &source->sinks, list_source) {
1932 if (path->sink == sink) {
1933 path->weak = 1;
1934 count++;
1935 }
1936 }
1937
1938 if (count == 0)
1939 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
1940 route->source, route->sink);
1941 if (count > 1)
1942 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
1943 count, route->source, route->sink);
1944
1945 return 0;
1946}
1947
1948/**
1949 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
1950 * @dapm: DAPM context
1951 * @route: audio routes
1952 * @num: number of routes
1953 *
1954 * Mark existing routes matching those specified in the passed array
1955 * as being weak, meaning that they are ignored for the purpose of
1956 * power decisions. The main intended use case is for sidetone paths
1957 * which couple audio between other independent paths if they are both
1958 * active in order to make the combination work better at the user
1959 * level but which aren't intended to be "used".
1960 *
1961 * Note that CODEC drivers should not use this as sidetone type paths
1962 * can frequently also be used as bypass paths.
1963 */
1964int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
1965 const struct snd_soc_dapm_route *route, int num)
1966{
1967 int i, err;
1968 int ret = 0;
1969
1970 for (i = 0; i < num; i++) {
1971 err = snd_soc_dapm_weak_route(dapm, route);
1972 if (err)
1973 ret = err;
1974 route++;
1975 }
1976
1977 return ret;
1978}
1979EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
1980
Mark Brown105f1c22008-05-13 14:52:19 +02001981/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001982 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001983 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001984 *
1985 * Checks the codec for any new dapm widgets and creates them if found.
1986 *
1987 * Returns 0 for success.
1988 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001989int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001990{
1991 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00001992 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001993
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001994 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001995 {
1996 if (w->new)
1997 continue;
1998
Stephen Warrenfad59882011-04-28 17:37:59 -06001999 if (w->num_kcontrols) {
2000 w->kcontrols = kzalloc(w->num_kcontrols *
2001 sizeof(struct snd_kcontrol *),
2002 GFP_KERNEL);
2003 if (!w->kcontrols)
2004 return -ENOMEM;
2005 }
2006
Richard Purdie2b97eab2006-10-06 18:32:18 +02002007 switch(w->id) {
2008 case snd_soc_dapm_switch:
2009 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002010 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01002011 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002012 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002013 break;
2014 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002015 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002016 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01002017 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002018 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002019 break;
2020 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01002021 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01002022 w->power_check = dapm_adc_check_power;
2023 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002024 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01002025 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01002026 w->power_check = dapm_dac_check_power;
2027 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002028 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002029 case snd_soc_dapm_out_drv:
Mark Brownb75576d2009-04-20 17:56:13 +01002030 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002031 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002032 break;
2033 case snd_soc_dapm_input:
2034 case snd_soc_dapm_output:
2035 case snd_soc_dapm_micbias:
2036 case snd_soc_dapm_spk:
2037 case snd_soc_dapm_hp:
2038 case snd_soc_dapm_mic:
2039 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01002040 w->power_check = dapm_generic_check_power;
2041 break;
Mark Brown246d0a12009-04-22 18:24:55 +01002042 case snd_soc_dapm_supply:
2043 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002044 case snd_soc_dapm_vmid:
2045 case snd_soc_dapm_pre:
2046 case snd_soc_dapm_post:
2047 break;
2048 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002049
2050 /* Read the initial power state from the device */
2051 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002052 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002053 val &= 1 << w->shift;
2054 if (w->invert)
2055 val = !val;
2056
2057 if (val)
2058 w->power = 1;
2059 }
2060
Richard Purdie2b97eab2006-10-06 18:32:18 +02002061 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002062
2063 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002064 }
2065
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002066 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002067 return 0;
2068}
2069EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2070
2071/**
2072 * snd_soc_dapm_get_volsw - dapm mixer get callback
2073 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002074 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002075 *
2076 * Callback to get the value of a dapm mixer control.
2077 *
2078 * Returns 0 for success.
2079 */
2080int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2081 struct snd_ctl_elem_value *ucontrol)
2082{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002083 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2084 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002085 struct soc_mixer_control *mc =
2086 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002087 unsigned int reg = mc->reg;
2088 unsigned int shift = mc->shift;
2089 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002090 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002091 unsigned int invert = mc->invert;
2092 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002093
Richard Purdie2b97eab2006-10-06 18:32:18 +02002094 ucontrol->value.integer.value[0] =
2095 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2096 if (shift != rshift)
2097 ucontrol->value.integer.value[1] =
2098 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2099 if (invert) {
2100 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002101 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002102 if (shift != rshift)
2103 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002104 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002105 }
2106
2107 return 0;
2108}
2109EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2110
2111/**
2112 * snd_soc_dapm_put_volsw - dapm mixer set callback
2113 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002114 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002115 *
2116 * Callback to set the value of a dapm mixer control.
2117 *
2118 * Returns 0 for success.
2119 */
2120int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2121 struct snd_ctl_elem_value *ucontrol)
2122{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002123 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2124 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2125 struct snd_soc_codec *codec = widget->codec;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002126 struct soc_mixer_control *mc =
2127 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002128 unsigned int reg = mc->reg;
2129 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002130 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002131 unsigned int mask = (1 << fls(max)) - 1;
2132 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002133 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002134 int connect, change;
2135 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002136 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002137
2138 val = (ucontrol->value.integer.value[0] & mask);
2139
2140 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002141 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002142 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002143 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002144
Stephen Warrenfafd2172011-04-28 17:38:00 -06002145 if (val)
2146 /* new connection */
2147 connect = invert ? 0 : 1;
2148 else
2149 /* old connection must be powered down */
2150 connect = invert ? 1 : 0;
2151
2152 mutex_lock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002153
Stephen Warrene9cf7042011-01-27 14:54:05 -07002154 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002155 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002156 for (wi = 0; wi < wlist->num_widgets; wi++) {
2157 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002158
Stephen Warrenfafd2172011-04-28 17:38:00 -06002159 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002160
Stephen Warrenfafd2172011-04-28 17:38:00 -06002161 update.kcontrol = kcontrol;
2162 update.widget = widget;
2163 update.reg = reg;
2164 update.mask = mask;
2165 update.val = val;
2166 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002167
Stephen Warrenfafd2172011-04-28 17:38:00 -06002168 dapm_mixer_update_power(widget, kcontrol, connect);
2169
2170 widget->dapm->update = NULL;
2171 }
Mark Brown283375c2009-12-07 18:09:03 +00002172 }
2173
Stephen Warrenfafd2172011-04-28 17:38:00 -06002174 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002175 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002176}
2177EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2178
2179/**
2180 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2181 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002182 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002183 *
2184 * Callback to get the value of a dapm enumerated double mixer control.
2185 *
2186 * Returns 0 for success.
2187 */
2188int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2189 struct snd_ctl_elem_value *ucontrol)
2190{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002191 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2192 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002193 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002194 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002195
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002196 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002197 ;
2198 val = snd_soc_read(widget->codec, e->reg);
2199 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2200 if (e->shift_l != e->shift_r)
2201 ucontrol->value.enumerated.item[1] =
2202 (val >> e->shift_r) & (bitmask - 1);
2203
2204 return 0;
2205}
2206EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2207
2208/**
2209 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2210 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002211 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002212 *
2213 * Callback to set the value of a dapm enumerated double mixer control.
2214 *
2215 * Returns 0 for success.
2216 */
2217int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2218 struct snd_ctl_elem_value *ucontrol)
2219{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002220 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2221 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2222 struct snd_soc_codec *codec = widget->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002223 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002224 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002225 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00002226 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002227 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002228
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002229 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002230 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002231 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002232 return -EINVAL;
2233 mux = ucontrol->value.enumerated.item[0];
2234 val = mux << e->shift_l;
2235 mask = (bitmask - 1) << e->shift_l;
2236 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002237 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002238 return -EINVAL;
2239 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2240 mask |= (bitmask - 1) << e->shift_r;
2241 }
2242
Stephen Warrenfafd2172011-04-28 17:38:00 -06002243 mutex_lock(&codec->mutex);
2244
Mark Brown3a655772009-10-05 17:23:30 +01002245 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002246 if (change) {
2247 for (wi = 0; wi < wlist->num_widgets; wi++) {
2248 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002249
Stephen Warrenfafd2172011-04-28 17:38:00 -06002250 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002251
Stephen Warrenfafd2172011-04-28 17:38:00 -06002252 update.kcontrol = kcontrol;
2253 update.widget = widget;
2254 update.reg = e->reg;
2255 update.mask = mask;
2256 update.val = val;
2257 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002258
Stephen Warrenfafd2172011-04-28 17:38:00 -06002259 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002260
Stephen Warrenfafd2172011-04-28 17:38:00 -06002261 widget->dapm->update = NULL;
2262 }
2263 }
2264
2265 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002266 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002267}
2268EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2269
2270/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002271 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2272 * @kcontrol: mixer control
2273 * @ucontrol: control element information
2274 *
2275 * Returns 0 for success.
2276 */
2277int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2278 struct snd_ctl_elem_value *ucontrol)
2279{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002280 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2281 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002282
2283 ucontrol->value.enumerated.item[0] = widget->value;
2284
2285 return 0;
2286}
2287EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2288
2289/**
2290 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2291 * @kcontrol: mixer control
2292 * @ucontrol: control element information
2293 *
2294 * Returns 0 for success.
2295 */
2296int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2297 struct snd_ctl_elem_value *ucontrol)
2298{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002299 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2300 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2301 struct snd_soc_codec *codec = widget->codec;
Mark Brownd2b247a2009-10-06 15:21:04 +01002302 struct soc_enum *e =
2303 (struct soc_enum *)kcontrol->private_value;
2304 int change;
2305 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002306 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002307
2308 if (ucontrol->value.enumerated.item[0] >= e->max)
2309 return -EINVAL;
2310
Stephen Warrenfafd2172011-04-28 17:38:00 -06002311 mutex_lock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002312
2313 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002314 if (change) {
2315 for (wi = 0; wi < wlist->num_widgets; wi++) {
2316 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002317
Stephen Warrenfafd2172011-04-28 17:38:00 -06002318 widget->value = ucontrol->value.enumerated.item[0];
2319
2320 dapm_mux_update_power(widget, kcontrol, change,
2321 widget->value, e);
2322 }
2323 }
2324
2325 mutex_unlock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002326 return ret;
2327}
2328EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2329
2330/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002331 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2332 * callback
2333 * @kcontrol: mixer control
2334 * @ucontrol: control element information
2335 *
2336 * Callback to get the value of a dapm semi enumerated double mixer control.
2337 *
2338 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2339 * used for handling bitfield coded enumeration for example.
2340 *
2341 * Returns 0 for success.
2342 */
2343int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2344 struct snd_ctl_elem_value *ucontrol)
2345{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002346 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2347 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002348 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002349 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002350
2351 reg_val = snd_soc_read(widget->codec, e->reg);
2352 val = (reg_val >> e->shift_l) & e->mask;
2353 for (mux = 0; mux < e->max; mux++) {
2354 if (val == e->values[mux])
2355 break;
2356 }
2357 ucontrol->value.enumerated.item[0] = mux;
2358 if (e->shift_l != e->shift_r) {
2359 val = (reg_val >> e->shift_r) & e->mask;
2360 for (mux = 0; mux < e->max; mux++) {
2361 if (val == e->values[mux])
2362 break;
2363 }
2364 ucontrol->value.enumerated.item[1] = mux;
2365 }
2366
2367 return 0;
2368}
2369EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2370
2371/**
2372 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2373 * callback
2374 * @kcontrol: mixer control
2375 * @ucontrol: control element information
2376 *
2377 * Callback to set the value of a dapm semi enumerated double mixer control.
2378 *
2379 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2380 * used for handling bitfield coded enumeration for example.
2381 *
2382 * Returns 0 for success.
2383 */
2384int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2385 struct snd_ctl_elem_value *ucontrol)
2386{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002387 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2388 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2389 struct snd_soc_codec *codec = widget->codec;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002390 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002391 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002392 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002393 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002394 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002395
2396 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2397 return -EINVAL;
2398 mux = ucontrol->value.enumerated.item[0];
2399 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2400 mask = e->mask << e->shift_l;
2401 if (e->shift_l != e->shift_r) {
2402 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2403 return -EINVAL;
2404 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2405 mask |= e->mask << e->shift_r;
2406 }
2407
Stephen Warrenfafd2172011-04-28 17:38:00 -06002408 mutex_lock(&codec->mutex);
2409
Mark Brown3a655772009-10-05 17:23:30 +01002410 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002411 if (change) {
2412 for (wi = 0; wi < wlist->num_widgets; wi++) {
2413 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002414
Stephen Warrenfafd2172011-04-28 17:38:00 -06002415 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002416
Stephen Warrenfafd2172011-04-28 17:38:00 -06002417 update.kcontrol = kcontrol;
2418 update.widget = widget;
2419 update.reg = e->reg;
2420 update.mask = mask;
2421 update.val = val;
2422 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002423
Stephen Warrenfafd2172011-04-28 17:38:00 -06002424 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002425
Stephen Warrenfafd2172011-04-28 17:38:00 -06002426 widget->dapm->update = NULL;
2427 }
2428 }
2429
2430 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002431 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002432}
2433EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2434
2435/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002436 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2437 *
2438 * @kcontrol: mixer control
2439 * @uinfo: control element information
2440 *
2441 * Callback to provide information about a pin switch control.
2442 */
2443int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2444 struct snd_ctl_elem_info *uinfo)
2445{
2446 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2447 uinfo->count = 1;
2448 uinfo->value.integer.min = 0;
2449 uinfo->value.integer.max = 1;
2450
2451 return 0;
2452}
2453EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2454
2455/**
2456 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2457 *
2458 * @kcontrol: mixer control
2459 * @ucontrol: Value
2460 */
2461int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2462 struct snd_ctl_elem_value *ucontrol)
2463{
2464 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2465 const char *pin = (const char *)kcontrol->private_value;
2466
2467 mutex_lock(&codec->mutex);
2468
2469 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002470 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002471
2472 mutex_unlock(&codec->mutex);
2473
2474 return 0;
2475}
2476EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2477
2478/**
2479 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2480 *
2481 * @kcontrol: mixer control
2482 * @ucontrol: Value
2483 */
2484int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2485 struct snd_ctl_elem_value *ucontrol)
2486{
2487 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2488 const char *pin = (const char *)kcontrol->private_value;
2489
2490 mutex_lock(&codec->mutex);
2491
2492 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002493 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002494 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002495 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002496
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002497 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002498
2499 mutex_unlock(&codec->mutex);
2500
2501 return 0;
2502}
2503EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2504
2505/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002506 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002507 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002508 * @widget: widget template
2509 *
2510 * Creates a new dapm control based upon the template.
2511 *
2512 * Returns 0 for success else error.
2513 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002514int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002515 const struct snd_soc_dapm_widget *widget)
2516{
2517 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002518 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002519
2520 if ((w = dapm_cnew_widget(widget)) == NULL)
2521 return -ENOMEM;
2522
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002523 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002524 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002525 name_len += 1 + strlen(dapm->codec->name_prefix);
2526 w->name = kmalloc(name_len, GFP_KERNEL);
2527 if (w->name == NULL) {
2528 kfree(w);
2529 return -ENOMEM;
2530 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002531 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002532 snprintf(w->name, name_len, "%s %s",
2533 dapm->codec->name_prefix, widget->name);
2534 else
2535 snprintf(w->name, name_len, "%s", widget->name);
2536
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002537 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002538 w->dapm = dapm;
2539 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01002540 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002541 INIT_LIST_HEAD(&w->sources);
2542 INIT_LIST_HEAD(&w->sinks);
2543 INIT_LIST_HEAD(&w->list);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002544 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002545
2546 /* machine layer set ups unconnected pins and insertions */
2547 w->connected = 1;
2548 return 0;
2549}
2550EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2551
2552/**
Mark Brown4ba13272008-05-13 14:51:19 +02002553 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002554 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002555 * @widget: widget array
2556 * @num: number of widgets
2557 *
2558 * Creates new DAPM controls based upon the templates.
2559 *
2560 * Returns 0 for success else error.
2561 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002562int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002563 const struct snd_soc_dapm_widget *widget,
2564 int num)
2565{
2566 int i, ret;
2567
2568 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002569 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002570 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002571 dev_err(dapm->dev,
2572 "ASoC: Failed to create DAPM control %s: %d\n",
2573 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002574 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002575 }
Mark Brown4ba13272008-05-13 14:51:19 +02002576 widget++;
2577 }
2578 return 0;
2579}
2580EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2581
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002582static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002583 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002584{
2585 struct snd_soc_dapm_widget *w;
2586
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002587 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002588 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002589 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002590 continue;
Liam Girdwoodee47b362011-07-25 11:15:50 +01002591 dev_vdbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002592 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002593 if (strstr(w->sname, stream)) {
2594 switch(event) {
2595 case SND_SOC_DAPM_STREAM_START:
2596 w->active = 1;
2597 break;
2598 case SND_SOC_DAPM_STREAM_STOP:
2599 w->active = 0;
2600 break;
2601 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002602 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002603 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002604 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2605 break;
2606 }
2607 }
2608 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002609
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002610 dapm_power_widgets(dapm, event);
Liam Girdwood64a648c2011-07-25 11:15:15 +01002611
2612 /* do we need to notify any clients that DAPM stream is complete */
2613 if (dapm->stream_event)
2614 dapm->stream_event(dapm, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002615}
2616
2617/**
2618 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2619 * @rtd: PCM runtime data
2620 * @stream: stream name
2621 * @event: stream event
2622 *
2623 * Sends a stream event to the dapm core. The core then makes any
2624 * necessary widget power changes.
2625 *
2626 * Returns 0 for success else error.
2627 */
2628int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2629 const char *stream, int event)
2630{
2631 struct snd_soc_codec *codec = rtd->codec;
2632
2633 if (stream == NULL)
2634 return 0;
2635
2636 mutex_lock(&codec->mutex);
2637 soc_dapm_stream_event(&codec->dapm, stream, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002638 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002639 return 0;
2640}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002641
2642/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002643 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002644 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002645 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002646 *
Mark Brown74b8f952009-06-06 11:26:15 +01002647 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002648 * a valid audio route and active audio stream.
2649 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2650 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002651 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002652int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002653{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002654 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002655}
Liam Girdwooda5302182008-07-07 13:35:17 +01002656EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002657
2658/**
Mark Brownda341832010-03-15 19:23:37 +00002659 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002660 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002661 * @pin: pin name
2662 *
2663 * Enables input/output pin regardless of any other state. This is
2664 * intended for use with microphone bias supplies used in microphone
2665 * jack detection.
2666 *
2667 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2668 * do any widget power switching.
2669 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002670int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2671 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002672{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002673 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00002674
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002675 if (!w) {
2676 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2677 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00002678 }
2679
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002680 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2681 w->connected = 1;
2682 w->force = 1;
Mark Brown0d867332011-04-06 11:38:14 +09002683
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002684 return 0;
Mark Brownda341832010-03-15 19:23:37 +00002685}
2686EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2687
2688/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002689 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002690 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002691 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002692 *
Mark Brown74b8f952009-06-06 11:26:15 +01002693 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002694 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2695 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002696 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002697int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2698 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002699{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002700 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002701}
2702EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2703
2704/**
Mark Brown5817b522008-09-24 11:23:11 +01002705 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002706 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002707 * @pin: pin name
2708 *
2709 * Marks the specified pin as being not connected, disabling it along
2710 * any parent or child widgets. At present this is identical to
2711 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2712 * additional things such as disabling controls which only affect
2713 * paths through the pin.
2714 *
2715 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2716 * do any widget power switching.
2717 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002718int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002719{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002720 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002721}
2722EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2723
2724/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002725 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002726 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002727 * @pin: audio signal pin endpoint (or start point)
2728 *
2729 * Get audio pin status - connected or disconnected.
2730 *
2731 * Returns 1 for connected otherwise 0.
2732 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002733int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2734 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002735{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002736 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002737
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002738 if (w)
2739 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06002740
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002741 return 0;
2742}
Liam Girdwooda5302182008-07-07 13:35:17 +01002743EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002744
2745/**
Mark Brown1547aba2010-05-07 21:11:40 +01002746 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002747 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01002748 * @pin: audio signal pin endpoint (or start point)
2749 *
2750 * Mark the given endpoint or pin as ignoring suspend. When the
2751 * system is disabled a path between two endpoints flagged as ignoring
2752 * suspend will not be disabled. The path must already be enabled via
2753 * normal means at suspend time, it will not be turned on if it was not
2754 * already enabled.
2755 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002756int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2757 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01002758{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002759 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01002760
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002761 if (!w) {
2762 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2763 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01002764 }
2765
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002766 w->ignore_suspend = 1;
2767
2768 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01002769}
2770EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2771
2772/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002773 * snd_soc_dapm_free - free dapm resources
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002774 * @card: SoC device
Richard Purdie2b97eab2006-10-06 18:32:18 +02002775 *
2776 * Free all dapm widgets and resources.
2777 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002778void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002779{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002780 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002781 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002782 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02002783 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002784}
2785EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2786
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002787static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01002788{
Mark Brown51737472009-06-22 13:16:51 +01002789 struct snd_soc_dapm_widget *w;
2790 LIST_HEAD(down_list);
2791 int powerdown = 0;
2792
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002793 list_for_each_entry(w, &dapm->card->widgets, list) {
2794 if (w->dapm != dapm)
2795 continue;
Mark Brown51737472009-06-22 13:16:51 +01002796 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00002797 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002798 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002799 powerdown = 1;
2800 }
2801 }
2802
2803 /* If there were no widgets to power down we're already in
2804 * standby.
2805 */
2806 if (powerdown) {
Mark Browned5a4c42011-02-18 11:12:42 -08002807 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00002808 dapm_seq_run(dapm, &down_list, 0, false);
Mark Browned5a4c42011-02-18 11:12:42 -08002809 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01002810 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002811}
Mark Brown51737472009-06-22 13:16:51 +01002812
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002813/*
2814 * snd_soc_dapm_shutdown - callback for system shutdown
2815 */
2816void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2817{
2818 struct snd_soc_codec *codec;
2819
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002820 list_for_each_entry(codec, &card->codec_dev_list, list) {
2821 soc_dapm_shutdown_codec(&codec->dapm);
Mark Browned5a4c42011-02-18 11:12:42 -08002822 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002823 }
Mark Brown51737472009-06-22 13:16:51 +01002824}
2825
Richard Purdie2b97eab2006-10-06 18:32:18 +02002826/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002827MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002828MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2829MODULE_LICENSE("GPL");