blob: d989dd71e3ac77e5d32b9eb1f8e1fa57f0695c48 [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
Mark Brown452c5ea2009-05-17 21:41:23 +0100127/**
128 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800129 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100130 * @level: level to configure
131 *
132 * Configure the bias (power) levels for the SoC audio device.
133 *
134 * Returns 0 for success else error.
135 */
Mark Browned5a4c42011-02-18 11:12:42 -0800136static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200137 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100138{
Mark Browned5a4c42011-02-18 11:12:42 -0800139 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100140 int ret = 0;
141
Mark Brown84e90932010-11-04 00:07:02 -0400142 trace_snd_soc_bias_level_start(card, level);
143
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000144 if (card && card->set_bias_level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100145 ret = card->set_bias_level(card, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100146 if (ret != 0)
147 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100148
Mark Brown171ec6b2011-06-06 18:15:19 +0100149 if (dapm->codec && dapm->codec->driver->set_bias_level)
150 ret = dapm->codec->driver->set_bias_level(dapm->codec, level);
151 else
152 dapm->bias_level = level;
153 if (ret != 0)
154 goto out;
155
156 if (card && card->set_bias_level_post)
157 ret = card->set_bias_level_post(card, level);
158out:
Mark Brown84e90932010-11-04 00:07:02 -0400159 trace_snd_soc_bias_level_done(card, level);
160
Mark Brown452c5ea2009-05-17 21:41:23 +0100161 return ret;
162}
163
Richard Purdie2b97eab2006-10-06 18:32:18 +0200164/* set up initial codec paths */
165static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
166 struct snd_soc_dapm_path *p, int i)
167{
168 switch (w->id) {
169 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000170 case snd_soc_dapm_mixer:
171 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200172 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100173 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600174 w->kcontrol_news[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400175 unsigned int reg = mc->reg;
176 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100177 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400178 unsigned int mask = (1 << fls(max)) - 1;
179 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200180
181 val = snd_soc_read(w->codec, reg);
182 val = (val >> shift) & mask;
183
184 if ((invert && !val) || (!invert && val))
185 p->connect = 1;
186 else
187 p->connect = 0;
188 }
189 break;
190 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600191 struct soc_enum *e = (struct soc_enum *)
192 w->kcontrol_news[i].private_value;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200193 int val, item, bitmask;
194
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100195 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Mark Brown88d96082011-06-06 16:16:34 +0100196 ;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200197 val = snd_soc_read(w->codec, e->reg);
198 item = (val >> e->shift_l) & (bitmask - 1);
199
200 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100201 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200202 if (!(strcmp(p->name, e->texts[i])) && item == i)
203 p->connect = 1;
204 }
205 }
206 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000207 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600208 struct soc_enum *e = (struct soc_enum *)
209 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000210
211 p->connect = 0;
212 /* since a virtual mux has no backing registers to
213 * decide which path to connect, it will try to match
214 * with the first enumeration. This is to ensure
215 * that the default mux choice (the first) will be
216 * correctly powered up during initialization.
217 */
218 if (!strcmp(p->name, e->texts[0]))
219 p->connect = 1;
220 }
221 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200222 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200223 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600224 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200225 int val, item;
226
227 val = snd_soc_read(w->codec, e->reg);
228 val = (val >> e->shift_l) & e->mask;
229 for (item = 0; item < e->max; item++) {
230 if (val == e->values[item])
231 break;
232 }
233
234 p->connect = 0;
235 for (i = 0; i < e->max; i++) {
236 if (!(strcmp(p->name, e->texts[i])) && item == i)
237 p->connect = 1;
238 }
239 }
240 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200241 /* does not effect routing - always connected */
242 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600243 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200244 case snd_soc_dapm_output:
245 case snd_soc_dapm_adc:
246 case snd_soc_dapm_input:
247 case snd_soc_dapm_dac:
248 case snd_soc_dapm_micbias:
249 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100250 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100251 case snd_soc_dapm_aif_in:
252 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200253 p->connect = 1;
254 break;
255 /* does effect routing - dynamically connected */
256 case snd_soc_dapm_hp:
257 case snd_soc_dapm_mic:
258 case snd_soc_dapm_spk:
259 case snd_soc_dapm_line:
260 case snd_soc_dapm_pre:
261 case snd_soc_dapm_post:
262 p->connect = 0;
263 break;
264 }
265}
266
Mark Brown74b8f952009-06-06 11:26:15 +0100267/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200268static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200269 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
270 struct snd_soc_dapm_path *path, const char *control_name,
271 const struct snd_kcontrol_new *kcontrol)
272{
273 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
274 int i;
275
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100276 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200277 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200278 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200279 list_add(&path->list_sink, &dest->sources);
280 list_add(&path->list_source, &src->sinks);
281 path->name = (char*)e->texts[i];
282 dapm_set_path_status(dest, path, 0);
283 return 0;
284 }
285 }
286
287 return -ENODEV;
288}
289
Mark Brown74b8f952009-06-06 11:26:15 +0100290/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200291static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200292 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
293 struct snd_soc_dapm_path *path, const char *control_name)
294{
295 int i;
296
297 /* search for mixer kcontrol */
298 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600299 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200300 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200301 list_add(&path->list_sink, &dest->sources);
302 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600303 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200304 dapm_set_path_status(dest, path, i);
305 return 0;
306 }
307 }
308 return -ENODEV;
309}
310
Stephen Warrenaf468002011-04-28 17:38:01 -0600311static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600312 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600313 const struct snd_kcontrol_new *kcontrol_new,
314 struct snd_kcontrol **kcontrol)
315{
316 struct snd_soc_dapm_widget *w;
317 int i;
318
319 *kcontrol = NULL;
320
321 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600322 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
323 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600324 for (i = 0; i < w->num_kcontrols; i++) {
325 if (&w->kcontrol_news[i] == kcontrol_new) {
326 if (w->kcontrols)
327 *kcontrol = w->kcontrols[i];
328 return 1;
329 }
330 }
331 }
332
333 return 0;
334}
335
Richard Purdie2b97eab2006-10-06 18:32:18 +0200336/* create new dapm mixer control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200337static int dapm_new_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200338 struct snd_soc_dapm_widget *w)
339{
340 int i, ret = 0;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000341 size_t name_len, prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200342 struct snd_soc_dapm_path *path;
Mark Brown12ea2c72011-03-02 18:17:32 +0000343 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000344 const char *prefix;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600345 struct snd_soc_dapm_widget_list *wlist;
346 size_t wlistsize;
Mark Brownefb7ac32011-03-08 17:23:24 +0000347
348 if (dapm->codec)
349 prefix = dapm->codec->name_prefix;
350 else
351 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200352
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000353 if (prefix)
354 prefix_len = strlen(prefix) + 1;
355 else
356 prefix_len = 0;
357
Richard Purdie2b97eab2006-10-06 18:32:18 +0200358 /* add kcontrol */
359 for (i = 0; i < w->num_kcontrols; i++) {
360
361 /* match name */
362 list_for_each_entry(path, &w->sources, list_sink) {
363
364 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600365 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200366 continue;
367
Stephen Warrenfafd2172011-04-28 17:38:00 -0600368 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
369 sizeof(struct snd_soc_dapm_widget *),
370 wlist = kzalloc(wlistsize, GFP_KERNEL);
371 if (wlist == NULL) {
372 dev_err(dapm->dev,
373 "asoc: can't allocate widget list for %s\n",
374 w->name);
375 return -ENOMEM;
376 }
377 wlist->num_widgets = 1;
378 wlist->widgets[0] = w;
379
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000380 /* add dapm control with long name.
381 * for dapm_mixer this is the concatenation of the
382 * mixer and kcontrol name.
383 * for dapm_mixer_named_ctl this is simply the
384 * kcontrol name.
385 */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600386 name_len = strlen(w->kcontrol_news[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000387 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000388 name_len += 1 + strlen(w->name);
389
Mark Brown219b93f2008-10-28 13:02:31 +0000390 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000391
Stephen Warrenfafd2172011-04-28 17:38:00 -0600392 if (path->long_name == NULL) {
393 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200394 return -ENOMEM;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600395 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200396
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000397 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000398 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000399 /* The control will get a prefix from
400 * the control creation process but
401 * we're also using the same prefix
402 * for widgets so cut the prefix off
403 * the front of the widget name.
404 */
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000405 snprintf(path->long_name, name_len, "%s %s",
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000406 w->name + prefix_len,
Stephen Warren82cfecd2011-04-28 17:37:58 -0600407 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000408 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000409 case snd_soc_dapm_mixer_named_ctl:
410 snprintf(path->long_name, name_len, "%s",
Stephen Warren82cfecd2011-04-28 17:37:58 -0600411 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000412 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000413 }
414
Mark Brown219b93f2008-10-28 13:02:31 +0000415 path->long_name[name_len - 1] = '\0';
416
Stephen Warrenfafd2172011-04-28 17:38:00 -0600417 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
418 wlist, path->long_name,
419 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200420 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200421 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200422 dev_err(dapm->dev,
423 "asoc: failed to add dapm kcontrol %s: %d\n",
424 path->long_name, ret);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600425 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200426 kfree(path->long_name);
427 path->long_name = NULL;
428 return ret;
429 }
Stephen Warrenfad59882011-04-28 17:37:59 -0600430 w->kcontrols[i] = path->kcontrol;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200431 }
432 }
433 return ret;
434}
435
436/* create new dapm mux control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200437static int dapm_new_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200438 struct snd_soc_dapm_widget *w)
439{
440 struct snd_soc_dapm_path *path = NULL;
441 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000442 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000443 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000444 size_t prefix_len;
Stephen Warrenaf468002011-04-28 17:38:01 -0600445 int ret;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600446 struct snd_soc_dapm_widget_list *wlist;
Stephen Warrenaf468002011-04-28 17:38:01 -0600447 int shared, wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600448 size_t wlistsize;
Stephen Warrenaf468002011-04-28 17:38:01 -0600449 char *name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200450
Stephen Warrenaf468002011-04-28 17:38:01 -0600451 if (w->num_kcontrols != 1) {
452 dev_err(dapm->dev,
453 "asoc: mux %s has incorrect number of controls\n",
454 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200455 return -EINVAL;
456 }
457
Stephen Warren1007da02011-05-26 09:57:33 -0600458 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
Stephen Warrenaf468002011-04-28 17:38:01 -0600459 &kcontrol);
460 if (kcontrol) {
461 wlist = kcontrol->private_data;
462 wlistentries = wlist->num_widgets + 1;
463 } else {
464 wlist = NULL;
465 wlistentries = 1;
466 }
Stephen Warrenfafd2172011-04-28 17:38:00 -0600467 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
Stephen Warrenaf468002011-04-28 17:38:01 -0600468 wlistentries * sizeof(struct snd_soc_dapm_widget *),
469 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600470 if (wlist == NULL) {
471 dev_err(dapm->dev,
472 "asoc: can't allocate widget list for %s\n", w->name);
473 return -ENOMEM;
474 }
Stephen Warrenaf468002011-04-28 17:38:01 -0600475 wlist->num_widgets = wlistentries;
476 wlist->widgets[wlistentries - 1] = w;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600477
Stephen Warrenaf468002011-04-28 17:38:01 -0600478 if (!kcontrol) {
479 if (dapm->codec)
480 prefix = dapm->codec->name_prefix;
481 else
482 prefix = NULL;
Mark Brownefb7ac32011-03-08 17:23:24 +0000483
Stephen Warrenaf468002011-04-28 17:38:01 -0600484 if (shared) {
485 name = w->kcontrol_news[0].name;
486 prefix_len = 0;
487 } else {
488 name = w->name;
489 if (prefix)
490 prefix_len = strlen(prefix) + 1;
491 else
492 prefix_len = 0;
493 }
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000494
Stephen Warrenaf468002011-04-28 17:38:01 -0600495 /*
496 * The control will get a prefix from the control creation
497 * process but we're also using the same prefix for widgets so
498 * cut the prefix off the front of the widget name.
499 */
500 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
501 name + prefix_len, prefix);
502 ret = snd_ctl_add(card, kcontrol);
503 if (ret < 0) {
504 dev_err(dapm->dev,
505 "asoc: failed to add kcontrol %s\n", w->name);
506 kfree(wlist);
507 return ret;
508 }
509 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200510
Stephen Warrenaf468002011-04-28 17:38:01 -0600511 kcontrol->private_data = wlist;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200512
Stephen Warrenfad59882011-04-28 17:37:59 -0600513 w->kcontrols[0] = kcontrol;
514
Richard Purdie2b97eab2006-10-06 18:32:18 +0200515 list_for_each_entry(path, &w->sources, list_sink)
516 path->kcontrol = kcontrol;
517
Stephen Warrenaf468002011-04-28 17:38:01 -0600518 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200519}
520
521/* create new dapm volume control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200522static int dapm_new_pga(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200523 struct snd_soc_dapm_widget *w)
524{
Mark Browna6c65732010-03-03 17:45:21 +0000525 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200526 dev_err(w->dapm->dev,
527 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200528
Mark Browna6c65732010-03-03 17:45:21 +0000529 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200530}
531
532/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200533static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200534{
535 struct snd_soc_dapm_path *p;
536
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200537 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200538 p->walked = 0;
539}
540
Mark Brown99497882010-05-07 20:24:05 +0100541/* We implement power down on suspend by checking the power state of
542 * the ALSA card - when we are suspending the ALSA state for the card
543 * is set to D3.
544 */
545static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
546{
Mark Brown12ea2c72011-03-02 18:17:32 +0000547 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100548
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000549 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100550 case SNDRV_CTL_POWER_D3hot:
551 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100552 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200553 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
554 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100555 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100556 default:
557 return 1;
558 }
559}
560
Richard Purdie2b97eab2006-10-06 18:32:18 +0200561/*
562 * Recursively check for a completed path to an active or physically connected
563 * output widget. Returns number of complete paths.
564 */
565static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
566{
567 struct snd_soc_dapm_path *path;
568 int con = 0;
569
Mark Brown246d0a12009-04-22 18:24:55 +0100570 if (widget->id == snd_soc_dapm_supply)
571 return 0;
572
Mark Brown010ff262009-08-17 17:39:22 +0100573 switch (widget->id) {
574 case snd_soc_dapm_adc:
575 case snd_soc_dapm_aif_out:
576 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100577 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100578 default:
579 break;
580 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200581
582 if (widget->connected) {
583 /* connected pin ? */
584 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100585 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200586
587 /* connected jack or spk ? */
588 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300589 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown99497882010-05-07 20:24:05 +0100590 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200591 }
592
593 list_for_each_entry(path, &widget->sinks, list_source) {
594 if (path->walked)
595 continue;
596
597 if (path->sink && path->connect) {
598 path->walked = 1;
599 con += is_connected_output_ep(path->sink);
600 }
601 }
602
603 return con;
604}
605
606/*
607 * Recursively check for a completed path to an active or physically connected
608 * input widget. Returns number of complete paths.
609 */
610static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
611{
612 struct snd_soc_dapm_path *path;
613 int con = 0;
614
Mark Brown246d0a12009-04-22 18:24:55 +0100615 if (widget->id == snd_soc_dapm_supply)
616 return 0;
617
Richard Purdie2b97eab2006-10-06 18:32:18 +0200618 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100619 switch (widget->id) {
620 case snd_soc_dapm_dac:
621 case snd_soc_dapm_aif_in:
622 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100623 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100624 default:
625 break;
626 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200627
628 if (widget->connected) {
629 /* connected pin ? */
630 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100631 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200632
633 /* connected VMID/Bias for lower pops */
634 if (widget->id == snd_soc_dapm_vmid)
Mark Brown99497882010-05-07 20:24:05 +0100635 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200636
637 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300638 if (widget->id == snd_soc_dapm_mic ||
639 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown99497882010-05-07 20:24:05 +0100640 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200641 }
642
643 list_for_each_entry(path, &widget->sources, list_sink) {
644 if (path->walked)
645 continue;
646
647 if (path->source && path->connect) {
648 path->walked = 1;
649 con += is_connected_input_ep(path->source);
650 }
651 }
652
653 return con;
654}
655
656/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300657 * Handler for generic register modifier widget.
658 */
659int dapm_reg_event(struct snd_soc_dapm_widget *w,
660 struct snd_kcontrol *kcontrol, int event)
661{
662 unsigned int val;
663
664 if (SND_SOC_DAPM_EVENT_ON(event))
665 val = w->on_val;
666 else
667 val = w->off_val;
668
669 snd_soc_update_bits(w->codec, -(w->reg + 1),
670 w->mask << w->shift, val << w->shift);
671
672 return 0;
673}
Mark Brown11589412008-07-29 11:42:23 +0100674EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300675
Mark Browncd0f2d42009-04-20 16:56:59 +0100676/* Generic check to see if a widget should be powered.
677 */
678static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
679{
680 int in, out;
681
682 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200683 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100684 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200685 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100686 return out != 0 && in != 0;
687}
688
Mark Brown6ea31b92009-04-20 17:15:41 +0100689/* Check to see if an ADC has power */
690static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
691{
692 int in;
693
694 if (w->active) {
695 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200696 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100697 return in != 0;
698 } else {
699 return dapm_generic_check_power(w);
700 }
701}
702
703/* Check to see if a DAC has power */
704static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
705{
706 int out;
707
708 if (w->active) {
709 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200710 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100711 return out != 0;
712 } else {
713 return dapm_generic_check_power(w);
714 }
715}
716
Mark Brown246d0a12009-04-22 18:24:55 +0100717/* Check to see if a power supply is needed */
718static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
719{
720 struct snd_soc_dapm_path *path;
721 int power = 0;
722
723 /* Check if one of our outputs is connected */
724 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +0100725 if (path->connected &&
726 !path->connected(path->source, path->sink))
727 continue;
728
Mark Brown30173582011-02-11 11:42:19 +0000729 if (!path->sink)
730 continue;
731
732 if (path->sink->force) {
733 power = 1;
734 break;
735 }
736
737 if (path->sink->power_check &&
Mark Brown246d0a12009-04-22 18:24:55 +0100738 path->sink->power_check(path->sink)) {
739 power = 1;
740 break;
741 }
742 }
743
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200744 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100745
746 return power;
747}
748
Mark Brown38357ab2009-06-06 19:03:23 +0100749static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
750 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +0000751 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +0000752{
Mark Brown828a8422011-01-15 13:14:30 +0000753 int *sort;
754
755 if (power_up)
756 sort = dapm_up_seq;
757 else
758 sort = dapm_down_seq;
759
Mark Brown38357ab2009-06-06 19:03:23 +0100760 if (sort[a->id] != sort[b->id])
761 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +0000762 if (a->subseq != b->subseq) {
763 if (power_up)
764 return a->subseq - b->subseq;
765 else
766 return b->subseq - a->subseq;
767 }
Mark Brownb22ead22009-06-07 12:51:26 +0100768 if (a->reg != b->reg)
769 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +0000770 if (a->dapm != b->dapm)
771 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +0000772
Mark Brown38357ab2009-06-06 19:03:23 +0100773 return 0;
774}
Mark Brown42aa3412009-03-01 19:21:10 +0000775
Mark Brown38357ab2009-06-06 19:03:23 +0100776/* Insert a widget in order into a DAPM power sequence. */
777static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
778 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +0000779 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +0100780{
781 struct snd_soc_dapm_widget *w;
782
783 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +0000784 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +0100785 list_add_tail(&new_widget->power_list, &w->power_list);
786 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000787 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100788
Mark Brown38357ab2009-06-06 19:03:23 +0100789 list_add_tail(&new_widget->power_list, list);
790}
Mark Brown42aa3412009-03-01 19:21:10 +0000791
Mark Brown68f89ad2010-11-03 23:51:49 -0400792static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
793 struct snd_soc_dapm_widget *w, int event)
794{
795 struct snd_soc_card *card = dapm->card;
796 const char *ev_name;
797 int power, ret;
798
799 switch (event) {
800 case SND_SOC_DAPM_PRE_PMU:
801 ev_name = "PRE_PMU";
802 power = 1;
803 break;
804 case SND_SOC_DAPM_POST_PMU:
805 ev_name = "POST_PMU";
806 power = 1;
807 break;
808 case SND_SOC_DAPM_PRE_PMD:
809 ev_name = "PRE_PMD";
810 power = 0;
811 break;
812 case SND_SOC_DAPM_POST_PMD:
813 ev_name = "POST_PMD";
814 power = 0;
815 break;
816 default:
817 BUG();
818 return;
819 }
820
821 if (w->power != power)
822 return;
823
824 if (w->event && (w->event_flags & event)) {
825 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
826 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -0400827 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400828 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -0400829 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400830 if (ret < 0)
831 pr_err("%s: %s event failed: %d\n",
832 ev_name, w->name, ret);
833 }
834}
835
Mark Brownb22ead22009-06-07 12:51:26 +0100836/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200837static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +0100838 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100839{
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200840 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -0400841 struct snd_soc_dapm_widget *w;
842 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +0100843 unsigned int value = 0;
844 unsigned int mask = 0;
845 unsigned int cur_mask;
846
847 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
848 power_list)->reg;
849
850 list_for_each_entry(w, pending, power_list) {
851 cur_mask = 1 << w->shift;
852 BUG_ON(reg != w->reg);
853
854 if (w->invert)
855 power = !w->power;
856 else
857 power = w->power;
858
859 mask |= cur_mask;
860 if (power)
861 value |= cur_mask;
862
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200863 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +0100864 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
865 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100866
Mark Brown68f89ad2010-11-03 23:51:49 -0400867 /* Check for events */
868 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
869 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100870 }
871
Mark Brown81628102009-06-07 13:21:24 +0100872 if (reg >= 0) {
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200873 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +0100874 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200875 value, mask, reg, card->pop_time);
876 pop_wait(card->pop_time);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200877 snd_soc_update_bits(dapm->codec, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +0100878 }
879
880 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -0400881 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
882 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +0000883 }
Mark Brown42aa3412009-03-01 19:21:10 +0000884}
885
Mark Brownb22ead22009-06-07 12:51:26 +0100886/* Apply a DAPM power sequence.
887 *
888 * We walk over a pre-sorted list of widgets to apply power to. In
889 * order to minimise the number of writes to the device required
890 * multiple widgets will be updated in a single write where possible.
891 * Currently anything that requires more than a single write is not
892 * handled.
893 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200894static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +0000895 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +0100896{
897 struct snd_soc_dapm_widget *w, *n;
898 LIST_HEAD(pending);
899 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +0000900 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +0100901 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200902 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +0000903 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +0000904 int *sort;
905
906 if (power_up)
907 sort = dapm_up_seq;
908 else
909 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +0100910
Mark Brownb22ead22009-06-07 12:51:26 +0100911 list_for_each_entry_safe(w, n, list, power_list) {
912 ret = 0;
913
914 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200915 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +0000916 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +0100917 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200918 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +0100919
Mark Brown474b62d2011-01-18 16:14:44 +0000920 if (cur_dapm && cur_dapm->seq_notifier) {
921 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
922 if (sort[i] == cur_sort)
923 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +0000924 i,
925 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +0000926 }
927
Mark Brownb22ead22009-06-07 12:51:26 +0100928 INIT_LIST_HEAD(&pending);
929 cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +0000930 cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +0100931 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200932 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +0100933 }
934
Mark Brown163cac02009-06-07 10:12:52 +0100935 switch (w->id) {
936 case snd_soc_dapm_pre:
937 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100938 list_for_each_entry_safe_continue(w, n, list,
939 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100940
Mark Brownb22ead22009-06-07 12:51:26 +0100941 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100942 ret = w->event(w,
943 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100944 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100945 ret = w->event(w,
946 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +0100947 break;
948
949 case snd_soc_dapm_post:
950 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100951 list_for_each_entry_safe_continue(w, n, list,
952 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100953
Mark Brownb22ead22009-06-07 12:51:26 +0100954 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100955 ret = w->event(w,
956 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100957 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100958 ret = w->event(w,
959 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100960 break;
961
Mark Brown163cac02009-06-07 10:12:52 +0100962 default:
Mark Brown81628102009-06-07 13:21:24 +0100963 /* Queue it up for application */
964 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +0000965 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +0100966 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200967 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +0100968 list_move(&w->power_list, &pending);
969 break;
Mark Brown163cac02009-06-07 10:12:52 +0100970 }
Mark Brownb22ead22009-06-07 12:51:26 +0100971
972 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200973 dev_err(w->dapm->dev,
974 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +0100975 }
Mark Brownb22ead22009-06-07 12:51:26 +0100976
977 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +0000978 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +0000979
980 if (cur_dapm && cur_dapm->seq_notifier) {
981 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
982 if (sort[i] == cur_sort)
983 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +0000984 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +0000985 }
Mark Brown163cac02009-06-07 10:12:52 +0100986}
987
Mark Brown97404f22010-12-14 16:13:57 +0000988static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
989{
990 struct snd_soc_dapm_update *update = dapm->update;
991 struct snd_soc_dapm_widget *w;
992 int ret;
993
994 if (!update)
995 return;
996
997 w = update->widget;
998
999 if (w->event &&
1000 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1001 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1002 if (ret != 0)
1003 pr_err("%s DAPM pre-event failed: %d\n",
1004 w->name, ret);
1005 }
1006
1007 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1008 update->val);
1009 if (ret < 0)
1010 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1011
1012 if (w->event &&
1013 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1014 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1015 if (ret != 0)
1016 pr_err("%s DAPM post-event failed: %d\n",
1017 w->name, ret);
1018 }
1019}
1020
Mark Brown9d0624a2011-02-18 11:49:43 -08001021/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1022 * they're changing state.
1023 */
1024static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1025{
1026 struct snd_soc_dapm_context *d = data;
1027 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001028
Mark Brown56fba412011-06-04 11:25:10 +01001029 /* If we're off and we're not supposed to be go into STANDBY */
1030 if (d->bias_level == SND_SOC_BIAS_OFF &&
1031 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001032 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1033 if (ret != 0)
1034 dev_err(d->dev,
1035 "Failed to turn on bias: %d\n", ret);
1036 }
1037
Mark Brown56fba412011-06-04 11:25:10 +01001038 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1039 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001040 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1041 if (ret != 0)
1042 dev_err(d->dev,
1043 "Failed to prepare bias: %d\n", ret);
1044 }
1045}
1046
1047/* Async callback run prior to DAPM sequences - brings to their final
1048 * state.
1049 */
1050static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1051{
1052 struct snd_soc_dapm_context *d = data;
1053 int ret;
1054
1055 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001056 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1057 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1058 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001059 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1060 if (ret != 0)
1061 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1062 ret);
1063 }
1064
1065 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001066 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1067 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001068 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1069 if (ret != 0)
1070 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1071 }
1072
1073 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001074 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1075 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001076 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1077 if (ret != 0)
1078 dev_err(d->dev, "Failed to apply active bias: %d\n",
1079 ret);
1080 }
1081}
Mark Brown97404f22010-12-14 16:13:57 +00001082
Mark Brown42aa3412009-03-01 19:21:10 +00001083/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001084 * Scan each dapm widget for complete audio path.
1085 * A complete path is a route that has valid endpoints i.e.:-
1086 *
1087 * o DAC to output pin.
1088 * o Input Pin to ADC.
1089 * o Input pin to Output pin (bypass, sidetone)
1090 * o DAC to ADC (loopback).
1091 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001092static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001093{
Mark Brown12ea2c72011-03-02 18:17:32 +00001094 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001095 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001096 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001097 LIST_HEAD(up_list);
1098 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001099 LIST_HEAD(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001100 enum snd_soc_bias_level bias;
Mark Brown38357ab2009-06-06 19:03:23 +01001101 int power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001102
Mark Brown84e90932010-11-04 00:07:02 -04001103 trace_snd_soc_dapm_start(card);
1104
Mark Brown56fba412011-06-04 11:25:10 +01001105 list_for_each_entry(d, &card->dapm_list, list) {
1106 if (d->n_widgets || d->codec == NULL) {
1107 if (d->idle_bias_off)
1108 d->target_bias_level = SND_SOC_BIAS_OFF;
1109 else
1110 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1111 }
1112 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001113
Mark Brown6d3ddc82009-05-16 17:47:29 +01001114 /* Check which widgets we need to power and store them in
1115 * lists indicating if they should be powered up or down.
1116 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001117 list_for_each_entry(w, &card->widgets, list) {
Mark Brown6d3ddc82009-05-16 17:47:29 +01001118 switch (w->id) {
1119 case snd_soc_dapm_pre:
Mark Brown828a8422011-01-15 13:14:30 +00001120 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001121 break;
1122 case snd_soc_dapm_post:
Mark Brown828a8422011-01-15 13:14:30 +00001123 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001124 break;
1125
1126 default:
1127 if (!w->power_check)
1128 continue;
1129
Mark Brown99497882010-05-07 20:24:05 +01001130 if (!w->force)
Mark Brown50b6bce2009-11-23 13:11:53 +00001131 power = w->power_check(w);
Mark Brown99497882010-05-07 20:24:05 +01001132 else
1133 power = 1;
Mark Browndfcc9042011-06-04 11:34:43 +01001134
1135 if (power) {
1136 d = w->dapm;
1137
1138 /* Supplies and micbiases only bring
1139 * the context up to STANDBY as unless
1140 * something else is active and
1141 * passing audio they generally don't
1142 * require full power.
1143 */
1144 switch (w->id) {
1145 case snd_soc_dapm_supply:
1146 case snd_soc_dapm_micbias:
1147 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1148 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1149 break;
1150 default:
1151 d->target_bias_level = SND_SOC_BIAS_ON;
1152 break;
1153 }
1154 }
Mark Brown452c5ea2009-05-17 21:41:23 +01001155
Mark Brown6d3ddc82009-05-16 17:47:29 +01001156 if (w->power == power)
1157 continue;
1158
Mark Brown84e90932010-11-04 00:07:02 -04001159 trace_snd_soc_dapm_widget_power(w, power);
1160
Mark Brown6d3ddc82009-05-16 17:47:29 +01001161 if (power)
Mark Brown828a8422011-01-15 13:14:30 +00001162 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001163 else
Mark Brown828a8422011-01-15 13:14:30 +00001164 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001165
1166 w->power = power;
1167 break;
1168 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001169 }
1170
Mark Brownb14b76a2009-08-17 11:55:38 +01001171 /* If there are no DAPM widgets then try to figure out power from the
1172 * event type.
1173 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001174 if (!dapm->n_widgets) {
Mark Brownb14b76a2009-08-17 11:55:38 +01001175 switch (event) {
1176 case SND_SOC_DAPM_STREAM_START:
1177 case SND_SOC_DAPM_STREAM_RESUME:
Mark Brown56fba412011-06-04 11:25:10 +01001178 dapm->target_bias_level = SND_SOC_BIAS_ON;
Mark Brownb14b76a2009-08-17 11:55:38 +01001179 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001180 case SND_SOC_DAPM_STREAM_STOP:
Mark Brown56fba412011-06-04 11:25:10 +01001181 if (dapm->codec->active)
1182 dapm->target_bias_level = SND_SOC_BIAS_ON;
1183 else
1184 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001185 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001186 case SND_SOC_DAPM_STREAM_SUSPEND:
Mark Brown56fba412011-06-04 11:25:10 +01001187 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown50b6bce2009-11-23 13:11:53 +00001188 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001189 case SND_SOC_DAPM_STREAM_NOP:
Mark Brown56fba412011-06-04 11:25:10 +01001190 dapm->target_bias_level = dapm->bias_level;
Mark Brown50b6bce2009-11-23 13:11:53 +00001191 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001192 default:
1193 break;
1194 }
1195 }
1196
Mark Brown52ba67b2011-04-04 21:05:11 +09001197 /* Force all contexts in the card to the same bias state */
Mark Brown56fba412011-06-04 11:25:10 +01001198 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001199 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001200 if (d->target_bias_level > bias)
1201 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001202 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001203 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001204
1205
Mark Brown9d0624a2011-02-18 11:49:43 -08001206 /* Run all the bias changes in parallel */
1207 list_for_each_entry(d, &dapm->card->dapm_list, list)
1208 async_schedule_domain(dapm_pre_sequence_async, d,
1209 &async_domain);
1210 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001211
Mark Brown6d3ddc82009-05-16 17:47:29 +01001212 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001213 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001214
Mark Brown97404f22010-12-14 16:13:57 +00001215 dapm_widget_update(dapm);
1216
Mark Brown6d3ddc82009-05-16 17:47:29 +01001217 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001218 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001219
Mark Brown9d0624a2011-02-18 11:49:43 -08001220 /* Run all the bias changes in parallel */
1221 list_for_each_entry(d, &dapm->card->dapm_list, list)
1222 async_schedule_domain(dapm_post_sequence_async, d,
1223 &async_domain);
1224 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001225
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001226 pop_dbg(dapm->dev, card->pop_time,
1227 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001228 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001229
Mark Brown84e90932010-11-04 00:07:02 -04001230 trace_snd_soc_dapm_done(card);
1231
Mark Brown42aa3412009-03-01 19:21:10 +00001232 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001233}
1234
Mark Brown79fb9382009-08-21 16:38:13 +01001235#ifdef CONFIG_DEBUG_FS
1236static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1237{
1238 file->private_data = inode->i_private;
1239 return 0;
1240}
1241
1242static ssize_t dapm_widget_power_read_file(struct file *file,
1243 char __user *user_buf,
1244 size_t count, loff_t *ppos)
1245{
1246 struct snd_soc_dapm_widget *w = file->private_data;
1247 char *buf;
1248 int in, out;
1249 ssize_t ret;
1250 struct snd_soc_dapm_path *p = NULL;
1251
1252 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1253 if (!buf)
1254 return -ENOMEM;
1255
1256 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001257 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001258 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001259 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001260
Mark Brownd033c362009-12-04 15:25:56 +00001261 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001262 w->name, w->power ? "On" : "Off", in, out);
1263
Mark Brownd033c362009-12-04 15:25:56 +00001264 if (w->reg >= 0)
1265 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1266 " - R%d(0x%x) bit %d",
1267 w->reg, w->reg, w->shift);
1268
1269 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1270
Mark Brown3eef08b2009-09-14 16:49:00 +01001271 if (w->sname)
1272 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1273 w->sname,
1274 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001275
1276 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001277 if (p->connected && !p->connected(w, p->sink))
1278 continue;
1279
Mark Brown79fb9382009-08-21 16:38:13 +01001280 if (p->connect)
1281 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001282 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001283 p->name ? p->name : "static",
1284 p->source->name);
1285 }
1286 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001287 if (p->connected && !p->connected(w, p->sink))
1288 continue;
1289
Mark Brown79fb9382009-08-21 16:38:13 +01001290 if (p->connect)
1291 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001292 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001293 p->name ? p->name : "static",
1294 p->sink->name);
1295 }
1296
1297 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1298
1299 kfree(buf);
1300 return ret;
1301}
1302
1303static const struct file_operations dapm_widget_power_fops = {
1304 .open = dapm_widget_power_open_file,
1305 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001306 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001307};
1308
Mark Brownef49e4f2011-04-04 20:48:13 +09001309static int dapm_bias_open_file(struct inode *inode, struct file *file)
1310{
1311 file->private_data = inode->i_private;
1312 return 0;
1313}
1314
1315static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1316 size_t count, loff_t *ppos)
1317{
1318 struct snd_soc_dapm_context *dapm = file->private_data;
1319 char *level;
1320
1321 switch (dapm->bias_level) {
1322 case SND_SOC_BIAS_ON:
1323 level = "On\n";
1324 break;
1325 case SND_SOC_BIAS_PREPARE:
1326 level = "Prepare\n";
1327 break;
1328 case SND_SOC_BIAS_STANDBY:
1329 level = "Standby\n";
1330 break;
1331 case SND_SOC_BIAS_OFF:
1332 level = "Off\n";
1333 break;
1334 default:
1335 BUG();
1336 level = "Unknown\n";
1337 break;
1338 }
1339
1340 return simple_read_from_buffer(user_buf, count, ppos, level,
1341 strlen(level));
1342}
1343
1344static const struct file_operations dapm_bias_fops = {
1345 .open = dapm_bias_open_file,
1346 .read = dapm_bias_read_file,
1347 .llseek = default_llseek,
1348};
1349
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001350void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1351 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001352{
Mark Brown79fb9382009-08-21 16:38:13 +01001353 struct dentry *d;
1354
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001355 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1356
1357 if (!dapm->debugfs_dapm) {
1358 printk(KERN_WARNING
1359 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001360 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001361 }
Mark Brown79fb9382009-08-21 16:38:13 +01001362
Mark Brownef49e4f2011-04-04 20:48:13 +09001363 d = debugfs_create_file("bias_level", 0444,
1364 dapm->debugfs_dapm, dapm,
1365 &dapm_bias_fops);
1366 if (!d)
1367 dev_warn(dapm->dev,
1368 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001369}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001370
1371static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1372{
1373 struct snd_soc_dapm_context *dapm = w->dapm;
1374 struct dentry *d;
1375
1376 if (!dapm->debugfs_dapm || !w->name)
1377 return;
1378
1379 d = debugfs_create_file(w->name, 0444,
1380 dapm->debugfs_dapm, w,
1381 &dapm_widget_power_fops);
1382 if (!d)
1383 dev_warn(w->dapm->dev,
1384 "ASoC: Failed to create %s debugfs file\n",
1385 w->name);
1386}
1387
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001388static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1389{
1390 debugfs_remove_recursive(dapm->debugfs_dapm);
1391}
1392
Mark Brown79fb9382009-08-21 16:38:13 +01001393#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001394void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1395 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001396{
1397}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001398
1399static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1400{
1401}
1402
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001403static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1404{
1405}
1406
Mark Brown79fb9382009-08-21 16:38:13 +01001407#endif
1408
Richard Purdie2b97eab2006-10-06 18:32:18 +02001409/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001410static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001411 struct snd_kcontrol *kcontrol, int change,
1412 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001413{
1414 struct snd_soc_dapm_path *path;
1415 int found = 0;
1416
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001417 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001418 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001419 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001420 return -ENODEV;
1421
Mark Brown3a655772009-10-05 17:23:30 +01001422 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001423 return 0;
1424
1425 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001426 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001427 if (path->kcontrol != kcontrol)
1428 continue;
1429
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001430 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001431 continue;
1432
1433 found = 1;
1434 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001435 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001436 path->connect = 1; /* new connection */
1437 else
1438 path->connect = 0; /* old connection must be powered down */
1439 }
1440
Mark Brownb91b8fa2010-01-20 18:18:35 +00001441 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001442 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001443
1444 return 0;
1445}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001446
Milan plzik1b075e32008-01-10 14:39:46 +01001447/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001448static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001449 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001450{
1451 struct snd_soc_dapm_path *path;
1452 int found = 0;
1453
Milan plzik1b075e32008-01-10 14:39:46 +01001454 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001455 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001456 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001457 return -ENODEV;
1458
Richard Purdie2b97eab2006-10-06 18:32:18 +02001459 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001460 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001461 if (path->kcontrol != kcontrol)
1462 continue;
1463
1464 /* found, now check type */
1465 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001466 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001467 break;
1468 }
1469
Mark Brownb91b8fa2010-01-20 18:18:35 +00001470 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001471 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001472
1473 return 0;
1474}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001475
1476/* show dapm widget status in sys fs */
1477static ssize_t dapm_widget_show(struct device *dev,
1478 struct device_attribute *attr, char *buf)
1479{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001480 struct snd_soc_pcm_runtime *rtd =
1481 container_of(dev, struct snd_soc_pcm_runtime, dev);
1482 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001483 struct snd_soc_dapm_widget *w;
1484 int count = 0;
1485 char *state = "not set";
1486
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001487 list_for_each_entry(w, &codec->card->widgets, list) {
1488 if (w->dapm != &codec->dapm)
1489 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001490
1491 /* only display widgets that burnm power */
1492 switch (w->id) {
1493 case snd_soc_dapm_hp:
1494 case snd_soc_dapm_mic:
1495 case snd_soc_dapm_spk:
1496 case snd_soc_dapm_line:
1497 case snd_soc_dapm_micbias:
1498 case snd_soc_dapm_dac:
1499 case snd_soc_dapm_adc:
1500 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001501 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001502 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001503 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001504 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001505 if (w->name)
1506 count += sprintf(buf + count, "%s: %s\n",
1507 w->name, w->power ? "On":"Off");
1508 break;
1509 default:
1510 break;
1511 }
1512 }
1513
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001514 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001515 case SND_SOC_BIAS_ON:
1516 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001517 break;
Mark Brown0be98982008-05-19 12:31:28 +02001518 case SND_SOC_BIAS_PREPARE:
1519 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001520 break;
Mark Brown0be98982008-05-19 12:31:28 +02001521 case SND_SOC_BIAS_STANDBY:
1522 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001523 break;
Mark Brown0be98982008-05-19 12:31:28 +02001524 case SND_SOC_BIAS_OFF:
1525 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001526 break;
1527 }
1528 count += sprintf(buf + count, "PM State: %s\n", state);
1529
1530 return count;
1531}
1532
1533static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1534
1535int snd_soc_dapm_sys_add(struct device *dev)
1536{
Troy Kisky12ef1932008-10-13 17:42:14 -07001537 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001538}
1539
1540static void snd_soc_dapm_sys_remove(struct device *dev)
1541{
Mark Brownaef90842009-05-16 17:53:16 +01001542 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001543}
1544
1545/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001546static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001547{
1548 struct snd_soc_dapm_widget *w, *next_w;
1549 struct snd_soc_dapm_path *p, *next_p;
1550
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001551 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1552 if (w->dapm != dapm)
1553 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001554 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001555 /*
1556 * remove source and sink paths associated to this widget.
1557 * While removing the path, remove reference to it from both
1558 * source and sink widgets so that path is removed only once.
1559 */
1560 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1561 list_del(&p->list_sink);
1562 list_del(&p->list_source);
1563 list_del(&p->list);
1564 kfree(p->long_name);
1565 kfree(p);
1566 }
1567 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1568 list_del(&p->list_sink);
1569 list_del(&p->list_source);
1570 list_del(&p->list);
1571 kfree(p->long_name);
1572 kfree(p);
1573 }
Stephen Warrenfad59882011-04-28 17:37:59 -06001574 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001575 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001576 kfree(w);
1577 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001578}
1579
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001580static struct snd_soc_dapm_widget *dapm_find_widget(
1581 struct snd_soc_dapm_context *dapm, const char *pin,
1582 bool search_other_contexts)
1583{
1584 struct snd_soc_dapm_widget *w;
1585 struct snd_soc_dapm_widget *fallback = NULL;
1586
1587 list_for_each_entry(w, &dapm->card->widgets, list) {
1588 if (!strcmp(w->name, pin)) {
1589 if (w->dapm == dapm)
1590 return w;
1591 else
1592 fallback = w;
1593 }
1594 }
1595
1596 if (search_other_contexts)
1597 return fallback;
1598
1599 return NULL;
1600}
1601
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001602static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001603 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001604{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001605 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01001606
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001607 if (!w) {
1608 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1609 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01001610 }
1611
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001612 w->connected = status;
1613 if (status == 0)
1614 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09001615
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001616 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001617}
1618
Richard Purdie2b97eab2006-10-06 18:32:18 +02001619/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001620 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001621 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001622 *
1623 * Walks all dapm audio paths and powers widgets according to their
1624 * stream or path usage.
1625 *
1626 * Returns 0 for success.
1627 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001628int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001629{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001630 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001631}
Liam Girdwooda5302182008-07-07 13:35:17 +01001632EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001633
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001634static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001635 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001636{
1637 struct snd_soc_dapm_path *path;
1638 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001639 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001640 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001641 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001642 const char *source;
1643 char prefixed_sink[80];
1644 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001645 int ret = 0;
1646
Mark Brown88e8b9a2011-03-02 18:18:24 +00001647 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001648 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1649 dapm->codec->name_prefix, route->sink);
1650 sink = prefixed_sink;
1651 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1652 dapm->codec->name_prefix, route->source);
1653 source = prefixed_source;
1654 } else {
1655 sink = route->sink;
1656 source = route->source;
1657 }
1658
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001659 /*
1660 * find src and dest widgets over all widgets but favor a widget from
1661 * current DAPM context
1662 */
1663 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001664 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001665 wtsink = w;
1666 if (w->dapm == dapm)
1667 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001668 continue;
1669 }
1670 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001671 wtsource = w;
1672 if (w->dapm == dapm)
1673 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001674 }
1675 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001676 /* use widget from another DAPM context if not found from this */
1677 if (!wsink)
1678 wsink = wtsink;
1679 if (!wsource)
1680 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001681
1682 if (wsource == NULL || wsink == NULL)
1683 return -ENODEV;
1684
1685 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1686 if (!path)
1687 return -ENOMEM;
1688
1689 path->source = wsource;
1690 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001691 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001692 INIT_LIST_HEAD(&path->list);
1693 INIT_LIST_HEAD(&path->list_source);
1694 INIT_LIST_HEAD(&path->list_sink);
1695
1696 /* check for external widgets */
1697 if (wsink->id == snd_soc_dapm_input) {
1698 if (wsource->id == snd_soc_dapm_micbias ||
1699 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001700 wsource->id == snd_soc_dapm_line ||
1701 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001702 wsink->ext = 1;
1703 }
1704 if (wsource->id == snd_soc_dapm_output) {
1705 if (wsink->id == snd_soc_dapm_spk ||
1706 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001707 wsink->id == snd_soc_dapm_line ||
1708 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001709 wsource->ext = 1;
1710 }
1711
1712 /* connect static paths */
1713 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001714 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001715 list_add(&path->list_sink, &wsink->sources);
1716 list_add(&path->list_source, &wsource->sinks);
1717 path->connect = 1;
1718 return 0;
1719 }
1720
1721 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08001722 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001723 case snd_soc_dapm_adc:
1724 case snd_soc_dapm_dac:
1725 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001726 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001727 case snd_soc_dapm_input:
1728 case snd_soc_dapm_output:
1729 case snd_soc_dapm_micbias:
1730 case snd_soc_dapm_vmid:
1731 case snd_soc_dapm_pre:
1732 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001733 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001734 case snd_soc_dapm_aif_in:
1735 case snd_soc_dapm_aif_out:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001736 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001737 list_add(&path->list_sink, &wsink->sources);
1738 list_add(&path->list_source, &wsource->sinks);
1739 path->connect = 1;
1740 return 0;
1741 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001742 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001743 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001744 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06001745 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001746 if (ret != 0)
1747 goto err;
1748 break;
1749 case snd_soc_dapm_switch:
1750 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001751 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001752 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001753 if (ret != 0)
1754 goto err;
1755 break;
1756 case snd_soc_dapm_hp:
1757 case snd_soc_dapm_mic:
1758 case snd_soc_dapm_line:
1759 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001760 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001761 list_add(&path->list_sink, &wsink->sources);
1762 list_add(&path->list_source, &wsource->sinks);
1763 path->connect = 0;
1764 return 0;
1765 }
1766 return 0;
1767
1768err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001769 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1770 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001771 kfree(path);
1772 return ret;
1773}
Mark Brown105f1c22008-05-13 14:52:19 +02001774
1775/**
Mark Brown105f1c22008-05-13 14:52:19 +02001776 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001777 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02001778 * @route: audio routes
1779 * @num: number of routes
1780 *
1781 * Connects 2 dapm widgets together via a named audio path. The sink is
1782 * the widget receiving the audio signal, whilst the source is the sender
1783 * of the audio signal.
1784 *
1785 * Returns 0 for success else error. On error all resources can be freed
1786 * with a call to snd_soc_card_free().
1787 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001788int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02001789 const struct snd_soc_dapm_route *route, int num)
1790{
1791 int i, ret;
1792
1793 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001794 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001795 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001796 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1797 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02001798 return ret;
1799 }
1800 route++;
1801 }
1802
1803 return 0;
1804}
1805EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1806
1807/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001808 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001809 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001810 *
1811 * Checks the codec for any new dapm widgets and creates them if found.
1812 *
1813 * Returns 0 for success.
1814 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001815int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001816{
1817 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00001818 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001819
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001820 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001821 {
1822 if (w->new)
1823 continue;
1824
Stephen Warrenfad59882011-04-28 17:37:59 -06001825 if (w->num_kcontrols) {
1826 w->kcontrols = kzalloc(w->num_kcontrols *
1827 sizeof(struct snd_kcontrol *),
1828 GFP_KERNEL);
1829 if (!w->kcontrols)
1830 return -ENOMEM;
1831 }
1832
Richard Purdie2b97eab2006-10-06 18:32:18 +02001833 switch(w->id) {
1834 case snd_soc_dapm_switch:
1835 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001836 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001837 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001838 dapm_new_mixer(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001839 break;
1840 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001841 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001842 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001843 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001844 dapm_new_mux(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001845 break;
1846 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01001847 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01001848 w->power_check = dapm_adc_check_power;
1849 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001850 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01001851 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01001852 w->power_check = dapm_dac_check_power;
1853 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001854 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001855 case snd_soc_dapm_out_drv:
Mark Brownb75576d2009-04-20 17:56:13 +01001856 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001857 dapm_new_pga(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001858 break;
1859 case snd_soc_dapm_input:
1860 case snd_soc_dapm_output:
1861 case snd_soc_dapm_micbias:
1862 case snd_soc_dapm_spk:
1863 case snd_soc_dapm_hp:
1864 case snd_soc_dapm_mic:
1865 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001866 w->power_check = dapm_generic_check_power;
1867 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001868 case snd_soc_dapm_supply:
1869 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001870 case snd_soc_dapm_vmid:
1871 case snd_soc_dapm_pre:
1872 case snd_soc_dapm_post:
1873 break;
1874 }
Mark Brownb66a70d2011-02-09 18:04:11 +00001875
1876 /* Read the initial power state from the device */
1877 if (w->reg >= 0) {
1878 val = snd_soc_read(w->codec, w->reg);
1879 val &= 1 << w->shift;
1880 if (w->invert)
1881 val = !val;
1882
1883 if (val)
1884 w->power = 1;
1885 }
1886
Richard Purdie2b97eab2006-10-06 18:32:18 +02001887 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001888
1889 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001890 }
1891
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001892 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001893 return 0;
1894}
1895EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1896
1897/**
1898 * snd_soc_dapm_get_volsw - dapm mixer get callback
1899 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001900 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001901 *
1902 * Callback to get the value of a dapm mixer control.
1903 *
1904 * Returns 0 for success.
1905 */
1906int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1907 struct snd_ctl_elem_value *ucontrol)
1908{
Stephen Warrenfafd2172011-04-28 17:38:00 -06001909 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
1910 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01001911 struct soc_mixer_control *mc =
1912 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001913 unsigned int reg = mc->reg;
1914 unsigned int shift = mc->shift;
1915 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001916 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001917 unsigned int invert = mc->invert;
1918 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001919
Richard Purdie2b97eab2006-10-06 18:32:18 +02001920 ucontrol->value.integer.value[0] =
1921 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1922 if (shift != rshift)
1923 ucontrol->value.integer.value[1] =
1924 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1925 if (invert) {
1926 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001927 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001928 if (shift != rshift)
1929 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001930 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001931 }
1932
1933 return 0;
1934}
1935EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1936
1937/**
1938 * snd_soc_dapm_put_volsw - dapm mixer set callback
1939 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001940 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001941 *
1942 * Callback to set the value of a dapm mixer control.
1943 *
1944 * Returns 0 for success.
1945 */
1946int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1947 struct snd_ctl_elem_value *ucontrol)
1948{
Stephen Warrenfafd2172011-04-28 17:38:00 -06001949 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
1950 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
1951 struct snd_soc_codec *codec = widget->codec;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001952 struct soc_mixer_control *mc =
1953 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001954 unsigned int reg = mc->reg;
1955 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001956 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001957 unsigned int mask = (1 << fls(max)) - 1;
1958 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07001959 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00001960 int connect, change;
1961 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06001962 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001963
1964 val = (ucontrol->value.integer.value[0] & mask);
1965
1966 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001967 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07001968 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001969 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001970
Stephen Warrenfafd2172011-04-28 17:38:00 -06001971 if (val)
1972 /* new connection */
1973 connect = invert ? 0 : 1;
1974 else
1975 /* old connection must be powered down */
1976 connect = invert ? 1 : 0;
1977
1978 mutex_lock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001979
Stephen Warrene9cf7042011-01-27 14:54:05 -07001980 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00001981 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06001982 for (wi = 0; wi < wlist->num_widgets; wi++) {
1983 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00001984
Stephen Warrenfafd2172011-04-28 17:38:00 -06001985 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00001986
Stephen Warrenfafd2172011-04-28 17:38:00 -06001987 update.kcontrol = kcontrol;
1988 update.widget = widget;
1989 update.reg = reg;
1990 update.mask = mask;
1991 update.val = val;
1992 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00001993
Stephen Warrenfafd2172011-04-28 17:38:00 -06001994 dapm_mixer_update_power(widget, kcontrol, connect);
1995
1996 widget->dapm->update = NULL;
1997 }
Mark Brown283375c2009-12-07 18:09:03 +00001998 }
1999
Stephen Warrenfafd2172011-04-28 17:38:00 -06002000 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002001 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002002}
2003EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2004
2005/**
2006 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2007 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002008 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002009 *
2010 * Callback to get the value of a dapm enumerated double mixer control.
2011 *
2012 * Returns 0 for success.
2013 */
2014int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2015 struct snd_ctl_elem_value *ucontrol)
2016{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002017 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2018 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002019 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002020 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002021
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002022 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002023 ;
2024 val = snd_soc_read(widget->codec, e->reg);
2025 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2026 if (e->shift_l != e->shift_r)
2027 ucontrol->value.enumerated.item[1] =
2028 (val >> e->shift_r) & (bitmask - 1);
2029
2030 return 0;
2031}
2032EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2033
2034/**
2035 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2036 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002037 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002038 *
2039 * Callback to set the value of a dapm enumerated double mixer control.
2040 *
2041 * Returns 0 for success.
2042 */
2043int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2044 struct snd_ctl_elem_value *ucontrol)
2045{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002046 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2047 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2048 struct snd_soc_codec *codec = widget->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002049 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002050 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002051 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00002052 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002053 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002054
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002055 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002056 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002057 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002058 return -EINVAL;
2059 mux = ucontrol->value.enumerated.item[0];
2060 val = mux << e->shift_l;
2061 mask = (bitmask - 1) << e->shift_l;
2062 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002063 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002064 return -EINVAL;
2065 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2066 mask |= (bitmask - 1) << e->shift_r;
2067 }
2068
Stephen Warrenfafd2172011-04-28 17:38:00 -06002069 mutex_lock(&codec->mutex);
2070
Mark Brown3a655772009-10-05 17:23:30 +01002071 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002072 if (change) {
2073 for (wi = 0; wi < wlist->num_widgets; wi++) {
2074 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002075
Stephen Warrenfafd2172011-04-28 17:38:00 -06002076 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002077
Stephen Warrenfafd2172011-04-28 17:38:00 -06002078 update.kcontrol = kcontrol;
2079 update.widget = widget;
2080 update.reg = e->reg;
2081 update.mask = mask;
2082 update.val = val;
2083 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002084
Stephen Warrenfafd2172011-04-28 17:38:00 -06002085 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002086
Stephen Warrenfafd2172011-04-28 17:38:00 -06002087 widget->dapm->update = NULL;
2088 }
2089 }
2090
2091 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002092 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002093}
2094EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2095
2096/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002097 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2098 * @kcontrol: mixer control
2099 * @ucontrol: control element information
2100 *
2101 * Returns 0 for success.
2102 */
2103int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2104 struct snd_ctl_elem_value *ucontrol)
2105{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002106 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2107 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002108
2109 ucontrol->value.enumerated.item[0] = widget->value;
2110
2111 return 0;
2112}
2113EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2114
2115/**
2116 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2117 * @kcontrol: mixer control
2118 * @ucontrol: control element information
2119 *
2120 * Returns 0 for success.
2121 */
2122int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2123 struct snd_ctl_elem_value *ucontrol)
2124{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002125 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2126 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2127 struct snd_soc_codec *codec = widget->codec;
Mark Brownd2b247a2009-10-06 15:21:04 +01002128 struct soc_enum *e =
2129 (struct soc_enum *)kcontrol->private_value;
2130 int change;
2131 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002132 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002133
2134 if (ucontrol->value.enumerated.item[0] >= e->max)
2135 return -EINVAL;
2136
Stephen Warrenfafd2172011-04-28 17:38:00 -06002137 mutex_lock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002138
2139 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002140 if (change) {
2141 for (wi = 0; wi < wlist->num_widgets; wi++) {
2142 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002143
Stephen Warrenfafd2172011-04-28 17:38:00 -06002144 widget->value = ucontrol->value.enumerated.item[0];
2145
2146 dapm_mux_update_power(widget, kcontrol, change,
2147 widget->value, e);
2148 }
2149 }
2150
2151 mutex_unlock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002152 return ret;
2153}
2154EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2155
2156/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002157 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2158 * callback
2159 * @kcontrol: mixer control
2160 * @ucontrol: control element information
2161 *
2162 * Callback to get the value of a dapm semi enumerated double mixer control.
2163 *
2164 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2165 * used for handling bitfield coded enumeration for example.
2166 *
2167 * Returns 0 for success.
2168 */
2169int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2170 struct snd_ctl_elem_value *ucontrol)
2171{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002172 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2173 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002174 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002175 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002176
2177 reg_val = snd_soc_read(widget->codec, e->reg);
2178 val = (reg_val >> e->shift_l) & e->mask;
2179 for (mux = 0; mux < e->max; mux++) {
2180 if (val == e->values[mux])
2181 break;
2182 }
2183 ucontrol->value.enumerated.item[0] = mux;
2184 if (e->shift_l != e->shift_r) {
2185 val = (reg_val >> e->shift_r) & e->mask;
2186 for (mux = 0; mux < e->max; mux++) {
2187 if (val == e->values[mux])
2188 break;
2189 }
2190 ucontrol->value.enumerated.item[1] = mux;
2191 }
2192
2193 return 0;
2194}
2195EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2196
2197/**
2198 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2199 * callback
2200 * @kcontrol: mixer control
2201 * @ucontrol: control element information
2202 *
2203 * Callback to set the value of a dapm semi enumerated double mixer control.
2204 *
2205 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2206 * used for handling bitfield coded enumeration for example.
2207 *
2208 * Returns 0 for success.
2209 */
2210int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2211 struct snd_ctl_elem_value *ucontrol)
2212{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002213 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2214 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2215 struct snd_soc_codec *codec = widget->codec;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002216 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002217 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002218 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002219 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002220 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002221
2222 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2223 return -EINVAL;
2224 mux = ucontrol->value.enumerated.item[0];
2225 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2226 mask = e->mask << e->shift_l;
2227 if (e->shift_l != e->shift_r) {
2228 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2229 return -EINVAL;
2230 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2231 mask |= e->mask << e->shift_r;
2232 }
2233
Stephen Warrenfafd2172011-04-28 17:38:00 -06002234 mutex_lock(&codec->mutex);
2235
Mark Brown3a655772009-10-05 17:23:30 +01002236 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002237 if (change) {
2238 for (wi = 0; wi < wlist->num_widgets; wi++) {
2239 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002240
Stephen Warrenfafd2172011-04-28 17:38:00 -06002241 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002242
Stephen Warrenfafd2172011-04-28 17:38:00 -06002243 update.kcontrol = kcontrol;
2244 update.widget = widget;
2245 update.reg = e->reg;
2246 update.mask = mask;
2247 update.val = val;
2248 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002249
Stephen Warrenfafd2172011-04-28 17:38:00 -06002250 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002251
Stephen Warrenfafd2172011-04-28 17:38:00 -06002252 widget->dapm->update = NULL;
2253 }
2254 }
2255
2256 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002257 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002258}
2259EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2260
2261/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002262 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2263 *
2264 * @kcontrol: mixer control
2265 * @uinfo: control element information
2266 *
2267 * Callback to provide information about a pin switch control.
2268 */
2269int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2270 struct snd_ctl_elem_info *uinfo)
2271{
2272 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2273 uinfo->count = 1;
2274 uinfo->value.integer.min = 0;
2275 uinfo->value.integer.max = 1;
2276
2277 return 0;
2278}
2279EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2280
2281/**
2282 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2283 *
2284 * @kcontrol: mixer control
2285 * @ucontrol: Value
2286 */
2287int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2288 struct snd_ctl_elem_value *ucontrol)
2289{
2290 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2291 const char *pin = (const char *)kcontrol->private_value;
2292
2293 mutex_lock(&codec->mutex);
2294
2295 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002296 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002297
2298 mutex_unlock(&codec->mutex);
2299
2300 return 0;
2301}
2302EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2303
2304/**
2305 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2306 *
2307 * @kcontrol: mixer control
2308 * @ucontrol: Value
2309 */
2310int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2311 struct snd_ctl_elem_value *ucontrol)
2312{
2313 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2314 const char *pin = (const char *)kcontrol->private_value;
2315
2316 mutex_lock(&codec->mutex);
2317
2318 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002319 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002320 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002321 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002322
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002323 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002324
2325 mutex_unlock(&codec->mutex);
2326
2327 return 0;
2328}
2329EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2330
2331/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002332 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002333 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002334 * @widget: widget template
2335 *
2336 * Creates a new dapm control based upon the template.
2337 *
2338 * Returns 0 for success else error.
2339 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002340int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002341 const struct snd_soc_dapm_widget *widget)
2342{
2343 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002344 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002345
2346 if ((w = dapm_cnew_widget(widget)) == NULL)
2347 return -ENOMEM;
2348
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002349 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002350 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002351 name_len += 1 + strlen(dapm->codec->name_prefix);
2352 w->name = kmalloc(name_len, GFP_KERNEL);
2353 if (w->name == NULL) {
2354 kfree(w);
2355 return -ENOMEM;
2356 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002357 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002358 snprintf(w->name, name_len, "%s %s",
2359 dapm->codec->name_prefix, widget->name);
2360 else
2361 snprintf(w->name, name_len, "%s", widget->name);
2362
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002363 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002364 w->dapm = dapm;
2365 w->codec = dapm->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002366 INIT_LIST_HEAD(&w->sources);
2367 INIT_LIST_HEAD(&w->sinks);
2368 INIT_LIST_HEAD(&w->list);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002369 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002370
2371 /* machine layer set ups unconnected pins and insertions */
2372 w->connected = 1;
2373 return 0;
2374}
2375EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2376
2377/**
Mark Brown4ba13272008-05-13 14:51:19 +02002378 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002379 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002380 * @widget: widget array
2381 * @num: number of widgets
2382 *
2383 * Creates new DAPM controls based upon the templates.
2384 *
2385 * Returns 0 for success else error.
2386 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002387int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002388 const struct snd_soc_dapm_widget *widget,
2389 int num)
2390{
2391 int i, ret;
2392
2393 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002394 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002395 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002396 dev_err(dapm->dev,
2397 "ASoC: Failed to create DAPM control %s: %d\n",
2398 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002399 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002400 }
Mark Brown4ba13272008-05-13 14:51:19 +02002401 widget++;
2402 }
2403 return 0;
2404}
2405EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2406
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002407static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002408 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002409{
2410 struct snd_soc_dapm_widget *w;
2411
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002412 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002413 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002414 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002415 continue;
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002416 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2417 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002418 if (strstr(w->sname, stream)) {
2419 switch(event) {
2420 case SND_SOC_DAPM_STREAM_START:
2421 w->active = 1;
2422 break;
2423 case SND_SOC_DAPM_STREAM_STOP:
2424 w->active = 0;
2425 break;
2426 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002427 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002428 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002429 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2430 break;
2431 }
2432 }
2433 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002434
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002435 dapm_power_widgets(dapm, event);
2436}
2437
2438/**
2439 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2440 * @rtd: PCM runtime data
2441 * @stream: stream name
2442 * @event: stream event
2443 *
2444 * Sends a stream event to the dapm core. The core then makes any
2445 * necessary widget power changes.
2446 *
2447 * Returns 0 for success else error.
2448 */
2449int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2450 const char *stream, int event)
2451{
2452 struct snd_soc_codec *codec = rtd->codec;
2453
2454 if (stream == NULL)
2455 return 0;
2456
2457 mutex_lock(&codec->mutex);
2458 soc_dapm_stream_event(&codec->dapm, stream, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002459 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002460 return 0;
2461}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002462
2463/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002464 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002465 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002466 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002467 *
Mark Brown74b8f952009-06-06 11:26:15 +01002468 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002469 * a valid audio route and active audio stream.
2470 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2471 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002472 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002473int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002474{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002475 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002476}
Liam Girdwooda5302182008-07-07 13:35:17 +01002477EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002478
2479/**
Mark Brownda341832010-03-15 19:23:37 +00002480 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002481 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002482 * @pin: pin name
2483 *
2484 * Enables input/output pin regardless of any other state. This is
2485 * intended for use with microphone bias supplies used in microphone
2486 * jack detection.
2487 *
2488 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2489 * do any widget power switching.
2490 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002491int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2492 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002493{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002494 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00002495
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002496 if (!w) {
2497 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2498 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00002499 }
2500
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002501 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2502 w->connected = 1;
2503 w->force = 1;
Mark Brown0d867332011-04-06 11:38:14 +09002504
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002505 return 0;
Mark Brownda341832010-03-15 19:23:37 +00002506}
2507EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2508
2509/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002510 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002511 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002512 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002513 *
Mark Brown74b8f952009-06-06 11:26:15 +01002514 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002515 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2516 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002517 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002518int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2519 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002520{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002521 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002522}
2523EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2524
2525/**
Mark Brown5817b522008-09-24 11:23:11 +01002526 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002527 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002528 * @pin: pin name
2529 *
2530 * Marks the specified pin as being not connected, disabling it along
2531 * any parent or child widgets. At present this is identical to
2532 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2533 * additional things such as disabling controls which only affect
2534 * paths through the pin.
2535 *
2536 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2537 * do any widget power switching.
2538 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002539int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002540{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002541 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002542}
2543EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2544
2545/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002546 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002547 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002548 * @pin: audio signal pin endpoint (or start point)
2549 *
2550 * Get audio pin status - connected or disconnected.
2551 *
2552 * Returns 1 for connected otherwise 0.
2553 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002554int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2555 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002556{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002557 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002558
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002559 if (w)
2560 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06002561
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002562 return 0;
2563}
Liam Girdwooda5302182008-07-07 13:35:17 +01002564EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002565
2566/**
Mark Brown1547aba2010-05-07 21:11:40 +01002567 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002568 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01002569 * @pin: audio signal pin endpoint (or start point)
2570 *
2571 * Mark the given endpoint or pin as ignoring suspend. When the
2572 * system is disabled a path between two endpoints flagged as ignoring
2573 * suspend will not be disabled. The path must already be enabled via
2574 * normal means at suspend time, it will not be turned on if it was not
2575 * already enabled.
2576 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002577int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2578 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01002579{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002580 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01002581
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002582 if (!w) {
2583 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2584 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01002585 }
2586
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002587 w->ignore_suspend = 1;
2588
2589 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01002590}
2591EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2592
2593/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002594 * snd_soc_dapm_free - free dapm resources
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002595 * @card: SoC device
Richard Purdie2b97eab2006-10-06 18:32:18 +02002596 *
2597 * Free all dapm widgets and resources.
2598 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002599void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002600{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002601 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002602 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002603 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02002604 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002605}
2606EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2607
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002608static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01002609{
Mark Brown51737472009-06-22 13:16:51 +01002610 struct snd_soc_dapm_widget *w;
2611 LIST_HEAD(down_list);
2612 int powerdown = 0;
2613
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002614 list_for_each_entry(w, &dapm->card->widgets, list) {
2615 if (w->dapm != dapm)
2616 continue;
Mark Brown51737472009-06-22 13:16:51 +01002617 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00002618 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002619 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002620 powerdown = 1;
2621 }
2622 }
2623
2624 /* If there were no widgets to power down we're already in
2625 * standby.
2626 */
2627 if (powerdown) {
Mark Browned5a4c42011-02-18 11:12:42 -08002628 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00002629 dapm_seq_run(dapm, &down_list, 0, false);
Mark Browned5a4c42011-02-18 11:12:42 -08002630 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01002631 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002632}
Mark Brown51737472009-06-22 13:16:51 +01002633
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002634/*
2635 * snd_soc_dapm_shutdown - callback for system shutdown
2636 */
2637void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2638{
2639 struct snd_soc_codec *codec;
2640
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002641 list_for_each_entry(codec, &card->codec_dev_list, list) {
2642 soc_dapm_shutdown_codec(&codec->dapm);
Mark Browned5a4c42011-02-18 11:12:42 -08002643 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002644 }
Mark Brown51737472009-06-22 13:16:51 +01002645}
2646
Richard Purdie2b97eab2006-10-06 18:32:18 +02002647/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002648MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002649MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2650MODULE_LICENSE("GPL");