blob: 60c8dec49480faa08d5beb30078fb171f41a3bf7 [file] [log] [blame]
Richard Purdie2b97eab2006-10-06 18:32:18 +02001/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020012 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
Mark Brown74b8f952009-06-06 11:26:15 +010015 * DACs/ADCs.
Richard Purdie2b97eab2006-10-06 18:32:18 +020016 * o Platform power domain - can support external components i.e. amps and
17 * mic/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +020021 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
24 * Todo:
25 * o DAPM power change sequencing - allow for configurable per
26 * codec sequences.
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
30 */
31
32#include <linux/module.h>
33#include <linux/moduleparam.h>
34#include <linux/init.h>
35#include <linux/delay.h>
36#include <linux/pm.h>
37#include <linux/bitops.h>
38#include <linux/platform_device.h>
39#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020040#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020042#include <sound/core.h>
43#include <sound/pcm.h>
44#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020045#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020046#include <sound/soc-dapm.h>
47#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,
60 [snd_soc_dapm_value_mux] = 5,
61 [snd_soc_dapm_dac] = 6,
62 [snd_soc_dapm_mixer] = 7,
63 [snd_soc_dapm_mixer_named_ctl] = 7,
64 [snd_soc_dapm_pga] = 8,
65 [snd_soc_dapm_adc] = 9,
66 [snd_soc_dapm_hp] = 10,
67 [snd_soc_dapm_spk] = 10,
68 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020069};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000070
Richard Purdie2b97eab2006-10-06 18:32:18 +020071static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010072 [snd_soc_dapm_pre] = 0,
73 [snd_soc_dapm_adc] = 1,
74 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010075 [snd_soc_dapm_spk] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010076 [snd_soc_dapm_pga] = 4,
77 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010078 [snd_soc_dapm_mixer] = 5,
79 [snd_soc_dapm_dac] = 6,
80 [snd_soc_dapm_mic] = 7,
81 [snd_soc_dapm_micbias] = 8,
82 [snd_soc_dapm_mux] = 9,
83 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010084 [snd_soc_dapm_aif_in] = 10,
85 [snd_soc_dapm_aif_out] = 10,
86 [snd_soc_dapm_supply] = 11,
87 [snd_soc_dapm_post] = 12,
Richard Purdie2b97eab2006-10-06 18:32:18 +020088};
89
Troy Kisky12ef1932008-10-13 17:42:14 -070090static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +010091{
92 if (pop_time)
93 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
94}
95
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +020096static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +010097{
98 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +020099 char *buf;
100
101 if (!pop_time)
102 return;
103
104 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
105 if (buf == NULL)
106 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100107
108 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200109 vsnprintf(buf, PAGE_SIZE, fmt, args);
110 dev_info(dev, buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100111 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200112
113 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100114}
115
Richard Purdie2b97eab2006-10-06 18:32:18 +0200116/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100117static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200118 const struct snd_soc_dapm_widget *_widget)
119{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100120 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200121}
122
Mark Brown452c5ea2009-05-17 21:41:23 +0100123/**
124 * snd_soc_dapm_set_bias_level - set the bias level for the system
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000125 * @card: audio device
Mark Brown452c5ea2009-05-17 21:41:23 +0100126 * @level: level to configure
127 *
128 * Configure the bias (power) levels for the SoC audio device.
129 *
130 * Returns 0 for success else error.
131 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000132static int snd_soc_dapm_set_bias_level(struct snd_soc_card *card,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200133 struct snd_soc_dapm_context *dapm,
134 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100135{
Mark Brown452c5ea2009-05-17 21:41:23 +0100136 int ret = 0;
137
Mark Brownf83fba82009-05-18 15:44:43 +0100138 switch (level) {
139 case SND_SOC_BIAS_ON:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200140 dev_dbg(dapm->dev, "Setting full bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100141 break;
142 case SND_SOC_BIAS_PREPARE:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200143 dev_dbg(dapm->dev, "Setting bias prepare\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100144 break;
145 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200146 dev_dbg(dapm->dev, "Setting standby bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100147 break;
148 case SND_SOC_BIAS_OFF:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200149 dev_dbg(dapm->dev, "Setting bias off\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100150 break;
151 default:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200152 dev_err(dapm->dev, "Setting invalid bias %d\n", level);
Mark Brownf83fba82009-05-18 15:44:43 +0100153 return -EINVAL;
154 }
155
Mark Brown84e90932010-11-04 00:07:02 -0400156 trace_snd_soc_bias_level_start(card, level);
157
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000158 if (card && card->set_bias_level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100159 ret = card->set_bias_level(card, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100160 if (ret == 0) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200161 if (dapm->codec && dapm->codec->driver->set_bias_level)
162 ret = dapm->codec->driver->set_bias_level(dapm->codec, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100163 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200164 dapm->bias_level = level;
Mark Brown474e09c2009-08-19 14:18:53 +0100165 }
Mark Brown452c5ea2009-05-17 21:41:23 +0100166
Mark Brown84e90932010-11-04 00:07:02 -0400167 trace_snd_soc_bias_level_done(card, level);
168
Mark Brown452c5ea2009-05-17 21:41:23 +0100169 return ret;
170}
171
Richard Purdie2b97eab2006-10-06 18:32:18 +0200172/* set up initial codec paths */
173static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
174 struct snd_soc_dapm_path *p, int i)
175{
176 switch (w->id) {
177 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000178 case snd_soc_dapm_mixer:
179 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200180 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100181 struct soc_mixer_control *mc = (struct soc_mixer_control *)
182 w->kcontrols[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400183 unsigned int reg = mc->reg;
184 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100185 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400186 unsigned int mask = (1 << fls(max)) - 1;
187 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200188
189 val = snd_soc_read(w->codec, reg);
190 val = (val >> shift) & mask;
191
192 if ((invert && !val) || (!invert && val))
193 p->connect = 1;
194 else
195 p->connect = 0;
196 }
197 break;
198 case snd_soc_dapm_mux: {
199 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
200 int val, item, bitmask;
201
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100202 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200203 ;
204 val = snd_soc_read(w->codec, e->reg);
205 item = (val >> e->shift_l) & (bitmask - 1);
206
207 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100208 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200209 if (!(strcmp(p->name, e->texts[i])) && item == i)
210 p->connect = 1;
211 }
212 }
213 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200214 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200215 struct soc_enum *e = (struct soc_enum *)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200216 w->kcontrols[i].private_value;
217 int val, item;
218
219 val = snd_soc_read(w->codec, e->reg);
220 val = (val >> e->shift_l) & e->mask;
221 for (item = 0; item < e->max; item++) {
222 if (val == e->values[item])
223 break;
224 }
225
226 p->connect = 0;
227 for (i = 0; i < e->max; i++) {
228 if (!(strcmp(p->name, e->texts[i])) && item == i)
229 p->connect = 1;
230 }
231 }
232 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200233 /* does not effect routing - always connected */
234 case snd_soc_dapm_pga:
235 case snd_soc_dapm_output:
236 case snd_soc_dapm_adc:
237 case snd_soc_dapm_input:
238 case snd_soc_dapm_dac:
239 case snd_soc_dapm_micbias:
240 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100241 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100242 case snd_soc_dapm_aif_in:
243 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200244 p->connect = 1;
245 break;
246 /* does effect routing - dynamically connected */
247 case snd_soc_dapm_hp:
248 case snd_soc_dapm_mic:
249 case snd_soc_dapm_spk:
250 case snd_soc_dapm_line:
251 case snd_soc_dapm_pre:
252 case snd_soc_dapm_post:
253 p->connect = 0;
254 break;
255 }
256}
257
Mark Brown74b8f952009-06-06 11:26:15 +0100258/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200259static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200260 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
261 struct snd_soc_dapm_path *path, const char *control_name,
262 const struct snd_kcontrol_new *kcontrol)
263{
264 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
265 int i;
266
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100267 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200268 if (!(strcmp(control_name, e->texts[i]))) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200269 list_add(&path->list, &dapm->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200270 list_add(&path->list_sink, &dest->sources);
271 list_add(&path->list_source, &src->sinks);
272 path->name = (char*)e->texts[i];
273 dapm_set_path_status(dest, path, 0);
274 return 0;
275 }
276 }
277
278 return -ENODEV;
279}
280
Mark Brown74b8f952009-06-06 11:26:15 +0100281/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200282static int dapm_connect_mixer(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{
286 int i;
287
288 /* search for mixer kcontrol */
289 for (i = 0; i < dest->num_kcontrols; i++) {
290 if (!strcmp(control_name, dest->kcontrols[i].name)) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200291 list_add(&path->list, &dapm->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200292 list_add(&path->list_sink, &dest->sources);
293 list_add(&path->list_source, &src->sinks);
294 path->name = dest->kcontrols[i].name;
295 dapm_set_path_status(dest, path, i);
296 return 0;
297 }
298 }
299 return -ENODEV;
300}
301
302/* update dapm codec register bits */
303static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
304{
305 int change, power;
Daniel Ribeiro46f58222009-06-07 02:49:11 -0300306 unsigned int old, new;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200307 struct snd_soc_codec *codec = widget->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200308 struct snd_soc_dapm_context *dapm = widget->dapm;
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200309 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200310
311 /* check for valid widgets */
312 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
313 widget->id == snd_soc_dapm_output ||
314 widget->id == snd_soc_dapm_hp ||
315 widget->id == snd_soc_dapm_mic ||
316 widget->id == snd_soc_dapm_line ||
317 widget->id == snd_soc_dapm_spk)
318 return 0;
319
320 power = widget->power;
321 if (widget->invert)
322 power = (power ? 0:1);
323
324 old = snd_soc_read(codec, widget->reg);
325 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
326
327 change = old != new;
328 if (change) {
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200329 pop_dbg(dapm->dev, card->pop_time,
330 "pop test %s : %s in %d ms\n",
Troy Kisky12ef1932008-10-13 17:42:14 -0700331 widget->name, widget->power ? "on" : "off",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200332 card->pop_time);
333 pop_wait(card->pop_time);
Mark Brown69224712010-03-03 14:57:09 +0000334 snd_soc_write(codec, widget->reg, new);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200335 }
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200336 dev_dbg(dapm->dev, "reg %x old %x new %x change %d\n", widget->reg,
337 old, new, change);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200338 return change;
339}
340
Richard Purdie2b97eab2006-10-06 18:32:18 +0200341/* create new dapm mixer control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200342static int dapm_new_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200343 struct snd_soc_dapm_widget *w)
344{
345 int i, ret = 0;
Mark Brown219b93f2008-10-28 13:02:31 +0000346 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200347 struct snd_soc_dapm_path *path;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200348 struct snd_card *card = dapm->codec->card->snd_card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200349
350 /* add kcontrol */
351 for (i = 0; i < w->num_kcontrols; i++) {
352
353 /* match name */
354 list_for_each_entry(path, &w->sources, list_sink) {
355
356 /* mixer/mux paths name must match control name */
357 if (path->name != (char*)w->kcontrols[i].name)
358 continue;
359
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000360 /* add dapm control with long name.
361 * for dapm_mixer this is the concatenation of the
362 * mixer and kcontrol name.
363 * for dapm_mixer_named_ctl this is simply the
364 * kcontrol name.
365 */
366 name_len = strlen(w->kcontrols[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000367 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000368 name_len += 1 + strlen(w->name);
369
Mark Brown219b93f2008-10-28 13:02:31 +0000370 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000371
Richard Purdie2b97eab2006-10-06 18:32:18 +0200372 if (path->long_name == NULL)
373 return -ENOMEM;
374
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000375 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000376 default:
377 snprintf(path->long_name, name_len, "%s %s",
378 w->name, w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000379 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000380 case snd_soc_dapm_mixer_named_ctl:
381 snprintf(path->long_name, name_len, "%s",
382 w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000383 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000384 }
385
Mark Brown219b93f2008-10-28 13:02:31 +0000386 path->long_name[name_len - 1] = '\0';
387
Richard Purdie2b97eab2006-10-06 18:32:18 +0200388 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
389 path->long_name);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200390 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200391 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200392 dev_err(dapm->dev,
393 "asoc: failed to add dapm kcontrol %s: %d\n",
394 path->long_name, ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200395 kfree(path->long_name);
396 path->long_name = NULL;
397 return ret;
398 }
399 }
400 }
401 return ret;
402}
403
404/* create new dapm mux control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200405static int dapm_new_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200406 struct snd_soc_dapm_widget *w)
407{
408 struct snd_soc_dapm_path *path = NULL;
409 struct snd_kcontrol *kcontrol;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200410 struct snd_card *card = dapm->codec->card->snd_card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200411 int ret = 0;
412
413 if (!w->num_kcontrols) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200414 dev_err(dapm->dev, "asoc: mux %s has no controls\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200415 return -EINVAL;
416 }
417
418 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200419 ret = snd_ctl_add(card, kcontrol);
420
Richard Purdie2b97eab2006-10-06 18:32:18 +0200421 if (ret < 0)
422 goto err;
423
424 list_for_each_entry(path, &w->sources, list_sink)
425 path->kcontrol = kcontrol;
426
427 return ret;
428
429err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200430 dev_err(dapm->dev, "asoc: failed to add kcontrol %s\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200431 return ret;
432}
433
434/* create new dapm volume control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200435static int dapm_new_pga(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200436 struct snd_soc_dapm_widget *w)
437{
Mark Browna6c65732010-03-03 17:45:21 +0000438 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200439 dev_err(w->dapm->dev,
440 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200441
Mark Browna6c65732010-03-03 17:45:21 +0000442 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200443}
444
445/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200446static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200447{
448 struct snd_soc_dapm_path *p;
449
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200450 list_for_each_entry(p, &dapm->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200451 p->walked = 0;
452}
453
Mark Brown99497882010-05-07 20:24:05 +0100454/* We implement power down on suspend by checking the power state of
455 * the ALSA card - when we are suspending the ALSA state for the card
456 * is set to D3.
457 */
458static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
459{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200460 int level = snd_power_get_state(widget->dapm->codec->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100461
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000462 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100463 case SNDRV_CTL_POWER_D3hot:
464 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100465 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200466 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
467 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100468 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100469 default:
470 return 1;
471 }
472}
473
Richard Purdie2b97eab2006-10-06 18:32:18 +0200474/*
475 * Recursively check for a completed path to an active or physically connected
476 * output widget. Returns number of complete paths.
477 */
478static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
479{
480 struct snd_soc_dapm_path *path;
481 int con = 0;
482
Mark Brown246d0a12009-04-22 18:24:55 +0100483 if (widget->id == snd_soc_dapm_supply)
484 return 0;
485
Mark Brown010ff262009-08-17 17:39:22 +0100486 switch (widget->id) {
487 case snd_soc_dapm_adc:
488 case snd_soc_dapm_aif_out:
489 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100490 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100491 default:
492 break;
493 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200494
495 if (widget->connected) {
496 /* connected pin ? */
497 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100498 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200499
500 /* connected jack or spk ? */
501 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300502 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown99497882010-05-07 20:24:05 +0100503 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200504 }
505
506 list_for_each_entry(path, &widget->sinks, list_source) {
507 if (path->walked)
508 continue;
509
510 if (path->sink && path->connect) {
511 path->walked = 1;
512 con += is_connected_output_ep(path->sink);
513 }
514 }
515
516 return con;
517}
518
519/*
520 * Recursively check for a completed path to an active or physically connected
521 * input widget. Returns number of complete paths.
522 */
523static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
524{
525 struct snd_soc_dapm_path *path;
526 int con = 0;
527
Mark Brown246d0a12009-04-22 18:24:55 +0100528 if (widget->id == snd_soc_dapm_supply)
529 return 0;
530
Richard Purdie2b97eab2006-10-06 18:32:18 +0200531 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100532 switch (widget->id) {
533 case snd_soc_dapm_dac:
534 case snd_soc_dapm_aif_in:
535 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100536 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100537 default:
538 break;
539 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200540
541 if (widget->connected) {
542 /* connected pin ? */
543 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100544 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200545
546 /* connected VMID/Bias for lower pops */
547 if (widget->id == snd_soc_dapm_vmid)
Mark Brown99497882010-05-07 20:24:05 +0100548 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200549
550 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300551 if (widget->id == snd_soc_dapm_mic ||
552 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown99497882010-05-07 20:24:05 +0100553 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200554 }
555
556 list_for_each_entry(path, &widget->sources, list_sink) {
557 if (path->walked)
558 continue;
559
560 if (path->source && path->connect) {
561 path->walked = 1;
562 con += is_connected_input_ep(path->source);
563 }
564 }
565
566 return con;
567}
568
569/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300570 * Handler for generic register modifier widget.
571 */
572int dapm_reg_event(struct snd_soc_dapm_widget *w,
573 struct snd_kcontrol *kcontrol, int event)
574{
575 unsigned int val;
576
577 if (SND_SOC_DAPM_EVENT_ON(event))
578 val = w->on_val;
579 else
580 val = w->off_val;
581
582 snd_soc_update_bits(w->codec, -(w->reg + 1),
583 w->mask << w->shift, val << w->shift);
584
585 return 0;
586}
Mark Brown11589412008-07-29 11:42:23 +0100587EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300588
Mark Brown025756e2009-04-13 11:09:18 +0100589/* Standard power change method, used to apply power changes to most
590 * widgets.
591 */
592static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
593{
594 int ret;
595
596 /* call any power change event handlers */
597 if (w->event)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200598 dev_dbg(w->dapm->dev, "power %s event for %s flags %x\n",
Mark Brown025756e2009-04-13 11:09:18 +0100599 w->power ? "on" : "off",
600 w->name, w->event_flags);
601
602 /* power up pre event */
603 if (w->power && w->event &&
604 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
605 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
606 if (ret < 0)
607 return ret;
608 }
609
610 /* power down pre event */
611 if (!w->power && w->event &&
612 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
613 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
614 if (ret < 0)
615 return ret;
616 }
617
Mark Brown025756e2009-04-13 11:09:18 +0100618 dapm_update_bits(w);
619
Mark Brown025756e2009-04-13 11:09:18 +0100620 /* power up post event */
621 if (w->power && w->event &&
622 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
623 ret = w->event(w,
624 NULL, SND_SOC_DAPM_POST_PMU);
625 if (ret < 0)
626 return ret;
627 }
628
629 /* power down post event */
630 if (!w->power && w->event &&
631 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
632 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
633 if (ret < 0)
634 return ret;
635 }
636
637 return 0;
638}
639
Mark Browncd0f2d42009-04-20 16:56:59 +0100640/* Generic check to see if a widget should be powered.
641 */
642static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
643{
644 int in, out;
645
646 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200647 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100648 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200649 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100650 return out != 0 && in != 0;
651}
652
Mark Brown6ea31b92009-04-20 17:15:41 +0100653/* Check to see if an ADC has power */
654static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
655{
656 int in;
657
658 if (w->active) {
659 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200660 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100661 return in != 0;
662 } else {
663 return dapm_generic_check_power(w);
664 }
665}
666
667/* Check to see if a DAC has power */
668static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
669{
670 int out;
671
672 if (w->active) {
673 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200674 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100675 return out != 0;
676 } else {
677 return dapm_generic_check_power(w);
678 }
679}
680
Mark Brown246d0a12009-04-22 18:24:55 +0100681/* Check to see if a power supply is needed */
682static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
683{
684 struct snd_soc_dapm_path *path;
685 int power = 0;
686
687 /* Check if one of our outputs is connected */
688 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +0100689 if (path->connected &&
690 !path->connected(path->source, path->sink))
691 continue;
692
Mark Brown246d0a12009-04-22 18:24:55 +0100693 if (path->sink && path->sink->power_check &&
694 path->sink->power_check(path->sink)) {
695 power = 1;
696 break;
697 }
698 }
699
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200700 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100701
702 return power;
703}
704
Mark Brown38357ab2009-06-06 19:03:23 +0100705static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
706 struct snd_soc_dapm_widget *b,
707 int sort[])
Mark Brown42aa3412009-03-01 19:21:10 +0000708{
Mark Brown38357ab2009-06-06 19:03:23 +0100709 if (sort[a->id] != sort[b->id])
710 return sort[a->id] - sort[b->id];
Mark Brownb22ead22009-06-07 12:51:26 +0100711 if (a->reg != b->reg)
712 return a->reg - b->reg;
Mark Brownbcbb2432010-11-12 15:14:55 +0000713 if (a->codec != b->codec)
714 return (unsigned long)a->codec - (unsigned long)b->codec;
Mark Brown42aa3412009-03-01 19:21:10 +0000715
Mark Brown38357ab2009-06-06 19:03:23 +0100716 return 0;
717}
Mark Brown42aa3412009-03-01 19:21:10 +0000718
Mark Brown38357ab2009-06-06 19:03:23 +0100719/* Insert a widget in order into a DAPM power sequence. */
720static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
721 struct list_head *list,
722 int sort[])
723{
724 struct snd_soc_dapm_widget *w;
725
726 list_for_each_entry(w, list, power_list)
727 if (dapm_seq_compare(new_widget, w, sort) < 0) {
728 list_add_tail(&new_widget->power_list, &w->power_list);
729 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000730 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100731
Mark Brown38357ab2009-06-06 19:03:23 +0100732 list_add_tail(&new_widget->power_list, list);
733}
Mark Brown42aa3412009-03-01 19:21:10 +0000734
Mark Brown68f89ad2010-11-03 23:51:49 -0400735static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
736 struct snd_soc_dapm_widget *w, int event)
737{
738 struct snd_soc_card *card = dapm->card;
739 const char *ev_name;
740 int power, ret;
741
742 switch (event) {
743 case SND_SOC_DAPM_PRE_PMU:
744 ev_name = "PRE_PMU";
745 power = 1;
746 break;
747 case SND_SOC_DAPM_POST_PMU:
748 ev_name = "POST_PMU";
749 power = 1;
750 break;
751 case SND_SOC_DAPM_PRE_PMD:
752 ev_name = "PRE_PMD";
753 power = 0;
754 break;
755 case SND_SOC_DAPM_POST_PMD:
756 ev_name = "POST_PMD";
757 power = 0;
758 break;
759 default:
760 BUG();
761 return;
762 }
763
764 if (w->power != power)
765 return;
766
767 if (w->event && (w->event_flags & event)) {
768 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
769 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -0400770 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400771 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -0400772 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400773 if (ret < 0)
774 pr_err("%s: %s event failed: %d\n",
775 ev_name, w->name, ret);
776 }
777}
778
Mark Brownb22ead22009-06-07 12:51:26 +0100779/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200780static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +0100781 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100782{
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200783 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -0400784 struct snd_soc_dapm_widget *w;
785 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +0100786 unsigned int value = 0;
787 unsigned int mask = 0;
788 unsigned int cur_mask;
789
790 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
791 power_list)->reg;
792
793 list_for_each_entry(w, pending, power_list) {
794 cur_mask = 1 << w->shift;
795 BUG_ON(reg != w->reg);
796
797 if (w->invert)
798 power = !w->power;
799 else
800 power = w->power;
801
802 mask |= cur_mask;
803 if (power)
804 value |= cur_mask;
805
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200806 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +0100807 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
808 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100809
Mark Brown68f89ad2010-11-03 23:51:49 -0400810 /* Check for events */
811 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
812 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100813 }
814
Mark Brown81628102009-06-07 13:21:24 +0100815 if (reg >= 0) {
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200816 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +0100817 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200818 value, mask, reg, card->pop_time);
819 pop_wait(card->pop_time);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200820 snd_soc_update_bits(dapm->codec, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +0100821 }
822
823 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -0400824 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
825 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +0000826 }
Mark Brown42aa3412009-03-01 19:21:10 +0000827}
828
Mark Brownb22ead22009-06-07 12:51:26 +0100829/* Apply a DAPM power sequence.
830 *
831 * We walk over a pre-sorted list of widgets to apply power to. In
832 * order to minimise the number of writes to the device required
833 * multiple widgets will be updated in a single write where possible.
834 * Currently anything that requires more than a single write is not
835 * handled.
836 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200837static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
838 struct list_head *list, int event, int sort[])
Mark Brownb22ead22009-06-07 12:51:26 +0100839{
840 struct snd_soc_dapm_widget *w, *n;
841 LIST_HEAD(pending);
842 int cur_sort = -1;
843 int cur_reg = SND_SOC_NOPM;
Mark Brown163cac02009-06-07 10:12:52 +0100844 int ret;
845
Mark Brownb22ead22009-06-07 12:51:26 +0100846 list_for_each_entry_safe(w, n, list, power_list) {
847 ret = 0;
848
849 /* Do we need to apply any queued changes? */
850 if (sort[w->id] != cur_sort || w->reg != cur_reg) {
851 if (!list_empty(&pending))
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200852 dapm_seq_run_coalesced(dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +0100853
854 INIT_LIST_HEAD(&pending);
855 cur_sort = -1;
856 cur_reg = SND_SOC_NOPM;
857 }
858
Mark Brown163cac02009-06-07 10:12:52 +0100859 switch (w->id) {
860 case snd_soc_dapm_pre:
861 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100862 list_for_each_entry_safe_continue(w, n, list,
863 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100864
Mark Brownb22ead22009-06-07 12:51:26 +0100865 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100866 ret = w->event(w,
867 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100868 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100869 ret = w->event(w,
870 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +0100871 break;
872
873 case snd_soc_dapm_post:
874 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100875 list_for_each_entry_safe_continue(w, n, list,
876 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100877
Mark Brownb22ead22009-06-07 12:51:26 +0100878 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100879 ret = w->event(w,
880 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100881 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100882 ret = w->event(w,
883 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100884 break;
885
886 case snd_soc_dapm_input:
887 case snd_soc_dapm_output:
888 case snd_soc_dapm_hp:
889 case snd_soc_dapm_mic:
890 case snd_soc_dapm_line:
891 case snd_soc_dapm_spk:
892 /* No register support currently */
Mark Brownb22ead22009-06-07 12:51:26 +0100893 ret = dapm_generic_apply_power(w);
Mark Brown163cac02009-06-07 10:12:52 +0100894 break;
895
896 default:
Mark Brown81628102009-06-07 13:21:24 +0100897 /* Queue it up for application */
898 cur_sort = sort[w->id];
899 cur_reg = w->reg;
900 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))
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200910 dapm_seq_run_coalesced(dapm, &pending);
Mark Brown163cac02009-06-07 10:12:52 +0100911}
912
Mark Brown42aa3412009-03-01 19:21:10 +0000913/*
Richard Purdie2b97eab2006-10-06 18:32:18 +0200914 * Scan each dapm widget for complete audio path.
915 * A complete path is a route that has valid endpoints i.e.:-
916 *
917 * o DAC to output pin.
918 * o Input Pin to ADC.
919 * o Input pin to Output pin (bypass, sidetone)
920 * o DAC to ADC (loopback).
921 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200922static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200923{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200924 struct snd_soc_card *card = dapm->codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200925 struct snd_soc_dapm_widget *w;
Mark Brown291f3bb2009-06-07 13:57:17 +0100926 LIST_HEAD(up_list);
927 LIST_HEAD(down_list);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100928 int ret = 0;
Mark Brown38357ab2009-06-06 19:03:23 +0100929 int power;
Mark Brown452c5ea2009-05-17 21:41:23 +0100930 int sys_power = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200931
Mark Brown84e90932010-11-04 00:07:02 -0400932 trace_snd_soc_dapm_start(card);
933
Mark Brown6d3ddc82009-05-16 17:47:29 +0100934 /* Check which widgets we need to power and store them in
935 * lists indicating if they should be powered up or down.
936 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200937 list_for_each_entry(w, &dapm->widgets, list) {
Mark Brown6d3ddc82009-05-16 17:47:29 +0100938 switch (w->id) {
939 case snd_soc_dapm_pre:
Mark Brown291f3bb2009-06-07 13:57:17 +0100940 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100941 break;
942 case snd_soc_dapm_post:
Mark Brown291f3bb2009-06-07 13:57:17 +0100943 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100944 break;
945
946 default:
947 if (!w->power_check)
948 continue;
949
Mark Brown99497882010-05-07 20:24:05 +0100950 if (!w->force)
Mark Brown50b6bce2009-11-23 13:11:53 +0000951 power = w->power_check(w);
Mark Brown99497882010-05-07 20:24:05 +0100952 else
953 power = 1;
954 if (power)
955 sys_power = 1;
Mark Brown452c5ea2009-05-17 21:41:23 +0100956
Mark Brown6d3ddc82009-05-16 17:47:29 +0100957 if (w->power == power)
958 continue;
959
Mark Brown84e90932010-11-04 00:07:02 -0400960 trace_snd_soc_dapm_widget_power(w, power);
961
Mark Brown6d3ddc82009-05-16 17:47:29 +0100962 if (power)
Mark Brown291f3bb2009-06-07 13:57:17 +0100963 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100964 else
Mark Brown291f3bb2009-06-07 13:57:17 +0100965 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100966
967 w->power = power;
968 break;
969 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200970 }
971
Mark Brownb14b76a2009-08-17 11:55:38 +0100972 /* If there are no DAPM widgets then try to figure out power from the
973 * event type.
974 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200975 if (list_empty(&dapm->widgets)) {
Mark Brownb14b76a2009-08-17 11:55:38 +0100976 switch (event) {
977 case SND_SOC_DAPM_STREAM_START:
978 case SND_SOC_DAPM_STREAM_RESUME:
979 sys_power = 1;
980 break;
Mark Brown50b6bce2009-11-23 13:11:53 +0000981 case SND_SOC_DAPM_STREAM_SUSPEND:
982 sys_power = 0;
983 break;
Mark Brownb14b76a2009-08-17 11:55:38 +0100984 case SND_SOC_DAPM_STREAM_NOP:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200985 switch (dapm->bias_level) {
Mark Browna96ca332010-01-19 22:49:43 +0000986 case SND_SOC_BIAS_STANDBY:
987 case SND_SOC_BIAS_OFF:
988 sys_power = 0;
989 break;
990 default:
991 sys_power = 1;
992 break;
993 }
Mark Brown50b6bce2009-11-23 13:11:53 +0000994 break;
Mark Brownb14b76a2009-08-17 11:55:38 +0100995 default:
996 break;
997 }
998 }
999
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001000 if (sys_power && dapm->bias_level == SND_SOC_BIAS_OFF) {
1001 ret = snd_soc_dapm_set_bias_level(card, dapm,
Mark Browna96ca332010-01-19 22:49:43 +00001002 SND_SOC_BIAS_STANDBY);
1003 if (ret != 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001004 dev_err(dapm->dev,
1005 "Failed to turn on bias: %d\n", ret);
Mark Browna96ca332010-01-19 22:49:43 +00001006 }
1007
Mark Brown452c5ea2009-05-17 21:41:23 +01001008 /* If we're changing to all on or all off then prepare */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001009 if ((sys_power && dapm->bias_level == SND_SOC_BIAS_STANDBY) ||
1010 (!sys_power && dapm->bias_level == SND_SOC_BIAS_ON)) {
1011 ret = snd_soc_dapm_set_bias_level(card, dapm, SND_SOC_BIAS_PREPARE);
Mark Brown452c5ea2009-05-17 21:41:23 +01001012 if (ret != 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001013 dev_err(dapm->dev,
1014 "Failed to prepare bias: %d\n", ret);
Mark Brown452c5ea2009-05-17 21:41:23 +01001015 }
1016
Mark Brown6d3ddc82009-05-16 17:47:29 +01001017 /* Power down widgets first; try to avoid amplifying pops. */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001018 dapm_seq_run(dapm, &down_list, event, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001019
1020 /* Now power up. */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001021 dapm_seq_run(dapm, &up_list, event, dapm_up_seq);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001022
Mark Brown452c5ea2009-05-17 21:41:23 +01001023 /* If we just powered the last thing off drop to standby bias */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001024 if (dapm->bias_level == SND_SOC_BIAS_PREPARE && !sys_power) {
1025 ret = snd_soc_dapm_set_bias_level(card, dapm, SND_SOC_BIAS_STANDBY);
Mark Brown452c5ea2009-05-17 21:41:23 +01001026 if (ret != 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001027 dev_err(dapm->dev,
1028 "Failed to apply standby bias: %d\n", ret);
Mark Brown452c5ea2009-05-17 21:41:23 +01001029 }
1030
Mark Browna96ca332010-01-19 22:49:43 +00001031 /* If we're in standby and can support bias off then do that */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001032 if (dapm->bias_level == SND_SOC_BIAS_STANDBY &&
1033 dapm->idle_bias_off) {
1034 ret = snd_soc_dapm_set_bias_level(card, dapm, SND_SOC_BIAS_OFF);
Mark Browna96ca332010-01-19 22:49:43 +00001035 if (ret != 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001036 dev_err(dapm->dev,
1037 "Failed to turn off bias: %d\n", ret);
Mark Browna96ca332010-01-19 22:49:43 +00001038 }
1039
Mark Brown452c5ea2009-05-17 21:41:23 +01001040 /* If we just powered up then move to active bias */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001041 if (dapm->bias_level == SND_SOC_BIAS_PREPARE && sys_power) {
1042 ret = snd_soc_dapm_set_bias_level(card, dapm, SND_SOC_BIAS_ON);
Mark Brown452c5ea2009-05-17 21:41:23 +01001043 if (ret != 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001044 dev_err(dapm->dev,
1045 "Failed to apply active bias: %d\n", ret);
Mark Brown452c5ea2009-05-17 21:41:23 +01001046 }
1047
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001048 pop_dbg(dapm->dev, card->pop_time,
1049 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001050 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001051
Mark Brown84e90932010-11-04 00:07:02 -04001052 trace_snd_soc_dapm_done(card);
1053
Mark Brown42aa3412009-03-01 19:21:10 +00001054 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001055}
1056
Mark Brown79fb9382009-08-21 16:38:13 +01001057#ifdef CONFIG_DEBUG_FS
1058static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1059{
1060 file->private_data = inode->i_private;
1061 return 0;
1062}
1063
1064static ssize_t dapm_widget_power_read_file(struct file *file,
1065 char __user *user_buf,
1066 size_t count, loff_t *ppos)
1067{
1068 struct snd_soc_dapm_widget *w = file->private_data;
1069 char *buf;
1070 int in, out;
1071 ssize_t ret;
1072 struct snd_soc_dapm_path *p = NULL;
1073
1074 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1075 if (!buf)
1076 return -ENOMEM;
1077
1078 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001079 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001080 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001081 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001082
Mark Brownd033c362009-12-04 15:25:56 +00001083 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001084 w->name, w->power ? "On" : "Off", in, out);
1085
Mark Brownd033c362009-12-04 15:25:56 +00001086 if (w->reg >= 0)
1087 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1088 " - R%d(0x%x) bit %d",
1089 w->reg, w->reg, w->shift);
1090
1091 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1092
Mark Brown3eef08b2009-09-14 16:49:00 +01001093 if (w->sname)
1094 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1095 w->sname,
1096 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001097
1098 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001099 if (p->connected && !p->connected(w, p->sink))
1100 continue;
1101
Mark Brown79fb9382009-08-21 16:38:13 +01001102 if (p->connect)
1103 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1104 " in %s %s\n",
1105 p->name ? p->name : "static",
1106 p->source->name);
1107 }
1108 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001109 if (p->connected && !p->connected(w, p->sink))
1110 continue;
1111
Mark Brown79fb9382009-08-21 16:38:13 +01001112 if (p->connect)
1113 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1114 " out %s %s\n",
1115 p->name ? p->name : "static",
1116 p->sink->name);
1117 }
1118
1119 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1120
1121 kfree(buf);
1122 return ret;
1123}
1124
1125static const struct file_operations dapm_widget_power_fops = {
1126 .open = dapm_widget_power_open_file,
1127 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001128 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001129};
1130
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001131void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001132{
1133 struct snd_soc_dapm_widget *w;
1134 struct dentry *d;
1135
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001136 if (!dapm->debugfs_dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001137 return;
1138
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001139 list_for_each_entry(w, &dapm->widgets, list) {
Mark Brown79fb9382009-08-21 16:38:13 +01001140 if (!w->name)
1141 continue;
1142
1143 d = debugfs_create_file(w->name, 0444,
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001144 dapm->debugfs_dapm, w,
Mark Brown79fb9382009-08-21 16:38:13 +01001145 &dapm_widget_power_fops);
1146 if (!d)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001147 dev_warn(w->dapm->dev,
1148 "ASoC: Failed to create %s debugfs file\n",
1149 w->name);
Mark Brown79fb9382009-08-21 16:38:13 +01001150 }
1151}
1152#else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001153void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001154{
1155}
1156#endif
1157
Richard Purdie2b97eab2006-10-06 18:32:18 +02001158/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001159static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001160 struct snd_kcontrol *kcontrol, int change,
1161 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001162{
1163 struct snd_soc_dapm_path *path;
1164 int found = 0;
1165
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001166 if (widget->id != snd_soc_dapm_mux &&
1167 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001168 return -ENODEV;
1169
Mark Brown3a655772009-10-05 17:23:30 +01001170 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001171 return 0;
1172
1173 /* find dapm widget path assoc with kcontrol */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001174 list_for_each_entry(path, &widget->dapm->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001175 if (path->kcontrol != kcontrol)
1176 continue;
1177
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001178 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001179 continue;
1180
1181 found = 1;
1182 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001183 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001184 path->connect = 1; /* new connection */
1185 else
1186 path->connect = 0; /* old connection must be powered down */
1187 }
1188
Mark Brownb91b8fa2010-01-20 18:18:35 +00001189 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001190 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001191
1192 return 0;
1193}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001194
Milan plzik1b075e32008-01-10 14:39:46 +01001195/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001196static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001197 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001198{
1199 struct snd_soc_dapm_path *path;
1200 int found = 0;
1201
Milan plzik1b075e32008-01-10 14:39:46 +01001202 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001203 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001204 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001205 return -ENODEV;
1206
Richard Purdie2b97eab2006-10-06 18:32:18 +02001207 /* find dapm widget path assoc with kcontrol */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001208 list_for_each_entry(path, &widget->dapm->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001209 if (path->kcontrol != kcontrol)
1210 continue;
1211
1212 /* found, now check type */
1213 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001214 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001215 break;
1216 }
1217
Mark Brownb91b8fa2010-01-20 18:18:35 +00001218 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001219 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001220
1221 return 0;
1222}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001223
1224/* show dapm widget status in sys fs */
1225static ssize_t dapm_widget_show(struct device *dev,
1226 struct device_attribute *attr, char *buf)
1227{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001228 struct snd_soc_pcm_runtime *rtd =
1229 container_of(dev, struct snd_soc_pcm_runtime, dev);
1230 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001231 struct snd_soc_dapm_widget *w;
1232 int count = 0;
1233 char *state = "not set";
1234
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001235 list_for_each_entry(w, &codec->dapm.widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001236
1237 /* only display widgets that burnm power */
1238 switch (w->id) {
1239 case snd_soc_dapm_hp:
1240 case snd_soc_dapm_mic:
1241 case snd_soc_dapm_spk:
1242 case snd_soc_dapm_line:
1243 case snd_soc_dapm_micbias:
1244 case snd_soc_dapm_dac:
1245 case snd_soc_dapm_adc:
1246 case snd_soc_dapm_pga:
1247 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001248 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001249 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001250 if (w->name)
1251 count += sprintf(buf + count, "%s: %s\n",
1252 w->name, w->power ? "On":"Off");
1253 break;
1254 default:
1255 break;
1256 }
1257 }
1258
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001259 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001260 case SND_SOC_BIAS_ON:
1261 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001262 break;
Mark Brown0be98982008-05-19 12:31:28 +02001263 case SND_SOC_BIAS_PREPARE:
1264 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001265 break;
Mark Brown0be98982008-05-19 12:31:28 +02001266 case SND_SOC_BIAS_STANDBY:
1267 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001268 break;
Mark Brown0be98982008-05-19 12:31:28 +02001269 case SND_SOC_BIAS_OFF:
1270 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001271 break;
1272 }
1273 count += sprintf(buf + count, "PM State: %s\n", state);
1274
1275 return count;
1276}
1277
1278static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1279
1280int snd_soc_dapm_sys_add(struct device *dev)
1281{
Troy Kisky12ef1932008-10-13 17:42:14 -07001282 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001283}
1284
1285static void snd_soc_dapm_sys_remove(struct device *dev)
1286{
Mark Brownaef90842009-05-16 17:53:16 +01001287 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001288}
1289
1290/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001291static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001292{
1293 struct snd_soc_dapm_widget *w, *next_w;
1294 struct snd_soc_dapm_path *p, *next_p;
1295
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001296 list_for_each_entry_safe(w, next_w, &dapm->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001297 list_del(&w->list);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001298 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001299 kfree(w);
1300 }
1301
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001302 list_for_each_entry_safe(p, next_p, &dapm->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001303 list_del(&p->list);
1304 kfree(p->long_name);
1305 kfree(p);
1306 }
1307}
1308
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001309static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001310 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001311{
1312 struct snd_soc_dapm_widget *w;
1313
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001314 list_for_each_entry(w, &dapm->widgets, list) {
Liam Girdwooda5302182008-07-07 13:35:17 +01001315 if (!strcmp(w->name, pin)) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001316 dev_dbg(w->dapm->dev, "dapm: pin %s = %d\n",
1317 pin, status);
Liam Girdwooda5302182008-07-07 13:35:17 +01001318 w->connected = status;
Mark Brown5b9e87c2010-03-22 13:36:13 +00001319 /* Allow disabling of forced pins */
1320 if (status == 0)
1321 w->force = 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001322 return 0;
1323 }
1324 }
1325
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001326 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001327 return -EINVAL;
1328}
1329
Richard Purdie2b97eab2006-10-06 18:32:18 +02001330/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001331 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001332 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001333 *
1334 * Walks all dapm audio paths and powers widgets according to their
1335 * stream or path usage.
1336 *
1337 * Returns 0 for success.
1338 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001339int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001340{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001341 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001342}
Liam Girdwooda5302182008-07-07 13:35:17 +01001343EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001344
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001345static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001346 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001347{
1348 struct snd_soc_dapm_path *path;
1349 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001350 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001351 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001352 const char *source;
1353 char prefixed_sink[80];
1354 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001355 int ret = 0;
1356
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001357 if (dapm->codec->name_prefix) {
1358 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1359 dapm->codec->name_prefix, route->sink);
1360 sink = prefixed_sink;
1361 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1362 dapm->codec->name_prefix, route->source);
1363 source = prefixed_source;
1364 } else {
1365 sink = route->sink;
1366 source = route->source;
1367 }
1368
Richard Purdie2b97eab2006-10-06 18:32:18 +02001369 /* find src and dest widgets */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001370 list_for_each_entry(w, &dapm->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001371
1372 if (!wsink && !(strcmp(w->name, sink))) {
1373 wsink = w;
1374 continue;
1375 }
1376 if (!wsource && !(strcmp(w->name, source))) {
1377 wsource = w;
1378 }
1379 }
1380
1381 if (wsource == NULL || wsink == NULL)
1382 return -ENODEV;
1383
1384 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1385 if (!path)
1386 return -ENOMEM;
1387
1388 path->source = wsource;
1389 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001390 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001391 INIT_LIST_HEAD(&path->list);
1392 INIT_LIST_HEAD(&path->list_source);
1393 INIT_LIST_HEAD(&path->list_sink);
1394
1395 /* check for external widgets */
1396 if (wsink->id == snd_soc_dapm_input) {
1397 if (wsource->id == snd_soc_dapm_micbias ||
1398 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001399 wsource->id == snd_soc_dapm_line ||
1400 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001401 wsink->ext = 1;
1402 }
1403 if (wsource->id == snd_soc_dapm_output) {
1404 if (wsink->id == snd_soc_dapm_spk ||
1405 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001406 wsink->id == snd_soc_dapm_line ||
1407 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001408 wsource->ext = 1;
1409 }
1410
1411 /* connect static paths */
1412 if (control == NULL) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001413 list_add(&path->list, &dapm->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001414 list_add(&path->list_sink, &wsink->sources);
1415 list_add(&path->list_source, &wsource->sinks);
1416 path->connect = 1;
1417 return 0;
1418 }
1419
1420 /* connect dynamic paths */
1421 switch(wsink->id) {
1422 case snd_soc_dapm_adc:
1423 case snd_soc_dapm_dac:
1424 case snd_soc_dapm_pga:
1425 case snd_soc_dapm_input:
1426 case snd_soc_dapm_output:
1427 case snd_soc_dapm_micbias:
1428 case snd_soc_dapm_vmid:
1429 case snd_soc_dapm_pre:
1430 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001431 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001432 case snd_soc_dapm_aif_in:
1433 case snd_soc_dapm_aif_out:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001434 list_add(&path->list, &dapm->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001435 list_add(&path->list_sink, &wsink->sources);
1436 list_add(&path->list_source, &wsource->sinks);
1437 path->connect = 1;
1438 return 0;
1439 case snd_soc_dapm_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001440 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001441 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Richard Purdie2b97eab2006-10-06 18:32:18 +02001442 &wsink->kcontrols[0]);
1443 if (ret != 0)
1444 goto err;
1445 break;
1446 case snd_soc_dapm_switch:
1447 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001448 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001449 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001450 if (ret != 0)
1451 goto err;
1452 break;
1453 case snd_soc_dapm_hp:
1454 case snd_soc_dapm_mic:
1455 case snd_soc_dapm_line:
1456 case snd_soc_dapm_spk:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001457 list_add(&path->list, &dapm->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001458 list_add(&path->list_sink, &wsink->sources);
1459 list_add(&path->list_source, &wsource->sinks);
1460 path->connect = 0;
1461 return 0;
1462 }
1463 return 0;
1464
1465err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001466 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1467 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001468 kfree(path);
1469 return ret;
1470}
Mark Brown105f1c22008-05-13 14:52:19 +02001471
1472/**
Mark Brown105f1c22008-05-13 14:52:19 +02001473 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001474 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02001475 * @route: audio routes
1476 * @num: number of routes
1477 *
1478 * Connects 2 dapm widgets together via a named audio path. The sink is
1479 * the widget receiving the audio signal, whilst the source is the sender
1480 * of the audio signal.
1481 *
1482 * Returns 0 for success else error. On error all resources can be freed
1483 * with a call to snd_soc_card_free().
1484 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001485int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02001486 const struct snd_soc_dapm_route *route, int num)
1487{
1488 int i, ret;
1489
1490 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001491 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001492 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001493 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1494 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02001495 return ret;
1496 }
1497 route++;
1498 }
1499
1500 return 0;
1501}
1502EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1503
1504/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001505 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001506 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001507 *
1508 * Checks the codec for any new dapm widgets and creates them if found.
1509 *
1510 * Returns 0 for success.
1511 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001512int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001513{
1514 struct snd_soc_dapm_widget *w;
1515
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001516 list_for_each_entry(w, &dapm->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001517 {
1518 if (w->new)
1519 continue;
1520
1521 switch(w->id) {
1522 case snd_soc_dapm_switch:
1523 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001524 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001525 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001526 dapm_new_mixer(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001527 break;
1528 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001529 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001530 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001531 dapm_new_mux(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001532 break;
1533 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01001534 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01001535 w->power_check = dapm_adc_check_power;
1536 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001537 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01001538 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01001539 w->power_check = dapm_dac_check_power;
1540 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001541 case snd_soc_dapm_pga:
Mark Brownb75576d2009-04-20 17:56:13 +01001542 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001543 dapm_new_pga(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001544 break;
1545 case snd_soc_dapm_input:
1546 case snd_soc_dapm_output:
1547 case snd_soc_dapm_micbias:
1548 case snd_soc_dapm_spk:
1549 case snd_soc_dapm_hp:
1550 case snd_soc_dapm_mic:
1551 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001552 w->power_check = dapm_generic_check_power;
1553 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001554 case snd_soc_dapm_supply:
1555 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001556 case snd_soc_dapm_vmid:
1557 case snd_soc_dapm_pre:
1558 case snd_soc_dapm_post:
1559 break;
1560 }
1561 w->new = 1;
1562 }
1563
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001564 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001565 return 0;
1566}
1567EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1568
1569/**
1570 * snd_soc_dapm_get_volsw - dapm mixer get callback
1571 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001572 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001573 *
1574 * Callback to get the value of a dapm mixer control.
1575 *
1576 * Returns 0 for success.
1577 */
1578int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1579 struct snd_ctl_elem_value *ucontrol)
1580{
1581 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001582 struct soc_mixer_control *mc =
1583 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001584 unsigned int reg = mc->reg;
1585 unsigned int shift = mc->shift;
1586 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001587 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001588 unsigned int invert = mc->invert;
1589 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001590
Richard Purdie2b97eab2006-10-06 18:32:18 +02001591 ucontrol->value.integer.value[0] =
1592 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1593 if (shift != rshift)
1594 ucontrol->value.integer.value[1] =
1595 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1596 if (invert) {
1597 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001598 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001599 if (shift != rshift)
1600 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001601 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001602 }
1603
1604 return 0;
1605}
1606EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1607
1608/**
1609 * snd_soc_dapm_put_volsw - dapm mixer set callback
1610 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001611 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001612 *
1613 * Callback to set the value of a dapm mixer control.
1614 *
1615 * Returns 0 for success.
1616 */
1617int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1618 struct snd_ctl_elem_value *ucontrol)
1619{
1620 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001621 struct soc_mixer_control *mc =
1622 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001623 unsigned int reg = mc->reg;
1624 unsigned int shift = mc->shift;
1625 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001626 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001627 unsigned int mask = (1 << fls(max)) - 1;
1628 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001629 unsigned int val, val2, val_mask;
Mark Brown283375c2009-12-07 18:09:03 +00001630 int connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001631 int ret;
1632
1633 val = (ucontrol->value.integer.value[0] & mask);
1634
1635 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001636 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001637 val_mask = mask << shift;
1638 val = val << shift;
1639 if (shift != rshift) {
1640 val2 = (ucontrol->value.integer.value[1] & mask);
1641 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001642 val2 = max - val2;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001643 val_mask |= mask << rshift;
1644 val |= val2 << rshift;
1645 }
1646
1647 mutex_lock(&widget->codec->mutex);
1648 widget->value = val;
1649
Mark Brown283375c2009-12-07 18:09:03 +00001650 if (snd_soc_test_bits(widget->codec, reg, val_mask, val)) {
1651 if (val)
1652 /* new connection */
1653 connect = invert ? 0:1;
1654 else
1655 /* old connection must be powered down */
1656 connect = invert ? 1:0;
1657
1658 dapm_mixer_update_power(widget, kcontrol, connect);
1659 }
1660
Richard Purdie2b97eab2006-10-06 18:32:18 +02001661 if (widget->event) {
1662 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001663 ret = widget->event(widget, kcontrol,
1664 SND_SOC_DAPM_PRE_REG);
1665 if (ret < 0) {
1666 ret = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001667 goto out;
Laim Girdwood9af6d952008-01-10 14:41:02 +01001668 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001669 }
1670 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1671 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001672 ret = widget->event(widget, kcontrol,
1673 SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001674 } else
1675 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1676
1677out:
1678 mutex_unlock(&widget->codec->mutex);
1679 return ret;
1680}
1681EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1682
1683/**
1684 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1685 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001686 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001687 *
1688 * Callback to get the value of a dapm enumerated double mixer control.
1689 *
1690 * Returns 0 for success.
1691 */
1692int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1693 struct snd_ctl_elem_value *ucontrol)
1694{
1695 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1696 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001697 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001698
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001699 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001700 ;
1701 val = snd_soc_read(widget->codec, e->reg);
1702 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1703 if (e->shift_l != e->shift_r)
1704 ucontrol->value.enumerated.item[1] =
1705 (val >> e->shift_r) & (bitmask - 1);
1706
1707 return 0;
1708}
1709EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1710
1711/**
1712 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1713 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001714 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001715 *
1716 * Callback to set the value of a dapm enumerated double mixer control.
1717 *
1718 * Returns 0 for success.
1719 */
1720int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1721 struct snd_ctl_elem_value *ucontrol)
1722{
1723 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1724 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001725 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001726 unsigned int mask, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001727 int ret = 0;
1728
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001729 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001730 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001731 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001732 return -EINVAL;
1733 mux = ucontrol->value.enumerated.item[0];
1734 val = mux << e->shift_l;
1735 mask = (bitmask - 1) << e->shift_l;
1736 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001737 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001738 return -EINVAL;
1739 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1740 mask |= (bitmask - 1) << e->shift_r;
1741 }
1742
1743 mutex_lock(&widget->codec->mutex);
1744 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01001745 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1746 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01001747
1748 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1749 ret = widget->event(widget,
1750 kcontrol, SND_SOC_DAPM_PRE_REG);
1751 if (ret < 0)
1752 goto out;
1753 }
1754
1755 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1756
1757 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1758 ret = widget->event(widget,
1759 kcontrol, SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001760
1761out:
1762 mutex_unlock(&widget->codec->mutex);
1763 return ret;
1764}
1765EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1766
1767/**
Mark Brownd2b247a2009-10-06 15:21:04 +01001768 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1769 * @kcontrol: mixer control
1770 * @ucontrol: control element information
1771 *
1772 * Returns 0 for success.
1773 */
1774int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1775 struct snd_ctl_elem_value *ucontrol)
1776{
1777 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1778
1779 ucontrol->value.enumerated.item[0] = widget->value;
1780
1781 return 0;
1782}
1783EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1784
1785/**
1786 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1787 * @kcontrol: mixer control
1788 * @ucontrol: control element information
1789 *
1790 * Returns 0 for success.
1791 */
1792int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
1793 struct snd_ctl_elem_value *ucontrol)
1794{
1795 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1796 struct soc_enum *e =
1797 (struct soc_enum *)kcontrol->private_value;
1798 int change;
1799 int ret = 0;
1800
1801 if (ucontrol->value.enumerated.item[0] >= e->max)
1802 return -EINVAL;
1803
1804 mutex_lock(&widget->codec->mutex);
1805
1806 change = widget->value != ucontrol->value.enumerated.item[0];
1807 widget->value = ucontrol->value.enumerated.item[0];
1808 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
1809
1810 mutex_unlock(&widget->codec->mutex);
1811 return ret;
1812}
1813EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
1814
1815/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001816 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1817 * callback
1818 * @kcontrol: mixer control
1819 * @ucontrol: control element information
1820 *
1821 * Callback to get the value of a dapm semi enumerated double mixer control.
1822 *
1823 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1824 * used for handling bitfield coded enumeration for example.
1825 *
1826 * Returns 0 for success.
1827 */
1828int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1829 struct snd_ctl_elem_value *ucontrol)
1830{
1831 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001832 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001833 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001834
1835 reg_val = snd_soc_read(widget->codec, e->reg);
1836 val = (reg_val >> e->shift_l) & e->mask;
1837 for (mux = 0; mux < e->max; mux++) {
1838 if (val == e->values[mux])
1839 break;
1840 }
1841 ucontrol->value.enumerated.item[0] = mux;
1842 if (e->shift_l != e->shift_r) {
1843 val = (reg_val >> e->shift_r) & e->mask;
1844 for (mux = 0; mux < e->max; mux++) {
1845 if (val == e->values[mux])
1846 break;
1847 }
1848 ucontrol->value.enumerated.item[1] = mux;
1849 }
1850
1851 return 0;
1852}
1853EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1854
1855/**
1856 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1857 * callback
1858 * @kcontrol: mixer control
1859 * @ucontrol: control element information
1860 *
1861 * Callback to set the value of a dapm semi enumerated double mixer control.
1862 *
1863 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1864 * used for handling bitfield coded enumeration for example.
1865 *
1866 * Returns 0 for success.
1867 */
1868int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1869 struct snd_ctl_elem_value *ucontrol)
1870{
1871 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001872 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001873 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001874 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001875 int ret = 0;
1876
1877 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1878 return -EINVAL;
1879 mux = ucontrol->value.enumerated.item[0];
1880 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1881 mask = e->mask << e->shift_l;
1882 if (e->shift_l != e->shift_r) {
1883 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1884 return -EINVAL;
1885 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1886 mask |= e->mask << e->shift_r;
1887 }
1888
1889 mutex_lock(&widget->codec->mutex);
1890 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01001891 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1892 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01001893
1894 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1895 ret = widget->event(widget,
1896 kcontrol, SND_SOC_DAPM_PRE_REG);
1897 if (ret < 0)
1898 goto out;
1899 }
1900
1901 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1902
1903 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1904 ret = widget->event(widget,
1905 kcontrol, SND_SOC_DAPM_POST_REG);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001906
1907out:
1908 mutex_unlock(&widget->codec->mutex);
1909 return ret;
1910}
1911EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1912
1913/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00001914 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1915 *
1916 * @kcontrol: mixer control
1917 * @uinfo: control element information
1918 *
1919 * Callback to provide information about a pin switch control.
1920 */
1921int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1922 struct snd_ctl_elem_info *uinfo)
1923{
1924 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1925 uinfo->count = 1;
1926 uinfo->value.integer.min = 0;
1927 uinfo->value.integer.max = 1;
1928
1929 return 0;
1930}
1931EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1932
1933/**
1934 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1935 *
1936 * @kcontrol: mixer control
1937 * @ucontrol: Value
1938 */
1939int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
1940 struct snd_ctl_elem_value *ucontrol)
1941{
1942 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1943 const char *pin = (const char *)kcontrol->private_value;
1944
1945 mutex_lock(&codec->mutex);
1946
1947 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001948 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00001949
1950 mutex_unlock(&codec->mutex);
1951
1952 return 0;
1953}
1954EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
1955
1956/**
1957 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1958 *
1959 * @kcontrol: mixer control
1960 * @ucontrol: Value
1961 */
1962int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
1963 struct snd_ctl_elem_value *ucontrol)
1964{
1965 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1966 const char *pin = (const char *)kcontrol->private_value;
1967
1968 mutex_lock(&codec->mutex);
1969
1970 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001971 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00001972 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001973 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00001974
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001975 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00001976
1977 mutex_unlock(&codec->mutex);
1978
1979 return 0;
1980}
1981EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
1982
1983/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001984 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001985 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001986 * @widget: widget template
1987 *
1988 * Creates a new dapm control based upon the template.
1989 *
1990 * Returns 0 for success else error.
1991 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001992int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02001993 const struct snd_soc_dapm_widget *widget)
1994{
1995 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001996 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001997
1998 if ((w = dapm_cnew_widget(widget)) == NULL)
1999 return -ENOMEM;
2000
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002001 name_len = strlen(widget->name) + 1;
2002 if (dapm->codec->name_prefix)
2003 name_len += 1 + strlen(dapm->codec->name_prefix);
2004 w->name = kmalloc(name_len, GFP_KERNEL);
2005 if (w->name == NULL) {
2006 kfree(w);
2007 return -ENOMEM;
2008 }
2009 if (dapm->codec->name_prefix)
2010 snprintf(w->name, name_len, "%s %s",
2011 dapm->codec->name_prefix, widget->name);
2012 else
2013 snprintf(w->name, name_len, "%s", widget->name);
2014
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002015 w->dapm = dapm;
2016 w->codec = dapm->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002017 INIT_LIST_HEAD(&w->sources);
2018 INIT_LIST_HEAD(&w->sinks);
2019 INIT_LIST_HEAD(&w->list);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002020 list_add(&w->list, &dapm->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002021
2022 /* machine layer set ups unconnected pins and insertions */
2023 w->connected = 1;
2024 return 0;
2025}
2026EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2027
2028/**
Mark Brown4ba13272008-05-13 14:51:19 +02002029 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002030 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002031 * @widget: widget array
2032 * @num: number of widgets
2033 *
2034 * Creates new DAPM controls based upon the templates.
2035 *
2036 * Returns 0 for success else error.
2037 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002038int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002039 const struct snd_soc_dapm_widget *widget,
2040 int num)
2041{
2042 int i, ret;
2043
2044 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002045 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002046 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002047 dev_err(dapm->dev,
2048 "ASoC: Failed to create DAPM control %s: %d\n",
2049 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002050 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002051 }
Mark Brown4ba13272008-05-13 14:51:19 +02002052 widget++;
2053 }
2054 return 0;
2055}
2056EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2057
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002058static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002059 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002060{
2061 struct snd_soc_dapm_widget *w;
2062
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002063 list_for_each_entry(w, &dapm->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002064 {
2065 if (!w->sname)
2066 continue;
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002067 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2068 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002069 if (strstr(w->sname, stream)) {
2070 switch(event) {
2071 case SND_SOC_DAPM_STREAM_START:
2072 w->active = 1;
2073 break;
2074 case SND_SOC_DAPM_STREAM_STOP:
2075 w->active = 0;
2076 break;
2077 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002078 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002079 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002080 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2081 break;
2082 }
2083 }
2084 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002085
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002086 dapm_power_widgets(dapm, event);
2087}
2088
2089/**
2090 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2091 * @rtd: PCM runtime data
2092 * @stream: stream name
2093 * @event: stream event
2094 *
2095 * Sends a stream event to the dapm core. The core then makes any
2096 * necessary widget power changes.
2097 *
2098 * Returns 0 for success else error.
2099 */
2100int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2101 const char *stream, int event)
2102{
2103 struct snd_soc_codec *codec = rtd->codec;
2104
2105 if (stream == NULL)
2106 return 0;
2107
2108 mutex_lock(&codec->mutex);
2109 soc_dapm_stream_event(&codec->dapm, stream, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002110 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002111 return 0;
2112}
2113EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
2114
2115/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002116 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002117 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002118 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002119 *
Mark Brown74b8f952009-06-06 11:26:15 +01002120 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002121 * a valid audio route and active audio stream.
2122 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2123 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002124 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002125int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002126{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002127 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002128}
Liam Girdwooda5302182008-07-07 13:35:17 +01002129EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002130
2131/**
Mark Brownda341832010-03-15 19:23:37 +00002132 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002133 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002134 * @pin: pin name
2135 *
2136 * Enables input/output pin regardless of any other state. This is
2137 * intended for use with microphone bias supplies used in microphone
2138 * jack detection.
2139 *
2140 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2141 * do any widget power switching.
2142 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002143int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2144 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002145{
2146 struct snd_soc_dapm_widget *w;
2147
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002148 list_for_each_entry(w, &dapm->widgets, list) {
Mark Brownda341832010-03-15 19:23:37 +00002149 if (!strcmp(w->name, pin)) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002150 dev_dbg(w->dapm->dev,
2151 "dapm: force enable pin %s\n", pin);
Mark Brownda341832010-03-15 19:23:37 +00002152 w->connected = 1;
2153 w->force = 1;
2154 return 0;
2155 }
2156 }
2157
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002158 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Mark Brownda341832010-03-15 19:23:37 +00002159 return -EINVAL;
2160}
2161EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2162
2163/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002164 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002165 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002166 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002167 *
Mark Brown74b8f952009-06-06 11:26:15 +01002168 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002169 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2170 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002171 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002172int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2173 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002174{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002175 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002176}
2177EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2178
2179/**
Mark Brown5817b522008-09-24 11:23:11 +01002180 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002181 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002182 * @pin: pin name
2183 *
2184 * Marks the specified pin as being not connected, disabling it along
2185 * any parent or child widgets. At present this is identical to
2186 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2187 * additional things such as disabling controls which only affect
2188 * paths through the pin.
2189 *
2190 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2191 * do any widget power switching.
2192 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002193int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002194{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002195 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002196}
2197EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2198
2199/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002200 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002201 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002202 * @pin: audio signal pin endpoint (or start point)
2203 *
2204 * Get audio pin status - connected or disconnected.
2205 *
2206 * Returns 1 for connected otherwise 0.
2207 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002208int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2209 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002210{
2211 struct snd_soc_dapm_widget *w;
2212
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002213 list_for_each_entry(w, &dapm->widgets, list) {
Liam Girdwooda5302182008-07-07 13:35:17 +01002214 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002215 return w->connected;
2216 }
2217
2218 return 0;
2219}
Liam Girdwooda5302182008-07-07 13:35:17 +01002220EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002221
2222/**
Mark Brown1547aba2010-05-07 21:11:40 +01002223 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002224 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01002225 * @pin: audio signal pin endpoint (or start point)
2226 *
2227 * Mark the given endpoint or pin as ignoring suspend. When the
2228 * system is disabled a path between two endpoints flagged as ignoring
2229 * suspend will not be disabled. The path must already be enabled via
2230 * normal means at suspend time, it will not be turned on if it was not
2231 * already enabled.
2232 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002233int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2234 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01002235{
2236 struct snd_soc_dapm_widget *w;
2237
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002238 list_for_each_entry(w, &dapm->widgets, list) {
Mark Brown1547aba2010-05-07 21:11:40 +01002239 if (!strcmp(w->name, pin)) {
2240 w->ignore_suspend = 1;
2241 return 0;
2242 }
2243 }
2244
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002245 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Mark Brown1547aba2010-05-07 21:11:40 +01002246 return -EINVAL;
2247}
2248EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2249
2250/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002251 * snd_soc_dapm_free - free dapm resources
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002252 * @card: SoC device
Richard Purdie2b97eab2006-10-06 18:32:18 +02002253 *
2254 * Free all dapm widgets and resources.
2255 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002256void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002257{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002258 snd_soc_dapm_sys_remove(dapm->dev);
2259 dapm_free_widgets(dapm);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002260}
2261EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2262
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002263static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01002264{
Mark Brown51737472009-06-22 13:16:51 +01002265 struct snd_soc_dapm_widget *w;
2266 LIST_HEAD(down_list);
2267 int powerdown = 0;
2268
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002269 list_for_each_entry(w, &dapm->widgets, list) {
Mark Brown51737472009-06-22 13:16:51 +01002270 if (w->power) {
2271 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002272 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002273 powerdown = 1;
2274 }
2275 }
2276
2277 /* If there were no widgets to power down we're already in
2278 * standby.
2279 */
2280 if (powerdown) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002281 snd_soc_dapm_set_bias_level(NULL, dapm, SND_SOC_BIAS_PREPARE);
2282 dapm_seq_run(dapm, &down_list, 0, dapm_down_seq);
2283 snd_soc_dapm_set_bias_level(NULL, dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01002284 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002285}
Mark Brown51737472009-06-22 13:16:51 +01002286
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002287/*
2288 * snd_soc_dapm_shutdown - callback for system shutdown
2289 */
2290void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2291{
2292 struct snd_soc_codec *codec;
2293
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002294 list_for_each_entry(codec, &card->codec_dev_list, list) {
2295 soc_dapm_shutdown_codec(&codec->dapm);
2296 snd_soc_dapm_set_bias_level(card, &codec->dapm, SND_SOC_BIAS_OFF);
2297 }
Mark Brown51737472009-06-22 13:16:51 +01002298}
2299
Richard Purdie2b97eab2006-10-06 18:32:18 +02002300/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002301MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002302MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2303MODULE_LICENSE("GPL");