blob: 25e54230cc6a53a831ebc8083236dd53b194a657 [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 Brown15e4c722008-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 Brown15e4c722008-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 Brown15e4c722008-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 Brown15e4c722008-07-02 11:51:20 +0100114 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200115
116 kfree(buf);
Mark Brown15e4c722008-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 Smirl815ecf82008-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 Smirl815ecf82008-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 Brown99497882010-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 Brown99497882010-05-07 20:24:05 +0100483
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000484 switch (level) {
Mark Brown99497882010-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 Brown99497882010-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 Brown99497882010-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 Brown99497882010-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 Brown99497882010-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 Brown99497882010-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 Brown99497882010-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 Brown99497882010-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 Brown99497882010-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 Brown025756e2009-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 Brown025756e2009-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 Brown025756e2009-04-13 11:09:18 +0100640 dapm_update_bits(w);
641
Mark Brown025756e2009-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 Brown30173582011-02-11 11:42:19 +0000715 if (!path->sink)
716 continue;
717
718 if (path->sink->force) {
719 power = 1;
720 break;
721 }
722
723 if (path->sink->power_check &&
Mark Brown246d0a12009-04-22 18:24:55 +0100724 path->sink->power_check(path->sink)) {
725 power = 1;
726 break;
727 }
728 }
729
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200730 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100731
732 return power;
733}
734
Mark Brown38357ab2009-06-06 19:03:23 +0100735static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
736 struct snd_soc_dapm_widget *b,
737 int sort[])
Mark Brown42aa3412009-03-01 19:21:10 +0000738{
Mark Brown38357ab2009-06-06 19:03:23 +0100739 if (sort[a->id] != sort[b->id])
740 return sort[a->id] - sort[b->id];
Mark Brownb22ead22009-06-07 12:51:26 +0100741 if (a->reg != b->reg)
742 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +0000743 if (a->dapm != b->dapm)
744 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +0000745
Mark Brown38357ab2009-06-06 19:03:23 +0100746 return 0;
747}
Mark Brown42aa3412009-03-01 19:21:10 +0000748
Mark Brown38357ab2009-06-06 19:03:23 +0100749/* Insert a widget in order into a DAPM power sequence. */
750static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
751 struct list_head *list,
752 int sort[])
753{
754 struct snd_soc_dapm_widget *w;
755
756 list_for_each_entry(w, list, power_list)
757 if (dapm_seq_compare(new_widget, w, sort) < 0) {
758 list_add_tail(&new_widget->power_list, &w->power_list);
759 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000760 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100761
Mark Brown38357ab2009-06-06 19:03:23 +0100762 list_add_tail(&new_widget->power_list, list);
763}
Mark Brown42aa3412009-03-01 19:21:10 +0000764
Mark Brown68f89ad2010-11-03 23:51:49 -0400765static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
766 struct snd_soc_dapm_widget *w, int event)
767{
768 struct snd_soc_card *card = dapm->card;
769 const char *ev_name;
770 int power, ret;
771
772 switch (event) {
773 case SND_SOC_DAPM_PRE_PMU:
774 ev_name = "PRE_PMU";
775 power = 1;
776 break;
777 case SND_SOC_DAPM_POST_PMU:
778 ev_name = "POST_PMU";
779 power = 1;
780 break;
781 case SND_SOC_DAPM_PRE_PMD:
782 ev_name = "PRE_PMD";
783 power = 0;
784 break;
785 case SND_SOC_DAPM_POST_PMD:
786 ev_name = "POST_PMD";
787 power = 0;
788 break;
789 default:
790 BUG();
791 return;
792 }
793
794 if (w->power != power)
795 return;
796
797 if (w->event && (w->event_flags & event)) {
798 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
799 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -0400800 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400801 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -0400802 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400803 if (ret < 0)
804 pr_err("%s: %s event failed: %d\n",
805 ev_name, w->name, ret);
806 }
807}
808
Mark Brownb22ead22009-06-07 12:51:26 +0100809/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200810static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +0100811 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100812{
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200813 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -0400814 struct snd_soc_dapm_widget *w;
815 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +0100816 unsigned int value = 0;
817 unsigned int mask = 0;
818 unsigned int cur_mask;
819
820 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
821 power_list)->reg;
822
823 list_for_each_entry(w, pending, power_list) {
824 cur_mask = 1 << w->shift;
825 BUG_ON(reg != w->reg);
826
827 if (w->invert)
828 power = !w->power;
829 else
830 power = w->power;
831
832 mask |= cur_mask;
833 if (power)
834 value |= cur_mask;
835
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200836 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +0100837 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
838 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100839
Mark Brown68f89ad2010-11-03 23:51:49 -0400840 /* Check for events */
841 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
842 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100843 }
844
Mark Brown81628102009-06-07 13:21:24 +0100845 if (reg >= 0) {
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200846 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +0100847 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200848 value, mask, reg, card->pop_time);
849 pop_wait(card->pop_time);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200850 snd_soc_update_bits(dapm->codec, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +0100851 }
852
853 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -0400854 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
855 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +0000856 }
Mark Brown42aa3412009-03-01 19:21:10 +0000857}
858
Mark Brownb22ead22009-06-07 12:51:26 +0100859/* Apply a DAPM power sequence.
860 *
861 * We walk over a pre-sorted list of widgets to apply power to. In
862 * order to minimise the number of writes to the device required
863 * multiple widgets will be updated in a single write where possible.
864 * Currently anything that requires more than a single write is not
865 * handled.
866 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200867static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
868 struct list_head *list, int event, int sort[])
Mark Brownb22ead22009-06-07 12:51:26 +0100869{
870 struct snd_soc_dapm_widget *w, *n;
871 LIST_HEAD(pending);
872 int cur_sort = -1;
873 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200874 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown163cac02009-06-07 10:12:52 +0100875 int ret;
876
Mark Brownb22ead22009-06-07 12:51:26 +0100877 list_for_each_entry_safe(w, n, list, power_list) {
878 ret = 0;
879
880 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200881 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
882 w->dapm != cur_dapm) {
Mark Brownb22ead22009-06-07 12:51:26 +0100883 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200884 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +0100885
886 INIT_LIST_HEAD(&pending);
887 cur_sort = -1;
888 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200889 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +0100890 }
891
Mark Brown163cac02009-06-07 10:12:52 +0100892 switch (w->id) {
893 case snd_soc_dapm_pre:
894 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100895 list_for_each_entry_safe_continue(w, n, list,
896 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100897
Mark Brownb22ead22009-06-07 12:51:26 +0100898 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100899 ret = w->event(w,
900 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100901 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100902 ret = w->event(w,
903 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +0100904 break;
905
906 case snd_soc_dapm_post:
907 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100908 list_for_each_entry_safe_continue(w, n, list,
909 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100910
Mark Brownb22ead22009-06-07 12:51:26 +0100911 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100912 ret = w->event(w,
913 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100914 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100915 ret = w->event(w,
916 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100917 break;
918
919 case snd_soc_dapm_input:
920 case snd_soc_dapm_output:
921 case snd_soc_dapm_hp:
922 case snd_soc_dapm_mic:
923 case snd_soc_dapm_line:
924 case snd_soc_dapm_spk:
925 /* No register support currently */
Mark Brownb22ead22009-06-07 12:51:26 +0100926 ret = dapm_generic_apply_power(w);
Mark Brown163cac02009-06-07 10:12:52 +0100927 break;
928
929 default:
Mark Brown81628102009-06-07 13:21:24 +0100930 /* Queue it up for application */
931 cur_sort = sort[w->id];
932 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200933 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +0100934 list_move(&w->power_list, &pending);
935 break;
Mark Brown163cac02009-06-07 10:12:52 +0100936 }
Mark Brownb22ead22009-06-07 12:51:26 +0100937
938 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200939 dev_err(w->dapm->dev,
940 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +0100941 }
Mark Brownb22ead22009-06-07 12:51:26 +0100942
943 if (!list_empty(&pending))
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200944 dapm_seq_run_coalesced(dapm, &pending);
Mark Brown163cac02009-06-07 10:12:52 +0100945}
946
Mark Brown97404f22010-12-14 16:13:57 +0000947static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
948{
949 struct snd_soc_dapm_update *update = dapm->update;
950 struct snd_soc_dapm_widget *w;
951 int ret;
952
953 if (!update)
954 return;
955
956 w = update->widget;
957
958 if (w->event &&
959 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
960 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
961 if (ret != 0)
962 pr_err("%s DAPM pre-event failed: %d\n",
963 w->name, ret);
964 }
965
966 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
967 update->val);
968 if (ret < 0)
969 pr_err("%s DAPM update failed: %d\n", w->name, ret);
970
971 if (w->event &&
972 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
973 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
974 if (ret != 0)
975 pr_err("%s DAPM post-event failed: %d\n",
976 w->name, ret);
977 }
978}
979
980
981
Mark Brown42aa3412009-03-01 19:21:10 +0000982/*
Richard Purdie2b97eab2006-10-06 18:32:18 +0200983 * Scan each dapm widget for complete audio path.
984 * A complete path is a route that has valid endpoints i.e.:-
985 *
986 * o DAC to output pin.
987 * o Input Pin to ADC.
988 * o Input pin to Output pin (bypass, sidetone)
989 * o DAC to ADC (loopback).
990 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200991static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200992{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200993 struct snd_soc_card *card = dapm->codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200994 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200995 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +0100996 LIST_HEAD(up_list);
997 LIST_HEAD(down_list);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100998 int ret = 0;
Mark Brown38357ab2009-06-06 19:03:23 +0100999 int power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001000
Mark Brown84e90932010-11-04 00:07:02 -04001001 trace_snd_soc_dapm_start(card);
1002
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001003 list_for_each_entry(d, &card->dapm_list, list)
1004 if (d->n_widgets)
1005 d->dev_power = 0;
1006
Mark Brown6d3ddc82009-05-16 17:47:29 +01001007 /* Check which widgets we need to power and store them in
1008 * lists indicating if they should be powered up or down.
1009 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001010 list_for_each_entry(w, &card->widgets, list) {
Mark Brown6d3ddc82009-05-16 17:47:29 +01001011 switch (w->id) {
1012 case snd_soc_dapm_pre:
Mark Brown291f3bb2009-06-07 13:57:17 +01001013 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001014 break;
1015 case snd_soc_dapm_post:
Mark Brown291f3bb2009-06-07 13:57:17 +01001016 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001017 break;
1018
1019 default:
1020 if (!w->power_check)
1021 continue;
1022
Mark Brown99497882010-05-07 20:24:05 +01001023 if (!w->force)
Mark Brown50b6bce2009-11-23 13:11:53 +00001024 power = w->power_check(w);
Mark Brown99497882010-05-07 20:24:05 +01001025 else
1026 power = 1;
1027 if (power)
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001028 w->dapm->dev_power = 1;
Mark Brown452c5ea2009-05-17 21:41:23 +01001029
Mark Brown6d3ddc82009-05-16 17:47:29 +01001030 if (w->power == power)
1031 continue;
1032
Mark Brown84e90932010-11-04 00:07:02 -04001033 trace_snd_soc_dapm_widget_power(w, power);
1034
Mark Brown6d3ddc82009-05-16 17:47:29 +01001035 if (power)
Mark Brown291f3bb2009-06-07 13:57:17 +01001036 dapm_seq_insert(w, &up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001037 else
Mark Brown291f3bb2009-06-07 13:57:17 +01001038 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001039
1040 w->power = power;
1041 break;
1042 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001043 }
1044
Mark Brownb14b76a2009-08-17 11:55:38 +01001045 /* If there are no DAPM widgets then try to figure out power from the
1046 * event type.
1047 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001048 if (!dapm->n_widgets) {
Mark Brownb14b76a2009-08-17 11:55:38 +01001049 switch (event) {
1050 case SND_SOC_DAPM_STREAM_START:
1051 case SND_SOC_DAPM_STREAM_RESUME:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001052 dapm->dev_power = 1;
Mark Brownb14b76a2009-08-17 11:55:38 +01001053 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001054 case SND_SOC_DAPM_STREAM_STOP:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001055 dapm->dev_power = !!dapm->codec->active;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001056 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001057 case SND_SOC_DAPM_STREAM_SUSPEND:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001058 dapm->dev_power = 0;
Mark Brown50b6bce2009-11-23 13:11:53 +00001059 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001060 case SND_SOC_DAPM_STREAM_NOP:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001061 switch (dapm->bias_level) {
Mark Browna96ca332010-01-19 22:49:43 +00001062 case SND_SOC_BIAS_STANDBY:
1063 case SND_SOC_BIAS_OFF:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001064 dapm->dev_power = 0;
Mark Browna96ca332010-01-19 22:49:43 +00001065 break;
1066 default:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001067 dapm->dev_power = 1;
Mark Browna96ca332010-01-19 22:49:43 +00001068 break;
1069 }
Mark Brown50b6bce2009-11-23 13:11:53 +00001070 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001071 default:
1072 break;
1073 }
1074 }
1075
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001076 list_for_each_entry(d, &dapm->card->dapm_list, list) {
1077 if (d->dev_power && d->bias_level == SND_SOC_BIAS_OFF) {
1078 ret = snd_soc_dapm_set_bias_level(card, d,
1079 SND_SOC_BIAS_STANDBY);
1080 if (ret != 0)
1081 dev_err(d->dev,
1082 "Failed to turn on bias: %d\n", ret);
1083 }
Mark Browna96ca332010-01-19 22:49:43 +00001084
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001085 /* If we're changing to all on or all off then prepare */
1086 if ((d->dev_power && d->bias_level == SND_SOC_BIAS_STANDBY) ||
1087 (!d->dev_power && d->bias_level == SND_SOC_BIAS_ON)) {
1088 ret = snd_soc_dapm_set_bias_level(card, d,
1089 SND_SOC_BIAS_PREPARE);
1090 if (ret != 0)
1091 dev_err(d->dev,
1092 "Failed to prepare bias: %d\n", ret);
1093 }
Mark Brown452c5ea2009-05-17 21:41:23 +01001094 }
1095
Mark Brown6d3ddc82009-05-16 17:47:29 +01001096 /* Power down widgets first; try to avoid amplifying pops. */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001097 dapm_seq_run(dapm, &down_list, event, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001098
Mark Brown97404f22010-12-14 16:13:57 +00001099 dapm_widget_update(dapm);
1100
Mark Brown6d3ddc82009-05-16 17:47:29 +01001101 /* Now power up. */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001102 dapm_seq_run(dapm, &up_list, event, dapm_up_seq);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001103
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001104 list_for_each_entry(d, &dapm->card->dapm_list, list) {
1105 /* If we just powered the last thing off drop to standby bias */
1106 if (d->bias_level == SND_SOC_BIAS_PREPARE && !d->dev_power) {
1107 ret = snd_soc_dapm_set_bias_level(card, d,
1108 SND_SOC_BIAS_STANDBY);
1109 if (ret != 0)
1110 dev_err(d->dev,
1111 "Failed to apply standby bias: %d\n",
1112 ret);
1113 }
Mark Brown452c5ea2009-05-17 21:41:23 +01001114
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001115 /* If we're in standby and can support bias off then do that */
1116 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1117 d->idle_bias_off) {
1118 ret = snd_soc_dapm_set_bias_level(card, d,
1119 SND_SOC_BIAS_OFF);
1120 if (ret != 0)
1121 dev_err(d->dev,
1122 "Failed to turn off bias: %d\n", ret);
1123 }
Mark Browna96ca332010-01-19 22:49:43 +00001124
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001125 /* If we just powered up then move to active bias */
1126 if (d->bias_level == SND_SOC_BIAS_PREPARE && d->dev_power) {
1127 ret = snd_soc_dapm_set_bias_level(card, d,
1128 SND_SOC_BIAS_ON);
1129 if (ret != 0)
1130 dev_err(d->dev,
1131 "Failed to apply active bias: %d\n",
1132 ret);
1133 }
Mark Brown452c5ea2009-05-17 21:41:23 +01001134 }
1135
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001136 pop_dbg(dapm->dev, card->pop_time,
1137 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001138 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001139
Mark Brown84e90932010-11-04 00:07:02 -04001140 trace_snd_soc_dapm_done(card);
1141
Mark Brown42aa3412009-03-01 19:21:10 +00001142 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001143}
1144
Mark Brown79fb9382009-08-21 16:38:13 +01001145#ifdef CONFIG_DEBUG_FS
1146static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1147{
1148 file->private_data = inode->i_private;
1149 return 0;
1150}
1151
1152static ssize_t dapm_widget_power_read_file(struct file *file,
1153 char __user *user_buf,
1154 size_t count, loff_t *ppos)
1155{
1156 struct snd_soc_dapm_widget *w = file->private_data;
1157 char *buf;
1158 int in, out;
1159 ssize_t ret;
1160 struct snd_soc_dapm_path *p = NULL;
1161
1162 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1163 if (!buf)
1164 return -ENOMEM;
1165
1166 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001167 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001168 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001169 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001170
Mark Brownd033c362009-12-04 15:25:56 +00001171 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001172 w->name, w->power ? "On" : "Off", in, out);
1173
Mark Brownd033c362009-12-04 15:25:56 +00001174 if (w->reg >= 0)
1175 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1176 " - R%d(0x%x) bit %d",
1177 w->reg, w->reg, w->shift);
1178
1179 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1180
Mark Brown3eef08b2009-09-14 16:49:00 +01001181 if (w->sname)
1182 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1183 w->sname,
1184 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001185
1186 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001187 if (p->connected && !p->connected(w, p->sink))
1188 continue;
1189
Mark Brown79fb9382009-08-21 16:38:13 +01001190 if (p->connect)
1191 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1192 " in %s %s\n",
1193 p->name ? p->name : "static",
1194 p->source->name);
1195 }
1196 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001197 if (p->connected && !p->connected(w, p->sink))
1198 continue;
1199
Mark Brown79fb9382009-08-21 16:38:13 +01001200 if (p->connect)
1201 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1202 " out %s %s\n",
1203 p->name ? p->name : "static",
1204 p->sink->name);
1205 }
1206
1207 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1208
1209 kfree(buf);
1210 return ret;
1211}
1212
1213static const struct file_operations dapm_widget_power_fops = {
1214 .open = dapm_widget_power_open_file,
1215 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001216 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001217};
1218
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001219void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001220{
1221 struct snd_soc_dapm_widget *w;
1222 struct dentry *d;
1223
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001224 if (!dapm->debugfs_dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001225 return;
1226
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001227 list_for_each_entry(w, &dapm->card->widgets, list) {
1228 if (!w->name || w->dapm != dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001229 continue;
1230
1231 d = debugfs_create_file(w->name, 0444,
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001232 dapm->debugfs_dapm, w,
Mark Brown79fb9382009-08-21 16:38:13 +01001233 &dapm_widget_power_fops);
1234 if (!d)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001235 dev_warn(w->dapm->dev,
1236 "ASoC: Failed to create %s debugfs file\n",
1237 w->name);
Mark Brown79fb9382009-08-21 16:38:13 +01001238 }
1239}
1240#else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001241void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001242{
1243}
1244#endif
1245
Richard Purdie2b97eab2006-10-06 18:32:18 +02001246/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001247static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001248 struct snd_kcontrol *kcontrol, int change,
1249 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001250{
1251 struct snd_soc_dapm_path *path;
1252 int found = 0;
1253
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001254 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001255 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001256 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001257 return -ENODEV;
1258
Mark Brown3a655772009-10-05 17:23:30 +01001259 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001260 return 0;
1261
1262 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001263 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001264 if (path->kcontrol != kcontrol)
1265 continue;
1266
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001267 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001268 continue;
1269
1270 found = 1;
1271 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001272 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001273 path->connect = 1; /* new connection */
1274 else
1275 path->connect = 0; /* old connection must be powered down */
1276 }
1277
Mark Brownb91b8fa2010-01-20 18:18:35 +00001278 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001279 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001280
1281 return 0;
1282}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001283
Milan plzik1b075e32008-01-10 14:39:46 +01001284/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001285static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001286 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001287{
1288 struct snd_soc_dapm_path *path;
1289 int found = 0;
1290
Milan plzik1b075e32008-01-10 14:39:46 +01001291 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001292 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001293 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001294 return -ENODEV;
1295
Richard Purdie2b97eab2006-10-06 18:32:18 +02001296 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001297 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001298 if (path->kcontrol != kcontrol)
1299 continue;
1300
1301 /* found, now check type */
1302 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001303 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001304 break;
1305 }
1306
Mark Brownb91b8fa2010-01-20 18:18:35 +00001307 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001308 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001309
1310 return 0;
1311}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001312
1313/* show dapm widget status in sys fs */
1314static ssize_t dapm_widget_show(struct device *dev,
1315 struct device_attribute *attr, char *buf)
1316{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001317 struct snd_soc_pcm_runtime *rtd =
1318 container_of(dev, struct snd_soc_pcm_runtime, dev);
1319 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001320 struct snd_soc_dapm_widget *w;
1321 int count = 0;
1322 char *state = "not set";
1323
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001324 list_for_each_entry(w, &codec->card->widgets, list) {
1325 if (w->dapm != &codec->dapm)
1326 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001327
1328 /* only display widgets that burnm power */
1329 switch (w->id) {
1330 case snd_soc_dapm_hp:
1331 case snd_soc_dapm_mic:
1332 case snd_soc_dapm_spk:
1333 case snd_soc_dapm_line:
1334 case snd_soc_dapm_micbias:
1335 case snd_soc_dapm_dac:
1336 case snd_soc_dapm_adc:
1337 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001338 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001339 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001340 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001341 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001342 if (w->name)
1343 count += sprintf(buf + count, "%s: %s\n",
1344 w->name, w->power ? "On":"Off");
1345 break;
1346 default:
1347 break;
1348 }
1349 }
1350
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001351 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001352 case SND_SOC_BIAS_ON:
1353 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001354 break;
Mark Brown0be98982008-05-19 12:31:28 +02001355 case SND_SOC_BIAS_PREPARE:
1356 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001357 break;
Mark Brown0be98982008-05-19 12:31:28 +02001358 case SND_SOC_BIAS_STANDBY:
1359 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001360 break;
Mark Brown0be98982008-05-19 12:31:28 +02001361 case SND_SOC_BIAS_OFF:
1362 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001363 break;
1364 }
1365 count += sprintf(buf + count, "PM State: %s\n", state);
1366
1367 return count;
1368}
1369
1370static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1371
1372int snd_soc_dapm_sys_add(struct device *dev)
1373{
Troy Kisky12ef1932008-10-13 17:42:14 -07001374 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001375}
1376
1377static void snd_soc_dapm_sys_remove(struct device *dev)
1378{
Mark Brownaef90842009-05-16 17:53:16 +01001379 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001380}
1381
1382/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001383static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001384{
1385 struct snd_soc_dapm_widget *w, *next_w;
1386 struct snd_soc_dapm_path *p, *next_p;
1387
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001388 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1389 if (w->dapm != dapm)
1390 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001391 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001392 /*
1393 * remove source and sink paths associated to this widget.
1394 * While removing the path, remove reference to it from both
1395 * source and sink widgets so that path is removed only once.
1396 */
1397 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1398 list_del(&p->list_sink);
1399 list_del(&p->list_source);
1400 list_del(&p->list);
1401 kfree(p->long_name);
1402 kfree(p);
1403 }
1404 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1405 list_del(&p->list_sink);
1406 list_del(&p->list_source);
1407 list_del(&p->list);
1408 kfree(p->long_name);
1409 kfree(p);
1410 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001411 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001412 kfree(w);
1413 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001414}
1415
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001416static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001417 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001418{
1419 struct snd_soc_dapm_widget *w;
1420
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001421 list_for_each_entry(w, &dapm->card->widgets, list) {
1422 if (w->dapm != dapm)
1423 continue;
Liam Girdwooda5302182008-07-07 13:35:17 +01001424 if (!strcmp(w->name, pin)) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001425 dev_dbg(w->dapm->dev, "dapm: pin %s = %d\n",
1426 pin, status);
Liam Girdwooda5302182008-07-07 13:35:17 +01001427 w->connected = status;
Mark Brown5b9e87c2010-03-22 13:36:13 +00001428 /* Allow disabling of forced pins */
1429 if (status == 0)
1430 w->force = 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001431 return 0;
1432 }
1433 }
1434
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001435 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001436 return -EINVAL;
1437}
1438
Richard Purdie2b97eab2006-10-06 18:32:18 +02001439/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001440 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001441 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001442 *
1443 * Walks all dapm audio paths and powers widgets according to their
1444 * stream or path usage.
1445 *
1446 * Returns 0 for success.
1447 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001448int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001449{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001450 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001451}
Liam Girdwooda5302182008-07-07 13:35:17 +01001452EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001453
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001454static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001455 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001456{
1457 struct snd_soc_dapm_path *path;
1458 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001459 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001460 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001461 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001462 const char *source;
1463 char prefixed_sink[80];
1464 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001465 int ret = 0;
1466
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001467 if (dapm->codec->name_prefix) {
1468 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1469 dapm->codec->name_prefix, route->sink);
1470 sink = prefixed_sink;
1471 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1472 dapm->codec->name_prefix, route->source);
1473 source = prefixed_source;
1474 } else {
1475 sink = route->sink;
1476 source = route->source;
1477 }
1478
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001479 /*
1480 * find src and dest widgets over all widgets but favor a widget from
1481 * current DAPM context
1482 */
1483 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001484 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001485 wtsink = w;
1486 if (w->dapm == dapm)
1487 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001488 continue;
1489 }
1490 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001491 wtsource = w;
1492 if (w->dapm == dapm)
1493 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001494 }
1495 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001496 /* use widget from another DAPM context if not found from this */
1497 if (!wsink)
1498 wsink = wtsink;
1499 if (!wsource)
1500 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001501
1502 if (wsource == NULL || wsink == NULL)
1503 return -ENODEV;
1504
1505 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1506 if (!path)
1507 return -ENOMEM;
1508
1509 path->source = wsource;
1510 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001511 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001512 INIT_LIST_HEAD(&path->list);
1513 INIT_LIST_HEAD(&path->list_source);
1514 INIT_LIST_HEAD(&path->list_sink);
1515
1516 /* check for external widgets */
1517 if (wsink->id == snd_soc_dapm_input) {
1518 if (wsource->id == snd_soc_dapm_micbias ||
1519 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001520 wsource->id == snd_soc_dapm_line ||
1521 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001522 wsink->ext = 1;
1523 }
1524 if (wsource->id == snd_soc_dapm_output) {
1525 if (wsink->id == snd_soc_dapm_spk ||
1526 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001527 wsink->id == snd_soc_dapm_line ||
1528 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001529 wsource->ext = 1;
1530 }
1531
1532 /* connect static paths */
1533 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001534 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001535 list_add(&path->list_sink, &wsink->sources);
1536 list_add(&path->list_source, &wsource->sinks);
1537 path->connect = 1;
1538 return 0;
1539 }
1540
1541 /* connect dynamic paths */
1542 switch(wsink->id) {
1543 case snd_soc_dapm_adc:
1544 case snd_soc_dapm_dac:
1545 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001546 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001547 case snd_soc_dapm_input:
1548 case snd_soc_dapm_output:
1549 case snd_soc_dapm_micbias:
1550 case snd_soc_dapm_vmid:
1551 case snd_soc_dapm_pre:
1552 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001553 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001554 case snd_soc_dapm_aif_in:
1555 case snd_soc_dapm_aif_out:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001556 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001557 list_add(&path->list_sink, &wsink->sources);
1558 list_add(&path->list_source, &wsource->sinks);
1559 path->connect = 1;
1560 return 0;
1561 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001562 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001563 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001564 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Richard Purdie2b97eab2006-10-06 18:32:18 +02001565 &wsink->kcontrols[0]);
1566 if (ret != 0)
1567 goto err;
1568 break;
1569 case snd_soc_dapm_switch:
1570 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001571 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001572 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001573 if (ret != 0)
1574 goto err;
1575 break;
1576 case snd_soc_dapm_hp:
1577 case snd_soc_dapm_mic:
1578 case snd_soc_dapm_line:
1579 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001580 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001581 list_add(&path->list_sink, &wsink->sources);
1582 list_add(&path->list_source, &wsource->sinks);
1583 path->connect = 0;
1584 return 0;
1585 }
1586 return 0;
1587
1588err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001589 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1590 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001591 kfree(path);
1592 return ret;
1593}
Mark Brown105f1c22008-05-13 14:52:19 +02001594
1595/**
Mark Brown105f1c22008-05-13 14:52:19 +02001596 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001597 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02001598 * @route: audio routes
1599 * @num: number of routes
1600 *
1601 * Connects 2 dapm widgets together via a named audio path. The sink is
1602 * the widget receiving the audio signal, whilst the source is the sender
1603 * of the audio signal.
1604 *
1605 * Returns 0 for success else error. On error all resources can be freed
1606 * with a call to snd_soc_card_free().
1607 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001608int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02001609 const struct snd_soc_dapm_route *route, int num)
1610{
1611 int i, ret;
1612
1613 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001614 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001615 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001616 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1617 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02001618 return ret;
1619 }
1620 route++;
1621 }
1622
1623 return 0;
1624}
1625EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1626
1627/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001628 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001629 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001630 *
1631 * Checks the codec for any new dapm widgets and creates them if found.
1632 *
1633 * Returns 0 for success.
1634 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001635int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001636{
1637 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00001638 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001639
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001640 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001641 {
1642 if (w->new)
1643 continue;
1644
1645 switch(w->id) {
1646 case snd_soc_dapm_switch:
1647 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001648 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001649 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001650 dapm_new_mixer(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001651 break;
1652 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001653 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001654 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001655 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001656 dapm_new_mux(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001657 break;
1658 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01001659 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01001660 w->power_check = dapm_adc_check_power;
1661 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001662 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01001663 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01001664 w->power_check = dapm_dac_check_power;
1665 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001666 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001667 case snd_soc_dapm_out_drv:
Mark Brownb75576d2009-04-20 17:56:13 +01001668 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001669 dapm_new_pga(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001670 break;
1671 case snd_soc_dapm_input:
1672 case snd_soc_dapm_output:
1673 case snd_soc_dapm_micbias:
1674 case snd_soc_dapm_spk:
1675 case snd_soc_dapm_hp:
1676 case snd_soc_dapm_mic:
1677 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001678 w->power_check = dapm_generic_check_power;
1679 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001680 case snd_soc_dapm_supply:
1681 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001682 case snd_soc_dapm_vmid:
1683 case snd_soc_dapm_pre:
1684 case snd_soc_dapm_post:
1685 break;
1686 }
Mark Brownb66a70d2011-02-09 18:04:11 +00001687
1688 /* Read the initial power state from the device */
1689 if (w->reg >= 0) {
1690 val = snd_soc_read(w->codec, w->reg);
1691 val &= 1 << w->shift;
1692 if (w->invert)
1693 val = !val;
1694
1695 if (val)
1696 w->power = 1;
1697 }
1698
Richard Purdie2b97eab2006-10-06 18:32:18 +02001699 w->new = 1;
1700 }
1701
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001702 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001703 return 0;
1704}
1705EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1706
1707/**
1708 * snd_soc_dapm_get_volsw - dapm mixer get callback
1709 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001710 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001711 *
1712 * Callback to get the value of a dapm mixer control.
1713 *
1714 * Returns 0 for success.
1715 */
1716int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1717 struct snd_ctl_elem_value *ucontrol)
1718{
1719 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001720 struct soc_mixer_control *mc =
1721 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04001722 unsigned int reg = mc->reg;
1723 unsigned int shift = mc->shift;
1724 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001725 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001726 unsigned int invert = mc->invert;
1727 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001728
Richard Purdie2b97eab2006-10-06 18:32:18 +02001729 ucontrol->value.integer.value[0] =
1730 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1731 if (shift != rshift)
1732 ucontrol->value.integer.value[1] =
1733 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1734 if (invert) {
1735 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001736 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001737 if (shift != rshift)
1738 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001739 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001740 }
1741
1742 return 0;
1743}
1744EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1745
1746/**
1747 * snd_soc_dapm_put_volsw - dapm mixer set callback
1748 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001749 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001750 *
1751 * Callback to set the value of a dapm mixer control.
1752 *
1753 * Returns 0 for success.
1754 */
1755int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1756 struct snd_ctl_elem_value *ucontrol)
1757{
1758 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001759 struct soc_mixer_control *mc =
1760 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04001761 unsigned int reg = mc->reg;
1762 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001763 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001764 unsigned int mask = (1 << fls(max)) - 1;
1765 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07001766 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00001767 int connect, change;
1768 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001769
1770 val = (ucontrol->value.integer.value[0] & mask);
1771
1772 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001773 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07001774 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001775 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001776
1777 mutex_lock(&widget->codec->mutex);
1778 widget->value = val;
1779
Stephen Warrene9cf7042011-01-27 14:54:05 -07001780 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00001781 if (change) {
Mark Brown283375c2009-12-07 18:09:03 +00001782 if (val)
1783 /* new connection */
1784 connect = invert ? 0:1;
1785 else
1786 /* old connection must be powered down */
1787 connect = invert ? 1:0;
1788
Mark Brown97404f22010-12-14 16:13:57 +00001789 update.kcontrol = kcontrol;
1790 update.widget = widget;
1791 update.reg = reg;
1792 update.mask = mask;
1793 update.val = val;
1794 widget->dapm->update = &update;
1795
Mark Brown283375c2009-12-07 18:09:03 +00001796 dapm_mixer_update_power(widget, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00001797
1798 widget->dapm->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00001799 }
1800
Richard Purdie2b97eab2006-10-06 18:32:18 +02001801 mutex_unlock(&widget->codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00001802 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001803}
1804EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1805
1806/**
1807 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1808 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001809 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001810 *
1811 * Callback to get the value of a dapm enumerated double mixer control.
1812 *
1813 * Returns 0 for success.
1814 */
1815int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1816 struct snd_ctl_elem_value *ucontrol)
1817{
1818 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1819 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001820 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001821
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001822 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001823 ;
1824 val = snd_soc_read(widget->codec, e->reg);
1825 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1826 if (e->shift_l != e->shift_r)
1827 ucontrol->value.enumerated.item[1] =
1828 (val >> e->shift_r) & (bitmask - 1);
1829
1830 return 0;
1831}
1832EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1833
1834/**
1835 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1836 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001837 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001838 *
1839 * Callback to set the value of a dapm enumerated double mixer control.
1840 *
1841 * Returns 0 for success.
1842 */
1843int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1844 struct snd_ctl_elem_value *ucontrol)
1845{
1846 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1847 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001848 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001849 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00001850 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001851
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001852 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001853 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001854 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001855 return -EINVAL;
1856 mux = ucontrol->value.enumerated.item[0];
1857 val = mux << e->shift_l;
1858 mask = (bitmask - 1) << e->shift_l;
1859 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001860 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001861 return -EINVAL;
1862 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1863 mask |= (bitmask - 1) << e->shift_r;
1864 }
1865
1866 mutex_lock(&widget->codec->mutex);
1867 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01001868 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00001869
1870 update.kcontrol = kcontrol;
1871 update.widget = widget;
1872 update.reg = e->reg;
1873 update.mask = mask;
1874 update.val = val;
1875 widget->dapm->update = &update;
1876
Mark Brown3a655772009-10-05 17:23:30 +01001877 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01001878
Mark Brown97404f22010-12-14 16:13:57 +00001879 widget->dapm->update = NULL;
Mark Brown1642e3d2009-10-05 16:24:26 +01001880
Richard Purdie2b97eab2006-10-06 18:32:18 +02001881 mutex_unlock(&widget->codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00001882 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001883}
1884EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1885
1886/**
Mark Brownd2b247a2009-10-06 15:21:04 +01001887 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1888 * @kcontrol: mixer control
1889 * @ucontrol: control element information
1890 *
1891 * Returns 0 for success.
1892 */
1893int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1894 struct snd_ctl_elem_value *ucontrol)
1895{
1896 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1897
1898 ucontrol->value.enumerated.item[0] = widget->value;
1899
1900 return 0;
1901}
1902EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1903
1904/**
1905 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1906 * @kcontrol: mixer control
1907 * @ucontrol: control element information
1908 *
1909 * Returns 0 for success.
1910 */
1911int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
1912 struct snd_ctl_elem_value *ucontrol)
1913{
1914 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1915 struct soc_enum *e =
1916 (struct soc_enum *)kcontrol->private_value;
1917 int change;
1918 int ret = 0;
1919
1920 if (ucontrol->value.enumerated.item[0] >= e->max)
1921 return -EINVAL;
1922
1923 mutex_lock(&widget->codec->mutex);
1924
1925 change = widget->value != ucontrol->value.enumerated.item[0];
1926 widget->value = ucontrol->value.enumerated.item[0];
1927 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
1928
1929 mutex_unlock(&widget->codec->mutex);
1930 return ret;
1931}
1932EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
1933
1934/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001935 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1936 * callback
1937 * @kcontrol: mixer control
1938 * @ucontrol: control element information
1939 *
1940 * Callback to get the value of a dapm semi enumerated double mixer control.
1941 *
1942 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1943 * used for handling bitfield coded enumeration for example.
1944 *
1945 * Returns 0 for success.
1946 */
1947int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1948 struct snd_ctl_elem_value *ucontrol)
1949{
1950 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001951 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001952 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001953
1954 reg_val = snd_soc_read(widget->codec, e->reg);
1955 val = (reg_val >> e->shift_l) & e->mask;
1956 for (mux = 0; mux < e->max; mux++) {
1957 if (val == e->values[mux])
1958 break;
1959 }
1960 ucontrol->value.enumerated.item[0] = mux;
1961 if (e->shift_l != e->shift_r) {
1962 val = (reg_val >> e->shift_r) & e->mask;
1963 for (mux = 0; mux < e->max; mux++) {
1964 if (val == e->values[mux])
1965 break;
1966 }
1967 ucontrol->value.enumerated.item[1] = mux;
1968 }
1969
1970 return 0;
1971}
1972EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1973
1974/**
1975 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1976 * callback
1977 * @kcontrol: mixer control
1978 * @ucontrol: control element information
1979 *
1980 * Callback to set the value of a dapm semi enumerated double mixer control.
1981 *
1982 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1983 * used for handling bitfield coded enumeration for example.
1984 *
1985 * Returns 0 for success.
1986 */
1987int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1988 struct snd_ctl_elem_value *ucontrol)
1989{
1990 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001991 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001992 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001993 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00001994 struct snd_soc_dapm_update update;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001995
1996 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1997 return -EINVAL;
1998 mux = ucontrol->value.enumerated.item[0];
1999 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2000 mask = e->mask << e->shift_l;
2001 if (e->shift_l != e->shift_r) {
2002 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2003 return -EINVAL;
2004 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2005 mask |= e->mask << e->shift_r;
2006 }
2007
2008 mutex_lock(&widget->codec->mutex);
2009 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01002010 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002011
2012 update.kcontrol = kcontrol;
2013 update.widget = widget;
2014 update.reg = e->reg;
2015 update.mask = mask;
2016 update.val = val;
2017 widget->dapm->update = &update;
2018
Mark Brown3a655772009-10-05 17:23:30 +01002019 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002020
Mark Brown97404f22010-12-14 16:13:57 +00002021 widget->dapm->update = NULL;
Mark Brown1642e3d2009-10-05 16:24:26 +01002022
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002023 mutex_unlock(&widget->codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002024 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002025}
2026EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2027
2028/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002029 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2030 *
2031 * @kcontrol: mixer control
2032 * @uinfo: control element information
2033 *
2034 * Callback to provide information about a pin switch control.
2035 */
2036int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2037 struct snd_ctl_elem_info *uinfo)
2038{
2039 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2040 uinfo->count = 1;
2041 uinfo->value.integer.min = 0;
2042 uinfo->value.integer.max = 1;
2043
2044 return 0;
2045}
2046EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2047
2048/**
2049 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2050 *
2051 * @kcontrol: mixer control
2052 * @ucontrol: Value
2053 */
2054int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2055 struct snd_ctl_elem_value *ucontrol)
2056{
2057 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2058 const char *pin = (const char *)kcontrol->private_value;
2059
2060 mutex_lock(&codec->mutex);
2061
2062 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002063 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002064
2065 mutex_unlock(&codec->mutex);
2066
2067 return 0;
2068}
2069EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2070
2071/**
2072 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2073 *
2074 * @kcontrol: mixer control
2075 * @ucontrol: Value
2076 */
2077int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2078 struct snd_ctl_elem_value *ucontrol)
2079{
2080 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2081 const char *pin = (const char *)kcontrol->private_value;
2082
2083 mutex_lock(&codec->mutex);
2084
2085 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002086 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002087 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002088 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002089
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002090 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002091
2092 mutex_unlock(&codec->mutex);
2093
2094 return 0;
2095}
2096EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2097
2098/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002099 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002100 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002101 * @widget: widget template
2102 *
2103 * Creates a new dapm control based upon the template.
2104 *
2105 * Returns 0 for success else error.
2106 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002107int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002108 const struct snd_soc_dapm_widget *widget)
2109{
2110 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002111 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002112
2113 if ((w = dapm_cnew_widget(widget)) == NULL)
2114 return -ENOMEM;
2115
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002116 name_len = strlen(widget->name) + 1;
2117 if (dapm->codec->name_prefix)
2118 name_len += 1 + strlen(dapm->codec->name_prefix);
2119 w->name = kmalloc(name_len, GFP_KERNEL);
2120 if (w->name == NULL) {
2121 kfree(w);
2122 return -ENOMEM;
2123 }
2124 if (dapm->codec->name_prefix)
2125 snprintf(w->name, name_len, "%s %s",
2126 dapm->codec->name_prefix, widget->name);
2127 else
2128 snprintf(w->name, name_len, "%s", widget->name);
2129
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002130 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002131 w->dapm = dapm;
2132 w->codec = dapm->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002133 INIT_LIST_HEAD(&w->sources);
2134 INIT_LIST_HEAD(&w->sinks);
2135 INIT_LIST_HEAD(&w->list);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002136 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002137
2138 /* machine layer set ups unconnected pins and insertions */
2139 w->connected = 1;
2140 return 0;
2141}
2142EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2143
2144/**
Mark Brown4ba13272008-05-13 14:51:19 +02002145 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002146 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002147 * @widget: widget array
2148 * @num: number of widgets
2149 *
2150 * Creates new DAPM controls based upon the templates.
2151 *
2152 * Returns 0 for success else error.
2153 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002154int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002155 const struct snd_soc_dapm_widget *widget,
2156 int num)
2157{
2158 int i, ret;
2159
2160 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002161 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002162 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002163 dev_err(dapm->dev,
2164 "ASoC: Failed to create DAPM control %s: %d\n",
2165 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002166 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002167 }
Mark Brown4ba13272008-05-13 14:51:19 +02002168 widget++;
2169 }
2170 return 0;
2171}
2172EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2173
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002174static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002175 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002176{
2177 struct snd_soc_dapm_widget *w;
2178
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002179 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002180 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002181 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002182 continue;
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002183 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2184 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002185 if (strstr(w->sname, stream)) {
2186 switch(event) {
2187 case SND_SOC_DAPM_STREAM_START:
2188 w->active = 1;
2189 break;
2190 case SND_SOC_DAPM_STREAM_STOP:
2191 w->active = 0;
2192 break;
2193 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002194 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002195 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002196 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2197 break;
2198 }
2199 }
2200 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002201
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002202 dapm_power_widgets(dapm, event);
2203}
2204
2205/**
2206 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2207 * @rtd: PCM runtime data
2208 * @stream: stream name
2209 * @event: stream event
2210 *
2211 * Sends a stream event to the dapm core. The core then makes any
2212 * necessary widget power changes.
2213 *
2214 * Returns 0 for success else error.
2215 */
2216int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2217 const char *stream, int event)
2218{
2219 struct snd_soc_codec *codec = rtd->codec;
2220
2221 if (stream == NULL)
2222 return 0;
2223
2224 mutex_lock(&codec->mutex);
2225 soc_dapm_stream_event(&codec->dapm, stream, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002226 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002227 return 0;
2228}
2229EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
2230
2231/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002232 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002233 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002234 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002235 *
Mark Brown74b8f952009-06-06 11:26:15 +01002236 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002237 * a valid audio route and active audio stream.
2238 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2239 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002240 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002241int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002242{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002243 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002244}
Liam Girdwooda5302182008-07-07 13:35:17 +01002245EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002246
2247/**
Mark Brownda341832010-03-15 19:23:37 +00002248 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002249 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002250 * @pin: pin name
2251 *
2252 * Enables input/output pin regardless of any other state. This is
2253 * intended for use with microphone bias supplies used in microphone
2254 * jack detection.
2255 *
2256 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2257 * do any widget power switching.
2258 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002259int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2260 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002261{
2262 struct snd_soc_dapm_widget *w;
2263
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002264 list_for_each_entry(w, &dapm->card->widgets, list) {
2265 if (w->dapm != dapm)
2266 continue;
Mark Brownda341832010-03-15 19:23:37 +00002267 if (!strcmp(w->name, pin)) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002268 dev_dbg(w->dapm->dev,
2269 "dapm: force enable pin %s\n", pin);
Mark Brownda341832010-03-15 19:23:37 +00002270 w->connected = 1;
2271 w->force = 1;
2272 return 0;
2273 }
2274 }
2275
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002276 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Mark Brownda341832010-03-15 19:23:37 +00002277 return -EINVAL;
2278}
2279EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2280
2281/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002282 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002283 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002284 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002285 *
Mark Brown74b8f952009-06-06 11:26:15 +01002286 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002287 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2288 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002289 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002290int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2291 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002292{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002293 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002294}
2295EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2296
2297/**
Mark Brown5817b522008-09-24 11:23:11 +01002298 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002299 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002300 * @pin: pin name
2301 *
2302 * Marks the specified pin as being not connected, disabling it along
2303 * any parent or child widgets. At present this is identical to
2304 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2305 * additional things such as disabling controls which only affect
2306 * paths through the pin.
2307 *
2308 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2309 * do any widget power switching.
2310 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002311int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002312{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002313 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002314}
2315EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2316
2317/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002318 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002319 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002320 * @pin: audio signal pin endpoint (or start point)
2321 *
2322 * Get audio pin status - connected or disconnected.
2323 *
2324 * Returns 1 for connected otherwise 0.
2325 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002326int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2327 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002328{
2329 struct snd_soc_dapm_widget *w;
2330
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002331 list_for_each_entry(w, &dapm->card->widgets, list) {
2332 if (w->dapm != dapm)
2333 continue;
Liam Girdwooda5302182008-07-07 13:35:17 +01002334 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002335 return w->connected;
2336 }
2337
2338 return 0;
2339}
Liam Girdwooda5302182008-07-07 13:35:17 +01002340EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002341
2342/**
Mark Brown1547aba2010-05-07 21:11:40 +01002343 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002344 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01002345 * @pin: audio signal pin endpoint (or start point)
2346 *
2347 * Mark the given endpoint or pin as ignoring suspend. When the
2348 * system is disabled a path between two endpoints flagged as ignoring
2349 * suspend will not be disabled. The path must already be enabled via
2350 * normal means at suspend time, it will not be turned on if it was not
2351 * already enabled.
2352 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002353int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2354 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01002355{
2356 struct snd_soc_dapm_widget *w;
2357
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002358 list_for_each_entry(w, &dapm->card->widgets, list) {
2359 if (w->dapm != dapm)
2360 continue;
Mark Brown1547aba2010-05-07 21:11:40 +01002361 if (!strcmp(w->name, pin)) {
2362 w->ignore_suspend = 1;
2363 return 0;
2364 }
2365 }
2366
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002367 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Mark Brown1547aba2010-05-07 21:11:40 +01002368 return -EINVAL;
2369}
2370EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2371
2372/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002373 * snd_soc_dapm_free - free dapm resources
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002374 * @card: SoC device
Richard Purdie2b97eab2006-10-06 18:32:18 +02002375 *
2376 * Free all dapm widgets and resources.
2377 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002378void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002379{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002380 snd_soc_dapm_sys_remove(dapm->dev);
2381 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02002382 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002383}
2384EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2385
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002386static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01002387{
Mark Brown51737472009-06-22 13:16:51 +01002388 struct snd_soc_dapm_widget *w;
2389 LIST_HEAD(down_list);
2390 int powerdown = 0;
2391
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002392 list_for_each_entry(w, &dapm->card->widgets, list) {
2393 if (w->dapm != dapm)
2394 continue;
Mark Brown51737472009-06-22 13:16:51 +01002395 if (w->power) {
2396 dapm_seq_insert(w, &down_list, dapm_down_seq);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002397 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002398 powerdown = 1;
2399 }
2400 }
2401
2402 /* If there were no widgets to power down we're already in
2403 * standby.
2404 */
2405 if (powerdown) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002406 snd_soc_dapm_set_bias_level(NULL, dapm, SND_SOC_BIAS_PREPARE);
2407 dapm_seq_run(dapm, &down_list, 0, dapm_down_seq);
2408 snd_soc_dapm_set_bias_level(NULL, dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01002409 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002410}
Mark Brown51737472009-06-22 13:16:51 +01002411
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002412/*
2413 * snd_soc_dapm_shutdown - callback for system shutdown
2414 */
2415void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2416{
2417 struct snd_soc_codec *codec;
2418
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002419 list_for_each_entry(codec, &card->codec_dev_list, list) {
2420 soc_dapm_shutdown_codec(&codec->dapm);
2421 snd_soc_dapm_set_bias_level(card, &codec->dapm, SND_SOC_BIAS_OFF);
2422 }
Mark Brown51737472009-06-22 13:16:51 +01002423}
2424
Richard Purdie2b97eab2006-10-06 18:32:18 +02002425/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002426MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002427MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2428MODULE_LICENSE("GPL");