blob: 0f94fd057f2907c4fafc7076603f613299b4889b [file] [log] [blame]
Richard Purdie2b97eab2006-10-06 18:32:18 +02001/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020012 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
Mark Brown74b8f952009-06-06 11:26:15 +010015 * DACs/ADCs.
Richard Purdie2b97eab2006-10-06 18:32:18 +020016 * o Platform power domain - can support external components i.e. amps and
17 * mic/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +020021 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
24 * Todo:
25 * o DAPM power change sequencing - allow for configurable per
26 * codec sequences.
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
30 */
31
32#include <linux/module.h>
33#include <linux/moduleparam.h>
34#include <linux/init.h>
35#include <linux/delay.h>
36#include <linux/pm.h>
37#include <linux/bitops.h>
38#include <linux/platform_device.h>
39#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020040#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020042#include <sound/core.h>
43#include <sound/pcm.h>
44#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020045#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020046#include <sound/initval.h>
47
Mark Brown84e90932010-11-04 00:07:02 -040048#include <trace/events/asoc.h>
49
Richard Purdie2b97eab2006-10-06 18:32:18 +020050/* dapm power sequences - make this per codec in the future */
51static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010052 [snd_soc_dapm_pre] = 0,
53 [snd_soc_dapm_supply] = 1,
54 [snd_soc_dapm_micbias] = 2,
Mark Brown010ff262009-08-17 17:39:22 +010055 [snd_soc_dapm_aif_in] = 3,
56 [snd_soc_dapm_aif_out] = 3,
57 [snd_soc_dapm_mic] = 4,
58 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000059 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010060 [snd_soc_dapm_value_mux] = 5,
61 [snd_soc_dapm_dac] = 6,
62 [snd_soc_dapm_mixer] = 7,
63 [snd_soc_dapm_mixer_named_ctl] = 7,
64 [snd_soc_dapm_pga] = 8,
65 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060066 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010067 [snd_soc_dapm_hp] = 10,
68 [snd_soc_dapm_spk] = 10,
69 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020070};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000071
Richard Purdie2b97eab2006-10-06 18:32:18 +020072static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010073 [snd_soc_dapm_pre] = 0,
74 [snd_soc_dapm_adc] = 1,
75 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010076 [snd_soc_dapm_spk] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060077 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010078 [snd_soc_dapm_pga] = 4,
79 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010080 [snd_soc_dapm_mixer] = 5,
81 [snd_soc_dapm_dac] = 6,
82 [snd_soc_dapm_mic] = 7,
83 [snd_soc_dapm_micbias] = 8,
84 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000085 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010086 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010087 [snd_soc_dapm_aif_in] = 10,
88 [snd_soc_dapm_aif_out] = 10,
89 [snd_soc_dapm_supply] = 11,
90 [snd_soc_dapm_post] = 12,
Richard Purdie2b97eab2006-10-06 18:32:18 +020091};
92
Troy Kisky12ef1932008-10-13 17:42:14 -070093static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +010094{
95 if (pop_time)
96 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
97}
98
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +020099static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100100{
101 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200102 char *buf;
103
104 if (!pop_time)
105 return;
106
107 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
108 if (buf == NULL)
109 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100110
111 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200112 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100113 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100114 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200115
116 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100117}
118
Richard Purdie2b97eab2006-10-06 18:32:18 +0200119/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100120static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200121 const struct snd_soc_dapm_widget *_widget)
122{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100123 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200124}
125
Mark Brown452c5ea2009-05-17 21:41:23 +0100126/**
127 * snd_soc_dapm_set_bias_level - set the bias level for the system
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000128 * @card: audio device
Mark Brown452c5ea2009-05-17 21:41:23 +0100129 * @level: level to configure
130 *
131 * Configure the bias (power) levels for the SoC audio device.
132 *
133 * Returns 0 for success else error.
134 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000135static int snd_soc_dapm_set_bias_level(struct snd_soc_card *card,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200136 struct snd_soc_dapm_context *dapm,
137 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100138{
Mark Brown452c5ea2009-05-17 21:41:23 +0100139 int ret = 0;
140
Mark Brownf83fba82009-05-18 15:44:43 +0100141 switch (level) {
142 case SND_SOC_BIAS_ON:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200143 dev_dbg(dapm->dev, "Setting full bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100144 break;
145 case SND_SOC_BIAS_PREPARE:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200146 dev_dbg(dapm->dev, "Setting bias prepare\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100147 break;
148 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200149 dev_dbg(dapm->dev, "Setting standby bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100150 break;
151 case SND_SOC_BIAS_OFF:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200152 dev_dbg(dapm->dev, "Setting bias off\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100153 break;
154 default:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200155 dev_err(dapm->dev, "Setting invalid bias %d\n", level);
Mark Brownf83fba82009-05-18 15:44:43 +0100156 return -EINVAL;
157 }
158
Mark Brown84e90932010-11-04 00:07:02 -0400159 trace_snd_soc_bias_level_start(card, level);
160
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000161 if (card && card->set_bias_level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100162 ret = card->set_bias_level(card, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100163 if (ret == 0) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200164 if (dapm->codec && dapm->codec->driver->set_bias_level)
165 ret = dapm->codec->driver->set_bias_level(dapm->codec, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100166 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200167 dapm->bias_level = level;
Mark Brown474e09c2009-08-19 14:18:53 +0100168 }
Mark Brown1badabd2010-12-04 12:41:04 +0000169 if (ret == 0) {
170 if (card && card->set_bias_level_post)
171 ret = card->set_bias_level_post(card, level);
172 }
Mark Brown452c5ea2009-05-17 21:41:23 +0100173
Mark Brown84e90932010-11-04 00:07:02 -0400174 trace_snd_soc_bias_level_done(card, level);
175
Mark Brown452c5ea2009-05-17 21:41:23 +0100176 return ret;
177}
178
Richard Purdie2b97eab2006-10-06 18:32:18 +0200179/* set up initial codec paths */
180static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
181 struct snd_soc_dapm_path *p, int i)
182{
183 switch (w->id) {
184 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000185 case snd_soc_dapm_mixer:
186 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200187 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100188 struct soc_mixer_control *mc = (struct soc_mixer_control *)
189 w->kcontrols[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400190 unsigned int reg = mc->reg;
191 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100192 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400193 unsigned int mask = (1 << fls(max)) - 1;
194 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200195
196 val = snd_soc_read(w->codec, reg);
197 val = (val >> shift) & mask;
198
199 if ((invert && !val) || (!invert && val))
200 p->connect = 1;
201 else
202 p->connect = 0;
203 }
204 break;
205 case snd_soc_dapm_mux: {
206 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
207 int val, item, bitmask;
208
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100209 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200210 ;
211 val = snd_soc_read(w->codec, e->reg);
212 item = (val >> e->shift_l) & (bitmask - 1);
213
214 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100215 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200216 if (!(strcmp(p->name, e->texts[i])) && item == i)
217 p->connect = 1;
218 }
219 }
220 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000221 case snd_soc_dapm_virt_mux: {
222 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
223
224 p->connect = 0;
225 /* since a virtual mux has no backing registers to
226 * decide which path to connect, it will try to match
227 * with the first enumeration. This is to ensure
228 * that the default mux choice (the first) will be
229 * correctly powered up during initialization.
230 */
231 if (!strcmp(p->name, e->texts[0]))
232 p->connect = 1;
233 }
234 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200235 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200236 struct soc_enum *e = (struct soc_enum *)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200237 w->kcontrols[i].private_value;
238 int val, item;
239
240 val = snd_soc_read(w->codec, e->reg);
241 val = (val >> e->shift_l) & e->mask;
242 for (item = 0; item < e->max; item++) {
243 if (val == e->values[item])
244 break;
245 }
246
247 p->connect = 0;
248 for (i = 0; i < e->max; i++) {
249 if (!(strcmp(p->name, e->texts[i])) && item == i)
250 p->connect = 1;
251 }
252 }
253 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200254 /* does not effect routing - always connected */
255 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600256 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200257 case snd_soc_dapm_output:
258 case snd_soc_dapm_adc:
259 case snd_soc_dapm_input:
260 case snd_soc_dapm_dac:
261 case snd_soc_dapm_micbias:
262 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100263 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100264 case snd_soc_dapm_aif_in:
265 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200266 p->connect = 1;
267 break;
268 /* does effect routing - dynamically connected */
269 case snd_soc_dapm_hp:
270 case snd_soc_dapm_mic:
271 case snd_soc_dapm_spk:
272 case snd_soc_dapm_line:
273 case snd_soc_dapm_pre:
274 case snd_soc_dapm_post:
275 p->connect = 0;
276 break;
277 }
278}
279
Mark Brown74b8f952009-06-06 11:26:15 +0100280/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200281static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200282 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
283 struct snd_soc_dapm_path *path, const char *control_name,
284 const struct snd_kcontrol_new *kcontrol)
285{
286 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
287 int i;
288
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100289 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200290 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200291 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200292 list_add(&path->list_sink, &dest->sources);
293 list_add(&path->list_source, &src->sinks);
294 path->name = (char*)e->texts[i];
295 dapm_set_path_status(dest, path, 0);
296 return 0;
297 }
298 }
299
300 return -ENODEV;
301}
302
Mark Brown74b8f952009-06-06 11:26:15 +0100303/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200304static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200305 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
306 struct snd_soc_dapm_path *path, const char *control_name)
307{
308 int i;
309
310 /* search for mixer kcontrol */
311 for (i = 0; i < dest->num_kcontrols; i++) {
312 if (!strcmp(control_name, dest->kcontrols[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200313 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200314 list_add(&path->list_sink, &dest->sources);
315 list_add(&path->list_source, &src->sinks);
316 path->name = dest->kcontrols[i].name;
317 dapm_set_path_status(dest, path, i);
318 return 0;
319 }
320 }
321 return -ENODEV;
322}
323
324/* update dapm codec register bits */
325static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
326{
327 int change, power;
Daniel Ribeiro46f58222009-06-07 02:49:11 -0300328 unsigned int old, new;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200329 struct snd_soc_codec *codec = widget->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200330 struct snd_soc_dapm_context *dapm = widget->dapm;
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200331 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200332
333 /* check for valid widgets */
334 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
335 widget->id == snd_soc_dapm_output ||
336 widget->id == snd_soc_dapm_hp ||
337 widget->id == snd_soc_dapm_mic ||
338 widget->id == snd_soc_dapm_line ||
339 widget->id == snd_soc_dapm_spk)
340 return 0;
341
342 power = widget->power;
343 if (widget->invert)
344 power = (power ? 0:1);
345
346 old = snd_soc_read(codec, widget->reg);
347 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
348
349 change = old != new;
350 if (change) {
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200351 pop_dbg(dapm->dev, card->pop_time,
352 "pop test %s : %s in %d ms\n",
Troy Kisky12ef1932008-10-13 17:42:14 -0700353 widget->name, widget->power ? "on" : "off",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200354 card->pop_time);
355 pop_wait(card->pop_time);
Mark Brown69224712010-03-03 14:57:09 +0000356 snd_soc_write(codec, widget->reg, new);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200357 }
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200358 dev_dbg(dapm->dev, "reg %x old %x new %x change %d\n", widget->reg,
359 old, new, change);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200360 return change;
361}
362
Richard Purdie2b97eab2006-10-06 18:32:18 +0200363/* create new dapm mixer control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200364static int dapm_new_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200365 struct snd_soc_dapm_widget *w)
366{
367 int i, ret = 0;
Mark Brown219b93f2008-10-28 13:02:31 +0000368 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200369 struct snd_soc_dapm_path *path;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200370 struct snd_card *card = dapm->codec->card->snd_card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200371
372 /* add kcontrol */
373 for (i = 0; i < w->num_kcontrols; i++) {
374
375 /* match name */
376 list_for_each_entry(path, &w->sources, list_sink) {
377
378 /* mixer/mux paths name must match control name */
379 if (path->name != (char*)w->kcontrols[i].name)
380 continue;
381
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000382 /* add dapm control with long name.
383 * for dapm_mixer this is the concatenation of the
384 * mixer and kcontrol name.
385 * for dapm_mixer_named_ctl this is simply the
386 * kcontrol name.
387 */
388 name_len = strlen(w->kcontrols[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000389 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000390 name_len += 1 + strlen(w->name);
391
Mark Brown219b93f2008-10-28 13:02:31 +0000392 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000393
Richard Purdie2b97eab2006-10-06 18:32:18 +0200394 if (path->long_name == NULL)
395 return -ENOMEM;
396
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000397 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000398 default:
399 snprintf(path->long_name, name_len, "%s %s",
400 w->name, w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000401 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000402 case snd_soc_dapm_mixer_named_ctl:
403 snprintf(path->long_name, name_len, "%s",
404 w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000405 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000406 }
407
Mark Brown219b93f2008-10-28 13:02:31 +0000408 path->long_name[name_len - 1] = '\0';
409
Richard Purdie2b97eab2006-10-06 18:32:18 +0200410 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
411 path->long_name);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200412 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200413 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200414 dev_err(dapm->dev,
415 "asoc: failed to add dapm kcontrol %s: %d\n",
416 path->long_name, ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200417 kfree(path->long_name);
418 path->long_name = NULL;
419 return ret;
420 }
421 }
422 }
423 return ret;
424}
425
426/* create new dapm mux control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200427static int dapm_new_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200428 struct snd_soc_dapm_widget *w)
429{
430 struct snd_soc_dapm_path *path = NULL;
431 struct snd_kcontrol *kcontrol;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200432 struct snd_card *card = dapm->codec->card->snd_card;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200433 int ret = 0;
434
435 if (!w->num_kcontrols) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200436 dev_err(dapm->dev, "asoc: mux %s has no controls\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200437 return -EINVAL;
438 }
439
440 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200441 ret = snd_ctl_add(card, kcontrol);
442
Richard Purdie2b97eab2006-10-06 18:32:18 +0200443 if (ret < 0)
444 goto err;
445
446 list_for_each_entry(path, &w->sources, list_sink)
447 path->kcontrol = kcontrol;
448
449 return ret;
450
451err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200452 dev_err(dapm->dev, "asoc: failed to add kcontrol %s\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200453 return ret;
454}
455
456/* create new dapm volume control */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200457static int dapm_new_pga(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200458 struct snd_soc_dapm_widget *w)
459{
Mark Browna6c65732010-03-03 17:45:21 +0000460 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200461 dev_err(w->dapm->dev,
462 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200463
Mark Browna6c65732010-03-03 17:45:21 +0000464 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200465}
466
467/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200468static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200469{
470 struct snd_soc_dapm_path *p;
471
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200472 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200473 p->walked = 0;
474}
475
Mark Brown99497882010-05-07 20:24:05 +0100476/* We implement power down on suspend by checking the power state of
477 * the ALSA card - when we are suspending the ALSA state for the card
478 * is set to D3.
479 */
480static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
481{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200482 int level = snd_power_get_state(widget->dapm->codec->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100483
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000484 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100485 case SNDRV_CTL_POWER_D3hot:
486 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100487 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200488 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
489 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100490 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100491 default:
492 return 1;
493 }
494}
495
Richard Purdie2b97eab2006-10-06 18:32:18 +0200496/*
497 * Recursively check for a completed path to an active or physically connected
498 * output widget. Returns number of complete paths.
499 */
500static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
501{
502 struct snd_soc_dapm_path *path;
503 int con = 0;
504
Mark Brown246d0a12009-04-22 18:24:55 +0100505 if (widget->id == snd_soc_dapm_supply)
506 return 0;
507
Mark Brown010ff262009-08-17 17:39:22 +0100508 switch (widget->id) {
509 case snd_soc_dapm_adc:
510 case snd_soc_dapm_aif_out:
511 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100512 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100513 default:
514 break;
515 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200516
517 if (widget->connected) {
518 /* connected pin ? */
519 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100520 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200521
522 /* connected jack or spk ? */
523 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300524 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown99497882010-05-07 20:24:05 +0100525 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200526 }
527
528 list_for_each_entry(path, &widget->sinks, list_source) {
529 if (path->walked)
530 continue;
531
532 if (path->sink && path->connect) {
533 path->walked = 1;
534 con += is_connected_output_ep(path->sink);
535 }
536 }
537
538 return con;
539}
540
541/*
542 * Recursively check for a completed path to an active or physically connected
543 * input widget. Returns number of complete paths.
544 */
545static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
546{
547 struct snd_soc_dapm_path *path;
548 int con = 0;
549
Mark Brown246d0a12009-04-22 18:24:55 +0100550 if (widget->id == snd_soc_dapm_supply)
551 return 0;
552
Richard Purdie2b97eab2006-10-06 18:32:18 +0200553 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100554 switch (widget->id) {
555 case snd_soc_dapm_dac:
556 case snd_soc_dapm_aif_in:
557 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100558 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100559 default:
560 break;
561 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200562
563 if (widget->connected) {
564 /* connected pin ? */
565 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100566 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200567
568 /* connected VMID/Bias for lower pops */
569 if (widget->id == snd_soc_dapm_vmid)
Mark Brown99497882010-05-07 20:24:05 +0100570 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200571
572 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300573 if (widget->id == snd_soc_dapm_mic ||
574 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown99497882010-05-07 20:24:05 +0100575 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200576 }
577
578 list_for_each_entry(path, &widget->sources, list_sink) {
579 if (path->walked)
580 continue;
581
582 if (path->source && path->connect) {
583 path->walked = 1;
584 con += is_connected_input_ep(path->source);
585 }
586 }
587
588 return con;
589}
590
591/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300592 * Handler for generic register modifier widget.
593 */
594int dapm_reg_event(struct snd_soc_dapm_widget *w,
595 struct snd_kcontrol *kcontrol, int event)
596{
597 unsigned int val;
598
599 if (SND_SOC_DAPM_EVENT_ON(event))
600 val = w->on_val;
601 else
602 val = w->off_val;
603
604 snd_soc_update_bits(w->codec, -(w->reg + 1),
605 w->mask << w->shift, val << w->shift);
606
607 return 0;
608}
Mark Brown11589412008-07-29 11:42:23 +0100609EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300610
Mark Brown025756e2009-04-13 11:09:18 +0100611/* Standard power change method, used to apply power changes to most
612 * widgets.
613 */
614static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
615{
616 int ret;
617
618 /* call any power change event handlers */
619 if (w->event)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200620 dev_dbg(w->dapm->dev, "power %s event for %s flags %x\n",
Mark Brown025756e2009-04-13 11:09:18 +0100621 w->power ? "on" : "off",
622 w->name, w->event_flags);
623
624 /* power up pre event */
625 if (w->power && w->event &&
626 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
627 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
628 if (ret < 0)
629 return ret;
630 }
631
632 /* power down pre event */
633 if (!w->power && w->event &&
634 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
635 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
636 if (ret < 0)
637 return ret;
638 }
639
Mark Brown025756e2009-04-13 11:09:18 +0100640 dapm_update_bits(w);
641
Mark Brown025756e2009-04-13 11:09:18 +0100642 /* power up post event */
643 if (w->power && w->event &&
644 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
645 ret = w->event(w,
646 NULL, SND_SOC_DAPM_POST_PMU);
647 if (ret < 0)
648 return ret;
649 }
650
651 /* power down post event */
652 if (!w->power && w->event &&
653 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
654 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
655 if (ret < 0)
656 return ret;
657 }
658
659 return 0;
660}
661
Mark Browncd0f2d42009-04-20 16:56:59 +0100662/* Generic check to see if a widget should be powered.
663 */
664static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
665{
666 int in, out;
667
668 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200669 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100670 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200671 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100672 return out != 0 && in != 0;
673}
674
Mark Brown6ea31b92009-04-20 17:15:41 +0100675/* Check to see if an ADC has power */
676static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
677{
678 int in;
679
680 if (w->active) {
681 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200682 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100683 return in != 0;
684 } else {
685 return dapm_generic_check_power(w);
686 }
687}
688
689/* Check to see if a DAC has power */
690static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
691{
692 int out;
693
694 if (w->active) {
695 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200696 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100697 return out != 0;
698 } else {
699 return dapm_generic_check_power(w);
700 }
701}
702
Mark Brown246d0a12009-04-22 18:24:55 +0100703/* Check to see if a power supply is needed */
704static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
705{
706 struct snd_soc_dapm_path *path;
707 int power = 0;
708
709 /* Check if one of our outputs is connected */
710 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +0100711 if (path->connected &&
712 !path->connected(path->source, path->sink))
713 continue;
714
Mark Brown246d0a12009-04-22 18:24:55 +0100715 if (path->sink && path->sink->power_check &&
716 path->sink->power_check(path->sink)) {
717 power = 1;
718 break;
719 }
720 }
721
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200722 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100723
724 return power;
725}
726
Mark Brown38357ab2009-06-06 19:03:23 +0100727static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
728 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +0000729 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +0000730{
Mark Brown828a8422011-01-15 13:14:30 +0000731 int *sort;
732
733 if (power_up)
734 sort = dapm_up_seq;
735 else
736 sort = dapm_down_seq;
737
Mark Brown38357ab2009-06-06 19:03:23 +0100738 if (sort[a->id] != sort[b->id])
739 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +0000740 if (a->subseq != b->subseq) {
741 if (power_up)
742 return a->subseq - b->subseq;
743 else
744 return b->subseq - a->subseq;
745 }
Mark Brownb22ead22009-06-07 12:51:26 +0100746 if (a->reg != b->reg)
747 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +0000748 if (a->dapm != b->dapm)
749 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +0000750
Mark Brown38357ab2009-06-06 19:03:23 +0100751 return 0;
752}
Mark Brown42aa3412009-03-01 19:21:10 +0000753
Mark Brown38357ab2009-06-06 19:03:23 +0100754/* Insert a widget in order into a DAPM power sequence. */
755static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
756 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +0000757 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +0100758{
759 struct snd_soc_dapm_widget *w;
760
761 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +0000762 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +0100763 list_add_tail(&new_widget->power_list, &w->power_list);
764 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000765 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100766
Mark Brown38357ab2009-06-06 19:03:23 +0100767 list_add_tail(&new_widget->power_list, list);
768}
Mark Brown42aa3412009-03-01 19:21:10 +0000769
Mark Brown68f89ad2010-11-03 23:51:49 -0400770static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
771 struct snd_soc_dapm_widget *w, int event)
772{
773 struct snd_soc_card *card = dapm->card;
774 const char *ev_name;
775 int power, ret;
776
777 switch (event) {
778 case SND_SOC_DAPM_PRE_PMU:
779 ev_name = "PRE_PMU";
780 power = 1;
781 break;
782 case SND_SOC_DAPM_POST_PMU:
783 ev_name = "POST_PMU";
784 power = 1;
785 break;
786 case SND_SOC_DAPM_PRE_PMD:
787 ev_name = "PRE_PMD";
788 power = 0;
789 break;
790 case SND_SOC_DAPM_POST_PMD:
791 ev_name = "POST_PMD";
792 power = 0;
793 break;
794 default:
795 BUG();
796 return;
797 }
798
799 if (w->power != power)
800 return;
801
802 if (w->event && (w->event_flags & event)) {
803 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
804 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -0400805 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400806 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -0400807 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400808 if (ret < 0)
809 pr_err("%s: %s event failed: %d\n",
810 ev_name, w->name, ret);
811 }
812}
813
Mark Brownb22ead22009-06-07 12:51:26 +0100814/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200815static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +0100816 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100817{
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200818 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -0400819 struct snd_soc_dapm_widget *w;
820 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +0100821 unsigned int value = 0;
822 unsigned int mask = 0;
823 unsigned int cur_mask;
824
825 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
826 power_list)->reg;
827
828 list_for_each_entry(w, pending, power_list) {
829 cur_mask = 1 << w->shift;
830 BUG_ON(reg != w->reg);
831
832 if (w->invert)
833 power = !w->power;
834 else
835 power = w->power;
836
837 mask |= cur_mask;
838 if (power)
839 value |= cur_mask;
840
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200841 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +0100842 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
843 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100844
Mark Brown68f89ad2010-11-03 23:51:49 -0400845 /* Check for events */
846 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
847 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100848 }
849
Mark Brown81628102009-06-07 13:21:24 +0100850 if (reg >= 0) {
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200851 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +0100852 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200853 value, mask, reg, card->pop_time);
854 pop_wait(card->pop_time);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200855 snd_soc_update_bits(dapm->codec, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +0100856 }
857
858 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -0400859 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
860 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +0000861 }
Mark Brown42aa3412009-03-01 19:21:10 +0000862}
863
Mark Brownb22ead22009-06-07 12:51:26 +0100864/* Apply a DAPM power sequence.
865 *
866 * We walk over a pre-sorted list of widgets to apply power to. In
867 * order to minimise the number of writes to the device required
868 * multiple widgets will be updated in a single write where possible.
869 * Currently anything that requires more than a single write is not
870 * handled.
871 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200872static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +0000873 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +0100874{
875 struct snd_soc_dapm_widget *w, *n;
876 LIST_HEAD(pending);
877 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +0000878 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +0100879 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200880 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +0000881 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +0000882 int *sort;
883
884 if (power_up)
885 sort = dapm_up_seq;
886 else
887 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +0100888
Mark Brownb22ead22009-06-07 12:51:26 +0100889 list_for_each_entry_safe(w, n, list, power_list) {
890 ret = 0;
891
892 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200893 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +0000894 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +0100895 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200896 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +0100897
Mark Brown474b62d2011-01-18 16:14:44 +0000898 if (cur_dapm && cur_dapm->seq_notifier) {
899 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
900 if (sort[i] == cur_sort)
901 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +0000902 i,
903 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +0000904 }
905
Mark Brownb22ead22009-06-07 12:51:26 +0100906 INIT_LIST_HEAD(&pending);
907 cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +0000908 cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +0100909 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200910 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +0100911 }
912
Mark Brown163cac02009-06-07 10:12:52 +0100913 switch (w->id) {
914 case snd_soc_dapm_pre:
915 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100916 list_for_each_entry_safe_continue(w, n, list,
917 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100918
Mark Brownb22ead22009-06-07 12:51:26 +0100919 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100920 ret = w->event(w,
921 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100922 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100923 ret = w->event(w,
924 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +0100925 break;
926
927 case snd_soc_dapm_post:
928 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100929 list_for_each_entry_safe_continue(w, n, list,
930 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100931
Mark Brownb22ead22009-06-07 12:51:26 +0100932 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100933 ret = w->event(w,
934 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100935 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100936 ret = w->event(w,
937 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100938 break;
939
940 case snd_soc_dapm_input:
941 case snd_soc_dapm_output:
942 case snd_soc_dapm_hp:
943 case snd_soc_dapm_mic:
944 case snd_soc_dapm_line:
945 case snd_soc_dapm_spk:
946 /* No register support currently */
Mark Brownb22ead22009-06-07 12:51:26 +0100947 ret = dapm_generic_apply_power(w);
Mark Brown163cac02009-06-07 10:12:52 +0100948 break;
949
950 default:
Mark Brown81628102009-06-07 13:21:24 +0100951 /* Queue it up for application */
952 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +0000953 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +0100954 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200955 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +0100956 list_move(&w->power_list, &pending);
957 break;
Mark Brown163cac02009-06-07 10:12:52 +0100958 }
Mark Brownb22ead22009-06-07 12:51:26 +0100959
960 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200961 dev_err(w->dapm->dev,
962 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +0100963 }
Mark Brownb22ead22009-06-07 12:51:26 +0100964
965 if (!list_empty(&pending))
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200966 dapm_seq_run_coalesced(dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +0000967
968 if (cur_dapm && cur_dapm->seq_notifier) {
969 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
970 if (sort[i] == cur_sort)
971 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +0000972 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +0000973 }
Mark Brown163cac02009-06-07 10:12:52 +0100974}
975
Mark Brown97404f22010-12-14 16:13:57 +0000976static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
977{
978 struct snd_soc_dapm_update *update = dapm->update;
979 struct snd_soc_dapm_widget *w;
980 int ret;
981
982 if (!update)
983 return;
984
985 w = update->widget;
986
987 if (w->event &&
988 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
989 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
990 if (ret != 0)
991 pr_err("%s DAPM pre-event failed: %d\n",
992 w->name, ret);
993 }
994
995 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
996 update->val);
997 if (ret < 0)
998 pr_err("%s DAPM update failed: %d\n", w->name, ret);
999
1000 if (w->event &&
1001 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1002 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1003 if (ret != 0)
1004 pr_err("%s DAPM post-event failed: %d\n",
1005 w->name, ret);
1006 }
1007}
1008
1009
1010
Mark Brown42aa3412009-03-01 19:21:10 +00001011/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001012 * Scan each dapm widget for complete audio path.
1013 * A complete path is a route that has valid endpoints i.e.:-
1014 *
1015 * o DAC to output pin.
1016 * o Input Pin to ADC.
1017 * o Input pin to Output pin (bypass, sidetone)
1018 * o DAC to ADC (loopback).
1019 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001020static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001021{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001022 struct snd_soc_card *card = dapm->codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001023 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001024 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001025 LIST_HEAD(up_list);
1026 LIST_HEAD(down_list);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001027 int ret = 0;
Mark Brown38357ab2009-06-06 19:03:23 +01001028 int power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001029
Mark Brown84e90932010-11-04 00:07:02 -04001030 trace_snd_soc_dapm_start(card);
1031
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001032 list_for_each_entry(d, &card->dapm_list, list)
1033 if (d->n_widgets)
1034 d->dev_power = 0;
1035
Mark Brown6d3ddc82009-05-16 17:47:29 +01001036 /* Check which widgets we need to power and store them in
1037 * lists indicating if they should be powered up or down.
1038 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001039 list_for_each_entry(w, &card->widgets, list) {
Mark Brown6d3ddc82009-05-16 17:47:29 +01001040 switch (w->id) {
1041 case snd_soc_dapm_pre:
Mark Brown828a8422011-01-15 13:14:30 +00001042 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001043 break;
1044 case snd_soc_dapm_post:
Mark Brown828a8422011-01-15 13:14:30 +00001045 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001046 break;
1047
1048 default:
1049 if (!w->power_check)
1050 continue;
1051
Mark Brown99497882010-05-07 20:24:05 +01001052 if (!w->force)
Mark Brown50b6bce2009-11-23 13:11:53 +00001053 power = w->power_check(w);
Mark Brown99497882010-05-07 20:24:05 +01001054 else
1055 power = 1;
1056 if (power)
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001057 w->dapm->dev_power = 1;
Mark Brown452c5ea2009-05-17 21:41:23 +01001058
Mark Brown6d3ddc82009-05-16 17:47:29 +01001059 if (w->power == power)
1060 continue;
1061
Mark Brown84e90932010-11-04 00:07:02 -04001062 trace_snd_soc_dapm_widget_power(w, power);
1063
Mark Brown6d3ddc82009-05-16 17:47:29 +01001064 if (power)
Mark Brown828a8422011-01-15 13:14:30 +00001065 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001066 else
Mark Brown828a8422011-01-15 13:14:30 +00001067 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001068
1069 w->power = power;
1070 break;
1071 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001072 }
1073
Mark Brownb14b76a2009-08-17 11:55:38 +01001074 /* If there are no DAPM widgets then try to figure out power from the
1075 * event type.
1076 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001077 if (!dapm->n_widgets) {
Mark Brownb14b76a2009-08-17 11:55:38 +01001078 switch (event) {
1079 case SND_SOC_DAPM_STREAM_START:
1080 case SND_SOC_DAPM_STREAM_RESUME:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001081 dapm->dev_power = 1;
Mark Brownb14b76a2009-08-17 11:55:38 +01001082 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001083 case SND_SOC_DAPM_STREAM_STOP:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001084 dapm->dev_power = !!dapm->codec->active;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001085 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001086 case SND_SOC_DAPM_STREAM_SUSPEND:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001087 dapm->dev_power = 0;
Mark Brown50b6bce2009-11-23 13:11:53 +00001088 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001089 case SND_SOC_DAPM_STREAM_NOP:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001090 switch (dapm->bias_level) {
Mark Browna96ca332010-01-19 22:49:43 +00001091 case SND_SOC_BIAS_STANDBY:
1092 case SND_SOC_BIAS_OFF:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001093 dapm->dev_power = 0;
Mark Browna96ca332010-01-19 22:49:43 +00001094 break;
1095 default:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001096 dapm->dev_power = 1;
Mark Browna96ca332010-01-19 22:49:43 +00001097 break;
1098 }
Mark Brown50b6bce2009-11-23 13:11:53 +00001099 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001100 default:
1101 break;
1102 }
1103 }
1104
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001105 list_for_each_entry(d, &dapm->card->dapm_list, list) {
1106 if (d->dev_power && d->bias_level == SND_SOC_BIAS_OFF) {
1107 ret = snd_soc_dapm_set_bias_level(card, d,
1108 SND_SOC_BIAS_STANDBY);
1109 if (ret != 0)
1110 dev_err(d->dev,
1111 "Failed to turn on bias: %d\n", ret);
1112 }
Mark Browna96ca332010-01-19 22:49:43 +00001113
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001114 /* If we're changing to all on or all off then prepare */
1115 if ((d->dev_power && d->bias_level == SND_SOC_BIAS_STANDBY) ||
1116 (!d->dev_power && d->bias_level == SND_SOC_BIAS_ON)) {
1117 ret = snd_soc_dapm_set_bias_level(card, d,
1118 SND_SOC_BIAS_PREPARE);
1119 if (ret != 0)
1120 dev_err(d->dev,
1121 "Failed to prepare bias: %d\n", ret);
1122 }
Mark Brown452c5ea2009-05-17 21:41:23 +01001123 }
1124
Mark Brown6d3ddc82009-05-16 17:47:29 +01001125 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001126 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001127
Mark Brown97404f22010-12-14 16:13:57 +00001128 dapm_widget_update(dapm);
1129
Mark Brown6d3ddc82009-05-16 17:47:29 +01001130 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001131 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001132
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001133 list_for_each_entry(d, &dapm->card->dapm_list, list) {
1134 /* If we just powered the last thing off drop to standby bias */
1135 if (d->bias_level == SND_SOC_BIAS_PREPARE && !d->dev_power) {
1136 ret = snd_soc_dapm_set_bias_level(card, d,
1137 SND_SOC_BIAS_STANDBY);
1138 if (ret != 0)
1139 dev_err(d->dev,
1140 "Failed to apply standby bias: %d\n",
1141 ret);
1142 }
Mark Brown452c5ea2009-05-17 21:41:23 +01001143
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001144 /* If we're in standby and can support bias off then do that */
1145 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1146 d->idle_bias_off) {
1147 ret = snd_soc_dapm_set_bias_level(card, d,
1148 SND_SOC_BIAS_OFF);
1149 if (ret != 0)
1150 dev_err(d->dev,
1151 "Failed to turn off bias: %d\n", ret);
1152 }
Mark Browna96ca332010-01-19 22:49:43 +00001153
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001154 /* If we just powered up then move to active bias */
1155 if (d->bias_level == SND_SOC_BIAS_PREPARE && d->dev_power) {
1156 ret = snd_soc_dapm_set_bias_level(card, d,
1157 SND_SOC_BIAS_ON);
1158 if (ret != 0)
1159 dev_err(d->dev,
1160 "Failed to apply active bias: %d\n",
1161 ret);
1162 }
Mark Brown452c5ea2009-05-17 21:41:23 +01001163 }
1164
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001165 pop_dbg(dapm->dev, card->pop_time,
1166 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001167 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001168
Mark Brown84e90932010-11-04 00:07:02 -04001169 trace_snd_soc_dapm_done(card);
1170
Mark Brown42aa3412009-03-01 19:21:10 +00001171 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001172}
1173
Mark Brown79fb9382009-08-21 16:38:13 +01001174#ifdef CONFIG_DEBUG_FS
1175static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1176{
1177 file->private_data = inode->i_private;
1178 return 0;
1179}
1180
1181static ssize_t dapm_widget_power_read_file(struct file *file,
1182 char __user *user_buf,
1183 size_t count, loff_t *ppos)
1184{
1185 struct snd_soc_dapm_widget *w = file->private_data;
1186 char *buf;
1187 int in, out;
1188 ssize_t ret;
1189 struct snd_soc_dapm_path *p = NULL;
1190
1191 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1192 if (!buf)
1193 return -ENOMEM;
1194
1195 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001196 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001197 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001198 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001199
Mark Brownd033c362009-12-04 15:25:56 +00001200 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001201 w->name, w->power ? "On" : "Off", in, out);
1202
Mark Brownd033c362009-12-04 15:25:56 +00001203 if (w->reg >= 0)
1204 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1205 " - R%d(0x%x) bit %d",
1206 w->reg, w->reg, w->shift);
1207
1208 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1209
Mark Brown3eef08b2009-09-14 16:49:00 +01001210 if (w->sname)
1211 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1212 w->sname,
1213 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001214
1215 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001216 if (p->connected && !p->connected(w, p->sink))
1217 continue;
1218
Mark Brown79fb9382009-08-21 16:38:13 +01001219 if (p->connect)
1220 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1221 " in %s %s\n",
1222 p->name ? p->name : "static",
1223 p->source->name);
1224 }
1225 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001226 if (p->connected && !p->connected(w, p->sink))
1227 continue;
1228
Mark Brown79fb9382009-08-21 16:38:13 +01001229 if (p->connect)
1230 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1231 " out %s %s\n",
1232 p->name ? p->name : "static",
1233 p->sink->name);
1234 }
1235
1236 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1237
1238 kfree(buf);
1239 return ret;
1240}
1241
1242static const struct file_operations dapm_widget_power_fops = {
1243 .open = dapm_widget_power_open_file,
1244 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001245 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001246};
1247
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001248void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001249{
1250 struct snd_soc_dapm_widget *w;
1251 struct dentry *d;
1252
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001253 if (!dapm->debugfs_dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001254 return;
1255
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001256 list_for_each_entry(w, &dapm->card->widgets, list) {
1257 if (!w->name || w->dapm != dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001258 continue;
1259
1260 d = debugfs_create_file(w->name, 0444,
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001261 dapm->debugfs_dapm, w,
Mark Brown79fb9382009-08-21 16:38:13 +01001262 &dapm_widget_power_fops);
1263 if (!d)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001264 dev_warn(w->dapm->dev,
1265 "ASoC: Failed to create %s debugfs file\n",
1266 w->name);
Mark Brown79fb9382009-08-21 16:38:13 +01001267 }
1268}
1269#else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001270void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
Mark Brown79fb9382009-08-21 16:38:13 +01001271{
1272}
1273#endif
1274
Richard Purdie2b97eab2006-10-06 18:32:18 +02001275/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001276static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001277 struct snd_kcontrol *kcontrol, int change,
1278 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001279{
1280 struct snd_soc_dapm_path *path;
1281 int found = 0;
1282
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001283 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001284 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001285 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001286 return -ENODEV;
1287
Mark Brown3a655772009-10-05 17:23:30 +01001288 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001289 return 0;
1290
1291 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001292 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001293 if (path->kcontrol != kcontrol)
1294 continue;
1295
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001296 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001297 continue;
1298
1299 found = 1;
1300 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001301 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001302 path->connect = 1; /* new connection */
1303 else
1304 path->connect = 0; /* old connection must be powered down */
1305 }
1306
Mark Brownb91b8fa2010-01-20 18:18:35 +00001307 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001308 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001309
1310 return 0;
1311}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001312
Milan plzik1b075e32008-01-10 14:39:46 +01001313/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001314static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001315 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001316{
1317 struct snd_soc_dapm_path *path;
1318 int found = 0;
1319
Milan plzik1b075e32008-01-10 14:39:46 +01001320 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001321 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001322 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001323 return -ENODEV;
1324
Richard Purdie2b97eab2006-10-06 18:32:18 +02001325 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001326 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001327 if (path->kcontrol != kcontrol)
1328 continue;
1329
1330 /* found, now check type */
1331 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001332 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001333 break;
1334 }
1335
Mark Brownb91b8fa2010-01-20 18:18:35 +00001336 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001337 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001338
1339 return 0;
1340}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001341
1342/* show dapm widget status in sys fs */
1343static ssize_t dapm_widget_show(struct device *dev,
1344 struct device_attribute *attr, char *buf)
1345{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001346 struct snd_soc_pcm_runtime *rtd =
1347 container_of(dev, struct snd_soc_pcm_runtime, dev);
1348 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001349 struct snd_soc_dapm_widget *w;
1350 int count = 0;
1351 char *state = "not set";
1352
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001353 list_for_each_entry(w, &codec->card->widgets, list) {
1354 if (w->dapm != &codec->dapm)
1355 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001356
1357 /* only display widgets that burnm power */
1358 switch (w->id) {
1359 case snd_soc_dapm_hp:
1360 case snd_soc_dapm_mic:
1361 case snd_soc_dapm_spk:
1362 case snd_soc_dapm_line:
1363 case snd_soc_dapm_micbias:
1364 case snd_soc_dapm_dac:
1365 case snd_soc_dapm_adc:
1366 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001367 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001368 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001369 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001370 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001371 if (w->name)
1372 count += sprintf(buf + count, "%s: %s\n",
1373 w->name, w->power ? "On":"Off");
1374 break;
1375 default:
1376 break;
1377 }
1378 }
1379
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001380 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001381 case SND_SOC_BIAS_ON:
1382 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001383 break;
Mark Brown0be98982008-05-19 12:31:28 +02001384 case SND_SOC_BIAS_PREPARE:
1385 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001386 break;
Mark Brown0be98982008-05-19 12:31:28 +02001387 case SND_SOC_BIAS_STANDBY:
1388 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001389 break;
Mark Brown0be98982008-05-19 12:31:28 +02001390 case SND_SOC_BIAS_OFF:
1391 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001392 break;
1393 }
1394 count += sprintf(buf + count, "PM State: %s\n", state);
1395
1396 return count;
1397}
1398
1399static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1400
1401int snd_soc_dapm_sys_add(struct device *dev)
1402{
Troy Kisky12ef1932008-10-13 17:42:14 -07001403 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001404}
1405
1406static void snd_soc_dapm_sys_remove(struct device *dev)
1407{
Mark Brownaef90842009-05-16 17:53:16 +01001408 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001409}
1410
1411/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001412static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001413{
1414 struct snd_soc_dapm_widget *w, *next_w;
1415 struct snd_soc_dapm_path *p, *next_p;
1416
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001417 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1418 if (w->dapm != dapm)
1419 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001420 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001421 /*
1422 * remove source and sink paths associated to this widget.
1423 * While removing the path, remove reference to it from both
1424 * source and sink widgets so that path is removed only once.
1425 */
1426 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1427 list_del(&p->list_sink);
1428 list_del(&p->list_source);
1429 list_del(&p->list);
1430 kfree(p->long_name);
1431 kfree(p);
1432 }
1433 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1434 list_del(&p->list_sink);
1435 list_del(&p->list_source);
1436 list_del(&p->list);
1437 kfree(p->long_name);
1438 kfree(p);
1439 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001440 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001441 kfree(w);
1442 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001443}
1444
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001445static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001446 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001447{
1448 struct snd_soc_dapm_widget *w;
1449
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001450 list_for_each_entry(w, &dapm->card->widgets, list) {
1451 if (w->dapm != dapm)
1452 continue;
Liam Girdwooda5302182008-07-07 13:35:17 +01001453 if (!strcmp(w->name, pin)) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001454 dev_dbg(w->dapm->dev, "dapm: pin %s = %d\n",
1455 pin, status);
Liam Girdwooda5302182008-07-07 13:35:17 +01001456 w->connected = status;
Mark Brown5b9e87c2010-03-22 13:36:13 +00001457 /* Allow disabling of forced pins */
1458 if (status == 0)
1459 w->force = 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001460 return 0;
1461 }
1462 }
1463
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001464 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001465 return -EINVAL;
1466}
1467
Richard Purdie2b97eab2006-10-06 18:32:18 +02001468/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001469 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001470 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001471 *
1472 * Walks all dapm audio paths and powers widgets according to their
1473 * stream or path usage.
1474 *
1475 * Returns 0 for success.
1476 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001477int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001478{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001479 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001480}
Liam Girdwooda5302182008-07-07 13:35:17 +01001481EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001482
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001483static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001484 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001485{
1486 struct snd_soc_dapm_path *path;
1487 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001488 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001489 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001490 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001491 const char *source;
1492 char prefixed_sink[80];
1493 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001494 int ret = 0;
1495
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001496 if (dapm->codec->name_prefix) {
1497 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1498 dapm->codec->name_prefix, route->sink);
1499 sink = prefixed_sink;
1500 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1501 dapm->codec->name_prefix, route->source);
1502 source = prefixed_source;
1503 } else {
1504 sink = route->sink;
1505 source = route->source;
1506 }
1507
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001508 /*
1509 * find src and dest widgets over all widgets but favor a widget from
1510 * current DAPM context
1511 */
1512 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001513 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001514 wtsink = w;
1515 if (w->dapm == dapm)
1516 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001517 continue;
1518 }
1519 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001520 wtsource = w;
1521 if (w->dapm == dapm)
1522 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001523 }
1524 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001525 /* use widget from another DAPM context if not found from this */
1526 if (!wsink)
1527 wsink = wtsink;
1528 if (!wsource)
1529 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001530
1531 if (wsource == NULL || wsink == NULL)
1532 return -ENODEV;
1533
1534 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1535 if (!path)
1536 return -ENOMEM;
1537
1538 path->source = wsource;
1539 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001540 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001541 INIT_LIST_HEAD(&path->list);
1542 INIT_LIST_HEAD(&path->list_source);
1543 INIT_LIST_HEAD(&path->list_sink);
1544
1545 /* check for external widgets */
1546 if (wsink->id == snd_soc_dapm_input) {
1547 if (wsource->id == snd_soc_dapm_micbias ||
1548 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001549 wsource->id == snd_soc_dapm_line ||
1550 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001551 wsink->ext = 1;
1552 }
1553 if (wsource->id == snd_soc_dapm_output) {
1554 if (wsink->id == snd_soc_dapm_spk ||
1555 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001556 wsink->id == snd_soc_dapm_line ||
1557 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001558 wsource->ext = 1;
1559 }
1560
1561 /* connect static paths */
1562 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001563 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001564 list_add(&path->list_sink, &wsink->sources);
1565 list_add(&path->list_source, &wsource->sinks);
1566 path->connect = 1;
1567 return 0;
1568 }
1569
1570 /* connect dynamic paths */
1571 switch(wsink->id) {
1572 case snd_soc_dapm_adc:
1573 case snd_soc_dapm_dac:
1574 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001575 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001576 case snd_soc_dapm_input:
1577 case snd_soc_dapm_output:
1578 case snd_soc_dapm_micbias:
1579 case snd_soc_dapm_vmid:
1580 case snd_soc_dapm_pre:
1581 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001582 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001583 case snd_soc_dapm_aif_in:
1584 case snd_soc_dapm_aif_out:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001585 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001586 list_add(&path->list_sink, &wsink->sources);
1587 list_add(&path->list_source, &wsource->sinks);
1588 path->connect = 1;
1589 return 0;
1590 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001591 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001592 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001593 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Richard Purdie2b97eab2006-10-06 18:32:18 +02001594 &wsink->kcontrols[0]);
1595 if (ret != 0)
1596 goto err;
1597 break;
1598 case snd_soc_dapm_switch:
1599 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001600 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001601 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001602 if (ret != 0)
1603 goto err;
1604 break;
1605 case snd_soc_dapm_hp:
1606 case snd_soc_dapm_mic:
1607 case snd_soc_dapm_line:
1608 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001609 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001610 list_add(&path->list_sink, &wsink->sources);
1611 list_add(&path->list_source, &wsource->sinks);
1612 path->connect = 0;
1613 return 0;
1614 }
1615 return 0;
1616
1617err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001618 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1619 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001620 kfree(path);
1621 return ret;
1622}
Mark Brown105f1c22008-05-13 14:52:19 +02001623
1624/**
Mark Brown105f1c22008-05-13 14:52:19 +02001625 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001626 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02001627 * @route: audio routes
1628 * @num: number of routes
1629 *
1630 * Connects 2 dapm widgets together via a named audio path. The sink is
1631 * the widget receiving the audio signal, whilst the source is the sender
1632 * of the audio signal.
1633 *
1634 * Returns 0 for success else error. On error all resources can be freed
1635 * with a call to snd_soc_card_free().
1636 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001637int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02001638 const struct snd_soc_dapm_route *route, int num)
1639{
1640 int i, ret;
1641
1642 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001643 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001644 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001645 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1646 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02001647 return ret;
1648 }
1649 route++;
1650 }
1651
1652 return 0;
1653}
1654EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1655
1656/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001657 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001658 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001659 *
1660 * Checks the codec for any new dapm widgets and creates them if found.
1661 *
1662 * Returns 0 for success.
1663 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001664int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001665{
1666 struct snd_soc_dapm_widget *w;
1667
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001668 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001669 {
1670 if (w->new)
1671 continue;
1672
1673 switch(w->id) {
1674 case snd_soc_dapm_switch:
1675 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001676 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001677 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001678 dapm_new_mixer(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001679 break;
1680 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001681 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001682 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001683 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001684 dapm_new_mux(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001685 break;
1686 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01001687 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01001688 w->power_check = dapm_adc_check_power;
1689 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001690 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01001691 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01001692 w->power_check = dapm_dac_check_power;
1693 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001694 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001695 case snd_soc_dapm_out_drv:
Mark Brownb75576d2009-04-20 17:56:13 +01001696 w->power_check = dapm_generic_check_power;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001697 dapm_new_pga(dapm, w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001698 break;
1699 case snd_soc_dapm_input:
1700 case snd_soc_dapm_output:
1701 case snd_soc_dapm_micbias:
1702 case snd_soc_dapm_spk:
1703 case snd_soc_dapm_hp:
1704 case snd_soc_dapm_mic:
1705 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001706 w->power_check = dapm_generic_check_power;
1707 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001708 case snd_soc_dapm_supply:
1709 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001710 case snd_soc_dapm_vmid:
1711 case snd_soc_dapm_pre:
1712 case snd_soc_dapm_post:
1713 break;
1714 }
1715 w->new = 1;
1716 }
1717
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001718 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001719 return 0;
1720}
1721EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1722
1723/**
1724 * snd_soc_dapm_get_volsw - dapm mixer get callback
1725 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001726 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001727 *
1728 * Callback to get the value of a dapm mixer control.
1729 *
1730 * Returns 0 for success.
1731 */
1732int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1733 struct snd_ctl_elem_value *ucontrol)
1734{
1735 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001736 struct soc_mixer_control *mc =
1737 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001738 unsigned int reg = mc->reg;
1739 unsigned int shift = mc->shift;
1740 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001741 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001742 unsigned int invert = mc->invert;
1743 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001744
Richard Purdie2b97eab2006-10-06 18:32:18 +02001745 ucontrol->value.integer.value[0] =
1746 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1747 if (shift != rshift)
1748 ucontrol->value.integer.value[1] =
1749 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1750 if (invert) {
1751 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001752 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001753 if (shift != rshift)
1754 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001755 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001756 }
1757
1758 return 0;
1759}
1760EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1761
1762/**
1763 * snd_soc_dapm_put_volsw - dapm mixer set callback
1764 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001765 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001766 *
1767 * Callback to set the value of a dapm mixer control.
1768 *
1769 * Returns 0 for success.
1770 */
1771int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1772 struct snd_ctl_elem_value *ucontrol)
1773{
1774 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001775 struct soc_mixer_control *mc =
1776 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001777 unsigned int reg = mc->reg;
1778 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001779 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001780 unsigned int mask = (1 << fls(max)) - 1;
1781 unsigned int invert = mc->invert;
Mark Brown97404f22010-12-14 16:13:57 +00001782 unsigned int val, val_mask;
1783 int connect, change;
1784 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001785
1786 val = (ucontrol->value.integer.value[0] & mask);
1787
1788 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001789 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001790 val_mask = mask << shift;
1791 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001792
1793 mutex_lock(&widget->codec->mutex);
1794 widget->value = val;
1795
Mark Brown97404f22010-12-14 16:13:57 +00001796 change = snd_soc_test_bits(widget->codec, reg, val_mask, val);
1797 if (change) {
Mark Brown283375c2009-12-07 18:09:03 +00001798 if (val)
1799 /* new connection */
1800 connect = invert ? 0:1;
1801 else
1802 /* old connection must be powered down */
1803 connect = invert ? 1:0;
1804
Mark Brown97404f22010-12-14 16:13:57 +00001805 update.kcontrol = kcontrol;
1806 update.widget = widget;
1807 update.reg = reg;
1808 update.mask = mask;
1809 update.val = val;
1810 widget->dapm->update = &update;
1811
Mark Brown283375c2009-12-07 18:09:03 +00001812 dapm_mixer_update_power(widget, kcontrol, connect);
Mark Brown97404f22010-12-14 16:13:57 +00001813
1814 widget->dapm->update = NULL;
Mark Brown283375c2009-12-07 18:09:03 +00001815 }
1816
Richard Purdie2b97eab2006-10-06 18:32:18 +02001817 mutex_unlock(&widget->codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00001818 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001819}
1820EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1821
1822/**
1823 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1824 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001825 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001826 *
1827 * Callback to get the value of a dapm enumerated double mixer control.
1828 *
1829 * Returns 0 for success.
1830 */
1831int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1832 struct snd_ctl_elem_value *ucontrol)
1833{
1834 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1835 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001836 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001837
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001838 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001839 ;
1840 val = snd_soc_read(widget->codec, e->reg);
1841 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1842 if (e->shift_l != e->shift_r)
1843 ucontrol->value.enumerated.item[1] =
1844 (val >> e->shift_r) & (bitmask - 1);
1845
1846 return 0;
1847}
1848EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1849
1850/**
1851 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1852 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001853 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001854 *
1855 * Callback to set the value of a dapm enumerated double mixer control.
1856 *
1857 * Returns 0 for success.
1858 */
1859int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1860 struct snd_ctl_elem_value *ucontrol)
1861{
1862 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1863 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01001864 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001865 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00001866 struct snd_soc_dapm_update update;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001867
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001868 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001869 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001870 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001871 return -EINVAL;
1872 mux = ucontrol->value.enumerated.item[0];
1873 val = mux << e->shift_l;
1874 mask = (bitmask - 1) << e->shift_l;
1875 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001876 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001877 return -EINVAL;
1878 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1879 mask |= (bitmask - 1) << e->shift_r;
1880 }
1881
1882 mutex_lock(&widget->codec->mutex);
1883 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01001884 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00001885
1886 update.kcontrol = kcontrol;
1887 update.widget = widget;
1888 update.reg = e->reg;
1889 update.mask = mask;
1890 update.val = val;
1891 widget->dapm->update = &update;
1892
Mark Brown3a655772009-10-05 17:23:30 +01001893 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01001894
Mark Brown97404f22010-12-14 16:13:57 +00001895 widget->dapm->update = NULL;
Mark Brown1642e3d2009-10-05 16:24:26 +01001896
Richard Purdie2b97eab2006-10-06 18:32:18 +02001897 mutex_unlock(&widget->codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00001898 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001899}
1900EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1901
1902/**
Mark Brownd2b247a2009-10-06 15:21:04 +01001903 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1904 * @kcontrol: mixer control
1905 * @ucontrol: control element information
1906 *
1907 * Returns 0 for success.
1908 */
1909int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1910 struct snd_ctl_elem_value *ucontrol)
1911{
1912 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1913
1914 ucontrol->value.enumerated.item[0] = widget->value;
1915
1916 return 0;
1917}
1918EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1919
1920/**
1921 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1922 * @kcontrol: mixer control
1923 * @ucontrol: control element information
1924 *
1925 * Returns 0 for success.
1926 */
1927int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
1928 struct snd_ctl_elem_value *ucontrol)
1929{
1930 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1931 struct soc_enum *e =
1932 (struct soc_enum *)kcontrol->private_value;
1933 int change;
1934 int ret = 0;
1935
1936 if (ucontrol->value.enumerated.item[0] >= e->max)
1937 return -EINVAL;
1938
1939 mutex_lock(&widget->codec->mutex);
1940
1941 change = widget->value != ucontrol->value.enumerated.item[0];
1942 widget->value = ucontrol->value.enumerated.item[0];
1943 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
1944
1945 mutex_unlock(&widget->codec->mutex);
1946 return ret;
1947}
1948EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
1949
1950/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001951 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1952 * callback
1953 * @kcontrol: mixer control
1954 * @ucontrol: control element information
1955 *
1956 * Callback to get the value of a dapm semi enumerated double mixer control.
1957 *
1958 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1959 * used for handling bitfield coded enumeration for example.
1960 *
1961 * Returns 0 for success.
1962 */
1963int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1964 struct snd_ctl_elem_value *ucontrol)
1965{
1966 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001967 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001968 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001969
1970 reg_val = snd_soc_read(widget->codec, e->reg);
1971 val = (reg_val >> e->shift_l) & e->mask;
1972 for (mux = 0; mux < e->max; mux++) {
1973 if (val == e->values[mux])
1974 break;
1975 }
1976 ucontrol->value.enumerated.item[0] = mux;
1977 if (e->shift_l != e->shift_r) {
1978 val = (reg_val >> e->shift_r) & e->mask;
1979 for (mux = 0; mux < e->max; mux++) {
1980 if (val == e->values[mux])
1981 break;
1982 }
1983 ucontrol->value.enumerated.item[1] = mux;
1984 }
1985
1986 return 0;
1987}
1988EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1989
1990/**
1991 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1992 * callback
1993 * @kcontrol: mixer control
1994 * @ucontrol: control element information
1995 *
1996 * Callback to set the value of a dapm semi enumerated double mixer control.
1997 *
1998 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1999 * used for handling bitfield coded enumeration for example.
2000 *
2001 * Returns 0 for success.
2002 */
2003int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2004 struct snd_ctl_elem_value *ucontrol)
2005{
2006 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002007 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002008 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002009 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002010 struct snd_soc_dapm_update update;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002011
2012 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2013 return -EINVAL;
2014 mux = ucontrol->value.enumerated.item[0];
2015 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2016 mask = e->mask << e->shift_l;
2017 if (e->shift_l != e->shift_r) {
2018 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2019 return -EINVAL;
2020 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2021 mask |= e->mask << e->shift_r;
2022 }
2023
2024 mutex_lock(&widget->codec->mutex);
2025 widget->value = val;
Mark Brown3a655772009-10-05 17:23:30 +01002026 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002027
2028 update.kcontrol = kcontrol;
2029 update.widget = widget;
2030 update.reg = e->reg;
2031 update.mask = mask;
2032 update.val = val;
2033 widget->dapm->update = &update;
2034
Mark Brown3a655772009-10-05 17:23:30 +01002035 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002036
Mark Brown97404f22010-12-14 16:13:57 +00002037 widget->dapm->update = NULL;
Mark Brown1642e3d2009-10-05 16:24:26 +01002038
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002039 mutex_unlock(&widget->codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002040 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002041}
2042EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2043
2044/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002045 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2046 *
2047 * @kcontrol: mixer control
2048 * @uinfo: control element information
2049 *
2050 * Callback to provide information about a pin switch control.
2051 */
2052int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2053 struct snd_ctl_elem_info *uinfo)
2054{
2055 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2056 uinfo->count = 1;
2057 uinfo->value.integer.min = 0;
2058 uinfo->value.integer.max = 1;
2059
2060 return 0;
2061}
2062EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2063
2064/**
2065 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2066 *
2067 * @kcontrol: mixer control
2068 * @ucontrol: Value
2069 */
2070int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2071 struct snd_ctl_elem_value *ucontrol)
2072{
2073 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2074 const char *pin = (const char *)kcontrol->private_value;
2075
2076 mutex_lock(&codec->mutex);
2077
2078 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002079 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002080
2081 mutex_unlock(&codec->mutex);
2082
2083 return 0;
2084}
2085EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2086
2087/**
2088 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2089 *
2090 * @kcontrol: mixer control
2091 * @ucontrol: Value
2092 */
2093int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2094 struct snd_ctl_elem_value *ucontrol)
2095{
2096 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2097 const char *pin = (const char *)kcontrol->private_value;
2098
2099 mutex_lock(&codec->mutex);
2100
2101 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002102 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002103 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002104 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002105
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002106 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002107
2108 mutex_unlock(&codec->mutex);
2109
2110 return 0;
2111}
2112EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2113
2114/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002115 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002116 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002117 * @widget: widget template
2118 *
2119 * Creates a new dapm control based upon the template.
2120 *
2121 * Returns 0 for success else error.
2122 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002123int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002124 const struct snd_soc_dapm_widget *widget)
2125{
2126 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002127 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002128
2129 if ((w = dapm_cnew_widget(widget)) == NULL)
2130 return -ENOMEM;
2131
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002132 name_len = strlen(widget->name) + 1;
2133 if (dapm->codec->name_prefix)
2134 name_len += 1 + strlen(dapm->codec->name_prefix);
2135 w->name = kmalloc(name_len, GFP_KERNEL);
2136 if (w->name == NULL) {
2137 kfree(w);
2138 return -ENOMEM;
2139 }
2140 if (dapm->codec->name_prefix)
2141 snprintf(w->name, name_len, "%s %s",
2142 dapm->codec->name_prefix, widget->name);
2143 else
2144 snprintf(w->name, name_len, "%s", widget->name);
2145
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002146 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002147 w->dapm = dapm;
2148 w->codec = dapm->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002149 INIT_LIST_HEAD(&w->sources);
2150 INIT_LIST_HEAD(&w->sinks);
2151 INIT_LIST_HEAD(&w->list);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002152 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002153
2154 /* machine layer set ups unconnected pins and insertions */
2155 w->connected = 1;
2156 return 0;
2157}
2158EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2159
2160/**
Mark Brown4ba13272008-05-13 14:51:19 +02002161 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002162 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002163 * @widget: widget array
2164 * @num: number of widgets
2165 *
2166 * Creates new DAPM controls based upon the templates.
2167 *
2168 * Returns 0 for success else error.
2169 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002170int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002171 const struct snd_soc_dapm_widget *widget,
2172 int num)
2173{
2174 int i, ret;
2175
2176 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002177 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002178 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002179 dev_err(dapm->dev,
2180 "ASoC: Failed to create DAPM control %s: %d\n",
2181 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002182 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002183 }
Mark Brown4ba13272008-05-13 14:51:19 +02002184 widget++;
2185 }
2186 return 0;
2187}
2188EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2189
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002190static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002191 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002192{
2193 struct snd_soc_dapm_widget *w;
2194
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002195 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002196 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002197 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002198 continue;
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002199 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2200 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002201 if (strstr(w->sname, stream)) {
2202 switch(event) {
2203 case SND_SOC_DAPM_STREAM_START:
2204 w->active = 1;
2205 break;
2206 case SND_SOC_DAPM_STREAM_STOP:
2207 w->active = 0;
2208 break;
2209 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002210 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002211 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002212 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2213 break;
2214 }
2215 }
2216 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002217
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002218 dapm_power_widgets(dapm, event);
2219}
2220
2221/**
2222 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2223 * @rtd: PCM runtime data
2224 * @stream: stream name
2225 * @event: stream event
2226 *
2227 * Sends a stream event to the dapm core. The core then makes any
2228 * necessary widget power changes.
2229 *
2230 * Returns 0 for success else error.
2231 */
2232int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2233 const char *stream, int event)
2234{
2235 struct snd_soc_codec *codec = rtd->codec;
2236
2237 if (stream == NULL)
2238 return 0;
2239
2240 mutex_lock(&codec->mutex);
2241 soc_dapm_stream_event(&codec->dapm, stream, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002242 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002243 return 0;
2244}
2245EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
2246
2247/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002248 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002249 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002250 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002251 *
Mark Brown74b8f952009-06-06 11:26:15 +01002252 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002253 * a valid audio route and active audio stream.
2254 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2255 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002256 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002257int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002258{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002259 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002260}
Liam Girdwooda5302182008-07-07 13:35:17 +01002261EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002262
2263/**
Mark Brownda341832010-03-15 19:23:37 +00002264 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002265 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002266 * @pin: pin name
2267 *
2268 * Enables input/output pin regardless of any other state. This is
2269 * intended for use with microphone bias supplies used in microphone
2270 * jack detection.
2271 *
2272 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2273 * do any widget power switching.
2274 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002275int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2276 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002277{
2278 struct snd_soc_dapm_widget *w;
2279
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002280 list_for_each_entry(w, &dapm->card->widgets, list) {
2281 if (w->dapm != dapm)
2282 continue;
Mark Brownda341832010-03-15 19:23:37 +00002283 if (!strcmp(w->name, pin)) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002284 dev_dbg(w->dapm->dev,
2285 "dapm: force enable pin %s\n", pin);
Mark Brownda341832010-03-15 19:23:37 +00002286 w->connected = 1;
2287 w->force = 1;
2288 return 0;
2289 }
2290 }
2291
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002292 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Mark Brownda341832010-03-15 19:23:37 +00002293 return -EINVAL;
2294}
2295EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2296
2297/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002298 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002299 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002300 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002301 *
Mark Brown74b8f952009-06-06 11:26:15 +01002302 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002303 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2304 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002305 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002306int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2307 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002308{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002309 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002310}
2311EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2312
2313/**
Mark Brown5817b522008-09-24 11:23:11 +01002314 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002315 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002316 * @pin: pin name
2317 *
2318 * Marks the specified pin as being not connected, disabling it along
2319 * any parent or child widgets. At present this is identical to
2320 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2321 * additional things such as disabling controls which only affect
2322 * paths through the pin.
2323 *
2324 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2325 * do any widget power switching.
2326 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002327int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002328{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002329 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002330}
2331EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2332
2333/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002334 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002335 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002336 * @pin: audio signal pin endpoint (or start point)
2337 *
2338 * Get audio pin status - connected or disconnected.
2339 *
2340 * Returns 1 for connected otherwise 0.
2341 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002342int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2343 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002344{
2345 struct snd_soc_dapm_widget *w;
2346
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002347 list_for_each_entry(w, &dapm->card->widgets, list) {
2348 if (w->dapm != dapm)
2349 continue;
Liam Girdwooda5302182008-07-07 13:35:17 +01002350 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002351 return w->connected;
2352 }
2353
2354 return 0;
2355}
Liam Girdwooda5302182008-07-07 13:35:17 +01002356EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002357
2358/**
Mark Brown1547aba2010-05-07 21:11:40 +01002359 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002360 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01002361 * @pin: audio signal pin endpoint (or start point)
2362 *
2363 * Mark the given endpoint or pin as ignoring suspend. When the
2364 * system is disabled a path between two endpoints flagged as ignoring
2365 * suspend will not be disabled. The path must already be enabled via
2366 * normal means at suspend time, it will not be turned on if it was not
2367 * already enabled.
2368 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002369int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2370 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01002371{
2372 struct snd_soc_dapm_widget *w;
2373
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002374 list_for_each_entry(w, &dapm->card->widgets, list) {
2375 if (w->dapm != dapm)
2376 continue;
Mark Brown1547aba2010-05-07 21:11:40 +01002377 if (!strcmp(w->name, pin)) {
2378 w->ignore_suspend = 1;
2379 return 0;
2380 }
2381 }
2382
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002383 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
Mark Brown1547aba2010-05-07 21:11:40 +01002384 return -EINVAL;
2385}
2386EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2387
2388/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002389 * snd_soc_dapm_free - free dapm resources
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002390 * @card: SoC device
Richard Purdie2b97eab2006-10-06 18:32:18 +02002391 *
2392 * Free all dapm widgets and resources.
2393 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002394void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002395{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002396 snd_soc_dapm_sys_remove(dapm->dev);
2397 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02002398 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002399}
2400EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2401
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002402static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01002403{
Mark Brown51737472009-06-22 13:16:51 +01002404 struct snd_soc_dapm_widget *w;
2405 LIST_HEAD(down_list);
2406 int powerdown = 0;
2407
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002408 list_for_each_entry(w, &dapm->card->widgets, list) {
2409 if (w->dapm != dapm)
2410 continue;
Mark Brown51737472009-06-22 13:16:51 +01002411 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00002412 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002413 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002414 powerdown = 1;
2415 }
2416 }
2417
2418 /* If there were no widgets to power down we're already in
2419 * standby.
2420 */
2421 if (powerdown) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002422 snd_soc_dapm_set_bias_level(NULL, dapm, SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00002423 dapm_seq_run(dapm, &down_list, 0, false);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002424 snd_soc_dapm_set_bias_level(NULL, dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01002425 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002426}
Mark Brown51737472009-06-22 13:16:51 +01002427
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002428/*
2429 * snd_soc_dapm_shutdown - callback for system shutdown
2430 */
2431void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2432{
2433 struct snd_soc_codec *codec;
2434
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002435 list_for_each_entry(codec, &card->codec_dev_list, list) {
2436 soc_dapm_shutdown_codec(&codec->dapm);
2437 snd_soc_dapm_set_bias_level(card, &codec->dapm, SND_SOC_BIAS_OFF);
2438 }
Mark Brown51737472009-06-22 13:16:51 +01002439}
2440
Richard Purdie2b97eab2006-10-06 18:32:18 +02002441/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002442MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002443MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2444MODULE_LICENSE("GPL");