blob: 200ae7cd2f0058b7c7249299c1b353881e4f7b4e [file] [log] [blame]
Richard Purdie2b97eab2006-10-06 18:32:18 +02001/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020012 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
Mark Brown74b8f952009-06-06 11:26:15 +010015 * DACs/ADCs.
Richard Purdie2b97eab2006-10-06 18:32:18 +020016 * o Platform power domain - can support external components i.e. amps and
17 * mic/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +020021 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
24 * Todo:
25 * o DAPM power change sequencing - allow for configurable per
26 * codec sequences.
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
30 */
31
32#include <linux/module.h>
33#include <linux/moduleparam.h>
34#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080035#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020036#include <linux/delay.h>
37#include <linux/pm.h>
38#include <linux/bitops.h>
39#include <linux/platform_device.h>
40#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020041#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020043#include <sound/core.h>
44#include <sound/pcm.h>
45#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020046#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020047#include <sound/initval.h>
48
Mark Brown84e90932010-11-04 00:07:02 -040049#include <trace/events/asoc.h>
50
Richard Purdie2b97eab2006-10-06 18:32:18 +020051/* dapm power sequences - make this per codec in the future */
52static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010053 [snd_soc_dapm_pre] = 0,
54 [snd_soc_dapm_supply] = 1,
55 [snd_soc_dapm_micbias] = 2,
Mark Brown010ff262009-08-17 17:39:22 +010056 [snd_soc_dapm_aif_in] = 3,
57 [snd_soc_dapm_aif_out] = 3,
58 [snd_soc_dapm_mic] = 4,
59 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000060 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010061 [snd_soc_dapm_value_mux] = 5,
62 [snd_soc_dapm_dac] = 6,
63 [snd_soc_dapm_mixer] = 7,
64 [snd_soc_dapm_mixer_named_ctl] = 7,
65 [snd_soc_dapm_pga] = 8,
66 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060067 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010068 [snd_soc_dapm_hp] = 10,
69 [snd_soc_dapm_spk] = 10,
70 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020071};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000072
Richard Purdie2b97eab2006-10-06 18:32:18 +020073static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010074 [snd_soc_dapm_pre] = 0,
75 [snd_soc_dapm_adc] = 1,
76 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010077 [snd_soc_dapm_spk] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060078 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010079 [snd_soc_dapm_pga] = 4,
80 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010081 [snd_soc_dapm_mixer] = 5,
82 [snd_soc_dapm_dac] = 6,
83 [snd_soc_dapm_mic] = 7,
84 [snd_soc_dapm_micbias] = 8,
85 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000086 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010087 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010088 [snd_soc_dapm_aif_in] = 10,
89 [snd_soc_dapm_aif_out] = 10,
90 [snd_soc_dapm_supply] = 11,
91 [snd_soc_dapm_post] = 12,
Richard Purdie2b97eab2006-10-06 18:32:18 +020092};
93
Troy Kisky12ef1932008-10-13 17:42:14 -070094static void pop_wait(u32 pop_time)
Mark Brown15e4c72f2008-07-02 11:51:20 +010095{
96 if (pop_time)
97 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
98}
99
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200100static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c72f2008-07-02 11:51:20 +0100101{
102 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200103 char *buf;
104
105 if (!pop_time)
106 return;
107
108 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
109 if (buf == NULL)
110 return;
Mark Brown15e4c72f2008-07-02 11:51:20 +0100111
112 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200113 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100114 dev_info(dev, "%s", buf);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100115 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200116
117 kfree(buf);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100118}
119
Richard Purdie2b97eab2006-10-06 18:32:18 +0200120/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100121static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200122 const struct snd_soc_dapm_widget *_widget)
123{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100124 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200125}
126
Mark Brown452c5ea2009-05-17 21:41:23 +0100127/**
128 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800129 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100130 * @level: level to configure
131 *
132 * Configure the bias (power) levels for the SoC audio device.
133 *
134 * Returns 0 for success else error.
135 */
Mark Browned5a4c42011-02-18 11:12:42 -0800136static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200137 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100138{
Mark Browned5a4c42011-02-18 11:12:42 -0800139 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100140 int ret = 0;
141
Mark Brownf83fba82009-05-18 15:44:43 +0100142 switch (level) {
143 case SND_SOC_BIAS_ON:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200144 dev_dbg(dapm->dev, "Setting full bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100145 break;
146 case SND_SOC_BIAS_PREPARE:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200147 dev_dbg(dapm->dev, "Setting bias prepare\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100148 break;
149 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200150 dev_dbg(dapm->dev, "Setting standby bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100151 break;
152 case SND_SOC_BIAS_OFF:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200153 dev_dbg(dapm->dev, "Setting bias off\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100154 break;
155 default:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200156 dev_err(dapm->dev, "Setting invalid bias %d\n", level);
Mark Brownf83fba82009-05-18 15:44:43 +0100157 return -EINVAL;
158 }
159
Mark Brown84e90932010-11-04 00:07:02 -0400160 trace_snd_soc_bias_level_start(card, level);
161
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000162 if (card && card->set_bias_level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100163 ret = card->set_bias_level(card, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100164 if (ret == 0) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200165 if (dapm->codec && dapm->codec->driver->set_bias_level)
166 ret = dapm->codec->driver->set_bias_level(dapm->codec, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100167 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200168 dapm->bias_level = level;
Mark Brown474e09c2009-08-19 14:18:53 +0100169 }
Mark Brown1badabd2010-12-04 12:41:04 +0000170 if (ret == 0) {
171 if (card && card->set_bias_level_post)
172 ret = card->set_bias_level_post(card, level);
173 }
Mark Brown452c5ea2009-05-17 21:41:23 +0100174
Mark Brown84e90932010-11-04 00:07:02 -0400175 trace_snd_soc_bias_level_done(card, level);
176
Mark Brown452c5ea2009-05-17 21:41:23 +0100177 return ret;
178}
179
Richard Purdie2b97eab2006-10-06 18:32:18 +0200180/* set up initial codec paths */
181static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
182 struct snd_soc_dapm_path *p, int i)
183{
184 switch (w->id) {
185 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000186 case snd_soc_dapm_mixer:
187 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200188 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100189 struct soc_mixer_control *mc = (struct soc_mixer_control *)
190 w->kcontrols[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400191 unsigned int reg = mc->reg;
192 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100193 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400194 unsigned int mask = (1 << fls(max)) - 1;
195 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200196
197 val = snd_soc_read(w->codec, reg);
198 val = (val >> shift) & mask;
199
200 if ((invert && !val) || (!invert && val))
201 p->connect = 1;
202 else
203 p->connect = 0;
204 }
205 break;
206 case snd_soc_dapm_mux: {
207 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
208 int val, item, bitmask;
209
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100210 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200211 ;
212 val = snd_soc_read(w->codec, e->reg);
213 item = (val >> e->shift_l) & (bitmask - 1);
214
215 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100216 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200217 if (!(strcmp(p->name, e->texts[i])) && item == i)
218 p->connect = 1;
219 }
220 }
221 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000222 case snd_soc_dapm_virt_mux: {
223 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
224
225 p->connect = 0;
226 /* since a virtual mux has no backing registers to
227 * decide which path to connect, it will try to match
228 * with the first enumeration. This is to ensure
229 * that the default mux choice (the first) will be
230 * correctly powered up during initialization.
231 */
232 if (!strcmp(p->name, e->texts[0]))
233 p->connect = 1;
234 }
235 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200236 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200237 struct soc_enum *e = (struct soc_enum *)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200238 w->kcontrols[i].private_value;
239 int val, item;
240
241 val = snd_soc_read(w->codec, e->reg);
242 val = (val >> e->shift_l) & e->mask;
243 for (item = 0; item < e->max; item++) {
244 if (val == e->values[item])
245 break;
246 }
247
248 p->connect = 0;
249 for (i = 0; i < e->max; i++) {
250 if (!(strcmp(p->name, e->texts[i])) && item == i)
251 p->connect = 1;
252 }
253 }
254 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200255 /* does not effect routing - always connected */
256 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600257 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200258 case snd_soc_dapm_output:
259 case snd_soc_dapm_adc:
260 case snd_soc_dapm_input:
261 case snd_soc_dapm_dac:
262 case snd_soc_dapm_micbias:
263 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100264 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100265 case snd_soc_dapm_aif_in:
266 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200267 p->connect = 1;
268 break;
269 /* does effect routing - dynamically connected */
270 case snd_soc_dapm_hp:
271 case snd_soc_dapm_mic:
272 case snd_soc_dapm_spk:
273 case snd_soc_dapm_line:
274 case snd_soc_dapm_pre:
275 case snd_soc_dapm_post:
276 p->connect = 0;
277 break;
278 }
279}
280
Mark Brown74b8f952009-06-06 11:26:15 +0100281/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200282static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200283 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
284 struct snd_soc_dapm_path *path, const char *control_name,
285 const struct snd_kcontrol_new *kcontrol)
286{
287 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
288 int i;
289
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100290 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200291 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200292 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200293 list_add(&path->list_sink, &dest->sources);
294 list_add(&path->list_source, &src->sinks);
295 path->name = (char*)e->texts[i];
296 dapm_set_path_status(dest, path, 0);
297 return 0;
298 }
299 }
300
301 return -ENODEV;
302}
303
Mark Brown74b8f952009-06-06 11:26:15 +0100304/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200305static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200306 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
307 struct snd_soc_dapm_path *path, const char *control_name)
308{
309 int i;
310
311 /* search for mixer kcontrol */
312 for (i = 0; i < dest->num_kcontrols; i++) {
313 if (!strcmp(control_name, dest->kcontrols[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200314 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200315 list_add(&path->list_sink, &dest->sources);
316 list_add(&path->list_source, &src->sinks);
317 path->name = dest->kcontrols[i].name;
318 dapm_set_path_status(dest, path, i);
319 return 0;
320 }
321 }
322 return -ENODEV;
323}
324
325/* update dapm codec register bits */
326static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
327{
328 int change, power;
Daniel Ribeiro46f58222009-06-07 02:49:11 -0300329 unsigned int old, new;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200330 struct snd_soc_codec *codec = widget->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200331 struct snd_soc_dapm_context *dapm = widget->dapm;
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200332 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200333
334 /* check for valid widgets */
335 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
336 widget->id == snd_soc_dapm_output ||
337 widget->id == snd_soc_dapm_hp ||
338 widget->id == snd_soc_dapm_mic ||
339 widget->id == snd_soc_dapm_line ||
340 widget->id == snd_soc_dapm_spk)
341 return 0;
342
343 power = widget->power;
344 if (widget->invert)
345 power = (power ? 0:1);
346
347 old = snd_soc_read(codec, widget->reg);
348 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
349
350 change = old != new;
351 if (change) {
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200352 pop_dbg(dapm->dev, card->pop_time,
353 "pop test %s : %s in %d ms\n",
Troy Kisky12ef1932008-10-13 17:42:14 -0700354 widget->name, widget->power ? "on" : "off",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200355 card->pop_time);
356 pop_wait(card->pop_time);
Mark Brown69224712010-03-03 14:57:09 +0000357 snd_soc_write(codec, widget->reg, new);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200358 }
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200359 dev_dbg(dapm->dev, "reg %x old %x new %x change %d\n", widget->reg,
360 old, new, change);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200361 return change;
362}
363
Richard Purdie2b97eab2006-10-06 18:32:18 +0200364/* create new dapm mixer control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200365static int dapm_new_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200366 struct snd_soc_dapm_widget *w)
367{
368 int i, ret = 0;
Mark Brown219b93f2008-10-28 13:02:31 +0000369 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200370 struct snd_soc_dapm_path *path;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200371 struct snd_card *card = dapm->codec->card->snd_card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200372
373 /* add kcontrol */
374 for (i = 0; i < w->num_kcontrols; i++) {
375
376 /* match name */
377 list_for_each_entry(path, &w->sources, list_sink) {
378
379 /* mixer/mux paths name must match control name */
380 if (path->name != (char*)w->kcontrols[i].name)
381 continue;
382
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000383 /* add dapm control with long name.
384 * for dapm_mixer this is the concatenation of the
385 * mixer and kcontrol name.
386 * for dapm_mixer_named_ctl this is simply the
387 * kcontrol name.
388 */
389 name_len = strlen(w->kcontrols[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000390 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000391 name_len += 1 + strlen(w->name);
392
Mark Brown219b93f2008-10-28 13:02:31 +0000393 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000394
Richard Purdie2b97eab2006-10-06 18:32:18 +0200395 if (path->long_name == NULL)
396 return -ENOMEM;
397
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000398 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000399 default:
400 snprintf(path->long_name, name_len, "%s %s",
401 w->name, w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000402 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000403 case snd_soc_dapm_mixer_named_ctl:
404 snprintf(path->long_name, name_len, "%s",
405 w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000406 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000407 }
408
Mark Brown219b93f2008-10-28 13:02:31 +0000409 path->long_name[name_len - 1] = '\0';
410
Richard Purdie2b97eab2006-10-06 18:32:18 +0200411 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
412 path->long_name);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200413 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200414 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200415 dev_err(dapm->dev,
416 "asoc: failed to add dapm kcontrol %s: %d\n",
417 path->long_name, ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200418 kfree(path->long_name);
419 path->long_name = NULL;
420 return ret;
421 }
422 }
423 }
424 return ret;
425}
426
427/* create new dapm mux control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200428static int dapm_new_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200429 struct snd_soc_dapm_widget *w)
430{
431 struct snd_soc_dapm_path *path = NULL;
432 struct snd_kcontrol *kcontrol;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200433 struct snd_card *card = dapm->codec->card->snd_card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200434 int ret = 0;
435
436 if (!w->num_kcontrols) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200437 dev_err(dapm->dev, "asoc: mux %s has no controls\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200438 return -EINVAL;
439 }
440
441 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200442 ret = snd_ctl_add(card, kcontrol);
443
Richard Purdie2b97eab2006-10-06 18:32:18 +0200444 if (ret < 0)
445 goto err;
446
447 list_for_each_entry(path, &w->sources, list_sink)
448 path->kcontrol = kcontrol;
449
450 return ret;
451
452err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200453 dev_err(dapm->dev, "asoc: failed to add kcontrol %s\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200454 return ret;
455}
456
457/* create new dapm volume control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200458static int dapm_new_pga(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200459 struct snd_soc_dapm_widget *w)
460{
Mark Browna6c65732010-03-03 17:45:21 +0000461 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200462 dev_err(w->dapm->dev,
463 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200464
Mark Browna6c65732010-03-03 17:45:21 +0000465 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200466}
467
468/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200469static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200470{
471 struct snd_soc_dapm_path *p;
472
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200473 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200474 p->walked = 0;
475}
476
Mark Brown9949788b2010-05-07 20:24:05 +0100477/* We implement power down on suspend by checking the power state of
478 * the ALSA card - when we are suspending the ALSA state for the card
479 * is set to D3.
480 */
481static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
482{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200483 int level = snd_power_get_state(widget->dapm->codec->card->snd_card);
Mark Brown9949788b2010-05-07 20:24:05 +0100484
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000485 switch (level) {
Mark Brown9949788b2010-05-07 20:24:05 +0100486 case SNDRV_CTL_POWER_D3hot:
487 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100488 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200489 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
490 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100491 return widget->ignore_suspend;
Mark Brown9949788b2010-05-07 20:24:05 +0100492 default:
493 return 1;
494 }
495}
496
Richard Purdie2b97eab2006-10-06 18:32:18 +0200497/*
498 * Recursively check for a completed path to an active or physically connected
499 * output widget. Returns number of complete paths.
500 */
501static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
502{
503 struct snd_soc_dapm_path *path;
504 int con = 0;
505
Mark Brown246d0a12009-04-22 18:24:55 +0100506 if (widget->id == snd_soc_dapm_supply)
507 return 0;
508
Mark Brown010ff262009-08-17 17:39:22 +0100509 switch (widget->id) {
510 case snd_soc_dapm_adc:
511 case snd_soc_dapm_aif_out:
512 if (widget->active)
Mark Brown9949788b2010-05-07 20:24:05 +0100513 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100514 default:
515 break;
516 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200517
518 if (widget->connected) {
519 /* connected pin ? */
520 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown9949788b2010-05-07 20:24:05 +0100521 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200522
523 /* connected jack or spk ? */
524 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300525 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown9949788b2010-05-07 20:24:05 +0100526 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200527 }
528
529 list_for_each_entry(path, &widget->sinks, list_source) {
530 if (path->walked)
531 continue;
532
533 if (path->sink && path->connect) {
534 path->walked = 1;
535 con += is_connected_output_ep(path->sink);
536 }
537 }
538
539 return con;
540}
541
542/*
543 * Recursively check for a completed path to an active or physically connected
544 * input widget. Returns number of complete paths.
545 */
546static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
547{
548 struct snd_soc_dapm_path *path;
549 int con = 0;
550
Mark Brown246d0a12009-04-22 18:24:55 +0100551 if (widget->id == snd_soc_dapm_supply)
552 return 0;
553
Richard Purdie2b97eab2006-10-06 18:32:18 +0200554 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100555 switch (widget->id) {
556 case snd_soc_dapm_dac:
557 case snd_soc_dapm_aif_in:
558 if (widget->active)
Mark Brown9949788b2010-05-07 20:24:05 +0100559 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100560 default:
561 break;
562 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200563
564 if (widget->connected) {
565 /* connected pin ? */
566 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown9949788b2010-05-07 20:24:05 +0100567 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200568
569 /* connected VMID/Bias for lower pops */
570 if (widget->id == snd_soc_dapm_vmid)
Mark Brown9949788b2010-05-07 20:24:05 +0100571 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200572
573 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300574 if (widget->id == snd_soc_dapm_mic ||
575 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown9949788b2010-05-07 20:24:05 +0100576 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200577 }
578
579 list_for_each_entry(path, &widget->sources, list_sink) {
580 if (path->walked)
581 continue;
582
583 if (path->source && path->connect) {
584 path->walked = 1;
585 con += is_connected_input_ep(path->source);
586 }
587 }
588
589 return con;
590}
591
592/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300593 * Handler for generic register modifier widget.
594 */
595int dapm_reg_event(struct snd_soc_dapm_widget *w,
596 struct snd_kcontrol *kcontrol, int event)
597{
598 unsigned int val;
599
600 if (SND_SOC_DAPM_EVENT_ON(event))
601 val = w->on_val;
602 else
603 val = w->off_val;
604
605 snd_soc_update_bits(w->codec, -(w->reg + 1),
606 w->mask << w->shift, val << w->shift);
607
608 return 0;
609}
Mark Brown11589412008-07-29 11:42:23 +0100610EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300611
Mark Brown025756ec2009-04-13 11:09:18 +0100612/* Standard power change method, used to apply power changes to most
613 * widgets.
614 */
615static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
616{
617 int ret;
618
619 /* call any power change event handlers */
620 if (w->event)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200621 dev_dbg(w->dapm->dev, "power %s event for %s flags %x\n",
Mark Brown025756ec2009-04-13 11:09:18 +0100622 w->power ? "on" : "off",
623 w->name, w->event_flags);
624
625 /* power up pre event */
626 if (w->power && w->event &&
627 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
628 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
629 if (ret < 0)
630 return ret;
631 }
632
633 /* power down pre event */
634 if (!w->power && w->event &&
635 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
636 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
637 if (ret < 0)
638 return ret;
639 }
640
Mark Brown025756ec2009-04-13 11:09:18 +0100641 dapm_update_bits(w);
642
Mark Brown025756ec2009-04-13 11:09:18 +0100643 /* power up post event */
644 if (w->power && w->event &&
645 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
646 ret = w->event(w,
647 NULL, SND_SOC_DAPM_POST_PMU);
648 if (ret < 0)
649 return ret;
650 }
651
652 /* power down post event */
653 if (!w->power && w->event &&
654 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
655 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
656 if (ret < 0)
657 return ret;
658 }
659
660 return 0;
661}
662
Mark Browncd0f2d42009-04-20 16:56:59 +0100663/* Generic check to see if a widget should be powered.
664 */
665static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
666{
667 int in, out;
668
669 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200670 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100671 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200672 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100673 return out != 0 && in != 0;
674}
675
Mark Brown6ea31b92009-04-20 17:15:41 +0100676/* Check to see if an ADC has power */
677static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
678{
679 int in;
680
681 if (w->active) {
682 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200683 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100684 return in != 0;
685 } else {
686 return dapm_generic_check_power(w);
687 }
688}
689
690/* Check to see if a DAC has power */
691static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
692{
693 int out;
694
695 if (w->active) {
696 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200697 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100698 return out != 0;
699 } else {
700 return dapm_generic_check_power(w);
701 }
702}
703
Mark Brown246d0a12009-04-22 18:24:55 +0100704/* Check to see if a power supply is needed */
705static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
706{
707 struct snd_soc_dapm_path *path;
708 int power = 0;
709
710 /* Check if one of our outputs is connected */
711 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +0100712 if (path->connected &&
713 !path->connected(path->source, path->sink))
714 continue;
715
Mark Brown30173582011-02-11 11:42:19 +0000716 if (!path->sink)
717 continue;
718
719 if (path->sink->force) {
720 power = 1;
721 break;
722 }
723
724 if (path->sink->power_check &&
Mark Brown246d0a12009-04-22 18:24:55 +0100725 path->sink->power_check(path->sink)) {
726 power = 1;
727 break;
728 }
729 }
730
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200731 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100732
733 return power;
734}
735
Mark Brown38357ab2009-06-06 19:03:23 +0100736static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
737 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +0000738 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +0000739{
Mark Brown828a8422011-01-15 13:14:30 +0000740 int *sort;
741
742 if (power_up)
743 sort = dapm_up_seq;
744 else
745 sort = dapm_down_seq;
746
Mark Brown38357ab2009-06-06 19:03:23 +0100747 if (sort[a->id] != sort[b->id])
748 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +0000749 if (a->subseq != b->subseq) {
750 if (power_up)
751 return a->subseq - b->subseq;
752 else
753 return b->subseq - a->subseq;
754 }
Mark Brownb22ead22009-06-07 12:51:26 +0100755 if (a->reg != b->reg)
756 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +0000757 if (a->dapm != b->dapm)
758 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +0000759
Mark Brown38357ab2009-06-06 19:03:23 +0100760 return 0;
761}
Mark Brown42aa3412009-03-01 19:21:10 +0000762
Mark Brown38357ab2009-06-06 19:03:23 +0100763/* Insert a widget in order into a DAPM power sequence. */
764static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
765 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +0000766 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +0100767{
768 struct snd_soc_dapm_widget *w;
769
770 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +0000771 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +0100772 list_add_tail(&new_widget->power_list, &w->power_list);
773 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000774 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100775
Mark Brown38357ab2009-06-06 19:03:23 +0100776 list_add_tail(&new_widget->power_list, list);
777}
Mark Brown42aa3412009-03-01 19:21:10 +0000778
Mark Brown68f89ad2010-11-03 23:51:49 -0400779static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
780 struct snd_soc_dapm_widget *w, int event)
781{
782 struct snd_soc_card *card = dapm->card;
783 const char *ev_name;
784 int power, ret;
785
786 switch (event) {
787 case SND_SOC_DAPM_PRE_PMU:
788 ev_name = "PRE_PMU";
789 power = 1;
790 break;
791 case SND_SOC_DAPM_POST_PMU:
792 ev_name = "POST_PMU";
793 power = 1;
794 break;
795 case SND_SOC_DAPM_PRE_PMD:
796 ev_name = "PRE_PMD";
797 power = 0;
798 break;
799 case SND_SOC_DAPM_POST_PMD:
800 ev_name = "POST_PMD";
801 power = 0;
802 break;
803 default:
804 BUG();
805 return;
806 }
807
808 if (w->power != power)
809 return;
810
811 if (w->event && (w->event_flags & event)) {
812 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
813 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -0400814 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400815 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -0400816 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400817 if (ret < 0)
818 pr_err("%s: %s event failed: %d\n",
819 ev_name, w->name, ret);
820 }
821}
822
Mark Brownb22ead22009-06-07 12:51:26 +0100823/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200824static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +0100825 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100826{
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200827 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -0400828 struct snd_soc_dapm_widget *w;
829 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +0100830 unsigned int value = 0;
831 unsigned int mask = 0;
832 unsigned int cur_mask;
833
834 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
835 power_list)->reg;
836
837 list_for_each_entry(w, pending, power_list) {
838 cur_mask = 1 << w->shift;
839 BUG_ON(reg != w->reg);
840
841 if (w->invert)
842 power = !w->power;
843 else
844 power = w->power;
845
846 mask |= cur_mask;
847 if (power)
848 value |= cur_mask;
849
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200850 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +0100851 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
852 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100853
Mark Brown68f89ad2010-11-03 23:51:49 -0400854 /* Check for events */
855 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
856 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100857 }
858
Mark Brown81628102009-06-07 13:21:24 +0100859 if (reg >= 0) {
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200860 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +0100861 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200862 value, mask, reg, card->pop_time);
863 pop_wait(card->pop_time);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200864 snd_soc_update_bits(dapm->codec, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +0100865 }
866
867 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -0400868 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
869 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +0000870 }
Mark Brown42aa3412009-03-01 19:21:10 +0000871}
872
Mark Brownb22ead22009-06-07 12:51:26 +0100873/* Apply a DAPM power sequence.
874 *
875 * We walk over a pre-sorted list of widgets to apply power to. In
876 * order to minimise the number of writes to the device required
877 * multiple widgets will be updated in a single write where possible.
878 * Currently anything that requires more than a single write is not
879 * handled.
880 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200881static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +0000882 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +0100883{
884 struct snd_soc_dapm_widget *w, *n;
885 LIST_HEAD(pending);
886 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +0000887 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +0100888 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200889 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +0000890 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +0000891 int *sort;
892
893 if (power_up)
894 sort = dapm_up_seq;
895 else
896 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +0100897
Mark Brownb22ead22009-06-07 12:51:26 +0100898 list_for_each_entry_safe(w, n, list, power_list) {
899 ret = 0;
900
901 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200902 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +0000903 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +0100904 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200905 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +0100906
Mark Brown474b62d2011-01-18 16:14:44 +0000907 if (cur_dapm && cur_dapm->seq_notifier) {
908 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
909 if (sort[i] == cur_sort)
910 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +0000911 i,
912 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +0000913 }
914
Mark Brownb22ead22009-06-07 12:51:26 +0100915 INIT_LIST_HEAD(&pending);
916 cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +0000917 cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +0100918 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200919 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +0100920 }
921
Mark Brown163cac02009-06-07 10:12:52 +0100922 switch (w->id) {
923 case snd_soc_dapm_pre:
924 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100925 list_for_each_entry_safe_continue(w, n, list,
926 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100927
Mark Brownb22ead22009-06-07 12:51:26 +0100928 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100929 ret = w->event(w,
930 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100931 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100932 ret = w->event(w,
933 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +0100934 break;
935
936 case snd_soc_dapm_post:
937 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100938 list_for_each_entry_safe_continue(w, n, list,
939 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100940
Mark Brownb22ead22009-06-07 12:51:26 +0100941 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100942 ret = w->event(w,
943 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100944 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100945 ret = w->event(w,
946 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100947 break;
948
949 case snd_soc_dapm_input:
950 case snd_soc_dapm_output:
951 case snd_soc_dapm_hp:
952 case snd_soc_dapm_mic:
953 case snd_soc_dapm_line:
954 case snd_soc_dapm_spk:
955 /* No register support currently */
Mark Brownb22ead22009-06-07 12:51:26 +0100956 ret = dapm_generic_apply_power(w);
Mark Brown163cac02009-06-07 10:12:52 +0100957 break;
958
959 default:
Mark Brown81628102009-06-07 13:21:24 +0100960 /* Queue it up for application */
961 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +0000962 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +0100963 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200964 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +0100965 list_move(&w->power_list, &pending);
966 break;
Mark Brown163cac02009-06-07 10:12:52 +0100967 }
Mark Brownb22ead22009-06-07 12:51:26 +0100968
969 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200970 dev_err(w->dapm->dev,
971 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +0100972 }
Mark Brownb22ead22009-06-07 12:51:26 +0100973
974 if (!list_empty(&pending))
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200975 dapm_seq_run_coalesced(dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +0000976
977 if (cur_dapm && cur_dapm->seq_notifier) {
978 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
979 if (sort[i] == cur_sort)
980 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +0000981 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +0000982 }
Mark Brown163cac02009-06-07 10:12:52 +0100983}
984
Mark Brown97404f22010-12-14 16:13:57 +0000985static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
986{
987 struct snd_soc_dapm_update *update = dapm->update;
988 struct snd_soc_dapm_widget *w;
989 int ret;
990
991 if (!update)
992 return;
993
994 w = update->widget;
995
996 if (w->event &&
997 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
998 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
999 if (ret != 0)
1000 pr_err("%s DAPM pre-event failed: %d\n",
1001 w->name, ret);
1002 }
1003
1004 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1005 update->val);
1006 if (ret < 0)
1007 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1008
1009 if (w->event &&
1010 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1011 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1012 if (ret != 0)
1013 pr_err("%s DAPM post-event failed: %d\n",
1014 w->name, ret);
1015 }
1016}
1017
Mark Brown9d0624a2011-02-18 11:49:43 -08001018/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1019 * they're changing state.
1020 */
1021static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1022{
1023 struct snd_soc_dapm_context *d = data;
1024 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001025
Mark Brown9d0624a2011-02-18 11:49:43 -08001026 if (d->dev_power && d->bias_level == SND_SOC_BIAS_OFF) {
1027 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1028 if (ret != 0)
1029 dev_err(d->dev,
1030 "Failed to turn on bias: %d\n", ret);
1031 }
1032
1033 /* If we're changing to all on or all off then prepare */
1034 if ((d->dev_power && d->bias_level == SND_SOC_BIAS_STANDBY) ||
1035 (!d->dev_power && d->bias_level == SND_SOC_BIAS_ON)) {
1036 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1037 if (ret != 0)
1038 dev_err(d->dev,
1039 "Failed to prepare bias: %d\n", ret);
1040 }
1041}
1042
1043/* Async callback run prior to DAPM sequences - brings to their final
1044 * state.
1045 */
1046static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1047{
1048 struct snd_soc_dapm_context *d = data;
1049 int ret;
1050
1051 /* If we just powered the last thing off drop to standby bias */
1052 if (d->bias_level == SND_SOC_BIAS_PREPARE && !d->dev_power) {
1053 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1054 if (ret != 0)
1055 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1056 ret);
1057 }
1058
1059 /* If we're in standby and can support bias off then do that */
1060 if (d->bias_level == SND_SOC_BIAS_STANDBY && d->idle_bias_off) {
1061 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1062 if (ret != 0)
1063 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1064 }
1065
1066 /* If we just powered up then move to active bias */
1067 if (d->bias_level == SND_SOC_BIAS_PREPARE && d->dev_power) {
1068 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1069 if (ret != 0)
1070 dev_err(d->dev, "Failed to apply active bias: %d\n",
1071 ret);
1072 }
1073}
Mark Brown97404f22010-12-14 16:13:57 +00001074
Mark Brown42aa3412009-03-01 19:21:10 +00001075/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001076 * Scan each dapm widget for complete audio path.
1077 * A complete path is a route that has valid endpoints i.e.:-
1078 *
1079 * o DAC to output pin.
1080 * o Input Pin to ADC.
1081 * o Input pin to Output pin (bypass, sidetone)
1082 * o DAC to ADC (loopback).
1083 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001084static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001085{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001086 struct snd_soc_card *card = dapm->codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001087 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001088 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001089 LIST_HEAD(up_list);
1090 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001091 LIST_HEAD(async_domain);
Mark Brown38357ab2009-06-06 19:03:23 +01001092 int power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001093
Mark Brown84e90932010-11-04 00:07:02 -04001094 trace_snd_soc_dapm_start(card);
1095
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001096 list_for_each_entry(d, &card->dapm_list, list)
1097 if (d->n_widgets)
1098 d->dev_power = 0;
1099
Mark Brown6d3ddc82009-05-16 17:47:29 +01001100 /* Check which widgets we need to power and store them in
1101 * lists indicating if they should be powered up or down.
1102 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001103 list_for_each_entry(w, &card->widgets, list) {
Mark Brown6d3ddc82009-05-16 17:47:29 +01001104 switch (w->id) {
1105 case snd_soc_dapm_pre:
Mark Brown828a8422011-01-15 13:14:30 +00001106 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001107 break;
1108 case snd_soc_dapm_post:
Mark Brown828a8422011-01-15 13:14:30 +00001109 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001110 break;
1111
1112 default:
1113 if (!w->power_check)
1114 continue;
1115
Mark Brown9949788b2010-05-07 20:24:05 +01001116 if (!w->force)
Mark Brown50b6bce2009-11-23 13:11:53 +00001117 power = w->power_check(w);
Mark Brown9949788b2010-05-07 20:24:05 +01001118 else
1119 power = 1;
1120 if (power)
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001121 w->dapm->dev_power = 1;
Mark Brown452c5ea2009-05-17 21:41:23 +01001122
Mark Brown6d3ddc82009-05-16 17:47:29 +01001123 if (w->power == power)
1124 continue;
1125
Mark Brown84e90932010-11-04 00:07:02 -04001126 trace_snd_soc_dapm_widget_power(w, power);
1127
Mark Brown6d3ddc82009-05-16 17:47:29 +01001128 if (power)
Mark Brown828a8422011-01-15 13:14:30 +00001129 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001130 else
Mark Brown828a8422011-01-15 13:14:30 +00001131 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001132
1133 w->power = power;
1134 break;
1135 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001136 }
1137
Mark Brownb14b76a52009-08-17 11:55:38 +01001138 /* If there are no DAPM widgets then try to figure out power from the
1139 * event type.
1140 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001141 if (!dapm->n_widgets) {
Mark Brownb14b76a52009-08-17 11:55:38 +01001142 switch (event) {
1143 case SND_SOC_DAPM_STREAM_START:
1144 case SND_SOC_DAPM_STREAM_RESUME:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001145 dapm->dev_power = 1;
Mark Brownb14b76a52009-08-17 11:55:38 +01001146 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001147 case SND_SOC_DAPM_STREAM_STOP:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001148 dapm->dev_power = !!dapm->codec->active;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001149 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001150 case SND_SOC_DAPM_STREAM_SUSPEND:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001151 dapm->dev_power = 0;
Mark Brown50b6bce2009-11-23 13:11:53 +00001152 break;
Mark Brownb14b76a52009-08-17 11:55:38 +01001153 case SND_SOC_DAPM_STREAM_NOP:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001154 switch (dapm->bias_level) {
Mark Browna96ca332010-01-19 22:49:43 +00001155 case SND_SOC_BIAS_STANDBY:
1156 case SND_SOC_BIAS_OFF:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001157 dapm->dev_power = 0;
Mark Browna96ca332010-01-19 22:49:43 +00001158 break;
1159 default:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001160 dapm->dev_power = 1;
Mark Browna96ca332010-01-19 22:49:43 +00001161 break;
1162 }
Mark Brown50b6bce2009-11-23 13:11:53 +00001163 break;
Mark Brownb14b76a52009-08-17 11:55:38 +01001164 default:
1165 break;
1166 }
1167 }
1168
Mark Brown9d0624a2011-02-18 11:49:43 -08001169 /* Run all the bias changes in parallel */
1170 list_for_each_entry(d, &dapm->card->dapm_list, list)
1171 async_schedule_domain(dapm_pre_sequence_async, d,
1172 &async_domain);
1173 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001174
Mark Brown6d3ddc82009-05-16 17:47:29 +01001175 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001176 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001177
Mark Brown97404f22010-12-14 16:13:57 +00001178 dapm_widget_update(dapm);
1179
Mark Brown6d3ddc82009-05-16 17:47:29 +01001180 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001181 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001182
Mark Brown9d0624a2011-02-18 11:49:43 -08001183 /* Run all the bias changes in parallel */
1184 list_for_each_entry(d, &dapm->card->dapm_list, list)
1185 async_schedule_domain(dapm_post_sequence_async, d,
1186 &async_domain);
1187 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001188
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001189 pop_dbg(dapm->dev, card->pop_time,
1190 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001191 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001192
Mark Brown84e90932010-11-04 00:07:02 -04001193 trace_snd_soc_dapm_done(card);
1194
Mark Brown42aa3412009-03-01 19:21:10 +00001195 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001196}
1197
Mark Brown79fb9382009-08-21 16:38:13 +01001198#ifdef CONFIG_DEBUG_FS
1199static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1200{
1201 file->private_data = inode->i_private;
1202 return 0;
1203}
1204
1205static ssize_t dapm_widget_power_read_file(struct file *file,
1206 char __user *user_buf,
1207 size_t count, loff_t *ppos)
1208{
1209 struct snd_soc_dapm_widget *w = file->private_data;
1210 char *buf;
1211 int in, out;
1212 ssize_t ret;
1213 struct snd_soc_dapm_path *p = NULL;
1214
1215 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1216 if (!buf)
1217 return -ENOMEM;
1218
1219 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001220 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001221 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001222 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001223
Mark Brownd033c362009-12-04 15:25:56 +00001224 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001225 w->name, w->power ? "On" : "Off", in, out);
1226
Mark Brownd033c362009-12-04 15:25:56 +00001227 if (w->reg >= 0)
1228 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1229 " - R%d(0x%x) bit %d",
1230 w->reg, w->reg, w->shift);
1231
1232 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1233
Mark Brown3eef08b2009-09-14 16:49:00 +01001234 if (w->sname)
1235 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1236 w->sname,
1237 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001238
1239 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001240 if (p->connected && !p->connected(w, p->sink))
1241 continue;
1242
Mark Brown79fb9382009-08-21 16:38:13 +01001243 if (p->connect)
1244 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1245 " in %s %s\n",
1246 p->name ? p->name : "static",
1247 p->source->name);
1248 }
1249 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001250 if (p->connected && !p->connected(w, p->sink))
1251 continue;
1252
Mark Brown79fb9382009-08-21 16:38:13 +01001253 if (p->connect)
1254 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1255 " out %s %s\n",
1256 p->name ? p->name : "static",
1257 p->sink->name);
1258 }
1259
1260 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1261
1262 kfree(buf);
1263 return ret;
1264}
1265
1266static const struct file_operations dapm_widget_power_fops = {
1267 .open = dapm_widget_power_open_file,
1268 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001269 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001270};
1271
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001272void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001273{
1274 struct snd_soc_dapm_widget *w;
1275 struct dentry *d;
1276
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001277 if (!dapm->debugfs_dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001278 return;
1279
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001280 list_for_each_entry(w, &dapm->card->widgets, list) {
1281 if (!w->name || w->dapm != dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001282 continue;
1283
1284 d = debugfs_create_file(w->name, 0444,
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001285 dapm->debugfs_dapm, w,
Mark Brown79fb9382009-08-21 16:38:13 +01001286 &dapm_widget_power_fops);
1287 if (!d)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001288 dev_warn(w->dapm->dev,
1289 "ASoC: Failed to create %s debugfs file\n",
1290 w->name);
Mark Brown79fb9382009-08-21 16:38:13 +01001291 }
1292}
1293#else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001294void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001295{
1296}
1297#endif
1298
Richard Purdie2b97eab2006-10-06 18:32:18 +02001299/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001300static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001301 struct snd_kcontrol *kcontrol, int change,
1302 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001303{
1304 struct snd_soc_dapm_path *path;
1305 int found = 0;
1306
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001307 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001308 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001309 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001310 return -ENODEV;
1311
Mark Brown3a655772009-10-05 17:23:30 +01001312 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001313 return 0;
1314
1315 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001316 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001317 if (path->kcontrol != kcontrol)
1318 continue;
1319
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001320 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001321 continue;
1322
1323 found = 1;
1324 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001325 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001326 path->connect = 1; /* new connection */
1327 else
1328 path->connect = 0; /* old connection must be powered down */
1329 }
1330
Mark Brownb91b8fa2010-01-20 18:18:35 +00001331 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001332 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001333
1334 return 0;
1335}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001336
Milan plzik1b075e32008-01-10 14:39:46 +01001337/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001338static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001339 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001340{
1341 struct snd_soc_dapm_path *path;
1342 int found = 0;
1343
Milan plzik1b075e32008-01-10 14:39:46 +01001344 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001345 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001346 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001347 return -ENODEV;
1348
Richard Purdie2b97eab2006-10-06 18:32:18 +02001349 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001350 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001351 if (path->kcontrol != kcontrol)
1352 continue;
1353
1354 /* found, now check type */
1355 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001356 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001357 break;
1358 }
1359
Mark Brownb91b8fa2010-01-20 18:18:35 +00001360 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001361 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001362
1363 return 0;
1364}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001365
1366/* show dapm widget status in sys fs */
1367static ssize_t dapm_widget_show(struct device *dev,
1368 struct device_attribute *attr, char *buf)
1369{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001370 struct snd_soc_pcm_runtime *rtd =
1371 container_of(dev, struct snd_soc_pcm_runtime, dev);
1372 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001373 struct snd_soc_dapm_widget *w;
1374 int count = 0;
1375 char *state = "not set";
1376
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001377 list_for_each_entry(w, &codec->card->widgets, list) {
1378 if (w->dapm != &codec->dapm)
1379 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001380
1381 /* only display widgets that burnm power */
1382 switch (w->id) {
1383 case snd_soc_dapm_hp:
1384 case snd_soc_dapm_mic:
1385 case snd_soc_dapm_spk:
1386 case snd_soc_dapm_line:
1387 case snd_soc_dapm_micbias:
1388 case snd_soc_dapm_dac:
1389 case snd_soc_dapm_adc:
1390 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001391 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001392 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001393 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001394 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001395 if (w->name)
1396 count += sprintf(buf + count, "%s: %s\n",
1397 w->name, w->power ? "On":"Off");
1398 break;
1399 default:
1400 break;
1401 }
1402 }
1403
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001404 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001405 case SND_SOC_BIAS_ON:
1406 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001407 break;
Mark Brown0be98982008-05-19 12:31:28 +02001408 case SND_SOC_BIAS_PREPARE:
1409 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001410 break;
Mark Brown0be98982008-05-19 12:31:28 +02001411 case SND_SOC_BIAS_STANDBY:
1412 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001413 break;
Mark Brown0be98982008-05-19 12:31:28 +02001414 case SND_SOC_BIAS_OFF:
1415 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001416 break;
1417 }
1418 count += sprintf(buf + count, "PM State: %s\n", state);
1419
1420 return count;
1421}
1422
1423static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1424
1425int snd_soc_dapm_sys_add(struct device *dev)
1426{
Troy Kisky12ef1932008-10-13 17:42:14 -07001427 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001428}
1429
1430static void snd_soc_dapm_sys_remove(struct device *dev)
1431{
Mark Brownaef90842009-05-16 17:53:16 +01001432 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001433}
1434
1435/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001436static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001437{
1438 struct snd_soc_dapm_widget *w, *next_w;
1439 struct snd_soc_dapm_path *p, *next_p;
1440
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001441 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1442 if (w->dapm != dapm)
1443 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001444 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001445 /*
1446 * remove source and sink paths associated to this widget.
1447 * While removing the path, remove reference to it from both
1448 * source and sink widgets so that path is removed only once.
1449 */
1450 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1451 list_del(&p->list_sink);
1452 list_del(&p->list_source);
1453 list_del(&p->list);
1454 kfree(p->long_name);
1455 kfree(p);
1456 }
1457 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1458 list_del(&p->list_sink);
1459 list_del(&p->list_source);
1460 list_del(&p->list);
1461 kfree(p->long_name);
1462 kfree(p);
1463 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001464 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001465 kfree(w);
1466 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001467}
1468
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001469static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001470 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001471{
1472 struct snd_soc_dapm_widget *w;
1473
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001474 list_for_each_entry(w, &dapm->card->widgets, list) {
1475 if (w->dapm != dapm)
1476 continue;
Liam Girdwooda5302182008-07-07 13:35:17 +01001477 if (!strcmp(w->name, pin)) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001478 dev_dbg(w->dapm->dev, "dapm: pin %s = %d\n",
1479 pin, status);
Liam Girdwooda5302182008-07-07 13:35:17 +01001480 w->connected = status;
Mark Brown5b9e87c2010-03-22 13:36:13 +00001481 /* Allow disabling of forced pins */
1482 if (status == 0)
1483 w->force = 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001484 return 0;
1485 }
1486 }
1487
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001488 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001489 return -EINVAL;
1490}
1491
Richard Purdie2b97eab2006-10-06 18:32:18 +02001492/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001493 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001494 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001495 *
1496 * Walks all dapm audio paths and powers widgets according to their
1497 * stream or path usage.
1498 *
1499 * Returns 0 for success.
1500 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001501int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001502{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001503 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001504}
Liam Girdwooda5302182008-07-07 13:35:17 +01001505EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001506
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001507static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001508 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001509{
1510 struct snd_soc_dapm_path *path;
1511 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001512 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001513 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001514 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001515 const char *source;
1516 char prefixed_sink[80];
1517 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001518 int ret = 0;
1519
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001520 if (dapm->codec->name_prefix) {
1521 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1522 dapm->codec->name_prefix, route->sink);
1523 sink = prefixed_sink;
1524 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1525 dapm->codec->name_prefix, route->source);
1526 source = prefixed_source;
1527 } else {
1528 sink = route->sink;
1529 source = route->source;
1530 }
1531
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001532 /*
1533 * find src and dest widgets over all widgets but favor a widget from
1534 * current DAPM context
1535 */
1536 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001537 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001538 wtsink = w;
1539 if (w->dapm == dapm)
1540 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001541 continue;
1542 }
1543 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001544 wtsource = w;
1545 if (w->dapm == dapm)
1546 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001547 }
1548 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001549 /* use widget from another DAPM context if not found from this */
1550 if (!wsink)
1551 wsink = wtsink;
1552 if (!wsource)
1553 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001554
1555 if (wsource == NULL || wsink == NULL)
1556 return -ENODEV;
1557
1558 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1559 if (!path)
1560 return -ENOMEM;
1561
1562 path->source = wsource;
1563 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001564 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001565 INIT_LIST_HEAD(&path->list);
1566 INIT_LIST_HEAD(&path->list_source);
1567 INIT_LIST_HEAD(&path->list_sink);
1568
1569 /* check for external widgets */
1570 if (wsink->id == snd_soc_dapm_input) {
1571 if (wsource->id == snd_soc_dapm_micbias ||
1572 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001573 wsource->id == snd_soc_dapm_line ||
1574 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001575 wsink->ext = 1;
1576 }
1577 if (wsource->id == snd_soc_dapm_output) {
1578 if (wsink->id == snd_soc_dapm_spk ||
1579 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001580 wsink->id == snd_soc_dapm_line ||
1581 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001582 wsource->ext = 1;
1583 }
1584
1585 /* connect static paths */
1586 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001587 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001588 list_add(&path->list_sink, &wsink->sources);
1589 list_add(&path->list_source, &wsource->sinks);
1590 path->connect = 1;
1591 return 0;
1592 }
1593
1594 /* connect dynamic paths */
1595 switch(wsink->id) {
1596 case snd_soc_dapm_adc:
1597 case snd_soc_dapm_dac:
1598 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001599 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001600 case snd_soc_dapm_input:
1601 case snd_soc_dapm_output:
1602 case snd_soc_dapm_micbias:
1603 case snd_soc_dapm_vmid:
1604 case snd_soc_dapm_pre:
1605 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001606 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001607 case snd_soc_dapm_aif_in:
1608 case snd_soc_dapm_aif_out:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001609 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001610 list_add(&path->list_sink, &wsink->sources);
1611 list_add(&path->list_source, &wsource->sinks);
1612 path->connect = 1;
1613 return 0;
1614 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001615 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001616 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001617 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Richard Purdie2b97eab2006-10-06 18:32:18 +02001618 &wsink->kcontrols[0]);
1619 if (ret != 0)
1620 goto err;
1621 break;
1622 case snd_soc_dapm_switch:
1623 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001624 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001625 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001626 if (ret != 0)
1627 goto err;
1628 break;
1629 case snd_soc_dapm_hp:
1630 case snd_soc_dapm_mic:
1631 case snd_soc_dapm_line:
1632 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001633 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001634 list_add(&path->list_sink, &wsink->sources);
1635 list_add(&path->list_source, &wsource->sinks);
1636 path->connect = 0;
1637 return 0;
1638 }
1639 return 0;
1640
1641err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001642 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1643 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001644 kfree(path);
1645 return ret;
1646}
Mark Brown105f1c22008-05-13 14:52:19 +02001647
1648/**
Mark Brown105f1c22008-05-13 14:52:19 +02001649 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001650 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02001651 * @route: audio routes
1652 * @num: number of routes
1653 *
1654 * Connects 2 dapm widgets together via a named audio path. The sink is
1655 * the widget receiving the audio signal, whilst the source is the sender
1656 * of the audio signal.
1657 *
1658 * Returns 0 for success else error. On error all resources can be freed
1659 * with a call to snd_soc_card_free().
1660 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001661int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02001662 const struct snd_soc_dapm_route *route, int num)
1663{
1664 int i, ret;
1665
1666 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001667 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001668 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001669 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1670 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02001671 return ret;
1672 }
1673 route++;
1674 }
1675
1676 return 0;
1677}
1678EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1679
1680/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001681 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001682 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001683 *
1684 * Checks the codec for any new dapm widgets and creates them if found.
1685 *
1686 * Returns 0 for success.
1687 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001688int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001689{
1690 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00001691 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001692
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001693 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001694 {
1695 if (w->new)
1696 continue;
1697
1698 switch(w->id) {
1699 case snd_soc_dapm_switch:
1700 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001701 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001702 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001703 dapm_new_mixer(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001704 break;
1705 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001706 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001707 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001708 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001709 dapm_new_mux(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001710 break;
1711 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01001712 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01001713 w->power_check = dapm_adc_check_power;
1714 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001715 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01001716 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01001717 w->power_check = dapm_dac_check_power;
1718 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001719 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001720 case snd_soc_dapm_out_drv:
Mark Brownb75576d2009-04-20 17:56:13 +01001721 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001722 dapm_new_pga(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001723 break;
1724 case snd_soc_dapm_input:
1725 case snd_soc_dapm_output:
1726 case snd_soc_dapm_micbias:
1727 case snd_soc_dapm_spk:
1728 case snd_soc_dapm_hp:
1729 case snd_soc_dapm_mic:
1730 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001731 w->power_check = dapm_generic_check_power;
1732 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001733 case snd_soc_dapm_supply:
1734 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001735 case snd_soc_dapm_vmid:
1736 case snd_soc_dapm_pre:
1737 case snd_soc_dapm_post:
1738 break;
1739 }
Mark Brownb66a70d2011-02-09 18:04:11 +00001740
1741 /* Read the initial power state from the device */
1742 if (w->reg >= 0) {
1743 val = snd_soc_read(w->codec, w->reg);
1744 val &= 1 << w->shift;
1745 if (w->invert)
1746 val = !val;
1747
1748 if (val)
1749 w->power = 1;
1750 }
1751
Richard Purdie2b97eab2006-10-06 18:32:18 +02001752 w->new = 1;
1753 }
1754
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001755 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001756 return 0;
1757}
1758EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1759
1760/**
1761 * snd_soc_dapm_get_volsw - dapm mixer get callback
1762 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001763 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001764 *
1765 * Callback to get the value of a dapm mixer control.
1766 *
1767 * Returns 0 for success.
1768 */
1769int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1770 struct snd_ctl_elem_value *ucontrol)
1771{
1772 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001773 struct soc_mixer_control *mc =
1774 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001775 unsigned int reg = mc->reg;
1776 unsigned int shift = mc->shift;
1777 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001778 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001779 unsigned int invert = mc->invert;
1780 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001781
Richard Purdie2b97eab2006-10-06 18:32:18 +02001782 ucontrol->value.integer.value[0] =
1783 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1784 if (shift != rshift)
1785 ucontrol->value.integer.value[1] =
1786 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1787 if (invert) {
1788 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001789 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001790 if (shift != rshift)
1791 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001792 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001793 }
1794
1795 return 0;
1796}
1797EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1798
1799/**
1800 * snd_soc_dapm_put_volsw - dapm mixer set callback
1801 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001802 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001803 *
1804 * Callback to set the value of a dapm mixer control.
1805 *
1806 * Returns 0 for success.
1807 */
1808int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1809 struct snd_ctl_elem_value *ucontrol)
1810{
1811 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001812 struct soc_mixer_control *mc =
1813 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001814 unsigned int reg = mc->reg;
1815 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001816 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001817 unsigned int mask = (1 << fls(max)) - 1;
1818 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07001819 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00001820 int connect, change;
1821 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001822
1823 val = (ucontrol->value.integer.value[0] & mask);
1824
1825 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001826 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07001827 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001828 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001829
1830 mutex_lock(&widget->codec->mutex);
1831 widget->value = val;
1832
Stephen Warrene9cf7042011-01-27 14:54:05 -07001833 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00001834 if (change) {
Mark Brown283375c2009-12-07 18:09:03 +00001835 if (val)
1836 /* new connection */
1837 connect = invert ? 0:1;
1838 else
1839 /* old connection must be powered down */
1840 connect = invert ? 1:0;
1841
Mark Brown97404f22010-12-14 16:13:57 +00001842 update.kcontrol = kcontrol;
1843 update.widget = widget;
1844 update.reg = reg;
1845 update.mask = mask;
1846 update.val = val;
1847 widget->dapm->update = &update;
1848
Mark Brown283375c2009-12-07 18:09:03 +00001849 dapm_mixer_update_power(widget, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00001850
1851 widget->dapm->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00001852 }
1853
Richard Purdie2b97eab2006-10-06 18:32:18 +02001854 mutex_unlock(&widget->codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00001855 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001856}
1857EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1858
1859/**
1860 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1861 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001862 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001863 *
1864 * Callback to get the value of a dapm enumerated double mixer control.
1865 *
1866 * Returns 0 for success.
1867 */
1868int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1869 struct snd_ctl_elem_value *ucontrol)
1870{
1871 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1872 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001873 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001874
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001875 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001876 ;
1877 val = snd_soc_read(widget->codec, e->reg);
1878 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1879 if (e->shift_l != e->shift_r)
1880 ucontrol->value.enumerated.item[1] =
1881 (val >> e->shift_r) & (bitmask - 1);
1882
1883 return 0;
1884}
1885EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1886
1887/**
1888 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1889 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001890 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001891 *
1892 * Callback to set the value of a dapm enumerated double mixer control.
1893 *
1894 * Returns 0 for success.
1895 */
1896int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1897 struct snd_ctl_elem_value *ucontrol)
1898{
1899 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1900 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001901 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001902 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00001903 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001904
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001905 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001906 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001907 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001908 return -EINVAL;
1909 mux = ucontrol->value.enumerated.item[0];
1910 val = mux << e->shift_l;
1911 mask = (bitmask - 1) << e->shift_l;
1912 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001913 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001914 return -EINVAL;
1915 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1916 mask |= (bitmask - 1) << e->shift_r;
1917 }
1918
1919 mutex_lock(&widget->codec->mutex);
1920 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01001921 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00001922
1923 update.kcontrol = kcontrol;
1924 update.widget = widget;
1925 update.reg = e->reg;
1926 update.mask = mask;
1927 update.val = val;
1928 widget->dapm->update = &update;
1929
Mark Brown3a655772009-10-05 17:23:30 +01001930 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01001931
Mark Brown97404f22010-12-14 16:13:57 +00001932 widget->dapm->update = NULL;
Mark Brown1642e3d2009-10-05 16:24:26 +01001933
Richard Purdie2b97eab2006-10-06 18:32:18 +02001934 mutex_unlock(&widget->codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00001935 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001936}
1937EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1938
1939/**
Mark Brownd2b247a2009-10-06 15:21:04 +01001940 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1941 * @kcontrol: mixer control
1942 * @ucontrol: control element information
1943 *
1944 * Returns 0 for success.
1945 */
1946int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1947 struct snd_ctl_elem_value *ucontrol)
1948{
1949 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1950
1951 ucontrol->value.enumerated.item[0] = widget->value;
1952
1953 return 0;
1954}
1955EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1956
1957/**
1958 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1959 * @kcontrol: mixer control
1960 * @ucontrol: control element information
1961 *
1962 * Returns 0 for success.
1963 */
1964int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
1965 struct snd_ctl_elem_value *ucontrol)
1966{
1967 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1968 struct soc_enum *e =
1969 (struct soc_enum *)kcontrol->private_value;
1970 int change;
1971 int ret = 0;
1972
1973 if (ucontrol->value.enumerated.item[0] >= e->max)
1974 return -EINVAL;
1975
1976 mutex_lock(&widget->codec->mutex);
1977
1978 change = widget->value != ucontrol->value.enumerated.item[0];
1979 widget->value = ucontrol->value.enumerated.item[0];
1980 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
1981
1982 mutex_unlock(&widget->codec->mutex);
1983 return ret;
1984}
1985EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
1986
1987/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001988 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1989 * callback
1990 * @kcontrol: mixer control
1991 * @ucontrol: control element information
1992 *
1993 * Callback to get the value of a dapm semi enumerated double mixer control.
1994 *
1995 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1996 * used for handling bitfield coded enumeration for example.
1997 *
1998 * Returns 0 for success.
1999 */
2000int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2001 struct snd_ctl_elem_value *ucontrol)
2002{
2003 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002004 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002005 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002006
2007 reg_val = snd_soc_read(widget->codec, e->reg);
2008 val = (reg_val >> e->shift_l) & e->mask;
2009 for (mux = 0; mux < e->max; mux++) {
2010 if (val == e->values[mux])
2011 break;
2012 }
2013 ucontrol->value.enumerated.item[0] = mux;
2014 if (e->shift_l != e->shift_r) {
2015 val = (reg_val >> e->shift_r) & e->mask;
2016 for (mux = 0; mux < e->max; mux++) {
2017 if (val == e->values[mux])
2018 break;
2019 }
2020 ucontrol->value.enumerated.item[1] = mux;
2021 }
2022
2023 return 0;
2024}
2025EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2026
2027/**
2028 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2029 * callback
2030 * @kcontrol: mixer control
2031 * @ucontrol: control element information
2032 *
2033 * Callback to set the value of a dapm semi enumerated double mixer control.
2034 *
2035 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2036 * used for handling bitfield coded enumeration for example.
2037 *
2038 * Returns 0 for success.
2039 */
2040int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2041 struct snd_ctl_elem_value *ucontrol)
2042{
2043 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002044 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002045 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002046 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002047 struct snd_soc_dapm_update update;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002048
2049 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2050 return -EINVAL;
2051 mux = ucontrol->value.enumerated.item[0];
2052 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2053 mask = e->mask << e->shift_l;
2054 if (e->shift_l != e->shift_r) {
2055 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2056 return -EINVAL;
2057 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2058 mask |= e->mask << e->shift_r;
2059 }
2060
2061 mutex_lock(&widget->codec->mutex);
2062 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01002063 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002064
2065 update.kcontrol = kcontrol;
2066 update.widget = widget;
2067 update.reg = e->reg;
2068 update.mask = mask;
2069 update.val = val;
2070 widget->dapm->update = &update;
2071
Mark Brown3a655772009-10-05 17:23:30 +01002072 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002073
Mark Brown97404f22010-12-14 16:13:57 +00002074 widget->dapm->update = NULL;
Mark Brown1642e3d2009-10-05 16:24:26 +01002075
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002076 mutex_unlock(&widget->codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002077 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002078}
2079EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2080
2081/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002082 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2083 *
2084 * @kcontrol: mixer control
2085 * @uinfo: control element information
2086 *
2087 * Callback to provide information about a pin switch control.
2088 */
2089int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2090 struct snd_ctl_elem_info *uinfo)
2091{
2092 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2093 uinfo->count = 1;
2094 uinfo->value.integer.min = 0;
2095 uinfo->value.integer.max = 1;
2096
2097 return 0;
2098}
2099EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2100
2101/**
2102 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2103 *
2104 * @kcontrol: mixer control
2105 * @ucontrol: Value
2106 */
2107int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2108 struct snd_ctl_elem_value *ucontrol)
2109{
2110 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2111 const char *pin = (const char *)kcontrol->private_value;
2112
2113 mutex_lock(&codec->mutex);
2114
2115 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002116 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002117
2118 mutex_unlock(&codec->mutex);
2119
2120 return 0;
2121}
2122EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2123
2124/**
2125 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2126 *
2127 * @kcontrol: mixer control
2128 * @ucontrol: Value
2129 */
2130int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2131 struct snd_ctl_elem_value *ucontrol)
2132{
2133 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2134 const char *pin = (const char *)kcontrol->private_value;
2135
2136 mutex_lock(&codec->mutex);
2137
2138 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002139 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002140 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002141 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002142
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002143 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002144
2145 mutex_unlock(&codec->mutex);
2146
2147 return 0;
2148}
2149EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2150
2151/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002152 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002153 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002154 * @widget: widget template
2155 *
2156 * Creates a new dapm control based upon the template.
2157 *
2158 * Returns 0 for success else error.
2159 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002160int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002161 const struct snd_soc_dapm_widget *widget)
2162{
2163 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002164 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002165
2166 if ((w = dapm_cnew_widget(widget)) == NULL)
2167 return -ENOMEM;
2168
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002169 name_len = strlen(widget->name) + 1;
2170 if (dapm->codec->name_prefix)
2171 name_len += 1 + strlen(dapm->codec->name_prefix);
2172 w->name = kmalloc(name_len, GFP_KERNEL);
2173 if (w->name == NULL) {
2174 kfree(w);
2175 return -ENOMEM;
2176 }
2177 if (dapm->codec->name_prefix)
2178 snprintf(w->name, name_len, "%s %s",
2179 dapm->codec->name_prefix, widget->name);
2180 else
2181 snprintf(w->name, name_len, "%s", widget->name);
2182
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002183 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002184 w->dapm = dapm;
2185 w->codec = dapm->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002186 INIT_LIST_HEAD(&w->sources);
2187 INIT_LIST_HEAD(&w->sinks);
2188 INIT_LIST_HEAD(&w->list);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002189 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002190
2191 /* machine layer set ups unconnected pins and insertions */
2192 w->connected = 1;
2193 return 0;
2194}
2195EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2196
2197/**
Mark Brown4ba13272008-05-13 14:51:19 +02002198 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002199 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002200 * @widget: widget array
2201 * @num: number of widgets
2202 *
2203 * Creates new DAPM controls based upon the templates.
2204 *
2205 * Returns 0 for success else error.
2206 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002207int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002208 const struct snd_soc_dapm_widget *widget,
2209 int num)
2210{
2211 int i, ret;
2212
2213 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002214 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002215 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002216 dev_err(dapm->dev,
2217 "ASoC: Failed to create DAPM control %s: %d\n",
2218 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002219 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002220 }
Mark Brown4ba13272008-05-13 14:51:19 +02002221 widget++;
2222 }
2223 return 0;
2224}
2225EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2226
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002227static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002228 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002229{
2230 struct snd_soc_dapm_widget *w;
2231
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002232 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002233 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002234 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002235 continue;
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002236 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2237 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002238 if (strstr(w->sname, stream)) {
2239 switch(event) {
2240 case SND_SOC_DAPM_STREAM_START:
2241 w->active = 1;
2242 break;
2243 case SND_SOC_DAPM_STREAM_STOP:
2244 w->active = 0;
2245 break;
2246 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002247 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002248 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002249 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2250 break;
2251 }
2252 }
2253 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002254
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002255 dapm_power_widgets(dapm, event);
2256}
2257
2258/**
2259 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2260 * @rtd: PCM runtime data
2261 * @stream: stream name
2262 * @event: stream event
2263 *
2264 * Sends a stream event to the dapm core. The core then makes any
2265 * necessary widget power changes.
2266 *
2267 * Returns 0 for success else error.
2268 */
2269int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2270 const char *stream, int event)
2271{
2272 struct snd_soc_codec *codec = rtd->codec;
2273
2274 if (stream == NULL)
2275 return 0;
2276
2277 mutex_lock(&codec->mutex);
2278 soc_dapm_stream_event(&codec->dapm, stream, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002279 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002280 return 0;
2281}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002282
2283/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002284 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002285 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002286 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002287 *
Mark Brown74b8f952009-06-06 11:26:15 +01002288 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002289 * a valid audio route and active audio stream.
2290 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2291 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002292 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002293int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002294{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002295 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002296}
Liam Girdwooda5302182008-07-07 13:35:17 +01002297EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002298
2299/**
Mark Brownda341832010-03-15 19:23:37 +00002300 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002301 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002302 * @pin: pin name
2303 *
2304 * Enables input/output pin regardless of any other state. This is
2305 * intended for use with microphone bias supplies used in microphone
2306 * jack detection.
2307 *
2308 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2309 * do any widget power switching.
2310 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002311int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2312 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002313{
2314 struct snd_soc_dapm_widget *w;
2315
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002316 list_for_each_entry(w, &dapm->card->widgets, list) {
2317 if (w->dapm != dapm)
2318 continue;
Mark Brownda341832010-03-15 19:23:37 +00002319 if (!strcmp(w->name, pin)) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002320 dev_dbg(w->dapm->dev,
2321 "dapm: force enable pin %s\n", pin);
Mark Brownda341832010-03-15 19:23:37 +00002322 w->connected = 1;
2323 w->force = 1;
2324 return 0;
2325 }
2326 }
2327
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002328 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Mark Brownda341832010-03-15 19:23:37 +00002329 return -EINVAL;
2330}
2331EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2332
2333/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002334 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002335 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002336 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002337 *
Mark Brown74b8f952009-06-06 11:26:15 +01002338 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002339 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2340 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002341 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002342int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2343 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002344{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002345 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002346}
2347EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2348
2349/**
Mark Brown5817b522008-09-24 11:23:11 +01002350 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002351 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002352 * @pin: pin name
2353 *
2354 * Marks the specified pin as being not connected, disabling it along
2355 * any parent or child widgets. At present this is identical to
2356 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2357 * additional things such as disabling controls which only affect
2358 * paths through the pin.
2359 *
2360 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2361 * do any widget power switching.
2362 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002363int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002364{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002365 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002366}
2367EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2368
2369/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002370 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002371 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002372 * @pin: audio signal pin endpoint (or start point)
2373 *
2374 * Get audio pin status - connected or disconnected.
2375 *
2376 * Returns 1 for connected otherwise 0.
2377 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002378int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2379 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002380{
2381 struct snd_soc_dapm_widget *w;
2382
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002383 list_for_each_entry(w, &dapm->card->widgets, list) {
2384 if (w->dapm != dapm)
2385 continue;
Liam Girdwooda5302182008-07-07 13:35:17 +01002386 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002387 return w->connected;
2388 }
2389
2390 return 0;
2391}
Liam Girdwooda5302182008-07-07 13:35:17 +01002392EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002393
2394/**
Mark Brown1547aba2010-05-07 21:11:40 +01002395 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002396 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01002397 * @pin: audio signal pin endpoint (or start point)
2398 *
2399 * Mark the given endpoint or pin as ignoring suspend. When the
2400 * system is disabled a path between two endpoints flagged as ignoring
2401 * suspend will not be disabled. The path must already be enabled via
2402 * normal means at suspend time, it will not be turned on if it was not
2403 * already enabled.
2404 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002405int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2406 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01002407{
2408 struct snd_soc_dapm_widget *w;
2409
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002410 list_for_each_entry(w, &dapm->card->widgets, list) {
2411 if (w->dapm != dapm)
2412 continue;
Mark Brown1547aba2010-05-07 21:11:40 +01002413 if (!strcmp(w->name, pin)) {
2414 w->ignore_suspend = 1;
2415 return 0;
2416 }
2417 }
2418
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002419 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Mark Brown1547aba2010-05-07 21:11:40 +01002420 return -EINVAL;
2421}
2422EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2423
2424/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002425 * snd_soc_dapm_free - free dapm resources
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002426 * @card: SoC device
Richard Purdie2b97eab2006-10-06 18:32:18 +02002427 *
2428 * Free all dapm widgets and resources.
2429 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002430void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002431{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002432 snd_soc_dapm_sys_remove(dapm->dev);
2433 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02002434 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002435}
2436EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2437
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002438static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01002439{
Mark Brown51737472009-06-22 13:16:51 +01002440 struct snd_soc_dapm_widget *w;
2441 LIST_HEAD(down_list);
2442 int powerdown = 0;
2443
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002444 list_for_each_entry(w, &dapm->card->widgets, list) {
2445 if (w->dapm != dapm)
2446 continue;
Mark Brown51737472009-06-22 13:16:51 +01002447 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00002448 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002449 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002450 powerdown = 1;
2451 }
2452 }
2453
2454 /* If there were no widgets to power down we're already in
2455 * standby.
2456 */
2457 if (powerdown) {
Mark Browned5a4c42011-02-18 11:12:42 -08002458 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00002459 dapm_seq_run(dapm, &down_list, 0, false);
Mark Browned5a4c42011-02-18 11:12:42 -08002460 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01002461 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002462}
Mark Brown51737472009-06-22 13:16:51 +01002463
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002464/*
2465 * snd_soc_dapm_shutdown - callback for system shutdown
2466 */
2467void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2468{
2469 struct snd_soc_codec *codec;
2470
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002471 list_for_each_entry(codec, &card->codec_dev_list, list) {
2472 soc_dapm_shutdown_codec(&codec->dapm);
Mark Browned5a4c42011-02-18 11:12:42 -08002473 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002474 }
Mark Brown51737472009-06-22 13:16:51 +01002475}
2476
Richard Purdie2b97eab2006-10-06 18:32:18 +02002477/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002478MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002479MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2480MODULE_LICENSE("GPL");