blob: c277228ec967e3fc665abda1ea85c93a9353c9d7 [file] [log] [blame]
Richard Purdie2b97eab2006-10-06 18:32:18 +02001/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020012 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
Mark Brown74b8f952009-06-06 11:26:15 +010015 * DACs/ADCs.
Richard Purdie2b97eab2006-10-06 18:32:18 +020016 * o Platform power domain - can support external components i.e. amps and
17 * mic/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +020021 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
24 * Todo:
25 * o DAPM power change sequencing - allow for configurable per
26 * codec sequences.
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
30 */
31
32#include <linux/module.h>
33#include <linux/moduleparam.h>
34#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080035#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020036#include <linux/delay.h>
37#include <linux/pm.h>
38#include <linux/bitops.h>
39#include <linux/platform_device.h>
40#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020041#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020043#include <sound/core.h>
44#include <sound/pcm.h>
45#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020046#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020047#include <sound/initval.h>
48
Mark Brown84e90932010-11-04 00:07:02 -040049#include <trace/events/asoc.h>
50
Mark Brownde02d072011-09-20 21:43:24 +010051#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
52
Richard Purdie2b97eab2006-10-06 18:32:18 +020053/* dapm power sequences - make this per codec in the future */
54static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010055 [snd_soc_dapm_pre] = 0,
56 [snd_soc_dapm_supply] = 1,
57 [snd_soc_dapm_micbias] = 2,
Mark Brown010ff262009-08-17 17:39:22 +010058 [snd_soc_dapm_aif_in] = 3,
59 [snd_soc_dapm_aif_out] = 3,
60 [snd_soc_dapm_mic] = 4,
61 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000062 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010063 [snd_soc_dapm_value_mux] = 5,
64 [snd_soc_dapm_dac] = 6,
65 [snd_soc_dapm_mixer] = 7,
66 [snd_soc_dapm_mixer_named_ctl] = 7,
67 [snd_soc_dapm_pga] = 8,
68 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060069 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010070 [snd_soc_dapm_hp] = 10,
71 [snd_soc_dapm_spk] = 10,
72 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020073};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000074
Richard Purdie2b97eab2006-10-06 18:32:18 +020075static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010076 [snd_soc_dapm_pre] = 0,
77 [snd_soc_dapm_adc] = 1,
78 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010079 [snd_soc_dapm_spk] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060080 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010081 [snd_soc_dapm_pga] = 4,
82 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010083 [snd_soc_dapm_mixer] = 5,
84 [snd_soc_dapm_dac] = 6,
85 [snd_soc_dapm_mic] = 7,
86 [snd_soc_dapm_micbias] = 8,
87 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000088 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010089 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010090 [snd_soc_dapm_aif_in] = 10,
91 [snd_soc_dapm_aif_out] = 10,
92 [snd_soc_dapm_supply] = 11,
93 [snd_soc_dapm_post] = 12,
Richard Purdie2b97eab2006-10-06 18:32:18 +020094};
95
Troy Kisky12ef1932008-10-13 17:42:14 -070096static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +010097{
98 if (pop_time)
99 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
100}
101
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200102static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100103{
104 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200105 char *buf;
106
107 if (!pop_time)
108 return;
109
110 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
111 if (buf == NULL)
112 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100113
114 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200115 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100116 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100117 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200118
119 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100120}
121
Richard Purdie2b97eab2006-10-06 18:32:18 +0200122/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100123static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200124 const struct snd_soc_dapm_widget *_widget)
125{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100126 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200127}
128
Liam Girdwood48056082011-07-20 12:23:33 +0100129/* get snd_card from DAPM context */
130static inline struct snd_card *dapm_get_snd_card(
131 struct snd_soc_dapm_context *dapm)
132{
133 if (dapm->codec)
134 return dapm->codec->card->snd_card;
135 else if (dapm->platform)
136 return dapm->platform->card->snd_card;
137 else
138 BUG();
139
140 /* unreachable */
141 return NULL;
142}
143
144/* get soc_card from DAPM context */
145static inline struct snd_soc_card *dapm_get_soc_card(
146 struct snd_soc_dapm_context *dapm)
147{
148 if (dapm->codec)
149 return dapm->codec->card;
150 else if (dapm->platform)
151 return dapm->platform->card;
152 else
153 BUG();
154
155 /* unreachable */
156 return NULL;
157}
158
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100159static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
160{
161 if (w->codec)
162 return snd_soc_read(w->codec, reg);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100163 else if (w->platform)
164 return snd_soc_platform_read(w->platform, reg);
165
166 dev_err(w->dapm->dev, "no valid widget read method\n");
167 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100168}
169
170static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
171{
172 if (w->codec)
173 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100174 else if (w->platform)
175 return snd_soc_platform_write(w->platform, reg, val);
176
177 dev_err(w->dapm->dev, "no valid widget write method\n");
178 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100179}
180
181static int soc_widget_update_bits(struct snd_soc_dapm_widget *w,
182 unsigned short reg, unsigned int mask, unsigned int value)
183{
184 int change;
185 unsigned int old, new;
186 int ret;
187
188 ret = soc_widget_read(w, reg);
189 if (ret < 0)
190 return ret;
191
192 old = ret;
193 new = (old & ~mask) | (value & mask);
194 change = old != new;
195 if (change) {
196 ret = soc_widget_write(w, reg, new);
197 if (ret < 0)
198 return ret;
199 }
200
201 return change;
202}
203
Mark Brown452c5ea2009-05-17 21:41:23 +0100204/**
205 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800206 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100207 * @level: level to configure
208 *
209 * Configure the bias (power) levels for the SoC audio device.
210 *
211 * Returns 0 for success else error.
212 */
Mark Browned5a4c42011-02-18 11:12:42 -0800213static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200214 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100215{
Mark Browned5a4c42011-02-18 11:12:42 -0800216 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100217 int ret = 0;
218
Mark Brown84e90932010-11-04 00:07:02 -0400219 trace_snd_soc_bias_level_start(card, level);
220
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000221 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100222 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100223 if (ret != 0)
224 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100225
Mark Browncc4c6702011-06-06 19:03:34 +0100226 if (dapm->codec) {
227 if (dapm->codec->driver->set_bias_level)
228 ret = dapm->codec->driver->set_bias_level(dapm->codec,
229 level);
230 else
231 dapm->bias_level = level;
232 }
Mark Brown171ec6b2011-06-06 18:15:19 +0100233 if (ret != 0)
234 goto out;
235
236 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100237 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100238out:
Mark Brown84e90932010-11-04 00:07:02 -0400239 trace_snd_soc_bias_level_done(card, level);
240
Mark Brown452c5ea2009-05-17 21:41:23 +0100241 return ret;
242}
243
Richard Purdie2b97eab2006-10-06 18:32:18 +0200244/* set up initial codec paths */
245static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
246 struct snd_soc_dapm_path *p, int i)
247{
248 switch (w->id) {
249 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000250 case snd_soc_dapm_mixer:
251 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200252 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100253 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600254 w->kcontrol_news[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400255 unsigned int reg = mc->reg;
256 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100257 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400258 unsigned int mask = (1 << fls(max)) - 1;
259 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200260
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100261 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200262 val = (val >> shift) & mask;
263
264 if ((invert && !val) || (!invert && val))
265 p->connect = 1;
266 else
267 p->connect = 0;
268 }
269 break;
270 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600271 struct soc_enum *e = (struct soc_enum *)
272 w->kcontrol_news[i].private_value;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200273 int val, item, bitmask;
274
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100275 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Mark Brown88d96082011-06-06 16:16:34 +0100276 ;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100277 val = soc_widget_read(w, e->reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200278 item = (val >> e->shift_l) & (bitmask - 1);
279
280 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100281 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200282 if (!(strcmp(p->name, e->texts[i])) && item == i)
283 p->connect = 1;
284 }
285 }
286 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000287 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600288 struct soc_enum *e = (struct soc_enum *)
289 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000290
291 p->connect = 0;
292 /* since a virtual mux has no backing registers to
293 * decide which path to connect, it will try to match
294 * with the first enumeration. This is to ensure
295 * that the default mux choice (the first) will be
296 * correctly powered up during initialization.
297 */
298 if (!strcmp(p->name, e->texts[0]))
299 p->connect = 1;
300 }
301 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200302 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200303 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600304 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200305 int val, item;
306
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100307 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200308 val = (val >> e->shift_l) & e->mask;
309 for (item = 0; item < e->max; item++) {
310 if (val == e->values[item])
311 break;
312 }
313
314 p->connect = 0;
315 for (i = 0; i < e->max; i++) {
316 if (!(strcmp(p->name, e->texts[i])) && item == i)
317 p->connect = 1;
318 }
319 }
320 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200321 /* does not effect routing - always connected */
322 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600323 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200324 case snd_soc_dapm_output:
325 case snd_soc_dapm_adc:
326 case snd_soc_dapm_input:
327 case snd_soc_dapm_dac:
328 case snd_soc_dapm_micbias:
329 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100330 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100331 case snd_soc_dapm_aif_in:
332 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200333 p->connect = 1;
334 break;
335 /* does effect routing - dynamically connected */
336 case snd_soc_dapm_hp:
337 case snd_soc_dapm_mic:
338 case snd_soc_dapm_spk:
339 case snd_soc_dapm_line:
340 case snd_soc_dapm_pre:
341 case snd_soc_dapm_post:
342 p->connect = 0;
343 break;
344 }
345}
346
Mark Brown74b8f952009-06-06 11:26:15 +0100347/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200348static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200349 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
350 struct snd_soc_dapm_path *path, const char *control_name,
351 const struct snd_kcontrol_new *kcontrol)
352{
353 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
354 int i;
355
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100356 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200357 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200358 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200359 list_add(&path->list_sink, &dest->sources);
360 list_add(&path->list_source, &src->sinks);
361 path->name = (char*)e->texts[i];
362 dapm_set_path_status(dest, path, 0);
363 return 0;
364 }
365 }
366
367 return -ENODEV;
368}
369
Mark Brown74b8f952009-06-06 11:26:15 +0100370/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200371static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200372 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
373 struct snd_soc_dapm_path *path, const char *control_name)
374{
375 int i;
376
377 /* search for mixer kcontrol */
378 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600379 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200380 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200381 list_add(&path->list_sink, &dest->sources);
382 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600383 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200384 dapm_set_path_status(dest, path, i);
385 return 0;
386 }
387 }
388 return -ENODEV;
389}
390
Stephen Warrenaf468002011-04-28 17:38:01 -0600391static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600392 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600393 const struct snd_kcontrol_new *kcontrol_new,
394 struct snd_kcontrol **kcontrol)
395{
396 struct snd_soc_dapm_widget *w;
397 int i;
398
399 *kcontrol = NULL;
400
401 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600402 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
403 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600404 for (i = 0; i < w->num_kcontrols; i++) {
405 if (&w->kcontrol_news[i] == kcontrol_new) {
406 if (w->kcontrols)
407 *kcontrol = w->kcontrols[i];
408 return 1;
409 }
410 }
411 }
412
413 return 0;
414}
415
Richard Purdie2b97eab2006-10-06 18:32:18 +0200416/* create new dapm mixer control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200417static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200418{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200419 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200420 int i, ret = 0;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000421 size_t name_len, prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200422 struct snd_soc_dapm_path *path;
Mark Brown12ea2c72011-03-02 18:17:32 +0000423 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000424 const char *prefix;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600425 struct snd_soc_dapm_widget_list *wlist;
426 size_t wlistsize;
Mark Brownefb7ac32011-03-08 17:23:24 +0000427
428 if (dapm->codec)
429 prefix = dapm->codec->name_prefix;
430 else
431 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200432
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000433 if (prefix)
434 prefix_len = strlen(prefix) + 1;
435 else
436 prefix_len = 0;
437
Richard Purdie2b97eab2006-10-06 18:32:18 +0200438 /* add kcontrol */
439 for (i = 0; i < w->num_kcontrols; i++) {
440
441 /* match name */
442 list_for_each_entry(path, &w->sources, list_sink) {
443
444 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600445 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200446 continue;
447
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200448 if (w->kcontrols[i]) {
449 path->kcontrol = w->kcontrols[i];
450 continue;
451 }
452
Stephen Warrenfafd2172011-04-28 17:38:00 -0600453 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
454 sizeof(struct snd_soc_dapm_widget *),
455 wlist = kzalloc(wlistsize, GFP_KERNEL);
456 if (wlist == NULL) {
457 dev_err(dapm->dev,
458 "asoc: can't allocate widget list for %s\n",
459 w->name);
460 return -ENOMEM;
461 }
462 wlist->num_widgets = 1;
463 wlist->widgets[0] = w;
464
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000465 /* add dapm control with long name.
466 * for dapm_mixer this is the concatenation of the
467 * mixer and kcontrol name.
468 * for dapm_mixer_named_ctl this is simply the
469 * kcontrol name.
470 */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600471 name_len = strlen(w->kcontrol_news[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000472 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000473 name_len += 1 + strlen(w->name);
474
Mark Brown219b93f2008-10-28 13:02:31 +0000475 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000476
Stephen Warrenfafd2172011-04-28 17:38:00 -0600477 if (path->long_name == NULL) {
478 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200479 return -ENOMEM;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600480 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200481
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000482 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000483 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000484 /* The control will get a prefix from
485 * the control creation process but
486 * we're also using the same prefix
487 * for widgets so cut the prefix off
488 * the front of the widget name.
489 */
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000490 snprintf(path->long_name, name_len, "%s %s",
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000491 w->name + prefix_len,
Stephen Warren82cfecd2011-04-28 17:37:58 -0600492 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000493 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000494 case snd_soc_dapm_mixer_named_ctl:
495 snprintf(path->long_name, name_len, "%s",
Stephen Warren82cfecd2011-04-28 17:37:58 -0600496 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000497 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000498 }
499
Mark Brown219b93f2008-10-28 13:02:31 +0000500 path->long_name[name_len - 1] = '\0';
501
Stephen Warrenfafd2172011-04-28 17:38:00 -0600502 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
503 wlist, path->long_name,
504 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200505 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200506 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200507 dev_err(dapm->dev,
508 "asoc: failed to add dapm kcontrol %s: %d\n",
509 path->long_name, ret);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600510 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200511 kfree(path->long_name);
512 path->long_name = NULL;
513 return ret;
514 }
Stephen Warrenfad59882011-04-28 17:37:59 -0600515 w->kcontrols[i] = path->kcontrol;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200516 }
517 }
518 return ret;
519}
520
521/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200522static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200523{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200524 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200525 struct snd_soc_dapm_path *path = NULL;
526 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000527 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000528 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000529 size_t prefix_len;
Stephen Warrenaf468002011-04-28 17:38:01 -0600530 int ret;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600531 struct snd_soc_dapm_widget_list *wlist;
Stephen Warrenaf468002011-04-28 17:38:01 -0600532 int shared, wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600533 size_t wlistsize;
Stephen Warrenaf468002011-04-28 17:38:01 -0600534 char *name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200535
Stephen Warrenaf468002011-04-28 17:38:01 -0600536 if (w->num_kcontrols != 1) {
537 dev_err(dapm->dev,
538 "asoc: mux %s has incorrect number of controls\n",
539 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200540 return -EINVAL;
541 }
542
Stephen Warren1007da02011-05-26 09:57:33 -0600543 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
Stephen Warrenaf468002011-04-28 17:38:01 -0600544 &kcontrol);
545 if (kcontrol) {
546 wlist = kcontrol->private_data;
547 wlistentries = wlist->num_widgets + 1;
548 } else {
549 wlist = NULL;
550 wlistentries = 1;
551 }
Stephen Warrenfafd2172011-04-28 17:38:00 -0600552 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
Stephen Warrenaf468002011-04-28 17:38:01 -0600553 wlistentries * sizeof(struct snd_soc_dapm_widget *),
554 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600555 if (wlist == NULL) {
556 dev_err(dapm->dev,
557 "asoc: can't allocate widget list for %s\n", w->name);
558 return -ENOMEM;
559 }
Stephen Warrenaf468002011-04-28 17:38:01 -0600560 wlist->num_widgets = wlistentries;
561 wlist->widgets[wlistentries - 1] = w;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600562
Stephen Warrenaf468002011-04-28 17:38:01 -0600563 if (!kcontrol) {
564 if (dapm->codec)
565 prefix = dapm->codec->name_prefix;
566 else
567 prefix = NULL;
Mark Brownefb7ac32011-03-08 17:23:24 +0000568
Stephen Warrenaf468002011-04-28 17:38:01 -0600569 if (shared) {
570 name = w->kcontrol_news[0].name;
571 prefix_len = 0;
572 } else {
573 name = w->name;
574 if (prefix)
575 prefix_len = strlen(prefix) + 1;
576 else
577 prefix_len = 0;
578 }
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000579
Stephen Warrenaf468002011-04-28 17:38:01 -0600580 /*
581 * The control will get a prefix from the control creation
582 * process but we're also using the same prefix for widgets so
583 * cut the prefix off the front of the widget name.
584 */
585 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
586 name + prefix_len, prefix);
587 ret = snd_ctl_add(card, kcontrol);
588 if (ret < 0) {
Mark Brown53daf202011-09-05 10:51:05 -0700589 dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
590 w->name, ret);
Stephen Warrenaf468002011-04-28 17:38:01 -0600591 kfree(wlist);
592 return ret;
593 }
594 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200595
Stephen Warrenaf468002011-04-28 17:38:01 -0600596 kcontrol->private_data = wlist;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200597
Stephen Warrenfad59882011-04-28 17:37:59 -0600598 w->kcontrols[0] = kcontrol;
599
Richard Purdie2b97eab2006-10-06 18:32:18 +0200600 list_for_each_entry(path, &w->sources, list_sink)
601 path->kcontrol = kcontrol;
602
Stephen Warrenaf468002011-04-28 17:38:01 -0600603 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200604}
605
606/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200607static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200608{
Mark Browna6c65732010-03-03 17:45:21 +0000609 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200610 dev_err(w->dapm->dev,
611 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200612
Mark Browna6c65732010-03-03 17:45:21 +0000613 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200614}
615
616/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200617static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200618{
619 struct snd_soc_dapm_path *p;
620
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200621 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200622 p->walked = 0;
623}
624
Mark Brown99497882010-05-07 20:24:05 +0100625/* We implement power down on suspend by checking the power state of
626 * the ALSA card - when we are suspending the ALSA state for the card
627 * is set to D3.
628 */
629static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
630{
Mark Brown12ea2c72011-03-02 18:17:32 +0000631 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100632
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000633 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100634 case SNDRV_CTL_POWER_D3hot:
635 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100636 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200637 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
638 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100639 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100640 default:
641 return 1;
642 }
643}
644
Richard Purdie2b97eab2006-10-06 18:32:18 +0200645/*
646 * Recursively check for a completed path to an active or physically connected
647 * output widget. Returns number of complete paths.
648 */
649static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
650{
651 struct snd_soc_dapm_path *path;
652 int con = 0;
653
Mark Brownde02d072011-09-20 21:43:24 +0100654 DAPM_UPDATE_STAT(widget, path_checks);
655
Mark Brown246d0a12009-04-22 18:24:55 +0100656 if (widget->id == snd_soc_dapm_supply)
657 return 0;
658
Mark Brown010ff262009-08-17 17:39:22 +0100659 switch (widget->id) {
660 case snd_soc_dapm_adc:
661 case snd_soc_dapm_aif_out:
662 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100663 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100664 default:
665 break;
666 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200667
668 if (widget->connected) {
669 /* connected pin ? */
670 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100671 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200672
673 /* connected jack or spk ? */
674 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300675 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown99497882010-05-07 20:24:05 +0100676 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200677 }
678
679 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e2011-09-21 18:19:14 +0100680 DAPM_UPDATE_STAT(widget, neighbour_checks);
681
Mark Brownbf3a9e12011-06-13 16:42:29 +0100682 if (path->weak)
683 continue;
684
Richard Purdie2b97eab2006-10-06 18:32:18 +0200685 if (path->walked)
686 continue;
687
688 if (path->sink && path->connect) {
689 path->walked = 1;
690 con += is_connected_output_ep(path->sink);
691 }
692 }
693
694 return con;
695}
696
697/*
698 * Recursively check for a completed path to an active or physically connected
699 * input widget. Returns number of complete paths.
700 */
701static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
702{
703 struct snd_soc_dapm_path *path;
704 int con = 0;
705
Mark Brownde02d072011-09-20 21:43:24 +0100706 DAPM_UPDATE_STAT(widget, path_checks);
707
Mark Brown246d0a12009-04-22 18:24:55 +0100708 if (widget->id == snd_soc_dapm_supply)
709 return 0;
710
Richard Purdie2b97eab2006-10-06 18:32:18 +0200711 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100712 switch (widget->id) {
713 case snd_soc_dapm_dac:
714 case snd_soc_dapm_aif_in:
715 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100716 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100717 default:
718 break;
719 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200720
721 if (widget->connected) {
722 /* connected pin ? */
723 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100724 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200725
726 /* connected VMID/Bias for lower pops */
727 if (widget->id == snd_soc_dapm_vmid)
Mark Brown99497882010-05-07 20:24:05 +0100728 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200729
730 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300731 if (widget->id == snd_soc_dapm_mic ||
732 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown99497882010-05-07 20:24:05 +0100733 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200734 }
735
736 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e2011-09-21 18:19:14 +0100737 DAPM_UPDATE_STAT(widget, neighbour_checks);
738
Mark Brownbf3a9e12011-06-13 16:42:29 +0100739 if (path->weak)
740 continue;
741
Richard Purdie2b97eab2006-10-06 18:32:18 +0200742 if (path->walked)
743 continue;
744
745 if (path->source && path->connect) {
746 path->walked = 1;
747 con += is_connected_input_ep(path->source);
748 }
749 }
750
751 return con;
752}
753
754/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300755 * Handler for generic register modifier widget.
756 */
757int dapm_reg_event(struct snd_soc_dapm_widget *w,
758 struct snd_kcontrol *kcontrol, int event)
759{
760 unsigned int val;
761
762 if (SND_SOC_DAPM_EVENT_ON(event))
763 val = w->on_val;
764 else
765 val = w->off_val;
766
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100767 soc_widget_update_bits(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300768 w->mask << w->shift, val << w->shift);
769
770 return 0;
771}
Mark Brown11589412008-07-29 11:42:23 +0100772EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300773
Mark Browncd0f2d42009-04-20 16:56:59 +0100774/* Generic check to see if a widget should be powered.
775 */
776static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
777{
778 int in, out;
779
Mark Brownde02d072011-09-20 21:43:24 +0100780 DAPM_UPDATE_STAT(w, power_checks);
781
Mark Browncd0f2d42009-04-20 16:56:59 +0100782 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200783 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100784 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200785 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100786 return out != 0 && in != 0;
787}
788
Mark Brown6ea31b92009-04-20 17:15:41 +0100789/* Check to see if an ADC has power */
790static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
791{
792 int in;
793
Mark Brownde02d072011-09-20 21:43:24 +0100794 DAPM_UPDATE_STAT(w, power_checks);
795
Mark Brown6ea31b92009-04-20 17:15:41 +0100796 if (w->active) {
797 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200798 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100799 return in != 0;
800 } else {
801 return dapm_generic_check_power(w);
802 }
803}
804
805/* Check to see if a DAC has power */
806static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
807{
808 int out;
809
Mark Brownde02d072011-09-20 21:43:24 +0100810 DAPM_UPDATE_STAT(w, power_checks);
811
Mark Brown6ea31b92009-04-20 17:15:41 +0100812 if (w->active) {
813 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200814 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100815 return out != 0;
816 } else {
817 return dapm_generic_check_power(w);
818 }
819}
820
Mark Brown246d0a12009-04-22 18:24:55 +0100821/* Check to see if a power supply is needed */
822static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
823{
824 struct snd_soc_dapm_path *path;
825 int power = 0;
826
Mark Brownde02d072011-09-20 21:43:24 +0100827 DAPM_UPDATE_STAT(w, power_checks);
828
Mark Brown246d0a12009-04-22 18:24:55 +0100829 /* Check if one of our outputs is connected */
830 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +0100831 DAPM_UPDATE_STAT(w, neighbour_checks);
832
Mark Brownbf3a9e12011-06-13 16:42:29 +0100833 if (path->weak)
834 continue;
835
Mark Brown215edda2009-09-08 18:59:05 +0100836 if (path->connected &&
837 !path->connected(path->source, path->sink))
838 continue;
839
Mark Brown30173582011-02-11 11:42:19 +0000840 if (!path->sink)
841 continue;
842
843 if (path->sink->force) {
844 power = 1;
845 break;
846 }
847
848 if (path->sink->power_check &&
Mark Brown246d0a12009-04-22 18:24:55 +0100849 path->sink->power_check(path->sink)) {
850 power = 1;
851 break;
852 }
853 }
854
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200855 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100856
857 return power;
858}
859
Mark Brown38357ab2009-06-06 19:03:23 +0100860static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
861 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +0000862 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +0000863{
Mark Brown828a8422011-01-15 13:14:30 +0000864 int *sort;
865
866 if (power_up)
867 sort = dapm_up_seq;
868 else
869 sort = dapm_down_seq;
870
Mark Brown38357ab2009-06-06 19:03:23 +0100871 if (sort[a->id] != sort[b->id])
872 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +0000873 if (a->subseq != b->subseq) {
874 if (power_up)
875 return a->subseq - b->subseq;
876 else
877 return b->subseq - a->subseq;
878 }
Mark Brownb22ead22009-06-07 12:51:26 +0100879 if (a->reg != b->reg)
880 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +0000881 if (a->dapm != b->dapm)
882 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +0000883
Mark Brown38357ab2009-06-06 19:03:23 +0100884 return 0;
885}
Mark Brown42aa3412009-03-01 19:21:10 +0000886
Mark Brown38357ab2009-06-06 19:03:23 +0100887/* Insert a widget in order into a DAPM power sequence. */
888static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
889 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +0000890 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +0100891{
892 struct snd_soc_dapm_widget *w;
893
894 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +0000895 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +0100896 list_add_tail(&new_widget->power_list, &w->power_list);
897 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000898 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100899
Mark Brown38357ab2009-06-06 19:03:23 +0100900 list_add_tail(&new_widget->power_list, list);
901}
Mark Brown42aa3412009-03-01 19:21:10 +0000902
Mark Brown68f89ad2010-11-03 23:51:49 -0400903static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
904 struct snd_soc_dapm_widget *w, int event)
905{
906 struct snd_soc_card *card = dapm->card;
907 const char *ev_name;
908 int power, ret;
909
910 switch (event) {
911 case SND_SOC_DAPM_PRE_PMU:
912 ev_name = "PRE_PMU";
913 power = 1;
914 break;
915 case SND_SOC_DAPM_POST_PMU:
916 ev_name = "POST_PMU";
917 power = 1;
918 break;
919 case SND_SOC_DAPM_PRE_PMD:
920 ev_name = "PRE_PMD";
921 power = 0;
922 break;
923 case SND_SOC_DAPM_POST_PMD:
924 ev_name = "POST_PMD";
925 power = 0;
926 break;
927 default:
928 BUG();
929 return;
930 }
931
932 if (w->power != power)
933 return;
934
935 if (w->event && (w->event_flags & event)) {
936 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
937 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -0400938 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400939 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -0400940 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400941 if (ret < 0)
942 pr_err("%s: %s event failed: %d\n",
943 ev_name, w->name, ret);
944 }
945}
946
Mark Brownb22ead22009-06-07 12:51:26 +0100947/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200948static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +0100949 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100950{
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200951 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -0400952 struct snd_soc_dapm_widget *w;
953 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +0100954 unsigned int value = 0;
955 unsigned int mask = 0;
956 unsigned int cur_mask;
957
958 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
959 power_list)->reg;
960
961 list_for_each_entry(w, pending, power_list) {
962 cur_mask = 1 << w->shift;
963 BUG_ON(reg != w->reg);
964
965 if (w->invert)
966 power = !w->power;
967 else
968 power = w->power;
969
970 mask |= cur_mask;
971 if (power)
972 value |= cur_mask;
973
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200974 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +0100975 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
976 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100977
Mark Brown68f89ad2010-11-03 23:51:49 -0400978 /* Check for events */
979 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
980 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100981 }
982
Mark Brown81628102009-06-07 13:21:24 +0100983 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +0100984 /* Any widget will do, they should all be updating the
985 * same register.
986 */
987 w = list_first_entry(pending, struct snd_soc_dapm_widget,
988 power_list);
989
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200990 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +0100991 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200992 value, mask, reg, card->pop_time);
993 pop_wait(card->pop_time);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100994 soc_widget_update_bits(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +0100995 }
996
997 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -0400998 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
999 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001000 }
Mark Brown42aa3412009-03-01 19:21:10 +00001001}
1002
Mark Brownb22ead22009-06-07 12:51:26 +01001003/* Apply a DAPM power sequence.
1004 *
1005 * We walk over a pre-sorted list of widgets to apply power to. In
1006 * order to minimise the number of writes to the device required
1007 * multiple widgets will be updated in a single write where possible.
1008 * Currently anything that requires more than a single write is not
1009 * handled.
1010 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001011static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +00001012 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001013{
1014 struct snd_soc_dapm_widget *w, *n;
1015 LIST_HEAD(pending);
1016 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001017 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001018 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001019 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001020 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001021 int *sort;
1022
1023 if (power_up)
1024 sort = dapm_up_seq;
1025 else
1026 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001027
Mark Brownb22ead22009-06-07 12:51:26 +01001028 list_for_each_entry_safe(w, n, list, power_list) {
1029 ret = 0;
1030
1031 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001032 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001033 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001034 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001035 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001036
Mark Brown474b62d2011-01-18 16:14:44 +00001037 if (cur_dapm && cur_dapm->seq_notifier) {
1038 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1039 if (sort[i] == cur_sort)
1040 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001041 i,
1042 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001043 }
1044
Mark Brownb22ead22009-06-07 12:51:26 +01001045 INIT_LIST_HEAD(&pending);
1046 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001047 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001048 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001049 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001050 }
1051
Mark Brown163cac02009-06-07 10:12:52 +01001052 switch (w->id) {
1053 case snd_soc_dapm_pre:
1054 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001055 list_for_each_entry_safe_continue(w, n, list,
1056 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001057
Mark Brownb22ead22009-06-07 12:51:26 +01001058 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001059 ret = w->event(w,
1060 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001061 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001062 ret = w->event(w,
1063 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001064 break;
1065
1066 case snd_soc_dapm_post:
1067 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001068 list_for_each_entry_safe_continue(w, n, list,
1069 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001070
Mark Brownb22ead22009-06-07 12:51:26 +01001071 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001072 ret = w->event(w,
1073 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001074 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001075 ret = w->event(w,
1076 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001077 break;
1078
Mark Brown163cac02009-06-07 10:12:52 +01001079 default:
Mark Brown81628102009-06-07 13:21:24 +01001080 /* Queue it up for application */
1081 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001082 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001083 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001084 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001085 list_move(&w->power_list, &pending);
1086 break;
Mark Brown163cac02009-06-07 10:12:52 +01001087 }
Mark Brownb22ead22009-06-07 12:51:26 +01001088
1089 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001090 dev_err(w->dapm->dev,
1091 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001092 }
Mark Brownb22ead22009-06-07 12:51:26 +01001093
1094 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001095 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001096
1097 if (cur_dapm && cur_dapm->seq_notifier) {
1098 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1099 if (sort[i] == cur_sort)
1100 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001101 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001102 }
Mark Brown163cac02009-06-07 10:12:52 +01001103}
1104
Mark Brown97404f22010-12-14 16:13:57 +00001105static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1106{
1107 struct snd_soc_dapm_update *update = dapm->update;
1108 struct snd_soc_dapm_widget *w;
1109 int ret;
1110
1111 if (!update)
1112 return;
1113
1114 w = update->widget;
1115
1116 if (w->event &&
1117 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1118 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1119 if (ret != 0)
1120 pr_err("%s DAPM pre-event failed: %d\n",
1121 w->name, ret);
1122 }
1123
1124 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1125 update->val);
1126 if (ret < 0)
1127 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1128
1129 if (w->event &&
1130 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1131 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1132 if (ret != 0)
1133 pr_err("%s DAPM post-event failed: %d\n",
1134 w->name, ret);
1135 }
1136}
1137
Mark Brown9d0624a2011-02-18 11:49:43 -08001138/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1139 * they're changing state.
1140 */
1141static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1142{
1143 struct snd_soc_dapm_context *d = data;
1144 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001145
Mark Brown56fba412011-06-04 11:25:10 +01001146 /* If we're off and we're not supposed to be go into STANDBY */
1147 if (d->bias_level == SND_SOC_BIAS_OFF &&
1148 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001149 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1150 if (ret != 0)
1151 dev_err(d->dev,
1152 "Failed to turn on bias: %d\n", ret);
1153 }
1154
Mark Brown56fba412011-06-04 11:25:10 +01001155 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1156 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001157 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1158 if (ret != 0)
1159 dev_err(d->dev,
1160 "Failed to prepare bias: %d\n", ret);
1161 }
1162}
1163
1164/* Async callback run prior to DAPM sequences - brings to their final
1165 * state.
1166 */
1167static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1168{
1169 struct snd_soc_dapm_context *d = data;
1170 int ret;
1171
1172 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001173 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1174 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1175 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001176 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1177 if (ret != 0)
1178 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1179 ret);
1180 }
1181
1182 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001183 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1184 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001185 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1186 if (ret != 0)
1187 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1188 }
1189
1190 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001191 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1192 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001193 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1194 if (ret != 0)
1195 dev_err(d->dev, "Failed to apply active bias: %d\n",
1196 ret);
1197 }
1198}
Mark Brown97404f22010-12-14 16:13:57 +00001199
Mark Brown7c81beb2011-09-20 22:22:32 +01001200static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1201 struct list_head *up_list,
1202 struct list_head *down_list)
1203{
1204 struct snd_soc_dapm_context *d;
1205 int power;
1206
1207 switch (w->id) {
1208 case snd_soc_dapm_pre:
1209 dapm_seq_insert(w, down_list, false);
1210 break;
1211 case snd_soc_dapm_post:
1212 dapm_seq_insert(w, up_list, true);
1213 break;
1214
1215 default:
1216 if (!w->power_check)
1217 break;
1218
1219 if (!w->force)
1220 power = w->power_check(w);
1221 else
1222 power = 1;
1223
1224 if (power) {
1225 d = w->dapm;
1226
1227 /* Supplies and micbiases only bring the
1228 * context up to STANDBY as unless something
1229 * else is active and passing audio they
1230 * generally don't require full power.
1231 */
1232 switch (w->id) {
1233 case snd_soc_dapm_supply:
1234 case snd_soc_dapm_micbias:
1235 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1236 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1237 break;
1238 default:
1239 d->target_bias_level = SND_SOC_BIAS_ON;
1240 break;
1241 }
1242 }
1243
1244 if (w->power == power)
1245 break;
1246
1247 trace_snd_soc_dapm_widget_power(w, power);
1248
1249 if (power)
1250 dapm_seq_insert(w, up_list, true);
1251 else
1252 dapm_seq_insert(w, down_list, false);
1253
1254 w->power = power;
1255 break;
1256 }
1257}
1258
Mark Brown42aa3412009-03-01 19:21:10 +00001259/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001260 * Scan each dapm widget for complete audio path.
1261 * A complete path is a route that has valid endpoints i.e.:-
1262 *
1263 * o DAC to output pin.
1264 * o Input Pin to ADC.
1265 * o Input pin to Output pin (bypass, sidetone)
1266 * o DAC to ADC (loopback).
1267 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001268static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001269{
Mark Brown12ea2c72011-03-02 18:17:32 +00001270 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001271 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001272 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001273 LIST_HEAD(up_list);
1274 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001275 LIST_HEAD(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001276 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001277
Mark Brown84e90932010-11-04 00:07:02 -04001278 trace_snd_soc_dapm_start(card);
1279
Mark Brown56fba412011-06-04 11:25:10 +01001280 list_for_each_entry(d, &card->dapm_list, list) {
1281 if (d->n_widgets || d->codec == NULL) {
1282 if (d->idle_bias_off)
1283 d->target_bias_level = SND_SOC_BIAS_OFF;
1284 else
1285 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1286 }
1287 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001288
Mark Brownde02d072011-09-20 21:43:24 +01001289 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
1290
Mark Brown6d3ddc82009-05-16 17:47:29 +01001291 /* Check which widgets we need to power and store them in
1292 * lists indicating if they should be powered up or down.
1293 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001294 list_for_each_entry(w, &card->widgets, list) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001295 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001296 }
1297
Mark Brownb14b76a2009-08-17 11:55:38 +01001298 /* If there are no DAPM widgets then try to figure out power from the
1299 * event type.
1300 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001301 if (!dapm->n_widgets) {
Mark Brownb14b76a2009-08-17 11:55:38 +01001302 switch (event) {
1303 case SND_SOC_DAPM_STREAM_START:
1304 case SND_SOC_DAPM_STREAM_RESUME:
Mark Brown56fba412011-06-04 11:25:10 +01001305 dapm->target_bias_level = SND_SOC_BIAS_ON;
Mark Brownb14b76a2009-08-17 11:55:38 +01001306 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001307 case SND_SOC_DAPM_STREAM_STOP:
Mark Brown56fba412011-06-04 11:25:10 +01001308 if (dapm->codec->active)
1309 dapm->target_bias_level = SND_SOC_BIAS_ON;
1310 else
1311 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001312 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001313 case SND_SOC_DAPM_STREAM_SUSPEND:
Mark Brown56fba412011-06-04 11:25:10 +01001314 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown50b6bce2009-11-23 13:11:53 +00001315 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001316 case SND_SOC_DAPM_STREAM_NOP:
Mark Brown56fba412011-06-04 11:25:10 +01001317 dapm->target_bias_level = dapm->bias_level;
Mark Brown50b6bce2009-11-23 13:11:53 +00001318 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001319 default:
1320 break;
1321 }
1322 }
1323
Mark Brown85a843c2011-09-21 21:29:47 +01001324 /* Force all contexts in the card to the same bias state if
1325 * they're not ground referenced.
1326 */
Mark Brown56fba412011-06-04 11:25:10 +01001327 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001328 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001329 if (d->target_bias_level > bias)
1330 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001331 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001332 if (!d->idle_bias_off)
1333 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001334
Mark Brownde02d072011-09-20 21:43:24 +01001335 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001336
Mark Brown9d0624a2011-02-18 11:49:43 -08001337 /* Run all the bias changes in parallel */
1338 list_for_each_entry(d, &dapm->card->dapm_list, list)
1339 async_schedule_domain(dapm_pre_sequence_async, d,
1340 &async_domain);
1341 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001342
Mark Brown6d3ddc82009-05-16 17:47:29 +01001343 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001344 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001345
Mark Brown97404f22010-12-14 16:13:57 +00001346 dapm_widget_update(dapm);
1347
Mark Brown6d3ddc82009-05-16 17:47:29 +01001348 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001349 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001350
Mark Brown9d0624a2011-02-18 11:49:43 -08001351 /* Run all the bias changes in parallel */
1352 list_for_each_entry(d, &dapm->card->dapm_list, list)
1353 async_schedule_domain(dapm_post_sequence_async, d,
1354 &async_domain);
1355 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001356
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001357 pop_dbg(dapm->dev, card->pop_time,
1358 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001359 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001360
Mark Brown84e90932010-11-04 00:07:02 -04001361 trace_snd_soc_dapm_done(card);
1362
Mark Brown42aa3412009-03-01 19:21:10 +00001363 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001364}
1365
Mark Brown79fb9382009-08-21 16:38:13 +01001366#ifdef CONFIG_DEBUG_FS
1367static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1368{
1369 file->private_data = inode->i_private;
1370 return 0;
1371}
1372
1373static ssize_t dapm_widget_power_read_file(struct file *file,
1374 char __user *user_buf,
1375 size_t count, loff_t *ppos)
1376{
1377 struct snd_soc_dapm_widget *w = file->private_data;
1378 char *buf;
1379 int in, out;
1380 ssize_t ret;
1381 struct snd_soc_dapm_path *p = NULL;
1382
1383 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1384 if (!buf)
1385 return -ENOMEM;
1386
1387 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001388 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001389 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001390 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001391
Mark Brownd033c362009-12-04 15:25:56 +00001392 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001393 w->name, w->power ? "On" : "Off", in, out);
1394
Mark Brownd033c362009-12-04 15:25:56 +00001395 if (w->reg >= 0)
1396 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1397 " - R%d(0x%x) bit %d",
1398 w->reg, w->reg, w->shift);
1399
1400 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1401
Mark Brown3eef08b2009-09-14 16:49:00 +01001402 if (w->sname)
1403 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1404 w->sname,
1405 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001406
1407 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001408 if (p->connected && !p->connected(w, p->sink))
1409 continue;
1410
Mark Brown79fb9382009-08-21 16:38:13 +01001411 if (p->connect)
1412 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001413 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001414 p->name ? p->name : "static",
1415 p->source->name);
1416 }
1417 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001418 if (p->connected && !p->connected(w, p->sink))
1419 continue;
1420
Mark Brown79fb9382009-08-21 16:38:13 +01001421 if (p->connect)
1422 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001423 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001424 p->name ? p->name : "static",
1425 p->sink->name);
1426 }
1427
1428 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1429
1430 kfree(buf);
1431 return ret;
1432}
1433
1434static const struct file_operations dapm_widget_power_fops = {
1435 .open = dapm_widget_power_open_file,
1436 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001437 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001438};
1439
Mark Brownef49e4f2011-04-04 20:48:13 +09001440static int dapm_bias_open_file(struct inode *inode, struct file *file)
1441{
1442 file->private_data = inode->i_private;
1443 return 0;
1444}
1445
1446static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1447 size_t count, loff_t *ppos)
1448{
1449 struct snd_soc_dapm_context *dapm = file->private_data;
1450 char *level;
1451
1452 switch (dapm->bias_level) {
1453 case SND_SOC_BIAS_ON:
1454 level = "On\n";
1455 break;
1456 case SND_SOC_BIAS_PREPARE:
1457 level = "Prepare\n";
1458 break;
1459 case SND_SOC_BIAS_STANDBY:
1460 level = "Standby\n";
1461 break;
1462 case SND_SOC_BIAS_OFF:
1463 level = "Off\n";
1464 break;
1465 default:
1466 BUG();
1467 level = "Unknown\n";
1468 break;
1469 }
1470
1471 return simple_read_from_buffer(user_buf, count, ppos, level,
1472 strlen(level));
1473}
1474
1475static const struct file_operations dapm_bias_fops = {
1476 .open = dapm_bias_open_file,
1477 .read = dapm_bias_read_file,
1478 .llseek = default_llseek,
1479};
1480
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001481void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1482 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001483{
Mark Brown79fb9382009-08-21 16:38:13 +01001484 struct dentry *d;
1485
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001486 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1487
1488 if (!dapm->debugfs_dapm) {
1489 printk(KERN_WARNING
1490 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001491 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001492 }
Mark Brown79fb9382009-08-21 16:38:13 +01001493
Mark Brownef49e4f2011-04-04 20:48:13 +09001494 d = debugfs_create_file("bias_level", 0444,
1495 dapm->debugfs_dapm, dapm,
1496 &dapm_bias_fops);
1497 if (!d)
1498 dev_warn(dapm->dev,
1499 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001500}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001501
1502static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1503{
1504 struct snd_soc_dapm_context *dapm = w->dapm;
1505 struct dentry *d;
1506
1507 if (!dapm->debugfs_dapm || !w->name)
1508 return;
1509
1510 d = debugfs_create_file(w->name, 0444,
1511 dapm->debugfs_dapm, w,
1512 &dapm_widget_power_fops);
1513 if (!d)
1514 dev_warn(w->dapm->dev,
1515 "ASoC: Failed to create %s debugfs file\n",
1516 w->name);
1517}
1518
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001519static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1520{
1521 debugfs_remove_recursive(dapm->debugfs_dapm);
1522}
1523
Mark Brown79fb9382009-08-21 16:38:13 +01001524#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001525void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1526 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001527{
1528}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001529
1530static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1531{
1532}
1533
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001534static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1535{
1536}
1537
Mark Brown79fb9382009-08-21 16:38:13 +01001538#endif
1539
Richard Purdie2b97eab2006-10-06 18:32:18 +02001540/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001541static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001542 struct snd_kcontrol *kcontrol, int change,
1543 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001544{
1545 struct snd_soc_dapm_path *path;
1546 int found = 0;
1547
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001548 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001549 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001550 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001551 return -ENODEV;
1552
Mark Brown3a655772009-10-05 17:23:30 +01001553 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001554 return 0;
1555
1556 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001557 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001558 if (path->kcontrol != kcontrol)
1559 continue;
1560
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001561 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001562 continue;
1563
1564 found = 1;
1565 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001566 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001567 path->connect = 1; /* new connection */
1568 else
1569 path->connect = 0; /* old connection must be powered down */
1570 }
1571
Mark Brownb91b8fa2010-01-20 18:18:35 +00001572 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001573 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001574
1575 return 0;
1576}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001577
Milan plzik1b075e32008-01-10 14:39:46 +01001578/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001579static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001580 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001581{
1582 struct snd_soc_dapm_path *path;
1583 int found = 0;
1584
Milan plzik1b075e32008-01-10 14:39:46 +01001585 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001586 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001587 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001588 return -ENODEV;
1589
Richard Purdie2b97eab2006-10-06 18:32:18 +02001590 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001591 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001592 if (path->kcontrol != kcontrol)
1593 continue;
1594
1595 /* found, now check type */
1596 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001597 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001598 }
1599
Mark Brownb91b8fa2010-01-20 18:18:35 +00001600 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001601 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001602
1603 return 0;
1604}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001605
1606/* show dapm widget status in sys fs */
1607static ssize_t dapm_widget_show(struct device *dev,
1608 struct device_attribute *attr, char *buf)
1609{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001610 struct snd_soc_pcm_runtime *rtd =
1611 container_of(dev, struct snd_soc_pcm_runtime, dev);
1612 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001613 struct snd_soc_dapm_widget *w;
1614 int count = 0;
1615 char *state = "not set";
1616
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001617 list_for_each_entry(w, &codec->card->widgets, list) {
1618 if (w->dapm != &codec->dapm)
1619 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001620
1621 /* only display widgets that burnm power */
1622 switch (w->id) {
1623 case snd_soc_dapm_hp:
1624 case snd_soc_dapm_mic:
1625 case snd_soc_dapm_spk:
1626 case snd_soc_dapm_line:
1627 case snd_soc_dapm_micbias:
1628 case snd_soc_dapm_dac:
1629 case snd_soc_dapm_adc:
1630 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001631 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001632 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001633 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001634 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001635 if (w->name)
1636 count += sprintf(buf + count, "%s: %s\n",
1637 w->name, w->power ? "On":"Off");
1638 break;
1639 default:
1640 break;
1641 }
1642 }
1643
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001644 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001645 case SND_SOC_BIAS_ON:
1646 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001647 break;
Mark Brown0be98982008-05-19 12:31:28 +02001648 case SND_SOC_BIAS_PREPARE:
1649 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001650 break;
Mark Brown0be98982008-05-19 12:31:28 +02001651 case SND_SOC_BIAS_STANDBY:
1652 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001653 break;
Mark Brown0be98982008-05-19 12:31:28 +02001654 case SND_SOC_BIAS_OFF:
1655 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001656 break;
1657 }
1658 count += sprintf(buf + count, "PM State: %s\n", state);
1659
1660 return count;
1661}
1662
1663static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1664
1665int snd_soc_dapm_sys_add(struct device *dev)
1666{
Troy Kisky12ef1932008-10-13 17:42:14 -07001667 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001668}
1669
1670static void snd_soc_dapm_sys_remove(struct device *dev)
1671{
Mark Brownaef90842009-05-16 17:53:16 +01001672 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001673}
1674
1675/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001676static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001677{
1678 struct snd_soc_dapm_widget *w, *next_w;
1679 struct snd_soc_dapm_path *p, *next_p;
1680
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001681 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1682 if (w->dapm != dapm)
1683 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001684 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001685 /*
1686 * remove source and sink paths associated to this widget.
1687 * While removing the path, remove reference to it from both
1688 * source and sink widgets so that path is removed only once.
1689 */
1690 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1691 list_del(&p->list_sink);
1692 list_del(&p->list_source);
1693 list_del(&p->list);
1694 kfree(p->long_name);
1695 kfree(p);
1696 }
1697 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1698 list_del(&p->list_sink);
1699 list_del(&p->list_source);
1700 list_del(&p->list);
1701 kfree(p->long_name);
1702 kfree(p);
1703 }
Stephen Warrenfad59882011-04-28 17:37:59 -06001704 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001705 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001706 kfree(w);
1707 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001708}
1709
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001710static struct snd_soc_dapm_widget *dapm_find_widget(
1711 struct snd_soc_dapm_context *dapm, const char *pin,
1712 bool search_other_contexts)
1713{
1714 struct snd_soc_dapm_widget *w;
1715 struct snd_soc_dapm_widget *fallback = NULL;
1716
1717 list_for_each_entry(w, &dapm->card->widgets, list) {
1718 if (!strcmp(w->name, pin)) {
1719 if (w->dapm == dapm)
1720 return w;
1721 else
1722 fallback = w;
1723 }
1724 }
1725
1726 if (search_other_contexts)
1727 return fallback;
1728
1729 return NULL;
1730}
1731
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001732static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001733 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001734{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001735 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01001736
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001737 if (!w) {
1738 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1739 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01001740 }
1741
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001742 w->connected = status;
1743 if (status == 0)
1744 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09001745
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001746 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001747}
1748
Richard Purdie2b97eab2006-10-06 18:32:18 +02001749/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001750 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001751 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001752 *
1753 * Walks all dapm audio paths and powers widgets according to their
1754 * stream or path usage.
1755 *
1756 * Returns 0 for success.
1757 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001758int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001759{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001760 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001761}
Liam Girdwooda5302182008-07-07 13:35:17 +01001762EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001763
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001764static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001765 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001766{
1767 struct snd_soc_dapm_path *path;
1768 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001769 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001770 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001771 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001772 const char *source;
1773 char prefixed_sink[80];
1774 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001775 int ret = 0;
1776
Mark Brown88e8b9a2011-03-02 18:18:24 +00001777 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001778 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1779 dapm->codec->name_prefix, route->sink);
1780 sink = prefixed_sink;
1781 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1782 dapm->codec->name_prefix, route->source);
1783 source = prefixed_source;
1784 } else {
1785 sink = route->sink;
1786 source = route->source;
1787 }
1788
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001789 /*
1790 * find src and dest widgets over all widgets but favor a widget from
1791 * current DAPM context
1792 */
1793 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001794 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001795 wtsink = w;
1796 if (w->dapm == dapm)
1797 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001798 continue;
1799 }
1800 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001801 wtsource = w;
1802 if (w->dapm == dapm)
1803 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001804 }
1805 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001806 /* use widget from another DAPM context if not found from this */
1807 if (!wsink)
1808 wsink = wtsink;
1809 if (!wsource)
1810 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001811
1812 if (wsource == NULL || wsink == NULL)
1813 return -ENODEV;
1814
1815 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1816 if (!path)
1817 return -ENOMEM;
1818
1819 path->source = wsource;
1820 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001821 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001822 INIT_LIST_HEAD(&path->list);
1823 INIT_LIST_HEAD(&path->list_source);
1824 INIT_LIST_HEAD(&path->list_sink);
1825
1826 /* check for external widgets */
1827 if (wsink->id == snd_soc_dapm_input) {
1828 if (wsource->id == snd_soc_dapm_micbias ||
1829 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001830 wsource->id == snd_soc_dapm_line ||
1831 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001832 wsink->ext = 1;
1833 }
1834 if (wsource->id == snd_soc_dapm_output) {
1835 if (wsink->id == snd_soc_dapm_spk ||
1836 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001837 wsink->id == snd_soc_dapm_line ||
1838 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001839 wsource->ext = 1;
1840 }
1841
1842 /* connect static paths */
1843 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001844 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001845 list_add(&path->list_sink, &wsink->sources);
1846 list_add(&path->list_source, &wsource->sinks);
1847 path->connect = 1;
1848 return 0;
1849 }
1850
1851 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08001852 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001853 case snd_soc_dapm_adc:
1854 case snd_soc_dapm_dac:
1855 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001856 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001857 case snd_soc_dapm_input:
1858 case snd_soc_dapm_output:
1859 case snd_soc_dapm_micbias:
1860 case snd_soc_dapm_vmid:
1861 case snd_soc_dapm_pre:
1862 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001863 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001864 case snd_soc_dapm_aif_in:
1865 case snd_soc_dapm_aif_out:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001866 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001867 list_add(&path->list_sink, &wsink->sources);
1868 list_add(&path->list_source, &wsource->sinks);
1869 path->connect = 1;
1870 return 0;
1871 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001872 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001873 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001874 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06001875 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001876 if (ret != 0)
1877 goto err;
1878 break;
1879 case snd_soc_dapm_switch:
1880 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001881 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001882 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001883 if (ret != 0)
1884 goto err;
1885 break;
1886 case snd_soc_dapm_hp:
1887 case snd_soc_dapm_mic:
1888 case snd_soc_dapm_line:
1889 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001890 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001891 list_add(&path->list_sink, &wsink->sources);
1892 list_add(&path->list_source, &wsource->sinks);
1893 path->connect = 0;
1894 return 0;
1895 }
1896 return 0;
1897
1898err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001899 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1900 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001901 kfree(path);
1902 return ret;
1903}
Mark Brown105f1c22008-05-13 14:52:19 +02001904
1905/**
Mark Brown105f1c22008-05-13 14:52:19 +02001906 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001907 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02001908 * @route: audio routes
1909 * @num: number of routes
1910 *
1911 * Connects 2 dapm widgets together via a named audio path. The sink is
1912 * the widget receiving the audio signal, whilst the source is the sender
1913 * of the audio signal.
1914 *
1915 * Returns 0 for success else error. On error all resources can be freed
1916 * with a call to snd_soc_card_free().
1917 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001918int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02001919 const struct snd_soc_dapm_route *route, int num)
1920{
1921 int i, ret;
1922
1923 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001924 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001925 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001926 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1927 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02001928 return ret;
1929 }
1930 route++;
1931 }
1932
1933 return 0;
1934}
1935EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1936
Mark Brownbf3a9e12011-06-13 16:42:29 +01001937static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
1938 const struct snd_soc_dapm_route *route)
1939{
1940 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
1941 route->source,
1942 true);
1943 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
1944 route->sink,
1945 true);
1946 struct snd_soc_dapm_path *path;
1947 int count = 0;
1948
1949 if (!source) {
1950 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
1951 route->source);
1952 return -ENODEV;
1953 }
1954
1955 if (!sink) {
1956 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
1957 route->sink);
1958 return -ENODEV;
1959 }
1960
1961 if (route->control || route->connected)
1962 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
1963 route->source, route->sink);
1964
1965 list_for_each_entry(path, &source->sinks, list_source) {
1966 if (path->sink == sink) {
1967 path->weak = 1;
1968 count++;
1969 }
1970 }
1971
1972 if (count == 0)
1973 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
1974 route->source, route->sink);
1975 if (count > 1)
1976 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
1977 count, route->source, route->sink);
1978
1979 return 0;
1980}
1981
1982/**
1983 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
1984 * @dapm: DAPM context
1985 * @route: audio routes
1986 * @num: number of routes
1987 *
1988 * Mark existing routes matching those specified in the passed array
1989 * as being weak, meaning that they are ignored for the purpose of
1990 * power decisions. The main intended use case is for sidetone paths
1991 * which couple audio between other independent paths if they are both
1992 * active in order to make the combination work better at the user
1993 * level but which aren't intended to be "used".
1994 *
1995 * Note that CODEC drivers should not use this as sidetone type paths
1996 * can frequently also be used as bypass paths.
1997 */
1998int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
1999 const struct snd_soc_dapm_route *route, int num)
2000{
2001 int i, err;
2002 int ret = 0;
2003
2004 for (i = 0; i < num; i++) {
2005 err = snd_soc_dapm_weak_route(dapm, route);
2006 if (err)
2007 ret = err;
2008 route++;
2009 }
2010
2011 return ret;
2012}
2013EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2014
Mark Brown105f1c22008-05-13 14:52:19 +02002015/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002016 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002017 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002018 *
2019 * Checks the codec for any new dapm widgets and creates them if found.
2020 *
2021 * Returns 0 for success.
2022 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002023int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002024{
2025 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002026 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002027
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002028 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002029 {
2030 if (w->new)
2031 continue;
2032
Stephen Warrenfad59882011-04-28 17:37:59 -06002033 if (w->num_kcontrols) {
2034 w->kcontrols = kzalloc(w->num_kcontrols *
2035 sizeof(struct snd_kcontrol *),
2036 GFP_KERNEL);
2037 if (!w->kcontrols)
2038 return -ENOMEM;
2039 }
2040
Richard Purdie2b97eab2006-10-06 18:32:18 +02002041 switch(w->id) {
2042 case snd_soc_dapm_switch:
2043 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002044 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01002045 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002046 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002047 break;
2048 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002049 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002050 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01002051 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002052 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002053 break;
2054 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01002055 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01002056 w->power_check = dapm_adc_check_power;
2057 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002058 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01002059 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01002060 w->power_check = dapm_dac_check_power;
2061 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002062 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002063 case snd_soc_dapm_out_drv:
Mark Brownb75576d2009-04-20 17:56:13 +01002064 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002065 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002066 break;
2067 case snd_soc_dapm_input:
2068 case snd_soc_dapm_output:
2069 case snd_soc_dapm_micbias:
2070 case snd_soc_dapm_spk:
2071 case snd_soc_dapm_hp:
2072 case snd_soc_dapm_mic:
2073 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01002074 w->power_check = dapm_generic_check_power;
2075 break;
Mark Brown246d0a12009-04-22 18:24:55 +01002076 case snd_soc_dapm_supply:
2077 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002078 case snd_soc_dapm_vmid:
2079 case snd_soc_dapm_pre:
2080 case snd_soc_dapm_post:
2081 break;
2082 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002083
2084 /* Read the initial power state from the device */
2085 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002086 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002087 val &= 1 << w->shift;
2088 if (w->invert)
2089 val = !val;
2090
2091 if (val)
2092 w->power = 1;
2093 }
2094
Richard Purdie2b97eab2006-10-06 18:32:18 +02002095 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002096
2097 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002098 }
2099
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002100 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002101 return 0;
2102}
2103EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2104
2105/**
2106 * snd_soc_dapm_get_volsw - dapm mixer get callback
2107 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002108 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002109 *
2110 * Callback to get the value of a dapm mixer control.
2111 *
2112 * Returns 0 for success.
2113 */
2114int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2115 struct snd_ctl_elem_value *ucontrol)
2116{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002117 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2118 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002119 struct soc_mixer_control *mc =
2120 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002121 unsigned int reg = mc->reg;
2122 unsigned int shift = mc->shift;
2123 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002124 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002125 unsigned int invert = mc->invert;
2126 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002127
Richard Purdie2b97eab2006-10-06 18:32:18 +02002128 ucontrol->value.integer.value[0] =
2129 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2130 if (shift != rshift)
2131 ucontrol->value.integer.value[1] =
2132 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2133 if (invert) {
2134 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002135 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002136 if (shift != rshift)
2137 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002138 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002139 }
2140
2141 return 0;
2142}
2143EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2144
2145/**
2146 * snd_soc_dapm_put_volsw - dapm mixer set callback
2147 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002148 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002149 *
2150 * Callback to set the value of a dapm mixer control.
2151 *
2152 * Returns 0 for success.
2153 */
2154int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2155 struct snd_ctl_elem_value *ucontrol)
2156{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002157 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2158 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2159 struct snd_soc_codec *codec = widget->codec;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002160 struct soc_mixer_control *mc =
2161 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002162 unsigned int reg = mc->reg;
2163 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002164 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002165 unsigned int mask = (1 << fls(max)) - 1;
2166 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002167 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002168 int connect, change;
2169 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002170 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002171
2172 val = (ucontrol->value.integer.value[0] & mask);
2173
2174 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002175 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002176 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002177 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002178
Stephen Warrenfafd2172011-04-28 17:38:00 -06002179 if (val)
2180 /* new connection */
2181 connect = invert ? 0 : 1;
2182 else
2183 /* old connection must be powered down */
2184 connect = invert ? 1 : 0;
2185
2186 mutex_lock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002187
Stephen Warrene9cf7042011-01-27 14:54:05 -07002188 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002189 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002190 for (wi = 0; wi < wlist->num_widgets; wi++) {
2191 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002192
Stephen Warrenfafd2172011-04-28 17:38:00 -06002193 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002194
Stephen Warrenfafd2172011-04-28 17:38:00 -06002195 update.kcontrol = kcontrol;
2196 update.widget = widget;
2197 update.reg = reg;
2198 update.mask = mask;
2199 update.val = val;
2200 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002201
Stephen Warrenfafd2172011-04-28 17:38:00 -06002202 dapm_mixer_update_power(widget, kcontrol, connect);
2203
2204 widget->dapm->update = NULL;
2205 }
Mark Brown283375c2009-12-07 18:09:03 +00002206 }
2207
Stephen Warrenfafd2172011-04-28 17:38:00 -06002208 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002209 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002210}
2211EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2212
2213/**
2214 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2215 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002216 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002217 *
2218 * Callback to get the value of a dapm enumerated double mixer control.
2219 *
2220 * Returns 0 for success.
2221 */
2222int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2223 struct snd_ctl_elem_value *ucontrol)
2224{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002225 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2226 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002227 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002228 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002229
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002230 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002231 ;
2232 val = snd_soc_read(widget->codec, e->reg);
2233 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2234 if (e->shift_l != e->shift_r)
2235 ucontrol->value.enumerated.item[1] =
2236 (val >> e->shift_r) & (bitmask - 1);
2237
2238 return 0;
2239}
2240EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2241
2242/**
2243 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2244 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002245 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002246 *
2247 * Callback to set the value of a dapm enumerated double mixer control.
2248 *
2249 * Returns 0 for success.
2250 */
2251int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2252 struct snd_ctl_elem_value *ucontrol)
2253{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002254 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2255 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2256 struct snd_soc_codec *codec = widget->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002257 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002258 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002259 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00002260 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002261 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002262
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002263 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002264 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002265 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002266 return -EINVAL;
2267 mux = ucontrol->value.enumerated.item[0];
2268 val = mux << e->shift_l;
2269 mask = (bitmask - 1) << e->shift_l;
2270 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002271 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002272 return -EINVAL;
2273 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2274 mask |= (bitmask - 1) << e->shift_r;
2275 }
2276
Stephen Warrenfafd2172011-04-28 17:38:00 -06002277 mutex_lock(&codec->mutex);
2278
Mark Brown3a655772009-10-05 17:23:30 +01002279 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002280 if (change) {
2281 for (wi = 0; wi < wlist->num_widgets; wi++) {
2282 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002283
Stephen Warrenfafd2172011-04-28 17:38:00 -06002284 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002285
Stephen Warrenfafd2172011-04-28 17:38:00 -06002286 update.kcontrol = kcontrol;
2287 update.widget = widget;
2288 update.reg = e->reg;
2289 update.mask = mask;
2290 update.val = val;
2291 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002292
Stephen Warrenfafd2172011-04-28 17:38:00 -06002293 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002294
Stephen Warrenfafd2172011-04-28 17:38:00 -06002295 widget->dapm->update = NULL;
2296 }
2297 }
2298
2299 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002300 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002301}
2302EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2303
2304/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002305 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2306 * @kcontrol: mixer control
2307 * @ucontrol: control element information
2308 *
2309 * Returns 0 for success.
2310 */
2311int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2312 struct snd_ctl_elem_value *ucontrol)
2313{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002314 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2315 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002316
2317 ucontrol->value.enumerated.item[0] = widget->value;
2318
2319 return 0;
2320}
2321EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2322
2323/**
2324 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2325 * @kcontrol: mixer control
2326 * @ucontrol: control element information
2327 *
2328 * Returns 0 for success.
2329 */
2330int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2331 struct snd_ctl_elem_value *ucontrol)
2332{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002333 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2334 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2335 struct snd_soc_codec *codec = widget->codec;
Mark Brownd2b247a2009-10-06 15:21:04 +01002336 struct soc_enum *e =
2337 (struct soc_enum *)kcontrol->private_value;
2338 int change;
2339 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002340 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002341
2342 if (ucontrol->value.enumerated.item[0] >= e->max)
2343 return -EINVAL;
2344
Stephen Warrenfafd2172011-04-28 17:38:00 -06002345 mutex_lock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002346
2347 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002348 if (change) {
2349 for (wi = 0; wi < wlist->num_widgets; wi++) {
2350 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002351
Stephen Warrenfafd2172011-04-28 17:38:00 -06002352 widget->value = ucontrol->value.enumerated.item[0];
2353
2354 dapm_mux_update_power(widget, kcontrol, change,
2355 widget->value, e);
2356 }
2357 }
2358
2359 mutex_unlock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002360 return ret;
2361}
2362EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2363
2364/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002365 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2366 * callback
2367 * @kcontrol: mixer control
2368 * @ucontrol: control element information
2369 *
2370 * Callback to get the value of a dapm semi enumerated double mixer control.
2371 *
2372 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2373 * used for handling bitfield coded enumeration for example.
2374 *
2375 * Returns 0 for success.
2376 */
2377int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2378 struct snd_ctl_elem_value *ucontrol)
2379{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002380 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2381 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002382 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002383 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002384
2385 reg_val = snd_soc_read(widget->codec, e->reg);
2386 val = (reg_val >> e->shift_l) & e->mask;
2387 for (mux = 0; mux < e->max; mux++) {
2388 if (val == e->values[mux])
2389 break;
2390 }
2391 ucontrol->value.enumerated.item[0] = mux;
2392 if (e->shift_l != e->shift_r) {
2393 val = (reg_val >> e->shift_r) & e->mask;
2394 for (mux = 0; mux < e->max; mux++) {
2395 if (val == e->values[mux])
2396 break;
2397 }
2398 ucontrol->value.enumerated.item[1] = mux;
2399 }
2400
2401 return 0;
2402}
2403EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2404
2405/**
2406 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2407 * callback
2408 * @kcontrol: mixer control
2409 * @ucontrol: control element information
2410 *
2411 * Callback to set the value of a dapm semi enumerated double mixer control.
2412 *
2413 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2414 * used for handling bitfield coded enumeration for example.
2415 *
2416 * Returns 0 for success.
2417 */
2418int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2419 struct snd_ctl_elem_value *ucontrol)
2420{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002421 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2422 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2423 struct snd_soc_codec *codec = widget->codec;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002424 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002425 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002426 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002427 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002428 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002429
2430 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2431 return -EINVAL;
2432 mux = ucontrol->value.enumerated.item[0];
2433 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2434 mask = e->mask << e->shift_l;
2435 if (e->shift_l != e->shift_r) {
2436 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2437 return -EINVAL;
2438 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2439 mask |= e->mask << e->shift_r;
2440 }
2441
Stephen Warrenfafd2172011-04-28 17:38:00 -06002442 mutex_lock(&codec->mutex);
2443
Mark Brown3a655772009-10-05 17:23:30 +01002444 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002445 if (change) {
2446 for (wi = 0; wi < wlist->num_widgets; wi++) {
2447 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002448
Stephen Warrenfafd2172011-04-28 17:38:00 -06002449 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002450
Stephen Warrenfafd2172011-04-28 17:38:00 -06002451 update.kcontrol = kcontrol;
2452 update.widget = widget;
2453 update.reg = e->reg;
2454 update.mask = mask;
2455 update.val = val;
2456 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002457
Stephen Warrenfafd2172011-04-28 17:38:00 -06002458 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002459
Stephen Warrenfafd2172011-04-28 17:38:00 -06002460 widget->dapm->update = NULL;
2461 }
2462 }
2463
2464 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002465 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002466}
2467EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2468
2469/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002470 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2471 *
2472 * @kcontrol: mixer control
2473 * @uinfo: control element information
2474 *
2475 * Callback to provide information about a pin switch control.
2476 */
2477int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2478 struct snd_ctl_elem_info *uinfo)
2479{
2480 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2481 uinfo->count = 1;
2482 uinfo->value.integer.min = 0;
2483 uinfo->value.integer.max = 1;
2484
2485 return 0;
2486}
2487EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2488
2489/**
2490 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2491 *
2492 * @kcontrol: mixer control
2493 * @ucontrol: Value
2494 */
2495int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2496 struct snd_ctl_elem_value *ucontrol)
2497{
2498 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2499 const char *pin = (const char *)kcontrol->private_value;
2500
2501 mutex_lock(&codec->mutex);
2502
2503 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002504 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002505
2506 mutex_unlock(&codec->mutex);
2507
2508 return 0;
2509}
2510EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2511
2512/**
2513 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2514 *
2515 * @kcontrol: mixer control
2516 * @ucontrol: Value
2517 */
2518int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2519 struct snd_ctl_elem_value *ucontrol)
2520{
2521 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2522 const char *pin = (const char *)kcontrol->private_value;
2523
2524 mutex_lock(&codec->mutex);
2525
2526 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002527 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002528 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002529 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002530
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002531 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002532
2533 mutex_unlock(&codec->mutex);
2534
2535 return 0;
2536}
2537EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2538
2539/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002540 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002541 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002542 * @widget: widget template
2543 *
2544 * Creates a new dapm control based upon the template.
2545 *
2546 * Returns 0 for success else error.
2547 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002548int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002549 const struct snd_soc_dapm_widget *widget)
2550{
2551 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002552 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002553
2554 if ((w = dapm_cnew_widget(widget)) == NULL)
2555 return -ENOMEM;
2556
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002557 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002558 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002559 name_len += 1 + strlen(dapm->codec->name_prefix);
2560 w->name = kmalloc(name_len, GFP_KERNEL);
2561 if (w->name == NULL) {
2562 kfree(w);
2563 return -ENOMEM;
2564 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002565 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002566 snprintf(w->name, name_len, "%s %s",
2567 dapm->codec->name_prefix, widget->name);
2568 else
2569 snprintf(w->name, name_len, "%s", widget->name);
2570
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002571 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002572 w->dapm = dapm;
2573 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01002574 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002575 INIT_LIST_HEAD(&w->sources);
2576 INIT_LIST_HEAD(&w->sinks);
2577 INIT_LIST_HEAD(&w->list);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002578 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002579
2580 /* machine layer set ups unconnected pins and insertions */
2581 w->connected = 1;
2582 return 0;
2583}
2584EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2585
2586/**
Mark Brown4ba13272008-05-13 14:51:19 +02002587 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002588 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002589 * @widget: widget array
2590 * @num: number of widgets
2591 *
2592 * Creates new DAPM controls based upon the templates.
2593 *
2594 * Returns 0 for success else error.
2595 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002596int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002597 const struct snd_soc_dapm_widget *widget,
2598 int num)
2599{
2600 int i, ret;
2601
2602 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002603 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002604 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002605 dev_err(dapm->dev,
2606 "ASoC: Failed to create DAPM control %s: %d\n",
2607 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002608 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002609 }
Mark Brown4ba13272008-05-13 14:51:19 +02002610 widget++;
2611 }
2612 return 0;
2613}
2614EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2615
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002616static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002617 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002618{
2619 struct snd_soc_dapm_widget *w;
2620
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002621 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002622 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002623 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002624 continue;
Liam Girdwoodee47b362011-07-25 11:15:50 +01002625 dev_vdbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002626 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002627 if (strstr(w->sname, stream)) {
2628 switch(event) {
2629 case SND_SOC_DAPM_STREAM_START:
2630 w->active = 1;
2631 break;
2632 case SND_SOC_DAPM_STREAM_STOP:
2633 w->active = 0;
2634 break;
2635 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002636 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002637 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002638 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2639 break;
2640 }
2641 }
2642 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002643
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002644 dapm_power_widgets(dapm, event);
Liam Girdwood64a648c2011-07-25 11:15:15 +01002645
2646 /* do we need to notify any clients that DAPM stream is complete */
2647 if (dapm->stream_event)
2648 dapm->stream_event(dapm, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002649}
2650
2651/**
2652 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2653 * @rtd: PCM runtime data
2654 * @stream: stream name
2655 * @event: stream event
2656 *
2657 * Sends a stream event to the dapm core. The core then makes any
2658 * necessary widget power changes.
2659 *
2660 * Returns 0 for success else error.
2661 */
2662int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2663 const char *stream, int event)
2664{
2665 struct snd_soc_codec *codec = rtd->codec;
2666
2667 if (stream == NULL)
2668 return 0;
2669
2670 mutex_lock(&codec->mutex);
2671 soc_dapm_stream_event(&codec->dapm, stream, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002672 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002673 return 0;
2674}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002675
2676/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002677 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002678 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002679 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002680 *
Mark Brown74b8f952009-06-06 11:26:15 +01002681 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002682 * a valid audio route and active audio stream.
2683 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2684 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002685 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002686int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002687{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002688 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002689}
Liam Girdwooda5302182008-07-07 13:35:17 +01002690EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002691
2692/**
Mark Brownda341832010-03-15 19:23:37 +00002693 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002694 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002695 * @pin: pin name
2696 *
2697 * Enables input/output pin regardless of any other state. This is
2698 * intended for use with microphone bias supplies used in microphone
2699 * jack detection.
2700 *
2701 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2702 * do any widget power switching.
2703 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002704int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2705 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002706{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002707 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00002708
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002709 if (!w) {
2710 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2711 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00002712 }
2713
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002714 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2715 w->connected = 1;
2716 w->force = 1;
Mark Brown0d867332011-04-06 11:38:14 +09002717
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002718 return 0;
Mark Brownda341832010-03-15 19:23:37 +00002719}
2720EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2721
2722/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002723 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002724 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002725 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002726 *
Mark Brown74b8f952009-06-06 11:26:15 +01002727 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002728 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2729 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002730 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002731int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2732 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002733{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002734 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002735}
2736EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2737
2738/**
Mark Brown5817b522008-09-24 11:23:11 +01002739 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002740 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002741 * @pin: pin name
2742 *
2743 * Marks the specified pin as being not connected, disabling it along
2744 * any parent or child widgets. At present this is identical to
2745 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2746 * additional things such as disabling controls which only affect
2747 * paths through the pin.
2748 *
2749 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2750 * do any widget power switching.
2751 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002752int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002753{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002754 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002755}
2756EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2757
2758/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002759 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002760 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002761 * @pin: audio signal pin endpoint (or start point)
2762 *
2763 * Get audio pin status - connected or disconnected.
2764 *
2765 * Returns 1 for connected otherwise 0.
2766 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002767int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2768 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002769{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002770 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002771
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002772 if (w)
2773 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06002774
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002775 return 0;
2776}
Liam Girdwooda5302182008-07-07 13:35:17 +01002777EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002778
2779/**
Mark Brown1547aba2010-05-07 21:11:40 +01002780 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002781 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01002782 * @pin: audio signal pin endpoint (or start point)
2783 *
2784 * Mark the given endpoint or pin as ignoring suspend. When the
2785 * system is disabled a path between two endpoints flagged as ignoring
2786 * suspend will not be disabled. The path must already be enabled via
2787 * normal means at suspend time, it will not be turned on if it was not
2788 * already enabled.
2789 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002790int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2791 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01002792{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002793 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01002794
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002795 if (!w) {
2796 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2797 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01002798 }
2799
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002800 w->ignore_suspend = 1;
2801
2802 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01002803}
2804EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2805
2806/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002807 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03002808 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002809 *
2810 * Free all dapm widgets and resources.
2811 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002812void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002813{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002814 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002815 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002816 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02002817 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002818}
2819EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2820
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002821static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01002822{
Mark Brown51737472009-06-22 13:16:51 +01002823 struct snd_soc_dapm_widget *w;
2824 LIST_HEAD(down_list);
2825 int powerdown = 0;
2826
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002827 list_for_each_entry(w, &dapm->card->widgets, list) {
2828 if (w->dapm != dapm)
2829 continue;
Mark Brown51737472009-06-22 13:16:51 +01002830 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00002831 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002832 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002833 powerdown = 1;
2834 }
2835 }
2836
2837 /* If there were no widgets to power down we're already in
2838 * standby.
2839 */
2840 if (powerdown) {
Mark Browned5a4c42011-02-18 11:12:42 -08002841 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00002842 dapm_seq_run(dapm, &down_list, 0, false);
Mark Browned5a4c42011-02-18 11:12:42 -08002843 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01002844 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002845}
Mark Brown51737472009-06-22 13:16:51 +01002846
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002847/*
2848 * snd_soc_dapm_shutdown - callback for system shutdown
2849 */
2850void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2851{
2852 struct snd_soc_codec *codec;
2853
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002854 list_for_each_entry(codec, &card->codec_dev_list, list) {
2855 soc_dapm_shutdown_codec(&codec->dapm);
Mark Browned5a4c42011-02-18 11:12:42 -08002856 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002857 }
Mark Brown51737472009-06-22 13:16:51 +01002858}
2859
Richard Purdie2b97eab2006-10-06 18:32:18 +02002860/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002861MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002862MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2863MODULE_LICENSE("GPL");