blob: 34106bca63cfb854161ded34f139ddbca43ec6d1 [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 Brownd4c60052011-06-06 19:13:23 +0100145 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100146 if (ret != 0)
147 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100148
Mark Browncc4c6702011-06-06 19:03:34 +0100149 if (dapm->codec) {
150 if (dapm->codec->driver->set_bias_level)
151 ret = dapm->codec->driver->set_bias_level(dapm->codec,
152 level);
153 else
154 dapm->bias_level = level;
155 }
Mark Brown171ec6b2011-06-06 18:15:19 +0100156 if (ret != 0)
157 goto out;
158
159 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100160 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100161out:
Mark Brown84e90932010-11-04 00:07:02 -0400162 trace_snd_soc_bias_level_done(card, level);
163
Mark Brown452c5ea2009-05-17 21:41:23 +0100164 return ret;
165}
166
Richard Purdie2b97eab2006-10-06 18:32:18 +0200167/* set up initial codec paths */
168static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
169 struct snd_soc_dapm_path *p, int i)
170{
171 switch (w->id) {
172 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000173 case snd_soc_dapm_mixer:
174 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200175 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100176 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600177 w->kcontrol_news[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400178 unsigned int reg = mc->reg;
179 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100180 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400181 unsigned int mask = (1 << fls(max)) - 1;
182 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200183
184 val = snd_soc_read(w->codec, reg);
185 val = (val >> shift) & mask;
186
187 if ((invert && !val) || (!invert && val))
188 p->connect = 1;
189 else
190 p->connect = 0;
191 }
192 break;
193 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600194 struct soc_enum *e = (struct soc_enum *)
195 w->kcontrol_news[i].private_value;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200196 int val, item, bitmask;
197
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100198 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Mark Brown88d96082011-06-06 16:16:34 +0100199 ;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200200 val = snd_soc_read(w->codec, e->reg);
201 item = (val >> e->shift_l) & (bitmask - 1);
202
203 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100204 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200205 if (!(strcmp(p->name, e->texts[i])) && item == i)
206 p->connect = 1;
207 }
208 }
209 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000210 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600211 struct soc_enum *e = (struct soc_enum *)
212 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000213
214 p->connect = 0;
215 /* since a virtual mux has no backing registers to
216 * decide which path to connect, it will try to match
217 * with the first enumeration. This is to ensure
218 * that the default mux choice (the first) will be
219 * correctly powered up during initialization.
220 */
221 if (!strcmp(p->name, e->texts[0]))
222 p->connect = 1;
223 }
224 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200225 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200226 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600227 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200228 int val, item;
229
230 val = snd_soc_read(w->codec, e->reg);
231 val = (val >> e->shift_l) & e->mask;
232 for (item = 0; item < e->max; item++) {
233 if (val == e->values[item])
234 break;
235 }
236
237 p->connect = 0;
238 for (i = 0; i < e->max; i++) {
239 if (!(strcmp(p->name, e->texts[i])) && item == i)
240 p->connect = 1;
241 }
242 }
243 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200244 /* does not effect routing - always connected */
245 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600246 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200247 case snd_soc_dapm_output:
248 case snd_soc_dapm_adc:
249 case snd_soc_dapm_input:
250 case snd_soc_dapm_dac:
251 case snd_soc_dapm_micbias:
252 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100253 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100254 case snd_soc_dapm_aif_in:
255 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200256 p->connect = 1;
257 break;
258 /* does effect routing - dynamically connected */
259 case snd_soc_dapm_hp:
260 case snd_soc_dapm_mic:
261 case snd_soc_dapm_spk:
262 case snd_soc_dapm_line:
263 case snd_soc_dapm_pre:
264 case snd_soc_dapm_post:
265 p->connect = 0;
266 break;
267 }
268}
269
Mark Brown74b8f952009-06-06 11:26:15 +0100270/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200271static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200272 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
273 struct snd_soc_dapm_path *path, const char *control_name,
274 const struct snd_kcontrol_new *kcontrol)
275{
276 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
277 int i;
278
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100279 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200280 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200281 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200282 list_add(&path->list_sink, &dest->sources);
283 list_add(&path->list_source, &src->sinks);
284 path->name = (char*)e->texts[i];
285 dapm_set_path_status(dest, path, 0);
286 return 0;
287 }
288 }
289
290 return -ENODEV;
291}
292
Mark Brown74b8f952009-06-06 11:26:15 +0100293/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200294static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200295 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
296 struct snd_soc_dapm_path *path, const char *control_name)
297{
298 int i;
299
300 /* search for mixer kcontrol */
301 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600302 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200303 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200304 list_add(&path->list_sink, &dest->sources);
305 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600306 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200307 dapm_set_path_status(dest, path, i);
308 return 0;
309 }
310 }
311 return -ENODEV;
312}
313
Stephen Warrenaf468002011-04-28 17:38:01 -0600314static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600315 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600316 const struct snd_kcontrol_new *kcontrol_new,
317 struct snd_kcontrol **kcontrol)
318{
319 struct snd_soc_dapm_widget *w;
320 int i;
321
322 *kcontrol = NULL;
323
324 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600325 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
326 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600327 for (i = 0; i < w->num_kcontrols; i++) {
328 if (&w->kcontrol_news[i] == kcontrol_new) {
329 if (w->kcontrols)
330 *kcontrol = w->kcontrols[i];
331 return 1;
332 }
333 }
334 }
335
336 return 0;
337}
338
Richard Purdie2b97eab2006-10-06 18:32:18 +0200339/* create new dapm mixer control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200340static int dapm_new_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200341 struct snd_soc_dapm_widget *w)
342{
343 int i, ret = 0;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000344 size_t name_len, prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200345 struct snd_soc_dapm_path *path;
Mark Brown12ea2c72011-03-02 18:17:32 +0000346 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000347 const char *prefix;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600348 struct snd_soc_dapm_widget_list *wlist;
349 size_t wlistsize;
Mark Brownefb7ac32011-03-08 17:23:24 +0000350
351 if (dapm->codec)
352 prefix = dapm->codec->name_prefix;
353 else
354 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200355
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000356 if (prefix)
357 prefix_len = strlen(prefix) + 1;
358 else
359 prefix_len = 0;
360
Richard Purdie2b97eab2006-10-06 18:32:18 +0200361 /* add kcontrol */
362 for (i = 0; i < w->num_kcontrols; i++) {
363
364 /* match name */
365 list_for_each_entry(path, &w->sources, list_sink) {
366
367 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600368 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200369 continue;
370
Stephen Warrenfafd2172011-04-28 17:38:00 -0600371 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
372 sizeof(struct snd_soc_dapm_widget *),
373 wlist = kzalloc(wlistsize, GFP_KERNEL);
374 if (wlist == NULL) {
375 dev_err(dapm->dev,
376 "asoc: can't allocate widget list for %s\n",
377 w->name);
378 return -ENOMEM;
379 }
380 wlist->num_widgets = 1;
381 wlist->widgets[0] = w;
382
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000383 /* add dapm control with long name.
384 * for dapm_mixer this is the concatenation of the
385 * mixer and kcontrol name.
386 * for dapm_mixer_named_ctl this is simply the
387 * kcontrol name.
388 */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600389 name_len = strlen(w->kcontrol_news[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000390 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000391 name_len += 1 + strlen(w->name);
392
Mark Brown219b93f2008-10-28 13:02:31 +0000393 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000394
Stephen Warrenfafd2172011-04-28 17:38:00 -0600395 if (path->long_name == NULL) {
396 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200397 return -ENOMEM;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600398 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200399
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000400 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000401 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000402 /* The control will get a prefix from
403 * the control creation process but
404 * we're also using the same prefix
405 * for widgets so cut the prefix off
406 * the front of the widget name.
407 */
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000408 snprintf(path->long_name, name_len, "%s %s",
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000409 w->name + prefix_len,
Stephen Warren82cfecd2011-04-28 17:37:58 -0600410 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000411 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000412 case snd_soc_dapm_mixer_named_ctl:
413 snprintf(path->long_name, name_len, "%s",
Stephen Warren82cfecd2011-04-28 17:37:58 -0600414 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000415 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000416 }
417
Mark Brown219b93f2008-10-28 13:02:31 +0000418 path->long_name[name_len - 1] = '\0';
419
Stephen Warrenfafd2172011-04-28 17:38:00 -0600420 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
421 wlist, path->long_name,
422 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200423 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200424 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200425 dev_err(dapm->dev,
426 "asoc: failed to add dapm kcontrol %s: %d\n",
427 path->long_name, ret);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600428 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200429 kfree(path->long_name);
430 path->long_name = NULL;
431 return ret;
432 }
Stephen Warrenfad59882011-04-28 17:37:59 -0600433 w->kcontrols[i] = path->kcontrol;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200434 }
435 }
436 return ret;
437}
438
439/* create new dapm mux control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200440static int dapm_new_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200441 struct snd_soc_dapm_widget *w)
442{
443 struct snd_soc_dapm_path *path = NULL;
444 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000445 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000446 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000447 size_t prefix_len;
Stephen Warrenaf468002011-04-28 17:38:01 -0600448 int ret;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600449 struct snd_soc_dapm_widget_list *wlist;
Stephen Warrenaf468002011-04-28 17:38:01 -0600450 int shared, wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600451 size_t wlistsize;
Stephen Warrenaf468002011-04-28 17:38:01 -0600452 char *name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200453
Stephen Warrenaf468002011-04-28 17:38:01 -0600454 if (w->num_kcontrols != 1) {
455 dev_err(dapm->dev,
456 "asoc: mux %s has incorrect number of controls\n",
457 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200458 return -EINVAL;
459 }
460
Stephen Warren1007da02011-05-26 09:57:33 -0600461 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
Stephen Warrenaf468002011-04-28 17:38:01 -0600462 &kcontrol);
463 if (kcontrol) {
464 wlist = kcontrol->private_data;
465 wlistentries = wlist->num_widgets + 1;
466 } else {
467 wlist = NULL;
468 wlistentries = 1;
469 }
Stephen Warrenfafd2172011-04-28 17:38:00 -0600470 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
Stephen Warrenaf468002011-04-28 17:38:01 -0600471 wlistentries * sizeof(struct snd_soc_dapm_widget *),
472 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600473 if (wlist == NULL) {
474 dev_err(dapm->dev,
475 "asoc: can't allocate widget list for %s\n", w->name);
476 return -ENOMEM;
477 }
Stephen Warrenaf468002011-04-28 17:38:01 -0600478 wlist->num_widgets = wlistentries;
479 wlist->widgets[wlistentries - 1] = w;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600480
Stephen Warrenaf468002011-04-28 17:38:01 -0600481 if (!kcontrol) {
482 if (dapm->codec)
483 prefix = dapm->codec->name_prefix;
484 else
485 prefix = NULL;
Mark Brownefb7ac32011-03-08 17:23:24 +0000486
Stephen Warrenaf468002011-04-28 17:38:01 -0600487 if (shared) {
488 name = w->kcontrol_news[0].name;
489 prefix_len = 0;
490 } else {
491 name = w->name;
492 if (prefix)
493 prefix_len = strlen(prefix) + 1;
494 else
495 prefix_len = 0;
496 }
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000497
Stephen Warrenaf468002011-04-28 17:38:01 -0600498 /*
499 * The control will get a prefix from the control creation
500 * process but we're also using the same prefix for widgets so
501 * cut the prefix off the front of the widget name.
502 */
503 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
504 name + prefix_len, prefix);
505 ret = snd_ctl_add(card, kcontrol);
506 if (ret < 0) {
507 dev_err(dapm->dev,
508 "asoc: failed to add kcontrol %s\n", w->name);
509 kfree(wlist);
510 return ret;
511 }
512 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200513
Stephen Warrenaf468002011-04-28 17:38:01 -0600514 kcontrol->private_data = wlist;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200515
Stephen Warrenfad59882011-04-28 17:37:59 -0600516 w->kcontrols[0] = kcontrol;
517
Richard Purdie2b97eab2006-10-06 18:32:18 +0200518 list_for_each_entry(path, &w->sources, list_sink)
519 path->kcontrol = kcontrol;
520
Stephen Warrenaf468002011-04-28 17:38:01 -0600521 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200522}
523
524/* create new dapm volume control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200525static int dapm_new_pga(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200526 struct snd_soc_dapm_widget *w)
527{
Mark Browna6c65732010-03-03 17:45:21 +0000528 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200529 dev_err(w->dapm->dev,
530 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200531
Mark Browna6c65732010-03-03 17:45:21 +0000532 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200533}
534
535/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200536static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200537{
538 struct snd_soc_dapm_path *p;
539
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200540 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200541 p->walked = 0;
542}
543
Mark Brown99497882010-05-07 20:24:05 +0100544/* We implement power down on suspend by checking the power state of
545 * the ALSA card - when we are suspending the ALSA state for the card
546 * is set to D3.
547 */
548static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
549{
Mark Brown12ea2c72011-03-02 18:17:32 +0000550 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100551
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000552 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100553 case SNDRV_CTL_POWER_D3hot:
554 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100555 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200556 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
557 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100558 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100559 default:
560 return 1;
561 }
562}
563
Richard Purdie2b97eab2006-10-06 18:32:18 +0200564/*
565 * Recursively check for a completed path to an active or physically connected
566 * output widget. Returns number of complete paths.
567 */
568static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
569{
570 struct snd_soc_dapm_path *path;
571 int con = 0;
572
Mark Brown246d0a12009-04-22 18:24:55 +0100573 if (widget->id == snd_soc_dapm_supply)
574 return 0;
575
Mark Brown010ff262009-08-17 17:39:22 +0100576 switch (widget->id) {
577 case snd_soc_dapm_adc:
578 case snd_soc_dapm_aif_out:
579 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100580 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100581 default:
582 break;
583 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200584
585 if (widget->connected) {
586 /* connected pin ? */
587 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100588 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200589
590 /* connected jack or spk ? */
591 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300592 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown99497882010-05-07 20:24:05 +0100593 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200594 }
595
596 list_for_each_entry(path, &widget->sinks, list_source) {
597 if (path->walked)
598 continue;
599
600 if (path->sink && path->connect) {
601 path->walked = 1;
602 con += is_connected_output_ep(path->sink);
603 }
604 }
605
606 return con;
607}
608
609/*
610 * Recursively check for a completed path to an active or physically connected
611 * input widget. Returns number of complete paths.
612 */
613static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
614{
615 struct snd_soc_dapm_path *path;
616 int con = 0;
617
Mark Brown246d0a12009-04-22 18:24:55 +0100618 if (widget->id == snd_soc_dapm_supply)
619 return 0;
620
Richard Purdie2b97eab2006-10-06 18:32:18 +0200621 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100622 switch (widget->id) {
623 case snd_soc_dapm_dac:
624 case snd_soc_dapm_aif_in:
625 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100626 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100627 default:
628 break;
629 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200630
631 if (widget->connected) {
632 /* connected pin ? */
633 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100634 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200635
636 /* connected VMID/Bias for lower pops */
637 if (widget->id == snd_soc_dapm_vmid)
Mark Brown99497882010-05-07 20:24:05 +0100638 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200639
640 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300641 if (widget->id == snd_soc_dapm_mic ||
642 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown99497882010-05-07 20:24:05 +0100643 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200644 }
645
646 list_for_each_entry(path, &widget->sources, list_sink) {
647 if (path->walked)
648 continue;
649
650 if (path->source && path->connect) {
651 path->walked = 1;
652 con += is_connected_input_ep(path->source);
653 }
654 }
655
656 return con;
657}
658
659/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300660 * Handler for generic register modifier widget.
661 */
662int dapm_reg_event(struct snd_soc_dapm_widget *w,
663 struct snd_kcontrol *kcontrol, int event)
664{
665 unsigned int val;
666
667 if (SND_SOC_DAPM_EVENT_ON(event))
668 val = w->on_val;
669 else
670 val = w->off_val;
671
672 snd_soc_update_bits(w->codec, -(w->reg + 1),
673 w->mask << w->shift, val << w->shift);
674
675 return 0;
676}
Mark Brown11589412008-07-29 11:42:23 +0100677EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300678
Mark Browncd0f2d42009-04-20 16:56:59 +0100679/* Generic check to see if a widget should be powered.
680 */
681static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
682{
683 int in, out;
684
685 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200686 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100687 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200688 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100689 return out != 0 && in != 0;
690}
691
Mark Brown6ea31b92009-04-20 17:15:41 +0100692/* Check to see if an ADC has power */
693static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
694{
695 int in;
696
697 if (w->active) {
698 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200699 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100700 return in != 0;
701 } else {
702 return dapm_generic_check_power(w);
703 }
704}
705
706/* Check to see if a DAC has power */
707static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
708{
709 int out;
710
711 if (w->active) {
712 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200713 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100714 return out != 0;
715 } else {
716 return dapm_generic_check_power(w);
717 }
718}
719
Mark Brown246d0a12009-04-22 18:24:55 +0100720/* Check to see if a power supply is needed */
721static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
722{
723 struct snd_soc_dapm_path *path;
724 int power = 0;
725
726 /* Check if one of our outputs is connected */
727 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +0100728 if (path->connected &&
729 !path->connected(path->source, path->sink))
730 continue;
731
Mark Brown30173582011-02-11 11:42:19 +0000732 if (!path->sink)
733 continue;
734
735 if (path->sink->force) {
736 power = 1;
737 break;
738 }
739
740 if (path->sink->power_check &&
Mark Brown246d0a12009-04-22 18:24:55 +0100741 path->sink->power_check(path->sink)) {
742 power = 1;
743 break;
744 }
745 }
746
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200747 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100748
749 return power;
750}
751
Mark Brown38357ab2009-06-06 19:03:23 +0100752static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
753 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +0000754 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +0000755{
Mark Brown828a8422011-01-15 13:14:30 +0000756 int *sort;
757
758 if (power_up)
759 sort = dapm_up_seq;
760 else
761 sort = dapm_down_seq;
762
Mark Brown38357ab2009-06-06 19:03:23 +0100763 if (sort[a->id] != sort[b->id])
764 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +0000765 if (a->subseq != b->subseq) {
766 if (power_up)
767 return a->subseq - b->subseq;
768 else
769 return b->subseq - a->subseq;
770 }
Mark Brownb22ead22009-06-07 12:51:26 +0100771 if (a->reg != b->reg)
772 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +0000773 if (a->dapm != b->dapm)
774 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +0000775
Mark Brown38357ab2009-06-06 19:03:23 +0100776 return 0;
777}
Mark Brown42aa3412009-03-01 19:21:10 +0000778
Mark Brown38357ab2009-06-06 19:03:23 +0100779/* Insert a widget in order into a DAPM power sequence. */
780static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
781 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +0000782 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +0100783{
784 struct snd_soc_dapm_widget *w;
785
786 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +0000787 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +0100788 list_add_tail(&new_widget->power_list, &w->power_list);
789 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000790 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100791
Mark Brown38357ab2009-06-06 19:03:23 +0100792 list_add_tail(&new_widget->power_list, list);
793}
Mark Brown42aa3412009-03-01 19:21:10 +0000794
Mark Brown68f89ad2010-11-03 23:51:49 -0400795static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
796 struct snd_soc_dapm_widget *w, int event)
797{
798 struct snd_soc_card *card = dapm->card;
799 const char *ev_name;
800 int power, ret;
801
802 switch (event) {
803 case SND_SOC_DAPM_PRE_PMU:
804 ev_name = "PRE_PMU";
805 power = 1;
806 break;
807 case SND_SOC_DAPM_POST_PMU:
808 ev_name = "POST_PMU";
809 power = 1;
810 break;
811 case SND_SOC_DAPM_PRE_PMD:
812 ev_name = "PRE_PMD";
813 power = 0;
814 break;
815 case SND_SOC_DAPM_POST_PMD:
816 ev_name = "POST_PMD";
817 power = 0;
818 break;
819 default:
820 BUG();
821 return;
822 }
823
824 if (w->power != power)
825 return;
826
827 if (w->event && (w->event_flags & event)) {
828 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
829 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -0400830 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400831 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -0400832 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400833 if (ret < 0)
834 pr_err("%s: %s event failed: %d\n",
835 ev_name, w->name, ret);
836 }
837}
838
Mark Brownb22ead22009-06-07 12:51:26 +0100839/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200840static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +0100841 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100842{
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200843 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -0400844 struct snd_soc_dapm_widget *w;
845 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +0100846 unsigned int value = 0;
847 unsigned int mask = 0;
848 unsigned int cur_mask;
849
850 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
851 power_list)->reg;
852
853 list_for_each_entry(w, pending, power_list) {
854 cur_mask = 1 << w->shift;
855 BUG_ON(reg != w->reg);
856
857 if (w->invert)
858 power = !w->power;
859 else
860 power = w->power;
861
862 mask |= cur_mask;
863 if (power)
864 value |= cur_mask;
865
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200866 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +0100867 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
868 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100869
Mark Brown68f89ad2010-11-03 23:51:49 -0400870 /* Check for events */
871 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
872 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100873 }
874
Mark Brown81628102009-06-07 13:21:24 +0100875 if (reg >= 0) {
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200876 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +0100877 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200878 value, mask, reg, card->pop_time);
879 pop_wait(card->pop_time);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200880 snd_soc_update_bits(dapm->codec, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +0100881 }
882
883 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -0400884 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
885 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +0000886 }
Mark Brown42aa3412009-03-01 19:21:10 +0000887}
888
Mark Brownb22ead22009-06-07 12:51:26 +0100889/* Apply a DAPM power sequence.
890 *
891 * We walk over a pre-sorted list of widgets to apply power to. In
892 * order to minimise the number of writes to the device required
893 * multiple widgets will be updated in a single write where possible.
894 * Currently anything that requires more than a single write is not
895 * handled.
896 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200897static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +0000898 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +0100899{
900 struct snd_soc_dapm_widget *w, *n;
901 LIST_HEAD(pending);
902 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +0000903 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +0100904 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200905 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +0000906 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +0000907 int *sort;
908
909 if (power_up)
910 sort = dapm_up_seq;
911 else
912 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +0100913
Mark Brownb22ead22009-06-07 12:51:26 +0100914 list_for_each_entry_safe(w, n, list, power_list) {
915 ret = 0;
916
917 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200918 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +0000919 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +0100920 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200921 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +0100922
Mark Brown474b62d2011-01-18 16:14:44 +0000923 if (cur_dapm && cur_dapm->seq_notifier) {
924 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
925 if (sort[i] == cur_sort)
926 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +0000927 i,
928 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +0000929 }
930
Mark Brownb22ead22009-06-07 12:51:26 +0100931 INIT_LIST_HEAD(&pending);
932 cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +0000933 cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +0100934 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200935 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +0100936 }
937
Mark Brown163cac02009-06-07 10:12:52 +0100938 switch (w->id) {
939 case snd_soc_dapm_pre:
940 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100941 list_for_each_entry_safe_continue(w, n, list,
942 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100943
Mark Brownb22ead22009-06-07 12:51:26 +0100944 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100945 ret = w->event(w,
946 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100947 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100948 ret = w->event(w,
949 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +0100950 break;
951
952 case snd_soc_dapm_post:
953 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100954 list_for_each_entry_safe_continue(w, n, list,
955 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100956
Mark Brownb22ead22009-06-07 12:51:26 +0100957 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100958 ret = w->event(w,
959 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100960 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100961 ret = w->event(w,
962 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100963 break;
964
Mark Brown163cac02009-06-07 10:12:52 +0100965 default:
Mark Brown81628102009-06-07 13:21:24 +0100966 /* Queue it up for application */
967 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +0000968 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +0100969 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200970 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +0100971 list_move(&w->power_list, &pending);
972 break;
Mark Brown163cac02009-06-07 10:12:52 +0100973 }
Mark Brownb22ead22009-06-07 12:51:26 +0100974
975 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200976 dev_err(w->dapm->dev,
977 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +0100978 }
Mark Brownb22ead22009-06-07 12:51:26 +0100979
980 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +0000981 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +0000982
983 if (cur_dapm && cur_dapm->seq_notifier) {
984 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
985 if (sort[i] == cur_sort)
986 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +0000987 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +0000988 }
Mark Brown163cac02009-06-07 10:12:52 +0100989}
990
Mark Brown97404f22010-12-14 16:13:57 +0000991static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
992{
993 struct snd_soc_dapm_update *update = dapm->update;
994 struct snd_soc_dapm_widget *w;
995 int ret;
996
997 if (!update)
998 return;
999
1000 w = update->widget;
1001
1002 if (w->event &&
1003 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1004 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1005 if (ret != 0)
1006 pr_err("%s DAPM pre-event failed: %d\n",
1007 w->name, ret);
1008 }
1009
1010 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1011 update->val);
1012 if (ret < 0)
1013 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1014
1015 if (w->event &&
1016 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1017 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1018 if (ret != 0)
1019 pr_err("%s DAPM post-event failed: %d\n",
1020 w->name, ret);
1021 }
1022}
1023
Mark Brown9d0624a2011-02-18 11:49:43 -08001024/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1025 * they're changing state.
1026 */
1027static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1028{
1029 struct snd_soc_dapm_context *d = data;
1030 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001031
Mark Brown56fba412011-06-04 11:25:10 +01001032 /* If we're off and we're not supposed to be go into STANDBY */
1033 if (d->bias_level == SND_SOC_BIAS_OFF &&
1034 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001035 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1036 if (ret != 0)
1037 dev_err(d->dev,
1038 "Failed to turn on bias: %d\n", ret);
1039 }
1040
Mark Brown56fba412011-06-04 11:25:10 +01001041 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1042 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001043 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1044 if (ret != 0)
1045 dev_err(d->dev,
1046 "Failed to prepare bias: %d\n", ret);
1047 }
1048}
1049
1050/* Async callback run prior to DAPM sequences - brings to their final
1051 * state.
1052 */
1053static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1054{
1055 struct snd_soc_dapm_context *d = data;
1056 int ret;
1057
1058 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001059 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1060 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1061 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001062 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1063 if (ret != 0)
1064 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1065 ret);
1066 }
1067
1068 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001069 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1070 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001071 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1072 if (ret != 0)
1073 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1074 }
1075
1076 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001077 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1078 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001079 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1080 if (ret != 0)
1081 dev_err(d->dev, "Failed to apply active bias: %d\n",
1082 ret);
1083 }
1084}
Mark Brown97404f22010-12-14 16:13:57 +00001085
Mark Brown42aa3412009-03-01 19:21:10 +00001086/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001087 * Scan each dapm widget for complete audio path.
1088 * A complete path is a route that has valid endpoints i.e.:-
1089 *
1090 * o DAC to output pin.
1091 * o Input Pin to ADC.
1092 * o Input pin to Output pin (bypass, sidetone)
1093 * o DAC to ADC (loopback).
1094 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001095static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001096{
Mark Brown12ea2c72011-03-02 18:17:32 +00001097 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001098 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001099 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001100 LIST_HEAD(up_list);
1101 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001102 LIST_HEAD(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001103 enum snd_soc_bias_level bias;
Mark Brown38357ab2009-06-06 19:03:23 +01001104 int power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001105
Mark Brown84e90932010-11-04 00:07:02 -04001106 trace_snd_soc_dapm_start(card);
1107
Mark Brown56fba412011-06-04 11:25:10 +01001108 list_for_each_entry(d, &card->dapm_list, list) {
1109 if (d->n_widgets || d->codec == NULL) {
1110 if (d->idle_bias_off)
1111 d->target_bias_level = SND_SOC_BIAS_OFF;
1112 else
1113 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1114 }
1115 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001116
Mark Brown6d3ddc82009-05-16 17:47:29 +01001117 /* Check which widgets we need to power and store them in
1118 * lists indicating if they should be powered up or down.
1119 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001120 list_for_each_entry(w, &card->widgets, list) {
Mark Brown6d3ddc82009-05-16 17:47:29 +01001121 switch (w->id) {
1122 case snd_soc_dapm_pre:
Mark Brown828a8422011-01-15 13:14:30 +00001123 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001124 break;
1125 case snd_soc_dapm_post:
Mark Brown828a8422011-01-15 13:14:30 +00001126 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001127 break;
1128
1129 default:
1130 if (!w->power_check)
1131 continue;
1132
Mark Brown99497882010-05-07 20:24:05 +01001133 if (!w->force)
Mark Brown50b6bce2009-11-23 13:11:53 +00001134 power = w->power_check(w);
Mark Brown99497882010-05-07 20:24:05 +01001135 else
1136 power = 1;
Mark Browndfcc9042011-06-04 11:34:43 +01001137
1138 if (power) {
1139 d = w->dapm;
1140
1141 /* Supplies and micbiases only bring
1142 * the context up to STANDBY as unless
1143 * something else is active and
1144 * passing audio they generally don't
1145 * require full power.
1146 */
1147 switch (w->id) {
1148 case snd_soc_dapm_supply:
1149 case snd_soc_dapm_micbias:
1150 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1151 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1152 break;
1153 default:
1154 d->target_bias_level = SND_SOC_BIAS_ON;
1155 break;
1156 }
1157 }
Mark Brown452c5ea2009-05-17 21:41:23 +01001158
Mark Brown6d3ddc82009-05-16 17:47:29 +01001159 if (w->power == power)
1160 continue;
1161
Mark Brown84e90932010-11-04 00:07:02 -04001162 trace_snd_soc_dapm_widget_power(w, power);
1163
Mark Brown6d3ddc82009-05-16 17:47:29 +01001164 if (power)
Mark Brown828a8422011-01-15 13:14:30 +00001165 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001166 else
Mark Brown828a8422011-01-15 13:14:30 +00001167 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001168
1169 w->power = power;
1170 break;
1171 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001172 }
1173
Mark Brownb14b76a2009-08-17 11:55:38 +01001174 /* If there are no DAPM widgets then try to figure out power from the
1175 * event type.
1176 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001177 if (!dapm->n_widgets) {
Mark Brownb14b76a2009-08-17 11:55:38 +01001178 switch (event) {
1179 case SND_SOC_DAPM_STREAM_START:
1180 case SND_SOC_DAPM_STREAM_RESUME:
Mark Brown56fba412011-06-04 11:25:10 +01001181 dapm->target_bias_level = SND_SOC_BIAS_ON;
Mark Brownb14b76a2009-08-17 11:55:38 +01001182 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001183 case SND_SOC_DAPM_STREAM_STOP:
Mark Brown56fba412011-06-04 11:25:10 +01001184 if (dapm->codec->active)
1185 dapm->target_bias_level = SND_SOC_BIAS_ON;
1186 else
1187 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001188 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001189 case SND_SOC_DAPM_STREAM_SUSPEND:
Mark Brown56fba412011-06-04 11:25:10 +01001190 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown50b6bce2009-11-23 13:11:53 +00001191 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001192 case SND_SOC_DAPM_STREAM_NOP:
Mark Brown56fba412011-06-04 11:25:10 +01001193 dapm->target_bias_level = dapm->bias_level;
Mark Brown50b6bce2009-11-23 13:11:53 +00001194 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001195 default:
1196 break;
1197 }
1198 }
1199
Mark Brown52ba67b2011-04-04 21:05:11 +09001200 /* Force all contexts in the card to the same bias state */
Mark Brown56fba412011-06-04 11:25:10 +01001201 bias = SND_SOC_BIAS_OFF;
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 if (d->target_bias_level > bias)
1204 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001205 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001206 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001207
1208
Mark Brown9d0624a2011-02-18 11:49:43 -08001209 /* Run all the bias changes in parallel */
1210 list_for_each_entry(d, &dapm->card->dapm_list, list)
1211 async_schedule_domain(dapm_pre_sequence_async, d,
1212 &async_domain);
1213 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001214
Mark Brown6d3ddc82009-05-16 17:47:29 +01001215 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001216 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001217
Mark Brown97404f22010-12-14 16:13:57 +00001218 dapm_widget_update(dapm);
1219
Mark Brown6d3ddc82009-05-16 17:47:29 +01001220 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001221 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001222
Mark Brown9d0624a2011-02-18 11:49:43 -08001223 /* Run all the bias changes in parallel */
1224 list_for_each_entry(d, &dapm->card->dapm_list, list)
1225 async_schedule_domain(dapm_post_sequence_async, d,
1226 &async_domain);
1227 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001228
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001229 pop_dbg(dapm->dev, card->pop_time,
1230 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001231 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001232
Mark Brown84e90932010-11-04 00:07:02 -04001233 trace_snd_soc_dapm_done(card);
1234
Mark Brown42aa3412009-03-01 19:21:10 +00001235 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001236}
1237
Mark Brown79fb9382009-08-21 16:38:13 +01001238#ifdef CONFIG_DEBUG_FS
1239static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1240{
1241 file->private_data = inode->i_private;
1242 return 0;
1243}
1244
1245static ssize_t dapm_widget_power_read_file(struct file *file,
1246 char __user *user_buf,
1247 size_t count, loff_t *ppos)
1248{
1249 struct snd_soc_dapm_widget *w = file->private_data;
1250 char *buf;
1251 int in, out;
1252 ssize_t ret;
1253 struct snd_soc_dapm_path *p = NULL;
1254
1255 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1256 if (!buf)
1257 return -ENOMEM;
1258
1259 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001260 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001261 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001262 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001263
Mark Brownd033c362009-12-04 15:25:56 +00001264 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001265 w->name, w->power ? "On" : "Off", in, out);
1266
Mark Brownd033c362009-12-04 15:25:56 +00001267 if (w->reg >= 0)
1268 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1269 " - R%d(0x%x) bit %d",
1270 w->reg, w->reg, w->shift);
1271
1272 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1273
Mark Brown3eef08b2009-09-14 16:49:00 +01001274 if (w->sname)
1275 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1276 w->sname,
1277 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001278
1279 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001280 if (p->connected && !p->connected(w, p->sink))
1281 continue;
1282
Mark Brown79fb9382009-08-21 16:38:13 +01001283 if (p->connect)
1284 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001285 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001286 p->name ? p->name : "static",
1287 p->source->name);
1288 }
1289 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001290 if (p->connected && !p->connected(w, p->sink))
1291 continue;
1292
Mark Brown79fb9382009-08-21 16:38:13 +01001293 if (p->connect)
1294 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001295 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001296 p->name ? p->name : "static",
1297 p->sink->name);
1298 }
1299
1300 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1301
1302 kfree(buf);
1303 return ret;
1304}
1305
1306static const struct file_operations dapm_widget_power_fops = {
1307 .open = dapm_widget_power_open_file,
1308 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001309 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001310};
1311
Mark Brownef49e4f2011-04-04 20:48:13 +09001312static int dapm_bias_open_file(struct inode *inode, struct file *file)
1313{
1314 file->private_data = inode->i_private;
1315 return 0;
1316}
1317
1318static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1319 size_t count, loff_t *ppos)
1320{
1321 struct snd_soc_dapm_context *dapm = file->private_data;
1322 char *level;
1323
1324 switch (dapm->bias_level) {
1325 case SND_SOC_BIAS_ON:
1326 level = "On\n";
1327 break;
1328 case SND_SOC_BIAS_PREPARE:
1329 level = "Prepare\n";
1330 break;
1331 case SND_SOC_BIAS_STANDBY:
1332 level = "Standby\n";
1333 break;
1334 case SND_SOC_BIAS_OFF:
1335 level = "Off\n";
1336 break;
1337 default:
1338 BUG();
1339 level = "Unknown\n";
1340 break;
1341 }
1342
1343 return simple_read_from_buffer(user_buf, count, ppos, level,
1344 strlen(level));
1345}
1346
1347static const struct file_operations dapm_bias_fops = {
1348 .open = dapm_bias_open_file,
1349 .read = dapm_bias_read_file,
1350 .llseek = default_llseek,
1351};
1352
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001353void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1354 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001355{
Mark Brown79fb9382009-08-21 16:38:13 +01001356 struct dentry *d;
1357
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001358 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1359
1360 if (!dapm->debugfs_dapm) {
1361 printk(KERN_WARNING
1362 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001363 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001364 }
Mark Brown79fb9382009-08-21 16:38:13 +01001365
Mark Brownef49e4f2011-04-04 20:48:13 +09001366 d = debugfs_create_file("bias_level", 0444,
1367 dapm->debugfs_dapm, dapm,
1368 &dapm_bias_fops);
1369 if (!d)
1370 dev_warn(dapm->dev,
1371 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001372}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001373
1374static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1375{
1376 struct snd_soc_dapm_context *dapm = w->dapm;
1377 struct dentry *d;
1378
1379 if (!dapm->debugfs_dapm || !w->name)
1380 return;
1381
1382 d = debugfs_create_file(w->name, 0444,
1383 dapm->debugfs_dapm, w,
1384 &dapm_widget_power_fops);
1385 if (!d)
1386 dev_warn(w->dapm->dev,
1387 "ASoC: Failed to create %s debugfs file\n",
1388 w->name);
1389}
1390
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001391static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1392{
1393 debugfs_remove_recursive(dapm->debugfs_dapm);
1394}
1395
Mark Brown79fb9382009-08-21 16:38:13 +01001396#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001397void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1398 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001399{
1400}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001401
1402static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1403{
1404}
1405
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001406static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1407{
1408}
1409
Mark Brown79fb9382009-08-21 16:38:13 +01001410#endif
1411
Richard Purdie2b97eab2006-10-06 18:32:18 +02001412/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001413static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001414 struct snd_kcontrol *kcontrol, int change,
1415 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001416{
1417 struct snd_soc_dapm_path *path;
1418 int found = 0;
1419
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001420 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001421 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001422 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001423 return -ENODEV;
1424
Mark Brown3a655772009-10-05 17:23:30 +01001425 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001426 return 0;
1427
1428 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001429 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001430 if (path->kcontrol != kcontrol)
1431 continue;
1432
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001433 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001434 continue;
1435
1436 found = 1;
1437 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001438 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001439 path->connect = 1; /* new connection */
1440 else
1441 path->connect = 0; /* old connection must be powered down */
1442 }
1443
Mark Brownb91b8fa2010-01-20 18:18:35 +00001444 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001445 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001446
1447 return 0;
1448}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001449
Milan plzik1b075e32008-01-10 14:39:46 +01001450/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001451static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001452 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001453{
1454 struct snd_soc_dapm_path *path;
1455 int found = 0;
1456
Milan plzik1b075e32008-01-10 14:39:46 +01001457 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001458 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001459 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001460 return -ENODEV;
1461
Richard Purdie2b97eab2006-10-06 18:32:18 +02001462 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001463 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001464 if (path->kcontrol != kcontrol)
1465 continue;
1466
1467 /* found, now check type */
1468 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001469 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001470 break;
1471 }
1472
Mark Brownb91b8fa2010-01-20 18:18:35 +00001473 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001474 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001475
1476 return 0;
1477}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001478
1479/* show dapm widget status in sys fs */
1480static ssize_t dapm_widget_show(struct device *dev,
1481 struct device_attribute *attr, char *buf)
1482{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001483 struct snd_soc_pcm_runtime *rtd =
1484 container_of(dev, struct snd_soc_pcm_runtime, dev);
1485 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001486 struct snd_soc_dapm_widget *w;
1487 int count = 0;
1488 char *state = "not set";
1489
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001490 list_for_each_entry(w, &codec->card->widgets, list) {
1491 if (w->dapm != &codec->dapm)
1492 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001493
1494 /* only display widgets that burnm power */
1495 switch (w->id) {
1496 case snd_soc_dapm_hp:
1497 case snd_soc_dapm_mic:
1498 case snd_soc_dapm_spk:
1499 case snd_soc_dapm_line:
1500 case snd_soc_dapm_micbias:
1501 case snd_soc_dapm_dac:
1502 case snd_soc_dapm_adc:
1503 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001504 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001505 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001506 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001507 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001508 if (w->name)
1509 count += sprintf(buf + count, "%s: %s\n",
1510 w->name, w->power ? "On":"Off");
1511 break;
1512 default:
1513 break;
1514 }
1515 }
1516
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001517 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001518 case SND_SOC_BIAS_ON:
1519 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001520 break;
Mark Brown0be98982008-05-19 12:31:28 +02001521 case SND_SOC_BIAS_PREPARE:
1522 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001523 break;
Mark Brown0be98982008-05-19 12:31:28 +02001524 case SND_SOC_BIAS_STANDBY:
1525 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001526 break;
Mark Brown0be98982008-05-19 12:31:28 +02001527 case SND_SOC_BIAS_OFF:
1528 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001529 break;
1530 }
1531 count += sprintf(buf + count, "PM State: %s\n", state);
1532
1533 return count;
1534}
1535
1536static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1537
1538int snd_soc_dapm_sys_add(struct device *dev)
1539{
Troy Kisky12ef1932008-10-13 17:42:14 -07001540 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001541}
1542
1543static void snd_soc_dapm_sys_remove(struct device *dev)
1544{
Mark Brownaef90842009-05-16 17:53:16 +01001545 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001546}
1547
1548/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001549static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001550{
1551 struct snd_soc_dapm_widget *w, *next_w;
1552 struct snd_soc_dapm_path *p, *next_p;
1553
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001554 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1555 if (w->dapm != dapm)
1556 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001557 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001558 /*
1559 * remove source and sink paths associated to this widget.
1560 * While removing the path, remove reference to it from both
1561 * source and sink widgets so that path is removed only once.
1562 */
1563 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1564 list_del(&p->list_sink);
1565 list_del(&p->list_source);
1566 list_del(&p->list);
1567 kfree(p->long_name);
1568 kfree(p);
1569 }
1570 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1571 list_del(&p->list_sink);
1572 list_del(&p->list_source);
1573 list_del(&p->list);
1574 kfree(p->long_name);
1575 kfree(p);
1576 }
Stephen Warrenfad59882011-04-28 17:37:59 -06001577 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001578 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001579 kfree(w);
1580 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001581}
1582
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001583static struct snd_soc_dapm_widget *dapm_find_widget(
1584 struct snd_soc_dapm_context *dapm, const char *pin,
1585 bool search_other_contexts)
1586{
1587 struct snd_soc_dapm_widget *w;
1588 struct snd_soc_dapm_widget *fallback = NULL;
1589
1590 list_for_each_entry(w, &dapm->card->widgets, list) {
1591 if (!strcmp(w->name, pin)) {
1592 if (w->dapm == dapm)
1593 return w;
1594 else
1595 fallback = w;
1596 }
1597 }
1598
1599 if (search_other_contexts)
1600 return fallback;
1601
1602 return NULL;
1603}
1604
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001605static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001606 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001607{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001608 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01001609
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001610 if (!w) {
1611 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1612 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01001613 }
1614
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001615 w->connected = status;
1616 if (status == 0)
1617 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09001618
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001619 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001620}
1621
Richard Purdie2b97eab2006-10-06 18:32:18 +02001622/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001623 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001624 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001625 *
1626 * Walks all dapm audio paths and powers widgets according to their
1627 * stream or path usage.
1628 *
1629 * Returns 0 for success.
1630 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001631int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001632{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001633 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001634}
Liam Girdwooda5302182008-07-07 13:35:17 +01001635EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001636
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001637static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001638 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001639{
1640 struct snd_soc_dapm_path *path;
1641 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001642 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001643 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001644 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001645 const char *source;
1646 char prefixed_sink[80];
1647 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001648 int ret = 0;
1649
Mark Brown88e8b9a2011-03-02 18:18:24 +00001650 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001651 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1652 dapm->codec->name_prefix, route->sink);
1653 sink = prefixed_sink;
1654 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1655 dapm->codec->name_prefix, route->source);
1656 source = prefixed_source;
1657 } else {
1658 sink = route->sink;
1659 source = route->source;
1660 }
1661
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001662 /*
1663 * find src and dest widgets over all widgets but favor a widget from
1664 * current DAPM context
1665 */
1666 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001667 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001668 wtsink = w;
1669 if (w->dapm == dapm)
1670 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001671 continue;
1672 }
1673 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001674 wtsource = w;
1675 if (w->dapm == dapm)
1676 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001677 }
1678 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001679 /* use widget from another DAPM context if not found from this */
1680 if (!wsink)
1681 wsink = wtsink;
1682 if (!wsource)
1683 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001684
1685 if (wsource == NULL || wsink == NULL)
1686 return -ENODEV;
1687
1688 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1689 if (!path)
1690 return -ENOMEM;
1691
1692 path->source = wsource;
1693 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001694 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001695 INIT_LIST_HEAD(&path->list);
1696 INIT_LIST_HEAD(&path->list_source);
1697 INIT_LIST_HEAD(&path->list_sink);
1698
1699 /* check for external widgets */
1700 if (wsink->id == snd_soc_dapm_input) {
1701 if (wsource->id == snd_soc_dapm_micbias ||
1702 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001703 wsource->id == snd_soc_dapm_line ||
1704 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001705 wsink->ext = 1;
1706 }
1707 if (wsource->id == snd_soc_dapm_output) {
1708 if (wsink->id == snd_soc_dapm_spk ||
1709 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001710 wsink->id == snd_soc_dapm_line ||
1711 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001712 wsource->ext = 1;
1713 }
1714
1715 /* connect static paths */
1716 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001717 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001718 list_add(&path->list_sink, &wsink->sources);
1719 list_add(&path->list_source, &wsource->sinks);
1720 path->connect = 1;
1721 return 0;
1722 }
1723
1724 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08001725 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001726 case snd_soc_dapm_adc:
1727 case snd_soc_dapm_dac:
1728 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001729 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001730 case snd_soc_dapm_input:
1731 case snd_soc_dapm_output:
1732 case snd_soc_dapm_micbias:
1733 case snd_soc_dapm_vmid:
1734 case snd_soc_dapm_pre:
1735 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001736 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001737 case snd_soc_dapm_aif_in:
1738 case snd_soc_dapm_aif_out:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001739 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001740 list_add(&path->list_sink, &wsink->sources);
1741 list_add(&path->list_source, &wsource->sinks);
1742 path->connect = 1;
1743 return 0;
1744 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001745 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001746 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001747 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06001748 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001749 if (ret != 0)
1750 goto err;
1751 break;
1752 case snd_soc_dapm_switch:
1753 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001754 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001755 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001756 if (ret != 0)
1757 goto err;
1758 break;
1759 case snd_soc_dapm_hp:
1760 case snd_soc_dapm_mic:
1761 case snd_soc_dapm_line:
1762 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001763 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001764 list_add(&path->list_sink, &wsink->sources);
1765 list_add(&path->list_source, &wsource->sinks);
1766 path->connect = 0;
1767 return 0;
1768 }
1769 return 0;
1770
1771err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001772 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1773 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001774 kfree(path);
1775 return ret;
1776}
Mark Brown105f1c22008-05-13 14:52:19 +02001777
1778/**
Mark Brown105f1c22008-05-13 14:52:19 +02001779 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001780 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02001781 * @route: audio routes
1782 * @num: number of routes
1783 *
1784 * Connects 2 dapm widgets together via a named audio path. The sink is
1785 * the widget receiving the audio signal, whilst the source is the sender
1786 * of the audio signal.
1787 *
1788 * Returns 0 for success else error. On error all resources can be freed
1789 * with a call to snd_soc_card_free().
1790 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001791int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02001792 const struct snd_soc_dapm_route *route, int num)
1793{
1794 int i, ret;
1795
1796 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001797 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001798 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001799 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1800 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02001801 return ret;
1802 }
1803 route++;
1804 }
1805
1806 return 0;
1807}
1808EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1809
1810/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001811 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001812 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001813 *
1814 * Checks the codec for any new dapm widgets and creates them if found.
1815 *
1816 * Returns 0 for success.
1817 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001818int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001819{
1820 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00001821 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001822
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001823 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001824 {
1825 if (w->new)
1826 continue;
1827
Stephen Warrenfad59882011-04-28 17:37:59 -06001828 if (w->num_kcontrols) {
1829 w->kcontrols = kzalloc(w->num_kcontrols *
1830 sizeof(struct snd_kcontrol *),
1831 GFP_KERNEL);
1832 if (!w->kcontrols)
1833 return -ENOMEM;
1834 }
1835
Richard Purdie2b97eab2006-10-06 18:32:18 +02001836 switch(w->id) {
1837 case snd_soc_dapm_switch:
1838 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001839 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001840 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001841 dapm_new_mixer(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001842 break;
1843 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001844 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001845 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001846 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001847 dapm_new_mux(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001848 break;
1849 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01001850 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01001851 w->power_check = dapm_adc_check_power;
1852 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001853 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01001854 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01001855 w->power_check = dapm_dac_check_power;
1856 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001857 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001858 case snd_soc_dapm_out_drv:
Mark Brownb75576d2009-04-20 17:56:13 +01001859 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001860 dapm_new_pga(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001861 break;
1862 case snd_soc_dapm_input:
1863 case snd_soc_dapm_output:
1864 case snd_soc_dapm_micbias:
1865 case snd_soc_dapm_spk:
1866 case snd_soc_dapm_hp:
1867 case snd_soc_dapm_mic:
1868 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001869 w->power_check = dapm_generic_check_power;
1870 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001871 case snd_soc_dapm_supply:
1872 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001873 case snd_soc_dapm_vmid:
1874 case snd_soc_dapm_pre:
1875 case snd_soc_dapm_post:
1876 break;
1877 }
Mark Brownb66a70d2011-02-09 18:04:11 +00001878
1879 /* Read the initial power state from the device */
1880 if (w->reg >= 0) {
1881 val = snd_soc_read(w->codec, w->reg);
1882 val &= 1 << w->shift;
1883 if (w->invert)
1884 val = !val;
1885
1886 if (val)
1887 w->power = 1;
1888 }
1889
Richard Purdie2b97eab2006-10-06 18:32:18 +02001890 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001891
1892 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001893 }
1894
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001895 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001896 return 0;
1897}
1898EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1899
1900/**
1901 * snd_soc_dapm_get_volsw - dapm mixer get callback
1902 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001903 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001904 *
1905 * Callback to get the value of a dapm mixer control.
1906 *
1907 * Returns 0 for success.
1908 */
1909int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1910 struct snd_ctl_elem_value *ucontrol)
1911{
Stephen Warrenfafd2172011-04-28 17:38:00 -06001912 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
1913 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01001914 struct soc_mixer_control *mc =
1915 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001916 unsigned int reg = mc->reg;
1917 unsigned int shift = mc->shift;
1918 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001919 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001920 unsigned int invert = mc->invert;
1921 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001922
Richard Purdie2b97eab2006-10-06 18:32:18 +02001923 ucontrol->value.integer.value[0] =
1924 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1925 if (shift != rshift)
1926 ucontrol->value.integer.value[1] =
1927 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1928 if (invert) {
1929 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001930 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001931 if (shift != rshift)
1932 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001933 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001934 }
1935
1936 return 0;
1937}
1938EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1939
1940/**
1941 * snd_soc_dapm_put_volsw - dapm mixer set callback
1942 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001943 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001944 *
1945 * Callback to set the value of a dapm mixer control.
1946 *
1947 * Returns 0 for success.
1948 */
1949int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1950 struct snd_ctl_elem_value *ucontrol)
1951{
Stephen Warrenfafd2172011-04-28 17:38:00 -06001952 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
1953 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
1954 struct snd_soc_codec *codec = widget->codec;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001955 struct soc_mixer_control *mc =
1956 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001957 unsigned int reg = mc->reg;
1958 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001959 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001960 unsigned int mask = (1 << fls(max)) - 1;
1961 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07001962 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00001963 int connect, change;
1964 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06001965 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001966
1967 val = (ucontrol->value.integer.value[0] & mask);
1968
1969 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001970 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07001971 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001972 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001973
Stephen Warrenfafd2172011-04-28 17:38:00 -06001974 if (val)
1975 /* new connection */
1976 connect = invert ? 0 : 1;
1977 else
1978 /* old connection must be powered down */
1979 connect = invert ? 1 : 0;
1980
1981 mutex_lock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001982
Stephen Warrene9cf7042011-01-27 14:54:05 -07001983 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00001984 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06001985 for (wi = 0; wi < wlist->num_widgets; wi++) {
1986 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00001987
Stephen Warrenfafd2172011-04-28 17:38:00 -06001988 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00001989
Stephen Warrenfafd2172011-04-28 17:38:00 -06001990 update.kcontrol = kcontrol;
1991 update.widget = widget;
1992 update.reg = reg;
1993 update.mask = mask;
1994 update.val = val;
1995 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00001996
Stephen Warrenfafd2172011-04-28 17:38:00 -06001997 dapm_mixer_update_power(widget, kcontrol, connect);
1998
1999 widget->dapm->update = NULL;
2000 }
Mark Brown283375c2009-12-07 18:09:03 +00002001 }
2002
Stephen Warrenfafd2172011-04-28 17:38:00 -06002003 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002004 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002005}
2006EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2007
2008/**
2009 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2010 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002011 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002012 *
2013 * Callback to get the value of a dapm enumerated double mixer control.
2014 *
2015 * Returns 0 for success.
2016 */
2017int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2018 struct snd_ctl_elem_value *ucontrol)
2019{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002020 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2021 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002022 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002023 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002024
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002025 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002026 ;
2027 val = snd_soc_read(widget->codec, e->reg);
2028 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2029 if (e->shift_l != e->shift_r)
2030 ucontrol->value.enumerated.item[1] =
2031 (val >> e->shift_r) & (bitmask - 1);
2032
2033 return 0;
2034}
2035EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2036
2037/**
2038 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2039 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002040 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002041 *
2042 * Callback to set the value of a dapm enumerated double mixer control.
2043 *
2044 * Returns 0 for success.
2045 */
2046int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2047 struct snd_ctl_elem_value *ucontrol)
2048{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002049 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2050 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2051 struct snd_soc_codec *codec = widget->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002052 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002053 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002054 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00002055 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002056 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002057
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002058 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002059 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002060 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002061 return -EINVAL;
2062 mux = ucontrol->value.enumerated.item[0];
2063 val = mux << e->shift_l;
2064 mask = (bitmask - 1) << e->shift_l;
2065 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002066 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002067 return -EINVAL;
2068 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2069 mask |= (bitmask - 1) << e->shift_r;
2070 }
2071
Stephen Warrenfafd2172011-04-28 17:38:00 -06002072 mutex_lock(&codec->mutex);
2073
Mark Brown3a655772009-10-05 17:23:30 +01002074 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002075 if (change) {
2076 for (wi = 0; wi < wlist->num_widgets; wi++) {
2077 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002078
Stephen Warrenfafd2172011-04-28 17:38:00 -06002079 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002080
Stephen Warrenfafd2172011-04-28 17:38:00 -06002081 update.kcontrol = kcontrol;
2082 update.widget = widget;
2083 update.reg = e->reg;
2084 update.mask = mask;
2085 update.val = val;
2086 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002087
Stephen Warrenfafd2172011-04-28 17:38:00 -06002088 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002089
Stephen Warrenfafd2172011-04-28 17:38:00 -06002090 widget->dapm->update = NULL;
2091 }
2092 }
2093
2094 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002095 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002096}
2097EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2098
2099/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002100 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2101 * @kcontrol: mixer control
2102 * @ucontrol: control element information
2103 *
2104 * Returns 0 for success.
2105 */
2106int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2107 struct snd_ctl_elem_value *ucontrol)
2108{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002109 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2110 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002111
2112 ucontrol->value.enumerated.item[0] = widget->value;
2113
2114 return 0;
2115}
2116EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2117
2118/**
2119 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2120 * @kcontrol: mixer control
2121 * @ucontrol: control element information
2122 *
2123 * Returns 0 for success.
2124 */
2125int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2126 struct snd_ctl_elem_value *ucontrol)
2127{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002128 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2129 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2130 struct snd_soc_codec *codec = widget->codec;
Mark Brownd2b247a2009-10-06 15:21:04 +01002131 struct soc_enum *e =
2132 (struct soc_enum *)kcontrol->private_value;
2133 int change;
2134 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002135 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002136
2137 if (ucontrol->value.enumerated.item[0] >= e->max)
2138 return -EINVAL;
2139
Stephen Warrenfafd2172011-04-28 17:38:00 -06002140 mutex_lock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002141
2142 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002143 if (change) {
2144 for (wi = 0; wi < wlist->num_widgets; wi++) {
2145 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002146
Stephen Warrenfafd2172011-04-28 17:38:00 -06002147 widget->value = ucontrol->value.enumerated.item[0];
2148
2149 dapm_mux_update_power(widget, kcontrol, change,
2150 widget->value, e);
2151 }
2152 }
2153
2154 mutex_unlock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002155 return ret;
2156}
2157EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2158
2159/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002160 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2161 * callback
2162 * @kcontrol: mixer control
2163 * @ucontrol: control element information
2164 *
2165 * Callback to get the value of a dapm semi enumerated double mixer control.
2166 *
2167 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2168 * used for handling bitfield coded enumeration for example.
2169 *
2170 * Returns 0 for success.
2171 */
2172int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2173 struct snd_ctl_elem_value *ucontrol)
2174{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002175 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2176 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002177 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002178 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002179
2180 reg_val = snd_soc_read(widget->codec, e->reg);
2181 val = (reg_val >> e->shift_l) & e->mask;
2182 for (mux = 0; mux < e->max; mux++) {
2183 if (val == e->values[mux])
2184 break;
2185 }
2186 ucontrol->value.enumerated.item[0] = mux;
2187 if (e->shift_l != e->shift_r) {
2188 val = (reg_val >> e->shift_r) & e->mask;
2189 for (mux = 0; mux < e->max; mux++) {
2190 if (val == e->values[mux])
2191 break;
2192 }
2193 ucontrol->value.enumerated.item[1] = mux;
2194 }
2195
2196 return 0;
2197}
2198EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2199
2200/**
2201 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2202 * callback
2203 * @kcontrol: mixer control
2204 * @ucontrol: control element information
2205 *
2206 * Callback to set the value of a dapm semi enumerated double mixer control.
2207 *
2208 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2209 * used for handling bitfield coded enumeration for example.
2210 *
2211 * Returns 0 for success.
2212 */
2213int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2214 struct snd_ctl_elem_value *ucontrol)
2215{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002216 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2217 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2218 struct snd_soc_codec *codec = widget->codec;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002219 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002220 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002221 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002222 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002223 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002224
2225 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2226 return -EINVAL;
2227 mux = ucontrol->value.enumerated.item[0];
2228 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2229 mask = e->mask << e->shift_l;
2230 if (e->shift_l != e->shift_r) {
2231 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2232 return -EINVAL;
2233 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2234 mask |= e->mask << e->shift_r;
2235 }
2236
Stephen Warrenfafd2172011-04-28 17:38:00 -06002237 mutex_lock(&codec->mutex);
2238
Mark Brown3a655772009-10-05 17:23:30 +01002239 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002240 if (change) {
2241 for (wi = 0; wi < wlist->num_widgets; wi++) {
2242 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002243
Stephen Warrenfafd2172011-04-28 17:38:00 -06002244 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002245
Stephen Warrenfafd2172011-04-28 17:38:00 -06002246 update.kcontrol = kcontrol;
2247 update.widget = widget;
2248 update.reg = e->reg;
2249 update.mask = mask;
2250 update.val = val;
2251 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002252
Stephen Warrenfafd2172011-04-28 17:38:00 -06002253 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002254
Stephen Warrenfafd2172011-04-28 17:38:00 -06002255 widget->dapm->update = NULL;
2256 }
2257 }
2258
2259 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002260 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002261}
2262EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2263
2264/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002265 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2266 *
2267 * @kcontrol: mixer control
2268 * @uinfo: control element information
2269 *
2270 * Callback to provide information about a pin switch control.
2271 */
2272int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2273 struct snd_ctl_elem_info *uinfo)
2274{
2275 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2276 uinfo->count = 1;
2277 uinfo->value.integer.min = 0;
2278 uinfo->value.integer.max = 1;
2279
2280 return 0;
2281}
2282EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2283
2284/**
2285 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2286 *
2287 * @kcontrol: mixer control
2288 * @ucontrol: Value
2289 */
2290int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2291 struct snd_ctl_elem_value *ucontrol)
2292{
2293 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2294 const char *pin = (const char *)kcontrol->private_value;
2295
2296 mutex_lock(&codec->mutex);
2297
2298 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002299 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002300
2301 mutex_unlock(&codec->mutex);
2302
2303 return 0;
2304}
2305EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2306
2307/**
2308 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2309 *
2310 * @kcontrol: mixer control
2311 * @ucontrol: Value
2312 */
2313int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2314 struct snd_ctl_elem_value *ucontrol)
2315{
2316 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2317 const char *pin = (const char *)kcontrol->private_value;
2318
2319 mutex_lock(&codec->mutex);
2320
2321 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002322 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002323 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002324 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002325
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002326 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002327
2328 mutex_unlock(&codec->mutex);
2329
2330 return 0;
2331}
2332EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2333
2334/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002335 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002336 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002337 * @widget: widget template
2338 *
2339 * Creates a new dapm control based upon the template.
2340 *
2341 * Returns 0 for success else error.
2342 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002343int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002344 const struct snd_soc_dapm_widget *widget)
2345{
2346 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002347 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002348
2349 if ((w = dapm_cnew_widget(widget)) == NULL)
2350 return -ENOMEM;
2351
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002352 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002353 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002354 name_len += 1 + strlen(dapm->codec->name_prefix);
2355 w->name = kmalloc(name_len, GFP_KERNEL);
2356 if (w->name == NULL) {
2357 kfree(w);
2358 return -ENOMEM;
2359 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002360 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002361 snprintf(w->name, name_len, "%s %s",
2362 dapm->codec->name_prefix, widget->name);
2363 else
2364 snprintf(w->name, name_len, "%s", widget->name);
2365
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002366 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002367 w->dapm = dapm;
2368 w->codec = dapm->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002369 INIT_LIST_HEAD(&w->sources);
2370 INIT_LIST_HEAD(&w->sinks);
2371 INIT_LIST_HEAD(&w->list);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002372 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002373
2374 /* machine layer set ups unconnected pins and insertions */
2375 w->connected = 1;
2376 return 0;
2377}
2378EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2379
2380/**
Mark Brown4ba13272008-05-13 14:51:19 +02002381 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002382 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002383 * @widget: widget array
2384 * @num: number of widgets
2385 *
2386 * Creates new DAPM controls based upon the templates.
2387 *
2388 * Returns 0 for success else error.
2389 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002390int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002391 const struct snd_soc_dapm_widget *widget,
2392 int num)
2393{
2394 int i, ret;
2395
2396 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002397 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002398 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002399 dev_err(dapm->dev,
2400 "ASoC: Failed to create DAPM control %s: %d\n",
2401 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002402 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002403 }
Mark Brown4ba13272008-05-13 14:51:19 +02002404 widget++;
2405 }
2406 return 0;
2407}
2408EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2409
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002410static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002411 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002412{
2413 struct snd_soc_dapm_widget *w;
2414
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002415 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002416 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002417 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002418 continue;
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002419 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2420 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002421 if (strstr(w->sname, stream)) {
2422 switch(event) {
2423 case SND_SOC_DAPM_STREAM_START:
2424 w->active = 1;
2425 break;
2426 case SND_SOC_DAPM_STREAM_STOP:
2427 w->active = 0;
2428 break;
2429 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002430 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002431 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002432 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2433 break;
2434 }
2435 }
2436 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002437
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002438 dapm_power_widgets(dapm, event);
2439}
2440
2441/**
2442 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2443 * @rtd: PCM runtime data
2444 * @stream: stream name
2445 * @event: stream event
2446 *
2447 * Sends a stream event to the dapm core. The core then makes any
2448 * necessary widget power changes.
2449 *
2450 * Returns 0 for success else error.
2451 */
2452int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2453 const char *stream, int event)
2454{
2455 struct snd_soc_codec *codec = rtd->codec;
2456
2457 if (stream == NULL)
2458 return 0;
2459
2460 mutex_lock(&codec->mutex);
2461 soc_dapm_stream_event(&codec->dapm, stream, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002462 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002463 return 0;
2464}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002465
2466/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002467 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002468 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002469 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002470 *
Mark Brown74b8f952009-06-06 11:26:15 +01002471 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002472 * a valid audio route and active audio stream.
2473 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2474 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002475 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002476int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002477{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002478 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002479}
Liam Girdwooda5302182008-07-07 13:35:17 +01002480EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002481
2482/**
Mark Brownda341832010-03-15 19:23:37 +00002483 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002484 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002485 * @pin: pin name
2486 *
2487 * Enables input/output pin regardless of any other state. This is
2488 * intended for use with microphone bias supplies used in microphone
2489 * jack detection.
2490 *
2491 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2492 * do any widget power switching.
2493 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002494int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2495 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002496{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002497 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00002498
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002499 if (!w) {
2500 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2501 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00002502 }
2503
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002504 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2505 w->connected = 1;
2506 w->force = 1;
Mark Brown0d867332011-04-06 11:38:14 +09002507
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002508 return 0;
Mark Brownda341832010-03-15 19:23:37 +00002509}
2510EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2511
2512/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002513 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002514 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002515 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002516 *
Mark Brown74b8f952009-06-06 11:26:15 +01002517 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002518 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2519 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002520 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002521int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2522 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002523{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002524 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002525}
2526EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2527
2528/**
Mark Brown5817b522008-09-24 11:23:11 +01002529 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002530 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002531 * @pin: pin name
2532 *
2533 * Marks the specified pin as being not connected, disabling it along
2534 * any parent or child widgets. At present this is identical to
2535 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2536 * additional things such as disabling controls which only affect
2537 * paths through the pin.
2538 *
2539 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2540 * do any widget power switching.
2541 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002542int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002543{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002544 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002545}
2546EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2547
2548/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002549 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002550 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002551 * @pin: audio signal pin endpoint (or start point)
2552 *
2553 * Get audio pin status - connected or disconnected.
2554 *
2555 * Returns 1 for connected otherwise 0.
2556 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002557int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2558 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002559{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002560 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002561
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002562 if (w)
2563 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06002564
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002565 return 0;
2566}
Liam Girdwooda5302182008-07-07 13:35:17 +01002567EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002568
2569/**
Mark Brown1547aba2010-05-07 21:11:40 +01002570 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002571 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01002572 * @pin: audio signal pin endpoint (or start point)
2573 *
2574 * Mark the given endpoint or pin as ignoring suspend. When the
2575 * system is disabled a path between two endpoints flagged as ignoring
2576 * suspend will not be disabled. The path must already be enabled via
2577 * normal means at suspend time, it will not be turned on if it was not
2578 * already enabled.
2579 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002580int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2581 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01002582{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002583 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01002584
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002585 if (!w) {
2586 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2587 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01002588 }
2589
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002590 w->ignore_suspend = 1;
2591
2592 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01002593}
2594EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2595
2596/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002597 * snd_soc_dapm_free - free dapm resources
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002598 * @card: SoC device
Richard Purdie2b97eab2006-10-06 18:32:18 +02002599 *
2600 * Free all dapm widgets and resources.
2601 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002602void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002603{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002604 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002605 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002606 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02002607 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002608}
2609EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2610
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002611static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01002612{
Mark Brown51737472009-06-22 13:16:51 +01002613 struct snd_soc_dapm_widget *w;
2614 LIST_HEAD(down_list);
2615 int powerdown = 0;
2616
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002617 list_for_each_entry(w, &dapm->card->widgets, list) {
2618 if (w->dapm != dapm)
2619 continue;
Mark Brown51737472009-06-22 13:16:51 +01002620 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00002621 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002622 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002623 powerdown = 1;
2624 }
2625 }
2626
2627 /* If there were no widgets to power down we're already in
2628 * standby.
2629 */
2630 if (powerdown) {
Mark Browned5a4c42011-02-18 11:12:42 -08002631 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00002632 dapm_seq_run(dapm, &down_list, 0, false);
Mark Browned5a4c42011-02-18 11:12:42 -08002633 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01002634 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002635}
Mark Brown51737472009-06-22 13:16:51 +01002636
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002637/*
2638 * snd_soc_dapm_shutdown - callback for system shutdown
2639 */
2640void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2641{
2642 struct snd_soc_codec *codec;
2643
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002644 list_for_each_entry(codec, &card->codec_dev_list, list) {
2645 soc_dapm_shutdown_codec(&codec->dapm);
Mark Browned5a4c42011-02-18 11:12:42 -08002646 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002647 }
Mark Brown51737472009-06-22 13:16:51 +01002648}
2649
Richard Purdie2b97eab2006-10-06 18:32:18 +02002650/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002651MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002652MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2653MODULE_LICENSE("GPL");