blob: 8454cc2967d69f260271443df79607cecb1edbea [file] [log] [blame]
Richard Purdie2b97eab2006-10-06 18:32:18 +02001/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020012 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
Mark Brown74b8f952009-06-06 11:26:15 +010015 * DACs/ADCs.
Richard Purdie2b97eab2006-10-06 18:32:18 +020016 * o Platform power domain - can support external components i.e. amps and
17 * mic/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +020021 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
24 * Todo:
25 * o DAPM power change sequencing - allow for configurable per
26 * codec sequences.
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
30 */
31
32#include <linux/module.h>
33#include <linux/moduleparam.h>
34#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080035#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020036#include <linux/delay.h>
37#include <linux/pm.h>
38#include <linux/bitops.h>
39#include <linux/platform_device.h>
40#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020041#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020043#include <sound/core.h>
44#include <sound/pcm.h>
45#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020046#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020047#include <sound/initval.h>
48
Mark Brown84e90932010-11-04 00:07:02 -040049#include <trace/events/asoc.h>
50
Richard Purdie2b97eab2006-10-06 18:32:18 +020051/* dapm power sequences - make this per codec in the future */
52static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010053 [snd_soc_dapm_pre] = 0,
54 [snd_soc_dapm_supply] = 1,
55 [snd_soc_dapm_micbias] = 2,
Mark Brown010ff262009-08-17 17:39:22 +010056 [snd_soc_dapm_aif_in] = 3,
57 [snd_soc_dapm_aif_out] = 3,
58 [snd_soc_dapm_mic] = 4,
59 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000060 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010061 [snd_soc_dapm_value_mux] = 5,
62 [snd_soc_dapm_dac] = 6,
63 [snd_soc_dapm_mixer] = 7,
64 [snd_soc_dapm_mixer_named_ctl] = 7,
65 [snd_soc_dapm_pga] = 8,
66 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060067 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010068 [snd_soc_dapm_hp] = 10,
69 [snd_soc_dapm_spk] = 10,
70 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020071};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000072
Richard Purdie2b97eab2006-10-06 18:32:18 +020073static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010074 [snd_soc_dapm_pre] = 0,
75 [snd_soc_dapm_adc] = 1,
76 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010077 [snd_soc_dapm_spk] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060078 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010079 [snd_soc_dapm_pga] = 4,
80 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010081 [snd_soc_dapm_mixer] = 5,
82 [snd_soc_dapm_dac] = 6,
83 [snd_soc_dapm_mic] = 7,
84 [snd_soc_dapm_micbias] = 8,
85 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000086 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010087 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010088 [snd_soc_dapm_aif_in] = 10,
89 [snd_soc_dapm_aif_out] = 10,
90 [snd_soc_dapm_supply] = 11,
91 [snd_soc_dapm_post] = 12,
Richard Purdie2b97eab2006-10-06 18:32:18 +020092};
93
Troy Kisky12ef1932008-10-13 17:42:14 -070094static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +010095{
96 if (pop_time)
97 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
98}
99
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200100static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100101{
102 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200103 char *buf;
104
105 if (!pop_time)
106 return;
107
108 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
109 if (buf == NULL)
110 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100111
112 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200113 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100114 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100115 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200116
117 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100118}
119
Richard Purdie2b97eab2006-10-06 18:32:18 +0200120/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100121static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200122 const struct snd_soc_dapm_widget *_widget)
123{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100124 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200125}
126
Mark Brown452c5ea2009-05-17 21:41:23 +0100127/**
128 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800129 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100130 * @level: level to configure
131 *
132 * Configure the bias (power) levels for the SoC audio device.
133 *
134 * Returns 0 for success else error.
135 */
Mark Browned5a4c42011-02-18 11:12:42 -0800136static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200137 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100138{
Mark Browned5a4c42011-02-18 11:12:42 -0800139 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100140 int ret = 0;
141
Mark Brownf83fba82009-05-18 15:44:43 +0100142 switch (level) {
143 case SND_SOC_BIAS_ON:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200144 dev_dbg(dapm->dev, "Setting full bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100145 break;
146 case SND_SOC_BIAS_PREPARE:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200147 dev_dbg(dapm->dev, "Setting bias prepare\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100148 break;
149 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200150 dev_dbg(dapm->dev, "Setting standby bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100151 break;
152 case SND_SOC_BIAS_OFF:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200153 dev_dbg(dapm->dev, "Setting bias off\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100154 break;
155 default:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200156 dev_err(dapm->dev, "Setting invalid bias %d\n", level);
Mark Brownf83fba82009-05-18 15:44:43 +0100157 return -EINVAL;
158 }
159
Mark Brown84e90932010-11-04 00:07:02 -0400160 trace_snd_soc_bias_level_start(card, level);
161
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000162 if (card && card->set_bias_level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100163 ret = card->set_bias_level(card, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100164 if (ret == 0) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200165 if (dapm->codec && dapm->codec->driver->set_bias_level)
166 ret = dapm->codec->driver->set_bias_level(dapm->codec, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100167 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200168 dapm->bias_level = level;
Mark Brown474e09c2009-08-19 14:18:53 +0100169 }
Mark Brown1badabd2010-12-04 12:41:04 +0000170 if (ret == 0) {
171 if (card && card->set_bias_level_post)
172 ret = card->set_bias_level_post(card, level);
173 }
Mark Brown452c5ea2009-05-17 21:41:23 +0100174
Mark Brown84e90932010-11-04 00:07:02 -0400175 trace_snd_soc_bias_level_done(card, level);
176
Mark Brown452c5ea2009-05-17 21:41:23 +0100177 return ret;
178}
179
Richard Purdie2b97eab2006-10-06 18:32:18 +0200180/* set up initial codec paths */
181static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
182 struct snd_soc_dapm_path *p, int i)
183{
184 switch (w->id) {
185 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000186 case snd_soc_dapm_mixer:
187 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200188 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100189 struct soc_mixer_control *mc = (struct soc_mixer_control *)
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
Richard Purdie2b97eab2006-10-06 18:32:18 +0200325/* create new dapm mixer control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200326static int dapm_new_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200327 struct snd_soc_dapm_widget *w)
328{
329 int i, ret = 0;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000330 size_t name_len, prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200331 struct snd_soc_dapm_path *path;
Mark Brown12ea2c72011-03-02 18:17:32 +0000332 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000333 const char *prefix;
334
335 if (dapm->codec)
336 prefix = dapm->codec->name_prefix;
337 else
338 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200339
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000340 if (prefix)
341 prefix_len = strlen(prefix) + 1;
342 else
343 prefix_len = 0;
344
Richard Purdie2b97eab2006-10-06 18:32:18 +0200345 /* add kcontrol */
346 for (i = 0; i < w->num_kcontrols; i++) {
347
348 /* match name */
349 list_for_each_entry(path, &w->sources, list_sink) {
350
351 /* mixer/mux paths name must match control name */
352 if (path->name != (char*)w->kcontrols[i].name)
353 continue;
354
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000355 /* add dapm control with long name.
356 * for dapm_mixer this is the concatenation of the
357 * mixer and kcontrol name.
358 * for dapm_mixer_named_ctl this is simply the
359 * kcontrol name.
360 */
361 name_len = strlen(w->kcontrols[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000362 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000363 name_len += 1 + strlen(w->name);
364
Mark Brown219b93f2008-10-28 13:02:31 +0000365 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000366
Richard Purdie2b97eab2006-10-06 18:32:18 +0200367 if (path->long_name == NULL)
368 return -ENOMEM;
369
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000370 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000371 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000372 /* The control will get a prefix from
373 * the control creation process but
374 * we're also using the same prefix
375 * for widgets so cut the prefix off
376 * the front of the widget name.
377 */
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000378 snprintf(path->long_name, name_len, "%s %s",
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000379 w->name + prefix_len,
380 w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000381 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000382 case snd_soc_dapm_mixer_named_ctl:
383 snprintf(path->long_name, name_len, "%s",
384 w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000385 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000386 }
387
Mark Brown219b93f2008-10-28 13:02:31 +0000388 path->long_name[name_len - 1] = '\0';
389
Richard Purdie2b97eab2006-10-06 18:32:18 +0200390 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
Mark Brownefb7ac32011-03-08 17:23:24 +0000391 path->long_name, prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200392 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200393 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200394 dev_err(dapm->dev,
395 "asoc: failed to add dapm kcontrol %s: %d\n",
396 path->long_name, ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200397 kfree(path->long_name);
398 path->long_name = NULL;
399 return ret;
400 }
401 }
402 }
403 return ret;
404}
405
406/* create new dapm mux control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200407static int dapm_new_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200408 struct snd_soc_dapm_widget *w)
409{
410 struct snd_soc_dapm_path *path = NULL;
411 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000412 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000413 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000414 size_t prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200415 int ret = 0;
416
417 if (!w->num_kcontrols) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200418 dev_err(dapm->dev, "asoc: mux %s has no controls\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200419 return -EINVAL;
420 }
421
Mark Brownefb7ac32011-03-08 17:23:24 +0000422 if (dapm->codec)
423 prefix = dapm->codec->name_prefix;
424 else
425 prefix = NULL;
426
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000427 if (prefix)
428 prefix_len = strlen(prefix) + 1;
429 else
430 prefix_len = 0;
431
432 /* The control will get a prefix from the control creation
433 * process but we're also using the same prefix for widgets so
434 * cut the prefix off the front of the widget name.
435 */
436 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name + prefix_len,
437 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200438 ret = snd_ctl_add(card, kcontrol);
439
Richard Purdie2b97eab2006-10-06 18:32:18 +0200440 if (ret < 0)
441 goto err;
442
443 list_for_each_entry(path, &w->sources, list_sink)
444 path->kcontrol = kcontrol;
445
446 return ret;
447
448err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200449 dev_err(dapm->dev, "asoc: failed to add kcontrol %s\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200450 return ret;
451}
452
453/* create new dapm volume control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200454static int dapm_new_pga(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200455 struct snd_soc_dapm_widget *w)
456{
Mark Browna6c65732010-03-03 17:45:21 +0000457 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200458 dev_err(w->dapm->dev,
459 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200460
Mark Browna6c65732010-03-03 17:45:21 +0000461 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200462}
463
464/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200465static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200466{
467 struct snd_soc_dapm_path *p;
468
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200469 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200470 p->walked = 0;
471}
472
Mark Brown99497882010-05-07 20:24:05 +0100473/* We implement power down on suspend by checking the power state of
474 * the ALSA card - when we are suspending the ALSA state for the card
475 * is set to D3.
476 */
477static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
478{
Mark Brown12ea2c72011-03-02 18:17:32 +0000479 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100480
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000481 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100482 case SNDRV_CTL_POWER_D3hot:
483 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100484 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200485 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
486 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100487 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100488 default:
489 return 1;
490 }
491}
492
Richard Purdie2b97eab2006-10-06 18:32:18 +0200493/*
494 * Recursively check for a completed path to an active or physically connected
495 * output widget. Returns number of complete paths.
496 */
497static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
498{
499 struct snd_soc_dapm_path *path;
500 int con = 0;
501
Mark Brown246d0a12009-04-22 18:24:55 +0100502 if (widget->id == snd_soc_dapm_supply)
503 return 0;
504
Mark Brown010ff262009-08-17 17:39:22 +0100505 switch (widget->id) {
506 case snd_soc_dapm_adc:
507 case snd_soc_dapm_aif_out:
508 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100509 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100510 default:
511 break;
512 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200513
514 if (widget->connected) {
515 /* connected pin ? */
516 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100517 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200518
519 /* connected jack or spk ? */
520 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300521 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown99497882010-05-07 20:24:05 +0100522 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200523 }
524
525 list_for_each_entry(path, &widget->sinks, list_source) {
526 if (path->walked)
527 continue;
528
529 if (path->sink && path->connect) {
530 path->walked = 1;
531 con += is_connected_output_ep(path->sink);
532 }
533 }
534
535 return con;
536}
537
538/*
539 * Recursively check for a completed path to an active or physically connected
540 * input widget. Returns number of complete paths.
541 */
542static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
543{
544 struct snd_soc_dapm_path *path;
545 int con = 0;
546
Mark Brown246d0a12009-04-22 18:24:55 +0100547 if (widget->id == snd_soc_dapm_supply)
548 return 0;
549
Richard Purdie2b97eab2006-10-06 18:32:18 +0200550 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100551 switch (widget->id) {
552 case snd_soc_dapm_dac:
553 case snd_soc_dapm_aif_in:
554 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100555 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100556 default:
557 break;
558 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200559
560 if (widget->connected) {
561 /* connected pin ? */
562 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100563 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200564
565 /* connected VMID/Bias for lower pops */
566 if (widget->id == snd_soc_dapm_vmid)
Mark Brown99497882010-05-07 20:24:05 +0100567 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200568
569 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300570 if (widget->id == snd_soc_dapm_mic ||
571 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown99497882010-05-07 20:24:05 +0100572 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200573 }
574
575 list_for_each_entry(path, &widget->sources, list_sink) {
576 if (path->walked)
577 continue;
578
579 if (path->source && path->connect) {
580 path->walked = 1;
581 con += is_connected_input_ep(path->source);
582 }
583 }
584
585 return con;
586}
587
588/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300589 * Handler for generic register modifier widget.
590 */
591int dapm_reg_event(struct snd_soc_dapm_widget *w,
592 struct snd_kcontrol *kcontrol, int event)
593{
594 unsigned int val;
595
596 if (SND_SOC_DAPM_EVENT_ON(event))
597 val = w->on_val;
598 else
599 val = w->off_val;
600
601 snd_soc_update_bits(w->codec, -(w->reg + 1),
602 w->mask << w->shift, val << w->shift);
603
604 return 0;
605}
Mark Brown11589412008-07-29 11:42:23 +0100606EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300607
Mark Browncd0f2d42009-04-20 16:56:59 +0100608/* Generic check to see if a widget should be powered.
609 */
610static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
611{
612 int in, out;
613
614 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200615 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100616 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200617 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100618 return out != 0 && in != 0;
619}
620
Mark Brown6ea31b92009-04-20 17:15:41 +0100621/* Check to see if an ADC has power */
622static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
623{
624 int in;
625
626 if (w->active) {
627 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200628 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100629 return in != 0;
630 } else {
631 return dapm_generic_check_power(w);
632 }
633}
634
635/* Check to see if a DAC has power */
636static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
637{
638 int out;
639
640 if (w->active) {
641 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200642 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100643 return out != 0;
644 } else {
645 return dapm_generic_check_power(w);
646 }
647}
648
Mark Brown246d0a12009-04-22 18:24:55 +0100649/* Check to see if a power supply is needed */
650static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
651{
652 struct snd_soc_dapm_path *path;
653 int power = 0;
654
655 /* Check if one of our outputs is connected */
656 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +0100657 if (path->connected &&
658 !path->connected(path->source, path->sink))
659 continue;
660
Mark Brown30173582011-02-11 11:42:19 +0000661 if (!path->sink)
662 continue;
663
664 if (path->sink->force) {
665 power = 1;
666 break;
667 }
668
669 if (path->sink->power_check &&
Mark Brown246d0a12009-04-22 18:24:55 +0100670 path->sink->power_check(path->sink)) {
671 power = 1;
672 break;
673 }
674 }
675
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200676 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100677
678 return power;
679}
680
Mark Brown38357ab2009-06-06 19:03:23 +0100681static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
682 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +0000683 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +0000684{
Mark Brown828a8422011-01-15 13:14:30 +0000685 int *sort;
686
687 if (power_up)
688 sort = dapm_up_seq;
689 else
690 sort = dapm_down_seq;
691
Mark Brown38357ab2009-06-06 19:03:23 +0100692 if (sort[a->id] != sort[b->id])
693 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +0000694 if (a->subseq != b->subseq) {
695 if (power_up)
696 return a->subseq - b->subseq;
697 else
698 return b->subseq - a->subseq;
699 }
Mark Brownb22ead22009-06-07 12:51:26 +0100700 if (a->reg != b->reg)
701 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +0000702 if (a->dapm != b->dapm)
703 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +0000704
Mark Brown38357ab2009-06-06 19:03:23 +0100705 return 0;
706}
Mark Brown42aa3412009-03-01 19:21:10 +0000707
Mark Brown38357ab2009-06-06 19:03:23 +0100708/* Insert a widget in order into a DAPM power sequence. */
709static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
710 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +0000711 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +0100712{
713 struct snd_soc_dapm_widget *w;
714
715 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +0000716 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +0100717 list_add_tail(&new_widget->power_list, &w->power_list);
718 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000719 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100720
Mark Brown38357ab2009-06-06 19:03:23 +0100721 list_add_tail(&new_widget->power_list, list);
722}
Mark Brown42aa3412009-03-01 19:21:10 +0000723
Mark Brown68f89ad2010-11-03 23:51:49 -0400724static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
725 struct snd_soc_dapm_widget *w, int event)
726{
727 struct snd_soc_card *card = dapm->card;
728 const char *ev_name;
729 int power, ret;
730
731 switch (event) {
732 case SND_SOC_DAPM_PRE_PMU:
733 ev_name = "PRE_PMU";
734 power = 1;
735 break;
736 case SND_SOC_DAPM_POST_PMU:
737 ev_name = "POST_PMU";
738 power = 1;
739 break;
740 case SND_SOC_DAPM_PRE_PMD:
741 ev_name = "PRE_PMD";
742 power = 0;
743 break;
744 case SND_SOC_DAPM_POST_PMD:
745 ev_name = "POST_PMD";
746 power = 0;
747 break;
748 default:
749 BUG();
750 return;
751 }
752
753 if (w->power != power)
754 return;
755
756 if (w->event && (w->event_flags & event)) {
757 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
758 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -0400759 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400760 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -0400761 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400762 if (ret < 0)
763 pr_err("%s: %s event failed: %d\n",
764 ev_name, w->name, ret);
765 }
766}
767
Mark Brownb22ead22009-06-07 12:51:26 +0100768/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200769static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +0100770 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100771{
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200772 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -0400773 struct snd_soc_dapm_widget *w;
774 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +0100775 unsigned int value = 0;
776 unsigned int mask = 0;
777 unsigned int cur_mask;
778
779 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
780 power_list)->reg;
781
782 list_for_each_entry(w, pending, power_list) {
783 cur_mask = 1 << w->shift;
784 BUG_ON(reg != w->reg);
785
786 if (w->invert)
787 power = !w->power;
788 else
789 power = w->power;
790
791 mask |= cur_mask;
792 if (power)
793 value |= cur_mask;
794
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200795 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +0100796 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
797 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100798
Mark Brown68f89ad2010-11-03 23:51:49 -0400799 /* Check for events */
800 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
801 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100802 }
803
Mark Brown81628102009-06-07 13:21:24 +0100804 if (reg >= 0) {
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200805 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +0100806 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200807 value, mask, reg, card->pop_time);
808 pop_wait(card->pop_time);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200809 snd_soc_update_bits(dapm->codec, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +0100810 }
811
812 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -0400813 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
814 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +0000815 }
Mark Brown42aa3412009-03-01 19:21:10 +0000816}
817
Mark Brownb22ead22009-06-07 12:51:26 +0100818/* Apply a DAPM power sequence.
819 *
820 * We walk over a pre-sorted list of widgets to apply power to. In
821 * order to minimise the number of writes to the device required
822 * multiple widgets will be updated in a single write where possible.
823 * Currently anything that requires more than a single write is not
824 * handled.
825 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200826static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +0000827 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +0100828{
829 struct snd_soc_dapm_widget *w, *n;
830 LIST_HEAD(pending);
831 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +0000832 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +0100833 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200834 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +0000835 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +0000836 int *sort;
837
838 if (power_up)
839 sort = dapm_up_seq;
840 else
841 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +0100842
Mark Brownb22ead22009-06-07 12:51:26 +0100843 list_for_each_entry_safe(w, n, list, power_list) {
844 ret = 0;
845
846 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200847 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +0000848 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +0100849 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200850 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +0100851
Mark Brown474b62d2011-01-18 16:14:44 +0000852 if (cur_dapm && cur_dapm->seq_notifier) {
853 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
854 if (sort[i] == cur_sort)
855 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +0000856 i,
857 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +0000858 }
859
Mark Brownb22ead22009-06-07 12:51:26 +0100860 INIT_LIST_HEAD(&pending);
861 cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +0000862 cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +0100863 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200864 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +0100865 }
866
Mark Brown163cac02009-06-07 10:12:52 +0100867 switch (w->id) {
868 case snd_soc_dapm_pre:
869 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100870 list_for_each_entry_safe_continue(w, n, list,
871 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100872
Mark Brownb22ead22009-06-07 12:51:26 +0100873 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100874 ret = w->event(w,
875 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100876 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100877 ret = w->event(w,
878 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +0100879 break;
880
881 case snd_soc_dapm_post:
882 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100883 list_for_each_entry_safe_continue(w, n, list,
884 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100885
Mark Brownb22ead22009-06-07 12:51:26 +0100886 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100887 ret = w->event(w,
888 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100889 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100890 ret = w->event(w,
891 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100892 break;
893
Mark Brown163cac02009-06-07 10:12:52 +0100894 default:
Mark Brown81628102009-06-07 13:21:24 +0100895 /* Queue it up for application */
896 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +0000897 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +0100898 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200899 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +0100900 list_move(&w->power_list, &pending);
901 break;
Mark Brown163cac02009-06-07 10:12:52 +0100902 }
Mark Brownb22ead22009-06-07 12:51:26 +0100903
904 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200905 dev_err(w->dapm->dev,
906 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +0100907 }
Mark Brownb22ead22009-06-07 12:51:26 +0100908
909 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +0000910 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +0000911
912 if (cur_dapm && cur_dapm->seq_notifier) {
913 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
914 if (sort[i] == cur_sort)
915 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +0000916 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +0000917 }
Mark Brown163cac02009-06-07 10:12:52 +0100918}
919
Mark Brown97404f22010-12-14 16:13:57 +0000920static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
921{
922 struct snd_soc_dapm_update *update = dapm->update;
923 struct snd_soc_dapm_widget *w;
924 int ret;
925
926 if (!update)
927 return;
928
929 w = update->widget;
930
931 if (w->event &&
932 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
933 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
934 if (ret != 0)
935 pr_err("%s DAPM pre-event failed: %d\n",
936 w->name, ret);
937 }
938
939 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
940 update->val);
941 if (ret < 0)
942 pr_err("%s DAPM update failed: %d\n", w->name, ret);
943
944 if (w->event &&
945 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
946 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
947 if (ret != 0)
948 pr_err("%s DAPM post-event failed: %d\n",
949 w->name, ret);
950 }
951}
952
Mark Brown9d0624a2011-02-18 11:49:43 -0800953/* Async callback run prior to DAPM sequences - brings to _PREPARE if
954 * they're changing state.
955 */
956static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
957{
958 struct snd_soc_dapm_context *d = data;
959 int ret;
Mark Brown97404f22010-12-14 16:13:57 +0000960
Mark Brown9d0624a2011-02-18 11:49:43 -0800961 if (d->dev_power && d->bias_level == SND_SOC_BIAS_OFF) {
962 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
963 if (ret != 0)
964 dev_err(d->dev,
965 "Failed to turn on bias: %d\n", ret);
966 }
967
968 /* If we're changing to all on or all off then prepare */
969 if ((d->dev_power && d->bias_level == SND_SOC_BIAS_STANDBY) ||
970 (!d->dev_power && d->bias_level == SND_SOC_BIAS_ON)) {
971 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
972 if (ret != 0)
973 dev_err(d->dev,
974 "Failed to prepare bias: %d\n", ret);
975 }
976}
977
978/* Async callback run prior to DAPM sequences - brings to their final
979 * state.
980 */
981static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
982{
983 struct snd_soc_dapm_context *d = data;
984 int ret;
985
986 /* If we just powered the last thing off drop to standby bias */
987 if (d->bias_level == SND_SOC_BIAS_PREPARE && !d->dev_power) {
988 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
989 if (ret != 0)
990 dev_err(d->dev, "Failed to apply standby bias: %d\n",
991 ret);
992 }
993
994 /* If we're in standby and can support bias off then do that */
995 if (d->bias_level == SND_SOC_BIAS_STANDBY && d->idle_bias_off) {
996 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
997 if (ret != 0)
998 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
999 }
1000
1001 /* If we just powered up then move to active bias */
1002 if (d->bias_level == SND_SOC_BIAS_PREPARE && d->dev_power) {
1003 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1004 if (ret != 0)
1005 dev_err(d->dev, "Failed to apply active bias: %d\n",
1006 ret);
1007 }
1008}
Mark Brown97404f22010-12-14 16:13:57 +00001009
Mark Brown42aa3412009-03-01 19:21:10 +00001010/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001011 * Scan each dapm widget for complete audio path.
1012 * A complete path is a route that has valid endpoints i.e.:-
1013 *
1014 * o DAC to output pin.
1015 * o Input Pin to ADC.
1016 * o Input pin to Output pin (bypass, sidetone)
1017 * o DAC to ADC (loopback).
1018 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001019static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001020{
Mark Brown12ea2c72011-03-02 18:17:32 +00001021 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001022 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001023 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001024 LIST_HEAD(up_list);
1025 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001026 LIST_HEAD(async_domain);
Mark Brown38357ab2009-06-06 19:03:23 +01001027 int power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001028
Mark Brown84e90932010-11-04 00:07:02 -04001029 trace_snd_soc_dapm_start(card);
1030
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001031 list_for_each_entry(d, &card->dapm_list, list)
1032 if (d->n_widgets)
1033 d->dev_power = 0;
1034
Mark Brown6d3ddc82009-05-16 17:47:29 +01001035 /* Check which widgets we need to power and store them in
1036 * lists indicating if they should be powered up or down.
1037 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001038 list_for_each_entry(w, &card->widgets, list) {
Mark Brown6d3ddc82009-05-16 17:47:29 +01001039 switch (w->id) {
1040 case snd_soc_dapm_pre:
Mark Brown828a8422011-01-15 13:14:30 +00001041 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001042 break;
1043 case snd_soc_dapm_post:
Mark Brown828a8422011-01-15 13:14:30 +00001044 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001045 break;
1046
1047 default:
1048 if (!w->power_check)
1049 continue;
1050
Mark Brown99497882010-05-07 20:24:05 +01001051 if (!w->force)
Mark Brown50b6bce2009-11-23 13:11:53 +00001052 power = w->power_check(w);
Mark Brown99497882010-05-07 20:24:05 +01001053 else
1054 power = 1;
1055 if (power)
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001056 w->dapm->dev_power = 1;
Mark Brown452c5ea2009-05-17 21:41:23 +01001057
Mark Brown6d3ddc82009-05-16 17:47:29 +01001058 if (w->power == power)
1059 continue;
1060
Mark Brown84e90932010-11-04 00:07:02 -04001061 trace_snd_soc_dapm_widget_power(w, power);
1062
Mark Brown6d3ddc82009-05-16 17:47:29 +01001063 if (power)
Mark Brown828a8422011-01-15 13:14:30 +00001064 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001065 else
Mark Brown828a8422011-01-15 13:14:30 +00001066 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001067
1068 w->power = power;
1069 break;
1070 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001071 }
1072
Mark Brownb14b76a2009-08-17 11:55:38 +01001073 /* If there are no DAPM widgets then try to figure out power from the
1074 * event type.
1075 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001076 if (!dapm->n_widgets) {
Mark Brownb14b76a2009-08-17 11:55:38 +01001077 switch (event) {
1078 case SND_SOC_DAPM_STREAM_START:
1079 case SND_SOC_DAPM_STREAM_RESUME:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001080 dapm->dev_power = 1;
Mark Brownb14b76a2009-08-17 11:55:38 +01001081 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001082 case SND_SOC_DAPM_STREAM_STOP:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001083 dapm->dev_power = !!dapm->codec->active;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001084 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001085 case SND_SOC_DAPM_STREAM_SUSPEND:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001086 dapm->dev_power = 0;
Mark Brown50b6bce2009-11-23 13:11:53 +00001087 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001088 case SND_SOC_DAPM_STREAM_NOP:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001089 switch (dapm->bias_level) {
Mark Browna96ca332010-01-19 22:49:43 +00001090 case SND_SOC_BIAS_STANDBY:
1091 case SND_SOC_BIAS_OFF:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001092 dapm->dev_power = 0;
Mark Browna96ca332010-01-19 22:49:43 +00001093 break;
1094 default:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001095 dapm->dev_power = 1;
Mark Browna96ca332010-01-19 22:49:43 +00001096 break;
1097 }
Mark Brown50b6bce2009-11-23 13:11:53 +00001098 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001099 default:
1100 break;
1101 }
1102 }
1103
Mark Brown52ba67b2011-04-04 21:05:11 +09001104 /* Force all contexts in the card to the same bias state */
1105 power = 0;
1106 list_for_each_entry(d, &card->dapm_list, list)
1107 if (d->dev_power)
1108 power = 1;
1109 list_for_each_entry(d, &card->dapm_list, list)
1110 d->dev_power = power;
1111
1112
Mark Brown9d0624a2011-02-18 11:49:43 -08001113 /* Run all the bias changes in parallel */
1114 list_for_each_entry(d, &dapm->card->dapm_list, list)
1115 async_schedule_domain(dapm_pre_sequence_async, d,
1116 &async_domain);
1117 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001118
Mark Brown6d3ddc82009-05-16 17:47:29 +01001119 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001120 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001121
Mark Brown97404f22010-12-14 16:13:57 +00001122 dapm_widget_update(dapm);
1123
Mark Brown6d3ddc82009-05-16 17:47:29 +01001124 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001125 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001126
Mark Brown9d0624a2011-02-18 11:49:43 -08001127 /* Run all the bias changes in parallel */
1128 list_for_each_entry(d, &dapm->card->dapm_list, list)
1129 async_schedule_domain(dapm_post_sequence_async, d,
1130 &async_domain);
1131 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001132
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001133 pop_dbg(dapm->dev, card->pop_time,
1134 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001135 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001136
Mark Brown84e90932010-11-04 00:07:02 -04001137 trace_snd_soc_dapm_done(card);
1138
Mark Brown42aa3412009-03-01 19:21:10 +00001139 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001140}
1141
Mark Brown79fb9382009-08-21 16:38:13 +01001142#ifdef CONFIG_DEBUG_FS
1143static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1144{
1145 file->private_data = inode->i_private;
1146 return 0;
1147}
1148
1149static ssize_t dapm_widget_power_read_file(struct file *file,
1150 char __user *user_buf,
1151 size_t count, loff_t *ppos)
1152{
1153 struct snd_soc_dapm_widget *w = file->private_data;
1154 char *buf;
1155 int in, out;
1156 ssize_t ret;
1157 struct snd_soc_dapm_path *p = NULL;
1158
1159 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1160 if (!buf)
1161 return -ENOMEM;
1162
1163 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001164 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001165 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001166 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001167
Mark Brownd033c362009-12-04 15:25:56 +00001168 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001169 w->name, w->power ? "On" : "Off", in, out);
1170
Mark Brownd033c362009-12-04 15:25:56 +00001171 if (w->reg >= 0)
1172 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1173 " - R%d(0x%x) bit %d",
1174 w->reg, w->reg, w->shift);
1175
1176 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1177
Mark Brown3eef08b2009-09-14 16:49:00 +01001178 if (w->sname)
1179 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1180 w->sname,
1181 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001182
1183 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001184 if (p->connected && !p->connected(w, p->sink))
1185 continue;
1186
Mark Brown79fb9382009-08-21 16:38:13 +01001187 if (p->connect)
1188 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001189 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001190 p->name ? p->name : "static",
1191 p->source->name);
1192 }
1193 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001194 if (p->connected && !p->connected(w, p->sink))
1195 continue;
1196
Mark Brown79fb9382009-08-21 16:38:13 +01001197 if (p->connect)
1198 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001199 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001200 p->name ? p->name : "static",
1201 p->sink->name);
1202 }
1203
1204 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1205
1206 kfree(buf);
1207 return ret;
1208}
1209
1210static const struct file_operations dapm_widget_power_fops = {
1211 .open = dapm_widget_power_open_file,
1212 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001213 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001214};
1215
Mark Brownef49e4f2011-04-04 20:48:13 +09001216static int dapm_bias_open_file(struct inode *inode, struct file *file)
1217{
1218 file->private_data = inode->i_private;
1219 return 0;
1220}
1221
1222static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1223 size_t count, loff_t *ppos)
1224{
1225 struct snd_soc_dapm_context *dapm = file->private_data;
1226 char *level;
1227
1228 switch (dapm->bias_level) {
1229 case SND_SOC_BIAS_ON:
1230 level = "On\n";
1231 break;
1232 case SND_SOC_BIAS_PREPARE:
1233 level = "Prepare\n";
1234 break;
1235 case SND_SOC_BIAS_STANDBY:
1236 level = "Standby\n";
1237 break;
1238 case SND_SOC_BIAS_OFF:
1239 level = "Off\n";
1240 break;
1241 default:
1242 BUG();
1243 level = "Unknown\n";
1244 break;
1245 }
1246
1247 return simple_read_from_buffer(user_buf, count, ppos, level,
1248 strlen(level));
1249}
1250
1251static const struct file_operations dapm_bias_fops = {
1252 .open = dapm_bias_open_file,
1253 .read = dapm_bias_read_file,
1254 .llseek = default_llseek,
1255};
1256
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001257void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1258 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001259{
Mark Brown79fb9382009-08-21 16:38:13 +01001260 struct dentry *d;
1261
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001262 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1263
1264 if (!dapm->debugfs_dapm) {
1265 printk(KERN_WARNING
1266 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001267 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001268 }
Mark Brown79fb9382009-08-21 16:38:13 +01001269
Mark Brownef49e4f2011-04-04 20:48:13 +09001270 d = debugfs_create_file("bias_level", 0444,
1271 dapm->debugfs_dapm, dapm,
1272 &dapm_bias_fops);
1273 if (!d)
1274 dev_warn(dapm->dev,
1275 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001276}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001277
1278static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1279{
1280 struct snd_soc_dapm_context *dapm = w->dapm;
1281 struct dentry *d;
1282
1283 if (!dapm->debugfs_dapm || !w->name)
1284 return;
1285
1286 d = debugfs_create_file(w->name, 0444,
1287 dapm->debugfs_dapm, w,
1288 &dapm_widget_power_fops);
1289 if (!d)
1290 dev_warn(w->dapm->dev,
1291 "ASoC: Failed to create %s debugfs file\n",
1292 w->name);
1293}
1294
Mark Brown79fb9382009-08-21 16:38:13 +01001295#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001296void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1297 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001298{
1299}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001300
1301static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1302{
1303}
1304
Mark Brown79fb9382009-08-21 16:38:13 +01001305#endif
1306
Richard Purdie2b97eab2006-10-06 18:32:18 +02001307/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001308static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001309 struct snd_kcontrol *kcontrol, int change,
1310 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001311{
1312 struct snd_soc_dapm_path *path;
1313 int found = 0;
1314
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001315 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001316 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001317 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001318 return -ENODEV;
1319
Mark Brown3a655772009-10-05 17:23:30 +01001320 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001321 return 0;
1322
1323 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001324 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001325 if (path->kcontrol != kcontrol)
1326 continue;
1327
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001328 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001329 continue;
1330
1331 found = 1;
1332 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001333 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001334 path->connect = 1; /* new connection */
1335 else
1336 path->connect = 0; /* old connection must be powered down */
1337 }
1338
Mark Brownb91b8fa2010-01-20 18:18:35 +00001339 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001340 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001341
1342 return 0;
1343}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001344
Milan plzik1b075e32008-01-10 14:39:46 +01001345/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001346static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001347 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001348{
1349 struct snd_soc_dapm_path *path;
1350 int found = 0;
1351
Milan plzik1b075e32008-01-10 14:39:46 +01001352 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001353 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001354 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001355 return -ENODEV;
1356
Richard Purdie2b97eab2006-10-06 18:32:18 +02001357 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001358 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001359 if (path->kcontrol != kcontrol)
1360 continue;
1361
1362 /* found, now check type */
1363 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001364 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001365 break;
1366 }
1367
Mark Brownb91b8fa2010-01-20 18:18:35 +00001368 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001369 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001370
1371 return 0;
1372}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001373
1374/* show dapm widget status in sys fs */
1375static ssize_t dapm_widget_show(struct device *dev,
1376 struct device_attribute *attr, char *buf)
1377{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001378 struct snd_soc_pcm_runtime *rtd =
1379 container_of(dev, struct snd_soc_pcm_runtime, dev);
1380 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001381 struct snd_soc_dapm_widget *w;
1382 int count = 0;
1383 char *state = "not set";
1384
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001385 list_for_each_entry(w, &codec->card->widgets, list) {
1386 if (w->dapm != &codec->dapm)
1387 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001388
1389 /* only display widgets that burnm power */
1390 switch (w->id) {
1391 case snd_soc_dapm_hp:
1392 case snd_soc_dapm_mic:
1393 case snd_soc_dapm_spk:
1394 case snd_soc_dapm_line:
1395 case snd_soc_dapm_micbias:
1396 case snd_soc_dapm_dac:
1397 case snd_soc_dapm_adc:
1398 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001399 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001400 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001401 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001402 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001403 if (w->name)
1404 count += sprintf(buf + count, "%s: %s\n",
1405 w->name, w->power ? "On":"Off");
1406 break;
1407 default:
1408 break;
1409 }
1410 }
1411
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001412 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001413 case SND_SOC_BIAS_ON:
1414 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001415 break;
Mark Brown0be98982008-05-19 12:31:28 +02001416 case SND_SOC_BIAS_PREPARE:
1417 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001418 break;
Mark Brown0be98982008-05-19 12:31:28 +02001419 case SND_SOC_BIAS_STANDBY:
1420 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001421 break;
Mark Brown0be98982008-05-19 12:31:28 +02001422 case SND_SOC_BIAS_OFF:
1423 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001424 break;
1425 }
1426 count += sprintf(buf + count, "PM State: %s\n", state);
1427
1428 return count;
1429}
1430
1431static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1432
1433int snd_soc_dapm_sys_add(struct device *dev)
1434{
Troy Kisky12ef1932008-10-13 17:42:14 -07001435 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001436}
1437
1438static void snd_soc_dapm_sys_remove(struct device *dev)
1439{
Mark Brownaef90842009-05-16 17:53:16 +01001440 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001441}
1442
1443/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001444static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001445{
1446 struct snd_soc_dapm_widget *w, *next_w;
1447 struct snd_soc_dapm_path *p, *next_p;
1448
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001449 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1450 if (w->dapm != dapm)
1451 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001452 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001453 /*
1454 * remove source and sink paths associated to this widget.
1455 * While removing the path, remove reference to it from both
1456 * source and sink widgets so that path is removed only once.
1457 */
1458 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1459 list_del(&p->list_sink);
1460 list_del(&p->list_source);
1461 list_del(&p->list);
1462 kfree(p->long_name);
1463 kfree(p);
1464 }
1465 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1466 list_del(&p->list_sink);
1467 list_del(&p->list_source);
1468 list_del(&p->list);
1469 kfree(p->long_name);
1470 kfree(p);
1471 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001472 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001473 kfree(w);
1474 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001475}
1476
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001477static struct snd_soc_dapm_widget *dapm_find_widget(
1478 struct snd_soc_dapm_context *dapm, const char *pin,
1479 bool search_other_contexts)
1480{
1481 struct snd_soc_dapm_widget *w;
1482 struct snd_soc_dapm_widget *fallback = NULL;
1483
1484 list_for_each_entry(w, &dapm->card->widgets, list) {
1485 if (!strcmp(w->name, pin)) {
1486 if (w->dapm == dapm)
1487 return w;
1488 else
1489 fallback = w;
1490 }
1491 }
1492
1493 if (search_other_contexts)
1494 return fallback;
1495
1496 return NULL;
1497}
1498
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001499static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001500 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001501{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001502 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01001503
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001504 if (!w) {
1505 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1506 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01001507 }
1508
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001509 w->connected = status;
1510 if (status == 0)
1511 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09001512
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001513 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001514}
1515
Richard Purdie2b97eab2006-10-06 18:32:18 +02001516/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001517 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001518 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001519 *
1520 * Walks all dapm audio paths and powers widgets according to their
1521 * stream or path usage.
1522 *
1523 * Returns 0 for success.
1524 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001525int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001526{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001527 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001528}
Liam Girdwooda5302182008-07-07 13:35:17 +01001529EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001530
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001531static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001532 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001533{
1534 struct snd_soc_dapm_path *path;
1535 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001536 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001537 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001538 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001539 const char *source;
1540 char prefixed_sink[80];
1541 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001542 int ret = 0;
1543
Mark Brown88e8b9a2011-03-02 18:18:24 +00001544 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001545 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1546 dapm->codec->name_prefix, route->sink);
1547 sink = prefixed_sink;
1548 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1549 dapm->codec->name_prefix, route->source);
1550 source = prefixed_source;
1551 } else {
1552 sink = route->sink;
1553 source = route->source;
1554 }
1555
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001556 /*
1557 * find src and dest widgets over all widgets but favor a widget from
1558 * current DAPM context
1559 */
1560 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001561 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001562 wtsink = w;
1563 if (w->dapm == dapm)
1564 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001565 continue;
1566 }
1567 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001568 wtsource = w;
1569 if (w->dapm == dapm)
1570 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001571 }
1572 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001573 /* use widget from another DAPM context if not found from this */
1574 if (!wsink)
1575 wsink = wtsink;
1576 if (!wsource)
1577 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001578
1579 if (wsource == NULL || wsink == NULL)
1580 return -ENODEV;
1581
1582 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1583 if (!path)
1584 return -ENOMEM;
1585
1586 path->source = wsource;
1587 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001588 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001589 INIT_LIST_HEAD(&path->list);
1590 INIT_LIST_HEAD(&path->list_source);
1591 INIT_LIST_HEAD(&path->list_sink);
1592
1593 /* check for external widgets */
1594 if (wsink->id == snd_soc_dapm_input) {
1595 if (wsource->id == snd_soc_dapm_micbias ||
1596 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001597 wsource->id == snd_soc_dapm_line ||
1598 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001599 wsink->ext = 1;
1600 }
1601 if (wsource->id == snd_soc_dapm_output) {
1602 if (wsink->id == snd_soc_dapm_spk ||
1603 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001604 wsink->id == snd_soc_dapm_line ||
1605 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001606 wsource->ext = 1;
1607 }
1608
1609 /* connect static paths */
1610 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001611 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001612 list_add(&path->list_sink, &wsink->sources);
1613 list_add(&path->list_source, &wsource->sinks);
1614 path->connect = 1;
1615 return 0;
1616 }
1617
1618 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08001619 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001620 case snd_soc_dapm_adc:
1621 case snd_soc_dapm_dac:
1622 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001623 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001624 case snd_soc_dapm_input:
1625 case snd_soc_dapm_output:
1626 case snd_soc_dapm_micbias:
1627 case snd_soc_dapm_vmid:
1628 case snd_soc_dapm_pre:
1629 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001630 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001631 case snd_soc_dapm_aif_in:
1632 case snd_soc_dapm_aif_out:
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 = 1;
1637 return 0;
1638 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001639 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001640 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001641 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Richard Purdie2b97eab2006-10-06 18:32:18 +02001642 &wsink->kcontrols[0]);
1643 if (ret != 0)
1644 goto err;
1645 break;
1646 case snd_soc_dapm_switch:
1647 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001648 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001649 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001650 if (ret != 0)
1651 goto err;
1652 break;
1653 case snd_soc_dapm_hp:
1654 case snd_soc_dapm_mic:
1655 case snd_soc_dapm_line:
1656 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001657 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001658 list_add(&path->list_sink, &wsink->sources);
1659 list_add(&path->list_source, &wsource->sinks);
1660 path->connect = 0;
1661 return 0;
1662 }
1663 return 0;
1664
1665err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001666 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1667 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001668 kfree(path);
1669 return ret;
1670}
Mark Brown105f1c22008-05-13 14:52:19 +02001671
1672/**
Mark Brown105f1c22008-05-13 14:52:19 +02001673 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001674 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02001675 * @route: audio routes
1676 * @num: number of routes
1677 *
1678 * Connects 2 dapm widgets together via a named audio path. The sink is
1679 * the widget receiving the audio signal, whilst the source is the sender
1680 * of the audio signal.
1681 *
1682 * Returns 0 for success else error. On error all resources can be freed
1683 * with a call to snd_soc_card_free().
1684 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001685int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02001686 const struct snd_soc_dapm_route *route, int num)
1687{
1688 int i, ret;
1689
1690 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001691 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001692 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001693 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1694 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02001695 return ret;
1696 }
1697 route++;
1698 }
1699
1700 return 0;
1701}
1702EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1703
1704/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001705 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001706 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001707 *
1708 * Checks the codec for any new dapm widgets and creates them if found.
1709 *
1710 * Returns 0 for success.
1711 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001712int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001713{
1714 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00001715 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001716
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001717 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001718 {
1719 if (w->new)
1720 continue;
1721
1722 switch(w->id) {
1723 case snd_soc_dapm_switch:
1724 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001725 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001726 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001727 dapm_new_mixer(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001728 break;
1729 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001730 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001731 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001732 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001733 dapm_new_mux(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001734 break;
1735 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01001736 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01001737 w->power_check = dapm_adc_check_power;
1738 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001739 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01001740 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01001741 w->power_check = dapm_dac_check_power;
1742 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001743 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001744 case snd_soc_dapm_out_drv:
Mark Brownb75576d2009-04-20 17:56:13 +01001745 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001746 dapm_new_pga(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001747 break;
1748 case snd_soc_dapm_input:
1749 case snd_soc_dapm_output:
1750 case snd_soc_dapm_micbias:
1751 case snd_soc_dapm_spk:
1752 case snd_soc_dapm_hp:
1753 case snd_soc_dapm_mic:
1754 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001755 w->power_check = dapm_generic_check_power;
1756 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001757 case snd_soc_dapm_supply:
1758 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001759 case snd_soc_dapm_vmid:
1760 case snd_soc_dapm_pre:
1761 case snd_soc_dapm_post:
1762 break;
1763 }
Mark Brownb66a70d2011-02-09 18:04:11 +00001764
1765 /* Read the initial power state from the device */
1766 if (w->reg >= 0) {
1767 val = snd_soc_read(w->codec, w->reg);
1768 val &= 1 << w->shift;
1769 if (w->invert)
1770 val = !val;
1771
1772 if (val)
1773 w->power = 1;
1774 }
1775
Richard Purdie2b97eab2006-10-06 18:32:18 +02001776 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001777
1778 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001779 }
1780
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001781 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001782 return 0;
1783}
1784EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1785
1786/**
1787 * snd_soc_dapm_get_volsw - dapm mixer get callback
1788 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001789 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001790 *
1791 * Callback to get the value of a dapm mixer control.
1792 *
1793 * Returns 0 for success.
1794 */
1795int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1796 struct snd_ctl_elem_value *ucontrol)
1797{
1798 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001799 struct soc_mixer_control *mc =
1800 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001801 unsigned int reg = mc->reg;
1802 unsigned int shift = mc->shift;
1803 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001804 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001805 unsigned int invert = mc->invert;
1806 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001807
Richard Purdie2b97eab2006-10-06 18:32:18 +02001808 ucontrol->value.integer.value[0] =
1809 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1810 if (shift != rshift)
1811 ucontrol->value.integer.value[1] =
1812 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1813 if (invert) {
1814 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001815 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001816 if (shift != rshift)
1817 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001818 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001819 }
1820
1821 return 0;
1822}
1823EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1824
1825/**
1826 * snd_soc_dapm_put_volsw - dapm mixer set callback
1827 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001828 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001829 *
1830 * Callback to set the value of a dapm mixer control.
1831 *
1832 * Returns 0 for success.
1833 */
1834int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1835 struct snd_ctl_elem_value *ucontrol)
1836{
1837 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001838 struct soc_mixer_control *mc =
1839 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001840 unsigned int reg = mc->reg;
1841 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001842 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001843 unsigned int mask = (1 << fls(max)) - 1;
1844 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07001845 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00001846 int connect, change;
1847 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001848
1849 val = (ucontrol->value.integer.value[0] & mask);
1850
1851 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001852 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07001853 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001854 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001855
1856 mutex_lock(&widget->codec->mutex);
1857 widget->value = val;
1858
Stephen Warrene9cf7042011-01-27 14:54:05 -07001859 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00001860 if (change) {
Mark Brown283375c2009-12-07 18:09:03 +00001861 if (val)
1862 /* new connection */
1863 connect = invert ? 0:1;
1864 else
1865 /* old connection must be powered down */
1866 connect = invert ? 1:0;
1867
Mark Brown97404f22010-12-14 16:13:57 +00001868 update.kcontrol = kcontrol;
1869 update.widget = widget;
1870 update.reg = reg;
1871 update.mask = mask;
1872 update.val = val;
1873 widget->dapm->update = &update;
1874
Mark Brown283375c2009-12-07 18:09:03 +00001875 dapm_mixer_update_power(widget, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00001876
1877 widget->dapm->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00001878 }
1879
Richard Purdie2b97eab2006-10-06 18:32:18 +02001880 mutex_unlock(&widget->codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00001881 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001882}
1883EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1884
1885/**
1886 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1887 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001888 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001889 *
1890 * Callback to get the value of a dapm enumerated double mixer control.
1891 *
1892 * Returns 0 for success.
1893 */
1894int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1895 struct snd_ctl_elem_value *ucontrol)
1896{
1897 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1898 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001899 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001900
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001901 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001902 ;
1903 val = snd_soc_read(widget->codec, e->reg);
1904 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1905 if (e->shift_l != e->shift_r)
1906 ucontrol->value.enumerated.item[1] =
1907 (val >> e->shift_r) & (bitmask - 1);
1908
1909 return 0;
1910}
1911EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1912
1913/**
1914 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1915 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001916 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001917 *
1918 * Callback to set the value of a dapm enumerated double mixer control.
1919 *
1920 * Returns 0 for success.
1921 */
1922int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1923 struct snd_ctl_elem_value *ucontrol)
1924{
1925 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1926 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001927 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001928 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00001929 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001930
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001931 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001932 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001933 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001934 return -EINVAL;
1935 mux = ucontrol->value.enumerated.item[0];
1936 val = mux << e->shift_l;
1937 mask = (bitmask - 1) << e->shift_l;
1938 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001939 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001940 return -EINVAL;
1941 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1942 mask |= (bitmask - 1) << e->shift_r;
1943 }
1944
1945 mutex_lock(&widget->codec->mutex);
1946 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01001947 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00001948
1949 update.kcontrol = kcontrol;
1950 update.widget = widget;
1951 update.reg = e->reg;
1952 update.mask = mask;
1953 update.val = val;
1954 widget->dapm->update = &update;
1955
Mark Brown3a655772009-10-05 17:23:30 +01001956 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01001957
Mark Brown97404f22010-12-14 16:13:57 +00001958 widget->dapm->update = NULL;
Mark Brown1642e3d2009-10-05 16:24:26 +01001959
Richard Purdie2b97eab2006-10-06 18:32:18 +02001960 mutex_unlock(&widget->codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00001961 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001962}
1963EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1964
1965/**
Mark Brownd2b247a2009-10-06 15:21:04 +01001966 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1967 * @kcontrol: mixer control
1968 * @ucontrol: control element information
1969 *
1970 * Returns 0 for success.
1971 */
1972int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1973 struct snd_ctl_elem_value *ucontrol)
1974{
1975 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1976
1977 ucontrol->value.enumerated.item[0] = widget->value;
1978
1979 return 0;
1980}
1981EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1982
1983/**
1984 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1985 * @kcontrol: mixer control
1986 * @ucontrol: control element information
1987 *
1988 * Returns 0 for success.
1989 */
1990int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
1991 struct snd_ctl_elem_value *ucontrol)
1992{
1993 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1994 struct soc_enum *e =
1995 (struct soc_enum *)kcontrol->private_value;
1996 int change;
1997 int ret = 0;
1998
1999 if (ucontrol->value.enumerated.item[0] >= e->max)
2000 return -EINVAL;
2001
2002 mutex_lock(&widget->codec->mutex);
2003
2004 change = widget->value != ucontrol->value.enumerated.item[0];
2005 widget->value = ucontrol->value.enumerated.item[0];
2006 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
2007
2008 mutex_unlock(&widget->codec->mutex);
2009 return ret;
2010}
2011EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2012
2013/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002014 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2015 * callback
2016 * @kcontrol: mixer control
2017 * @ucontrol: control element information
2018 *
2019 * Callback to get the value of a dapm semi enumerated double mixer control.
2020 *
2021 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2022 * used for handling bitfield coded enumeration for example.
2023 *
2024 * Returns 0 for success.
2025 */
2026int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2027 struct snd_ctl_elem_value *ucontrol)
2028{
2029 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002030 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002031 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002032
2033 reg_val = snd_soc_read(widget->codec, e->reg);
2034 val = (reg_val >> e->shift_l) & e->mask;
2035 for (mux = 0; mux < e->max; mux++) {
2036 if (val == e->values[mux])
2037 break;
2038 }
2039 ucontrol->value.enumerated.item[0] = mux;
2040 if (e->shift_l != e->shift_r) {
2041 val = (reg_val >> e->shift_r) & e->mask;
2042 for (mux = 0; mux < e->max; mux++) {
2043 if (val == e->values[mux])
2044 break;
2045 }
2046 ucontrol->value.enumerated.item[1] = mux;
2047 }
2048
2049 return 0;
2050}
2051EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2052
2053/**
2054 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2055 * callback
2056 * @kcontrol: mixer control
2057 * @ucontrol: control element information
2058 *
2059 * Callback to set the value of a dapm semi enumerated double mixer control.
2060 *
2061 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2062 * used for handling bitfield coded enumeration for example.
2063 *
2064 * Returns 0 for success.
2065 */
2066int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2067 struct snd_ctl_elem_value *ucontrol)
2068{
2069 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002070 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002071 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002072 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002073 struct snd_soc_dapm_update update;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002074
2075 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2076 return -EINVAL;
2077 mux = ucontrol->value.enumerated.item[0];
2078 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2079 mask = e->mask << e->shift_l;
2080 if (e->shift_l != e->shift_r) {
2081 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2082 return -EINVAL;
2083 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2084 mask |= e->mask << e->shift_r;
2085 }
2086
2087 mutex_lock(&widget->codec->mutex);
2088 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01002089 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002090
2091 update.kcontrol = kcontrol;
2092 update.widget = widget;
2093 update.reg = e->reg;
2094 update.mask = mask;
2095 update.val = val;
2096 widget->dapm->update = &update;
2097
Mark Brown3a655772009-10-05 17:23:30 +01002098 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002099
Mark Brown97404f22010-12-14 16:13:57 +00002100 widget->dapm->update = NULL;
Mark Brown1642e3d2009-10-05 16:24:26 +01002101
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002102 mutex_unlock(&widget->codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002103 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002104}
2105EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2106
2107/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002108 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2109 *
2110 * @kcontrol: mixer control
2111 * @uinfo: control element information
2112 *
2113 * Callback to provide information about a pin switch control.
2114 */
2115int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2116 struct snd_ctl_elem_info *uinfo)
2117{
2118 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2119 uinfo->count = 1;
2120 uinfo->value.integer.min = 0;
2121 uinfo->value.integer.max = 1;
2122
2123 return 0;
2124}
2125EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2126
2127/**
2128 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2129 *
2130 * @kcontrol: mixer control
2131 * @ucontrol: Value
2132 */
2133int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2134 struct snd_ctl_elem_value *ucontrol)
2135{
2136 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2137 const char *pin = (const char *)kcontrol->private_value;
2138
2139 mutex_lock(&codec->mutex);
2140
2141 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002142 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002143
2144 mutex_unlock(&codec->mutex);
2145
2146 return 0;
2147}
2148EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2149
2150/**
2151 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2152 *
2153 * @kcontrol: mixer control
2154 * @ucontrol: Value
2155 */
2156int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2157 struct snd_ctl_elem_value *ucontrol)
2158{
2159 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2160 const char *pin = (const char *)kcontrol->private_value;
2161
2162 mutex_lock(&codec->mutex);
2163
2164 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002165 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002166 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002167 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002168
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002169 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002170
2171 mutex_unlock(&codec->mutex);
2172
2173 return 0;
2174}
2175EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2176
2177/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002178 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002179 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002180 * @widget: widget template
2181 *
2182 * Creates a new dapm control based upon the template.
2183 *
2184 * Returns 0 for success else error.
2185 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002186int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002187 const struct snd_soc_dapm_widget *widget)
2188{
2189 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002190 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002191
2192 if ((w = dapm_cnew_widget(widget)) == NULL)
2193 return -ENOMEM;
2194
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002195 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002196 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002197 name_len += 1 + strlen(dapm->codec->name_prefix);
2198 w->name = kmalloc(name_len, GFP_KERNEL);
2199 if (w->name == NULL) {
2200 kfree(w);
2201 return -ENOMEM;
2202 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002203 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002204 snprintf(w->name, name_len, "%s %s",
2205 dapm->codec->name_prefix, widget->name);
2206 else
2207 snprintf(w->name, name_len, "%s", widget->name);
2208
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002209 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002210 w->dapm = dapm;
2211 w->codec = dapm->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002212 INIT_LIST_HEAD(&w->sources);
2213 INIT_LIST_HEAD(&w->sinks);
2214 INIT_LIST_HEAD(&w->list);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002215 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002216
2217 /* machine layer set ups unconnected pins and insertions */
2218 w->connected = 1;
2219 return 0;
2220}
2221EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2222
2223/**
Mark Brown4ba13272008-05-13 14:51:19 +02002224 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002225 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002226 * @widget: widget array
2227 * @num: number of widgets
2228 *
2229 * Creates new DAPM controls based upon the templates.
2230 *
2231 * Returns 0 for success else error.
2232 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002233int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002234 const struct snd_soc_dapm_widget *widget,
2235 int num)
2236{
2237 int i, ret;
2238
2239 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002240 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002241 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002242 dev_err(dapm->dev,
2243 "ASoC: Failed to create DAPM control %s: %d\n",
2244 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002245 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002246 }
Mark Brown4ba13272008-05-13 14:51:19 +02002247 widget++;
2248 }
2249 return 0;
2250}
2251EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2252
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002253static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002254 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002255{
2256 struct snd_soc_dapm_widget *w;
2257
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002258 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002259 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002260 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002261 continue;
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002262 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2263 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002264 if (strstr(w->sname, stream)) {
2265 switch(event) {
2266 case SND_SOC_DAPM_STREAM_START:
2267 w->active = 1;
2268 break;
2269 case SND_SOC_DAPM_STREAM_STOP:
2270 w->active = 0;
2271 break;
2272 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002273 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002274 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002275 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2276 break;
2277 }
2278 }
2279 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002280
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002281 dapm_power_widgets(dapm, event);
2282}
2283
2284/**
2285 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2286 * @rtd: PCM runtime data
2287 * @stream: stream name
2288 * @event: stream event
2289 *
2290 * Sends a stream event to the dapm core. The core then makes any
2291 * necessary widget power changes.
2292 *
2293 * Returns 0 for success else error.
2294 */
2295int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2296 const char *stream, int event)
2297{
2298 struct snd_soc_codec *codec = rtd->codec;
2299
2300 if (stream == NULL)
2301 return 0;
2302
2303 mutex_lock(&codec->mutex);
2304 soc_dapm_stream_event(&codec->dapm, stream, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002305 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002306 return 0;
2307}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002308
2309/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002310 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002311 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002312 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002313 *
Mark Brown74b8f952009-06-06 11:26:15 +01002314 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002315 * a valid audio route and active audio stream.
2316 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2317 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002318 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002319int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002320{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002321 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002322}
Liam Girdwooda5302182008-07-07 13:35:17 +01002323EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002324
2325/**
Mark Brownda341832010-03-15 19:23:37 +00002326 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002327 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002328 * @pin: pin name
2329 *
2330 * Enables input/output pin regardless of any other state. This is
2331 * intended for use with microphone bias supplies used in microphone
2332 * jack detection.
2333 *
2334 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2335 * do any widget power switching.
2336 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002337int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2338 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002339{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002340 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00002341
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002342 if (!w) {
2343 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2344 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00002345 }
2346
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002347 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2348 w->connected = 1;
2349 w->force = 1;
Mark Brown0d867332011-04-06 11:38:14 +09002350
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002351 return 0;
Mark Brownda341832010-03-15 19:23:37 +00002352}
2353EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2354
2355/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002356 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002357 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002358 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002359 *
Mark Brown74b8f952009-06-06 11:26:15 +01002360 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002361 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2362 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002363 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002364int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2365 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002366{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002367 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002368}
2369EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2370
2371/**
Mark Brown5817b522008-09-24 11:23:11 +01002372 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002373 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002374 * @pin: pin name
2375 *
2376 * Marks the specified pin as being not connected, disabling it along
2377 * any parent or child widgets. At present this is identical to
2378 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2379 * additional things such as disabling controls which only affect
2380 * paths through the pin.
2381 *
2382 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2383 * do any widget power switching.
2384 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002385int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002386{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002387 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002388}
2389EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2390
2391/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002392 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002393 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002394 * @pin: audio signal pin endpoint (or start point)
2395 *
2396 * Get audio pin status - connected or disconnected.
2397 *
2398 * Returns 1 for connected otherwise 0.
2399 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002400int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2401 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002402{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002403 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002404
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002405 if (w)
2406 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06002407
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002408 return 0;
2409}
Liam Girdwooda5302182008-07-07 13:35:17 +01002410EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002411
2412/**
Mark Brown1547aba2010-05-07 21:11:40 +01002413 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002414 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01002415 * @pin: audio signal pin endpoint (or start point)
2416 *
2417 * Mark the given endpoint or pin as ignoring suspend. When the
2418 * system is disabled a path between two endpoints flagged as ignoring
2419 * suspend will not be disabled. The path must already be enabled via
2420 * normal means at suspend time, it will not be turned on if it was not
2421 * already enabled.
2422 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002423int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2424 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01002425{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002426 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01002427
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002428 if (!w) {
2429 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2430 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01002431 }
2432
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002433 w->ignore_suspend = 1;
2434
2435 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01002436}
2437EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2438
2439/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002440 * snd_soc_dapm_free - free dapm resources
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002441 * @card: SoC device
Richard Purdie2b97eab2006-10-06 18:32:18 +02002442 *
2443 * Free all dapm widgets and resources.
2444 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002445void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002446{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002447 snd_soc_dapm_sys_remove(dapm->dev);
2448 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02002449 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002450}
2451EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2452
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002453static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01002454{
Mark Brown51737472009-06-22 13:16:51 +01002455 struct snd_soc_dapm_widget *w;
2456 LIST_HEAD(down_list);
2457 int powerdown = 0;
2458
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002459 list_for_each_entry(w, &dapm->card->widgets, list) {
2460 if (w->dapm != dapm)
2461 continue;
Mark Brown51737472009-06-22 13:16:51 +01002462 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00002463 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002464 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002465 powerdown = 1;
2466 }
2467 }
2468
2469 /* If there were no widgets to power down we're already in
2470 * standby.
2471 */
2472 if (powerdown) {
Mark Browned5a4c42011-02-18 11:12:42 -08002473 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00002474 dapm_seq_run(dapm, &down_list, 0, false);
Mark Browned5a4c42011-02-18 11:12:42 -08002475 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01002476 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002477}
Mark Brown51737472009-06-22 13:16:51 +01002478
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002479/*
2480 * snd_soc_dapm_shutdown - callback for system shutdown
2481 */
2482void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2483{
2484 struct snd_soc_codec *codec;
2485
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002486 list_for_each_entry(codec, &card->codec_dev_list, list) {
2487 soc_dapm_shutdown_codec(&codec->dapm);
Mark Browned5a4c42011-02-18 11:12:42 -08002488 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002489 }
Mark Brown51737472009-06-22 13:16:51 +01002490}
2491
Richard Purdie2b97eab2006-10-06 18:32:18 +02002492/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002493MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002494MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2495MODULE_LICENSE("GPL");