blob: 776e6f418306fe004936692fd70dab7c89cdec6d [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 Brownf83fba82009-05-18 15:44:43 +0100142 switch (level) {
143 case SND_SOC_BIAS_ON:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200144 dev_dbg(dapm->dev, "Setting full bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100145 break;
146 case SND_SOC_BIAS_PREPARE:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200147 dev_dbg(dapm->dev, "Setting bias prepare\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100148 break;
149 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200150 dev_dbg(dapm->dev, "Setting standby bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100151 break;
152 case SND_SOC_BIAS_OFF:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200153 dev_dbg(dapm->dev, "Setting bias off\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100154 break;
155 default:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200156 dev_err(dapm->dev, "Setting invalid bias %d\n", level);
Mark Brownf83fba82009-05-18 15:44:43 +0100157 return -EINVAL;
158 }
159
Mark Brown84e90932010-11-04 00:07:02 -0400160 trace_snd_soc_bias_level_start(card, level);
161
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000162 if (card && card->set_bias_level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100163 ret = card->set_bias_level(card, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100164 if (ret == 0) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200165 if (dapm->codec && dapm->codec->driver->set_bias_level)
166 ret = dapm->codec->driver->set_bias_level(dapm->codec, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100167 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200168 dapm->bias_level = level;
Mark Brown474e09c2009-08-19 14:18:53 +0100169 }
Mark Brown1badabd2010-12-04 12:41:04 +0000170 if (ret == 0) {
171 if (card && card->set_bias_level_post)
172 ret = card->set_bias_level_post(card, level);
173 }
Mark Brown452c5ea2009-05-17 21:41:23 +0100174
Mark Brown84e90932010-11-04 00:07:02 -0400175 trace_snd_soc_bias_level_done(card, level);
176
Mark Brown452c5ea2009-05-17 21:41:23 +0100177 return ret;
178}
179
Richard Purdie2b97eab2006-10-06 18:32:18 +0200180/* set up initial codec paths */
181static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
182 struct snd_soc_dapm_path *p, int i)
183{
184 switch (w->id) {
185 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000186 case snd_soc_dapm_mixer:
187 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200188 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100189 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600190 w->kcontrol_news[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400191 unsigned int reg = mc->reg;
192 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100193 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400194 unsigned int mask = (1 << fls(max)) - 1;
195 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200196
197 val = snd_soc_read(w->codec, reg);
198 val = (val >> shift) & mask;
199
200 if ((invert && !val) || (!invert && val))
201 p->connect = 1;
202 else
203 p->connect = 0;
204 }
205 break;
206 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600207 struct soc_enum *e = (struct soc_enum *)
208 w->kcontrol_news[i].private_value;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200209 int val, item, bitmask;
210
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100211 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200212 ;
213 val = snd_soc_read(w->codec, e->reg);
214 item = (val >> e->shift_l) & (bitmask - 1);
215
216 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100217 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200218 if (!(strcmp(p->name, e->texts[i])) && item == i)
219 p->connect = 1;
220 }
221 }
222 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000223 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600224 struct soc_enum *e = (struct soc_enum *)
225 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000226
227 p->connect = 0;
228 /* since a virtual mux has no backing registers to
229 * decide which path to connect, it will try to match
230 * with the first enumeration. This is to ensure
231 * that the default mux choice (the first) will be
232 * correctly powered up during initialization.
233 */
234 if (!strcmp(p->name, e->texts[0]))
235 p->connect = 1;
236 }
237 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200238 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200239 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600240 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200241 int val, item;
242
243 val = snd_soc_read(w->codec, e->reg);
244 val = (val >> e->shift_l) & e->mask;
245 for (item = 0; item < e->max; item++) {
246 if (val == e->values[item])
247 break;
248 }
249
250 p->connect = 0;
251 for (i = 0; i < e->max; i++) {
252 if (!(strcmp(p->name, e->texts[i])) && item == i)
253 p->connect = 1;
254 }
255 }
256 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200257 /* does not effect routing - always connected */
258 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600259 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200260 case snd_soc_dapm_output:
261 case snd_soc_dapm_adc:
262 case snd_soc_dapm_input:
263 case snd_soc_dapm_dac:
264 case snd_soc_dapm_micbias:
265 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100266 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100267 case snd_soc_dapm_aif_in:
268 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200269 p->connect = 1;
270 break;
271 /* does effect routing - dynamically connected */
272 case snd_soc_dapm_hp:
273 case snd_soc_dapm_mic:
274 case snd_soc_dapm_spk:
275 case snd_soc_dapm_line:
276 case snd_soc_dapm_pre:
277 case snd_soc_dapm_post:
278 p->connect = 0;
279 break;
280 }
281}
282
Mark Brown74b8f952009-06-06 11:26:15 +0100283/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200284static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200285 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
286 struct snd_soc_dapm_path *path, const char *control_name,
287 const struct snd_kcontrol_new *kcontrol)
288{
289 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
290 int i;
291
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100292 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200293 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200294 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200295 list_add(&path->list_sink, &dest->sources);
296 list_add(&path->list_source, &src->sinks);
297 path->name = (char*)e->texts[i];
298 dapm_set_path_status(dest, path, 0);
299 return 0;
300 }
301 }
302
303 return -ENODEV;
304}
305
Mark Brown74b8f952009-06-06 11:26:15 +0100306/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200307static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200308 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
309 struct snd_soc_dapm_path *path, const char *control_name)
310{
311 int i;
312
313 /* search for mixer kcontrol */
314 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600315 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200316 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200317 list_add(&path->list_sink, &dest->sources);
318 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600319 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200320 dapm_set_path_status(dest, path, i);
321 return 0;
322 }
323 }
324 return -ENODEV;
325}
326
Stephen Warrenaf468002011-04-28 17:38:01 -0600327static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600328 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600329 const struct snd_kcontrol_new *kcontrol_new,
330 struct snd_kcontrol **kcontrol)
331{
332 struct snd_soc_dapm_widget *w;
333 int i;
334
335 *kcontrol = NULL;
336
337 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600338 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
339 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600340 for (i = 0; i < w->num_kcontrols; i++) {
341 if (&w->kcontrol_news[i] == kcontrol_new) {
342 if (w->kcontrols)
343 *kcontrol = w->kcontrols[i];
344 return 1;
345 }
346 }
347 }
348
349 return 0;
350}
351
Richard Purdie2b97eab2006-10-06 18:32:18 +0200352/* create new dapm mixer control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200353static int dapm_new_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200354 struct snd_soc_dapm_widget *w)
355{
356 int i, ret = 0;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000357 size_t name_len, prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200358 struct snd_soc_dapm_path *path;
Mark Brown12ea2c72011-03-02 18:17:32 +0000359 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000360 const char *prefix;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600361 struct snd_soc_dapm_widget_list *wlist;
362 size_t wlistsize;
Mark Brownefb7ac32011-03-08 17:23:24 +0000363
364 if (dapm->codec)
365 prefix = dapm->codec->name_prefix;
366 else
367 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200368
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000369 if (prefix)
370 prefix_len = strlen(prefix) + 1;
371 else
372 prefix_len = 0;
373
Richard Purdie2b97eab2006-10-06 18:32:18 +0200374 /* add kcontrol */
375 for (i = 0; i < w->num_kcontrols; i++) {
376
377 /* match name */
378 list_for_each_entry(path, &w->sources, list_sink) {
379
380 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600381 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200382 continue;
383
Stephen Warrenfafd2172011-04-28 17:38:00 -0600384 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
385 sizeof(struct snd_soc_dapm_widget *),
386 wlist = kzalloc(wlistsize, GFP_KERNEL);
387 if (wlist == NULL) {
388 dev_err(dapm->dev,
389 "asoc: can't allocate widget list for %s\n",
390 w->name);
391 return -ENOMEM;
392 }
393 wlist->num_widgets = 1;
394 wlist->widgets[0] = w;
395
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000396 /* add dapm control with long name.
397 * for dapm_mixer this is the concatenation of the
398 * mixer and kcontrol name.
399 * for dapm_mixer_named_ctl this is simply the
400 * kcontrol name.
401 */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600402 name_len = strlen(w->kcontrol_news[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000403 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000404 name_len += 1 + strlen(w->name);
405
Mark Brown219b93f2008-10-28 13:02:31 +0000406 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000407
Stephen Warrenfafd2172011-04-28 17:38:00 -0600408 if (path->long_name == NULL) {
409 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200410 return -ENOMEM;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600411 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200412
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000413 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000414 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000415 /* The control will get a prefix from
416 * the control creation process but
417 * we're also using the same prefix
418 * for widgets so cut the prefix off
419 * the front of the widget name.
420 */
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000421 snprintf(path->long_name, name_len, "%s %s",
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000422 w->name + prefix_len,
Stephen Warren82cfecd2011-04-28 17:37:58 -0600423 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000424 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000425 case snd_soc_dapm_mixer_named_ctl:
426 snprintf(path->long_name, name_len, "%s",
Stephen Warren82cfecd2011-04-28 17:37:58 -0600427 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000428 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000429 }
430
Mark Brown219b93f2008-10-28 13:02:31 +0000431 path->long_name[name_len - 1] = '\0';
432
Stephen Warrenfafd2172011-04-28 17:38:00 -0600433 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
434 wlist, path->long_name,
435 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200436 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200437 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200438 dev_err(dapm->dev,
439 "asoc: failed to add dapm kcontrol %s: %d\n",
440 path->long_name, ret);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600441 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200442 kfree(path->long_name);
443 path->long_name = NULL;
444 return ret;
445 }
Stephen Warrenfad59882011-04-28 17:37:59 -0600446 w->kcontrols[i] = path->kcontrol;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200447 }
448 }
449 return ret;
450}
451
452/* create new dapm mux control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200453static int dapm_new_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200454 struct snd_soc_dapm_widget *w)
455{
456 struct snd_soc_dapm_path *path = NULL;
457 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000458 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000459 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000460 size_t prefix_len;
Stephen Warrenaf468002011-04-28 17:38:01 -0600461 int ret;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600462 struct snd_soc_dapm_widget_list *wlist;
Stephen Warrenaf468002011-04-28 17:38:01 -0600463 int shared, wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600464 size_t wlistsize;
Stephen Warrenaf468002011-04-28 17:38:01 -0600465 char *name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200466
Stephen Warrenaf468002011-04-28 17:38:01 -0600467 if (w->num_kcontrols != 1) {
468 dev_err(dapm->dev,
469 "asoc: mux %s has incorrect number of controls\n",
470 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200471 return -EINVAL;
472 }
473
Stephen Warren1007da02011-05-26 09:57:33 -0600474 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
Stephen Warrenaf468002011-04-28 17:38:01 -0600475 &kcontrol);
476 if (kcontrol) {
477 wlist = kcontrol->private_data;
478 wlistentries = wlist->num_widgets + 1;
479 } else {
480 wlist = NULL;
481 wlistentries = 1;
482 }
Stephen Warrenfafd2172011-04-28 17:38:00 -0600483 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
Stephen Warrenaf468002011-04-28 17:38:01 -0600484 wlistentries * sizeof(struct snd_soc_dapm_widget *),
485 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600486 if (wlist == NULL) {
487 dev_err(dapm->dev,
488 "asoc: can't allocate widget list for %s\n", w->name);
489 return -ENOMEM;
490 }
Stephen Warrenaf468002011-04-28 17:38:01 -0600491 wlist->num_widgets = wlistentries;
492 wlist->widgets[wlistentries - 1] = w;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600493
Stephen Warrenaf468002011-04-28 17:38:01 -0600494 if (!kcontrol) {
495 if (dapm->codec)
496 prefix = dapm->codec->name_prefix;
497 else
498 prefix = NULL;
Mark Brownefb7ac32011-03-08 17:23:24 +0000499
Stephen Warrenaf468002011-04-28 17:38:01 -0600500 if (shared) {
501 name = w->kcontrol_news[0].name;
502 prefix_len = 0;
503 } else {
504 name = w->name;
505 if (prefix)
506 prefix_len = strlen(prefix) + 1;
507 else
508 prefix_len = 0;
509 }
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000510
Stephen Warrenaf468002011-04-28 17:38:01 -0600511 /*
512 * The control will get a prefix from the control creation
513 * process but we're also using the same prefix for widgets so
514 * cut the prefix off the front of the widget name.
515 */
516 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
517 name + prefix_len, prefix);
518 ret = snd_ctl_add(card, kcontrol);
519 if (ret < 0) {
520 dev_err(dapm->dev,
521 "asoc: failed to add kcontrol %s\n", w->name);
522 kfree(wlist);
523 return ret;
524 }
525 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200526
Stephen Warrenaf468002011-04-28 17:38:01 -0600527 kcontrol->private_data = wlist;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200528
Stephen Warrenfad59882011-04-28 17:37:59 -0600529 w->kcontrols[0] = kcontrol;
530
Richard Purdie2b97eab2006-10-06 18:32:18 +0200531 list_for_each_entry(path, &w->sources, list_sink)
532 path->kcontrol = kcontrol;
533
Stephen Warrenaf468002011-04-28 17:38:01 -0600534 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200535}
536
537/* create new dapm volume control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200538static int dapm_new_pga(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200539 struct snd_soc_dapm_widget *w)
540{
Mark Browna6c65732010-03-03 17:45:21 +0000541 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200542 dev_err(w->dapm->dev,
543 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200544
Mark Browna6c65732010-03-03 17:45:21 +0000545 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200546}
547
548/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200549static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200550{
551 struct snd_soc_dapm_path *p;
552
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200553 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200554 p->walked = 0;
555}
556
Mark Brown99497882010-05-07 20:24:05 +0100557/* We implement power down on suspend by checking the power state of
558 * the ALSA card - when we are suspending the ALSA state for the card
559 * is set to D3.
560 */
561static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
562{
Mark Brown12ea2c72011-03-02 18:17:32 +0000563 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100564
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000565 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100566 case SNDRV_CTL_POWER_D3hot:
567 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100568 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200569 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
570 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100571 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100572 default:
573 return 1;
574 }
575}
576
Richard Purdie2b97eab2006-10-06 18:32:18 +0200577/*
578 * Recursively check for a completed path to an active or physically connected
579 * output widget. Returns number of complete paths.
580 */
581static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
582{
583 struct snd_soc_dapm_path *path;
584 int con = 0;
585
Mark Brown246d0a12009-04-22 18:24:55 +0100586 if (widget->id == snd_soc_dapm_supply)
587 return 0;
588
Mark Brown010ff262009-08-17 17:39:22 +0100589 switch (widget->id) {
590 case snd_soc_dapm_adc:
591 case snd_soc_dapm_aif_out:
592 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100593 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100594 default:
595 break;
596 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200597
598 if (widget->connected) {
599 /* connected pin ? */
600 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100601 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200602
603 /* connected jack or spk ? */
604 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300605 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown99497882010-05-07 20:24:05 +0100606 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200607 }
608
609 list_for_each_entry(path, &widget->sinks, list_source) {
610 if (path->walked)
611 continue;
612
613 if (path->sink && path->connect) {
614 path->walked = 1;
615 con += is_connected_output_ep(path->sink);
616 }
617 }
618
619 return con;
620}
621
622/*
623 * Recursively check for a completed path to an active or physically connected
624 * input widget. Returns number of complete paths.
625 */
626static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
627{
628 struct snd_soc_dapm_path *path;
629 int con = 0;
630
Mark Brown246d0a12009-04-22 18:24:55 +0100631 if (widget->id == snd_soc_dapm_supply)
632 return 0;
633
Richard Purdie2b97eab2006-10-06 18:32:18 +0200634 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100635 switch (widget->id) {
636 case snd_soc_dapm_dac:
637 case snd_soc_dapm_aif_in:
638 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100639 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100640 default:
641 break;
642 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200643
644 if (widget->connected) {
645 /* connected pin ? */
646 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100647 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200648
649 /* connected VMID/Bias for lower pops */
650 if (widget->id == snd_soc_dapm_vmid)
Mark Brown99497882010-05-07 20:24:05 +0100651 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200652
653 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300654 if (widget->id == snd_soc_dapm_mic ||
655 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown99497882010-05-07 20:24:05 +0100656 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200657 }
658
659 list_for_each_entry(path, &widget->sources, list_sink) {
660 if (path->walked)
661 continue;
662
663 if (path->source && path->connect) {
664 path->walked = 1;
665 con += is_connected_input_ep(path->source);
666 }
667 }
668
669 return con;
670}
671
672/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300673 * Handler for generic register modifier widget.
674 */
675int dapm_reg_event(struct snd_soc_dapm_widget *w,
676 struct snd_kcontrol *kcontrol, int event)
677{
678 unsigned int val;
679
680 if (SND_SOC_DAPM_EVENT_ON(event))
681 val = w->on_val;
682 else
683 val = w->off_val;
684
685 snd_soc_update_bits(w->codec, -(w->reg + 1),
686 w->mask << w->shift, val << w->shift);
687
688 return 0;
689}
Mark Brown11589412008-07-29 11:42:23 +0100690EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300691
Mark Browncd0f2d42009-04-20 16:56:59 +0100692/* Generic check to see if a widget should be powered.
693 */
694static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
695{
696 int in, out;
697
698 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200699 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100700 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200701 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100702 return out != 0 && in != 0;
703}
704
Mark Brown6ea31b92009-04-20 17:15:41 +0100705/* Check to see if an ADC has power */
706static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
707{
708 int in;
709
710 if (w->active) {
711 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200712 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100713 return in != 0;
714 } else {
715 return dapm_generic_check_power(w);
716 }
717}
718
719/* Check to see if a DAC has power */
720static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
721{
722 int out;
723
724 if (w->active) {
725 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200726 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100727 return out != 0;
728 } else {
729 return dapm_generic_check_power(w);
730 }
731}
732
Mark Brown246d0a12009-04-22 18:24:55 +0100733/* Check to see if a power supply is needed */
734static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
735{
736 struct snd_soc_dapm_path *path;
737 int power = 0;
738
739 /* Check if one of our outputs is connected */
740 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +0100741 if (path->connected &&
742 !path->connected(path->source, path->sink))
743 continue;
744
Mark Brown30173582011-02-11 11:42:19 +0000745 if (!path->sink)
746 continue;
747
748 if (path->sink->force) {
749 power = 1;
750 break;
751 }
752
753 if (path->sink->power_check &&
Mark Brown246d0a12009-04-22 18:24:55 +0100754 path->sink->power_check(path->sink)) {
755 power = 1;
756 break;
757 }
758 }
759
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200760 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100761
762 return power;
763}
764
Mark Brown38357ab2009-06-06 19:03:23 +0100765static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
766 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +0000767 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +0000768{
Mark Brown828a8422011-01-15 13:14:30 +0000769 int *sort;
770
771 if (power_up)
772 sort = dapm_up_seq;
773 else
774 sort = dapm_down_seq;
775
Mark Brown38357ab2009-06-06 19:03:23 +0100776 if (sort[a->id] != sort[b->id])
777 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +0000778 if (a->subseq != b->subseq) {
779 if (power_up)
780 return a->subseq - b->subseq;
781 else
782 return b->subseq - a->subseq;
783 }
Mark Brownb22ead22009-06-07 12:51:26 +0100784 if (a->reg != b->reg)
785 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +0000786 if (a->dapm != b->dapm)
787 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +0000788
Mark Brown38357ab2009-06-06 19:03:23 +0100789 return 0;
790}
Mark Brown42aa3412009-03-01 19:21:10 +0000791
Mark Brown38357ab2009-06-06 19:03:23 +0100792/* Insert a widget in order into a DAPM power sequence. */
793static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
794 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +0000795 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +0100796{
797 struct snd_soc_dapm_widget *w;
798
799 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +0000800 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +0100801 list_add_tail(&new_widget->power_list, &w->power_list);
802 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000803 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100804
Mark Brown38357ab2009-06-06 19:03:23 +0100805 list_add_tail(&new_widget->power_list, list);
806}
Mark Brown42aa3412009-03-01 19:21:10 +0000807
Mark Brown68f89ad2010-11-03 23:51:49 -0400808static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
809 struct snd_soc_dapm_widget *w, int event)
810{
811 struct snd_soc_card *card = dapm->card;
812 const char *ev_name;
813 int power, ret;
814
815 switch (event) {
816 case SND_SOC_DAPM_PRE_PMU:
817 ev_name = "PRE_PMU";
818 power = 1;
819 break;
820 case SND_SOC_DAPM_POST_PMU:
821 ev_name = "POST_PMU";
822 power = 1;
823 break;
824 case SND_SOC_DAPM_PRE_PMD:
825 ev_name = "PRE_PMD";
826 power = 0;
827 break;
828 case SND_SOC_DAPM_POST_PMD:
829 ev_name = "POST_PMD";
830 power = 0;
831 break;
832 default:
833 BUG();
834 return;
835 }
836
837 if (w->power != power)
838 return;
839
840 if (w->event && (w->event_flags & event)) {
841 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
842 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -0400843 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400844 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -0400845 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400846 if (ret < 0)
847 pr_err("%s: %s event failed: %d\n",
848 ev_name, w->name, ret);
849 }
850}
851
Mark Brownb22ead22009-06-07 12:51:26 +0100852/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200853static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +0100854 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100855{
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200856 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -0400857 struct snd_soc_dapm_widget *w;
858 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +0100859 unsigned int value = 0;
860 unsigned int mask = 0;
861 unsigned int cur_mask;
862
863 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
864 power_list)->reg;
865
866 list_for_each_entry(w, pending, power_list) {
867 cur_mask = 1 << w->shift;
868 BUG_ON(reg != w->reg);
869
870 if (w->invert)
871 power = !w->power;
872 else
873 power = w->power;
874
875 mask |= cur_mask;
876 if (power)
877 value |= cur_mask;
878
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200879 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +0100880 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
881 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100882
Mark Brown68f89ad2010-11-03 23:51:49 -0400883 /* Check for events */
884 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
885 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100886 }
887
Mark Brown81628102009-06-07 13:21:24 +0100888 if (reg >= 0) {
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200889 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +0100890 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200891 value, mask, reg, card->pop_time);
892 pop_wait(card->pop_time);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200893 snd_soc_update_bits(dapm->codec, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +0100894 }
895
896 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -0400897 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
898 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +0000899 }
Mark Brown42aa3412009-03-01 19:21:10 +0000900}
901
Mark Brownb22ead22009-06-07 12:51:26 +0100902/* Apply a DAPM power sequence.
903 *
904 * We walk over a pre-sorted list of widgets to apply power to. In
905 * order to minimise the number of writes to the device required
906 * multiple widgets will be updated in a single write where possible.
907 * Currently anything that requires more than a single write is not
908 * handled.
909 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200910static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +0000911 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +0100912{
913 struct snd_soc_dapm_widget *w, *n;
914 LIST_HEAD(pending);
915 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +0000916 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +0100917 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200918 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +0000919 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +0000920 int *sort;
921
922 if (power_up)
923 sort = dapm_up_seq;
924 else
925 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +0100926
Mark Brownb22ead22009-06-07 12:51:26 +0100927 list_for_each_entry_safe(w, n, list, power_list) {
928 ret = 0;
929
930 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200931 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +0000932 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +0100933 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200934 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +0100935
Mark Brown474b62d2011-01-18 16:14:44 +0000936 if (cur_dapm && cur_dapm->seq_notifier) {
937 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
938 if (sort[i] == cur_sort)
939 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +0000940 i,
941 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +0000942 }
943
Mark Brownb22ead22009-06-07 12:51:26 +0100944 INIT_LIST_HEAD(&pending);
945 cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +0000946 cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +0100947 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200948 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +0100949 }
950
Mark Brown163cac02009-06-07 10:12:52 +0100951 switch (w->id) {
952 case snd_soc_dapm_pre:
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_PRE_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_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +0100963 break;
964
965 case snd_soc_dapm_post:
966 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100967 list_for_each_entry_safe_continue(w, n, list,
968 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100969
Mark Brownb22ead22009-06-07 12:51:26 +0100970 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100971 ret = w->event(w,
972 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100973 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100974 ret = w->event(w,
975 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100976 break;
977
Mark Brown163cac02009-06-07 10:12:52 +0100978 default:
Mark Brown81628102009-06-07 13:21:24 +0100979 /* Queue it up for application */
980 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +0000981 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +0100982 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200983 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +0100984 list_move(&w->power_list, &pending);
985 break;
Mark Brown163cac02009-06-07 10:12:52 +0100986 }
Mark Brownb22ead22009-06-07 12:51:26 +0100987
988 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200989 dev_err(w->dapm->dev,
990 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +0100991 }
Mark Brownb22ead22009-06-07 12:51:26 +0100992
993 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +0000994 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +0000995
996 if (cur_dapm && cur_dapm->seq_notifier) {
997 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
998 if (sort[i] == cur_sort)
999 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001000 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001001 }
Mark Brown163cac02009-06-07 10:12:52 +01001002}
1003
Mark Brown97404f22010-12-14 16:13:57 +00001004static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1005{
1006 struct snd_soc_dapm_update *update = dapm->update;
1007 struct snd_soc_dapm_widget *w;
1008 int ret;
1009
1010 if (!update)
1011 return;
1012
1013 w = update->widget;
1014
1015 if (w->event &&
1016 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1017 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1018 if (ret != 0)
1019 pr_err("%s DAPM pre-event failed: %d\n",
1020 w->name, ret);
1021 }
1022
1023 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1024 update->val);
1025 if (ret < 0)
1026 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1027
1028 if (w->event &&
1029 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1030 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1031 if (ret != 0)
1032 pr_err("%s DAPM post-event failed: %d\n",
1033 w->name, ret);
1034 }
1035}
1036
Mark Brown9d0624a2011-02-18 11:49:43 -08001037/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1038 * they're changing state.
1039 */
1040static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1041{
1042 struct snd_soc_dapm_context *d = data;
1043 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001044
Mark Brown9d0624a2011-02-18 11:49:43 -08001045 if (d->dev_power && d->bias_level == SND_SOC_BIAS_OFF) {
1046 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1047 if (ret != 0)
1048 dev_err(d->dev,
1049 "Failed to turn on bias: %d\n", ret);
1050 }
1051
1052 /* If we're changing to all on or all off then prepare */
1053 if ((d->dev_power && d->bias_level == SND_SOC_BIAS_STANDBY) ||
1054 (!d->dev_power && d->bias_level == SND_SOC_BIAS_ON)) {
1055 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1056 if (ret != 0)
1057 dev_err(d->dev,
1058 "Failed to prepare bias: %d\n", ret);
1059 }
1060}
1061
1062/* Async callback run prior to DAPM sequences - brings to their final
1063 * state.
1064 */
1065static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1066{
1067 struct snd_soc_dapm_context *d = data;
1068 int ret;
1069
1070 /* If we just powered the last thing off drop to standby bias */
1071 if (d->bias_level == SND_SOC_BIAS_PREPARE && !d->dev_power) {
1072 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1073 if (ret != 0)
1074 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1075 ret);
1076 }
1077
1078 /* If we're in standby and can support bias off then do that */
1079 if (d->bias_level == SND_SOC_BIAS_STANDBY && d->idle_bias_off) {
1080 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1081 if (ret != 0)
1082 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1083 }
1084
1085 /* If we just powered up then move to active bias */
1086 if (d->bias_level == SND_SOC_BIAS_PREPARE && d->dev_power) {
1087 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1088 if (ret != 0)
1089 dev_err(d->dev, "Failed to apply active bias: %d\n",
1090 ret);
1091 }
1092}
Mark Brown97404f22010-12-14 16:13:57 +00001093
Mark Brown42aa3412009-03-01 19:21:10 +00001094/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001095 * Scan each dapm widget for complete audio path.
1096 * A complete path is a route that has valid endpoints i.e.:-
1097 *
1098 * o DAC to output pin.
1099 * o Input Pin to ADC.
1100 * o Input pin to Output pin (bypass, sidetone)
1101 * o DAC to ADC (loopback).
1102 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001103static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001104{
Mark Brown12ea2c72011-03-02 18:17:32 +00001105 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001106 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001107 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001108 LIST_HEAD(up_list);
1109 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001110 LIST_HEAD(async_domain);
Mark Brown38357ab2009-06-06 19:03:23 +01001111 int power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001112
Mark Brown84e90932010-11-04 00:07:02 -04001113 trace_snd_soc_dapm_start(card);
1114
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001115 list_for_each_entry(d, &card->dapm_list, list)
Jarkko Nikulaea77b942011-05-26 16:32:18 +03001116 if (d->n_widgets || d->codec == NULL)
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001117 d->dev_power = 0;
1118
Mark Brown6d3ddc82009-05-16 17:47:29 +01001119 /* Check which widgets we need to power and store them in
1120 * lists indicating if they should be powered up or down.
1121 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001122 list_for_each_entry(w, &card->widgets, list) {
Mark Brown6d3ddc82009-05-16 17:47:29 +01001123 switch (w->id) {
1124 case snd_soc_dapm_pre:
Mark Brown828a8422011-01-15 13:14:30 +00001125 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001126 break;
1127 case snd_soc_dapm_post:
Mark Brown828a8422011-01-15 13:14:30 +00001128 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001129 break;
1130
1131 default:
1132 if (!w->power_check)
1133 continue;
1134
Mark Brown99497882010-05-07 20:24:05 +01001135 if (!w->force)
Mark Brown50b6bce2009-11-23 13:11:53 +00001136 power = w->power_check(w);
Mark Brown99497882010-05-07 20:24:05 +01001137 else
1138 power = 1;
1139 if (power)
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001140 w->dapm->dev_power = 1;
Mark Brown452c5ea2009-05-17 21:41:23 +01001141
Mark Brown6d3ddc82009-05-16 17:47:29 +01001142 if (w->power == power)
1143 continue;
1144
Mark Brown84e90932010-11-04 00:07:02 -04001145 trace_snd_soc_dapm_widget_power(w, power);
1146
Mark Brown6d3ddc82009-05-16 17:47:29 +01001147 if (power)
Mark Brown828a8422011-01-15 13:14:30 +00001148 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001149 else
Mark Brown828a8422011-01-15 13:14:30 +00001150 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001151
1152 w->power = power;
1153 break;
1154 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001155 }
1156
Mark Brownb14b76a2009-08-17 11:55:38 +01001157 /* If there are no DAPM widgets then try to figure out power from the
1158 * event type.
1159 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001160 if (!dapm->n_widgets) {
Mark Brownb14b76a2009-08-17 11:55:38 +01001161 switch (event) {
1162 case SND_SOC_DAPM_STREAM_START:
1163 case SND_SOC_DAPM_STREAM_RESUME:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001164 dapm->dev_power = 1;
Mark Brownb14b76a2009-08-17 11:55:38 +01001165 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001166 case SND_SOC_DAPM_STREAM_STOP:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001167 dapm->dev_power = !!dapm->codec->active;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001168 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001169 case SND_SOC_DAPM_STREAM_SUSPEND:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001170 dapm->dev_power = 0;
Mark Brown50b6bce2009-11-23 13:11:53 +00001171 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001172 case SND_SOC_DAPM_STREAM_NOP:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001173 switch (dapm->bias_level) {
Mark Browna96ca332010-01-19 22:49:43 +00001174 case SND_SOC_BIAS_STANDBY:
1175 case SND_SOC_BIAS_OFF:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001176 dapm->dev_power = 0;
Mark Browna96ca332010-01-19 22:49:43 +00001177 break;
1178 default:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001179 dapm->dev_power = 1;
Mark Browna96ca332010-01-19 22:49:43 +00001180 break;
1181 }
Mark Brown50b6bce2009-11-23 13:11:53 +00001182 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001183 default:
1184 break;
1185 }
1186 }
1187
Mark Brown52ba67b2011-04-04 21:05:11 +09001188 /* Force all contexts in the card to the same bias state */
1189 power = 0;
1190 list_for_each_entry(d, &card->dapm_list, list)
1191 if (d->dev_power)
1192 power = 1;
1193 list_for_each_entry(d, &card->dapm_list, list)
1194 d->dev_power = power;
1195
1196
Mark Brown9d0624a2011-02-18 11:49:43 -08001197 /* Run all the bias changes in parallel */
1198 list_for_each_entry(d, &dapm->card->dapm_list, list)
1199 async_schedule_domain(dapm_pre_sequence_async, d,
1200 &async_domain);
1201 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001202
Mark Brown6d3ddc82009-05-16 17:47:29 +01001203 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001204 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001205
Mark Brown97404f22010-12-14 16:13:57 +00001206 dapm_widget_update(dapm);
1207
Mark Brown6d3ddc82009-05-16 17:47:29 +01001208 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001209 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001210
Mark Brown9d0624a2011-02-18 11:49:43 -08001211 /* Run all the bias changes in parallel */
1212 list_for_each_entry(d, &dapm->card->dapm_list, list)
1213 async_schedule_domain(dapm_post_sequence_async, d,
1214 &async_domain);
1215 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001216
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001217 pop_dbg(dapm->dev, card->pop_time,
1218 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001219 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001220
Mark Brown84e90932010-11-04 00:07:02 -04001221 trace_snd_soc_dapm_done(card);
1222
Mark Brown42aa3412009-03-01 19:21:10 +00001223 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001224}
1225
Mark Brown79fb9382009-08-21 16:38:13 +01001226#ifdef CONFIG_DEBUG_FS
1227static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1228{
1229 file->private_data = inode->i_private;
1230 return 0;
1231}
1232
1233static ssize_t dapm_widget_power_read_file(struct file *file,
1234 char __user *user_buf,
1235 size_t count, loff_t *ppos)
1236{
1237 struct snd_soc_dapm_widget *w = file->private_data;
1238 char *buf;
1239 int in, out;
1240 ssize_t ret;
1241 struct snd_soc_dapm_path *p = NULL;
1242
1243 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1244 if (!buf)
1245 return -ENOMEM;
1246
1247 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001248 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001249 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001250 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001251
Mark Brownd033c362009-12-04 15:25:56 +00001252 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001253 w->name, w->power ? "On" : "Off", in, out);
1254
Mark Brownd033c362009-12-04 15:25:56 +00001255 if (w->reg >= 0)
1256 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1257 " - R%d(0x%x) bit %d",
1258 w->reg, w->reg, w->shift);
1259
1260 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1261
Mark Brown3eef08b2009-09-14 16:49:00 +01001262 if (w->sname)
1263 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1264 w->sname,
1265 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001266
1267 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001268 if (p->connected && !p->connected(w, p->sink))
1269 continue;
1270
Mark Brown79fb9382009-08-21 16:38:13 +01001271 if (p->connect)
1272 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001273 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001274 p->name ? p->name : "static",
1275 p->source->name);
1276 }
1277 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001278 if (p->connected && !p->connected(w, p->sink))
1279 continue;
1280
Mark Brown79fb9382009-08-21 16:38:13 +01001281 if (p->connect)
1282 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001283 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001284 p->name ? p->name : "static",
1285 p->sink->name);
1286 }
1287
1288 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1289
1290 kfree(buf);
1291 return ret;
1292}
1293
1294static const struct file_operations dapm_widget_power_fops = {
1295 .open = dapm_widget_power_open_file,
1296 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001297 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001298};
1299
Mark Brownef49e4f2011-04-04 20:48:13 +09001300static int dapm_bias_open_file(struct inode *inode, struct file *file)
1301{
1302 file->private_data = inode->i_private;
1303 return 0;
1304}
1305
1306static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1307 size_t count, loff_t *ppos)
1308{
1309 struct snd_soc_dapm_context *dapm = file->private_data;
1310 char *level;
1311
1312 switch (dapm->bias_level) {
1313 case SND_SOC_BIAS_ON:
1314 level = "On\n";
1315 break;
1316 case SND_SOC_BIAS_PREPARE:
1317 level = "Prepare\n";
1318 break;
1319 case SND_SOC_BIAS_STANDBY:
1320 level = "Standby\n";
1321 break;
1322 case SND_SOC_BIAS_OFF:
1323 level = "Off\n";
1324 break;
1325 default:
1326 BUG();
1327 level = "Unknown\n";
1328 break;
1329 }
1330
1331 return simple_read_from_buffer(user_buf, count, ppos, level,
1332 strlen(level));
1333}
1334
1335static const struct file_operations dapm_bias_fops = {
1336 .open = dapm_bias_open_file,
1337 .read = dapm_bias_read_file,
1338 .llseek = default_llseek,
1339};
1340
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001341void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1342 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001343{
Mark Brown79fb9382009-08-21 16:38:13 +01001344 struct dentry *d;
1345
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001346 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1347
1348 if (!dapm->debugfs_dapm) {
1349 printk(KERN_WARNING
1350 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001351 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001352 }
Mark Brown79fb9382009-08-21 16:38:13 +01001353
Mark Brownef49e4f2011-04-04 20:48:13 +09001354 d = debugfs_create_file("bias_level", 0444,
1355 dapm->debugfs_dapm, dapm,
1356 &dapm_bias_fops);
1357 if (!d)
1358 dev_warn(dapm->dev,
1359 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001360}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001361
1362static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1363{
1364 struct snd_soc_dapm_context *dapm = w->dapm;
1365 struct dentry *d;
1366
1367 if (!dapm->debugfs_dapm || !w->name)
1368 return;
1369
1370 d = debugfs_create_file(w->name, 0444,
1371 dapm->debugfs_dapm, w,
1372 &dapm_widget_power_fops);
1373 if (!d)
1374 dev_warn(w->dapm->dev,
1375 "ASoC: Failed to create %s debugfs file\n",
1376 w->name);
1377}
1378
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001379static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1380{
1381 debugfs_remove_recursive(dapm->debugfs_dapm);
1382}
1383
Mark Brown79fb9382009-08-21 16:38:13 +01001384#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001385void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1386 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001387{
1388}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001389
1390static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1391{
1392}
1393
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001394static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1395{
1396}
1397
Mark Brown79fb9382009-08-21 16:38:13 +01001398#endif
1399
Richard Purdie2b97eab2006-10-06 18:32:18 +02001400/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001401static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001402 struct snd_kcontrol *kcontrol, int change,
1403 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001404{
1405 struct snd_soc_dapm_path *path;
1406 int found = 0;
1407
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001408 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001409 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001410 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001411 return -ENODEV;
1412
Mark Brown3a655772009-10-05 17:23:30 +01001413 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001414 return 0;
1415
1416 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001417 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001418 if (path->kcontrol != kcontrol)
1419 continue;
1420
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001421 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001422 continue;
1423
1424 found = 1;
1425 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001426 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001427 path->connect = 1; /* new connection */
1428 else
1429 path->connect = 0; /* old connection must be powered down */
1430 }
1431
Mark Brownb91b8fa2010-01-20 18:18:35 +00001432 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001433 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001434
1435 return 0;
1436}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001437
Milan plzik1b075e32008-01-10 14:39:46 +01001438/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001439static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001440 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001441{
1442 struct snd_soc_dapm_path *path;
1443 int found = 0;
1444
Milan plzik1b075e32008-01-10 14:39:46 +01001445 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001446 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001447 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001448 return -ENODEV;
1449
Richard Purdie2b97eab2006-10-06 18:32:18 +02001450 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001451 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001452 if (path->kcontrol != kcontrol)
1453 continue;
1454
1455 /* found, now check type */
1456 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001457 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001458 break;
1459 }
1460
Mark Brownb91b8fa2010-01-20 18:18:35 +00001461 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001462 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001463
1464 return 0;
1465}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001466
1467/* show dapm widget status in sys fs */
1468static ssize_t dapm_widget_show(struct device *dev,
1469 struct device_attribute *attr, char *buf)
1470{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001471 struct snd_soc_pcm_runtime *rtd =
1472 container_of(dev, struct snd_soc_pcm_runtime, dev);
1473 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001474 struct snd_soc_dapm_widget *w;
1475 int count = 0;
1476 char *state = "not set";
1477
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001478 list_for_each_entry(w, &codec->card->widgets, list) {
1479 if (w->dapm != &codec->dapm)
1480 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001481
1482 /* only display widgets that burnm power */
1483 switch (w->id) {
1484 case snd_soc_dapm_hp:
1485 case snd_soc_dapm_mic:
1486 case snd_soc_dapm_spk:
1487 case snd_soc_dapm_line:
1488 case snd_soc_dapm_micbias:
1489 case snd_soc_dapm_dac:
1490 case snd_soc_dapm_adc:
1491 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001492 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001493 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001494 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001495 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001496 if (w->name)
1497 count += sprintf(buf + count, "%s: %s\n",
1498 w->name, w->power ? "On":"Off");
1499 break;
1500 default:
1501 break;
1502 }
1503 }
1504
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001505 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001506 case SND_SOC_BIAS_ON:
1507 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001508 break;
Mark Brown0be98982008-05-19 12:31:28 +02001509 case SND_SOC_BIAS_PREPARE:
1510 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001511 break;
Mark Brown0be98982008-05-19 12:31:28 +02001512 case SND_SOC_BIAS_STANDBY:
1513 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001514 break;
Mark Brown0be98982008-05-19 12:31:28 +02001515 case SND_SOC_BIAS_OFF:
1516 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001517 break;
1518 }
1519 count += sprintf(buf + count, "PM State: %s\n", state);
1520
1521 return count;
1522}
1523
1524static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1525
1526int snd_soc_dapm_sys_add(struct device *dev)
1527{
Troy Kisky12ef1932008-10-13 17:42:14 -07001528 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001529}
1530
1531static void snd_soc_dapm_sys_remove(struct device *dev)
1532{
Mark Brownaef90842009-05-16 17:53:16 +01001533 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001534}
1535
1536/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001537static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001538{
1539 struct snd_soc_dapm_widget *w, *next_w;
1540 struct snd_soc_dapm_path *p, *next_p;
1541
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001542 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1543 if (w->dapm != dapm)
1544 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001545 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001546 /*
1547 * remove source and sink paths associated to this widget.
1548 * While removing the path, remove reference to it from both
1549 * source and sink widgets so that path is removed only once.
1550 */
1551 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1552 list_del(&p->list_sink);
1553 list_del(&p->list_source);
1554 list_del(&p->list);
1555 kfree(p->long_name);
1556 kfree(p);
1557 }
1558 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1559 list_del(&p->list_sink);
1560 list_del(&p->list_source);
1561 list_del(&p->list);
1562 kfree(p->long_name);
1563 kfree(p);
1564 }
Stephen Warrenfad59882011-04-28 17:37:59 -06001565 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001566 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001567 kfree(w);
1568 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001569}
1570
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001571static struct snd_soc_dapm_widget *dapm_find_widget(
1572 struct snd_soc_dapm_context *dapm, const char *pin,
1573 bool search_other_contexts)
1574{
1575 struct snd_soc_dapm_widget *w;
1576 struct snd_soc_dapm_widget *fallback = NULL;
1577
1578 list_for_each_entry(w, &dapm->card->widgets, list) {
1579 if (!strcmp(w->name, pin)) {
1580 if (w->dapm == dapm)
1581 return w;
1582 else
1583 fallback = w;
1584 }
1585 }
1586
1587 if (search_other_contexts)
1588 return fallback;
1589
1590 return NULL;
1591}
1592
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001593static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001594 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001595{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001596 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01001597
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001598 if (!w) {
1599 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1600 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01001601 }
1602
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001603 w->connected = status;
1604 if (status == 0)
1605 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09001606
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001607 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001608}
1609
Richard Purdie2b97eab2006-10-06 18:32:18 +02001610/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001611 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001612 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001613 *
1614 * Walks all dapm audio paths and powers widgets according to their
1615 * stream or path usage.
1616 *
1617 * Returns 0 for success.
1618 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001619int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001620{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001621 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001622}
Liam Girdwooda5302182008-07-07 13:35:17 +01001623EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001624
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001625static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001626 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001627{
1628 struct snd_soc_dapm_path *path;
1629 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001630 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001631 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001632 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001633 const char *source;
1634 char prefixed_sink[80];
1635 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001636 int ret = 0;
1637
Mark Brown88e8b9a2011-03-02 18:18:24 +00001638 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001639 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1640 dapm->codec->name_prefix, route->sink);
1641 sink = prefixed_sink;
1642 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1643 dapm->codec->name_prefix, route->source);
1644 source = prefixed_source;
1645 } else {
1646 sink = route->sink;
1647 source = route->source;
1648 }
1649
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001650 /*
1651 * find src and dest widgets over all widgets but favor a widget from
1652 * current DAPM context
1653 */
1654 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001655 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001656 wtsink = w;
1657 if (w->dapm == dapm)
1658 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001659 continue;
1660 }
1661 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001662 wtsource = w;
1663 if (w->dapm == dapm)
1664 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001665 }
1666 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001667 /* use widget from another DAPM context if not found from this */
1668 if (!wsink)
1669 wsink = wtsink;
1670 if (!wsource)
1671 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001672
1673 if (wsource == NULL || wsink == NULL)
1674 return -ENODEV;
1675
1676 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1677 if (!path)
1678 return -ENOMEM;
1679
1680 path->source = wsource;
1681 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001682 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001683 INIT_LIST_HEAD(&path->list);
1684 INIT_LIST_HEAD(&path->list_source);
1685 INIT_LIST_HEAD(&path->list_sink);
1686
1687 /* check for external widgets */
1688 if (wsink->id == snd_soc_dapm_input) {
1689 if (wsource->id == snd_soc_dapm_micbias ||
1690 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001691 wsource->id == snd_soc_dapm_line ||
1692 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001693 wsink->ext = 1;
1694 }
1695 if (wsource->id == snd_soc_dapm_output) {
1696 if (wsink->id == snd_soc_dapm_spk ||
1697 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001698 wsink->id == snd_soc_dapm_line ||
1699 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001700 wsource->ext = 1;
1701 }
1702
1703 /* connect static paths */
1704 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001705 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001706 list_add(&path->list_sink, &wsink->sources);
1707 list_add(&path->list_source, &wsource->sinks);
1708 path->connect = 1;
1709 return 0;
1710 }
1711
1712 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08001713 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001714 case snd_soc_dapm_adc:
1715 case snd_soc_dapm_dac:
1716 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001717 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001718 case snd_soc_dapm_input:
1719 case snd_soc_dapm_output:
1720 case snd_soc_dapm_micbias:
1721 case snd_soc_dapm_vmid:
1722 case snd_soc_dapm_pre:
1723 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001724 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001725 case snd_soc_dapm_aif_in:
1726 case snd_soc_dapm_aif_out:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001727 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001728 list_add(&path->list_sink, &wsink->sources);
1729 list_add(&path->list_source, &wsource->sinks);
1730 path->connect = 1;
1731 return 0;
1732 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001733 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001734 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001735 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06001736 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001737 if (ret != 0)
1738 goto err;
1739 break;
1740 case snd_soc_dapm_switch:
1741 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001742 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001743 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001744 if (ret != 0)
1745 goto err;
1746 break;
1747 case snd_soc_dapm_hp:
1748 case snd_soc_dapm_mic:
1749 case snd_soc_dapm_line:
1750 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001751 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001752 list_add(&path->list_sink, &wsink->sources);
1753 list_add(&path->list_source, &wsource->sinks);
1754 path->connect = 0;
1755 return 0;
1756 }
1757 return 0;
1758
1759err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001760 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1761 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001762 kfree(path);
1763 return ret;
1764}
Mark Brown105f1c22008-05-13 14:52:19 +02001765
1766/**
Mark Brown105f1c22008-05-13 14:52:19 +02001767 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001768 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02001769 * @route: audio routes
1770 * @num: number of routes
1771 *
1772 * Connects 2 dapm widgets together via a named audio path. The sink is
1773 * the widget receiving the audio signal, whilst the source is the sender
1774 * of the audio signal.
1775 *
1776 * Returns 0 for success else error. On error all resources can be freed
1777 * with a call to snd_soc_card_free().
1778 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001779int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02001780 const struct snd_soc_dapm_route *route, int num)
1781{
1782 int i, ret;
1783
1784 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001785 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001786 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001787 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1788 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02001789 return ret;
1790 }
1791 route++;
1792 }
1793
1794 return 0;
1795}
1796EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1797
1798/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001799 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001800 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001801 *
1802 * Checks the codec for any new dapm widgets and creates them if found.
1803 *
1804 * Returns 0 for success.
1805 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001806int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001807{
1808 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00001809 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001810
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001811 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001812 {
1813 if (w->new)
1814 continue;
1815
Stephen Warrenfad59882011-04-28 17:37:59 -06001816 if (w->num_kcontrols) {
1817 w->kcontrols = kzalloc(w->num_kcontrols *
1818 sizeof(struct snd_kcontrol *),
1819 GFP_KERNEL);
1820 if (!w->kcontrols)
1821 return -ENOMEM;
1822 }
1823
Richard Purdie2b97eab2006-10-06 18:32:18 +02001824 switch(w->id) {
1825 case snd_soc_dapm_switch:
1826 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001827 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001828 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001829 dapm_new_mixer(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001830 break;
1831 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001832 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001833 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001834 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001835 dapm_new_mux(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001836 break;
1837 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01001838 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01001839 w->power_check = dapm_adc_check_power;
1840 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001841 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01001842 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01001843 w->power_check = dapm_dac_check_power;
1844 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001845 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001846 case snd_soc_dapm_out_drv:
Mark Brownb75576d2009-04-20 17:56:13 +01001847 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001848 dapm_new_pga(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001849 break;
1850 case snd_soc_dapm_input:
1851 case snd_soc_dapm_output:
1852 case snd_soc_dapm_micbias:
1853 case snd_soc_dapm_spk:
1854 case snd_soc_dapm_hp:
1855 case snd_soc_dapm_mic:
1856 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001857 w->power_check = dapm_generic_check_power;
1858 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001859 case snd_soc_dapm_supply:
1860 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001861 case snd_soc_dapm_vmid:
1862 case snd_soc_dapm_pre:
1863 case snd_soc_dapm_post:
1864 break;
1865 }
Mark Brownb66a70d2011-02-09 18:04:11 +00001866
1867 /* Read the initial power state from the device */
1868 if (w->reg >= 0) {
1869 val = snd_soc_read(w->codec, w->reg);
1870 val &= 1 << w->shift;
1871 if (w->invert)
1872 val = !val;
1873
1874 if (val)
1875 w->power = 1;
1876 }
1877
Richard Purdie2b97eab2006-10-06 18:32:18 +02001878 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001879
1880 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001881 }
1882
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001883 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001884 return 0;
1885}
1886EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1887
1888/**
1889 * snd_soc_dapm_get_volsw - dapm mixer get callback
1890 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001891 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001892 *
1893 * Callback to get the value of a dapm mixer control.
1894 *
1895 * Returns 0 for success.
1896 */
1897int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1898 struct snd_ctl_elem_value *ucontrol)
1899{
Stephen Warrenfafd2172011-04-28 17:38:00 -06001900 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
1901 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01001902 struct soc_mixer_control *mc =
1903 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001904 unsigned int reg = mc->reg;
1905 unsigned int shift = mc->shift;
1906 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001907 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001908 unsigned int invert = mc->invert;
1909 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001910
Richard Purdie2b97eab2006-10-06 18:32:18 +02001911 ucontrol->value.integer.value[0] =
1912 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1913 if (shift != rshift)
1914 ucontrol->value.integer.value[1] =
1915 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1916 if (invert) {
1917 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001918 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001919 if (shift != rshift)
1920 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001921 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001922 }
1923
1924 return 0;
1925}
1926EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1927
1928/**
1929 * snd_soc_dapm_put_volsw - dapm mixer set callback
1930 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001931 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001932 *
1933 * Callback to set the value of a dapm mixer control.
1934 *
1935 * Returns 0 for success.
1936 */
1937int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1938 struct snd_ctl_elem_value *ucontrol)
1939{
Stephen Warrenfafd2172011-04-28 17:38:00 -06001940 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
1941 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
1942 struct snd_soc_codec *codec = widget->codec;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001943 struct soc_mixer_control *mc =
1944 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001945 unsigned int reg = mc->reg;
1946 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001947 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001948 unsigned int mask = (1 << fls(max)) - 1;
1949 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07001950 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00001951 int connect, change;
1952 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06001953 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001954
1955 val = (ucontrol->value.integer.value[0] & mask);
1956
1957 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001958 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07001959 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001960 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001961
Stephen Warrenfafd2172011-04-28 17:38:00 -06001962 if (val)
1963 /* new connection */
1964 connect = invert ? 0 : 1;
1965 else
1966 /* old connection must be powered down */
1967 connect = invert ? 1 : 0;
1968
1969 mutex_lock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001970
Stephen Warrene9cf7042011-01-27 14:54:05 -07001971 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00001972 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06001973 for (wi = 0; wi < wlist->num_widgets; wi++) {
1974 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00001975
Stephen Warrenfafd2172011-04-28 17:38:00 -06001976 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00001977
Stephen Warrenfafd2172011-04-28 17:38:00 -06001978 update.kcontrol = kcontrol;
1979 update.widget = widget;
1980 update.reg = reg;
1981 update.mask = mask;
1982 update.val = val;
1983 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00001984
Stephen Warrenfafd2172011-04-28 17:38:00 -06001985 dapm_mixer_update_power(widget, kcontrol, connect);
1986
1987 widget->dapm->update = NULL;
1988 }
Mark Brown283375c2009-12-07 18:09:03 +00001989 }
1990
Stephen Warrenfafd2172011-04-28 17:38:00 -06001991 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00001992 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001993}
1994EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1995
1996/**
1997 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1998 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001999 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002000 *
2001 * Callback to get the value of a dapm enumerated double mixer control.
2002 *
2003 * Returns 0 for success.
2004 */
2005int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2006 struct snd_ctl_elem_value *ucontrol)
2007{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002008 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2009 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002010 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002011 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002012
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002013 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002014 ;
2015 val = snd_soc_read(widget->codec, e->reg);
2016 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2017 if (e->shift_l != e->shift_r)
2018 ucontrol->value.enumerated.item[1] =
2019 (val >> e->shift_r) & (bitmask - 1);
2020
2021 return 0;
2022}
2023EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2024
2025/**
2026 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2027 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002028 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002029 *
2030 * Callback to set the value of a dapm enumerated double mixer control.
2031 *
2032 * Returns 0 for success.
2033 */
2034int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2035 struct snd_ctl_elem_value *ucontrol)
2036{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002037 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2038 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2039 struct snd_soc_codec *codec = widget->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002040 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002041 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002042 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00002043 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002044 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002045
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002046 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002047 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002048 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002049 return -EINVAL;
2050 mux = ucontrol->value.enumerated.item[0];
2051 val = mux << e->shift_l;
2052 mask = (bitmask - 1) << e->shift_l;
2053 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002054 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002055 return -EINVAL;
2056 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2057 mask |= (bitmask - 1) << e->shift_r;
2058 }
2059
Stephen Warrenfafd2172011-04-28 17:38:00 -06002060 mutex_lock(&codec->mutex);
2061
Mark Brown3a655772009-10-05 17:23:30 +01002062 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002063 if (change) {
2064 for (wi = 0; wi < wlist->num_widgets; wi++) {
2065 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002066
Stephen Warrenfafd2172011-04-28 17:38:00 -06002067 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002068
Stephen Warrenfafd2172011-04-28 17:38:00 -06002069 update.kcontrol = kcontrol;
2070 update.widget = widget;
2071 update.reg = e->reg;
2072 update.mask = mask;
2073 update.val = val;
2074 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002075
Stephen Warrenfafd2172011-04-28 17:38:00 -06002076 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002077
Stephen Warrenfafd2172011-04-28 17:38:00 -06002078 widget->dapm->update = NULL;
2079 }
2080 }
2081
2082 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002083 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002084}
2085EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2086
2087/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002088 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2089 * @kcontrol: mixer control
2090 * @ucontrol: control element information
2091 *
2092 * Returns 0 for success.
2093 */
2094int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2095 struct snd_ctl_elem_value *ucontrol)
2096{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002097 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2098 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002099
2100 ucontrol->value.enumerated.item[0] = widget->value;
2101
2102 return 0;
2103}
2104EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2105
2106/**
2107 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2108 * @kcontrol: mixer control
2109 * @ucontrol: control element information
2110 *
2111 * Returns 0 for success.
2112 */
2113int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2114 struct snd_ctl_elem_value *ucontrol)
2115{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002116 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2117 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2118 struct snd_soc_codec *codec = widget->codec;
Mark Brownd2b247a2009-10-06 15:21:04 +01002119 struct soc_enum *e =
2120 (struct soc_enum *)kcontrol->private_value;
2121 int change;
2122 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002123 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002124
2125 if (ucontrol->value.enumerated.item[0] >= e->max)
2126 return -EINVAL;
2127
Stephen Warrenfafd2172011-04-28 17:38:00 -06002128 mutex_lock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002129
2130 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002131 if (change) {
2132 for (wi = 0; wi < wlist->num_widgets; wi++) {
2133 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002134
Stephen Warrenfafd2172011-04-28 17:38:00 -06002135 widget->value = ucontrol->value.enumerated.item[0];
2136
2137 dapm_mux_update_power(widget, kcontrol, change,
2138 widget->value, e);
2139 }
2140 }
2141
2142 mutex_unlock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002143 return ret;
2144}
2145EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2146
2147/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002148 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2149 * callback
2150 * @kcontrol: mixer control
2151 * @ucontrol: control element information
2152 *
2153 * Callback to get the value of a dapm semi enumerated double mixer control.
2154 *
2155 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2156 * used for handling bitfield coded enumeration for example.
2157 *
2158 * Returns 0 for success.
2159 */
2160int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2161 struct snd_ctl_elem_value *ucontrol)
2162{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002163 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2164 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002165 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002166 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002167
2168 reg_val = snd_soc_read(widget->codec, e->reg);
2169 val = (reg_val >> e->shift_l) & e->mask;
2170 for (mux = 0; mux < e->max; mux++) {
2171 if (val == e->values[mux])
2172 break;
2173 }
2174 ucontrol->value.enumerated.item[0] = mux;
2175 if (e->shift_l != e->shift_r) {
2176 val = (reg_val >> e->shift_r) & e->mask;
2177 for (mux = 0; mux < e->max; mux++) {
2178 if (val == e->values[mux])
2179 break;
2180 }
2181 ucontrol->value.enumerated.item[1] = mux;
2182 }
2183
2184 return 0;
2185}
2186EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2187
2188/**
2189 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2190 * callback
2191 * @kcontrol: mixer control
2192 * @ucontrol: control element information
2193 *
2194 * Callback to set the value of a dapm semi enumerated double mixer control.
2195 *
2196 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2197 * used for handling bitfield coded enumeration for example.
2198 *
2199 * Returns 0 for success.
2200 */
2201int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2202 struct snd_ctl_elem_value *ucontrol)
2203{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002204 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2205 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2206 struct snd_soc_codec *codec = widget->codec;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002207 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002208 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002209 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002210 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002211 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002212
2213 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2214 return -EINVAL;
2215 mux = ucontrol->value.enumerated.item[0];
2216 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2217 mask = e->mask << e->shift_l;
2218 if (e->shift_l != e->shift_r) {
2219 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2220 return -EINVAL;
2221 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2222 mask |= e->mask << e->shift_r;
2223 }
2224
Stephen Warrenfafd2172011-04-28 17:38:00 -06002225 mutex_lock(&codec->mutex);
2226
Mark Brown3a655772009-10-05 17:23:30 +01002227 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002228 if (change) {
2229 for (wi = 0; wi < wlist->num_widgets; wi++) {
2230 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002231
Stephen Warrenfafd2172011-04-28 17:38:00 -06002232 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002233
Stephen Warrenfafd2172011-04-28 17:38:00 -06002234 update.kcontrol = kcontrol;
2235 update.widget = widget;
2236 update.reg = e->reg;
2237 update.mask = mask;
2238 update.val = val;
2239 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002240
Stephen Warrenfafd2172011-04-28 17:38:00 -06002241 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002242
Stephen Warrenfafd2172011-04-28 17:38:00 -06002243 widget->dapm->update = NULL;
2244 }
2245 }
2246
2247 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002248 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002249}
2250EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2251
2252/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002253 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2254 *
2255 * @kcontrol: mixer control
2256 * @uinfo: control element information
2257 *
2258 * Callback to provide information about a pin switch control.
2259 */
2260int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2261 struct snd_ctl_elem_info *uinfo)
2262{
2263 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2264 uinfo->count = 1;
2265 uinfo->value.integer.min = 0;
2266 uinfo->value.integer.max = 1;
2267
2268 return 0;
2269}
2270EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2271
2272/**
2273 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2274 *
2275 * @kcontrol: mixer control
2276 * @ucontrol: Value
2277 */
2278int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2279 struct snd_ctl_elem_value *ucontrol)
2280{
2281 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2282 const char *pin = (const char *)kcontrol->private_value;
2283
2284 mutex_lock(&codec->mutex);
2285
2286 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002287 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002288
2289 mutex_unlock(&codec->mutex);
2290
2291 return 0;
2292}
2293EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2294
2295/**
2296 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2297 *
2298 * @kcontrol: mixer control
2299 * @ucontrol: Value
2300 */
2301int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2302 struct snd_ctl_elem_value *ucontrol)
2303{
2304 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2305 const char *pin = (const char *)kcontrol->private_value;
2306
2307 mutex_lock(&codec->mutex);
2308
2309 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002310 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002311 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002312 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002313
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002314 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002315
2316 mutex_unlock(&codec->mutex);
2317
2318 return 0;
2319}
2320EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2321
2322/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002323 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002324 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002325 * @widget: widget template
2326 *
2327 * Creates a new dapm control based upon the template.
2328 *
2329 * Returns 0 for success else error.
2330 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002331int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002332 const struct snd_soc_dapm_widget *widget)
2333{
2334 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002335 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002336
2337 if ((w = dapm_cnew_widget(widget)) == NULL)
2338 return -ENOMEM;
2339
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002340 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002341 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002342 name_len += 1 + strlen(dapm->codec->name_prefix);
2343 w->name = kmalloc(name_len, GFP_KERNEL);
2344 if (w->name == NULL) {
2345 kfree(w);
2346 return -ENOMEM;
2347 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002348 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002349 snprintf(w->name, name_len, "%s %s",
2350 dapm->codec->name_prefix, widget->name);
2351 else
2352 snprintf(w->name, name_len, "%s", widget->name);
2353
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002354 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002355 w->dapm = dapm;
2356 w->codec = dapm->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002357 INIT_LIST_HEAD(&w->sources);
2358 INIT_LIST_HEAD(&w->sinks);
2359 INIT_LIST_HEAD(&w->list);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002360 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002361
2362 /* machine layer set ups unconnected pins and insertions */
2363 w->connected = 1;
2364 return 0;
2365}
2366EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2367
2368/**
Mark Brown4ba13272008-05-13 14:51:19 +02002369 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002370 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002371 * @widget: widget array
2372 * @num: number of widgets
2373 *
2374 * Creates new DAPM controls based upon the templates.
2375 *
2376 * Returns 0 for success else error.
2377 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002378int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002379 const struct snd_soc_dapm_widget *widget,
2380 int num)
2381{
2382 int i, ret;
2383
2384 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002385 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002386 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002387 dev_err(dapm->dev,
2388 "ASoC: Failed to create DAPM control %s: %d\n",
2389 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002390 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002391 }
Mark Brown4ba13272008-05-13 14:51:19 +02002392 widget++;
2393 }
2394 return 0;
2395}
2396EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2397
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002398static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002399 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002400{
2401 struct snd_soc_dapm_widget *w;
2402
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002403 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002404 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002405 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002406 continue;
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002407 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2408 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002409 if (strstr(w->sname, stream)) {
2410 switch(event) {
2411 case SND_SOC_DAPM_STREAM_START:
2412 w->active = 1;
2413 break;
2414 case SND_SOC_DAPM_STREAM_STOP:
2415 w->active = 0;
2416 break;
2417 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002418 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002419 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002420 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2421 break;
2422 }
2423 }
2424 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002425
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002426 dapm_power_widgets(dapm, event);
2427}
2428
2429/**
2430 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2431 * @rtd: PCM runtime data
2432 * @stream: stream name
2433 * @event: stream event
2434 *
2435 * Sends a stream event to the dapm core. The core then makes any
2436 * necessary widget power changes.
2437 *
2438 * Returns 0 for success else error.
2439 */
2440int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2441 const char *stream, int event)
2442{
2443 struct snd_soc_codec *codec = rtd->codec;
2444
2445 if (stream == NULL)
2446 return 0;
2447
2448 mutex_lock(&codec->mutex);
2449 soc_dapm_stream_event(&codec->dapm, stream, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002450 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002451 return 0;
2452}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002453
2454/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002455 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002456 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002457 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002458 *
Mark Brown74b8f952009-06-06 11:26:15 +01002459 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002460 * a valid audio route and active audio stream.
2461 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2462 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002463 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002464int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002465{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002466 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002467}
Liam Girdwooda5302182008-07-07 13:35:17 +01002468EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002469
2470/**
Mark Brownda341832010-03-15 19:23:37 +00002471 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002472 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002473 * @pin: pin name
2474 *
2475 * Enables input/output pin regardless of any other state. This is
2476 * intended for use with microphone bias supplies used in microphone
2477 * jack detection.
2478 *
2479 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2480 * do any widget power switching.
2481 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002482int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2483 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002484{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002485 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00002486
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002487 if (!w) {
2488 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2489 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00002490 }
2491
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002492 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2493 w->connected = 1;
2494 w->force = 1;
Mark Brown0d867332011-04-06 11:38:14 +09002495
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002496 return 0;
Mark Brownda341832010-03-15 19:23:37 +00002497}
2498EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2499
2500/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002501 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002502 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002503 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002504 *
Mark Brown74b8f952009-06-06 11:26:15 +01002505 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002506 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2507 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002508 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002509int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2510 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002511{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002512 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002513}
2514EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2515
2516/**
Mark Brown5817b522008-09-24 11:23:11 +01002517 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002518 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002519 * @pin: pin name
2520 *
2521 * Marks the specified pin as being not connected, disabling it along
2522 * any parent or child widgets. At present this is identical to
2523 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2524 * additional things such as disabling controls which only affect
2525 * paths through the pin.
2526 *
2527 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2528 * do any widget power switching.
2529 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002530int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002531{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002532 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002533}
2534EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2535
2536/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002537 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002538 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002539 * @pin: audio signal pin endpoint (or start point)
2540 *
2541 * Get audio pin status - connected or disconnected.
2542 *
2543 * Returns 1 for connected otherwise 0.
2544 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002545int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2546 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002547{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002548 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002549
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002550 if (w)
2551 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06002552
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002553 return 0;
2554}
Liam Girdwooda5302182008-07-07 13:35:17 +01002555EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002556
2557/**
Mark Brown1547aba2010-05-07 21:11:40 +01002558 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002559 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01002560 * @pin: audio signal pin endpoint (or start point)
2561 *
2562 * Mark the given endpoint or pin as ignoring suspend. When the
2563 * system is disabled a path between two endpoints flagged as ignoring
2564 * suspend will not be disabled. The path must already be enabled via
2565 * normal means at suspend time, it will not be turned on if it was not
2566 * already enabled.
2567 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002568int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2569 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01002570{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002571 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01002572
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002573 if (!w) {
2574 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2575 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01002576 }
2577
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002578 w->ignore_suspend = 1;
2579
2580 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01002581}
2582EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2583
2584/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002585 * snd_soc_dapm_free - free dapm resources
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002586 * @card: SoC device
Richard Purdie2b97eab2006-10-06 18:32:18 +02002587 *
2588 * Free all dapm widgets and resources.
2589 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002590void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002591{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002592 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002593 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002594 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02002595 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002596}
2597EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2598
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002599static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01002600{
Mark Brown51737472009-06-22 13:16:51 +01002601 struct snd_soc_dapm_widget *w;
2602 LIST_HEAD(down_list);
2603 int powerdown = 0;
2604
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002605 list_for_each_entry(w, &dapm->card->widgets, list) {
2606 if (w->dapm != dapm)
2607 continue;
Mark Brown51737472009-06-22 13:16:51 +01002608 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00002609 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002610 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002611 powerdown = 1;
2612 }
2613 }
2614
2615 /* If there were no widgets to power down we're already in
2616 * standby.
2617 */
2618 if (powerdown) {
Mark Browned5a4c42011-02-18 11:12:42 -08002619 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00002620 dapm_seq_run(dapm, &down_list, 0, false);
Mark Browned5a4c42011-02-18 11:12:42 -08002621 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01002622 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002623}
Mark Brown51737472009-06-22 13:16:51 +01002624
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002625/*
2626 * snd_soc_dapm_shutdown - callback for system shutdown
2627 */
2628void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2629{
2630 struct snd_soc_codec *codec;
2631
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002632 list_for_each_entry(codec, &card->codec_dev_list, list) {
2633 soc_dapm_shutdown_codec(&codec->dapm);
Mark Browned5a4c42011-02-18 11:12:42 -08002634 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002635 }
Mark Brown51737472009-06-22 13:16:51 +01002636}
2637
Richard Purdie2b97eab2006-10-06 18:32:18 +02002638/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002639MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002640MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2641MODULE_LICENSE("GPL");