blob: f362d1de78f3e7e70bf3283c6525d95142784941 [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>
35#include <linux/delay.h>
36#include <linux/pm.h>
37#include <linux/bitops.h>
38#include <linux/platform_device.h>
39#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020040#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020042#include <sound/core.h>
43#include <sound/pcm.h>
44#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020045#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020046#include <sound/initval.h>
47
Mark Brown84e90932010-11-04 00:07:02 -040048#include <trace/events/asoc.h>
49
Richard Purdie2b97eab2006-10-06 18:32:18 +020050/* dapm power sequences - make this per codec in the future */
51static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010052 [snd_soc_dapm_pre] = 0,
53 [snd_soc_dapm_supply] = 1,
54 [snd_soc_dapm_micbias] = 2,
Mark Brown010ff262009-08-17 17:39:22 +010055 [snd_soc_dapm_aif_in] = 3,
56 [snd_soc_dapm_aif_out] = 3,
57 [snd_soc_dapm_mic] = 4,
58 [snd_soc_dapm_mux] = 5,
59 [snd_soc_dapm_value_mux] = 5,
60 [snd_soc_dapm_dac] = 6,
61 [snd_soc_dapm_mixer] = 7,
62 [snd_soc_dapm_mixer_named_ctl] = 7,
63 [snd_soc_dapm_pga] = 8,
64 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060065 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010066 [snd_soc_dapm_hp] = 10,
67 [snd_soc_dapm_spk] = 10,
68 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020069};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000070
Richard Purdie2b97eab2006-10-06 18:32:18 +020071static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010072 [snd_soc_dapm_pre] = 0,
73 [snd_soc_dapm_adc] = 1,
74 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010075 [snd_soc_dapm_spk] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060076 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010077 [snd_soc_dapm_pga] = 4,
78 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010079 [snd_soc_dapm_mixer] = 5,
80 [snd_soc_dapm_dac] = 6,
81 [snd_soc_dapm_mic] = 7,
82 [snd_soc_dapm_micbias] = 8,
83 [snd_soc_dapm_mux] = 9,
84 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010085 [snd_soc_dapm_aif_in] = 10,
86 [snd_soc_dapm_aif_out] = 10,
87 [snd_soc_dapm_supply] = 11,
88 [snd_soc_dapm_post] = 12,
Richard Purdie2b97eab2006-10-06 18:32:18 +020089};
90
Troy Kisky12ef1932008-10-13 17:42:14 -070091static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +010092{
93 if (pop_time)
94 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
95}
96
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +020097static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +010098{
99 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200100 char *buf;
101
102 if (!pop_time)
103 return;
104
105 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
106 if (buf == NULL)
107 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100108
109 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200110 vsnprintf(buf, PAGE_SIZE, fmt, args);
111 dev_info(dev, buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100112 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200113
114 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100115}
116
Richard Purdie2b97eab2006-10-06 18:32:18 +0200117/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100118static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200119 const struct snd_soc_dapm_widget *_widget)
120{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100121 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200122}
123
Mark Brown452c5ea2009-05-17 21:41:23 +0100124/**
125 * snd_soc_dapm_set_bias_level - set the bias level for the system
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000126 * @card: audio device
Mark Brown452c5ea2009-05-17 21:41:23 +0100127 * @level: level to configure
128 *
129 * Configure the bias (power) levels for the SoC audio device.
130 *
131 * Returns 0 for success else error.
132 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000133static int snd_soc_dapm_set_bias_level(struct snd_soc_card *card,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200134 struct snd_soc_dapm_context *dapm,
135 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100136{
Mark Brown452c5ea2009-05-17 21:41:23 +0100137 int ret = 0;
138
Mark Brownf83fba82009-05-18 15:44:43 +0100139 switch (level) {
140 case SND_SOC_BIAS_ON:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200141 dev_dbg(dapm->dev, "Setting full bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100142 break;
143 case SND_SOC_BIAS_PREPARE:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200144 dev_dbg(dapm->dev, "Setting bias prepare\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100145 break;
146 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200147 dev_dbg(dapm->dev, "Setting standby bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100148 break;
149 case SND_SOC_BIAS_OFF:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200150 dev_dbg(dapm->dev, "Setting bias off\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100151 break;
152 default:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200153 dev_err(dapm->dev, "Setting invalid bias %d\n", level);
Mark Brownf83fba82009-05-18 15:44:43 +0100154 return -EINVAL;
155 }
156
Mark Brown84e90932010-11-04 00:07:02 -0400157 trace_snd_soc_bias_level_start(card, level);
158
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000159 if (card && card->set_bias_level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100160 ret = card->set_bias_level(card, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100161 if (ret == 0) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200162 if (dapm->codec && dapm->codec->driver->set_bias_level)
163 ret = dapm->codec->driver->set_bias_level(dapm->codec, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100164 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200165 dapm->bias_level = level;
Mark Brown474e09c2009-08-19 14:18:53 +0100166 }
Mark Brown1badabd2010-12-04 12:41:04 +0000167 if (ret == 0) {
168 if (card && card->set_bias_level_post)
169 ret = card->set_bias_level_post(card, level);
170 }
Mark Brown452c5ea2009-05-17 21:41:23 +0100171
Mark Brown84e90932010-11-04 00:07:02 -0400172 trace_snd_soc_bias_level_done(card, level);
173
Mark Brown452c5ea2009-05-17 21:41:23 +0100174 return ret;
175}
176
Richard Purdie2b97eab2006-10-06 18:32:18 +0200177/* set up initial codec paths */
178static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
179 struct snd_soc_dapm_path *p, int i)
180{
181 switch (w->id) {
182 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000183 case snd_soc_dapm_mixer:
184 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200185 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100186 struct soc_mixer_control *mc = (struct soc_mixer_control *)
187 w->kcontrols[i].private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -0400188 unsigned int reg = mc->reg;
189 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100190 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -0400191 unsigned int mask = (1 << fls(max)) - 1;
192 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200193
194 val = snd_soc_read(w->codec, reg);
195 val = (val >> shift) & mask;
196
197 if ((invert && !val) || (!invert && val))
198 p->connect = 1;
199 else
200 p->connect = 0;
201 }
202 break;
203 case snd_soc_dapm_mux: {
204 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
205 int val, item, bitmask;
206
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +0100207 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200208 ;
209 val = snd_soc_read(w->codec, e->reg);
210 item = (val >> e->shift_l) & (bitmask - 1);
211
212 p->connect = 0;
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +0100213 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200214 if (!(strcmp(p->name, e->texts[i])) && item == i)
215 p->connect = 1;
216 }
217 }
218 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200219 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200220 struct soc_enum *e = (struct soc_enum *)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200221 w->kcontrols[i].private_value;
222 int val, item;
223
224 val = snd_soc_read(w->codec, e->reg);
225 val = (val >> e->shift_l) & e->mask;
226 for (item = 0; item < e->max; item++) {
227 if (val == e->values[item])
228 break;
229 }
230
231 p->connect = 0;
232 for (i = 0; i < e->max; i++) {
233 if (!(strcmp(p->name, e->texts[i])) && item == i)
234 p->connect = 1;
235 }
236 }
237 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200238 /* does not effect routing - always connected */
239 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600240 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200241 case snd_soc_dapm_output:
242 case snd_soc_dapm_adc:
243 case snd_soc_dapm_input:
244 case snd_soc_dapm_dac:
245 case snd_soc_dapm_micbias:
246 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100247 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100248 case snd_soc_dapm_aif_in:
249 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200250 p->connect = 1;
251 break;
252 /* does effect routing - dynamically connected */
253 case snd_soc_dapm_hp:
254 case snd_soc_dapm_mic:
255 case snd_soc_dapm_spk:
256 case snd_soc_dapm_line:
257 case snd_soc_dapm_pre:
258 case snd_soc_dapm_post:
259 p->connect = 0;
260 break;
261 }
262}
263
Mark Brown74b8f952009-06-06 11:26:15 +0100264/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200265static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200266 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
267 struct snd_soc_dapm_path *path, const char *control_name,
268 const struct snd_kcontrol_new *kcontrol)
269{
270 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
271 int i;
272
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +0100273 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200274 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200275 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200276 list_add(&path->list_sink, &dest->sources);
277 list_add(&path->list_source, &src->sinks);
278 path->name = (char*)e->texts[i];
279 dapm_set_path_status(dest, path, 0);
280 return 0;
281 }
282 }
283
284 return -ENODEV;
285}
286
Mark Brown74b8f952009-06-06 11:26:15 +0100287/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200288static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200289 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
290 struct snd_soc_dapm_path *path, const char *control_name)
291{
292 int i;
293
294 /* search for mixer kcontrol */
295 for (i = 0; i < dest->num_kcontrols; i++) {
296 if (!strcmp(control_name, dest->kcontrols[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200297 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200298 list_add(&path->list_sink, &dest->sources);
299 list_add(&path->list_source, &src->sinks);
300 path->name = dest->kcontrols[i].name;
301 dapm_set_path_status(dest, path, i);
302 return 0;
303 }
304 }
305 return -ENODEV;
306}
307
308/* update dapm codec register bits */
309static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
310{
311 int change, power;
Daniel Ribeiro46f58222009-06-07 02:49:11 -0300312 unsigned int old, new;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200313 struct snd_soc_codec *codec = widget->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200314 struct snd_soc_dapm_context *dapm = widget->dapm;
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200315 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200316
317 /* check for valid widgets */
318 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
319 widget->id == snd_soc_dapm_output ||
320 widget->id == snd_soc_dapm_hp ||
321 widget->id == snd_soc_dapm_mic ||
322 widget->id == snd_soc_dapm_line ||
323 widget->id == snd_soc_dapm_spk)
324 return 0;
325
326 power = widget->power;
327 if (widget->invert)
328 power = (power ? 0:1);
329
330 old = snd_soc_read(codec, widget->reg);
331 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
332
333 change = old != new;
334 if (change) {
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200335 pop_dbg(dapm->dev, card->pop_time,
336 "pop test %s : %s in %d ms\n",
Troy Kisky12ef1932008-10-13 17:42:14 -0700337 widget->name, widget->power ? "on" : "off",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200338 card->pop_time);
339 pop_wait(card->pop_time);
Mark Brown69224712010-03-03 14:57:09 +0000340 snd_soc_write(codec, widget->reg, new);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200341 }
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200342 dev_dbg(dapm->dev, "reg %x old %x new %x change %d\n", widget->reg,
343 old, new, change);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200344 return change;
345}
346
Richard Purdie2b97eab2006-10-06 18:32:18 +0200347/* create new dapm mixer control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200348static int dapm_new_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200349 struct snd_soc_dapm_widget *w)
350{
351 int i, ret = 0;
Mark Brown219b93f2008-10-28 13:02:31 +0000352 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200353 struct snd_soc_dapm_path *path;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200354 struct snd_card *card = dapm->codec->card->snd_card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200355
356 /* add kcontrol */
357 for (i = 0; i < w->num_kcontrols; i++) {
358
359 /* match name */
360 list_for_each_entry(path, &w->sources, list_sink) {
361
362 /* mixer/mux paths name must match control name */
363 if (path->name != (char*)w->kcontrols[i].name)
364 continue;
365
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000366 /* add dapm control with long name.
367 * for dapm_mixer this is the concatenation of the
368 * mixer and kcontrol name.
369 * for dapm_mixer_named_ctl this is simply the
370 * kcontrol name.
371 */
372 name_len = strlen(w->kcontrols[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000373 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000374 name_len += 1 + strlen(w->name);
375
Mark Brown219b93f2008-10-28 13:02:31 +0000376 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000377
Richard Purdie2b97eab2006-10-06 18:32:18 +0200378 if (path->long_name == NULL)
379 return -ENOMEM;
380
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000381 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000382 default:
383 snprintf(path->long_name, name_len, "%s %s",
384 w->name, w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000385 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000386 case snd_soc_dapm_mixer_named_ctl:
387 snprintf(path->long_name, name_len, "%s",
388 w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000389 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000390 }
391
Mark Brown219b93f2008-10-28 13:02:31 +0000392 path->long_name[name_len - 1] = '\0';
393
Richard Purdie2b97eab2006-10-06 18:32:18 +0200394 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
395 path->long_name);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200396 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200397 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200398 dev_err(dapm->dev,
399 "asoc: failed to add dapm kcontrol %s: %d\n",
400 path->long_name, ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200401 kfree(path->long_name);
402 path->long_name = NULL;
403 return ret;
404 }
405 }
406 }
407 return ret;
408}
409
410/* create new dapm mux control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200411static int dapm_new_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200412 struct snd_soc_dapm_widget *w)
413{
414 struct snd_soc_dapm_path *path = NULL;
415 struct snd_kcontrol *kcontrol;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200416 struct snd_card *card = dapm->codec->card->snd_card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200417 int ret = 0;
418
419 if (!w->num_kcontrols) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200420 dev_err(dapm->dev, "asoc: mux %s has no controls\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200421 return -EINVAL;
422 }
423
424 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200425 ret = snd_ctl_add(card, kcontrol);
426
Richard Purdie2b97eab2006-10-06 18:32:18 +0200427 if (ret < 0)
428 goto err;
429
430 list_for_each_entry(path, &w->sources, list_sink)
431 path->kcontrol = kcontrol;
432
433 return ret;
434
435err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200436 dev_err(dapm->dev, "asoc: failed to add kcontrol %s\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200437 return ret;
438}
439
440/* create new dapm volume control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200441static int dapm_new_pga(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200442 struct snd_soc_dapm_widget *w)
443{
Mark Browna6c65732010-03-03 17:45:21 +0000444 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200445 dev_err(w->dapm->dev,
446 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200447
Mark Browna6c65732010-03-03 17:45:21 +0000448 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200449}
450
451/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200452static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200453{
454 struct snd_soc_dapm_path *p;
455
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200456 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200457 p->walked = 0;
458}
459
Mark Brown99497882010-05-07 20:24:05 +0100460/* We implement power down on suspend by checking the power state of
461 * the ALSA card - when we are suspending the ALSA state for the card
462 * is set to D3.
463 */
464static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
465{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200466 int level = snd_power_get_state(widget->dapm->codec->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100467
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000468 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100469 case SNDRV_CTL_POWER_D3hot:
470 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100471 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200472 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
473 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100474 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100475 default:
476 return 1;
477 }
478}
479
Richard Purdie2b97eab2006-10-06 18:32:18 +0200480/*
481 * Recursively check for a completed path to an active or physically connected
482 * output widget. Returns number of complete paths.
483 */
484static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
485{
486 struct snd_soc_dapm_path *path;
487 int con = 0;
488
Mark Brown246d0a12009-04-22 18:24:55 +0100489 if (widget->id == snd_soc_dapm_supply)
490 return 0;
491
Mark Brown010ff262009-08-17 17:39:22 +0100492 switch (widget->id) {
493 case snd_soc_dapm_adc:
494 case snd_soc_dapm_aif_out:
495 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100496 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100497 default:
498 break;
499 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200500
501 if (widget->connected) {
502 /* connected pin ? */
503 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100504 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200505
506 /* connected jack or spk ? */
507 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300508 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown99497882010-05-07 20:24:05 +0100509 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200510 }
511
512 list_for_each_entry(path, &widget->sinks, list_source) {
513 if (path->walked)
514 continue;
515
516 if (path->sink && path->connect) {
517 path->walked = 1;
518 con += is_connected_output_ep(path->sink);
519 }
520 }
521
522 return con;
523}
524
525/*
526 * Recursively check for a completed path to an active or physically connected
527 * input widget. Returns number of complete paths.
528 */
529static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
530{
531 struct snd_soc_dapm_path *path;
532 int con = 0;
533
Mark Brown246d0a12009-04-22 18:24:55 +0100534 if (widget->id == snd_soc_dapm_supply)
535 return 0;
536
Richard Purdie2b97eab2006-10-06 18:32:18 +0200537 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100538 switch (widget->id) {
539 case snd_soc_dapm_dac:
540 case snd_soc_dapm_aif_in:
541 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100542 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100543 default:
544 break;
545 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200546
547 if (widget->connected) {
548 /* connected pin ? */
549 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100550 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200551
552 /* connected VMID/Bias for lower pops */
553 if (widget->id == snd_soc_dapm_vmid)
Mark Brown99497882010-05-07 20:24:05 +0100554 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200555
556 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300557 if (widget->id == snd_soc_dapm_mic ||
558 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown99497882010-05-07 20:24:05 +0100559 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200560 }
561
562 list_for_each_entry(path, &widget->sources, list_sink) {
563 if (path->walked)
564 continue;
565
566 if (path->source && path->connect) {
567 path->walked = 1;
568 con += is_connected_input_ep(path->source);
569 }
570 }
571
572 return con;
573}
574
575/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300576 * Handler for generic register modifier widget.
577 */
578int dapm_reg_event(struct snd_soc_dapm_widget *w,
579 struct snd_kcontrol *kcontrol, int event)
580{
581 unsigned int val;
582
583 if (SND_SOC_DAPM_EVENT_ON(event))
584 val = w->on_val;
585 else
586 val = w->off_val;
587
588 snd_soc_update_bits(w->codec, -(w->reg + 1),
589 w->mask << w->shift, val << w->shift);
590
591 return 0;
592}
Mark Brown11589412008-07-29 11:42:23 +0100593EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300594
Mark Brown025756e2009-04-13 11:09:18 +0100595/* Standard power change method, used to apply power changes to most
596 * widgets.
597 */
598static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
599{
600 int ret;
601
602 /* call any power change event handlers */
603 if (w->event)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200604 dev_dbg(w->dapm->dev, "power %s event for %s flags %x\n",
Mark Brown025756e2009-04-13 11:09:18 +0100605 w->power ? "on" : "off",
606 w->name, w->event_flags);
607
608 /* power up pre event */
609 if (w->power && w->event &&
610 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
611 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
612 if (ret < 0)
613 return ret;
614 }
615
616 /* power down pre event */
617 if (!w->power && w->event &&
618 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
619 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
620 if (ret < 0)
621 return ret;
622 }
623
Mark Brown025756e2009-04-13 11:09:18 +0100624 dapm_update_bits(w);
625
Mark Brown025756e2009-04-13 11:09:18 +0100626 /* power up post event */
627 if (w->power && w->event &&
628 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
629 ret = w->event(w,
630 NULL, SND_SOC_DAPM_POST_PMU);
631 if (ret < 0)
632 return ret;
633 }
634
635 /* power down post event */
636 if (!w->power && w->event &&
637 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
638 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
639 if (ret < 0)
640 return ret;
641 }
642
643 return 0;
644}
645
Mark Browncd0f2d42009-04-20 16:56:59 +0100646/* Generic check to see if a widget should be powered.
647 */
648static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
649{
650 int in, out;
651
652 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200653 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100654 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200655 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100656 return out != 0 && in != 0;
657}
658
Mark Brown6ea31b92009-04-20 17:15:41 +0100659/* Check to see if an ADC has power */
660static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
661{
662 int in;
663
664 if (w->active) {
665 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200666 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100667 return in != 0;
668 } else {
669 return dapm_generic_check_power(w);
670 }
671}
672
673/* Check to see if a DAC has power */
674static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
675{
676 int out;
677
678 if (w->active) {
679 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200680 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100681 return out != 0;
682 } else {
683 return dapm_generic_check_power(w);
684 }
685}
686
Mark Brown246d0a12009-04-22 18:24:55 +0100687/* Check to see if a power supply is needed */
688static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
689{
690 struct snd_soc_dapm_path *path;
691 int power = 0;
692
693 /* Check if one of our outputs is connected */
694 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +0100695 if (path->connected &&
696 !path->connected(path->source, path->sink))
697 continue;
698
Mark Brown246d0a12009-04-22 18:24:55 +0100699 if (path->sink && path->sink->power_check &&
700 path->sink->power_check(path->sink)) {
701 power = 1;
702 break;
703 }
704 }
705
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200706 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100707
708 return power;
709}
710
Mark Brown38357ab2009-06-06 19:03:23 +0100711static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
712 struct snd_soc_dapm_widget *b,
713 int sort[])
Mark Brown42aa3412009-03-01 19:21:10 +0000714{
Mark Brown38357ab2009-06-06 19:03:23 +0100715 if (sort[a->id] != sort[b->id])
716 return sort[a->id] - sort[b->id];
Mark Brownb22ead22009-06-07 12:51:26 +0100717 if (a->reg != b->reg)
718 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +0000719 if (a->dapm != b->dapm)
720 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +0000721
Mark Brown38357ab2009-06-06 19:03:23 +0100722 return 0;
723}
Mark Brown42aa3412009-03-01 19:21:10 +0000724
Mark Brown38357ab2009-06-06 19:03:23 +0100725/* Insert a widget in order into a DAPM power sequence. */
726static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
727 struct list_head *list,
728 int sort[])
729{
730 struct snd_soc_dapm_widget *w;
731
732 list_for_each_entry(w, list, power_list)
733 if (dapm_seq_compare(new_widget, w, sort) < 0) {
734 list_add_tail(&new_widget->power_list, &w->power_list);
735 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000736 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100737
Mark Brown38357ab2009-06-06 19:03:23 +0100738 list_add_tail(&new_widget->power_list, list);
739}
Mark Brown42aa3412009-03-01 19:21:10 +0000740
Mark Brown68f89ad2010-11-03 23:51:49 -0400741static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
742 struct snd_soc_dapm_widget *w, int event)
743{
744 struct snd_soc_card *card = dapm->card;
745 const char *ev_name;
746 int power, ret;
747
748 switch (event) {
749 case SND_SOC_DAPM_PRE_PMU:
750 ev_name = "PRE_PMU";
751 power = 1;
752 break;
753 case SND_SOC_DAPM_POST_PMU:
754 ev_name = "POST_PMU";
755 power = 1;
756 break;
757 case SND_SOC_DAPM_PRE_PMD:
758 ev_name = "PRE_PMD";
759 power = 0;
760 break;
761 case SND_SOC_DAPM_POST_PMD:
762 ev_name = "POST_PMD";
763 power = 0;
764 break;
765 default:
766 BUG();
767 return;
768 }
769
770 if (w->power != power)
771 return;
772
773 if (w->event && (w->event_flags & event)) {
774 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
775 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -0400776 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400777 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -0400778 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400779 if (ret < 0)
780 pr_err("%s: %s event failed: %d\n",
781 ev_name, w->name, ret);
782 }
783}
784
Mark Brownb22ead22009-06-07 12:51:26 +0100785/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200786static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +0100787 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100788{
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200789 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -0400790 struct snd_soc_dapm_widget *w;
791 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +0100792 unsigned int value = 0;
793 unsigned int mask = 0;
794 unsigned int cur_mask;
795
796 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
797 power_list)->reg;
798
799 list_for_each_entry(w, pending, power_list) {
800 cur_mask = 1 << w->shift;
801 BUG_ON(reg != w->reg);
802
803 if (w->invert)
804 power = !w->power;
805 else
806 power = w->power;
807
808 mask |= cur_mask;
809 if (power)
810 value |= cur_mask;
811
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200812 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +0100813 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
814 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100815
Mark Brown68f89ad2010-11-03 23:51:49 -0400816 /* Check for events */
817 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
818 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100819 }
820
Mark Brown81628102009-06-07 13:21:24 +0100821 if (reg >= 0) {
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200822 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +0100823 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200824 value, mask, reg, card->pop_time);
825 pop_wait(card->pop_time);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200826 snd_soc_update_bits(dapm->codec, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +0100827 }
828
829 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -0400830 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
831 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +0000832 }
Mark Brown42aa3412009-03-01 19:21:10 +0000833}
834
Mark Brownb22ead22009-06-07 12:51:26 +0100835/* Apply a DAPM power sequence.
836 *
837 * We walk over a pre-sorted list of widgets to apply power to. In
838 * order to minimise the number of writes to the device required
839 * multiple widgets will be updated in a single write where possible.
840 * Currently anything that requires more than a single write is not
841 * handled.
842 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200843static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
844 struct list_head *list, int event, int sort[])
Mark Brownb22ead22009-06-07 12:51:26 +0100845{
846 struct snd_soc_dapm_widget *w, *n;
847 LIST_HEAD(pending);
848 int cur_sort = -1;
849 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200850 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown163cac02009-06-07 10:12:52 +0100851 int ret;
852
Mark Brownb22ead22009-06-07 12:51:26 +0100853 list_for_each_entry_safe(w, n, list, power_list) {
854 ret = 0;
855
856 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200857 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
858 w->dapm != cur_dapm) {
Mark Brownb22ead22009-06-07 12:51:26 +0100859 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200860 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +0100861
862 INIT_LIST_HEAD(&pending);
863 cur_sort = -1;
864 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200865 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +0100866 }
867
Mark Brown163cac02009-06-07 10:12:52 +0100868 switch (w->id) {
869 case snd_soc_dapm_pre:
870 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100871 list_for_each_entry_safe_continue(w, n, list,
872 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100873
Mark Brownb22ead22009-06-07 12:51:26 +0100874 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100875 ret = w->event(w,
876 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100877 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100878 ret = w->event(w,
879 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +0100880 break;
881
882 case snd_soc_dapm_post:
883 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100884 list_for_each_entry_safe_continue(w, n, list,
885 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100886
Mark Brownb22ead22009-06-07 12:51:26 +0100887 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100888 ret = w->event(w,
889 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100890 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100891 ret = w->event(w,
892 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100893 break;
894
895 case snd_soc_dapm_input:
896 case snd_soc_dapm_output:
897 case snd_soc_dapm_hp:
898 case snd_soc_dapm_mic:
899 case snd_soc_dapm_line:
900 case snd_soc_dapm_spk:
901 /* No register support currently */
Mark Brownb22ead22009-06-07 12:51:26 +0100902 ret = dapm_generic_apply_power(w);
Mark Brown163cac02009-06-07 10:12:52 +0100903 break;
904
905 default:
Mark Brown81628102009-06-07 13:21:24 +0100906 /* Queue it up for application */
907 cur_sort = sort[w->id];
908 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200909 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +0100910 list_move(&w->power_list, &pending);
911 break;
Mark Brown163cac02009-06-07 10:12:52 +0100912 }
Mark Brownb22ead22009-06-07 12:51:26 +0100913
914 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200915 dev_err(w->dapm->dev,
916 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +0100917 }
Mark Brownb22ead22009-06-07 12:51:26 +0100918
919 if (!list_empty(&pending))
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200920 dapm_seq_run_coalesced(dapm, &pending);
Mark Brown163cac02009-06-07 10:12:52 +0100921}
922
Mark Brown42aa3412009-03-01 19:21:10 +0000923/*
Richard Purdie2b97eab2006-10-06 18:32:18 +0200924 * Scan each dapm widget for complete audio path.
925 * A complete path is a route that has valid endpoints i.e.:-
926 *
927 * o DAC to output pin.
928 * o Input Pin to ADC.
929 * o Input pin to Output pin (bypass, sidetone)
930 * o DAC to ADC (loopback).
931 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200932static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200933{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200934 struct snd_soc_card *card = dapm->codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200935 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200936 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +0100937 LIST_HEAD(up_list);
938 LIST_HEAD(down_list);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100939 int ret = 0;
Mark Brown38357ab2009-06-06 19:03:23 +0100940 int power;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200941
Mark Brown84e90932010-11-04 00:07:02 -0400942 trace_snd_soc_dapm_start(card);
943
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200944 list_for_each_entry(d, &card->dapm_list, list)
945 if (d->n_widgets)
946 d->dev_power = 0;
947
Mark Brown6d3ddc82009-05-16 17:47:29 +0100948 /* Check which widgets we need to power and store them in
949 * lists indicating if they should be powered up or down.
950 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +0200951 list_for_each_entry(w, &card->widgets, list) {
Mark Brown6d3ddc82009-05-16 17:47:29 +0100952 switch (w->id) {
953 case snd_soc_dapm_pre:
Mark Brown291f3bb2009-06-07 13:57:17 +0100954 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100955 break;
956 case snd_soc_dapm_post:
Mark Brown291f3bb2009-06-07 13:57:17 +0100957 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100958 break;
959
960 default:
961 if (!w->power_check)
962 continue;
963
Mark Brown99497882010-05-07 20:24:05 +0100964 if (!w->force)
Mark Brown50b6bce2009-11-23 13:11:53 +0000965 power = w->power_check(w);
Mark Brown99497882010-05-07 20:24:05 +0100966 else
967 power = 1;
968 if (power)
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200969 w->dapm->dev_power = 1;
Mark Brown452c5ea2009-05-17 21:41:23 +0100970
Mark Brown6d3ddc82009-05-16 17:47:29 +0100971 if (w->power == power)
972 continue;
973
Mark Brown84e90932010-11-04 00:07:02 -0400974 trace_snd_soc_dapm_widget_power(w, power);
975
Mark Brown6d3ddc82009-05-16 17:47:29 +0100976 if (power)
Mark Brown291f3bb2009-06-07 13:57:17 +0100977 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100978 else
Mark Brown291f3bb2009-06-07 13:57:17 +0100979 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100980
981 w->power = power;
982 break;
983 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200984 }
985
Mark Brownb14b76a2009-08-17 11:55:38 +0100986 /* If there are no DAPM widgets then try to figure out power from the
987 * event type.
988 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +0200989 if (!dapm->n_widgets) {
Mark Brownb14b76a2009-08-17 11:55:38 +0100990 switch (event) {
991 case SND_SOC_DAPM_STREAM_START:
992 case SND_SOC_DAPM_STREAM_RESUME:
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200993 dapm->dev_power = 1;
Mark Brownb14b76a2009-08-17 11:55:38 +0100994 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +0200995 case SND_SOC_DAPM_STREAM_STOP:
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200996 dapm->dev_power = !!dapm->codec->active;
Jarkko Nikula862af8a2010-12-10 20:53:55 +0200997 break;
Mark Brown50b6bce2009-11-23 13:11:53 +0000998 case SND_SOC_DAPM_STREAM_SUSPEND:
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200999 dapm->dev_power = 0;
Mark Brown50b6bce2009-11-23 13:11:53 +00001000 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001001 case SND_SOC_DAPM_STREAM_NOP:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001002 switch (dapm->bias_level) {
Mark Browna96ca332010-01-19 22:49:43 +00001003 case SND_SOC_BIAS_STANDBY:
1004 case SND_SOC_BIAS_OFF:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001005 dapm->dev_power = 0;
Mark Browna96ca332010-01-19 22:49:43 +00001006 break;
1007 default:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001008 dapm->dev_power = 1;
Mark Browna96ca332010-01-19 22:49:43 +00001009 break;
1010 }
Mark Brown50b6bce2009-11-23 13:11:53 +00001011 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001012 default:
1013 break;
1014 }
1015 }
1016
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001017 list_for_each_entry(d, &dapm->card->dapm_list, list) {
1018 if (d->dev_power && d->bias_level == SND_SOC_BIAS_OFF) {
1019 ret = snd_soc_dapm_set_bias_level(card, d,
1020 SND_SOC_BIAS_STANDBY);
1021 if (ret != 0)
1022 dev_err(d->dev,
1023 "Failed to turn on bias: %d\n", ret);
1024 }
Mark Browna96ca332010-01-19 22:49:43 +00001025
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001026 /* If we're changing to all on or all off then prepare */
1027 if ((d->dev_power && d->bias_level == SND_SOC_BIAS_STANDBY) ||
1028 (!d->dev_power && d->bias_level == SND_SOC_BIAS_ON)) {
1029 ret = snd_soc_dapm_set_bias_level(card, d,
1030 SND_SOC_BIAS_PREPARE);
1031 if (ret != 0)
1032 dev_err(d->dev,
1033 "Failed to prepare bias: %d\n", ret);
1034 }
Mark Brown452c5ea2009-05-17 21:41:23 +01001035 }
1036
Mark Brown6d3ddc82009-05-16 17:47:29 +01001037 /* Power down widgets first; try to avoid amplifying pops. */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001038 dapm_seq_run(dapm, &down_list, event, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001039
1040 /* Now power up. */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001041 dapm_seq_run(dapm, &up_list, event, dapm_up_seq);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001042
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001043 list_for_each_entry(d, &dapm->card->dapm_list, list) {
1044 /* If we just powered the last thing off drop to standby bias */
1045 if (d->bias_level == SND_SOC_BIAS_PREPARE && !d->dev_power) {
1046 ret = snd_soc_dapm_set_bias_level(card, d,
1047 SND_SOC_BIAS_STANDBY);
1048 if (ret != 0)
1049 dev_err(d->dev,
1050 "Failed to apply standby bias: %d\n",
1051 ret);
1052 }
Mark Brown452c5ea2009-05-17 21:41:23 +01001053
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001054 /* If we're in standby and can support bias off then do that */
1055 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1056 d->idle_bias_off) {
1057 ret = snd_soc_dapm_set_bias_level(card, d,
1058 SND_SOC_BIAS_OFF);
1059 if (ret != 0)
1060 dev_err(d->dev,
1061 "Failed to turn off bias: %d\n", ret);
1062 }
Mark Browna96ca332010-01-19 22:49:43 +00001063
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001064 /* If we just powered up then move to active bias */
1065 if (d->bias_level == SND_SOC_BIAS_PREPARE && d->dev_power) {
1066 ret = snd_soc_dapm_set_bias_level(card, d,
1067 SND_SOC_BIAS_ON);
1068 if (ret != 0)
1069 dev_err(d->dev,
1070 "Failed to apply active bias: %d\n",
1071 ret);
1072 }
Mark Brown452c5ea2009-05-17 21:41:23 +01001073 }
1074
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001075 pop_dbg(dapm->dev, card->pop_time,
1076 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001077 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001078
Mark Brown84e90932010-11-04 00:07:02 -04001079 trace_snd_soc_dapm_done(card);
1080
Mark Brown42aa3412009-03-01 19:21:10 +00001081 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001082}
1083
Mark Brown79fb9382009-08-21 16:38:13 +01001084#ifdef CONFIG_DEBUG_FS
1085static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1086{
1087 file->private_data = inode->i_private;
1088 return 0;
1089}
1090
1091static ssize_t dapm_widget_power_read_file(struct file *file,
1092 char __user *user_buf,
1093 size_t count, loff_t *ppos)
1094{
1095 struct snd_soc_dapm_widget *w = file->private_data;
1096 char *buf;
1097 int in, out;
1098 ssize_t ret;
1099 struct snd_soc_dapm_path *p = NULL;
1100
1101 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1102 if (!buf)
1103 return -ENOMEM;
1104
1105 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001106 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001107 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001108 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001109
Mark Brownd033c362009-12-04 15:25:56 +00001110 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001111 w->name, w->power ? "On" : "Off", in, out);
1112
Mark Brownd033c362009-12-04 15:25:56 +00001113 if (w->reg >= 0)
1114 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1115 " - R%d(0x%x) bit %d",
1116 w->reg, w->reg, w->shift);
1117
1118 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1119
Mark Brown3eef08b2009-09-14 16:49:00 +01001120 if (w->sname)
1121 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1122 w->sname,
1123 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001124
1125 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001126 if (p->connected && !p->connected(w, p->sink))
1127 continue;
1128
Mark Brown79fb9382009-08-21 16:38:13 +01001129 if (p->connect)
1130 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1131 " in %s %s\n",
1132 p->name ? p->name : "static",
1133 p->source->name);
1134 }
1135 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001136 if (p->connected && !p->connected(w, p->sink))
1137 continue;
1138
Mark Brown79fb9382009-08-21 16:38:13 +01001139 if (p->connect)
1140 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1141 " out %s %s\n",
1142 p->name ? p->name : "static",
1143 p->sink->name);
1144 }
1145
1146 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1147
1148 kfree(buf);
1149 return ret;
1150}
1151
1152static const struct file_operations dapm_widget_power_fops = {
1153 .open = dapm_widget_power_open_file,
1154 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001155 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001156};
1157
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001158void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001159{
1160 struct snd_soc_dapm_widget *w;
1161 struct dentry *d;
1162
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001163 if (!dapm->debugfs_dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001164 return;
1165
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001166 list_for_each_entry(w, &dapm->card->widgets, list) {
1167 if (!w->name || w->dapm != dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001168 continue;
1169
1170 d = debugfs_create_file(w->name, 0444,
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001171 dapm->debugfs_dapm, w,
Mark Brown79fb9382009-08-21 16:38:13 +01001172 &dapm_widget_power_fops);
1173 if (!d)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001174 dev_warn(w->dapm->dev,
1175 "ASoC: Failed to create %s debugfs file\n",
1176 w->name);
Mark Brown79fb9382009-08-21 16:38:13 +01001177 }
1178}
1179#else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001180void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001181{
1182}
1183#endif
1184
Richard Purdie2b97eab2006-10-06 18:32:18 +02001185/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001186static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001187 struct snd_kcontrol *kcontrol, int change,
1188 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001189{
1190 struct snd_soc_dapm_path *path;
1191 int found = 0;
1192
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001193 if (widget->id != snd_soc_dapm_mux &&
1194 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001195 return -ENODEV;
1196
Mark Brown3a655772009-10-05 17:23:30 +01001197 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001198 return 0;
1199
1200 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001201 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001202 if (path->kcontrol != kcontrol)
1203 continue;
1204
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001205 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001206 continue;
1207
1208 found = 1;
1209 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001210 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001211 path->connect = 1; /* new connection */
1212 else
1213 path->connect = 0; /* old connection must be powered down */
1214 }
1215
Mark Brownb91b8fa2010-01-20 18:18:35 +00001216 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001217 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001218
1219 return 0;
1220}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001221
Milan plzik1b075e32008-01-10 14:39:46 +01001222/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001223static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001224 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001225{
1226 struct snd_soc_dapm_path *path;
1227 int found = 0;
1228
Milan plzik1b075e32008-01-10 14:39:46 +01001229 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001230 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001231 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001232 return -ENODEV;
1233
Richard Purdie2b97eab2006-10-06 18:32:18 +02001234 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001235 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001236 if (path->kcontrol != kcontrol)
1237 continue;
1238
1239 /* found, now check type */
1240 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001241 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001242 break;
1243 }
1244
Mark Brownb91b8fa2010-01-20 18:18:35 +00001245 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001246 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001247
1248 return 0;
1249}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001250
1251/* show dapm widget status in sys fs */
1252static ssize_t dapm_widget_show(struct device *dev,
1253 struct device_attribute *attr, char *buf)
1254{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001255 struct snd_soc_pcm_runtime *rtd =
1256 container_of(dev, struct snd_soc_pcm_runtime, dev);
1257 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001258 struct snd_soc_dapm_widget *w;
1259 int count = 0;
1260 char *state = "not set";
1261
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001262 list_for_each_entry(w, &codec->card->widgets, list) {
1263 if (w->dapm != &codec->dapm)
1264 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001265
1266 /* only display widgets that burnm power */
1267 switch (w->id) {
1268 case snd_soc_dapm_hp:
1269 case snd_soc_dapm_mic:
1270 case snd_soc_dapm_spk:
1271 case snd_soc_dapm_line:
1272 case snd_soc_dapm_micbias:
1273 case snd_soc_dapm_dac:
1274 case snd_soc_dapm_adc:
1275 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001276 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001277 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001278 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001279 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001280 if (w->name)
1281 count += sprintf(buf + count, "%s: %s\n",
1282 w->name, w->power ? "On":"Off");
1283 break;
1284 default:
1285 break;
1286 }
1287 }
1288
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001289 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001290 case SND_SOC_BIAS_ON:
1291 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001292 break;
Mark Brown0be98982008-05-19 12:31:28 +02001293 case SND_SOC_BIAS_PREPARE:
1294 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001295 break;
Mark Brown0be98982008-05-19 12:31:28 +02001296 case SND_SOC_BIAS_STANDBY:
1297 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001298 break;
Mark Brown0be98982008-05-19 12:31:28 +02001299 case SND_SOC_BIAS_OFF:
1300 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001301 break;
1302 }
1303 count += sprintf(buf + count, "PM State: %s\n", state);
1304
1305 return count;
1306}
1307
1308static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1309
1310int snd_soc_dapm_sys_add(struct device *dev)
1311{
Troy Kisky12ef1932008-10-13 17:42:14 -07001312 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001313}
1314
1315static void snd_soc_dapm_sys_remove(struct device *dev)
1316{
Mark Brownaef90842009-05-16 17:53:16 +01001317 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001318}
1319
1320/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001321static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001322{
1323 struct snd_soc_dapm_widget *w, *next_w;
1324 struct snd_soc_dapm_path *p, *next_p;
1325
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001326 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1327 if (w->dapm != dapm)
1328 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001329 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001330 /*
1331 * remove source and sink paths associated to this widget.
1332 * While removing the path, remove reference to it from both
1333 * source and sink widgets so that path is removed only once.
1334 */
1335 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1336 list_del(&p->list_sink);
1337 list_del(&p->list_source);
1338 list_del(&p->list);
1339 kfree(p->long_name);
1340 kfree(p);
1341 }
1342 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1343 list_del(&p->list_sink);
1344 list_del(&p->list_source);
1345 list_del(&p->list);
1346 kfree(p->long_name);
1347 kfree(p);
1348 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001349 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001350 kfree(w);
1351 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001352}
1353
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001354static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001355 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001356{
1357 struct snd_soc_dapm_widget *w;
1358
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001359 list_for_each_entry(w, &dapm->card->widgets, list) {
1360 if (w->dapm != dapm)
1361 continue;
Liam Girdwooda5302182008-07-07 13:35:17 +01001362 if (!strcmp(w->name, pin)) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001363 dev_dbg(w->dapm->dev, "dapm: pin %s = %d\n",
1364 pin, status);
Liam Girdwooda5302182008-07-07 13:35:17 +01001365 w->connected = status;
Mark Brown5b9e87c2010-03-22 13:36:13 +00001366 /* Allow disabling of forced pins */
1367 if (status == 0)
1368 w->force = 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001369 return 0;
1370 }
1371 }
1372
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001373 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001374 return -EINVAL;
1375}
1376
Richard Purdie2b97eab2006-10-06 18:32:18 +02001377/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001378 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001379 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001380 *
1381 * Walks all dapm audio paths and powers widgets according to their
1382 * stream or path usage.
1383 *
1384 * Returns 0 for success.
1385 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001386int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001387{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001388 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001389}
Liam Girdwooda5302182008-07-07 13:35:17 +01001390EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001391
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001392static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001393 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001394{
1395 struct snd_soc_dapm_path *path;
1396 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001397 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001398 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001399 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001400 const char *source;
1401 char prefixed_sink[80];
1402 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001403 int ret = 0;
1404
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001405 if (dapm->codec->name_prefix) {
1406 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1407 dapm->codec->name_prefix, route->sink);
1408 sink = prefixed_sink;
1409 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1410 dapm->codec->name_prefix, route->source);
1411 source = prefixed_source;
1412 } else {
1413 sink = route->sink;
1414 source = route->source;
1415 }
1416
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001417 /*
1418 * find src and dest widgets over all widgets but favor a widget from
1419 * current DAPM context
1420 */
1421 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001422 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001423 wtsink = w;
1424 if (w->dapm == dapm)
1425 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001426 continue;
1427 }
1428 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001429 wtsource = w;
1430 if (w->dapm == dapm)
1431 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001432 }
1433 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001434 /* use widget from another DAPM context if not found from this */
1435 if (!wsink)
1436 wsink = wtsink;
1437 if (!wsource)
1438 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001439
1440 if (wsource == NULL || wsink == NULL)
1441 return -ENODEV;
1442
1443 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1444 if (!path)
1445 return -ENOMEM;
1446
1447 path->source = wsource;
1448 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001449 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001450 INIT_LIST_HEAD(&path->list);
1451 INIT_LIST_HEAD(&path->list_source);
1452 INIT_LIST_HEAD(&path->list_sink);
1453
1454 /* check for external widgets */
1455 if (wsink->id == snd_soc_dapm_input) {
1456 if (wsource->id == snd_soc_dapm_micbias ||
1457 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001458 wsource->id == snd_soc_dapm_line ||
1459 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001460 wsink->ext = 1;
1461 }
1462 if (wsource->id == snd_soc_dapm_output) {
1463 if (wsink->id == snd_soc_dapm_spk ||
1464 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001465 wsink->id == snd_soc_dapm_line ||
1466 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001467 wsource->ext = 1;
1468 }
1469
1470 /* connect static paths */
1471 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001472 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001473 list_add(&path->list_sink, &wsink->sources);
1474 list_add(&path->list_source, &wsource->sinks);
1475 path->connect = 1;
1476 return 0;
1477 }
1478
1479 /* connect dynamic paths */
1480 switch(wsink->id) {
1481 case snd_soc_dapm_adc:
1482 case snd_soc_dapm_dac:
1483 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001484 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001485 case snd_soc_dapm_input:
1486 case snd_soc_dapm_output:
1487 case snd_soc_dapm_micbias:
1488 case snd_soc_dapm_vmid:
1489 case snd_soc_dapm_pre:
1490 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001491 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001492 case snd_soc_dapm_aif_in:
1493 case snd_soc_dapm_aif_out:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001494 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001495 list_add(&path->list_sink, &wsink->sources);
1496 list_add(&path->list_source, &wsource->sinks);
1497 path->connect = 1;
1498 return 0;
1499 case snd_soc_dapm_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001500 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001501 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Richard Purdie2b97eab2006-10-06 18:32:18 +02001502 &wsink->kcontrols[0]);
1503 if (ret != 0)
1504 goto err;
1505 break;
1506 case snd_soc_dapm_switch:
1507 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001508 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001509 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001510 if (ret != 0)
1511 goto err;
1512 break;
1513 case snd_soc_dapm_hp:
1514 case snd_soc_dapm_mic:
1515 case snd_soc_dapm_line:
1516 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001517 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001518 list_add(&path->list_sink, &wsink->sources);
1519 list_add(&path->list_source, &wsource->sinks);
1520 path->connect = 0;
1521 return 0;
1522 }
1523 return 0;
1524
1525err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001526 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1527 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001528 kfree(path);
1529 return ret;
1530}
Mark Brown105f1c22008-05-13 14:52:19 +02001531
1532/**
Mark Brown105f1c22008-05-13 14:52:19 +02001533 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001534 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02001535 * @route: audio routes
1536 * @num: number of routes
1537 *
1538 * Connects 2 dapm widgets together via a named audio path. The sink is
1539 * the widget receiving the audio signal, whilst the source is the sender
1540 * of the audio signal.
1541 *
1542 * Returns 0 for success else error. On error all resources can be freed
1543 * with a call to snd_soc_card_free().
1544 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001545int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02001546 const struct snd_soc_dapm_route *route, int num)
1547{
1548 int i, ret;
1549
1550 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001551 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001552 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001553 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1554 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02001555 return ret;
1556 }
1557 route++;
1558 }
1559
1560 return 0;
1561}
1562EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1563
1564/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001565 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001566 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001567 *
1568 * Checks the codec for any new dapm widgets and creates them if found.
1569 *
1570 * Returns 0 for success.
1571 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001572int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001573{
1574 struct snd_soc_dapm_widget *w;
1575
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001576 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001577 {
1578 if (w->new)
1579 continue;
1580
1581 switch(w->id) {
1582 case snd_soc_dapm_switch:
1583 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001584 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001585 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001586 dapm_new_mixer(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001587 break;
1588 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001589 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001590 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001591 dapm_new_mux(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001592 break;
1593 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01001594 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01001595 w->power_check = dapm_adc_check_power;
1596 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001597 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01001598 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01001599 w->power_check = dapm_dac_check_power;
1600 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001601 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001602 case snd_soc_dapm_out_drv:
Mark Brownb75576d2009-04-20 17:56:13 +01001603 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001604 dapm_new_pga(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001605 break;
1606 case snd_soc_dapm_input:
1607 case snd_soc_dapm_output:
1608 case snd_soc_dapm_micbias:
1609 case snd_soc_dapm_spk:
1610 case snd_soc_dapm_hp:
1611 case snd_soc_dapm_mic:
1612 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001613 w->power_check = dapm_generic_check_power;
1614 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001615 case snd_soc_dapm_supply:
1616 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001617 case snd_soc_dapm_vmid:
1618 case snd_soc_dapm_pre:
1619 case snd_soc_dapm_post:
1620 break;
1621 }
1622 w->new = 1;
1623 }
1624
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001625 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001626 return 0;
1627}
1628EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1629
1630/**
1631 * snd_soc_dapm_get_volsw - dapm mixer get callback
1632 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001633 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001634 *
1635 * Callback to get the value of a dapm mixer control.
1636 *
1637 * Returns 0 for success.
1638 */
1639int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1640 struct snd_ctl_elem_value *ucontrol)
1641{
1642 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001643 struct soc_mixer_control *mc =
1644 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04001645 unsigned int reg = mc->reg;
1646 unsigned int shift = mc->shift;
1647 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001648 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001649 unsigned int invert = mc->invert;
1650 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001651
Richard Purdie2b97eab2006-10-06 18:32:18 +02001652 ucontrol->value.integer.value[0] =
1653 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1654 if (shift != rshift)
1655 ucontrol->value.integer.value[1] =
1656 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1657 if (invert) {
1658 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001659 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001660 if (shift != rshift)
1661 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001662 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001663 }
1664
1665 return 0;
1666}
1667EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1668
1669/**
1670 * snd_soc_dapm_put_volsw - dapm mixer set callback
1671 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001672 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001673 *
1674 * Callback to set the value of a dapm mixer control.
1675 *
1676 * Returns 0 for success.
1677 */
1678int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1679 struct snd_ctl_elem_value *ucontrol)
1680{
1681 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001682 struct soc_mixer_control *mc =
1683 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04001684 unsigned int reg = mc->reg;
1685 unsigned int shift = mc->shift;
1686 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001687 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001688 unsigned int mask = (1 << fls(max)) - 1;
1689 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001690 unsigned int val, val2, val_mask;
Mark Brown283375c2009-12-07 18:09:03 +00001691 int connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001692 int ret;
1693
1694 val = (ucontrol->value.integer.value[0] & mask);
1695
1696 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001697 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001698 val_mask = mask << shift;
1699 val = val << shift;
1700 if (shift != rshift) {
1701 val2 = (ucontrol->value.integer.value[1] & mask);
1702 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001703 val2 = max - val2;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001704 val_mask |= mask << rshift;
1705 val |= val2 << rshift;
1706 }
1707
1708 mutex_lock(&widget->codec->mutex);
1709 widget->value = val;
1710
Mark Brown283375c2009-12-07 18:09:03 +00001711 if (snd_soc_test_bits(widget->codec, reg, val_mask, val)) {
1712 if (val)
1713 /* new connection */
1714 connect = invert ? 0:1;
1715 else
1716 /* old connection must be powered down */
1717 connect = invert ? 1:0;
1718
1719 dapm_mixer_update_power(widget, kcontrol, connect);
1720 }
1721
Richard Purdie2b97eab2006-10-06 18:32:18 +02001722 if (widget->event) {
1723 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001724 ret = widget->event(widget, kcontrol,
1725 SND_SOC_DAPM_PRE_REG);
1726 if (ret < 0) {
1727 ret = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001728 goto out;
Laim Girdwood9af6d952008-01-10 14:41:02 +01001729 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001730 }
1731 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1732 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001733 ret = widget->event(widget, kcontrol,
1734 SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001735 } else
1736 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1737
1738out:
1739 mutex_unlock(&widget->codec->mutex);
1740 return ret;
1741}
1742EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1743
1744/**
1745 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1746 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001747 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001748 *
1749 * Callback to get the value of a dapm enumerated double mixer control.
1750 *
1751 * Returns 0 for success.
1752 */
1753int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1754 struct snd_ctl_elem_value *ucontrol)
1755{
1756 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1757 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001758 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001759
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +01001760 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001761 ;
1762 val = snd_soc_read(widget->codec, e->reg);
1763 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1764 if (e->shift_l != e->shift_r)
1765 ucontrol->value.enumerated.item[1] =
1766 (val >> e->shift_r) & (bitmask - 1);
1767
1768 return 0;
1769}
1770EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1771
1772/**
1773 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1774 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001775 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001776 *
1777 * Callback to set the value of a dapm enumerated double mixer control.
1778 *
1779 * Returns 0 for success.
1780 */
1781int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1782 struct snd_ctl_elem_value *ucontrol)
1783{
1784 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1785 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001786 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001787 unsigned int mask, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001788 int ret = 0;
1789
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +01001790 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001791 ;
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +01001792 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001793 return -EINVAL;
1794 mux = ucontrol->value.enumerated.item[0];
1795 val = mux << e->shift_l;
1796 mask = (bitmask - 1) << e->shift_l;
1797 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +01001798 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001799 return -EINVAL;
1800 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1801 mask |= (bitmask - 1) << e->shift_r;
1802 }
1803
1804 mutex_lock(&widget->codec->mutex);
1805 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01001806 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1807 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01001808
1809 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1810 ret = widget->event(widget,
1811 kcontrol, SND_SOC_DAPM_PRE_REG);
1812 if (ret < 0)
1813 goto out;
1814 }
1815
1816 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1817
1818 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1819 ret = widget->event(widget,
1820 kcontrol, SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001821
1822out:
1823 mutex_unlock(&widget->codec->mutex);
1824 return ret;
1825}
1826EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1827
1828/**
Mark Brownd2b247a2009-10-06 15:21:04 +01001829 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1830 * @kcontrol: mixer control
1831 * @ucontrol: control element information
1832 *
1833 * Returns 0 for success.
1834 */
1835int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1836 struct snd_ctl_elem_value *ucontrol)
1837{
1838 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1839
1840 ucontrol->value.enumerated.item[0] = widget->value;
1841
1842 return 0;
1843}
1844EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1845
1846/**
1847 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1848 * @kcontrol: mixer control
1849 * @ucontrol: control element information
1850 *
1851 * Returns 0 for success.
1852 */
1853int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
1854 struct snd_ctl_elem_value *ucontrol)
1855{
1856 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1857 struct soc_enum *e =
1858 (struct soc_enum *)kcontrol->private_value;
1859 int change;
1860 int ret = 0;
1861
1862 if (ucontrol->value.enumerated.item[0] >= e->max)
1863 return -EINVAL;
1864
1865 mutex_lock(&widget->codec->mutex);
1866
1867 change = widget->value != ucontrol->value.enumerated.item[0];
1868 widget->value = ucontrol->value.enumerated.item[0];
1869 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
1870
1871 mutex_unlock(&widget->codec->mutex);
1872 return ret;
1873}
1874EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
1875
1876/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001877 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1878 * callback
1879 * @kcontrol: mixer control
1880 * @ucontrol: control element information
1881 *
1882 * Callback to get the value of a dapm semi enumerated double mixer control.
1883 *
1884 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1885 * used for handling bitfield coded enumeration for example.
1886 *
1887 * Returns 0 for success.
1888 */
1889int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1890 struct snd_ctl_elem_value *ucontrol)
1891{
1892 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001893 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001894 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001895
1896 reg_val = snd_soc_read(widget->codec, e->reg);
1897 val = (reg_val >> e->shift_l) & e->mask;
1898 for (mux = 0; mux < e->max; mux++) {
1899 if (val == e->values[mux])
1900 break;
1901 }
1902 ucontrol->value.enumerated.item[0] = mux;
1903 if (e->shift_l != e->shift_r) {
1904 val = (reg_val >> e->shift_r) & e->mask;
1905 for (mux = 0; mux < e->max; mux++) {
1906 if (val == e->values[mux])
1907 break;
1908 }
1909 ucontrol->value.enumerated.item[1] = mux;
1910 }
1911
1912 return 0;
1913}
1914EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1915
1916/**
1917 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1918 * callback
1919 * @kcontrol: mixer control
1920 * @ucontrol: control element information
1921 *
1922 * Callback to set the value of a dapm semi enumerated double mixer control.
1923 *
1924 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1925 * used for handling bitfield coded enumeration for example.
1926 *
1927 * Returns 0 for success.
1928 */
1929int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1930 struct snd_ctl_elem_value *ucontrol)
1931{
1932 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001933 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001934 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001935 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001936 int ret = 0;
1937
1938 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1939 return -EINVAL;
1940 mux = ucontrol->value.enumerated.item[0];
1941 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1942 mask = e->mask << e->shift_l;
1943 if (e->shift_l != e->shift_r) {
1944 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1945 return -EINVAL;
1946 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1947 mask |= e->mask << e->shift_r;
1948 }
1949
1950 mutex_lock(&widget->codec->mutex);
1951 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01001952 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1953 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01001954
1955 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1956 ret = widget->event(widget,
1957 kcontrol, SND_SOC_DAPM_PRE_REG);
1958 if (ret < 0)
1959 goto out;
1960 }
1961
1962 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1963
1964 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1965 ret = widget->event(widget,
1966 kcontrol, SND_SOC_DAPM_POST_REG);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001967
1968out:
1969 mutex_unlock(&widget->codec->mutex);
1970 return ret;
1971}
1972EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1973
1974/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00001975 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1976 *
1977 * @kcontrol: mixer control
1978 * @uinfo: control element information
1979 *
1980 * Callback to provide information about a pin switch control.
1981 */
1982int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1983 struct snd_ctl_elem_info *uinfo)
1984{
1985 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1986 uinfo->count = 1;
1987 uinfo->value.integer.min = 0;
1988 uinfo->value.integer.max = 1;
1989
1990 return 0;
1991}
1992EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1993
1994/**
1995 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1996 *
1997 * @kcontrol: mixer control
1998 * @ucontrol: Value
1999 */
2000int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2001 struct snd_ctl_elem_value *ucontrol)
2002{
2003 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2004 const char *pin = (const char *)kcontrol->private_value;
2005
2006 mutex_lock(&codec->mutex);
2007
2008 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002009 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002010
2011 mutex_unlock(&codec->mutex);
2012
2013 return 0;
2014}
2015EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2016
2017/**
2018 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2019 *
2020 * @kcontrol: mixer control
2021 * @ucontrol: Value
2022 */
2023int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2024 struct snd_ctl_elem_value *ucontrol)
2025{
2026 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2027 const char *pin = (const char *)kcontrol->private_value;
2028
2029 mutex_lock(&codec->mutex);
2030
2031 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002032 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002033 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002034 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002035
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002036 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002037
2038 mutex_unlock(&codec->mutex);
2039
2040 return 0;
2041}
2042EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2043
2044/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002045 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002046 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002047 * @widget: widget template
2048 *
2049 * Creates a new dapm control based upon the template.
2050 *
2051 * Returns 0 for success else error.
2052 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002053int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002054 const struct snd_soc_dapm_widget *widget)
2055{
2056 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002057 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002058
2059 if ((w = dapm_cnew_widget(widget)) == NULL)
2060 return -ENOMEM;
2061
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002062 name_len = strlen(widget->name) + 1;
2063 if (dapm->codec->name_prefix)
2064 name_len += 1 + strlen(dapm->codec->name_prefix);
2065 w->name = kmalloc(name_len, GFP_KERNEL);
2066 if (w->name == NULL) {
2067 kfree(w);
2068 return -ENOMEM;
2069 }
2070 if (dapm->codec->name_prefix)
2071 snprintf(w->name, name_len, "%s %s",
2072 dapm->codec->name_prefix, widget->name);
2073 else
2074 snprintf(w->name, name_len, "%s", widget->name);
2075
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002076 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002077 w->dapm = dapm;
2078 w->codec = dapm->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002079 INIT_LIST_HEAD(&w->sources);
2080 INIT_LIST_HEAD(&w->sinks);
2081 INIT_LIST_HEAD(&w->list);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002082 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002083
2084 /* machine layer set ups unconnected pins and insertions */
2085 w->connected = 1;
2086 return 0;
2087}
2088EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2089
2090/**
Mark Brown4ba13272008-05-13 14:51:19 +02002091 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002092 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002093 * @widget: widget array
2094 * @num: number of widgets
2095 *
2096 * Creates new DAPM controls based upon the templates.
2097 *
2098 * Returns 0 for success else error.
2099 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002100int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002101 const struct snd_soc_dapm_widget *widget,
2102 int num)
2103{
2104 int i, ret;
2105
2106 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002107 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002108 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002109 dev_err(dapm->dev,
2110 "ASoC: Failed to create DAPM control %s: %d\n",
2111 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002112 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002113 }
Mark Brown4ba13272008-05-13 14:51:19 +02002114 widget++;
2115 }
2116 return 0;
2117}
2118EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2119
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002120static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002121 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002122{
2123 struct snd_soc_dapm_widget *w;
2124
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002125 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002126 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002127 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002128 continue;
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002129 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2130 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002131 if (strstr(w->sname, stream)) {
2132 switch(event) {
2133 case SND_SOC_DAPM_STREAM_START:
2134 w->active = 1;
2135 break;
2136 case SND_SOC_DAPM_STREAM_STOP:
2137 w->active = 0;
2138 break;
2139 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002140 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002141 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002142 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2143 break;
2144 }
2145 }
2146 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002147
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002148 dapm_power_widgets(dapm, event);
2149}
2150
2151/**
2152 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2153 * @rtd: PCM runtime data
2154 * @stream: stream name
2155 * @event: stream event
2156 *
2157 * Sends a stream event to the dapm core. The core then makes any
2158 * necessary widget power changes.
2159 *
2160 * Returns 0 for success else error.
2161 */
2162int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2163 const char *stream, int event)
2164{
2165 struct snd_soc_codec *codec = rtd->codec;
2166
2167 if (stream == NULL)
2168 return 0;
2169
2170 mutex_lock(&codec->mutex);
2171 soc_dapm_stream_event(&codec->dapm, stream, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002172 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002173 return 0;
2174}
2175EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
2176
2177/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002178 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002179 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002180 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002181 *
Mark Brown74b8f952009-06-06 11:26:15 +01002182 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002183 * a valid audio route and active audio stream.
2184 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2185 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002186 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002187int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002188{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002189 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002190}
Liam Girdwooda5302182008-07-07 13:35:17 +01002191EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002192
2193/**
Mark Brownda341832010-03-15 19:23:37 +00002194 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002195 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002196 * @pin: pin name
2197 *
2198 * Enables input/output pin regardless of any other state. This is
2199 * intended for use with microphone bias supplies used in microphone
2200 * jack detection.
2201 *
2202 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2203 * do any widget power switching.
2204 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002205int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2206 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002207{
2208 struct snd_soc_dapm_widget *w;
2209
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002210 list_for_each_entry(w, &dapm->card->widgets, list) {
2211 if (w->dapm != dapm)
2212 continue;
Mark Brownda341832010-03-15 19:23:37 +00002213 if (!strcmp(w->name, pin)) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002214 dev_dbg(w->dapm->dev,
2215 "dapm: force enable pin %s\n", pin);
Mark Brownda341832010-03-15 19:23:37 +00002216 w->connected = 1;
2217 w->force = 1;
2218 return 0;
2219 }
2220 }
2221
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002222 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Mark Brownda341832010-03-15 19:23:37 +00002223 return -EINVAL;
2224}
2225EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2226
2227/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002228 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002229 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002230 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002231 *
Mark Brown74b8f952009-06-06 11:26:15 +01002232 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002233 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2234 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002235 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002236int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2237 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002238{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002239 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002240}
2241EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2242
2243/**
Mark Brown5817b522008-09-24 11:23:11 +01002244 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002245 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002246 * @pin: pin name
2247 *
2248 * Marks the specified pin as being not connected, disabling it along
2249 * any parent or child widgets. At present this is identical to
2250 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2251 * additional things such as disabling controls which only affect
2252 * paths through the pin.
2253 *
2254 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2255 * do any widget power switching.
2256 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002257int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002258{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002259 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002260}
2261EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2262
2263/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002264 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002265 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002266 * @pin: audio signal pin endpoint (or start point)
2267 *
2268 * Get audio pin status - connected or disconnected.
2269 *
2270 * Returns 1 for connected otherwise 0.
2271 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002272int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2273 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002274{
2275 struct snd_soc_dapm_widget *w;
2276
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002277 list_for_each_entry(w, &dapm->card->widgets, list) {
2278 if (w->dapm != dapm)
2279 continue;
Liam Girdwooda5302182008-07-07 13:35:17 +01002280 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002281 return w->connected;
2282 }
2283
2284 return 0;
2285}
Liam Girdwooda5302182008-07-07 13:35:17 +01002286EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002287
2288/**
Mark Brown1547aba2010-05-07 21:11:40 +01002289 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002290 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01002291 * @pin: audio signal pin endpoint (or start point)
2292 *
2293 * Mark the given endpoint or pin as ignoring suspend. When the
2294 * system is disabled a path between two endpoints flagged as ignoring
2295 * suspend will not be disabled. The path must already be enabled via
2296 * normal means at suspend time, it will not be turned on if it was not
2297 * already enabled.
2298 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002299int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2300 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01002301{
2302 struct snd_soc_dapm_widget *w;
2303
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002304 list_for_each_entry(w, &dapm->card->widgets, list) {
2305 if (w->dapm != dapm)
2306 continue;
Mark Brown1547aba2010-05-07 21:11:40 +01002307 if (!strcmp(w->name, pin)) {
2308 w->ignore_suspend = 1;
2309 return 0;
2310 }
2311 }
2312
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002313 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Mark Brown1547aba2010-05-07 21:11:40 +01002314 return -EINVAL;
2315}
2316EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2317
2318/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002319 * snd_soc_dapm_free - free dapm resources
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002320 * @card: SoC device
Richard Purdie2b97eab2006-10-06 18:32:18 +02002321 *
2322 * Free all dapm widgets and resources.
2323 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002324void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002325{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002326 snd_soc_dapm_sys_remove(dapm->dev);
2327 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02002328 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002329}
2330EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2331
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002332static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01002333{
Mark Brown51737472009-06-22 13:16:51 +01002334 struct snd_soc_dapm_widget *w;
2335 LIST_HEAD(down_list);
2336 int powerdown = 0;
2337
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002338 list_for_each_entry(w, &dapm->card->widgets, list) {
2339 if (w->dapm != dapm)
2340 continue;
Mark Brown51737472009-06-22 13:16:51 +01002341 if (w->power) {
2342 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002343 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002344 powerdown = 1;
2345 }
2346 }
2347
2348 /* If there were no widgets to power down we're already in
2349 * standby.
2350 */
2351 if (powerdown) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002352 snd_soc_dapm_set_bias_level(NULL, dapm, SND_SOC_BIAS_PREPARE);
2353 dapm_seq_run(dapm, &down_list, 0, dapm_down_seq);
2354 snd_soc_dapm_set_bias_level(NULL, dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01002355 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002356}
Mark Brown51737472009-06-22 13:16:51 +01002357
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002358/*
2359 * snd_soc_dapm_shutdown - callback for system shutdown
2360 */
2361void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2362{
2363 struct snd_soc_codec *codec;
2364
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002365 list_for_each_entry(codec, &card->codec_dev_list, list) {
2366 soc_dapm_shutdown_codec(&codec->dapm);
2367 snd_soc_dapm_set_bias_level(card, &codec->dapm, SND_SOC_BIAS_OFF);
2368 }
Mark Brown51737472009-06-22 13:16:51 +01002369}
2370
Richard Purdie2b97eab2006-10-06 18:32:18 +02002371/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002372MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002373MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2374MODULE_LICENSE("GPL");