blob: c26531132c669eb5a1d4ef0f930ae64ca8f91ad5 [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
Stephen Warrenfafd2172011-04-28 17:38:00 -0600446 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
447 sizeof(struct snd_soc_dapm_widget *),
448 wlist = kzalloc(wlistsize, GFP_KERNEL);
449 if (wlist == NULL) {
450 dev_err(dapm->dev,
451 "asoc: can't allocate widget list for %s\n",
452 w->name);
453 return -ENOMEM;
454 }
455 wlist->num_widgets = 1;
456 wlist->widgets[0] = w;
457
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000458 /* add dapm control with long name.
459 * for dapm_mixer this is the concatenation of the
460 * mixer and kcontrol name.
461 * for dapm_mixer_named_ctl this is simply the
462 * kcontrol name.
463 */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600464 name_len = strlen(w->kcontrol_news[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000465 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000466 name_len += 1 + strlen(w->name);
467
Mark Brown219b93f2008-10-28 13:02:31 +0000468 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000469
Stephen Warrenfafd2172011-04-28 17:38:00 -0600470 if (path->long_name == NULL) {
471 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200472 return -ENOMEM;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600473 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200474
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000475 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000476 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000477 /* The control will get a prefix from
478 * the control creation process but
479 * we're also using the same prefix
480 * for widgets so cut the prefix off
481 * the front of the widget name.
482 */
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000483 snprintf(path->long_name, name_len, "%s %s",
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000484 w->name + prefix_len,
Stephen Warren82cfecd2011-04-28 17:37:58 -0600485 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000486 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000487 case snd_soc_dapm_mixer_named_ctl:
488 snprintf(path->long_name, name_len, "%s",
Stephen Warren82cfecd2011-04-28 17:37:58 -0600489 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000490 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000491 }
492
Mark Brown219b93f2008-10-28 13:02:31 +0000493 path->long_name[name_len - 1] = '\0';
494
Stephen Warrenfafd2172011-04-28 17:38:00 -0600495 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
496 wlist, path->long_name,
497 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200498 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200499 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200500 dev_err(dapm->dev,
501 "asoc: failed to add dapm kcontrol %s: %d\n",
502 path->long_name, ret);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600503 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200504 kfree(path->long_name);
505 path->long_name = NULL;
506 return ret;
507 }
Stephen Warrenfad59882011-04-28 17:37:59 -0600508 w->kcontrols[i] = path->kcontrol;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200509 }
510 }
511 return ret;
512}
513
514/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200515static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200516{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200517 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200518 struct snd_soc_dapm_path *path = NULL;
519 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000520 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000521 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000522 size_t prefix_len;
Stephen Warrenaf468002011-04-28 17:38:01 -0600523 int ret;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600524 struct snd_soc_dapm_widget_list *wlist;
Stephen Warrenaf468002011-04-28 17:38:01 -0600525 int shared, wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600526 size_t wlistsize;
Stephen Warrenaf468002011-04-28 17:38:01 -0600527 char *name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200528
Stephen Warrenaf468002011-04-28 17:38:01 -0600529 if (w->num_kcontrols != 1) {
530 dev_err(dapm->dev,
531 "asoc: mux %s has incorrect number of controls\n",
532 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200533 return -EINVAL;
534 }
535
Stephen Warren1007da02011-05-26 09:57:33 -0600536 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
Stephen Warrenaf468002011-04-28 17:38:01 -0600537 &kcontrol);
538 if (kcontrol) {
539 wlist = kcontrol->private_data;
540 wlistentries = wlist->num_widgets + 1;
541 } else {
542 wlist = NULL;
543 wlistentries = 1;
544 }
Stephen Warrenfafd2172011-04-28 17:38:00 -0600545 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
Stephen Warrenaf468002011-04-28 17:38:01 -0600546 wlistentries * sizeof(struct snd_soc_dapm_widget *),
547 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600548 if (wlist == NULL) {
549 dev_err(dapm->dev,
550 "asoc: can't allocate widget list for %s\n", w->name);
551 return -ENOMEM;
552 }
Stephen Warrenaf468002011-04-28 17:38:01 -0600553 wlist->num_widgets = wlistentries;
554 wlist->widgets[wlistentries - 1] = w;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600555
Stephen Warrenaf468002011-04-28 17:38:01 -0600556 if (!kcontrol) {
557 if (dapm->codec)
558 prefix = dapm->codec->name_prefix;
559 else
560 prefix = NULL;
Mark Brownefb7ac32011-03-08 17:23:24 +0000561
Stephen Warrenaf468002011-04-28 17:38:01 -0600562 if (shared) {
563 name = w->kcontrol_news[0].name;
564 prefix_len = 0;
565 } else {
566 name = w->name;
567 if (prefix)
568 prefix_len = strlen(prefix) + 1;
569 else
570 prefix_len = 0;
571 }
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000572
Stephen Warrenaf468002011-04-28 17:38:01 -0600573 /*
574 * The control will get a prefix from the control creation
575 * process but we're also using the same prefix for widgets so
576 * cut the prefix off the front of the widget name.
577 */
578 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
579 name + prefix_len, prefix);
580 ret = snd_ctl_add(card, kcontrol);
581 if (ret < 0) {
582 dev_err(dapm->dev,
583 "asoc: failed to add kcontrol %s\n", w->name);
584 kfree(wlist);
585 return ret;
586 }
587 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200588
Stephen Warrenaf468002011-04-28 17:38:01 -0600589 kcontrol->private_data = wlist;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200590
Stephen Warrenfad59882011-04-28 17:37:59 -0600591 w->kcontrols[0] = kcontrol;
592
Richard Purdie2b97eab2006-10-06 18:32:18 +0200593 list_for_each_entry(path, &w->sources, list_sink)
594 path->kcontrol = kcontrol;
595
Stephen Warrenaf468002011-04-28 17:38:01 -0600596 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200597}
598
599/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200600static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200601{
Mark Browna6c65732010-03-03 17:45:21 +0000602 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200603 dev_err(w->dapm->dev,
604 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200605
Mark Browna6c65732010-03-03 17:45:21 +0000606 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200607}
608
609/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200610static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200611{
612 struct snd_soc_dapm_path *p;
613
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200614 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200615 p->walked = 0;
616}
617
Mark Brown99497882010-05-07 20:24:05 +0100618/* We implement power down on suspend by checking the power state of
619 * the ALSA card - when we are suspending the ALSA state for the card
620 * is set to D3.
621 */
622static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
623{
Mark Brown12ea2c72011-03-02 18:17:32 +0000624 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100625
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000626 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100627 case SNDRV_CTL_POWER_D3hot:
628 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100629 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200630 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
631 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100632 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100633 default:
634 return 1;
635 }
636}
637
Richard Purdie2b97eab2006-10-06 18:32:18 +0200638/*
639 * Recursively check for a completed path to an active or physically connected
640 * output widget. Returns number of complete paths.
641 */
642static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
643{
644 struct snd_soc_dapm_path *path;
645 int con = 0;
646
Mark Brown246d0a12009-04-22 18:24:55 +0100647 if (widget->id == snd_soc_dapm_supply)
648 return 0;
649
Mark Brown010ff262009-08-17 17:39:22 +0100650 switch (widget->id) {
651 case snd_soc_dapm_adc:
652 case snd_soc_dapm_aif_out:
653 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100654 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100655 default:
656 break;
657 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200658
659 if (widget->connected) {
660 /* connected pin ? */
661 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100662 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200663
664 /* connected jack or spk ? */
665 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300666 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown99497882010-05-07 20:24:05 +0100667 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200668 }
669
670 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Brownbf3a9e12011-06-13 16:42:29 +0100671 if (path->weak)
672 continue;
673
Richard Purdie2b97eab2006-10-06 18:32:18 +0200674 if (path->walked)
675 continue;
676
677 if (path->sink && path->connect) {
678 path->walked = 1;
679 con += is_connected_output_ep(path->sink);
680 }
681 }
682
683 return con;
684}
685
686/*
687 * Recursively check for a completed path to an active or physically connected
688 * input widget. Returns number of complete paths.
689 */
690static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
691{
692 struct snd_soc_dapm_path *path;
693 int con = 0;
694
Mark Brown246d0a12009-04-22 18:24:55 +0100695 if (widget->id == snd_soc_dapm_supply)
696 return 0;
697
Richard Purdie2b97eab2006-10-06 18:32:18 +0200698 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100699 switch (widget->id) {
700 case snd_soc_dapm_dac:
701 case snd_soc_dapm_aif_in:
702 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100703 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100704 default:
705 break;
706 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200707
708 if (widget->connected) {
709 /* connected pin ? */
710 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100711 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200712
713 /* connected VMID/Bias for lower pops */
714 if (widget->id == snd_soc_dapm_vmid)
Mark Brown99497882010-05-07 20:24:05 +0100715 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200716
717 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300718 if (widget->id == snd_soc_dapm_mic ||
719 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown99497882010-05-07 20:24:05 +0100720 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200721 }
722
723 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Brownbf3a9e12011-06-13 16:42:29 +0100724 if (path->weak)
725 continue;
726
Richard Purdie2b97eab2006-10-06 18:32:18 +0200727 if (path->walked)
728 continue;
729
730 if (path->source && path->connect) {
731 path->walked = 1;
732 con += is_connected_input_ep(path->source);
733 }
734 }
735
736 return con;
737}
738
739/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300740 * Handler for generic register modifier widget.
741 */
742int dapm_reg_event(struct snd_soc_dapm_widget *w,
743 struct snd_kcontrol *kcontrol, int event)
744{
745 unsigned int val;
746
747 if (SND_SOC_DAPM_EVENT_ON(event))
748 val = w->on_val;
749 else
750 val = w->off_val;
751
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100752 soc_widget_update_bits(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300753 w->mask << w->shift, val << w->shift);
754
755 return 0;
756}
Mark Brown11589412008-07-29 11:42:23 +0100757EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300758
Mark Browncd0f2d42009-04-20 16:56:59 +0100759/* Generic check to see if a widget should be powered.
760 */
761static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
762{
763 int in, out;
764
765 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200766 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100767 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200768 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100769 return out != 0 && in != 0;
770}
771
Mark Brown6ea31b92009-04-20 17:15:41 +0100772/* Check to see if an ADC has power */
773static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
774{
775 int in;
776
777 if (w->active) {
778 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200779 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100780 return in != 0;
781 } else {
782 return dapm_generic_check_power(w);
783 }
784}
785
786/* Check to see if a DAC has power */
787static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
788{
789 int out;
790
791 if (w->active) {
792 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200793 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100794 return out != 0;
795 } else {
796 return dapm_generic_check_power(w);
797 }
798}
799
Mark Brown246d0a12009-04-22 18:24:55 +0100800/* Check to see if a power supply is needed */
801static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
802{
803 struct snd_soc_dapm_path *path;
804 int power = 0;
805
806 /* Check if one of our outputs is connected */
807 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brownbf3a9e12011-06-13 16:42:29 +0100808 if (path->weak)
809 continue;
810
Mark Brown215edda2009-09-08 18:59:05 +0100811 if (path->connected &&
812 !path->connected(path->source, path->sink))
813 continue;
814
Mark Brown30173582011-02-11 11:42:19 +0000815 if (!path->sink)
816 continue;
817
818 if (path->sink->force) {
819 power = 1;
820 break;
821 }
822
823 if (path->sink->power_check &&
Mark Brown246d0a12009-04-22 18:24:55 +0100824 path->sink->power_check(path->sink)) {
825 power = 1;
826 break;
827 }
828 }
829
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200830 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100831
832 return power;
833}
834
Mark Brown38357ab2009-06-06 19:03:23 +0100835static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
836 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +0000837 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +0000838{
Mark Brown828a8422011-01-15 13:14:30 +0000839 int *sort;
840
841 if (power_up)
842 sort = dapm_up_seq;
843 else
844 sort = dapm_down_seq;
845
Mark Brown38357ab2009-06-06 19:03:23 +0100846 if (sort[a->id] != sort[b->id])
847 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +0000848 if (a->subseq != b->subseq) {
849 if (power_up)
850 return a->subseq - b->subseq;
851 else
852 return b->subseq - a->subseq;
853 }
Mark Brownb22ead22009-06-07 12:51:26 +0100854 if (a->reg != b->reg)
855 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +0000856 if (a->dapm != b->dapm)
857 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +0000858
Mark Brown38357ab2009-06-06 19:03:23 +0100859 return 0;
860}
Mark Brown42aa3412009-03-01 19:21:10 +0000861
Mark Brown38357ab2009-06-06 19:03:23 +0100862/* Insert a widget in order into a DAPM power sequence. */
863static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
864 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +0000865 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +0100866{
867 struct snd_soc_dapm_widget *w;
868
869 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +0000870 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +0100871 list_add_tail(&new_widget->power_list, &w->power_list);
872 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000873 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100874
Mark Brown38357ab2009-06-06 19:03:23 +0100875 list_add_tail(&new_widget->power_list, list);
876}
Mark Brown42aa3412009-03-01 19:21:10 +0000877
Mark Brown68f89ad2010-11-03 23:51:49 -0400878static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
879 struct snd_soc_dapm_widget *w, int event)
880{
881 struct snd_soc_card *card = dapm->card;
882 const char *ev_name;
883 int power, ret;
884
885 switch (event) {
886 case SND_SOC_DAPM_PRE_PMU:
887 ev_name = "PRE_PMU";
888 power = 1;
889 break;
890 case SND_SOC_DAPM_POST_PMU:
891 ev_name = "POST_PMU";
892 power = 1;
893 break;
894 case SND_SOC_DAPM_PRE_PMD:
895 ev_name = "PRE_PMD";
896 power = 0;
897 break;
898 case SND_SOC_DAPM_POST_PMD:
899 ev_name = "POST_PMD";
900 power = 0;
901 break;
902 default:
903 BUG();
904 return;
905 }
906
907 if (w->power != power)
908 return;
909
910 if (w->event && (w->event_flags & event)) {
911 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
912 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -0400913 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400914 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -0400915 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400916 if (ret < 0)
917 pr_err("%s: %s event failed: %d\n",
918 ev_name, w->name, ret);
919 }
920}
921
Mark Brownb22ead22009-06-07 12:51:26 +0100922/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200923static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +0100924 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100925{
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200926 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -0400927 struct snd_soc_dapm_widget *w;
928 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +0100929 unsigned int value = 0;
930 unsigned int mask = 0;
931 unsigned int cur_mask;
932
933 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
934 power_list)->reg;
935
936 list_for_each_entry(w, pending, power_list) {
937 cur_mask = 1 << w->shift;
938 BUG_ON(reg != w->reg);
939
940 if (w->invert)
941 power = !w->power;
942 else
943 power = w->power;
944
945 mask |= cur_mask;
946 if (power)
947 value |= cur_mask;
948
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200949 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +0100950 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
951 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100952
Mark Brown68f89ad2010-11-03 23:51:49 -0400953 /* Check for events */
954 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
955 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100956 }
957
Mark Brown81628102009-06-07 13:21:24 +0100958 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +0100959 /* Any widget will do, they should all be updating the
960 * same register.
961 */
962 w = list_first_entry(pending, struct snd_soc_dapm_widget,
963 power_list);
964
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200965 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +0100966 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200967 value, mask, reg, card->pop_time);
968 pop_wait(card->pop_time);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100969 soc_widget_update_bits(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +0100970 }
971
972 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -0400973 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
974 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +0000975 }
Mark Brown42aa3412009-03-01 19:21:10 +0000976}
977
Mark Brownb22ead22009-06-07 12:51:26 +0100978/* Apply a DAPM power sequence.
979 *
980 * We walk over a pre-sorted list of widgets to apply power to. In
981 * order to minimise the number of writes to the device required
982 * multiple widgets will be updated in a single write where possible.
983 * Currently anything that requires more than a single write is not
984 * handled.
985 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200986static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +0000987 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +0100988{
989 struct snd_soc_dapm_widget *w, *n;
990 LIST_HEAD(pending);
991 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +0000992 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +0100993 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200994 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +0000995 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +0000996 int *sort;
997
998 if (power_up)
999 sort = dapm_up_seq;
1000 else
1001 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001002
Mark Brownb22ead22009-06-07 12:51:26 +01001003 list_for_each_entry_safe(w, n, list, power_list) {
1004 ret = 0;
1005
1006 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001007 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001008 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001009 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001010 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001011
Mark Brown474b62d2011-01-18 16:14:44 +00001012 if (cur_dapm && cur_dapm->seq_notifier) {
1013 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1014 if (sort[i] == cur_sort)
1015 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001016 i,
1017 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001018 }
1019
Mark Brownb22ead22009-06-07 12:51:26 +01001020 INIT_LIST_HEAD(&pending);
1021 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001022 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001023 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001024 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001025 }
1026
Mark Brown163cac02009-06-07 10:12:52 +01001027 switch (w->id) {
1028 case snd_soc_dapm_pre:
1029 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001030 list_for_each_entry_safe_continue(w, n, list,
1031 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001032
Mark Brownb22ead22009-06-07 12:51:26 +01001033 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001034 ret = w->event(w,
1035 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001036 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001037 ret = w->event(w,
1038 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001039 break;
1040
1041 case snd_soc_dapm_post:
1042 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001043 list_for_each_entry_safe_continue(w, n, list,
1044 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001045
Mark Brownb22ead22009-06-07 12:51:26 +01001046 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001047 ret = w->event(w,
1048 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001049 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001050 ret = w->event(w,
1051 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001052 break;
1053
Mark Brown163cac02009-06-07 10:12:52 +01001054 default:
Mark Brown81628102009-06-07 13:21:24 +01001055 /* Queue it up for application */
1056 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001057 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001058 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001059 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001060 list_move(&w->power_list, &pending);
1061 break;
Mark Brown163cac02009-06-07 10:12:52 +01001062 }
Mark Brownb22ead22009-06-07 12:51:26 +01001063
1064 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001065 dev_err(w->dapm->dev,
1066 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001067 }
Mark Brownb22ead22009-06-07 12:51:26 +01001068
1069 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001070 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001071
1072 if (cur_dapm && cur_dapm->seq_notifier) {
1073 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1074 if (sort[i] == cur_sort)
1075 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001076 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001077 }
Mark Brown163cac02009-06-07 10:12:52 +01001078}
1079
Mark Brown97404f22010-12-14 16:13:57 +00001080static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1081{
1082 struct snd_soc_dapm_update *update = dapm->update;
1083 struct snd_soc_dapm_widget *w;
1084 int ret;
1085
1086 if (!update)
1087 return;
1088
1089 w = update->widget;
1090
1091 if (w->event &&
1092 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1093 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1094 if (ret != 0)
1095 pr_err("%s DAPM pre-event failed: %d\n",
1096 w->name, ret);
1097 }
1098
1099 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1100 update->val);
1101 if (ret < 0)
1102 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1103
1104 if (w->event &&
1105 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1106 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1107 if (ret != 0)
1108 pr_err("%s DAPM post-event failed: %d\n",
1109 w->name, ret);
1110 }
1111}
1112
Mark Brown9d0624a2011-02-18 11:49:43 -08001113/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1114 * they're changing state.
1115 */
1116static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1117{
1118 struct snd_soc_dapm_context *d = data;
1119 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001120
Mark Brown56fba412011-06-04 11:25:10 +01001121 /* If we're off and we're not supposed to be go into STANDBY */
1122 if (d->bias_level == SND_SOC_BIAS_OFF &&
1123 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001124 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1125 if (ret != 0)
1126 dev_err(d->dev,
1127 "Failed to turn on bias: %d\n", ret);
1128 }
1129
Mark Brown56fba412011-06-04 11:25:10 +01001130 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1131 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001132 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1133 if (ret != 0)
1134 dev_err(d->dev,
1135 "Failed to prepare bias: %d\n", ret);
1136 }
1137}
1138
1139/* Async callback run prior to DAPM sequences - brings to their final
1140 * state.
1141 */
1142static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1143{
1144 struct snd_soc_dapm_context *d = data;
1145 int ret;
1146
1147 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001148 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1149 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1150 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001151 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1152 if (ret != 0)
1153 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1154 ret);
1155 }
1156
1157 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001158 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1159 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001160 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1161 if (ret != 0)
1162 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1163 }
1164
1165 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001166 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1167 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001168 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1169 if (ret != 0)
1170 dev_err(d->dev, "Failed to apply active bias: %d\n",
1171 ret);
1172 }
1173}
Mark Brown97404f22010-12-14 16:13:57 +00001174
Mark Brown42aa3412009-03-01 19:21:10 +00001175/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001176 * Scan each dapm widget for complete audio path.
1177 * A complete path is a route that has valid endpoints i.e.:-
1178 *
1179 * o DAC to output pin.
1180 * o Input Pin to ADC.
1181 * o Input pin to Output pin (bypass, sidetone)
1182 * o DAC to ADC (loopback).
1183 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001184static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001185{
Mark Brown12ea2c72011-03-02 18:17:32 +00001186 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001187 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001188 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001189 LIST_HEAD(up_list);
1190 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001191 LIST_HEAD(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001192 enum snd_soc_bias_level bias;
Mark Brown38357ab2009-06-06 19:03:23 +01001193 int power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001194
Mark Brown84e90932010-11-04 00:07:02 -04001195 trace_snd_soc_dapm_start(card);
1196
Mark Brown56fba412011-06-04 11:25:10 +01001197 list_for_each_entry(d, &card->dapm_list, list) {
1198 if (d->n_widgets || d->codec == NULL) {
1199 if (d->idle_bias_off)
1200 d->target_bias_level = SND_SOC_BIAS_OFF;
1201 else
1202 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1203 }
1204 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001205
Mark Brown6d3ddc82009-05-16 17:47:29 +01001206 /* Check which widgets we need to power and store them in
1207 * lists indicating if they should be powered up or down.
1208 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001209 list_for_each_entry(w, &card->widgets, list) {
Mark Brown6d3ddc82009-05-16 17:47:29 +01001210 switch (w->id) {
1211 case snd_soc_dapm_pre:
Mark Brown828a8422011-01-15 13:14:30 +00001212 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001213 break;
1214 case snd_soc_dapm_post:
Mark Brown828a8422011-01-15 13:14:30 +00001215 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001216 break;
1217
1218 default:
1219 if (!w->power_check)
1220 continue;
1221
Mark Brown99497882010-05-07 20:24:05 +01001222 if (!w->force)
Mark Brown50b6bce2009-11-23 13:11:53 +00001223 power = w->power_check(w);
Mark Brown99497882010-05-07 20:24:05 +01001224 else
1225 power = 1;
Mark Browndfcc9042011-06-04 11:34:43 +01001226
1227 if (power) {
1228 d = w->dapm;
1229
1230 /* Supplies and micbiases only bring
1231 * the context up to STANDBY as unless
1232 * something else is active and
1233 * passing audio they generally don't
1234 * require full power.
1235 */
1236 switch (w->id) {
1237 case snd_soc_dapm_supply:
1238 case snd_soc_dapm_micbias:
1239 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1240 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1241 break;
1242 default:
1243 d->target_bias_level = SND_SOC_BIAS_ON;
1244 break;
1245 }
1246 }
Mark Brown452c5ea2009-05-17 21:41:23 +01001247
Mark Brown6d3ddc82009-05-16 17:47:29 +01001248 if (w->power == power)
1249 continue;
1250
Mark Brown84e90932010-11-04 00:07:02 -04001251 trace_snd_soc_dapm_widget_power(w, power);
1252
Mark Brown6d3ddc82009-05-16 17:47:29 +01001253 if (power)
Mark Brown828a8422011-01-15 13:14:30 +00001254 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001255 else
Mark Brown828a8422011-01-15 13:14:30 +00001256 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001257
1258 w->power = power;
1259 break;
1260 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001261 }
1262
Mark Brownb14b76a2009-08-17 11:55:38 +01001263 /* If there are no DAPM widgets then try to figure out power from the
1264 * event type.
1265 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001266 if (!dapm->n_widgets) {
Mark Brownb14b76a2009-08-17 11:55:38 +01001267 switch (event) {
1268 case SND_SOC_DAPM_STREAM_START:
1269 case SND_SOC_DAPM_STREAM_RESUME:
Mark Brown56fba412011-06-04 11:25:10 +01001270 dapm->target_bias_level = SND_SOC_BIAS_ON;
Mark Brownb14b76a2009-08-17 11:55:38 +01001271 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001272 case SND_SOC_DAPM_STREAM_STOP:
Mark Brown56fba412011-06-04 11:25:10 +01001273 if (dapm->codec->active)
1274 dapm->target_bias_level = SND_SOC_BIAS_ON;
1275 else
1276 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001277 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001278 case SND_SOC_DAPM_STREAM_SUSPEND:
Mark Brown56fba412011-06-04 11:25:10 +01001279 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown50b6bce2009-11-23 13:11:53 +00001280 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001281 case SND_SOC_DAPM_STREAM_NOP:
Mark Brown56fba412011-06-04 11:25:10 +01001282 dapm->target_bias_level = dapm->bias_level;
Mark Brown50b6bce2009-11-23 13:11:53 +00001283 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001284 default:
1285 break;
1286 }
1287 }
1288
Mark Brown52ba67b2011-04-04 21:05:11 +09001289 /* Force all contexts in the card to the same bias state */
Mark Brown56fba412011-06-04 11:25:10 +01001290 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001291 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001292 if (d->target_bias_level > bias)
1293 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001294 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001295 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001296
1297
Mark Brown9d0624a2011-02-18 11:49:43 -08001298 /* Run all the bias changes in parallel */
1299 list_for_each_entry(d, &dapm->card->dapm_list, list)
1300 async_schedule_domain(dapm_pre_sequence_async, d,
1301 &async_domain);
1302 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001303
Mark Brown6d3ddc82009-05-16 17:47:29 +01001304 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001305 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001306
Mark Brown97404f22010-12-14 16:13:57 +00001307 dapm_widget_update(dapm);
1308
Mark Brown6d3ddc82009-05-16 17:47:29 +01001309 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001310 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001311
Mark Brown9d0624a2011-02-18 11:49:43 -08001312 /* Run all the bias changes in parallel */
1313 list_for_each_entry(d, &dapm->card->dapm_list, list)
1314 async_schedule_domain(dapm_post_sequence_async, d,
1315 &async_domain);
1316 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001317
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001318 pop_dbg(dapm->dev, card->pop_time,
1319 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001320 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001321
Mark Brown84e90932010-11-04 00:07:02 -04001322 trace_snd_soc_dapm_done(card);
1323
Mark Brown42aa3412009-03-01 19:21:10 +00001324 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001325}
1326
Mark Brown79fb9382009-08-21 16:38:13 +01001327#ifdef CONFIG_DEBUG_FS
1328static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1329{
1330 file->private_data = inode->i_private;
1331 return 0;
1332}
1333
1334static ssize_t dapm_widget_power_read_file(struct file *file,
1335 char __user *user_buf,
1336 size_t count, loff_t *ppos)
1337{
1338 struct snd_soc_dapm_widget *w = file->private_data;
1339 char *buf;
1340 int in, out;
1341 ssize_t ret;
1342 struct snd_soc_dapm_path *p = NULL;
1343
1344 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1345 if (!buf)
1346 return -ENOMEM;
1347
1348 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001349 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001350 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001351 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001352
Mark Brownd033c362009-12-04 15:25:56 +00001353 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001354 w->name, w->power ? "On" : "Off", in, out);
1355
Mark Brownd033c362009-12-04 15:25:56 +00001356 if (w->reg >= 0)
1357 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1358 " - R%d(0x%x) bit %d",
1359 w->reg, w->reg, w->shift);
1360
1361 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1362
Mark Brown3eef08b2009-09-14 16:49:00 +01001363 if (w->sname)
1364 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1365 w->sname,
1366 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001367
1368 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001369 if (p->connected && !p->connected(w, p->sink))
1370 continue;
1371
Mark Brown79fb9382009-08-21 16:38:13 +01001372 if (p->connect)
1373 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001374 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001375 p->name ? p->name : "static",
1376 p->source->name);
1377 }
1378 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001379 if (p->connected && !p->connected(w, p->sink))
1380 continue;
1381
Mark Brown79fb9382009-08-21 16:38:13 +01001382 if (p->connect)
1383 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001384 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001385 p->name ? p->name : "static",
1386 p->sink->name);
1387 }
1388
1389 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1390
1391 kfree(buf);
1392 return ret;
1393}
1394
1395static const struct file_operations dapm_widget_power_fops = {
1396 .open = dapm_widget_power_open_file,
1397 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001398 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001399};
1400
Mark Brownef49e4f2011-04-04 20:48:13 +09001401static int dapm_bias_open_file(struct inode *inode, struct file *file)
1402{
1403 file->private_data = inode->i_private;
1404 return 0;
1405}
1406
1407static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1408 size_t count, loff_t *ppos)
1409{
1410 struct snd_soc_dapm_context *dapm = file->private_data;
1411 char *level;
1412
1413 switch (dapm->bias_level) {
1414 case SND_SOC_BIAS_ON:
1415 level = "On\n";
1416 break;
1417 case SND_SOC_BIAS_PREPARE:
1418 level = "Prepare\n";
1419 break;
1420 case SND_SOC_BIAS_STANDBY:
1421 level = "Standby\n";
1422 break;
1423 case SND_SOC_BIAS_OFF:
1424 level = "Off\n";
1425 break;
1426 default:
1427 BUG();
1428 level = "Unknown\n";
1429 break;
1430 }
1431
1432 return simple_read_from_buffer(user_buf, count, ppos, level,
1433 strlen(level));
1434}
1435
1436static const struct file_operations dapm_bias_fops = {
1437 .open = dapm_bias_open_file,
1438 .read = dapm_bias_read_file,
1439 .llseek = default_llseek,
1440};
1441
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001442void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1443 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001444{
Mark Brown79fb9382009-08-21 16:38:13 +01001445 struct dentry *d;
1446
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001447 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1448
1449 if (!dapm->debugfs_dapm) {
1450 printk(KERN_WARNING
1451 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001452 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001453 }
Mark Brown79fb9382009-08-21 16:38:13 +01001454
Mark Brownef49e4f2011-04-04 20:48:13 +09001455 d = debugfs_create_file("bias_level", 0444,
1456 dapm->debugfs_dapm, dapm,
1457 &dapm_bias_fops);
1458 if (!d)
1459 dev_warn(dapm->dev,
1460 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001461}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001462
1463static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1464{
1465 struct snd_soc_dapm_context *dapm = w->dapm;
1466 struct dentry *d;
1467
1468 if (!dapm->debugfs_dapm || !w->name)
1469 return;
1470
1471 d = debugfs_create_file(w->name, 0444,
1472 dapm->debugfs_dapm, w,
1473 &dapm_widget_power_fops);
1474 if (!d)
1475 dev_warn(w->dapm->dev,
1476 "ASoC: Failed to create %s debugfs file\n",
1477 w->name);
1478}
1479
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001480static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1481{
1482 debugfs_remove_recursive(dapm->debugfs_dapm);
1483}
1484
Mark Brown79fb9382009-08-21 16:38:13 +01001485#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001486void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1487 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001488{
1489}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001490
1491static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1492{
1493}
1494
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001495static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1496{
1497}
1498
Mark Brown79fb9382009-08-21 16:38:13 +01001499#endif
1500
Richard Purdie2b97eab2006-10-06 18:32:18 +02001501/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001502static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001503 struct snd_kcontrol *kcontrol, int change,
1504 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001505{
1506 struct snd_soc_dapm_path *path;
1507 int found = 0;
1508
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001509 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001510 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001511 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001512 return -ENODEV;
1513
Mark Brown3a655772009-10-05 17:23:30 +01001514 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001515 return 0;
1516
1517 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001518 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001519 if (path->kcontrol != kcontrol)
1520 continue;
1521
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001522 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001523 continue;
1524
1525 found = 1;
1526 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001527 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001528 path->connect = 1; /* new connection */
1529 else
1530 path->connect = 0; /* old connection must be powered down */
1531 }
1532
Mark Brownb91b8fa2010-01-20 18:18:35 +00001533 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001534 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001535
1536 return 0;
1537}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001538
Milan plzik1b075e32008-01-10 14:39:46 +01001539/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001540static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001541 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001542{
1543 struct snd_soc_dapm_path *path;
1544 int found = 0;
1545
Milan plzik1b075e32008-01-10 14:39:46 +01001546 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001547 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001548 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001549 return -ENODEV;
1550
Richard Purdie2b97eab2006-10-06 18:32:18 +02001551 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001552 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001553 if (path->kcontrol != kcontrol)
1554 continue;
1555
1556 /* found, now check type */
1557 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001558 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001559 break;
1560 }
1561
Mark Brownb91b8fa2010-01-20 18:18:35 +00001562 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001563 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001564
1565 return 0;
1566}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001567
1568/* show dapm widget status in sys fs */
1569static ssize_t dapm_widget_show(struct device *dev,
1570 struct device_attribute *attr, char *buf)
1571{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001572 struct snd_soc_pcm_runtime *rtd =
1573 container_of(dev, struct snd_soc_pcm_runtime, dev);
1574 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001575 struct snd_soc_dapm_widget *w;
1576 int count = 0;
1577 char *state = "not set";
1578
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001579 list_for_each_entry(w, &codec->card->widgets, list) {
1580 if (w->dapm != &codec->dapm)
1581 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001582
1583 /* only display widgets that burnm power */
1584 switch (w->id) {
1585 case snd_soc_dapm_hp:
1586 case snd_soc_dapm_mic:
1587 case snd_soc_dapm_spk:
1588 case snd_soc_dapm_line:
1589 case snd_soc_dapm_micbias:
1590 case snd_soc_dapm_dac:
1591 case snd_soc_dapm_adc:
1592 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001593 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001594 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001595 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001596 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001597 if (w->name)
1598 count += sprintf(buf + count, "%s: %s\n",
1599 w->name, w->power ? "On":"Off");
1600 break;
1601 default:
1602 break;
1603 }
1604 }
1605
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001606 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001607 case SND_SOC_BIAS_ON:
1608 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001609 break;
Mark Brown0be98982008-05-19 12:31:28 +02001610 case SND_SOC_BIAS_PREPARE:
1611 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001612 break;
Mark Brown0be98982008-05-19 12:31:28 +02001613 case SND_SOC_BIAS_STANDBY:
1614 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001615 break;
Mark Brown0be98982008-05-19 12:31:28 +02001616 case SND_SOC_BIAS_OFF:
1617 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001618 break;
1619 }
1620 count += sprintf(buf + count, "PM State: %s\n", state);
1621
1622 return count;
1623}
1624
1625static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1626
1627int snd_soc_dapm_sys_add(struct device *dev)
1628{
Troy Kisky12ef1932008-10-13 17:42:14 -07001629 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001630}
1631
1632static void snd_soc_dapm_sys_remove(struct device *dev)
1633{
Mark Brownaef90842009-05-16 17:53:16 +01001634 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001635}
1636
1637/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001638static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001639{
1640 struct snd_soc_dapm_widget *w, *next_w;
1641 struct snd_soc_dapm_path *p, *next_p;
1642
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001643 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1644 if (w->dapm != dapm)
1645 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001646 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001647 /*
1648 * remove source and sink paths associated to this widget.
1649 * While removing the path, remove reference to it from both
1650 * source and sink widgets so that path is removed only once.
1651 */
1652 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1653 list_del(&p->list_sink);
1654 list_del(&p->list_source);
1655 list_del(&p->list);
1656 kfree(p->long_name);
1657 kfree(p);
1658 }
1659 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1660 list_del(&p->list_sink);
1661 list_del(&p->list_source);
1662 list_del(&p->list);
1663 kfree(p->long_name);
1664 kfree(p);
1665 }
Stephen Warrenfad59882011-04-28 17:37:59 -06001666 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001667 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001668 kfree(w);
1669 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001670}
1671
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001672static struct snd_soc_dapm_widget *dapm_find_widget(
1673 struct snd_soc_dapm_context *dapm, const char *pin,
1674 bool search_other_contexts)
1675{
1676 struct snd_soc_dapm_widget *w;
1677 struct snd_soc_dapm_widget *fallback = NULL;
1678
1679 list_for_each_entry(w, &dapm->card->widgets, list) {
1680 if (!strcmp(w->name, pin)) {
1681 if (w->dapm == dapm)
1682 return w;
1683 else
1684 fallback = w;
1685 }
1686 }
1687
1688 if (search_other_contexts)
1689 return fallback;
1690
1691 return NULL;
1692}
1693
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001694static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001695 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001696{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001697 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01001698
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001699 if (!w) {
1700 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1701 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01001702 }
1703
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001704 w->connected = status;
1705 if (status == 0)
1706 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09001707
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001708 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001709}
1710
Richard Purdie2b97eab2006-10-06 18:32:18 +02001711/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001712 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001713 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001714 *
1715 * Walks all dapm audio paths and powers widgets according to their
1716 * stream or path usage.
1717 *
1718 * Returns 0 for success.
1719 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001720int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001721{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001722 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001723}
Liam Girdwooda5302182008-07-07 13:35:17 +01001724EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001725
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001726static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001727 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001728{
1729 struct snd_soc_dapm_path *path;
1730 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001731 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001732 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001733 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001734 const char *source;
1735 char prefixed_sink[80];
1736 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001737 int ret = 0;
1738
Mark Brown88e8b9a2011-03-02 18:18:24 +00001739 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001740 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1741 dapm->codec->name_prefix, route->sink);
1742 sink = prefixed_sink;
1743 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1744 dapm->codec->name_prefix, route->source);
1745 source = prefixed_source;
1746 } else {
1747 sink = route->sink;
1748 source = route->source;
1749 }
1750
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001751 /*
1752 * find src and dest widgets over all widgets but favor a widget from
1753 * current DAPM context
1754 */
1755 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001756 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001757 wtsink = w;
1758 if (w->dapm == dapm)
1759 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001760 continue;
1761 }
1762 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001763 wtsource = w;
1764 if (w->dapm == dapm)
1765 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001766 }
1767 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001768 /* use widget from another DAPM context if not found from this */
1769 if (!wsink)
1770 wsink = wtsink;
1771 if (!wsource)
1772 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001773
1774 if (wsource == NULL || wsink == NULL)
1775 return -ENODEV;
1776
1777 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1778 if (!path)
1779 return -ENOMEM;
1780
1781 path->source = wsource;
1782 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001783 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001784 INIT_LIST_HEAD(&path->list);
1785 INIT_LIST_HEAD(&path->list_source);
1786 INIT_LIST_HEAD(&path->list_sink);
1787
1788 /* check for external widgets */
1789 if (wsink->id == snd_soc_dapm_input) {
1790 if (wsource->id == snd_soc_dapm_micbias ||
1791 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001792 wsource->id == snd_soc_dapm_line ||
1793 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001794 wsink->ext = 1;
1795 }
1796 if (wsource->id == snd_soc_dapm_output) {
1797 if (wsink->id == snd_soc_dapm_spk ||
1798 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001799 wsink->id == snd_soc_dapm_line ||
1800 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001801 wsource->ext = 1;
1802 }
1803
1804 /* connect static paths */
1805 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001806 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001807 list_add(&path->list_sink, &wsink->sources);
1808 list_add(&path->list_source, &wsource->sinks);
1809 path->connect = 1;
1810 return 0;
1811 }
1812
1813 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08001814 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001815 case snd_soc_dapm_adc:
1816 case snd_soc_dapm_dac:
1817 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001818 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001819 case snd_soc_dapm_input:
1820 case snd_soc_dapm_output:
1821 case snd_soc_dapm_micbias:
1822 case snd_soc_dapm_vmid:
1823 case snd_soc_dapm_pre:
1824 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001825 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001826 case snd_soc_dapm_aif_in:
1827 case snd_soc_dapm_aif_out:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001828 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001829 list_add(&path->list_sink, &wsink->sources);
1830 list_add(&path->list_source, &wsource->sinks);
1831 path->connect = 1;
1832 return 0;
1833 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001834 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001835 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001836 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06001837 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001838 if (ret != 0)
1839 goto err;
1840 break;
1841 case snd_soc_dapm_switch:
1842 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001843 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001844 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001845 if (ret != 0)
1846 goto err;
1847 break;
1848 case snd_soc_dapm_hp:
1849 case snd_soc_dapm_mic:
1850 case snd_soc_dapm_line:
1851 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001852 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001853 list_add(&path->list_sink, &wsink->sources);
1854 list_add(&path->list_source, &wsource->sinks);
1855 path->connect = 0;
1856 return 0;
1857 }
1858 return 0;
1859
1860err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001861 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1862 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001863 kfree(path);
1864 return ret;
1865}
Mark Brown105f1c22008-05-13 14:52:19 +02001866
1867/**
Mark Brown105f1c22008-05-13 14:52:19 +02001868 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001869 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02001870 * @route: audio routes
1871 * @num: number of routes
1872 *
1873 * Connects 2 dapm widgets together via a named audio path. The sink is
1874 * the widget receiving the audio signal, whilst the source is the sender
1875 * of the audio signal.
1876 *
1877 * Returns 0 for success else error. On error all resources can be freed
1878 * with a call to snd_soc_card_free().
1879 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001880int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02001881 const struct snd_soc_dapm_route *route, int num)
1882{
1883 int i, ret;
1884
1885 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001886 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001887 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001888 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1889 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02001890 return ret;
1891 }
1892 route++;
1893 }
1894
1895 return 0;
1896}
1897EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1898
Mark Brownbf3a9e12011-06-13 16:42:29 +01001899static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
1900 const struct snd_soc_dapm_route *route)
1901{
1902 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
1903 route->source,
1904 true);
1905 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
1906 route->sink,
1907 true);
1908 struct snd_soc_dapm_path *path;
1909 int count = 0;
1910
1911 if (!source) {
1912 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
1913 route->source);
1914 return -ENODEV;
1915 }
1916
1917 if (!sink) {
1918 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
1919 route->sink);
1920 return -ENODEV;
1921 }
1922
1923 if (route->control || route->connected)
1924 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
1925 route->source, route->sink);
1926
1927 list_for_each_entry(path, &source->sinks, list_source) {
1928 if (path->sink == sink) {
1929 path->weak = 1;
1930 count++;
1931 }
1932 }
1933
1934 if (count == 0)
1935 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
1936 route->source, route->sink);
1937 if (count > 1)
1938 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
1939 count, route->source, route->sink);
1940
1941 return 0;
1942}
1943
1944/**
1945 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
1946 * @dapm: DAPM context
1947 * @route: audio routes
1948 * @num: number of routes
1949 *
1950 * Mark existing routes matching those specified in the passed array
1951 * as being weak, meaning that they are ignored for the purpose of
1952 * power decisions. The main intended use case is for sidetone paths
1953 * which couple audio between other independent paths if they are both
1954 * active in order to make the combination work better at the user
1955 * level but which aren't intended to be "used".
1956 *
1957 * Note that CODEC drivers should not use this as sidetone type paths
1958 * can frequently also be used as bypass paths.
1959 */
1960int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
1961 const struct snd_soc_dapm_route *route, int num)
1962{
1963 int i, err;
1964 int ret = 0;
1965
1966 for (i = 0; i < num; i++) {
1967 err = snd_soc_dapm_weak_route(dapm, route);
1968 if (err)
1969 ret = err;
1970 route++;
1971 }
1972
1973 return ret;
1974}
1975EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
1976
Mark Brown105f1c22008-05-13 14:52:19 +02001977/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001978 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001979 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001980 *
1981 * Checks the codec for any new dapm widgets and creates them if found.
1982 *
1983 * Returns 0 for success.
1984 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001985int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001986{
1987 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00001988 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001989
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001990 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001991 {
1992 if (w->new)
1993 continue;
1994
Stephen Warrenfad59882011-04-28 17:37:59 -06001995 if (w->num_kcontrols) {
1996 w->kcontrols = kzalloc(w->num_kcontrols *
1997 sizeof(struct snd_kcontrol *),
1998 GFP_KERNEL);
1999 if (!w->kcontrols)
2000 return -ENOMEM;
2001 }
2002
Richard Purdie2b97eab2006-10-06 18:32:18 +02002003 switch(w->id) {
2004 case snd_soc_dapm_switch:
2005 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002006 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01002007 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002008 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002009 break;
2010 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002011 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002012 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01002013 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002014 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002015 break;
2016 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01002017 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01002018 w->power_check = dapm_adc_check_power;
2019 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002020 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01002021 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01002022 w->power_check = dapm_dac_check_power;
2023 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002024 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002025 case snd_soc_dapm_out_drv:
Mark Brownb75576d2009-04-20 17:56:13 +01002026 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002027 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002028 break;
2029 case snd_soc_dapm_input:
2030 case snd_soc_dapm_output:
2031 case snd_soc_dapm_micbias:
2032 case snd_soc_dapm_spk:
2033 case snd_soc_dapm_hp:
2034 case snd_soc_dapm_mic:
2035 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01002036 w->power_check = dapm_generic_check_power;
2037 break;
Mark Brown246d0a12009-04-22 18:24:55 +01002038 case snd_soc_dapm_supply:
2039 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002040 case snd_soc_dapm_vmid:
2041 case snd_soc_dapm_pre:
2042 case snd_soc_dapm_post:
2043 break;
2044 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002045
2046 /* Read the initial power state from the device */
2047 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002048 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002049 val &= 1 << w->shift;
2050 if (w->invert)
2051 val = !val;
2052
2053 if (val)
2054 w->power = 1;
2055 }
2056
Richard Purdie2b97eab2006-10-06 18:32:18 +02002057 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002058
2059 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002060 }
2061
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002062 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002063 return 0;
2064}
2065EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2066
2067/**
2068 * snd_soc_dapm_get_volsw - dapm mixer get callback
2069 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002070 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002071 *
2072 * Callback to get the value of a dapm mixer control.
2073 *
2074 * Returns 0 for success.
2075 */
2076int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2077 struct snd_ctl_elem_value *ucontrol)
2078{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002079 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2080 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002081 struct soc_mixer_control *mc =
2082 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002083 unsigned int reg = mc->reg;
2084 unsigned int shift = mc->shift;
2085 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002086 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002087 unsigned int invert = mc->invert;
2088 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002089
Richard Purdie2b97eab2006-10-06 18:32:18 +02002090 ucontrol->value.integer.value[0] =
2091 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2092 if (shift != rshift)
2093 ucontrol->value.integer.value[1] =
2094 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2095 if (invert) {
2096 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002097 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002098 if (shift != rshift)
2099 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002100 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002101 }
2102
2103 return 0;
2104}
2105EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2106
2107/**
2108 * snd_soc_dapm_put_volsw - dapm mixer set callback
2109 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002110 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002111 *
2112 * Callback to set the value of a dapm mixer control.
2113 *
2114 * Returns 0 for success.
2115 */
2116int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2117 struct snd_ctl_elem_value *ucontrol)
2118{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002119 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2120 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2121 struct snd_soc_codec *codec = widget->codec;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002122 struct soc_mixer_control *mc =
2123 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002124 unsigned int reg = mc->reg;
2125 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002126 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002127 unsigned int mask = (1 << fls(max)) - 1;
2128 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002129 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002130 int connect, change;
2131 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002132 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002133
2134 val = (ucontrol->value.integer.value[0] & mask);
2135
2136 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002137 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002138 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002139 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002140
Stephen Warrenfafd2172011-04-28 17:38:00 -06002141 if (val)
2142 /* new connection */
2143 connect = invert ? 0 : 1;
2144 else
2145 /* old connection must be powered down */
2146 connect = invert ? 1 : 0;
2147
2148 mutex_lock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002149
Stephen Warrene9cf7042011-01-27 14:54:05 -07002150 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002151 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002152 for (wi = 0; wi < wlist->num_widgets; wi++) {
2153 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002154
Stephen Warrenfafd2172011-04-28 17:38:00 -06002155 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002156
Stephen Warrenfafd2172011-04-28 17:38:00 -06002157 update.kcontrol = kcontrol;
2158 update.widget = widget;
2159 update.reg = reg;
2160 update.mask = mask;
2161 update.val = val;
2162 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002163
Stephen Warrenfafd2172011-04-28 17:38:00 -06002164 dapm_mixer_update_power(widget, kcontrol, connect);
2165
2166 widget->dapm->update = NULL;
2167 }
Mark Brown283375c2009-12-07 18:09:03 +00002168 }
2169
Stephen Warrenfafd2172011-04-28 17:38:00 -06002170 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002171 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002172}
2173EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2174
2175/**
2176 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2177 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002178 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002179 *
2180 * Callback to get the value of a dapm enumerated double mixer control.
2181 *
2182 * Returns 0 for success.
2183 */
2184int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2185 struct snd_ctl_elem_value *ucontrol)
2186{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002187 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2188 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002189 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002190 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002191
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002192 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002193 ;
2194 val = snd_soc_read(widget->codec, e->reg);
2195 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2196 if (e->shift_l != e->shift_r)
2197 ucontrol->value.enumerated.item[1] =
2198 (val >> e->shift_r) & (bitmask - 1);
2199
2200 return 0;
2201}
2202EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2203
2204/**
2205 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2206 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002207 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002208 *
2209 * Callback to set the value of a dapm enumerated double mixer control.
2210 *
2211 * Returns 0 for success.
2212 */
2213int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2214 struct snd_ctl_elem_value *ucontrol)
2215{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002216 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2217 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2218 struct snd_soc_codec *codec = widget->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002219 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002220 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002221 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00002222 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002223 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002224
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002225 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002226 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002227 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002228 return -EINVAL;
2229 mux = ucontrol->value.enumerated.item[0];
2230 val = mux << e->shift_l;
2231 mask = (bitmask - 1) << e->shift_l;
2232 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002233 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002234 return -EINVAL;
2235 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2236 mask |= (bitmask - 1) << e->shift_r;
2237 }
2238
Stephen Warrenfafd2172011-04-28 17:38:00 -06002239 mutex_lock(&codec->mutex);
2240
Mark Brown3a655772009-10-05 17:23:30 +01002241 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002242 if (change) {
2243 for (wi = 0; wi < wlist->num_widgets; wi++) {
2244 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002245
Stephen Warrenfafd2172011-04-28 17:38:00 -06002246 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002247
Stephen Warrenfafd2172011-04-28 17:38:00 -06002248 update.kcontrol = kcontrol;
2249 update.widget = widget;
2250 update.reg = e->reg;
2251 update.mask = mask;
2252 update.val = val;
2253 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002254
Stephen Warrenfafd2172011-04-28 17:38:00 -06002255 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002256
Stephen Warrenfafd2172011-04-28 17:38:00 -06002257 widget->dapm->update = NULL;
2258 }
2259 }
2260
2261 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002262 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002263}
2264EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2265
2266/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002267 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2268 * @kcontrol: mixer control
2269 * @ucontrol: control element information
2270 *
2271 * Returns 0 for success.
2272 */
2273int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2274 struct snd_ctl_elem_value *ucontrol)
2275{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002276 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2277 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002278
2279 ucontrol->value.enumerated.item[0] = widget->value;
2280
2281 return 0;
2282}
2283EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2284
2285/**
2286 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2287 * @kcontrol: mixer control
2288 * @ucontrol: control element information
2289 *
2290 * Returns 0 for success.
2291 */
2292int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2293 struct snd_ctl_elem_value *ucontrol)
2294{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002295 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2296 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2297 struct snd_soc_codec *codec = widget->codec;
Mark Brownd2b247a2009-10-06 15:21:04 +01002298 struct soc_enum *e =
2299 (struct soc_enum *)kcontrol->private_value;
2300 int change;
2301 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002302 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002303
2304 if (ucontrol->value.enumerated.item[0] >= e->max)
2305 return -EINVAL;
2306
Stephen Warrenfafd2172011-04-28 17:38:00 -06002307 mutex_lock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002308
2309 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002310 if (change) {
2311 for (wi = 0; wi < wlist->num_widgets; wi++) {
2312 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002313
Stephen Warrenfafd2172011-04-28 17:38:00 -06002314 widget->value = ucontrol->value.enumerated.item[0];
2315
2316 dapm_mux_update_power(widget, kcontrol, change,
2317 widget->value, e);
2318 }
2319 }
2320
2321 mutex_unlock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002322 return ret;
2323}
2324EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2325
2326/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002327 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2328 * callback
2329 * @kcontrol: mixer control
2330 * @ucontrol: control element information
2331 *
2332 * Callback to get the value of a dapm semi enumerated double mixer control.
2333 *
2334 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2335 * used for handling bitfield coded enumeration for example.
2336 *
2337 * Returns 0 for success.
2338 */
2339int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2340 struct snd_ctl_elem_value *ucontrol)
2341{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002342 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2343 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002344 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002345 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002346
2347 reg_val = snd_soc_read(widget->codec, e->reg);
2348 val = (reg_val >> e->shift_l) & e->mask;
2349 for (mux = 0; mux < e->max; mux++) {
2350 if (val == e->values[mux])
2351 break;
2352 }
2353 ucontrol->value.enumerated.item[0] = mux;
2354 if (e->shift_l != e->shift_r) {
2355 val = (reg_val >> e->shift_r) & e->mask;
2356 for (mux = 0; mux < e->max; mux++) {
2357 if (val == e->values[mux])
2358 break;
2359 }
2360 ucontrol->value.enumerated.item[1] = mux;
2361 }
2362
2363 return 0;
2364}
2365EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2366
2367/**
2368 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2369 * callback
2370 * @kcontrol: mixer control
2371 * @ucontrol: control element information
2372 *
2373 * Callback to set the value of a dapm semi enumerated double mixer control.
2374 *
2375 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2376 * used for handling bitfield coded enumeration for example.
2377 *
2378 * Returns 0 for success.
2379 */
2380int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2381 struct snd_ctl_elem_value *ucontrol)
2382{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002383 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2384 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2385 struct snd_soc_codec *codec = widget->codec;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002386 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002387 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002388 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002389 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002390 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002391
2392 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2393 return -EINVAL;
2394 mux = ucontrol->value.enumerated.item[0];
2395 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2396 mask = e->mask << e->shift_l;
2397 if (e->shift_l != e->shift_r) {
2398 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2399 return -EINVAL;
2400 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2401 mask |= e->mask << e->shift_r;
2402 }
2403
Stephen Warrenfafd2172011-04-28 17:38:00 -06002404 mutex_lock(&codec->mutex);
2405
Mark Brown3a655772009-10-05 17:23:30 +01002406 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002407 if (change) {
2408 for (wi = 0; wi < wlist->num_widgets; wi++) {
2409 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002410
Stephen Warrenfafd2172011-04-28 17:38:00 -06002411 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002412
Stephen Warrenfafd2172011-04-28 17:38:00 -06002413 update.kcontrol = kcontrol;
2414 update.widget = widget;
2415 update.reg = e->reg;
2416 update.mask = mask;
2417 update.val = val;
2418 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002419
Stephen Warrenfafd2172011-04-28 17:38:00 -06002420 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002421
Stephen Warrenfafd2172011-04-28 17:38:00 -06002422 widget->dapm->update = NULL;
2423 }
2424 }
2425
2426 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002427 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002428}
2429EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2430
2431/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002432 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2433 *
2434 * @kcontrol: mixer control
2435 * @uinfo: control element information
2436 *
2437 * Callback to provide information about a pin switch control.
2438 */
2439int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2440 struct snd_ctl_elem_info *uinfo)
2441{
2442 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2443 uinfo->count = 1;
2444 uinfo->value.integer.min = 0;
2445 uinfo->value.integer.max = 1;
2446
2447 return 0;
2448}
2449EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2450
2451/**
2452 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2453 *
2454 * @kcontrol: mixer control
2455 * @ucontrol: Value
2456 */
2457int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2458 struct snd_ctl_elem_value *ucontrol)
2459{
2460 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2461 const char *pin = (const char *)kcontrol->private_value;
2462
2463 mutex_lock(&codec->mutex);
2464
2465 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002466 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002467
2468 mutex_unlock(&codec->mutex);
2469
2470 return 0;
2471}
2472EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2473
2474/**
2475 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2476 *
2477 * @kcontrol: mixer control
2478 * @ucontrol: Value
2479 */
2480int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2481 struct snd_ctl_elem_value *ucontrol)
2482{
2483 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2484 const char *pin = (const char *)kcontrol->private_value;
2485
2486 mutex_lock(&codec->mutex);
2487
2488 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002489 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002490 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002491 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002492
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002493 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002494
2495 mutex_unlock(&codec->mutex);
2496
2497 return 0;
2498}
2499EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2500
2501/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002502 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002503 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002504 * @widget: widget template
2505 *
2506 * Creates a new dapm control based upon the template.
2507 *
2508 * Returns 0 for success else error.
2509 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002510int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002511 const struct snd_soc_dapm_widget *widget)
2512{
2513 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002514 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002515
2516 if ((w = dapm_cnew_widget(widget)) == NULL)
2517 return -ENOMEM;
2518
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002519 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002520 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002521 name_len += 1 + strlen(dapm->codec->name_prefix);
2522 w->name = kmalloc(name_len, GFP_KERNEL);
2523 if (w->name == NULL) {
2524 kfree(w);
2525 return -ENOMEM;
2526 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002527 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002528 snprintf(w->name, name_len, "%s %s",
2529 dapm->codec->name_prefix, widget->name);
2530 else
2531 snprintf(w->name, name_len, "%s", widget->name);
2532
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002533 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002534 w->dapm = dapm;
2535 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01002536 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002537 INIT_LIST_HEAD(&w->sources);
2538 INIT_LIST_HEAD(&w->sinks);
2539 INIT_LIST_HEAD(&w->list);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002540 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002541
2542 /* machine layer set ups unconnected pins and insertions */
2543 w->connected = 1;
2544 return 0;
2545}
2546EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2547
2548/**
Mark Brown4ba13272008-05-13 14:51:19 +02002549 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002550 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002551 * @widget: widget array
2552 * @num: number of widgets
2553 *
2554 * Creates new DAPM controls based upon the templates.
2555 *
2556 * Returns 0 for success else error.
2557 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002558int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002559 const struct snd_soc_dapm_widget *widget,
2560 int num)
2561{
2562 int i, ret;
2563
2564 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002565 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002566 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002567 dev_err(dapm->dev,
2568 "ASoC: Failed to create DAPM control %s: %d\n",
2569 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002570 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002571 }
Mark Brown4ba13272008-05-13 14:51:19 +02002572 widget++;
2573 }
2574 return 0;
2575}
2576EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2577
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002578static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002579 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002580{
2581 struct snd_soc_dapm_widget *w;
2582
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002583 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002584 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002585 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002586 continue;
Liam Girdwoodee47b362011-07-25 11:15:50 +01002587 dev_vdbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002588 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002589 if (strstr(w->sname, stream)) {
2590 switch(event) {
2591 case SND_SOC_DAPM_STREAM_START:
2592 w->active = 1;
2593 break;
2594 case SND_SOC_DAPM_STREAM_STOP:
2595 w->active = 0;
2596 break;
2597 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002598 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002599 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002600 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2601 break;
2602 }
2603 }
2604 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002605
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002606 dapm_power_widgets(dapm, event);
Liam Girdwood64a648c2011-07-25 11:15:15 +01002607
2608 /* do we need to notify any clients that DAPM stream is complete */
2609 if (dapm->stream_event)
2610 dapm->stream_event(dapm, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002611}
2612
2613/**
2614 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2615 * @rtd: PCM runtime data
2616 * @stream: stream name
2617 * @event: stream event
2618 *
2619 * Sends a stream event to the dapm core. The core then makes any
2620 * necessary widget power changes.
2621 *
2622 * Returns 0 for success else error.
2623 */
2624int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2625 const char *stream, int event)
2626{
2627 struct snd_soc_codec *codec = rtd->codec;
2628
2629 if (stream == NULL)
2630 return 0;
2631
2632 mutex_lock(&codec->mutex);
2633 soc_dapm_stream_event(&codec->dapm, stream, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002634 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002635 return 0;
2636}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002637
2638/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002639 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002640 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002641 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002642 *
Mark Brown74b8f952009-06-06 11:26:15 +01002643 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002644 * a valid audio route and active audio stream.
2645 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2646 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002647 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002648int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002649{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002650 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002651}
Liam Girdwooda5302182008-07-07 13:35:17 +01002652EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002653
2654/**
Mark Brownda341832010-03-15 19:23:37 +00002655 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002656 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002657 * @pin: pin name
2658 *
2659 * Enables input/output pin regardless of any other state. This is
2660 * intended for use with microphone bias supplies used in microphone
2661 * jack detection.
2662 *
2663 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2664 * do any widget power switching.
2665 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002666int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2667 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002668{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002669 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00002670
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002671 if (!w) {
2672 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2673 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00002674 }
2675
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002676 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2677 w->connected = 1;
2678 w->force = 1;
Mark Brown0d867332011-04-06 11:38:14 +09002679
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002680 return 0;
Mark Brownda341832010-03-15 19:23:37 +00002681}
2682EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2683
2684/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002685 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002686 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002687 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002688 *
Mark Brown74b8f952009-06-06 11:26:15 +01002689 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002690 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2691 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002692 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002693int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2694 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002695{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002696 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002697}
2698EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2699
2700/**
Mark Brown5817b522008-09-24 11:23:11 +01002701 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002702 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002703 * @pin: pin name
2704 *
2705 * Marks the specified pin as being not connected, disabling it along
2706 * any parent or child widgets. At present this is identical to
2707 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2708 * additional things such as disabling controls which only affect
2709 * paths through the pin.
2710 *
2711 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2712 * do any widget power switching.
2713 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002714int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002715{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002716 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002717}
2718EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2719
2720/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002721 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002722 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002723 * @pin: audio signal pin endpoint (or start point)
2724 *
2725 * Get audio pin status - connected or disconnected.
2726 *
2727 * Returns 1 for connected otherwise 0.
2728 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002729int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2730 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002731{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002732 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002733
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002734 if (w)
2735 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06002736
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002737 return 0;
2738}
Liam Girdwooda5302182008-07-07 13:35:17 +01002739EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002740
2741/**
Mark Brown1547aba2010-05-07 21:11:40 +01002742 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002743 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01002744 * @pin: audio signal pin endpoint (or start point)
2745 *
2746 * Mark the given endpoint or pin as ignoring suspend. When the
2747 * system is disabled a path between two endpoints flagged as ignoring
2748 * suspend will not be disabled. The path must already be enabled via
2749 * normal means at suspend time, it will not be turned on if it was not
2750 * already enabled.
2751 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002752int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2753 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01002754{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002755 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01002756
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002757 if (!w) {
2758 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2759 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01002760 }
2761
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002762 w->ignore_suspend = 1;
2763
2764 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01002765}
2766EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2767
2768/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002769 * snd_soc_dapm_free - free dapm resources
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002770 * @card: SoC device
Richard Purdie2b97eab2006-10-06 18:32:18 +02002771 *
2772 * Free all dapm widgets and resources.
2773 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002774void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002775{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002776 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002777 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002778 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02002779 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002780}
2781EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2782
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002783static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01002784{
Mark Brown51737472009-06-22 13:16:51 +01002785 struct snd_soc_dapm_widget *w;
2786 LIST_HEAD(down_list);
2787 int powerdown = 0;
2788
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002789 list_for_each_entry(w, &dapm->card->widgets, list) {
2790 if (w->dapm != dapm)
2791 continue;
Mark Brown51737472009-06-22 13:16:51 +01002792 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00002793 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002794 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002795 powerdown = 1;
2796 }
2797 }
2798
2799 /* If there were no widgets to power down we're already in
2800 * standby.
2801 */
2802 if (powerdown) {
Mark Browned5a4c42011-02-18 11:12:42 -08002803 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00002804 dapm_seq_run(dapm, &down_list, 0, false);
Mark Browned5a4c42011-02-18 11:12:42 -08002805 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01002806 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002807}
Mark Brown51737472009-06-22 13:16:51 +01002808
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002809/*
2810 * snd_soc_dapm_shutdown - callback for system shutdown
2811 */
2812void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2813{
2814 struct snd_soc_codec *codec;
2815
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002816 list_for_each_entry(codec, &card->codec_dev_list, list) {
2817 soc_dapm_shutdown_codec(&codec->dapm);
Mark Browned5a4c42011-02-18 11:12:42 -08002818 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002819 }
Mark Brown51737472009-06-22 13:16:51 +01002820}
2821
Richard Purdie2b97eab2006-10-06 18:32:18 +02002822/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002823MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002824MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2825MODULE_LICENSE("GPL");