blob: 05da8a8f0aef8f7bc66f77a57e74d5931764adf5 [file] [log] [blame]
Richard Purdie2b97eab2006-10-06 18:32:18 +02001/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020012 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
Mark Brown74b8f952009-06-06 11:26:15 +010015 * DACs/ADCs.
Richard Purdie2b97eab2006-10-06 18:32:18 +020016 * o Platform power domain - can support external components i.e. amps and
17 * mic/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +020021 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
24 * Todo:
25 * o DAPM power change sequencing - allow for configurable per
26 * codec sequences.
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
30 */
31
32#include <linux/module.h>
33#include <linux/moduleparam.h>
34#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080035#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020036#include <linux/delay.h>
37#include <linux/pm.h>
38#include <linux/bitops.h>
39#include <linux/platform_device.h>
40#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020041#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020043#include <sound/core.h>
44#include <sound/pcm.h>
45#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020046#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020047#include <sound/initval.h>
48
Mark Brown84e90932010-11-04 00:07:02 -040049#include <trace/events/asoc.h>
50
Richard Purdie2b97eab2006-10-06 18:32:18 +020051/* dapm power sequences - make this per codec in the future */
52static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010053 [snd_soc_dapm_pre] = 0,
54 [snd_soc_dapm_supply] = 1,
55 [snd_soc_dapm_micbias] = 2,
Mark Brown010ff262009-08-17 17:39:22 +010056 [snd_soc_dapm_aif_in] = 3,
57 [snd_soc_dapm_aif_out] = 3,
58 [snd_soc_dapm_mic] = 4,
59 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000060 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010061 [snd_soc_dapm_value_mux] = 5,
62 [snd_soc_dapm_dac] = 6,
63 [snd_soc_dapm_mixer] = 7,
64 [snd_soc_dapm_mixer_named_ctl] = 7,
65 [snd_soc_dapm_pga] = 8,
66 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060067 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010068 [snd_soc_dapm_hp] = 10,
69 [snd_soc_dapm_spk] = 10,
70 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020071};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000072
Richard Purdie2b97eab2006-10-06 18:32:18 +020073static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010074 [snd_soc_dapm_pre] = 0,
75 [snd_soc_dapm_adc] = 1,
76 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010077 [snd_soc_dapm_spk] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060078 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010079 [snd_soc_dapm_pga] = 4,
80 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010081 [snd_soc_dapm_mixer] = 5,
82 [snd_soc_dapm_dac] = 6,
83 [snd_soc_dapm_mic] = 7,
84 [snd_soc_dapm_micbias] = 8,
85 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000086 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010087 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010088 [snd_soc_dapm_aif_in] = 10,
89 [snd_soc_dapm_aif_out] = 10,
90 [snd_soc_dapm_supply] = 11,
91 [snd_soc_dapm_post] = 12,
Richard Purdie2b97eab2006-10-06 18:32:18 +020092};
93
Troy Kisky12ef1932008-10-13 17:42:14 -070094static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +010095{
96 if (pop_time)
97 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
98}
99
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200100static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100101{
102 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200103 char *buf;
104
105 if (!pop_time)
106 return;
107
108 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
109 if (buf == NULL)
110 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100111
112 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200113 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100114 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100115 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200116
117 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100118}
119
Richard Purdie2b97eab2006-10-06 18:32:18 +0200120/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100121static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200122 const struct snd_soc_dapm_widget *_widget)
123{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100124 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200125}
126
Mark Brown452c5ea2009-05-17 21:41:23 +0100127/**
128 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800129 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100130 * @level: level to configure
131 *
132 * Configure the bias (power) levels for the SoC audio device.
133 *
134 * Returns 0 for success else error.
135 */
Mark Browned5a4c42011-02-18 11:12:42 -0800136static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200137 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100138{
Mark Browned5a4c42011-02-18 11:12:42 -0800139 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100140 int ret = 0;
141
Mark Brownf83fba82009-05-18 15:44:43 +0100142 switch (level) {
143 case SND_SOC_BIAS_ON:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200144 dev_dbg(dapm->dev, "Setting full bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100145 break;
146 case SND_SOC_BIAS_PREPARE:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200147 dev_dbg(dapm->dev, "Setting bias prepare\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100148 break;
149 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200150 dev_dbg(dapm->dev, "Setting standby bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100151 break;
152 case SND_SOC_BIAS_OFF:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200153 dev_dbg(dapm->dev, "Setting bias off\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100154 break;
155 default:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200156 dev_err(dapm->dev, "Setting invalid bias %d\n", level);
Mark Brownf83fba82009-05-18 15:44:43 +0100157 return -EINVAL;
158 }
159
Mark Brown84e90932010-11-04 00:07:02 -0400160 trace_snd_soc_bias_level_start(card, level);
161
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000162 if (card && card->set_bias_level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100163 ret = card->set_bias_level(card, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100164 if (ret == 0) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200165 if (dapm->codec && dapm->codec->driver->set_bias_level)
166 ret = dapm->codec->driver->set_bias_level(dapm->codec, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100167 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200168 dapm->bias_level = level;
Mark Brown474e09c2009-08-19 14:18:53 +0100169 }
Mark Brown1badabd2010-12-04 12:41:04 +0000170 if (ret == 0) {
171 if (card && card->set_bias_level_post)
172 ret = card->set_bias_level_post(card, level);
173 }
Mark Brown452c5ea2009-05-17 21:41:23 +0100174
Mark Brown84e90932010-11-04 00:07:02 -0400175 trace_snd_soc_bias_level_done(card, level);
176
Mark Brown452c5ea2009-05-17 21:41:23 +0100177 return ret;
178}
179
Richard Purdie2b97eab2006-10-06 18:32:18 +0200180/* set up initial codec paths */
181static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
182 struct snd_soc_dapm_path *p, int i)
183{
184 switch (w->id) {
185 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000186 case snd_soc_dapm_mixer:
187 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200188 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100189 struct soc_mixer_control *mc = (struct soc_mixer_control *)
190 w->kcontrols[i].private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -0400191 unsigned int reg = mc->reg;
192 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100193 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -0400194 unsigned int mask = (1 << fls(max)) - 1;
195 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200196
197 val = snd_soc_read(w->codec, reg);
198 val = (val >> shift) & mask;
199
200 if ((invert && !val) || (!invert && val))
201 p->connect = 1;
202 else
203 p->connect = 0;
204 }
205 break;
206 case snd_soc_dapm_mux: {
207 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
208 int val, item, bitmask;
209
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +0100210 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200211 ;
212 val = snd_soc_read(w->codec, e->reg);
213 item = (val >> e->shift_l) & (bitmask - 1);
214
215 p->connect = 0;
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +0100216 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200217 if (!(strcmp(p->name, e->texts[i])) && item == i)
218 p->connect = 1;
219 }
220 }
221 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000222 case snd_soc_dapm_virt_mux: {
223 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
224
225 p->connect = 0;
226 /* since a virtual mux has no backing registers to
227 * decide which path to connect, it will try to match
228 * with the first enumeration. This is to ensure
229 * that the default mux choice (the first) will be
230 * correctly powered up during initialization.
231 */
232 if (!strcmp(p->name, e->texts[0]))
233 p->connect = 1;
234 }
235 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200236 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200237 struct soc_enum *e = (struct soc_enum *)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200238 w->kcontrols[i].private_value;
239 int val, item;
240
241 val = snd_soc_read(w->codec, e->reg);
242 val = (val >> e->shift_l) & e->mask;
243 for (item = 0; item < e->max; item++) {
244 if (val == e->values[item])
245 break;
246 }
247
248 p->connect = 0;
249 for (i = 0; i < e->max; i++) {
250 if (!(strcmp(p->name, e->texts[i])) && item == i)
251 p->connect = 1;
252 }
253 }
254 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200255 /* does not effect routing - always connected */
256 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600257 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200258 case snd_soc_dapm_output:
259 case snd_soc_dapm_adc:
260 case snd_soc_dapm_input:
261 case snd_soc_dapm_dac:
262 case snd_soc_dapm_micbias:
263 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100264 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100265 case snd_soc_dapm_aif_in:
266 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200267 p->connect = 1;
268 break;
269 /* does effect routing - dynamically connected */
270 case snd_soc_dapm_hp:
271 case snd_soc_dapm_mic:
272 case snd_soc_dapm_spk:
273 case snd_soc_dapm_line:
274 case snd_soc_dapm_pre:
275 case snd_soc_dapm_post:
276 p->connect = 0;
277 break;
278 }
279}
280
Mark Brown74b8f952009-06-06 11:26:15 +0100281/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200282static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200283 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
284 struct snd_soc_dapm_path *path, const char *control_name,
285 const struct snd_kcontrol_new *kcontrol)
286{
287 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
288 int i;
289
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +0100290 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200291 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200292 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200293 list_add(&path->list_sink, &dest->sources);
294 list_add(&path->list_source, &src->sinks);
295 path->name = (char*)e->texts[i];
296 dapm_set_path_status(dest, path, 0);
297 return 0;
298 }
299 }
300
301 return -ENODEV;
302}
303
Mark Brown74b8f952009-06-06 11:26:15 +0100304/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200305static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200306 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
307 struct snd_soc_dapm_path *path, const char *control_name)
308{
309 int i;
310
311 /* search for mixer kcontrol */
312 for (i = 0; i < dest->num_kcontrols; i++) {
313 if (!strcmp(control_name, dest->kcontrols[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200314 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200315 list_add(&path->list_sink, &dest->sources);
316 list_add(&path->list_source, &src->sinks);
317 path->name = dest->kcontrols[i].name;
318 dapm_set_path_status(dest, path, i);
319 return 0;
320 }
321 }
322 return -ENODEV;
323}
324
325/* update dapm codec register bits */
326static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
327{
328 int change, power;
Daniel Ribeiro46f58222009-06-07 02:49:11 -0300329 unsigned int old, new;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200330 struct snd_soc_codec *codec = widget->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200331 struct snd_soc_dapm_context *dapm = widget->dapm;
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200332 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200333
334 /* check for valid widgets */
335 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
336 widget->id == snd_soc_dapm_output ||
337 widget->id == snd_soc_dapm_hp ||
338 widget->id == snd_soc_dapm_mic ||
339 widget->id == snd_soc_dapm_line ||
340 widget->id == snd_soc_dapm_spk)
341 return 0;
342
343 power = widget->power;
344 if (widget->invert)
345 power = (power ? 0:1);
346
347 old = snd_soc_read(codec, widget->reg);
348 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
349
350 change = old != new;
351 if (change) {
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200352 pop_dbg(dapm->dev, card->pop_time,
353 "pop test %s : %s in %d ms\n",
Troy Kisky12ef1932008-10-13 17:42:14 -0700354 widget->name, widget->power ? "on" : "off",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200355 card->pop_time);
356 pop_wait(card->pop_time);
Mark Brown69224712010-03-03 14:57:09 +0000357 snd_soc_write(codec, widget->reg, new);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200358 }
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200359 dev_dbg(dapm->dev, "reg %x old %x new %x change %d\n", widget->reg,
360 old, new, change);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200361 return change;
362}
363
Richard Purdie2b97eab2006-10-06 18:32:18 +0200364/* create new dapm mixer control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200365static int dapm_new_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200366 struct snd_soc_dapm_widget *w)
367{
368 int i, ret = 0;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000369 size_t name_len, prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200370 struct snd_soc_dapm_path *path;
Mark Brown12ea2c72011-03-02 18:17:32 +0000371 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000372 const char *prefix;
373
374 if (dapm->codec)
375 prefix = dapm->codec->name_prefix;
376 else
377 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200378
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000379 if (prefix)
380 prefix_len = strlen(prefix) + 1;
381 else
382 prefix_len = 0;
383
Richard Purdie2b97eab2006-10-06 18:32:18 +0200384 /* add kcontrol */
385 for (i = 0; i < w->num_kcontrols; i++) {
386
387 /* match name */
388 list_for_each_entry(path, &w->sources, list_sink) {
389
390 /* mixer/mux paths name must match control name */
391 if (path->name != (char*)w->kcontrols[i].name)
392 continue;
393
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000394 /* add dapm control with long name.
395 * for dapm_mixer this is the concatenation of the
396 * mixer and kcontrol name.
397 * for dapm_mixer_named_ctl this is simply the
398 * kcontrol name.
399 */
400 name_len = strlen(w->kcontrols[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000401 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000402 name_len += 1 + strlen(w->name);
403
Mark Brown219b93f2008-10-28 13:02:31 +0000404 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000405
Richard Purdie2b97eab2006-10-06 18:32:18 +0200406 if (path->long_name == NULL)
407 return -ENOMEM;
408
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000409 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000410 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000411 /* The control will get a prefix from
412 * the control creation process but
413 * we're also using the same prefix
414 * for widgets so cut the prefix off
415 * the front of the widget name.
416 */
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000417 snprintf(path->long_name, name_len, "%s %s",
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000418 w->name + prefix_len,
419 w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000420 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000421 case snd_soc_dapm_mixer_named_ctl:
422 snprintf(path->long_name, name_len, "%s",
423 w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000424 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000425 }
426
Mark Brown219b93f2008-10-28 13:02:31 +0000427 path->long_name[name_len - 1] = '\0';
428
Richard Purdie2b97eab2006-10-06 18:32:18 +0200429 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
Mark Brownefb7ac32011-03-08 17:23:24 +0000430 path->long_name, prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200431 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200432 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200433 dev_err(dapm->dev,
434 "asoc: failed to add dapm kcontrol %s: %d\n",
435 path->long_name, ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200436 kfree(path->long_name);
437 path->long_name = NULL;
438 return ret;
439 }
440 }
441 }
442 return ret;
443}
444
445/* create new dapm mux control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200446static int dapm_new_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200447 struct snd_soc_dapm_widget *w)
448{
449 struct snd_soc_dapm_path *path = NULL;
450 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000451 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000452 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000453 size_t prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200454 int ret = 0;
455
456 if (!w->num_kcontrols) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200457 dev_err(dapm->dev, "asoc: mux %s has no controls\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200458 return -EINVAL;
459 }
460
Mark Brownefb7ac32011-03-08 17:23:24 +0000461 if (dapm->codec)
462 prefix = dapm->codec->name_prefix;
463 else
464 prefix = NULL;
465
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000466 if (prefix)
467 prefix_len = strlen(prefix) + 1;
468 else
469 prefix_len = 0;
470
471 /* The control will get a prefix from the control creation
472 * process but we're also using the same prefix for widgets so
473 * cut the prefix off the front of the widget name.
474 */
475 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name + prefix_len,
476 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200477 ret = snd_ctl_add(card, kcontrol);
478
Richard Purdie2b97eab2006-10-06 18:32:18 +0200479 if (ret < 0)
480 goto err;
481
482 list_for_each_entry(path, &w->sources, list_sink)
483 path->kcontrol = kcontrol;
484
485 return ret;
486
487err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200488 dev_err(dapm->dev, "asoc: failed to add kcontrol %s\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200489 return ret;
490}
491
492/* create new dapm volume control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200493static int dapm_new_pga(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200494 struct snd_soc_dapm_widget *w)
495{
Mark Browna6c65732010-03-03 17:45:21 +0000496 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200497 dev_err(w->dapm->dev,
498 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200499
Mark Browna6c65732010-03-03 17:45:21 +0000500 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200501}
502
503/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200504static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200505{
506 struct snd_soc_dapm_path *p;
507
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200508 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200509 p->walked = 0;
510}
511
Mark Brown99497882010-05-07 20:24:05 +0100512/* We implement power down on suspend by checking the power state of
513 * the ALSA card - when we are suspending the ALSA state for the card
514 * is set to D3.
515 */
516static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
517{
Mark Brown12ea2c72011-03-02 18:17:32 +0000518 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100519
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000520 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100521 case SNDRV_CTL_POWER_D3hot:
522 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100523 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200524 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
525 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100526 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100527 default:
528 return 1;
529 }
530}
531
Richard Purdie2b97eab2006-10-06 18:32:18 +0200532/*
533 * Recursively check for a completed path to an active or physically connected
534 * output widget. Returns number of complete paths.
535 */
536static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
537{
538 struct snd_soc_dapm_path *path;
539 int con = 0;
540
Mark Brown246d0a12009-04-22 18:24:55 +0100541 if (widget->id == snd_soc_dapm_supply)
542 return 0;
543
Mark Brown010ff262009-08-17 17:39:22 +0100544 switch (widget->id) {
545 case snd_soc_dapm_adc:
546 case snd_soc_dapm_aif_out:
547 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100548 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100549 default:
550 break;
551 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200552
553 if (widget->connected) {
554 /* connected pin ? */
555 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100556 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200557
558 /* connected jack or spk ? */
559 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300560 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown99497882010-05-07 20:24:05 +0100561 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200562 }
563
564 list_for_each_entry(path, &widget->sinks, list_source) {
565 if (path->walked)
566 continue;
567
568 if (path->sink && path->connect) {
569 path->walked = 1;
570 con += is_connected_output_ep(path->sink);
571 }
572 }
573
574 return con;
575}
576
577/*
578 * Recursively check for a completed path to an active or physically connected
579 * input widget. Returns number of complete paths.
580 */
581static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
582{
583 struct snd_soc_dapm_path *path;
584 int con = 0;
585
Mark Brown246d0a12009-04-22 18:24:55 +0100586 if (widget->id == snd_soc_dapm_supply)
587 return 0;
588
Richard Purdie2b97eab2006-10-06 18:32:18 +0200589 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100590 switch (widget->id) {
591 case snd_soc_dapm_dac:
592 case snd_soc_dapm_aif_in:
593 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100594 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100595 default:
596 break;
597 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200598
599 if (widget->connected) {
600 /* connected pin ? */
601 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100602 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200603
604 /* connected VMID/Bias for lower pops */
605 if (widget->id == snd_soc_dapm_vmid)
Mark Brown99497882010-05-07 20:24:05 +0100606 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200607
608 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300609 if (widget->id == snd_soc_dapm_mic ||
610 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown99497882010-05-07 20:24:05 +0100611 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200612 }
613
614 list_for_each_entry(path, &widget->sources, list_sink) {
615 if (path->walked)
616 continue;
617
618 if (path->source && path->connect) {
619 path->walked = 1;
620 con += is_connected_input_ep(path->source);
621 }
622 }
623
624 return con;
625}
626
627/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300628 * Handler for generic register modifier widget.
629 */
630int dapm_reg_event(struct snd_soc_dapm_widget *w,
631 struct snd_kcontrol *kcontrol, int event)
632{
633 unsigned int val;
634
635 if (SND_SOC_DAPM_EVENT_ON(event))
636 val = w->on_val;
637 else
638 val = w->off_val;
639
640 snd_soc_update_bits(w->codec, -(w->reg + 1),
641 w->mask << w->shift, val << w->shift);
642
643 return 0;
644}
Mark Brown11589412008-07-29 11:42:23 +0100645EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300646
Mark Brown025756e2009-04-13 11:09:18 +0100647/* Standard power change method, used to apply power changes to most
648 * widgets.
649 */
650static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
651{
652 int ret;
653
654 /* call any power change event handlers */
655 if (w->event)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200656 dev_dbg(w->dapm->dev, "power %s event for %s flags %x\n",
Mark Brown025756e2009-04-13 11:09:18 +0100657 w->power ? "on" : "off",
658 w->name, w->event_flags);
659
660 /* power up pre event */
661 if (w->power && w->event &&
662 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
663 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
664 if (ret < 0)
665 return ret;
666 }
667
668 /* power down pre event */
669 if (!w->power && w->event &&
670 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
671 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
672 if (ret < 0)
673 return ret;
674 }
675
Mark Brown025756e2009-04-13 11:09:18 +0100676 dapm_update_bits(w);
677
Mark Brown025756e2009-04-13 11:09:18 +0100678 /* power up post event */
679 if (w->power && w->event &&
680 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
681 ret = w->event(w,
682 NULL, SND_SOC_DAPM_POST_PMU);
683 if (ret < 0)
684 return ret;
685 }
686
687 /* power down post event */
688 if (!w->power && w->event &&
689 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
690 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
691 if (ret < 0)
692 return ret;
693 }
694
695 return 0;
696}
697
Mark Browncd0f2d42009-04-20 16:56:59 +0100698/* Generic check to see if a widget should be powered.
699 */
700static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
701{
702 int in, out;
703
704 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200705 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100706 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200707 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100708 return out != 0 && in != 0;
709}
710
Mark Brown6ea31b92009-04-20 17:15:41 +0100711/* Check to see if an ADC has power */
712static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
713{
714 int in;
715
716 if (w->active) {
717 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200718 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100719 return in != 0;
720 } else {
721 return dapm_generic_check_power(w);
722 }
723}
724
725/* Check to see if a DAC has power */
726static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
727{
728 int out;
729
730 if (w->active) {
731 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200732 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100733 return out != 0;
734 } else {
735 return dapm_generic_check_power(w);
736 }
737}
738
Mark Brown246d0a12009-04-22 18:24:55 +0100739/* Check to see if a power supply is needed */
740static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
741{
742 struct snd_soc_dapm_path *path;
743 int power = 0;
744
745 /* Check if one of our outputs is connected */
746 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +0100747 if (path->connected &&
748 !path->connected(path->source, path->sink))
749 continue;
750
Mark Brown30173582011-02-11 11:42:19 +0000751 if (!path->sink)
752 continue;
753
754 if (path->sink->force) {
755 power = 1;
756 break;
757 }
758
759 if (path->sink->power_check &&
Mark Brown246d0a12009-04-22 18:24:55 +0100760 path->sink->power_check(path->sink)) {
761 power = 1;
762 break;
763 }
764 }
765
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200766 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100767
768 return power;
769}
770
Mark Brown38357ab2009-06-06 19:03:23 +0100771static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
772 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +0000773 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +0000774{
Mark Brown828a8422011-01-15 13:14:30 +0000775 int *sort;
776
777 if (power_up)
778 sort = dapm_up_seq;
779 else
780 sort = dapm_down_seq;
781
Mark Brown38357ab2009-06-06 19:03:23 +0100782 if (sort[a->id] != sort[b->id])
783 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +0000784 if (a->subseq != b->subseq) {
785 if (power_up)
786 return a->subseq - b->subseq;
787 else
788 return b->subseq - a->subseq;
789 }
Mark Brownb22ead22009-06-07 12:51:26 +0100790 if (a->reg != b->reg)
791 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +0000792 if (a->dapm != b->dapm)
793 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +0000794
Mark Brown38357ab2009-06-06 19:03:23 +0100795 return 0;
796}
Mark Brown42aa3412009-03-01 19:21:10 +0000797
Mark Brown38357ab2009-06-06 19:03:23 +0100798/* Insert a widget in order into a DAPM power sequence. */
799static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
800 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +0000801 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +0100802{
803 struct snd_soc_dapm_widget *w;
804
805 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +0000806 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +0100807 list_add_tail(&new_widget->power_list, &w->power_list);
808 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000809 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100810
Mark Brown38357ab2009-06-06 19:03:23 +0100811 list_add_tail(&new_widget->power_list, list);
812}
Mark Brown42aa3412009-03-01 19:21:10 +0000813
Mark Brown68f89ad2010-11-03 23:51:49 -0400814static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
815 struct snd_soc_dapm_widget *w, int event)
816{
817 struct snd_soc_card *card = dapm->card;
818 const char *ev_name;
819 int power, ret;
820
821 switch (event) {
822 case SND_SOC_DAPM_PRE_PMU:
823 ev_name = "PRE_PMU";
824 power = 1;
825 break;
826 case SND_SOC_DAPM_POST_PMU:
827 ev_name = "POST_PMU";
828 power = 1;
829 break;
830 case SND_SOC_DAPM_PRE_PMD:
831 ev_name = "PRE_PMD";
832 power = 0;
833 break;
834 case SND_SOC_DAPM_POST_PMD:
835 ev_name = "POST_PMD";
836 power = 0;
837 break;
838 default:
839 BUG();
840 return;
841 }
842
843 if (w->power != power)
844 return;
845
846 if (w->event && (w->event_flags & event)) {
847 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
848 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -0400849 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400850 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -0400851 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400852 if (ret < 0)
853 pr_err("%s: %s event failed: %d\n",
854 ev_name, w->name, ret);
855 }
856}
857
Mark Brownb22ead22009-06-07 12:51:26 +0100858/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200859static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +0100860 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100861{
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200862 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -0400863 struct snd_soc_dapm_widget *w;
864 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +0100865 unsigned int value = 0;
866 unsigned int mask = 0;
867 unsigned int cur_mask;
868
869 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
870 power_list)->reg;
871
872 list_for_each_entry(w, pending, power_list) {
873 cur_mask = 1 << w->shift;
874 BUG_ON(reg != w->reg);
875
876 if (w->invert)
877 power = !w->power;
878 else
879 power = w->power;
880
881 mask |= cur_mask;
882 if (power)
883 value |= cur_mask;
884
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200885 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +0100886 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
887 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100888
Mark Brown68f89ad2010-11-03 23:51:49 -0400889 /* Check for events */
890 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
891 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100892 }
893
Mark Brown81628102009-06-07 13:21:24 +0100894 if (reg >= 0) {
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200895 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +0100896 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200897 value, mask, reg, card->pop_time);
898 pop_wait(card->pop_time);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200899 snd_soc_update_bits(dapm->codec, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +0100900 }
901
902 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -0400903 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
904 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +0000905 }
Mark Brown42aa3412009-03-01 19:21:10 +0000906}
907
Mark Brownb22ead22009-06-07 12:51:26 +0100908/* Apply a DAPM power sequence.
909 *
910 * We walk over a pre-sorted list of widgets to apply power to. In
911 * order to minimise the number of writes to the device required
912 * multiple widgets will be updated in a single write where possible.
913 * Currently anything that requires more than a single write is not
914 * handled.
915 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200916static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +0000917 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +0100918{
919 struct snd_soc_dapm_widget *w, *n;
920 LIST_HEAD(pending);
921 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +0000922 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +0100923 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200924 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +0000925 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +0000926 int *sort;
927
928 if (power_up)
929 sort = dapm_up_seq;
930 else
931 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +0100932
Mark Brownb22ead22009-06-07 12:51:26 +0100933 list_for_each_entry_safe(w, n, list, power_list) {
934 ret = 0;
935
936 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200937 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +0000938 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +0100939 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200940 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +0100941
Mark Brown474b62d2011-01-18 16:14:44 +0000942 if (cur_dapm && cur_dapm->seq_notifier) {
943 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
944 if (sort[i] == cur_sort)
945 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +0000946 i,
947 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +0000948 }
949
Mark Brownb22ead22009-06-07 12:51:26 +0100950 INIT_LIST_HEAD(&pending);
951 cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +0000952 cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +0100953 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200954 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +0100955 }
956
Mark Brown163cac02009-06-07 10:12:52 +0100957 switch (w->id) {
958 case snd_soc_dapm_pre:
959 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100960 list_for_each_entry_safe_continue(w, n, list,
961 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100962
Mark Brownb22ead22009-06-07 12:51:26 +0100963 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100964 ret = w->event(w,
965 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100966 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100967 ret = w->event(w,
968 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +0100969 break;
970
971 case snd_soc_dapm_post:
972 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100973 list_for_each_entry_safe_continue(w, n, list,
974 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100975
Mark Brownb22ead22009-06-07 12:51:26 +0100976 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100977 ret = w->event(w,
978 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100979 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100980 ret = w->event(w,
981 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100982 break;
983
984 case snd_soc_dapm_input:
985 case snd_soc_dapm_output:
986 case snd_soc_dapm_hp:
987 case snd_soc_dapm_mic:
988 case snd_soc_dapm_line:
989 case snd_soc_dapm_spk:
990 /* No register support currently */
Mark Brownb22ead22009-06-07 12:51:26 +0100991 ret = dapm_generic_apply_power(w);
Mark Brown163cac02009-06-07 10:12:52 +0100992 break;
993
994 default:
Mark Brown81628102009-06-07 13:21:24 +0100995 /* Queue it up for application */
996 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +0000997 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +0100998 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200999 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001000 list_move(&w->power_list, &pending);
1001 break;
Mark Brown163cac02009-06-07 10:12:52 +01001002 }
Mark Brownb22ead22009-06-07 12:51:26 +01001003
1004 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001005 dev_err(w->dapm->dev,
1006 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001007 }
Mark Brownb22ead22009-06-07 12:51:26 +01001008
1009 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001010 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001011
1012 if (cur_dapm && cur_dapm->seq_notifier) {
1013 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1014 if (sort[i] == cur_sort)
1015 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001016 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001017 }
Mark Brown163cac02009-06-07 10:12:52 +01001018}
1019
Mark Brown97404f22010-12-14 16:13:57 +00001020static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1021{
1022 struct snd_soc_dapm_update *update = dapm->update;
1023 struct snd_soc_dapm_widget *w;
1024 int ret;
1025
1026 if (!update)
1027 return;
1028
1029 w = update->widget;
1030
1031 if (w->event &&
1032 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1033 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1034 if (ret != 0)
1035 pr_err("%s DAPM pre-event failed: %d\n",
1036 w->name, ret);
1037 }
1038
1039 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1040 update->val);
1041 if (ret < 0)
1042 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1043
1044 if (w->event &&
1045 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1046 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1047 if (ret != 0)
1048 pr_err("%s DAPM post-event failed: %d\n",
1049 w->name, ret);
1050 }
1051}
1052
Mark Brown9d0624a2011-02-18 11:49:43 -08001053/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1054 * they're changing state.
1055 */
1056static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1057{
1058 struct snd_soc_dapm_context *d = data;
1059 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001060
Mark Brown9d0624a2011-02-18 11:49:43 -08001061 if (d->dev_power && d->bias_level == SND_SOC_BIAS_OFF) {
1062 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1063 if (ret != 0)
1064 dev_err(d->dev,
1065 "Failed to turn on bias: %d\n", ret);
1066 }
1067
1068 /* If we're changing to all on or all off then prepare */
1069 if ((d->dev_power && d->bias_level == SND_SOC_BIAS_STANDBY) ||
1070 (!d->dev_power && d->bias_level == SND_SOC_BIAS_ON)) {
1071 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1072 if (ret != 0)
1073 dev_err(d->dev,
1074 "Failed to prepare bias: %d\n", ret);
1075 }
1076}
1077
1078/* Async callback run prior to DAPM sequences - brings to their final
1079 * state.
1080 */
1081static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1082{
1083 struct snd_soc_dapm_context *d = data;
1084 int ret;
1085
1086 /* If we just powered the last thing off drop to standby bias */
1087 if (d->bias_level == SND_SOC_BIAS_PREPARE && !d->dev_power) {
1088 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1089 if (ret != 0)
1090 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1091 ret);
1092 }
1093
1094 /* If we're in standby and can support bias off then do that */
1095 if (d->bias_level == SND_SOC_BIAS_STANDBY && d->idle_bias_off) {
1096 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1097 if (ret != 0)
1098 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1099 }
1100
1101 /* If we just powered up then move to active bias */
1102 if (d->bias_level == SND_SOC_BIAS_PREPARE && d->dev_power) {
1103 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1104 if (ret != 0)
1105 dev_err(d->dev, "Failed to apply active bias: %d\n",
1106 ret);
1107 }
1108}
Mark Brown97404f22010-12-14 16:13:57 +00001109
Mark Brown42aa3412009-03-01 19:21:10 +00001110/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001111 * Scan each dapm widget for complete audio path.
1112 * A complete path is a route that has valid endpoints i.e.:-
1113 *
1114 * o DAC to output pin.
1115 * o Input Pin to ADC.
1116 * o Input pin to Output pin (bypass, sidetone)
1117 * o DAC to ADC (loopback).
1118 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001119static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001120{
Mark Brown12ea2c72011-03-02 18:17:32 +00001121 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001122 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001123 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001124 LIST_HEAD(up_list);
1125 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001126 LIST_HEAD(async_domain);
Mark Brown38357ab2009-06-06 19:03:23 +01001127 int power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001128
Mark Brown84e90932010-11-04 00:07:02 -04001129 trace_snd_soc_dapm_start(card);
1130
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001131 list_for_each_entry(d, &card->dapm_list, list)
1132 if (d->n_widgets)
1133 d->dev_power = 0;
1134
Mark Brown6d3ddc82009-05-16 17:47:29 +01001135 /* Check which widgets we need to power and store them in
1136 * lists indicating if they should be powered up or down.
1137 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001138 list_for_each_entry(w, &card->widgets, list) {
Mark Brown6d3ddc82009-05-16 17:47:29 +01001139 switch (w->id) {
1140 case snd_soc_dapm_pre:
Mark Brown828a8422011-01-15 13:14:30 +00001141 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001142 break;
1143 case snd_soc_dapm_post:
Mark Brown828a8422011-01-15 13:14:30 +00001144 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001145 break;
1146
1147 default:
1148 if (!w->power_check)
1149 continue;
1150
Mark Brown99497882010-05-07 20:24:05 +01001151 if (!w->force)
Mark Brown50b6bce2009-11-23 13:11:53 +00001152 power = w->power_check(w);
Mark Brown99497882010-05-07 20:24:05 +01001153 else
1154 power = 1;
1155 if (power)
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001156 w->dapm->dev_power = 1;
Mark Brown452c5ea2009-05-17 21:41:23 +01001157
Mark Brown6d3ddc82009-05-16 17:47:29 +01001158 if (w->power == power)
1159 continue;
1160
Mark Brown84e90932010-11-04 00:07:02 -04001161 trace_snd_soc_dapm_widget_power(w, power);
1162
Mark Brown6d3ddc82009-05-16 17:47:29 +01001163 if (power)
Mark Brown828a8422011-01-15 13:14:30 +00001164 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001165 else
Mark Brown828a8422011-01-15 13:14:30 +00001166 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001167
1168 w->power = power;
1169 break;
1170 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001171 }
1172
Mark Brownb14b76a2009-08-17 11:55:38 +01001173 /* If there are no DAPM widgets then try to figure out power from the
1174 * event type.
1175 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001176 if (!dapm->n_widgets) {
Mark Brownb14b76a2009-08-17 11:55:38 +01001177 switch (event) {
1178 case SND_SOC_DAPM_STREAM_START:
1179 case SND_SOC_DAPM_STREAM_RESUME:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001180 dapm->dev_power = 1;
Mark Brownb14b76a2009-08-17 11:55:38 +01001181 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001182 case SND_SOC_DAPM_STREAM_STOP:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001183 dapm->dev_power = !!dapm->codec->active;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001184 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001185 case SND_SOC_DAPM_STREAM_SUSPEND:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001186 dapm->dev_power = 0;
Mark Brown50b6bce2009-11-23 13:11:53 +00001187 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001188 case SND_SOC_DAPM_STREAM_NOP:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001189 switch (dapm->bias_level) {
Mark Browna96ca332010-01-19 22:49:43 +00001190 case SND_SOC_BIAS_STANDBY:
1191 case SND_SOC_BIAS_OFF:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001192 dapm->dev_power = 0;
Mark Browna96ca332010-01-19 22:49:43 +00001193 break;
1194 default:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001195 dapm->dev_power = 1;
Mark Browna96ca332010-01-19 22:49:43 +00001196 break;
1197 }
Mark Brown50b6bce2009-11-23 13:11:53 +00001198 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001199 default:
1200 break;
1201 }
1202 }
1203
Mark Brown9d0624a2011-02-18 11:49:43 -08001204 /* Run all the bias changes in parallel */
1205 list_for_each_entry(d, &dapm->card->dapm_list, list)
1206 async_schedule_domain(dapm_pre_sequence_async, d,
1207 &async_domain);
1208 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001209
Mark Brown6d3ddc82009-05-16 17:47:29 +01001210 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001211 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001212
Mark Brown97404f22010-12-14 16:13:57 +00001213 dapm_widget_update(dapm);
1214
Mark Brown6d3ddc82009-05-16 17:47:29 +01001215 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001216 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001217
Mark Brown9d0624a2011-02-18 11:49:43 -08001218 /* Run all the bias changes in parallel */
1219 list_for_each_entry(d, &dapm->card->dapm_list, list)
1220 async_schedule_domain(dapm_post_sequence_async, d,
1221 &async_domain);
1222 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001223
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001224 pop_dbg(dapm->dev, card->pop_time,
1225 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001226 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001227
Mark Brown84e90932010-11-04 00:07:02 -04001228 trace_snd_soc_dapm_done(card);
1229
Mark Brown42aa3412009-03-01 19:21:10 +00001230 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001231}
1232
Mark Brown79fb9382009-08-21 16:38:13 +01001233#ifdef CONFIG_DEBUG_FS
1234static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1235{
1236 file->private_data = inode->i_private;
1237 return 0;
1238}
1239
1240static ssize_t dapm_widget_power_read_file(struct file *file,
1241 char __user *user_buf,
1242 size_t count, loff_t *ppos)
1243{
1244 struct snd_soc_dapm_widget *w = file->private_data;
1245 char *buf;
1246 int in, out;
1247 ssize_t ret;
1248 struct snd_soc_dapm_path *p = NULL;
1249
1250 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1251 if (!buf)
1252 return -ENOMEM;
1253
1254 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001255 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001256 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001257 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001258
Mark Brownd033c362009-12-04 15:25:56 +00001259 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001260 w->name, w->power ? "On" : "Off", in, out);
1261
Mark Brownd033c362009-12-04 15:25:56 +00001262 if (w->reg >= 0)
1263 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1264 " - R%d(0x%x) bit %d",
1265 w->reg, w->reg, w->shift);
1266
1267 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1268
Mark Brown3eef08b2009-09-14 16:49:00 +01001269 if (w->sname)
1270 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1271 w->sname,
1272 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001273
1274 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001275 if (p->connected && !p->connected(w, p->sink))
1276 continue;
1277
Mark Brown79fb9382009-08-21 16:38:13 +01001278 if (p->connect)
1279 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001280 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001281 p->name ? p->name : "static",
1282 p->source->name);
1283 }
1284 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001285 if (p->connected && !p->connected(w, p->sink))
1286 continue;
1287
Mark Brown79fb9382009-08-21 16:38:13 +01001288 if (p->connect)
1289 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001290 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001291 p->name ? p->name : "static",
1292 p->sink->name);
1293 }
1294
1295 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1296
1297 kfree(buf);
1298 return ret;
1299}
1300
1301static const struct file_operations dapm_widget_power_fops = {
1302 .open = dapm_widget_power_open_file,
1303 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001304 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001305};
1306
Mark Brownef49e4f2011-04-04 20:48:13 +09001307static int dapm_bias_open_file(struct inode *inode, struct file *file)
1308{
1309 file->private_data = inode->i_private;
1310 return 0;
1311}
1312
1313static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1314 size_t count, loff_t *ppos)
1315{
1316 struct snd_soc_dapm_context *dapm = file->private_data;
1317 char *level;
1318
1319 switch (dapm->bias_level) {
1320 case SND_SOC_BIAS_ON:
1321 level = "On\n";
1322 break;
1323 case SND_SOC_BIAS_PREPARE:
1324 level = "Prepare\n";
1325 break;
1326 case SND_SOC_BIAS_STANDBY:
1327 level = "Standby\n";
1328 break;
1329 case SND_SOC_BIAS_OFF:
1330 level = "Off\n";
1331 break;
1332 default:
1333 BUG();
1334 level = "Unknown\n";
1335 break;
1336 }
1337
1338 return simple_read_from_buffer(user_buf, count, ppos, level,
1339 strlen(level));
1340}
1341
1342static const struct file_operations dapm_bias_fops = {
1343 .open = dapm_bias_open_file,
1344 .read = dapm_bias_read_file,
1345 .llseek = default_llseek,
1346};
1347
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001348void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001349{
1350 struct snd_soc_dapm_widget *w;
1351 struct dentry *d;
1352
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001353 if (!dapm->debugfs_dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001354 return;
1355
Mark Brownef49e4f2011-04-04 20:48:13 +09001356 d = debugfs_create_file("bias_level", 0444,
1357 dapm->debugfs_dapm, dapm,
1358 &dapm_bias_fops);
1359 if (!d)
1360 dev_warn(dapm->dev,
1361 "ASoC: Failed to create bias level debugfs file\n");
1362
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001363 list_for_each_entry(w, &dapm->card->widgets, list) {
1364 if (!w->name || w->dapm != dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001365 continue;
1366
1367 d = debugfs_create_file(w->name, 0444,
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001368 dapm->debugfs_dapm, w,
Mark Brown79fb9382009-08-21 16:38:13 +01001369 &dapm_widget_power_fops);
1370 if (!d)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001371 dev_warn(w->dapm->dev,
1372 "ASoC: Failed to create %s debugfs file\n",
1373 w->name);
Mark Brown79fb9382009-08-21 16:38:13 +01001374 }
1375}
1376#else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001377void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001378{
1379}
1380#endif
1381
Richard Purdie2b97eab2006-10-06 18:32:18 +02001382/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001383static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001384 struct snd_kcontrol *kcontrol, int change,
1385 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001386{
1387 struct snd_soc_dapm_path *path;
1388 int found = 0;
1389
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001390 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001391 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001392 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001393 return -ENODEV;
1394
Mark Brown3a655772009-10-05 17:23:30 +01001395 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001396 return 0;
1397
1398 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001399 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001400 if (path->kcontrol != kcontrol)
1401 continue;
1402
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001403 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001404 continue;
1405
1406 found = 1;
1407 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001408 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001409 path->connect = 1; /* new connection */
1410 else
1411 path->connect = 0; /* old connection must be powered down */
1412 }
1413
Mark Brownb91b8fa2010-01-20 18:18:35 +00001414 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001415 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001416
1417 return 0;
1418}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001419
Milan plzik1b075e32008-01-10 14:39:46 +01001420/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001421static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001422 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001423{
1424 struct snd_soc_dapm_path *path;
1425 int found = 0;
1426
Milan plzik1b075e32008-01-10 14:39:46 +01001427 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001428 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001429 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001430 return -ENODEV;
1431
Richard Purdie2b97eab2006-10-06 18:32:18 +02001432 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001433 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001434 if (path->kcontrol != kcontrol)
1435 continue;
1436
1437 /* found, now check type */
1438 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001439 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001440 break;
1441 }
1442
Mark Brownb91b8fa2010-01-20 18:18:35 +00001443 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001444 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001445
1446 return 0;
1447}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001448
1449/* show dapm widget status in sys fs */
1450static ssize_t dapm_widget_show(struct device *dev,
1451 struct device_attribute *attr, char *buf)
1452{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001453 struct snd_soc_pcm_runtime *rtd =
1454 container_of(dev, struct snd_soc_pcm_runtime, dev);
1455 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001456 struct snd_soc_dapm_widget *w;
1457 int count = 0;
1458 char *state = "not set";
1459
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001460 list_for_each_entry(w, &codec->card->widgets, list) {
1461 if (w->dapm != &codec->dapm)
1462 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001463
1464 /* only display widgets that burnm power */
1465 switch (w->id) {
1466 case snd_soc_dapm_hp:
1467 case snd_soc_dapm_mic:
1468 case snd_soc_dapm_spk:
1469 case snd_soc_dapm_line:
1470 case snd_soc_dapm_micbias:
1471 case snd_soc_dapm_dac:
1472 case snd_soc_dapm_adc:
1473 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001474 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001475 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001476 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001477 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001478 if (w->name)
1479 count += sprintf(buf + count, "%s: %s\n",
1480 w->name, w->power ? "On":"Off");
1481 break;
1482 default:
1483 break;
1484 }
1485 }
1486
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001487 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001488 case SND_SOC_BIAS_ON:
1489 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001490 break;
Mark Brown0be98982008-05-19 12:31:28 +02001491 case SND_SOC_BIAS_PREPARE:
1492 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001493 break;
Mark Brown0be98982008-05-19 12:31:28 +02001494 case SND_SOC_BIAS_STANDBY:
1495 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001496 break;
Mark Brown0be98982008-05-19 12:31:28 +02001497 case SND_SOC_BIAS_OFF:
1498 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001499 break;
1500 }
1501 count += sprintf(buf + count, "PM State: %s\n", state);
1502
1503 return count;
1504}
1505
1506static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1507
1508int snd_soc_dapm_sys_add(struct device *dev)
1509{
Troy Kisky12ef1932008-10-13 17:42:14 -07001510 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001511}
1512
1513static void snd_soc_dapm_sys_remove(struct device *dev)
1514{
Mark Brownaef90842009-05-16 17:53:16 +01001515 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001516}
1517
1518/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001519static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001520{
1521 struct snd_soc_dapm_widget *w, *next_w;
1522 struct snd_soc_dapm_path *p, *next_p;
1523
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001524 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1525 if (w->dapm != dapm)
1526 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001527 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001528 /*
1529 * remove source and sink paths associated to this widget.
1530 * While removing the path, remove reference to it from both
1531 * source and sink widgets so that path is removed only once.
1532 */
1533 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1534 list_del(&p->list_sink);
1535 list_del(&p->list_source);
1536 list_del(&p->list);
1537 kfree(p->long_name);
1538 kfree(p);
1539 }
1540 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1541 list_del(&p->list_sink);
1542 list_del(&p->list_source);
1543 list_del(&p->list);
1544 kfree(p->long_name);
1545 kfree(p);
1546 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001547 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001548 kfree(w);
1549 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001550}
1551
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001552static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001553 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001554{
1555 struct snd_soc_dapm_widget *w;
1556
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001557 list_for_each_entry(w, &dapm->card->widgets, list) {
1558 if (w->dapm != dapm)
1559 continue;
Liam Girdwooda5302182008-07-07 13:35:17 +01001560 if (!strcmp(w->name, pin)) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001561 dev_dbg(w->dapm->dev, "dapm: pin %s = %d\n",
1562 pin, status);
Liam Girdwooda5302182008-07-07 13:35:17 +01001563 w->connected = status;
Mark Brown5b9e87c2010-03-22 13:36:13 +00001564 /* Allow disabling of forced pins */
1565 if (status == 0)
1566 w->force = 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001567 return 0;
1568 }
1569 }
1570
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001571 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001572 return -EINVAL;
1573}
1574
Richard Purdie2b97eab2006-10-06 18:32:18 +02001575/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001576 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001577 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001578 *
1579 * Walks all dapm audio paths and powers widgets according to their
1580 * stream or path usage.
1581 *
1582 * Returns 0 for success.
1583 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001584int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001585{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001586 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001587}
Liam Girdwooda5302182008-07-07 13:35:17 +01001588EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001589
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001590static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001591 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001592{
1593 struct snd_soc_dapm_path *path;
1594 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001595 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001596 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001597 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001598 const char *source;
1599 char prefixed_sink[80];
1600 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001601 int ret = 0;
1602
Mark Brown88e8b9a2011-03-02 18:18:24 +00001603 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001604 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1605 dapm->codec->name_prefix, route->sink);
1606 sink = prefixed_sink;
1607 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1608 dapm->codec->name_prefix, route->source);
1609 source = prefixed_source;
1610 } else {
1611 sink = route->sink;
1612 source = route->source;
1613 }
1614
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001615 /*
1616 * find src and dest widgets over all widgets but favor a widget from
1617 * current DAPM context
1618 */
1619 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001620 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001621 wtsink = w;
1622 if (w->dapm == dapm)
1623 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001624 continue;
1625 }
1626 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001627 wtsource = w;
1628 if (w->dapm == dapm)
1629 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001630 }
1631 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001632 /* use widget from another DAPM context if not found from this */
1633 if (!wsink)
1634 wsink = wtsink;
1635 if (!wsource)
1636 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001637
1638 if (wsource == NULL || wsink == NULL)
1639 return -ENODEV;
1640
1641 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1642 if (!path)
1643 return -ENOMEM;
1644
1645 path->source = wsource;
1646 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001647 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001648 INIT_LIST_HEAD(&path->list);
1649 INIT_LIST_HEAD(&path->list_source);
1650 INIT_LIST_HEAD(&path->list_sink);
1651
1652 /* check for external widgets */
1653 if (wsink->id == snd_soc_dapm_input) {
1654 if (wsource->id == snd_soc_dapm_micbias ||
1655 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001656 wsource->id == snd_soc_dapm_line ||
1657 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001658 wsink->ext = 1;
1659 }
1660 if (wsource->id == snd_soc_dapm_output) {
1661 if (wsink->id == snd_soc_dapm_spk ||
1662 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001663 wsink->id == snd_soc_dapm_line ||
1664 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001665 wsource->ext = 1;
1666 }
1667
1668 /* connect static paths */
1669 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001670 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001671 list_add(&path->list_sink, &wsink->sources);
1672 list_add(&path->list_source, &wsource->sinks);
1673 path->connect = 1;
1674 return 0;
1675 }
1676
1677 /* connect dynamic paths */
1678 switch(wsink->id) {
1679 case snd_soc_dapm_adc:
1680 case snd_soc_dapm_dac:
1681 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001682 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001683 case snd_soc_dapm_input:
1684 case snd_soc_dapm_output:
1685 case snd_soc_dapm_micbias:
1686 case snd_soc_dapm_vmid:
1687 case snd_soc_dapm_pre:
1688 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001689 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001690 case snd_soc_dapm_aif_in:
1691 case snd_soc_dapm_aif_out:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001692 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001693 list_add(&path->list_sink, &wsink->sources);
1694 list_add(&path->list_source, &wsource->sinks);
1695 path->connect = 1;
1696 return 0;
1697 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001698 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001699 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001700 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Richard Purdie2b97eab2006-10-06 18:32:18 +02001701 &wsink->kcontrols[0]);
1702 if (ret != 0)
1703 goto err;
1704 break;
1705 case snd_soc_dapm_switch:
1706 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001707 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001708 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001709 if (ret != 0)
1710 goto err;
1711 break;
1712 case snd_soc_dapm_hp:
1713 case snd_soc_dapm_mic:
1714 case snd_soc_dapm_line:
1715 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001716 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001717 list_add(&path->list_sink, &wsink->sources);
1718 list_add(&path->list_source, &wsource->sinks);
1719 path->connect = 0;
1720 return 0;
1721 }
1722 return 0;
1723
1724err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001725 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1726 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001727 kfree(path);
1728 return ret;
1729}
Mark Brown105f1c22008-05-13 14:52:19 +02001730
1731/**
Mark Brown105f1c22008-05-13 14:52:19 +02001732 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001733 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02001734 * @route: audio routes
1735 * @num: number of routes
1736 *
1737 * Connects 2 dapm widgets together via a named audio path. The sink is
1738 * the widget receiving the audio signal, whilst the source is the sender
1739 * of the audio signal.
1740 *
1741 * Returns 0 for success else error. On error all resources can be freed
1742 * with a call to snd_soc_card_free().
1743 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001744int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02001745 const struct snd_soc_dapm_route *route, int num)
1746{
1747 int i, ret;
1748
1749 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001750 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001751 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001752 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1753 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02001754 return ret;
1755 }
1756 route++;
1757 }
1758
1759 return 0;
1760}
1761EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1762
1763/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001764 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001765 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001766 *
1767 * Checks the codec for any new dapm widgets and creates them if found.
1768 *
1769 * Returns 0 for success.
1770 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001771int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001772{
1773 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00001774 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001775
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001776 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001777 {
1778 if (w->new)
1779 continue;
1780
1781 switch(w->id) {
1782 case snd_soc_dapm_switch:
1783 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001784 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001785 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001786 dapm_new_mixer(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001787 break;
1788 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001789 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001790 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001791 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001792 dapm_new_mux(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001793 break;
1794 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01001795 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01001796 w->power_check = dapm_adc_check_power;
1797 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001798 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01001799 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01001800 w->power_check = dapm_dac_check_power;
1801 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001802 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001803 case snd_soc_dapm_out_drv:
Mark Brownb75576d2009-04-20 17:56:13 +01001804 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001805 dapm_new_pga(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001806 break;
1807 case snd_soc_dapm_input:
1808 case snd_soc_dapm_output:
1809 case snd_soc_dapm_micbias:
1810 case snd_soc_dapm_spk:
1811 case snd_soc_dapm_hp:
1812 case snd_soc_dapm_mic:
1813 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001814 w->power_check = dapm_generic_check_power;
1815 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001816 case snd_soc_dapm_supply:
1817 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001818 case snd_soc_dapm_vmid:
1819 case snd_soc_dapm_pre:
1820 case snd_soc_dapm_post:
1821 break;
1822 }
Mark Brownb66a70d2011-02-09 18:04:11 +00001823
1824 /* Read the initial power state from the device */
1825 if (w->reg >= 0) {
1826 val = snd_soc_read(w->codec, w->reg);
1827 val &= 1 << w->shift;
1828 if (w->invert)
1829 val = !val;
1830
1831 if (val)
1832 w->power = 1;
1833 }
1834
Richard Purdie2b97eab2006-10-06 18:32:18 +02001835 w->new = 1;
1836 }
1837
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001838 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001839 return 0;
1840}
1841EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1842
1843/**
1844 * snd_soc_dapm_get_volsw - dapm mixer get callback
1845 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001846 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001847 *
1848 * Callback to get the value of a dapm mixer control.
1849 *
1850 * Returns 0 for success.
1851 */
1852int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1853 struct snd_ctl_elem_value *ucontrol)
1854{
1855 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001856 struct soc_mixer_control *mc =
1857 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04001858 unsigned int reg = mc->reg;
1859 unsigned int shift = mc->shift;
1860 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001861 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001862 unsigned int invert = mc->invert;
1863 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001864
Richard Purdie2b97eab2006-10-06 18:32:18 +02001865 ucontrol->value.integer.value[0] =
1866 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1867 if (shift != rshift)
1868 ucontrol->value.integer.value[1] =
1869 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1870 if (invert) {
1871 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001872 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001873 if (shift != rshift)
1874 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001875 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001876 }
1877
1878 return 0;
1879}
1880EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1881
1882/**
1883 * snd_soc_dapm_put_volsw - dapm mixer set callback
1884 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001885 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001886 *
1887 * Callback to set the value of a dapm mixer control.
1888 *
1889 * Returns 0 for success.
1890 */
1891int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1892 struct snd_ctl_elem_value *ucontrol)
1893{
1894 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001895 struct soc_mixer_control *mc =
1896 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04001897 unsigned int reg = mc->reg;
1898 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001899 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001900 unsigned int mask = (1 << fls(max)) - 1;
1901 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07001902 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00001903 int connect, change;
1904 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001905
1906 val = (ucontrol->value.integer.value[0] & mask);
1907
1908 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001909 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07001910 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001911 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001912
1913 mutex_lock(&widget->codec->mutex);
1914 widget->value = val;
1915
Stephen Warrene9cf7042011-01-27 14:54:05 -07001916 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00001917 if (change) {
Mark Brown283375c2009-12-07 18:09:03 +00001918 if (val)
1919 /* new connection */
1920 connect = invert ? 0:1;
1921 else
1922 /* old connection must be powered down */
1923 connect = invert ? 1:0;
1924
Mark Brown97404f22010-12-14 16:13:57 +00001925 update.kcontrol = kcontrol;
1926 update.widget = widget;
1927 update.reg = reg;
1928 update.mask = mask;
1929 update.val = val;
1930 widget->dapm->update = &update;
1931
Mark Brown283375c2009-12-07 18:09:03 +00001932 dapm_mixer_update_power(widget, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00001933
1934 widget->dapm->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00001935 }
1936
Richard Purdie2b97eab2006-10-06 18:32:18 +02001937 mutex_unlock(&widget->codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00001938 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001939}
1940EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1941
1942/**
1943 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1944 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001945 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001946 *
1947 * Callback to get the value of a dapm enumerated double mixer control.
1948 *
1949 * Returns 0 for success.
1950 */
1951int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1952 struct snd_ctl_elem_value *ucontrol)
1953{
1954 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1955 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001956 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001957
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +01001958 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001959 ;
1960 val = snd_soc_read(widget->codec, e->reg);
1961 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1962 if (e->shift_l != e->shift_r)
1963 ucontrol->value.enumerated.item[1] =
1964 (val >> e->shift_r) & (bitmask - 1);
1965
1966 return 0;
1967}
1968EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1969
1970/**
1971 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1972 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001973 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001974 *
1975 * Callback to set the value of a dapm enumerated double mixer control.
1976 *
1977 * Returns 0 for success.
1978 */
1979int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1980 struct snd_ctl_elem_value *ucontrol)
1981{
1982 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1983 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001984 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001985 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00001986 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001987
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +01001988 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001989 ;
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +01001990 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001991 return -EINVAL;
1992 mux = ucontrol->value.enumerated.item[0];
1993 val = mux << e->shift_l;
1994 mask = (bitmask - 1) << e->shift_l;
1995 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +01001996 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001997 return -EINVAL;
1998 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1999 mask |= (bitmask - 1) << e->shift_r;
2000 }
2001
2002 mutex_lock(&widget->codec->mutex);
2003 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01002004 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002005
2006 update.kcontrol = kcontrol;
2007 update.widget = widget;
2008 update.reg = e->reg;
2009 update.mask = mask;
2010 update.val = val;
2011 widget->dapm->update = &update;
2012
Mark Brown3a655772009-10-05 17:23:30 +01002013 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002014
Mark Brown97404f22010-12-14 16:13:57 +00002015 widget->dapm->update = NULL;
Mark Brown1642e3d2009-10-05 16:24:26 +01002016
Richard Purdie2b97eab2006-10-06 18:32:18 +02002017 mutex_unlock(&widget->codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002018 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002019}
2020EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2021
2022/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002023 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2024 * @kcontrol: mixer control
2025 * @ucontrol: control element information
2026 *
2027 * Returns 0 for success.
2028 */
2029int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2030 struct snd_ctl_elem_value *ucontrol)
2031{
2032 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
2033
2034 ucontrol->value.enumerated.item[0] = widget->value;
2035
2036 return 0;
2037}
2038EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2039
2040/**
2041 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2042 * @kcontrol: mixer control
2043 * @ucontrol: control element information
2044 *
2045 * Returns 0 for success.
2046 */
2047int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2048 struct snd_ctl_elem_value *ucontrol)
2049{
2050 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
2051 struct soc_enum *e =
2052 (struct soc_enum *)kcontrol->private_value;
2053 int change;
2054 int ret = 0;
2055
2056 if (ucontrol->value.enumerated.item[0] >= e->max)
2057 return -EINVAL;
2058
2059 mutex_lock(&widget->codec->mutex);
2060
2061 change = widget->value != ucontrol->value.enumerated.item[0];
2062 widget->value = ucontrol->value.enumerated.item[0];
2063 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
2064
2065 mutex_unlock(&widget->codec->mutex);
2066 return ret;
2067}
2068EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2069
2070/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002071 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2072 * callback
2073 * @kcontrol: mixer control
2074 * @ucontrol: control element information
2075 *
2076 * Callback to get the value of a dapm semi enumerated double mixer control.
2077 *
2078 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2079 * used for handling bitfield coded enumeration for example.
2080 *
2081 * Returns 0 for success.
2082 */
2083int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2084 struct snd_ctl_elem_value *ucontrol)
2085{
2086 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002087 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002088 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002089
2090 reg_val = snd_soc_read(widget->codec, e->reg);
2091 val = (reg_val >> e->shift_l) & e->mask;
2092 for (mux = 0; mux < e->max; mux++) {
2093 if (val == e->values[mux])
2094 break;
2095 }
2096 ucontrol->value.enumerated.item[0] = mux;
2097 if (e->shift_l != e->shift_r) {
2098 val = (reg_val >> e->shift_r) & e->mask;
2099 for (mux = 0; mux < e->max; mux++) {
2100 if (val == e->values[mux])
2101 break;
2102 }
2103 ucontrol->value.enumerated.item[1] = mux;
2104 }
2105
2106 return 0;
2107}
2108EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2109
2110/**
2111 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2112 * callback
2113 * @kcontrol: mixer control
2114 * @ucontrol: control element information
2115 *
2116 * Callback to set the value of a dapm semi enumerated double mixer control.
2117 *
2118 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2119 * used for handling bitfield coded enumeration for example.
2120 *
2121 * Returns 0 for success.
2122 */
2123int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2124 struct snd_ctl_elem_value *ucontrol)
2125{
2126 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002127 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002128 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002129 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002130 struct snd_soc_dapm_update update;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002131
2132 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2133 return -EINVAL;
2134 mux = ucontrol->value.enumerated.item[0];
2135 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2136 mask = e->mask << e->shift_l;
2137 if (e->shift_l != e->shift_r) {
2138 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2139 return -EINVAL;
2140 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2141 mask |= e->mask << e->shift_r;
2142 }
2143
2144 mutex_lock(&widget->codec->mutex);
2145 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01002146 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002147
2148 update.kcontrol = kcontrol;
2149 update.widget = widget;
2150 update.reg = e->reg;
2151 update.mask = mask;
2152 update.val = val;
2153 widget->dapm->update = &update;
2154
Mark Brown3a655772009-10-05 17:23:30 +01002155 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002156
Mark Brown97404f22010-12-14 16:13:57 +00002157 widget->dapm->update = NULL;
Mark Brown1642e3d2009-10-05 16:24:26 +01002158
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002159 mutex_unlock(&widget->codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002160 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002161}
2162EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2163
2164/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002165 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2166 *
2167 * @kcontrol: mixer control
2168 * @uinfo: control element information
2169 *
2170 * Callback to provide information about a pin switch control.
2171 */
2172int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2173 struct snd_ctl_elem_info *uinfo)
2174{
2175 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2176 uinfo->count = 1;
2177 uinfo->value.integer.min = 0;
2178 uinfo->value.integer.max = 1;
2179
2180 return 0;
2181}
2182EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2183
2184/**
2185 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2186 *
2187 * @kcontrol: mixer control
2188 * @ucontrol: Value
2189 */
2190int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2191 struct snd_ctl_elem_value *ucontrol)
2192{
2193 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2194 const char *pin = (const char *)kcontrol->private_value;
2195
2196 mutex_lock(&codec->mutex);
2197
2198 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002199 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002200
2201 mutex_unlock(&codec->mutex);
2202
2203 return 0;
2204}
2205EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2206
2207/**
2208 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2209 *
2210 * @kcontrol: mixer control
2211 * @ucontrol: Value
2212 */
2213int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2214 struct snd_ctl_elem_value *ucontrol)
2215{
2216 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2217 const char *pin = (const char *)kcontrol->private_value;
2218
2219 mutex_lock(&codec->mutex);
2220
2221 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002222 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002223 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002224 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002225
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002226 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002227
2228 mutex_unlock(&codec->mutex);
2229
2230 return 0;
2231}
2232EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2233
2234/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002235 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002236 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002237 * @widget: widget template
2238 *
2239 * Creates a new dapm control based upon the template.
2240 *
2241 * Returns 0 for success else error.
2242 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002243int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002244 const struct snd_soc_dapm_widget *widget)
2245{
2246 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002247 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002248
2249 if ((w = dapm_cnew_widget(widget)) == NULL)
2250 return -ENOMEM;
2251
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002252 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002253 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002254 name_len += 1 + strlen(dapm->codec->name_prefix);
2255 w->name = kmalloc(name_len, GFP_KERNEL);
2256 if (w->name == NULL) {
2257 kfree(w);
2258 return -ENOMEM;
2259 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002260 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002261 snprintf(w->name, name_len, "%s %s",
2262 dapm->codec->name_prefix, widget->name);
2263 else
2264 snprintf(w->name, name_len, "%s", widget->name);
2265
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002266 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002267 w->dapm = dapm;
2268 w->codec = dapm->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002269 INIT_LIST_HEAD(&w->sources);
2270 INIT_LIST_HEAD(&w->sinks);
2271 INIT_LIST_HEAD(&w->list);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002272 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002273
2274 /* machine layer set ups unconnected pins and insertions */
2275 w->connected = 1;
2276 return 0;
2277}
2278EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2279
2280/**
Mark Brown4ba13272008-05-13 14:51:19 +02002281 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002282 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002283 * @widget: widget array
2284 * @num: number of widgets
2285 *
2286 * Creates new DAPM controls based upon the templates.
2287 *
2288 * Returns 0 for success else error.
2289 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002290int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002291 const struct snd_soc_dapm_widget *widget,
2292 int num)
2293{
2294 int i, ret;
2295
2296 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002297 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002298 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002299 dev_err(dapm->dev,
2300 "ASoC: Failed to create DAPM control %s: %d\n",
2301 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002302 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002303 }
Mark Brown4ba13272008-05-13 14:51:19 +02002304 widget++;
2305 }
2306 return 0;
2307}
2308EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2309
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002310static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002311 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002312{
2313 struct snd_soc_dapm_widget *w;
2314
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002315 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002316 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002317 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002318 continue;
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002319 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2320 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002321 if (strstr(w->sname, stream)) {
2322 switch(event) {
2323 case SND_SOC_DAPM_STREAM_START:
2324 w->active = 1;
2325 break;
2326 case SND_SOC_DAPM_STREAM_STOP:
2327 w->active = 0;
2328 break;
2329 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002330 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002331 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002332 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2333 break;
2334 }
2335 }
2336 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002337
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002338 dapm_power_widgets(dapm, event);
2339}
2340
2341/**
2342 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2343 * @rtd: PCM runtime data
2344 * @stream: stream name
2345 * @event: stream event
2346 *
2347 * Sends a stream event to the dapm core. The core then makes any
2348 * necessary widget power changes.
2349 *
2350 * Returns 0 for success else error.
2351 */
2352int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2353 const char *stream, int event)
2354{
2355 struct snd_soc_codec *codec = rtd->codec;
2356
2357 if (stream == NULL)
2358 return 0;
2359
2360 mutex_lock(&codec->mutex);
2361 soc_dapm_stream_event(&codec->dapm, stream, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002362 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002363 return 0;
2364}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002365
2366/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002367 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002368 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002369 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002370 *
Mark Brown74b8f952009-06-06 11:26:15 +01002371 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002372 * a valid audio route and active audio stream.
2373 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2374 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002375 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002376int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002377{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002378 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002379}
Liam Girdwooda5302182008-07-07 13:35:17 +01002380EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002381
2382/**
Mark Brownda341832010-03-15 19:23:37 +00002383 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002384 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002385 * @pin: pin name
2386 *
2387 * Enables input/output pin regardless of any other state. This is
2388 * intended for use with microphone bias supplies used in microphone
2389 * jack detection.
2390 *
2391 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2392 * do any widget power switching.
2393 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002394int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2395 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002396{
2397 struct snd_soc_dapm_widget *w;
2398
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002399 list_for_each_entry(w, &dapm->card->widgets, list) {
2400 if (w->dapm != dapm)
2401 continue;
Mark Brownda341832010-03-15 19:23:37 +00002402 if (!strcmp(w->name, pin)) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002403 dev_dbg(w->dapm->dev,
2404 "dapm: force enable pin %s\n", pin);
Mark Brownda341832010-03-15 19:23:37 +00002405 w->connected = 1;
2406 w->force = 1;
2407 return 0;
2408 }
2409 }
2410
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002411 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Mark Brownda341832010-03-15 19:23:37 +00002412 return -EINVAL;
2413}
2414EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2415
2416/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002417 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002418 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002419 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002420 *
Mark Brown74b8f952009-06-06 11:26:15 +01002421 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002422 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2423 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002424 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002425int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2426 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002427{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002428 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002429}
2430EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2431
2432/**
Mark Brown5817b522008-09-24 11:23:11 +01002433 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002434 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002435 * @pin: pin name
2436 *
2437 * Marks the specified pin as being not connected, disabling it along
2438 * any parent or child widgets. At present this is identical to
2439 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2440 * additional things such as disabling controls which only affect
2441 * paths through the pin.
2442 *
2443 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2444 * do any widget power switching.
2445 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002446int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002447{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002448 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002449}
2450EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2451
2452/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002453 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002454 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002455 * @pin: audio signal pin endpoint (or start point)
2456 *
2457 * Get audio pin status - connected or disconnected.
2458 *
2459 * Returns 1 for connected otherwise 0.
2460 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002461int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2462 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002463{
2464 struct snd_soc_dapm_widget *w;
2465
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002466 list_for_each_entry(w, &dapm->card->widgets, list) {
2467 if (w->dapm != dapm)
2468 continue;
Liam Girdwooda5302182008-07-07 13:35:17 +01002469 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002470 return w->connected;
2471 }
2472
2473 return 0;
2474}
Liam Girdwooda5302182008-07-07 13:35:17 +01002475EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002476
2477/**
Mark Brown1547aba2010-05-07 21:11:40 +01002478 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002479 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01002480 * @pin: audio signal pin endpoint (or start point)
2481 *
2482 * Mark the given endpoint or pin as ignoring suspend. When the
2483 * system is disabled a path between two endpoints flagged as ignoring
2484 * suspend will not be disabled. The path must already be enabled via
2485 * normal means at suspend time, it will not be turned on if it was not
2486 * already enabled.
2487 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002488int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2489 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01002490{
2491 struct snd_soc_dapm_widget *w;
2492
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002493 list_for_each_entry(w, &dapm->card->widgets, list) {
2494 if (w->dapm != dapm)
2495 continue;
Mark Brown1547aba2010-05-07 21:11:40 +01002496 if (!strcmp(w->name, pin)) {
2497 w->ignore_suspend = 1;
2498 return 0;
2499 }
2500 }
2501
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002502 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Mark Brown1547aba2010-05-07 21:11:40 +01002503 return -EINVAL;
2504}
2505EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2506
2507/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002508 * snd_soc_dapm_free - free dapm resources
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002509 * @card: SoC device
Richard Purdie2b97eab2006-10-06 18:32:18 +02002510 *
2511 * Free all dapm widgets and resources.
2512 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002513void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002514{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002515 snd_soc_dapm_sys_remove(dapm->dev);
2516 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02002517 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002518}
2519EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2520
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002521static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01002522{
Mark Brown51737472009-06-22 13:16:51 +01002523 struct snd_soc_dapm_widget *w;
2524 LIST_HEAD(down_list);
2525 int powerdown = 0;
2526
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002527 list_for_each_entry(w, &dapm->card->widgets, list) {
2528 if (w->dapm != dapm)
2529 continue;
Mark Brown51737472009-06-22 13:16:51 +01002530 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00002531 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002532 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002533 powerdown = 1;
2534 }
2535 }
2536
2537 /* If there were no widgets to power down we're already in
2538 * standby.
2539 */
2540 if (powerdown) {
Mark Browned5a4c42011-02-18 11:12:42 -08002541 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00002542 dapm_seq_run(dapm, &down_list, 0, false);
Mark Browned5a4c42011-02-18 11:12:42 -08002543 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01002544 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002545}
Mark Brown51737472009-06-22 13:16:51 +01002546
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002547/*
2548 * snd_soc_dapm_shutdown - callback for system shutdown
2549 */
2550void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2551{
2552 struct snd_soc_codec *codec;
2553
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002554 list_for_each_entry(codec, &card->codec_dev_list, list) {
2555 soc_dapm_shutdown_codec(&codec->dapm);
Mark Browned5a4c42011-02-18 11:12:42 -08002556 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002557 }
Mark Brown51737472009-06-22 13:16:51 +01002558}
2559
Richard Purdie2b97eab2006-10-06 18:32:18 +02002560/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002561MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002562MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2563MODULE_LICENSE("GPL");