blob: 4df96ec9a8134cfa33e5306b342871c36e3af673 [file] [log] [blame]
Richard Purdie2b97eab2006-10-06 18:32:18 +02001/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020012 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
Mark Brown74b8f952009-06-06 11:26:15 +010015 * DACs/ADCs.
Richard Purdie2b97eab2006-10-06 18:32:18 +020016 * o Platform power domain - can support external components i.e. amps and
17 * mic/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +020021 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
24 * Todo:
25 * o DAPM power change sequencing - allow for configurable per
26 * codec sequences.
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
30 */
31
32#include <linux/module.h>
33#include <linux/moduleparam.h>
34#include <linux/init.h>
35#include <linux/delay.h>
36#include <linux/pm.h>
37#include <linux/bitops.h>
38#include <linux/platform_device.h>
39#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020040#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020042#include <sound/core.h>
43#include <sound/pcm.h>
44#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020045#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020046#include <sound/initval.h>
47
Mark Brown84e90932010-11-04 00:07:02 -040048#include <trace/events/asoc.h>
49
Richard Purdie2b97eab2006-10-06 18:32:18 +020050/* dapm power sequences - make this per codec in the future */
51static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010052 [snd_soc_dapm_pre] = 0,
53 [snd_soc_dapm_supply] = 1,
54 [snd_soc_dapm_micbias] = 2,
Mark Brown010ff262009-08-17 17:39:22 +010055 [snd_soc_dapm_aif_in] = 3,
56 [snd_soc_dapm_aif_out] = 3,
57 [snd_soc_dapm_mic] = 4,
58 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000059 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010060 [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,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060066 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010067 [snd_soc_dapm_hp] = 10,
68 [snd_soc_dapm_spk] = 10,
69 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020070};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000071
Richard Purdie2b97eab2006-10-06 18:32:18 +020072static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010073 [snd_soc_dapm_pre] = 0,
74 [snd_soc_dapm_adc] = 1,
75 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010076 [snd_soc_dapm_spk] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060077 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010078 [snd_soc_dapm_pga] = 4,
79 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010080 [snd_soc_dapm_mixer] = 5,
81 [snd_soc_dapm_dac] = 6,
82 [snd_soc_dapm_mic] = 7,
83 [snd_soc_dapm_micbias] = 8,
84 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000085 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010086 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010087 [snd_soc_dapm_aif_in] = 10,
88 [snd_soc_dapm_aif_out] = 10,
89 [snd_soc_dapm_supply] = 11,
90 [snd_soc_dapm_post] = 12,
Richard Purdie2b97eab2006-10-06 18:32:18 +020091};
92
Troy Kisky12ef1932008-10-13 17:42:14 -070093static void pop_wait(u32 pop_time)
Mark Brown15e4c72f2008-07-02 11:51:20 +010094{
95 if (pop_time)
96 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
97}
98
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +020099static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c72f2008-07-02 11:51:20 +0100100{
101 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200102 char *buf;
103
104 if (!pop_time)
105 return;
106
107 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
108 if (buf == NULL)
109 return;
Mark Brown15e4c72f2008-07-02 11:51:20 +0100110
111 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200112 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100113 dev_info(dev, "%s", buf);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100114 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200115
116 kfree(buf);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100117}
118
Richard Purdie2b97eab2006-10-06 18:32:18 +0200119/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100120static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200121 const struct snd_soc_dapm_widget *_widget)
122{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100123 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200124}
125
Mark Brown452c5ea2009-05-17 21:41:23 +0100126/**
127 * snd_soc_dapm_set_bias_level - set the bias level for the system
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000128 * @card: audio device
Mark Brown452c5ea2009-05-17 21:41:23 +0100129 * @level: level to configure
130 *
131 * Configure the bias (power) levels for the SoC audio device.
132 *
133 * Returns 0 for success else error.
134 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000135static int snd_soc_dapm_set_bias_level(struct snd_soc_card *card,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200136 struct snd_soc_dapm_context *dapm,
137 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100138{
Mark Brown452c5ea2009-05-17 21:41:23 +0100139 int ret = 0;
140
Mark Brownf83fba82009-05-18 15:44:43 +0100141 switch (level) {
142 case SND_SOC_BIAS_ON:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200143 dev_dbg(dapm->dev, "Setting full bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100144 break;
145 case SND_SOC_BIAS_PREPARE:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200146 dev_dbg(dapm->dev, "Setting bias prepare\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100147 break;
148 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200149 dev_dbg(dapm->dev, "Setting standby bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100150 break;
151 case SND_SOC_BIAS_OFF:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200152 dev_dbg(dapm->dev, "Setting bias off\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100153 break;
154 default:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200155 dev_err(dapm->dev, "Setting invalid bias %d\n", level);
Mark Brownf83fba82009-05-18 15:44:43 +0100156 return -EINVAL;
157 }
158
Mark Brown84e90932010-11-04 00:07:02 -0400159 trace_snd_soc_bias_level_start(card, level);
160
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000161 if (card && card->set_bias_level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100162 ret = card->set_bias_level(card, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100163 if (ret == 0) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200164 if (dapm->codec && dapm->codec->driver->set_bias_level)
165 ret = dapm->codec->driver->set_bias_level(dapm->codec, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100166 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200167 dapm->bias_level = level;
Mark Brown474e09c2009-08-19 14:18:53 +0100168 }
Mark Brown1badabd2010-12-04 12:41:04 +0000169 if (ret == 0) {
170 if (card && card->set_bias_level_post)
171 ret = card->set_bias_level_post(card, level);
172 }
Mark Brown452c5ea2009-05-17 21:41:23 +0100173
Mark Brown84e90932010-11-04 00:07:02 -0400174 trace_snd_soc_bias_level_done(card, level);
175
Mark Brown452c5ea2009-05-17 21:41:23 +0100176 return ret;
177}
178
Richard Purdie2b97eab2006-10-06 18:32:18 +0200179/* set up initial codec paths */
180static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
181 struct snd_soc_dapm_path *p, int i)
182{
183 switch (w->id) {
184 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000185 case snd_soc_dapm_mixer:
186 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200187 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100188 struct soc_mixer_control *mc = (struct soc_mixer_control *)
189 w->kcontrols[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400190 unsigned int reg = mc->reg;
191 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100192 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400193 unsigned int mask = (1 << fls(max)) - 1;
194 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200195
196 val = snd_soc_read(w->codec, reg);
197 val = (val >> shift) & mask;
198
199 if ((invert && !val) || (!invert && val))
200 p->connect = 1;
201 else
202 p->connect = 0;
203 }
204 break;
205 case snd_soc_dapm_mux: {
206 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
207 int val, item, bitmask;
208
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100209 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200210 ;
211 val = snd_soc_read(w->codec, e->reg);
212 item = (val >> e->shift_l) & (bitmask - 1);
213
214 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100215 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200216 if (!(strcmp(p->name, e->texts[i])) && item == i)
217 p->connect = 1;
218 }
219 }
220 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000221 case snd_soc_dapm_virt_mux: {
222 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
223
224 p->connect = 0;
225 /* since a virtual mux has no backing registers to
226 * decide which path to connect, it will try to match
227 * with the first enumeration. This is to ensure
228 * that the default mux choice (the first) will be
229 * correctly powered up during initialization.
230 */
231 if (!strcmp(p->name, e->texts[0]))
232 p->connect = 1;
233 }
234 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200235 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200236 struct soc_enum *e = (struct soc_enum *)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200237 w->kcontrols[i].private_value;
238 int val, item;
239
240 val = snd_soc_read(w->codec, e->reg);
241 val = (val >> e->shift_l) & e->mask;
242 for (item = 0; item < e->max; item++) {
243 if (val == e->values[item])
244 break;
245 }
246
247 p->connect = 0;
248 for (i = 0; i < e->max; i++) {
249 if (!(strcmp(p->name, e->texts[i])) && item == i)
250 p->connect = 1;
251 }
252 }
253 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200254 /* does not effect routing - always connected */
255 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600256 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200257 case snd_soc_dapm_output:
258 case snd_soc_dapm_adc:
259 case snd_soc_dapm_input:
260 case snd_soc_dapm_dac:
261 case snd_soc_dapm_micbias:
262 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100263 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100264 case snd_soc_dapm_aif_in:
265 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200266 p->connect = 1;
267 break;
268 /* does effect routing - dynamically connected */
269 case snd_soc_dapm_hp:
270 case snd_soc_dapm_mic:
271 case snd_soc_dapm_spk:
272 case snd_soc_dapm_line:
273 case snd_soc_dapm_pre:
274 case snd_soc_dapm_post:
275 p->connect = 0;
276 break;
277 }
278}
279
Mark Brown74b8f952009-06-06 11:26:15 +0100280/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200281static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200282 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
283 struct snd_soc_dapm_path *path, const char *control_name,
284 const struct snd_kcontrol_new *kcontrol)
285{
286 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
287 int i;
288
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100289 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200290 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200291 list_add(&path->list, &dapm->card->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 = (char*)e->texts[i];
295 dapm_set_path_status(dest, path, 0);
296 return 0;
297 }
298 }
299
300 return -ENODEV;
301}
302
Mark Brown74b8f952009-06-06 11:26:15 +0100303/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200304static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200305 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
306 struct snd_soc_dapm_path *path, const char *control_name)
307{
308 int i;
309
310 /* search for mixer kcontrol */
311 for (i = 0; i < dest->num_kcontrols; i++) {
312 if (!strcmp(control_name, dest->kcontrols[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200313 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200314 list_add(&path->list_sink, &dest->sources);
315 list_add(&path->list_source, &src->sinks);
316 path->name = dest->kcontrols[i].name;
317 dapm_set_path_status(dest, path, i);
318 return 0;
319 }
320 }
321 return -ENODEV;
322}
323
324/* update dapm codec register bits */
325static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
326{
327 int change, power;
Daniel Ribeiro46f58222009-06-07 02:49:11 -0300328 unsigned int old, new;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200329 struct snd_soc_codec *codec = widget->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200330 struct snd_soc_dapm_context *dapm = widget->dapm;
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200331 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200332
333 /* check for valid widgets */
334 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
335 widget->id == snd_soc_dapm_output ||
336 widget->id == snd_soc_dapm_hp ||
337 widget->id == snd_soc_dapm_mic ||
338 widget->id == snd_soc_dapm_line ||
339 widget->id == snd_soc_dapm_spk)
340 return 0;
341
342 power = widget->power;
343 if (widget->invert)
344 power = (power ? 0:1);
345
346 old = snd_soc_read(codec, widget->reg);
347 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
348
349 change = old != new;
350 if (change) {
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200351 pop_dbg(dapm->dev, card->pop_time,
352 "pop test %s : %s in %d ms\n",
Troy Kisky12ef1932008-10-13 17:42:14 -0700353 widget->name, widget->power ? "on" : "off",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200354 card->pop_time);
355 pop_wait(card->pop_time);
Mark Brown69224712010-03-03 14:57:09 +0000356 snd_soc_write(codec, widget->reg, new);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200357 }
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200358 dev_dbg(dapm->dev, "reg %x old %x new %x change %d\n", widget->reg,
359 old, new, change);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200360 return change;
361}
362
Richard Purdie2b97eab2006-10-06 18:32:18 +0200363/* create new dapm mixer control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200364static int dapm_new_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200365 struct snd_soc_dapm_widget *w)
366{
367 int i, ret = 0;
Mark Brown219b93f2008-10-28 13:02:31 +0000368 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200369 struct snd_soc_dapm_path *path;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200370 struct snd_card *card = dapm->codec->card->snd_card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200371
372 /* add kcontrol */
373 for (i = 0; i < w->num_kcontrols; i++) {
374
375 /* match name */
376 list_for_each_entry(path, &w->sources, list_sink) {
377
378 /* mixer/mux paths name must match control name */
379 if (path->name != (char*)w->kcontrols[i].name)
380 continue;
381
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000382 /* add dapm control with long name.
383 * for dapm_mixer this is the concatenation of the
384 * mixer and kcontrol name.
385 * for dapm_mixer_named_ctl this is simply the
386 * kcontrol name.
387 */
388 name_len = strlen(w->kcontrols[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000389 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000390 name_len += 1 + strlen(w->name);
391
Mark Brown219b93f2008-10-28 13:02:31 +0000392 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000393
Richard Purdie2b97eab2006-10-06 18:32:18 +0200394 if (path->long_name == NULL)
395 return -ENOMEM;
396
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000397 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000398 default:
399 snprintf(path->long_name, name_len, "%s %s",
400 w->name, w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000401 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000402 case snd_soc_dapm_mixer_named_ctl:
403 snprintf(path->long_name, name_len, "%s",
404 w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000405 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000406 }
407
Mark Brown219b93f2008-10-28 13:02:31 +0000408 path->long_name[name_len - 1] = '\0';
409
Richard Purdie2b97eab2006-10-06 18:32:18 +0200410 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
411 path->long_name);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200412 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200413 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200414 dev_err(dapm->dev,
415 "asoc: failed to add dapm kcontrol %s: %d\n",
416 path->long_name, ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200417 kfree(path->long_name);
418 path->long_name = NULL;
419 return ret;
420 }
421 }
422 }
423 return ret;
424}
425
426/* create new dapm mux control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200427static int dapm_new_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200428 struct snd_soc_dapm_widget *w)
429{
430 struct snd_soc_dapm_path *path = NULL;
431 struct snd_kcontrol *kcontrol;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200432 struct snd_card *card = dapm->codec->card->snd_card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200433 int ret = 0;
434
435 if (!w->num_kcontrols) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200436 dev_err(dapm->dev, "asoc: mux %s has no controls\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200437 return -EINVAL;
438 }
439
440 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200441 ret = snd_ctl_add(card, kcontrol);
442
Richard Purdie2b97eab2006-10-06 18:32:18 +0200443 if (ret < 0)
444 goto err;
445
446 list_for_each_entry(path, &w->sources, list_sink)
447 path->kcontrol = kcontrol;
448
449 return ret;
450
451err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200452 dev_err(dapm->dev, "asoc: failed to add kcontrol %s\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200453 return ret;
454}
455
456/* create new dapm volume control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200457static int dapm_new_pga(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200458 struct snd_soc_dapm_widget *w)
459{
Mark Browna6c65732010-03-03 17:45:21 +0000460 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200461 dev_err(w->dapm->dev,
462 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200463
Mark Browna6c65732010-03-03 17:45:21 +0000464 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200465}
466
467/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200468static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200469{
470 struct snd_soc_dapm_path *p;
471
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200472 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200473 p->walked = 0;
474}
475
Mark Brown9949788b2010-05-07 20:24:05 +0100476/* We implement power down on suspend by checking the power state of
477 * the ALSA card - when we are suspending the ALSA state for the card
478 * is set to D3.
479 */
480static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
481{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200482 int level = snd_power_get_state(widget->dapm->codec->card->snd_card);
Mark Brown9949788b2010-05-07 20:24:05 +0100483
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000484 switch (level) {
Mark Brown9949788b2010-05-07 20:24:05 +0100485 case SNDRV_CTL_POWER_D3hot:
486 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100487 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200488 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
489 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100490 return widget->ignore_suspend;
Mark Brown9949788b2010-05-07 20:24:05 +0100491 default:
492 return 1;
493 }
494}
495
Richard Purdie2b97eab2006-10-06 18:32:18 +0200496/*
497 * Recursively check for a completed path to an active or physically connected
498 * output widget. Returns number of complete paths.
499 */
500static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
501{
502 struct snd_soc_dapm_path *path;
503 int con = 0;
504
Mark Brown246d0a12009-04-22 18:24:55 +0100505 if (widget->id == snd_soc_dapm_supply)
506 return 0;
507
Mark Brown010ff262009-08-17 17:39:22 +0100508 switch (widget->id) {
509 case snd_soc_dapm_adc:
510 case snd_soc_dapm_aif_out:
511 if (widget->active)
Mark Brown9949788b2010-05-07 20:24:05 +0100512 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100513 default:
514 break;
515 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200516
517 if (widget->connected) {
518 /* connected pin ? */
519 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown9949788b2010-05-07 20:24:05 +0100520 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200521
522 /* connected jack or spk ? */
523 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300524 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown9949788b2010-05-07 20:24:05 +0100525 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200526 }
527
528 list_for_each_entry(path, &widget->sinks, list_source) {
529 if (path->walked)
530 continue;
531
532 if (path->sink && path->connect) {
533 path->walked = 1;
534 con += is_connected_output_ep(path->sink);
535 }
536 }
537
538 return con;
539}
540
541/*
542 * Recursively check for a completed path to an active or physically connected
543 * input widget. Returns number of complete paths.
544 */
545static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
546{
547 struct snd_soc_dapm_path *path;
548 int con = 0;
549
Mark Brown246d0a12009-04-22 18:24:55 +0100550 if (widget->id == snd_soc_dapm_supply)
551 return 0;
552
Richard Purdie2b97eab2006-10-06 18:32:18 +0200553 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100554 switch (widget->id) {
555 case snd_soc_dapm_dac:
556 case snd_soc_dapm_aif_in:
557 if (widget->active)
Mark Brown9949788b2010-05-07 20:24:05 +0100558 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100559 default:
560 break;
561 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200562
563 if (widget->connected) {
564 /* connected pin ? */
565 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown9949788b2010-05-07 20:24:05 +0100566 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200567
568 /* connected VMID/Bias for lower pops */
569 if (widget->id == snd_soc_dapm_vmid)
Mark Brown9949788b2010-05-07 20:24:05 +0100570 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200571
572 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300573 if (widget->id == snd_soc_dapm_mic ||
574 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown9949788b2010-05-07 20:24:05 +0100575 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200576 }
577
578 list_for_each_entry(path, &widget->sources, list_sink) {
579 if (path->walked)
580 continue;
581
582 if (path->source && path->connect) {
583 path->walked = 1;
584 con += is_connected_input_ep(path->source);
585 }
586 }
587
588 return con;
589}
590
591/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300592 * Handler for generic register modifier widget.
593 */
594int dapm_reg_event(struct snd_soc_dapm_widget *w,
595 struct snd_kcontrol *kcontrol, int event)
596{
597 unsigned int val;
598
599 if (SND_SOC_DAPM_EVENT_ON(event))
600 val = w->on_val;
601 else
602 val = w->off_val;
603
604 snd_soc_update_bits(w->codec, -(w->reg + 1),
605 w->mask << w->shift, val << w->shift);
606
607 return 0;
608}
Mark Brown11589412008-07-29 11:42:23 +0100609EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300610
Mark Brown025756ec2009-04-13 11:09:18 +0100611/* Standard power change method, used to apply power changes to most
612 * widgets.
613 */
614static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
615{
616 int ret;
617
618 /* call any power change event handlers */
619 if (w->event)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200620 dev_dbg(w->dapm->dev, "power %s event for %s flags %x\n",
Mark Brown025756ec2009-04-13 11:09:18 +0100621 w->power ? "on" : "off",
622 w->name, w->event_flags);
623
624 /* power up pre event */
625 if (w->power && w->event &&
626 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
627 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
628 if (ret < 0)
629 return ret;
630 }
631
632 /* power down pre event */
633 if (!w->power && w->event &&
634 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
635 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
636 if (ret < 0)
637 return ret;
638 }
639
Mark Brown025756ec2009-04-13 11:09:18 +0100640 dapm_update_bits(w);
641
Mark Brown025756ec2009-04-13 11:09:18 +0100642 /* power up post event */
643 if (w->power && w->event &&
644 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
645 ret = w->event(w,
646 NULL, SND_SOC_DAPM_POST_PMU);
647 if (ret < 0)
648 return ret;
649 }
650
651 /* power down post event */
652 if (!w->power && w->event &&
653 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
654 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
655 if (ret < 0)
656 return ret;
657 }
658
659 return 0;
660}
661
Mark Browncd0f2d42009-04-20 16:56:59 +0100662/* Generic check to see if a widget should be powered.
663 */
664static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
665{
666 int in, out;
667
668 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200669 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100670 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200671 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100672 return out != 0 && in != 0;
673}
674
Mark Brown6ea31b92009-04-20 17:15:41 +0100675/* Check to see if an ADC has power */
676static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
677{
678 int in;
679
680 if (w->active) {
681 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200682 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100683 return in != 0;
684 } else {
685 return dapm_generic_check_power(w);
686 }
687}
688
689/* Check to see if a DAC has power */
690static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
691{
692 int out;
693
694 if (w->active) {
695 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200696 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100697 return out != 0;
698 } else {
699 return dapm_generic_check_power(w);
700 }
701}
702
Mark Brown246d0a12009-04-22 18:24:55 +0100703/* Check to see if a power supply is needed */
704static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
705{
706 struct snd_soc_dapm_path *path;
707 int power = 0;
708
709 /* Check if one of our outputs is connected */
710 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +0100711 if (path->connected &&
712 !path->connected(path->source, path->sink))
713 continue;
714
Mark Brown246d0a12009-04-22 18:24:55 +0100715 if (path->sink && path->sink->power_check &&
716 path->sink->power_check(path->sink)) {
717 power = 1;
718 break;
719 }
720 }
721
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200722 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100723
724 return power;
725}
726
Mark Brown38357ab2009-06-06 19:03:23 +0100727static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
728 struct snd_soc_dapm_widget *b,
729 int sort[])
Mark Brown42aa3412009-03-01 19:21:10 +0000730{
Mark Brown38357ab2009-06-06 19:03:23 +0100731 if (sort[a->id] != sort[b->id])
732 return sort[a->id] - sort[b->id];
Mark Brownb22ead22009-06-07 12:51:26 +0100733 if (a->reg != b->reg)
734 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +0000735 if (a->dapm != b->dapm)
736 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +0000737
Mark Brown38357ab2009-06-06 19:03:23 +0100738 return 0;
739}
Mark Brown42aa3412009-03-01 19:21:10 +0000740
Mark Brown38357ab2009-06-06 19:03:23 +0100741/* Insert a widget in order into a DAPM power sequence. */
742static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
743 struct list_head *list,
744 int sort[])
745{
746 struct snd_soc_dapm_widget *w;
747
748 list_for_each_entry(w, list, power_list)
749 if (dapm_seq_compare(new_widget, w, sort) < 0) {
750 list_add_tail(&new_widget->power_list, &w->power_list);
751 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000752 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100753
Mark Brown38357ab2009-06-06 19:03:23 +0100754 list_add_tail(&new_widget->power_list, list);
755}
Mark Brown42aa3412009-03-01 19:21:10 +0000756
Mark Brown68f89ad2010-11-03 23:51:49 -0400757static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
758 struct snd_soc_dapm_widget *w, int event)
759{
760 struct snd_soc_card *card = dapm->card;
761 const char *ev_name;
762 int power, ret;
763
764 switch (event) {
765 case SND_SOC_DAPM_PRE_PMU:
766 ev_name = "PRE_PMU";
767 power = 1;
768 break;
769 case SND_SOC_DAPM_POST_PMU:
770 ev_name = "POST_PMU";
771 power = 1;
772 break;
773 case SND_SOC_DAPM_PRE_PMD:
774 ev_name = "PRE_PMD";
775 power = 0;
776 break;
777 case SND_SOC_DAPM_POST_PMD:
778 ev_name = "POST_PMD";
779 power = 0;
780 break;
781 default:
782 BUG();
783 return;
784 }
785
786 if (w->power != power)
787 return;
788
789 if (w->event && (w->event_flags & event)) {
790 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
791 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -0400792 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400793 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -0400794 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400795 if (ret < 0)
796 pr_err("%s: %s event failed: %d\n",
797 ev_name, w->name, ret);
798 }
799}
800
Mark Brownb22ead22009-06-07 12:51:26 +0100801/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200802static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +0100803 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100804{
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200805 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -0400806 struct snd_soc_dapm_widget *w;
807 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +0100808 unsigned int value = 0;
809 unsigned int mask = 0;
810 unsigned int cur_mask;
811
812 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
813 power_list)->reg;
814
815 list_for_each_entry(w, pending, power_list) {
816 cur_mask = 1 << w->shift;
817 BUG_ON(reg != w->reg);
818
819 if (w->invert)
820 power = !w->power;
821 else
822 power = w->power;
823
824 mask |= cur_mask;
825 if (power)
826 value |= cur_mask;
827
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200828 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +0100829 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
830 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100831
Mark Brown68f89ad2010-11-03 23:51:49 -0400832 /* Check for events */
833 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
834 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100835 }
836
Mark Brown81628102009-06-07 13:21:24 +0100837 if (reg >= 0) {
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200838 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +0100839 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200840 value, mask, reg, card->pop_time);
841 pop_wait(card->pop_time);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200842 snd_soc_update_bits(dapm->codec, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +0100843 }
844
845 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -0400846 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
847 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +0000848 }
Mark Brown42aa3412009-03-01 19:21:10 +0000849}
850
Mark Brownb22ead22009-06-07 12:51:26 +0100851/* Apply a DAPM power sequence.
852 *
853 * We walk over a pre-sorted list of widgets to apply power to. In
854 * order to minimise the number of writes to the device required
855 * multiple widgets will be updated in a single write where possible.
856 * Currently anything that requires more than a single write is not
857 * handled.
858 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200859static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
860 struct list_head *list, int event, int sort[])
Mark Brownb22ead22009-06-07 12:51:26 +0100861{
862 struct snd_soc_dapm_widget *w, *n;
863 LIST_HEAD(pending);
864 int cur_sort = -1;
865 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200866 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown163cac02009-06-07 10:12:52 +0100867 int ret;
868
Mark Brownb22ead22009-06-07 12:51:26 +0100869 list_for_each_entry_safe(w, n, list, power_list) {
870 ret = 0;
871
872 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200873 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
874 w->dapm != cur_dapm) {
Mark Brownb22ead22009-06-07 12:51:26 +0100875 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200876 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +0100877
878 INIT_LIST_HEAD(&pending);
879 cur_sort = -1;
880 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200881 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +0100882 }
883
Mark Brown163cac02009-06-07 10:12:52 +0100884 switch (w->id) {
885 case snd_soc_dapm_pre:
886 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100887 list_for_each_entry_safe_continue(w, n, list,
888 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100889
Mark Brownb22ead22009-06-07 12:51:26 +0100890 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100891 ret = w->event(w,
892 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100893 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100894 ret = w->event(w,
895 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +0100896 break;
897
898 case snd_soc_dapm_post:
899 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100900 list_for_each_entry_safe_continue(w, n, list,
901 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100902
Mark Brownb22ead22009-06-07 12:51:26 +0100903 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100904 ret = w->event(w,
905 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100906 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100907 ret = w->event(w,
908 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100909 break;
910
911 case snd_soc_dapm_input:
912 case snd_soc_dapm_output:
913 case snd_soc_dapm_hp:
914 case snd_soc_dapm_mic:
915 case snd_soc_dapm_line:
916 case snd_soc_dapm_spk:
917 /* No register support currently */
Mark Brownb22ead22009-06-07 12:51:26 +0100918 ret = dapm_generic_apply_power(w);
Mark Brown163cac02009-06-07 10:12:52 +0100919 break;
920
921 default:
Mark Brown81628102009-06-07 13:21:24 +0100922 /* Queue it up for application */
923 cur_sort = sort[w->id];
924 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200925 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +0100926 list_move(&w->power_list, &pending);
927 break;
Mark Brown163cac02009-06-07 10:12:52 +0100928 }
Mark Brownb22ead22009-06-07 12:51:26 +0100929
930 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200931 dev_err(w->dapm->dev,
932 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +0100933 }
Mark Brownb22ead22009-06-07 12:51:26 +0100934
935 if (!list_empty(&pending))
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200936 dapm_seq_run_coalesced(dapm, &pending);
Mark Brown163cac02009-06-07 10:12:52 +0100937}
938
Mark Brown97404f22010-12-14 16:13:57 +0000939static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
940{
941 struct snd_soc_dapm_update *update = dapm->update;
942 struct snd_soc_dapm_widget *w;
943 int ret;
944
945 if (!update)
946 return;
947
948 w = update->widget;
949
950 if (w->event &&
951 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
952 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
953 if (ret != 0)
954 pr_err("%s DAPM pre-event failed: %d\n",
955 w->name, ret);
956 }
957
958 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
959 update->val);
960 if (ret < 0)
961 pr_err("%s DAPM update failed: %d\n", w->name, ret);
962
963 if (w->event &&
964 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
965 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
966 if (ret != 0)
967 pr_err("%s DAPM post-event failed: %d\n",
968 w->name, ret);
969 }
970}
971
972
973
Mark Brown42aa3412009-03-01 19:21:10 +0000974/*
Richard Purdie2b97eab2006-10-06 18:32:18 +0200975 * Scan each dapm widget for complete audio path.
976 * A complete path is a route that has valid endpoints i.e.:-
977 *
978 * o DAC to output pin.
979 * o Input Pin to ADC.
980 * o Input pin to Output pin (bypass, sidetone)
981 * o DAC to ADC (loopback).
982 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200983static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200984{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200985 struct snd_soc_card *card = dapm->codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200986 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200987 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +0100988 LIST_HEAD(up_list);
989 LIST_HEAD(down_list);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100990 int ret = 0;
Mark Brown38357ab2009-06-06 19:03:23 +0100991 int power;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200992
Mark Brown84e90932010-11-04 00:07:02 -0400993 trace_snd_soc_dapm_start(card);
994
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200995 list_for_each_entry(d, &card->dapm_list, list)
996 if (d->n_widgets)
997 d->dev_power = 0;
998
Mark Brown6d3ddc82009-05-16 17:47:29 +0100999 /* Check which widgets we need to power and store them in
1000 * lists indicating if they should be powered up or down.
1001 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001002 list_for_each_entry(w, &card->widgets, list) {
Mark Brown6d3ddc82009-05-16 17:47:29 +01001003 switch (w->id) {
1004 case snd_soc_dapm_pre:
Mark Brown291f3bb2009-06-07 13:57:17 +01001005 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001006 break;
1007 case snd_soc_dapm_post:
Mark Brown291f3bb2009-06-07 13:57:17 +01001008 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001009 break;
1010
1011 default:
1012 if (!w->power_check)
1013 continue;
1014
Mark Brown9949788b2010-05-07 20:24:05 +01001015 if (!w->force)
Mark Brown50b6bce2009-11-23 13:11:53 +00001016 power = w->power_check(w);
Mark Brown9949788b2010-05-07 20:24:05 +01001017 else
1018 power = 1;
1019 if (power)
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001020 w->dapm->dev_power = 1;
Mark Brown452c5ea2009-05-17 21:41:23 +01001021
Mark Brown6d3ddc82009-05-16 17:47:29 +01001022 if (w->power == power)
1023 continue;
1024
Mark Brown84e90932010-11-04 00:07:02 -04001025 trace_snd_soc_dapm_widget_power(w, power);
1026
Mark Brown6d3ddc82009-05-16 17:47:29 +01001027 if (power)
Mark Brown291f3bb2009-06-07 13:57:17 +01001028 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001029 else
Mark Brown291f3bb2009-06-07 13:57:17 +01001030 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001031
1032 w->power = power;
1033 break;
1034 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001035 }
1036
Mark Brownb14b76a52009-08-17 11:55:38 +01001037 /* If there are no DAPM widgets then try to figure out power from the
1038 * event type.
1039 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001040 if (!dapm->n_widgets) {
Mark Brownb14b76a52009-08-17 11:55:38 +01001041 switch (event) {
1042 case SND_SOC_DAPM_STREAM_START:
1043 case SND_SOC_DAPM_STREAM_RESUME:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001044 dapm->dev_power = 1;
Mark Brownb14b76a52009-08-17 11:55:38 +01001045 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001046 case SND_SOC_DAPM_STREAM_STOP:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001047 dapm->dev_power = !!dapm->codec->active;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001048 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001049 case SND_SOC_DAPM_STREAM_SUSPEND:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001050 dapm->dev_power = 0;
Mark Brown50b6bce2009-11-23 13:11:53 +00001051 break;
Mark Brownb14b76a52009-08-17 11:55:38 +01001052 case SND_SOC_DAPM_STREAM_NOP:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001053 switch (dapm->bias_level) {
Mark Browna96ca332010-01-19 22:49:43 +00001054 case SND_SOC_BIAS_STANDBY:
1055 case SND_SOC_BIAS_OFF:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001056 dapm->dev_power = 0;
Mark Browna96ca332010-01-19 22:49:43 +00001057 break;
1058 default:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001059 dapm->dev_power = 1;
Mark Browna96ca332010-01-19 22:49:43 +00001060 break;
1061 }
Mark Brown50b6bce2009-11-23 13:11:53 +00001062 break;
Mark Brownb14b76a52009-08-17 11:55:38 +01001063 default:
1064 break;
1065 }
1066 }
1067
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001068 list_for_each_entry(d, &dapm->card->dapm_list, list) {
1069 if (d->dev_power && d->bias_level == SND_SOC_BIAS_OFF) {
1070 ret = snd_soc_dapm_set_bias_level(card, d,
1071 SND_SOC_BIAS_STANDBY);
1072 if (ret != 0)
1073 dev_err(d->dev,
1074 "Failed to turn on bias: %d\n", ret);
1075 }
Mark Browna96ca332010-01-19 22:49:43 +00001076
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001077 /* If we're changing to all on or all off then prepare */
1078 if ((d->dev_power && d->bias_level == SND_SOC_BIAS_STANDBY) ||
1079 (!d->dev_power && d->bias_level == SND_SOC_BIAS_ON)) {
1080 ret = snd_soc_dapm_set_bias_level(card, d,
1081 SND_SOC_BIAS_PREPARE);
1082 if (ret != 0)
1083 dev_err(d->dev,
1084 "Failed to prepare bias: %d\n", ret);
1085 }
Mark Brown452c5ea2009-05-17 21:41:23 +01001086 }
1087
Mark Brown6d3ddc82009-05-16 17:47:29 +01001088 /* Power down widgets first; try to avoid amplifying pops. */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001089 dapm_seq_run(dapm, &down_list, event, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001090
Mark Brown97404f22010-12-14 16:13:57 +00001091 dapm_widget_update(dapm);
1092
Mark Brown6d3ddc82009-05-16 17:47:29 +01001093 /* Now power up. */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001094 dapm_seq_run(dapm, &up_list, event, dapm_up_seq);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001095
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001096 list_for_each_entry(d, &dapm->card->dapm_list, list) {
1097 /* If we just powered the last thing off drop to standby bias */
1098 if (d->bias_level == SND_SOC_BIAS_PREPARE && !d->dev_power) {
1099 ret = snd_soc_dapm_set_bias_level(card, d,
1100 SND_SOC_BIAS_STANDBY);
1101 if (ret != 0)
1102 dev_err(d->dev,
1103 "Failed to apply standby bias: %d\n",
1104 ret);
1105 }
Mark Brown452c5ea2009-05-17 21:41:23 +01001106
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001107 /* If we're in standby and can support bias off then do that */
1108 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1109 d->idle_bias_off) {
1110 ret = snd_soc_dapm_set_bias_level(card, d,
1111 SND_SOC_BIAS_OFF);
1112 if (ret != 0)
1113 dev_err(d->dev,
1114 "Failed to turn off bias: %d\n", ret);
1115 }
Mark Browna96ca332010-01-19 22:49:43 +00001116
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001117 /* If we just powered up then move to active bias */
1118 if (d->bias_level == SND_SOC_BIAS_PREPARE && d->dev_power) {
1119 ret = snd_soc_dapm_set_bias_level(card, d,
1120 SND_SOC_BIAS_ON);
1121 if (ret != 0)
1122 dev_err(d->dev,
1123 "Failed to apply active bias: %d\n",
1124 ret);
1125 }
Mark Brown452c5ea2009-05-17 21:41:23 +01001126 }
1127
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001128 pop_dbg(dapm->dev, card->pop_time,
1129 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001130 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001131
Mark Brown84e90932010-11-04 00:07:02 -04001132 trace_snd_soc_dapm_done(card);
1133
Mark Brown42aa3412009-03-01 19:21:10 +00001134 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001135}
1136
Mark Brown79fb9382009-08-21 16:38:13 +01001137#ifdef CONFIG_DEBUG_FS
1138static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1139{
1140 file->private_data = inode->i_private;
1141 return 0;
1142}
1143
1144static ssize_t dapm_widget_power_read_file(struct file *file,
1145 char __user *user_buf,
1146 size_t count, loff_t *ppos)
1147{
1148 struct snd_soc_dapm_widget *w = file->private_data;
1149 char *buf;
1150 int in, out;
1151 ssize_t ret;
1152 struct snd_soc_dapm_path *p = NULL;
1153
1154 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1155 if (!buf)
1156 return -ENOMEM;
1157
1158 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001159 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001160 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001161 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001162
Mark Brownd033c362009-12-04 15:25:56 +00001163 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001164 w->name, w->power ? "On" : "Off", in, out);
1165
Mark Brownd033c362009-12-04 15:25:56 +00001166 if (w->reg >= 0)
1167 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1168 " - R%d(0x%x) bit %d",
1169 w->reg, w->reg, w->shift);
1170
1171 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1172
Mark Brown3eef08b2009-09-14 16:49:00 +01001173 if (w->sname)
1174 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1175 w->sname,
1176 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001177
1178 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001179 if (p->connected && !p->connected(w, p->sink))
1180 continue;
1181
Mark Brown79fb9382009-08-21 16:38:13 +01001182 if (p->connect)
1183 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1184 " in %s %s\n",
1185 p->name ? p->name : "static",
1186 p->source->name);
1187 }
1188 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001189 if (p->connected && !p->connected(w, p->sink))
1190 continue;
1191
Mark Brown79fb9382009-08-21 16:38:13 +01001192 if (p->connect)
1193 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1194 " out %s %s\n",
1195 p->name ? p->name : "static",
1196 p->sink->name);
1197 }
1198
1199 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1200
1201 kfree(buf);
1202 return ret;
1203}
1204
1205static const struct file_operations dapm_widget_power_fops = {
1206 .open = dapm_widget_power_open_file,
1207 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001208 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001209};
1210
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001211void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001212{
1213 struct snd_soc_dapm_widget *w;
1214 struct dentry *d;
1215
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001216 if (!dapm->debugfs_dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001217 return;
1218
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001219 list_for_each_entry(w, &dapm->card->widgets, list) {
1220 if (!w->name || w->dapm != dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001221 continue;
1222
1223 d = debugfs_create_file(w->name, 0444,
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001224 dapm->debugfs_dapm, w,
Mark Brown79fb9382009-08-21 16:38:13 +01001225 &dapm_widget_power_fops);
1226 if (!d)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001227 dev_warn(w->dapm->dev,
1228 "ASoC: Failed to create %s debugfs file\n",
1229 w->name);
Mark Brown79fb9382009-08-21 16:38:13 +01001230 }
1231}
1232#else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001233void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001234{
1235}
1236#endif
1237
Richard Purdie2b97eab2006-10-06 18:32:18 +02001238/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001239static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001240 struct snd_kcontrol *kcontrol, int change,
1241 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001242{
1243 struct snd_soc_dapm_path *path;
1244 int found = 0;
1245
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001246 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001247 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001248 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001249 return -ENODEV;
1250
Mark Brown3a655772009-10-05 17:23:30 +01001251 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001252 return 0;
1253
1254 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001255 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001256 if (path->kcontrol != kcontrol)
1257 continue;
1258
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001259 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001260 continue;
1261
1262 found = 1;
1263 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001264 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001265 path->connect = 1; /* new connection */
1266 else
1267 path->connect = 0; /* old connection must be powered down */
1268 }
1269
Mark Brownb91b8fa2010-01-20 18:18:35 +00001270 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001271 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001272
1273 return 0;
1274}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001275
Milan plzik1b075e32008-01-10 14:39:46 +01001276/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001277static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001278 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001279{
1280 struct snd_soc_dapm_path *path;
1281 int found = 0;
1282
Milan plzik1b075e32008-01-10 14:39:46 +01001283 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001284 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001285 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001286 return -ENODEV;
1287
Richard Purdie2b97eab2006-10-06 18:32:18 +02001288 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001289 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001290 if (path->kcontrol != kcontrol)
1291 continue;
1292
1293 /* found, now check type */
1294 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001295 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001296 break;
1297 }
1298
Mark Brownb91b8fa2010-01-20 18:18:35 +00001299 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001300 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001301
1302 return 0;
1303}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001304
1305/* show dapm widget status in sys fs */
1306static ssize_t dapm_widget_show(struct device *dev,
1307 struct device_attribute *attr, char *buf)
1308{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001309 struct snd_soc_pcm_runtime *rtd =
1310 container_of(dev, struct snd_soc_pcm_runtime, dev);
1311 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001312 struct snd_soc_dapm_widget *w;
1313 int count = 0;
1314 char *state = "not set";
1315
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001316 list_for_each_entry(w, &codec->card->widgets, list) {
1317 if (w->dapm != &codec->dapm)
1318 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001319
1320 /* only display widgets that burnm power */
1321 switch (w->id) {
1322 case snd_soc_dapm_hp:
1323 case snd_soc_dapm_mic:
1324 case snd_soc_dapm_spk:
1325 case snd_soc_dapm_line:
1326 case snd_soc_dapm_micbias:
1327 case snd_soc_dapm_dac:
1328 case snd_soc_dapm_adc:
1329 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001330 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001331 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001332 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001333 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001334 if (w->name)
1335 count += sprintf(buf + count, "%s: %s\n",
1336 w->name, w->power ? "On":"Off");
1337 break;
1338 default:
1339 break;
1340 }
1341 }
1342
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001343 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001344 case SND_SOC_BIAS_ON:
1345 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001346 break;
Mark Brown0be98982008-05-19 12:31:28 +02001347 case SND_SOC_BIAS_PREPARE:
1348 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001349 break;
Mark Brown0be98982008-05-19 12:31:28 +02001350 case SND_SOC_BIAS_STANDBY:
1351 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001352 break;
Mark Brown0be98982008-05-19 12:31:28 +02001353 case SND_SOC_BIAS_OFF:
1354 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001355 break;
1356 }
1357 count += sprintf(buf + count, "PM State: %s\n", state);
1358
1359 return count;
1360}
1361
1362static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1363
1364int snd_soc_dapm_sys_add(struct device *dev)
1365{
Troy Kisky12ef1932008-10-13 17:42:14 -07001366 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001367}
1368
1369static void snd_soc_dapm_sys_remove(struct device *dev)
1370{
Mark Brownaef90842009-05-16 17:53:16 +01001371 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001372}
1373
1374/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001375static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001376{
1377 struct snd_soc_dapm_widget *w, *next_w;
1378 struct snd_soc_dapm_path *p, *next_p;
1379
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001380 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1381 if (w->dapm != dapm)
1382 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001383 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001384 /*
1385 * remove source and sink paths associated to this widget.
1386 * While removing the path, remove reference to it from both
1387 * source and sink widgets so that path is removed only once.
1388 */
1389 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1390 list_del(&p->list_sink);
1391 list_del(&p->list_source);
1392 list_del(&p->list);
1393 kfree(p->long_name);
1394 kfree(p);
1395 }
1396 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1397 list_del(&p->list_sink);
1398 list_del(&p->list_source);
1399 list_del(&p->list);
1400 kfree(p->long_name);
1401 kfree(p);
1402 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001403 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001404 kfree(w);
1405 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001406}
1407
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001408static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001409 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001410{
1411 struct snd_soc_dapm_widget *w;
1412
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001413 list_for_each_entry(w, &dapm->card->widgets, list) {
1414 if (w->dapm != dapm)
1415 continue;
Liam Girdwooda5302182008-07-07 13:35:17 +01001416 if (!strcmp(w->name, pin)) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001417 dev_dbg(w->dapm->dev, "dapm: pin %s = %d\n",
1418 pin, status);
Liam Girdwooda5302182008-07-07 13:35:17 +01001419 w->connected = status;
Mark Brown5b9e87c2010-03-22 13:36:13 +00001420 /* Allow disabling of forced pins */
1421 if (status == 0)
1422 w->force = 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001423 return 0;
1424 }
1425 }
1426
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001427 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001428 return -EINVAL;
1429}
1430
Richard Purdie2b97eab2006-10-06 18:32:18 +02001431/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001432 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001433 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001434 *
1435 * Walks all dapm audio paths and powers widgets according to their
1436 * stream or path usage.
1437 *
1438 * Returns 0 for success.
1439 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001440int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001441{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001442 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001443}
Liam Girdwooda5302182008-07-07 13:35:17 +01001444EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001445
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001446static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001447 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001448{
1449 struct snd_soc_dapm_path *path;
1450 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001451 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001452 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001453 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001454 const char *source;
1455 char prefixed_sink[80];
1456 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001457 int ret = 0;
1458
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001459 if (dapm->codec->name_prefix) {
1460 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1461 dapm->codec->name_prefix, route->sink);
1462 sink = prefixed_sink;
1463 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1464 dapm->codec->name_prefix, route->source);
1465 source = prefixed_source;
1466 } else {
1467 sink = route->sink;
1468 source = route->source;
1469 }
1470
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001471 /*
1472 * find src and dest widgets over all widgets but favor a widget from
1473 * current DAPM context
1474 */
1475 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001476 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001477 wtsink = w;
1478 if (w->dapm == dapm)
1479 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001480 continue;
1481 }
1482 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001483 wtsource = w;
1484 if (w->dapm == dapm)
1485 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001486 }
1487 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001488 /* use widget from another DAPM context if not found from this */
1489 if (!wsink)
1490 wsink = wtsink;
1491 if (!wsource)
1492 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001493
1494 if (wsource == NULL || wsink == NULL)
1495 return -ENODEV;
1496
1497 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1498 if (!path)
1499 return -ENOMEM;
1500
1501 path->source = wsource;
1502 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001503 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001504 INIT_LIST_HEAD(&path->list);
1505 INIT_LIST_HEAD(&path->list_source);
1506 INIT_LIST_HEAD(&path->list_sink);
1507
1508 /* check for external widgets */
1509 if (wsink->id == snd_soc_dapm_input) {
1510 if (wsource->id == snd_soc_dapm_micbias ||
1511 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001512 wsource->id == snd_soc_dapm_line ||
1513 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001514 wsink->ext = 1;
1515 }
1516 if (wsource->id == snd_soc_dapm_output) {
1517 if (wsink->id == snd_soc_dapm_spk ||
1518 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001519 wsink->id == snd_soc_dapm_line ||
1520 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001521 wsource->ext = 1;
1522 }
1523
1524 /* connect static paths */
1525 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001526 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001527 list_add(&path->list_sink, &wsink->sources);
1528 list_add(&path->list_source, &wsource->sinks);
1529 path->connect = 1;
1530 return 0;
1531 }
1532
1533 /* connect dynamic paths */
1534 switch(wsink->id) {
1535 case snd_soc_dapm_adc:
1536 case snd_soc_dapm_dac:
1537 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001538 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001539 case snd_soc_dapm_input:
1540 case snd_soc_dapm_output:
1541 case snd_soc_dapm_micbias:
1542 case snd_soc_dapm_vmid:
1543 case snd_soc_dapm_pre:
1544 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001545 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001546 case snd_soc_dapm_aif_in:
1547 case snd_soc_dapm_aif_out:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001548 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001549 list_add(&path->list_sink, &wsink->sources);
1550 list_add(&path->list_source, &wsource->sinks);
1551 path->connect = 1;
1552 return 0;
1553 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001554 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001555 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001556 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Richard Purdie2b97eab2006-10-06 18:32:18 +02001557 &wsink->kcontrols[0]);
1558 if (ret != 0)
1559 goto err;
1560 break;
1561 case snd_soc_dapm_switch:
1562 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001563 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001564 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001565 if (ret != 0)
1566 goto err;
1567 break;
1568 case snd_soc_dapm_hp:
1569 case snd_soc_dapm_mic:
1570 case snd_soc_dapm_line:
1571 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001572 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001573 list_add(&path->list_sink, &wsink->sources);
1574 list_add(&path->list_source, &wsource->sinks);
1575 path->connect = 0;
1576 return 0;
1577 }
1578 return 0;
1579
1580err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001581 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1582 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001583 kfree(path);
1584 return ret;
1585}
Mark Brown105f1c22008-05-13 14:52:19 +02001586
1587/**
Mark Brown105f1c22008-05-13 14:52:19 +02001588 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001589 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02001590 * @route: audio routes
1591 * @num: number of routes
1592 *
1593 * Connects 2 dapm widgets together via a named audio path. The sink is
1594 * the widget receiving the audio signal, whilst the source is the sender
1595 * of the audio signal.
1596 *
1597 * Returns 0 for success else error. On error all resources can be freed
1598 * with a call to snd_soc_card_free().
1599 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001600int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02001601 const struct snd_soc_dapm_route *route, int num)
1602{
1603 int i, ret;
1604
1605 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001606 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001607 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001608 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1609 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02001610 return ret;
1611 }
1612 route++;
1613 }
1614
1615 return 0;
1616}
1617EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1618
1619/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001620 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001621 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001622 *
1623 * Checks the codec for any new dapm widgets and creates them if found.
1624 *
1625 * Returns 0 for success.
1626 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001627int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001628{
1629 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00001630 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001631
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001632 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001633 {
1634 if (w->new)
1635 continue;
1636
1637 switch(w->id) {
1638 case snd_soc_dapm_switch:
1639 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001640 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001641 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001642 dapm_new_mixer(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001643 break;
1644 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001645 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001646 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001647 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001648 dapm_new_mux(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001649 break;
1650 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01001651 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01001652 w->power_check = dapm_adc_check_power;
1653 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001654 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01001655 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01001656 w->power_check = dapm_dac_check_power;
1657 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001658 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001659 case snd_soc_dapm_out_drv:
Mark Brownb75576d2009-04-20 17:56:13 +01001660 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001661 dapm_new_pga(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001662 break;
1663 case snd_soc_dapm_input:
1664 case snd_soc_dapm_output:
1665 case snd_soc_dapm_micbias:
1666 case snd_soc_dapm_spk:
1667 case snd_soc_dapm_hp:
1668 case snd_soc_dapm_mic:
1669 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001670 w->power_check = dapm_generic_check_power;
1671 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001672 case snd_soc_dapm_supply:
1673 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001674 case snd_soc_dapm_vmid:
1675 case snd_soc_dapm_pre:
1676 case snd_soc_dapm_post:
1677 break;
1678 }
Mark Brownb66a70d2011-02-09 18:04:11 +00001679
1680 /* Read the initial power state from the device */
1681 if (w->reg >= 0) {
1682 val = snd_soc_read(w->codec, w->reg);
1683 val &= 1 << w->shift;
1684 if (w->invert)
1685 val = !val;
1686
1687 if (val)
1688 w->power = 1;
1689 }
1690
Richard Purdie2b97eab2006-10-06 18:32:18 +02001691 w->new = 1;
1692 }
1693
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001694 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001695 return 0;
1696}
1697EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1698
1699/**
1700 * snd_soc_dapm_get_volsw - dapm mixer get callback
1701 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001702 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001703 *
1704 * Callback to get the value of a dapm mixer control.
1705 *
1706 * Returns 0 for success.
1707 */
1708int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1709 struct snd_ctl_elem_value *ucontrol)
1710{
1711 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001712 struct soc_mixer_control *mc =
1713 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001714 unsigned int reg = mc->reg;
1715 unsigned int shift = mc->shift;
1716 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001717 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001718 unsigned int invert = mc->invert;
1719 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001720
Richard Purdie2b97eab2006-10-06 18:32:18 +02001721 ucontrol->value.integer.value[0] =
1722 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1723 if (shift != rshift)
1724 ucontrol->value.integer.value[1] =
1725 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1726 if (invert) {
1727 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001728 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001729 if (shift != rshift)
1730 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001731 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001732 }
1733
1734 return 0;
1735}
1736EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1737
1738/**
1739 * snd_soc_dapm_put_volsw - dapm mixer set callback
1740 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001741 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001742 *
1743 * Callback to set the value of a dapm mixer control.
1744 *
1745 * Returns 0 for success.
1746 */
1747int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1748 struct snd_ctl_elem_value *ucontrol)
1749{
1750 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001751 struct soc_mixer_control *mc =
1752 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001753 unsigned int reg = mc->reg;
1754 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001755 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001756 unsigned int mask = (1 << fls(max)) - 1;
1757 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07001758 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00001759 int connect, change;
1760 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001761
1762 val = (ucontrol->value.integer.value[0] & mask);
1763
1764 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001765 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07001766 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001767 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001768
1769 mutex_lock(&widget->codec->mutex);
1770 widget->value = val;
1771
Stephen Warrene9cf7042011-01-27 14:54:05 -07001772 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00001773 if (change) {
Mark Brown283375c2009-12-07 18:09:03 +00001774 if (val)
1775 /* new connection */
1776 connect = invert ? 0:1;
1777 else
1778 /* old connection must be powered down */
1779 connect = invert ? 1:0;
1780
Mark Brown97404f22010-12-14 16:13:57 +00001781 update.kcontrol = kcontrol;
1782 update.widget = widget;
1783 update.reg = reg;
1784 update.mask = mask;
1785 update.val = val;
1786 widget->dapm->update = &update;
1787
Mark Brown283375c2009-12-07 18:09:03 +00001788 dapm_mixer_update_power(widget, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00001789
1790 widget->dapm->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00001791 }
1792
Richard Purdie2b97eab2006-10-06 18:32:18 +02001793 mutex_unlock(&widget->codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00001794 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001795}
1796EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1797
1798/**
1799 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1800 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001801 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001802 *
1803 * Callback to get the value of a dapm enumerated double mixer control.
1804 *
1805 * Returns 0 for success.
1806 */
1807int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1808 struct snd_ctl_elem_value *ucontrol)
1809{
1810 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1811 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001812 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001813
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001814 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001815 ;
1816 val = snd_soc_read(widget->codec, e->reg);
1817 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1818 if (e->shift_l != e->shift_r)
1819 ucontrol->value.enumerated.item[1] =
1820 (val >> e->shift_r) & (bitmask - 1);
1821
1822 return 0;
1823}
1824EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1825
1826/**
1827 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1828 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001829 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001830 *
1831 * Callback to set the value of a dapm enumerated double mixer control.
1832 *
1833 * Returns 0 for success.
1834 */
1835int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1836 struct snd_ctl_elem_value *ucontrol)
1837{
1838 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1839 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001840 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001841 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00001842 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001843
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001844 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001845 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001846 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001847 return -EINVAL;
1848 mux = ucontrol->value.enumerated.item[0];
1849 val = mux << e->shift_l;
1850 mask = (bitmask - 1) << e->shift_l;
1851 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001852 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001853 return -EINVAL;
1854 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1855 mask |= (bitmask - 1) << e->shift_r;
1856 }
1857
1858 mutex_lock(&widget->codec->mutex);
1859 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01001860 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00001861
1862 update.kcontrol = kcontrol;
1863 update.widget = widget;
1864 update.reg = e->reg;
1865 update.mask = mask;
1866 update.val = val;
1867 widget->dapm->update = &update;
1868
Mark Brown3a655772009-10-05 17:23:30 +01001869 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01001870
Mark Brown97404f22010-12-14 16:13:57 +00001871 widget->dapm->update = NULL;
Mark Brown1642e3d2009-10-05 16:24:26 +01001872
Richard Purdie2b97eab2006-10-06 18:32:18 +02001873 mutex_unlock(&widget->codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00001874 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001875}
1876EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1877
1878/**
Mark Brownd2b247a2009-10-06 15:21:04 +01001879 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1880 * @kcontrol: mixer control
1881 * @ucontrol: control element information
1882 *
1883 * Returns 0 for success.
1884 */
1885int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1886 struct snd_ctl_elem_value *ucontrol)
1887{
1888 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1889
1890 ucontrol->value.enumerated.item[0] = widget->value;
1891
1892 return 0;
1893}
1894EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1895
1896/**
1897 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1898 * @kcontrol: mixer control
1899 * @ucontrol: control element information
1900 *
1901 * Returns 0 for success.
1902 */
1903int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
1904 struct snd_ctl_elem_value *ucontrol)
1905{
1906 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1907 struct soc_enum *e =
1908 (struct soc_enum *)kcontrol->private_value;
1909 int change;
1910 int ret = 0;
1911
1912 if (ucontrol->value.enumerated.item[0] >= e->max)
1913 return -EINVAL;
1914
1915 mutex_lock(&widget->codec->mutex);
1916
1917 change = widget->value != ucontrol->value.enumerated.item[0];
1918 widget->value = ucontrol->value.enumerated.item[0];
1919 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
1920
1921 mutex_unlock(&widget->codec->mutex);
1922 return ret;
1923}
1924EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
1925
1926/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001927 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1928 * callback
1929 * @kcontrol: mixer control
1930 * @ucontrol: control element information
1931 *
1932 * Callback to get the value of a dapm semi enumerated double mixer control.
1933 *
1934 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1935 * used for handling bitfield coded enumeration for example.
1936 *
1937 * Returns 0 for success.
1938 */
1939int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1940 struct snd_ctl_elem_value *ucontrol)
1941{
1942 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001943 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001944 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001945
1946 reg_val = snd_soc_read(widget->codec, e->reg);
1947 val = (reg_val >> e->shift_l) & e->mask;
1948 for (mux = 0; mux < e->max; mux++) {
1949 if (val == e->values[mux])
1950 break;
1951 }
1952 ucontrol->value.enumerated.item[0] = mux;
1953 if (e->shift_l != e->shift_r) {
1954 val = (reg_val >> e->shift_r) & e->mask;
1955 for (mux = 0; mux < e->max; mux++) {
1956 if (val == e->values[mux])
1957 break;
1958 }
1959 ucontrol->value.enumerated.item[1] = mux;
1960 }
1961
1962 return 0;
1963}
1964EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1965
1966/**
1967 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1968 * callback
1969 * @kcontrol: mixer control
1970 * @ucontrol: control element information
1971 *
1972 * Callback to set the value of a dapm semi enumerated double mixer control.
1973 *
1974 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1975 * used for handling bitfield coded enumeration for example.
1976 *
1977 * Returns 0 for success.
1978 */
1979int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1980 struct snd_ctl_elem_value *ucontrol)
1981{
1982 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001983 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001984 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001985 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00001986 struct snd_soc_dapm_update update;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001987
1988 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1989 return -EINVAL;
1990 mux = ucontrol->value.enumerated.item[0];
1991 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1992 mask = e->mask << e->shift_l;
1993 if (e->shift_l != e->shift_r) {
1994 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1995 return -EINVAL;
1996 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1997 mask |= e->mask << e->shift_r;
1998 }
1999
2000 mutex_lock(&widget->codec->mutex);
2001 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01002002 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002003
2004 update.kcontrol = kcontrol;
2005 update.widget = widget;
2006 update.reg = e->reg;
2007 update.mask = mask;
2008 update.val = val;
2009 widget->dapm->update = &update;
2010
Mark Brown3a655772009-10-05 17:23:30 +01002011 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002012
Mark Brown97404f22010-12-14 16:13:57 +00002013 widget->dapm->update = NULL;
Mark Brown1642e3d2009-10-05 16:24:26 +01002014
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002015 mutex_unlock(&widget->codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002016 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002017}
2018EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2019
2020/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002021 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2022 *
2023 * @kcontrol: mixer control
2024 * @uinfo: control element information
2025 *
2026 * Callback to provide information about a pin switch control.
2027 */
2028int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2029 struct snd_ctl_elem_info *uinfo)
2030{
2031 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2032 uinfo->count = 1;
2033 uinfo->value.integer.min = 0;
2034 uinfo->value.integer.max = 1;
2035
2036 return 0;
2037}
2038EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2039
2040/**
2041 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2042 *
2043 * @kcontrol: mixer control
2044 * @ucontrol: Value
2045 */
2046int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2047 struct snd_ctl_elem_value *ucontrol)
2048{
2049 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2050 const char *pin = (const char *)kcontrol->private_value;
2051
2052 mutex_lock(&codec->mutex);
2053
2054 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002055 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002056
2057 mutex_unlock(&codec->mutex);
2058
2059 return 0;
2060}
2061EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2062
2063/**
2064 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2065 *
2066 * @kcontrol: mixer control
2067 * @ucontrol: Value
2068 */
2069int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2070 struct snd_ctl_elem_value *ucontrol)
2071{
2072 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2073 const char *pin = (const char *)kcontrol->private_value;
2074
2075 mutex_lock(&codec->mutex);
2076
2077 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002078 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002079 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002080 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002081
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002082 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002083
2084 mutex_unlock(&codec->mutex);
2085
2086 return 0;
2087}
2088EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2089
2090/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002091 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002092 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002093 * @widget: widget template
2094 *
2095 * Creates a new dapm control based upon the template.
2096 *
2097 * Returns 0 for success else error.
2098 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002099int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002100 const struct snd_soc_dapm_widget *widget)
2101{
2102 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002103 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002104
2105 if ((w = dapm_cnew_widget(widget)) == NULL)
2106 return -ENOMEM;
2107
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002108 name_len = strlen(widget->name) + 1;
2109 if (dapm->codec->name_prefix)
2110 name_len += 1 + strlen(dapm->codec->name_prefix);
2111 w->name = kmalloc(name_len, GFP_KERNEL);
2112 if (w->name == NULL) {
2113 kfree(w);
2114 return -ENOMEM;
2115 }
2116 if (dapm->codec->name_prefix)
2117 snprintf(w->name, name_len, "%s %s",
2118 dapm->codec->name_prefix, widget->name);
2119 else
2120 snprintf(w->name, name_len, "%s", widget->name);
2121
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002122 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002123 w->dapm = dapm;
2124 w->codec = dapm->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002125 INIT_LIST_HEAD(&w->sources);
2126 INIT_LIST_HEAD(&w->sinks);
2127 INIT_LIST_HEAD(&w->list);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002128 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002129
2130 /* machine layer set ups unconnected pins and insertions */
2131 w->connected = 1;
2132 return 0;
2133}
2134EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2135
2136/**
Mark Brown4ba13272008-05-13 14:51:19 +02002137 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002138 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002139 * @widget: widget array
2140 * @num: number of widgets
2141 *
2142 * Creates new DAPM controls based upon the templates.
2143 *
2144 * Returns 0 for success else error.
2145 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002146int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002147 const struct snd_soc_dapm_widget *widget,
2148 int num)
2149{
2150 int i, ret;
2151
2152 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002153 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002154 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002155 dev_err(dapm->dev,
2156 "ASoC: Failed to create DAPM control %s: %d\n",
2157 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002158 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002159 }
Mark Brown4ba13272008-05-13 14:51:19 +02002160 widget++;
2161 }
2162 return 0;
2163}
2164EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2165
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002166static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002167 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002168{
2169 struct snd_soc_dapm_widget *w;
2170
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002171 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002172 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002173 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002174 continue;
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002175 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2176 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002177 if (strstr(w->sname, stream)) {
2178 switch(event) {
2179 case SND_SOC_DAPM_STREAM_START:
2180 w->active = 1;
2181 break;
2182 case SND_SOC_DAPM_STREAM_STOP:
2183 w->active = 0;
2184 break;
2185 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002186 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002187 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002188 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2189 break;
2190 }
2191 }
2192 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002193
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002194 dapm_power_widgets(dapm, event);
2195}
2196
2197/**
2198 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2199 * @rtd: PCM runtime data
2200 * @stream: stream name
2201 * @event: stream event
2202 *
2203 * Sends a stream event to the dapm core. The core then makes any
2204 * necessary widget power changes.
2205 *
2206 * Returns 0 for success else error.
2207 */
2208int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2209 const char *stream, int event)
2210{
2211 struct snd_soc_codec *codec = rtd->codec;
2212
2213 if (stream == NULL)
2214 return 0;
2215
2216 mutex_lock(&codec->mutex);
2217 soc_dapm_stream_event(&codec->dapm, stream, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002218 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002219 return 0;
2220}
2221EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
2222
2223/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002224 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002225 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002226 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002227 *
Mark Brown74b8f952009-06-06 11:26:15 +01002228 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002229 * a valid audio route and active audio stream.
2230 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2231 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002232 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002233int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002234{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002235 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002236}
Liam Girdwooda5302182008-07-07 13:35:17 +01002237EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002238
2239/**
Mark Brownda341832010-03-15 19:23:37 +00002240 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002241 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002242 * @pin: pin name
2243 *
2244 * Enables input/output pin regardless of any other state. This is
2245 * intended for use with microphone bias supplies used in microphone
2246 * jack detection.
2247 *
2248 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2249 * do any widget power switching.
2250 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002251int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2252 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002253{
2254 struct snd_soc_dapm_widget *w;
2255
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002256 list_for_each_entry(w, &dapm->card->widgets, list) {
2257 if (w->dapm != dapm)
2258 continue;
Mark Brownda341832010-03-15 19:23:37 +00002259 if (!strcmp(w->name, pin)) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002260 dev_dbg(w->dapm->dev,
2261 "dapm: force enable pin %s\n", pin);
Mark Brownda341832010-03-15 19:23:37 +00002262 w->connected = 1;
2263 w->force = 1;
2264 return 0;
2265 }
2266 }
2267
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002268 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Mark Brownda341832010-03-15 19:23:37 +00002269 return -EINVAL;
2270}
2271EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2272
2273/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002274 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002275 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002276 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002277 *
Mark Brown74b8f952009-06-06 11:26:15 +01002278 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002279 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2280 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002281 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002282int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2283 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002284{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002285 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002286}
2287EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2288
2289/**
Mark Brown5817b522008-09-24 11:23:11 +01002290 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002291 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002292 * @pin: pin name
2293 *
2294 * Marks the specified pin as being not connected, disabling it along
2295 * any parent or child widgets. At present this is identical to
2296 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2297 * additional things such as disabling controls which only affect
2298 * paths through the pin.
2299 *
2300 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2301 * do any widget power switching.
2302 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002303int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002304{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002305 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002306}
2307EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2308
2309/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002310 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002311 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002312 * @pin: audio signal pin endpoint (or start point)
2313 *
2314 * Get audio pin status - connected or disconnected.
2315 *
2316 * Returns 1 for connected otherwise 0.
2317 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002318int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2319 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002320{
2321 struct snd_soc_dapm_widget *w;
2322
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002323 list_for_each_entry(w, &dapm->card->widgets, list) {
2324 if (w->dapm != dapm)
2325 continue;
Liam Girdwooda5302182008-07-07 13:35:17 +01002326 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002327 return w->connected;
2328 }
2329
2330 return 0;
2331}
Liam Girdwooda5302182008-07-07 13:35:17 +01002332EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002333
2334/**
Mark Brown1547aba2010-05-07 21:11:40 +01002335 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002336 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01002337 * @pin: audio signal pin endpoint (or start point)
2338 *
2339 * Mark the given endpoint or pin as ignoring suspend. When the
2340 * system is disabled a path between two endpoints flagged as ignoring
2341 * suspend will not be disabled. The path must already be enabled via
2342 * normal means at suspend time, it will not be turned on if it was not
2343 * already enabled.
2344 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002345int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2346 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01002347{
2348 struct snd_soc_dapm_widget *w;
2349
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002350 list_for_each_entry(w, &dapm->card->widgets, list) {
2351 if (w->dapm != dapm)
2352 continue;
Mark Brown1547aba2010-05-07 21:11:40 +01002353 if (!strcmp(w->name, pin)) {
2354 w->ignore_suspend = 1;
2355 return 0;
2356 }
2357 }
2358
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002359 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Mark Brown1547aba2010-05-07 21:11:40 +01002360 return -EINVAL;
2361}
2362EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2363
2364/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002365 * snd_soc_dapm_free - free dapm resources
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002366 * @card: SoC device
Richard Purdie2b97eab2006-10-06 18:32:18 +02002367 *
2368 * Free all dapm widgets and resources.
2369 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002370void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002371{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002372 snd_soc_dapm_sys_remove(dapm->dev);
2373 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02002374 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002375}
2376EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2377
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002378static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01002379{
Mark Brown51737472009-06-22 13:16:51 +01002380 struct snd_soc_dapm_widget *w;
2381 LIST_HEAD(down_list);
2382 int powerdown = 0;
2383
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002384 list_for_each_entry(w, &dapm->card->widgets, list) {
2385 if (w->dapm != dapm)
2386 continue;
Mark Brown51737472009-06-22 13:16:51 +01002387 if (w->power) {
2388 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002389 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002390 powerdown = 1;
2391 }
2392 }
2393
2394 /* If there were no widgets to power down we're already in
2395 * standby.
2396 */
2397 if (powerdown) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002398 snd_soc_dapm_set_bias_level(NULL, dapm, SND_SOC_BIAS_PREPARE);
2399 dapm_seq_run(dapm, &down_list, 0, dapm_down_seq);
2400 snd_soc_dapm_set_bias_level(NULL, dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01002401 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002402}
Mark Brown51737472009-06-22 13:16:51 +01002403
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002404/*
2405 * snd_soc_dapm_shutdown - callback for system shutdown
2406 */
2407void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2408{
2409 struct snd_soc_codec *codec;
2410
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002411 list_for_each_entry(codec, &card->codec_dev_list, list) {
2412 soc_dapm_shutdown_codec(&codec->dapm);
2413 snd_soc_dapm_set_bias_level(card, &codec->dapm, SND_SOC_BIAS_OFF);
2414 }
Mark Brown51737472009-06-22 13:16:51 +01002415}
2416
Richard Purdie2b97eab2006-10-06 18:32:18 +02002417/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002418MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002419MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2420MODULE_LICENSE("GPL");